Skip to content

Commit 16de06a

Browse files
committed
Make emit1_eio.ml emit deterministic signals
As soon as we start running this in multible system threads, the race to trigger the globals `stop` and `iterations` makes the signal emissions non-deterministic, which makes the test kind of meaningless. This change should make them determinstic.
1 parent 0890a1a commit 16de06a

File tree

1 file changed

+54
-56
lines changed

1 file changed

+54
-56
lines changed

tests/bin/emit1_eio.ml

Lines changed: 54 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ let sleep_outer = ref 2.0
1111

1212
let n_jobs = ref 1
1313

14-
let iterations = Atomic.make 1
15-
1614
let num_sleep = Atomic.make 0
1715

1816
let stress_alloc_ = ref true
@@ -24,63 +22,61 @@ let num_tr = Atomic.make 0
2422
(* Counter used to mark simulated failures *)
2523
let i = ref 0
2624

27-
let run_job clock _job_id : unit =
28-
while not @@ Atomic.get stop do
29-
let@ scope =
30-
Atomic.incr num_tr;
31-
OT.Trace.with_ ~kind:OT.Span.Span_kind_producer "loop.outer"
32-
~attrs:[ "i", `Int !i ]
33-
in
34-
35-
for j = 0 to Atomic.get iterations do
36-
if j >= Atomic.get iterations then
37-
(* Terminate program, having reached our max iterations *)
38-
Atomic.set stop true
39-
else
40-
(* parent scope is found via thread local storage *)
25+
let run_job clock _job_id iterations : unit =
26+
let@ scope =
27+
Atomic.incr num_tr;
28+
OT.Trace.with_ ~kind:OT.Span.Span_kind_producer "loop.outer"
29+
~attrs:[ "i", `Int !i ]
30+
in
31+
32+
for j = 0 to iterations do
33+
if j >= iterations then
34+
(* Terminate program, having reached our max iterations *)
35+
Atomic.set stop true
36+
else
37+
(* parent scope is found via thread local storage *)
38+
let@ scope =
39+
Atomic.incr num_tr;
40+
OT.Trace.with_ ~scope ~kind:OT.Span.Span_kind_internal
41+
~attrs:[ "j", `Int j ]
42+
"loop.inner"
43+
in
44+
45+
let () = Eio.Time.sleep clock !sleep_outer in
46+
Atomic.incr num_sleep;
47+
48+
OT.Logs.(
49+
emit
50+
[
51+
make_strf ~trace_id:scope.trace_id ~span_id:scope.span_id
52+
~severity:Severity_number_info "inner at %d" j;
53+
]);
54+
55+
incr i;
56+
57+
try
58+
Atomic.incr num_tr;
4159
let@ scope =
42-
Atomic.incr num_tr;
43-
OT.Trace.with_ ~scope ~kind:OT.Span.Span_kind_internal
44-
~attrs:[ "j", `Int j ]
45-
"loop.inner"
60+
OT.Trace.with_ ~kind:OT.Span.Span_kind_internal ~scope "alloc"
4661
in
62+
(* allocate some stuff *)
63+
if !stress_alloc_ then (
64+
let _arr = Sys.opaque_identity @@ Array.make (25 * 25551) 42.0 in
65+
ignore _arr
66+
);
4767

48-
let () = Eio.Time.sleep clock !sleep_outer in
68+
let () = Eio.Time.sleep clock !sleep_inner in
4969
Atomic.incr num_sleep;
5070

51-
OT.Logs.(
52-
emit
53-
[
54-
make_strf ~trace_id:scope.trace_id ~span_id:scope.span_id
55-
~severity:Severity_number_info "inner at %d" j;
56-
]);
57-
58-
incr i;
59-
60-
try
61-
Atomic.incr num_tr;
62-
let@ scope =
63-
OT.Trace.with_ ~kind:OT.Span.Span_kind_internal ~scope "alloc"
64-
in
65-
(* allocate some stuff *)
66-
if !stress_alloc_ then (
67-
let _arr = Sys.opaque_identity @@ Array.make (25 * 25551) 42.0 in
68-
ignore _arr
69-
);
70-
71-
let () = Eio.Time.sleep clock !sleep_inner in
72-
Atomic.incr num_sleep;
73-
74-
if j = 4 && !i mod 13 = 0 then failwith "oh no";
75-
76-
(* simulate a failure *)
77-
Opentelemetry.Scope.add_event scope (fun () ->
78-
OT.Event.make "done with alloc")
79-
with Failure _ -> ()
80-
done
71+
if j = 4 && !i mod 13 = 0 then failwith "oh no";
72+
73+
(* simulate a failure *)
74+
Opentelemetry.Scope.add_event scope (fun () ->
75+
OT.Event.make "done with alloc")
76+
with Failure _ -> ()
8177
done
8278

83-
let run env proc () : unit =
79+
let run env proc iterations () : unit =
8480
OT.GC_metrics.basic_setup ();
8581

8682
OT.Metrics_callbacks.register (fun () ->
@@ -95,7 +91,7 @@ let run env proc () : unit =
9591

9692
Eio.Switch.run (fun sw ->
9793
for j = 1 to n_jobs do
98-
Eio.Fiber.fork ~sw (fun () -> run_job env#clock j)
94+
Eio.Fiber.fork ~sw (fun () -> run_job env#clock j iterations)
9995
done)
10096

10197
let () =
@@ -109,6 +105,7 @@ let () =
109105
let batch_metrics = ref 3 in
110106
let batch_logs = ref 400 in
111107
let url = ref None in
108+
let n_iterations = ref 1 in
112109
let n_procs = ref 1 in
113110
let opts =
114111
[
@@ -127,7 +124,7 @@ let () =
127124
"--sleep-inner", Arg.Set_float sleep_inner, " sleep (in s) in inner loop";
128125
"--sleep-outer", Arg.Set_float sleep_outer, " sleep (in s) in outer loop";
129126
( "--iterations",
130-
Arg.Int (Atomic.set iterations),
127+
Arg.Set_int n_iterations,
131128
" the number of iterations to run" );
132129
"-j", Arg.Set_int n_jobs, " number of jobs per processes";
133130
"--procs", Arg.Set_int n_procs, " number of processes";
@@ -161,14 +158,15 @@ let () =
161158
in
162159
Eio_main.run @@ fun env ->
163160
(if !n_procs < 2 then
164-
Opentelemetry_client_cohttp_eio.with_setup ~stop ~config (run env 0) env
161+
Opentelemetry_client_cohttp_eio.with_setup ~stop ~config
162+
(run env 0 !n_iterations) env
165163
else
166164
Eio.Switch.run @@ fun sw ->
167165
Opentelemetry_client_cohttp_eio.setup ~stop ~config ~sw env;
168166
let dm = Eio.Stdenv.domain_mgr env in
169167
Eio.Switch.run (fun sw ->
170168
for proc = 1 to !n_procs do
171169
Eio.Fiber.fork ~sw @@ fun () ->
172-
Eio.Domain_manager.run dm (run env proc)
170+
Eio.Domain_manager.run dm (run env proc !n_iterations)
173171
done));
174172
Opentelemetry.Collector.remove_backend () ~on_done:ignore

0 commit comments

Comments
 (0)