From 9f28ae51312f12a58bee188f96042a032e0b11e1 Mon Sep 17 00:00:00 2001 From: stoltembergg Date: Wed, 25 Nov 2026 07:07:25 +0000 Subject: [PATCH] gpu: log frame-time median and p95 (re-port #24) Durations between presented frames are sampled and logged each 5-second performance line as p50/p95 milliseconds: the numbers the 30 fps target is measured by. Re-ported onto the fxceless base; the percentiles join the richer fxceless frame statistics line. Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- crates/victory-gpu/src/host.rs | 86 +++++++++++++++++++++++++++++++++- 1 file changed, 84 insertions(+), 2 deletions(-) diff --git a/crates/victory-gpu/src/host.rs b/crates/victory-gpu/src/host.rs index 1eafeba4..2549f705 100644 --- a/crates/victory-gpu/src/host.rs +++ b/crates/victory-gpu/src/host.rs @@ -5742,6 +5742,7 @@ struct Presenter { struct Meter { since: std::time::Instant, frames: u32, + frame_times: FrameTimes, #[cfg(feature = "diagnostics")] copy_stats_frames: u64, drawn: u64, @@ -5750,12 +5751,45 @@ struct Meter { caption: Option>, } +/// Durations between presented frames, kept until the next performance log. +#[derive(Default)] +struct FrameTimes { + previous: Option, + samples: Vec, +} + +impl FrameTimes { + fn record(&mut self, now: std::time::Instant) { + if let Some(previous) = self.previous.replace(now) { + self.samples.push(now.saturating_duration_since(previous)); + } + } + + /// Returns nearest-rank p50 and p95, then clears samples but retains the + /// previous presentation time so the next interval has no gap. + fn take_percentiles(&mut self) -> Option<(std::time::Duration, std::time::Duration)> { + if self.samples.is_empty() { + return None; + } + self.samples.sort_unstable(); + let len = self.samples.len(); + let percentile = |percent: usize| { + let index = (len * percent).div_ceil(100).saturating_sub(1); + self.samples[index.min(len - 1)] + }; + let result = (percentile(50), percentile(95)); + self.samples.clear(); + Some(result) + } +} + impl Meter { fn new() -> Self { let now = std::time::Instant::now(); Meter { since: now, frames: 0, + frame_times: FrameTimes::default(), #[cfg(feature = "diagnostics")] copy_stats_frames: 0, drawn: crate::draw::drawn(), @@ -5770,12 +5804,14 @@ impl Meter { tracing::debug!(target: "victory_gpu::frame", token = crate::frame::current(), "present"); #[cfg(feature = "diagnostics")] crate::copystat::presented(); + let now = std::time::Instant::now(); + self.frame_times.record(now); self.frames += 1; #[cfg(feature = "diagnostics")] { self.copy_stats_frames += 1; } - let elapsed = self.since.elapsed(); + let elapsed = now.saturating_duration_since(self.since); if elapsed < std::time::Duration::from_secs(1) { return; } @@ -5862,6 +5898,17 @@ impl Meter { let drops = crate::pipeline::take_drops(); if self.logged.elapsed() >= std::time::Duration::from_secs(5) { self.logged = std::time::Instant::now(); + let frame_timing = self + .frame_times + .take_percentiles() + .map(|(p50, p95)| { + format!( + "frame time p50/p95 {:.1}/{:.1} ms", + p50.as_secs_f64() * 1000.0, + p95.as_secs_f64() * 1000.0 + ) + }) + .unwrap_or_else(|| "frame time p50/p95 n/a".into()); tracing::info!( "placed texture views: {} writes of another view of the texture a slot showed (it kept the old view until the new was made)", crate::descriptor::host::KEPT_VIEWS.load(Ordering::Relaxed) @@ -5919,7 +5966,7 @@ impl Meter { {copy_mb:.1} MB, textures {texture_ms:.1} ms {texture_mb:.1} MB; {gpu:.1} on the \ GPU; {back_ms:.1} copying back {back_mb:.1} MB), {draws:.0} draws/frame, \ dropped {dropped:.0} without a host pipeline ({making} being made) and \ - {indirect:.0} indirect; heap buffers {picked:.1} passes {picked_ms:.1} ms; texture slots written so far: {placed} placed, {objects} of objects; \ + {indirect:.0} indirect; {frame_timing}; heap buffers {picked:.1} passes {picked_ms:.1} ms; texture slots written so far: {placed} placed, {objects} of objects; \ textures picked by index: {picked_textures} uploaded so far ({picked_mb} MB), \ {picked_waiting} waiting, {picked_away} passed over unmapped; depth textures with unordered access: {du0} made, bound as depth {du1}x, cleared as depth {du2}x, unordered views refused {du3}x; buffer views left null as the host would refuse them: {refused}", picked_textures = crate::draw::HEAP_TEXTURES.load(Ordering::Relaxed), @@ -5990,6 +6037,41 @@ impl Meter { } } +#[cfg(test)] +mod frame_time_tests { + use super::FrameTimes; + use std::time::{Duration, Instant}; + + #[test] + fn reports_nearest_rank_median_and_p95_and_starts_a_fresh_window() { + let start = Instant::now(); + let mut times = FrameTimes::default(); + for millis in [0, 100, 110, 150, 170, 200] { + times.record(start + Duration::from_millis(millis)); + } + + assert_eq!( + times.take_percentiles(), + Some((Duration::from_millis(30), Duration::from_millis(100))) + ); + assert_eq!(times.take_percentiles(), None); + + // Clearing a window must not lose the previous presentation time. + times.record(start + Duration::from_millis(240)); + assert_eq!( + times.take_percentiles(), + Some((Duration::from_millis(40), Duration::from_millis(40))) + ); + } + + #[test] + fn first_presentation_has_no_interval_sample() { + let mut times = FrameTimes::default(); + times.record(Instant::now()); + assert_eq!(times.take_percentiles(), None); + } +} + impl Host { /// Shows `texture`, which is in `state` (a host state) and stays in it, /// as frame `frame` (saved if captures are on). `None` if nothing was