perf(agc): spin before blocking on the GPU mutex and the draw committer hand-offs #6632
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/6632"
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 AlxcBTW on 2026-10-15T23:55:45Z.
What
Companion: #6631 (independent of this one; the two together give more than the sum, see Tested).
At the Minecraft Dungeons II (PPSA16064) title screen (3840x2160, about 8 FPS) the GPU idles for 44 ms of every 126 ms frame. A trace of the plateau shows that for 32 % of the window the GPU waited for a batch that had not been submitted yet, and that the three threads which submit it are not out of CPU: the queue 0 worker is 34 % busy, the draw committer 22 %, the compute worker 21 %, and every other thread under 2 %. They hand work to each other 49,000 times per 10 s (GPU mutex, the committer's condition variable,
DrawPipeline::Drain), and each hand-off that has to wait costs a kernel wake-up (its latency was not timed on its own; the effect is what the rows below measure). The queue 0 worker spends 39 % of its time waiting for the committer inDrain, the committer is idle for half of the window, and the GPU mutex is held 79 % of the time.GpuMutexType::lockretriestry_lockfor a window before it blocks; the committer polls its item count for the window before it waits on its condition variable;DrawPipeline::Drainpolls the item count for twice the window before it waits for the committer. A hand-off that completes within the window costs no wake-up.HandoffSpin.hppholds the policy (HandoffSpinWindowFor) and the two helpers (SpinUntil,LockSpinning).CpuTopology;hardware_concurrency()where the mask is unknown) and 0 otherwise, where spinning threads would compete with the thread they wait for, which may be a sibling hyperthread of the spinner.APS5_HANDOFF_SPIN_US=<n>sets it (0 turns it off, values above 10000 are limited to 10000).docs/dev/ARCHITECTURE.md: one paragraph.Tested
Windows 11, MinGW-w64 GCC 15.2, NVIDIA GeForce RTX 5070 Laptop GPU (driver 617.42), 20 logical processors (Ryzen AI 9 365) for the title rows; ubuntu-latest (4 logical processors, lavapipe) for the tests. The title rows ran on this change on top of
44df21632; the branch is squashed onto5d9dcc42d(the 59 commits in between merge without a conflict and the changed lines are the same).agc_handoff_spin(new, no GPU): the window policy for usable processor counts (9 spin, 8 and 4 do not) and the processor count itself, andAPS5_HANDOFF_SPIN_USvalues (unset, 0, number, negative, text, above the limit),SpinUntil(ready at once, zero window, ready on the fifth evaluation, never ready for a window, ready from another thread), four threads contending onstd::mutexandstd::recursive_mutexwith windows of 0, 50 us, 1 ms and 150 us (never two holders, no lost increment), the fallback to the blocking lock when the holder outlasts the window, recursion. Fork CI (Linux, lavapipe, ubuntu-latest; this squashed head onorigin/main5d9dcc42d): https://github.com/AlxcBTW/AnyPS5/actions/runs/38817879745,agc_handoff_spinpasses in 0.05 s. After the guard change (head23a804e39, onorigin/maineb901bcf4): https://github.com/AlxcBTW/AnyPS5/actions/runs/38943017617,agc_handoff_spinpasses in 0.04 s and the driver and recorder tests pass withAPS5_HANDOFF_SPIN_US=150; the one failure isagc_driver_graphics, which also fails without this change.APS5_HANDOFF_SPIN_US=150; the 455 tests of^(agc_handoff_spin|agc_driver_|agc_recorder)pass, including the pipelined draw tests that drain the committer (same run).libsandrelinkercompile on the fork CI (MinGW-w64 GCC): https://github.com/AlxcBTW/AnyPS5/actions/runs/38817908816 (this squashed head).tools/check_conventions.py --base origin/mainpasses.APS5_TEXTURE_CACHE_MIB=3072,APS5_PROFILE_DRAW=1,APS5_PROFILE_GPU=1,APS5_NO_PAD_INPUT=1; themainarm is the binary with both switches off, which an earlier session measured against a build of origin/main: 7.80 against 7.93 FPS):main:APS5_NO_DCC_PRESYNC=1 APS5_HANDOFF_SPIN_US=0APS5_HANDOFF_SPIN_US=0APS5_NO_DCC_PRESYNC=1Rows run in the order both, companion, this PR, main, main, this PR, companion, both. The warm-up row and the first
bothrow, the first two after the relink, found no title windows (probably cold caches), sobothhas one row of eight windows. Rows of identical code differ by up to 0.8 FPS (the twomainrows read 7.08 and 7.62, the two rows of this PR alone 7.16 and 7.97), about 10 %, so a single row cannot resolve an effect of that size; the pooled arms and the repetition in other sessions can. The same scene on the same machine in two earlier sessions: origin/main against the first commit of the companion with this PR, alternating rows, 7.93 against 8.76 FPS (+10.5 %); an earlier lab build, levers off against both levers on, 7.94 against 8.76 (+10.2 %), with the companion's lever alone at +2.6 % and this PR's at +2.9 %. The baseline therefore lands between 7.35 and 7.94 FPS from session to session, and the changed build between 8.55 and 8.76 FPS: +10 % to +16 %, +12 % on average of the three sessions. Alone, this PR gives +2.9 % and +3.0 %, the companion +2.6 % and +4.0 %; together they give more than the sum, and the GPU busy time stays at about 80 ms while its idle time per frame falls from 54.7 to 36.3 ms.Spin window, measured with the DCC pre-sync of the companion PR on (two rows of eight windows each, anchor without both 7.88 FPS): 50 us 8.28 FPS, 150 us 8.62, 400 us 8.59; the compute worker's label-wait spin (1500 us) and a mutex priority for the queue 0 worker and the committer added nothing on top (8.53 and 8.56) and are not in this PR.
APS5_TRACE_SECONDS=60:10is set besidesAPS5_TEXTURE_CACHE_MIB=3072,APS5_NO_PAD_INPUT=1and the trace directory; same machine, scene and session, one capture per build, no event dropped): origin/main, 81 presents in 10.0 s = 8.09 FPS, frame time p50 120.7 / p90 138.8 / p99 152.7 ms, GPU busy 79.2 ms; both PRs, 80 presents in 10.0 s = 7.97 FPS, p50 120.1 / p90 153.0 / p99 176.4 ms, GPU busy 78.5 ms. These two captures do not show the difference: a capture is 80 frames of one row, and rows of identical code differ by more than the effect (see above). The captures of the earlier session (20 s,APS5_TRACE_SECONDS=60:20, the queue 0 worker's event buffer full after 12 s, so they are flaggedtrace_dropped) read 8.27 FPS (p99 151.9 ms) for origin/main and 8.99 FPS (p99 130.6 ms) for the first commit of the companion with this PR (+8.7 %). The summary lines of the four captures follow as a comment; the traces are kept and available on request.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 AlxcBTW on 2026-10-15T23:56:21Z.
The performance report captures for this PR and #6631 are in https://github.com/boykopovar/AnyPS5/pull/6631#issuecomment-6204773507 (one comment for both PRs, since the captures ran with both levers together and with neither).
AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-16T00:59:46Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)docs/dev/ARCHITECTURE.mdL100-101core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawPipeline.cppL3docs/dev/ARCHITECTURE.mdL100-101Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
core/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtdocs/dev/ARCHITECTURE.mdcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtdocs/dev/ARCHITECTURE.mdcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawPipeline.cppcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppdocs/dev/ARCHITECTURE.mdcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtdocs/dev/ARCHITECTURE.mdcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppdocs/dev/ARCHITECTURE.mdcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawPipeline.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtImported from boykopovar/AnyPS5. Originally posted by AlxcBTW on 2026-10-16T04:58:02Z.
Second-machine numbers for this PR and #6631 are on #6631. They were measured by @parl0v on an i5-14400F with an RTX 4060 8 GB, at the Minecraft Dungeons II title over three alternating rounds. This PR alone gives +3.2 % against
main(+4.3 / +4.1 / +1.0 % per round), and both PRs together give +10.3 %. The gain is in the idle gaps, and there were no fatal lines or wait timeouts. Link: https://github.com/boykopovar/AnyPS5/pull/6631Imported from boykopovar/AnyPS5. Originally posted by AlxcBTW on 2026-10-16T12:52:43Z.
Rebased onto
maineb901bcf4, which has #1747, #4943, #4649, #5875, #5872 and #6784. Head is nowe74484e0f. The only conflict was the queue-0 paragraph ofdocs/dev/ARCHITECTURE.md. I kept upstream's text there, including #1747's sentence on pipelined dispatches, and put this PR's paragraph next to it. The code merged without conflicts and is unchanged.Fork CI, Linux with lavapipe, every
agc_test: 626 of 627 pass (run 38899806599). The one failure isagc_driver_graphics, which fails onmain+ #6784 without this PR as well (run 38898087865). The game numbers on this PR and on #6631 were measured on the earlier base and were not re-run on this one.AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by Raghav314 on 2026-10-16T20:04:16Z.
Data point from #5867 (now closed in favour of this), which polled only in the committer's wait, for the core-count policy here. Hades II (PPSA36082), level UI/menu, Linux, 7800X3D (8 cores / 16 threads), RTX 4090, pipelined draws at the default depth. One build that switched a 50 us poll off and on every 15 s in one sitting, 4 windows each, MangoHud fps per window:
taskset -c 0-3: mean fpstaskset -c 0-3: p99 frame timeWith cores to spare the poll won every pair; on 4 cores it gave nothing and lengthened the tail, which is what a window of 0 at 8 or fewer logical processors avoids.
LLM assistance used.
Imported from boykopovar/AnyPS5. Originally posted by teepean on 2026-10-18T21:43:47Z.
Data point from a CPU with two L3 domains, where the spin costs Stray (PPSA02100) about 10 %. The committer's poll accounts for about half of that.
Setup. Linux (CachyOS, kernel 7.2.10), Ryzen 9 9950X (16 cores in two CCDs, each with its own L3: CPUs 0-7/16-23 and 8-15/24-31), RTX 3090 (NVIDIA 615.78.08), GCC 16.2.1 Release, 3840x2160, the scx_lavd scheduler (sched_ext, lavd 1.1.3 with
--autopilot), stockengine.ini. Stray stands at the same save. The numbers come fromAPS5_TRACE_SECONDS=185:20summaries (the runner redirects the log to a file), one run at a time, with the arms alternating. The process may run on all 32 logical processors, so the window is 150 us. Stray is CPU-bound here: GPU busy per present is 89 to 99 ms in every arm below.On and off, frame p50 and presents in the 20 s window:
mainf1af6c591APS5_HANDOFF_SPIN_US=0mainb51c65dd1(before #6631 and this PR)One more
mainf1af6c591run hit the #5372 import race (about 1 fps) and is left out.Per site. These runs use a local build of
f1af6c591that reads one window per site from the environment. That build is not proposed. Three alternating rounds:Drainpoll offWhere the cost comes from. I sampled per-thread CPU time and the CPU each thread last ran on from
/proc, once a second at 140-170 s of every run.Drainin 42 %. With all off, they were there in 6 % and 16 %, and with only the committer poll off in 11 % and 20 %.Driver.Draw124 against 106 ms,shader_compile_and_link76 against 62 ms.My guess is that a hand-off partner that blocks less often looks less latency-critical to lavd, which then moves it off the best-ranked cores, and those are all on CCD0 here. I have not checked that in lavd's code.
On one CCD the spin helps. I ran the same build with the whole process on CCD0 (
taskset -c 0-7,16-23). That is 16 logical processors, so the window stays 150 us. Three alternating rounds; one run of each CCD0 arm hit the import race and is left out:Draincaller change CPU 26-196 times a second. Unpinned with the spin, they change CPU 3,300-6,900 times.So the spin itself is fine here. The cost comes from the scheduler spreading the hand-off partners over both L3 domains, and keeping the process in one domain would remove it without turning the spin off.
EEVDF does this by itself. With sched_ext off, the kernel's scheduler keeps every busy thread on CCD0 (0 of 90-97 samples on CCD1). On
main1c523bdb4, which has the spin, Stray runs at 149.5 ms p50 and 132.7 presents (three runs). That is where CCD0 with the spin is under scx_lavd. I didn't repeat the per-site on/off under EEVDF.Not checked: Windows on this machine, other titles.
AI-assisted: yes (analysis and text with Claude Code; the runs are from my machine).