From de3c463e2f226359fc7771e4673eaa7ece38a7fe Mon Sep 17 00:00:00 2001 From: IlyassRS Date: Wed, 25 Nov 2026 12:20:17 +0100 Subject: [PATCH 1/2] d3d12: finish CompileActivity before checkpointing and retry deferred cache snapshots --- runtime/shims/d3d12_pipeline_checkpoint.h | 15 +++++- runtime/shims/d3d12_vulkan_device.cpp | 56 ++++++++++++++-------- runtime/src/pipeline_cache_store.h | 52 ++++++++++++++++++++ runtime/test/pipeline_cache_store_test.cpp | 54 ++++++++++++++++++++- 4 files changed, 154 insertions(+), 23 deletions(-) diff --git a/runtime/shims/d3d12_pipeline_checkpoint.h b/runtime/shims/d3d12_pipeline_checkpoint.h index 7756818a9b..4ca66374e4 100644 --- a/runtime/shims/d3d12_pipeline_checkpoint.h +++ b/runtime/shims/d3d12_pipeline_checkpoint.h @@ -12,8 +12,19 @@ namespace rt::d3d12 { struct CompileActivity { std::atomic& count; - explicit CompileActivity(std::atomic& active) : count(active) { ++count; } - ~CompileActivity() { --count; } + bool active = true; + explicit CompileActivity(std::atomic& c) : count(c) { + count.fetch_add(1, std::memory_order_acq_rel); + } + void finish() { + if (active) { + count.fetch_sub(1, std::memory_order_acq_rel); + active = false; + } + } + ~CompileActivity() { finish(); } + CompileActivity(const CompileActivity&) = delete; + CompileActivity& operator=(const CompileActivity&) = delete; }; inline bool direct_session_cache() { diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 49743d0c1d..fd2f386a25 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -1420,30 +1420,35 @@ bool startup_cache_checkpoints_enabled() { } // Writes the session cache at a startup checkpoint or during device teardown. The size cap applies to either write. -void save_session_pipeline_cache(const leonida::VulkanDeviceFunctions& vk, VkDevice device, VkPipelineCache cache, - bool checkpoint = false) { - if (!cache || !vk.vkGetPipelineCacheData) return; +leonida::pipeline_cache_store::CheckpointOutcome save_session_pipeline_cache( + const leonida::VulkanDeviceFunctions& vk, VkDevice device, VkPipelineCache cache, bool checkpoint = false, + const std::atomic* active_compiles = nullptr) { + using leonida::pipeline_cache_store::CheckpointOutcome; + if (!cache || !vk.vkGetPipelineCacheData) return CheckpointOutcome::skipped; auto limit = pipeline_cache_save_limit(); // The output is an opaque Vulkan blob. A prefix is not an incremental cache. if (checkpoint) limit = (std::min)(limit, std::size_t{512} << 20); - if (!limit) return; + if (!limit) return CheckpointOutcome::skipped; const auto start = compile_us(); std::size_t size = 0; if (vk.vkGetPipelineCacheData(device, cache, &size, nullptr) != VK_SUCCESS || - size <= sizeof(VkPipelineCacheHeaderVersionOne)) return; + size <= sizeof(VkPipelineCacheHeaderVersionOne)) + return CheckpointOutcome::skipped; if (size > limit) { std::fprintf(stderr, "[rt] d3d12: pipeline cache is %zu bytes, over the %zu-byte write limit\n", size, limit); - return; + return CheckpointOutcome::skipped; } const auto queried = compile_us(); - // A size query can itself serialize the driver cache. Let newly queued work run first. - if (checkpoint && PipelineCompiler::instance().has_compile_waiter()) return; + // A size query can itself serialize the driver cache. Let newly queued or active compiles finish first. + if (checkpoint && ((active_compiles && active_compiles->load(std::memory_order_acquire)) || + PipelineCompiler::instance().has_compile_waiter())) + return CheckpointOutcome::deferred; const auto path = pipeline_cache_session_path(); - if (path.empty()) return; + if (path.empty()) return CheckpointOutcome::skipped; CacheStagingFile staging(path, size); if (!staging.data) { std::fprintf(stderr, "[rt] d3d12: could not map staging file for %zu pipeline cache bytes\n", size); - return; + return CheckpointOutcome::skipped; } auto written = size; const auto result = vk.vkGetPipelineCacheData(device, cache, &written, staging.data); @@ -1456,6 +1461,7 @@ void save_session_pipeline_cache(const leonida::VulkanDeviceFunctions& vk, VkDev compile_us()-filled, int(ok), int(result)); if (ok) std::fprintf(stderr, "[rt] d3d12: wrote %zu bytes to pipeline_cache.session.bin\n", written); else std::fprintf(stderr, "[rt] d3d12: pipeline cache snapshot incomplete (%d); previous session retained\n", int(result)); + return ok ? CheckpointOutcome::saved : CheckpointOutcome::skipped; } // A shader whose subgroup size cannot be had runs at the device's: inexact if it crosses the halves of a wave64 (or, @@ -1961,7 +1967,8 @@ struct DeviceState { leonida::cached_pso::Identity pso_identity; // what GetCachedBlob records and CachedPSO is checked against std::once_flag pipeline_cache_once; VkPipelineCache pipeline_cache_handle = VK_NULL_HANDLE; - std::atomic next_checkpoint{GetTickCount64() + 120000}; + leonida::pipeline_cache_store::CheckpointSchedule checkpoint_schedule{ + GetTickCount64() + leonida::pipeline_cache_store::default_checkpoint_interval_ms}; std::atomic active_compiles{0}; VkPipelineCache pipeline_cache() { @@ -9079,7 +9086,7 @@ class PipelineState final // failure is logged and not retried. VkPipeline build_compute(const char* trigger) { CompileActivity activity(device_.active_compiles); - std::lock_guard lock(variants_mutex_); + std::unique_lock lock(variants_mutex_); if (pipeline_.pipeline || compute_failed_) return pipeline_.pipeline; compute_failed_ = true; // until the pipeline exists auto& vk = device_.context->vk; @@ -9134,9 +9141,11 @@ class PipelineState final if (took >= 2000) std::fprintf(stderr, "[rt] d3d12: compute pipeline %s took %.1f s to build (%zu B SPIR-V)\n", entry->name.c_str(), double(took) / 1000.0, bytes); - maybe_checkpoint(cache); pipeline_.pipeline = built; compute_failed_ = false; + lock.unlock(); + activity.finish(); + maybe_checkpoint(cache); return built; } @@ -9147,24 +9156,30 @@ class PipelineState final // The startup cache checkpoint is the device's job, not this PSO's: ~Device cancels it before the cache goes. void maybe_checkpoint(VkPipelineCache cache) { if (!cache || !startup_cache_checkpoints_enabled()) return; - auto now = GetTickCount64(); - auto next = device_.next_checkpoint.load(); - if (now < next || !device_.next_checkpoint.compare_exchange_strong(next, ULLONG_MAX)) return; + using leonida::pipeline_cache_store::CheckpointOutcome; + const auto now = GetTickCount64(); + if (!device_.checkpoint_schedule.try_claim(now)) return; DeviceState* const state = &device_; bool queued = false; try { queued = PipelineCompiler::instance().submit(state, state, SIZE_MAX, [state, cache] { + CheckpointOutcome outcome = CheckpointOutcome::deferred; try { - if (!state->active_compiles.load() && !PipelineCompiler::instance().has_compile_waiter()) - save_session_pipeline_cache(state->context->vk, state->info.device, cache, true); + if (!state->active_compiles.load(std::memory_order_acquire) && + !PipelineCompiler::instance().has_compile_waiter()) { + outcome = save_session_pipeline_cache(state->context->vk, state->info.device, cache, true, + &state->active_compiles); + } } catch (const std::exception& e) { + outcome = CheckpointOutcome::skipped; std::fprintf(stderr, "[rt] pipeline cache checkpoint failed: %s\n", e.what()); } - state->next_checkpoint.store(GetTickCount64() + 120000); + state->checkpoint_schedule.complete(GetTickCount64(), outcome); }); } catch (...) { } - if (!queued) device_.next_checkpoint.store(now + 120000); // refused after an exit stop, or out of memory + // Refused after an exit stop, or out of memory. + if (!queued) device_.checkpoint_schedule.complete(now, CheckpointOutcome::skipped); } VkPipeline queue_build(bool compute, std::uint32_t unbound, std::size_t warm_bytes = 0) { @@ -9439,6 +9454,7 @@ class PipelineState final pipeline = VK_NULL_HANDLE; } drop_modules(); + activity.finish(); if (pipeline) maybe_checkpoint(cache); return pipeline; } diff --git a/runtime/src/pipeline_cache_store.h b/runtime/src/pipeline_cache_store.h index 6d519d99b1..759dee0879 100644 --- a/runtime/src/pipeline_cache_store.h +++ b/runtime/src/pipeline_cache_store.h @@ -14,6 +14,7 @@ // part of Vulkan's ABI, not something a driver chooses. #pragma once +#include #include #include #include @@ -66,4 +67,55 @@ inline bool header_matches(const void* data, std::size_t size, const cached_pso: std::memcmp(bytes + 16, identity.cache_uuid, sizeof(identity.cache_uuid)) == 0; } +inline constexpr std::uint64_t default_checkpoint_interval_ms = 120000; + +enum class CheckpointOutcome : std::uint8_t { + saved, // snapshot written; wait interval_ms before the next checkpoint + deferred, // skipped because a compile was active or queued; retry on the next compile completion + skipped, // empty cache, over size cap, or I/O failure; wait interval_ms before retrying +}; + +// Coalesces startup pipeline-cache checkpoint scheduling across compiling threads. +// At most one checkpoint runs at a time; a deferred checkpoint stays eligible immediately instead of burning the +// full cooldown interval. +class CheckpointSchedule { +public: + explicit CheckpointSchedule(std::uint64_t initial_due_ms, + std::uint64_t interval_ms = default_checkpoint_interval_ms) + : next_due_ms_(clamp_due(initial_due_ms)), interval_ms_(interval_ms) {} + + bool try_claim(std::uint64_t now_ms) { + std::uint64_t expected = next_due_ms_.load(std::memory_order_acquire); + while (expected != in_flight && now_ms >= expected) { + if (next_due_ms_.compare_exchange_weak(expected, in_flight, std::memory_order_acq_rel, + std::memory_order_acquire)) { + return true; + } + } + return false; + } + + void complete(std::uint64_t now_ms, CheckpointOutcome outcome) { + const std::uint64_t next = + outcome == CheckpointOutcome::deferred ? clamp_due(now_ms) : saturate_add(now_ms, interval_ms_); + next_due_ms_.store(next, std::memory_order_release); + } + + void abort_claim(std::uint64_t now_ms) { next_due_ms_.store(clamp_due(now_ms), std::memory_order_release); } + + std::uint64_t next_due_ms() const { return next_due_ms_.load(std::memory_order_acquire); } + bool in_progress() const { return next_due_ms_.load(std::memory_order_acquire) == in_flight; } + +private: + static constexpr std::uint64_t in_flight = (std::numeric_limits::max)(); + static constexpr std::uint64_t max_due = in_flight - 1; + static std::uint64_t clamp_due(std::uint64_t t) { return t > max_due ? max_due : t; } + static std::uint64_t saturate_add(std::uint64_t a, std::uint64_t b) { + return a > max_due - b ? max_due : a + b; + } + + std::atomic next_due_ms_; + std::uint64_t interval_ms_; +}; + } // namespace leonida::pipeline_cache_store diff --git a/runtime/test/pipeline_cache_store_test.cpp b/runtime/test/pipeline_cache_store_test.cpp index dbf174768b..f842cff246 100644 --- a/runtime/test/pipeline_cache_store_test.cpp +++ b/runtime/test/pipeline_cache_store_test.cpp @@ -87,7 +87,59 @@ static int test_header() { return 0; } +static int test_checkpoint_schedule() { + using store::CheckpointOutcome; + using store::CheckpointSchedule; + + CheckpointSchedule schedule(120000, 120000); + CHECK(!schedule.in_progress()); + CHECK(!schedule.try_claim(0)); + CHECK(!schedule.try_claim(119999)); + + // At the due time, one caller claims the slot; concurrent callers see in_progress and are coalesced. + CHECK(schedule.try_claim(120000)); + CHECK(schedule.in_progress()); + CHECK(!schedule.try_claim(120000)); + CHECK(!schedule.try_claim(240000)); + + // A deferred checkpoint (active compile or waiter) must NOT burn the 120 s cooldown: it becomes immediately + // eligible on the next compile completion. + schedule.complete(120050, CheckpointOutcome::deferred); + CHECK(!schedule.in_progress()); + CHECK(schedule.next_due_ms() == 120050); + CHECK(!schedule.try_claim(120049)); + CHECK(schedule.try_claim(120050)); + + // Queue failure (abort_claim) also preserves immediate retry without burning the cooldown. + schedule.abort_claim(120060); + CHECK(!schedule.in_progress()); + CHECK(schedule.next_due_ms() == 120060); + CHECK(schedule.try_claim(120060)); + + // A saved snapshot advances the deadline by interval_ms. + schedule.complete(125000, CheckpointOutcome::saved); + CHECK(!schedule.in_progress()); + CHECK(schedule.next_due_ms() == 245000); + CHECK(!schedule.try_claim(244999)); + CHECK(schedule.try_claim(245000)); + + // A skipped snapshot (empty cache, over limit, or I/O error) backs off by interval_ms so disk is not hammered. + schedule.complete(245010, CheckpointOutcome::skipped); + CHECK(schedule.next_due_ms() == 365010); + CHECK(!schedule.try_claim(365009)); + + // Near-UINT64_MAX timestamps saturate below the in_flight sentinel rather than wrapping or sticking in_flight. + const std::uint64_t near_max = (std::numeric_limits::max)() - 10; + CheckpointSchedule hi(near_max, 120000); + CHECK(hi.try_claim(near_max)); + hi.complete(near_max, CheckpointOutcome::saved); + CHECK(!hi.in_progress()); + CHECK(hi.next_due_ms() == (std::numeric_limits::max)() - 1); + return 0; +} + int main() { if (const int failed = test_limit()) return failed; - return test_header(); + if (const int failed = test_header()) return failed; + return test_checkpoint_schedule(); } From 90506453e8da6494a41fc9b024f766d8d4d7876f Mon Sep 17 00:00:00 2001 From: IlyassRS Date: Wed, 25 Nov 2026 13:18:37 +0100 Subject: [PATCH 2/2] d3d12: schedule startup cache checkpoints at 30s/priority-1 and yield only to priority-0 waiters Incorporate @DadaCap's RTX 5080 retail boot findings (#1608#issuecomment-7161936928) into #1756 so startup checkpoints actually persist compiled pipelines before a world-start crash: - Lower default checkpoint initial delay and post-save interval from 120 s to 30 s, with a 5 s retry backoff when deferred (CheckpointSchedule). - Submit checkpoints at priority 1 (behind priority-0 awaited first-use draws/ dispatches, ahead of speculative warm-up builds keyed by SPIR-V byte size). - Narrow PipelineCompiler::has_compile_waiter() to priority-0 awaited jobs only and drop the cross-thread active_compiles gate on checkpoints (vkGetPipelineCacheData is thread-safe against concurrent vkCreate*Pipelines and takes ~7-28 ms). - Update docs/d3d12_vulkan.md and runtime/test/pipeline_cache_store_test.cpp. --- docs/d3d12_vulkan.md | 5 +- runtime/shims/d3d12_pipeline_compile.h | 4 +- runtime/shims/d3d12_vulkan_device.cpp | 34 ++++++-------- runtime/src/pipeline_cache_store.h | 23 ++++++---- runtime/test/pipeline_cache_store_test.cpp | 53 ++++++++++++---------- 5 files changed, 64 insertions(+), 55 deletions(-) diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 53d9747968..cd007914c8 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -2363,8 +2363,9 @@ Pipeline creation: compute programs and drops their dispatches, direct and indirect; the first dropped dispatch of each is logged. It is a way past a program that hangs the GPU while the program or its input is fixed, not a fix. `LEONIDA_STARTUP_CACHE_CHECKPOINTS=1` also schedules a snapshot after a successful pipeline build, - at most once per 120 seconds. It starts on the compile worker when queued builds have drained and the device - has no active builds. These snapshots have an additional 512 MiB ceiling. The driver fills a mapped staging + starting 30 seconds after device creation and at most once per 30 seconds after a save. It queues on the + compile worker at priority 1 (behind priority-0 awaited first-use builds and ahead of speculative warm-up), + yielding only when a priority-0 build is queued (retrying after 5 seconds). These snapshots have an additional 512 MiB ceiling. The driver fills a mapped staging file directly. A successful snapshot atomically replaces the session. An interrupted write preserves the previous cache; the next launch removes unheld staging files older than ten minutes. `LEONIDA_TRACE_PIPELINE_COMPILE=1` records each graphics and compute build's trigger, thread, elapsed time, diff --git a/runtime/shims/d3d12_pipeline_compile.h b/runtime/shims/d3d12_pipeline_compile.h index 3a5e045840..191fce1761 100644 --- a/runtime/shims/d3d12_pipeline_compile.h +++ b/runtime/shims/d3d12_pipeline_compile.h @@ -138,9 +138,11 @@ class PipelineCompiler { return closed_; } + // Priority 0 is an awaited first-use draw/dispatch build; priority 1 is a cache checkpoint; higher priorities are + // speculative compute warm-up builds (keyed by SPIR-V byte size). Only priority-0 waiters preempt a checkpoint. bool has_compile_waiter() { std::lock_guard lock(mutex_); - return !jobs_.empty() && jobs_.begin()->first != SIZE_MAX; + return !jobs_.empty() && jobs_.begin()->first == 0; } // Returns when no job is queued or running. A job owns its pipeline state, and through it the device, until it // finishes; a caller that counts references (rt::d3d12_xs::device_proxy_ref_count) waits here first. diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index fd2f386a25..c5f963e7b4 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -1421,8 +1421,7 @@ bool startup_cache_checkpoints_enabled() { // Writes the session cache at a startup checkpoint or during device teardown. The size cap applies to either write. leonida::pipeline_cache_store::CheckpointOutcome save_session_pipeline_cache( - const leonida::VulkanDeviceFunctions& vk, VkDevice device, VkPipelineCache cache, bool checkpoint = false, - const std::atomic* active_compiles = nullptr) { + const leonida::VulkanDeviceFunctions& vk, VkDevice device, VkPipelineCache cache, bool checkpoint = false) { using leonida::pipeline_cache_store::CheckpointOutcome; if (!cache || !vk.vkGetPipelineCacheData) return CheckpointOutcome::skipped; auto limit = pipeline_cache_save_limit(); @@ -1439,10 +1438,8 @@ leonida::pipeline_cache_store::CheckpointOutcome save_session_pipeline_cache( return CheckpointOutcome::skipped; } const auto queried = compile_us(); - // A size query can itself serialize the driver cache. Let newly queued or active compiles finish first. - if (checkpoint && ((active_compiles && active_compiles->load(std::memory_order_acquire)) || - PipelineCompiler::instance().has_compile_waiter())) - return CheckpointOutcome::deferred; + // Yield only if a priority-0 awaited draw/dispatch build arrived while querying cache size. + if (checkpoint && PipelineCompiler::instance().has_compile_waiter()) return CheckpointOutcome::deferred; const auto path = pipeline_cache_session_path(); if (path.empty()) return CheckpointOutcome::skipped; CacheStagingFile staging(path, size); @@ -9162,20 +9159,19 @@ class PipelineState final DeviceState* const state = &device_; bool queued = false; try { - queued = PipelineCompiler::instance().submit(state, state, SIZE_MAX, [state, cache] { - CheckpointOutcome outcome = CheckpointOutcome::deferred; - try { - if (!state->active_compiles.load(std::memory_order_acquire) && - !PipelineCompiler::instance().has_compile_waiter()) { - outcome = save_session_pipeline_cache(state->context->vk, state->info.device, cache, true, - &state->active_compiles); + queued = PipelineCompiler::instance().submit( + state, state, leonida::pipeline_cache_store::checkpoint_priority, [state, cache] { + CheckpointOutcome outcome = CheckpointOutcome::deferred; + try { + if (!PipelineCompiler::instance().has_compile_waiter()) { + outcome = save_session_pipeline_cache(state->context->vk, state->info.device, cache, true); + } + } catch (const std::exception& e) { + outcome = CheckpointOutcome::skipped; + std::fprintf(stderr, "[rt] pipeline cache checkpoint failed: %s\n", e.what()); } - } catch (const std::exception& e) { - outcome = CheckpointOutcome::skipped; - std::fprintf(stderr, "[rt] pipeline cache checkpoint failed: %s\n", e.what()); - } - state->checkpoint_schedule.complete(GetTickCount64(), outcome); - }); + state->checkpoint_schedule.complete(GetTickCount64(), outcome); + }); } catch (...) { } // Refused after an exit stop, or out of memory. diff --git a/runtime/src/pipeline_cache_store.h b/runtime/src/pipeline_cache_store.h index 759dee0879..52e2e85f0d 100644 --- a/runtime/src/pipeline_cache_store.h +++ b/runtime/src/pipeline_cache_store.h @@ -67,22 +67,25 @@ inline bool header_matches(const void* data, std::size_t size, const cached_pso: std::memcmp(bytes + 16, identity.cache_uuid, sizeof(identity.cache_uuid)) == 0; } -inline constexpr std::uint64_t default_checkpoint_interval_ms = 120000; +inline constexpr std::uint64_t default_checkpoint_interval_ms = 30000; +inline constexpr std::uint64_t default_checkpoint_retry_ms = 5000; +inline constexpr std::size_t checkpoint_priority = 1; // behind priority-0 awaited builds, ahead of warm-up (SPIR-V bytes) enum class CheckpointOutcome : std::uint8_t { saved, // snapshot written; wait interval_ms before the next checkpoint - deferred, // skipped because a compile was active or queued; retry on the next compile completion + deferred, // yielded to a priority-0 awaited build; retry after retry_ms skipped, // empty cache, over size cap, or I/O failure; wait interval_ms before retrying }; // Coalesces startup pipeline-cache checkpoint scheduling across compiling threads. -// At most one checkpoint runs at a time; a deferred checkpoint stays eligible immediately instead of burning the +// At most one checkpoint runs at a time; a deferred checkpoint backs off briefly (retry_ms) instead of burning the // full cooldown interval. class CheckpointSchedule { public: explicit CheckpointSchedule(std::uint64_t initial_due_ms, - std::uint64_t interval_ms = default_checkpoint_interval_ms) - : next_due_ms_(clamp_due(initial_due_ms)), interval_ms_(interval_ms) {} + std::uint64_t interval_ms = default_checkpoint_interval_ms, + std::uint64_t retry_ms = default_checkpoint_retry_ms) + : next_due_ms_(clamp_due(initial_due_ms)), interval_ms_(interval_ms), retry_ms_(retry_ms) {} bool try_claim(std::uint64_t now_ms) { std::uint64_t expected = next_due_ms_.load(std::memory_order_acquire); @@ -96,12 +99,13 @@ class CheckpointSchedule { } void complete(std::uint64_t now_ms, CheckpointOutcome outcome) { - const std::uint64_t next = - outcome == CheckpointOutcome::deferred ? clamp_due(now_ms) : saturate_add(now_ms, interval_ms_); - next_due_ms_.store(next, std::memory_order_release); + const std::uint64_t wait = outcome == CheckpointOutcome::deferred ? retry_ms_ : interval_ms_; + next_due_ms_.store(saturate_add(now_ms, wait), std::memory_order_release); } - void abort_claim(std::uint64_t now_ms) { next_due_ms_.store(clamp_due(now_ms), std::memory_order_release); } + void abort_claim(std::uint64_t now_ms) { + next_due_ms_.store(saturate_add(now_ms, retry_ms_), std::memory_order_release); + } std::uint64_t next_due_ms() const { return next_due_ms_.load(std::memory_order_acquire); } bool in_progress() const { return next_due_ms_.load(std::memory_order_acquire) == in_flight; } @@ -116,6 +120,7 @@ class CheckpointSchedule { std::atomic next_due_ms_; std::uint64_t interval_ms_; + std::uint64_t retry_ms_; }; } // namespace leonida::pipeline_cache_store diff --git a/runtime/test/pipeline_cache_store_test.cpp b/runtime/test/pipeline_cache_store_test.cpp index f842cff246..7d1352ae7d 100644 --- a/runtime/test/pipeline_cache_store_test.cpp +++ b/runtime/test/pipeline_cache_store_test.cpp @@ -91,46 +91,51 @@ static int test_checkpoint_schedule() { using store::CheckpointOutcome; using store::CheckpointSchedule; - CheckpointSchedule schedule(120000, 120000); + CHECK(store::default_checkpoint_interval_ms == 30000); + CHECK(store::default_checkpoint_retry_ms == 5000); + CHECK(store::checkpoint_priority == 1); + + CheckpointSchedule schedule(30000); CHECK(!schedule.in_progress()); CHECK(!schedule.try_claim(0)); - CHECK(!schedule.try_claim(119999)); + CHECK(!schedule.try_claim(29999)); // At the due time, one caller claims the slot; concurrent callers see in_progress and are coalesced. - CHECK(schedule.try_claim(120000)); + CHECK(schedule.try_claim(30000)); CHECK(schedule.in_progress()); - CHECK(!schedule.try_claim(120000)); - CHECK(!schedule.try_claim(240000)); + CHECK(!schedule.try_claim(30000)); + CHECK(!schedule.try_claim(90000)); - // A deferred checkpoint (active compile or waiter) must NOT burn the 120 s cooldown: it becomes immediately - // eligible on the next compile completion. - schedule.complete(120050, CheckpointOutcome::deferred); + // A deferred checkpoint (priority-0 awaited build queued) retries after retry_ms (5 s) rather than burning the + // full 30 s cooldown. + schedule.complete(30050, CheckpointOutcome::deferred); CHECK(!schedule.in_progress()); - CHECK(schedule.next_due_ms() == 120050); - CHECK(!schedule.try_claim(120049)); - CHECK(schedule.try_claim(120050)); + CHECK(schedule.next_due_ms() == 35050); + CHECK(!schedule.try_claim(35049)); + CHECK(schedule.try_claim(35050)); - // Queue failure (abort_claim) also preserves immediate retry without burning the cooldown. - schedule.abort_claim(120060); + // Queue failure (abort_claim) also retries after retry_ms. + schedule.abort_claim(35060); CHECK(!schedule.in_progress()); - CHECK(schedule.next_due_ms() == 120060); - CHECK(schedule.try_claim(120060)); + CHECK(schedule.next_due_ms() == 40060); + CHECK(!schedule.try_claim(40059)); + CHECK(schedule.try_claim(40060)); - // A saved snapshot advances the deadline by interval_ms. - schedule.complete(125000, CheckpointOutcome::saved); + // A saved snapshot advances the deadline by interval_ms (30 s). + schedule.complete(41000, CheckpointOutcome::saved); CHECK(!schedule.in_progress()); - CHECK(schedule.next_due_ms() == 245000); - CHECK(!schedule.try_claim(244999)); - CHECK(schedule.try_claim(245000)); + CHECK(schedule.next_due_ms() == 71000); + CHECK(!schedule.try_claim(70999)); + CHECK(schedule.try_claim(71000)); // A skipped snapshot (empty cache, over limit, or I/O error) backs off by interval_ms so disk is not hammered. - schedule.complete(245010, CheckpointOutcome::skipped); - CHECK(schedule.next_due_ms() == 365010); - CHECK(!schedule.try_claim(365009)); + schedule.complete(71010, CheckpointOutcome::skipped); + CHECK(schedule.next_due_ms() == 101010); + CHECK(!schedule.try_claim(101009)); // Near-UINT64_MAX timestamps saturate below the in_flight sentinel rather than wrapping or sticking in_flight. const std::uint64_t near_max = (std::numeric_limits::max)() - 10; - CheckpointSchedule hi(near_max, 120000); + CheckpointSchedule hi(near_max); CHECK(hi.try_claim(near_max)); hi.complete(near_max, CheckpointOutcome::saved); CHECK(!hi.in_progress());