okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b749503db
main
1// card_node_test: when the engine restricts itself to the graphics card's NUMA node (include/strata/kernels/cpu/card_node.hpp). 2// The decision on made-up machines, the sysfs / proc readers on a fake tree in a temp directory, and (Linux) the restriction 3// of a two-thread process to a subset of its own CPUs. No GPU, no model. 4#include "strata/kernels/cpu/card_node.hpp" 5 6#include <atomic> 7#include <chrono> 8#include <cstdio> 9#include <ctime> 10#include <filesystem> 11#include <fstream> 12#include <string> 13#include <thread> 14#include <vector> 15 16namespace cpu= strata::kernels::cpu; 17namespace fs= std::filesystem; 18 19static int g_fail = 0 ; 20 21static void expect (bool ok ,const std::string & what ) { 22if (!ok ) { 23++ g_fail ; 24 std::fprintf (stderr ,"FAIL: %s\n" ,what .c_str ()); 25 } 26} 27 28// A two-socket machine, one node per socket, 2-way SMT: node k has cores [0, cores_k), CPU numbers as Linux gives them 29// (node 0's first threads, node 1's first threads, then node 0's siblings, node 1's siblings). 30static std::vector < cpu::CardNodeCpu > machine (int cores0 ,int cores1 ) { 31 std::vector < cpu::CardNodeCpu > m ; 32int n = 0 ; 33for (int pass = 0 ;pass < 2 ;++ pass ) { 34for (int c = 0 ;c < cores0 ;++ c )m .push_back ({n ++ ,0 ,0 ,c }); 35for (int c = 0 ;c < cores1 ;++ c )m .push_back ({n ++ ,1 ,1 ,c }); 36 } 37return m ; 38} 39 40static std::vector < int > cpus_of (const std::vector < cpu::CardNodeCpu >& m ,int node ) { 41 std::vector < int > out ; 42for (const auto & c :m ) 43if (c .node == node )out .push_back (c .cpu ); 44return out ; 45} 46 47// The RTX 4090 pod's shape: 2 x 32 cores, 128 logical, a 13.6-CPU quota (12 workers), card on node 1, plenty of free memory. 48static cpu::CardNodeInput pod () { 49 cpu::CardNodeInput in ; 50in .nodes_with_cpus = 2 ; 51in .mask = machine (32 ,32 ); 52in .mems_known = true; 53in .mems_allowed = {0 ,1 }; 54in .card_nodes = {1 }; 55in .card_node_free_bytes = 100ll <<30 ; 56in .pinned_need_bytes = cpu::card_node_pinned_need (131072 ); 57in .cpu_quota = 13.6 ; 58in .today_workers = 12 ; 59return in ; 60} 61 62static void decisions () { 63// two nodes, the card on node 1: restricted to node 1's 64 logical CPUs, the pool keeps the quota's 12 workers 64 { 65const auto d = cpu::decide_card_node (pod ()); 66expect (d .on && d .why .empty (),"pod: restricted (" + d .why + ")" ); 67expect (d .node == 1 ,"pod: node 1" ); 68expect (d .cpus == cpus_of (machine (32 ,32 ),1 )&& d .cpus .size ()== 64 ,"pod: node 1's 64 CPUs" ); 69expect (d .node_cores == 32 && d .node_workers == 12 ,"pod: 32 cores, 12 workers on the node (quota)" ); 70 } 71// the card on node 0 72 { 73auto in = pod (); 74in .card_nodes = {0 }; 75const auto d = cpu::decide_card_node (in ); 76expect (d .on && d .node == 0 && d .cpus == cpus_of (in .mask ,0 ),"card on node 0: node 0's CPUs" ); 77 } 78// one node: nothing 79 { 80auto in = pod (); 81in .nodes_with_cpus = 1 ; 82const auto d = cpu::decide_card_node (in ); 83expect (!d .on && d .why == "one NUMA node" && d .cpus .empty (),"one node: left as is" ); 84 } 85// the switch off 86 { 87auto in = pod (); 88in .switch_off = true; 89const auto d = cpu::decide_card_node (in ); 90expect (!d .on && d .why == "STRATA_NUMA_CARD=0" ,"switch off: left as is" ); 91 } 92// the card's node not known (sysfs -1 or unreadable) 93 { 94auto in = pod (); 95in .card_nodes = {-1 }; 96expect (!cpu::decide_card_node (in ).on ,"card node -1: left as is" ); 97in .card_nodes = {}; 98expect (!cpu::decide_card_node (in ).on ,"no card: left as is" ); 99 } 100// two cards on different nodes; two on the same node 101 { 102auto in = pod (); 103in .card_nodes = {1 ,0 }; 104const auto d = cpu::decide_card_node (in ); 105expect (!d .on && d .why == "the cards sit on different NUMA nodes" ,"cards on nodes 1 and 0: left as is" ); 106in .card_nodes = {1 ,1 }; 107expect (cpu::decide_card_node (in ).on ,"two cards on node 1: restricted" ); 108in .card_nodes = {1 ,-1 }; 109expect (!cpu::decide_card_node (in ).on ,"one card's node unknown: left as is" ); 110 } 111// a process already restricted to one node (taskset / numactl / cpuset): left as is, also when it is the other node 112 { 113auto in = pod (); 114 std::vector < cpu::CardNodeCpu > one ; 115for (const auto & c :in .mask ) 116if (c .node == 0 )one .push_back (c ); 117in .mask = one ; 118const auto d = cpu::decide_card_node (in ); 119expect (!d .on && d .why == "the process already runs on one NUMA node" ,"pre-restricted to node 0: left as is" ); 120 std::vector < cpu::CardNodeCpu > part ;// a subset that still spans both nodes (a cpuset over both): the rule applies 121for (const auto & c :pod ().mask ) 122if (c .cpu %4 == 0 )part .push_back (c ); 123in .mask = part ; 124in .today_workers = 7 ;// that mask's own pool: 16 cores in all, the host's socket's 8 -> 7 workers 125const auto e = cpu::decide_card_node (in ); 126expect (e .on && e .cpus .size ()== 16 && e .node_workers == 7 ,"a mask over both nodes, 16 of node 1's CPUs: restricted to those 16" ); 127in .today_workers = 12 ; 128expect (!cpu::decide_card_node (in ).on ,"the same mask with 12 workers today: 7 on the node, left as is" ); 129 } 130// a CPU whose node is not known 131 { 132auto in = pod (); 133in .mask [5 ].node = -1 ; 134expect (!cpu::decide_card_node (in ).on ,"a CPU without a node: left as is" ); 135 } 136// a node with too few CPUs for the pool: 4 + 60 cores, no quota, today's pool spans both sockets: 32 workers 137 { 138 cpu::CardNodeInput in ; 139in .nodes_with_cpus = 2 ; 140in .mask = machine (4 ,60 ); 141in .card_nodes = {0 }; 142in .card_node_free_bytes = 100ll <<30 ; 143in .today_workers = 32 ; 144const auto d = cpu::decide_card_node (in ); 145expect (!d .on && d .node_workers == 3 && d .cpus .empty (),"card on a 4-core node, 32 workers today: left as is (" + d .why + ")" ); 146in .card_nodes = {1 }; 147const auto e = cpu::decide_card_node (in ); 148expect (e .on && e .node_workers == 32 ,"card on the 60-core node: restricted, 32 workers" ); 149// a 16-core node against 32 spread: 16 pinned only matched 32 spread in the measurement, so the count must not drop 150in .mask = machine (16 ,16 ); 151in .card_nodes = {1 }; 152in .today_workers = 30 ; 153expect (!cpu::decide_card_node (in ).on ,"2 x 16 cores, 30 workers today, 15 on a node: left as is" ); 154 } 155// --pool-workers N: the node needs N + 1 physical cores 156 { 157auto in = pod (); 158in .user_workers = in .today_workers = 31 ; 159expect (cpu::decide_card_node (in ).on ,"--pool-workers 31 on a 32-core node: restricted" ); 160in .user_workers = in .today_workers = 32 ; 161expect (!cpu::decide_card_node (in ).on ,"--pool-workers 32 on a 32-core node: left as is" ); 162in .user_workers = in .today_workers = 4 ; 163expect (cpu::decide_card_node (in ).on ,"--pool-workers 4: restricted" ); 164 } 165// a container quota smaller than the node: the pool is bounded by the quota either way and the rule does not move it 166 { 167 cpu::CardNodeInput in ; 168in .nodes_with_cpus = 2 ; 169in .mask = machine (96 ,96 );// the second RTX 5090 host: 2 x EPYC 9654, card on node 0, a 46-CPU quota 170in .card_nodes = {0 }; 171in .card_node_free_bytes = 100ll <<30 ; 172in .cpu_quota = 46 ; 173in .today_workers = 32 ; 174const auto d = cpu::decide_card_node (in ); 175expect (d .on && d .node_workers == 32 && d .cpus .size ()== 192 ,"quota 46 on 2 x 96 cores: restricted, 32 workers" ); 176in .cpu_quota = 17.55 ;// the first RTX 5090 host's quota: 16 workers either way 177in .today_workers = 16 ; 178const auto e = cpu::decide_card_node (in ); 179expect (e .on && e .node_workers == 16 ,"quota 17.55: restricted, 16 workers" ); 180// a quota larger than one node (2 x 8 cores, 30 CPUs): today 15 workers, a node 7: left as is 181in .mask = machine (8 ,8 ); 182in .cpu_quota = 30 ; 183in .today_workers = 15 ; 184expect (!cpu::decide_card_node (in ).on ,"quota above one node's CPUs: left as is" ); 185 } 186// memory: the process may not allocate on the card's node, or the node is short of free memory 187 { 188auto in = pod (); 189in .mems_allowed = {0 }; 190const auto d = cpu::decide_card_node (in ); 191expect (!d .on && d .why == "the process may not allocate memory on node 1" ,"mems_allowed without the card's node: left as is" ); 192in .mems_known = false;// not readable: not a reason to stop 193expect (cpu::decide_card_node (in ).on ,"mems_allowed not known: restricted" ); 194in = pod (); 195in .card_node_free_bytes = (int64_t ) (2.6 * 1073741824.0 );// the 2.6 GB free seen on one node of the first 5090 host 196const auto e = cpu::decide_card_node (in ); 197expect (!e .on && e .why .find ("MiB free, under the" )!= std::string::npos ,"2.6 GiB free on the card's node: left as is (" + e .why + ")" ); 198in .card_node_free_bytes = -1 ; 199expect (!cpu::decide_card_node (in ).on ,"free memory not known: left as is" ); 200in .card_node_free_bytes = (int64_t )in .pinned_need_bytes ; 201expect (cpu::decide_card_node (in ).on ,"free memory exactly the need: restricted" ); 202expect (cpu::card_node_pinned_need (131072 )== 131072ull * 12700 + (2ull <<30 ),"pinned need at 131,072 cells" ); 203expect (cpu::card_node_pinned_need (0 )== (2ull <<30 )&& cpu::card_node_pinned_need (-5 )== (2ull <<30 ),"pinned need without a context" ); 204 } 205// the person placed the threads; a hybrid CPU; the process has no CPU on the card's node 206 { 207auto in = pod (); 208in .user_placed = "--pool-cores is set" ; 209const auto d = cpu::decide_card_node (in ); 210expect (!d .on && d .why == "--pool-cores is set" ,"--pool-cores: left as is" ); 211in = pod (); 212in .hybrid = true; 213expect (!cpu::decide_card_node (in ).on ,"hybrid: left as is" ); 214in = pod (); 215in .nodes_with_cpus = 3 ; 216in .card_nodes = {2 }; 217in .mems_known = false; 218const auto e = cpu::decide_card_node (in ); 219expect (!e .on && e .why == "the process has no CPU on node 2" ,"card on a node the process has no CPU on: left as is" ); 220 } 221} 222 223static void write (const fs::path & p ,const std::string & text ) { 224 fs::create_directories (p .parent_path ()); 225 std::ofstream (p ) <<text ; 226} 227 228static void readers () { 229const fs::path root = fs::temp_directory_path () / ("card_node_test_" + std::to_string ((long long ) std::time (nullptr ))); 230 fs::remove_all (root ); 231const fs::path node = root /"devices/system/node" ; 232write (node /"node0/cpulist" ,"0-1,4-5\n" ); 233write (node /"node1/cpulist" ,"2-3,6-7\n" ); 234write (node /"node2/cpulist" ,"\n" );// a memory-only node (CXL): not counted 235write (node /"online" ,"0-2\n" ); 236 fs::create_directories (node /"nodeX" ); 237write (node /"node1/meminfo" ,"Node 1 MemTotal: 264000000 kB\nNode 1 MemFree: 112233 kB\nNode 1 MemUsed: 1 kB\n" ); 238write (node /"node0/meminfo" ,"Node 0 MemTotal: 5 kB\n" );// no MemFree line 239for (int c = 0 ;c < 8 ;++ c ) { 240const fs::path t = root / ("devices/system/cpu/cpu" + std::to_string (c )) /"topology" ; 241write (t /"physical_package_id" , std::to_string ((c /2 ) %2 )+ "\n" ); 242write (t /"core_id" , std::to_string (c %2 )+ "\n" ); 243 } 244#if !defined(_WIN32 ) 245// (a PCI address has colons, which no Windows file name may have: create_directories throws there. The engine reads sysfs on Linux only.) 246write (root /"bus/pci/devices/0000:e1:00.0/numa_node" ,"1\n" ); 247write (root /"bus/pci/devices/0000:21:00.0/numa_node" ,"-1\n" ); 248write (root /"bus/pci/devices/0000:22:00.0/numa_node" ,"junk\n" ); 249#endif 250 251#if defined(__linux__ ) 252const auto nodes = cpu::card_node_node_cpus (root .string ()); 253expect (nodes .size ()== 2 ,"fake sysfs: 2 nodes with CPUs (memory-only node2 and nodeX left out)" ); 254expect (nodes .count (0 )&& nodes .at (0 )== std::vector < int > ({0 ,1 ,4 ,5 }),"fake sysfs: node 0's CPUs" ); 255expect (nodes .count (1 )&& nodes .at (1 )== std::vector < int > ({2 ,3 ,6 ,7 }),"fake sysfs: node 1's CPUs" ); 256const auto desc = cpu::card_node_describe (root .string (), {0 ,3 ,6 ,9 },nodes ); 257expect (desc .size ()== 4 && desc [0 ].node == 0 && desc [1 ].node == 1 && desc [2 ].node == 1 && desc [3 ].node == -1 ,"describe: nodes" ); 258expect (desc [1 ].pkg == 1 && desc [1 ].core == 1 && desc [3 ].pkg == -1 && desc [3 ].core == -1 ,"describe: package and core, missing CPU -1" ); 259expect (cpu::card_node_node_cpus ((root /"nothing" ).string ()).empty (),"no node folder: no nodes" ); 260#endif 261#if !defined(_WIN32 ) 262expect (cpu::card_node_pci_node (root .string (),"0000:e1:00.0" )== 1 ,"pci numa_node 1" ); 263expect (cpu::card_node_pci_node (root .string (),"0000:21:00.0" )== -1 ,"pci numa_node -1" ); 264expect (cpu::card_node_pci_node (root .string (),"0000:22:00.0" )== -1 ,"pci numa_node junk: -1" ); 265expect (cpu::card_node_pci_node (root .string (),"0000:99:00.0" )== -1 ,"pci device missing: -1" ); 266#endif 267expect (cpu::card_node_free_bytes (root .string (),1 )== 112233ll * 1024 ,"node 1 MemFree" ); 268expect (cpu::card_node_free_bytes (root .string (),0 )== -1 ,"node 0 without MemFree: -1" ); 269expect (cpu::card_node_free_bytes (root .string (),5 )== -1 ,"node 5 missing: -1" ); 270expect (cpu::card_node_free_bytes (root .string (),-1 )== -1 ,"node -1: -1" ); 271expect (cpu::card_node_bdf (0 ,0xE1 ,0 )== "0000:e1:00.0" ,"bdf 0000:e1:00.0" ); 272expect (cpu::card_node_bdf (0x10 ,0x3b ,0x1f ,1 )== "0010:3b:1f.1" ,"bdf 0010:3b:1f.1" ); 273 fs::remove_all (root ); 274 275 std::vector < int > v ; 276expect (cpu::card_node_parse_cpulist ("0-31,64-95\n" ,v )&& v .size ()== 64 && v .front ()== 0 && v .back ()== 95 ,"cpulist 0-31,64-95" ); 277expect (cpu::card_node_parse_cpulist (" 3 " ,v )&& v == std::vector < int > ({3 }),"cpulist 3" ); 278expect (cpu::card_node_parse_cpulist ("\n" ,v )&& v .empty (),"cpulist empty" ); 279expect (!cpu::card_node_parse_cpulist ("3-1" ,v )&& v .empty (),"cpulist reversed: refused" ); 280expect (!cpu::card_node_parse_cpulist ("1,,2" ,v ),"cpulist empty item: refused" ); 281expect (!cpu::card_node_parse_cpulist ("a" ,v ),"cpulist letters: refused" ); 282 std::vector < int > mems ; 283expect (cpu::card_node_mems_allowed ("Name:\tstrata\nMems_allowed:\t00000003\nMems_allowed_list:\t0-1\nVoluntary: 1\n" ,mems )&& 284mems == std::vector < int > ({0 ,1 }),"Mems_allowed_list 0-1" ); 285expect (cpu::card_node_mems_allowed ("Mems_allowed_list:\t1\n" ,mems )&& mems == std::vector < int > ({1 }),"Mems_allowed_list 1" ); 286expect (!cpu::card_node_mems_allowed ("Name:\tx\n" ,mems )&& mems .empty (),"no Mems_allowed_list line" ); 287for (const char * off : {"0" ,"off" ,"OFF" ,"false" ,"no" })expect (cpu::card_node_switch_off (off ), std::string ("switch off: " )+ off ); 288for (const char * on : {"1" ,"" ,"on" ,"yes" })expect (!cpu::card_node_switch_off (on ), std::string ("switch not off: '" )+ on + "'" ); 289expect (!cpu::card_node_switch_off (nullptr ),"switch unset: not off" ); 290} 291 292#if defined(__linux__ ) 293// restrict_process_cpus on this process: a second thread exists before the call and must be restricted too 294static void restriction () { 295const std::vector < int > mine = cpu::card_node_thread_cpus (); 296if (mine .size ()< 2 ) { 297 std::printf ("card_node_test: restriction skipped (this process may run on %zu CPU)\n" ,mine .size ()); 298return ; 299 } 300 std::atomic < bool > stop {false}; 301 std::atomic < int > other_tid {0 }; 302 std::vector < int > other_after ; 303 std::thread t ([& ] { 304other_tid = (int )syscall (SYS_gettid ); 305while (!stop .load ()) std::this_thread::sleep_for (std::chrono::milliseconds (2 )); 306other_after = cpu::card_node_thread_cpus (); 307 }); 308while (other_tid .load ()== 0 ) std::this_thread::sleep_for (std::chrono::milliseconds (1 )); 309const std::vector < int > half (mine .begin (),mine .begin ()+ (long ) (mine .size () /2 )); 310 std::vector < int > want = half ; 311want .push_back (100000 );// a CPU the process does not have is ignored 312int refused = -1 ; 313const int changed = cpu::restrict_process_cpus (want ,refused ); 314expect (changed >=2 && refused == 0 ,"restriction: both threads changed (" + std::to_string (changed )+ ", refused " + 315 std::to_string (refused )+ ")" ); 316expect (cpu::card_node_thread_cpus ()== half ,"restriction: the calling thread has the subset" ); 317 std::vector < int > created_after ; 318 std::thread n ([& ] {created_after = cpu::card_node_thread_cpus (); }); 319n .join (); 320expect (created_after == half ,"restriction: a thread created afterwards inherits the subset" ); 321stop = true; 322t .join (); 323expect (other_after == half ,"restriction: the thread that existed before has the subset" ); 324int refused2 = -1 ; 325expect (cpu::restrict_process_cpus (half ,refused2 )== 0 && refused2 == 0 ,"restriction: a second call changes nothing" ); 326 std::vector < int > none = {100000 }; 327expect (cpu::restrict_process_cpus (none ,refused2 )== 0 && cpu::card_node_thread_cpus ()== half , 328"restriction: no common CPU keeps the mask" ); 329} 330#endif 331 332int main () { 333decisions (); 334readers (); 335#if defined(__linux__ ) 336restriction (); 337#endif 338if (g_fail != 0 ) { 339 std::fprintf (stderr ,"card_node_test: %d check(s) FAILED\n" ,g_fail ); 340return 1 ; 341 } 342 std::printf ("card_node_test: all checks passed\n" ); 343return 0 ; 344}