perf(libc): read private write watches per allocation without a region query #9098
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/9098"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-21T12:12:22Z.
What
On Windows,
WindowsMappings::Collectwalks a range of guest memory and collects the pages written since the last collect. For the guest arena's private allocations, each step askedVirtualQueryfor the region before reading its write watch.RegionSizeby walking the rest of the region, so a collect deep in a large commit pays for the whole remainder. In one 449 MiB private commit, a query took 379.2 µs and theGetWriteWatch2.2 µs.The change (this head replaces the earlier one; the first commit is fable-void's, under his name):
perf(libc): read private write watches per allocation without a query(fable-void, from his 2760ad808).GetWriteWatchper allocation from that map. A read can't span two allocations, so pieces committed one by one still take one read each, but the query per piece is gone.fix(libc): return false from a private collect over memory released behind the mappings.WindowsMappingsreleases memory inside a recorded extent,GetWriteWatchfails there.Collectreturnsfalseas before; otherwise the failure is still fatal.test(libc): check private collects against a region query under APS5_VERIFY_DIRECT_COLLECT.WindowsMappings.GetWriteWatchstill succeeds over it there.Collectthrows, naming the span, if it succeeded over memory that's no longer committed private.perf(libc): count private collect calls only in the collect test. The call counters compile only whereAPS5_COUNT_MAPPING_QUERIESis set, as the address-space query counter does. The runtime'sCollectupdates no counter.Linux and macOS are unchanged:
WindowsMappings.hppis Windows-only.Measured on GitHub's Windows runner (a bench of Resident Evil Village (PPSA01556)'s traced load, 5,765 retired imports over 2,077 refreshes, on fork
exp/rewatch-phases-fixed, not part of this PR; runs 39697304565 and 39697362432):mainmain), with noVirtualQueryResident Evil Village (PPSA01556) in-game: loading isn't faster.
APS5_LAZY_SHADER_PREPARE=1,APS5_NO_GPL=1,APS5_HOST_IMPORT_MIB=6400, andAPS5_TRACE_SUMMARY_EVERY=1for per-second summary lines, with no other profiling switch. GPU clocks weren't held.main1ff5c8ac5cplus the #6457 fix (tree d56bdc5155);Driver.Packet.dispatchover the load (per-run table and sources):q3b_check.py, its output): C at most 956 ms, A 1,191, and B 1,190 and 1,191.A second session, B A A B (2026-10-24) (runs, table and script):
Driver.Packet.dispatchper frame over the load (presents-weighted as above, with the window starting at Yes):Overlap:
fbf50df422(its merge withmain0ea0f25e1b), whose shared-view change it composes with.core/libs/tests/WindowsWriteWatch.cppcame in through #9505's merge, and this PR's case runs beside them (see Tested).main: merged in98fd280383, through #9505's merge.main's #10070 moved the placeholder replacement inCommitinto areplacehelper and added a path that carves whole granules out of one placeholder. This PR's bookkeeping moved with it:replacerecords each private allocation, so allocations on either path are recorded, and the new path erases the records over the range it replaces, asresetdoes on the other path.Tested
agc_private_collect(new, fable-void's): 256 MiB of 16 KiB pieces with scattered writes returns the same pages as a reference read, with at most one system call per piece. One allocation in five protection regions takes one call.windows_write_watch: a collected private allocation released withVirtualFreebehind the mappings must collectfalsewithout throwing. Without the second commit it throws.windows_write_watch_verify_direct: under the switch, a collect after a page is decommitted in place must not succeed. Without the third commit's check it succeeds.agc_private_collectwith the guarded counters,windows_shared_map_costand both write-watch tests;main7b32a842f8(run 39890788684):agc_private_collect,windows_shared_map_cost,windows_write_watchandwindows_write_watch_verify_directpass ×10. The 8agc_import_*tests skip, because the runner has no Vulkan device;agc_import_watchpasses ×10;APS5_SKIP_TIMING_TESTS=1, which relaxesprecise_wait's median bounds on GitHub's hosted runners;main's code, the Windows tests don't build, because they use the new calls.98fd280383, merged with #9505's merge ofmain1e20650e84(run), with the bookkeeping moved as described under Overlap:agc_private_collect,windows_shared_map_cost,windows_write_watch,windows_write_watch_verify_direct,guest_memory,guest_shm_openandguest_jit_shared_memorypass ×10: 8 run, 7 pass, 1 skip, 0 fail.agc_import_watchskips, as the runner has no Vulkan device;agc_import_watch,guest_memory,guest_shm_openandguest_jit_shared_memorypass ×10: 4 run, 4 pass, 0 skip, 0 fail;APS5_SKIP_TIMING_TESTS=1. All pass except libkernel'sguest_raise_in_system_callon Windows, which timed out after 30 s as the suite's 211th of 213. Run alone 20 times in a row, it andguest_raise_exceptionpass on this head (run) and onmain1e20650e84(run), as #9505 records for its own head.bc0971afbc, merged with #9505's merge ofmain0ea0f25e1b(run). One conflict, the list of cases inWindowsWriteWatch.cpp'smain(), where both sides are kept: this PR'sCheckReleasedPrivateAllocationand #9159's four compared-shared cases. This PR's own added and removed lines are unchanged.agc_private_collect,windows_shared_map_cost,windows_write_watch,windows_write_watch_verify_direct, #9159'sagc_rewatched_direct_shared,agc_import_alias_sharedandagc_compared_shared_generations,guest_memory,guest_shm_open,guest_jit_shared_memoryandguest_raise_exceptionpass ×10: 12 run, 11 pass, 1 skip (agc_import_watch, no Vulkan device), 0 fail;agc_import_watch,guest_memory,guest_shm_open,guest_jit_shared_memoryandguest_raise_exceptionpass ×10: 5 run, 5 pass, 0 skip, 0 fail;APS5_SKIP_TIMING_TESTS=1, which relaxesprecise_wait's median bounds on GitHub's hosted runners.check_conventions.py: passes. No comment lines are added.1f691a369e,agc_private_collect_testsbuilt in a plain build (run), after thuanquang's review: the target was added withEXCLUDE_FROM_ALLand nothing turned it back on, so a plaincmake --build buildnever built it, and a full ctest would reportagc_private_collectas Not Run. The flag is dropped. On Windows, a plain build ofallbuilds it, and the test passes 10 runs in a row: 1 run, 1 pass, 0 skip, 0 fail. Linux: 0 run, as the test is registered on Windows only. Suites: libc and libkernel pass on Windows (170 and 221). The suites ran withAPS5_SKIP_TIMING_TESTS=1, which relaxesprecise_wait's median bounds on GitHub's hosted runners.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)core/libs/prx/libc/include/WindowsMappings.hpp): this PR first, then #11152 rebases onto itImported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-21T12:12:40Z.
This pull request is labeled
perf, so it needs the summary line of a capture onmainand one with the change, from the same title, scene and machine, as the Performance report form asks. Please paste bothaps5-summary.jsonllines, attach the two traces and add the output oftools/trace_digest.py.Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-10-21T17:42:52Z.
Review: COMMENTED
Good PR: the cost is isolated and measured (
VirtualQuery379.2 us of a 383.9 us collect inside one 449 MiB commit, because Windows walks the rest of the region), the direct path trusts only the proofCollectalready relied on, and the safety argument lists every path that clears it. The large-commit result (2,221 ms to 28.5 ms for 5,765 spans) is clear.GetWriteWatchcall and then the old queries. A title whose arena is committed in many small pieces pays this on every collect. Remember the failure: keep the proof from being used for that allocation (or that range) until the arena's commit state changes, so the failed call happens once, not once per span.Collectin isolation. The effect on a title is the thing that matters: Village's load with #8926's rewatch, load time or presents, main against this PR, from the same machine, withoutAPS5_PROFILE_*. 2.2 s of collect time over the load is real, but a title measurement shows whether it's on the critical path.WindowsMappings. Nothing in the tree does that." Add a debug check that the direct call's result matches the old path's on a sample (or under a verify switch), so a later caller that does that is caught, rather than relying on nothing doing it.Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-21T17:47:54Z.
@CnRJay, thank you for the review.
We ran that title measurement on Resident Evil Village (PPSA01556):
main1ff5c8ac5cwith #8926, against the same with this PR, on one machine, warm shader cache, noAPS5_PROFILE_*, timed from Yes to the first gameplay frame, in the order B A A B:It was slower in both runs, so we don't claim a load-time gain. That's two runs per arm, with a 5-minute spread between the two base runs, and GPU clocks weren't held. A traced minute of each load showed a ~16 s stall with two queue workers in
Driver.Packet.memory_waitin this PR's run only, but the two windows fell ~50 s apart in their loads. We're profiling the collect path in the load before saying more.We'll add both of your other points: remembering a failed
GetWriteWatchfor that allocation until the arena's commit state changes, and a verify switch that compares the direct call's result with the old path's.Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-21T18:49:40Z.
@CnRJay, your first and third points are in:
APS5_VERIFY_DIRECT_COLLECT, which checks each direct call against a region query and throws, naming the span, on a mismatch.Each has its red with only its check reverted (run 39651317856, run 39651193432), and both pass ×10 on Windows (run 39648160729).
One correction and one finding:
GetWriteWatchtime (118.8 ms with them, 112.9 without). With 9e9afb9c74 the collect makes the same calls asmain's path, and took 217.8 ms in all, against 218.9 with the first commit alone. The description now has the same-run numbers.GetWriteWatchsucceeds over a page decommitted in place inside a proven allocation, so the direct path would report no writes there where the old path returnsfalse. Nothing in the tree decommits that way today, and the switch now catches it if a caller ever does.Your second point, the Resident Evil Village (PPSA01556) load, follows after the profiled pair.