xmem: coalesce contiguous runs in XMemMapPhysicalPages #742

Closed
github-bot wants to merge 2 commits from gh-pr/742/head into gh-pr/742/base
Owner

@⁠madhyn on GitHub · 2026-11-24 13:17 UTC · original
madhyn:perf/xmem-coalesce-map-physical-pages → main · +163 −23 · 2 files · 2 commits · closed without merging

What and why

During retail startup (1.0.2.1), after CopyDescriptorsSimple, the title calls XMemMapPhysicalPages with 99,776 contiguous pages (6,236.0 MiB) in a single call. Mapping them one 64 KiB page at a time issues ~100k separate VirtualFree(MEM_PRESERVE_PLACEHOLDER) + MapViewOfFile3 calls, which under Wine 11.22 takes longer than 30 seconds and triggers the title's internal 30 s watchdog assert (0x142215c35).

This change coalesces contiguous runs of pages from the same Section and consecutive offsets into a single isolate_placeholder + MapViewOfFile3 call, while preserving per-page unmap/remap semantics:

  • MappedPage records view_base, view_pages, and view_protect.
  • unmap_page splits a multi-page view when a middle page is unmapped or overwritten, re-mapping the surviving prefix and suffix runs while keeping their backing data intact.
  • If a coalesced map_view_run fails (e.g. pre-split placeholders), XMemMapPhysicalPages falls back to mapping that run page-by-page.

Testing

  • Added test_coalesced_run_partial_unmap in runtime/test/xmem_test.cpp (maps 4 contiguous pages, unmaps a middle page, verifies prefix/suffix data survival, remaps the middle page back, unmaps and frees).
  • ctest -R xmem under Wine 11.22 (Arch Linux):
    • d3d12_xmem_placed: passed.
    • xmem: 223/228 checks passed, including all 11 checks of test_coalesced_run_partial_unmap (the 5 existing failures are Wine 11.22 VirtualQuery / PAGE_READONLY / commit-charge checks that also fail on main).
  • Retail gta6rt.sh --run (with #385 + #445 stack): the 99,776-page (6,236 MiB) XMemMapPhysicalPages call dropped from >30,000 ms (watchdog hang) to 14 ms, advancing boot through device slot 74, dstorage (CreateStatusArray, CreateQueue1, CPU zlib inflate), and reaching device slot 108.

AI-assisted: yes

**@⁠madhyn** on GitHub · 2026-11-24 13:17 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/742) `madhyn:perf/xmem-coalesce-map-physical-pages` → `main` · +163 −23 · 2 files · 2 commits · closed without merging ### What and why During retail startup (1.0.2.1), after `CopyDescriptorsSimple`, the title calls `XMemMapPhysicalPages` with **99,776 contiguous pages (6,236.0 MiB)** in a single call. Mapping them one 64 KiB page at a time issues ~100k separate `VirtualFree(MEM_PRESERVE_PLACEHOLDER)` + `MapViewOfFile3` calls, which under Wine 11.22 takes longer than 30 seconds and triggers the title's internal 30 s watchdog assert (`0x142215c35`). This change coalesces contiguous runs of pages from the same `Section` and consecutive offsets into a single `isolate_placeholder` + `MapViewOfFile3` call, while preserving per-page unmap/remap semantics: - `MappedPage` records `view_base`, `view_pages`, and `view_protect`. - `unmap_page` splits a multi-page view when a middle page is unmapped or overwritten, re-mapping the surviving prefix and suffix runs while keeping their backing data intact. - If a coalesced `map_view_run` fails (e.g. pre-split placeholders), `XMemMapPhysicalPages` falls back to mapping that run page-by-page. ### Testing - Added `test_coalesced_run_partial_unmap` in `runtime/test/xmem_test.cpp` (maps 4 contiguous pages, unmaps a middle page, verifies prefix/suffix data survival, remaps the middle page back, unmaps and frees). - `ctest -R xmem` under Wine 11.22 (Arch Linux): - `d3d12_xmem_placed`: passed. - `xmem`: 223/228 checks passed, including all 11 checks of `test_coalesced_run_partial_unmap` (the 5 existing failures are Wine 11.22 `VirtualQuery` / `PAGE_READONLY` / commit-charge checks that also fail on `main`). - Retail `gta6rt.sh --run` (with #385 + #445 stack): the 99,776-page (6,236 MiB) `XMemMapPhysicalPages` call dropped from **>30,000 ms (watchdog hang)** to **14 ms**, advancing boot through device slot 74, `dstorage` (`CreateStatusArray`, `CreateQueue1`, CPU zlib inflate), and reaching device slot 108. AI-assisted: yes
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:18:59 +00:00
github-bot closed this pull request 2026-11-26 09:18:59 +00:00
Author
Owner

