diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 50dd51614b..7ff2cd4177 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -2935,8 +2935,29 @@ submitting thread; at the maintainers' decision it was replaced by this one when `after_running` and the teardown rules hold as they were. `LEONIDA_COMPILE_THREADS=1` is the layout before the pool: one general worker and the graphics-only one; there too a PSO has one build running at a time (two variants of one graphics PSO, one on each worker, let its last release delete it under the other build, qhukz13). A submission still - waits at the compile gate for every compute create in flight (and graphics creates too with `LEONIDA_COMPILE_GATE=adaptive` or `1`; the classic default gates compute builds only), so more workers + waits at the compile gate for every demanded compute create in flight (and graphics creates too with `LEONIDA_COMPILE_GATE=adaptive` or `1`; the + classic default gates compute builds only; warm-ups: next paragraph), so more workers do not shorten that wait (`SubmitMutex::CompileTurn`; `LEONIDA_COMPILE_GATE=0` turns it off). +- **Warm-ups do not hold the compile gate** (RT-COMPILE-GATE-WARMUP-FREE). With the adaptive gate, a submission + waits at the compile gate (`SubmitMutex::CompileTurn`) only for the demanded creates in the driver, not for a + warm-up's: a compute create made by a compile worker's speculative job (`compile_job_speculative`: the compute + warm-up) passes the gate as speculative. The checkpoint is + a speculative job too, but makes no create. A warm-up's pipeline is simply not ready yet; whoever needs it builds or + waits for it as before. A warm-up still never enters the driver while a submission is in it or waits at the gate, + and at most `LEONIDA_COMPILE_GATE_WARMUP_MAX` of them (default 1) are in the driver at once, so the creates beside a + submission stay bounded: the pool's demanded builds plus that many warm-ups, and the pool starts no warm-up while a + demanded build runs. Why: on HasiKe's RTX 2070 SUPER (#2233) a cold boot crawled at about one submission a second + behind 290 s of serial warm-ups with the gate on, and ran at about 100 frames a second with `LEONIDA_COMPILE_GATE=0`; + MoonEyes measured 142 s of submission waits behind warm-ups (#1608). The gate stays for demanded creates, which the + title waits for anyway. A speculative create holds the gate as a demanded one does (`SubmitMutex::warmup_passes`, + lixonik's review of #2275): with the classic gate and with `LEONIDA_COMPILE_GATE=1`, which are in use where the + driver holds a submission behind a create or that is not known (`auto`'s calibration on 630.20), and where no probe + ever says otherwise; as a graphics or mesh prewarm (`LEONIDA_PIPELINE_PREWARM_ORDER`, also a speculative job), since the calibration measures only a compute create and a + submission was held 54 s beside a mesh create on 621.40; and as a create of a kind the adaptive gate found to hold + submissions, where a submission beside it would wait in the driver instead. `LEONIDA_COMPILE_GATE_WARMUPS=1` makes + every warm-up hold the gate, as before; `=0` lets compute ones pass with every gate, the classic one and `=1` too; + `LEONIDA_COMPILE_GATE=0` still turns the whole gate off. The paragraphs above and below that say a warm-up holds the + gate for its whole create describe those cases. - **Only the large ones asynchronous** (`LEONIDA_ASYNC_MIN_BYTES=`, with `LEONIDA_ASYNC_PIPELINES=1`). With every pipeline asynchronous, the small lighting and post programs are skipped with the large ones until the worker gets to them, and on an RTX 4070 Laptop (NVIDIA 621.40) the world phase was reached with the screen black; with none, a diff --git a/runtime/README.md b/runtime/README.md index bd9105ea14..bdb3322a2d 100644 --- a/runtime/README.md +++ b/runtime/README.md @@ -425,11 +425,13 @@ 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` | `auto` (adaptive or classic, by calibration) | 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 or `auto`: the device measures the driver once, before the title has it (`src/compile_gate_calibration.h`, `docs/d3d12_vulkan.md`): a worker thread creates a synthetic compute pipeline that takes about a second to compile while the queue gets empty submissions, each waited for. Submissions back in under 50 ms with the create still in the driver give the adaptive gate; one held until the create leaves, a create too fast to tell, a submission slow for another reason or any failure give the classic gate. The verdict is kept per GPU and driver (vendor, device, driver version, pipeline cache UUID) in `compile_gate_calibration.txt` beside the pipeline cache, so the measurement runs once per driver (a failed or unclear calibration is not kept: the next launch measures again), and is logged once: `[rt] compile-gate: auto: adaptive (calibrated on NVIDIA 621.40 in 0.7 s: 115 submissions came back in <= 0.5 ms beside a 722 ms create; cached in ...)`, or `auto: classic (...)`, or `(cached calibration of ...)`. Only gta6rt's title process calibrates: `--precompile-pipelines`, `--compile-server` and tests keep the classic gate. `classic`: compute creates only, waited for however long they take (`LEONIDA_COMPILE_GATE_WAIT_MS` can bound it), no probes, the default before `auto`. `adaptive`: the adaptive gate whatever the calibration says; on a driver that holds submissions (630.20) 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` | `auto` (adaptive or classic, by calibration) | 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 or `auto`: the device measures the driver once, before the title has it (`src/compile_gate_calibration.h`, `docs/d3d12_vulkan.md`): a worker thread creates a synthetic compute pipeline that takes about a second to compile while the queue gets empty submissions, each waited for. Submissions back in under 50 ms with the create still in the driver give the adaptive gate; one held until the create leaves, a create too fast to tell, a submission slow for another reason or any failure give the classic gate. The verdict is kept per GPU and driver (vendor, device, driver version, pipeline cache UUID) in `compile_gate_calibration.txt` beside the pipeline cache, so the measurement runs once per driver (a failed or unclear calibration is not kept: the next launch measures again), and is logged once: `[rt] compile-gate: auto: adaptive (calibrated on NVIDIA 621.40 in 0.7 s: 115 submissions came back in <= 0.5 ms beside a 722 ms create; cached in ...)`, or `auto: classic (...)`, or `(cached calibration of ...)`. Only gta6rt's title process calibrates: `--precompile-pipelines`, `--compile-server` and tests keep the classic gate. `classic`: compute creates only, waited for however long they take (`LEONIDA_COMPILE_GATE_WAIT_MS` can bound it), no probes, the default before `auto`. `adaptive`: the adaptive gate whatever the calibration says; on a driver that holds submissions (630.20) 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. See `LEONIDA_COMPILE_GATE_WARMUPS` for warm-ups. | | `LEONIDA_COMPILE_GATE_WAIT_MS` | 0 (15000 with the adaptive gate) | ``: 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 with the adaptive gate (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 by the adaptive gate. | | `LEONIDA_COMPILE_GATE_RECALIBRATE` | unset | `1`: with `LEONIDA_COMPILE_GATE` unset or `auto`, the device measures the driver again instead of reading the verdict kept in `compile_gate_calibration.txt`, and writes the new one there (after a driver update the key changes and it measures anyway). | +| `LEONIDA_COMPILE_GATE_WARMUPS` | unset (compute warm-ups free with the adaptive gate) | Unset: with the adaptive gate (`LEONIDA_COMPILE_GATE=adaptive`, or `auto` where the calibration chose it), a submission at the compile gate (`SubmitMutex::CompileTurn`) does not wait for a speculative compute create, one a compile worker's speculative job makes (`compile_job_speculative`: the compute warm-up), though such a create still never enters the driver while a submission is in it or waits; it waits for demanded creates as before, and for any create of a kind the adaptive gate found to hold submissions. Graphics and mesh prewarms (`LEONIDA_PIPELINE_PREWARM_ORDER`), speculative jobs too, hold the gate as demanded creates do (the calibration measures only a compute create, and NVIDIA 621.40 held a submission 54 s beside a mesh create), and so do all speculative creates with the classic gate and with `LEONIDA_COMPILE_GATE=1`, which are in use where the driver holds submissions or that is not known. `1`: every speculative create holds the gate as a demanded create does, the behaviour before. `0`: a speculative compute create passes with every gate, the classic one and `1` included (on a driver that holds submissions a submission may then wait in the driver beside it). See `docs/d3d12_vulkan.md`, "Warm-ups do not hold the compile gate". | +| `LEONIDA_COMPILE_GATE_WARMUP_MAX` | `1` | `` (1-64): at most n speculative creates that pass the compile gate without holding submissions (`LEONIDA_COMPILE_GATE_WARMUPS`) in the driver at once. | | `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_COMPILE_THREADS` | half the logical processors, 2-8 | ``: the compile workers of the asynchronous builds (`LEONIDA_ASYNC_PIPELINES=1`), the compute warm-up and the cache checkpoint (`shims/d3d12_pipeline_compile.h`, `CompilePool`). A demanded build goes before warm-ups; large and speculative builds take at most n-1 workers, so one stays free for a small build a submission waits for; one build of a PSO at a time; warm-ups one at a time, and none while a demanded build is queued or running. `1`: one general worker plus the graphics-only one (`LEONIDA_GRAPHICS_COMPILE_WORKER`), the layout before the pool. | | `LEONIDA_COMPILE_HEAVY_MAX` | threads-1 (`1` with `LEONIDA_SERIAL_BIG_COMPILES=1`) | ``: at most n builds of large modules (compute over 128 KiB, graphics of 1 MiB of SPIR-V or more) on the compile workers at once, 1 to threads-1. They also keep within the memory budget of `LEONIDA_BIG_COMPILE_BUDGET_MB`. | diff --git a/runtime/shims/d3d12_pipeline_compile.h b/runtime/shims/d3d12_pipeline_compile.h index 7fb47b72fa..52aed79e4c 100644 --- a/runtime/shims/d3d12_pipeline_compile.h +++ b/runtime/shims/d3d12_pipeline_compile.h @@ -166,9 +166,17 @@ inline bool compile_worker_trigger(const char* trigger) { // 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, as its kind -// (compute, or graphics with mesh): the adaptive gate keeps a verdict per kind. +// (compute, or graphics with mesh): the adaptive gate keeps a verdict per kind. As speculative when a compile worker +// runs it for a speculative job (compile_job_speculative); the gate decides whether that lets it pass without +// holding submissions (SubmitMutex::warmup_passes: a compute create, with the adaptive gate). // - 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. +// True on a compile worker while it runs a speculative job (any job of a priority other than 0: today the compute warm-up, +// the graphics and mesh prewarms of LEONIDA_PIPELINE_PREWARM_ORDER, and the checkpoint, which makes no create; +// PipelineCompiler::run): its pipeline creates ask the compile gate to pass them as speculative, which a compute one +// with the adaptive gate does without holding submissions (SubmitMutex::CompileTurn, warmup_passes). +inline thread_local bool compile_job_speculative = false; + class DriverTurn { public: DriverTurn(leonida::SubmitMutex& submits, const char* trigger, const char* call, bool pipeline, @@ -189,7 +197,8 @@ class DriverTurn { if (waited >= 50) note("waited for the submit lock", trigger, call, name, waited); } if (pipeline) { - gate_.emplace(submits, leonida::SubmitMutex::CreateInfo{kind, name, trigger, bytes}); + gate_.emplace(submits, leonida::SubmitMutex::CreateInfo{kind, name, trigger, bytes}, + compile_job_speculative); if (gate_->waited_ms() >= 50) note("waited at the compile gate", trigger, call, name, gate_->waited_ms()); } } @@ -607,12 +616,14 @@ class PipelineCompiler { stats_.most_running = (std::max)(stats_.most_running, running_.size()); const auto probe = probe_; lock.unlock(); + compile_job_speculative = speculative; // its creates pass the gate as speculative (DriverTurn) try { if (probe) probe(job.owner); job.work(); } catch (...) { std::fprintf(stderr, "[rt] pipeline compile worker: a job threw\n"); } + compile_job_speculative = false; job.work = nullptr; // its captures go before the job counts as done lock.lock(); ++stats_.completed; diff --git a/runtime/src/vulkan_device_fault.h b/runtime/src/vulkan_device_fault.h index 3c49fce8d2..0c96d28554 100644 --- a/runtime/src/vulkan_device_fault.h +++ b/runtime/src/vulkan_device_fault.h @@ -422,6 +422,18 @@ 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. // + // A speculative create (a warm-up or a prewarm: nobody waits for its pipeline yet) of a compute pipeline, with the + // adaptive gate, does not hold submissions: they wait only for the demanded creates in the driver. It still never + // enters while a submission is in the driver or waits there, and at most warmup_max of them are in the driver at + // once, so the creates beside a submission stay bounded (HasiKe and MoonEyes, #2233 / #1608: a cold boot crawled at + // about one submission a second behind 290 s of warm-ups). Every other speculative create holds submissions as a + // demanded create does (warmup_passes): with the classic gate or LEONIDA_COMPILE_GATE=1, which are in use where the + // driver holds a submission behind a create or that is not known (auto's calibration, 630.20), and no probe ever + // tells otherwise; a graphics or mesh one, since the calibration measures only a compute create and a submission + // was held 54 s beside a mesh create on 621.40; and one of a kind the adaptive gate found to hold submissions + // (is_blocking). LEONIDA_COMPILE_GATE_WARMUPS=1: speculative creates always hold the gate (the behaviour before); + // =0: a compute one passes with every gate, the classic one and =1 too. + // // 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 @@ -502,16 +514,20 @@ class SubmitMutex { using GateAhead = CompileGateAhead; class CompileTurn { // around vkCreate*Pipelines public: - explicit CompileTurn(SubmitMutex& gate, const CreateInfo& info = {}) - : gate_(gate), waited_(gate.enter_compile(seq_, info)) {} + // `speculative`: asked for; speculative() says whether the gate let it pass as one (warmup_passes). + explicit CompileTurn(SubmitMutex& gate, const CreateInfo& info = {}, bool speculative = false) + : gate_(gate), waited_(gate.enter_compile(seq_, info, speculative, &speculative_)) {} ~CompileTurn() { gate_.leave_compile(seq_); } CompileTurn(const CompileTurn&) = delete; CompileTurn& operator=(const CompileTurn&) = delete; std::int64_t waited_ms() const { return waited_; } + bool speculative() const { return speculative_; } + private: SubmitMutex& gate_; std::uint64_t seq_ = 0; // declared before waited_: enter_compile sets it + bool speculative_ = false; // declared before waited_: enter_compile sets it std::int64_t waited_; }; class SubmitTurn { // around vkQueueSubmit, with the submit lock held @@ -698,6 +714,41 @@ class SubmitMutex { return logged_held_waits_; } + // Whether speculative creates may pass the gate without holding submissions (see CompileTurn), and how many may be + // in the driver at once. LEONIDA_COMPILE_GATE_WARMUPS: unset, compute ones with the adaptive gate; 1, none (they + // hold it as demanded creates do); 0, compute ones with every gate (`everywhere`). LEONIDA_COMPILE_GATE_WARMUP_MAX + // =: n at once (1-64, default 1). Read once; a test sets them per gate (set_warmup_policy). + static constexpr unsigned kMaxWarmups = 64; + struct WarmupPolicy { + bool free = true; + unsigned max = 1; + bool everywhere = false; // with the classic gate and LEONIDA_COMPILE_GATE=1 too + }; + static WarmupPolicy warmup_policy_from_environment() { + WarmupPolicy policy; + if (const char* warmups = std::getenv("LEONIDA_COMPILE_GATE_WARMUPS")) { + policy.free = std::strcmp(warmups, "1") != 0; + policy.everywhere = !std::strcmp(warmups, "0"); + } + if (const char* max = std::getenv("LEONIDA_COMPILE_GATE_WARMUP_MAX")) { + char* end = nullptr; + const unsigned long n = std::strtoul(max, &end, 10); + if (end != max && !*end && n >= 1 && n <= kMaxWarmups) policy.max = unsigned(n); + } + return policy; + } + // max is clamped to 1-64: 0 would keep every speculative create waiting for a place. + void set_warmup_policy(WarmupPolicy policy) { + policy.max = (std::clamp)(policy.max, 1u, kMaxWarmups); + std::lock_guard lock(gate_mutex_); + policy_ = policy; + } + // Speculative creates in the driver now (they are not counted in what a submission waits for). + unsigned speculative_compiles() { + std::lock_guard lock(gate_mutex_); + return speculative_; + } + // 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); } @@ -713,8 +764,12 @@ class SubmitMutex { return stats_; } - // `seq` receives the create's number, which leave_compile takes back. - std::int64_t enter_compile(std::uint64_t& seq, const CreateInfo& info = {}) { + // `seq` receives the create's number, which leave_compile takes back. `speculative`: a warm-up or prewarm, which + // passes without holding submissions where warmup_passes says so (CompileTurn) and then also waits for a free + // place among the warmup_max speculative ones; `passed` (optional) is told whether it did. + std::int64_t enter_compile(std::uint64_t& seq, const CreateInfo& info = {}, bool speculative = false, + bool* passed = nullptr) { + if (passed) *passed = false; if (!compile_gate_enabled()) return 0; std::unique_lock lock(gate_mutex_); std::int64_t waited = 0; @@ -722,31 +777,40 @@ class SubmitMutex { seq = 0; return 0; } + speculative = speculative && warmup_passes(info.kind); + if (passed) *passed = speculative; // 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 auto blocked = [&] { + return (submits_ && !own && !kind_off(info.kind)) || (speculative && speculative_ >= policy_.max); + }; + if (blocked()) { const std::int64_t start = diagnostic_ms(); - gate_changed_.wait(lock, [&] { return submits_ == 0 || kind_off(info.kind); }); + gate_changed_.wait(lock, [&] { return !blocked(); }); waited = diagnostic_ms() - start; ++stats_.creates; stats_.create_ms += waited; stats_.create_max_ms = (std::max)(stats_.create_max_ms, waited); } seq = ++last_create_; - in_driver_.push_back(InDriver{seq, info, diagnostic_ms(), diagnostic_thread_id()}); + in_driver_.push_back(InDriver{seq, info, diagnostic_ms(), diagnostic_thread_id(), speculative}); + if (speculative) ++speculative_; return waited; } void leave_compile(std::uint64_t seq) { if (!compile_gate_enabled()) return; std::lock_guard lock(gate_mutex_); + bool speculative = false; for (auto it = in_driver_.begin(); it != in_driver_.end(); ++it) if (it->seq == seq) { recent_leaves_[leave_count_++ % kRecentLeaves] = Leave{seq, diagnostic_ms(), it->info.kind}; + speculative = it->speculative; + if (speculative) --speculative_; in_driver_.erase(it); break; } - if (submits_) gate_changed_.notify_all(); + if (submits_ || speculative) gate_changed_.notify_all(); // speculative: a warm-up waiting for a place } // `ahead` (optional) is told whether the submission entered the driver alongside creates the gate does not wait @@ -901,17 +965,23 @@ class SubmitMutex { CreateInfo info; std::int64_t since_ms; std::uint32_t thread; + bool speculative = false; // a warm-up (CompileTurn): submissions do not wait for it }; // With gate_mutex_ held. + // Whether a speculative create of this kind passes without holding submissions (see CompileTurn). + bool warmup_passes(CreateKind kind) const { + return policy_.free && kind == CreateKind::compute && (adaptive_ || policy_.everywhere); + } 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. + // gone past at the limit or the budget and not a warm-up. bool is_blocking(const InDriver& c) const { if (kind_off(c.info.kind)) return false; if (verdict_[std::size_t(c.info.kind)] == GateVerdict::driver_holds) return c.seq > held_overdue_through_; + if (c.speculative) return false; // a warm-up, of a kind not known to hold submissions return c.seq > overdue_through_; } bool held_blocking() const { @@ -1104,6 +1174,8 @@ class SubmitMutex { 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 + unsigned speculative_ = 0; // warm-up creates in the driver, while warm-ups are free (policy_) + WarmupPolicy policy_ = warmup_policy_from_environment(); 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 diff --git a/runtime/test/d3d12_compile_pool_test.cpp b/runtime/test/d3d12_compile_pool_test.cpp index 2bf1cdb61c..a8cc6e842f 100644 --- a/runtime/test/d3d12_compile_pool_test.cpp +++ b/runtime/test/d3d12_compile_pool_test.cpp @@ -6,6 +6,8 @@ // - demanded jobs go first, in the order they came, then warm-ups by size, and a checkpoint only when nothing runs; // - a checkpoint's second look sees a demanded build that started on another worker meanwhile; // - one job of a PSO at a time, also with one worker plus the graphics-only one; +// - a warm-up's and the checkpoint's creates pass the compile gate as speculative, a demanded build's do not, through +// DriverTurn as the runtime makes them; // - the last release with jobs running and queued: the queued ones are dropped, the running ones finish, the // workers are joined; a device's cancel waits for its running jobs; stop_for_exit does not wait past its limit. #include "../shims/d3d12_pipeline_compile.h" @@ -18,6 +20,7 @@ #include #include #include +#include #include #include @@ -390,6 +393,87 @@ void test_a_checkpoint_sees_a_demand_that_started_during_it() { pool.release(); } +// What a job's pipeline creates tell the compile gate (DriverTurn, compile_job_speculative): a warm-up and the checkpoint +// are speculative and do not hold submissions; a demanded build is not, also on a worker that ran a warm-up before. +void test_jobs_tell_the_gate_whether_they_are_speculative() { + for (const unsigned threads : {1u, 3u}) { + auto& pool = make_pool(threads, threads > 1 ? threads - 1 : 1); + Log log; + std::mutex mutex; + std::vector> seen; + Owners owner; + const auto job = [&](const void* who, const std::string& name, std::size_t priority) { + EXPECT(pool.submit(who, device, priority, [&, name] { + std::lock_guard lock(mutex); + seen.emplace_back(name, rt::d3d12::compile_job_speculative); + log.start(name, false); + log.finish(name, false); + })); + }; + job(owner[0], "warm", 100); + EXPECT(log.wait_finished(1)); + job(owner[1], "demand", 0); + EXPECT(log.wait_finished(2)); + job(device, "checkpoint", SIZE_MAX); + EXPECT(log.wait_finished(3)); + job(owner[2], "demand again", 0); + EXPECT(log.wait_finished(4)); + EXPECT(!rt::d3d12::compile_job_speculative); // this thread is no worker + std::lock_guard lock(mutex); + const std::vector> want{ + {"warm", true}, {"demand", false}, {"checkpoint", true}, {"demand again", false}}; + EXPECT(seen == want); + pool.release(); + } +} + +// The one line that turns the warm-up-free gate on in the runtime: a pipeline create's DriverTurn on a thread whose job is +// speculative passes the gate as speculative, so a submission does not wait for it; on any other thread it holds +// submissions as before (qhukz13, #2275). +void test_a_driver_turn_passes_the_speculative_flag_to_the_gate() { + if (!leonida::SubmitMutex::compile_gate_enabled()) return; // LEONIDA_COMPILE_GATE=0: no gate to check + leonida::SubmitMutex gate; + gate.set_gate_adaptive(true); // warm-ups pass only the adaptive gate (submit_mutex_test has the other modes) + gate.set_gate_wait_limit_ms(0); + gate.set_gate_probe_budget_ms(0); + gate.set_gate_graphics(true); // graphics creates pass the gate, as with the adaptive gate + gate.set_warmup_policy({true, 1}); + // A speculative compute create (a warm-up), a demanded one, and a speculative graphics one (a prewarm of + // LEONIDA_PIPELINE_PREWARM_ORDER, which holds submissions: the calibration measures only compute creates). + struct Case { + bool speculative; + leonida::SubmitMutex::CreateKind kind; + bool passes; + }; + for (const Case c : {Case{true, leonida::SubmitMutex::CreateKind::compute, true}, + Case{false, leonida::SubmitMutex::CreateKind::compute, false}, + Case{true, leonida::SubmitMutex::CreateKind::graphics, false}}) { + const bool speculative = c.speculative; + std::atomic inside{false}, release{false}, submitted{false}; + std::thread create([&] { + rt::d3d12::compile_job_speculative = speculative; + { + const rt::d3d12::DriverTurn turn(gate, "warmup", "vkCreateComputePipelines", true, nullptr, c.kind); + inside = true; + while (!release) std::this_thread::sleep_for(1ms); + } + rt::d3d12::compile_job_speculative = false; + }); + for (int i = 0; i < 2000 && !inside; ++i) std::this_thread::sleep_for(1ms); + EXPECT(inside.load()); + std::thread submit([&] { + const leonida::SubmitMutex::SubmitTurn turn(gate); + submitted = true; + }); + std::this_thread::sleep_for(150ms); + EXPECT(submitted.load() == c.passes); // past a warm-up at once; behind a demanded create or a prewarm + release = true; + create.join(); + submit.join(); + EXPECT(submitted.load()); + } +} + void test_one_job_of_a_pso_at_a_time() { auto& pool = make_pool(4, 3); Log log; @@ -526,6 +610,8 @@ int main() { test_order(); test_a_checkpoint_sees_a_demand_that_started_during_it(); test_one_job_of_a_pso_at_a_time(); + test_jobs_tell_the_gate_whether_they_are_speculative(); + test_a_driver_turn_passes_the_speculative_flag_to_the_gate(); test_a_finished_job_wakes_the_idle_workers(); test_release_with_jobs_running_and_queued(); test_cancel_waits_for_a_devices_running_jobs(); diff --git a/runtime/test/submit_mutex_test.cpp b/runtime/test/submit_mutex_test.cpp index a98c6effa6..2cd16e29b8 100644 --- a/runtime/test/submit_mutex_test.cpp +++ b/runtime/test/submit_mutex_test.cpp @@ -1,7 +1,7 @@ // leonida::SubmitMutex (src/vulkan_device_fault.h), on the CPU: the submit lock's owner and hold tracking, its waiter // count, let_submit_finish (the handoff a pipeline create makes to submits before entering the driver) and the -// compile gate between pipeline creates and queue submissions. Windows only: the owner is a Win32 thread id. No GPU, -// no game data. +// compile gate between pipeline creates and queue submissions, with warm-up creates that do not hold submissions +// (RT-COMPILE-GATE-WARMUP-FREE). Windows only: the owner is a Win32 thread id. No GPU, no game data. #include "../src/vulkan_device_fault.h" #include @@ -13,6 +13,7 @@ #include #include #include +#include #define EXPECT(condition) do { \ if (!(condition)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); return 1; } \ @@ -20,6 +21,12 @@ namespace { +// For checks inside threads, where EXPECT's return would leave the thread, not the test. +std::atomic failed_void{false}; +#define EXPECT_VOID(condition) do { \ + if (!(condition)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); failed_void = true; } \ +} while (0) + using leonida::SubmitMutex; using namespace std::chrono_literals; @@ -201,6 +208,204 @@ int compile_gate() { return 0; } +// Speculative creates (CompileTurn speculative: warm-ups and prewarms). With the adaptive gate a compute one does not +// hold submissions; demanded ones still do; at most warmup_max are in the driver at once; none enters while a +// submission waits or is in the driver. They hold submissions as demanded creates do with the classic gate and with +// LEONIDA_COMPILE_GATE=1 (unless LEONIDA_COMPILE_GATE_WARMUPS=0), as graphics or mesh ones, and with +// LEONIDA_COMPILE_GATE_WARMUPS=1. Each case on a gate of its own, so no probe of one case gives a verdict to the next. +int warmups_do_not_hold_the_gate() { + if (!SubmitMutex::compile_gate_enabled()) { + std::printf("warm-up gate: compile gate off (LEONIDA_COMPILE_GATE=0): not checked\n"); + return 0; + } + // A gate as each mode makes it: the adaptive one without early probes or a wait limit (a verdict could turn the + // compute gate off under a later case), the classic one, and LEONIDA_COMPILE_GATE=1 (graphics creates pass it). + enum class Mode { adaptive, classic, always }; + const auto make = [](Mode mode, SubmitMutex::WarmupPolicy policy = {true, 1}) { + auto m = std::make_unique(); + m->set_gate_adaptive(mode == Mode::adaptive); + m->set_gate_graphics(mode != Mode::classic); + m->set_gate_wait_limit_ms(0); + m->set_gate_probe_budget_ms(0); + m->set_warmup_policy(policy); + return m; + }; + // Whether a submission waits for a speculative create of `kind` on gate `m`: the create stays 150 ms. + const auto submission_waits = [](SubmitMutex& m, SubmitMutex::CreateKind kind, bool* passed) { + std::atomic in{false}; + std::atomic left{0}; + std::thread warm([&] { + const SubmitMutex::CompileTurn turn(m, SubmitMutex::CreateInfo{kind, "warm", "test", 0}, true); + *passed = turn.speculative(); + in = true; + std::this_thread::sleep_for(150ms); + left = now_ms(); + }); + bool entered = false; + for (int i = 0; i < 2000 && !in; ++i) std::this_thread::sleep_for(1ms); + entered = in.load(); + std::int64_t submitted = 0; + { + const SubmitMutex::SubmitTurn turn(m); + submitted = now_ms(); + } + warm.join(); + return entered && submitted >= left.load(); + }; + using Kind = SubmitMutex::CreateKind; + // Adaptive gate: a submission is not held by a compute warm-up in the driver. + { + auto m = make(Mode::adaptive); + bool passed = false; + EXPECT(!submission_waits(*m, Kind::compute, &passed)); + EXPECT(passed && m->speculative_compiles() == 0); + } + // ... but by a graphics or mesh prewarm (the calibration measures compute creates only). + { + auto m = make(Mode::adaptive); + bool passed = true; + EXPECT(submission_waits(*m, Kind::graphics, &passed)); + EXPECT(!passed); + } + // Classic gate (also auto's choice where the driver holds submissions) and LEONIDA_COMPILE_GATE=1: a compute + // warm-up holds submissions; with =1 a graphics prewarm does too. + for (const Mode mode : {Mode::classic, Mode::always}) { + auto m = make(mode); + bool passed = true; + EXPECT(submission_waits(*m, Kind::compute, &passed)); + EXPECT(!passed); + if (mode == Mode::always) { + passed = true; + EXPECT(submission_waits(*m, Kind::graphics, &passed)); + EXPECT(!passed); + } + } + // LEONIDA_COMPILE_GATE_WARMUPS=0: a compute warm-up passes with the classic gate and =1 too; graphics ones never. + for (const Mode mode : {Mode::classic, Mode::always}) { + auto m = make(mode, {true, 1, true}); + bool passed = false; + EXPECT(!submission_waits(*m, Kind::compute, &passed)); + EXPECT(passed); + if (mode == Mode::always) { + passed = true; + EXPECT(submission_waits(*m, Kind::graphics, &passed)); + EXPECT(!passed); + } + } + // LEONIDA_COMPILE_GATE_WARMUPS=1: a warm-up holds submissions with the adaptive gate too. + { + auto m = make(Mode::adaptive, {false, 1}); + bool passed = true; + EXPECT(submission_waits(*m, Kind::compute, &passed)); + EXPECT(!passed && m->speculative_compiles() == 0); + } + // A demanded create is still waited for, also beside a warm-up: the submission waits for the demanded one only. + { + auto m = make(Mode::adaptive); + std::atomic warm_in{false}, demand_in{false}, release_warm{false}; + std::atomic demand_left{0}, submitted{0}; + std::thread warm([&] { + const SubmitMutex::CompileTurn turn(*m, {}, true); + warm_in = true; + while (!release_warm) std::this_thread::sleep_for(1ms); + }); + std::thread demand([&] { + const SubmitMutex::CompileTurn turn(*m); + demand_in = true; + std::this_thread::sleep_for(200ms); + demand_left = now_ms(); + }); + EXPECT(eventually([&] { return warm_in.load() && demand_in.load(); })); + EXPECT(m->speculative_compiles() == 1); + std::thread submit([&] { + const SubmitMutex::SubmitTurn turn(*m); + submitted = now_ms(); + }); + submit.join(); + demand.join(); + EXPECT(submitted.load() >= demand_left.load()); // waited for the demanded create + EXPECT(warm_in.load() && !release_warm.load()); // and not for the warm-up, still in the driver + release_warm = true; + warm.join(); + EXPECT(m->speculative_compiles() == 0); + } + // At most warmup_max warm-ups in the driver; a demanded create does not wait for them. A max of 0 is taken as 1: + // it would keep every warm-up waiting for a place. + for (const unsigned max : {0u, 1u, 2u}) { + auto m = make(Mode::adaptive, {true, max}); + const int places = int((std::max)(max, 1u)); + std::atomic inside{0}, most{0}; + std::atomic release{false}; + std::vector warms; + for (int i = 0; i < 4; ++i) + warms.emplace_back([&] { + const SubmitMutex::CompileTurn turn(*m, {}, true); + const int now = ++inside; + int seen = most.load(); + while (now > seen && !most.compare_exchange_weak(seen, now)) { + } + while (!release) std::this_thread::sleep_for(1ms); + --inside; + }); + EXPECT(eventually([&] { return inside.load() == places; })); + std::this_thread::sleep_for(100ms); + EXPECT(inside.load() == places); // the others wait for a place + std::atomic demand_waited{-1}; + std::thread demand([&] { + const SubmitMutex::CompileTurn turn(*m); + demand_waited = turn.waited_ms(); + }); + demand.join(); + EXPECT(demand_waited.load() == 0); + release = true; + for (auto& t : warms) t.join(); + EXPECT(most.load() == places); + EXPECT(m->speculative_compiles() == 0); + } + // A warm-up never enters while a submission is in the driver. + { + auto m = make(Mode::adaptive); + std::atomic submitting{false}, release{false}; + std::atomic submit_left{0}, warm_entered{0}; + std::thread submit([&] { + const SubmitMutex::SubmitTurn turn(*m); + submitting = true; + while (!release) std::this_thread::sleep_for(1ms); + submit_left = now_ms(); + }); + EXPECT(eventually([&] { return submitting.load(); })); + std::thread warm([&] { + const SubmitMutex::CompileTurn turn(*m, {}, true); + warm_entered = now_ms(); + }); + std::this_thread::sleep_for(100ms); + EXPECT(warm_entered.load() == 0); + release = true; + submit.join(); + warm.join(); + EXPECT(warm_entered.load() >= submit_left.load()); + } + // The settings. + _putenv_s("LEONIDA_COMPILE_GATE_WARMUPS", "1"); + _putenv_s("LEONIDA_COMPILE_GATE_WARMUP_MAX", "3"); + auto policy = SubmitMutex::warmup_policy_from_environment(); + EXPECT(!policy.free && !policy.everywhere && policy.max == 3); + _putenv_s("LEONIDA_COMPILE_GATE_WARMUPS", "0"); + _putenv_s("LEONIDA_COMPILE_GATE_WARMUP_MAX", "0"); + policy = SubmitMutex::warmup_policy_from_environment(); + EXPECT(policy.free && policy.everywhere && policy.max == 1); + _putenv_s("LEONIDA_COMPILE_GATE_WARMUP_MAX", "x"); + EXPECT(SubmitMutex::warmup_policy_from_environment().max == 1); + _putenv_s("LEONIDA_COMPILE_GATE_WARMUPS", ""); + _putenv_s("LEONIDA_COMPILE_GATE_WARMUP_MAX", ""); + policy = SubmitMutex::warmup_policy_from_environment(); + EXPECT(policy.free && !policy.everywhere && policy.max == 1); + std::printf("warm-up gate: adaptive gate: compute warm-ups pass, graphics prewarms and demanded creates hold, " + "warm-ups capped (max 0 taken as 1); classic and =1: warm-ups hold unless " + "LEONIDA_COMPILE_GATE_WARMUPS=0\n"); + 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"); @@ -390,8 +595,34 @@ int gate_adapts() { EXPECT(entered); EXPECT(held == Verdict::driver_holds && still == Verdict::driver_holds); EXPECT(!ahead && entered_at >= released_at.load()); + // A warm-up of that kind is waited for too: a submission beside it would wait in the driver instead. + m.set_warmup_policy({true, 1}); + std::atomic warm_in{false}, warm_release{false}; + std::thread warm([&] { + const SubmitMutex::CompileTurn turn(m, {}, true); + EXPECT_VOID(turn.speculative()); + warm_in = true; + while (!warm_release) std::this_thread::sleep_for(1ms); + }); + const bool warm_entered = eventually([&] { return warm_in.load(); }); + std::atomic warm_released_at{0}; + std::thread warm_releaser([&] { + std::this_thread::sleep_for(300ms); + warm_released_at = now_ms(); + warm_release = true; + }); + std::int64_t warm_submitted = 0; + { + const SubmitMutex::SubmitTurn turn(m); + warm_submitted = now_ms(); + } + warm_releaser.join(); + warm.join(); + EXPECT(warm_entered && warm_submitted >= warm_released_at.load()); + if (failed_void) return 1; } std::printf("compile gate: a probe held 2.1 s keeps it on, and the create is then waited out past the limit\n"); + std::printf("compile gate: a warm-up of a kind that holds submissions is waited for as a demanded create is\n"); // A slow submission that comes back with the create beside it still there was not held by it: no verdict. { SubmitMutex m; @@ -952,7 +1183,7 @@ int gate_classic() { 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() || warmups_do_not_hold_the_gate() || gate_wait_limit() || gate_adapts() || gate_probe_budget() || gate_per_kind() || gate_review_cases() || gate_second_review() || gate_classic()) return 1;