Merge the save commit and driver cache fixes: a save writes only its changed files and hard-links the rest (300-380 ms freezes at each save), and build_game re-warms the GPU driver's cache after relinking a prepared build (40-220 ms freezes on every new effect after a rebuild)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Valiantsin DzerakhandClaude Opus 5.5 committed 2026-10-27 09:01:54 +03:00
commit e1476494d5
6 files changed
+121 -12

No files matched your search

+1 -1
View File
@@ -43,7 +43,7 @@ Setup and verify commands: [docs/toolchain-windows.md](docs/toolchain-windows.md
- `tests\diff\atomic.py`: exclusive load/store sequences and ARMv8.1 atomics; needs no game files.
- `tests\diff\func.py`: whole leaf functions as the generator emits them, loops and jump tables included.
- `tests\excl_mt.py`: the exclusive monitor helpers of `cpu.h` under contention and ABA, on several threads; needs no game files.
- Game code build (`.venv`): `recomp\gen.py --exefs build\extracted\v121\exefs --out generated\v121 --patches recomp\patches-v121.txt` writes the C (about 15 s). The patch table holds the 60 fps and real-time frame step code patches, switchable at run time (`recomp/patches.py`); `.venv-py314\Scripts\python tools\make_romfs_patches.py` writes their RomFS half to `build\v121\romfs-patches`. `tools\build_game.py` compiles and links `build\v121\totk.exe` with Ninja (about 200 s clean, incremental after). `recomp/layout.py` alone decides module bases. Jump tables are resolved by `recomp/jumptables.py`; after changing it, `tests\jumptables_check.py` must report 0 disagreements with its Unicorn oracle (on 1.4.3 too: `--exefs build\extracted\v143\exefs`).
- Game code build (`.venv`): `recomp\gen.py --exefs build\extracted\v121\exefs --out generated\v121 --patches recomp\patches-v121.txt` writes the C (about 15 s). The patch table holds the 60 fps and real-time frame step code patches, switchable at run time (`recomp/patches.py`); `.venv-py314\Scripts\python tools\make_romfs_patches.py` writes their RomFS half to `build\v121\romfs-patches`. `tools\build_game.py` compiles and links `build\v121\totk.exe` with Ninja (about 200 s clean, incremental after); relinking a build that has `shader-prepared.bin` re-warms the GPU driver's cache, which is keyed to the exe (`totk.exe --warm-shaders`, about 3.5 min; `--no-warm` skips it). `recomp/layout.py` alone decides module bases. Jump tables are resolved by `recomp/jumptables.py`; after changing it, `tests\jumptables_check.py` must report 0 disagreements with its Unicorn oracle (on 1.4.3 too: `--exefs build\extracted\v143\exefs`).
- Game version 1.4.3: the same commands with `v143` (`--patches recomp\patches-v143.txt`, `build_game.py --version v143`, `make_romfs_patches.py --version v143`). Its builds default to `build\profile-v143`; `run_game.py` runs it on copies of the 1.2.1 save backups.
- Shader census (`.venv-py314`): `tools\shader_census.py --version v121 [--translate]` extracts every Maxwell shader to `build/v121/shaders/` and runs it through `build\xlate\totk_xlate.exe` and `spirv-val`; failure groups go to `failures.txt`. `tools\xlate_smoke.py` checks the translator alone, without game files.
- Shader preparation (`.venv-py314`, once per build dir and translator): `tools\prepare_shaders.py build\v121` translates every game shader's default variant and the variants of the MacNativeGames shared shader list (read from `refs/` at run time, with the rows its history dropped: `tools/shader_list.py`) into the build's shader cache and builds their stage libraries into the driver cache (`totk.exe --prepare-shaders`; 1.4.3: 29,587 variants, 7 min on 24 threads, 1.7 GB); play then translates and compiles only variants neither covers.
+68 -10
View File
@@ -155,7 +155,8 @@ static int remove_tree(const char *path) {
// <profile>/save-tx/new/ Commit's staging copy of the working copy
// <profile>/save-tx/old/ the committed save a Commit replaced, on its way to save-backup
//
// Commit: (1) copy work to new and flush every file (FlushFileBuffers) and directory; (2) rename save/<uid> to old;
// Commit: (1) copy work to new and flush every file (FlushFileBuffers) and directory, a file equal to the committed
// one as a hard link to it; (2) rename save/<uid> to old;
// (3) rename new to save/<uid>, the commit point; (4) move old to save-backup/<UTC time> and keep the newest three.
// Both renames are MoveFileExW with MOVEFILE_WRITE_THROUGH. save/<uid> is renamed away only after new is complete
// on disk, so the recovery rule at start (save_recover) is:
@@ -231,9 +232,40 @@ static int copy_file_w(const wchar_t *src, const wchar_t *dst, int durable, uint
return ok ? 0 : -1;
}
// Whether files a and b hold the same bytes; 0 when either cannot be read.
static int same_file_w(const wchar_t *a, const wchar_t *b) {
HANDLE h[2];
const wchar_t *p[2] = {a, b};
for (int i = 0; i < 2; i++)
h[i] = CreateFileW(p[i], GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
LARGE_INTEGER na, nb;
int same = h[0] != INVALID_HANDLE_VALUE && h[1] != INVALID_HANDLE_VALUE && GetFileSizeEx(h[0], &na) &&
GetFileSizeEx(h[1], &nb) && na.QuadPart == nb.QuadPart;
uint8_t *buf = same ? malloc(2 * (size_t)COPY_CHUNK) : NULL;
if (same) need(buf != NULL, "compare buffer");
while (same) {
DWORD ga = 0, gb = 0;
if (!ReadFile(h[0], buf, COPY_CHUNK, &ga, NULL) || !ReadFile(h[1], buf + COPY_CHUNK, COPY_CHUNK, &gb, NULL) ||
ga != gb || memcmp(buf, buf + COPY_CHUNK, ga))
same = 0;
else if (!ga)
break;
}
free(buf);
for (int i = 0; i < 2; i++)
if (h[i] != INVALID_HANDLE_VALUE) CloseHandle(h[i]);
return same;
}
// Copy the tree src to dst, which must not exist; reparse points are skipped. durable: every file is flushed, and
// every directory as far as the file system allows (NTFS journals the entries either way). 0 on success.
static int copy_tree_w(const wchar_t *src, const wchar_t *dst, int durable, int *nfiles, uint64_t *bytes) {
// every directory as far as the file system allows (NTFS journals the entries either way). base (or NULL): the
// committed save; a file whose bytes equal base's file at the same path becomes a hard link to it instead of a copy
// (*nlinked), as committed files are never written in place (the game writes the working copy). A save commit then
// writes the 3-4 files of the slot the game saved instead of all 82 (300-380 ms of flushes on the game thread).
// 0 on success.
static int copy_tree_w(const wchar_t *src, const wchar_t *dst, const wchar_t *base, int durable, int *nfiles,
int *nlinked, uint64_t *bytes) {
if (!CreateDirectoryW(dst, NULL)) return -1;
wchar_t *pattern = wjoin(src, L"*");
WIN32_FIND_DATAW fd;
@@ -246,14 +278,18 @@ static int copy_tree_w(const wchar_t *src, const wchar_t *dst, int durable, int
(fd.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT))
continue;
wchar_t *s = wjoin(src, fd.cFileName), *d = wjoin(dst, fd.cFileName);
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) bad = copy_tree_w(s, d, durable, nfiles, bytes);
wchar_t *b = base ? wjoin(base, fd.cFileName) : NULL;
if (b && !exists_w(b)) free(b), b = NULL;
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) bad = copy_tree_w(s, d, b, durable, nfiles, nlinked, bytes);
else {
bad = copy_file_w(s, d, durable, bytes);
if (b && same_file_w(s, b) && CreateHardLinkW(d, b, NULL)) bad = 0, ++*nlinked;
else bad = copy_file_w(s, d, durable, bytes);
*nfiles += !bad;
if (!bad && durable && crashed()) bad = -1;
}
free(s);
free(d);
free(b);
} while (!bad && FindNextFileW(f, &fd));
FindClose(f);
if (!bad && durable) {
@@ -338,9 +374,9 @@ static void save_recover(void) {
// The first save mount: a fresh working copy of the committed save.
static void work_create(void) {
int n = 0;
int n = 0, linked = 0;
uint64_t bytes = 0;
if (remove_tree_w(w_work) || copy_tree_w(w_save, w_work, 0, &n, &bytes))
if (remove_tree_w(w_work) || copy_tree_w(w_save, w_work, NULL, 0, &n, &linked, &bytes))
rt_fatal("fs: cannot make the save working copy (error %lu)", GetLastError());
rt_log("host save working copy: %d files, %.1f MiB", n, bytes / 1048576.0);
}
@@ -354,10 +390,11 @@ static void work_discard(void) {
// Commit of the save mount, tx_lock held. Steps (1) to (4) above; every failure before (3) leaves the committed save.
static uint32_t save_commit_locked(void) {
uint64_t t0 = plat_now_ns(), bytes = 0;
int nfiles = 0;
int nfiles = 0, linked = 0;
tx_step = tx_dead = 0;
rotate_backup(); // an old left by a failed (4)
if (remove_tree_w(w_new) || copy_tree_w(w_work, w_new, 1, &nfiles, &bytes)) {
const wchar_t *base = exists_w(w_save) ? w_save : NULL;
if (remove_tree_w(w_new) || copy_tree_w(w_work, w_new, base, 1, &nfiles, &linked, &bytes)) {
if (tx_dead) return FS_R_INVALID_ARGUMENT;
rt_log("fs: save commit: copying the working copy failed (error %lu); committed save unchanged",
GetLastError());
@@ -383,7 +420,8 @@ static uint32_t save_commit_locked(void) {
if (crashed()) return FS_R_INVALID_ARGUMENT;
rotate_backup();
InterlockedExchange(&save_dirty, 0);
rt_log("fs commit save: %d files, %.1f MiB in %.0f ms", nfiles, bytes / 1048576.0, (plat_now_ns() - t0) / 1e6);
rt_log("fs commit save: %d files, %d changed (%.1f MiB written), %d unchanged linked, in %.0f ms", nfiles,
nfiles - linked, bytes / 1048576.0, linked, (plat_now_ns() - t0) / 1e6);
return 0;
}
@@ -1338,6 +1376,18 @@ static int tx_read_committed(const char *name) {
return tx_byte(ok);
}
// Hard links of a file of the committed save on disk; 0 when absent.
static int tx_links_committed(const char *name) {
wchar_t *n = fs_wide(name), *p = wjoin(w_save, n);
HANDLE f = CreateFileW(p, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
free(n);
free(p);
BY_HANDLE_FILE_INFORMATION fi;
int links = f != INVALID_HANDLE_VALUE && GetFileInformationByHandle(f, &fi) ? (int)fi.nNumberOfLinks : 0;
if (f != INVALID_HANDLE_VALUE) CloseHandle(f);
return links;
}
static int count_dirs(const wchar_t *dir) {
wchar_t *pattern = wjoin(dir, L"*");
WIN32_FIND_DATAW fd;
@@ -1404,6 +1454,14 @@ static void probe_save_tx(const uint64_t *uid) {
int backups = count_dirs(w_backup);
CHECK(backups == SAVE_BACKUPS, "save-backup holds %d saves", backups);
// a Commit writes the changed file and links the unchanged one to the committed save it replaces
int kept = committed;
CHECK(tx_write(a, ++gen) && RES(S_COMMIT, U("save")) == 0 && tx_read_committed("tx_a.sav") == gen &&
tx_read_committed("tx_b.sav") == kept && tx_links_committed("tx_b.sav") >= 2 &&
tx_links_committed("tx_a.sav") == 1, "Commit of one changed file: tx_a %d links, tx_b %d links",
tx_links_committed("tx_a.sav"), tx_links_committed("tx_b.sav"));
committed = gen;
// the read-only mount shows the committed save and refuses changes
CHECK(tx_write(a, 99) && RES(S_MOUNT_SAVE_RO, U("ro"), TOTK_APPLICATION_ID, U(uid)) == 0 &&
tx_read("ro:/tx_a.sav") == committed && RES(S_OPEN_FILE, U(&h), U("ro:/tx_a.sav"), FS_OPEN_WRITE) ==
+1
View File
@@ -260,4 +260,5 @@ int probe_shader(void); // M4 T4
int probe_vkmem(void), probe_vkexec(void), probe_astc(void); // M4 T5, T6
int probe_movie(void); // movie_probe.c: host VP9 movie::Decoder on one RomFS movie
int vk_pipeline_prepare_shaders(void); // vk_pipeline.c: --prepare-shaders (tools/prepare_shaders.py)
int vk_pipeline_warm_shaders(void); // vk_pipeline.c: --warm-shaders (tools/build_game.py)
// gfx_stubs.c defines each of these weakly: prints "probe-x: not built yet", returns 2.
+2 -1
View File
@@ -388,7 +388,7 @@ static int usage(const char *argv0) {
fprintf(stderr, "usage: %s [--exefs DIR] [--romfs DIR] [--profile DIR] [--selftest | --probe-loader | "
"--probe-cfg | --probe-kernel | --probe-ipc | --probe-services | --probe-fs | --probe-gfx-abi | "
"--probe-nvn | --probe-nvn-tex | --probe-window | --probe-shader | --probe-vkmem | "
"--probe-vkexec | --probe-astc | --probe-movie | --prepare-shaders | --boot |--frames N [--shot PNG]]\n", argv0);
"--probe-vkexec | --probe-astc | --probe-movie | --prepare-shaders | --warm-shaders | --boot |--frames N [--shot PNG]]\n", argv0);
return 2;
}
@@ -488,6 +488,7 @@ int main(int argc, char **argv) {
if (!strcmp(mode, "--probe-vkdevice")) return probe_vkdevice();
if (!strcmp(mode, "--probe-movie")) return probe_movie();
if (!strcmp(mode, "--prepare-shaders")) return vk_pipeline_prepare_shaders();
if (!strcmp(mode, "--warm-shaders")) return vk_pipeline_warm_shaders();
if (!strcmp(mode, "--boot")) { cfg.boot = 1; rt_boot(); return 0; }
return usage(argv[0]);
}
+23
View File
@@ -2926,6 +2926,29 @@ int vk_pipeline_prepare_shaders(void) {
return rc;
}
// totk.exe --warm-shaders (tools/build_game.py after it relinked a prepared build): the driver's disk cache is keyed to
// the exe, so a rebuilt totk.exe finds it cold and a new library costs 40-220 ms in play instead of about 2 ms. This
// builds every variant of shader-prepared.bin into it again from the shader cache, without translating the seed.
int vk_pipeline_warm_shaders(void) {
if (!ensure_init()) {
fprintf(stderr, "warm-shaders: no Vulkan device\n");
return 1;
}
char dir[MAX_PATH * 2];
snprintf(dir, sizeof dir, "%s", P.cache_path);
char *cut = strrchr(dir, '\\');
if (!cut) return 1;
*cut = 0;
char prp[MAX_PATH * 2 + 32];
snprintf(prp, sizeof prp, "%s\\shader-prepared.bin", dir);
if (GetFileAttributesA(prp) == INVALID_FILE_ATTRIBUTES) {
fprintf(stderr, "warm-shaders: no %s (tools/prepare_shaders.py writes it)\n", prp);
return 1;
}
lb_warm_prepared(dir);
return 0;
}
// ---- dynamic state
void vk_pipeline_set_dynamic(VkCommandBuffer cb, const VkPipeState *s) {
+26
View File
@@ -567,6 +567,27 @@ def mib(n):
return f'{n / (1 << 20):,.1f} MiB'
def warm_driver_cache(exe):
"""Rebuild the GPU driver's disk cache of a prepared build after its totk.exe changed.
tools/prepare_shaders.py builds every prepared variant's stage library once so the driver's own disk cache holds it
(vk_pipeline.c "stage library prebuild"). That cache is keyed to the exe: after a relink, a library a draw needs
took 40-220 ms in play instead of about 2 ms. totk.exe --warm-shaders builds them again from the shader cache
(about 3.5 min on 24 threads), without translating. Builds without shader-prepared.bin are left alone.
"""
if not (exe.parent / 'shader-prepared.bin').is_file():
return
print('build_game: totk.exe changed in a prepared build: warming the driver cache (--no-warm skips it)', flush=True)
t0 = time.monotonic()
r = subprocess.run([str(exe), '--warm-shaders'], cwd=str(exe.parent), stdout=subprocess.PIPE, text=True,
errors='replace')
last = (r.stdout.strip().splitlines() or [''])[-1]
print(f'warm: rc={r.returncode}, {time.monotonic() - t0:.0f} s; {last}')
if r.returncode:
print(f'build_game: warning: the driver cache stays cold; run python tools/prepare_shaders.py '
f'{below_root(exe.parent)}', file=sys.stderr)
def main():
ap = argparse.ArgumentParser(description=__doc__.splitlines()[0])
ap.add_argument('--version', default='v121', help='game version (default v121)')
@@ -600,6 +621,8 @@ def main():
f'({", ".join(sorted(INSTALL_SIDE))} and modules.c still compile)')
ap.add_argument('--expected', type=Path,
help='expected hashes (default tools/data/expected-<display version>.json)')
ap.add_argument('--no-warm', action='store_true',
help='leave the driver cache cold after relinking a prepared build (see warm_driver_cache)')
args = ap.parse_args()
gen = (args.gen or ROOT / 'generated' / args.version).resolve()
@@ -706,6 +729,7 @@ def main():
return
if args.make_runtime_lib:
exe.unlink(missing_ok=True) # ar rcs would keep members of deleted sources
before = exe.stat().st_mtime_ns if exe.exists() else 0
code, dt, peak_one, _ = run_measured(base + [flag_path(exe)], env)
print(f'link: {dt:.1f} s, peak {mib(peak_one)}')
if code:
@@ -722,6 +746,8 @@ def main():
f'image {mib(image)}; ' + ', '.join(f'{k} {mib(v)}' for k, v in sections.items() if k.startswith('.') and v))
if image > 3 * GiB // 2:
print('build_game: warning: image over 1.5 GiB; RIP-relative code reaches only 2 GiB', file=sys.stderr)
if exe.stat().st_mtime_ns != before and not args.no_warm:
warm_driver_cache(exe)
if __name__ == '__main__':