perf(agc): refuse a staging shadow that would take the last eighth of the device-local budget #14815
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/14815"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by hugodearaujo on 2026-11-01T18:18:38Z.
What
A staged buffer region (a written element, a swept range up to 256 MiB on a discrete GPU, an atomic element) gets a device-local shadow from
stagingBuffer(GuestBufferMemory.cpp). Each shadow is its ownVkDeviceMemory, it lives as long as the cached build that holds it, and its allocation never looks at the device-local budget:BufferMemoryTypeonly checks the budget for host-coherent buffers, so a device-local shadow is allocated until the driver refuses it. The other big device-local consumers adapt to the budget (the storage image cache keepsbudget / 8free in its reserve, the sampled texture cache has its own budget), so when a title fills the heap it is the shadows that keep growing into that last eighth and past the budget.Context::DeviceLocalHeadroom(type, bytes)(new): whether an allocation ofbytesfrom memory typetypestill ends at or below seven eighths of the budgetVK_EXT_memory_budgetreports for that type's heap. A type on a heap withoutVK_MEMORY_HEAP_DEVICE_LOCAL_BIT, an unknown type, a device without the extension and a heap without a reported budget all answer yes, so those cases behave as before.Buffer::WithinDeviceLocalHeadroom(context, bytes, usage)(new) makes a device-localBufferthrough the normal constructor path. A buffer the pool already retains (BufferPool::Take) is reused as before, without the check, since it adds nothing to the heap. Only on a pool miss, afterBufferMemoryType(requirements.memoryTypeBits, ...)has picked the memory type, is the new allocation checked against the heap of that type; without headroom theVkBufferjust created is destroyed and the function returnsnullptr.stagingBuffermakes its shadows through it. A refused shadow is treated exactly like a shadow the device cannot provide:nullptr, counted instagingRefused(the[buffers] stagingline ofAPS5_PROFILE_DRAW), and the region takes the path it takes without staging (Region::unstaged: in place when aligned, otherwise the host copy). The first four refusals print one[buffers] staging shadow of N bytes refused: ...line each.docs/dev/debt/libSceAgcDriver.mdgets a "Staging shadow headroom" entry: the eighth comes from one title on one GPU, budget readings are estimates, and the check reserves nothing against concurrent allocations.Why (PRAGMATA, PPSA02530)
On an RX 9070 XT (16 GiB, RADV, Linux), the title is stopped by device memory every time the player walks from the "Basic Movement" tutorial save into the forklift room: the device-local heap reaches 15.3 to 16.1 GiB, and free GTT drops next (1.3 GiB of VRAM and 3.0 GiB of GTT free at the stop; our local guard stops the process there, before the desktop compositor starts failing command submission, which an earlier run on this machine caused). With
APS5_TRACE_MEMORY_BUDGET=1(#10194) plus a local diagnostic that names genericBufferallocations by their caller, the device-local heap at the stop was:stagingBuffer<GuestBufferMemory::UploadPrepare)Between the lobby and the forklift room the shadows grew by 1.3 GiB active (3132 -> 4415 MiB) while the storage cache stayed at its budget, and the heap went from 13.3 to 15.3 GiB.
Same build (main
8f55414024+ the PRs PRAGMATA needs to reach gameplay + #14367 + #10194, plus that diagnostic), same save, same scripted route (Continue, then the left stick held forward), one run each, with the first version of this change (a37af8b910):APS5_NO_WRITTEN_SHADOW=1The storage image cache takes part of the room the shadows leave (5.6 -> 6.4-6.8 GiB), which is its budget doing its job.
Performance
Captured as the Performance report form describes: PRAGMATA (PPSA02530), the "Basic Movement" save, Continue, then the left stick held forward by script; window
APS5_TRACE_SECONDS=410:20, which falls in the Delphi lobby on the way to door A2 (screenshots match between the arms), before the point where the base is stopped on entering the forklift room. No otherAPS5_TRACE_*orAPS5_PROFILE_*switch (the summary lines show onlyAPS5_TRACE_SECONDSand the input file of our runner, which also captures the log to a file); warm shader and pipeline caches (the same cache copied into both run directories); RX 9070 XT, 3840x2160, same session. PRAGMATA does not reach gameplay onmainalone, so the base is our test stack:main8f55414024+ the PRs PRAGMATA needs to reach gameplay + #14367 + #10194 (commita9b16f94d161in the lines), and the change is that stack +a37af8b910andb491e43a20cherry-picked (e5b4c1a76648). Two pairs, in the order base, change, change, base:a9b16f94d161e5b4c1a76648e5b4c1a76648a9b16f94d161The run-to-run spread is larger than any difference between the arms: capture 2 ran no refused shadow inside its window, so it ran the same code as the base there, and is still the slowest; capture 3, with the gate refusing shadows, is within the two base captures. In every capture the largest phase after
Driver.PacketisDriver.Packet.memory_wait.Trace files: 1, base (
aps5-trace-a9b16f94d161-2684461), 2, change (aps5-trace-e5b4c1a76648-2745028), 3, change (aps5-trace-e5b4c1a76648-2978920), 4, base (aps5-trace-a9b16f94d161-3039591).python3 tools/trace_digest.py <trace>:Full digest, capture 3 (change)
seconds 410:20 from present 2292: 13 presents, frame p50 1612.9 / p90 1918.2 / p99 1923.4 / max 1923.4 ms, GPU busy 270.9 ms per present
AMD Radeon RX 9070 XT (RADV GFX1201), radv Mesa 26.2.6-arch1.1, Linux 7.2.11-arch1-1, 3840x2160, commit e5b4c1a76648, dropped: no
Slowest frames (largest slices inside each, inclusive)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): Driver.Packet 5942.8, Driver.Packet.memory_wait 3613.6, Driver.Packet.dispatch 1377.6, Driver.Dispatch 1234.5, Resources.Complete 905.4, Driver.Packet.draw 846.7, Driver.Draw 846.3, Graphics.Draw 732.2, Graphics.Draw.draw_and_resource_release 720.6, Vulkan.Dispatch 658.9
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
Full digest, capture 4 (base)
seconds 410:20 from present 2270: 12 presents, frame p50 1760.8 / p90 2023.4 / p99 2109.7 / max 2109.7 ms, GPU busy 296.6 ms per present
AMD Radeon RX 9070 XT (RADV GFX1201), radv Mesa 26.2.6-arch1.1, Linux 7.2.11-arch1-1, 3840x2160, commit a9b16f94d161, dropped: no
Slowest frames (largest slices inside each, inclusive)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): Driver.Packet 6354.3, Driver.Packet.memory_wait 3929.9, Driver.Packet.dispatch 1381.8, Driver.Dispatch 1276.4, Resources.Complete 997.3, Driver.Packet.draw 924.0, Driver.Draw 923.6, Graphics.Draw 787.1, Graphics.Draw.draw_and_resource_release 776.0, Vulkan.Dispatch 695.2
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
Frame rate through the forklift room, same builds and route,
APS5_TRACE_SUMMARY_EVERY=10(separate runs, not the captures above), presents per 10 s counted from the end of the loading screen:The change's process VRAM peaked at 13561 MiB over its 900 s run.
Tested
On
mainf2d874c1fdand this head (b491e43a20), same machine (RX 9070 XT, RADV Mesa 26.2.6, Linux),ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation:ctest -V -R 'agc_device_memory_fallback|agc_adaptive_staging|agc_buffer_pool|staging': 7/7 pass on both (agc_adaptive_staging,agc_buffer_pooland its two small-device modes,agc_device_memory_fallback,agc_bda_staging,agc_read_written_staging), noVUID-line and nowas not found but was requestedline in either log.agc_device_memory_fallback(mock budget):HeadroomTestschecks room below seven eighths, an allocation ending exactly at seven eighths, one crossing it, usage already past it, no reported budget, a host memory type, an unknown type, noVK_EXT_memory_budgetand no device-local heap.SecondHeapHeadroomTestsadds a second device-local heap and type and picks the type from the mask asBufferdoes: with a mask that excludes the first device-local type, pressure on the first heap leaves room and pressure on the second refuses; with a mask that allows both, only the first heap counts.agc_buffer_pool(mock device),StagingHeadroom: a 24 MiB shadow made with room and released to the pool is reused at 1900 of 2048 MiB (past seven eighths) with no new allocation; a 40 MiB shadow at the same usage is refused with no allocation and noVkBufferleft alive; withmemoryTypeBitsexcluding the first device-local type, a full first heap does not refuse a shadow that goes to the second heap, and a full second heap does.agc_device_memory_fallbackfails the two mask-excludes-first cases (and the host-type and no-device-local-heap cases) andagc_buffer_poolfails both second-heap cases; with the check moved back before the pool lookup,agc_buffer_poolfailsa retained staging buffer was refused within the last eighth of the budget, although reusing it allocates nothing.python tools/check_conventions.py --base origin/mainpasses.Overlap: no open PR changes
stagingBufferor the shadow size limits (checked the diffs of the 11 open PRs that touchGuestBufferMemory.cpp). #4472 drops the buffer pool's retained device memory over the budget; it is complementary (the 1.3 GiB of retained shadow memory above is that pool's) and does not act here, because the heap stays under the reported budget until the very end.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-01T18:24:36Z.
Review: CHANGES_REQUESTED
Reviewed — changes requested
The headroom gate can refuse reusable staging buffers and does not necessarily inspect the heap selected for a new buffer.
What I ran
Reviewed head
a37af8b9107e47a6d3236942a0a98a581c1c567aagainst basef2d874c1fdccf781c0fa5d86619bb5dd8894b958; the stack preview was clean. Environment: Linux x86_64, Python 3.11.2, GCC 12.2.0. I did not build or compile.Results
Precheck output and full table
The precheck deliberately does not configure, build, or compile AnyPS5.git diff --check f2d874c1fdccf781c0fa5d86619bb5dd8894b958...a37af8b9107e47a6d3236942a0a98a581c1c567a/usr/bin/python3 tools/check_conventions.py --base f2d874c1fdccf781c0fa5d86619bb5dd8894b958 --head a37af8b9107e47a6d3236942a0a98a581c1c567a --title perf(agc): refuse a staging shadow that would take the last eighth of the device-local budget --body /tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-14815-a37af8b9107e/pr-body.md/usr/bin/python3 tools/pr_abi_audit.py --base f2d874c1fdccf781c0fa5d86619bb5dd8894b958 --head a37af8b9107e47a6d3236942a0a98a581c1c567a --jsonNo changed Python implementation files.No changed tools/ module has an existing sibling tools/tests/test_<module>.py suite.No changed test_*.py files./usr/bin/python3 /marc/AnyPS5/tools/pr_stack_check.py 14815 --repo boykopovar/AnyPS5 --output-dir /tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-14815-a37af8b9107e/stackNot run: C++ tests need exact-head binaries, which require a build; the precheck does not build./usr/bin/python3 /tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-14815-a37af8b9107e/head-a37af8b9107e-25a7f1e3/tools/pr_test_quality.py --root /tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-14815-a37af8b9107e/head-a37af8b9107e-25a7f1e3 --json core/libs/prx/libSceAgcDriver/Graphics/include/Context.hpp core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp core/libs/prx/libSceAgcDriver/tests/DeviceMemoryFallback.cppNo changed Python test module in tools/ can be run as a no-build base-source control.The precheck's ABI and test-quality helper launches failed because those local scripts are not part of the isolated PR checkout. I ran both helpers from the workspace; the ABI check was not applicable, and manual inspection confirmed the test-quality warning is a false positive because
agc_device_memory_fallback_tests.cmakeregisters this source.Unedited helper output:
Unedited label dry-run output:
Unedited relevant command output:
git diff --checkproduced no output.Conventions:
Stack check:
Negative control
Not run. The C++ tests require a built binary; no build was performed.
Findings
Blocking
stagingBufferchecksDeviceLocalHeadroomand returnsnullptrbefore constructing aBuffer. TheBufferconstructor first asksBufferPool::Takefor a matching cached allocation. At the 7/8 threshold, the new early return therefore skips a possible cache hit even though reusing that allocation would add no heap usage; the retained allocation remains in the pool while this region falls back to the unstaged path. Please preserve cached-buffer reuse and apply the headroom refusal only when a fresh device allocation is needed, with a test for a reusable entry at the threshold.DeviceLocalHeadroomuses the first device-local memory type, butBufferchooses a type after Vulkan returns this buffer'smemoryTypeBits. If the selected type belongs to another heap, the check reads the wrong heap's budget and can either refuse useful staging or permit allocation past the selected heap's threshold. Please base the check on the actual compatible type/heap and cover differing memory-type masks in the budget test.perf(...)rule. The description has no two valid, distinct-commitaps5-summary.jsonllines, raw traces ortrace_digest.pyoutput; the label dry run also reportsperf numbers missing. Please add the matched baseline and candidate captures required by the performance report form.Non-blocking
Sources
BufferMemoryTypeselects from the buffer'smemoryTypeBits;DeviceLocalHeadroomchecks the first device-local type.perftitles; the performance form asks for the traces and digest.Not verified
To get this to a good state
Preserve pool reuse, check the actual selected heap, and provide the matched performance artifacts. I will rerun the same no-build checks on the updated head.
Imported from boykopovar/AnyPS5. Originally posted by BunnyFl on 2026-11-03T03:26:58Z.
Review: APPROVED
Thanks @hugodearaujo, that settles my question about the captures.
I fetched
pr14815-perf-captures.a9b16f94d1is exactly two commits belowe5b4c1a766, and between them the four production files (GuestBufferMemory.cpp,Resources.cpp,Resources.hpp,Context.hpp) change in exactly the same lines as this PR's own diff atb491e43a20(0 differing+/-lines). So the change captures ran this head's rework, including the check inBuffer's constructor.That and my Windows re-check of
b491e43a20cover what I raised. On Windows 11 with an RX 9070 XT, the 7agc_buffer_pool*/agc_device_memory_fallback/agc_memory_accounting*tests pass merged ontomain. With the check disabled,agc_buffer_poolfails on the threshold, chosen-heap andVkBuffercases. The PR head is stillb491e43a20today.I didn't run the validation layer or PRAGMATA, and I didn't re-derive the
trace_digest.pynumbers. @MarcZX10's change request froma37af8b910is still open on his side.