Skip to content

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

Closed
TheAggie wants to merge 2 commits into
boykopovar:mainfrom
TheAggie:perf/agc-untracked-forget-stamp
Closed

TheAggie wants to merge 2 commits into
boykopovar:mainfrom
TheAggie:perf/agc-untracked-forget-stamp

Conversation

@TheAggie

@TheAggie TheAggie commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

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, perf(agc): find cached draws across moved user-data pointers #4943, perf(agc): reuse draw snapshots across draws that nothing wrote between #5906 and perf(agc): reuse cached draw stages when only buffer base addresses moved #5609, a port of perf(agc): pipeline queue-0 draw commits behind the packet worker #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

@github-actions github-actions Bot added build agc libSceAgc* labels Oct 15, 2026
@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 15, 2026
This was referenced Oct 15, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

@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.

@tibzejoker

Copy link
Copy Markdown
Contributor

GTA V (PPSA04264) data from Windows 11, RTX 4090, i9-13900K, in case a second title helps once the lavapipe crash is fixed.

Build: main 3bbd232 + #9181 + #9159; GTA needs both to stay alive in the open world. Three ~8 min runs (boot, Story resume, open world) with APS5_PROFILE_DRAW=1, without this PR. Per run:

  • [guestmem] forgets: 23.5k to 29.2k, covering 4.7 to 6.1 GB. So ForgetSerial moves about 50 times a second, and every untracked memo is dropped each time.
  • compares: 1.9M to 3.3M calls, 490 to 790 GB, 35 to 53 s.
  • [textures] untracked snapshots: about 15k kept by the hint, 2.9k compared.

GTA looks like Flycast here: frequent forgets in a title with many compared surfaces. Two caveats. I have not split which part of the stamp makes compareUntracked miss. The compare counter also covers more than compareUntracked.

