//! Calls as an idea, a lens on the stack: the tree of every call of one //! function, which grows as the program runs (the frames themselves are //! `context.Backtrace` and the stack's words). It reads the recording's //! index of calls and returns, with what each was given and gave back, //! and is the last level a step reaches: a node appears after the //! registers that carried its argument have struck. const std = @import("std"); const sk = @import("sketch"); const anim = @import("anim"); const ui = @import("ui.zig"); const cursors = @import("cursor.zig"); const Cursor = cursors.Cursor; const Recording = @import("Recording.zig"); const context = @import("context.zig"); const Stage = @import("stage.zig").Stage; const ties = @import("ties.zig"); const rows = @import("rows.zig"); const theme = ui.theme; const time = theme.time; const Rect = ui.Rect; const t = theme.color.text; pub const Tree = struct { cursor: Cursor, stage: *const Stage, /// The function whose calls are the tree. function: []const u8, focus: bool = false, pub fn draw(k: *const Tree, f: *sk.Frame, at: anim.Placed) void { k.backtrace(f, at); const box = k.stage.box(at, .band); if (box.h < theme.metric.context.tag + 24) return; const u = context.at(f, at, box).faded(at.value(k.stage.drawing)); // A relayout moves the band: what is drawn in it stays inside it. u.clip(.{ .x = 0, .y = 0, .w = box.w, .h = box.h }); defer u.unclip(); const now = k.cursor.now(at); const rec = k.cursor.rec; const m = metric; const body = u.panel(box.w, box.h, u.print("calls of {s}", .{k.function}), .{ .focus = k.focus, .note = "each call a node \u{00b7} its result on its shoulder" }); const nodes = layout(f.scratch, rec, k.function) catch return; if (nodes.len == 0) return; var leaves: f32 = 0; for (nodes) |n| leaves = @max(leaves, n.x + 1); // The caller's box left of the root on its level; the leaves spread over the rest. const caller_room = m.caller[0] + 48; const pitch = (body.w - 2 * theme.metric.panel.pad_x - 2 * m.radius - caller_room) / @max(leaves - 1, 1); const left = theme.metric.panel.pad_x + caller_room + m.radius; const top = body.y + m.root; const since = now.since(.idea, 0); const returned = now.walking() and rec.depths[now.stops[0]] < rec.depths[now.stops[1]]; const moment = if (now.plan.gait == .sprint or since >= (if (returned) @as(f32, time.badge_after) else 0)) now.stops[0] else now.stops[1]; const edge = cursors.standard(now.moving(.idea, time.quick, time.quick)); const appears = now.moving(.idea, 0, time.snap); const at_of = struct { fn c(lf: f32, p: f32, tp: f32, n: Node) ui.Vec { return .{ lf + n.x * p, tp + @as(f32, @floatFromInt(n.level)) * metric.level }; } }.c; // The caller: gold until the first call, then waiting, ringed; an edge into the root. if (nodes[0].call.parent) |p| { const caller = rec.calls[p]; const root_c = at_of(left, pitch, top, nodes[0]); const r: Rect = .{ .x = theme.metric.panel.pad_x, .y = root_c[1] - m.caller[1] / 2, .w = m.caller[0], .h = m.caller[1] }; const called = nodes[0].call.enter <= moment; if (nodes[0].call.enter <= now.stops[0]) { const to = ui.Vec{ root_c[0] - m.radius, root_c[1] }; const from = ui.Vec{ r.x + r.w, root_c[1] }; const live = nodes[0].call.active(moment) or nodes[0].call.enter > moment; u.line(from, if (nodes[0].call.enter > now.stops[1]) sk.math.mix(from, to, edge) else to, if (live) ui.rgb(t.primary) else ui.rgb(theme.color.rule), theme.stroke.line); } if (!called) { u.fill(r, theme.mark(.current).solid); u.label(.mono_bold, theme.size.mono.context, r, .center, rec.name(caller.function), theme.mark(.current).on_solid); } else { u.fill(r, theme.color.page); u.frame(r, t.primary, theme.stroke.line); u.label(.mono, theme.size.mono.context, r, .center, rec.name(caller.function), t.primary); } } // Edges under nodes: the waiting path in primary. for (nodes) |n| { const parent = n.parent orelse continue; if (n.call.enter > now.stops[0]) continue; const from = at_of(left, pitch, top, nodes[parent]); var to = at_of(left, pitch, top, n); if (n.call.enter > now.stops[1]) to = sk.math.mix(from, to, edge); const live = n.call.active(moment); u.line(from, to, if (live or n.call.enter > moment) ui.rgb(t.primary) else ui.rgb(theme.color.rule), theme.stroke.line); } const deepest = deepestActive(nodes, moment); for (nodes, 0..) |n, i| { const c = at_of(left, pitch, top, n); // Where a projection from its frame's way back (the word its // call pushed, at the call's stack pointer) lands: under it, // there before the node, which comes when it arrives. if (n.call.enter <= now.stops[0]) ties.anchor(u, .{ .node_address = n.call.sp }, .{ .x = c[0] - m.radius, .y = c[1] - m.radius, .w = 2 * m.radius, .h = 2 * m.radius }); if (n.call.enter > moment) continue; const v = if (n.call.enter > now.stops[1]) u.faded(appears) else u; if (deepest == i) { v.disc(c, m.radius, ui.rgb(theme.mark(.current).solid), null, 0); } else if (n.call.active(moment)) { v.disc(c, m.radius - theme.stroke.line / 2, ui.rgb(theme.color.page), ui.rgb(t.primary), theme.stroke.line); } else v.disc(c, m.radius, ui.rgb(theme.color.raised), null, 0); } // Then the words, so shapes and text each go in one run. const size = theme.size.mono.small; for (nodes, 0..) |n, i| { if (n.call.enter > moment) continue; const c = at_of(left, pitch, top, n); const active = n.call.active(moment); const words = if (n.call.arg) |arg| u.print("{d}", .{@as(i64, @bitCast(arg))}) else "?"; const cell: Rect = .{ .x = c[0] - m.radius, .y = c[1] - m.radius, .w = 2 * m.radius, .h = 2 * m.radius }; const v = if (n.call.enter > now.stops[1]) u.faded(appears) else u; if (deepest == i) { v.label(.mono_bold, size, cell, .center, words, theme.mark(.current).on_solid); } else v.label(.mono, size, cell, .center, words, t.primary); if (active) continue; // Done: what it returned, on its shoulder; struck as it lands. const badge: Rect = .{ .x = c[0] + m.badge_at[0], .y = c[1] - m.badge_at[1], .w = m.badge, .h = m.badge }; const heat: ui.Heat = if (n.call.exit.? > now.stops[1]) now.heat(0, null, since - time.badge_after) else .none; const ink = u.cellWritten(badge, heat, theme.color.page); if (heat.level < 1) u.frame(badge, t.faint, theme.stroke.rule); u.label(ink.face, size, badge, .center, u.print("{d}", .{@as(i64, @bitCast(n.call.result))}), if (heat.level >= 1) ink.ink else ui.rgb(t.primary)); } } /// Its view: the backtrace, the waiting path read as rows, newest on /// top, with each call's arguments (`n=4`) and where it is. fn backtrace(k: *const Tree, f: *sk.Frame, at: anim.Placed) void { const box = k.stage.box(at, .view); const cm = theme.metric.context; if (box.h < cm.tag + cm.row) return; const u = context.at(f, at, box).faded(at.value(k.stage.view)); u.clip(.{ .x = 0, .y = 0, .w = box.w, .h = box.h }); defer u.unclip(); const now = k.cursor.now(at); const rec = k.cursor.rec; // A frame is its words on the stack: the view changes with memory. const moment = now.stop(.memory); var buf: [24]context.Frame = undefined; const chain = context.frames(rec, moment, &buf); const room = theme.layout.rowsIn(box.h); const body = u.panel(box.w, box.h, "backtrace", .{ .note = if (chain.len > room) u.print("the calls waiting \u{00b7} newest on top \u{00b7} {d} more", .{chain.len - room}) else "the calls waiting \u{00b7} newest on top" }); const open = rec.stack(f.scratch, moment); for (chain[0..@min(chain.len, room)], 0..) |frame, i| { const y = body.y + cm.pad + @as(f32, @floatFromInt(i)) * cm.row; const name = ui.symbol(u, rec.strings.get(frame.pc) orelse u.print("{x}", .{frame.pc})); // The call this frame is, by its place on the chain: its arguments. rows.callRow(u, y, box.w, i, name, context.callArguments(u, rec, open, i, frame.pc), context.frameWhere(u, rec, now.marked(), i, frame.pc), i == 0); } context.veiled(u, box, now.sprint_veil); } }; /// A call's arguments as its backtrace row and its span say them: its /// parameter's name and value, `n=4`; "" for one that takes nothing. pub fn arguments(u: ui.Ui, rec: *const Recording, c: Recording.Call) []const u8 { const f = for (rec.functions) |fun| { if (std.mem.eql(u8, fun.name, rec.name(c.function))) break fun; } else return ""; if (!f.takes) return ""; const arg = c.arg orelse return ""; const name = if (c.parameter_name) |id| rec.name(id) else f.param; // A pointer is an address, hex; a number is its decimal. if (rows.regionOf(rec.moment(c.enter), arg) != .none) return u.print("{s}={x}", .{ name, arg }); return u.print("{s}={d}", .{ name, @as(i64, @bitCast(arg)) }); } /// The tree's measures (design/README.md, `calls`): full size. const metric = theme.metric.calls; pub const Node = struct { call: Recording.Call, parent: ?usize, level: u32, /// In leaf slots: a leaf has a whole number, a parent sits over the /// middle of its children. x: f32, }; fn deepestActive(nodes: []const Node, moment: u64) ?usize { var best: ?usize = null; for (nodes, 0..) |n, i| { if (n.call.active(moment) and (best == null or n.level >= nodes[best.?].level)) best = i; } return best; } /// Every call of `function`, placed as a tidy tree of the whole run, so a /// node is where it will always be from the moment it appears. pub fn layout(arena: std.mem.Allocator, rec: *const Recording, function: []const u8) ![]Node { var nodes: std.ArrayList(Node) = .empty; // Calls come in the order they were made: a parent before its children. var index: std.AutoHashMapUnmanaged(u32, usize) = .empty; for (rec.calls, 0..) |call, i| { if (!std.mem.eql(u8, rec.name(call.function), function)) continue; const parent: ?usize = if (call.parent) |p| index.get(p) else null; try index.put(arena, @intCast(i), nodes.items.len); try nodes.append(arena, .{ .call = call, .parent = parent, .level = if (parent) |p| nodes.items[p].level + 1 else 0, .x = -1 }); } // Leaves left to right in call order; then each parent over its children, // deepest first (children come after their parent, so backwards). const lo = try arena.alloc(f32, nodes.items.len); const hi = try arena.alloc(f32, nodes.items.len); @memset(lo, std.math.inf(f32)); @memset(hi, -std.math.inf(f32)); var next_leaf: f32 = 0; for (nodes.items, 0..) |*n, i| { const leaf = for (nodes.items[i + 1 ..]) |other| { if (other.parent == i) break false; } else true; if (!leaf) continue; n.x = next_leaf; next_leaf += 1; } var i = nodes.items.len; while (i > 0) { i -= 1; const n = &nodes.items[i]; if (n.x < 0) n.x = (lo[i] + hi[i]) / 2; if (n.parent) |p| { lo[p] = @min(lo[p], n.x); hi[p] = @max(hi[p], n.x); } } return nodes.items; }