okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
067879249
main
1// src/memlock.hpp: the split engine locks its host-read tables as far as the machine lets it and never fails because of it. No GPU. Cases: 2// 1 plan budget handed out in order: the first part first, page-sized pieces, cut by the memory-lock limit and by the free RAM 3// 2 fake an mlock that always succeeds / always fails / fails past a total: no exception, the numbers add up, the tables are untouched, the line is right 4// 3 real the real mlock under a lowered RLIMIT_MEMLOCK in a child process (the lock may fail or succeed; either way the lock call returns and the bytes are intact) 5// 4 raise a soft limit below the need and a hard limit above it is raised (child process) 6#include "../src/memlock.hpp" 7 8#include <sys/mman.h> 9#include <sys/wait.h> 10#include <unistd.h> 11 12#include <atomic> 13#include <cstdio> 14#include <cstdlib> 15 16using namespace fnx ; 17 18namespace { 19 20int fails = 0 ; 21#define CHECK (c ) \ 22 do { \ 23 if (!(c)) { \ 24 std::printf("FAIL line %d: %s\n", __LINE__, #c); \ 25 ++fails; \ 26 } \ 27 } while (0) 28 29constexpr size_t MiB = 1u <<20 ; 30 31uint8_t * map_pages (size_t n ,uint8_t seed ) { 32void * p = mmap (nullptr ,n ,PROT_READ |PROT_WRITE ,MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); 33if (p == MAP_FAILED ) std::abort (); 34auto * b = (uint8_t * )p ; 35for (size_t i = 0 ;i < n ;++ i )b [i ]= (uint8_t ) (seed + i * 7 ); 36return b ; 37} 38uint64_t sum (const uint8_t * b ,size_t n ) { 39uint64_t s = 0 ; 40for (size_t i = 0 ;i < n ;++ i )s = s * 31 + b [i ]; 41return s ; 42} 43 44std::atomic < uint64_t > g_locked {0 },g_calls {0 },g_cap {UINT64_MAX }; 45int fake_ok (const void * ,size_t ) {++ g_calls ;return 0 ; } 46int fake_fail (const void * ,size_t ) {++ g_calls ;errno = ENOMEM ;return -1 ; } 47int fake_cap (const void * ,size_t n ) {// like the kernel: a call that would pass the allowance fails whole 48++ g_calls ; 49uint64_t cur = g_locked .load (); 50for (;;) { 51if (cur + n > g_cap .load ()) {errno = ENOMEM ;return -1 ; } 52if (g_locked .compare_exchange_weak (cur ,cur + n ))return 0 ; 53 } 54} 55 56std::vector < LockPart > two_parts (uint8_t * a ,size_t na ,uint8_t * b ,size_t nb ) { 57LockPart x ,y ; 58x .name = "token_embd.weight" ;x .data = a ;x .nbytes = na ; 59y .name = "per_layer_token_embd.weight" ;y .data = b ;y .nbytes = nb ; 60return {x ,y }; 61} 62 63void test_plan () { 64uint8_t * a = map_pages (2 * MiB ,1 ); 65uint8_t * b = map_pages (8 * MiB ,2 ); 66auto parts = two_parts (a ,2 * MiB ,b ,8 * MiB ); 67 std::vector < uint64_t > t ; 68LockEnv e ;// nothing limits 69CHECK (plan_lock (parts ,e ,t )== 10 * MiB && t [0 ]== 2 * MiB && t [1 ]== 8 * MiB ); 70e .limit = 1 * MiB ;// the limit cuts inside the first part: the second gets nothing 71CHECK (plan_lock (parts ,e ,t )== 1 * MiB && t [0 ]== 1 * MiB && t [1 ]== 0 ); 72e .limit = 5 * MiB ;// the first whole, the second its start 73CHECK (plan_lock (parts ,e ,t )== 5 * MiB && t [0 ]== 2 * MiB && t [1 ]== 3 * MiB ); 74e .limit = 0 ; 75CHECK (plan_lock (parts ,e ,t )== 0 ); 76e = LockEnv {}; 77e .avail = 12 * MiB ;e .reserve = 9 * MiB ;// the free RAM leaves 3 MiB 78bool ram = false; 79CHECK (plan_lock (parts ,e ,t ,& ram )== 3 * MiB && ram ); 80e .avail = 4 * MiB ;// below the reserve: nothing 81CHECK (plan_lock (parts ,e ,t ,& ram )== 0 && ram ); 82munmap (a ,2 * MiB );munmap (b ,8 * MiB ); 83} 84 85void test_fake () { 86uint8_t * a = map_pages (2 * MiB + 100 ,3 );// not a whole number of pages: the piece edges must still work 87uint8_t * b = map_pages (8 * MiB + 17 ,4 ); 88const uint64_t sa = sum (a ,2 * MiB + 100 ),sb = sum (b ,8 * MiB + 17 ); 89// everything locks: no line 90 { 91auto parts = two_parts (a ,2 * MiB + 100 ,b ,8 * MiB + 17 ); 92g_calls = 0 ; 93LockOutcome o = lock_parts (parts ,LockEnv {},fake_ok ); 94CHECK (o .locked == o .want && o .want == 10 * MiB + 117 ); 95CHECK (parts [0 ].locked == parts [0 ].nbytes && parts [1 ].locked == parts [1 ].nbytes ); 96CHECK (lock_line (parts ,o ).empty ()); 97CHECK (g_calls > 0 ); 98 } 99// the limit lets the first part and a bit of the second: partial, the line says "only partly" and names the limit 100 { 101auto parts = two_parts (a ,2 * MiB + 100 ,b ,8 * MiB + 17 ); 102LockEnv e ; 103e .limit = 4 * MiB ; 104LockOutcome o = lock_parts (parts ,e ,fake_ok ); 105CHECK (parts [0 ].locked == parts [0 ].nbytes ); 106CHECK (parts [1 ].locked > 0 && parts [1 ].locked < parts [1 ].nbytes ); 107CHECK (o .locked <=4 * MiB + 4096 ); 108const std::string l = lock_line (parts ,o ); 109CHECK (l .find (" of its " )!= std::string::npos && l .find ("locked in RAM" )!= std::string::npos && l .find ("at most 4 MiB" )!= std::string::npos ); 110CHECK (l .find ("runs as usual" )!= std::string::npos ); 111 } 112// nothing may be locked: mlock is not even called, the tables are read in, the line says "not locked" 113 { 114auto parts = two_parts (a ,2 * MiB + 100 ,b ,8 * MiB + 17 ); 115LockEnv e ; 116e .limit = 0 ; 117g_calls = 0 ; 118LockOutcome o = lock_parts (parts ,e ,fake_ok ); 119CHECK (o .locked == 0 && g_calls == 0 ); 120const std::string l = lock_line (parts ,o ); 121CHECK (l .find ("not locked in RAM" )!= std::string::npos ); 122 } 123// the system refuses although the plan allowed it (another process took the allowance): no throw, nothing counted as locked, the errno in the line 124 { 125auto parts = two_parts (a ,2 * MiB + 100 ,b ,8 * MiB + 17 ); 126LockEnv e ;// no limit known 127LockOutcome o = lock_parts (parts ,e ,fake_fail ); 128CHECK (o .locked == 0 && o .err == ENOMEM ); 129CHECK (lock_line (parts ,o ).find ("the system refused" )!= std::string::npos ); 130 } 131// the allowance runs out in the middle (a kernel that fails a call whole): a partial state, no throw, the count equals what the fake locked 132 { 133auto parts = two_parts (a ,2 * MiB + 100 ,b ,8 * MiB + 17 ); 134g_locked = 0 ;g_cap = 6 * MiB ; 135LockOutcome o = lock_parts (parts ,LockEnv {},fake_cap ); 136CHECK (o .locked > 0 && o .locked < o .want ); 137CHECK (o .locked == g_locked .load ()); 138CHECK (!lock_line (parts ,o ).empty ()); 139g_cap = UINT64_MAX ; 140 } 141// RAM short: the line names the reserve 142 { 143auto parts = two_parts (a ,2 * MiB + 100 ,b ,8 * MiB + 17 ); 144LockEnv e ; 145e .avail = 6 * MiB ;e .reserve = 5 * MiB ; 146LockOutcome o = lock_parts (parts ,e ,fake_ok ); 147CHECK (o .limited_by_ram && o .locked <=1 * MiB + 4096 ); 148CHECK (lock_line (parts ,o ).find ("less than" )!= std::string::npos ); 149 } 150CHECK (sum (a ,2 * MiB + 100 )== sa && sum (b ,8 * MiB + 17 )== sb );// the tables are exactly as they were 151munmap (a ,2 * MiB + 100 );munmap (b ,8 * MiB + 17 ); 152} 153 154// runs `f` in a child (limits set there stay there); returns the child's exit code 155template < class F > int in_child (F f ) { 156const pid_t pid = fork (); 157if (pid == 0 )_exit (f ()); 158int st = 0 ; 159waitpid (pid ,& st ,0 ); 160return WIFEXITED (st ) ?WEXITSTATUS (st ) :99 ; 161} 162 163// the sentences in the owner's wording of 11-11, at the real sizes 164void test_words () { 165const uint64_t G = 1ull <<30 ; 166 std::vector < LockPart > none ; 167LockOutcome o ; 168o .want = (size_t ) (27.5 * G ); 169o .env .limit = 8 * (1ull <<20 ); 170o .locked = 0 ; 171CHECK (lock_line (none ,o )== "Split engine: its word tables (27.5 GiB) are not locked in RAM (this account may lock at most 8 MiB). It runs as usual. The tables are read into memory at the start; " 172"only if the PC runs short of memory can long prompts slow down." ); 173o .locked = 8u <<20 ;// a sliver of a table of GiB counts as nothing locked 174CHECK (lock_line (none ,o ).find ("are not locked in RAM" )!= std::string::npos ); 175o .locked = (size_t ) (14.0 * G ); 176o .env .limit = 14 * G ; 177CHECK (lock_line (none ,o )== "Split engine: 14.0 of its 27.5 GiB of word tables are locked in RAM (this account may lock at most 14.0 GiB). It runs as usual. The rest is read into memory at the start; " 178"only if the PC runs short of memory can long prompts slow down." ); 179o .env .limit = UINT64_MAX ; 180o .limited_by_ram = true; 181o .env .reserve = (uint64_t ) (6.3 * G ); 182CHECK (lock_line (none ,o ).find ("(locking more would leave less than 6.3 GiB of RAM free)" )!= std::string::npos ); 183o .locked = o .want ; 184CHECK (lock_line (none ,o ).empty ()); 185} 186 187void test_real () { 188// the soft AND hard limit are lowered to 256 KiB: the soft limit cannot be raised, the real mlock refuses most of 10 MiB (a process with CAP_IPC_LOCK is not limited: then all locks) 189const int rc = in_child ([] { 190struct rlimit lo {256 * 1024 ,256 * 1024 }; 191if (setrlimit (RLIMIT_MEMLOCK ,& lo )!= 0 )return 90 ; 192uint8_t * a = map_pages (2 * MiB ,5 ); 193uint8_t * b = map_pages (8 * MiB ,6 ); 194const uint64_t sa = sum (a ,2 * MiB ),sb = sum (b ,8 * MiB ); 195auto parts = two_parts (a ,2 * MiB ,b ,8 * MiB ); 196const LockEnv e = read_lock_env (10 * MiB ); 197LockOutcome o = lock_parts (parts ,e );// must return, whatever the kernel says 198const bool privileged = o .locked == o .want ; 199if (!privileged && o .locked > 256 * 1024 + 4096 )return 91 ; 200if (!privileged && lock_line (parts ,o ).empty ())return 92 ; 201if (sum (a ,2 * MiB )!= sa || sum (b ,8 * MiB )!= sb )return 93 ; 202return 0 ; 203 }); 204CHECK (rc == 0 ); 205} 206 207void test_raise () { 208// soft 128 KiB, hard 64 MiB, need 10 MiB: the soft limit is raised (privileged or not) and the whole lock succeeds 209const int rc = in_child ([] { 210struct rlimit lo {128 * 1024 ,64 * MiB }; 211if (setrlimit (RLIMIT_MEMLOCK ,& lo )!= 0 )return 77 ;// the hard limit of this process is already below 64 MiB (a test run under ulimit -l 8192): it cannot be raised here, the case does not apply 212const LockEnv e = read_lock_env (10 * MiB ); 213if (!e .raised )return 91 ; 214struct rlimit now {}; 215getrlimit (RLIMIT_MEMLOCK ,& now ); 216if (now .rlim_cur != 64 * MiB )return 92 ;// need + 1 GiB, capped at the hard limit 217uint8_t * a = map_pages (2 * MiB ,7 ); 218uint8_t * b = map_pages (8 * MiB ,8 ); 219auto parts = two_parts (a ,2 * MiB ,b ,8 * MiB ); 220LockOutcome o = lock_parts (parts ,e ); 221if (o .locked != o .want && !(e .avail != UINT64_MAX && e .avail < o .want + e .reserve ))return 93 ;// all locked, unless this PC is short of RAM 222return 0 ; 223 }); 224CHECK (rc == 0 || rc == 77 ); 225} 226 227}// namespace 228 229int main () { 230test_plan (); 231test_fake (); 232test_words (); 233test_real (); 234test_raise (); 235 std::printf ("%s\n" ,fails ?"memlock_test: FAILED" :"memlock_test: all passed" ); 236return fails ?1 :0 ; 237}