park pools: the segment pool is min(2 GiB, a quarter of the cache) (measured: 55K-token park 283 ms with 1 GiB, 117 ms with 2 GiB)

l620, 11-12, three runs per arm: with the arena at min(1 GiB, B / 8) the park of a
new 55K-token conversation beside three older parked ones stays at 283-291 ms
(K/V resize 205-213 ms: the arena is full of the older images, 47 misses of 372
allocations); with 2 GiB (a 16 GiB cache) it is 117-118 ms (K/V resize 41-44 ms).
Parks up to 20K are the same with either (35-59 ms).

Sizes now (park_pool_policy.hpp): segment pool min(2 GiB, B / 4) in 64 MiB steps,
none under 128 MiB; ready checkpoints as many as fit in B / 12, at most 4. At the
automatic cache sizes: ~8 GiB free RAM -> 2 GiB cache -> 512 MiB + 1 ready;
~12 GiB -> 3.75 GiB -> 960 MiB + 2; ~20 GiB -> 7 GiB -> 1792 MiB + 4; 40 GiB or
more -> 8 GiB -> 2048 MiB + 4. At most a third of the cache is held idle (30% at
8 GiB, 2.4 GiB); it is given back first (before any parked conversation is
evicted, before the RAM floor), and filled only while the cache budget and the RAM
floor plus 1 GiB leave room. Unit tests: the sizes at 8 to 200 GiB free and small
explicit caches.
This commit is contained in:
Niko1221 committed 2026-11-12 00:57:11 +01:00
1 parent 01e21ef20e
commit ef371eba84
4 files changed
+24 -23

No files matched your search

