Files
mandy/runtime/test/pipeline_cache_snapshot.h
Thatters 6f9ec5c4f9 pipeline_cache_snapshot: read into a buffer with headroom so a busy cache doesn't fail every checkpoint
vk_pipeline_check saves its pipeline cache every 10 minutes while workers keep compiling. The snapshot queried the
size and then read into a buffer of exactly that size, so any pipeline added between the two calls made the read
VK_INCOMPLETE. On a large, busy cache that can happen four times in a row, and the checkpoint is lost:

    periodic pipeline cache save to ...\pipeline_cache.bin failed: vkGetPipelineCacheData returned VK_INCOMPLETE
    on four attempts

(seen during a full `--jobs 12` pipelines stage, ~4 GB cache, RTX 5070 Ti / 621.24). A crash later in the stage,
as in #884, then loses more work than it needs to.

The read buffer now has room to spare: 1/16 of the queried size on the first attempt, doubling on each retry
(1/8, 1/4, 1/2). A larger buffer is valid for vkGetPipelineCacheData: the driver writes the whole cache and reports
the bytes written, and the snapshot is trimmed to that. A cache that still outgrows the last attempt ends in the same
error as before, and the byte-count checks are unchanged (more than the buffer given is still rejected).

pipeline_cache_snapshot_test: the scripted driver is now a cache that grows on every call, as a busy one does.
New cases: growth under 1/16 succeeds in one attempt, growth past the first headroom succeeds on the retry, and a
cache that doubles between calls still ends in the four-attempt error. With the old header the first case fails
(the four VK_INCOMPLETEs this fixes); a failing case is now reported by CHECK instead of an uncaught exception.

Tested on Windows 11 Pro 25H2, MSVC 14.51 + Ninja, RTX 5070 Ti / 621.24: pipeline_cache_snapshot_test passes
(fails at the new busy-cache case with the old header); vk_pipeline_check on 300 real modules from my own copy's
work folder wrote a 28.4 MB cache through the new path, and a second run loaded it (64 s, then 1 s). No warnings.
No game data. AI-assisted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 10:21:49 -05:00

38 lines
1.7 KiB
C++

// vkGetPipelineCacheData can return a prefix if workers grow the cache between calls.
#pragma once
#include <vulkan/vulkan_core.h>
#include <stdexcept>
#include <string>
#include <vector>
template<class GetData>
std::vector<char> pipeline_cache_snapshot(GetData get) {
for (int attempt = 0; attempt < 4; ++attempt) {
size_t size = 0;
VkResult result = get(&size, nullptr);
if (result != VK_SUCCESS)
throw std::runtime_error("vkGetPipelineCacheData size query returned " + std::to_string(result));
if (size < sizeof(VkPipelineCacheHeaderVersionOne))
throw std::runtime_error("cache size is smaller than its header");
// Workers keep adding pipelines between the size query and the read, so read into a buffer with room to
// spare: 1/16 of the queried size on the first attempt, doubling on each retry (1/8, 1/4, 1/2). A buffer
// larger than the data is fine; the driver writes the whole cache and reports the bytes it wrote.
const size_t capacity = size + (size >> (4 - attempt));
// Destroy each incomplete snapshot before allocating its replacement.
std::vector<char> data(capacity);
size = capacity;
result = get(&size, data.data());
if (result == VK_INCOMPLETE) continue;
if (result != VK_SUCCESS)
throw std::runtime_error("vkGetPipelineCacheData read returned " + std::to_string(result));
if (size < sizeof(VkPipelineCacheHeaderVersionOne) || size > data.size())
throw std::runtime_error("invalid pipeline cache byte count");
data.resize(size);
return data;
}
throw std::runtime_error("vkGetPipelineCacheData returned VK_INCOMPLETE on four attempts");
}