blob: 37b3bb79ee08db66122c30f6ae5af2473f005d7a [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>
Arnaldo Carvalho de Melo76b31a22017-04-18 12:26:44 -03006#include <dirent.h>
Paul Mackerrasa12b51c2010-03-10 20:36:09 +11007#include <stdio.h>
Stephane Eranian86ee6e12013-02-14 13:57:27 +01008#include <stdlib.h>
Jiri Olsaf77b57a2015-10-25 15:51:25 +01009#include <linux/bitmap.h>
Jiri Olsaf30a79b2015-06-23 00:36:04 +020010#include "asm/bug.h"
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110011
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030012#include "sane_ctype.h"
13
Arnaldo Carvalho de Melo5ac76282016-01-26 15:51:46 -030014static int max_cpu_num;
Jan Stancek92a7e122017-02-17 12:10:24 +010015static int max_present_cpu_num;
Arnaldo Carvalho de Melo5ac76282016-01-26 15:51:46 -030016static int max_node_num;
17static int *cpunode_map;
18
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020019static struct cpu_map *cpu_map__default_new(void)
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110020{
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020021 struct cpu_map *cpus;
22 int nr_cpus;
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110023
24 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020025 if (nr_cpus < 0)
26 return NULL;
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110027
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020028 cpus = malloc(sizeof(*cpus) + nr_cpus * sizeof(int));
29 if (cpus != NULL) {
30 int i;
31 for (i = 0; i < nr_cpus; ++i)
32 cpus->map[i] = i;
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110033
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020034 cpus->nr = nr_cpus;
Elena Reshetovaec09a422017-02-21 17:34:56 +020035 refcount_set(&cpus->refcnt, 1);
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020036 }
37
38 return cpus;
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110039}
40
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020041static struct cpu_map *cpu_map__trim_new(int nr_cpus, int *tmp_cpus)
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110042{
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020043 size_t payload_size = nr_cpus * sizeof(int);
44 struct cpu_map *cpus = malloc(sizeof(*cpus) + payload_size);
45
46 if (cpus != NULL) {
47 cpus->nr = nr_cpus;
48 memcpy(cpus->map, tmp_cpus, payload_size);
Elena Reshetovaec09a422017-02-21 17:34:56 +020049 refcount_set(&cpus->refcnt, 1);
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020050 }
51
52 return cpus;
53}
54
Yan, Zheng7ae92e72012-09-10 15:53:50 +080055struct cpu_map *cpu_map__read(FILE *file)
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020056{
57 struct cpu_map *cpus = NULL;
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110058 int nr_cpus = 0;
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020059 int *tmp_cpus = NULL, *tmp;
60 int max_entries = 0;
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110061 int n, cpu, prev;
62 char sep;
63
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110064 sep = 0;
65 prev = -1;
66 for (;;) {
Yan, Zheng7ae92e72012-09-10 15:53:50 +080067 n = fscanf(file, "%u%c", &cpu, &sep);
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110068 if (n <= 0)
69 break;
70 if (prev >= 0) {
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020071 int new_max = nr_cpus + cpu - prev - 1;
72
73 if (new_max >= max_entries) {
74 max_entries = new_max + MAX_NR_CPUS / 2;
75 tmp = realloc(tmp_cpus, max_entries * sizeof(int));
76 if (tmp == NULL)
77 goto out_free_tmp;
78 tmp_cpus = tmp;
79 }
80
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110081 while (++prev < cpu)
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020082 tmp_cpus[nr_cpus++] = prev;
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110083 }
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -020084 if (nr_cpus == max_entries) {
85 max_entries += MAX_NR_CPUS;
86 tmp = realloc(tmp_cpus, max_entries * sizeof(int));
87 if (tmp == NULL)
88 goto out_free_tmp;
89 tmp_cpus = tmp;
90 }
91
92 tmp_cpus[nr_cpus++] = cpu;
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110093 if (n == 2 && sep == '-')
94 prev = cpu;
95 else
96 prev = -1;
97 if (n == 1 || sep == '\n')
98 break;
99 }
Paul Mackerrasa12b51c2010-03-10 20:36:09 +1100100
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200101 if (nr_cpus > 0)
102 cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
103 else
104 cpus = cpu_map__default_new();
105out_free_tmp:
106 free(tmp_cpus);
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800107 return cpus;
108}
109
110static struct cpu_map *cpu_map__read_all_cpu_map(void)
111{
112 struct cpu_map *cpus = NULL;
113 FILE *onlnf;
114
115 onlnf = fopen("/sys/devices/system/cpu/online", "r");
116 if (!onlnf)
117 return cpu_map__default_new();
118
119 cpus = cpu_map__read(onlnf);
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200120 fclose(onlnf);
121 return cpus;
Paul Mackerrasa12b51c2010-03-10 20:36:09 +1100122}
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200123
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200124struct cpu_map *cpu_map__new(const char *cpu_list)
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200125{
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200126 struct cpu_map *cpus = NULL;
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200127 unsigned long start_cpu, end_cpu = 0;
128 char *p = NULL;
129 int i, nr_cpus = 0;
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200130 int *tmp_cpus = NULL, *tmp;
131 int max_entries = 0;
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200132
133 if (!cpu_list)
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200134 return cpu_map__read_all_cpu_map();
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200135
136 if (!isdigit(*cpu_list))
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200137 goto out;
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200138
139 while (isdigit(*cpu_list)) {
140 p = NULL;
141 start_cpu = strtoul(cpu_list, &p, 0);
142 if (start_cpu >= INT_MAX
143 || (*p != '\0' && *p != ',' && *p != '-'))
144 goto invalid;
145
146 if (*p == '-') {
147 cpu_list = ++p;
148 p = NULL;
149 end_cpu = strtoul(cpu_list, &p, 0);
150
151 if (end_cpu >= INT_MAX || (*p != '\0' && *p != ','))
152 goto invalid;
153
154 if (end_cpu < start_cpu)
155 goto invalid;
156 } else {
157 end_cpu = start_cpu;
158 }
159
160 for (; start_cpu <= end_cpu; start_cpu++) {
161 /* check for duplicates */
162 for (i = 0; i < nr_cpus; i++)
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200163 if (tmp_cpus[i] == (int)start_cpu)
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200164 goto invalid;
165
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200166 if (nr_cpus == max_entries) {
167 max_entries += MAX_NR_CPUS;
168 tmp = realloc(tmp_cpus, max_entries * sizeof(int));
169 if (tmp == NULL)
170 goto invalid;
171 tmp_cpus = tmp;
172 }
173 tmp_cpus[nr_cpus++] = (int)start_cpu;
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200174 }
175 if (*p)
176 ++p;
177
178 cpu_list = p;
179 }
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200180
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200181 if (nr_cpus > 0)
182 cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
183 else
184 cpus = cpu_map__default_new();
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200185invalid:
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200186 free(tmp_cpus);
187out:
188 return cpus;
189}
190
Jiri Olsaf77b57a2015-10-25 15:51:25 +0100191static struct cpu_map *cpu_map__from_entries(struct cpu_map_entries *cpus)
192{
193 struct cpu_map *map;
194
195 map = cpu_map__empty_new(cpus->nr);
196 if (map) {
197 unsigned i;
198
Jiri Olsa15d2b992016-01-06 11:49:55 +0100199 for (i = 0; i < cpus->nr; i++) {
200 /*
201 * Special treatment for -1, which is not real cpu number,
202 * and we need to use (int) -1 to initialize map[i],
203 * otherwise it would become 65535.
204 */
205 if (cpus->cpu[i] == (u16) -1)
206 map->map[i] = -1;
207 else
208 map->map[i] = (int) cpus->cpu[i];
209 }
Jiri Olsaf77b57a2015-10-25 15:51:25 +0100210 }
211
212 return map;
213}
214
215static struct cpu_map *cpu_map__from_mask(struct cpu_map_mask *mask)
216{
217 struct cpu_map *map;
218 int nr, nbits = mask->nr * mask->long_size * BITS_PER_BYTE;
219
220 nr = bitmap_weight(mask->mask, nbits);
221
222 map = cpu_map__empty_new(nr);
223 if (map) {
224 int cpu, i = 0;
225
226 for_each_set_bit(cpu, mask->mask, nbits)
227 map->map[i++] = cpu;
228 }
229 return map;
230
231}
232
233struct cpu_map *cpu_map__new_data(struct cpu_map_data *data)
234{
235 if (data->type == PERF_CPU_MAP__CPUS)
236 return cpu_map__from_entries((struct cpu_map_entries *)data->data);
237 else
238 return cpu_map__from_mask((struct cpu_map_mask *)data->data);
239}
240
Arnaldo Carvalho de Melo9ae7d332012-01-19 14:07:23 -0200241size_t cpu_map__fprintf(struct cpu_map *map, FILE *fp)
242{
Jiri Olsaa24020e2016-06-28 13:29:04 +0200243#define BUFSIZE 1024
244 char buf[BUFSIZE];
Arnaldo Carvalho de Melo9ae7d332012-01-19 14:07:23 -0200245
Jiri Olsaa24020e2016-06-28 13:29:04 +0200246 cpu_map__snprint(map, buf, sizeof(buf));
247 return fprintf(fp, "%s\n", buf);
248#undef BUFSIZE
Arnaldo Carvalho de Melo9ae7d332012-01-19 14:07:23 -0200249}
250
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200251struct cpu_map *cpu_map__dummy_new(void)
252{
253 struct cpu_map *cpus = malloc(sizeof(*cpus) + sizeof(int));
254
255 if (cpus != NULL) {
256 cpus->nr = 1;
257 cpus->map[0] = -1;
Elena Reshetovaec09a422017-02-21 17:34:56 +0200258 refcount_set(&cpus->refcnt, 1);
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -0200259 }
260
261 return cpus;
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200262}
Arnaldo Carvalho de Melo915fce22011-01-14 16:19:12 -0200263
Jiri Olsa2322f572015-10-25 15:51:17 +0100264struct cpu_map *cpu_map__empty_new(int nr)
265{
266 struct cpu_map *cpus = malloc(sizeof(*cpus) + sizeof(int) * nr);
267
268 if (cpus != NULL) {
269 int i;
270
271 cpus->nr = nr;
272 for (i = 0; i < nr; i++)
273 cpus->map[i] = -1;
274
Elena Reshetovaec09a422017-02-21 17:34:56 +0200275 refcount_set(&cpus->refcnt, 1);
Jiri Olsa2322f572015-10-25 15:51:17 +0100276 }
277
278 return cpus;
279}
280
Jiri Olsaf30a79b2015-06-23 00:36:04 +0200281static void cpu_map__delete(struct cpu_map *map)
Arnaldo Carvalho de Melo915fce22011-01-14 16:19:12 -0200282{
Jiri Olsaf30a79b2015-06-23 00:36:04 +0200283 if (map) {
Elena Reshetovaec09a422017-02-21 17:34:56 +0200284 WARN_ONCE(refcount_read(&map->refcnt) != 0,
Jiri Olsaf30a79b2015-06-23 00:36:04 +0200285 "cpu_map refcnt unbalanced\n");
286 free(map);
287 }
288}
289
290struct cpu_map *cpu_map__get(struct cpu_map *map)
291{
292 if (map)
Elena Reshetovaec09a422017-02-21 17:34:56 +0200293 refcount_inc(&map->refcnt);
Jiri Olsaf30a79b2015-06-23 00:36:04 +0200294 return map;
295}
296
297void cpu_map__put(struct cpu_map *map)
298{
Elena Reshetovaec09a422017-02-21 17:34:56 +0200299 if (map && refcount_dec_and_test(&map->refcnt))
Jiri Olsaf30a79b2015-06-23 00:36:04 +0200300 cpu_map__delete(map);
Arnaldo Carvalho de Melo915fce22011-01-14 16:19:12 -0200301}
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100302
Arnaldo Carvalho de Melo5d8cf722015-09-11 10:49:45 -0300303static int cpu__get_topology_int(int cpu, const char *name, int *value)
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100304{
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100305 char path[PATH_MAX];
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100306
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100307 snprintf(path, PATH_MAX,
Arnaldo Carvalho de Melo5d8cf722015-09-11 10:49:45 -0300308 "devices/system/cpu/cpu%d/topology/%s", cpu, name);
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100309
Arnaldo Carvalho de Melo5d8cf722015-09-11 10:49:45 -0300310 return sysfs__read_int(path, value);
311}
Kan Liang193b6bd2015-09-01 09:58:11 -0400312
Arnaldo Carvalho de Melo5d8cf722015-09-11 10:49:45 -0300313int cpu_map__get_socket_id(int cpu)
314{
315 int value, ret = cpu__get_topology_int(cpu, "physical_package_id", &value);
316 return ret ?: value;
Kan Liang193b6bd2015-09-01 09:58:11 -0400317}
318
Jiri Olsa1fe7a302015-10-16 12:41:15 +0200319int cpu_map__get_socket(struct cpu_map *map, int idx, void *data __maybe_unused)
Kan Liang193b6bd2015-09-01 09:58:11 -0400320{
321 int cpu;
322
323 if (idx > map->nr)
324 return -1;
325
326 cpu = map->map[idx];
327
328 return cpu_map__get_socket_id(cpu);
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100329}
330
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100331static int cmp_ids(const void *a, const void *b)
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100332{
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100333 return *(int *)a - *(int *)b;
334}
335
Jiri Olsaf1cbb8f2015-10-16 12:41:14 +0200336int cpu_map__build_map(struct cpu_map *cpus, struct cpu_map **res,
Jiri Olsa1fe7a302015-10-16 12:41:15 +0200337 int (*f)(struct cpu_map *map, int cpu, void *data),
338 void *data)
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100339{
340 struct cpu_map *c;
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100341 int nr = cpus->nr;
342 int cpu, s1, s2;
343
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100344 /* allocate as much as possible */
345 c = calloc(1, sizeof(*c) + nr * sizeof(int));
346 if (!c)
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100347 return -1;
348
349 for (cpu = 0; cpu < nr; cpu++) {
Jiri Olsa1fe7a302015-10-16 12:41:15 +0200350 s1 = f(cpus, cpu, data);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100351 for (s2 = 0; s2 < c->nr; s2++) {
352 if (s1 == c->map[s2])
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100353 break;
354 }
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100355 if (s2 == c->nr) {
356 c->map[c->nr] = s1;
357 c->nr++;
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100358 }
359 }
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100360 /* ensure we process id in increasing order */
361 qsort(c->map, c->nr, sizeof(int), cmp_ids);
362
Elena Reshetovaec09a422017-02-21 17:34:56 +0200363 refcount_set(&c->refcnt, 1);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100364 *res = c;
Stephane Eranian5ac59a82013-02-06 15:46:01 +0100365 return 0;
366}
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100367
Kan Liang193b6bd2015-09-01 09:58:11 -0400368int cpu_map__get_core_id(int cpu)
Stephane Eranian12c08a92013-02-14 13:57:29 +0100369{
Arnaldo Carvalho de Melo5d8cf722015-09-11 10:49:45 -0300370 int value, ret = cpu__get_topology_int(cpu, "core_id", &value);
371 return ret ?: value;
Kan Liang193b6bd2015-09-01 09:58:11 -0400372}
373
Jiri Olsa1fe7a302015-10-16 12:41:15 +0200374int cpu_map__get_core(struct cpu_map *map, int idx, void *data)
Kan Liang193b6bd2015-09-01 09:58:11 -0400375{
376 int cpu, s;
377
378 if (idx > map->nr)
Stephane Eranian12c08a92013-02-14 13:57:29 +0100379 return -1;
380
Kan Liang193b6bd2015-09-01 09:58:11 -0400381 cpu = map->map[idx];
382
383 cpu = cpu_map__get_core_id(cpu);
384
Jiri Olsa1fe7a302015-10-16 12:41:15 +0200385 s = cpu_map__get_socket(map, idx, data);
Stephane Eranian12c08a92013-02-14 13:57:29 +0100386 if (s == -1)
387 return -1;
388
389 /*
390 * encode socket in upper 16 bits
391 * core_id is relative to socket, and
392 * we need a global id. So we combine
393 * socket+ core id
394 */
395 return (s << 16) | (cpu & 0xffff);
396}
397
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100398int cpu_map__build_socket_map(struct cpu_map *cpus, struct cpu_map **sockp)
399{
Jiri Olsa1fe7a302015-10-16 12:41:15 +0200400 return cpu_map__build_map(cpus, sockp, cpu_map__get_socket, NULL);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100401}
Stephane Eranian12c08a92013-02-14 13:57:29 +0100402
403int cpu_map__build_core_map(struct cpu_map *cpus, struct cpu_map **corep)
404{
Jiri Olsa1fe7a302015-10-16 12:41:15 +0200405 return cpu_map__build_map(cpus, corep, cpu_map__get_core, NULL);
Stephane Eranian12c08a92013-02-14 13:57:29 +0100406}
Don Zickus7780c252014-04-07 14:55:21 -0400407
408/* setup simple routines to easily access node numbers given a cpu number */
409static int get_max_num(char *path, int *max)
410{
411 size_t num;
412 char *buf;
413 int err = 0;
414
415 if (filename__read_str(path, &buf, &num))
416 return -1;
417
418 buf[num] = '\0';
419
420 /* start on the right, to find highest node num */
421 while (--num) {
422 if ((buf[num] == ',') || (buf[num] == '-')) {
423 num++;
424 break;
425 }
426 }
427 if (sscanf(&buf[num], "%d", max) < 1) {
428 err = -1;
429 goto out;
430 }
431
432 /* convert from 0-based to 1-based */
433 (*max)++;
434
435out:
436 free(buf);
437 return err;
438}
439
440/* Determine highest possible cpu in the system for sparse allocation */
441static void set_max_cpu_num(void)
442{
443 const char *mnt;
444 char path[PATH_MAX];
445 int ret = -1;
446
447 /* set up default */
448 max_cpu_num = 4096;
Jan Stancek92a7e122017-02-17 12:10:24 +0100449 max_present_cpu_num = 4096;
Don Zickus7780c252014-04-07 14:55:21 -0400450
451 mnt = sysfs__mountpoint();
452 if (!mnt)
453 goto out;
454
455 /* get the highest possible cpu number for a sparse allocation */
Don Zickusf5b1f4e2014-04-07 14:55:22 -0400456 ret = snprintf(path, PATH_MAX, "%s/devices/system/cpu/possible", mnt);
Don Zickus7780c252014-04-07 14:55:21 -0400457 if (ret == PATH_MAX) {
458 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
459 goto out;
460 }
461
462 ret = get_max_num(path, &max_cpu_num);
Jan Stancek92a7e122017-02-17 12:10:24 +0100463 if (ret)
464 goto out;
465
466 /* get the highest present cpu number for a sparse allocation */
467 ret = snprintf(path, PATH_MAX, "%s/devices/system/cpu/present", mnt);
468 if (ret == PATH_MAX) {
469 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
470 goto out;
471 }
472
473 ret = get_max_num(path, &max_present_cpu_num);
Don Zickus7780c252014-04-07 14:55:21 -0400474
475out:
476 if (ret)
477 pr_err("Failed to read max cpus, using default of %d\n", max_cpu_num);
478}
479
480/* Determine highest possible node in the system for sparse allocation */
481static void set_max_node_num(void)
482{
483 const char *mnt;
484 char path[PATH_MAX];
485 int ret = -1;
486
487 /* set up default */
488 max_node_num = 8;
489
490 mnt = sysfs__mountpoint();
491 if (!mnt)
492 goto out;
493
494 /* get the highest possible cpu number for a sparse allocation */
495 ret = snprintf(path, PATH_MAX, "%s/devices/system/node/possible", mnt);
496 if (ret == PATH_MAX) {
497 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
498 goto out;
499 }
500
501 ret = get_max_num(path, &max_node_num);
502
503out:
504 if (ret)
505 pr_err("Failed to read max nodes, using default of %d\n", max_node_num);
506}
507
Arnaldo Carvalho de Melo5ac76282016-01-26 15:51:46 -0300508int cpu__max_node(void)
509{
510 if (unlikely(!max_node_num))
511 set_max_node_num();
512
513 return max_node_num;
514}
515
516int cpu__max_cpu(void)
517{
518 if (unlikely(!max_cpu_num))
519 set_max_cpu_num();
520
521 return max_cpu_num;
522}
523
Jan Stancek92a7e122017-02-17 12:10:24 +0100524int cpu__max_present_cpu(void)
525{
526 if (unlikely(!max_present_cpu_num))
527 set_max_cpu_num();
528
529 return max_present_cpu_num;
530}
531
532
Arnaldo Carvalho de Melo5ac76282016-01-26 15:51:46 -0300533int cpu__get_node(int cpu)
534{
535 if (unlikely(cpunode_map == NULL)) {
536 pr_debug("cpu_map not initialized\n");
537 return -1;
538 }
539
540 return cpunode_map[cpu];
541}
542
Don Zickus7780c252014-04-07 14:55:21 -0400543static int init_cpunode_map(void)
544{
545 int i;
546
547 set_max_cpu_num();
548 set_max_node_num();
549
550 cpunode_map = calloc(max_cpu_num, sizeof(int));
551 if (!cpunode_map) {
552 pr_err("%s: calloc failed\n", __func__);
553 return -1;
554 }
555
556 for (i = 0; i < max_cpu_num; i++)
557 cpunode_map[i] = -1;
558
559 return 0;
560}
561
562int cpu__setup_cpunode_map(void)
563{
564 struct dirent *dent1, *dent2;
565 DIR *dir1, *dir2;
566 unsigned int cpu, mem;
567 char buf[PATH_MAX];
568 char path[PATH_MAX];
569 const char *mnt;
570 int n;
571
572 /* initialize globals */
573 if (init_cpunode_map())
574 return -1;
575
576 mnt = sysfs__mountpoint();
577 if (!mnt)
578 return 0;
579
580 n = snprintf(path, PATH_MAX, "%s/devices/system/node", mnt);
581 if (n == PATH_MAX) {
582 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
583 return -1;
584 }
585
586 dir1 = opendir(path);
587 if (!dir1)
588 return 0;
589
590 /* walk tree and setup map */
591 while ((dent1 = readdir(dir1)) != NULL) {
592 if (dent1->d_type != DT_DIR || sscanf(dent1->d_name, "node%u", &mem) < 1)
593 continue;
594
595 n = snprintf(buf, PATH_MAX, "%s/%s", path, dent1->d_name);
596 if (n == PATH_MAX) {
597 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
598 continue;
599 }
600
601 dir2 = opendir(buf);
602 if (!dir2)
603 continue;
604 while ((dent2 = readdir(dir2)) != NULL) {
605 if (dent2->d_type != DT_LNK || sscanf(dent2->d_name, "cpu%u", &cpu) < 1)
606 continue;
607 cpunode_map[cpu] = mem;
608 }
609 closedir(dir2);
610 }
611 closedir(dir1);
612 return 0;
613}
Jiri Olsae632aa62016-04-12 15:29:25 +0200614
615bool cpu_map__has(struct cpu_map *cpus, int cpu)
616{
Mark Rutland9a6c5822016-07-15 11:08:11 +0100617 return cpu_map__idx(cpus, cpu) != -1;
618}
619
620int cpu_map__idx(struct cpu_map *cpus, int cpu)
621{
Jiri Olsae632aa62016-04-12 15:29:25 +0200622 int i;
623
624 for (i = 0; i < cpus->nr; ++i) {
625 if (cpus->map[i] == cpu)
Mark Rutland9a6c5822016-07-15 11:08:11 +0100626 return i;
Jiri Olsae632aa62016-04-12 15:29:25 +0200627 }
628
Mark Rutland9a6c5822016-07-15 11:08:11 +0100629 return -1;
630}
631
632int cpu_map__cpu(struct cpu_map *cpus, int idx)
633{
634 return cpus->map[idx];
Jiri Olsae632aa62016-04-12 15:29:25 +0200635}
Jiri Olsaa24020e2016-06-28 13:29:04 +0200636
637size_t cpu_map__snprint(struct cpu_map *map, char *buf, size_t size)
638{
639 int i, cpu, start = -1;
640 bool first = true;
641 size_t ret = 0;
642
643#define COMMA first ? "" : ","
644
645 for (i = 0; i < map->nr + 1; i++) {
646 bool last = i == map->nr;
647
648 cpu = last ? INT_MAX : map->map[i];
649
650 if (start == -1) {
651 start = i;
652 if (last) {
653 ret += snprintf(buf + ret, size - ret,
654 "%s%d", COMMA,
655 map->map[i]);
656 }
657 } else if (((i - start) != (cpu - map->map[start])) || last) {
658 int end = i - 1;
659
660 if (start == end) {
661 ret += snprintf(buf + ret, size - ret,
662 "%s%d", COMMA,
663 map->map[start]);
664 } else {
665 ret += snprintf(buf + ret, size - ret,
666 "%s%d-%d", COMMA,
667 map->map[start], map->map[end]);
668 }
669 first = false;
670 start = i;
671 }
672 }
673
674#undef COMMA
675
676 pr_debug("cpumask list: %s\n", buf);
677 return ret;
678}
Namhyung Kim4400ac82017-02-24 10:12:49 +0900679
680static char hex_char(unsigned char val)
681{
682 if (val < 10)
683 return val + '0';
684 if (val < 16)
685 return val - 10 + 'a';
686 return '?';
687}
688
689size_t cpu_map__snprint_mask(struct cpu_map *map, char *buf, size_t size)
690{
691 int i, cpu;
692 char *ptr = buf;
693 unsigned char *bitmap;
694 int last_cpu = cpu_map__cpu(map, map->nr - 1);
695
696 bitmap = zalloc((last_cpu + 7) / 8);
697 if (bitmap == NULL) {
698 buf[0] = '\0';
699 return 0;
700 }
701
702 for (i = 0; i < map->nr; i++) {
703 cpu = cpu_map__cpu(map, i);
704 bitmap[cpu / 8] |= 1 << (cpu % 8);
705 }
706
707 for (cpu = last_cpu / 4 * 4; cpu >= 0; cpu -= 4) {
708 unsigned char bits = bitmap[cpu / 8];
709
710 if (cpu % 8)
711 bits >>= 4;
712 else
713 bits &= 0xf;
714
715 *ptr++ = hex_char(bits);
716 if ((cpu % 32) == 0 && cpu > 0)
717 *ptr++ = ',';
718 }
719 *ptr = '\0';
720 free(bitmap);
721
722 buf[size - 1] = '\0';
723 return ptr - buf;
724}