perf(agc): resume an idle store that yielded to a GPU mutex waiter #26450

Merged
oneandonlydean merged 3 commits from upstream-pr/26450 into main 2026-11-27 14:53:19 +00:00
oneandonlydean commented 2026-11-23 23:03:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-11-23T23:03:51Z.


What

An idle store that yields to a thread blocked on the GPU mutex is now resumed while queue 0 stays idle, instead of leaving its remaining results pending.

Since #20145 StoreIdleResults stops after a piece as soon as another thread is blocked on GuestMemory::GpuMutex. The queue worker did not come back for the rest: after the yield, queue 0 went dormant with results still pending, and nothing stored them until the idle epoch moved and another 50 ms quiet period passed. One short lock by a thread that queues nothing (the presenter, the draw committer, a title thread) therefore cancelled the store for that idle stretch, and the next submissions met pending images. That matches what #25535 measures on PPSA20423: about 150000 pending-image fast-fails per run with the yield against about 200 without it, while GpuMutex.wait stays at about 10 ms per 10 s in both arms.

VulkanDevice::StoreIdleResults now returns whether it finished. When it stopped early and queue 0 is still idle, the worker stands back for the blocked thread (at most 2 ms, ended at once by new work) and then takes the next piece without a new quiet period. What #20145 gave is kept: a blocked thread waits for one piece at most, and the first piece of each pass is still always stored, so a mutex that is always contended cannot starve the store. If work came and went during the store, the quiet period applies again as before.

Related: #25535. I do not have PPSA20423 or PPSA24423 (the title #20145 was measured on), so this PR does not claim the beach numbers; see Tested.

Tested

New test agc_driver_idle_store_resumes (tests/execution/IdleStoreResumes.cpp): eight pending storage images and a thread that keeps taking the GPU mutex without queueing anything; the results must be stored while queue 0 stays idle and nothing is submitted. On main 47d328f1c4 it fails ("an idle store that yielded to a thread blocked on the GPU mutex left results pending while queue 0 stayed idle"); with this change it passed 3 of 3 runs. agc_driver_idle_store_yields, agc_driver_idle_store_over_unmap, agc_driver_queue_zero_quiet_interrupt, agc_driver_storage_idle_store_completes, agc_driver_store_idle_results, agc_driver_store_idle_results_wake and agc_driver_store_idle_results_disabled pass with it. Linux, RTX 4090.

No-harm check on a title I have: Linux, RTX 4090 (24 GB), i9-14900KF, PPSA21564 01.007.000, 3840x2160, -lvl hub_crashsite from a save, no input, no profile, 180 s per arm, one exclusive window, order warm-up (discarded), main, PR, PR, main. main is 47d328f1c4, the PR arm is this head. Means of the last two 10 s windows of each arm:

main PR PR main
frame p50 60.3 ms 60.8 ms 59.4 ms 60.0 ms
presents per 10 s 133 135 147 145
GPU busy per present 49.2 ms 48.4 ms 48.4 ms 49.0 ms

Neutral: main 60.1 ms, PR 60.1 ms on average, and the two arms of one build differ by 0.4 ms (main) and 1.5 ms (PR). It shows only that nothing got slower on this scene.

