WIP: perf(os): LockMutex and UnlockMutex find their host object through a number kept in the guest MutexType (HOS_MUTEX_GUEST_ID, off unless set) #2090

Draft
siantm wants to merge 27 commits from siantm/Longshot:perf/mutex-guest-id into main
Contributor

Summary

Depends on #1957 (obj_at: LockMutex and UnlockMutex take the calling thread and the core checkpoint's test from
their context; itself on #1956 and #1953) and #1969 at 617c4fba (HOS_OBJ_CACHE=sets: the sync objects' numbers, and
obj_is, the caches' identity check); their commits show in this diff until they merge. This PR's own commits are its
last four: 16081d49, the change; 08a323a2, a test it failed (a mutex address within 0x18 of 2^64, whose word's address
wrapped round to the guest window's first bytes and was written there); 8febfeaf, the fix (the struct checked from its
address to the end of the word as one guest range); and 179e9947, the switch off unless set.

LockMutex and UnlockMutex look their host object up by the mutex's guest address: the per-thread cache
(HOS_OBJ_CACHE), then the shared table. A thread that goes round more mutexes than its cache keeps misses into the
table on every lock, while the guest's own MutexType is right there.

This PR keeps the mutex's object number in its MutexType, in the internal critical-section word at +0x18:
nn::os's own mutex code was the only user of that word, and the HLE functions replace all of it. obj_at, the two
HLE functions' lookup, reads it (obj_by_word). HOS_MUTEX_GUEST_ID says where:

  • unset (the default), 0, or another off value of the switches' rule (empty, false, off, no): off, the guest
    struct left alone and the parents' lookup;
  • 1: only where the per-thread cache misses, before the table (obj_miss; obj_at names the word to it through a
    thread-local pointer while it looks up);
  • 2 or any other value: before the per-thread cache, so a mutex whose word names it needs neither the cache nor the
    table.

The guest word is untrusted: the object it names is taken only while it is still that address's mutex (obj_is,
#1969's check: its key and kind, one word read in one load). Anything else there (0, the game's bytes, a number out of
range or not given yet, another mutex's or another kind's number) is never taken: the lookup runs as before, after which the word is
written and marked (HOS_WRITTEN). The word is read and written with relaxed atomics, only on a 4-byte boundary and
only when the struct from its address to the end of the word lies inside the guest window, as one range (no address
wraps round past 2^64); a mutex whose word lies elsewhere is looked up as before and its bytes are left alone. While
the switch is on, the table numbers its objects (chunks of 1024, as HOS_OBJ_CACHE=sets does), so the word works with
every per-thread cache.

