From 6c6a8cd47d74e5dc94751f4efdaa603e893b20f9 Mon Sep 17 00:00:00 2001 From: c0pperlane <84517100+c0pperlane@users.noreply.github.com> Date: Wed, 25 Nov 2026 20:34:18 +0100 Subject: [PATCH] vk_pipeline_check: a large module that must wait frees its worker (VK-PIPELINE-CHECK-LARGE-ADMISSION) A worker took the next index and only then waited in the large-module gate, so with the memory budget full the modules behind a large one sat untaken while spare workers blocked, and the pool ran below --jobs. LargeModuleQueue (large_module_gate.h) now hands out the indices. A large module the gate can't admit yet is put back and the worker takes the next one. The oldest module put back goes first as soon as the gate admits it and no later large module passes it; once every index is out, a worker waits for it. Nothing is put back while the gate admits each large module as it comes, so with one worker or room under the budget the order is unchanged, and --large-parallel 1 and --large-module-bytes 0 never ask the queue to admit. large_module_gate_test: the waiting module frees its worker (fails with the old claim-then-admit order), oldest-first and no double entry, the unchanged order, and every module once under 8 threads. AI-assisted (Claude Code). --- docs/pipeline_check.md | 4 +- runtime/test/large_module_gate.h | 73 ++++++++++++ runtime/test/large_module_gate_test.cpp | 150 ++++++++++++++++++++++++ runtime/test/vk_pipeline_check.cpp | 12 +- 4 files changed, 232 insertions(+), 7 deletions(-) diff --git a/docs/pipeline_check.md b/docs/pipeline_check.md index d5d76cb931..bf1295c95b 100644 --- a/docs/pipeline_check.md +++ b/docs/pipeline_check.md @@ -259,7 +259,9 @@ showed why that matters: several compiles that each needed gigabytes of host RAM modules now share a memory budget (`runtime/test/large_module_gate.h`): each one reserves 512 bytes of RAM per SPIR-V byte, and they enter the driver while their reservations fit three quarters of the memory available when the run starts, up to `--jobs` of them. One large module always gets in when none is compiling, so a module -bigger than the whole budget still compiles, alone among the large ones. Small modules are never held back. On +bigger than the whole budget still compiles, alone among the large ones. Small modules are never held back, and a +large module that doesn't fit yet doesn't hold its thread: it goes back in the queue and the thread takes the next +module. The oldest one put back gets in first once it fits, and no later large module passes it. On Linux the budget is the smaller of `MemAvailable` and the room under the cgroup v2 `memory.max` of the process and every cgroup above it (a container, or `systemd-run -p MemoryMax=...`); on Windows it is `ullAvailPhys`. Where neither can be read, every large module compiles alone, as before. After each large module, glibc builds call diff --git a/runtime/test/large_module_gate.h b/runtime/test/large_module_gate.h index ea805c9268..a7d95e4dc9 100644 --- a/runtime/test/large_module_gate.h +++ b/runtime/test/large_module_gate.h @@ -9,14 +9,22 @@ // The process grew past one module's peak even one at a time because glibc keeps freed memory in per-thread arenas // (6 threads: 6.2 GB; MALLOC_ARENA_MAX=1: 2.1 GB, same modules). release_free_memory() hands it back after each // large module. +// +// A worker used to take the next module and then wait in the gate when it was large and didn't fit, so with the +// budget full the other modules sat untaken behind it and the pool ran below --jobs. LargeModuleQueue now puts such +// a module back and the worker takes the next one. #pragma once #include #include +#include #include #include +#include #include +#include #include +#include #ifdef _WIN32 #ifndef NOMINMAX @@ -125,6 +133,14 @@ class LargeModuleGate { used_ += estimate; ++inflight_; } + // enter() only if the gate admits the module now; false, with nothing taken, otherwise. + bool try_enter(uint64_t estimate) { + std::lock_guard g(mu_); + if (!admits_locked(estimate)) return false; + used_ += estimate; + ++inflight_; + return true; + } void leave(uint64_t estimate) { { std::lock_guard g(mu_); @@ -145,6 +161,9 @@ class LargeModuleGate { public: Ticket() = default; Ticket(LargeModuleGate& gate, uint64_t estimate) : gate_(&gate), estimate_(estimate) { gate.enter(estimate); } + // A place only if the gate admits the module now (as std::unique_lock with std::try_to_lock); empty if not. + Ticket(LargeModuleGate& gate, uint64_t estimate, std::try_to_lock_t) + : gate_(gate.try_enter(estimate) ? &gate : nullptr), estimate_(estimate) {} Ticket(Ticket&& o) noexcept : gate_(o.gate_), estimate_(o.estimate_) { o.gate_ = nullptr; } Ticket& operator=(Ticket&& o) noexcept { if (this != &o) { release(); gate_ = o.gate_; estimate_ = o.estimate_; o.gate_ = nullptr; } @@ -153,6 +172,7 @@ class LargeModuleGate { Ticket(const Ticket&) = delete; Ticket& operator=(const Ticket&) = delete; ~Ticket() { release(); } + explicit operator bool() const { return gate_ != nullptr; } void release() { if (!gate_) return; gate_->leave(estimate_); @@ -178,4 +198,57 @@ class LargeModuleGate { unsigned inflight_ = 0; }; +// The order vk_pipeline_check's workers take modules 0..count-1 in. A large module the gate doesn't admit yet is put +// back (admit() returns false) and the worker takes the next module. The oldest module put back goes first as soon +// as the gate admits it, and no later large module passes it, so none waits forever; once every index is out, a +// worker waits for it. Nothing is put back while the gate admits each large module as it comes (one worker, or room +// under the budget), so then the order is 0, 1, 2... as before. +class LargeModuleQueue { +public: + struct Claim { + size_t index = 0; + LargeModuleGate::Ticket ticket; // a module put back comes back with its place in the gate + }; + + LargeModuleQueue(LargeModuleGate& gate, size_t count) : gate_(gate), count_(count) {} + + // The next module, or none when every one is out. Drop the previous claim first: its ticket counts in the gate. + std::optional take() { + std::lock_guard g(mu_); + if (!waiting_.empty()) { + const auto [index, estimate] = waiting_.front(); + LargeModuleGate::Ticket ticket(gate_, estimate, std::try_to_lock); + // Nothing new left: wait for its place, holding mu_ so no other module passes it. The gate frees places + // without mu_, and a worker that blocks on mu_ holds no ticket. + if (!ticket && next_ == count_) ticket = LargeModuleGate::Ticket(gate_, estimate); + if (ticket) { + waiting_.pop_front(); + return Claim{index, std::move(ticket)}; + } + } + if (next_ == count_) return std::nullopt; + return Claim{next_++, {}}; + } + + // A large module's place in the gate: true when the claim holds one (it came back with it, or the gate admits + // the module now); false when the module was put back, and the worker drops the claim and takes the next one. + bool admit(Claim& claim, uint64_t estimate) { + if (claim.ticket) return true; + std::lock_guard g(mu_); + if (waiting_.empty()) { // otherwise the module waiting goes first + claim.ticket = LargeModuleGate::Ticket(gate_, estimate, std::try_to_lock); + if (claim.ticket) return true; + } + waiting_.emplace_back(claim.index, estimate); + return false; + } + +private: + LargeModuleGate& gate_; + const size_t count_; + std::mutex mu_; + size_t next_ = 0; + std::deque> waiting_; // put back: index and estimate, oldest first +}; + } // namespace leonida diff --git a/runtime/test/large_module_gate_test.cpp b/runtime/test/large_module_gate_test.cpp index d114095fd5..ce39ec88f8 100644 --- a/runtime/test/large_module_gate_test.cpp +++ b/runtime/test/large_module_gate_test.cpp @@ -7,7 +7,9 @@ #include #endif #include +#include #include +#include #include #include @@ -19,6 +21,7 @@ } while (0) using leonida::LargeModuleGate; +using leonida::LargeModuleQueue; static int budget_and_count() { LargeModuleGate gate(10, 3); @@ -102,6 +105,149 @@ static int threads_never_exceed_the_budget() { return 0; } +// A worker of vk_pipeline_check over modules given by their estimate (0: a small one): enter(k) is module k going +// into the driver, while its claim (and ticket) is held. +template +static void run_worker(LargeModuleQueue& queue, const std::vector& estimates, Enter enter) { + while (auto claim = queue.take()) { + const uint64_t estimate = estimates[claim->index]; + if (estimate && !queue.admit(*claim, estimate)) continue; + enter(claim->index); + } +} + +static int a_waiting_large_module_frees_its_worker() { + // Before: the worker took module 0 and waited in the gate behind another worker's large module, and the small + // modules 1 and 2 waited untaken until that one was done. + LargeModuleGate gate(10, 4); + LargeModuleGate::Ticket other(gate, 8); // another worker's large module, in the driver + const std::vector estimates{6, 0, 0}; // 8 + 6 > 10 + LargeModuleQueue queue(gate, estimates.size()); + std::mutex mu; + std::condition_variable cv; + std::vector entered, while_other_in; + std::thread worker([&] { + run_worker(queue, estimates, [&](size_t k) { + { + std::lock_guard g(mu); + entered.push_back(k); + } + cv.notify_all(); + }); + }); + { + std::unique_lock l(mu); + cv.wait_for(l, std::chrono::seconds(10), [&] { return entered.size() == 2; }); + while_other_in = entered; + } + other.release(); + worker.join(); + CHECK((while_other_in == std::vector{1, 2})); + CHECK((entered == std::vector{1, 2, 0})); // and module 0 once the other one was done + CHECK(gate.inflight() == 0); + return 0; +} + +static int the_oldest_module_put_back_goes_first() { + LargeModuleGate gate(10, 4); + LargeModuleQueue queue(gate, 4); + LargeModuleGate::Ticket other(gate, 8); + auto a = queue.take(); + CHECK(a && a->index == 0 && !queue.admit(*a, 6)); // 8 + 6 > 10: put back + a.reset(); + auto b = queue.take(); + CHECK(b && b->index == 1 && !queue.admit(*b, 2)); // 8 + 2 fits, but module 0 waits ahead of it + b.reset(); + auto c = queue.take(); + CHECK(c && c->index == 2 && !c->ticket); // a small module meanwhile + c.reset(); + other.release(); + a = queue.take(); // before the new module 3 + CHECK(a && a->index == 0 && a->ticket && gate.inflight() == 1); + CHECK(queue.admit(*a, 6) && gate.inflight() == 1); // it came back with its place: not counted twice + b = queue.take(); + CHECK(b && b->index == 1 && b->ticket && gate.inflight() == 2); // 6 + 2 fits + c = queue.take(); + CHECK(c && c->index == 3 && !c->ticket); + CHECK(!queue.admit(*c, 4) && gate.inflight() == 2); // 6 + 2 + 4 > 10 + c.reset(); + a.reset(); + c = queue.take(); + CHECK(c && c->index == 3 && c->ticket && gate.inflight() == 2); // 2 + 4 fits + b.reset(); + c.reset(); + CHECK(!queue.take()); + CHECK(gate.inflight() == 0); + return 0; +} + +static int nothing_is_put_back_while_the_gate_admits() { + // One worker: no other large module is in when one comes, so even one over the budget gets in and the order is + // the old one. + LargeModuleGate gate(10, 2); + const std::vector estimates{6, 0, 100, 6, 0, 9}; + LargeModuleQueue queue(gate, estimates.size()); + std::vector entered; + run_worker(queue, estimates, [&](size_t k) { entered.push_back(k); }); + CHECK((entered == std::vector{0, 1, 2, 3, 4, 5})); + // --large-parallel 2 (a count, no memory budget) with a place left beside another worker's large module. + LargeModuleGate counted(UINT64_MAX, 2); + LargeModuleGate::Ticket other(counted, 8); + LargeModuleQueue roomy(counted, 3); + entered.clear(); + run_worker(roomy, {6, 0, 6}, [&](size_t k) { entered.push_back(k); }); + CHECK((entered == std::vector{0, 1, 2})); + other.release(); + // The gate disabled (--large-module-bytes 0) or --large-parallel 1 (its exclusive lock): admit() is never asked, + // and the queue hands out 0, 1, 2 as the old counter did. + LargeModuleQueue counter(gate, 3); + for (size_t k = 0; k < 3; ++k) { + auto claim = counter.take(); + CHECK(claim && claim->index == k && !claim->ticket); + } + CHECK(!counter.take()); + CHECK(gate.inflight() == 0 && counted.inflight() == 0); + return 0; +} + +static int threads_check_every_module_once() { + constexpr uint64_t kBudget = 10; + LargeModuleGate gate(kBudget, 3); + std::vector estimates(300); + for (size_t k = 0; k < estimates.size(); ++k) + estimates[k] = k % 3 ? 0 : 1 + (k * 7) % 12; // a third large, 1..12: some over the budget alone + LargeModuleQueue queue(gate, estimates.size()); + std::mutex mu; + std::vector entered(estimates.size()); + 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([&] { + run_worker(queue, estimates, [&](size_t k) { + { + std::lock_guard g(mu); + ++entered[k]; + } + const uint64_t est = estimates[k]; + if (!est) return; + 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(); + for (const int n : entered) CHECK(n == 1); // every module once, none lost while put back + 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) @@ -162,6 +308,10 @@ int main() { 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 = a_waiting_large_module_frees_its_worker()) return r; + if (int r = the_oldest_module_put_back_goes_first()) return r; + if (int r = nothing_is_put_back_while_the_gate_admits()) return r; + if (int r = threads_check_every_module_once()) return r; if (int r = estimate_and_memory_query()) return r; std::puts("large_module_gate: ok"); return 0; diff --git a/runtime/test/vk_pipeline_check.cpp b/runtime/test/vk_pipeline_check.cpp index 0e39d0f621..b8139ec239 100644 --- a/runtime/test/vk_pipeline_check.cpp +++ b/runtime/test/vk_pipeline_check.cpp @@ -1454,6 +1454,7 @@ int main(int argc, char** argv) { leonida::LargeModuleGate large_gate( large_parallel == 0 ? available_memory / 4 * 3 : UINT64_MAX, large_parallel == 0 ? jobs : std::min(large_parallel, jobs)); + leonida::LargeModuleQueue work_queue(large_gate, work.size()); // a large module that must wait frees its thread if (large_module_bytes) { if (large_exclusive) std::fprintf(stderr, "large modules (over %zu bytes): one at a time, the other threads waiting\n", @@ -1469,7 +1470,7 @@ int main(int argc, char** argv) { std::map modules_ok, modules_bad, big_skipped; std::map stats; std::map stub_cache; - std::atomic next{0}, done{0}; + std::atomic done{0}; std::atomic total_pipes_done{0}; // What each worker is doing in the driver: creating the shader module, then creating its pipeline. struct InFlight { @@ -1597,7 +1598,8 @@ int main(int argc, char** argv) { }; auto worker = [&] { - for (size_t k; (k = next++) < work.size();) { + while (auto claim = work_queue.take()) { // its ticket is left after the module and its pipeline are gone + const size_t k = claim->index; const std::string& name = *work[k].first; const ShaderMeta& meta = *work[k].second; const std::string& stage = meta.stage; @@ -1648,13 +1650,11 @@ int main(int argc, char** argv) { const bool isolated = large_module_bytes && std::cmp_greater(file_size, large_module_bytes); std::unique_lock exclusive(compile_gate, std::defer_lock); std::shared_lock parallel(compile_gate, std::defer_lock); - leonida::LargeModuleGate::Ticket large_ticket; // left after the module and its pipeline are gone if (large_exclusive) { if (isolated) exclusive.lock(); else if (large_module_bytes) parallel.lock(); - } else if (isolated) { - large_ticket = leonida::LargeModuleGate::Ticket(large_gate, - leonida::large_module_estimate(file_size)); + } else if (isolated && !work_queue.admit(*claim, leonida::large_module_estimate(file_size))) { + continue; // not within the budget yet: put back, and this thread takes the next module } { // in flight from here: vkCreateShaderModule can also take long or die in the driver std::lock_guard g(mu);