WIP: perf(os): mutex_lock's wait for a held mutex is out of line and cold #1953

Draft
siantm wants to merge 4 commits from siantm/Longshot:perf/mutex-lock-wait-cold into main
Contributor

Summary

mutex_lock (host/nn_os_sync.c), behind the game's nn::os mutex locks, had the uncontended lock (the owner check,
one compare-exchange) and the whole wait after a failed immediate acquire (the spin, the sleep, HOS_MUTEX_TRACE's log)
in one function, so the short path sat in a function shaped for the long one.

The spin and the sleep move into mutex_lock_wait, marked noinline and cold; mutex_lock keeps the recursion check
and the compare-exchange and calls it whenever that immediate acquire fails: another thread holds the mutex, or it is
reserved for a woken condition-variable waiter while its owner is already NULL. The aim is that the uncontended lock can
compile to a small leaf that saves no registers and sets up no frame for the wait (not shown yet: Still to test). The
take, the waiter registration and retry, and the unlock's wake are unchanged.

The moved wait carried three races in its diagnostics, fixed here in their own commits (d3e02c70, the failing test
first, then 18e90465), inside mutex_lock_wait:

  • HOS_MUTEX_TRACE was cached in a plain static two first waits could write at once, and parsed with atoi. It is
    read through an atomic and kept in a local for the wait; only a number of milliseconds from 1 to 2147483647 turns it
    on, anything else is off (-1 used to log every wait).
  • The long-wait log and the Tracy zone named the holder through its HosThread after the wait, when it could have
    ended and been freed. The name is copied with the wait counted in mwait and m->lk held, where the holder's unlock
    waits for m->lk; the 10 s log reads it there too.
  • The 10 s log printed m->count, which the owner changes without m->lk: dropped.

Left as they are on main by this PR, each fixed in a pull request of its own: spin_begin's lazily cached
HOS_SPIN_ALWAYS (#2012, merged into main; this branch merges with it without a conflict); hos_chain_on, a
volatile flag the recorder thread writes (#2019); hos_thread_set_name writing a thread's name while others read it
(#2020, no conflict with this branch). #2019 and this PR both change the line of mutex_lock_wait that reads the flag
(const int chain = hos_chain_on;, moved here): whichever lands second is rebased, the line then reading
__atomic_load_n(&hos_chain_on, __ATOMIC_RELAXED).

From cafb4100 on exp/x86-asm-sync (its other half, each virtual core's owner slot on a cache line of its own, needs
lock-free slots main does not have, and is not here). scripts/profiling/cpu_functions.json describes both
functions for the CPU report.

Read first: mutex_lock and mutex_lock_wait in host/nn_os_sync.c; tests/test_cv_handoff_threads.py, which cuts
the new function too and runs HOS_MUTEX_TRACE's cases.

Testing

Tested: Windows 11, i7-9700, RTX 4060 (NVIDIA 610.88), llvm-mingw clang 23.1.5; game-free checks only. This
change has not run in the game on this PC yet (Still to test). Each check ran in a hidden console of its own, through
the PC's job queue, on this branch rebased onto main @ 6e1521a7 (head 8443bba1):

  • python -I -B tests/test_cv_handoff_threads.py (the mutexes and condition variables on real threads, the source as
    it is; now with HOS_MUTEX_TRACE unset, 0, 1, -1, 1x, 2147483648 and 99999999999 in fresh processes, the variable
    flipped after the first wait, and a holder that has ended before the waiter logs; the compiler from --cc, CC or
    LONGSHOT_MINGW, no --cc given): passes at 8443bba1; at the test commit d3e02c70 it fails (the log read the ended
    holder's record).
  • python -I -B tests/run_all.py --strict --changed origin/main @ 8443bba1 (8 at once): 32 passed, 7 failed, 3 skipped.
    4 of the 7 (test_crash_dialog, test_dev_ab, test_frame_overlap, test_linux_pressure; picked because
    gpu/vk/switches.def changed) fail the same way on main @ 6e1521a7 in the queue; the skips are Linux-only checks.
    The other three (test_present_queue_default, test_target_reuse_memory, test_vk_occlusion_queries) compiled
    nothing in that run nor in the run on main (no Vulkan headers on the job's path: "not found: vulkan_headers
    (--strict)"). Rerun with the headers (job fb3-vk-2, RX_CPM set) at the head 8443bba1 and on main @ 6e1521a7, each
    fails at both with the same first error: test_present_queue_default "srccut: '{TAB_DEBUG, "Present queue",
    &opts.present_queue, ...' is not in gpu/vk/vk_menu.cpp"; test_target_reuse_memory 2 of its 8 tests
    ("'vmaGetAllocationInfo2' unexpectedly found" in the VMA source, and its fixture's "use of undeclared identifier
    'destroy_image_later'"); test_vk_occlusion_queries "the ring fixture did not compile" ("no member named 'policy'").
    None fails on this branch alone.
  • host/nn_os_sync.c compiled (syntax only) with the host build's own flags, with and without TRACY_ENABLE, at
    93f9b35a (this PR's first commit) and 8443bba1: no errors.

Earlier, the same change on main @ 8859673e (1e6a4721): tests/test_cv_handoff_threads.py passed, and the
micro-benchmark in Measurements.

Still to test:

  • Windows 11, this PC (i7-9700, RTX 4060 (NVIDIA 610.88)): the generated mutex_lock in the production host build
    against main's (by hand)
  • Windows 11, this PC: brief contention that ends without a sleep (a failed first take, the spin succeeding), this PR
    against main, a micro-benchmark through the fixture (by hand)
  • Windows 11, this PC: Castle Town's market, 60 s, this PR and main interleaved and warmed, no switches set, average
    fps, 1 % lows, median and p99 frame times, expecting them within the runs' spread or better (by hand, queued)
  • Linux: build the host and run python3 -I -B tests/test_cv_handoff_threads.py, expecting it to pass (by hand)
  • Linux and Windows with other CPUs (an AMD CPU, fewer cores), and the Steam Deck: Castle Town's market for two
    minutes, this PR against main, expecting the same frame times as main or better and no stall (@Beanstalk play
    Castle Town's market for two minutes on PCs with AMD CPUs and on the Steam Deck, this PR against main, comparing frame
    times)

Measurements

Verdict: micro-benchmark only, not measured in play yet: an uncontended lock and unlock took 18.55 ns on main and
18.03 ns with this change, within the rounds' spread (no gain shown).

Setup: Windows 11, i7-9700, llvm-mingw clang 23.1.5, -O2 -march=native as the host is built; an uncontended
mutex_lock + mutex_unlock through tests/test_cv_handoff_threads.py's own fixture (each checkout's nn_os_sync.c
cut as that test cuts it); 21 interleaved rounds, the median and the rounds' range, in one job of the PC's job queue
(microbench.py mutex). Measured on main @ 8859673e with this change as it was there (1e6a4721); the rebase onto
fc4a6769 changed none of its lines.

Before: main @ 8859673e After: this PR @ 1e6a4721 (the same change on 8859673e)
uncontended lock + unlock, ns (median of 21 rounds, range) 18.55 (18.14 to 19.68) 18.03 (17.26 to 18.98)
uncontended mutex_lock + mutex_unlock    main           median    18.55 ns  (rounds 18.14 to 19.68)
uncontended mutex_lock + mutex_unlock    PR             median    18.03 ns  (rounds 17.26 to 18.98)

The figures are from before the rebase and the diagnostics' fixes, which change only mutex_lock_wait (the lock that
waits), not the uncontended lock measured. The cost of the failed-first-take path is not measured (Still to test).

Risks

The wait is the same code in another function, so a mistake would show as a lock that never waits or never wakes: a
stall, which tests/test_cv_handoff_threads.py and the other mutex checks cover. A lock whose immediate acquire fails
pays one more call, even when the spin then succeeds at once. HOS_MUTEX_TRACE (a diagnostic) is now off for a value
that is not a number of milliseconds from 1 to 2147483647. No switch: a revert is enough. Players notice nothing (no
saves, longshot_options.ini or caches change).

Checklist

Ready for review

  • One topic, on current main; the title is a Conventional Commit.
  • Tested on my own PC, in the game for a change the game runs, as Testing says.
  • mise run test passes (any failure that also fails on main is named).
  • Measured against main, or Measurements says why there is nothing to measure.
  • No game files, build output or hand-edited generated files; third_party/ untouched; nothing GPL-incompatible.

When it applies

  • A check under tests/ that reads changed source is updated, not weakened.
  • Host code builds on Windows (llvm-mingw) and Linux, with UTF-8 paths.
  • Docs updated.
  • AI assistance recorded with Assisted-by: / Co-Authored-By: trailers.

Ready to merge

  • Everything in Still to test has run, its results in Tested.
  • Tier 1 hardware it can affect is tested (docs/dev/standards.md, "Hardware tiers").
  • Approved in review, every comment answered.
  • CI passes on the head; on current main, measured again or a comment says why the numbers still hold.
## Summary `mutex_lock` (`host/nn_os_sync.c`), behind the game's `nn::os` mutex locks, had the uncontended lock (the owner check, one compare-exchange) and the whole wait after a failed immediate acquire (the spin, the sleep, `HOS_MUTEX_TRACE`'s log) in one function, so the short path sat in a function shaped for the long one. The spin and the sleep move into `mutex_lock_wait`, marked `noinline` and `cold`; `mutex_lock` keeps the recursion check and the compare-exchange and calls it whenever that immediate acquire fails: another thread holds the mutex, or it is reserved for a woken condition-variable waiter while its owner is already NULL. The aim is that the uncontended lock can compile to a small leaf that saves no registers and sets up no frame for the wait (not shown yet: Still to test). The take, the waiter registration and retry, and the unlock's wake are unchanged. The moved wait carried three races in its diagnostics, fixed here in their own commits (d3e02c70, the failing test first, then 18e90465), inside `mutex_lock_wait`: - `HOS_MUTEX_TRACE` was cached in a plain static two first waits could write at once, and parsed with `atoi`. It is read through an atomic and kept in a local for the wait; only a number of milliseconds from 1 to 2147483647 turns it on, anything else is off (`-1` used to log every wait). - The long-wait log and the Tracy zone named the holder through its `HosThread` after the wait, when it could have ended and been freed. The name is copied with the wait counted in `mwait` and `m->lk` held, where the holder's unlock waits for `m->lk`; the 10 s log reads it there too. - The 10 s log printed `m->count`, which the owner changes without `m->lk`: dropped. Left as they are on `main` by this PR, each fixed in a pull request of its own: `spin_begin`'s lazily cached `HOS_SPIN_ALWAYS` (#2012, merged into `main`; this branch merges with it without a conflict); `hos_chain_on`, a `volatile` flag the recorder thread writes (#2019); `hos_thread_set_name` writing a thread's name while others read it (#2020, no conflict with this branch). #2019 and this PR both change the line of `mutex_lock_wait` that reads the flag (`const int chain = hos_chain_on;`, moved here): whichever lands second is rebased, the line then reading `__atomic_load_n(&hos_chain_on, __ATOMIC_RELAXED)`. From cafb4100 on `exp/x86-asm-sync` (its other half, each virtual core's owner slot on a cache line of its own, needs lock-free slots `main` does not have, and is not here). `scripts/profiling/cpu_functions.json` describes both functions for the CPU report. Read first: `mutex_lock` and `mutex_lock_wait` in `host/nn_os_sync.c`; `tests/test_cv_handoff_threads.py`, which cuts the new function too and runs `HOS_MUTEX_TRACE`'s cases. ## Testing **Tested:** Windows 11, i7-9700, RTX 4060 (NVIDIA 610.88), llvm-mingw clang 23.1.5; game-free checks only. This change has not run in the game on this PC yet (Still to test). Each check ran in a hidden console of its own, through the PC's job queue, on this branch rebased onto `main` @ 6e1521a7 (head 8443bba1): - `python -I -B tests/test_cv_handoff_threads.py` (the mutexes and condition variables on real threads, the source as it is; now with `HOS_MUTEX_TRACE` unset, 0, 1, -1, 1x, 2147483648 and 99999999999 in fresh processes, the variable flipped after the first wait, and a holder that has ended before the waiter logs; the compiler from `--cc`, `CC` or `LONGSHOT_MINGW`, no `--cc` given): passes at 8443bba1; at the test commit d3e02c70 it fails (the log read the ended holder's record). - `python -I -B tests/run_all.py --strict --changed origin/main` @ 8443bba1 (8 at once): 32 passed, 7 failed, 3 skipped. 4 of the 7 (`test_crash_dialog`, `test_dev_ab`, `test_frame_overlap`, `test_linux_pressure`; picked because `gpu/vk/switches.def` changed) fail the same way on `main` @ 6e1521a7 in the queue; the skips are Linux-only checks. The other three (`test_present_queue_default`, `test_target_reuse_memory`, `test_vk_occlusion_queries`) compiled nothing in that run nor in the run on `main` (no Vulkan headers on the job's path: "not found: vulkan_headers (--strict)"). Rerun with the headers (job fb3-vk-2, `RX_CPM` set) at the head 8443bba1 and on `main` @ 6e1521a7, each fails at both with the same first error: `test_present_queue_default` "srccut: '{TAB_DEBUG, "Present queue", &opts.present_queue, ...' is not in gpu/vk/vk_menu.cpp"; `test_target_reuse_memory` 2 of its 8 tests ("'vmaGetAllocationInfo2' unexpectedly found" in the VMA source, and its fixture's "use of undeclared identifier 'destroy_image_later'"); `test_vk_occlusion_queries` "the ring fixture did not compile" ("no member named 'policy'"). None fails on this branch alone. - `host/nn_os_sync.c` compiled (syntax only) with the host build's own flags, with and without `TRACY_ENABLE`, at 93f9b35a (this PR's first commit) and 8443bba1: no errors. Earlier, the same change on `main` @ 8859673e (1e6a4721): `tests/test_cv_handoff_threads.py` passed, and the micro-benchmark in Measurements. **Still to test:** - Windows 11, this PC (i7-9700, RTX 4060 (NVIDIA 610.88)): the generated `mutex_lock` in the production host build against `main`'s (by hand) - Windows 11, this PC: brief contention that ends without a sleep (a failed first take, the spin succeeding), this PR against `main`, a micro-benchmark through the fixture (by hand) - Windows 11, this PC: Castle Town's market, 60 s, this PR and `main` interleaved and warmed, no switches set, average fps, 1 % lows, median and p99 frame times, expecting them within the runs' spread or better (by hand, queued) - Linux: build the host and run `python3 -I -B tests/test_cv_handoff_threads.py`, expecting it to pass (by hand) - Linux and Windows with other CPUs (an AMD CPU, fewer cores), and the Steam Deck: Castle Town's market for two minutes, this PR against `main`, expecting the same frame times as `main` or better and no stall (@Beanstalk play Castle Town's market for two minutes on PCs with AMD CPUs and on the Steam Deck, this PR against main, comparing frame times) ## Measurements **Verdict:** micro-benchmark only, not measured in play yet: an uncontended lock and unlock took 18.55 ns on `main` and 18.03 ns with this change, within the rounds' spread (no gain shown). Setup: Windows 11, i7-9700, llvm-mingw clang 23.1.5, `-O2 -march=native` as the host is built; an uncontended `mutex_lock` + `mutex_unlock` through `tests/test_cv_handoff_threads.py`'s own fixture (each checkout's `nn_os_sync.c` cut as that test cuts it); 21 interleaved rounds, the median and the rounds' range, in one job of the PC's job queue (`microbench.py mutex`). Measured on `main` @ 8859673e with this change as it was there (1e6a4721); the rebase onto fc4a6769 changed none of its lines. | | Before: main @ 8859673e | After: this PR @ 1e6a4721 (the same change on 8859673e) | |---|---|---| | uncontended lock + unlock, ns (median of 21 rounds, range) | 18.55 (18.14 to 19.68) | 18.03 (17.26 to 18.98) | ``` uncontended mutex_lock + mutex_unlock main median 18.55 ns (rounds 18.14 to 19.68) uncontended mutex_lock + mutex_unlock PR median 18.03 ns (rounds 17.26 to 18.98) ``` The figures are from before the rebase and the diagnostics' fixes, which change only `mutex_lock_wait` (the lock that waits), not the uncontended lock measured. The cost of the failed-first-take path is not measured (Still to test). ## Risks The wait is the same code in another function, so a mistake would show as a lock that never waits or never wakes: a stall, which `tests/test_cv_handoff_threads.py` and the other mutex checks cover. A lock whose immediate acquire fails pays one more call, even when the spin then succeeds at once. `HOS_MUTEX_TRACE` (a diagnostic) is now off for a value that is not a number of milliseconds from 1 to 2147483647. No switch: a revert is enough. Players notice nothing (no saves, `longshot_options.ini` or caches change). ## Checklist ### Ready for review - [x] One topic, on current `main`; the title is a Conventional Commit. - [ ] Tested on my own PC, in the game for a change the game runs, as Testing says. - [ ] `mise run test` passes (any failure that also fails on `main` is named). - [ ] Measured against `main`, or Measurements says why there is nothing to measure. - [x] No game files, build output or hand-edited generated files; `third_party/` untouched; nothing GPL-incompatible. ### When it applies - [x] A check under `tests/` that reads changed source is updated, not weakened. - [ ] Host code builds on Windows (llvm-mingw) and Linux, with UTF-8 paths. - [x] Docs updated. - [x] AI assistance recorded with `Assisted-by:` / `Co-Authored-By:` trailers. ### Ready to merge - [ ] Everything in Still to test has run, its results in Tested. - [ ] Tier 1 hardware it can affect is tested (docs/dev/standards.md, "Hardware tiers"). - [ ] Approved in review, every comment answered. - [ ] CI passes on the head; on current `main`, measured again or a comment says why the numbers still hold.
perf(os): mutex_lock's wait for a held mutex is out of line and cold
Some checks failed
PR description / Description follows the template (pull_request_target) Successful in 11s
Website / Website on Cloudflare (pull_request_target) Has been skipped
CI / Conventional Commit title (pull_request) Has been cancelled
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Has been cancelled
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Has been cancelled
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Has been cancelled
CI / Workflow lint (actionlint) (pull_request) Has been cancelled
CI / Website build and checks (pull_request) Has been cancelled
Guide screenshots / Guide images and local UI source review (pull_request) Has been cancelled
Third-party parts / As their notes say (pull_request) Has been cancelled
Third-party parts / The same as upstream (pull_request) Has been cancelled
95c0fd4152
mutex_lock's spin and sleep, taken only when another thread holds the mutex, are moved word for word into
mutex_lock_wait (noinline, cold), so the uncontended lock (owner check, one compare-exchange) is a small leaf that does
not save registers or set up a frame for the wait. tests/test_cv_handoff_threads.py cuts the new function too.

From cafb4100 on exp/x86-asm-sync (its other half, the virtual cores' owner slots on their own cache lines, needs the
lock-free slots main does not have).

Assisted-by: Claude Code
Member

Possibly related, for context:

  • issue #1504 On Linux the Windows lock shim sleeps at once on a busy lock and makes a system call on every wake, even when nobody waits — Related Linux lock performance investigation; targets different shim/lock path
Possibly related, for context: - issue #1504 On Linux the Windows lock shim sleeps at once on a busy lock and makes a system call on every wake, even when nobody waits — Related Linux lock performance investigation; targets different shim/lock path
BeanBot left a comment

Recommendation: Request changes. The split preserves the mutex protocol in source, but the fixture shows no reliable speedup and leaves the cost of brief contention unmeasured. The extracted wait also carries forward races and an unsafe owner pointer in its diagnostics.

Moves spinning, sleeping, and wait diagnostics out of the ordinary guest mutex lock. This gives the compiler a smaller fast path to lay out, at the cost of an out-of-line transfer whenever the first acquire fails. The existing threaded fixture compiles the extracted helper.

Does it help: Unproven. In 21 interleaved Windows rounds on the i7-9700 with llvm-mingw Clang 23.1.5, median uncontended lock plus unlock went from 18.55 ns on 8859673e to 18.03 ns on 1e6a4721. The ranges overlap (18.14–19.68 ns and 17.26–18.98 ns). The fixture uses a local thread lookup and a no-op spin. No in-game measurements or contended timings are reported for 95c0fd41 against 2e1f6c32.
Other systems: Direct LockMutex and condition-variable reacquisition without a reservation enter the changed path. The reserved reacquire path, TryLockMutex, and UnlockMutex retain their source logic. Short contention can affect frame pacing, audio, and other guest workers on both OSes. The pthread fixture models wake ordering, while Windows uses native SRW waits and Linux and Steam Deck use the futex-backed compatibility layer. GPU vendors share this CPU-side path. Tracy retains the zone label but records the helper as its source function. The CPU profiler still categorizes the helper as os/sync by object file. Saves, settings, caches, and renderer interfaces have no changes in the diff.
Switches: None added, changed, or removed. HOS_MUTEX_TRACE still defaults to 0 (off), which keeps optional trace logging off. Its existing switch-table row remains applicable.

Reviewed: 95c0fd4152 against 2e1f6c32d3: Read AGENTS.md, CONTRIBUTING.md, docs/dev/standards.md, the Forgejo PR template, and both changed files in full. No docs/review-checklist.md was present. Read the relevant host thread lifetime and wait code, Linux wait implementation, host build flags, Tracy and chain paths, source cutter, and test runner selection. Skimmed sibling checks, profiling metadata, and related recorded runs. No matching recorded run or tester comment was available. The earlier PR revisions named in Testing and Measurements were unavailable in the checkout. No translations or generated files changed.; nothing run (a code review).
Checked and fine:

  • The recursive owner check, reservation check, CAS ordering, waiter registration and retry, and unlock wake sequence are preserved.
  • The extraction adds no allocation, buffer, guest offset, file access, or CPU feature requirement. The attribute syntax already has precedent in the host and is supported by the Windows and Linux toolchains.
  • The fixture extracts the helper before its caller, clears inherited HOS_ switches, and retains both handoff modes and the existing failure controls.
  • The diff is one topic. The commit has an Assisted-by trailer, and the diff contains no game-derived artifacts, vendor SDKs, third-party or generated changes, or versioned API changes.

Blocking (to change before merge)

  1. host/nn_os_sync.c:635: The extracted trace path retains an unsynchronized lazy threshold. Two threads entering their first sleeping wait on different mutexes can read and write the same static trace_ms even with the switch unset. An oversized HOS_MUTEX_TRACE value also exceeds atoi's representable range. Parse with range checks and publish the result once with synchronization, or use atomic accesses and keep a local threshold for the whole wait.
  2. host/nn_os_sync.c:670: The moved diagnostic code retains an owner pointer without protecting its lifetime or name. An owner can unlock, exit, and be destroyed while the waiter is still blocked or reacquiring a virtual core. The trace then dereferences freed storage. Concurrent SetThreadName can also race with the Tracy and long-wait name reads. Capture the name by value through a lifetime- and rename-safe lookup, or log a stable owner identifier, in all three diagnostic paths.
  3. host/nn_os_sync.c:656: The moved ten-second log reads m->count under m->lk, while the guest mutex owner updates the count without taking m->lk. A recursive acquire or final unlock during the log gives a C data race. Omit the count from this diagnostic or make every access to the mutex count atomic.
  4. host/nn_os_sync.c:640: The extracted wait still reads the volatile hos_chain_on flag while the recorder thread starts or stops capture and a producer can clear it at capacity. Volatile gives neither synchronization nor publication of recs. Use atomic flag accesses throughout the recorder and at its call sites, with release publication and acquire reads before using the buffer.
  5. host/nn_os_sync.c:633: The spin moved into this helper still reaches spin_begin's plain lazy adaptive flag. Two x86-64 workers can first spin on separate mutexes at the same time and race initializing it. Use an atomic cached value or initialize the setting before workers start. The fixture replaces the spin, so it cannot exercise this path.
  6. tests/test_cv_handoff_threads.py:92: The fixture still forces gcc in the runner and defaults to gcc when invoked directly, ignoring CC. A documented Windows llvm-mingw setup can have Clang and winpthreads without gcc on PATH, so the check fails or is skipped before it covers the new helper. Honor --cc and CC, use the configured platform compiler as the fallback, and give the runner the same compiler selection.

Waiting on the author (a run, a measurement or a decision; no edit does it)

  • host/nn_os_sync.c:674: The production fast path still calls hos_self from a separate translation unit in the ordinary non-LTO host build. The fixture's local definition cannot establish the claimed leaf or frame shape. Compare the generated production callers on Windows and Linux, then show a gain on current base versus head in warmed, interleaved game runs with the same route and settings. Report average FPS, 1% lows, median and p99 frame time, and the cost of the targeted lock path.
  • host/nn_os_sync.c:676: A briefly held job-queue mutex now sends a waiter into cold code even when the retry succeeds without a sleep. That adds a taken transfer into a different part of the instruction stream on short handoffs, which the single-thread benchmark never enters. Measure failed-first-CAS acquisitions that finish without parking and compare frame-time tails on AMD and lower-core systems, including Steam Deck, as well as the Windows Intel system.
  • This host change opened before the required author game run. The reported older Windows check covered a source-cut fixture, and the strict changed suite reported one skip. Record the author's game run, Windows and Linux host builds, the full checks with the skip explained, and CI on the reviewed head. Record the outstanding Tier 1 play results before merge.

Suggestions (the author may take or leave)

  • Update the existing mutex_lock description in scripts/profiling/cpu_functions.json and add one for mutex_lock_wait so profiler reports explain the new symbol.
  • host/nn_os_sync.c:656: Holding m->lk does not protect count from the owner’s recursive acquire or unlock. Drop this diagnostic field or make all mutex count accesses atomic.
  • host/nn_os_sync.c:640: The recorder changes this volatile flag concurrently. Use atomic accesses and release/acquire publication for the recording buffer.

Testing: Read-only review. The author reports a passing threaded fixture and 11 passes, 0 failures, and 1 skip in the changed suite at a105a307. The fixture exercises sleeping on contention and handoff ordering, including a case that stalls if the helper call is removed. Its no-op spin leaves spin success and spin cost untested. The author reports no rerun at HEAD.
Overlaps and merge order: None found in the available history or review discussion. The description identifies a separate experimental virtual-core owner-slot change and gives no merge dependency.


Automated code review by BeanBot (gpt-6.2-sol), on the pull request's opening. Nothing was run; a maintainer decides.

**Recommendation: Request changes.** The split preserves the mutex protocol in source, but the fixture shows no reliable speedup and leaves the cost of brief contention unmeasured. The extracted wait also carries forward races and an unsafe owner pointer in its diagnostics. Moves spinning, sleeping, and wait diagnostics out of the ordinary guest mutex lock. This gives the compiler a smaller fast path to lay out, at the cost of an out-of-line transfer whenever the first acquire fails. The existing threaded fixture compiles the extracted helper. **Does it help:** Unproven. In 21 interleaved Windows rounds on the i7-9700 with llvm-mingw Clang 23.1.5, median uncontended lock plus unlock went from 18.55 ns on 8859673e to 18.03 ns on 1e6a4721. The ranges overlap (18.14–19.68 ns and 17.26–18.98 ns). The fixture uses a local thread lookup and a no-op spin. No in-game measurements or contended timings are reported for 95c0fd41 against 2e1f6c32. **Other systems:** Direct LockMutex and condition-variable reacquisition without a reservation enter the changed path. The reserved reacquire path, TryLockMutex, and UnlockMutex retain their source logic. Short contention can affect frame pacing, audio, and other guest workers on both OSes. The pthread fixture models wake ordering, while Windows uses native SRW waits and Linux and Steam Deck use the futex-backed compatibility layer. GPU vendors share this CPU-side path. Tracy retains the zone label but records the helper as its source function. The CPU profiler still categorizes the helper as os/sync by object file. Saves, settings, caches, and renderer interfaces have no changes in the diff. **Switches:** None added, changed, or removed. HOS_MUTEX_TRACE still defaults to 0 (off), which keeps optional trace logging off. Its existing switch-table row remains applicable. **Reviewed:** 95c0fd41526b against 2e1f6c32d30e: Read AGENTS.md, CONTRIBUTING.md, docs/dev/standards.md, the Forgejo PR template, and both changed files in full. No docs/review-checklist.md was present. Read the relevant host thread lifetime and wait code, Linux wait implementation, host build flags, Tracy and chain paths, source cutter, and test runner selection. Skimmed sibling checks, profiling metadata, and related recorded runs. No matching recorded run or tester comment was available. The earlier PR revisions named in Testing and Measurements were unavailable in the checkout. No translations or generated files changed.; nothing run (a code review). **Checked and fine:** - The recursive owner check, reservation check, CAS ordering, waiter registration and retry, and unlock wake sequence are preserved. - The extraction adds no allocation, buffer, guest offset, file access, or CPU feature requirement. The attribute syntax already has precedent in the host and is supported by the Windows and Linux toolchains. - The fixture extracts the helper before its caller, clears inherited HOS_ switches, and retains both handoff modes and the existing failure controls. - The diff is one topic. The commit has an Assisted-by trailer, and the diff contains no game-derived artifacts, vendor SDKs, third-party or generated changes, or versioned API changes. **Blocking** (to change before merge) 1. host/nn_os_sync.c:635: The extracted trace path retains an unsynchronized lazy threshold. Two threads entering their first sleeping wait on different mutexes can read and write the same static trace_ms even with the switch unset. An oversized HOS_MUTEX_TRACE value also exceeds atoi's representable range. Parse with range checks and publish the result once with synchronization, or use atomic accesses and keep a local threshold for the whole wait. 2. host/nn_os_sync.c:670: The moved diagnostic code retains an owner pointer without protecting its lifetime or name. An owner can unlock, exit, and be destroyed while the waiter is still blocked or reacquiring a virtual core. The trace then dereferences freed storage. Concurrent SetThreadName can also race with the Tracy and long-wait name reads. Capture the name by value through a lifetime- and rename-safe lookup, or log a stable owner identifier, in all three diagnostic paths. 3. host/nn_os_sync.c:656: The moved ten-second log reads m->count under m->lk, while the guest mutex owner updates the count without taking m->lk. A recursive acquire or final unlock during the log gives a C data race. Omit the count from this diagnostic or make every access to the mutex count atomic. 4. host/nn_os_sync.c:640: The extracted wait still reads the volatile hos_chain_on flag while the recorder thread starts or stops capture and a producer can clear it at capacity. Volatile gives neither synchronization nor publication of recs. Use atomic flag accesses throughout the recorder and at its call sites, with release publication and acquire reads before using the buffer. 5. host/nn_os_sync.c:633: The spin moved into this helper still reaches spin_begin's plain lazy adaptive flag. Two x86-64 workers can first spin on separate mutexes at the same time and race initializing it. Use an atomic cached value or initialize the setting before workers start. The fixture replaces the spin, so it cannot exercise this path. 6. tests/test_cv_handoff_threads.py:92: The fixture still forces gcc in the runner and defaults to gcc when invoked directly, ignoring CC. A documented Windows llvm-mingw setup can have Clang and winpthreads without gcc on PATH, so the check fails or is skipped before it covers the new helper. Honor --cc and CC, use the configured platform compiler as the fallback, and give the runner the same compiler selection. **Waiting on the author** (a run, a measurement or a decision; no edit does it) - host/nn_os_sync.c:674: The production fast path still calls hos_self from a separate translation unit in the ordinary non-LTO host build. The fixture's local definition cannot establish the claimed leaf or frame shape. Compare the generated production callers on Windows and Linux, then show a gain on current base versus head in warmed, interleaved game runs with the same route and settings. Report average FPS, 1% lows, median and p99 frame time, and the cost of the targeted lock path. - host/nn_os_sync.c:676: A briefly held job-queue mutex now sends a waiter into cold code even when the retry succeeds without a sleep. That adds a taken transfer into a different part of the instruction stream on short handoffs, which the single-thread benchmark never enters. Measure failed-first-CAS acquisitions that finish without parking and compare frame-time tails on AMD and lower-core systems, including Steam Deck, as well as the Windows Intel system. - This host change opened before the required author game run. The reported older Windows check covered a source-cut fixture, and the strict changed suite reported one skip. Record the author's game run, Windows and Linux host builds, the full checks with the skip explained, and CI on the reviewed head. Record the outstanding Tier 1 play results before merge. **Suggestions** (the author may take or leave) - Update the existing mutex_lock description in scripts/profiling/cpu_functions.json and add one for mutex_lock_wait so profiler reports explain the new symbol. - `host/nn_os_sync.c:656`: Holding m->lk does not protect count from the owner’s recursive acquire or unlock. Drop this diagnostic field or make all mutex count accesses atomic. - `host/nn_os_sync.c:640`: The recorder changes this volatile flag concurrently. Use atomic accesses and release/acquire publication for the recording buffer. **Testing:** Read-only review. The author reports a passing threaded fixture and 11 passes, 0 failures, and 1 skip in the changed suite at a105a307. The fixture exercises sleeping on contention and handoff ordering, including a case that stalls if the helper call is removed. Its no-op spin leaves spin success and spin cost untested. The author reports no rerun at HEAD. **Overlaps and merge order:** None found in the available history or review discussion. The description identifies a separate experimental virtual-core owner-slot change and gives no merge dependency. --- <sub>Automated code review by BeanBot (gpt-6.2-sol), on the pull request's opening. Nothing was run; a maintainer decides.</sub>
@ -634,1 +630,4 @@
/* mutex_lock's way when another thread holds the mutex: its spin, then its sleep. Out of line and cold, so the uncontended
lock below does not save registers and set up a stack frame for this part */
static __attribute__((noinline, cold)) void mutex_lock_wait(Obj *m, HosThread *self, uint64_t guest, Ctx *c) {
spin_until_free(m);
Member

Two first-time spinners can race initializing spin_begin’s adaptive flag. Initialize before workers start or cache it atomically.

Two first-time spinners can race initializing spin_begin’s adaptive flag. Initialize before workers start or cache it atomically.
@ -634,3 +632,4 @@
static __attribute__((noinline, cold)) void mutex_lock_wait(Obj *m, HosThread *self, uint64_t guest, Ctx *c) {
spin_until_free(m);
if (mutex_take(m, self)) { m->count = 1; return; }
/* HOS_MUTEX_TRACE=<ms> (diagnostic): waits longer than that are logged with the thread that held the mutex */
Member

Concurrent first waits race initializing trace_ms. Use a race-free cached threshold and range-check HOS_MUTEX_TRACE before converting it.

Concurrent first waits race initializing trace_ms. Use a race-free cached threshold and range-check HOS_MUTEX_TRACE before converting it.
@ -671,6 +670,12 @@ static void mutex_lock(Obj *m, uint64_t guest, Ctx *c) {
(unsigned long long)(c ? c->x[30] : 0), hos_thread_name(holder));
Member

The original owner can unlock and be destroyed before this log dereferences it. Use a lifetime-safe name snapshot or stable identifier here and in the other owner diagnostics.

The original owner can unlock and be destroyed before this log dereferences it. Use a lifetime-safe name snapshot or stable identifier here and in the other owner diagnostics.
@ -631,3 +630,1 @@
HosThread *self = hos_self();
if (__atomic_load_n(&m->owner, __ATOMIC_RELAXED) == self) { m->count++; return; }
if (mutex_take(m, self)) { m->count = 1; return; }
/* mutex_lock's way when another thread holds the mutex: its spin, then its sleep. Out of line and cold, so the uncontended
Member

A reservation can send this path a mutex whose owner is already NULL. Describe the trigger as a failed immediate acquire.

A reservation can send this path a mutex whose owner is already NULL. Describe the trigger as a failed immediate acquire.
@ -672,2 +671,4 @@
}
}
static void mutex_lock(Obj *m, uint64_t guest, Ctx *c) {
HosThread *self = hos_self();
Member

Production still calls the separately compiled hos_self. Show the fast-path assembly from the actual Windows and Linux builds and measure the change in play.

Production still calls the separately compiled hos_self. Show the fast-path assembly from the actual Windows and Linux builds and measure the change in play.
@ -674,0 +673,4 @@
static void mutex_lock(Obj *m, uint64_t guest, Ctx *c) {
HosThread *self = hos_self();
if (__atomic_load_n(&m->owner, __ATOMIC_RELAXED) == self) { m->count++; return; }
if (mutex_take(m, self)) { m->count = 1; return; }
Member

The first failed acquire now enters cold code even when the spin succeeds immediately. Measure that handoff separately and check frame-time tails on the affected CPUs.

The first failed acquire now enters cold code even when the spin succeeds immediately. Measure that handoff separately and check frame-time tails on the affected CPUs.
@ -90,2 +90,3 @@
"static int mutex_take(Obj *m, HosThread *self) {", "static int mutex_take_owed(Obj *m, HosThread *self) {",
"static void mutex_give(Obj *m) {", "static void mutex_lock(Obj *m, uint64_t guest, Ctx *c) {",
"static void mutex_give(Obj *m) {",
"static __attribute__((noinline, cold)) void mutex_lock_wait(Obj *m, HosThread *self, uint64_t guest, Ctx *c) {",
Member

The runner forces gcc and the direct default ignores CC. Use the configured compiler so the fixture runs on a standard llvm-mingw setup.

The runner forces gcc and the direct default ignores CC. Use the configured compiler so the fixture runs on a standard llvm-mingw setup.
Member

The fixture marker can start at void mutex_lock_wait so an attribute spelling change does not break a behavior check.

The fixture marker can start at `void mutex_lock_wait` so an attribute spelling change does not break a behavior check.
futondestroyer left a comment

Recommendation: Wait for author-side gameplay and a measured benefit. The cold split preserves the ownership and waiter protocol. The numbers so far leave its benefit unresolved and omit the path that gets a new call.

mutex_lock still checks for recursion and attempts the same immediate acquisition. The new call runs after any immediate take fails, including an owner-free mutex reserved for a condition-variable waiter. The reserved reacquire still uses mutex_take_owed. The spin, registration, retry, core handoff and wake code are carried forward. The existing exact-head review calls out diagnostic lifetime and initialization defects. Those lines are inherited from the merge base and deserve their own fixes. This split adds no protection around them.

Does it help? Unproven. The author's Windows/i7-9700 fixture reports 18.55 ns before and 18.03 ns after for an uncontended lock/unlock over 21 rounds, with overlapping ranges. It substitutes hos_self and the spin helper, so it cannot establish the generated production fast path or the cost of the newly out-of-line failed-first-take path. Include brief contention that resolves without sleeping in the next comparison, with production assembly and the author's warmed, interleaved Market runs against exact main.

Reviewed. 95c0fd41526b05ade41ab265166a3c5c4ba55f06 against 2e1f6c32d30ec3582093743a968c8447bb435962. Reviewed both changed files, the mutex take/give and CV reacquire callers, and the threaded fixture. No new switch. The author reports a passing source-cut fixture and 11 passes with one skip on the earlier rebase. No confirmed exact-head gameplay or farm results. No PR code run in this review.

Overlap. The cold helper is already carried in the context mutex stack. If the cold split earns its own landing, rebase that stack to avoid landing the same step twice.

**Recommendation: Wait for author-side gameplay and a measured benefit.** The cold split preserves the ownership and waiter protocol. The numbers so far leave its benefit unresolved and omit the path that gets a new call. `mutex_lock` still checks for recursion and attempts the same immediate acquisition. The new call runs after any immediate take fails, including an owner-free mutex reserved for a condition-variable waiter. The reserved reacquire still uses `mutex_take_owed`. The spin, registration, retry, core handoff and wake code are carried forward. The [existing exact-head review](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1953#issuecomment-61957) calls out diagnostic lifetime and initialization defects. Those lines are inherited from the merge base and deserve their own fixes. This split adds no protection around them. **Does it help?** Unproven. The author's Windows/i7-9700 fixture reports 18.55 ns before and 18.03 ns after for an uncontended lock/unlock over 21 rounds, with overlapping ranges. It substitutes `hos_self` and the spin helper, so it cannot establish the generated production fast path or the cost of the newly out-of-line failed-first-take path. Include brief contention that resolves without sleeping in the next comparison, with production assembly and the author's warmed, interleaved Market runs against exact main. **Reviewed.** `95c0fd41526b05ade41ab265166a3c5c4ba55f06` against `2e1f6c32d30ec3582093743a968c8447bb435962`. Reviewed both changed files, the mutex take/give and CV reacquire callers, and the threaded fixture. No new switch. The author reports a passing source-cut fixture and 11 passes with one skip on the earlier rebase. No confirmed exact-head gameplay or farm results. No PR code run in this review. **Overlap.** The cold helper is already carried in the [context mutex stack](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1957). If the cold split earns its own landing, rebase that stack to avoid landing the same step twice.
siantm force-pushed perf/mutex-lock-wait-cold from 95c0fd4152
Some checks failed
PR description / Description follows the template (pull_request_target) Successful in 11s
Website / Website on Cloudflare (pull_request_target) Has been skipped
CI / Conventional Commit title (pull_request) Has been cancelled
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Has been cancelled
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Has been cancelled
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Has been cancelled
CI / Workflow lint (actionlint) (pull_request) Has been cancelled
CI / Website build and checks (pull_request) Has been cancelled
Guide screenshots / Guide images and local UI source review (pull_request) Has been cancelled
Third-party parts / As their notes say (pull_request) Has been cancelled
Third-party parts / The same as upstream (pull_request) Has been cancelled
to 8443bba1f9
Some checks are pending
CI / Conventional Commit title (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Blocked by required conditions
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Blocked by required conditions
CI / Workflow lint (actionlint) (pull_request) Blocked by required conditions
CI / Website build and checks (pull_request) Blocked by required conditions
Guide screenshots / Guide images and local UI source review (pull_request) Blocked by required conditions
Third-party parts / As their notes say (pull_request) Blocked by required conditions
Third-party parts / The same as upstream (pull_request) Blocked by required conditions
Website / Website on Cloudflare (pull_request) Blocked by required conditions
Website / Website on Cloudflare (pull_request_target) Has been skipped
beanstalk/approval waiting for a BigBean's 👍 on the request
PR description / Description follows the template (pull_request_target) Successful in 17s
2026-11-21 03:25:58 +00:00
Compare
Author
Contributor

Pushed d3e02c70 (test), 18e90465 (fix) and 8443bba1 (profiling descriptions) for BeanBot's review, with the branch rebased onto main @ 6e1521a7 (from 2e1f6c32; git range-diff shows 95c0fd41 unchanged as 93f9b35a). The description is updated.

  • Blocking 1, trace_ms: fixed in 18e90465. HOS_MUTEX_TRACE is cached through an atomic (two first waits store the same value), kept in a local for the whole wait, and parsed with strtoll over the whole string: only 1 to 2147483647 ms turns it on, anything else is off (-1 used to log every wait). The switch's row in gpu/vk/switches.def says so.
  • Blocking 2, owner pointer: fixed for the lifetime in 18e90465. The holder's name is copied with the wait counted in mwait and m->lk held; the holder's unlock (mutex_give) then waits for m->lk, so it cannot end and be freed during the copy. The Tracy text and the long-wait log use that copy, and the 10 s log reads the name under m->lk too. The rename race (hos_thread_set_name writing a name while another thread reads it) is in host/hos_thread.c and affects every name reader. It is pre-existing on main, unchanged by this PR, and left for a separate change.
  • Blocking 3, m->count in the 10 s log: fixed in 18e90465, the count is dropped (and mwait is read as an atomic).
  • Blocking 4, volatile hos_chain_on: not changed here. The flag, its writer and HOS_CHAIN live in host/hos_chain.c, host/hos.h and six call sites in nn_os_sync.c, all as they are on main; this PR moves one read of it. Making the recorder atomic with release/acquire publication of recs is a separate change, listed for the owner and named in the description.
  • Blocking 5, spin_begin's flag: not changed here. spin_begin is unchanged by this PR (it moves the call to spin_until_free). The race is pre-existing on main and left for a separate change, named in the description.
  • Blocking 6, gcc forced: fixed in d3e02c70. RUN_ALL_ARGS is gone; the compiler is --cc, else CC, else LONGSHOT_MINGW's clang on Windows, else clang, gcc or cc, as #1954's test does. The queue run below gave it no --cc, so it used the configured llvm-mingw clang.
  • Inline, fixture marker: taken in d3e02c70: the cut starts at void mutex_lock_wait(, and the test cuts it as a plain static function.
  • Inline, the trigger: taken in 18e90465 and in the description: a failed immediate acquire, including a reserved mutex with no owner.
  • Suggestion, cpu_functions.json: taken in 8443bba1 (mutex_lock updated, mutex_lock_wait added).
  • Waiting, production assembly and play and the failed-first-take handoff: pending, for the owner. Nothing is measured for them yet; both are in Still to test, and the description no longer claims the leaf shape.
  • The author's game run, host builds on both OSes, the full checks and CI: pending. Run at 8443bba1 through this PC's job queue: tests/test_cv_handoff_threads.py passes, and it fails at d3e02c70 (the log read a holder that had ended). tests/run_all.py --strict --changed origin/main: 32 passed, 7 failed, 3 skipped; the 7 fail the same way on main @ 6e1521a7. host/nn_os_sync.c compiles with the host build's flags with and without Tracy.
Pushed d3e02c70 (test), 18e90465 (fix) and 8443bba1 (profiling descriptions) for [BeanBot's review](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1953#issuecomment-61957), with the branch rebased onto `main` @ 6e1521a7 (from 2e1f6c32; `git range-diff` shows 95c0fd41 unchanged as 93f9b35a). The description is updated. - [Blocking 1, trace_ms](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1953#issuecomment-61949): fixed in 18e90465. `HOS_MUTEX_TRACE` is cached through an atomic (two first waits store the same value), kept in a local for the whole wait, and parsed with `strtoll` over the whole string: only 1 to 2147483647 ms turns it on, anything else is off (`-1` used to log every wait). The switch's row in `gpu/vk/switches.def` says so. - [Blocking 2, owner pointer](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1953#issuecomment-61950): fixed for the lifetime in 18e90465. The holder's name is copied with the wait counted in `mwait` and `m->lk` held; the holder's unlock (`mutex_give`) then waits for `m->lk`, so it cannot end and be freed during the copy. The Tracy text and the long-wait log use that copy, and the 10 s log reads the name under `m->lk` too. The rename race (`hos_thread_set_name` writing a name while another thread reads it) is in `host/hos_thread.c` and affects every name reader. It is pre-existing on `main`, unchanged by this PR, and left for a separate change. - Blocking 3, `m->count` in the 10 s log: fixed in 18e90465, the count is dropped (and `mwait` is read as an atomic). - Blocking 4, `volatile hos_chain_on`: not changed here. The flag, its writer and `HOS_CHAIN` live in `host/hos_chain.c`, `host/hos.h` and six call sites in `nn_os_sync.c`, all as they are on `main`; this PR moves one read of it. Making the recorder atomic with release/acquire publication of `recs` is a separate change, listed for the owner and named in the description. - [Blocking 5, spin_begin's flag](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1953#issuecomment-61951): not changed here. `spin_begin` is unchanged by this PR (it moves the call to `spin_until_free`). The race is pre-existing on `main` and left for a separate change, named in the description. - [Blocking 6, gcc forced](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1953#issuecomment-61952): fixed in d3e02c70. `RUN_ALL_ARGS` is gone; the compiler is `--cc`, else `CC`, else `LONGSHOT_MINGW`'s clang on Windows, else clang, gcc or cc, as #1954's test does. The queue run below gave it no `--cc`, so it used the configured llvm-mingw clang. - [Inline, fixture marker](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1953#issuecomment-61955): taken in d3e02c70: the cut starts at `void mutex_lock_wait(`, and the test cuts it as a plain static function. - [Inline, the trigger](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1953#issuecomment-61956): taken in 18e90465 and in the description: a failed immediate acquire, including a reserved mutex with no owner. - Suggestion, `cpu_functions.json`: taken in 8443bba1 (`mutex_lock` updated, `mutex_lock_wait` added). - [Waiting, production assembly and play](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1953#issuecomment-61953) and [the failed-first-take handoff](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1953#issuecomment-61954): pending, for the owner. Nothing is measured for them yet; both are in Still to test, and the description no longer claims the leaf shape. - The author's game run, host builds on both OSes, the full checks and CI: pending. Run at 8443bba1 through this PC's job queue: `tests/test_cv_handoff_threads.py` passes, and it fails at d3e02c70 (the log read a holder that had ended). `tests/run_all.py --strict --changed origin/main`: 32 passed, 7 failed, 3 skipped; the 7 fail the same way on `main` @ 6e1521a7. `host/nn_os_sync.c` compiles with the host build's flags with and without Tracy.
Author
Contributor

@futondestroyer, on your review:

  • The benefit is still unshown. The description no longer claims the leaf shape. Still to test now lists the production mutex_lock against main's, brief contention that ends without a sleep (the new out-of-line path), and warmed, interleaved Market runs against exact main with fps, 1 % lows, median and p99 frame times. None of these has run yet; they are for the owner.
  • The inherited diagnostic defects: the three that sit inside the new function (the HOS_MUTEX_TRACE cache and parse, the holder's name read after it may have ended, m->count in the 10 s log) are fixed in their own commits, d3e02c70 (failing test) and 18e90465 (fix), on top of the split. If you would rather land them as a PR of their own, they can move as they are. hos_chain_on, spin_begin's cached flag and the thread-name rename race are left on main for separate changes, and the description says so.
  • #1957 carries this commit. It will be rebased onto this head once this PR is settled, so the step does not land twice.
@futondestroyer, on [your review](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1953#issuecomment-62896): - The benefit is still unshown. The description no longer claims the leaf shape. Still to test now lists the production `mutex_lock` against `main`'s, brief contention that ends without a sleep (the new out-of-line path), and warmed, interleaved Market runs against exact `main` with fps, 1 % lows, median and p99 frame times. None of these has run yet; they are for the owner. - The inherited diagnostic defects: the three that sit inside the new function (the `HOS_MUTEX_TRACE` cache and parse, the holder's name read after it may have ended, `m->count` in the 10 s log) are fixed in their own commits, d3e02c70 (failing test) and 18e90465 (fix), on top of the split. If you would rather land them as a PR of their own, they can move as they are. `hos_chain_on`, `spin_begin`'s cached flag and the thread-name rename race are left on `main` for separate changes, and the description says so. - #1957 carries this commit. It will be rebased onto this head once this PR is settled, so the step does not land twice.
Author
Contributor

@Beanstalk This moves the guest mutex lock's wait for a held mutex out of line and cold (host/nn_os_sync.c, mutex_lock): every guest mutex lock whose first attempt fails takes the new path; the uncontended path gets shorter. No switch: a revert gives back main's code. It has not run in the game on these PCs yet. Please play Castle Town Market for two minutes, the PR against main on the same PC, on a Windows PC with an AMD processor, a Linux PC with an AMD processor, a PC with 6 or fewer logical processors and the Steam Deck. No hang or stall and no audio or frame pacing trouble; frame time p99 equal to main's or better, within the run-to-run spread.

@Beanstalk This moves the guest mutex lock's wait for a held mutex out of line and cold (host/nn_os_sync.c, mutex_lock): every guest mutex lock whose first attempt fails takes the new path; the uncontended path gets shorter. No switch: a revert gives back main's code. It has not run in the game on these PCs yet. Please play Castle Town Market for two minutes, the PR against main on the same PC, on a Windows PC with an AMD processor, a Linux PC with an AMD processor, a PC with 6 or fewer logical processors and the Steam Deck. No hang or stall and no audio or frame pacing trouble; frame time p99 equal to main's or better, within the run-to-run spread.
Member

@siantm only members of BigBeans can start the farm. One of them can approve this request by reacting 👍 to it or to this comment.

@siantm only members of `BigBeans` can start the farm. One of them can approve this request by reacting 👍 to it or to this comment.
Author
Contributor

The three pre-existing defects this PR left on main (named in BeanBot's review) are now fixes of their own, each with a game-free test that fails on main (this PC's job queue, fb3-sep-1/2); none has run in the game yet:

  • Blocking 5, spin_begin's flag: #2012 (merged as 56da0e2e). tests/test_spin_always_once.py: 8 threads first spinning at once read HOS_SPIN_ALWAYS 8 times on main, once with the fix.
  • Blocking 4, volatile hos_chain_on: #2019 (fix/os-chain-flag-atomic @ a6fffab8). The flag is atomic (release once the buffer is there, acquire before using it, relaxed at the six waits); a note's record is published by its kind (release) and the recorder waits for it (acquire) instead of sleeping 50 ms. tests/test_chain_recorder.py: a note held across the stop left a record of zeros on main. It conflicts with this PR on the one moved line const int chain = hos_chain_on;; whichever lands second is rebased.
  • Blocking 2, the rename part: #2020 (fix/os-thread-names-kept @ 67baca96). A name is never written over: a rename publishes a kept name with one atomic store, so every hos_thread_name reader gets a whole name. tests/test_thread_rename.py: a reader during a rename read half of each name on main. It merges with this PR without a conflict.
The three pre-existing defects this PR left on `main` (named in [BeanBot's review](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1953#issuecomment-61957)) are now fixes of their own, each with a game-free test that fails on `main` (this PC's job queue, fb3-sep-1/2); none has run in the game yet: - [Blocking 5, spin_begin's flag](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1953#issuecomment-61951): #2012 (merged as 56da0e2e). `tests/test_spin_always_once.py`: 8 threads first spinning at once read `HOS_SPIN_ALWAYS` 8 times on `main`, once with the fix. - Blocking 4, `volatile hos_chain_on`: #2019 (`fix/os-chain-flag-atomic` @ a6fffab8). The flag is atomic (release once the buffer is there, acquire before using it, relaxed at the six waits); a note's record is published by its kind (release) and the recorder waits for it (acquire) instead of sleeping 50 ms. `tests/test_chain_recorder.py`: a note held across the stop left a record of zeros on `main`. It conflicts with this PR on the one moved line `const int chain = hos_chain_on;`; whichever lands second is rebased. - [Blocking 2, the rename part](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1953#issuecomment-61950): #2020 (`fix/os-thread-names-kept` @ 67baca96). A name is never written over: a rename publishes a kept name with one atomic store, so every `hos_thread_name` reader gets a whole name. `tests/test_thread_rename.py`: a reader during a rename read half of each name on `main`. It merges with this PR without a conflict.
Author
Contributor

Description: Summary says which PRs now carry the three defects left on main (#2012 merged, #2019, #2020) and the landing order with #2019 (one shared line).

Description: Summary says which PRs now carry the three defects left on `main` (#2012 merged, #2019, #2020) and the landing order with #2019 (one shared line).
Author
Contributor

The three renderer checks (test_present_queue_default, test_target_reuse_memory, test_vk_occlusion_queries) did not compile in the earlier run (no Vulkan headers on that job's path); rerun with them in job fb3-vk-2, at the head 8443bba1 and on main @ 6e1521a7: all three fail at both with the same first error, so they fail on main too; Tested's sentence and counts corrected.

The three renderer checks (`test_present_queue_default`, `test_target_reuse_memory`, `test_vk_occlusion_queries`) did not compile in the earlier run (no Vulkan headers on that job's path); rerun with them in job fb3-vk-2, at the head 8443bba1 and on `main` @ 6e1521a7: all three fail at both with the same first error, so they fail on `main` too; Tested's sentence and counts corrected.
Some checks are pending
CI / Conventional Commit title (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Blocked by required conditions
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Blocked by required conditions
CI / Workflow lint (actionlint) (pull_request) Blocked by required conditions
CI / Website build and checks (pull_request) Blocked by required conditions
Guide screenshots / Guide images and local UI source review (pull_request) Blocked by required conditions
Third-party parts / As their notes say (pull_request) Blocked by required conditions
Third-party parts / The same as upstream (pull_request) Blocked by required conditions
Website / Website on Cloudflare (pull_request) Blocked by required conditions
Website / Website on Cloudflare (pull_request_target) Has been skipped
beanstalk/approval waiting for a BigBean's 👍 on the request
PR description / Description follows the template (pull_request_target) Successful in 17s
This pull request is marked as a work in progress.
Some workflows are waiting to be reviewed.
This branch is out-of-date with the base branch
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u 'perf/mutex-lock-wait-cold:siantm-perf/mutex-lock-wait-cold'
git switch 'siantm-perf/mutex-lock-wait-cold'
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
4 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
MagicBeanDev/Longshot!1953
No description provided.