okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
6b63729a9
main
1#include "strata/core/expert_source.hpp" 2#include "strata/core/foresight_swap.hpp" 3#include "strata/kernels/cpu/expert.hpp" 4#include "strata/kernels/cpu/expert_layout.hpp" 5 6#if defined(STRATA_NATIVE_EXPERTS ) 7#include "strata/kernels/cpu/native_expert.hpp" 8#include "ggml.h" 9#endif 10 11#include <cuda_runtime.h> 12#include <cstdlib> 13#include <cstring> 14#include <algorithm> 15#include <chrono> 16#include <cstdio> 17#include <cstdlib> 18#include <cstring> 19#include <cstddef> 20#include <cstdint> 21#include <cstdlib> 22#include <cstring> 23#include <filesystem> 24#include <fstream> 25#include <iostream> 26#include <limits> 27#include <stdexcept> 28#include <string> 29#include <system_error> 30#include <thread> 31#include <utility> 32#include <vector> 33 34#if defined(_WIN32 ) 35#define WIN32_LEAN_AND_MEAN 36#ifndef NOMINMAX 37#define NOMINMAX 38#endif 39#define PSAPI_VERSION 2 40#include <windows.h> 41#include <psapi.h> 42#endif 43 44namespace fs= std::filesystem; 45 46namespace { 47 48void require (bool ok ,const std::string & message ) { 49if (!ok )throw std::runtime_error (message ); 50} 51 52struct TempDirectory { 53 fs::path path ; 54 55explicit TempDirectory (const fs::path & base = fs::temp_directory_path ()) { 56const auto stamp = std::chrono::steady_clock::now ().time_since_epoch ().count (); 57path = base / ("strata-file-source-test-" + std::to_string (stamp )); 58 fs::create_directories (path ); 59 } 60 61 ~TempDirectory () { 62 std::error_code ignored ; 63 fs::remove_all (path ,ignored ); 64 } 65}; 66 67void create_pack (const fs::path & dir ,uint64_t bytes ,const std::vector < std::pair < uint64_t ,char >>& markers = {}) { 68const fs::path file = dir / "experts.bin" ; 69{ 70std::ofstream out ( file , std::ios:: binary | std::ios:: trunc ); 71require ((bool) out , "could not create synthetic experts.bin" ); 72if ( bytes > 0 ) { 73out . seekp ((std:: streamoff ) ( bytes - 1 )); 74out . put ( '\0' ); 75} 76require ((bool) out , "could not size synthetic experts.bin" ); 77} 78for ( const auto & [ offset , marker ] : markers ) { 79require ( offset < bytes , "synthetic marker lies beyond experts.bin" ); 80std::fstream file_out ( file , std::ios:: binary | std::ios:: in | std::ios:: out ); 81require ((bool) file_out , "could not open synthetic experts.bin for markers" ); 82file_out . seekp ((std::streamoff) offset ); 83file_out . put ( marker ); 84require ((bool) file_out , "could not write synthetic expert marker" ); 85} 86} 87 88void check_file_size_rejection (strata::core::FileExpertSource & source , const fs::path & dir , int64_t layers , 89int64_t experts , uint64_t expected_bytes ) { 90std::string err ; 91source . close (); 92fs:: resize_file ( dir / "experts.bin" , expected_bytes - 1 ); 93require (! source . open ( dir . string (), layers , experts , err ), "a truncated expert file was accepted" ); 94require (! source . mapped () && ! err . empty (), "truncated-file rejection left the source mapped or unreported" ); 95 96fs:: resize_file ( dir / "experts.bin" , expected_bytes + 1 ); 97err . clear (); 98require (! source . open ( dir . string (), layers , experts , err ), "an oversized expert file was accepted" ); 99require (! source . mapped () && ! err . empty (), "oversized-file rejection left the source mapped or unreported" ); 100} 101 102#if defined( _WIN32 ) 103void check_mapped_release (strata::core::FileExpertSource & source , int64_t layer , int64_t expert , uint64_t bytes ) { 104const uint8_t * p = source . blob ( layer , expert ); 105require ( p != nullptr , "release fixture has no mapped blob" ); 106const std::vector < uint8_t > saved ( p , p + (size_t) bytes ); 107SYSTEM_INFO info {}; 108GetSystemInfo ( & info ); 109const uintptr_t page = info . dwPageSize ; 110const uintptr_t first = ((uintptr_t) p + page - 1 ) / page * page ; 111const uintptr_t end = ((uintptr_t) p + bytes ) / page * page ; 112require ( end > first , "release fixture has no full interior page" ); 113auto resident = [ & ](uintptr_t address ) { 114PSAPI_WORKING_SET_EX_INFORMATION state {}; 115state . VirtualAddress = (void * ) address ; 116require ( QueryWorkingSetEx ( GetCurrentProcess (), & state , sizeof state ) != 0 , 117"QueryWorkingSetEx failed for the release fixture" ); 118return state . VirtualAttributes . Valid != 0 ; 119}; 120const char * setting = std:: getenv ( "STRATA_FILE_RELEASE" ); 121const bool enabled = setting != nullptr && std:: strcmp ( setting , "1" ) == 0 ; 122for (int round = 0 ; round < 3 ; ++ round ) { 123require (std:: memcmp ( p , saved . data (), saved . size ()) == 0 , "release changed the mapped expert bytes" ); 124require ( resident ( first ), "reading the expert did not bring its interior page into the working set" ); 125const uint64_t released = source . release ( layer , expert ); 126require ( released == ( enabled ? end - first : 0 ), "release did not honor full pages or its opt-in switch" ); 127// Off: the page stays. On: Windows may keep a trimmed page on its standby list and map it back at once, so a 128// page that is still valid is reported, not failed (the byte count above is the contract). 129if (! enabled ) require ( resident ( first ), "release trimmed pages with the switch off" ); 130else if ( resident ( first )) std:: fprintf ( stderr , "note: the trimmed page is already back in the working set\n" ); 131if ((uintptr_t) p < first ) 132require ( resident ((uintptr_t) p ), "release trimmed the expert's shared first page" ); 133if ( end < (uintptr_t) p + bytes ) 134require ( resident ( end ), "release trimmed the expert's shared last page" ); 135} 136require (std:: memcmp ( p , saved . data (), saved . size ()) == 0 , "the expert changed after repeated release/read cycles" ); 137require ( source . release ( -1 , expert ) == 0 && source . release ( layer , -1 ) == 0 && 138source . release (std::numeric_limits < int64_t > :: max (), expert ) == 0 && 139source . release ( layer , std::numeric_limits < int64_t > :: max ()) == 0 , 140"release accepted an invalid layer or expert" ); 141} 142#endif 143 144void test_canonical_layout () { 145using namespace strata:: core ; 146using namespace strata::kernels:: cpu ; 147constexpr int64_t layers = 2 ; 148constexpr int64_t experts = 3 ; 149const uint64_t layer_bytes = (uint64_t) experts * BLOB ; 150const uint64_t total = (uint64_t) layers * layer_bytes ; 151TempDirectory dir ; 152 153std::string err ; 154const bool layout_ok = expert_layout_load ( dir . path . string (), layers , experts , err ); 155require ( layout_ok , "could not load canonical layout: " + err ); 156create_pack ( dir . path , total , {{ 0 , 'a' }, {(uint64_t) BLOB , 'b' }, { layer_bytes , 'c' }, 157{ layer_bytes + (uint64_t) BLOB , 'd' }}); 158 159FileExpertSource source ; 160bool opened = source . open ( dir . path . string (), layers , experts , err ); 161require ( opened , "could not map canonical pack: " + err ); 162require ( source . blobs () == layers * experts , "canonical blob count is wrong" ); 163const uint8_t * first = source . blob ( 0 , 0 ); 164const uint8_t * second = source . blob ( 0 , 1 ); 165const uint8_t * next_layer = source . blob ( 1 , 0 ); 166require ( first && second && next_layer , "valid canonical blob lookup failed" ); 167require ( second - first == (ptrdiff_t) BLOB && next_layer - first == (ptrdiff_t) layer_bytes , 168"canonical expert or layer stride is wrong" ); 169require ( first [ 0 ] == 'a' && second [ 0 ] == 'b' && next_layer [ 0 ] == 'c' , 170"canonical blob lookup returned bytes from the wrong expert" ); 171require ( source . blob ( -1 , 0 ) == nullptr && source . blob ( 0 , -1 ) == nullptr && 172source . blob ( layers , 0 ) == nullptr && source . blob ( 0 , experts ) == nullptr , 173"canonical bounds check accepted an invalid layer or expert" ); 174require ( source . reads () == 3 , "invalid canonical lookups changed the read count" ); 175#if defined( _WIN32 ) 176check_mapped_release ( source , 0 , 0 , BLOB ); 177check_mapped_release ( source , 0 , 1 , BLOB ); 178#endif 179 180check_file_size_rejection ( source , dir . path , layers , experts , total ); 181create_pack ( dir . path , total , {{ layer_bytes , 'c' }}); 182err . clear (); 183opened = source . open ( dir . path . string (), layers , experts , err ); 184require ( opened , "source failed to reopen after size errors: " + err ); 185const uint8_t * reopened = source . blob ( 1 , 0 ); 186require ( reopened && source . reads () == 1 && reopened [ 0 ] == 'c' , "reopened canonical source kept stale state" ); 187source . close (); 188require ( source . release ( 0 , 0 ) == 0 , "a closed file source released pages" ); 189} 190 191#if defined( STRATA_NATIVE_EXPERTS ) 192void test_native_variable_layout () { 193using namespace strata:: core ; 194using namespace strata::kernels:: cpu ; 195constexpr int64_t layers = 2 ; 196constexpr int64_t experts = 3 ; 197constexpr int64_t embedding = 2560 ; 198constexpr int64_t feed_forward = 640 ; 199TempDirectory dir ; 200 201NativeFmt first_fmt , second_fmt ; 202std::string err ; 203const bool first_ok = native_fmt ( GGML_TYPE_IQ3_XXS , GGML_TYPE_IQ4_NL , embedding , feed_forward , first_fmt , err ); 204require ( first_ok , "IQ3_XXS/IQ4_NL synthetic format is unavailable: " + err ); 205err . clear (); 206const bool second_ok = native_fmt ( GGML_TYPE_IQ2_XS , GGML_TYPE_IQ4_NL , embedding , feed_forward , second_fmt , err ); 207require ( second_ok , "IQ2_XS/IQ4_NL synthetic format is unavailable: " + err ); 208require ( first_fmt . bytes != second_fmt . bytes , "chosen native formats do not exercise variable layer sizes" ); 209 210const uint64_t layer0_bytes = (uint64_t) first_fmt . bytes * experts ; 211const uint64_t total = layer0_bytes + (uint64_t) second_fmt . bytes * experts ; 212{ 213std::ofstream metadata ( dir . path / "native_experts.txt" ); 214require ((bool) metadata , "could not create synthetic native_experts.txt" ); 215metadata << "0 " << GGML_TYPE_IQ3_XXS << ' ' << GGML_TYPE_IQ4_NL << " 0 " << first_fmt . bytes << '\n' ; 216metadata << "1 " << GGML_TYPE_IQ2_XS << ' ' << GGML_TYPE_IQ4_NL << ' ' << layer0_bytes << ' ' 217<< second_fmt . bytes << '\n' ; 218require ((bool) metadata , "could not write synthetic native_experts.txt" ); 219} 220create_pack ( dir . path , total , {{ 0 , 'a' }, {(uint64_t) first_fmt . bytes , 'b' }, 221{ layer0_bytes , 'c' }, { layer0_bytes + (uint64_t) second_fmt . bytes , 'd' }}); 222 223const bool layout_ok = expert_layout_load ( dir . path . string (), layers , experts , err ); 224require ( layout_ok , "could not load synthetic native layout: " + err ); 225FileExpertSource source ; 226bool opened = source . open ( dir . path . string (), layers , experts , err ); 227require ( opened , "could not map native pack: " + err ); 228require ( source . blobs () == layers * experts , "native blob count is wrong" ); 229const uint8_t * first = source . blob ( 0 , 0 ); 230const uint8_t * second = source . blob ( 0 , 1 ); 231const uint8_t * next_layer = source . blob ( 1 , 0 ); 232const uint8_t * next_layer_second = source . blob ( 1 , 1 ); 233require ( first && second && next_layer && next_layer_second , "valid native blob lookup failed" ); 234require ( second - first == (ptrdiff_t) first_fmt . bytes && next_layer - first == (ptrdiff_t) layer0_bytes && 235next_layer_second - next_layer == (ptrdiff_t) second_fmt . bytes , 236"native per-layer expert stride is wrong" ); 237require ( first [ 0 ] == 'a' && second [ 0 ] == 'b' && next_layer [ 0 ] == 'c' && next_layer_second [ 0 ] == 'd' , 238"native blob lookup returned bytes from the wrong expert" ); 239require ( source . blob ( -1 , 0 ) == nullptr && source . blob ( 0 , -1 ) == nullptr && 240source . blob ( layers , 0 ) == nullptr && source . blob ( 0 , experts ) == nullptr , 241"native bounds check accepted an invalid layer or expert" ); 242require ( source . reads () == 4 , "invalid native lookups changed the read count" ); 243#if defined( _WIN32 ) 244check_mapped_release ( source , 0 , 1 , first_fmt . bytes ); 245check_mapped_release ( source , 1 , 1 , second_fmt . bytes ); 246#endif 247 248check_file_size_rejection ( source , dir . path , layers , experts , total ); 249create_pack ( dir . path , total , {{ layer0_bytes , 'c' }}); 250err . clear (); 251opened = source . open ( dir . path . string (), layers , experts , err ); 252require ( opened , "source failed to reopen after size errors: " + err ); 253const uint8_t * reopened = source . blob ( 1 , 0 ); 254require ( reopened && source . reads () == 1 && reopened [ 0 ] == 'c' , "reopened native source kept stale state" ); 255source . close (); 256} 257#endif 258 259void test_complement_plan () { 260using namespace strata::core:: detail ; 261std::vector < uint64_t > offsets ; 262uint64_t bytes = 0 ; 263std::string error ; 264require ( make_cache_complement_plan ( 2 , 3 , { 3 , 5 }, {{ 0 , 1 }, { 1 , 2 }}, {}, offsets , bytes , error ), error ); 265require ( bytes == 16 && offsets == std::vector < uint64_t > { 0 , kNoCacheComplement , 3 , 6 , 11 , kNoCacheComplement }, 266"wrong compact offsets for variable native layer sizes" ); 267const uint8_t resident [ 16 ] = {}, mapped [ 5 ] = {}; 268require ( cache_complement_blob_or_fallback ( 1 , offsets , resident , mapped ) == mapped , 269"GPU-resident expert lost mmap fallback" ); 270require ( cache_complement_blob_or_fallback ( 4 , offsets , resident , mapped ) == resident + 11 , 271"CPU miss did not use resident complement" ); 272require (! make_cache_complement_plan ( 2 , 3 , { 3 , 5 }, {{ 0 , 1 }, { 0 , 1 }}, {}, offsets , bytes , error ) 273&& offsets . empty () && bytes == 0 , "duplicate pair accepted" ); 274require (! make_cache_complement_plan ( 2 , 3 , { 3 , 5 }, {{ 0 , 1 }}, {{ 0 , 1 }}, offsets , bytes , error ), 275"overlapping tiers accepted" ); 276require (! make_cache_complement_plan ( 2 , 3 , { 3 , 5 }, {{ 2 , 0 }}, {}, offsets , bytes , error ), 277"out-of-range pair accepted" ); 278require (! make_cache_complement_plan ( 2 , 3 , { 0 , 5 }, {}, {}, offsets , bytes , error ), "zero-size layer accepted" ); 279} 280 281void test_resident_lend_region () { 282using namespace strata::core:: detail ; 283// four slots (sizes 5, 3, 3, 5); the experts no slot holds take 10 bytes 284const std::vector < uint64_t > slots { 5 , 3 , 3 , 5 }; 285require ( choose_resident_keep_from ( slots , 10 , 9 , 1 ) == -1 , "a base larger than the budget was accepted" ); 286require ( choose_resident_keep_from ( slots , 10 , 10 , 1 ) == 4 , "no room: the lend region must stay on the file" ); 287require ( choose_resident_keep_from ( slots , 10 , 15 , 1 ) == 3 , "the last slot fits and must be kept first" ); 288require ( choose_resident_keep_from ( slots , 10 , 17 , 1 ) == 3 , "a slot that does not fit ends the lend region's walk" ); 289require ( choose_resident_keep_from ( slots , 10 , 18 , 1 ) == 2 , "two slots fit" ); 290require ( choose_resident_keep_from ( slots , 10 , 100 , 1 ) == 1 , "the walk must stop at the lend region's first slot" ); 291require ( choose_resident_keep_from ( slots , 10 , 100 , -1 ) == 4 , "no lend region kept slots in RAM" ); 292require ( choose_resident_keep_from ( slots , 10 , 100 , 9 ) == 4 , "an out-of-range lend region kept slots in RAM" ); 293require ( choose_resident_keep_from ({}, 0 , 0 , 0 ) == 0 , "an empty cache" ); 294} 295 296void test_split_lend_regions () { 297using namespace strata::core:: detail ; 298// two layers, three experts; a layer-0 blob is 5 bytes and a layer-1 blob 3. The plan under 299// construction already holds the (0,1) expert (an expert no cache holds); stage 0's region lends 300// (0,2) and (0,1) from its highest slots, stage 1's lends (1,2), (1,0), (1,1) in that order. 301const std::vector < uint64_t > blobs { 5 , 3 }; 302const std::vector < std::vector < std::pair < int32_t, int32_t >>> regions = {{{ 0 , 2 }, { 0 , 1 }}, {{ 1 , 2 }, { 1 , 0 }, { 1 , 1 }}}; 303std::string err ; 304std::vector < uint64_t > offsets ; 305uint64_t bytes = 0 ; 306 307// room for everything: (0,1) is already in the copy and is passed over, not counted twice 308offsets = { kNoCacheComplement , 0 , kNoCacheComplement , kNoCacheComplement , kNoCacheComplement , kNoCacheComplement }; 309bytes = 5 ; 310require ( append_stage_lend_regions ( 2 , 3 , blobs , offsets , bytes , 100 , regions , err ) == 4 , 311"both stage lend regions must be kept" ); 312require ( bytes == 19 && 313offsets == std::vector < uint64_t > { kNoCacheComplement , 0 , 5 , 13 , 16 , 10 }, 314"the stage lend regions got wrong compact offsets" ); 315 316// the cap is the whole copy's: the stage-0 walk fits (0,2) and stage 1 stops at the first pair that 317// does not fit; that stage's rest stays on the file 318offsets = { kNoCacheComplement , 0 , kNoCacheComplement , kNoCacheComplement , kNoCacheComplement , kNoCacheComplement }; 319bytes = 5 ; 320require ( append_stage_lend_regions ( 2 , 3 , blobs , offsets , bytes , 10 , regions , err ) == 1 , "cap 10 keeps only (0,2)" ); 321require ( bytes == 10 && offsets [ 2 ] == 5 && offsets [ 3 ] == kNoCacheComplement && offsets [ 5 ] == kNoCacheComplement , 322"the walk placed experts past the cap" ); 323 324// (0,2)'s 5 bytes do not fit in the 4 left: stage 0 keeps nothing, and stage 1's own walk still runs - 325// its (1,2) fits, its (1,0) stops that stage 326offsets = { kNoCacheComplement , 0 , kNoCacheComplement , kNoCacheComplement , kNoCacheComplement , kNoCacheComplement }; 327bytes = 5 ; 328require ( append_stage_lend_regions ( 2 , 3 , blobs , offsets , bytes , 9 , regions , err ) == 1 , 329"a stage refused at the cap must not stop the next stage's walk" ); 330require ( bytes == 8 && offsets [ 2 ] == kNoCacheComplement && offsets [ 5 ] == 5 && offsets [ 3 ] == kNoCacheComplement , 331"the refused stage's experts entered the copy, or the next stage's did not" ); 332 333// a pair outside the geometry is an error, and the plan is left untouched: validate before appending 334offsets = { kNoCacheComplement , 0 , kNoCacheComplement , kNoCacheComplement , kNoCacheComplement , kNoCacheComplement }; 335bytes = 5 ; 336require ( append_stage_lend_regions ( 2 , 3 , blobs , offsets , bytes , 100 , {{{ 0 , 2 }, { 2 , 0 }}}, err ) == -1 && 337! err . empty () && bytes == 5 && offsets [ 2 ] == kNoCacheComplement , 338"an out-of-range lend-region pair was accepted, or a rejected walk touched the plan" ); 339} 340 341void test_resident_exchange () { 342using namespace strata:: core ; 343using namespace strata::core:: detail ; 344// 2 layers x 3 experts; the GPU holds (0,1) and (1,2): the compact copy holds the other four 345std::vector < uint64_t > offsets ; 346uint64_t bytes = 0 ; 347std::string error ; 348require ( make_cache_complement_plan ( 2 , 3 , { 3 , 5 }, {{ 0 , 1 }, { 1 , 2 }}, {}, offsets , bytes , error ), error ); 349const std::vector < uint64_t > before = offsets ; 350// (1,0) moves into the GPU, (1,2) leaves it: (1,2) takes (1,0)'s bytes' place 351require ( exchange_cache_complement ( offsets , 3 , 5 ), "a valid exchange was refused" ); 352require ( offsets [ 5 ] == before [ 3 ] && offsets [ 3 ] == kNoCacheComplement , "the exchange did not move the place" ); 353for (size_t i : { 0u , 1u , 2u , 4u }) require ( offsets [ i ] == before [ i ], "an exchange touched another expert" ); 354// the copy's size and its set of places are unchanged 355std::vector < uint64_t > a , b ; 356for (uint64_t o : before ) if ( o != kNoCacheComplement ) a . push_back ( o ); 357for (uint64_t o : offsets ) if ( o != kNoCacheComplement ) b . push_back ( o ); 358std:: sort ( a . begin (), a . end ()); 359std:: sort ( b . begin (), b . end ()); 360require ( a == b , "an exchange changed the compact copy's places" ); 361const std::vector < uint64_t > after = offsets ; 362require (! exchange_cache_complement ( offsets , 3 , 0 ), "an `in` the copy does not hold was accepted" ); 363require (! exchange_cache_complement ( offsets , 0 , 2 ), "an `out` the copy holds already was accepted" ); 364require (! exchange_cache_complement ( offsets , 0 , 0 ), "a self-exchange was accepted" ); 365require (! exchange_cache_complement ( offsets , 0 , 6 ), "an out-of-range `out` was accepted" ); 366require ( offsets == after , "a refused exchange changed the offsets" ); 367// and back: the copy is again what the plan makes for the original placement 368require ( exchange_cache_complement ( offsets , 5 , 3 ), "the reverse exchange was refused" ); 369require ( offsets == before , "exchanging back did not restore the plan" ); 370} 371 372// #4051: the wait for the RAM an arena needs (an engine still exiting gives it back a few seconds late) 373void test_wait_for_ram () { 374using namespace strata::core:: detail ; 375constexpr uint64_t GiB = 1ull << 30 ; 376double waited = -1.0 ; 377uint64_t avail = 0 ; 378int slept = 0 , noted = 0 ; 379const auto sleeper = [ & ](double) { ++ slept ; }; 380const auto note = [ & ](double, uint64_t) { ++ noted ; }; 381// enough at once: no wait, no sleep, no note 382require ( wait_for_ram ( 40 * GiB , 20.0 , 0.25 , 5.0 , [](uint64_t & a ) { a = 50 * GiB ; return true; }, sleeper , note , waited , avail ) && 383waited == 0.0 && slept == 0 && noted == 0 && avail == 50 * GiB , "RAM that was there was waited for" ); 384// 10 GiB free for a 40 GiB arena, the exiting engine gives its 50 GiB back after 3 s (12 steps of 0.25 s) 385int reads = 0 ; 386slept = noted = 0 ; 387require ( wait_for_ram ( 40 * GiB , 20.0 , 0.25 , 5.0 , [ & ](uint64_t & a ) { a = ++ reads > 12 ? 60 * GiB : 10 * GiB ; return true; }, 388sleeper , note , waited , avail ) && waited == 3.0 && slept == 12 && noted == 1 && avail == 60 * GiB , 389"RAM that came back after 3 s was not waited for" ); 390// it never comes: the wait ends at the limit and says so (the load then goes on and warns) 391slept = noted = 0 ; 392require (! wait_for_ram ( 40 * GiB , 20.0 , 0.25 , 5.0 , [](uint64_t & a ) { a = 10 * GiB ; return true; }, sleeper , note , waited , avail ) && 393waited == 20.0 && slept == 80 && noted == 4 && avail == 10 * GiB , "the wait did not end at its limit" ); 394// no wait asked for: one reading 395slept = 0 ; 396require (! wait_for_ram ( 40 * GiB , 0.0 , 0.25 , 5.0 , [](uint64_t & a ) { a = 10 * GiB ; return true; }, sleeper , note , waited , avail ) && 397waited == 0.0 && slept == 0 , "a zero wait slept" ); 398// the RAM cannot be read: the wait ends at once 399slept = 0 ; 400require (! wait_for_ram ( 40 * GiB , 20.0 , 0.25 , 5.0 , [](uint64_t & ) { return false; }, sleeper , note , waited , avail ) && slept == 0 , 401"an unreadable RAM was waited for" ); 402} 403 404// #1250: when the page-locked complement is registered in steps, and what a meminfo reading says 405void test_pin_pacing () { 406using namespace strata::core:: detail ; 407constexpr uint64_t GiB = 1ull << 30 ; 408// the reporter's reading: 54 GiB available but 16 GiB really free for a 26 GiB complement 409require ( pin_depends_on_reclaim ( 16 * GiB , 26 * GiB , 4 * GiB ), "a complement bigger than the free pages was not flagged" ); 410require (! pin_depends_on_reclaim ( 40 * GiB , 26 * GiB , 4 * GiB ), "a complement the free pages cover was flagged" ); 411require ( pin_depends_on_reclaim ( 30 * GiB , 26 * GiB , 4 * GiB ) == false, "exactly free = bytes + reserve is enough" ); 412require (! pin_depends_on_reclaim ( 0 , 0 , 4 * GiB ), "no complement, nothing to pin" ); 413require ( pin_step_fits ( 6 * GiB , 1 * GiB , 4 * GiB ) && ! pin_step_fits ( 4 * GiB , 1 * GiB , 4 * GiB ), 414"a step must leave the reserve" ); 415const std::string info = 416"MemTotal: 65011484 kB\nMemFree: 1284096 kB\nMemAvailable: 33463296 kB\nCached: 54507520 kB\n" ; 417require ( meminfo_bytes ( info , "MemFree" ) == 1284096ull * 1024 , "MemFree was misread" ); 418require ( meminfo_bytes ( info , "MemAvailable" ) == 33463296ull * 1024 , "MemAvailable was misread" ); 419require ( meminfo_bytes ( info , "Mem" ) == 0 && meminfo_bytes ( info , "Shmem" ) == 0 , "an absent key was not 0" ); 420// 0.1.42 (dual-socket report): node 1 full of another model's cache, node 0 empty of it 421require ( numa_pin_free ( 100 * GiB , {}) == 100 * GiB && numa_pin_free ( 100 * GiB , { 100 * GiB }) == 100 * GiB , 422"a single-node host's figure changed" ); 423require ( numa_pin_free ( 90 * GiB , { 88 * GiB , 2 * GiB }) == 4 * GiB , "an emptied node did not lower the figure" ); 424require ( numa_pin_free ( 40 * GiB , { 20 * GiB , 20 * GiB }) == 40 * GiB , "balanced nodes changed the figure" ); 425require ( numa_pin_free ( 10 * GiB , { 50 * GiB , 50 * GiB }) == 10 * GiB , "the total must still bound it" ); 426require ( pin_depends_on_reclaim ( numa_pin_free ( 90 * GiB , { 88 * GiB , 2 * GiB }), 26 * GiB , 4 * GiB ), 427"an unbalanced two-node host was not paced" ); 428require ( page_cache_hint ( info ). find ( "drop_caches" ) != std::string:: npos && 429page_cache_hint ( info ). find ( "STRATA_DROP_CACHE_ON_EXIT" ) != std::string:: npos && 430page_cache_hint ( info ). find ( "52.0 GiB" ) != std::string:: npos , "the cache hint is incomplete" ); 431require ( page_cache_hint ( "MemFree: 100 kB\nCached: 1000 kB\n" ). empty (), "a small cache was reported" ); 432} 433 434// NUMA split: per-slice byte ranges of one complement slot (page rule: a page holding a slice-0 byte is slice 0's) 435void test_numa_slice_ranges () { 436using strata::core::detail:: numa_slice_ranges ; 437constexpr uint64_t P = 4096 ; 438const uint64_t roles [ 3 ] = { 870400 , 870400 , 921600 }; 439const uint64_t slot_bytes = roles [ 0 ] + roles [ 1 ] + roles [ 2 ]; 440const uint64_t offs [ 3 ] = { 0 , 2662400ull * 7 , P * 3 + 100 }; 441for (uint64_t off : offs ) { 442const auto r0 = numa_slice_ranges ( off , roles , 0 ), r1 = numa_slice_ranges ( off , roles , 1 ); 443for (int r = 0 ; r < 3 ; ++ r ) { 444require ( r0 [ r ]. first % P == 0 && r0 [ r ]. second % P == 0 && r1 [ r ]. first % P == 0 && r1 [ r ]. second % P == 0 , 445"a slice range boundary was not page aligned" ); 446require ( r0 [ r ]. first <= r0 [ r ]. second && r1 [ r ]. first <= r1 [ r ]. second , "a slice range was inverted" ); 447} 448// every page fully inside the slot is in exactly one of the six ranges (and none is in two) 449for (uint64_t pg = ( off + P - 1 ) / P * P ; pg + P <= off + slot_bytes ; pg += P ) { 450int hits = 0 ; 451for (int r = 0 ; r < 3 ; ++ r ) { 452hits += ( pg >= r0 [ r ]. first && pg + P <= r0 [ r ]. second ); 453hits += ( pg >= r1 [ r ]. first && pg + P <= r1 [ r ]. second ); 454} 455require ( hits == 1 , "a page was in " + std:: to_string ( hits ) + " slice ranges, not exactly one" ); 456} 457} 458// (b) no slice-1 range intersects a slice-0 range, within a slot and across two consecutive slots 459const auto meet = []( const std::pair < uint64_t, uint64_t >& x , const std::pair < uint64_t, uint64_t >& y ) { 460return x . first < y . second && y . first < x . second ; 461}; 462for (uint64_t off : offs ) { 463const auto s0 = numa_slice_ranges ( off , roles , 0 ), s1 = numa_slice_ranges ( off , roles , 1 ); 464const auto n0 = numa_slice_ranges ( off + slot_bytes , roles , 0 ), n1 = numa_slice_ranges ( off + slot_bytes , roles , 1 ); 465for (int i = 0 ; i < 3 ; ++ i ) 466for (int j = 0 ; j < 3 ; ++ j ) { 467require (! meet ( s0 [ i ], s1 [ j ]), "a slice-1 range intersects a slice-0 range of the same slot" ); 468require (! meet ( s1 [ i ], n0 [ j ]) && ! meet ( n1 [ i ], s0 [ j ]), 469"a slice-1 range intersects a slice-0 range of the neighbouring slot" ); 470} 471} 472const auto a = numa_slice_ranges ( 0 , roles , 0 ), b = numa_slice_ranges ( 0 , roles , 1 ); 473require ( a [ 0 ]. first <= 435199 && 435199 < a [ 0 ]. second , "last slice-0 gate byte not in slice 0's gate range" ); 474require ( b [ 0 ]. first <= 435200 + P && 435200 + P < b [ 0 ]. second , "a slice-1 gate byte not in slice 1's gate range" ); 475require ( a [ 1 ]. first <= 870400 && 870400 < a [ 1 ]. second , "the gate/up boundary page was not slice 0's up range" ); 476require (!( b [ 0 ]. first <= 870400 && 870400 < b [ 0 ]. second ), "the gate/up boundary page was in slice 1's gate range" ); 477} 478 479void test_resident_memory_budget () { 480using strata::core::detail:: clamp_resident_budget ; 481constexpr uint64_t GiB = 1ull << 30 , margin = 256ull << 20 , headroom = 4 * GiB ; 482constexpr uint64_t unlimited = std::numeric_limits < uint64_t > :: max (); 483// #730: RAM can hold the requested cache, but Windows cannot commit it. 484require ( clamp_resident_budget ( 66 * GiB , 69 * GiB , 47 * GiB , headroom ) == 43 * GiB - margin , 485"a RAM budget exceeded available commit capacity" ); 486require ( clamp_resident_budget ( 66 * GiB , 47 * GiB , 69 * GiB , headroom ) == 43 * GiB - margin , 487"a RAM budget exceeded available physical memory" ); 488require ( clamp_resident_budget ( 40 * GiB , 69 * GiB , 47 * GiB , headroom ) == 40 * GiB , 489"a smaller explicit budget was changed" ); 490require ( clamp_resident_budget ( unlimited , 69 * GiB , 47 * GiB , headroom ) == 43 * GiB - margin , 491"the what-fits budget ignored commit capacity" ); 492require ( clamp_resident_budget ( 66 * GiB , 47 * GiB , unlimited , headroom ) == 43 * GiB - margin , 493"a platform without a commit reading lost its RAM limit" ); 494require ( clamp_resident_budget ( 43 * GiB , 69 * GiB , 47 * GiB , headroom ) == 43 * GiB , 495"a budget that exactly fits received the clamping margin" ); 496require ( clamp_resident_budget ( 66 * GiB , 69 * GiB , 0 , headroom ) == 0 , 497"exhausted commit capacity was treated as unlimited" ); 498require ( clamp_resident_budget ( 66 * GiB , 0 , 69 * GiB , headroom ) == 0 , 499"exhausted RAM produced a positive budget" ); 500require ( clamp_resident_budget ( 66 * GiB , 69 * GiB , headroom - 1 , headroom ) == 0 && 501clamp_resident_budget ( 66 * GiB , 69 * GiB , headroom , headroom ) == 0 , 502"subtracting headroom underflowed" ); 503require ( clamp_resident_budget ( 66 * GiB , 69 * GiB , headroom + margin - 1 , headroom ) == 0 && 504clamp_resident_budget ( 66 * GiB , 69 * GiB , headroom + margin , headroom ) == 0 , 505"subtracting the clamping margin underflowed" ); 506} 507 508void test_cgroup_memory_budget () { 509using namespace strata::core:: detail ; 510constexpr uint64_t GiB = 1ull << 30 ; 511uint64_t bytes = 0 ; 512 513// the kernel keeps min(half the clean cache, limit / 64) of it: 56 GiB / 64 = 0.875 GiB here 514const uint64_t keep56 = 56 * GiB / 64 ; 515CgroupMemoryStat clean_cache { 40 * GiB , 32 * GiB , 0 , 0 , true}; 516require ( cgroup_available_bytes ( 56 * GiB , clean_cache , bytes ), "valid cgroup memory.stat was rejected" ); 517require ( bytes == 48 * GiB - keep56 , "clean inactive file cache was not credited against the cgroup cap" ); 518 519CgroupMemoryStat dirty_cache { 40 * GiB , 32 * GiB , 4 * GiB , 2 * GiB , true}; 520require ( cgroup_available_bytes ( 56 * GiB , dirty_cache , bytes ), "valid dirty-cache counters were rejected" ); 521require ( bytes == 42 * GiB - keep56 , "dirty/writeback pages were incorrectly counted as reclaimable" ); 522 523CgroupMemoryStat oversized_inactive { 10 * GiB , 20 * GiB , 0 , 0 , true}; 524require ( cgroup_available_bytes ( 12 * GiB , oversized_inactive , bytes ), 525"valid oversized inactive-file counter was rejected" ); 526require ( bytes == 12 * GiB - 12 * GiB / 64 , "inactive-file accounting exceeded charged current usage" ); 527 528// 149: the ACTIVE page cache is reclaimable too (memory.stat "file" = active + inactive), as MemAvailable counts 529// it: a 32 GiB group whose own reads left 26 GiB of cache, 25 GiB of it active, and 4.5 GiB of anonymous memory 530CgroupMemoryStat active_cache { 30 * GiB + GiB / 2 , 1 * GiB , 0 , 0 , true, 26 * GiB }; 531require ( cgroup_available_bytes ( 32 * GiB , active_cache , bytes ), "a memory.stat with file was rejected" ); 532require ( bytes == 32 * GiB - ( 4 * GiB + GiB / 2 ) - 32 * GiB / 64 , "the active page cache was not credited" ); 533// without "file" (an old kernel's stat): inactive_file alone, as before 534CgroupMemoryStat no_file { 30 * GiB + GiB / 2 , 1 * GiB , 0 , 0 , true}; 535require ( cgroup_available_bytes ( 32 * GiB , no_file , bytes ) && bytes == 32 * GiB - ( 29 * GiB + GiB / 2 ) - GiB / 2 , 536"a stat without file did not fall back to inactive_file" ); 537// dirty and writeback pages of the active cache stay charged; a small cache keeps half of itself 538CgroupMemoryStat dirty_active { 20 * GiB , 1 * GiB , 2 * GiB , 1 * GiB , true, 10 * GiB }; 539require ( cgroup_available_bytes ( 32 * GiB , dirty_active , bytes ) && bytes == 32 * GiB - ( 20 * GiB - 7 * GiB ) - GiB / 2 , 540"dirty / writeback pages of the active cache were counted as reclaimable" ); 541CgroupMemoryStat tiny_cache { 8 * GiB , 0 , 0 , 0 , true, 256ull << 20 }; 542require ( cgroup_available_bytes ( 32 * GiB , tiny_cache , bytes ) && bytes == 32 * GiB - 8 * GiB + ( 128ull << 20 ), 543"a small clean cache was not kept at half" ); 544// "file" larger than what is charged (a racy read) is bounded by memory.current 545CgroupMemoryStat racy { 4 * GiB , 1 * GiB , 0 , 0 , true, 9 * GiB }; 546require ( cgroup_available_bytes ( 32 * GiB , racy , bytes ) && bytes == 32 * GiB - GiB / 2 , 547"a file counter above memory.current escaped the bound" ); 548 549const uint64_t max = std::numeric_limits < uint64_t > :: max (); 550CgroupMemoryStat large_counters { max , max , max - 1 , max , true}; 551require ( cgroup_available_bytes ( max , large_counters , bytes ), 552"large memory.stat counters were rejected" ); 553require ( bytes == 0 , "dirty/writeback subtraction overflowed or escaped the usage cap" ); 554 555CgroupMemoryStat usage_over_limit { 60 * GiB , 0 , 0 , 0 , true}; 556require ( cgroup_available_bytes ( 56 * GiB , usage_over_limit , bytes ), 557"valid over-limit memory counters were rejected" ); 558require ( bytes == 0 , "over-limit cgroup usage produced a positive budget" ); 559 560CgroupMemoryStat missing_stat {}; 561bytes = 123 ; 562require (! cgroup_available_bytes ( 56 * GiB , missing_stat , bytes ) && bytes == 0 , 563"missing memory.stat counters did not fail closed" ); 564} 565 566void set_env ( const char * name , const char * value ) { 567#if defined( _WIN32 ) 568_putenv_s ( name , value != nullptr ? value : "" ); 569#else 570if ( value != nullptr ) setenv ( name , value , 1 ); 571else unsetenv ( name ); 572#endif 573} 574 575// #286: the unbuffered file tier (Windows: FILE_FLAG_NO_BUFFERING; Linux: O_DIRECT + io_submit) hands out the same 576// bytes as the mapping - one blob at a time, a batch of adjacent experts (merged into one request), and copy_blob - and 577// never falls back to the mapping. The pack lives in the working directory, not the temp directory: a tmpfs /tmp has 578// no O_DIRECT, and there the test says so and skips. 579void test_unbuffered_reads () { 580using namespace strata:: core ; 581using namespace strata::kernels:: cpu ; 582constexpr int64_t layers = 2 ; 583constexpr int64_t experts = 3 ; 584const uint64_t layer_bytes = (uint64_t) experts * BLOB ; 585const uint64_t total = (uint64_t) layers * layer_bytes ; 586TempDirectory dir (fs:: current_path ()); 587std::string err ; 588require ( expert_layout_load ( dir . path . string (), layers , experts , err ), "could not load canonical layout: " + err ); 589std::vector < uint8_t > bytes ((size_t) total ); 590uint64_t x = 0x9E3779B97F4A7C15ull ; 591for (uint8_t & b : bytes ) { 592x = x * 6364136223846793005ull + 1442695040888963407ull ; 593b = (uint8_t) ( x >> 56 ); 594} 595{ 596std::ofstream out ( dir . path / "experts.bin" , std::ios:: binary | std::ios:: trunc ); 597out . write (( const char * ) bytes . data (), (std::streamsize) bytes . size ()); 598require ((bool) out , "could not write the synthetic experts.bin" ); 599} 600FileExpertSource source ; 601require ( source . open ( dir . path . string (), layers , experts , err ), "could not map the synthetic pack: " + err ); 602set_env ( "STRATA_UNBUFFERED_LOAD" , "1" ); 603std::string why ; 604const bool unbuffered = source . set_unbuffered ( 0 , why ); 605set_env ( "STRATA_UNBUFFERED_LOAD" , nullptr ); 606#if defined( _WIN32 ) || defined( __linux__ ) 607if (! unbuffered ) { 608std:: cout << "file_expert_source_test: unbuffered reads skipped (" << why << ")\n" ; 609source . close (); 610return ; 611} 612#else 613require (! unbuffered , "unbuffered reads on a platform without them" ); 614source . close (); 615return ; 616#endif 617require ( source . unbuffered (), "set_unbuffered succeeded but the source is not unbuffered" ); 618const int64_t batch [ experts ] = { 0 , 1 , 2 }; 619source . prefetch ( 1 , batch , experts ); // layer 1 in one batch: three adjacent blobs 620for (int64_t l = 0 ; l < layers ; ++ l ) 621for (int64_t e = 0 ; e < experts ; ++ e ) { 622const uint8_t * b = source . blob ( l , e ); 623require ( b != nullptr , "an unbuffered blob lookup failed" ); 624require (std:: memcmp ( b , bytes . data () + (size_t) ( l * (int64_t) layer_bytes + e * (int64_t) BLOB ), 625(size_t) BLOB ) == 0 , 626"an unbuffered blob differs from the file" ); 627} 628std::vector < uint8_t > copy ((size_t) BLOB ); 629require ( source . copy_blob ( 0 , 2 , copy . data ()) && 630std:: memcmp ( copy . data (), bytes . data () + (size_t) ( 2 * BLOB ), (size_t) BLOB ) == 0 , 631"an unbuffered copy_blob differs from the file" ); 632require ( source . direct_fallbacks () == 0 , "an unbuffered read fell back to the mapping" ); 633source . close (); 634} 635 636// #633: the host RAM probe with fake /proc and cgroup trees: v2 (a limit, "max", a missing or malformed limit), v1 637// (a limit, unlimited), and no cgroup line at all. Elsewhere than Linux it reads the machine's RAM. 638void test_host_memory () { 639using namespace strata::core:: detail ; 640constexpr uint64_t GiB = 1ull << 30 ; 641HostMemory m ; 642#if defined( __linux__ ) 643TempDirectory t ; 644auto put = [ & ]( const fs::path & rel , const std::string & text ) { 645fs:: create_directories (( t . path / rel ). parent_path ()); 646std:: ofstream ( t . path / rel ) << text ; 647}; 648put ( "meminfo" , "MemTotal: 134217728 kB\nMemAvailable: 104857600 kB\n" ); // 100 GiB available 649const std::string mi = ( t . path / "meminfo" ). string (), cg = ( t . path / "cgroup" ). string (), 650root = ( t . path / "fs" ). string (); 651auto probe = [ & ]( const std::string & self ) { 652put ( "cgroup" , self ); 653m . commit = 0 ; 654const bool ok = host_available_memory ( m , mi , cg , root ); 655require ( m . commit == ~ 0ull , "Linux memory probe retained a commit limit" ); 656return ok ; 657}; 658// no cgroup line at all: MemAvailable alone (before #633: "cannot determine") 659require ( probe ( "" ) && m . available == 100 * GiB && m . cgroup_limit == ~ 0ull , "no cgroup: MemAvailable alone" ); 660// v2, a 48 GiB limit with 16 GiB charged, 4 GiB of it clean cache 661put ( "fs/cgroup.controllers" , "memory\n" ); 662put ( "fs/box/memory.max" , std:: to_string ( 48 * GiB ) + "\n" ); 663put ( "fs/box/memory.current" , std:: to_string ( 16 * GiB ) + "\n" ); 664put ( "fs/box/memory.stat" , "inactive_file " + std:: to_string ( 4 * GiB ) + "\nfile_dirty 0\nfile_writeback 0\n" ); 665require ( probe ( "0::/box\n" ) && m . available == 36 * GiB - 48 * GiB / 64 && m . cgroup_limit == 48 * GiB , "v2 limit" ); 666// 149: the active page cache counts too: 12 GiB of cache (8 active, 4 inactive), 4 GiB anonymous 667put ( "fs/box/memory.stat" , "file " + std:: to_string ( 12 * GiB ) + "\nactive_file " + std:: to_string ( 8 * GiB ) + 668"\ninactive_file " + std:: to_string ( 4 * GiB ) + "\nfile_dirty 0\nfile_writeback 0\n" ); 669require ( probe ( "0::/box\n" ) && m . available == 44 * GiB - 48 * GiB / 64 && m . cgroup_limit == 48 * GiB , 670"v2 limit, the active page cache credited" ); 671// 3 GiB of shmem (a memfd, the KV pool) is in "file" but on the anon lists: it cannot be reclaimed without swap, and an 672// allocation up to a figure that counted it was OOM-killed in a 24 GiB scope. Under a container's limit (below 673// MemTotal) it is not credited; nor are locked pages (the unevictable list) 674put ( "fs/box/memory.stat" , "file " + std:: to_string ( 15 * GiB ) + "\nshmem " + std:: to_string ( 3 * GiB ) + "\nactive_file " + 675std:: to_string ( 8 * GiB ) + "\ninactive_file " + std:: to_string ( 4 * GiB ) + "\nunevictable " + 676std:: to_string ( 2 * GiB ) + "\nfile_dirty 0\nfile_writeback 0\n" ); 677put ( "fs/box/memory.current" , std:: to_string ( 19 * GiB ) + "\n" ); 678require ( probe ( "0::/box\n" ) && m . available == 41 * GiB - 48 * GiB / 64 , "container: shmem and locked pages are not credited" ); 679set_env ( "STRATA_MEM_CREDIT_INACTIVE" , "1" ); 680require ( probe ( "0::/box\n" ) && m . available == 44 * GiB - 48 * GiB / 64 , "STRATA_MEM_CREDIT_INACTIVE=1: the earlier file credit" ); 681set_env ( "STRATA_MEM_CREDIT_INACTIVE" , nullptr ); 682put ( "meminfo" , "MemTotal: 41943040 kB\nMemAvailable: 104857600 kB\n" ); // 40 GiB: the 48 GiB limit is not a container's 683require ( probe ( "0::/box\n" ) && m . available == 44 * GiB - 48 * GiB / 64 , "a limit above MemTotal: the earlier file credit" ); 684put ( "meminfo" , "MemTotal: 134217728 kB\nMemAvailable: 104857600 kB\n" ); 685put ( "fs/box/memory.current" , std:: to_string ( 16 * GiB ) + "\n" ); 686put ( "fs/box/memory.stat" , "inactive_file " + std:: to_string ( 4 * GiB ) + "\nfile_dirty 0\nfile_writeback 0\n" ); 687put ( "fs/box/memory.max" , "max\n" ); 688require ( probe ( "0::/box\n" ) && m . available == 100 * GiB && m . cgroup_limit == ~ 0ull , "v2 max" ); 689put ( "fs/box/memory.max" , "48G\n" ); 690require (! probe ( "0::/box\n" ), "v2 malformed limit accepted" ); 691fs:: remove ( t . path / "fs/box/memory.max" ); 692require (! probe ( "0::/box\n" ), "v2 group without memory.max accepted" ); 693// v1: the memory controller's group (a hybrid line list), a 32 GiB limit with 10 GiB used 694put ( "fs/memory/docker/abc/memory.limit_in_bytes" , std:: to_string ( 32 * GiB ) + "\n" ); 695put ( "fs/memory/docker/abc/memory.usage_in_bytes" , std:: to_string ( 10 * GiB ) + "\n" ); 696put ( "fs/memory/memory.limit_in_bytes" , "9223372036854771712\n" ); // the root: unlimited 697put ( "fs/memory/memory.usage_in_bytes" , std:: to_string ( 50 * GiB ) + "\n" ); 698require ( probe ( "12:cpu,cpuacct:/docker/abc\n4:memory:/docker/abc\n1:name=systemd:/docker/abc\n" ) && 699m . available == 22 * GiB && m . cgroup_limit == 32 * GiB , "v1 limit" ); 700put ( "fs/memory/docker/abc/memory.limit_in_bytes" , "9223372036854771712\n" ); 701require ( probe ( "4:memory:/docker/abc\n" ) && m . available == 100 * GiB && m . cgroup_limit == ~ 0ull , "v1 unlimited" ); 702// a v1 group not visible here (another namespace): MemAvailable alone 703require ( probe ( "4:memory:/elsewhere\n" ) && m . available == 100 * GiB , "v1 group not mounted" ); 704#else 705require ( host_available_memory ( m ) && m . available > 0 && m . cgroup_limit == ~ 0ull , "this PC's RAM" ); 706#if defined( _WIN32 ) 707require ( m . commit != ~ 0ull , "Windows memory probe did not report commit capacity" ); 708#else 709require ( m . commit == ~ 0ull , "a platform without commit accounting imposed a limit" ); 710#endif 711(void) GiB ; 712#endif 713} 714 715// STRATA_IO_PREFETCH: the experts.bin file tier hands out the same bytes with the Linux I/O path on (whole-blob 716// preads, the read-ahead workers, the mapping for cached blobs), however the reads and the predictions interleave. 717void test_io_prefetch () { 718using namespace strata:: core ; 719using namespace strata::kernels:: cpu ; 720constexpr int64_t layers = 3 ; 721constexpr int64_t experts = 8 ; 722const uint64_t layer_bytes = (uint64_t) experts * BLOB ; 723const uint64_t total = (uint64_t) layers * layer_bytes ; 724TempDirectory dir (fs:: current_path ()); 725std::string err ; 726require ( expert_layout_load ( dir . path . string (), layers , experts , err ), "could not load canonical layout: " + err ); 727std::vector < uint8_t > bytes ((size_t) total ); 728uint64_t x = 0xD1B54A32D192ED03ull ; 729for (uint8_t & b : bytes ) { 730x = x * 6364136223846793005ull + 1442695040888963407ull ; 731b = (uint8_t) ( x >> 56 ); 732} 733{ 734std::ofstream out ( dir . path / "experts.bin" , std::ios:: binary | std::ios:: trunc ); 735out . write (( const char * ) bytes . data (), (std::streamsize) bytes . size ()); 736require ((bool) out , "could not write the synthetic experts.bin" ); 737} 738FileExpertSource source ; 739require ( source . open ( dir . path . string (), layers , experts , err ), "could not map the synthetic pack: " + err ); 740source . set_io_prefetch (true, 3 ); 741#if defined( __linux__ ) 742require ( source . io_prefetch () && source . warms (), "io prefetch did not turn on" ); 743// predictions for the next layers, some of them wrong or repeated, while the layers are read 744for (int round = 0 ; round < 20 ; ++ round ) 745for (int64_t l = 0 ; l < layers ; ++ l ) { 746const int64_t pred [ 5 ] = {( round + l ) % experts , ( round * 3 + 1 ) % experts , ( round + 2 * l + 5 ) % experts , 747( round + l ) % experts , 7 }; 748source . warm (( l + 1 ) % layers , pred , 5 ); 749source . begin_layer ( l , nullptr , 0 ); 750std::vector < int64_t > want = {( round + l ) % experts , ( round * 5 + 2 ) % experts , ( l * 3 + round ) % experts }; 751source . prefetch ( l , want . data (), (int64_t) want . size ()); 752for (int64_t e : want ) { 753const uint8_t * b = source . blob ( l , e ); 754require ( b != nullptr , "an io-prefetch blob lookup failed" ); 755require (std:: memcmp ( b , bytes . data () + (size_t) ( l * (int64_t) layer_bytes + e * (int64_t) BLOB ), (size_t) BLOB ) == 0 , 756"an io-prefetch blob differs from the file" ); 757const uint8_t * s2 = source . blob_stable ( l , e ); 758require ( s2 != nullptr && std:: memcmp ( s2 , bytes . data () + (size_t) ( l * (int64_t) layer_bytes + e * (int64_t) BLOB ), (size_t) BLOB ) == 0 , 759"a blob_stable pointer differs from the file" ); 760} 761} 762std::vector < uint8_t > copy ((size_t) BLOB ); 763require ( source . copy_blob ( 1 , 6 , copy . data ()) && 764std:: memcmp ( copy . data (), bytes . data () + (size_t) ( layer_bytes + 6 * BLOB ), (size_t) BLOB ) == 0 , 765"an io-prefetch copy_blob differs from the file" ); 766const FileExpertSource::IoCounters c = source . io_counters (); 767require ( c . cached_bytes + c . uncached_bytes > 0 , "no residency counted" ); 768std:: cout << "io prefetch: " << c . pf_read_blobs << " read ahead, " << c . pf_used << " used, " << c . pf_resident_skips 769<< " skipped, pread " << c . pread_bytes << " B: bytes PASS\n" ; 770#else 771require (! source . io_prefetch (), "io prefetch on a platform without it" ); 772#endif 773source . close (); 774} 775 776void test_rotating_source (bool rotate , bool pin ) { 777using namespace strata:: core ; 778using namespace strata::kernels:: cpu ; 779#if defined( _WIN32 ) 780_putenv_s ( "STRATA_EXCHANGE_ROTATE" , rotate ? "1" : "0" ); 781#else 782setenv ( "STRATA_EXCHANGE_ROTATE" , rotate ? "1" : "0" , 1 ); 783#endif 784TempDirectory dir ; 785std::string err ; 786require ( expert_layout_load ( dir . path . string (), 1 , 5 , err ), err ); 787const size_t bytes = (size_t) BLOB ; 788std::vector < std::vector < uint8_t >> truth ( 5 , std:: vector < uint8_t > ( bytes )); 789{ 790std::ofstream out ( dir . path / "experts.bin" , std::ios:: binary ); 791for (size_t e = 0 ; e < truth . size (); ++ e ) { 792for (size_t i = 0 ; i < bytes ; ++ i ) truth [ e ][ i ] = (uint8_t)(( i * 131u + e * 37u ) ^ ( i >> 8 )); 793out . write (( const char * ) truth [ e ]. data (), (std::streamsize) bytes ); 794} 795require ((bool) out , "fixture write failed" ); 796} 797FileExpertSource src ; 798ExpertCache cache ; 799require ( src . open ( dir . path . string (), 1 , 5 , err ), err ); 800require ( cache . open ( 2 , 1 , 5 , BLOB , err ), err ); 801for (int e : { 2 , 3 }) { 802const int slot = cache . admit ( 0 , e ); 803require ( slot >= 0 && cache . fill_slot_blocking ( slot , truth [ e ]. data (), err ), err ); 804} 805const std::vector < std::pair < int32_t, int32_t >> rank {{ 0 , 0 },{ 0 , 1 },{ 0 , 2 },{ 0 , 3 },{ 0 , 4 }}; 806// Keep expert 4 on the file tier to check that the fallback survives rotation. 807require ( src . pin_cache_complement ( cache , err , pin , {}, -1 , 0 , 2 * bytes , & rank ), err ); 808require ( src . reserve_exchanges ( 2 , err ), err ); 809require ( src . exchange_rotation () == ( rotate && pin ), "rotation activation/fallback wrong" ); 810require ( src . reserve_exchanges ( 1 , err ), "smaller capacity rejected" ); 811if ( rotate && pin ) require (! src . reserve_exchanges ( 3 , err ), "live arena growth accepted" ); 812int incoming [ 2 ] = { 0 , 1 }, outgoing [ 2 ] = { 2 , 3 }; 813std::vector < uint8_t > actual (bytes); 814double commit_ms = 0 ; 815for (int round = 0 ; round < 32 ; ++ round ) { 816const uint8_t * prior [ 2 ]{}; 817uint8_t * eviction [ 2 ]{}; 818for (int q = 0 ; q < 2 ; ++ q ) { 819prior [ q ] = src . blob ( 0 , incoming [ q ]); eviction [ q ] = src . exchange_buffer ( q ); 820require ( prior [ q ] && !std:: memcmp ( prior [ q ], truth [ incoming [ q ]]. data (), bytes ), "wrong incoming bytes" ); 821require ( cudaMemcpy ( eviction [ q ], cache . device_slot ( q ), bytes , cudaMemcpyDeviceToHost ) == cudaSuccess , 822"D2H eviction failed" ); 823require ( src . stage_exchange ( 0 , incoming [ q ], outgoing [ q ], q ), "stage rejected" ); 824require (!std:: memcmp ( src . blob ( 0 , outgoing [ q ]), truth [ outgoing [ q ]]. data (), bytes ), "staged override wrong" ); 825require ( cache . fill_slot_blocking ( q , prior [ q ], err ), err ); // H2D completed before commit 826} 827require (! src . stage_exchange ( 0 , incoming [ 0 ], outgoing [ 0 ], 0 ), "duplicate stage accepted" ); 828require (! src . stage_exchange ( 0 , incoming [ 0 ], 5 , 0 ), "invalid expert accepted" ); 829const auto t0 = std::chrono::steady_clock:: now (); 830require ( src . commit_exchanges () == 2 , "commit count wrong" ); 831commit_ms += std::chrono:: duration < double, std::milli > (std::chrono::steady_clock:: now () - t0 ). count (); 832require ( src . commit_exchanges () == 0 , "empty commit not empty" ); 833for (int q = 0 ; q < 2 ; ++ q ) { 834require (! src . has_resident ( 0 , incoming [ q ]) && src . has_resident ( 0 , outgoing [ q ]), "residency wrong" ); 835const uint8_t * held = src . blob ( 0 , outgoing [ q ]); 836require (!std:: memcmp ( held , truth [ outgoing [ q ]]. data (), bytes ), "evicted bytes corrupted" ); 837require ( held == (( rotate && pin ) ? eviction [ q ] : prior [ q ]), "wrong storage selected" ); 838if ( rotate && pin ) require ( src . exchange_buffer ( q ) == prior [ q ], "old input not recycled" ); 839require ( src . copy_blob ( 0 , outgoing [ q ], actual . data ()) && actual == truth [ outgoing [ q ]], "copy_blob mismatch" ); 840require (! src . transient ( 0 , outgoing [ q ]), "resident became transient" ); 841if ( pin ) { 842require ( src . pinned ( 0 , outgoing [ q ]) && src . device_alias ( 0 , outgoing [ q ]), "mapping lost" ); 843require ( cudaMemcpy ( actual . data (), src . device_alias ( 0 , outgoing [ q ]), bytes , cudaMemcpyDeviceToHost ) == cudaSuccess , 844"device alias read failed" ); 845require ( actual == truth [ outgoing [ q ]], "CUDA alias points at stale expert" ); 846} 847require ( cache . verify_slot ( q , truth [ incoming [ q ]]. data (), err ), err ); 848std:: swap ( incoming [ q ], outgoing [ q ]); 849} 850require (! src . has_resident ( 0 , 4 ) && src . copy_blob ( 0 , 4 , actual . data ()) && actual == truth [ 4 ], "file fallback changed" ); 851} 852require ( src . exchanges () == 64 , "exchange accounting wrong" ); 853require ( src . rotated_exchanges () == (( rotate && pin ) ? 64u : 0u ), "rotation accounting wrong" ); 854require ( src . avoided_exchange_copy_bytes () == (( rotate && pin ) ? 64 * bytes : 0u ), "avoided bytes wrong" ); 855std:: cout << "rotation integration: rotate=" << rotate << " pinned=" << pin 856<< " exchanges=64 commit_ms=" << commit_ms << " exact_bytes=PASS\n" ; 857src . close (); 858require (! src . exchange_rotation () && ! src . exchange_buffer ( 0 ), "close retained storage" ); 859require ( src . open ( dir . path . string (), 1 , 5 , err ), err ); 860require (! src . has_resident ( 0 , 0 ) && ! src . exchange_rotation (), "reopen retained residency" ); 861} 862 863// #2134: a Foresight slot copied from an expert's RAM place before an exchange rewrote that place is never served; the 864// experts an exchange moves get no copy between commit_copies and commit_flip; and a dropped slot that a window may 865// still read waits `depth` finished windows before it is refilled. 866void test_foresight_exchange (bool two_phase ) { 867using namespace strata:: core ; 868using namespace strata::kernels:: cpu ; 869#if defined( _WIN32 ) 870_putenv_s ( "STRATA_EXCHANGE_ROTATE" , "0" ); 871#else 872setenv ( "STRATA_EXCHANGE_ROTATE" , "0" , 1 ); 873#endif 874TempDirectory dir ; 875std::string err ; 876require ( expert_layout_load ( dir . path . string (), 1 , 5 , err ), err ); 877const size_t bytes = (size_t) BLOB ; 878std::vector < std::vector < uint8_t >> truth ( 5 , std:: vector < uint8_t > ( bytes )); 879{ 880std::ofstream out ( dir . path / "experts.bin" , std::ios:: binary ); 881for (size_t e = 0 ; e < truth . size (); ++ e ) { 882for (size_t i = 0 ; i < bytes ; ++ i ) truth [ e ][ i ] = (uint8_t) (( i * 131u + e * 37u ) ^ ( i >> 8 )); 883out . write (( const char * ) truth [ e ]. data (), (std::streamsize) bytes ); 884} 885require ((bool) out , "fixture write failed" ); 886} 887FileExpertSource src ; 888ExpertCache cache ; 889require ( src . open ( dir . path . string (), 1 , 5 , err ), err ); 890require ( cache . open ( 2 , 1 , 5 , BLOB , err ), err ); 891for (int e : { 2 , 3 }) { 892const int slot = cache . admit ( 0 , e ); 893require ( slot >= 0 && cache . fill_slot_blocking ( slot , truth [ e ]. data (), err ), err ); 894} 895const std::vector < std::pair < int32_t, int32_t >> rank {{ 0 , 0 },{ 0 , 1 },{ 0 , 2 },{ 0 , 3 },{ 0 , 4 }}; 896require ( src . pin_cache_complement ( cache , err , true, {}, -1 , 0 , 2 * bytes , & rank ), err ); 897require ( src . reserve_exchanges ( 1 , err ), err ); 898require ( src . pinned ( 0 , 0 ), "expert 0 is not in the pinned RAM copy" ); 899int dev = 0 ; 900cudaGetDevice ( & dev ); 901ForesightSwap fs ; 902require ( fs . init ( 1 , 1 , 5 , { 0 }, { dev }, err ), err ); // one slot, depth 4 903fs . verify = true; 904src . set_foresight_swap ( & fs ); 905unsigned long long ptr = 0 ; 906auto landed = [ & ](int32_t e ) { 907for (int i = 0 ; i < 5000 ; ++ i ) { 908if ( fs . take ( 0 , e , ptr )) return true; 909std::this_thread:: sleep_for (std::chrono:: milliseconds ( 1 )); 910} 911return false; 912}; 913fs . note_miss ( & src , 0 , 0 ); // expert 0 -> the slot, from its RAM place 914require ( landed ( 0 ) && fs . verify_ok == 1 , "the first copy did not land" ); 915// the adaptive tier swaps expert 0 into cache slot 0 and evicts expert 2 into the exchange buffer 916uint8_t * eviction = src . exchange_buffer ( 0 ); 917const uint8_t * prior = src . blob ( 0 , 0 ); 918require ( cudaMemcpy ( eviction , cache . device_slot ( 0 ), bytes , cudaMemcpyDeviceToHost ) == cudaSuccess , "D2H eviction failed" ); 919require ( src . stage_exchange ( 0 , 0 , 2 , 0 ), "stage rejected" ); 920require ( cache . fill_slot_blocking ( 0 , prior , err ), err ); 921if ( two_phase ) { 922src . commit_copies (); // 0's place holds 2's bytes from here on 923const uint64_t c0 = fs . copies ; 924fs . completed . fetch_add ( 8 ); 925fs . note_miss ( & src , 0 , 2 ); 926fs . predict ( & src , 0 , 0 ); 927require ( fs . copies == c0 , "an expert was copied while its exchange was half done" ); 928require (! fs . take ( 0 , 0 , ptr ), "a slot dropped by commit_copies was served" ); 929require ( src . commit_flip () == 1 , "flip count wrong" ); 930} else { 931require ( src . commit_exchanges () == 1 , "commit count wrong" ); 932} 933require ( fs . dropped == 1 , "the exchange did not drop the slot" ); 934require (! fs . take ( 0 , 0 , ptr ), "a slot copied before the exchange was served" ); 935if (! two_phase ) { // the slot was served at window 0: no refill before window 0 + depth 936const uint64_t c0 = fs . copies , b0 = fs . busy ; 937fs . note_miss ( & src , 0 , 2 ); 938require ( fs . copies == c0 && fs . busy == b0 + 1 , "a dropped slot was refilled while a window may still read it" ); 939fs . completed . fetch_add ( 4 ); 940} 941require ( src . has_resident ( 0 , 2 ) && ! src . has_resident ( 0 , 0 ), "residency wrong" ); 942fs . note_miss ( & src , 0 , 2 ); // expert 2, now at 0's old place 943require ( landed ( 2 ), "the copy after the exchange did not land" ); 944std::vector < uint8_t > back (bytes); 945require ( cudaMemcpy ( back . data (), ( const void * ) ptr , bytes , cudaMemcpyDeviceToHost ) == cudaSuccess && back == truth [ 2 ], 946"the slot does not hold expert 2's bytes" ); 947require ( fs . verify_bad == 0 && fs . verify_ok == 2 , "verify counted a bad slot" ); 948src . set_foresight_swap ( nullptr ); 949std:: cout << "foresight exchange (" << ( two_phase ? "commit_copies + commit_flip" : "commit_exchanges" ) 950<< "): dropped " << fs . dropped << ", " << fs . report () << ": PASS\n" ; 951src . close (); 952} 953 954// STRATA_RAM_ADAPT: an expert read from the file takes the RAM place of another of its layer. 955void test_replace_resident (bool pin ) { 956using namespace strata:: core ; 957using namespace strata::kernels:: cpu ; 958#if defined( _WIN32 ) 959_putenv_s ( "STRATA_EXCHANGE_ROTATE" , "0" ); 960#else 961setenv ( "STRATA_EXCHANGE_ROTATE" , "0" , 1 ); 962#endif 963TempDirectory dir ; 964std::string err ; 965require ( expert_layout_load ( dir . path . string (), 1 , 5 , err ), err ); 966const size_t bytes = (size_t) BLOB ; 967std::vector < std::vector < uint8_t >> truth ( 5 , std:: vector < uint8_t > ( bytes )); 968{ 969std::ofstream out ( dir . path / "experts.bin" , std::ios:: binary ); 970for (size_t e = 0 ; e < truth . size (); ++ e ) { 971for (size_t i = 0 ; i < bytes ; ++ i ) truth [ e ][ i ] = (uint8_t)(( i * 131u + e * 37u ) ^ ( i >> 8 )); 972out . write (( const char * ) truth [ e ]. data (), (std::streamsize) bytes ); 973} 974require ((bool) out , "fixture write failed" ); 975} 976FileExpertSource src ; 977ExpertCache cache ; 978require ( src . open ( dir . path . string (), 1 , 5 , err ), err ); 979require ( cache . open ( 2 , 1 , 5 , BLOB , err ), err ); 980for (int e : { 2 , 3 }) { 981const int slot = cache . admit ( 0 , e ); 982require ( slot >= 0 && cache . fill_slot_blocking ( slot , truth [ e ]. data (), err ), err ); 983} 984const std::vector < std::pair < int32_t, int32_t >> rank {{ 0 , 0 },{ 0 , 1 },{ 0 , 2 },{ 0 , 3 },{ 0 , 4 }}; 985// experts 2 and 3 are in VRAM, the RAM budget holds 0 and 1, expert 4 stays on the file tier 986require ( src . pin_cache_complement ( cache , err , pin , {}, -1 , 0 , 2 * bytes , & rank ), err ); 987require ( src . reserve_exchanges ( 1 , err ), err ); 988require ( src . has_resident ( 0 , 0 ) && src . has_resident ( 0 , 1 ) && ! src . has_resident ( 0 , 4 ), "fixture residency wrong" ); 989std::vector < uint8_t > actual (bytes); 990auto holds = [ & ](int e ) { 991const uint8_t * b = src . blob ( 0 , e ); 992return b != nullptr && !std:: memcmp ( b , truth [(size_t) e ]. data (), bytes ); 993}; 994require (! src . replace_resident ( 0 , 1 , 0 ), "an expert already in RAM accepted" ); 995require (! src . replace_resident ( 0 , 4 , 2 ), "a place the RAM does not hold accepted" ); 996require (! src . replace_resident ( 0 , 4 , 4 ) && ! src . replace_resident ( 0 , 5 , 0 ) && ! src . replace_resident ( 1 , 4 , 0 ), 997"invalid arguments accepted" ); 998require ( src . ram_adapts () == 0 && holds ( 0 ) && holds ( 1 ) && holds ( 4 ), "a refused call changed the copy" ); 999// a staged exchange owns the tables until it commits 1000require ( cudaMemcpy ( src . exchange_buffer ( 0 ), cache . device_slot ( 0 ), bytes , cudaMemcpyDeviceToHost ) == cudaSuccess , 1001"D2H eviction failed" ); 1002require ( src . stage_exchange ( 0 , 0 , 2 , 0 ), "stage rejected" ); 1003require (! src . replace_resident ( 0 , 4 , 1 ), "accepted beside a staged exchange" ); 1004require ( cache . fill_slot_blocking ( 0 , truth [ 0 ]. data (), err ), err ); 1005require ( src . commit_exchanges () == 1 , "commit count wrong" ); // RAM: 2 (in 0's place) and 1; VRAM: 0 and 3 1006const uint8_t * place = src . blob ( 0 , 1 ); 1007require ( src . replace_resident ( 0 , 4 , 1 ), "replace rejected" ); 1008require ( src . ram_adapts () == 1 && src . has_resident ( 0 , 4 ) && ! src . has_resident ( 0 , 1 ), "residency wrong" ); 1009require ( src . blob ( 0 , 4 ) == place && holds ( 4 ), "the incoming expert is not in the outgoing one's place" ); 1010require ( holds ( 1 ) && holds ( 2 ), "another expert's bytes changed" ); 1011require ( src . copy_blob ( 0 , 1 , actual . data ()) && actual == truth [ 1 ], "the outgoing expert is not read from the file" ); 1012require (! src . transient ( 0 , 4 ), "resident became transient" ); 1013if ( pin ) { 1014require ( src . pinned ( 0 , 4 ) && src . device_alias ( 0 , 4 ), "mapping lost" ); 1015require ( cudaMemcpy ( actual . data (), src . device_alias ( 0 , 4 ), bytes , cudaMemcpyDeviceToHost ) == cudaSuccess , 1016"device alias read failed" ); 1017require ( actual == truth [ 4 ], "CUDA alias points at the old expert" ); 1018} 1019// and back, then an exchange with the VRAM tier still works on the moved place 1020require ( src . replace_resident ( 0 , 1 , 4 ) && holds ( 1 ) && src . blob ( 0 , 1 ) == place && ! src . has_resident ( 0 , 4 ), 1021"replace back wrong" ); 1022require ( cudaMemcpy ( src . exchange_buffer ( 0 ), cache . device_slot ( 1 ), bytes , cudaMemcpyDeviceToHost ) == cudaSuccess , 1023"D2H eviction failed" ); 1024require ( src . stage_exchange ( 0 , 1 , 3 , 0 ), "stage after replace rejected" ); 1025require ( cache . fill_slot_blocking ( 1 , truth [ 1 ]. data (), err ), err ); 1026require ( src . commit_exchanges () == 1 && src . has_resident ( 0 , 3 ) && holds ( 3 ) && ! src . has_resident ( 0 , 1 ), 1027"exchange after replace wrong" ); 1028require ( src . ram_adapts () == 2 , "accounting wrong" ); 1029// a staged incoming blob: RAM {2,3}, VRAM {0,1}, 4 on the file tier 1030require (! src . replace_resident_staged ( 0 , 4 , 3 ) && src . has_resident ( 0 , 3 ) && src . ram_adapts () == 2 , 1031"an unstaged expert moved" ); 1032std::string why ; 1033set_env ( "STRATA_UNBUFFERED_LOAD" , "1" ); 1034const bool unbuffered = src . set_unbuffered ( 0 , why ); 1035set_env ( "STRATA_UNBUFFERED_LOAD" , nullptr ); 1036if ( unbuffered && src . stages_blobs ()) { 1037require (! src . replace_resident_staged ( 0 , 4 , 3 ) && holds ( 3 ), "moved before any demand read" ); 1038require (! src . has_resident ( 0 , 4 ) && holds ( 4 ), "demand read wrong" ); // leaves 4 in a stage buffer 1039require (! src . replace_resident_staged ( 0 , 4 , 0 ) && ! src . replace_resident_staged ( 0 , 2 , 3 ), 1040"invalid staged pair accepted" ); 1041require ( src . replace_resident_staged ( 0 , 4 , 3 ) && src . has_resident ( 0 , 4 ) && ! src . has_resident ( 0 , 3 ), 1042"staged replacement residency wrong" ); 1043require ( src . resident_blob ( 0 , 4 ) != nullptr && !std:: memcmp ( src . resident_blob ( 0 , 4 ), truth [ 4 ]. data (), bytes ), 1044"staged replacement bytes wrong" ); 1045require ( holds ( 2 ) && holds ( 3 ) && src . ram_adapts () == 3 && src . ram_staged () == 1 , 1046"staged replacement changed another expert" ); 1047std:: cout << "replace_resident_staged: pinned=" << pin << " exact_bytes=PASS\n" ; 1048} else { 1049std:: cout << "replace_resident_staged: pinned=" << pin << " unbuffered unavailable here (" << why << "), positive case SKIPPED\n" ; 1050} 1051std:: cout << "replace_resident: pinned=" << pin << " exact_bytes=PASS\n" ; 1052} 1053 1054} // namespace 1055 1056int main (int argc , char ** argv ) { 1057try { 1058test_complement_plan (); 1059test_resident_lend_region (); 1060test_split_lend_regions (); 1061test_resident_exchange (); 1062test_resident_memory_budget (); 1063test_pin_pacing (); 1064test_wait_for_ram (); 1065test_numa_slice_ranges (); 1066test_cgroup_memory_budget (); 1067test_host_memory (); 1068test_canonical_layout (); 1069test_unbuffered_reads (); 1070test_io_prefetch (); 1071#if defined( STRATA_NATIVE_EXPERTS ) 1072test_native_variable_layout (); 1073#endif 1074if ( argc == 2 && std:: string ( argv [ 1 ]) == "--rotation-gpu" ) { 1075test_rotating_source (false, true); 1076test_rotating_source (true, true); 1077test_rotating_source (true, false); 1078test_foresight_exchange (false); 1079test_foresight_exchange (true); 1080test_replace_resident (true); 1081test_replace_resident (false); 1082} 1083std:: cout << "file_expert_source_test: PASS\n" ; 1084return 0 ; 1085} catch ( const std::exception & error ) { 1086std:: cerr << "file_expert_source_test: " << error . what () << '\n' ; 1087return 1 ; 1088 } 1089}