Skip to content

perf(agc): a DCC key proof over untracked memory holds until the next ordering point - #6209

Open
TheAggie wants to merge 4 commits into
boykopovar:mainfrom
TheAggie:perf/agc-untracked-key-proofs
Open

TheAggie wants to merge 4 commits into
boykopovar:mainfrom
TheAggie:perf/agc-untracked-key-proofs

Conversation

@TheAggie

@TheAggie TheAggie commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

What

Stacked on #6187; only the last two commits are this PR. Independent of #6192, except that both add a test at the same place in CMakeLists.txt and tests/Recorder.cpp, a trivial conflict for whichever lands second.

ProvedClearKeys and ProvedCurrentDccKeys skip the scan of a surface's DCC keys while the key range is unchanged since the last scan, proved by CollectWrites and UnchangedSince. Over keys the write watch does not cover (on Windows, every host-pointer import of the guest mapping, #1441), CollectWrites returns 0, so no proof is ever kept and every use scans the keys again.

In RetroArch's Flycast core (PPSA99169) on Windows: 3.7 million key scans per 10 s (72 GB read, 2.2 s), with 0 proved and every one counted unstable.

  • Over untracked keys both functions take a stamp: the collect epoch, NewestStamp (driver stores into the keys), ForgetStamp (perf(agc): an untracked comparison memo holds until memory it covers is forgotten #6187) and the recorder's newest pending write note over the keys. A scan that is otherwise stable becomes the proof with that stamp, and a later call with the same stamp returns the proved keys without scanning.
  • A collect epoch of 0 or a forget in progress gives no stamp, so the scan runs as before.
  • The rule is the one untracked storage images already follow (compareUntracked, docs/dev/ARCHITECTURE.md): a CPU store into untracked memory is seen at the next ordering point. Pending GPU writes still make a scan unstable, as before.

Tested

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

  • ctest: 1233 of 1249 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_key_proofs (agc_driver_recorder_tests --untracked-key-proofs-only): keys in an untracked allocation. In one collect epoch the second ProvedClearKeys and the second ProvedCurrentDccKeys come from the proof; after a store into the keys and a new epoch the keys are scanned again and read as changed. Without this change the test fails with "a second key proof over untracked memory in the same collect epoch scanned the keys again".
  • RetroArch (PPSA99169), Flycast, Crazy Taxi attract demo, on the local stack described in perf(agc): an untracked comparison memo holds until memory it covers is forgotten #6187: key scans per 10 s went from 3.7 million (0 proved) to about 40,000 (2.38 million proved), and the time spent scanning from 2.2 s to 38 ms. Unprofiled audio, two runs each: the ProvedClearKeys change alone did not move it measurably; with the ProvedCurrentDccKeys change as well, 65.3% and 66.9% of real time against 68.0% and 68.0% (that stack also had perf(agc): fast revalidation over untracked memory proves a surface by its stamp until the next ordering point #6192). I have no measurement of main alone.
  • Linux is untested; on Linux most key ranges are write-watched and keep the existing path.

Related: #6060 keeps Windows imports of private write-watched memory on GetWriteWatch when a start-up probe allows it; key ranges it keeps watched never reach this path.

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: perf(agc): an untracked comparison memo holds until memory it covers is forgotten #6187
  • AI-assisted: yes. Written with Claude Code under my direction.

🤖 Generated with Claude Code

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

Copy link
Copy Markdown
Contributor

SWAG (PPSA22102) check: the untracked population is small here, no hot-path reduction

Stack tested: #6187 + #6209 combined at 4cb0fd041 (base 448cf78c6), prx SHA-256 prefix E28ABA5A5C33DA72; baseline prx 5AB0413B6D02CBFC. Same tree, only the stack differs.

Measured

Setup: Squirrel with a Gun 01.000.000, stock settings (0156186E), stock scene, fixed pad schedule, APS5_SAMPLE_WORKER + APS5_PROFILE_DRAW=1, 360 s both runs. Baseline 22,952 samples (mid snapshot 17,933); stack 22,939 (17,932); worker thread 99.9% busy in both.

Worker sampler, inclusive share of samples (function anywhere in the stack, all PCs aggregated, counted once per stack). Baseline final/mid2 -> stack final/mid2:

function baseline stack
ShaderResources::Complete 24.12 / 23.15 % 22.14 / 22.17 %
ShaderResources::buildComplete 24.82 / 23.77 % 22.89 / 22.81 %
resolveImageBinding 13.57 / 12.84 % 13.21 / 11.91 %
cachedStorageTexture 14.91 / 14.21 % 14.15 / 13.04 %
CompletePendingCpuWrites 10.18 / 9.81 % 6.70 / 7.61 %
SyncThrough 12.63 / 12.91 % 10.27 / 11.81 %
UploadFinish 11.05 / 10.76 % 9.50 / 10.76 %
DrawPipeline::Enqueue 16.97 / 16.75 % 14.38 / 13.49 %
Recorder::Sync 3.54 / 3.64 % 7.35 / 8.99 %
VulkanDevice::Dispatch 13.25 / 13.47 % 15.92 / 16.80 %
DrawPipeline::Drain 22.40 / 22.39 % 23.38 / 22.99 %
GuestMemory::GpuMutexType::lock 7.04 / 6.40 % 7.08 / 6.27 %

Method note: this aggregates every PC of a function. My earlier #7947 comment quoted only the hottest PC, which understates multi-PC functions there (e.g. buildComplete is 24.8% aggregate, not 13.6%).

Untracked population, per 10 s window: [textures] untracked snapshots kept/compared: 215/46 baseline vs 174/42 stack. [buffers] host imports under 1 MiB: baseline ~6235 settle reads, 6223-6226 back in the write watch, 3 kept compared under a storage image; stack ~6334, 6329-6332 back in the watch, 4 kept compared. The stack run also logs the proof work in [draw] pipelined: 2,583,788 read ranges and 6,992 stores held per 10 s; the baseline does not print these counters.

Timing, same setup, steady tail medians (last 20 x 10 s windows): readTarget 134.3 -> 163.0 us/draw, resources 104.9 -> 130.2, record 28.7 -> 32.4, prepare 6.1 -> 7.7, commit 260.4 -> 305.3 us, enqueue wait 110.1 -> 114.7. Caveat: the two runs' scene trajectories diverged (draws per 10 s window differ by up to ~2x in the first ~100 s; the two stock repeats show the same class of divergence), so these deltas are not clean PR attribution from one pair.

AI-generated analysis (not measured)

The proof only pays when target key ranges are not write-watched. On SWAG nearly all sub-1 MiB host imports are back in the write watch (6223-6332 of ~6240), so there is almost nothing for the memo/proof to elide, unlike the MGS4 ranges in ps54all's comment. What changes instead is when the worker blocks: DCC path counts that do involve untracked comparisons go down (Complete, buildComplete, cachedStorageTexture, CompletePendingCpuWrites, snapshots compared 46->42), while Recorder::Sync and VulkanDevice::Dispatch go up by more than that. Net: the hot path (ShaderResources::Complete 22-24%, storage-texture binding work) is not reduced; on this title the change reads as correctness/coverage with a small overhead, not a perf win. I would keep MGS4 as the perf case and treat SWAG as neutral. No further A/B planned unless you want one; if useful I can run a low-settings variant (fewer dispatches, more draw/commit path) or a dedicated 4-run paired timing.

Artifacts (I will drag them in): swag-run26-baseline-symbolized.txt, swag-run27-stack-symbolized.txt, swag-stack-agg.txt, swag-stack-timing.txt, swag-run27-stack-worker.collapsed.txt.

@Rajkamalcode

Copy link
Copy Markdown
Contributor

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@TheAggie This PR now conflicts with main in core/libs/prx/libSceAgcDriver/CMakeLists.txt, core/libs/prx/libSceAgcDriver/Graphics/src/Texture.cpp. Main’s texture refresh and proof tracking changed the same path this PR extends with longer-lived DCC proofs. Could you reconcile when the proof remains valid and when a refresh must fall back to current tracking?

00xDisaster added a commit to 00xDisaster/AnyPS5 that referenced this pull request Oct 27, 2026
cachedTexture scans its sampled texture's DCC keys before the storage-source choice, after a keys reset and after a pending flush. Route those scans through ProvedClearKeys and keep the proof in a small per-thread table so a lookup reuses it instead of rereading the same key range. The proof is still validated by ProvedClearKeys, so a write between scans scans again. On Windows, untracked host imports continue to scan until boykopovar#6209 supplies their proof.
00xDisaster added a commit to 00xDisaster/AnyPS5 that referenced this pull request Oct 27, 2026
cachedTexture scans its sampled texture's DCC keys before the storage-source choice, after a keys reset and after a pending flush. Route those scans through ProvedClearKeys and keep the proof in a small per-thread table so a lookup reuses it instead of rereading the same key range. The proof is still validated by ProvedClearKeys, so a write between scans scans again. On Windows, untracked host imports continue to scan until boykopovar#6209 supplies their proof.
00xDisaster added a commit to 00xDisaster/AnyPS5 that referenced this pull request Oct 27, 2026
cachedTexture scans its sampled texture's DCC keys before the storage-source choice, after a keys reset and after a pending flush. Route those scans through ProvedClearKeys and keep the proof in a small per-thread table so a lookup reuses it instead of rereading the same key range. The proof is still validated by ProvedClearKeys, so a write between scans scans again. Key the table by the DCC address, surface bytes, guest format and alpha position: textureClearKeys can return a clear for one view and Uncompressed for another view of the same range when FillDccClear cannot encode it. Add a two-format DccKeyPresync case that samples one range as R16_UNORM, then as R5G6B5, and checks the second view reads its stored texels. On Windows, untracked host imports continue to scan until boykopovar#6209 supplies their proof.
00xDisaster added a commit to 00xDisaster/AnyPS5 that referenced this pull request Oct 27, 2026
cachedTexture scans its sampled texture's DCC keys before the storage-source choice, after a keys reset and after a pending flush. Route those scans through ProvedClearKeys and keep the proof in a small per-thread table so a lookup reuses it instead of rereading the same key range. The proof is still validated by ProvedClearKeys, so a write between scans scans again. Key the table by the DCC address, surface bytes, guest format and alpha position: textureClearKeys can return a clear for one view and Uncompressed for another view of the same range when FillDccClear cannot encode it. Add a two-format DccKeyPresync case that samples one range as R16_UNORM, then as R5G6B5, and checks the second view reads its stored texels. On Windows, untracked host imports continue to scan until boykopovar#6209 supplies their proof.
00xDisaster added a commit to 00xDisaster/AnyPS5 that referenced this pull request Oct 28, 2026
cachedTexture scans its sampled texture's DCC keys before the storage-source choice, after a keys reset and after a pending flush. Route those scans through ProvedClearKeys and keep the proof in a small per-thread table so a lookup reuses it instead of rereading the same key range. The proof is still validated by ProvedClearKeys, so a write between scans scans again. Key the table by the DCC address, surface bytes, guest format and alpha position: textureClearKeys can return a clear for one view and Uncompressed for another view of the same range when FillDccClear cannot encode it. Add a two-format DccKeyPresync case that samples one range as R16_UNORM, then as R5G6B5, and checks the second view reads its stored texels. On Windows, untracked host imports continue to scan until boykopovar#6209 supplies their proof.
@fogydes

fogydes commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

stale+rebase

1. Freshness Envelope

  • Head SHA: b00f936b31fe9ba4946ee35dd3fd9f9bc707806c
  • Merge-base SHA: 92e50418f33b8950ceea950cb37331a941f5be0e
  • Behind origin/main: 13,751 commits
  • Merge-tree status: exit code 1 (conflicts against origin/main in core/libs/prx/libSceAgcDriver/CMakeLists.txt and core/libs/prx/libSceAgcDriver/Graphics/src/Texture.cpp)

2. Findings

  1. [verified] Merge conflicts require rebase: git merge-tree --write-tree origin/main b00f936b31fe9ba4946ee35dd3fd9f9bc707806c exits with code 1 due to conflicts in core/libs/prx/libSceAgcDriver/CMakeLists.txt and core/libs/prx/libSceAgcDriver/Graphics/src/Texture.cpp.
  2. [verified] Conventions clean against merge-base: python tools/check_conventions.py --base 92e50418f33b8950ceea950cb37331a941f5be0e in an isolated scratch worktree reported 0 findings.
  3. [read-only] Missing template checklist lines: The PR description omits the required Fixes:, Unblocks:, and GPUs tested: lines from .github/pull_request_template.md.
  4. [read-only] Missing trace digest comparison: AGENTS.md specifies that speed / perf claims require two aps5-summary.jsonl lines plus tools/trace_digest.py summaries from the same machine and scene comparing main against the branch (author reports no baseline measurement on main alone).
  5. [read-only] Missing Vulkan validation layer comparison: AGENTS.md requires AGC PRs to report test runs with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1, and VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, confirming no new VUID- validation errors are introduced.
  6. [author-reported] Reported RetroArch Flycast Crazy Taxi demo (PPSA99169) DCC key scan reduction and scan duration on RTX 5070 Ti.

3. Cross-PR Ledger

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@TheAggie, #6187 is closed: it landed on main through #14261 (merged 4 Nov) with your commits and authorship, and #14262 carries #6192 the same way. This branch still contains #6187's commits and conflicts with main in core/libs/prx/libSceAgcDriver/CMakeLists.txt and Texture.cpp.

If you want to continue this change, please rebase it onto current main so that only the DCC key proof commit remains, and reconcile it with the untracked-memory proof tracking main now has. The earlier request for perf numbers also still stands: the two reports so far (an MGS4 run on a local stack, and PPSA22102 showing a small untracked population and no hot-path reduction) do not show a gain for this head. A paired run on PPSA21564 or another title that shows a difference above the noise floor would settle whether it is worth carrying.

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed — changes requested

Changes are required before merge: guard the address-range arithmetic in ForgetPages and ForgetStamp against overflow.

What I ran

Reviewed head b00f936b31fe9ba4946ee35dd3fd9f9bc707806c from the prepared diff and pinned reader. I did not run tests or a build.

No commands run; review bundle inspected through reader tools.

Results

Check Result Note
Diff and changed code passed Reviewed included changed files and relevant ranges
Build and tests not run No execution environment available

Findings

Blocking

  • GuestMemory::ForgetPages and ForgetStamp compute address + bytes - 1 without checking overflow. If a nonempty range ends past UINT64_MAX, this wraps and computes an invalid last block; ForgetPages may then fail to invalidate the range stamp, and ForgetStamp may report a stale stamp. This can leave a cached comparison or DCC key proof valid after memory in its range was forgotten. Validate the range before deriving last, or use checked/saturating range arithmetic. Changed lines: core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp:487 and core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp:81.

Non-blocking

None.

Sources

  • The arithmetic is used in ForgetPages and ForgetStamp in the reviewed changed code at core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp.

Not verified

  • No build or tests were run. Runtime behavior on Windows and Linux was not verified.
  • files.json was unavailable through the reader, so the exclusions list could not be checked. All seven changed paths listed in manifest.json were included; the final manifest entry, core/libs/prx/libSceAgcDriver/tests/Recorder.cpp, was not read in full, only the changed test hunks from the diff were inspected.

To get this to a good state

  • Add overflow-safe range validation to both calculations, then rerun the relevant recorder tests and build on the new head.

This branch has not been deployed

No deployments
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.

6 participants