From fe5bff83284770912ebdc47272147024d4c98c78 Mon Sep 17 00:00:00 2001 From: Leyukaka <45659323+Leyukaka@users.noreply.github.com> Date: Fri, 13 Nov 2026 00:10:02 +0100 Subject: [PATCH] Keep the pipeline cache files under their limit when writing them, and set the limit from the RAM. The limit was only checked when a file was read, all or nothing, so a run could write a file the next start then dropped whole. On an RTX 5080 (0.9.2) pipeline-cache-play.bin reached 524.9 MB in one session and the next start began empty; pipeline-cache.bin grows 14-28 MB at each fold and crosses 512 MB within a few sessions. Now a fold is made only when its result fits, and a cache past the limit is not saved again, so the file keeps its last save under the limit. Unset, the limit is the RAM / 32 between 512 and 4096 MB (512 on 16 GB as before, 2048 on 64 GB), which on that PC took boot hits from 14 to 84 % and left no pipeline to background builds. Taken into the 0.10.0 line with its conflicts resolved: the line had raised the limit to 1024 MB and says why in the comment above cache_data_read, in nx2navi.ini.example and in AUDIT.md; those texts keep that reason and give the RAM-based default, whose floor the next commit raises to match. --- AUDIT.md | 2 +- nx2navi.ini.example | 10 +-- runtime/cfg.c | 2 +- runtime/vk_pipeline.c | 97 +++++++++++++++++++++++++++--- tests/cache_exit_snapshot_check.py | 16 ++++- 5 files changed, 113 insertions(+), 14 deletions(-) diff --git a/AUDIT.md b/AUDIT.md index 16a58342..194ec94d 100644 --- a/AUDIT.md +++ b/AUDIT.md @@ -1150,7 +1150,7 @@ back. | 115 | Pipeline layouts, set layouts, update templates | `P.lock` | None found | Shutdown | | 116 | Stage libraries `libs[1024]` chained; failed builds cached as null | `P.lock` | None found | Shutdown (`libs_destroy`) | | 117 | Library and pipeline job queues | `P.lock` | None found | When built; dropped at shutdown | -| 118 | `VkPipelineCache` in memory and `pipeline-cache.bin` | `cache_rw`, `save_lock` | File > `NX2NAVI_PIPELINE_CACHE_MAX_MB` (1024) starts empty; in memory: the driver's, none found | Saved by "pipe autosave" at most every 20 s and at exit (waits ≤ 5 s) | +| 118 | `VkPipelineCache` in memory and `pipeline-cache.bin` | `cache_rw`, `save_lock` | Each file > `NX2NAVI_PIPELINE_CACHE_MAX_MB` (RAM / 32, 1024 to 4096 MB) starts empty, and no cache past it is written; in memory: the driver's, none found | Saved by "pipe autosave" at most every 20 s and at exit (waits ≤ 5 s) | | 119 | Pipeline binaries `pipeline-binaries.bin` | `PB.lock` | `NX2NAVI_PIPE_BINARIES_MAX_MB` 1024; `NX2NAVI_PIPE_BINARIES` default 0 | — | | 120 | `pipeline-keys.bin` and its `seen` set | `K.io` | None found (append-only) | Never | | 121 | `compute-used.bin` | `cu_io` | `NX2NAVI_COMPUTE_KEEP` 512 | — | diff --git a/nx2navi.ini.example b/nx2navi.ini.example index a9beb1a8..d4978eda 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -209,10 +209,12 @@ # build_reserve_mb = 64 # 1: the log says how many pipelines came from pipeline-cache.bin; 2 also logs every compute pipeline built. 0: off. # pipe_feedback = 1 -# pipeline-cache.bin larger than this (MB) starts empty and is rewritten (0: no limit). With the pipeline list of -# 0.9.1 and later a full cache is about 550 MB on an NVIDIA card, so the limit has to stay well above that: under -# it the cache is thrown away at every start and the pipelines are built again each time. Default: 1024. -# pipeline_cache_max_mb = 1024 +# The limit of pipeline-cache.bin and of pipeline-cache-play.bin, each, in MB (0: no limit). A file larger than +# this starts empty, so the game never writes one past it: once a cache reaches the limit, pipelines compiled after +# that are compiled again at the next start. With the pipeline list of 0.9.1 and later a full cache is about 550 MB +# on an NVIDIA card, so the limit has to stay well above that. Default: your RAM / 32, between 1024 and 4096 (2048 +# with 64 GB). +# pipeline_cache_max_mb = 2048 # 1: the pipelines a run compiles go into pipeline-cache-play.bin, a small file of its own, so pipeline-cache.bin # (hundreds of MB) is not written again while you play; the small file is folded into the large one at a start once # it has grown. 0: one file, written whole whenever a run made a new pipeline. diff --git a/runtime/cfg.c b/runtime/cfg.c index f476a23b..9c9f055b 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -325,7 +325,7 @@ static const CfgMeta metas[] = { {"PIPE_PREBUILD", "1", "0|1", RESTART}, {"PIPE_SEED", "1", "0|1", RESTART}, {"PIPE_SEED_BOOT", "auto", "0|1|auto", RESTART}, // vk_pipeline.c keys_open_seed {"PREWARM_XLATE", "", "1..64", RESTART}, {"GPL", "", "0|1", RESTART}, // vk_caps.c vk_caps_gpl: by the driver, off with AMD's for RDNA 2 and older {"LIB_PREBUILD_BIG_BG", "1", "0|1", RESTART}, {"PIPE_FEEDBACK", "1", "0|1|2", RESTART}, - {"PIPELINE_CACHE_MAX_MB", "1024", "0..65536", RESTART}, + {"PIPELINE_CACHE_MAX_MB", "", "0..65536", RESTART}, // vk_pipeline.c cache_cap: unset, from the RAM (1024 to 4096) {"FFX_PIPE_CACHE", "1", "0|1", RESTART}, // vk_ffx.cpp vk_ffx_proc_addr: FSR 3.1's pipelines through the pipeline caches {"PIPELINE_CACHE_PLAY", "1", "0|1", RESTART}, {"PIPELINE_CACHE_PLAY_MB", "64", "1..65536", RESTART}, // vk_pipeline.c "the play cache": what a run compiles goes into a file of its own {"PIPE_BINARIES", "0", "0|1", RESTART}, {"ONESHOT_WAIT_MS", "30000", "0..60000", RESTART}, {"ONESHOT_BUDGET", "1", "0|1", RESTART}, {"PIPE_BINARIES_MAX_MB", "1024", "0..65536", RESTART}, {"BIG_BUILD_FREE_MB", "8192", "0..262144", RESTART}, diff --git a/runtime/vk_pipeline.c b/runtime/vk_pipeline.c index 44341bd4..bb40b491 100644 --- a/runtime/vk_pipeline.c +++ b/runtime/vk_pipeline.c @@ -753,12 +753,12 @@ static VkResult pipe_create(int kind, void *ci, VkPipeline *pipe) { // The data of the pipeline cache file at path for vkCreatePipelineCache (malloc'd, its size in *size), NULL when the // run starts without it, which the log says with the file's size. Cache data whose header names another device or // driver is dropped before Vulkan sees it. -// NX2NAVI_PIPELINE_CACHE_MAX_MB (default 1024; 0 no limit): a larger file starts the run empty, and the run's -// saves replace it. The limit has to stay above what one run builds, or nothing ever replaces the file: at 512, with -// the pipeline list of 0.9.1 (8,849 records), an RTX 2080 Ti's file held 551 MB and the play file the run then wrote -// 541 MB, so both were dropped at every start, 7,247 of 8,531 pipelines were built again in each (17 s of the -// boot) and 541 MB were written again in each session (cache_load says so when both files are over). The file only -// grows: entries of earlier translators and builds stay, and on NVIDIA +// NX2NAVI_PIPELINE_CACHE_MAX_MB (cache_cap; 0 no limit): a larger file starts the run empty, and the run's +// saves replace it. The limit bounds each of the two files, not their sum. It has to stay above what one run +// builds: at 512, with the pipeline list of 0.9.1 (8,849 records), an RTX 2080 Ti's file held 551 MB and the play +// file the run then wrote 541 MB, so both were dropped at every start, 7,247 of 8,531 pipelines were built again in +// each (17 s of the boot) and 541 MB were written again in each session (cache_load says so when both files are +// over). The file only grows: entries of earlier translators and builds stay, and on NVIDIA // (621.01) an entry another executable wrote is neither used nor written again, so a renamed or updated exe // compiles while the file keeps its size (2026-11-09: 650 MB, 15-18 % of the pipelines read from it). The // whole file sits in process memory twice at the boot (this read and the driver's copy) and is copied out @@ -766,13 +766,45 @@ static VkResult pipe_create(int kind, void *ci, VkPipeline *pipe) { // read, so a file that is dropped never takes its size in memory. Sizes are 64-bit all the way // (vk_cache_file.h cache_file_read): a file past 2 GB is read with the limit raised or off, and refused by // its real size otherwise. +// The limit is kept on the write side too (cache_load's fold, cache_save_one): a run never writes a file the next start +// would refuse. Before, only the read checked it, all or nothing, and nothing kept the files under it: on an RTX 5080 +// (0.9.2, 2026-11-12) pipeline-cache-play.bin reached 524.9 MB in one session of about 15 minutes, the next start +// dropped it whole ("starting empty") and the session after compiled its pipelines again (boot hits 13-18 %, about +// 5,000 pipelines left to background builds at 250-273 ms each). pipeline-cache.bin itself grows 14-28 MB at each +// fold (323 -> 448 -> 461 -> 489 MB in four starts) and so crosses 512 MB in a few sessions (0.8.0: a 766 MB file +// dropped at a start). With the limit at 2048 MB the same files were read and folded: boot hits 84 %, nothing left to +// background builds, the boot 17 s instead of 24. +// cache_cap: NX2NAVI_PIPELINE_CACHE_MAX_MB when set (0: no limit); unset, the machine's RAM / 32 between 512 and +// 4096 MB (16 GB: 512 as before; 32 GB: 1024; 64 GB: 2048). The RAM and not the commit headroom at the boot, so the +// same file is not read on one start and refused on the next. +static uint64_t cache_cap(void) { + static uint64_t cap = UINT64_MAX; + if (cap != UINT64_MAX) return cap; + const char *set = cfg_get("NX2NAVI_PIPELINE_CACHE_MAX_MB"); + float used = 0, total = 0, proc = 0; + uint64_t mb; + if (set && *set) mb = strtoull(set, NULL, 10); + else { + plat_mem_info(&used, &total, &proc); + mb = total > 0 ? (uint64_t)total / 32 : 512; + mb = mb < 512 ? 512 : mb > 4096 ? 4096 : mb; + } + if (set && *set) + rt_log("vk_pipeline: pipeline cache files up to %llu MB%s (NX2NAVI_PIPELINE_CACHE_MAX_MB)", + (unsigned long long)mb, mb ? "" : ": no limit"); + else + rt_log("vk_pipeline: pipeline cache files up to %llu MB (from %.0f GB of RAM; NX2NAVI_PIPELINE_CACHE_MAX_MB " + "sets it)", (unsigned long long)mb, total / 1024.0); + return cap = mb << 20; +} + static int cache_over_cap; // the last file cache_data_read was asked for is over the limit static int cache_foreign; // pipeline-cache.bin was written by another device or driver (a driver update) static void *cache_data_read(VkCtx *c, const char *path, size_t *size) { *size = 0; cache_over_cap = 0; if (!path[0]) return NULL; - uint64_t cap = (uint64_t)strtoull(cfg_value("NX2NAVI_PIPELINE_CACHE_MAX_MB"), NULL, 10) << 20, len = 0; + uint64_t cap = cache_cap(), len = 0; int why = CACHE_FILE_NONE; void *data = cache_file_read(path, sizeof(VkPipelineCacheHeaderVersionOne), cap, &len, &why); if (!data) { @@ -780,6 +812,7 @@ static void *cache_data_read(VkCtx *c, const char *path, size_t *size) { cache_over_cap = 1, rt_log("vk_pipeline: %s holds %.0f MB, over NX2NAVI_PIPELINE_CACHE_MAX_MB %llu: starting empty; it is " "replaced when the play file is next folded in, at the next start that finds one", path, len / 1048576.0, (unsigned long long)(cap >> 20)); + // (A file over the limit comes from before the limit was kept on the write side, or from a lower limit.) else if (why != CACHE_FILE_NONE) rt_log("vk_pipeline: %s (%llu bytes, %.1f MB) is not read: %s; starting empty", path, (unsigned long long)len, len / 1048576.0, cache_file_why(why)); @@ -851,6 +884,38 @@ static void cache_load(VkCtx *c) { free(pdata); uint64_t fold_at = (uint64_t)strtoull(cfg_value("NX2NAVI_PIPELINE_CACHE_PLAY_MB"), NULL, 10) << 20; int fold = play && psize && (!on || psize >= fold_at || base_over); + if (fold && on && !base_over && cache_cap()) { + // Only a fold whose result fits: pipeline-cache.bin past the limit would be dropped whole at the next start + // (cache_data_read). The merge into P.cache is the one the kept play cache gets below anyway, so it is done + // here once and its size asked; a play file mostly holds what the other already has (461.2 + 524.9 MB folded + // to 489.0 MB, 2026-11-12), so the size of the two files says little. + VkResult r = vkMergePipelineCaches(c->device, P.cache, 1, &play); + size_t merged = 0; + if (r == VK_SUCCESS && vkGetPipelineCacheData(c->device, P.cache, &merged, NULL) == VK_SUCCESS && + merged > cache_cap()) { + rt_log("vk_pipeline: %s: %.1f MB read, the driver holds %.1f MB of it; folding it in would make %s %.1f MB, " + "over the limit of %llu MB: both files stay as they are, and this run's pipelines go into the play " + "file as long as it stays under the limit", P.play_path, psize / 1048576.0, pkept / 1048576.0, + P.cache_path, merged / 1048576.0, (unsigned long long)(cache_cap() >> 20)); + fold = 0, psize = 0; // merged already: the branch for a kept play file below is skipped + } else if (r != VK_SUCCESS) { + rt_log("vk_pipeline: %s did not merge into the cache of %s (%d); it is left as it is and not used in this run", + P.play_path, P.cache_path, (int)r); + vkDestroyPipelineCache(c->device, play, NULL); + play = VK_NULL_HANDLE, fold = 0, on = 0, psize = 0; + } else { + rt_log("vk_pipeline: folding %s in makes %s %.1f MB (the limit is %llu MB)", P.play_path, P.cache_path, + merged / 1048576.0, (unsigned long long)(cache_cap() >> 20)); + vkDestroyPipelineCache(c->device, play, NULL); + play = VK_NULL_HANDLE; + P.fold = 1, P.saved[0] = 0; + rt_log("vk_pipeline: %s: %.1f MB read, the driver holds %.1f MB of it; merged into the cache of %s, which is " + "written once now (it reached the size at which it is folded in, before play)", P.play_path, + psize / 1048576.0, pkept / 1048576.0, P.cache_path); + play = cache_make(c, NULL, 0, &pkept), psize = 0; + fold = 0; // done + } + } if (fold) { // Into P.cache now; the file is written by the first save, which then removes the play file. A run that ends // before that save finds both files as they were and merges again. @@ -8339,6 +8404,24 @@ static int cache_save_one(int which, uint32_t wait_ms, int exiting) { size_t size = 0; void *data = NULL; int sized = vkGetPipelineCacheData(P.device, cache, &size, NULL) == VK_SUCCESS && size; + // A cache past the limit is not written: the next start would drop the file whole (cache_data_read), so the file + // keeps its last save under the limit and only the pipelines made since then are compiled again. A preparation + // writes anyway: what it builds is the base every start reads, and a file it cannot write would leave none. + static int over_said[2]; + uint64_t cap = cache_cap(); + if (sized && cap && size > cap && !prep_mode) { + plat_mutex_unlock(&cache_rw); + if (exiting) __atomic_store_n(&cache_saving_at_exit, 0, __ATOMIC_RELEASE); + if (!over_said[which]++) + rt_log("vk_pipeline: %s not written past its last save: the cache holds %.1f MB, over the limit of %llu MB; " + "pipelines made from here on compile again at the next start (NX2NAVI_PIPELINE_CACHE_MAX_MB raises " + "the limit)", path, size / 1048576.0, (unsigned long long)(cap >> 20)); + return 1; + } + if (sized && cap && size > cap) + rt_log("vk_pipeline: %s holds %.1f MB, over the limit of %llu MB: written for the preparation, and dropped at " + "the next start unless NX2NAVI_PIPELINE_CACHE_MAX_MB is raised", path, size / 1048576.0, + (unsigned long long)(cap >> 20)); int same = sized && size == P.saved[which]; int got = sized && !same && (data = malloc(size)) && vkGetPipelineCacheData(P.device, cache, &size, data) == VK_SUCCESS; plat_mutex_unlock(&cache_rw); diff --git a/tests/cache_exit_snapshot_check.py b/tests/cache_exit_snapshot_check.py index a734cb62..d90f6d17 100644 --- a/tests/cache_exit_snapshot_check.py +++ b/tests/cache_exit_snapshot_check.py @@ -39,6 +39,9 @@ static volatile int32_t cache_saving_at_exit; static volatile LONG active, snapshots; static HANDLE old_in, release_old, new_in, file_in, release_file; static int block_file, fail_file, writes, save_result; +static uint64_t mock_cap; // cache_cap: 0 no limit +static int prep_mode; +static uint64_t cache_cap(void) { return mock_cap; } static struct { VkPipelineCache cache, play; int device, fold, feedback; @@ -143,7 +146,18 @@ int main(void) { assert(plat_mutex_trylock(&cache_rw)); assert(cache_save_one(0, 0, 1) == 0 && writes == before && !cache_saving_at_exit); plat_mutex_unlock(&cache_rw); - puts("cache save at exit: builds held back and waited for, autosave unchanged, timeout, failures and merges passed"); + + // A cache past the limit is not written (the next start would drop the file), holds no build back and lets the + // next save run; a preparation writes it anyway. + P.saved[0] = 0; mock_cap = 2; before = writes; + assert(cache_save_one(0, 100, 1) == 1 && writes == before && !cache_saving_at_exit && P.saved[0] == 0); + assert(cache_save_one(0, 0, 0) == 1 && writes == before); + cache_build_enter(); plat_mutex_unlock_shared(&cache_rw); + prep_mode = 1; + assert(cache_save_one(0, 100, 1) == 1 && writes == before + 1); + prep_mode = 0; mock_cap = 0; + puts("cache save at exit: builds held back and waited for, autosave unchanged, timeout, failures, merges and the " + "size limit passed"); return 0; } '''