Skip to content

Commit 8323ef3

Browse files
authored
Merge pull request #17 from talex5/link-domains
Link spawned domains to their parent
2 parents 4d1110b + c58aeab commit 8323ef3

File tree

4 files changed

+89
-22
lines changed

4 files changed

+89
-22
lines changed

lib/layout.ml

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,16 @@ and item = {
6262
module Ring = struct
6363
type id = Trace.Ring.id
6464

65+
type root = {
66+
mutable parent : (timestamp * int) option;
67+
mutable cc : (timestamp * item) option;
68+
}
69+
6570
type t = {
6671
events : (timestamp * string list) array;
6772
mutable y : int;
6873
mutable height : int;
69-
mutable roots : (timestamp * item) list;
74+
mutable roots : root list;
7075
}
7176
end
7277

@@ -149,7 +154,8 @@ let layout ~duration (ring : Ring.t) =
149154
max_y := max !max_y i.y;
150155
if debug_layout then Fmt.epr "%d is at %d+%d@." i.id y i.height;
151156
in
152-
let visit_domain (_ts, (i : item)) =
157+
let visit_domain root =
158+
root.Ring.cc |> Option.iter @@ fun (_ts, (i : item)) ->
153159
i.y <- ring.y + 1;
154160
i.height <- 1;
155161
i.end_cc_label <- None;
@@ -160,8 +166,8 @@ let layout ~duration (ring : Ring.t) =
160166
visit ~y:(ring.y + i.height) child;
161167
i.height <- child.y - ring.y + child.height;
162168
if i.end_cc_label = None then (
163-
i.end_cc_label <- child.end_cc_label;
164-
);
169+
i.end_cc_label <- child.end_cc_label;
170+
);
165171
);
166172
in
167173
List.iter visit_domain ring.roots;
@@ -208,9 +214,15 @@ let of_trace (trace : Trace.t) =
208214
and import_events events =
209215
events |> List.rev |> List.map (fun (ts, x) -> (time ts, map_event import x)) |> Array.of_list
210216
in
217+
let import_root { Trace.Ring.parent; cc } =
218+
{
219+
Ring.parent = Option.map (fun (ts, id) -> time ts, id) parent;
220+
cc = Option.map (fun (ts, i) -> time ts, import i) cc;
221+
}
222+
in
211223
let import_ring r =
212224
let events = List.map (fun (ts, s) -> time ts, s) r.Trace.Ring.events |> List.rev |> Array.of_list in
213-
let roots = List.map (fun (ts, i) -> time ts, import i) (List.rev r.roots) in
225+
let roots = List.map import_root (List.rev r.roots) in
214226
{ Ring.events; y = 0; height = 1; roots }
215227
in
216228
let rings = Trace.Rings.map import_ring trace.rings in

lib/render.ml

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ module Make (C : CANVAS) = struct
4747

4848
let running_fiber cr =
4949
C.set_source_rgb cr ~r:0.4 ~g:0.8 ~b:0.4
50+
51+
let suspended_fiber cr =
52+
C.set_source_rgb cr ~r:0.4 ~g:0.4 ~b:0.4
5053
end
5154

