Source file 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
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 ""
type atomic_op =
| Start
| Make
| Get
| Set
| Exchange
| CompareAndSwap
| FetchAndAdd
let atomic_op_str x =
match x with
| Start -> "start"
| Make -> "make"
| Get -> "get"
| Set -> "set"
| Exchange -> "exchange"
| CompareAndSwap -> "compare_and_swap"
| FetchAndAdd -> "fetch_and_add"
let tracing = ref false
let finished_processes = ref 0
type process_data = {
mutable next_op : atomic_op;
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 () -> ())
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 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))
exception Terminated_early
let discontinue k () =
discontinue_with k Terminated_early
{
retc = (fun _ -> ());
exnc = (function Terminated_early -> () | e -> raise 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) ->
update_process_data current_process_id
(fun h -> continue_with k (Atomic.compare_and_set x s v) h)
CompareAndSwap (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; obj_ptr : int option }
type state_cell = {
procs : proc_rec list;
run_proc : int;
run_op : atomic_op;
run_ptr : int option;
enabled : IntSet.t;
mutable backtrack : IntSet.t;
}
let num_states = ref 0
let num_interleavings = ref 0
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_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 ();
tracing := true;
schedule_for_checks := init_schedule;
let num_processes = CCVector.length processes in
let rec run_trace s () =
tracing := false;
!every_func ();
tracing := true;
match s with
| [] ->
if !finished_processes == num_processes then (
tracing := false;
num_interleavings := !num_interleavings + 1;
if !record_traces_flag then
Trace_tracker.add_trace !schedule_for_checks;
(match !interleavings_chan with
| None -> ()
| Some chan -> print_execution_sequence chan);
!final_func ();
tracing := true)
| (process_id_to_run, next_op, next_ptr) :: schedule ->
if !finished_processes == num_processes then
failwith "no enabled processes"
else
let process_to_run = CCVector.get processes process_id_to_run in
assert (process_to_run.next_op = next_op);
assert (process_to_run.next_repr = next_ptr);
process_to_run.resume_func
(handler process_id_to_run (run_trace schedule))
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 filter_out_happen_after operation sequence =
let dependent_proc = ref (IntSet.singleton operation.run_proc) in
let dependent_vars =
ref
(Option.map IntSet.singleton operation.run_ptr
|> Option.value ~default:IntSet.empty)
in
List.filter_map
(fun (state_cell : state_cell) ->
let happen_after =
IntSet.mem state_cell.run_proc !dependent_proc
||
match state_cell.run_ptr with
| None -> false
| Some run_ptr -> IntSet.mem run_ptr !dependent_vars
in
if happen_after then (
dependent_proc := IntSet.add state_cell.run_proc !dependent_proc;
match state_cell.run_ptr with
| None -> ()
| Some run_ptr -> dependent_vars := IntSet.add run_ptr !dependent_vars);
if happen_after then None else Some state_cell)
sequence
let rec explore_source func state sleep_sets =
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 -> ()
| 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
let reversible_race =
Option.bind proc.obj_ptr (fun obj_ptr ->
let dependent_ops =
List.filter
(fun proc' ->
match proc'.run_ptr with
| None -> false
| Some run_ptr -> obj_ptr = run_ptr && proc'.run_proc <> p)
state
in
match List.rev dependent_ops with [] -> None | v :: _ -> Some v)
in
(match reversible_race with
| None -> ()
| Some e ->
let prefix, suffix =
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;
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
let indep_and_p =
let indep = filter_out_happen_after e suffix in
indep @ [ state_top ]
in
let initials =
let rec loop = function
| [] -> []
| initial :: sequence ->
initial.run_proc
:: loop (filter_out_happen_after initial sequence)
in
loop indep_and_p
in
let prefix_top = last_element prefix in
if
IntSet.(cardinal (inter prefix_top.backtrack (of_list initials)))
= 0
then
let initial = last_element initials in
prefix_top.backtrack <- IntSet.add initial prefix_top.backtrack);
let sleep' =
IntSet.filter
(fun q ->
if q == p then false
else
let proc' = List.nth s.procs q in
match proc'.obj_ptr with
| None -> true
| Some obj_ptr' ->
Option.map (fun obj_ptr -> obj_ptr <> obj_ptr') proc.obj_ptr
|> Option.value ~default:true)
!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 (
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)
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 (
Printf.printf "Found assertion violation at run %d:\n" !num_interleavings;
print_execution_sequence stdout;
assert false);
tracing := tracing_at_start
let reset_state () =
finished_processes := 0;
atomics_counter := 1;
num_states := 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);
(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