okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-pool-idle 379e966a5 into f-p4-int (the park pools are refilled only while the engine is idle)271eb25a6

main
4.2 KiB61 linesraw
1// The park pools' sizes from the conversation cache's budget (park_pool_policy.hpp), with the cache sizes small machines get.
2#include "strata/core/conversation_memory.hpp"
3#include "strata/core/park_pool_policy.hpp"
4
5#include <cstdio>
6#include <cstdlib>
7
8using namespace strata::core;
9
10namespace {
11int checks = 0;
12void check(bool value, const char* description) {
13    ++checks;
14    if (!value) { std::fprintf(stderr, "FAIL: %s\n", description); std::exit(1); }
15}
16constexpr uint64_t MiB = uint64_t(1) << 20, GiB = uint64_t(1) << 30;
17constexpr uint64_t kCkpt = 112 * MiB;   // one ready checkpoint on Qwen3.8-Flash-Next (the running state, ~118 MB)
18}
19
20int main() {
21    // no cache, no pool
22    check(park_pool_plan(0, kCkpt).segpool_mib == 0 && park_pool_plan(0, kCkpt).ckpt_ready == 0, "a cache that is off gets no pool");
23    // the sizes at the cache sizes the automatic rule gives (free RAM after the model is loaded, floor 2560 MiB)
24    struct Row { uint64_t free_gib; int64_t cache; size_t seg, ready; };
25    const Row rows[] = {
26        {4, 0, 0, 0},         // under ~5 GiB free: the cache is off, so no pool
27        {8, 2048, 512, 1},    // a 16 GB machine with ~8 GiB free
28        {12, 3840, 960, 2},
29        {20, 7168, 1792, 4},  // a 32 GB machine with ~20 GiB free
30        {40, 8192, 2048, 4},  // a 64 GB machine with 40 GiB or more free
31        {200, 8192, 2048, 4}, // a big server: the cache cap is 8 GiB, so the pools stay at 2 GiB and 4
32    };
33    for (const Row& r : rows) {
34        const int64_t cache = auto_conversation_cache_mib(r.free_gib * GiB, 2560);
35        check(cache == r.cache, "the automatic cache size for this free RAM");
36        const ParkPoolPlan p = park_pool_plan((uint64_t) cache, kCkpt);
37        check(p.segpool_mib == r.seg && p.ckpt_ready == r.ready, "the pools for this cache");
38        // what is held idle at most: the arena plus the ready checkpoints, never more than a third of the cache (34% with rounding)
39        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");
40    }
41    // small explicit budgets
42    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)");
43    check(park_pool_plan(448, kCkpt).segpool_mib == 0, "a 448 MiB cache: a quarter is 112 MiB, under the minimum: no arena");
44    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");
45    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");
46    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");
47    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");
48    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");
49    // a bigger checkpoint (another model) gets fewer ready ones, never more than 4
50    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");
51    // the segment pool is a multiple of 64 MiB (granules and 2 MiB alignment hold)
52    for (uint64_t c = 1; c < 20000; c += 37) {
53        const size_t s = park_pool_plan(c, kCkpt).segpool_mib;
54        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");
55    }
56    // upkeep only while the engine is idle
57    check(park_pool_permit(true, 64 * MiB) == 0 && park_pool_permit(true, 1) == 0, "a request in flight: the upkeep may make nothing");
58    check(park_pool_permit(false, 64 * MiB) == 64 * MiB && park_pool_permit(false, 0) == 0, "idle: the upkeep may make what it wants");
59    std::printf("park_pool_policy_test: %d checks passed\n", checks);
60    return 0;
61}