diff --git a/d3d12/crates/leonida-gpu/src/ab.rs b/d3d12/crates/leonida-gpu/src/ab.rs index 79516c7b17..44d974d728 100644 --- a/d3d12/crates/leonida-gpu/src/ab.rs +++ b/d3d12/crates/leonida-gpu/src/ab.rs @@ -6,7 +6,7 @@ //! logged at its end (the frames of its first second left out). A switch //! named here does not go by its own variable any more. -use std::sync::atomic::{AtomicU32, Ordering::Relaxed}; +use std::sync::atomic::{AtomicU32, AtomicU64, Ordering::Relaxed}; use std::sync::{Mutex, OnceLock}; struct Asked { @@ -62,10 +62,61 @@ struct Phase { /// and the submitting thread's worked microseconds then (`crate::host::worked`). idle_from: Option, worked_from: Option, + /// The frame lines' counters ([`snapshot`]) when the phase's frames began to count. + counters_from: Option>, } static PHASE: Mutex> = Mutex::new(None); +/// What of the running totals `total` came since the last take of `taken` (the frame lines'). The +/// totals are never reset, so that a phase reads them too ([`snapshot`]); two takers at once never +/// count the same amount twice. +pub(crate) fn take(total: &[AtomicU64; N], taken: &[AtomicU64; N]) -> [u64; N] { + std::array::from_fn(|i| { + let now = total[i].load(Relaxed); + now.saturating_sub(taken[i].fetch_max(now, Relaxed)) + }) +} + +/// The running totals as they are now. +pub(crate) fn load(total: &[AtomicU64; N]) -> [u64; N] { + std::array::from_fn(|i| total[i].load(Relaxed)) +} + +/// The frame lines' counters a phase reports, in the order of [`names`]: asking about title memory +/// (`crate::watch::ask_totals`), the title's faults (`crate::watch::fault_totals`) and the +/// submitting thread's preparing (`crate::host::split_totals`). +fn snapshot() -> Vec { + let mut all = Vec::new(); + all.extend(crate::watch::ask_totals()); + all.extend(crate::watch::fault_totals()); + all.extend(crate::host::split_totals()); + all +} + +/// Their names: those ending in `_ns` are nanoseconds. +fn names() -> Vec<&'static str> { + crate::watch::ASK_NAMES.iter().chain(&crate::watch::FAULT_NAMES).chain(&crate::host::SPLIT_NAMES).copied().collect() +} + +/// A phase's counters line: each counter that moved between the snapshots `from` and `to`, per +/// frame of `frames`, nanoseconds as ms. +fn counters_line(names: &[&str], from: &[u64], to: &[u64], frames: usize) -> String { + let frames = frames.max(1) as f64; + let moved: Vec = names + .iter() + .zip(from.iter().zip(to)) + .filter_map(|(name, (a, b))| { + let n = b.saturating_sub(*a) as f64 / frames; + (n > 0.0).then(|| match name.strip_suffix("_ns") { + Some(stem) => format!("{stem}_ms={:.3}", n / 1e6), + None => format!("{name}={n:.2}"), + }) + }) + .collect(); + if moved.is_empty() { "none moved".to_owned() } else { moved.join(" ") } +} + /// The frames' mean, median and nine tenths. fn summary(frames: &mut [f32]) -> (f32, f32, f32) { if frames.is_empty() { @@ -128,6 +179,7 @@ pub(crate) fn presented(change: impl Fn(&str, bool, bool)) { shown: Vec::new(), idle_from: None, worked_from: None, + counters_from: None, }); if (now - p.began).as_secs_f64() > 1.0 { p.frames.push((now - p.last).as_secs_f32() * 1000.0); @@ -135,6 +187,7 @@ pub(crate) fn presented(change: impl Fn(&str, bool, bool)) { // (From the end of the frame before the first counted one: the counted frames' span.) p.idle_from.get_or_insert(crate::host::CARD_IDLE_TOTAL_US.load(Relaxed)); p.worked_from.get_or_insert_with(crate::host::worked); + p.counters_from.get_or_insert_with(snapshot); } (p.last, p.drawn) = (now, drawn); let number = ((now - p.started).as_secs_f64() / asked.seconds) as u64; @@ -166,6 +219,15 @@ pub(crate) fn presented(change: impl Fn(&str, bool, bool)) { p.shown.len(), (mean - idle).max(0.0) ); + // Leonida: the frame lines' counters per frame over the same frames, so that what a switch + // changes in them (protections changed, refreshes, waits) is told by arm. + if let Some(from) = p.counters_from.take() { + tracing::info!( + "A/B counters phase {} bits {was}: {}", + p.number, + counters_line(&names(), &from, &snapshot(), p.frames.len()) + ); + } for (n, switch) in asked.switches.iter().enumerate() { change(switch, was >> n & 1 != 0, will >> n & 1 != 0); } @@ -194,4 +256,47 @@ mod tests { assert_eq!(jitter(&[60.0, 80.0, 60.0, 80.0]), (20.0, 10.0)); assert_eq!(pick("LEONIDA_NONE_SUCH"), None); } + + #[test] + fn a_take_counts_what_came_since_the_last_one_and_the_totals_stay() { + let total = [AtomicU64::new(0), AtomicU64::new(0)]; + let taken = [AtomicU64::new(0), AtomicU64::new(0)]; + total[0].fetch_add(5, Relaxed); + assert_eq!(take(&total, &taken), [5, 0]); + total[0].fetch_add(3, Relaxed); + total[1].fetch_add(7, Relaxed); + assert_eq!(take(&total, &taken), [3, 7]); + assert_eq!(take(&total, &taken), [0, 0]); + assert_eq!(load(&total), [8, 7]); + } + + #[test] + fn takers_at_once_count_everything_once() { + static TOTAL: [AtomicU64; 1] = [const { AtomicU64::new(0) }; 1]; + static TAKEN: [AtomicU64; 1] = [const { AtomicU64::new(0) }; 1]; + let sum = AtomicU64::new(0); + std::thread::scope(|s| { + for _ in 0..4 { + s.spawn(|| { + for _ in 0..10_000 { + TOTAL[0].fetch_add(1, Relaxed); + sum.fetch_add(take(&TOTAL, &TAKEN)[0], Relaxed); + } + }); + } + }); + sum.fetch_add(take(&TOTAL, &TAKEN)[0], Relaxed); + assert_eq!(sum.load(Relaxed), 40_000); + } + + #[test] + fn a_phase_tells_what_moved_per_frame() { + let listed = ["protect_ns", "blocks_read_only", "kernel_asks"]; + assert_eq!( + counters_line(&listed, &[0, 10, 5], &[4_000_000, 30, 5], 4), + "protect_ms=1.000 blocks_read_only=5.00" + ); + assert_eq!(counters_line(&listed, &[1, 1, 1], &[1, 1, 1], 4), "none moved"); + assert_eq!(names().len(), snapshot().len(), "a name for each counter"); + } } diff --git a/d3d12/crates/leonida-gpu/src/host.rs b/d3d12/crates/leonida-gpu/src/host.rs index b380d0c330..1a40af3a11 100644 --- a/d3d12/crates/leonida-gpu/src/host.rs +++ b/d3d12/crates/leonida-gpu/src/host.rs @@ -10269,7 +10269,7 @@ mod address_hash_tests { } } -/// Since the last frame-rate line, nanoseconds of the submitting thread's +/// Running totals, never reset (`crate::watch`'s `ASKS`), of the nanoseconds of the submitting thread's /// preparing: the write-watch asking for a submission; the title's bytes put /// in under flights' writes; the lists' steps, the refreshes among them; the /// snapshots; what goes to copies in the card's memory; the lists sent; and @@ -10282,6 +10282,35 @@ mod address_hash_tests { /// end, the wait for the heaps' records. #[cfg(feature = "diagnostics")] static SPLIT: [std::sync::atomic::AtomicU64; 16] = [const { std::sync::atomic::AtomicU64::new(0) }; 16]; +/// What of [`SPLIT`] the frame-rate line took. +#[cfg(feature = "diagnostics")] +static SPLIT_TAKEN: [std::sync::atomic::AtomicU64; 16] = [const { std::sync::atomic::AtomicU64::new(0) }; 16]; +/// [`SPLIT`]'s names, for the A/B counters line (`_ns`: nanoseconds; 10 is a count). +#[cfg(feature = "diagnostics")] +pub(crate) const SPLIT_NAMES: [&str; 16] = [ + "ask_watch_ns", + "title_bytes_ns", + "steps_ns", + "snapshots_ns", + "card_copies_ns", + "send_ns", + "refresh_ns", + "copies_fills_ns", + "ask_find_ns", + "refresh_one_at_a_time_ns", + "refreshes_one_at_a_time", + "submit_lock_ns", + "card_steps_ns", + "flights_wait_ns", + "heap_records_ns", + "heap_records_end_ns", +]; + +/// [`SPLIT`] since the start. +#[cfg(feature = "diagnostics")] +pub(crate) fn split_totals() -> [u64; 16] { + crate::ab::load(&SPLIT) +} /// Since the last frame-rate line: flushes of refreshes, their refreshes, /// those known to copy nothing, and those done on several threads. @@ -10313,7 +10342,7 @@ impl Split { /// The split of the preparing, per frame, for the frame-rate line. #[cfg(feature = "diagnostics")] fn split_line(frames: f64) { - let ms: Vec = SPLIT.iter().map(|n| n.swap(0, Ordering::Relaxed) as f64 / 1e6 / frames).collect(); + let ms: Vec = crate::ab::take(&SPLIT, &SPLIT_TAKEN).iter().map(|&n| n as f64 / 1e6 / frames).collect(); let f: Vec = FLUSHES.iter().map(|n| n.swap(0, Ordering::Relaxed) as f64 / frames).collect(); let e: Vec = crate::watch::early_asks().iter().map(|&n| n as f64 / frames).collect(); let a: Vec = crate::watch::asks().iter().map(|&n| n as f64 / frames).collect(); diff --git a/d3d12/crates/leonida-gpu/src/watch.rs b/d3d12/crates/leonida-gpu/src/watch.rs index 02edd8e29d..6947a93f49 100644 --- a/d3d12/crates/leonida-gpu/src/watch.rs +++ b/d3d12/crates/leonida-gpu/src/watch.rs @@ -2196,7 +2196,8 @@ fn published(start: usize, end: usize) -> bool { light() && PUBLISHED.all(first, last) && !BESIDE.any(first, last) } -/// Since the last frame line: what asking about title memory was. Kernel +/// Running totals of what asking about title memory was, never reset (the frame line takes what +/// came since its last take, [`asks`]; an A/B phase reads them, `crate::ab`). Kernel /// askings and the KiB they walked; view ranges answered by their bits, by /// the guards whole clean, and block by block; blocks made read-only; blocks /// opened by ourselves; nanoseconds waiting for the guards' lock and @@ -2205,16 +2206,42 @@ fn published(start: usize, end: usize) -> bool { /// 4 KiB pages they found written. #[cfg(feature = "diagnostics")] static ASKS: [std::sync::atomic::AtomicU64; 13] = [const { std::sync::atomic::AtomicU64::new(0) }; 13]; +/// What of [`ASKS`] the frame line took. +#[cfg(feature = "diagnostics")] +static ASKS_TAKEN: [std::sync::atomic::AtomicU64; 13] = [const { std::sync::atomic::AtomicU64::new(0) }; 13]; +/// [`ASKS`]' names, for the A/B counters line (`_ns`: nanoseconds). +#[cfg(feature = "diagnostics")] +pub(crate) const ASK_NAMES: [&str; 13] = [ + "kernel_asks", + "kernel_kib", + "ranges_by_bits", + "ranges_whole_clean", + "ranges_block_by_block", + "blocks_read_only", + "blocks_opened", + "guard_wait_ns", + "protect_ns", + "hole_questions", + "kernel_asks_refused", + "kernel_ask_ns", + "pages_written", +]; #[cfg(feature = "diagnostics")] fn tally(what: usize, n: u64) { ASKS[what].fetch_add(n, std::sync::atomic::Ordering::Relaxed); } -/// [`ASKS`], taken. +/// [`ASKS`] since the last take. #[cfg(feature = "diagnostics")] pub(crate) fn asks() -> [u64; 13] { - std::array::from_fn(|i| ASKS[i].swap(0, std::sync::atomic::Ordering::Relaxed)) + crate::ab::take(&ASKS, &ASKS_TAKEN) +} + +/// [`ASKS`] since the start. +#[cfg(feature = "diagnostics")] +pub(crate) fn ask_totals() -> [u64; 13] { + crate::ab::load(&ASKS) } /// The guards' lock, the wait for it counted. @@ -4497,7 +4524,7 @@ fn page_protection(at: usize) -> Option<(u32, bool)> { said } -/// Since the last frame line: what the title's faults asked Windows, and how +/// Running totals, never reset (as [`ASKS`]), of what the title's faults asked Windows, and how /// long they were looked at. Questions about a region ([`writable`] and /// [`readable_view`] with `VirtualQuery`) and their nanoseconds; questions /// about one page that the page answered, those it did not, and the @@ -4505,6 +4532,28 @@ fn page_protection(at: usize) -> Option<(u32, bool)> { /// nanoseconds, and the same with it on (all threads). #[cfg(feature = "diagnostics")] static FAULT_ASKS: [std::sync::atomic::AtomicU64; 9] = [const { std::sync::atomic::AtomicU64::new(0) }; 9]; +/// What of [`FAULT_ASKS`] the frame line took. +#[cfg(feature = "diagnostics")] +static FAULT_TAKEN: [std::sync::atomic::AtomicU64; 9] = [const { std::sync::atomic::AtomicU64::new(0) }; 9]; +/// [`FAULT_ASKS`]' names, for the A/B counters line (`_ns`: nanoseconds). +#[cfg(feature = "diagnostics")] +pub(crate) const FAULT_NAMES: [&str; 9] = [ + "fault_region_questions", + "fault_region_ns", + "fault_page_questions", + "fault_page_unanswered", + "fault_page_ns", + "faults_regions_only", + "faults_regions_only_ns", + "faults_page_first", + "faults_page_first_ns", +]; + +/// [`FAULT_ASKS`] since the start. +#[cfg(feature = "diagnostics")] +pub(crate) fn fault_totals() -> [u64; 9] { + crate::ab::load(&FAULT_ASKS) +} #[cfg(feature = "diagnostics")] const FA_REGIONS: usize = 0; #[cfg(feature = "diagnostics")] @@ -4540,7 +4589,7 @@ impl Drop for FaultTimed { /// The faults' line of the frame lines, per frame ([`FAULT_ASKS`]). #[cfg(feature = "diagnostics")] pub(crate) fn fault_asks_line(frames: f64) { - let n: Vec = FAULT_ASKS.iter().map(|c| c.swap(0, std::sync::atomic::Ordering::Relaxed) as f64 / frames).collect(); + let n: Vec = crate::ab::take(&FAULT_ASKS, &FAULT_TAKEN).iter().map(|&c| c as f64 / frames).collect(); if n.iter().all(|&v| v == 0.0) { return; } @@ -4693,6 +4742,18 @@ mod parts_tests { #[cfg(test)] mod tests { use super::*; + + /// The frame line takes what came since its last take and leaves the total, which an A/B + /// phase reads as well (`crate::ab`). + #[cfg(feature = "diagnostics")] + #[test] + fn a_running_total_survives_the_frame_lines_take() { + use std::sync::atomic::Ordering::Relaxed; + let before = ASKS[8].load(Relaxed); + tally(8, 1 << 40); + assert!(asks()[8] >= 1 << 40, "the take has what came"); + assert!(ASKS[8].load(Relaxed) >= before + (1 << 40), "the take emptied the total"); + } use windows_sys::Win32::System::Memory::{ MEM_COMMIT, MEM_RELEASE, MEM_RESERVE, PAGE_READWRITE, VirtualAlloc, VirtualFree, };