fix(agc): keep resident heap mirrors without host copies #15296

Open
Prophet069 wants to merge 6 commits from upstream-pr/15296 into main
Prophet069 commented 2026-11-02 23:01:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Prophet069 on 2026-11-02T23:01:25Z.


What

A resident heap mirror kept a 16 MiB host copy of every slab, so building a multi-GiB mirror needed as much host memory again, and a rebuild while the old mirror was alive needed it twice. The host copies are gone.

A refresh now only marks the changed ranges (ResidentBuffer::MarkChanged, an interval map in which a later mark wins). The flush reads the marked ranges straight from guest memory into bounded 64 MiB staging segments, waits after every 256 MiB when the caller allows it, and fills newly bound or zeroed blocks on the device with vkCmdFillBuffer.

The write-back reference of each use is a device copy of the written ranges recorded after the uploads and before the shader (ResidentBuffer::RecordReference); the write-back compares the readback after the shader with it, so only bytes the shader changed reach the guest and CPU writes made meanwhile are kept. Non-resident heap mirrors keep their pooled host references.

Reference, upload and readback buffers count as kept bytes of the recorder, so its existing bound submits before they pile up, and references made inside a batch completion are recorded in order with the uploads.

Tests: partly written 64 KiB blocks, CPU writes made while a dispatch is in flight, and two dispatches writing the same range.

Tested

