fix(libkernel): use precise waits for select and poll on pipes on Windows #6070

Merged
fdfawef324 merged 1 commit from upstream-pr/6070 into main 2026-10-17 04:53:29 +00:00
fdfawef324 commented 2026-10-14 21:16:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fdfawef324 on 2026-10-14T21:16:20Z.


What

Part of #1471: the select and poll waits on pipes that still went through winpthreads after #4754, #4980 and #5013. On Windows, select and poll on pipes slept with std::this_thread::sleep_for. winpthreads ends these sleeps on 15.6 ms timer ticks, so a short guest timeout on an idle pipe waited about 15 ms per call, and a longer wait checked the pipes only every 15.6 ms.

  • HostPoll in Socket.cpp (Windows select over pipes, added in #5013) slept sleep_for(1 ms) between readiness checks, which could pass its deadline by a whole tick. It now sleeps with PreciseSleepUntil until 1 ms later or the deadline, whichever comes first. The same helper is used for select with no descriptors since #4754.
  • poll_nid_postfix in Udp.cpp with pipes and no native sockets slept sleep_for(slice). Once less than 1 ms was left, the slice truncated to 0 and the loop spun until the deadline without sleeping. It now sleeps with PreciseSleepUntil in the same way as HostPoll, and an infinite timeout still sleeps 1 ms per round. WSAPoll with sockets keeps its integer slice. <thread> is no longer used in Udp.cpp and is removed.

Each sleep requests at most 1 ms; the high-resolution timer overshoots by about 0.5 ms here, so the pipes are now checked about every 1.5 ms instead of every 15.6 ms. This has two effects:

  • A pipe that becomes ready is noticed within about 1.5 ms instead of up to 15.6 ms, closer to FreeBSD, where pipe_write wakes a poller at once through pipeselwakeup.
  • A thread blocked on an idle pipe in a long or infinite select or poll wakes about ten times as often (roughly 700 times a second instead of 64), and each wake queries every pipe with NtQueryInformationFile. This is the 1 ms interval docs/dev/debt/libkernel.md already describes for both paths.

A 1 ms poll on an idle pipe now returns after about 1.5 ms instead of spinning on the CPU for exactly 1 ms. The Linux paths (::poll) are untouched.

Other winpthreads waits listed in #1471 (for example in libSceAgcDriver WorkerSampler.cpp and WaitMemory.cpp, and libSceUsbd Export.cpp) are not part of this PR.

Tests:

  • guest_poll (TestPipes) times 20 poll calls with a 2 ms timeout on an empty pipe read end and requires the total to fall in [40 ms, 200 ms).
  • guest_select times 20 select calls with a 1 ms timeout on the same kind of descriptor and requires [20 ms, 200 ms).

A 1 ms poll can't catch the old sleep. On main the remaining time truncates to 0 ms, so the loop spins until the deadline and never reaches sleep_for; 20 such calls take 20 ms there. That is why the poll check uses 2 ms.

Tested

Windows Server 2025 (10.0.26100), AMD Ryzen 9 5900X, WinLibs MinGW-w64 GCC 15.2.0, Ninja, Release, BUILD_TESTING=ON. Branch rebased on main 0ae1deb85.

  • Red, with main's Socket.cpp and Udp.cpp and the new tests: guest_poll and guest_select abort (0xc0000409) in 3 of 3 runs. Measured with temporary timing output that was not committed: 20 × 2 ms poll took 312 ms and 20 × 1 ms select took 312 ms.
  • Green, with this change: both pass in 3 of 3 runs with the same timing output. Measured: 49 to 50 ms for the poll set and 29 to 37 ms for the select set.
  • CPU use, with the same temporary output (thread time from GetThreadTimes, 15.6 ms resolution): 200 poll calls with a 1 ms timeout on an idle pipe took 201 ms wall and 188 to 203 ms CPU on main, and 284 to 301 ms wall and 0 to 31 ms CPU with this change. One 1000 ms poll on an idle pipe used 0 to 15.6 ms CPU in both.
  • ctest -j4 -R '^(precise_wait|guest_socket_.*|guest_sce_net|guest_net_ctl|guest_udp|guest_poll|guest_select_wait|guest_select|guest_tcp|guest_kqueue)$': 28 of 28 passed in 3 of 3 runs on the final base, and in 8 of 8 runs on fa539dd9b, 4 commits earlier (libSceAgcDriver changes only).
  • #5999 (open) also changes the Windows HostPoll, a few lines above the changed sleep. A trial merge of its head 6ccea54b9 with this branch (git merge-tree) has no conflict, so neither PR depends on the other.

Not tested: Linux (the changed lines are Windows-only, and the new checks use ::poll timeouts there), macOS, titles.

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
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none
  • GPUs tested: n/a (no AGC, shader recompiler or VideoOut change)
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4864638489 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6070). Originally posted by [fdfawef324](https://github.com/fdfawef324) on 2026-10-14T21:16:20Z. --- ### What Part of #1471: the `select` and `poll` waits on pipes that still went through winpthreads after #4754, #4980 and #5013. On Windows, `select` and `poll` on pipes slept with `std::this_thread::sleep_for`. winpthreads ends these sleeps on 15.6 ms timer ticks, so a short guest timeout on an idle pipe waited about 15 ms per call, and a longer wait checked the pipes only every 15.6 ms. - `HostPoll` in `Socket.cpp` (Windows `select` over pipes, added in #5013) slept `sleep_for(1 ms)` between readiness checks, which could pass its deadline by a whole tick. It now sleeps with `PreciseSleepUntil` until 1 ms later or the deadline, whichever comes first. The same helper is used for `select` with no descriptors since #4754. - `poll_nid_postfix` in `Udp.cpp` with pipes and no native sockets slept `sleep_for(slice)`. Once less than 1 ms was left, the slice truncated to 0 and the loop spun until the deadline without sleeping. It now sleeps with `PreciseSleepUntil` in the same way as `HostPoll`, and an infinite timeout still sleeps 1 ms per round. `WSAPoll` with sockets keeps its integer slice. `<thread>` is no longer used in `Udp.cpp` and is removed. Each sleep requests at most 1 ms; the high-resolution timer overshoots by about 0.5 ms here, so the pipes are now checked about every 1.5 ms instead of every 15.6 ms. This has two effects: - A pipe that becomes ready is noticed within about 1.5 ms instead of up to 15.6 ms, closer to FreeBSD, where `pipe_write` wakes a poller at once through `pipeselwakeup`. - A thread blocked on an idle pipe in a long or infinite `select` or `poll` wakes about ten times as often (roughly 700 times a second instead of 64), and each wake queries every pipe with `NtQueryInformationFile`. This is the 1 ms interval `docs/dev/debt/libkernel.md` already describes for both paths. A 1 ms `poll` on an idle pipe now returns after about 1.5 ms instead of spinning on the CPU for exactly 1 ms. The Linux paths (`::poll`) are untouched. Other winpthreads waits listed in #1471 (for example in libSceAgcDriver `WorkerSampler.cpp` and `WaitMemory.cpp`, and libSceUsbd `Export.cpp`) are not part of this PR. Tests: - `guest_poll` (`TestPipes`) times 20 `poll` calls with a 2 ms timeout on an empty pipe read end and requires the total to fall in [40 ms, 200 ms). - `guest_select` times 20 `select` calls with a 1 ms timeout on the same kind of descriptor and requires [20 ms, 200 ms). A 1 ms `poll` can't catch the old sleep. On `main` the remaining time truncates to 0 ms, so the loop spins until the deadline and never reaches `sleep_for`; 20 such calls take 20 ms there. That is why the `poll` check uses 2 ms. ### Tested Windows Server 2025 (10.0.26100), AMD Ryzen 9 5900X, WinLibs MinGW-w64 GCC 15.2.0, Ninja, Release, `BUILD_TESTING=ON`. Branch rebased on `main` 0ae1deb85. - Red, with `main`'s `Socket.cpp` and `Udp.cpp` and the new tests: `guest_poll` and `guest_select` abort (`0xc0000409`) in 3 of 3 runs. Measured with temporary timing output that was not committed: 20 × 2 ms `poll` took 312 ms and 20 × 1 ms `select` took 312 ms. - Green, with this change: both pass in 3 of 3 runs with the same timing output. Measured: 49 to 50 ms for the `poll` set and 29 to 37 ms for the `select` set. - CPU use, with the same temporary output (thread time from `GetThreadTimes`, 15.6 ms resolution): 200 `poll` calls with a 1 ms timeout on an idle pipe took 201 ms wall and 188 to 203 ms CPU on `main`, and 284 to 301 ms wall and 0 to 31 ms CPU with this change. One 1000 ms `poll` on an idle pipe used 0 to 15.6 ms CPU in both. - `ctest -j4 -R '^(precise_wait|guest_socket_.*|guest_sce_net|guest_net_ctl|guest_udp|guest_poll|guest_select_wait|guest_select|guest_tcp|guest_kqueue)$'`: 28 of 28 passed in 3 of 3 runs on the final base, and in 8 of 8 runs on fa539dd9b, 4 commits earlier (libSceAgcDriver changes only). - #5999 (open) also changes the Windows `HostPoll`, a few lines above the changed sleep. A trial merge of its head 6ccea54b9 with this branch (`git merge-tree`) has no conflict, so neither PR depends on the other. Not tested: Linux (the changed lines are Windows-only, and the new checks use `::poll` timeouts there), macOS, titles. ### 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 - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: none - [x] GPUs tested: n/a (no AGC, shader recompiler or VideoOut change) - [x] AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-15 12:14:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T12:14:10Z.


Some 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.

Pull request Shared files not compared
PR 5021 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 6044 core/libs/tests/GuestPoll.cpp
core/libs/tests/GuestSelect.cpp
PR 6222 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 6447 core/libs/prx/libkernel/Socket/src/Socket.cpp
PR 6828 core/libs/prx/libkernel/Socket/src/Udp.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6191785702 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6070#issuecomment-6191785702). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-15T12:14:10Z. --- <!-- pr-overlap --> Some 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. | Pull request | Shared files not compared | |---|---| | [PR 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `core/libs/prx/libkernel/Socket/src/Udp.cpp` | | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/tests/GuestPoll.cpp`<br>`core/libs/tests/GuestSelect.cpp` | | [PR 6222](https://www.github.com/boykopovar/AnyPS5/pull/6222) | `core/libs/prx/libkernel/Socket/src/Udp.cpp` | | [PR 6447](https://www.github.com/boykopovar/AnyPS5/pull/6447) | `core/libs/prx/libkernel/Socket/src/Socket.cpp` | | [PR 6828](https://www.github.com/boykopovar/AnyPS5/pull/6828) | `core/libs/prx/libkernel/Socket/src/Udp.cpp` |
MarcZX10 commented 2026-10-16 03:38:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-16T03:38:59Z.

Review: APPROVED


I reviewed both Windows pipe polling paths and the added timing cases. HostPoll now caps each precise sleep at its deadline, and the poll path also waits through a sub-millisecond remainder instead of spinning; zero-timeout calls remain immediate. The reported Windows red/green runs exercise these cases, and the Linux behavior stays on native poll. I found no blocker. Hosted platform build jobs are path-skipped, so I am relying on the detailed Windows validation in the PR body rather than claiming a local rerun.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5542792012 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6070#pullrequestreview-5542792012). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-16T03:38:59Z. **Review:** APPROVED --- I reviewed both Windows pipe polling paths and the added timing cases. `HostPoll` now caps each precise sleep at its deadline, and the `poll` path also waits through a sub-millisecond remainder instead of spinning; zero-timeout calls remain immediate. The reported Windows red/green runs exercise these cases, and the Linux behavior stays on native `poll`. I found no blocker. Hosted platform build jobs are path-skipped, so I am relying on the detailed Windows validation in the PR body rather than claiming a local rerun.
adriadam10 commented 2026-10-16 22:06:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-16T22:06:46Z.


Checked the diff: HostPoll and the pipe-only poll path now cap each precise sleep at 1 ms or the deadline; the no-descriptor poll case still goes through PreciseSleepUs before this loop and the infinite timeout keeps its 1 ms rounds. Build and ctest for libs/libkernel pass on Linux (181/181, including guest_poll, guest_select, guest_select_wait). The changed lines are Windows-only, so the Windows result from the PR body and the Windows Build are the evidence; merging once that is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6225612590 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6070#issuecomment-6225612590). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-16T22:06:46Z. --- Checked the diff: `HostPoll` and the pipe-only `poll` path now cap each precise sleep at 1 ms or the deadline; the no-descriptor `poll` case still goes through `PreciseSleepUs` before this loop and the infinite timeout keeps its 1 ms rounds. Build and ctest for libs/libkernel pass on Linux (181/181, including `guest_poll`, `guest_select`, `guest_select_wait`). The changed lines are Windows-only, so the Windows result from the PR body and the Windows Build are the evidence; merging once that is green.
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!6070
No description provided.