memory guard: parking and session save/restore read the RAM the engine really has (--memory-limit-mib)

available_host_bytes() = min(MemAvailable, the room under every cgroup limit above the engine, the operator's cap).
Parking and session save/restore read MemAvailable alone: in a container whose real limit sits on a parent cgroup it
cannot see, MemAvailable is stale (about 25 GiB shown when about 1 GiB was left) and the engine was OOM-killed.

conversation_available_memory() is replaced by available_host_bytes() / sample_host_memory() (the SYCL port's
generate.cpp too). The cgroup term covers memory.max and memory.high (v2) and memory.limit_in_bytes (v1) of the
process's group and every visible ancestor, with clean inactive file cache credited; an unparsable cgroup file makes the
sample unknown (fail closed). --memory-limit-mib / STRATA_MEMORY_LIMIT_MIB names the total RAM the container or cgroup
may use. Refusals say how much was available and from which source; one start-up line reports the limit.
tests/core/memory_guard_test.cpp runs on fixture directories (CPU only). Docs: DETAILS.md.
This commit is contained in:
noon-at-cgn authored and Niko1221 committed 2026-10-09 03:28:31 +02:00
1 parent bbb74cfca4
commit daa765524d
8 files changed
+797 -35

No files matched your search

+68 -15
View File
@@ -596,6 +596,9 @@ struct Options {
/// conversation_cache_slots whatever its length, so without it a client that interleaves short side requests
/// with one long conversation evicts the long one (oldest first). 0 = park every conversation.
int64_t conversation_cache_min_tokens = 0;
/// --memory-limit-mib N / STRATA_MEMORY_LIMIT_MIB: the total RAM this engine's container or cgroup may use, for a
/// container that cannot see its real limit (0 or unset = none; the flag wins over the variable)
int64_t memory_limit_mib = -1;
/// --serve SAVE: disk space a session file must leave free where it is written (MiB; 0 = no check)
int64_t session_min_free_mib = 4096;
bool session_save_reclaim = false;
@@ -739,8 +742,11 @@ void usage() {
" of RAM each; 0 = read every prompt from the start)\n"
" --conversation-cache-mib N --serve: RAM budget for parked conversations (default 0 = off)\n"
" --conversation-cache-slots N --serve: at most N parked conversations (default 4)\n"
" --conversation-cache-min-free-mib N --serve: physical RAM floor when parking or restoring a\n"
" session file (default 2560)\n"
" --conversation-cache-min-free-mib N --serve: RAM floor (under MemAvailable, the cgroup limits and\n"
" --memory-limit-mib) when parking, saving or restoring a session (default 2560)\n"
" --memory-limit-mib N --serve: the total RAM (MiB) this engine's container or cgroup may use, when\n"
" the container cannot see its real limit (also STRATA_MEMORY_LIMIT_MIB; the flag\n"
" wins; 0 = none). Parking and session save/restore keep their floor under it\n"
" --conversation-cache-min-tokens N --serve: park only conversations of at least N tokens, so\n"
" short side requests do not take the slots a long one needs (default 0 = park any)\n"
" --session-min-free-mib N --serve: disk space a SAVE must leave free (default 4096; 0 = no check)\n"
@@ -1033,6 +1039,13 @@ struct GpuStage {
int32_t* mrope = nullptr; ///< --vision: the image-position table on its device
};
// what a refusal on the RAM guard says it had: "900 MiB available, source cgroup" (the smallest of MemAvailable, the
// cgroup limits above the engine and --memory-limit-mib; see strata/core/conversation_memory.hpp)
static std::string ram_note(const std::optional<strata::core::HostMemoryReading>& mem) {
if (!mem) return "RAM telemetry unavailable";
return std::to_string(mem->available >> 20) + " MiB available, source " + strata::core::memory_source_name(mem->source);
}
// ---- issue #31: what the watchdog prints before it stops a stalled engine
struct MemSample {
unsigned long long faults = 0, rss_mib = 0, avail_mib = 0, commit_mib = 0; // faults: hard (Linux) / all (Windows)
@@ -1825,7 +1838,7 @@ int main(int argc, char** argv) {
else if (a == "--prompt-cache") o.prompt_cache = std::max(0, std::atoi(next("--prompt-cache")));
else if (a == "--conversation-cache-mib" || a == "--conversation-cache-slots" ||
a == "--conversation-cache-min-free-mib" || a == "--conversation-cache-min-tokens" ||
a == "--session-min-free-mib") {
a == "--session-min-free-mib" || a == "--memory-limit-mib") {
const std::string value = next(a.c_str());
int64_t number = 0;
const auto result = std::from_chars(value.data(), value.data() + value.size(), number);
@@ -1839,6 +1852,7 @@ int main(int argc, char** argv) {
else if (a == "--conversation-cache-min-free-mib") o.conversation_cache_min_free_mib = number;
else if (a == "--conversation-cache-min-tokens") o.conversation_cache_min_tokens = number;
else if (a == "--session-min-free-mib") o.session_min_free_mib = number;
else if (a == "--memory-limit-mib") o.memory_limit_mib = number;
else o.conversation_cache_slots = (int) number;
}
else if (a == "--prompt-cache-tail") o.prompt_cache_tail = true;
@@ -1978,6 +1992,38 @@ int main(int argc, char** argv) {
if (hc == "last") strata::kernels::cpu::set_host_core(strata::kernels::cpu::HostCore::Last);
}
strata::core::set_peer_portable(o.peer_device >= 1); // multi-GPU: the Portable flag on mapped host buffers only with a peer device (before any allocation)
{ // the RAM guard (parking, session save/restore): --memory-limit-mib, else STRATA_MEMORY_LIMIT_MIB, else none
int64_t mib = o.memory_limit_mib;
if (mib < 0) {
const char* env = std::getenv("STRATA_MEMORY_LIMIT_MIB");
if (env != nullptr && env[0] != '\0') {
const size_t len = std::strlen(env);
const auto parsed = std::from_chars(env, env + len, mib);
if (parsed.ec != std::errc{} || parsed.ptr != env + len || mib < 0 || mib > INT64_MAX / (1024 * 1024)) {
std::fprintf(stderr, "STRATA_MEMORY_LIMIT_MIB needs a nonnegative integer within range (got \"%s\")\n", env);
return 2;
}
} else mib = 0;
}
strata::core::set_memory_limit_bytes((uint64_t) mib << 20);
if (o.serve || mib > 0) {
constexpr double GiB = 1073741824.0;
const auto mem = strata::core::sample_host_memory();
if (!mem) {
std::fprintf(stderr, "strata generate: memory guard: the available RAM cannot be read; parking and session "
"save/restore are refused\n");
} else {
std::fprintf(stderr, "strata generate: memory guard: limit %.1f GiB (source: %s), current %.1f GiB, "
"reclaimable cache credited %.1f GiB; available %.1f GiB (MemAvailable %.1f GiB%s), "
"floor %lld MiB\n",
(double) mem->limit / GiB, strata::core::memory_source_name(mem->source),
(double) mem->current / GiB, (double) mem->credit / GiB, (double) mem->available / GiB,
(double) mem->mem_available / GiB,
mib > 0 ? (", --memory-limit-mib " + std::to_string((long long) mib)).c_str() : "",
(long long) o.conversation_cache_min_free_mib);
}
}
}
if (o.serve && o.conversation_cache_mib > 0 && (o.prompt_cache == 0 || o.conversation_cache_slots == 0))
std::fprintf(stderr, "strata serve: warning: conversation caching is disabled by %s\n",
o.prompt_cache == 0 ? "--prompt-cache 0" : "--conversation-cache-slots 0");
@@ -7156,18 +7202,21 @@ int main(int argc, char** argv) {
// left. Each ConversationBuffer is a list of 16 MiB segments, each segment its own
// allocation, so an evicted entry is back with the kernel before the next check reads
// /proc/meminfo; no waiting is needed. slots() bounds the loop, and the two lines
// below say what it did either way.
// below say what it did either way. The reading is the guard's (MemAvailable, the cgroup
// limits above the engine, --memory-limit-mib); `mem` keeps the last one for the refusal line.
size_t evicted = 0;
std::optional<strata::core::HostMemoryReading> mem;
auto admit = [&] {
mem = strata::core::sample_host_memory();
return strata::core::conversation_memory_admit(
strata::core::conversation_available_memory(), additional, floor);
mem ? std::optional<uint64_t>(mem->available) : std::nullopt, additional, floor);
};
while (!admit() && conversations.size() > 0 && evicted < conversations.slots() &&
conversations.evict_oldest())
++evicted;
if (!admit()) {
std::fprintf(stderr, "strata serve: conversation cache: skip parking (physical RAM admission; need %zu MiB plus %lld MiB floor; evicted %zu, %zu still parked, or telemetry unavailable)\n",
additional >> 20, (long long) o.conversation_cache_min_free_mib,
std::fprintf(stderr, "strata serve: conversation cache: skip parking (physical RAM admission; need %zu MiB plus %lld MiB floor, %s; evicted %zu, %zu still parked)\n",
additional >> 20, (long long) o.conversation_cache_min_free_mib, ram_note(mem).c_str(),
evicted, conversations.size());
return true;
}
@@ -7190,8 +7239,11 @@ int main(int argc, char** argv) {
return false;
image.stage_images.push_back(std::move(part));
}
if (!strata::core::conversation_memory_admit(strata::core::conversation_available_memory(), 0, floor)) {
std::fprintf(stderr, "strata serve: conversation cache: skip parking (physical RAM floor after capture, or telemetry unavailable)\n");
const auto mem_after = strata::core::sample_host_memory();
if (!strata::core::conversation_memory_admit(
mem_after ? std::optional<uint64_t>(mem_after->available) : std::nullopt, 0, floor)) {
std::fprintf(stderr, "strata serve: conversation cache: skip parking (RAM floor after capture: %lld MiB needed, %s)\n",
(long long) o.conversation_cache_min_free_mib, ram_note(mem_after).c_str());
return true;
}
const size_t snapshot_bytes = image.bytes();
@@ -8815,7 +8867,7 @@ int main(int argc, char** argv) {
const uint64_t floor = (uint64_t) o.conversation_cache_min_free_mib << 20;
if (o.session_save_reclaim) {
const auto freed = strata::core::session_save_reclaim(
checks, conversations, sl, floor, strata::core::conversation_available_memory);
checks, conversations, sl, floor, strata::core::available_host_bytes);
if (freed.bytes || freed.checkpoints || freed.parked) {
// A removed tail must not mark a later checkpoint at this length as a tail.
if (tail_ckpt_len >= 0 && std::none_of(checks.begin(), checks.end(), [&](const auto& c) {
@@ -8827,13 +8879,13 @@ int main(int argc, char** argv) {
}
}
auto admit = [&](uint64_t need, std::string& why) {
const auto avail = strata::core::conversation_available_memory();
const auto mem = strata::core::sample_host_memory();
const std::optional<uint64_t> avail = mem ? std::optional<uint64_t>(mem->available) : std::nullopt;
if (strata::core::conversation_memory_admit(avail, need, floor)) return true;
why = "not enough RAM to save the session (" +
(need == UINT64_MAX ? std::string("unknown") : std::to_string(need >> 20)) +
" MiB plus a floor of " + std::to_string((long long) o.conversation_cache_min_free_mib) +
" MiB needed, " + (avail ? std::to_string(*avail >> 20) + " MiB available)"
: "RAM telemetry unavailable)");
" MiB needed, " + ram_note(mem) + ")";
return false;
};
std::vector<ConvCheckpoint> disk_checks;
@@ -8885,11 +8937,12 @@ int main(int argc, char** argv) {
limits.progress = moving;
const uint64_t floor = (uint64_t) o.conversation_cache_min_free_mib << 20;
limits.admit = [floor, &o](uint64_t need, std::string& why) {
const auto avail = strata::core::conversation_available_memory();
const auto mem = strata::core::sample_host_memory();
const std::optional<uint64_t> avail = mem ? std::optional<uint64_t>(mem->available) : std::nullopt;
if (strata::core::conversation_memory_admit(avail, need, floor)) return true;
why = "not enough RAM to read it (" + std::to_string(need >> 20) + " MiB plus a floor of " +
std::to_string((long long) o.conversation_cache_min_free_mib) + " MiB needed, " +
(avail ? std::to_string(*avail >> 20) + " MiB available)" : "RAM telemetry unavailable)");
ram_note(mem) + ")";
return false;
};
if (!strata::core::conversation_session_read_limits(