+1 -1
View File
@@ -24135,7 +24135,7 @@
"last_date": "",
"status": "default",
"owner": "core",
"note": "on by default on Linux (lane PARK-POOL): unset = an arena of min(1 GiB, an eighth of the conversation cache) in 64 MiB steps (none under 128 MiB), reserved once and made resident in the background; the K/V buffers of parked conversations (segments of 512 KiB and more) are taken from it, so a park no longer pays the first touch of fresh pages. Idle bytes count against --conversation-cache-mib and are given back before any parked conversation is evicted or the RAM floor is reached. NAME=0 turns it off (the diagnostic switch; no user setting), NAME=N sets N MiB (still at most an eighth of the cache). Windows and macOS: the OS layer is not written, the arena is never created there (inert).",
"note": "on by default on Linux (lane PARK-POOL): unset = an arena of min(2 GiB, a quarter of the conversation cache) in 64 MiB steps (none under 128 MiB), reserved once and made resident in the background; the K/V buffers of parked conversations (segments of 512 KiB and more) are taken from it, so a park no longer pays the first touch of fresh pages. Idle bytes count against --conversation-cache-mib and are given back before any parked conversation is evicted or the RAM floor is reached. NAME=0 turns it off (the diagnostic switch; no user setting), NAME=N sets N MiB (still at most a quarter of the cache). Windows and macOS: the OS layer is not written, the arena is never created there (inert).",
"retire_after": "",
"retired_in": ""
},
+4 -4
View File
@@ -15,19 +15,19 @@ struct ParkPoolPlan {
size_t ckpt_ready = 0; ///< checkpoints kept ready for the next save (0: none ahead of time)
};
constexpr size_t kSegpoolMaxMib = 1024; ///< the arena never exceeds 1 GiB
constexpr size_t kSegpoolMaxMib = 2048; ///< the arena never exceeds 2 GiB (l620, 11-12: a 55K-token park 283 ms with 1 GiB, 117 ms with 2 GiB)
constexpr size_t kSegpoolMinMib = 128; ///< below this an arena does not hold even an 8K image: none
constexpr size_t kCkptReadyMax = 4; ///< a park needs the live state plus one copy per checkpoint (up to 3 seen at 55K): 4
/// `cache_mib`: the conversation cache's budget (--conversation-cache-mib, or the automatic size);
/// `ckpt_bytes`: one ready checkpoint (the running state of the model: ~112 MB on Qwen3.8-Flash-Next).
/// segpool = min(1 GiB, an eighth of the cache) in whole 64 MiB steps, none under 128 MiB;
/// segpool = min(2 GiB, a quarter of the cache) in whole 64 MiB steps, none under 128 MiB;
/// ckpt_ready = as many checkpoints as fit in a twelfth of the cache, at most 4.
/// Together they hold at most 21% of the cache idle (18-19% at the usual 7-8 GiB caches).
/// Together they hold at most a third of the cache idle (30% at the usual 7-8 GiB caches); idle memory is given back first.
inline ParkPoolPlan park_pool_plan(uint64_t cache_mib, uint64_t ckpt_bytes) {
ParkPoolPlan p;
if (cache_mib == 0) return p;
const size_t seg = std::min<uint64_t>(kSegpoolMaxMib, cache_mib / 8) / 64 * 64;
const size_t seg = std::min<uint64_t>(kSegpoolMaxMib, cache_mib / 4) / 64 * 64;
p.segpool_mib = seg >= kSegpoolMinMib ? seg : 0;
if (ckpt_bytes > 0) p.ckpt_ready = (size_t) std::min<uint64_t>(kCkptReadyMax, (cache_mib << 20) / 12 / ckpt_bytes);
return p;
+15 -14
View File
@@ -24,33 +24,34 @@ int main() {
struct Row { uint64_t free_gib; int64_t cache; size_t seg, ready; };
const Row rows[] = {
{4, 0, 0, 0}, // under ~5 GiB free: the cache is off, so no pool
{8, 2048, 256, 1}, // a 16 GB machine with ~8 GiB free
{12, 3840, 448, 2},
{20, 7168, 896, 4}, // a 32 GB machine with ~20 GiB free
{40, 8192, 1024, 4}, // a 64 GB machine with 40 GiB or more free
{200, 8192, 1024, 4}, // a big server: the cache cap is 8 GiB, so the pools stay at 1 GiB and 4
{8, 2048, 512, 1}, // a 16 GB machine with ~8 GiB free
{12, 3840, 960, 2},
{20, 7168, 1792, 4}, // a 32 GB machine with ~20 GiB free
{40, 8192, 2048, 4}, // a 64 GB machine with 40 GiB or more free
{200, 8192, 2048, 4}, // a big server: the cache cap is 8 GiB, so the pools stay at 2 GiB and 4
};
for (const Row& r : rows) {
const int64_t cache = auto_conversation_cache_mib(r.free_gib * GiB, 2560);
check(cache == r.cache, "the automatic cache size for this free RAM");
const ParkPoolPlan p = park_pool_plan((uint64_t) cache, kCkpt);
check(p.segpool_mib == r.seg && p.ckpt_ready == r.ready, "the pools for this cache");
// what is held idle at most: the arena plus the ready checkpoints, never more than 21% of the cache
check(cache == 0 || (p.segpool_mib * MiB + p.ckpt_ready * kCkpt) * 100 <= (uint64_t) cache * MiB * 21, "held idle: at most 21% of the cache");
// what is held idle at most: the arena plus the ready checkpoints, never more than a third of the cache (34% with rounding)
check(cache == 0 || (p.segpool_mib * MiB + p.ckpt_ready * kCkpt) * 100 <= (uint64_t) cache * MiB * 34, "held idle: at most 34% of the cache");
}
// small explicit budgets
check(park_pool_plan(512, kCkpt).segpool_mib == 0 && park_pool_plan(512, kCkpt).ckpt_ready == 0, "a 512 MiB cache: no arena (64 MiB is under the minimum), no ready checkpoint (a twelfth is 43 MiB)");
check(park_pool_plan(1024, kCkpt).segpool_mib == 128 && park_pool_plan(1024, kCkpt).ckpt_ready == 0, "a 1 GiB cache: a 128 MiB arena, no ready checkpoint");
check(park_pool_plan(1536, kCkpt).ckpt_ready == 1 && park_pool_plan(1536, kCkpt).segpool_mib == 192, "1.5 GiB: one ready checkpoint, a 192 MiB arena");
check(park_pool_plan(4096, kCkpt).ckpt_ready == 3 && park_pool_plan(4096, kCkpt).segpool_mib == 512, "4 GiB: 3 ready checkpoints, a 512 MiB arena");
check(park_pool_plan(1 << 20, kCkpt).segpool_mib == 1024 && park_pool_plan(1 << 20, kCkpt).ckpt_ready == 4, "a huge cache: the caps, 1 GiB and 4");
check(park_pool_plan(8192, 0).ckpt_ready == 0 && park_pool_plan(8192, 0).segpool_mib == 1024, "an unknown checkpoint size: none ready, the arena as usual");
check(park_pool_plan(512, kCkpt).segpool_mib == 128 && park_pool_plan(512, kCkpt).ckpt_ready == 0, "a 512 MiB cache: a 128 MiB arena (a quarter), no ready checkpoint (a twelfth is 43 MiB)");
check(park_pool_plan(448, kCkpt).segpool_mib == 0, "a 448 MiB cache: a quarter is 112 MiB, under the minimum: no arena");
check(park_pool_plan(1024, kCkpt).segpool_mib == 256 && park_pool_plan(1024, kCkpt).ckpt_ready == 0, "a 1 GiB cache: a 256 MiB arena, no ready checkpoint");
check(park_pool_plan(1536, kCkpt).ckpt_ready == 1 && park_pool_plan(1536, kCkpt).segpool_mib == 384, "1.5 GiB: one ready checkpoint, a 384 MiB arena");
check(park_pool_plan(4096, kCkpt).ckpt_ready == 3 && park_pool_plan(4096, kCkpt).segpool_mib == 1024, "4 GiB: 3 ready checkpoints, a 1 GiB arena");
check(park_pool_plan(1 << 20, kCkpt).segpool_mib == 2048 && park_pool_plan(1 << 20, kCkpt).ckpt_ready == 4, "a huge cache: the caps, 2 GiB and 4");
check(park_pool_plan(8192, 0).ckpt_ready == 0 && park_pool_plan(8192, 0).segpool_mib == 2048, "an unknown checkpoint size: none ready, the arena as usual");
// a bigger checkpoint (another model) gets fewer ready ones, never more than 4
check(park_pool_plan(8192, 400 * MiB).ckpt_ready == 1 && park_pool_plan(8192, 30 * MiB).ckpt_ready == 4, "ready checkpoints scale with their size");
// the segment pool is a multiple of 64 MiB (granules and 2 MiB alignment hold)
for (uint64_t c = 1; c < 20000; c += 37) {
const size_t s = park_pool_plan(c, kCkpt).segpool_mib;
check(s == 0 || (s % 64 == 0 && s >= kSegpoolMinMib && s <= kSegpoolMaxMib && s <= c / 8), "an arena is whole 64 MiB steps, between 128 MiB and 1 GiB, within an eighth of the cache");
check(s == 0 || (s % 64 == 0 && s >= kSegpoolMinMib && s <= kSegpoolMaxMib && s <= c / 4), "an arena is whole 64 MiB steps, between 128 MiB and 2 GiB, within a quarter of the cache");
}
std::printf("park_pool_policy_test: %d checks passed\n", checks);
return 0;
+4 -4
View File
@@ -401,9 +401,9 @@ size_t ckpt_pool_ready_env() {
// how many dropped checkpoints the pool keeps (a batch admission drops and takes about two): 2, or more when STRATA_CKPT_POOL_READY asks
size_t ckpt_pool_cap_env() { return std::max<size_t>(2, ckpt_pool_ready_env()); }
// STRATA_PARK_SEGPOOL_MIB=N (read once; Linux only, inert where the OS layer is not written - Windows, macOS): the host buffers of parked
// conversations (K/V segments of 512 KiB and more) come from one arena of N MiB (at most an eighth of the conversation cache) that a
// conversations (K/V segments of 512 KiB and more) come from one arena of N MiB (at most a quarter of the conversation cache) that a
// background thread makes resident, so a park's first touch of fresh pages is paid once at start instead of at every park (include/
// strata/core/conversation_segment_pool.hpp). Unset: the engine's default, min(1 GiB, an eighth of the cache) in 64 MiB steps
// strata/core/conversation_segment_pool.hpp). Unset: the engine's default, min(2 GiB, a quarter of the cache) in 64 MiB steps
// (park_pool_policy.hpp); 0: off (a diagnostic switch). Its idle bytes count against --conversation-cache-mib and are given back
// before any parked conversation is evicted and before the RAM floor is reached. Every byte a park writes is the same; only where
// the buffers live differs. -1: unset.
@@ -12129,7 +12129,7 @@ int main(int argc, char** argv) {
(size_t) o.conversation_cache_slots, o.conversation_cache_min_tokens);
// The park pools (park_pool_policy.hpp): memory held idle between parks so a park does not pay the first touch of fresh pages.
// * the segment pool: one arena for the K/V buffers of parked conversations (STRATA_PARK_SEGPOOL_MIB=N sets its size, 0 turns it
// off; unset: min(1 GiB, an eighth of the cache); Linux only, the OS layer of Windows and macOS is not written yet: inert there);
// off; unset: min(2 GiB, a quarter of the cache); Linux only, the OS layer of Windows and macOS is not written yet: inert there);
// * the checkpoint pool with ready checkpoints (STRATA_CKPT_POOL=0 turns it off; STRATA_CKPT_POOL_READY=N sets the count; unset:
// as many as fit in a twelfth of the cache, at most 4; a single-stage serve only, not in the Metal build).
// Both count against the cache's budget (ConversationCache::set_pool) and are given back before any parked conversation is evicted
@@ -12171,7 +12171,7 @@ int main(int argc, char** argv) {
const size_t want_mib = env_mib >= 0 ? (size_t) env_mib : park_plan.segpool_mib;
if (want_mib > 0 && conversations.enabled()) {
const size_t budget = (size_t) o.conversation_cache_mib * 1024 * 1024;
const size_t bytes = std::min(want_mib * 1024 * 1024, budget / 8); // never more than an eighth of the cache
const size_t bytes = std::min(want_mib * 1024 * 1024, budget / 4); // never more than a quarter of the cache
segpool = new strata::core::SegmentPool(strata::core::segment_pool_os(), bytes);
if (segpool->active() && strata::core::segment_pool_install(segpool)) {
pools_budget = ParkPoolsBudget(segpool, &ckpt_pool);