| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
#ifndef _GNU_SOURCE |
| 30 |
# define _GNU_SOURCE |
| 31 |
#endif |
| 32 |
|
| 33 |
#include <sys/sysinfo.h> |
| 34 |
|
| 35 |
#include <ctype.h> |
| 36 |
#include <sched.h> |
| 37 |
#include <stdio.h> |
| 38 |
#include <stdlib.h> |
| 39 |
#include <string.h> |
| 40 |
|
| 41 |
#include "common.h" |
| 42 |
#include "cputopology.h" |
| 43 |
#include "sysfsparser.h" |
| 44 |
|
| 45 |
#define PATH_SYS_SYSTEM PATH_SYS "/devices/system" |
| 46 |
#define PATH_SYS_CPU PATH_SYS_SYSTEM "/cpu" |
| 47 |
|
| 48 |
#if !HAVE_DECL_CPU_ALLOC |
| 49 |
|
| 50 |
int |
| 51 |
__cpuset_count_s (size_t setsize, const cpu_set_t * set) |
| 52 |
{ |
| 53 |
int s = 0; |
| 54 |
const __cpu_mask *p = set->__bits; |
| 55 |
const __cpu_mask *end = &set->__bits[setsize / sizeof (__cpu_mask)]; |
| 56 |
|
| 57 |
while (p < end) |
| 58 |
{ |
| 59 |
__cpu_mask l = *p++; |
| 60 |
|
| 61 |
if (l == 0) |
| 62 |
continue; |
| 63 |
#if LONG_BIT > 32 |
| 64 |
l = (l & 0x5555555555555555ul) + ((l >> 1) & 0x5555555555555555ul); |
| 65 |
l = (l & 0x3333333333333333ul) + ((l >> 2) & 0x3333333333333333ul); |
| 66 |
l = (l & 0x0f0f0f0f0f0f0f0ful) + ((l >> 4) & 0x0f0f0f0f0f0f0f0ful); |
| 67 |
l = (l & 0x00ff00ff00ff00fful) + ((l >> 8) & 0x00ff00ff00ff00fful); |
| 68 |
l = (l & 0x0000ffff0000fffful) + ((l >> 16) & 0x0000ffff0000fffful); |
| 69 |
l = (l & 0x00000000fffffffful) + ((l >> 32) & 0x00000000fffffffful); |
| 70 |
#else |
| 71 |
l = (l & 0x55555555ul) + ((l >> 1) & 0x55555555ul); |
| 72 |
l = (l & 0x33333333ul) + ((l >> 2) & 0x33333333ul); |
| 73 |
l = (l & 0x0f0f0f0ful) + ((l >> 4) & 0x0f0f0f0ful); |
| 74 |
l = (l & 0x00ff00fful) + ((l >> 8) & 0x00ff00fful); |
| 75 |
l = (l & 0x0000fffful) + ((l >> 16) & 0x0000fffful); |
| 76 |
#endif |
| 77 |
s += l; |
| 78 |
} |
| 79 |
return s; |
| 80 |
} |
| 81 |
#endif |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
int |
| 94 |
get_processor_number_total (void) |
| 95 |
{ |
| 96 |
|
| 97 |
return |
| 98 |
#if defined (HAVE_GET_NPROCS_CONF) |
| 99 |
get_nprocs_conf (); |
| 100 |
#elif defined (HAVE_SYSCONF__SC_NPROCESSORS_CONF) |
| 101 |
sysconf (_SC_NPROCESSORS_CONF); |
| 102 |
#else |
| 103 |
-1; |
| 104 |
#endif |
| 105 |
} |
| 106 |
|
| 107 |
int |
| 108 |
get_processor_number_online (void) |
| 109 |
{ |
| 110 |
|
| 111 |
return |
| 112 |
#if defined (HAVE_GET_NPROCS) |
| 113 |
get_nprocs (); |
| 114 |
#elif defined (HAVE_SYSCONF__SC_NPROCESSORS_ONLN) |
| 115 |
sysconf (_SC_NPROCESSORS_ONLN); |
| 116 |
#else |
| 117 |
-1; |
| 118 |
#endif |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
int |
| 124 |
get_processor_number_kernel_max () |
| 125 |
{ |
| 126 |
unsigned long long value; |
| 127 |
|
| 128 |
sysfsparser_getvalue (&value, PATH_SYS_CPU "/kernel_max"); |
| 129 |
return value + 1; |
| 130 |
} |
| 131 |
|
| 132 |
static inline int |
| 133 |
char_to_val (int c) |
| 134 |
{ |
| 135 |
int cl; |
| 136 |
|
| 137 |
cl = tolower (c); |
| 138 |
if (c >= '0' && c <= '9') |
| 139 |
return c - '0'; |
| 140 |
else if (cl >= 'a' && cl <= 'f') |
| 141 |
return cl + (10 - 'a'); |
| 142 |
else |
| 143 |
return -1; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
static int |
| 149 |
cpumask_parse (const char *str, cpu_set_t *set, size_t setsize) |
| 150 |
{ |
| 151 |
const char *ptr; |
| 152 |
int len, cpu = 0; |
| 153 |
|
| 154 |
if (!str) |
| 155 |
return -1; |
| 156 |
|
| 157 |
len = strlen (str); |
| 158 |
ptr = str + len - 1; |
| 159 |
|
| 160 |
|
| 161 |
if (len > 1 && !memcmp (str, "0x", 2L)) |
| 162 |
str += 2; |
| 163 |
|
| 164 |
CPU_ZERO_S (setsize, set); |
| 165 |
|
| 166 |
while (ptr >= str) |
| 167 |
{ |
| 168 |
char val; |
| 169 |
|
| 170 |
|
| 171 |
if (*ptr == ',') |
| 172 |
ptr--; |
| 173 |
|
| 174 |
val = char_to_val (*ptr); |
| 175 |
if (val == (char) -1) |
| 176 |
return -1; |
| 177 |
if (val & 1) |
| 178 |
CPU_SET_S (cpu, setsize, set); |
| 179 |
if (val & 2) |
| 180 |
CPU_SET_S (cpu + 1, setsize, set); |
| 181 |
if (val & 4) |
| 182 |
CPU_SET_S (cpu + 2, setsize, set); |
| 183 |
if (val & 8) |
| 184 |
CPU_SET_S (cpu + 3, setsize, set); |
| 185 |
ptr--; |
| 186 |
cpu += 4; |
| 187 |
} |
| 188 |
|
| 189 |
return CPU_COUNT_S (setsize, set); |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
void |
| 195 |
get_cputopology_read (unsigned int *nsockets, unsigned int *ncores, |
| 196 |
unsigned int *nthreads) |
| 197 |
{ |
| 198 |
cpu_set_t *set; |
| 199 |
size_t cpu, |
| 200 |
|
| 201 |
maxcpus, setsize = 0; |
| 202 |
|
| 203 |
*nsockets = *ncores = *nthreads = 1; |
| 204 |
maxcpus = get_processor_number_kernel_max (); |
| 205 |
|
| 206 |
if (!(set = CPU_ALLOC (maxcpus))) |
| 207 |
return; |
| 208 |
setsize = CPU_ALLOC_SIZE (maxcpus); |
| 209 |
|
| 210 |
for (cpu = 0; cpu < maxcpus; cpu++) |
| 211 |
{ |
| 212 |
|
| 213 |
|
| 214 |
char *thread_siblings = |
| 215 |
sysfsparser_getline (PATH_SYS_CPU "/cpu%lu/topology/thread_siblings", |
| 216 |
(unsigned long)cpu); |
| 217 |
if (!thread_siblings) |
| 218 |
continue; |
| 219 |
|
| 220 |
|
| 221 |
*nthreads = cpumask_parse (thread_siblings, set, setsize); |
| 222 |
if (*nthreads == 0) |
| 223 |
*nthreads = 1; |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
char *core_siblings = |
| 228 |
sysfsparser_getline (PATH_SYS_CPU "/cpu%lu/topology/core_siblings", |
| 229 |
(unsigned long)cpu); |
| 230 |
|
| 231 |
*ncores = cpumask_parse (core_siblings, set, setsize) / *nthreads; |
| 232 |
if (*ncores == 0) |
| 233 |
*ncores = 1; |
| 234 |
|
| 235 |
int ncpus_online = get_processor_number_online (); |
| 236 |
unsigned int ncpus = ncpus_online > 0 ? ncpus_online : 0; |
| 237 |
|
| 238 |
*nsockets = ncpus / *nthreads / *ncores; |
| 239 |
if (!*nsockets) |
| 240 |
*nsockets = 1; |
| 241 |
|
| 242 |
free (core_siblings); |
| 243 |
free (thread_siblings); |
| 244 |
} |
| 245 |
|
| 246 |
CPU_FREE (set); |
| 247 |
} |