diff options
| author | radv-tinker <221537828+radv-tinker@users.noreply.github.com> | 2026-11-23 00:26:54 +0000 |
|---|---|---|
| committer | radv-tinker <221537828+radv-tinker@users.noreply.github.com> | 2026-11-23 00:30:02 +0000 |
| commit | 1c604af1a0a9649ae2eef19156a7c0627c676318 (patch) | |
| tree | 20cc8c18641dec460576a6bbe0edba58ce0626bb /runtime/test/large_module_gate_test.cpp | |
| parent | aa2988cc555bad7ecd508b48a702adbb32fe7c95 (diff) | |
vk_pipeline_check: large modules share a memory budget instead of compiling alone
Modules over --large-module-bytes compiled one at a time with every other thread waiting. On the translated
corpus that is 695 modules (the ray-tracing compute shaders, ~14k RDNA2 instructions each, ~4.9 MB of SPIR-V),
about 12 s and 2 GB each on RADV GFX1201, so the pipelines stage spent hours mostly single-threaded.
large_module_gate.h lets large modules in while 512 bytes per SPIR-V byte fits 3/4 of the memory available at
start (Linux: MemAvailable and the cgroup v2 memory.max room up the tree; Windows: ullAvailPhys), one always
admitted when none is in, small modules never held back. Unknown memory keeps the old exclusive gate, as does
--large-parallel 1. malloc_trim(0) after each large module returns what glibc kept in per-thread arenas.
40 modules (27 large), Mesa shader cache off, RX 9070 XT / Mesa 25.3.6: 297 s -> 83 s on 6 threads (7.1 -> 8.3 GB
peak), 56 s on 12 threads (15.1 GB).
Tested: large_module_gate_test (Linux, and the clang-cl build under Proton), ctest on Linux (40 passed, 2
skipped), vk_pipeline_check over 40 real modules in three modes, and the pipelines stage over the corpus with the
gate: 26,000+ of 26,503 modules in 15 min, none failed. The largest module (26 MB of SPIR-V) was still compiling
alone on RADV after 25 min, as it would under the old gate. AI-assisted.
Diffstat (limited to 'runtime/test/large_module_gate_test.cpp')
| -rw-r--r-- | runtime/test/large_module_gate_test.cpp | 159 |
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 | |||
| 21 | using leonida::LargeModuleGate; | ||
| 22 | |||
| 23 | static 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 | |||
| 41 | static 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 | |||
| 52 | static 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 | |||
| 61 | static 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 | |||
| 78 | static 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 | |||
| 105 | static 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 | |||
| 118 | static 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 | |||
| 123 | static 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 | |||
| 147 | int 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 | } | ||