fix(linux): sleep short waits on the timer instead of yielding #2313
Closed
Kolkka wants to merge 2 commits from
Kolkka/linux/short-sleep-timer into main
pull from: Kolkka/linux/short-sleep-timer
merge into: MagicBeanDev:main
MagicBeanDev:main
MagicBeanDev:github/pr/2893
MagicBeanDev:github/pr/2890
MagicBeanDev:github/pr/2887
MagicBeanDev:github/pr/2878
MagicBeanDev:github/pr/2875
MagicBeanDev:github/pr/2874
MagicBeanDev:github/pr/2873
MagicBeanDev:github/pr/2871
MagicBeanDev:github/pr/2756
MagicBeanDev:github/pr/2897
MagicBeanDev:github/pr/2896
MagicBeanDev:github/pr/2718
MagicBeanDev:github/pr/2842
MagicBeanDev:github/pr/2691
MagicBeanDev:github/pr/2895
MagicBeanDev:github/pr/2743
MagicBeanDev:github/pr/2765
MagicBeanDev:github/pr/2708
MagicBeanDev:github/pr/2732
MagicBeanDev:github/pr/2755
MagicBeanDev:github/pr/2817
MagicBeanDev:github/pr/2876
MagicBeanDev:release-please--branches--main
MagicBeanDev:release-please--branches--main--release-notes
MagicBeanDev:github/pr/2778
MagicBeanDev:github/pr/2888
MagicBeanDev:github/pr/2866
MagicBeanDev:github/pr/2877
MagicBeanDev:github/pr/2800
MagicBeanDev:github/pr/2829
MagicBeanDev:github/pr/2850
MagicBeanDev:github/pr/2889
MagicBeanDev:github/pr/2882
MagicBeanDev:github/pr/2860
MagicBeanDev:github/pr/2849
MagicBeanDev:github/pr/2886
MagicBeanDev:github/pr/2794
MagicBeanDev:github/pr/2840
MagicBeanDev:github/pr/2830
MagicBeanDev:github/pr/2834
MagicBeanDev:github/pr/2719
MagicBeanDev:github/pr/2742
MagicBeanDev:github/pr/2741
MagicBeanDev:github/pr/2820
MagicBeanDev:github/pr/2771
MagicBeanDev:github/pr/2859
MagicBeanDev:github/pr/2858
MagicBeanDev:github/pr/2823
MagicBeanDev:github/pr/2757
MagicBeanDev:github/pr/2769
MagicBeanDev:github/pr/2844
MagicBeanDev:github/pr/2843
MagicBeanDev:github/pr/2673
MagicBeanDev:github/pr/2855
MagicBeanDev:github/pr/2739
MagicBeanDev:github/pr/2807
MagicBeanDev:github/pr/2851
MagicBeanDev:github/pr/2789
MagicBeanDev:github/pr/2814
MagicBeanDev:github/pr/2818
MagicBeanDev:github/pr/2774
MagicBeanDev:github/pr/2772
MagicBeanDev:github/pr/2750
MagicBeanDev:github/pr/2786
MagicBeanDev:github/pr/2785
MagicBeanDev:github/pr/2782
MagicBeanDev:github/pr/2779
MagicBeanDev:github/pr/2775
MagicBeanDev:github/pr/2706
MagicBeanDev:github/pr/2766
MagicBeanDev:github/pr/2747
MagicBeanDev:github/pr/2759
MagicBeanDev:github/pr/2745
MagicBeanDev:github/pr/2740
MagicBeanDev:github/pr/2733
MagicBeanDev:github/pr/2725
MagicBeanDev:github/pr/2727
MagicBeanDev:github/pr/2728
MagicBeanDev:github/pr/2729
MagicBeanDev:github/pr/2721
MagicBeanDev:github/pr/2722
MagicBeanDev:github/pr/2715
MagicBeanDev:github/pr/2714
MagicBeanDev:github/pr/2707
MagicBeanDev:github/pr/2703
MagicBeanDev:github/pr/2705
MagicBeanDev:github/pr/2697
MagicBeanDev:github/pr/2679
MagicBeanDev:github/pr/2676
MagicBeanDev:github/pr/2669
MagicBeanDev:docs/what-hookshot-connects-to
MagicBeanDev:fix/hdr-encode-test
MagicBeanDev:fix/main-tests
MagicBeanDev:fix/play-cache-files
MagicBeanDev:perf/game-barrier-order
MagicBeanDev:feat/heap-lazy
MagicBeanDev:feat/texture-detail
MagicBeanDev:perf/astc-opaque-bc1
MagicBeanDev:fix/fs-dir-lock
MagicBeanDev:fix/framegen-native-kept
MagicBeanDev:fix/update-no-downgrade
MagicBeanDev:port/k10-gcn-f16-mirrors
MagicBeanDev:fix/framegen-mult-cap
MagicBeanDev:chore/release-as-2.1.1
MagicBeanDev:perf/pcache-load-switch
MagicBeanDev:fix/prepare-progress-log
MagicBeanDev:fix/update-closes-stale-game
MagicBeanDev:fix/hdr-scaled-sampled
MagicBeanDev:perf/first-start-preset
MagicBeanDev:perf/zero-upload
MagicBeanDev:fix/vcore-escape-2s
MagicBeanDev:test/fake-gh-atomic
MagicBeanDev:perf/batch-idle-flush
MagicBeanDev:test/finish-stub-a
MagicBeanDev:chore/record-2.1.0
MagicBeanDev:fix/main-red-tests
MagicBeanDev:fix/test-display-startup-driver-cache
MagicBeanDev:feat/mesh-shader-mode
MagicBeanDev:fix/menu-probes-at-startup
MagicBeanDev:fix/fps-counter-classic
MagicBeanDev:fix/test-log-batch-flake
MagicBeanDev:fix/narrow-screen-hud
MagicBeanDev:ci/prereleases
MagicBeanDev:release/v2.0.15
MagicBeanDev:hotfix/v2.0.15
MagicBeanDev:fix/present-ahead-replay
MagicBeanDev:release/v2.0.13
MagicBeanDev:fix/bracket-paths-2013
MagicBeanDev:fix/hdr-overlays-2013
MagicBeanDev:fix/fps-counter-scale-2013
MagicBeanDev:fix/stick-seen-at-rest
MagicBeanDev:hotfix/menu-fits-screen
MagicBeanDev:fix/f1-always-opens-menu
MagicBeanDev:fix/ini-keep-lines
MagicBeanDev:claude/cranky-pike-7cf247
MagicBeanDev:perf/texture-answer-flat
MagicBeanDev:fix/stamp-test-modules
MagicBeanDev:fix/appimage-fonts
MagicBeanDev:fix/display-awake
MagicBeanDev:release/v2.0.12
MagicBeanDev:fix/discord-windows
MagicBeanDev:rc/v2.0.12-tests
MagicBeanDev:fix/dlss-keep-feature
MagicBeanDev:rc/v2.0.12
MagicBeanDev:button-icons
No reviewers
Labels
Clear labels
accessibility
Barrier affecting people with disabilities
autorelease: pending
autorelease: tagged
bug
Something isn't working
dependencies
Pull requests that update a dependency file
documentation
Improvements or additions to documentation
duplicate
This issue or pull request already exists
enhancement
New feature or request
github_actions
Pull requests that update GitHub Actions code
good first issue
Good for newcomers
help wanted
Extra attention is needed
invalid
This doesn't seem right
question
Further information is requested
testing wanted
wontfix
This will not be worked on
No labels
accessibility
autorelease: pending
autorelease: tagged
bug
dependencies
documentation
duplicate
enhancement
github_actions
good first issue
help wanted
invalid
question
testing wanted
wontfix
Milestone
Clear milestone
No items
No milestone
Assignees
Clear assignees
No assignees
2 participants
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/Hookshot!2313
Loading…
Reference in a new issue
No description provided.
Delete branch "Kolkka/linux/short-sleep-timer"
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?
On Linux, the game's many short waits spin the CPU in the kernel. On the Steam Deck that takes the power the GPU needs: Kokiri Forest runs at 6.6 fps.
What changed: sleeps under 1 ms yield in a loop until the time, because Windows' timer is too coarse (~0.5 ms) for them. On Linux,
clock_nanosleepwakes within ~50 µs, so the loop only burned CPU. OoT's job wait (f_800998a0, a 20 µs SleepThread between checks) ran about 35,000 times a second, and each worker spent 0.4–0.6 s per second in the kernel with ~20,000 context switches. The Deck's APU shares 15 W, so the CPU took about 11.8 W and the GPU was held at ~220 MHz. Linux now sleeps short waits on the timer, and Windows keeps the yield loop.HOS_SHORT_SLEEP_TIMER=0restores the yields on Linux.Tested: Steam Deck LCD, Kokiri Forest, standing still, 50% resolution:
The rest of the gap is the Deck's power split: with the GPU clock set manually, the same build runs at 14.3 fps.
tests/run_all.py: no new failures againstmain.🤖 Generated with Claude Code
The measurements make a good case: on Linux the yield loop only burns CPU, and on the Deck that power comes out of the GPU's share. I'd like this fix in. But main changed this exact block an hour before the PR was opened, so it can't merge as it is.
blocking: merge conflict in
host/hos_thread.cwith #2263 (4d488bc0). That PR moved the decision intohos_short_sleep_timer(asked, threads)and gavesleep_nsaguestargument. Short sleeps can now go three ways: all yield; the game's own sleeps (SleepThread, throughhos_sleep_logged) wait on the timer while the host's yield; or all wait on the timer.git merge-treeagainstorigin/mainconflicts in this hunk. Rather than resolving the conflict line by line, I'd fold the Linux rule into that design. For example, on non-Windows, non-web builds, have the unset case return 1 (or 2, see below) instead of going through the processor count, and keephos_short_sleep_timerthe single place the choice is made.blocking:
host/hos_thread.c:874reads the switch withatoi(getenv(...)) != 0. Since #2042, switches followenv_on/env_value_off, andHOS_SHORT_SLEEP_TIMER=on(ortrue/yes) would parse as 0 here and turn the timer off, the opposite of what was asked. Main'stests/test_short_sleep_timer.pyhas a broken variant for exactly this ("read as a number instead of by env_on's rule"). Reusinghos_short_sleep_timerfixes it for free.should fix: decide on purpose which sleeps the timer applies to. As written, the PR puts every sleep under 1 ms on the Linux timer, including the host's: the frame limiter (
host/nn_vi.c:30,:177) and the audio renderer (host/nn_audio.c:1168). #2263 deliberately kept those yielding so that pacing isn't late. With Linux's ~50 µs slack that may well be fine, but it is a separate change from the job-worker fix you measured. Either use mode 1 (only the game's sleeps on the timer), which coversf_800998a0, or keep mode 2 and say in the PR that frame pacing and audio were checked on the Deck.should fix: tests.
tests/test_short_sleep_timer.pyforces_WIN32on the cut code (line 53), so it would not see the Linux default at all. Please add cases for the non-Windows build: unset means the timer, and0/offmeans yield. Also run that test against the rebased head. Right now CI has only run the title check; the other workflows are waiting for approval.minor: the log line in main's version says "where Windows has the high-resolution one". If Linux now logs through the same path, word it for both platforms. The measurement covered the Deck only. A desktop Linux run with many threads (where #2263 found yielding helps TOTK-style 20 µs waits on Windows) would show whether "always the timer on Linux" or "the timer by processor count" is the better rule. The ~50 µs slack makes me expect the former, but it's worth one number.
It conflicts in
host/hos_thread.cwith #2263'shos_short_sleep_timer, so it needs a rebase onto that. Two other things:atoi(getenv("HOS_SHORT_SLEEP_TIMER")) != 0makes=onturn the timer off, whichtests/test_short_sleep_timer.pyexists to catch.nn_vi.c) and audio (nn_audio.c) sleeps onto the timer, not just the job waits that were measured.This now conflicts with main in
host/hos_thread.c. Could you merge main into the branch (or rebase) when you get a chance? I'll take another look once it's up to date.Merged main into the branch.
Superseded by #2637