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
Labels
No labels
area/accessibility
area/audio
area/input
area/install
area/menu
area/renderer
area/saves
area/tooling
area/upscaling
beanstalk: approved
beanstalk: failed
beanstalk: needs approval
beanstalk: running
beanstalk: tested
ci: approved
good first issue
gpu/amd
gpu/igpu
gpu/intel
gpu/nvidia
help wanted
kind
bug
kind
chore
kind
docs
kind
feature
kind
performance
kind
question
needs measurements
platform/linux
platform/macos
platform/steam-deck
platform/windows
preview
priority
critical
priority
high
priority
low
regression
risk
high
risk
low
risk
medium
status
blocked
status
confirmed
status
duplicate
status
invalid
status
needs-info
status
wontfix
ci: approved
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot#1504
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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
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.futexcalls and stresses lock and wake races; a micro-benchmark; then paired runs on Linux with a null pair first.Evidence
Code on
forge/mainat310e7c9dfa(the files read here are unchanged since347aa6e82, the tree the research for #1493 read):host/posix/windows.h:155-167:SRWLOCKispthread_rwlock_t; exclusive acquire ispthread_rwlock_wrlock. About 210 exclusive and 50 shared acquire sites inhost/andgpu/(agit grepcount).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:WakeConditionVariableandWakeAllConditionVariableadd toseqand callfutex(FUTEX_WAKE_PRIVATE)every time.:96-105: a waiter unlocks, sleeps onseq, then locks again withpthread_rwlock_wrlock.vc_grantwakes 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 Windowspools_muis astd::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.pycalls this the "not known" case).Measured:
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 oncopy_lock; libc was 23.7% of their samples before and 15.1% after;EndRecording4.4% to 2.0%,AddControlMemory4.4% to 1.9%. The libc time there ispthread_rwlock_rdlockandwrlockon the futex. The PR lists Windows, where the slim lock spins first, as not tested.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.SignalLightEventis 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 onefutexcall. Its cost on Linux is not measured.Proposed change
host/posix/: one 32-bit word (writer bit, reader count, waiters flag), zero-initialised, shared and exclusive modes, a bounded spin of pause instructions, thenFUTEX_WAIT. It replacespthread_rwlock_tforSRWLOCK. Nothing in the tree usessizeof(SRWLOCK).CONDITION_VARIABLEgets a waiter count besideseq. 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, asevent_signaland the mutex code do. The 14 wake sites built on Linux (12 inhost/nn_os_sync.c,vc_grant,host/nvdrv.c) are audited one by one; one that cannot show it keeps the unconditional wake.scripts/prof.py: count the new lock's sleep frame as waiting.How we would test it
host/posix/win32_posix.castests/test_posix_thread_priority.pydoes, with a fixture that countsfutexcalls (an interposedsyscall, orstrace -cwhere it exists). A million wakes with no waiter make a million calls today and must make none.tests/test_cv_signal_race.pystyle run of the realnn_os_sync.con the new shim.HOS_SAMPLE_ALLwith the profiler fixed, so the share of job workers undercopy_lockis visible; CPU a frame;HOS_WAKE_LATfor 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_lockchange). #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.
Possibly related, for context: