HLE: overlapped DirectStorage I/O and persistent xmem section views #234

Open
wbkepler wants to merge 2 commits from upstream-pr/234 into main
wbkepler commented 2026-11-24 01:40:48 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by wbkepler on 2026-11-24T01:40:48Z.


Summary

Eliminates DirectStorage kernel file-lock serialization and xmem per-chunk TLB shootdowns during asset streaming:

  • dstorage.rs:
    • Opens archive files with FILE_FLAG_OVERLAPPED (OwnedHandle) and issues concurrent positioned overlapped reads via thread-local manual-reset Win32 events (ReadFile + GetOverlappedResult loop handling partial reads and EOF/Win32 errors).
    • Sets worker threads to THREAD_PRIORITY_BELOW_NORMAL, reuses per-worker Scratch buffers (trimmed above 8 MiB), and replaces unconditional notify_all() wakeups with targeted notify_one() and waiter-aware notifications.
  • xmem.rs:
    • Keeps a persistent host-VA linear mapping (MapViewOfFile3 above 2^40 with MapViewOfFile fallback) and per-page AtomicU64 committed bitmap on each Section (preserving Wine whole-section SEC_COMMIT), eliminating transient MapViewOfFile/UnmapViewOfFile TLB shootdowns in page_window/write_physical.
    • Drops freed sections outside the PHYSICAL lock, coalesces contiguous runs in XMemMapPhysicalPagesScatter and map_alias (with placeholder-boundary fallback), and guards zero-length map/unmap operations.
  • Adds unit tests for concurrent overlapped reads on one handle, scratch reuse/trimming, persistent section commit/drop cleanup across bitmap words, and coalesced scatter/alias + zero-length safety.

Verification

  • cargo clippy --workspace --all-targets — passed (zero new warnings)
  • cargo clippy -p victory --features diagnostics --all-targets — passed
  • cargo test --workspace — passed (victory-hle: 90 passed, 1 ignored)
  • cargo xtask policy — passed (151 files ok)