5255
(** Draw [msg] in the area (min_x, max_x) and ideally centred at [x]. *)
@@ -155,7 +158,7 @@ module Make (C : CANVAS) = struct
155158
let x1 = View.x_of_time v t1 in
156159
let w = x1 -. x0 in
157160
begin match stack with
158-
| `Suspend _ :: _ -> C.set_source_rgb cr ~r:0.4 ~g:0.4 ~b:0.4
161+
| `Suspend _ :: _ -> Style.suspended_fiber cr
159162
| `Span _ :: _ -> C.set_source_rgb cr ~r:0.5 ~g:0.9 ~b:0.5
160163
| [] -> Style.running_fiber cr
161164
end;
@@ -210,7 +213,7 @@ module Make (C : CANVAS) = struct
210213
done;
211214
fn (v.View.layout.duration, [])
212215

213-
let render_gc_events v cr (ring : Layout.Ring.t) =
216+
let render_gc_events v cr (ring : Layout.Ring.t) layer =
214217
let y = y_of_row v ring.y in
215218
let h = float ring.height *. Style.line_spacing in
216219
let prev_stack = ref [] in
@@ -227,23 +230,52 @@ module Make (C : CANVAS) = struct
227230
| [] -> ()
228231
| op :: p ->
229232
let g = 1.0 -. min 1.0 (0.1 *. float (List.length stack)) in
230-
C.set_source_rgba cr ~r:g ~g:g ~b:(g /. 2.) ~a:0.9;
231-
C.rectangle cr ~x:x0 ~y ~w ~h;
232-
C.fill cr;
233-
if p == !prev_stack then (
234-
let clip_area = (w -. 0.2, v.height) in
235-
if g < 0.5 then C.set_source_rgb cr ~r:1.0 ~g:1.0 ~b:1.0
236-
else C.set_source_rgb cr ~r:0.0 ~g:0.0 ~b:0.0;
237-
C.paint_text cr ~x:(x0 +. 2.) ~y:(y +. 12.) op
238-
~clip_area
239-
)
233+
match layer with
234+
| `Bg ->
235+
C.set_source_rgb cr ~r:g ~g:g ~b:(g /. 2.);
236+
C.rectangle cr ~x:x0 ~y ~w ~h;
237+
C.fill cr
238+
| `Fg ->
239+
if p == !prev_stack then (
240+
let clip_area = (w -. 0.2, v.height) in
241+
if g < 0.5 then C.set_source_rgb cr ~r:1.0 ~g:1.0 ~b:1.0
242+
else C.set_source_rgb cr ~r:0.0 ~g:0.0 ~b:0.0;
243+
C.paint_text cr ~x:(x0 +. 2.) ~y:(y +. 12.) op
244+
~clip_area
245+
)
240246
end;
241247
prev_stack := stack
242248
)
243249

244-
let render_ring v cr ring =
245-
render_gc_events v cr ring;
246-
List.iter (fun (_ts, cc) -> render_events v cr cc) ring.roots
250+
let link_domain v cr ~x (fiber : Layout.item) (ring : Layout.Ring.t) =
251+
let fiber_y = y_of_row v fiber.y +. Style.fiber_padding_top in
252+
let ring_y = y_of_row v ring.y in
253+
let (y1, y2) =
254+
if fiber.y < ring.y then (
255+
(fiber_y +. Style.fiber_height, ring_y +. float ring.height *. View.pixels_per_row)
256+
) else (
257+
fiber_y, ring_y
258+
)
259+
in
260+
Style.suspended_fiber cr;
261+
C.move_to cr ~x ~y:y1;
262+
C.line_to cr ~x ~y:y2;
263+
C.stroke cr
264+
265+
let render_ring_bg v cr ring =
266+
render_gc_events v cr ring `Bg;
267+
ring.roots |> List.iter @@ fun (root : Layout.Ring.root) ->
268+
C.set_line_width cr 4.0;
269+
root.parent |> Option.iter (fun (ts, parent) ->
270+
Layout.get v.layout parent |> Option.iter @@ fun (parent : Layout.item) ->
271+
let x = View.x_of_time v ts in
272+
link_domain v cr ~x parent ring
273+
)
274+
275+
let render_ring v cr (ring : Layout.Ring.t) =
276+
render_gc_events v cr ring `Fg;
277+
ring.roots |> List.iter @@ fun (root : Layout.Ring.root) ->
278+
root.cc |> Option.iter (fun (_ts, cc) -> render_events v cr cc)
247279

248280
let render_grid v cr =
249281
C.set_line_width cr 1.0;
@@ -272,6 +304,7 @@ module Make (C : CANVAS) = struct
272304
let render (v : View.t) cr =
273305
C.set_source_rgb cr ~r:0.9 ~g:0.9 ~b:0.9;
274306
C.paint cr;
307+
v.layout.rings |> Trace.Rings.iter (fun _id -> render_ring_bg v cr);
275308
render_grid v cr;
276309
C.set_source_rgb cr ~r:0.0 ~g:0.0 ~b:0.0;
277310
v.layout.rings |> Trace.Rings.iter (fun _id -> render_ring v cr)

lib/trace.ml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,15 @@ and item = {
3232
module Ring = struct
3333
type id = int
3434

35+
type root = {
36+
mutable parent : (timestamp * int) option;
37+
mutable cc : (timestamp * item) option;
38+
}
39+
3540
type t = {
3641
mutable current_fiber : int option;
3742
mutable events : (timestamp * string list) list;
38-
mutable roots : (timestamp * item) list;
43+
mutable roots : root list;
3944
}
4045

4146
let push t ts e =
@@ -136,7 +141,12 @@ let process_event t e =
136141
cc.parent <- Some parent_item;
137142
parent_fiber.inner_cc <- id
138143
| None ->
139-
ring.roots <- (timestamp, cc) :: ring.roots
144+
begin match ring.roots with
145+
| root :: _ when root.cc = None -> root.cc <- Some (timestamp, cc)
146+
| _ ->
147+
let root = { Ring.parent = None; cc = Some (timestamp, cc) } in
148+
ring.roots <- root :: ring.roots
149+
end
140150
end
141151
| "eio", "cc", Duration_end ->
142152
fiber_of_thread t thread |> Option.iter @@ fun fiber ->
@@ -189,6 +199,12 @@ let process_event t e =
189199
| "gc", _, Duration_end ->
190200
let r = ring_of_thread t thread |> Option.get in
191201
Ring.pop r timestamp
202+
| "eio", "domain-spawn", Instant ->
203+
ring_of_thread t thread |> Option.iter (fun (ring : Ring.t) ->
204+
let parent = List.assoc_opt "parent" args |> Option.get |> id_of_pointer in
205+
let root = { Ring.parent = Some (timestamp, parent); cc = None } in
206+
ring.roots <- root :: ring.roots
207+
)
192208
| _ -> ()
193209

194210
let process t reader =

src/record.ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ let callbacks t =
8282
Write.thread_wakeup ~cpu:ring_id ~ts t.fxt (Int64.of_int id)
8383
in
8484
(* Fmt.epr "%a@." Eio_runtime_events.pp_event e; *)
85+
(* Allow matching unknown events so we can build with older Eio versions if needed. *)
86+
let e = (e : Eio_runtime_events.event :> [> Eio_runtime_events.event]) in
8587
match e with
8688
| `Fiber id ->
8789
set_current_fiber id;
@@ -136,6 +138,10 @@ let callbacks t =
136138
Write.duration_begin t.fxt ~thread:(ring_thread t ring_id) ~ts ~name:"suspend-domain" ~category:"eio"
137139
| `Suspend_domain End ->
138140
Write.duration_end t.fxt ~thread:(ring_thread t ring_id) ~ts ~name:"suspend-domain" ~category:"eio"
141+
| `Domain_spawn parent ->
142+
Write.instant_event t.fxt ~thread ~ts ~name:"domain-spawn" ~category:"eio" ~args:[
143+
"parent", `Pointer (Int64.of_int parent);
144+
];
139145
| _ -> ()
140146
)
141147

0 commit comments

Comments
 (0)