okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
6ca60e9a0
main
1// src/ngram/ple_reader_stress_test.cpp - #3602: the direct table reader under load, every delivered row compared byte 2// for byte with a plain buffered read of the same file. CPU and a temp file only, no model, no GPU. 3// 4// ple_reader_stress_test [--dir D] [--seconds S] [--big] [--no-sparse-check] 5// 6// What it covers that ple_reader_test --selftest does not: 7// * several client threads issuing and collecting at once (the io thread, the row cache and the slot buffers are 8// shared), several tickets outstanding per client, collected in any order; 9// * a row cache so small that nearly every insert evicts, next to one that holds everything; 10// * max_inflight 1 and 2 (a slot is reused at once), randomly injected completion delays, the keep-alive reads; 11// * rows that straddle a page boundary at every phase (header offsets 0, 192, 4051, 4000 and row sizes 90, 110, 160, 12// 4096), the first and last rows of the table, a file with and without bytes after the table, out-of-range rows; 13// * "late writers": every collected buffer is kept and compared again after later tickets ran, so a read that 14// lands in a buffer after `collect` returned shows up; 15// * the reader after a failed read: the next issue/collect must fail, not wait for ever (Linux: the file is cut 16// short under the open reader); 17// * --big: a sparse table whose rows sit at file offsets around 2^31 and 2^32 and at the end of a 5 GiB file. 18#include "strata/ngram/ple_reader.hpp" 19#include "strata/platform/direct_file.hpp" 20 21#include <algorithm> 22#include <atomic> 23#include <chrono> 24#include <cstdint> 25#include <cstdio> 26#include <cstdlib> 27#include <cstring> 28#include <deque> 29#include <filesystem> 30#include <fstream> 31#include <future> 32#include <mutex> 33#include <random> 34#include <set> 35#include <string> 36#include <thread> 37#include <vector> 38 39#if defined(_WIN32 ) 40#define WIN32_LEAN_AND_MEAN 41#define NOMINMAX 42#include <windows.h> 43#include <winioctl.h> 44#else 45#include <unistd.h> 46#endif 47 48namespace ng= strata::ngram; 49 50namespace { 51 52std::atomic < int > g_fail {0 }; 53std::mutex g_print ; 54#define CHECK (c , ...) \ 55 do { \ 56 if (!(c)) { \ 57 std::lock_guard<std::mutex> lk_(g_print); \ 58 std::fprintf(stderr, "FAIL %s:%d: ", __FILE__, __LINE__); \ 59 std::fprintf(stderr, __VA_ARGS__); \ 60 std::fprintf(stderr, "\n"); \ 61 ++g_fail; \ 62 } \ 63 } while (0) 64 65constexpr uint32_t PAGE = ng::PAGE ; 66 67/// The bytes of row `row` (rb bytes): a xorshift stream seeded by the row, so no two rows alike and a row read from 68/// the wrong place never looks like another row's. 69void fill_row (uint32_t row ,uint32_t rb ,uint8_t * out ) { 70uint64_t x = 0x9E3779B97F4A7C15ull * (uint64_t ) (row + 1 )+ 0x1234567ull ; 71for (uint32_t b = 0 ;b < rb ;++ b ) { 72x ^=x <<13 ; 73x ^=x >>7 ; 74x ^=x <<17 ; 75out [b ]= (uint8_t ) (x >>24 ); 76 } 77} 78 79struct Spec { 80const char * name ; 81uint32_t rb ; 82uint64_t header ; 83uint32_t n_rows ; 84uint64_t trailing ;// bytes after the table 85}; 86 87bool make_table (const std::string & path ,const Spec & s ) { 88 std::ofstream f (path , std::ios::binary | std::ios::trunc ); 89if (!f )return false; 90 std::vector < uint8_t > head (s .header ,0xAB ); 91if (!head .empty ())f .write ((const char * )head .data (), (std::streamsize )head .size ()); 92 std::vector < uint8_t > r (s .rb ); 93for (uint32_t i = 0 ;i < s .n_rows ;++ i ) { 94fill_row (i ,s .rb ,r .data ()); 95f .write ((const char * )r .data (),s .rb ); 96 } 97 std::vector < uint8_t > tail (s .trailing ,0xEE ); 98if (!tail .empty ())f .write ((const char * )tail .data (), (std::streamsize )tail .size ()); 99return (bool )f ; 100} 101 102/// A buffered read of the file: the reference. 103struct Ref { 104 std::ifstream f ; 105uint64_t header ; 106uint32_t rb ; 107uint64_t n_rows ; 108Ref (const std::string & path ,uint64_t h ,uint32_t r ,uint64_t n ) :f (path , std::ios::binary ),header (h ),rb (r ),n_rows (n ) {} 109void row (uint32_t r ,uint8_t * out ) { 110if (r >=n_rows ) { 111 std::memset (out ,0 ,rb ); 112return ; 113 } 114f .clear (); 115f .seekg ((std::streamoff ) (header + (uint64_t )r * rb )); 116f .read ((char * )out ,rb ); 117if ((uint32_t )f .gcount ()!= rb ) std::memset (out ,0x77 ,rb );// the reference itself failed: never equal 118 } 119}; 120 121struct Delivered { 122 std::vector < uint32_t > rows ; 123 std::vector < uint8_t > buf ; 124}; 125 126/// 0 = delivered bytes equal the reference (and, where `expect` is set, the generated row). 127bool verify (Ref & ref ,const Delivered & d ,uint32_t rb ,const char * what ,bool generated ) { 128 std::vector < uint8_t > want (rb ),gen (rb ); 129for (size_t i = 0 ;i < d .rows .size ();++ i ) { 130ref .row (d .rows [i ],want .data ()); 131if (std::memcmp (want .data (),& d .buf [i * rb ],rb )!= 0 ) { 132size_t b = 0 ; 133while (b < rb && want [b ]== d .buf [i * rb + b ])++ b ; 134CHECK (false,"%s: row %u (index %zu of %zu) differs from the file at byte %zu" ,what ,d .rows [i ],i ,d .rows .size (),b ); 135return false; 136 } 137if (generated && d .rows [i ]< ref .n_rows ) { 138fill_row (d .rows [i ],rb ,gen .data ()); 139if (std::memcmp (gen .data (),want .data (),rb )!= 0 ) { 140CHECK (false,"%s: row %u: the file is not what the writer wrote" ,what ,d .rows [i ]); 141return false; 142 } 143 } 144 } 145return true; 146} 147 148struct Picker { 149const Spec & s ; 150 std::vector < uint32_t > straddlers ,tail_rows ,hot ; 151explicit Picker (const Spec & sp , std::mt19937 & rng ) :s (sp ) { 152for (uint32_t r = 0 ;r < s .n_rows ;++ r ) { 153const uint64_t a = s .header + (uint64_t )r * s .rb ; 154if (a /PAGE != (a + s .rb - 1 ) /PAGE )straddlers .push_back (r ); 155 } 156for (uint32_t k = 0 ;k < 200 && k < s .n_rows ;++ k )tail_rows .push_back (s .n_rows - 1 - k ); 157for (int k = 0 ;k < 48 ;++ k )hot .push_back (rng () %s .n_rows ); 158 } 159uint32_t one (std::mt19937 & rng )const { 160switch (rng () %9 ) { 161case 0 :return straddlers .empty () ?rng () %s .n_rows :straddlers [rng () %straddlers .size ()]; 162case 1 :return tail_rows [rng () %tail_rows .size ()]; 163case 2 :return hot [rng () %hot .size ()]; 164case 3 :return (uint32_t ) (rng () %3 )+ (uint32_t ) (rng () %4 );// the first rows 165case 4 :return s .n_rows + (uint32_t ) (rng () %4 );// just out of range 166case 5 :return rng () %40 == 0 ?0xFFFFFFFFu :rng () %s .n_rows ; 167default :return rng () %s .n_rows ; 168 } 169 } 170 std::vector < uint32_t > make (std::mt19937 & rng )const { 171static const size_t sizes []= {1 ,2 ,5 ,16 ,16 ,17 ,64 ,130 ,700 ,2500 }; 172const size_t n = sizes [rng () % (sizeof sizes /sizeof sizes [0 ])]; 173 std::vector < uint32_t > r ; 174r .reserve (n ); 175switch (rng () %5 ) { 176case 0 : {// a run: neighbours share pages 177const uint32_t at = rng () %s .n_rows ; 178for (size_t i = 0 ;i < n ;++ i )r .push_back (at + (uint32_t )i ); 179break ; 180 } 181case 1 :// duplicates 182for (size_t i = 0 ;i < n ;++ i )r .push_back (hot [rng () %4 ]); 183break ; 184default : 185for (size_t i = 0 ;i < n ;++ i )r .push_back (one (rng )); 186 } 187return r ; 188 } 189}; 190 191struct Config { 192bool threaded ; 193uint32_t inflight ; 194uint64_t cache_rows ; 195bool delay ; 196bool keepalive ; 197int clients ; 198}; 199 200void client (ng::PleReader & rd ,const Spec & s ,const Picker & pk ,const std::string & path ,uint32_t seed , 201 std::chrono::steady_clock::time_point until ,long min_tickets , std::atomic < long >& done ) { 202 std::mt19937 rng (seed ); 203Ref ref (path ,s .header ,s .rb ,s .n_rows ); 204 std::deque < Delivered > keep ; 205long tickets = 0 ; 206while (std::chrono::steady_clock::now ()< until || tickets < min_tickets ) { 207const int k = 1 + (int ) (rng () %3 ); 208 std::vector < Delivered > ds ((size_t )k ); 209 std::vector < ng::PleReader ::Ticket > ts ((size_t )k ); 210for (int j = 0 ;j < k ;++ j ) { 211ds [(size_t )j ].rows = pk .make (rng ); 212ds [(size_t )j ].buf .assign (ds [(size_t )j ].rows .size ()* s .rb ,0xCC ); 213ts [(size_t )j ]= rd .issue (ds [(size_t )j ].rows .data (),ds [(size_t )j ].rows .size (),ds [(size_t )j ].buf .data ()); 214 } 215 std::vector < int > order ((size_t )k ); 216for (int j = 0 ;j < k ;++ j )order [(size_t )j ]= j ; 217 std::shuffle (order .begin (),order .end (),rng ); 218for (int j :order ) { 219 std::string err ; 220if (!rd .collect (ts [(size_t )j ],err )) { 221CHECK (false,"collect failed: %s" ,err .c_str ()); 222return ; 223 } 224verify (ref ,ds [(size_t )j ],s .rb ,"ticket" , true); 225++ tickets ; 226keep .push_back (std::move (ds [(size_t )j ])); 227if (keep .size ()> 6 )keep .pop_front (); 228 } 229if (rng () %4 == 0 )// late writers: what was collected earlier must still be what it was 230for (const Delivered & d :keep )verify (ref ,d ,s .rb ,"buffer changed after collect" , false); 231if (g_fail .load ()> 20 )return ; 232 } 233for (const Delivered & d :keep )verify (ref ,d ,s .rb ,"buffer changed after collect (end)" , false); 234done += tickets ; 235} 236 237bool run_config (const std::string & dir ,const Spec & s ,const Config & c ,double seconds ,uint32_t seed ) { 238const std::string path = dir + "/ple_stress_" + std::to_string (s .rb )+ "_" + std::to_string (s .header )+ ".bin" ; 239 std::mt19937 rng (seed ); 240Picker pk (s ,rng ); 241 ng::PleReader rd ; 242 std::string err ; 243const uint64_t cache_rows = std::min < uint64_t > (c .cache_rows , (64ull <<20 ) /s .rb );// at most 64 MiB of cache 244if (!rd .open (path ,s .header ,s .n_rows ,c .inflight ,cache_rows ,err ,c .threaded ,s .rb )) { 245 std::fprintf (stderr ,"cannot open %s: %s\n" ,path .c_str (),err .c_str ()); 246return false; 247 } 248if (c .keepalive )rd .set_keepalive (1.0 ,2.0 ); 249 std::atomic < bool > stop {false}; 250 std::thread chaos ; 251// (the caller-thread reader has no lock: its delay is set once, from this thread, before any client starts) 252if (c .delay && !c .threaded )rd .set_injected_delay_us (300 ); 253if (c .delay && c .threaded ) 254chaos = std::thread ([& ] { 255 std::mt19937 r (seed ^77u ); 256while (!stop .load ()) { 257static const double d []= {0 ,0 ,150 ,600 ,2500 }; 258rd .set_injected_delay_us (d [r () %5 ]); 259 std::this_thread::sleep_for (std::chrono::milliseconds (3 )); 260 } 261rd .set_injected_delay_us (0 ); 262 }); 263const auto until = std::chrono::steady_clock::now ()+ std::chrono::milliseconds ((long ) (seconds * 1000 )); 264 std::atomic < long > done {0 }; 265 std::vector < std::thread > th ; 266const int clients = c .threaded ?c .clients :1 ; 267// a run that is slow (delays) still makes at least this many tickets per client 268for (int i = 0 ;i < clients ;++ i ) 269th .emplace_back (client , std::ref (rd ), std::cref (s ), std::cref (pk ), std::cref (path ),seed + 1000u * (uint32_t )i ,until ,12L , 270 std::ref (done )); 271for (auto & t :th )t .join (); 272stop = true; 273if (chaos .joinable ())chaos .join (); 274CHECK (rd .cache_size () <=rd .cache_capacity (),"row cache %llu over its capacity %llu" , (unsigned long long )rd .cache_size (), 275 (unsigned long long )rd .cache_capacity ()); 276const ng::ReaderStats st = rd .snapshot (); 277 std::printf (" %-14s rb %3u hdr %4llu | %s inflight %2u cache %5llu%s%s clients %d: %ld tickets, %llu rows, %.1f%% cache hits, %llu reads\n" , 278s .name ,s .rb , (unsigned long long )s .header ,c .threaded ?"thread" :"caller" ,c .inflight , 279 (unsigned long long )c .cache_rows ,c .delay ?" delay" :"" ,c .keepalive ?" keepalive" :"" ,clients , 280done .load (), (unsigned long long )st .requests ,st .requests ?100.0 * (double )st .cache_hits / (double )st .requests :0.0 , 281 (unsigned long long )st .reads ); 282rd .close (); 283return true; 284} 285 286#if !defined(_WIN32 ) 287/// After a failed read the reader must keep failing (collect returns false) and must not hang. 288void error_does_not_hang (const std::string & dir ) { 289const Spec s {"error" ,90 ,192 ,20000 ,0 }; 290const std::string path = dir + "/ple_stress_error.bin" ; 291if (!make_table (path ,s )) {CHECK (false,"cannot write %s" ,path .c_str ());return ; } 292for (bool threaded : {true, false}) { 293if (!make_table (path ,s )) {CHECK (false,"cannot rewrite %s" ,path .c_str ());return ; } 294 ng::PleReader rd ; 295 std::string err ; 296if (!rd .open (path ,s .header ,s .n_rows ,8 ,0 ,err ,threaded ,s .rb )) {CHECK (false,"open: %s" ,err .c_str ());return ; } 297 std::vector < uint32_t > rows = {0 ,1 ,2 ,3 }; 298 std::vector < uint8_t > out (rows .size ()* s .rb ); 299CHECK (rd .collect (rd .issue (rows .data (),rows .size (),out .data ()),err ),"first ticket: %s" ,err .c_str ()); 300// cut the file short under the open reader: every page past the cut now reads as a short read 301if (truncate (path .c_str (), (off_t ) (s .header + 100 * s .rb ))!= 0 ) {CHECK (false,"truncate failed" );return ; } 302 std::vector < uint32_t > beyond = {15000 ,15001 ,19999 }; 303 std::vector < uint8_t > out2 (beyond .size ()* s .rb ); 304auto go = std::async (std::launch::async , [& ]()-> int { 305 std::string e1 ,e2 ,e3 ; 306const bool a = rd .collect (rd .issue (beyond .data (),beyond .size (),out2 .data ()),e1 ); 307// a ticket after the failure: nothing to read (or something the cache holds) - it must still return 308 std::vector < uint32_t > again = {5 ,15500 }; 309 std::vector < uint8_t > out3 (again .size ()* s .rb ); 310const bool b = rd .collect (rd .issue (again .data (),again .size (),out3 .data ()),e2 ); 311const bool c = rd .collect (rd .issue (beyond .data (),beyond .size (),out2 .data ()),e3 ); 312return (a ?1 :0 ) | (b ?2 :0 ) | (c ?4 :0 ); 313 }); 314if (go .wait_for (std::chrono::seconds (20 ))!= std::future_status::ready ) { 315 std::fprintf (stderr ,"FAIL: a ticket issued after a failed read never returned (%s reader)\n" ,threaded ?"thread" :"caller" ); 316++ g_fail ; 317 std::_Exit (3 );// the reader's destructor would wait for the same thing 318 } 319const int r = go .get (); 320CHECK ((r & 1 )== 0 ,"a read past the cut file did not fail (%s reader)" ,threaded ?"thread" :"caller" ); 321CHECK ((r & 2 )== 0 && (r & 4 )== 0 ,"a ticket after the failure succeeded (mask %d, %s reader)" ,r ,threaded ?"thread" :"caller" ); 322rd .close (); 323 } 324 std::filesystem::remove (path ); 325 std::printf (" error path: a failed read fails the next tickets too, none hangs\n" ); 326} 327#endif 328 329// ----------------------------------------------------------------------------------------------- the big sparse table 330/// A sparse file of `size` bytes with the given (offset, bytes) pieces written. 331bool make_sparse (const std::string & path ,uint64_t size ,const std::vector < std::pair < uint64_t , std::vector < uint8_t >>>& pieces ) { 332#if defined(_WIN32 ) 333HANDLE h = CreateFileA (path .c_str (),GENERIC_READ |GENERIC_WRITE ,0 ,nullptr ,CREATE_ALWAYS ,FILE_ATTRIBUTE_NORMAL ,nullptr ); 334if (h == INVALID_HANDLE_VALUE )return false; 335DWORD ret = 0 ; 336DeviceIoControl (h ,FSCTL_SET_SPARSE ,nullptr ,0 ,nullptr ,0 ,& ret ,nullptr );// if it is refused the file is just big 337bool ok = true; 338for (const auto & p :pieces ) { 339LARGE_INTEGER at ; 340at .QuadPart = (LONGLONG )p .first ; 341DWORD w = 0 ; 342ok = ok && SetFilePointerEx (h ,at ,nullptr ,FILE_BEGIN )&& WriteFile (h ,p .second .data (), (DWORD )p .second .size (),& w ,nullptr )&& 343w == p .second .size (); 344 } 345LARGE_INTEGER end ; 346end .QuadPart = (LONGLONG )size ; 347ok = ok && SetFilePointerEx (h ,end ,nullptr ,FILE_BEGIN )&& SetEndOfFile (h ); 348CloseHandle (h ); 349return ok ; 350#else 351 { 352 std::ofstream f (path , std::ios::binary | std::ios::trunc ); 353if (!f )return false; 354for (const auto & p :pieces ) { 355f .seekp ((std::streamoff )p .first ); 356f .write ((const char * )p .second .data (), (std::streamsize )p .second .size ()); 357 } 358if (!f )return false; 359 } 360 std::error_code ec ; 361 std::filesystem::resize_file (path ,size ,ec ); 362return !ec ; 363#endif 364} 365 366void big_offsets (const std::string & dir ) { 367constexpr uint32_t RB = 90 ; 368constexpr uint64_t HEADER = 192 ; 369constexpr uint32_t N = 61000000u ;// 5.49e9 bytes: past 2^32 (4.29e9), below 2^33 370const uint64_t size = HEADER + (uint64_t )N * RB ; 371 std::set < uint32_t > marks ; 372auto around = [& ](uint64_t byte_offset ) { 373const uint32_t r = (uint32_t ) ((byte_offset - HEADER ) /RB ); 374for (int d = -3 ;d <=3 ;++ d ) 375if ((int64_t )r + d >=0 && (uint64_t ) ((int64_t )r + d )< N )marks .insert ((uint32_t ) ((int64_t )r + d )); 376 }; 377around (1ull <<31 ); 378around ((1ull <<32 )- 4096 ); 379around (1ull <<32 ); 380around ((1ull <<32 )+ 4096 ); 381around (3ull <<30 ); 382around (5ull <<30 ); 383for (uint32_t k = 0 ;k < 6 ;++ k )marks .insert (N - 1 - k ); 384for (uint32_t k = 0 ;k < 4 ;++ k )marks .insert (k ); 385// rows that cross a page boundary in the neighbourhood of 2^32 386for (uint32_t r = (uint32_t ) (((1ull <<32 )- HEADER ) /RB )- 200 ;r < (uint32_t ) (((1ull <<32 )- HEADER ) /RB )+ 200 ;++ r ) { 387const uint64_t a = HEADER + (uint64_t )r * RB ; 388if (a /PAGE != (a + RB - 1 ) /PAGE )marks .insert (r ); 389 } 390 std::vector < std::pair < uint64_t , std::vector < uint8_t >>> pieces ; 391for (uint32_t r :marks ) { 392 std::vector < uint8_t > b (RB ); 393fill_row (r ,RB ,b .data ()); 394pieces .emplace_back (HEADER + (uint64_t )r * RB , std::move (b )); 395 } 396const std::string path = dir + "/ple_stress_big.bin" ; 397if (!make_sparse (path ,size ,pieces )) { 398 std::fprintf (stderr ," big table: cannot create the sparse file here, skipped\n" ); 399return ; 400 } 401Ref ref (path ,HEADER ,RB ,N ); 402 std::mt19937 rng (99 ); 403for (bool threaded : {true, false}) { 404 ng::PleReader rd ; 405 std::string err ; 406if (!rd .open (path ,HEADER ,N ,16 ,1024 ,err ,threaded ,RB )) {CHECK (false,"big open: %s" ,err .c_str ());break ; } 407 std::vector < uint32_t > mk (marks .begin (),marks .end ()); 408for (int round = 0 ;round < 40 ;++ round ) { 409Delivered d ; 410for (int i = 0 ;i < 64 ;++ i )d .rows .push_back (rng () %3 == 0 ? (uint32_t ) (rng () %N ) :mk [rng () %mk .size ()]); 411d .buf .assign (d .rows .size ()* RB ,0xCC ); 412CHECK (rd .collect (rd .issue (d .rows .data (),d .rows .size (),d .buf .data ()),err ),"big collect: %s" ,err .c_str ()); 413verify (ref ,d ,RB ,"big table" , false); 414// and against what was written: a marked row is its generated bytes, any other row is zeros 415 std::vector < uint8_t > want (RB ); 416for (size_t i = 0 ;i < d .rows .size ();++ i ) { 417if (marks .count (d .rows [i ]))fill_row (d .rows [i ],RB ,want .data ()); 418else std::memset (want .data (),0 ,RB ); 419CHECK (std::memcmp (want .data (),& d .buf [i * RB ],RB )== 0 ,"big table: row %u is not what was written" ,d .rows [i ]); 420 } 421 } 422rd .close (); 423 } 424 std::error_code ec ; 425 std::filesystem::remove (path ,ec ); 426 std::printf (" big table: %zu marked rows at offsets around 2^31, 2^32 and the end of a %.2f GiB file, both readers: done\n" , 427marks .size (), (double )size / (1024.0 * 1024.0 * 1024.0 )); 428} 429 430}// namespace 431 432int main (int argc ,char ** argv ) { 433 std::string dir = std::filesystem::temp_directory_path ().string (); 434double seconds = 0.3 ; 435bool big = false,only_error = false; 436for (int i = 1 ;i < argc ;++ i ) { 437const std::string a = argv [i ]; 438if (a == "--dir" && i + 1 < argc )dir = argv [++ i ]; 439else if (a == "--seconds" && i + 1 < argc )seconds = std::atof (argv [++ i ]); 440else if (a == "--big" )big = true; 441else if (a == "--only-error-path" )only_error = true; 442else { std::fprintf (stderr ,"usage: ple_reader_stress_test [--dir D] [--seconds S] [--big] [--only-error-path]\n" );return 2 ; } 443 } 444// a file system without direct reads (tmpfs on an older kernel): say so and skip 445 { 446const std::string probe = dir + "/ple_stress_probe.bin" ; 447const Spec s {"probe" ,90 ,192 ,1000 ,0 }; 448if (!make_table (probe ,s )) { std::fprintf (stderr ,"cannot write in %s\n" ,dir .c_str ());return 2 ; } 449 ng::PleReader rd ; 450 std::string err ; 451const bool ok = rd .open (probe ,s .header ,s .n_rows ,4 ,0 ,err , true,s .rb ); 452rd .close (); 453 std::filesystem::remove (probe ); 454if (!ok ) { 455 std::printf ("SKIP: direct reads are not possible in %s (%s)\n" ,dir .c_str (),err .c_str ()); 456return 77 ; 457 } 458 } 459const Spec specs []= { 460 {"iq4nl" ,90 ,192 ,150000 ,0 },// the real shard's shape: header 192, 90-byte rows, the table ends the file 461 {"iq4nl+tail" ,90 ,192 ,150000 ,777 },// bytes after the table 462 {"odd phase" ,90 ,4051 ,120000 ,0 },// the first row straddles 463 {"q5_0" ,110 ,4000 ,100000 ,0 }, 464 {"fp8" ,160 ,0 ,90000 ,123 }, 465 {"page row" ,4096 ,192 ,3000 ,0 },// every row crosses a page boundary 466 {"tiny" ,1 ,5 ,30000 ,0 }, 467 }; 468 std::vector < Config > cfgs ; 469for (uint32_t inflight : {1u ,2u ,3u ,64u }) 470for (uint64_t cache : {0ull ,16ull ,100000ull }) 471cfgs .push_back ({true,inflight ,cache , (inflight + cache ) %2 == 0 ,inflight == 2 ,4 }); 472cfgs .push_back ({false,1 ,16 , false, false,1 }); 473cfgs .push_back ({false,8 ,100000 , false, false,1 }); 474cfgs .push_back ({false,64 ,0 , true, false,1 }); 475 std::printf ("ple_reader stress: %zu tables x %zu configurations, %.1f s each (dir %s)\n" ,sizeof specs /sizeof specs [0 ], 476cfgs .size (),seconds ,dir .c_str ()); 477uint32_t seed = 1 ; 478for (const Spec & s :specs ) { 479if (only_error )break ; 480const std::string path = dir + "/ple_stress_" + std::to_string (s .rb )+ "_" + std::to_string (s .header )+ ".bin" ; 481if (!make_table (path ,s )) { std::fprintf (stderr ,"cannot write %s\n" ,path .c_str ());return 2 ; } 482for (const Config & c :cfgs ) { 483if (!run_config (dir ,s ,c ,seconds ,++ seed ))return 2 ; 484if (g_fail .load ()> 0 )break ; 485 } 486 std::filesystem::remove (path ); 487if (g_fail .load ()> 0 )break ; 488 } 489#if !defined(_WIN32 ) 490if (g_fail .load ()== 0 )error_does_not_hang (dir ); 491#endif 492if (big && g_fail .load ()== 0 )big_offsets (dir ); 493 std::printf ("ple_reader stress: %s\n" ,g_fail .load () ?"FAILED" :"OK" ); 494return g_fail .load () ?1 :0 ; 495}