From 1c604af1a0a9649ae2eef19156a7c0627c676318 Mon Sep 17 00:00:00 2001 From: radv-tinker <221537828+radv-tinker@users.noreply.github.com> Date: Mon, 23 Nov 2026 00:26:54 +0000 Subject: 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. --- runtime/test/large_module_gate_test.cpp | 159 ++++++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 runtime/test/large_module_gate_test.cpp (limited to 'runtime/test/large_module_gate_test.cpp') 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 @@ +// vk_pipeline_check's memory budget for large modules (large_module_gate.h), without a Vulkan device. +#include "large_module_gate.h" + +#include +#include +#ifndef _WIN32 +#include +#endif +#include +#include +#include +#include + +#define CHECK(condition) do { \ + if (!(condition)) { \ + std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); \ + return 1; \ + } \ +} while (0) + +using leonida::LargeModuleGate; + +static int budget_and_count() { + LargeModuleGate gate(10, 3); + CHECK(gate.admits(4)); + gate.enter(4); + CHECK(gate.admits(4)); + gate.enter(4); + CHECK(!gate.admits(4)); // 12 > 10 + CHECK(gate.admits(2)); // 10 fits exactly + gate.enter(2); + CHECK(!gate.admits(0)); // three in: max_inflight + gate.leave(4); + CHECK(gate.admits(4)); + gate.leave(4); + gate.leave(2); + CHECK(gate.inflight() == 0); + return 0; +} + +static int oversized_module_still_gets_in_alone() { + // Before: a module whose estimate exceeds the whole budget would wait forever. + LargeModuleGate gate(10, 4); + CHECK(gate.admits(100)); + gate.enter(100); + CHECK(!gate.admits(1)); // nothing joins it + gate.leave(100); + CHECK(gate.admits(1)); + return 0; +} + +static int max_inflight_zero_means_one() { + LargeModuleGate gate(100, 0); + CHECK(gate.max_inflight() == 1); + gate.enter(1); + CHECK(!gate.admits(1)); + gate.leave(1); + return 0; +} + +static int ticket_releases() { + LargeModuleGate gate(10, 2); + { + LargeModuleGate::Ticket a(gate, 6); + CHECK(gate.inflight() == 1); + LargeModuleGate::Ticket moved = std::move(a); // the moved-from ticket must not leave twice + CHECK(gate.inflight() == 1); + LargeModuleGate::Ticket b; + b = LargeModuleGate::Ticket(gate, 4); + CHECK(gate.inflight() == 2); + b.release(); + CHECK(gate.inflight() == 1); + } + CHECK(gate.inflight() == 0); + return 0; +} + +static int threads_never_exceed_the_budget() { + constexpr uint64_t kBudget = 10; + LargeModuleGate gate(kBudget, 3); + std::atomic used{0}; + std::atomic in{0}; + std::atomic over{false}; + std::vector threads; + for (int t = 0; t < 8; ++t) { + threads.emplace_back([&, t] { + for (int i = 0; i < 200; ++i) { + const uint64_t est = 1 + uint64_t((t + i) % 5); // 1..5 + LargeModuleGate::Ticket ticket(gate, est); + const uint64_t u = used += est; + const unsigned n = ++in; + if (n > 3 || (n > 1 && u > kBudget)) over = true; + std::this_thread::sleep_for(std::chrono::microseconds(50)); + --in; + used -= est; + } + }); + } + for (auto& th : threads) th.join(); + CHECK(!over); + CHECK(gate.inflight() == 0); + return 0; +} + +static int estimate_and_memory_query() { + CHECK(leonida::large_module_estimate(4u << 20) == (uint64_t(4) << 20) * leonida::kBytesPerSpirvByte); +#if defined(__linux__) || defined(_WIN32) + CHECK(leonida::available_memory_bytes() > 0); // Linux: MemAvailable; Windows: ullAvailPhys +#endif + leonida::release_free_memory(); // must be callable anywhere + return 0; +} + +#if defined(__linux__) +#include +#include + +static void write_file(const std::filesystem::path& p, const char* text) { + std::filesystem::create_directories(p.parent_path()); + std::ofstream(p) << text; +} + +static int cgroup_limits_are_read_up_the_tree() { + // Before: only MemAvailable was read, so under systemd-run -p MemoryMax=... (or in a container) the budget was + // the whole machine's and the checker could be killed by its own cgroup. + namespace fs = std::filesystem; + const fs::path root = fs::temp_directory_path() / ("lmg_test_" + std::to_string(::getpid())); + fs::remove_all(root); + write_file(root / "self", "0::/user.slice/app.scope\n"); + write_file(root / "cg/user.slice/memory.max", "10000\n"); + write_file(root / "cg/user.slice/memory.current", "4000\n"); // room 6000 + write_file(root / "cg/user.slice/app.scope/memory.max", "max\n"); // no limit of its own + write_file(root / "cg/user.slice/app.scope/memory.current", "3000\n"); + CHECK(leonida::cgroup_memory_room((root / "self").string(), (root / "cg").string()) == 6000); + write_file(root / "cg/user.slice/app.scope/memory.max", "5000\n"); // room 2000, the tighter one + CHECK(leonida::cgroup_memory_room((root / "self").string(), (root / "cg").string()) == 2000); + write_file(root / "cg/user.slice/app.scope/memory.current", "7000\n"); // over its limit: no room + CHECK(leonida::cgroup_memory_room((root / "self").string(), (root / "cg").string()) == 0); + write_file(root / "self", "12:memory:/legacy\n"); // cgroup v1 only: no limit read + CHECK(leonida::cgroup_memory_room((root / "self").string(), (root / "cg").string()) == UINT64_MAX); + CHECK(leonida::cgroup_memory_room((root / "missing").string(), (root / "cg").string()) == UINT64_MAX); + fs::remove_all(root); + return 0; +} +#endif + +int main() { +#if defined(__linux__) + if (int r = cgroup_limits_are_read_up_the_tree()) return r; +#endif + if (int r = budget_and_count()) return r; + if (int r = oversized_module_still_gets_in_alone()) return r; + if (int r = max_inflight_zero_means_one()) return r; + if (int r = ticket_releases()) return r; + if (int r = threads_never_exceed_the_budget()) return r; + if (int r = estimate_and_memory_query()) return r; + std::puts("large_module_gate: ok"); + return 0; +} -- cgit v1.2.3