Skip to content
Merged
Show file tree
Hide file tree
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
119 changes: 110 additions & 9 deletions crates/victory-gpu/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down Expand Up @@ -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<Spare<Chunk>>; 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<u64> = 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<Chunk> {
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();
Expand Down Expand Up @@ -2687,6 +2768,10 @@ struct Deferred {
/// Destination, source, bytes.
copies: Vec<Step>,
uploads: Vec<ID3D12Resource>,
/// 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
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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))
Expand Down
5 changes: 5 additions & 0 deletions crates/victory-gpu/src/residency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ pub(crate) fn headroom() -> Option<u64> {
(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)
}
Expand Down
Loading