Not tested here: PPSA20423 (the #25535 beach) and PPSA24423. An A/B of this head against main on those two scenes is what would show the gain and that the waits #20145 removed stay gone.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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 RTX 4090, driver 610.57.04, Linux
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5462765122 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/26450). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-11-23T23:03:51Z. --- ### What An idle store that yields to a thread blocked on the GPU mutex is now resumed while queue 0 stays idle, instead of leaving its remaining results pending. Since #20145 `StoreIdleResults` stops after a piece as soon as another thread is blocked on `GuestMemory::GpuMutex`. The queue worker did not come back for the rest: after the yield, queue 0 went dormant with results still pending, and nothing stored them until the idle epoch moved and another 50 ms quiet period passed. One short lock by a thread that queues nothing (the presenter, the draw committer, a title thread) therefore cancelled the store for that idle stretch, and the next submissions met pending images. That matches what #25535 measures on PPSA20423: about 150000 pending-image fast-fails per run with the yield against about 200 without it, while `GpuMutex.wait` stays at about 10 ms per 10 s in both arms. `VulkanDevice::StoreIdleResults` now returns whether it finished. When it stopped early and queue 0 is still idle, the worker stands back for the blocked thread (at most 2 ms, ended at once by new work) and then takes the next piece without a new quiet period. What #20145 gave is kept: a blocked thread waits for one piece at most, and the first piece of each pass is still always stored, so a mutex that is always contended cannot starve the store. If work came and went during the store, the quiet period applies again as before. Related: #25535. I do not have PPSA20423 or PPSA24423 (the title #20145 was measured on), so this PR does not claim the beach numbers; see Tested. ### Tested New test `agc_driver_idle_store_resumes` (`tests/execution/IdleStoreResumes.cpp`): eight pending storage images and a thread that keeps taking the GPU mutex without queueing anything; the results must be stored while queue 0 stays idle and nothing is submitted. On `main` 47d328f1c4 it fails ("an idle store that yielded to a thread blocked on the GPU mutex left results pending while queue 0 stayed idle"); with this change it passed 3 of 3 runs. `agc_driver_idle_store_yields`, `agc_driver_idle_store_over_unmap`, `agc_driver_queue_zero_quiet_interrupt`, `agc_driver_storage_idle_store_completes`, `agc_driver_store_idle_results`, `agc_driver_store_idle_results_wake` and `agc_driver_store_idle_results_disabled` pass with it. Linux, RTX 4090. No-harm check on a title I have: Linux, RTX 4090 (24 GB), i9-14900KF, PPSA21564 01.007.000, 3840x2160, `-lvl hub_crashsite` from a save, no input, no profile, 180 s per arm, one exclusive window, order warm-up (discarded), `main`, PR, PR, `main`. `main` is 47d328f1c4, the PR arm is this head. Means of the last two 10 s windows of each arm: | | `main` | PR | PR | `main` | |---|---|---|---|---| | frame p50 | 60.3 ms | 60.8 ms | 59.4 ms | 60.0 ms | | presents per 10 s | 133 | 135 | 147 | 145 | | GPU busy per present | 49.2 ms | 48.4 ms | 48.4 ms | 49.0 ms | Neutral: `main` 60.1 ms, PR 60.1 ms on average, and the two arms of one build differ by 0.4 ms (`main`) and 1.5 ms (PR). It shows only that nothing got slower on this scene. Not tested here: PPSA20423 (the #25535 beach) and PPSA24423. An A/B of this head against `main` on those two scenes is what would show the gain and that the waits #20145 removed stay gone. ### 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) and the licence notice of code adapted from upstream (see AGENTS.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 RTX 4090, driver 610.57.04, Linux - [x] AI-assisted: yes
malekaifa commented 2026-11-23 23:13:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by malekaifa on 2026-11-23T23:13:43Z.

Review: CHANGES_REQUESTED


Blocking fairness gap on exact head abb9aa2e615c6222a835b30b85bbf65a64ecc8a1.

The resume idea addresses the measured pending-image problem, but the implementation does not yet guarantee the PR's stated property that a thread already blocked on GuestMemory::GpuMutex() waits for at most one stored piece. GpuMutexType::block() keeps Waiters() nonzero until the waiter actually acquires the underlying AgcDriver::Mutex; that mutex is barging (unlock() stores Unlocked and then notify_one(), while a new caller may win try_lock() before the notified waiter runs). In QueueWorker.cpp, after a yielded store we wait only until Waiters()==0 or 2 ms expires. If the counted waiter has not been scheduled/acquired within those 2 ms, queue 0 immediately loops, takes StoreIdleResults() again, and its YieldToGpuMutexWaiters deliberately stores the first 4 MiB piece before checking waiters. The same waiter can therefore sit through multiple pieces/passes; under scheduler load this can reintroduce the long lock hold #20145 was intended to cap.

The new IdleStoreResumes test only proves eventual completion while another thread repeatedly acquires/releases the mutex; it does not prove that the waiter which caused a yield gets the handoff before the next store piece.

