From 26f4331469fb1a1709e77607612f5d6176e59dca Mon Sep 17 00:00:00 2001 From: John Howard <49204095+Royalflamejlh@users.noreply.github.com> Date: Tue, 1 Dec 2026 10:15:08 -0700 Subject: [PATCH] D3D12.X: under Wine read a few resident pages of the title's in one read MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit read_committed_into reads the title's bytes with one ReadProcessMemory on Windows. Under Wine that read uses up a guard page in the range (#2844: title_bytes_leave_guard_pages_alone fails with it), so there every read asked VirtualQuery about each region first, even a 32-byte one. In a run in the title's world those queries were the first call site of all, about 17 µs each, mostly for 32-byte descriptor slots. Under Wine 11.21, with no other thread, a VirtualQuery in a 300 MB committed run took 2.5 µs and a working-set question about 1, 4 or 16 pages 0.33, 0.38 and 0.61 µs, both under Wine's virtual-memory lock. Under Wine a range of up to 16 pages that the working set holds as readable (gpu::shader::pages_readable, now crate-visible) is read in one copy_committed_region, which survives a concurrent decommit. A guard page is never in the working set, so a range with one is read region by region as before, and its guard stays. Windows is unchanged. Measured with two other changes (the superseded-target scan reading view heap slots in place, and the memory report reusing its commit sum) on a tree carrying other unmerged work, RTX 4060 Ti, Wine 11.21, 1 run each with the same options, the title in its world: 1.19 -> 3.70 frames a second, and this VirtualQuery call site went from 1,120 ms of thread time a second on average to out of the top list. What each change gives on its own was not measured. The 16 is pages_readable's own limit (SMALL_RANGE_PAGES, now shared): past it that function reads a byte of each page that is not resident, which under Wine would use up a guard page. Test: read the Wine way on any platform, a 32-byte read and a 16-page read of resident memory ask no region (red without the change, on Windows too); a 32-page read reads the same bytes. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/gpu/shader.rs | 8 ++-- crates/xcl/src/hle/d3d12x/title_textures.rs | 41 ++++++++++++++++++++- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/crates/xcl/src/gpu/shader.rs b/crates/xcl/src/gpu/shader.rs index 0d510f18d3..37fccdae0e 100644 --- a/crates/xcl/src/gpu/shader.rs +++ b/crates/xcl/src/gpu/shader.rs @@ -230,6 +230,9 @@ pub fn readable(p: usize, len: usize) -> bool { true } +/// Up to this many pages `pages_readable` reads no byte of a range: a page not resident is `None` +pub(crate) const SMALL_RANGE_PAGES: usize = 16; + /// Whether each page of `[p, end)` is readable, asked of the working set – the kernel answers for /// up to 512 pages in one call that touches none of them. `readable` asks this first because /// `VirtualQuery` sizes the whole region around an address by walking it: in a title's 2 GB block @@ -243,15 +246,14 @@ pub fn readable(p: usize, len: usize) -> bool { /// out, guard, reserved, no access). In a longer range such a page is read by the kernel (a /// byte of it): a committed one is then resident, as the caller's reads would make it, any other /// is not readable. -fn pages_readable(p: usize, end: usize) -> Option { +pub(crate) fn pages_readable(p: usize, end: usize) -> Option { use windows_sys::Win32::System::Diagnostics::Debug::ReadProcessMemory; use windows_sys::Win32::System::Memory::{PAGE_GUARD, PAGE_NOACCESS}; use windows_sys::Win32::System::ProcessStatus::{K32QueryWorkingSetEx, PSAPI_WORKING_SET_EX_INFORMATION}; use windows_sys::Win32::System::Threading::GetCurrentProcess; - const SMALL: usize = 16; const BATCH: usize = 512; let (first, last) = (p >> 12, (end - 1) >> 12); - let small = last - first < SMALL; + let small = last - first < SMALL_RANGE_PAGES; let mut ws: [PSAPI_WORKING_SET_EX_INFORMATION; BATCH] = unsafe { std::mem::zeroed() }; let mut at = first; while at <= last { diff --git a/crates/xcl/src/hle/d3d12x/title_textures.rs b/crates/xcl/src/hle/d3d12x/title_textures.rs index d809b1c2a2..a59e9b7ff0 100644 --- a/crates/xcl/src/hle/d3d12x/title_textures.rs +++ b/crates/xcl/src/hle/d3d12x/title_textures.rs @@ -3321,6 +3321,11 @@ pub(super) unsafe fn read_committed(at: usize, len: usize) -> Vec { /// `read_committed` of `out.len()` bytes at `at` into `out` (every byte of it written). pub(super) unsafe fn read_committed_into(at: usize, out: &mut [u8]) { + read_committed_into_as(at, out, crate::util::under_wine()) +} + +/// `read_committed_into` as it reads on Windows or, with `wine`, under Wine. +unsafe fn read_committed_into_as(at: usize, out: &mut [u8], wine: bool) { use windows_sys::Win32::System::Diagnostics::Debug::ReadProcessMemory; use windows_sys::Win32::System::Memory::{VirtualQuery, MEMORY_BASIC_INFORMATION, MEM_COMMIT}; use windows_sys::Win32::System::Threading::GetCurrentProcess; @@ -3337,12 +3342,17 @@ pub(super) unsafe fn read_committed_into(at: usize, out: &mut [u8]) { // of MiB – a 16 KiB texture cost ~120 µs at the start of a 64 MiB run, ~1.3 ms of 512 MiB. // `ReadProcessMemory` refuses a range with a page that is not committed, no-access or a guard // page (and leaves the guard in place: `title_bytes_leave_guard_pages_alone`). Under Wine - // that test fails with it (#2844), so there it is region by region. + // that test fails with it (#2844), so there it is one read of up to 16 pages the working set + // holds as readable (a guard page never is), else region by region. let mut read = 0usize; if len == 0 { return; } - if !crate::util::under_wine() && ReadProcessMemory(GetCurrentProcess(), at as *const c_void, out.as_mut_ptr() as *mut c_void, len, &mut read) != 0 { + if !wine && ReadProcessMemory(GetCurrentProcess(), at as *const c_void, out.as_mut_ptr() as *mut c_void, len, &mut read) != 0 { + return; + } + if wine && ((limit - 1) >> 12) - (at >> 12) < crate::gpu::shader::SMALL_RANGE_PAGES && crate::gpu::shader::pages_readable(at, limit) == Some(true) { + copy_committed_region(at, out.as_mut_ptr(), len); return; } // with holes: region by region (a refused `ReadProcessMemory` costs more than the walk; a @@ -3351,6 +3361,8 @@ pub(super) unsafe fn read_committed_into(at: usize, out: &mut [u8]) { let mut p = at; while p < limit { let mut m: MEMORY_BASIC_INFORMATION = std::mem::zeroed(); + #[cfg(test)] + tests::REGION_QUERIES.with(|n| n.set(n.get() + 1)); if VirtualQuery(p as *const c_void, &mut m, std::mem::size_of_val(&m)) == 0 || m.RegionSize == 0 { break; } @@ -4858,6 +4870,8 @@ pub(super) mod tests { thread_local! { /// the VirtualQuery calls `mirror_source` made on this thread pub(super) static QUERIES: std::cell::Cell = const { std::cell::Cell::new(0) }; + /// the VirtualQuery calls `read_committed_into` made on this thread + pub(super) static REGION_QUERIES: std::cell::Cell = const { std::cell::Cell::new(0) }; } /// One call of the queue's page-mapping method: `ranges` (kind, start, count) from GPU-view @@ -7200,6 +7214,29 @@ pub(super) mod tests { } } + #[test] + fn a_few_resident_pages_are_read_without_asking_their_regions() { + use windows_sys::Win32::System::Memory::{VirtualAlloc, VirtualFree, MEM_COMMIT, MEM_RELEASE, MEM_RESERVE, PAGE_READWRITE}; + let few = crate::gpu::shader::SMALL_RANGE_PAGES << 12; + unsafe { + let base = VirtualAlloc(std::ptr::null(), 2 * few, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE) as usize; + assert_ne!(base, 0); + std::ptr::write_bytes(base as *mut u8, 0x5A, 2 * few); + let under_wine = |at: usize, len: usize| { + let mut out = vec![0u8; len]; + read_committed_into_as(at, &mut out, true); + out + }; + let asked = || REGION_QUERIES.with(|n| n.get()); + let before = asked(); + assert_eq!(under_wine(base + 0x1F0, 32), vec![0x5A; 32]); + assert_eq!(under_wine(base + 0x1000, few), vec![0x5A; few]); + assert_eq!(asked(), before, "no region asked about"); + assert_eq!(under_wine(base, 2 * few), vec![0x5A; 2 * few]); + VirtualFree(base as *mut c_void, 0, MEM_RELEASE); + } + } + /// A guard or no-access page in the title's bytes reads as zeros, and the guard stays (a /// title's guard page is its own: reading must not use it up). #[test]