fix(agc): record CPU stores to pages that leave the write watch during a capture #22028
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/22028"
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-17T02:14:27Z.
What
A frame capture lost CPU stores to guest pages that left the write watch while it recorded.
agc_driver_frame_replay_rewind_copyfails onmainabout 1 run in 10 under load because of it. The capture misses the test'smemset, records no memory delta at that flip, and the replay reads the wrong bytes.The race:
DMA_DATAcopy runsCopyBufferon the queue thread, which imports the source buffer as host memory.GuestMemory::ImportWatched(..., staysWatched=false)takes the pages out of the write watch.CollectWrittenBlocksnever lists the page, so the recorder writes no delta for it.This was documented debt ("Pages that leave the write watch during a frame capture", and the limit that only write-watched memory records CPU stores).
The change:
SetLeftWatchCompare), the unwatch path hashes each page before its final write walk and unregister, and keeps the hash. A page that leaves the watch without that step is marked unknown.ChangedComparedPageshashes the kept pages again and lists those that changed or are unknown. A page that becomes watched again gets one last compare and is then dropped.CollectWrittenBlockswalks the watched pages of partly watched blocks. These blocks were skipped before, so a store to the watched page of a block with an unwatched neighbour was lost too.The cost applies only while a capture records: one hash per page that left the watch, plus hashing those pages once per delta. Nothing changes when no capture is active.
The debt entry for pages leaving the watch is removed, the entry for unwatched memory is rewritten to the remaining limit, and the capture paragraph in
BUILD.mdis updated.Overlap with #15136 (draft). #15136 keeps the debt entry for pages that leave the write watch. For part 3 it takes a different route: a 64 KiB block that holds an unwatched page no longer counts as watched, so the next delta reads and hashes the whole block. This PR instead walks the block's watched pages and compares the unwatched ones. Neither PR duplicates the other's main change: this one records the stores that #15136's entry still lists as lost. The two conflict in that debt entry and in
CollectWrittenBlocks. Whichever lands second rebases, and if #15136 lands first, part 3 here can be reduced to its approach.Tested
Linux (Ubuntu 24.04, GCC 15, lavapipe, Mesa llvmpipe LLVM 20.1.2).
New test
agc_driver_frame_replay_store_after_unwatch(tests/FrameReplayState.cpp):ImportWatched(..., staysWatched=false)path.Results:
mainthe test fails ("the written byte is in no memory delta"). With the change it passes.rewind_copystress under load (ctest at -j12 beside pm4, blocking, concurrent and fence_reuse):mainfails 13 of 120 runs; this branch fails 0 of 150.maine4cee4952bagainst heada9c0d373f0merged onto it,rewind_copyalone under CPU load, 60 runs per GPU and build.mainfails 9 of 60; this branch, 0 of 60.mainfails 12 of 60; this branch, 0 of 60.mainare the missing memory delta at the flip after thememset.main's push build for9b262cc4ee(run 44195405674).frame_replay,write_tracking,guest_memory,native_depthandpm4tests pass, 95 of 95.main802a0aeb2d: theframe_replay,write_trackingandnative_depth_clearstests pass, 52 of 52.Fork CI on this head 3f9715df6e (run 43996785867):
main802a0aeb2d: 51 of 51), including the newstore_after_unwatch. No VUID lines.agc_driver_frame_replay_cold_pipelines, also fails on the base.Fork CI on 168677615f, the same change before its rebase onto
main802a0aeb2d(run 43938198092):main's sanitizer jobs):store_after_unwatchpasses. The 11 replay tests that fail are all inmain's own TSan failing set (control run 43906132257).python3 tools/check_conventions.py --base origin/mainpasses.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)🤖 Generated with Claude Code
Summary by CodeRabbit
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-17T08:15:52Z.
Review: COMMENTED
Reviewed — no blocking findings
Nothing blocks this; the points below are optional.
What I ran
a9c0d373f0deb1b43d96640da616f2ac428e97ac, basemain.repo_read.py):Results
Excluded by rule: none (
files.jsonlists no exclusions).Findings
Blocking
None.
Non-blocking
core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp:2440-2496(ChangedComparedPages): every memory delta hashes every compared page and takes the tracker lock, even when nothing changed. Large unwatched ranges (the debt file names imports of 3 to 4.5 GiB) make this a per-delta cost, and the description does not measure it.Sources
GuestMemory.cpp:2526-2545andGuestMemory.cpp:2506-2516ata9c0d373f0de.Not verified
ChangedComparedPagesdrops a page's compare entry under the tracker lock once the page is covered again. A CPU store made while the page was unwatched, after the hash and before that rewatch, may never reach a delta. The rewatch stamps (rewatchStamps) sit in the_WIN32block (GuestMemory.cpp:2539). Whether the Linux path stamps those blocks was not checked. The new race test covers an unwatch during the compare, not a rewatch.#ifdef _WIN32paths in the new code were checked only as diff text.agc_driver_frame_replay_cold_pipelinesfailing on base too; the stress run at 13 of 120 onmainand 0 of 150 on the branch) come from the description. They were not run here.docs/dev/BUILD.mdanddocs/dev/debt/libSceAgcDriver.mdwere checked as diff text only.To get this to a good state
Nothing is required.
Optional: measure the per-delta hashing cost on a title with large unwatched imports, and confirm the Linux rewatch path stamps the blocks.
Imported from boykopovar/AnyPS5. Originally posted by kurexw on 2026-11-17T09:44:02Z.
Review: COMMENTED
Tested on macOS 27.2 on an Apple M4. This is an x86-64 build under Rosetta with Apple clang 21.0.0, on MoltenVK 1.4.2 from the LunarG Vulkan SDK 1.4.363.1.
Tested at
a9c0d373f0, which includes the later commit "decide compared-page erasure under the write-watch lock". I testedda4bb852eafirst, with the same results.The new tests don't run on macOS.
agc_driver_frame_replay_store_after_unwatchandcompared_unwatch_raceare added only to theWIN32 OR Linuxmode list inagc_driver_frame_replay_state_tests.cmake, as are the other capture modes. On macOS ctest registers just 9agc_driver_frame_replay_*tests, and neither new test is among them. BUILD.md also describes frame capture as Windows and Linux only. So on a Mac this can only be a regression check of the sharedGuestMemory.cppandFrameRecorder.cppchanges.Method. The base was
mainata72186d3cfplus #22056 and #22074, which fix the macOS build of two other targets; both have since merged. I built the base and this PR merged onto it (a clean merge) in two separate build directories. I ran them alternately, base then PR, three times each, withANYPS5_REQUIRE_VULKAN=1,ANYPS5_NO_SHADER_CACHE=1,VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validationandVK_LOADER_DEBUG=layer:agc_write_trackingagc_driver_native_depth_publicationwas not found but was requestedVUID-linesBoth failures happen on the base too, so they come from macOS, not from this PR:
agc_write_tracking:write tracking test failed: a page only an import window stamped as a possible CPU touch was not listed.agc_driver_native_depth_publication:AGC graphics: guest writes raced native multisample depth publication.All runs gave the same results. I found no regression on macOS. The fix itself isn't exercised here, since the capture path it changes is built only for Windows and Linux.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T13:17:28Z.
Review: APPROVED
Reviewed — looks good
The change at head
3a479a24f538e6256ba52eaa8fe022ad763712a4addresses capture of stores to pages that leave the write watch and adds focused coverage; no verified blocking defect was found in the inspected code.What I ran
No commands were run. I read the prepared review bundle and inspected changed code at the pinned head. I did not run the build or tests.
Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
The reported Linux, Windows, GPU, stress, mutant, and CI test results in the PR description were not independently verified. No build or tests were run. All changed files listed in the manifest were read:
GuestMemory.hpp,FrameRecorder.cpp,GuestMemory.cpp,FrameReplayState.cpp,agc_driver_frame_replay_state_tests.cmake,docs/dev/BUILD.md, anddocs/dev/debt/libSceAgcDriver.md. No changed files were excluded by the prepared file listing.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-26T14:35:23Z.
Review: APPROVED
Reviewed — looks good
Nothing blocks this change; the implementation and regression coverage support the described capture behavior.
What I ran
Reviewed head
8a6bd8d38b307be7834b1da338bfe25b9ff7f409using the prepared review bundle andrepo_read.py. No commands or tests were run by me; reported test and CI results are author supplied.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
No tests were run independently. Runtime behavior on Windows and physical consoles was not verified. Read:
GuestMemory.hpp,FrameRecorder.cpp,GuestMemory.cpp,FrameReplayState.cpp,agc_driver_frame_replay_state_tests.cmake,docs/dev/BUILD.md, anddocs/dev/debt/libSceAgcDriver.md. No changed files were excluded; all seven reviewed files were listed as included in the prepared manifest.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by coderabbitai[bot] on 2026-11-26T14:39:48Z.
Review: COMMENTED
Actionable comments posted: 3
29abb538-16f4-4ca8-bd95-2fdc4361bc59Reviewing files that changed from the base of the PR and between
3dd06de43cand8a6bd8d38b.core/libs/prx/libSceAgcDriver/Execution/include/GuestMemory.hppcore/libs/prx/libSceAgcDriver/Execution/src/FrameRecorder.cppcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/tests/FrameReplayState.cppcore/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_frame_replay_state_tests.cmakedocs/dev/BUILD.mddocs/dev/debt/libSceAgcDriver.mdIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.