upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/test/pipeline_cache_journal_test.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/test/pipeline_cache_journal_test.cpp')
-rw-r--r--runtime/test/pipeline_cache_journal_test.cpp337
1 files changed, 337 insertions, 0 deletions
diff --git a/runtime/test/pipeline_cache_journal_test.cpp b/runtime/test/pipeline_cache_journal_test.cpp
new file mode 100644
index 000000000..c3793c96c
--- /dev/null
+++ b/runtime/test/pipeline_cache_journal_test.cpp
@@ -0,0 +1,337 @@
1// The pipeline journal's file naming, reading and writing (pipeline_cache_journal.h). No Vulkan, no GPU.
2#include "../src/pipeline_cache_journal.h"
3
4#include <chrono>
5#include <cstdint>
6#include <cstdio>
7#include <cstring>
8#include <filesystem>
9#include <fstream>
10#include <string>
11#include <vector>
12
13namespace fs = std::filesystem;
14namespace journal = leonida::pipeline_journal;
15namespace store = leonida::pipeline_cache_store;
16
17#define CHECK(condition) do { \
18 if (!(condition)) { \
19 std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); \
20 return 1; \
21 } \
22} while (0)
23
24static void put32(std::uint8_t* p, std::uint32_t value) {
25 for (int i = 0; i < 4; ++i) p[i] = static_cast<std::uint8_t>(value >> (8 * i));
26}
27
28static leonida::cached_pso::Identity identity() {
29 leonida::cached_pso::Identity id;
30 id.vendor = 0x10de;
31 id.device = 0x2782;
32 for (int i = 0; i < 16; ++i) id.cache_uuid[i] = static_cast<std::uint8_t>(0xa0 + i);
33 return id;
34}
35
36// A VkPipelineCacheHeaderVersionOne for `id` followed by `payload` bytes.
37static std::vector<char> blob(const leonida::cached_pso::Identity& id, std::size_t payload) {
38 std::vector<char> out(store::header_bytes + payload, '\x5a');
39 auto* p = reinterpret_cast<std::uint8_t*>(out.data());
40 put32(p, store::header_bytes);
41 put32(p + 4, store::header_version_one);
42 put32(p + 8, id.vendor);
43 put32(p + 12, id.device);
44 std::memcpy(p + 16, id.cache_uuid, sizeof(id.cache_uuid));
45 return out;
46}
47
48static int test_hash() {
49 const std::uint32_t words[] = {0x07230203, 0x00010600, 1, 2, 3};
50 journal::Hasher a, b, c;
51 a.text("cs");
52 a.bytes(words, sizeof(words));
53 a.value(std::uint32_t(32));
54 b.text("cs");
55 b.bytes(words, sizeof(words));
56 b.value(std::uint32_t(32));
57 CHECK(a.digest() == b.digest()); // deterministic: the next boot finds the same file
58 c.text("cs");
59 c.bytes(words, sizeof(words));
60 c.value(std::uint32_t(64)); // another subgroup size is another pipeline
61 CHECK(a.digest() != c.digest());
62 journal::Hasher d, e;
63 d.text("ab");
64 d.text("c");
65 e.text("a");
66 e.text("bc");
67 CHECK(d.digest() != e.digest()); // text is length-prefixed
68 return 0;
69}
70
71static int test_path() {
72 const auto path = journal::entry_path("dir", "cs", 0x0123456789abcdefull);
73 CHECK(path.filename().string() == "cs-0123456789abcdef.bin");
74 CHECK(journal::entry_path("dir", "cs", 1).filename().string() == "cs-0000000000000001.bin");
75 return 0;
76}
77
78static int test_files(const fs::path& dir) {
79 const auto id = identity();
80 const auto path = journal::entry_path(dir / "pipeline_cache.d", "cs", 42);
81 CHECK(journal::read_entry(path, id).empty()); // missing
82
83 // write_entry creates the directory and publishes the whole blob.
84 const auto data = blob(id, 1000);
85 CHECK(!journal::write_entry(path, data.data(), data.size()));
86 CHECK(journal::read_entry(path, id) == data);
87
88 // A second write replaces the first.
89 const auto next = blob(id, 10);
90 CHECK(!journal::write_entry(path, next.data(), next.size()));
91 CHECK(journal::read_entry(path, id) == next);
92
93 // Another device or driver (pipeline-cache UUID): not read, so the build starts over and rewrites it.
94 auto other = id;
95 other.cache_uuid[3] ^= 1;
96 CHECK(journal::read_entry(path, other).empty());
97 other = id;
98 other.device = 0x1234;
99 CHECK(journal::read_entry(path, other).empty());
100
101 // Truncated to less than a header, or over the read limit: not read.
102 {
103 std::ofstream(path, std::ios::binary | std::ios::trunc).write(next.data(), 16);
104 }
105 CHECK(journal::read_entry(path, id).empty());
106 CHECK(!journal::write_entry(path, data.data(), data.size()));
107 CHECK(journal::read_entry(path, id, data.size() - 1).empty());
108 CHECK(journal::read_entry(path, id, data.size()) == data);
109
110 CHECK(journal::directory_bytes(path.parent_path()) == data.size());
111 CHECK(journal::directory_bytes(dir / "missing") == 0);
112
113 // No staging files are left behind.
114 std::size_t files = 0;
115 for (const auto& item : fs::directory_iterator(path.parent_path())) {
116 (void)item;
117 ++files;
118 }
119 CHECK(files == 1);
120 return 0;
121}
122
123// When a build writes its file, and when the shared cache's answer was a compile.
124static int test_policy() {
125 // No file (or one the driver declined): always written, whatever the feedback or time.
126 CHECK(journal::should_write(false, 1, 0));
127 CHECK(journal::should_write(false, -1, 0));
128 CHECK(journal::should_write(false, 0, 0));
129 // From the file: the feedback decides; without feedback, the length of the driver call.
130 CHECK(!journal::should_write(true, 1, 5000));
131 CHECK(journal::should_write(true, 0, 1));
132 CHECK(!journal::should_write(true, -1, journal::unreported_compile_ms - 1));
133 CHECK(journal::should_write(true, -1, journal::unreported_compile_ms));
134 // FAIL_ON_PIPELINE_COMPILE_REQUIRED: a slow pipeline without the hit bit was compiled; a fast one came out of
135 // the driver's own disk cache, and a hit or no feedback is never taken for a compile.
136 CHECK(journal::probe_compiled(0, journal::probe_compile_ms));
137 CHECK(!journal::probe_compiled(0, journal::probe_compile_ms - 1));
138 CHECK(!journal::probe_compiled(1, 60000));
139 CHECK(!journal::probe_compiled(-1, 60000));
140 return 0;
141}
142
143static int test_accounting() {
144 CHECK(journal::replace_bytes(100, 0, 30) == 130); // a new file
145 CHECK(journal::replace_bytes(100, 40, 30) == 90); // a rewrite replaces the old size
146 CHECK(journal::replace_bytes(10, 40, 30) == 30); // never below zero
147 const auto t0 = fs::file_time_type::clock::now();
148 using std::chrono::hours;
149 const std::vector<journal::EntryInfo> entries = {
150 {"a", 40, t0 - hours(3)}, {"b", 40, t0 - hours(1)}, {"c", 40, t0 - hours(2)}, {"d", 40, t0}};
151 CHECK(journal::plan_eviction(entries, 160, 120).empty()); // at the budget: nothing goes
152 // Over it: least recently used first (a, then c), until the target holds.
153 const auto out = journal::plan_eviction(entries, 150, 100);
154 CHECK(out.size() == 2 && out[0] == 0 && out[1] == 2);
155 CHECK(journal::plan_eviction(entries, 0, 0).size() == 4);
156 return 0;
157}
158
159static int test_names() {
160 CHECK(journal::entry_name("cs-0123456789abcdef.bin"));
161 CHECK(!journal::entry_name("cs-0123456789abcde.bin")); // 15 digits
162 CHECK(!journal::entry_name("cs-0123456789ABCDEF.bin")); // entry_path writes lowercase
163 CHECK(!journal::entry_name("-0123456789abcdef.bin"));
164 CHECK(!journal::entry_name("pipeline_cache.session.bin"));
165 CHECK(journal::staging_name("cs-0123456789abcdef.bin.tmp.123456.0"));
166 CHECK(!journal::staging_name("cs-0123456789abcdef.bin.tmp.123456"));
167 CHECK(!journal::staging_name("cs-0123456789abcdef.bin.tmp.12x.0"));
168 CHECK(!journal::staging_name("pipeline_cache.session.bin.tmp.1.0")); // the session file's, swept elsewhere
169 return 0;
170}
171
172static void put_file(const fs::path& path, std::size_t bytes, fs::file_time_type when) {
173 std::ofstream(path, std::ios::binary | std::ios::trunc) << std::string(bytes, 'x');
174 fs::last_write_time(path, when);
175}
176
177static int test_tidy(const fs::path& root) {
178 const auto dir = root / "tidy";
179 fs::create_directories(dir);
180 const auto now = fs::file_time_type::clock::now();
181 using std::chrono::hours;
182 using std::chrono::minutes;
183 const auto a = journal::entry_path(dir, "cs", 0xa), b = journal::entry_path(dir, "cs", 0xb),
184 c = journal::entry_path(dir, "cs", 0xc);
185 put_file(a, 400, now - hours(5));
186 put_file(b, 400, now - hours(1));
187 put_file(c, 400, now - hours(3));
188 const fs::path old_stage = a.string() + ".tmp.111.0", new_stage = b.string() + ".tmp.222.1";
189 put_file(old_stage, 50, now - minutes(30));
190 put_file(new_stage, 50, now - minutes(2));
191 put_file(dir / "notes.txt", 999, now - hours(9));
192
193 // Under the budget: only the old staging file goes; the young one may still be being written.
194 auto result = journal::tidy(dir, 2000, minutes(10), now);
195 CHECK(result.swept == 1 && result.evicted == 0 && result.entries == 3 && result.bytes == 1200);
196 CHECK(!fs::exists(old_stage) && fs::exists(new_stage) && fs::exists(dir / "notes.txt"));
197
198 // Over it: the least recently used go until three quarters is left (1200 > 1000; 750 holds b alone).
199 result = journal::tidy(dir, 1000, minutes(10), now);
200 CHECK(result.evicted == 2 && result.entries == 1 && result.bytes == 400);
201 CHECK(!fs::exists(a) && !fs::exists(c) && fs::exists(b));
202
203 // A hit refreshes a file's last use, which keeps it over older ones.
204 put_file(a, 400, now - hours(5));
205 journal::touch(a, now);
206 put_file(c, 400, now - hours(3));
207 result = journal::tidy(dir, 1000, minutes(10), now); // 1200 again: c (3 h) is now the oldest, then b (1 h)
208 CHECK(fs::exists(a) && !fs::exists(c) && !fs::exists(b) && result.entries == 1);
209 CHECK(journal::tidy(root / "missing", 1000).entries == 0);
210 return 0;
211}
212
213static int test_entry_file(const fs::path& dir) {
214 const auto id = identity();
215 const auto path = journal::entry_path(dir, "cs", 77);
216 auto missing = journal::read_entry_file(path, id);
217 CHECK(missing.data.empty() && missing.bytes == 0);
218 // A foreign file still reports its size, which its rewrite replaces in the budget.
219 auto other = id;
220 other.device ^= 1;
221 const auto data = blob(other, 100);
222 CHECK(!journal::write_entry(path, data.data(), data.size()));
223 const auto foreign = journal::read_entry_file(path, id);
224 CHECK(foreign.data.empty() && foreign.bytes == data.size());
225 const auto own = journal::read_entry_file(path, other);
226 CHECK(own.data == data && own.bytes == data.size());
227 return 0;
228}
229
230static int test_overlay(const fs::path& dir) {
231 using std::chrono::hours;
232 const auto local = dir / "overlay" / "pipeline_cache.d", seed = dir / "seed";
233 const auto dst = journal::entry_path(local, "cs", 1), src = journal::entry_path(seed, "cs", 1);
234 const auto id = identity();
235 auto wrong = id;
236 wrong.cache_uuid[0] ^= 1;
237 const auto donor = blob(id, 32), rebuilt = blob(id, 53), stale = blob(wrong, 45);
238 CHECK(!journal::write_entry(src, donor.data(), donor.size()));
239 const auto now = fs::file_time_type::clock::now();
240 fs::last_write_time(src, now - hours(8));
241 const auto donor_time = fs::last_write_time(src);
242 const auto roots = journal::resolve_roots(local, seed);
243 CHECK(!roots.writable.empty() && fs::equivalent(roots.seed, seed));
244
245 // No local entry: donor bytes may seed the driver, but do not count against the writable budget.
246 constexpr auto runtime = journal::ReadRole::runtime;
247 constexpr auto builder = journal::ReadRole::materialize_local;
248 auto entry = journal::read_with_seed(dst, roots.seed, id, runtime);
249 CHECK(entry.from_seed && entry.data == donor && entry.bytes == 0 && entry.used == fs::file_time_type{});
250 // Same roots, but the host building a requested local file must still see a miss.
251 CHECK(!journal::entry_usable(dst, id) && journal::entry_usable(src, id));
252 const auto missing_build = journal::read_with_seed(dst, roots.seed, id, builder);
253 CHECK(missing_build.data.empty() && !missing_build.from_seed && missing_build.bytes == 0);
254 journal::touch_local_hit(dst, entry, now);
255 CHECK(!fs::exists(local) && fs::last_write_time(src) == donor_time);
256 CHECK(journal::replace_bytes(1000, entry.bytes, rebuilt.size()) == 1000 + rebuilt.size());
257 CHECK(journal::read_with_seed(dst, {}, id, runtime).data.empty());
258 CHECK(journal::read_with_seed(dst, seed, wrong, runtime).data.empty());
259 CHECK(journal::read_with_seed(dst, seed, id, runtime, donor.size() - 1).data.empty());
260 CHECK(journal::read_with_seed(dst, seed, id, runtime, donor.size()).data == donor);
261 CHECK(journal::read_with_seed(journal::entry_path(local, "cs", 2), seed, id, runtime).data.empty());
262
263 // Even with a stale local object the recorded byte count and LRU timestamp belong to that object.
264 CHECK(!journal::write_entry(dst, stale.data(), stale.size()));
265 fs::last_write_time(dst, now - hours(3));
266 const auto stale_time = fs::last_write_time(dst);
267 entry = journal::read_with_seed(dst, seed, id, runtime);
268 CHECK(entry.from_seed && entry.data == donor && entry.bytes == stale.size() && entry.used == stale_time);
269 const auto stale_build = journal::read_with_seed(dst, roots.seed, id, builder);
270 CHECK(stale_build.data.empty() && !stale_build.from_seed && stale_build.bytes == stale.size() &&
271 stale_build.used == stale_time && !journal::entry_usable(dst, id));
272 journal::touch_local_hit(dst, entry, now);
273 CHECK(fs::last_write_time(dst) == stale_time && fs::last_write_time(src) == donor_time);
274 CHECK(journal::read_entry(dst, wrong) == stale); // neither promoted nor rewritten by the hit
275 auto used = journal::tidy(roots.writable, stale.size());
276 CHECK(used.entries == 1 && used.bytes == stale.size() && used.evicted == 0);
277 CHECK(journal::replace_bytes(used.bytes, entry.bytes, rebuilt.size()) == rebuilt.size());
278
279 CHECK(!journal::write_entry(dst, rebuilt.data(), 3)); // truncated local is not a seed hit on the local file
280 entry = journal::read_with_seed(dst, seed, id, runtime);
281 CHECK(entry.from_seed && entry.data == donor && entry.bytes == 3 && fs::file_size(dst) == 3);
282 CHECK(!journal::write_entry(dst, rebuilt.data(), rebuilt.size()));
283 fs::last_write_time(dst, now - hours(3));
284 entry = journal::read_with_seed(dst, seed, id, runtime);
285 CHECK(!entry.from_seed && entry.data == rebuilt && entry.bytes == rebuilt.size());
286 CHECK(journal::entry_usable(dst, id));
287 CHECK(!journal::entry_usable(dst, id, rebuilt.size() - 1) && journal::entry_usable(dst, id, rebuilt.size()));
288 const auto ready_build = journal::read_with_seed(dst, roots.seed, id, builder);
289 CHECK(!ready_build.from_seed && ready_build.data == rebuilt && ready_build.bytes == rebuilt.size());
290 journal::touch_local_hit(dst, entry, now);
291 const auto refreshed = fs::last_write_time(dst);
292 CHECK(refreshed > entry.used);
293 entry = journal::read_with_seed(dst, seed, id, runtime);
294 journal::touch_local_hit(dst, entry, now + std::chrono::minutes(30));
295 CHECK(fs::last_write_time(dst) == refreshed);
296 used = journal::tidy(roots.writable, 0);
297 CHECK(used.evicted == 1 && used.bytes == 0 && !fs::exists(dst));
298 CHECK(journal::read_entry(src, id) == donor && fs::last_write_time(src) == donor_time);
299
300 const auto unset = journal::resolve_roots(local, {});
301 CHECK(unset.writable == local && unset.seed.empty());
302 const auto disabled = [](const fs::path& sink, const fs::path& source) {
303 const auto r = journal::resolve_roots(sink, source);
304 return r.writable.empty() && r.seed.empty();
305 };
306 CHECK(disabled({}, seed) && disabled(local, "relative") && disabled(local, dir / "no-seed"));
307 CHECK(disabled(local, src) && disabled(src, seed));
308 CHECK(disabled(seed / ".", seed) && disabled(seed / "missing" / "journal", seed) && disabled(dir, seed));
309#ifdef _WIN32
310 if (fs::exists(dir / "SEED")) CHECK(disabled(dir / "SEED", seed));
311#endif
312 return 0;
313}
314
315int main() {
316 const auto dir = fs::temp_directory_path() /
317 ("leonida_pipeline_journal_test_" + std::to_string(std::chrono::steady_clock::now().time_since_epoch().count()));
318 std::error_code ec;
319 fs::create_directories(dir, ec);
320 if (ec) {
321 std::fprintf(stderr, "cannot create %s\n", dir.string().c_str());
322 return 1;
323 }
324 int failed = test_hash();
325 if (!failed) failed = test_path();
326 if (!failed) failed = test_files(dir);
327 if (!failed) failed = test_policy();
328 if (!failed) failed = test_accounting();
329 if (!failed) failed = test_names();
330 if (!failed) failed = test_tidy(dir);
331 if (!failed) failed = test_entry_file(dir);
332 if (!failed) failed = test_overlay(dir);
333 fs::remove_all(dir, ec);
334 if (failed) return failed;
335 std::printf("pipeline_cache_journal_test: ok\n");
336 return 0;
337}