perf(host): on Linux 6.10+ the code warm touches cached code a run at a time, not a page at a time #3087
No reviewers
Labels
No labels
area/accessibility
area/audio
area/input
area/install
area/menu
area/mods
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
3 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!3087
Loading…
Reference in a new issue
No description provided.
Delete branch "klimovich008/Longshot:perf/code-warm-populate"
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
Why: on Linux, the code warm thread (
host/code_warm.c,HOS_CODE_WARM, on by default) keeps the game's code in memory. Every 2 s it asksmincorewhich pages of the program's text are cached and reads one byte of each through the kernel (process_vm_readv). That keeps the pages from being let go and faulted back in the middle of guest code. Reading page by page is expensive where much of the text stays cached. On a Steam Deck OLED at 15 W, in play, a pass over the game's 750 MB took 335 ms every 2 s: 14 % of a core, at nice 19, all the time.What changes:
host/code_warm.c, on Linux 6.10 and later: each run of cached pages, up to 64 KB, is touched with onemadvise(MADV_POPULATE_READ)instead of a copy per page.mincoreand the call is skipped page by page. Examples:ENOMEMorEINVALfrom an unmap ormprotect,EFAULT,EINTR.mmapormunmapwaits for at most 64 KB of it (16 pages of 4 KB, or one page where pages are 64 KB). A pass with that cap took 48 ms, against 42 ms uncapped, out of play.process_vm_readvis refused (ENOSYS, orEPERMunder a sandbox), the copy reads/proc/self/mem, as before.HOS_CODE_WARM_COPY=1(off by default) copies a page at a time as before the runs, everywhere. It is read on the starting thread: agetenvon the warm thread could race asetenvof the start-up.HOS_CODE_WARM=0still turns the thread off.by runs: N calls a pass, M failed since the last line (those runs copied page by page), ora page at a time.gpu/vk/switches.def(the new row),docs/dev/reference/architecture.md(the code warm's paragraph).tests/code_warm_pass_test.candtests/test_code_warm.py: see Testing.Result: on the Steam Deck the code warm's pass takes 57 ms instead of 335 ms, and the thread uses 2.7 % of a core instead of 14 % (below). Frame rate does not change measurably: the thread runs at nice 19. The gain is CPU time and power the game doesn't spend.
Unchanged:
mincorepicks, in the same 2 MB steps.gpu/but the switch's row, which is not in the shader stamp, so no player prepares shaders again.Read first:
touch_runsandpopulate_supported_forinhost/code_warm.c, thenwarm_thread.Testing
Tested: in the game on a Steam Deck OLED (SteamOS 3.9.5, kernel 7.2.7, Mesa 26.2.0-devel RADV, 4 cores / 8 threads, AC power, stock 15 W). The PR at 4d4a6ceb (on main
b4d56f52) was built from source on the Deck, against main2ca7d4f74. The head since then is that commit rebased onto main1e782fa4, plus comments; the PR's files changed by comments only. Market as a child, uncapped, the Deck's harness:code warmthread's user and system time from/proc, over a 20 s window in play.HOS_CODE_WARM_COPY=1: 2.89 CPU-s, 14.4 %, the copy as before.750 MB of the game's code kept in memory, of 750 MB in 1 mappings; a pass 56.7 ms every 2 s, by runs: 12002 calls a pass, 0 failed. main:a pass 335.1 ms.HOS_CODE_WARM_COPY=1, 3 pairs: a pass 311-348 ms against 47-58 ms.Game-free, on the Deck:
tests/test_code_warm.py: passes. The pass test (tests/code_warm_pass_test.c, up to three runs) builds the realcode_warm.cand checks:PROT_NONE, and a page unmapped, betweenmincoreand the call: the run is copied page by page and the bad page skipped.process_vm_readvgets the copy through/proc/self/mem.process_vm_readv(EPERM,EACCES) andmadvise(EPERM,ENOSYS): each passes.Negative controls: each mutation below was put into a copy of
code_warm.c, and the test fails on each:EINVALonly; stopping after a failure; the/proc/self/memdescriptor lost (intouch_runs, and inwarm_range); noEPERMfallback; every call failing;tests/run_all.py, the whole suite, on main238f4f65: 731 passed, 8 failed, 146 skipped. main238f4f65on the same Deck: the same 731 and the same 8 failed. They come from this Deck's toolchain, not the change:test_audio_source,test_obj_cache_size,test_obj_guest_id,test_obj_table_bounded;test_check_cache,test_game_font,test_vk_wb_cpu_words_fast;test_i18n_complete: missing translations, main's.Rebased since onto main
1e782fa4: no file of this PR changed, andtests/test_code_warm.pypasses there.pre-commit (as
mise run lint; gitleaks not installed here): passes.Windows: not built here. Every change is inside
#if defined(__linux__)or the file's first comment, so Windows compiles the samehos_code_warm_start(void) {}as main.A second-opinion review (gpt-6.2-sol), 15 rounds over the whole diff. It found and this fixed:
EINVAL;getenvon the warm thread;Round 15 (4d4a6ceb) found none.
Still to test:
code:log line to sayby runs: ... 0 failedon a 6.10+ kernel (a page at a timeon an older one), and no crash (@Beanstalk)by runswith 0 failed and no crash (@Beanstalk)Measurements
Verdict: the code warm thread uses a fifth of the CPU it did on the Steam Deck: 2.7 % of a core instead of 13.8 %. A pass takes 57 ms instead of 335 ms, with all 750 MB of text cached. Whole-game fps doesn't change measurably (+0.8 %, the pairs disagreeing in sign), as expected for a nice-19 thread.
Setup: Steam Deck OLED, SteamOS 3.9.5, kernel 7.2.7, Mesa 26.2.0-devel RADV, stock 15 W, AC; Market as a child, uncapped (
HOS_FPS_MAX=240), headless as the farm starts it, 1280x720 at 75 %, the Deck's thread shape. The thread's CPU: one run each,/proc/<pid>/task/*/statover a 20 s window in play. The pass: the game's own log line. fps: 3 alternating pairs, 2 admitted on each side (one PR run swapped 120 MB in and was refused), a 20 s window each, the median. The PR's build is 4d4a6ceb (on mainb4d56f52, one menu commit after2ca7d4f74); the head differs from it by a rebase and comments.2ca7d4f742ca7d4f74HOS_CODE_WARM_COPYships off: the copy costs five times the CPU for the same pages.Risks
mmapormunmapcan wait behind one call, of at most 64 KB.HOS_CODE_WARM_COPY=1brings back the copy;HOS_CODE_WARM=0turns the thread off. A revert is enough.mincorecounts every page as cached, so both ways read the whole text in.Not covered:
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.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
https://claude.ai/code/session_012MibRabbL9JhKKrsNVaBfg
Tested in game: Steam Deck OLED (SteamOS 3.9.5, kernel 7.2.7, Mesa 26.2.0-devel RADV), stock 15 W, AC: Market as a child, uncapped, headless, the PR built from source (4d4a6ceb) against main
2ca7d4f74. The code warm's log: 'a pass 56.7 ms every 2 s, by runs: 12002 calls a pass, 0 failed' (main: 335.1 ms); its thread 2.7 % of a core against 13.8 % on main (14.4 % with HOS_CODE_WARM_COPY=1). No crash, game clock 1.000-1.002, fps as main.@Beanstalk linux (RADV and NVIDIA) and the Steam Deck, this PR's head: start the game and play a minute in any area. No timing is needed. Expected in the log, after a minute: the
code:line ends withby runs: N calls a pass, 0 failed since the last lineon a kernel 6.10 or later, ora page at a timeon an older one; no crash. Optional, on the same machine: one start withHOS_CODE_WARM_COPY=1, expectinga page at a timeand a longer pass (a pass ... ms) than without it.@review
Possibly related, for context:
Beanstalk test farm · ⚠️ couldn't be judged on linux+radv, linux+nvidia, steamdeck
Commit
a3d0abd· approved by klimovich008 (reviewed) · dashboard⚠️ Note: The log check cannot choose the expected branch by kernel version or ensure that the matching line was emitted after the minute; it accepts either branch anywhere in the run log.
⚠️ Note: The plan does not measure the claimed reduction in code-warm thread CPU time or pass duration, and does not run the optional copy-switch comparison.
Done (started 15:23 UTC, last report 15:23 UTC). Comment
@Beanstalkto run it again.linux+radvlinux+nvidiasteamdeckCouldn't be judged: the tester's PC could not finish the test. The farm tries another PC, or the same one after a fix; it says nothing about the PR on its own.
linux+radv, report 1:main's tools.lock: tools.lock line 32 (sdl): its first URLs are not Longshot's store'slinux+radv, report 2:main's tools.lock: tools.lock line 32 (sdl): its first URLs are not Longshot's store'slinux+nvidia, report 1:main's tools.lock: tools.lock line 32 (sdl): its first URLs are not Longshot's store'ssteamdeck, report 1:main's tools.lock: tools.lock line 32 (sdl): its first URLs are not Longshot's store'ssteamdeck, report 2:main's tools.lock: tools.lock line 32 (sdl): its first URLs are not Longshot's store'sWhat was tested
What will be tested · AI plan (
gpt-6.1-terra)On Linux RADV, Linux NVIDIA and Steam Deck, the plan starts the game and spends at least a minute in Hyrule Field. It checks for a code-warm log line reporting either zero failed run calls or page-at-a-time operation, and fails on a crash or a missing matching line. No timing comparison is made; main also runs because the farm uses it as the baseline.
castle-gate(switchesHOS_FPS_MAX=30,HOS_OUTPUT_RES=1280x720), then teleport to Hyrule Field; 1 run on each side.Measurements on `linux+radv`, for the PR description
Verdict: could not be judged on linux+radv (2 reports).
Hardware: 1: linux+amd+radv+igpu
AMD Radeon 890M Graphics (RADV STRIX1), 26.1.99AMD Ryzen Z2 Extreme, 32 GB RAM; 2: linux+amd+radv+igpuAMD Radeon 760M Graphics (RADV PHOENIX), 26.0.8AMD Ryzen 5 230 w/ Radeon 760M Graphics, 16 GB RAMScene: field-code-warm, from the repository's save castle-gate (HOS_FPS_MAX=30, HOS_OUTPUT_RES=1280x720); each cell's settings and switches as the table names them
Method: the Beanstalk test farm: main and this PR built and run on the same PC in one session, game starts alternating (ABBA); field-code-warm: 1 game start a side; 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)Nothing was measured: no metric came back.
Flags and settings
None: the plan varied no setting, switch or build option.
As Markdown for the PR description:
Measurements on `linux+nvidia`, for the PR description
Verdict: could not be judged on linux+nvidia (1 report).
Hardware: 1: linux+nvidia+nvidia-proprietary+dgpu
NVIDIA GeForce RTX 5070, 620.73.4AMD Ryzen 5 9600X 6-Core Processor, 32 GB RAMScene: field-code-warm, from the repository's save castle-gate (HOS_FPS_MAX=30, HOS_OUTPUT_RES=1280x720); each cell's settings and switches as the table names them
Method: the Beanstalk test farm: main and this PR built and run on the same PC in one session, game starts alternating (ABBA); field-code-warm: 1 game start a side; each number the median of the runs, then of 1 report
How to reproduce: the farm's plan above (
@Beanstalkon this PR runs it again)Nothing was measured: no metric came back.
Flags and settings
None: the plan varied no setting, switch or build option.
As Markdown for the PR description:
Measurements on `steamdeck`, for the PR description
Verdict: could not be judged on steamdeck (2 reports).
Hardware: 1: linux+amd+radv+igpu+handheld
AMD Custom GPU 0932 (RADV VANGOGH), 26.1.99AMD Custom APU 0932, 16 GB RAM; 2: linux+amd+radv+igpu+handheldAMD Custom GPU 0405 (RADV VANGOGH), 26.1.99AMD Custom APU 0405, 16 GB RAMScene: field-code-warm, from the repository's save castle-gate (HOS_FPS_MAX=30, HOS_OUTPUT_RES=1280x720); each cell's settings and switches as the table names them
Method: the Beanstalk test farm: main and this PR built and run on the same PC in one session, game starts alternating (ABBA); field-code-warm: 1 game start a side; 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)Nothing was measured: no metric came back.
Flags and settings
None: the plan varied no setting, switch or build option.
As Markdown for the PR description:
The testers' hardware
linux+radvAMD Radeon 890M Graphics (RADV STRIX1), 26.1.99AMD Ryzen Z2 Extreme, 32 GB RAMlinux+radvAMD Radeon 760M Graphics (RADV PHOENIX), 26.0.8AMD Ryzen 5 230 w/ Radeon 760M Graphics, 16 GB RAMlinux+nvidiaNVIDIA GeForce RTX 5070, 620.73.4AMD Ryzen 5 9600X 6-Core Processor, 32 GB RAMsteamdeckAMD Custom GPU 0932 (RADV VANGOGH), 26.1.99AMD Custom APU 0932, 16 GB RAMsteamdeckAMD Custom GPU 0405 (RADV VANGOGH), 26.1.99AMD Custom APU 0405, 16 GB RAMRecommendation: Approve. The Deck results show a substantial reduction in warm-thread CPU time, but the new memory-warming path still needs the requested Linux hardware runs and a comparison under memory pressure. The fallback test also has a gap that can hide a broken copy path. Nothing blocking is left: what remains is for the author before merging (below). Recommend approval; not approved on the forge.
On Linux 6.10 and later, cached executable pages are touched in runs of up to 64 KB instead of copied one byte per page. This reduces warm-thread work on the tested Deck at the cost of a new kernel-dependent path and fallback logic.
Should fix (1): not blocking, but worth doing before merging.
tests/code_warm_pass_test.c:52: The capability check uses the copy implementation under test to decide whether to skip its own assertions. (more on the line) Do: Probe process_vm_readv and /proc/self/mem independently of copy_pages, then require the copy assertions whenever either underlying method works.Testing before merging (2) (runs on the systems it can change; it doesn't hold the recommendation)
host/code_warm.c:138: Check that the new touch method preserves the original protection against code eviction under sustained memory pressure. (more on the line) Do: Compare main and this head over repeated equivalent memory-pressure game runs, recording code residency, major faults and frame-time tails, including the slow-storage case the warmer addresses.host/code_warm.c:138: The affected Linux RADV and NVIDIA systems still need the in-game runs requested by the author. (more on the line) Do: Complete the requested Linux RADV and NVIDIA game runs on the head, checking the code-warm log and normal play; record the results before merge.@fixcan do L1.Checks: Still running: 11 checks.
More: does it help, other systems, switches, what was read
Does it help: Yes on the tested Steam Deck: the PR reports 2.7% of one core versus 13.8% on main and a 56.7 ms pass versus 335.1 ms with 750 MB cached. The CPU figures are one run per side; earlier alternating pairs reported 47–58 ms versus 311–348 ms per pass. The measured build predates the current rebase.
Other systems: The change runs only on Linux; Windows and macOS retain the existing stub. Linux kernels before 6.10 use the copy path. The Deck was tested in game, but the requested Linux RADV and NVIDIA runs, kernels 6.10–7.1, 64 KB pages, and filesystems other than ext4 have no reported in-game coverage. Memory-pressure retention and mmap contention remain relevant across Linux systems.
Switches: HOS_CODE_WARM_COPY is added, default off; setting it to 1 selects the prior copy path. Its default is supported by the Deck timing but remains to be checked on the other affected Linux systems. HOS_CODE_WARM remains on by default and its table description is updated.
Reviewed:
a3d0abdce6against1e782fa4bb: Read AGENTS.md, CONTRIBUTING.md, the PR template, relevant standards, the complete diff, host/code_warm.c, tests/code_warm_pass_test.c, tests/test_code_warm.py, the code-warm header and startup caller, and the commit messages. Read the changed architecture and switch-table entries. docs/review-checklist.md and ../Tooling/AGENTS.md were absent. Other architecture and switch-table entries were skimmed; no recorded runs, translations, or generated files were changed.; nothing run (a code review).Checked and fine:
Testing: Read-only review; no code, tests, builds, or project scripts were run. The author reports game-free checks and Deck play; Forgejo checks are still running.
Overlaps and merge order: Not checked; the network is closed.
Automated code review by BeanBot (gpt-6.1-terra), asked for by klimovich008. Nothing was run; a maintainer decides.
@ -80,0 +135,4 @@k++;continue;}uint64_t e = k;L2 (should fix): Check that the new touch method preserves the original protection against code eviction under sustained memory pressure.
When: When executable pages compete with game memory, a difference in reclaim behavior or readahead can change guest major faults and frame stalls even if pagemap and smaps show pages touched immediately after a pass.
Do: Compare main and this head over repeated equivalent memory-pressure game runs, recording code residency, major faults and frame-time tails, including the slow-storage case the warmer addresses.
L3 (should fix): The affected Linux RADV and NVIDIA systems still need the in-game runs requested by the author.
When: The default changes the kernel calls made by the warm thread on Linux 6.10 and later, while the reported in-game result covers only the Deck configuration.
Do: Complete the requested Linux RADV and NVIDIA game runs on the head, checking the code-warm log and normal play; record the results before merge.
@ -28,0 +49,4 @@madvise_works, whether madvise can set up the cases' pages (MADV_DONTNEED). */static int copy_reads_here(long page, int memfd) {char *p = mmap(NULL, (size_t)page, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);if (p == MAP_FAILED) return 0;L1 (should fix): The capability check uses the copy implementation under test to decide whether to skip its own assertions.
When: If copy_pages regresses to return zero for every page, copy_reads_here reports that the environment cannot copy, the copy cases are skipped, and the sandbox child also skips. On a supported kernel the run-path checks can still pass, leaving the broken fallback undetected.
Do: Probe process_vm_readv and /proc/self/mem independently of copy_pages, then require the copy assertions whenever either underlying method works.
WIP: perf(host): on Linux 6.10+ the code warm touches cached code a run at a time, not a page at a timeto perf(host): on Linux 6.10+ the code warm touches cached code a run at a time, not a page at a time