--host-headroom-mib N: the RAM radiance leaves free for the system at startup becomes a flag -- kLockMargin's fixed 4 GiB suited a busy server, and a 30 GiB host paid it in dead RAM to no purpose: startup refuses a host pin unless available memory covers it with that much still free, and --checkpoint-slots auto's bound is priced with it; so when 1.7.0 began counting the checkpoint store against this check, configs that 1.6.0 ran refused to start and the fix cost a quarter of the prefix cache.

This commit is contained in:
sfjes authored and StillDeadcode committed 2026-10-19 10:35:58 +02:00
1 parent d37639ef71
commit f10c8521cd
7 files changed
+57 -36

No files matched your search

+36 -29
View File
@@ -275,10 +275,11 @@ static void report_pool_refusal(int rank, int device, const Pools& pools, const
}
/* ------------------------------------------------------------------ locked host memory */
/* Host memory left free beside a table locked whole: the process's unpooled allocations, the
* load's staging windows, and the page cache holding the container's hot pages. Without it the
* kernel reclaims the files the engine reads, then the OOM killer runs. */
static const int64_t kLockMargin = (int64_t)4 << 30;
/* The margin (--host-headroom-mib, Engine::host_headroom_bytes): host memory left free beside a
* table locked whole -- the process's unpooled allocations, the load's staging windows, and the
* page cache holding the container's hot pages. Without it the kernel reclaims the files the
* engine reads, then the OOM killer runs. It is dead RAM by design; a host that can reclaim
* cheaply trades it for pool, cache or slots. */
/* Every level of every memory cgroup the process is in, leaf first: `visit(dir, room)` until it
* returns false (cgroup_levels). /proc/self/cgroup reads "0::/path" on v2 and "N:memory:/path"
@@ -309,12 +310,13 @@ static void memory_cgroups(const std::function<bool(const std::string&, int64_t)
}
/* Whether `need` more bytes fit in host memory after `still` more this process will allocate
* first, or why not. Checks MemAvailable; every memory cgroup from the process's own up to the
* visible root (Docker's --memory, enforced by the OOM killer); and, for an mlock()ed copy
* (`mlocks`), RLIMIT_MEMLOCK unless the process has CAP_IPC_LOCK. Pinned memory a card reads is a
* driver userptr mapping, which RLIMIT_MEMLOCK does not count. An unreadable limit is ignored,
* never guessed. */
static bool lockable_room(int64_t need, int64_t still, bool mlocks, std::string* why) {
* first and `margin` (--host-headroom-mib) stays free, or why not. Checks MemAvailable; every
* memory cgroup from the process's own up to the visible root (Docker's --memory, enforced by
* the OOM killer); and, for an mlock()ed copy (`mlocks`), RLIMIT_MEMLOCK unless the process has
* CAP_IPC_LOCK. Pinned memory a card reads is a driver userptr mapping, which RLIMIT_MEMLOCK
* does not count. An unreadable limit is ignored, never guessed. */
static bool lockable_room(int64_t need, int64_t still, int64_t margin, bool mlocks,
std::string* why) {
long long locked_kb = 0;
unsigned long long caps = 0;
{
@@ -334,43 +336,44 @@ static bool lockable_room(int64_t need, int64_t still, bool mlocks, std::string*
return false;
}
if (const int64_t avail = meminfo_bytes("MemAvailable");
avail >= 0 && avail - still - kLockMargin < need) {
avail >= 0 && avail - still - margin < need) {
*why = fmt("%s of memory is available, %s more is still to be allocated (the pinned host "
"pools, the prefix cache's host tier, the checkpoint slots and the host "
"activation arenas), and this wants %s with %s to spare",
"activation arenas), and this wants %s with %s to spare "
"(--host-headroom-mib)",
humanb(avail).c_str(), humanb(still).c_str(), humanb(need).c_str(),
humanb(kLockMargin).c_str());
humanb(margin).c_str());
return false;
}
bool fits = true;
memory_cgroups([&](const std::string& dir, int64_t room) {
if (room - still - kLockMargin >= need) return true;
if (room - still - margin >= need) return true;
*why = fmt("the memory cgroup at %s leaves %s, %s more is still to be allocated, "
"and this wants %s with %s to spare", dir.c_str(),
"and this wants %s with %s to spare (--host-headroom-mib)", dir.c_str(),
humanb(room).c_str(), humanb(still).c_str(), humanb(need).c_str(),
humanb(kLockMargin).c_str());
humanb(margin).c_str());
fits = false;
return false;
});
return fits;
}
/* The most lockable_room(need, still, false) admits, and in `limit` the bound that decides it.
* INT64_MAX when no limit can be read. */
static int64_t lockable_bytes(int64_t still, std::string* limit) {
/* The most lockable_room(need, still, margin, false) admits, and in `limit` the bound that
* decides it. INT64_MAX when no limit can be read. */
static int64_t lockable_bytes(int64_t still, int64_t margin, std::string* limit) {
int64_t most = INT64_MAX;
*limit = "no readable memory limit";
if (const int64_t avail = meminfo_bytes("MemAvailable"); avail >= 0) {
most = avail - still - kLockMargin;
most = avail - still - margin;
*limit = fmt("%s of memory available, less %s still to be allocated and %s to spare",
humanb(avail).c_str(), humanb(still).c_str(), humanb(kLockMargin).c_str());
humanb(avail).c_str(), humanb(still).c_str(), humanb(margin).c_str());
}
memory_cgroups([&](const std::string& dir, int64_t room) {
if (room - still - kLockMargin < most) {
most = room - still - kLockMargin;
if (room - still - margin < most) {
most = room - still - margin;
*limit = fmt("the memory cgroup at %s, which leaves %s, less %s still to be allocated "
"and %s to spare", dir.c_str(), humanb(room).c_str(),
humanb(still).c_str(), humanb(kLockMargin).c_str());
humanb(still).c_str(), humanb(margin).c_str());
}
return true;
});
@@ -470,7 +473,9 @@ int Engine::resolve_table_placement(const std::vector<VramFacts>& facts,
const int64_t pools_still = (int64_t)cfg_.tp * cfg_.host_pool_mib * MiB +
cfg_.prefix_cache_mib * MiB + host_late;
std::string emb_room_why;
auto emb_room = [&]() { return lockable_room(emb_pin, pools_still, false, &emb_room_why); };
auto emb_room = [&]() {
return lockable_room(emb_pin, pools_still, host_headroom_bytes(), false, &emb_room_why);
};
if (emb_max == 0) {
if (emb == TablePlacement::Ram && emb_asked) {
RAD_ERR("--embedding-placement ram: this model has no token embedding table that only "
@@ -525,7 +530,7 @@ int Engine::resolve_table_placement(const std::vector<VramFacts>& facts,
std::string why;
const int64_t pin = (int64_t)cfg_.tp * align_up(tower_max, (int64_t)MiB);
if (!lockable_room(pin, pools_still + (int64_t)cfg_.tp * cfg_.embedding_host_mib * MiB,
false, &why)) {
host_headroom_bytes(), false, &why)) {
RAD_ERR("--vision-tower-placement ram: the encoder towers (%s a rank, pinned on every "
"rank) cannot be held in host memory: %s. Use vram.", humanb(tower_max).c_str(),
why.c_str());
@@ -609,7 +614,8 @@ int Engine::resolve_table_placement(const std::vector<VramFacts>& facts,
} else {
std::string card_why, ram_why;
const bool card = ng == TablePlacement::Auto && ngram_device && card_holds(&card_why);
const bool room = !card && lockable_room(need, still, !ngram_device, &ram_why);
const bool room =
!card && lockable_room(need, still, host_headroom_bytes(), !ngram_device, &ram_why);
if (ng == TablePlacement::Ram) {
if (!room) {
RAD_ERR("--ngram-placement ram: the n-gram table (%s) cannot be held in memory as "
@@ -2763,7 +2769,7 @@ int Engine::size_checkpoint_store(int64_t slot, const char* what) {
int64_t still = cfg_.prefix_cache_mib * ((int64_t)1 << 20);
for (const auto& r : ranks_) still += arena_host_bytes(r->program);
std::string limit;
int64_t room = lockable_bytes(still, &limit);
int64_t room = lockable_bytes(still, host_headroom_bytes(), &limit);
if (!device_is_host()) {
bool lifted = false;
if (const int64_t cap = amdgpu_pin_cap(&lifted); cap > 0) {
@@ -2906,7 +2912,8 @@ int Engine::configure_kv() {
};
if (!sized) return refuse("no host has");
std::string why;
if (!lockable_room(slot * n, 0, false, &why)) return refuse("does not fit: " + why);
if (!lockable_room(slot * n, 0, host_headroom_bytes(), false, &why))
return refuse("does not fit: " + why);
if (ck_store_.open(slot, slot * n) != RAD_OK)
return refuse(std::string("the host did not pin: ") + rad_dev_last_error());
if (!sized_auto)