Memory the card was given stays with it while there is room

Both losses were reads in memory given back seconds before. Empty texture
blocks (up to 4 GiB) stay below 85 % of the card's budget, windows of title
memory and staging buffers stay for five minutes with 12 GiB of the PC's
memory free, and up to 1 GiB of scratch chunks stays. Nothing read past a view
or through a wrong slot finds the hole then. VICTORY_KEEP_FREED=0: as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
localandClaude Opus 5.5 committed 2026-11-25 02:38:31 -06:00
1 parent c3830a5d36
commit 719dc45e38
2 files changed
+71 -7

No files matched your search

+34 -2
View File
@@ -157,11 +157,37 @@ fn vram_idle_for(video_memory: Option<[u64; 4]>, keep: bool) -> u64 {
}
}
/// Whether memory the card was given stays with it while there is room (on
/// by default; `VICTORY_KEEP_FREED=0`: it is given back as before). A shader
/// that reads past a view (a raw load through a typed, structured or
/// constant buffer view has no end on AMD: `indirect::tests::raw_loads_past_views`)
/// or through a slot that shows wrong data reads whatever lies there, and
/// nothing happens; where memory was given back it is a page fault, and the
/// device is lost. Both losses of 2026-11-25 were reads in memory given back
/// seconds before (a block of textures; a list's scratch and a window of
/// title memory). Kept are: empty blocks of textures ([`crate::pool`]) below
/// 85 % of the card's budget, windows of title memory and staging buffers
/// for five minutes with 12 GiB of the PC's memory free, and 1 GiB of
/// scratch chunks.
pub(crate) fn keep_freed() -> bool {
static ON: OnceLock<bool> = OnceLock::new();
*ON.get_or_init(|| std::env::var_os("VICTORY_KEEP_FREED").is_none_or(|v| v != "0"))
}
/// Effective idle timeout (in milliseconds) for system-RAM caches (staging
/// buffers, loose shadows, and the mapped pipeline library), scaled down
/// when free physical RAM is low. Never returns less than 2,000 ms.
/// With plenty of it five minutes ([`keep_freed`]).
pub(crate) fn ram_idle_ms(avail_phys: Option<u64>) -> u64 {
ram_idle_for(avail_phys, keep_freed())
}
fn ram_idle_for(avail_phys: Option<u64>, keep: bool) -> u64 {
const GIB: u64 = 1024 * 1024 * 1024;
const KEPT_MS: u64 = 300_000;
if keep && avail_phys.is_some_and(|avail| avail >= 12 * GIB) {
return KEPT_MS;
}
const NORMAL_MS: u64 = 10_000;
const MODERATE_MS: u64 = 5_000;
const HIGH_MS: u64 = 2_000;
@@ -215,6 +241,7 @@ fn sweep() {
{
return;
}
crate::pool::room(host().and_then(|h| h.video_memory()));
crate::pipeline::trim_library();
crate::shadow::let_go_idle();
crate::texture::let_go_idle();
@@ -1541,6 +1568,8 @@ const SCRATCH_CHUNK: u64 = 4 << 20;
const SCRATCH_FIRST: u64 = 256 << 10;
/// The most bytes of chunks kept while no list holds them.
const SPARE_MOST: u64 = 128 << 20;
/// The same with [`keep_freed`].
const SPARE_KEPT: u64 = 1 << 30;
/// Whether lists share their scratch chunks: a list takes a chunk when it
/// first needs one, a small one first, and gives all of them back when it
@@ -1621,7 +1650,7 @@ fn give_chunks(chunks: Vec<(Chunk, u64)>) {
let mut spare = SPARE.lock().unwrap_or_else(|e| e.into_inner());
for (chunk, size) in chunks {
let back = if scratch_pool() {
spare.give(chunk, size, SPARE_MOST)
spare.give(chunk, size, if keep_freed() { SPARE_KEPT } else { SPARE_MOST })
} else {
Some(chunk)
};
@@ -8985,7 +9014,10 @@ mod batch { // Batch::texture_due lives here; `mod tests` is already taken at
#[test]
fn ram_idle_ms_scales_with_pressure() {
use super::ram_idle_ms;
let ram_idle_ms = |avail| super::ram_idle_for(avail, false);
assert_eq!(super::ram_idle_for(Some(12 << 30), true), 300_000);
assert_eq!(super::ram_idle_for(Some((12 << 30) - 1), true), 10_000);
assert_eq!(super::ram_idle_for(None, true), 10_000);
let gib = 1024 * 1024 * 1024;
// Normal (>= 4 GiB)
assert_eq!(ram_idle_ms(Some(8 * gib)), 10_000);
+37 -5
View File
@@ -20,7 +20,7 @@
use std::collections::{BTreeMap, VecDeque};
use std::ffi::c_void;
use std::sync::Mutex;
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::atomic::{AtomicU32, AtomicUsize, Ordering};
use windows::Win32::Graphics::Direct3D12::*;
use windows::core::{GUID, HRESULT, IUnknown, Interface};
@@ -29,6 +29,25 @@ use windows::core::{GUID, HRESULT, IUnknown, Interface};
const BLOCK: u64 = 64 << 20;
/// Blocks with nothing in them that are kept.
const SPARE: usize = 2;
/// The same while the card has room ([`room`]): 4 GiB of them.
const SPARE_KEPT: usize = 64;
/// How many are kept now.
static SPARES: AtomicUsize = AtomicUsize::new(SPARE);
/// Sets how many empty blocks stay, by the card's memory (budget and use):
/// [`SPARE_KEPT`] below 85 % ([`crate::host::keep_freed`]), else [`SPARE`].
/// Asked once a second.
pub(crate) fn room(memory: Option<[u64; 4]>) {
SPARES.store(spares(memory, crate::host::keep_freed()), Ordering::Relaxed);
}
fn spares(memory: Option<[u64; 4]>, keep: bool) -> usize {
match memory {
Some([budget, used, ..]) if keep && u128::from(used) * 100 < u128::from(budget) * 85 => SPARE_KEPT,
_ => SPARE,
}
}
/// Milliseconds until a piece given back is used again. (It was three
/// frames, a tenth of a second while the title shows a loading screen at 30
/// a second, with up to 18 submissions in the air.)
@@ -106,11 +125,13 @@ struct Pool<B> {
blocks: Vec<Option<Block<B>>>,
/// Pieces given back, and when each may be used again.
resting: VecDeque<(u64, Piece)>,
/// The empty blocks it was last told to keep.
spare: usize,
}
impl<B: Clone> Pool<B> {
const fn new() -> Pool<B> {
Pool { blocks: Vec::new(), resting: VecDeque::new() }
Pool { blocks: Vec::new(), resting: VecDeque::new(), spare: 0 }
}
/// A piece of `len` bytes and the memory it lies in, and whether that
@@ -168,12 +189,15 @@ impl<B: Clone> Pool<B> {
any |= block.free.whole(BLOCK);
}
}
if any {
// (Without a piece given back too, once there is less room.)
let most = SPARES.load(Ordering::Relaxed);
if any || most < self.spare {
self.spare = most;
let mut spare = 0;
for block in &mut self.blocks {
if block.as_ref().is_some_and(|b| b.free.whole(BLOCK)) {
spare += 1;
if spare > SPARE {
if spare > most {
*block = None;
}
}
@@ -379,6 +403,14 @@ mod tests {
assert!(r.whole(1024));
}
#[test]
fn empty_blocks_stay_while_the_card_has_room() {
assert_eq!(spares(Some([100, 84, 0, 0]), true), SPARE_KEPT);
assert_eq!(spares(Some([100, 85, 0, 0]), true), SPARE);
assert_eq!(spares(Some([100, 10, 0, 0]), false), SPARE);
assert_eq!(spares(None, true), SPARE);
}
#[test]
fn pieces_never_overlap_and_rest_before_they_are_used_again() {
let mut pool: Pool<u32> = Pool::new();
@@ -408,7 +440,7 @@ mod tests {
pool.give(p, 20);
}
pool.rested(20 + REST);
assert_eq!(pool.blocks(), blocks.min(SPARE));
assert_eq!(pool.blocks(), blocks.min(SPARES.load(Ordering::Relaxed)));
// Too big for a block, and a block that cannot be made.
assert!(pool.take(BLOCK, 65536, 30, make).is_none());
let mut none: Pool<u32> = Pool::new();