okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
36ef9d6e6
main
1#include "strata/platform/direct_file.hpp" 2#include <cstdlib> 3#include <cstring> 4#include <fstream> 5#include <iostream> 6#include <stdexcept> 7#include <string> 8#include <thread> 9#include <vector> 10#include <unistd.h> 11using strata::platform::DirectFile ; 12using strata::platform::Completion ; 13static void check (bool ok ,const char * why ) {if (!ok )throw std::runtime_error (why ); } 14int main () { 15char name []= "/tmp/strata-direct-test-XXXXXX" ; 16int fd = mkstemp (name ); 17if (fd < 0 )return 1 ; 18close (fd ); 19void * memory = DirectFile ::alloc_aligned (4096 * 18 ); 20try { 21check (memory != nullptr ,"buffer allocation" ); 22 { std::ofstream f (name , std::ios::binary ); std::string bytes (4096 * 16 + 17 ,'x' );f .write (bytes .data (),bytes .size ()); } 23DirectFile f ; 24 std::string error ; 25setenv ("STRATA_IO_THREADS" ,"4" ,1 );// the pool's size (out-of-range values are clamped to 1..64) 26check (f .open (name ,error ),error .c_str ()); 27check (!f .submit (1 ,memory ,4096 ,0 ,error ),"unaligned request accepted" ); 28for (unsigned i = 0 ;i < 17 ;++ i ) 29check (f .submit (i * 4096 ,static_cast < char *> (memory )+ i * 4096 ,4096 ,i ,error ),error .c_str ()); 30 std::vector < bool > seen (17 ); 31unsigned count = 0 ; 32while (count < 17 ) { 33Completion c [17 ];int n = f .wait (c ,17 ,5000 ); 34check (n > 0 ,"completion timeout" ); 35for (int i = 0 ;i < n ;++ i ) { 36check (c [i ].tag < 17 && !seen [c [i ].tag ],"wrong or repeated completion" ); 37seen [c [i ].tag ]= true;++ count ; 38check (c [i ].ok && c [i ].bytes == (c [i ].tag == 16 ?17u :4096u ),"incorrect full/EOF completion" ); 39check (static_cast < char *> (memory )[c [i ].tag * 4096 ]== 'x' ,"read data mismatch" ); 40 } 41 } 42 std::jthread waker ([& ] {f .wake (); }); 43Completion c ;check (f .wait (& c ,1 ,5000 )== 1 && c .tag == DirectFile ::WAKE_TAG ,"wake lost" ); 44waker .join (); 45check (f .submit (0 ,memory ,4096 ,99 ,error ),error .c_str ()); 46f .close ();// Must drain outstanding buffer writes before returning. 47 std::memset (memory ,0 ,4096 ); 48check (f .open (name ,error ),error .c_str ()); 49check (f .wait (& c ,1 ,0 )== 0 ,"stale completion on reopen" ); 50check (f .submit (0 ,memory ,4096 ,100 ,error ),error .c_str ()); 51check (f .wait (& c ,1 ,5000 )== 1 && c .ok && c .tag == 100 ,"reopened read failed" ); 52f .close (); 53DirectFile ::free_aligned (memory );unlink (name ); 54 std::cout <<"direct_file_async: passed queued reads, short EOF, wake, close drain, reopen\n" ; 55return 0 ; 56 }catch (const std::exception & e ) { 57DirectFile ::free_aligned (memory );unlink (name ); 58 std::cerr <<e .what () <<'\n' ;return 1 ; 59 } 60}