perf(libc): carve fresh private guest pages out of their placeholder #10070

Merged
thuanquang merged 1 commit from upstream-pr/10070 into main 2026-10-24 06:54:04 +00:00
thuanquang commented 2026-10-23 08:09:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by thuanquang on 2026-10-23T08:09:46Z.


What

On Windows, DirectMemory.cpp commits guest memory through CommitArenaRange with a 16 KiB granule: every anonymous map (mmap, mmap_aligned and the MAP_FIXED path), a fixed remap of a registered range to anonymous memory, and an mprotect of an arena range (which commits the pages that are not committed yet and only changes the protection of the rest). That calls WindowsMappings::Commit. For each fresh 16 KiB page, Commit ran reset() before replacing the placeholder: 10 VirtualQuery calls, one placeholder split and the VirtualAlloc2, so 12 system calls per page. When the map lands at the arena's first-fit frontier, the placeholder those queries look at is the arena's free tail, hundreds of GiB. For 256 fresh pages one page into a 1 GiB placeholder, main's loop makes 2,561 VirtualQuery calls; this change makes one.

This PR keeps one host allocation per 16 KiB page: each page is still its own allocation with the same protection and write watch, Commit reports the same created ranges as before, and munmap, MAP_FIXED remaps, views and collects see the same layout as before. What changes is how a fresh range is cut out of its placeholder:

  • Commit queries the placeholder once, splits off the part before the range (if the range starts inside the placeholder) and the part after it (if the placeholder extends past the range), and then carves the pages from that bounded placeholder: one split and one VirtualAlloc2 per page, and no split for the last page.
  • A piece that would cross the end of its placeholder (a whole-range granule over several placeholders) still takes the coalescing path through reset(), and committed memory in the range still only changes its protection, as on main.
  • A zero granule now throws std::invalid_argument before touching the address space; the carve loop needs a granule to make progress. Every caller passes a non-zero multiple of 4 KiB (DirectMemory.cpp 16 KiB, GuestHeap.cpp and the driver's Lifecycle.cpp the whole range).

windows_write_watch counts the queries through a counter that only that test defines (APS5_COUNT_MAPPING_QUERIES, WindowsMappings::MemoryQueries(), copied exactly from #9505 so whichever PR lands second drops its copy).

Overlap:

  • #9505 (EagleBearerYS, one view per run of shared guest pages) adds the same query counter: the counter hunks here (APS5_COUNT_MAPPING_QUERIES, MemoryQueries(), the increment in query() and the test's compile definition) are copied verbatim from its head a2e0bf19e6, so whichever PR lands second drops its copy. #9505 also adds two breakView calls to reset(): pages carved from one placeholder no longer go through reset(), while the coalescing path still does, and there #9505's calls run as on main.
  • #9098 (EagleBearerYS, draft) carries the same counter and records each private allocation right after the allocate call that this change moves into replace; whichever of the two lands second has to keep that record inside replace.
  • #9181 and #9159 do not touch Commit.
  • #10025 adds MapZero to the same file (read-only zero-page views for unbacked AMM PRT pages) and edits DirectMemory.cpp; it merges without conflicts with this branch, and a later Commit over its committed zero-page views takes the protection-only branch, as on main.
  • Textually, this branch conflicts with #9505 and #9159 only in tests/WindowsWriteWatch.cpp (new test functions and their calls in main() at the same places), and with #9098 also in WindowsMappings.hpp (the moved allocate call).

Tested

Windows 11 26200, Ryzen 5 5600X, AMD Radeon RX 6700 XT (driver 32.0.21045.16010) for the GPU tests and the title, WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON, this head 73f9b5d9fb on main 9d6adeb550.

  • windows_write_watch, red on main's Commit: with main's WindowsMappings.hpp plus only the counter hunks, the test fails with committing 256 fresh pages queried the address space 2561 times. With this change it passes 10 times in a row.
  • The 40 related tests (ctest -R "^(windows_write_watch|windows_mappings_release.*|guest_memory.*|guest_mmap.*|guest_mprotect.*|guest_protection_report|guest_flexible_memory_.*|guest_page_table_stats|guest_kernel_stack_protect|guest_mlock|agc_write_tracking.*|agc_resident_direct_remap|agc_write_watch_coverage|agc_import_.*|agc_host_import_.*|agc_heap_mirror_.*)$", ANYPS5_REQUIRE_VULKAN=1) pass three times in a row. guest_memory's CheckRemappedDirectMemoryIsChargedOnce failed once in another series of CPU-only runs while a build ran next to it; it compares the system-wide commit charge, and it fails the same way on main 9d6adeb550 (1 of 3 runs under the same load).
  • Full ctest --parallel 4 --timeout 300 (1937 tests) on this head and on main 9d6adeb550: 1932 pass on both, and the same five fail on both: log_macros and log_macros_timestamps (a local artefact of this tree's ccache base directory, which makes __FILE__ relative) and the three agc_driver_sample_order tests. In an earlier full run on c51ca2b323, guest_memory exited with 0xc0000409 once; it passed in both reruns, and its system-wide commit charge check is the one that flakes under load on main too.

Title, PPSA14363 (METAL GEAR SOLID Master Collection Vol. 1), same machine, PC idle (no input for over 20 minutes, no other game running), the exclusive GPU lock and the build lock held for the whole series, so nothing else ran on the PC. One run directory prepared from main 9d6adeb550; each arm swaps in all of its build's .prx files (checked by hash before every run): A = main 9d6adeb5500c, B = this change built as c51ca2b3232e, which has the same production code as this head 73f9b5d9fb (the head only moves one new test function into the anonymous namespace and rewords the commit message). Order W(arm)-A-B-B-A-A-B-B-A, 45 s each from start-up, APS5_TRACE_SUMMARY_EVERY=10, warm shader cache (all hits). The first 10 s window after the first present covers the boot's anonymous maps:

Run first 10 s: presents frame p99 frame max
A (main), 4 runs 546, 547, 550, 552 25.6-34.5 ms 578.0, 603.1, 605.5, 704.4 ms
B (this change), 3 runs 588, 593, 594 17.4-22.2 ms 96.6, 116.1, 116.8 ms

The later windows match on both sides (599-600 presents, p99 about 17.2 ms). Summary lines of one run per side, first window:

{"commit":"9d6adeb5500c","dirty":false,"env":{"APS5_LOG_TIMESTAMPS":"1","APS5_TRACE_DIR":"<path>","APS5_TRACE_SUMMARY_EVERY":"10"},"gpu":"AMD Radeon RX 6700 XT","driver":"AMD proprietary driver 26.10.1 (AMD proprietary shader compiler)","os":"Windows 10.0.26200","cpu":"AMD Ryzen 5 5600X 6-Core Processor","cpu_threads":12,"memory_mib":32690,"title_id":"PPSA14363","title":"METAL GEAR SOLID - Master Collection Version","content_version":"03.000.000","resolution":"1920x1080","window":{"spec":"summary_every 10","first_present":0,"ms":10001.714},"interval_start_s":0.000,"interval_end_s":10.002,"presents":546,"frame_ms":{"p50":16.683,"p90":16.941,"p99":26.658,"max":605.460},"gpu_busy_ms_per_present":1.220,"worker_busy_pct":{"queue worker 0x0":2.706},"top_phases_ms_per_frame":{"VideoOut.Flip":15.720,"VideoOut.Flip.vblank_wait":14.346,"Driver.Present":1.258,"VideoOut.Flip.present":1.233,"Driver.Present.present_display_buffer":0.623,"Driver.Packet":0.492,"Driver.Packet.device_lock":0.375,"GpuMutex.wait.flush":0.346},"trace_dropped":false,"trace":""}
{"commit":"c51ca2b3232e","dirty":false,"env":{"APS5_LOG_TIMESTAMPS":"1","APS5_TRACE_DIR":"<path>","APS5_TRACE_SUMMARY_EVERY":"10"},"gpu":"AMD Radeon RX 6700 XT","driver":"AMD proprietary driver 26.10.1 (AMD proprietary shader compiler)","os":"Windows 10.0.26200","cpu":"AMD Ryzen 5 5600X 6-Core Processor","cpu_threads":12,"memory_mib":32690,"title_id":"PPSA14363","title":"METAL GEAR SOLID - Master Collection Version","content_version":"03.000.000","resolution":"1920x1080","window":{"spec":"summary_every 10","first_present":0,"ms":10012.783},"interval_start_s":0.000,"interval_end_s":10.013,"presents":594,"frame_ms":{"p50":16.683,"p90":16.912,"p99":17.463,"max":96.551},"gpu_busy_ms_per_present":1.208,"worker_busy_pct":{"queue worker 0x0":2.325},"top_phases_ms_per_frame":{"VideoOut.Flip":15.602,"VideoOut.Flip.vblank_wait":14.827,"Driver.Present":0.673,"VideoOut.Flip.present":0.650,"Driver.Packet":0.389,"Driver.Packet.device_lock":0.286,"Driver.Present.acquire_image":0.263,"Vulkan.Acquire":0.262},"trace_dropped":false,"trace":""}
tools/performance_compare.py on those two lines

Performance comparison

Capture Commit Trace Presents Window (ms)
baseline 9d6adeb550 546 10001.714
candidate c51ca2b3232e 594 10012.783
  • Compare captures from the same machine, title, content version and scene; matching recorded metadata cannot verify the physical machine or scene.
  • Changes are candidate minus baseline. A negative frame-time change means a shorter frame interval.
  • A missing top phase is unreported, not zero; its change is unavailable.

Frame and GPU times (ms)

Metric Baseline Candidate Change Change (%)
frame_ms.p50 16.683 16.683 +0.000 +0.000
frame_ms.p90 16.941 16.912 -0.029 -0.171
frame_ms.p99 26.658 17.463 -9.195 -34.492
frame_ms.max 605.460 96.551 -508.909 -84.053
mean_frame_ms 18.318 16.857 -1.462 -7.979
gpu_busy_ms_per_present 1.220 1.208 -0.012 -0.984

Top phases (ms per frame)

Metric Baseline Candidate Change Change (%)
Driver.Packet 0.492 0.389 -0.103 -20.935
Driver.Packet.device_lock 0.375 0.286 -0.089 -23.733
Driver.Present 1.258 0.673 -0.585 -46.502
Driver.Present.acquire_image n/a 0.263 n/a n/a
Driver.Present.present_display_buffer 0.623 n/a n/a n/a
GpuMutex.wait.flush 0.346 n/a n/a n/a
VideoOut.Flip 15.720 15.602 -0.118 -0.751
VideoOut.Flip.present 1.233 0.650 -0.583 -47.283
VideoOut.Flip.vblank_wait 14.346 14.827 +0.481 +3.353
Vulkan.Acquire n/a 0.262 n/a n/a

One more B start wrote only the first characters of its first stderr line in its 45 s and never reached guest code (no other output); the next six starts of the same files reached the guest in 0.96-1.49 s. Traces are not attached: for this commercial title we share summary lines only.

Not tested: Linux (the change is Windows-only code), other CPUs and Windows builds.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a (Windows memory mapping only)
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4979812458 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10070). Originally posted by [thuanquang](https://github.com/thuanquang) on 2026-10-23T08:09:46Z. --- ### What On Windows, `DirectMemory.cpp` commits guest memory through `CommitArenaRange` with a 16 KiB granule: every anonymous map (`mmap`, `mmap_aligned` and the `MAP_FIXED` path), a fixed remap of a registered range to anonymous memory, and an `mprotect` of an arena range (which commits the pages that are not committed yet and only changes the protection of the rest). That calls `WindowsMappings::Commit`. For each fresh 16 KiB page, `Commit` ran `reset()` before replacing the placeholder: 10 `VirtualQuery` calls, one placeholder split and the `VirtualAlloc2`, so 12 system calls per page. When the map lands at the arena's first-fit frontier, the placeholder those queries look at is the arena's free tail, hundreds of GiB. For 256 fresh pages one page into a 1 GiB placeholder, `main`'s loop makes 2,561 `VirtualQuery` calls; this change makes one. This PR keeps one host allocation per 16 KiB page: each page is still its own allocation with the same protection and write watch, `Commit` reports the same created ranges as before, and `munmap`, `MAP_FIXED` remaps, views and collects see the same layout as before. What changes is how a fresh range is cut out of its placeholder: - `Commit` queries the placeholder once, splits off the part before the range (if the range starts inside the placeholder) and the part after it (if the placeholder extends past the range), and then carves the pages from that bounded placeholder: one split and one `VirtualAlloc2` per page, and no split for the last page. - A piece that would cross the end of its placeholder (a whole-range granule over several placeholders) still takes the coalescing path through `reset()`, and committed memory in the range still only changes its protection, as on `main`. - A zero granule now throws `std::invalid_argument` before touching the address space; the carve loop needs a granule to make progress. Every caller passes a non-zero multiple of 4 KiB (`DirectMemory.cpp` 16 KiB, `GuestHeap.cpp` and the driver's `Lifecycle.cpp` the whole range). `windows_write_watch` counts the queries through a counter that only that test defines (`APS5_COUNT_MAPPING_QUERIES`, `WindowsMappings::MemoryQueries()`, copied exactly from #9505 so whichever PR lands second drops its copy). Overlap: - #9505 (EagleBearerYS, one view per run of shared guest pages) adds the same query counter: the counter hunks here (`APS5_COUNT_MAPPING_QUERIES`, `MemoryQueries()`, the increment in `query()` and the test's compile definition) are copied verbatim from its head `a2e0bf19e6`, so whichever PR lands second drops its copy. #9505 also adds two `breakView` calls to `reset()`: pages carved from one placeholder no longer go through `reset()`, while the coalescing path still does, and there #9505's calls run as on `main`. - #9098 (EagleBearerYS, draft) carries the same counter and records each private allocation right after the `allocate` call that this change moves into `replace`; whichever of the two lands second has to keep that record inside `replace`. - #9181 and #9159 do not touch `Commit`. - #10025 adds `MapZero` to the same file (read-only zero-page views for unbacked AMM PRT pages) and edits `DirectMemory.cpp`; it merges without conflicts with this branch, and a later `Commit` over its committed zero-page views takes the protection-only branch, as on `main`. - Textually, this branch conflicts with #9505 and #9159 only in `tests/WindowsWriteWatch.cpp` (new test functions and their calls in `main()` at the same places), and with #9098 also in `WindowsMappings.hpp` (the moved `allocate` call). ### Tested Windows 11 26200, Ryzen 5 5600X, AMD Radeon RX 6700 XT (driver 32.0.21045.16010) for the GPU tests and the title, WinLibs GCC 15.2.0 r7, Release, `BUILD_TESTING=ON`, this head `73f9b5d9fb` on `main` `9d6adeb550`. - `windows_write_watch`, red on `main`'s `Commit`: with `main`'s `WindowsMappings.hpp` plus only the counter hunks, the test fails with `committing 256 fresh pages queried the address space 2561 times`. With this change it passes 10 times in a row. - The 40 related tests (`ctest -R "^(windows_write_watch|windows_mappings_release.*|guest_memory.*|guest_mmap.*|guest_mprotect.*|guest_protection_report|guest_flexible_memory_.*|guest_page_table_stats|guest_kernel_stack_protect|guest_mlock|agc_write_tracking.*|agc_resident_direct_remap|agc_write_watch_coverage|agc_import_.*|agc_host_import_.*|agc_heap_mirror_.*)$"`, `ANYPS5_REQUIRE_VULKAN=1`) pass three times in a row. `guest_memory`'s `CheckRemappedDirectMemoryIsChargedOnce` failed once in another series of CPU-only runs while a build ran next to it; it compares the system-wide commit charge, and it fails the same way on `main` `9d6adeb550` (1 of 3 runs under the same load). - Full `ctest --parallel 4 --timeout 300` (1937 tests) on this head and on `main` `9d6adeb550`: 1932 pass on both, and the same five fail on both: `log_macros` and `log_macros_timestamps` (a local artefact of this tree's ccache base directory, which makes `__FILE__` relative) and the three `agc_driver_sample_order` tests. In an earlier full run on `c51ca2b323`, `guest_memory` exited with 0xc0000409 once; it passed in both reruns, and its system-wide commit charge check is the one that flakes under load on `main` too. **Title**, PPSA14363 (METAL GEAR SOLID Master Collection Vol. 1), same machine, PC idle (no input for over 20 minutes, no other game running), the exclusive GPU lock and the build lock held for the whole series, so nothing else ran on the PC. One run directory prepared from `main` `9d6adeb550`; each arm swaps in all of its build's `.prx` files (checked by hash before every run): A = `main` `9d6adeb5500c`, B = this change built as `c51ca2b3232e`, which has the same production code as this head `73f9b5d9fb` (the head only moves one new test function into the anonymous namespace and rewords the commit message). Order W(arm)-A-B-B-A-A-B-B-A, 45 s each from start-up, `APS5_TRACE_SUMMARY_EVERY=10`, warm shader cache (all hits). The first 10 s window after the first present covers the boot's anonymous maps: | Run | first 10 s: presents | frame p99 | frame max | |---|---|---|---| | A (`main`), 4 runs | 546, 547, 550, 552 | 25.6-34.5 ms | 578.0, 603.1, 605.5, 704.4 ms | | B (this change), 3 runs | 588, 593, 594 | 17.4-22.2 ms | 96.6, 116.1, 116.8 ms | The later windows match on both sides (599-600 presents, p99 about 17.2 ms). Summary lines of one run per side, first window: ```json {"commit":"9d6adeb5500c","dirty":false,"env":{"APS5_LOG_TIMESTAMPS":"1","APS5_TRACE_DIR":"<path>","APS5_TRACE_SUMMARY_EVERY":"10"},"gpu":"AMD Radeon RX 6700 XT","driver":"AMD proprietary driver 26.10.1 (AMD proprietary shader compiler)","os":"Windows 10.0.26200","cpu":"AMD Ryzen 5 5600X 6-Core Processor","cpu_threads":12,"memory_mib":32690,"title_id":"PPSA14363","title":"METAL GEAR SOLID - Master Collection Version","content_version":"03.000.000","resolution":"1920x1080","window":{"spec":"summary_every 10","first_present":0,"ms":10001.714},"interval_start_s":0.000,"interval_end_s":10.002,"presents":546,"frame_ms":{"p50":16.683,"p90":16.941,"p99":26.658,"max":605.460},"gpu_busy_ms_per_present":1.220,"worker_busy_pct":{"queue worker 0x0":2.706},"top_phases_ms_per_frame":{"VideoOut.Flip":15.720,"VideoOut.Flip.vblank_wait":14.346,"Driver.Present":1.258,"VideoOut.Flip.present":1.233,"Driver.Present.present_display_buffer":0.623,"Driver.Packet":0.492,"Driver.Packet.device_lock":0.375,"GpuMutex.wait.flush":0.346},"trace_dropped":false,"trace":""} {"commit":"c51ca2b3232e","dirty":false,"env":{"APS5_LOG_TIMESTAMPS":"1","APS5_TRACE_DIR":"<path>","APS5_TRACE_SUMMARY_EVERY":"10"},"gpu":"AMD Radeon RX 6700 XT","driver":"AMD proprietary driver 26.10.1 (AMD proprietary shader compiler)","os":"Windows 10.0.26200","cpu":"AMD Ryzen 5 5600X 6-Core Processor","cpu_threads":12,"memory_mib":32690,"title_id":"PPSA14363","title":"METAL GEAR SOLID - Master Collection Version","content_version":"03.000.000","resolution":"1920x1080","window":{"spec":"summary_every 10","first_present":0,"ms":10012.783},"interval_start_s":0.000,"interval_end_s":10.013,"presents":594,"frame_ms":{"p50":16.683,"p90":16.912,"p99":17.463,"max":96.551},"gpu_busy_ms_per_present":1.208,"worker_busy_pct":{"queue worker 0x0":2.325},"top_phases_ms_per_frame":{"VideoOut.Flip":15.602,"VideoOut.Flip.vblank_wait":14.827,"Driver.Present":0.673,"VideoOut.Flip.present":0.650,"Driver.Packet":0.389,"Driver.Packet.device_lock":0.286,"Driver.Present.acquire_image":0.263,"Vulkan.Acquire":0.262},"trace_dropped":false,"trace":""} ``` <details><summary>tools/performance_compare.py on those two lines</summary> **Performance comparison** | Capture | Commit | Trace | Presents | Window (ms) | | --- | --- | --- | ---: | ---: | | baseline | 9d6adeb5500c | | 546 | 10001.714 | | candidate | c51ca2b3232e | | 594 | 10012.783 | - Compare captures from the same machine, title, content version and scene; matching recorded metadata cannot verify the physical machine or scene. - Changes are candidate minus baseline. A negative frame-time change means a shorter frame interval. - A missing top phase is unreported, not zero; its change is unavailable. **Frame and GPU times (ms)** | Metric | Baseline | Candidate | Change | Change (%) | | --- | ---: | ---: | ---: | ---: | | frame_ms.p50 | 16.683 | 16.683 | +0.000 | +0.000 | | frame_ms.p90 | 16.941 | 16.912 | -0.029 | -0.171 | | frame_ms.p99 | 26.658 | 17.463 | -9.195 | -34.492 | | frame_ms.max | 605.460 | 96.551 | -508.909 | -84.053 | | mean_frame_ms | 18.318 | 16.857 | -1.462 | -7.979 | | gpu_busy_ms_per_present | 1.220 | 1.208 | -0.012 | -0.984 | **Top phases (ms per frame)** | Metric | Baseline | Candidate | Change | Change (%) | | --- | ---: | ---: | ---: | ---: | | Driver.Packet | 0.492 | 0.389 | -0.103 | -20.935 | | Driver.Packet.device_lock | 0.375 | 0.286 | -0.089 | -23.733 | | Driver.Present | 1.258 | 0.673 | -0.585 | -46.502 | | Driver.Present.acquire_image | n/a | 0.263 | n/a | n/a | | Driver.Present.present_display_buffer | 0.623 | n/a | n/a | n/a | | GpuMutex.wait.flush | 0.346 | n/a | n/a | n/a | | VideoOut.Flip | 15.720 | 15.602 | -0.118 | -0.751 | | VideoOut.Flip.present | 1.233 | 0.650 | -0.583 | -47.283 | | VideoOut.Flip.vblank_wait | 14.346 | 14.827 | +0.481 | +3.353 | | Vulkan.Acquire | n/a | 0.262 | n/a | n/a | </details> One more B start wrote only the first characters of its first stderr line in its 45 s and never reached guest code (no other output); the next six starts of the same files reached the guest in 0.96-1.49 s. Traces are not attached: for this commercial title we share summary lines only. **Not tested:** Linux (the change is Windows-only code), other CPUs and Windows builds. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a (Windows memory mapping only) - [x] AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
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
boykopovar/AnyPS5!10070
No description provided.