okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
43b105a75
main
1// aux_cpus_test: the CPU-set derivation of --aux-cpus (pure functions, no GPU) and, on Linux, the placement of live 2// threads (a thread of the process moved to the aux set by the sweep, one that pins itself at creation, the owned ones 3// left alone, and the original placement put back). 4#include "strata/platform/aux_cpus.hpp" 5 6#include <array> 7#include <atomic> 8#include <chrono> 9#include <cstdio> 10#include <thread> 11 12using namespace strata::aux_cpus ; 13 14namespace { 15int failures = 0 ; 16#define CHECK (c ) \ 17 do { \ 18 if (!(c)) { \ 19 std::fprintf(stderr, "FAILED %s:%d: %s\n", __FILE__, __LINE__, #c); \ 20 ++failures; \ 21 } \ 22 } while (0) 23 24using V = std::vector < int > ; 25 26std::vector < LogicalCpu > cpus (std::initializer_list < std::array < long ,3 >> rows ) { 27 std::vector < LogicalCpu > out ; 28for (const auto & r :rows )out .push_back ({(int )r [0 ],r [1 ],r [2 ]}); 29return out ; 30} 31 32// SMT machine: CPU i and i + n are the two threads of core i 33std::vector < LogicalCpu > smt (int n_cores ,int n_threads_total ) { 34 std::vector < LogicalCpu > out ; 35for (int c = 0 ;c < n_threads_total ;++ c )out .push_back ({c ,0 ,c %n_cores }); 36return out ; 37} 38 39void test_parse () { 40V v ; 41CHECK (parse_cpu_list ("24" ,v )&& v == V {24 }); 42CHECK (parse_cpu_list ("26,24" ,v )&& v == (V {24 ,26 })); 43CHECK (parse_cpu_list ("24-27,30,25" ,v )&& v == (V {24 ,25 ,26 ,27 ,30 })); 44CHECK (parse_cpu_list ("0" ,v )&& v == V {0 }); 45for (const char * bad : {"" ,"," ,"1," ,",1" ,"a" ,"1-" ,"-1" ,"3-1" ,"1,,2" ,"1 2" ,"1;2" ,"99999999" }) { 46CHECK (!parse_cpu_list (bad ,v )); 47CHECK (v .empty ()); 48 } 49Config c ; 50 std::string err ; 51CHECK (parse_spec ("" ,c ,err )&& !c .on ); 52CHECK (parse_spec ("off" ,c ,err )&& !c .on ); 53CHECK (parse_spec ("auto" ,c ,err )&& c .on && !c .use_list ); 54CHECK (parse_spec ("24,26" ,c ,err )&& c .on && c .use_list && c .list == (V {24 ,26 })); 55CHECK (!parse_spec ("fast" ,c ,err )&& !err .empty ()); 56} 57 58void test_derive () { 59// the production box's shape: 16 workers on 16 physical cores, the host on core 2, 24 allowed CPUs - CPUs 0-15 60// are cores 0-15 and 22-29 the second threads of cores 0-7: the host's sibling (CPU 24) is the only spare one 61 { 62 std::vector < LogicalCpu > a ; 63for (int c = 0 ;c < 16 ;++ c )a .push_back ({c ,0 ,c }); 64for (int c = 22 ;c < 30 ;++ c )a .push_back ({c ,0 ,c - 22 }); 65V workers ; 66for (int c = 0 ;c < 16 ;++ c ) 67if (c != 2 )workers .push_back (c ); 68CHECK (derive (a ,2 ,workers )== V {24 }); 69// an explicit list is taken as given, minus the host's CPU and the workers' CPUs 70CHECK (restrict_list ({24 ,26 ,2 ,5 ,99 },a ,2 ,workers )== (V {24 ,26 })); 71 } 72// 12 cores x 2 threads, host on CPU 0, workers on CPUs 1-7: the host's sibling first (12), then both threads of 73// the four worker-free cores 8-11; the siblings of the workers (13-19) are never offered 74 { 75const auto a = smt (12 ,24 ); 76CHECK (derive (a ,0 ,V {1 ,2 ,3 ,4 ,5 ,6 ,7 })== (V {12 ,8 ,9 ,10 ,11 ,20 ,21 ,22 ,23 })); 77 } 78// no SMT, every other core a worker: nothing spare 79 { 80const auto a = cpus ({{0 ,0 ,0 }, {1 ,0 ,1 }, {2 ,0 ,2 }, {3 ,0 ,3 }}); 81CHECK (derive (a ,0 ,V {1 ,2 ,3 }).empty ()); 82 } 83// no SMT, a core without a worker: that core 84 { 85const auto a = cpus ({{0 ,0 ,0 }, {1 ,0 ,1 }, {2 ,0 ,2 }, {3 ,0 ,3 }}); 86CHECK (derive (a ,0 ,V {1 ,2 })== V {3 }); 87 } 88// every CPU is a worker or the host's: nothing, with SMT too 89 { 90const auto a = smt (4 ,8 ); 91CHECK (derive (a ,0 ,V {1 ,2 ,3 })== V {4 });// only the host's sibling 92CHECK (derive (a ,0 ,V {1 ,2 ,3 ,4 }).empty ());// CPU 4 is a worker: the host core's sibling is taken 93 } 94// two packages: core 1 of package 1 is not core 1 of package 0 95 { 96const auto a = cpus ({{0 ,0 ,0 }, {1 ,0 ,1 }, {2 ,1 ,0 }, {3 ,1 ,1 }}); 97CHECK (derive (a ,0 ,V {1 ,2 })== V {3 }); 98 } 99// unknown cores (no sysfs): a CPU of unknown core could be a worker's sibling - none offered 100 { 101const auto a = cpus ({{0 ,-1 ,-1 }, {1 ,-1 ,-1 }, {2 ,-1 ,-1 }, {3 ,-1 ,-1 }}); 102CHECK (derive (a ,0 ,V {1 }).empty ()); 103 } 104// the host's CPU unknown: the free cores only 105 { 106const auto a = smt (4 ,8 ); 107CHECK (derive (a ,-1 ,V {0 ,1 })== (V {2 ,3 ,6 ,7 })); 108 } 109// CPUs the process may not use are not offered (a taskset): 5 and 6 are outside `a` 110 { 111const auto a = cpus ({{0 ,0 ,0 }, {1 ,0 ,1 }, {4 ,0 ,0 }}); 112CHECK (derive (a ,0 ,V {1 })== V {4 }); 113 } 114CHECK (derive (std::vector < LogicalCpu > {},0 ,V {1 }).empty ()); 115} 116 117#if defined(__linux__ ) 118int tid_of_self () {return (int )syscall (SYS_gettid ); } 119 120std::vector < int > mask_of (int tid ) { 121cpu_set_t m ; 122CPU_ZERO (& m ); 123 std::vector < int > out ; 124if (sched_getaffinity (tid ,sizeof m ,& m )!= 0 )return out ; 125for (int c = 0 ;c < CPU_SETSIZE ;++ c ) 126if (CPU_ISSET (c ,& m ))out .push_back (c ); 127return out ; 128} 129 130void test_live () { 131const std::vector < int > allowed = mask_of (0 ); 132if (allowed .size ()< 4 ) { 133 std::printf ("aux_cpus_test: live placement skipped (needs 4 allowed CPUs, have %zu)\n" ,allowed .size ()); 134return ; 135 } 136const int host = allowed [0 ],worker = allowed [1 ],aux_a = allowed [2 ],aux_b = allowed [3 ]; 137 std::string err ; 138CHECK (configure (std::to_string (aux_a )+ "," + std::to_string (aux_b ),err )); 139// before the plan exists nothing is moved 140CHECK (sweep ()== 0 ); 141const std::string line = set_topology (host ,V {worker }); 142 std::printf ("%s\n" ,line .c_str ()); 143CHECK (plan ()== (V {aux_a ,aux_b })); 144CHECK (enabled ()); 145 146// threads that stay alive until told: `floater` is created before the sweep (a CUDA driver thread's case), `owned` 147// is a pool worker (registered), `late` pins itself at creation 148 std::atomic < int > tid_floater {0 },tid_owned {0 },tid_late {0 }; 149 std::atomic < bool > stop {false}; 150auto body = [& ](std::atomic < int >& tid ,bool owned ,bool pin ) { 151if (owned )note_owned_thread (); 152if (pin )pin_current_thread (); 153tid = tid_of_self (); 154while (!stop .load ()) std::this_thread::sleep_for (std::chrono::milliseconds (2 )); 155 }; 156set_enabled (false);// earlier threads are born unmoved 157CHECK (!enabled ()); 158 std::thread floater ([& ] {body (tid_floater , false, false); }); 159 std::thread owned ([& ] {body (tid_owned , true, false); }); 160for (int i = 0 ;i < 500 && (!tid_floater || !tid_owned );++ i ) std::this_thread::sleep_for (std::chrono::milliseconds (2 )); 161CHECK (mask_of (tid_floater )== allowed ); 162 163set_enabled (true);// the sweep 164const V aux {aux_a ,aux_b }; 165CHECK (mask_of (tid_floater )== aux ); 166CHECK (mask_of (tid_owned )== allowed );// owned threads are left alone 167CHECK (mask_of (tid_of_self ())== allowed );// so is the main thread (configure registered it) 168 169 std::thread late ([& ] {body (tid_late , false, true); }); 170for (int i = 0 ;i < 500 && !tid_late ;++ i ) std::this_thread::sleep_for (std::chrono::milliseconds (2 )); 171CHECK (mask_of (tid_late )== aux );// pinned at creation 172 173set_enabled (false);// back to the placements they had 174CHECK (mask_of (tid_floater )== allowed ); 175CHECK (mask_of (tid_late )== allowed ); 176CHECK (mask_of (tid_owned )== allowed ); 177CHECK (sweep ()== 0 );// off: nothing moves 178 179// a thread pinned to ONE worker CPU by someone else is not ours even when unregistered 180 std::atomic < int > tid_pinned {0 }; 181 std::thread pinned ([& ] { 182cpu_set_t m ; 183CPU_ZERO (& m ); 184CPU_SET (worker ,& m ); 185sched_setaffinity (0 ,sizeof m ,& m ); 186tid_pinned = tid_of_self (); 187while (!stop .load ()) std::this_thread::sleep_for (std::chrono::milliseconds (2 )); 188 }); 189for (int i = 0 ;i < 500 && !tid_pinned ;++ i ) std::this_thread::sleep_for (std::chrono::milliseconds (2 )); 190set_enabled (true); 191CHECK (mask_of (tid_pinned )== V {worker }); 192CHECK (mask_of (tid_floater )== aux ); 193set_enabled (false); 194 195stop = true; 196floater .join (); 197owned .join (); 198late .join (); 199pinned .join (); 200} 201#else 202void test_live () { std::printf ("aux_cpus_test: live placement is Linux only\n" ); } 203#endif 204 205}// namespace 206 207int main () { 208test_parse (); 209test_derive (); 210test_live (); 211if (failures ) { 212 std::fprintf (stderr ,"aux_cpus_test: %d check(s) failed\n" ,failures ); 213return 1 ; 214 } 215 std::printf ("aux_cpus_test: ok\n" ); 216return 0 ; 217}