#[cfg(windows)] use std::sync::{Mutex, OnceLock}; #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub(crate) enum Lane { Unknown, NoEntries, Recording, Fence, Value, Mixed, Notified, } impl Lane { pub(crate) fn heads(mut reasons: impl Iterator) -> Self { let Some(first) = reasons.next() else { return Self::Unknown; }; if reasons.all(|r| r == first) { first } else { Self::Mixed } } } #[derive(Clone, Copy)] pub(crate) enum Change { Empty(bool), Work(bool), Park(Lane), Notify, Resume, } struct Clock { last: u64, empty: bool, working: bool, parked: bool, lane: Lane, bins: [u64; 8], } impl Clock { const fn new() -> Self { Self { last: 0, empty: false, working: false, parked: false, lane: Lane::Unknown, bins: [0; 8], } } fn advance(&mut self, now: u64) { if self.empty { self.bins[if self.working { 7 } else { self.lane as usize }] += now - self.last; } self.last = now; } fn change(&mut self, now: u64, change: Change) { self.advance(now); match change { Change::Empty(on) => self.empty = on, Change::Work(on) => self.working = on, Change::Park(reason) => { self.parked = true; self.lane = reason; } Change::Notify if self.parked => self.lane = Lane::Notified, Change::Notify => (), Change::Resume => { self.parked = false; self.lane = Lane::Unknown; } } } } #[cfg(windows)] pub(crate) fn enabled() -> bool { static ON: OnceLock = OnceLock::new(); *ON.get_or_init(|| std::env::var_os("EONACAT_XS_QUEUE_GAPS").is_some_and(|v| v == "1")) } #[cfg(windows)] static CLOCK: Mutex = Mutex::new(Clock::new()); #[cfg(windows)] pub(crate) fn change(change: Change) { if enabled() { let mut clock = CLOCK.lock().unwrap_or_else(|e| e.into_inner()); match change { Change::Work(on) if on != clock.working => crate::timeline::point("submit work", u64::from(on)), Change::Park(reason) => crate::timeline::point("lane parked", reason as u64), Change::Notify if clock.parked && clock.lane != Lane::Notified => crate::timeline::point("parked lane notified", 0), Change::Resume if clock.parked => crate::timeline::point("lane resumed", 0), _ => (), } clock.change(super::now_us(), change); } } #[cfg(windows)] pub(super) fn report() { if enabled() { let bins = { let mut clock = CLOCK.lock().unwrap_or_else(|e| e.into_inner()); clock.advance(super::now_us()); std::mem::take(&mut clock.bins) }; let [ unknown, no_entries, recording, fence, value, mixed, notified, submit_work, ] = bins; tracing::info!( unknown, no_entries, recording, fence, value, mixed, notified, submit_work, observed_empty = bins.iter().sum::(), "graphics queue gap window (microseconds)" ); } }