fix(agc): validate host imports against their recorded backing #16751

Merged
chitters2004 merged 3 commits from upstream-pr/16751 into main 2026-11-12 13:38:26 +00:00
chitters2004 commented 2026-11-07 15:13:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by chitters2004 on 2026-11-07T15:13:27Z.


What

A host import was valid only while the import policy would still choose the same run for it. refreshImports checked each import with sameRange, which compared it with the run importRunBounds picks now, under the current budget and remembered refusals and failures. Any change to the guest allocation registry that rechecked an import could retire it although every range it was made from was still registered, for example when a neighbouring range made a larger run possible or a remembered refusal expired. importRunAt also retired the imports a larger run overlapped before it tried to create the larger one, so a refused or failed replacement left those ranges with no import and their uses fell back to copies.

The first commit makes an import's validity depend only on its recorded backing:

  • sameRange keeps an import while every registered range it was made from is still registered with the same owner.
  • An import whose run grew is queued, and the next build that rebuilds its address space replaces it with the larger import before its pass. The replacement is created before the imports it replaces retire, so a refused or failed replacement leaves them in place. A failed allocation lowers the budget only when the replacement would have come within an eighth of it without the imports it replaces.

The second commit keeps growth owed until it is made:

  • Growth is owed to a run, not to an import. A run that a held build, the budget or a failed import kept from being imported whole is queued with that reason, as is a run an unchanged import became too small for. It stays queued through retirements and replacements until an import covers it, and is dropped when the run changes.
  • A build that rebuilds its address space retries the runs whose reason may have lifted: held and untried runs always, budget runs once the budget rose or the pinned bytes shrank, failed runs after a registry change in their window. A cached address space is rebuilt for them only when the budget rises past the lowest one that refused a run, or when a retired import is released while a budget run is owed; both are flags read without a budget query.
  • The budget bounds every pinned import, apart from the imports a replacement replaces while it is created (below): the live imports plus the retired imports a recorder or settle batch still holds, counted from retirement until the last holder destroys them. A replacement is admitted against both, and credits the imports it replaces only when no recorder will hold them (retireFreesAtOnce, shared by retirement, admission and the evicting refusal). The [gpu] host import budget line and the performance overlay report pinned bytes.

Remaining gaps, in the Host import refresh entry of docs/dev/debt/libSceAgcDriver.md:

  • While a recorder is active, a replacement credits nothing for the imports it replaces and an eviction frees nothing until the recorder completes, so a replacement needs budget for the old and new bytes together.
  • On Windows, an import retired into a settle batch with no recorder active is counted only after the admission that retired it credited its bytes, so that admission can exceed the budget by them.
  • The imports a replacement replaces stay pinned until it is created, so that a failed replacement leaves them in place; the pinned bytes can exceed the budget by them meanwhile, and their pages lie inside the replacement. The unrelated imports an admission counts as evictable retire before the replacement is created.
  • A run split by a held build, the budget or a failed import is regrown only by a build that rebuilds its address space, and a higher VK_EXT_memory_budget report is seen only when the budget is next computed.
  • A range imported in windows is not regrown.
  • A request from outside a build is refused while a build holds an import it would replace.

Overlap, checked on 2026-11-07 against main 46552cb461 with tools/pr_overlap.py and by hand: three open pull requests change lines of GuestBufferMemory.cpp that this branch changes, and none implements the same thing. #2434 adds an early failure in importRunAt, which has to stay before the retirement loop that now runs after the replacement import is created; #4448 changes the same check in refreshImports and adds RetireIdleImports, which retires through retireImport and so gets the pinned accounting unchanged; #15919 adds another reason to rebuild a cached address space in AcquireRegistered beside the growth rebuild, and the two compose. #15674, #16474, #6548, #10443, #15302 and #16286 change other functions of the same file (#16286 only reads imports through ExistingHostImportFor). The other 70 the tool reported conflict with main; I compared those with a commit since 2026-10-24 by function. Rechecked against 4977f64052: the tool confirms the same three (#2434, #4448, #15919) and could not compare 81 that conflict with main.

Tested

Head 11a59dedf4 on main 6b733c4ef2.

