fix(libSceJobManager): spin idle workers without taking the manager lock #12085

Merged
JJenkx merged 1 commit from upstream-pr/12085 into main 2026-10-29 23:59:12 +00:00
JJenkx commented 2026-10-27 19:48:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-27T19:48:59Z.


What

A JobManager worker that spins, whether inside its spin window or because it is persistent, unlocked and relocked the manager mutex around every _mm_pause(). With more than one spinning worker, they contend on that mutex continuously. On Windows the winpthreads mutex then parks them in the kernel, so "spinning" workers mostly sleep.

guest_job_manager_worker_count shows this on current main. With 4 workers and 2 set persistent, a local trace confirms that both workers enter the spin path. Yet over the test's 2-second window they used 125 ms and 0 ms of CPU, so the check active == persistent fails. The test fails in every run here.

Workers now pause on a workEpoch atomic instead of the mutex. WakeWorkers (every publication of work), setPersistentThreadCount and shutdown advance it. A spinner retakes the lock as soon as the epoch changes, or after 1024 pauses, so the spin-window deadline and the other conditions are still re-checked regularly. Sleeping workers and the wake-token logic are unchanged.

Tested

Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X (16 cores), WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, BUILD_TESTING=ON, on main d13b3d7c55. Vulkan is hidden (VK_DRIVER_FILES pointing at a missing manifest), so guest_job_manager_profiler_gpu skips.

  • Before: guest_job_manager_worker_count fails at Require(active == persistent) in each of 4 runs on main 14332cd8d0. It was found in a full Windows CTest run of that head.
  • After: all 29 guest_job_manager_* tests pass in each of 3 runs on the head f25e58c8cc, from all five test executables. profiler_gpu skips, as above.
  • Single worker: guest_job_manager_worker_spin measures one worker's idle CPU with spin 50. It is about the same: 91.8 to 92.1 M cycles on main against 98.3 to 100.5 M with this change, 3 runs each. One spinner has no contention, so the old loop already spun there.
  • python tools/check_conventions.py --base origin/main and git diff --check pass.

Not tested: Linux and macOS, whose futex-based mutexes may have hidden the effect; game runs; a frame-time comparison. This PR claims only the worker behaviour above, not a performance change.

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: none
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5040134982 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12085). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-27T19:48:59Z. --- ### What A JobManager worker that spins, whether inside its spin window or because it is persistent, unlocked and relocked the manager mutex around every `_mm_pause()`. With more than one spinning worker, they contend on that mutex continuously. On Windows the winpthreads mutex then parks them in the kernel, so "spinning" workers mostly sleep. `guest_job_manager_worker_count` shows this on current `main`. With 4 workers and 2 set persistent, a local trace confirms that both workers enter the spin path. Yet over the test's 2-second window they used **125 ms and 0 ms of CPU**, so the check `active == persistent` fails. The test fails in every run here. Workers now pause on a `workEpoch` atomic instead of the mutex. `WakeWorkers` (every publication of work), `setPersistentThreadCount` and shutdown advance it. A spinner retakes the lock as soon as the epoch changes, or after 1024 pauses, so the spin-window deadline and the other conditions are still re-checked regularly. Sleeping workers and the wake-token logic are unchanged. ### Tested Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X (16 cores), WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, `BUILD_TESTING=ON`, on `main` `d13b3d7c55`. Vulkan is hidden (`VK_DRIVER_FILES` pointing at a missing manifest), so `guest_job_manager_profiler_gpu` skips. - **Before:** `guest_job_manager_worker_count` fails at `Require(active == persistent)` in each of 4 runs on `main` `14332cd8d0`. It was found in a full Windows CTest run of that head. - **After:** all 29 `guest_job_manager_*` tests pass in each of 3 runs on the head `f25e58c8cc`, from all five test executables. `profiler_gpu` skips, as above. - **Single worker:** `guest_job_manager_worker_spin` measures one worker's idle CPU with spin 50. It is about the same: 91.8 to 92.1 M cycles on `main` against 98.3 to 100.5 M with this change, 3 runs each. One spinner has no contention, so the old loop already spun there. - `python tools/check_conventions.py --base origin/main` and `git diff --check` pass. **Not tested:** Linux and macOS, whose futex-based mutexes may have hidden the effect; game runs; a frame-time comparison. This PR claims only the worker behaviour above, not a performance change. ### 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: none - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes AI-assisted (Claude Code).
MarcZX10 commented 2026-10-27 20:32:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T20:32:21Z.

Review: CHANGES_REQUESTED


