xref: /openbmc/linux/tools/perf/util/cpumap.c (revision e5f586c763a079349398e2b0c7c271386193ac34)
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 <linux/bitmap.h>
9 #include "asm/bug.h"
10 
11 static int max_cpu_num;
12 static int max_present_cpu_num;
13 static int max_node_num;
14 static int *cpunode_map;
15 
16 static struct cpu_map *cpu_map__default_new(void)
17 {
18 	struct cpu_map *cpus;
19 	int nr_cpus;
20 
21 	nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
22 	if (nr_cpus < 0)
23 		return NULL;
24 
25 	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;
30 
31 		cpus->nr = nr_cpus;
32 		atomic_set(&cpus->refcnt, 1);
33 	}
34 
35 	return cpus;
36 }
37 
38 static struct cpu_map *cpu_map__trim_new(int nr_cpus, int *tmp_cpus)
39 {
40 	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);
46 		atomic_set(&cpus->refcnt, 1);
47 	}
48 
49 	return cpus;
50 }
51 
52 struct cpu_map *cpu_map__read(FILE *file)
53 {
54 	struct cpu_map *cpus = NULL;
55 	int nr_cpus = 0;
56 	int *tmp_cpus = NULL, *tmp;
57 	int max_entries = 0;
58 	int n, cpu, prev;
59 	char sep;
60 
61 	sep = 0;
62 	prev = -1;
63 	for (;;) {
64 		n = fscanf(file, "%u%c", &cpu, &sep);
65 		if (n <= 0)
66 			break;
67 		if (prev >= 0) {
68 			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 
78 			while (++prev < cpu)
79 				tmp_cpus[nr_cpus++] = prev;
80 		}
81 		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;
90 		if (n == 2 && sep == '-')
91 			prev = cpu;
92 		else
93 			prev = -1;
94 		if (n == 1 || sep == '\n')
95 			break;
96 	}
97 
98 	if (nr_cpus > 0)
99 		cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
100 	else
101 		cpus = cpu_map__default_new();
102 out_free_tmp:
103 	free(tmp_cpus);
104 	return cpus;
105 }
106 
107 static 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);
117 	fclose(onlnf);
118 	return cpus;
119 }
120 
121 struct cpu_map *cpu_map__new(const char *cpu_list)
122 {
123 	struct cpu_map *cpus = NULL;
124 	unsigned long start_cpu, end_cpu = 0;
125 	char *p = NULL;
126 	int i, nr_cpus = 0;
127 	int *tmp_cpus = NULL, *tmp;
128 	int max_entries = 0;
129 
130 	if (!cpu_list)
131 		return cpu_map__read_all_cpu_map();
132 
133 	if (!isdigit(*cpu_list))
134 		goto out;
135 
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++)
160 				if (tmp_cpus[i] == (int)start_cpu)
161 					goto invalid;
162 
163 			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;
171 		}
172 		if (*p)
173 			++p;
174 
175 		cpu_list = p;
176 	}
177 
178 	if (nr_cpus > 0)
179 		cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
180 	else
181 		cpus = cpu_map__default_new();
182 invalid:
183 	free(tmp_cpus);
184 out:
185 	return cpus;
186 }
187 
188 static 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 
196 		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 		}
207 	}
208 
209 	return map;
210 }
211 
212 static 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 
230 struct 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 
238 size_t cpu_map__fprintf(struct cpu_map *map, FILE *fp)
239 {
240 #define BUFSIZE 1024
241 	char buf[BUFSIZE];
242 
243 	cpu_map__snprint(map, buf, sizeof(buf));
244 	return fprintf(fp, "%s\n", buf);
245 #undef BUFSIZE
246 }
247 
248 struct 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;
255 		atomic_set(&cpus->refcnt, 1);
256 	}
257 
258 	return cpus;
259 }
260 
261 struct 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 
272 		atomic_set(&cpus->refcnt, 1);
273 	}
274 
275 	return cpus;
276 }
277 
278 static void cpu_map__delete(struct cpu_map *map)
279 {
280 	if (map) {
281 		WARN_ONCE(atomic_read(&map->refcnt) != 0,
282 			  "cpu_map refcnt unbalanced\n");
283 		free(map);
284 	}
285 }
286 
287 struct cpu_map *cpu_map__get(struct cpu_map *map)
288 {
289 	if (map)
290 		atomic_inc(&map->refcnt);
291 	return map;
292 }
293 
294 void cpu_map__put(struct cpu_map *map)
295 {
296 	if (map && atomic_dec_and_test(&map->refcnt))
297 		cpu_map__delete(map);
298 }
299 
300 static int cpu__get_topology_int(int cpu, const char *name, int *value)
301 {
302 	char path[PATH_MAX];
303 
304 	snprintf(path, PATH_MAX,
305 		"devices/system/cpu/cpu%d/topology/%s", cpu, name);
306 
307 	return sysfs__read_int(path, value);
308 }
309 
310 int 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;
314 }
315 
316 int cpu_map__get_socket(struct cpu_map *map, int idx, void *data __maybe_unused)
317 {
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);
326 }
327 
328 static int cmp_ids(const void *a, const void *b)
329 {
330 	return *(int *)a - *(int *)b;
331 }
332 
333 int cpu_map__build_map(struct cpu_map *cpus, struct cpu_map **res,
334 		       int (*f)(struct cpu_map *map, int cpu, void *data),
335 		       void *data)
336 {
337 	struct cpu_map *c;
338 	int nr = cpus->nr;
339 	int cpu, s1, s2;
340 
341 	/* allocate as much as possible */
342 	c = calloc(1, sizeof(*c) + nr * sizeof(int));
343 	if (!c)
344 		return -1;
345 
346 	for (cpu = 0; cpu < nr; cpu++) {
347 		s1 = f(cpus, cpu, data);
348 		for (s2 = 0; s2 < c->nr; s2++) {
349 			if (s1 == c->map[s2])
350 				break;
351 		}
352 		if (s2 == c->nr) {
353 			c->map[c->nr] = s1;
354 			c->nr++;
355 		}
356 	}
357 	/* ensure we process id in increasing order */
358 	qsort(c->map, c->nr, sizeof(int), cmp_ids);
359 
360 	atomic_set(&c->refcnt, 1);
361 	*res = c;
362 	return 0;
363 }
364 
365 int cpu_map__get_core_id(int cpu)
366 {
367 	int value, ret = cpu__get_topology_int(cpu, "core_id", &value);
368 	return ret ?: value;
369 }
370 
371 int cpu_map__get_core(struct cpu_map *map, int idx, void *data)
372 {
373 	int cpu, s;
374 
375 	if (idx > map->nr)
376 		return -1;
377 
378 	cpu = map->map[idx];
379 
380 	cpu = cpu_map__get_core_id(cpu);
381 
382 	s = cpu_map__get_socket(map, idx, data);
383 	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 
395 int cpu_map__build_socket_map(struct cpu_map *cpus, struct cpu_map **sockp)
396 {
397 	return cpu_map__build_map(cpus, sockp, cpu_map__get_socket, NULL);
398 }
399 
400 int cpu_map__build_core_map(struct cpu_map *cpus, struct cpu_map **corep)
401 {
402 	return cpu_map__build_map(cpus, corep, cpu_map__get_core, NULL);
403 }
404 
405 /* setup simple routines to easily access node numbers given a cpu number */
406 static 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 
432 out:
433 	free(buf);
434 	return err;
435 }
436 
437 /* Determine highest possible cpu in the system for sparse allocation */
438 static 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;
446 	max_present_cpu_num = 4096;
447 
448 	mnt = sysfs__mountpoint();
449 	if (!mnt)
450 		goto out;
451 
452 	/* get the highest possible cpu number for a sparse allocation */
453 	ret = snprintf(path, PATH_MAX, "%s/devices/system/cpu/possible", mnt);
454 	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);
460 	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);
471 
472 out:
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 */
478 static 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 
500 out:
501 	if (ret)
502 		pr_err("Failed to read max nodes, using default of %d\n", max_node_num);
503 }
504 
505 int cpu__max_node(void)
506 {
507 	if (unlikely(!max_node_num))
508 		set_max_node_num();
509 
510 	return max_node_num;
511 }
512 
513 int cpu__max_cpu(void)
514 {
515 	if (unlikely(!max_cpu_num))
516 		set_max_cpu_num();
517 
518 	return max_cpu_num;
519 }
520 
521 int 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 
530 int 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 
540 static 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 
559 int 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 }
611 
612 bool cpu_map__has(struct cpu_map *cpus, int cpu)
613 {
614 	return cpu_map__idx(cpus, cpu) != -1;
615 }
616 
617 int cpu_map__idx(struct cpu_map *cpus, int cpu)
618 {
619 	int i;
620 
621 	for (i = 0; i < cpus->nr; ++i) {
622 		if (cpus->map[i] == cpu)
623 			return i;
624 	}
625 
626 	return -1;
627 }
628 
629 int cpu_map__cpu(struct cpu_map *cpus, int idx)
630 {
631 	return cpus->map[idx];
632 }
633 
634 size_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 }
676