/[eiffelstudio]/FreeELKS/trunk/library/kernel/memory.e
ViewVC logotype

Contents of /FreeELKS/trunk/library/kernel/memory.e

Parent Directory Parent Directory | Revision Log Revision Log


Revision 91477 - (show annotations)
Sun Jan 14 09:47:13 2007 UTC (13 years ago) by ericb
File size: 11828 byte(s)
Synchronized with ISE 6.0.65740
1 indexing
2 description: "[
3 Facilities for tuning up the garbage collection mechanism.
4 This class may be used as ancestor by classes needing its facilities.
5 ]"
6 library: "Free implementation of ELKS library"
7 copyright: "Copyright (c) 1986-2004, Eiffel Software and others"
8 license: "Eiffel Forum License v2 (see forum.txt)"
9 date: "$Date$"
10 revision: "$Revision$"
11
12 class
13 MEMORY
14
15 inherit
16 DISPOSABLE
17
18 MEM_CONST
19
20 feature -- Measurement
21
22 memory_statistics (memory_type: INTEGER): MEM_INFO is
23 -- Memory usage information for `memory_type'
24 require
25 type_ok:
26 memory_type = Total_memory or
27 memory_type = Eiffel_memory or
28 memory_type = C_memory
29 do
30 create Result.make (memory_type)
31 end
32
33 gc_statistics (collector_type: INTEGER): GC_INFO is
34 -- Garbage collection information for `collector_type'.
35 require
36 type_ok:
37 collector_type = Full_collector or
38 collector_type = Incremental_collector
39 do
40 create Result.make (collector_type)
41 end
42
43 feature -- Status report
44
45 memory_threshold: INTEGER is
46 -- Minimum amount of bytes to be allocated before
47 -- starting an automatic garbage collection.
48 external
49 "C use %"eif_memory.h%""
50 alias
51 "eif_mem_tget"
52 end
53
54 collection_period: INTEGER is
55 -- Period of full collection.
56 -- If the environment variable EIF_FULL_COLLECTION_PERIOD
57 -- is defined, it is set to the closest reasonable
58 -- value from it.
59 -- If null, no full collection is launched.
60 external
61 "C use %"eif_memory.h%""
62 alias
63 "eif_mem_pget"
64 end
65
66 coalesce_period: INTEGER is
67 -- Period of full coalesce (in number of collections)
68 -- If the environment variable EIF_FULL_COALESCE_PERIOD
69 -- is defined, it is set to the closest reasonable
70 -- value from it.
71 -- If null, no full coalescing is launched.
72 external
73 "C use %"eif_memory.h%""
74 alias
75 "eif_coalesce_period"
76 end
77
78 collecting: BOOLEAN is
79 -- Is garbage collection enabled?
80 external
81 "C use %"eif_memory.h%""
82 alias
83 "eif_gc_ison"
84 end
85
86 largest_coalesced_block: INTEGER is
87 -- Size of largest coalesced block since last call to
88 -- `largest_coalesced'; 0 if none.
89 external
90 "C use %"eif_memory.h%""
91 alias
92 "eif_mem_largest"
93 end
94
95 max_mem: INTEGER is
96 -- Maximum amount of bytes the run-time can allocate.
97 external
98 "C use %"eif_memory.h%""
99 alias
100 "eif_get_max_mem"
101 end
102
103 chunk_size: INTEGER is
104 -- Minimal size of a memory chunk. The run-time always
105 -- allocates a multiple of this size.
106 -- If the environment variable EIF_MEMORY_CHUNK
107 -- is defined, it is set to the closest reasonable
108 -- value from it.
109 external
110 "C use %"eif_memory.h%""
111 alias
112 "eif_get_chunk_size"
113 end
114
115 tenure: INTEGER is
116 -- Maximum age of object before being considered
117 -- as old (old objects are not scanned during
118 -- partial collection).
119 -- If the environment variable EIF_TENURE_MAX
120 -- is defined, it is set to the closest reasonable
121 -- value from it.
122 external
123 "C use %"eif_memory.h%""
124 alias
125 "eif_tenure"
126 end
127
128 generation_object_limit: INTEGER is
129 -- Maximum size of object in generational scavenge zone.
130 -- If the environment variable EIF_GS_LIMIT
131 -- is defined, it is set to the closest reasonable
132 -- value from it.
133 external
134 "C use %"eif_memory.h%""
135 alias
136 "eif_generation_object_limit"
137 end
138
139 scavenge_zone_size: INTEGER is
140 -- Size of generational scavenge zone.
141 -- If the environment variable EIF_MEMORY_SCAVENGE
142 -- is defined, it is set to the closest reasonable
143 -- value from it.
144
145 external
146 "C use %"eif_memory.h%""
147 alias
148 "eif_scavenge_zone_size"
149 end
150
151 feature -- Status report
152
153 referers (an_object: ANY): SPECIAL [ANY] is
154 -- Objects that refer to `an_object'.
155 do
156 Result := find_referers ($an_object, special_any_dynamic_type)
157 end
158
159 objects_instance_of (an_object: ANY): SPECIAL [ANY] is
160 -- Objects that have same dynamic type as `an_object'.
161 do
162 Result := find_instance_of (internal.dynamic_type (an_object),
163 special_any_dynamic_type)
164 end
165
166 memory_map: HASH_TABLE [ARRAYED_LIST [ANY], INTEGER] is
167 -- Retrieves all object in system as a table indexed by dynamic type
168 -- where elements are all instances of a given data type.
169 local
170 i, nb, dtype: INTEGER
171 l_spec: SPECIAL [ANY]
172 l_item: ANY
173 l_list: ARRAYED_LIST [ANY]
174 l_memory_count_map: HASH_TABLE [INTEGER, INTEGER]
175 do
176 -- First get all object instances in runtime.
177 l_spec := find_all_instances (special_any_dynamic_type)
178
179 -- Now create a memory count map of all objects. There are two reasons
180 -- why we do not simply query `memory_count_map':
181 -- 1. This would cause two calls to `find_all_instances'.
182 -- 2. The new objects created by the first call would be included in the
183 -- second list so they would not match exactly.
184
185 -- The reason why we prepass and create a memory count map is
186 -- to enable us to create the arrayed lists in `Result' with
187 -- the exact size required for their contents. Even though we now have to
188 -- perform the prepass, `memory_map' is approx 15-20% faster as
189 -- resizing the arrayed lists each time an item was addded is slow.
190
191 create l_memory_count_map.make (100)
192 from
193 i := 0
194 nb := l_spec.count
195 until
196 i >= nb
197 loop
198 l_item := l_spec.item (i)
199 if l_item /= Void then
200 dtype := internal.dynamic_type (l_item)
201 l_memory_count_map.force (l_memory_count_map.item (dtype) + 1, dtype)
202 end
203 i := i + 1
204 end
205
206 -- Now create table indexed by dynamic type. For a given
207 -- dynamic type, we will have a list of all objects of
208 -- this type.
209 create Result.make (100)
210 from
211 i := 0
212 nb := l_spec.count
213 until
214 i >= nb
215 loop
216 l_item := l_spec.item (i)
217 if l_item /= Void then
218 dtype := internal.dynamic_type (l_item)
219 Result.search (dtype)
220 if Result.found then
221 l_list := Result.found_item
222 else
223 create l_list.make_filled (l_memory_count_map.item (dtype))
224 l_list.start
225 Result.put (l_list, dtype)
226 end
227 l_list.put_i_th (l_item, l_list.index)
228 l_list.forth
229 end
230 i := i + 1
231 end
232 end
233
234 memory_count_map: HASH_TABLE [INTEGER, INTEGER] is
235 -- Number of instances per dynamic type present in system.
236 -- Same as `memory_map' except that no references on the objects themselves
237 -- is kept.
238 local
239 i, nb, dtype: INTEGER
240 l_spec: SPECIAL [ANY]
241 l_item: ANY
242 do
243 -- First get all object instances in runtime.
244 l_spec := find_all_instances (special_any_dynamic_type)
245
246 -- Now create table indexed by dynamic type. For a given
247 -- dynamic type, we will count all objects of this type.
248 create Result.make (100)
249 from
250 i := 0
251 nb := l_spec.count
252 until
253 i >= nb
254 loop
255 l_item := l_spec.item (i)
256 if l_item /= Void then
257 dtype := internal.dynamic_type (l_item)
258 Result.search (dtype)
259 if Result.found then
260 Result.force (Result.found_item + 1, dtype)
261 else
262 Result.put (1, dtype)
263 end
264 end
265 i := i + 1
266 end
267 end
268
269 feature -- Status setting
270
271 collection_off is
272 -- Disable garbage collection.
273 external
274 "C use %"eif_memory.h%""
275 alias
276 "eif_gc_stop"
277 end
278
279 collection_on is
280 -- Enable garbage collection.
281 external
282 "C use %"eif_memory.h%""
283 alias
284 "eif_gc_run"
285 end
286
287 allocate_fast is
288 -- Enter ``speed'' mode: will optimize speed of memory
289 -- allocation rather than memory usage.
290 external
291 "C use %"eif_memory.h%""
292 alias
293 "eif_mem_speed"
294 end
295
296 allocate_compact is
297 -- Enter ``memory'' mode: will try to compact memory
298 -- before requesting more from the operating system.
299 external
300 "C use %"eif_memory.h%""
301 alias
302 "eif_mem_slow"
303 end
304
305 allocate_tiny is
306 -- Enter ``tiny'' mode: will enter ``memory'' mode
307 -- after having freed as much memory as possible.
308 external
309 "C use %"eif_memory.h%""
310 alias
311 "eif_mem_tiny"
312 end
313
314 enable_time_accounting is
315 -- Enable GC time accouting, accessible in `gc_statistics'.
316 do
317 gc_monitoring (True)
318 end
319
320 disable_time_accounting is
321 -- Disable GC time accounting (default).
322 do
323 gc_monitoring (False)
324 end
325
326 set_memory_threshold (value: INTEGER) is
327 -- Set a new `memory_threshold' in bytes. Whenever the memory
328 -- allocated for Eiffel reaches this value, an automatic
329 -- collection is performed.
330 require
331 positive_value: value > 0
332 external
333 "C use %"eif_memory.h%""
334 alias
335 "eif_mem_tset"
336 end
337
338 set_collection_period (value: INTEGER) is
339 -- Set `collection_period'. Every `value' collection,
340 -- the Garbage collector will perform a collection
341 -- on the whole memory (full collection), otherwise
342 -- a simple partial collection is done.
343 require
344 positive_value: value >= 0
345 external
346 "C use %"eif_memory.h%""
347 alias
348 "eif_mem_pset"
349 end
350
351 set_coalesce_period (value: INTEGER) is
352 -- Set `coalesce_period'. Every `value' collection,
353 -- the Garbage Collector will coalesce
354 -- the whole memory.
355 require
356 positive_value: value >= 0
357 external
358 "C use %"eif_memory.h%""
359 alias
360 "eif_set_coalesce_period"
361 end
362
363 set_max_mem (value: INTEGER) is
364 -- Set the maximum amount of memory the run-time can allocate.
365 require
366 positive_value: value > 0
367 external
368 "C use %"eif_memory.h%""
369 alias
370 "eif_set_max_mem"
371 end
372
373 feature -- Removal
374
375 dispose is
376 -- Action to be executed just before garbage collection
377 -- reclaims an object.
378 -- Default version does nothing; redefine in descendants
379 -- to perform specific dispose actions. Those actions
380 -- should only take care of freeing external resources;
381 -- they should not perform remote calls on other objects
382 -- since these may also be dead and reclaimed.
383 do
384 end
385
386 free (object: ANY) is
387 -- Free `object', by-passing garbage collection.
388 -- Erratic behavior will result if the object is still
389 -- referenced.
390 do
391 mem_free ($object)
392 end
393
394 mem_free (addr: POINTER) is
395 -- Free memory of object at `addr'.
396 -- (Preferred interface is `free'.)
397 external
398 "C signature (EIF_REFERENCE) use %"eif_memory.h%""
399 alias
400 "eif_mem_free"
401 end
402
403 full_coalesce is
404 -- Coalesce the whole memory: merge adjacent free
405 -- blocks to reduce fragmentation. Useful, when
406 -- a lot of memory is allocated with garbage collector off.
407 external
408 "C use %"eif_memory.h%""
409 alias
410 "eif_mem_coalesc"
411 end
412
413 collect is
414 -- Force a partial collection cycle if garbage
415 -- collection is enabled; do nothing otherwise.
416 external
417 "C use %"eif_memory.h%""
418 end
419
420 full_collect is
421 -- Force a full collection cycle if garbage
422 -- collection is enabled; do nothing otherwise.
423 external
424 "C use %"eif_memory.h%""
425 alias
426 "plsc"
427 end
428
429 feature {NONE} -- Implementation
430
431 gc_monitoring (flag: BOOLEAN) is
432 -- Set up GC monitoring according to `flag'
433 external
434 "C use %"eif_memory.h%""
435 alias
436 "gc_mon"
437 end
438
439 find_referers (target: POINTER; result_type: INTEGER): SPECIAL [ANY] is
440 external
441 "C signature (EIF_REFERENCE, EIF_INTEGER): EIF_REFERENCE use %"eif_traverse.h%""
442 end
443
444 find_instance_of (dtype, result_type: INTEGER): SPECIAL [ANY] is
445 external
446 "C signature (EIF_INTEGER, EIF_INTEGER): EIF_REFERENCE use %"eif_traverse.h%""
447 end
448
449 find_all_instances (result_type: INTEGER): SPECIAL [ANY] is
450 external
451 "C signature (EIF_INTEGER): EIF_REFERENCE use %"eif_traverse.h%""
452 end
453
454 special_any_dynamic_type: INTEGER is
455 -- Dynamic type ID of an instance of `SPECIAL [ANY]'
456 local
457 a: ARRAY [ANY]
458 spec: SPECIAL [ANY]
459 once
460 create a.make (0, 0)
461 spec := a.area
462 Result := internal.dynamic_type (spec)
463 end
464
465 internal: INTERNAL is
466 -- Internal features
467 once
468 create Result
469 ensure
470 internal_not_void: Result /= Void
471 end
472
473 end

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.23