summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-05 00:09:17 -0300
committerGabriel Schneider <[email protected]>2026-11-05 01:21:29 -0300
commit0fd5243709628fd4eddf7c6e5e3a63fdd28531d6 (patch)
tree5ee3bcbe6775266e87d16ebceeb29ec31f59fb71
parentb5f8c6d173ccebefb6f08192873a96bcc9f86cd9 (diff)
downloadpardes-0fd5243709628fd4eddf7c6e5e3a63fdd28531d6.tar.gz
pardes-0fd5243709628fd4eddf7c6e5e3a63fdd28531d6.zip
FrameStats: every core keeps 1024 frames (what the Debug box reads: the last second's, the input latency over all of them, as before), and only a session with PARDES_FRAMES set the 16384 its dump writes, in a ring allocated at its first frame and let go by the dump: Pardes 3,052,152 to 1,700,480 bytes a core, the Debug binary's .data.rel.ro 7,944,088 to 6,141,976 (the core's default value is a constant there), and an idle detached Debug session's RSS 37.3 to 29.4 MB (three runs each, VmRSS and VmHWM alike); a test runs the long ring past the short one's length
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
-rw-r--r--src/FrameStats.zig58
1 files changed, 44 insertions, 14 deletions
diff --git a/src/FrameStats.zig b/src/FrameStats.zig
index fb882d69..680987fb 100644
--- a/src/FrameStats.zig
+++ b/src/FrameStats.zig
@@ -7,7 +7,8 @@
//! fixed ring embedded in `Pardes`, nothing allocated per frame, and cost
//! three reads of the monotonic clock a frame, so the probe is always on:
//! `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.
+//! keeps a longer ring (`long_capacity`, from the first frame) and writes
+//! it and its summary there when the session ends.
//!
//! 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
@@ -27,8 +28,12 @@ const animation = @import("animation.zig");
const FrameStats = @This();
-/// 4.5 minutes at 60 Hz: the longest timedemo fits whole (1.3 MB).
-pub const capacity = 16384;
+/// The frames every core keeps (90 KB), what the Debug box reads: the
+/// last second's, and the input latency over all of them.
+pub const capacity = 1024;
+/// The frames a core keeps with PARDES_FRAMES set: 4.5 minutes at 60 Hz,
+/// so the longest timedemo fits whole (1.4 MB, allocated at the first).
+pub const long_capacity = 16384;
pub const Frame = struct {
/// When the frame's step began, the input that woke it read: the
@@ -75,7 +80,9 @@ pub const Zone = enum(u8) { body_text, body_rows, body_color, gui_build, gui_sha
pub const zone_count = std.enums.values(Zone).len;
ring: [capacity]Frame = @splat(.{}),
-/// Frames recorded since the start; the ring holds the last `capacity`.
+/// PARDES_FRAMES's ring, which takes the frames in place of `ring`.
+long: ?*[long_capacity]Frame = null,
+/// Frames recorded since the start; the ring holds the last of them.
count: u64 = 0,
/// The display's refresh period, as the shell knows it.
period_ns: u64 = animation.frame_ns,
@@ -174,7 +181,11 @@ pub fn record(stats: *FrameStats, end_ns: u64, moving: bool, skipped: bool) void
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;
if (skipped) stats.skipped += 1;
- stats.ring[@intCast(stats.count % capacity)] = frame;
+ if (comptime pardes.hosted) if (stats.count == 0 and std.c.getenv("PARDES_FRAMES") != null) {
+ stats.long = std.heap.page_allocator.create([long_capacity]Frame) catch null;
+ };
+ const ring: []Frame = if (stats.long) |long| long else &stats.ring;
+ ring[@intCast(stats.count % ring.len)] = frame;
stats.count += 1;
stats.last_end_ns = end_ns;
stats.armed = moving;
@@ -182,11 +193,12 @@ pub fn record(stats: *FrameStats, end_ns: u64, moving: bool, skipped: bool) void
/// The last `n` frames, oldest first, as two slices of the ring.
pub fn last(stats: *const FrameStats, n: usize) [2][]const Frame {
- const have: usize = @intCast(@min(stats.count, capacity));
+ const ring: []const Frame = if (stats.long) |long| long else &stats.ring;
+ const have: usize = @intCast(@min(stats.count, ring.len));
const take = @min(n, have);
- const end: usize = @intCast(stats.count % capacity);
- if (take <= end) return .{ stats.ring[end - take .. end], &.{} };
- return .{ stats.ring[capacity - (take - end) ..], stats.ring[0..end] };
+ const end: usize = @intCast(stats.count % ring.len);
+ if (take <= end) return .{ ring[end - take .. end], &.{} };
+ return .{ ring[ring.len - (take - end) ..], ring[0..end] };
}
/// Order statistics of one of a run of frames' times, µs: its median,
@@ -202,7 +214,7 @@ pub const Spread = struct {
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;
+ var values: [long_capacity]u32 = undefined;
var n: usize = 0;
var sum: f64 = 0;
for (frames) |part| for (part) |f| {
@@ -237,7 +249,7 @@ 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);
- const latency = Spread.of(stats.last(capacity / 16), .latency);
+ const latency = Spread.of(stats.last(capacity), .latency);
const ms = struct {
fn of(v: u32) f64 {
return @as(f64, @floatFromInt(v)) / 1000;
@@ -249,14 +261,18 @@ pub fn summary(stats: *const FrameStats, buf: []u8) []const u8 {
}
/// Writes the summary and every frame the ring holds to `path`: a header
-/// of `# ` lines, then one tab-separated line a frame.
-pub fn dump(stats: *const FrameStats, path: [:0]const u8) void {
+/// of `# ` lines, then one tab-separated line a frame. The long ring goes.
+pub fn dump(stats: *FrameStats, path: [:0]const u8) void {
if (comptime !pardes.hosted) return;
+ defer if (stats.long) |long| {
+ std.heap.page_allocator.destroy(long);
+ stats.long = null;
+ };
const fd = std.c.open(path, .{ .ACCMODE = .WRONLY, .CREAT = true, .TRUNC = true, .CLOEXEC = true }, @as(std.c.mode_t, 0o644));
if (fd < 0) return;
defer _ = std.c.close(fd);
var buf: [256]u8 = undefined;
- const all = stats.last(capacity);
+ const all = stats.last(long_capacity);
const write = struct {
fn line(f: c_int, b: []u8, comptime fmt: []const u8, args: anytype) void {
const text = std.fmt.bufPrint(b, fmt, args) catch return;
@@ -462,6 +478,20 @@ test "frame stats: gaps into a motion count as jank, idle gaps do not, and the r
try std.testing.expectEqual(@as(u32, 0), stats.last(1)[0][0].latency_us);
}
+test "frame stats: PARDES_FRAMES's long ring takes every frame, up to long_capacity" {
+ const long = try std.testing.allocator.create([long_capacity]Frame);
+ defer std.testing.allocator.destroy(long);
+ var stats: FrameStats = .{ .long = long };
+ for (0..capacity * 3) |i| {
+ stats.scroll = @floatFromInt(i);
+ stats.record(i + 1, false, false);
+ }
+ const all = stats.last(long_capacity);
+ try std.testing.expectEqual(@as(usize, capacity * 3), all[0].len + all[1].len);
+ try std.testing.expectEqual(@as(f32, 0), all[0][0].scroll);
+ try std.testing.expectEqual(@as(usize, capacity), stats.last(capacity)[0].len);
+}
+
test "every script in tools/demos reads, its steps in time order, and ends the run" {
const io = std.testing.io;
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);