fix(libc): let the macOS write watch fault handler go first #7695
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7695"
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 jotitadev on 2026-10-19T00:53:03Z.
What
write_watch_threadstimed out on thewrite-watch (macos-15-intel)job of run 39151604364: the first of the three repetitions took 6.75 s, the second hit the 30 s timeout. That run was for #7622, which does not touch the write watch. Over the last ~400 Build runs onmain, the intel job failed once in 71 runs and the test took 1-2 s typically, with a tail up to 11.7 s.The macOS backend serializes the fault handler and collections with a ticket lock whose waiters spin on
sched_yield. When the thread holding the next turn is not running, every other thread waits for it even though the lock is free. With more runnable threads than cores (4 writers plus the collector on a 4-vCPU runner), each handoff can wait a full scheduling round, and the test, where nearly every write faults because the collector keeps re-protecting the pages, goes from under a second to tens of seconds.The lock is now a
pthread_mutex_twithPTHREAD_MUTEX_POLICY_FIRSTFIT_NP(the macOS default since 10.14, set explicitly soPTHREAD_MUTEX_DEFAULT_POLICYcannot change it): a waiter blocks in the kernel, and the mutex goes to whichever thread takes it first instead of to a fixed turn.A plain first-fit mutex would bring back what the ticket lock was added for in #1686: a thread collecting in a loop kept winning a test-and-set lock and writers waiting in the handler starved (119 s for the four-thread test under Rosetta). So the handler goes first: it counts itself before it takes the mutex, and
Collectand the other entry points, once they hold the mutex, wait on a condition variable until no fault is waiting or running. Faults cannot starve them in turn, because a fault lifts the protection of a page and only those entry points protect pages again, so the faults that can arrive between two of them are bounded by the pages they protected. The handler takes the same lock from the same place as before.Tested
macOS 27.2 arm64 (Apple M2 Max), standalone
core/libs/tests/write_watchproject, Release. Runs arewrite_watch_tests threadsundertaskpolicy -b(efficiency cores only, 4 on this chip) with twotaskpolicy -b yesbusy loops, binaries alternated run by run. The time is measured inside the process (static constructor to destructor, watchdog at 30 s), because at background priority on a loaded machine starting the process alone took up to 42 s for a no-op shell script; that is what the occasional wall-clock timeouts of an earlier mutex-only version of this change were.mainreached 30 s in 24 of 40 runs; this change 0 of 40, slowest 5.4 s; the mutex without the handler going first 0 of 40, slowest 2.3 s.main64-123 ms; a test-and-set lock 88-143 ms, longest wait of a fault for the lock up to 10 ms; the mutex alone 103-230 ms, with the collector taking the lock 30,000-467,000 times for about 14,000 faults and faults waiting up to 8 ms; this change 30-49 ms, 650-5,800 collections, faults waiting at most 260 us.taskpolicy -bwith two busy loops:main10 of 10 at 30 s; test-and-set 2 of 10 at 30 s, the rest 3.7-23 s; the mutex alone 0.3-12.3 s; this change 8-277 ms.write_watch_*tests pass withctest --repeat until-fail:5.-Wall -Wextra -Werroris clean.python3 tools/check_conventions.py --base origin/mainpasses.tools/pr_overlap.pyreports only #7192, which addsHandleFaultto the same file; it callsrecord, so it goes through the handler's side of the lock.x86_64 under Rosetta 2, same machine, rebased on
maina373e85e5:write_watch_*tests pass withctest --repeat until-fail:5(standalone project with-DCMAKE_OSX_ARCHITECTURES=x86_64,-Wall -Wextra -Werrorclean).main338-392 ms, longest wait of a fault for the lock up to 1.3 ms; a test-and-set lock 786-1,171 ms, up to 10.7 ms; this change 234-252 ms, at most 120 us.mainunder Rosetta. At normal priority, also with three copies at once, both pass in 0.2-0.45 s. Undertaskpolicy -bboth reach 30 s, while the same binaries built for arm64 at the same load givemain30 s and this change 0.2-1.7 s: under Rosetta the handler's wait for the lock stays near zero in both and they manage about 20 faults per second, so the cost of each emulated fault sets the time there, not the lock. The failure this addresses was on the native intel runner.Not run: Linux and Windows (the change is under
__APPLE__).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-20T00:33:04Z.
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/libc/src/GuestWriteWatch.cppImported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-20T00:41:27Z.
Read the full diff. The fault handler counting itself before taking the mutex, with
Lockwaiting on the condvar until no fault is pending, keeps the property the ticket lock was added for (#1686) without thesched_yieldconvoy.write-watch (macos-15)and(macos-15-intel)are green on this head, as are conventions and relinker; merged.