okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
7c5e84612
main
1#include "strata/kernels/cpu/pool.hpp" 2#include "pool_affinity_win.hpp" 3 4#include <algorithm> 5#include <cstdio> 6#include <thread> 7#include <vector> 8 9namespace cpu= strata::kernels::cpu; 10 11static bool check (bool ok ,const char * message ) { 12if (!ok ) std::fprintf (stderr ,"Windows affinity test: %s (error %lu)\n" ,message , (unsigned long )GetLastError ()); 13return ok ; 14} 15 16static bool select (const std::vector < ULONG >& ids ) { 17return SetThreadSelectedCpuSets (GetCurrentThread (),ids .empty () ?nullptr :ids .data (), (ULONG )ids .size ())!= 0 ; 18} 19 20static bool selected_is (std::vector < ULONG > expected ) { 21 std::vector < ULONG > actual ; 22if (!cpu::detail::get_thread_cpu_sets (actual ))return false; 23 std::sort (actual .begin (),actual .end ()); 24 std::sort (expected .begin (),expected .end ()); 25return actual == expected ; 26} 27 28// Establish reachability before the round trip: CPU Sets are soft affinity and cannot override an 29// existing job/hard-affinity restriction. Afterwards, require the same observable reachability. 30static bool observe (int core ) { 31for (int i = 0 ;i < 200 ;++ i ) { 32Sleep (1 ); 33PROCESSOR_NUMBER current {}; 34GetCurrentProcessorNumberEx (& current ); 35if ((int )current .Group * 64 + current .Number == core )return true; 36 } 37return false; 38} 39 40static bool host_round_trips (const std::vector < int >& cores ,const std::vector < ULONG >& ids ) { 41if (!check (select ({}),"clear initial thread CPU Sets" ))return false; 42 std::vector < bool > reachable ; 43for (size_t i = 0 ;i < cores .size ();++ i ) { 44if (!check (select ({ids [i ]}),"select baseline CPU Set" ))return false; 45reachable .push_back (observe (cores [i ])); 46 } 47if (!check (select ({}),"clear baseline CPU Sets" ))return false; 48 49for (size_t i = 0 ;i < cores .size ();++ i ) { 50const auto previous = cpu::pin_current_thread (cores [i ]); 51const bool pinned = previous .valid && previous .cpu_sets .empty ()&& selected_is ({ids [i ]}); 52 cpu::restore_thread_affinity (previous ); 53if (!check (pinned && selected_is ({}),"restore empty thread assignment" ))return false; 54 } 55 56// GetThreadGroupAffinity alone misses a lost implicit all-group state. Selecting each CPU Set 57// again must still permit execution there, without ever setting a hard group affinity. 58for (size_t i = 0 ;i < cores .size ();++ i ) { 59if (!check (select ({ids [i ]}),"select CPU Set after restore" ))return false; 60if (reachable [i ]) { 61if (!check (observe (cores [i ]),"lost CPU/group eligibility after host restore" ))return false; 62 std::printf ("Host restore: still executes on group %d processor %d\n" ,cores [i ] /64 ,cores [i ]& 63 ); 63 }else { 64 std::printf ("SKIP scheduling observation for group %d: not reachable before test\n" ,cores [i ] /64 ); 65 } 66 } 67 68// A nonempty selection may span groups. Nested host selections must restore the entire set. 69if (!check (select (ids ),"set existing multi-CPU selection" ))return false; 70const auto outer = cpu::pin_current_thread (cores .front ()); 71const auto inner = cpu::pin_current_thread (cores .back ()); 72const bool inner_ok = inner .valid && selected_is ({ids .back ()}); 73 cpu::restore_thread_affinity (inner ); 74const bool outer_ok = outer .valid && selected_is ({ids .front ()}); 75 cpu::restore_thread_affinity (outer ); 76if (!check (inner_ok && outer_ok && selected_is (ids ),"restore nested / existing CPU Set selections" ))return false; 77const auto invalid = cpu::pin_current_thread (-1 ); 78 cpu::restore_thread_affinity (invalid ); 79if (!check (!invalid .valid && selected_is (ids ),"invalid target must not mutate selection" ))return false; 80return check (select ({}),"clear final host selection" ); 81} 82 83static bool inherited_default (int core ,ULONG id ,int temporary_core ,ULONG temporary_id ) { 84ULONG count = 0 ; 85if (!GetProcessDefaultCpuSets (GetCurrentProcess (),nullptr ,0 ,& count )&& 86GetLastError ()!= ERROR_INSUFFICIENT_BUFFER )return false; 87 std::vector < ULONG > saved (count ); 88if (count && !GetProcessDefaultCpuSets (GetCurrentProcess (),saved .data (),count ,& count ))return false; 89saved .resize (count ); 90if (!check (SetProcessDefaultCpuSets (GetCurrentProcess (),& id ,1 )!= 0 ,"set process default" ))return false; 91bool ok = false; 92 std::thread host ([& ] { 93if (!select ({}))return ; 94const bool reachable = observe (core ); 95const auto previous = cpu::pin_current_thread (temporary_core ); 96const bool overridden = previous .valid && selected_is ({temporary_id }); 97 cpu::restore_thread_affinity (previous ); 98ULONG inherited = 0 ,required = 0 ; 99ok = overridden && previous .cpu_sets .empty ()&& selected_is ({})&& 100GetProcessDefaultCpuSets (GetCurrentProcess (),& inherited ,1 ,& required )&& 101required == 1 && inherited == id && (!reachable || observe (core )); 102 }); 103host .join (); 104const bool restored = SetProcessDefaultCpuSets (GetCurrentProcess (),saved .empty () ?nullptr :saved .data (), 105 (ULONG )saved .size ())!= 0 ; 106return check (ok && restored ,"restore inherited process default" ); 107} 108 109// --host-core sibling: the host on the first layout's host core's SMT sibling, which no worker takes, and the first 110// logical processor left to nothing; `first` on a core without SMT. Every affinity mode. 111static bool host_sibling_layout () { 112bool ok = true; 113for (const auto affinity : {cpu::PoolAffinity ::All , cpu::PoolAffinity ::Auto , cpu::PoolAffinity ::PCores }) { 114 cpu::set_host_core (cpu::HostCore ::First ); 115const cpu::CpuTopology first = cpu::detect_cpu_topology (true,affinity ); 116 cpu::set_host_core (cpu::HostCore ::Sibling ); 117const cpu::CpuTopology sib = cpu::detect_cpu_topology (true,affinity ); 118 cpu::set_host_core (cpu::HostCore ::First ); 119const auto has = [& ](int core ) { 120return std::find (sib .worker_cores .begin (),sib .worker_cores .end (),core )!= sib .worker_cores .end (); 121 }; 122if (first .host_sibling < 0 ) 123ok = check (sib .host_core == first .host_core && sib .worker_cores == first .worker_cores , 124"--host-core sibling without SMT must be first" )&& ok ; 125else 126ok = check (sib .host_core == first .host_sibling && sib .host_core != first .host_core && !has (sib .host_core )&& 127 !has (first .host_core ), 128"--host-core sibling: the host on the sibling, no worker on its core" )&& ok ; 129 } 130return ok ; 131} 132 133int main () { 134if (!host_sibling_layout ())return 1 ; 135const auto all = cpu::physical_cores (false); 136 std::vector < int > cores ; 137 std::vector < ULONG > ids ; 138for (int core :all ) { 139if (std::any_of (cores .begin (),cores .end (), [core ](int other ) {return other /64 == core /64 ; }))continue ; 140ULONG id = 0 ; 141if (!cpu::detail::cpu_set_for_core (core ,id ))continue ; 142cores .push_back (core ); 143ids .push_back (id ); 144 } 145if (!check (!cores .empty (),"no usable CPU Sets" ))return 1 ; 146if (!check (cores .size ()== GetActiveProcessorGroupCount (),"CPU Sets do not cover all active groups" ))return 1 ; 147 148// Hard pins belong on pool-owned threads; never contaminate the caller under test with one. 149for (size_t i = 0 ;i < cores .size ();++ i ) { 150bool ok = false; 151 std::thread worker ([& ] { 152if (!cpu::detail::set_thread_group_affinity (cores [i ], (int )i ))return ; 153GROUP_AFFINITY pinned {}; 154if (!GetThreadGroupAffinity (GetCurrentThread (),& pinned ))return ; 155ok = pinned .Group == cores [i ] /64 && pinned .Mask == (KAFFINITY (1 ) << (cores [i ]& 63 )); 156// Host placement must also preserve an existing restrictive hard affinity. 157const auto previous = cpu::pin_current_thread (cores .back ()); 158 cpu::restore_thread_affinity (previous ); 159GROUP_AFFINITY after {}; 160ok = ok && previous .valid && GetThreadGroupAffinity (GetCurrentThread (),& after )&& 161after .Group == pinned .Group && after .Mask == pinned .Mask ; 162 }); 163worker .join (); 164if (!check (ok ,"worker hard pin / caller hard-affinity preservation" ))return 1 ; 165 } 166 167bool host_ok = false; 168 std::thread host ([& ] {host_ok = host_round_trips (cores ,ids ); }); 169host .join (); 170if (!host_ok || !inherited_default (cores .back (),ids .back (),cores .front (),ids .front ()))return 1 ; 171 std::printf ("Windows affinity test: worker pins, host CPU Sets and process-default restoration passed (%zu groups)\n" ,cores .size ()); 172return 0 ; 173}