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 #1504

Open
opened 2026-11-18 01:41:09 +00:00 by mooki · 1 comment
Member

Type: performance · Area: host (Linux Windows-shim) · Related: #1493

TL;DR

On Linux the host's locks and condition variables are emulations of the Windows ones. The slim lock is a pthread rwlock that sleeps at once when it is busy, every condition-variable wake is a system call even with no waiter, and the profiler counts rwlock sleeps as running time. Game job workers still spent about 4% of their running samples on one such lock after #766, and about 210 lock sites in the tree go through the shim. We propose a small futex lock with a short spin, a waiter count so a wake with no waiter stays in user mode, and a profiler fix, proved by a game-free syscall count and stress test first.

Breakdown

  • Who it affects: Linux players, including the Steam Deck. Windows uses its own native locks and is not touched.
  • What is wrong: a busy lock goes to sleep without a spin (Windows spins first); a wake costs a system call even when no thread waits; the woken waiter retakes its lock while the waker may still hold it; profiles show lock sleeps as running time.
  • What we propose: one small lock and condition-variable implementation in host/posix/, a short bounded spin before the sleep (its length set by the micro-benchmark), a waiter count next to the wake counter, and a profiler that counts the new lock's sleep as waiting.
  • What does not change: the API and names, shared mode, zero-initialised locks, lock order, and everything on Windows.
  • What we do not claim: a size. The only Linux number is one profile from one reporter; the gain is their guess of 1 to 2% in CPU-bound areas, to be measured.
  • How we would prove it: a game-free test that counts futex calls and stresses lock and wake races; a micro-benchmark; then paired runs on Linux with a null pair first.

Evidence

