use std::time::Instant; pub struct PerfTelemetry { pub working_set_mb: f32, pub private_mb: f32, pub vram_mb: f32, pub fps: f32, pub frame_ms: f32, last_sample: Instant, last_frame: Instant, frame_accum: u32, fps_timer: Instant, } impl PerfTelemetry { pub fn new() -> Self { let now = Instant::now(); let mut s = Self { working_set_mb: 0.0, private_mb: 0.0, vram_mb: 0.0, fps: 60.0, frame_ms: 16.6, last_sample: now, last_frame: now, frame_accum: 0, fps_timer: now, }; s.sample_memory(); s } pub fn tick(&mut self, vram_bytes: u64) { let now = Instant::now(); let dt = now.duration_since(self.last_frame).as_secs_f32(); self.last_frame = now; if dt > 0.0001 && dt < 1.0 { self.frame_ms = self.frame_ms * 0.85 + (dt * 1000.0) * 0.15; } self.frame_accum += 1; let fps_dt = now.duration_since(self.fps_timer).as_secs_f32(); if fps_dt >= 0.5 { self.fps = self.frame_accum as f32 / fps_dt; self.frame_accum = 0; self.fps_timer = now; } self.vram_mb = vram_bytes as f32 / (1024.0 * 1024.0); if now.duration_since(self.last_sample).as_secs_f32() >= 1.0 { self.sample_memory(); self.last_sample = now; } } #[cfg(windows)] pub fn trim_working_set() { use windows_sys::Win32::System::ProcessStatus::K32EmptyWorkingSet; use windows_sys::Win32::System::Threading::GetCurrentProcess; unsafe { let _ = K32EmptyWorkingSet(GetCurrentProcess()); } } #[cfg(not(windows))] pub fn trim_working_set() {} #[cfg(windows)] pub fn sample_memory(&mut self) { use windows_sys::Win32::System::ProcessStatus::{ K32GetProcessMemoryInfo, PROCESS_MEMORY_COUNTERS_EX, }; use windows_sys::Win32::System::Threading::GetCurrentProcess; unsafe { let mut pmc: PROCESS_MEMORY_COUNTERS_EX = std::mem::zeroed(); pmc.cb = std::mem::size_of::() as u32; let ok = K32GetProcessMemoryInfo( GetCurrentProcess(), &mut pmc as *mut _ as *mut _, pmc.cb, ); if ok != 0 { self.working_set_mb = pmc.WorkingSetSize as f32 / (1024.0 * 1024.0); self.private_mb = pmc.PrivateUsage as f32 / (1024.0 * 1024.0); } } } #[cfg(not(windows))] pub fn sample_memory(&mut self) { self.working_set_mb = 28.0; self.private_mb = 22.0; } }