Memory by address: no arena storm when the low range is full; reserve its room at the start
With the structure builder on (VICTORY_BVH_BUILDER=1), memory::ready asks for another arena at every build dispatch while the spares are low. Once upload::reserve could find no 64 MiB gap in [4 GiB, 2^37), nothing remembered it: every one of thousands of dispatches a frame searched the address range again under the MEMORY lock (the recording threads ~70 % in the kernel under VirtualAlloc2), frames took 7 to 32 s, and the title's own hang watchdog (the store to 0 at +0x23cf683) ended the run. - Memory::another: after a failure, none is tried again before the next frame; the reason is logged (no room, or its copy could not be made), and a reservation whose copy could not be made is given back. - upload::reserve_early, at host creation: 50 reservations of 64 MiB in the low range, with write watch, when the builder is on (VICTORY_LOW_EARLY_MB, 3200; 0 off). An arena commits one of them before looking for room. Separate pieces, not one 3.2 GB reservation: as one, the submissions' write-watch askings walked across all arenas in single runs, about 2 ms a frame more. RTX 4090, prison area, ray tracing on, frame generation off: - before: stuck at 19 arenas, ~280-650 pages a frame read as zeros, watchdog crash 1-3 minutes in; - after: 28 arenas, ~18 pages a frame once settled, no watchdog crash, 22-28 fps (30 cap) after the first minute. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
29d8dbf7d9
commit
30b979ee48
3 files changed
+118
-1
No files matched your search
@@ -984,6 +984,7 @@ fn create() -> windows::core::Result<Host> {
|
||||
.and_then(|factory| factory.EnumAdapterByLuid::<IDXGIAdapter3>(device.GetAdapterLuid()))
|
||||
.ok();
|
||||
tracing::info!("host D3D12 device ready");
|
||||
crate::upload::reserve_early();
|
||||
Ok(Host {
|
||||
device,
|
||||
queue,
|
||||
|
||||
@@ -389,11 +389,46 @@ impl Memory {
|
||||
}
|
||||
|
||||
/// Another arena's pages.
|
||||
///
|
||||
/// After one that could not be made, no other is tried before
|
||||
/// the next frame. `ready` asks for one at every dispatch of the builder
|
||||
/// while the spares are low (thousands a frame); once the reservation
|
||||
/// failed every ask searched the address range again under this lock,
|
||||
/// and frames took 30 s until the title's watchdog ended the run (RTX
|
||||
/// 4090, 11-27: 19 arenas for minutes, the recording threads 70 % in
|
||||
/// the kernel under `upload::reserve`). A reservation whose copy could
|
||||
/// not be made is given back, not left behind.
|
||||
fn another(&mut self) -> bool {
|
||||
use std::sync::atomic::{AtomicU64, Ordering::Relaxed};
|
||||
static FAILED: AtomicU64 = AtomicU64::new(u64::MAX);
|
||||
static WHY: [AtomicU64; 2] = [const { AtomicU64::new(0) }; 2];
|
||||
let frame = crate::frame::current();
|
||||
if FAILED.load(Relaxed) == frame {
|
||||
return false;
|
||||
}
|
||||
let arenas = self.arenas.len();
|
||||
let failed = |why: usize| {
|
||||
FAILED.store(frame, Relaxed);
|
||||
let n = WHY[why].fetch_add(1, Relaxed) + 1;
|
||||
if n.is_power_of_two() {
|
||||
tracing::warn!(
|
||||
arenas,
|
||||
times = n,
|
||||
"title memory by address: no other arena ({}); none tried again this frame",
|
||||
["the address range has no room for one", "its copy could not be made"][why]
|
||||
);
|
||||
}
|
||||
};
|
||||
let Some(at) = crate::upload::reserve(ARENA as usize) else {
|
||||
failed(0);
|
||||
return false;
|
||||
};
|
||||
let Some(n) = crate::shadow::by_address(at as u64, ARENA).and_then(|s| self.number(s)) else {
|
||||
failed(1);
|
||||
// SAFETY: the reservation just made, which nothing else knows of.
|
||||
unsafe {
|
||||
windows_sys::Win32::System::Memory::VirtualFree(at as *mut _, 0, windows_sys::Win32::System::Memory::MEM_RELEASE)
|
||||
};
|
||||
return false;
|
||||
};
|
||||
self.arenas.push((n, at));
|
||||
|
||||
@@ -54,10 +54,91 @@ fn gap(taken: &[bool], n: usize) -> Option<usize> {
|
||||
None
|
||||
}
|
||||
|
||||
/// Pieces of address space in [4 GiB, 2^37), [`EARLY_PIECE`] each, reserved
|
||||
/// at the start (`VICTORY_LOW_EARLY_MB`, 3,200 with the structure
|
||||
/// builder, else 0). [`reserve`] commits a whole one for a request of that
|
||||
/// size (an arena of memory by address) before it looks for room.
|
||||
///
|
||||
/// By the time the builder runs, the title's mappings and ours fill that
|
||||
/// range: no 64 MiB gap was left after 19 of the builder's arenas (RTX 4090,
|
||||
/// 11-27), and the pages it could not hold read zeros (broken reflections,
|
||||
/// flicker). Reserved before the title spreads, the range costs no memory
|
||||
/// until a piece is committed. Each piece is a reservation of its own: as
|
||||
/// one reservation of 3.2 GB the submissions' write-watch askings ran
|
||||
/// across the arenas in single long walks, 2 ms a frame more.
|
||||
static EARLY: Mutex<Vec<usize>> = Mutex::new(Vec::new());
|
||||
/// The size of an [`EARLY`] piece: `memory.rs`'s `ARENA`.
|
||||
const EARLY_PIECE: usize = 64 << 20;
|
||||
|
||||
/// Reserves [`EARLY`]; once, as early as the host is made.
|
||||
pub(crate) fn reserve_early() {
|
||||
use windows_sys::Win32::System::Memory::{
|
||||
MEM_ADDRESS_REQUIREMENTS, MEM_EXTENDED_PARAMETER, MEM_RESERVE, MemExtendedParameterAddressRequirements, PAGE_READWRITE,
|
||||
VirtualAlloc2,
|
||||
};
|
||||
const MEM_WRITE_WATCH: u32 = 0x20_0000;
|
||||
static ONCE: std::sync::Once = std::sync::Once::new();
|
||||
ONCE.call_once(|| {
|
||||
let builder = std::env::var_os("VICTORY_BVH_BUILDER").is_some_and(|v| v == "1");
|
||||
let mb = std::env::var("VICTORY_LOW_EARLY_MB")
|
||||
.ok()
|
||||
.and_then(|v| v.trim().parse::<usize>().ok())
|
||||
.unwrap_or(if builder { 3200 } else { 0 });
|
||||
let mut early = EARLY.lock().unwrap_or_else(|e| e.into_inner());
|
||||
for _ in 0..(mb << 20) / EARLY_PIECE {
|
||||
let mut requirements = MEM_ADDRESS_REQUIREMENTS {
|
||||
LowestStartingAddress: (1usize << 32) as *mut _,
|
||||
HighestEndingAddress: ((1usize << 37) - 1) as *mut _,
|
||||
Alignment: 0,
|
||||
};
|
||||
let mut parameter = MEM_EXTENDED_PARAMETER::default();
|
||||
parameter.Anonymous1._bitfield = MemExtendedParameterAddressRequirements as u64;
|
||||
parameter.Anonymous2.Pointer = (&raw mut requirements).cast();
|
||||
// SAFETY: a fresh reservation; the parameter points at
|
||||
// `requirements`, which outlives the call.
|
||||
let at = unsafe {
|
||||
VirtualAlloc2(
|
||||
std::ptr::null_mut(),
|
||||
std::ptr::null(),
|
||||
EARLY_PIECE,
|
||||
MEM_RESERVE | MEM_WRITE_WATCH,
|
||||
PAGE_READWRITE,
|
||||
&mut parameter,
|
||||
1,
|
||||
)
|
||||
} as usize;
|
||||
if at == 0 {
|
||||
break;
|
||||
}
|
||||
early.push(at);
|
||||
}
|
||||
// The lowest first.
|
||||
early.sort_unstable_by(|a, b| b.cmp(a));
|
||||
if mb > 0 {
|
||||
tracing::info!(mb, pieces = early.len(), "low memory reserved early (VICTORY_LOW_EARLY_MB)");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// A whole [`EARLY`] piece, committed, for a request of its size.
|
||||
fn from_early(len: usize) -> Option<usize> {
|
||||
use windows_sys::Win32::System::Memory::{MEM_COMMIT, PAGE_READWRITE, VirtualAlloc};
|
||||
if len != EARLY_PIECE {
|
||||
return None;
|
||||
}
|
||||
let at = EARLY.lock().unwrap_or_else(|e| e.into_inner()).pop()?;
|
||||
// SAFETY: a reservation of our own, not committed before.
|
||||
let done = unsafe { VirtualAlloc(at as *const _, len, MEM_COMMIT, PAGE_READWRITE) } as usize;
|
||||
(done == at).then_some(at)
|
||||
}
|
||||
|
||||
/// Fresh zeroed memory in [4 GiB, 2^37) (where the directory of memory by
|
||||
/// address reaches, `MEMORY_PAGES`), write-watched as the title's own
|
||||
/// pools are (copies then take only written pages).
|
||||
/// pools are (copies then take only written pages). From [`EARLY`] first.
|
||||
pub(crate) fn reserve(len: usize) -> Option<usize> {
|
||||
if let Some(at) = from_early(len) {
|
||||
return Some(at);
|
||||
}
|
||||
use windows_sys::Win32::System::Memory::{
|
||||
MEM_ADDRESS_REQUIREMENTS, MEM_COMMIT, MEM_EXTENDED_PARAMETER, MEM_RESERVE, MemExtendedParameterAddressRequirements,
|
||||
PAGE_READWRITE, VirtualAlloc2,
|
||||
|
||||
Reference in new issue
Block a user