Code on forge/main at 310e7c9dfa (the files read here are unchanged since 347aa6e82, the tree the research for #1493 read):

  • host/posix/windows.h:155-167: SRWLOCK is pthread_rwlock_t; exclusive acquire is pthread_rwlock_wrlock. About 210 exclusive and 50 shared acquire sites in host/ and gpu/ (a git grep count).
  • glibc's rwlock has no spin-then-sleep step on a busy lock: the design comment in nptl/pthread_rwlock_common.c (glibc master, read 2026-11-17) has waiting threads block on a futex word and says they may not "spin-wait on __readers". #766's profile shows the workers asleep on that futex. The same comment says writer contention costs more than a mutex's.
  • host/posix/win32_posix.c:88-95: WakeConditionVariable and WakeAllConditionVariable add to seq and call futex(FUTEX_WAKE_PRIVATE) every time.
  • :96-105: a waiter unlocks, sleeps on seq, then locks again with pthread_rwlock_wrlock. vc_grant wakes while holding its lock (host/hos_thread.c:1751), so the woken thread can meet a held lock and sleep a second time.
  • gpu/vk/vk_memory.cpp:182-191: off Windows pools_mu is a std::mutex, because the shim's slim lock is "more atomic steps than a mutex".
  • scripts/prof.py:37-50: the wait test lists no rwlock or futex frame. The lock call is inline, so a sleep on it is counted under its caller as running time (#1020 reports the same). #1321 and #1347 (open) rewrite this matching for Windows and for names with parameter lists; their descriptions do not mention rwlock sleeps.
  • host/hos_thread.c:658-671, :682-695: process_threads() is 0 off Windows, so on Linux every thread spins 20 us before an event, queue or mutex sleeps; the 5 us rule measured on Windows is not in force there (tests/test_spin_by_role.py calls this the "not known" case).

Measured:

  • #766 (merged 2026-11-15, c9fbb0d6b), Linux, RTX 3090, Ryzen 9 7900X, the Market, HOS_SAMPLE_ALL: the game's job worker threads spent 13% of their running samples asleep on copy_lock; libc was 23.7% of their samples before and 15.1% after; EndRecording 4.4% to 2.0%, AddControlMemory 4.4% to 1.9%. The libc time there is pthread_rwlock_rdlock and wrlock on the futex. The PR lists Windows, where the slim lock spins first, as not tested.
  • #1020 (HumanBean, 2026-11-16): the remaining 4% on copy_lock; "a short spin in the shim would help every SRW lock on Linux"; guess 1 to 2% in CPU-bound areas. One machine.
  • Windows, not Linux: SignalLightEvent is 0.49 ms of a 19.5 ms frame in Hyrule Field, 543 calls a frame, 0.91 us a call (docs/cpu-profile.md; the page says its call-count columns are to be regenerated). On Linux each such signal is at least one futex call. Its cost on Linux is not measured.

Proposed change

  1. A futex lock in host/posix/: one 32-bit word (writer bit, reader count, waiters flag), zero-initialised, shared and exclusive modes, a bounded spin of pause instructions, then FUTEX_WAIT. It replaces pthread_rwlock_t for SRWLOCK. Nothing in the tree uses sizeof(SRWLOCK).
  2. CONDITION_VARIABLE gets a waiter count beside seq. A waiter counts itself in before it unlocks. A wake returns without a syscall when the count is 0. This holds only where the caller changes its condition under the lock the waiter sleeps with, as event_signal and the mutex code do. The 14 wake sites built on Linux (12 in host/nn_os_sync.c, vc_grant, host/nvdrv.c) are audited one by one; one that cannot show it keeps the unconditional wake.
  3. Wake after the waker lets go of its lock where it is easy to do.
  4. scripts/prof.py: count the new lock's sleep frame as waiting.
  5. Optional, separate: let the processor count be known on Linux, so the 5 us spin rule can apply there (measure it).

How we would test it

  • Game-free, fails today: build host/posix/win32_posix.c as tests/test_posix_thread_priority.py does, with a fixture that counts futex calls (an interposed syscall, or strace -c where it exists). A million wakes with no waiter make a million calls today and must make none.
  • Game-free, passes today and guards the rewrite: lock tests (writers exclude everything, readers share, a million rounds on 8 threads, zero-initialised locks); condition-variable stress (every wake observed by a waiter that registered before it; timed waits; a forced pause between "check the condition" and "sleep"); a tests/test_cv_signal_race.py style run of the real nn_os_sync.c on the new shim.
  • Micro-benchmark, not a pass or fail: uncontended and 2, 4, 8 thread contended acquire; a wake with and without a waiter; old shim against new, on a quiet machine. It decides whether the spin and the waiter count are worth their code.
  • Game: Linux, Kokiri, Hyrule Field and the Market, on a 12 and a 24 processor setup and a Steam Deck. HOS_SAMPLE_ALL with the profiler fixed, so the share of job workers under copy_lock is visible; CPU a frame; HOS_WAKE_LAT for signal to running. For any speed claim, main against main first, then interleaved pairs of main and the change.

#1493 (renderer rewrite, parallel CPU track). #1020 (open: HumanBean's lead on a spin in the shim, and the Deck results). #766 (merged: the copy_lock change). #402 (open: the Deck is CPU-bound). #1321 and #1347 (open): the profiler's wait matching; step 4 should follow whichever lands. #1284 (open): runs the log writer on Linux through this shim. #437, folded into #845 (both closed, not merged): a macOS version of these waits in the same two files; the platform part of a new lock should stay in one small function.

New on this forge; not opened on GitHub.

**Type:** performance · **Area:** host (Linux Windows-shim) · **Related:** #1493 ## TL;DR On Linux the host's locks and condition variables are emulations of the Windows ones. The slim lock is a pthread rwlock that sleeps at once when it is busy, every condition-variable wake is a system call even with no waiter, and the profiler counts rwlock sleeps as running time. Game job workers still spent about 4% of their running samples on one such lock after #766, and about 210 lock sites in the tree go through the shim. We propose a small futex lock with a short spin, a waiter count so a wake with no waiter stays in user mode, and a profiler fix, proved by a game-free syscall count and stress test first. ## Breakdown - **Who it affects:** Linux players, including the Steam Deck. Windows uses its own native locks and is not touched. - **What is wrong:** a busy lock goes to sleep without a spin (Windows spins first); a wake costs a system call even when no thread waits; the woken waiter retakes its lock while the waker may still hold it; profiles show lock sleeps as running time. - **What we propose:** one small lock and condition-variable implementation in `host/posix/`, a short bounded spin before the sleep (its length set by the micro-benchmark), a waiter count next to the wake counter, and a profiler that counts the new lock's sleep as waiting. - **What does not change:** the API and names, shared mode, zero-initialised locks, lock order, and everything on Windows. - **What we do not claim:** a size. The only Linux number is one profile from one reporter; the gain is their guess of 1 to 2% in CPU-bound areas, to be measured. - **How we would prove it:** a game-free test that counts `futex` calls and stresses lock and wake races; a micro-benchmark; then paired runs on Linux with a null pair first. ## Evidence Code on `forge/main` at `310e7c9dfa` (the files read here are unchanged since `347aa6e82`, the tree the research for #1493 read): - `host/posix/windows.h:155-167`: `SRWLOCK` is `pthread_rwlock_t`; exclusive acquire is `pthread_rwlock_wrlock`. About 210 exclusive and 50 shared acquire sites in `host/` and `gpu/` (a `git grep` count). - glibc's rwlock has no spin-then-sleep step on a busy lock: the design comment in `nptl/pthread_rwlock_common.c` (glibc master, read 2026-11-17) has waiting threads block on a futex word and says they may not "spin-wait on __readers". #766's profile shows the workers asleep on that futex. The same comment says writer contention costs more than a mutex's. - `host/posix/win32_posix.c:88-95`: `WakeConditionVariable` and `WakeAllConditionVariable` add to `seq` and call `futex(FUTEX_WAKE_PRIVATE)` every time. - `:96-105`: a waiter unlocks, sleeps on `seq`, then locks again with `pthread_rwlock_wrlock`. `vc_grant` wakes while holding its lock (`host/hos_thread.c:1751`), so the woken thread can meet a held lock and sleep a second time. - `gpu/vk/vk_memory.cpp:182-191`: off Windows `pools_mu` is a `std::mutex`, because the shim's slim lock is "more atomic steps than a mutex". - `scripts/prof.py:37-50`: the wait test lists no rwlock or futex frame. The lock call is inline, so a sleep on it is counted under its caller as running time (#1020 reports the same). #1321 and #1347 (open) rewrite this matching for Windows and for names with parameter lists; their descriptions do not mention rwlock sleeps. - `host/hos_thread.c:658-671`, `:682-695`: `process_threads()` is 0 off Windows, so on Linux every thread spins 20 us before an event, queue or mutex sleeps; the 5 us rule measured on Windows is not in force there (`tests/test_spin_by_role.py` calls this the "not known" case). Measured: - #766 (merged 2026-11-15, `c9fbb0d6b`), Linux, RTX 3090, Ryzen 9 7900X, the Market, `HOS_SAMPLE_ALL`: the game's job worker threads spent 13% of their running samples asleep on `copy_lock`; libc was 23.7% of their samples before and 15.1% after; `EndRecording` 4.4% to 2.0%, `AddControlMemory` 4.4% to 1.9%. The libc time there is `pthread_rwlock_rdlock` and `wrlock` on the futex. The PR lists Windows, where the slim lock spins first, as not tested. - #1020 (HumanBean, 2026-11-16): the remaining 4% on `copy_lock`; "a short spin in the shim would help every SRW lock on Linux"; guess 1 to 2% in CPU-bound areas. One machine. - Windows, not Linux: `SignalLightEvent` is 0.49 ms of a 19.5 ms frame in Hyrule Field, 543 calls a frame, 0.91 us a call (`docs/cpu-profile.md`; the page says its call-count columns are to be regenerated). On Linux each such signal is at least one `futex` call. Its cost on Linux is not measured. ## Proposed change 1. A futex lock in `host/posix/`: one 32-bit word (writer bit, reader count, waiters flag), zero-initialised, shared and exclusive modes, a bounded spin of pause instructions, then `FUTEX_WAIT`. It replaces `pthread_rwlock_t` for `SRWLOCK`. Nothing in the tree uses `sizeof(SRWLOCK)`. 2. `CONDITION_VARIABLE` gets a waiter count beside `seq`. A waiter counts itself in before it unlocks. A wake returns without a syscall when the count is 0. This holds only where the caller changes its condition under the lock the waiter sleeps with, as `event_signal` and the mutex code do. The 14 wake sites built on Linux (12 in `host/nn_os_sync.c`, `vc_grant`, `host/nvdrv.c`) are audited one by one; one that cannot show it keeps the unconditional wake. 3. Wake after the waker lets go of its lock where it is easy to do. 4. `scripts/prof.py`: count the new lock's sleep frame as waiting. 5. Optional, separate: let the processor count be known on Linux, so the 5 us spin rule can apply there (measure it). ## How we would test it - Game-free, fails today: build `host/posix/win32_posix.c` as `tests/test_posix_thread_priority.py` does, with a fixture that counts `futex` calls (an interposed `syscall`, or `strace -c` where it exists). A million wakes with no waiter make a million calls today and must make none. - Game-free, passes today and guards the rewrite: lock tests (writers exclude everything, readers share, a million rounds on 8 threads, zero-initialised locks); condition-variable stress (every wake observed by a waiter that registered before it; timed waits; a forced pause between "check the condition" and "sleep"); a `tests/test_cv_signal_race.py` style run of the real `nn_os_sync.c` on the new shim. - Micro-benchmark, not a pass or fail: uncontended and 2, 4, 8 thread contended acquire; a wake with and without a waiter; old shim against new, on a quiet machine. It decides whether the spin and the waiter count are worth their code. - Game: Linux, Kokiri, Hyrule Field and the Market, on a 12 and a 24 processor setup and a Steam Deck. `HOS_SAMPLE_ALL` with the profiler fixed, so the share of job workers under `copy_lock` is visible; CPU a frame; `HOS_WAKE_LAT` for signal to running. For any speed claim, main against main first, then interleaved pairs of main and the change. ## Related #1493 (renderer rewrite, parallel CPU track). #1020 (open: HumanBean's lead on a spin in the shim, and the Deck results). #766 (merged: the `copy_lock` change). #402 (open: the Deck is CPU-bound). #1321 and #1347 (open): the profiler's wait matching; step 4 should follow whichever lands. #1284 (open): runs the log writer on Linux through this shim. #437, folded into #845 (both closed, not merged): a macOS version of these waits in the same two files; the platform part of a new lock should stay in one small function. New on this forge; not opened on GitHub.
Collaborator

Possibly related, for context:

  • issue #1020 Perf: possible performance improvement leads — Same Linux SRW lock spin and copy_lock performance lead, expanded with CV wake and profiler issues.
  • PR #766 perf(nvn): recording threads wait less on the recordings' copy lock — Merged copy_lock performance change; this proposal targets remaining lock contention.
Possibly related, for context: - issue #1020 Perf: possible performance improvement leads — Same Linux SRW lock spin and copy_lock performance lead, expanded with CV wake and profiler issues. - PR #766 perf(nvn): recording threads wait less on the recordings' copy lock — Merged copy_lock performance change; this proposal targets remaining lock contention.
Sign in to join this conversation.
No milestone
No project
No assignees
2 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#1504
No description provided.