fix(libkernel): use precise sleep for sub-millisecond host waits #1576

Closed
neuralsin wants to merge 1 commit from upstream-pr/1576 into main
neuralsin commented 2026-10-08 13:15:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by neuralsin on 2026-10-08T13:15:04Z.


What

Addresses the sub-millisecond sleep busy-spin issue documented in #1471:
On Windows, std::this_thread::sleep_for goes through winpthreads' nanosleep, which truncates sub-millisecond durations (tv_nsec / 1'000'000) to 0 ms and returns immediately without sleeping or yielding. As a result, polling loops requesting sub-millisecond pauses spin at 100% CPU.

This PR switches the sub-millisecond host waits in four areas to use PreciseSleepUs() (which uses a Windows high-resolution waitable timer CREATE_WAITABLE_TIMER_HIGH_RESOLUTION on Windows and standard microsecond sleep on POSIX):

  • core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp: WAIT_REG_MEM polling loop (50 µs)
  • core/libs/prx/libkernel/Apr/src/Apr.cpp: _execute counter and address wait loop (50 µs)
  • core/libs/prx/libkernel/Aio/src/Aio.cpp: sceKernelAioWaitRequests and sceKernelAioWaitRequest polling loops (10 µs)
  • core/libs/prx/libSceFont/src/State.cpp: Font::Backoff spinlock backoff (30 µs)

