Files
mandy/runtime/test/pipeline_cache_file_test.cpp
Daiko d3a1c290b5 d3d12: persist the pipeline cache across launches (D3D-704)
PSOs a session built were compiled again by the next one. The layer already read the `pipelines` stage's
pipeline_cache.bin beside gta6rt.exe (#367), but never wrote anything back.

Now the runtime keeps its own cache in pipeline_cache.session.bin beside it: vkGetPipelineCacheData on device
teardown, then vkMergePipelineCaches into the cache at the next launch. Vulkan takes one blob per create, so the
offline seed and the session file are two caches merged together rather than one file. They are deliberately
separate files, so a run can never replace the multi-GB corpus cache with the handful of pipelines it compiled.

The write is capped at 256 MiB (LEONIDA_PIPELINE_CACHE_BYTES=<bytes>, 0 turns it off): a cache seeded from the
corpus comes back as the whole union, which is not worth rewriting at every exit; past the cap the previous
session file is left alone. A foreign or truncated session file is ignored with one line, and so is a failed
write (a read-only install directory).

The header check moves to runtime/src/pipeline_cache_store.h, shared with the offline seed and tested without
Vulkan (runtime/test/pipeline_cache_store_test.cpp: the limit parser and the 32-byte
VkPipelineCacheHeaderVersionOne comparison). write_pipeline_cache_file moves from runtime/test to runtime/src so
the runtime and vk_pipeline_check use the same staged write.

Not run: this machine has neither MSVC nor the Windows SDK, so the shim itself is compiled by the Windows job.
The portable test passes here (23/23 ctest on Linux). The end-to-end check needs a Vulkan GPU: run
d3d12_vulkan_device_test twice and look for "wrote N bytes to pipeline_cache.session.bin", then "merged N bytes"
on the second run.
2026-11-23 17:39:38 +01:00

84 lines
3.1 KiB
C++

// Exercise checkpoint replacement and real filesystem errors without a Vulkan device.
#include "../src/pipeline_cache_file.h"
#include <chrono>
#include <cstdio>
#include <filesystem>
#include <fstream>
#include <iterator>
#include <string>
#ifdef _WIN32
#include <windows.h>
#else
#include <csignal>
#include <sys/resource.h>
#endif
namespace fs = std::filesystem;
#define CHECK(condition) do { \
if (!(condition)) { \
std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); \
return 1; \
} \
} while (0)
static std::string contents(const fs::path& path) {
std::ifstream file(path, std::ios::binary);
return {std::istreambuf_iterator<char>(file), {}};
}
static int test_checkpoint(const fs::path& dir) {
const auto cache = dir / "cache.bin";
const auto partial = dir / "cache.bin.partial";
std::ofstream(partial, std::ios::binary) << "unrelated";
const std::string saved("new\0data\r\n", 10);
CHECK(!write_pipeline_cache_file(cache, "older checkpoint", 16));
CHECK(contents(cache) == "older checkpoint");
CHECK(!write_pipeline_cache_file(cache, saved.data(), saved.size()));
CHECK(contents(cache) == saved);
const auto directory = dir / "empty";
CHECK(fs::create_directory(directory));
CHECK(write_pipeline_cache_file(directory, "next", 4));
CHECK(fs::is_directory(directory));
#ifdef _WIN32
// A reader that did not share DELETE access prevents replacement.
HANDLE held = CreateFileW(cache.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
CHECK(held != INVALID_HANDLE_VALUE);
const auto failed = write_pipeline_cache_file(cache, "next", 4);
CloseHandle(held);
CHECK(failed);
#else
// A buffered write may first report the size limit when it flushes or closes.
struct rlimit original;
CHECK(getrlimit(RLIMIT_FSIZE, &original) == 0);
struct rlimit limited = original;
limited.rlim_cur = 2;
CHECK(std::signal(SIGXFSZ, SIG_IGN) != SIG_ERR);
CHECK(setrlimit(RLIMIT_FSIZE, &limited) == 0);
const auto failed = write_pipeline_cache_file(cache, "next", 4);
const auto absent = dir / "absent";
const auto failed_create = write_pipeline_cache_file(absent, "next", 4);
CHECK(setrlimit(RLIMIT_FSIZE, &original) == 0);
CHECK(failed && failed_create);
CHECK(!fs::exists(absent));
#endif
CHECK(contents(cache) == saved);
CHECK(contents(partial) == "unrelated");
CHECK(std::distance(fs::directory_iterator(dir), fs::directory_iterator{}) == 3);
return 0;
}
int main() {
const auto stamp = std::chrono::steady_clock::now().time_since_epoch().count();
const auto dir = fs::temp_directory_path() / ("leonida_pipeline_cache_" + std::to_string(stamp));
CHECK(fs::create_directory(dir));
const int result = test_checkpoint(dir);
fs::remove_all(dir);
return result;
}