Skip to content

fix(hle): isolate per-worker DirectStorage file handles and keep 64 KiB xmem views independent - #400

Open
gdemontalivet wants to merge 2 commits into
pragyaangaur:mainfrom
gdemontalivet:fix/macos-dstorage-handles-xmem-runs
Open

gdemontalivet wants to merge 2 commits into
pragyaangaur:mainfrom
gdemontalivet:fix/macos-dstorage-handles-xmem-runs

Conversation

@gdemontalivet

Copy link
Copy Markdown
Contributor

Two concurrency bugs and two xmem mapping bottlenecks show up when streaming workers saturate DirectStorage and physical-page remapping under Wine on macOS:

  1. Shared synchronous file cursor across DirectStorage workers: Concurrent DirectStorage workers share a single synchronous std::fs::File handle per open archive and issue FileExt::seek_read calls in parallel. Under Wine on macOS, concurrent synchronous reads on the same file handle race on the kernel file object's shared position and return bytes from another worker's offset, causing zlib inflation failures (0x8007000d, E_INVALIDARG).
  2. Tearing down sibling 64 KiB pages during partial XMemMapPhysicalPages remaps: On Xbox hardware each 64 KiB page has an independent PTE, whereas XMemMapPhysicalPages previously merged consecutive pages into one multi-page MapViewOfFile3 view and called unmap(base, n * PAGE) across the whole call whenever any single page differed. A later partial remap or unmap of one page inside a multi-page view had to UnmapViewOfFile2 the entire host view before remapping its outer halves—briefly unmapping sibling 64 KiB pages that streaming threads were actively reading (0xc0000005).
  3. Global xmem lock contention and redundant placeholder splits/commits: DirectStorage page-destination lookups contended with XMemMapPhysicalPages on a single global mutex, and remapping an unmapped 64 KiB slot re-split its placeholder and re-committed already-committed section pages every time.

This series resolves these issues in two commits:

  1. Per-worker DirectStorage file handles and decoupled xmem page lookups (dstorage.rs, xmem.rs):

    • Assign each DirectStorage worker thread a dedicated 1-to-1 handle slot (0..workers, avoiding thread-ID modulo collisions) backed by an independent OS file description opened via CreateFileW on the recorded path (including mounted virtual drives) or ReOpenFile, with VICTORY_DSTORAGE_HANDLES=0 opt-out.
    • Guard every handle slot (and the single-handle fallback) with its own Mutex<std::fs::File> so non-worker callers or fallback handles never race seek_read cursors under Wine.
    • Split xmem's physical-page table (PAGES) and reverse slice-to-address index (SHOWN) into dedicated RwLocks separate from PHYSICAL so DirectStorage page_window / write_physical lookups never block behind host MapViewOfFile3 / UnmapViewOfFile2 syscalls.
  2. Independent 64 KiB views, per-page unchanged skip, and lean placeholder hole reuse (xmem.rs, docs/macos.md):

    • Keep each 64 KiB page as its own independent MapViewOfFile3 view by default and skip individual unchanged pages (shows(...)) in XMemMapPhysicalPages and map_alias instead of unmapping across n * PAGE, so remapping one page never unmaps sibling pages being read on other threads.
    • VICTORY_MAP_LEAN (on by default, =0 opt-out): track exact-size unmapped placeholder holes (Physical::holes) and already-committed section pages (Section::committed) so remapping a 64 KiB slot replaces its placeholder directly without redundant VirtualFree splits or commit calls.
    • VICTORY_MAP_RUNS=1 (opt-in, default off): optional multi-page run coalescing for XMemMapPhysicalPages, XMemMapPhysicalPagesScatter, and map_alias, documented in docs/macos.md.

Synthetic Benchmarks & Concurrency Tests (CrossOver Preview + Wine on M5 Max)

  • Concurrent virtual-drive >5 GiB zlib reads (8 workers): 0 decompression errors across 8,064+ concurrent high-offset compressed reads; multi-worker cached read throughput rose from 11.2 GB/s (1 shared handle) to 24.1 GB/s (2.14x).
  • Concurrent sibling-page reader stress (partial_remap_keeps_sibling_pages_mapped_for_concurrent_readers): 1,200 multi-page and single-page remaps of middle page 1 while two reader threads continuously read pages 0 and 2 via read_volatile — 0 access violations and exactly 1 MapViewOfFile3 call per changed page.
  • Decoupled xmem page lookup under concurrent mapping churn: 3.17M page lookups/s (4.58 us/req) sustained concurrently alongside mapping threads.

Live M5 Max Verification (Combined Build on CrossOver Preview + D3DMetal)

  • In a combined warm-cache 720p opening-sequence run on Apple M5 Max (CrossOver Preview + D3DMetal, wave32=true), the runtime reached and sustained the 3D world through +8, +24, +48, +90 at 1.5–1.8 FPS (549.7–672.1 ms/frame on 1,021–1,258 draws/frame, 1.275 FPS overall across +8..+90) with 0 2.48 s allocation stalls, 7.2–9.0 ms/frame GUARDS lock hold time (down from 4,887 ms/frame before unlocking GUARDS), 149–154 MB/frame buffer copy volume, 0 DirectStorage decompression or access-violation failures, and 38/38 over-32 KiB aligned mesh/pixel pairs linked with 0 host mesh refusals.

Verification

  • CrossOver Preview with D3DMetal on an M5 Max, using an isolated test bottle.
  • Native macOS workspace test suite passed; Windows victory-hle xmem (21/21) and dstorage suites passed.
  • cargo xtask policy, git diff --check, and cargo clippy (-- -D warnings, with and without --features diagnostics) passed. All test inputs are synthetic.

…pendent

Track exact-size unmapped placeholder holes and already-committed section
pages (VICTORY_MAP_LEAN, on by default), skip unchanged pages individually
in XMemMapPhysicalPages and map_alias instead of unmapping sibling pages,
and keep each 64 KiB page in its own view unless VICTORY_MAP_RUNS=1 is set
so partial remaps never briefly unmap sibling pages under concurrent reads.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant