perf(agc): take the retire registry snapshot only when a shadow reads it #26179

Merged
oneandonlydean merged 3 commits from upstream-pr/26179 into main 2026-11-24 02:24:18 +00:00
oneandonlydean commented 2026-11-23 11:48:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-11-23T11:48:15Z.


What

Builds on #25963, which is merged: this branch is that PR's head (86765d3651) plus two commits, so the diff against main is those two commits. It extends #25963's skip from the unit-shadows-off case to the default path. The branch is not rebased onto today's main; it merges cleanly with main 7db4926943.

When a guest munmap or mprotect waits for an unpinned range, WaitForLeasesAndImports retires the host imports over that range. Before each retire pass it copied the registry (GuestAllocationsAcquireRegistered). While the registry holds retiring ranges that call walks every range and copies a shared_ptr for each one. The profile in #6453 (PPSA01474, the scene change that remaps memory through AMM command buffers) has that copy at 50 % of the main thread, and its A/B with the copy skipped under APS5_NO_UNIT_SHADOW=1 gives 34, 35 against 51, 46 presents in the first minute.

The copy has one reader: the coverage test that RetireShadow runs for a live unit of the import's unit shadow. An import with no shadow, or with a shadow that has no live unit, never reads it. #25963 uses that for the case where unit shadows are off. This PR uses it for every pass:

  • RetireShadowReadsRegistered(import) answers with the same condition RetireShadow applies before it calls the coverage test (shadows on, a shadow exists, a live unit with a non-empty range).
  • The retire pass (retireImportsOver, shared by both sites) starts without a snapshot. When it meets an overlapping import whose shadow would read one, it leaves the import table's mutex, takes the snapshot, and walks the table again from the start. That happens at most once per pass. Imports retired before that point did not need the snapshot.
  • With unit shadows off the answer is always no, so #25963's behaviour is kept and its two guards become the one helper.

The part that carries the risk, stated plainly:

  • Same locks in the same order. The snapshot takes the registry mutex and is still taken with the import table's mutex released, as before. The new check takes the shadow registry's mutex under the import table's mutex, which is the order RetireShadow already uses inside retireImport.
  • No window between the check and the retire. Both happen under one hold of the import table's mutex, and both sites hold the GPU mutex, under which units become live (MarkShadowed).
  • A live unit still gets a real snapshot. A live unit with no coverage answer would be dropped, so the pass never retires such an import with the empty lease; the unit test fails if it does.
  • The snapshot can now be taken after the first imports of the pass were retired, and it is released before the recorder sync that follows the pass. Nothing reads it there.

The [address-sync] profile line gains two counts: retire passes that took the snapshot and passes that retired imports without it.

