From 82fca5c9f29124f37f6dcd08e73e6999ce44a69d Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Mon, 16 Nov 2026 20:31:59 -0300 Subject: Add display-paced frame timing and presentation feedback in Zig --- src/host/Console.zig | 41 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) (limited to 'src/host/Console.zig') diff --git a/src/host/Console.zig b/src/host/Console.zig index 40845f7..0c44ea2 100644 --- a/src/host/Console.zig +++ b/src/host/Console.zig @@ -14,8 +14,9 @@ pub const Key = enum { left, right, home, end, backspace, delete, clear, tab, su const Line = struct { bytes: [118]u8 = @splat(0), len: usize = 0 }; const host_commands = [_][]const u8{ - "help", "overlay memory", "overlay sound", "get", "set", "reset", "save", "load", "snapshot", "recording", - "record", "stop", "play", "pause", "step", "seek start", "seek end", "diff", "quit", "exit", + "help", "overlay memory", "overlay sound", "get", "set", "reset", "save", "load", "snapshot", "recording", + "record", "stop", "play", "pause", "step", "seek start", "seek end", "diff", "quit", "exit", + "overlay timing", "timing on", "timing off", "timing frame", "timing work", "timing reset", "timing stats", }; visible: bool = false, @@ -135,14 +136,29 @@ pub fn execute(self: *Console, host: *Host, line: []const u8, now: u64) ![]const if (eq(command, "help")) { self.log("save|load [1..7] snapshot N | record|play [0..15] recording N | stop | pause | step N (frames) | seek start|end|"); self.log("diff [N] | reset | get | set | overlay memory|sound | quit"); + self.log("timing [on|off|frame|work|reset|stats] | overlay timing"); if (host.forward(self, line, now) == .unknown) self.log("Backtick hides the console. Tab completes."); return &.{}; } if (eq(command, "overlay")) { const name = arg orelse return error.ExpectedOverlay; - const flag = if (eq(name, "memory")) &self.overlay_memory else if (eq(name, "sound")) &self.overlay_sound else return error.UnknownOverlay; + if (rest.len != 0) return error.UnexpectedArgument; + const flag = if (eq(name, "memory")) &self.overlay_memory else if (eq(name, "sound")) &self.overlay_sound else if (eq(name, "timing")) &host.timing.visible else return error.UnknownOverlay; flag.* = !flag.*; + host.timing.dirty = true; self.print("{s} overlay: {s}", .{ name, if (flag.*) "on" else "off" }); + } else if (eq(command, "timing")) { + if (rest.len != 0) return error.UnexpectedArgument; + if (eq(arg orelse "", "stats")) { + self.print("Timing: last {d}/240 frames; frame includes present, work excludes present (wall clock).", .{host.timing.count}); + inline for (.{ .frame, .work }) |metric| { + const s = host.timing.stats(metric); + self.print("{s}: avg {d:.3} ms | p90 {d:.3} ms | p99 {d:.3} ms | max {d:.3} ms", .{ @tagName(metric), s.mean, s.p90, s.p99, s.max }); + } + } else { + try host.timing.command(arg); + self.print("Timing overlay: {s}, graph: {t}, samples: {d}", .{ if (host.timing.visible) "on" else "off", host.timing.metric, host.timing.count }); + } } else if (eq(command, "get")) { const path = arg orelse return error.ExpectedField; var buf: [160]u8 = undefined; @@ -276,3 +292,22 @@ test "line editing, completion and submission" { try std.testing.expectEqualStrings("", c.text()); try std.testing.expect(c.edit(.submit, &.{}) == null); } + +test "timing console controls and validation" { + var host: Host = undefined; + host.timing = .{}; + var console: Console = .{}; + _ = try console.execute(&host, "timing work", 0); + try std.testing.expect(host.timing.visible and host.timing.metric == .work); + _ = try console.execute(&host, "overlay timing", 0); + try std.testing.expect(!host.timing.visible); + host.timing.add(.{ .frame = 17000000, .work = 500000 }); + _ = try console.execute(&host, "timing stats", 0); + const last = console.lines[console.lines.len - 1]; + try std.testing.expect(std.mem.indexOf(u8, last.bytes[0..last.len], "p99 0.500 ms") != null); + try std.testing.expectError(error.UnexpectedArgument, console.execute(&host, "timing on extra", 0)); + try std.testing.expect(!host.timing.visible); + try std.testing.expectError(error.ExpectedTimingOnOffFrameWorkResetStats, console.execute(&host, "timing bogus", 0)); + _ = try console.execute(&host, "timing reset", 0); + try std.testing.expectEqual(@as(usize, 0), host.timing.count); +} -- cgit v1.3