1 #include "util.h"
2 #include <api/fs/fs.h>
3 #include "../perf.h"
4 #include "cpumap.h"
5 #include <assert.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include "asm/bug.h"
9
cpu_map__default_new(void)10 static struct cpu_map *cpu_map__default_new(void)
11 {
12 struct cpu_map *cpus;
13 int nr_cpus;
14
15 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
16 if (nr_cpus < 0)
17 return NULL;
18
19 cpus = malloc(sizeof(*cpus) + nr_cpus * sizeof(int));
20 if (cpus != NULL) {
21 int i;
22 for (i = 0; i < nr_cpus; ++i)
23 cpus->map[i] = i;
24
25 cpus->nr = nr_cpus;
26 atomic_set(&cpus->refcnt, 1);
27 }
28
29 return cpus;
30 }
31
cpu_map__trim_new(int nr_cpus,int * tmp_cpus)32 static struct cpu_map *cpu_map__trim_new(int nr_cpus, int *tmp_cpus)
33 {
34 size_t payload_size = nr_cpus * sizeof(int);
35 struct cpu_map *cpus = malloc(sizeof(*cpus) + payload_size);
36
37 if (cpus != NULL) {
38 cpus->nr = nr_cpus;
39 memcpy(cpus->map, tmp_cpus, payload_size);
40 atomic_set(&cpus->refcnt, 1);
41 }
42
43 return cpus;
44 }
45
cpu_map__read(FILE * file)46 struct cpu_map *cpu_map__read(FILE *file)
47 {
48 struct cpu_map *cpus = NULL;
49 int nr_cpus = 0;
50 int *tmp_cpus = NULL, *tmp;
51 int max_entries = 0;
52 int n, cpu, prev;
53 char sep;
54
55 sep = 0;
56 prev = -1;
57 for (;;) {
58 n = fscanf(file, "%u%c", &cpu, &sep);
59 if (n <= 0)
60 break;
61 if (prev >= 0) {
62 int new_max = nr_cpus + cpu - prev - 1;
63
64 if (new_max >= max_entries) {
65 max_entries = new_max + MAX_NR_CPUS / 2;
66 tmp = realloc(tmp_cpus, max_entries * sizeof(int));
67 if (tmp == NULL)
68 goto out_free_tmp;
69 tmp_cpus = tmp;
70 }
71
72 while (++prev < cpu)
73 tmp_cpus[nr_cpus++] = prev;
74 }
75 if (nr_cpus == max_entries) {
76 max_entries += MAX_NR_CPUS;
77 tmp = realloc(tmp_cpus, max_entries * sizeof(int));
78 if (tmp == NULL)
79 goto out_free_tmp;
80 tmp_cpus = tmp;
81 }
82
83 tmp_cpus[nr_cpus++] = cpu;
84 if (n == 2 && sep == '-')
85 prev = cpu;
86 else
87 prev = -1;
88 if (n == 1 || sep == '\n')
89 break;
90 }
91
92 if (nr_cpus > 0)
93 cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
94 else
95 cpus = cpu_map__default_new();
96 out_free_tmp:
97 free(tmp_cpus);
98 return cpus;
99 }
100
cpu_map__read_all_cpu_map(void)101 static struct cpu_map *cpu_map__read_all_cpu_map(void)
102 {
103 struct cpu_map *cpus = NULL;
104 FILE *onlnf;
105
106 onlnf = fopen("/sys/devices/system/cpu/online", "r");
107 if (!onlnf)
108 return cpu_map__default_new();
109
110 cpus = cpu_map__read(onlnf);
111 fclose(onlnf);
112 return cpus;
113 }
114
cpu_map__new(const char * cpu_list)115 struct cpu_map *cpu_map__new(const char *cpu_list)
116 {
117 struct cpu_map *cpus = NULL;
118 unsigned long start_cpu, end_cpu = 0;
119 char *p = NULL;
120 int i, nr_cpus = 0;
121 int *tmp_cpus = NULL, *tmp;
122 int max_entries = 0;
123
124 if (!cpu_list)
125 return cpu_map__read_all_cpu_map();
126
127 /*
128 * must handle the case of empty cpumap to cover
129 * TOPOLOGY header for NUMA nodes with no CPU
130 * ( e.g., because of CPU hotplug)
131 */
132 if (!isdigit(*cpu_list) && *cpu_list != '\0')
133 goto out;
134
135 while (isdigit(*cpu_list)) {
136 p = NULL;
137 start_cpu = strtoul(cpu_list, &p, 0);
138 if (start_cpu >= INT_MAX
139 || (*p != '\0' && *p != ',' && *p != '-'))
140 goto invalid;
141
142 if (*p == '-') {
143 cpu_list = ++p;
144 p = NULL;
145 end_cpu = strtoul(cpu_list, &p, 0);
146
147 if (end_cpu >= INT_MAX || (*p != '\0' && *p != ','))
148 goto invalid;
149
150 if (end_cpu < start_cpu)
151 goto invalid;
152 } else {
153 end_cpu = start_cpu;
154 }
155
156 for (; start_cpu <= end_cpu; start_cpu++) {
157 /* check for duplicates */
158 for (i = 0; i < nr_cpus; i++)
159 if (tmp_cpus[i] == (int)start_cpu)
160 goto invalid;
161
162 if (nr_cpus == max_entries) {
163 max_entries += MAX_NR_CPUS;
164 tmp = realloc(tmp_cpus, max_entries * sizeof(int));
165 if (tmp == NULL)
166 goto invalid;
167 tmp_cpus = tmp;
168 }
169 tmp_cpus[nr_cpus++] = (int)start_cpu;
170 }
171 if (*p)
172 ++p;
173
174 cpu_list = p;
175 }
176
177 if (nr_cpus > 0)
178 cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
179 else if (*cpu_list != '\0')
180 cpus = cpu_map__default_new();
181 else
182 cpus = cpu_map__dummy_new();
183 invalid:
184 free(tmp_cpus);
185 out:
186 return cpus;
187 }
188
cpu_map__fprintf(struct cpu_map * map,FILE * fp)189 size_t cpu_map__fprintf(struct cpu_map *map, FILE *fp)
190 {
191 int i;
192 size_t printed = fprintf(fp, "%d cpu%s: ",
193 map->nr, map->nr > 1 ? "s" : "");
194 for (i = 0; i < map->nr; ++i)
195 printed += fprintf(fp, "%s%d", i ? ", " : "", map->map[i]);
196
197 return printed + fprintf(fp, "\n");
198 }
199
cpu_map__dummy_new(void)200 struct cpu_map *cpu_map__dummy_new(void)
201 {
202 struct cpu_map *cpus = malloc(sizeof(*cpus) + sizeof(int));
203
204 if (cpus != NULL) {
205 cpus->nr = 1;
206 cpus->map[0] = -1;
207 atomic_set(&cpus->refcnt, 1);
208 }
209
210 return cpus;
211 }
212
cpu_map__empty_new(int nr)213 struct cpu_map *cpu_map__empty_new(int nr)
214 {
215 struct cpu_map *cpus = malloc(sizeof(*cpus) + sizeof(int) * nr);
216
217 if (cpus != NULL) {
218 int i;
219
220 cpus->nr = nr;
221 for (i = 0; i < nr; i++)
222 cpus->map[i] = -1;
223
224 atomic_set(&cpus->refcnt, 1);
225 }
226
227 return cpus;
228 }
229
cpu_map__delete(struct cpu_map * map)230 static void cpu_map__delete(struct cpu_map *map)
231 {
232 if (map) {
233 WARN_ONCE(atomic_read(&map->refcnt) != 0,
234 "cpu_map refcnt unbalanced\n");
235 free(map);
236 }
237 }
238
cpu_map__get(struct cpu_map * map)239 struct cpu_map *cpu_map__get(struct cpu_map *map)
240 {
241 if (map)
242 atomic_inc(&map->refcnt);
243 return map;
244 }
245
cpu_map__put(struct cpu_map * map)246 void cpu_map__put(struct cpu_map *map)
247 {
248 if (map && atomic_dec_and_test(&map->refcnt))
249 cpu_map__delete(map);
250 }
251
cpu__get_topology_int(int cpu,const char * name,int * value)252 static int cpu__get_topology_int(int cpu, const char *name, int *value)
253 {
254 char path[PATH_MAX];
255
256 snprintf(path, PATH_MAX,
257 "devices/system/cpu/cpu%d/topology/%s", cpu, name);
258
259 return sysfs__read_int(path, value);
260 }
261
cpu_map__get_socket_id(int cpu)262 int cpu_map__get_socket_id(int cpu)
263 {
264 int value, ret = cpu__get_topology_int(cpu, "physical_package_id", &value);
265 return ret ?: value;
266 }
267
cpu_map__get_socket(struct cpu_map * map,int idx,void * data __maybe_unused)268 int cpu_map__get_socket(struct cpu_map *map, int idx, void *data __maybe_unused)
269 {
270 int cpu;
271
272 if (idx > map->nr)
273 return -1;
274
275 cpu = map->map[idx];
276
277 return cpu_map__get_socket_id(cpu);
278 }
279
cmp_ids(const void * a,const void * b)280 static int cmp_ids(const void *a, const void *b)
281 {
282 return *(int *)a - *(int *)b;
283 }
284
cpu_map__build_map(struct cpu_map * cpus,struct cpu_map ** res,int (* f)(struct cpu_map * map,int cpu,void * data),void * data)285 int cpu_map__build_map(struct cpu_map *cpus, struct cpu_map **res,
286 int (*f)(struct cpu_map *map, int cpu, void *data),
287 void *data)
288 {
289 struct cpu_map *c;
290 int nr = cpus->nr;
291 int cpu, s1, s2;
292
293 /* allocate as much as possible */
294 c = calloc(1, sizeof(*c) + nr * sizeof(int));
295 if (!c)
296 return -1;
297
298 for (cpu = 0; cpu < nr; cpu++) {
299 s1 = f(cpus, cpu, data);
300 for (s2 = 0; s2 < c->nr; s2++) {
301 if (s1 == c->map[s2])
302 break;
303 }
304 if (s2 == c->nr) {
305 c->map[c->nr] = s1;
306 c->nr++;
307 }
308 }
309 /* ensure we process id in increasing order */
310 qsort(c->map, c->nr, sizeof(int), cmp_ids);
311
312 atomic_set(&c->refcnt, 1);
313 *res = c;
314 return 0;
315 }
316
cpu_map__get_core_id(int cpu)317 int cpu_map__get_core_id(int cpu)
318 {
319 int value, ret = cpu__get_topology_int(cpu, "core_id", &value);
320 return ret ?: value;
321 }
322
cpu_map__get_core(struct cpu_map * map,int idx,void * data)323 int cpu_map__get_core(struct cpu_map *map, int idx, void *data)
324 {
325 int cpu, s;
326
327 if (idx > map->nr)
328 return -1;
329
330 cpu = map->map[idx];
331
332 cpu = cpu_map__get_core_id(cpu);
333
334 s = cpu_map__get_socket(map, idx, data);
335 if (s == -1)
336 return -1;
337
338 /*
339 * encode socket in upper 16 bits
340 * core_id is relative to socket, and
341 * we need a global id. So we combine
342 * socket+ core id
343 */
344 return (s << 16) | (cpu & 0xffff);
345 }
346
cpu_map__build_socket_map(struct cpu_map * cpus,struct cpu_map ** sockp)347 int cpu_map__build_socket_map(struct cpu_map *cpus, struct cpu_map **sockp)
348 {
349 return cpu_map__build_map(cpus, sockp, cpu_map__get_socket, NULL);
350 }
351
cpu_map__build_core_map(struct cpu_map * cpus,struct cpu_map ** corep)352 int cpu_map__build_core_map(struct cpu_map *cpus, struct cpu_map **corep)
353 {
354 return cpu_map__build_map(cpus, corep, cpu_map__get_core, NULL);
355 }
356
357 /* setup simple routines to easily access node numbers given a cpu number */
get_max_num(char * path,int * max)358 static int get_max_num(char *path, int *max)
359 {
360 size_t num;
361 char *buf;
362 int err = 0;
363
364 if (filename__read_str(path, &buf, &num))
365 return -1;
366
367 buf[num] = '\0';
368
369 /* start on the right, to find highest node num */
370 while (--num) {
371 if ((buf[num] == ',') || (buf[num] == '-')) {
372 num++;
373 break;
374 }
375 }
376 if (sscanf(&buf[num], "%d", max) < 1) {
377 err = -1;
378 goto out;
379 }
380
381 /* convert from 0-based to 1-based */
382 (*max)++;
383
384 out:
385 free(buf);
386 return err;
387 }
388
389 /* Determine highest possible cpu in the system for sparse allocation */
set_max_cpu_num(void)390 static void set_max_cpu_num(void)
391 {
392 const char *mnt;
393 char path[PATH_MAX];
394 int ret = -1;
395
396 /* set up default */
397 max_cpu_num = 4096;
398
399 mnt = sysfs__mountpoint();
400 if (!mnt)
401 goto out;
402
403 /* get the highest possible cpu number for a sparse allocation */
404 ret = snprintf(path, PATH_MAX, "%s/devices/system/cpu/possible", mnt);
405 if (ret == PATH_MAX) {
406 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
407 goto out;
408 }
409
410 ret = get_max_num(path, &max_cpu_num);
411
412 out:
413 if (ret)
414 pr_err("Failed to read max cpus, using default of %d\n", max_cpu_num);
415 }
416
417 /* Determine highest possible node in the system for sparse allocation */
set_max_node_num(void)418 static void set_max_node_num(void)
419 {
420 const char *mnt;
421 char path[PATH_MAX];
422 int ret = -1;
423
424 /* set up default */
425 max_node_num = 8;
426
427 mnt = sysfs__mountpoint();
428 if (!mnt)
429 goto out;
430
431 /* get the highest possible cpu number for a sparse allocation */
432 ret = snprintf(path, PATH_MAX, "%s/devices/system/node/possible", mnt);
433 if (ret == PATH_MAX) {
434 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
435 goto out;
436 }
437
438 ret = get_max_num(path, &max_node_num);
439
440 out:
441 if (ret)
442 pr_err("Failed to read max nodes, using default of %d\n", max_node_num);
443 }
444
init_cpunode_map(void)445 static int init_cpunode_map(void)
446 {
447 int i;
448
449 set_max_cpu_num();
450 set_max_node_num();
451
452 cpunode_map = calloc(max_cpu_num, sizeof(int));
453 if (!cpunode_map) {
454 pr_err("%s: calloc failed\n", __func__);
455 return -1;
456 }
457
458 for (i = 0; i < max_cpu_num; i++)
459 cpunode_map[i] = -1;
460
461 return 0;
462 }
463
cpu__setup_cpunode_map(void)464 int cpu__setup_cpunode_map(void)
465 {
466 struct dirent *dent1, *dent2;
467 DIR *dir1, *dir2;
468 unsigned int cpu, mem;
469 char buf[PATH_MAX];
470 char path[PATH_MAX];
471 const char *mnt;
472 int n;
473
474 /* initialize globals */
475 if (init_cpunode_map())
476 return -1;
477
478 mnt = sysfs__mountpoint();
479 if (!mnt)
480 return 0;
481
482 n = snprintf(path, PATH_MAX, "%s/devices/system/node", mnt);
483 if (n == PATH_MAX) {
484 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
485 return -1;
486 }
487
488 dir1 = opendir(path);
489 if (!dir1)
490 return 0;
491
492 /* walk tree and setup map */
493 while ((dent1 = readdir(dir1)) != NULL) {
494 if (dent1->d_type != DT_DIR || sscanf(dent1->d_name, "node%u", &mem) < 1)
495 continue;
496
497 n = snprintf(buf, PATH_MAX, "%s/%s", path, dent1->d_name);
498 if (n == PATH_MAX) {
499 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
500 continue;
501 }
502
503 dir2 = opendir(buf);
504 if (!dir2)
505 continue;
506 while ((dent2 = readdir(dir2)) != NULL) {
507 if (dent2->d_type != DT_LNK || sscanf(dent2->d_name, "cpu%u", &cpu) < 1)
508 continue;
509 cpunode_map[cpu] = mem;
510 }
511 closedir(dir2);
512 }
513 closedir(dir1);
514 return 0;
515 }
516