From 07d5ecfacb4c90cfaf06ba23aa3f96733247c088 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Thu, 29 Oct 2026 00:01:54 +0100 Subject: [PATCH] engine: wait up to 20 s for the RAM the expert arena needs, then load with a plainer warning (#4051) An arena load with 10 GiB available for a 40 GiB arena sent a 60 GiB host into memory reclaim until it was reset. The check that is there warns only (a cgroup limit below the arena still stops the start), and its own example is an engine that is still exiting. Before the warning, the load now waits for the RAM to come back, re-reading it every 0.25 s for up to 20 s, and says so in the log every 5 s (STRATA_ARENA_RAM_WAIT_S changes the seconds, 0 loads at once). If it is still short the load goes on as before, with the numbers; below half of the arena the warning says the whole PC can stall in reclaim and names a systemd MemoryHigh= as the way to keep one program from taking all the RAM. Nothing is refused or capped. detail::wait_for_ram is the loop with its sampler and sleeper passed in, tested in file_expert_source_test (test_wait_for_ram). expert_source.cpp passes g++ -std=c++20 -fsyntax-only against the CUDA 13.1 headers; the engine itself was not built or run for this change. --- include/strata/core/expert_source.hpp | 11 +++++ src/core/expert_source.cpp | 65 +++++++++++++++++++++++--- tests/core/file_expert_source_test.cpp | 33 +++++++++++++ 3 files changed, 103 insertions(+), 6 deletions(-) diff --git a/include/strata/core/expert_source.hpp b/include/strata/core/expert_source.hpp index 2bcfca39..45ac7f74 100644 --- a/include/strata/core/expert_source.hpp +++ b/include/strata/core/expert_source.hpp @@ -36,6 +36,7 @@ #include #include #include +#include #include #include #include @@ -94,6 +95,16 @@ bool host_available_memory(HostMemory& m, const std::string& meminfo = "/proc/me const std::string& self_cgroup = "/proc/self/cgroup", const std::string& cgroup_root = "/sys/fs/cgroup"); +/// #4051: wait for the RAM an arena needs to come back. An engine that was still exiting (or a program that was ending) +/// holds memory a few seconds longer than its process; loading into that shortfall sent a 60 GiB host into reclaim for +/// good. Calls `sample(available)` (false: the RAM cannot be read; the wait ends) every `step_s` seconds, through +/// `sleep(step_s)`, until `available >= need` or `max_wait_s` seconds have gone. `waited` gets the seconds waited, +/// `available` the last reading. True when the RAM is there. `progress(waited, available)` is called about every +/// `note_s` seconds while waiting. `max_wait_s <= 0` is no wait at all. +bool wait_for_ram(uint64_t need, double max_wait_s, double step_s, double note_s, + const std::function& sample, const std::function& sleep, + const std::function& progress, double& waited, uint64_t& available); + /// Bound a resident budget by RAM and commit capacity after headroom; leave 256 MiB more when clamping. /// Pass UINT64_MAX for commit when the platform does not report it. uint64_t clamp_resident_budget(uint64_t requested, uint64_t physical, uint64_t commit, uint64_t headroom); diff --git a/src/core/expert_source.cpp b/src/core/expert_source.cpp index 1b5b1155..41d882dd 100644 --- a/src/core/expert_source.cpp +++ b/src/core/expert_source.cpp @@ -104,6 +104,24 @@ bool pin_depends_on_reclaim(uint64_t free_now, uint64_t bytes, uint64_t reserve) return bytes > 0 && free_now < bytes + reserve; } +bool wait_for_ram(uint64_t need, double max_wait_s, double step_s, double note_s, + const std::function& sample, const std::function& sleep, + const std::function& progress, double& waited, uint64_t& available) { + waited = 0.0; + if (!sample(available)) return false; + double next_note = 0.0; + while (available < need && waited < max_wait_s) { + if (progress && waited >= next_note) { + progress(waited, available); + next_note = waited + note_s; + } + sleep(step_s); + waited += step_s; + if (!sample(available)) return false; + } + return available >= need; +} + bool pin_step_fits(uint64_t available, uint64_t step, uint64_t reserve) { return available >= step + reserve; } @@ -5820,12 +5838,47 @@ bool ArenaExpertSource::open(const std::string& pack_dir, int64_t n_layers, int6 return false; } if (hm.available < need) { - std::snprintf(buf, sizeof buf, - "the expert arena needs %.2f GiB of RAM but %.2f GiB is available (%.2f GiB short): the load " - "may swap or be stopped by the OS. Close other programs (or wait for an engine that is " - "still exiting), or run with less RAM: --mmap-experts with --resident-budget-gib N", - (double) need / gib, (double) hm.available / gib, (double) (need - hm.available) / gib); - ram_warning_ = buf; + // #4051: the shortfall is often an engine that was still exiting (the code's own example), a few seconds + // from giving its RAM back: wait for it, not for ever, and say so. STRATA_ARENA_RAM_WAIT_S changes the + // seconds (0 loads at once, as before this wait). The load then goes on either way - it only warns. + double wait_s = 20.0; + if (const char* w = std::getenv("STRATA_ARENA_RAM_WAIT_S"); w != nullptr && w[0] != '\0') { + char* end = nullptr; + const double v = std::strtod(w, &end); + if (end != w && *end == '\0' && v >= 0.0 && v <= 3600.0) wait_s = v; + } + double waited = 0.0; + uint64_t avail = hm.available; + if (wait_s > 0.0) { + const bool came = detail::wait_for_ram( + need, wait_s, 0.25, 5.0, + [](uint64_t& a) { detail::HostMemory h; if (!detail::host_available_memory(h)) return false; a = h.available; return true; }, + [](double s) { std::this_thread::sleep_for(std::chrono::duration(s)); }, + [&](double w, uint64_t a) { + std::fprintf(stderr, "strata generate: the expert arena needs %.2f GiB of RAM and %.2f GiB is " + "available; waiting up to %.0f s for it (another engine still exiting?) - " + "%.0f s so far (STRATA_ARENA_RAM_WAIT_S changes this)\n", + (double) need / gib, (double) a / gib, wait_s, w); + }, + waited, avail); + if (came) + std::fprintf(stderr, "strata generate: %.2f GiB of RAM is available now (after %.1f s): loading the " + "expert arena\n", (double) avail / gib, waited); + hm.available = avail; + } + if (hm.available < need) { + std::snprintf(buf, sizeof buf, + "the expert arena needs %.2f GiB of RAM but %.2f GiB is available (%.2f GiB short%s): the " + "load may swap or be stopped by the OS%s. Close other programs, or run with less RAM: " + "--mmap-experts with --resident-budget-gib N", + (double) need / gib, (double) hm.available / gib, (double) (need - hm.available) / gib, + waited > 0.0 ? ", after waiting" : "", + hm.available < need / 2 ? " - with this little room the whole machine can stall in " + "memory reclaim until it is restarted (#4051); on Linux a " + "systemd MemoryHigh= below the RAM keeps one program from " + "taking all of it" : ""); + ram_warning_ = buf; + } } } diff --git a/tests/core/file_expert_source_test.cpp b/tests/core/file_expert_source_test.cpp index d6a274f1..3f3cf532 100644 --- a/tests/core/file_expert_source_test.cpp +++ b/tests/core/file_expert_source_test.cpp @@ -369,6 +369,38 @@ void test_resident_exchange() { require(offsets == before, "exchanging back did not restore the plan"); } +// #4051: the wait for the RAM an arena needs (an engine still exiting gives it back a few seconds late) +void test_wait_for_ram() { + using namespace strata::core::detail; + constexpr uint64_t GiB = 1ull << 30; + double waited = -1.0; + uint64_t avail = 0; + int slept = 0, noted = 0; + const auto sleeper = [&](double) { ++slept; }; + const auto note = [&](double, uint64_t) { ++noted; }; + // enough at once: no wait, no sleep, no note + require(wait_for_ram(40 * GiB, 20.0, 0.25, 5.0, [](uint64_t& a) { a = 50 * GiB; return true; }, sleeper, note, waited, avail) && + waited == 0.0 && slept == 0 && noted == 0 && avail == 50 * GiB, "RAM that was there was waited for"); + // 10 GiB free for a 40 GiB arena, the exiting engine gives its 50 GiB back after 3 s (12 steps of 0.25 s) + int reads = 0; + slept = noted = 0; + require(wait_for_ram(40 * GiB, 20.0, 0.25, 5.0, [&](uint64_t& a) { a = ++reads > 12 ? 60 * GiB : 10 * GiB; return true; }, + sleeper, note, waited, avail) && waited == 3.0 && slept == 12 && noted == 1 && avail == 60 * GiB, + "RAM that came back after 3 s was not waited for"); + // it never comes: the wait ends at the limit and says so (the load then goes on and warns) + slept = noted = 0; + require(!wait_for_ram(40 * GiB, 20.0, 0.25, 5.0, [](uint64_t& a) { a = 10 * GiB; return true; }, sleeper, note, waited, avail) && + waited == 20.0 && slept == 80 && noted == 4 && avail == 10 * GiB, "the wait did not end at its limit"); + // no wait asked for: one reading + slept = 0; + require(!wait_for_ram(40 * GiB, 0.0, 0.25, 5.0, [](uint64_t& a) { a = 10 * GiB; return true; }, sleeper, note, waited, avail) && + waited == 0.0 && slept == 0, "a zero wait slept"); + // the RAM cannot be read: the wait ends at once + slept = 0; + require(!wait_for_ram(40 * GiB, 20.0, 0.25, 5.0, [](uint64_t&) { return false; }, sleeper, note, waited, avail) && slept == 0, + "an unreadable RAM was waited for"); +} + // #1250: when the page-locked complement is registered in steps, and what a meminfo reading says void test_pin_pacing() { using namespace strata::core::detail; @@ -1014,6 +1046,7 @@ int main(int argc, char** argv) { test_resident_exchange(); test_resident_memory_budget(); test_pin_pacing(); + test_wait_for_ram(); test_numa_slice_ranges(); test_cgroup_memory_budget(); test_host_memory();