blob: 70ec8d031f9d69d401e1487c63181d22d1439f50 [file] [log] [blame]
Paul Mackerrasa12b51c2010-03-10 20:36:09 +11001#include "util.h"
Borislav Petkovcd0cfad2013-12-09 17:14:24 +01002#include <api/fs/fs.h>
Paul Mackerrasa12b51c2010-03-10 20:36:09 +11003#include "../perf.h"
4#include "cpumap.h"
5#include <assert.h>
6#include <stdio.h>
Stephane Eranian86ee6e12013-02-14 13:57:27 +01007#include <stdlib.h>
Jiri Olsaf30a79b2015-06-23 00:36:04 +02008#include "asm/bug.h"
Paul Mackerrasa12b51c2010-03-10 20:36:09 +11009
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020010static struct cpu_map *cpu_map__default_new(void)
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110011{
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020012 struct cpu_map *cpus;
13 int nr_cpus;
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110014
15 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020016 if (nr_cpus < 0)
17 return NULL;
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110018
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020019 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;
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110024
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020025 cpus->nr = nr_cpus;
Jiri Olsaf30a79b2015-06-23 00:36:04 +020026 atomic_set(&cpus->refcnt, 1);
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020027 }
28
29 return cpus;
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110030}
31
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020032static struct cpu_map *cpu_map__trim_new(int nr_cpus, int *tmp_cpus)
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110033{
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020034 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);
Jiri Olsaf30a79b2015-06-23 00:36:04 +020040 atomic_set(&cpus->refcnt, 1);
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020041 }
42
43 return cpus;
44}
45
Yan, Zheng7ae92e72012-09-10 15:53:50 +080046struct cpu_map *cpu_map__read(FILE *file)
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020047{
48 struct cpu_map *cpus = NULL;
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110049 int nr_cpus = 0;
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020050 int *tmp_cpus = NULL, *tmp;
51 int max_entries = 0;
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110052 int n, cpu, prev;
53 char sep;
54
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110055 sep = 0;
56 prev = -1;
57 for (;;) {
Yan, Zheng7ae92e72012-09-10 15:53:50 +080058 n = fscanf(file, "%u%c", &cpu, &sep);
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110059 if (n <= 0)
60 break;
61 if (prev >= 0) {
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020062 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
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110072 while (++prev < cpu)
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020073 tmp_cpus[nr_cpus++] = prev;
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110074 }
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020075 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;
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110084 if (n == 2 && sep == '-')
85 prev = cpu;
86 else
87 prev = -1;
88 if (n == 1 || sep == '\n')
89 break;
90 }
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110091
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020092 if (nr_cpus > 0)
93 cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
94 else
95 cpus = cpu_map__default_new();
96out_free_tmp:
97 free(tmp_cpus);
Yan, Zheng7ae92e72012-09-10 15:53:50 +080098 return cpus;
99}
100
101static 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);
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200111 fclose(onlnf);
112 return cpus;
Paul Mackerrasa12b51c2010-03-10 20:36:09 +1100113}
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200114
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200115struct cpu_map *cpu_map__new(const char *cpu_list)
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200116{
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200117 struct cpu_map *cpus = NULL;
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200118 unsigned long start_cpu, end_cpu = 0;
119 char *p = NULL;
120 int i, nr_cpus = 0;
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200121 int *tmp_cpus = NULL, *tmp;
122 int max_entries = 0;
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200123
124 if (!cpu_list)
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200125 return cpu_map__read_all_cpu_map();
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200126
127 if (!isdigit(*cpu_list))
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200128 goto out;
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200129
130 while (isdigit(*cpu_list)) {
131 p = NULL;
132 start_cpu = strtoul(cpu_list, &p, 0);
133 if (start_cpu >= INT_MAX
134 || (*p != '\0' && *p != ',' && *p != '-'))
135 goto invalid;
136
137 if (*p == '-') {
138 cpu_list = ++p;
139 p = NULL;
140 end_cpu = strtoul(cpu_list, &p, 0);
141
142 if (end_cpu >= INT_MAX || (*p != '\0' && *p != ','))
143 goto invalid;
144
145 if (end_cpu < start_cpu)
146 goto invalid;
147 } else {
148 end_cpu = start_cpu;
149 }
150
151 for (; start_cpu <= end_cpu; start_cpu++) {
152 /* check for duplicates */
153 for (i = 0; i < nr_cpus; i++)
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200154 if (tmp_cpus[i] == (int)start_cpu)
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200155 goto invalid;
156
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200157 if (nr_cpus == max_entries) {
158 max_entries += MAX_NR_CPUS;
159 tmp = realloc(tmp_cpus, max_entries * sizeof(int));
160 if (tmp == NULL)
161 goto invalid;
162 tmp_cpus = tmp;
163 }
164 tmp_cpus[nr_cpus++] = (int)start_cpu;
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200165 }
166 if (*p)
167 ++p;
168
169 cpu_list = p;
170 }
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200171
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200172 if (nr_cpus > 0)
173 cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
174 else
175 cpus = cpu_map__default_new();
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200176invalid:
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200177 free(tmp_cpus);
178out:
179 return cpus;
180}
181
Arnaldo Carvalho de Melo9ae7d332012-01-19 14:07:23 -0200182size_t cpu_map__fprintf(struct cpu_map *map, FILE *fp)
183{
184 int i;
185 size_t printed = fprintf(fp, "%d cpu%s: ",
186 map->nr, map->nr > 1 ? "s" : "");
187 for (i = 0; i < map->nr; ++i)
188 printed += fprintf(fp, "%s%d", i ? ", " : "", map->map[i]);
189
190 return printed + fprintf(fp, "\n");
191}
192
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200193struct cpu_map *cpu_map__dummy_new(void)
194{
195 struct cpu_map *cpus = malloc(sizeof(*cpus) + sizeof(int));
196
197 if (cpus != NULL) {
198 cpus->nr = 1;
199 cpus->map[0] = -1;
Jiri Olsaf30a79b2015-06-23 00:36:04 +0200200 atomic_set(&cpus->refcnt, 1);
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200201 }
202
203 return cpus;
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200204}
Arnaldo Carvalho de Melo915fce22011-01-14 16:19:12 -0200205
Jiri Olsaf30a79b2015-06-23 00:36:04 +0200206static void cpu_map__delete(struct cpu_map *map)
Arnaldo Carvalho de Melo915fce22011-01-14 16:19:12 -0200207{
Jiri Olsaf30a79b2015-06-23 00:36:04 +0200208 if (map) {
209 WARN_ONCE(atomic_read(&map->refcnt) != 0,
210 "cpu_map refcnt unbalanced\n");
211 free(map);
212 }
213}
214
215struct cpu_map *cpu_map__get(struct cpu_map *map)
216{
217 if (map)
218 atomic_inc(&map->refcnt);
219 return map;
220}
221
222void cpu_map__put(struct cpu_map *map)
223{
224 if (map && atomic_dec_and_test(&map->refcnt))
225 cpu_map__delete(map);
Arnaldo Carvalho de Melo915fce22011-01-14 16:19:12 -0200226}
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100227
Arnaldo Carvalho de Melo5d8cf722015-09-11 10:49:45 -0300228static int cpu__get_topology_int(int cpu, const char *name, int *value)
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100229{
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100230 char path[PATH_MAX];
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100231
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100232 snprintf(path, PATH_MAX,
Arnaldo Carvalho de Melo5d8cf722015-09-11 10:49:45 -0300233 "devices/system/cpu/cpu%d/topology/%s", cpu, name);
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100234
Arnaldo Carvalho de Melo5d8cf722015-09-11 10:49:45 -0300235 return sysfs__read_int(path, value);
236}
Kan Liang193b6bd2015-09-01 09:58:11 -0400237
Arnaldo Carvalho de Melo5d8cf722015-09-11 10:49:45 -0300238int cpu_map__get_socket_id(int cpu)
239{
240 int value, ret = cpu__get_topology_int(cpu, "physical_package_id", &value);
241 return ret ?: value;
Kan Liang193b6bd2015-09-01 09:58:11 -0400242}
243
244int cpu_map__get_socket(struct cpu_map *map, int idx)
245{
246 int cpu;
247
248 if (idx > map->nr)
249 return -1;
250
251 cpu = map->map[idx];
252
253 return cpu_map__get_socket_id(cpu);
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100254}
255
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100256static int cmp_ids(const void *a, const void *b)
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100257{
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100258 return *(int *)a - *(int *)b;
259}
260
Jiri Olsaf1cbb8f2015-10-16 12:41:14 +0200261int cpu_map__build_map(struct cpu_map *cpus, struct cpu_map **res,
262 int (*f)(struct cpu_map *map, int cpu))
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100263{
264 struct cpu_map *c;
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100265 int nr = cpus->nr;
266 int cpu, s1, s2;
267
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100268 /* allocate as much as possible */
269 c = calloc(1, sizeof(*c) + nr * sizeof(int));
270 if (!c)
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100271 return -1;
272
273 for (cpu = 0; cpu < nr; cpu++) {
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100274 s1 = f(cpus, cpu);
275 for (s2 = 0; s2 < c->nr; s2++) {
276 if (s1 == c->map[s2])
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100277 break;
278 }
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100279 if (s2 == c->nr) {
280 c->map[c->nr] = s1;
281 c->nr++;
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100282 }
283 }
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100284 /* ensure we process id in increasing order */
285 qsort(c->map, c->nr, sizeof(int), cmp_ids);
286
Jiri Olsaf30a79b2015-06-23 00:36:04 +0200287 atomic_set(&cpus->refcnt, 1);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100288 *res = c;
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100289 return 0;
290}
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100291
Kan Liang193b6bd2015-09-01 09:58:11 -0400292int cpu_map__get_core_id(int cpu)
Stephane Eranian12c08a92013-02-14 13:57:29 +0100293{
Arnaldo Carvalho de Melo5d8cf722015-09-11 10:49:45 -0300294 int value, ret = cpu__get_topology_int(cpu, "core_id", &value);
295 return ret ?: value;
Kan Liang193b6bd2015-09-01 09:58:11 -0400296}
297
298int cpu_map__get_core(struct cpu_map *map, int idx)
299{
300 int cpu, s;
301
302 if (idx > map->nr)
Stephane Eranian12c08a92013-02-14 13:57:29 +0100303 return -1;
304
Kan Liang193b6bd2015-09-01 09:58:11 -0400305 cpu = map->map[idx];
306
307 cpu = cpu_map__get_core_id(cpu);
308
Stephane Eranian12c08a92013-02-14 13:57:29 +0100309 s = cpu_map__get_socket(map, idx);
310 if (s == -1)
311 return -1;
312
313 /*
314 * encode socket in upper 16 bits
315 * core_id is relative to socket, and
316 * we need a global id. So we combine
317 * socket+ core id
318 */
319 return (s << 16) | (cpu & 0xffff);
320}
321
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100322int cpu_map__build_socket_map(struct cpu_map *cpus, struct cpu_map **sockp)
323{
324 return cpu_map__build_map(cpus, sockp, cpu_map__get_socket);
325}
Stephane Eranian12c08a92013-02-14 13:57:29 +0100326
327int cpu_map__build_core_map(struct cpu_map *cpus, struct cpu_map **corep)
328{
329 return cpu_map__build_map(cpus, corep, cpu_map__get_core);
330}
Don Zickus7780c252014-04-07 14:55:21 -0400331
332/* setup simple routines to easily access node numbers given a cpu number */
333static int get_max_num(char *path, int *max)
334{
335 size_t num;
336 char *buf;
337 int err = 0;
338
339 if (filename__read_str(path, &buf, &num))
340 return -1;
341
342 buf[num] = '\0';
343
344 /* start on the right, to find highest node num */
345 while (--num) {
346 if ((buf[num] == ',') || (buf[num] == '-')) {
347 num++;
348 break;
349 }
350 }
351 if (sscanf(&buf[num], "%d", max) < 1) {
352 err = -1;
353 goto out;
354 }
355
356 /* convert from 0-based to 1-based */
357 (*max)++;
358
359out:
360 free(buf);
361 return err;
362}
363
364/* Determine highest possible cpu in the system for sparse allocation */
365static void set_max_cpu_num(void)
366{
367 const char *mnt;
368 char path[PATH_MAX];
369 int ret = -1;
370
371 /* set up default */
372 max_cpu_num = 4096;
373
374 mnt = sysfs__mountpoint();
375 if (!mnt)
376 goto out;
377
378 /* get the highest possible cpu number for a sparse allocation */
Don Zickusf5b1f4e2014-04-07 14:55:22 -0400379 ret = snprintf(path, PATH_MAX, "%s/devices/system/cpu/possible", mnt);
Don Zickus7780c252014-04-07 14:55:21 -0400380 if (ret == PATH_MAX) {
381 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
382 goto out;
383 }
384
385 ret = get_max_num(path, &max_cpu_num);
386
387out:
388 if (ret)
389 pr_err("Failed to read max cpus, using default of %d\n", max_cpu_num);
390}
391
392/* Determine highest possible node in the system for sparse allocation */
393static void set_max_node_num(void)
394{
395 const char *mnt;
396 char path[PATH_MAX];
397 int ret = -1;
398
399 /* set up default */
400 max_node_num = 8;
401
402 mnt = sysfs__mountpoint();
403 if (!mnt)
404 goto out;
405
406 /* get the highest possible cpu number for a sparse allocation */
407 ret = snprintf(path, PATH_MAX, "%s/devices/system/node/possible", mnt);
408 if (ret == PATH_MAX) {
409 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
410 goto out;
411 }
412
413 ret = get_max_num(path, &max_node_num);
414
415out:
416 if (ret)
417 pr_err("Failed to read max nodes, using default of %d\n", max_node_num);
418}
419
420static int init_cpunode_map(void)
421{
422 int i;
423
424 set_max_cpu_num();
425 set_max_node_num();
426
427 cpunode_map = calloc(max_cpu_num, sizeof(int));
428 if (!cpunode_map) {
429 pr_err("%s: calloc failed\n", __func__);
430 return -1;
431 }
432
433 for (i = 0; i < max_cpu_num; i++)
434 cpunode_map[i] = -1;
435
436 return 0;
437}
438
439int cpu__setup_cpunode_map(void)
440{
441 struct dirent *dent1, *dent2;
442 DIR *dir1, *dir2;
443 unsigned int cpu, mem;
444 char buf[PATH_MAX];
445 char path[PATH_MAX];
446 const char *mnt;
447 int n;
448
449 /* initialize globals */
450 if (init_cpunode_map())
451 return -1;
452
453 mnt = sysfs__mountpoint();
454 if (!mnt)
455 return 0;
456
457 n = snprintf(path, PATH_MAX, "%s/devices/system/node", mnt);
458 if (n == PATH_MAX) {
459 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
460 return -1;
461 }
462
463 dir1 = opendir(path);
464 if (!dir1)
465 return 0;
466
467 /* walk tree and setup map */
468 while ((dent1 = readdir(dir1)) != NULL) {
469 if (dent1->d_type != DT_DIR || sscanf(dent1->d_name, "node%u", &mem) < 1)
470 continue;
471
472 n = snprintf(buf, PATH_MAX, "%s/%s", path, dent1->d_name);
473 if (n == PATH_MAX) {
474 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
475 continue;
476 }
477
478 dir2 = opendir(buf);
479 if (!dir2)
480 continue;
481 while ((dent2 = readdir(dir2)) != NULL) {
482 if (dent2->d_type != DT_LNK || sscanf(dent2->d_name, "cpu%u", &cpu) < 1)
483 continue;
484 cpunode_map[cpu] = mem;
485 }
486 closedir(dir2);
487 }
488 closedir(dir1);
489 return 0;
490}