WIP: perf(linux): the code that runs in play in anonymous 2 MB pages, in one piece at the start of the text (HOS_CODE_HUGE) #2921
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
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!2921
Loading…
Reference in a new issue
No description provided.
Delete branch "HumanBean/Longshot:perf/code-huge-pages"
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
On a PC short of memory, the game's code is dropped from RAM and read back from disk while you play. A thread that
needs a dropped page stops until the disk has read it, and on a hard drive that gives 100-500 ms frames. The lab PC
(RX 580, 16 GB RAM, game on a hard drive) shows these stutters. #2893 (HOS_CODE_WARM) cut them from ~18 to ~13 per
16-minute tour by keeping cached code pages warm.
This PR goes further on Linux. At start, it copies all the code the guest profile saw run (about 255 MB) into the
game's own memory, in 2 MB pages, and swaps that copy in place of the file's pages:
first. zram swap-in takes microseconds.
The move runs on a thread of its own while the game starts from the file's pages. The swap is one
mremapcall: thesame bytes at the same addresses. A thread running that code during the swap waits for the mappings, then runs the
copy.
Why "all the code that ran" and not the hot units. First I checked how the code was laid out:
other code that ran, and 31 MB of code that never ran, are mixed in.
quarter of it.
code needs the first 230-240 MB.
So this moves everything the profile saw run, not a hot subset.
What changes in the build (
game_link_order, Linux only): hfsort still puts the functions it orders first. Therest now follow in a fixed object order:
host/code_huge_end.c's objectSo the code that runs in play is the text from its start up to
longshot_code_ran_end: 257 MB, holding all 65 MB ofhot units, all 159 MB of other code that ran, 1.6 MB of host code, and the 29.5 MB of never-ran code hfsort mixes in.
host/code_huge.cmoves that span. The link time is unchanged (3 s against main's 4 s here).Two other layouts were tried and dropped:
--symbol-ordering-file: it cost 12-19 s more a link, whether it named 2 symbols or 72,000.Windows is unchanged.
HOS_CODE_HUGEis on by default on Linux with 12 GB of RAM or more, when transparent huge pages aren't off.=0keepsthe code in the file's pages;
=1moves it below 12 GB too. Windows builds a stub: large pages there need a privilegeplayers don't have.
What a crash record shows. A crash in the moved code is still named
longshot+0x..., throughhos_code_huge_at.The profilers use
dladdr, which reads the program headers, so they are unaffected.With HOS_CODE_WARM. It reads only file mappings, so it leaves the moved part alone and keeps the rest warm.
Read first:
host/code_huge.c,game_link_orderinbuilder/crates/build-compile/src/lib.rs,tests/test_code_huge.py.See #2893.
Testing
Tested: RTX 3090 / 620.70.06 / Linux (CachyOS, kernel 7.3.3, btrfs, 32 GB). Runs from main's base (
d193d12d)against this PR's pack, through the job queue:
scripts/memtour.pytours from the market-entrance save (8 or 5 areas), with and without memory pressure.dev.py abin Hyrule Field and the Market.code: 254-256 MB of the game's code that runs in play ... moved to anonymous memory. smaps showed the whole range in 2 MB pages (AnonHugePages) through an unpressured tour.HOS_CODE_HUGE=0logs that the code stays in the file's pages.tests/test_code_huge.pycheckswithout the game.
Checks:
tests/test_code_huge.pypasses with clang and gcc, and 10 times in a row. It links the real marker file andcode_huge.cin the game's order, after 4 MB of code. It checks the move, the code running from the copy, a secondthread running that code all through the move, the crash-record lookup,
HOS_CODE_HUGE=0, and the span's bounds.cargo test -p build-compilepasses, with a new test ofgame_link_order; alsocargo test -p lift.needless_borrowslints at the.icolines of build-compile'slib.rsand arustfmt diff in
toolchain.rsfail onmaintoo, with the pinned 1.99.1.code_huge*.c,hos_crash_record.c.run_all.py --strict --changed: the checks reading the changed files pass, except failures that also fail onmain:test_switch_table(LONGSHOT_LAUNCHER_STARTUP has no row),test_crash_record(posix/display_sdl.c),test_bug_report,test_windows_on_linux, and checks this PC lacks the tools for.Still to test:
main against this PR, expecting fewer frames over 100 ms and fewer major faults on code (by hand; it will be
checked there).
code: ... movedline and nothing elsedifferent (@Beanstalk).
Measurements
Verdict: under memory pressure, frames over 100 ms are about halved, in two separate batches on this PC. Nothing
else changes, unpressured or under pressure: fps, 1 % low and p99 are within the runs' spread. The lab (RX 580, 16 GB,
hard drive), where code is really read back from disk, is where the gain should be largest. It will be checked there,
and this stays WIP until then.
Setup: Linux (CachyOS, kernel 7.3.3), RTX 3090, driver 620.70.06, 32 GB, btrfs NVMe. main is
d193d12d(with #2893),built as the base; this PR is its pack over it.
Under pressure. A
scripts/memtour.pytour from the market-entrance save: 5 areas, 30 s each, headless, the game's60 cap. From the start of play, the game's own systemd scope is asked every 2 s to reclaim 400 MB (
memory.reclaim, atswappiness 60: file and anonymous memory as a usual distribution balances them). Nothing outside the game's scope is
touched. Runs were interleaved main / HUGE=0 / HUGE=1, twice. Numbers are per minute of play from the frame trace
(RX_VK_FRAME_TRACE), medians of the two runs. Play windows differ (155-257 s) because the tour sometimes stalls in an
in-game event, on every side alike.
d193d12d8909ac9f8909ac9fAn earlier batch, with the code in object order and no hfsort (that layout was dropped for the Market result
below), gave the same picture with a busier PC:
Unpressured,
dev.py ab. 3 interleaved pairs, 60 s each, uncapped, 67 % render resolution, the median:d193d12d8909ac9fOn the dropped object-order layout, a Market switch A/B (
--env HOS_CODE_HUGE=0, 3 pairs) showed no difference(-0.9 %, within the spread). So the move itself costs nothing in play; that layout's 3.5 % came from losing hfsort.
The move's cost:
pages must be read back from btrfs). In a fragmented run, 144 of 256 MB landed in 2 MB pages.
The build: the link takes 3 s (base 4 s). In isolation, the game's link took 1.6-2.2 s with hfsort, 1.2 s with no
sort, and 13.8-20.7 s with an order file. The compile is unchanged, so the players' build time is unchanged.
Code pages touched in a tour (the play window's pagemap union, after the game's file pages were evicted once):
260 MB on main, 76.5 % of it within the first 227 MB.
HOS_CODE_WARM finds nothing on btrfs. On main here, #2893 logs "0 MB of the game's code kept in memory, of 0 MB
in 0 mappings". It matches mappings by
stat()'s device, and btrfs reports a subvolume's own device there, not the one/proc/self/maps shows. That is #2893's, not this PR's, and is reported separately; it doesn't change the comparison.
Risks
while the game played, but it could drop them, and now only swap can take them. Guarded at 12 GB of RAM.
HOS_CODE_HUGE=0turns it off.file's pages must be read back from disk; on a hard drive a few seconds. The game doesn't wait for it.
can't be dropped as page cache, but there is no TLB gain, and the log says how much landed in 2 MB pages (110 of
224 MB in one run here).
handles it, and the samplers use
dladdr. Something external likeperfwould show those addresses as[anon]; setHOS_CODE_HUGE=0for such a run.fps measured the same in both scenes.
code:line.Checklist
Ready for review
main; the title is a Conventional Commit.mise run testpasses (any failure that also fails onmainis named): the checks reading the changed files ran (see Testing); the whole suite not yet.main, or Measurements says why there is nothing to measure.third_party/untouched; nothing GPL-incompatible.When it applies
gpu/vk/switches.def.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_016o1k1SdGuww9Q4o1mvYD9c
Tested in game: RTX 3090 / 620.70.06 / Linux (CachyOS 7.3.3, btrfs, 32 GB): main
d193d12dagainst this PR's pack, through the job queue. Memtours from the market-entrance save with and without memory pressure (the game's own scope reclaiming 400 MB every 2 s), and dev.py ab in Hyrule Field and the Market. Every start with the move on logged 254-256 MB moved to anonymous memory (254 MB in 2 MB pages; 144 MB in one fragmented run), no crash, play the same. Under pressure, frames over 100 ms per minute 3.9 on main against 1.6 here; unpressured fps within the spread in both scenes. The lab (RX 580, HDD, 16 GB) is where the gain should be largest; it will be checked there.Lab test (RX 580 / RADV Mesa 26.2.6 / Linux, 16 GB, game on a hard drive, btrfs, zram at swappiness 150): no gain. It is a little worse, so this stays WIP.
Build: this PR's commit duplicated onto main
e6a71a1d. That main already has #2916, so HOS_CODE_WARM works on btrfs on both sides. It was a full game build on the lab. The A/B isHOS_CODE_HUGE=0against the default on that one build. I ran the 16-minute tour (Kokiri, Lost Woods, Zora's River, Hyrule Field, Kakariko, 2 laps), headless and uncapped, after a warm-up run, interleaved off / on / on / off.The
code:lines:What I think happens: with the move, the process carries about 250 MB more anonymous memory (end of run: AnonHugePages 567 MB against 240 MB, Swap 2.29 GB against 1.57 GB). At 16 GB with swappiness 150, that pushes other memory into zram and the rest of the page cache (data, the 486 MB of text left in the file) out to disk. So the disk-read faults move elsewhere instead of going away. The memory-pressure win measured on the 32 GB PC doesn't carry over. Ideas: a lower default threshold, moving only the hot units, or turning it off when swappiness is high or zram is in use. All of those would need another lab round.
Boxes: nothing new settled from the lab. The run didn't crash, and the screenshots weren't checked for visual differences (headless tour, no reference).
Dropped: on the RX 580 lab (16 GB, zram) it made in-play hitches worse (frames over 100 ms 9/6 off vs 15/13 on; disk-read-fault frames 4/4 vs 11/7), because the ~250 MB of anonymous huge pages pushes other memory to zram and page cache to disk. It only helped on a PC with spare RAM. Numbers: #2921 (comment)
Pull request closed