Rebased onto main 6b733c4ef2. 0254c4186f conflicted with #15919 (patch the cached address space after an import change) in GuestBufferMemory.cpp and .hpp. The resolution keeps both counters (patchedEpoch and rebuiltGrowth) in the stats struct, the stats return and the profile line. It keeps main's order in the rebuild decision: when the imports epoch changed it tries patchedSpace first, and the growth check comes next, so a patched space still rebuilds when growth is owed. #16130 (keep joined imports when a window block is picked) merged cleanly; it changes only how importRunBounds chooses a block. The debt entry was shortened to stay under 1000 characters.

  • Local RTX 3080 gate (driver 621.14), with ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation and the same focused AGC set. Each side was built by the fork's Windows CI job and run here, with the toolchain and commit checked.
    • 6b733c4ef2: 233 tests, 100 % passed.
    • 11a59dedf4: 234 tests, 100 % passed, including agc_host_import_validity.
    • Neither side has a VUID- line in its log or its replay subprocess logs, neither log contains was not found but was requested by env var VK_INSTANCE_LAYERS, and there are no new failures.
  • GitHub runners, Mesa lavapipe with validation.
    • Run 42403227941, host-import, address-space, import-settle and BDA tests: Linux 11 of 13 passed, 2 skipped; Windows 13 of 26 passed, 13 skipped.
    • Run 42404437607, agc_driver_graphics (which runs the patched-space and growth cases), agc_host_import_validity and agc_host_import_retire*: Linux and Windows 4 of 4 passed.
    • No failures and no VUID- lines; the conventions check passed.

Review fix efaedd5df4 (rebased as 11a59dedf4; unrelated imports retire before a replacement is created). The new agc_host_import_validity case fails on 0254c4186f's order with "a join that evicts an unrelated import was created while that import was still pinned" and passes with the fix. Local RTX 3080 gate (driver 621.14), same environment and focused AGC set as below: 233 tests, 100 % passed, no VUID- line, no new failures against main 4977f64052. GitHub runners, Mesa lavapipe with validation, rebased onto 4977f64052 (run 42395591789): Linux and Windows pass, no VUID- lines.

Refreshed onto main 4977f64052. Head 0254c4186f; git range-diff against f62ea7bd08 shows the same patches (2 commits, =). Local RTX 3080 gate on both, same environment as below (ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation), built one after the other in one build tree, with the focused AGC set of all five pull requests of this series (233 tests on this head, 232 on main): both 100 % passed, no VUID- line in either log or in replay subprocess logs, and neither log contains was not found but was requested by env var VK_INSTANCE_LAYERS. Fork CI on the rebased head against 4977f64052: run 42374063297: Linux 37 of 38 passed, 1 skipped; Windows 38 of 44 passed, 5 skipped, and agc_driver_recorder failed, as it does on main. No VUID- line on either side, conventions check passed. The full runs below were on the previous base 46552cb461.

