fix(libc): let the macOS write watch fault handler go first #7695

Merged
jotitadev merged 1 commit from upstream-pr/7695 into main 2026-10-20 00:38:47 +00:00
jotitadev commented 2026-10-19 00:53:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-19T00:53:03Z.


What

write_watch_threads timed out on the write-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 on main, 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_t with PTHREAD_MUTEX_POLICY_FIRSTFIT_NP (the macOS default since 10.14, set explicitly so PTHREAD_MUTEX_DEFAULT_POLICY cannot 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 Collect and 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_watch project, Release. Runs are write_watch_tests threads under taskpolicy -b (efficiency cores only, 4 on this chip) with two taskpolicy -b yes busy 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.

  • 40 rounds, load average 14-37: main reached 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.
  • 50 rounds, load average 12-22: this change 0 of 50, slowest 0.82 s (the CMake-built binary, timed from outside: 0 of 50 over 30 s, slowest 5.5 s); the mutex alone, slowest 13.3 s.
  • Starvation, with a local copy of the test whose collector loops without yielding, and counters in the lock, 10 runs each:
    • normal priority: main 64-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.
    • under taskpolicy -b with two busy loops: main 10 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.
  • All ten write_watch_* tests pass with ctest --repeat until-fail:5. -Wall -Wextra -Werror is clean.
  • python3 tools/check_conventions.py --base origin/main passes. tools/pr_overlap.py reports only #7192, which adds HandleFault to the same file; it calls record, so it goes through the handler's side of the lock.

x86_64 under Rosetta 2, same machine, rebased on main a373e85e5:

  • All ten write_watch_* tests pass with ctest --repeat until-fail:5 (standalone project with -DCMAKE_OSX_ARCHITECTURES=x86_64, -Wall -Wextra -Werror clean).
  • Starvation, the same local copy with the collector looping without yielding, normal priority, 5 runs each: main 338-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.
  • I could not reproduce the timeout of main under Rosetta. At normal priority, also with three copies at once, both pass in 0.2-0.45 s. Under taskpolicy -b both reach 30 s, while the same binaries built for arm64 at the same load give main 30 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

  • 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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4914308722 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7695). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-19T00:53:03Z. --- ### What `write_watch_threads` timed out on the `write-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 on `main`, 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_t` with `PTHREAD_MUTEX_POLICY_FIRSTFIT_NP` (the macOS default since 10.14, set explicitly so `PTHREAD_MUTEX_DEFAULT_POLICY` cannot 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 `Collect` and 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_watch` project, Release. Runs are `write_watch_tests threads` under `taskpolicy -b` (efficiency cores only, 4 on this chip) with two `taskpolicy -b yes` busy 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. - 40 rounds, load average 14-37: `main` reached 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. - 50 rounds, load average 12-22: this change 0 of 50, slowest 0.82 s (the CMake-built binary, timed from outside: 0 of 50 over 30 s, slowest 5.5 s); the mutex alone, slowest 13.3 s. - Starvation, with a local copy of the test whose collector loops without yielding, and counters in the lock, 10 runs each: - normal priority: `main` 64-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. - under `taskpolicy -b` with two busy loops: `main` 10 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. - All ten `write_watch_*` tests pass with `ctest --repeat until-fail:5`. `-Wall -Wextra -Werror` is clean. - `python3 tools/check_conventions.py --base origin/main` passes. `tools/pr_overlap.py` reports only #7192, which adds `HandleFault` to the same file; it calls `record`, so it goes through the handler's side of the lock. x86_64 under Rosetta 2, same machine, rebased on `main` a373e85e5: - All ten `write_watch_*` tests pass with `ctest --repeat until-fail:5` (standalone project with `-DCMAKE_OSX_ARCHITECTURES=x86_64`, `-Wall -Wextra -Werror` clean). - Starvation, the same local copy with the collector looping without yielding, normal priority, 5 runs each: `main` 338-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. - I could not reproduce the timeout of `main` under Rosetta. At normal priority, also with three copies at once, both pass in 0.2-0.45 s. Under `taskpolicy -b` both reach 30 s, while the same binaries built for arm64 at the same load give `main` 30 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 - [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] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-20 00:33:04 +00:00 (Migrated from github.com)

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.

Pull request Shared files not compared
PR 7192 core/libs/prx/libc/src/GuestWriteWatch.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6280473341 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7695#issuecomment-6280473341). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-20T00:33:04Z. --- <!-- 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 7192](https://www.github.com/boykopovar/AnyPS5/pull/7192) | `core/libs/prx/libc/src/GuestWriteWatch.cpp` |
adriadam10 commented 2026-10-20 00:41:27 +00:00 (Migrated from github.com)

Imported 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 Lock waiting on the condvar until no fault is pending, keeps the property the ticket lock was added for (#1686) without the sched_yield convoy. write-watch (macos-15) and (macos-15-intel) are green on this head, as are conventions and relinker; merged.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6280583095 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7695#issuecomment-6280583095). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-20T00:41:27Z. --- Read the full diff. The fault handler counting itself before taking the mutex, with `Lock` waiting on the condvar until no fault is pending, keeps the property the ticket lock was added for (#1686) without the `sched_yield` convoy. `write-watch (macos-15)` and `(macos-15-intel)` are green on this head, as are conventions and relinker; merged.
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!7695
No description provided.