okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fcb0c2d0e
main
1#include "strata/core/conversation_memory.hpp" 2 3#include <cstdio> 4#include <cstdlib> 5#include <limits> 6#include <sstream> 7 8#if defined(_WIN32 ) 9#ifndef NOMINMAX 10#define NOMINMAX 11#endif 12#include <windows.h> 13#endif 14 15using namespace strata::core ; 16namespace { 17int checks = 0 ; 18void check (bool ok ,const char * label ) { 19++ checks ; 20if (!ok ) { std::fprintf (stderr ,"FAIL: %s\n" ,label ); std::exit (1 ); } 21} 22} 23 24int main () { 25for (const char * text : {"" ,"MemFree: 100 kB\n" ,"MemAvailable: -1 kB\n" , 26"MemAvailable: +1 kB\n" ,"MemAvailable: 1 MB\n" , 27"MemAvailable: 1\n" ,"MemAvailable: 1x kB\n" , 28"MemAvailable: 18446744073709551615 kB\n" , 29"MemAvailable: 18446744073709551616 kB\n" , 30"MemAvailable: 1 kB trailing\n" , 31"MemAvailable: 1 kB\nMemAvailable: 2 kB\n" }) { 32 std::istringstream input (text ); 33check (!conversation_mem_available (input ),"missing/malformed telemetry is unknown" ); 34 } 35 std::istringstream normal ("MemTotal: 999999 kB\nMemAvailable: 12345 kB\nSwapFree: 777 kB\n" ); 36check (conversation_mem_available (normal )== 12345ULL * 1024 ,"only MemAvailable is counted" ); 37 std::istringstream zero ("MemAvailable: 0 kB" ); 38check (conversation_mem_available (zero )== 0 ,"zero available memory is known" ); 39 std::istringstream broken ("MemAvailable: 123 kB\n" ); 40broken .setstate (std::ios::badbit ); 41check (!conversation_mem_available (broken ),"I/O failure declines admission" ); 42check (!conversation_memory_admit ({},0 ,0 ),"unknown fails closed even with zero floor" ); 43check (conversation_memory_admit (100 ,40 ,60 ),"exact allocation plus floor fits" ); 44check (!conversation_memory_admit (99 ,40 ,60 ),"one byte below required memory rejected" ); 45check (!conversation_memory_admit (59 ,0 ,60 ),"floor subtraction cannot underflow" ); 46check (conversation_memory_admit (60 ,0 ,60 ),"post-capture floor check" ); 47check (!conversation_memory_admit (100 , std::numeric_limits < uint64_t > ::max (),1 ),"allocation arithmetic cannot overflow" ); 48check (conversation_memory_admit (std::numeric_limits < uint64_t > ::max (), 49 std::numeric_limits < uint64_t > ::max (),0 ),"maximal exact bound" ); 50check (!conversation_memory_admit (std::numeric_limits < uint64_t > ::max (), 51 std::numeric_limits < uint64_t > ::max (),1 ),"maximal sum overflow rejected" ); 52// #3493: the budget used when --conversation-cache-mib is not given 53const auto GiB = [](uint64_t n ) {return n <<30 ; }; 54check (auto_conversation_cache_mib ({},2560 )== 0 ,"unknown telemetry: no cache" ); 55check (auto_conversation_cache_mib (GiB (2 ),2560 )== 0 ,"below the parking floor: off" ); 56check (auto_conversation_cache_mib (GiB (4 ),2560 )== 0 ,"4 GiB free: 40% of 1.5 GiB is under 1 GiB: off" ); 57check (auto_conversation_cache_mib (GiB (8 ),2560 )== 2048 ,"8 GiB free: 40% of 5.5 GiB, in steps of 256" ); 58check (auto_conversation_cache_mib (GiB (20 ),2560 )== 7168 ,"20 GiB free: 40% of 17.5 GiB" ); 59check (auto_conversation_cache_mib (GiB (40 ),2560 )== 8192 ,"capped at 8 GiB" ); 60check (auto_conversation_cache_mib (GiB (1000 ),0 )== 8192 ,"a big server: still the cap" ); 61check (auto_conversation_cache_mib (GiB (40 ),1000000 )== 0 ,"a floor above the RAM: off, no underflow" ); 62// Test the real provider without assuming any particular amount of free RAM. 63const auto available = available_host_bytes (); 64check (!available || conversation_memory_admit (available ,0 ,0 ),"provider returns bytes or unknown" ); 65#if defined(_WIN32 ) 66// Unknown must fail closed in production, but must not let a broken Windows provider pass this test. 67// Compare with total physical memory, not a second available reading: other processes can allocate 68// between calls. No pressure allocation or fixed free-memory assumption is needed. 69MEMORYSTATUSEX physical {}; 70physical .dwLength = sizeof physical ; 71check (GlobalMemoryStatusEx (& physical )!= 0 ,"Windows physical-memory API is available" ); 72check (available .has_value ()&& * available <=physical .ullTotalPhys , 73"Windows provider returns a known, physically bounded sample" ); 74#endif 75 std::printf ("conversation_memory_test: %d checks passed\n" ,checks ); 76}