mem: give the checkpoint store its floor before the host reserve

--host-headroom-mib is a courtesy to the system: the process's unpooled allocations, the
load's staging windows, the page cache. A linear-state snapshot store is not a courtesy.
A store too small for one snapshot does not serve a smaller store -- it serves none, and
every prefix hit re-prefills the whole conversation, while /metrics still counts the
attention blocks as a hit and the request log's "(cache 0)" is the only honest number.

--checkpoint-slots auto already promises a floor of kCheckpointSlotsFloor slots, but the
floor was only checked against what the host had left once the host pool, the streaming
mirror and the reserve had taken theirs. On a host where those fit exactly, the store
resolved to 0.

Charge the floor before the reserve: when the reserve is what stands between the host's
spare memory and the store's floor, it gives back exactly the shortfall -- whole MiB,
rounded up so the store is never a byte short, never below kHostReserveFloor (1 GiB) and
never above the margin the user asked for. A host that can pin the floor gets it; a host
that cannot still reports the shortfall, now naming --host-headroom-mib among the levers.
The boot guard a few lines below the sizing takes the same margin, or a count auto had
just fitted by giving the reserve back would be refused a moment later.

The rule is bytes over the host's own measurements: checkpoint_slots_want for the count,
MemAvailable, the memory cgroups and the driver's pin cap for the room. It needs no case
per model or per KV encoding -- a wider slot (more linear or windowed layers), a wider
streaming mirror (bf16 rather than fp8) and a larger --tp arrive as different numbers,
not as different rules.

Measured on 2x RX 9070 (16 GB), Qwen3.8-Flash-Next at --tp 2, bf16 KV, 2x262144,
--host-pool-mib 23652: 8 snapshots of 112.73 MiB, the reserve giving back 685 MiB and
holding 3.33 GiB (before: 0 snapshots, every turn re-prefilling 125-134k tokens at
ttft ~31 s with "(cache 0)"). The same 6398-token prompt twice: ttft 1.66 s -> 0.65 s,
cached 0 -> 5120, linear_checkpoints_written_total 0 -> 1.

Tests: the rule itself (fits -> untouched, short -> exactly the shortfall, beyond it ->
the 1 GiB floor, a smaller flag -> never exceeded) and checkpoint_slots_want against the
counts auto already reports.
This commit is contained in:
Boris Manojlovic authored and StillDeadcode committed 2026-10-29 10:36:10 +01:00
1 parent a64c75eb34
commit 481fcd5df0
6 files changed
+151 -28

No files matched your search

+19 -2
View File
@@ -122,8 +122,7 @@ CheckpointSlots checkpoint_slots_auto(const CheckpointPolicy& policy, int64_t ma
int64_t share_bytes, int64_t room_bytes) {
CheckpointSlots c;
c.per_seq = policy.held(ctx, interval) + 1;
const int64_t seqs = std::max<int64_t>(max_seqs, 1);
c.want = seqs > INT64_MAX / c.per_seq ? INT64_MAX : seqs * c.per_seq;
c.want = checkpoint_slots_want(policy, max_seqs, ctx, interval);
if (slot_bytes <= 0) return c;
const int64_t share = std::max<int64_t>(share_bytes, 0) / slot_bytes;
const int64_t room = std::max<int64_t>(room_bytes, 0) / slot_bytes;
@@ -136,6 +135,24 @@ CheckpointSlots checkpoint_slots_auto(const CheckpointPolicy& policy, int64_t ma
return c;
}
int64_t checkpoint_slots_want(const CheckpointPolicy& policy, int64_t max_seqs, int64_t ctx,
int64_t interval) {
const int64_t per = policy.held(ctx, interval) + 1;
const int64_t seqs = std::max<int64_t>(max_seqs, 1);
return seqs > INT64_MAX / per ? INT64_MAX : seqs * per;
}
int64_t host_reserve_for_floor(int64_t need_bytes, int64_t room_bytes, int64_t margin_bytes) {
if (need_bytes <= 0 || margin_bytes <= 0 || room_bytes >= need_bytes) return margin_bytes;
/* Whole MiB, the unit --host-headroom-mib takes, and the shortfall rounding up: the reserve
* gives back at least what the floor asked for, so the store is never a byte short of it. */
const int64_t mib = (int64_t)1 << 20;
const int64_t shortfall = (need_bytes - room_bytes + mib - 1) / mib;
const int64_t margin = margin_bytes / mib;
const int64_t keep = margin - shortfall;
return std::min(margin, std::max(kHostReserveFloor / mib, keep)) * mib;
}
/* ------------------------------------------------------------------ configure */
int PrefixCache::configure(const Config& cfg, KVManager* kv, int64_t checkpoint_interval) {