-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtracedAtomic.ml
679 lines (605 loc) · 22.7 KB
/
tracedAtomic.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
open Effect
open Effect.Shallow
type 'a t = 'a Atomic.t * int
type _ Effect.t +=
| Make : 'a -> 'a t Effect.t
| Get : 'a t -> 'a Effect.t
| Set : ('a t * 'a) -> unit Effect.t
| Exchange : ('a t * 'a) -> 'a Effect.t
| CompareAndSwap : ('a t * 'a * 'a) -> bool Effect.t
| FetchAndAdd : (int t * int) -> int Effect.t
module IntSet = Set.Make (Int)
module IntMap = Map.Make (Int)
let _string_of_set s = IntSet.fold (fun y x -> string_of_int y ^ "," ^ x) s ""
let tracing = ref false
let finished_processes = ref 0
type process_data = {
mutable next_op : Atomic_op.t;
mutable next_repr : int option;
mutable resume_func : (unit, unit) handler -> unit;
mutable finished : bool;
mutable discontinue_f : unit -> unit;
}
let every_func = ref (fun () -> ())
let final_func = ref (fun () -> ())
(* Atomics implementation *)
let atomics_counter = ref 1
let make v =
if !tracing then perform (Make v)
else
let i = !atomics_counter in
atomics_counter := !atomics_counter + 1;
(Atomic.make v, i)
let make_contended = make
let get r =
if !tracing then perform (Get r) else match r with v, _ -> Atomic.get v
let set r v =
if !tracing then perform (Set (r, v))
else match r with x, _ -> Atomic.set x v
let exchange r v =
if !tracing then perform (Exchange (r, v))
else match r with x, _ -> Atomic.exchange x v
let compare_and_set r seen v =
if !tracing then perform (CompareAndSwap (r, seen, v))
else match r with x, _ -> Atomic.compare_and_set x seen v
let fetch_and_add r n =
if !tracing then perform (FetchAndAdd (r, n))
else match r with x, _ -> Atomic.fetch_and_add x n
let incr r = ignore (fetch_and_add r 1)
let decr r = ignore (fetch_and_add r (-1))
(* Tracing infrastructure *)
exception Terminated_early
let discontinue k () =
discontinue_with k Terminated_early
{
retc = (fun _ -> ());
exnc = (function Terminated_early -> () | _e -> ());
effc = (fun (type a) (_ : a Effect.t) -> None);
}
let processes = CCVector.create ()
let update_process_data process_id f op repr k =
let process_rec = CCVector.get processes process_id in
process_rec.resume_func <- f;
process_rec.next_repr <- repr;
process_rec.next_op <- op;
process_rec.discontinue_f <- discontinue k
let finish_process process_id =
let process_rec = CCVector.get processes process_id in
process_rec.finished <- true;
process_rec.discontinue_f <- (fun () -> ());
finished_processes := !finished_processes + 1
let handler current_process_id runner =
{
retc =
(fun _ ->
finish_process current_process_id;
runner ());
exnc = (fun s -> raise s);
effc =
(fun (type a) (e : a Effect.t) ->
match e with
| Make v ->
Some
(fun (k : (a, _) continuation) ->
let i = !atomics_counter in
let m = (Atomic.make v, i) in
atomics_counter := !atomics_counter + 1;
update_process_data current_process_id
(fun h -> continue_with k m h)
Make (Some i) k;
runner ())
| Get (v, i) ->
Some
(fun (k : (a, _) continuation) ->
update_process_data current_process_id
(fun h -> continue_with k (Atomic.get v) h)
Get (Some i) k;
runner ())
| Set ((r, i), v) ->
Some
(fun (k : (a, _) continuation) ->
update_process_data current_process_id
(fun h -> continue_with k (Atomic.set r v) h)
Set (Some i) k;
runner ())
| Exchange ((a, i), b) ->
Some
(fun (k : (a, _) continuation) ->
update_process_data current_process_id
(fun h -> continue_with k (Atomic.exchange a b) h)
Exchange (Some i) k;
runner ())
| CompareAndSwap ((x, i), s, v) ->
Some
(fun (k : (a, _) continuation) ->
let rec status =
ref
(`Unknown
(fun () ->
let result = Atomic.get x == s in
status := if result then `Success else `Fail;
if result then `Success else `Fail))
in
update_process_data current_process_id
(fun h ->
continue_with k
((match !status with
| `Success | `Fail ->
failwith "this result has been predicted"
| `Unknown _ -> ());
let result = Atomic.compare_and_set x s v in
status := if result then `Success else `Fail;
result)
h)
(Atomic_op.CompareAndSwap status) (Some i) k;
runner ())
| FetchAndAdd ((v, i), x) ->
Some
(fun (k : (a, _) continuation) ->
update_process_data current_process_id
(fun h -> continue_with k (Atomic.fetch_and_add v x) h)
FetchAndAdd (Some i) k;
runner ())
| _ -> None);
}
let spawn f =
let fiber_f = fiber f in
let resume_func = continue_with fiber_f () in
CCVector.push processes
{
next_op = Start;
next_repr = None;
resume_func;
finished = false;
discontinue_f = discontinue fiber_f;
}
let rec last_element l =
match l with h :: [] -> h | [] -> assert false | _ :: tl -> last_element tl
type proc_rec = { proc_id : int; op : Atomic_op.t; obj_ptr : int option }
type state_cell = {
procs : proc_rec list;
run_proc : int;
run_op : Atomic_op.t;
run_ptr : int option;
enabled : IntSet.t;
mutable backtrack : IntSet.t;
}
let sleep_set_blocked = ref 0
let num_states = ref 0
let num_interleavings = ref 0
(* we stash the current state in case a check fails and we need to log it *)
let schedule_for_checks = ref []
let var_name i =
match i with
| None -> ""
| Some i ->
let c = Char.chr (i + 96) in
Printf.sprintf "%c" c
let print_execution_sequence chan =
let highest_proc =
List.fold_left
(fun highest (curr_proc, _, _) ->
if curr_proc > highest then curr_proc else highest)
(-1) !schedule_for_checks
in
let bar =
List.init ((highest_proc * 20) + 20) (fun _ -> "-") |> String.concat ""
in
Printf.fprintf chan "\nsequence %d\n" !num_interleavings;
Printf.fprintf chan "%s\n" bar;
List.init (highest_proc + 1) (fun proc ->
Printf.fprintf chan "P%d\t\t\t" proc)
|> ignore;
Printf.fprintf chan "\n%s\n" bar;
List.iter
(fun s ->
match s with
| last_run_proc, last_run_op, last_run_ptr ->
let last_run_ptr = var_name last_run_ptr in
let tabs =
List.init last_run_proc (fun _ -> "\t\t\t") |> String.concat ""
in
Printf.fprintf chan "%s%s %s\n" tabs
(Atomic_op.to_str last_run_op)
last_run_ptr)
!schedule_for_checks;
Printf.fprintf chan "%s\n%!" bar
let interleavings_chan = (ref None : out_channel option ref)
let record_traces_flag = ref false
let do_run init_func init_schedule =
init_func ();
(*set up run *)
tracing := true;
schedule_for_checks := init_schedule;
(* cache the number of processes in case it's expensive*)
let num_processes = CCVector.length processes in
(* current number of ops we are through the current run *)
finished_processes := 0;
let rec run_trace s true_schedule_rev () =
tracing := false;
!every_func ();
tracing := true;
match s with
| [] ->
if !finished_processes == num_processes then begin
tracing := false;
num_interleavings := !num_interleavings + 1;
if !record_traces_flag then
Trace_tracker.add_trace (List.rev true_schedule_rev);
(match !interleavings_chan with
| None -> ()
| Some chan -> print_execution_sequence chan);
!final_func ();
tracing := true
end
| (process_id_to_run, next_op, next_ptr) :: schedule -> begin
if !finished_processes == num_processes then
(* this should never happen *)
failwith "no enabled processes"
else
let process_to_run = CCVector.get processes process_id_to_run in
let at = process_to_run.next_op in
assert (Atomic_op.weak_cmp process_to_run.next_op next_op);
assert (process_to_run.next_repr = next_ptr);
let true_schedule_rev =
(process_id_to_run, process_to_run.next_op, process_to_run.next_repr)
:: true_schedule_rev
in
process_to_run.resume_func
(handler process_id_to_run (run_trace schedule true_schedule_rev));
match at with
| CompareAndSwap cas -> begin
match !cas with `Unknown _ -> assert false | _ -> ()
end
| _ -> ()
end
in
tracing := true;
run_trace init_schedule [] ();
finished_processes := 0;
tracing := false;
num_states := !num_states + 1;
let procs =
CCVector.mapi
(fun i p -> { proc_id = i; op = p.next_op; obj_ptr = p.next_repr })
processes
|> CCVector.to_list
in
let current_enabled =
CCVector.to_seq processes |> OSeq.zip_index
|> Seq.filter (fun (_, proc) -> not proc.finished)
|> Seq.map (fun (id, _) -> id)
|> IntSet.of_seq
in
CCVector.iter (fun proc -> proc.discontinue_f ()) processes;
CCVector.clear processes;
atomics_counter := 1;
match last_element init_schedule with
| run_proc, run_op, run_ptr ->
{
procs;
enabled = current_enabled;
run_proc;
run_op;
run_ptr;
backtrack = IntSet.empty;
}
let rec explore_random func state =
let s = last_element state in
let enabled = IntSet.to_seq s.enabled |> List.of_seq in
let len = List.length enabled in
if len == 0 then ()
else
let random_index = Random.int len in
let j = List.nth enabled random_index in
let j_proc = List.nth s.procs j in
let schedule =
List.map (fun s -> (s.run_proc, s.run_op, s.run_ptr)) state
@ [ (j, j_proc.op, j_proc.obj_ptr) ]
in
let statedash = state @ [ do_run func schedule ] in
explore_random func statedash
let same_proc state_cell1 state_cell2 =
state_cell1.run_proc = state_cell2.run_proc
module Causality = struct
let hb ((proc1 : int), (ptr1 : int option), op1)
((proc2 : int), (ptr2 : int option), op2) =
(* assumes the two ops are adjacent *)
let same_proc = proc1 = proc2 in
let same_var =
match (ptr1, ptr2) with
| Some ptr1, Some ptr2 -> ptr1 = ptr2
| Some _, None | None, Some _ | None, None -> false
in
let conflicting =
let is_write = Atomic_op.is_write ~allow_unknown:true in
Lazy.from_val (is_write op1 || is_write op2)
in
same_proc || (same_var && Lazy.force conflicting)
let happens_before = function
| `State (state_cell1, state_cell2) ->
hb
(state_cell1.run_proc, state_cell1.run_ptr, state_cell1.run_op)
(state_cell2.run_proc, state_cell2.run_ptr, state_cell2.run_op)
| `Proc (proc1, proc2) ->
hb
(proc1.proc_id, proc1.obj_ptr, proc1.op)
(proc2.proc_id, proc2.obj_ptr, proc2.op)
let mark_happen_before operation (sequence : state_cell list) =
let sequence = List.map (fun v -> (v, ref false)) sequence in
let sequence = (operation, ref true) :: sequence in
let mark_intransitive hd_sequence tl_sequence =
List.iter
(fun (state_cell, hb) ->
hb := !hb || happens_before (`State (hd_sequence, state_cell)))
tl_sequence
in
let rec mark_all = function
| [] | _ :: [] -> ()
| (op, hb) :: tl ->
if !hb then mark_intransitive op tl;
mark_all tl
in
mark_all sequence;
match sequence with
| [] -> assert false
| (operation', _) :: sequence ->
assert (operation == operation');
sequence
end
let is_reversible_race (op1 : state_cell) (between : state_cell list)
(op2 : state_cell) =
let hb_intransitively =
(* Two ops have to be causally related for us to want to reverse them. *)
Causality.happens_before (`State (op1, op2))
in
let diff_proc =
(* If two ops belong two the same proc, they cannot be reversed. *)
not (same_proc op1 op2)
in
if hb_intransitively && diff_proc then
let not_transitively_related =
(* If two ops are related transitively, technically not a race (see paper). *)
let between = Causality.mark_happen_before op1 between in
let between_hb =
List.filter_map (fun (op, hb) -> if !hb then Some op else None) between
in
let op2_not_transitively_related =
List.for_all
(fun op -> not (Causality.happens_before (`State (op2, op))))
between_hb
in
op2_not_transitively_related
in
not_transitively_related
else false
let filter_out_happen_after operation sequence =
Causality.mark_happen_before operation sequence
|> List.filter_map (fun (op, hb) -> if !hb then None else Some op)
let rec explore_source func state sleep_sets =
(* The code here closely follows the Algorithm 1 outlined in [Source Sets:
A Foundation for Optimal Dynamic Partial Order Reduction]. Likewise
variable names (e.g. reversible race, indep_and_p, initials) etc.
reference constructs introduced in the paper.
*)
let sleep = ref (last_element sleep_sets) in
let s = last_element state in
let p_maybe = IntSet.min_elt_opt (IntSet.diff s.enabled !sleep) in
match p_maybe with
| None ->
if not (IntSet.is_empty s.enabled) then
sleep_set_blocked := !sleep_set_blocked + 1
| Some p ->
s.backtrack <- IntSet.singleton p;
while IntSet.(cardinal (diff s.backtrack !sleep)) > 0 do
let p = IntSet.min_elt (IntSet.diff s.backtrack !sleep) in
let proc = List.nth s.procs p in
let state_top =
let schedule =
List.map (fun s -> (s.run_proc, s.run_op, s.run_ptr)) state
@ [ (p, proc.op, proc.obj_ptr) ]
in
do_run func schedule
in
assert (state_top.run_proc = p);
let new_state = state @ [ state_top ] in
(* Find the races (transitions dependent directly, without a transitive dependency).
*)
let reversible_races =
List.fold_right
(fun op1 ((between, reversible_races, skip_rest) as acc) ->
if skip_rest then acc
else if is_reversible_race op1 between state_top then
( op1 :: between,
op1 :: reversible_races,
Atomic_op.is_write op1.run_op )
else (op1 :: between, reversible_races, false))
state ([], [], false)
|> function
| _, l, _ -> l
in
List.iter
(fun e ->
let prefix, suffix =
(* We need the last operation before the first operation of the race
occured because that's where alternative (reversal) is scheduled.
We need the suffix to compute how to schedule the reversal. *)
let found_e, prefix_rev, suffix_rev =
List.fold_left
(fun (seen_e, prefix, suffix) proc' ->
if seen_e then (seen_e, prefix, proc' :: suffix)
else if proc' == e then (true, prefix, suffix)
else (false, proc' :: prefix, suffix))
(false, [], []) state
in
assert found_e;
(* Out first operation is always a spawn, which cannot
race with anything. Thus, any race has a prefix.
*)
assert (List.length prefix_rev > 0);
assert (
List.length suffix_rev
= List.length state - List.length prefix_rev - 1);
(List.rev prefix_rev, List.rev suffix_rev)
in
(* Filter out operations that are dependent on the first operation
of the race (e.g. successive operations of e.run_proc). We definitely
don't want to schedule them.
*)
let indep_and_p =
let indep = filter_out_happen_after e suffix in
indep @ [ state_top ]
in
(* Compute the set of operations, that lead to reversal of the race.
The main premise here is that there may be a number of independent
sequences that lead to reversal.
For example, suppose two racing operations t, t' and some sequences
E, w, u. Suppose the current sequence is E.t.w.u.t', t' happens
after u and w is independent of everything.
There's at least two ways to reverse t and t':
- E.u.t'.(t,w in any order)
- E.w.u.t'.t
Thus, initials consist of the first operations of u and w, since
these are the operations that lead to exploration of the above
sequences from E.
*)
let initials =
let rec loop = function
| [] -> []
| initial :: sequence ->
initial.run_proc
:: loop (filter_out_happen_after initial sequence)
in
loop indep_and_p
in
(* Exploring one of the initials guarantees that reversal has been
visited. Thus, schedule one of the initials only if none of them
is in backtrack. *)
let prefix_top = last_element prefix in
if
IntSet.(cardinal (inter prefix_top.backtrack (of_list initials)))
= 0
&&
let slp = List.nth sleep_sets (List.length prefix - 1) in
IntSet.(cardinal (inter slp (of_list initials))) = 0
then
(* We can add any initial *)
let initial = last_element initials in
prefix_top.backtrack <- IntSet.add initial prefix_top.backtrack)
reversible_races;
let sleep' =
(* Keep q that are independent with p only. Must be other thread of execution and act on a different object (or none).
The key idea here is as follows. Suppose we have processed some execution sequence E and there are
just two enabled transitions left. Namely, t=(read a), t'=(read b). Crucially, they are independent.
We begin the exploration from E with E.t and descend into E.t.t' afterwards. Since no more transitions
are enabled, we return back to E and execute E.t'. But there's no point in executing E.t'.t. Since t and
t' are independent, there's a guarantee that E.t.t' and E.t'.t belong to the same trace.
Therefore, at E, t is put into sleep set, and we explore with E.t' with sleep=[t]. Then E.t'.t gets
sleep-set blocked and we save an execution sequence. Naturally, if there's some w such that it's dependent
with t, then before we explore E.t'.w, we have to "wake" t up.
*)
IntSet.filter
(fun q ->
let proc' = List.nth s.procs q in
not (Causality.happens_before (`Proc (proc, proc'))))
!sleep
in
explore_source func new_state (sleep_sets @ [ sleep' ]);
sleep := IntSet.add p !sleep
done
let rec explore func state clock last_access =
let s = last_element state in
List.iter
(fun proc ->
let j = proc.proc_id in
let i =
Option.bind proc.obj_ptr (fun ptr -> IntMap.find_opt ptr last_access)
|> Option.value ~default:0
in
if i != 0 then
let pre_s = List.nth state (i - 1) in
if IntSet.mem j pre_s.enabled then
pre_s.backtrack <- IntSet.add j pre_s.backtrack
else pre_s.backtrack <- IntSet.union pre_s.backtrack pre_s.enabled)
s.procs;
if IntSet.cardinal s.enabled > 0 then begin
let p = IntSet.min_elt s.enabled in
let dones = ref IntSet.empty in
s.backtrack <- IntSet.singleton p;
while IntSet.(cardinal (diff s.backtrack !dones)) > 0 do
let j = IntSet.min_elt (IntSet.diff s.backtrack !dones) in
dones := IntSet.add j !dones;
let j_proc = List.nth s.procs j in
let schedule =
List.map (fun s -> (s.run_proc, s.run_op, s.run_ptr)) state
@ [ (j, j_proc.op, j_proc.obj_ptr) ]
in
let statedash = state @ [ do_run func schedule ] in
let state_time = List.length statedash - 1 in
let new_last_access =
match j_proc.obj_ptr with
| Some ptr -> IntMap.add ptr state_time last_access
| None -> last_access
in
let new_clock = IntMap.add j state_time clock in
explore func statedash new_clock new_last_access
done
end
let every f = every_func := f
let final f = final_func := f
let check f =
let tracing_at_start = !tracing in
tracing := false;
if not (f ()) then begin
Printf.printf "Found assertion violation at run %d:\n" !num_interleavings;
print_execution_sequence stdout;
assert false
end;
tracing := tracing_at_start
let reset_state () =
finished_processes := 0;
atomics_counter := 1;
num_states := 0;
sleep_set_blocked := 0;
num_interleavings := 0;
schedule_for_checks := [];
Trace_tracker.clear_traces ();
CCVector.clear processes
let dscheck_trace_file_env = Sys.getenv_opt "dscheck_trace_file"
let random func iters =
reset_state ();
let empty_state = do_run func [ (0, Start, None) ] :: [] in
for _ = 1 to iters do
explore_random func empty_state
done
let dpor func =
reset_state ();
let empty_state = do_run func [ (0, Start, None) ] :: [] in
let empty_clock = IntMap.empty in
let empty_last_access = IntMap.empty in
explore func empty_state empty_clock empty_last_access
let dpor_source func =
reset_state ();
let empty_state = do_run func [ (0, Start, None) ] in
explore_source func [ empty_state ] [ IntSet.empty ]
let trace ?(impl = `Dpor_source) ?interleavings ?(record_traces = false) func =
record_traces_flag := record_traces || Option.is_some dscheck_trace_file_env;
interleavings_chan := interleavings;
(match impl with
| `Dpor -> dpor func
| `Random iters -> random func iters
| `Dpor_source -> dpor_source func);
(* print reports *)
if record_traces && Option.is_none !interleavings_chan then
interleavings_chan := Some stdout;
(match !interleavings_chan with
| None -> ()
| Some chan ->
Printf.fprintf chan "\nexplored %d interleavings and %d states\n"
!num_interleavings !num_states);
match dscheck_trace_file_env with
| None -> ()
| Some path ->
let chan = open_out path in
Trace_tracker.print_traces chan;
close_out chan