Source file newProfile.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
module MiniJson = struct
type t =[
| `Intlit of string
| `String of string
| `Assoc of (string * t) list
| `List of t list
]
let prstring ch s =
let buf = Buffer.create (String.length s) in
let encode c =
match c with
| '"' -> Buffer.add_string buf "\\\""
| '\\' -> Buffer.add_string buf "\\\\"
| '\b' -> Buffer.add_string buf "\\b"
| '\012' -> Buffer.add_string buf "\\f"
| '\n' -> Buffer.add_string buf "\\n"
| '\r' -> Buffer.add_string buf "\\r"
| '\t' -> Buffer.add_string buf "\\t"
| '\x00'..'\x1F'
| '\x7F' ->
Buffer.add_string buf (Printf.sprintf "\\u%04x" (Char.code c))
| _ -> Buffer.add_char buf c
in
String.iter encode s;
Format.fprintf ch "\"%s\"" (Buffer.contents buf)
let rec pr ch : t -> _ = function
| `String s -> prstring ch s
| `Intlit s -> Format.fprintf ch "%s" s
| `Assoc elts ->
Format.fprintf ch "{ %a }" prrecord elts
| `List elts ->
Format.fprintf ch "[\n%a\n]" prarray elts
and prrecord ch = function
| [] -> ()
| [(x,v)] -> Format.fprintf ch "%a: %a" prstring x pr v
| (x,v) :: l -> Format.fprintf ch "%a: %a, %a" prstring x pr v prrecord l
and prarray ch = function
| [] -> ()
| [x] -> pr ch x
| x :: l -> Format.fprintf ch "%a,\n%a" pr x prarray l
let pid = Unix.getpid()
let pids = string_of_int pid
let base = [("pid", `Intlit pids); ("tid", `Intlit pids)]
let duration ~name ~ph ~ts ?args () =
let l = ("name", `String name) :: ("ph", `String ph) :: ("ts", `Intlit ts) :: base in
let l = match args with
| None -> l
| Some args -> ("args", `Assoc args) :: l
in
`Assoc l
end
type accu = {
ch : Format.formatter;
mutable sums : (float * (float * int) CString.Map.t) list;
}
let accu = ref None
let is_profiling () = Option.has_some !accu
let f fmt = match !accu with
| None -> assert false
| Some { ch } -> Format.fprintf ch fmt
let gettime = Unix.gettimeofday
let gettimeopt = function
| Some t -> t
| None -> gettime()
let prtime t =
Format.sprintf "%.0f" (t *. 1E6)
module Counters = struct
type t = {
major_words : float;
minor_words : float;
major_collections : int;
minor_collections : int;
instr : System.instruction_count;
}
let get () =
let gc = Gc.quick_stat() in
{
major_words = gc.major_words;
minor_words = gc.minor_words;
major_collections = gc.major_collections;
minor_collections = gc.minor_collections;
instr = Instr.read_counter();
}
let global_start = get ()
let (-) b a = {
major_words = b.major_words -. a.major_words;
minor_words = b.minor_words -. a.minor_words;
major_collections = b.major_collections - a.major_collections;
minor_collections = b.minor_collections - a.minor_collections;
instr = System.instructions_between ~c_start:a.instr ~c_end:b.instr;
}
let format x =
let ppf tdiff = `String (Format.sprintf "%.3G w" tdiff) in
let ppi i = `Intlit (string_of_int i) in
let instr = match x.instr with
| Ok count -> [("instr", `Intlit (Int64.to_string count))]
| Error _ -> []
in
("major_words",ppf x.major_words) ::
("minor_words", ppf x.minor_words) ::
("major_collect", ppi x.major_collections) ::
("minor_collect", ppi x.minor_collections) ::
instr
let make_diffs ~start ~stop = format (stop - start)
end
let global_start_time = gettime ()
let duration ~time name ph ?args ?(last=",") () =
f "%a%s\n" MiniJson.pr (MiniJson.duration ~name ~ph ~ts:(prtime time) ?args ()) last
let enter_sums ?time () =
let accu = Option.get !accu in
let time = gettimeopt time in
accu.sums <- (time, CString.Map.empty) :: accu.sums
let enter ?time name ?args () =
let time = gettimeopt time in
enter_sums ~time ();
duration ~time name "B" ?args ()
let leave_sums ?time name () =
let accu = Option.get !accu in
let time = gettimeopt time in
match accu.sums with
| [] -> assert false
| [start,sum] -> accu.sums <- []; sum, time -. start
| (start, sum) :: (start', next) :: rest ->
let dur = time -. start in
let next = CString.Map.update name (function
| None -> Some (dur, 1)
| Some (dur', cnt) -> Some (dur +. dur', cnt+1))
next
in
let next = CString.Map.union (fun name' (l,cnt) (r,cnt') ->
if String.equal name name' then Some (r,cnt+cnt')
else Some (l +. r, cnt+cnt'))
sum next
in
accu. sums <- (start', next) :: rest;
sum, dur
let leave ?time name ?(args=[]) ?last () =
let time = gettimeopt time in
let sum, dur = leave_sums ~time name () in
let sum = List.map (fun (name, (t, cnt)) ->
name, `String
(Format.sprintf "%.3G us, %d %s" (t *. 1E6) cnt (CString.plural cnt "call")))
(CString.Map.bindings sum)
in
let args = ("subtimes", `Assoc sum) :: args in
duration ~time name "E" ~args ?last ()
let components =
match Sys.getenv_opt "COQ_PROFILE_COMPONENTS" with
| None -> CString.Pred.(full |> remove "unification" |> remove "Conversion")
| Some s ->
List.fold_left (fun cs c -> CString.Pred.add c cs)
CString.Pred.empty
(String.split_on_char ',' s)
let profile name ?args f () =
if not (is_profiling ()) then f ()
else if CString.Pred.mem name components then begin
let args = Option.map (fun f -> f()) args in
enter name ?args ();
let start = Counters.get () in
let v = try f ()
with e ->
let e = Exninfo.capture e in
let args = Counters.make_diffs ~start ~stop:(Counters.get()) in
leave name ~args ();
Exninfo.iraise e
in
let args = Counters.make_diffs ~start ~stop:(Counters.get()) in
leave name ~args ();
v
end
else begin
enter_sums ();
let v = try f ()
with e ->
let e = Exninfo.capture e in
ignore (leave_sums name () : _ * _);
Exninfo.iraise e
in
ignore (leave_sums name () : _ * _);
v
end
type settings =
{ output : Format.formatter
}
let init { output } =
let () = assert (not (is_profiling())) in
accu := Some { ch = output; sums = [] };
f "{ \"traceEvents\": [\n";
enter ~time:global_start_time "process" ();
enter ~time:global_start_time "init" ();
let args = Counters.(make_diffs ~start:global_start ~stop:(get())) in
leave "init" ~args ()
let pause () =
let v = !accu in
accu := None;
v
let resume v =
assert (not (is_profiling()));
accu := Some v
let finish () = match !accu with
| None -> assert false
| Some { ch } ->
let args = Counters.(make_diffs ~start:global_start ~stop:(get())) in
leave "process" ~last:"" ~args ();
Format.fprintf ch "],\n\"displayTimeUnit\": \"us\" }";
accu := None