NVIDIA GeForce RTX 3080 Laptop GPU, driver 616.64, Windows 10 Pro 22H2 (19045.6466).
Focused tests on main (9090972b7e) and on this branch with VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, ANYPS5_NO_SHADER_CACHE=1, ANYPS5_REQUIRE_VULKAN=1, ctest -V -R "heap_mirror|^agc_driver_graphics$|^agc_driver_key_miss$|^agc_driver_bda_device$|^agc_driver_recorder$|native_multisample_depth|depth_storage|^agc_host_import_retire$|^agc_host_import_sparse$":

  • main: 17 of 20 selected tests passed (agc_host_import_sparse skips on this machine; the three agc_driver_depth_storage_* tests were not built in this run); validation layer loaded in both logs; VUID- errors: none.
  • this branch: 17 of 20 selected tests passed (agc_host_import_sparse skips on this machine; the three agc_driver_depth_storage_* tests were not built in this run), the same as main; VUID- errors: none.
  • agc_driver_resident_heap_mirror includes the new partial-block and in-flight CPU write cases.
  • Death Stranding 2 (PPSA02015), local integration branch with the GPU heap path pull requests: two of three runs hit the host memory limit while building ~6 GiB heap mirrors at world load (16 GB RAM machine); with this change the world loaded and the run reached its 45 minute limit in the opening cutscene.

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: #15295, #12827
  • Fixes: none
  • Unblocks: PPSA02015 #5424
  • GPUs tested: NVIDIA GeForce RTX 3080 Laptop GPU, driver 616.64, Windows 10 Pro 22H2 (19045.6466)
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5128813660 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15296). Originally posted by [Prophet069](https://github.com/Prophet069) on 2026-11-02T23:01:25Z. --- ### What A resident heap mirror kept a 16 MiB host copy of every slab, so building a multi-GiB mirror needed as much host memory again, and a rebuild while the old mirror was alive needed it twice. The host copies are gone. A refresh now only marks the changed ranges (`ResidentBuffer::MarkChanged`, an interval map in which a later mark wins). The flush reads the marked ranges straight from guest memory into bounded 64 MiB staging segments, waits after every 256 MiB when the caller allows it, and fills newly bound or zeroed blocks on the device with `vkCmdFillBuffer`. The write-back reference of each use is a device copy of the written ranges recorded after the uploads and before the shader (`ResidentBuffer::RecordReference`); the write-back compares the readback after the shader with it, so only bytes the shader changed reach the guest and CPU writes made meanwhile are kept. Non-resident heap mirrors keep their pooled host references. Reference, upload and readback buffers count as kept bytes of the recorder, so its existing bound submits before they pile up, and references made inside a batch completion are recorded in order with the uploads. Tests: partly written 64 KiB blocks, CPU writes made while a dispatch is in flight, and two dispatches writing the same range. ### Tested NVIDIA GeForce RTX 3080 Laptop GPU, driver 616.64, Windows 10 Pro 22H2 (19045.6466). Focused tests on `main` (9090972b7e) and on this branch with `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`, `ANYPS5_NO_SHADER_CACHE=1`, `ANYPS5_REQUIRE_VULKAN=1`, `ctest -V -R "heap_mirror|^agc_driver_graphics$|^agc_driver_key_miss$|^agc_driver_bda_device$|^agc_driver_recorder$|native_multisample_depth|depth_storage|^agc_host_import_retire$|^agc_host_import_sparse$"`: - `main`: 17 of 20 selected tests passed (agc_host_import_sparse skips on this machine; the three agc_driver_depth_storage_* tests were not built in this run); validation layer loaded in both logs; `VUID-` errors: none. - this branch: 17 of 20 selected tests passed (agc_host_import_sparse skips on this machine; the three agc_driver_depth_storage_* tests were not built in this run), the same as main; `VUID-` errors: none. - `agc_driver_resident_heap_mirror` includes the new partial-block and in-flight CPU write cases. - Death Stranding 2 (PPSA02015), local integration branch with the GPU heap path pull requests: two of three runs hit the host memory limit while building ~6 GiB heap mirrors at world load (16 GB RAM machine); with this change the world loaded and the run reached its 45 minute limit in the opening cutscene. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed 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: #15295, #12827 - [x] Fixes: none - [x] Unblocks: PPSA02015 #5424 - [x] GPUs tested: NVIDIA GeForce RTX 3080 Laptop GPU, driver 616.64, Windows 10 Pro 22H2 (19045.6466) - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
XairRrR commented 2026-11-03 00:12:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-11-03T00:12:28Z.

Review: COMMENTED


Reviewed — comments

The PR merges and builds, the heap-mirror tests pass, and the validation comparison shows no new VUIDs. But the new tests also pass with main's production code, and the new slab-refusal, budget-shrink and nonzero-scan-cache paths have no test.

What I ran

Head a64a9a4a280cb5be0f80bb01ce98812e56bcda6d, main 5cb015caaaaf83f8ee431414ee6a457b5b307d49, merged tree a5a36646c486d472781b0f1732a84113e9acecda (--no-ff), Debian 13 VM, GCC 14.2.0, lavapipe (Mesa 26.1.6, LLVM 19).

git diff --check 5cb015caaaaf83f8ee431414ee6a457b5b307d49...a64a9a4a280cb5be0f80bb01ce98812e56bcda6d
python3 tools/check_conventions.py --base 5cb015caaaaf83f8ee431414ee6a457b5b307d49
ctest --test-dir build -j8 --test-load 32 --output-on-failure -R "heap_mirror|lod_clamp"
# negative control: the five production files from main
git show 5cb015caaa…:core/libs/prx/libSceAgcDriver/Graphics/{include/Context.hpp,include/GuestBufferMemory.hpp,include/ResidentMirror.hpp,src/GuestBufferMemory.cpp,src/ResidentMirror.cpp} > …
ninja -C build -j32 -l32 agc_driver_resident_heap_mirror_tests agc_driver_image_sample_lod_clamp_tests
ctest --test-dir build --output-on-failure -R "^agc_driver_resident_heap_mirror$|lod_clamp_shader_lane"
# restore the merged files from a5a36646c4…, rebuild everything
ninja -C build -j32 -l32
git status --porcelain

Results

Check Result
git diff --check clean
check_conventions.py All conventions checks passed
Configure / build / libs (script) ok
agc_driver_resident_heap_mirror (with the new PartialBlockTests and ConcurrentWriteTests) passed
agc_heap_mirror_*, agc_sparse_heap_mirror_* (10 tests) passed
agc_driver_image_sample_lod_clamp_shader_lane (listed by the script as a new failure) fails, and fails the same way with main's production files, so it comes from main (baseline was the older 096c45570f), not from this PR
unwrap_self_..._destination_directory_group (listed as a new failure) a Python tool test failing on os.chown in a temporary directory (EINVAL); this PR changes no tool or relinker file, so it is the environment, not the PR
Validation layer (script, 759 tests) layer loaded in 758, missing in none, 0 tests with new VUID- lines
Worktree after restore git status --porcelain empty
lod_clamp_shader_lane output (same on merged tree and with main's production files)
AGC graphics: image_sample_lz through the S# that holds the LOD at 0 with T# MIN_LOD 768 over BASE_LEVEL 0 with a second S# of words 1 0 and 2 5242880, and a second T# of MIN_LOD 0 and LAST_LEVEL 0: thread 0 component 1 is 0.000000, expected 0.062500

Negative control

I put back main's versions of Context.hpp, GuestBufferMemory.hpp, ResidentMirror.hpp, GuestBufferMemory.cpp and ResidentMirror.cpp, kept the PR's tests/execution/ResidentHeapMirror.cpp, and rebuilt. agc_driver_resident_heap_mirror still passed (2.23 s).

That can be expected: the new partial-block, in-flight CPU write and double-dispatch cases describe behaviour that main's host-copy design already had. So they guard against regressions in the rewrite, but they don't show the change itself. Nothing tests the host-memory saving, the slab refusal, the budget shrink-and-retry loop or the nonzero-scan cache. I then put the merged files back, rebuilt the whole tree, and checked that the worktree is clean.

Findings

Blocking

None.

Non-blocking

  1. No test reaches the slab-refusal and budget-shrink path. ResidentMemory::refuseSlab (core/libs/prx/libSceAgcDriver/Graphics/src/ResidentMirror.cpp:176), shrinkHeapMirrorBudget (core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp:2814) and the 8-attempt retry loops (GuestBufferMemory.cpp:2849, GuestBufferMemory.cpp:3387) are the fix the PR depends on for PPSA02015. They run only when the device heap fills or when slabs pass APS5_HEAP_MIRROR_MIB. The existing tests pass on main without any of this code. Suggested fix: add a case that sets a small budget with SetHeapMirrorBudget (which HeapMirrorSlabBudget already reads). It should check that a slab past it is refused, the budget is lowered, an older mirror is evicted, and the new mirror is still built.
  2. The lowered budget never goes back up. heapMirrorMemoryLimit (GuestBufferMemory.cpp:2714) is only ever lowered, at GuestBufferMemory.cpp:2824, and nothing raises it again. So one refusal caused by a temporary peak in device-heap use leaves heapMirrorBudget() lowered for the rest of the process. The soft-target escape in reserveHeapBytes/growResident only applies on a retry, after a sweep has already evicted mirrors. So after one temporary peak, mirrors keep being evicted early for the whole session. Suggested fix: raise the limit again once the reported heap has headroom, or say in the description that the drop is permanent by design.
  3. The refusal is recognised by matching the error text. shrinkHeapMirrorBudget tests error.what() for "of a heap mirror slab". A change to either message (refuseSlab or allocate's operation string) would silently turn off the retry and bring back the stop this PR fixes. Suggested fix: use a dedicated exception type derived from std::runtime_error, or a flag returned by Take.
  4. Context::HeapReport::Fits is never used (core/libs/prx/libSceAgcDriver/Graphics/include/Context.hpp:340). refuseSlab does its own headroom arithmetic. Either use Fits or remove it.
  5. The nonzero-scan cache is shared global state. GuestNonzeroBlocks now keeps a process-wide unordered_map of up to 4M entries (ResidentMirror.cpp:379-380) behind one mutex, which it takes once or twice per 64 KiB block. The whole map is dropped when it fills (ResidentMirror.cpp:408). Its correctness depends on UnchangedSince coming back false for pages that were unmapped and mapped again (ForgetPages). I didn't find a test of that for this cache (ReusedAddressTests passes, but it also passed before the cache existed). This is the third separate change in the first commit; see the next item.
  6. One topic per PR. Besides dropping the host copies, the PR also includes a cherry-picked commit by another author (8a30d9a70f8b, "store a resident heap mirror's write-back from its readback buffer"), the slab-refusal and fallback work, fullest-slab allocation, and the scan cache (1743177ae4af). The checklist names Depends on: #15295, #12827. If those PRs carry some of these commits, the commits would be merged twice or conflict. Please state which commits belong to which PR, or move the separate fixes into their own PRs.

Not verified

  • The claimed host-memory saving and the PPSA02015 world-load result: I launched no title. lavapipe has a 64 GiB host-backed heap, so the device-heap headroom refusal and the host-memory fallback never run here.
  • The Windows/NVIDIA validation run in the description: I only ran on lavapipe.
  • The contents of #15295 and #12827: no context was available, so I couldn't check the overlap described in finding 6.
  • Whether a RecordStagingCopies flush that uploads newly marked ranges after takeHeapReferences recorded the device reference could put those bytes into the write-back. That would happen if the range differs between the reference and the readback without any shader store. I didn't build a test for this ordering. The new ConcurrentWriteTests doesn't cover it.

To get this to a good state

  • Add a test that reaches the refusal, shrink, sweep and retry path (finding 1). Ideally also add one for the scan cache across an unmap and remap at the same address (finding 5).
  • Decide whether the lowered budget should recover (finding 2), and replace the error-text match (finding 3).
  • Remove or use HeapReport::Fits (finding 4).
  • Clarify how this PR's commits relate to #15295 and #12827, or split the separate fixes out (finding 6).
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5745258116 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15296#pullrequestreview-5745258116). Originally posted by [XairRrR](https://github.com/XairRrR) on 2026-11-03T00:12:28Z. **Review:** COMMENTED --- # Reviewed — comments The PR merges and builds, the heap-mirror tests pass, and the validation comparison shows no new VUIDs. But the new tests also pass with `main`'s production code, and the new slab-refusal, budget-shrink and nonzero-scan-cache paths have no test. ## What I ran Head `a64a9a4a280cb5be0f80bb01ce98812e56bcda6d`, `main` `5cb015caaaaf83f8ee431414ee6a457b5b307d49`, merged tree `a5a36646c486d472781b0f1732a84113e9acecda` (`--no-ff`), Debian 13 VM, GCC 14.2.0, lavapipe (Mesa 26.1.6, LLVM 19). ``` git diff --check 5cb015caaaaf83f8ee431414ee6a457b5b307d49...a64a9a4a280cb5be0f80bb01ce98812e56bcda6d python3 tools/check_conventions.py --base 5cb015caaaaf83f8ee431414ee6a457b5b307d49 ctest --test-dir build -j8 --test-load 32 --output-on-failure -R "heap_mirror|lod_clamp" # negative control: the five production files from main git show 5cb015caaa…:core/libs/prx/libSceAgcDriver/Graphics/{include/Context.hpp,include/GuestBufferMemory.hpp,include/ResidentMirror.hpp,src/GuestBufferMemory.cpp,src/ResidentMirror.cpp} > … ninja -C build -j32 -l32 agc_driver_resident_heap_mirror_tests agc_driver_image_sample_lod_clamp_tests ctest --test-dir build --output-on-failure -R "^agc_driver_resident_heap_mirror$|lod_clamp_shader_lane" # restore the merged files from a5a36646c4…, rebuild everything ninja -C build -j32 -l32 git status --porcelain ``` ## Results | Check | Result | |---|---| | `git diff --check` | clean | | `check_conventions.py` | All conventions checks passed | | Configure / build / libs (script) | ok | | `agc_driver_resident_heap_mirror` (with the new `PartialBlockTests` and `ConcurrentWriteTests`) | passed | | `agc_heap_mirror_*`, `agc_sparse_heap_mirror_*` (10 tests) | passed | | `agc_driver_image_sample_lod_clamp_shader_lane` (listed by the script as a new failure) | fails, **and fails the same way with `main`'s production files**, so it comes from `main` (baseline was the older `096c45570f`), not from this PR | | `unwrap_self_..._destination_directory_group` (listed as a new failure) | a Python tool test failing on `os.chown` in a temporary directory (EINVAL); this PR changes no tool or relinker file, so it is the environment, not the PR | | Validation layer (script, 759 tests) | layer loaded in 758, missing in none, 0 tests with new `VUID-` lines | | Worktree after restore | `git status --porcelain` empty | <details><summary>lod_clamp_shader_lane output (same on merged tree and with main's production files)</summary> ``` AGC graphics: image_sample_lz through the S# that holds the LOD at 0 with T# MIN_LOD 768 over BASE_LEVEL 0 with a second S# of words 1 0 and 2 5242880, and a second T# of MIN_LOD 0 and LAST_LEVEL 0: thread 0 component 1 is 0.000000, expected 0.062500 ``` </details> ## Negative control I put back `main`'s versions of `Context.hpp`, `GuestBufferMemory.hpp`, `ResidentMirror.hpp`, `GuestBufferMemory.cpp` and `ResidentMirror.cpp`, kept the PR's `tests/execution/ResidentHeapMirror.cpp`, and rebuilt. `agc_driver_resident_heap_mirror` **still passed** (2.23 s). That can be expected: the new partial-block, in-flight CPU write and double-dispatch cases describe behaviour that `main`'s host-copy design already had. So they guard against regressions in the rewrite, but they don't show the change itself. Nothing tests the host-memory saving, the slab refusal, the budget shrink-and-retry loop or the nonzero-scan cache. I then put the merged files back, rebuilt the whole tree, and checked that the worktree is clean. ## Findings ### Blocking None. ### Non-blocking 1. **No test reaches the slab-refusal and budget-shrink path.** `ResidentMemory::refuseSlab` (`core/libs/prx/libSceAgcDriver/Graphics/src/ResidentMirror.cpp:176`), `shrinkHeapMirrorBudget` (`core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp:2814`) and the 8-attempt retry loops (`GuestBufferMemory.cpp:2849`, `GuestBufferMemory.cpp:3387`) are the fix the PR depends on for PPSA02015. They run only when the device heap fills or when slabs pass `APS5_HEAP_MIRROR_MIB`. The existing tests pass on `main` without any of this code. Suggested fix: add a case that sets a small budget with `SetHeapMirrorBudget` (which `HeapMirrorSlabBudget` already reads). It should check that a slab past it is refused, the budget is lowered, an older mirror is evicted, and the new mirror is still built. 2. **The lowered budget never goes back up.** `heapMirrorMemoryLimit` (`GuestBufferMemory.cpp:2714`) is only ever lowered, at `GuestBufferMemory.cpp:2824`, and nothing raises it again. So one refusal caused by a temporary peak in device-heap use leaves `heapMirrorBudget()` lowered for the rest of the process. The soft-target escape in `reserveHeapBytes`/`growResident` only applies on a retry, after a sweep has already evicted mirrors. So after one temporary peak, mirrors keep being evicted early for the whole session. Suggested fix: raise the limit again once the reported heap has headroom, or say in the description that the drop is permanent by design. 3. **The refusal is recognised by matching the error text.** `shrinkHeapMirrorBudget` tests `error.what()` for `"of a heap mirror slab"`. A change to either message (`refuseSlab` or `allocate`'s operation string) would silently turn off the retry and bring back the stop this PR fixes. Suggested fix: use a dedicated exception type derived from `std::runtime_error`, or a flag returned by `Take`. 4. **`Context::HeapReport::Fits` is never used** (`core/libs/prx/libSceAgcDriver/Graphics/include/Context.hpp:340`). `refuseSlab` does its own headroom arithmetic. Either use `Fits` or remove it. 5. **The nonzero-scan cache is shared global state.** `GuestNonzeroBlocks` now keeps a process-wide `unordered_map` of up to 4M entries (`ResidentMirror.cpp:379-380`) behind one mutex, which it takes once or twice per 64 KiB block. The whole map is dropped when it fills (`ResidentMirror.cpp:408`). Its correctness depends on `UnchangedSince` coming back false for pages that were unmapped and mapped again (`ForgetPages`). I didn't find a test of that for this cache (`ReusedAddressTests` passes, but it also passed before the cache existed). This is the third separate change in the first commit; see the next item. 6. **One topic per PR.** Besides dropping the host copies, the PR also includes a cherry-picked commit by another author (`8a30d9a70f8b`, "store a resident heap mirror's write-back from its readback buffer"), the slab-refusal and fallback work, fullest-slab allocation, and the scan cache (`1743177ae4af`). The checklist names `Depends on: #15295, #12827`. If those PRs carry some of these commits, the commits would be merged twice or conflict. Please state which commits belong to which PR, or move the separate fixes into their own PRs. ## Not verified - The claimed host-memory saving and the PPSA02015 world-load result: I launched no title. lavapipe has a 64 GiB host-backed heap, so the device-heap headroom refusal and the host-memory fallback never run here. - The Windows/NVIDIA validation run in the description: I only ran on lavapipe. - The contents of #15295 and #12827: no context was available, so I couldn't check the overlap described in finding 6. - Whether a `RecordStagingCopies` flush that uploads newly marked ranges *after* `takeHeapReferences` recorded the device reference could put those bytes into the write-back. That would happen if the range differs between the reference and the readback without any shader store. I didn't build a test for this ordering. The new `ConcurrentWriteTests` doesn't cover it. ## To get this to a good state - Add a test that reaches the refusal, shrink, sweep and retry path (finding 1). Ideally also add one for the scan cache across an unmap and remap at the same address (finding 5). - Decide whether the lowered budget should recover (finding 2), and replace the error-text match (finding 3). - Remove or use `HeapReport::Fits` (finding 4). - Clarify how this PR's commits relate to #15295 and #12827, or split the separate fixes out (finding 6). <!-- anyps5-bots job=2710 head=a64a9a4a280cb5be0f80bb01ce98812e56bcda6d -->
XairRrR commented 2026-11-03 06:15:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-11-03T06:15:30Z.

Review: APPROVED


Re-checked — approved

The new commit 89e3292072 ("let a small heap mirror reach the slab allocation and its host fallback") does four things. It replaces the error-text match with a HeapMirrorSlabRefused type. It lets a lowered budget recover after 30 s. It uses HeapReport::Fits. It refuses a slab only while the heap mirrors hold more than 256 MiB. Its new FullDeviceHeapTests case fails when that last guard is removed. The other commits were rebased onto it with the same content.

What changed since my last review

  • New commit 89e329207201, between the slab-refusal commit and the cherry-picked write-back commit. The later commits were rebased onto it. Per the range-diff, they only change around the new test struct and the try/catch in ResidentMemory::Take. The head commit 7bbf091cf8 is unchanged.
  • The author explained in discussion how the commits split across PRs. The first two commits are #15295, 7c184fb3ae is the cherry-pick from #12827, and the last three are this PR's own. The checklist says Depends on: #15295, #12827. The author also agreed with EagleBearerYS that #12840 lands after this PR.

What I ran

Head 7bbf091cf83a767b16e38f966d2d72d9321b0d81, main d1ae628bc2f62459cce1e9872ba5632c938c71f2, merged tree bf1ec647053b1866f995446eb40a85dcac8f0323 (--no-ff). Debian 13 VM, GCC 14.2.0, lavapipe (Mesa 26.1.6, LLVM 19).

git diff --check d1ae628bc2f62459cce1e9872ba5632c938c71f2...7bbf091cf83a767b16e38f966d2d72d9321b0d81
python3 tools/check_conventions.py --base d1ae628bc2f62459cce1e9872ba5632c938c71f2
ctest --test-dir build -j8 --test-load 32 --output-on-failure -R "heap_mirror"
# negative control: delete `if (live <= Reserve) return;` from ResidentMemory::refuseSlab
sed -i "/if (live <= Reserve) return;/d" core/libs/prx/libSceAgcDriver/Graphics/src/ResidentMirror.cpp
ninja -C build -j32 -l32 agc_driver_resident_heap_mirror_tests
ctest --test-dir build --output-on-failure -R "^agc_driver_resident_heap_mirror$"
# restore
git show bf1ec647053b1866f995446eb40a85dcac8f0323:core/libs/prx/libSceAgcDriver/Graphics/src/ResidentMirror.cpp > core/libs/prx/libSceAgcDriver/Graphics/src/ResidentMirror.cpp
ninja -C build -j32 -l32
ctest --test-dir build --output-on-failure -R "^agc_driver_resident_heap_mirror$"
git status --porcelain
Check Result
git diff --check clean
check_conventions.py All conventions checks passed
Configure / build / libs (script) ok
heap_mirror tests (10, including agc_driver_resident_heap_mirror with FullDeviceHeapTests) all passed (2.5 s)
Validation layer (script, 767 tests) layer loaded in 766, missing in none, 0 tests with new VUID- lines
unwrap_self_..._destination_directory_group (script: new failure) os.chown on a temporary directory fails with EINVAL. This PR changes no tool file, so the cause is the environment, as in my last review
Negative control (guard removed) agc_driver_resident_heap_mirror fails
After restore test passes, git status --porcelain empty
Negative control output
[heap-mirror] refused: slabs 0 (0 MiB), free slots 0 MiB, live mirrors 0 bound 0 MiB, budget 24576 MiB
AGC graphics: heap mirror of 0x7f7b55800000+0x2000000: vkAllocateMemory of a heap mirror slab refused: its device heap would keep less than 256 MiB of its reported budget; heap mirrors hold 15 MiB of 24576 MiB (APS5_HEAP_MIRROR_MIB)

I didn't do a full restore of main's production files. The new test calls SetDeviceHeapFullForTests, which doesn't exist on main, so it wouldn't compile. Removing only the guard is the narrower control, and it shows the new test exercises the headroom refusal.

Previous findings

  1. No test reaches the refusal/shrink path. Partly resolved, still open for the shrink. FullDeviceHeapTests (core/libs/prx/libSceAgcDriver/tests/execution/ResidentHeapMirror.cpp:370) now reaches refuseSlab (see the negative control). But it holds less than 256 MiB, so shrinkHeapMirrorBudget returns at core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp:2841. The lowering, the eviction sweep and the 8-attempt retry are still not run by any test. Non-blocking.
  2. The lowered budget never recovers. Resolved. heapMirrorBudget puts the limit back after 30 s (GuestBufferMemory.cpp:2734), and the time a refusal lowers it is stored at GuestBufferMemory.cpp:2842. No test covers it.
  3. Refusal matched by error text. Resolved. It is now dynamic_cast<const HeapMirrorSlabRefused*> (GuestBufferMemory.cpp:2834). Both the refusal and a failed slab allocation throw that type (core/libs/prx/libSceAgcDriver/Graphics/src/ResidentMirror.cpp:155-159, :199). That is the same set of errors the old text match caught.
  4. HeapReport::Fits unused. Resolved. It is used at ResidentMirror.cpp:193.
  5. Nonzero-scan cache is shared global state, with no test across unmap and remap. Still open, unchanged (ResidentMirror.cpp:381). Non-blocking.
  6. One topic per PR. Resolved by declaration. The commits are attributed to #15295 and #12827 in discussion, and the checklist says Depends on: #15295, #12827. The landing order is author-reported. I couldn't check the other PRs' heads.

New findings

Blocking

None.

Non-blocking

  1. A flush after the reference copy is still possible. oneandonlydean raised this, and it was under "Not verified" in my last review. The author said they would look at it next, and the head commit hasn't changed. I can confirm the order this would happen in. recordTransfer records uploads into recorder->Commands() in sequence (ResidentMirror.cpp:61-68), and so does RecordReference (ResidentMirror.cpp:616-627). So any range still marked when RecordStagingCopies calls ResidentBuffer::FlushChanged(context, true) (GuestBufferMemory.cpp:6928) is uploaded after the reference that takeHeapReferences copied (GuestBufferMemory.cpp:6715-6718). The write-back would then treat it as a shader write. I didn't build a case that puts a mark between the two calls. Suggested fix: show that no mark can land there, or make the write-back ignore ranges flushed after the reference. Add a test either way.
  2. Recovery is by time only. After 30 s the limit goes back to unlimited, whether or not the device heap has room again (GuestBufferMemory.cpp:2734). On a heap that stays full, this can mean another refuse, shrink and evict cycle every 30 s. That is acceptable as a design choice, but a short note in the commit or description would help.

Not verified

  • The host-memory saving and the PPSA02015 world-load result: I launched no title.
  • The NVIDIA/Windows run in the description (author-reported). lavapipe reports a large host-backed heap, so the real headroom refusal and the host fallback only ran through the test override.
  • The shrink, evict and retry loop, and the 30 s recovery: no test reaches either, and I didn't write one.
  • The contents and landing order of #15295, #12827 and #12840.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5747944392 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15296#pullrequestreview-5747944392). Originally posted by [XairRrR](https://github.com/XairRrR) on 2026-11-03T06:15:30Z. **Review:** APPROVED --- # Re-checked — approved The new commit `89e3292072` ("let a small heap mirror reach the slab allocation and its host fallback") does four things. It replaces the error-text match with a `HeapMirrorSlabRefused` type. It lets a lowered budget recover after 30 s. It uses `HeapReport::Fits`. It refuses a slab only while the heap mirrors hold more than 256 MiB. Its new `FullDeviceHeapTests` case fails when that last guard is removed. The other commits were rebased onto it with the same content. ## What changed since my last review - New commit `89e329207201`, between the slab-refusal commit and the cherry-picked write-back commit. The later commits were rebased onto it. Per the range-diff, they only change around the new test struct and the `try`/`catch` in `ResidentMemory::Take`. The head commit `7bbf091cf8` is unchanged. - The author explained in discussion how the commits split across PRs. The first two commits are #15295, `7c184fb3ae` is the cherry-pick from #12827, and the last three are this PR's own. The checklist says `Depends on: #15295, #12827`. The author also agreed with EagleBearerYS that #12840 lands after this PR. ## What I ran Head `7bbf091cf83a767b16e38f966d2d72d9321b0d81`, `main` `d1ae628bc2f62459cce1e9872ba5632c938c71f2`, merged tree `bf1ec647053b1866f995446eb40a85dcac8f0323` (`--no-ff`). Debian 13 VM, GCC 14.2.0, lavapipe (Mesa 26.1.6, LLVM 19). ``` git diff --check d1ae628bc2f62459cce1e9872ba5632c938c71f2...7bbf091cf83a767b16e38f966d2d72d9321b0d81 python3 tools/check_conventions.py --base d1ae628bc2f62459cce1e9872ba5632c938c71f2 ctest --test-dir build -j8 --test-load 32 --output-on-failure -R "heap_mirror" # negative control: delete `if (live <= Reserve) return;` from ResidentMemory::refuseSlab sed -i "/if (live <= Reserve) return;/d" core/libs/prx/libSceAgcDriver/Graphics/src/ResidentMirror.cpp ninja -C build -j32 -l32 agc_driver_resident_heap_mirror_tests ctest --test-dir build --output-on-failure -R "^agc_driver_resident_heap_mirror$" # restore git show bf1ec647053b1866f995446eb40a85dcac8f0323:core/libs/prx/libSceAgcDriver/Graphics/src/ResidentMirror.cpp > core/libs/prx/libSceAgcDriver/Graphics/src/ResidentMirror.cpp ninja -C build -j32 -l32 ctest --test-dir build --output-on-failure -R "^agc_driver_resident_heap_mirror$" git status --porcelain ``` | Check | Result | |---|---| | `git diff --check` | clean | | `check_conventions.py` | All conventions checks passed | | Configure / build / libs (script) | ok | | `heap_mirror` tests (10, including `agc_driver_resident_heap_mirror` with `FullDeviceHeapTests`) | all passed (2.5 s) | | Validation layer (script, 767 tests) | layer loaded in 766, missing in none, 0 tests with new `VUID-` lines | | `unwrap_self_..._destination_directory_group` (script: new failure) | `os.chown` on a temporary directory fails with EINVAL. This PR changes no tool file, so the cause is the environment, as in my last review | | Negative control (guard removed) | `agc_driver_resident_heap_mirror` **fails** | | After restore | test passes, `git status --porcelain` empty | <details><summary>Negative control output</summary> ``` [heap-mirror] refused: slabs 0 (0 MiB), free slots 0 MiB, live mirrors 0 bound 0 MiB, budget 24576 MiB AGC graphics: heap mirror of 0x7f7b55800000+0x2000000: vkAllocateMemory of a heap mirror slab refused: its device heap would keep less than 256 MiB of its reported budget; heap mirrors hold 15 MiB of 24576 MiB (APS5_HEAP_MIRROR_MIB) ``` </details> I didn't do a full restore of `main`'s production files. The new test calls `SetDeviceHeapFullForTests`, which doesn't exist on `main`, so it wouldn't compile. Removing only the guard is the narrower control, and it shows the new test exercises the headroom refusal. ## Previous findings 1. **No test reaches the refusal/shrink path.** Partly resolved, still open for the shrink. `FullDeviceHeapTests` (`core/libs/prx/libSceAgcDriver/tests/execution/ResidentHeapMirror.cpp:370`) now reaches `refuseSlab` (see the negative control). But it holds less than 256 MiB, so `shrinkHeapMirrorBudget` returns at `core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp:2841`. The lowering, the eviction sweep and the 8-attempt retry are still not run by any test. Non-blocking. 2. **The lowered budget never recovers.** Resolved. `heapMirrorBudget` puts the limit back after 30 s (`GuestBufferMemory.cpp:2734`), and the time a refusal lowers it is stored at `GuestBufferMemory.cpp:2842`. No test covers it. 3. **Refusal matched by error text.** Resolved. It is now `dynamic_cast<const HeapMirrorSlabRefused*>` (`GuestBufferMemory.cpp:2834`). Both the refusal and a failed slab allocation throw that type (`core/libs/prx/libSceAgcDriver/Graphics/src/ResidentMirror.cpp:155-159`, `:199`). That is the same set of errors the old text match caught. 4. **`HeapReport::Fits` unused.** Resolved. It is used at `ResidentMirror.cpp:193`. 5. **Nonzero-scan cache is shared global state, with no test across unmap and remap.** Still open, unchanged (`ResidentMirror.cpp:381`). Non-blocking. 6. **One topic per PR.** Resolved by declaration. The commits are attributed to #15295 and #12827 in discussion, and the checklist says `Depends on: #15295, #12827`. The landing order is author-reported. I couldn't check the other PRs' heads. ## New findings ### Blocking None. ### Non-blocking 1. **A flush after the reference copy is still possible.** oneandonlydean raised this, and it was under "Not verified" in my last review. The author said they would look at it next, and the head commit hasn't changed. I can confirm the order this would happen in. `recordTransfer` records uploads into `recorder->Commands()` in sequence (`ResidentMirror.cpp:61-68`), and so does `RecordReference` (`ResidentMirror.cpp:616-627`). So any range still marked when `RecordStagingCopies` calls `ResidentBuffer::FlushChanged(context, true)` (`GuestBufferMemory.cpp:6928`) is uploaded after the reference that `takeHeapReferences` copied (`GuestBufferMemory.cpp:6715-6718`). The write-back would then treat it as a shader write. I didn't build a case that puts a mark between the two calls. Suggested fix: show that no mark can land there, or make the write-back ignore ranges flushed after the reference. Add a test either way. 2. **Recovery is by time only.** After 30 s the limit goes back to unlimited, whether or not the device heap has room again (`GuestBufferMemory.cpp:2734`). On a heap that stays full, this can mean another refuse, shrink and evict cycle every 30 s. That is acceptable as a design choice, but a short note in the commit or description would help. ## Not verified - The host-memory saving and the PPSA02015 world-load result: I launched no title. - The NVIDIA/Windows run in the description (author-reported). lavapipe reports a large host-backed heap, so the real headroom refusal and the host fallback only ran through the test override. - The shrink, evict and retry loop, and the 30 s recovery: no test reaches either, and I didn't write one. - The contents and landing order of #15295, #12827 and #12840. <!-- anyps5-bots job=3091 head=7bbf091cf83a767b16e38f966d2d72d9321b0d81 -->
MarcZX10 commented 2026-11-20 21:18:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T21:18:37Z.

Review: COMMENTED


Reviewed — no blocking findings

Nothing blocks this change; the checks and source ranges I inspected support merging, though I did not run the reported tests.

What I ran

Reviewed head 7bbf091cf83a767b16e38f966d2d72d9321b0d81 against the prepared diff and targeted source context. I did not run commands or tests. The author reports focused test runs on main and this branch; those are not runs I performed.

Results

Check Result Note
Source review passed Changed code and relevant callers inspected
Tests not run Only author-reported results available
Build and runtime not run No local execution

Findings

Blocking

None.

Non-blocking

None.

Not verified

I read all six changed files listed in the manifest: Context.hpp, GuestBufferMemory.hpp, ResidentMirror.hpp, GuestBufferMemory.cpp, ResidentMirror.cpp, and ResidentHeapMirror.cpp. No changed file was excluded. I did not read every added line in the large GuestBufferMemory.cpp or all added lines in the test file, so the review is not complete against the review folder's instruction to read every changed line. Build, automated tests, and physical-console behavior were not independently verified.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5999552174 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15296#pullrequestreview-5999552174). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-20T21:18:37Z. **Review:** COMMENTED --- # Reviewed — no blocking findings Nothing blocks this change; the checks and source ranges I inspected support merging, though I did not run the reported tests. ## What I ran Reviewed head `7bbf091cf83a767b16e38f966d2d72d9321b0d81` against the prepared diff and targeted source context. I did not run commands or tests. The author reports focused test runs on `main` and this branch; those are not runs I performed. ## Results | Check | Result | Note | |---|---|---| | Source review | passed | Changed code and relevant callers inspected | | Tests | not run | Only author-reported results available | | Build and runtime | not run | No local execution | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified I read all six changed files listed in the manifest: `Context.hpp`, `GuestBufferMemory.hpp`, `ResidentMirror.hpp`, `GuestBufferMemory.cpp`, `ResidentMirror.cpp`, and `ResidentHeapMirror.cpp`. No changed file was excluded. I did not read every added line in the large `GuestBufferMemory.cpp` or all added lines in the test file, so the review is not complete against the review folder's instruction to read every changed line. Build, automated tests, and physical-console behavior were not independently verified. ## To get this to a good state Nothing is required.
ligmaSec commented 2026-12-03 22:45:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T22:45:11Z.

Review: COMMENTED


The trial merge onto c1601e56d5 conflicts in the mirror context, implementation, and ResidentHeapMirror fixture. Please preserve one focused case in which two queued uses of the same slab need different write-back reference bytes. Removing the host copies means the device reference must be captured after the upload and before that use’s writes. A successful current readback alone would miss reuse of the wrong reference. The Windows result in the PR is from the author.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6179945861 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15296#pullrequestreview-6179945861). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T22:45:11Z. **Review:** COMMENTED --- The trial merge onto `c1601e56d5` conflicts in the mirror context, implementation, and `ResidentHeapMirror` fixture. Please preserve one focused case in which two queued uses of the same slab need different write-back reference bytes. Removing the host copies means the device reference must be captured after the upload and before that use’s writes. A successful current readback alone would miss reuse of the wrong reference. The Windows result in the PR is from the author. AI-assisted: yes.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/15296:upstream-pr/15296'
git switch 'upstream-pr/15296'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/15296'
git switch 'upstream-pr/15296'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/15296'
git switch 'upstream-pr/15296'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/15296'
git switch 'main'
git merge --squash 'upstream-pr/15296'
git switch 'main'
git merge --ff-only 'upstream-pr/15296'
git switch 'main'
git merge 'upstream-pr/15296'
git push origin 'main'
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!15296
No description provided.