From 5922ee911b9a6604f60dc83be36ba9e6c9abac98 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 11 Nov 2026 02:11:12 -0300 Subject: canim: what the second review found, fixed A system call stepped over plays in order: the registers set, the crossing from the call row with the kernel's window under it, the stores after rax. A strike covers the bytes that changed. The band's view changes with the argument framing. In the descent a callee sits under its caller's return row, and rows fade before others glide. The heap scene follows the storyboard's nine captions: pushes at a run without marks, each free and the malloc that takes the head back walked, the tcache's loupe over the stack and closed with each advance, a chunk in neither list drawn, the freed node travelling to malloc's row. In the dive the map falls into the corner and becomes the minimap; on the map boxes are as long as their rows. A narrow read is shown at its width. The chart checks captions and one-frame changes. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_016Y9MNCKgYCGiANnEwcEQxa --- anim/src/syscalls.zig | 176 +++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 140 insertions(+), 36 deletions(-) (limited to 'anim/src/syscalls.zig') diff --git a/anim/src/syscalls.zig b/anim/src/syscalls.zig index 4a46170..ca98920 100644 --- a/anim/src/syscalls.zig +++ b/anim/src/syscalls.zig @@ -63,12 +63,15 @@ pub const Plan = struct { pub fn of(call: Recording.Syscall) Plan { var p: Plan = .{}; - // Down: the bytes it was given. Up: the bytes it wrote into the program. - const down: usize = @min(call.text.len, shown_bytes); + // Down: the bytes of a buffer it was given (a path is read as the + // call comes in: no work). Up: the bytes it wrote into the program. + const down: usize = if (bufferRead(call) != null) @min(call.text.len, shown_bytes) else 0; const up: u64 = if (fills(call)) |span| span.len else 0; p.bytes = @floatFromInt(@max(down, @min(up, 32))); - if (p.bytes > 0) p.back = p.fills + p.bytes * byte + 10.0 / 60.0; - p.returns = p.back - 6.0 / 60.0; + // The kernel answers when its work is done; a call with none (open, + // close) at frame 108, straight after its call has come in. + p.returns = if (p.bytes > 0) p.fills + p.bytes * byte + 6.0 / 60.0 else 108.0 / 60.0; + p.back = p.returns + 6.0 / 60.0; p.struck = p.back + cross; p.stored = p.struck + time.cascade; p.advance = p.stored + time.read_value; @@ -76,6 +79,25 @@ pub const Plan = struct { return p; } + /// The plan of the call this step crosses with: stepped over (the step + /// went on from the call row the cascade before it advanced onto), the + /// chart with its frame 18 at the step's first: no second fire. + pub fn in(now: cursors.Now, made: Recording.Syscall) Plan { + const p = of(made); + return if (fromCall(now.rec, now.stops[1])) p.shifted(-time.cascade) else p; + } + + fn shifted(p: Plan, by: f32) Plan { + var q = p; + inline for (.{ "registers", "enters", "leaves", "name", "rows", "card", "work", "fills", "returns", "back", "struck", "stored", "advance", "total" }) |field| @field(q, field) += by; + return q; + } + + /// When byte `k` of what the call fills lands in the program's memory. + pub fn filled(p: Plan, k: u64) f32 { + return p.fills + @as(f32, @floatFromInt(@min(k, 31))) * byte; + } + /// The same as the cursor's plan: the registers are the CPU's level, /// the bytes memory's. pub fn step(p: Plan) cursors.Plan { @@ -88,17 +110,65 @@ pub const Plan = struct { const per_row = 10; const shown_bytes = 2 * per_row; -/// One line of the program and the call into the kernel it makes, as one -/// step of the cursor, timed by the crossing. A line that makes no call -/// is walked like any other. +/// One line of the program and the call into the kernel it makes, timed +/// by the crossing. Stepped over (the line calls libc's wrapper), in two +/// steps of the cursor: the instructions before the call as a cascade, the +/// mark advancing onto the call's row; then the crossing from that row, +/// with what the line stores after the call in memory's slot, after rax. +/// A line that makes no call is walked like any other. pub fn step(c: cursors.Cursor, s: *anim.Frame, by: Recording.Stop) void { + const rec = c.rec; const from = c.moment(s); - const to = c.rec.next(by, from); - const made = for (c.rec.syscalls) |made| { + const to = rec.next(by, from); + const made = for (rec.syscalls) |made| { if (made.enter > from and made.enter <= to) break made; } else return c.walk(s, by, to); - const p = Plan.of(made); - c.step(s, to, p.step(), p.total); + // The call that leads to it: the line's first. + var call = from; + while (call < made.enter and !(if (rec.insn_branches[call]) |b| b.kind == .call else false)) call += 1; + if (call >= made.enter) call = from; + // 1, the instructions before the call: a cascade, the plaque onto the + // call's row 12 f after the last strike, no rest (the line stays current). + if (call > from) { + var before = c.reading(from, call); + const regs_before: f32 = @floatFromInt(@popCount(rec.registersChanged(from, call) & ~cursors.quiet)); + var last = before.at[0] + @min(@max(regs_before - 1, 0), 3) * cursors.write_apart; + for (rec.written(from, call)) |span| if (rec.drawn(span, from, call)) { + last = @max(last, before.at[1]); + }; + before.advance = last + 12.0 / 60.0; + before.travel = time.step; + before.total = before.advance + before.travel; + c.step(s, call, before, before.total); + } + // 2, the crossing from the call row (its chart's frame 18 first); 3, the stores after. + const p = if (call > from) Plan.of(made).shifted(-time.cascade) else Plan.of(made); + var plan = p.step(); + plan.fires = call == from; + // The store after the call: 18 f after rax, and after the registers the call left (6 f apart). + const regs: f32 = @floatFromInt(@popCount(rec.registersChanged(call, to) & ~cursors.quiet)); + const last_register = p.struck + @min(@max(regs - 1, 0), 3) * cursors.write_apart; + var stores = false; + for (rec.written(call, to)) |span| if (rec.drawn(span, call, to)) { + if (fills(made)) |fill| if (span.addr >= fill.addr and span.addr < fill.addr + fill.len) continue; + stores = true; + }; + plan.at[1] = @max(p.stored, last_register + cursors.write_apart); + plan.at[2] = plan.at[1]; + plan.advance = (if (stores) plan.at[1] else last_register) + time.read_value; + plan.total = plan.advance + time.step + time.rest; + c.step(s, to, plan, plan.total); +} + +/// In a crossing, when the kernel's write of the byte at `addr` lands in +/// the program's memory (seconds into the step): what a read fills strikes +/// there as the kernel's work does it, before it returns. Null for any other byte. +pub fn filledAt(now: cursors.Now, addr: u64) ?f32 { + if (!now.crossings) return null; + const made = inStep(now) orelse return null; + const span = fills(made) orelse return null; + if (addr < span.addr or addr >= span.addr + span.len) return null; + return Plan.in(now, made).filled(addr - span.addr); } /// How long after its slot in the cascade a register this step changed @@ -107,10 +177,16 @@ pub fn step(c: cursors.Cursor, s: *anim.Frame, by: Recording.Stop) void { pub fn crossingDelay(now: cursors.Now) f32 { if (!now.crossings) return 0; const made = inStep(now) orelse return 0; - const p = Plan.of(made); + const p = Plan.in(now, made); return p.struck - p.registers; } +/// Whether the instruction at moment `i` is a call: a step from it that +/// crosses into the kernel went over libc's wrapper. +fn fromCall(rec: *const Recording, i: u64) bool { + return if (i < rec.insn_branches.len) if (rec.insn_branches[i]) |b| b.kind == .call else false else false; +} + /// The argument registers of a syscall, in order. const carriers = [_][]const u8{ "rdi", "rsi", "rdx", "r10", "r8", "r9" }; @@ -190,7 +266,7 @@ pub const Crossing = struct { // A crossing's own row comes in once it is back (rax struck), 12 f. var arrives: f32 = 1; if (newest) |n| if (inStep(now) != null and now.walking()) { - const p = Plan.of(rec.syscalls[n]); + const p = Plan.in(now, rec.syscalls[n]); arrives = std.math.clamp((now.time - p.struck - 6.0 / 60.0) / theme.time.quick, 0, 1); }; if (newest) |n| if (n < first) { @@ -208,7 +284,9 @@ pub const Crossing = struct { const v = if (i == n) u.faded(arrives) else u; const y = top + @as(f32, @floatFromInt(j)) * row_h; logRow(v, rec, rec.syscalls[i], y, box.w - theme.metric.context.side); + // The newest's turned corner; the one before keeps it until the new row is in. if (i == n) v.corner(theme.metric.context.side, y + 2, theme.metric.context.corner, 1); + if (i + 1 == n and arrives < 1) u.faded(1 - arrives).corner(theme.metric.context.side, y + 2, theme.metric.context.corner, 1); } } @@ -257,39 +335,61 @@ pub const Crossing = struct { } } - /// Its view: the bytes the newest call is given or fills (its path, a - /// buffer), as a dump over the state column; struck as the kernel - /// fills them. + /// Its view: the bytes the call crossing in this step is given or + /// fills (its path, a buffer), as a dump over the state column. They + /// come in with the argument framing (the note before them out 12 f, + /// they in 12 f); what a read fills lands a byte every 2 f with the + /// kernel's work, struck; a write's buffer is framed as read while the + /// kernel takes it; and they clear with the log's new row. Between + /// crossings the view is clear. fn bytesView(c: *const Crossing, f: *sk.Frame, placed: anim.Placed) void { - const rec = c.cursor.rec; const now = c.cursor.now(placed); - var newest = latest(rec, now.stops[0]); - // No effect before its cause: until the kernel has taken the call, the last one's bytes. - if (newest) |n| if (inStep(now) != null and now.walking() and now.time < Plan.of(rec.syscalls[n]).name) { - newest = if (n > 0) n - 1 else null; - }; - const made: ?Recording.Syscall = if (newest) |n| rec.syscalls[n] else null; + const made: ?Recording.Syscall = if (now.walking()) inStep(now) else null; + const tm = now.time; + const quick = theme.time.quick; + // The call's bytes, and the note that says there is none crossing. + var shows: f32 = 0; + var clear: f32 = 1; + if (made) |given| { + const p = Plan.in(now, given); + const out = std.math.clamp((tm - p.struck - 6.0 / 60.0) / quick, 0, 1); + shows = std.math.clamp((tm - p.registers - quick) / quick, 0, 1) * (1 - out); + clear = @max(1 - std.math.clamp((tm - p.registers) / quick, 0, 1), std.math.clamp((tm - p.struck - 6.0 / 60.0 - quick) / quick, 0, 1)); + } const path_at: ?usize = if (made) |given| std.mem.indexOfScalar(u8, zt.syscalls.signature(given.nr), 's') else null; const span: ?Recording.Span = if (made) |given| bufferRead(given) orelse fills(given) orelse if (given.text.len > 0 and path_at != null) .{ .addr = given.args[path_at.?], .len = given.text.len + 1 } else null else null; - const scratch = context.at(f, placed, c.stage.box(placed, .view)); - const about = if (span) |sp| scratch.print("the {s}, {x} to {x}", .{ if (bufferRead(made.?) != null) "buffer given" else if (fills(made.?) != null) "buffer filled" else "path given", sp.addr, sp.addr + sp.len - 1 }) else "the call names no bytes"; - const v = hexdump.panel(f, placed, c.stage, about, &.{}); + if (span) |sp| @import("map.zig").measure(.view, (sp.len + 15) / 16); + const v = hexdump.panel(f, placed, c.stage, "", &.{}); defer v.u.unclip(); + const note_x = 28 + v.u.width(.sans_medium, theme.size.sans.tag, "its bytes") + 12; + const note_y = theme.metric.context.tag - 6; + if (clear > 0) v.u.faded(clear).text(.sans, theme.size.sans.micro, note_x, note_y, "no call crossing now", theme.color.text.faint); + if (shows <= 0) return; + const given = made.?; + const about = if (span) |sp| v.u.print("the {s}, {x} to {x}", .{ if (bufferRead(given) != null) "buffer given" else if (fills(given) != null) "buffer it fills" else "path given", sp.addr, sp.addr + sp.len - 1 }) else "the call names no bytes"; + v.u.faded(shows).text(.sans, theme.size.sans.micro, note_x, note_y, about, theme.color.text.faint); const sp = span orelse return; if (v.rows == 0) return; // The bytes it names are the field the view is about: a path or a // buffer of text reads as text, anything else is left to the eye. - const given = made.?; - const text = given.text[0..@min(given.text.len, sp.len)]; + const p = Plan.in(now, given); + // What a read fills is what the program's memory holds after it. + var text = given.text[0..@min(given.text.len, sp.len)]; + if (fills(given) != null) { + const after = v.u.f.scratch.alloc(u8, sp.len) catch return; + now.moment().read(sp.addr, after, null) catch return; + text = after; + } const printable = for (text) |byte| { if (!std.ascii.isPrint(byte) and byte != '\n' and byte != '\t' and byte != 0) break false; } else true; - hexdump.fielded(v, now, theme.metric.context.tag + theme.metric.context.pad, .{ + hexdump.fielded(.{ .u = v.u.faded(shows), .rows = v.rows, .box = v.box }, now, theme.metric.context.tag + theme.metric.context.pad, .{ .base = sp.addr, .len = sp.len, .fields = &.{.{ .at = 0, .len = sp.len, .reads = if (printable) .text else .bytes }}, .region = context.ink(context.regionOf(now.moment(), sp.addr)), - .given = .{ .addr = sp.addr, .bytes = text, .shows = 1 }, + .given = .{ .addr = sp.addr, .bytes = text, .shows = 1, .since = if (fills(given) != null) tm - p.fills else null }, + .read = if (bufferRead(given) != null) now.held(p.work) else 0, }); v.veil(now); } @@ -355,14 +455,18 @@ pub const Timeline = struct { }; test "a crossing is as long as what the call does" { - // A call with no work: 3.9 s. Fifteen bytes copied up: 5.0. - try std.testing.expectApproxEqAbs(@as(f32, 3.90), Plan.of(.{ .nr = 3, .args = @splat(0), .enter = 0 }).total, 1e-3); + // A call with no bytes to move: `returns` at 108 f, then the full climb; 3.8 s. Fifteen bytes filled: 5.0. + const close = Plan.of(.{ .nr = 3, .args = @splat(0), .enter = 0 }); + try std.testing.expectApproxEqAbs(@as(f32, 108.0 / 60.0), close.returns, 1e-3); + try std.testing.expectApproxEqAbs(@as(f32, 3.80), close.total, 1e-3); const read = Plan.of(.{ .nr = 0, .args = .{ 3, 0x1000, 32, 0, 0, 0 }, .enter = 0, .exit = 0, .result = 15 }); - try std.testing.expectApproxEqAbs(@as(f32, 4.97), read.total, 1e-2); - // What came back is in the kernel's rows before the arrow back leaves, + try std.testing.expectApproxEqAbs(@as(f32, 5.0), read.total, 1e-2); + // What came back is in the kernel's rows before the answer climbs (24 f), // struck in rax as it lands (armed on its way), then stored. try std.testing.expect(read.returns < read.back and read.struck == read.back + Plan.cross and read.struck - time.strike.arm > read.back and read.stored > read.struck); - // Nothing on the kernel's side before the arrow is through the line. + // Nothing on the kernel's side before the lane is through; the last byte filled before `returns`. try std.testing.expect(read.leaves >= read.enters and read.name >= read.enters + Plan.cross); - try std.testing.expect(read.fills > read.name and read.returns > read.fills + 15 * Plan.byte); + try std.testing.expect(read.fills > read.name and read.returns > read.filled(14)); + // Stepped over: the chart from its frame 18. + try std.testing.expectApproxEqAbs(read.enters - time.cascade, read.shifted(-time.cascade).enters, 1e-4); } -- cgit v1.3