xref: /openbmc/linux/tools/perf/util/cpumap.c (revision 91802e73)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <api/fs/fs.h>
3 #include "cpumap.h"
4 #include "debug.h"
5 #include "event.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 <linux/ctype.h>
14 #include <linux/zalloc.h>
15 
16 static int max_cpu_num;
17 static int max_present_cpu_num;
18 static int max_node_num;
19 /**
20  * The numa node X as read from /sys/devices/system/node/nodeX indexed by the
21  * CPU number.
22  */
23 static int *cpunode_map;
24 
25 static struct perf_cpu_map *cpu_map__from_entries(struct cpu_map_entries *cpus)
26 {
27 	struct perf_cpu_map *map;
28 
29 	map = perf_cpu_map__empty_new(cpus->nr);
30 	if (map) {
31 		unsigned i;
32 
33 		for (i = 0; i < cpus->nr; i++) {
34 			/*
35 			 * Special treatment for -1, which is not real cpu number,
36 			 * and we need to use (int) -1 to initialize map[i],
37 			 * otherwise it would become 65535.
38 			 */
39 			if (cpus->cpu[i] == (u16) -1)
40 				map->map[i] = -1;
41 			else
42 				map->map[i] = (int) cpus->cpu[i];
43 		}
44 	}
45 
46 	return map;
47 }
48 
49 static struct perf_cpu_map *cpu_map__from_mask(struct perf_record_record_cpu_map *mask)
50 {
51 	struct perf_cpu_map *map;
52 	int nr, nbits = mask->nr * mask->long_size * BITS_PER_BYTE;
53 
54 	nr = bitmap_weight(mask->mask, nbits);
55 
56 	map = perf_cpu_map__empty_new(nr);
57 	if (map) {
58 		int cpu, i = 0;
59 
60 		for_each_set_bit(cpu, mask->mask, nbits)
61 			map->map[i++] = cpu;
62 	}
63 	return map;
64 
65 }
66 
67 struct perf_cpu_map *cpu_map__new_data(struct perf_record_cpu_map_data *data)
68 {
69 	if (data->type == PERF_CPU_MAP__CPUS)
70 		return cpu_map__from_entries((struct cpu_map_entries *)data->data);
71 	else
72 		return cpu_map__from_mask((struct perf_record_record_cpu_map *)data->data);
73 }
74 
75 size_t cpu_map__fprintf(struct perf_cpu_map *map, FILE *fp)
76 {
77 #define BUFSIZE 1024
78 	char buf[BUFSIZE];
79 
80 	cpu_map__snprint(map, buf, sizeof(buf));
81 	return fprintf(fp, "%s\n", buf);
82 #undef BUFSIZE
83 }
84 
85 struct perf_cpu_map *perf_cpu_map__empty_new(int nr)
86 {
87 	struct perf_cpu_map *cpus = malloc(sizeof(*cpus) + sizeof(int) * nr);
88 
89 	if (cpus != NULL) {
90 		int i;
91 
92 		cpus->nr = nr;
93 		for (i = 0; i < nr; i++)
94 			cpus->map[i] = -1;
95 
96 		refcount_set(&cpus->refcnt, 1);
97 	}
98 
99 	return cpus;
100 }
101 
102 struct cpu_aggr_map *cpu_aggr_map__empty_new(int nr)
103 {
104 	struct cpu_aggr_map *cpus = malloc(sizeof(*cpus) + sizeof(struct aggr_cpu_id) * nr);
105 
106 	if (cpus != NULL) {
107 		int i;
108 
109 		cpus->nr = nr;
110 		for (i = 0; i < nr; i++)
111 			cpus->map[i] = aggr_cpu_id__empty();
112 
113 		refcount_set(&cpus->refcnt, 1);
114 	}
115 
116 	return cpus;
117 }
118 
119 static int cpu__get_topology_int(int cpu, const char *name, int *value)
120 {
121 	char path[PATH_MAX];
122 
123 	snprintf(path, PATH_MAX,
124 		"devices/system/cpu/cpu%d/topology/%s", cpu, name);
125 
126 	return sysfs__read_int(path, value);
127 }
128 
129 int cpu__get_socket_id(int cpu)
130 {
131 	int value, ret = cpu__get_topology_int(cpu, "physical_package_id", &value);
132 	return ret ?: value;
133 }
134 
135 struct aggr_cpu_id aggr_cpu_id__socket(int cpu, void *data __maybe_unused)
136 {
137 	struct aggr_cpu_id id = aggr_cpu_id__empty();
138 
139 	id.socket = cpu__get_socket_id(cpu);
140 	return id;
141 }
142 
143 static int aggr_cpu_id__cmp(const void *a_pointer, const void *b_pointer)
144 {
145 	struct aggr_cpu_id *a = (struct aggr_cpu_id *)a_pointer;
146 	struct aggr_cpu_id *b = (struct aggr_cpu_id *)b_pointer;
147 
148 	if (a->node != b->node)
149 		return a->node - b->node;
150 	else if (a->socket != b->socket)
151 		return a->socket - b->socket;
152 	else if (a->die != b->die)
153 		return a->die - b->die;
154 	else if (a->core != b->core)
155 		return a->core - b->core;
156 	else
157 		return a->thread - b->thread;
158 }
159 
160 struct cpu_aggr_map *cpu_aggr_map__new(const struct perf_cpu_map *cpus,
161 				       aggr_cpu_id_get_t get_id,
162 				       void *data)
163 {
164 	int cpu, idx;
165 	struct cpu_aggr_map *c = cpu_aggr_map__empty_new(cpus->nr);
166 
167 	if (!c)
168 		return NULL;
169 
170 	/* Reset size as it may only be partially filled */
171 	c->nr = 0;
172 
173 	perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
174 		bool duplicate = false;
175 		struct aggr_cpu_id cpu_id = get_id(cpu, data);
176 
177 		for (int j = 0; j < c->nr; j++) {
178 			if (aggr_cpu_id__equal(&cpu_id, &c->map[j])) {
179 				duplicate = true;
180 				break;
181 			}
182 		}
183 		if (!duplicate) {
184 			c->map[c->nr] = cpu_id;
185 			c->nr++;
186 		}
187 	}
188 	/* Trim. */
189 	if (c->nr != cpus->nr) {
190 		struct cpu_aggr_map *trimmed_c =
191 			realloc(c,
192 				sizeof(struct cpu_aggr_map) + sizeof(struct aggr_cpu_id) * c->nr);
193 
194 		if (trimmed_c)
195 			c = trimmed_c;
196 	}
197 	/* ensure we process id in increasing order */
198 	qsort(c->map, c->nr, sizeof(struct aggr_cpu_id), aggr_cpu_id__cmp);
199 
200 	return c;
201 
202 }
203 
204 int cpu__get_die_id(int cpu)
205 {
206 	int value, ret = cpu__get_topology_int(cpu, "die_id", &value);
207 
208 	return ret ?: value;
209 }
210 
211 struct aggr_cpu_id aggr_cpu_id__die(int cpu, void *data)
212 {
213 	struct aggr_cpu_id id;
214 	int die;
215 
216 	die = cpu__get_die_id(cpu);
217 	/* There is no die_id on legacy system. */
218 	if (die == -1)
219 		die = 0;
220 
221 	/*
222 	 * die_id is relative to socket, so start
223 	 * with the socket ID and then add die to
224 	 * make a unique ID.
225 	 */
226 	id = aggr_cpu_id__socket(cpu, data);
227 	if (aggr_cpu_id__is_empty(&id))
228 		return id;
229 
230 	id.die = die;
231 	return id;
232 }
233 
234 int cpu__get_core_id(int cpu)
235 {
236 	int value, ret = cpu__get_topology_int(cpu, "core_id", &value);
237 	return ret ?: value;
238 }
239 
240 struct aggr_cpu_id aggr_cpu_id__core(int cpu, void *data)
241 {
242 	struct aggr_cpu_id id;
243 	int core = cpu__get_core_id(cpu);
244 
245 	/* aggr_cpu_id__die returns a struct with socket and die set. */
246 	id = aggr_cpu_id__die(cpu, data);
247 	if (aggr_cpu_id__is_empty(&id))
248 		return id;
249 
250 	/*
251 	 * core_id is relative to socket and die, we need a global id.
252 	 * So we combine the result from cpu_map__get_die with the core id
253 	 */
254 	id.core = core;
255 	return id;
256 
257 }
258 
259 struct aggr_cpu_id aggr_cpu_id__cpu(int cpu, void *data)
260 {
261 	struct aggr_cpu_id id;
262 
263 	/* aggr_cpu_id__core returns a struct with socket, die and core set. */
264 	id = aggr_cpu_id__core(cpu, data);
265 	if (aggr_cpu_id__is_empty(&id))
266 		return id;
267 
268 	id.cpu = cpu;
269 	return id;
270 
271 }
272 
273 struct aggr_cpu_id aggr_cpu_id__node(int cpu, void *data __maybe_unused)
274 {
275 	struct aggr_cpu_id id = aggr_cpu_id__empty();
276 
277 	id.node = cpu__get_node(cpu);
278 	return id;
279 }
280 
281 /* setup simple routines to easily access node numbers given a cpu number */
282 static int get_max_num(char *path, int *max)
283 {
284 	size_t num;
285 	char *buf;
286 	int err = 0;
287 
288 	if (filename__read_str(path, &buf, &num))
289 		return -1;
290 
291 	buf[num] = '\0';
292 
293 	/* start on the right, to find highest node num */
294 	while (--num) {
295 		if ((buf[num] == ',') || (buf[num] == '-')) {
296 			num++;
297 			break;
298 		}
299 	}
300 	if (sscanf(&buf[num], "%d", max) < 1) {
301 		err = -1;
302 		goto out;
303 	}
304 
305 	/* convert from 0-based to 1-based */
306 	(*max)++;
307 
308 out:
309 	free(buf);
310 	return err;
311 }
312 
313 /* Determine highest possible cpu in the system for sparse allocation */
314 static void set_max_cpu_num(void)
315 {
316 	const char *mnt;
317 	char path[PATH_MAX];
318 	int ret = -1;
319 
320 	/* set up default */
321 	max_cpu_num = 4096;
322 	max_present_cpu_num = 4096;
323 
324 	mnt = sysfs__mountpoint();
325 	if (!mnt)
326 		goto out;
327 
328 	/* get the highest possible cpu number for a sparse allocation */
329 	ret = snprintf(path, PATH_MAX, "%s/devices/system/cpu/possible", mnt);
330 	if (ret >= PATH_MAX) {
331 		pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
332 		goto out;
333 	}
334 
335 	ret = get_max_num(path, &max_cpu_num);
336 	if (ret)
337 		goto out;
338 
339 	/* get the highest present cpu number for a sparse allocation */
340 	ret = snprintf(path, PATH_MAX, "%s/devices/system/cpu/present", mnt);
341 	if (ret >= PATH_MAX) {
342 		pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
343 		goto out;
344 	}
345 
346 	ret = get_max_num(path, &max_present_cpu_num);
347 
348 out:
349 	if (ret)
350 		pr_err("Failed to read max cpus, using default of %d\n", max_cpu_num);
351 }
352 
353 /* Determine highest possible node in the system for sparse allocation */
354 static void set_max_node_num(void)
355 {
356 	const char *mnt;
357 	char path[PATH_MAX];
358 	int ret = -1;
359 
360 	/* set up default */
361 	max_node_num = 8;
362 
363 	mnt = sysfs__mountpoint();
364 	if (!mnt)
365 		goto out;
366 
367 	/* get the highest possible cpu number for a sparse allocation */
368 	ret = snprintf(path, PATH_MAX, "%s/devices/system/node/possible", mnt);
369 	if (ret >= PATH_MAX) {
370 		pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
371 		goto out;
372 	}
373 
374 	ret = get_max_num(path, &max_node_num);
375 
376 out:
377 	if (ret)
378 		pr_err("Failed to read max nodes, using default of %d\n", max_node_num);
379 }
380 
381 int cpu__max_node(void)
382 {
383 	if (unlikely(!max_node_num))
384 		set_max_node_num();
385 
386 	return max_node_num;
387 }
388 
389 int cpu__max_cpu(void)
390 {
391 	if (unlikely(!max_cpu_num))
392 		set_max_cpu_num();
393 
394 	return max_cpu_num;
395 }
396 
397 int cpu__max_present_cpu(void)
398 {
399 	if (unlikely(!max_present_cpu_num))
400 		set_max_cpu_num();
401 
402 	return max_present_cpu_num;
403 }
404 
405 
406 int cpu__get_node(int cpu)
407 {
408 	if (unlikely(cpunode_map == NULL)) {
409 		pr_debug("cpu_map not initialized\n");
410 		return -1;
411 	}
412 
413 	return cpunode_map[cpu];
414 }
415 
416 static int init_cpunode_map(void)
417 {
418 	int i;
419 
420 	set_max_cpu_num();
421 	set_max_node_num();
422 
423 	cpunode_map = calloc(max_cpu_num, sizeof(int));
424 	if (!cpunode_map) {
425 		pr_err("%s: calloc failed\n", __func__);
426 		return -1;
427 	}
428 
429 	for (i = 0; i < max_cpu_num; i++)
430 		cpunode_map[i] = -1;
431 
432 	return 0;
433 }
434 
435 int cpu__setup_cpunode_map(void)
436 {
437 	struct dirent *dent1, *dent2;
438 	DIR *dir1, *dir2;
439 	unsigned int cpu, mem;
440 	char buf[PATH_MAX];
441 	char path[PATH_MAX];
442 	const char *mnt;
443 	int n;
444 
445 	/* initialize globals */
446 	if (init_cpunode_map())
447 		return -1;
448 
449 	mnt = sysfs__mountpoint();
450 	if (!mnt)
451 		return 0;
452 
453 	n = snprintf(path, PATH_MAX, "%s/devices/system/node", mnt);
454 	if (n >= PATH_MAX) {
455 		pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
456 		return -1;
457 	}
458 
459 	dir1 = opendir(path);
460 	if (!dir1)
461 		return 0;
462 
463 	/* walk tree and setup map */
464 	while ((dent1 = readdir(dir1)) != NULL) {
465 		if (dent1->d_type != DT_DIR || sscanf(dent1->d_name, "node%u", &mem) < 1)
466 			continue;
467 
468 		n = snprintf(buf, PATH_MAX, "%s/%s", path, dent1->d_name);
469 		if (n >= PATH_MAX) {
470 			pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
471 			continue;
472 		}
473 
474 		dir2 = opendir(buf);
475 		if (!dir2)
476 			continue;
477 		while ((dent2 = readdir(dir2)) != NULL) {
478 			if (dent2->d_type != DT_LNK || sscanf(dent2->d_name, "cpu%u", &cpu) < 1)
479 				continue;
480 			cpunode_map[cpu] = mem;
481 		}
482 		closedir(dir2);
483 	}
484 	closedir(dir1);
485 	return 0;
486 }
487 
488 size_t cpu_map__snprint(struct perf_cpu_map *map, char *buf, size_t size)
489 {
490 	int i, cpu, start = -1;
491 	bool first = true;
492 	size_t ret = 0;
493 
494 #define COMMA first ? "" : ","
495 
496 	for (i = 0; i < map->nr + 1; i++) {
497 		bool last = i == map->nr;
498 
499 		cpu = last ? INT_MAX : map->map[i];
500 
501 		if (start == -1) {
502 			start = i;
503 			if (last) {
504 				ret += snprintf(buf + ret, size - ret,
505 						"%s%d", COMMA,
506 						map->map[i]);
507 			}
508 		} else if (((i - start) != (cpu - map->map[start])) || last) {
509 			int end = i - 1;
510 
511 			if (start == end) {
512 				ret += snprintf(buf + ret, size - ret,
513 						"%s%d", COMMA,
514 						map->map[start]);
515 			} else {
516 				ret += snprintf(buf + ret, size - ret,
517 						"%s%d-%d", COMMA,
518 						map->map[start], map->map[end]);
519 			}
520 			first = false;
521 			start = i;
522 		}
523 	}
524 
525 #undef COMMA
526 
527 	pr_debug2("cpumask list: %s\n", buf);
528 	return ret;
529 }
530 
531 static char hex_char(unsigned char val)
532 {
533 	if (val < 10)
534 		return val + '0';
535 	if (val < 16)
536 		return val - 10 + 'a';
537 	return '?';
538 }
539 
540 size_t cpu_map__snprint_mask(struct perf_cpu_map *map, char *buf, size_t size)
541 {
542 	int i, cpu;
543 	char *ptr = buf;
544 	unsigned char *bitmap;
545 	int last_cpu = perf_cpu_map__cpu(map, map->nr - 1);
546 
547 	if (buf == NULL)
548 		return 0;
549 
550 	bitmap = zalloc(last_cpu / 8 + 1);
551 	if (bitmap == NULL) {
552 		buf[0] = '\0';
553 		return 0;
554 	}
555 
556 	for (i = 0; i < map->nr; i++) {
557 		cpu = perf_cpu_map__cpu(map, i);
558 		bitmap[cpu / 8] |= 1 << (cpu % 8);
559 	}
560 
561 	for (cpu = last_cpu / 4 * 4; cpu >= 0; cpu -= 4) {
562 		unsigned char bits = bitmap[cpu / 8];
563 
564 		if (cpu % 8)
565 			bits >>= 4;
566 		else
567 			bits &= 0xf;
568 
569 		*ptr++ = hex_char(bits);
570 		if ((cpu % 32) == 0 && cpu > 0)
571 			*ptr++ = ',';
572 	}
573 	*ptr = '\0';
574 	free(bitmap);
575 
576 	buf[size - 1] = '\0';
577 	return ptr - buf;
578 }
579 
580 const struct perf_cpu_map *cpu_map__online(void) /* thread unsafe */
581 {
582 	static const struct perf_cpu_map *online = NULL;
583 
584 	if (!online)
585 		online = perf_cpu_map__new(NULL); /* from /sys/devices/system/cpu/online */
586 
587 	return online;
588 }
589 
590 bool aggr_cpu_id__equal(const struct aggr_cpu_id *a, const struct aggr_cpu_id *b)
591 {
592 	return a->thread == b->thread &&
593 		a->node == b->node &&
594 		a->socket == b->socket &&
595 		a->die == b->die &&
596 		a->core == b->core &&
597 		a->cpu == b->cpu;
598 }
599 
600 bool aggr_cpu_id__is_empty(const struct aggr_cpu_id *a)
601 {
602 	return a->thread == -1 &&
603 		a->node == -1 &&
604 		a->socket == -1 &&
605 		a->die == -1 &&
606 		a->core == -1 &&
607 		a->cpu == -1;
608 }
609 
610 struct aggr_cpu_id aggr_cpu_id__empty(void)
611 {
612 	struct aggr_cpu_id ret = {
613 		.thread = -1,
614 		.node = -1,
615 		.socket = -1,
616 		.die = -1,
617 		.core = -1,
618 		.cpu = -1
619 	};
620 	return ret;
621 }
622