Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 103 additions & 28 deletions d3d12/crates/leonida-gpu/src/residency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 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, 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;
Expand All @@ -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];
Expand All @@ -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 ({} 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 ({} 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),
Expand Down Expand Up @@ -177,31 +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 {
if was {
physical < 5 * GIB || commit < 6 * GIB
#[derive(Debug, Eq, PartialEq)]
enum PressureCause {
PhysicalOnly,
CommitOnly,
Both,
}

fn next_pressure(was: bool, physical: u64, commit: u64) -> Option<PressureCause> {
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)
};
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 = 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 commit > 4 * GIB {
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 {
Expand Down Expand Up @@ -277,13 +307,58 @@ 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));
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),
] {
assert_eq!(
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]
Expand Down