Hold new pipeline builds back during the save at exit on every driver, instead of copying the cache while NVIDIA's builds run.

The save at exit now copies the cache only once the builds already in the driver have finished, with new ones held back meanwhile, as it already did on other drivers. Intel's driver crashed copying a cache while builds ran into it, and when the window closes the game's save is written after the pipeline cache, so a crash in that copy would lose it. The case the save at exit missed, a window closed while the boot's prewarm workers built pipelines back to back, is covered by holding new builds back: a build from the file takes about half a millisecond. The prebuild's comment keeps why its default is two threads, with the measurements of one thread for the rest of the list.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-11 18:12:47 -05:00
parent 27f93a314a
commit 15bd64f4b8
3 changed files with 60 additions and 86 deletions

View File

@@ -872,7 +872,8 @@ int vk_pipeline_set_indep(int on);
typedef struct { uint64_t graphics, compute, failures, hits, layouts, push_layouts, async_built, pending_skips; } VkPipeStats;
void vk_pipeline_stats(VkPipeStats *s);
void vk_pipeline_save(void); // writes the VkPipelineCache to pipeline-cache.bin next to nx2navi.exe
void vk_pipeline_save_exit(void); // final snapshot: briefly stop new cache builds, with the same bounded wait
void vk_pipeline_save_exit(void); // vk_pipeline_save as the process ends: new builds wait while it waits for
// the running ones
void vk_pipeline_autosave(void); // vk_pipeline_save at most every 20 s, when pipelines were built since
void vk_pipeline_shutdown(void); // waits for background builds, saves, destroys pipelines, layouts and the
// cache; before vk_shutdown

View File

@@ -521,18 +521,20 @@ static void pb_log(const char *when) {
// compiled with P.cache and its binaries captured. The create info is restored before returning.
typedef struct { VkStructureType sType; const void *pNext; VkPipelineCreateFlags flags; } PipeCiHead;
// Builds into P.cache against reads of its data. Every create with P.cache holds this shared (pipe_create_in); the
// normal autosave holds it exclusively and only takes it when no build is running. The NVIDIA exit snapshot uses
// shared access too, so an unfinished compile cannot prevent saving completed data; merges still take exclusive
// access. The Vulkan rules
// allow reading a cache created without VK_PIPELINE_CACHE_CREATE_EXTERNALLY_SYNCHRONIZED_BIT while pipelines are
// built into it, but Intel's Windows driver (101.8991 on an Arc 140V) crashed inside vkGetPipelineCacheData during the
// copy, reading through a null entry, with compute and library builds running beside the autosave.
// Builds into P.cache against reads of its data. Every create with P.cache holds this shared (pipe_create_in); a save
// holds it exclusively for the copy of the data and only takes it when no build is running. The Vulkan rules allow
// reading a cache created without VK_PIPELINE_CACHE_CREATE_EXTERNALLY_SYNCHRONIZED_BIT while pipelines are built into
// it, but Intel's Windows driver (101.8991 on an Arc 140V) crashed inside vkGetPipelineCacheData during the copy,
// reading through a null entry, with compute and library builds running beside the autosave. So no copy is made while
// a build runs, on any driver.
static PlatMutex cache_rw = PLAT_MUTEX_INIT;
// Set while the save at exit waits for cache_rw: the builds already in the driver finish, and new ones wait in
// cache_build_enter until the copy is made. Without it the boot's prewarm workers started builds back to back, no moment
// without one came within the save's 5 s, and the pipelines built in that run were not saved (a window closed during
// the prewarm, 2026-11-11). The autosave never sets it, so it never holds a build back.
static volatile int32_t cache_saving_at_exit;
// Normal autosaves never hold new builds back. At process exit, let existing
// driver calls finish without admitting more until the cache has been copied.
// Takes cache_rw shared for a build into P.cache; while the save at exit waits for its copy, first waits for that.
static void cache_build_enter(void) {
for (;;) {
while (__atomic_load_n(&cache_saving_at_exit, __ATOMIC_ACQUIRE)) plat_sleep_ms(1);
@@ -6175,10 +6177,16 @@ static void lb_start(const uint32_t pr_hist[16], const uint32_t fs_hist[4]) {
LB.n = n;
LB.idle = 1;
int cpus = (int)plat_cpu_usable();
// Two threads for the recent-pipeline prefix by default, then one for the remaining cache-warming work. Lower
// tail concurrency leaves more CPU/driver capacity for rendering without removing any prepared variants. An
// explicit thread setting retains its requested concurrency throughout. A cold driver can spend far longer on
// a request than the idle-priority shader loader; fewer concurrent builds also reduce driver contention.
// Two threads by default, not half the CPUs. On a 32-thread CPU with NVIDIA's driver, 16 builders kept the driver's
// lock busy: 77% of the process's running time was a spin loop in nvoglv64.dll (about 8 cores), the prebuild had not
// finished after 34,000 frames, and lib_prebuild = 0 took it to 5% and the game to its frame limit (RTX 4080 SUPER,
// driver 617.14, 2026-11-08). With the driver's disk cache warm (--prepare-shaders, --warm-shaders) a build takes
// well under a millisecond, so two threads still finish the list quickly.
// The two take the shaders dispatched most recently (e[0..n_keep)); one goes on with the rest (lb_next), which is
// only built into the caches. Where the rest still runs during play, two builders there cost frames: one against
// two for the whole list, one 20 s route each on an RTX 5090, Castle Town 76.1 against 73.7 fps with 1 frame over
// 25 ms against 6, Hyrule Field 85.7 against 80.7 fps. NX2NAVI_LIB_PREBUILD_THREADS keeps its count for the whole
// list.
int t = te && *te ? atoi(te) : (cpus < 4 ? 1 : 2); // idle priority; a cold driver takes ~2 s per large compute shader
LB.tail_one = !(te && *te);
t = t < 1 ? 1 : t > LB_MAX_THREADS ? LB_MAX_THREADS : t;
@@ -6753,23 +6761,19 @@ static PlatMutex save_lock = PLAT_MUTEX_INIT; // one cache save at a time; shu
// every save copied and wrote the whole cache whenever a pipeline had been created since the last one, also when the
// cache had held it: three saves of 358.1 MB in one run that added 0.3 MB (2026-11-11).
// The lock is only ever tried, never waited for: a waiting exclusive request would hold back every new build
// (a shared request queues behind it), so a pending autosave could stall the replay. Only the final process-exit
// snapshot uses shared access on NVIDIA; other drivers retain the admission gate and exclusive snapshot, including
// the reported Intel concurrent-read failure above. wait_ms: how long to keep trying
// while builds run (0: one try). Returns 0 when no build-free moment came, -1 when the data could not be copied or
// its file not written (the log says which), 1 otherwise (saved, or nothing to save).
// (a shared request queues behind it), so a pending autosave could stall the replay. The save at exit (exiting) holds
// new builds back instead (cache_saving_at_exit) and so only waits for the builds already running. wait_ms: how long to
// keep trying while builds run (0: one try). Returns 0 when no build-free moment came, -1 when the data could not be
// copied or its file not written (the log says which), 1 otherwise (saved, or nothing to save).
static int cache_save_one(int which, uint32_t wait_ms, int exiting) {
VkPipelineCache cache = which ? P.play : P.cache;
const char *path = which ? P.play_path : P.cache_path;
if (!cache) return 1;
VkCtx *ctx = vk_ctx();
int concurrent = exiting && ctx && ctx->props.vendorID == 0x10de;
int gated = exiting && !concurrent;
uint64_t until = plat_now_ms() + wait_ms;
if (gated) __atomic_store_n(&cache_saving_at_exit, 1, __ATOMIC_RELEASE);
while (!(concurrent ? plat_mutex_trylock_shared(&cache_rw) : plat_mutex_trylock(&cache_rw))) {
if (exiting) __atomic_store_n(&cache_saving_at_exit, 1, __ATOMIC_RELEASE);
while (!plat_mutex_trylock(&cache_rw)) {
if (plat_now_ms() >= until) {
if (gated) __atomic_store_n(&cache_saving_at_exit, 0, __ATOMIC_RELEASE);
if (exiting) __atomic_store_n(&cache_saving_at_exit, 0, __ATOMIC_RELEASE);
return 0;
}
plat_sleep_ms(2);
@@ -6778,26 +6782,10 @@ static int cache_save_one(int which, uint32_t wait_ms, int exiting) {
void *data = NULL;
int sized = vkGetPipelineCacheData(P.device, cache, &size, NULL) == VK_SUCCESS && size;
int same = sized && size == P.saved[which];
int got = 0;
for (int attempt = 0; sized && !same && attempt < (concurrent ? 3 : 1); attempt++) {
// The cache may grow between the size query and read. A little spare capacity absorbs small additions; an
// incomplete read is retried from a fresh size query. Never replace the last saved cache with a partial one.
size_t spare = concurrent ? 65536 : 0;
if (size > SIZE_MAX - spare) break;
size_t capacity = size + spare;
if (!(data = malloc(capacity))) break;
size = capacity;
VkResult r = vkGetPipelineCacheData(P.device, cache, &size, data);
if (r == VK_SUCCESS && size && size <= capacity) { got = 1; break; }
free(data), data = NULL;
if (!concurrent || r != VK_INCOMPLETE) break;
sized = vkGetPipelineCacheData(P.device, cache, &size, NULL) == VK_SUCCESS && size;
}
if (concurrent) plat_mutex_unlock_shared(&cache_rw);
else plat_mutex_unlock(&cache_rw);
// Release admission before logging, file IO or taking any other lock. A
// paused builder may still own its caller's pipeline or library lock.
if (gated) __atomic_store_n(&cache_saving_at_exit, 0, __ATOMIC_RELEASE);
int got = sized && !same && (data = malloc(size)) && vkGetPipelineCacheData(P.device, cache, &size, data) == VK_SUCCESS;
plat_mutex_unlock(&cache_rw);
// The held-back builds go on before the log line and the file write: one of them may hold a lock those wait for.
if (exiting) __atomic_store_n(&cache_saving_at_exit, 0, __ATOMIC_RELEASE);
static uint64_t skipped[2];
int failed = !sized || (!same && !got);
if (same) skipped[which]++;
@@ -6913,11 +6901,13 @@ static int save_with(uint32_t wait_ms, int exiting) {
return saved;
}
// The save at exit and after the shader preparation: it waits up to SAVE_WAIT_MS for a moment with no build running.
// The save after the shader preparation and at shutdown: it waits up to SAVE_WAIT_MS for a moment with no build running.
void vk_pipeline_save(void) {
save_with(SAVE_WAIT_MS, 0);
}
// The save as the process ends (window closed, the game's exit, a timed run's end): new builds wait while it waits up to
// SAVE_WAIT_MS for the running ones to finish (cache_saving_at_exit).
void vk_pipeline_save_exit(void) {
save_with(SAVE_WAIT_MS, 1);
}

View File

@@ -1,4 +1,7 @@
"""Windows: exercise exit snapshots during builds, growing cache data, and the conservative other-driver path."""
"""Windows: the pipeline cache save at exit holds new builds back, waits for the running ones, and copies the cache only
while no build runs; the autosave never holds a build back. Builds runtime/vk_pipeline.c's cache_build_enter and
cache_save_one into a test program against stand-ins for the driver and the file system.
Usage: python tests/cache_exit_snapshot_check.py [path to clang]"""
from pathlib import Path
import subprocess
import sys
@@ -29,12 +32,8 @@ typedef SRWLOCK PlatMutex;
typedef void *VkPipelineCache;
typedef int VkResult;
#define VK_SUCCESS 0
#define VK_INCOMPLETE 5
typedef struct { struct { unsigned vendorID; } props; } VkCtx;
static VkCtx context = {{0x8086}};
static VkCtx *vk_ctx(void) { return &context; }
static size_t mock_size = 4;
static int grow_once, grow_always, query_fail;
static int query_fail;
static PlatMutex cache_rw = SRWLOCK_INIT;
static volatile int32_t cache_saving_at_exit;
static volatile LONG active, snapshots;
@@ -49,7 +48,6 @@ static struct {
static void plat_mutex_lock_shared(PlatMutex *m) { AcquireSRWLockShared(m); }
static void plat_mutex_unlock_shared(PlatMutex *m) { ReleaseSRWLockShared(m); }
static int plat_mutex_trylock(PlatMutex *m) { return TryAcquireSRWLockExclusive(m); }
static int plat_mutex_trylock_shared(PlatMutex *m) { return TryAcquireSRWLockShared(m); }
static void plat_mutex_unlock(PlatMutex *m) { ReleaseSRWLockExclusive(m); }
static void plat_sleep_ms(uint32_t ms) { Sleep(ms); }
static uint64_t plat_now_ms(void) { return GetTickCount64(); }
@@ -58,7 +56,7 @@ static void fb_log(const char *when) { (void)when; assert(!cache_saving_at_exit)
static void plat_file_delete(const char *path) { (void)path; }
static int plat_file_write_atomic(const char *path, const void *data, size_t size) {
(void)path;
assert(!cache_saving_at_exit); // admission must reopen before IO and diagnostics
assert(!cache_saving_at_exit); // builds go on before the file write
assert(size == mock_size && !memcmp(data, "test", 4));
writes++;
if (block_file) {
@@ -69,12 +67,9 @@ static int plat_file_write_atomic(const char *path, const void *data, size_t siz
}
static int vkGetPipelineCacheData(int device, VkPipelineCache cache, size_t *size, void *data) {
(void)device; (void)cache;
if (context.props.vendorID != 0x10de) assert(!active);
assert(!active); // never a copy while a build runs, on any driver
InterlockedIncrement(&snapshots);
if (!data && query_fail) return -1;
if (data && (grow_once || grow_always)) {
grow_once = 0; mock_size = *size + 100; return VK_INCOMPLETE;
}
if (data) { assert(*size >= mock_size); memcpy(data, "test", 4); }
*size = mock_size;
return VK_SUCCESS;
@@ -105,6 +100,9 @@ int main(void) {
new_in = CreateEventW(NULL, TRUE, FALSE, NULL);
file_in = CreateEventW(NULL, TRUE, FALSE, NULL);
release_file = CreateEventW(NULL, TRUE, FALSE, NULL);
// The save at exit: a build is running; a build that comes while the save waits is held back until the copy is
// made, and goes on before the file is written.
HANDLE old = CreateThread(NULL, 0, builder, (void *)1, 0, NULL);
assert(WaitForSingleObject(old_in, 2000) == WAIT_OBJECT_0);
block_file = 1;
@@ -117,50 +115,35 @@ int main(void) {
assert(WaitForSingleObject(new_in, 20) == WAIT_TIMEOUT);
SetEvent(release_old);
assert(WaitForSingleObject(file_in, 2000) == WAIT_OBJECT_0);
assert(WaitForSingleObject(new_in, 2000) == WAIT_OBJECT_0); // IO does not hold admission closed
assert(WaitForSingleObject(new_in, 2000) == WAIT_OBJECT_0);
SetEvent(release_file);
join(old); join(next); join(saving);
assert(save_result == 1 && snapshots == 2 && writes == 1 && !cache_saving_at_exit);
// Ordinary autosave does not wait behind an active build or close admission.
// The autosave does not wait for a running build and holds none back.
cache_build_enter();
assert(cache_save_one(0, 0, 0) == 0 && !cache_saving_at_exit);
// A final save times out on a genuinely long driver call and restores admission.
// The save at exit gives up on a build that outlasts its wait, and lets builds in again.
assert(cache_save_one(0, 1, 1) == 0 && !cache_saving_at_exit);
plat_mutex_unlock_shared(&cache_rw);
cache_build_enter(); plat_mutex_unlock_shared(&cache_rw);
// Data of the size already saved is not written again.
block_file = 0;
assert(cache_save_one(0, 100, 1) == 1 && writes == 1); // unchanged data is not rewritten
assert(cache_save_one(0, 100, 1) == 1 && writes == 1);
// A failed write or size query keeps the file and the size it was saved at.
P.saved[0] = 0; fail_file = 1;
assert(cache_save_one(0, 100, 1) == -1 && !cache_saving_at_exit && P.saved[0] == 0);
// NVIDIA exit reads can finish while a build remains active; normal autosaves retain the conservative path.
fail_file=0;context.props.vendorID=0x10de;P.saved[0]=0;
ResetEvent(old_in);ResetEvent(release_old);
old=CreateThread(NULL,0,builder,(void *)1,0,NULL);
assert(WaitForSingleObject(old_in,2000)==WAIT_OBJECT_0);
assert(active==1);
int before=writes;
assert(cache_save_one(0,100,1)==1 && writes==before+1 && active==1 && !cache_saving_at_exit);
assert(cache_save_one(0,0,0)==0 && active==1); // no gameplay autosave behavior change
SetEvent(release_old);join(old);
fail_file = 0; P.saved[0] = mock_size; query_fail = 1;
int before = writes;
assert(cache_save_one(0, 100, 1) == -1 && writes == before && P.saved[0] == mock_size);
query_fail = 0;
// Cache growth retries a fresh size query, and a partial result never replaces an existing file.
P.saved[0]=0;grow_once=1;
assert(cache_save_one(0,100,1)==1 && P.saved[0]==mock_size);
before=writes;
assert(cache_save_one(0,100,1)==1 && writes==before);
uint64_t saved=P.saved[0];mock_size++;grow_always=1;
assert(cache_save_one(0,100,1)==-1 && writes==before && P.saved[0]==saved);
grow_always=0;query_fail=1;
assert(cache_save_one(0,100,1)==-1 && writes==before && P.saved[0]==saved);
query_fail=0;
// A merge owns exclusive access: even the concurrent snapshot must not race it.
// A merge holds the cache exclusively: the save at exit waits for it like for a build.
assert(plat_mutex_trylock(&cache_rw));
assert(cache_save_one(0,0,1)==0 && writes==before);
assert(cache_save_one(0, 0, 1) == 0 && writes == before && !cache_saving_at_exit);
plat_mutex_unlock(&cache_rw);
puts("Exit snapshot: active builds, merge exclusion, cache growth/retry, partial/error preservation and fallback passed");
puts("cache save at exit: builds held back and waited for, autosave unchanged, timeout, failures and merges passed");
return 0;
}
'''