Repository navigation
Description
Following on from d510ee4 ("the dips while moving, measured"): a kernel trace says what the title's map and unmap calls wait for, and a small change removes the wait without changing how pages are mapped. Patch below; it applies cleanly to main at d510ee4.
What the calls wait for
Setup: RTX 4070 Ti, Ryzen 7 5800X, Windows 11 26200, --console series-x, the first minutes of the prologue. An xperf trace (PROC_THREAD+LOADER+CSWITCH+DISPATCHER, stacks on CSwitch and ReadyThread) taken while the title streams, read for 4 seconds:
- 806 of the title's map/unmap calls blocked for 1 ms or more, 2.6 s in total.
- Where they stood:
ExfAcquirePushLockExclusiveExunderMiPostInsertVad < MiInsertVad < MiMapViewOfDataSection(1.06 s) and underMiManageSubsectionView < MiDeleteVad < MiUnmapVad(1.00 s). So mostly not the address space lock itself but the section's. - Who released them: for 2.06 s of the 2.6 s, one thread of the System process, from
ExfReleasePushLockShared < MiTrimSection < MiTrimSharedPage < MiTradePage < MiClaimPhysicalRun < MiFindContiguousPagesEx < MiRebuildLargePage < MiRebuildLargePages < ExpWorkerThread. - That worker was on a processor 93 % of the time, nearly always inside
MiViewMayContainPage < MiTrimSection.
So: Windows' large-page rebuild worker trades the section's pages one by one. For each page it looks through the section's views and holds the section meanwhile. XMemAllocatePhysicalPages makes one section per request (the title asks for 6.1 GiB at once) and the title maps it a 64 KiB view at a time, so that one section has views by the hundred thousand: every trade is slow and every trade holds all of the title's physical memory.
Ruled out on the way, by measurement: the kernel reads of title memory (VICTORY_COPY_PLAIN=1 changed nothing), write-watch askings (none over 1 ms), and crowding of the title's pinned threads (VICTORY_SPREAD=1 with all processors spare changed nothing).
The change
Sections of 2 MiB instead of one per request (VICTORY_SECTION_MB, 0 = as before). A trade then looks through a few dozen views and holds one small section. Mapping is otherwise as it was (still one view per run of consecutive pages of one section, so map_alias still maps page by page); Views::sections becomes a map because there are now thousands.
Measured
First 90 s after reaching the prison, map/unmap system calls of 1 ms or more:
| calls | time | |
|---|---|---|
| one section per request | about 11,000 | about 60 s |
| 2 MiB sections | 31 | 55 ms |
Not shown
- Frame times in that phase did not change here (same share of frames over 250 ms; settled frame 128 ms either way). In my runs the long frames of that phase are the submit thread's (140 to 190 MB copied and up to 259 texture uploads a frame), so the mapping calls were not what the frame waited for. d510ee4 measures them on the render thread's way in cutscenes, so it may show more there.
- Picture: no errors in the log and the prologue looks as before, but I have not checked faces through a whole cutscene, which is where the longer views broke.
- Only NVIDIA, only Series X mode, only Windows (not Wine).
Patch
diff --git a/crates/victory-gpu/src/inplace.rs b/crates/victory-gpu/src/inplace.rs
index 0173b97..e660d27 100644
--- a/crates/victory-gpu/src/inplace.rs
+++ b/crates/victory-gpu/src/inplace.rs
@@ -274,11 +274,11 @@ struct Views {
/// (`crate::watch` guards them all).
pages: BTreeMap<(usize, u64), Vec<usize>>,
/// Sections by handle: bytes, and whether made committed.
- sections: Vec<(usize, u64, bool)>,
+ sections: BTreeMap<usize, (u64, bool)>,
}
static VIEWS: Mutex<Views> =
- Mutex::new(Views { views: BTreeMap::new(), pages: BTreeMap::new(), sections: Vec::new() });
+ Mutex::new(Views { views: BTreeMap::new(), pages: BTreeMap::new(), sections: BTreeMap::new() });
fn views() -> std::sync::MutexGuard<'static, Views> {
VIEWS.lock().unwrap_or_else(|e| e.into_inner())
@@ -289,8 +289,7 @@ fn views() -> std::sync::MutexGuard<'static, Views> {
pub fn title_memory_section(section: usize, bytes: u64, committed: bool) {
{
let mut v = views();
- v.sections.retain(|s| s.0 != section);
- v.sections.push((section, bytes, committed));
+ v.sections.insert(section, (bytes, committed));
}
// A view of our own made on an earlier section that had this handle's
// number shows that section's pages, not this one's: it is forgotten
@@ -518,7 +517,7 @@ pub(crate) fn write_own(at: usize, bytes: &[u8]) -> Option<Vec<usize>> {
if shown.len() > crate::watch::MANY_VIEWS {
return None;
}
- let section_bytes = v.sections.iter().find(|s| s.0 == section)?.1;
+ let section_bytes = v.sections.get(§ion)?.0;
(section, offset, shown, section_bytes)
};
if offset + bytes.len() as u64 > section_bytes {
@@ -540,11 +539,18 @@ pub(crate) fn write_own(at: usize, bytes: &[u8]) -> Option<Vec<usize>> {
);
return None;
}
- tracing::info!(
- section,
- mb = section_bytes >> 20,
- "a view of our own on a section of title pages, for what shaders wrote (VICTORY_BACK_OWN_VIEW=1)"
- );
+ // Sections are small and many (the runtime's `VICTORY_SECTION_MB`):
+ // the first few are said, the rest counted.
+ static SAID: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0);
+ let said = SAID.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
+ if said < 4 || said.is_power_of_two() {
+ tracing::info!(
+ section,
+ mb = section_bytes >> 20,
+ so_far = said + 1,
+ "a view of our own on a section of title pages, for what shaders wrote (VICTORY_BACK_OWN_VIEW=1)"
+ );
+ }
Some(Own {
base: at.Value as usize,
bytes: section_bytes,
@@ -1162,7 +1168,7 @@ fn census_of(spans: &[(u64, u64, bool)]) -> Vec<String> {
bytes >> 20,
big >> 20
));
- for &(_, bytes, committed) in &v.sections {
+ for &(bytes, committed) in v.sections.values() {
lines.push(format!(
"census, a section of {} MB, {}",
bytes >> 20,
diff --git a/crates/victory-hle/src/modules/xmem.rs b/crates/victory-hle/src/modules/xmem.rs
index e9d0dd8..0450d8b 100644
--- a/crates/victory-hle/src/modules/xmem.rs
+++ b/crates/victory-hle/src/modules/xmem.rs
@@ -412,59 +412,103 @@ pub extern "system" fn XMemAllocatePhysicalPages(
}
// SAFETY: checked non-null.
let n = unsafe { *count };
- let Some(size) = n.checked_mul(PAGE).filter(|&s| s > 0) else {
+ let Some(_) = n.checked_mul(PAGE).filter(|&s| s > 0) else {
return 0;
};
- // A pagefile-backed section of `size` bytes, its pages committed when
- // first mapped or written (`commit`): the title asks for 6.1
- // GiB at once, which a busy host often cannot commit (error 1450).
- // SAFETY: plain section creation.
- let handle = unsafe {
- CreateFileMappingW(
- INVALID_HANDLE_VALUE,
- std::ptr::null(),
- // Under Wine a committed section is a sparse file, free until
- // touched, and committing a reserved one page by page costs a
- // wineserver request each that scans every range committed so
- // far: commit it whole there.
- PAGE_READWRITE | if on_wine() { SEC_COMMIT } else { SEC_RESERVE },
- (size as u64 >> 32) as u32,
- size as u32,
- std::ptr::null(),
- )
- };
- if handle.is_null() {
- let error = std::io::Error::last_os_error();
- tracing::warn!(n, %error, "XMemAllocatePhysicalPages failed");
- // SAFETY: checked non-null.
- unsafe { count.write(0) };
- return 0;
+ // Pagefile-backed sections of `size` bytes together, their pages
+ // committed when first mapped or written (`commit`): the title asks for
+ // 6.1 GiB at once, which a busy host often cannot commit (error 1450).
+ //
+ // Not one section for the whole request but one per `section_pages()`
+ // pages (2 MiB; `VICTORY_SECTION_MB=0`: one section, as before). Why:
+ // the title maps these pages a 64 KiB view at a time, so one section
+ // came to have views by the hundred thousand. Windows' worker that
+ // gathers physical memory into large pages (MiRebuildLargePages) trades
+ // such a section's pages one by one, looks through all of the section's
+ // views for each (MiViewMayContainPage) and holds the section meanwhile;
+ // every map and unmap of any of its pages waits for it. A kernel trace
+ // while the title streamed (RTX 4070 Ti, Ryzen 7 5800X, 2026-11-25): the
+ // worker 93 % of a core in exactly that, 806 of the title's map and
+ // unmap calls blocked for 2.6 s out of 4, 2.06 s of it released by that
+ // worker; frames of half a second and more. With small sections a trade
+ // looks through a few dozen views and holds only its own section.
+ let per = section_pages().unwrap_or(n).max(1);
+ let mut sections: Vec<(Arc<Section>, usize)> = Vec::with_capacity(n.div_ceil(per));
+ let mut left = n;
+ while left > 0 {
+ let pages_here = left.min(per);
+ let bytes = pages_here * PAGE;
+ // SAFETY: plain section creation.
+ let handle = unsafe {
+ CreateFileMappingW(
+ INVALID_HANDLE_VALUE,
+ std::ptr::null(),
+ // Under Wine a committed section is a sparse file, free until
+ // touched, and committing a reserved one page by page costs a
+ // wineserver request each that scans every range committed so
+ // far: commit it whole there.
+ PAGE_READWRITE | if on_wine() { SEC_COMMIT } else { SEC_RESERVE },
+ (bytes as u64 >> 32) as u32,
+ bytes as u32,
+ std::ptr::null(),
+ )
+ };
+ if handle.is_null() {
+ let error = std::io::Error::last_os_error();
+ tracing::warn!(n, %error, "XMemAllocatePhysicalPages failed");
+ // All or nothing: the sections made so far close as they drop.
+ // SAFETY: checked non-null.
+ unsafe { count.write(0) };
+ return 0;
+ }
+ // For the GPU side's in-place census: only a committed section can
+ // be opened as a host heap.
+ victory_gpu::title_memory_section(handle as usize, bytes as u64, on_wine());
+ sections.push((Arc::new(Section(handle)), pages_here));
+ left -= pages_here;
}
tracing::debug!(
n,
+ sections = sections.len(),
flags = format_args!("{flags:#x}"),
"XMemAllocatePhysicalPages"
);
- // For the GPU side's in-place census: only a committed section can be
- // opened as a host heap.
- victory_gpu::title_memory_section(handle as usize, size as u64, on_wine());
- let section = Arc::new(Section(handle));
let mut physical = physical();
let first = physical.next_page;
physical.next_page += n * FRAMES;
- for index in 0..n {
- let slice = Slice {
- section: section.clone(),
- index,
- };
- let number = first + index * FRAMES;
- physical.pages.insert(number, slice);
- // SAFETY: the title passes room for `n` page numbers.
- unsafe { pages.add(index).write(number) };
+ let mut k = 0;
+ for (section, pages_here) in sections {
+ for index in 0..pages_here {
+ let slice = Slice {
+ section: section.clone(),
+ index,
+ };
+ let number = first + k * FRAMES;
+ physical.pages.insert(number, slice);
+ // SAFETY: the title passes room for `n` page numbers.
+ unsafe { pages.add(k).write(number) };
+ k += 1;
+ }
}
1
}
+/// How many 64 KiB pages a section of physical pages holds at most
+/// (`VICTORY_SECTION_MB`, 2 by default; 0: `None`, one section for each
+/// request however large).
+fn section_pages() -> Option<usize> {
+ static PAGES: std::sync::OnceLock<Option<usize>> = std::sync::OnceLock::new();
+ *PAGES.get_or_init(|| {
+ let mb = std::env::var("VICTORY_SECTION_MB").ok().and_then(|v| v.parse::<usize>().ok()).unwrap_or(2);
+ let pages = (mb << 20) / PAGE;
+ tracing::info!(
+ "physical pages: {} (VICTORY_SECTION_MB)",
+ if pages == 0 { "one section for each request".to_string() } else { format!("sections of {mb} MiB") }
+ );
+ (pages > 0).then_some(pages)
+ })
+}
+
/// Page numbers count 4 KiB frames, so a 64 KiB page's is a multiple of
/// 16: the title keeps them as 21-bit `number >> 4` and rebuilds
/// them with `<< 4` (128 GiB of pages at most).
Activity
HLVM04 commentedon Nov 25, 2026
A data point for two of the "not shown" items: Series S mode, and frame times. RTX 5070 Ti, Ryzen 7 9800X3D, Windows 11 build 26200, first gameplay scene, on our own stack (an older fork main plus several open PRs and local changes).
The patch from this issue, applied alone with the default
VICTORY_SECTION_MB=2:So here the gain is mostly in the long frames, which fits the dips you describe. That's one scene and few runs, and I didn't take a kernel trace, so I can't say how many map/unmap calls still block. On top of it, #868's
VICTORY_SKIP_WC_BACK=1gave a median of 71–72 ms with no crash or device loss in 2 runs of 200 s.Edit (2026-11-26):
VICTORY_SKIP_WC_BACK=1turned out not to be free on our stack: the room's lamp light goes missing and rooms render dark (our earlier runs were judged only by the character). The section-patch numbers above were taken without it.Exopus-0x65f7ad0 commentedon Nov 25, 2026
Confirmed on a third machine, and in the opening cutscene, where it pays the most: RTX 5060 Ti 8 GB, Ryzen 7 8700F, 16 GB RAM, Windows 11, Series S. Tree: fork
maind510ee4 plus a stack of fork PRs (#868, #1031, #1063 among them). I applied your patch unchanged, with the defaultVICTORY_SECTION_MB=2.I summed the frame log's
remapping:field over the long frames from the first 3D frame to the first gameplay frame:What remains in the cutscene's long frames is mostly the submit thread's copies (~11 GB over the cutscene) and page faults. No new defect in the picture: faces through the bar and prison shots look as they did before. One run with the patch so far, so the fps figure is a first reading. The remapping drop, though, is far outside the run-to-run noise.
🤖 Generated with Claude Code
Physical page allocations come from sections of up to 2 MiB (VICTORY_…
KainanP commentedon Nov 26, 2026
A synthetic reproduction of this stall, which needs no game, with numbers on an i7-14650HX: #1274 (comment below the PR). 4 GiB of physical pages in one section, remapped page by page while another thread makes and frees memory, had 1,386 remaps of 1 ms or more in 10 s. With 2 MiB sections that drops to 28, and throughput goes from 6,644 to 50,607 remaps/s (median of 5 rounds). The same change is also proposed for upstream (Marvisak/VIctory#3177) and AnyXboxX (LooperSalty/AnyXboxX#352), with credit to you for the idea and the first patch.
AI use: Claude Code (Claude Opus 5.5) wrote the benchmark and this note.
perf(victory): physical pages from 2 MiB sections (VICTORY_SECTION_MB…