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