Older context: on a 10-12 base (main 325e0a8 + #2802's memo), 87 % of GTA's untracked memo lookups hit. Most of the misses came from the collect epoch. Main now uses UntrackedHint in place of the epoch for hinted ranges. So the forget serial is probably a bigger share of what is left. That is a guess, not a measurement.

Offer: once the test fix is pushed, I can run GTA open world on main and on main + #6187, alternating, in the same session. I can also count which stamp part changed, as you did for Flycast.

AI-assisted: yes (Claude Code)

@tibzejoker

Copy link
Copy Markdown
Contributor

@TheAggie GTA V numbers for this PR and #6192 together are on #6192: committer 39.3 -> 24.7 µs per commit in the open world, on a port to current main. I left a branch with the two code commits on current main, authored by you: tibzejoker/AnyPS5 port/pr-6192-main. I haven't measured this PR alone.

AI-assisted: yes (Claude Code)

TheBlenDed pushed a commit to TheBlenDed/AnyPS5 that referenced this pull request Oct 25, 2026
…for boykopovar#6187/boykopovar#6192

The per-block walk of untracked texture snapshots (drawcpu r2) walked each run of
watched or hinted blocks with one collect. A run with uncommitted or no-access
pages makes the Windows collector refuse the whole walk, so every block of the run
was hashed ('walk refused', ~490k blocks / 30 GB per 10 s in the open world).
The walk now takes the range's readable runs (DescribeCommitted) and collects
only those pages. The entry keeps the layout it was proved at; a block is clean
only when its clipped readable runs equal the baseline's and no stamp or hint is
newer, so the hash (zeros for unreadable pages) cannot differ. The compare hashes
with the same layout. [texhash] adds 'partly readable' and 'layout changed'.
APS5_NO_UNTRACKED_LAYOUT=1 restores the whole-run walk.
APS5_NO_UNTRACKED_FAST_PROOF=1 turns off boykopovar#6192's stamp proof,
APS5_NO_FORGET_STAMPS=1 makes boykopovar#6187's ForgetStamp the global serial again.

AI-assisted: yes
@EagleBearerYS

Copy link
Copy Markdown
Contributor

@TheAggie the Linux SIGSEGV in agc_untracked_forget (CI run 39153907046) comes from the test's teardown, not from the memo. Every check passes first, and the last line printed is "untracked comparison memo: ok".

  • The test's last Refresh records a detile dispatch with the test's own TextureDetiler, and that batch is still pending when the function returns.
  • The detiler is declared after the Unregister guard, so it's destroyed first, and ~TextureDetiler destroys its pipelines at once.
  • Unregister then removes the allocations, which syncs the recorder. lavapipe runs the pending dispatch with the freed pipeline and crashes on its queue thread.
  • Under gdb with mesa's debug symbols, lvp_DestroyPipeline fires once before the crash, from ~TextureDetiler. The crash is in handle_compute_shader, binding that same pipeline.

Production can't reach it. VulkanDevice owns the only TextureDetiler, and its ~State() waits for the device to go idle before the members are destroyed.

The fix is one test-only commit on your head a049317: fix/6187-untracked-forget-teardown on our fork (bdc31eb31a, tests/Recorder.cpp, +6/−2).

  • A Finish guard declared after the detiler syncs the recorder before the detiler goes away. It's the same idiom the file already uses, and the neighbouring untracked tests get the same effect from their Clear guards.
  • Every check the test makes is kept.

We tested it on a port of this PR and #6192 onto main 2960e9a, on lavapipe. Without the fix the test crashed 20 of 20 times. With it, agc_untracked_forget and agc_untracked_revalidate passed 20 of 20 each. With our #6192 follow-up on top as well, ctest -R '^agc' gave 1,046 passed, 73 skipped, 0 failed. On your head the commit applies without conflicts, but we haven't built it there.

It's yours to take: cherry-pick it or merge the branch.

@fogydes

fogydes commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

stale+rebase

Findings

  1. [verified] Branch freshness & merge conflicts: The PR branch is 13,700 commits behind origin/main (0a2cda921d2830843d1a8e1e793e2b2fa8090547). git merge-tree --write-tree origin/main a049317f3e4d538805b9e81a90d5552d791ab3bf fails with exit code 1 due to conflicts in core/libs/prx/libSceAgcDriver/CMakeLists.txt and core/libs/prx/libSceAgcDriver/Graphics/src/Texture.cpp. A rebase onto current main is required.
  2. [verified] Conventions: python tools/check_conventions.py --base 92e50418f33b8950ceea950cb37331a941f5be0e passed cleanly in an isolated scratch worktree with 0 warnings or errors.
  3. [read-only] PR template checklist: The PR description omits multiple standard checklist lines from .github/pull_request_template.md:
    • - [ ] Fixes: <!-- issue it closes as Fixes #N, or none -->
    • - [ ] Unblocks: <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none -->
    • - [ ] GPUs tested: <!-- vendor, model, driver and OS -->
  4. [read-only] Missing performance proof & AGC validation comparison: Per AGENTS.md, performance changes claim verification requires the two aps5-summary.jsonl trace digest summaries of main and the change captured from the same machine and scene, and AGC changes require focused Vulkan tests with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1, and VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation compared to confirm no new VUID- validation errors are logged.
  5. [author-reported] Performance claims: Author reports Flycast Crazy Taxi audio generation improved from ~52.7% to ~62.8% of real time on a custom local stack; performance was not independently measured in this review session.

Freshness Envelope

  • Head SHA: a049317f3e4d538805b9e81a90d5552d791ab3bf
  • Base SHA: 92e50418f33b8950ceea950cb37331a941f5be0e
  • Commits behind origin/main: 13,700
  • git merge-tree with origin/main: exit 1 (conflicts in CMakeLists.txt and Texture.cpp)

Cross-PR Ledger

@oneandonlydean

Copy link
Copy Markdown
Collaborator

Thank you @TheAggie for this change, and @tibzejoker for the port onto current main.

We've carried it forward in #14261, so it can land on today's main without asking you to rebase again. Your two original commits keep your authorship there, and the PR description credits you both. On top of them, #14261 adds a fix for a crash in the new test's teardown, and makes that test skip, rather than pass, when it can't run.

I'm closing this one in favour of #14261 so there's only one place to review. If you'd rather keep this PR and pick up those two test fixes yourself, say so and we'll do it that way instead.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

Hi @TheAggie, thank you for this change. It is on main now through #14261 (merged 4 Nov), which carried your commits forward with your authorship so it could land on current main: ForgetStamp and the agc_untracked_forget test are both there. This branch now conflicts with main in CMakeLists.txt, Texture.cpp and Recorder.cpp, so there is nothing left to merge from it.

Hi @adriadam10, since you are assigned here: unless you see something in this PR that #14261 did not carry, this one can be closed as merged through #14261.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

Closing as merged through #14261 (merge 60a8b3b, 4 Nov), which carried these commits onto current main with the original authorship. Thanks again @TheAggie; @adriadam10 for visibility as assignee.

Pipodi pushed a commit to Pipodi/AnyPS5 that referenced this pull request Nov 14, 2026
…for boykopovar#6187/boykopovar#6192

The per-block walk of untracked texture snapshots (drawcpu r2) walked each run of
watched or hinted blocks with one collect. A run with uncommitted or no-access
pages makes the Windows collector refuse the whole walk, so every block of the run
was hashed ('walk refused', ~490k blocks / 30 GB per 10 s in the open world).
The walk now takes the range's readable runs (DescribeCommitted) and collects
only those pages. The entry keeps the layout it was proved at; a block is clean
only when its clipped readable runs equal the baseline's and no stamp or hint is
newer, so the hash (zeros for unreadable pages) cannot differ. The compare hashes
with the same layout. [texhash] adds 'partly readable' and 'layout changed'.
APS5_NO_UNTRACKED_LAYOUT=1 restores the whole-run walk.
APS5_NO_UNTRACKED_FAST_PROOF=1 turns off boykopovar#6192's stamp proof,
APS5_NO_FORGET_STAMPS=1 makes boykopovar#6187's ForgetStamp the global serial again.

AI-assisted: yes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* build needs rebase Conflicts with main, rebase needed perf numbers missing perf pull request without a before and after summary line perf Performance change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants