From 60ac5f5982a10e4645a83ff27f8f103e7063184c Mon Sep 17 00:00:00 2001 From: "automata." Date: Wed, 11 Nov 2026 17:30:34 -0500 Subject: [PATCH] Prioritize recent compute pipelines before reducing background concurrency --- runtime/vk_pipeline.c | 32 ++++++++--- tests/compute_prebuild_queue_check.py | 79 +++++++++++++++++++++++++++ 2 files changed, 104 insertions(+), 7 deletions(-) create mode 100644 tests/compute_prebuild_queue_check.py diff --git a/runtime/vk_pipeline.c b/runtime/vk_pipeline.c index fb4c1c46..3b8f9edd 100644 --- a/runtime/vk_pipeline.c +++ b/runtime/vk_pipeline.c @@ -4926,10 +4926,25 @@ static struct { volatile int32_t kept; // libraries entered in libs[] so far int32_t keep_n; int idle; // build at PLAT_PRIO_IDLE (boot), normal (preparation) + int tail_one; // automatic boot policy: only worker 0 takes the warming tail PlatMutex stat; } LB = {.stat = PLAT_MUTEX_INIT}; static __thread int lb_me = -1; // on a boot prebuild thread (lb_worker): its index in LB.th +// Claim a boot job without dropping one when the extra workers finish the recent-pipeline prefix. Recent pipelines +// retain parallel creation; a single worker continues the complete tail. Explicit thread counts override this +// policy, and installer preparation uses its own queue at full concurrency. The CAS makes the prefix boundary +// exact even when two workers reach it together. No pipeline creation or dispatch waits on this queue lock. +static size_t lb_next(int worker) { + int64_t next = __atomic_load_n(&LB.next, __ATOMIC_RELAXED); + for (;;) { + if (LB.stop || (size_t)next >= LB.n || + (LB.idle && LB.tail_one && worker > 0 && (size_t)next >= LB.n_keep)) return LB.n; + if (__atomic_compare_exchange_n(&LB.next, &next, next + 1, 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED)) + return (size_t)next; + } +} + // ---- preparation that outlives a driver crash. A bug in the driver's shader compiler can kill the process inside // vkCreate*Pipelines: NVIDIA 615.78.08 on an RTX 5080 crashed on the same vertex library at the same point of every // preparation, natively and under Wine, so preparation never finished (issue #30 and a report on PR #60). @@ -5846,8 +5861,8 @@ static void lb_worker(void *arg) { lb_me = (int)(intptr_t)arg; // its handle is LB.th[lb_me] plat_thread_priority(PLAT_PRIO_IDLE); for (;;) { - size_t i = (size_t)plat_inc64(&LB.next) - 1; - if (i >= LB.n || LB.stop) break; + size_t i = lb_next(lb_me); + if (i >= LB.n) break; if (skip_listed(LB.e[i].iid)) { skip_count(); plat_mutex_lock(&LB.stat); @@ -6146,12 +6161,12 @@ 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 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. + // 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. 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; if ((size_t)t > n) t = (int)n; LB.t0 = now_ms(); @@ -6171,6 +6186,9 @@ static void lb_start(const uint32_t pr_hist[16], const uint32_t fs_hist[4]) { "threads; library state clamp %u, polygon mode %u, depth -w..w %u, samples %u; %s (NX2NAVI_LIB_PREBUILD_BIG)", n_cs < n ? n_cs : n, LB.n_keep, n > n_cs ? n - n_cs : 0, LB.keep_n, started, LB.clamp, LB.poly, LB.minus_w, LB.samples, big); + if (LB.tail_one && started > 1) + rt_log("vk_pipeline: stage library prebuild: after the %zu recent compute pipelines, one worker continues the " + "remaining list; NX2NAVI_LIB_PREBUILD_THREADS overrides this background limit", LB.n_keep); } static void lb_stop(void) { diff --git a/tests/compute_prebuild_queue_check.py b/tests/compute_prebuild_queue_check.py new file mode 100644 index 00000000..688fb704 --- /dev/null +++ b/tests/compute_prebuild_queue_check.py @@ -0,0 +1,79 @@ +"""Stress the production recent-first prebuild queue: changing concurrency must not omit or repeat any job.""" +from pathlib import Path +import subprocess +import sys + +root = Path(__file__).resolve().parents[1] +source = (root / 'runtime/vk_pipeline.c').read_text() +start = source.index('static size_t lb_next(int worker)') +brace = source.index('{', start) +depth, end = 1, brace + 1 +while depth: + depth += (source[end] == '{') - (source[end] == '}') + end += 1 +harness = r''' +#include +#include +#include +#include +static struct { + volatile int64_t next; + volatile int32_t stop; + size_t n, n_keep; + int idle, tail_one; +} LB; +static volatile LONG seen[2000]; +static HANDLE start_event; +''' +harness += source[start:end] +harness += r''' +static DWORD WINAPI worker(void *arg) { + int id=(int)(intptr_t)arg; + assert(WaitForSingleObject(start_event,2000)==WAIT_OBJECT_0); + for (;;) { + size_t i=lb_next(id); + if (i>=LB.n) break; + assert(InterlockedIncrement(&seen[i])==1); + if (LB.idle && LB.tail_one && i>=LB.n_keep) assert(id==0); + if (i%7==0) SwitchToThread(); + } + return 0; +} +static void run(size_t prefix, int automatic, int idle) { + LB.next=0;LB.n=2000;LB.n_keep=prefix;LB.stop=0;LB.tail_one=automatic;LB.idle=idle; + for (int i=0;i<2000;i++) seen[i]=0; + ResetEvent(start_event); + HANDLE threads[8]; + for (int i=0;i<8;i++) { threads[i]=CreateThread(NULL,0,worker,(void *)(intptr_t)i,0,NULL);assert(threads[i]); } + SetEvent(start_event); + assert(WaitForMultipleObjects(8,threads,TRUE,5000)==WAIT_OBJECT_0); + for (int i=0;i<8;i++) CloseHandle(threads[i]); + assert(LB.next==2000); + for (int i=0;i<2000;i++) assert(seen[i]==1); +} +int main(void) { + start_event=CreateEventW(NULL,TRUE,FALSE,NULL);assert(start_event); + for (int repeat=0;repeat<20;repeat++) { + run(289,1,1);run(0,1,1);run(2000,1,1);run(1,1,1); + run(289,0,1);run(289,1,0); + } + LB.next=289;LB.n_keep=289;LB.tail_one=1;LB.idle=1; + assert(lb_next(1)==LB.n && LB.next==289); + assert(lb_next(0)==289); + LB.stop=1; + assert(lb_next(0)==LB.n && LB.next==290); + LB.stop=0;LB.tail_one=0; + assert(lb_next(1)==290); + CloseHandle(start_event); + puts("Prebuild queue: concurrent exactly-once coverage, recent/tail boundary, explicit override and stop checks passed"); + return 0; +} +''' +out = root / 'build/test/compute-prebuild-queue' +out.mkdir(parents=True, exist_ok=True) +c = out / 'check.c' +c.write_text(harness) +cc = sys.argv[1] if len(sys.argv) > 1 else 'C:/msys64/ucrt64/bin/clang.exe' +exe = out / 'check.exe' +subprocess.run([cc, '-std=gnu11', '-O2', '-Wall', '-Wextra', '-Werror', str(c), '-o', str(exe)], check=True) +subprocess.run([str(exe)], check=True, timeout=15)