blob: 39ad2caccf56bcf5eaaa01f5bd9b1f171fb81121 [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 Olsaf77b57a2015-10-25 15:51:25 +01008#include <linux/bitmap.h>
Jiri Olsaf30a79b2015-06-23 00:36:04 +02009#include "asm/bug.h"
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110010
Arnaldo Carvalho de Melo5ac76282016-01-26 15:51:46 -030011static int max_cpu_num;
Jan Stancek92a7e122017-02-17 12:10:24 +010012static int max_present_cpu_num;
Arnaldo Carvalho de Melo5ac76282016-01-26 15:51:46 -030013static int max_node_num;
14static int *cpunode_map;
15
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020016static struct cpu_map *cpu_map__default_new(void)
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110017{
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020018 struct cpu_map *cpus;
19 int nr_cpus;
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110020
21 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020022 if (nr_cpus < 0)
23 return NULL;
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110024
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020025 cpus = malloc(sizeof(*cpus) + nr_cpus * sizeof(int));
26 if (cpus != NULL) {
27 int i;
28 for (i = 0; i < nr_cpus; ++i)
29 cpus->map[i] = i;
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110030
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020031 cpus->nr = nr_cpus;
Elena Reshetovaec09a422017-02-21 17:34:56 +020032 refcount_set(&cpus->refcnt, 1);
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020033 }
34
35 return cpus;
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110036}
37
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020038static struct cpu_map *cpu_map__trim_new(int nr_cpus, int *tmp_cpus)
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110039{
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020040 size_t payload_size = nr_cpus * sizeof(int);
41 struct cpu_map *cpus = malloc(sizeof(*cpus) + payload_size);
42
43 if (cpus != NULL) {
44 cpus->nr = nr_cpus;
45 memcpy(cpus->map, tmp_cpus, payload_size);
Elena Reshetovaec09a422017-02-21 17:34:56 +020046 refcount_set(&cpus->refcnt, 1);
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020047 }
48
49 return cpus;
50}
51
Yan, Zheng7ae92e72012-09-10 15:53:50 +080052struct cpu_map *cpu_map__read(FILE *file)
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020053{
54 struct cpu_map *cpus = NULL;
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110055 int nr_cpus = 0;
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020056 int *tmp_cpus = NULL, *tmp;
57 int max_entries = 0;
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110058 int n, cpu, prev;
59 char sep;
60
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110061 sep = 0;
62 prev = -1;
63 for (;;) {
Yan, Zheng7ae92e72012-09-10 15:53:50 +080064 n = fscanf(file, "%u%c", &cpu, &sep);
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110065 if (n <= 0)
66 break;
67 if (prev >= 0) {
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020068 int new_max = nr_cpus + cpu - prev - 1;
69
70 if (new_max >= max_entries) {
71 max_entries = new_max + MAX_NR_CPUS / 2;
72 tmp = realloc(tmp_cpus, max_entries * sizeof(int));
73 if (tmp == NULL)
74 goto out_free_tmp;
75 tmp_cpus = tmp;
76 }
77
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110078 while (++prev < cpu)
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020079 tmp_cpus[nr_cpus++] = prev;
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110080 }
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020081 if (nr_cpus == max_entries) {
82 max_entries += MAX_NR_CPUS;
83 tmp = realloc(tmp_cpus, max_entries * sizeof(int));
84 if (tmp == NULL)
85 goto out_free_tmp;
86 tmp_cpus = tmp;
87 }
88
89 tmp_cpus[nr_cpus++] = cpu;
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110090 if (n == 2 && sep == '-')
91 prev = cpu;
92 else
93 prev = -1;
94 if (n == 1 || sep == '\n')
95 break;
96 }
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110097
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020098 if (nr_cpus > 0)
99 cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
100 else
101 cpus = cpu_map__default_new();
102out_free_tmp:
103 free(tmp_cpus);
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800104 return cpus;
105}
106
107static struct cpu_map *cpu_map__read_all_cpu_map(void)
108{
109 struct cpu_map *cpus = NULL;
110 FILE *onlnf;
111
112 onlnf = fopen("/sys/devices/system/cpu/online", "r");
113 if (!onlnf)
114 return cpu_map__default_new();
115
116 cpus = cpu_map__read(onlnf);
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200117 fclose(onlnf);
118 return cpus;
Paul Mackerrasa12b51c2010-03-10 20:36:09 +1100119}
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200120
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200121struct cpu_map *cpu_map__new(const char *cpu_list)
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200122{
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200123 struct cpu_map *cpus = NULL;
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200124 unsigned long start_cpu, end_cpu = 0;
125 char *p = NULL;
126 int i, nr_cpus = 0;
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200127 int *tmp_cpus = NULL, *tmp;
128 int max_entries = 0;
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200129
130 if (!cpu_list)
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200131 return cpu_map__read_all_cpu_map();
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200132
133 if (!isdigit(*cpu_list))
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200134 goto out;
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200135
136 while (isdigit(*cpu_list)) {
137 p = NULL;
138 start_cpu = strtoul(cpu_list, &p, 0);
139 if (start_cpu >= INT_MAX
140 || (*p != '\0' && *p != ',' && *p != '-'))
141 goto invalid;
142
143 if (*p == '-') {
144 cpu_list = ++p;
145 p = NULL;
146 end_cpu = strtoul(cpu_list, &p, 0);
147
148 if (end_cpu >= INT_MAX || (*p != '\0' && *p != ','))
149 goto invalid;
150
151 if (end_cpu < start_cpu)
152 goto invalid;
153 } else {
154 end_cpu = start_cpu;
155 }
156
157 for (; start_cpu <= end_cpu; start_cpu++) {
158 /* check for duplicates */
159 for (i = 0; i < nr_cpus; i++)
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200160 if (tmp_cpus[i] == (int)start_cpu)
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200161 goto invalid;
162
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200163 if (nr_cpus == max_entries) {
164 max_entries += MAX_NR_CPUS;
165 tmp = realloc(tmp_cpus, max_entries * sizeof(int));
166 if (tmp == NULL)
167 goto invalid;
168 tmp_cpus = tmp;
169 }
170 tmp_cpus[nr_cpus++] = (int)start_cpu;
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200171 }
172 if (*p)
173 ++p;
174
175 cpu_list = p;
176 }
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200177
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200178 if (nr_cpus > 0)
179 cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
180 else
181 cpus = cpu_map__default_new();
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200182invalid:
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200183 free(tmp_cpus);
184out:
185 return cpus;
186}
187
Jiri Olsaf77b57a2015-10-25 15:51:25 +0100188static struct cpu_map *cpu_map__from_entries(struct cpu_map_entries *cpus)
189{
190 struct cpu_map *map;
191
192 map = cpu_map__empty_new(cpus->nr);
193 if (map) {
194 unsigned i;
195
Jiri Olsa15d2b992016-01-06 11:49:55 +0100196 for (i = 0; i < cpus->nr; i++) {
197 /*
198 * Special treatment for -1, which is not real cpu number,
199 * and we need to use (int) -1 to initialize map[i],
200 * otherwise it would become 65535.
201 */
202 if (cpus->cpu[i] == (u16) -1)
203 map->map[i] = -1;
204 else
205 map->map[i] = (int) cpus->cpu[i];
206 }
Jiri Olsaf77b57a2015-10-25 15:51:25 +0100207 }
208
209 return map;
210}
211
212static struct cpu_map *cpu_map__from_mask(struct cpu_map_mask *mask)
213{
214 struct cpu_map *map;
215 int nr, nbits = mask->nr * mask->long_size * BITS_PER_BYTE;
216
217 nr = bitmap_weight(mask->mask, nbits);
218
219 map = cpu_map__empty_new(nr);
220 if (map) {
221 int cpu, i = 0;
222
223 for_each_set_bit(cpu, mask->mask, nbits)
224 map->map[i++] = cpu;
225 }
226 return map;
227
228}
229
230struct cpu_map *cpu_map__new_data(struct cpu_map_data *data)
231{
232 if (data->type == PERF_CPU_MAP__CPUS)
233 return cpu_map__from_entries((struct cpu_map_entries *)data->data);
234 else
235 return cpu_map__from_mask((struct cpu_map_mask *)data->data);
236}
237
Arnaldo Carvalho de Melo9ae7d332012-01-19 14:07:23 -0200238size_t cpu_map__fprintf(struct cpu_map *map, FILE *fp)
239{
Jiri Olsaa24020e2016-06-28 13:29:04 +0200240#define BUFSIZE 1024
241 char buf[BUFSIZE];
Arnaldo Carvalho de Melo9ae7d332012-01-19 14:07:23 -0200242
Jiri Olsaa24020e2016-06-28 13:29:04 +0200243 cpu_map__snprint(map, buf, sizeof(buf));
244 return fprintf(fp, "%s\n", buf);
245#undef BUFSIZE
Arnaldo Carvalho de Melo9ae7d332012-01-19 14:07:23 -0200246}
247
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200248struct cpu_map *cpu_map__dummy_new(void)
249{
250 struct cpu_map *cpus = malloc(sizeof(*cpus) + sizeof(int));
251
252 if (cpus != NULL) {
253 cpus->nr = 1;
254 cpus->map[0] = -1;
Elena Reshetovaec09a422017-02-21 17:34:56 +0200255 refcount_set(&cpus->refcnt, 1);
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200256 }
257
258 return cpus;
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200259}
Arnaldo Carvalho de Melo915fce22011-01-14 16:19:12 -0200260
Jiri Olsa2322f572015-10-25 15:51:17 +0100261struct cpu_map *cpu_map__empty_new(int nr)
262{
263 struct cpu_map *cpus = malloc(sizeof(*cpus) + sizeof(int) * nr);
264
265 if (cpus != NULL) {
266 int i;
267
268 cpus->nr = nr;
269 for (i = 0; i < nr; i++)
270 cpus->map[i] = -1;
271
Elena Reshetovaec09a422017-02-21 17:34:56 +0200272 refcount_set(&cpus->refcnt, 1);
Jiri Olsa2322f572015-10-25 15:51:17 +0100273 }
274
275 return cpus;
276}
277
Jiri Olsaf30a79b2015-06-23 00:36:04 +0200278static void cpu_map__delete(struct cpu_map *map)
Arnaldo Carvalho de Melo915fce22011-01-14 16:19:12 -0200279{
Jiri Olsaf30a79b2015-06-23 00:36:04 +0200280 if (map) {
Elena Reshetovaec09a422017-02-21 17:34:56 +0200281 WARN_ONCE(refcount_read(&map->refcnt) != 0,
Jiri Olsaf30a79b2015-06-23 00:36:04 +0200282 "cpu_map refcnt unbalanced\n");
283 free(map);
284 }
285}
286
287struct cpu_map *cpu_map__get(struct cpu_map *map)
288{
289 if (map)
Elena Reshetovaec09a422017-02-21 17:34:56 +0200290 refcount_inc(&map->refcnt);
Jiri Olsaf30a79b2015-06-23 00:36:04 +0200291 return map;
292}
293
294void cpu_map__put(struct cpu_map *map)
295{
Elena Reshetovaec09a422017-02-21 17:34:56 +0200296 if (map && refcount_dec_and_test(&map->refcnt))
Jiri Olsaf30a79b2015-06-23 00:36:04 +0200297 cpu_map__delete(map);
Arnaldo Carvalho de Melo915fce22011-01-14 16:19:12 -0200298}
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100299
Arnaldo Carvalho de Melo5d8cf722015-09-11 10:49:45 -0300300static int cpu__get_topology_int(int cpu, const char *name, int *value)
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100301{
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100302 char path[PATH_MAX];
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100303
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100304 snprintf(path, PATH_MAX,
Arnaldo Carvalho de Melo5d8cf722015-09-11 10:49:45 -0300305 "devices/system/cpu/cpu%d/topology/%s", cpu, name);
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100306
Arnaldo Carvalho de Melo5d8cf722015-09-11 10:49:45 -0300307 return sysfs__read_int(path, value);
308}
Kan Liang193b6bd2015-09-01 09:58:11 -0400309
Arnaldo Carvalho de Melo5d8cf722015-09-11 10:49:45 -0300310int cpu_map__get_socket_id(int cpu)
311{
312 int value, ret = cpu__get_topology_int(cpu, "physical_package_id", &value);
313 return ret ?: value;
Kan Liang193b6bd2015-09-01 09:58:11 -0400314}
315
Jiri Olsa1fe7a302015-10-16 12:41:15 +0200316int cpu_map__get_socket(struct cpu_map *map, int idx, void *data __maybe_unused)
Kan Liang193b6bd2015-09-01 09:58:11 -0400317{
318 int cpu;
319
320 if (idx > map->nr)
321 return -1;
322
323 cpu = map->map[idx];
324
325 return cpu_map__get_socket_id(cpu);
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100326}
327
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100328static int cmp_ids(const void *a, const void *b)
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100329{
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100330 return *(int *)a - *(int *)b;
331}
332
Jiri Olsaf1cbb8f2015-10-16 12:41:14 +0200333int cpu_map__build_map(struct cpu_map *cpus, struct cpu_map **res,
Jiri Olsa1fe7a302015-10-16 12:41:15 +0200334 int (*f)(struct cpu_map *map, int cpu, void *data),
335 void *data)
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100336{
337 struct cpu_map *c;
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100338 int nr = cpus->nr;
339 int cpu, s1, s2;
340
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100341 /* allocate as much as possible */
342 c = calloc(1, sizeof(*c) + nr * sizeof(int));
343 if (!c)
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100344 return -1;
345
346 for (cpu = 0; cpu < nr; cpu++) {
Jiri Olsa1fe7a302015-10-16 12:41:15 +0200347 s1 = f(cpus, cpu, data);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100348 for (s2 = 0; s2 < c->nr; s2++) {
349 if (s1 == c->map[s2])
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100350 break;
351 }
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100352 if (s2 == c->nr) {
353 c->map[c->nr] = s1;
354 c->nr++;
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100355 }
356 }
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100357 /* ensure we process id in increasing order */
358 qsort(c->map, c->nr, sizeof(int), cmp_ids);
359
Elena Reshetovaec09a422017-02-21 17:34:56 +0200360 refcount_set(&c->refcnt, 1);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100361 *res = c;
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100362 return 0;
363}
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100364
Kan Liang193b6bd2015-09-01 09:58:11 -0400365int cpu_map__get_core_id(int cpu)
Stephane Eranian12c08a92013-02-14 13:57:29 +0100366{
Arnaldo Carvalho de Melo5d8cf722015-09-11 10:49:45 -0300367 int value, ret = cpu__get_topology_int(cpu, "core_id", &value);
368 return ret ?: value;
Kan Liang193b6bd2015-09-01 09:58:11 -0400369}
370
Jiri Olsa1fe7a302015-10-16 12:41:15 +0200371int cpu_map__get_core(struct cpu_map *map, int idx, void *data)
Kan Liang193b6bd2015-09-01 09:58:11 -0400372{
373 int cpu, s;
374
375 if (idx > map->nr)
Stephane Eranian12c08a92013-02-14 13:57:29 +0100376 return -1;
377
Kan Liang193b6bd2015-09-01 09:58:11 -0400378 cpu = map->map[idx];
379
380 cpu = cpu_map__get_core_id(cpu);
381
Jiri Olsa1fe7a302015-10-16 12:41:15 +0200382 s = cpu_map__get_socket(map, idx, data);
Stephane Eranian12c08a92013-02-14 13:57:29 +0100383 if (s == -1)
384 return -1;
385
386 /*
387 * encode socket in upper 16 bits
388 * core_id is relative to socket, and
389 * we need a global id. So we combine
390 * socket+ core id
391 */
392 return (s << 16) | (cpu & 0xffff);
393}
394
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100395int cpu_map__build_socket_map(struct cpu_map *cpus, struct cpu_map **sockp)
396{
Jiri Olsa1fe7a302015-10-16 12:41:15 +0200397 return cpu_map__build_map(cpus, sockp, cpu_map__get_socket, NULL);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100398}
Stephane Eranian12c08a92013-02-14 13:57:29 +0100399
400int cpu_map__build_core_map(struct cpu_map *cpus, struct cpu_map **corep)
401{
Jiri Olsa1fe7a302015-10-16 12:41:15 +0200402 return cpu_map__build_map(cpus, corep, cpu_map__get_core, NULL);
Stephane Eranian12c08a92013-02-14 13:57:29 +0100403}
Don Zickus7780c252014-04-07 14:55:21 -0400404
405/* setup simple routines to easily access node numbers given a cpu number */
406static int get_max_num(char *path, int *max)
407{
408 size_t num;
409 char *buf;
410 int err = 0;
411
412 if (filename__read_str(path, &buf, &num))
413 return -1;
414
415 buf[num] = '\0';
416
417 /* start on the right, to find highest node num */
418 while (--num) {
419 if ((buf[num] == ',') || (buf[num] == '-')) {
420 num++;
421 break;
422 }
423 }
424 if (sscanf(&buf[num], "%d", max) < 1) {
425 err = -1;
426 goto out;
427 }
428
429 /* convert from 0-based to 1-based */
430 (*max)++;
431
432out:
433 free(buf);
434 return err;
435}
436
437/* Determine highest possible cpu in the system for sparse allocation */
438static void set_max_cpu_num(void)
439{
440 const char *mnt;
441 char path[PATH_MAX];
442 int ret = -1;
443
444 /* set up default */
445 max_cpu_num = 4096;
Jan Stancek92a7e122017-02-17 12:10:24 +0100446 max_present_cpu_num = 4096;
Don Zickus7780c252014-04-07 14:55:21 -0400447
448 mnt = sysfs__mountpoint();
449 if (!mnt)
450 goto out;
451
452 /* get the highest possible cpu number for a sparse allocation */
Don Zickusf5b1f4e2014-04-07 14:55:22 -0400453 ret = snprintf(path, PATH_MAX, "%s/devices/system/cpu/possible", mnt);
Don Zickus7780c252014-04-07 14:55:21 -0400454 if (ret == PATH_MAX) {
455 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
456 goto out;
457 }
458
459 ret = get_max_num(path, &max_cpu_num);
Jan Stancek92a7e122017-02-17 12:10:24 +0100460 if (ret)
461 goto out;
462
463 /* get the highest present cpu number for a sparse allocation */
464 ret = snprintf(path, PATH_MAX, "%s/devices/system/cpu/present", mnt);
465 if (ret == PATH_MAX) {
466 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
467 goto out;
468 }
469
470 ret = get_max_num(path, &max_present_cpu_num);
Don Zickus7780c252014-04-07 14:55:21 -0400471
472out:
473 if (ret)
474 pr_err("Failed to read max cpus, using default of %d\n", max_cpu_num);
475}
476
477/* Determine highest possible node in the system for sparse allocation */
478static void set_max_node_num(void)
479{
480 const char *mnt;
481 char path[PATH_MAX];
482 int ret = -1;
483
484 /* set up default */
485 max_node_num = 8;
486
487 mnt = sysfs__mountpoint();
488 if (!mnt)
489 goto out;
490
491 /* get the highest possible cpu number for a sparse allocation */
492 ret = snprintf(path, PATH_MAX, "%s/devices/system/node/possible", mnt);
493 if (ret == PATH_MAX) {
494 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
495 goto out;
496 }
497
498 ret = get_max_num(path, &max_node_num);
499
500out:
501 if (ret)
502 pr_err("Failed to read max nodes, using default of %d\n", max_node_num);
503}
504
Arnaldo Carvalho de Melo5ac76282016-01-26 15:51:46 -0300505int cpu__max_node(void)
506{
507 if (unlikely(!max_node_num))
508 set_max_node_num();
509
510 return max_node_num;
511}
512
513int cpu__max_cpu(void)
514{
515 if (unlikely(!max_cpu_num))
516 set_max_cpu_num();
517
518 return max_cpu_num;
519}
520
Jan Stancek92a7e122017-02-17 12:10:24 +0100521int cpu__max_present_cpu(void)
522{
523 if (unlikely(!max_present_cpu_num))
524 set_max_cpu_num();
525
526 return max_present_cpu_num;
527}
528
529
Arnaldo Carvalho de Melo5ac76282016-01-26 15:51:46 -0300530int cpu__get_node(int cpu)
531{
532 if (unlikely(cpunode_map == NULL)) {
533 pr_debug("cpu_map not initialized\n");
534 return -1;
535 }
536
537 return cpunode_map[cpu];
538}
539
Don Zickus7780c252014-04-07 14:55:21 -0400540static int init_cpunode_map(void)
541{
542 int i;
543
544 set_max_cpu_num();
545 set_max_node_num();
546
547 cpunode_map = calloc(max_cpu_num, sizeof(int));
548 if (!cpunode_map) {
549 pr_err("%s: calloc failed\n", __func__);
550 return -1;
551 }
552
553 for (i = 0; i < max_cpu_num; i++)
554 cpunode_map[i] = -1;
555
556 return 0;
557}
558
559int cpu__setup_cpunode_map(void)
560{
561 struct dirent *dent1, *dent2;
562 DIR *dir1, *dir2;
563 unsigned int cpu, mem;
564 char buf[PATH_MAX];
565 char path[PATH_MAX];
566 const char *mnt;
567 int n;
568
569 /* initialize globals */
570 if (init_cpunode_map())
571 return -1;
572
573 mnt = sysfs__mountpoint();
574 if (!mnt)
575 return 0;
576
577 n = snprintf(path, PATH_MAX, "%s/devices/system/node", mnt);
578 if (n == PATH_MAX) {
579 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
580 return -1;
581 }
582
583 dir1 = opendir(path);
584 if (!dir1)
585 return 0;
586
587 /* walk tree and setup map */
588 while ((dent1 = readdir(dir1)) != NULL) {
589 if (dent1->d_type != DT_DIR || sscanf(dent1->d_name, "node%u", &mem) < 1)
590 continue;
591
592 n = snprintf(buf, PATH_MAX, "%s/%s", path, dent1->d_name);
593 if (n == PATH_MAX) {
594 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
595 continue;
596 }
597
598 dir2 = opendir(buf);
599 if (!dir2)
600 continue;
601 while ((dent2 = readdir(dir2)) != NULL) {
602 if (dent2->d_type != DT_LNK || sscanf(dent2->d_name, "cpu%u", &cpu) < 1)
603 continue;
604 cpunode_map[cpu] = mem;
605 }
606 closedir(dir2);
607 }
608 closedir(dir1);
609 return 0;
610}
Jiri Olsae632aa62016-04-12 15:29:25 +0200611
612bool cpu_map__has(struct cpu_map *cpus, int cpu)
613{
Mark Rutland9a6c5822016-07-15 11:08:11 +0100614 return cpu_map__idx(cpus, cpu) != -1;
615}
616
617int cpu_map__idx(struct cpu_map *cpus, int cpu)
618{
Jiri Olsae632aa62016-04-12 15:29:25 +0200619 int i;
620
621 for (i = 0; i < cpus->nr; ++i) {
622 if (cpus->map[i] == cpu)
Mark Rutland9a6c5822016-07-15 11:08:11 +0100623 return i;
Jiri Olsae632aa62016-04-12 15:29:25 +0200624 }
625
Mark Rutland9a6c5822016-07-15 11:08:11 +0100626 return -1;
627}
628
629int cpu_map__cpu(struct cpu_map *cpus, int idx)
630{
631 return cpus->map[idx];
Jiri Olsae632aa62016-04-12 15:29:25 +0200632}
Jiri Olsaa24020e2016-06-28 13:29:04 +0200633
634size_t cpu_map__snprint(struct cpu_map *map, char *buf, size_t size)
635{
636 int i, cpu, start = -1;
637 bool first = true;
638 size_t ret = 0;
639
640#define COMMA first ? "" : ","
641
642 for (i = 0; i < map->nr + 1; i++) {
643 bool last = i == map->nr;
644
645 cpu = last ? INT_MAX : map->map[i];
646
647 if (start == -1) {
648 start = i;
649 if (last) {
650 ret += snprintf(buf + ret, size - ret,
651 "%s%d", COMMA,
652 map->map[i]);
653 }
654 } else if (((i - start) != (cpu - map->map[start])) || last) {
655 int end = i - 1;
656
657 if (start == end) {
658 ret += snprintf(buf + ret, size - ret,
659 "%s%d", COMMA,
660 map->map[start]);
661 } else {
662 ret += snprintf(buf + ret, size - ret,
663 "%s%d-%d", COMMA,
664 map->map[start], map->map[end]);
665 }
666 first = false;
667 start = i;
668 }
669 }
670
671#undef COMMA
672
673 pr_debug("cpumask list: %s\n", buf);
674 return ret;
675}