<!-- greyfinch-sync:Marvisak/VIctory:issue:6572657721 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/234). Originally posted by [wbkepler](https://github.com/wbkepler) on 2026-11-24T01:40:48Z. --- ## Summary Eliminates DirectStorage kernel file-lock serialization and `xmem` per-chunk TLB shootdowns during asset streaming: - **`dstorage.rs`**: - Opens archive files with `FILE_FLAG_OVERLAPPED` (`OwnedHandle`) and issues concurrent positioned overlapped reads via thread-local manual-reset Win32 events (`ReadFile` + `GetOverlappedResult` loop handling partial reads and EOF/Win32 errors). - Sets worker threads to `THREAD_PRIORITY_BELOW_NORMAL`, reuses per-worker `Scratch` buffers (trimmed above 8 MiB), and replaces unconditional `notify_all()` wakeups with targeted `notify_one()` and waiter-aware notifications. - **`xmem.rs`**: - Keeps a persistent host-VA linear mapping (`MapViewOfFile3` above 2^40 with `MapViewOfFile` fallback) and per-page `AtomicU64` committed bitmap on each `Section` (preserving Wine whole-section `SEC_COMMIT`), eliminating transient `MapViewOfFile`/`UnmapViewOfFile` TLB shootdowns in `page_window`/`write_physical`. - Drops freed sections outside the `PHYSICAL` lock, coalesces contiguous runs in `XMemMapPhysicalPagesScatter` and `map_alias` (with placeholder-boundary fallback), and guards zero-length map/unmap operations. - Adds unit tests for concurrent overlapped reads on one handle, scratch reuse/trimming, persistent section commit/drop cleanup across bitmap words, and coalesced scatter/alias + zero-length safety. ## Verification - `cargo clippy --workspace --all-targets` — passed (zero new warnings) - `cargo clippy -p victory --features diagnostics --all-targets` — passed - `cargo test --workspace` — passed (`victory-hle`: 90 passed, 1 ignored) - `cargo xtask policy` — passed (151 files ok)
wbkepler commented 2026-11-24 06:55:25 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by wbkepler on 2026-11-24T03:22:53Z.


Found and fixed a regression in this PR (423d3f4).

Coalescing consecutive XMemMapPhysicalPagesScatter / map_alias pages into multi-page views made later single-page remaps unsafe. unmap cannot shrink a view, so remapping one page of a coalesced view unmapped the whole view and then mapped the remainder back. Neighbouring pages were briefly unmapped while other threads read them, which caused access violations on unmapped title memory during world loading (3/3 runs).

Scatter and alias now map one view per page again, as before this PR. The persistent section views, overlapped reads, single-lock scatter and run-coalesced unmaps are unchanged.

Tests: a new regression test checks that remapping one scattered page leaves its neighbours' views in place, and a one-view-per-page test replaces the coalescing test. Workspace clippy, tests and cargo xtask policy pass. A live run now loads into the world (~3k draws/frame, indirect draws > 0) with no exceptions.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7121034254 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/234#issuecomment-7121034254). Originally posted by [wbkepler](https://github.com/wbkepler) on 2026-11-24T03:22:53Z. --- Found and fixed a regression in this PR (423d3f4). Coalescing consecutive `XMemMapPhysicalPagesScatter` / `map_alias` pages into multi-page views made later single-page remaps unsafe. `unmap` cannot shrink a view, so remapping one page of a coalesced view unmapped the whole view and then mapped the remainder back. Neighbouring pages were briefly unmapped while other threads read them, which caused access violations on unmapped title memory during world loading (3/3 runs). Scatter and alias now map one view per page again, as before this PR. The persistent section views, overlapped reads, single-lock scatter and run-coalesced unmaps are unchanged. Tests: a new regression test checks that remapping one scattered page leaves its neighbours' views in place, and a one-view-per-page test replaces the coalescing test. Workspace clippy, tests and `cargo xtask policy` pass. A live run now loads into the world (~3k draws/frame, indirect draws > 0) with no exceptions.
wbkepler commented 2026-11-24 21:03:36 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by wbkepler on 2026-11-24T20:39:11Z.


Rebased onto current main (ffe310e) as e39fc9c; one small conflict in xmem.rs unmap (kept upstream's zero-length guard and comment with this PR's saturating end). Gate (clippy -D warnings, tests, policy) clean.

AI-assisted (Claude Code); checked on Windows 11 / RTX 5090.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7143873670 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/234#issuecomment-7143873670). Originally posted by [wbkepler](https://github.com/wbkepler) on 2026-11-24T20:39:11Z. --- Rebased onto current main (`ffe310e`) as `e39fc9c`; one small conflict in xmem.rs `unmap` (kept upstream's zero-length guard and comment with this PR's saturating end). Gate (clippy -D warnings, tests, policy) clean. _AI-assisted (Claude Code); checked on Windows 11 / RTX 5090._
Marvisak commented 2026-11-25 01:07:42 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T00:22:32Z.


Automated triage for the maintainers.

Needs rebase. This PR does not merge cleanly into current main (ac2525d). Conflicting files: crates/victory-hle/src/modules/dstorage.rs.

The content has not been reviewed yet; it will be once the branch is rebased onto main.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7148443594 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/234#issuecomment-7148443594). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T00:22:32Z. --- <!-- victory-triage v1 head=e39fc9cbe8b9275aeb87d27ccc1a56c8d462931a verdict=needs-rebase at=2026-11-25T00:13:20Z --> Automated triage for the maintainers. **Needs rebase.** This PR does not merge cleanly into current `main` (ac2525d). Conflicting files: `crates/victory-hle/src/modules/dstorage.rs`. The content has not been reviewed yet; it will be once the branch is rebased onto `main`. --- _Generated by [Claude Code](https://claude.ai/code)_
crashbashash commented 2026-11-25 01:07:43 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by crashbashash on 2026-11-25T00:23:02Z.


Data from Linux/Proton on the persistent-section-view part, in case it helps review. I measured the same idea in a minimal version (one view per section, mapped on first write, unmapped with the section; a page window holds its section), not this PR's code: main 70564ae plus #1849 and #994's Proton commits, RX 6800 XT, GE-Proton11-14, hands-free, 120 s in the world per run, 4 runs before and 3 after.

  • Under Wine every map and unmap is a wineserver request that walks every view of the process (server/mapping.c: is_valid_view_addr, find_mapped_view), and the game keeps 130,000+ views alive in the world. wineserver ran at 75 % of a core, 99.5 % of it in those two walks, about 0.5 ms per request. The per-read DirectStorage views were 27 % of its map/unmap requests.
  • With a view per section there were no per-read views left (25,509 per run before). The game allocates three sections at startup and frees none in the world, so a whole run maps one section view. wineserver map/unmap requests went from 1,432 to 1,176 per second, GPU tile mappings served from 405 to 493 per second (ranges 398–415 and 429–530), game CPU from 80 % to 103 % of a core. Frame time showed no change beyond run-to-run variation.
  • On your revert to one view per page: in the same runs none of 87,955 map_alias pages had a source consecutive with the one before, so coalescing could not have saved views there.

AI-assisted (Claude Code). No game data attached.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7148452456 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/234#issuecomment-7148452456). Originally posted by [crashbashash](https://github.com/crashbashash) on 2026-11-25T00:23:02Z. --- Data from Linux/Proton on the persistent-section-view part, in case it helps review. I measured the same idea in a minimal version (one view per section, mapped on first write, unmapped with the section; a page window holds its section), not this PR's code: `main` `70564ae` plus #1849 and #994's Proton commits, RX 6800 XT, GE-Proton11-14, hands-free, 120 s in the world per run, 4 runs before and 3 after. - Under Wine every map and unmap is a wineserver request that walks every view of the process (`server/mapping.c`: `is_valid_view_addr`, `find_mapped_view`), and the game keeps 130,000+ views alive in the world. wineserver ran at 75 % of a core, 99.5 % of it in those two walks, about 0.5 ms per request. The per-read DirectStorage views were 27 % of its map/unmap requests. - With a view per section there were no per-read views left (25,509 per run before). The game allocates three sections at startup and frees none in the world, so a whole run maps one section view. wineserver map/unmap requests went from 1,432 to 1,176 per second, GPU tile mappings served from 405 to 493 per second (ranges 398–415 and 429–530), game CPU from 80 % to 103 % of a core. Frame time showed no change beyond run-to-run variation. - On your revert to one view per page: in the same runs none of 87,955 `map_alias` pages had a source consecutive with the one before, so coalescing could not have saved views there. AI-assisted (Claude Code). No game data attached.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/234:upstream-pr/234'
git switch 'upstream-pr/234'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/234'
git switch 'upstream-pr/234'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/234'
git switch 'upstream-pr/234'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/234'
git switch 'main'
git merge --squash 'upstream-pr/234'
git switch 'main'
git merge --ff-only 'upstream-pr/234'
git switch 'main'
git merge 'upstream-pr/234'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
Marvisak/VIctory!234
No description provided.