okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
3289d8280
main
1#include "strata/core/ple_residency.hpp" 2 3#include <cstdio> 4#include <cstdlib> 5#include <limits> 6 7using namespace strata::core ; 8namespace { 9int checks = 0 ; 10void check (bool ok ,const char * label ) { 11++ checks ; 12if (!ok ) { std::fprintf (stderr ,"FAIL: %s\n" ,label ); std::exit (1 ); } 13} 14constexpr uint64_t GiB = 1ull <<30 ; 15} 16 17int main () { 18const uint64_t table = 26ull * GiB + 870ull * (1ull <<20 );// IQ3_S's table, 26.8 GiB 19const uint64_t arena = 51ull * GiB + 100ull * (1ull <<20 );// IQ3_S's 51.1 GiB of experts 20// the owner PC, 2026-10-26: 45.2 GiB available when the table is opened, 63 GiB total. The 0.2.7 first policy 21// (table + 8 GiB) admitted it and the expert load then failed with error 1453. Not admitted now. 22check (!ple_resident_admit (45ull * GiB + (1ull <<28 ),table ,arena ,8 * GiB ),"64 GB PC with the IQ3_S arena: not admitted" ); 23check (!ple_resident_admit (40ull * GiB ,table ,arena ,8 * GiB ),"40 GiB: not admitted" ); 24// STRATA_DEFAULTS=026: the policy does not run (v0.2.6 had none); an explicit STRATA_PLE_AUTO_RAM wins either way 25check (ple_auto_ram_allowed (nullptr , false),"policy on by default" ); 26check (!ple_auto_ram_allowed (nullptr , true),"policy off under STRATA_DEFAULTS=026" ); 27check (ple_auto_ram_allowed ("1" , true),"STRATA_PLE_AUTO_RAM=1 wins over 026" ); 28check (!ple_auto_ram_allowed ("0" , false),"STRATA_PLE_AUTO_RAM=0 is the old off switch" ); 29check (!ple_auto_ram_allowed ("0" , true),"STRATA_PLE_AUTO_RAM=0 under 026" ); 30// a PC that holds the table, the arena and 8 GiB 31check (ple_resident_admit (table + arena + 8 * GiB ,table ,arena ,8 * GiB ),"exactly table + arena + headroom is admitted" ); 32check (!ple_resident_admit (table + arena + 8 * GiB - 1 ,table ,arena ,8 * GiB ),"one byte below is not" ); 33check (ple_resident_admit (240ull * GiB ,table ,arena ,8 * GiB ),"a 256 GB box: admitted" ); 34// a smaller model's arena on a mid PC (Q2_0: 34 GiB arena): 26.8 + 34 + 8 = 68.8 35check (ple_resident_admit (80ull * GiB ,table ,34ull * GiB ,8 * GiB ),"Q2_0 on 96 GB: admitted" ); 36check (!ple_resident_admit (60ull * GiB ,table ,34ull * GiB ,8 * GiB ),"Q2_0 with 60 GiB available: not admitted" ); 37// unknowns fail closed 38check (!ple_resident_admit ({},table ,arena ,8 * GiB ),"unknown telemetry" ); 39check (!ple_resident_admit (240ull * GiB ,0 ,arena ,8 * GiB ),"unknown table size" ); 40check (!ple_resident_admit (240ull * GiB ,table ,0 ,8 * GiB ),"unknown arena size" ); 41check (!ple_resident_admit (std::numeric_limits < uint64_t > ::max (),table , std::numeric_limits < uint64_t > ::max (),8 * GiB ), 42"sizes cannot overflow" ); 43check (!ple_resident_admit (0 ,table ,arena ,0 ),"zero available" ); 44// #4518: after the load, does the table's lock leave the automatic conversation cache no room? (min free 2560 MiB, cache 40% of the 45// rest, 8192 MiB at most, 1024 MiB at least) 46 { 47const int64_t floor_mib = 2560 ; 48check (!ple_lock_blocks_cache (std::nullopt ,table ,floor_mib ),"unknown telemetry: leave the lock" ); 49check (!ple_lock_blocks_cache (40ull * GiB ,0 ,floor_mib ),"unknown table size: leave the lock" ); 50check (ple_lock_blocks_cache (4ull * GiB ,table ,floor_mib ),"the reporter: 4 GiB left with the table locked (cache off), 30 GiB without" ); 51check (ple_lock_blocks_cache (10ull * GiB ,table ,floor_mib ),"10 GiB left: cache 1.9 GiB with the lock, 8 GiB without" ); 52check (!ple_lock_blocks_cache (40ull * GiB ,table ,floor_mib ),"40 GiB left: the cache is already at its 8 GiB cap" ); 53check (!ple_lock_blocks_cache (23ull * GiB ,table ,floor_mib ),"23 GiB: the cap is just reached with the lock" ); 54check (ple_lock_blocks_cache (22ull * GiB ,table ,floor_mib ),"22 GiB: the lock costs the cache some of its 8 GiB" ); 55check (!ple_lock_blocks_cache (1ull * GiB ,1ull <<20 ,floor_mib ),"a tiny table cannot make a cache" ); 56check (!ple_lock_blocks_cache (std::numeric_limits < uint64_t > ::max (),table ,floor_mib ),"no overflow" ); 57 } 58// #4846 (owner 11-01): RAM still short at the arena after the wait and the give-back: the --mmap-experts source, never a refusal, 59// never registering anyway. A hand-set --ple-io ram or a shared arena file is respected (warn only). 60 { 61const uint64_t need = arena + (1ull <<20 );// the arena and one blob 62const uint64_t none = std::numeric_limits < uint64_t > ::max ();// no cgroup limit 63check (arena_ram_verdict (need ,need ,none , true)== ArenaRamVerdict ::Load ,"RAM exactly there: load" ); 64check (arena_ram_verdict (need ,240ull * GiB ,none , true)== ArenaRamVerdict ::Load ,"plenty: load" ); 65check (arena_ram_verdict (need ,need - 1 ,none , true)== ArenaRamVerdict ::FallBack ,"one byte short: file source" ); 66check (arena_ram_verdict (need ,20ull * GiB ,none , true)== ArenaRamVerdict ::FallBack ,"20 GiB of 51: file source" ); 67check (arena_ram_verdict (need ,20ull * GiB ,none , false)== ArenaRamVerdict ::Warn ,"20 GiB of 51, fixed by hand: warn only" ); 68check (arena_ram_verdict (need ,240ull * GiB ,30ull * GiB , true)== ArenaRamVerdict ::FallBack ,"a 30 GiB container limit: file source, not a refusal" ); 69check (arena_ram_verdict (need ,240ull * GiB ,30ull * GiB , false)== ArenaRamVerdict ::Refuse ,"a 30 GiB container limit, fixed by hand: as before" ); 70check (arena_ram_verdict (need ,240ull * GiB ,need , true)== ArenaRamVerdict ::Load ,"a limit of exactly the arena: load" ); 71check (arena_ram_verdict (0 ,0 ,0 , true)== ArenaRamVerdict ::Load ,"an unpriced arena loads" ); 72check (arena_ram_fallback_allowed (nullptr , false, false),"fallback on by default" ); 73check (!arena_ram_fallback_allowed (nullptr , true, false),"a hand-set --ple-io ram is kept" ); 74check (!arena_ram_fallback_allowed (nullptr , false, true),"a shared arena file is kept" ); 75check (!arena_ram_fallback_allowed ("0" , false, false),"STRATA_ARENA_RAM_FALLBACK=0 is the old behaviour" ); 76check (arena_ram_fallback_allowed ("1" , false, false),"STRATA_ARENA_RAM_FALLBACK=1" ); 77check (arena_ram_fallback_allowed ("" , false, false),"an empty value is unset" ); 78check (!arena_ram_fallback_allowed ("1" , true, false),"the user's --ple-io ram wins over the env value" ); 79 } 80// #5327: a shortfall the kernel can serve by dropping the box's own file cache loads through it (0.1.55's 81// warn-and-proceed), one it cannot (anonymous RAM: #4051's host) keeps the #4846 fallback 82 { 83const uint64_t need = 31ull * GiB + 687000000ull ;// the reporter's Q2_0 arena: 31.64 GiB 84check (arena_shortfall_served_by_file_cache (need ,1ull * GiB ,38ull * GiB ), 85"12.9 GiB available, the model's files hot in the cache: load through the file cache" ); 86check (!arena_shortfall_served_by_file_cache (need ,300ull <<20 ,6ull * GiB ), 87"#4051's host: an exiting engine's anonymous RAM is not the cache: keep the fallback" ); 88check (arena_shortfall_served_by_file_cache (need ,need ,0 ),"free pages alone cover it" ); 89check (!arena_shortfall_served_by_file_cache (need ,0 ,need - 1 ),"one byte short even with the cache" ); 90check (!arena_shortfall_served_by_file_cache (0 ,0 ,0 ),"an unpriced arena is not this decision" ); 91check (arena_shortfall_served_by_file_cache (std::numeric_limits < uint64_t > ::max (),5 , std::numeric_limits < uint64_t > ::max ()), 92"no overflow" ); 93check (arena_shortfall_served_by_file_cache (need ,1ull * GiB ,6ull * GiB ,32ull * GiB ), 94"a reserved hugetlb pool that holds the whole arena: load" ); 95check (!arena_shortfall_served_by_file_cache (need ,1ull * GiB ,6ull * GiB ,need - 1 ), 96"a pool one page short serves none of it (MAP_HUGETLB is all-or-nothing): keep the fallback" ); 97uint64_t free_b = 1 ,drop_b = 1 ,huge_b = 1 ; 98check (meminfo_free_and_droppable ( 99"MemTotal: 1000 kB\nMemFree: 10 kB\nMemAvailable: 100 kB\nBuffers: 5 kB\n" 100"Cached: 400 kB\nSwapCached: 0 kB\nActive(file): 200 kB\nDirty: 2 kB\n" 101"Shmem: 30 kB\nSReclaimable: 15 kB\nHugePages_Total: 16384\nHugePages_Free: 16384\n" 102"Hugepagesize: 2048 kB\n" , 103free_b ,drop_b ,& huge_b )&& free_b == 10 * 1024 && drop_b == (400 + 5 + 15 - 30 - 2 )* 1024 && 104huge_b == 16384ull * 2048 * 1024 , 105"free, droppable and huge pages parsed (Shmem and Dirty are not droppable)" ); 106check (meminfo_free_and_droppable ("MemFree: 1 kB\nCached: 2 kB\n" ,free_b ,drop_b )&& free_b == 1024 && drop_b == 2 * 1024 , 107"Buffers/SReclaimable/Shmem/Dirty are optional" ); 108check (!meminfo_free_and_droppable ("MemFree: 1 kB\n" ,free_b ,drop_b ),"no Cached line: unknown, no credit" ); 109check (!meminfo_free_and_droppable ("Cached: 1 kB\n" ,free_b ,drop_b ),"no MemFree line: unknown, no credit" ); 110// the loopholes a review of #5531 found: locked file pages (the PLE table's mlock) sit in Cached but the kernel cannot 111// drop them (MemAvailable leaves them out); reserved huge pages are counted free but spoken for; only a 2 MiB pool 112// is the arena's pool 113check (meminfo_free_and_droppable ("MemFree: 10 kB\nCached: 4000 kB\nBuffers: 5 kB\nMlocked: 3000 kB\nDirty: 2 kB\nShmem: 30 kB\n" , 114free_b ,drop_b )&& 115drop_b == (4000 + 5 - 30 - 2 - 3000 )* 1024 , 116"Mlocked pages are not droppable" ); 117check (meminfo_free_and_droppable ("MemFree: 1 kB\nCached: 100 kB\nMlocked: 5000 kB\n" ,free_b ,drop_b )&& drop_b == 0 , 118"Mlocked above the cache floors at 0" ); 119check (meminfo_free_and_droppable ("MemFree: 1 kB\nCached: 1 kB\nHugePages_Free: 16384\nHugePages_Rsvd: 16000\nHugepagesize: 2048 kB\n" , 120free_b ,drop_b ,& huge_b )&& 121huge_b == 384ull * 2048 * 1024 , 122"reserved huge pages are not free for the arena" ); 123check (meminfo_free_and_droppable ("MemFree: 1 kB\nCached: 1 kB\nHugePages_Free: 16\nHugePages_Rsvd: 20\nHugepagesize: 2048 kB\n" , 124free_b ,drop_b ,& huge_b )&& 125huge_b == 0 , 126"more reserved than free: none" ); 127check (meminfo_free_and_droppable ("MemFree: 1 kB\nCached: 1 kB\nHugePages_Free: 64\nHugepagesize: 1048576 kB\n" , 128free_b ,drop_b ,& huge_b )&& 129huge_b == 0 , 130"a 1 GiB default pool is not the 2 MiB pool the arena maps" ); 131// #4051's other shape: an engine still exiting holds the PLE table mlocked. Cached has it, MemAvailable does not. 132 { 133const uint64_t need_x = 47ull * GiB ; 134check (meminfo_free_and_droppable ("MemFree: " + std::to_string (2 * GiB /1024 )+ " kB\nCached: " + std::to_string (55 * GiB /1024 )+ 135" kB\nMlocked: " + std::to_string (27 * GiB /1024 )+ " kB\n" , 136free_b ,drop_b )&& 137 !arena_shortfall_served_by_file_cache (need_x ,free_b ,drop_b ), 138"a locked PLE table in the cache does not serve the arena: keep the fallback" ); 139 } 140 } 141// --ram-keep-gib W (R5070): ONE setting for both RAM decisions at the start. The arena is used only if W GiB stay available after it; 142// otherwise the RAM copy leaves max(W, the safety headroom: 8 GiB Windows, 4 GiB elsewhere), unless STRATA_RESIDENT_HEADROOM_GIB is set. 143 { 144const uint64_t need = 46ull * GiB + 860ull * (1ull <<20 )+ 2660000ull ;// IQ3_S: 46.84 GiB of experts and the spare blob 145const uint64_t none = std::numeric_limits < uint64_t > ::max (); 146const uint64_t win = fallback_safety_headroom (true),lin = fallback_safety_headroom (false); 147check (win == 8 * GiB && lin == 4 * GiB ,"safety headroom: 8 GiB Windows, 4 GiB elsewhere" ); 148check (ram_keep_from_gib (0.0 )== 0 && ram_keep_from_gib (-3.0 )== 0 && ram_keep_from_gib (8.0 )== 8 * GiB ,"W in GiB" ); 149check (ram_keep_from_gib (1e9 )== kRamKeepMax ,"a typo cannot ask for more than 1 TiB" ); 150check (arena_need_with_keep (need ,0 )== need && arena_need_with_keep (need ,8 * GiB )== need + 8 * GiB ,"need plus W" ); 151check (arena_need_with_keep (none - 5 ,100 )== none ,"need plus W cannot overflow" ); 152// W = 0 is the behaviour before the setting: the arena exactly when arena_ram_verdict says Load, the copy leaves the safety headroom 153const double frees []= {10.0 ,24.0 ,30.0 ,36.0 ,40.0 ,44.6 ,46.5 ,48.0 ,48.9 ,49.1 ,50.0 ,54.9 ,60.0 ,100.0 }; 154for (const double f :frees ) { 155const uint64_t avail = (uint64_t ) (f * (double )GiB ); 156for (const uint64_t safety : {win ,lin }) { 157const RamKeepPlan p0 = ram_keep_plan (need ,avail ,0 , std::nullopt ,safety ); 158check (p0 .arena == (arena_ram_verdict (need ,avail ,none , true)== ArenaRamVerdict ::Load ),"W=0: the arena whenever it fits, as before" ); 159check (p0 .headroom == safety ,"W=0: the safety headroom" ); 160check (p0 .budget == (p0 .arena ?0 : (avail > safety + GiB ?avail - safety :0 )),"W=0: the budget is what is available less the safety headroom" ); 161uint64_t prev_budget = none ; 162bool prev_arena = true; 163for (const uint64_t w : {0ull ,5ull * GiB ,7ull * GiB ,8ull * GiB ,16ull * GiB }) { 164const RamKeepPlan p = ram_keep_plan (need ,avail ,w , std::nullopt ,safety ); 165check (p .arena == (avail >=need + w ),"the arena only if W GiB stay available after it" ); 166if (p .arena )check (avail - need >=w ,"an arena start leaves at least W free" ); 167check (p .headroom == (w > safety ?w :safety ),"the headroom is max(W, safety)" ); 168if (!p .arena && p .budget > 0 )check (avail - p .budget >=p .headroom ,"a RAM copy leaves at least the headroom free" ); 169check (!p .arena || prev_arena ,"a larger W never turns the fallback into the arena" ); 170check (p .budget <=prev_budget || prev_arena ,"a larger W never grows the copy" ); 171prev_arena = p .arena ; 172prev_budget = p .arena ?none :p .budget ; 173 } 174// the environment variable wins over both W and the safety headroom, even when smaller 175const RamKeepPlan e = ram_keep_plan (need ,avail ,8 * GiB , std::optional < uint64_t > (2 * GiB ),safety ); 176check (e .headroom == 2 * GiB ,"STRATA_RESIDENT_HEADROOM_GIB still wins" ); 177check (e .arena == (avail >=need + 8 * GiB ),"the environment does not move the arena decision" ); 178 } 179 } 180// the owner's PC (63.1 GB, 12 GB card, IQ3_S): 48.9 GiB free at the start 181const uint64_t f489 = (uint64_t ) (48.9 * (double )GiB ),f446 = (uint64_t ) (44.6 * (double )GiB ); 182check (ram_keep_plan (need ,f489 ,0 , std::nullopt ,win ).arena ,"48.9 GiB free, W=0: the arena" ); 183check (!ram_keep_plan (need ,f489 ,8 * GiB , std::nullopt ,win ).arena ,"48.9 GiB free, W=8: not the arena (2 GiB left)" ); 184const RamKeepPlan w8 = ram_keep_plan (need ,f489 ,8 * GiB , std::nullopt ,win ); 185check (w8 .headroom == 8 * GiB && w8 .budget > 39 * GiB && w8 .budget < 41 * GiB ,"48.9 GiB free, W=8: a 40.9 GiB RAM copy, 8 GiB left" ); 186const RamKeepPlan w5 = ram_keep_plan (need ,f489 ,5 * GiB , std::nullopt ,win ); 187check (!w5 .arena && w5 .headroom == 8 * GiB ,"48.9 GiB free, W=5 on Windows: 48.9 < 46.8 + 5, so the copy, and the safety headroom of 8 GiB wins over 5" ); 188check (ram_keep_plan (need ,f489 ,5 * GiB , std::nullopt ,lin ).headroom == 5 * GiB ,"W=5 on Linux: the copy leaves 5 GiB" ); 189check (!ram_keep_plan (need ,f446 ,0 , std::nullopt ,win ).arena && ram_keep_plan (need ,f446 ,0 , std::nullopt ,win ).budget > 36 * GiB , 190"44.6 GiB free, W=0: the fallback with a 36.6 GiB copy" ); 191 } 192// A HARD memory limit just above the arena (R5070, found on l620: a 49 GiB cgroup limit, the 46.84 GiB arena chosen, the engine killed by the 193// cgroup OOM killer at the start). With the fallback allowed the arena needs its headroom under the limit too. 194 { 195const uint64_t need = 46ull * GiB + 860ull * (1ull <<20 )+ 2660000ull ;// IQ3_S: 46.84 GiB of experts and the spare blob 196const uint64_t head4 = 4 * GiB ,head8 = 8 * GiB ; 197auto verdict = [& ](uint64_t limit ,uint64_t keep ,uint64_t headroom ,bool fallback ) { 198return arena_ram_verdict (arena_hard_limit_need (need ,keep ,headroom ,fallback ), ~uint64_t {0 },limit ,fallback ); 199 }; 200check (arena_hard_limit_need (need ,0 ,head4 , true)== need + head4 ,"fallback allowed: the arena needs its headroom under the limit" ); 201check (arena_hard_limit_need (need ,8 * GiB ,head4 , true)== need + 8 * GiB ,"the RAM to keep counts when it is larger than the headroom" ); 202check (arena_hard_limit_need (need ,0 ,head4 , false)== need ,"fallback not allowed: the comparison stays as it was" ); 203check (arena_hard_limit_need (need ,8 * GiB ,head4 , false)== need + 8 * GiB ,"fallback not allowed: the RAM to keep is still asked" ); 204check (verdict (49ull * GiB ,0 ,head4 , true)== ArenaRamVerdict ::FallBack ,"49 GiB limit, 4 GiB headroom: the copy, not an arena that the OOM killer ends" ); 205check (verdict (49ull * GiB ,0 ,head8 , true)== ArenaRamVerdict ::FallBack ,"49 GiB limit, 8 GiB headroom (Windows): the copy" ); 206check (verdict (51ull * GiB ,0 ,head4 , true)== ArenaRamVerdict ::Load ,"51 GiB limit, 4 GiB headroom: the arena" ); 207check (verdict (need + head4 ,0 ,head4 , true)== ArenaRamVerdict ::Load ,"a limit of exactly arena + headroom: the arena" ); 208check (verdict (need + head4 - 1 ,0 ,head4 , true)== ArenaRamVerdict ::FallBack ,"one byte under: the copy" ); 209check (verdict (need - 1 ,0 ,head4 , true)== ArenaRamVerdict ::FallBack ,"a limit under the arena: the copy, as before" ); 210check (verdict (49ull * GiB ,0 ,head4 , false)== ArenaRamVerdict ::Load ,"fallback not allowed, 49 GiB limit: as before (no new refusal)" ); 211check (verdict (45ull * GiB ,0 ,head4 , false)== ArenaRamVerdict ::Refuse ,"fallback not allowed, limit under the arena: refused as before" ); 212check (verdict (60ull * GiB ,8 * GiB ,head4 , true)== ArenaRamVerdict ::Load ,"60 GiB limit, 8 GiB to keep: the arena" ); 213check (verdict (54ull * GiB ,8 * GiB ,head4 , true)== ArenaRamVerdict ::FallBack ,"54 GiB limit, 8 GiB to keep: the copy" ); 214check (verdict (std::numeric_limits < uint64_t > ::max (),0 ,head4 , true)== ArenaRamVerdict ::Load ,"no limit: the arena" ); 215 } 216 std::printf ("ple_residency_test: %d checks passed\n" ,checks ); 217return 0 ; 218}