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 
perf_env__insert_bpf_prog_info(struct perf_env * env,struct bpf_prog_info_node * info_node)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 
perf_env__find_bpf_prog_info(struct perf_env * env,__u32 prog_id)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 
perf_env__insert_btf(struct perf_env * env,struct btf_node * btf_node)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 
perf_env__find_btf(struct perf_env * env,__u32 btf_id)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 */
perf_env__purge_bpf(struct perf_env * env)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
perf_env__purge_bpf(struct perf_env * env __maybe_unused)176 static void perf_env__purge_bpf(struct perf_env *env __maybe_unused)
177 {
178 }
179 #endif // HAVE_LIBBPF_SUPPORT
180 
perf_env__exit(struct perf_env * env)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 
perf_env__init(struct perf_env * env)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 
perf_env__init_kernel_mode(struct perf_env * env)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 
perf_env__kernel_is_64_bit(struct perf_env * env)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 
perf_env__set_cmdline(struct perf_env * env,int argc,const char * argv[])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 
perf_env__read_cpu_topology_map(struct perf_env * env)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 
perf_env__read_pmu_mappings(struct perf_env * env)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 		pmu_num++;
329 
330 	if (!pmu_num) {
331 		pr_debug("pmu mappings not available\n");
332 		return -ENOENT;
333 	}
334 	env->nr_pmu_mappings = pmu_num;
335 
336 	if (strbuf_init(&sb, 128 * pmu_num) < 0)
337 		return -ENOMEM;
338 
339 	while ((pmu = perf_pmus__scan(pmu))) {
340 		if (strbuf_addf(&sb, "%u:%s", pmu->type, pmu->name) < 0)
341 			goto error;
342 		/* include a NULL character at the end */
343 		if (strbuf_add(&sb, "", 1) < 0)
344 			goto error;
345 	}
346 
347 	env->pmu_mappings = strbuf_detach(&sb, NULL);
348 
349 	return 0;
350 
351 error:
352 	strbuf_release(&sb);
353 	return -1;
354 }
355 
perf_env__read_cpuid(struct perf_env * env)356 int perf_env__read_cpuid(struct perf_env *env)
357 {
358 	char cpuid[128];
359 	int err = get_cpuid(cpuid, sizeof(cpuid));
360 
361 	if (err)
362 		return err;
363 
364 	free(env->cpuid);
365 	env->cpuid = strdup(cpuid);
366 	if (env->cpuid == NULL)
367 		return ENOMEM;
368 	return 0;
369 }
370 
perf_env__read_arch(struct perf_env * env)371 static int perf_env__read_arch(struct perf_env *env)
372 {
373 	struct utsname uts;
374 
375 	if (env->arch)
376 		return 0;
377 
378 	if (!uname(&uts))
379 		env->arch = strdup(uts.machine);
380 
381 	return env->arch ? 0 : -ENOMEM;
382 }
383 
perf_env__read_nr_cpus_avail(struct perf_env * env)384 static int perf_env__read_nr_cpus_avail(struct perf_env *env)
385 {
386 	if (env->nr_cpus_avail == 0)
387 		env->nr_cpus_avail = cpu__max_present_cpu().cpu;
388 
389 	return env->nr_cpus_avail ? 0 : -ENOENT;
390 }
391 
perf_env__raw_arch(struct perf_env * env)392 const char *perf_env__raw_arch(struct perf_env *env)
393 {
394 	return env && !perf_env__read_arch(env) ? env->arch : "unknown";
395 }
396 
perf_env__nr_cpus_avail(struct perf_env * env)397 int perf_env__nr_cpus_avail(struct perf_env *env)
398 {
399 	return env && !perf_env__read_nr_cpus_avail(env) ? env->nr_cpus_avail : 0;
400 }
401 
cpu_cache_level__free(struct cpu_cache_level * cache)402 void cpu_cache_level__free(struct cpu_cache_level *cache)
403 {
404 	zfree(&cache->type);
405 	zfree(&cache->map);
406 	zfree(&cache->size);
407 }
408 
409 /*
410  * Return architecture name in a normalized form.
411  * The conversion logic comes from the Makefile.
412  */
normalize_arch(char * arch)413 static const char *normalize_arch(char *arch)
414 {
415 	if (!strcmp(arch, "x86_64"))
416 		return "x86";
417 	if (arch[0] == 'i' && arch[2] == '8' && arch[3] == '6')
418 		return "x86";
419 	if (!strcmp(arch, "sun4u") || !strncmp(arch, "sparc", 5))
420 		return "sparc";
421 	if (!strncmp(arch, "aarch64", 7) || !strncmp(arch, "arm64", 5))
422 		return "arm64";
423 	if (!strncmp(arch, "arm", 3) || !strcmp(arch, "sa110"))
424 		return "arm";
425 	if (!strncmp(arch, "s390", 4))
426 		return "s390";
427 	if (!strncmp(arch, "parisc", 6))
428 		return "parisc";
429 	if (!strncmp(arch, "powerpc", 7) || !strncmp(arch, "ppc", 3))
430 		return "powerpc";
431 	if (!strncmp(arch, "mips", 4))
432 		return "mips";
433 	if (!strncmp(arch, "sh", 2) && isdigit(arch[2]))
434 		return "sh";
435 	if (!strncmp(arch, "loongarch", 9))
436 		return "loongarch";
437 
438 	return arch;
439 }
440 
perf_env__arch(struct perf_env * env)441 const char *perf_env__arch(struct perf_env *env)
442 {
443 	char *arch_name;
444 
445 	if (!env || !env->arch) { /* Assume local operation */
446 		static struct utsname uts = { .machine[0] = '\0', };
447 		if (uts.machine[0] == '\0' && uname(&uts) < 0)
448 			return NULL;
449 		arch_name = uts.machine;
450 	} else
451 		arch_name = env->arch;
452 
453 	return normalize_arch(arch_name);
454 }
455 
perf_env__cpuid(struct perf_env * env)456 const char *perf_env__cpuid(struct perf_env *env)
457 {
458 	int status;
459 
460 	if (!env || !env->cpuid) { /* Assume local operation */
461 		status = perf_env__read_cpuid(env);
462 		if (status)
463 			return NULL;
464 	}
465 
466 	return env->cpuid;
467 }
468 
perf_env__nr_pmu_mappings(struct perf_env * env)469 int perf_env__nr_pmu_mappings(struct perf_env *env)
470 {
471 	int status;
472 
473 	if (!env || !env->nr_pmu_mappings) { /* Assume local operation */
474 		status = perf_env__read_pmu_mappings(env);
475 		if (status)
476 			return 0;
477 	}
478 
479 	return env->nr_pmu_mappings;
480 }
481 
perf_env__pmu_mappings(struct perf_env * env)482 const char *perf_env__pmu_mappings(struct perf_env *env)
483 {
484 	int status;
485 
486 	if (!env || !env->pmu_mappings) { /* Assume local operation */
487 		status = perf_env__read_pmu_mappings(env);
488 		if (status)
489 			return NULL;
490 	}
491 
492 	return env->pmu_mappings;
493 }
494 
perf_env__numa_node(struct perf_env * env,struct perf_cpu cpu)495 int perf_env__numa_node(struct perf_env *env, struct perf_cpu cpu)
496 {
497 	if (!env->nr_numa_map) {
498 		struct numa_node *nn;
499 		int i, nr = 0;
500 
501 		for (i = 0; i < env->nr_numa_nodes; i++) {
502 			nn = &env->numa_nodes[i];
503 			nr = max(nr, perf_cpu_map__max(nn->map).cpu);
504 		}
505 
506 		nr++;
507 
508 		/*
509 		 * We initialize the numa_map array to prepare
510 		 * it for missing cpus, which return node -1
511 		 */
512 		env->numa_map = malloc(nr * sizeof(int));
513 		if (!env->numa_map)
514 			return -1;
515 
516 		for (i = 0; i < nr; i++)
517 			env->numa_map[i] = -1;
518 
519 		env->nr_numa_map = nr;
520 
521 		for (i = 0; i < env->nr_numa_nodes; i++) {
522 			struct perf_cpu tmp;
523 			int j;
524 
525 			nn = &env->numa_nodes[i];
526 			perf_cpu_map__for_each_cpu(tmp, j, nn->map)
527 				env->numa_map[tmp.cpu] = i;
528 		}
529 	}
530 
531 	return cpu.cpu >= 0 && cpu.cpu < env->nr_numa_map ? env->numa_map[cpu.cpu] : -1;
532 }
533 
perf_env__find_pmu_cap(struct perf_env * env,const char * pmu_name,const char * cap)534 char *perf_env__find_pmu_cap(struct perf_env *env, const char *pmu_name,
535 			     const char *cap)
536 {
537 	char *cap_eq;
538 	int cap_size;
539 	char **ptr;
540 	int i, j;
541 
542 	if (!pmu_name || !cap)
543 		return NULL;
544 
545 	cap_size = strlen(cap);
546 	cap_eq = zalloc(cap_size + 2);
547 	if (!cap_eq)
548 		return NULL;
549 
550 	memcpy(cap_eq, cap, cap_size);
551 	cap_eq[cap_size] = '=';
552 
553 	if (!strcmp(pmu_name, "cpu")) {
554 		for (i = 0; i < env->nr_cpu_pmu_caps; i++) {
555 			if (!strncmp(env->cpu_pmu_caps[i], cap_eq, cap_size + 1)) {
556 				free(cap_eq);
557 				return &env->cpu_pmu_caps[i][cap_size + 1];
558 			}
559 		}
560 		goto out;
561 	}
562 
563 	for (i = 0; i < env->nr_pmus_with_caps; i++) {
564 		if (strcmp(env->pmu_caps[i].pmu_name, pmu_name))
565 			continue;
566 
567 		ptr = env->pmu_caps[i].caps;
568 
569 		for (j = 0; j < env->pmu_caps[i].nr_caps; j++) {
570 			if (!strncmp(ptr[j], cap_eq, cap_size + 1)) {
571 				free(cap_eq);
572 				return &ptr[j][cap_size + 1];
573 			}
574 		}
575 	}
576 
577 out:
578 	free(cap_eq);
579 	return NULL;
580 }
581