Please make the resume condition observe an actual handoff (or otherwise avoid reacquiring while the causal waiter is still counted), and add a regression that starts a single counted waiter and proves it acquires before queue 0 begins the second resumed piece. New work must still interrupt the wait immediately. I did not find another blocker in the four-file diff. Hosted checks were still queued at review time, so I am not making a CI claim here.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6031020430 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/26450#pullrequestreview-6031020430). Originally posted by [malekaifa](https://github.com/malekaifa) on 2026-11-23T23:13:43Z. **Review:** CHANGES_REQUESTED --- Blocking fairness gap on exact head `abb9aa2e615c6222a835b30b85bbf65a64ecc8a1`. The resume idea addresses the measured pending-image problem, but the implementation does not yet guarantee the PR's stated property that a thread already blocked on `GuestMemory::GpuMutex()` waits for at most one stored piece. `GpuMutexType::block()` keeps `Waiters()` nonzero until the waiter actually acquires the underlying `AgcDriver::Mutex`; that mutex is barging (`unlock()` stores `Unlocked` and then `notify_one()`, while a new caller may win `try_lock()` before the notified waiter runs). In `QueueWorker.cpp`, after a yielded store we wait only until `Waiters()==0` **or 2 ms expires**. If the counted waiter has not been scheduled/acquired within those 2 ms, queue 0 immediately loops, takes `StoreIdleResults()` again, and its `YieldToGpuMutexWaiters` deliberately stores the first 4 MiB piece before checking waiters. The same waiter can therefore sit through multiple pieces/passes; under scheduler load this can reintroduce the long lock hold #20145 was intended to cap. The new `IdleStoreResumes` test only proves eventual completion while another thread repeatedly acquires/releases the mutex; it does not prove that the waiter which caused a yield gets the handoff before the next store piece. Please make the resume condition observe an actual handoff (or otherwise avoid reacquiring while the causal waiter is still counted), and add a regression that starts a single counted waiter and proves it acquires before queue 0 begins the second resumed piece. New work must still interrupt the wait immediately. I did not find another blocker in the four-file diff. Hosted checks were still queued at review time, so I am not making a CI claim here. AI-assisted: yes
MarcZX10 commented 2026-11-25 07:49:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-25T07:49:53Z.

Review: COMMENTED


Reviewed — comments

The code review found no verified blocking defect, but the reported tests and target-scene performance results were not independently run or confirmed.

What I ran

Reviewed head 9ce2c86411b7150a289e3aa58bc65bb2e4a98b90 against the prepared diff and targeted source context using repo_read.py. No tests or builds were run.

Results

Check Result Note
Diff and targeted source review passed Four changed files examined
Build and tests not run Results in the PR description are author-reported
Target-scene performance not run PPSA20423 and PPSA24423 not tested here

Negative control

Not run. The PR description reports that the new test fails on the stated main revision and passes on this head; I did not independently confirm those runs.

Findings

Blocking

None found in the code reviewed.

Non-blocking

  • The reported no-harm comparison covers PPSA21564 only. The description identifies PPSA20423 and PPSA24423 as the scenes needed to establish the intended performance gain and confirm that the earlier waits remain eliminated. Consider adding those A/B results before treating the performance claim as demonstrated.

Sources

  • Changed-line evidence: QueueWorker.cpp:248-262 and VulkanDevice.cpp:3156-3163.
  • The prepared description and diff were reviewed; no external source links were needed.

Not verified

  • No build, test, or benchmark was run in this review. The reported test passes, negative control, and PPSA21564 comparison are author-reported.
  • No physical console behavior or results on PPSA20423/PPSA24423 were verified.
  • Read changed files: VulkanDevice.hpp, QueueWorker.cpp, VulkanDevice.cpp (targeted range), and IdleStoreResumes.cpp (targeted range). VulkanDevice.cpp was excluded from the prepared full-text bundle because of size; its changed function was read through repo_read.py.

To get this to a good state

Nothing is required. Optional: provide matched A/B results on PPSA20423 and PPSA24423 to support the stated performance goal.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6053694612 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/26450#pullrequestreview-6053694612). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-25T07:49:53Z. **Review:** COMMENTED --- # Reviewed — comments The code review found no verified blocking defect, but the reported tests and target-scene performance results were not independently run or confirmed. ## What I ran Reviewed head `9ce2c86411b7150a289e3aa58bc65bb2e4a98b90` against the prepared diff and targeted source context using `repo_read.py`. No tests or builds were run. ## Results | Check | Result | Note | |---|---|---| | Diff and targeted source review | passed | Four changed files examined | | Build and tests | not run | Results in the PR description are author-reported | | Target-scene performance | not run | PPSA20423 and PPSA24423 not tested here | ## Negative control Not run. The PR description reports that the new test fails on the stated `main` revision and passes on this head; I did not independently confirm those runs. ## Findings ### Blocking None found in the code reviewed. ### Non-blocking - The reported no-harm comparison covers PPSA21564 only. The description identifies PPSA20423 and PPSA24423 as the scenes needed to establish the intended performance gain and confirm that the earlier waits remain eliminated. Consider adding those A/B results before treating the performance claim as demonstrated. ## Sources - Changed-line evidence: `QueueWorker.cpp:248-262` and `VulkanDevice.cpp:3156-3163`. - The prepared description and diff were reviewed; no external source links were needed. ## Not verified - No build, test, or benchmark was run in this review. The reported test passes, negative control, and PPSA21564 comparison are author-reported. - No physical console behavior or results on PPSA20423/PPSA24423 were verified. - Read changed files: `VulkanDevice.hpp`, `QueueWorker.cpp`, `VulkanDevice.cpp` (targeted range), and `IdleStoreResumes.cpp` (targeted range). `VulkanDevice.cpp` was excluded from the prepared full-text bundle because of size; its changed function was read through `repo_read.py`. ## To get this to a good state Nothing is required. Optional: provide matched A/B results on PPSA20423 and PPSA24423 to support the stated performance goal.
malekaifa commented 2026-11-26 15:59:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by malekaifa on 2026-11-26T15:59:28Z.

