okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
29ebb7926
main
1// the wait before READY on an integrated AMD GPU under Linux (include/strata/core/kfd_settle.hpp). No GPU, no sleeping: the 2// counter reader and the clock are faked; the folder reader is tried on a temporary folder. Cases: file missing, unreadable, garbage, 3// counter moving, counter quiet, cap reached. (The device rule is checked in src/core/arch_defaults_test.cpp.) 4#include "strata/core/kfd_settle.hpp" 5 6#include <chrono> 7#include <cstdint> 8#include <cstdio> 9#include <cstdlib> 10#include <functional> 11#include <string> 12 13#if defined(__linux__ ) 14#include <sys/stat.h> 15#include <unistd.h> 16#endif 17 18using namespace strata::core ; 19using namespace strata::core::kfd_settle ; 20 21namespace { 22int failed = 0 ; 23#define CHECK (x ) do { if (!(x)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #x); ++failed; } } while (0) 24 25struct Fake { 26double t = 0.0 ; 27int sleeps = 0 ; 28 std::function < Reading (int )> reading ;// the reading at poll number i (0 = the first, before any sleep) 29Result run (double quiet ,double cap ,double poll = 0.2 ) { 30return settle ([& ] {return reading (sleeps ); }, [& ] {return t ; }, [& ](double s ) {t += s ;++ sleeps ; },quiet ,cap ,poll ); 31 } 32}; 33Reading ok (int64_t ms ) {Reading r ;r .kind = Reading ::Ok ;r .ms = ms ;return r ; } 34Reading kind (Reading ::Kind k ) {Reading r ;r .kind = k ;return r ; } 35bool near (double a ,double b ,double tol ) {return a > b - tol && a < b + tol ; } 36 37#if defined(__linux__ ) 38void put (const std::string & path ,const std::string & text ) { 39if (std::FILE * f = std::fopen (path .c_str (),"w" )) { std::fwrite (text .data (),1 ,text .size (),f ); std::fclose (f ); } 40} 41#endif 42}// namespace 43 44int main () { 45// 1. the counter file is missing: nothing is waited, nothing is slept 46 { 47Fake f ; 48f .reading = [](int ) {return kind (Reading ::Missing ); }; 49const Result r = f .run (2.0 ,10.0 ); 50CHECK (r .why == Result ::NoCounter ); 51CHECK (r .waited_s == 0.0 ); 52CHECK (f .sleeps == 0 ); 53 } 54// 2. unreadable: the same at the first reading; a counter that stops being readable while waiting ends the wait 55 { 56Fake f ; 57f .reading = [](int ) {return kind (Reading ::Unreadable ); }; 58const Result r = f .run (2.0 ,10.0 ); 59CHECK (r .why == Result ::NoCounter && r .waited_s == 0.0 && f .sleeps == 0 ); 60Fake g ; 61g .reading = [](int i ) {return i < 3 ?ok (100 + i ) :kind (Reading ::Unreadable ); };// moving, then gone 62const Result s = g .run (2.0 ,10.0 ); 63CHECK (s .why == Result ::NoCounter ); 64CHECK (near (s .waited_s ,0.6 ,0.05 )); 65 } 66// 3. garbage in the file 67 { 68int64_t ms = -1 ; 69CHECK (!parse_evicted_ms ("" ,ms )); 70CHECK (!parse_evicted_ms (" \n" ,ms )); 71CHECK (!parse_evicted_ms ("abc" ,ms )); 72CHECK (!parse_evicted_ms ("12x" ,ms )); 73CHECK (!parse_evicted_ms ("-5" ,ms )); 74CHECK (!parse_evicted_ms ("+5" ,ms )); 75CHECK (!parse_evicted_ms ("1 2" ,ms )); 76CHECK (!parse_evicted_ms ("1.5" ,ms )); 77CHECK (!parse_evicted_ms ("1234567890123456789" ,ms ));// 19 digits 78CHECK (ms == -1 );// untouched on failure 79CHECK (parse_evicted_ms ("0" ,ms )&& ms == 0 ); 80CHECK (parse_evicted_ms ("12\n" ,ms )&& ms == 12 ); 81CHECK (parse_evicted_ms (" 7 \t\n" ,ms )&& ms == 7 ); 82CHECK (parse_evicted_ms ("123456789012345678" ,ms )&& ms == 123456789012345678LL );// 18 digits 83 } 84// 4. the counter moving: every poll for 1 s, then still: the wait ends two quiet seconds after the last move 85 { 86Fake f ; 87f .reading = [](int i ) {return ok (i <=5 ?1000 + 10 * i :1050 ); };// moves at polls 1..5 (t = 0.2 .. 1.0) 88const Result r = f .run (2.0 ,10.0 ); 89CHECK (r .why == Result ::Quiet ); 90CHECK (near (r .waited_s ,3.0 ,0.25 ));// last move at 1.0 s + 2.0 s quiet 91CHECK (r .ms == 1050 ); 92 } 93// 5. the counter quiet from the start: the least wait is the quiet time 94 { 95Fake f ; 96f .reading = [](int ) {return ok (4242 ); }; 97const Result r = f .run (2.0 ,10.0 ); 98CHECK (r .why == Result ::Quiet ); 99CHECK (near (r .waited_s ,2.0 ,0.21 )); 100CHECK (r .ms == 4242 ); 101Fake g ; 102g .reading = [](int ) {return ok (0 ); };// a process that was never evicted 103CHECK (g .run (2.0 ,10.0 ).why == Result ::Quiet ); 104 } 105// 6. the cap: a counter that never stops ends the wait at the cap 106 { 107Fake f ; 108f .reading = [](int i ) {return ok (i * 5 ); }; 109const Result r = f .run (2.0 ,10.0 ); 110CHECK (r .why == Result ::Cap ); 111CHECK (r .waited_s >=10.0 && r .waited_s < 10.3 ); 112// quiet is judged before the cap: quiet exactly at the cap counts as quiet 113Fake g ; 114g .reading = [](int i ) {return ok (i < 40 ?i :40 ); };// moves until 8.0 s, quiet from then: 10.0 s 115const Result q = g .run (2.0 ,10.0 ); 116CHECK (q .why == Result ::Quiet ); 117 } 118#if defined(__linux__ ) 119// the folder reader: sum over stats_*, missing, unreadable 120 { 121char tmpl []= "/tmp/kfd_settle_test_XXXXXX" ; 122const char * dir = mkdtemp (tmpl ); 123CHECK (dir != nullptr ); 124if (dir != nullptr ) { 125const std::string d = dir ; 126CHECK (read_own_evicted_ms (d + "/no_such_folder" ).kind == Reading ::Missing ); 127CHECK (read_own_evicted_ms (d ).kind == Reading ::Missing );// an empty folder 128mkdir ((d + "/stats_1" ).c_str (),0700 ); 129CHECK (read_own_evicted_ms (d ).kind == Reading ::Missing );// a GPU folder without the file 130put (d + "/stats_1/evicted_ms" ,"12\n" ); 131Reading r = read_own_evicted_ms (d ); 132CHECK (r .kind == Reading ::Ok && r .ms == 12 ); 133mkdir ((d + "/stats_2" ).c_str (),0700 ); 134put (d + "/stats_2/evicted_ms" ,"30" ); 135r = read_own_evicted_ms (d ); 136CHECK (r .kind == Reading ::Ok && r .ms == 42 );// two GPUs: the sum 137put (d + "/stats_2/evicted_ms" ,"not a number\n" ); 138CHECK (read_own_evicted_ms (d ).kind == Reading ::Unreadable ); 139put (d + "/stats_2/evicted_ms" ,"" ); 140CHECK (read_own_evicted_ms (d ).kind == Reading ::Unreadable ); 141 std::remove ((d + "/stats_1/evicted_ms" ).c_str ()); 142 std::remove ((d + "/stats_2/evicted_ms" ).c_str ()); 143rmdir ((d + "/stats_1" ).c_str ()); 144rmdir ((d + "/stats_2" ).c_str ()); 145rmdir (d .c_str ()); 146 } 147// this process's own folder: whatever the box has, the call returns (it is Missing on a box without KFD) 148 (void )read_own_evicted_ms (own_proc_dir ()); 149 } 150#endif 151// the engine's call: with the flag down (the default) it returns at once 152 { 153CHECK (!kfd_settle_flag ().load ()); 154const auto t0 = std::chrono::steady_clock::now (); 155settle_before_ready (); 156CHECK (std::chrono::duration < double > (std::chrono::steady_clock::now ()- t0 ).count ()< 0.5 ); 157 } 158if (failed == 0 ) std::printf ("kfd_settle_test: ok\n" ); 159return failed == 0 ?0 :1 ; 160}