WIP: perf(os): HOS_RESERVE_LEND=1 lends the render thread's reserved core to the game's threads while it idles (off by default) #2436
No reviewers
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!2436
Loading…
Reference in a new issue
No description provided.
Delete branch "klimovich008/Longshot:perf/reserved-core-lent-while-idle"
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?
Summary
From 8 logical processors the host keeps one whole core for the render thread, and no game thread may use it. On a CPU
with 4 or 6 cores that is a quarter or a sixth of the CPU, held for a thread that in CPU-heavy scenes sleeps more than
half the frame. This PR adds a switch,
HOS_RESERVE_LEND=1, off unless set, that lends the reserved core to thegame's threads while the render thread is idle, and takes it back when the render thread gets busy. Measured on one
8-core PC held to 4 and 6 cores by an affinity mask, it gets back what
HOS_RESERVE_CORE=0gains in Lost Woods withoutlosing what keeping the core gains in Hyrule Field. Whether it becomes a default is the maintainers' call after real 4-
and 6-core CPUs have run it.
Where it acts. Only when a core is reserved, the game may use fewer than 8 whole cores (counted as the reserved
core's own rule counts them: inside the process's mask and the cache cores), and the cores are of one kind (a hybrid CPU
declines). It also declines under
HOS_RESERVE_CORE_MASKandHOS_PIN_BY_CORE. One log line at start says what it doesor why it does nothing:
threads: HOS_RESERVE_LEND=1: the reserved core 0xc0 is lent to the game's threads while the render thread idles (...)/threads: HOS_RESERVE_LEND=1 does nothing here: <why>. With threaded rendering offnothing asks the host, and the core stays kept as today.
The decision (
gpu/vk/rt_lend.h, a pure header, on the render thread, from its own job times over windows ofpresents):
0.5 % of the window (a frame cap's waits are not idle: at a cap the core stays kept).
from idle, so a heavily paced window takes it back), or a window that decides nothing (a present over 250 ms, or under
20 presents a second: a load, a pause, a menu).
a lend, a take-back, then nothing for 20 s, so no 20 s holds more than two changes, whatever the scene does. Without
it, three earlier rules each still cycled in some steady sequence (at the 60 cap with a large gain, uncapped with a
large gain, or a long hitch every 10 s); with it a 20,000-run fuzz of random windows never had more than 2 changes in
any 20 s. It changed none of the measured runs: the shortest take-back-to-lend gap in any game run was about 21.7 s.
HOS_PINplaces are skipped; threads made or renamed while lent get the lent mask). The render thread's own mask isnot touched. A log line names each change and its reason (
vk: HOS_RESERVE_LEND: the reserved core lent to the game's threads|taken back: the render thread idle NN % ...), and a line after the 600-present line counts them.Also changed:
MKWVK_API_VERSION45 -> 46:MkwVkHostgainsreserved_lend, appended. An exe and a renderer of differentversions refuse each other, as before.
vk_api.his not a shader-cache stamp file: no re-prepare.host/posix/win32_posix.c(Linux), a change on every path:SetThreadAffinityMasknow takes the thread's lockand refuses a thread that has ended (
ERROR_INVALID_PARAMETER, as Windows refuses one). This applies with the switchunset too, to every caller of the shim's call (
hos_thread_pin,pin_by_core, the replay thread's pin,RX_VK_RECORDER_PIN). Why: an ended detachedpthread_tmay already name another thread, and setting its affinitycould move that one. The existing callers set running threads' masks, so it refuses nothing they did before; a call
on an ended thread used to reach
pthread_setaffinity_np(undefined there) and now fails. It is one self-containedfunction and can be split into its own PR if preferred.
host/nn_vi.c: the host's frame pacing adds its planned sleep to a counter while the rule applies (no new clockread).
host/rx_backend.c: the API field.host/hos_thread.c: the rule, the topology reader, the walk.gpu/vk/vk_replay.cpp: counting at the two job-timing sites and the present.gpu/vk/switches.def,docs/dev/reference/switches.md,docs/dev/reference/threads.md. Tests:tests/test_reserved_lend.py(new),tests/test_reserved_core.py,tests/test_pacing_policy.py(two stub definitions).With our other two placement PRs (neither on main):
RX_VK_RT_HOME): this PR sets areserved_lentflag with no reader yet. Whichever of the two mergessecond adds the one test in
rt_home_frame(the home guard off while lent, its counters cleared at the take-back) andits case in
tests/test_rt_pin.py. Both edit the same render-thread bullet inthreads.md.HOS_WORKERS_SPREAD): the walk skips threadsHOS_PINplaces; the workers' set needs the same skip(
spread && !strncmp(name, "BaseProcExecutor", 16)), added by whichever merges second. They can meet only at exactly 7whole cores.
lend_coresand #2360'sworker_coresread the same thing; whichever mergessecond should share one. This PR already folds the Linux reserved-core reader into one reader (
cpu_core_list,sys_cores), with unset reading exactly what main read.Read first:
gpu/vk/rt_lend.h(decide), thenhost/hos_thread.c(lend_rule_cpus,lend_applies,hos_reserved_lend,hos_lend_affinity), thenvk_replay.cpp'slend_job/lend_present.Testing
Tested: Windows 11, Ryzen 7 5700X3D (8 cores, 16 threads), RX 9070 XT, AMD 26.11.2. The PR's build over main
0e32bc10(the code of
297aa184; API 46; shaders byte-identical to main's), the switch on againstHOS_RESERVE_CORE=0and againstunset. Every masked number is this 8-core CPU held to 4 cores (
0xff) or 6 cores (0xfff) by an affinity mask, not areal 4- or 6-core CPU. Each A/B is 3 alternating pairs, the fixed test profile, a fixed light GPU load beside the game,
uncapped unless marked. Lost Woods and Hyrule Field are kept apart.
launch, which put 21 early threads (the main thread, the Presentation thread, the loaders) back on the reserved core
in every "kept" arm. From the second round on it no longer does, so unset arms have every game thread off the reserved
core, as on a player's PC. The first round's larger gains (+8.4 / +6.3 %) were against that partly-kept control and
are not quoted.
control's.
0xff, Lost Woods and Hyrule, 3 pairs each, two rounds): fps flat (59.9-60.0 both arms), kept, 0changes after the save load.
0xff, the PR's build unset against main's: 64.49 vs 64.49 fps; the placement of everythread identical by name and mask in all 6 runs; no log line.
load, lent in Lost Woods, taken back at the teleport; the long frames are the loads', no worse than the control's.
tests/test_reserved_lend.py(llvm-mingw clang and MSYS2 gcc): the realrt_lend.hover window sequences (lend,take-back, loads, the 60 cap, steady cycles and periodic hitches each held to at most 2 changes in any 20 s, the 20 s
quiet and its edge, the pacing veto's level); the host's rule and walk over stubbed topologies and thread tables
(masks inside the process's mask and cache cores,
HOS_PINthreads untouched, threads made while lent, declines); thetwo renderer call sites cut from
vk_replay.cppand run. 26 broken variants, each failing; astatic_assertpins theconstants an equivalence argument depends on. It fails on main.
8aa5ab87: main merged cleanly (35 commits, among themthreads.md,vk_main.cpp,vk_replay.cpp,hos_thread.c: no conflict); on the merged headtests/test_reserved_lend.pypasses with clang and gcc (26 broken variants fail), and the focused checks pass (test_reserved_core,test_reserved_core_mask,test_fps_stats,test_frame_overlap,test_threaded_rendering,test_switch_table,test_env_on,test_switches_removed,test_doc_anchors,test_hos_ab,byte_compile).run_all.py --changed 8aa5ab87: 277 passed, 13 failed; each failure also fails on main (four of them,test_menu_text_field,test_rdna2_mesh_only,test_rumble_menu,test_vibration_menu, fail on main8aa5ab87itself; the rest on this PC on0e32bc10too). The changed host files compile whole (MSVC-target clang, llvm-mingw with main's warning counts, Linux clang and gcc);nn_vi.cshows only the tworx_optionerrors main's file has against the same headers. Not re-run in the game after the merge (the merge changed no line this PR adds).mise run lint: only main's shellcheck SC2016 on.forgejo/workflows/build-windows.yml:82.Still to test:
HOS_RESERVE_LEND=1against0on the PR'sbuild, Lost Woods and Hyrule Field, uncapped, 3 starts a cell, expecting Lost Woods up, Hyrule level, no long frames
(@Beanstalk).
HOS_RESERVE_LENDlines, expecting none after the save load(@Beanstalk).
there (@Beanstalk, if a Deck tester is online).
Measurements
Verdict: on this PC's 4- and 6-core masks, Lost Woods gains +5.6 % and +5.4 % against today (unset), within 0.5 % of
HOS_RESERVE_CORE=0; Hyrule Field stays within 1 % of today; the 60 cap is unchanged. Off by default.Setup: Windows 11, Ryzen 7 5700X3D, RX 9070 XT, AMD 26.11.2. Lost Woods as child (the profile save, then the teleport),
fast rotation, 16:9,
RX_VK_ASYNC=1, frame rate unlocked (HOS_FPS_MAX=240), the fixed test profile and GPU load, 20 swindow, 3 alternating pairs, medians, the game's mask set at launch (
--affinity). Before: main's placement (the PR'sbuild unset, shown equal to main's above, or main's own build); After: the PR's build with
HOS_RESERVE_LEND=1.0e32bc10297aa184,HOS_RESERVE_LEND=10xff), fps median0xfff), fps medianAgainst
HOS_RESERVE_CORE=0(the core never kept), Lost Woods: 4 cores 69.09 -> 68.79 (-0.43 %) and, on the finalcode, 67.54 -> 67.29 (-0.37 %); 6 cores 79.74 -> 79.39 (-0.44 %). The 60 cap: Lost Woods 60.00 / 59.99 and Hyrule 59.99 /
59.94 fps (control / on), 0 changes after the save load. The first four rows are from the round before the 20 s quiet
(head
47b18d90); the quiet changes none of those runs (their shortest take-back-to-lend gap was 21.7 s), and the finalcode was re-measured against
=0(-0.37 %). Std_test labels every A/B here "diagnostic" (under 5 % with 3 pairs, orwithin 1 %); the Hyrule 4-core arm had every switch-on run below every control run (76.99-77.04 against 77.49-78.09)
with the same placement in the window, inside the 1 % bar.
Risks
shared core (the reason the core is kept). The take-back needs one 120-present window; loads take it back at once.
change (title, loads).
not counted as pacing;
change of the process mask in play does the same to every thread); on Linux the shim's process mask is the launch
mask, so a take-back can widen threads
taskset -anarrowed later;the current mask;
RX_VK_PRESENTS_AHEAD=2and a timed-out drain miscount the render thread's job time slightly (as the 600-presentline does);
and the same placement thread for thread in the game). API 46 means the exe and the renderer go together, as every
build ships them.
Checklist
Ready for review
main; the title is a Conventional Commit.mise run testpasses (any failure that also fails onmainis named).main, or Measurements says why there is nothing to measure.third_party/untouched; nothing GPL-incompatible.When it applies
gpu/vk/switches.def.tests/that reads changed source is updated, not weakened.MKWVK_API_VERSIONbumped (MkwVkHostorMkwVkRendereringpu/vk/vk_api.hchanged).Assisted-by:/Co-Authored-By:trailers.Ready to merge
main, measured again or a comment says why the numbers still hold.🤖 Generated with Claude Code
8aa5ab87) into perf/reserved-core-lent-while-idleTested in game: Windows 11, Ryzen 7 5700X3D (8C/16T) held to 4 cores (0xff) and 6 cores (0xfff) by an affinity mask, RX 9070 XT, AMD 26.11.2; not a real 4- or 6-core CPU. 3 alternating pairs each, uncapped (HOS_FPS_MAX=240), RX_VK_ASYNC=1, the fixed test profile. Lost Woods (child): +5.6 % (0xff) and +5.4 % (0xfff) against unset, within 0.5 % of HOS_RESERVE_CORE=0 (final code: -0.37 % on 0xff); Hyrule Field within 1 % of today on both masks; the 60 cap flat in both scenes; 0 lend/take-back changes inside any measured window; 0 frames over 50 ms in the uncapped switch-on runs; clock 0.998-1.002; unset = main thread for thread. Crossing run (title, save load, Lost Woods, teleport to Hyrule Field): changes only at the title and the loads. Not run: real 4C/8T or 6C/12T CPUs, the Steam Deck, Linux, V-Sync on, other caps, with #2240 or #2360.
@Beanstalk the PR's own build only, its switch against itself: baseline ref = pr, [matrix.pr_only] HOS_RESERVE_LEND = ["0", "1"], and pass.compare HOS_RESERVE_LEND = "1" against "0" on the pr side (0 is exactly unset: today's placement). It is a runtime switch, so one build serves both sides. 3 game starts a cell.
What it does: on CPUs with fewer than 8 whole cores and 8 or more logical processors, the host keeps one core for the render thread; with the switch on it lends that core to the game's threads while the render thread is idle and takes it back when it gets busy. On 8+ cores, hybrid CPUs or without SMT it declines (logged), so only these targets can show anything. Our numbers are an 8-core CPU held to 4 and 6 cores by a mask; we need real ones.
Targets, 1-2 reports each, by model; don't let the plan wait on any one:
Scenes, in one start: Lost Woods as child Link (from the save kokiri-start, teleport destination 5) measured 30 s, where the game's threads hold the frame (expect a gain); then Hyrule Field (HyruleField_Child) measured 30 s, where the render thread holds it (expect level). Settings: frame_rate_cap = 6 (Uncapped), vsync = 2 (Off), frame_generation = 0; headless is fine. Compare rules, pr side, =1 vs =0, per scene: fps_mean min_delta_pct = 3 (fail on a loss of more than 3 %), frametime_p99_ms max_delta_pct = 10; crash = false. Hitch counts (run_hitch_count, run_frametime_max_ms) report-only.
Plus one start a side at the 60 fps cap (frame_rate_cap at 60, vsync off), the same scenes, on the 4C/8T target if it fits the time (else any of the three): report-only, counting the log lines "vk: HOS_RESERVE_LEND: the reserved core lent" and "... taken back" in the =1 start. Expected at the cap: one lend at the title screen and one take-back at the save load, then none in play.
Log on the =1 cells: "threads: HOS_RESERVE_LEND=1: the reserved core" expected on these targets ("does nothing here" on a PC where the rule doesn't apply: please show which line came).
Beanstalk test farm · 🔄 testing (2 of 3 reports so far)
Commit
d453599· approved by klimovich008 (reviewed) · dashboard⚠️ Note: The requested three starts per cell and an additional capped run exceed the 60-minute limit with this two-cell matrix, so the plan uses two starts per cell and omits the capped run.
⚠️ Note: Log counts show whether lending applied or was declined and how often transitions occurred, but do not establish exactly when transitions happened within a scene.
⚠️ Note: Using only the PR build does not compare its switch-off changes, including the Linux affinity shim, against main.
Started at 05:03 UTC.
windows+cpu-3300x_i3-10100_i3-12100_i3-13100_i7-6700_i7-6700k_i7-7700_i7-7700k(CPU "3300x" or "i3 10100" or "i3 12100" or "i3 13100" or "i7 6700" or "i7 6700k" or "i7 7700" or "i7 7700k")windows+cpu-3600_5600_5600x_7600_7600x_i5-10400_i5-11400_i7-8700k(CPU "3600" or "5600" or "5600x" or "7600" or "7600x" or "i5 10400" or "i5 11400" or "i7 8700k")steamdeckuncapped-scenes· frame time p99 (ms)HOS_RESERVE_LEND=0(reference)HOS_RESERVE_LEND=1windows+cpu-3300x_i3-10100_i3-12100_i3-13100_i7-6700_i7-6700k_i7-7700_i7-7700k(CPU "3300x" or "i3 10100" or "i3 12100" or "i3 13100" or "i7 6700" or "i7 6700k" or "i7 7700" or "i7 7700k")windows+cpu-3600_5600_5600x_7600_7600x_i5-10400_i5-11400_i7-8700k(CPU "3600" or "5600" or "5600x" or "7600" or "7600x" or "i5 10400" or "i5 11400" or "i7 8700k")steamdeckWhat was tested
What will be tested · AI plan (
gpt-6.1-terra)On real 4-core and 6-core Windows CPUs and the Steam Deck, the PR build will run with core lending off and on, measuring 30 seconds each in child Lost Woods and Hyrule Field with the frame cap disabled. Two starts per cell provide FPS and frame-time comparisons, while hitch metrics and lending log counts are reported. A crash, an FPS loss over 3%, or a p99 frame-time increase over 10% in either scene fails the test. The Windows CPU groups have no matching models listed online and will wait for eligible testers; Steam Deck testers are online. No tester online now matches windows+cpu-3300x_i3-10100_i3-12100_i3-13100_i7-6700_i7-6700k_i7-7700_i7-7700k, windows+cpu-3600_5600_5600x_7600_7600x_i5-10400_i5-11400_i7-8700k: those targets wait for one.
HOS_RESERVE_LEND) and off; main is not built or run.0or1(PR only).kokiri-start(settings vsync=2, frame_rate_cap=6, frame_generation=0), then teleport to Lost Woods, measure lost-woods for 30 s (after 25 s warm-up), teleport to Hyrule Field, measure hyrule-field for 30 s (after 10 s warm-up); 2 runs on each side for each of 2 cells (2 game starts a run).Measurements on `steamdeck`, for the PR description
Verdict: pass on steamdeck (2 reports): no rule failed and no PR run crashed.
Hardware: 1: linux+amd+radv+igpu+handheld
AMD Custom GPU 0405 (RADV VANGOGH), 26.1.99AMD Custom APU 0405, 12 GB RAM; 2: linux+amd+radv+igpu+handheldAMD Custom GPU 0405 (RADV VANGOGH), 26.1.99AMD Custom APU 0405, 12 GB RAMScene: uncapped-scenes, from the repository's save kokiri-start (vsync=2, frame_rate_cap=6, frame_generation=0); each cell's settings and switches as the table names them
Method: the Beanstalk test farm: this PR built once and run with its PR-only switches on and off on the same PC in one session, game starts alternating (ABBA); uncapped-scenes: 2 game starts a side, lost-woods 30 s after 25 s warm-up, hyrule-field 30 s after 10 s warm-up; each number the median of the runs, then of 2 reports
How to reproduce: the farm's plan above (
@Beanstalkon this PR runs it again)d453599, switch offd453599, switch onHOS_RESERVE_LEND=0 (PR only)· lost-woods · frame time (p99) (ms)HOS_RESERVE_LEND=0 (PR only)· hyrule-field · frame time (p99) (ms)HOS_RESERVE_LEND=1 (PR only)· lost-woods · frame time (p99) (ms)HOS_RESERVE_LEND=1 (PR only)· hyrule-field · frame time (p99) (ms)Flags and settings
HOS_RESERVE_LEND0,1As Markdown for the PR description:
The testers' hardware
steamdeckAMD Custom GPU 0405 (RADV VANGOGH), 26.1.99AMD Custom APU 0405, 12 GB RAMsteamdeckAMD Custom GPU 0405 (RADV VANGOGH), 26.1.99AMD Custom APU 0405, 12 GB RAMView command line instructions
Checkout
From your project repository, check out a new branch and test the changes.