The virtual-core gate takes one global lock on every block and wake, and a low-priority thread can wait up to a second for a core #1503
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#1503
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 (virtual cores) · Related: #1493
TL;DR
Guest threads run only while they hold one of six virtual cores. Every take and give-up goes through one global lock (a yield with nobody queued has returned without it since #1553), a waiting thread wakes every millisecond to look again, and a low-priority thread can wait a full second before the gate lets it in. In a Windows profile the gate cost 1.1 to 1.5 ms of CPU a frame, and one Steam Deck session logged 184 core waits of 50 ms or more with the default limit and none with a 20 ms limit. We propose a gate with no lock when a core is free, waits that end at a real deadline, and a limit that follows the machine, with a game-free fixture first.
Breakdown
HOS_VCORES=0.Evidence
Code on
forge/mainat310e7c9dfa(host/hos_thread.c; unchanged since347aa6e82, the tree the research for #1493 read)::1679-1682: onevc_lock,vc_owner[8],vc_queue[1024].:1820-1827:vc_acquiretakesvc_lockbefore it looks for a free core.:1905-1913:vc_releasetakes it too. (Line numbers are from4b68c42be. Update 18 Nov: since #1553 a yield with an empty queue and no kick returns without the lock; what is still missing on main is the take and give-up of a free core without it, a per-core waiter count so the yield fast path is per core rather than a global-queue test, and the waits and the starve limit below.):1735-1739,:1724-1731,:1921-1924:vc_grant,vc_dequeueandvc_yieldscan the queue linearly. The array holds 1,024 entries, but no more threads can wait than the game has; how deep the queue gets is not measured.:1863: a waiter sleeps with a 1 ms timeout, and each time it returns it retakes the lock, scans the holders (:1842-1862) and tests its slice, starve and escape times (:1873-1875). A grant ends the sleep early (:1864).:1751:vc_grantwakes the waiter while it still holdsvc_lock; the waiter needs that lock first.:1818: starve 1,000,000 us (HOS_VCORE_STARVE_US), slice 2,000 us, escape 2 s.:1846,:1848-1855: a waiter may ask only a worse holder, after the slice an equal one, and only once starved any holder.:1171-1182: every short sleep gives the core up and takes one again, two gate trips per sleep.Measured:
49f75dc82of 2026-11-11, replay on the Presentation Thread, sampled (docs/cpu-profile.md): virtual cores 1.49 ms a frame in Hyrule Field (7.7% of a core), 1.48 in Kokiri, 1.11 on the Castle Town to Temple walk; in Hyrulevc_acquire0.92,vc_yield0.23,vc_release0.12, checkpoints 0.11 ms. That is sampled CPU, not time the frame waited.vcores:log line): about 7,500 waits a second, mean wait 44 to 54 us (longest 12 to 16 ms), about 3,500 kicks a second, no escapes.HOS_VCORES=0: 30.6 fps (28.7 to 32.1) against 29.2 (27.6 to 30.6); runs differ by about 5% on their own.HOS_VCORE_STARVE_US=20000none logged. Frame rate under 1,200 draws 29.5 against 32.0, 1,200 to 1,800 draws 24.2 against 26 to 29, over 1,800 the same. The reporter calls 1 to 2 fps noise; the wait counts are the robust part. One logged wait: a priority 24 thread waited 132.9 ms for a core given by the main thread (priority 16), which held it 154.1 ms and was never asked.Existing test:
tests/test_vcore_escape.pycompiles the real block with no-op locks, a clock that the waits advance and one thread. It pins the escape and starve rules; it cannot see two threads at once.Proposed change
vc_free_forchooses today (:1707-1715).mwait(host/nn_os_sync.c:28) andcv_waiting(:43,:609) already use.vcores:line first. Replace the scan with an ordered structure only if depth shows it matters.How we would test it
hos_thread.c.4d488bc02), Kokiri. Count waits of 50 ms or more, longest wait, kicks, escapes, the Presentation Thread's core wait a frame (HOS_PROFILE=1), CPU a frame and frame p99. For any speed claim: main against main first (the null pair), then interleaved pairs, arms main, new gate,HOS_VCORES=0. Then the soak: a long route (the comment at:1669records a 25-minute one) and many save loads, Linux and Windows.Related
#1493 (renderer rewrite, parallel CPU track). #262 (closed: escapes while a save loads) and #594 (merged: the 1 s starve rule). #1020 (open: the Deck lead on the starve limit). #402 (open: the Deck frame is CPU-bound). #1218 (merged: the long-wait log line used above). #1150 (merged: the virtual core is kept for an invalidate with nothing to wait for). #327 (open): changes the same block (mask 0 and the guest's core cache); a rewrite must keep its per-take core write, so it should land or be rebased first. #249 (closed, not merged:
HOS_VCORE_FIRST, within noise once the wait it targeted was fixed); its closing note says to reopen ifHOS_PROFILE=1shows the Presentation Thread waiting more than about 0.5 ms a frame for a core on the frame's path, which the game run below should record.New on this forge; not opened on GitHub.
Possibly related, for context: