Source file profiling.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
open Format
open Options
module SE = Expr.Set
module MS = Map.Make(String)
type inst_info =
{
loc : Loc.t;
kept : int;
ignored : int;
all_insts : SE.t;
confl : int;
decided : int;
consumed : SE.t;
all : SE.t;
produced : SE.t;
_new : SE.t;
}
type t = {
decisions : int ref;
assumes : int ref;
assumes_current_lvl : int ref;
queries : int ref;
instantiation_rounds : int ref;
instances : int ref;
decision_lvl : int ref;
instantiation_lvl : int ref;
th_conflicts : int ref;
b_conflicts : int ref;
bcp_th_conflicts : int ref;
bcp_b_conflicts : int ref;
bcp_mix_conflicts : int ref;
t_red : int ref;
b_red : int ref;
t_elim : int ref;
b_elim : int ref;
instances_map : inst_info MS.t ref;
instances_map_printed : bool ref
}
let state =
{ decisions = ref 0;
assumes = ref 0;
assumes_current_lvl = ref 0;
queries = ref 0;
instantiation_rounds = ref 0;
instances = ref 0;
decision_lvl = ref 0;
instantiation_lvl = ref 0;
th_conflicts = ref 0;
b_conflicts = ref 0;
bcp_th_conflicts = ref 0;
bcp_b_conflicts = ref 0;
bcp_mix_conflicts = ref 0;
t_red = ref 0;
b_red = ref 0;
t_elim = ref 0;
b_elim = ref 0;
instances_map = ref MS.empty;
instances_map_printed = ref false
}
let set_sigprof () =
let tm =
let v = Options.profiling_period () in
if (Stdlib.compare v 0.) = 1 then v else -. v
in
ignore
(Unix.setitimer Unix.ITIMER_PROF
{ Unix.it_value = tm; Unix.it_interval = 0. })
let init () =
state.decisions := 0;
state.assumes := 0;
state.queries := 0;
state.instantiation_rounds := 0;
state.instances := 0;
state.decision_lvl := 0;
state.instantiation_lvl := 0;
state.assumes_current_lvl := 0;
state.th_conflicts := 0;
state.b_conflicts := 0;
state.bcp_th_conflicts := 0;
state.bcp_b_conflicts := 0;
state.bcp_mix_conflicts := 0;
state.t_red := 0;
state.b_red := 0;
state.t_elim := 0;
state.b_elim := 0;
state.instances_map := MS.empty;
state.instances_map_printed := false;
set_sigprof ()
let assume nb =
state.assumes := nb + !(state.assumes);
state.assumes_current_lvl := nb + !(state.assumes_current_lvl)
let query () =
incr state.queries
let instances l =
state.instances := !(state.instances) + List.length l
let instantiation ilvl =
incr state.instantiation_rounds;
incr state.instantiation_lvl;
if not (!(state.instantiation_lvl) = ilvl) then begin
Format.eprintf "state.instantiation_lvl = %d et ilvl = %d@."
!(state.instantiation_lvl) ilvl;
assert false
end
let bool_conflict () =
incr state.b_conflicts
let theory_conflict () =
incr state.th_conflicts
let bcp_conflict b1 b2 =
if b1 && b2 then incr state.bcp_b_conflicts
else if (not b1) && (not b2) then incr state.bcp_th_conflicts
else incr state.bcp_mix_conflicts
let red b = if b then incr state.b_red else incr state.t_red
let elim b = if b then incr state.b_elim else incr state.t_elim
let reset_ilevel n = state.instantiation_lvl := n
let reset_dlevel n = state.decision_lvl := n
let empty_inst_info loc =
{
loc = loc;
kept = 0;
ignored = 0;
confl = 0;
decided = 0;
all_insts = SE.empty;
consumed = SE.empty;
all = SE.empty;
produced = SE.empty;
_new = SE.empty;
}
let new_instance_of axiom inst loc kept =
let () = state.instances_map_printed := false in
let ii =
try MS.find axiom !(state.instances_map)
with Not_found -> empty_inst_info loc
in
assert (ii.loc == loc);
let ii =
if kept then
{ii with kept = ii.kept + 1; all_insts = SE.add inst ii.all_insts}
else
{ii with ignored = ii.ignored + 1}
in
state.instances_map := MS.add axiom ii !(state.instances_map)
let conflicting_instance axiom loc =
let ii =
try MS.find axiom !(state.instances_map)
with Not_found -> empty_inst_info loc
in
let ii = {ii with confl = ii.confl + 1} in
assert (ii.loc == loc);
state.instances_map := MS.add axiom ii !(state.instances_map)
let decision_on_instance axiom_name =
try
let ii =
MS.find axiom_name !(state.instances_map)
in
let ii = {ii with decided = ii.decided + 1} in
state.instances_map := MS.add axiom_name ii !(state.instances_map)
with Not_found -> ()
let decision d origin =
incr state.decisions;
incr state.decision_lvl;
if not (!(state.decision_lvl) = d) then begin
Format.eprintf "state.decision_lvl = %d et d = %d@."
!(state.decision_lvl) d;
assert false
end;
state.assumes_current_lvl := 0;
decision_on_instance origin
let register_produced_terms axiom loc consumed all produced _new =
let ii =
try MS.find axiom !(state.instances_map)
with Not_found -> empty_inst_info loc
in
assert (ii.loc == loc);
let ii =
{ii with
consumed = SE.union ii.consumed consumed;
all = SE.union ii.all all;
produced = SE.union ii.produced produced;
_new = SE.union ii._new _new }
in
state.instances_map := MS.add axiom ii !(state.instances_map)
type mode =
| Stats
| Timers
| CallTree
| FunctionsTimers
| Instances
let mode = ref Stats
let max_nb_prints = 30
let nb_prints = ref max_nb_prints
let initial_info = ref true
let string_resize s i =
let sz = max 0 (i - (String.length s)) in
let tmp = String.make sz ' ' in
s ^ tmp
let int_resize n i = string_resize (sprintf "%d" n) i
let float_resize f i = string_resize (sprintf "%f" f) i
let percent total a =
(string_of_int (int_of_float (a *. 100. /. total))) ^ "%"
let columns =
[
"GTimer", "Global timer", 11, None,
(fun _ gtime _ sz -> float_resize gtime sz);
"Steps", "Number of Steps", 14, None,
(fun steps gtime _ sz ->
let avg = int_of_float (Int64.to_float steps /. gtime) in
sprintf "%s~%s"
(string_resize (sprintf "%Ld" steps) (sz-7))
(string_resize (sprintf "%d/s" avg) 6)
);
"Case splits", "Number of Case Splits", 14, None,
(fun _ gtime _ sz ->
let avg = int_of_float (float_of_int (Options.cs_steps()) /. gtime) in
sprintf "%s~%s"
(string_resize (sprintf "%d" (Options.cs_steps())) (sz-7))
(string_resize (sprintf "%d/s" avg) 6)
);
"Mod.", "Current active module", 7, None,
(fun _ _ timers sz ->
let kd, _msg, _ = Timers.current_timer timers in
string_resize (Timers.string_of_ty_module kd) sz);
"Module Id", "Each call to a module is tagged with a fresh Id", 10, None,
(fun _ _ timers sz ->
let _kd, _msg, id = Timers.current_timer timers in
int_resize id sz);
"ilvl", "Current Instantiaton level", 6, Some true,
(fun _ _ _ sz -> int_resize !(state.instantiation_lvl) sz);
"#i rnds", "Number of intantiation rounds", 8, Some true,
(fun _ _ _ sz ->
int_resize !(state.instantiation_rounds) sz);
"#insts", "Number of generated instances", 8, Some true,
(fun _ _ _ sz -> int_resize !(state.instances) sz);
"i/r", "AVG number of generated instances per instantiation rounds",
8, Some true,
(fun _ _ _ sz ->
int_resize
(!(state.instances) / (max 1 !(state.instantiation_rounds))) sz);
"dlvl", "Current Decision level", 6, Some true,
(fun _ _ _ sz -> int_resize !(state.decision_lvl) sz);
"#decs", "Number of decisions", 6, Some true,
(fun _ _ _ sz -> int_resize !(state.decisions) sz);
"T-asm", "Number of calls to Theory.assume", 6, Some true,
(fun _ _ _ sz -> int_resize !(state.assumes) sz);
"T/d", "Number of Theory.assume after last decision", 6, Some true,
(fun _ _ _ sz -> int_resize !(state.assumes_current_lvl) sz);
"T-qr", "Number of calls to Theory.query", 15, Some true,
(fun _ _ _ sz -> int_resize !(state.queries) sz);
"B-R", "Number of reduced clauses by Boolean propagation", 6, Some true,
(fun _ _ _ sz -> int_resize !(state.b_red) sz);
"B-E", "Number of eliminated clauses by Boolean propagation", 6,
Some true, (fun _ _ _ sz -> int_resize !(state.b_elim) sz);
"T-R", "Number of reduced clauses by Theory propagation", 6, Some true,
(fun _ _ _ sz -> int_resize !(state.t_red) sz);
"T-E", "Number of eliminated clauses by Theory propagation", 6,
Some true, (fun _ _ _ sz -> int_resize !(state.t_elim) sz);
"B-!", "Number of direct Boolean conflicts", 5, Some true,
(fun _ _ _ sz -> int_resize !(state.b_conflicts) sz);
"T-!", "Number of direct Theory conflicts" , 5, Some true,
(fun _ _ _ sz -> int_resize !(state.th_conflicts) sz);
"B>!", "Number of Boolean conflicts deduced by BCP", 5, Some true,
(fun _ _ _ sz -> int_resize !(state.bcp_b_conflicts) sz);
"T>!", "Number of Theory conflicts deduced by BCP", 5, Some true,
(fun _ _ _ sz -> int_resize !(state.bcp_th_conflicts) sz);
"M>!", "Number of Mix conflicts deduced by BCP", 5, Some true,
(fun _ _ _ sz -> int_resize !(state.bcp_mix_conflicts) sz);
"SAT", "Time spent in SAT module(s)", 16, Some false,
(fun _ gtime timers sz ->
let curr = Timers.get_sum timers Timers.M_Sat in
sprintf "%s~%s"
(float_resize curr (sz - 5)) (string_resize (percent gtime curr) 4));
"Matching", "Time spent in Matching module(s)", 16, Some false,
(fun _ gtime timers sz ->
let curr = Timers.get_sum timers Timers.M_Match in
sprintf "%s~%s"
(float_resize curr (sz - 5)) (string_resize (percent gtime curr) 4));
"CC", "Time spent in CC module(s)", 16, Some false,
(fun _ gtime timers sz ->
let curr = Timers.get_sum timers Timers.M_CC in
sprintf "%s~%s"
(float_resize curr (sz - 5)) (string_resize (percent gtime curr) 4)
);
"Arith", "Time spent in Arith module(s)", 16, Some false,
(fun _ gtime timers sz ->
let curr = Timers.get_sum timers Timers.M_Arith in
sprintf "%s~%s"
(float_resize curr (sz - 5)) (string_resize (percent gtime curr) 4));
"Arrays", "Time spent in Arrays module(s)", 16, Some false,
(fun _ gtime timers sz ->
let curr = Timers.get_sum timers Timers.M_Arrays in
sprintf "%s~%s"
(float_resize curr (sz - 5)) (string_resize (percent gtime curr) 4));
"Sum", "Time spent in Sum module(s)", 16, Some false,
(fun _ gtime timers sz ->
let curr = Timers.get_sum timers Timers.M_Sum in
sprintf "%s~%s"
(float_resize curr (sz - 5)) (string_resize (percent gtime curr) 4));
"Records", "Time spent in Records module(s)", 16, Some false,
(fun _ gtime timers sz ->
let curr = Timers.get_sum timers Timers.M_Records in
sprintf "%s~%s"
(float_resize curr (sz - 5)) (string_resize (percent gtime curr) 4));
"AC", "Time spent in AC module(s)", 16, Some false,
(fun _ gtime timers sz ->
let curr = Timers.get_sum timers Timers.M_AC in
sprintf "%s~%s"
(float_resize curr (sz - 5)) (string_resize (percent gtime curr) 4));
"Total", "Time spent in 'supervised' module(s)", 11, Some false,
(fun _ _ timers sz ->
let tsat = Timers.get_sum timers Timers.M_Sat in
let tmatch = Timers.get_sum timers Timers.M_Match in
let tcc = Timers.get_sum timers Timers.M_CC in
let tarith = Timers.get_sum timers Timers.M_Arith in
let tarrays = Timers.get_sum timers Timers.M_Arrays in
let tsum = Timers.get_sum timers Timers.M_Sum in
let trecs = Timers.get_sum timers Timers.M_Records in
let tac = Timers.get_sum timers Timers.M_AC in
let total = tsat+.tmatch+.tcc+.tarith+.tarrays+.tsum+.trecs+.tac in
float_resize total sz);
]
let print_initial_info () =
if !initial_info then begin
initial_info := false;
let max =
List.fold_left (fun z (id,_, _,_,_) -> max z (String.length id))
0 columns
in
List.iter
(fun (id, descr, _, _, _) ->
fprintf fmt "%s : %s@." (string_resize id max) descr
)columns
end
let stats_limit, timers_limit =
let aux tmp sz =
tmp := sprintf "%s|" !tmp;
for _ = 1 to sz do tmp := sprintf "%s-" !tmp done
in
let tmp_s = ref "" in
let tmp_t = ref "" in
List.iter
(fun (_, _, sz, opt, _) ->
match opt with
| Some true -> aux tmp_s sz
| Some false -> aux tmp_t sz
| _ -> aux tmp_s sz; aux tmp_t sz
)columns;
!tmp_s ^ "|", !tmp_t ^ "|"
let fmt =
let pp_stats =
match !mode with Stats -> true | Timers -> false | _ -> assert false in
if header || !nb_prints >= max_nb_prints then begin
nb_prints := 0;
fprintf fmt "%s@." (if pp_stats then stats_limit else timers_limit);
List.iter
(fun (id, _, sz, opt, _) ->
match opt with
| Some b when b != pp_stats -> ()
| _ -> fprintf fmt "|%s" (string_resize id sz)
)columns;
fprintf fmt "|@.";
fprintf fmt "%s@." (if pp_stats then stats_limit else timers_limit)
end;
incr nb_prints
let print_stats steps fmt timers =
print_header header fmt;
let gtime = Options.Time.value() in
List.iter
(fun (_, _, sz, opt, func) ->
match opt with
| Some false -> ()
| _ -> fprintf fmt "|%s" (func steps gtime timers sz)
)columns;
fprintf fmt "|@."
let print_timers steps fmt timers =
Timers.update timers;
print_header header fmt;
let gtime = Options.Time.value() in
List.iter
(fun (_, _, sz, opt, func) ->
match opt with
| Some true -> ()
| _ -> fprintf fmt "|%s" (func steps gtime timers sz)
)columns;
fprintf fmt "|@."
let (@@) a b = if a <> 0 then a else b
let print_instances_generation forced _steps _timers =
if not forced && !(state.instances_map_printed) then
fprintf fmt "[Instances profiling] No change since last print@."
else
let () = state.instances_map_printed := true in
if not forced then ignore(Sys.command("clear"));
fprintf fmt "[Instances profiling] ...@.";
let insts =
MS.fold
(fun name ii acc ->
let f1 = float_of_int ii.kept in
let f2 = float_of_int ii.ignored in
let ratio = f1 /. (f1 +. f2) in
let all_card = SE.cardinal ii.all_insts in
(name, ii, all_card, ratio) :: acc)
!(state.instances_map) []
in
let insts =
List.fast_sort (fun (_, i1, c1, _) (_, i2, c2, _) ->
(i1.decided - i2.decided) @@
(c1 - c2) @@
(i1.kept - i2.kept) @@
(i1.confl - i2.confl) @@
(i1.ignored - i2.ignored) @@
(SE.cardinal i1._new - SE.cardinal i2._new)
) insts
in
List.iter
(fun (name, i, card, r) ->
fprintf fmt "ratio kept/all: %s| " (float_resize r 8);
fprintf fmt "<> insts: %s| " (int_resize card 5);
fprintf fmt "kept: %s| " (int_resize i.kept 7);
fprintf fmt "ignored: %s| " (int_resize i.ignored 7) ;
fprintf fmt "decided: %s| " (int_resize i.decided 4);
fprintf fmt "conflicted: %s| " (int_resize i.confl 4);
fprintf fmt "consumed: %s| "
(int_resize (SE.cardinal i.consumed) 5);
fprintf fmt "produced: %s| "
(int_resize (SE.cardinal i.produced) 5);
fprintf fmt "new: %s|| "
(int_resize (SE.cardinal i._new) 5);
fprintf fmt "%s" (string_resize name 30);
fprintf fmt "@."
)insts;
()
let print_call_tree _forced _steps timers =
let stack = Timers.get_stack timers in
List.iter
(fun (k, f, id) ->
fprintf fmt "(%s, %s, %s) --> "
(string_resize (Timers.string_of_ty_module k) 5)
(string_resize (Timers.string_of_ty_function f) 10)
(int_resize id 7)
)(List.rev stack);
let m, f, id = Timers.current_timer timers in
fprintf fmt "(%s, %s, %s)@."
(string_resize (Timers.string_of_ty_module m) 5)
(string_resize (Timers.string_of_ty_function f) 10)
(int_resize id 7)
let switch () =
let next, next_msg = match !mode with
| Stats -> Timers, "Time"
| Timers -> CallTree, "CallTree"
| CallTree -> FunctionsTimers, "Functions Timers"
| FunctionsTimers -> Instances, "Instances generation"
| Instances -> Stats, "Stats"
in
fprintf fmt
"@.>>> Switch to %s profiling. Use \"Ctrl + AltGr + \\\" to exit\n"
next_msg;
nb_prints := max_nb_prints;
mode := next
let float_print fmt v =
if Stdlib.(=) v 0. then fprintf fmt "-- "
else if (Stdlib.compare v 10.) < 0 then fprintf fmt "%0.5f" v
else if (Stdlib.compare v 100.) < 0 then fprintf fmt "%0.4f" v
else fprintf fmt "%0.3f" v
let line_of_module arr f =
fprintf fmt "%s " (string_resize (Timers.string_of_ty_function f) 13);
let cpt = ref 0. in
List.iter
(fun m ->
let v = arr.(Timers.mtag m).(Timers.ftag f) in
cpt := !cpt +. v;
fprintf fmt "| %a " float_print v
) Timers.all_modules;
fprintf fmt "| %a |@." float_print !cpt
let line_of_sum_module timers =
for _ = 0 to 206 do fprintf fmt "-" done;
fprintf fmt "|@.";
fprintf fmt "%s " (string_resize "" 13);
List.iter
(fun m ->
fprintf fmt "| %a " float_print (Timers.get_sum timers m))
Timers.all_modules;
fprintf fmt "| GTimer %a |@." float_print (Options.Time.value())
let timers_table forced timers =
if not forced then ignore(Sys.command("clear"));
Timers.update timers;
fprintf fmt "@.";
fprintf fmt " ";
List.iter
(fun f ->
fprintf fmt"| %s" (string_resize (Timers.string_of_ty_module f) 9))
Timers.all_modules;
fprintf fmt "|@.";
for _ = 0 to 206 do fprintf fmt "-" done;
fprintf fmt "|@.";
let arr_timers = Timers.get_timers_array timers in
List.iter
(line_of_module arr_timers)
Timers.all_functions;
line_of_sum_module timers
let print all steps timers fmt =
print_initial_info ();
set_sigprof();
if all then begin
mode := Stats;
fprintf fmt "@.";
print_stats true steps fmt timers;
fprintf fmt "@.";
mode := Timers;
print_timers true steps fmt timers;
fprintf fmt "@.";
timers_table true timers;
fprintf fmt "@.";
print_instances_generation true steps timers;
fprintf fmt "@.";
end
else match !mode with
| Stats -> print_stats false steps fmt timers
| Timers -> print_timers false steps fmt timers
| CallTree -> print_call_tree false steps timers
| FunctionsTimers -> timers_table false timers;
| Instances -> print_instances_generation false steps timers