okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
3450cddd5
main
1// Aligned reads must preserve every matrix byte, including unaligned file offsets and reused slots. 2#include "ds4_store.hpp" 3 4#include <filesystem> 5#include <fstream> 6#include <thread> 7 8using strata::ds4::ExpertStore ; 9namespace os= strata::ds4::os; 10 11struct File { 12 std::string path = (std::filesystem::temp_directory_path () / 13 ("strata-store-" + std::to_string (std::chrono::steady_clock::now ().time_since_epoch ().count ()))).string (); 14 std::vector < uint8_t > bytes = std::vector < uint8_t > (1 <<20 ); 15 os::fd_t fd = os::kBadFd ,direct = os::kBadFd ; 16File () { 17uint32_t v = 17 ; 18for (auto & b :bytes ) {v = v * 1664525u + 1013904223u ;b = (uint8_t ) (v >>24 ); } 19 { std::ofstream out (path , std::ios::binary );out .write ((const char * )bytes .data (),bytes .size ()); } 20fd = os::open_par (path ); 21direct = os::open_direct (path ); 22 } 23 ~File () { 24 os::close_fd (direct ); 25 os::close_fd (fd ); 26 std::filesystem::remove (path ); 27 } 28}; 29 30enum class Mode {Buffered ,Direct ,Mapped ,Failed }; 31 32static bool check (File & file ,bool inplace ,Mode mode ,size_t piece ) { 33ExpertStore ::LayerGeo geo ; 34geo .n_exp = 3 ; 35const size_t bytes = 65539 ; 36for (int r = 0 ;r < 3 ;++ r ) { 37geo .file [r ]= 0 ; 38geo .off [r ]= 123 + (size_t )r * 300001 ; 39geo .stride [r ]= bytes ; 40geo .bytes [r ]= bytes ; 41geo .tr [r ]= (size_t )r * (((bytes + 255 )& ~(size_t )255 )+ ExpertStore ::kSlack ); 42 } 43geo .blob = geo .tr [2 ]+ bytes ; 44if (mode == Mode ::Failed )geo .off [2 ]= file .bytes .size ()+ 4096 ; 45ExpertStore ::Cfg cfg ; 46cfg .budget_bytes = 2 * ((geo .blob + 2 * ExpertStore ::kSlack + 4095 )& ~(size_t )4095 ); 47cfg .floor_bytes = 0 ; 48cfg .threads = 2 ; 49cfg .read_in_place = inplace ; 50cfg .piece_bytes = piece ; 51if (mode == Mode ::Mapped )cfg .map_chunk = [](void * p ,size_t ) {return p ; }; 52ExpertStore store ; 53if (!store .init (cfg , {geo }, {mode == Mode ::Direct ?file .direct : os::kBadFd }, {file .fd }))return false; 54for (int pass = 0 ;pass < 3 ;++ pass ) { 55for (int e = 0 ;e < 3 ;++ e ) { 56 std::vector < char > ok ; 57const bool ready = store .ensure (0 ,& e ,1 ,ok ); 58if (mode == Mode ::Failed ) {if (ready || ok [0 ])return false;continue ; } 59if (!ready || !ok [0 ])return false; 60const int slot = store .find (0 ,e ); 61if (slot < 0 )return false; 62for (int r = 0 ;r < 3 ;++ r ) { 63if (std::memcmp (store .mat (slot ,r ),file .bytes .data ()+ geo .off [r ]+ e * bytes ,bytes ))return false; 64if (store .bytes_ready (slot ,r ,0 ,bytes )!= 1 || store .bytes_ready (slot ,r ,bytes - 1 ,bytes )!= 1 )return false; 65if (mode == Mode ::Mapped && store .mat (slot ,r )!= (const uint8_t * )store .dev_blob (slot )+ geo .tr [r ])return false; 66 } 67store .release (0 ,& e ,1 ,ok ); 68 } 69 } 70return true; 71} 72 73// A pinned slot whose read failed must not be handed to another expert while its window still holds it (DS4_LANE_STREAM 74// reads the pieces of it that did land until the window ends). 75static bool pinned_failed_kept (File & file ,size_t piece ) { 76ExpertStore ::LayerGeo geo ; 77geo .n_exp = 2 ; 78const size_t bytes = 65539 ; 79for (int r = 0 ;r < 3 ;++ r ) { 80geo .file [r ]= 0 ; 81geo .off [r ]= file .bytes .size ()+ 4096 + (size_t )r * 300001 ;// (past the file's end: every read fails) 82geo .stride [r ]= bytes ; 83geo .bytes [r ]= bytes ; 84geo .tr [r ]= (size_t )r * (((bytes + 255 )& ~(size_t )255 )+ ExpertStore ::kSlack ); 85 } 86geo .blob = geo .tr [2 ]+ bytes ; 87ExpertStore ::Cfg cfg ; 88cfg .budget_bytes = (geo .blob + 2 * ExpertStore ::kSlack + 4095 )& ~(size_t )4095 ;// (one slot) 89cfg .floor_bytes = 0 ; 90cfg .threads = 2 ; 91cfg .read_in_place = true; 92cfg .piece_bytes = piece ; 93ExpertStore store ; 94if (!store .init (cfg , {geo }, {file .direct }, {file .fd }))return false; 95 std::vector < char > oka ,okb ; 96 std::vector < int > sla ,slb ; 97const int a = 0 ,b = 1 ; 98store .begin (0 ,& a ,1 ,oka ,sla ); 99if (sla [0 ]< 0 )return false; 100while (!store .loaded (sla [0 ])) std::this_thread::yield (); 101store .begin (0 ,& b ,1 ,okb ,slb ); 102if (slb [0 ]== sla [0 ])return false; 103store .wait_slots (sla ,oka ,0 ,1 ); 104store .wait_slots (slb ,okb ,0 ,1 ); 105store .release (0 ,& a ,1 ,oka ); 106store .release (0 ,& b ,1 ,okb ); 107return !oka [0 ]; 108} 109 110int main () { 111File file ; 112if (file .fd == os::kBadFd )return 1 ; 113for (size_t piece : {(size_t )0 , (size_t )4096 }) 114if (!pinned_failed_kept (file ,piece )) { 115 std::fprintf (stderr ,"store layout failed: a pinned failed slot was reused (piece %zu)\n" ,piece ); 116return 3 ; 117 } 118for (bool inplace : {false, true}) { 119for (Mode mode : {Mode ::Buffered ,Mode ::Direct ,Mode ::Mapped ,Mode ::Failed }) 120for (size_t piece : {(size_t )0 , (size_t )4096 , (size_t )16384 }) {// (pieces: in place only) 121if (mode == Mode ::Direct && file .direct == os::kBadFd )continue ; 122if (!check (file ,inplace ,mode ,piece )) { 123 std::fprintf (stderr ,"store layout failed: inplace %d, mode %d, piece %zu\n" , (int )inplace , (int )mode ,piece ); 124return 2 ; 125 } 126 } 127 } 128 std::puts ("store matrix layouts, piece reads, slot reuse, mapped layout and failed reads: OK" ); 129return 0 ; 130}