mirror of
https://github.com/DoubleGhosty/cool_fox.git
synced 2026-12-06 08:38:35 +00:00
residency: memory pressure from the card's share of the PC's memory (VICTORY_SHARED_PRESSURE=0: off)
Every buffer the CPU maps and the card reads counts against DXGI's non-local budget, which Windows caps at half the RAM, not against free RAM. On 16 and 24 GB PCs it ran out with gigabytes free, every such buffer was refused from then on, down to 256 bytes, and the run ended when a host command list could not close (0x8007000E; #156, #206). The once-a-second sweep now reads that budget: pressure from 85 % (off under 75 %), critical from 95 %, the same levels the RAM sample sets, so spares, staging and texture prefetch shrink before the cap is reached. The limit and its ending are VIctory-NVIDIA's issue 1520 (trustno1-coder), whose fix scaled its own L0 caches down on PCs under 24 GiB.
This commit is contained in:
@ -6,6 +6,10 @@ use windows_sys::Win32::System::SystemInformation::{GlobalMemoryStatusEx, MEMORY
|
||||
const MIB: u64 = 1 << 20;
|
||||
const GIB: u64 = 1 << 30;
|
||||
static PRESSURED: AtomicBool = AtomicBool::new(false);
|
||||
/// The card's share of the PC's memory (DXGI's non-local budget, which Windows
|
||||
/// caps at half the RAM) is nearly used up: [`SHARED`]'s levels.
|
||||
static SHARED_PRESSURED: AtomicBool = AtomicBool::new(false);
|
||||
static SHARED_CRITICAL: AtomicBool = AtomicBool::new(false);
|
||||
static HEADROOM: AtomicU64 = AtomicU64::new(u64::MAX);
|
||||
|
||||
fn memory_status() -> Option<MEMORYSTATUSEX> {
|
||||
@ -110,6 +114,7 @@ fn next_pressure(was: bool, physical: u64, commit: u64) -> bool {
|
||||
|
||||
/// Called by the once-per-second submission sweep. A failed sample keeps the last good state.
|
||||
pub(crate) fn refresh() {
|
||||
refresh_shared();
|
||||
let Some(status) = memory_status() else { return };
|
||||
let (physical, commit) = (status.ullAvailPhys, status.ullAvailPageFile);
|
||||
HEADROOM.store(physical.min(commit), Ordering::Relaxed);
|
||||
@ -141,14 +146,65 @@ pub(crate) fn headroom() -> Option<u64> {
|
||||
}
|
||||
|
||||
pub(crate) fn pressured() -> bool {
|
||||
PRESSURED.load(Ordering::Relaxed)
|
||||
PRESSURED.load(Ordering::Relaxed) || SHARED_PRESSURED.load(Ordering::Relaxed)
|
||||
}
|
||||
|
||||
/// Whether the shared memory's use asks for pressure, and for the critical
|
||||
/// level, from its use and budget (`was`: pressure was on). On at 85 % of the
|
||||
/// budget, off again under 75 %; critical from 95 %.
|
||||
///
|
||||
/// Every buffer the CPU maps and the card reads (upload, shared, readback)
|
||||
/// counts against that budget, not against free RAM: on a 16 or 24 GB PC it
|
||||
/// ran out with gigabytes of RAM still free, and from then on every such
|
||||
/// buffer was refused, down to 256 bytes, until a host command list could not
|
||||
/// close (0x8007000E; #156, #206). The same limit and ending as VIctory-NVIDIA's
|
||||
/// issue 1520, whose fix scaled its L0 caches down on PCs under 24 GiB.
|
||||
fn shared_levels(was: bool, usage: u64, budget: u64) -> (bool, bool) {
|
||||
if budget == 0 {
|
||||
return (false, false);
|
||||
}
|
||||
let percent = u128::from(usage) * 100 / u128::from(budget);
|
||||
let pressure = if was { percent >= 75 } else { percent >= 85 };
|
||||
(pressure, percent >= 95)
|
||||
}
|
||||
|
||||
/// Samples the shared memory's use (once a second, by [`refresh`]).
|
||||
/// `VICTORY_SHARED_PRESSURE=0`: not looked at, as before.
|
||||
fn refresh_shared() {
|
||||
static ON: OnceLock<bool> = OnceLock::new();
|
||||
if !*ON.get_or_init(|| std::env::var_os("VICTORY_SHARED_PRESSURE").is_none_or(|v| v != "0")) {
|
||||
return;
|
||||
}
|
||||
let Some([_, [budget, usage, ..]]) = crate::host::host().and_then(|h| h.video_memory_groups()) else {
|
||||
return;
|
||||
};
|
||||
let was = SHARED_PRESSURED.load(Ordering::Relaxed);
|
||||
let (pressure, critical) = shared_levels(was, usage, budget);
|
||||
SHARED_CRITICAL.store(critical, Ordering::Relaxed);
|
||||
if pressure != was {
|
||||
SHARED_PRESSURED.store(pressure, Ordering::Relaxed);
|
||||
if pressure {
|
||||
tracing::warn!(
|
||||
frame = crate::frame::current(),
|
||||
shared_used_mib = usage >> 20,
|
||||
shared_budget_mib = budget >> 20,
|
||||
buffer_mirror_span_mib = crate::shadow::live_span_bytes() >> 20,
|
||||
texture_staging_mib = crate::texture::live_staging_bytes() >> 20,
|
||||
"the card's share of the PC's memory is nearly used: limiting texture prefetch and idle upload storage (VICTORY_SHARED_PRESSURE=0: off)"
|
||||
);
|
||||
} else {
|
||||
tracing::info!(shared_used_mib = usage >> 20, shared_budget_mib = budget >> 20, "shared memory pressure cleared");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// True when OS commit headroom is critically low (< 1.5 GiB) or an allocation
|
||||
/// just failed with `E_OUTOFMEMORY`, requiring emergency throttling.
|
||||
pub(crate) fn critical_commit() -> bool {
|
||||
let now = crate::host::coarse_ms();
|
||||
now < RETRY_UPLOAD_AT.load(Ordering::Relaxed) || sample_commit(now) < 1536 * MIB
|
||||
now < RETRY_UPLOAD_AT.load(Ordering::Relaxed)
|
||||
|| SHARED_CRITICAL.load(Ordering::Relaxed)
|
||||
|| sample_commit(now) < 1536 * MIB
|
||||
}
|
||||
|
||||
/// A soft per-frame admission limit. A single texture can cross it. The
|
||||
@ -167,6 +223,17 @@ pub(crate) fn prefetch_limit(bytes: u64, pressure: bool, critical: bool, limited
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn shared_memory_pressure_has_a_gap_and_a_critical_level() {
|
||||
let budget = 12 * GIB;
|
||||
assert_eq!(shared_levels(false, budget / 100 * 80, budget), (false, false));
|
||||
assert_eq!(shared_levels(false, budget / 100 * 86, budget), (true, false));
|
||||
assert_eq!(shared_levels(true, budget / 100 * 80, budget), (true, false));
|
||||
assert_eq!(shared_levels(true, budget / 100 * 70, budget), (false, false));
|
||||
assert_eq!(shared_levels(false, budget / 100 * 96, budget), (true, true));
|
||||
assert_eq!(shared_levels(false, 5, 0), (false, false));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bake_and_lookahead_leave_room_for_live_game_allocations() {
|
||||
for (commit, bake, lookahead, growth) in [
|
||||
|
||||
@ -316,6 +316,7 @@ The on/off switches below (`...=1`) are off when unset or set to `0`.
|
||||
| `VICTORY_KEYS=30:Enter,35:X` | presses keys at those seconds, focus or not |
|
||||
| `VICTORY_KEYS_UNTIL=<file>` | no more of those keys once the file is there (a script makes it when the world draws: a run with nobody at the keyboard) |
|
||||
| `VICTORY_SAMPLER_ORDER=0` | static samplers in the order each process meets them; by default they are made in the order of `pipelines/static-samplers.txt`, so the driver's disk cache finds pipelines again across runs (from VIctory-NVIDIA) |
|
||||
| `VICTORY_SHARED_PRESSURE=0` | the card's share of the PC's memory (Task Manager's "Shared GPU memory", half the RAM) is not watched; by default memory pressure is on from 85 % of it (off under 75 %) and critical from 95 %, so spares, staging and texture prefetch shrink before every shared buffer is refused (#156, #206) |
|
||||
| `VICTORY_CRASH_SAVE=<rva>:<len>,...` | saves title memory to the cache folder on a crash |
|
||||
| `VICTORY_CAPTURE=<dir>` | saves presented frames (powers of two, or `VICTORY_CAPTURE_FRAMES=<list>`) |
|
||||
| `VICTORY_DUMP_TARGETS=1` | with the two above, saves every render target of those frames |
|
||||
|
||||
Reference in New Issue
Block a user