I reviewed the current head f25e58c8cc07da5c12e96e559d8d6d9d0c0e018e.

The change keeps manager state under manager.lock and uses workEpoch only to bound how long a worker spins before rechecking that state. The Release guest_job_manager_tests target built successfully from this exact head in my isolated Linux checkout. I could not get a local runtime pass: all 29 registered guest_job_manager_* tests abort on this host because its Xeon E5-1620 v2 lacks the AVX2/BMI/LZCNT features required by the project. I therefore do not count that run as a test failure in the change. The PR reports 29 Windows test cases passing three times, and the author later supplied focused Linux failure/registration test runs.

There is an integration blocker with open PR #10249. Its current Export.cpp implementation replaces WorkerEntry's spin loop with condition-variable waiting, while this PR changes that same loop to poll workEpoch; a pairwise merge preview conflicts in Export.cpp. These are competing worker-loop behaviors, not just unrelated edits to the same file. Please coordinate with #10249's author on which behavior should land first or whether one supersedes the other, then declare the agreed order with Depends on if applicable. The overlap check also cannot compare #10249 against main while #10249 itself conflicts with the base, so this needs an explicit resolution before the stack can be integrated confidently.

I found no separate defect in the epoch signaling on this head. I’m requesting changes for the unresolved integration order, not for a demonstrated runtime regression.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5673898804 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12085#pullrequestreview-5673898804). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-27T20:32:21Z. **Review:** CHANGES_REQUESTED --- I reviewed the current head `f25e58c8cc07da5c12e96e559d8d6d9d0c0e018e`. The change keeps manager state under `manager.lock` and uses `workEpoch` only to bound how long a worker spins before rechecking that state. The Release `guest_job_manager_tests` target built successfully from this exact head in my isolated Linux checkout. I could not get a local runtime pass: all 29 registered `guest_job_manager_*` tests abort on this host because its Xeon E5-1620 v2 lacks the AVX2/BMI/LZCNT features required by the project. I therefore do not count that run as a test failure in the change. The PR reports 29 Windows test cases passing three times, and the author later supplied focused Linux failure/registration test runs. There is an integration blocker with open PR #10249. Its current `Export.cpp` implementation replaces `WorkerEntry`'s spin loop with condition-variable waiting, while this PR changes that same loop to poll `workEpoch`; a pairwise merge preview conflicts in `Export.cpp`. These are competing worker-loop behaviors, not just unrelated edits to the same file. Please coordinate with #10249's author on which behavior should land first or whether one supersedes the other, then declare the agreed order with `Depends on` if applicable. The overlap check also cannot compare #10249 against `main` while #10249 itself conflicts with the base, so this needs an explicit resolution before the stack can be integrated confidently. I found no separate defect in the epoch signaling on this head. I’m requesting changes for the unresolved integration order, not for a demonstrated runtime regression.
MarcZX10 commented 2026-10-29 08:51:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T08:51:53Z.

Review: APPROVED


Re-reviewed exact head f25e58c8cc07da5c12e96e559d8d6d9d0c0e018e. The Windows evidence closes the requested worker-count check: guest_job_manager_worker_count passes 3/3 on a clean merge with main edf6804cf5; the author also reports all 29 guest_job_manager_* tests passing in five repeated runs on the earlier merged tree. Current-main merge preview is clean and the effective change remains the worker spin/epoch signaling edit. I checked the overlap note: #10249’s conflict with main is in its stale SubmitCompute context, not this PR’s worker-loop hunk; that PR still needs its own main rebase. No dependency on #10249 is required for this independent worker change. Hosted Windows and conventions pass; product Linux is skipped. I did not run local tests.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5695052800 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12085#pullrequestreview-5695052800). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-29T08:51:53Z. **Review:** APPROVED --- Re-reviewed exact head `f25e58c8cc07da5c12e96e559d8d6d9d0c0e018e`. The Windows evidence closes the requested worker-count check: `guest_job_manager_worker_count` passes 3/3 on a clean merge with main `edf6804cf5`; the author also reports all 29 `guest_job_manager_*` tests passing in five repeated runs on the earlier merged tree. Current-main merge preview is clean and the effective change remains the worker spin/epoch signaling edit. I checked the overlap note: #10249’s conflict with main is in its stale `SubmitCompute` context, not this PR’s worker-loop hunk; that PR still needs its own main rebase. No dependency on #10249 is required for this independent worker change. Hosted Windows and conventions pass; product Linux is skipped. I did not run local tests.
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!12085
No description provided.