okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
466626cbb
main
1// The GPU pause counter (strata/core/gpu_pause_time.hpp): the reader against a fake KFD tree (present, missing, 2// unreadable, garbage, several GPUs) and the two lines. No GPU, no driver. Off Linux the reader is always "not 3// available", which is checked instead. 4#include "strata/core/gpu_pause_time.hpp" 5 6#include <chrono> 7#include <cstdio> 8#include <cstdlib> 9#include <string> 10 11#if defined(__linux__ ) 12#include <sys/stat.h> 13#include <unistd.h> 14#endif 15 16using namespace strata::core ; 17 18namespace { 19int failed = 0 ; 20#define CHECK (x ) do { if (!(x)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #x); ++failed; } } while (0) 21 22#if defined(__linux__ ) 23void put (const std::string & path ,const char * text ) { 24 std::FILE * f = std::fopen (path .c_str (),"w" ); 25if (f == nullptr ) { std::fprintf (stderr ,"cannot write %s\n" ,path .c_str ());++ failed ;return ; } 26 std::fputs (text ,f ); 27 std::fclose (f ); 28} 29void gpu (const std::string & proc ,const char * id ,const char * text ) { 30const std::string d = proc + "/stats_" + id ; 31mkdir (d .c_str (),0755 ); 32if (text != nullptr )put (d + "/evicted_ms" ,text ); 33} 34#endif 35}// namespace 36 37int main () { 38#if defined(__linux__ ) 39char tmpl []= "/tmp/gpu_pause_test_XXXXXX" ; 40const char * root = mkdtemp (tmpl ); 41CHECK (root != nullptr ); 42if (root == nullptr )return 1 ; 43const std::string r = root ; 44auto proc = [& ](long pid ) { 45const std::string p = r + "/" + std::to_string (pid ); 46mkdir (p .c_str (),0755 ); 47return p ; 48 }; 49 50CHECK (gpu_pause_ms (r .c_str (),1 )== -1 );// no folder for the process: not available 51proc (2 ); 52CHECK (gpu_pause_ms (r .c_str (),2 )== -1 );// a folder without GPUs 53put (proc (2 )+ "/pasid" ,"32770\n" );// other entries are ignored 54CHECK (gpu_pause_ms (r .c_str (),2 )== -1 ); 55 56gpu (proc (3 ),"41945" ,"954\n" );// one GPU, as the driver writes it 57CHECK (gpu_pause_ms (r .c_str (),3 )== 954 ); 58gpu (proc (3 ),"17288" ,"0\n" );// a second GPU: summed 59CHECK (gpu_pause_ms (r .c_str (),3 )== 954 ); 60put (proc (3 )+ "/stats_17288/evicted_ms" ,"15\n" ); 61CHECK (gpu_pause_ms (r .c_str (),3 )== 969 ); 62put (proc (3 )+ "/stats_17288/cu_occupancy" ,"12\n" );// other counters beside it are not read 63CHECK (gpu_pause_ms (r .c_str (),3 )== 969 ); 64 65gpu (proc (4 ),"1" ,nullptr );// a GPU folder without the counter: not available 66CHECK (gpu_pause_ms (r .c_str (),4 )== -1 ); 67gpu (proc (5 ),"1" ,"12" );// no newline is fine 68CHECK (gpu_pause_ms (r .c_str (),5 )== 12 ); 69const char * garbage []= {"" ,"\n" ,"abc\n" ,"-5\n" ,"12 ms\n" ,"1.5\n" ,"99999999999999999999999\n" }; 70long pid = 10 ; 71for (const char * g :garbage ) { 72gpu (proc (pid ),"1" ,g ); 73CHECK (gpu_pause_ms (r .c_str (),pid )== -1 ); 74++ pid ; 75 } 76gpu (proc (30 ),"1" ,"100\n" );// one good GPU and one bad: not available 77gpu (proc (30 ),"2" ,"x\n" ); 78CHECK (gpu_pause_ms (r .c_str (),30 )== -1 ); 79 80gpu (proc (40 ),"1" ,"77\n" );// unreadable (skipped when run as root) 81const std::string locked = r + "/40/stats_1/evicted_ms" ; 82chmod (locked .c_str (),0 ); 83if (geteuid ()!= 0 )CHECK (gpu_pause_ms (r .c_str (),40 )== -1 ); 84chmod (locked .c_str (),0644 ); 85CHECK (gpu_pause_ms (r .c_str (),40 )== 77 ); 86 87// the real tree for this process: whatever it is, it is a number or "not available", and it does not throw 88const int64_t self = gpu_pause_ms_self (); 89CHECK (self >=-1 ); 90 std::printf ("this process on this machine: %lld\n" , (long long )self ); 91 92// no KFD tree (a CUDA box, a container without sysfs): not available, no line, and it costs one failed opendir. 93// 10,000 reads: the mean is printed; the bound (50 us each) only catches something gone badly wrong 94 { 95const std::string none = r + "/no-kfd-here" ; 96const auto t0 = std::chrono::steady_clock::now (); 97int64_t v = 0 ; 98for (int i = 0 ;i < 10000 ;++ i )v += gpu_pause_ms (none .c_str (),1234 ); 99const double us = std::chrono::duration < double , std::micro > (std::chrono::steady_clock::now ()- t0 ).count () /10000 ; 100CHECK (v == -10000 ); 101CHECK (gpu_pause_request_line (gpu_pause_ms (none .c_str (),1234 ),gpu_pause_ms (none .c_str (),1234 )).empty ()); 102CHECK (gpu_pause_stall_line (gpu_pause_ms (none .c_str (),1234 ),-1 ,-1 ).empty ()); 103CHECK (us < 50.0 ); 104 std::printf ("no KFD tree: %.2f us per read\n" ,us ); 105// the present tree with two GPUs, for comparison (the real sysfs read costs more: measured on p620) 106const auto t1 = std::chrono::steady_clock::now (); 107for (int i = 0 ;i < 10000 ;++ i )v += gpu_pause_ms (r .c_str (),3 ); 108 std::printf ("fake tree, 2 GPUs: %.2f us per read\n" , 109 std::chrono::duration < double , std::micro > (std::chrono::steady_clock::now ()- t1 ).count () /10000 ); 110 } 111 112const std::string rm = "rm -rf '" + r + "'" ; 113if (std::system (rm .c_str ())!= 0 ) std::fprintf (stderr ,"could not remove %s\n" ,r .c_str ()); 114#else 115CHECK (gpu_pause_ms ("C:/nowhere" ,1 )== -1 ); 116CHECK (gpu_pause_ms_self ()== -1 ); 117#endif 118 119// the stall report's line 120CHECK (gpu_pause_stall_line (-1 ,5 ,-1 ).empty ());// not available: no line 121CHECK (gpu_pause_stall_line (-1 ,-1 ,7 ).empty ()); 122CHECK (gpu_pause_stall_line (6123 ,5169 ,-1 )== 123" GPU work paused by the graphics driver: 6123 ms since the engine started, 954 ms in this request (the AMD " 124"driver pauses all of the engine's GPU work while Linux moves memory the GPU uses)" ); 125CHECK (gpu_pause_stall_line (8073 ,5169 ,6123 )== 126" GPU work paused by the graphics driver: 8073 ms since the engine started, 2904 ms in this request, 1950 ms " 127"in the last 2 s" ); 128CHECK (gpu_pause_stall_line (40 ,-1 ,-1 )== // between requests: no request part 129" GPU work paused by the graphics driver: 40 ms since the engine started (the AMD driver pauses all of the " 130"engine's GPU work while Linux moves memory the GPU uses)" ); 131CHECK (gpu_pause_stall_line (40 ,-1 ,40 )== 132" GPU work paused by the graphics driver: 40 ms since the engine started, 0 ms in the last 2 s" ); 133 134// the request line: only at or above the threshold, and never from an unread or a reset counter 135CHECK (kGpuPauseReportMs == 100 ); 136CHECK (gpu_pause_request_line (-1 ,900 ).empty ()); 137CHECK (gpu_pause_request_line (100 ,-1 ).empty ()); 138CHECK (gpu_pause_request_line (100 ,50 ).empty ()); 139CHECK (gpu_pause_request_line (1000 ,1015 ).empty ());// the usual 15 ms: no line 140CHECK (gpu_pause_request_line (1000 ,1099 ).empty ()); 141CHECK (gpu_pause_request_line (1000 ,1100 )== 142"strata serve: the graphics driver paused the GPU for 100 ms during this request while Linux moved memory " 143"the GPU uses; the request was slower by up to that much" ); 144CHECK (gpu_pause_request_line (15 ,969 )== 145"strata serve: the graphics driver paused the GPU for 954 ms during this request while Linux moved memory " 146"the GPU uses; the request was slower by up to that much" ); 147 148// the gap line: the same bar, from READY or the end of the previous request to the next request's begin 149CHECK (gpu_pause_between_line (-1 ,900 ).empty ());// never idle yet / the read failed 150CHECK (gpu_pause_between_line (100 ,-1 ).empty ()); 151CHECK (gpu_pause_between_line (100 ,50 ).empty ());// a counter that went back (a new process): no line 152CHECK (gpu_pause_between_line (1000 ,1099 ).empty ()); 153CHECK (gpu_pause_between_line (1000 ,1100 )== 154"strata serve: the graphics driver paused the GPU for 100 ms between requests while Linux moved memory the GPU uses" ); 155 156// the bookkeeping over a sequence, as the request loop calls it 157 { 158GpuPauseIdle idle ; 159CHECK (idle .begin (500 ).empty ());// before READY was read: nothing to compare with 160idle .idle (500 );// READY 161CHECK (idle .begin (560 ).empty ());// 60 ms before the first request: below the bar 162idle .idle (900 );// the first request ended (it paused 340 ms inside: its own line) 163CHECK (idle .begin (900 ).empty ());// the next one began at once: no gap 164idle .idle (950 );// it ended 165CHECK (idle .begin (1450 )== // 500 ms of driver pause while idle 166"strata serve: the graphics driver paused the GPU for 500 ms between requests while Linux moved memory the GPU uses" ); 167CHECK (idle .begin (1450 )== idle .begin (1450 ));// begin() does not move the mark: only idle() does 168idle .idle (-1 );// a failed read at an end: no gap is made from unrelated counters 169CHECK (idle .begin (5000 ).empty ()); 170idle .idle (5000 ); 171CHECK (idle .begin (5099 ).empty ()); 172CHECK (!idle .begin (5100 ).empty ()); 173 } 174 175if (failed ) { std::fprintf (stderr ,"gpu_pause_time_test: %d failed\n" ,failed );return 1 ; } 176 std::printf ("gpu_pause_time_test: ok\n" ); 177return 0 ; 178}