diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index c2e36319e2..473c8f4414 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -2375,6 +2375,16 @@ Pipeline creation: The files must match the Vulkan vendor, device and pipeline-cache UUID. Vulkan supplies an opaque snapshot. Replacing part of the previous file would corrupt it. `LEONIDA_PIPELINE_CACHE_BYTES` caps session writes (256 MiB by default, zero disables the session file). + A driver can stop keeping new pipelines once its cache is large, with no error. On NVIDIA 610.88 (RTX 5060 Ti), + a 1.23 GB or 1.35 GB cache came back from `vkGetPipelineCacheData` the same size to the byte after a 2 s compile + (with the 1.35 GB one the next process compiled the pipeline again; a hit then comes only from the driver's own + disk cache), where an empty cache grew ~210 KiB and served the next process in 0.01 s. In the game, seeded with + the 1.23 GB file, 68 of ~110 builds over 200 ms were slow again in the next run; started with no seed, 5 of 164 + were. An older cache of other pipelines stopped growing at 4.9 GB, so the point is not a fixed size. The runtime + takes the cache's size when it creates it and at each session save, counts the title's builds that took at least + 200 ms (a hit takes a few milliseconds), and says once when 16 or more of them compiled while the cache grew by + less than 64 KiB. The remedy is a smaller seed: without `pipeline_cache.bin`, the session file holds only what the + title builds. `LEONIDA_SKIP_COMPUTE=[,...]` (`all`: every compute program) never builds the matching 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. diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 45f899b2f3..1da16e67f2 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -1421,6 +1421,41 @@ bool startup_cache_checkpoints_enabled() { } // Writes the session cache at a startup checkpoint or during device teardown. The size cap applies to either write. +// RT-PIPELINE-CACHE-GROWTH-STALL: says once when the title's builds keep compiling but the driver's cache stops +// growing, so the next run would compile them again (pipeline_cache_store::GrowthWatch). The size is taken when the +// cache is created and at every session save, also one over the write limit. +std::mutex& pipeline_cache_growth_mutex() { + static std::mutex mutex; + return mutex; +} + +leonida::pipeline_cache_store::GrowthWatch& pipeline_cache_growth() { + static leonida::pipeline_cache_store::GrowthWatch watch; + return watch; +} + +void note_pipeline_cache_size(const leonida::VulkanDeviceFunctions& vk, VkDevice device, VkPipelineCache cache) { + std::size_t size = 0; + if (!cache || !vk.vkGetPipelineCacheData || vk.vkGetPipelineCacheData(device, cache, &size, nullptr) != VK_SUCCESS) + return; + std::lock_guard lock(pipeline_cache_growth_mutex()); + pipeline_cache_growth().observe(size, leonida::pipeline_profile::compiled_builds().load()); +} + +void report_pipeline_cache_growth(std::size_t size) { + const auto compiled = leonida::pipeline_profile::compiled_builds().load(); + std::lock_guard lock(pipeline_cache_growth_mutex()); + auto& watch = pipeline_cache_growth(); + const std::size_t before = watch.bytes(); + const unsigned long long builds = compiled - watch.compiled(); + if (watch.stalled(size, compiled)) + std::fprintf(stderr, "[rt] d3d12: the Vulkan pipeline cache grew %zu bytes (to %zu) over %llu pipeline builds " + "that compiled: the driver no longer keeps new pipelines, so the next run compiles them " + "again. A large pipeline_cache.bin or pipeline_cache.session.bin can cause it " + "(docs/d3d12_vulkan.md, pipeline caches)\n", + size > before ? size - before : 0, size, builds); +} + void save_session_pipeline_cache(const leonida::VulkanDeviceFunctions& vk, VkDevice device, VkPipelineCache cache, bool checkpoint = false) { if (!cache || !vk.vkGetPipelineCacheData) return; @@ -1432,6 +1467,7 @@ void save_session_pipeline_cache(const leonida::VulkanDeviceFunctions& vk, VkDev std::size_t size = 0; if (vk.vkGetPipelineCacheData(device, cache, &size, nullptr) != VK_SUCCESS || size <= sizeof(VkPipelineCacheHeaderVersionOne)) return; + report_pipeline_cache_growth(size); if (size > limit) { std::fprintf(stderr, "[rt] d3d12: pipeline cache is %zu bytes, over the %zu-byte write limit\n", size, limit); return; @@ -1984,6 +2020,7 @@ struct DeviceState { else if (!direct_session_cache()) merge_session_pipeline_cache(vk, info.device, pipeline_cache_handle, pso_identity); } + note_pipeline_cache_size(vk, info.device, pipeline_cache_handle); }); // Without EXTERNALLY_SYNCHRONIZED_BIT the driver synchronizes concurrent pipeline creation using this cache. return pipeline_cache_handle; diff --git a/runtime/src/pipeline_cache_store.h b/runtime/src/pipeline_cache_store.h index 6d519d99b1..19913c97f7 100644 --- a/runtime/src/pipeline_cache_store.h +++ b/runtime/src/pipeline_cache_store.h @@ -66,4 +66,42 @@ 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; } +// Whether the driver still keeps the pipelines the title compiles (RT-PIPELINE-CACHE-GROWTH-STALL). A driver can stop +// adding to a large cache without any error. Measured on NVIDIA 610.88 (RTX 5060 Ti): after a 2 s compile, a 1.23 GB +// or 1.35 GB cache came back from vkGetPipelineCacheData the same size to the byte, where an empty cache grew ~210 KiB +// for the same module; an older cache of other pipelines stopped at 4.9 GB, so there is no fixed size to test. +// Instead two sizes of the live cache are compared with the builds that compiled in between (pipeline_profile:: +// compiled_builds): each one adds tens to hundreds of KiB when the driver keeps it. +inline constexpr unsigned long long stalled_after_builds = 16; +inline constexpr std::size_t stalled_below_bytes = std::size_t{64} << 10; // 64 KiB + +class GrowthWatch { +public: + // The cache's size and the compiled-build count when it is created, or at a save. + void observe(std::size_t bytes, unsigned long long compiled) { + bytes_ = bytes; + compiled_ = compiled; + known_ = true; + } + + // At a later size: true once, the first time at least stalled_after_builds builds compiled since the last + // observation while the cache grew by less than stalled_below_bytes. The new size is recorded either way. + bool stalled(std::size_t bytes, unsigned long long compiled) { + const bool stall = known_ && !reported_ && compiled >= compiled_ && compiled - compiled_ >= stalled_after_builds && + bytes < bytes_ + stalled_below_bytes; + if (stall) reported_ = true; + observe(bytes, compiled); + return stall; + } + + std::size_t bytes() const { return bytes_; } + unsigned long long compiled() const { return compiled_; } + +private: + std::size_t bytes_ = 0; + unsigned long long compiled_ = 0; + bool known_ = false; + bool reported_ = false; +}; + } // namespace leonida::pipeline_cache_store diff --git a/runtime/src/pipeline_profile.h b/runtime/src/pipeline_profile.h index f81c00c7de..965d8f5255 100644 --- a/runtime/src/pipeline_profile.h +++ b/runtime/src/pipeline_profile.h @@ -39,23 +39,33 @@ inline long long thread_cpu_us() { return -1; } +// Title pipeline builds that took long enough to have compiled rather than come from the pipeline cache: a cache hit +// takes a few milliseconds, also while the title keeps every core busy. Counted whether or not profiling is on; the +// session cache save compares the count with how much the cache grew (pipeline_cache_store::GrowthWatch). +inline constexpr long long compiled_build_us = 200000; // 200 ms +inline std::atomic& compiled_builds() { + static std::atomic count{0}; + return count; +} + class DriverCall { public: DriverCall(const char* stage, const char* origin, const char* program, const char* fragment = "-", - unsigned unbound = 0) { + unsigned unbound = 0) + : start_(std::chrono::steady_clock::now()) { if (!enabled()) return; static std::atomic sequence{0}; id_ = sequence.fetch_add(1, std::memory_order_relaxed) + 1; - start_ = std::chrono::steady_clock::now(); cpu_start_ = thread_cpu_us(); std::fprintf(stderr, "[rt] pso-driver begin id=%llu unix_ms=%lld stage=%s origin=%s program=%s ps=%s mask=%u\n", id_, unix_ms(), stage, origin, program, fragment, unbound); } void finish(int result) const { - if (!id_) return; const auto us = std::chrono::duration_cast( std::chrono::steady_clock::now() - start_).count(); + if (result == 0 && us >= compiled_build_us) compiled_builds().fetch_add(1, std::memory_order_relaxed); + if (!id_) return; const long long cpu = thread_cpu_us(); std::fprintf(stderr, "[rt] pso-driver end id=%llu unix_ms=%lld us=%lld cpu_us=%lld result=%d\n", id_, unix_ms(), static_cast(us), cpu >= 0 && cpu_start_ >= 0 ? cpu - cpu_start_ : -1, diff --git a/runtime/test/pipeline_cache_store_test.cpp b/runtime/test/pipeline_cache_store_test.cpp index dbf174768b..b226233cde 100644 --- a/runtime/test/pipeline_cache_store_test.cpp +++ b/runtime/test/pipeline_cache_store_test.cpp @@ -87,7 +87,48 @@ static int test_header() { return 0; } +static int test_growth() { + using store::GrowthWatch; + constexpr auto builds = store::stalled_after_builds; + constexpr auto kept = store::stalled_below_bytes; + // No size at creation: nothing to compare with yet, and the first save becomes the reference. + GrowthWatch fresh; + CHECK(!fresh.stalled(1000, builds * 4)); + CHECK(fresh.stalled(1000, builds * 5)); + // A cache that grows with its builds is fine at any size. + GrowthWatch growing; + growing.observe(std::size_t{1} << 30, 10); + CHECK(!growing.stalled((std::size_t{1} << 30) + kept, 10 + builds)); + CHECK(growing.bytes() == (std::size_t{1} << 30) + kept); + CHECK(growing.compiled() == 10 + builds); + // Too few builds compiled to tell: a few cache-sized misses can be refused for other reasons. + GrowthWatch few; + few.observe(5000, 0); + CHECK(!few.stalled(5000, builds - 1)); + // Enough compiled builds and less than stalled_below_bytes of growth: said once, then never again. + GrowthWatch stuck; + stuck.observe(1346482377, 100); + CHECK(stuck.stalled(1346482377 + kept - 1, 100 + builds)); + CHECK(!stuck.stalled(1346482377 + kept - 1, 100 + builds * 3)); + // Builds and growth are counted from the previous save: growth then, none now. + GrowthWatch interval; + interval.observe(2000, 0); + CHECK(!interval.stalled(2000 + kept * 8, builds * 3)); + CHECK(interval.stalled(2000 + kept * 8, builds * 4)); + // Too few builds at one save still moves the reference to that save. + GrowthWatch moved; + moved.observe(2000, 0); + CHECK(!moved.stalled(2000, builds - 1)); + CHECK(moved.stalled(2000, builds * 2 - 1)); + // A count that went backwards (a second device's counter view) is not read as a huge number of builds. + GrowthWatch backwards; + backwards.observe(3000, 50); + CHECK(!backwards.stalled(3000, 10)); + 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_growth(); }