okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
4bd591269
main
1// numa_pool_test: when the CPU expert pool splits per NUMA node (include/strata/kernels/cpu/numa_pool.hpp), on mock machines. 2// No threads, no OS topology: a fake sysfs node folder in a temp directory and a made-up core -> node map. It runs anywhere. 3#include "strata/kernels/cpu/numa_pool.hpp" 4 5#include <cstdio> 6#include <ctime> 7#include <filesystem> 8#include <fstream> 9#include <string> 10#include <vector> 11 12namespace cpu= strata::kernels::cpu; 13namespace fs= std::filesystem; 14 15static int g_fail = 0 ; 16 17static void expect (bool ok ,const std::string & what ) { 18if (!ok ) { 19++ g_fail ; 20 std::fprintf (stderr ,"FAIL: %s\n" ,what .c_str ()); 21 } 22} 23 24// /sys/devices/system/node with `node<i>/cpulist` files; an empty string is a memory-only node 25static fs::path make_nodes (const fs::path & root ,const std::vector < std::string >& cpulists ,bool noise = true) { 26 fs::remove_all (root ); 27 fs::create_directories (root ); 28for (size_t i = 0 ;i < cpulists .size ();++ i ) { 29 fs::create_directories (root / ("node" + std::to_string (i ))); 30 std::ofstream (root / ("node" + std::to_string (i )) /"cpulist" ) <<cpulists [i ] <<"\n" ; 31 } 32if (noise ) {// files and folders in that directory that are not nodes 33 std::ofstream (root /"online" ) <<"0-1\n" ; 34 std::ofstream (root /"possible" ) <<"0-1\n" ; 35 fs::create_directories (root /"nodes" ); 36 fs::create_directories (root /"node" ); 37 fs::create_directories (root /"nodeX" ); 38 } 39return root ; 40} 41 42int main () { 43const fs::path tmp = fs::temp_directory_path () / ("strata_numa_pool_test_" + std::to_string ((long long ) std::time (nullptr ))); 44 45// ---- the node count of a sysfs node folder 46expect (cpu::numa_cpu_node_count_at ((tmp /"missing" ).string ())== 0 ,"no folder: 0 nodes" ); 47expect (cpu::numa_cpu_node_count_at (make_nodes (tmp , {"0-23" }).string ())== 1 ,"one node" ); 48expect (cpu::numa_cpu_node_count_at (make_nodes (tmp , {"0-11,24-35" ,"12-23,36-47" }).string ())== 2 ,"two sockets: 2 nodes" ); 49expect (cpu::numa_cpu_node_count_at (make_nodes (tmp , {"0-7" ,"8-15" ,"16-23" ,"24-31" }).string ())== 4 ,"four nodes" ); 50expect (cpu::numa_cpu_node_count_at (make_nodes (tmp , {"0-23" ,"" }).string ())== 1 ,"a memory-only node (CXL) is not a node of CPUs" ); 51expect (cpu::numa_cpu_node_count_at (make_nodes (tmp , {"0-11" ,"12-23" ,"" }).string ())== 2 ,"two nodes of CPUs and a memory-only one" ); 52 53// ---- pick_numa_pool: one node is today's path 54using C = cpu::NumaPoolChoice ; 55auto pick = [](const char * pool ,const char * split ,int nodes ) {return cpu::pick_numa_pool (pool ,split ,nodes ); }; 56for (int nodes : {0 ,1 }) { 57for (const char * pool : {(const char * )nullptr ,"" ,"1" ,"garbage" }) { 58for (const char * split : {(const char * )nullptr ,"" }) { 59const auto p = pick (pool ,split ,nodes ); 60expect (p .choice == C ::Unchanged && p .log .empty (),"one node, nothing set: Unchanged and silent" ); 61 } 62 } 63const auto zero = pick ("0" ,nullptr ,nodes ); 64expect (zero .choice == C ::Unchanged && zero .log .empty (),"one node, STRATA_NUMA_POOL=0: Unchanged and silent" ); 65expect (pick (nullptr ,"1" ,nodes ).choice == C ::Unchanged ,"one node, STRATA_NUMA_SPLIT=1: the old variable decides" ); 66expect (pick (nullptr ,"test" ,nodes ).choice == C ::Unchanged ,"one node, STRATA_NUMA_SPLIT=test: the old variable decides" ); 67 } 68 { 69const auto p = pick (nullptr ,nullptr ,2 ); 70expect (p .choice == C ::Auto && p .log .empty (),"two nodes, nothing set: Auto" ); 71expect (pick ("1" ,nullptr ,2 ).choice == C ::Auto ,"two nodes, STRATA_NUMA_POOL=1: Auto" ); 72expect (pick ("" ,"" ,2 ).choice == C ::Auto ,"two nodes, empty variables: Auto" ); 73expect (pick (nullptr ,nullptr ,4 ).choice == C ::Auto ,"four nodes: Auto (the decision refuses it, with a line)" ); 74for (const char * off : {"0" ,"off" ,"OFF" ,"false" ,"No" }) { 75const auto o = pick (off ,nullptr ,2 ); 76expect (o .choice == C ::Off , std::string ("two nodes, STRATA_NUMA_POOL=" )+ off + ": Off" ); 77expect (o .log .find ("STRATA_NUMA_POOL=0" )!= std::string::npos && o .log .find ("2 NUMA nodes" )!= std::string::npos , 78"the off line names the switch and the node count" ); 79 } 80expect (pick ("0" ,"1" ,2 ).choice == C ::Off ,"STRATA_NUMA_POOL=0 beats STRATA_NUMA_SPLIT=1" ); 81expect (pick ("0" ,"test" ,1 ).choice == C ::Off ,"STRATA_NUMA_POOL=0 beats STRATA_NUMA_SPLIT=test, even on one node" ); 82expect (pick (nullptr ,"1" ,2 ).choice == C ::Unchanged ,"two nodes, STRATA_NUMA_SPLIT=1: the old variable decides" ); 83expect (pick (nullptr ,"0" ,2 ).choice == C ::Unchanged ,"two nodes, STRATA_NUMA_SPLIT=0: the old variable decides (off)" ); 84 } 85 86// ---- decide_numa_split on a made-up 2 x 12-core box (host core 0; cores 0-11 node 0, 12-23 node 1) 87auto node_of = [](int core ) {return core < 0 ?-1 :core < 12 ?0 :1 ; }; 88auto cores = [](std::initializer_list < int > v ) {return std::vector < int > (v ); }; 89 {// 22 workers interleaved over the nodes + the host 90 std::vector < int > c ; 91for (int i = 0 ;i < 11 ;++ i ) {c .push_back (1 + i );c .push_back (12 + i ); } 92c .push_back (0 );// the host 93const auto d = cpu::decide_numa_split (c ,22 , true, false,node_of ); 94expect (d .on ,"22 workers + host on 2 nodes: on" ); 95expect (d .nodes [0 ]== 0 && d .nodes [1 ]== 1 ,"the nodes are 0 and 1" ); 96expect (d .per [0 ]== 12 && d .per [1 ]== 11 ,"12 threads on node 0 (the host counted), 11 on node 1" ); 97expect (d .slice .size ()== c .size ()&& d .slice .back ()== 0 ,"the host drains in slice 0" ); 98expect (d .slice [0 ]== 0 && d .slice [1 ]== 1 ,"workers alternate between the slices" ); 99 } 100 {// all workers on one node: refused 101const auto d = cpu::decide_numa_split (cores ({1 ,2 ,3 ,4 ,0 }),4 , true, false,node_of ); 102expect (!d .on && d .why .find ("span 1 NUMA node(s)" )!= std::string::npos ,"one node's threads only: refused with the count" ); 103 } 104 {// a node with under half of the other's threads: unbalanced 105 std::vector < int > c = {1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,12 ,0 }; 106const auto d = cpu::decide_numa_split (c ,10 , true, false,node_of ); 107expect (!d .on && d .why .find ("unbalanced" )!= std::string::npos ,"10 + 1 threads: refused as unbalanced" ); 108expect (d .nodes [0 ]== -1 && d .slice .empty ()&& d .per [0 ]== 0 ,"a refused split leaves no plan behind" ); 109 } 110 {// three nodes 111auto three = [](int core ) {return core < 8 ?0 :core < 16 ?1 :2 ; }; 112const auto d = cpu::decide_numa_split (cores ({1 ,9 ,17 ,2 ,10 ,18 ,0 }),6 , true, false,three ); 113expect (!d .on && d .why .find ("span 3 NUMA node(s)" )!= std::string::npos && d .why .find ("exactly 2" )!= std::string::npos , 114"three nodes: refused, exactly 2 are supported" ); 115 } 116 { 117const auto base = cores ({1 ,12 ,2 ,13 ,0 }); 118expect (cpu::decide_numa_split (base ,4 , true, false,node_of ).on ,"4 workers + host, balanced: on" ); 119const auto rot = cpu::decide_numa_split (base ,4 , true, true,node_of ); 120expect (!rot .on && rot .why .find ("STRATA_EXCHANGE_ROTATE=1" )!= std::string::npos ,"rotating slots: refused" ); 121const auto nopin = cpu::decide_numa_split (base ,4 , false, false,node_of ); 122expect (!nopin .on && nopin .why .find ("not pinned" )!= std::string::npos ,"unpinned threads: refused" ); 123const auto nocore = cpu::decide_numa_split (cores ({1 ,-1 ,2 ,13 }),4 , true, false,node_of ); 124expect (!nocore .on && nocore .why .find ("worker 1 has no core" )!= std::string::npos ,"a worker without a core: refused" ); 125const auto nohost = cpu::decide_numa_split (cores ({1 ,12 ,-1 }),2 , true, false,node_of ); 126expect (!nohost .on && nohost .why .find ("host thread has no core" )!= std::string::npos ,"a host without a core: refused" ); 127auto unknown = [](int core ) {return core == 12 ?-1 :0 ; }; 128const auto un = cpu::decide_numa_split (base ,4 , true, false,unknown ); 129expect (!un .on && un .why .find ("CPU 12 has no known NUMA node" )!= std::string::npos ,"a core with no node: refused" ); 130const auto one = cpu::decide_numa_split (cores ({1 }),1 , true, false,node_of ); 131expect (!one .on && one .why .find ("needs 2 or more" )!= std::string::npos ,"one thread: refused" ); 132 } 133 {// a single-node machine fed to the decision (STRATA_NUMA_SPLIT=1 on one node): refused, never half-on 134auto flat = [](int core ) {return core < 0 ?-1 :0 ; }; 135const auto d = cpu::decide_numa_split (cores ({1 ,2 ,3 ,4 ,0 }),4 , true, false,flat ); 136expect (!d .on ,"every core on node 0: off" ); 137 } 138 139 fs::remove_all (tmp ); 140if (g_fail ) { 141 std::fprintf (stderr ,"numa_pool_test: %d failure(s)\n" ,g_fail ); 142return 1 ; 143 } 144 std::printf ("numa_pool_test: OK\n" ); 145return 0 ; 146}