Skip to content

Commit 5e014fc

Browse files
authored
Merge pull request #661 from talex5/trace-switch
Tracing: add labels to switches
2 parents 4db993d + c88c58e commit 5e014fc

File tree

21 files changed

+50
-40
lines changed

21 files changed

+50
-40
lines changed

bench/bench_http.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ let run_client ~n_requests id conn =
6262
let main net domain_mgr ~n_client_domains ~n_server_domains ~n_connections_per_domain ~n_requests_per_connection =
6363
let total = Atomic.make 0 in
6464
let t0 = Unix.gettimeofday () in
65-
Switch.run (fun sw ->
65+
Switch.run ~name:"main" (fun sw ->
6666
let addr = `Tcp (Eio.Net.Ipaddr.V4.loopback, 8085) in
6767
let backlog = n_connections_per_domain * n_client_domains in
6868
let server_socket = Eio.Net.listen ~reuse_addr:true ~backlog ~sw net addr in
@@ -74,7 +74,7 @@ let main net domain_mgr ~n_client_domains ~n_server_domains ~n_connections_per_d
7474
for domain = 1 to n_client_domains do
7575
Fiber.fork ~sw (fun () ->
7676
Eio.Domain_manager.run domain_mgr (fun () ->
77-
Switch.run @@ fun sw ->
77+
Switch.run ~name:"client-domain" @@ fun sw ->
7878
for i = 1 to n_connections_per_domain do
7979
Fiber.fork ~sw (fun () ->
8080
let id = Printf.sprintf "domain %d / conn %d" domain i in

examples/net/client.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ module Write = Eio.Buf_write
88

99
(* Connect to [addr] on [net], send a message and then read the reply. *)
1010
let run ~net ~addr =
11+
Switch.run ~name:"client" @@ fun sw ->
1112
traceln "Connecting to server at %a..." Eio.Net.Sockaddr.pp addr;
12-
Switch.run @@ fun sw ->
1313
let flow = Eio.Net.connect ~sw net addr in
1414
(* We use a buffered writer here so we can create the message in multiple
1515
steps but still send it efficiently as a single packet: *)

examples/net/main.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ let addr = `Tcp (Eio.Net.Ipaddr.V4.loopback, 8080)
44

55
(* Run a server and a test client, communicating using [net]. *)
66
let main ~net =
7-
Switch.run @@ fun sw ->
7+
Switch.run ~name:"main" @@ fun sw ->
88
(* We create the listening socket first so that we can be sure it is ready
99
as soon as the client wants to use it. *)
1010
let listening_socket = Eio.Net.listen net ~sw ~reuse_addr:true ~backlog:5 addr in

examples/trace/main.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ let trace ~finished (clock, delay) cursor =
4040

4141
(* The program to be traced. *)
4242
let main net =
43-
Switch.run @@ fun sw ->
43+
Switch.run ~name:"main" @@ fun sw ->
4444
let addr = `Tcp (Eio.Net.Ipaddr.V4.loopback, 8123) in
4545
let s = Eio.Net.listen ~sw ~backlog:1 ~reuse_addr:true net addr in
4646
Fiber.both

lib_eio/buf_write.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ let copy t flow =
481481
with End_of_file -> ()
482482

483483
let with_flow ?(initial_size=0x1000) flow fn =
484-
Switch.run @@ fun sw ->
484+
Switch.run ~name:"Buf_write.with_flow" @@ fun sw ->
485485
let t = create ~sw initial_size in
486486
Fiber.fork ~sw (fun () -> copy t flow);
487487
match fn t with

lib_eio/core/cancel.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,11 @@ let cancel t ex =
170170
Printexc.raise_with_backtrace ex bt
171171
)
172172

173-
let sub_checked purpose fn =
173+
let sub_checked ?name purpose fn =
174174
let ctx = Effect.perform Get_context in
175175
let parent = ctx.cancel_context in
176176
with_cc ~ctx ~parent ~protected:false purpose @@ fun t ->
177+
Option.iter (Trace.name t.id) name;
177178
fn t
178179

179180
let sub fn =

lib_eio/core/eio__core.mli

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,18 @@ module Switch : sig
3434

3535
(** {2 Switch creation} *)
3636

37-
val run : (t -> 'a) -> 'a
37+
val run : ?name:string -> (t -> 'a) -> 'a
3838
(** [run fn] runs [fn] with a fresh switch (initially on).
3939
4040
When [fn] finishes, [run] waits for all fibers registered with the switch to finish,
4141
and then releases all attached resources.
4242
4343
If {!fail} is called, [run] will re-raise the exception (after everything is cleaned up).
44-
If [fn] raises an exception, it is passed to {!fail}. *)
44+
If [fn] raises an exception, it is passed to {!fail}.
45+
46+
@param name Used to name the switch when tracing. *)
4547

46-
val run_protected : (t -> 'a) -> 'a
48+
val run_protected : ?name:string -> (t -> 'a) -> 'a
4749
(** [run_protected fn] is like [run] but ignores cancellation requests from the parent context. *)
4850

4951
(** {2 Cancellation and failure} *)

lib_eio/core/fiber.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,13 @@ let fork_promise_exn ~sw f =
7070
p
7171

7272
let all xs =
73-
Switch.run @@ fun sw ->
73+
Switch.run ~name:"all" @@ fun sw ->
7474
List.iter (fork ~sw) xs
7575

7676
let both f g = all [f; g]
7777

7878
let pair f g =
79-
Switch.run @@ fun sw ->
79+
Switch.run ~name:"pair" @@ fun sw ->
8080
let x = fork_promise ~sw f in
8181
let y = g () in
8282
(Promise.await_exn x, y)
@@ -225,7 +225,7 @@ module List = struct
225225
match items with
226226
| [] -> [] (* Avoid creating a switch in the simple case *)
227227
| items ->
228-
Switch.run @@ fun sw ->
228+
Switch.run ~name:"filter_map" @@ fun sw ->
229229
let limiter = Limiter.create ~sw max_fibers in
230230
let rec aux = function
231231
| [] -> []
@@ -244,7 +244,7 @@ module List = struct
244244
match items with
245245
| [] -> () (* Avoid creating a switch in the simple case *)
246246
| items ->
247-
Switch.run @@ fun sw ->
247+
Switch.run ~name:"iter" @@ fun sw ->
248248
let limiter = Limiter.create ~sw max_fibers in
249249
let rec aux = function
250250
| [] -> ()

lib_eio/core/switch.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,12 @@ let run_internal t fn =
146146
maybe_raise_exs t;
147147
assert false
148148

149-
let run fn = Cancel.sub_checked Switch (fun cc -> run_internal (create cc) fn)
149+
let run ?name fn = Cancel.sub_checked ?name Switch (fun cc -> run_internal (create cc) fn)
150150

151-
let run_protected fn =
151+
let run_protected ?name fn =
152152
let ctx = Effect.perform Cancel.Get_context in
153153
Cancel.with_cc ~ctx ~parent:ctx.cancel_context ~protected:true Switch @@ fun cancel ->
154+
Option.iter (Trace.name cancel.id) name;
154155
run_internal (create cancel) fn
155156

156157
(* Run [fn ()] in [t]'s cancellation context.

lib_eio/core/trace.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ let create_fiber ~cc id =
3636
add_event RE.create_fiber (id, cc)
3737

3838
let log = add_event RE.log
39+
let name id x = add_event RE.name (id, x)
3940
let enter_span = add_event RE.enter_span
4041
let exit_span = add_event RE.exit_span
4142
let fiber = add_event RE.fiber

0 commit comments

Comments
 (0)