perf(libkernel): wake guest mutex waiters on release on Windows #5429
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/5429"
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 Dripl1x on 2026-10-13T19:30:58Z.
What
On Windows a thread blocked on a guest mutex polled the host mutex.
scePthreadMutexLockslept 1 ms between attempts, the last 0.5 ms of it in a busy wait, andscePthreadMutexTimedlockslept 0.5 ms. A released mutex reached its waiter up to 1 ms late, and an untimed waiter kept part of a processor busy. #2051 introduced the polling so thatsceKernelRaiseExceptionreaches a waiting thread.Waiters now park and the release wakes them:
TimedWaitgains a parking table keyed by the lock's address: 256 buckets, each a list of the threads' existing wait events. A waiter registers, tries the lock once more and then waits alertably on its event, so an exception is still delivered during the wait through the APC path of #2051, and the wait goes on afterwards.scePthreadMutexUnlockand the release inside a condition wait wake one waiter of that mutex. They use only the mutex's address after unlocking it, so the thread that acquires the mutex next may destroy it at once.try_lockattempts, so a short critical section is handed over without a kernel wait.scePthreadMutexTimedlock, whichpthread_mutex_timedlockcalls, parks until its deadline; underAPS5_COARSE_TIMED_WAITSit keeps the hosttry_lock_for.MutexOperations::Timedlock, which takes an absolute deadline and has no caller at present, parks in slices of 100 ms and reads the wall clock between them.TimedWait::AcquireUntil, which this change leaves unreachable, andPollSleepUntilare removed.Related:
Mutex.cpp. A trial merge conflicts only on those lines; it is not a functional prerequisite. It is older and smaller, so this PR goes after it and is rebased when it merges; the checklist declares that order.RaiseDeferralfor the address-wait lock on Windows. This PR defers delivery around its own list lock through the existing wait counter and does not need it; a trial merge of the two is clean.Tested
Windows 11, Ryzen 7 5800H, WinLibs MinGW-w64 GCC 15.2.0 r7; Ubuntu 26.04 under WSL, GCC 15.2.0. Release,
BUILD_TESTING=ON, root configuration withFFMPEG_PREBUILT_DIRpointed at an already unpacked package. Head99eaf73102ce5f9d4c530720ff76ad93a81bcdceonmain18c438b10. The raw output ofmain, the performance report and the traces are in this comment; they were captured at the previous head2710e219a, which differs from this one only in how the spin loop pauses (SpinPause, defined asYieldProcessor(), instead of the compiler builtin) and in the test. The output for this head is in this comment.The new Windows test
guest_mutex_contentionchecks behaviour only, for error-checking and recursive mutexes: a blocked waiter of a plain and of a timed lock gets the mutex its holder releases, a timed lock of a held mutex times out and not before its deadline, four threads of 20,000 acquisitions each exclude one another, and 400 handoffs after which the waiter destroys the mutex. It asserts no latency and no processor share, so it passes onmainas well.Run with
--report, the same executable measures instead: 31 handoffs of a held mutex to a blocked waiter, the processor time of a waiter blocked for 200 ms, and four threads contending for 0.3 s. Three--reportruns of each build:mainguest_mutex_contentionpasses 20 of 20 repeated runs. Since the review it has no timing assertion, so no run of it fails onmain; what separates the two builds is the--reporttable above.guest_mutex_raise_exceptionandguest_raise_exception, which deliver an exception to a thread blocked in a mutex lock and in a timed mutex lock, the condition-wait tests andguest_pthread_cancel_recoverywith its coarse variant.mainand of this change from the same machine and scene (ps5-doom,-timedemo demo1) are in the same comment. That scene is limited by the 60 Hz present, so its frame times are equal and it shows no regression; it does not show the effect measured above.git diff --checkpass.Not tested: a title that contends on guest mutexes, the Unity title of #2051 (PPSA29141), macOS, the full test suite. Full PR builds need a collaborator's manual Build run.
Checklist
main; no other open PR implements this mutex wait.mdfiles or images added to the repositoryImported from boykopovar/AnyPS5. Originally posted by Dripl1x on 2026-10-13T19:34:04Z.
Evidence for head
2710e219aonmain18c438b10. Local build paths are replaced with<build>.Component measurements
guest_mutex_contention_tests --report, three runs of each build of the same test source. Windows 11, Ryzen 7 5800H, WinLibs MinGW-w64 GCC 15.2.0 r7, Release.main: the four production files from
18c438b10This PR
Negative control: with the four production files from
mainthe test fails 5 of 5 runs; the first:With the change it passes 20 of 20 runs (
ctest --repeat until-fail:20).Performance report
Captured as the Performance report form describes.
Title and scene: PPSA99666 (ps5-doom v1.1.1 homebrew),
-timedemo demo1, presents 600 to 1499 (APS5_TRACE_FRAMES=600:900), 1920x1080.What changed: nothing measurable in this scene. All four captures are limited by the 60 Hz present, and this homebrew has no contended guest mutex in its frame, so the captures show no regression and do not show the effect measured above. I have no title that contends on guest mutexes to capture.
main18c438b1092b2710e219a2e12710e219a2e1main18c438b1092bBoth builds are clean checkouts (
dirtyfalse), each with its own complete set of libraries. NVIDIA GeForce RTX 3070 Laptop GPU, driver 617.42. The traces and the summary lines are inDripl1x/AnyPS5@237574c7d.Summary lines (capture order:
main, this PR, this PR,main):Capture:
APS5_TRACE_*orAPS5_PROFILE_*switch was set, and the log was not redirected (the title ran in its own console window).Digest: main, first capture
frames 600:900 from present 600: 900 presents, frame p50 16.7 / p90 17.0 / p99 17.2 / max 17.7 ms, GPU busy 3.8 ms per present
NVIDIA GeForce RTX 3070 Laptop GPU, NVIDIA 617.42, Windows 10.0.26300, 1920x1080, commit
18c438b109, dropped: noSlowest frames (largest slices inside each, inclusive)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): Driver.Packet 16.4, Driver.Packet.rendering_wait 16.1, batch 3.8, Driver.Present 2.8, Driver.Present.render_fence_wait 1.7, Vulkan.Retire 1.7
Longest waits
Digest: this PR, first capture
frames 600:900 from present 600: 900 presents, frame p50 16.7 / p90 17.0 / p99 17.2 / max 17.9 ms, GPU busy 3.7 ms per present
NVIDIA GeForce RTX 3070 Laptop GPU, NVIDIA 617.42, Windows 10.0.26300, 1920x1080, commit 2710e219a2e1, dropped: no
Slowest frames (largest slices inside each, inclusive)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): Driver.Packet 16.4, Driver.Packet.rendering_wait 16.2, batch 3.7, Driver.Present 2.7, Driver.Present.render_fence_wait 1.7, Vulkan.Retire 1.7
Longest waits
Tests
Windows: 57 thread, synchronization, signal and exception CTests
Linux: 13 mutex, condition, cancellation, rwlock, semaphore and exception CTests
AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by Dripl1x on 2026-10-13T19:34:44Z.
@xXPBSXx this PR edits the three lock call sites in
Pthread/src/Mutex.cppwhere #4971 (6ddb7e5c5) switchesacquireMutextoacquireResolved. A trialgit merge-treeconflicts only on those lines. #4971 is older and smaller, so this PR lists it inDepends onas a merge order, not a prerequisite, and I rebase after it lands: the calls becomeacquireResolved(current, ...), with the null check beforecurrentis used as the parking key.AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-13T23:06:08Z.
Two things before this can go in:
TimedWait.hpp:34: please useYieldProcessor(), asTimedWait.cppalready does, instead of__builtin_ia32_pause().GuestMutexContention.cpp:162-164asserts on wall-clock latency and processor share, which will fail at random on loaded CI runners. Please keep those numbers in--reportand leave the test to the checks that don't depend on timing.Imported from boykopovar/AnyPS5. Originally posted by Dripl1x on 2026-10-13T23:20:08Z.
@oneandonlydean both are done in
99eaf7310.TimedWait.hpp: the spin loop now callsTimedWait::SpinPause(), whichTimedWait.cppdefines asYieldProcessor(). The header cannot nameYieldProcessoritself without including<windows.h>into every file that includes it (Pthread.hpp,VideoOutDriver.hppandAvPlayer.hppamong them), so the call sits behind a function in the file that already uses it.__builtin_ia32_pauseis gone.GuestMutexContention.cpp: the three assertions on latency and processor share are removed. I also removed the two other places that depended on timing: the upper bound on the 20 ms timed-lock timeout, and the fixed 0.3 s the four contenders ran for; each of them now makes 20,000 acquisitions. The measurements run only with--report.What the test still checks: a blocked waiter of a plain and of a timed lock gets the mutex its holder releases, a timed lock of a held mutex times out and not before its deadline, the four contenders exclude one another and each finishes its acquisitions, and 400 handoffs after which the waiter destroys the mutex, for error-checking and recursive mutexes.
One consequence: without a timing assertion the test passes on
maintoo, so the failing control the description reported no longer exists. The description says so now; what separates the two builds is the--reportoutput.Results at this head, same machines as before (Windows 11, Ryzen 7 5800H, WinLibs MinGW-w64 GCC 15.2.0 r7; Ubuntu 26.04 under WSL, GCC 15.2.0; Release). The DOOM traces were not captured again: the production change since
2710e219ais the pause call alone.guest_mutex_contention: 20 of 20 repeated runs pass.--report, three runs: handoff median 20 to 23 us, 90th percentile 23 to 33 us, slowest 25 to 105 us; a blocked waiter uses 0.0 % of a processor; four contenders make 751,000 to 793,000 acquisitions per second. The table in the description has these numbers; themaincolumn is unchanged.--report, three runs at
99eaf7310guest_mutex_contention, 20 repeated runs
Windows, 57 tests
Linux, 13 tests
AI-assisted (Claude Code).