-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperf.ml
275 lines (262 loc) · 12 KB
/
perf.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
type timer = Mtime_clock.counter
let pp_timer (f : Format.formatter) (_timer : timer) =
Format.fprintf f "(timer)"
type timed_counter = {
start_count : int;
end_count : int;
top_level_timer : timer option;
top_level_time : Mtime.Span.t;
top_level_end_count : int;
}
[@@deriving show]
let empty_timed_counter =
{
start_count = 0;
end_count = 0;
top_level_timer = None;
top_level_time = Mtime.Span.zero;
top_level_end_count = 0;
}
type counters = {
(* Special fields *)
enable_printing : bool ref;
reset_at : timer ref;
(* Normal counters *)
interpret_non_phi_instruction : timed_counter ref;
handle_separately_no_phi : timed_counter ref;
instruction_alloc : timed_counter ref;
instruction_get_global : timed_counter ref;
instruction_load : timed_counter ref;
instruction_store : timed_counter ref;
instruction_store_empty_table : timed_counter ref;
instruction_store_closure : timed_counter ref;
instruction_get_field : timed_counter ref;
instruction_get_index : timed_counter ref;
instruction_constant : timed_counter ref;
instruction_unary_op : timed_counter ref;
instruction_binary_op : timed_counter ref;
call_misc_internals : timed_counter ref;
builtin_call : timed_counter ref;
closure_call : timed_counter ref;
closure_call_noop : int ref;
closure_call_prepare_inner_states : timed_counter ref;
closure_call_process_inner_results : timed_counter ref;
gc : timed_counter ref;
normalize_state_maps_except_heap : timed_counter ref;
flow_join : timed_counter ref;
flow_accumulate : timed_counter ref;
flow_analyze : timed_counter ref;
flow_analyze_flow_block_phi : timed_counter ref;
flow_analyze_flow_block_before_join : timed_counter ref;
flow_analyze_flow_block_post_phi : timed_counter ref;
flow_analyze_flow_branch : timed_counter ref;
flow_analyze_flow_return : timed_counter ref;
fixpoint : timed_counter ref;
fixpoint_prepare : timed_counter ref;
fixpoint_internals : timed_counter ref;
cbr_filter : timed_counter ref;
filter_vector : int ref;
filter_vector_real : int ref;
(* Counters per Lua function *)
lua_functions : timed_counter ref list ref;
}
[@@deriving show]
let global_counters : counters =
{
enable_printing = ref false;
reset_at = ref @@ Mtime_clock.counter ();
interpret_non_phi_instruction = ref empty_timed_counter;
handle_separately_no_phi = ref empty_timed_counter;
instruction_alloc = ref empty_timed_counter;
instruction_get_global = ref empty_timed_counter;
instruction_load = ref empty_timed_counter;
instruction_store = ref empty_timed_counter;
instruction_store_empty_table = ref empty_timed_counter;
instruction_store_closure = ref empty_timed_counter;
instruction_get_field = ref empty_timed_counter;
instruction_get_index = ref empty_timed_counter;
instruction_constant = ref empty_timed_counter;
instruction_unary_op = ref empty_timed_counter;
instruction_binary_op = ref empty_timed_counter;
call_misc_internals = ref empty_timed_counter;
builtin_call = ref empty_timed_counter;
closure_call = ref empty_timed_counter;
closure_call_noop = ref 0;
closure_call_prepare_inner_states = ref empty_timed_counter;
closure_call_process_inner_results = ref empty_timed_counter;
gc = ref empty_timed_counter;
normalize_state_maps_except_heap = ref empty_timed_counter;
flow_join = ref empty_timed_counter;
flow_accumulate = ref empty_timed_counter;
flow_analyze = ref empty_timed_counter;
flow_analyze_flow_block_phi = ref empty_timed_counter;
flow_analyze_flow_block_before_join = ref empty_timed_counter;
flow_analyze_flow_block_post_phi = ref empty_timed_counter;
flow_analyze_flow_branch = ref empty_timed_counter;
flow_analyze_flow_return = ref empty_timed_counter;
fixpoint = ref empty_timed_counter;
fixpoint_prepare = ref empty_timed_counter;
fixpoint_internals = ref empty_timed_counter;
cbr_filter = ref empty_timed_counter;
filter_vector = ref 0;
filter_vector_real = ref 0;
lua_functions = ref [];
}
let usecs_of_span span =
let nsecs = Mtime.Span.to_uint64_ns span in
Int64.to_int @@ Int64.div nsecs 1000L
let secs_of_span span = (float_of_int @@ usecs_of_span span) /. 1_000_000.0
let show_timed_counter (counter : timed_counter) =
Printf.sprintf
"start_count: %d, end_count: %d, top_level_time: %fs%s, \
top_level_end_count: %d"
counter.start_count counter.end_count
(secs_of_span counter.top_level_time)
(match counter.top_level_timer with
| Some timer ->
Printf.sprintf " + %fs" @@ secs_of_span @@ Mtime_clock.count timer
| None -> "")
counter.top_level_end_count
let print_counters () =
Printf.printf "Performance counters (%f seconds total):\n"
@@ secs_of_span
@@ Mtime_clock.count !(global_counters.reset_at);
Printf.printf " interpret_non_phi_instruction: %s\n"
@@ show_timed_counter !(global_counters.interpret_non_phi_instruction);
Printf.printf " handle_separately_no_phi: %s\n"
@@ show_timed_counter !(global_counters.handle_separately_no_phi);
Printf.printf " instruction_alloc: %s\n"
@@ show_timed_counter !(global_counters.instruction_alloc);
Printf.printf " instruction_get_global: %s\n"
@@ show_timed_counter !(global_counters.instruction_get_global);
Printf.printf " instruction_load: %s\n"
@@ show_timed_counter !(global_counters.instruction_load);
Printf.printf " instruction_store: %s\n"
@@ show_timed_counter !(global_counters.instruction_store);
Printf.printf " instruction_store_empty_table: %s\n"
@@ show_timed_counter !(global_counters.instruction_store_empty_table);
Printf.printf " instruction_store_closure: %s\n"
@@ show_timed_counter !(global_counters.instruction_store_closure);
Printf.printf " instruction_get_field: %s\n"
@@ show_timed_counter !(global_counters.instruction_get_field);
Printf.printf " instruction_get_index: %s\n"
@@ show_timed_counter !(global_counters.instruction_get_index);
Printf.printf " instruction_constant: %s\n"
@@ show_timed_counter !(global_counters.instruction_constant);
Printf.printf " instruction_unary_op: %s\n"
@@ show_timed_counter !(global_counters.instruction_unary_op);
Printf.printf " instruction_binary_op: %s\n"
@@ show_timed_counter !(global_counters.instruction_binary_op);
Printf.printf " call_misc_internals: %s\n"
@@ show_timed_counter !(global_counters.call_misc_internals);
Printf.printf " builtin_call: %s\n"
@@ show_timed_counter !(global_counters.builtin_call);
Printf.printf " closure_call: %s\n"
@@ show_timed_counter !(global_counters.closure_call);
Printf.printf " closure_call_noop: %d\n" !(global_counters.closure_call_noop);
Printf.printf " closure_call_prepare_inner_states: %s\n"
@@ show_timed_counter !(global_counters.closure_call_prepare_inner_states);
Printf.printf " closure_call_process_inner_results: %s\n"
@@ show_timed_counter !(global_counters.closure_call_process_inner_results);
Printf.printf " gc: %s\n" @@ show_timed_counter !(global_counters.gc);
Printf.printf " normalize_state_maps_except_heap: %s\n"
@@ show_timed_counter !(global_counters.normalize_state_maps_except_heap);
Printf.printf " flow_join: %s\n"
@@ show_timed_counter !(global_counters.flow_join);
Printf.printf " flow_accumulate: %s\n"
@@ show_timed_counter !(global_counters.flow_accumulate);
Printf.printf " flow_analyze: %s\n"
@@ show_timed_counter !(global_counters.flow_analyze);
Printf.printf " flow_analyze_flow_block_phi: %s\n"
@@ show_timed_counter !(global_counters.flow_analyze_flow_block_phi);
Printf.printf " flow_analyze_flow_block_before_join: %s\n"
@@ show_timed_counter !(global_counters.flow_analyze_flow_block_before_join);
Printf.printf " flow_analyze_flow_block_post_phi: %s\n"
@@ show_timed_counter !(global_counters.flow_analyze_flow_block_post_phi);
Printf.printf " flow_analyze_flow_branch: %s\n"
@@ show_timed_counter !(global_counters.flow_analyze_flow_branch);
Printf.printf " flow_analyze_flow_return: %s\n"
@@ show_timed_counter !(global_counters.flow_analyze_flow_return);
Printf.printf " fixpoint %s\n"
@@ show_timed_counter !(global_counters.fixpoint);
Printf.printf " fixpoint_prepare: %s\n"
@@ show_timed_counter !(global_counters.fixpoint_prepare);
Printf.printf " fixpoint_internals %s\n"
@@ show_timed_counter !(global_counters.fixpoint_internals);
Printf.printf " cbr_filter: %s\n"
@@ show_timed_counter !(global_counters.cbr_filter);
Printf.printf " filter_vector: %d\n" !(global_counters.filter_vector);
Printf.printf " filter_vector_real: %d\n"
!(global_counters.filter_vector_real);
Printf.printf "%!"
let print_named_counters (counters : (string * timed_counter) list) =
let counters =
counters
|> List.filter (fun (_, counter) -> counter.start_count > 0)
|> List.sort (fun (_, a) (_, b) ->
compare b.top_level_time a.top_level_time)
in
Printf.printf "Named performance counters:\n";
List.iter
(fun (name, counter) ->
Printf.printf " %s: %s\n" name @@ show_timed_counter counter)
counters;
Printf.printf "%!"
let reset_counters () =
global_counters.reset_at := Mtime_clock.counter ();
global_counters.interpret_non_phi_instruction := empty_timed_counter;
global_counters.handle_separately_no_phi := empty_timed_counter;
global_counters.instruction_alloc := empty_timed_counter;
global_counters.instruction_get_global := empty_timed_counter;
global_counters.instruction_load := empty_timed_counter;
global_counters.instruction_store := empty_timed_counter;
global_counters.instruction_store_empty_table := empty_timed_counter;
global_counters.instruction_store_closure := empty_timed_counter;
global_counters.instruction_get_field := empty_timed_counter;
global_counters.instruction_get_index := empty_timed_counter;
global_counters.instruction_constant := empty_timed_counter;
global_counters.instruction_unary_op := empty_timed_counter;
global_counters.instruction_binary_op := empty_timed_counter;
global_counters.call_misc_internals := empty_timed_counter;
global_counters.builtin_call := empty_timed_counter;
global_counters.closure_call := empty_timed_counter;
global_counters.closure_call_noop := 0;
global_counters.closure_call_prepare_inner_states := empty_timed_counter;
global_counters.closure_call_process_inner_results := empty_timed_counter;
global_counters.gc := empty_timed_counter;
global_counters.normalize_state_maps_except_heap := empty_timed_counter;
global_counters.flow_join := empty_timed_counter;
global_counters.flow_accumulate := empty_timed_counter;
global_counters.flow_analyze := empty_timed_counter;
global_counters.flow_analyze_flow_block_phi := empty_timed_counter;
global_counters.flow_analyze_flow_block_before_join := empty_timed_counter;
global_counters.flow_analyze_flow_block_post_phi := empty_timed_counter;
global_counters.flow_analyze_flow_branch := empty_timed_counter;
global_counters.flow_analyze_flow_return := empty_timed_counter;
global_counters.fixpoint := empty_timed_counter;
global_counters.fixpoint_prepare := empty_timed_counter;
global_counters.fixpoint_internals := empty_timed_counter;
global_counters.cbr_filter := empty_timed_counter;
global_counters.filter_vector := 0;
global_counters.filter_vector_real := 0;
List.iter (fun c -> c := empty_timed_counter) !(global_counters.lua_functions)
let count_and_time_with_options ~(allow_recursion : bool)
(c : timed_counter ref) f =
let is_top_level = Option.is_none !c.top_level_timer in
assert (allow_recursion || is_top_level);
c := { !c with start_count = !c.start_count + 1 };
let timer = Mtime_clock.counter () in
if is_top_level then c := { !c with top_level_timer = Some timer };
let res = f () in
let span = Mtime_clock.count timer in
c := { !c with end_count = !c.end_count + 1 };
if is_top_level then
c :=
{
!c with
top_level_timer = None;
top_level_time = Mtime.Span.add !c.top_level_time span;
top_level_end_count = !c.top_level_end_count + 1;
};
res
let count_and_time c f = count_and_time_with_options ~allow_recursion:true c f