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

+33
View File
@@ -3416,6 +3416,39 @@ TEST(a_sessions_standing_share_is_what_retention_keeps) {
CHECK_EQ((int64_t)most, 9ll);
}
/* The reserve --host-headroom-mib charges a store's floor: it gives back exactly what the floor is
* short, never below a gigabyte, never above what was asked for, and stays put when the floor
* already fits. A byte rule, so a wider slot (any hybrid model's linear state) or a wider mirror
* (any KV encoding) needs no second case. */
TEST(the_host_reserve_gives_way_to_a_stores_floor_and_no_further) {
const int64_t gib = (int64_t)1 << 30, mib = (int64_t)1 << 20;
const int64_t slot = 113 * mib, floor8 = 8 * slot, reserve = 4 * gib;
/* The floor fits: the reserve never moves, so a host with room to spare sees nothing. */
CHECK_EQ(host_reserve_for_floor(floor8, 20 * floor8, reserve), reserve);
CHECK_EQ(host_reserve_for_floor(floor8, floor8, reserve), reserve);
/* Short: the shortfall comes back in whole MiB, rounded up so the store is never a byte
* short of its floor, and the rest of the reserve stays. */
CHECK_EQ(host_reserve_for_floor(floor8, slot / 2, reserve), reserve - 848 * mib);
CHECK_EQ(host_reserve_for_floor(floor8, floor8 - 1, reserve), reserve - 1 * mib);
CHECK_EQ(host_reserve_for_floor(floor8, floor8 - mib, reserve), reserve - 1 * mib);
CHECK_EQ(host_reserve_for_floor(floor8, floor8 - mib - 1, reserve), reserve - 2 * mib);
/* A floor beyond both the spare memory and the reserve: down to the floor, never below. */
CHECK_EQ(host_reserve_for_floor(64 * gib, 0, reserve), gib);
/* Never above the margin asked for, so a small --host-headroom-mib is still a real bound. */
CHECK_EQ(host_reserve_for_floor(floor8, 0, 512 * mib), 512 * mib);
/* Nothing to protect. */
CHECK_EQ(host_reserve_for_floor(0, 0, reserve), reserve);
CHECK_EQ(host_reserve_for_floor(floor8, 0, 0), 0);
/* The count a floor is taken from: the policy's hold plus a running turn, per sequence. */
const GeometricBackoff g(2048);
CHECK_EQ(checkpoint_slots_want(g, 8, 200000, 2048), 80ll);
CHECK_EQ(checkpoint_slots_want(g, 1, 200000, 2048), 10ll);
CHECK_EQ(checkpoint_slots_want(g, 0, 200000, 2048), 10ll);
CHECK_EQ(checkpoint_slots_want(KeepTipOnly(), 1, 200000, 2048), 2ll);
}
/* --checkpoint-slots auto: the policy's share plus one for each sequence, held to the share of
* memory but not below the floor, and to the room the host has. */
TEST(the_auto_store_gives_every_sequence_its_sessions_share) {