From 924911fbaf497bb6c63a28cc57a649c476f9af94 Mon Sep 17 00:00:00 2001 From: Yorivel <197960446+Yorivel@users.noreply.github.com> Date: Fri, 27 Nov 2026 17:19:37 +0200 Subject: [PATCH] Host lists: copy sources come from a pool; under pressure with memory to spare, keep 128 MiB of spares Every copy a list recorded (HostList::upload) made a new upload or shared buffer and waited for it to be resident, then freed it at the list's reset. They are now taken from a pool by size class (power of two, 64 KiB at least) and given back at the reset; VICTORY_UPLOAD_POOL_MB (1024 per kind, 0 = as before) caps what is kept. Under memory pressure, the spare scratch chunks (and the pooled copy sources) are trimmed down to SPARE_MOST (128 MiB) instead of all of them, but only while 8 GiB or more of the PC's memory is free (keeps_under_pressure). Then the pressure is the commit limit's, not the memory's: with 64 GB and a system-managed page file the commit headroom stays under 4 GiB for the whole session with ~20 GB free, and the spares freed every second were made again at once. With memory really short they all go, as before: on a 16 GB PC keeping them cost ~40 ms a frame of waits for flights (review on #57). RTX 4090, i9-14900K, 64 GB, about 5 minutes of play each: - waits for new memory to be resident: 68,000 / 17.3 s -> 37,000 / 9.3 s - longest single wait: 1,006 ms -> 152 ms - copy-source buffers made: 19,324 -> 93 - scratch chunks made: 12,925 -> 2,462 Co-Authored-By: Claude Opus 5.5 --- crates/victory-gpu/src/host.rs | 119 +++++++++++++++++++++++++--- crates/victory-gpu/src/residency.rs | 5 ++ 2 files changed, 115 insertions(+), 9 deletions(-) diff --git a/crates/victory-gpu/src/host.rs b/crates/victory-gpu/src/host.rs index 66710676f0..3b8d47a0d0 100644 --- a/crates/victory-gpu/src/host.rs +++ b/crates/victory-gpu/src/host.rs @@ -454,6 +454,7 @@ fn sweep() { crate::book::check(); crate::residency::refresh(); if crate::residency::pressured() { + trim_spare_uploads(); trim_spare_chunks(); } crate::pool::room(host().and_then(|h| h.video_memory())); @@ -2393,16 +2394,96 @@ fn give_chunks(chunks: Vec<(Chunk, u64)>) { /// since the start. pub(crate) static SPARE_TRIMMED: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); -/// Drops spare scratch chunks kept idle when host memory is pressured. +/// Copy sources of [`HostList::upload`] no list holds: upload buffers, +/// then shared buffers, by size class. +static UPLOAD_SPARE: [Mutex>; 2] = [const { Mutex::new(Spare::new()) }; 2]; + +/// The most bytes [`UPLOAD_SPARE`] keeps of each kind +/// (`VICTORY_UPLOAD_POOL_MB`, 1024; 0: no pool, a buffer per copy as +/// before). Every copy a list recorded made a buffer of its own and waited +/// for it to be resident: 19,324 shared buffers and 9.3 s of waits in a +/// 5-minute session (RTX 4090). +fn upload_pool_most() -> u64 { + static MB: OnceLock = OnceLock::new(); + *MB.get_or_init(|| { + std::env::var("VICTORY_UPLOAD_POOL_MB").ok().and_then(|v| v.trim().parse().ok()).unwrap_or(1024) + }) << 20 +} + +fn take_upload(shared: bool, class: u64) -> Option { + UPLOAD_SPARE[usize::from(shared)].lock().unwrap_or_else(|e| e.into_inner()).take(class) +} + +/// The copy sources of a list whose submissions have run. +fn give_uploads(pooled: Vec<(Chunk, u64, bool)>) { + if pooled.is_empty() { + return; + } + let most = upload_pool_most(); + let mut freed = Vec::new(); + for (chunk, class, shared) in pooled { + if let Some(chunk) = UPLOAD_SPARE[usize::from(shared)].lock().unwrap_or_else(|e| e.into_inner()).give(chunk, class, most) { + freed.push(chunk); + } + } + // Freed outside the lock. + drop(freed); +} + +/// Under memory pressure: the kept copy sources down to [`SPARE_MOST`] of +/// each kind, the ones given last kept, while plenty of the PC's memory is +/// free ([`keeps_under_pressure`]); else all of them. +fn trim_spare_uploads() { + let most = if keeps_under_pressure() { SPARE_MOST } else { 0 }; + let mut freed = Vec::new(); + for spare in &UPLOAD_SPARE { + let mut spare = spare.lock().unwrap_or_else(|e| e.into_inner()); + while spare.bytes > most && !spare.kept.is_empty() { + let (chunk, size) = spare.kept.remove(0); + spare.bytes -= size; + freed.push(chunk); + } + spare.low = spare.low.min(spare.bytes); + } + drop(freed); +} + +/// Whether spares are kept, down to [`SPARE_MOST`], under memory pressure: +/// with 8 GiB or more of the PC's memory free. The pressure is then the +/// commit limit's, not the memory's: with 64 GB and a system-managed page +/// file the commit headroom stays under 4 GiB while the title runs, with +/// 20 GB of memory free, and the spares freed every second were made again +/// at once (RTX 4090: 12,925 chunks, 13.5 GB and 7.7 s of waits in a +/// 5-minute session). With memory short they all go, as before: on a +/// 16 GB PC keeping them cost some 40 ms a frame of waits for flights +/// (PR #57). +fn keeps_under_pressure() -> bool { + crate::residency::physical_free() >= 8 << 30 +} + +/// Drops spare scratch chunks kept idle when host memory is pressured (down +/// to [`SPARE_MOST`] with [`keeps_under_pressure`]). fn trim_spare_chunks() { let freed: Vec<(Chunk, u64)> = { let mut spare = SPARE.lock().unwrap_or_else(|e| e.into_inner()); if spare.kept.is_empty() { return; } - spare.bytes = 0; - spare.low = 0; - std::mem::take(&mut spare.kept) + if !keeps_under_pressure() { + spare.bytes = 0; + spare.low = 0; + std::mem::take(&mut spare.kept) + } else { + // The chunks given last are kept. + let mut freed = Vec::new(); + while spare.bytes > SPARE_MOST && !spare.kept.is_empty() { + let (chunk, size) = spare.kept.remove(0); + spare.bytes -= size; + freed.push((chunk, size)); + } + spare.low = spare.low.min(spare.bytes); + freed + } }; let count = freed.len() as u64; let bytes: u64 = freed.iter().map(|(_, s)| *s).sum(); @@ -2687,6 +2768,10 @@ struct Deferred { /// Destination, source, bytes. copies: Vec, uploads: Vec, + /// Copy sources from [`HostList::upload`]'s pool: the buffer, its size + /// class, whether it is a shared buffer. Given back to the pool when the + /// list is reset. + pooled: Vec<(Chunk, u64, bool)>, /// Standalone upload footprints kept through this list's fence (movie planes). plane_uploads: Vec<(Chunk, u64)>, /// Title textures whose upload the list's prelude records if the check at @@ -2757,6 +2842,7 @@ impl Deferred { impl Drop for Deferred { fn drop(&mut self) { give_chunks(std::mem::take(&mut self.plane_uploads)); + give_uploads(std::mem::take(&mut self.pooled)); ALIVE[6].fetch_sub(self.uploads.len() as u64, Ordering::Relaxed); ALIVE[7].fetch_sub(self.upload_bytes, Ordering::Relaxed); } @@ -7692,13 +7778,28 @@ impl HostList { self.deferred().writes.push((dst as usize, what, bytes)); } - /// A mapped upload buffer of `size` bytes, alive until the list is - /// reset. - /// - /// shortcut: one buffer per copy; pool them once uploads are frequent. + /// A mapped upload buffer of at least `size` bytes, alive until the list + /// is reset: from a pool by size class ([`UPLOAD_SPARE`]), so that it may + /// hold what an earlier list wrote. Callers write or zero the bytes they + /// use and copy by offset, never the whole buffer. pub(crate) fn upload(&self, host: &Host, size: u64) -> Option<(ID3D12Resource, *mut u8)> { // With [`card_steps`] the card may be the one that fills it. - let (buffer, data) = if card_steps::stage() > 1 { host.shared_buffer(size)? } else { host.upload_buffer(size)? }; + let shared = card_steps::stage() > 1; + if upload_pool_most() > 0 { + let class = size.max(64 << 10).next_power_of_two(); + let chunk = match take_upload(shared, class) { + Some(chunk) => chunk, + None => { + let (buffer, data) = if shared { host.shared_buffer(class)? } else { host.upload_buffer(class)? }; + name(&buffer, || format!("list upload, pooled {class:#x}")); + Chunk { buffer, data: data as usize } + } + }; + let out = (chunk.buffer.clone(), chunk.data as *mut u8); + self.deferred().pooled.push((chunk, class, shared)); + return Some(out); + } + let (buffer, data) = if shared { host.shared_buffer(size)? } else { host.upload_buffer(size)? }; name(&buffer, || format!("list upload {size:#x}")); self.deferred().hold_upload(buffer.clone(), size); Some((buffer, data)) diff --git a/crates/victory-gpu/src/residency.rs b/crates/victory-gpu/src/residency.rs index 0e701e5df7..9c462b34ec 100644 --- a/crates/victory-gpu/src/residency.rs +++ b/crates/victory-gpu/src/residency.rs @@ -135,6 +135,11 @@ pub(crate) fn headroom() -> Option { (bytes != u64::MAX).then_some(bytes) } +/// The PC's memory free now, in bytes (0 if Windows does not say). +pub(crate) fn physical_free() -> u64 { + memory_status().map_or(0, |s| s.ullAvailPhys) +} + pub(crate) fn pressured() -> bool { PRESSURED.load(Ordering::Relaxed) }