diff --git a/d3d12/crates/leonida-gpu/src/port_descriptors.rs b/d3d12/crates/leonida-gpu/src/port_descriptors.rs index 0b9d96fbf3..966dd83a88 100644 --- a/d3d12/crates/leonida-gpu/src/port_descriptors.rs +++ b/d3d12/crates/leonida-gpu/src/port_descriptors.rs @@ -400,7 +400,8 @@ pub(crate) fn missed(index: u32, handle: u64, view: bool, tabled: bool) { CHECKED[3].fetch_add(1, Relaxed); return; } - let n = CHECKED[1].fetch_add(1, Relaxed); + CHECKED[1].fetch_add(1, Relaxed); + let warn = next_warning(); MISSED_ALL.fetch_add(1, Relaxed); let rip = a.writers.get(((handle - a.start) >> 16) as usize).map_or(0, |w| w.load(Relaxed)); let (base, size) = (IMAGE[0].load(Relaxed), IMAGE[1].load(Relaxed)); @@ -409,7 +410,7 @@ pub(crate) fn missed(index: u32, handle: u64, view: bool, tabled: bool) { let mut by = WRITERS.lock().unwrap_or_else(|e| e.into_inner()); *by.entry(writer.clone()).or_default() += 1; } - if n < 20 || n.is_power_of_two() { + if warn { tracing::warn!( slot = index, handle = format_args!("{handle:#x}"), @@ -496,6 +497,21 @@ static TAKEN: [AtomicU64; 3] = [const { AtomicU64::new(0) }; 3]; pub(crate) static CHECKED: [AtomicU64; 5] = [const { AtomicU64::new(0) }; 5]; /// Since the start: view records the fallback found that no mark told of. static MISSED_ALL: AtomicU64 = AtomicU64::new(0); +/// Warnings said so far. `CHECKED[1]` is swapped to zero by the frame line, so +/// keying the rate limit on it says the first twenty again every period. This +/// one is never reset: the early records and the powers of two are enough to +/// tell whether the fallback is finding unmarked views, without writing a +/// line for each of the tens of thousands a long run finds. +static WARNED: AtomicU64 = AtomicU64::new(0); + +fn warn_worthy(n: u64) -> bool { + n < 20 || n.is_power_of_two() +} + +/// Counts a missed record; whether it is one to say. +fn next_warning() -> bool { + warn_worthy(WARNED.fetch_add(1, Relaxed)) +} /// Since the start: the missed records by the place that last faulted on their block. static WRITERS: std::sync::Mutex> = std::sync::Mutex::new(std::collections::BTreeMap::new()); /// Since the start: slots taken, of them marked by our calls; pages checked. @@ -554,6 +570,30 @@ pub(crate) fn line(frames: f64) { mod tests { use super::*; + /// The warning budget is for the whole run, not for each frame line: a + /// long run still says something at the start and as the count grows, but + /// does not write a line for every missed record. + #[test] + fn the_warning_stays_bounded_over_a_long_run() { + let said = (0..100_000u64).filter(|&n| warn_worthy(n)).count(); + assert!(said <= 40, "said {said} warnings in 100,000 misses"); + assert!(warn_worthy(0)); + assert!(!warn_worthy(21)); + assert!(warn_worthy(1 << 20)); + } + + /// The frame line swaps `CHECKED[1]` to zero every period: that does not + /// say the first twenty again. + #[test] + fn a_new_period_does_not_start_the_warnings_again() { + let mut said = 0; + for _ in 0..100 { + CHECKED[1].store(0, Relaxed); + said += (0..100).filter(|_| next_warning()).count(); + } + assert!(said <= 40, "said {said} warnings in 10,000 misses over 100 periods"); + } + #[test] fn marks_layout_is_what_the_stores_read() { assert_eq!(size_of::(), 32);