Skip to content

RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process

Cut per-frame clock reads: cache frame time, gate stage timers

oxydizer6179c1879b10
diff --git a/TESTING.md b/TESTING.md
index a043ea9..03d183b 100644
--- a/TESTING.md
+++ b/TESTING.md
@@ -407,8 +407,12 @@ scale, input rate and compiler backend identical across comparisons.
`glass_reblurs` counts the blurs it re-took). CPU and GPU overlap; don't
add them or invert them into FPS.
- The GPU timer starts on the first stats request and stops after five idle
- minutes; it costs CPU per frame. For CPU A/B runs, make no stats requests and
- read process CPU time (per-thread schedstat) instead.
+ minutes; it costs CPU per frame (about 0.9 % of a core with a 60 Hz client).
+ The per-stage wall timers (`taskbar_cpu`, `chrome_cpu`, `panels_cpu`,
+ `projection_cpu`, `anim_cpu`) run in the same window: they stay empty until a
+ stats request or `reset_performance_stats`, so reset first, then measure. For
+ CPU A/B runs, make no stats requests and read process CPU time (per-thread
+ schedstat) or `perf stat -e instructions:u` instead.
## Still manual
diff --git a/src/Output.zig b/src/Output.zig
index f2e0fce..23e4bec 100644
--- a/src/Output.zig
+++ b/src/Output.zig
@@ -1250,9 +1250,13 @@ fn handleFrame(listener: *wl.Listener(*wlr.Output), _: *wlr.Output) void {
fn runFrame(output: *Output) void {
const frame_started = stats.nowNs();
- anim.beginFrame(output.animation_budget.coarse, stats.nowNs);
+ // Stage timing costs a clock read per boundary (and two per animation
+ // sample); only pay it while someone is reading the stats.
+ const observing = output.server.observingStats(frame_started);
+ anim.beginFrame(output.animation_budget.coarse, if (observing) stats.nowNs else null, @intCast(frame_started / std.time.ns_per_ms));
defer {
- stats.global_stats.anim_cpu.record(anim.endFrame());
+ const sampling_ns = anim.endFrame();
+ if (observing) stats.global_stats.anim_cpu.record(sampling_ns);
const elapsed = stats.nowNs() -% frame_started;
stats.global_stats.cpu_frame.record(elapsed);
output.animation_budget.record(elapsed, output.wlr_output.refresh);
@@ -1269,14 +1273,14 @@ fn runFrame(output: *Output) void {
const switcher_animating = output.server.switcher.tick(output, now_ms);
const osd_animating = output.osd.tick(output, now_ms);
const dock_animating = output.dock.tick(output, now_ms);
- const taskbar_started = stats.nowNs();
+ const taskbar_started = if (observing) stats.nowNs() else 0;
var bar_animating = false;
if (output.taskbar) |bar| bar_animating = bar.tick(now_ms);
- stats.global_stats.taskbar_cpu.record(stats.nowNs() -% taskbar_started);
- const chrome_started = stats.nowNs();
+ if (observing) stats.global_stats.taskbar_cpu.record(stats.nowNs() -% taskbar_started);
+ const chrome_started = if (observing) stats.nowNs() else 0;
const chrome_animating = output.server.world.tickHover(now_ms);
- stats.global_stats.chrome_cpu.record(stats.nowNs() -% chrome_started);
- const panels_started = stats.nowNs();
+ if (observing) stats.global_stats.chrome_cpu.record(stats.nowNs() -% chrome_started);
+ const panels_started = if (observing) stats.nowNs() else 0;
var cc_animating = false;
if (output.server.input.open_control_center) |cc| {
@@ -1316,7 +1320,7 @@ fn runFrame(output: *Output) void {
const desktop_animating = if (output.server.desktop) |desktop| desktop.frame(output) else false;
- stats.global_stats.panels_cpu.record(stats.nowNs() -% panels_started);
+ if (observing) stats.global_stats.panels_cpu.record(stats.nowNs() -% panels_started);
if (anim.observeLongestMs() > 0) {
const longest: u64 = @intCast(anim.observeLongestMs());
if (longest > stats.global_stats.anim_longest_ms) stats.global_stats.anim_longest_ms = longest;
@@ -1328,10 +1332,10 @@ fn runFrame(output: *Output) void {
return;
};
const reveal_animating = output.server.reveal.tick(output, now_ms);
- const projection_started = stats.nowNs();
+ const projection_started = if (observing) stats.nowNs() else 0;
output.server.world.refreshBounds();
output.server.world.syncPresentation();
- stats.global_stats.projection_cpu.record(stats.nowNs() -% projection_started);
+ if (observing) stats.global_stats.projection_cpu.record(stats.nowNs() -% projection_started);
output.server.world.flushCameraEvent();
const mini_map_animating = output.server.world.mini_map.tick(output, now_ms);
// GPU timestamps enclose the scene submission, glass included (the output
diff --git a/src/Server.zig b/src/Server.zig
index 4ba6080..a4e5a91 100644
--- a/src/Server.zig
+++ b/src/Server.zig
@@ -1558,13 +1558,20 @@ pub fn ensureGpuTimer(server: *Server) void {
server.gpu_timer = @import("gpu_timing.zig").Timer.create(server.renderer);
}
+/// Whether a stats request came in recently enough that per-stage frame timing
+/// is worth its clock reads (the same window the GPU timer uses).
+pub fn observingStats(server: *const Server, now_ns: u64) bool {
+ return server.gpu_timer_tried and now_ns -% server.gpu_timer_used_ns < gpu_timer_idle_ns;
+}
+
+const gpu_timer_idle_ns: u64 = 5 * 60 * std.time.ns_per_s;
+
/// One `perf` query must not leave the timer's per-frame cost (measured
/// ~0.17 ms at 60 Hz on Iris Xe) running for the rest of the session. After
/// five minutes without a stats request it stops; the next request restarts it.
pub fn expireGpuTimer(server: *Server) void {
const timer = server.gpu_timer orelse return;
- const idle_ns: u64 = 5 * 60 * std.time.ns_per_s;
- if (@import("ipc/stats.zig").nowNs() -% server.gpu_timer_used_ns < idle_ns) return;
+ if (@import("ipc/stats.zig").nowNs() -% server.gpu_timer_used_ns < gpu_timer_idle_ns) return;
timer.destroy();
server.gpu_timer = null;
server.gpu_timer_tried = false;
diff --git a/src/ui/anim.zig b/src/ui/anim.zig
index 0cbee19..71606bb 100644
--- a/src/ui/anim.zig
+++ b/src/ui/anim.zig
@@ -230,17 +230,23 @@ pub const FrameBudget = struct {
var raster_step: f32 = 1;
var sampling_clock: ?*const fn () u64 = null;
var sampling_ns: u64 = 0;
+var frame_ms: ?i64 = null;
/// Scope these settings to an output frame; event/IPC sampling is unmeasured.
-pub fn beginFrame(coarse: bool, clock: *const fn () u64) void {
+/// `clock` is null unless somebody is reading the sampling cost (two clock
+/// reads per sample otherwise). `now_ms` is the frame's start: `nowMs` returns
+/// it until `endFrame`, so one frame reads the clock once, not once per tick.
+pub fn beginFrame(coarse: bool, clock: ?*const fn () u64, now_ms: i64) void {
raster_step = if (coarse) 2 else 1;
sampling_clock = clock;
sampling_ns = 0;
+ frame_ms = now_ms;
}
pub fn endFrame() u64 {
sampling_clock = null;
raster_step = 1;
+ frame_ms = null;
return sampling_ns;
}
@@ -282,7 +288,7 @@ test "raster degradation preserves sampling and settlement, and publishes endpoi
};
var a = Anim.initDuration(0, 1, 0, 100, .out_cubic);
const before = a.value(50);
- beginFrame(true, Clock.read);
+ beginFrame(true, Clock.read, 0);
defer _ = endFrame();
try std.testing.expectEqual(@as(f32, 2), rasterPixelQuantum(1));
try std.testing.expectApproxEqAbs(@as(f32, 2.0 / 1.5), rasterPixelQuantum(1.5), 0.0001);
@@ -306,7 +312,7 @@ test "sampling timer excludes work between samples and outside frames" {
};
Clock.ns = 0;
const a = Anim.initDuration(0, 1, 0, 100, .out_cubic);
- beginFrame(false, Clock.read);
+ beginFrame(false, Clock.read, 0);
_ = a.value(20);
Clock.ns += 1_000_000; // Raster work between samples.
_ = a.velocity(20);
@@ -737,6 +743,7 @@ pub fn clockOverridden() bool {
pub fn nowMs() i64 {
if (override_ms) |ms| return ms;
+ if (frame_ms) |ms| return ms;
var ts: std.posix.timespec = undefined;
switch (std.posix.errno(std.posix.system.clock_gettime(std.posix.CLOCK.MONOTONIC, &ts))) {
.SUCCESS => {},
@@ -838,6 +845,7 @@ pub fn captureIfLive(
fn resetGlobals() void {
override_ms = null;
+ frame_ms = null;
speed_g = 1.0;
enabled_g = true;
reduced_motion_g = false;