upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/test/large_module_gate_test.cpp
diff options
context:
space:
mode:
authorshayder <darknessshayder@gmail.com>2026-11-22 18:32:52 -0600
committerGitHub <noreply@github.com>2026-11-22 18:32:52 -0600
commit79e7cc5efd7239fb84f31e32c389a0e721f0d09d (patch)
tree20cc8c18641dec460576a6bbe0edba58ce0626bb /runtime/test/large_module_gate_test.cpp
parentaa2988cc555bad7ecd508b48a702adbb32fe7c95 (diff)
parent1c604af1a0a9649ae2eef19156a7c0627c676318 (diff)
Merge pull request #4278 from radv-tinker/pr/pipeline-check-memory-budget
vk_pipeline_check: large modules share a memory budget instead of compiling alone
Diffstat (limited to 'runtime/test/large_module_gate_test.cpp')
-rw-r--r--runtime/test/large_module_gate_test.cpp159
1 files changed, 159 insertions, 0 deletions
diff --git a/runtime/test/large_module_gate_test.cpp b/runtime/test/large_module_gate_test.cpp
new file mode 100644
index 000000000..ed8d18a0c
--- /dev/null
+++ b/runtime/test/large_module_gate_test.cpp
@@ -0,0 +1,159 @@
1// vk_pipeline_check's memory budget for large modules (large_module_gate.h), without a Vulkan device.
2#include "large_module_gate.h"
3
4#include <atomic>
5#include <string>
6#ifndef _WIN32
7#include <unistd.h>
8#endif
9#include <chrono>
10#include <cstdio>
11#include <thread>
12#include <vector>
13
14#define CHECK(condition) do { \
15 if (!(condition)) { \
16 std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); \
17 return 1; \
18 } \
19} while (0)
20
21using leonida::LargeModuleGate;
22
23static int budget_and_count() {
24 LargeModuleGate gate(10, 3);
25 CHECK(gate.admits(4));
26 gate.enter(4);
27 CHECK(gate.admits(4));
28 gate.enter(4);
29 CHECK(!gate.admits(4)); // 12 > 10
30 CHECK(gate.admits(2)); // 10 fits exactly
31 gate.enter(2);
32 CHECK(!gate.admits(0)); // three in: max_inflight
33 gate.leave(4);
34 CHECK(gate.admits(4));
35 gate.leave(4);
36 gate.leave(2);
37 CHECK(gate.inflight() == 0);
38 return 0;
39}
40
41static int oversized_module_still_gets_in_alone() {
42 // Before: a module whose estimate exceeds the whole budget would wait forever.
43 LargeModuleGate gate(10, 4);
44 CHECK(gate.admits(100));
45 gate.enter(100);
46 CHECK(!gate.admits(1)); // nothing joins it
47 gate.leave(100);
48 CHECK(gate.admits(1));
49 return 0;
50}
51
52static int max_inflight_zero_means_one() {
53 LargeModuleGate gate(100, 0);
54 CHECK(gate.max_inflight() == 1);
55 gate.enter(1);
56 CHECK(!gate.admits(1));
57 gate.leave(1);
58 return 0;
59}
60
61static int ticket_releases() {
62 LargeModuleGate gate(10, 2);
63 {
64 LargeModuleGate::Ticket a(gate, 6);
65 CHECK(gate.inflight() == 1);
66 LargeModuleGate::Ticket moved = std::move(a); // the moved-from ticket must not leave twice
67 CHECK(gate.inflight() == 1);
68 LargeModuleGate::Ticket b;
69 b = LargeModuleGate::Ticket(gate, 4);
70 CHECK(gate.inflight() == 2);
71 b.release();
72 CHECK(gate.inflight() == 1);
73 }
74 CHECK(gate.inflight() == 0);
75 return 0;
76}
77
78static int threads_never_exceed_the_budget() {
79 constexpr uint64_t kBudget = 10;
80 LargeModuleGate gate(kBudget, 3);
81 std::atomic<uint64_t> used{0};
82 std::atomic<unsigned> in{0};
83 std::atomic<bool> over{false};
84 std::vector<std::thread> threads;
85 for (int t = 0; t < 8; ++t) {
86 threads.emplace_back([&, t] {
87 for (int i = 0; i < 200; ++i) {
88 const uint64_t est = 1 + uint64_t((t + i) % 5); // 1..5
89 LargeModuleGate::Ticket ticket(gate, est);
90 const uint64_t u = used += est;
91 const unsigned n = ++in;
92 if (n > 3 || (n > 1 && u > kBudget)) over = true;
93 std::this_thread::sleep_for(std::chrono::microseconds(50));
94 --in;
95 used -= est;
96 }
97 });
98 }
99 for (auto& th : threads) th.join();
100 CHECK(!over);
101 CHECK(gate.inflight() == 0);
102 return 0;
103}
104
105static int estimate_and_memory_query() {
106 CHECK(leonida::large_module_estimate(4u << 20) == (uint64_t(4) << 20) * leonida::kBytesPerSpirvByte);
107#if defined(__linux__) || defined(_WIN32)
108 CHECK(leonida::available_memory_bytes() > 0); // Linux: MemAvailable; Windows: ullAvailPhys
109#endif
110 leonida::release_free_memory(); // must be callable anywhere
111 return 0;
112}
113
114#if defined(__linux__)
115#include <filesystem>
116#include <fstream>
117
118static void write_file(const std::filesystem::path& p, const char* text) {
119 std::filesystem::create_directories(p.parent_path());
120 std::ofstream(p) << text;
121}
122
123static int cgroup_limits_are_read_up_the_tree() {
124 // Before: only MemAvailable was read, so under systemd-run -p MemoryMax=... (or in a container) the budget was
125 // the whole machine's and the checker could be killed by its own cgroup.
126 namespace fs = std::filesystem;
127 const fs::path root = fs::temp_directory_path() / ("lmg_test_" + std::to_string(::getpid()));
128 fs::remove_all(root);
129 write_file(root / "self", "0::/user.slice/app.scope\n");
130 write_file(root / "cg/user.slice/memory.max", "10000\n");
131 write_file(root / "cg/user.slice/memory.current", "4000\n"); // room 6000
132 write_file(root / "cg/user.slice/app.scope/memory.max", "max\n"); // no limit of its own
133 write_file(root / "cg/user.slice/app.scope/memory.current", "3000\n");
134 CHECK(leonida::cgroup_memory_room((root / "self").string(), (root / "cg").string()) == 6000);
135 write_file(root / "cg/user.slice/app.scope/memory.max", "5000\n"); // room 2000, the tighter one
136 CHECK(leonida::cgroup_memory_room((root / "self").string(), (root / "cg").string()) == 2000);
137 write_file(root / "cg/user.slice/app.scope/memory.current", "7000\n"); // over its limit: no room
138 CHECK(leonida::cgroup_memory_room((root / "self").string(), (root / "cg").string()) == 0);
139 write_file(root / "self", "12:memory:/legacy\n"); // cgroup v1 only: no limit read
140 CHECK(leonida::cgroup_memory_room((root / "self").string(), (root / "cg").string()) == UINT64_MAX);
141 CHECK(leonida::cgroup_memory_room((root / "missing").string(), (root / "cg").string()) == UINT64_MAX);
142 fs::remove_all(root);
143 return 0;
144}
145#endif
146
147int main() {
148#if defined(__linux__)
149 if (int r = cgroup_limits_are_read_up_the_tree()) return r;
150#endif
151 if (int r = budget_and_count()) return r;
152 if (int r = oversized_module_still_gets_in_alone()) return r;
153 if (int r = max_inflight_zero_means_one()) return r;
154 if (int r = ticket_releases()) return r;
155 if (int r = threads_never_exceed_the_budget()) return r;
156 if (int r = estimate_and_memory_query()) return r;
157 std::puts("large_module_gate: ok");
158 return 0;
159}