From de2d0d46c15f4a526a2c60a8b586379678d10ea1 Mon Sep 17 00:00:00 2001 From: Netzlash Date: Tue, 1 Dec 2026 21:50:41 +0100 Subject: [PATCH 1/2] d3d12: attribute residency pressure using active thresholds Use the active entry or retention thresholds for both the pressure decision and its physical-only diagnostic. Cover commit-only retention and exact boundaries with synthetic regression tests. --- d3d12/crates/leonida-gpu/src/residency.rs | 66 ++++++++++++++++------- 1 file changed, 48 insertions(+), 18 deletions(-) diff --git a/d3d12/crates/leonida-gpu/src/residency.rs b/d3d12/crates/leonida-gpu/src/residency.rs index 0612d6d4b2..324a0ac4a0 100644 --- a/d3d12/crates/leonida-gpu/src/residency.rs +++ b/d3d12/crates/leonida-gpu/src/residency.rs @@ -87,9 +87,9 @@ pub(crate) fn admit_texture_pass(bake: bool) -> bool { } /// What this module said since the start, for the memory lines ([`line`]): -/// looks once a second ([`refresh`]), those under pressure, those of them -/// with commit above where pressure by commit begins (4 GiB: the pressure was -/// physical memory's), those with commit under [`critical_commit`]'s 1.5 GiB; +/// looks once a second ([`refresh`]), those under pressure, those under +/// pressure from physical memory alone at the active thresholds, those with +/// commit under [`critical_commit`]'s 1.5 GiB; /// texture passes asked for and refused for a failed allocation and for the /// reserve ([`admit_texture_pass`]); new staging buffers asked for and /// refused likewise ([`allow_staging_growth`]); allocations that failed for @@ -126,8 +126,8 @@ pub(crate) fn line() { bytes => bytes >> 20, }; tracing::info!( - "memory, the PC's (residency), since the start: {} looks a second apart, {} under pressure ({} of them with more than 4 GiB left to commit: \ - by physical memory alone), {} with less than 1.5 GiB left to commit; the least seen: {} MB of physical memory free, {} MB left to commit; \ + "memory, the PC's (residency), since the start: {} looks a second apart, {} under pressure ({} by physical memory alone), \ + {} with less than 1.5 GiB left to commit; the least seen: {} MB of physical memory free, {} MB left to commit; \ texture passes asked for {}, refused {} after a failed allocation and {} for the reserve; new staging buffers ahead asked for {}, refused {} \ and {}; allocations failed for want of memory {}; commit critical in {} of {} questions; now {} (LEONIDA_COMMIT_FLOOR: {})", n[SAID_LOOKS], @@ -177,12 +177,16 @@ pub(crate) fn limited_host_ram() -> bool { *LIMITED.get_or_init(|| memory_status().is_some_and(|s| s.ullTotalPhys <= 32 * GIB)) } -fn next_pressure(was: bool, physical: u64, commit: u64) -> bool { - if was { - physical < 5 * GIB || commit < 6 * GIB +fn next_pressure(was: bool, physical: u64, commit: u64) -> (bool, bool) { + let (physical_pressure, commit_pressure) = if was { + (physical < 5 * GIB, commit < 6 * GIB) } else { - physical <= 3 * GIB || commit <= 4 * GIB - } + (physical <= 3 * GIB, commit <= 4 * GIB) + }; + ( + physical_pressure || commit_pressure, + physical_pressure && !commit_pressure, + ) } /// Called by the once-per-second submission sweep. A failed sample keeps the last good state. @@ -191,11 +195,11 @@ pub(crate) fn refresh() { let (physical, commit) = (status.ullAvailPhys, status.ullAvailPageFile); HEADROOM.store(physical.min(commit), Ordering::Relaxed); let was = pressured(); - let pressure = next_pressure(was, physical, commit); + let (pressure, physical_only) = next_pressure(was, physical, commit); count(SAID_LOOKS); if pressure { count(SAID_PRESSURED); - if commit > 4 * GIB { + if physical_only { count(SAID_PRESSURED_PHYSICAL); } } @@ -278,12 +282,38 @@ mod tests { #[test] fn either_constraint_enters_pressure_and_both_must_recover() { - assert!(next_pressure(false, 12 * GIB, 4 * GIB)); - assert!(next_pressure(false, 3 * GIB, 16 * GIB)); - assert!(next_pressure(true, 4 * GIB, 7 * GIB)); - assert!(next_pressure(true, 6 * GIB, 5 * GIB)); - assert!(!next_pressure(true, 5 * GIB, 6 * GIB)); - assert!(!next_pressure(false, 4 * GIB, 5 * GIB)); + assert!(next_pressure(false, 12 * GIB, 4 * GIB).0); + assert!(next_pressure(false, 3 * GIB, 16 * GIB).0); + assert!(next_pressure(true, 4 * GIB, 7 * GIB).0); + assert!(next_pressure(true, 6 * GIB, 5 * GIB).0); + assert!(!next_pressure(true, 5 * GIB, 6 * GIB).0); + assert!(!next_pressure(false, 4 * GIB, 5 * GIB).0); + } + + #[test] + fn physical_only_pressure_uses_the_active_thresholds() { + for (was, physical, commit, expected_pressure, expected_physical_only) in [ + (true, 6 * GIB, 5 * GIB, true, false), + (true, 4 * GIB, 5 * GIB, true, false), + (false, 3 * GIB, 4 * GIB + 1, true, true), + (false, 3 * GIB, 4 * GIB, true, false), + (false, 3 * GIB + 1, 4 * GIB, true, false), + (false, 3 * GIB + 1, 4 * GIB + 1, false, false), + (true, 5 * GIB - 1, 6 * GIB, true, true), + (true, 5 * GIB - 1, 6 * GIB - 1, true, false), + (true, 5 * GIB, 6 * GIB - 1, true, false), + (true, 5 * GIB, 6 * GIB, false, false), + ] { + let (pressure, physical_only) = next_pressure(was, physical, commit); + assert_eq!( + pressure, expected_pressure, + "was={was}, physical={physical}, commit={commit}" + ); + assert_eq!( + physical_only, expected_physical_only, + "was={was}, physical={physical}, commit={commit}" + ); + } } #[test] From 26da88e8ea79b35c4618278ff52c8de1477ac102 Mon Sep 17 00:00:00 2001 From: Netzlash Date: Tue, 1 Dec 2026 23:24:15 +0100 Subject: [PATCH 2/2] d3d12: report mixed residency pressure in sample counters --- d3d12/crates/leonida-gpu/src/residency.rs | 143 ++++++++++++++-------- 1 file changed, 94 insertions(+), 49 deletions(-) diff --git a/d3d12/crates/leonida-gpu/src/residency.rs b/d3d12/crates/leonida-gpu/src/residency.rs index 324a0ac4a0..7dceb19795 100644 --- a/d3d12/crates/leonida-gpu/src/residency.rs +++ b/d3d12/crates/leonida-gpu/src/residency.rs @@ -87,14 +87,15 @@ pub(crate) fn admit_texture_pass(bake: bool) -> bool { } /// What this module said since the start, for the memory lines ([`line`]): -/// looks once a second ([`refresh`]), those under pressure, those under -/// pressure from physical memory alone at the active thresholds, those with -/// commit under [`critical_commit`]'s 1.5 GiB; +/// looks once a second ([`refresh`]), those under pressure, their breakdown +/// by active physical and commit constraints, and those with commit under +/// [`critical_commit`]'s 1.5 GiB; /// texture passes asked for and refused for a failed allocation and for the /// reserve ([`admit_texture_pass`]); new staging buffers asked for and /// refused likewise ([`allow_staging_growth`]); allocations that failed for /// want of memory; questions whether commit is critical and how often it was. -static SAID: [AtomicU64; 13] = [const { AtomicU64::new(0) }; 13]; +const SAID_COUNT: usize = 15; +static SAID: [AtomicU64; SAID_COUNT] = [const { AtomicU64::new(0) }; SAID_COUNT]; const SAID_LOOKS: usize = 0; const SAID_PRESSURED: usize = 1; const SAID_PRESSURED_PHYSICAL: usize = 2; @@ -108,6 +109,8 @@ const SAID_GROWTH_REFUSED_RESERVE: usize = 9; const SAID_FAILED: usize = 10; const SAID_CRITICAL_ASKED: usize = 11; const SAID_CRITICAL: usize = 12; +const SAID_PRESSURED_COMMIT: usize = 13; +const SAID_PRESSURED_BOTH: usize = 14; /// The least physical memory free and the least commit left that [`refresh`] /// has seen. static LEAST: [AtomicU64; 2] = [const { AtomicU64::new(u64::MAX) }; 2]; @@ -126,13 +129,15 @@ pub(crate) fn line() { bytes => bytes >> 20, }; tracing::info!( - "memory, the PC's (residency), since the start: {} looks a second apart, {} under pressure ({} by physical memory alone), \ + "memory, the PC's (residency), since the start: {} looks a second apart, {} under pressure ({} physical only, {} commit only, {} both), \ {} with less than 1.5 GiB left to commit; the least seen: {} MB of physical memory free, {} MB left to commit; \ texture passes asked for {}, refused {} after a failed allocation and {} for the reserve; new staging buffers ahead asked for {}, refused {} \ and {}; allocations failed for want of memory {}; commit critical in {} of {} questions; now {} (LEONIDA_COMMIT_FLOOR: {})", n[SAID_LOOKS], n[SAID_PRESSURED], n[SAID_PRESSURED_PHYSICAL], + n[SAID_PRESSURED_COMMIT], + n[SAID_PRESSURED_BOTH], n[SAID_LOOKS_CRITICAL], least(0), least(1), @@ -177,35 +182,56 @@ pub(crate) fn limited_host_ram() -> bool { *LIMITED.get_or_init(|| memory_status().is_some_and(|s| s.ullTotalPhys <= 32 * GIB)) } -fn next_pressure(was: bool, physical: u64, commit: u64) -> (bool, bool) { +#[derive(Debug, Eq, PartialEq)] +enum PressureCause { + PhysicalOnly, + CommitOnly, + Both, +} + +fn next_pressure(was: bool, physical: u64, commit: u64) -> Option { let (physical_pressure, commit_pressure) = if was { (physical < 5 * GIB, commit < 6 * GIB) } else { (physical <= 3 * GIB, commit <= 4 * GIB) }; - ( - physical_pressure || commit_pressure, - physical_pressure && !commit_pressure, - ) + match (physical_pressure, commit_pressure) { + (true, false) => Some(PressureCause::PhysicalOnly), + (false, true) => Some(PressureCause::CommitOnly), + (true, true) => Some(PressureCause::Both), + (false, false) => None, + } } -/// Called by the once-per-second submission sweep. A failed sample keeps the last good state. -pub(crate) fn refresh() { - let Some(status) = memory_status() else { return }; - let (physical, commit) = (status.ullAvailPhys, status.ullAvailPageFile); - HEADROOM.store(physical.min(commit), Ordering::Relaxed); - let was = pressured(); - let (pressure, physical_only) = next_pressure(was, physical, commit); +/// Records one successful memory-status sample and returns the admission flag. +fn record_sample(was: bool, physical: u64, commit: u64, counts: &[AtomicU64; SAID_COUNT]) -> bool { + let count = |index: usize| { + counts[index].fetch_add(1, Ordering::Relaxed); + }; + let cause = next_pressure(was, physical, commit); + let pressure = cause.is_some(); count(SAID_LOOKS); - if pressure { + if let Some(cause) = cause { count(SAID_PRESSURED); - if physical_only { - count(SAID_PRESSURED_PHYSICAL); - } + count(match cause { + PressureCause::PhysicalOnly => SAID_PRESSURED_PHYSICAL, + PressureCause::CommitOnly => SAID_PRESSURED_COMMIT, + PressureCause::Both => SAID_PRESSURED_BOTH, + }); } if commit < 1536 * MIB { count(SAID_LOOKS_CRITICAL); } + pressure +} + +/// Called by the once-per-second submission sweep. A failed sample keeps the last good state. +pub(crate) fn refresh() { + let Some(status) = memory_status() else { return }; + let (physical, commit) = (status.ullAvailPhys, status.ullAvailPageFile); + HEADROOM.store(physical.min(commit), Ordering::Relaxed); + let was = pressured(); + let pressure = record_sample(was, physical, commit, &SAID); LEAST[0].fetch_min(physical, Ordering::Relaxed); LEAST[1].fetch_min(commit, Ordering::Relaxed); if pressure != was { @@ -281,41 +307,60 @@ mod tests { } #[test] - fn either_constraint_enters_pressure_and_both_must_recover() { - assert!(next_pressure(false, 12 * GIB, 4 * GIB).0); - assert!(next_pressure(false, 3 * GIB, 16 * GIB).0); - assert!(next_pressure(true, 4 * GIB, 7 * GIB).0); - assert!(next_pressure(true, 6 * GIB, 5 * GIB).0); - assert!(!next_pressure(true, 5 * GIB, 6 * GIB).0); - assert!(!next_pressure(false, 4 * GIB, 5 * GIB).0); - } - - #[test] - fn physical_only_pressure_uses_the_active_thresholds() { - for (was, physical, commit, expected_pressure, expected_physical_only) in [ - (true, 6 * GIB, 5 * GIB, true, false), - (true, 4 * GIB, 5 * GIB, true, false), - (false, 3 * GIB, 4 * GIB + 1, true, true), - (false, 3 * GIB, 4 * GIB, true, false), - (false, 3 * GIB + 1, 4 * GIB, true, false), - (false, 3 * GIB + 1, 4 * GIB + 1, false, false), - (true, 5 * GIB - 1, 6 * GIB, true, true), - (true, 5 * GIB - 1, 6 * GIB - 1, true, false), - (true, 5 * GIB, 6 * GIB - 1, true, false), - (true, 5 * GIB, 6 * GIB, false, false), + fn pressure_attribution_uses_the_active_thresholds() { + use PressureCause::{Both, CommitOnly, PhysicalOnly}; + for (was, physical, commit, expected) in [ + (true, 6 * GIB, 5 * GIB, Some(CommitOnly)), + (true, 4 * GIB, 5 * GIB, Some(Both)), + (false, 3 * GIB, 4 * GIB + 1, Some(PhysicalOnly)), + (false, 3 * GIB, 4 * GIB, Some(Both)), + (false, 3 * GIB + 1, 4 * GIB, Some(CommitOnly)), + (false, 3 * GIB + 1, 4 * GIB + 1, None), + (true, 5 * GIB - 1, 6 * GIB, Some(PhysicalOnly)), + (true, 5 * GIB - 1, 6 * GIB - 1, Some(Both)), + (true, 5 * GIB, 6 * GIB - 1, Some(CommitOnly)), + (true, 5 * GIB, 6 * GIB, None), ] { - let (pressure, physical_only) = next_pressure(was, physical, commit); assert_eq!( - pressure, expected_pressure, - "was={was}, physical={physical}, commit={commit}" - ); - assert_eq!( - physical_only, expected_physical_only, + next_pressure(was, physical, commit), + expected, "was={was}, physical={physical}, commit={commit}" ); } } + #[test] + fn successful_samples_update_the_reported_counters() { + let counts = [const { AtomicU64::new(0) }; SAID_COUNT]; + let read = || { + [ + SAID_LOOKS, + SAID_PRESSURED, + SAID_PRESSURED_PHYSICAL, + SAID_PRESSURED_COMMIT, + SAID_PRESSURED_BOTH, + SAID_LOOKS_CRITICAL, + ] + .map(|index| counts[index].load(Ordering::Relaxed)) + }; + let mut was = false; + // The same severe physical sample qualifies as physical-only at entry + // and as both constraints on the next sample at retention thresholds. + let commit_above_entry = 59 * GIB / 10; + for (physical, commit, expected_pressure, totals) in [ + (4 * GIB, 5 * GIB, false, [1, 0, 0, 0, 0, 0]), + (GIB, commit_above_entry, true, [2, 1, 1, 0, 0, 0]), + (GIB, commit_above_entry, true, [3, 2, 1, 0, 1, 0]), + (6 * GIB, 5 * GIB, true, [4, 3, 1, 1, 1, 0]), + (GIB, 1536 * MIB - 1, true, [5, 4, 1, 1, 2, 1]), + (5 * GIB, 6 * GIB, false, [6, 4, 1, 1, 2, 1]), + ] { + was = record_sample(was, physical, commit, &counts); + assert_eq!(was, expected_pressure); + assert_eq!(read(), totals); + } + } + #[test] fn prefetch_respects_existing_budgets_and_resumes_after_recovery() { assert_eq!(prefetch_limit(512 << 20, false, false, true, GIB), 64 << 20);