okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
01f8c3255
main
1// pool_plan_test: the default CPU expert worker count on mock CPUs (include/strata/kernels/cpu/pool_plan.hpp). 2// Pure arithmetic, no threads and no OS calls: it runs on any machine. 3#include "strata/kernels/cpu/pool_plan.hpp" 4 5#include <cstdio> 6#include <string> 7 8namespace cpu= strata::kernels::cpu; 9 10static int g_fail = 0 ; 11 12static void expect (bool ok ,const char * what ) { 13if (!ok ) { 14++ g_fail ; 15 std::fprintf (stderr ,"FAIL: %s\n" ,what ); 16 } 17} 18 19static cpu::PoolPlanInput cpu_of (int logical , std::vector < int > per_package ,double quota = 0 ) { 20 cpu::PoolPlanInput in ; 21in .logical = logical ; 22in .cores_per_package = std::move (per_package ); 23in .cpu_quota = quota ; 24return in ; 25} 26 27static void line (const char * name ,const cpu::PoolPlanInput & in ) { 28const cpu::PoolPlan p = cpu::plan_pool_workers (in ); 29 std::printf (" %-34s logical %3d cores %3d (host socket %3d, %d socket%s) -> %2d workers [%s]\n" ,name ,in .logical , 30p .cores ,p .cores_local ,p .packages ,p .packages == 1 ?"" :"s" ,p .workers ,p .limit ); 31} 32 33int main () { 34 std::printf ("pool_plan_test: cap %d, reserve %d from %d workers\n" , cpu::kPoolWorkersCap , cpu::kPoolReserve , 35 cpu::kPoolReserveFrom ); 36 37// 8 cores / 16 threads: every physical core but the host's, never the SMT siblings 38const auto c8 = cpu_of (16 , {8 }); 39line ("8c/16t" ,c8 ); 40expect (cpu::plan_pool_workers (c8 ).workers == 7 ,"8c/16t -> 7 workers" ); 41 42// 6 cores / 12 threads (the small test PCs) and a CPU without SMT 43expect (cpu::plan_pool_workers (cpu_of (12 , {6 })).workers == 5 ,"6c/12t -> 5 workers" ); 44expect (cpu::plan_pool_workers (cpu_of (4 , {4 })).workers == 3 ,"4c/4t (no SMT) -> 3 workers" ); 45expect (cpu::plan_pool_workers (cpu_of (1 , {1 })).workers == 1 ,"1 core -> 1 worker (never 0)" ); 46expect (cpu::plan_pool_workers (cpu_of (2 , {2 })).workers == 1 ,"2 cores -> 1 worker" ); 47 48// 16 cores / 32 threads 49const auto c16 = cpu_of (32 , {16 }); 50line ("16c/32t" ,c16 ); 51 { 52const int w = cpu::plan_pool_workers (c16 ).workers ; 53expect (w == 15 - (15 >= cpu::kPoolReserveFrom ? cpu::kPoolReserve :0 )|| w == cpu::kPoolWorkersCap , 54"16c/32t -> physical cores minus the host (and the reserve)" ); 55expect (w <=15 ,"16c/32t never above the physical cores minus the host" ); 56 } 57 58// 64 cores / 128 threads: the cap, far below the logical count 59const auto c64 = cpu_of (128 , {64 }); 60line ("64c/128t" ,c64 ); 61expect (cpu::plan_pool_workers (c64 ).workers == cpu::kPoolWorkersCap ,"64c/128t -> the cap" ); 62expect (cpu::plan_pool_workers (c64 ).workers < 64 ,"64c/128t is not near the logical count" ); 63 64// 2 sockets x 64 cores / 256 threads: the user report - never 255, one socket's cores at most, under the cap 65const auto c2s = cpu_of (256 , {64 ,64 }); 66line ("2-socket 128c/256t" ,c2s ); 67expect (cpu::plan_pool_workers (c2s ).workers == cpu::kPoolWorkersCap ,"2-socket 128c/256t -> the cap" ); 68expect (cpu::plan_pool_workers (c2s ).packages == 2 ,"2-socket: two packages" ); 69expect (cpu::plan_pool_workers (c2s ).cores == 128 ,"2-socket: 128 physical cores counted" ); 70 71// 2 sockets x 20 cores (the Xeons of bench #674 #707: 17-18 workers beat 35): the host's socket only 72const auto xeon = cpu_of (80 , {20 ,20 }); 73line ("2-socket 40c/80t (Xeon)" ,xeon ); 74expect (cpu::plan_pool_workers (xeon ).workers == 18 ,"2x20c: 18 workers (bench #674 #707: 17-18 beat 35): the host socket only, minus the host and the reserve" ); 75// ... and a smaller second socket does not matter; the host's socket comes first in the input 76expect (cpu::plan_pool_workers (cpu_of (48 , {6 ,18 })).workers == 5 ,"host socket of 6 cores: 5 workers" ); 77 78// an unknown core layout (no sysfs, a container): half the logical processors for a many-thread CPU, never all 79const auto unk = cpu_of (256 , {}); 80line ("256 logical, layout unknown" ,unk ); 81expect (!cpu::plan_pool_workers (unk ).topology_known ,"unknown layout is flagged" ); 82expect (cpu::plan_pool_workers (unk ).workers <= cpu::kPoolWorkersCap ,"unknown 256 -> at most the cap" ); 83expect (cpu::plan_pool_workers (unk ).workers < 255 ,"unknown 256 -> never 255" ); 84expect (cpu::plan_pool_workers (cpu_of (8 , {})).workers == 7 ,"unknown, 8 logical -> 7 workers" ); 85 86// hybrid CPUs: with more E-cores than P-cores, the P-cores but the host's plus half of the E-cores 87 { 88auto h = cpu_of (20 , {20 }); 89h .p_cores = 8 ; 90h .e_cores = 12 ; 91line ("hybrid 8P+12E (Core Ultra 7 265)" ,h ); 92expect (cpu::plan_pool_workers (h ).workers == 13 ,"8P+12E -> 13 workers (measured: 12-16 beat the 19 of one per core)" ); 93auto h2 = cpu_of (32 , {24 }); 94h2 .p_cores = 8 ; 95h2 .e_cores = 16 ; 96expect (cpu::plan_pool_workers (h2 ).workers == 15 ,"8P+16E -> 15 workers (#642)" ); 97auto h3 = cpu_of (24 , {16 }); 98h3 .p_cores = 8 ; 99h3 .e_cores = 8 ; 100expect (cpu::plan_pool_workers (h3 ).workers == 15 ,"8P+8E: not more E-cores than P-cores, every core but the host's" ); 101 } 102 103// #3262: Core Ultra 7 255H (6P + 8E + 2 LP-E, no SMT). The LP-E cores (CPUs 14-15) are in cpu_lowpower/cpus, in 104// neither the core nor the atom list; they are efficiency cores. 105 { 106const std::vector < int > core = {0 ,1 ,2 ,3 ,4 ,5 },atom = {6 ,7 ,8 ,9 ,10 ,11 ,12 ,13 },lp = {14 ,15 }; 107int e = 0 ,p = 0 ; 108for (int c = 0 ;c < 16 ;++ c ) { 109const long cap = c < 6 ?1024 :c < 14 ?774 :312 ; 110 (cpu::hybrid_cpu_is_efficiency (c ,cap ,1024 , true,core ,atom ,lp ) ?e :p )++ ; 111 } 112expect (p == 6 && e == 10 ,"255H: 6 P-cores, 8 E + 2 LP-E counted as efficiency cores" ); 113expect (cpu::hybrid_cpu_is_efficiency (14 ,312 ,1024 , true,core ,atom ,lp ),"LP-E core 14 is an E-core" ); 114expect (!cpu::hybrid_cpu_is_efficiency (5 ,1024 ,1024 , true,core ,atom ,lp ),"P-core 5 stays a P-core" ); 115// a CPU in the lists of no kind falls back to its capacity (312 < 90% of 1024: an E-core) 116expect (cpu::hybrid_cpu_is_efficiency (14 ,312 ,1024 , true,core ,atom , {}),"LP-E core with no third list: capacity says E" ); 117// a CPU in no list with a favoured-P capacity is a P-core 118expect (!cpu::hybrid_cpu_is_efficiency (20 ,1012 ,1024 , true,core ,atom ,lp ),"unlisted CPU, capacity 1012/1024: P" ); 119// the plan with the right counts: 6P + 10E -> 5 + 5 workers, not the 15 of "every core but the host's" 120auto h = cpu_of (16 , {16 }); 121h .p_cores = 6 ; 122h .e_cores = 10 ; 123expect (cpu::plan_pool_workers (h ).workers == 10 ,"255H -> 10 workers (the measured 9-13 are flat; 15 is -35%)" ); 124 } 125 126// a container quota 127const auto q = cpu_of (128 , {64 },4.0 ); 128line ("64c, container quota 4 CPUs" ,q ); 129expect (cpu::plan_pool_workers (q ).workers == 3 ,"quota 4 CPUs -> 3 workers" ); 130expect (cpu::plan_pool_workers (cpu_of (128 , {64 },200.0 )).workers == cpu::kPoolWorkersCap ,"a big quota changes nothing" ); 131 132// warnings: only above the plan, and each says what to do 133const cpu::PoolPlan p2s = cpu::plan_pool_workers (c2s ); 134expect (cpu::pool_workers_warning (0 ,c2s ,p2s ).empty (),"0 (the default) never warns" ); 135expect (cpu::pool_workers_warning (p2s .workers ,c2s ,p2s ).empty (),"the plan itself never warns" ); 136expect (cpu::pool_workers_warning (4 ,c2s ,p2s ).empty (),"fewer than the plan never warns" ); 137 { 138const std::string w = cpu::pool_workers_warning (256 ,c2s ,p2s ); 139expect (!w .empty (),"256 workers on 256 threads warns" ); 140expect (w .find ("--pool-workers " + std::to_string (p2s .workers ))!= std::string::npos ,"the warning names the recommendation" ); 141expect (w .find ("still runs" )!= std::string::npos ,"the warning says it still runs" ); 142 } 143expect (cpu::pool_workers_warning (200 ,c2s ,p2s ).find ("physical cores" )!= std::string::npos , 144"200 workers on 128 cores: more than the physical cores" ); 145expect (cpu::pool_workers_warning (300 ,c2s ,p2s ).find ("256 CPU threads" )!= std::string::npos , 146"300 workers on 256 threads: more than the CPU threads" ); 147expect (cpu::pool_workers_warning (30 ,xeon , cpu::plan_pool_workers (xeon )).find ("socket" )!= std::string::npos , 148"30 workers on 2x20c: crosses the socket" ); 149expect (!cpu::pool_workers_warning (40 ,c64 , cpu::plan_pool_workers (c64 )).empty (),"40 workers on 64c is above the cap: warns" ); 150expect (cpu::pool_workers_warning (8 ,q , cpu::plan_pool_workers (q )).find ("container" )!= std::string::npos , 151"8 workers under a 4-CPU quota: names the container limit" ); 152 153// STRATA_NUMA_SPLIT (span_packages): the default spans every socket, the other bounds stay; off = the plan above 154 { 155auto x = xeon ;// 2 x 20 cores: 39 but the host's, one kept free -> 38, then the cap 156x .span_packages = true; 157line ("2-socket 40c/80t, NUMA split" ,x ); 158const cpu::PoolPlan px = cpu::plan_pool_workers (x ); 159expect (px .workers == cpu::kPoolWorkersCap ,"2x20c split: both sockets, then the cap" ); 160expect (px .cores_local == 40 ,"2x20c split: every socket counts as local" ); 161auto b = cpu_of (48 , {12 ,12 });// the 2 x Xeon Gold 6226 box: 23 but the host's, one kept free 162expect (cpu::plan_pool_workers (b ).workers == 11 ,"2x12c no split: the host socket's 11 (unchanged)" ); 163b .span_packages = true; 164expect (cpu::plan_pool_workers (b ).workers == 22 ,"2x12c split: 22 workers across both sockets" ); 165auto one = cpu_of (16 , {8 }); 166one .span_packages = true; 167expect (cpu::plan_pool_workers (one ).workers == 7 ,"one socket: the split changes nothing" ); 168// the socket warning is silenced while the split is on; the others stay 169expect (cpu::pool_workers_warning (30 ,x ,px ).empty (),"30 workers on 2x20c under the split: no socket warning" ); 170auto bx = cpu_of (80 , {20 ,20 }); 171bx .span_packages = true; 172const std::string w = cpu::pool_workers_warning (36 ,bx , cpu::plan_pool_workers (bx )); 173expect (!w .empty ()&& w .find ("socket" )== std::string::npos ,"36 on 2x20c under the split: the cap warning, not the socket one" ); 174expect (cpu::pool_workers_warning (60 ,bx , cpu::plan_pool_workers (bx )).find ("physical cores" )!= std::string::npos , 175"60 on 2x20c under the split: still more than the physical cores" ); 176 } 177// interleave_packages: round-robin over the packages (host's first), stable inside each 178 { 179const std::vector < size_t > o = cpu::interleave_packages ({0 ,0 ,0 ,1 ,1 ,1 ,1 }); 180const std::vector < size_t > want = {0 ,3 ,1 ,4 ,2 ,5 ,6 }; 181expect (o == want ,"interleave 3+4: 0 3 1 4 2 5 6" ); 182expect (cpu::interleave_packages ({}).empty (),"interleave of nothing" ); 183const std::vector < size_t > same = cpu::interleave_packages ({1 ,1 ,1 }); 184expect (same == std::vector < size_t > ({0 ,1 ,2 }),"one package: the order is kept" ); 185 } 186// a configured count under the split, on the 2 x 12-core box: the worker cores as fill_plan_fields orders them 187// (the host's socket first: its 11 cores but the host's, then the other socket's 12), taken in interleave order; 188// the host thread drains on socket 0 189 { 190 std::vector < int > pkg (11 ,0 ); 191pkg .insert (pkg .end (),12 ,1 ); 192const std::vector < size_t > o = cpu::interleave_packages (pkg ); 193auto per = [& ](int n ,bool interleaved ,int s ) { 194int c = 0 ; 195for (int i = 0 ;i < n ;++ i )c += pkg [interleaved ?o [(size_t )i ] : (size_t )i ]== s ; 196return c ; 197 }; 198expect (per (12 , true,0 )== 6 && per (12 , true,1 )== 6 ,"--pool-workers 12 under the split: 6 + 6 workers" ); 199expect (cpu::numa_slices_balanced (per (12 , true,0 )+ 1 ,per (12 , true,1 )),"12: 7 / 6 threads, balanced" ); 200expect (per (13 , true,0 )== 7 && per (13 , true,1 )== 6 ,"--pool-workers 13 under the split: 7 + 6 workers" ); 201expect (cpu::numa_slices_balanced (per (13 , true,0 )+ 1 ,per (13 , true,1 )),"13: 8 / 6 threads, balanced" ); 202expect (!cpu::numa_slices_balanced (per (12 , false,0 )+ 1 ,per (12 , false,1 )), 203"12 in the host-first order would be 12 / 1 threads: refused (what the interleave avoids)" ); 204expect (per (22 , true,0 )== 11 && per (22 , true,1 )== 11 ,"22 (the split default here): 11 + 11 workers" ); 205 } 206 207// ---- --pool-cores and the start-up line (pure helpers) ---- 208 { 209 std::vector < int > v ; 210 std::string err ; 211expect (cpu::parse_pool_cores ("0,2,4,6" ,v ,err )&& v == std::vector < int > ({0 ,2 ,4 ,6 }),"list: 0,2,4,6" ); 212expect (cpu::parse_pool_cores ("2-14:2" ,v ,err )&& v == std::vector < int > ({2 ,4 ,6 ,8 ,10 ,12 ,14 }),"list: 2-14:2" ); 213expect (cpu::parse_pool_cores ("4-6" ,v ,err )&& v == std::vector < int > ({4 ,5 ,6 }),"list: 4-6" ); 214expect (cpu::parse_pool_cores (" 6 , 2 ,2, 0-1 " ,v ,err )&& v == std::vector < int > ({6 ,2 ,0 ,1 }), 215"list: spaces ok, the person's order kept, a repeated number once" ); 216expect (!cpu::parse_pool_cores ("" ,v ,err )&& v .empty ()&& !err .empty (),"list: empty is an error with a message" ); 217expect (!cpu::parse_pool_cores ("0,,2" ,v ,err )&& v .empty (),"list: an empty item" ); 218expect (!cpu::parse_pool_cores ("0,a" ,v ,err ),"list: letters" ); 219expect (!cpu::parse_pool_cores ("6-2" ,v ,err ),"list: a range that goes down" ); 220expect (!cpu::parse_pool_cores ("0-8:0" ,v ,err ),"list: a step of 0" ); 221expect (!cpu::parse_pool_cores ("-3" ,v ,err ),"list: a negative number" ); 222expect (!cpu::parse_pool_cores ("0-99999" ,v ,err )&& v .empty (),"list: a silly range is refused with a message, not expanded" ); 223expect (cpu::parse_pool_cores ("0-4095" ,v ,err )&& v .size ()== 4096 ,"list: 4096 entries are fine" ); 224expect (cpu::format_cpu_list ({2 ,4 ,6 ,8 ,10 ,12 ,14 ,16 ,18 ,20 ,22 ,24 ,26 ,28 ,30 })== "2,4,...,30" ,"format: a stride" ); 225expect (cpu::format_cpu_list ({0 ,1 ,2 ,3 ,4 })== "0-4" ,"format: a run" ); 226expect (cpu::format_cpu_list ({2 ,4 ,7 })== "2,4,7" ,"format: short and irregular lists in full" ); 227expect (cpu::format_cpu_list ({1 ,2 ,4 ,5 ,9 })== "1,2,4,5,9" ,"format: irregular" ); 228expect (cpu::format_cpu_list ({})== "none" ,"format: none" ); 229 230// Windows-style numbering of 16 cores / 32 threads: core k = logical 2k and 2k+1 231 std::vector < cpu::LogicalCpuInfo > win ; 232for (int i = 0 ;i < 32 ;++ i )win .push_back ({i ,i /2 }); 233 std::vector < int > w ; 234for (int c = 1 ;c < 16 ;++ c )w .push_back (2 * c ); 235const std::string line = cpu::describe_worker_placement (w ,0 ,win , false); 236 std::printf (" %s\n" ,line .c_str ()); 237expect (line == "CPU workers on logical processors 2,4,...,30 (15 workers, one per physical core; host on 0)" , 238"the default line on 16c/32t" ); 239// the mistake the person worried about: workers on logical 0..7 of an adjacent-numbering SMT PC 240const std::string bad = cpu::describe_worker_placement ({1 ,2 ,3 ,4 ,5 ,6 ,7 },0 ,win , false); 241 std::printf (" %s\n" ,bad .c_str ()); 242expect (bad .find ("one per physical core" )== std::string::npos && bad .find ("carry more than one worker" )!= std::string::npos , 243"workers on logical 1..7 of 16c/32t are reported as sharing cores" ); 244expect (bad .find ("shares the host's core" )!= std::string::npos ,"... and the worker on the host's core is named" ); 245const std::string unk = cpu::describe_worker_placement ({1 ,2 ,3 },0 , {}, false); 246expect (unk .find ("did not say" )!= std::string::npos && unk .find ("one per physical core" )== std::string::npos , 247"unknown layout: no claim" ); 248expect (cpu::describe_worker_placement ({2 ,4 },-1 ,win , true).find ("your --pool-cores list" )!= std::string::npos && 249 cpu::describe_worker_placement ({2 ,4 },-1 ,win , true).find ("host not pinned" )!= std::string::npos , 250"a given list and an unpinned host are said" ); 251 252// check_pool_cores: drop what cannot be used, warn about siblings and the host's processor, never refuse 253 cpu::PoolCoresCheck c = cpu::check_pool_cores ({2 ,4 ,6 ,8 },win ,0 ); 254expect (c .cpus == std::vector < int > ({2 ,4 ,6 ,8 })&& c .warnings .empty (),"check: one per core, no warning" ); 255c = cpu::check_pool_cores ({2 ,3 ,6 },win ,0 ); 256expect (c .cpus == std::vector < int > ({2 ,3 ,6 })&& c .warnings .size ()== 1 && c .warnings [0 ].find ("two hardware threads" )!= std::string::npos , 257"check: SMT siblings are kept with one warning" ); 258c = cpu::check_pool_cores ({0 ,2 },win ,0 ); 259expect (c .cpus == std::vector < int > ({0 ,2 })&& c .warnings .size ()== 1 && c .warnings [0 ].find ("host thread's" )!= std::string::npos , 260"check: the host's processor is kept with a warning" ); 261c = cpu::check_pool_cores ({1 ,2 },win ,0 ); 262expect (c .cpus == std::vector < int > ({1 ,2 })&& c .warnings .size ()== 1 && c .warnings [0 ].find ("shares a core with the host" )!= std::string::npos , 263"check: the host's SMT sibling is kept with a warning" ); 264c = cpu::check_pool_cores ({2 ,40 ,99 },win ,0 ); 265expect (c .cpus == std::vector < int > ({2 })&& c .warnings .size ()== 1 && c .warnings [0 ].find ("40,99" )!= std::string::npos , 266"check: processors this process may not use are left out and named" ); 267c = cpu::check_pool_cores ({40 },win ,0 ); 268expect (c .cpus .empty ()&& c .warnings .size ()== 1 ,"check: nothing usable" ); 269c = cpu::check_pool_cores ({2 ,3 }, {{2 ,-1 }, {3 ,-1 }},0 ); 270expect (c .cpus .size ()== 2 && c .warnings .empty (),"check: an unknown layout cannot tell siblings: no warning" ); 271 } 272 273if (g_fail ) { 274 std::fprintf (stderr ,"pool_plan_test: %d failure(s)\n" ,g_fail ); 275return 1 ; 276 } 277 std::printf ("pool_plan_test: OK\n" ); 278return 0 ; 279}