Tested

  • Linux / WSL 2 Ubuntu 24.04 (x86_64), GCC 15.2.0, Ninja 1.13.2
  • ctest -R 'guest_aio|guest_ampr|guest_font': 4/4 passed (100% tests passed: guest_ampr_markers, guest_font, guest_font_writing_line, guest_aio)
  • python tools/check_conventions.py --base origin/main: 100% passed

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 in TechnicalDebt
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4787360836 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1576). Originally posted by [neuralsin](https://github.com/neuralsin) on 2026-10-08T13:15:04Z. --- ### What Addresses the sub-millisecond sleep busy-spin issue documented in #1471: On Windows, `std::this_thread::sleep_for` goes through winpthreads' `nanosleep`, which truncates sub-millisecond durations (`tv_nsec / 1'000'000`) to 0 ms and returns immediately without sleeping or yielding. As a result, polling loops requesting sub-millisecond pauses spin at 100% CPU. This PR switches the sub-millisecond host waits in four areas to use `PreciseSleepUs()` (which uses a Windows high-resolution waitable timer `CREATE_WAITABLE_TIMER_HIGH_RESOLUTION` on Windows and standard microsecond sleep on POSIX): - `core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp`: `WAIT_REG_MEM` polling loop (50 µs) - `core/libs/prx/libkernel/Apr/src/Apr.cpp`: `_execute` counter and address wait loop (50 µs) - `core/libs/prx/libkernel/Aio/src/Aio.cpp`: `sceKernelAioWaitRequests` and `sceKernelAioWaitRequest` polling loops (10 µs) - `core/libs/prx/libSceFont/src/State.cpp`: `Font::Backoff` spinlock backoff (30 µs) ### Tested - Linux / WSL 2 Ubuntu 24.04 (x86_64), GCC 15.2.0, Ninja 1.13.2 - `ctest -R 'guest_aio|guest_ampr|guest_font'`: 4/4 passed (100% tests passed: `guest_ampr_markers`, `guest_font`, `guest_font_writing_line`, `guest_aio`) - `python tools/check_conventions.py --base origin/main`: 100% passed ### 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 - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in TechnicalDebt - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
MarcZX10 commented 2026-10-08 22:25:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-08T22:25:00Z.


I found that the previous head of #1551 duplicated the same four production substitutions as this PR. I have re-scoped #1551 to add and register only a focused PreciseSleepUs CTest; it no longer changes those production files. The focused test passes locally on Linux, and fork CI is running on the updated head.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6070264138 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1576#issuecomment-6070264138). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-08T22:25:00Z. --- I found that the previous head of #1551 duplicated the same four production substitutions as this PR. I have re-scoped #1551 to add and register only a focused `PreciseSleepUs` CTest; it no longer changes those production files. The focused test passes locally on Linux, and fork CI is running on the updated head.
MarcZX10 commented 2026-10-08 22:40:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-08T22:40:17Z.


The updated test-only head of #1551 has now passed its fork Build: 495/495 Linux tests, 486/486 Windows tests, and all three relinker jobs. Its diff contains only PreciseWait.cpp and CTest registration; no production files overlap this PR.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6070484271 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1576#issuecomment-6070484271). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-08T22:40:17Z. --- The updated test-only head of #1551 has now passed its fork Build: 495/495 Linux tests, 486/486 Windows tests, and all three relinker jobs. Its diff contains only `PreciseWait.cpp` and CTest registration; no production files overlap this PR.
popcoder565 commented 2026-10-09 08:41:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by popcoder565 on 2026-10-09T08:41:49Z.


Windows numbers for this, since it's only been run on Linux so far. Setup: Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), with the 0.5 ms timer resolution libkernel sets.

wait takes
std::this_thread::sleep_for for 10, 50 or 200 µs 0 ms (returns at once)
high-resolution waitable timer (PreciseSleepUs) for 10, 50 or 200 µs about 0.50 ms each

So on Windows each of these polls goes from a busy spin to a 0.5 ms wait, 10–50 times the requested pause. On Linux they sleep roughly 60 µs.

  • AIO: neither loop runs in practice. SubmitCommands performs the reads and sets the final state before it returns. On main, 2000 back-to-back 4 KB sceKernelAioSubmitReadCommands + sceKernelAioWaitRequest pairs take 3.2 ms in total, and every wait returns on its first check. The AIO hunk doesn't change anything measurable.
  • Font backoff and APR waits: these do loop, so each contended backoff or unmet wait now costs about 0.5 ms instead of spinning. I haven't run a workload that hits them.

🤖 Addressed by Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6077516702 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1576#issuecomment-6077516702). Originally posted by [popcoder565](https://github.com/popcoder565) on 2026-10-09T08:41:49Z. --- Windows numbers for this, since it's only been run on Linux so far. Setup: Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), with the 0.5 ms timer resolution libkernel sets. | wait | takes | |---|---| | `std::this_thread::sleep_for` for 10, 50 or 200 µs | 0 ms (returns at once) | | high-resolution waitable timer (`PreciseSleepUs`) for 10, 50 or 200 µs | about 0.50 ms each | So on Windows each of these polls goes from a busy spin to a 0.5 ms wait, 10–50 times the requested pause. On Linux they sleep roughly 60 µs. - **AIO:** neither loop runs in practice. `SubmitCommands` performs the reads and sets the final state before it returns. On `main`, 2000 back-to-back 4 KB `sceKernelAioSubmitReadCommands` + `sceKernelAioWaitRequest` pairs take 3.2 ms in total, and every wait returns on its first check. The AIO hunk doesn't change anything measurable. - **Font backoff and APR waits:** these do loop, so each contended backoff or unmet wait now costs about 0.5 ms instead of spinning. I haven't run a workload that hits them. _🤖 Addressed by [Claude Code](https://claude.com/claude-code)_
MarcZX10 commented 2026-10-09 12:04:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-09T12:04:41Z.


I have integrated the four production call-site changes from #1576 into my #1551, alongside its focused PreciseSleepUs regression test and Windows test setup; the commit credits you as co-author. The combined current head passes the seven focused Linux Release tests and Conventions. #1551 is now the more complete canonical PR for this change; I have asked an upstream collaborator to run its required full Build under #2113. Please close #1576 as superseded once that current-head Build is confirmed, so the same call-site fix is reviewed and merged once.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6080515741 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1576#issuecomment-6080515741). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-09T12:04:41Z. --- I have integrated the four production call-site changes from #1576 into my #1551, alongside its focused `PreciseSleepUs` regression test and Windows test setup; the commit credits you as co-author. The combined current head passes the seven focused Linux Release tests and Conventions. #1551 is now the more complete canonical PR for this change; I have asked an upstream collaborator to run its required full Build under #2113. Please close #1576 as superseded once that current-head Build is confirmed, so the same call-site fix is reviewed and merged once.
MarcZX10 commented 2026-10-09 20:02:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-09T20:02:10Z.


Update on the overlapping wait changes

I have rebased #1551 onto current main at 8b393b6e (head 46a77cca) and rerun its seven focused Linux tests; all pass. It contains the same four production call-site changes as #1576, credits you as co-author, and also carries the focused PreciseSleepUs regression test. For that reason #1551 is the more complete canonical PR for this behavior.

#1576 currently has no checks reported, while #1551 still needs the repository-required upstream Build/relinker run. Please close #1576 as superseded once that required validation is recorded on #1551, so reviewers only have to land one copy of the call-site changes.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6088323498 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1576#issuecomment-6088323498). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-09T20:02:10Z. --- ### Update on the overlapping wait changes I have rebased #1551 onto current `main` at `8b393b6e` (head `46a77cca`) and rerun its seven focused Linux tests; all pass. It contains the same four production call-site changes as #1576, credits you as co-author, and also carries the focused `PreciseSleepUs` regression test. For that reason #1551 is the more complete canonical PR for this behavior. #1576 currently has no checks reported, while #1551 still needs the repository-required upstream Build/relinker run. Please close #1576 as superseded once that required validation is recorded on #1551, so reviewers only have to land one copy of the call-site changes.
MarcZX10 commented 2026-10-09 23:01:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-09T23:01:56Z.


Consolidation status

I refreshed Marc's existing PR #1551 onto upstream main 0df342c6 (head e777d26b) and reran focused Linux validation. It still carries the four precise-wait call-site changes from this PR, credits Shaan as co-author, and also includes the dedicated helper regression test.

PR Scope Current status
#1551 Four production call sites plus helper regression test Canonical carrier; Linux focused tests pass on the current head; upstream Build/relinker matrix still needs a collaborator dispatch
#1576 Same four production call sites Duplicate behavior already integrated into #1551 with attribution retained

Could you close #1576 so the same production change is not reviewed twice? Please keep this PR open if you have additional behavior or evidence that is not covered by #1551; we can review that separately.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6090730497 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1576#issuecomment-6090730497). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-09T23:01:56Z. --- ### Consolidation status I refreshed Marc's existing [PR #1551](https://github.com/boykopovar/AnyPS5/pull/1551) onto upstream `main` `0df342c6` (head `e777d26b`) and reran focused Linux validation. It still carries the four precise-wait call-site changes from this PR, credits Shaan as co-author, and also includes the dedicated helper regression test. | PR | Scope | Current status | |---|---|---| | #1551 | Four production call sites plus helper regression test | Canonical carrier; Linux focused tests pass on the current head; upstream Build/relinker matrix still needs a collaborator dispatch | | #1576 | Same four production call sites | Duplicate behavior already integrated into #1551 with attribution retained | Could you close #1576 so the same production change is not reviewed twice? Please keep this PR open if you have additional behavior or evidence that is not covered by #1551; we can review that separately.
adriadam10 commented 2026-10-10 00:02:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T00:02:34Z.


Closing as a duplicate: #1551 now carries the same four call-site changes (Pm4.cpp, Apr.cpp, Aio.cpp, State.cpp), credits you as co-author, and adds a regression test for PreciseSleepUs, so the change should be reviewed and merged once, there.

Two points from @popcoder565's Windows measurements that are worth addressing in #1551: each wait becomes ~0.5 ms instead of 10-50 µs, and the AIO loops don't run in practice, so that hunk changes nothing measurable.

Thanks for the patch; feel free to reopen if #1551 doesn't move forward.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6091361684 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1576#issuecomment-6091361684). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-10T00:02:34Z. --- Closing as a duplicate: #1551 now carries the same four call-site changes (`Pm4.cpp`, `Apr.cpp`, `Aio.cpp`, `State.cpp`), credits you as co-author, and adds a regression test for `PreciseSleepUs`, so the change should be reviewed and merged once, there. Two points from @popcoder565's Windows measurements that are worth addressing in #1551: each wait becomes ~0.5 ms instead of 10-50 µs, and the AIO loops don't run in practice, so that hunk changes nothing measurable. Thanks for the patch; feel free to reopen if #1551 doesn't move forward.
MarcZX10 commented 2026-10-10 07:21:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-10T07:21:37Z.


Coordination requested after the #1551 review

The reviewer on #1551 confirmed that both open PRs currently contain the same four production call-site changes and asked us to choose one carrier. I have preserved your call-site work in #1551 with you credited as co-author; #1551 also contains the focused helper regression test. I prefer #1551 as the single carrier.

Could you confirm whether you agree to close #1576 as superseded once its changes are retained in #1551? If you prefer #1576 to carry the production changes, please say so and I will remove those call-site edits from #1551. I will not ask for both PRs to merge.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6095071772 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1576#issuecomment-6095071772). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-10T07:21:37Z. --- ### Coordination requested after the #1551 review The reviewer on #1551 confirmed that both open PRs currently contain the same four production call-site changes and asked us to choose one carrier. I have preserved your call-site work in #1551 with you credited as co-author; #1551 also contains the focused helper regression test. I prefer #1551 as the single carrier. Could you confirm whether you agree to close #1576 as superseded once its changes are retained in #1551? If you prefer #1576 to carry the production changes, please say so and I will remove those call-site edits from #1551. I will not ask for both PRs to merge.

Pull request closed

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!1576
No description provided.