Files
LeonidaArchived/runtime/test/pipeline_cache_snapshot_test.cpp
ThattersandClaude Opus 5.5 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

116 lines
4.1 KiB
C++

// Script the Vulkan size and data calls without loading a driver.
#include "pipeline_cache_snapshot.h"
#include <cstdio>
#include <cstring>
#define CHECK(condition) do { \
if (!(condition)) { \
std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); \
return 1; \
} \
} while (0)
namespace {
// A cache that workers keep growing: every call (size query or read) first adds `growth` bytes, or doubles the
// cache when `growth` is 0. A read into a buffer smaller than the cache writes a prefix and returns VK_INCOMPLETE;
// otherwise it writes the whole cache and reports its size, as vkGetPipelineCacheData does.
struct BusyCache {
size_t bytes, growth;
int queries = 0, reads = 0;
VkResult operator()(size_t* size, void* data) {
bytes += growth ? growth : bytes;
if (!data) {
++queries;
*size = bytes;
return VK_SUCCESS;
}
++reads;
if (*size < bytes) {
std::memset(data, 'p', *size);
return VK_INCOMPLETE;
}
*size = bytes;
std::memset(data, 's', *size);
return VK_SUCCESS;
}
};
// The snapshot, or an empty vector if it threw, so a failing case is reported by CHECK instead of ending the test.
std::vector<char> snapshot(BusyCache& cache) {
try {
return pipeline_cache_snapshot([&](size_t* size, void* d) { return cache(size, d); });
} catch (const std::exception&) {
return {};
}
}
} // namespace
int main() {
// A busy cache that grows less than 1/16 between the size query and the read: one attempt is enough. Reading
// into exactly the queried size returned VK_INCOMPLETE here on every attempt.
{
BusyCache cache{4096, 128};
CHECK(snapshot(cache) == std::vector<char>(4096 + 2 * 128, 's'));
CHECK(cache.queries == 1 && cache.reads == 1);
}
// Growth past the first attempt's headroom: the retry asks for more room and succeeds.
{
BusyCache cache{4096, 400};
CHECK(snapshot(cache) == std::vector<char>(4096 + 4 * 400, 's'));
CHECK(cache.queries == 2 && cache.reads == 2);
}
// A cache that doubles between every call outgrows even the last attempt's 1/2 headroom: the four-attempt error.
{
BusyCache cache{4096, 0};
CHECK(snapshot(cache).empty() && cache.queries == 4 && cache.reads == 4);
}
// A successful read can return less than the size query.
{
int queries = 0, reads = 0;
const auto shrinking = [&](size_t* size, void* data) {
if (!data) {
++queries;
*size = 64;
return VK_SUCCESS;
}
++reads;
*size = 48;
std::memset(data, 's', *size);
return VK_SUCCESS;
};
CHECK(pipeline_cache_snapshot(shrinking) == std::vector<char>(48, 's'));
CHECK(queries == 1 && reads == 1);
}
auto rejected = [](VkResult query_result, size_t available, VkResult read_result, size_t written) {
int calls = 0;
auto get = [&](size_t* size, void* data) {
++calls;
*size = data ? written : available;
return data ? read_result : query_result;
};
try {
pipeline_cache_snapshot(get);
} catch (const std::exception&) {
return calls;
}
return 0;
};
CHECK(rejected(VK_ERROR_DEVICE_LOST, 32, VK_SUCCESS, 32) == 1);
CHECK(rejected(VK_SUCCESS, 0, VK_SUCCESS, 0) == 1);
CHECK(rejected(VK_SUCCESS, 16, VK_SUCCESS, 16) == 1);
CHECK(rejected(VK_SUCCESS, 32, VK_ERROR_DEVICE_LOST, 32) == 2);
CHECK(rejected(VK_SUCCESS, 32, VK_SUCCESS, 0) == 2);
CHECK(rejected(VK_SUCCESS, 32, VK_SUCCESS, 16) == 2);
CHECK(rejected(VK_SUCCESS, 32, VK_SUCCESS, 32 + 32 / 16 + 1) == 2); // more than the buffer it was given
CHECK(rejected(VK_SUCCESS, 32, VK_INCOMPLETE, 32) == 8);
return 0;
}