From 2f27fbe0aa2da0f85d599c1e5f3ac14d025f590c Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 11 Nov 2026 13:22:07 -0300 Subject: canim: the graph of a function's blocks From the user. A blocks lens draws a function's basic blocks as boxes with their offsets and visit counts, loops in their lanes with the turn count, the path's last edges gold, the pc's block gold, and the disassembly as its view; leave and turn carry the mark along an edge, with the compare's flags framed for the edge chosen. On the map the graph takes the disassembly's place with its rows. The layout is a pure function, tested on sort, reverse and libc's open (twelve blocks the run never reached fold into six boxes). The sort scene walks one trip of its inner loop block by block and sprints the middle on the map. Every scene's linear rows carry a rule and a count at each block's head. The stack's panel no longer reads registers from the file on every draw: a frame is 20 ms where it was 57. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_016Y9MNCKgYCGiANnEwcEQxa --- anim/test/gallery.script | 8 +++ anim/test/gallery/atoms.png | Bin 104960 -> 104822 bytes anim/test/gallery/graph-map.png | Bin 0 -> 158401 bytes anim/test/gallery/graph.png | Bin 0 -> 76868 bytes anim/test/gallery/hello.ztrace | Bin 0 -> 86647 bytes anim/test/gallery/loupe.png | Bin 237245 -> 237479 bytes anim/test/gallery/none.png | Bin 226785 -> 226735 bytes anim/test/gallery/rest.png | Bin 233677 -> 233721 bytes anim/test/gallery/rows.png | Bin 140384 -> 140530 bytes anim/test/gallery/sort.ztrace | Bin 0 -> 775616 bytes anim/test/gallery/step.png | Bin 232337 -> 232385 bytes anim/test/gallery/subject.png | Bin 209937 -> 210007 bytes anim/test/recordings.zig | 105 ++++++++++++++++++++++++++++++++++++++++ 13 files changed, 113 insertions(+) create mode 100644 anim/test/gallery/graph-map.png create mode 100644 anim/test/gallery/graph.png create mode 100644 anim/test/gallery/hello.ztrace create mode 100644 anim/test/gallery/sort.ztrace (limited to 'anim/test') diff --git a/anim/test/gallery.script b/anim/test/gallery.script index 993ff7e..461f100 100644 --- a/anim/test/gallery.script +++ b/anim/test/gallery.script @@ -31,3 +31,11 @@ console set t 19.8 screenshot {dir}/loupe.png frames 1 expect-same-screenshot {dir}/loupe.png test/gallery/loupe.png 24 0 0 1920 1040 +console set t 21.8 +screenshot {dir}/graph.png +frames 1 +expect-same-screenshot {dir}/graph.png test/gallery/graph.png 24 0 0 1920 1040 +console set t 25.5 +screenshot {dir}/graph-map.png +frames 1 +expect-same-screenshot {dir}/graph-map.png test/gallery/graph-map.png 24 0 0 1920 1040 diff --git a/anim/test/gallery/atoms.png b/anim/test/gallery/atoms.png index 6e64609..8da5c01 100644 Binary files a/anim/test/gallery/atoms.png and b/anim/test/gallery/atoms.png differ diff --git a/anim/test/gallery/graph-map.png b/anim/test/gallery/graph-map.png new file mode 100644 index 0000000..b16caf6 Binary files /dev/null and b/anim/test/gallery/graph-map.png differ diff --git a/anim/test/gallery/graph.png b/anim/test/gallery/graph.png new file mode 100644 index 0000000..ea9695e Binary files /dev/null and b/anim/test/gallery/graph.png differ diff --git a/anim/test/gallery/hello.ztrace b/anim/test/gallery/hello.ztrace new file mode 100644 index 0000000..2ea1f5c Binary files /dev/null and b/anim/test/gallery/hello.ztrace differ diff --git a/anim/test/gallery/loupe.png b/anim/test/gallery/loupe.png index 346bd44..7f07d94 100644 Binary files a/anim/test/gallery/loupe.png and b/anim/test/gallery/loupe.png differ diff --git a/anim/test/gallery/none.png b/anim/test/gallery/none.png index 39f0206..bac000d 100644 Binary files a/anim/test/gallery/none.png and b/anim/test/gallery/none.png differ diff --git a/anim/test/gallery/rest.png b/anim/test/gallery/rest.png index 9dfdbe2..c368a30 100644 Binary files a/anim/test/gallery/rest.png and b/anim/test/gallery/rest.png differ diff --git a/anim/test/gallery/rows.png b/anim/test/gallery/rows.png index 7e6ba84..dd85690 100644 Binary files a/anim/test/gallery/rows.png and b/anim/test/gallery/rows.png differ diff --git a/anim/test/gallery/sort.ztrace b/anim/test/gallery/sort.ztrace new file mode 100644 index 0000000..a7aced1 Binary files /dev/null and b/anim/test/gallery/sort.ztrace differ diff --git a/anim/test/gallery/step.png b/anim/test/gallery/step.png index b85cba0..19ffc00 100644 Binary files a/anim/test/gallery/step.png and b/anim/test/gallery/step.png differ diff --git a/anim/test/gallery/subject.png b/anim/test/gallery/subject.png index d912474..182f8ba 100644 Binary files a/anim/test/gallery/subject.png and b/anim/test/gallery/subject.png differ diff --git a/anim/test/recordings.zig b/anim/test/recordings.zig index 38798af..ce5a9db 100644 --- a/anim/test/recordings.zig +++ b/anim/test/recordings.zig @@ -105,3 +105,108 @@ test "a walked step is as long as what it changed: fire, a slot a level, the hol const line = quiet.reading(rec.lineMoment(4, from), rec.lineMoment(6, from)); try std.testing.expectApproxEqAbs(t.fire + t.step + t.rest + t.step_per_row, line.total, 1e-3); } + +/// The first moment whose pc is in the function named `name` (as the recording names it: `libc.so.6!open`). +fn firstIn(rec: *const Recording, name: []const u8) u64 { + for (rec.registers, 0..) |regs, i| { + const at = rec.strings.get(regs.rip) orelse continue; + if (std.mem.startsWith(u8, at, name) and at.len > name.len and at[name.len] == '+') return i; + } + unreachable; +} + +test "sort's graph: two loops in their lanes, the hot path straight down, the jle never taken" { + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + const graph = canim.graph; + const rec = canim.open(recordings.sort); + const at = firstIn(rec, "sort"); + var g = graph.read(arena.allocator(), rec, rec.end(), rec.registers[at].rip).?; + try expect(g.exact and g.text); + try expectEqual(@as(usize, 11), g.nodes.len); + for (g.nodes) |n| try expect(n.reached); + // The inner loop's `j > 0` is never false: its jle never taken. + const jle = g.edgeOf(g.nodeOf(g.start + 0x8a).?, g.nodeOf(g.start + 0xbc).?).?; + try expect(g.edges[jle].kind == .taken and g.edges[jle].total == 0); + // The compare's jne to the swap, ten times; the swap entered as often. + const swap = g.nodeOf(g.start + 0xc5).?; + try expectEqual(@as(u32, 10), g.edges[g.edgeOf(g.nodeOf(g.start + 0xbc).?, swap).?].count); + try expectEqual(@as(u32, 10), g.nodes[swap].visits); + graph.sizes(rec, rec.end(), &g, .map); + graph.place(&g, graph.metricOf(.map)); + var backs: usize = 0; + for (g.edges) |e| backs += @intFromBool(e.back); + try expectEqual(@as(usize, 2), backs); + // The outer loop (i++ back to the test) outermost, the inner (the swap back to `j > 0`) inside it. + const outer = g.edges[g.edgeOf(g.nodeOf(g.start + 0xff).?, g.nodeOf(g.start + 0x78).?).?]; + const inner = g.edges[g.edgeOf(swap, g.nodeOf(g.start + 0x8a).?).?]; + try expect(outer.back and inner.back and outer.lane == 0 and inner.lane == 1); + // The swap straight under the compare that goes to it ten times in ten; the way on beside it. + try expectEqual(g.nodes[g.nodeOf(g.start + 0xbc).?].col, g.nodes[swap].col); + try expect(g.nodes[g.nodeOf(g.start + 0xc3).?].col > g.nodes[swap].col); + // The jle and the loop's exit would cross blocks: the right channel. + try expectEqual(@as(u8, 6), g.edges[jle].n); + try expectEqual(g.channel, g.edges[jle].points[2][0]); + // On the map (1688 high): too tall; the long prologue folds, and the outer loop is panned into view whole. + var buf: [1 << 20]u8 = undefined; + var fba = std.heap.FixedBufferAllocator.init(&buf); + const room = 1688 - 24; + const in_loop = rec.lineMoment(12, at); + const laid = graph.layout(fba.allocator(), rec, in_loop, rec.registers[in_loop].rip, .map, room).?; + try expectEqual(@as(u32, 9), laid.nodes[0].shown); + const off = graph.pan(laid, room, graph.metricOf(.map)); + for (laid.nodes[1 .. laid.nodes.len - 1]) |n| try expect(n.y >= off and n.y + n.h <= off + room); +} + +test "reverse's graph: one loop, its exit in the right channel" { + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + const graph = canim.graph; + const rec = canim.open(recordings.list); + const at = firstIn(rec, "reverse"); + var g = graph.read(arena.allocator(), rec, rec.end(), rec.registers[at].rip).?; + try expectEqual(@as(usize, 4), g.nodes.len); + graph.sizes(rec, rec.end(), &g, .map); + graph.place(&g, graph.metricOf(.map)); + const layers = [_]u32{ 0, 1, 2, 3 }; + for (g.nodes, layers) |n, l| try expect(n.layer == l and n.col == 0); + // The body's jmp back to the test: the one edge that climbs, three turns. + const turn = g.edges[g.edgeOf(2, 1).?]; + try expect(turn.back and turn.count == 3); + try expectEqual(@as(u8, 6), g.edges[g.edgeOf(1, 3).?].n); + // The recording begins inside it: its first block was entered once. + try expectEqual(@as(u32, 1), g.nodes[0].visits); + try expect(g.h <= 1688 - 24); +} + +test "libc's open at its syscall: the blocks never reached folded, no loop made by folding" { + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + const graph = canim.graph; + const rec = canim.open(recordings.hello); + var at = firstIn(rec, "libc.so.6!open"); + var buf: [256]u8 = undefined; + while (!std.mem.eql(u8, rec.instruction(at, rec.registers[at].rip, &buf).?.text, "syscall")) at += 1; + const g = graph.layout(arena.allocator(), rec, at, rec.registers[at].rip, .map, 1688 - 24).?; + // Twenty blocks (two of `nop` padding left out), eight of them reached. + try expectEqual(@as(usize, 20), g.blocks); + var reached: usize = 0; + var folds: usize = 0; + for (g.nodes) |n| { + reached += @intFromBool(n.reached); + folds += @intFromBool(n.folded > 0); + } + try expectEqual(@as(usize, 8), reached); + try expectEqual(@as(usize, 12), g.foldedBlocks()); + try expect(folds >= 4 and folds < 12); + // open has no loop, and the folds make none. + for (g.edges) |e| try expect(!e.back); + // At its syscall: the blocks before entered once, those after not yet. + try expectEqual(g.nodeOf(rec.registers[at].rip).?, g.here.?); + for (g.nodes) |n| if (n.reached) try expectEqual(@as(u32, @intFromBool(n.start <= rec.registers[at].rip)), n.visits); + // Still taller than the map: it pans, the pc's block in view; as wide as two columns at most. + const room = 1688 - 24; + const off = graph.pan(g, room, graph.metricOf(.map)); + const here = g.nodes[g.here.?]; + try expect(here.y >= off and here.y + here.h <= off + room and g.w <= 1792); +} -- cgit v1.3