The switch is read once through an atomic word, as #1969 reads HOS_OBJ_CACHE: at the first LockMutex or
UnlockMutex or the first sync object made, whichever comes first, not at process start. It is off unless set: an
experiment while it is measured, as futondestroyer answered for the host experiments
(#1966 (comment): "Keep the draft experiment off while
gathering the enabled and disabled results"). Unset, the lookup is the parents' with one relaxed load of the mode in
obj_at. =2 is the value the performance branch this comes from ran it at, and the one where the word spares a lock
both lookups rather than only the table's on a cache miss. Its row in gpu/vk/switches.def is an attempt, off unset.

Tests: tests/test_obj_guest_id.py is new. tests/test_cv_handoff_threads.py (#1957's) gets the switch at its
default, off, in its fixture (mutex_guest_id 0, obj_nockpt the lookup; its obj_by_word stub is the same lookup)
and its "lookup before the checkpoint" variant moved to obj_at's new lookup line; tests/test_obj_cache_size.py
(#1969's) clears HOS_MUTEX_GUEST_ID for its runs, as its cut now reads it (they run at the default, off).

From c565d05a, 02be0b02 and 064574ab on exp/x86-asm-sync, where it sat in that branch's set cache.

Read first: obj_at, mutex_guest_id, obj_of_word, obj_miss and obj_by_word in host/nn_os_sync.c;
tests/test_obj_guest_id.py.

Testing

Tested: Windows 11, i7-9700, RTX 4060, llvm-mingw clang 23.1.5; game-free checks only. This change has not run in
the game on this PC (Still to test). Each check ran in a hidden console of its own, in one job of the PC's job queue,
on this head 179e9947 (on #1969's commits up to 617c4fba over #1957's d111a643, on main @ 9dd41326):

  • python -I -B tests/test_obj_guest_id.py: exit 0. 36 runs, each a fresh process: HOS_MUTEX_GUEST_ID unset, 0,
    off, 1, 2, 3, 12, on and unset again, each with the 4 per-thread caches (the sized one,
    HOS_OBJ_CACHE=0, sets, none). Read as off unset (both times) and for 0, off; 1 for 1; 2 for 2, 3, 12,
    on; kept when the variable is flipped after the first lookups. Every lookup answered the table's object; 5345
    guest words written in each run that is on, none in the 16 off (no word marked, guest memory unchanged, objects
    unnumbered without sets); last line PASS: the mutex lookup answers the table's object with and without the guest word; only the words change.
  • At 2f7195ca (this PR's same four commits on #1969's commits up to 712ee7e7, before the restack), the same test against
    two broken copies of that head's host/nn_os_sync.c (worktrees of their own, in that job), each exit 1: unset read as 2
    again (the default on, as at bca5fcc9): the 8 unset runs fail "HOS_MUTEX_GUEST_ID read as expected", the 28 others
    pass; obj_at taking the guest-word path when off: the 4 off runs with the sets cache (unset twice, 0, off)
    fail "off: no guest write marked", the 32 others pass.
  • The parents' checks that read the lookup, each exit 0: tests/test_obj_cache_size.py (ok: the cache mode is read inline (no call to obj_cache_mode_read), ok: test_obj_cache_size),
    tests/test_cv_handoff_threads.py, tests/test_multiwait_wake.py (PASS: WaitAny woken only for watched addresses ...; no watched-target lookups or lookups under the extracted paths' mw_lock ...); and tests/test_switch_table.py
    for the row (ok: 737 switches (diag 296, tune 109, kill 129, attempt 81, hw 68, player 54), 144 removed, 745 names read).

Earlier, with the switch on unless =0 (the tree of bca5fcc9, on #1969's commits up to 712ee7e7, before the switch was
made off unless set):

  • At the test commit (81ad2464, then 17c045c9, now 08a323a2): exit 1, the 24 runs with the switch on failing "only the mutexes' words
    changed" (the wrapping mutex's number written at guest address 8); the 8 with it off pass.
  • The same test against host/nn_os_sync.c with one change each (copies outside the worktree), each exit 1: the
    guest-word path never taken, and objects numbered only with HOS_OBJ_CACHE=sets ("the first lookup writes the
    mutex's number in its word"); the word's object taken by its kind alone, or by its key alone ("the lookup answers
    the table's mutex"); the switch read at every call ("read once"); 0 not honoured, and 12 read as 1 ("read as
    expected"); a word off a 4-byte boundary used ("only the mutexes' words changed"). Against the parent's
    nn_os_sync.c (#1969's 712ee7e7) it stops at its cut ("mutex_guest_id is not in test_obj_cache_size's cut").
  • These three, which do not read the object lookup, ran each exit 0 on the earlier cut of this change (136f0867, on
    #1969's commits up to 479a4f90): tests/test_vcore_escape.py, tests/test_vcore_mask0.py, and
    tests/test_invalidate_vcore.py run alone (4 PASS lines).
  • python -I -B tests/run_all.py --strict --changed origin/main on bca5fcc9 (the 130 checks that read one of the 22
    changed files, 8 at once): 109 passed, 21 failed, 5 skipped. The 21 that fail run again by name on main @
    9dd41326 (same job) fail there too, all 21: none fails on this branch alone. Not run again since.

Still to test:

  • Windows 11, this PC (i7-9700, RTX 4060): Castle Town's market, 60 s, four runs a round: main @ 9dd41326, #1957's
    head d111a643, this PR as it is (HOS_MUTEX_GUEST_ID unset: off) and this PR with HOS_MUTEX_GUEST_ID=2; #1957's
    and this PR's builds lift the game again (#1956's A64_OS_INLINE). Each warmed once, then interleaved, at least
    two rounds, the medians of fps and frame times compared; expecting the same game, no hang or crash, unset as #1957's
    head, and frame times with =2 as unset or better (by hand)
  • Linux, x86-64: python3 -I -B tests/test_obj_guest_id.py, expecting its 36 runs to pass with the sanitizers on (by
    hand)
  • Steam Deck: zeldaootr's run covered =2 against unset in the Market reached by teleport, three pairs, with no
    difference shown and no hang or crash (Measurements). Still open there: this PR against main, from the
    market-entrance save, expecting no hang or crash and frame times as main's or better (the Test Farm, below)
  • Linux and Windows with other CPUs (an AMD CPU) and the Steam Deck: two minutes from the market-entrance save, this
    PR against main, on the PR's side with HOS_MUTEX_GUEST_ID unset and =2, expecting no hang or crash and frame
    times with =2 as unset or better (the Test Farm: two minutes from the market-entrance save on PCs with AMD CPUs
    and on the Steam Deck, this PR against main, with HOS_MUTEX_GUEST_ID unset and =2 on the PR's side, comparing frame
    times and checking for hangs and crashes)

Measurements

Verdict: no gain in play shown: not measured in play on this PC yet, and a Steam Deck run by zeldaootr (below)
shows no difference the Deck can resolve. A micro-benchmark on authored input, not measured in play: the lookup alone takes
5.39 ns with HOS_MUTEX_GUEST_ID=2 against 12.86 ns unset (off, as it ships), and 15.60 ns with =1, which is slower
than unset. =1 is the cache-miss form (the word read only where the per-thread cache misses, before the shared table,
so cache hits stay as they are); in this benchmark it costs more than it spares, so it gives no reason to set =1.
The switch stays off.

Setup: Windows 11, Intel i7-9700, llvm-mingw clang 23.1.5 (-O2 -march=native); no GPU involved
Scene: none (game-free); a 16 MB guest window of random bytes, 4,000 mutexes 0x80 bytes apart among 60,000 other objects
Method: this PR's own obj_at and obj_by_word (179e9947), cut and built as tests/test_obj_guest_id.py builds
them; the 4,000 mutexes looked up in turn, 50 passes a repetition, best of 7 repetitions; one process per mode and
round, the modes interleaved (order reversed every other round), 15 rounds; ns a lookup, the median of the rounds and
their range. Run alone on the PC on 2026-11-22.
How to reproduce: a local script of this PC (bench_guest_id.py, not part of this PR) over an export of 179e9947,
with HOS_MUTEX_GUEST_ID unset, 1 and 2, each with HOS_OBJ_CACHE unset (the sized cache) and =sets (#1969's).

Baseline: this PR with the switch unset, which runs the parents' lookup (#1969's and #1957's, the code this PR sits on);
main has neither of those lookups, so it is not the comparison here.

obj_at, ns a lookup (median, rounds' range, 15 rounds) Before: this PR @ 179e9947, unset (off) After, =1: this PR @ 179e9947 After, =2: this PR @ 179e9947
sized cache (HOS_OBJ_CACHE unset) 12.86 (12.80 to 13.24) 15.60 (15.49 to 16.17) 5.39 (5.08 to 5.49)
HOS_OBJ_CACHE=sets 13.18 (13.07 to 13.67) 16.18 (16.00 to 16.75) 5.05 (5.00 to 5.38)
obj_at, HOS_MUTEX_GUEST_ID unset       median   12.86 ns a lookup  (rounds 12.80 to 13.24, 15 rounds)
obj_at, HOS_MUTEX_GUEST_ID =1          median   15.60 ns a lookup  (rounds 15.49 to 16.17, 15 rounds)
obj_at, HOS_MUTEX_GUEST_ID =2          median    5.39 ns a lookup  (rounds 5.08 to 5.49, 15 rounds)
obj_at, HOS_MUTEX_GUEST_ID unset sets  median   13.18 ns a lookup  (rounds 13.07 to 13.67, 15 rounds)
obj_at, HOS_MUTEX_GUEST_ID =1 sets     median   16.18 ns a lookup  (rounds 16.00 to 16.75, 15 rounds)
obj_at, HOS_MUTEX_GUEST_ID =2 sets     median    5.05 ns a lookup  (rounds 5.00 to 5.38, 15 rounds)

A Steam Deck run by zeldaootr (an outside contributor; his run and his figures, from his comment
on this PR of 2026-11-22, 04:05 UTC): Steam Deck OLED, SteamOS 3.9.5,
RADV VANGOGH (Mesa 26.2.0-devel), GPU clock fixed at 1600 MHz; Minimum, Render 50 % (640x400), output 1280x800,
uncapped, V-Sync and frame generation off. One build, main @ 9dd41326 with this PR's head 179e9947 merged in (and so
the PRs it is stacked on); HOS_MUTEX_GUEST_ID unset against =2, dev.py ab (with a local change to compare two
prebuilt runtimes), the Market reached by teleport then standing, 3 interleaved pairs of 120 s after a warm-up
(A1 B1 A2 B2 A3 B3), the median.

Steam Deck, Market (his run) unset =2
fps, each run (median) 26.0 / 26.2 / 27.3 (26.2) 26.4 / 28.0 / 26.0 (26.4), +0.6 %
median frame time 36.59 ms 36.46 ms
p99 frame time 60.81 ms 58.49 ms
frames of 50 ms or more, each run 216 / 188 / 108 190 / 64 / 206
waited ... ms for a core lines, each run 107 / 49 / 76 61 / 41 / 61

His reading: no difference shown, within the runs' spread; the better p99 and 1 % low follow the fastest run of the six
(B2), and the other two =2 runs sit with the unset ones. No hang, crash, error, FATAL, device-lost or validation line
in the six logs; every run exited normally. His stated limits: not this PR against main (what it is stacked on changes
much more than the switch does); the Market as dev.py ab reaches it, not the market-entrance save; the Deck's runs vary
by 5 to 10 % from start to start, so a gain of about 1 % would not show. tests/test_obj_guest_id.py also passed once on
his Linux PC (without the sanitizers).

In play on this PC, nothing is measured on this head; that comparison is in Still to test.

The earlier measurement of this change, on exp/x86-asm-sync where it sat in that branch's set cache (02be0b02, not
this head): Kokiri Forest, camera turning, 800x600 window (the CPU sets the pace), i7-9700 + RTX 4060, interleaved
30 s runs, HOS_MUTEX_GUEST_ID=2 71.8 fps (71.2 to 72.6, 4 runs) against 71.1 (70.4 to 72.8) without it in the same
build, CPU 59.7 against 60.3 ms a frame; that commit recorded "about 1 %, inside the runs' spread": no gain measured.
A later note on that branch (064574ab), with its larger object cache: Hyrule, 804x581, 6 interleaved pairs, 68.9 ->
70.0 fps and 45.3 -> 46.4 CPU ms a frame, recorded as within noise.

Before: main @ 9dd41326 After: this PR @ 179e9947, HOS_MUTEX_GUEST_ID unset (off) / =2
Castle Town's market, fps and frame times not measured yet not measured yet / not measured yet

Risks

Off unless set. With =1 or =2, it writes a 4-byte word of guest memory in every mutex locked (the internal
critical-section word at +0x18 of its MutexType) and numbers the sync objects, which are then made in chunks of 1024
(never freed, as objects never are) instead of one by one. If the game itself read or wrote that word (only
nn::os's mutex code did, and the HLE functions replace it), a wrong word costs a lookup and is written again, never
a wrong object, and the game would see the number instead of its own bytes. Unset or =0 it is off (the guest struct
left alone, the parents' lookup); a revert is enough. With =1 the lookup costs more than unset in the micro-benchmark
(15.6 against 12.9 ns a lookup). A player notices nothing after updating: no save, option or
cache changes.

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 switch added or changed: it ships at the default the measurements chose, with its row in gpu/vk/switches.def.
  • 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.
  • 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 Depends on #1957 (`obj_at`: LockMutex and UnlockMutex take the calling thread and the core checkpoint's test from their context; itself on #1956 and #1953) and #1969 at 617c4fba (`HOS_OBJ_CACHE=sets`: the sync objects' numbers, and `obj_is`, the caches' identity check); their commits show in this diff until they merge. This PR's own commits are its last four: 16081d49, the change; 08a323a2, a test it failed (a mutex address within 0x18 of 2^64, whose word's address wrapped round to the guest window's first bytes and was written there); 8febfeaf, the fix (the struct checked from its address to the end of the word as one guest range); and 179e9947, the switch off unless set. `LockMutex` and `UnlockMutex` look their host object up by the mutex's guest address: the per-thread cache (`HOS_OBJ_CACHE`), then the shared table. A thread that goes round more mutexes than its cache keeps misses into the table on every lock, while the guest's own `MutexType` is right there. This PR keeps the mutex's object number in its `MutexType`, in the internal critical-section word at +0x18: `nn::os`'s own mutex code was the only user of that word, and the HLE functions replace all of it. `obj_at`, the two HLE functions' lookup, reads it (`obj_by_word`). `HOS_MUTEX_GUEST_ID` says where: - unset (the default), `0`, or another off value of the switches' rule (empty, `false`, `off`, `no`): off, the guest struct left alone and the parents' lookup; - `1`: only where the per-thread cache misses, before the table (`obj_miss`; `obj_at` names the word to it through a thread-local pointer while it looks up); - `2` or any other value: before the per-thread cache, so a mutex whose word names it needs neither the cache nor the table. The guest word is untrusted: the object it names is taken only while it is still that address's mutex (`obj_is`, #1969's check: its key and kind, one word read in one load). Anything else there (0, the game's bytes, a number out of range or not given yet, another mutex's or another kind's number) is never taken: the lookup runs as before, after which the word is written and marked (`HOS_WRITTEN`). The word is read and written with relaxed atomics, only on a 4-byte boundary and only when the struct from its address to the end of the word lies inside the guest window, as one range (no address wraps round past 2^64); a mutex whose word lies elsewhere is looked up as before and its bytes are left alone. While the switch is on, the table numbers its objects (chunks of 1024, as `HOS_OBJ_CACHE=sets` does), so the word works with every per-thread cache. The switch is read once through an atomic word, as #1969 reads `HOS_OBJ_CACHE`: at the first `LockMutex` or `UnlockMutex` or the first sync object made, whichever comes first, not at process start. It is off unless set: an experiment while it is measured, as futondestroyer answered for the host experiments (https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1966#issuecomment-67223: "Keep the draft experiment off while gathering the enabled and disabled results"). Unset, the lookup is the parents' with one relaxed load of the mode in `obj_at`. `=2` is the value the performance branch this comes from ran it at, and the one where the word spares a lock both lookups rather than only the table's on a cache miss. Its row in `gpu/vk/switches.def` is an attempt, off unset. Tests: `tests/test_obj_guest_id.py` is new. `tests/test_cv_handoff_threads.py` (#1957's) gets the switch at its default, off, in its fixture (`mutex_guest_id` 0, `obj_nockpt` the lookup; its `obj_by_word` stub is the same lookup) and its "lookup before the checkpoint" variant moved to `obj_at`'s new lookup line; `tests/test_obj_cache_size.py` (#1969's) clears `HOS_MUTEX_GUEST_ID` for its runs, as its cut now reads it (they run at the default, off). From c565d05a, 02be0b02 and 064574ab on `exp/x86-asm-sync`, where it sat in that branch's set cache. Read first: `obj_at`, `mutex_guest_id`, `obj_of_word`, `obj_miss` and `obj_by_word` in `host/nn_os_sync.c`; `tests/test_obj_guest_id.py`. ## Testing **Tested:** Windows 11, i7-9700, RTX 4060, llvm-mingw clang 23.1.5; game-free checks only. This change has not run in the game on this PC (Still to test). Each check ran in a hidden console of its own, in one job of the PC's job queue, on this head 179e9947 (on #1969's commits up to 617c4fba over #1957's d111a643, on `main` @ 9dd41326): - `python -I -B tests/test_obj_guest_id.py`: exit 0. 36 runs, each a fresh process: `HOS_MUTEX_GUEST_ID` unset, `0`, `off`, `1`, `2`, `3`, `12`, `on` and unset again, each with the 4 per-thread caches (the sized one, `HOS_OBJ_CACHE=0`, `sets`, none). Read as off unset (both times) and for `0`, `off`; 1 for `1`; 2 for `2`, `3`, `12`, `on`; kept when the variable is flipped after the first lookups. Every lookup answered the table's object; 5345 guest words written in each run that is on, none in the 16 off (no word marked, guest memory unchanged, objects unnumbered without `sets`); last line `PASS: the mutex lookup answers the table's object with and without the guest word; only the words change`. - At 2f7195ca (this PR's same four commits on #1969's commits up to 712ee7e7, before the restack), the same test against two broken copies of that head's `host/nn_os_sync.c` (worktrees of their own, in that job), each exit 1: unset read as 2 again (the default on, as at bca5fcc9): the 8 unset runs fail "HOS_MUTEX_GUEST_ID read as expected", the 28 others pass; `obj_at` taking the guest-word path when off: the 4 off runs with the `sets` cache (unset twice, `0`, `off`) fail "off: no guest write marked", the 32 others pass. - The parents' checks that read the lookup, each exit 0: `tests/test_obj_cache_size.py` (`ok: the cache mode is read inline (no call to obj_cache_mode_read)`, `ok: test_obj_cache_size`), `tests/test_cv_handoff_threads.py`, `tests/test_multiwait_wake.py` (`PASS: WaitAny woken only for watched addresses ...; no watched-target lookups or lookups under the extracted paths' mw_lock ...`); and `tests/test_switch_table.py` for the row (`ok: 737 switches (diag 296, tune 109, kill 129, attempt 81, hw 68, player 54), 144 removed, 745 names read`). Earlier, with the switch on unless `=0` (the tree of bca5fcc9, on #1969's commits up to 712ee7e7, before the switch was made off unless set): - At the test commit (81ad2464, then 17c045c9, now 08a323a2): exit 1, the 24 runs with the switch on failing "only the mutexes' words changed" (the wrapping mutex's number written at guest address 8); the 8 with it off pass. - The same test against `host/nn_os_sync.c` with one change each (copies outside the worktree), each exit 1: the guest-word path never taken, and objects numbered only with `HOS_OBJ_CACHE=sets` ("the first lookup writes the mutex's number in its word"); the word's object taken by its kind alone, or by its key alone ("the lookup answers the table's mutex"); the switch read at every call ("read once"); `0` not honoured, and `12` read as 1 ("read as expected"); a word off a 4-byte boundary used ("only the mutexes' words changed"). Against the parent's `nn_os_sync.c` (#1969's 712ee7e7) it stops at its cut ("mutex_guest_id is not in test_obj_cache_size's cut"). - These three, which do not read the object lookup, ran each exit 0 on the earlier cut of this change (136f0867, on #1969's commits up to 479a4f90): `tests/test_vcore_escape.py`, `tests/test_vcore_mask0.py`, and `tests/test_invalidate_vcore.py` run alone (4 `PASS` lines). - `python -I -B tests/run_all.py --strict --changed origin/main` on bca5fcc9 (the 130 checks that read one of the 22 changed files, 8 at once): 109 passed, 21 failed, 5 skipped. The 21 that fail run again by name on `main` @ 9dd41326 (same job) fail there too, all 21: none fails on this branch alone. Not run again since. **Still to test:** - Windows 11, this PC (i7-9700, RTX 4060): Castle Town's market, 60 s, four runs a round: `main` @ 9dd41326, #1957's head d111a643, this PR as it is (`HOS_MUTEX_GUEST_ID` unset: off) and this PR with `HOS_MUTEX_GUEST_ID=2`; #1957's and this PR's builds lift the game again (#1956's `A64_OS_INLINE`). Each warmed once, then interleaved, at least two rounds, the medians of fps and frame times compared; expecting the same game, no hang or crash, unset as #1957's head, and frame times with `=2` as unset or better (by hand) - Linux, x86-64: `python3 -I -B tests/test_obj_guest_id.py`, expecting its 36 runs to pass with the sanitizers on (by hand) - Steam Deck: zeldaootr's run covered `=2` against unset in the Market reached by teleport, three pairs, with no difference shown and no hang or crash (Measurements). Still open there: this PR against `main`, from the market-entrance save, expecting no hang or crash and frame times as `main`'s or better (the Test Farm, below) - Linux and Windows with other CPUs (an AMD CPU) and the Steam Deck: two minutes from the market-entrance save, this PR against `main`, on the PR's side with `HOS_MUTEX_GUEST_ID` unset and `=2`, expecting no hang or crash and frame times with `=2` as unset or better (the Test Farm: two minutes from the market-entrance save on PCs with AMD CPUs and on the Steam Deck, this PR against main, with HOS_MUTEX_GUEST_ID unset and =2 on the PR's side, comparing frame times and checking for hangs and crashes) ## Measurements **Verdict:** no gain in play shown: not measured in play on this PC yet, and a Steam Deck run by zeldaootr (below) shows no difference the Deck can resolve. A micro-benchmark on authored input, not measured in play: the lookup alone takes 5.39 ns with `HOS_MUTEX_GUEST_ID=2` against 12.86 ns unset (off, as it ships), and 15.60 ns with `=1`, which is slower than unset. `=1` is the cache-miss form (the word read only where the per-thread cache misses, before the shared table, so cache hits stay as they are); in this benchmark it costs more than it spares, so it gives no reason to set `=1`. The switch stays off. Setup: Windows 11, Intel i7-9700, llvm-mingw clang 23.1.5 (`-O2 -march=native`); no GPU involved Scene: none (game-free); a 16 MB guest window of random bytes, 4,000 mutexes 0x80 bytes apart among 60,000 other objects Method: this PR's own `obj_at` and `obj_by_word` (179e9947), cut and built as `tests/test_obj_guest_id.py` builds them; the 4,000 mutexes looked up in turn, 50 passes a repetition, best of 7 repetitions; one process per mode and round, the modes interleaved (order reversed every other round), 15 rounds; ns a lookup, the median of the rounds and their range. Run alone on the PC on 2026-11-22. How to reproduce: a local script of this PC (`bench_guest_id.py`, not part of this PR) over an export of 179e9947, with `HOS_MUTEX_GUEST_ID` unset, `1` and `2`, each with `HOS_OBJ_CACHE` unset (the sized cache) and `=sets` (#1969's). Baseline: this PR with the switch unset, which runs the parents' lookup (#1969's and #1957's, the code this PR sits on); `main` has neither of those lookups, so it is not the comparison here. | `obj_at`, ns a lookup (median, rounds' range, 15 rounds) | Before: this PR @ 179e9947, unset (off) | After, `=1`: this PR @ 179e9947 | After, `=2`: this PR @ 179e9947 | |---|---|---|---| | sized cache (`HOS_OBJ_CACHE` unset) | 12.86 (12.80 to 13.24) | 15.60 (15.49 to 16.17) | 5.39 (5.08 to 5.49) | | `HOS_OBJ_CACHE=sets` | 13.18 (13.07 to 13.67) | 16.18 (16.00 to 16.75) | 5.05 (5.00 to 5.38) | ``` obj_at, HOS_MUTEX_GUEST_ID unset median 12.86 ns a lookup (rounds 12.80 to 13.24, 15 rounds) obj_at, HOS_MUTEX_GUEST_ID =1 median 15.60 ns a lookup (rounds 15.49 to 16.17, 15 rounds) obj_at, HOS_MUTEX_GUEST_ID =2 median 5.39 ns a lookup (rounds 5.08 to 5.49, 15 rounds) obj_at, HOS_MUTEX_GUEST_ID unset sets median 13.18 ns a lookup (rounds 13.07 to 13.67, 15 rounds) obj_at, HOS_MUTEX_GUEST_ID =1 sets median 16.18 ns a lookup (rounds 16.00 to 16.75, 15 rounds) obj_at, HOS_MUTEX_GUEST_ID =2 sets median 5.05 ns a lookup (rounds 5.00 to 5.38, 15 rounds) ``` **A Steam Deck run by zeldaootr** (an outside contributor; his run and his figures, from his comment on this PR of 2026-11-22, 04:05 UTC): Steam Deck OLED, SteamOS 3.9.5, RADV VANGOGH (Mesa 26.2.0-devel), GPU clock fixed at 1600 MHz; Minimum, Render 50 % (640x400), output 1280x800, uncapped, V-Sync and frame generation off. One build, `main` @ 9dd41326 with this PR's head 179e9947 merged in (and so the PRs it is stacked on); `HOS_MUTEX_GUEST_ID` unset against `=2`, `dev.py ab` (with a local change to compare two prebuilt runtimes), the Market reached by teleport then standing, 3 interleaved pairs of 120 s after a warm-up (A1 B1 A2 B2 A3 B3), the median. | Steam Deck, Market (his run) | unset | `=2` | |---|---|---| | fps, each run (median) | 26.0 / 26.2 / 27.3 (26.2) | 26.4 / 28.0 / 26.0 (26.4), +0.6 % | | median frame time | 36.59 ms | 36.46 ms | | p99 frame time | 60.81 ms | 58.49 ms | | frames of 50 ms or more, each run | 216 / 188 / 108 | 190 / 64 / 206 | | `waited ... ms for a core` lines, each run | 107 / 49 / 76 | 61 / 41 / 61 | His reading: no difference shown, within the runs' spread; the better p99 and 1 % low follow the fastest run of the six (B2), and the other two `=2` runs sit with the unset ones. No hang, crash, error, `FATAL`, device-lost or validation line in the six logs; every run exited normally. His stated limits: not this PR against `main` (what it is stacked on changes much more than the switch does); the Market as `dev.py ab` reaches it, not the market-entrance save; the Deck's runs vary by 5 to 10 % from start to start, so a gain of about 1 % would not show. `tests/test_obj_guest_id.py` also passed once on his Linux PC (without the sanitizers). In play on this PC, nothing is measured on this head; that comparison is in Still to test. The earlier measurement of this change, on `exp/x86-asm-sync` where it sat in that branch's set cache (02be0b02, not this head): Kokiri Forest, camera turning, 800x600 window (the CPU sets the pace), i7-9700 + RTX 4060, interleaved 30 s runs, `HOS_MUTEX_GUEST_ID=2` 71.8 fps (71.2 to 72.6, 4 runs) against 71.1 (70.4 to 72.8) without it in the same build, CPU 59.7 against 60.3 ms a frame; that commit recorded "about 1 %, inside the runs' spread": no gain measured. A later note on that branch (064574ab), with its larger object cache: Hyrule, 804x581, 6 interleaved pairs, 68.9 -> 70.0 fps and 45.3 -> 46.4 CPU ms a frame, recorded as within noise. | | Before: main @ 9dd41326 | After: this PR @ 179e9947, `HOS_MUTEX_GUEST_ID` unset (off) / `=2` | |---|---|---| | Castle Town's market, fps and frame times | not measured yet | not measured yet / not measured yet | ## Risks Off unless set. With `=1` or `=2`, it writes a 4-byte word of guest memory in every mutex locked (the internal critical-section word at +0x18 of its `MutexType`) and numbers the sync objects, which are then made in chunks of 1024 (never freed, as objects never are) instead of one by one. If the game itself read or wrote that word (only `nn::os`'s mutex code did, and the HLE functions replace it), a wrong word costs a lookup and is written again, never a wrong object, and the game would see the number instead of its own bytes. Unset or `=0` it is off (the guest struct left alone, the parents' lookup); a revert is enough. With `=1` the lookup costs more than unset in the micro-benchmark (15.6 against 12.9 ns a lookup). A player notices nothing after updating: no save, option or cache changes. ## 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 - [ ] A switch added or changed: it ships at the default the measurements chose, with its row in `gpu/vk/switches.def`. - [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] 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.
Each of the four is an HLE function that reads or writes one field of the calling thread's record. Called from lifted
code it pays the way out: the caller's registers synced to the context and back, the hooked definition's tail call,
HOS_HLE_STATS's test and the host thread-local. nx2/guest_os.py rewrites each direct call, devirtualised call and tail
call of their hooked definitions into the field access itself through Ctx.thread (the HosThread running the context);
GetThreadPriority only about the calling thread's own ThreadType, the hooked definition as before about another. Calls
through a pointer still reach the HLE functions. HosThread now starts with guest_tt, tls_slots and priority, at offsets
_Static_assert'ed in host/hos_thread.c. Lift time, on by default (bit-exact against the HLE functions by test);
A64_OS_INLINE=0 lifts as before. HOS_HLE_STATS=1 no longer counts the inlined calls.

tests/test_guest_os.py: the rewrite (calls, the devirtualised arm, tail calls, other calls kept, =0 and a lift without
the hooks unchanged), the wiring in nx2/lift.py and nx2/chunks.py, and the four HLE functions cut from hos_thread.c
against the inlined C over two threads' records, 16,000 calls; --bench times GetTlsValue and GetThreadPriority called
and inlined.

From 4d98f819 (its lifter and HosThread half) and bb27910b on exp/x86-asm-sync. vc_kick stays where main has it (the
mutex change that reads it from the context is a branch of its own).

Assisted-by: Claude Code
tests/test_spawn_emit.py gains the os_inline configuration: four functions of its made-up image hooked to nn::os's
GetCurrentThread, GetTlsValue, SetTlsValue and GetThreadPriority, reached by BLs, a devirtualised BLR's likely arm, a
B (a tail call) and a PLT stub, emitted by the real lifter with A64_LOCAL_REGS=4 in 1 process and in 3 spawned ones
(nx2/guest_os.py installed at the file's module level, as nx2/lift.py does). The same files both ways; each site is the
field access through HosThreadHead (TLS slots as its array, slots and priority atomic), the tail site's other-thread
arm a TAILCALL, the PLT stub's body left whole, only functions that call a hooked one scanned, and the files holding
sites compile against the generated a64.h. With A64_OS_INLINE=0, no site.

tests/test_guest_os.py: the rewrite's tail site for another thread's priority keeps its TAILCALL, also through
nx2/split_giants.py's pieces; the C fixture reaches the record as HosThreadHead, cuts with tests/srccut.py, and runs
the tail form too; the compiler is --cc, then CC, then LONGSHOT_MINGW's clang (Windows), then clang, gcc or cc
(RUN_ALL_ARGS no longer forces gcc).

Assisted-by: Claude Code
- The inlined C reached tls_slots by indexing past guest_tt, a scalar member. The record's head is now a type,
  HosThreadHead (nx2/rtgen.py, in the generated a64.h, which the host includes too); host/hos_thread.c asserts its
  HosThread fields at those offsets, and the lifted code indexes HosThreadHead's tls_slots array.
- A TLS slot (the virtual-core scheduler writes a running thread's core cell, vc_sync_tls) and the priority
  (ChangeThreadPriority, svcSetThreadPriority) are written by other threads: every access is atomic, relaxed, in the
  lifted code, the GetTlsValue/SetTlsValue HLE functions, vc_pref_core and vc_sync_tls, hos_thread_priority (an
  exchange when it sets) and every scheduler and diagnostic read of the priority (hos_thread_prio). The unused
  hos_tls_slots, which handed out the array, is gone.
- GetThreadPriority about another thread at a tail site is a TAILCALL of the hooked definition again (nx2/split_giants.py
  keeps it a tail call of the dispatcher), not a call followed by a return.
- A PLT stub's emitted body is left whole (a function pointer can enter it), and a function whose refs name none of
  the hooked definitions is not scanned.
- HOS_HLE_STATS: the four functions' counts are partial in a default lift. scripts/perf.py cpu --calls says so in its
  report, and docs/dev/reference/build.md and profiling.md say to lift with A64_OS_INLINE=0 for full counts.
- tests/test_vcore_escape.py and test_vcore_mask0.py cut hos_thread_prio with the scheduler code that now calls it.

Assisted-by: Claude Code
On main both stop at their own harness: test_vcore_escape.py declares hos_fps_timing twice (volatile and not), and
test_vcore_mask0.py's cut virtual-core block calls env_on_or without gpu/vk/compat/longshot_env.h, which
test_vcore_escape.py already includes. One declaration is left, and the mask-0 fixture includes the header as the escape
fixture does. Nothing else in either test changes.

Assisted-by: Claude Code
The lifted code reads the record through Ctx.thread as HosThreadHead; tests/test_guest_os.py now checks that the record's
first member is that type (named head), and its C fixture, compiled at -O2 with the compiler's default aliasing rules,
reaches the records' fields through it and asserts the head's offset. It fails until hos_thread.c's HosThread embeds the
head.

Assisted-by: Claude Code
fix(hle): HosThreadHead is HosThread's first member, so the lifted code and the host reach the same typed object
Some checks are pending
CI / Workflow lint (actionlint) (pull_request) Blocked by required conditions
CI / Website build and checks (pull_request) Blocked by required conditions
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
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
PR description / Description follows the template (pull_request_target) Successful in 8s
Website / Website on Cloudflare (pull_request_target) Has been skipped
beanstalk/approval waiting for a BigBean's 👍 on the request
5a4e7a5b1e
HosThread repeated HosThreadHead's fields at its offsets (asserted), and the lifted code read them through Ctx.thread
cast to the separate HosThreadHead type: equal layout, but two distinct struct types, so the access relied on how the
compiler treats aliasing between them (the game's build disables strict aliasing; tests/test_guest_os.py's fixture
compiles at -O2 with the default). HosThread now begins with `HosThreadHead head;`: the pointer to the record is a
pointer to its first member, and the host reads and writes guest_tt, tls_slots and priority through it. The offset
asserts are replaced by one that HOS_TLS_SLOTS is the head's slot count. The C inlined at the call sites is unchanged.

The tests that hold HosThread's fields (test_vcore_escape.py, test_vcore_mask0.py, test_thread_arg_self.py) reach them
through head.

Assisted-by: Claude Code
mutex_lock's spin and sleep, taken only when its immediate acquire fails (another thread holds the mutex, or it is
reserved for a woken condition variable waiter), are moved word for word into mutex_lock_wait (noinline, cold), so the
uncontended lock (owner check, one compare-exchange) is small enough for the compiler to inline into its callers and
saves no registers and sets up no frame of its own for the wait; it still calls hos_self (in hos_thread.c).
tests/test_cv_handoff_threads.py cuts the new function too (from its name on, so its attributes may change).

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
Assisted-by: Claude Code
The two hottest nn::os calls found the calling thread through the host thread-local (hos_self) and ran the virtual-core
checkpoint as a call on every lock and unlock. Their context already names the thread (Ctx.thread, the HosThread running
it; a fiber's context takes the thread's at each switch), so they take it from there: mutex_lock and mutex_unlock take
the thread as an argument, and obj_at tests the thread's vc_kick through the context (hos.h hos_vcore_checkpoint_at) and
calls hos_vcore_checkpoint only when a better waiter asked for the core, which reads the kick again as before. vc_kick
moves to just after the HosThread head (offset 272, _Static_assert'ed); nothing else moves. Not switched: the same
thread and the same checkpoint, without the thread-local reads and the call.

tests/test_cv_handoff_threads.py: the fixture's contexts carry their thread, and the LockMutex/UnlockMutex HLEs are checked
to take the context's thread, which is the calling thread.

From 4d98f819 (its nn_os_sync.c half) on exp/x86-asm-sync. On top of perf/os-inline (the HosThread head) and
perf/mutex-lock-wait-cold's 1e6a4721 (cherry-picked: mutex_lock_wait's signature).

Assisted-by: Claude Code
tests/test_cv_handoff_threads.py compiled its own obj_at and hos_ctx_thread and gave every context hos_self() as its
thread, so LockMutex or UnlockMutex going back to obj() or to hos_self() still passed. The fixture now cuts obj and
obj_at from host/nn_os_sync.c and hos_ctx_thread, hos_vcore_checkpoint_at and HOS_THREAD_VC_KICK from host/hos.h; its
thread record has vc_kick at that offset, and hos_vcore_checkpoint (hos_thread.c's gives the core up) counts its calls,
numbered in one sequence with the fixture's lookups (obj_nockpt).

New cases ("ctx"): the two entries through a context whose thread is a record other than hos_self(). The mutex is owned
and let go by that record; with no kick, and with one set on hos_self()'s record, neither entry calls the checkpoint;
with one set on the context's record each calls it once, before its lookup. Broken variants that must fail: either
entry through obj() or obj_nockpt(), or taking hos_self(); hos_vcore_checkpoint_at testing hos_self()'s kick; obj_at
looking up before its checkpoint.

Assisted-by: Claude Code
A waiting thread sets the running thread's vc_kick with an atomic store (vc_acquire) while that thread may be entering
LockMutex or UnlockMutex, and hos_vcore_checkpoint reads it with __atomic_load_n. The context's test read it through a
volatile pointer: a data race in C's terms. It now reads it as hos_vcore_checkpoint does, __atomic_load_n, relaxed.

Assisted-by: Claude Code
The offset was a bare number (268, then 272 once #1956 made HosThreadHead HosThread's first member), which hos.h and
hos_thread.c had to agree on by hand. vc_kick is declared right after the embedded head, so its offset is the head's
size: hos.h, which includes the generated a64.h, says so, and hos_thread.c keeps its assert. The CV fixture builds its
thread record the same way, from nx2/rtgen.py's HosThreadHead.

Assisted-by: Claude Code
fix(os): hos_vcore_checkpoint_at reads vc_kick through a volatile-qualified pointer, as it is declared
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 4s
d111a64354
vc_kick is a volatile int; reading it through a plain const int * is undefined (C11 6.7.3p6). The relaxed atomic load
now goes through const volatile int *. The same relaxed load.

Assisted-by: Claude Code
Beside #1781's sized cache (HOS_OBJ_CACHE, 16384 entries of four ways by default), a second shape of the same 16384
entries to compare it with: 2048 sets of 8 ways, a set one 64-byte line of 8 tags and the 8 objects' numbers (128 KB a
thread, made at its first lookup), an address in either of two sets by two hashes, so no set overflows before the
cache is nearly full; a miss takes a stale way of the address's, else an empty one, else one at random. The table now
numbers its objects as it makes them (chunks of 1024, never freed, as objects never are), so an entry is a 32-bit
number and a set holds eight. Counted in the existing sync: object cache log line. Off unless HOS_OBJ_CACHE=sets,
which obj_cache_entries reads with the size, once (OBJ_CACHE_SETS; no string compare on the lookup's path).

tests/test_obj_cache_size.py runs the sets too: obj_cache_entries("sets") and its near misses, the 4,000-object walk's
later passes miss under 2 %, every lookup is the table's object over 200,000 random operations with objects re-made,
buried and reused, the log line; --bench times it beside the other sizes. tests/test_multiwait_wake.py allows
__builtin_ctz under mw_lock (the compiler's, one instruction: the set's ways are found with it).

From 3cf76270 on exp/x86-asm-sync, where it replaced the 1,024-entry cache (its HOS_OBJ_CACHE_SMALL switch is main's
HOS_OBJ_CACHE=0 now).

Assisted-by: Claude Code
tests/test_obj_cache_size.py gains two modes:
- reuse (with HOS_OBJ_CACHE=sets, unset and 0): an address's object is dropped, buried and, after the grave's 30 s, made a
  spare; a lookup of the old address through its stale cache entry reads the object's identity, and the table gives the
  spare to a new address right after that lookup's first read of it (the harness runs the reuse after the n-th atomic
  load of the object's key or kind), with the same kind and with another; before the lookup as the control. The lookup
  must return its own address's object, and the spare must have gone to the new address. It fails here for the same kind
  in all three caches: each reads the key, then the kind, so the old key and the new kind pass.
- churn (sets): 64 threads made and ended one after another, each looking 4,000 objects up, with calloc and free of 64 KB
  and up counted: what is live after them must be what was live before. It fails here: each thread leaves its 128 KB.

The sets case of the walk now has to miss at most 10 of 4,000 on its last pass (the four-way default misses 47), so a sets
dispatch that fell back to the default would fail. Each fixture run clears HOS_KEEP_DEAD_OBJECTS and HOS_SYNC_TABLE_LOCK,
which the cut code reads, as well as the two cache switches; the compiler is --cc, then CC, then LONGSHOT_MINGW's clang,
then cc, clang or gcc on PATH (RUN_ALL_ARGS no longer forces clang).

Assisted-by: Claude Code
- Identity: every cache (sets, the sized default, the fixed one) and find_unlocked checked an object by its key, then its
  kind (acquire). The table gives a spare to another address by storing kind 0, the new key, then the new kind (release):
  a lookup could read the old key, then the new kind, and take the spare, now another address's, for the old one (with
  the same kind), up to holder_ready under mw_lock. obj_is reads the kind first (acquire), then the key: a reader that
  sees the new kind sees the new key. main's pointer caches had the same order; the same two loads swapped fix them. What
  a lookup returns stays that address's until the guest drops it: objects are never freed, and a dropped one is reused
  only after 30 s in the graves and not while a condition variable waiter or a multi-wait holds it.
- A thread's sets are freed as it ends, from the allocation's base: an FLS slot's callback on Windows (ExitThread too),
  a pthread key's destructor on Linux (the POSIX layer's ExitThread is pthread_exit), the slot or key made once. If the
  cache or its owner cannot be had, the thread goes without, not asking again at every lookup.
- Objects are numbered (made in chunks of 1024) with HOS_OBJ_CACHE=sets only; otherwise each is made alone, as before. An
  object that cannot be had ends the process with hos_fatal, not a write through NULL.
- HOS_NO_OBJ_CACHE and HOS_OBJ_CACHE are read once into one atomic word (obj_cache_mode_read), as is
  HOS_SYNC_TABLE_LOCK: two first lookups at once no longer race on plain statics.
- The comment and the switch row (now a string: sets is a word) say the replacement policy as it is (stale way, else
  empty, else the next of the two sets' 16 ways in turn) and the memory (128 KB a thread against the default's 256 KB).
- tests/test_multiwait_wake.py allows the once-only calls and hos_fatal under mw_lock: none sleeps or waits for a core.

tests/test_obj_cache_size.py passes (reuse with sets, unset and 0; churn).

Assisted-by: Claude Code
tests/test_obj_cache_size.py:
- reuse now runs on every way a lookup reads an object without table_lock: sets, the sized default, the fixed cache,
  and no cache (HOS_NO_OBJ_CACHE=1, the table's unlocked path). The object an address's lookup will read is reassigned
  by the table's own paths (dropped, buried, after the grave's 30 s a spare, taken) to another address of the same
  kind, of another kind, or to another address and then back to its own as another kind; before the lookup, and right
  after each of the lookup's atomic loads in turn (the harness counts every load the cut code makes outside its locks,
  where a writer could come in), until the lookup has no load left. The lookup must return the table's object of that
  address and kind, or the old object only if it read the object's identity wholly before the reassignment. It fails
  here in all four: a kind read before the reassignments, then the address read back after them, return the address's
  new event for its old mutex.
- fiber (sets, Windows): a thread makes its cache on a guest fiber (a Win32 fiber, as hos_thread.c runs the game's),
  switches back, deletes the fiber and looks the same 4,000 objects up again; the harness keeps a freed allocation of
  64 KB and up filled with 0xA5. The cache must still be live and hit, nothing may be written in freed memory, and it is
  freed as the thread ends. It fails here: the FLS slot's callback frees the cache with the fiber, and the thread's
  lookups write into the freed memory.

Assisted-by: Claude Code
The cache's pointer is a thread-local, but its release was an FLS slot's callback, which follows the fiber that made
the cache: a guest fiber is a Win32 fiber (hos_thread.c, fibers) that may run on several threads in turn and that the
game deletes (FinalizeFiber) while the thread that ran it runs on, so deleting it freed the cache under that thread,
whose lookups then read and wrote the freed memory. Now pointer and release have the same owner, the host thread: on
Windows the image's TLS callback (DLL_THREAD_DETACH, run by the loader on the ending thread with its thread-locals still
there, ExitThread too, whatever fiber it runs) frees it from its base; elsewhere the pthread key's destructor, as
before. A lookup after it on that thread goes without. tests/test_multiwait_wake.py drops InitOnceExecuteOnce and
FlsSetValue from what may be called under mw_lock (no longer called).

tests/test_obj_cache_size.py fiber and churn pass.

Assisted-by: Claude Code
obj_is read the kind, then the key: a kind read before a reassignment could pair with a key read after it, and an
object given to another address and then back to its own as another kind passed for the old kind (a mutex lookup
returning the address's new event). Every lookup without table_lock uses obj_is: the sets cache, the sized default, the
fixed cache and find_unlocked. An object now has a seq beside its life (kept as life is): obj_lookup steps it to odd,
with a release fence, before it gives the object another identity (a spare taken by an address, a tombstone made again
at its address) and back to even (release) after; obj_is takes the key and kind only if seq was even and the same
before and after them (acquire, then an acquire fence before the second read), so both are of one life. A new object
needs no step: no reader has it before its slot is published. The cost on a hit: two more loads of the object's first
cache line and a compiler barrier on x86-64. tests/test_multiwait_wake.py allows __atomic_thread_fence under mw_lock.

tests/test_obj_cache_size.py reuse passes with sets, unset, 0 and HOS_NO_OBJ_CACHE=1.

Assisted-by: Claude Code
The harness set an object's kind to 0 itself to make a tombstone (the reuse mode's drops and the check mode's
finalizations); it now calls drop, cut from host/nn_os_sync.c with the table, so a tombstone is whatever drop makes. The
reuse mode's watch (a call after every atomic load of the cut code) is left out of the --bench build (OBJ_BENCH): it
measured the watch with the loads, a call for each, which made a check with more loads look dearer than it is in the
game, where nothing follows a load.

tests/test_obj_cache_size.py passes as before; reuse and fiber still fail on 9baebe0d's source.

Assisted-by: Claude Code
obj_is read a sequence, the kind, the key and the sequence again (four loads and an acquire fence) to take the key and
kind of one life; the micro-benchmark (built with the test's call after each load) put that at about 3 ns a lookup in
every cache mode, the default included. An object now keeps its key and kind as one 64-bit word as well (ident: the
kind in the top 4 bits over a key under 2^60, a guest address; 0 for a tombstone or a larger key), and obj_is compares
that word, read once (acquire), with the one the lookup makes from its key and kind: one load, so a kind never pairs
with another identity's key. Every write of a kind goes through obj_kind_set, which writes the word after it (release);
an object gets another identity only as a tombstone, whose word is 0, and gets the new word last, so the sequence, its
steps and the fences go. An object with a key of 2^60 or more is never a cache hit (its word is 0) and is found under
table_lock. An object grows by 8 bytes (the word; the sequence's 4 went). tests/test_multiwait_wake.py no longer allows
__atomic_thread_fence under mw_lock (none is made).

tests/test_obj_cache_size.py reuse (sets, unset, 0, HOS_NO_OBJ_CACHE=1), fiber, churn and check pass.

Assisted-by: Claude Code
A mutex's object number is kept in its guest MutexType's internal critical-section word (+0x18; nn::os's own mutex code
was its only user, and the HLE functions replace all of it). obj_at, LockMutex's and UnlockMutex's lookup, reads it: with
2 (the default) before the per-thread cache, with =1 only where the cache misses, before the table (obj_miss, through a
thread-local pointer obj_at sets around its lookup). The object it names is taken only while it is still that address's
mutex (obj_is: its key and kind as one word, as the caches check an entry); anything else there (0, the game's bytes, a number
out of range or not given yet, another object's) is never taken: the lookup as before, after which the word is written
and marked. The word is read and written with relaxed atomics, and only on a 4-byte boundary inside the guest window;
otherwise the lookup alone. While it is on the table numbers its objects (chunks of 1024), as HOS_OBJ_CACHE=sets does,
so the word works with every cache.

HOS_MUTEX_GUEST_ID is read once through an atomic word, as HOS_OBJ_CACHE (obj_cache_mode_read): at the first LockMutex
or UnlockMutex or the first object made. Unset: 2; 0 or another of env_on's off values (empty, false, off, no): off, the
guest struct left alone and the parents' lookup, with one relaxed load of the mode in obj_at; exactly 1: 1; any other
value: 2, as unset. 2 is the value the performance branch ran it at, and the one that spares a mutex found through its
word both the cache and the table; =0 is the kill switch for a change that writes a word of guest memory. Not measured
in play yet.

tests/test_obj_guest_id.py: the real table, caches, lookup and switch read (as test_obj_cache_size.py cuts them) with
obj_at and obj_by_word; HOS_MUTEX_GUEST_ID unset, 0, off, 1, 2, 3, 12 and on, each in a fresh process with the child's
environment set, with each cache (sized, fixed, sets, none): the value read (unset 2; 0 and off: off; exactly 1: 1;
else 2) and kept when the variable is flipped after the first lookups; the table's object every time, through 100,000
operations with mutexes finalized (drop, cut with the table), made again and re-made as another kind; words written and
marked only when on, fixed when they name anything else, no table lookup once right (2: no cache lookup either); off
without sets, objects unnumbered as before; an object with no number, a word outside the window and one off a 4-byte
boundary are left alone;
only the mutexes' words change. The compiler is test_obj_cache_size.py's (--cc, CC, LONGSHOT_MINGW's clang, a C compiler
on PATH).
tests/test_cv_handoff_threads.py: its fixture has the switch at its default (mutex_guest_id 2, obj_by_word its lookup)
and its kick-after-lookup variant moves the checkpoint past obj_at's new lookup line. tests/test_obj_cache_size.py
clears HOS_MUTEX_GUEST_ID for its runs (its cut now reads it: they run at the default).

From c565d05a, 02be0b02 and 064574ab on exp/x86-asm-sync, where it sat in the set cache's obj_find. Needs the object
numbers of perf/obj-cache-sets (#1969) and obj_at of perf/mutex-ctx-thread (#1957).

Assisted-by: Claude Code
tests/test_obj_guest_id.py locks a mutex at 0xFFFFFFFFFFFFFFF0: its address plus MUTEX_GUEST_WORD (+0x18) wraps round
to guest address 8, inside the window. obj_by_word checks only the word's own 4 bytes (hos_guest_range(key + 0x18, 4)),
so it writes the mutex's number there, at the window's start, and the check "only the mutexes' words changed" fails
with the switch on (unset, 1, 2: every cache). The mutex itself is still the table's.

Assisted-by: Claude Code
fix(os): HOS_MUTEX_GUEST_ID checks a mutex's struct up to its word as one guest range, so no address wraps round
Some checks failed
PR description / Description follows the template (pull_request_target) Successful in 9s
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
Website / Website on Cloudflare (pull_request) Has been cancelled
bca5fcc928
obj_by_word checked only the word's 4 bytes at key + MUTEX_GUEST_WORD: a mutex address within 0x18 of 2^64 wrapped
round to the guest window's first bytes, and the mutex's number was written there. It now checks the struct from its
address to the end of the word (hos_guest_range(key, MUTEX_GUEST_WORD + 4)), which has no wrap: such a mutex is looked
up as before and no guest byte is touched. tests/test_obj_guest_id.py (the commit before) passes again.

Assisted-by: Claude Code
perf(os): HOS_MUTEX_GUEST_ID is off unless set, as an experiment while it is measured
Some checks failed
PR description / Description follows the template (pull_request_target) Successful in 7s
beanstalk/approval waiting for a BigBean's 👍 on the request
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
Website / Website on Cloudflare (pull_request) Has been cancelled
2f7195ca81
futondestroyer's answer for the host experiments (#1966, comment 67223): keep the draft experiment off while the
enabled and disabled results are gathered. Unset now reads as 0: LockMutex and UnlockMutex look their object up
through the per-thread cache and the table, the guest MutexType is left alone and the sync objects are numbered only
with HOS_OBJ_CACHE=sets, as before this change (obj_at keeps one relaxed load of the mode). =1 and =2 keep their
meanings (the word read only where the cache misses; before the cache), any other on value is 2, the off values
(0, empty, false, off, no) are off.

gpu/vk/switches.def: the row is an attempt, off unset. tests/test_obj_guest_id.py: unset expects off (no guest write,
no numbers without sets), and runs again last, each in a fresh process with 0, off, 1, 2, 3, 12 and on.
tests/test_cv_handoff_threads.py: its fixture has the switch at the default, off (obj_nockpt the lookup).

Assisted-by: Claude Code
Author
Contributor

Pushed 2f7195ca on top of bca5fcc9 (the earlier commits unchanged): HOS_MUTEX_GUEST_ID is now off unless set, as futondestroyer answered for the host experiments (#1966 (comment)); =1 and =2 keep their meanings. The code's default, the test's unset case (off, run twice), the fixture of tests/test_cv_handoff_threads.py and the gpu/vk/switches.def row (now an attempt) changed together. The description's default line, Measurements (the earlier measurement, about 1 % inside the runs' spread, and nothing measured on this head yet), Still to test (the A/B is now unset against =2) and Tested (the checks on 2f7195ca) are updated.

Pushed 2f7195ca on top of bca5fcc9 (the earlier commits unchanged): `HOS_MUTEX_GUEST_ID` is now off unless set, as futondestroyer answered for the host experiments (https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1966#issuecomment-67223); `=1` and `=2` keep their meanings. The code's default, the test's unset case (off, run twice), the fixture of `tests/test_cv_handoff_threads.py` and the `gpu/vk/switches.def` row (now an attempt) changed together. The description's default line, Measurements (the earlier measurement, about 1 % inside the runs' spread, and nothing measured on this head yet), Still to test (the A/B is now unset against `=2`) and Tested (the checks on 2f7195ca) are updated.
siantm changed title from WIP: perf(os): LockMutex and UnlockMutex find their host object through a number kept in the guest MutexType (HOS_MUTEX_GUEST_ID, default 2) to WIP: perf(os): LockMutex and UnlockMutex find their host object through a number kept in the guest MutexType (HOS_MUTEX_GUEST_ID, off unless set) 2026-11-21 17:07:37 +00:00
Author
Contributor

@Beanstalk This adds HOS_MUTEX_GUEST_ID, off by default: with HOS_MUTEX_GUEST_ID=2 the guest mutex lookup goes through a number kept in the guest's MutexType word (host/nn_os_sync.c); unset, it is main's lookup. It is on #1957 and #1969, whose commits are in its diff (so the build lifts the game again, as #1957's does). Please start from the market-entrance save and play Castle Town Market for two minutes, the PR against main on the same PC, on the PR's side with the switch unset and with HOS_MUTEX_GUEST_ID=2, at least two starts a side, on Windows and Linux PCs with an AMD processor (matched by CPU model) and on the Steam Deck. The same game, no stall, hang or crash; unset, frame times as main's; =2, frame time p99 equal to unset or better, within the run-to-run spread.

@Beanstalk This adds HOS_MUTEX_GUEST_ID, off by default: with HOS_MUTEX_GUEST_ID=2 the guest mutex lookup goes through a number kept in the guest's MutexType word (host/nn_os_sync.c); unset, it is main's lookup. It is on #1957 and #1969, whose commits are in its diff (so the build lifts the game again, as #1957's does). Please start from the market-entrance save and play Castle Town Market for two minutes, the PR against main on the same PC, on the PR's side with the switch unset and with HOS_MUTEX_GUEST_ID=2, at least two starts a side, on Windows and Linux PCs with an AMD processor (matched by CPU model) and on the Steam Deck. The same game, no stall, hang or crash; unset, frame times as main's; =2, frame time p99 equal to unset 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. This request was for 2f7195c, and the PR has changed since (179e994), so a 👍 no longer approves it. Comment @Beanstalk again to ask for the new commit.

@siantm only members of `BigBeans` can start the farm. ~~One of them can approve this request by reacting 👍 to it or to this comment.~~ This request was for `2f7195c`, and the PR has changed since (`179e994`), so a 👍 no longer approves it. Comment `@Beanstalk` again to ask for the new commit.
siantm force-pushed perf/mutex-guest-id from 2f7195ca81
Some checks failed
PR description / Description follows the template (pull_request_target) Successful in 7s
beanstalk/approval waiting for a BigBean's 👍 on the request
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
Website / Website on Cloudflare (pull_request) Has been cancelled
to 179e99477e
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
PR description / Description follows the template (pull_request_target) Successful in 5s
beanstalk/approval waiting for a BigBean's 👍 on the request
2026-11-21 17:19:25 +00:00
Compare
Author
Contributor

Restacked on #1969 at 617c4fba (its two new commits cherry-picked unchanged; #1957 and this PR's four commits unchanged but for conflict context in host/nn_os_sync.c): new head 179e9947, its checks rerun and passing (Tested).

Restacked on #1969 at 617c4fba (its two new commits cherry-picked unchanged; #1957 and this PR's four commits unchanged but for conflict context in `host/nn_os_sync.c`): new head 179e9947, its checks rerun and passing (Tested).
Author
Contributor

@Beanstalk This adds HOS_MUTEX_GUEST_ID, off by default: with HOS_MUTEX_GUEST_ID=2 LockMutex and UnlockMutex find their host object through a number kept in the guest MutexType (host/nn_os_sync.c); unset, it is main's lookup. It sits on #1957 (d111a643, itself on #1956, so the build lifts the game again) and #1969 (617c4fba), so its diff against its merge base includes those PRs' commits; the farm cannot take them as the comparison, so please compare with main. Please start from the market-entrance save and take the picture right after it loads and settles, before any input, with a main-against-itself picture on the same PC at that point; then, for frame times only, play Castle Town Market for two minutes; at least two starts a side, interleaved, the PR against main on the same PC, on the PR's side unset (the default, off) and with HOS_MUTEX_GUEST_ID=2 (on), on Windows with a Ryzen 9 7900X, Windows with a Core i9-12900K, Linux with a Ryzen 5 5600X and the Steam Deck. The unset and =2 arms on the PR's side show this PR's own effect. The same picture as main within main's own difference from itself; unset, frame time p99 equal to main's; with HOS_MUTEX_GUEST_ID=2, equal to unset or better, within the run-to-run spread. The log must have no FATAL line, and no run may stall or hang.

@Beanstalk This adds HOS_MUTEX_GUEST_ID, off by default: with HOS_MUTEX_GUEST_ID=2 LockMutex and UnlockMutex find their host object through a number kept in the guest MutexType (host/nn_os_sync.c); unset, it is main's lookup. It sits on #1957 (d111a643, itself on #1956, so the build lifts the game again) and #1969 (617c4fba), so its diff against its merge base includes those PRs' commits; the farm cannot take them as the comparison, so please compare with main. Please start from the market-entrance save and take the picture right after it loads and settles, before any input, with a main-against-itself picture on the same PC at that point; then, for frame times only, play Castle Town Market for two minutes; at least two starts a side, interleaved, the PR against main on the same PC, on the PR's side unset (the default, off) and with HOS_MUTEX_GUEST_ID=2 (on), on Windows with a Ryzen 9 7900X, Windows with a Core i9-12900K, Linux with a Ryzen 5 5600X and the Steam Deck. The unset and =2 arms on the PR's side show this PR's own effect. The same picture as main within main's own difference from itself; unset, frame time p99 equal to main's; with HOS_MUTEX_GUEST_ID=2, equal to unset or better, within the run-to-run spread. The log must have no FATAL line, and no run may stall or hang.
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

@Beanstalk One correction to my request 69722 above, for its picture point: please take each picture 5 s (300 frames at 60 fps) after the first frame whose picture is not a single uniform colour, with no input in between, and take the main-against-itself picture from the same point. A picture taken during the load's fade-in from black is not comparable: run 70269 on #2156 caught the fade in 3 of 4 starts.

@Beanstalk One correction to my request 69722 above, for its picture point: please take each picture 5 s (300 frames at 60 fps) after the first frame whose picture is not a single uniform colour, with no input in between, and take the main-against-itself picture from the same point. A picture taken during the load's fade-in from black is not comparable: run 70269 on #2156 caught the fade in 3 of 4 starts.
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

Description updated: Measurements now has a micro-benchmark of the mutex lookup at 179e9947 on authored input, not measured in play (15 interleaved rounds, run alone on this PC, ns a lookup, median (rounds' range)): HOS_MUTEX_GUEST_ID unset 12.86 (12.80 to 13.24), =1 15.60 (15.49 to 16.17), =2 5.39 (5.08 to 5.49); with HOS_OBJ_CACHE=sets 13.18, 16.18 and 5.05. =1, the cache-miss form (the word read only where the per-thread cache misses), is slower than unset here, so it gives no reason to set =1. The switch stays off; the in-game comparison is still to come. The description also has the template's newer sections (TL;DR, Breakdown, Flags and settings, the Everyone checklist).

Description updated: Measurements now has a micro-benchmark of the mutex lookup at 179e9947 on authored input, not measured in play (15 interleaved rounds, run alone on this PC, ns a lookup, median (rounds' range)): `HOS_MUTEX_GUEST_ID` unset 12.86 (12.80 to 13.24), `=1` 15.60 (15.49 to 16.17), `=2` 5.39 (5.08 to 5.49); with `HOS_OBJ_CACHE=sets` 13.18, 16.18 and 5.05. `=1`, the cache-miss form (the word read only where the per-thread cache misses), is slower than unset here, so it gives no reason to set `=1`. The switch stays off; the in-game comparison is still to come. The description also has the template's newer sections (TL;DR, Breakdown, Flags and settings, the Everyone checklist).
Author
Contributor

Description corrected to the current template: the TL;DR, Breakdown, Flags and settings and Everyone sections added in the last update are taken out again (they are not in main's template). The figures and the text of the last update stay as they were.

Description corrected to the current template: the TL;DR, Breakdown, Flags and settings and Everyone sections added in the last update are taken out again (they are not in `main`'s template). The figures and the text of the last update stay as they were.
Contributor

Steam Deck result: I ran this PR with HOS_MUTEX_GUEST_ID unset against =2, on a Steam Deck OLED in the Market (the arrival, then two minutes standing), three pairs. It showed no difference the Deck can resolve (+0.6 % on the median with =2, the runs overlapping), and no hang, crash or error in any log.

Before, HOS_MUTEX_GUEST_ID unset After, HOS_MUTEX_GUEST_ID=2 Δ
fps (each run) 26.0 / 26.2 / 27.3 (median 26.2) 26.4 / 28.0 / 26.0 (median 26.4) +0.6%
1% low fps (1000 / p99) 16.4 17.1 +4.0%
median frame time 36.59 ms 36.46 ms -0.13 ms
p99 frame time 60.81 ms 58.49 ms -2.32 ms
frames of 50 ms or more (each run) 216 / 188 / 108 190 / 64 / 206

dev.py ab's verdict: no difference shown: +0.6% fps (median 26.4 against 26.2), within the runs' spread (A 26.0 to 27.3, B 26.0 to 28.0). Run order A1 B1 A2 B2 A3 B3. The p99 and 1 % low look a little better with =2, but they follow the fastest run of the six (B2, 28.0 fps, 64 long frames); the other two =2 runs sit with the unset ones. So frame times with =2 were as unset or better, within what this Deck resolves. (Both columns are this PR's build, longshot v3.0.0-171-g2fa57ab85 in every log; the =2 runs' switches set line lists HOS_MUTEX_GUEST_ID=2 (attempt).)

From the logs: no FATAL, device-lost, VK_ERROR, validation, hang or crash line in the 6 logs; every run exited normally. The vcores' waited ... ms for a core lines (the start and the arrival's loading threads, one of 745-759 ms on each side, PreActorModelCompose behind sead::MainThread) came 107 / 49 / 76 times unset and 61 / 41 / 61 times with =2; no ran without a core line on either side. Every log reads vk: Threaded rendering: Auto: off.

The PR's own check, on Linux (the PC that built it), on main 9dd41326 with this PR merged: tests/test_obj_guest_id.py passes (run once, without the sanitizers).

Hardware: Steam Deck OLED, SteamOS 3.9.5, RADV VANGOGH, Mesa 26.2.0-devel, GPU clock manual 1600 MHz, headless.
Settings: Minimum, Render 50 % (640x400), output 1280x800, uncapped, V-Sync and frame generation off.
Game data: complete (the dump, with the three damaged textures replaced).
Built: main 9dd41326 with this PR's head 179e9947 merged in, which carries what it is stacked on (#1969's commits up to 617c4fba and #1957's d111a643, with #1956's lift change: the game lifted again), no conflicts, x86-64-v3, shaders prepared on the Deck. Both sides are this one build; only the variable differs.
Measured: dev.py ab, Market, 3 interleaved pairs of 120 s after the warm-up (until no frame waits for a pipeline, at least 10 s), the median (the project's tool, with a local change to compare two prebuilt runtimes, since the Deck does not build).

Limits: the Market as dev.py ab reaches it (a teleport to the child Market, then standing), not the market-entrance save; I didn't run this PR against main, since what's under it changes much more than this switch does. The switch isn't read through HOS_AB, so an in-run comparison wasn't possible; in the Market the Deck's runs vary by 5-10 % from start to start, so a gain of the 1 % measured on the desktop would not show.

Steam Deck result: I ran this PR with `HOS_MUTEX_GUEST_ID` unset against `=2`, on a Steam Deck OLED in the Market (the arrival, then two minutes standing), three pairs. It showed no difference the Deck can resolve (+0.6 % on the median with `=2`, the runs overlapping), and no hang, crash or error in any log. | | Before, HOS_MUTEX_GUEST_ID unset | After, HOS_MUTEX_GUEST_ID=2 | Δ | |---|---|---|---| | fps (each run) | 26.0 / 26.2 / 27.3 (median 26.2) | 26.4 / 28.0 / 26.0 (median 26.4) | +0.6% | | 1% low fps (1000 / p99) | 16.4 | 17.1 | +4.0% | | median frame time | 36.59 ms | 36.46 ms | -0.13 ms | | p99 frame time | 60.81 ms | 58.49 ms | -2.32 ms | | frames of 50 ms or more (each run) | 216 / 188 / 108 | 190 / 64 / 206 | | `dev.py ab`'s verdict: **no difference shown: +0.6% fps (median 26.4 against 26.2), within the runs' spread (A 26.0 to 27.3, B 26.0 to 28.0).** Run order A1 B1 A2 B2 A3 B3. The p99 and 1 % low look a little better with `=2`, but they follow the fastest run of the six (B2, 28.0 fps, 64 long frames); the other two `=2` runs sit with the unset ones. So frame times with `=2` were as unset or better, within what this Deck resolves. (Both columns are this PR's build, `longshot v3.0.0-171-g2fa57ab85` in every log; the `=2` runs' `switches set` line lists `HOS_MUTEX_GUEST_ID=2 (attempt)`.) From the logs: no `FATAL`, device-lost, `VK_ERROR`, validation, hang or crash line in the 6 logs; every run exited normally. The vcores' `waited ... ms for a core` lines (the start and the arrival's loading threads, one of 745-759 ms on each side, `PreActorModelCompose` behind `sead::MainThread`) came 107 / 49 / 76 times unset and 61 / 41 / 61 times with `=2`; no `ran without a core` line on either side. Every log reads `vk: Threaded rendering: Auto: off`. The PR's own check, on Linux (the PC that built it), on main 9dd41326 with this PR merged: `tests/test_obj_guest_id.py` passes (run once, without the sanitizers). Hardware: Steam Deck OLED, SteamOS 3.9.5, RADV VANGOGH, Mesa 26.2.0-devel, GPU clock manual 1600 MHz, headless. Settings: Minimum, Render 50 % (640x400), output 1280x800, uncapped, V-Sync and frame generation off. Game data: complete (the dump, with the three damaged textures replaced). Built: main 9dd41326 with this PR's head 179e9947 merged in, which carries what it is stacked on (https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1969's commits up to 617c4fba and https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1957's d111a643, with https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1956's lift change: the game lifted again), no conflicts, x86-64-v3, shaders prepared on the Deck. Both sides are this one build; only the variable differs. Measured: `dev.py ab`, Market, 3 interleaved pairs of 120 s after the warm-up (until no frame waits for a pipeline, at least 10 s), the median (the project's tool, with a local change to compare two prebuilt runtimes, since the Deck does not build). Limits: the Market as `dev.py ab` reaches it (a teleport to the child Market, then standing), not the market-entrance save; I didn't run this PR against main, since what's under it changes much more than this switch does. The switch isn't read through `HOS_AB`, so an in-run comparison wasn't possible; in the Market the Deck's runs vary by 5-10 % from start to start, so a gain of the 1 % measured on the desktop would not show.
Author
Contributor

@zeldaootr, thank you for the Steam Deck run and for writing its limits down so plainly. Your result is now in Measurements as your run, beside this PC's micro-benchmark of the lookup: on a Steam Deck OLED, one build of main 9dd41326 with this PR merged, HOS_MUTEX_GUEST_ID unset against =2, the Market as dev.py ab reaches it, three interleaved pairs of 120 s: no difference the Deck resolves (+0.6 % fps on the median, the runs overlapping), no hang, crash or error, and fewer waited ... ms for a core lines with =2 in each pair. It covers =2 against unset on the Deck and Linux; it does not cover this PR against main, the market-entrance save, or a gain as small as the desktop's micro-benchmark could suggest, as you said. The verdict stays as it was: no gain in play shown, and the switch stays off.

@zeldaootr, thank you for the Steam Deck run and for writing its limits down so plainly. Your result is now in Measurements as your run, beside this PC's micro-benchmark of the lookup: on a Steam Deck OLED, one build of main 9dd41326 with this PR merged, `HOS_MUTEX_GUEST_ID` unset against `=2`, the Market as `dev.py ab` reaches it, three interleaved pairs of 120 s: no difference the Deck resolves (+0.6 % fps on the median, the runs overlapping), no hang, crash or error, and fewer `waited ... ms for a core` lines with `=2` in each pair. It covers `=2` against unset on the Deck and Linux; it does not cover this PR against main, the market-entrance save, or a gain as small as the desktop's micro-benchmark could suggest, as you said. The verdict stays as it was: no gain in play shown, and the switch stays off.
Author
Contributor

A note on 71590: the "about 1 %" there is the earlier in-play result on the desktop (Kokiri Forest, =2 71.8 against 71.1 fps, inside the runs' spread), not the micro-benchmark. Still to test's Steam Deck item now says what the Deck run covered and what is still open there (this PR against main, from the market-entrance save).

A note on 71590: the "about 1 %" there is the earlier in-play result on the desktop (Kokiri Forest, `=2` 71.8 against 71.1 fps, inside the runs' spread), not the micro-benchmark. Still to test's Steam Deck item now says what the Deck run covered and what is still open there (this PR against main, from the market-entrance save).
Author
Contributor

@Beanstalk This replaces my request 69722 on this head and the corrections under it, in a form the farm can run: it cannot time a picture from the first frame that is not a uniform colour (its answer on #2131), so each picture here follows a fixed wait after the map event. This adds HOS_MUTEX_GUEST_ID, off by default: with HOS_MUTEX_GUEST_ID=2 LockMutex and UnlockMutex find their host object through a number kept in the guest MutexType (host/nn_os_sync.c); unset, it is main's lookup. It sits on #1957 (d111a643, itself on #1956, so the build lifts the game again) and #1969 (617c4fba), so its diff against its merge base includes those PRs' commits; the farm cannot take them as the comparison, so please compare with main. Please start from the market-entrance save and take the picture 10 s (600 frames at 60 fps) after the map event that ends the load, a fixed wait with no input in between (the farm cannot time it from the first frame that is not a uniform colour), with a main-against-itself picture on the same PC from the same point; then, for frame times only, play Castle Town Market for two minutes; at least two starts a side, interleaved, the PR against main on the same PC, on the PR's side unset (the default, off) and with HOS_MUTEX_GUEST_ID=2 (on), on Windows with a Ryzen 9 7900X, Windows with a Core i9-12900K, Linux with a Ryzen 5 5600X and the Steam Deck. The unset and =2 arms on the PR's side show this PR's own effect. The same picture as main within main's own difference from itself; unset, frame time p99 equal to main's; with HOS_MUTEX_GUEST_ID=2, equal to unset or better, within the run-to-run spread. The log must have no FATAL line, and no run may stall or hang.

@Beanstalk This replaces my request 69722 on this head and the corrections under it, in a form the farm can run: it cannot time a picture from the first frame that is not a uniform colour (its answer on #2131), so each picture here follows a fixed wait after the map event. This adds HOS_MUTEX_GUEST_ID, off by default: with HOS_MUTEX_GUEST_ID=2 LockMutex and UnlockMutex find their host object through a number kept in the guest MutexType (host/nn_os_sync.c); unset, it is main's lookup. It sits on #1957 (d111a643, itself on #1956, so the build lifts the game again) and #1969 (617c4fba), so its diff against its merge base includes those PRs' commits; the farm cannot take them as the comparison, so please compare with main. Please start from the market-entrance save and take the picture 10 s (600 frames at 60 fps) after the map event that ends the load, a fixed wait with no input in between (the farm cannot time it from the first frame that is not a uniform colour), with a main-against-itself picture on the same PC from the same point; then, for frame times only, play Castle Town Market for two minutes; at least two starts a side, interleaved, the PR against main on the same PC, on the PR's side unset (the default, off) and with HOS_MUTEX_GUEST_ID=2 (on), on Windows with a Ryzen 9 7900X, Windows with a Core i9-12900K, Linux with a Ryzen 5 5600X and the Steam Deck. The unset and =2 arms on the PR's side show this PR's own effect. The same picture as main within main's own difference from itself; unset, frame time p99 equal to main's; with HOS_MUTEX_GUEST_ID=2, equal to unset or better, within the run-to-run spread. The log must have no FATAL line, and no run may stall or hang.
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.
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
PR description / Description follows the template (pull_request_target) Successful in 5s
beanstalk/approval waiting for a BigBean's 👍 on the request
This pull request has changes conflicting with the target branch.
  • docs/dev/reference/build.md
  • host/hos_thread.c
  • tests/test_vcore_mask0.py
Some workflows are waiting to be reviewed.
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u 'perf/mutex-guest-id:siantm-perf/mutex-guest-id'
git switch 'siantm-perf/mutex-guest-id'
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
3 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!2090
No description provided.