Repository navigation
fix(kyty): populate direct memory mappings on Windows too - #286
talha-devp wants to merge 2 commits into
Conversation
On Windows, direct memory is a pagefile-backed section mapped on demand, so the first write to each 4 KiB page of a fresh mapping takes a demand-zero page fault on the writing thread. afaf3ef removed these faults on Linux; Windows kept them. Hades II tiles its textures with the CPU into its 6 GiB GPU pool while its loader streams them: about 19,000 faults a second slow the thread that releases the 16 MiB staging buffers, the 512 MiB upload pool fills, and the game crashes before its first frame on a memcpy to NULL after a failed allocation (simnJS#219). Kyty builds on Windows now populate each new direct mapping the way the Linux populate backing does: while a quarter of host memory stays available afterwards, the readable pages of the new view are read once on several threads, so they are resident when the game first writes them. KYTY_DIRECT_MEMORY_PAGES=demand keeps the old demand paging; private needs a Linux host and stops on Windows. The direct-pages memory test now also runs on Windows, checking residency with QueryWorkingSetEx. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
The Windows CI build passed, and the Hades II fault-rate measurements explain the benefit. I found a lifetime gap in the prefault loop. |
The Windows populate ran after MapDirectRecorded had released its mutation guard, then checked each region with VirtualQuery and read its pages directly. Another thread could unmap or protect the range between the check and the read, and the host took an access violation. Each page is now read through ReadProcessMemory, which returns an error instead of faulting when the page is gone or no longer readable; the populate stops at the first such page. A new Windows test, guest_direct_memory_pages_changing, remaps a 64 MiB direct mapping while another thread protects and unmaps parts of it. It crashed 3 of 3 runs before this change and passes 5 of 5 after it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks, you're right. I reproduced it before changing anything: the new test crashed with 20f6840 now reads one byte of each page through New Windows test
|
|
Merged into AnyKyty as |
What
On Windows, direct memory is a pagefile-backed section mapped on demand, so the first write to each 4 KiB page of a fresh mapping takes a demand-zero page fault on the writing thread. #163 (
afaf3ef8b4) removed these faults on Linux; Windows kept them.Hades II tiles its textures with the CPU into its 6 GiB GPU pool while its loader streams them. On Windows that loader takes about 19,000 page faults a second, so it falls behind, and the 16 MiB staging buffers it releases pile up in the game's 512 MiB CPU upload pool. Once the pool is full, the next staging allocation fails silently and the game copies the texture to NULL: the
LoadTaskSchedulercrash of #219.Kyty builds on Windows now populate each new direct mapping the way the Linux
populatebacking does. While a quarter of host memory stays available afterwards, the readable pages of the new view are read once, on several threads (256 MiB chunks), so they are resident when the game first writes them. Reading, not writing, leaves the pages unmarked for the write tracking, as on Linux.KYTY_DIRECT_MEMORY_PAGESnow applies on Windows too: unset orpopulatepopulates,demandkeeps the old demand paging, andprivate(the Linux tmpfs) stops with a message. The log names the choice, as on Linux. Thedirect-pagescheck ofguest_memory_testsgets a Windows variant that checks residency withQueryWorkingSetEx, registered asguest_direct_memory_pages_populateand_demand. Like its Linux variant, it is not in the CI's shim test pattern.How it was found: an outside reader polled the game's pool and loader state with
ReadProcessMemoryevery 0.5 to 2 ms, which leaves the timing alone (a debugger hides the crash). The GPU fences always completed at once, and the loader's queue grew because tiling each 16 MiB texture was slow; most samples of the loader sat on its first store into each new block. Upstreammaindoes not hit this, because its GPU driver imports guest memory into Vulkan, which keeps it resident (7.3 GB working set against 1.3 GB on AnyKyty).Tested
Windows 10 22H2 (19045), Ryzen 5 5600, RTX 4060 (NVIDIA 621.14), 32 GB. AnyKyty
7de61e3451, WinLibs GCC 15.2 and LLVM 21.1.8, Release, as the Windows job builds it.Hades II (PPSA36082), cold launches of 60 s replaying the same controller recording:
7de61e3451main47139cd910Five Kyty switches (
KYTY_LABELS_AROUND_GPU_WRITES=0,KYTY_ASYNC_PIPELINES=0,KYTY_SUSPEND_POINT_DEPTH=1,KYTY_WRITE_FAULT_BLOCKS=0,KYTY_REPLAY_GPU_PAGE_READS=0withKYTY_REPLAY_AFTER_READBACK=0) did not change the crash on the base.Cost: Hades maps about 7.8 GiB of direct memory at start, which now becomes resident in about 0.9 s (the 6 GiB pool at about 7 GB/s; read from the working set over time). Later mappings that would leave less than a quarter of host memory available stay demand-paged, as on Linux.
Tests on the same machine, Windows build with
-DBUILD_TESTING=ON:guest_direct_memory_pages_populateandguest_direct_memory_pages_demandpass;ctest -R '^kyty_shim_.*$'passes (98 of 98;kyty_shim_rt_corpus,kyty_shim_bda_load_formsandkyty_shim_wave_split_written_readsskip).guest_memoryfails at its first check (CheckResidentMappingBookkeepingStaysSmall, "resident direct mapping bookkeeping: -992.12 MiB per GiB mapped"); it fails the same way on the base7de61e3451without this change, and it is not in the CI's test pattern.tools/check_conventions.py --base origin/AnyKytyandtools/check_kyty_settings.pypass. Not tested on Linux: the Linux code is unchanged and the CI builds it.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)LoadTaskScheduler#219LoadTaskScheduler#219