diff options
| author | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-26 09:15:59 -0500 |
|---|---|---|
| committer | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-26 09:16:06 -0500 |
| commit | 32a04b17f9bf2304c8ca0d63dfb95a005317d79f (patch) | |
| tree | af19df2a040ee526ded2ee1310758e18053d3936 /runtime/src | |
| parent | e6a82e72b2a9f3a99c9119df4ee5ede30b683b1b (diff) | |
| parent | a42bffdc3e0d30347eedb3c433405e9a141cc06b (diff) | |
Merge #1914 from kawaii-shakal: a pipeline cache for the offline cache's misses (RT-HEAVY-PIPELINE-CACHE)
Head a42bffdc3. The conflict in docs/d3d12_vulkan.md keeps main's session-cache cap (#1886) and #1914's heavy cache text.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/heavy_pipeline_cache.h | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/runtime/src/heavy_pipeline_cache.h b/runtime/src/heavy_pipeline_cache.h new file mode 100644 index 000000000..8cad10e66 --- /dev/null +++ b/runtime/src/heavy_pipeline_cache.h | |||
| @@ -0,0 +1,94 @@ | |||
| 1 | // A pipeline cache that survives a crash, for the pipelines the offline corpus does not have (RT-HEAVY-PIPELINE-CACHE). | ||
| 2 | // | ||
| 3 | // The offline seed (pipeline_cache.bin, several GiB) covers what the `pipelines` build stage creates. A pipeline it | ||
| 4 | // misses is compiled on the spot, and some of those take the driver seconds each. pipeline_cache.session.bin cannot | ||
| 5 | // keep them across a crash: it is written at device teardown only, and a cache seeded from the corpus returns the | ||
| 6 | // whole corpus, over its write limit, so it is not written at all. | ||
| 7 | // | ||
| 8 | // So a pipeline is first created against the corpus cache with VK_PIPELINE_CREATE_FAIL_ON_PIPELINE_COMPILE_REQUIRED. | ||
| 9 | // When the driver would have to compile, it is created again against a cache of its own, seeded from | ||
| 10 | // pipeline_cache.heavy.bin. That cache holds only the corpus's misses, so it stays small. After a compile that took | ||
| 11 | // at least LEONIDA_HEAVY_PIPELINE_CACHE_MS (default 500 ms), it is written back to disk on a thread of its own: a | ||
| 12 | // staging file renamed over the old one, so a crash at any point leaves the last complete file. | ||
| 13 | // | ||
| 14 | // This header holds the pure decisions (the threshold and when to write). Vulkan and the file live in the device. | ||
| 15 | #pragma once | ||
| 16 | |||
| 17 | #include <cstdint> | ||
| 18 | #include <cstdlib> | ||
| 19 | |||
| 20 | namespace leonida::heavy_pipeline_cache { | ||
| 21 | |||
| 22 | inline constexpr std::uint32_t default_threshold_ms = 500; | ||
| 23 | |||
| 24 | struct Threshold { | ||
| 25 | std::uint32_t ms = default_threshold_ms; // 0: off | ||
| 26 | bool valid = true; // false: the text was not a number; ms stays the default | ||
| 27 | }; | ||
| 28 | |||
| 29 | // LEONIDA_HEAVY_PIPELINE_CACHE_MS: a compile at least this long schedules a write; 0 turns the heavy cache off. | ||
| 30 | inline Threshold parse_threshold(const char* text) { | ||
| 31 | if (!text || !*text) return {}; | ||
| 32 | std::uint64_t value = 0; | ||
| 33 | for (const char* p = text; *p; ++p) { | ||
| 34 | if (*p < '0' || *p > '9') return {default_threshold_ms, false}; | ||
| 35 | value = value * 10 + std::uint64_t(*p - '0'); | ||
| 36 | if (value > 3'600'000) return {default_threshold_ms, false}; // an hour: past it the text is a mistake | ||
| 37 | } | ||
| 38 | return {std::uint32_t(value), true}; | ||
| 39 | } | ||
| 40 | |||
| 41 | // When the writer thread writes. A heavy compile marks the cache dirty; the write waits `settle_ms` after the | ||
| 42 | // newest one (heavy compiles come in bursts at a load, and each write copies the whole heavy cache) but never more | ||
| 43 | // than `max_delay_ms` after the first unwritten one, so a long burst is still saved as it goes. A write that fails | ||
| 44 | // keeps the cache dirty and is tried again `retry_ms` later, the delay doubling with each failure in a row up to | ||
| 45 | // `max_retry_ms`, so a disk that stays full or a driver that keeps failing does not make the writer spin. | ||
| 46 | class WriteSchedule { | ||
| 47 | public: | ||
| 48 | static constexpr std::uint64_t settle_ms = 3000; | ||
| 49 | static constexpr std::uint64_t max_delay_ms = 20000; | ||
| 50 | static constexpr std::uint64_t retry_ms = 5000; | ||
| 51 | static constexpr std::uint64_t max_retry_ms = 120000; | ||
| 52 | |||
| 53 | void note_heavy(std::uint64_t now_ms) { | ||
| 54 | if (!dirty_) first_ = now_ms; | ||
| 55 | dirty_ = true; | ||
| 56 | last_ = now_ms; | ||
| 57 | ++generation_; | ||
| 58 | } | ||
| 59 | bool dirty() const { return dirty_; } | ||
| 60 | // The time to write at; only meaningful while dirty. | ||
| 61 | std::uint64_t due_ms() const { | ||
| 62 | const std::uint64_t settled = last_ + settle_ms, latest = first_ + max_delay_ms; | ||
| 63 | const std::uint64_t at = settled < latest ? settled : latest; | ||
| 64 | return at > retry_at_ ? at : retry_at_; | ||
| 65 | } | ||
| 66 | bool due(std::uint64_t now_ms) const { return dirty_ && now_ms >= due_ms(); } | ||
| 67 | // Taken when a write starts; the write covers every heavy compile noted up to then. | ||
| 68 | std::uint64_t generation() const { return generation_; } | ||
| 69 | // A write that started at `generation` and `started_ms` is done. A heavy compile noted after it started, even in | ||
| 70 | // the same millisecond tick, keeps the cache dirty for the next write, due as if it were first noted then. | ||
| 71 | void written(std::uint64_t generation, std::uint64_t started_ms) { | ||
| 72 | failures_ = 0; | ||
| 73 | retry_at_ = 0; | ||
| 74 | if (generation_ == generation) dirty_ = false; | ||
| 75 | else first_ = started_ms; | ||
| 76 | } | ||
| 77 | // A write that started at `started_ms` failed: the cache stays dirty and the next try waits out the backoff. | ||
| 78 | void failed(std::uint64_t started_ms) { | ||
| 79 | std::uint64_t delay = retry_ms; | ||
| 80 | for (std::uint32_t i = 0; i < failures_ && delay < max_retry_ms; ++i) delay *= 2; | ||
| 81 | if (delay > max_retry_ms) delay = max_retry_ms; | ||
| 82 | if (failures_ < 64) ++failures_; | ||
| 83 | retry_at_ = started_ms + delay; | ||
| 84 | } | ||
| 85 | std::uint32_t failures() const { return failures_; } | ||
| 86 | |||
| 87 | private: | ||
| 88 | bool dirty_ = false; | ||
| 89 | std::uint64_t first_ = 0, last_ = 0, generation_ = 0; | ||
| 90 | std::uint32_t failures_ = 0; // failed writes in a row | ||
| 91 | std::uint64_t retry_at_ = 0; // no write before this time after a failure | ||
| 92 | }; | ||
| 93 | |||
| 94 | } // namespace leonida::heavy_pipeline_cache | ||