okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
af6226a3c
main
1// pool_cores_test: where the CPU pool's workers are placed, and --pool-cores (experimental, off by default). 2// Builds real pools (no model, no GPU). The default placement must be what detect_cpu_topology() says (one logical 3// processor per physical core, the host's core left out); a list given to set_pool_cores() must be taken as it is. 4#include "strata/kernels/cpu/pool.hpp" 5 6#include <algorithm> 7#include <cstdio> 8#include <cstdlib> 9#include <string> 10#include <vector> 11 12namespace cpu= strata::kernels::cpu; 13 14static int g_fail = 0 ; 15 16static void expect (bool ok ,const char * what ) { 17if (!ok ) { 18++ g_fail ; 19 std::fprintf (stderr ,"FAIL: %s\n" ,what ); 20 } 21} 22 23static std::string join (const std::vector < int >& v ) { 24 std::string s ; 25for (size_t i = 0 ;i < v .size ();++ i )s += (i ?"," :"" )+ std::to_string (v [i ]); 26return s ; 27} 28 29int main () { 30// On a machine with two NUMA nodes (a two-socket CPU, or a container on one) the pool splits itself over both nodes by 31// default and takes cores from each, so its workers no longer follow the topology's order: this test is about that 32// order, so the split is switched off here (pool_tasks_test covers the split). 33#if defined(_WIN32 ) 34_putenv_s ("STRATA_NUMA_POOL" ,"0" ); 35#else 36setenv ("STRATA_NUMA_POOL" ,"0" ,1 ); 37#endif 38const std::vector < cpu::LogicalCpuInfo > layout = cpu::logical_cpu_layout (); 39 std::printf ("pool_cores_test: %zu logical processors\n" ,layout .size ()); 40expect (!layout .empty (),"the system names its logical processors" ); 41const cpu::CpuTopology topo = cpu::detect_cpu_topology (true); 42expect (!topo .worker_cores .empty ()|| topo .host_core >=0 ,"the topology has cores" ); 43 44// 1. the default: nothing set, the placement is the topology's, one worker per physical core 45 { 46 cpu::set_pool_cores ({}); 47const int n = (int ) std::min < size_t > (2 ,topo .worker_cores .size ()); 48if (n >=1 ) { 49 cpu::ExpertPool pool (n ,true ,true ); 50expect (!pool .explicit_cores (),"default: not an explicit list" ); 51expect (pool .workers ()== n ,"default: the worker count asked for" ); 52expect (std::equal (topo .worker_cores .begin (),topo .worker_cores .begin ()+ n ,pool .worker_cores ().begin ()), 53"default: the workers sit on the topology's cores, in its order" ); 54const std::string line = pool .placement_line (); 55 std::printf (" default: %s\n" ,line .c_str ()); 56expect (line .rfind ("CPU workers on logical processors " ,0 )== 0 ,"the start-up line starts as documented" ); 57 std::vector < long > cores ; 58bool known = true; 59for (int i = 0 ;i < n ;++ i ) { 60long c = -1 ; 61for (const auto & l :layout )if (l .cpu == pool .worker_cores ()[(size_t )i ])c = l .core ; 62known = known && c >=0 ; 63cores .push_back (c ); 64 } 65if (known ) { 66 std::sort (cores .begin (),cores .end ()); 67expect (std::unique (cores .begin (),cores .end ())== cores .end (),"default: no two workers on one physical core" ); 68expect (line .find ("one per physical core" )!= std::string::npos ,"the line says one per physical core" ); 69 }else { 70expect (line .find ("one per physical core" )== std::string::npos ,"unknown layout: the line does not claim it" ); 71 } 72for (const auto & l :layout )if (l .cpu == topo .host_core ) 73for (int i = 0 ;i < n ;++ i ) 74for (const auto & m :layout ) 75if (m .cpu == pool .worker_cores ()[(size_t )i ]&& l .core >=0 ) 76expect (m .core != l .core ,"default: no worker on the host's core" ); 77 } 78 } 79 80// 2. --pool-cores: the list as given, in its order; the count is the list's length 81if (topo .worker_cores .size () >=3 ) { 82const std::vector < int > want {topo .worker_cores [2 ],topo .worker_cores [0 ]}; 83 cpu::set_pool_cores (want ); 84 cpu::ExpertPool pool (0 , true, true); 85expect (pool .explicit_cores (),"list: explicit" ); 86expect (pool .workers ()== 2 ,"list: the count is the list's length" ); 87expect (std::equal (want .begin (),want .end (),pool .worker_cores ().begin ()),"list: the workers sit on the listed processors, in order" ); 88expect (std::string (pool .plan_limit ()).find ("--pool-cores" )!= std::string::npos ,"list: the start-up line names the list" ); 89const std::string line = pool .placement_line (); 90 std::printf (" list %s: %s\n" ,join (want ).c_str (),line .c_str ()); 91expect (line .find ("your --pool-cores list" )!= std::string::npos ,"list: the line says it is the person's list" ); 92 93// --pool-workers under the list: the first N entries 94 cpu::ExpertPool one (1 , true, true); 95expect (one .workers ()== 1 && one .worker_cores ()[0 ]== want [0 ],"list + --pool-workers 1: the first entry" ); 96 } 97 98// 3. a processor that does not exist is left out (warning), the rest stays; nothing usable falls back to the default 99if (topo .worker_cores .size () >=2 ) { 100 cpu::set_pool_cores ({99999 ,topo .worker_cores [1 ]}); 101 cpu::ExpertPool pool (0 , true, true); 102expect (pool .explicit_cores ()&& pool .workers ()== 1 && pool .worker_cores ()[0 ]== topo .worker_cores [1 ], 103"an unusable entry is left out, the usable one kept" ); 104 cpu::set_pool_cores ({99999 }); 105 cpu::ExpertPool fallback (1 , true, true); 106expect (!fallback .explicit_cores ()&& fallback .worker_cores ()[0 ]== topo .worker_cores [0 ], 107"nothing usable: the default placement" ); 108 } 109 110// 4. two SMT siblings of one core are kept (and warned about by check_pool_cores, tested in pool_plan_test) 111for (const auto & a :layout ) { 112const cpu::LogicalCpuInfo * b = nullptr ; 113for (const auto & c :layout )if (c .cpu != a .cpu && a .core >=0 && c .core == a .core ) {b = & c ;break ; } 114if (b == nullptr )continue ; 115 cpu::set_pool_cores ({a .cpu ,b -> cpu }); 116 cpu::ExpertPool pool (0 , true, true); 117expect (pool .explicit_cores ()&& pool .workers ()== 2 && pool .worker_cores ()[0 ]== a .cpu && pool .worker_cores ()[1 ]== b -> cpu , 118"SMT siblings in the list are kept as given" ); 119const std::string line = pool .placement_line (); 120 std::printf (" siblings %d,%d: %s\n" ,a .cpu ,b -> cpu ,line .c_str ()); 121expect (line .find ("more than one worker" )!= std::string::npos ,"the line says two workers share a core" ); 122break ; 123 } 124 cpu::set_pool_cores ({}); 125 126if (g_fail ) { 127 std::fprintf (stderr ,"pool_cores_test: %d failure(s)\n" ,g_fail ); 128return 1 ; 129 } 130 std::printf ("pool_cores_test: OK\n" ); 131return 0 ; 132}