]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/ocaml/contrib/stdlib/gc.mli
Inital import
[l4.git] / l4 / pkg / ocaml / contrib / stdlib / gc.mli
1 (***********************************************************************)
2 (*                                                                     *)
3 (*                           Objective Caml                            *)
4 (*                                                                     *)
5 (*             Damien Doligez, projet Para, INRIA Rocquencourt         *)
6 (*                                                                     *)
7 (*  Copyright 1996 Institut National de Recherche en Informatique et   *)
8 (*  en Automatique.  All rights reserved.  This file is distributed    *)
9 (*  under the terms of the GNU Library General Public License, with    *)
10 (*  the special exception on linking described in file ../LICENSE.     *)
11 (*                                                                     *)
12 (***********************************************************************)
13
14 (* $Id: gc.mli 9131 2008-11-18 10:24:43Z doligez $ *)
15
16 (** Memory management control and statistics; finalised values. *)
17
18 type stat =
19   { minor_words : float;
20     (** Number of words allocated in the minor heap since
21        the program was started.  This number is accurate in
22        byte-code programs, but only an approximation in programs
23        compiled to native code. *)
24
25     promoted_words : float;
26     (** Number of words allocated in the minor heap that
27        survived a minor collection and were moved to the major heap
28        since the program was started. *)
29
30     major_words : float;
31     (** Number of words allocated in the major heap, including
32        the promoted words, since the program was started. *)
33
34     minor_collections : int;
35     (** Number of minor collections since the program was started. *)
36
37     major_collections : int;
38     (** Number of major collection cycles completed since the program
39         was started. *)
40
41     heap_words : int;
42     (** Total size of the major heap, in words. *)
43
44     heap_chunks : int;
45     (** Number of contiguous pieces of memory that make up the major heap. *)
46
47     live_words : int;
48     (** Number of words of live data in the major heap, including the header
49        words. *)
50
51     live_blocks : int;
52     (** Number of live blocks in the major heap. *)
53
54     free_words : int;
55     (** Number of words in the free list. *)
56
57     free_blocks : int;
58     (** Number of blocks in the free list. *)
59
60     largest_free : int;
61     (** Size (in words) of the largest block in the free list. *)
62
63     fragments : int;
64     (** Number of wasted words due to fragmentation.  These are
65        1-words free blocks placed between two live blocks.  They
66        are not available for allocation. *)
67
68     compactions : int;
69     (** Number of heap compactions since the program was started. *)
70
71     top_heap_words : int;
72     (** Maximum size reached by the major heap, in words. *)
73 }
74 (** The memory management counters are returned in a [stat] record.
75
76    The total amount of memory allocated by the program since it was started
77    is (in words) [minor_words + major_words - promoted_words].  Multiply by
78    the word size (4 on a 32-bit machine, 8 on a 64-bit machine) to get
79    the number of bytes.
80 *)
81
82 type control =
83   { mutable minor_heap_size : int;
84     (** The size (in words) of the minor heap.  Changing
85        this parameter will trigger a minor collection.  Default: 32k. *)
86
87     mutable major_heap_increment : int;
88     (** The minimum number of words to add to the
89        major heap when increasing it.  Default: 124k. *)
90
91     mutable space_overhead : int;
92     (** The major GC speed is computed from this parameter.
93        This is the memory that will be "wasted" because the GC does not
94        immediatly collect unreachable blocks.  It is expressed as a
95        percentage of the memory used for live data.
96        The GC will work more (use more CPU time and collect
97        blocks more eagerly) if [space_overhead] is smaller.
98        Default: 80. *)
99
100     mutable verbose : int;
101     (** This value controls the GC messages on standard error output.
102        It is a sum of some of the following flags, to print messages
103        on the corresponding events:
104        - [0x001] Start of major GC cycle.
105        - [0x002] Minor collection and major GC slice.
106        - [0x004] Growing and shrinking of the heap.
107        - [0x008] Resizing of stacks and memory manager tables.
108        - [0x010] Heap compaction.
109        - [0x020] Change of GC parameters.
110        - [0x040] Computation of major GC slice size.
111        - [0x080] Calling of finalisation functions.
112        - [0x100] Bytecode executable search at start-up.
113        - [0x200] Computation of compaction triggering condition.
114        Default: 0. *)
115
116     mutable max_overhead : int;
117     (** Heap compaction is triggered when the estimated amount
118        of "wasted" memory is more than [max_overhead] percent of the
119        amount of live data.  If [max_overhead] is set to 0, heap
120        compaction is triggered at the end of each major GC cycle
121        (this setting is intended for testing purposes only).
122        If [max_overhead >= 1000000], compaction is never triggered.
123        Default: 500. *)
124
125     mutable stack_limit : int;
126     (** The maximum size of the stack (in words).  This is only
127        relevant to the byte-code runtime, as the native code runtime
128        uses the operating system's stack.  Default: 256k. *)
129
130     mutable allocation_policy : int;
131     (** The policy used for allocating in the heap.  Possible
132         values are 0 and 1.  0 is the next-fit policy, which is
133         quite fast but can result in fragmentation.  1 is the
134         first-fit policy, which can be slower in some cases but
135         can be better for programs with fragmentation problems.
136         Default: 0. *)
137 }
138 (** The GC parameters are given as a [control] record.  Note that
139     these parameters can also be initialised by setting the
140     OCAMLRUNPARAM environment variable.  See the documentation of
141     ocamlrun. *)
142
143 external stat : unit -> stat = "caml_gc_stat"
144 (** Return the current values of the memory management counters in a
145    [stat] record.  This function examines every heap block to get the
146    statistics. *)
147
148 external quick_stat : unit -> stat = "caml_gc_quick_stat"
149 (** Same as [stat] except that [live_words], [live_blocks], [free_words],
150     [free_blocks], [largest_free], and [fragments] are set to 0.  This
151     function is much faster than [stat] because it does not need to go
152     through the heap. *)
153
154 external counters : unit -> float * float * float = "caml_gc_counters"
155 (** Return [(minor_words, promoted_words, major_words)].  This function
156     is as fast at [quick_stat]. *)
157
158 external get : unit -> control = "caml_gc_get"
159 (** Return the current values of the GC parameters in a [control] record. *)
160
161 external set : control -> unit = "caml_gc_set"
162 (** [set r] changes the GC parameters according to the [control] record [r].
163    The normal usage is: [Gc.set { (Gc.get()) with Gc.verbose = 0x00d }] *)
164
165 external minor : unit -> unit = "caml_gc_minor"
166 (** Trigger a minor collection. *)
167
168 external major_slice : int -> int = "caml_gc_major_slice";;
169 (** Do a minor collection and a slice of major collection.  The argument
170     is the size of the slice, 0 to use the automatically-computed
171     slice size.  In all cases, the result is the computed slice size. *)
172
173 external major : unit -> unit = "caml_gc_major"
174 (** Do a minor collection and finish the current major collection cycle. *)
175
176 external full_major : unit -> unit = "caml_gc_full_major"
177 (** Do a minor collection, finish the current major collection cycle,
178    and perform a complete new cycle.  This will collect all currently
179    unreachable blocks. *)
180
181 external compact : unit -> unit = "caml_gc_compaction"
182 (** Perform a full major collection and compact the heap.  Note that heap
183    compaction is a lengthy operation. *)
184
185 val print_stat : out_channel -> unit
186 (** Print the current values of the memory management counters (in
187    human-readable form) into the channel argument. *)
188
189 val allocated_bytes : unit -> float
190 (** Return the total number of bytes allocated since the program was
191    started.  It is returned as a [float] to avoid overflow problems
192    with [int] on 32-bit machines. *)
193
194 val finalise : ('a -> unit) -> 'a -> unit
195 (** [finalise f v] registers [f] as a finalisation function for [v].
196    [v] must be heap-allocated.  [f] will be called with [v] as
197    argument at some point between the first time [v] becomes unreachable
198    and the time [v] is collected by the GC.  Several functions can
199    be registered for the same value, or even several instances of the
200    same function.  Each instance will be called once (or never,
201    if the program terminates before [v] becomes unreachable).
202
203    The GC will call the finalisation functions in the order of
204    deallocation.  When several values become unreachable at the
205    same time (i.e. during the same GC cycle), the finalisation
206    functions will be called in the reverse order of the corresponding
207    calls to [finalise].  If [finalise] is called in the same order
208    as the values are allocated, that means each value is finalised
209    before the values it depends upon.  Of course, this becomes
210    false if additional dependencies are introduced by assignments.
211
212    Anything reachable from the closure of finalisation functions
213    is considered reachable, so the following code will not work
214    as expected:
215    - [ let v = ... in Gc.finalise (fun x -> ...) v ]
216
217    Instead you should write:
218    - [ let f = fun x -> ... ;; let v = ... in Gc.finalise f v ]
219
220
221    The [f] function can use all features of O'Caml, including
222    assignments that make the value reachable again.  It can also
223    loop forever (in this case, the other
224    finalisation functions will be called during the execution of f).
225    It can call [finalise] on [v] or other values to register other
226    functions or even itself.  It can raise an exception; in this case
227    the exception will interrupt whatever the program was doing when
228    the function was called.
229
230
231    [finalise] will raise [Invalid_argument] if [v] is not
232    heap-allocated.  Some examples of values that are not
233    heap-allocated are integers, constant constructors, booleans,
234    the empty array, the empty list, the unit value.  The exact list
235    of what is heap-allocated or not is implementation-dependent.
236    Some constant values can be heap-allocated but never deallocated
237    during the lifetime of the program, for example a list of integer
238    constants; this is also implementation-dependent.
239    You should also be aware that compiler optimisations may duplicate
240    some immutable values, for example floating-point numbers when
241    stored into arrays, so they can be finalised and collected while
242    another copy is still in use by the program.
243
244
245    The results of calling {!String.make}, {!String.create},
246    {!Array.make}, and {!Pervasives.ref} are guaranteed to be
247    heap-allocated and non-constant except when the length argument is [0].
248 *)
249
250 val finalise_release : unit -> unit;;
251 (** A finalisation function may call [finalise_release] to tell the
252     GC that it can launch the next finalisation function without waiting
253     for the current one to return. *)
254
255 type alarm
256 (** An alarm is a piece of data that calls a user function at the end of
257    each major GC cycle.  The following functions are provided to create
258    and delete alarms. *)
259
260 val create_alarm : (unit -> unit) -> alarm
261 (** [create_alarm f] will arrange for [f] to be called at the end of each
262    major GC cycle, starting with the current cycle or the next one.
263    A value of type [alarm] is returned that you can
264    use to call [delete_alarm]. *)
265
266 val delete_alarm : alarm -> unit
267 (** [delete_alarm a] will stop the calls to the function associated
268    to [a].  Calling [delete_alarm a] again has no effect. *)