okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
2e279f758
main
1// home_cpus_test: the widest CPU set a thread of the engine takes (include/strata/platform/home_cpus.hpp). Without a home 2// set a thread that widens gets every CPU the process may use (as before); with one it stays inside it, also when it 3// started on a single CPU (the host's, inherited). No GPU. 4#include "strata/platform/home_cpus.hpp" 5 6#include <cstdio> 7#include <string> 8#include <thread> 9#include <vector> 10 11static int g_fail = 0 ; 12 13static void expect (bool ok ,const std::string & what ) { 14if (!ok ) { 15++ g_fail ; 16 std::fprintf (stderr ,"FAIL: %s\n" ,what .c_str ()); 17 } 18} 19 20#if defined(__linux__ ) 21static std::vector < int > mine () { 22 std::vector < int > out ; 23cpu_set_t m ; 24CPU_ZERO (& m ); 25if (sched_getaffinity (0 ,sizeof m ,& m )== 0 ) 26for (int c = 0 ;c < CPU_SETSIZE ;++ c ) 27if (CPU_ISSET (c ,& m ))out .push_back (c ); 28return out ; 29} 30 31static void pin_one (int cpu ) { 32cpu_set_t m ; 33CPU_ZERO (& m ); 34CPU_SET (cpu ,& m ); 35 (void )sched_setaffinity (0 ,sizeof m ,& m ); 36} 37#endif 38 39int main () { 40#if defined(__linux__ ) 41const std::vector < int > start = mine (); 42if (start .size ()< 2 ) { 43 std::printf ("home_cpus_test: skipped (this process may run on %zu CPU)\n" ,start .size ()); 44return 0 ; 45 } 46 {// no home: the old behaviour, every CPU the process may use 47const cpu_set_t h = strata::platform::home_mask (); 48expect (CPU_COUNT (& h )== CPU_SETSIZE ,"no home: the mask is every CPU number" ); 49 std::vector < int > got ; 50 std::thread t ([& ] { 51pin_one (start .front ()); 52 strata::platform::widen_to_home (); 53got = mine (); 54 }); 55t .join (); 56expect (got == start ,"no home: a pinned thread widens back to all the process's CPUs" ); 57 } 58const std::vector < int > half (start .begin (),start .begin ()+ (long ) (start .size () /2 )); 59 strata::platform::set_home_cpus ({});// empty: nothing changes 60 { 61const cpu_set_t h = strata::platform::home_mask (); 62expect (CPU_COUNT (& h )== CPU_SETSIZE ,"empty home list: still every CPU number" ); 63 } 64 strata::platform::set_home_cpus (half ); 65 { 66 std::vector < int > got ; 67 std::thread t ([& ] { 68pin_one (start .front ()); 69 strata::platform::widen_to_home (); 70got = mine (); 71 }); 72t .join (); 73expect (got == half ,"home set: a pinned thread widens to the home CPUs only" ); 74 std::vector < int > got2 ; 75 std::thread u ([& ] {// a thread that runs on every CPU of the process widens DOWN to the home set too 76 strata::platform::widen_to_home (); 77got2 = mine (); 78 }); 79u .join (); 80expect (got2 == half ,"home set: a wide thread ends on the home CPUs" ); 81 } 82expect (mine ()== start ,"the calling thread is not moved by set_home_cpus" ); 83#else 84 strata::platform::set_home_cpus ({0 }); 85 strata::platform::widen_to_home (); 86 std::printf ("home_cpus_test: not Linux, nothing to check\n" ); 87#endif 88if (g_fail != 0 ) { 89 std::fprintf (stderr ,"home_cpus_test: %d check(s) FAILED\n" ,g_fail ); 90return 1 ; 91 } 92 std::printf ("home_cpus_test: all checks passed\n" ); 93return 0 ; 94}