From 93d901ec5b80c36cb6c9d9027a8b39de5adafaca Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sat, 24 Oct 2026 14:09:45 -0300 Subject: anim 1b: the probe measures input-to-present latency, where each frame's work goes (zones: body rows and colours, the GUI's build, shaping, rasterizing, a soft present's GPU wait, upload and flip) and the view presented; timedemos for throughput (stress), every file kind of a corpus (kinds) and every kind of pane (panes) --- src/FrameStats.zig | 619 +++++++++++++++++++++++++++++++++++++++++++++++++++-- src/body_layer.zig | 6 + src/draw.zig | 2 +- src/gui/gui.zig | 118 +++++++++- src/pardes.zig | 3 +- src/tty/tty.zig | 10 + 6 files changed, 725 insertions(+), 33 deletions(-) diff --git a/src/FrameStats.zig b/src/FrameStats.zig index 74f875df..0b252ff9 100644 --- a/src/FrameStats.zig +++ b/src/FrameStats.zig @@ -9,19 +9,25 @@ //! `Debug` shows the last second of it in its box, and `PARDES_FRAMES=path` //! writes the whole ring and its summary there when the session ends. //! -//! `PARDES_FRAMES_DEMO=scroll` runs `demo`, a timed script of scroll -//! inputs, in a pixel shell (the GUI feeds them through its own wheel and -//! key paths) and then ends the session: a timedemo, so two builds are -//! compared on the same motion, on a real display. +//! Each frame also carries its input-to-present latency (from the first +//! input it answers, the shell's timestamp of the key or wheel event, to +//! the end of its present), where its work went (`Zone`: the core's body +//! text, rows and colours; the shell's instance build, word shaping and +//! glyph rasterizing) and the view it showed, so a spike is attributed and +//! a scroll's throughput is counted in rows actually presented. +//! +//! `PARDES_FRAMES_DEMO=scroll` (or `stress`) runs a timed script of scroll +//! inputs (`named`) in a pixel shell (the GUI feeds them through its own +//! wheel and key paths) and then ends the session: a timedemo, so two +//! builds are compared on the same motion, on a real display. const std = @import("std"); const pardes = @import("pardes.zig"); const animation = @import("animation.zig"); const FrameStats = @This(); -/// 68 s at 60 Hz: a scripted run fits whole. -/// ponytail: a fixed ring, a file-backed log if a session's whole history matters. -pub const capacity = 4096; +/// 4.5 minutes at 60 Hz: the longest timedemo fits whole (1.3 MB). +pub const capacity = 16384; pub const Frame = struct { /// When the frame's step began, the input that woke it read: the @@ -41,8 +47,26 @@ pub const Frame = struct { /// The shell's motion in it: a scroll's picture offset from the text, /// rows (0 at rest). scroll: f32 = 0, + /// From the first input this frame answers to its present's end, µs; + /// 0 when it answers none. + latency_us: u32 = 0, + /// The focused pane's first line: with `scroll`, where the picture is. + view: i32 = 0, + /// µs a frame spent in each zone, and how many words the shell shaped + /// and glyphs it rasterized anew. + zones: [zone_count]u32 = @splat(0), + shaped: u16 = 0, + rastered: u16 = 0, + /// Bytes a cell shell wrote to its terminal for the frame. + bytes: u32 = 0, }; +/// Where a frame's work goes, as a game's profiler splits its budget. +/// The gui's are nested: shape and raster inside build; a soft present's +/// wait for the GPU, its upload of the frame and its flip after it. +pub const Zone = enum(u8) { body_text, body_rows, body_color, gui_build, gui_shape, gui_raster, gui_wait, gui_upload, gui_flip }; +pub const zone_count = std.enums.values(Zone).len; + ring: [capacity]Frame = @splat(.{}), /// Frames recorded since the start; the ring holds the last `capacity`. count: u64 = 0, @@ -57,13 +81,45 @@ armed: bool = false, jank: u64 = 0, over_budget: u64 = 0, skipped: u64 = 0, -/// What the shell sets while it draws the frame (Frame.scroll). +/// What the shell sets while it draws the frame (Frame.scroll), and the +/// pump (view). scroll: f32 = 0, +view: i32 = 0, +/// The first input not yet answered by a drawn frame, monotonic ns. +input_ns: u64 = 0, +/// This frame's zones so far. +zone_us: [zone_count]u32 = @splat(0), +shaped: u16 = 0, +rastered: u16 = 0, +bytes: u32 = 0, +/// When the timedemo started, monotonic ns (0: none ran), and which. +demo_ns: u64 = 0, +demo_name: []const u8 = "", /// The frame being timed: its marks, the monotonic clock's ns. mark_start: u64 = 0, mark_update: u64 = 0, mark_render: u64 = 0, +/// An input arrived at `at_ns`: the next drawn frame answers it. +pub fn input(stats: *FrameStats, at_ns: u64) void { + if (stats.input_ns == 0 or at_ns < stats.input_ns) stats.input_ns = at_ns; +} + +/// The pump had nothing to draw: what came in changed nothing on screen, +/// so no frame answers it. +pub fn idle(stats: *FrameStats) void { + stats.input_ns = 0; + // Nor was a frame owed after all (a motion that ended between frames): + // the next frame's gap is no motion's. + stats.armed = false; +} + +/// Adds the time since `from_ns` to `which`. +pub fn zone(stats: *FrameStats, which: Zone, from_ns: u64) void { + const us: u32 = @intCast(@min(std.math.maxInt(u32), (nowNs() -| from_ns) / std.time.ns_per_us)); + stats.zone_us[@backingInt(which)] +|= us; +} + pub fn nowNs() u64 { if (comptime !pardes.hosted) return 0; var ts: std.c.timespec = undefined; @@ -90,7 +146,18 @@ pub fn record(stats: *FrameStats, end_ns: u64, moving: bool, skipped: bool) void .moving = stats.armed, .skipped = skipped, .scroll = stats.scroll, + .latency_us = if (stats.input_ns == 0) 0 else @max(1, us.of(stats.input_ns, end_ns)), + .view = stats.view, + .zones = stats.zone_us, + .shaped = stats.shaped, + .rastered = stats.rastered, + .bytes = stats.bytes, }; + stats.bytes = 0; + stats.input_ns = 0; + stats.zone_us = @splat(0); + stats.shaped = 0; + stats.rastered = 0; // Half a period late is a refresh the motion missed. if (stats.armed and @as(u64, gap) * std.time.ns_per_us * 2 > stats.period_ns * 3) stats.jank += 1; if (@as(u64, frame.update_us + frame.render_us + frame.present_us) * std.time.ns_per_us > stats.period_ns) stats.over_budget += 1; @@ -120,7 +187,7 @@ pub const Spread = struct { max: u32 = 0, mean: f64 = 0, - pub const What = enum { work, update, render, present, gap }; + pub const What = enum { work, update, render, present, gap, latency }; pub fn of(frames: [2][]const Frame, comptime what: What) Spread { var values: [capacity]u32 = undefined; @@ -133,6 +200,7 @@ pub const Spread = struct { .render => f.render_us, .present => f.present_us, .gap => if (f.moving) f.gap_us else continue, + .latency => if (f.latency_us != 0) f.latency_us else continue, }; values[n] = v; n += 1; @@ -151,15 +219,20 @@ pub const Spread = struct { } }; -/// The Debug box's lines: the last second's work and gaps, and the -/// counters since the start, in ms. +/// The Debug box's lines: the last second's work, gaps and input latency, +/// and the jank counter since the start, in ms. pub fn summary(stats: *const FrameStats, buf: []u8) []const u8 { const recent = stats.last(@intCast(@max(1, std.time.ns_per_s / @max(1, stats.period_ns)))); const work = Spread.of(recent, .work); const gap = Spread.of(recent, .gap); - return std.fmt.bufPrint(buf, "frame {d:.2}/{d:.2}/{d:.2} ms\ngap {d:.2}/{d:.2} ms jank {d}", .{ - @as(f64, @floatFromInt(work.p50)) / 1000, @as(f64, @floatFromInt(work.p99)) / 1000, @as(f64, @floatFromInt(work.max)) / 1000, - @as(f64, @floatFromInt(gap.p50)) / 1000, @as(f64, @floatFromInt(gap.max)) / 1000, stats.jank, + const latency = Spread.of(stats.last(capacity / 16), .latency); + const ms = struct { + fn of(v: u32) f64 { + return @as(f64, @floatFromInt(v)) / 1000; + } + }; + return std.fmt.bufPrint(buf, "frame {d:.2}/{d:.2}/{d:.2} ms\ngap {d:.2}/{d:.2} ms jank {d}\ninput {d:.2}/{d:.2} ms", .{ + ms.of(work.p50), ms.of(work.p99), ms.of(work.max), ms.of(gap.p50), ms.of(gap.max), stats.jank, ms.of(latency.p50), ms.of(latency.p90), }) catch ""; } @@ -185,11 +258,21 @@ pub fn dump(stats: *const FrameStats, path: [:0]const u8) void { } write.line(fd, &buf, "# start_ms\tupdate_us\trender_us\tpresent_us\tgap_us\tmoving\tskipped\tscroll\n", .{}); const t0 = if (all[0].len != 0) all[0][0].start_ns else 0; + // The timedemo's phases, on the frames' clock. + if (stats.demo_ns != 0) if (named(stats.demo_name)) |steps| for (steps) |step| switch (step.act) { + .phase => |name| write.line(fd, &buf, "# phase {s} {d:.1}\n", .{ name, @as(f64, @floatFromInt((stats.demo_ns + @as(u64, step.at_ms) * std.time.ns_per_ms) -| t0)) / std.time.ns_per_ms }), + else => {}, + }; + write.line(fd, &buf, "# start_ms\tupdate_us\trender_us\tpresent_us\tgap_us\tmoving\tskipped\tscroll\tlatency_us\tview", .{}); + inline for (comptime std.enums.values(Zone)) |which| write.line(fd, &buf, "\t{t}_us", .{which}); + write.line(fd, &buf, "\tshaped\trastered\tbytes\n", .{}); for (all) |part| for (part) |f| { - write.line(fd, &buf, "{d:.3}\t{d}\t{d}\t{d}\t{d}\t{d}\t{d}\t{d:.4}\n", .{ + write.line(fd, &buf, "{d:.3}\t{d}\t{d}\t{d}\t{d}\t{d}\t{d}\t{d:.4}\t{d}\t{d}", .{ @as(f64, @floatFromInt(f.start_ns -| t0)) / std.time.ns_per_ms, f.update_us, f.render_us, f.present_us, f.gap_us, - @intFromBool(f.moving), @intFromBool(f.skipped), f.scroll, + @intFromBool(f.moving), @intFromBool(f.skipped), f.scroll, f.latency_us, f.view, }); + for (f.zones) |us| write.line(fd, &buf, "\t{d}", .{us}); + write.line(fd, &buf, "\t{d}\t{d}\t{d}\n", .{ f.shaped, f.rastered, f.bytes }); }; } @@ -200,6 +283,19 @@ pub const Step = struct { /// Wheel notches, down positive, as a mouse's. wheel: i8, key: pardes.Key, + /// A named stretch of the run starts (the dump lists them). + phase: []const u8, + /// A press and release of `button` over the focused pane, `at` of + /// the way down its body, on its rail or in its text. + click: struct { button: pardes.Mouse.Button, at: f32, rail: bool }, + /// Wheel notches over the focused pane's rail (the gutter). + rail_wheel: i8, + /// Text pasted (bracketed, as a terminal's paste arrives). + paste: []const u8, + /// A line run as Exec runs it, in the focused pane (a builtin, a + /// setting, else a shell command), the pane it opens focused: what + /// opens the other kinds of pane. + exec: []const u8, /// The run is over: the session ends. end, }, @@ -208,19 +304,41 @@ pub const Step = struct { /// Every kind of scroll a user makes, on a long file from its top: single /// notches, a fast spin of the wheel both ways (notches 8 ms apart, two a /// frame: each retargets the one before), half pages, pages, the far jumps -/// to the end and back, half pages faster than they settle, and the cursor +/// to the end (ge) and back (gg), half pages faster than they settle, and the cursor /// moving a line, a word and, in insert mode, a column at a time. +/// The timedemo PARDES_FRAMES_DEMO names, if any. +pub fn named(name: []const u8) ?[]const Step { + if (std.mem.eql(u8, name, "scroll")) return &demo; + if (std.mem.eql(u8, name, "stress")) return &stress; + if (std.mem.eql(u8, name, "kinds")) return &kinds; + if (std.mem.eql(u8, name, "panes")) return &panes; + if (std.mem.eql(u8, name, "one")) return &one; + if (std.mem.eql(u8, name, "jumps")) return &jumps; + if (std.mem.eql(u8, name, "longlines")) return &longlines; + if (std.mem.eql(u8, name, "clip-flick")) return &clip_flick; + if (std.mem.eql(u8, name, "clip-far")) return &clip_far; + if (std.mem.eql(u8, name, "clip-cursor")) return &clip_cursor; + if (std.mem.eql(u8, name, "clip-query")) return &clip_query; + if (std.mem.eql(u8, name, "clip-terminal")) return &clip_terminal; + if (std.mem.eql(u8, name, "clip-pages")) return &clip_pages; + return null; +} + pub const demo = blk: { @setEvalBranchQuota(20_000); var steps: [128]Step = undefined; var n: usize = 0; var t: u32 = 1500; + steps[n] = .{ .at_ms = t, .act = .{ .phase = "notches" } }; + n += 1; for (0..10) |_| { steps[n] = .{ .at_ms = t, .act = .{ .wheel = 1 } }; n += 1; t += 150; } t += 400; + steps[n] = .{ .at_ms = t, .act = .{ .phase = "spin" } }; + n += 1; for ([_]i8{ 1, -1 }) |dir| { for (0..24) |_| { steps[n] = .{ .at_ms = t, .act = .{ .wheel = dir } }; @@ -234,25 +352,33 @@ pub const demo = blk: { return .{ .cp = cp, .ctrl = true }; } }; + steps[n] = .{ .at_ms = t, .act = .{ .phase = "pages" } }; + n += 1; for ([_]u21{ 'd', 'd', 'd', 'u', 'u', 'f', 'b' }) |cp| { steps[n] = .{ .at_ms = t, .act = .{ .key = ctrl.key(cp) } }; n += 1; t += 600; } - steps[n] = .{ .at_ms = t, .act = .{ .key = .{ .cp = 'G', .text = "G" } } }; - n += 1; + for ([_]u21{ 'g', 'e' }, [_][]const u8{ "g", "e" }, 0..) |cp, text, i| { + steps[n] = .{ .at_ms = t + 20 * i, .act = .{ .key = .{ .cp = cp, .text = text } } }; + n += 1; + } t += 800; for (0..2) |i| { steps[n] = .{ .at_ms = t + 20 * i, .act = .{ .key = .{ .cp = 'g', .text = "g" } } }; n += 1; } t += 800; + steps[n] = .{ .at_ms = t, .act = .{ .phase = "retarget" } }; + n += 1; for (0..4) |_| { steps[n] = .{ .at_ms = t, .act = .{ .key = ctrl.key('d') } }; n += 1; t += 80; } t += 700; + steps[n] = .{ .at_ms = t, .act = .{ .phase = "cursor" } }; + n += 1; for ([_]struct { u21, []const u8, u32 }{ .{ 'j', "j", 60 }, .{ 'w', "w", 90 } }) |run| { for (0..10) |_| { steps[n] = .{ .at_ms = t, .act = .{ .key = .{ .cp = run[0], .text = run[1] } } }; @@ -279,6 +405,444 @@ pub const demo = blk: { break :blk out; }; +/// The scrolls every kind of text and pane is put through (`kinds`, +/// `panes`), from `t`: notches, a spin, half pages, PageDown held, the far +/// jumps, the cursor down a held j. 7.5 s. +fn scrollSet(steps: []Step, n: *usize, t: *u32) void { + const at = struct { + fn add(s: []Step, k: *usize, ms: u32, act: @FieldType(Step, "act")) void { + s[k.*] = .{ .at_ms = ms, .act = act }; + k.* += 1; + } + }; + for (0..6) |_| { + at.add(steps, n, t.*, .{ .wheel = 1 }); + t.* += 150; + } + for (0..100) |_| { + at.add(steps, n, t.*, .{ .wheel = 1 }); + t.* += 8; + } + t.* += 500; + for (0..3) |_| { + at.add(steps, n, t.*, .{ .key = .{ .cp = 'd', .ctrl = true } }); + t.* += 400; + } + for (0..45) |_| { + at.add(steps, n, t.*, .{ .key = .{ .cp = pardes.Key.page_down } }); + t.* += 33; + } + t.* += 400; + for ([_][]const u8{ "e", "g" }) |second| { + at.add(steps, n, t.*, .{ .key = .{ .cp = 'g', .text = "g" } }); + at.add(steps, n, t.* + 20, .{ .key = .{ .cp = second[0], .text = second } }); + t.* += 600; + } + for (0..30) |_| { + at.add(steps, n, t.*, .{ .key = .{ .cp = 'j', .text = "j" } }); + t.* += 33; + } + t.* += 500; +} + +/// `scrollSet` alone, on the file the session opened: one kind, for a +/// profiler's run. +pub const one = blk: { + @setEvalBranchQuota(20_000); + var steps: [512]Step = undefined; + var n: usize = 0; + var t: u32 = 1500; + steps[n] = .{ .at_ms = t, .act = .{ .phase = "one" } }; + n += 1; + scrollSet(&steps, &n, &t); + steps[n] = .{ .at_ms = t, .act = .end }; + n += 1; + const out = steps[0..n].*; + break :blk out; +}; + +/// The samples' scenes (docs/next-anim.md), a few seconds each, recorded +/// with PARDES_FRAMES_CAPTURE: a wheel spun hard; Ctrl-d held then ge and +/// gg; j held then the thin caret typing and arrowing; a `/` typed and its +/// places stepped with Ctrl-n; a command's output then the wheel over it; +/// Ctrl-f a page at a time. +fn clipSteps(comptime which: usize, steps: *[256]Step) usize { + @setEvalBranchQuota(100_000); + var n: usize = 0; + var t: u32 = 1200; + const add = struct { + fn step(s: []Step, k: *usize, at: *u32, gap: u32, act: @FieldType(Step, "act")) void { + s[k.*] = .{ .at_ms = at.*, .act = act }; + k.* += 1; + at.* += gap; + } + }; + switch (which) { + 0 => for (0..150) |_| add.step(steps, &n, &t, 12, .{ .wheel = 1 }), + 1 => { + for (0..36) |_| add.step(steps, &n, &t, 33, .{ .key = .{ .cp = 'd', .ctrl = true } }); + t += 600; + add.step(steps, &n, &t, 20, .{ .key = .{ .cp = 'g', .text = "g" } }); + add.step(steps, &n, &t, 900, .{ .key = .{ .cp = 'e', .text = "e" } }); + add.step(steps, &n, &t, 20, .{ .key = .{ .cp = 'g', .text = "g" } }); + add.step(steps, &n, &t, 900, .{ .key = .{ .cp = 'g', .text = "g" } }); + }, + 2 => { + for (0..45) |_| add.step(steps, &n, &t, 33, .{ .key = .{ .cp = 'j', .text = "j" } }); + t += 300; + add.step(steps, &n, &t, 300, .{ .key = .{ .cp = 'i', .text = "i" } }); + for ("the quick brown fox jumps") |ch| add.step(steps, &n, &t, 60, .{ .key = .{ .cp = ch, .text = &.{ch} } }); + for (0..10) |_| add.step(steps, &n, &t, 70, .{ .key = .{ .cp = pardes.Key.left } }); + for (0..4) |_| add.step(steps, &n, &t, 90, .{ .key = .{ .cp = pardes.Key.up } }); + add.step(steps, &n, &t, 400, .{ .key = .{ .cp = pardes.Key.escape } }); + }, + 3 => { + for ("/pane.") |ch| add.step(steps, &n, &t, 180, .{ .key = .{ .cp = ch, .text = &.{ch} } }); + t += 300; + for (0..8) |_| add.step(steps, &n, &t, 300, .{ .key = .{ .cp = 'n', .ctrl = true } }); + }, + 4 => { + add.step(steps, &n, &t, 1800, .{ .exec = "cat stress.zig" }); + for (0..60) |_| add.step(steps, &n, &t, 16, .{ .wheel = -1 }); + t += 400; + for (0..12) |_| add.step(steps, &n, &t, 150, .{ .wheel = 1 }); + }, + else => for (0..8) |_| add.step(steps, &n, &t, 450, .{ .key = .{ .cp = 'f', .ctrl = true } }), + } + t += 800; + add.step(steps, &n, &t, 0, .end); + return n; +} + +const clip_flick = blk: { + var steps: [256]Step = undefined; + const n = clipSteps(0, &steps); + const out = steps[0..n].*; + break :blk out; +}; +const clip_far = blk: { + var steps: [256]Step = undefined; + const n = clipSteps(1, &steps); + const out = steps[0..n].*; + break :blk out; +}; +const clip_cursor = blk: { + var steps: [256]Step = undefined; + const n = clipSteps(2, &steps); + const out = steps[0..n].*; + break :blk out; +}; +const clip_query = blk: { + var steps: [256]Step = undefined; + const n = clipSteps(3, &steps); + const out = steps[0..n].*; + break :blk out; +}; +const clip_terminal = blk: { + var steps: [256]Step = undefined; + const n = clipSteps(4, &steps); + const out = steps[0..n].*; + break :blk out; +}; +const clip_pages = blk: { + var steps: [256]Step = undefined; + const n = clipSteps(5, &steps); + const out = steps[0..n].*; + break :blk out; +}; + +/// The cursor along long lines (a minified JS, a one-line JSON, six 360 KB +/// lines), Wrap on and then off (off, the view scrolls sideways): w and e +/// held, l held, $ and 0, f and t, gl and gh, typing at a line's end and in +/// its middle, a selection swept with held L and W. +pub const longlines = blk: { + @setEvalBranchQuota(1_000_000); + var steps: [8192]Step = undefined; + var n: usize = 0; + var t: u32 = 1500; + const add = struct { + fn step(s: []Step, k: *usize, at: *u32, gap: u32, act: @FieldType(Step, "act")) void { + s[k.*] = .{ .at_ms = at.*, .act = act }; + k.* += 1; + at.* += gap; + } + fn keys(s: []Step, k: *usize, at: *u32, gap: u32, text: []const u8, times: usize) void { + for (0..times) |_| for (text, 0..) |_, i| step(s, k, at, gap, .{ .key = .{ .cp = text[i], .text = text[i .. i + 1] } }); + } + }; + for ([_][]const u8{ "wrap", "nowrap" }) |mode| { + if (std.mem.eql(u8, mode, "nowrap")) add.step(&steps, &n, &t, 300, .{ .exec = "Wrap" }); + for ([_][]const u8{ "minified.js", "oneline.json", "hugelines.txt" }) |file| { + add.step(&steps, &n, &t, 1500, .{ .exec = "Look kinds/" ++ file }); + const tag = file ++ "-" ++ mode; + add.keys(&steps, &n, &t, 400, "gg", 1); + add.step(&steps, &n, &t, 0, .{ .phase = "w-held-" ++ tag }); + add.keys(&steps, &n, &t, 33, "w", 60); + add.step(&steps, &n, &t, 0, .{ .phase = "e-held-" ++ tag }); + add.keys(&steps, &n, &t, 33, "e", 60); + add.step(&steps, &n, &t, 0, .{ .phase = "l-held-" ++ tag }); + add.keys(&steps, &n, &t, 33, "l", 90); + add.step(&steps, &n, &t, 0, .{ .phase = "dollar-zero-" ++ tag }); + add.keys(&steps, &n, &t, 600, "$0", 2); + add.step(&steps, &n, &t, 0, .{ .phase = "f-t-" ++ tag }); + add.keys(&steps, &n, &t, 150, "f,", 8); + add.keys(&steps, &n, &t, 150, "t ", 8); + add.step(&steps, &n, &t, 0, .{ .phase = "gl-gh-" ++ tag }); + add.keys(&steps, &n, &t, 500, "glgh", 3); + add.step(&steps, &n, &t, 0, .{ .phase = "insert-end-" ++ tag }); + add.keys(&steps, &n, &t, 300, "$a", 1); + add.keys(&steps, &n, &t, 45, "typed at the end ", 2); + add.step(&steps, &n, &t, 300, .{ .key = .{ .cp = pardes.Key.escape } }); + add.step(&steps, &n, &t, 0, .{ .phase = "insert-middle-" ++ tag }); + add.keys(&steps, &n, &t, 40, "0", 1); + add.keys(&steps, &n, &t, 40, "40000l", 1); + add.keys(&steps, &n, &t, 300, "i", 1); + add.keys(&steps, &n, &t, 45, "typed in the middle ", 2); + add.step(&steps, &n, &t, 300, .{ .key = .{ .cp = pardes.Key.escape } }); + add.step(&steps, &n, &t, 0, .{ .phase = "sweep-L-W-" ++ tag }); + add.keys(&steps, &n, &t, 33, "L", 40); + add.keys(&steps, &n, &t, 33, "W", 30); + // Del twice: the second throws the typing away. + add.step(&steps, &n, &t, 300, .{ .exec = "Del" }); + add.step(&steps, &n, &t, 600, .{ .exec = "Del" }); + } + } + add.step(&steps, &n, &t, 0, .end); + const out = steps[0..n].*; + break :blk out; +}; + +/// What insert mode's paste pastes: 200 lines. +const pasted = blk: { + const line = "a pasted line, one of two hundred\n"; + var out: [line.len * 200]u8 = undefined; + for (0..200) |i| @memcpy(out[i * line.len ..][0..line.len], line); + break :blk out; +}; + +/// Every other way a view moves, each its own phase, on the session's file +/// (a big one) and then on a small one: the counted jumps (12G, 9000G, gg), +/// the upward pages, a search's n/N, a query's Ctrl-n moving its owner's +/// view, `*`, Looks at `:40`, at a far line and at another file (a new +/// pane: nothing to glide from), the jumplist both ways, the rail's +/// buttons and the wheel over it, and insert mode: typing, arrows, Enter at +/// the bottom of the view, a 200-line paste. +pub const jumps = blk: { + @setEvalBranchQuota(400_000); + var steps: [2048]Step = undefined; + var n: usize = 0; + var t: u32 = 1500; + const add = struct { + fn step(s: []Step, k: *usize, at: *u32, gap: u32, act: @FieldType(Step, "act")) void { + s[k.*] = .{ .at_ms = at.*, .act = act }; + k.* += 1; + at.* += gap; + } + fn text(s: []Step, k: *usize, at: *u32, gap: u32, keys: []const u8) void { + for (keys, 0..) |_, i| step(s, k, at, gap, .{ .key = .{ .cp = keys[i], .text = keys[i .. i + 1] } }); + } + fn key(s: []Step, k: *usize, at: *u32, gap: u32, cp: u21, ctrl: bool, times: usize) void { + for (0..times) |_| step(s, k, at, gap, .{ .key = .{ .cp = cp, .ctrl = ctrl } }); + } + }; + for ([_][]const u8{ "big", "small" }) |size| { + if (std.mem.eql(u8, size, "small")) add.step(&steps, &n, &t, 1200, .{ .exec = "Look kinds/guide.typ" }); + add.step(&steps, &n, &t, 0, .{ .phase = "counted-G-" ++ size }); + for ([_][]const u8{ "12G", "300G", "9000G", "gg", "ge", "gg" }) |keys| { + add.text(&steps, &n, &t, 20, keys); + t += 700; + } + add.step(&steps, &n, &t, 0, .{ .phase = "pages-up-" ++ size }); + add.text(&steps, &n, &t, 700, "ge"); + add.key(&steps, &n, &t, 500, 'u', true, 3); + add.key(&steps, &n, &t, 500, 'b', true, 3); + add.key(&steps, &n, &t, 500, pardes.Key.page_up, false, 3); + add.step(&steps, &n, &t, 0, .{ .phase = "search-n-" ++ size }); + add.text(&steps, &n, &t, 700, "gg"); + add.text(&steps, &n, &t, 120, "/const "); + add.key(&steps, &n, &t, 600, pardes.Key.enter, false, 1); + add.text(&steps, &n, &t, 400, "nnnnnnNNNNNN"); + add.step(&steps, &n, &t, 0, .{ .phase = "rail-" ++ size }); + add.text(&steps, &n, &t, 700, "ge"); + for ([_]pardes.Mouse.Button{ .left, .left, .right, .right, .middle }, [_]f32{ 0.8, 0.3, 0.6, 0.9, 0.5 }) |button, at| + add.step(&steps, &n, &t, 600, .{ .click = .{ .button = button, .at = at, .rail = true } }); + for (0..8) |_| add.step(&steps, &n, &t, 120, .{ .rail_wheel = 1 }); + t += 500; + add.step(&steps, &n, &t, 0, .{ .phase = "query-ctrl-n-" ++ size }); + add.text(&steps, &n, &t, 120, "/pub fn"); + add.key(&steps, &n, &t, 250, 'n', true, 8); + add.key(&steps, &n, &t, 250, 'p', true, 4); + add.key(&steps, &n, &t, 700, pardes.Key.escape, false, 1); + add.step(&steps, &n, &t, 0, .{ .phase = "star-" ++ size }); + add.text(&steps, &n, &t, 300, "ggww*"); + add.text(&steps, &n, &t, 500, "nnnn"); + add.step(&steps, &n, &t, 0, .{ .phase = "look-" ++ size }); + add.step(&steps, &n, &t, 900, .{ .exec = "Look :40" }); + add.step(&steps, &n, &t, 900, .{ .exec = "Look :7000" }); + add.step(&steps, &n, &t, 900, .{ .exec = "Look :200" }); + add.step(&steps, &n, &t, 0, .{ .phase = "jumplist-" ++ size }); + add.key(&steps, &n, &t, 600, 'o', true, 3); + add.key(&steps, &n, &t, 600, 'i', true, 3); + add.text(&steps, &n, &t, 40, " jo"); + t += 600; + add.text(&steps, &n, &t, 40, " ji"); + t += 600; + add.step(&steps, &n, &t, 0, .{ .phase = "insert-" ++ size }); + add.text(&steps, &n, &t, 300, "ggi"); + for (0..4) |_| add.text(&steps, &n, &t, 45, "the quick brown fox "); + add.key(&steps, &n, &t, 60, pardes.Key.left, false, 12); + add.key(&steps, &n, &t, 60, pardes.Key.right, false, 12); + add.key(&steps, &n, &t, 60, pardes.Key.down, false, 6); + // Enter after Enter until the view has to follow, a line at a time. + add.key(&steps, &n, &t, 70, pardes.Key.enter, false, 60); + add.step(&steps, &n, &t, 600, .{ .paste = &pasted }); + add.key(&steps, &n, &t, 700, pardes.Key.escape, false, 1); + } + add.step(&steps, &n, &t, 0, .{ .phase = "look-new-pane" }); + add.step(&steps, &n, &t, 1500, .{ .exec = "Look ../../src/layout.zig:9000" }); + add.step(&steps, &n, &t, 0, .end); + const out = steps[0..n].*; + break :blk out; +}; + +/// Each file of the corpus (`.scratch/tools/corpus.sh`) is opened by name, +/// relative to the first file's directory, and put through `scrollSet`; +/// the long-line ones a second time with Wrap off. +pub const kinds = blk: { + @setEvalBranchQuota(200_000); + var steps: [4096]Step = undefined; + var n: usize = 0; + var t: u32 = 1500; + const files = [_][]const u8{ "generated-parser.c", "commands.rs", "tables.py", "jquery.js", "minified.js", "grammar.json", "oneline.json", "CHANGELOG.md", "cascade.html", "simdutf.cpp", "parser.ts", "fcnal-test.sh", "data.go", "guide.typ", "unicode.txt", "big.log", "words.txt", "hugelines.txt" }; + for (files ++ [_][]const u8{ "minified.js!", "oneline.json!", "hugelines.txt!" }) |file| { + const unwrapped = file[file.len - 1] == '!'; + const name = if (unwrapped) file[0 .. file.len - 1] else file; + // Wrap goes off once, before the second round. + if (unwrapped and std.mem.eql(u8, name, "minified.js")) { + steps[n] = .{ .at_ms = t, .act = .{ .exec = "Wrap" } }; + n += 1; + t += 200; + } + steps[n] = .{ .at_ms = t, .act = .{ .exec = "Look kinds/" ++ name } }; + n += 1; + t += 1200; + steps[n] = .{ .at_ms = t, .act = .{ .phase = file } }; + n += 1; + scrollSet(&steps, &n, &t); + steps[n] = .{ .at_ms = t, .act = .{ .exec = "Del" } }; + n += 1; + t += 400; + } + steps[n] = .{ .at_ms = t, .act = .end }; + n += 1; + const out = steps[0..n].*; + break :blk out; +}; + +/// The other kinds of pane, opened from the first file's directory, each +/// put through `scrollSet`: a command's fast output (the stress file, then +/// `yes`), a +Pager, a directory, a PDF, a picture, Help's output pane, a +/// +Diff, and the query pane typed into (its rows refilled each key, its +/// owner's places drawn). +pub const panes = blk: { + @setEvalBranchQuota(200_000); + var steps: [4096]Step = undefined; + var n: usize = 0; + var t: u32 = 1500; + const Kind = struct { name: []const u8, open: []const u8, settle: u32 }; + for ([_]Kind{ + .{ .name = "command-cat", .open = "cat stress.zig", .settle = 300 }, + .{ .name = "command-yes", .open = "yes | head -200000", .settle = 300 }, + .{ .name = "pager", .open = "cat stress.zig | pardes -", .settle = 2500 }, + .{ .name = "directory", .open = "Look kinds/", .settle = 1200 }, + .{ .name = "pdf", .open = "Look ../../docs/design.pdf", .settle = 2000 }, + .{ .name = "image", .open = "Look ../../docs/screenshots/atelier.png", .settle = 1200 }, + .{ .name = "help", .open = "Help", .settle = 1200 }, + .{ .name = "changelog", .open = "Changelog", .settle = 1200 }, + }) |kind| { + steps[n] = .{ .at_ms = t, .act = .{ .phase = kind.name } }; + n += 1; + steps[n] = .{ .at_ms = t, .act = .{ .exec = kind.open } }; + n += 1; + // A command's output streams in while it is scrolled. + t += kind.settle; + scrollSet(&steps, &n, &t); + steps[n] = .{ .at_ms = t, .act = .{ .exec = "Del" } }; + n += 1; + t += 600; + } + steps[n] = .{ .at_ms = t, .act = .{ .phase = "query-typing" } }; + n += 1; + steps[n] = .{ .at_ms = t, .act = .{ .key = .{ .cp = '/', .text = "/" } } }; + n += 1; + t += 300; + for ("pane.vie") |ch| { + steps[n] = .{ .at_ms = t, .act = .{ .key = .{ .cp = ch, .text = &.{ch} } } }; + n += 1; + t += 150; + } + for (0..12) |_| { + steps[n] = .{ .at_ms = t, .act = .{ .key = .{ .cp = 'n', .ctrl = true } } }; + n += 1; + t += 120; + } + steps[n] = .{ .at_ms = t, .act = .{ .key = .{ .cp = pardes.Key.escape } } }; + n += 1; + t += 800; + steps[n] = .{ .at_ms = t, .act = .end }; + n += 1; + const out = steps[0..n].*; + break :blk out; +}; + +/// Throughput on a big file (layout.zig x23, 30k lines): each input held +/// for three seconds at a keyboard's repeat (33 ms) or a wheel spun hard (a +/// notch every 8 ms), so the picture has to keep up with more than it can +/// show: a spin, held Ctrl-d, held j, held PageDown, then the far jumps. +pub const stress = blk: { + @setEvalBranchQuota(100_000); + var steps: [1024]Step = undefined; + var n: usize = 0; + var t: u32 = 1500; + const Held = struct { name: []const u8, key: ?pardes.Key, every: u32 }; + for ([_]Held{ + .{ .name = "spin", .key = null, .every = 8 }, + .{ .name = "held-ctrl-d", .key = .{ .cp = 'd', .ctrl = true }, .every = 33 }, + .{ .name = "held-j", .key = .{ .cp = 'j', .text = "j" }, .every = 33 }, + .{ .name = "held-pagedown", .key = .{ .cp = pardes.Key.page_down }, .every = 33 }, + }) |held| { + // Each from the top. + for ([_]u32{ 0, 20 }) |dt| { + steps[n] = .{ .at_ms = t + dt, .act = .{ .key = .{ .cp = 'g', .text = "g" } } }; + n += 1; + } + t += 800; + steps[n] = .{ .at_ms = t, .act = .{ .phase = held.name } }; + n += 1; + const end = t + 3000; + while (t < end) : (t += held.every) { + steps[n] = .{ .at_ms = t, .act = if (held.key) |key| .{ .key = key } else .{ .wheel = 1 } }; + n += 1; + } + t += 1000; + } + steps[n] = .{ .at_ms = t, .act = .{ .phase = "far" } }; + n += 1; + for (0..3) |_| { + for ([_][]const u8{ "e", "g" }) |second| { + for ([_][]const u8{ "g", second }, 0..) |text, i| { + steps[n] = .{ .at_ms = t + 20 * @as(u32, i), .act = .{ .key = .{ .cp = text[0], .text = text } } }; + n += 1; + } + t += 700; + } + } + steps[n] = .{ .at_ms = t + 500, .act = .end }; + n += 1; + const out = steps[0..n].*; + break :blk out; +}; + test "frame stats: gaps into a motion count as jank, idle gaps do not, and the ring keeps the last frames" { var stats: FrameStats = .{ .period_ns = 16 * std.time.ns_per_ms }; const ms = std.time.ns_per_ms; @@ -321,7 +885,18 @@ test "frame stats: gaps into a motion count as jank, idle gaps do not, and the r try std.testing.expectEqual(@as(usize, 10), tail[0].len + tail[1].len); const newest = if (tail[1].len != 0) tail[1][tail[1].len - 1] else tail[0][tail[0].len - 1]; try std.testing.expectEqual(@as(f32, capacity + 4), newest.scroll); - // The demo ends, and its steps are in time order. - try std.testing.expect(demo[demo.len - 1].act == .end); - for (demo[1..], demo[0 .. demo.len - 1]) |b, a| try std.testing.expect(b.at_ms >= a.at_ms); + // Each timedemo ends, and its steps are in time order. + for ([_][]const Step{ &demo, &stress, &kinds, &panes, &one, &jumps, &longlines }) |steps| { + try std.testing.expect(steps[steps.len - 1].act == .end); + for (steps[1..], steps[0 .. steps.len - 1]) |b, a| try std.testing.expect(b.at_ms >= a.at_ms); + } + // An input is answered by the next drawn frame, and only by it. + stats.input(t - 2 * ms); + stats.mark_start = t; + stats.mark_update = t; + stats.mark_render = t; + stats.record(t + 3 * ms, false, false); + try std.testing.expectEqual(@as(u32, 5000), stats.last(1)[0][0].latency_us); + stats.record(t + 4 * ms, false, false); + try std.testing.expectEqual(@as(u32, 0), stats.last(1)[0][0].latency_us); } diff --git a/src/body_layer.zig b/src/body_layer.zig index 3684605f..d4ca6d69 100644 --- a/src/body_layer.zig +++ b/src/body_layer.zig @@ -255,7 +255,9 @@ pub fn renderBody(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane .bg = pane_bg, }; const tz_body = tracy.zone(@src(), "bodyText"); + const probe_text = pardes.FrameStats.nowNs(); const body = built orelse try bodyText(p, arena, pane); + p.frames.zone(.body_text, probe_text); tz_body.end(); // A select sweep begun on the tag and dragged into the body marks the // body rows it crossed only where no text is: it is laid down before @@ -268,6 +270,8 @@ pub fn renderBody(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane // this is what writes it into the surface. const tz_rows = tracy.zone(@src(), "paneBodyRows"); defer tz_rows.end(); + const probe_rows = pardes.FrameStats.nowNs(); + defer p.frames.zone(.body_rows, probe_rows); var it = std.mem.splitScalar(u8, body, '\n'); var i: u16 = 0; while (it.next()) |line| : (i += 1) { @@ -277,6 +281,8 @@ pub fn renderBody(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane } const tz_color = tracy.zone(@src(), "paneRecolor"); + const probe_color = pardes.FrameStats.nowNs(); + defer p.frames.zone(.body_color, probe_color); // File layout is independent of whether its producer requests colors. if (pane.file) |*f| { panes.File.drawGutter(p, s, pane, r, tx, tw, body_h, active); diff --git a/src/draw.zig b/src/draw.zig index b4fc3a2c..3ba5801f 100644 --- a/src/draw.zig +++ b/src/draw.zig @@ -756,7 +756,7 @@ pub fn render(p: *Pardes, arena: std.mem.Allocator) !*Surface { // wide enough for the longest theme name compiled in, which is the // only row here whose width is not ours to choose const ow: u16 = 38; - const oh: u16 = 12; + const oh: u16 = 13; const ox: u16 = if (s.cols > ow) s.cols - ow else 0; if (ox + ow <= s.cols and p.topBarHeight() + oh <= s.rows) { s.addRegion(.{ .kind = .debug, .rect = .{ .x = ox, .y = p.topBarHeight(), .w = ow, .h = oh } }); diff --git a/src/gui/gui.zig b/src/gui/gui.zig index 125f53cf..0a3f3340 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -1475,8 +1475,10 @@ const Gui = struct { /// owns the wheel (terminal.ownsWheel). program_wheel: f32 = 0, glide: Glide = .{}, - /// PARDES_FRAMES_DEMO: the scroll timedemo's next step and when it - /// started (FrameStats.demo); null when none runs. + /// PARDES_FRAMES_DEMO: the timedemo (FrameStats.named), its next step + /// and when it started; null when none runs. + demo: []const pardes.FrameStats.Step = &.{}, + demo_name: []const u8 = "", demo_next: ?usize = null, demo_start_ns: u64 = 0, /// G9 and G11: the selection's and the hovered word's glow blocks as @@ -2160,7 +2162,9 @@ fn runNative(init: std.process.Init, opts_in: pardes.Options, attach: ?[]const u .capture_series = test_mode and env.get("PARDES_TEST_SERIES") != null, .capture_dir = capture_dir orelse "", .soft_present = soft_present, - .demo_next = if (env.get("PARDES_FRAMES_DEMO") != null) 0 else null, + .demo = if (env.get("PARDES_FRAMES_DEMO")) |name| pardes.FrameStats.named(name) orelse &.{} else &.{}, + .demo_name = env.get("PARDES_FRAMES_DEMO") orelse "", + .demo_next = if (env.get("PARDES_FRAMES_DEMO")) |name| (if (pardes.FrameStats.named(name) != null) @as(?usize, 0) else null) else null, .surface_opacity = surface_opacity, .soft_renderer = soft_renderer, }; @@ -2323,6 +2327,8 @@ fn localSession( defer shell.shaders.deinit(gpa, inotify_fd, &watches); // A virtual clock never blinks: goldens and feel series hold still. g.no_blink = shell.test_clock != null; + frame_probe = &core.frames; + frame_capture_dir = if (std.c.getenv("PARDES_FRAMES_CAPTURE")) |dir| std.mem.span(dir) else null; const host = shell.host(); core.host = host; @@ -3128,6 +3134,13 @@ const Input = struct { }; fn dispatch(g: *Gui, in: *Input, sev: *const c.SDL_Event) void { + // An input's time, for its latency to the frame that answers it + // (FrameStats): SDL's stamp, moved onto the monotonic clock. + switch (sev.type) { + c.SDL_EVENT_KEY_DOWN, c.SDL_EVENT_TEXT_INPUT, c.SDL_EVENT_MOUSE_BUTTON_DOWN, c.SDL_EVENT_MOUSE_WHEEL => if (frame_probe) |probe| + probe.input(monoNs() -| (c.SDL_GetTicksNS() -| sev.common.timestamp)), + else => {}, + } // What the post chain sees of the pointer and the focus. switch (sev.type) { c.SDL_EVENT_MOUSE_MOTION => { @@ -4202,11 +4215,36 @@ fn pollFrame(ctx: ?*anyopaque) void { /// Frames drawn for a blink alone, with no core render (tests, tracing). var blink_redraws: u64 = 0; +/// PARDES_FRAMES_CAPTURE's directory, and how many frames it has. +var frame_capture_dir: ?[]const u8 = null; +var frame_capture_count: u32 = 0; + +fn captureSeries(dir: []const u8, pixels: []const u8, w: u32, h: u32) void { + var path_buf: [4096]u8 = undefined; + const path = std.fmt.bufPrintSentinel(&path_buf, "{s}/{d:0>6}.bgra", .{ dir, frame_capture_count }, 0) catch return; + const fd = libc.open(path, .{ .ACCMODE = .WRONLY, .CREAT = true, .TRUNC = true }, @as(libc.mode_t, 0o644)); + if (fd < 0) return; + _ = libc.write(fd, pixels.ptr, pixels.len); + _ = libc.close(fd); + const times = std.fmt.bufPrintSentinel(&path_buf, "{s}/times.txt", .{dir}, 0) catch return; + const tf = libc.open(times, .{ .ACCMODE = .WRONLY, .CREAT = true, .APPEND = true }, @as(libc.mode_t, 0o644)); + if (tf < 0) return; + var line_buf: [96]u8 = undefined; + const line = std.fmt.bufPrint(&line_buf, "{d} {d} {d} {d}\n", .{ frame_capture_count, monoNs(), w, h }) catch ""; + _ = libc.write(tf, line.ptr, line.len); + _ = libc.close(tf); + frame_capture_count += 1; +} + +/// The core's frame probe (FrameStats), for the latency of the inputs the +/// shell reads and the zones its own work goes in; null in tests. +var frame_probe: ?*pardes.FrameStats = null; + /// How long until the timedemo's next step, ms; null when none runs. fn demoDue(g: *const Gui) ?c_int { const next = g.demo_next orelse return null; - if (g.demo_start_ns == 0 or next >= pardes.FrameStats.demo.len) return 0; - const at = g.demo_start_ns + @as(u64, pardes.FrameStats.demo[next].at_ms) * std.time.ns_per_ms; + if (g.demo_start_ns == 0 or next >= g.demo.len) return 0; + const at = g.demo_start_ns + @as(u64, g.demo[next].at_ms) * std.time.ns_per_ms; return @intCast(@min(std.math.maxInt(c_int), (at -| monoNs()) / std.time.ns_per_ms)); } @@ -4215,16 +4253,56 @@ fn demoDue(g: *const Gui) ?c_int { /// focused pane's body; its clock starts at the first loop. Its end /// ends the session (PARDES_FRAMES has the timings then). fn runDemo(g: *Gui, in: *Input, core: *pardes.Pardes) void { - const steps = pardes.FrameStats.demo; + const steps = g.demo; var next = g.demo_next orelse return; const now = monoNs(); - if (g.demo_start_ns == 0) g.demo_start_ns = now; + if (g.demo_start_ns == 0) { + g.demo_start_ns = now; + core.frames.demo_ns = now; + core.frames.demo_name = g.demo_name; + } defer g.demo_next = next; while (next < steps.len and g.demo_start_ns + @as(u64, steps[next].at_ms) * std.time.ns_per_ms <= now) : (next += 1) { const r = core.rects[core.active]; switch (steps[next].act) { - .wheel => |notches| wheelAt(g, in, .{ .y = @floatFromInt(-notches), .integer_y = -notches }, .{ .col = r.x + r.w / 2, .row = r.y + r.h / 2 }), - .key => |key| in.post(.{ .key = key }), + .wheel => |notches| { + core.frames.input(now); + wheelAt(g, in, .{ .y = @floatFromInt(-notches), .integer_y = -notches }, .{ .col = r.x + r.w / 2, .row = r.y + r.h / 2 }); + }, + .key => |key| { + core.frames.input(now); + in.post(.{ .key = key }); + }, + .phase => {}, + .click => |click| { + core.frames.input(now); + const pane = core.panes[core.active] orelse continue; + const top = core.bodyTop(pane, r); + const row: u16 = top + @as(u16, @intFromFloat(click.at * @as(f32, @floatFromInt(r.h -| pane.tag_rows -| 1)))); + const col: u16 = if (click.rail) r.x else r.x + config.GUTTER + 4; + for ([_]pardes.Mouse.Kind{ .press, .release }) |kind| in.post(.{ .mouse = .{ .button = click.button, .kind = kind, .col = col, .row = row } }); + }, + .rail_wheel => |notches| { + core.frames.input(now); + wheelAt(g, in, .{ .y = @floatFromInt(-notches), .integer_y = -notches }, .{ .col = r.x, .row = r.y + r.h / 2 }); + }, + .paste => |text| { + core.frames.input(now); + in.post(.{ .paste = text }); + }, + .exec => |line| { + core.frames.input(now); + var newest: u32 = 0; + for (core.panes) |slot| if (slot) |pane| { + newest = @max(newest, pane.serial); + }; + _ = pardes.exec.executeFrom(core, core.active, line, false); + // What it opened is what the next steps scroll. + for (core.panes, 0..) |slot, id| if (slot) |pane| if (pane.serial > newest) { + core.active = id; + core.needs_frame = true; + }; + }, .end => in.close(), } } @@ -5922,6 +6000,7 @@ fn renderFrame( // The frame's CPU side: groups, decor, cover and every instance, up to // their upload. const build_zone = tracy.zone(@src(), "gui frame build"); + const probe_build = monoNs(); var building = true; defer if (building) build_zone.end(); const layout = fixedCellLayout(g); @@ -6265,6 +6344,7 @@ fn renderFrame( build_zone.end(); building = false; + if (frame_probe) |probe| probe.zone(.gui_build, probe_build); const rp = c.SDL_BeginGPURenderPass(cmd, &color_target, 1, null); pushLayerOpacity(cmd, rp, background_opacity); const whole = c.SDL_Rect{ .x = 0, .y = 0, .w = @intCast(sw), .h = @intCast(sh) }; @@ -7572,6 +7652,11 @@ fn cellSlot(g: *Gui, line: []const pardes.Cell, at: usize, cursor: ?usize, role: // A new word (a hash collision takes the map entry over). Runs of one // face are shaped in that face; fallback faces shape like the primary. + const probe_shape = monoNs(); + defer if (frame_probe) |probe| { + probe.zone(.gui_shape, probe_shape); + probe.shaped +|= 1; + }; const n = text.len; words.slots.ensureUnusedCapacity(gpa, n) catch { words.text.shrinkRetainingCapacity(text_start); @@ -7769,6 +7854,11 @@ fn cachedGlyph(g: *Gui, key: GlyphKey) ?Slot { g.pen_y += g.cell_h; } if (g.pen_y + g.cell_h > atlas_h) return null; // atlas full: fall back + const probe_raster = monoNs(); + defer if (frame_probe) |probe| { + probe.zone(.gui_raster, probe_raster); + probe.rastered +|= 1; + }; const s = Slot{ .u = g.pen_x, .v = g.pen_y }; const out = g.atlas_stage.ptr + @as(usize, s.v) * atlas_w + s.u; const face = if (key.face == 0) g.font else g.fallbacks[key.face - 1].?.face; @@ -8367,21 +8457,31 @@ fn softPresentFrame(g: *Gui, cmd: *c.SDL_GPUCommandBuffer, target: *c.SDL_GPUTex const fence = c.SDL_SubmitGPUCommandBufferAndAcquireFence(cmd) orelse return error.GpuSubmit; defer c.SDL_ReleaseGPUFence(g.device, fence); var fences = [_]*c.SDL_GPUFence{fence}; + const probe_wait = monoNs(); if (!c.SDL_WaitForGPUFences(g.device, true, &fences, 1)) return error.GpuSubmit; + if (frame_probe) |probe| probe.zone(.gui_wait, probe_wait); const mapped = c.SDL_MapGPUTransferBuffer(g.device, xfer, false) orelse return error.GpuMap; defer c.SDL_UnmapGPUTransferBuffer(g.device, xfer); const texture = try ensureSoftTexture(g, sw, sh); const renderer = g.soft_renderer orelse return error.GpuCreate; + const probe_upload = monoNs(); + defer if (frame_probe) |probe| probe.zone(.gui_upload, probe_upload); if (!c.SDL_UpdateTexture(texture, null, mapped, @intCast(sw * bpp))) return error.GpuMap; + // PARDES_FRAMES_CAPTURE: every presented frame's pixels as they went + // out, and when, for a recording at the frames' own times (the + // samples of docs/next-anim.md). One write a frame; nothing else. + if (frame_capture_dir) |dir| captureSeries(dir, @as([*]const u8, @ptrCast(mapped))[0..size], sw, sh); // The cell shader already produced premultiplied backgrounds with opaque // foreground ink. Copy that alpha unchanged; never modulate the full frame. _ = c.SDL_SetRenderDrawBlendMode(renderer, c.SDL_BLENDMODE_NONE); _ = c.SDL_SetRenderDrawColor(renderer, 0, 0, 0, if (g.surface_opacity) 0 else 255); _ = c.SDL_RenderClear(renderer); _ = c.SDL_RenderTexture(renderer, texture, null, null); + const probe_flip = monoNs(); _ = c.SDL_RenderPresent(renderer); + if (frame_probe) |probe| probe.zone(.gui_flip, probe_flip); } fn backgroundOpacity(percent: u8) f32 { diff --git a/src/pardes.zig b/src/pardes.zig index 22b43a51..df6d347f 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -5968,13 +5968,14 @@ pub const Pardes = struct { // rendering and nothing else. // A shell with a clock draws what `advance` stepped; one without // steps its own `.tick`s and draws while anything animates. - if (!p.needs_frame and !p.continuous() and (v.now != null or !p.animationActive())) return; + if (!p.needs_frame and !p.continuous() and (v.now != null or !p.animationActive())) return p.frames.idle(); // No one to see it: the frame stays owed (needs_frame), not drawn. // A pty streaming into an unwatched session spent most of its time // drawing what nobody looked at. if (v.watched) |f| if (!f(h.ctx)) return; _ = p.frame_arena.reset(.{ .retain_with_limit = memory.limits.frame_kept }); p.frames.mark_update = FrameStats.nowNs(); + p.frames.view = if (p.panes[p.active]) |pane| pane.scroll() else 0; const surface = try p.render(p.frame_arena.allocator()); p.frames.mark_render = FrameStats.nowNs(); if (v.present) |f| f(h.ctx, surface); diff --git a/src/tty/tty.zig b/src/tty/tty.zig index 572a6f2a..efe8c752 100644 --- a/src/tty/tty.zig +++ b/src/tty/tty.zig @@ -1619,6 +1619,13 @@ const Shell = struct { const core = s.core; const tz_event = tracy.zone(@src(), "event"); defer tz_event.end(); + // A key's or a click's time, for its latency to the frame that + // answers it (FrameStats): when the loop takes it in. + switch (event) { + .key_press => core.frames.input(pardes.FrameStats.nowNs()), + .mouse => |m| if (m.type == .press) core.frames.input(pardes.FrameStats.nowNs()), + else => {}, + } switch (event) { .nop => {}, .wake => if (s.test_clock) |*virtual| { @@ -1877,6 +1884,9 @@ const Shell = struct { const tz_render = tracy.zone(@src(), "vx.render"); const before = s.tty.tty_writer.pos + s.tty.writer().end; vx.render(s.tty.writer()) catch {}; + // The frame's cost to the terminal (FrameStats.Frame.bytes): what + // vaxis's diff of the cells wrote. + s.core.frames.bytes +|= @intCast(@min(std.math.maxInt(u32), s.tty.tty_writer.pos + s.tty.writer().end -| before)); if (s.tty.tty_writer.pos + s.tty.writer().end != before) FrameAck.ask(s.tty.writer(), @intCast(std.Io.Clock.awake.now(s.io).nanoseconds)); tz_render.end(); } -- cgit v1.3