From 62be3fa1898ffdb80e0ea0ea6a7e6a0be2af3b94 Mon Sep 17 00:00:00 2001 From: OreoFR <29048345+OreoFR@users.noreply.github.com> Date: Thu, 26 Nov 2026 22:45:08 +0100 Subject: [PATCH 1/8] compile gate: a submission waits at most 15 s for creates in the driver, then goes ahead (LEONIDA_COMPILE_GATE_WAIT_MS) The gate keeps vkQueueSubmit out of the driver while a pipeline create is in it (on NVIDIA 630.20 a submission that entered during a long compute create waited 10 s in the driver and lost the device). The waiter holds the submit lock, so every queue stops behind it: on an RTX 4070 Laptop (NVIDIA 621.40) one compute create stayed in the driver for over 54 s during the story load and the title's 30 s hang watchdog ended the process. A submission now goes ahead after LEONIDA_COMPILE_GATE_WAIT_MS (15 s by default; 0: no limit). The creates in the driver then hold no later submission back; creates that enter afterwards are waited for again. On 630.20 that only gives up creates over 25 s (15 s here plus the driver's 10 s), which the watchdog ends anyway. With the watchdog kept kicked (LEONIDA_TITLE_WATCHDOG=0 and LEONIDA_TITLE_WATCHDOG_KICK) the default stays no limit. The log names each submission that goes ahead and how long it then spent in the driver. The README row, which still described the gate as opt-in, now says it is on by default. Tested: submit_mutex (new gate_wait_limit case), MSVC, Windows 11. Co-Authored-By: Claude Opus 5.5 --- docs/d3d12_vulkan.md | 12 +++ runtime/README.md | 3 +- runtime/src/vulkan_backend.h | 5 +- runtime/src/vulkan_device_fault.h | 127 +++++++++++++++++++++++++---- runtime/test/submit_mutex_test.cpp | 84 ++++++++++++++++++- 5 files changed, 213 insertions(+), 18 deletions(-) diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 45d4b4be47..1d1334f578 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -2882,6 +2882,18 @@ submitting thread; at the maintainers' decision it was replaced by this one when title submits (a submission in the last second, `src/pipeline_wait.h`); the pipeline is built at its first use instead. Opt-in: unset or 0, every warm-up runs, as on `main`, and `LargeModuleGate` (#1889) orders them behind a submission's compiles. Smaller warm-ups, and every warm-up before the first submission, run as before. +- **A bounded wait at the compile gate** (`SubmitMutex::enter_submit`, `src/vulkan_device_fault.h`). The gate keeps a + `vkQueueSubmit` out of the driver while a create is in it, since on NVIDIA 630.20 one that entered during a long + `vkCreateComputePipelines` waited 10 s and lost the device. The waiting submission holds the submit lock, so all + queues stop: on an RTX 4070 Laptop (NVIDIA 621.40) one compute create of a 2.3 MB module stayed in the driver for + more than 54 s during the story load, the submission waited for all of it and the title's 30 s hang watchdog ended + the process; with `LEONIDA_COMPILE_GATE=0` the same build went past that point. A submission now waits at most + `LEONIDA_COMPILE_GATE_WAIT_MS` (15 s by default) and then goes ahead; the creates in the driver at that moment hold + no later submission back, and creates that enter after it are waited for again. On 630.20 that loses only creates + over 25 s (15 s here, then the driver's 10 s), which the watchdog ends anyway. With the watchdog kept kicked + (`LEONIDA_TITLE_WATCHDOG=0` and `LEONIDA_TITLE_WATCHDOG_KICK`) the default is no limit, as before. The log names + each submission that goes ahead and how long it then took in the driver, which tells whether that driver holds a + submission behind a create at all. Tested by `submit_mutex` (no GPU); not yet measured in the title. **Open:** whether the game uses `D3D12_PIPELINE_STATE_STREAM_DESC` for mesh/RT and which subobject types appear. The trace will enumerate them. diff --git a/runtime/README.md b/runtime/README.md index 9aabdf12c5..175c1b386e 100644 --- a/runtime/README.md +++ b/runtime/README.md @@ -419,7 +419,8 @@ ones marked *diagnostic* only add log lines or exist to compare a run with and w | `LEONIDA_TEXTURE_REFRESH_ASYNC` | unset | `1` moves the before-submission refresh of the title textures (`refresh_title_textures`) to a worker: a submission only wakes it, and fills by itself only the mapped textures that were never filled, so their first draw has their bytes. Persistent mappings are hashed whole on the worker every pass; the rest is swept 128 textures at a time. A texture changed since may be sampled a frame behind. | | `LEONIDA_SUBMIT_ASYNC` | `0` | ``: each queue's `ExecuteCommandLists`, `Signal`, `Wait` and markers run in call order on a worker of the queue's own (`src/vulkan_backend.h`), at most n batches queued; the caller of the next one waits for room. The replay, the before-submission refreshes and `vkQueueSubmit` then leave the title's thread. A batch that fails is reported as a held one is. `0`: on the caller's thread. | | `LEONIDA_COMPUTE_WARMUP_LIVE_MAX_BYTES` | unset | ``: a compute warm-up of a module over n bytes of SPIR-V is not started while the title submits (a submission in the last second); the pipeline is built at its first use instead (docs/d3d12_vulkan.md, "Pipelines still being built"). Unset or `0`: every warm-up runs, ordered behind a submission's compiles by the large-compile gate. | -| `LEONIDA_COMPILE_GATE` | unset | `1`: a compute pipeline build holds the device's submit lock for its driver calls, so no compute compile runs while a queue submits. Unset: the asynchronous workers compile while other queues submit. | +| `LEONIDA_COMPILE_GATE` | on | A queue submission (and a present or device wait) does not enter the driver while a pipeline create is in it, and waits for it in the runtime instead (`SubmitMutex::CompileTurn`, `src/vulkan_device_fault.h`): on NVIDIA 630.20 a `vkQueueSubmit` that entered during a long `vkCreateComputePipelines` waited 10 s in the driver and lost the device. Creates pass together; one that arrives while a submission waits queues behind it. The wait is bounded by `LEONIDA_COMPILE_GATE_WAIT_MS`. `0` turns the gate off. | +| `LEONIDA_COMPILE_GATE_WAIT_MS` | 15000 (none with the watchdog kept kicked) | ``: how long a submission waits at the compile gate before it goes ahead anyway; the creates in the driver then hold no later submission back (creates that enter after it are waited for again), and the log says so with how long that submission then took in the driver. The waiter holds the submit lock, so every queue stops behind it, and the title's hang watchdog ends the process after 30 s (one create stayed in the driver for over 54 s on NVIDIA 621.40). 15 s keeps the 630.20 protection for creates of up to 25 s. `0`: no limit. Unset with `LEONIDA_TITLE_WATCHDOG=0` and `LEONIDA_TITLE_WATCHDOG_KICK` set: no limit, since nothing ends a long stop then. | | `LEONIDA_FAST_PIPELINES` | unset | `graphics`, `compute` or `all` (`1` = all) creates those pipelines with `VK_PIPELINE_CREATE_DISABLE_OPTIMIZATION_BIT` (`shims/d3d12_pipeline_compile.h`): slower shaders, much shorter builds. Part of the compute journal key, so optimized and fast builds keep separate files. | | `LEONIDA_GRAPHICS_COMPILE_WORKER` | on | A second compile worker that takes graphics pipeline builds only, so a long compute build does not hold them up (`shims/d3d12_pipeline_compile.h`). `0`: the one general worker builds everything. | | `LEONIDA_CONSOLE_HEAP_GPU` | unset | `1`: the V#s of the console descriptor heaps are rebased by a compute pass at the start of each submission (`src/console_heap_gpu.h`) instead of on the CPU. The CPU still finds the changed slots and registers the T#s; a V# the pass cannot answer is resolved after its submission and its slot queued again. | diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h index bf970aa282..4494718cd2 100644 --- a/runtime/src/vulkan_backend.h +++ b/runtime/src/vulkan_backend.h @@ -2308,10 +2308,11 @@ class VulkanContext { if (gate.submits != gate_logged.submits || gate.creates != gate_logged.creates) { gate_logged = gate; std::fprintf(stderr, "[rt] compile-gate: t=%.0f s: %llu submissions waited for pipeline creates " - "(%lld ms in all, longest %lld ms); %llu creates waited behind a submission " - "(%lld ms in all, longest %lld ms)\n", + "(%lld ms in all, longest %lld ms, %llu went ahead at the limit); %llu creates " + "waited behind a submission (%lld ms in all, longest %lld ms)\n", double(diagnostic_ms()) / 1000.0, static_cast(gate.submits), static_cast(gate.submit_ms), static_cast(gate.submit_max_ms), + static_cast(gate.ahead), static_cast(gate.creates), static_cast(gate.create_ms), static_cast(gate.create_max_ms)); } diff --git a/runtime/src/vulkan_device_fault.h b/runtime/src/vulkan_device_fault.h index b3af188d13..272d8a4ec7 100644 --- a/runtime/src/vulkan_device_fault.h +++ b/runtime/src/vulkan_device_fault.h @@ -399,27 +399,44 @@ class SubmitMutex { // the driver. A long compile then stalls the submitting thread here, in our code, instead of in the driver. // No deadlock: a create holds the gate only across its vkCreate*Pipelines and a submission only across its // vkQueueSubmit, and neither driver call waits for anything of ours. LEONIDA_COMPILE_GATE=0 turns it off. + // The wait is bounded (gate_wait_limit_ms, LEONIDA_COMPILE_GATE_WAIT_MS): a submission waiting at the gate holds + // the submit lock, so every queue stops behind it, and the title's hang watchdog ends the process once its main + // loop has not reported for 30 s. One create stayed in the driver for more than 54 s (RTX 4070 Laptop, NVIDIA + // 621.40), and the gate turned it into that exit. Past the limit the submission goes ahead, and the creates that + // were in the driver then no longer hold any submission back; creates that enter later are waited for again. The + // default limit, 15 s, keeps the protection measured above for every create of up to 25 s: 630.20 gave the + // submission 10 s in the driver before the loss, and a stop much longer than 25 s ends at the watchdog anyway. + // With the watchdog kept kicked (LEONIDA_TITLE_WATCHDOG=0 with LEONIDA_TITLE_WATCHDOG_KICK) nothing ends a long + // stop, so the wait is not bounded unless the variable asks for it. + struct GateAhead { // a submission that went ahead at the limit + std::int64_t since_ms = -1; // when it went ahead, or -1: it did not + std::uint32_t creates = 0; // the creates still in the driver then + }; class CompileTurn { // around vkCreate*Pipelines public: - explicit CompileTurn(SubmitMutex& gate) : gate_(gate), waited_(gate.enter_compile()) {} - ~CompileTurn() { gate_.leave_compile(); } + explicit CompileTurn(SubmitMutex& gate) : gate_(gate), waited_(gate.enter_compile(seq_)) {} + ~CompileTurn() { gate_.leave_compile(seq_); } CompileTurn(const CompileTurn&) = delete; CompileTurn& operator=(const CompileTurn&) = delete; std::int64_t waited_ms() const { return waited_; } private: SubmitMutex& gate_; + std::uint64_t seq_ = 0; // declared before waited_: enter_compile sets it std::int64_t waited_; }; class SubmitTurn { // around vkQueueSubmit, with the submit lock held public: - explicit SubmitTurn(SubmitMutex& gate) : gate_(gate) { gate.enter_submit(); } - ~SubmitTurn() { gate_.leave_submit(); } + explicit SubmitTurn(SubmitMutex& gate) : gate_(gate) { gate.enter_submit(&ahead_); } + ~SubmitTurn() { gate_.leave_submit(ahead_); } SubmitTurn(const SubmitTurn&) = delete; SubmitTurn& operator=(const SubmitTurn&) = delete; + // It went ahead at the wait limit with creates still in the driver. + bool went_ahead() const { return ahead_.since_ms >= 0; } private: SubmitMutex& gate_; + GateAhead ahead_; }; static bool compile_gate_enabled() { @@ -430,12 +447,53 @@ class SubmitMutex { return on; } + // How long a submission waits at the gate before it goes ahead (0: until the creates leave), read once: + // LEONIDA_COMPILE_GATE_WAIT_MS=; unset, kDefaultGateWaitMs with the title's hang watchdog armed and no limit + // with it kept kicked (both variables set, as src/main.cpp asks for). + static constexpr std::int64_t kDefaultGateWaitMs = 15000; + static std::int64_t default_gate_wait_limit_ms() { + static const std::int64_t ms = []() -> std::int64_t { + std::int64_t limit = kDefaultGateWaitMs; + const char* watchdog = std::getenv("LEONIDA_TITLE_WATCHDOG"); + const char* kick = std::getenv("LEONIDA_TITLE_WATCHDOG_KICK"); + if (watchdog && !std::strcmp(watchdog, "0") && kick && *kick) limit = 0; + if (const char* value = std::getenv("LEONIDA_COMPILE_GATE_WAIT_MS"); value && *value) { + char* end = nullptr; + const long long n = std::strtoll(value, &end, 10); + if (end && !*end && n >= 0) limit = n; + else + std::fprintf(stderr, "[rt] compile-gate: LEONIDA_COMPILE_GATE_WAIT_MS=%s is not a number of " + "milliseconds; ignored\n", value); + } + if (compile_gate_enabled()) { + if (limit) + std::fprintf(stderr, "[rt] compile-gate: a submission waits at most %lld ms for pipeline creates " + "to leave the driver, then goes ahead (LEONIDA_COMPILE_GATE_WAIT_MS)\n", + static_cast(limit)); + else + std::fprintf(stderr, "[rt] compile-gate: a submission waits for pipeline creates to leave the " + "driver, however long they take (LEONIDA_COMPILE_GATE_WAIT_MS)\n"); + } + return limit; + }(); + return ms; + } + std::int64_t gate_wait_limit_ms() { + std::lock_guard lock(gate_mutex_); + return wait_limit_ms_; + } + void set_gate_wait_limit_ms(std::int64_t ms) { // for tests + std::lock_guard lock(gate_mutex_); + wait_limit_ms_ = (std::max)(ms, std::int64_t{0}); + } + // The thread waiting in enter_submit for creates to leave the driver, or 0. std::uint32_t gate_waiter() const { return gate_waiter_.load(std::memory_order_relaxed); } struct GateStats { std::uint64_t submits = 0; // submissions that had to wait for a create std::int64_t submit_ms = 0, submit_max_ms = 0; + std::uint64_t ahead = 0; // of those, the ones that went ahead at the wait limit std::uint64_t creates = 0; // creates that waited behind a submission std::int64_t create_ms = 0, create_max_ms = 0; }; @@ -444,7 +502,8 @@ class SubmitMutex { return stats_; } - std::int64_t enter_compile() { + // `seq` receives the create's number, which leave_compile takes back. + std::int64_t enter_compile(std::uint64_t& seq) { if (!compile_gate_enabled()) return 0; std::unique_lock lock(gate_mutex_); std::int64_t waited = 0; @@ -457,28 +516,46 @@ class SubmitMutex { stats_.create_max_ms = (std::max)(stats_.create_max_ms, waited); } ++compiles_; + ++fresh_; + seq = ++last_create_; return waited; } - void leave_compile() { + void leave_compile(std::uint64_t seq) { if (!compile_gate_enabled()) return; std::lock_guard lock(gate_mutex_); - if (--compiles_ == 0 && submits_) gate_changed_.notify_all(); + --compiles_; + // A create a submission went past at the limit is no longer counted in fresh_. + if (seq > overdue_through_ && --fresh_ == 0 && submits_) gate_changed_.notify_all(); } - std::int64_t enter_submit() { + // `ahead` (optional) is told whether the submission went ahead at the wait limit, for leave_submit. + std::int64_t enter_submit(GateAhead* ahead = nullptr) { if (!compile_gate_enabled()) return 0; std::unique_lock lock(gate_mutex_); ++submits_; - if (!compiles_) return 0; + if (!fresh_) return 0; const std::uint32_t self = diagnostic_thread_id(); gate_waiter_.store(self, std::memory_order_relaxed); const std::int64_t start = diagnostic_ms(); - const GateWait what{self, compiles_, driver_compile.name.load(std::memory_order_acquire), + const std::int64_t limit = wait_limit_ms_; + const GateWait what{self, fresh_, driver_compile.name.load(std::memory_order_acquire), driver_compile.trigger.load(std::memory_order_relaxed), driver_compile.bytes.load(std::memory_order_relaxed)}; std::int64_t next_note = 2000; - while (!gate_changed_.wait_for(lock, std::chrono::milliseconds(250), [&] { return compiles_ == 0; })) { + bool went_ahead = false; + for (;;) { + std::int64_t step = 250; + if (limit > 0) step = std::clamp(limit - (diagnostic_ms() - start), 1, 250); + if (gate_changed_.wait_for(lock, std::chrono::milliseconds(step), [&] { return fresh_ == 0; })) break; const std::int64_t waited = diagnostic_ms() - start; + if (limit > 0 && waited >= limit) { + // Every create in the driver now stops holding submissions back; later ones are waited for. + overdue_through_ = last_create_; + fresh_ = 0; + went_ahead = true; + gate_changed_.notify_all(); + break; + } if (waited < next_note) continue; next_note = waited + 5000; note_gate_wait("has waited", what, waited); @@ -488,16 +565,33 @@ class SubmitMutex { ++stats_.submits; stats_.submit_ms += waited; stats_.submit_max_ms = (std::max)(stats_.submit_max_ms, waited); - // Every wait of 500 ms or more, and the first 32 of 50 ms or more. - if (waited >= 500 || (waited >= 50 && logged_waits_ < 32)) { + if (went_ahead) { + ++stats_.ahead; + if (ahead) *ahead = GateAhead{diagnostic_ms(), compiles_}; + note_gate_wait("goes ahead after", what, waited); + std::fprintf(stderr, "[rt] compile-gate: the %u create(s) still in the driver hold no submission back from " + "now on (%llu submissions went ahead at the %lld ms limit so far, " + "LEONIDA_COMPILE_GATE_WAIT_MS)\n", + compiles_, static_cast(stats_.ahead), static_cast(limit)); + } else if (waited >= 500 || (waited >= 50 && logged_waits_ < 32)) { + // Every wait of 500 ms or more, and the first 32 of 50 ms or more. ++logged_waits_; note_gate_wait("waited", what, waited); } return waited; } - void leave_submit() { + void leave_submit(const GateAhead& ahead = {}) { if (!compile_gate_enabled()) return; std::lock_guard lock(gate_mutex_); + // What going ahead cost: a quick return with the creates it went past still in the driver means this driver + // did not hold the submission behind them. The first 8. + if (ahead.since_ms >= 0 && logged_ahead_ < 8) { + ++logged_ahead_; + std::fprintf(stderr, "[rt] compile-gate: the submission that went ahead on thread %u was in the driver " + "for %lld ms; %u of the %u create(s) it went past are still there\n", + diagnostic_thread_id(), static_cast(diagnostic_ms() - ahead.since_ms), + compiles_ - fresh_, ahead.creates); + } if (--submits_ == 0) gate_changed_.notify_all(); } @@ -549,10 +643,15 @@ class SubmitMutex { std::mutex gate_mutex_; // leaf: guards the gate counts and stats std::condition_variable gate_changed_; std::uint32_t compiles_ = 0; // creates in the driver + std::uint32_t fresh_ = 0; // of those, the ones numbered after overdue_through_: what a submission waits for + std::uint64_t last_create_ = 0; // the number of the last create that entered + std::uint64_t overdue_through_ = 0; // creates up to this number were in the driver when a submission went ahead std::uint32_t submits_ = 0; // submissions waiting for them or in the driver + std::int64_t wait_limit_ms_ = default_gate_wait_limit_ms(); // 0: no limit std::atomic gate_waiter_{0}; GateStats stats_; unsigned logged_waits_ = 0; + unsigned logged_ahead_ = 0; }; // One vkQueueSubmit of a VulkanQueue: its waits, its signals and how long the call took (-1: not returned yet). diff --git a/runtime/test/submit_mutex_test.cpp b/runtime/test/submit_mutex_test.cpp index 345f308871..c3b9b2d173 100644 --- a/runtime/test/submit_mutex_test.cpp +++ b/runtime/test/submit_mutex_test.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -136,6 +137,7 @@ int compile_gate() { return 0; } SubmitMutex m; + m.set_gate_wait_limit_ms(0); // no limit: gate_wait_limit checks the limit // Nothing in the driver: a submission passes at once. { const std::int64_t start = now_ms(); @@ -197,12 +199,92 @@ int compile_gate() { return 0; } +int gate_wait_limit() { + if (!SubmitMutex::compile_gate_enabled()) { + std::printf("compile gate off (LEONIDA_COMPILE_GATE=0): wait limit not checked\n"); + return 0; + } + // The default with neither LEONIDA_COMPILE_GATE_WAIT_MS nor the watchdog kick set. + if (!std::getenv("LEONIDA_COMPILE_GATE_WAIT_MS") && !std::getenv("LEONIDA_TITLE_WATCHDOG_KICK")) + EXPECT(SubmitMutex::default_gate_wait_limit_ms() == SubmitMutex::kDefaultGateWaitMs); + SubmitMutex m; + m.set_gate_wait_limit_ms(150); + EXPECT(m.gate_wait_limit_ms() == 150); + // A create that stays in the driver until it is let go (one that takes longer than the limit). + std::atomic long_in{false}, release_long{false}; + std::atomic long_left_at{0}; + std::thread long_create([&] { + const SubmitMutex::CompileTurn turn(m); + long_in = true; + while (!release_long) std::this_thread::sleep_for(1ms); + long_left_at = now_ms(); + }); + EXPECT(eventually([&] { return long_in.load(); })); + // The submission goes ahead at the limit, the create still in the driver. + std::int64_t first_waited = 0; + bool first_ahead = false; + { + const std::int64_t start = now_ms(); + const SubmitMutex::SubmitTurn turn(m); + first_waited = now_ms() - start; + first_ahead = turn.went_ahead(); + } + // The next one passes at once: the create the first went past holds no submission back. + std::int64_t second_waited = 0; + bool second_ahead = true; + { + const std::int64_t start = now_ms(); + const SubmitMutex::SubmitTurn turn(m); + second_waited = now_ms() - start; + second_ahead = turn.went_ahead(); + } + // A create that enters after that is waited for again, until it leaves (well within the limit now), while the + // long one is still in the driver. + m.set_gate_wait_limit_ms(5000); + std::atomic short_in{false}; + std::atomic short_left_at{0}; + std::thread short_create([&] { + const SubmitMutex::CompileTurn turn(m); + short_in = true; + std::this_thread::sleep_for(200ms); + short_left_at = now_ms(); + }); + const bool short_entered = eventually([&] { return short_in.load(); }); + std::int64_t third_entered_at = 0; + bool third_ahead = true; + { + const SubmitMutex::SubmitTurn turn(m); + third_entered_at = now_ms(); + third_ahead = turn.went_ahead(); + } + const bool long_still_in = long_left_at.load() == 0; + release_long = true; + long_create.join(); + short_create.join(); + EXPECT(short_entered); + EXPECT(first_ahead && first_waited >= 140 && first_waited < 1000); + EXPECT(!second_ahead && second_waited < 50); + EXPECT(!third_ahead && third_entered_at >= short_left_at.load() && long_still_in); + const SubmitMutex::GateStats stats = m.gate_stats(); + EXPECT(stats.submits == 2 && stats.ahead == 1); + // Both gone: a submission passes at once. + { + const std::int64_t start = now_ms(); + const SubmitMutex::SubmitTurn turn(m); + EXPECT(now_ms() - start < 50 && !turn.went_ahead()); + } + std::printf("compile gate: a submission went ahead after %lld ms (limit 150 ms) with the create still in the " + "driver, the next passed in %lld ms, and a create that entered later was waited for again\n", + static_cast(first_waited), static_cast(second_waited)); + return 0; +} + } // namespace int main() { std::setvbuf(stdout, nullptr, _IONBF, 0); if (owner_tracking() || waits_for_holder() || waits_for_queued_submitter() || handoff_is_capped() || - compile_gate()) + compile_gate() || gate_wait_limit()) return 1; std::printf("submit_mutex_test: all passed\n"); return 0; From b882b245fbc905dbf8a0154e4934c4d1bf3251f9 Mon Sep 17 00:00:00 2001 From: OreoFR <29048345+OreoFR@users.noreply.github.com> Date: Thu, 26 Nov 2026 23:13:26 +0100 Subject: [PATCH 2/8] compile gate: adaptive by default, off once submissions come back quickly with a create in the driver With the bounded wait, the three submissions that went ahead on NVIDIA 621.40 came back from the driver in 11, 1 and 0 ms with the create still there, but consecutive long creates made submissions wait the full 15 s again and the title's watchdog still ended the run. Every submission that enters the driver next to a create the gate went past at the limit is now a probe. Two that come back in under 250 ms, at least 1 s apart, with that create still in the driver turn the gate off for the rest of the run; one that stays 2 s or more keeps it on. Both verdicts are logged once. Two probes a second apart, because on 630.20 back-to-back creates take and release the driver's lock and one submission could slip in between; the second costs no extra wait. LEONIDA_COMPILE_GATE=1 gates always without probing (and compute builds keep holding the submit lock), =0 has no gate. The README row now says what =1 does again. Tested: submit_mutex (new gate_adapts case; also under LEONIDA_COMPILE_GATE=1 and =0), MSVC, Windows 11. Co-Authored-By: Claude Opus 5.5 --- docs/d3d12_vulkan.md | 18 +++- runtime/README.md | 4 +- runtime/src/vulkan_device_fault.h | 131 +++++++++++++++++++++++------ runtime/test/submit_mutex_test.cpp | 106 ++++++++++++++++++++++- 4 files changed, 225 insertions(+), 34 deletions(-) diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 1d1334f578..7bcfec7861 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -2882,7 +2882,7 @@ submitting thread; at the maintainers' decision it was replaced by this one when title submits (a submission in the last second, `src/pipeline_wait.h`); the pipeline is built at its first use instead. Opt-in: unset or 0, every warm-up runs, as on `main`, and `LargeModuleGate` (#1889) orders them behind a submission's compiles. Smaller warm-ups, and every warm-up before the first submission, run as before. -- **A bounded wait at the compile gate** (`SubmitMutex::enter_submit`, `src/vulkan_device_fault.h`). The gate keeps a +- **A bounded, adaptive compile gate** (`SubmitMutex::enter_submit`, `src/vulkan_device_fault.h`). The gate keeps a `vkQueueSubmit` out of the driver while a create is in it, since on NVIDIA 630.20 one that entered during a long `vkCreateComputePipelines` waited 10 s and lost the device. The waiting submission holds the submit lock, so all queues stop: on an RTX 4070 Laptop (NVIDIA 621.40) one compute create of a 2.3 MB module stayed in the driver for @@ -2891,9 +2891,19 @@ submitting thread; at the maintainers' decision it was replaced by this one when `LEONIDA_COMPILE_GATE_WAIT_MS` (15 s by default) and then goes ahead; the creates in the driver at that moment hold no later submission back, and creates that enter after it are waited for again. On 630.20 that loses only creates over 25 s (15 s here, then the driver's 10 s), which the watchdog ends anyway. With the watchdog kept kicked - (`LEONIDA_TITLE_WATCHDOG=0` and `LEONIDA_TITLE_WATCHDOG_KICK`) the default is no limit, as before. The log names - each submission that goes ahead and how long it then took in the driver, which tells whether that driver holds a - submission behind a create at all. Tested by `submit_mutex` (no GPU); not yet measured in the title. + (`LEONIDA_TITLE_WATCHDOG=0` and `LEONIDA_TITLE_WATCHDOG_KICK`) the default is no limit. + On the same laptop the three submissions that went ahead came back from the driver in 11, 1 and 0 ms with the + create still there, and no device was lost; but consecutive long creates made submissions wait the full 15 s again + and again, and the title's watchdog still ended the run. So by default the gate adapts: every submission that + enters the driver next to a create it no longer waits for is a probe. Two that come back in under 250 ms, at least + 1 s apart, with that create still in the driver turn the gate off for the rest of the run (a driver that holds a + submission behind a create holds it as long as the create stays, and 630.20 held one for the full 10 s). Two, and a + second apart, because back-to-back creates take and release that driver's lock, so one submission can slip in + between; the second costs no extra wait, since after one has gone ahead the next enter at once while the create is + still there. One probe that stays 2 s or more keeps the gate on for the rest of the run; the turn also covers + presents and idle waits, so it can be a false alarm, but it only keeps the bounded gate. Both verdicts are logged + once. `LEONIDA_COMPILE_GATE=1` gates always and never probes. Tested by `submit_mutex` (no GPU); the adaptive switch + is not yet measured in the title. **Open:** whether the game uses `D3D12_PIPELINE_STATE_STREAM_DESC` for mesh/RT and which subobject types appear. The trace will enumerate them. diff --git a/runtime/README.md b/runtime/README.md index 175c1b386e..8cec193810 100644 --- a/runtime/README.md +++ b/runtime/README.md @@ -419,8 +419,8 @@ ones marked *diagnostic* only add log lines or exist to compare a run with and w | `LEONIDA_TEXTURE_REFRESH_ASYNC` | unset | `1` moves the before-submission refresh of the title textures (`refresh_title_textures`) to a worker: a submission only wakes it, and fills by itself only the mapped textures that were never filled, so their first draw has their bytes. Persistent mappings are hashed whole on the worker every pass; the rest is swept 128 textures at a time. A texture changed since may be sampled a frame behind. | | `LEONIDA_SUBMIT_ASYNC` | `0` | ``: each queue's `ExecuteCommandLists`, `Signal`, `Wait` and markers run in call order on a worker of the queue's own (`src/vulkan_backend.h`), at most n batches queued; the caller of the next one waits for room. The replay, the before-submission refreshes and `vkQueueSubmit` then leave the title's thread. A batch that fails is reported as a held one is. `0`: on the caller's thread. | | `LEONIDA_COMPUTE_WARMUP_LIVE_MAX_BYTES` | unset | ``: a compute warm-up of a module over n bytes of SPIR-V is not started while the title submits (a submission in the last second); the pipeline is built at its first use instead (docs/d3d12_vulkan.md, "Pipelines still being built"). Unset or `0`: every warm-up runs, ordered behind a submission's compiles by the large-compile gate. | -| `LEONIDA_COMPILE_GATE` | on | A queue submission (and a present or device wait) does not enter the driver while a pipeline create is in it, and waits for it in the runtime instead (`SubmitMutex::CompileTurn`, `src/vulkan_device_fault.h`): on NVIDIA 630.20 a `vkQueueSubmit` that entered during a long `vkCreateComputePipelines` waited 10 s in the driver and lost the device. Creates pass together; one that arrives while a submission waits queues behind it. The wait is bounded by `LEONIDA_COMPILE_GATE_WAIT_MS`. `0` turns the gate off. | -| `LEONIDA_COMPILE_GATE_WAIT_MS` | 15000 (none with the watchdog kept kicked) | ``: how long a submission waits at the compile gate before it goes ahead anyway; the creates in the driver then hold no later submission back (creates that enter after it are waited for again), and the log says so with how long that submission then took in the driver. The waiter holds the submit lock, so every queue stops behind it, and the title's hang watchdog ends the process after 30 s (one create stayed in the driver for over 54 s on NVIDIA 621.40). 15 s keeps the 630.20 protection for creates of up to 25 s. `0`: no limit. Unset with `LEONIDA_TITLE_WATCHDOG=0` and `LEONIDA_TITLE_WATCHDOG_KICK` set: no limit, since nothing ends a long stop then. | +| `LEONIDA_COMPILE_GATE` | adaptive | The compile gate (`SubmitMutex::CompileTurn`, `src/vulkan_device_fault.h`): a queue submission (or a present or device wait) does not enter the driver while a pipeline create is in it, and waits in the runtime instead, since on NVIDIA 630.20 a `vkQueueSubmit` that entered during a long `vkCreateComputePipelines` waited 10 s in the driver and lost the device. Creates pass together; one that arrives while a submission waits queues behind it. The wait is bounded by `LEONIDA_COMPILE_GATE_WAIT_MS`. Unset: adaptive. Each submission that enters the driver next to a create the gate went past at that limit is a probe; two that come back in under 250 ms, at least 1 s apart, with the create still in the driver turn the gate off for the rest of the run (NVIDIA 621.40: 0-11 ms), and one that stays 2 s or more keeps it on; both are logged once. `1`: always gate, never turn it off, and a compute pipeline build also holds the submit lock for its driver calls (`compute_driver_turn`, `shims/d3d12_vulkan_device.cpp`). `0`: no gate. | +| `LEONIDA_COMPILE_GATE_WAIT_MS` | 15000 (none with the watchdog kept kicked) | ``: how long a submission waits at the compile gate before it goes ahead anyway; the creates in the driver then hold no later submission back (creates that enter after it are waited for again), and the log says how long the submission then took in the driver. The waiter holds the submit lock, so every queue stops behind it, and the title's hang watchdog ends the process after 30 s (one create stayed in the driver for over 54 s on NVIDIA 621.40). 15 s keeps the 630.20 protection for creates of up to 25 s. `0`: no limit, and so no probes for the adaptive gate. Unset with `LEONIDA_TITLE_WATCHDOG=0` and `LEONIDA_TITLE_WATCHDOG_KICK` set: no limit, since nothing ends a long stop then. | | `LEONIDA_FAST_PIPELINES` | unset | `graphics`, `compute` or `all` (`1` = all) creates those pipelines with `VK_PIPELINE_CREATE_DISABLE_OPTIMIZATION_BIT` (`shims/d3d12_pipeline_compile.h`): slower shaders, much shorter builds. Part of the compute journal key, so optimized and fast builds keep separate files. | | `LEONIDA_GRAPHICS_COMPILE_WORKER` | on | A second compile worker that takes graphics pipeline builds only, so a long compute build does not hold them up (`shims/d3d12_pipeline_compile.h`). `0`: the one general worker builds everything. | | `LEONIDA_CONSOLE_HEAP_GPU` | unset | `1`: the V#s of the console descriptor heaps are rebased by a compute pass at the start of each submission (`src/console_heap_gpu.h`) instead of on the CPU. The CPU still finds the changed slots and registers the T#s; a V# the pass cannot answer is resolved after its submission and its slot queued again. | diff --git a/runtime/src/vulkan_device_fault.h b/runtime/src/vulkan_device_fault.h index 272d8a4ec7..cb9a439b4d 100644 --- a/runtime/src/vulkan_device_fault.h +++ b/runtime/src/vulkan_device_fault.h @@ -398,7 +398,8 @@ class SubmitMutex { // that arrives while a submission waits queues behind it, so a submission waits only for the creates already in // the driver. A long compile then stalls the submitting thread here, in our code, instead of in the driver. // No deadlock: a create holds the gate only across its vkCreate*Pipelines and a submission only across its - // vkQueueSubmit, and neither driver call waits for anything of ours. LEONIDA_COMPILE_GATE=0 turns it off. + // vkQueueSubmit, and neither driver call waits for anything of ours. + // // The wait is bounded (gate_wait_limit_ms, LEONIDA_COMPILE_GATE_WAIT_MS): a submission waiting at the gate holds // the submit lock, so every queue stops behind it, and the title's hang watchdog ends the process once its main // loop has not reported for 30 s. One create stayed in the driver for more than 54 s (RTX 4070 Laptop, NVIDIA @@ -408,9 +409,29 @@ class SubmitMutex { // submission 10 s in the driver before the loss, and a stop much longer than 25 s ends at the watchdog anyway. // With the watchdog kept kicked (LEONIDA_TITLE_WATCHDOG=0 with LEONIDA_TITLE_WATCHDOG_KICK) nothing ends a long // stop, so the wait is not bounded unless the variable asks for it. - struct GateAhead { // a submission that went ahead at the limit - std::int64_t since_ms = -1; // when it went ahead, or -1: it did not - std::uint32_t creates = 0; // the creates still in the driver then + // + // Adaptive (the default): each submission that enters the driver alongside a create the gate no longer waits for + // is a probe of whether this driver holds a submission behind a create. Back while that create is still in the + // driver: under kProbeQuickMs is quick, kProbeHeldMs or more is held, between says nothing. On 621.40 the three + // that went ahead came back in 0-11 ms, the create still there; the one 630.20 held stayed 10 s, until the loss. + // Two quick probes at least kProbeSpanMs apart turn the gate off for the rest of the run (the device's, which is + // the process's), logged once. Two because on 630.20 back-to-back creates take and release the driver's lock (see + // let_submit_finish), so one submission could slip in between and come back quickly from a driver that does hold + // submissions; a second one a second later must be as lucky. It costs no extra wait: once one has gone ahead, + // the next ones enter at once while that create is still in the driver. One held probe keeps the gate on for the + // rest of the run (the turn also covers presents and idle waits, which can take that long on their own, so a held + // probe may be a false alarm; it only keeps the bounded gate, never less protection). LEONIDA_COMPILE_GATE=1 gates + // without probing, LEONIDA_COMPILE_GATE=0 turns the gate off. let_submit_finish is unaffected by all of this. + enum class GateMode { off, adaptive, always }; + enum class GateVerdict { pending, driver_does_not_hold, driver_holds }; + static constexpr std::int64_t kProbeQuickMs = 250; + static constexpr std::int64_t kProbeHeldMs = 2000; + static constexpr std::int64_t kProbeSpanMs = 1000; + static constexpr unsigned kQuickProbes = 2; + struct GateAhead { // a submission that entered the driver alongside a create not waited for + std::int64_t since_ms = -1; // when it entered, or -1: no such create was in the driver + std::uint32_t creates = 0; // such creates in the driver then + bool went_ahead = false; // it waited at the gate up to the limit }; class CompileTurn { // around vkCreate*Pipelines public: @@ -432,20 +453,25 @@ class SubmitMutex { SubmitTurn(const SubmitTurn&) = delete; SubmitTurn& operator=(const SubmitTurn&) = delete; // It went ahead at the wait limit with creates still in the driver. - bool went_ahead() const { return ahead_.since_ms >= 0; } + bool went_ahead() const { return ahead_.went_ahead; } private: SubmitMutex& gate_; GateAhead ahead_; }; - static bool compile_gate_enabled() { - static const bool on = [] { + // LEONIDA_COMPILE_GATE: 0 off, 1 always (and compute builds hold the submit lock, compute_driver_turn in + // shims/d3d12_vulkan_device.cpp), anything else adaptive. + static GateMode compile_gate_mode() { + static const GateMode mode = [] { const char* value = std::getenv("LEONIDA_COMPILE_GATE"); - return !(value && !std::strcmp(value, "0")); + if (value && !std::strcmp(value, "0")) return GateMode::off; + if (value && !std::strcmp(value, "1")) return GateMode::always; + return GateMode::adaptive; }(); - return on; + return mode; } + static bool compile_gate_enabled() { return compile_gate_mode() != GateMode::off; } // How long a submission waits at the gate before it goes ahead (0: until the creates leave), read once: // LEONIDA_COMPILE_GATE_WAIT_MS=; unset, kDefaultGateWaitMs with the title's hang watchdog armed and no limit @@ -466,13 +492,16 @@ class SubmitMutex { "milliseconds; ignored\n", value); } if (compile_gate_enabled()) { + const char* how = compile_gate_mode() == GateMode::adaptive + ? "adaptive: off once submissions come back quickly with a create in the driver" + : "always (LEONIDA_COMPILE_GATE=1)"; if (limit) - std::fprintf(stderr, "[rt] compile-gate: a submission waits at most %lld ms for pipeline creates " - "to leave the driver, then goes ahead (LEONIDA_COMPILE_GATE_WAIT_MS)\n", - static_cast(limit)); + std::fprintf(stderr, "[rt] compile-gate: %s; a submission waits at most %lld ms for pipeline " + "creates to leave the driver, then goes ahead (LEONIDA_COMPILE_GATE_WAIT_MS)\n", + how, static_cast(limit)); else - std::fprintf(stderr, "[rt] compile-gate: a submission waits for pipeline creates to leave the " - "driver, however long they take (LEONIDA_COMPILE_GATE_WAIT_MS)\n"); + std::fprintf(stderr, "[rt] compile-gate: %s; a submission waits for pipeline creates to leave the " + "driver, however long they take (LEONIDA_COMPILE_GATE_WAIT_MS)\n", how); } return limit; }(); @@ -486,6 +515,14 @@ class SubmitMutex { std::lock_guard lock(gate_mutex_); wait_limit_ms_ = (std::max)(ms, std::int64_t{0}); } + void set_gate_adaptive(bool on) { // for tests + std::lock_guard lock(gate_mutex_); + adaptive_ = on; + } + GateVerdict gate_verdict() { + std::lock_guard lock(gate_mutex_); + return verdict_; + } // The thread waiting in enter_submit for creates to leave the driver, or 0. std::uint32_t gate_waiter() const { return gate_waiter_.load(std::memory_order_relaxed); } @@ -507,9 +544,9 @@ class SubmitMutex { if (!compile_gate_enabled()) return 0; std::unique_lock lock(gate_mutex_); std::int64_t waited = 0; - if (submits_) { + if (submits_ && !gate_off()) { const std::int64_t start = diagnostic_ms(); - gate_changed_.wait(lock, [&] { return submits_ == 0; }); + gate_changed_.wait(lock, [&] { return submits_ == 0 || gate_off(); }); waited = diagnostic_ms() - start; ++stats_.creates; stats_.create_ms += waited; @@ -528,12 +565,16 @@ class SubmitMutex { if (seq > overdue_through_ && --fresh_ == 0 && submits_) gate_changed_.notify_all(); } - // `ahead` (optional) is told whether the submission went ahead at the wait limit, for leave_submit. + // `ahead` (optional) is told whether the submission entered the driver alongside a create the gate no longer + // waits for, and whether it went ahead at the wait limit, for leave_submit. std::int64_t enter_submit(GateAhead* ahead = nullptr) { if (!compile_gate_enabled()) return 0; std::unique_lock lock(gate_mutex_); ++submits_; - if (!fresh_) return 0; + if (!fresh_ || gate_off()) { + if (ahead && compiles_ > fresh_) *ahead = GateAhead{diagnostic_ms(), compiles_ - fresh_, false}; + return 0; + } const std::uint32_t self = diagnostic_thread_id(); gate_waiter_.store(self, std::memory_order_relaxed); const std::int64_t start = diagnostic_ms(); @@ -546,7 +587,9 @@ class SubmitMutex { for (;;) { std::int64_t step = 250; if (limit > 0) step = std::clamp(limit - (diagnostic_ms() - start), 1, 250); - if (gate_changed_.wait_for(lock, std::chrono::milliseconds(step), [&] { return fresh_ == 0; })) break; + if (gate_changed_.wait_for(lock, std::chrono::milliseconds(step), + [&] { return fresh_ == 0 || gate_off(); })) + break; const std::int64_t waited = diagnostic_ms() - start; if (limit > 0 && waited >= limit) { // Every create in the driver now stops holding submissions back; later ones are waited for. @@ -567,7 +610,6 @@ class SubmitMutex { stats_.submit_max_ms = (std::max)(stats_.submit_max_ms, waited); if (went_ahead) { ++stats_.ahead; - if (ahead) *ahead = GateAhead{diagnostic_ms(), compiles_}; note_gate_wait("goes ahead after", what, waited); std::fprintf(stderr, "[rt] compile-gate: the %u create(s) still in the driver hold no submission back from " "now on (%llu submissions went ahead at the %lld ms limit so far, " @@ -578,24 +620,55 @@ class SubmitMutex { ++logged_waits_; note_gate_wait("waited", what, waited); } + if (ahead && compiles_ > fresh_) *ahead = GateAhead{diagnostic_ms(), compiles_ - fresh_, went_ahead}; return waited; } void leave_submit(const GateAhead& ahead = {}) { if (!compile_gate_enabled()) return; std::lock_guard lock(gate_mutex_); - // What going ahead cost: a quick return with the creates it went past still in the driver means this driver - // did not hold the submission behind them. The first 8. - if (ahead.since_ms >= 0 && logged_ahead_ < 8) { + if (ahead.since_ms >= 0) probe(ahead); + if (--submits_ == 0) gate_changed_.notify_all(); + } + +private: + // With gate_mutex_ held. + bool gate_off() const { return verdict_ == GateVerdict::driver_does_not_hold; } + + // With gate_mutex_ held, as a submission leaves the driver: what its time there says about the driver (see the + // gate's comment). The ones that went ahead are logged (the first 8), and the verdict once. + void probe(const GateAhead& ahead) { + const std::int64_t now = diagnostic_ms(); + const std::int64_t took = now - ahead.since_ms; + const std::uint32_t still = compiles_ - fresh_; // creates not waited for, still in the driver + if (ahead.went_ahead && logged_ahead_ < 8) { ++logged_ahead_; std::fprintf(stderr, "[rt] compile-gate: the submission that went ahead on thread %u was in the driver " "for %lld ms; %u of the %u create(s) it went past are still there\n", - diagnostic_thread_id(), static_cast(diagnostic_ms() - ahead.since_ms), - compiles_ - fresh_, ahead.creates); + diagnostic_thread_id(), static_cast(took), still, ahead.creates); } - if (--submits_ == 0) gate_changed_.notify_all(); + if (!adaptive_ || verdict_ != GateVerdict::pending || !still) return; + if (took >= kProbeHeldMs) { + verdict_ = GateVerdict::driver_holds; + std::fprintf(stderr, "[rt] compile-gate: a submission stayed %lld ms in the driver with %u create(s) still " + "in it at t=%.3f s: this driver may hold submissions behind creates, so the gate " + "stays on for the rest of the run\n", + static_cast(took), still, double(now) / 1000.0); + return; + } + if (took >= kProbeQuickMs) return; // says nothing either way + if (quick_probes_++ == 0) first_quick_ms_ = now; + longest_quick_ms_ = (std::max)(longest_quick_ms_, took); + if (quick_probes_ < kQuickProbes || now - first_quick_ms_ < kProbeSpanMs) return; + verdict_ = GateVerdict::driver_does_not_hold; + gate_changed_.notify_all(); // the submissions and creates waiting now go + std::fprintf(stderr, "[rt] compile-gate: off for the rest of the run at t=%.3f s: %u submissions entered the " + "driver with a create in it and came back in at most %lld ms (under %lld ms), the first " + "and last %lld ms apart, the create still there: this driver does not hold a submission " + "behind a create (LEONIDA_COMPILE_GATE=1 keeps the gate)\n", + double(now) / 1000.0, quick_probes_, static_cast(longest_quick_ms_), + static_cast(kProbeQuickMs), static_cast(now - first_quick_ms_)); } -private: void taken() { since_.store(diagnostic_ms(), std::memory_order_relaxed); owner_.store(diagnostic_thread_id(), std::memory_order_relaxed); @@ -648,6 +721,10 @@ class SubmitMutex { std::uint64_t overdue_through_ = 0; // creates up to this number were in the driver when a submission went ahead std::uint32_t submits_ = 0; // submissions waiting for them or in the driver std::int64_t wait_limit_ms_ = default_gate_wait_limit_ms(); // 0: no limit + bool adaptive_ = compile_gate_mode() == GateMode::adaptive; + GateVerdict verdict_ = GateVerdict::pending; + unsigned quick_probes_ = 0; + std::int64_t first_quick_ms_ = 0, longest_quick_ms_ = 0; std::atomic gate_waiter_{0}; GateStats stats_; unsigned logged_waits_ = 0; diff --git a/runtime/test/submit_mutex_test.cpp b/runtime/test/submit_mutex_test.cpp index c3b9b2d173..23aa7be40e 100644 --- a/runtime/test/submit_mutex_test.cpp +++ b/runtime/test/submit_mutex_test.cpp @@ -138,6 +138,7 @@ int compile_gate() { } SubmitMutex m; m.set_gate_wait_limit_ms(0); // no limit: gate_wait_limit checks the limit + m.set_gate_adaptive(false); // gate_adapts checks the switch // Nothing in the driver: a submission passes at once. { const std::int64_t start = now_ms(); @@ -208,6 +209,7 @@ int gate_wait_limit() { if (!std::getenv("LEONIDA_COMPILE_GATE_WAIT_MS") && !std::getenv("LEONIDA_TITLE_WATCHDOG_KICK")) EXPECT(SubmitMutex::default_gate_wait_limit_ms() == SubmitMutex::kDefaultGateWaitMs); SubmitMutex m; + m.set_gate_adaptive(false); // as LEONIDA_COMPILE_GATE=1: it never turns off m.set_gate_wait_limit_ms(150); EXPECT(m.gate_wait_limit_ms() == 150); // A create that stays in the driver until it is let go (one that takes longer than the limit). @@ -267,6 +269,7 @@ int gate_wait_limit() { EXPECT(!third_ahead && third_entered_at >= short_left_at.load() && long_still_in); const SubmitMutex::GateStats stats = m.gate_stats(); EXPECT(stats.submits == 2 && stats.ahead == 1); + EXPECT(m.gate_verdict() == SubmitMutex::GateVerdict::pending); // quick probes, but not adaptive // Both gone: a submission passes at once. { const std::int64_t start = now_ms(); @@ -279,12 +282,113 @@ int gate_wait_limit() { return 0; } +int gate_adapts() { + if (!SubmitMutex::compile_gate_enabled()) { + std::printf("compile gate off (LEONIDA_COMPILE_GATE=0): adaptive switch not checked\n"); + return 0; + } + using Verdict = SubmitMutex::GateVerdict; + // Quick probes: the gate goes off once two, a second apart, came back with the create still in the driver. + { + SubmitMutex m; + m.set_gate_adaptive(true); + m.set_gate_wait_limit_ms(100); + std::atomic in{false}, release{false}; + std::thread create([&] { + const SubmitMutex::CompileTurn turn(m); + in = true; + while (!release) std::this_thread::sleep_for(1ms); + }); + const bool entered = eventually([&] { return in.load(); }); + bool first_ahead = false; + { + const SubmitMutex::SubmitTurn turn(m); // goes ahead at the limit: the first quick probe + first_ahead = turn.went_ahead(); + } + { const SubmitMutex::SubmitTurn turn(m); } // the second, too soon after the first + const Verdict soon = m.gate_verdict(); + std::this_thread::sleep_for(1100ms); + { const SubmitMutex::SubmitTurn turn(m); } // the third, a second later + const Verdict later = m.gate_verdict(); + // Off: a create that enters now holds no submission back. + std::atomic fresh_in{false}, release_fresh{false}; + std::thread fresh([&] { + const SubmitMutex::CompileTurn turn(m); + fresh_in = true; + while (!release_fresh) std::this_thread::sleep_for(1ms); + }); + const bool fresh_entered = eventually([&] { return fresh_in.load(); }); + const std::int64_t start = now_ms(); + bool fresh_ahead = true; + { + const SubmitMutex::SubmitTurn turn(m); + fresh_ahead = turn.went_ahead(); + } + const std::int64_t passed = now_ms() - start; + release = true; + release_fresh = true; + create.join(); + fresh.join(); + EXPECT(entered && fresh_entered && first_ahead); + EXPECT(soon == Verdict::pending); + EXPECT(later == Verdict::driver_does_not_hold); + EXPECT(passed < 50 && !fresh_ahead); + } + std::printf("compile gate: off after two quick probes a second apart, not after two within the second\n"); + // A held probe: the gate stays on, and quick probes after it change nothing. + { + SubmitMutex m; + m.set_gate_adaptive(true); + m.set_gate_wait_limit_ms(100); + std::atomic in{false}, release{false}; + std::thread create([&] { + const SubmitMutex::CompileTurn turn(m); + in = true; + while (!release) std::this_thread::sleep_for(1ms); + }); + const bool entered = eventually([&] { return in.load(); }); + { + const SubmitMutex::SubmitTurn turn(m); // goes ahead, then stays "in the driver" for 2.1 s + std::this_thread::sleep_for(2100ms); + } + const Verdict held = m.gate_verdict(); + { const SubmitMutex::SubmitTurn turn(m); } + std::this_thread::sleep_for(1100ms); + { const SubmitMutex::SubmitTurn turn(m); } + const Verdict still = m.gate_verdict(); + // Still gating: a create that enters now is waited for. + m.set_gate_wait_limit_ms(5000); + std::atomic short_in{false}; + std::atomic short_left_at{0}; + std::thread short_create([&] { + const SubmitMutex::CompileTurn turn(m); + short_in = true; + std::this_thread::sleep_for(200ms); + short_left_at = now_ms(); + }); + const bool short_entered = eventually([&] { return short_in.load(); }); + std::int64_t entered_at = 0; + { + const SubmitMutex::SubmitTurn turn(m); + entered_at = now_ms(); + } + release = true; + create.join(); + short_create.join(); + EXPECT(entered && short_entered); + EXPECT(held == Verdict::driver_holds && still == Verdict::driver_holds); + EXPECT(entered_at >= short_left_at.load()); + } + std::printf("compile gate: a probe held 2.1 s keeps it on, later quick probes do not turn it off\n"); + return 0; +} + } // namespace int main() { std::setvbuf(stdout, nullptr, _IONBF, 0); if (owner_tracking() || waits_for_holder() || waits_for_queued_submitter() || handoff_is_capped() || - compile_gate() || gate_wait_limit()) + compile_gate() || gate_wait_limit() || gate_adapts()) return 1; std::printf("submit_mutex_test: all passed\n"); return 0; From a3404f5a415a3e62f03296fec92ce36fdb774508 Mon Sep 17 00:00:00 2001 From: OreoFR <29048345+OreoFR@users.noreply.github.com> Date: Thu, 26 Nov 2026 23:34:54 +0100 Subject: [PATCH 3/8] compile gate: probe once the waits of the last 10 s add up to 2.5 s, whatever the watchdog does (LEONIDA_COMPILE_GATE_PROBE_MS) The adaptive gate never probed in two runs because no single wait reached the 15 s limit: on NVIDIA 621.40, 80 waits of 3-7 s back to back (90 s in all) kept the title's main loop from reporting for 30 s and its watchdog ended the run; on an RTX 4060 with the watchdog kept kicked the limit was 0, so nothing could probe, and 459 waits of up to 2.9 s took 155 s by t=202 s. Until a verdict is in, a submission now also goes ahead once the waits at the gate in the last 10 s, its own included, add up to LEONIDA_COMPILE_GATE_PROBE_MS (2500 ms). A budget over a window rather than a shorter per-wait limit, because it also counts many short waits and a single wait reaches it as soon as a 2.5 s limit would; before the verdict the gate stops the queues for at most about a quarter of any 10 s. On a driver that holds submissions (630.20) the first probe enters after 2.5 s instead of 15 s and loses the device if the create stays 10 s more; the held verdict (still 2 s, inside those 10 s) ends the early probes and leaves only the per-wait limit, so that is one probe per run. The limit and the budget no longer depend on the title's watchdog. Only LEONIDA_COMPILE_GATE_WAIT_MS=0 (the budget then defaults to 0) or LEONIDA_COMPILE_GATE=1 (no limit by default, never probes) waits however long the creates take. Tested: submit_mutex (new gate_probe_budget case; also under LEONIDA_COMPILE_GATE=1 and =0, with the watchdog variables set, and with LEONIDA_COMPILE_GATE_WAIT_MS=0), MSVC, Windows 11. Co-Authored-By: Claude Opus 5.5 --- docs/d3d12_vulkan.md | 22 +++- runtime/README.md | 5 +- runtime/src/vulkan_device_fault.h | 178 +++++++++++++++++++++-------- runtime/test/submit_mutex_test.cpp | 118 ++++++++++++++++++- 4 files changed, 263 insertions(+), 60 deletions(-) diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 7bcfec7861..1b62079197 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -2890,8 +2890,7 @@ submitting thread; at the maintainers' decision it was replaced by this one when the process; with `LEONIDA_COMPILE_GATE=0` the same build went past that point. A submission now waits at most `LEONIDA_COMPILE_GATE_WAIT_MS` (15 s by default) and then goes ahead; the creates in the driver at that moment hold no later submission back, and creates that enter after it are waited for again. On 630.20 that loses only creates - over 25 s (15 s here, then the driver's 10 s), which the watchdog ends anyway. With the watchdog kept kicked - (`LEONIDA_TITLE_WATCHDOG=0` and `LEONIDA_TITLE_WATCHDOG_KICK`) the default is no limit. + over 25 s (15 s here, then the driver's 10 s), which the watchdog ends anyway. On the same laptop the three submissions that went ahead came back from the driver in 11, 1 and 0 ms with the create still there, and no device was lost; but consecutive long creates made submissions wait the full 15 s again and again, and the title's watchdog still ended the run. So by default the gate adapts: every submission that @@ -2902,8 +2901,23 @@ submitting thread; at the maintainers' decision it was replaced by this one when between; the second costs no extra wait, since after one has gone ahead the next enter at once while the create is still there. One probe that stays 2 s or more keeps the gate on for the rest of the run; the turn also covers presents and idle waits, so it can be a false alarm, but it only keeps the bounded gate. Both verdicts are logged - once. `LEONIDA_COMPILE_GATE=1` gates always and never probes. Tested by `submit_mutex` (no GPU); the adaptive switch - is not yet measured in the title. + once. `LEONIDA_COMPILE_GATE=1` gates always and never probes. + In the next run on that laptop the story load passed, but no wait reached 15 s, so no probe came: 80 submissions + waited 3-7 s each (90 s in all, the longest 6.7 s), back to back, and the title's watchdog ended the run at 433 s. + So until a verdict is in, a submission also goes ahead once the submissions' waits at the gate in the last 10 s, its + own included, add up to `LEONIDA_COMPILE_GATE_PROBE_MS` (2500 ms). A budget over a + window rather than a shorter limit per wait, because it also counts many short waits, and one wait reaches it as + soon as a 2.5 s limit would. Before the verdict the gate then stops the queues for at most about a quarter of any + 10 s, after a quick verdict not at all. On a driver that does hold submissions (630.20) the first probe now enters + the driver after 2.5 s of waiting instead of 15 s, and loses the device if the create stays there 10 s more: one + probe per run, since the held verdict, 2 s into that probe, ends the early probes and leaves only the per-wait + limit. The held threshold stays 2 s, well inside the 10 s such a driver gives before the loss. The limit and the + budget apply with the title's watchdog kept kicked too (`LEONIDA_TITLE_WATCHDOG=0` and + `LEONIDA_TITLE_WATCHDOG_KICK`): nothing ends the run then, but the stops still cost the frames, and an RTX 4060 run + that way had 459 waits, 155 s in all and the longest 2.9 s, by t=202 s. Only `LEONIDA_COMPILE_GATE_WAIT_MS=0` (with + no `LEONIDA_COMPILE_GATE_PROBE_MS`) or `LEONIDA_COMPILE_GATE=1` (with no `LEONIDA_COMPILE_GATE_WAIT_MS`) waits + however long the creates take and never adapts. + Tested by `submit_mutex` (no GPU); the probe budget is not yet measured in the title. **Open:** whether the game uses `D3D12_PIPELINE_STATE_STREAM_DESC` for mesh/RT and which subobject types appear. The trace will enumerate them. diff --git a/runtime/README.md b/runtime/README.md index 8cec193810..3fa397ee8e 100644 --- a/runtime/README.md +++ b/runtime/README.md @@ -419,8 +419,9 @@ ones marked *diagnostic* only add log lines or exist to compare a run with and w | `LEONIDA_TEXTURE_REFRESH_ASYNC` | unset | `1` moves the before-submission refresh of the title textures (`refresh_title_textures`) to a worker: a submission only wakes it, and fills by itself only the mapped textures that were never filled, so their first draw has their bytes. Persistent mappings are hashed whole on the worker every pass; the rest is swept 128 textures at a time. A texture changed since may be sampled a frame behind. | | `LEONIDA_SUBMIT_ASYNC` | `0` | ``: each queue's `ExecuteCommandLists`, `Signal`, `Wait` and markers run in call order on a worker of the queue's own (`src/vulkan_backend.h`), at most n batches queued; the caller of the next one waits for room. The replay, the before-submission refreshes and `vkQueueSubmit` then leave the title's thread. A batch that fails is reported as a held one is. `0`: on the caller's thread. | | `LEONIDA_COMPUTE_WARMUP_LIVE_MAX_BYTES` | unset | ``: a compute warm-up of a module over n bytes of SPIR-V is not started while the title submits (a submission in the last second); the pipeline is built at its first use instead (docs/d3d12_vulkan.md, "Pipelines still being built"). Unset or `0`: every warm-up runs, ordered behind a submission's compiles by the large-compile gate. | -| `LEONIDA_COMPILE_GATE` | adaptive | The compile gate (`SubmitMutex::CompileTurn`, `src/vulkan_device_fault.h`): a queue submission (or a present or device wait) does not enter the driver while a pipeline create is in it, and waits in the runtime instead, since on NVIDIA 630.20 a `vkQueueSubmit` that entered during a long `vkCreateComputePipelines` waited 10 s in the driver and lost the device. Creates pass together; one that arrives while a submission waits queues behind it. The wait is bounded by `LEONIDA_COMPILE_GATE_WAIT_MS`. Unset: adaptive. Each submission that enters the driver next to a create the gate went past at that limit is a probe; two that come back in under 250 ms, at least 1 s apart, with the create still in the driver turn the gate off for the rest of the run (NVIDIA 621.40: 0-11 ms), and one that stays 2 s or more keeps it on; both are logged once. `1`: always gate, never turn it off, and a compute pipeline build also holds the submit lock for its driver calls (`compute_driver_turn`, `shims/d3d12_vulkan_device.cpp`). `0`: no gate. | -| `LEONIDA_COMPILE_GATE_WAIT_MS` | 15000 (none with the watchdog kept kicked) | ``: how long a submission waits at the compile gate before it goes ahead anyway; the creates in the driver then hold no later submission back (creates that enter after it are waited for again), and the log says how long the submission then took in the driver. The waiter holds the submit lock, so every queue stops behind it, and the title's hang watchdog ends the process after 30 s (one create stayed in the driver for over 54 s on NVIDIA 621.40). 15 s keeps the 630.20 protection for creates of up to 25 s. `0`: no limit, and so no probes for the adaptive gate. Unset with `LEONIDA_TITLE_WATCHDOG=0` and `LEONIDA_TITLE_WATCHDOG_KICK` set: no limit, since nothing ends a long stop then. | +| `LEONIDA_COMPILE_GATE` | adaptive | The compile gate (`SubmitMutex::CompileTurn`, `src/vulkan_device_fault.h`): a queue submission (or a present or device wait) does not enter the driver while a pipeline create is in it, and waits in the runtime instead, since on NVIDIA 630.20 a `vkQueueSubmit` that entered during a long `vkCreateComputePipelines` waited 10 s in the driver and lost the device. Creates pass together; one that arrives while a submission waits queues behind it. The wait is bounded by `LEONIDA_COMPILE_GATE_WAIT_MS`. Unset: adaptive. Each submission that enters the driver next to a create the gate went past (at that limit, or earlier at the probe budget, `LEONIDA_COMPILE_GATE_PROBE_MS`) is a probe; two that come back in under 250 ms, at least 1 s apart, with the create still in the driver turn the gate off for the rest of the run (NVIDIA 621.40: 0-11 ms), and one that stays 2 s or more keeps it on, with only the per-wait limit from then on; both are logged once. `1`: always gate with no limit unless `LEONIDA_COMPILE_GATE_WAIT_MS` sets one, never probe or turn it off, and a compute pipeline build also holds the submit lock for its driver calls (`compute_driver_turn`, `shims/d3d12_vulkan_device.cpp`). `0`: no gate. | +| `LEONIDA_COMPILE_GATE_WAIT_MS` | 15000 (0 with `LEONIDA_COMPILE_GATE=1`) | ``: how long a submission waits at the compile gate before it goes ahead anyway; the creates in the driver then hold no later submission back (creates that enter after it are waited for again), and the log says how long the submission then took in the driver. The waiter holds the submit lock, so every queue stops behind it, and the title's hang watchdog ends the process after 30 s (one create stayed in the driver for over 54 s on NVIDIA 621.40). 15 s keeps the 630.20 protection for creates of up to 25 s. The same with the watchdog kept kicked: nothing ends the run then, but the stops still cost the frames (an RTX 4060 run that way had 459 waits, 155 s in all, by t=202 s). `0`: no limit, and with `LEONIDA_COMPILE_GATE_PROBE_MS` unset no probes either: the gate waits however long the creates take and never adapts. | +| `LEONIDA_COMPILE_GATE_PROBE_MS` | 2500 (0 with `LEONIDA_COMPILE_GATE_WAIT_MS=0`) | ``: the adaptive gate's probe budget. Until it knows whether the driver holds a submission behind a create, a submission also goes ahead once the submissions' waits at the gate in the last 10 s, its own included, add up to this; a single wait reaches it at 2.5 s. On NVIDIA 621.40 waits of 3-7 s, none near the 15 s limit, came back to back and kept the title's main loop from reporting for 30 s; on an RTX 4060 with the watchdog kept kicked, 459 waits of up to 2.9 s took 155 s by t=202 s. The cost on a driver that does hold submissions (630.20): the first probe enters the driver after 2.5 s of waiting and loses the device if the create stays 10 s more; the held verdict then stops the early probes. `0`: probes only at `LEONIDA_COMPILE_GATE_WAIT_MS`. Not used with `LEONIDA_COMPILE_GATE=1`. | | `LEONIDA_FAST_PIPELINES` | unset | `graphics`, `compute` or `all` (`1` = all) creates those pipelines with `VK_PIPELINE_CREATE_DISABLE_OPTIMIZATION_BIT` (`shims/d3d12_pipeline_compile.h`): slower shaders, much shorter builds. Part of the compute journal key, so optimized and fast builds keep separate files. | | `LEONIDA_GRAPHICS_COMPILE_WORKER` | on | A second compile worker that takes graphics pipeline builds only, so a long compute build does not hold them up (`shims/d3d12_pipeline_compile.h`). `0`: the one general worker builds everything. | | `LEONIDA_CONSOLE_HEAP_GPU` | unset | `1`: the V#s of the console descriptor heaps are rebased by a compute pass at the start of each submission (`src/console_heap_gpu.h`) instead of on the CPU. The CPU still finds the changed slots and registers the T#s; a V# the pass cannot answer is resolved after its submission and its slot queued again. | diff --git a/runtime/src/vulkan_device_fault.h b/runtime/src/vulkan_device_fault.h index cb9a439b4d..b14469d9f9 100644 --- a/runtime/src/vulkan_device_fault.h +++ b/runtime/src/vulkan_device_fault.h @@ -407,8 +407,9 @@ class SubmitMutex { // were in the driver then no longer hold any submission back; creates that enter later are waited for again. The // default limit, 15 s, keeps the protection measured above for every create of up to 25 s: 630.20 gave the // submission 10 s in the driver before the loss, and a stop much longer than 25 s ends at the watchdog anyway. - // With the watchdog kept kicked (LEONIDA_TITLE_WATCHDOG=0 with LEONIDA_TITLE_WATCHDOG_KICK) nothing ends a long - // stop, so the wait is not bounded unless the variable asks for it. + // The same with the watchdog kept kicked (LEONIDA_TITLE_WATCHDOG=0 with LEONIDA_TITLE_WATCHDOG_KICK): nothing ends + // the run then, but the stops still cost the frames (an RTX 4060 run that way had 459 waits, 155 s in all, by + // t=202 s). Only LEONIDA_COMPILE_GATE_WAIT_MS=0 waits for the creates however long they take. // // Adaptive (the default): each submission that enters the driver alongside a create the gate no longer waits for // is a probe of whether this driver holds a submission behind a create. Back while that create is still in the @@ -422,16 +423,31 @@ class SubmitMutex { // rest of the run (the turn also covers presents and idle waits, which can take that long on their own, so a held // probe may be a false alarm; it only keeps the bounded gate, never less protection). LEONIDA_COMPILE_GATE=1 gates // without probing, LEONIDA_COMPILE_GATE=0 turns the gate off. let_submit_finish is unaffected by all of this. + // + // The first probes come early: while no verdict is in, a submission also goes ahead once the submissions' waits at + // the gate in the last kProbeWindowMs, its own included, add up to the probe budget (gate_probe_budget_ms, + // LEONIDA_COMPILE_GATE_PROBE_MS, 2500 ms). On 621.40 no single wait reached 15 s, but 80 waits of up to 6.7 s, + // back to back, kept the main loop from reporting for 30 s. A budget over a window rather than a shorter limit + // per wait, because it also counts many short waits, and a single wait reaches it as soon as a 2.5 s limit + // would. Before the verdict the gate then stops the queues for at most a quarter of any 10 s; after a quick + // verdict not at all. The price is on a driver that does hold submissions (630.20): the first probe enters the + // driver after 2.5 s of waiting instead of 15, and loses the device if the create stays in the driver 10 s more. + // That is one probe per run, the held verdict comes 2 s into it, and from then on only the per-wait limit + // applies, so no submission enters next to a create early again. The alternative there is the same run starved + // by waits like the ones above. LEONIDA_COMPILE_GATE_WAIT_MS=0 without LEONIDA_COMPILE_GATE_PROBE_MS also makes the + // budget 0: the gate then waits however long the creates take and never probes, as LEONIDA_COMPILE_GATE=1 does. enum class GateMode { off, adaptive, always }; enum class GateVerdict { pending, driver_does_not_hold, driver_holds }; static constexpr std::int64_t kProbeQuickMs = 250; static constexpr std::int64_t kProbeHeldMs = 2000; static constexpr std::int64_t kProbeSpanMs = 1000; static constexpr unsigned kQuickProbes = 2; + static constexpr std::int64_t kProbeWindowMs = 10000; + static constexpr std::int64_t kDefaultProbeBudgetMs = 2500; struct GateAhead { // a submission that entered the driver alongside a create not waited for std::int64_t since_ms = -1; // when it entered, or -1: no such create was in the driver std::uint32_t creates = 0; // such creates in the driver then - bool went_ahead = false; // it waited at the gate up to the limit + bool went_ahead = false; // it waited at the gate up to the limit or the probe budget }; class CompileTurn { // around vkCreate*Pipelines public: @@ -473,40 +489,49 @@ class SubmitMutex { } static bool compile_gate_enabled() { return compile_gate_mode() != GateMode::off; } - // How long a submission waits at the gate before it goes ahead (0: until the creates leave), read once: - // LEONIDA_COMPILE_GATE_WAIT_MS=; unset, kDefaultGateWaitMs with the title's hang watchdog armed and no limit - // with it kept kicked (both variables set, as src/main.cpp asks for). + // Read once. How long one submission waits at the gate before it goes ahead (0: until the creates leave): + // LEONIDA_COMPILE_GATE_WAIT_MS=. The adaptive gate's probe budget over kProbeWindowMs (0: no early probes): + // LEONIDA_COMPILE_GATE_PROBE_MS=. Unset, kDefaultGateWaitMs and kDefaultProbeBudgetMs, whatever the title's + // hang watchdog does; the budget is 0 when the limit is, and the limit 0 with LEONIDA_COMPILE_GATE=1. static constexpr std::int64_t kDefaultGateWaitMs = 15000; - static std::int64_t default_gate_wait_limit_ms() { - static const std::int64_t ms = []() -> std::int64_t { - std::int64_t limit = kDefaultGateWaitMs; - const char* watchdog = std::getenv("LEONIDA_TITLE_WATCHDOG"); - const char* kick = std::getenv("LEONIDA_TITLE_WATCHDOG_KICK"); - if (watchdog && !std::strcmp(watchdog, "0") && kick && *kick) limit = 0; - if (const char* value = std::getenv("LEONIDA_COMPILE_GATE_WAIT_MS"); value && *value) { + struct GateDefaults { + std::int64_t wait_ms; + std::int64_t probe_ms; + }; + static GateDefaults gate_defaults() { + static const GateDefaults defaults = [] { + const auto read = [](const char* name, std::int64_t fallback) -> std::int64_t { + const char* value = std::getenv(name); + if (!value || !*value) return fallback; char* end = nullptr; const long long n = std::strtoll(value, &end, 10); - if (end && !*end && n >= 0) limit = n; - else - std::fprintf(stderr, "[rt] compile-gate: LEONIDA_COMPILE_GATE_WAIT_MS=%s is not a number of " - "milliseconds; ignored\n", value); - } - if (compile_gate_enabled()) { - const char* how = compile_gate_mode() == GateMode::adaptive - ? "adaptive: off once submissions come back quickly with a create in the driver" - : "always (LEONIDA_COMPILE_GATE=1)"; - if (limit) - std::fprintf(stderr, "[rt] compile-gate: %s; a submission waits at most %lld ms for pipeline " - "creates to leave the driver, then goes ahead (LEONIDA_COMPILE_GATE_WAIT_MS)\n", - how, static_cast(limit)); - else - std::fprintf(stderr, "[rt] compile-gate: %s; a submission waits for pipeline creates to leave the " - "driver, however long they take (LEONIDA_COMPILE_GATE_WAIT_MS)\n", how); - } - return limit; + if (end && !*end && n >= 0) return n; + std::fprintf(stderr, "[rt] compile-gate: %s=%s is not a number of milliseconds; ignored\n", name, value); + return fallback; + }; + const bool always = compile_gate_mode() == GateMode::always; // the gate as before: no limit by default + const std::int64_t wait_ms = read("LEONIDA_COMPILE_GATE_WAIT_MS", always ? 0 : kDefaultGateWaitMs); + const GateDefaults d{wait_ms, read("LEONIDA_COMPILE_GATE_PROBE_MS", wait_ms ? kDefaultProbeBudgetMs : 0)}; + if (always && d.wait_ms) + std::fprintf(stderr, "[rt] compile-gate: always (LEONIDA_COMPILE_GATE=1); a submission waits at most " + "%lld ms for pipeline creates to leave the driver (LEONIDA_COMPILE_GATE_WAIT_MS)\n", + static_cast(d.wait_ms)); + else if (always) + std::fprintf(stderr, "[rt] compile-gate: always (LEONIDA_COMPILE_GATE=1); a submission waits for " + "pipeline creates to leave the driver, however long they take\n"); + else if (compile_gate_mode() == GateMode::adaptive) + std::fprintf(stderr, "[rt] compile-gate: adaptive, off once submissions come back quickly with a create " + "in the driver; a submission goes ahead after %lld ms of waiting (0: no limit, " + "LEONIDA_COMPILE_GATE_WAIT_MS) or, until that is known, once the waits of the last " + "%lld s add up to %lld ms (0: never, LEONIDA_COMPILE_GATE_PROBE_MS)\n", + static_cast(d.wait_ms), static_cast(kProbeWindowMs / 1000), + static_cast(d.probe_ms)); + return d; }(); - return ms; + return defaults; } + static std::int64_t default_gate_wait_limit_ms() { return gate_defaults().wait_ms; } + static std::int64_t default_gate_probe_budget_ms() { return gate_defaults().probe_ms; } std::int64_t gate_wait_limit_ms() { std::lock_guard lock(gate_mutex_); return wait_limit_ms_; @@ -515,6 +540,14 @@ class SubmitMutex { std::lock_guard lock(gate_mutex_); wait_limit_ms_ = (std::max)(ms, std::int64_t{0}); } + std::int64_t gate_probe_budget_ms() { + std::lock_guard lock(gate_mutex_); + return probe_budget_ms_; + } + void set_gate_probe_budget_ms(std::int64_t ms) { // for tests + std::lock_guard lock(gate_mutex_); + probe_budget_ms_ = (std::max)(ms, std::int64_t{0}); + } void set_gate_adaptive(bool on) { // for tests std::lock_guard lock(gate_mutex_); adaptive_ = on; @@ -579,48 +612,71 @@ class SubmitMutex { gate_waiter_.store(self, std::memory_order_relaxed); const std::int64_t start = diagnostic_ms(); const std::int64_t limit = wait_limit_ms_; + // The probe budget, only while the adaptive gate has no verdict. + const std::int64_t budget = adaptive_ && verdict_ == GateVerdict::pending ? probe_budget_ms_ : 0; const GateWait what{self, fresh_, driver_compile.name.load(std::memory_order_acquire), driver_compile.trigger.load(std::memory_order_relaxed), driver_compile.bytes.load(std::memory_order_relaxed)}; std::int64_t next_note = 2000; - bool went_ahead = false; + const char* why = nullptr; // set: it goes ahead + std::int64_t window_ms = 0; // with the probe budget: the waits of the window, this one's included for (;;) { - std::int64_t step = 250; - if (limit > 0) step = std::clamp(limit - (diagnostic_ms() - start), 1, 250); - if (gate_changed_.wait_for(lock, std::chrono::milliseconds(step), - [&] { return fresh_ == 0 || gate_off(); })) - break; - const std::int64_t waited = diagnostic_ms() - start; + const std::int64_t now = diagnostic_ms(); + const std::int64_t waited = now - start; if (limit > 0 && waited >= limit) { + why = "limit"; + } else if (budget > 0 && (window_ms = recent_waits_ms(now) + waited) >= budget) { + why = "probe budget"; + } + if (why) { // Every create in the driver now stops holding submissions back; later ones are waited for. overdue_through_ = last_create_; fresh_ = 0; - went_ahead = true; gate_changed_.notify_all(); break; } - if (waited < next_note) continue; - next_note = waited + 5000; - note_gate_wait("has waited", what, waited); + if (waited >= next_note) { + next_note = waited + 5000; + note_gate_wait("has waited", what, waited); + } + std::int64_t step = 250; + if (limit > 0) step = (std::min)(step, limit - waited); + if (budget > 0) step = (std::min)(step, budget - window_ms); + step = (std::max)(step, std::int64_t{1}); + if (gate_changed_.wait_for(lock, std::chrono::milliseconds(step), + [&] { return fresh_ == 0 || gate_off(); })) + break; } gate_waiter_.store(0, std::memory_order_relaxed); - const std::int64_t waited = diagnostic_ms() - start; + const std::int64_t end = diagnostic_ms(); + const std::int64_t waited = end - start; + remember_wait(start, end); ++stats_.submits; stats_.submit_ms += waited; stats_.submit_max_ms = (std::max)(stats_.submit_max_ms, waited); - if (went_ahead) { + if (why && logged_go_ahead_ >= 16) { + ++stats_.ahead; // the stats line counts the rest + } else if (why) { ++stats_.ahead; + ++logged_go_ahead_; note_gate_wait("goes ahead after", what, waited); - std::fprintf(stderr, "[rt] compile-gate: the %u create(s) still in the driver hold no submission back from " - "now on (%llu submissions went ahead at the %lld ms limit so far, " - "LEONIDA_COMPILE_GATE_WAIT_MS)\n", - compiles_, static_cast(stats_.ahead), static_cast(limit)); + if (why[0] == 'l') + std::fprintf(stderr, "[rt] compile-gate: at the %lld ms limit (LEONIDA_COMPILE_GATE_WAIT_MS): the %u " + "create(s) still in the driver hold no submission back from now on (%llu " + "submissions went ahead so far)\n", + static_cast(limit), compiles_, static_cast(stats_.ahead)); + else + std::fprintf(stderr, "[rt] compile-gate: the waits of the last %lld s add up to %lld ms, the probe " + "budget is %lld ms (LEONIDA_COMPILE_GATE_PROBE_MS): the %u create(s) still in the " + "driver hold no submission back from now on (%llu submissions went ahead so far)\n", + static_cast(kProbeWindowMs / 1000), static_cast(window_ms), + static_cast(budget), compiles_, static_cast(stats_.ahead)); } else if (waited >= 500 || (waited >= 50 && logged_waits_ < 32)) { // Every wait of 500 ms or more, and the first 32 of 50 ms or more. ++logged_waits_; note_gate_wait("waited", what, waited); } - if (ahead && compiles_ > fresh_) *ahead = GateAhead{diagnostic_ms(), compiles_ - fresh_, went_ahead}; + if (ahead && compiles_ > fresh_) *ahead = GateAhead{end, compiles_ - fresh_, why != nullptr}; return waited; } void leave_submit(const GateAhead& ahead = {}) { @@ -634,6 +690,24 @@ class SubmitMutex { // With gate_mutex_ held. bool gate_off() const { return verdict_ == GateVerdict::driver_does_not_hold; } + // With gate_mutex_ held: the submissions' past waits at the gate, for the probe budget. The last kRecentWaits + // are kept; more in one window are short ones, so dropping the oldest of them costs little. + static constexpr std::size_t kRecentWaits = 64; + void remember_wait(std::int64_t start, std::int64_t end) { + if (end <= start) return; + recent_waits_[recent_count_++ % kRecentWaits] = {start, end}; + } + // How much of the last kProbeWindowMs before `now` they took. + std::int64_t recent_waits_ms(std::int64_t now) const { + const std::int64_t from = now - kProbeWindowMs; + std::int64_t sum = 0; + for (std::size_t i = 0; i < (std::min)(recent_count_, kRecentWaits); ++i) { + const auto& [start, end] = recent_waits_[i]; + sum += (std::max)(std::int64_t{0}, (std::min)(end, now) - (std::max)(start, from)); + } + return sum; + } + // With gate_mutex_ held, as a submission leaves the driver: what its time there says about the driver (see the // gate's comment). The ones that went ahead are logged (the first 8), and the verdict once. void probe(const GateAhead& ahead) { @@ -721,7 +795,10 @@ class SubmitMutex { std::uint64_t overdue_through_ = 0; // creates up to this number were in the driver when a submission went ahead std::uint32_t submits_ = 0; // submissions waiting for them or in the driver std::int64_t wait_limit_ms_ = default_gate_wait_limit_ms(); // 0: no limit + std::int64_t probe_budget_ms_ = default_gate_probe_budget_ms(); // 0: no early probes bool adaptive_ = compile_gate_mode() == GateMode::adaptive; + std::array, kRecentWaits> recent_waits_{}; // [start, end) ms + std::size_t recent_count_ = 0; GateVerdict verdict_ = GateVerdict::pending; unsigned quick_probes_ = 0; std::int64_t first_quick_ms_ = 0, longest_quick_ms_ = 0; @@ -729,6 +806,7 @@ class SubmitMutex { GateStats stats_; unsigned logged_waits_ = 0; unsigned logged_ahead_ = 0; + unsigned logged_go_ahead_ = 0; }; // One vkQueueSubmit of a VulkanQueue: its waits, its signals and how long the call took (-1: not returned yet). diff --git a/runtime/test/submit_mutex_test.cpp b/runtime/test/submit_mutex_test.cpp index 23aa7be40e..0cc38bc9c6 100644 --- a/runtime/test/submit_mutex_test.cpp +++ b/runtime/test/submit_mutex_test.cpp @@ -205,9 +205,11 @@ int gate_wait_limit() { std::printf("compile gate off (LEONIDA_COMPILE_GATE=0): wait limit not checked\n"); return 0; } - // The default with neither LEONIDA_COMPILE_GATE_WAIT_MS nor the watchdog kick set. - if (!std::getenv("LEONIDA_COMPILE_GATE_WAIT_MS") && !std::getenv("LEONIDA_TITLE_WATCHDOG_KICK")) - EXPECT(SubmitMutex::default_gate_wait_limit_ms() == SubmitMutex::kDefaultGateWaitMs); + // The default with LEONIDA_COMPILE_GATE_WAIT_MS unset, whatever the title watchdog variables say: none with + // LEONIDA_COMPILE_GATE=1. + const bool always = SubmitMutex::compile_gate_mode() == SubmitMutex::GateMode::always; + if (!std::getenv("LEONIDA_COMPILE_GATE_WAIT_MS")) + EXPECT(SubmitMutex::default_gate_wait_limit_ms() == (always ? 0 : SubmitMutex::kDefaultGateWaitMs)); SubmitMutex m; m.set_gate_adaptive(false); // as LEONIDA_COMPILE_GATE=1: it never turns off m.set_gate_wait_limit_ms(150); @@ -293,6 +295,7 @@ int gate_adapts() { SubmitMutex m; m.set_gate_adaptive(true); m.set_gate_wait_limit_ms(100); + m.set_gate_probe_budget_ms(0); // probes at the limit only: gate_probe_budget checks the budget std::atomic in{false}, release{false}; std::thread create([&] { const SubmitMutex::CompileTurn turn(m); @@ -340,6 +343,7 @@ int gate_adapts() { SubmitMutex m; m.set_gate_adaptive(true); m.set_gate_wait_limit_ms(100); + m.set_gate_probe_budget_ms(0); // probes at the limit only: gate_probe_budget checks the budget std::atomic in{false}, release{false}; std::thread create([&] { const SubmitMutex::CompileTurn turn(m); @@ -383,12 +387,118 @@ int gate_adapts() { return 0; } +int gate_probe_budget() { + if (!SubmitMutex::compile_gate_enabled()) { + std::printf("compile gate off (LEONIDA_COMPILE_GATE=0): probe budget not checked\n"); + return 0; + } + using Verdict = SubmitMutex::GateVerdict; + // The default budget, 0 when the limit is. + if (!std::getenv("LEONIDA_COMPILE_GATE_PROBE_MS")) + EXPECT(SubmitMutex::default_gate_probe_budget_ms() == + (SubmitMutex::default_gate_wait_limit_ms() ? SubmitMutex::kDefaultProbeBudgetMs : 0)); + // Short waits add up: with no per-wait limit, a submission goes ahead once the waits of the window reach the budget. + { + SubmitMutex m; + m.set_gate_adaptive(true); + m.set_gate_wait_limit_ms(0); + m.set_gate_probe_budget_ms(300); + EXPECT(m.gate_probe_budget_ms() == 300); + std::atomic a_in{false}; + std::thread a([&] { + const SubmitMutex::CompileTurn turn(m); + a_in = true; + std::this_thread::sleep_for(200ms); + }); + const bool a_entered = eventually([&] { return a_in.load(); }); + std::int64_t first_waited = 0; + bool first_ahead = true; + { + const std::int64_t start = now_ms(); + const SubmitMutex::SubmitTurn turn(m); // waits the create out: under the budget + first_waited = now_ms() - start; + first_ahead = turn.went_ahead(); + } + a.join(); + std::atomic b_in{false}, release{false}; + std::thread b([&] { + const SubmitMutex::CompileTurn turn(m); + b_in = true; + while (!release) std::this_thread::sleep_for(1ms); + }); + const bool b_entered = eventually([&] { return b_in.load(); }); + std::int64_t second_waited = 0; + bool second_ahead = false; + { + const std::int64_t start = now_ms(); + const SubmitMutex::SubmitTurn turn(m); // goes ahead once both waits add up to 300 ms + second_waited = now_ms() - start; + second_ahead = turn.went_ahead(); + } + const Verdict after_one = m.gate_verdict(); + release = true; + b.join(); + EXPECT(a_entered && b_entered); + EXPECT(!first_ahead && first_waited >= 100); + EXPECT(second_ahead && first_waited + second_waited >= 250 && second_waited < 300); + EXPECT(after_one == Verdict::pending); // one quick probe is not enough + std::printf("compile gate: waits of %lld and %lld ms reached the 300 ms probe budget, the second went ahead\n", + static_cast(first_waited), static_cast(second_waited)); + } + // After a held verdict the budget no longer applies: a submission waits a create out again. + { + SubmitMutex m; + m.set_gate_adaptive(true); + m.set_gate_wait_limit_ms(0); + m.set_gate_probe_budget_ms(100); + std::atomic in{false}, release{false}; + std::thread create([&] { + const SubmitMutex::CompileTurn turn(m); + in = true; + while (!release) std::this_thread::sleep_for(1ms); + }); + const bool entered = eventually([&] { return in.load(); }); + bool ahead = false; + { + const SubmitMutex::SubmitTurn turn(m); // goes ahead at the budget, then is held 2.1 s + ahead = turn.went_ahead(); + std::this_thread::sleep_for(2100ms); + } + const Verdict held = m.gate_verdict(); + std::atomic short_in{false}; + std::atomic short_left_at{0}; + std::thread short_create([&] { + const SubmitMutex::CompileTurn turn(m); + short_in = true; + std::this_thread::sleep_for(400ms); + short_left_at = now_ms(); + }); + const bool short_entered = eventually([&] { return short_in.load(); }); + std::int64_t entered_at = 0; + bool later_ahead = true; + { + const SubmitMutex::SubmitTurn turn(m); + entered_at = now_ms(); + later_ahead = turn.went_ahead(); + } + release = true; + create.join(); + short_create.join(); + EXPECT(entered && short_entered && ahead); + EXPECT(held == Verdict::driver_holds); + EXPECT(!later_ahead && entered_at >= short_left_at.load()); + } + std::printf("compile gate: after a held probe a 400 ms create is waited out past the 100 ms budget\n"); + return 0; +} + } // namespace int main() { std::setvbuf(stdout, nullptr, _IONBF, 0); if (owner_tracking() || waits_for_holder() || waits_for_queued_submitter() || handoff_is_capped() || - compile_gate() || gate_wait_limit() || gate_adapts()) + compile_gate() || gate_wait_limit() || gate_adapts() || + gate_probe_budget()) return 1; std::printf("submit_mutex_test: all passed\n"); return 0; From f7b2497303466eb3bcebe7fe88cde838b1f45334 Mon Sep 17 00:00:00 2001 From: OreoFR <29048345+OreoFR@users.noreply.github.com> Date: Fri, 27 Nov 2026 00:19:05 +0100 Subject: [PATCH 4/8] compile gate: a verdict per kind of create, re-armed by a held submission, and the creates in the driver on a device loss On NVIDIA 621.40 every probe beside a compute create came back in 0-12 ms, but a run whose gate those probes had turned off lost the device 9 s later with a submission held 7.8 s beside a mesh pipeline create, and with the gate off a vkQueueSubmit sat 54 s in the driver beside a 56 s mesh create before the device was lost. Graphics creates never passed the gate. - Graphics creates (mesh included) now pass the gate (PipelineState::build through DriverTurn), and CompileTurn carries the create's kind, name, trigger and size. The adaptive gate keeps a verdict per kind; a probe counts for each kind that had a create in the driver from before the submission entered until it came back, a held one against all of them. - A kind that is off is still timed: a submission that stays 2 s or more beside one of its creates turns that kind back on for the rest of the run. - No submission goes ahead, at the limit or the probe budget, while a create of a kind that holds submissions is in the driver, and such creates are waited for even if one went past them before the verdict. - A create never waits behind the submission of the thread that holds the submit lock. - VulkanContext::mark_device_lost logs the creates in the driver (kind, program, size, thread, how long, whether submissions wait for them) and each kind's gate. Tested: submit_mutex (new gate_per_kind case; held-verdict cases adjusted; also under LEONIDA_COMPILE_GATE=1 and =0), MSVC, Windows 11. Co-Authored-By: Claude Opus 5.5 --- docs/d3d12_vulkan.md | 14 ++ runtime/README.md | 2 +- runtime/shims/d3d12_pipeline_compile.h | 8 +- runtime/shims/d3d12_vulkan_device.cpp | 25 ++- runtime/src/vulkan_backend.h | 2 +- runtime/src/vulkan_device_fault.h | 268 +++++++++++++++++++------ runtime/test/submit_mutex_test.cpp | 119 ++++++++--- 7 files changed, 340 insertions(+), 98 deletions(-) diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 1b62079197..a5831c2673 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -2918,6 +2918,20 @@ submitting thread; at the maintainers' decision it was replaced by this one when no `LEONIDA_COMPILE_GATE_PROBE_MS`) or `LEONIDA_COMPILE_GATE=1` (with no `LEONIDA_COMPILE_GATE_WAIT_MS`) waits however long the creates take and never adapts. Tested by `submit_mutex` (no GPU); the probe budget is not yet measured in the title. + The verdict is kept per kind of create, compute and graphics (mesh pipelines included), and graphics creates now + pass the gate too (before, only compute builds did). On 621.40 every probe beside a compute create came back in + 0-12 ms (and on an RTX 4060 with 610.88, 133 of them), but a run whose gate those probes had turned off lost the + device 9 s later with a submission held 7.8 s beside a mesh create, and with `LEONIDA_COMPILE_GATE=0` a + `vkQueueSubmit` sat 54 s in the driver beside a 56 s mesh create (541 KB) and the device was lost. A probe counts + for each kind that had a create in the driver from before the submission entered until it came back; a held one + counts against all of them, since which create held it cannot be told. A kind that is off is still timed: a + submission that stays 2 s or more beside one of its creates turns that kind back on for the rest of the run. While + a create of a kind that holds submissions is in the driver, no submission goes ahead, at the limit or the budget, + and such a create is waited for even when one went past it before the verdict: the title then starves as long as + the create lasts (its watchdog may end the run), but the device is kept. On a device loss the log names every create + in the driver (kind, program, SPIR-V size, thread, how long it had been there, whether submissions waited for it) + and each kind's gate. Long graphics creates would leave the title's process only through the compile helper, which + builds compute pipelines alone today. **Open:** whether the game uses `D3D12_PIPELINE_STATE_STREAM_DESC` for mesh/RT and which subobject types appear. The trace will enumerate them. diff --git a/runtime/README.md b/runtime/README.md index 3fa397ee8e..a53d131186 100644 --- a/runtime/README.md +++ b/runtime/README.md @@ -419,7 +419,7 @@ ones marked *diagnostic* only add log lines or exist to compare a run with and w | `LEONIDA_TEXTURE_REFRESH_ASYNC` | unset | `1` moves the before-submission refresh of the title textures (`refresh_title_textures`) to a worker: a submission only wakes it, and fills by itself only the mapped textures that were never filled, so their first draw has their bytes. Persistent mappings are hashed whole on the worker every pass; the rest is swept 128 textures at a time. A texture changed since may be sampled a frame behind. | | `LEONIDA_SUBMIT_ASYNC` | `0` | ``: each queue's `ExecuteCommandLists`, `Signal`, `Wait` and markers run in call order on a worker of the queue's own (`src/vulkan_backend.h`), at most n batches queued; the caller of the next one waits for room. The replay, the before-submission refreshes and `vkQueueSubmit` then leave the title's thread. A batch that fails is reported as a held one is. `0`: on the caller's thread. | | `LEONIDA_COMPUTE_WARMUP_LIVE_MAX_BYTES` | unset | ``: a compute warm-up of a module over n bytes of SPIR-V is not started while the title submits (a submission in the last second); the pipeline is built at its first use instead (docs/d3d12_vulkan.md, "Pipelines still being built"). Unset or `0`: every warm-up runs, ordered behind a submission's compiles by the large-compile gate. | -| `LEONIDA_COMPILE_GATE` | adaptive | The compile gate (`SubmitMutex::CompileTurn`, `src/vulkan_device_fault.h`): a queue submission (or a present or device wait) does not enter the driver while a pipeline create is in it, and waits in the runtime instead, since on NVIDIA 630.20 a `vkQueueSubmit` that entered during a long `vkCreateComputePipelines` waited 10 s in the driver and lost the device. Creates pass together; one that arrives while a submission waits queues behind it. The wait is bounded by `LEONIDA_COMPILE_GATE_WAIT_MS`. Unset: adaptive. Each submission that enters the driver next to a create the gate went past (at that limit, or earlier at the probe budget, `LEONIDA_COMPILE_GATE_PROBE_MS`) is a probe; two that come back in under 250 ms, at least 1 s apart, with the create still in the driver turn the gate off for the rest of the run (NVIDIA 621.40: 0-11 ms), and one that stays 2 s or more keeps it on, with only the per-wait limit from then on; both are logged once. `1`: always gate with no limit unless `LEONIDA_COMPILE_GATE_WAIT_MS` sets one, never probe or turn it off, and a compute pipeline build also holds the submit lock for its driver calls (`compute_driver_turn`, `shims/d3d12_vulkan_device.cpp`). `0`: no gate. | +| `LEONIDA_COMPILE_GATE` | adaptive | The compile gate (`SubmitMutex::CompileTurn`, `src/vulkan_device_fault.h`): a queue submission (or a present or device wait) does not enter the driver while a pipeline create is in it, and waits in the runtime instead, since on NVIDIA 630.20 a `vkQueueSubmit` that entered during a long `vkCreateComputePipelines` waited 10 s in the driver and lost the device. Creates pass together; one that arrives while a submission waits queues behind it. The wait is bounded by `LEONIDA_COMPILE_GATE_WAIT_MS`. Unset: adaptive. Each submission that enters the driver next to a create the gate went past (at that limit, or earlier at the probe budget, `LEONIDA_COMPILE_GATE_PROBE_MS`) is a probe; two that come back in under 250 ms, at least 1 s apart, with the create still in the driver turn the gate off for the rest of the run (NVIDIA 621.40: 0-11 ms), and one that stays 2 s or more keeps it on; both are logged once. The verdict is kept per kind of create, compute and graphics (mesh included; graphics creates pass the gate since then, only compute ones did before): on NVIDIA 621.40 submissions came back in 0-12 ms beside compute creates, but one sat 54 s in the driver beside a 56 s mesh create and the device was lost. A kind that is off is still timed, and a submission that stays 2 s beside one of its creates turns it back on. A submission never goes ahead, at the limit or the budget, beside a create of a kind whose verdict is that it holds submissions: it waits for that create however long it takes. On a device loss the log names the creates in the driver (kind, program, size, how long) and each kind's gate. `1`: always gate with no limit unless `LEONIDA_COMPILE_GATE_WAIT_MS` sets one, never probe or turn it off, and a compute pipeline build also holds the submit lock for its driver calls (`compute_driver_turn`, `shims/d3d12_vulkan_device.cpp`). `0`: no gate. | | `LEONIDA_COMPILE_GATE_WAIT_MS` | 15000 (0 with `LEONIDA_COMPILE_GATE=1`) | ``: how long a submission waits at the compile gate before it goes ahead anyway; the creates in the driver then hold no later submission back (creates that enter after it are waited for again), and the log says how long the submission then took in the driver. The waiter holds the submit lock, so every queue stops behind it, and the title's hang watchdog ends the process after 30 s (one create stayed in the driver for over 54 s on NVIDIA 621.40). 15 s keeps the 630.20 protection for creates of up to 25 s. The same with the watchdog kept kicked: nothing ends the run then, but the stops still cost the frames (an RTX 4060 run that way had 459 waits, 155 s in all, by t=202 s). `0`: no limit, and with `LEONIDA_COMPILE_GATE_PROBE_MS` unset no probes either: the gate waits however long the creates take and never adapts. | | `LEONIDA_COMPILE_GATE_PROBE_MS` | 2500 (0 with `LEONIDA_COMPILE_GATE_WAIT_MS=0`) | ``: the adaptive gate's probe budget. Until it knows whether the driver holds a submission behind a create, a submission also goes ahead once the submissions' waits at the gate in the last 10 s, its own included, add up to this; a single wait reaches it at 2.5 s. On NVIDIA 621.40 waits of 3-7 s, none near the 15 s limit, came back to back and kept the title's main loop from reporting for 30 s; on an RTX 4060 with the watchdog kept kicked, 459 waits of up to 2.9 s took 155 s by t=202 s. The cost on a driver that does hold submissions (630.20): the first probe enters the driver after 2.5 s of waiting and loses the device if the create stays 10 s more; the held verdict then stops the early probes. `0`: probes only at `LEONIDA_COMPILE_GATE_WAIT_MS`. Not used with `LEONIDA_COMPILE_GATE=1`. | | `LEONIDA_FAST_PIPELINES` | unset | `graphics`, `compute` or `all` (`1` = all) creates those pipelines with `VK_PIPELINE_CREATE_DISABLE_OPTIMIZATION_BIT` (`shims/d3d12_pipeline_compile.h`): slower shaders, much shorter builds. Part of the compute journal key, so optimized and fast builds keep separate files. | diff --git a/runtime/shims/d3d12_pipeline_compile.h b/runtime/shims/d3d12_pipeline_compile.h index 2889d7fd36..2c1cbb7177 100644 --- a/runtime/shims/d3d12_pipeline_compile.h +++ b/runtime/shims/d3d12_pipeline_compile.h @@ -138,13 +138,15 @@ inline bool compile_worker_trigger(const char* trigger) { // behind it go first, and does so before its vkCreateShaderModule and vkDestroyShaderModule too; a title thread // waits only for the holder, and only before a pipeline create (it may wait out a vkDeviceWaitIdle or a present: // waits of 50 ms or more are logged). -// - a pipeline create passes the compile gate (SubmitMutex::CompileTurn) for the length of the call. +// - a pipeline create passes the compile gate (SubmitMutex::CompileTurn) for the length of the call, as its kind +// (compute, or graphics with mesh): the adaptive gate keeps a verdict per kind. // - the compile worker, which runs below normal priority, runs the call at normal priority, so a preempted worker // does not sit on a driver lock a submit waits for. class DriverTurn { public: DriverTurn(leonida::SubmitMutex& submits, const char* trigger, const char* call, bool pipeline, - const char* name = nullptr) + const char* name = nullptr, + leonida::SubmitMutex::CreateKind kind = leonida::SubmitMutex::CreateKind::compute, std::size_t bytes = 0) : worker_(compile_worker_trigger(trigger)) { // Normal priority first: a below-normal worker that waits its turn at normal priority cannot keep losing the // race to the next submit, and holds the gate (below) only at normal priority. @@ -160,7 +162,7 @@ class DriverTurn { if (waited >= 50) note("waited for the submit lock", trigger, call, name, waited); } if (pipeline) { - gate_.emplace(submits); + gate_.emplace(submits, leonida::SubmitMutex::CreateInfo{kind, name, trigger, bytes}); if (gate_->waited_ms() >= 50) note("waited at the compile gate", trigger, call, name, gate_->waited_ms()); } } diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index ea36503835..cb0f6e886d 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -11143,7 +11143,8 @@ ComputeBuild build_compute_pipeline(DeviceState& device, const leonida::ShaderEn // gate: from the journal a build takes milliseconds and the warm-up worker makes them back to back, and // three warm boots lost the device in a vkQueueSubmit (no GPU fault, no TDR) while only the create was // gated. No file I/O in here. - const DriverTurn turn(submits, trigger, "vkCreateComputePipelines", true, entry->name.c_str()); + const DriverTurn turn(submits, trigger, "vkCreateComputePipelines", true, entry->name.c_str(), + leonida::SubmitMutex::CreateKind::compute, bytes); { const leonida::aftermath::ShaderName named(entry->name); if (vk.vkCreateShaderModule(dev, &module_info, nullptr, &module) != VK_SUCCESS) return out; @@ -12774,11 +12775,23 @@ class PipelineState final const leonida::dev_status::Build dev_build(r.mesh ? "mesh pipeline" : "graphics pipeline"); const leonida::pipeline_profile::DriverCall profile(r.mesh ? "ms" : "vs", lazy_ ? "lazy" : "eager", program, fragment, unbound); - const auto result = device_.create_pipeline(flags, [&](VkPipelineCache with, VkPipelineCreateFlags f) { - info.flags = f; - pipeline = VK_NULL_HANDLE; - return device_.context->vk.vkCreateGraphicsPipelines(device_.info.device, with, 1, &info, nullptr, &pipeline); - }); + // Through the compile gate as a graphics create (a mesh pipeline is one): on NVIDIA 621.40 a vkQueueSubmit + // sat in the driver for as long as a mesh create was in it (54 s) and lost the device. + const char* gate_name = nullptr; + for (const auto& source : sources_) + if (source.entry && source.slot == 0) gate_name = source.entry->name.c_str(); + VkResult result = VK_ERROR_UNKNOWN; + { + const DriverTurn turn(device_.context->submit_mutex(), trigger, + r.mesh ? "vkCreateGraphicsPipelines (mesh)" : "vkCreateGraphicsPipelines", true, + gate_name, leonida::SubmitMutex::CreateKind::graphics, bytes); + result = device_.create_pipeline(flags, [&](VkPipelineCache with, VkPipelineCreateFlags f) { + info.flags = f; + pipeline = VK_NULL_HANDLE; + return device_.context->vk.vkCreateGraphicsPipelines(device_.info.device, with, 1, &info, nullptr, + &pipeline); + }); + } if (status) *status = result; profile.finish(int(result)); trace.end(trigger, profile_shaders_.c_str(), unbound, bytes, result); diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h index 4494718cd2..103cdaa930 100644 --- a/runtime/src/vulkan_backend.h +++ b/runtime/src/vulkan_backend.h @@ -2073,7 +2073,7 @@ class VulkanContext { } void mark_device_lost(const char* where = "a Vulkan call returned VK_ERROR_DEVICE_LOST") noexcept { - set_device_lost(device_lost_, where); + if (set_device_lost(device_lost_, where)) submit_mutex_.report_creates_on_loss(); // which creates were in it run_device_lost_hook(); // LEONIDA_AFTERMATH: the GPU crash dump is written before waiters wake if (fence_dispatcher_) { try { diff --git a/runtime/src/vulkan_device_fault.h b/runtime/src/vulkan_device_fault.h index b14469d9f9..26e02bbb0a 100644 --- a/runtime/src/vulkan_device_fault.h +++ b/runtime/src/vulkan_device_fault.h @@ -436,6 +436,21 @@ class SubmitMutex { // applies, so no submission enters next to a create early again. The alternative there is the same run starved // by waits like the ones above. LEONIDA_COMPILE_GATE_WAIT_MS=0 without LEONIDA_COMPILE_GATE_PROBE_MS also makes the // budget 0: the gate then waits however long the creates take and never probes, as LEONIDA_COMPILE_GATE=1 does. + // + // A verdict per kind of create (CreateKind: compute, or graphics with mesh): a submission that came back quickly + // beside a compute create says nothing about a graphics one, and a run on 621.40 lost the device beside a mesh + // pipeline create 9 s after compute probes had turned the gate off. A probe counts for each kind that had a + // create in the driver from before the submission entered until it came back: a quick one for all of them, a + // held one against all of them, since which create held it cannot be told. A submission waits for the creates of + // every kind whose gate is not off; the probe budget applies only while all of those are of kinds still pending. + // A kind that is off is still timed: a submission that stays kProbeHeldMs or more beside one of its creates turns + // that kind's gate back on for the rest of the run (logged once), so the submissions after it wait. The one that + // stayed cannot be helped. A submission never goes ahead (at the limit or the budget) while a create of a kind + // that holds submissions is in the driver, and such creates are waited for even if one was gone past before the + // verdict: on 621.40, with the gate off, a vkQueueSubmit sat 54 s in the driver beside a 56 s mesh create and the + // device was lost; waiting here starves the title as long, but keeps the device. Graphics creates pass the gate + // since this split; before it only compute ones did. On a device loss, report_creates_on_loss names the creates + // in the driver and the verdicts. enum class GateMode { off, adaptive, always }; enum class GateVerdict { pending, driver_does_not_hold, driver_holds }; static constexpr std::int64_t kProbeQuickMs = 250; @@ -444,14 +459,28 @@ class SubmitMutex { static constexpr unsigned kQuickProbes = 2; static constexpr std::int64_t kProbeWindowMs = 10000; static constexpr std::int64_t kDefaultProbeBudgetMs = 2500; - struct GateAhead { // a submission that entered the driver alongside a create not waited for + enum class CreateKind : std::uint8_t { compute, graphics }; // graphics: mesh pipelines too + static constexpr std::size_t kKinds = 2; + static const char* kind_name(CreateKind kind) { return kind == CreateKind::compute ? "compute" : "graphics"; } + // What a create is, for the verdicts and the reports. Names are ShaderLibrary strings, valid until device + // teardown (or literals). + struct CreateInfo { + CreateKind kind = CreateKind::compute; + const char* name = nullptr; + const char* trigger = nullptr; + std::size_t bytes = 0; // SPIR-V + }; + struct GateAhead { // a submission that entered the driver alongside creates not waited for std::int64_t since_ms = -1; // when it entered, or -1: no such create was in the driver + std::uint64_t last_create = 0; // the number of the last create that had entered then std::uint32_t creates = 0; // such creates in the driver then + std::uint8_t kinds = 0; // their kinds, bit per CreateKind bool went_ahead = false; // it waited at the gate up to the limit or the probe budget }; class CompileTurn { // around vkCreate*Pipelines public: - explicit CompileTurn(SubmitMutex& gate) : gate_(gate), waited_(gate.enter_compile(seq_)) {} + explicit CompileTurn(SubmitMutex& gate, const CreateInfo& info = {}) + : gate_(gate), waited_(gate.enter_compile(seq_, info)) {} ~CompileTurn() { gate_.leave_compile(seq_); } CompileTurn(const CompileTurn&) = delete; CompileTurn& operator=(const CompileTurn&) = delete; @@ -552,9 +581,9 @@ class SubmitMutex { std::lock_guard lock(gate_mutex_); adaptive_ = on; } - GateVerdict gate_verdict() { + GateVerdict gate_verdict(CreateKind kind = CreateKind::compute) { std::lock_guard lock(gate_mutex_); - return verdict_; + return verdict_[std::size_t(kind)]; } // The thread waiting in enter_submit for creates to leave the driver, or 0. @@ -573,50 +602,54 @@ class SubmitMutex { } // `seq` receives the create's number, which leave_compile takes back. - std::int64_t enter_compile(std::uint64_t& seq) { + std::int64_t enter_compile(std::uint64_t& seq, const CreateInfo& info = {}) { if (!compile_gate_enabled()) return 0; std::unique_lock lock(gate_mutex_); std::int64_t waited = 0; - if (submits_ && !gate_off()) { + // Not behind this thread's own submission (it holds the submit lock: none of its submissions is in the + // driver now, and waiting for them would never end). + const bool own = owner_.load(std::memory_order_relaxed) == diagnostic_thread_id(); + if (submits_ && !own && !kind_off(info.kind)) { const std::int64_t start = diagnostic_ms(); - gate_changed_.wait(lock, [&] { return submits_ == 0 || gate_off(); }); + gate_changed_.wait(lock, [&] { return submits_ == 0 || kind_off(info.kind); }); waited = diagnostic_ms() - start; ++stats_.creates; stats_.create_ms += waited; stats_.create_max_ms = (std::max)(stats_.create_max_ms, waited); } - ++compiles_; - ++fresh_; seq = ++last_create_; + in_driver_.push_back(InDriver{seq, info, diagnostic_ms(), diagnostic_thread_id()}); return waited; } void leave_compile(std::uint64_t seq) { if (!compile_gate_enabled()) return; std::lock_guard lock(gate_mutex_); - --compiles_; - // A create a submission went past at the limit is no longer counted in fresh_. - if (seq > overdue_through_ && --fresh_ == 0 && submits_) gate_changed_.notify_all(); + for (auto it = in_driver_.begin(); it != in_driver_.end(); ++it) + if (it->seq == seq) { + in_driver_.erase(it); + break; + } + if (submits_) gate_changed_.notify_all(); } - // `ahead` (optional) is told whether the submission entered the driver alongside a create the gate no longer - // waits for, and whether it went ahead at the wait limit, for leave_submit. + // `ahead` (optional) is told whether the submission entered the driver alongside creates the gate does not wait + // for, and whether it went ahead at the wait limit or the probe budget, for leave_submit. std::int64_t enter_submit(GateAhead* ahead = nullptr) { if (!compile_gate_enabled()) return 0; std::unique_lock lock(gate_mutex_); ++submits_; - if (!fresh_ || gate_off()) { - if (ahead && compiles_ > fresh_) *ahead = GateAhead{diagnostic_ms(), compiles_ - fresh_, false}; + const InDriver* first = first_blocking(); + if (!first) { + if (ahead) *ahead = beside(diagnostic_ms(), false); return 0; } const std::uint32_t self = diagnostic_thread_id(); gate_waiter_.store(self, std::memory_order_relaxed); const std::int64_t start = diagnostic_ms(); - const std::int64_t limit = wait_limit_ms_; - // The probe budget, only while the adaptive gate has no verdict. - const std::int64_t budget = adaptive_ && verdict_ == GateVerdict::pending ? probe_budget_ms_ : 0; - const GateWait what{self, fresh_, driver_compile.name.load(std::memory_order_acquire), - driver_compile.trigger.load(std::memory_order_relaxed), - driver_compile.bytes.load(std::memory_order_relaxed)}; + std::int64_t limit = wait_limit_ms_; + // The probe budget, only while every kind it waits for has no verdict. + std::int64_t budget = adaptive_ && blocking_kinds_pending() ? probe_budget_ms_ : 0; + const GateWait what{self, blocking(), first->info}; std::int64_t next_note = 2000; const char* why = nullptr; // set: it goes ahead std::int64_t window_ms = 0; // with the probe budget: the waits of the window, this one's included @@ -628,10 +661,21 @@ class SubmitMutex { } else if (budget > 0 && (window_ms = recent_waits_ms(now) + waited) >= budget) { why = "probe budget"; } + if (why && held_blocking()) { + // Never into the driver beside a create of a kind that holds submissions: there it would wait for + // the create anyway, and the driver lost the device under such waits (621.40, a mesh create). It + // waits here for those, however long. + if (logged_held_waits_++ < 8) + std::fprintf(stderr, "[rt] compile-gate: vkQueueSubmit on thread %u reached the %s after %lld ms, " + "but a create of a kind that holds submissions is in the driver: it waits " + "until that create leaves\n", + self, why, static_cast(waited)); + why = nullptr; + limit = budget = 0; + } if (why) { // Every create in the driver now stops holding submissions back; later ones are waited for. overdue_through_ = last_create_; - fresh_ = 0; gate_changed_.notify_all(); break; } @@ -643,8 +687,7 @@ class SubmitMutex { if (limit > 0) step = (std::min)(step, limit - waited); if (budget > 0) step = (std::min)(step, budget - window_ms); step = (std::max)(step, std::int64_t{1}); - if (gate_changed_.wait_for(lock, std::chrono::milliseconds(step), - [&] { return fresh_ == 0 || gate_off(); })) + if (gate_changed_.wait_for(lock, std::chrono::milliseconds(step), [&] { return !first_blocking(); })) break; } gate_waiter_.store(0, std::memory_order_relaxed); @@ -654,6 +697,7 @@ class SubmitMutex { ++stats_.submits; stats_.submit_ms += waited; stats_.submit_max_ms = (std::max)(stats_.submit_max_ms, waited); + const auto creates = static_cast(in_driver_.size()); if (why && logged_go_ahead_ >= 16) { ++stats_.ahead; // the stats line counts the rest } else if (why) { @@ -664,19 +708,19 @@ class SubmitMutex { std::fprintf(stderr, "[rt] compile-gate: at the %lld ms limit (LEONIDA_COMPILE_GATE_WAIT_MS): the %u " "create(s) still in the driver hold no submission back from now on (%llu " "submissions went ahead so far)\n", - static_cast(limit), compiles_, static_cast(stats_.ahead)); + static_cast(limit), creates, static_cast(stats_.ahead)); else std::fprintf(stderr, "[rt] compile-gate: the waits of the last %lld s add up to %lld ms, the probe " "budget is %lld ms (LEONIDA_COMPILE_GATE_PROBE_MS): the %u create(s) still in the " "driver hold no submission back from now on (%llu submissions went ahead so far)\n", static_cast(kProbeWindowMs / 1000), static_cast(window_ms), - static_cast(budget), compiles_, static_cast(stats_.ahead)); + static_cast(budget), creates, static_cast(stats_.ahead)); } else if (waited >= 500 || (waited >= 50 && logged_waits_ < 32)) { // Every wait of 500 ms or more, and the first 32 of 50 ms or more. ++logged_waits_; note_gate_wait("waited", what, waited); } - if (ahead && compiles_ > fresh_) *ahead = GateAhead{end, compiles_ - fresh_, why != nullptr}; + if (ahead) *ahead = beside(end, why != nullptr); return waited; } void leave_submit(const GateAhead& ahead = {}) { @@ -686,9 +730,84 @@ class SubmitMutex { if (--submits_ == 0) gate_changed_.notify_all(); } + // On a device loss (VulkanContext::mark_device_lost, once): the creates in the driver, how long each has been + // there and whether submissions wait for it, and the verdict of each kind. + void report_creates_on_loss() noexcept { + if (!compile_gate_enabled()) return; + try { + std::lock_guard lock(gate_mutex_); + const std::int64_t now = diagnostic_ms(); + std::fprintf(stderr, "[rt] compile-gate: at the device loss (t=%.3f s): compute gate %s, graphics gate %s; " + "%zu pipeline create(s) in the driver\n", + double(now) / 1000.0, verdict_name(CreateKind::compute), verdict_name(CreateKind::graphics), + in_driver_.size()); + for (const auto& c : in_driver_) + std::fprintf(stderr, "[rt] compile-gate: %s create %s (%zu B SPIR-V, %s) on thread %u, in the driver " + "for %lld ms, %s\n", + kind_name(c.info.kind), c.info.name ? c.info.name : "-", c.info.bytes, + c.info.trigger ? c.info.trigger : "-", c.thread, static_cast(now - c.since_ms), + is_blocking(c) ? "submissions wait for it" : "submissions do not wait for it"); + } catch (...) { + } + } + private: + struct InDriver { + std::uint64_t seq; + CreateInfo info; + std::int64_t since_ms; + std::uint32_t thread; + }; + // With gate_mutex_ held. - bool gate_off() const { return verdict_ == GateVerdict::driver_does_not_hold; } + bool kind_off(CreateKind kind) const { + return verdict_[std::size_t(kind)] == GateVerdict::driver_does_not_hold; + } + // A create a submission waits for: of a kind whose gate is on, and either of a kind that holds submissions or not + // gone past at the limit or the budget. + bool is_blocking(const InDriver& c) const { + if (kind_off(c.info.kind)) return false; + return verdict_[std::size_t(c.info.kind)] == GateVerdict::driver_holds || c.seq > overdue_through_; + } + bool held_blocking() const { + for (const auto& c : in_driver_) + if (verdict_[std::size_t(c.info.kind)] == GateVerdict::driver_holds) return true; + return false; + } + const InDriver* first_blocking() const { + for (const auto& c : in_driver_) + if (is_blocking(c)) return &c; + return nullptr; + } + std::uint32_t blocking() const { + std::uint32_t n = 0; + for (const auto& c : in_driver_) n += is_blocking(c); + return n; + } + bool blocking_kinds_pending() const { + for (const auto& c : in_driver_) + if (is_blocking(c) && verdict_[std::size_t(c.info.kind)] != GateVerdict::pending) return false; + return true; + } + // The creates a submission entering the driver at `now` does not wait for (all of those in it: it got past the + // gate). + GateAhead beside(std::int64_t now, bool went_ahead) const { + GateAhead a; + if (in_driver_.empty()) return a; + a.since_ms = now; + a.last_create = last_create_; + a.creates = static_cast(in_driver_.size()); + for (const auto& c : in_driver_) a.kinds |= std::uint8_t(1u << unsigned(c.info.kind)); + a.went_ahead = went_ahead; + return a; + } + const char* verdict_name(CreateKind kind) const { + switch (verdict_[std::size_t(kind)]) { + case GateVerdict::driver_does_not_hold: return "off (the driver does not hold submissions)"; + case GateVerdict::driver_holds: return rearmed_[std::size_t(kind)] ? "on again (re-armed)" : "on (held probe)"; + default: return adaptive_ ? "on (no verdict yet)" : "on"; + } + } // With gate_mutex_ held: the submissions' past waits at the gate, for the probe budget. The last kRecentWaits // are kept; more in one window are short ones, so dropping the oldest of them costs little. @@ -708,39 +827,60 @@ class SubmitMutex { return sum; } - // With gate_mutex_ held, as a submission leaves the driver: what its time there says about the driver (see the - // gate's comment). The ones that went ahead are logged (the first 8), and the verdict once. + // With gate_mutex_ held, as a submission leaves the driver: what its time there says about the driver, for each + // kind with a create in the driver from before it entered until now (see the gate's comment). The ones that went + // ahead are logged (the first 8), each verdict once. void probe(const GateAhead& ahead) { const std::int64_t now = diagnostic_ms(); const std::int64_t took = now - ahead.since_ms; - const std::uint32_t still = compiles_ - fresh_; // creates not waited for, still in the driver + std::uint32_t still = 0; + std::uint8_t kinds = 0; // of the creates it entered beside, the kinds still in the driver + for (const auto& c : in_driver_) + if (c.seq <= ahead.last_create) { + ++still; + kinds |= std::uint8_t(1u << unsigned(c.info.kind)); + } + kinds &= ahead.kinds; if (ahead.went_ahead && logged_ahead_ < 8) { ++logged_ahead_; std::fprintf(stderr, "[rt] compile-gate: the submission that went ahead on thread %u was in the driver " "for %lld ms; %u of the %u create(s) it went past are still there\n", diagnostic_thread_id(), static_cast(took), still, ahead.creates); } - if (!adaptive_ || verdict_ != GateVerdict::pending || !still) return; + if (!adaptive_) return; + for (std::size_t k = 0; k < kKinds; ++k) + if (kinds & (1u << k)) judge(CreateKind(k), took, now); + } + void judge(CreateKind kind, std::int64_t took, std::int64_t now) { + const std::size_t k = std::size_t(kind); + GateVerdict& verdict = verdict_[k]; + if (verdict == GateVerdict::driver_holds) return; if (took >= kProbeHeldMs) { - verdict_ = GateVerdict::driver_holds; - std::fprintf(stderr, "[rt] compile-gate: a submission stayed %lld ms in the driver with %u create(s) still " - "in it at t=%.3f s: this driver may hold submissions behind creates, so the gate " - "stays on for the rest of the run\n", - static_cast(took), still, double(now) / 1000.0); + const bool was_off = verdict == GateVerdict::driver_does_not_hold; + verdict = GateVerdict::driver_holds; + rearmed_[k] = was_off; + std::fprintf(stderr, "[rt] compile-gate: a submission stayed %lld ms in the driver beside a %s create at " + "t=%.3f s: this driver may hold submissions behind %s creates, so the %s gate is %s " + "for the rest of the run\n", + static_cast(took), kind_name(kind), double(now) / 1000.0, kind_name(kind), + kind_name(kind), was_off ? "back on" : "on"); return; } - if (took >= kProbeQuickMs) return; // says nothing either way - if (quick_probes_++ == 0) first_quick_ms_ = now; - longest_quick_ms_ = (std::max)(longest_quick_ms_, took); - if (quick_probes_ < kQuickProbes || now - first_quick_ms_ < kProbeSpanMs) return; - verdict_ = GateVerdict::driver_does_not_hold; + if (verdict != GateVerdict::pending || took >= kProbeQuickMs) return; // off already, or says nothing + if (quick_probes_[k]++ == 0) first_quick_ms_[k] = now; + longest_quick_ms_[k] = (std::max)(longest_quick_ms_[k], took); + if (quick_probes_[k] < kQuickProbes || now - first_quick_ms_[k] < kProbeSpanMs) return; + verdict = GateVerdict::driver_does_not_hold; gate_changed_.notify_all(); // the submissions and creates waiting now go - std::fprintf(stderr, "[rt] compile-gate: off for the rest of the run at t=%.3f s: %u submissions entered the " - "driver with a create in it and came back in at most %lld ms (under %lld ms), the first " - "and last %lld ms apart, the create still there: this driver does not hold a submission " - "behind a create (LEONIDA_COMPILE_GATE=1 keeps the gate)\n", - double(now) / 1000.0, quick_probes_, static_cast(longest_quick_ms_), - static_cast(kProbeQuickMs), static_cast(now - first_quick_ms_)); + std::fprintf(stderr, "[rt] compile-gate: %s gate off at t=%.3f s: %u submissions entered the driver beside a " + "%s create and came back in at most %lld ms (under %lld ms), the first and last %lld ms " + "apart, the create still there: this driver does not hold a submission behind a %s " + "create. Still timed: one that stays %lld ms turns it back on (LEONIDA_COMPILE_GATE=1 " + "keeps the gate)\n", + kind_name(kind), double(now) / 1000.0, quick_probes_[k], kind_name(kind), + static_cast(longest_quick_ms_[k]), static_cast(kProbeQuickMs), + static_cast(now - first_quick_ms_[k]), kind_name(kind), + static_cast(kProbeHeldMs)); } void taken() { @@ -759,23 +899,21 @@ class SubmitMutex { } // With gate_mutex_ held. - // The creates a submission found in the driver: how many, and the compute one named last (graphics creates are - // not named). Names are ShaderLibrary strings, valid until device teardown. + // The creates a submission found in the driver: how many it waits for, and the first of them. struct GateWait { std::uint32_t thread; std::uint32_t creates; - const char* name; - const char* trigger; - std::size_t bytes; + CreateInfo first; }; void note_gate_wait(const char* how, const GateWait& what, std::int64_t waited) const { std::fprintf(stderr, "[rt] compile-gate: vkQueueSubmit on thread %u %s %lld ms for %u pipeline create(s) to " - "leave the driver (compute %s, %zu B SPIR-V, %s) at t=%.3f s; %llu such waits so far, " + "leave the driver (%s %s, %zu B SPIR-V, %s) at t=%.3f s; %llu such waits so far, " "%lld ms in all, longest %lld ms\n", - what.thread, how, static_cast(waited), what.creates, what.name ? what.name : "-", - what.name ? what.bytes : std::size_t{0}, what.name && what.trigger ? what.trigger : "-", - double(diagnostic_ms()) / 1000.0, static_cast(stats_.submits), - static_cast(stats_.submit_ms), static_cast(stats_.submit_max_ms)); + what.thread, how, static_cast(waited), what.creates, kind_name(what.first.kind), + what.first.name ? what.first.name : "-", what.first.bytes, + what.first.trigger ? what.first.trigger : "-", double(diagnostic_ms()) / 1000.0, + static_cast(stats_.submits), static_cast(stats_.submit_ms), + static_cast(stats_.submit_max_ms)); } std::mutex mutex_; @@ -789,8 +927,7 @@ class SubmitMutex { std::mutex gate_mutex_; // leaf: guards the gate counts and stats std::condition_variable gate_changed_; - std::uint32_t compiles_ = 0; // creates in the driver - std::uint32_t fresh_ = 0; // of those, the ones numbered after overdue_through_: what a submission waits for + std::vector in_driver_; // the creates in the driver now, oldest first std::uint64_t last_create_ = 0; // the number of the last create that entered std::uint64_t overdue_through_ = 0; // creates up to this number were in the driver when a submission went ahead std::uint32_t submits_ = 0; // submissions waiting for them or in the driver @@ -799,9 +936,12 @@ class SubmitMutex { bool adaptive_ = compile_gate_mode() == GateMode::adaptive; std::array, kRecentWaits> recent_waits_{}; // [start, end) ms std::size_t recent_count_ = 0; - GateVerdict verdict_ = GateVerdict::pending; - unsigned quick_probes_ = 0; - std::int64_t first_quick_ms_ = 0, longest_quick_ms_ = 0; + // Per CreateKind. + std::array verdict_{GateVerdict::pending, GateVerdict::pending}; + std::array rearmed_{}; + std::array quick_probes_{}; + std::array first_quick_ms_{}, longest_quick_ms_{}; + unsigned logged_held_waits_ = 0; std::atomic gate_waiter_{0}; GateStats stats_; unsigned logged_waits_ = 0; diff --git a/runtime/test/submit_mutex_test.cpp b/runtime/test/submit_mutex_test.cpp index 0cc38bc9c6..57ca4558c3 100644 --- a/runtime/test/submit_mutex_test.cpp +++ b/runtime/test/submit_mutex_test.cpp @@ -338,7 +338,7 @@ int gate_adapts() { EXPECT(passed < 50 && !fresh_ahead); } std::printf("compile gate: off after two quick probes a second apart, not after two within the second\n"); - // A held probe: the gate stays on, and quick probes after it change nothing. + // A held probe: the gate stays on, and the create is waited for again however long it takes. { SubmitMutex m; m.set_gate_adaptive(true); @@ -356,34 +356,29 @@ int gate_adapts() { std::this_thread::sleep_for(2100ms); } const Verdict held = m.gate_verdict(); - { const SubmitMutex::SubmitTurn turn(m); } - std::this_thread::sleep_for(1100ms); - { const SubmitMutex::SubmitTurn turn(m); } - const Verdict still = m.gate_verdict(); - // Still gating: a create that enters now is waited for. - m.set_gate_wait_limit_ms(5000); - std::atomic short_in{false}; - std::atomic short_left_at{0}; - std::thread short_create([&] { - const SubmitMutex::CompileTurn turn(m); - short_in = true; - std::this_thread::sleep_for(200ms); - short_left_at = now_ms(); + // The create it stayed beside is waited for again, past the limit (its kind holds submissions), until it + // leaves. + std::atomic released_at{0}; + std::thread releaser([&] { + std::this_thread::sleep_for(300ms); + released_at = now_ms(); + release = true; }); - const bool short_entered = eventually([&] { return short_in.load(); }); std::int64_t entered_at = 0; + bool ahead = true; { const SubmitMutex::SubmitTurn turn(m); entered_at = now_ms(); + ahead = turn.went_ahead(); } - release = true; + releaser.join(); create.join(); - short_create.join(); - EXPECT(entered && short_entered); + const Verdict still = m.gate_verdict(); + EXPECT(entered); EXPECT(held == Verdict::driver_holds && still == Verdict::driver_holds); - EXPECT(entered_at >= short_left_at.load()); + EXPECT(!ahead && entered_at >= released_at.load()); } - std::printf("compile gate: a probe held 2.1 s keeps it on, later quick probes do not turn it off\n"); + std::printf("compile gate: a probe held 2.1 s keeps it on, and the create is then waited out past the limit\n"); return 0; } @@ -465,6 +460,8 @@ int gate_probe_budget() { std::this_thread::sleep_for(2100ms); } const Verdict held = m.gate_verdict(); + release = true; // the create it stayed beside leaves: it would be waited for again now + create.join(); std::atomic short_in{false}; std::atomic short_left_at{0}; std::thread short_create([&] { @@ -481,8 +478,6 @@ int gate_probe_budget() { entered_at = now_ms(); later_ahead = turn.went_ahead(); } - release = true; - create.join(); short_create.join(); EXPECT(entered && short_entered && ahead); EXPECT(held == Verdict::driver_holds); @@ -492,13 +487,91 @@ int gate_probe_budget() { return 0; } +int gate_per_kind() { + if (!SubmitMutex::compile_gate_enabled()) { + std::printf("compile gate off (LEONIDA_COMPILE_GATE=0): per-kind verdicts not checked\n"); + return 0; + } + using Verdict = SubmitMutex::GateVerdict; + using Kind = SubmitMutex::CreateKind; + SubmitMutex m; + m.set_gate_adaptive(true); + m.set_gate_wait_limit_ms(100); + m.set_gate_probe_budget_ms(0); + const SubmitMutex::CreateInfo compute{Kind::compute, "test compute", "test", 1000}; + const SubmitMutex::CreateInfo graphics{Kind::graphics, "test mesh", "test", 2000}; + // A compute create that stays until let go; the compute gate goes off after two quick probes a second apart. + std::atomic c_in{false}, c_release{false}; + std::atomic c_left_at{0}; + std::thread c([&] { + const SubmitMutex::CompileTurn turn(m, compute); + c_in = true; + while (!c_release) std::this_thread::sleep_for(1ms); + c_left_at = now_ms(); + }); + const bool c_entered = eventually([&] { return c_in.load(); }); + { const SubmitMutex::SubmitTurn turn(m); } // goes ahead at the limit: the first quick probe + std::this_thread::sleep_for(1100ms); + { const SubmitMutex::SubmitTurn turn(m); } // the second + const Verdict compute_off = m.gate_verdict(Kind::compute), graphics_before = m.gate_verdict(Kind::graphics); + // A graphics create is still waited for: its kind has no verdict, so the submission goes ahead only at the limit. + std::atomic g_in{false}, g_release{false}; + std::thread g([&] { + const SubmitMutex::CompileTurn turn(m, graphics); + g_in = true; + while (!g_release) std::this_thread::sleep_for(1ms); + }); + const bool g_entered = eventually([&] { return g_in.load(); }); + std::int64_t g_waited = 0; + bool g_ahead = false; + { + const std::int64_t start = now_ms(); + const SubmitMutex::SubmitTurn turn(m); + g_waited = now_ms() - start; + g_ahead = turn.went_ahead(); + } + g_release = true; + g.join(); + const Verdict graphics_after = m.gate_verdict(Kind::graphics); // one quick probe: still pending + // Re-arm: beside the compute create (gate off), a submission stays 2.1 s in the "driver". + { const SubmitMutex::SubmitTurn turn(m); std::this_thread::sleep_for(2100ms); } + const Verdict compute_rearmed = m.gate_verdict(Kind::compute); + // The compute create is waited for again, and not gone past at the limit: its kind holds submissions. + std::atomic released_at{0}; + std::thread releaser([&] { + std::this_thread::sleep_for(400ms); + released_at = now_ms(); + c_release = true; + }); + std::int64_t entered_at = 0; + bool after_ahead = true; + { + const SubmitMutex::SubmitTurn turn(m); + entered_at = now_ms(); + after_ahead = turn.went_ahead(); + } + releaser.join(); + c.join(); + EXPECT(c_entered && g_entered); + EXPECT(compute_off == Verdict::driver_does_not_hold && graphics_before == Verdict::pending); + EXPECT(g_ahead && g_waited >= 90 && g_waited < 1000); + EXPECT(graphics_after == Verdict::pending); + EXPECT(compute_rearmed == Verdict::driver_holds); + EXPECT(!after_ahead && entered_at >= c_left_at.load() && entered_at >= released_at.load()); + m.report_creates_on_loss(); // nothing in the driver now: the verdicts only + std::printf("compile gate: compute off with graphics still gated; a 2.1 s submission re-armed compute, and the next " + "one waited %lld ms past the 100 ms limit for the compute create\n", + static_cast(entered_at - (released_at.load() - 400))); + return 0; +} + } // namespace int main() { std::setvbuf(stdout, nullptr, _IONBF, 0); if (owner_tracking() || waits_for_holder() || waits_for_queued_submitter() || handoff_is_capped() || compile_gate() || gate_wait_limit() || gate_adapts() || - gate_probe_budget()) + gate_probe_budget() || gate_per_kind()) return 1; std::printf("submit_mutex_test: all passed\n"); return 0; From e5d1a14681fa65de2d8b49b2efeeec62370ec8ee Mon Sep 17 00:00:00 2001 From: OreoFR <29048345+OreoFR@users.noreply.github.com> Date: Fri, 27 Nov 2026 02:20:24 +0100 Subject: [PATCH 5/8] compile gate: a held probe needs the create beside it to have just left A submission that stays 2 s or more in the driver turned its create kind's gate on (or back on). It now counts only if it came back within 500 ms after a create of that kind, there when it entered, left the driver: a submission held behind a create comes back when the create does. On the 4070 Laptop (621.40, 8 GB nearly full) a 36 s submission came back 18 s before the compute create beside it; counted as held, it re-armed the gate, which then kept the next submission 78 s behind compute creates. One that is slow with its creates still there is logged (the first 8) and judged nothing. Tests: the held-probe tests release the create 100 ms before the submission comes back; a new one checks that a slow submission with the create still there gives no held verdict. Co-Authored-By: Claude Opus 5.5 --- docs/d3d12_vulkan.md | 9 ++- runtime/src/vulkan_device_fault.h | 35 +++++++++++- runtime/test/submit_mutex_test.cpp | 90 +++++++++++++++++++++++------- 3 files changed, 111 insertions(+), 23 deletions(-) diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index a5831c2673..8e71cdd01e 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -2925,7 +2925,14 @@ submitting thread; at the maintainers' decision it was replaced by this one when `vkQueueSubmit` sat 54 s in the driver beside a 56 s mesh create (541 KB) and the device was lost. A probe counts for each kind that had a create in the driver from before the submission entered until it came back; a held one counts against all of them, since which create held it cannot be told. A kind that is off is still timed: a - submission that stays 2 s or more beside one of its creates turns that kind back on for the rest of the run. While + submission that stays 2 s or more beside one of its creates turns that kind back on for the rest of the run. A + held probe (pending or off) counts only if the submission came back within 500 ms after a create of that kind, + there when it entered, left the driver: a submission held behind a create comes back when the create does. On the + 4070 Laptop (621.40, 8 GB nearly full) a 36 s submission came back 18 s before the 58 s compute create beside it; + counted as held, it re-armed the gate, which then kept the next submission 78 s behind compute creates. A + synthetic probe on the same machine (one to three threads creating 2 MB graphics pipelines of 8-10 s each, with + and without `VK_NV_device_diagnostics_config` and a shared pipeline cache) never held a submission past 80 ms. + While a create of a kind that holds submissions is in the driver, no submission goes ahead, at the limit or the budget, and such a create is waited for even when one went past it before the verdict: the title then starves as long as the create lasts (its watchdog may end the run), but the device is kept. On a device loss the log names every create diff --git a/runtime/src/vulkan_device_fault.h b/runtime/src/vulkan_device_fault.h index 26e02bbb0a..0641c49f32 100644 --- a/runtime/src/vulkan_device_fault.h +++ b/runtime/src/vulkan_device_fault.h @@ -445,7 +445,11 @@ class SubmitMutex { // every kind whose gate is not off; the probe budget applies only while all of those are of kinds still pending. // A kind that is off is still timed: a submission that stays kProbeHeldMs or more beside one of its creates turns // that kind's gate back on for the rest of the run (logged once), so the submissions after it wait. The one that - // stayed cannot be helped. A submission never goes ahead (at the limit or the budget) while a create of a kind + // stayed cannot be helped. A held probe counts only if the submission came back within kHeldReturnMs after a + // create of that kind, there when it entered, left the driver: a submission held behind a create comes back when + // the create does. One that comes back slow with its creates still there was slow for another reason (on 621.40, + // a 36 s submission came back 18 s before the compute create beside it, and the 4070 Laptop's 8 GB were full); + // counting it re-armed the gate and starved the title for 78 s behind creates that were not holding anything. A submission never goes ahead (at the limit or the budget) while a create of a kind // that holds submissions is in the driver, and such creates are waited for even if one was gone past before the // verdict: on 621.40, with the gate off, a vkQueueSubmit sat 54 s in the driver beside a 56 s mesh create and the // device was lost; waiting here starves the title as long, but keeps the device. Graphics creates pass the gate @@ -455,6 +459,7 @@ class SubmitMutex { enum class GateVerdict { pending, driver_does_not_hold, driver_holds }; static constexpr std::int64_t kProbeQuickMs = 250; static constexpr std::int64_t kProbeHeldMs = 2000; + static constexpr std::int64_t kHeldReturnMs = 500; static constexpr std::int64_t kProbeSpanMs = 1000; static constexpr unsigned kQuickProbes = 2; static constexpr std::int64_t kProbeWindowMs = 10000; @@ -626,6 +631,9 @@ class SubmitMutex { std::lock_guard lock(gate_mutex_); for (auto it = in_driver_.begin(); it != in_driver_.end(); ++it) if (it->seq == seq) { + const std::size_t k = std::size_t(it->info.kind); + last_leave_ms_[k] = diagnostic_ms(); + last_leave_seq_[k] = seq; in_driver_.erase(it); break; } @@ -848,8 +856,25 @@ class SubmitMutex { diagnostic_thread_id(), static_cast(took), still, ahead.creates); } if (!adaptive_) return; - for (std::size_t k = 0; k < kKinds; ++k) - if (kinds & (1u << k)) judge(CreateKind(k), took, now); + for (std::size_t k = 0; k < kKinds; ++k) { + if (!(ahead.kinds & (1u << k))) continue; + if (took >= kProbeHeldMs) { + // Held behind a create of this kind only if one it entered beside has just left. + const bool left_just_before = last_leave_seq_[k] != 0 && last_leave_seq_[k] <= ahead.last_create && + last_leave_ms_[k] >= ahead.since_ms && + now - last_leave_ms_[k] <= kHeldReturnMs; + if (left_just_before) { + judge(CreateKind(k), took, now); + } else if (logged_not_held_++ < 8) { + std::fprintf(stderr, "[rt] compile-gate: a submission stayed %lld ms in the driver beside a %s " + "create but came back %s: not counted as held by it\n", + static_cast(took), kind_name(CreateKind(k)), + (kinds & (1u << k)) ? "with the create still there" : "long after the create left"); + } + } else if (kinds & (1u << k)) { + judge(CreateKind(k), took, now); + } + } } void judge(CreateKind kind, std::int64_t took, std::int64_t now) { const std::size_t k = std::size_t(kind); @@ -939,6 +964,10 @@ class SubmitMutex { // Per CreateKind. std::array verdict_{GateVerdict::pending, GateVerdict::pending}; std::array rearmed_{}; + // When the last create of each kind left the driver, and its number: a held probe needs one to have just left. + std::array last_leave_ms_{}; + std::array last_leave_seq_{}; + unsigned logged_not_held_ = 0; std::array quick_probes_{}; std::array first_quick_ms_{}, longest_quick_ms_{}; unsigned logged_held_waits_ = 0; diff --git a/runtime/test/submit_mutex_test.cpp b/runtime/test/submit_mutex_test.cpp index 57ca4558c3..69b7c8e1e6 100644 --- a/runtime/test/submit_mutex_test.cpp +++ b/runtime/test/submit_mutex_test.cpp @@ -338,26 +338,36 @@ int gate_adapts() { EXPECT(passed < 50 && !fresh_ahead); } std::printf("compile gate: off after two quick probes a second apart, not after two within the second\n"); - // A held probe: the gate stays on, and the create is waited for again however long it takes. + // A held probe: a submission that comes back just after the create beside it leaves. The gate stays on, and a + // later create is waited for however long it takes. { SubmitMutex m; m.set_gate_adaptive(true); m.set_gate_wait_limit_ms(100); m.set_gate_probe_budget_ms(0); // probes at the limit only: gate_probe_budget checks the budget - std::atomic in{false}, release{false}; - std::thread create([&] { + std::atomic first_in{false}, first_release{false}; + std::thread first([&] { const SubmitMutex::CompileTurn turn(m); - in = true; - while (!release) std::this_thread::sleep_for(1ms); + first_in = true; + while (!first_release) std::this_thread::sleep_for(1ms); }); - const bool entered = eventually([&] { return in.load(); }); + const bool first_entered = eventually([&] { return first_in.load(); }); { const SubmitMutex::SubmitTurn turn(m); // goes ahead, then stays "in the driver" for 2.1 s - std::this_thread::sleep_for(2100ms); + std::this_thread::sleep_for(2000ms); + first_release = true; // the create leaves 100 ms before the submission comes back + first.join(); + std::this_thread::sleep_for(100ms); } const Verdict held = m.gate_verdict(); - // The create it stayed beside is waited for again, past the limit (its kind holds submissions), until it - // leaves. + // A create of the same kind is waited for, past the limit (its kind holds submissions), until it leaves. + std::atomic in{false}, release{false}; + std::thread create([&] { + const SubmitMutex::CompileTurn turn(m); + in = true; + while (!release) std::this_thread::sleep_for(1ms); + }); + const bool entered = first_entered && eventually([&] { return in.load(); }); std::atomic released_at{0}; std::thread releaser([&] { std::this_thread::sleep_for(300ms); @@ -379,6 +389,30 @@ int gate_adapts() { EXPECT(!ahead && entered_at >= released_at.load()); } std::printf("compile gate: a probe held 2.1 s keeps it on, and the create is then waited out past the limit\n"); + // A slow submission that comes back with the create beside it still there was not held by it: no verdict. + { + SubmitMutex m; + m.set_gate_adaptive(true); + m.set_gate_wait_limit_ms(100); + m.set_gate_probe_budget_ms(0); + std::atomic in{false}, release{false}; + std::thread create([&] { + const SubmitMutex::CompileTurn turn(m); + in = true; + while (!release) std::this_thread::sleep_for(1ms); + }); + const bool entered = eventually([&] { return in.load(); }); + { + const SubmitMutex::SubmitTurn turn(m); // goes ahead at the limit, then stays 2.1 s + std::this_thread::sleep_for(2100ms); + } + const Verdict after = m.gate_verdict(); + release = true; + create.join(); + EXPECT(entered); + EXPECT(after != Verdict::driver_holds); + } + std::printf("compile gate: a 2.1 s submission that came back with the create still there is not a held probe\n"); return 0; } @@ -457,11 +491,12 @@ int gate_probe_budget() { { const SubmitMutex::SubmitTurn turn(m); // goes ahead at the budget, then is held 2.1 s ahead = turn.went_ahead(); - std::this_thread::sleep_for(2100ms); + std::this_thread::sleep_for(2000ms); + release = true; // the create it stayed beside leaves 100 ms before it comes back: a held probe + create.join(); + std::this_thread::sleep_for(100ms); } const Verdict held = m.gate_verdict(); - release = true; // the create it stayed beside leaves: it would be waited for again now - create.join(); std::atomic short_in{false}; std::atomic short_left_at{0}; std::thread short_create([&] { @@ -533,15 +568,32 @@ int gate_per_kind() { g_release = true; g.join(); const Verdict graphics_after = m.gate_verdict(Kind::graphics); // one quick probe: still pending - // Re-arm: beside the compute create (gate off), a submission stays 2.1 s in the "driver". - { const SubmitMutex::SubmitTurn turn(m); std::this_thread::sleep_for(2100ms); } + // A second compute create, there through the rest. + std::atomic c2_in{false}, c2_release{false}; + std::atomic c2_left_at{0}; + std::thread c2([&] { + const SubmitMutex::CompileTurn turn(m, compute); + c2_in = true; + while (!c2_release) std::this_thread::sleep_for(1ms); + c2_left_at = now_ms(); + }); + const bool c2_entered = eventually([&] { return c2_in.load(); }); + // Re-arm: beside the compute creates (gate off), a submission stays 2.1 s in the "driver" and comes back 100 ms + // after the first one leaves. + { + const SubmitMutex::SubmitTurn turn(m); + std::this_thread::sleep_for(2000ms); + c_release = true; + c.join(); + std::this_thread::sleep_for(100ms); + } const Verdict compute_rearmed = m.gate_verdict(Kind::compute); - // The compute create is waited for again, and not gone past at the limit: its kind holds submissions. + // The second compute create is waited for, and not gone past at the limit: its kind holds submissions. std::atomic released_at{0}; std::thread releaser([&] { std::this_thread::sleep_for(400ms); released_at = now_ms(); - c_release = true; + c2_release = true; }); std::int64_t entered_at = 0; bool after_ahead = true; @@ -551,13 +603,13 @@ int gate_per_kind() { after_ahead = turn.went_ahead(); } releaser.join(); - c.join(); - EXPECT(c_entered && g_entered); + c2.join(); + EXPECT(c_entered && c2_entered && g_entered); EXPECT(compute_off == Verdict::driver_does_not_hold && graphics_before == Verdict::pending); EXPECT(g_ahead && g_waited >= 90 && g_waited < 1000); EXPECT(graphics_after == Verdict::pending); EXPECT(compute_rearmed == Verdict::driver_holds); - EXPECT(!after_ahead && entered_at >= c_left_at.load() && entered_at >= released_at.load()); + EXPECT(!after_ahead && entered_at >= c2_left_at.load() && entered_at >= released_at.load()); m.report_creates_on_loss(); // nothing in the driver now: the verdicts only std::printf("compile gate: compute off with graphics still gated; a 2.1 s submission re-armed compute, and the next " "one waited %lld ms past the 100 ms limit for the compute create\n", From b2cccb953a4282b8b82e7f5266a0b9dc22fab68b Mon Sep 17 00:00:00 2001 From: OreoFR <29048345+OreoFR@users.noreply.github.com> Date: Fri, 27 Nov 2026 02:53:48 +0100 Subject: [PATCH 6/8] compile gate: probes from submissions only, a ring of recent leaves, and a held limit Review of #2412 (nicoco23, ecox982745): - A later create leaving hid the earlier one a held probe needs: last_leave_seq_ kept one leave per kind, so when A (there when the submission entered) and then B (entered after it) left just before the submission came back, B replaced A and the probe was not counted. The recent leaves are now a ring of 64, and any leave of the kind, of a create the submission entered beside, within 500 ms before it came back counts. - Presents and idle or semaphore waits passed the gate as probes: they can take seconds on their own while the GPU is busy, so a slow present could mark a kind as holding, and quick ones could turn a kind off. SubmitTurn takes `probe` (true by default); the presents, vkQueueWaitIdle, vkDeviceWaitIdle and vkWaitSemaphores turns pass false. vkQueueSubmit and vkQueueBindSparse probe. - A kind that holds submissions was waited for with no limit, so a false verdict brought back the unbounded gate for the rest of the run. It is now waited for up to LEONIDA_COMPILE_GATE_HELD_WAIT_MS (60 s; 0: no limit), after which the creates in the driver are gone past and not waited for again. The header comment that said a false alarm "only keeps the bounded gate" is rewritten. Tests: submit_mutex_test gains both finish orders of A and B, B alone with A still there, a 2.1 s present that judges nothing, and a holding kind gone past at a 400 ms held limit. All pass (MSVC, Windows 11). The docs tests pass. Co-Authored-By: Claude Opus 5.5 --- docs/d3d12_vulkan.md | 12 ++- runtime/README.md | 1 + runtime/shims/d3d12_vulkan_device.cpp | 4 +- runtime/shims/dxgi_swapchain.h | 2 +- runtime/src/vulkan_backend.h | 8 +- runtime/src/vulkan_device_fault.h | 93 ++++++++++++---- runtime/src/vulkan_present.h | 8 +- runtime/test/submit_mutex_test.cpp | 146 +++++++++++++++++++++++++- 8 files changed, 236 insertions(+), 38 deletions(-) diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 8e71cdd01e..6b2a826f8b 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -2932,10 +2932,14 @@ submitting thread; at the maintainers' decision it was replaced by this one when counted as held, it re-armed the gate, which then kept the next submission 78 s behind compute creates. A synthetic probe on the same machine (one to three threads creating 2 MB graphics pipelines of 8-10 s each, with and without `VK_NV_device_diagnostics_config` and a shared pipeline cache) never held a submission past 80 ms. - While - a create of a kind that holds submissions is in the driver, no submission goes ahead, at the limit or the budget, - and such a create is waited for even when one went past it before the verdict: the title then starves as long as - the create lasts (its watchdog may end the run), but the device is kept. On a device loss the log names every create + The recent leaves are kept in a ring, so a create that entered after the submission and left after the one it + entered beside does not hide that one. Only submissions (`vkQueueSubmit`, `vkQueueBindSparse`) probe: presents and + idle or semaphore waits pass the gate the same way but can take seconds on their own, so they judge nothing. While + a create of a kind that holds submissions is in the driver, no submission goes ahead at the limit or the budget, + and such a create is waited for even when one went past it before the verdict, up to the held limit + (`LEONIDA_COMPILE_GATE_HELD_WAIT_MS`, 60 s; 0 waits however long): then the creates in the driver are gone past + and the next submissions no longer wait for them. A false "holds" verdict so costs at most that per create, not the + rest of the run. On a device loss the log names every create in the driver (kind, program, SPIR-V size, thread, how long it had been there, whether submissions waited for it) and each kind's gate. Long graphics creates would leave the title's process only through the compile helper, which builds compute pipelines alone today. diff --git a/runtime/README.md b/runtime/README.md index a53d131186..221b7ead98 100644 --- a/runtime/README.md +++ b/runtime/README.md @@ -421,6 +421,7 @@ ones marked *diagnostic* only add log lines or exist to compare a run with and w | `LEONIDA_COMPUTE_WARMUP_LIVE_MAX_BYTES` | unset | ``: a compute warm-up of a module over n bytes of SPIR-V is not started while the title submits (a submission in the last second); the pipeline is built at its first use instead (docs/d3d12_vulkan.md, "Pipelines still being built"). Unset or `0`: every warm-up runs, ordered behind a submission's compiles by the large-compile gate. | | `LEONIDA_COMPILE_GATE` | adaptive | The compile gate (`SubmitMutex::CompileTurn`, `src/vulkan_device_fault.h`): a queue submission (or a present or device wait) does not enter the driver while a pipeline create is in it, and waits in the runtime instead, since on NVIDIA 630.20 a `vkQueueSubmit` that entered during a long `vkCreateComputePipelines` waited 10 s in the driver and lost the device. Creates pass together; one that arrives while a submission waits queues behind it. The wait is bounded by `LEONIDA_COMPILE_GATE_WAIT_MS`. Unset: adaptive. Each submission that enters the driver next to a create the gate went past (at that limit, or earlier at the probe budget, `LEONIDA_COMPILE_GATE_PROBE_MS`) is a probe; two that come back in under 250 ms, at least 1 s apart, with the create still in the driver turn the gate off for the rest of the run (NVIDIA 621.40: 0-11 ms), and one that stays 2 s or more keeps it on; both are logged once. The verdict is kept per kind of create, compute and graphics (mesh included; graphics creates pass the gate since then, only compute ones did before): on NVIDIA 621.40 submissions came back in 0-12 ms beside compute creates, but one sat 54 s in the driver beside a 56 s mesh create and the device was lost. A kind that is off is still timed, and a submission that stays 2 s beside one of its creates turns it back on. A submission never goes ahead, at the limit or the budget, beside a create of a kind whose verdict is that it holds submissions: it waits for that create however long it takes. On a device loss the log names the creates in the driver (kind, program, size, how long) and each kind's gate. `1`: always gate with no limit unless `LEONIDA_COMPILE_GATE_WAIT_MS` sets one, never probe or turn it off, and a compute pipeline build also holds the submit lock for its driver calls (`compute_driver_turn`, `shims/d3d12_vulkan_device.cpp`). `0`: no gate. | | `LEONIDA_COMPILE_GATE_WAIT_MS` | 15000 (0 with `LEONIDA_COMPILE_GATE=1`) | ``: how long a submission waits at the compile gate before it goes ahead anyway; the creates in the driver then hold no later submission back (creates that enter after it are waited for again), and the log says how long the submission then took in the driver. The waiter holds the submit lock, so every queue stops behind it, and the title's hang watchdog ends the process after 30 s (one create stayed in the driver for over 54 s on NVIDIA 621.40). 15 s keeps the 630.20 protection for creates of up to 25 s. The same with the watchdog kept kicked: nothing ends the run then, but the stops still cost the frames (an RTX 4060 run that way had 459 waits, 155 s in all, by t=202 s). `0`: no limit, and with `LEONIDA_COMPILE_GATE_PROBE_MS` unset no probes either: the gate waits however long the creates take and never adapts. | +| `LEONIDA_COMPILE_GATE_HELD_WAIT_MS` | 60000 | ``: with the adaptive gate, how long a submission waits for creates of a kind the gate found to hold submissions (`docs/d3d12_vulkan.md`) before it goes ahead anyway; those creates are then not waited for again. `0`: until they leave. | | `LEONIDA_COMPILE_GATE_PROBE_MS` | 2500 (0 with `LEONIDA_COMPILE_GATE_WAIT_MS=0`) | ``: the adaptive gate's probe budget. Until it knows whether the driver holds a submission behind a create, a submission also goes ahead once the submissions' waits at the gate in the last 10 s, its own included, add up to this; a single wait reaches it at 2.5 s. On NVIDIA 621.40 waits of 3-7 s, none near the 15 s limit, came back to back and kept the title's main loop from reporting for 30 s; on an RTX 4060 with the watchdog kept kicked, 459 waits of up to 2.9 s took 155 s by t=202 s. The cost on a driver that does hold submissions (630.20): the first probe enters the driver after 2.5 s of waiting and loses the device if the create stays 10 s more; the held verdict then stops the early probes. `0`: probes only at `LEONIDA_COMPILE_GATE_WAIT_MS`. Not used with `LEONIDA_COMPILE_GATE=1`. | | `LEONIDA_FAST_PIPELINES` | unset | `graphics`, `compute` or `all` (`1` = all) creates those pipelines with `VK_PIPELINE_CREATE_DISABLE_OPTIMIZATION_BIT` (`shims/d3d12_pipeline_compile.h`): slower shaders, much shorter builds. Part of the compute journal key, so optimized and fast builds keep separate files. | | `LEONIDA_GRAPHICS_COMPILE_WORKER` | on | A second compile worker that takes graphics pipeline builds only, so a long compute build does not hold them up (`shims/d3d12_pipeline_compile.h`). `0`: the one general worker builds everything. | diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index cb0f6e886d..a9f44017f0 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -14841,7 +14841,7 @@ class Device final // after it, so the generations flush() retires are free at once. { std::lock_guard submit(context.submit_mutex()); - const leonida::SubmitMutex::SubmitTurn turn(context.submit_mutex()); // the compile gate + const leonida::SubmitMutex::SubmitTurn turn(context.submit_mutex(), false); // the compile gate context.observe_result(context.vk.vkDeviceWaitIdle(state_.info.device), "vkDeviceWaitIdle"); } state_.bindless_idle_waits.fetch_add(1, std::memory_order_relaxed); @@ -15160,7 +15160,7 @@ class Device final if (state_.context) { { std::lock_guard lock(state_.context->submit_mutex()); - const leonida::SubmitMutex::SubmitTurn turn(state_.context->submit_mutex()); // the compile gate + const leonida::SubmitMutex::SubmitTurn turn(state_.context->submit_mutex(), false); // the compile gate state_.context->observe_result(state_.context->vk.vkDeviceWaitIdle(state_.info.device), "vkDeviceWaitIdle"); } diff --git a/runtime/shims/dxgi_swapchain.h b/runtime/shims/dxgi_swapchain.h index 477bc29b2e..87351a344d 100644 --- a/runtime/shims/dxgi_swapchain.h +++ b/runtime/shims/dxgi_swapchain.h @@ -220,7 +220,7 @@ class VulkanSwapchain { pi.pSwapchains = &swapchain_; pi.pImageIndices = &image_index; std::lock_guard lock(submit_mutex_); - const SubmitMutex::SubmitTurn turn(submit_mutex_); // a queue operation: the compile gate + const SubmitMutex::SubmitTurn turn(submit_mutex_, false); // a queue operation: the compile gate const VkResult r = observe(fn_.queue_present(queue_, &pi), "vkQueuePresentKHR (DXGI)"); return r == VK_SUCCESS || r == VK_SUBOPTIMAL_KHR; } diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h index 103cdaa930..18bcc07baa 100644 --- a/runtime/src/vulkan_backend.h +++ b/runtime/src/vulkan_backend.h @@ -1700,7 +1700,7 @@ class VulkanContext { ++host_texture_stats_.waits; // A device wait is a queue operation: it passes the compile gate, so it never waits in the driver behind a // long pipeline create (the starved wait that loses the device after 10 s). - const SubmitMutex::SubmitTurn turn(submit_mutex_); + const SubmitMutex::SubmitTurn turn(submit_mutex_, false); const auto result = wait_submitted_locked(kTitleWriteWaitNs, "vkWaitSemaphores (title texture upload)"); // Not done in time: VK_NOT_READY, so the caller takes its staged path (ordered on the queue) or tries // again later, and the skip is not counted as a failed fill. @@ -1722,7 +1722,7 @@ class VulkanContext { LayoutPieces pieces; if (!tracker_.pieces(id, range, pieces) || pieces.empty()) return VK_ERROR_UNKNOWN; if (image_work_pending_) { - const SubmitMutex::SubmitTurn turn(submit_mutex_); // a queue operation: the compile gate (as above) + const SubmitMutex::SubmitTurn turn(submit_mutex_, false); // a queue operation: the compile gate (as above) const auto r = wait_submitted_locked(kTitleWriteWaitNs, "vkWaitSemaphores (title staging)"); if (r != VK_SUCCESS) return r; image_work_pending_ = false; @@ -2615,7 +2615,7 @@ class VulkanQueue : private QueueHolds::Holder { context_.holds().pump_ready(); // presents queued behind what was just dropped can run if (queue_) { std::lock_guard lock(context_.submit_mutex()); - const SubmitMutex::SubmitTurn turn(context_.submit_mutex()); // a queue operation: the compile gate + const SubmitMutex::SubmitTurn turn(context_.submit_mutex(), false); // a queue operation: the compile gate for (; open_labels_; --open_labels_) context_.labels_.queue_end(queue_); const VkResult idle_result = context_.observe_result(vk.vkQueueWaitIdle(queue_), "vkQueueWaitIdle"); if (idle_result == VK_SUCCESS) { @@ -2904,7 +2904,7 @@ class VulkanQueue : private QueueHolds::Holder { bool idle() { flush_async(); std::lock_guard lock(context_.submit_mutex()); - const SubmitMutex::SubmitTurn turn(context_.submit_mutex()); // a queue operation: the compile gate + const SubmitMutex::SubmitTurn turn(context_.submit_mutex(), false); // a queue operation: the compile gate return context_.observe_result(context_.vk.vkQueueWaitIdle(queue_), "vkQueueWaitIdle") == VK_SUCCESS; } diff --git a/runtime/src/vulkan_device_fault.h b/runtime/src/vulkan_device_fault.h index 0641c49f32..f9f0cb8021 100644 --- a/runtime/src/vulkan_device_fault.h +++ b/runtime/src/vulkan_device_fault.h @@ -420,8 +420,9 @@ class SubmitMutex { // let_submit_finish), so one submission could slip in between and come back quickly from a driver that does hold // submissions; a second one a second later must be as lucky. It costs no extra wait: once one has gone ahead, // the next ones enter at once while that create is still in the driver. One held probe keeps the gate on for the - // rest of the run (the turn also covers presents and idle waits, which can take that long on their own, so a held - // probe may be a false alarm; it only keeps the bounded gate, never less protection). LEONIDA_COMPILE_GATE=1 gates + // rest of the run. Only submissions (vkQueueSubmit, vkQueueBindSparse) probe: presents and idle or semaphore waits + // pass the gate as submissions do, but take seconds on their own while the GPU is busy, so their time in the driver + // judges nothing. LEONIDA_COMPILE_GATE=1 gates // without probing, LEONIDA_COMPILE_GATE=0 turns the gate off. let_submit_finish is unaffected by all of this. // // The first probes come early: while no verdict is in, a submission also goes ahead once the submissions' waits at @@ -481,6 +482,7 @@ class SubmitMutex { std::uint32_t creates = 0; // such creates in the driver then std::uint8_t kinds = 0; // their kinds, bit per CreateKind bool went_ahead = false; // it waited at the gate up to the limit or the probe budget + bool probe = true; // its time in the driver is a probe (a submission, not a present or a wait) }; class CompileTurn { // around vkCreate*Pipelines public: @@ -498,7 +500,12 @@ class SubmitMutex { }; class SubmitTurn { // around vkQueueSubmit, with the submit lock held public: - explicit SubmitTurn(SubmitMutex& gate) : gate_(gate) { gate.enter_submit(&ahead_); } + // `probe` false for a present or an idle or semaphore wait: it waits at the gate like a submission, but its + // time in the driver judges nothing (it can take seconds on its own while the GPU is busy). + explicit SubmitTurn(SubmitMutex& gate, bool probe = true) : gate_(gate) { + gate.enter_submit(&ahead_); + ahead_.probe = probe; + } ~SubmitTurn() { gate_.leave_submit(ahead_); } SubmitTurn(const SubmitTurn&) = delete; SubmitTurn& operator=(const SubmitTurn&) = delete; @@ -566,6 +573,28 @@ class SubmitMutex { } static std::int64_t default_gate_wait_limit_ms() { return gate_defaults().wait_ms; } static std::int64_t default_gate_probe_budget_ms() { return gate_defaults().probe_ms; } + // How long a submission waits for creates of a kind that holds submissions before it goes ahead anyway + // (LEONIDA_COMPILE_GATE_HELD_WAIT_MS, 0: until they leave). Longer than the plain limit: going ahead there means + // waiting in the driver, where the device was lost once (a 54 s submission beside a 56 s mesh create on 621.40, + // the gate off); a false "holds" verdict costs at most this per create instead of the rest of the run. + static constexpr std::int64_t kDefaultHeldWaitMs = 60000; + static std::int64_t default_gate_held_limit_ms() { + static const std::int64_t ms = [] { + const char* value = std::getenv("LEONIDA_COMPILE_GATE_HELD_WAIT_MS"); + if (!value || !*value) return kDefaultHeldWaitMs; + char* end = nullptr; + const long long n = std::strtoll(value, &end, 10); + if (end && !*end && n >= 0) return std::int64_t{n}; + std::fprintf(stderr, "[rt] compile-gate: LEONIDA_COMPILE_GATE_HELD_WAIT_MS=%s is not a number of " + "milliseconds; ignored\n", value); + return kDefaultHeldWaitMs; + }(); + return ms; + } + void set_gate_held_limit_ms(std::int64_t ms) { // for tests + std::lock_guard lock(gate_mutex_); + held_limit_ms_ = (std::max)(ms, std::int64_t{0}); + } std::int64_t gate_wait_limit_ms() { std::lock_guard lock(gate_mutex_); return wait_limit_ms_; @@ -631,9 +660,7 @@ class SubmitMutex { std::lock_guard lock(gate_mutex_); for (auto it = in_driver_.begin(); it != in_driver_.end(); ++it) if (it->seq == seq) { - const std::size_t k = std::size_t(it->info.kind); - last_leave_ms_[k] = diagnostic_ms(); - last_leave_seq_[k] = seq; + recent_leaves_[leave_count_++ % kRecentLeaves] = Leave{seq, diagnostic_ms(), it->info.kind}; in_driver_.erase(it); break; } @@ -669,21 +696,24 @@ class SubmitMutex { } else if (budget > 0 && (window_ms = recent_waits_ms(now) + waited) >= budget) { why = "probe budget"; } - if (why && held_blocking()) { - // Never into the driver beside a create of a kind that holds submissions: there it would wait for - // the create anyway, and the driver lost the device under such waits (621.40, a mesh create). It - // waits here for those, however long. + if (why && why[0] != 'h' && held_blocking()) { + // Not into the driver beside a create of a kind that holds submissions: there it would wait for the + // create anyway, and the driver lost the device under such waits (621.40, a mesh create). It waits + // here for those, up to the held limit (LEONIDA_COMPILE_GATE_HELD_WAIT_MS; 0: however long). if (logged_held_waits_++ < 8) std::fprintf(stderr, "[rt] compile-gate: vkQueueSubmit on thread %u reached the %s after %lld ms, " "but a create of a kind that holds submissions is in the driver: it waits " - "until that create leaves\n", - self, why, static_cast(waited)); + "until that create leaves or %lld ms have passed\n", + self, why, static_cast(waited), static_cast(held_limit_ms_)); why = nullptr; - limit = budget = 0; + budget = 0; + limit = held_limit_ms_; + if (limit > 0 && waited >= limit) why = "held limit"; } if (why) { // Every create in the driver now stops holding submissions back; later ones are waited for. overdue_through_ = last_create_; + if (why[0] == 'h') held_overdue_through_ = last_create_; gate_changed_.notify_all(); break; } @@ -717,6 +747,11 @@ class SubmitMutex { "create(s) still in the driver hold no submission back from now on (%llu " "submissions went ahead so far)\n", static_cast(limit), creates, static_cast(stats_.ahead)); + else if (why[0] == 'h') + std::fprintf(stderr, "[rt] compile-gate: at the %lld ms held limit (LEONIDA_COMPILE_GATE_HELD_WAIT_MS): " + "the %u create(s) still in the driver hold no submission back from now on, those " + "of a kind that holds submissions included (%llu submissions went ahead so far)\n", + static_cast(limit), creates, static_cast(stats_.ahead)); else std::fprintf(stderr, "[rt] compile-gate: the waits of the last %lld s add up to %lld ms, the probe " "budget is %lld ms (LEONIDA_COMPILE_GATE_PROBE_MS): the %u create(s) still in the " @@ -734,7 +769,7 @@ class SubmitMutex { void leave_submit(const GateAhead& ahead = {}) { if (!compile_gate_enabled()) return; std::lock_guard lock(gate_mutex_); - if (ahead.since_ms >= 0) probe(ahead); + if (ahead.since_ms >= 0 && ahead.probe) probe(ahead); if (--submits_ == 0) gate_changed_.notify_all(); } @@ -775,11 +810,13 @@ class SubmitMutex { // gone past at the limit or the budget. bool is_blocking(const InDriver& c) const { if (kind_off(c.info.kind)) return false; - return verdict_[std::size_t(c.info.kind)] == GateVerdict::driver_holds || c.seq > overdue_through_; + if (verdict_[std::size_t(c.info.kind)] == GateVerdict::driver_holds) return c.seq > held_overdue_through_; + return c.seq > overdue_through_; } bool held_blocking() const { for (const auto& c : in_driver_) - if (verdict_[std::size_t(c.info.kind)] == GateVerdict::driver_holds) return true; + if (verdict_[std::size_t(c.info.kind)] == GateVerdict::driver_holds && c.seq > held_overdue_through_) + return true; return false; } const InDriver* first_blocking() const { @@ -860,9 +897,12 @@ class SubmitMutex { if (!(ahead.kinds & (1u << k))) continue; if (took >= kProbeHeldMs) { // Held behind a create of this kind only if one it entered beside has just left. - const bool left_just_before = last_leave_seq_[k] != 0 && last_leave_seq_[k] <= ahead.last_create && - last_leave_ms_[k] >= ahead.since_ms && - now - last_leave_ms_[k] <= kHeldReturnMs; + bool left_just_before = false; + for (std::size_t i = 0; i < (std::min)(leave_count_, kRecentLeaves) && !left_just_before; ++i) { + const Leave& l = recent_leaves_[i]; + left_just_before = std::size_t(l.kind) == k && l.seq <= ahead.last_create && + l.ms >= ahead.since_ms && now - l.ms <= kHeldReturnMs; + } if (left_just_before) { judge(CreateKind(k), took, now); } else if (logged_not_held_++ < 8) { @@ -955,6 +995,8 @@ class SubmitMutex { std::vector in_driver_; // the creates in the driver now, oldest first std::uint64_t last_create_ = 0; // the number of the last create that entered std::uint64_t overdue_through_ = 0; // creates up to this number were in the driver when a submission went ahead + std::uint64_t held_overdue_through_ = 0; // the same, for creates of a kind that holds (at the held limit) + std::int64_t held_limit_ms_ = default_gate_held_limit_ms(); // 0: no limit std::uint32_t submits_ = 0; // submissions waiting for them or in the driver std::int64_t wait_limit_ms_ = default_gate_wait_limit_ms(); // 0: no limit std::int64_t probe_budget_ms_ = default_gate_probe_budget_ms(); // 0: no early probes @@ -964,9 +1006,16 @@ class SubmitMutex { // Per CreateKind. std::array verdict_{GateVerdict::pending, GateVerdict::pending}; std::array rearmed_{}; - // When the last create of each kind left the driver, and its number: a held probe needs one to have just left. - std::array last_leave_ms_{}; - std::array last_leave_seq_{}; + // The last creates to leave the driver: a held probe needs one it entered beside to have just left. A ring, so a + // later create leaving does not hide an earlier one that did. + struct Leave { + std::uint64_t seq = 0; + std::int64_t ms = 0; + CreateKind kind{}; + }; + static constexpr std::size_t kRecentLeaves = 64; + std::array recent_leaves_{}; + std::size_t leave_count_ = 0; unsigned logged_not_held_ = 0; std::array quick_probes_{}; std::array first_quick_ms_{}, longest_quick_ms_{}; diff --git a/runtime/src/vulkan_present.h b/runtime/src/vulkan_present.h index 81f3b57e1d..3c81d5920c 100644 --- a/runtime/src/vulkan_present.h +++ b/runtime/src/vulkan_present.h @@ -212,7 +212,7 @@ class VulkanPresenter { const VkDevice device = context_.device(); { std::lock_guard lock(context_.submit_mutex()); - const SubmitMutex::SubmitTurn turn(context_.submit_mutex()); // a queue operation: the compile gate + const SubmitMutex::SubmitTurn turn(context_.submit_mutex(), false); // a queue operation: the compile gate context_.observe_result(vk.vkQueueWaitIdle(queue_), "vkQueueWaitIdle (present)"); // A WSI layer may perform a device-wide idle while destroying the swapchain. // Keep that implicit idle serialized with submits from every context queue. @@ -1083,7 +1083,7 @@ class VulkanPresenter { const VkDevice device = context_.device(); { std::lock_guard lock(context_.submit_mutex()); - const SubmitMutex::SubmitTurn turn(context_.submit_mutex()); // a queue operation: the compile gate + const SubmitMutex::SubmitTurn turn(context_.submit_mutex(), false); // a queue operation: the compile gate // The old swapchain's images and semaphores may still be in use. context_.observe_result(vk.vkQueueWaitIdle(queue_), "vkQueueWaitIdle (present)"); release(swapchain_); @@ -1174,7 +1174,7 @@ class VulkanPresenter { return true; { std::lock_guard lock(context_.submit_mutex()); - const SubmitMutex::SubmitTurn turn(context_.submit_mutex()); // a queue operation: the compile gate + const SubmitMutex::SubmitTurn turn(context_.submit_mutex(), false); // a queue operation: the compile gate // The old target may still be in use. context_.observe_result(context_.vk.vkQueueWaitIdle(queue_), "vkQueueWaitIdle (present)"); } @@ -1531,7 +1531,7 @@ class VulkanPresenter { const VkDevice device = context_.device(); { std::lock_guard lock(context_.submit_mutex()); - const SubmitMutex::SubmitTurn turn(context_.submit_mutex()); // a queue operation: the compile gate + const SubmitMutex::SubmitTurn turn(context_.submit_mutex(), false); // a queue operation: the compile gate // The old swapchain's images and semaphores may still be in use. context_.observe_result(vk.vkQueueWaitIdle(queue_), "vkQueueWaitIdle (present)"); } diff --git a/runtime/test/submit_mutex_test.cpp b/runtime/test/submit_mutex_test.cpp index 69b7c8e1e6..6b5663d748 100644 --- a/runtime/test/submit_mutex_test.cpp +++ b/runtime/test/submit_mutex_test.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -617,13 +618,156 @@ int gate_per_kind() { return 0; } +// A create that stays in the "driver" until `release` is set; `in` is set once it is there, `left_at` when it leaves. +struct HeldCreate { + std::atomic in{false}, release{false}; + std::atomic left_at{0}; + std::thread thread; + HeldCreate(SubmitMutex& m, const SubmitMutex::CreateInfo& info) { + thread = std::thread([&m, info, this] { + const SubmitMutex::CompileTurn turn(m, info); + in = true; + while (!release) std::this_thread::sleep_for(1ms); + left_at = now_ms(); + }); + } + void let_go() { + release = true; + if (thread.joinable()) thread.join(); + } + ~HeldCreate() { let_go(); } +}; + +// The cases raised in review of #2412: a later create leaving does not hide an earlier one, presents and waits do +// not probe, and a kind that holds submissions is waited for up to the held limit only. +int gate_review_cases() { + if (!SubmitMutex::compile_gate_enabled()) { + std::printf("compile gate off (LEONIDA_COMPILE_GATE=0): review cases not checked\n"); + return 0; + } + using Verdict = SubmitMutex::GateVerdict; + using Kind = SubmitMutex::CreateKind; + const SubmitMutex::CreateInfo compute{Kind::compute, "test compute", "test", 1000}; + const auto turn_compute_off = [&](SubmitMutex& m, HeldCreate& c) { + { const SubmitMutex::SubmitTurn turn(m); } + std::this_thread::sleep_for(1100ms); + { const SubmitMutex::SubmitTurn turn(m); } + return m.gate_verdict(Kind::compute) == Verdict::driver_does_not_hold && c.in.load(); + }; + // A, there when the submission entered, leaves; then B, which entered after it, leaves; then the submission comes + // back. A's leave still counts: re-armed. + for (const bool a_first : {true, false}) { + SubmitMutex m; + m.set_gate_adaptive(true); + m.set_gate_wait_limit_ms(100); + m.set_gate_probe_budget_ms(0); + HeldCreate a(m, compute); + const bool off = eventually([&] { return a.in.load(); }) && turn_compute_off(m, a); + std::unique_ptr b; + { + const SubmitMutex::SubmitTurn turn(m); + b = std::make_unique(m, compute); // enters after the submission did + EXPECT(eventually([&] { return b->in.load(); })); + std::this_thread::sleep_for(2000ms); + if (a_first) { + a.let_go(); + std::this_thread::sleep_for(30ms); + b->let_go(); + } else { + b->let_go(); + std::this_thread::sleep_for(30ms); + a.let_go(); + } + std::this_thread::sleep_for(50ms); + } + EXPECT(off); + EXPECT(m.gate_verdict(Kind::compute) == Verdict::driver_holds); + } + std::printf("compile gate: A then B and B then A leaving just before a 2.1 s submission both re-arm compute\n"); + // B alone leaves (it entered after the submission), A is still there: not held. + { + SubmitMutex m; + m.set_gate_adaptive(true); + m.set_gate_wait_limit_ms(100); + m.set_gate_probe_budget_ms(0); + HeldCreate a(m, compute); + const bool off = eventually([&] { return a.in.load(); }) && turn_compute_off(m, a); + { + const SubmitMutex::SubmitTurn turn(m); + HeldCreate b(m, compute); + EXPECT(eventually([&] { return b.in.load(); })); + std::this_thread::sleep_for(2000ms); + b.let_go(); + std::this_thread::sleep_for(50ms); + } + EXPECT(off); + EXPECT(m.gate_verdict(Kind::compute) == Verdict::driver_does_not_hold); + } + std::printf("compile gate: a create that entered after the submission leaving does not re-arm compute\n"); + // A present or wait (probe false) that comes back 2.1 s later, just after the create left, judges nothing. + { + SubmitMutex m; + m.set_gate_adaptive(true); + m.set_gate_wait_limit_ms(100); + m.set_gate_probe_budget_ms(0); + HeldCreate a(m, compute); + EXPECT(eventually([&] { return a.in.load(); })); + { + const SubmitMutex::SubmitTurn turn(m, false); + std::this_thread::sleep_for(2000ms); + a.let_go(); + std::this_thread::sleep_for(50ms); + } + EXPECT(m.gate_verdict(Kind::compute) == Verdict::pending); + } + std::printf("compile gate: a 2.1 s present or wait judges nothing\n"); + // A kind that holds is waited for up to the held limit, then gone past; the next submission does not wait for it. + { + SubmitMutex m; + m.set_gate_adaptive(true); + m.set_gate_wait_limit_ms(100); + m.set_gate_probe_budget_ms(0); + m.set_gate_held_limit_ms(400); + { + HeldCreate first(m, compute); + EXPECT(eventually([&] { return first.in.load(); })); + const SubmitMutex::SubmitTurn turn(m); + std::this_thread::sleep_for(2000ms); + first.let_go(); + std::this_thread::sleep_for(50ms); + } + const Verdict held = m.gate_verdict(Kind::compute); + HeldCreate stays(m, compute); + EXPECT(eventually([&] { return stays.in.load(); })); + std::int64_t waited = 0, next_waited = 0; + bool ahead = false; + { + const std::int64_t start = now_ms(); + const SubmitMutex::SubmitTurn turn(m); + waited = now_ms() - start; + ahead = turn.went_ahead(); + } + { + const std::int64_t start = now_ms(); + const SubmitMutex::SubmitTurn turn(m); + next_waited = now_ms() - start; + } + EXPECT(held == Verdict::driver_holds); + EXPECT(ahead && waited >= 390 && waited < 2000); + EXPECT(next_waited < 50); + std::printf("compile gate: with a holding kind a submission waited %lld ms (held limit 400 ms), the next %lld ms\n", + static_cast(waited), static_cast(next_waited)); + } + return 0; +} + } // namespace int main() { std::setvbuf(stdout, nullptr, _IONBF, 0); if (owner_tracking() || waits_for_holder() || waits_for_queued_submitter() || handoff_is_capped() || compile_gate() || gate_wait_limit() || gate_adapts() || - gate_probe_budget() || gate_per_kind()) + gate_probe_budget() || gate_per_kind() || gate_review_cases()) return 1; std::printf("submit_mutex_test: all passed\n"); return 0; From ee81c0e4a0de30ea61e342265a06a8a045498995 Mon Sep 17 00:00:00 2001 From: OreoFR <29048345+OreoFR@users.noreply.github.com> Date: Fri, 27 Nov 2026 12:20:11 +0100 Subject: [PATCH 7/8] compile gate: builds outside MSVC, and the classic gate stays the default - CreateKind, CreateInfo and GateAhead move out of SubmitMutex to namespace scope (CompileGateKind, CompileGateCreate, CompileGateAhead), with using aliases so SubmitMutex::CreateInfo and the rest still work: clang and GCC reject a `= {}` default argument of a nested class with default member initializers inside the enclosing class (review on #2412). - LEONIDA_COMPILE_GATE unset is the classic gate again, as on main: compute creates only, no limit, no probes; graphics creates do not pass it. The adaptive gate is LEONIDA_COMPILE_GATE=adaptive: on a driver that holds submissions (630.20) its first probe of a kind enters the driver after the 2.5 s probe budget and loses the device if that create stays 10 s more, which the classic gate waits out. `1` and `0` are unchanged. - submit_mutex_test: a classic-gate case (a graphics create holds nothing back, a 3 s compute create is waited out with no verdict), the adaptive cases opt into graphics gating; passes unset, with `adaptive` and with `1`. - README rows and docs/d3d12_vulkan.md say so; the "held verdict 2 s into the probe" wording is corrected (it comes when the probe returns, and the probe holds the submit lock until then). Co-Authored-By: Claude Opus 5.5 --- docs/d3d12_vulkan.md | 32 +++--- runtime/README.md | 6 +- runtime/shims/d3d12_vulkan_device.cpp | 11 +- runtime/src/vulkan_device_fault.h | 147 ++++++++++++++++---------- runtime/test/submit_mutex_test.cpp | 81 +++++++++++++- 5 files changed, 197 insertions(+), 80 deletions(-) diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 07f07116d1..f0ea2b920b 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -2899,26 +2899,28 @@ submitting thread; at the maintainers' decision it was replaced by this one when title submits (a submission in the last second, `src/pipeline_wait.h`); the pipeline is built at its first use instead. Opt-in: unset or 0, every warm-up runs, as on `main`, and `LargeModuleGate` (#1889) orders them behind a submission's compiles. Smaller warm-ups, and every warm-up before the first submission, run as before. -- **A bounded, adaptive compile gate** (`SubmitMutex::enter_submit`, `src/vulkan_device_fault.h`). The gate keeps a +- **A bounded, adaptive compile gate, opt-in** (`SubmitMutex::enter_submit`, `src/vulkan_device_fault.h`, + `LEONIDA_COMPILE_GATE=adaptive`). Unset, the gate stays the classic one: compute creates only, each waited for + however long it takes, no probes, as before. The adaptive gate is opt-in because of what it costs on a driver that + does hold submissions behind a create (630.20, below): no run of it there has been reported yet. The gate keeps a `vkQueueSubmit` out of the driver while a create is in it, since on NVIDIA 630.20 one that entered during a long `vkCreateComputePipelines` waited 10 s and lost the device. The waiting submission holds the submit lock, so all queues stop: on an RTX 4070 Laptop (NVIDIA 621.40) one compute create of a 2.3 MB module stayed in the driver for more than 54 s during the story load, the submission waited for all of it and the title's 30 s hang watchdog ended - the process; with `LEONIDA_COMPILE_GATE=0` the same build went past that point. A submission now waits at most - `LEONIDA_COMPILE_GATE_WAIT_MS` (15 s by default) and then goes ahead; the creates in the driver at that moment hold - no later submission back, and creates that enter after it are waited for again. On 630.20 that loses only creates - over 25 s (15 s here, then the driver's 10 s), which the watchdog ends anyway. + the process; with `LEONIDA_COMPILE_GATE=0` the same build went past that point. With the adaptive gate a + submission waits at most `LEONIDA_COMPILE_GATE_WAIT_MS` (15 s by default there) and then goes ahead; the creates in + the driver at that moment hold no later submission back, and creates that enter after it are waited for again. On + 630.20 that loses only creates over 25 s (15 s here, then the driver's 10 s), which the watchdog ends anyway. On the same laptop the three submissions that went ahead came back from the driver in 11, 1 and 0 ms with the create still there, and no device was lost; but consecutive long creates made submissions wait the full 15 s again - and again, and the title's watchdog still ended the run. So by default the gate adapts: every submission that + and again, and the title's watchdog still ended the run. So the adaptive gate adapts: every submission that enters the driver next to a create it no longer waits for is a probe. Two that come back in under 250 ms, at least 1 s apart, with that create still in the driver turn the gate off for the rest of the run (a driver that holds a submission behind a create holds it as long as the create stays, and 630.20 held one for the full 10 s). Two, and a second apart, because back-to-back creates take and release that driver's lock, so one submission can slip in between; the second costs no extra wait, since after one has gone ahead the next enter at once while the create is - still there. One probe that stays 2 s or more keeps the gate on for the rest of the run; the turn also covers - presents and idle waits, so it can be a false alarm, but it only keeps the bounded gate. Both verdicts are logged - once. `LEONIDA_COMPILE_GATE=1` gates always and never probes. + still there. One probe that stays 2 s or more keeps the gate on for the rest of the run (presents and idle waits do + not probe, below). Both verdicts are logged once. `LEONIDA_COMPILE_GATE=1` gates always and never probes. In the next run on that laptop the story load passed, but no wait reached 15 s, so no probe came: 80 submissions waited 3-7 s each (90 s in all, the longest 6.7 s), back to back, and the title's watchdog ended the run at 433 s. So until a verdict is in, a submission also goes ahead once the submissions' waits at the gate in the last 10 s, its @@ -2926,17 +2928,19 @@ submitting thread; at the maintainers' decision it was replaced by this one when window rather than a shorter limit per wait, because it also counts many short waits, and one wait reaches it as soon as a 2.5 s limit would. Before the verdict the gate then stops the queues for at most about a quarter of any 10 s, after a quick verdict not at all. On a driver that does hold submissions (630.20) the first probe now enters - the driver after 2.5 s of waiting instead of 15 s, and loses the device if the create stays there 10 s more: one - probe per run, since the held verdict, 2 s into that probe, ends the early probes and leaves only the per-wait - limit. The held threshold stays 2 s, well inside the 10 s such a driver gives before the loss. The limit and the + the driver after 2.5 s of waiting instead of 15 s, and loses the device if the create stays there 10 s more (the + classic gate waits that create out). One probe per kind and run: it holds the submit lock while it is in the + driver, so no other submission enters until it comes back, and it then gives the held verdict (2 s or more, back + within 500 ms of the create leaving), after which no submission goes ahead beside a create of that kind before the + held limit. The held threshold stays 2 s, well inside the 10 s such a driver gives before the loss. The limit and the budget apply with the title's watchdog kept kicked too (`LEONIDA_TITLE_WATCHDOG=0` and `LEONIDA_TITLE_WATCHDOG_KICK`): nothing ends the run then, but the stops still cost the frames, and an RTX 4060 run that way had 459 waits, 155 s in all and the longest 2.9 s, by t=202 s. Only `LEONIDA_COMPILE_GATE_WAIT_MS=0` (with no `LEONIDA_COMPILE_GATE_PROBE_MS`) or `LEONIDA_COMPILE_GATE=1` (with no `LEONIDA_COMPILE_GATE_WAIT_MS`) waits however long the creates take and never adapts. Tested by `submit_mutex` (no GPU); the probe budget is not yet measured in the title. - The verdict is kept per kind of create, compute and graphics (mesh pipelines included), and graphics creates now - pass the gate too (before, only compute builds did). On 621.40 every probe beside a compute create came back in + The verdict is kept per kind of create, compute and graphics (mesh pipelines included), and graphics creates + pass this gate and `LEONIDA_COMPILE_GATE=1` too (the classic gate, as before, only compute builds). On 621.40 every probe beside a compute create came back in 0-12 ms (and on an RTX 4060 with 610.88, 133 of them), but a run whose gate those probes had turned off lost the device 9 s later with a submission held 7.8 s beside a mesh create, and with `LEONIDA_COMPILE_GATE=0` a `vkQueueSubmit` sat 54 s in the driver beside a 56 s mesh create (541 KB) and the device was lost. A probe counts diff --git a/runtime/README.md b/runtime/README.md index 77e27bcc27..b885981fc3 100644 --- a/runtime/README.md +++ b/runtime/README.md @@ -421,10 +421,10 @@ ones marked *diagnostic* only add log lines or exist to compare a run with and w | `LEONIDA_TEXTURE_REFRESH_ASYNC` | unset | `1` moves the before-submission refresh of the title textures (`refresh_title_textures`) to a worker: a submission only wakes it, and fills by itself only the mapped textures that were never filled, so their first draw has their bytes. Persistent mappings are hashed whole on the worker every pass; the rest is swept 128 textures at a time. A texture changed since may be sampled a frame behind. | | `LEONIDA_SUBMIT_ASYNC` | `0` | ``: each queue's `ExecuteCommandLists`, `Signal`, `Wait` and markers run in call order on a worker of the queue's own (`src/vulkan_backend.h`), at most n batches queued; the caller of the next one waits for room. The replay, the before-submission refreshes and `vkQueueSubmit` then leave the title's thread. A batch that fails is reported as a held one is. `0`: on the caller's thread. | | `LEONIDA_COMPUTE_WARMUP_LIVE_MAX_BYTES` | unset | ``: a compute warm-up of a module over n bytes of SPIR-V is not started while the title submits (a submission in the last second); the pipeline is built at its first use instead (docs/d3d12_vulkan.md, "Pipelines still being built"). Unset or `0`: every warm-up runs, ordered behind a submission's compiles by the large-compile gate. | -| `LEONIDA_COMPILE_GATE` | adaptive | The compile gate (`SubmitMutex::CompileTurn`, `src/vulkan_device_fault.h`): a queue submission (or a present or device wait) does not enter the driver while a pipeline create is in it, and waits in the runtime instead, since on NVIDIA 630.20 a `vkQueueSubmit` that entered during a long `vkCreateComputePipelines` waited 10 s in the driver and lost the device. Creates pass together; one that arrives while a submission waits queues behind it. The wait is bounded by `LEONIDA_COMPILE_GATE_WAIT_MS`. Unset: adaptive. Each submission that enters the driver next to a create the gate went past (at that limit, or earlier at the probe budget, `LEONIDA_COMPILE_GATE_PROBE_MS`) is a probe; two that come back in under 250 ms, at least 1 s apart, with the create still in the driver turn the gate off for the rest of the run (NVIDIA 621.40: 0-11 ms), and one that stays 2 s or more keeps it on; both are logged once. The verdict is kept per kind of create, compute and graphics (mesh included; graphics creates pass the gate since then, only compute ones did before): on NVIDIA 621.40 submissions came back in 0-12 ms beside compute creates, but one sat 54 s in the driver beside a 56 s mesh create and the device was lost. A kind that is off is still timed, and a submission that stays 2 s beside one of its creates turns it back on. A submission never goes ahead, at the limit or the budget, beside a create of a kind whose verdict is that it holds submissions: it waits for that create however long it takes. On a device loss the log names the creates in the driver (kind, program, size, how long) and each kind's gate. `1`: always gate with no limit unless `LEONIDA_COMPILE_GATE_WAIT_MS` sets one, never probe or turn it off, and a compute pipeline build also holds the submit lock for its driver calls (`compute_driver_turn`, `shims/d3d12_vulkan_device.cpp`). `0`: no gate. | -| `LEONIDA_COMPILE_GATE_WAIT_MS` | 15000 (0 with `LEONIDA_COMPILE_GATE=1`) | ``: how long a submission waits at the compile gate before it goes ahead anyway; the creates in the driver then hold no later submission back (creates that enter after it are waited for again), and the log says how long the submission then took in the driver. The waiter holds the submit lock, so every queue stops behind it, and the title's hang watchdog ends the process after 30 s (one create stayed in the driver for over 54 s on NVIDIA 621.40). 15 s keeps the 630.20 protection for creates of up to 25 s. The same with the watchdog kept kicked: nothing ends the run then, but the stops still cost the frames (an RTX 4060 run that way had 459 waits, 155 s in all, by t=202 s). `0`: no limit, and with `LEONIDA_COMPILE_GATE_PROBE_MS` unset no probes either: the gate waits however long the creates take and never adapts. | +| `LEONIDA_COMPILE_GATE` | on (classic) | The compile gate (`SubmitMutex::CompileTurn`, `src/vulkan_device_fault.h`): a queue submission (or a present or device wait) does not enter the driver while a pipeline create is in it, and waits in the runtime instead, since on NVIDIA 630.20 a `vkQueueSubmit` that entered during a long `vkCreateComputePipelines` waited 10 s in the driver and lost the device. Creates pass together; one that arrives while a submission waits queues behind it. Unset: the classic gate, as before the adaptive one: compute creates only, waited for however long they take (`LEONIDA_COMPILE_GATE_WAIT_MS` can bound it), no probes. `adaptive`: the adaptive gate, opt-in until it has run on a driver that holds submissions (630.20), where its first probe of a kind enters the driver after the probe budget and loses the device if that create stays 10 s more. The adaptive gate's wait is bounded by `LEONIDA_COMPILE_GATE_WAIT_MS`. Each submission that enters the driver next to a create the gate went past (at that limit, or earlier at the probe budget, `LEONIDA_COMPILE_GATE_PROBE_MS`) is a probe; two that come back in under 250 ms, at least 1 s apart, with the create still in the driver turn the gate off for the rest of the run (NVIDIA 621.40: 0-11 ms), and one that stays 2 s or more keeps it on; both are logged once. The verdict is kept per kind of create, compute and graphics (mesh included; graphics creates pass the gate since then, only compute ones did before): on NVIDIA 621.40 submissions came back in 0-12 ms beside compute creates, but one sat 54 s in the driver beside a 56 s mesh create and the device was lost. A kind that is off is still timed, and a submission that stays 2 s beside one of its creates turns it back on. A submission never goes ahead, at the limit or the budget, beside a create of a kind whose verdict is that it holds submissions: it waits for that create up to `LEONIDA_COMPILE_GATE_HELD_WAIT_MS`. On a device loss the log names the creates in the driver (kind, program, size, how long) and each kind's gate. `1`: always gate, graphics creates too, with no limit unless `LEONIDA_COMPILE_GATE_WAIT_MS` sets one, never probe or turn it off, and a compute pipeline build also holds the submit lock for its driver calls (`compute_driver_turn`, `shims/d3d12_vulkan_device.cpp`). `0`: no gate. | +| `LEONIDA_COMPILE_GATE_WAIT_MS` | 0 (15000 with `LEONIDA_COMPILE_GATE=adaptive`) | ``: how long a submission waits at the compile gate before it goes ahead anyway; the creates in the driver then hold no later submission back (creates that enter after it are waited for again), and the log says how long the submission then took in the driver. The waiter holds the submit lock, so every queue stops behind it, and the title's hang watchdog ends the process after 30 s (one create stayed in the driver for over 54 s on NVIDIA 621.40). 15 s keeps the 630.20 protection for creates of up to 25 s. The same with the watchdog kept kicked: nothing ends the run then, but the stops still cost the frames (an RTX 4060 run that way had 459 waits, 155 s in all, by t=202 s). `0`: no limit, and with `LEONIDA_COMPILE_GATE_PROBE_MS` unset no probes either: the gate waits however long the creates take and never adapts. | | `LEONIDA_COMPILE_GATE_HELD_WAIT_MS` | 60000 | ``: with the adaptive gate, how long a submission waits for creates of a kind the gate found to hold submissions (`docs/d3d12_vulkan.md`) before it goes ahead anyway; those creates are then not waited for again. `0`: until they leave. | -| `LEONIDA_COMPILE_GATE_PROBE_MS` | 2500 (0 with `LEONIDA_COMPILE_GATE_WAIT_MS=0`) | ``: the adaptive gate's probe budget. Until it knows whether the driver holds a submission behind a create, a submission also goes ahead once the submissions' waits at the gate in the last 10 s, its own included, add up to this; a single wait reaches it at 2.5 s. On NVIDIA 621.40 waits of 3-7 s, none near the 15 s limit, came back to back and kept the title's main loop from reporting for 30 s; on an RTX 4060 with the watchdog kept kicked, 459 waits of up to 2.9 s took 155 s by t=202 s. The cost on a driver that does hold submissions (630.20): the first probe enters the driver after 2.5 s of waiting and loses the device if the create stays 10 s more; the held verdict then stops the early probes. `0`: probes only at `LEONIDA_COMPILE_GATE_WAIT_MS`. Not used with `LEONIDA_COMPILE_GATE=1`. | +| `LEONIDA_COMPILE_GATE_PROBE_MS` | 2500 with `LEONIDA_COMPILE_GATE=adaptive` (0 with `LEONIDA_COMPILE_GATE_WAIT_MS=0`) | ``: the adaptive gate's probe budget. Until it knows whether the driver holds a submission behind a create, a submission also goes ahead once the submissions' waits at the gate in the last 10 s, its own included, add up to this; a single wait reaches it at 2.5 s. On NVIDIA 621.40 waits of 3-7 s, none near the 15 s limit, came back to back and kept the title's main loop from reporting for 30 s; on an RTX 4060 with the watchdog kept kicked, 459 waits of up to 2.9 s took 155 s by t=202 s. The cost on a driver that does hold submissions (630.20): the first probe enters the driver after 2.5 s of waiting and loses the device if the create stays 10 s more; the held verdict then stops the early probes. `0`: probes only at `LEONIDA_COMPILE_GATE_WAIT_MS`. Used only with `LEONIDA_COMPILE_GATE=adaptive`. | | `LEONIDA_FAST_PIPELINES` | unset | `graphics`, `compute` or `all` (`1` = all) creates those pipelines with `VK_PIPELINE_CREATE_DISABLE_OPTIMIZATION_BIT` (`shims/d3d12_pipeline_compile.h`): slower shaders, much shorter builds. Part of the compute journal key, so optimized and fast builds keep separate files. | | `LEONIDA_GRAPHICS_COMPILE_WORKER` | on | A second compile worker that takes graphics pipeline builds only, so a long compute build does not hold them up (`shims/d3d12_pipeline_compile.h`). `0`: the one general worker builds everything. | | `LEONIDA_CONSOLE_HEAP_GPU` | unset | `1`: the V#s of the console descriptor heaps are rebased by a compute pass at the start of each submission (`src/console_heap_gpu.h`) instead of on the CPU. The CPU still finds the changed slots and registers the T#s; a V# the pass cannot answer is resolved after its submission and its slot queued again. | diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index e887555d12..6c8d9345c3 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -12904,15 +12904,18 @@ class PipelineState final const leonida::pipeline_profile::DriverCall profile(r.mesh ? "ms" : "vs", lazy_ ? "lazy" : "eager", program, fragment, unbound); // Through the compile gate as a graphics create (a mesh pipeline is one): on NVIDIA 621.40 a vkQueueSubmit - // sat in the driver for as long as a mesh create was in it (54 s) and lost the device. + // sat in the driver for as long as a mesh create was in it (54 s) and lost the device. Not with the classic + // gate (the default), which keeps graphics creates out of it as before. const char* gate_name = nullptr; for (const auto& source : sources_) if (source.entry && source.slot == 0) gate_name = source.entry->name.c_str(); VkResult result = VK_ERROR_UNKNOWN; { - const DriverTurn turn(device_.context->submit_mutex(), trigger, - r.mesh ? "vkCreateGraphicsPipelines (mesh)" : "vkCreateGraphicsPipelines", true, - gate_name, leonida::SubmitMutex::CreateKind::graphics, bytes); + std::optional turn; + if (leonida::SubmitMutex::compile_gate_graphics()) + turn.emplace(device_.context->submit_mutex(), trigger, + r.mesh ? "vkCreateGraphicsPipelines (mesh)" : "vkCreateGraphicsPipelines", true, gate_name, + leonida::SubmitMutex::CreateKind::graphics, bytes); result = device_.create_pipeline(flags, [&](VkPipelineCache with, VkPipelineCreateFlags f) { info.flags = f; pipeline = VK_NULL_HANDLE; diff --git a/runtime/src/vulkan_device_fault.h b/runtime/src/vulkan_device_fault.h index f9f0cb8021..2064d83a0e 100644 --- a/runtime/src/vulkan_device_fault.h +++ b/runtime/src/vulkan_device_fault.h @@ -309,6 +309,27 @@ struct DriverCompile { }; inline DriverCompile driver_compile; +// The compile gate's types, for SubmitMutex (which names them CreateKind, CreateInfo and GateAhead). At namespace +// scope, not nested: a default argument `= {}` of a nested class with default member initializers is rejected by +// clang and GCC inside the enclosing class (the member initializers are not parsed until that class is complete). +enum class CompileGateKind : std::uint8_t { compute, graphics }; // graphics: mesh pipelines too +// What a create is, for the verdicts and the reports. Names are ShaderLibrary strings, valid until device teardown +// (or literals). +struct CompileGateCreate { + CompileGateKind kind = CompileGateKind::compute; + const char* name = nullptr; + const char* trigger = nullptr; + std::size_t bytes = 0; // SPIR-V +}; +struct CompileGateAhead { // a submission that entered the driver alongside creates not waited for + std::int64_t since_ms = -1; // when it entered, or -1: no such create was in the driver + std::uint64_t last_create = 0; // the number of the last create that had entered then + std::uint32_t creates = 0; // such creates in the driver then + std::uint8_t kinds = 0; // their kinds, bit per CompileGateKind + bool went_ahead = false; // it waited at the gate up to the limit or the probe budget + bool probe = true; // its time in the driver is a probe (a submission, not a present or a wait) +}; + // VulkanContext's submit lock, which also remembers who holds it, since when and how many threads wait for it. A // submit or an idle wait blocked inside the driver holds it for as long as it blocks; the stall watchdog and the loss // report name that thread, and a hold of a second or more is logged when it ends. @@ -400,30 +421,33 @@ class SubmitMutex { // No deadlock: a create holds the gate only across its vkCreate*Pipelines and a submission only across its // vkQueueSubmit, and neither driver call waits for anything of ours. // - // The wait is bounded (gate_wait_limit_ms, LEONIDA_COMPILE_GATE_WAIT_MS): a submission waiting at the gate holds - // the submit lock, so every queue stops behind it, and the title's hang watchdog ends the process once its main - // loop has not reported for 30 s. One create stayed in the driver for more than 54 s (RTX 4070 Laptop, NVIDIA - // 621.40), and the gate turned it into that exit. Past the limit the submission goes ahead, and the creates that - // were in the driver then no longer hold any submission back; creates that enter later are waited for again. The - // default limit, 15 s, keeps the protection measured above for every create of up to 25 s: 630.20 gave the - // submission 10 s in the driver before the loss, and a stop much longer than 25 s ends at the watchdog anyway. - // The same with the watchdog kept kicked (LEONIDA_TITLE_WATCHDOG=0 with LEONIDA_TITLE_WATCHDOG_KICK): nothing ends - // the run then, but the stops still cost the frames (an RTX 4060 run that way had 459 waits, 155 s in all, by - // t=202 s). Only LEONIDA_COMPILE_GATE_WAIT_MS=0 waits for the creates however long they take. + // By default (classic) only compute creates pass the gate and a submission waits for them however long they take, + // as before the adaptive gate; LEONIDA_COMPILE_GATE_WAIT_MS bounds that too. With the adaptive gate + // (LEONIDA_COMPILE_GATE=adaptive) the wait is bounded (gate_wait_limit_ms, LEONIDA_COMPILE_GATE_WAIT_MS): a + // submission waiting at the gate holds the submit lock, so every queue stops behind it, and the title's hang + // watchdog ends the process once its main loop has not reported for 30 s. One create stayed in the driver for more + // than 54 s (RTX 4070 Laptop, NVIDIA 621.40), and the gate turned it into that exit. Past the limit the submission + // goes ahead, and the creates that were in the driver then no longer hold any submission back; creates that enter + // later are waited for again. The default limit, 15 s, keeps the protection measured above for every create of up + // to 25 s: 630.20 gave the submission 10 s in the driver before the loss, and a stop much longer than 25 s ends at + // the watchdog anyway. The same with the watchdog kept kicked (LEONIDA_TITLE_WATCHDOG=0 with + // LEONIDA_TITLE_WATCHDOG_KICK): nothing ends the run then, but the stops still cost the frames (an RTX 4060 run + // that way had 459 waits, 155 s in all, by t=202 s). Only LEONIDA_COMPILE_GATE_WAIT_MS=0 waits for the creates + // however long they take. // - // Adaptive (the default): each submission that enters the driver alongside a create the gate no longer waits for - // is a probe of whether this driver holds a submission behind a create. Back while that create is still in the - // driver: under kProbeQuickMs is quick, kProbeHeldMs or more is held, between says nothing. On 621.40 the three - // that went ahead came back in 0-11 ms, the create still there; the one 630.20 held stayed 10 s, until the loss. - // Two quick probes at least kProbeSpanMs apart turn the gate off for the rest of the run (the device's, which is - // the process's), logged once. Two because on 630.20 back-to-back creates take and release the driver's lock (see - // let_submit_finish), so one submission could slip in between and come back quickly from a driver that does hold - // submissions; a second one a second later must be as lucky. It costs no extra wait: once one has gone ahead, - // the next ones enter at once while that create is still in the driver. One held probe keeps the gate on for the - // rest of the run. Only submissions (vkQueueSubmit, vkQueueBindSparse) probe: presents and idle or semaphore waits - // pass the gate as submissions do, but take seconds on their own while the GPU is busy, so their time in the driver - // judges nothing. LEONIDA_COMPILE_GATE=1 gates - // without probing, LEONIDA_COMPILE_GATE=0 turns the gate off. let_submit_finish is unaffected by all of this. + // Adaptive (LEONIDA_COMPILE_GATE=adaptive, opt-in): each submission that enters the driver alongside a create the + // gate no longer waits for is a probe of whether this driver holds a submission behind a create. Back while that + // create is still in the driver: under kProbeQuickMs is quick, kProbeHeldMs or more is held, between says nothing. + // On 621.40 the three that went ahead came back in 0-11 ms, the create still there; the one 630.20 held stayed 10 + // s, until the loss. Two quick probes at least kProbeSpanMs apart turn the gate off for the rest of the run (the + // device's, which is the process's), logged once. Two because on 630.20 back-to-back creates take and release the + // driver's lock (see let_submit_finish), so one submission could slip in between and come back quickly from a + // driver that does hold submissions; a second one a second later must be as lucky. It costs no extra wait: once one + // has gone ahead, the next ones enter at once while that create is still in the driver. One held probe keeps the + // gate on for the rest of the run. Only submissions (vkQueueSubmit, vkQueueBindSparse) probe: presents and idle or + // semaphore waits pass the gate as submissions do, but take seconds on their own while the GPU is busy, so their + // time in the driver judges nothing. LEONIDA_COMPILE_GATE=1 gates without probing, LEONIDA_COMPILE_GATE=0 turns the + // gate off. let_submit_finish is unaffected by all of this. // // The first probes come early: while no verdict is in, a submission also goes ahead once the submissions' waits at // the gate in the last kProbeWindowMs, its own included, add up to the probe budget (gate_probe_budget_ms, @@ -433,8 +457,10 @@ class SubmitMutex { // would. Before the verdict the gate then stops the queues for at most a quarter of any 10 s; after a quick // verdict not at all. The price is on a driver that does hold submissions (630.20): the first probe enters the // driver after 2.5 s of waiting instead of 15, and loses the device if the create stays in the driver 10 s more. - // That is one probe per run, the held verdict comes 2 s into it, and from then on only the per-wait limit - // applies, so no submission enters next to a create early again. The alternative there is the same run starved + // That is one probe per kind and run: the probe holds the submit lock while it is in the driver, so no other + // submission enters until it comes back, and then (within 500 ms of the create leaving, 2 s or more) it gives the + // held verdict, after which no submission enters next to a create of that kind early again. This is why the + // adaptive gate is opt-in until it has run on such a driver. The alternative there is the same run starved // by waits like the ones above. LEONIDA_COMPILE_GATE_WAIT_MS=0 without LEONIDA_COMPILE_GATE_PROBE_MS also makes the // budget 0: the gate then waits however long the creates take and never probes, as LEONIDA_COMPILE_GATE=1 does. // @@ -454,9 +480,10 @@ class SubmitMutex { // that holds submissions is in the driver, and such creates are waited for even if one was gone past before the // verdict: on 621.40, with the gate off, a vkQueueSubmit sat 54 s in the driver beside a 56 s mesh create and the // device was lost; waiting here starves the title as long, but keeps the device. Graphics creates pass the gate - // since this split; before it only compute ones did. On a device loss, report_creates_on_loss names the creates - // in the driver and the verdicts. - enum class GateMode { off, adaptive, always }; + // since this split (adaptive and LEONIDA_COMPILE_GATE=1; the classic gate, as before, only compute ones). On a + // device loss, report_creates_on_loss names the creates in the driver and the verdicts. + // classic (the default): the gate as on main before the adaptive one, compute creates only, no limit, no probes. + enum class GateMode { off, classic, adaptive, always }; enum class GateVerdict { pending, driver_does_not_hold, driver_holds }; static constexpr std::int64_t kProbeQuickMs = 250; static constexpr std::int64_t kProbeHeldMs = 2000; @@ -465,25 +492,11 @@ class SubmitMutex { static constexpr unsigned kQuickProbes = 2; static constexpr std::int64_t kProbeWindowMs = 10000; static constexpr std::int64_t kDefaultProbeBudgetMs = 2500; - enum class CreateKind : std::uint8_t { compute, graphics }; // graphics: mesh pipelines too + using CreateKind = CompileGateKind; // graphics: mesh pipelines too static constexpr std::size_t kKinds = 2; static const char* kind_name(CreateKind kind) { return kind == CreateKind::compute ? "compute" : "graphics"; } - // What a create is, for the verdicts and the reports. Names are ShaderLibrary strings, valid until device - // teardown (or literals). - struct CreateInfo { - CreateKind kind = CreateKind::compute; - const char* name = nullptr; - const char* trigger = nullptr; - std::size_t bytes = 0; // SPIR-V - }; - struct GateAhead { // a submission that entered the driver alongside creates not waited for - std::int64_t since_ms = -1; // when it entered, or -1: no such create was in the driver - std::uint64_t last_create = 0; // the number of the last create that had entered then - std::uint32_t creates = 0; // such creates in the driver then - std::uint8_t kinds = 0; // their kinds, bit per CreateKind - bool went_ahead = false; // it waited at the gate up to the limit or the probe budget - bool probe = true; // its time in the driver is a probe (a submission, not a present or a wait) - }; + using CreateInfo = CompileGateCreate; + using GateAhead = CompileGateAhead; class CompileTurn { // around vkCreate*Pipelines public: explicit CompileTurn(SubmitMutex& gate, const CreateInfo& info = {}) @@ -517,18 +530,25 @@ class SubmitMutex { GateAhead ahead_; }; - // LEONIDA_COMPILE_GATE: 0 off, 1 always (and compute builds hold the submit lock, compute_driver_turn in - // shims/d3d12_vulkan_device.cpp), anything else adaptive. + // LEONIDA_COMPILE_GATE: 0 off, 1 always (both kinds, and compute builds hold the submit lock, compute_driver_turn + // in shims/d3d12_vulkan_device.cpp), `adaptive` the adaptive gate, anything else (unset) classic. Classic stays + // the default until the adaptive gate has run on a driver that holds submissions (630.20): there its first probe + // of a kind enters the driver after the probe budget and loses the device if the create stays 10 s more. static GateMode compile_gate_mode() { static const GateMode mode = [] { const char* value = std::getenv("LEONIDA_COMPILE_GATE"); if (value && !std::strcmp(value, "0")) return GateMode::off; if (value && !std::strcmp(value, "1")) return GateMode::always; - return GateMode::adaptive; + if (value && !std::strcmp(value, "adaptive")) return GateMode::adaptive; + return GateMode::classic; }(); return mode; } static bool compile_gate_enabled() { return compile_gate_mode() != GateMode::off; } + // Whether graphics (and mesh) creates pass the gate: not in the classic gate, where only compute ones did. + static bool compile_gate_graphics() { + return compile_gate_mode() == GateMode::adaptive || compile_gate_mode() == GateMode::always; + } // Read once. How long one submission waits at the gate before it goes ahead (0: until the creates leave): // LEONIDA_COMPILE_GATE_WAIT_MS=. The adaptive gate's probe budget over kProbeWindowMs (0: no early probes): @@ -550,20 +570,28 @@ class SubmitMutex { std::fprintf(stderr, "[rt] compile-gate: %s=%s is not a number of milliseconds; ignored\n", name, value); return fallback; }; - const bool always = compile_gate_mode() == GateMode::always; // the gate as before: no limit by default - const std::int64_t wait_ms = read("LEONIDA_COMPILE_GATE_WAIT_MS", always ? 0 : kDefaultGateWaitMs); + const GateMode mode = compile_gate_mode(); + const bool always = mode == GateMode::always; + const bool adaptive = mode == GateMode::adaptive; // classic and always: no limit by default + const std::int64_t wait_ms = read("LEONIDA_COMPILE_GATE_WAIT_MS", adaptive ? kDefaultGateWaitMs : 0); const GateDefaults d{wait_ms, read("LEONIDA_COMPILE_GATE_PROBE_MS", wait_ms ? kDefaultProbeBudgetMs : 0)}; - if (always && d.wait_ms) + if (mode == GateMode::classic && d.wait_ms) + std::fprintf(stderr, "[rt] compile-gate: classic (compute creates); a submission waits at most %lld ms " + "for them to leave the driver (LEONIDA_COMPILE_GATE_WAIT_MS; " + "LEONIDA_COMPILE_GATE=adaptive for the adaptive gate)\n", + static_cast(d.wait_ms)); + else if (always && d.wait_ms) std::fprintf(stderr, "[rt] compile-gate: always (LEONIDA_COMPILE_GATE=1); a submission waits at most " "%lld ms for pipeline creates to leave the driver (LEONIDA_COMPILE_GATE_WAIT_MS)\n", static_cast(d.wait_ms)); else if (always) std::fprintf(stderr, "[rt] compile-gate: always (LEONIDA_COMPILE_GATE=1); a submission waits for " "pipeline creates to leave the driver, however long they take\n"); - else if (compile_gate_mode() == GateMode::adaptive) - std::fprintf(stderr, "[rt] compile-gate: adaptive, off once submissions come back quickly with a create " - "in the driver; a submission goes ahead after %lld ms of waiting (0: no limit, " - "LEONIDA_COMPILE_GATE_WAIT_MS) or, until that is known, once the waits of the last " + else if (adaptive) + std::fprintf(stderr, "[rt] compile-gate: adaptive (LEONIDA_COMPILE_GATE=adaptive), off once " + "submissions come back quickly with a create in the driver; a submission goes " + "ahead after %lld ms of waiting (0: no limit, LEONIDA_COMPILE_GATE_WAIT_MS) or, " + "until that is known, once the waits of the last " "%lld s add up to %lld ms (0: never, LEONIDA_COMPILE_GATE_PROBE_MS)\n", static_cast(d.wait_ms), static_cast(kProbeWindowMs / 1000), static_cast(d.probe_ms)); @@ -615,6 +643,10 @@ class SubmitMutex { std::lock_guard lock(gate_mutex_); adaptive_ = on; } + void set_gate_graphics(bool on) { // for tests: whether graphics creates pass the gate + std::lock_guard lock(gate_mutex_); + graphics_ = on; + } GateVerdict gate_verdict(CreateKind kind = CreateKind::compute) { std::lock_guard lock(gate_mutex_); return verdict_[std::size_t(kind)]; @@ -640,6 +672,10 @@ class SubmitMutex { if (!compile_gate_enabled()) return 0; std::unique_lock lock(gate_mutex_); std::int64_t waited = 0; + if (info.kind == CreateKind::graphics && !graphics_) { // the classic gate: compute creates only + seq = 0; + return 0; + } // Not behind this thread's own submission (it holds the submit lock: none of its submissions is in the // driver now, and waiting for them would never end). const bool own = owner_.load(std::memory_order_relaxed) == diagnostic_thread_id(); @@ -1001,6 +1037,7 @@ class SubmitMutex { std::int64_t wait_limit_ms_ = default_gate_wait_limit_ms(); // 0: no limit std::int64_t probe_budget_ms_ = default_gate_probe_budget_ms(); // 0: no early probes bool adaptive_ = compile_gate_mode() == GateMode::adaptive; + bool graphics_ = compile_gate_graphics(); // graphics creates pass the gate std::array, kRecentWaits> recent_waits_{}; // [start, end) ms std::size_t recent_count_ = 0; // Per CreateKind. diff --git a/runtime/test/submit_mutex_test.cpp b/runtime/test/submit_mutex_test.cpp index 6b5663d748..c391f9241c 100644 --- a/runtime/test/submit_mutex_test.cpp +++ b/runtime/test/submit_mutex_test.cpp @@ -207,10 +207,10 @@ int gate_wait_limit() { return 0; } // The default with LEONIDA_COMPILE_GATE_WAIT_MS unset, whatever the title watchdog variables say: none with - // LEONIDA_COMPILE_GATE=1. - const bool always = SubmitMutex::compile_gate_mode() == SubmitMutex::GateMode::always; + // LEONIDA_COMPILE_GATE=1 or unset (classic), kDefaultGateWaitMs with LEONIDA_COMPILE_GATE=adaptive. + const bool adaptive = SubmitMutex::compile_gate_mode() == SubmitMutex::GateMode::adaptive; if (!std::getenv("LEONIDA_COMPILE_GATE_WAIT_MS")) - EXPECT(SubmitMutex::default_gate_wait_limit_ms() == (always ? 0 : SubmitMutex::kDefaultGateWaitMs)); + EXPECT(SubmitMutex::default_gate_wait_limit_ms() == (adaptive ? SubmitMutex::kDefaultGateWaitMs : 0)); SubmitMutex m; m.set_gate_adaptive(false); // as LEONIDA_COMPILE_GATE=1: it never turns off m.set_gate_wait_limit_ms(150); @@ -295,6 +295,7 @@ int gate_adapts() { { SubmitMutex m; m.set_gate_adaptive(true); + m.set_gate_graphics(true); // as LEONIDA_COMPILE_GATE=adaptive m.set_gate_wait_limit_ms(100); m.set_gate_probe_budget_ms(0); // probes at the limit only: gate_probe_budget checks the budget std::atomic in{false}, release{false}; @@ -344,6 +345,7 @@ int gate_adapts() { { SubmitMutex m; m.set_gate_adaptive(true); + m.set_gate_graphics(true); // as LEONIDA_COMPILE_GATE=adaptive m.set_gate_wait_limit_ms(100); m.set_gate_probe_budget_ms(0); // probes at the limit only: gate_probe_budget checks the budget std::atomic first_in{false}, first_release{false}; @@ -394,6 +396,7 @@ int gate_adapts() { { SubmitMutex m; m.set_gate_adaptive(true); + m.set_gate_graphics(true); // as LEONIDA_COMPILE_GATE=adaptive m.set_gate_wait_limit_ms(100); m.set_gate_probe_budget_ms(0); std::atomic in{false}, release{false}; @@ -431,6 +434,7 @@ int gate_probe_budget() { { SubmitMutex m; m.set_gate_adaptive(true); + m.set_gate_graphics(true); // as LEONIDA_COMPILE_GATE=adaptive m.set_gate_wait_limit_ms(0); m.set_gate_probe_budget_ms(300); EXPECT(m.gate_probe_budget_ms() == 300); @@ -479,6 +483,7 @@ int gate_probe_budget() { { SubmitMutex m; m.set_gate_adaptive(true); + m.set_gate_graphics(true); // as LEONIDA_COMPILE_GATE=adaptive m.set_gate_wait_limit_ms(0); m.set_gate_probe_budget_ms(100); std::atomic in{false}, release{false}; @@ -532,6 +537,7 @@ int gate_per_kind() { using Kind = SubmitMutex::CreateKind; SubmitMutex m; m.set_gate_adaptive(true); + m.set_gate_graphics(true); // as LEONIDA_COMPILE_GATE=adaptive m.set_gate_wait_limit_ms(100); m.set_gate_probe_budget_ms(0); const SubmitMutex::CreateInfo compute{Kind::compute, "test compute", "test", 1000}; @@ -659,6 +665,7 @@ int gate_review_cases() { for (const bool a_first : {true, false}) { SubmitMutex m; m.set_gate_adaptive(true); + m.set_gate_graphics(true); // as LEONIDA_COMPILE_GATE=adaptive m.set_gate_wait_limit_ms(100); m.set_gate_probe_budget_ms(0); HeldCreate a(m, compute); @@ -688,6 +695,7 @@ int gate_review_cases() { { SubmitMutex m; m.set_gate_adaptive(true); + m.set_gate_graphics(true); // as LEONIDA_COMPILE_GATE=adaptive m.set_gate_wait_limit_ms(100); m.set_gate_probe_budget_ms(0); HeldCreate a(m, compute); @@ -708,6 +716,7 @@ int gate_review_cases() { { SubmitMutex m; m.set_gate_adaptive(true); + m.set_gate_graphics(true); // as LEONIDA_COMPILE_GATE=adaptive m.set_gate_wait_limit_ms(100); m.set_gate_probe_budget_ms(0); HeldCreate a(m, compute); @@ -725,6 +734,7 @@ int gate_review_cases() { { SubmitMutex m; m.set_gate_adaptive(true); + m.set_gate_graphics(true); // as LEONIDA_COMPILE_GATE=adaptive m.set_gate_wait_limit_ms(100); m.set_gate_probe_budget_ms(0); m.set_gate_held_limit_ms(400); @@ -761,13 +771,76 @@ int gate_review_cases() { return 0; } +// The classic gate (LEONIDA_COMPILE_GATE unset, the default): compute creates are waited for however long they take, +// graphics creates do not pass the gate, nothing probes. +int gate_classic() { + if (!SubmitMutex::compile_gate_enabled()) { + std::printf("compile gate off (LEONIDA_COMPILE_GATE=0): classic gate not checked\n"); + return 0; + } + using Kind = SubmitMutex::CreateKind; + using Verdict = SubmitMutex::GateVerdict; + const SubmitMutex::GateMode mode = SubmitMutex::compile_gate_mode(); + if (mode == SubmitMutex::GateMode::classic) { + EXPECT(!SubmitMutex::compile_gate_graphics()); + if (!std::getenv("LEONIDA_COMPILE_GATE_PROBE_MS") && !std::getenv("LEONIDA_COMPILE_GATE_WAIT_MS")) + EXPECT(SubmitMutex::default_gate_wait_limit_ms() == 0 && SubmitMutex::default_gate_probe_budget_ms() == 0); + } + SubmitMutex m; + m.set_gate_adaptive(false); + m.set_gate_graphics(false); + m.set_gate_wait_limit_ms(0); + m.set_gate_probe_budget_ms(0); + // A graphics create in the driver holds no submission back. + std::atomic g_in{false}, g_release{false}; + std::thread g([&] { + const SubmitMutex::CompileTurn turn(m, SubmitMutex::CreateInfo{Kind::graphics, "test mesh", "test", 2000}); + g_in = true; + while (!g_release) std::this_thread::sleep_for(1ms); + }); + const bool g_entered = eventually([&] { return g_in.load(); }); + std::int64_t beside_graphics = 0; + { + const std::int64_t start = now_ms(); + const SubmitMutex::SubmitTurn turn(m); + beside_graphics = now_ms() - start; + } + g_release = true; + g.join(); + // A compute create is waited for until it leaves, well past the adaptive gate's 2.5 s budget, and decides nothing. + std::atomic c_in{false}; + std::atomic c_left_at{0}; + std::thread c([&] { + const SubmitMutex::CompileTurn turn(m, SubmitMutex::CreateInfo{Kind::compute, "test compute", "test", 1000}); + c_in = true; + std::this_thread::sleep_for(3000ms); + c_left_at = now_ms(); + }); + const bool c_entered = eventually([&] { return c_in.load(); }); + std::int64_t entered_at = 0; + bool ahead = true; + { + const SubmitMutex::SubmitTurn turn(m); + entered_at = now_ms(); + ahead = turn.went_ahead(); + } + c.join(); + EXPECT(g_entered && c_entered); + EXPECT(beside_graphics < 50); + EXPECT(entered_at >= c_left_at.load() && !ahead); + EXPECT(m.gate_verdict(Kind::compute) == Verdict::pending && m.gate_verdict(Kind::graphics) == Verdict::pending); + std::printf("classic gate: %lld ms beside a graphics create; a submission waited out a 3 s compute create\n", + static_cast(beside_graphics)); + return 0; +} + } // namespace int main() { std::setvbuf(stdout, nullptr, _IONBF, 0); if (owner_tracking() || waits_for_holder() || waits_for_queued_submitter() || handoff_is_capped() || compile_gate() || gate_wait_limit() || gate_adapts() || - gate_probe_budget() || gate_per_kind() || gate_review_cases()) + gate_probe_budget() || gate_per_kind() || gate_review_cases() || gate_classic()) return 1; std::printf("submit_mutex_test: all passed\n"); return 0; From ebdc9916278981721453db22a533659a4e971efa Mon Sep 17 00:00:00 2001 From: OreoFR <29048345+OreoFR@users.noreply.github.com> Date: Fri, 27 Nov 2026 12:46:36 +0100 Subject: [PATCH 8/8] compile gate: the held check on every pass of the wait, and presents never use the probe budget Second review on #2412: - enter_submit no longer switches to the held limit for good once it sees a create of a kind that holds submissions: each pass checks held_blocking(). With one in the driver the held limit applies on its own (also when it is under the plain limit) and the budget is off; without, the plain limit and budget apply, so a submission goes ahead at the plain limit once the holding create left while a create of a kind with no verdict stays. The "reached the limit, but ..." notice comes once per wait, and the wait wakes when a holding create leaves. - SubmitTurn passes `probe` into enter_submit: a present or wait never goes ahead at the probe budget, only at the limit. - VulkanPresenter::submit's turn does not probe (it covers the startup frame's fence wait and the present); the nested queue_submit turn still probes the vkQueueSubmit alone. - submit_mutex_test: gate_second_review covers the four cases. Co-Authored-By: Claude Opus 5.5 --- docs/d3d12_vulkan.md | 20 ++--- runtime/src/vulkan_device_fault.h | 63 +++++++++++----- runtime/src/vulkan_present.h | 5 +- runtime/test/submit_mutex_test.cpp | 116 ++++++++++++++++++++++++++++- 4 files changed, 173 insertions(+), 31 deletions(-) diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index f0ea2b920b..a368c77b48 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -2955,15 +2955,17 @@ submitting thread; at the maintainers' decision it was replaced by this one when and without `VK_NV_device_diagnostics_config` and a shared pipeline cache) never held a submission past 80 ms. The recent leaves are kept in a ring, so a create that entered after the submission and left after the one it entered beside does not hide that one. Only submissions (`vkQueueSubmit`, `vkQueueBindSparse`) probe: presents and - idle or semaphore waits pass the gate the same way but can take seconds on their own, so they judge nothing. While - a create of a kind that holds submissions is in the driver, no submission goes ahead at the limit or the budget, - and such a create is waited for even when one went past it before the verdict, up to the held limit - (`LEONIDA_COMPILE_GATE_HELD_WAIT_MS`, 60 s; 0 waits however long): then the creates in the driver are gone past - and the next submissions no longer wait for them. A false "holds" verdict so costs at most that per create, not the - rest of the run. On a device loss the log names every create - in the driver (kind, program, SPIR-V size, thread, how long it had been there, whether submissions waited for it) - and each kind's gate. Long graphics creates would leave the title's process only through the compile helper, which - builds compute pipelines alone today. + idle or semaphore waits pass the gate the same way but can take seconds on their own, so they judge nothing, and + they never go ahead at the probe budget, only at the limit. While a create of a kind that holds submissions is in + the driver, no submission goes ahead at the limit or the budget, and such a create is waited for even when one went + past it before the verdict, up to the held limit (`LEONIDA_COMPILE_GATE_HELD_WAIT_MS`, 60 s; 0 waits however long, + and a held limit under the plain one applies on its own): then the creates in the driver are gone past and the + next submissions no longer wait for them. This is checked on every pass of the wait: once such a create leaves + while creates of a kind with no verdict stay, the plain limit and budget apply again. A false + "holds" verdict so costs at most that per create, not the rest of the run. On a device loss the log names every + create in the driver (kind, program, SPIR-V size, thread, how long it had been there, whether submissions waited + for it) and each kind's gate. Long graphics creates would leave the title's process only through the compile + helper, which builds compute pipelines alone today. **Open:** whether the game uses `D3D12_PIPELINE_STATE_STREAM_DESC` for mesh/RT and which subobject types appear. The trace will enumerate them. diff --git a/runtime/src/vulkan_device_fault.h b/runtime/src/vulkan_device_fault.h index 2064d83a0e..17f4209344 100644 --- a/runtime/src/vulkan_device_fault.h +++ b/runtime/src/vulkan_device_fault.h @@ -516,7 +516,7 @@ class SubmitMutex { // `probe` false for a present or an idle or semaphore wait: it waits at the gate like a submission, but its // time in the driver judges nothing (it can take seconds on its own while the GPU is busy). explicit SubmitTurn(SubmitMutex& gate, bool probe = true) : gate_(gate) { - gate.enter_submit(&ahead_); + gate.enter_submit(&ahead_, probe); ahead_.probe = probe; } ~SubmitTurn() { gate_.leave_submit(ahead_); } @@ -652,6 +652,12 @@ class SubmitMutex { return verdict_[std::size_t(kind)]; } + // How many "reached the limit, but a create of a kind that holds ..." notices were due (for tests: one per wait). + unsigned gate_held_notices() { + std::lock_guard lock(gate_mutex_); + return logged_held_waits_; + } + // The thread waiting in enter_submit for creates to leave the driver, or 0. std::uint32_t gate_waiter() const { return gate_waiter_.load(std::memory_order_relaxed); } @@ -705,7 +711,9 @@ class SubmitMutex { // `ahead` (optional) is told whether the submission entered the driver alongside creates the gate does not wait // for, and whether it went ahead at the wait limit or the probe budget, for leave_submit. - std::int64_t enter_submit(GateAhead* ahead = nullptr) { + // `probe` false (a present or an idle or semaphore wait): it never goes ahead at the probe budget, only at the + // wait limit, since its time in the driver judges nothing. + std::int64_t enter_submit(GateAhead* ahead = nullptr, bool probe = true) { if (!compile_gate_enabled()) return 0; std::unique_lock lock(gate_mutex_); ++submits_; @@ -717,34 +725,48 @@ class SubmitMutex { const std::uint32_t self = diagnostic_thread_id(); gate_waiter_.store(self, std::memory_order_relaxed); const std::int64_t start = diagnostic_ms(); - std::int64_t limit = wait_limit_ms_; - // The probe budget, only while every kind it waits for has no verdict. - std::int64_t budget = adaptive_ && blocking_kinds_pending() ? probe_budget_ms_ : 0; const GateWait what{self, blocking(), first->info}; std::int64_t next_note = 2000; const char* why = nullptr; // set: it goes ahead + std::int64_t limit = 0, budget = 0; // in force at the last check std::int64_t window_ms = 0; // with the probe budget: the waits of the window, this one's included + bool held_noted = false; // the "reached the limit, but ..." notice, once per wait for (;;) { const std::int64_t now = diagnostic_ms(); const std::int64_t waited = now - start; - if (limit > 0 && waited >= limit) { - why = "limit"; - } else if (budget > 0 && (window_ms = recent_waits_ms(now) + waited) >= budget) { - why = "probe budget"; + // Checked each time: a create of a kind that holds submissions, not gone past, may have left (or entered) + // since, while creates of other kinds stay. + const bool held = held_blocking(); + // The plain limit, and the probe budget only for a probe while every kind it waits for has no verdict. + const std::int64_t plain_budget = probe && adaptive_ && blocking_kinds_pending() ? probe_budget_ms_ : 0; + const char* plain = nullptr; + if (plain_budget > 0) window_ms = recent_waits_ms(now) + waited; + if (wait_limit_ms_ > 0 && waited >= wait_limit_ms_) { + plain = "limit"; + } else if (plain_budget > 0 && window_ms >= plain_budget) { + plain = "probe budget"; } - if (why && why[0] != 'h' && held_blocking()) { + if (!held) { + limit = wait_limit_ms_; + budget = plain_budget; + why = plain; + } else { // Not into the driver beside a create of a kind that holds submissions: there it would wait for the // create anyway, and the driver lost the device under such waits (621.40, a mesh create). It waits - // here for those, up to the held limit (LEONIDA_COMPILE_GATE_HELD_WAIT_MS; 0: however long). - if (logged_held_waits_++ < 8) - std::fprintf(stderr, "[rt] compile-gate: vkQueueSubmit on thread %u reached the %s after %lld ms, " - "but a create of a kind that holds submissions is in the driver: it waits " - "until that create leaves or %lld ms have passed\n", - self, why, static_cast(waited), static_cast(held_limit_ms_)); - why = nullptr; - budget = 0; + // here for those, up to the held limit (LEONIDA_COMPILE_GATE_HELD_WAIT_MS; 0: however long), which + // applies on its own, also when it is shorter than the plain limit. limit = held_limit_ms_; + budget = 0; if (limit > 0 && waited >= limit) why = "held limit"; + if (plain && !why && !held_noted) { + held_noted = true; + if (logged_held_waits_++ < 8) + std::fprintf(stderr, "[rt] compile-gate: vkQueueSubmit on thread %u reached the %s after %lld " + "ms, but a create of a kind that holds submissions is in the driver: it " + "waits until that create leaves or %lld ms have passed\n", + self, plain, static_cast(waited), + static_cast(held_limit_ms_)); + } } if (why) { // Every create in the driver now stops holding submissions back; later ones are waited for. @@ -761,7 +783,10 @@ class SubmitMutex { if (limit > 0) step = (std::min)(step, limit - waited); if (budget > 0) step = (std::min)(step, budget - window_ms); step = (std::max)(step, std::int64_t{1}); - if (gate_changed_.wait_for(lock, std::chrono::milliseconds(step), [&] { return !first_blocking(); })) + // Woken when no create holds it back any more, or when whether a holding create does has changed. + if (gate_changed_.wait_for(lock, std::chrono::milliseconds(step), + [&] { return !first_blocking() || held_blocking() != held; }) && + !first_blocking()) break; } gate_waiter_.store(0, std::memory_order_relaxed); diff --git a/runtime/src/vulkan_present.h b/runtime/src/vulkan_present.h index 3c81d5920c..da9376e545 100644 --- a/runtime/src/vulkan_present.h +++ b/runtime/src/vulkan_present.h @@ -650,8 +650,9 @@ class VulkanPresenter { { std::lock_guard lock(context_.submit_mutex()); // One turn of the compile gate for the submit and the present after it: no pipeline create enters the - // driver between the two (queue_submit's own turn nests inside this one). - const SubmitMutex::SubmitTurn turn(context_.submit_mutex()); // a queue operation: the compile gate + // driver between the two (queue_submit's own turn nests inside this one). Not a probe: it also covers + // finish_startup_frame's fence wait and the present; the nested turn probes the vkQueueSubmit alone. + const SubmitMutex::SubmitTurn turn(context_.submit_mutex(), false); // a queue operation: the compile gate submitted = context_.observe_result(context_.queue_submit(queue_, 1, &submit_info, frame.done), "vkQueueSubmit (present)"); if (submitted == VK_SUCCESS) context_.note_image_work_locked(); diff --git a/runtime/test/submit_mutex_test.cpp b/runtime/test/submit_mutex_test.cpp index c391f9241c..a98c6effa6 100644 --- a/runtime/test/submit_mutex_test.cpp +++ b/runtime/test/submit_mutex_test.cpp @@ -771,6 +771,119 @@ int gate_review_cases() { return 0; } +// The second review of #2412: whether a create that holds is in the driver is checked on every pass of the wait, the +// held limit applies on its own, the notice comes once per wait, and a present or wait never uses the probe budget. +int gate_second_review() { + if (!SubmitMutex::compile_gate_enabled()) { + std::printf("compile gate off (LEONIDA_COMPILE_GATE=0): second review cases not checked\n"); + return 0; + } + using Verdict = SubmitMutex::GateVerdict; + using Kind = SubmitMutex::CreateKind; + const SubmitMutex::CreateInfo compute{Kind::compute, "test compute", "test", 1000}; + const SubmitMutex::CreateInfo graphics{Kind::graphics, "test mesh", "test", 2000}; + const auto adaptive = [](SubmitMutex& m) { + m.set_gate_adaptive(true); + m.set_gate_graphics(true); // as LEONIDA_COMPILE_GATE=adaptive + m.set_gate_probe_budget_ms(0); + }; + // Compute gets the held verdict: a submission goes ahead at a 100 ms limit, stays 2 s, and comes back just after + // the create leaves. + const auto compute_holds = [&](SubmitMutex& m) { + m.set_gate_wait_limit_ms(100); + HeldCreate first(m, compute); + if (!eventually([&] { return first.in.load(); })) return false; + { + const SubmitMutex::SubmitTurn turn(m); + std::this_thread::sleep_for(2000ms); + first.let_go(); + std::this_thread::sleep_for(50ms); + } + return m.gate_verdict(Kind::compute) == Verdict::driver_holds; + }; + // (a) The holding compute create leaves while a graphics create (no verdict) stays: the submission goes ahead at + // the plain limit, even with no held limit. + { + SubmitMutex m; + adaptive(m); + const bool holds = compute_holds(m); + m.set_gate_wait_limit_ms(300); + m.set_gate_held_limit_ms(0); + HeldCreate c(m, compute); + HeldCreate g(m, graphics); + EXPECT(eventually([&] { return c.in.load() && g.in.load(); })); + std::atomic waited{-1}; + std::atomic ahead{false}; + std::thread submit([&] { + const std::int64_t start = now_ms(); + const SubmitMutex::SubmitTurn turn(m); + waited = now_ms() - start; + ahead = turn.went_ahead(); + }); + std::this_thread::sleep_for(150ms); + c.let_go(); + const bool went = eventually([&] { return waited.load() >= 0; }); // within 2 s, the graphics one still there + g.let_go(); + submit.join(); + EXPECT(holds); + EXPECT(went && ahead.load() && waited.load() >= 290 && waited.load() < 1000); + std::printf("compile gate: the holding create left, the other kind's stayed: went ahead after %lld ms " + "(limit 300 ms, no held limit)\n", static_cast(waited.load())); + } + // (b) and (c): a held limit under the plain limit applies on its own, and one over it gives one notice per wait. + for (const bool shorter : {true, false}) { + SubmitMutex m; + adaptive(m); + const bool holds = compute_holds(m); + m.set_gate_wait_limit_ms(shorter ? 2000 : 100); + m.set_gate_held_limit_ms(shorter ? 300 : 400); + HeldCreate c(m, compute); + EXPECT(eventually([&] { return c.in.load(); })); + const unsigned notices = m.gate_held_notices(); + std::int64_t waited = 0; + bool ahead = false; + { + const std::int64_t start = now_ms(); + const SubmitMutex::SubmitTurn turn(m); + waited = now_ms() - start; + ahead = turn.went_ahead(); + } + const unsigned noted = m.gate_held_notices() - notices; + c.let_go(); + EXPECT(holds && ahead); + if (shorter) { + EXPECT(waited >= 290 && waited < 1000 && noted == 0); + } else { + EXPECT(waited >= 390 && waited < 1000 && noted == 1); + } + std::printf("compile gate: held limit %s the plain limit: went ahead after %lld ms, %u notice(s)\n", + shorter ? "300 ms, under" : "400 ms, over", static_cast(waited), noted); + } + // (d) A present or wait beside a create with no verdict never goes ahead at the probe budget, only at the limit. + { + SubmitMutex m; + adaptive(m); + m.set_gate_wait_limit_ms(1000); + m.set_gate_probe_budget_ms(200); + HeldCreate c(m, compute); + EXPECT(eventually([&] { return c.in.load(); })); + std::int64_t waited = 0; + bool ahead = false; + { + const std::int64_t start = now_ms(); + const SubmitMutex::SubmitTurn turn(m, false); + waited = now_ms() - start; + ahead = turn.went_ahead(); + } + c.let_go(); + EXPECT(ahead && waited >= 990 && waited < 2000); + EXPECT(m.gate_verdict(Kind::compute) == Verdict::pending); + std::printf("compile gate: a present beside an unverified create waited %lld ms (budget 200 ms, limit 1000 " + "ms)\n", static_cast(waited)); + } + return 0; +} + // The classic gate (LEONIDA_COMPILE_GATE unset, the default): compute creates are waited for however long they take, // graphics creates do not pass the gate, nothing probes. int gate_classic() { @@ -840,7 +953,8 @@ int main() { std::setvbuf(stdout, nullptr, _IONBF, 0); if (owner_tracking() || waits_for_holder() || waits_for_queued_submitter() || handoff_is_capped() || compile_gate() || gate_wait_limit() || gate_adapts() || - gate_probe_budget() || gate_per_kind() || gate_review_cases() || gate_classic()) + gate_probe_budget() || gate_per_kind() || gate_review_cases() || gate_second_review() || + gate_classic()) return 1; std::printf("submit_mutex_test: all passed\n"); return 0;