okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
a1bbe27b4
main
1// the file-cache give-back during and after the expert load (include/strata/core/page_cache_drop.hpp). No GPU. Cases: the flag and the 2// switch, one range and one whole file of a temporary file, every error path (empty path, missing file, bad descriptor, empty range) ignored, 3// the final call over a mixed list (a missing file and a repeated path among good ones), the meminfo reader, the draft line. (The device rule 4// is checked in src/core/arch_defaults_test.cpp.) 5#include "strata/core/page_cache_drop.hpp" 6 7#include <cstdint> 8#include <cstdio> 9#include <cstdlib> 10#include <string> 11#include <vector> 12 13#if defined(__linux__ ) 14#include <fcntl.h> 15#include <sys/mman.h> 16#include <sys/stat.h> 17#include <unistd.h> 18#endif 19 20using namespace strata::core ; 21namespace pcd= strata::core::page_cache_drop; 22 23namespace { 24int failed = 0 ; 25#define CHECK (x ) do { if (!(x)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #x); ++failed; } } while (0) 26 27#if defined(__linux__ ) 28// a temporary file of `bytes` bytes of a pattern, written through, flushed and read back once (so its pages are clean and cached); "" on failure 29std::string make_file (size_t bytes ) { 30char path []= "/tmp/page_cache_drop_test_XXXXXX" ; 31const int fd = ::mkstemp (path ); 32if (fd < 0 )return std::string (); 33 std::vector < unsigned char > buf (bytes ); 34for (size_t i = 0 ;i < bytes ;++ i )buf [i ]= (unsigned char ) (i * 131 + 7 ); 35bool ok = ::write (fd ,buf .data (),bytes )== (ssize_t )bytes && ::fdatasync (fd )== 0 ; 36 std::vector < unsigned char > back (bytes ); 37ok = ok && ::pread (fd ,back .data (),bytes ,0 )== (ssize_t )bytes ; 38 ::close (fd ); 39if (!ok ) { ::unlink (path );return std::string (); } 40return std::string (path ); 41} 42// how many pages of the file are resident (mincore); -1 when it cannot be told 43long resident_pages (const std::string & path ) { 44const int fd = ::open (path .c_str (),O_RDONLY ); 45if (fd < 0 )return -1 ; 46struct stat st {}; 47if (::fstat (fd ,& st )!= 0 || st .st_size == 0 ) { ::close (fd );return -1 ; } 48void * m = ::mmap (nullptr , (size_t )st .st_size ,PROT_READ ,MAP_SHARED ,fd ,0 ); 49if (m == MAP_FAILED ) { ::close (fd );return -1 ; } 50const size_t page = (size_t ) ::sysconf (_SC_PAGESIZE ); 51const size_t n = ((size_t )st .st_size + page - 1 ) /page ; 52 std::vector < unsigned char > vec (n ); 53long r = ::mincore (m , (size_t )st .st_size ,vec .data ())== 0 ?0 :-1 ; 54if (r == 0 )for (unsigned char c :vec )r += c & 1 ; 55 ::munmap (m , (size_t )st .st_size ); 56 ::close (fd ); 57return r ; 58} 59#endif 60}// namespace 61 62int main () { 63// 1. the flag and the switch 64 { 65arena_cache_drop_flag ().store (false); 66CHECK (!pcd::enabled ()); 67#if defined(__linux__ ) 68 ::unsetenv ("STRATA_NO_ARENA_CACHE_DROP" ); 69arena_cache_drop_flag ().store (true); 70CHECK (pcd::enabled ()); 71 ::setenv ("STRATA_NO_ARENA_CACHE_DROP" ,"1" ,1 ); 72CHECK (!pcd::enabled ()&& pcd::off_by_env ()); 73 ::setenv ("STRATA_NO_ARENA_CACHE_DROP" ,"0" ,1 ); 74CHECK (pcd::enabled ()&& !pcd::off_by_env ()); 75 ::setenv ("STRATA_NO_ARENA_CACHE_DROP" ,"" ,1 ); 76CHECK (pcd::enabled ()&& !pcd::off_by_env ()); 77 ::unsetenv ("STRATA_NO_ARENA_CACHE_DROP" ); 78#else 79arena_cache_drop_flag ().store (true); 80CHECK (!pcd::enabled ());// never off Linux 81#endif 82arena_cache_drop_flag ().store (false); 83 } 84#if defined(__linux__ ) 85// 2. one range and one whole file of a real file: the calls work, the advised bytes add up, the file is unchanged 86 { 87const size_t sz = 4u <<20 ; 88const std::string p = make_file (sz ); 89CHECK (!p .empty ()); 90if (!p .empty ()) { 91const int fd = ::open (p .c_str (),O_RDONLY ); 92CHECK (fd >=0 ); 93const uint64_t a0 = pcd::advised_bytes ().load (); 94CHECK (pcd::drop_range (fd ,0 ,1u <<20 )); 95CHECK (pcd::advised_bytes ().load ()== a0 + (1u <<20 )); 96CHECK (pcd::drop_range (fd ,1u <<20 ,sz - (1u <<20 ))); 97CHECK (pcd::advised_bytes ().load ()== a0 + sz ); 98CHECK (pcd::drop_range (fd ,100u <<20 ,4096 ));// beyond the end: advice, not an error 99const long before = resident_pages (p ); 100CHECK (pcd::drop_file (p )); 101const long after = resident_pages (p ); 102CHECK (before < 0 || after < 0 || after <=before );// (tmpfs keeps its pages: only "not more" is required) 103 std::vector < unsigned char > back (sz ); 104CHECK (::pread (fd ,back .data (),sz ,0 )== (ssize_t )sz ); 105bool same = true; 106for (size_t i = 0 ;i < sz && same ;++ i )same = back [i ]== (unsigned char ) (i * 131 + 7 ); 107CHECK (same );// what a later reader needs is read again, unchanged 108 ::close (fd ); 109 ::unlink (p .c_str ()); 110 } 111 } 112// 3. every error path is ignored and returns false; nothing is added to the advised bytes 113 { 114const uint64_t a0 = pcd::advised_bytes ().load (); 115CHECK (!pcd::drop_range (-1 ,0 ,4096 )); 116CHECK (!pcd::drop_range (0x7fffffff ,0 ,4096 ));// not an open descriptor 117const std::string p = make_file (8192 ); 118const int fd = p .empty () ?-1 : ::open (p .c_str (),O_RDONLY ); 119CHECK (fd >=0 ); 120CHECK (!pcd::drop_range (fd ,0 ,0 ));// an empty range 121 ::close (fd ); 122CHECK (!pcd::drop_range (fd ,0 ,4096 ));// closed 123if (!p .empty ()) ::unlink (p .c_str ()); 124CHECK (pcd::advised_bytes ().load ()== a0 ); 125CHECK (!pcd::drop_file ("" )); 126CHECK (!pcd::drop_file ("/nonexistent/page_cache_drop_test/file.bin" )); 127CHECK (!pcd::drop_file ("/proc/self/mem/x" ));// a path that cannot be opened 128 } 129// 4. the final call over a mixed list: a missing file, an empty path and a repeated path among good ones 130 { 131const std::string a = make_file (1u <<20 ),b = make_file (1u <<20 ); 132CHECK (!a .empty ()&& !b .empty ()); 133const pcd::Outcome o = pcd::give_back ({a ,"/nonexistent/page_cache_drop_test/x.bin" ,"" ,b ,a }); 134CHECK (o .files == 3 );// a, the missing one, b (the empty path and the repeat are left out) 135CHECK (o .failed == 1 );// the missing one, ignored 136CHECK (o .ms >=0.0 ); 137CHECK (o .cached_before > 0 && o .cached_after > 0 );// /proc/meminfo is readable 138const pcd::Outcome none = pcd::give_back ({}); 139CHECK (none .files == 0 && none .failed == 0 ); 140if (!a .empty ()) ::unlink (a .c_str ()); 141if (!b .empty ()) ::unlink (b .c_str ()); 142 } 143// 5. the meminfo reader 144 { 145CHECK (pcd::meminfo_bytes ("Cached" )> 0 ); 146CHECK (pcd::meminfo_bytes ("MemFree" )> 0 ); 147CHECK (pcd::meminfo_bytes ("NoSuchKey" )== 0 ); 148CHECK (pcd::meminfo_bytes ("Cach" )== 0 );// a prefix is not a key 149 } 150#endif 151// 6. the draft line: nothing when nothing was given back, a line otherwise, with the numbers and the switch 152 { 153 pcd::Outcome none ; 154CHECK (pcd::draft_line (none ).empty ()); 155 pcd::Outcome o ; 156o .advised_behind = 9ull <<30 ; 157o .cached_before = 7ull <<30 ; 158o .cached_after = 1ull <<30 ; 159o .ms = 520.0 ; 160const std::string s = pcd::draft_line (o ); 161CHECK (s .rfind ("the model files' cached pages were given back after the expert load: " ,0 )== 0 ); 162CHECK (s .find ("9.0 GiB" )!= std::string::npos ); 163CHECK (s .find ("6.0 GiB" )!= std::string::npos ); 164CHECK (s .find ("520 ms" )!= std::string::npos ); 165CHECK (s .find ("STRATA_NO_ARENA_CACHE_DROP=1" )!= std::string::npos ); 166 pcd::Outcome grew ;// the cache grew meanwhile and nothing was advised behind: nothing to say 167grew .cached_before = 1ull <<30 ; 168grew .cached_after = 2ull <<30 ; 169CHECK (pcd::draft_line (grew ).empty ()); 170 } 171if (failed != 0 ) { 172 std::fprintf (stderr ,"page_cache_drop_test: %d check(s) failed\n" ,failed ); 173return 1 ; 174 } 175 std::printf ("page_cache_drop_test: ok\n" ); 176return 0 ; 177}