@⁠abnalstlll-byte on GitHub · 2026-11-24 13:24 UTC · original · approved these changes

Audited and verified. Clean-room compliant.

  • Coalesces contiguous physical page mappings into single virtual memory placeholder view mappings via map_view_run in runtime/shims/xmem.cpp.
  • Handles partial unmapping inside coalesced views by splitting into prefix/suffix views.
  • Test coverage in test_coalesced_run_partial_unmap (xmem_test.cpp) verifies preservation of unmapped/remapped physical backing.
**@⁠abnalstlll-byte** on GitHub · 2026-11-24 13:24 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/742#pullrequestreview-6039963475) · **approved these changes** Audited and verified. Clean-room compliant. - Coalesces contiguous physical page mappings into single virtual memory placeholder view mappings via map_view_run in runtime/shims/xmem.cpp. - Handles partial unmapping inside coalesced views by splitting into prefix/suffix views. - Test coverage in test_coalesced_run_partial_unmap (xmem_test.cpp) verifies preservation of unmapped/remapped physical backing.
Author
Owner

@⁠moffington on GitHub · 2026-11-24 14:15 UTC · original

Independently tested 6ed318f2e against 2d73c242f under Wine 11.20 with clang-cl 23.1.3 and xwin.

All 11 new XMem checks pass, d3d12_xmem_placed passes, and additional partial-unmap/remap probes behaved correctly. The same five pre-existing Wine metadata failures remain.

The mapping improvement is substantial (4,096 pages: 150.65 ms → 0.60 ms), but I found a significant bulk-unmap regression:

Pages Base PR
1,024 12.22 ms 97.76 ms
2,048 23.20 ms 357.69 ms
4,096 49.91 ms 1,207.14 ms

The remap race test also increased from 249 ms to 2,404 ms.

The apparent cause is page-by-page unmap_page calls repeatedly unmapping and reconstructing the remaining coalesced view, producing quadratic scaling. This could be particularly expensive for the large mappings motivating this PR.

Could we introduce a range-aware unmap helper that unmaps each intersecting view once and reconstructs only surviving prefixes and suffixes? That should preserve the mapping speedup without introducing the bulk-unmap regression.

**@⁠moffington** on GitHub · 2026-11-24 14:15 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/742#issuecomment-7134079773) Independently tested `6ed318f2e` against `2d73c242f` under Wine 11.20 with clang-cl 23.1.3 and xwin. All 11 new XMem checks pass, `d3d12_xmem_placed` passes, and additional partial-unmap/remap probes behaved correctly. The same five pre-existing Wine metadata failures remain. The mapping improvement is substantial (4,096 pages: 150.65 ms → 0.60 ms), but I found a significant bulk-unmap regression: | Pages | Base | PR | |---:|---:|---:| | 1,024 | 12.22 ms | 97.76 ms | | 2,048 | 23.20 ms | 357.69 ms | | 4,096 | 49.91 ms | 1,207.14 ms | The remap race test also increased from 249 ms to 2,404 ms. The apparent cause is page-by-page `unmap_page` calls repeatedly unmapping and reconstructing the remaining coalesced view, producing quadratic scaling. This could be particularly expensive for the large mappings motivating this PR. Could we introduce a range-aware unmap helper that unmaps each intersecting view once and reconstructs only surviving prefixes and suffixes? That should preserve the mapping speedup without introducing the bulk-unmap regression.
Author
Owner

