Line data Source code
1 : #define _GNU_SOURCE 2 : #include <errno.h> 3 : #include <sched.h> 4 : 5 : #include <unistd.h> 6 : #include <fcntl.h> 7 : #include <stdio.h> 8 : #include <stdlib.h> 9 : 10 : #include "fd_tile_private.h" 11 : 12 : int 13 : fd_cpuset_getaffinity( ulong pid, 14 5206 : fd_cpuset_t * mask ) { 15 5206 : # if defined(__linux__) 16 5206 : return sched_getaffinity( (int)pid, fd_cpuset_word_cnt<<3, (cpu_set_t *)fd_type_pun( mask ) ); 17 : # else 18 : (void)pid; (void)mask; 19 : errno = ENOTSUP; 20 : return -1; 21 : # endif 22 5206 : } 23 : 24 : int 25 : fd_cpuset_setaffinity( ulong pid, 26 9 : fd_cpuset_t const * mask ) { 27 9 : # if defined(__linux__) 28 9 : return sched_setaffinity( (int)pid, fd_cpuset_word_cnt<<3, (cpu_set_t const *)fd_type_pun_const( mask ) ); 29 : # else 30 : (void)pid; (void)mask; 31 : errno = ENOTSUP; 32 : return -1; 33 : # endif 34 9 : } 35 : 36 : ulong 37 0 : fd_tile_private_sibling_idx( ulong cpu_idx ) { 38 0 : # if defined(__linux__) 39 0 : char path[ PATH_MAX ]; 40 0 : FD_TEST( fd_cstr_printf_check( path, PATH_MAX, NULL, "/sys/devices/system/cpu/cpu%lu/topology/thread_siblings_list", cpu_idx ) ); 41 : 42 0 : int fd = open( path, O_RDONLY ); 43 0 : if( FD_UNLIKELY( -1==fd ) ) FD_LOG_ERR(( "open( \"%s\" ) failed (%i-%s)", path, errno, fd_io_strerror( errno ) )); 44 : 45 0 : char line[ 1024 ] = {0}; 46 0 : long bytes_read = read( fd, line, sizeof( line ) ); 47 0 : if( FD_UNLIKELY( -1==bytes_read ) ) FD_LOG_ERR(( "read( \"%s\" ) failed (%i-%s)", path, errno, fd_io_strerror( errno ) )); 48 0 : else if ( FD_UNLIKELY( (ulong)bytes_read>=sizeof( line ) ) ) FD_LOG_ERR(( "read( \"%s\" ) failed: buffer too small", path )); 49 : 50 0 : if( FD_UNLIKELY( close( fd ) ) ) FD_LOG_ERR(( "close( \"%s\" ) failed (%i-%s)", path, errno, fd_io_strerror( errno ) )); 51 : 52 0 : char * sep = strchr( line, ',' ); 53 0 : if( FD_UNLIKELY( !sep ) ) return ULONG_MAX; 54 : 55 0 : *sep = '\0'; 56 0 : errno = 0; 57 0 : char * endptr; 58 0 : ulong pair1 = strtoul( line, &endptr, 10 ); 59 0 : if( FD_UNLIKELY( *endptr!='\0' || errno==ERANGE || errno==EINVAL ) ) FD_LOG_ERR(( "failed to parse cpu siblings list of cpu%lu `%s`", cpu_idx, line )); 60 : 61 0 : ulong pair2 = strtoul( sep+1UL, &endptr, 10 ); 62 0 : if( FD_UNLIKELY( *endptr!='\n' || errno==ERANGE || errno==EINVAL ) ) FD_LOG_ERR(( "failed to parse cpu siblings list of cpu%lu `%s`", pair1, sep+1UL )); 63 : 64 0 : if( FD_LIKELY( pair1==cpu_idx ) ) return pair2; 65 0 : else if( FD_LIKELY( pair2==cpu_idx ) ) return pair1; 66 0 : else FD_LOG_ERR(( "failed to find sibling of cpu%lu", cpu_idx )); 67 : # else 68 : (void)cpu_idx; 69 : return ULONG_MAX; 70 : # endif 71 0 : }