Debian Codegraph @

clib/cObj.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 (*s Logical and physical size of ocaml values. *)
12
13 (** {6 Logical sizes} *)
14
15 let c = ref 0
16 let s = ref 0
17 let b = ref 0
18 let m = ref 0
19
20 let rec obj_stats d t =
21 if Obj.is_int t then m := max d !m
22 else if Obj.tag t >= Obj.no_scan_tag then
23 if Obj.tag t = Obj.string_tag then
24 (c := !c + Obj.size t; b := !b + 1; m := max d !m)
25 else if Obj.tag t = Obj.double_tag then
26 (s := !s + 2; b := !b + 1; m := max d !m)
27 else if Obj.tag t = Obj.double_array_tag then
28 (s := !s + 2 * Obj.size t; b := !b + 1; m := max d !m)
29 else (b := !b + 1; m := max d !m)
30 else
31 let n = Obj.size t in
32 s := !s + n; b := !b + 1;
33 block_stats (d + 1) (n - 1) t
34
35 and block_stats d i t =
36 if i >= 0 then (obj_stats d (Obj.field t i); block_stats d (i-1) t)
37
38 let obj_stats a =
39 c := 0; s:= 0; b:= 0; m:= 0;
40 obj_stats 0 (Obj.repr a);
41 (!c, !s + !b, !m)
42
43 (** {6 Physical sizes} *)
44
45 (*s Pointers already visited are stored in a hash-table, where
46 comparisons are done using physical equality. *)
47
48 module H = Hashtbl.Make(
49 struct
50 type t = Obj.t
51 let equal = (==)
52 let hash = Hashtbl.hash
53 end)
54
55 let node_table = (H.create 257 : unit H.t)
56
57 let in_table o = try H.find node_table o; true with Not_found -> false
58
59 let add_in_table o = H.add node_table o ()
60
61 let reset_table () = H.clear node_table
62
63 (*s Objects are traversed recursively, as soon as their tags are less than
64 [no_scan_tag]. [count] records the numbers of words already visited. *)
65
66 let size_of_double = Obj.size (Obj.repr 1.0)
67
68 let count = ref 0
69
70 let rec traverse t =
71 if not (in_table t) && Obj.is_block t then begin
72 add_in_table t;
73 let n = Obj.size t in
74 let tag = Obj.tag t in
75 if tag < Obj.no_scan_tag then
76 begin
77 count := !count + 1 + n;
78 for i = 0 to n - 1 do traverse (Obj.field t i) done
79 end
80 else if tag = Obj.string_tag then
81 count := !count + 1 + n
82 else if tag = Obj.double_tag then
83 count := !count + size_of_double
84 else if tag = Obj.double_array_tag then
85 count := !count + 1 + size_of_double * n
86 else
87 incr count
88 end
89
90 (*s Sizes of objects in words and in bytes. The size in bytes is computed
91 system-independently according to [Sys.word_size]. *)
92
93 let size o =
94 reset_table ();
95 count := 0;
96 traverse (Obj.repr o);
97 !count
98
99 let size_b o = (size o) * (Sys.word_size / 8)
100
101 let size_kb o = (size o) / (8192 / Sys.word_size)
102
103 (** {6 Physical sizes with sharing} *)
104
105 (** This time, all the size of objects are computed with respect
106 to a larger object containing them all, and we only count
107 the new blocks not already seen earlier in the left-to-right
108 visit of the englobing object.
109
110 The very same object could have a zero size or not, depending
111 of the occurrence we're considering in the englobing object.
112 For speaking of occurrences, we use an [int list] for a path
113 of field indexes from the outmost block to the one we're looking.
114 In the list, the leftmost integer is the field index in the deepest
115 block.
116 *)
117
118 (** We now store in the hashtable the size (with sharing), and
119 also the position of the first occurrence of the object *)
120
121 let node_sizes = (H.create 257 : (int*int list) H.t)
122 let get_size o = H.find node_sizes o
123 let add_size o n pos = H.replace node_sizes o (n,pos)
124 let reset_sizes () = H.clear node_sizes
125 let global_object = ref (Obj.repr 0)
126
127 (** [sum n f] is [f 0 + f 1 + ... + f (n-1)], evaluated from left to right *)
128
129 let sum n f =
130 let rec loop k acc = if k >= n then acc else loop (k+1) (acc + f k)
131 in loop 0 0
132
133 (** Recursive visit of the main object, filling the hashtable *)
134
135 let rec compute_size o pos =
136 if not (Obj.is_block o) then 0
137 else
138 try
139 let _ = get_size o in 0 (* already seen *)
140 with Not_found ->
141 let n = Obj.size o in
142 add_size o (-1) pos (* temp size, for cyclic values *);
143 let tag = Obj.tag o in
144 let size =
145 if tag < Obj.no_scan_tag then
146 1 + n + sum n (fun i -> compute_size (Obj.field o i) (i::pos))
147 else if tag = Obj.string_tag then
148 1 + n
149 else if tag = Obj.double_tag then
150 size_of_double
151 else if tag = Obj.double_array_tag then
152 size_of_double * n
153 else
154 1
155 in
156 add_size o size pos;
157 size
158
159 (** Provides the global object in which we'll search shared sizes *)
160
161 let register_shared_size t =
162 let o = Obj.repr t in
163 reset_sizes ();
164 global_object := o;
165 ignore (compute_size o [])
166
167 (** Shared size of an object with respect to the global object given
168 by the last [register_shared_size] *)
169
170 let shared_size pos o =
171 if not (Obj.is_block o) then 0
172 else
173 let size,pos' =
174 try get_size o
175 with Not_found -> failwith "shared_size: unregistered structure ?"
176 in
177 match pos with
178 | Some p when p <> pos' -> 0
179 | _ -> size
180
181 let shared_size_of_obj t = shared_size None (Obj.repr t)
182
183 (** Shared size of the object at some positiion in the global object given
184 by the last [register_shared_size] *)
185
186 let shared_size_of_pos pos =
187 let rec obj_of_pos o = function
188 | [] -> o
189 | n::pos' ->
190 let o' = obj_of_pos o pos' in
191 assert (Obj.is_block o' && n < Obj.size o');
192 Obj.field o' n
193 in
194 shared_size (Some pos) (obj_of_pos !global_object pos)
195
196
197 (*s Total size of the allocated ocaml heap. *)
198
199 let heap_size () =
200 let stat = Gc.stat ()
201 and control = Gc.get () in
202 let max_words_total = stat.Gc.heap_words + control.Gc.minor_heap_size in
203 (max_words_total * (Sys.word_size / 8))
204
205 let heap_size_kb () = (heap_size () + 1023) / 1024