@⁠madhyn on GitHub · 2026-11-24 15:01 UTC · original

Implemented unmap_range in f6cfa2f25 to address the O(N²) bulk-unmap regression:

  • Added unmap_range(start, count) in runtime/shims/xmem.cpp so each intersecting coalesced view is unmapped once, with at most one prefix and/or suffix map_view_run reconstruction for surviving pages outside the unmapped range. unmap_page now delegates to unmap_range(address, 1).
  • Updated XMemMapPhysicalPages to determine contiguous zero/non-zero runs first and call unmap_range once per run instead of calling unmap_page page-by-page.
  • Extended test_coalesced_run_partial_unmap in runtime/test/xmem_test.cpp to cover multi-page middle sub-range unmaps.
  • ctest -R xmem (Wine 11.22): d3d12_xmem_placed passed; xmem 227/232 checks passed (all 15 checks in test_coalesced_run_partial_unmap pass; same 5 pre-existing Wine metadata/protection failures), and the 4,096-page remap race test dropped to 6 ms.

AI-assisted: yes

**@⁠madhyn** on GitHub · 2026-11-24 15:01 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/742#issuecomment-7135331771) Implemented `unmap_range` in `f6cfa2f25` to address the O(N²) bulk-unmap regression: - Added `unmap_range(start, count)` in `runtime/shims/xmem.cpp` so each intersecting coalesced view is unmapped once, with at most one prefix and/or suffix `map_view_run` reconstruction for surviving pages outside the unmapped range. `unmap_page` now delegates to `unmap_range(address, 1)`. - Updated `XMemMapPhysicalPages` to determine contiguous zero/non-zero runs first and call `unmap_range` once per run instead of calling `unmap_page` page-by-page. - Extended `test_coalesced_run_partial_unmap` in `runtime/test/xmem_test.cpp` to cover multi-page middle sub-range unmaps. - `ctest -R xmem` (Wine 11.22): `d3d12_xmem_placed` passed; `xmem` 227/232 checks passed (all 15 checks in `test_coalesced_run_partial_unmap` pass; same 5 pre-existing Wine metadata/protection failures), and the 4,096-page `remap race` test dropped to **6 ms**. AI-assisted: yes
Author
Owner

@⁠milk4sale on GitHub · 2026-11-24 18:12 UTC · original

Tested f6cfa2f25 with the retail 1.0.2.1 package on an RTX 4070 Ti (NVIDIA 620.70.06), wine-staging 11.21, Linux (CachyOS): the two commits cherry-picked onto #939's head eb6a08cd3 (they apply cleanly), gta6rt --first-run-ui, network off, clang-cl 23.1.4 + xwin build. Counts only.

#939 alone (4 runs) #939 + this pull request (2 runs)
End of the run the title's watchdog (the write to 0 at +0x2215c35) at 32.0 to 32.4 s, 30 s after the 99,776-page request still running at my 240 s limit, no exception
D3D12 calls 1,657 to 1,662 676,947 and 738,683
PresentX ok 0 10,946 and 12,110

The wineserver's side of the stall, which I have not seen posted: sampled once a second from outside the sandbox (/proc/<pid>/task/*/stat and status), after XMemAllocatePhysicalPages of 99,776 pages the title's main thread makes about 7,300 voluntary context switches a second for 27 s or more, about 190,000 in all, which is two server round trips a page. Its own CPU use falls from 0.8 to 0.05 s per second while the wineserver's rises from 0.2 to 0.9 s per second, so each map costs the server more than the one before. With this change the wineserver gains under 0.2 s of CPU in the 5 s after the request.

