Files
prometeu-runtime/crates/host/prometeu-host-desktop-winit/src/stats.rs
T
bquarkz a32f834b2b
Intrepid/Prometeu/Runtime/pipeline/head This commit looks good
feat(graphics): migrate native target to 640x360
Centralize the fixed 640x360 RGBA8888/60 Hz contract in the existing HAL.
Preserve 8/16/32 tiles and composition, cover partial edges and mixed-size
camera movement, and adapt firmware, fixtures, hosts and native tests.

Share nearest-neighbor presentation geometry with pointer input. Measure
actual framebuffer publications and successful host presentations separately
from VM ticks, with bounded opt-in diagnostics and release harnesses.

Validated with the complete tooling verify gate, native rendering tests,
GPU readback, headless/process integration, formatting and strict clippy.
The existing synchronous session path is documented; no worker/session
architecture replacement or format compatibility layer is introduced.

Work: WRK-0016 / DEC-0001 / PLN-0001
Commit and push explicitly authorized by the user after completion.
2026-11-20 10:34:45 +00:00

174 lines
6.6 KiB
Rust

use prometeu_firmware::Firmware;
use std::time::{Duration, Instant};
use winit::window::Window;
pub struct HostStats {
pub last_stats_update: Instant,
metrics_path: Option<std::path::PathBuf>,
present_us: Vec<u64>,
present_count: u64,
ticks_total: u64,
first_presentations: u64,
repeated_presentations: u64,
last_presented: Option<(prometeu_hal::RenderOwnership, u64)>,
pub frames_since_last_update: u64,
pub current_fps: f64,
pub audio_load_accum_us: u64,
pub audio_load_samples: u64,
recent_host_cpu_us: [u64; 5],
recent_host_cpu_count: usize,
recent_host_cpu_cursor: usize,
}
impl Default for HostStats {
fn default() -> Self {
Self::new()
}
}
impl HostStats {
pub fn new() -> Self {
Self {
last_stats_update: Instant::now(),
metrics_path: std::env::var_os("PROMETEU_GRAPHICS_METRICS").map(Into::into),
present_us: Vec::new(),
present_count: 0,
ticks_total: 0,
first_presentations: 0,
repeated_presentations: 0,
last_presented: None,
frames_since_last_update: 0,
current_fps: 0.0,
audio_load_accum_us: 0,
audio_load_samples: 0,
recent_host_cpu_us: [0; 5],
recent_host_cpu_count: 0,
recent_host_cpu_cursor: 0,
}
}
/// API latency, including texture upload/submit; not a physical scanout timestamp.
pub fn record_present_time(&mut self, duration: Duration) {
self.present_count += 1;
if self.metrics_path.is_some() {
let value = duration.as_micros() as u64;
if self.present_us.len() < 16384 {
self.present_us.push(value);
} else {
self.present_us[(self.present_count as usize - 1) % 16384] = value;
}
}
}
pub fn record_frame(&mut self) {
self.frames_since_last_update += 1;
self.ticks_total += 1;
}
pub fn record_presented(&mut self, ownership: prometeu_hal::RenderOwnership, frame_id: u64) {
let marker = (ownership, frame_id);
if self.last_presented == Some(marker) {
self.repeated_presentations += 1;
} else {
self.first_presentations += 1;
}
self.last_presented = Some(marker);
}
pub fn record_audio_perf(&mut self, us: u64) {
self.audio_load_accum_us += us;
self.audio_load_samples += 1;
}
pub fn record_host_cpu_time(&mut self, us: u64) {
self.recent_host_cpu_us[self.recent_host_cpu_cursor] = us;
self.recent_host_cpu_cursor =
(self.recent_host_cpu_cursor + 1) % self.recent_host_cpu_us.len();
self.recent_host_cpu_count =
self.recent_host_cpu_count.saturating_add(1).min(self.recent_host_cpu_us.len());
}
pub fn average_host_cpu_ms(&self) -> f64 {
if self.recent_host_cpu_count == 0 {
0.0
} else {
let sum: u64 = self.recent_host_cpu_us.iter().take(self.recent_host_cpu_count).sum();
(sum as f64 / self.recent_host_cpu_count as f64) / 1000.0
}
}
pub fn update(&mut self, now: Instant, window: Option<&Window>, firmware: &mut Firmware) {
let stats_elapsed = now.duration_since(self.last_stats_update);
if stats_elapsed >= Duration::from_secs(1) {
self.current_fps = self.frames_since_last_update as f64 / stats_elapsed.as_secs_f64();
if let Some(window) = window {
// Fixed comparison always against 60Hz, keep even when doing CPU stress tests
let frame_budget_us =
1_000_000.0 / prometeu_hal::graphics_contract::REFRESH_HZ as f64;
let cpu_load_core = (self.average_host_cpu_ms() * 1000.0 / frame_budget_us) * 100.0;
let cpu_load_audio = if self.audio_load_samples > 0 {
(self.audio_load_accum_us as f64 / stats_elapsed.as_micros() as f64) * 100.0
} else {
0.0
};
let title = format!(
"PROMETEU | GFX: {:.1} KB | Host ticks/s: {:.1} | Load: {:.1}% (C) + {:.1}% (A) | Frame: tick {} logical {} done {}",
0,
self.current_fps,
cpu_load_core,
cpu_load_audio,
firmware.os.vm().tick_index(),
firmware.os.vm().logical_frame_index(),
firmware.os.vm().completed_logical_frames(),
);
window.set_title(&title);
}
self.last_stats_update = now;
self.frames_since_last_update = 0;
self.audio_load_accum_us = 0;
self.audio_load_samples = 0;
}
}
}
impl Drop for HostStats {
fn drop(&mut self) {
if let Some(path) = &self.metrics_path {
self.present_us.sort_unstable();
let n = self.present_us.len();
let percentile =
|p: usize| if n == 0 { 0 } else { self.present_us[(n * p).div_ceil(100) - 1] };
let report = serde_json::json!({"schema":1,"host_ticks":self.ticks_total,
"first_presentations":self.first_presentations,"repeated_presentations":self.repeated_presentations,
"present_api_calls":self.present_count,"samples":n,
"present_api_mean_us":if n==0 {0.0} else {self.present_us.iter().sum::<u64>() as f64/n as f64},
"present_api_p95_us":percentile(95),"present_api_p99_us":percentile(99),
"present_api_max_us":self.present_us.last().copied().unwrap_or(0),
"present_api_over_budget":self.present_us.iter().filter(|&&v|v > 1_000_000 / u64::from(prometeu_hal::graphics_contract::REFRESH_HZ)).count(),
"physical_display_hz":null,"limitation":"bounded last 16384 API samples; API upload/submit/present time is not scanout latency"});
if let Err(error) = std::fs::write(path, report.to_string()) {
eprintln!("graphics metrics: {error}");
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use prometeu_hal::{RenderOwnership, app_mode::AppMode};
#[test]
fn host_counts_actual_submissions_by_identity_and_epoch() {
let mut stats = HostStats::new();
let first = RenderOwnership::new(1, AppMode::Game, 2);
stats.record_presented(first, 0);
stats.record_presented(first, 0);
stats.record_presented(RenderOwnership::new(2, AppMode::Game, 2), 0);
assert_eq!(stats.first_presentations, 2);
assert_eq!(stats.repeated_presentations, 1);
}
}