Tested

  • New ctest agc_host_import_retire_snapshot (mock device, Linux, needs /dev/udmabuf): an unmap retires an import with no shadow, with a shadow that has no live unit, and with a live unit, each with the device lock free and held, so both retire sites run. Only the live unit takes a snapshot, and its results are copied into the import before the buffer is destroyed. The test fails when the check answers no for a live unit, when the pass always takes the snapshot, when the snapshot is taken but not handed to the retire, and when the counts are not kept.
  • agc_host_import_sparse_tests passes in both modes; tools/check_conventions.py, tools/debt_fold.py --check, tools/env_reference.py --check and tools/check_test_registration.py pass.
  • PPSA21564, 450 s from boot through the intro, title and menu into the level, Linux, RTX 4090: this head against #25963's head in one window. Both runs pass the same 9 of 9 picture checkpoints at the same points of the run (title, menu, level), with the same frame rates per phase (intro 29.2 against 30.0 fps, title 33.0 / 33.3, menu 32.6 / 32.6). This title takes the changed path only a few times, so this run shows that nothing breaks, not a gain.
  • The two new counts, from profiled runs on this head (APS5_PROFILE_DRAW=1, the [address-sync] leases line), Linux, RTX 4090. PPSA04263, a 420 s route replay with 290 s of gameplay: 378 pin-contention waits, retire registry snapshots 0 taken, 227 skipped. PPSA21564, 240 s of a level: 7 waits, 0 taken, 5 skipped. No retire pass met a shadow with a live unit in either title, so every pass on the default path skipped the copy; the path that takes the snapshot ran only in the new test. These are counts from single runs, not a paired frame time comparison.
  • Not measured by me: the scene of #6453. It is 520 s into a hand-driven route that I cannot replay here, and PPSA21564 barely takes this path (7 pin-contention waits in a 240 s profiled run). The frame numbers for this change have to come from that scene on this head; the two new counts say how often the default path skips the copy there.
  • Not run: the Windows build and the _WIN32 lines of the waiter, which this change does not touch.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • 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 (#25963 is merged)
  • Fixes: none (part of #6453)
  • Unblocks: none
  • GPUs tested: NVIDIA GeForce RTX 4090, driver 610.57.04, Linux
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5452483993 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/26179). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-11-23T11:48:15Z. --- ### What Builds on #25963, which is merged: this branch is that PR's head (`86765d3651`) plus two commits, so the diff against main is those two commits. It extends #25963's skip from the unit-shadows-off case to the default path. The branch is not rebased onto today's main; it merges cleanly with main `7db4926943`. When a guest `munmap` or `mprotect` waits for an unpinned range, `WaitForLeasesAndImports` retires the host imports over that range. Before each retire pass it copied the registry (`GuestAllocationsAcquireRegistered`). While the registry holds retiring ranges that call walks every range and copies a `shared_ptr` for each one. The profile in #6453 (PPSA01474, the scene change that remaps memory through AMM command buffers) has that copy at 50 % of the main thread, and its A/B with the copy skipped under `APS5_NO_UNIT_SHADOW=1` gives 34, 35 against 51, 46 presents in the first minute. The copy has one reader: the coverage test that `RetireShadow` runs for a live unit of the import's unit shadow. An import with no shadow, or with a shadow that has no live unit, never reads it. #25963 uses that for the case where unit shadows are off. This PR uses it for every pass: - `RetireShadowReadsRegistered(import)` answers with the same condition `RetireShadow` applies before it calls the coverage test (shadows on, a shadow exists, a live unit with a non-empty range). - The retire pass (`retireImportsOver`, shared by both sites) starts without a snapshot. When it meets an overlapping import whose shadow would read one, it leaves the import table's mutex, takes the snapshot, and walks the table again from the start. That happens at most once per pass. Imports retired before that point did not need the snapshot. - With unit shadows off the answer is always no, so #25963's behaviour is kept and its two guards become the one helper. The part that carries the risk, stated plainly: - Same locks in the same order. The snapshot takes the registry mutex and is still taken with the import table's mutex released, as before. The new check takes the shadow registry's mutex under the import table's mutex, which is the order `RetireShadow` already uses inside `retireImport`. - No window between the check and the retire. Both happen under one hold of the import table's mutex, and both sites hold the GPU mutex, under which units become live (`MarkShadowed`). - A live unit still gets a real snapshot. A live unit with no coverage answer would be dropped, so the pass never retires such an import with the empty lease; the unit test fails if it does. - The snapshot can now be taken after the first imports of the pass were retired, and it is released before the recorder sync that follows the pass. Nothing reads it there. The `[address-sync]` profile line gains two counts: retire passes that took the snapshot and passes that retired imports without it. ### Tested - New ctest `agc_host_import_retire_snapshot` (mock device, Linux, needs `/dev/udmabuf`): an unmap retires an import with no shadow, with a shadow that has no live unit, and with a live unit, each with the device lock free and held, so both retire sites run. Only the live unit takes a snapshot, and its results are copied into the import before the buffer is destroyed. The test fails when the check answers no for a live unit, when the pass always takes the snapshot, when the snapshot is taken but not handed to the retire, and when the counts are not kept. - `agc_host_import_sparse_tests` passes in both modes; `tools/check_conventions.py`, `tools/debt_fold.py --check`, `tools/env_reference.py --check` and `tools/check_test_registration.py` pass. - PPSA21564, 450 s from boot through the intro, title and menu into the level, Linux, RTX 4090: this head against #25963's head in one window. Both runs pass the same 9 of 9 picture checkpoints at the same points of the run (title, menu, level), with the same frame rates per phase (intro 29.2 against 30.0 fps, title 33.0 / 33.3, menu 32.6 / 32.6). This title takes the changed path only a few times, so this run shows that nothing breaks, not a gain. - The two new counts, from profiled runs on this head (`APS5_PROFILE_DRAW=1`, the `[address-sync] leases` line), Linux, RTX 4090. PPSA04263, a 420 s route replay with 290 s of gameplay: 378 pin-contention waits, retire registry snapshots 0 taken, 227 skipped. PPSA21564, 240 s of a level: 7 waits, 0 taken, 5 skipped. No retire pass met a shadow with a live unit in either title, so every pass on the default path skipped the copy; the path that takes the snapshot ran only in the new test. These are counts from single runs, not a paired frame time comparison. - Not measured by me: the scene of #6453. It is 520 s into a hand-driven route that I cannot replay here, and PPSA21564 barely takes this path (7 pin-contention waits in a 240 s profiled run). The frame numbers for this change have to come from that scene on this head; the two new counts say how often the default path skips the copy there. - Not run: the Windows build and the `_WIN32` lines of the waiter, which this change does not touch. ### 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) and the licence notice of code adapted from upstream (see AGENTS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [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 (#25963 is merged) - [x] Fixes: none (part of #6453) - [x] Unblocks: none - [x] GPUs tested: NVIDIA GeForce RTX 4090, driver 610.57.04, Linux - [x] AI-assisted: yes
mihiii1337 commented 2026-11-23 12:23:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mihiii1337 on 2026-11-23T12:23:17Z.

Review: COMMENTED


AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6021629790 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/26179#pullrequestreview-6021629790). Originally posted by [mihiii1337](https://github.com/mihiii1337) on 2026-11-23T12:23:17Z. **Review:** COMMENTED --- AI-assisted: yes
Superseabas commented 2026-11-23 12:38:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Superseabas on 2026-11-23T12:38:06Z.

Review: APPROVED


Windows check of head 6520616bd2 with GCC 16 on an NVIDIA and an AMD GPU, merged onto main 071799dc88 (clean merge), and a read of the retire pass. The new agc_host_import_retire_snapshot needs /dev/udmabuf, so it skips here. This is a regression check of the shared retire path on Windows, which also runs releaseSettleRetired after it.

Setup: Windows 11 Home 10.0.26200, WinLibs GCC 16.2.0 (x86_64-ucrt-posix-seh, r3), Release full build, run from PowerShell with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1 and VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation. GPUs, each picked with ANYPS5_GPU: NVIDIA GeForce RTX 5070 Ti (621.01) and the Ryzen 7 7700X's integrated AMD Radeon (1002:164e, Adrenalin 26.10.2). This was built together with #26096, #26174 and #26178, which touch other files.

  • agc_host_import*, the unmap, retire and mprotect tests and the unit-shadow tests (^(agc_host_import.*|agc_.*unmap.*|agc_.*retire.*|agc_.*mprotect.*|agc_driver_unit_shadow.*|agc_unit_shadow.*)$): main 23 of 23, and with this PR 24 of 24 in 2 of 2 runs, on both GPUs. The only skips are agc_host_import_sparse and the new agc_host_import_retire_snapshot, which need /dev/udmabuf. No VUID- lines.

The code: RetireShadowReadsRegistered asks the same thing RetireShadow does before it calls the coverage test: shadows on, a shadow found, and a live unit with a non-empty range. A live unit whose range is empty is dropped either way (RetireShadow drops it without asking), so skipping the snapshot for it changes nothing. In retireImportsOver, the imports retired before a restart were ones that didn't read the snapshot, so retiring them with the empty lease is equivalent. The restart happens at most once, because leased stays set. mihiii1337's liveUnits == 0 early-out would cut the extra walk for idle shadows without changing any of this.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6021822332 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/26179#pullrequestreview-6021822332). Originally posted by [Superseabas](https://github.com/Superseabas) on 2026-11-23T12:38:06Z. **Review:** APPROVED --- Windows check of head `6520616bd2` with GCC 16 on an NVIDIA and an AMD GPU, merged onto `main` `071799dc88` (clean merge), and a read of the retire pass. The new `agc_host_import_retire_snapshot` needs `/dev/udmabuf`, so it skips here. This is a regression check of the shared retire path on Windows, which also runs `releaseSettleRetired` after it. **Setup:** Windows 11 Home 10.0.26200, WinLibs GCC 16.2.0 (`x86_64-ucrt-posix-seh`, r3), Release full build, run from PowerShell with `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1` and `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`. GPUs, each picked with `ANYPS5_GPU`: NVIDIA GeForce RTX 5070 Ti (621.01) and the Ryzen 7 7700X's integrated AMD Radeon (`1002:164e`, Adrenalin 26.10.2). This was built together with #26096, #26174 and #26178, which touch other files. - `agc_host_import*`, the unmap, retire and `mprotect` tests and the unit-shadow tests (`^(agc_host_import.*|agc_.*unmap.*|agc_.*retire.*|agc_.*mprotect.*|agc_driver_unit_shadow.*|agc_unit_shadow.*)$`): `main` 23 of 23, and with this PR 24 of 24 in 2 of 2 runs, on both GPUs. The only skips are `agc_host_import_sparse` and the new `agc_host_import_retire_snapshot`, which need `/dev/udmabuf`. No `VUID-` lines. **The code:** `RetireShadowReadsRegistered` asks the same thing `RetireShadow` does before it calls the coverage test: shadows on, a shadow found, and a live unit with a non-empty range. A live unit whose range is empty is dropped either way (`RetireShadow` drops it without asking), so skipping the snapshot for it changes nothing. In `retireImportsOver`, the imports retired before a restart were ones that didn't read the snapshot, so retiring them with the empty lease is equivalent. The restart happens at most once, because `leased` stays set. mihiii1337's `liveUnits == 0` early-out would cut the extra walk for idle shadows without changing any of this. AI-assisted: yes (Claude Code)
malekaifa commented 2026-11-23 14:11:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by malekaifa on 2026-11-23T14:11:33Z.

Review: COMMENTED


Reviewed follow-up head 92ce50f1e50e42c51de2397ca3af676e939dec1b against the previously reviewed 6520616bd2. The only new implementation change is the requested shadow->liveUnits == 0 early-out under registry.mutex, so an idle shadow no longer incurs the O(Units) scan on every overlapping retire pass. That resolves my source-level concern, and the current head has green Windows, portability, conventions, eligibility, PR and report checks.

I am not treating the performance evidence gate as cleared: the existing perf label bot still asks for paired aps5-summary.jsonl lines, traces, and tools/trace_digest.py output from the same title/scene/machine. That is separate from this resolved source concern.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6023131214 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/26179#pullrequestreview-6023131214). Originally posted by [malekaifa](https://github.com/malekaifa) on 2026-11-23T14:11:33Z. **Review:** COMMENTED --- Reviewed follow-up head `92ce50f1e50e42c51de2397ca3af676e939dec1b` against the previously reviewed `6520616bd2`. The only new implementation change is the requested `shadow->liveUnits == 0` early-out under `registry.mutex`, so an idle shadow no longer incurs the O(Units) scan on every overlapping retire pass. That resolves my source-level concern, and the current head has green Windows, portability, conventions, eligibility, PR and report checks. I am not treating the performance evidence gate as cleared: the existing `perf` label bot still asks for paired `aps5-summary.jsonl` lines, traces, and `tools/trace_digest.py` output from the same title/scene/machine. That is separate from this resolved source concern. AI-assisted: yes
Superseabas commented 2026-11-23 14:55:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Superseabas on 2026-11-23T14:55:43Z.

Review: APPROVED


Follow-up on Windows for head 92ce50f1e5, which adds the liveUnits == 0 early-out.

Setup: Windows 11 Home 10.0.26200, WinLibs GCC 16.2.0 (x86_64-ucrt-posix-seh, r3), Release full build, run from PowerShell with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1 and VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation. GPUs, each picked with ANYPS5_GPU: NVIDIA GeForce RTX 5070 Ti (621.01) and the Ryzen 7 7700X's integrated AMD Radeon (1002:164e, Adrenalin 26.10.2). Merged onto main 26508fa3f9 (clean merge), built together with #26174 and #21948, which touch other files.

  • agc_host_import*, the unmap, retire and mprotect tests and the unit-shadow tests: main 23 of 23, and with this PR 24 of 24 in 2 of 2 runs, on both GPUs. The only skips are agc_host_import_sparse and agc_host_import_retire_snapshot, which need /dev/udmabuf. No VUID- lines.

The change: liveUnits is read under the same registry.mutex as the unit walk, and a shadow with no live unit has nothing for RetireShadow's coverage test to keep. So the answer is unchanged, and an idle shadow costs nothing more. No test can tell the early-out from the walk, so the runs only show the retire path is unchanged on Windows.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6023821891 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/26179#pullrequestreview-6023821891). Originally posted by [Superseabas](https://github.com/Superseabas) on 2026-11-23T14:55:43Z. **Review:** APPROVED --- Follow-up on Windows for head `92ce50f1e5`, which adds the `liveUnits == 0` early-out. **Setup:** Windows 11 Home 10.0.26200, WinLibs GCC 16.2.0 (`x86_64-ucrt-posix-seh`, r3), Release full build, run from PowerShell with `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1` and `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`. GPUs, each picked with `ANYPS5_GPU`: NVIDIA GeForce RTX 5070 Ti (621.01) and the Ryzen 7 7700X's integrated AMD Radeon (`1002:164e`, Adrenalin 26.10.2). Merged onto `main` `26508fa3f9` (clean merge), built together with #26174 and #21948, which touch other files. - `agc_host_import*`, the unmap, retire and `mprotect` tests and the unit-shadow tests: `main` 23 of 23, and with this PR 24 of 24 in 2 of 2 runs, on both GPUs. The only skips are `agc_host_import_sparse` and `agc_host_import_retire_snapshot`, which need `/dev/udmabuf`. No `VUID-` lines. **The change:** `liveUnits` is read under the same `registry.mutex` as the unit walk, and a shadow with no live unit has nothing for `RetireShadow`'s coverage test to keep. So the answer is unchanged, and an idle shadow costs nothing more. No test can tell the early-out from the walk, so the runs only show the retire path is unchanged on Windows. AI-assisted: yes (Claude Code)
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!26179
No description provided.