perf(agc): read committed guest pages directly on Windows #14799
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/14799"
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 chitters2004 on 2026-11-01T18:13:17Z.
What
On Windows every committed guest copy and compare goes through
ReadProcessMemory, added in4201b8212aso that a page released during the read counts as uncommitted instead of faulting. Revalidation issues one system call per compare and then compares a second copy of the bytes. In GTA V (PPSA04263)ZwReadVirtualMemorywas 12% of the committer thread's samples, and the committer is the thread that bounds the frame: it waited for only 12% of the steady window.Ranges the guest arena owns, outside recorded host regions, are now read by leaf copy and compare routines in
GuestArena.cpp. Hosts with AVX2 take 64 bytes per pass, with 16-byte and byte tails; other hosts keep the 16-byte loop. A fault on one of their source loads resumes at a failure return, and the read falls back to the existing page by page path, so released pages keep their behaviour: zero in a copy, left out of a compare. Host regions and addresses outside the arena keepReadProcessMemory, which covers the host heap case of4201b8212a. Linux is unchanged.GuestArenaRecoverRead_nid_no_patchruns first inReportCrashandReturnBreakHandler. It takes only read access violations and in-page errors, at an exact source load, with the fault address inside the interval being read. A fault on the destination or expected buffer, or anywhere else, still reaches the crash path.Guard pages are not recovered.
AccessibleandCommittedRangesexclude a guarded page on a fresh query, so a direct read only meets one through stale page state, which means a guard was set on arena memory without going throughGuestAllocations. No non-test code setsPAGE_GUARD. OS stack guards are out of reach: a stack inside the arena bounds at startup is in_hostRegions, later stacks cannot land in the reserved placeholders, and guest threads run on_beginthreadexstacks. That relies on Windows honouring reservations. Such a guard ends as an unhandled exception; it may not print the usualFATALreport. The existingCommittedReadMappingcase that set a raw guard now announces it throughGuestAllocations::Mutation, the contract real code follows.Tested
Windows 11, Ryzen 7 5800X, RTX 3080, GTA V (PPSA04263), save at the first autosave, standing still, 3840x2160. Main
e0caf13600and this PR79ee45a6e2each with #11580 applied, because main aborts at Story load without it (#11425), giving commitsb9c59ab4094aandcfbd1938d453. Boots interleaved. Frame time is the median from videoout present stamps, +20 to +140 s after the player stands still.About 19 ms, 10.8%, about 5.6 to 6.3 fps. GPU busy per present stayed at about 42 ms. No fatal in any boot. The previous head of this PR, without the wider loops and on
f2d874c1fd, read 180.0 and 161.0 ms over four boots a side.Warm captures as the form describes: the scene ran once, the title was restarted on that cache, same save and input, one extra boot a side after the timing boots,
APS5_TRACE_SECONDS=240:20. These replace the previous captures: an input script left over from the earlier fix capture was still tapping during the earlier main capture, so that one should not be used. The four boots above are the claim.Deviations from the form: the log is redirected and frames are dumped on every run, because the script that drives the menus reads both, so the two sides pay the same cost. A d-pad tap every 15 s holds the normal camera, because the idle cinematic camera otherwise adds about 45 ms at +72 s.
APS5_ADDRESS_COPY_MAX_MIB=512is set on both sides. CPU and GPU utilisation loggers (typeperf,nvidia-smi) ran during every boot. The timing boots keepAPS5_TRACE_SUMMARY_EVERY=10and no trace. The order was a warm-up boot of this PR, then this PR, main, this PR, main; no run was thrown away. The first main boot waited about 50 s longer at the title because the menu script missed a press; the frame window starts when the player stands still, so it is unaffected. The game window had focus for the timing boots, but only 22 of 56 samples in the capture of this PR and 52 of 55 in the capture of main, which if anything favours main. Both traces report dropped events.aps5-summary.jsonllines, warm capture main and this PR, then one 10 s window of a timing boot of each:Traces: warm main, warm this PR. Raw files of every boot, digests, comparison and benchmark source and results: pr14799-evidence-v3.zip.
tools/trace_digest.pysummaries:seconds 240:20 from present 3925: 109 presents, frame p50 183.9 / p90 190.4 / p99 200.0 / max 240.3 ms, GPU busy 41.7 ms per present
NVIDIA GeForce RTX 3080, NVIDIA 621.01, Windows 10.0.26220, 3840x2160, commit b9c59ab4094a, dropped: yes
Slowest frames (largest slices inside each, inclusive)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): Graphics.Draw 75.4, Graphics.Draw.draw_and_resource_release 60.0, batch 41.6, Driver.Packet 40.1, Driver.Packet.draw 31.1, Driver.Draw 30.8, VideoOut.Flip 14.3, VideoOut.Flip.present 14.2, Driver.Present 14.2, Driver.Present.render_fence_wait 13.7
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
seconds 240:20 from present 4009: 128 presents, frame p50 156.1 / p90 163.0 / p99 180.3 / max 214.9 ms, GPU busy 41.5 ms per present
NVIDIA GeForce RTX 3080, NVIDIA 621.01, Windows 10.0.26220, 3840x2160, commit cfbd1938d453, dropped: yes
Slowest frames (largest slices inside each, inclusive)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): Graphics.Draw 52.0, batch 41.3, Graphics.Draw.draw_and_resource_release 38.5, Driver.Packet 29.1, Driver.Packet.draw 21.8, Driver.Draw 21.6, VideoOut.Flip 14.2, VideoOut.Flip.present 14.1, Driver.Present 14.1, Driver.Present.render_fence_wait 13.6
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
Benchmark, one pinned thread, median of 21, GB/s, equal data, 4 KiB / 256 KiB / 4 MiB / 64 MiB:
ReadProcessMemory+memcmpmemcmpThe 4 KiB and 4 MiB results varied between runs, so ranges are given. Copy,
ReadProcessMemorythen this PR: 5.3 then 107-145, 27 then 60-67, 26-29 then 38-63, 11 then 14 GB/s.memcpyreaches about 21 GB/s at 64 MiB, ahead of this loop at that size only. Prefetching gave no gain and was left out.Fault cost: a local counter build, not part of this PR, recorded 0 recovered faults in about 1.44 million guarded reads on this scene. There is no data from a streaming-heavy title.
OwnedByGuesttakes no lock:Containsis arithmetic andOverlapsHostRegionwalks_hostRegions, which on Windows is only written at startup. It held 80 entries here, 46.6 ns a call in a standalone timing, about 0.03 ms a frame at about 720 calls a frame.Tests:
agc_driver_committed_read_mapping(released page, mismatch beside a released page, straddling copy and compare across the 64-byte, 16-byte and byte loops, concurrent decommit),windows_guarded_read_wild_destination,_expected,_outside,windows_backtrace_*,windows_crash_*,agc_driver_guest_memory_lifetime*: 16 of 16 pass. Red controls: with recovery disabled the mapping test segfaults; with the classifier's address and instruction checks removed,windows_guarded_read_wild_expectedand_outsidefail.Paired gate on
e0caf13600and79ee45a6e2withANYPS5_REQUIRE_VULKAN=1,ANYPS5_NO_SHADER_CACHE=1,VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation,ctest -R "committed_read_mapping|agc_driver_guest_memory_lifetime" -V: all pass on both, no missing-layer line, noVUID-on either side.Not tested: Linux (no change there), the non-AVX2 path as the main loop on a host without AVX2, AMD or Intel GPUs locally, Demon's Souls or any title but GTA V, and the unannounced guard path at run time. Overlap:
pr_overlapreports shared files only.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.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-01T18:18:57Z.
Review: CHANGES_REQUESTED
Reviewed — changes requested
The performance report is incomplete because the two required capture trace files are not attached to this PR.
What I ran
Reviewed head
63dce0027a2bea23047c800148ab20eaafe74c8cagainst basef2d874c1fdccf781c0fa5d86619bb5dd8894b958. The stack check found a clean merge preview. Environment: Linux x86_64, Python 3.11.2, GCC 12.2.0. I did not build or compile the Windows changes.Results
Unedited local check output
git diff --check:Conventions:
Precheck:
The precheck's two helper launches failed because those local review-helper scripts are not part of the isolated PR checkout; I ran the ABI audit from the main workspace successfully. The test-quality warning was resolved by inspecting the CMake
file(GLOB ... tests/execution/*.cpp)registration and the test target.ABI audit:
Test-quality heuristic:
Stack check:
Negative control
Not run. The tests are Windows-specific and their exact-head binaries are unavailable without building.
Findings
Blocking
perfreport says the two.json.gztraces “are not attached yet,” and no later PR comment contains them. The PR template requires the baseline and change traces, while the performance report form marks the trace attachments as required. Please attach the exact baseline and candidate traces so the captures and pasted digests can be checked against the reported commits.Non-blocking
Sources
Not verified
To get this to a good state
Attach both capture trace files. I will check that they match the supplied summary lines and rerun the same no-build checks on the updated head.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-01T18:52:03Z.
Review: COMMENTED
Re-checked — traces verified, CI pending
The new evidence supplies both capture traces and reproducible digests; six macOS jobs remain pending, so I am holding approval.
What I ran
Re-checked the new evidence for unchanged PR head
63dce0027a2bea23047c800148ab20eaafe74c8cagainst basef2d874c1fdccf781c0fa5d86619bb5dd8894b958. Environment: Linux x86_64, Python 3.11.2; no compiler or build was used. The earlier source review still applies because the PR head is unchanged.Results
Commands and unedited output
unzip -t /tmp/pr14799-evidence-v2.zip | tail -1sha256sum /tmp/pr14799-baseline.json.gz /tmp/pr14799-evidence/aps5-trace-a6111d1f071c-31820.json.gz /tmp/pr14799-candidate.json.gz /tmp/pr14799-evidence/aps5-trace-ee8448fc2ee5-29424.json.gzRerun digest output, baseline and candidate summary lines:
diff -q --strip-trailing-cr /tmp/pr14799-evidence/digest-M3T-rl5.txt /tmp/pr14799-run-main.txtdiff -q --strip-trailing-cr /tmp/pr14799-evidence/digest-GnT-rl4.txt /tmp/pr14799-run-fix.txtBoth commands produced no output and exited successfully.
gh api repos/boykopovar/AnyPS5/pulls/14799 --jq '[.state, .head.sha, .base.sha] | @tsv'gh pr checks 14799 --repo boykopovar/AnyPS5Negative control
Not applicable to this evidence-only follow-up: the source and tests did not change, and I did not run a build or modify the code under test.
Findings
Blocking
None found in the newly supplied trace and digest evidence. The missing-trace finding from my earlier review is resolved.
Non-blocking
Approval remains pending the six hosted macOS jobs. I did not independently reproduce the performance run on the stated PC or game scene.
Sources
trace_digest.pymatches summary records to trace filenames at this headtrace_digest.pylimits its digest to the summary window at this headNot verified
The physical PC, title scene, and timing boots were not reproduced here; those remain author-supplied measurements. Six hosted macOS checks are pending. Linux was skipped by CI.
To get this to a good state
No additional trace evidence is needed. Once the pending macOS checks finish, I will re-check the exact head and approve if they pass; if the head changes, I will rerun the same integrity and digest commands on the new evidence.
Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-11-01T19:38:31Z.
Review: COMMENTED
Removing a syscall per compare on the committer is the right target, and the recovery is scoped tightly: read faults only, exact source-load RIPs, fault address inside the interval. Three points on cost and coverage that the description does not address.
1. The loops are 16 bytes per iteration.
GuardedCopyandGuardedComparedo onemovdqupair, onepcmpeqb/pmovmskb/cmp/jne, oneaddand one bound check per 16 bytes, with no unrolling and no prefetch. The path they replace wasReadProcessMemoryinto a staging copy plus the CRTmemcmp, which uses AVX2 where available. The GTA V scene is dominated by the syscall, so the net is positive there. For a large compare (a 33–66 MB target, or the 256 MiB swept ranges), a loop at this width can be slower per byte than the old path, and the gain disappears once the syscall is amortized over many pages. Please unroll to 64 bytes per iteration (four independentpcmpeqbresults OR-ed or AND-ed before the branch), and show a microbenchmark ofGuardedCompareagainstmemcmpfor 4 KiB, 256 KiB and 64 MiB. The fault handler needs one more load label per width, which the RIP check already supports.2. The cost of a hit is not measured. Every compare or copy that contains a released page now takes an access violation, runs the vectored handler, and then does the whole read again through the page-by-page
ReadProcessMemorypath. That is strictly slower than main for that range, by one exception dispatch (tens of microseconds on Windows) plus the discarded partial read. The test shows correctness with a thread decommitting a page in a loop, but not how often a title hits it. Please add a counter for the fallback after a fault, and report it for the GTA V scene and for a streaming-heavy title, so we know whether a heap that decommits and recommits pages makes this a regression. If the count is not negligible, remember that a range just recovered as released could be skipped for direct reads until its page state changes.3.
OwnedByGuestruns per call.Arena::Get().ContainsandOverlapsHostRegionare on the fast path for every compare, and the description does not say whether the second takes a lock or walks a list. With 135k proofs per 10 s on this title (see #12921), please confirm it is lock-free and O(1), or state its cost.Coverage. The guard-page case: after the first access to a
PAGE_GUARDpage the OS clears the guard bit and raisesSTATUS_GUARD_PAGE_VIOLATION.ReadProcessMemoryonly failed and left the bit set. The description says nothing in core sets a guard on arena memory, which is fine today. But a guestmprotector stack-growth probe on arena memory would now be consumed by a CPU-side compare and produce an unhandled exception, where main returned a quiet failure. Please state which guest-visible calls can reach a guard bit, or makeOwnedByGuestrefuse ranges whose state has a guard.The measurement itself is clear and reproducible. 8 boots at 180.0 vs 161.0 ms with non-overlapping ranges, and the traces are attached.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-02T10:52:55Z.
Review: APPROVED
Reviewed — looks good
At head
79ee45a6e2a60846373a1e03708b26f5214ab35a, the Windows guarded-read path, fallback behavior, regression coverage, and attached performance evidence are consistent with the reviewed change.What I ran
Reviewed all eight changed files against base
b2a47847651b0c1281241c5f6da51b4bdf1c83ae. Environment: Debian Linux 6.1, Python 3.11.2, GCC 12.2.0. No local build or CTest was run. Hosted Windows, relinker, sanitizer, register-context, and write-watch checks passed on this head; Linux was skipped. The performance captures and GPU results were supplied by the author.Results
mainPrecheck table and relevant raw output
git diff --check b2a47847651b0c1281241c5f6da51b4bdf1c83ae...79ee45a6e2a60846373a1e03708b26f5214ab35aThe heuristic's suspected missing test registration is a false positive:
CommittedReadMapping.cppis included by the test target, and the changed Windows cases are registered fromlibc/CMakeLists.txt.Negative control
Findings
Blocking
None found in the reviewed diff.
Non-blocking
Sources
Not verified
To get this to a good state
No changes are required from this review. The Windows and relinker checks passed; Linux coverage remains unverified here.