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