Review: APPROVED


Re-reviewed exact head 9ce2c86411b7150a289e3aa58bc65bb2e4a98b90 against the fairness issue raised on abb9aa2e615c6222a835b30b85bbf65a64ecc8a1.

The 2 ms escape is gone. After StoreIdleResults yields, queue 0 stays in the handoff loop until GpuMutex().Waiters() == 0, new queue-0 work, a pending completion, or an idle-epoch change. GpuMutexType::block() increments the count before waiting and decrements it only after LockSpinning has acquired the underlying mutex, so zero is an actual handoff rather than a timer. A clean handoff resumes the pending store without another quiet period; work/completion/epoch interruptions return to the ordinary idle path. I found no remaining blocking defect in the changed code.

On the single-waiter variant: I do not think it adds a distinct regression signal. The observable fairness window is when one waiter has acquired the mutex while another is still counted. The two-waiter regression deliberately creates that interval and checks that queue 0 does not re-enter while the second waiter remains blocked. With only one waiter, Waiters() reaches zero at the handoff, at which point queue 0 is supposed to resume; a separate test would duplicate the lower-level single-waiter accounting already covered by IdleStoreYields::CountsMutexWaiters, or require additional instrumentation to measure the next stored piece. Keep the current two-waiter regression.

The exact-head hosted Build, Emscripten portability, and Progress report workflows are successful. The PR discussion also contains GPU regression runs from the author (3/3) and an additional RTX 3070 report (3/3 with the validation layer). I did not run a local GPU test in this review.

One non-blocking description correction remains: the PR body still says “at most 2 ms”; the implementation now waits without a time limit until the handoff or an interruption. Please update that wording.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6077169737 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/26450#pullrequestreview-6077169737). Originally posted by [malekaifa](https://github.com/malekaifa) on 2026-11-26T15:59:28Z. **Review:** APPROVED --- Re-reviewed exact head `9ce2c86411b7150a289e3aa58bc65bb2e4a98b90` against the fairness issue raised on `abb9aa2e615c6222a835b30b85bbf65a64ecc8a1`. The 2 ms escape is gone. After `StoreIdleResults` yields, queue 0 stays in the handoff loop until `GpuMutex().Waiters() == 0`, new queue-0 work, a pending completion, or an idle-epoch change. `GpuMutexType::block()` increments the count before waiting and decrements it only after `LockSpinning` has acquired the underlying mutex, so zero is an actual handoff rather than a timer. A clean handoff resumes the pending store without another quiet period; work/completion/epoch interruptions return to the ordinary idle path. I found no remaining blocking defect in the changed code. On the single-waiter variant: I do not think it adds a distinct regression signal. The observable fairness window is when one waiter has acquired the mutex while another is still counted. The two-waiter regression deliberately creates that interval and checks that queue 0 does not re-enter while the second waiter remains blocked. With only one waiter, `Waiters()` reaches zero at the handoff, at which point queue 0 is supposed to resume; a separate test would duplicate the lower-level single-waiter accounting already covered by `IdleStoreYields::CountsMutexWaiters`, or require additional instrumentation to measure the next stored piece. Keep the current two-waiter regression. The exact-head hosted Build, Emscripten portability, and Progress report workflows are successful. The PR discussion also contains GPU regression runs from the author (3/3) and an additional RTX 3070 report (3/3 with the validation layer). I did not run a local GPU test in this review. One non-blocking description correction remains: the PR body still says “at most 2 ms”; the implementation now waits without a time limit until the handoff or an interruption. Please update that wording.
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!26450
No description provided.