xref: /linux/tools/perf/util/cpumap.c (revision b7019ac550eb3916f34d79db583e9b7ea2524afa)
1 // SPDX-License-Identifier: GPL-2.0
2 #include "util.h"
3 #include <api/fs/fs.h>
4 #include "../perf.h"
5 #include "cpumap.h"
6 #include <assert.h>
7 #include <dirent.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <linux/bitmap.h>
11 #include "asm/bug.h"
12 
13 #include "sane_ctype.h"
14 
15 static int max_cpu_num;
16 static int max_present_cpu_num;
17 static int max_node_num;
18 static int *cpunode_map;
19 
20 static struct cpu_map *cpu_map__default_new(void)
21 {
22 	struct cpu_map *cpus;
23 	int nr_cpus;
24 
25 	nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
26 	if (nr_cpus < 0)
27 		return NULL;
28 
29 	cpus = malloc(sizeof(*cpus) + nr_cpus * sizeof(int));
30 	if (cpus != NULL) {
31 		int i;
32 		for (i = 0; i < nr_cpus; ++i)
33 			cpus->map[i] = i;
34 
35 		cpus->nr = nr_cpus;
36 		refcount_set(&cpus->refcnt, 1);
37 	}
38 
39 	return cpus;
40 }
41 
42 static struct cpu_map *cpu_map__trim_new(int nr_cpus, int *tmp_cpus)
43 {
44 	size_t payload_size = nr_cpus * sizeof(int);
45 	struct cpu_map *cpus = malloc(sizeof(*cpus) + payload_size);
46 
47 	if (cpus != NULL) {
48 		cpus->nr = nr_cpus;
49 		memcpy(cpus->map, tmp_cpus, payload_size);
50 		refcount_set(&cpus->refcnt, 1);
51 	}
52 
53 	return cpus;
54 }
55 
56 struct cpu_map *cpu_map__read(FILE *file)
57 {
58 	struct cpu_map *cpus = NULL;
59 	int nr_cpus = 0;
60 	int *tmp_cpus = NULL, *tmp;
61 	int max_entries = 0;
62 	int n, cpu, prev;
63 	char sep;
64 
65 	sep = 0;
66 	prev = -1;
67 	for (;;) {
68 		n = fscanf(file, "%u%c", &cpu, &sep);
69 		if (n <= 0)
70 			break;
71 		if (prev >= 0) {
72 			int new_max = nr_cpus + cpu - prev - 1;
73 
74 			if (new_max >= max_entries) {
75 				max_entries = new_max + MAX_NR_CPUS / 2;
76 				tmp = realloc(tmp_cpus, max_entries * sizeof(int));
77 				if (tmp == NULL)
78 					goto out_free_tmp;
79 				tmp_cpus = tmp;
80 			}
81 
82 			while (++prev < cpu)
83 				tmp_cpus[nr_cpus++] = prev;
84 		}
85 		if (nr_cpus == max_entries) {
86 			max_entries += MAX_NR_CPUS;
87 			tmp = realloc(tmp_cpus, max_entries * sizeof(int));
88 			if (tmp == NULL)
89 				goto out_free_tmp;
90 			tmp_cpus = tmp;
91 		}
92 
93 		tmp_cpus[nr_cpus++] = cpu;
94 		if (n == 2 && sep == '-')
95 			prev = cpu;
96 		else
97 			prev = -1;
98 		if (n == 1 || sep == '\n')
99 			break;
100 	}
101 
102 	if (nr_cpus > 0)
103 		cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
104 	else
105 		cpus = cpu_map__default_new();
106 out_free_tmp:
107 	free(tmp_cpus);
108 	return cpus;
109 }
110 
111 static struct cpu_map *cpu_map__read_all_cpu_map(void)
112 {
113 	struct cpu_map *cpus = NULL;
114 	FILE *onlnf;
115 
116 	onlnf = fopen("/sys/devices/system/cpu/online", "r");
117 	if (!onlnf)
118 		return cpu_map__default_new();
119 
120 	cpus = cpu_map__read(onlnf);
121 	fclose(onlnf);
122 	return cpus;
123 }
124 
125 struct cpu_map *cpu_map__new(const char *cpu_list)
126 {
127 	struct cpu_map *cpus = NULL;
128 	unsigned long start_cpu, end_cpu = 0;
129 	char *p = NULL;
130 	int i, nr_cpus = 0;
131 	int *tmp_cpus = NULL, *tmp;
132 	int max_entries = 0;
133 
134 	if (!cpu_list)
135 		return cpu_map__read_all_cpu_map();
136 
137 	/*
138 	 * must handle the case of empty cpumap to cover
139 	 * TOPOLOGY header for NUMA nodes with no CPU
140 	 * ( e.g., because of CPU hotplug)
141 	 */
142 	if (!isdigit(*cpu_list) && *cpu_list != '\0')
143 		goto out;
144 
145 	while (isdigit(*cpu_list)) {
146 		p = NULL;
147 		start_cpu = strtoul(cpu_list, &p, 0);
148 		if (start_cpu >= INT_MAX
149 		    || (*p != '\0' && *p != ',' && *p != '-'))
150 			goto invalid;
151 
152 		if (*p == '-') {
153 			cpu_list = ++p;
154 			p = NULL;
155 			end_cpu = strtoul(cpu_list, &p, 0);
156 
157 			if (end_cpu >= INT_MAX || (*p != '\0' && *p != ','))
158 				goto invalid;
159 
160 			if (end_cpu < start_cpu)
161 				goto invalid;
162 		} else {
163 			end_cpu = start_cpu;
164 		}
165 
166 		for (; start_cpu <= end_cpu; start_cpu++) {
167 			/* check for duplicates */
168 			for (i = 0; i < nr_cpus; i++)
169 				if (tmp_cpus[i] == (int)start_cpu)
170 					goto invalid;
171 
172 			if (nr_cpus == max_entries) {
173 				max_entries += MAX_NR_CPUS;
174 				tmp = realloc(tmp_cpus, max_entries * sizeof(int));
175 				if (tmp == NULL)
176 					goto invalid;
177 				tmp_cpus = tmp;
178 			}
179 			tmp_cpus[nr_cpus++] = (int)start_cpu;
180 		}
181 		if (*p)
182 			++p;
183 
184 		cpu_list = p;
185 	}
186 
187 	if (nr_cpus > 0)
188 		cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
189 	else if (*cpu_list != '\0')
190 		cpus = cpu_map__default_new();
191 	else
192 		cpus = cpu_map__dummy_new();
193 invalid:
194 	free(tmp_cpus);
195 out:
196 	return cpus;
197 }
198 
199 static struct cpu_map *cpu_map__from_entries(struct cpu_map_entries *cpus)
200 {
201 	struct cpu_map *map;
202 
203 	map = cpu_map__empty_new(cpus->nr);
204 	if (map) {
205 		unsigned i;
206 
207 		for (i = 0; i < cpus->nr; i++) {
208 			/*
209 			 * Special treatment for -1, which is not real cpu number,
210 			 * and we need to use (int) -1 to initialize map[i],
211 			 * otherwise it would become 65535.
212 			 */
213 			if (cpus->cpu[i] == (u16) -1)
214 				map->map[i] = -1;
215 			else
216 				map->map[i] = (int) cpus->cpu[i];
217 		}
218 	}
219 
220 	return map;
221 }
222 
223 static struct cpu_map *cpu_map__from_mask(struct cpu_map_mask *mask)
224 {
225 	struct cpu_map *map;
226 	int nr, nbits = mask->nr * mask->long_size * BITS_PER_BYTE;
227 
228 	nr = bitmap_weight(mask->mask, nbits);
229 
230 	map = cpu_map__empty_new(nr);
231 	if (map) {
232 		int cpu, i = 0;
233 
234 		for_each_set_bit(cpu, mask->mask, nbits)
235 			map->map[i++] = cpu;
236 	}
237 	return map;
238 
239 }
240 
241 struct cpu_map *cpu_map__new_data(struct cpu_map_data *data)
242 {
243 	if (data->type == PERF_CPU_MAP__CPUS)
244 		return cpu_map__from_entries((struct cpu_map_entries *)data->data);
245 	else
246 		return cpu_map__from_mask((struct cpu_map_mask *)data->data);
247 }
248 
249 size_t cpu_map__fprintf(struct cpu_map *map, FILE *fp)
250 {
251 #define BUFSIZE 1024
252 	char buf[BUFSIZE];
253 
254 	cpu_map__snprint(map, buf, sizeof(buf));
255 	return fprintf(fp, "%s\n", buf);
256 #undef BUFSIZE
257 }
258 
259 struct cpu_map *cpu_map__dummy_new(void)
260 {
261 	struct cpu_map *cpus = malloc(sizeof(*cpus) + sizeof(int));
262 
263 	if (cpus != NULL) {
264 		cpus->nr = 1;
265 		cpus->map[0] = -1;
266 		refcount_set(&cpus->refcnt, 1);
267 	}
268 
269 	return cpus;
270 }
271 
272 struct cpu_map *cpu_map__empty_new(int nr)
273 {
274 	struct cpu_map *cpus = malloc(sizeof(*cpus) + sizeof(int) * nr);
275 
276 	if (cpus != NULL) {
277 		int i;
278 
279 		cpus->nr = nr;
280 		for (i = 0; i < nr; i++)
281 			cpus->map[i] = -1;
282 
283 		refcount_set(&cpus->refcnt, 1);
284 	}
285 
286 	return cpus;
287 }
288 
289 static void cpu_map__delete(struct cpu_map *map)
290 {
291 	if (map) {
292 		WARN_ONCE(refcount_read(&map->refcnt) != 0,
293 			  "cpu_map refcnt unbalanced\n");
294 		free(map);
295 	}
296 }
297 
298 struct cpu_map *cpu_map__get(struct cpu_map *map)
299 {
300 	if (map)
301 		refcount_inc(&map->refcnt);
302 	return map;
303 }
304 
305 void cpu_map__put(struct cpu_map *map)
306 {
307 	if (map && refcount_dec_and_test(&map->refcnt))
308 		cpu_map__delete(map);
309 }
310 
311 static int cpu__get_topology_int(int cpu, const char *name, int *value)
312 {
313 	char path[PATH_MAX];
314 
315 	snprintf(path, PATH_MAX,
316 		"devices/system/cpu/cpu%d/topology/%s", cpu, name);
317 
318 	return sysfs__read_int(path, value);
319 }
320 
321 int cpu_map__get_socket_id(int cpu)
322 {
323 	int value, ret = cpu__get_topology_int(cpu, "physical_package_id", &value);
324 	return ret ?: value;
325 }
326 
327 int cpu_map__get_socket(struct cpu_map *map, int idx, void *data __maybe_unused)
328 {
329 	int cpu;
330 
331 	if (idx > map->nr)
332 		return -1;
333 
334 	cpu = map->map[idx];
335 
336 	return cpu_map__get_socket_id(cpu);
337 }
338 
339 static int cmp_ids(const void *a, const void *b)
340 {
341 	return *(int *)a - *(int *)b;
342 }
343 
344 int cpu_map__build_map(struct cpu_map *cpus, struct cpu_map **res,
345 		       int (*f)(struct cpu_map *map, int cpu, void *data),
346 		       void *data)
347 {
348 	struct cpu_map *c;
349 	int nr = cpus->nr;
350 	int cpu, s1, s2;
351 
352 	/* allocate as much as possible */
353 	c = calloc(1, sizeof(*c) + nr * sizeof(int));
354 	if (!c)
355 		return -1;
356 
357 	for (cpu = 0; cpu < nr; cpu++) {
358 		s1 = f(cpus, cpu, data);
359 		for (s2 = 0; s2 < c->nr; s2++) {
360 			if (s1 == c->map[s2])
361 				break;
362 		}
363 		if (s2 == c->nr) {
364 			c->map[c->nr] = s1;
365 			c->nr++;
366 		}
367 	}
368 	/* ensure we process id in increasing order */
369 	qsort(c->map, c->nr, sizeof(int), cmp_ids);
370 
371 	refcount_set(&c->refcnt, 1);
372 	*res = c;
373 	return 0;
374 }
375 
376 int cpu_map__get_core_id(int cpu)
377 {
378 	int value, ret = cpu__get_topology_int(cpu, "core_id", &value);
379 	return ret ?: value;
380 }
381 
382 int cpu_map__get_core(struct cpu_map *map, int idx, void *data)
383 {
384 	int cpu, s;
385 
386 	if (idx > map->nr)
387 		return -1;
388 
389 	cpu = map->map[idx];
390 
391 	cpu = cpu_map__get_core_id(cpu);
392 
393 	s = cpu_map__get_socket(map, idx, data);
394 	if (s == -1)
395 		return -1;
396 
397 	/*
398 	 * encode socket in upper 16 bits
399 	 * core_id is relative to socket, and
400 	 * we need a global id. So we combine
401 	 * socket+ core id
402 	 */
403 	return (s << 16) | (cpu & 0xffff);
404 }
405 
406 int cpu_map__build_socket_map(struct cpu_map *cpus, struct cpu_map **sockp)
407 {
408 	return cpu_map__build_map(cpus, sockp, cpu_map__get_socket, NULL);
409 }
410 
411 int cpu_map__build_core_map(struct cpu_map *cpus, struct cpu_map **corep)
412 {
413 	return cpu_map__build_map(cpus, corep, cpu_map__get_core, NULL);
414 }
415 
416 /* setup simple routines to easily access node numbers given a cpu number */
417 static int get_max_num(char *path, int *max)
418 {
419 	size_t num;
420 	char *buf;
421 	int err = 0;
422 
423 	if (filename__read_str(path, &buf, &num))
424 		return -1;
425 
426 	buf[num] = '\0';
427 
428 	/* start on the right, to find highest node num */
429 	while (--num) {
430 		if ((buf[num] == ',') || (buf[num] == '-')) {
431 			num++;
432 			break;
433 		}
434 	}
435 	if (sscanf(&buf[num], "%d", max) < 1) {
436 		err = -1;
437 		goto out;
438 	}
439 
440 	/* convert from 0-based to 1-based */
441 	(*max)++;
442 
443 out:
444 	free(buf);
445 	return err;
446 }
447 
448 /* Determine highest possible cpu in the system for sparse allocation */
449 static void set_max_cpu_num(void)
450 {
451 	const char *mnt;
452 	char path[PATH_MAX];
453 	int ret = -1;
454 
455 	/* set up default */
456 	max_cpu_num = 4096;
457 	max_present_cpu_num = 4096;
458 
459 	mnt = sysfs__mountpoint();
460 	if (!mnt)
461 		goto out;
462 
463 	/* get the highest possible cpu number for a sparse allocation */
464 	ret = snprintf(path, PATH_MAX, "%s/devices/system/cpu/possible", 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_cpu_num);
471 	if (ret)
472 		goto out;
473 
474 	/* get the highest present cpu number for a sparse allocation */
475 	ret = snprintf(path, PATH_MAX, "%s/devices/system/cpu/present", mnt);
476 	if (ret == PATH_MAX) {
477 		pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
478 		goto out;
479 	}
480 
481 	ret = get_max_num(path, &max_present_cpu_num);
482 
483 out:
484 	if (ret)
485 		pr_err("Failed to read max cpus, using default of %d\n", max_cpu_num);
486 }
487 
488 /* Determine highest possible node in the system for sparse allocation */
489 static void set_max_node_num(void)
490 {
491 	const char *mnt;
492 	char path[PATH_MAX];
493 	int ret = -1;
494 
495 	/* set up default */
496 	max_node_num = 8;
497 
498 	mnt = sysfs__mountpoint();
499 	if (!mnt)
500 		goto out;
501 
502 	/* get the highest possible cpu number for a sparse allocation */
503 	ret = snprintf(path, PATH_MAX, "%s/devices/system/node/possible", mnt);
504 	if (ret == PATH_MAX) {
505 		pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
506 		goto out;
507 	}
508 
509 	ret = get_max_num(path, &max_node_num);
510 
511 out:
512 	if (ret)
513 		pr_err("Failed to read max nodes, using default of %d\n", max_node_num);
514 }
515 
516 int cpu__max_node(void)
517 {
518 	if (unlikely(!max_node_num))
519 		set_max_node_num();
520 
521 	return max_node_num;
522 }
523 
524 int cpu__max_cpu(void)
525 {
526 	if (unlikely(!max_cpu_num))
527 		set_max_cpu_num();
528 
529 	return max_cpu_num;
530 }
531 
532 int cpu__max_present_cpu(void)
533 {
534 	if (unlikely(!max_present_cpu_num))
535 		set_max_cpu_num();
536 
537 	return max_present_cpu_num;
538 }
539 
540 
541 int cpu__get_node(int cpu)
542 {
543 	if (unlikely(cpunode_map == NULL)) {
544 		pr_debug("cpu_map not initialized\n");
545 		return -1;
546 	}
547 
548 	return cpunode_map[cpu];
549 }
550 
551 static int init_cpunode_map(void)
552 {
553 	int i;
554 
555 	set_max_cpu_num();
556 	set_max_node_num();
557 
558 	cpunode_map = calloc(max_cpu_num, sizeof(int));
559 	if (!cpunode_map) {
560 		pr_err("%s: calloc failed\n", __func__);
561 		return -1;
562 	}
563 
564 	for (i = 0; i < max_cpu_num; i++)
565 		cpunode_map[i] = -1;
566 
567 	return 0;
568 }
569 
570 int cpu__setup_cpunode_map(void)
571 {
572 	struct dirent *dent1, *dent2;
573 	DIR *dir1, *dir2;
574 	unsigned int cpu, mem;
575 	char buf[PATH_MAX];
576 	char path[PATH_MAX];
577 	const char *mnt;
578 	int n;
579 
580 	/* initialize globals */
581 	if (init_cpunode_map())
582 		return -1;
583 
584 	mnt = sysfs__mountpoint();
585 	if (!mnt)
586 		return 0;
587 
588 	n = snprintf(path, PATH_MAX, "%s/devices/system/node", mnt);
589 	if (n == PATH_MAX) {
590 		pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
591 		return -1;
592 	}
593 
594 	dir1 = opendir(path);
595 	if (!dir1)
596 		return 0;
597 
598 	/* walk tree and setup map */
599 	while ((dent1 = readdir(dir1)) != NULL) {
600 		if (dent1->d_type != DT_DIR || sscanf(dent1->d_name, "node%u", &mem) < 1)
601 			continue;
602 
603 		n = snprintf(buf, PATH_MAX, "%s/%s", path, dent1->d_name);
604 		if (n == PATH_MAX) {
605 			pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
606 			continue;
607 		}
608 
609 		dir2 = opendir(buf);
610 		if (!dir2)
611 			continue;
612 		while ((dent2 = readdir(dir2)) != NULL) {
613 			if (dent2->d_type != DT_LNK || sscanf(dent2->d_name, "cpu%u", &cpu) < 1)
614 				continue;
615 			cpunode_map[cpu] = mem;
616 		}
617 		closedir(dir2);
618 	}
619 	closedir(dir1);
620 	return 0;
621 }
622 
623 bool cpu_map__has(struct cpu_map *cpus, int cpu)
624 {
625 	return cpu_map__idx(cpus, cpu) != -1;
626 }
627 
628 int cpu_map__idx(struct cpu_map *cpus, int cpu)
629 {
630 	int i;
631 
632 	for (i = 0; i < cpus->nr; ++i) {
633 		if (cpus->map[i] == cpu)
634 			return i;
635 	}
636 
637 	return -1;
638 }
639 
640 int cpu_map__cpu(struct cpu_map *cpus, int idx)
641 {
642 	return cpus->map[idx];
643 }
644 
645 size_t cpu_map__snprint(struct cpu_map *map, char *buf, size_t size)
646 {
647 	int i, cpu, start = -1;
648 	bool first = true;
649 	size_t ret = 0;
650 
651 #define COMMA first ? "" : ","
652 
653 	for (i = 0; i < map->nr + 1; i++) {
654 		bool last = i == map->nr;
655 
656 		cpu = last ? INT_MAX : map->map[i];
657 
658 		if (start == -1) {
659 			start = i;
660 			if (last) {
661 				ret += snprintf(buf + ret, size - ret,
662 						"%s%d", COMMA,
663 						map->map[i]);
664 			}
665 		} else if (((i - start) != (cpu - map->map[start])) || last) {
666 			int end = i - 1;
667 
668 			if (start == end) {
669 				ret += snprintf(buf + ret, size - ret,
670 						"%s%d", COMMA,
671 						map->map[start]);
672 			} else {
673 				ret += snprintf(buf + ret, size - ret,
674 						"%s%d-%d", COMMA,
675 						map->map[start], map->map[end]);
676 			}
677 			first = false;
678 			start = i;
679 		}
680 	}
681 
682 #undef COMMA
683 
684 	pr_debug2("cpumask list: %s\n", buf);
685 	return ret;
686 }
687 
688 static char hex_char(unsigned char val)
689 {
690 	if (val < 10)
691 		return val + '0';
692 	if (val < 16)
693 		return val - 10 + 'a';
694 	return '?';
695 }
696 
697 size_t cpu_map__snprint_mask(struct cpu_map *map, char *buf, size_t size)
698 {
699 	int i, cpu;
700 	char *ptr = buf;
701 	unsigned char *bitmap;
702 	int last_cpu = cpu_map__cpu(map, map->nr - 1);
703 
704 	bitmap = zalloc((last_cpu + 7) / 8);
705 	if (bitmap == NULL) {
706 		buf[0] = '\0';
707 		return 0;
708 	}
709 
710 	for (i = 0; i < map->nr; i++) {
711 		cpu = cpu_map__cpu(map, i);
712 		bitmap[cpu / 8] |= 1 << (cpu % 8);
713 	}
714 
715 	for (cpu = last_cpu / 4 * 4; cpu >= 0; cpu -= 4) {
716 		unsigned char bits = bitmap[cpu / 8];
717 
718 		if (cpu % 8)
719 			bits >>= 4;
720 		else
721 			bits &= 0xf;
722 
723 		*ptr++ = hex_char(bits);
724 		if ((cpu % 32) == 0 && cpu > 0)
725 			*ptr++ = ',';
726 	}
727 	*ptr = '\0';
728 	free(bitmap);
729 
730 	buf[size - 1] = '\0';
731 	return ptr - buf;
732 }
733 
734 const struct cpu_map *cpu_map__online(void) /* thread unsafe */
735 {
736 	static const struct cpu_map *online = NULL;
737 
738 	if (!online)
739 		online = cpu_map__new(NULL); /* from /sys/devices/system/cpu/online */
740 
741 	return online;
742 }
743