perf(shader): bound resident resource plans by a byte budget #14367
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/14367"
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-01T10:37:51Z.
What
Every shader the title draws keeps its resource plan (
IrResourcePlan) in itsSourceEntry, and the source table ingetSourceis never trimmed. #6178 drops plans only for registered shaders that were never captured, so the plan of every shader that was drawn once stays resident for the rest of the run.In PRAGMATA (PPSA02530) with a warm shader cache, process-private memory goes from ~5 GiB at 10 s to ~20 GiB by 130 s. A diagnostic build counted the cause: the value arrays of the resident plans alone are 9 GiB of 19.7 GiB of
mallocin use (6961 plans). Free memory inside the malloc arenas stays at 0.3–0.5 GiB, so this is live data, not fragmentation.This change adds an opt-in byte budget for resident plans. It is off unless
APS5_RESOURCE_PLAN_MBsets one, so by default every plan stays resident as onmain:SourceEntrykeeps the disk cache key its plan was loaded or stored under, and the shader hash it was loaded with. A dropped plan reloads straight from that entry, inacquirePlanunder the source's mutex. The reload does not look the source up again by the current request, so it reads the same file.getSourcekeys a source onRecompileCacheKey::Buildplus the host subgroup size and matches the code word for word, andBuildPlanKeyadds only the format constants and the switch key to those fields.resourcePlanBytes). The count covers theIrValuearray, the operand and use lists of each value, the blocks, the plan's vectors and its compact form.use_count()equals the group size). A group is ordered by its most recent use.try_lockand runs only from the public entry points, after the plan was acquired and no recompiler lock is held:GetResourcePlan, bothCaptureResources,PreparedShaderInvocation::CaptureandPrepareShader.APS5_RESOURCE_PLAN_MBsets it in MiB, and0(the default) keeps every plan, as onmain. A default can be picked once paired runs in several titles show how often evicted plans reload and what that costs in frame time.releaseUndrawnPlandrops reloads from that key inacquirePlan, instead of going throughgetSourceagain. Both read the same disk entry.docs/dev/ENVIRONMENT.mdand listed in the shader disk cache's neutral switches, because it only decides how long a plan stays in memory, never what a compile produces.47e2440ed3, the budget was on by default, sized from physical memory: an eighth of it, between 1 and 8 GiB. That default was removed in review. The runs below used it: 3899 MiB on this host, which is nowAPS5_RESOURCE_PLAN_MB=3899.APS5_PROFILE_DRAW, the result memo report adds a line[recompile] resource plans (10 s): N reloaded, M evicted; K plans held, X MiB of a Y MiB budget. The interval comes fromTakeResourcePlanInterval, which reads the counters and updates the reported counts under one mutex, so overlapping reporters cannot count an interval twice or underflow.releaseUndrawnPlannow goes through the same accounting, and the dropped plan is destroyed outside the source's mutex.Not covered: a plan shared by several sources is counted once per source that holds it, so while it is shared the held bytes are higher than the memory it uses. In PRAGMATA only 8–20 resident plans had another owner at any time.
Tested
Arch Linux (kernel 7.2.11), Ryzen 7 9800X3D, 31 GiB RAM with zram swap, RX 9070 XT (RADV, Mesa 26.2.6), GCC 16.2.1, Release. Head
3a86c44a46. Merge basemain26fa1212b8. The head still merges cleanly withmain926f7dd492.Since
47e2440ed3(the budget off by default, from review), merged locally withmain118d39300d, kernel 7.2.14. The latermain1d7582cea6changes only.github/workflows/build.yml.^shader|^agc_shadertests pass withVK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation,ANYPS5_NO_SHADER_CACHE=1andANYPS5_REQUIRE_VULKAN=1,agc_shader_disk_cache(all four budget checks above) included. The loader inserted the layer, and the log has noVUID-line and nowas not found but was requested.tools/check_conventions.py --base 1d7582cea6andgit diff --checkpass.SetResourcePlanBudget, so they don't depend on the default.Check
verifyPlanBudgetinagc_shader_disk_cache:PreparedShaderInvocationwith a relocated push layout, reloads from its own disk cache entry with no miss and no write.Check
verifySharedPlanBudget:s_nop) reload one plan object from two disk entries.Check
verifyQueuedPlanWrite:planWrites).Flush()it is evicted and reloads as one disk hit with no miss and no write.Check
verifyPlanReport:SetResourcePlanReportHook) runs inside an admitted reporter after it has read the counters.Red/green:
a recently used plan was evicted.a budget of one byte did not evict the unowned plans or evicted the owned ones.use_count() == 1) fails withshared plan budget: a plan shared by two idle sources was not evicted from both.use_count() > owners + 1) fails 5 of 5 withqueued plan write: a plan whose disk write was still queued was evicted and missed on its next use. The earlier budget checks were disabled for that run, because they catch the same mutation first. Without the mutation the check passed 20 of 20 runs.TakeResourcePlanIntervalfails 5 of 5 withplan report: a reporter delayed past the next interval counted it twice or underflowed.Paired run of the touched set on
main26fa1212b8and on3a86c44a46:agc_shader_disk_cache*,agc_driver_prepared_shaders*,agc_shader_result_memo_budget*andagc_shader_memory.VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation,ANYPS5_NO_SHADER_CACHE=1andANYPS5_REQUIRE_VULKAN=1.Insert instance layer "VK_LAYER_KHRONOS_validation"51 times in each run, with nowas not found but was requestedline.agc_driver_prepared_shaders_debug_failureis aWILL_FAILtest) with noVUID-lines.python3 tools/check_conventions.py --base origin/mainandgit diff --checkpass.Performance (PRAGMATA, PPSA02530, 01.200.000)
These runs predate
47e2440ed3: the change arm ran with the budget on, at 3899 MiB (nowAPS5_RESOURCE_PLAN_MB=3899).Route. This is the route of the memory runs below: boot to the title, no input before the title shows (Cross only after it, by a screenshot match). The capture is
APS5_TRACE_SECONDS=40:70, 40 to 110 s after the first present, with no other trace or profile switch. In the profiled run of the change, that window holds three eviction bursts: 1512, 2908 and 603 evictions, and 258 reloads.Builds. Both arms are the title's integration stack: #10935
895e0341d9, #1030431f895cb59, #10497218dce3ad3, #12898d2d43310ad, #444988b99b28b6, #9999d72cc8ca6aand #11580fedf3e91a7, each merged with--no-ff. It is the same stack as the memory runs below, without theirmallinfo2()print: no diagnostics in either build.0510c350a6=main26fa1212b8+ the stack.7d85b40418= this head3a86c44a46+ the same stack.Caches. The change's shader cache source version was pinned to the base's (
38d10bea705877c0), so both read the same cache. This change does not alter compiled output.shader_cache/driver) and the save was restored. Then the arms ran alternated: base, change, base, change.dirtyisnullin the lines:git statusfails in that build tree, whose submodules are symlinks. The tracked sources had no changes (git status --ignore-submoduleswas empty)./proc/<pid>/statusevery 2 s; the sampling stops the title at 200 s. "Title shown" is the first 8 s screenshot that matches the title, so it is coarse.tools/performance_compare.py, pair 1 and pair 2).process private memory reached the registered shader preparation budgetat ~109 s (#9626). The change runs do not, which is why they have ~3000 more cache hits.APS5_PROFILE_DRAW=1, not traced) peak at 18470 MiB (base) and 10932 MiB (change). The held plan bytes stayed under the 3899 MiB budget (largest: 3802.0 MiB).Summary lines (base 1, change 1, base 2, change 2):
Traces: base 1 pr14367-pragmata-base-1.json.gz, change 1 pr14367-pragmata-change-1.json.gz, base 2 pr14367-pragmata-base-2.json.gz, change 2 pr14367-pragmata-change-2.json.gz.
python3 tools/trace_digest.py aps5-trace-0510c350a6f5-2831162.json.gz(base 1)seconds 40:70 from present 674: 825 presents, frame p50 66.8 / p90 136.3 / p99 353.3 / max 730.7 ms, GPU busy 30.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 0510c350a6f5, dropped: no
Slowest frames (largest slices inside each, inclusive)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): Driver.WorkerWait 203.5, Driver.Packet 133.6, Driver.WorkerWait.idle_and_completion_poll 123.6, Driver.WorkerWait.dequeue_order 79.9, Driver.Packet.dispatch 74.4, Driver.Dispatch 60.3, Driver.Packet.memory_wait 48.1, Vulkan.Dispatch 47.7, Vulkan.Dispatch.shader_resources 47.5, batch 31.0
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
python3 tools/trace_digest.py aps5-trace-7d85b4041817-2855161.json.gz(change 1)seconds 40:70 from present 686: 839 presents, frame p50 66.4 / p90 134.1 / p99 273.9 / max 545.5 ms, GPU busy 30.5 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 7d85b4041817, dropped: no
Slowest frames (largest slices inside each, inclusive)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): Driver.WorkerWait 201.4, Driver.Packet 129.9, Driver.WorkerWait.idle_and_completion_poll 122.5, Driver.WorkerWait.dequeue_order 78.9, Driver.Packet.dispatch 73.1, Driver.Dispatch 59.4, Vulkan.Dispatch 45.5, Driver.Packet.memory_wait 45.5, Vulkan.Dispatch.shader_resources 45.4, batch 30.6
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
python3 tools/trace_digest.py aps5-trace-0510c350a6f5-2879961.json.gz(base 2)seconds 40:70 from present 676: 843 presents, frame p50 66.5 / p90 133.1 / p99 241.5 / max 494.5 ms, GPU busy 31.0 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 0510c350a6f5, dropped: no
Slowest frames (largest slices inside each, inclusive)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): Driver.WorkerWait 201.0, Driver.Packet 128.9, Driver.WorkerWait.idle_and_completion_poll 122.3, Driver.WorkerWait.dequeue_order 78.7, Driver.Packet.dispatch 72.6, Driver.Dispatch 58.9, Vulkan.Dispatch 45.9, Vulkan.Dispatch.shader_resources 45.8, Driver.Packet.memory_wait 44.8, batch 31.0
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
python3 tools/trace_digest.py aps5-trace-7d85b4041817-2936462.json.gz(change 2)seconds 40:70 from present 691: 854 presents, frame p50 66.9 / p90 130.4 / p99 260.8 / max 491.2 ms, GPU busy 30.5 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 7d85b4041817, dropped: no
Slowest frames (largest slices inside each, inclusive)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): Driver.WorkerWait 199.5, Driver.Packet 126.1, Driver.WorkerWait.idle_and_completion_poll 121.8, Driver.WorkerWait.dequeue_order 77.7, Driver.Packet.dispatch 71.1, Driver.Dispatch 57.4, Vulkan.Dispatch 45.1, Vulkan.Dispatch.shader_resources 45.0, Driver.Packet.memory_wait 44.3, batch 30.6
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
The earlier memory runs (
main8f55414024+ the same stack + amallinfo2()print every 10 s,mallocin use; the fix arm was86e0294bd2):mallocin use at 80 / 130 / 180 sAPS5_PROFILE_DRAW=1APS5_PROFILE_DRAW=1APS5_REGISTRATION_PREPARE_COMMIT_GB=0APS5_REGISTRATION_PREPARE_COMMIT_GB=0Where the 3012 misses come from (first run of this change).
process private memory reached the registered shader preparation budget. Later registrations are then left for their first use.From the profiled run of this change:
74 reloaded, 3327 evicted; 1070 plans held, 3056.0 MiB of a 3899.0 MiB budget.VmRSS still peaks at 23–25 GiB in both arms, because the shader disk cache maps 6–9 GiB of entries (
RssFile). Those are clean file pages and are not part of this change.Checklist
main; no other open PR implements the same functions.mdfiles or images added to the repositoryImported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-01T10:43:53Z.
Review: CHANGES_REQUESTED
Reviewed — changes requested
The trim can leave a shared resource plan unevictable even when no caller is using it, and the required matched performance artifacts are missing.
What I ran
Reviewed head
86e0294bd2217c3d0b5ac7874746ddee2f30141fagainst PR base8527dad32ed743d3225042579bfd22acbf2616aa; the live merge preview was clean against targetmainate6f57307fa7f47165d7fc1bee945abdb2f6816e7. Environment: Linux 6.1 x86-64, Python 3.11.2, GCC C++ driver 12.2.0 (not used for a build).Results
git diff --checkemitted no output.mainate6f57307fa7f47165d7fc1bee945abdb2f6816e7; no declared dependencies.tests/cmake/agc_shader_disk_cache_tests.cmake.aps5-summary.jsonllines, attached baseline/change traces, andtrace_digest.pyoutput required for thisperfchange.Unedited outputs
Conventions stdout from the precheck:
Test-quality heuristic JSON:
Manual CMake registration lookup:
Hosted check status:
Negative control
use_count() == 1guardSourceEntryowners can be trimmedFindings
Blocking
Shared plans can remain permanently above budget.
SharedPlanTable::Getreturns the sameshared_ptrto multipleSourceEntryobjects when cache payload and shader hash match. The newevictablePlanonly accepts a plan whenuse_count() == 1; consequently, if two persistent source entries share a plan and neither has an external caller, both are excluded from the trim candidates. Neither entry releases its reference, so the shared object stays resident and the advertised budget may remain exceeded. The PR description acknowledges shared plans but the current code never evicts their references as a group. Please make a shared plan evictable when all owners are eligible (or use equivalent ownership accounting), and add a regression test with two source entries sharing one disk plan that verifies trimming and subsequent reload.Required performance artifacts are absent. The repository’s performance instructions and PR template require matched
main/change summary lines and traces for aperfPR; the performance report form also requests the trace digest. Please add both raw summary lines, attach both trace files, and includepython3 tools/trace_digest.pyoutput so the reported memory and timing comparison can be checked against the captures.Non-blocking
No other blocking source issue was established in this no-build review.
Sources
use_count()guard at this headNot verified
I did not compile or run CTest, independently reproduce the reported game captures, verify the author's red/green runs, or test on Windows, macOS, or a console. The author-reported test run did not specify its exact head SHA. Hosted checks were still pending.
To get this to a good state
Please address the shared-owner eviction case with a regression test and provide the matched performance artifacts. I will rerun the same no-build checks against the updated head.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-14T05:08:55Z.
Review: APPROVED
Reviewed — looks good
Nothing blocks this change; review findings and tests support merging, while title performance runs remain author-reported.
What I ran
Reviewed head
e8b34de5969fdaaff19d318e04b585d96ae8451f. I read the prepared description, commits, manifest and diff, and all five changed files. I did not run commands or tests; the test and performance results below are supplied by the author.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
I did not execute the tests, convention checks, or paired title performance runs. The reported runs and measurements are author supplied.
To get this to a good state
Nothing is required.
Files read:
core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp,core/shader/recompiler/Recompiler.cpp,core/shader/recompiler/Recompiler.hpp,core/shader/recompiler/ShaderDiskCache.cpp,docs/dev/ENVIRONMENT.md. Files reviewed: the same five changed files; none excluded.