okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
e85f56209
main
1// CPU only: pressure is injected through telemetry, not by exhausting the host. 2#include "strata/core/session_save_reclaim.hpp" 3#include <cstdio> 4#include <cstdlib> 5#include <new> 6 7static bool deny_alloc = false; 8void * operatornew (std::size_t n ) { 9if (deny_alloc )throw std::bad_alloc (); 10if (void * p = std::malloc (n ?n :1 ))return p ; 11throw std::bad_alloc (); 12} 13void operatordelete (void * p )noexcept { std::free (p ); } 14void operatordelete (void * p , std::size_t )noexcept { std::free (p ); } 15 16using namespace strata::core ; 17static int checks_run = 0 ; 18static void check (bool ok ,const char * label ) { 19++ checks_run ; 20if (!ok ) { std::fprintf (stderr ,"FAIL: %s\n" ,label ); std::exit (1 ); } 21} 22static ConversationCheckpoint checkpoint (size_t n ,bool pinned = false) { 23ConversationCheckpoint c ; 24c .ids .assign (n ,42 );c .imgs = {{1 ,99 }}; 25c .gdn .assign (4096 ,1 );c .ple = {2 };c .tails = {3 };c .dead = {4 };c .block_pos = {5 }; 26c .used = n ;c .pinned = pinned ; 27return c ; 28} 29static bool same (const ConversationCheckpoint & a ,const ConversationCheckpoint & b ) { 30return a .ids == b .ids && a .imgs == b .imgs && a .gdn == b .gdn && a .ple == b .ple && 31a .tails == b .tails && a .dead == b .dead && a .block_pos == b .block_pos && 32a .used == b .used && a .pinned == b .pinned && a .stage_parts .size ()== b .stage_parts .size (); 33} 34static void park (ConversationCache & cache ,int tokens ,bool pin = false) { 35SavedConversation c ;c .live = checkpoint (tokens ); 36c .checkpoints .push_back (checkpoint (tokens - 1 ,pin )); 37// Distinct histories, so put() does not remove superseded entries. 38c .live .ids [0 ]= tokens ;c .checkpoints [0 ].ids [0 ]= tokens ; 39check (cache .put (std::move (c )),"fixture parked" ); 40} 41int main () { 42SessionSaveLive live ;live .state_bytes = 1000 ;live .tokens = 100 ;live .images = 1 ;live .kv_layers = 2 ; 43const uint64_t floor = 1u <<20 ; 44// Every selected position, first-tie preference, and explicit pins (a pin can be shallower). 45for (size_t selected = 0 ;selected < 4 ;++ selected )for (bool pin : {false, true}) { 46 std::vector < ConversationCheckpoint > chain ; 47for (size_t i = 0 ;i < 4 ;++ i )chain .push_back (checkpoint (i == selected ?300 :100 ,pin && i == selected )); 48if (pin )chain [(selected + 1 ) %4 ].ids .resize (500 );// deeper, unpinned 49auto before = * session_deepest_checkpoint (chain ); 50const auto * storage = session_deepest_checkpoint (chain )-> gdn .data (); 51ConversationCache cache (1u <<20 ,10 ); 52park (cache ,10 );park (cache ,20 , true);park (cache ,30 ); 53 std::vector < ConversationKv > kv (1 );kv [0 ].k .resize (16384 );cache .retain (std::move (kv ),100 ); 54const size_t bytes_before = cache .bytes (); 55size_t chain_bytes = 0 ;for (const auto & c :chain )chain_bytes += c .bytes (); 56deny_alloc = true; 57auto freed = session_save_reclaim (chain ,cache ,live ,floor , [] {return std::optional < uint64_t > (0 ); }); 58deny_alloc = false; 59check (chain .size ()== 1 && same (chain [0 ],before ),"selected checkpoint unchanged under pressure" ); 60check (chain [0 ].gdn .data ()== storage ,"checkpoint moved without copying storage" ); 61check (freed .checkpoints == 3 && freed .parked == 2 && cache .size ()== 1 ,"pins survive, other entries reclaimed" ); 62check (cache .evictions ()== 2 && cache .retained_bytes ()== 0 ,"cache accounting and retained buffers" ); 63check (freed .bytes == bytes_before - cache .bytes ()+ chain_bytes - chain [0 ].bytes (),"released byte accounting" ); 64check (session_deepest_checkpoint (chain )-> ids == before .ids ,"same file selection after reclaim" ); 65auto again = session_save_reclaim (chain ,cache ,live ,floor , [] {return std::optional < uint64_t > (0 ); }); 66check (again .bytes == 0 && again .checkpoints == 0 && again .parked == 0 ,"exhausted reclaim is idempotent" ); 67 } 68// No-pressure path, unknown telemetry, overflowed estimate: no cache mutation. 69for (int mode = 0 ;mode < 4 ;++ mode ) { 70 std::vector < ConversationCheckpoint > chain {checkpoint (100 ),checkpoint (300 )}; 71ConversationCache cache (1u <<20 ,10 );park (cache ,10 ); 72const auto before = chain ; 73const auto bytes = cache .bytes ();auto sl = live ; 74if (mode == 2 )sl .state_bytes = UINT64_MAX ; 75const uint64_t need = session_save_peak_bytes (session_deepest_checkpoint (chain ),sl ); 76auto r = session_save_reclaim (chain ,cache ,sl ,floor , [& ]()-> std::optional < uint64_t > { 77if (mode == 1 )return std::nullopt ; 78return mode == 2 ?0 :need + floor + (mode == 3 ?1 :0 ); 79 }); 80check (!r .bytes && !r .parked && !r .checkpoints && cache .bytes ()== bytes ,"no eviction without known pressure" ); 81check (chain .size ()== 2 && same (chain [0 ],before [0 ])&& same (chain [1 ],before [1 ]),"unchanged order/content" ); 82 } 83// Admit after any individual step; never clear the whole cache when one release suffices. 84for (int sufficient_after = 1 ;sufficient_after <=4 ;++ sufficient_after ) { 85 std::vector < ConversationCheckpoint > chain {checkpoint (100 ),checkpoint (300 ),checkpoint (300 )}; 86auto * selected_storage = chain [1 ].gdn .data (); 87ConversationCache cache (1u <<20 ,10 );park (cache ,10 );park (cache ,20 ); 88 std::vector < ConversationKv > kv (1 );kv [0 ].v .resize (100 );cache .retain (std::move (kv ),100 ); 89const auto need = session_save_peak_bytes (session_deepest_checkpoint (chain ),live ); 90int probes = 0 ; 91auto r = session_save_reclaim (chain ,cache ,live ,floor , [& ]()-> std::optional < uint64_t > { 92return probes ++ >=sufficient_after ?need + floor :0 ; 93 }); 94check (probes == sufficient_after + 1 ,"stop immediately once RAM meets unchanged floor" ); 95check (r .parked == size_t (std::min (sufficient_after - 1 ,2 )),"one-at-a-time parked eviction" ); 96check (r .checkpoints == size_t (sufficient_after == 4 ),"checkpoint eviction only after parked cache" ); 97check (session_deepest_checkpoint (chain )-> gdn .data ()== selected_storage ,"first deepest tie retained" ); 98 } 99 { 100 std::vector < ConversationCheckpoint > chain {checkpoint (100 , true),checkpoint (300 , true),checkpoint (500 )}; 101ConversationCache cache (0 ,0 ); 102auto r = session_save_reclaim (chain ,cache ,live ,floor , [] {return std::optional < uint64_t > (0 ); }); 103check (r .checkpoints == 1 && chain .size ()== 2 && chain [0 ].pinned && chain [1 ].pinned ,"all pins survive in original order" ); 104 std::vector < ConversationCheckpoint > out ; std::string why ; 105check (!session_save_checkpoints (chain ,live , [](uint64_t , std::string & w ) {w = "low RAM" ;return false; },out ,why ) 106&& out .empty ()&& why == "low RAM" ,"still refused before copying when reclamation is insufficient" ); 107 } 108 { 109 std::vector < ConversationCheckpoint > chain ; 110ConversationCache cache (0 ,0 ); 111auto r = session_save_reclaim (chain ,cache ,live ,floor , [] {return std::optional < uint64_t > (0 ); }); 112check (!r .bytes && !r .checkpoints && !r .parked ,"empty chain and disabled cache" ); 113 } 114 std::printf ("session_save_reclaim: %d checks passed\n" ,checks_run ); 115}