perf(libc): skip never-written pages when evicting a resident direct-memory run #12227
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/12227"
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 klosixgaming-arch on 2026-10-28T03:58:58Z.
What
On Windows, releasing a resident direct-memory run (
WindowsMappings::evict) read every page of it withblank(). Reading a committed page that was never written faults in a zeroed page, and about 97% of the pages Soul Hackers 2 releases were never written. The scan cost 2.9 us per page and held the guest-allocation registry mutex for about 7 s of every 10 s, which is what limited the CPU side of its light scene.The run's
GetWriteWatchis now read once (after the run is made read-only) andblank()is only called for the pages it lists. That is only done when the list is complete: the run was committed withMEM_WRITE_WATCH, no collection has cleared its watch (Run::reset), and no host import touched it (Run::imported, because GPU writes through an import are not seen by the write watch). Otherwise, and ifGetWriteWatchfails, the old full scan runs, soAPS5_NO_WRITE_WATCHkeeps the old behaviour.Where the time went (timers inside
evict, 10 s window, light scene, before the change): total 3,340 ms,blank()2,979 ms, VirtualProtect 74 ms, views.erase 113 ms, copy of written pages 31 ms; 1,029,031 pages scanned, 26,670 written. Scanning the same pages a second time took 181 ms, and an SSE2blank()changed nothing, so the cost is the first-touch faults. With the change (probe build): mutations holding the registry mutex 7.1 s -> 1.5 s per 10 s, lookups waiting on it 4.4 s -> 0.47 s,evict0.53 s per 10 s.Tested
Windows 10.0.26200, RTX 5090 (NVIDIA 617.42), Ryzen 9 9950X3D, Soul Hackers 2 (PPSA03743) from the start of the game, on the same machine and in the same session. Both builds were captured with
APS5_TRACE_FRAMES=400:180(the same 180 game frames, from present 400, sofirst_presentmatches) and no otherAPS5_TRACE_*/APS5_PROFILE_*switch, one run each,mainfirst. The caches were warm.main2227f1366188:aps5-trace-2227f1366188-12632.json.gze21aa4cabddf(rebased on thatmain):aps5-trace-e21aa4cabddf-23548.json.gzThe traces are attached in the comment below.
aps5-summary.jsonlline,main:aps5-summary.jsonlline, this branch:tools/performance_compare.pyon those two lines:Performance comparison
2227f13661e21aa4cabdFrame and GPU times (ms)
Top phases (ms per frame)
The only slow frame on the branch (present 448, 70.7 ms) is a graphics pipeline build (
Vulkan.GraphicsPipelinePrebuild), not the change; the other 179 frames are 17.1 ms or less, at the 60 Hz limit (VideoOut.Flip.vblank_wait). Onmainthe queue worker is busy 96% of the time.tools/trace_digest.py, main
frames 400:180 from present 400: 180 presents, frame p50 25.2 / p90 30.0 / p99 38.1 / max 41.4 ms, GPU busy 1.2 ms per present
NVIDIA GeForce RTX 5090, NVIDIA 617.42, Windows 10.0.26200, 3840x2160, commit
2227f13661, dropped: noSlowest frames (largest slices inside each, inclusive)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): Driver.Packet 24.2, Driver.Packet.dispatch 16.3, Driver.Dispatch 9.0, Graphics.Draw 6.7, Graphics.Draw.draw_and_resource_release 6.2, Vulkan.Dispatch 4.6, VideoOut.Flip 4.6, Driver.Dispatch.decode_key_device_setup 4.3, Resources.Complete 4.3, Vulkan.Dispatch.shader_resources 3.4
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
tools/trace_digest.py, this branch
frames 400:180 from present 400: 180 presents, frame p50 16.7 / p90 16.9 / p99 17.1 / max 70.7 ms, GPU busy 1.1 ms per present
NVIDIA GeForce RTX 5090, NVIDIA 617.42, Windows 10.0.26200, 3840x2160, commit
e21aa4cabd, dropped: noSlowest frames (largest slices inside each, inclusive)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): VideoOut.Flip 16.2, VideoOut.Flip.vblank_wait 15.2, Driver.WorkerWait 10.5, Driver.WorkerWait.idle_and_completion_poll 10.5, Driver.Packet 5.9, Graphics.Draw 3.9, Graphics.Draw.draw_and_resource_release 3.6, Driver.Packet.dispatch 2.2, Driver.Packet.draw 2.1, Driver.Draw 2.1
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
Where the time went before the change (timers inside
evict, 10 s window, light scene): total 3,340 ms,blank()2,979 ms, VirtualProtect 74 ms, views.erase 113 ms, copy of written pages 31 ms; 1,029,031 pages scanned, 26,670 written. Scanning the same pages a second time took 181 ms, and an SSE2blank()changed nothing, so the cost is the first-touch faults. With the change: mutations holding the registry mutex 7.1 s -> 1.5 s per 10 s, lookups waiting on it 4.4 s -> 0.47 s,evict0.53 s per 10 s.The heavy scene later in the game is GPU-bound and I see no difference there.
Tests:
windows_mappings_releasepasses on the rebased branch (CheckTouchedPagesAnyOrder,CheckEvictKeepsWrittenPages, which fails ifRun::resethandling or themarkImportedcall is removed).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)Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T04:00:59Z.
Review: CHANGES_REQUESTED
I found a correctness risk in the write-watch filter:
GetWriteWatchreturns an array of page addresses, but the Windows API documentation does not promise that the addresses are sorted.evictadvancesnextDirtymonotonically as if they are ascending. If the array is in another order, a page that was written can be skipped, so its contents are not saved and can be lost on eviction. Please sort the returned addresses (or use an order-independent page mark structure) and add a regression that supplies addresses out of order.The host-import test also does not demonstrate that
Run::importeddisables filtering: as the PR notes, its test writes through the CPU-mapped run, which is itself visible to write watch. Please add a test where the bytes change through a path that write watch cannot see, or otherwise isolate that guard.The current hosted Conventions check fails with 14 findings: explanatory comments were added in
WindowsMappings.hppandWindowsMappingsRelease.cpp, and the PR description omits the required### What,### Testedand### Checklisttemplate headings. Please remove or relocate those comments according to repository rules and restore the template. Theperflabel also requires pairedaps5-summary.jsonllines, attached traces andtools/trace_digest.pyoutput. The current tables are not those artifacts, and the description says the scene windows are not paired; please provide same-machine captures of the same title and scene on currentmainand this head before claiming the measured improvement.Finally, this branch is based on
439d03d, 33 upstream commits behind currentmain.CONTRIBUTING.mdrequires branches to be based on and kept up to date withmain; please rebase and rerun the focused Windows mapping tests and checks after addressing the above.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T04:08:10Z.
Review: CHANGES_REQUESTED
I rechecked the new head
bdd5dc8bbfd5322d8d7e5249a6db586467289e20. The branch is now based on061cdb66b0; comments/template issues from my prior review are resolved, and current hosted Conventions passes. The implementation and tests still have these blockers:GetWriteWatchresults are consumed as if sorted:nextDirtyonly advances. The API does not promise address order. An out-of-order list can cause a written page to be skipped and its contents lost on eviction. Please make the membership check order-independent and add an unsorted-list regression.base[12 * step + 7], within the watched resident range. That write is visible to write-watch, so this test would continue passing ifmarkImportedwere removed. Please exercise the import path with a write through a separate alias or another path that write-watch cannot observe, and show the filter falls back in that case.perfreport is still incomplete under CONTRIBUTING and the performance label bot: the description says the scene intervals are not paired and that thefirst_presentvalues differ, no traces are attached, and there is notrace_digest.pyoutput. Please provide compliant same-machine/same-scene captures on main and this change, with both traces and digests, or remove the performance claim/label through the repository’s normal process.The reported focused Windows tests are useful, but hosted Windows is still running; Linux and specialized jobs are skipped. I independently ran
git diff --checkandpython3 tools/check_conventions.py --base refs/review/mainsuccessfully. The current-main merge preview is clean, and the seven commits since the branch base do not touch the changed files. I’m keeping changes requested because of the correctness and required performance-evidence gaps above.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T09:19:47Z.
Review: CHANGES_REQUESTED
I rechecked the updated performance material against the current PR description and branch. The correctness fixes for unordered
GetWriteWatchresults and the reset/import fallback now have regressions, and the attached traces/digests in the comments are useful. Two required items still prevent approval:trace_digest.pyoutput, while the later comments attach frame-window traces. The performance rule requires the pairedaps5-summary.jsonllines, traces and digest in the PR report. Please consolidate the actual evidence into the description and remove the stale claim.mainb4b0c220a98cwith this head, while currentmainhas advanced toa444a18d93640c49a5504758b67b1ed0a553cc73; this branch has not changed sinced9bfa99d7f. Please rebase/update to currentmain, rerun the focused Windows mapping tests, and capture the same title/scene/machine on the updatedmainand rebased head before claiming the performance result.The focused Windows mapping tests and hosted matrix reported for
d9bfa99d7fpass, but they do not cover the latest base or repair the report's stale description. I am keeping changes requested pending those items.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T10:11:17Z.
Review: APPROVED
Rechecked exact head
e21aa4cabddf. The change now treatsGetWriteWatchresults as an unordered set, and only filters pages for a run whose write-watch state is still complete; reset, import, and API-failure cases use the full-scan fallback. The added tests cover scrambled address order, eviction retention, cleared write-watch on imported memory, and reset behavior. Currentmainhas a clean merge preview and no files changed on both sides since the capture base. Current-main conventions andgit diff --checkpass; the hosted Windows and policy checks pass, while Linux and specialized jobs are skipped.The PR description now contains the paired
main/change summaries, same-title/same-session capture details, performance comparison, both digests and attached traces. The reported 180-frame comparison is onmain2227f1366188and the rebased PR heade21aa4cabddf; the PR files have not changed on current main since that base. I did not independently rerun the Windows tests or capture. The earlier correctness, report-format and base-sync blockers are addressed; approved.