ソースを参照

xmem: commit physical-page sections whole under Wine

XMemAllocatePhysicalPages makes SEC_RESERVE sections and commits their
pages one view at a time. Under Wine each commit is a wineserver request
that scans every range committed so far, so wineserver ran at full load
while the title's threads waited on it, and loading hardly moved.

Under Wine (detected by ntdll's wine_get_version export) the sections
are now SEC_COMMIT and the per-view commit is skipped. Wine backs a
committed section with a sparse file, so untouched pages still cost
nothing. Windows keeps SEC_RESERVE, since a busy host may be unable to
commit the 6.1 GiB the title asks for in one go.

Tested: Linux (Wine 11.21), i7-12700K, RTX 4070. wineserver load drops
after the first seconds and the title gets to its loading screen,
drawing and dispatching.
meower 3 日 前
親
コミット
78b84047e5
1 ファイル変更、23 行追加、3 行削除
  1. 23 3
      crates/victory-hle/src/modules/xmem.rs

+ 23 - 3
crates/victory-hle/src/modules/xmem.rs

@@ -18,8 +18,8 @@ use windows_sys::Win32::System::Memory::{
     CreateFileMappingW, MEM_ADDRESS_REQUIREMENTS, MEM_COMMIT, MEM_EXTENDED_PARAMETER,
     MEM_PRESERVE_PLACEHOLDER, MEM_RELEASE, MEM_REPLACE_PLACEHOLDER, MEM_RESERVE,
     MEM_RESERVE_PLACEHOLDER, MEMORY_BASIC_INFORMATION, MEMORY_MAPPED_VIEW_ADDRESS, MapViewOfFile3,
-    MemExtendedParameterAddressRequirements, PAGE_NOACCESS, PAGE_READWRITE, SEC_RESERVE,
-    UnmapViewOfFile2, VirtualAlloc, VirtualAlloc2, VirtualFree, VirtualQueryEx,
+    MemExtendedParameterAddressRequirements, PAGE_NOACCESS, PAGE_READWRITE, SEC_COMMIT,
+    SEC_RESERVE, UnmapViewOfFile2, VirtualAlloc, VirtualAlloc2, VirtualFree, VirtualQueryEx,
 };
 use windows_sys::Win32::System::ProcessStatus::{K32GetProcessMemoryInfo, PROCESS_MEMORY_COUNTERS};
 use windows_sys::Win32::System::Threading::GetCurrentProcess;
@@ -29,6 +29,19 @@ use crate::{HleModule, hle_export};
 
 const MIB: usize = 1 << 20;
 
+/// Whether we run under Wine (its ntdll exports `wine_get_version`).
+pub(crate) fn on_wine() -> bool {
+    static WINE: LazyLock<bool> = LazyLock::new(|| {
+        use windows_sys::Win32::System::LibraryLoader::{GetModuleHandleA, GetProcAddress};
+        // SAFETY: plain lookups in ntdll, always loaded.
+        unsafe {
+            let ntdll = GetModuleHandleA(c"ntdll.dll".as_ptr().cast());
+            !ntdll.is_null() && GetProcAddress(ntdll, c"wine_get_version".as_ptr().cast()).is_some()
+        }
+    });
+    *WINE
+}
+
 /// Standard memory the title has handed to the system.
 static TRANSFERRED: AtomicUsize = AtomicUsize::new(0);
 
@@ -388,7 +401,11 @@ pub extern "system" fn XMemAllocatePhysicalPages(
         CreateFileMappingW(
             INVALID_HANDLE_VALUE,
             std::ptr::null(),
-            PAGE_READWRITE | SEC_RESERVE,
+            // 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(),
@@ -635,6 +652,9 @@ fn map(physical: &mut Physical, base: usize, len: usize, slice: Slice) -> bool {
 
 /// Commits the section pages a view shows (once per page, whichever view).
 fn commit(base: usize, len: usize) -> bool {
+    if on_wine() {
+        return true; // sections are committed whole there
+    }
     // SAFETY: `[base, base + len)` is a view of a reserved section.
     let done =
         !unsafe { VirtualAlloc(base as *const c_void, len, MEM_COMMIT, PAGE_READWRITE) }.is_null();