One of the four runs had #939's file-backed section switched off (LEONIDA_XMEM_BACKING_DIR set to a non-absolute value) and stalled the same way, so it is the number of views and not the kind of section.

Not checked here: xmem_test, a partial unmap inside the 99,776-page view, and Windows. The frames the #939 stack presents under Wine are black in both runs (every captured frame up to frame 5,851); I have not looked into why, and it is not this pull request's subject. AI-assisted (Claude Code).

**@⁠milk4sale** on GitHub · 2026-11-24 18:12 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/742#issuecomment-7140304091) Tested `f6cfa2f25` with the retail 1.0.2.1 package on an RTX 4070 Ti (NVIDIA 620.70.06), wine-staging 11.21, Linux (CachyOS): the two commits cherry-picked onto #939's head `eb6a08cd3` (they apply cleanly), `gta6rt --first-run-ui`, network off, clang-cl 23.1.4 + xwin build. Counts only. | | #939 alone (4 runs) | #939 + this pull request (2 runs) | |---|---|---| | End of the run | the title's watchdog (the write to 0 at `+0x2215c35`) at 32.0 to 32.4 s, 30 s after the 99,776-page request | still running at my 240 s limit, no exception | | D3D12 calls | 1,657 to 1,662 | 676,947 and 738,683 | | `PresentX ok` | 0 | 10,946 and 12,110 | The wineserver's side of the stall, which I have not seen posted: sampled once a second from outside the sandbox (`/proc/<pid>/task/*/stat` and `status`), after `XMemAllocatePhysicalPages` of 99,776 pages the title's main thread makes about 7,300 voluntary context switches a second for 27 s or more, about 190,000 in all, which is two server round trips a page. Its own CPU use falls from 0.8 to 0.05 s per second while the wineserver's rises from 0.2 to 0.9 s per second, so each map costs the server more than the one before. With this change the wineserver gains under 0.2 s of CPU in the 5 s after the request. One of the four runs had #939's file-backed section switched off (`LEONIDA_XMEM_BACKING_DIR` set to a non-absolute value) and stalled the same way, so it is the number of views and not the kind of section. Not checked here: `xmem_test`, a partial unmap inside the 99,776-page view, and Windows. The frames the #939 stack presents under Wine are black in both runs (every captured frame up to frame 5,851); I have not looked into why, and it is not this pull request's subject. AI-assisted (Claude Code).
Author
Owner

@⁠milk4sale on GitHub · 2026-11-24 19:48 UTC · original

Correction to my comment above: the two runs with this pull request presented 5,473 and 6,055 frames, not 10,946 and 12,110. Each present writes two trace lines that contain PresentX ok (the queue method and the presentation core), and I counted lines. The call counts, the timings and the wineserver numbers stand; so does the result (no stall, 240 s without an exception).

**@⁠milk4sale** on GitHub · 2026-11-24 19:48 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/742#issuecomment-7142627547) Correction to my comment above: the two runs with this pull request presented 5,473 and 6,055 frames, not 10,946 and 12,110. Each present writes two trace lines that contain `PresentX ok` (the queue method and the presentation core), and I counted lines. The call counts, the timings and the wineserver numbers stand; so does the result (no stall, 240 s without an exception).
Author
Owner

@⁠madhyn on GitHub · 2026-11-25 05:35 UTC · original

Closing as superseded — coalesced physical page views and range-based unmap_range landed on main via #984. Huge thanks to @⁠moffington for catching the O(N²) bulk-unmap regression and to @⁠milk4sale for the Wine/wineserver benchmarks.

**@⁠madhyn** on GitHub · 2026-11-25 05:35 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/742#issuecomment-7153938076) Closing as superseded — coalesced physical page views and range-based `unmap_range` landed on `main` via #984. Huge thanks to @⁠moffington for catching the O(N²) bulk-unmap regression and to @⁠milk4sale for the Wine/wineserver benchmarks.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.