perf(agc): an untracked comparison memo holds until memory it covers is forgotten #6187

Closed
TheAggie wants to merge 2 commits from upstream-pr/6187 into main
TheAggie commented 2026-10-15 01:19:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by TheAggie on 2026-10-15T01:19:25Z.


What

A storage image over guest memory the write watch does not cover (on Windows, every host-pointer import of the guest mapping, #1441) is checked by comparing its saved bytes with guest memory (StorageTexture::compareUntracked). The comparison is memoized under a stamp, and one part of that stamp is GuestMemory::ForgetSerial, a single counter that moves on every ForgetPages call anywhere in guest memory. A title that maps and frees memory all the time therefore drops every untracked image's memo, and each refresh compares the whole surface again.

In RetroArch's Flycast core (PPSA99169) on Windows, that is the 1.7 MB render target compared about 26,000 times per 10 s, about 44 GB of memcmp, a third of the draw committer's busy time. Counting which part of the stamp changed between compares: the forget serial in about 255,000 cases, the epoch or driver-store stamp in under 4,000 each, the write note in none.

  • ForgetPages also raises a per-block stamp: a fixed table of 65,536 slots, one per 64 KiB block of the forgotten range (block index modulo the table), each holding the newest forget serial that touched it. A range wider than the table raises one shared stamp instead.
  • GuestMemory::ForgetStamp(address, bytes) is the newest of those over a range. A collision between blocks only makes it newer, so the memo is dropped too often, never too rarely.
  • compareUntracked stamps with ForgetStamp of the surface instead of the global serial. A forget in progress (odd ForgetSerial) still disables the memo, as before.

No runtime switch: the memo rule is the same, only the forget part of the stamp is now per range.

Tested

Windows 11, NVIDIA GeForce RTX 5070 Ti, MinGW-w64 GCC 15.2.0, on main 8cd937e29; rebased onto main 92e50418f (only a CMake test list conflicted), where the full suite with #6192 on top passes 1277 of 1296, 14 skipped, failing only the known tests below.

  • ctest: 1232 of 1248 pass, 14 skipped. guest_call_trace_abort and windows_backtrace_report fail, and fail the same way on plain main here. agc_driver_prepared_graphics_prebuild_0/_1 and undeliverable_host_exception fail under my test runner because it skips their environment changes and pass pattern; run with those, they pass.
  • New test agc_untracked_forget (agc_driver_recorder_tests --untracked-forget-only): an untracked surface in its own allocation and a second untracked allocation. After the first refresh, a second refresh in the same epoch does not compare; invalidating the other allocation does not make the refresh compare; invalidating the surface's own memory does. With the old stamp (global ForgetSerial) the test fails with "forgetting memory the surface does not cover made its refresh compare again".
  • RetroArch (PPSA99169), Flycast, Crazy Taxi attract demo, ANYPS5_PRESENT_MODE=immediate, unprofiled. Measured on a local stack: main a453108cf plus local frontend support, #4943, #5906 and #5609, a port of #1201's pipelined draw committer, and two local changes (GPU-only lease pins, DCC key proofs in unwatched memory). Same stack without and with this change, two runs each: audio produced at 52.5% and 52.9% of real time against 63.1% and 62.6%. The committer's render target phase went from 10.4 to 5.25 µs per draw. I have no measurement of main alone with only this change.
  • Linux is untested; on Linux most of guest memory is write-watched, so this path is rare there.

Related: #1441 introduced the compared imports on Windows. #5906 removes another Windows cost of the same exclusion (draw snapshots). #4448 attacks the same compare cost from the other side on i915, by keeping more surfaces watched (a smaller import window, no self-import of a watched surface); this PR only changes when the memo of a surface that stays untracked is dropped, so the two complement each other. Both add a function right after ForgetSerial in GuestMemory.hpp/.cpp, which is a trivial conflict for whichever lands second. #6060 keeps Windows imports of private write-watched memory on GetWriteWatch when a start-up probe allows it; memory it keeps watched never reaches this path, so this PR matters for what stays untracked (imports through views, devices whose probe fails). I have not measured how much of the Flycast workload #6060 would cover.

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 in TechnicalDebt
  • 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
  • AI-assisted: yes. Written with Claude Code under my direction.

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4866786919 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6187). Originally posted by [TheAggie](https://github.com/TheAggie) on 2026-10-15T01:19:25Z. --- ### What A storage image over guest memory the write watch does not cover (on Windows, every host-pointer import of the guest mapping, #1441) is checked by comparing its saved bytes with guest memory (`StorageTexture::compareUntracked`). The comparison is memoized under a stamp, and one part of that stamp is `GuestMemory::ForgetSerial`, a single counter that moves on every `ForgetPages` call anywhere in guest memory. A title that maps and frees memory all the time therefore drops every untracked image's memo, and each refresh compares the whole surface again. In RetroArch's Flycast core (PPSA99169) on Windows, that is the 1.7 MB render target compared about 26,000 times per 10 s, about 44 GB of `memcmp`, a third of the draw committer's busy time. Counting which part of the stamp changed between compares: the forget serial in about 255,000 cases, the epoch or driver-store stamp in under 4,000 each, the write note in none. - `ForgetPages` also raises a per-block stamp: a fixed table of 65,536 slots, one per 64 KiB block of the forgotten range (block index modulo the table), each holding the newest forget serial that touched it. A range wider than the table raises one shared stamp instead. - `GuestMemory::ForgetStamp(address, bytes)` is the newest of those over a range. A collision between blocks only makes it newer, so the memo is dropped too often, never too rarely. - `compareUntracked` stamps with `ForgetStamp` of the surface instead of the global serial. A forget in progress (odd `ForgetSerial`) still disables the memo, as before. No runtime switch: the memo rule is the same, only the forget part of the stamp is now per range. ### Tested Windows 11, NVIDIA GeForce RTX 5070 Ti, MinGW-w64 GCC 15.2.0, on main `8cd937e29`; rebased onto main `92e50418f` (only a CMake test list conflicted), where the full suite with #6192 on top passes 1277 of 1296, 14 skipped, failing only the known tests below. - `ctest`: 1232 of 1248 pass, 14 skipped. `guest_call_trace_abort` and `windows_backtrace_report` fail, and fail the same way on plain main here. `agc_driver_prepared_graphics_prebuild_0`/`_1` and `undeliverable_host_exception` fail under my test runner because it skips their environment changes and pass pattern; run with those, they pass. - New test `agc_untracked_forget` (`agc_driver_recorder_tests --untracked-forget-only`): an untracked surface in its own allocation and a second untracked allocation. After the first refresh, a second refresh in the same epoch does not compare; invalidating the other allocation does not make the refresh compare; invalidating the surface's own memory does. With the old stamp (global `ForgetSerial`) the test fails with "forgetting memory the surface does not cover made its refresh compare again". - RetroArch (PPSA99169), Flycast, Crazy Taxi attract demo, `ANYPS5_PRESENT_MODE=immediate`, unprofiled. Measured on a local stack: `main` `a453108cf` plus local frontend support, #4943, #5906 and #5609, a port of #1201's pipelined draw committer, and two local changes (GPU-only lease pins, DCC key proofs in unwatched memory). Same stack without and with this change, two runs each: audio produced at 52.5% and 52.9% of real time against 63.1% and 62.6%. The committer's render target phase went from 10.4 to 5.25 µs per draw. I have no measurement of `main` alone with only this change. - Linux is untested; on Linux most of guest memory is write-watched, so this path is rare there. Related: #1441 introduced the compared imports on Windows. #5906 removes another Windows cost of the same exclusion (draw snapshots). #4448 attacks the same compare cost from the other side on i915, by keeping more surfaces watched (a smaller import window, no self-import of a watched surface); this PR only changes when the memo of a surface that stays untracked is dropped, so the two complement each other. Both add a function right after `ForgetSerial` in `GuestMemory.hpp`/`.cpp`, which is a trivial conflict for whichever lands second. #6060 keeps Windows imports of private write-watched memory on `GetWriteWatch` when a start-up probe allows it; memory it keeps watched never reaches this path, so this PR matters for what stays untracked (imports through views, devices whose probe fails). I have not measured how much of the Flycast workload #6060 would cover. ### 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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes. Written with Claude Code under my direction. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
nicolasfalesy commented 2026-10-16 08:52:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by nicolasfalesy on 2026-10-16T08:52:51Z.


Tested on Linux (Arch), c++ (GCC) 16.2.1 20260810, Release, BUILD_TESTING=ON, main at 5d9dcc42d2ec5401aebd089028b8d4d7410a77b4. GPU tests ran on lavapipe (Mesa 26.2.2, llvmpipe with LLVM 22.1.8).

agc_untracked_forget (this PR's test, agc_driver_recorder_tests --untracked-forget-only) crashes on main plus this PR (2 of 2), and also in a combined build of many PRs (3 of 3):

agc_untracked_forget ...***Exception: SegFault

The fault is inside lavapipe on a worker thread, not in AnyPS5 code:

Thread 2 "agc_driver_reco" received signal SIGSEGV, Segmentation fault.
#0  ?? () from libvulkan_lvp.so
...

It is not seen on an Intel Arc GPU, where the same build passes. So the test probably leaves memory that a queued command still reads (or writes) after forget, which real GPUs tolerate and lavapipe does not. CONTRIBUTING says CI runs the Vulkan tests on lavapipe. I did not find which access it is.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6211825461 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6187#issuecomment-6211825461). Originally posted by [nicolasfalesy](https://github.com/nicolasfalesy) on 2026-10-16T08:52:51Z. --- Tested on Linux (Arch), `c++ (GCC) 16.2.1 20260810`, Release, `BUILD_TESTING=ON`, `main` at `5d9dcc42d2ec5401aebd089028b8d4d7410a77b4`. GPU tests ran on lavapipe (Mesa 26.2.2, llvmpipe with LLVM 22.1.8). `agc_untracked_forget` (this PR's test, `agc_driver_recorder_tests --untracked-forget-only`) crashes on `main` plus this PR (2 of 2), and also in a combined build of many PRs (3 of 3): ``` agc_untracked_forget ...***Exception: SegFault ``` The fault is inside lavapipe on a worker thread, not in AnyPS5 code: ``` Thread 2 "agc_driver_reco" received signal SIGSEGV, Segmentation fault. #0 ?? () from libvulkan_lvp.so ... ``` It is not seen on an Intel Arc GPU, where the same build passes. So the test probably leaves memory that a queued command still reads (or writes) after `forget`, which real GPUs tolerate and lavapipe does not. CONTRIBUTING says CI runs the Vulkan tests on lavapipe. I did not find which access it is. AI-assisted: yes
adriadam10 commented 2026-10-18 19:16:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-18T19:16:52Z.

Review: CHANGES_REQUESTED


The Linux build of this head (run 39153907046, lavapipe) fails on agc_untracked_forget with ***Exception: SegFault after 2 s; it passes on main, so it comes from the new test. It is the same crash @nicolasfalesy reported on 2026-10-16 (inside lavapipe, on a worker thread), now reproduced in CI, so it blocks the merge.

  • core/libs/prx/libSceAgcDriver/tests/Recorder.cpp:4102 (untrackedForgetTests): the test host-imports the aligned_alloc blocks, runs Refresh() on a StorageTexture, then tears down (Unregister removes the allocations and calls HostImportFor again). Please find which step lavapipe faults in (a backtrace on lavapipe) and make the teardown safe: wait for the device/queue to be idle before destroying the image and releasing the import, and keep the blocks alive until then. If the test does not need a real Vulkan import to prove the memo rule, testing GuestMemory::ForgetStamp directly would also avoid it.

The stamp logic itself is unchanged from the last review and fine; once the test passes on lavapipe this can go back to the queue.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5564557621 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6187#pullrequestreview-5564557621). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-18T19:16:52Z. **Review:** CHANGES_REQUESTED --- The Linux build of this head (run 39153907046, lavapipe) fails on `agc_untracked_forget` with `***Exception: SegFault` after 2 s; it passes on `main`, so it comes from the new test. It is the same crash @nicolasfalesy reported on 2026-10-16 (inside lavapipe, on a worker thread), now reproduced in CI, so it blocks the merge. - `core/libs/prx/libSceAgcDriver/tests/Recorder.cpp:4102` (`untrackedForgetTests`): the test host-imports the `aligned_alloc` blocks, runs `Refresh()` on a `StorageTexture`, then tears down (`Unregister` removes the allocations and calls `HostImportFor` again). Please find which step lavapipe faults in (a backtrace on lavapipe) and make the teardown safe: wait for the device/queue to be idle before destroying the image and releasing the import, and keep the blocks alive until then. If the test does not need a real Vulkan import to prove the memo rule, testing `GuestMemory::ForgetStamp` directly would also avoid it. The stamp logic itself is unchanged from the last review and fine; once the test passes on lavapipe this can go back to the queue.
oneandonlydean commented 2026-10-20 15:38:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-20T15:38:37Z.


@TheAggie a nudge on this one, because #6192 is stacked on it and shows a solid gain in a tester's run (p50 about 9% better). What blocks it is the maintainer's 18 Oct change request: this PR's own new test, agc_untracked_forget, segfaults on Linux CI after about 2 s on lavapipe (run 39153907046), in untrackedForgetTests in Recorder.cpp. It passes on main, and nicolasfalesy reproduced the crash on 16 Oct. Once that's fixed (the remaining conflict is only the CMake test list), we can run CI on both PRs.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6294757679 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6187#issuecomment-6294757679). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-20T15:38:37Z. --- @TheAggie a nudge on this one, because #6192 is stacked on it and shows a solid gain in a tester's run (p50 about 9% better). What blocks it is the maintainer's 18 Oct change request: this PR's own new test, `agc_untracked_forget`, segfaults on Linux CI after about 2 s on lavapipe (run 39153907046), in `untrackedForgetTests` in `Recorder.cpp`. It passes on main, and nicolasfalesy reproduced the crash on 16 Oct. Once that's fixed (the remaining conflict is only the CMake test list), we can run CI on both PRs.

Pull request closed

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!6187
No description provided.