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.
This commit is contained in:
Niko1221 committed 2026-10-29 00:01:54 +01:00
1 parent d71132738e
commit 07d5ecfacb
3 files changed
+103 -6

No files matched your search

+59 -6
View File
@@ -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<bool(uint64_t&)>& sample, const std::function<void(double)>& sleep,
const std::function<void(double, uint64_t)>& 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<double>(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;
}
}
}