okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
6a74320ca
main
1#pragma once 2 3#if defined(__linux__ ) 4#include <pthread.h> 5#include <sched.h> 6 7#include <cerrno> 8#include <climits> 9#include <cstring> 10#include <vector> 11 12namespace strata::kernels::cpu::detail { 13 14// CPU_ALLOC and the sized macros also handle kernels whose CPU mask exceeds CPU_SETSIZE. 15// Keep the native mask opaque: copy its bytes for storage and use CPU_*_S to inspect it. 16class LinuxCpuSet { 17public : 18explicit LinuxCpuSet (int cpus ) :bytes (CPU_ALLOC_SIZE (cpus )),set (CPU_ALLOC (cpus )) { 19if (set )CPU_ZERO_S (bytes ,set ); 20 } 21 ~LinuxCpuSet () {CPU_FREE (set ); } 22LinuxCpuSet (const LinuxCpuSet & )= delete ; 23LinuxCpuSet & operator= (const LinuxCpuSet & )= delete ; 24 25const size_t bytes ; 26cpu_set_t * const set ; 27}; 28 29// pthread affinity functions return an error number directly; they do not set errno. 30// A too-small query buffer returns EINVAL, even when the number of online CPUs is small. 31inline int get_thread_affinity (std::vector < unsigned long >& mask , std::vector < int >* cpus = nullptr ) { 32for (int count = CPU_SETSIZE ;;) { 33LinuxCpuSet current (count ); 34if (!current .set )return ENOMEM ; 35const int error = pthread_getaffinity_np (pthread_self (),current .bytes ,current .set ); 36if (error == 0 ) { 37mask .resize (current .bytes /sizeof (unsigned long )); 38 std::memcpy (mask .data (),current .set ,current .bytes ); 39if (cpus ) { 40cpus -> clear (); 41for (int cpu = 0 ;cpu < count ;++ cpu ) 42if (CPU_ISSET_S (cpu ,current .bytes ,current .set ))cpus -> push_back (cpu ); 43 } 44return 0 ; 45 } 46if (error != EINVAL || count > INT_MAX /2 )return error ; 47count *=2 ; 48 } 49} 50 51inline int set_thread_affinity (const std::vector < unsigned long >& mask ) { 52constexpr size_t word_bits = sizeof (unsigned long )* CHAR_BIT ; 53if (mask .empty ()|| mask .size ()> INT_MAX /word_bits )return EINVAL ; 54LinuxCpuSet target (static_cast < int > (mask .size ()* word_bits )); 55if (!target .set )return ENOMEM ; 56 std::memcpy (target .set ,mask .data (),target .bytes ); 57return pthread_setaffinity_np (pthread_self (),target .bytes ,target .set ); 58} 59 60inline int pin_thread_to_cpu (int core ) { 61if (core < 0 || core == INT_MAX )return EINVAL ; 62LinuxCpuSet target (core < CPU_SETSIZE ?CPU_SETSIZE :core + 1 ); 63if (!target .set )return ENOMEM ; 64CPU_SET_S (core ,target .bytes ,target .set ); 65return pthread_setaffinity_np (pthread_self (),target .bytes ,target .set ); 66} 67 68}// namespace strata::kernels::cpu::detail 69#elif defined(__APPLE__ ) 70#include <thread> 71#include <vector> 72 73// macOS has no thread-to-core pinning (the scheduler places threads; affinity tags are hints at most): every CPU is 74// allowed and a pin is a no-op, so the pool keeps its threads where the OS puts them. 75namespace strata::kernels::cpu::detail { 76inline int get_thread_affinity (std::vector < unsigned long >& mask , std::vector < int >* cpus = nullptr ) { 77mask .assign (1 , ~0ul ); 78if (cpus ) { 79cpus -> clear (); 80for (unsigned i = 0 ;i < std::thread::hardware_concurrency ();++ i )cpus -> push_back ((int )i ); 81 } 82return 0 ; 83} 84inline int set_thread_affinity (const std::vector < unsigned long >& ) {return 0 ; } 85inline int pin_thread_to_cpu (int ) {return 0 ; }// nothing to do: the OS places the thread 86}// namespace strata::kernels::cpu::detail 87#endif