mirror of
https://github.com/valentderah/totk-pc-recomp.git
synced 2026-11-19 12:25:56 +00:00
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
6.4 KiB
6.4 KiB
Windows host behaviour
What the Windows host does differently from the console and from MacNativeGames' macOS host, measured on machine A (Ryzen 9 7900X, RX 9070, NVMe) in 2026-10. Each entry: the behaviour, the numbers, where the runtime handles it, and the rule for new code. Linux equivalents of the fixes: backlog.md item 35.
Rules
- The game threads (guest main, job workers) and the render thread never open, read, write or flush a file, and never wait for a thread that does. Move file I/O to a background thread and hand results over in memory.
- Short waits spin; waits over 1 ms use the high-resolution timer.
- Every frame-time claim needs three runs per build: single runs of one recording differ by up to three times.
Timers and time
- Waitable timer overshoot. A high-resolution waitable timer ends a wait 0.3-0.5 ms late at best: the SDK's
20 us polling sleeps (main thread and job workers, about 1.5 per frame each) took 460-515 us.
kernel.csleep_nsspins on the performance counter up toSLEEP_SPIN_NS(1 ms), the virtual core already given up. macOSnanosleepis accurate, so MacNativeGames never had this. Measure withTOTK_TRACE=sched("sleep asked / took" per thread). - A 1 ms timer thread does not help. A run with a thread waking every 1 ms (the sampler of
TOTK_PROFILE_MAIN) had more slow frames than one without, not fewer: no evidence for core parking or C-state wake-up costs here. - Counter conversion.
QueryPerformanceCounterticks to ns or to the guest's 19.2 MHz counter as(unsigned __int128)t * num / dencompiles to__udivti3, a library call; it took a tenth of the guest threads' running time in a fight, as the scheduler and the guest's counter reads convert many thousands of times a second.platform.hPlatScalemultiplies by a precomputed integer and 64-bit fraction (branchtask/perf2). A lazily set two-field scale shared by threads must be published behind a flag stored after it.
Files
- Small-file opens stall behind other writers. Opening a 20-byte file in a 139k-file cache folder takes 0.1 ms
alone (p99 0.3 ms) but took 11-40 ms in play, while the translator, the pipeline cache and the save wrote and
flushed on the same volume. Defender's real-time scan was off; the stall is the file system.
gfx_shader.creads all.usemasks on a background thread at start (45k files in 12 s). - Flushes are expensive. A save Commit (copy or hard-link 82 files,
FlushFileBufferson each file and directory, twoMoveFileExWwithMOVEFILE_WRITE_THROUGH, rotate backups) took 93-135 ms: compare 23-41, hard links 17-31, copy 0-18, directory flush 22-24, oldest backup deletion 16.fs_host.cruns it on a thread; game writes wait for it. - Saving a cache holds its lock.
vkGetPipelineCacheDataplus the file write took about 60 ms; done on the present thread under the pipeline lock, every 20 s autosave froze the replay.vk_pipeline.csaves on its own thread under a separate lock. - A crash leaves zero-filled files. After bugchecks, thousands of
.spvand.res.jsonfiles were zero-filled (3647 of 21924 once). Validate cached files on load (gfx_shader.cchecks the.res.jsonshape) instead of trusting their presence. - Unicode paths. Use the W APIs with UTF-8 to UTF-16 conversion (
fs_wide); exes that take paths carry anactiveCodePageUTF-8 manifest (installer.md, Constraints).
Threads and sampling
- Stopping a thread waits for user mode.
SuspendThreadreturns at once, butGetThreadContextwaits until the thread has left the kernel; a thread inside a long kernel call (a file open, a driver call) stalls the sampler for that long.prof_win.crecords such stops over 2 ms aststall: they name the thread's kernel call. - Guard page. A thread stopped between its
sub rspand the stack probe has Rsp inside its guard page; copying the stack from Rsp raisedSTATUS_GUARD_PAGE_VIOLATIONand ended the process. CheckPAGE_GUARDwithVirtualQuerybefore reading another thread's stack. - Idle threads.
QueryThreadCycleTimetells without suspending whether a thread ran since the last look; the sampler repeats an idle thread's last chain instead of stopping it. - Three virtual cores are the CPU budget. TotK keys per-core data by
GetCurrentProcessorNumberwithout locks, so guest threads share three virtual cores whatever the host has. In a fight the guest main thread and five job workers keep 2.6 of 3 busy and ready threads queue for a core: guest code speed decides the frame rate there.
Vulkan and the driver (AMD, Windows)
- New memory blocks. VMA's
vkAllocateMemoryof a 256 MiB device-local block took 8-11 ms (memory type 3: 11-19 ms, 650 MiB: 23 ms) under VMA's block lock; texture creation on the render thread paid it four times in the first minute of play while VRAM grew from 3.0 to 4.0 GB. Each allocation over 2 ms is logged byvk_device.ctimed_allocate(branchtask/perf2). - Driver pipeline cache. Keyed by the exe path and limited in size; preparing another build dir can evict this one's libraries (backlog.md item 12, agent-playbook.md).
win32u.dllframes in a host stack are kernel graphics calls (WDDM), usually memory allocation or submission.
Measuring
- Run-to-run spread. One recording replayed on near-identical builds gave 1% lows from 31.5 to 50 fps; three runs
of one build agreed within 46-50. Compare medians of three
--quietruns, never one run against another. - Replays need game time. With the real-time frame step, input keyed by presents lands at other game times at
another frame rate; recordings key input by the world clock (
frame_rate.c), and a load of any length no longer shifts it. The fight still diverges after about 15 game seconds (enemy AI, physics). - Bit flips on machine A. Its RAM flips bits (bugcheck 0x139 and others, several a day): a cached file, a
hard-linked object or a release can carry a flip without an error. Compare two independent builds; objects embed
the toolchain path of the folder they were built in, so compare their
.text,.dataand.rdata, not whole files. A file that reads wrong only through the file cache: hash it withFILE_FLAG_NO_BUFFERINGtoo. - Swapping exes.
build_game.pydoes not relink when onlytotk.exewas replaced by a copy; delete the exe before rebuilding to restore a build dir.