xref: /openbmc/linux/tools/perf/util/env.c (revision 801b27e8)
1 // SPDX-License-Identifier: GPL-2.0
2 #include "cpumap.h"
3 #include "debug.h"
4 #include "env.h"
5 #include "util/header.h"
6 #include <linux/ctype.h>
7 #include <linux/zalloc.h>
8 #include "cgroup.h"
9 #include <errno.h>
10 #include <sys/utsname.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include "pmus.h"
14 #include "strbuf.h"
15 
16 struct perf_env perf_env;
17 
18 #ifdef HAVE_LIBBPF_SUPPORT
19 #include "bpf-event.h"
20 #include "bpf-utils.h"
21 #include <bpf/libbpf.h>
22 
23 void perf_env__insert_bpf_prog_info(struct perf_env *env,
24 				    struct bpf_prog_info_node *info_node)
25 {
26 	__u32 prog_id = info_node->info_linear->info.id;
27 	struct bpf_prog_info_node *node;
28 	struct rb_node *parent = NULL;
29 	struct rb_node **p;
30 
31 	down_write(&env->bpf_progs.lock);
32 	p = &env->bpf_progs.infos.rb_node;
33 
34 	while (*p != NULL) {
35 		parent = *p;
36 		node = rb_entry(parent, struct bpf_prog_info_node, rb_node);
37 		if (prog_id < node->info_linear->info.id) {
38 			p = &(*p)->rb_left;
39 		} else if (prog_id > node->info_linear->info.id) {
40 			p = &(*p)->rb_right;
41 		} else {
42 			pr_debug("duplicated bpf prog info %u\n", prog_id);
43 			goto out;
44 		}
45 	}
46 
47 	rb_link_node(&info_node->rb_node, parent, p);
48 	rb_insert_color(&info_node->rb_node, &env->bpf_progs.infos);
49 	env->bpf_progs.infos_cnt++;
50 out:
51 	up_write(&env->bpf_progs.lock);
52 }
53 
54 struct bpf_prog_info_node *perf_env__find_bpf_prog_info(struct perf_env *env,
55 							__u32 prog_id)
56 {
57 	struct bpf_prog_info_node *node = NULL;
58 	struct rb_node *n;
59 
60 	down_read(&env->bpf_progs.lock);
61 	n = env->bpf_progs.infos.rb_node;
62 
63 	while (n) {
64 		node = rb_entry(n, struct bpf_prog_info_node, rb_node);
65 		if (prog_id < node->info_linear->info.id)
66 			n = n->rb_left;
67 		else if (prog_id > node->info_linear->info.id)
68 			n = n->rb_right;
69 		else
70 			goto out;
71 	}
72 	node = NULL;
73 
74 out:
75 	up_read(&env->bpf_progs.lock);
76 	return node;
77 }
78 
79 bool perf_env__insert_btf(struct perf_env *env, struct btf_node *btf_node)
80 {
81 	struct rb_node *parent = NULL;
82 	__u32 btf_id = btf_node->id;
83 	struct btf_node *node;
84 	struct rb_node **p;
85 	bool ret = true;
86 
87 	down_write(&env->bpf_progs.lock);
88 	p = &env->bpf_progs.btfs.rb_node;
89 
90 	while (*p != NULL) {
91 		parent = *p;
92 		node = rb_entry(parent, struct btf_node, rb_node);
93 		if (btf_id < node->id) {
94 			p = &(*p)->rb_left;
95 		} else if (btf_id > node->id) {
96 			p = &(*p)->rb_right;
97 		} else {
98 			pr_debug("duplicated btf %u\n", btf_id);
99 			ret = false;
100 			goto out;
101 		}
102 	}
103 
104 	rb_link_node(&btf_node->rb_node, parent, p);
105 	rb_insert_color(&btf_node->rb_node, &env->bpf_progs.btfs);
106 	env->bpf_progs.btfs_cnt++;
107 out:
108 	up_write(&env->bpf_progs.lock);
109 	return ret;
110 }
111 
112 struct btf_node *perf_env__find_btf(struct perf_env *env, __u32 btf_id)
113 {
114 	struct btf_node *node = NULL;
115 	struct rb_node *n;
116 
117 	down_read(&env->bpf_progs.lock);
118 	n = env->bpf_progs.btfs.rb_node;
119 
120 	while (n) {
121 		node = rb_entry(n, struct btf_node, rb_node);
122 		if (btf_id < node->id)
123 			n = n->rb_left;
124 		else if (btf_id > node->id)
125 			n = n->rb_right;
126 		else
127 			goto out;
128 	}
129 	node = NULL;
130 
131 out:
132 	up_read(&env->bpf_progs.lock);
133 	return node;
134 }
135 
136 /* purge data in bpf_progs.infos tree */
137 static void perf_env__purge_bpf(struct perf_env *env)
138 {
139 	struct rb_root *root;
140 	struct rb_node *next;
141 
142 	down_write(&env->bpf_progs.lock);
143 
144 	root = &env->bpf_progs.infos;
145 	next = rb_first(root);
146 
147 	while (next) {
148 		struct bpf_prog_info_node *node;
149 
150 		node = rb_entry(next, struct bpf_prog_info_node, rb_node);
151 		next = rb_next(&node->rb_node);
152 		rb_erase(&node->rb_node, root);
153 		zfree(&node->info_linear);
154 		free(node);
155 	}
156 
157 	env->bpf_progs.infos_cnt = 0;
158 
159 	root = &env->bpf_progs.btfs;
160 	next = rb_first(root);
161 
162 	while (next) {
163 		struct btf_node *node;
164 
165 		node = rb_entry(next, struct btf_node, rb_node);
166 		next = rb_next(&node->rb_node);
167 		rb_erase(&node->rb_node, root);
168 		free(node);
169 	}
170 
171 	env->bpf_progs.btfs_cnt = 0;
172 
173 	up_write(&env->bpf_progs.lock);
174 }
175 #else // HAVE_LIBBPF_SUPPORT
176 static void perf_env__purge_bpf(struct perf_env *env __maybe_unused)
177 {
178 }
179 #endif // HAVE_LIBBPF_SUPPORT
180 
181 void perf_env__exit(struct perf_env *env)
182 {
183 	int i, j;
184 
185 	perf_env__purge_bpf(env);
186 	perf_env__purge_cgroups(env);
187 	zfree(&env->hostname);
188 	zfree(&env->os_release);
189 	zfree(&env->version);
190 	zfree(&env->arch);
191 	zfree(&env->cpu_desc);
192 	zfree(&env->cpuid);
193 	zfree(&env->cmdline);
194 	zfree(&env->cmdline_argv);
195 	zfree(&env->sibling_dies);
196 	zfree(&env->sibling_cores);
197 	zfree(&env->sibling_threads);
198 	zfree(&env->pmu_mappings);
199 	zfree(&env->cpu);
200 	for (i = 0; i < env->nr_cpu_pmu_caps; i++)
201 		zfree(&env->cpu_pmu_caps[i]);
202 	zfree(&env->cpu_pmu_caps);
203 	zfree(&env->numa_map);
204 
205 	for (i = 0; i < env->nr_numa_nodes; i++)
206 		perf_cpu_map__put(env->numa_nodes[i].map);
207 	zfree(&env->numa_nodes);
208 
209 	for (i = 0; i < env->caches_cnt; i++)
210 		cpu_cache_level__free(&env->caches[i]);
211 	zfree(&env->caches);
212 
213 	for (i = 0; i < env->nr_memory_nodes; i++)
214 		zfree(&env->memory_nodes[i].set);
215 	zfree(&env->memory_nodes);
216 
217 	for (i = 0; i < env->nr_hybrid_nodes; i++) {
218 		zfree(&env->hybrid_nodes[i].pmu_name);
219 		zfree(&env->hybrid_nodes[i].cpus);
220 	}
221 	zfree(&env->hybrid_nodes);
222 
223 	for (i = 0; i < env->nr_pmus_with_caps; i++) {
224 		for (j = 0; j < env->pmu_caps[i].nr_caps; j++)
225 			zfree(&env->pmu_caps[i].caps[j]);
226 		zfree(&env->pmu_caps[i].caps);
227 		zfree(&env->pmu_caps[i].pmu_name);
228 	}
229 	zfree(&env->pmu_caps);
230 }
231 
232 void perf_env__init(struct perf_env *env)
233 {
234 #ifdef HAVE_LIBBPF_SUPPORT
235 	env->bpf_progs.infos = RB_ROOT;
236 	env->bpf_progs.btfs = RB_ROOT;
237 	init_rwsem(&env->bpf_progs.lock);
238 #endif
239 	env->kernel_is_64_bit = -1;
240 }
241 
242 static void perf_env__init_kernel_mode(struct perf_env *env)
243 {
244 	const char *arch = perf_env__raw_arch(env);
245 
246 	if (!strncmp(arch, "x86_64", 6) || !strncmp(arch, "aarch64", 7) ||
247 	    !strncmp(arch, "arm64", 5) || !strncmp(arch, "mips64", 6) ||
248 	    !strncmp(arch, "parisc64", 8) || !strncmp(arch, "riscv64", 7) ||
249 	    !strncmp(arch, "s390x", 5) || !strncmp(arch, "sparc64", 7))
250 		env->kernel_is_64_bit = 1;
251 	else
252 		env->kernel_is_64_bit = 0;
253 }
254 
255 int perf_env__kernel_is_64_bit(struct perf_env *env)
256 {
257 	if (env->kernel_is_64_bit == -1)
258 		perf_env__init_kernel_mode(env);
259 
260 	return env->kernel_is_64_bit;
261 }
262 
263 int perf_env__set_cmdline(struct perf_env *env, int argc, const char *argv[])
264 {
265 	int i;
266 
267 	/* do not include NULL termination */
268 	env->cmdline_argv = calloc(argc, sizeof(char *));
269 	if (env->cmdline_argv == NULL)
270 		goto out_enomem;
271 
272 	/*
273 	 * Must copy argv contents because it gets moved around during option
274 	 * parsing:
275 	 */
276 	for (i = 0; i < argc ; i++) {
277 		env->cmdline_argv[i] = argv[i];
278 		if (env->cmdline_argv[i] == NULL)
279 			goto out_free;
280 	}
281 
282 	env->nr_cmdline = argc;
283 
284 	return 0;
285 out_free:
286 	zfree(&env->cmdline_argv);
287 out_enomem:
288 	return -ENOMEM;
289 }
290 
291 int perf_env__read_cpu_topology_map(struct perf_env *env)
292 {
293 	int idx, nr_cpus;
294 
295 	if (env->cpu != NULL)
296 		return 0;
297 
298 	if (env->nr_cpus_avail == 0)
299 		env->nr_cpus_avail = cpu__max_present_cpu().cpu;
300 
301 	nr_cpus = env->nr_cpus_avail;
302 	if (nr_cpus == -1)
303 		return -EINVAL;
304 
305 	env->cpu = calloc(nr_cpus, sizeof(env->cpu[0]));
306 	if (env->cpu == NULL)
307 		return -ENOMEM;
308 
309 	for (idx = 0; idx < nr_cpus; ++idx) {
310 		struct perf_cpu cpu = { .cpu = idx };
311 
312 		env->cpu[idx].core_id	= cpu__get_core_id(cpu);
313 		env->cpu[idx].socket_id	= cpu__get_socket_id(cpu);
314 		env->cpu[idx].die_id	= cpu__get_die_id(cpu);
315 	}
316 
317 	env->nr_cpus_avail = nr_cpus;
318 	return 0;
319 }
320 
321 int perf_env__read_pmu_mappings(struct perf_env *env)
322 {
323 	struct perf_pmu *pmu = NULL;
324 	u32 pmu_num = 0;
325 	struct strbuf sb;
326 
327 	while ((pmu = perf_pmus__scan(pmu))) {
328 		if (!pmu->name)
329 			continue;
330 		pmu_num++;
331 	}
332 	if (!pmu_num) {
333 		pr_debug("pmu mappings not available\n");
334 		return -ENOENT;
335 	}
336 	env->nr_pmu_mappings = pmu_num;
337 
338 	if (strbuf_init(&sb, 128 * pmu_num) < 0)
339 		return -ENOMEM;
340 
341 	while ((pmu = perf_pmus__scan(pmu))) {
342 		if (!pmu->name)
343 			continue;
344 		if (strbuf_addf(&sb, "%u:%s", pmu->type, pmu->name) < 0)
345 			goto error;
346 		/* include a NULL character at the end */
347 		if (strbuf_add(&sb, "", 1) < 0)
348 			goto error;
349 	}
350 
351 	env->pmu_mappings = strbuf_detach(&sb, NULL);
352 
353 	return 0;
354 
355 error:
356 	strbuf_release(&sb);
357 	return -1;
358 }
359 
360 int perf_env__read_cpuid(struct perf_env *env)
361 {
362 	char cpuid[128];
363 	int err = get_cpuid(cpuid, sizeof(cpuid));
364 
365 	if (err)
366 		return err;
367 
368 	free(env->cpuid);
369 	env->cpuid = strdup(cpuid);
370 	if (env->cpuid == NULL)
371 		return ENOMEM;
372 	return 0;
373 }
374 
375 static int perf_env__read_arch(struct perf_env *env)
376 {
377 	struct utsname uts;
378 
379 	if (env->arch)
380 		return 0;
381 
382 	if (!uname(&uts))
383 		env->arch = strdup(uts.machine);
384 
385 	return env->arch ? 0 : -ENOMEM;
386 }
387 
388 static int perf_env__read_nr_cpus_avail(struct perf_env *env)
389 {
390 	if (env->nr_cpus_avail == 0)
391 		env->nr_cpus_avail = cpu__max_present_cpu().cpu;
392 
393 	return env->nr_cpus_avail ? 0 : -ENOENT;
394 }
395 
396 const char *perf_env__raw_arch(struct perf_env *env)
397 {
398 	return env && !perf_env__read_arch(env) ? env->arch : "unknown";
399 }
400 
401 int perf_env__nr_cpus_avail(struct perf_env *env)
402 {
403 	return env && !perf_env__read_nr_cpus_avail(env) ? env->nr_cpus_avail : 0;
404 }
405 
406 void cpu_cache_level__free(struct cpu_cache_level *cache)
407 {
408 	zfree(&cache->type);
409 	zfree(&cache->map);
410 	zfree(&cache->size);
411 }
412 
413 /*
414  * Return architecture name in a normalized form.
415  * The conversion logic comes from the Makefile.
416  */
417 static const char *normalize_arch(char *arch)
418 {
419 	if (!strcmp(arch, "x86_64"))
420 		return "x86";
421 	if (arch[0] == 'i' && arch[2] == '8' && arch[3] == '6')
422 		return "x86";
423 	if (!strcmp(arch, "sun4u") || !strncmp(arch, "sparc", 5))
424 		return "sparc";
425 	if (!strncmp(arch, "aarch64", 7) || !strncmp(arch, "arm64", 5))
426 		return "arm64";
427 	if (!strncmp(arch, "arm", 3) || !strcmp(arch, "sa110"))
428 		return "arm";
429 	if (!strncmp(arch, "s390", 4))
430 		return "s390";
431 	if (!strncmp(arch, "parisc", 6))
432 		return "parisc";
433 	if (!strncmp(arch, "powerpc", 7) || !strncmp(arch, "ppc", 3))
434 		return "powerpc";
435 	if (!strncmp(arch, "mips", 4))
436 		return "mips";
437 	if (!strncmp(arch, "sh", 2) && isdigit(arch[2]))
438 		return "sh";
439 	if (!strncmp(arch, "loongarch", 9))
440 		return "loongarch";
441 
442 	return arch;
443 }
444 
445 const char *perf_env__arch(struct perf_env *env)
446 {
447 	char *arch_name;
448 
449 	if (!env || !env->arch) { /* Assume local operation */
450 		static struct utsname uts = { .machine[0] = '\0', };
451 		if (uts.machine[0] == '\0' && uname(&uts) < 0)
452 			return NULL;
453 		arch_name = uts.machine;
454 	} else
455 		arch_name = env->arch;
456 
457 	return normalize_arch(arch_name);
458 }
459 
460 const char *perf_env__cpuid(struct perf_env *env)
461 {
462 	int status;
463 
464 	if (!env || !env->cpuid) { /* Assume local operation */
465 		status = perf_env__read_cpuid(env);
466 		if (status)
467 			return NULL;
468 	}
469 
470 	return env->cpuid;
471 }
472 
473 int perf_env__nr_pmu_mappings(struct perf_env *env)
474 {
475 	int status;
476 
477 	if (!env || !env->nr_pmu_mappings) { /* Assume local operation */
478 		status = perf_env__read_pmu_mappings(env);
479 		if (status)
480 			return 0;
481 	}
482 
483 	return env->nr_pmu_mappings;
484 }
485 
486 const char *perf_env__pmu_mappings(struct perf_env *env)
487 {
488 	int status;
489 
490 	if (!env || !env->pmu_mappings) { /* Assume local operation */
491 		status = perf_env__read_pmu_mappings(env);
492 		if (status)
493 			return NULL;
494 	}
495 
496 	return env->pmu_mappings;
497 }
498 
499 int perf_env__numa_node(struct perf_env *env, struct perf_cpu cpu)
500 {
501 	if (!env->nr_numa_map) {
502 		struct numa_node *nn;
503 		int i, nr = 0;
504 
505 		for (i = 0; i < env->nr_numa_nodes; i++) {
506 			nn = &env->numa_nodes[i];
507 			nr = max(nr, perf_cpu_map__max(nn->map).cpu);
508 		}
509 
510 		nr++;
511 
512 		/*
513 		 * We initialize the numa_map array to prepare
514 		 * it for missing cpus, which return node -1
515 		 */
516 		env->numa_map = malloc(nr * sizeof(int));
517 		if (!env->numa_map)
518 			return -1;
519 
520 		for (i = 0; i < nr; i++)
521 			env->numa_map[i] = -1;
522 
523 		env->nr_numa_map = nr;
524 
525 		for (i = 0; i < env->nr_numa_nodes; i++) {
526 			struct perf_cpu tmp;
527 			int j;
528 
529 			nn = &env->numa_nodes[i];
530 			perf_cpu_map__for_each_cpu(tmp, j, nn->map)
531 				env->numa_map[tmp.cpu] = i;
532 		}
533 	}
534 
535 	return cpu.cpu >= 0 && cpu.cpu < env->nr_numa_map ? env->numa_map[cpu.cpu] : -1;
536 }
537 
538 char *perf_env__find_pmu_cap(struct perf_env *env, const char *pmu_name,
539 			     const char *cap)
540 {
541 	char *cap_eq;
542 	int cap_size;
543 	char **ptr;
544 	int i, j;
545 
546 	if (!pmu_name || !cap)
547 		return NULL;
548 
549 	cap_size = strlen(cap);
550 	cap_eq = zalloc(cap_size + 2);
551 	if (!cap_eq)
552 		return NULL;
553 
554 	memcpy(cap_eq, cap, cap_size);
555 	cap_eq[cap_size] = '=';
556 
557 	if (!strcmp(pmu_name, "cpu")) {
558 		for (i = 0; i < env->nr_cpu_pmu_caps; i++) {
559 			if (!strncmp(env->cpu_pmu_caps[i], cap_eq, cap_size + 1)) {
560 				free(cap_eq);
561 				return &env->cpu_pmu_caps[i][cap_size + 1];
562 			}
563 		}
564 		goto out;
565 	}
566 
567 	for (i = 0; i < env->nr_pmus_with_caps; i++) {
568 		if (strcmp(env->pmu_caps[i].pmu_name, pmu_name))
569 			continue;
570 
571 		ptr = env->pmu_caps[i].caps;
572 
573 		for (j = 0; j < env->pmu_caps[i].nr_caps; j++) {
574 			if (!strncmp(ptr[j], cap_eq, cap_size + 1)) {
575 				free(cap_eq);
576 				return &ptr[j][cap_size + 1];
577 			}
578 		}
579 	}
580 
581 out:
582 	free(cap_eq);
583 	return NULL;
584 }
585