okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
e1a17f116
main
1// keeping the KFD quiet on an integrated gfx11 GPU under Linux (include/strata/core/kfd_quiet.hpp). No GPU: the switch rule and the 2// load order's rule. (The device rule itself is igpu_kfd_settle_default, checked in src/core/arch_defaults_test.cpp.) 3#include "strata/core/kfd_quiet.hpp" 4 5#include <cstdio> 6#include <string> 7#include <utility> 8#include <vector> 9 10#if defined(__linux__ ) 11#include <sys/stat.h> 12#include <unistd.h> 13#include <cstdlib> 14#endif 15 16using namespace strata::core::kfd_quiet ; 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 22void switches () { 23CHECK (switch_on (nullptr ,nullptr )); 24CHECK (switch_on ("1" ,nullptr )); 25CHECK (switch_on ("" ,"" ));// empty = unset 26CHECK (!switch_on ("0" ,nullptr ));// the kill switch turns everything off 27CHECK (!switch_on ("0" ,"1" ));// ... also when the fix's own switch says 1 28CHECK (!switch_on (nullptr ,"0" ));// one fix off 29CHECK (!switch_on ("1" ,"0" )); 30CHECK (switch_on ("yes" ,"on" )); 31CHECK (std::string (fix_env (Fix ::Load ))== "STRATA_KFD_QUIET_LOAD" ); 32CHECK (std::string (fix_env (Fix ::Exit ))== "STRATA_KFD_QUIET_EXIT" ); 33CHECK (std::string (fix_env (Fix ::Xfer ))== "STRATA_KFD_QUIET_XFER" ); 34} 35 36void load_order () { 37// not the class: none of the three moves, whatever the switches say (every other card keeps the old order) 38for (const char * all : {(const char * )nullptr ,"1" ,"0" }) 39for (const char * one : {(const char * )nullptr ,"1" ,"0" }) { 40const LoadMoves m = load_moves (false,all ,one ); 41CHECK (!m .hc_check_early && !m .settle_after_cache && !m .first_prompt_buffers && !m .cpu_share_buffers && !m .staged_copy_warm ); 42 } 43// the class, nothing set: all of them 44 { 45const LoadMoves m = load_moves (true,nullptr ,nullptr ); 46CHECK (m .hc_check_early && m .settle_after_cache && m .first_prompt_buffers && m .cpu_share_buffers && m .staged_copy_warm ); 47 } 48CHECK (kStagedWarmBytes > (1u <<20 ));// above the runtime's own staging threshold 49// the class, STRATA_KFD_QUIET=0 or STRATA_KFD_QUIET_LOAD=0: the old order (the A/B arm of the same binary) 50using P = std::pair < const char * ,const char *> ; 51for (const P & p : std::vector < P > {P {"0" ,nullptr },P {nullptr ,"0" },P {"0" ,"0" },P {"0" ,"1" }}) { 52const LoadMoves m = load_moves (true,p .first ,p .second ); 53CHECK (!m .hc_check_early && !m .settle_after_cache && !m .first_prompt_buffers && !m .cpu_share_buffers && !m .staged_copy_warm ); 54 } 55// the flag as the engine sets it: off until the card is named 56CHECK (!class_flag ().load ()); 57CHECK (!on (Fix ::Load )&& !on (Fix ::Exit )); 58} 59// the class from the topology text, as /sys/class/kfd/kfd/topology/nodes/N/properties reads (shortened) 60const char * kCpuNode = "cpu_cores_count 16\nsimd_count 0\nmem_banks_count 1\ngfx_target_version 0\nvendor_id 0\n" ; 61const char * k780M = "cpu_cores_count 0\nsimd_count 24\nmem_banks_count 1\ngfx_target_version 110003\nvendor_id 4098\ndevice_id 5567\n" ; 62const char * k890M = "simd_count 32\ngfx_target_version 110500\n" ; 63const char * k8060S = "simd_count 80\r\ngfx_target_version 110501\r\n" ;// \r tolerated: the number ends at the first non-digit 64const char * k7900 = "simd_count 192\ngfx_target_version 110000\n" ;// a discrete RX 7900 XTX 65const char * kR9700 = "simd_count 128\ngfx_target_version 120001\n" ; 66 67void topology () { 68 { 69const Node n = parse_node_properties (k780M ); 70CHECK (n .gfx == 110003 && n .simds == 24 ); 71const Node c = parse_node_properties (kCpuNode ); 72CHECK (c .gfx == 0 && c .simds == 0 ); 73CHECK (parse_node_properties (k8060S ).gfx == 110501 ); 74CHECK (parse_node_properties ("" ).gfx == 0 ); 75CHECK (parse_node_properties ("gfx_target_version\nsimd_count x\n" ).simds == 0 );// no number: not read 76CHECK (parse_node_properties ("xgfx_target_version 110003\n" ).gfx == 0 );// another key 77 } 78auto cls = [](std::vector < const char *> nodes ) { 79 std::vector < Node > v ; 80for (const char * t :nodes )v .push_back (parse_node_properties (t )); 81return nodes_in_class (v ); 82 }; 83CHECK (cls ({kCpuNode ,k780M })); 84CHECK (cls ({kCpuNode ,k890M })); 85CHECK (cls ({kCpuNode ,k8060S })); 86CHECK (!cls ({}));// no topology (no KFD, another OS, a container without /sys/class/kfd) 87CHECK (!cls ({kCpuNode }));// no GPU node 88CHECK (!cls ({kCpuNode ,k7900 }));// a discrete gfx11 89CHECK (!cls ({kCpuNode ,kR9700 })); 90CHECK (!cls ({kCpuNode ,k780M ,k7900 }));// a 780M beside a discrete card: not the class (the card keeps its path) 91CHECK (!cls ({kCpuNode ,k890M ,kR9700 })); 92// the value: only in the class, only with the switches on, never over the caller's own value 93CHECK (xfer_value (true,nullptr ,nullptr ,nullptr )!= nullptr && std::string (xfer_value (true,nullptr ,nullptr ,nullptr ))== "1000000" ); 94CHECK (xfer_value (false,nullptr ,nullptr ,nullptr )== nullptr ); 95CHECK (xfer_value (true,"0" ,nullptr ,nullptr )== nullptr ); 96CHECK (xfer_value (true,nullptr ,"0" ,nullptr )== nullptr ); 97CHECK (xfer_value (true,nullptr ,nullptr ,"4" )== nullptr ); 98CHECK (xfer_value (true,nullptr ,nullptr ,"" )== nullptr );// set, even empty: the caller's 99#if defined(__linux__ ) 100// the folder reader on a temporary tree: two nodes, a stray file, a node without properties 101char tmpl []= "/tmp/kfdqXXXXXX" ; 102if (mkdtemp (tmpl )!= nullptr ) { 103const std::string root = tmpl ; 104auto put = [](const std::string & path ,const char * text ) { 105if (std::FILE * f = std::fopen (path .c_str (),"w" )) { std::fputs (text ,f ); std::fclose (f ); } 106 }; 107mkdir ((root + "/0" ).c_str (),0700 ); 108mkdir ((root + "/1" ).c_str (),0700 ); 109mkdir ((root + "/2" ).c_str (),0700 ); 110put (root + "/0/properties" ,kCpuNode ); 111put (root + "/1/properties" ,k780M ); 112put (root + "/README" ,"not a node" ); 113const std::vector < Node > v = read_topology (root ); 114CHECK (v .size ()== 2 ); 115CHECK (nodes_in_class (v )); 116CHECK (nodes_in_noreg_class (v )); 117put (root + "/2/properties" ,k7900 ); 118CHECK (!nodes_in_class (read_topology (root ))); 119CHECK (!nodes_in_noreg_class (read_topology (root ))); 120CHECK (read_topology (root + "/none" ).empty ()); 121for (const char * p : {"/0/properties" ,"/1/properties" ,"/2/properties" ,"/README" }) std::remove ((root + p ).c_str ()); 122for (const char * p : {"/0" ,"/1" ,"/2" ,"" })rmdir ((root + p ).c_str ()); 123 }else { 124 std::fprintf (stderr ,"FAIL %s:%d: mkdtemp failed\n" ,__FILE__ ,__LINE__ ); 125++ failed ; 126 } 127#endif 128} 129 130// a fake GPU layer for the stop: a clock in seconds, a GPU that finishes after `busy_s` once released (never when `hung`) 131struct FakeGpu { 132double t = 0.0 ; 133double busy_s = 0.5 ; 134bool hung = false; 135bool stopped = false,unregistered = false; 136 std::string order ; 137TeardownResult run (double bound ) { 138auto stop = [& ] {stopped = true;order += "S" ; }; 139auto wait_for = [& ](int ms ) {// waits until done or ms passed; true when done 140const double lim = ms /1000.0 ; 141if (!hung && busy_s <=lim ) {t += busy_s ;busy_s = 0.0 ;return true; } 142t += lim ; 143if (!hung )busy_s -= lim ; 144return false; 145 }; 146auto release = [& ](int ms ) {order += "R" ;return wait_for (ms ); }; 147auto drain = [& ](int ms ) {order += "D" ;return wait_for (ms ); }; 148auto unreg = [& ] {unregistered = true;order += "U" ; }; 149return teardown (stop ,release ,drain ,unreg , [& ] {return t ; },bound ); 150 } 151}; 152 153void stop_order () { 154// a GPU that finishes in 0.5 s: stop, release, drain, unregister; inside the bound 155 { 156FakeGpu g ; 157const TeardownResult r = g .run (kExitBoundS ); 158CHECK (g .order == "SRDU" ); 159CHECK (r .released && r .drained && r .unregistered && g .unregistered ); 160CHECK (g .t <=kExitBoundS + 1e-9 ); 161 } 162// a GPU that takes 2.5 s: the release uses most of the bound, the drain the rest; still drained 163 { 164FakeGpu g ; 165g .busy_s = 2.5 ; 166const TeardownResult r = g .run (kExitBoundS ); 167CHECK (r .released && r .drained && r .unregistered ); 168CHECK (g .t <=kExitBoundS + 1e-9 ); 169 } 170// a GPU that takes 5 s: not drained within 3 s: the registration stays (unregistering under a running copy is worse), and 171// the whole stop took no more than the bound 172 { 173FakeGpu g ; 174g .busy_s = 5.0 ; 175const TeardownResult r = g .run (kExitBoundS ); 176CHECK (!r .released && !r .drained && !r .unregistered && !g .unregistered ); 177CHECK (g .order == "SRD" ); 178CHECK (g .t <=kExitBoundS + 0.002 ); 179 } 180// a hung GPU: the same, bounded 181 { 182FakeGpu g ; 183g .hung = true; 184const TeardownResult r = g .run (kExitBoundS ); 185CHECK (!r .drained && !g .unregistered && g .stopped ); 186CHECK (g .t <=kExitBoundS + 0.002 ); 187 } 188// the bound stays inside the server's 5 s between SIGTERM and SIGKILL (serve/server.py ENGINE_TERM_WAIT_S) 189CHECK (kExitBoundS > 0.0 && kExitBoundS < 5.0 ); 190// the first signal starts the stop, a second one takes the default action at once 191CHECK (on_signal (0 )== OnSignal ::StartTeardown ); 192CHECK (on_signal (1 )== OnSignal ::DefaultAction ); 193CHECK (on_signal (2 )== OnSignal ::DefaultAction ); 194} 195 196void noreg () { 197CHECK (std::string (fix_env (Fix ::NoReg ))== "STRATA_KFD_QUIET_NOREG" ); 198// the class default: unregistered, unless a switch says 0 or STRATA_UMA_REGISTER=1 asks for the registration 199CHECK (noreg_default (true,nullptr ,nullptr ,nullptr )); 200CHECK (noreg_default (true,"1" ,"1" ,nullptr )); 201CHECK (noreg_default (true,nullptr ,nullptr ,"0" )); 202CHECK (!noreg_default (false,nullptr ,nullptr ,nullptr ));// every other card keeps its registered arena 203CHECK (!noreg_default (false,"1" ,"1" ,nullptr )); 204CHECK (!noreg_default (true,"0" ,nullptr ,nullptr ));// STRATA_KFD_QUIET=0 205CHECK (!noreg_default (true,nullptr ,"0" ,nullptr ));// STRATA_KFD_QUIET_NOREG=0 206CHECK (!noreg_default (true,nullptr ,nullptr ,"1" ));// STRATA_UMA_REGISTER=1 207// NOREG's own class: the Radeon 780M class only (gfx1103 = 110003); Strix Point, Strix Halo and discrete cards stay registered 208auto noreg_cls = [](const std::vector < const char *>& texts ) { 209 std::vector < Node > v ; 210for (const char * t :texts )v .push_back (parse_node_properties (t )); 211return nodes_in_noreg_class (v ); 212 }; 213auto noreg_on = [& ](const std::vector < const char *>& texts ) {return noreg_default (noreg_cls (texts ),nullptr ,nullptr ,nullptr ); }; 214CHECK (gfx_in_noreg_class (110003 )); 215CHECK (!gfx_in_noreg_class (110500 )); 216CHECK (!gfx_in_noreg_class (110501 )); 217CHECK (!gfx_in_noreg_class (110000 )); 218CHECK (!gfx_in_noreg_class (120001 )); 219CHECK (noreg_on ({kCpuNode ,k780M }));// 110003: unregistered 220CHECK (!noreg_on ({kCpuNode ,k890M }));// 110500: registered 221CHECK (!noreg_on ({kCpuNode ,k8060S }));// 110501: registered 222CHECK (!noreg_on ({kCpuNode ,k7900 }));// a discrete card: unchanged (registered) 223CHECK (!noreg_on ({kCpuNode ,kR9700 })); 224CHECK (!noreg_on ({kCpuNode ,k780M ,k7900 }));// a 780M beside a discrete card: registered, as before 225CHECK (!noreg_on ({kCpuNode ,k780M ,k890M }));// every GPU node must be a gfx1103 226CHECK (!noreg_on ({})); 227CHECK (!noreg_on ({kCpuNode })); 228// the switches still win on the 780M: STRATA_KFD_QUIET_NOREG=0 / STRATA_KFD_QUIET=0 / STRATA_UMA_REGISTER=1 register 229CHECK (!noreg_default (noreg_cls ({kCpuNode ,k780M }),nullptr ,"0" ,nullptr )); 230CHECK (!noreg_default (noreg_cls ({kCpuNode ,k780M }),"0" ,nullptr ,nullptr )); 231CHECK (!noreg_default (noreg_cls ({kCpuNode ,k780M }),nullptr ,nullptr ,"1" )); 232// the user's STRATA_ARENA_PIN_GIB wins over the class default both ways 233CHECK (arena_unregistered (nullptr ,noreg_on ({kCpuNode ,k780M }))); 234CHECK (!arena_unregistered ("0" ,noreg_on ({kCpuNode ,k780M }))); 235CHECK (!arena_unregistered (nullptr ,noreg_on ({kCpuNode ,k8060S }))); 236CHECK (arena_unregistered ("none" ,noreg_on ({kCpuNode ,k8060S }))); 237// LOAD / EXIT / XFER keep the whole integrated gfx11 class 238auto cls = [](const std::vector < const char *>& texts ) { 239 std::vector < Node > v ; 240for (const char * t :texts )v .push_back (parse_node_properties (t )); 241return nodes_in_class (v ); 242 }; 243CHECK (cls ({kCpuNode ,k780M })&& cls ({kCpuNode ,k890M })&& cls ({kCpuNode ,k8060S })); 244// the arena's mode: the user's STRATA_ARENA_PIN_GIB wins both ways; unset or empty, the engine's flag 245CHECK (arena_unregistered (nullptr , true)); 246CHECK (arena_unregistered ("" , true)); 247CHECK (!arena_unregistered (nullptr , false)); 248CHECK (arena_unregistered ("none" , false)); 249CHECK (arena_unregistered ("none" , true)); 250CHECK (!arena_unregistered ("0" , true));// the user registers the whole arena 251CHECK (!arena_unregistered ("8" , true));// ... or a capped part of it 252CHECK (!arena_unregistered ("auto" , true)); 253// the lock: never tried in the class default; a STRATA_ARENA_LOCK the user set decides; outside NOREG as before (lock unless "0") 254CHECK (!arena_lock_wanted (true,nullptr )); 255CHECK (arena_lock_wanted (true,"1" )); 256CHECK (!arena_lock_wanted (true,"0" )); 257CHECK (arena_lock_wanted (false,nullptr )); 258CHECK (arena_lock_wanted (false,"1" )); 259CHECK (arena_lock_wanted (false,"" )); 260CHECK (!arena_lock_wanted (false,"0" )); 261} 262 263void lend () { 264CHECK (std::string (fix_env (Fix ::Lend ))== "STRATA_KFD_QUIET_LEND" ); 265// the switch: written off by default; _LEND=1 on, =0 / STRATA_KFD_QUIET=0 / STRATA_PREFILL_BORROW=1 off; never outside the class 266CHECK (!kNoLendDefaultOn ); 267CHECK (lend_rule_on (true,nullptr ,nullptr ,nullptr )== kNoLendDefaultOn ); 268CHECK (lend_rule_on (true,nullptr ,nullptr ,nullptr , true));// the constant flipped: the class default 269CHECK (!lend_rule_on (true,nullptr ,nullptr ,nullptr , false)); 270CHECK (lend_rule_on (true,nullptr ,"1" ,nullptr )); 271CHECK (lend_rule_on (true,"1" ,"1" ,nullptr )); 272CHECK (!lend_rule_on (true,nullptr ,"0" ,nullptr , true)); 273CHECK (!lend_rule_on (true,"0" ,"1" ,nullptr , true)); 274CHECK (!lend_rule_on (true,nullptr ,"1" ,"1" , true));// STRATA_PREFILL_BORROW=1 keeps the loan 275CHECK (lend_rule_on (true,nullptr ,"1" ,"0" )); 276CHECK (!lend_rule_on (false,nullptr ,"1" ,nullptr , true));// not the class 277// the pick: the chunk always comes back as it went in (8192 stays 8192: no drop to 2048) 278const int64_t own8k = 2560 ;// about the 780M's own buffers at 8192 (2.5 GiB) 279for (int64_t c : {(int64_t )512 , (int64_t )2048 , (int64_t )8192 , (int64_t )16384 , (int64_t )32768 }) { 280CHECK (no_lend_pick (true, true, true,c ,100000 ,own8k ).chunk == c ); 281CHECK (no_lend_pick (true, true, true,c ,0 ,own8k ).chunk == c ); 282CHECK (no_lend_pick (false, true, true,c ,100000 ,own8k ).chunk == c ); 283 } 284// the fit check decides: room >= own + kNoLendKeepMib 285CHECK (no_lend_pick (true, true, true,8192 ,own8k + kNoLendKeepMib ,own8k ).no_lend ); 286CHECK (!no_lend_pick (true, true, true,8192 ,own8k + kNoLendKeepMib - 1 ,own8k ).no_lend ); 287CHECK (!no_lend_pick (true, true, true,8192 ,own8k + kNoLendKeepMib - 1 ,own8k ).fits ); 288CHECK (no_lend_pick (true, true, true,8192 ,18000 ,own8k ).no_lend );// p2006: ~18 GiB free beside a 7.72 GiB cache (G) 289CHECK (!no_lend_pick (true, true, true,8192 ,0 ,own8k ).no_lend ); 290// only where the path would lend, on one GPU, a chunk of 1 .. kNoLendMaxChunk, with the rule on 291CHECK (!no_lend_pick (false, true, true,8192 ,100000 ,own8k ).no_lend ); 292CHECK (!no_lend_pick (true, false, true,8192 ,100000 ,own8k ).no_lend ); 293CHECK (!no_lend_pick (true, true, false,8192 ,100000 ,own8k ).no_lend ); 294CHECK (!no_lend_pick (true, true, true,0 ,100000 ,own8k ).no_lend ); 295CHECK (no_lend_pick (true, true, true,kNoLendMaxChunk ,100000 ,own8k ).no_lend ); 296CHECK (!no_lend_pick (true, true, true,kNoLendMaxChunk + 1 ,100000 ,own8k ).no_lend ); 297} 298}// namespace 299 300int main () { 301switches (); 302noreg (); 303lend (); 304load_order (); 305topology (); 306stop_order (); 307if (failed ) { 308 std::fprintf (stderr ,"kfd_quiet_test: %d check(s) failed\n" ,failed ); 309return 1 ; 310 } 311 std::printf ("kfd_quiet_test: ok\n" ); 312return 0 ; 313}