Skip to content

fix(kyty): populate as much of a Windows direct mapping as fits - #301

Closed
talha-devp wants to merge 1 commit into
simnJS:AnyKytyfrom
talha-devp:fix/windows-populate-reserve
Closed

talha-devp wants to merge 1 commit into
simnJS:AnyKytyfrom
talha-devp:fix/windows-populate-reserve

Conversation

@talha-devp

Copy link
Copy Markdown
Contributor

What

Windows populated a new direct mapping only while a quarter of host memory stayed available afterwards, and otherwise left the whole mapping demand paged. On a 16 GB host, Hades II's 6 GiB pool needs about 10 GiB free for that, but by the time the game maps it, its 1 GiB heap and 512 MiB pool are already resident and only about 6.5 to 8.5 GiB are left. So the pool stays demand paged and the game crashes as in #219. This is the 16 GB part of #287.

Windows now keeps a sixteenth of host memory free instead of a quarter (1 GiB on a 16 GB host, 2 GiB on 32 GB). It also no longer decides all or nothing: it populates the first pages of a mapping while that reserve holds and leaves only the rest on demand. A lower all-or-nothing threshold alone was not enough. With a 1 GiB reserve, the 6 GiB mapping landed 200 to 600 MiB short of the threshold in 2 of 3 launches, and one of them crashed. When a mapping is only partly populated, the log says how much, once.

Linux is unchanged: its default private backing does not need the populate, and the quarter rule still applies to its populate fallback. The memory that the populate holds up front (the other half of #287) is unchanged.

Tested

Windows 10 22H2 (19045), Ryzen 5 5600, RTX 4060 (NVIDIA 621.14), 32 GB. AnyKyty eb5103d101, WinLibs GCC 15.2 and LLVM 21.1.8, Release, as the Windows job builds it.

To simulate a 16 GB host, a helper process locked RAM with VirtualLock until about 10 GB (or 9 GB for the tight case) stayed free. A local build evaluated the reserve with a 16 GiB total and logged each decision. Hades II (PPSA36082), cold launches replaying the same controller recording:

Simulated 16 GB host 6 GiB pool Launches Result Frames
Quarter rule (eb5103d101), 10.6 GB free skipped 3/3 (7,841 to 8,435 MiB free, rule needs 10,240) 3 × 60 s 2 of 3 crashed (LoadTaskScheduler, 0xc0000005) 4,740 / 30 / 30
Sixteenth, all or nothing, 10.2 GB free populated 1/3 (6,573 to 7,826 MiB free, rule needs 7,168) 3 × 180 s 1 of 3 crashed 18,840 / 90 / 18,330
This PR, 10.2 to 10.8 GB free populated fully 4/4 (7,608 to 8,341 MiB free) 1 × 180 s + 3 × 90 s 0 of 4 crashed 8,310 to 8,520 per 90 s
This PR, 9.2 GB free partly 3/3 (5,051 to 5,307 of 6,144 MiB) 3 × 90 s 0 of 3 crashed 8,220 to 8,430

On the full 32 GB host this PR populates everything, as before: 2 of 2 launches of 60 s ran (5,280 and 4,770 frames).

Memory: with everything populated on the simulated 16 GB host, Hades' working set peaked at about 12 GB and free memory fell to about 0.9 GB. Under the reserve, Windows can still trim the populated pages later; I measured only 90 to 180 s sessions.

Tests on the same machine, Windows build with -DBUILD_TESTING=ON: guest_direct_memory_pages_populate, _demand and _changing pass (3 rounds each). guest_memory fails at its first check (CheckResidentMappingBookkeepingStaysSmall, "resident direct mapping bookkeeping: -992.11 MiB per GiB mapped"). It fails the same way on the base without this change, and it is not in the CI's test pattern.

tools/check_conventions.py --base origin/AnyKyty and tools/check_kyty_settings.py pass. Not tested on Linux: the Linux code is unchanged and the CI builds it.

Checklist

Windows populated a new direct mapping only while a quarter of host
memory stayed available afterwards, and otherwise left the whole
mapping demand paged. On a 16 GB host that needs about 10 GiB free for
Hades II's 6 GiB pool, which a 16 GB PC rarely has once the game's
earlier mappings are resident, so the pool stayed demand paged and the
game crashed as in simnJS#219 (simnJS#287).

Windows now keeps a sixteenth of host memory free instead, 1 GiB on a
16 GB host, and populates the first pages of a mapping for as long as
that reserve holds, leaving only the rest on demand.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@simnJS

simnJS commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Merged into AnyKyty as 47326de335 (authorship kept) after a green CI on Linux, sanitizers and Windows. Thanks for measuring the simulated 16 GB host so carefully, @talha-devp!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants