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