fix(libkernel): use precise waits for select and poll on pipes on Windows #6070
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/6070"
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 fdfawef324 on 2026-10-14T21:16:20Z.
What
Part of #1471: the
selectandpollwaits on pipes that still went through winpthreads after #4754, #4980 and #5013. On Windows,selectandpollon pipes slept withstd::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.HostPollinSocket.cpp(Windowsselectover pipes, added in #5013) sleptsleep_for(1 ms)between readiness checks, which could pass its deadline by a whole tick. It now sleeps withPreciseSleepUntiluntil 1 ms later or the deadline, whichever comes first. The same helper is used forselectwith no descriptors since #4754.poll_nid_postfixinUdp.cppwith pipes and no native sockets sleptsleep_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 withPreciseSleepUntilin the same way asHostPoll, and an infinite timeout still sleeps 1 ms per round.WSAPollwith sockets keeps its integer slice.<thread>is no longer used inUdp.cppand 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:
pipe_writewakes a poller at once throughpipeselwakeup.selectorpollwakes about ten times as often (roughly 700 times a second instead of 64), and each wake queries every pipe withNtQueryInformationFile. This is the 1 ms intervaldocs/dev/debt/libkernel.mdalready describes for both paths.A 1 ms
pollon 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.cppandWaitMemory.cpp, and libSceUsbdExport.cpp) are not part of this PR.Tests:
guest_poll(TestPipes) times 20pollcalls with a 2 ms timeout on an empty pipe read end and requires the total to fall in [40 ms, 200 ms).guest_selecttimes 20selectcalls with a 1 ms timeout on the same kind of descriptor and requires [20 ms, 200 ms).A 1 ms
pollcan't catch the old sleep. Onmainthe remaining time truncates to 0 ms, so the loop spins until the deadline and never reachessleep_for; 20 such calls take 20 ms there. That is why thepollcheck 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 onmain0ae1deb85.main'sSocket.cppandUdp.cppand the new tests:guest_pollandguest_selectabort (0xc0000409) in 3 of 3 runs. Measured with temporary timing output that was not committed: 20 × 2 mspolltook 312 ms and 20 × 1 msselecttook 312 ms.pollset and 29 to 37 ms for theselectset.GetThreadTimes, 15.6 ms resolution): 200pollcalls with a 1 ms timeout on an idle pipe took 201 ms wall and 188 to 203 ms CPU onmain, and 284 to 301 ms wall and 0 to 31 ms CPU with this change. One 1000 mspollon 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 onfa539dd9b, 4 commits earlier (libSceAgcDriver changes only).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
::polltimeouts there), macOS, titles.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)🤖 Generated with Claude Code
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.
core/libs/prx/libkernel/Socket/src/Udp.cppcore/libs/tests/GuestPoll.cppcore/libs/tests/GuestSelect.cppcore/libs/prx/libkernel/Socket/src/Udp.cppcore/libs/prx/libkernel/Socket/src/Socket.cppcore/libs/prx/libkernel/Socket/src/Udp.cppImported 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.
HostPollnow caps each precise sleep at its deadline, and thepollpath 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 nativepoll. 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.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-16T22:06:46Z.
Checked the diff:
HostPolland the pipe-onlypollpath now cap each precise sleep at 1 ms or the deadline; the no-descriptorpollcase still goes throughPreciseSleepUsbefore this loop and the infinite timeout keeps its 1 ms rounds. Build and ctest for libs/libkernel pass on Linux (181/181, includingguest_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.