Vulkan validation. RTX 3080, ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, ctest --test-dir build -R 'agc|write_tracking|guest|indirect_draw|draw_finisher|depth_bias|draw_pipeline' -V on f62ea7bd08 (3200 tests) and on main 46552cb461 (3199 tests). Every test failing in either full run was then rerun alone on both builds, one build at a time with nothing else on the GPU, using ctest -R with the union of the 16 names and the same environment: on both, 15 of 16 passed and only agc_driver_direct_write_completion failed, which fails on main too (#15547); the other failures of the full runs were timeouts and 0xc0000409 exits under load from concurrent gates and did not reproduce alone. Both full logs hold the same 18 VUID-VkPipelineColorBlendStateCreateInfo-pAttachments-00605 lines and no other VUID-; neither contains was not found but was requested by env var VK_INSTANCE_LAYERS. The change adds no validation error.

Fork CI (GitHub runners, Mesa lavapipe with the Khronos validation layer, head against main): run 42329076516, rebased there onto 46552cb461. Linux: 28 of 31 passed, 3 skipped; Windows: 30 of 48 passed, 17 skipped, and agc_driver_recorder failed, as it does on main on the same runner. No VUID- line on either side, conventions check passed. The skips are lavapipe's: no other queue family to settle imports on, no deferred settle of small imports, no /dev/udmabuf, and write watching not kept for native clear imports; the RTX 3080 gate above runs them.

Tests.

  • agc_host_import_validity (new): imports stay while their ranges are unchanged and retire when their backing changes; a range registered next to an imported block does not retire it; a block whose larger replacement failed is kept, stays in the address space and is not counted against the budget; a request for a larger run does not replace an import a build still holds; a run kept from being imported whole by a held build, the budget or a registry change is queued and imported whole by the first build that allows it, also when only the budget rose; a partial promotion stays queued until one import covers the run; the queue holds one entry per live run over the budget; cached builds with a run queued for growth do not query the budget; a join that evicts an unrelated import retires it before the join is created, so the imported bytes stay within the budget plus the imports the join replaces (a mock cap on total imported bytes refuses the join on the old order).
  • agc_host_import_retire_tests (extended): replaced imports a recorder holds are counted as retired and pinned until it releases them; the live and retired bytes match the memory the driver holds; a grown run that fits only when replaced and retired imports are not counted is refused; a run refused for pinned bytes is promoted by the next build after the recorder releases them, with no registry or budget change.
  • The growth tests in BdaResources.cpp, HostImportProofs.cpp and Recorder.cpp now expect an unchanged import to be kept and replaced by exactly one larger import when the larger run can be made, and kept with nothing made when it cannot.

Red proof, from the branch history:

  • agc_host_import_validity failed 3 of its 5 checks on the main of 2026-11-04, before the first commit.
  • Before the second commit, five probes of its cases were red on the first commit alone: a run whose import a registry change retired was imported again smaller with no growth queued; a run first imported smaller under the budget was not queued; a run a held build kept from being imported whole was not queued; the queue held 0 runs with 1 live run over the budget; cached builds queried the budget 5 times.
  • Six mutants of the growth queue (owed growth dropped, held runs not eligible, a stale entry kept after the run shrank, no budget signal, a budget query on the cached path, covered runs left queued) each turn agc_host_import_validity red.
  • Pinned accounting: on the code before it, 13 checks of agc_host_import_retire_tests failed, among them replaced imports held by the recorder not counted, counts not matching the memory the driver holds, a grown run imported although it fits only without the retired imports, and imports pinning more than the budget. Mutants that drop the count on retirement, drop the subtraction on release, admit without the retired bytes, skip the wake-up after a release, or free at once while an idle recorder's hold keeps the import each turn it red (13, 17, 8, 2 and 14 failing checks).

GTA V, under host import pressure. GTA V (PPSA04263, content 01.011.001), standing scene, 3840x2160, with APS5_HOST_IMPORT_MIB=4096 so that the budget refuses imports (at the default budget neither side refused anything in this scene, with about 8.1 GiB imported). Counts over 75 s after the player stands still, one boot per side, APS5_PROFILE_DRAW=1 (counts only, never timing), with a local counter patch applied identically to both sides and not part of this PR (diag-imp.patch). main was 7383be1a19, the change 0159dee3a1, this branch before its rebase onto 46552cb461 (git range-diff shows the same patches).

75 s window main this change
presents / draws 380 / 1,195,723 479 / 1,419,887
imports refused (per draw) 111,188 (0.093) 94,287 (0.066)
refusals by requester: map / build / access 6 / 111,182 / 0 5 / 92,800 / 1,482
imports live at the window end 86 (4,096 MiB) 203 (4,096 MiB)
GPU copies out of imports, KiB per frame 11,313 11,447
staged copy-in, KiB per frame 11,434 11,358
snapshot compares per frame 248 3.2
image mirror refreshes per frame (per draw) 8,349 (2.66) 2,912 (0.98)
image mirror blocks copied per frame 263 86
retired and still pinned not counted on main 16.2 MiB at the first print, 0.2 MiB at the last

Refusals per draw are 29 % lower, copies and staging per frame are within 1.2 % of main (two main boots at the default budget differ by 1.4 to 2.6 % on these counters), the fallback work that refusals cause is lower, and the retired-and-pinned bytes drain. 1,482 refusals in the window come from the access requester on this change and none on main; their cause was not traced. One boot per side, and the two boots drew a different number of draws per present (about 3,150 and 2,960), so the sizes are uncertain; the direction is the same for every fallback counter.

This is a fix, not a speed claim. Frame time in two interleaved pairs of the standing scene at the default budget (warm caches, same settings as above but without the counter patch or APS5_PROFILE_DRAW) shows no change beyond noise:

capture commit presents in 30 s mean ms/frame p50 p90 p99 max GPU busy ms/present queue worker busy
main, pair 1 7383be1a19c7 219 137.5 136.2 140.1 179.0 193.0 34.0 94.4 %
change, pair 1 0159dee3a18e 220 136.7 135.7 141.5 158.4 200.3 33.7 94.8 %
main, pair 2 7383be1a19c7 221 136.2 134.9 141.8 179.9 198.3 33.7 94.9 %
change, pair 2 0159dee3a18e 217 138.8 137.3 143.6 180.8 211.5 33.8 95.0 %

Change minus main, mean ms/frame: pair 1 -0.77, pair 2 +2.58, mean +0.90 (+0.7 %); p50 -0.5 and +2.4; p99 -20.6 and +0.9.

Twelve main captures of this scene give a standard deviation of 1.85 ms (1.35 %) per frame, and the pair deltas have opposite signs. These pairs ran on this branch before its rebase (0159dee3a1), on the host before Resizable BAR was turned on and Windows moved from build 26220 to 26320 (driver 621.14 on both). Summary lines, digests and the deviations from the Performance report form are attached to the pr-imp-traces release.

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 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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: NVIDIA GeForce RTX 3080 (driver 621.14, Windows 10.0.26220); Mesa lavapipe on the fork's GitHub runners (Linux and Windows)
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5202965419 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16751). Originally posted by [chitters2004](https://github.com/chitters2004) on 2026-11-07T15:13:27Z. --- ### What A host import was valid only while the import policy would still choose the same run for it. `refreshImports` checked each import with `sameRange`, which compared it with the run `importRunBounds` picks now, under the current budget and remembered refusals and failures. Any change to the guest allocation registry that rechecked an import could retire it although every range it was made from was still registered, for example when a neighbouring range made a larger run possible or a remembered refusal expired. `importRunAt` also retired the imports a larger run overlapped before it tried to create the larger one, so a refused or failed replacement left those ranges with no import and their uses fell back to copies. The first commit makes an import's validity depend only on its recorded backing: - `sameRange` keeps an import while every registered range it was made from is still registered with the same owner. - An import whose run grew is queued, and the next build that rebuilds its address space replaces it with the larger import before its pass. The replacement is created before the imports it replaces retire, so a refused or failed replacement leaves them in place. A failed allocation lowers the budget only when the replacement would have come within an eighth of it without the imports it replaces. The second commit keeps growth owed until it is made: - Growth is owed to a run, not to an import. A run that a held build, the budget or a failed import kept from being imported whole is queued with that reason, as is a run an unchanged import became too small for. It stays queued through retirements and replacements until an import covers it, and is dropped when the run changes. - A build that rebuilds its address space retries the runs whose reason may have lifted: held and untried runs always, budget runs once the budget rose or the pinned bytes shrank, failed runs after a registry change in their window. A cached address space is rebuilt for them only when the budget rises past the lowest one that refused a run, or when a retired import is released while a budget run is owed; both are flags read without a budget query. - The budget bounds every pinned import, apart from the imports a replacement replaces while it is created (below): the live imports plus the retired imports a recorder or settle batch still holds, counted from retirement until the last holder destroys them. A replacement is admitted against both, and credits the imports it replaces only when no recorder will hold them (`retireFreesAtOnce`, shared by retirement, admission and the evicting refusal). The `[gpu] host import budget` line and the performance overlay report pinned bytes. Remaining gaps, in the `Host import refresh` entry of `docs/dev/debt/libSceAgcDriver.md`: - While a recorder is active, a replacement credits nothing for the imports it replaces and an eviction frees nothing until the recorder completes, so a replacement needs budget for the old and new bytes together. - On Windows, an import retired into a settle batch with no recorder active is counted only after the admission that retired it credited its bytes, so that admission can exceed the budget by them. - The imports a replacement replaces stay pinned until it is created, so that a failed replacement leaves them in place; the pinned bytes can exceed the budget by them meanwhile, and their pages lie inside the replacement. The unrelated imports an admission counts as evictable retire before the replacement is created. - A run split by a held build, the budget or a failed import is regrown only by a build that rebuilds its address space, and a higher `VK_EXT_memory_budget` report is seen only when the budget is next computed. - A range imported in windows is not regrown. - A request from outside a build is refused while a build holds an import it would replace. Overlap, checked on 2026-11-07 against `main` `46552cb461` with `tools/pr_overlap.py` and by hand: three open pull requests change lines of `GuestBufferMemory.cpp` that this branch changes, and none implements the same thing. #2434 adds an early failure in `importRunAt`, which has to stay before the retirement loop that now runs after the replacement import is created; #4448 changes the same check in `refreshImports` and adds `RetireIdleImports`, which retires through `retireImport` and so gets the pinned accounting unchanged; #15919 adds another reason to rebuild a cached address space in `AcquireRegistered` beside the growth rebuild, and the two compose. #15674, #16474, #6548, #10443, #15302 and #16286 change other functions of the same file (#16286 only reads imports through `ExistingHostImportFor`). The other 70 the tool reported conflict with `main`; I compared those with a commit since 2026-10-24 by function. Rechecked against `4977f64052`: the tool confirms the same three (#2434, #4448, #15919) and could not compare 81 that conflict with `main`. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Head `11a59dedf4` on `main` `6b733c4ef2`. **Rebased onto `main` `6b733c4ef2`.** `0254c4186f` conflicted with #15919 (patch the cached address space after an import change) in `GuestBufferMemory.cpp` and `.hpp`. The resolution keeps both counters (`patchedEpoch` and `rebuiltGrowth`) in the stats struct, the stats return and the profile line. It keeps `main`'s order in the rebuild decision: when the imports epoch changed it tries `patchedSpace` first, and the growth check comes next, so a patched space still rebuilds when growth is owed. #16130 (keep joined imports when a window block is picked) merged cleanly; it changes only how `importRunBounds` chooses a block. The debt entry was shortened to stay under 1000 characters. - Local RTX 3080 gate (driver 621.14), with `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation` and the same focused AGC set. Each side was built by the fork's Windows CI job and run here, with the toolchain and commit checked. - `6b733c4ef2`: 233 tests, 100 % passed. - `11a59dedf4`: 234 tests, 100 % passed, including `agc_host_import_validity`. - Neither side has a `VUID-` line in its log or its replay subprocess logs, neither log contains `was not found but was requested by env var VK_INSTANCE_LAYERS`, and there are no new failures. - GitHub runners, Mesa lavapipe with validation. - [Run 42403227941](https://github.com/chitters2004/AnyPS5/actions/runs/42403227941), host-import, address-space, import-settle and BDA tests: Linux 11 of 13 passed, 2 skipped; Windows 13 of 26 passed, 13 skipped. - [Run 42404437607](https://github.com/chitters2004/AnyPS5/actions/runs/42404437607), `agc_driver_graphics` (which runs the patched-space and growth cases), `agc_host_import_validity` and `agc_host_import_retire*`: Linux and Windows 4 of 4 passed. - No failures and no `VUID-` lines; the conventions check passed. **Review fix `efaedd5df4`** (rebased as `11a59dedf4`; unrelated imports retire before a replacement is created). The new `agc_host_import_validity` case fails on `0254c4186f`'s order with "a join that evicts an unrelated import was created while that import was still pinned" and passes with the fix. Local RTX 3080 gate (driver 621.14), same environment and focused AGC set as below: 233 tests, 100 % passed, no `VUID-` line, no new failures against `main` `4977f64052`. GitHub runners, Mesa lavapipe with validation, rebased onto `4977f64052` ([run 42395591789](https://github.com/chitters2004/AnyPS5/actions/runs/42395591789)): Linux and Windows pass, no `VUID-` lines. **Refreshed onto `main` `4977f64052`.** Head `0254c4186f`; `git range-diff` against `f62ea7bd08` shows the same patches (2 commits, `=`). Local RTX 3080 gate on both, same environment as below (`ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`), built one after the other in one build tree, with the focused AGC set of all five pull requests of this series (233 tests on this head, 232 on `main`): both 100 % passed, no `VUID-` line in either log or in replay subprocess logs, and neither log contains `was not found but was requested by env var VK_INSTANCE_LAYERS`. Fork CI on the rebased head against `4977f64052`: [run 42374063297](https://github.com/chitters2004/AnyPS5/actions/runs/42374063297): Linux 37 of 38 passed, 1 skipped; Windows 38 of 44 passed, 5 skipped, and `agc_driver_recorder` failed, as it does on `main`. No `VUID-` line on either side, conventions check passed. The full runs below were on the previous base `46552cb461`. **Vulkan validation.** RTX 3080, `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`, `ctest --test-dir build -R 'agc|write_tracking|guest|indirect_draw|draw_finisher|depth_bias|draw_pipeline' -V` on `f62ea7bd08` (3200 tests) and on `main` `46552cb461` (3199 tests). Every test failing in either full run was then rerun alone on both builds, one build at a time with nothing else on the GPU, using `ctest -R` with the union of the 16 names and the same environment: on both, 15 of 16 passed and only `agc_driver_direct_write_completion` failed, which fails on `main` too (#15547); the other failures of the full runs were timeouts and `0xc0000409` exits under load from concurrent gates and did not reproduce alone. Both full logs hold the same 18 `VUID-VkPipelineColorBlendStateCreateInfo-pAttachments-00605` lines and no other `VUID-`; neither contains `was not found but was requested by env var VK_INSTANCE_LAYERS`. The change adds no validation error. **Fork CI** (GitHub runners, Mesa lavapipe with the Khronos validation layer, head against `main`): [run 42329076516](https://github.com/chitters2004/AnyPS5/actions/runs/42329076516), rebased there onto `46552cb461`. Linux: 28 of 31 passed, 3 skipped; Windows: 30 of 48 passed, 17 skipped, and `agc_driver_recorder` failed, as it does on `main` on the same runner. No `VUID-` line on either side, conventions check passed. The skips are lavapipe's: no other queue family to settle imports on, no deferred settle of small imports, no `/dev/udmabuf`, and write watching not kept for native clear imports; the RTX 3080 gate above runs them. **Tests.** - `agc_host_import_validity` (new): imports stay while their ranges are unchanged and retire when their backing changes; a range registered next to an imported block does not retire it; a block whose larger replacement failed is kept, stays in the address space and is not counted against the budget; a request for a larger run does not replace an import a build still holds; a run kept from being imported whole by a held build, the budget or a registry change is queued and imported whole by the first build that allows it, also when only the budget rose; a partial promotion stays queued until one import covers the run; the queue holds one entry per live run over the budget; cached builds with a run queued for growth do not query the budget; a join that evicts an unrelated import retires it before the join is created, so the imported bytes stay within the budget plus the imports the join replaces (a mock cap on total imported bytes refuses the join on the old order). - `agc_host_import_retire_tests` (extended): replaced imports a recorder holds are counted as retired and pinned until it releases them; the live and retired bytes match the memory the driver holds; a grown run that fits only when replaced and retired imports are not counted is refused; a run refused for pinned bytes is promoted by the next build after the recorder releases them, with no registry or budget change. - The growth tests in `BdaResources.cpp`, `HostImportProofs.cpp` and `Recorder.cpp` now expect an unchanged import to be kept and replaced by exactly one larger import when the larger run can be made, and kept with nothing made when it cannot. Red proof, from the branch history: - `agc_host_import_validity` failed 3 of its 5 checks on the `main` of 2026-11-04, before the first commit. - Before the second commit, five probes of its cases were red on the first commit alone: a run whose import a registry change retired was imported again smaller with no growth queued; a run first imported smaller under the budget was not queued; a run a held build kept from being imported whole was not queued; the queue held 0 runs with 1 live run over the budget; cached builds queried the budget 5 times. - Six mutants of the growth queue (owed growth dropped, held runs not eligible, a stale entry kept after the run shrank, no budget signal, a budget query on the cached path, covered runs left queued) each turn `agc_host_import_validity` red. - Pinned accounting: on the code before it, 13 checks of `agc_host_import_retire_tests` failed, among them replaced imports held by the recorder not counted, counts not matching the memory the driver holds, a grown run imported although it fits only without the retired imports, and imports pinning more than the budget. Mutants that drop the count on retirement, drop the subtraction on release, admit without the retired bytes, skip the wake-up after a release, or free at once while an idle recorder's hold keeps the import each turn it red (13, 17, 8, 2 and 14 failing checks). **GTA V, under host import pressure.** GTA V (PPSA04263, content 01.011.001), standing scene, 3840x2160, with `APS5_HOST_IMPORT_MIB=4096` so that the budget refuses imports (at the default budget neither side refused anything in this scene, with about 8.1 GiB imported). Counts over 75 s after the player stands still, one boot per side, `APS5_PROFILE_DRAW=1` (counts only, never timing), with a local counter patch applied identically to both sides and not part of this PR ([diag-imp.patch](https://github.com/chitters2004/AnyPS5/releases/download/pr-imp-traces/diag-imp.patch)). `main` was `7383be1a19`, the change `0159dee3a1`, this branch before its rebase onto `46552cb461` (`git range-diff` shows the same patches). | 75 s window | `main` | this change | |---|---|---| | presents / draws | 380 / 1,195,723 | 479 / 1,419,887 | | imports refused (per draw) | 111,188 (0.093) | 94,287 (0.066) | | refusals by requester: map / build / access | 6 / 111,182 / 0 | 5 / 92,800 / 1,482 | | imports live at the window end | 86 (4,096 MiB) | 203 (4,096 MiB) | | GPU copies out of imports, KiB per frame | 11,313 | 11,447 | | staged copy-in, KiB per frame | 11,434 | 11,358 | | snapshot compares per frame | 248 | 3.2 | | image mirror refreshes per frame (per draw) | 8,349 (2.66) | 2,912 (0.98) | | image mirror blocks copied per frame | 263 | 86 | | retired and still pinned | not counted on `main` | 16.2 MiB at the first print, 0.2 MiB at the last | Refusals per draw are 29 % lower, copies and staging per frame are within 1.2 % of `main` (two `main` boots at the default budget differ by 1.4 to 2.6 % on these counters), the fallback work that refusals cause is lower, and the retired-and-pinned bytes drain. 1,482 refusals in the window come from the access requester on this change and none on `main`; their cause was not traced. One boot per side, and the two boots drew a different number of draws per present (about 3,150 and 2,960), so the sizes are uncertain; the direction is the same for every fallback counter. This is a fix, not a speed claim. Frame time in two interleaved pairs of the standing scene at the default budget (warm caches, same settings as above but without the counter patch or `APS5_PROFILE_DRAW`) shows no change beyond noise: | capture | commit | presents in 30 s | mean ms/frame | p50 | p90 | p99 | max | GPU busy ms/present | queue worker busy | |---|---|---|---|---|---|---|---|---|---| | main, pair 1 | `7383be1a19c7` | 219 | 137.5 | 136.2 | 140.1 | 179.0 | 193.0 | 34.0 | 94.4 % | | change, pair 1 | `0159dee3a18e` | 220 | 136.7 | 135.7 | 141.5 | 158.4 | 200.3 | 33.7 | 94.8 % | | main, pair 2 | `7383be1a19c7` | 221 | 136.2 | 134.9 | 141.8 | 179.9 | 198.3 | 33.7 | 94.9 % | | change, pair 2 | `0159dee3a18e` | 217 | 138.8 | 137.3 | 143.6 | 180.8 | 211.5 | 33.8 | 95.0 % | Change minus main, mean ms/frame: pair 1 -0.77, pair 2 +2.58, mean +0.90 (+0.7 %); p50 -0.5 and +2.4; p99 -20.6 and +0.9. Twelve `main` captures of this scene give a standard deviation of 1.85 ms (1.35 %) per frame, and the pair deltas have opposite signs. These pairs ran on this branch before its rebase (`0159dee3a1`), on the host before Resizable BAR was turned on and Windows moved from build 26220 to 26320 (driver 621.14 on both). Summary lines, digests and the deviations from the Performance report form are attached to the [pr-imp-traces release](https://github.com/chitters2004/AnyPS5/releases/tag/pr-imp-traces). ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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 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 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: NVIDIA GeForce RTX 3080 (driver 621.14, Windows 10.0.26220); Mesa lavapipe on the fork's GitHub runners (Linux and Windows) - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
gabichuuuuu commented 2026-11-07 16:06:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-07T16:06:16Z.

Review: COMMENTED


One ordering change that lets the pinned total go past the budget while a replacement is created.

On main (importRunAt), the imports a run replaces are retired, and retireLeastRecent frees down to the budget, before ImportConstruction. At 0254c4186f both steps come after the new import is created: GuestMemory::ImportWatched calls createImport at GuestBufferMemory.cpp:2459, but retireImport for the replaced range runs at 2538–2543 and retireLeastRecent at 2544. Admission at 2362–2366 counts the replaced and evictable bytes as already free when no recorder is active (immediate). So while createImport runs, every byte it counted is still pinned.

Example, budget B = 256 MiB, no recorder:

  • A 64 MiB import A covers half of a 128 MiB run, and an unrelated 192 MiB import C is evictable, for 256 MiB pinned.
  • A request for the whole run gives live = 192, and 192 + 128 > 256. So evictable = 192 and the request is admitted.
  • createImport allocates 128 MiB with 256 MiB still pinned, which makes 384 MiB, or B + 128. On main, A and then C were retired first, so the peak stayed at B.

This contradicts the description's "The budget bounds every pinned import". If the driver refuses the host-pointer allocation over heapBudget − granted/8, the run falls to copies where main imported it, and allocationFailed switches the budget source. That last part is inferred, not run.

I see why the order changed: retireLeastRecent doesn't skip [base, base + bytes), so moving it back as is could evict the very imports being replaced. Retiring the unrelated least-recent imports before the construction would keep the bound, for example with a variant that skips entries overlapping the run, like evictableBytes already does. Otherwise the debt entry could state the transient overshoot. A test with mock.importLimit equal to the budget, a replacement that evicts an LRU import, and no recorder would show which one happens.

Read only; not built or run here.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5813479350 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16751#pullrequestreview-5813479350). Originally posted by [gabichuuuuu](https://github.com/gabichuuuuu) on 2026-11-07T16:06:16Z. **Review:** COMMENTED --- One ordering change that lets the pinned total go past the budget while a replacement is created. On `main` (`importRunAt`), the imports a run replaces are retired, and `retireLeastRecent` frees down to the budget, before `ImportConstruction`. At `0254c4186f` both steps come after the new import is created: `GuestMemory::ImportWatched` calls `createImport` at `GuestBufferMemory.cpp:2459`, but `retireImport` for the replaced range runs at 2538–2543 and `retireLeastRecent` at 2544. Admission at 2362–2366 counts the `replaced` and `evictable` bytes as already free when no recorder is active (`immediate`). So while `createImport` runs, every byte it counted is still pinned. Example, budget B = 256 MiB, no recorder: - A 64 MiB import A covers half of a 128 MiB run, and an unrelated 192 MiB import C is evictable, for 256 MiB pinned. - A request for the whole run gives `live = 192`, and `192 + 128 > 256`. So `evictable = 192` and the request is admitted. - `createImport` allocates 128 MiB with 256 MiB still pinned, which makes 384 MiB, or B + 128. On `main`, A and then C were retired first, so the peak stayed at B. This contradicts the description's "The budget bounds every pinned import". If the driver refuses the host-pointer allocation over `heapBudget − granted/8`, the run falls to copies where `main` imported it, and `allocationFailed` switches the budget source. That last part is inferred, not run. I see why the order changed: `retireLeastRecent` doesn't skip `[base, base + bytes)`, so moving it back as is could evict the very imports being replaced. Retiring the unrelated least-recent imports before the construction would keep the bound, for example with a variant that skips entries overlapping the run, like `evictableBytes` already does. Otherwise the debt entry could state the transient overshoot. A test with `mock.importLimit` equal to the budget, a replacement that evicts an LRU import, and no recorder would show which one happens. Read only; not built or run here. AI-assisted: yes.
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!16751
No description provided.