xref: /openbmc/linux/tools/perf/util/env.c (revision 842ed298)
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);
148 	}
149 
150 	env->bpf_progs.infos_cnt = 0;
151 
152 	root = &env->bpf_progs.btfs;
153 	next = rb_first(root);
154 
155 	while (next) {
156 		struct btf_node *node;
157 
158 		node = rb_entry(next, struct btf_node, rb_node);
159 		next = rb_next(&node->rb_node);
160 		rb_erase(&node->rb_node, root);
161 		free(node);
162 	}
163 
164 	env->bpf_progs.btfs_cnt = 0;
165 
166 	up_write(&env->bpf_progs.lock);
167 }
168 #else // HAVE_LIBBPF_SUPPORT
169 static void perf_env__purge_bpf(struct perf_env *env __maybe_unused)
170 {
171 }
172 #endif // HAVE_LIBBPF_SUPPORT
173 
174 void perf_env__exit(struct perf_env *env)
175 {
176 	int i;
177 
178 	perf_env__purge_bpf(env);
179 	perf_env__purge_cgroups(env);
180 	zfree(&env->hostname);
181 	zfree(&env->os_release);
182 	zfree(&env->version);
183 	zfree(&env->arch);
184 	zfree(&env->cpu_desc);
185 	zfree(&env->cpuid);
186 	zfree(&env->cmdline);
187 	zfree(&env->cmdline_argv);
188 	zfree(&env->sibling_cores);
189 	zfree(&env->sibling_threads);
190 	zfree(&env->pmu_mappings);
191 	zfree(&env->cpu);
192 	zfree(&env->numa_map);
193 
194 	for (i = 0; i < env->nr_numa_nodes; i++)
195 		perf_cpu_map__put(env->numa_nodes[i].map);
196 	zfree(&env->numa_nodes);
197 
198 	for (i = 0; i < env->caches_cnt; i++)
199 		cpu_cache_level__free(&env->caches[i]);
200 	zfree(&env->caches);
201 
202 	for (i = 0; i < env->nr_memory_nodes; i++)
203 		zfree(&env->memory_nodes[i].set);
204 	zfree(&env->memory_nodes);
205 }
206 
207 void perf_env__init(struct perf_env *env __maybe_unused)
208 {
209 #ifdef HAVE_LIBBPF_SUPPORT
210 	env->bpf_progs.infos = RB_ROOT;
211 	env->bpf_progs.btfs = RB_ROOT;
212 	init_rwsem(&env->bpf_progs.lock);
213 #endif
214 }
215 
216 int perf_env__set_cmdline(struct perf_env *env, int argc, const char *argv[])
217 {
218 	int i;
219 
220 	/* do not include NULL termination */
221 	env->cmdline_argv = calloc(argc, sizeof(char *));
222 	if (env->cmdline_argv == NULL)
223 		goto out_enomem;
224 
225 	/*
226 	 * Must copy argv contents because it gets moved around during option
227 	 * parsing:
228 	 */
229 	for (i = 0; i < argc ; i++) {
230 		env->cmdline_argv[i] = argv[i];
231 		if (env->cmdline_argv[i] == NULL)
232 			goto out_free;
233 	}
234 
235 	env->nr_cmdline = argc;
236 
237 	return 0;
238 out_free:
239 	zfree(&env->cmdline_argv);
240 out_enomem:
241 	return -ENOMEM;
242 }
243 
244 int perf_env__read_cpu_topology_map(struct perf_env *env)
245 {
246 	int cpu, nr_cpus;
247 
248 	if (env->cpu != NULL)
249 		return 0;
250 
251 	if (env->nr_cpus_avail == 0)
252 		env->nr_cpus_avail = cpu__max_present_cpu();
253 
254 	nr_cpus = env->nr_cpus_avail;
255 	if (nr_cpus == -1)
256 		return -EINVAL;
257 
258 	env->cpu = calloc(nr_cpus, sizeof(env->cpu[0]));
259 	if (env->cpu == NULL)
260 		return -ENOMEM;
261 
262 	for (cpu = 0; cpu < nr_cpus; ++cpu) {
263 		env->cpu[cpu].core_id	= cpu_map__get_core_id(cpu);
264 		env->cpu[cpu].socket_id	= cpu_map__get_socket_id(cpu);
265 		env->cpu[cpu].die_id	= cpu_map__get_die_id(cpu);
266 	}
267 
268 	env->nr_cpus_avail = nr_cpus;
269 	return 0;
270 }
271 
272 int perf_env__read_cpuid(struct perf_env *env)
273 {
274 	char cpuid[128];
275 	int err = get_cpuid(cpuid, sizeof(cpuid));
276 
277 	if (err)
278 		return err;
279 
280 	free(env->cpuid);
281 	env->cpuid = strdup(cpuid);
282 	if (env->cpuid == NULL)
283 		return ENOMEM;
284 	return 0;
285 }
286 
287 static int perf_env__read_arch(struct perf_env *env)
288 {
289 	struct utsname uts;
290 
291 	if (env->arch)
292 		return 0;
293 
294 	if (!uname(&uts))
295 		env->arch = strdup(uts.machine);
296 
297 	return env->arch ? 0 : -ENOMEM;
298 }
299 
300 static int perf_env__read_nr_cpus_avail(struct perf_env *env)
301 {
302 	if (env->nr_cpus_avail == 0)
303 		env->nr_cpus_avail = cpu__max_present_cpu();
304 
305 	return env->nr_cpus_avail ? 0 : -ENOENT;
306 }
307 
308 const char *perf_env__raw_arch(struct perf_env *env)
309 {
310 	return env && !perf_env__read_arch(env) ? env->arch : "unknown";
311 }
312 
313 int perf_env__nr_cpus_avail(struct perf_env *env)
314 {
315 	return env && !perf_env__read_nr_cpus_avail(env) ? env->nr_cpus_avail : 0;
316 }
317 
318 void cpu_cache_level__free(struct cpu_cache_level *cache)
319 {
320 	zfree(&cache->type);
321 	zfree(&cache->map);
322 	zfree(&cache->size);
323 }
324 
325 /*
326  * Return architecture name in a normalized form.
327  * The conversion logic comes from the Makefile.
328  */
329 static const char *normalize_arch(char *arch)
330 {
331 	if (!strcmp(arch, "x86_64"))
332 		return "x86";
333 	if (arch[0] == 'i' && arch[2] == '8' && arch[3] == '6')
334 		return "x86";
335 	if (!strcmp(arch, "sun4u") || !strncmp(arch, "sparc", 5))
336 		return "sparc";
337 	if (!strcmp(arch, "aarch64") || !strcmp(arch, "arm64"))
338 		return "arm64";
339 	if (!strncmp(arch, "arm", 3) || !strcmp(arch, "sa110"))
340 		return "arm";
341 	if (!strncmp(arch, "s390", 4))
342 		return "s390";
343 	if (!strncmp(arch, "parisc", 6))
344 		return "parisc";
345 	if (!strncmp(arch, "powerpc", 7) || !strncmp(arch, "ppc", 3))
346 		return "powerpc";
347 	if (!strncmp(arch, "mips", 4))
348 		return "mips";
349 	if (!strncmp(arch, "sh", 2) && isdigit(arch[2]))
350 		return "sh";
351 
352 	return arch;
353 }
354 
355 const char *perf_env__arch(struct perf_env *env)
356 {
357 	char *arch_name;
358 
359 	if (!env || !env->arch) { /* Assume local operation */
360 		static struct utsname uts = { .machine[0] = '\0', };
361 		if (uts.machine[0] == '\0' && uname(&uts) < 0)
362 			return NULL;
363 		arch_name = uts.machine;
364 	} else
365 		arch_name = env->arch;
366 
367 	return normalize_arch(arch_name);
368 }
369 
370 
371 int perf_env__numa_node(struct perf_env *env, int cpu)
372 {
373 	if (!env->nr_numa_map) {
374 		struct numa_node *nn;
375 		int i, nr = 0;
376 
377 		for (i = 0; i < env->nr_numa_nodes; i++) {
378 			nn = &env->numa_nodes[i];
379 			nr = max(nr, perf_cpu_map__max(nn->map));
380 		}
381 
382 		nr++;
383 
384 		/*
385 		 * We initialize the numa_map array to prepare
386 		 * it for missing cpus, which return node -1
387 		 */
388 		env->numa_map = malloc(nr * sizeof(int));
389 		if (!env->numa_map)
390 			return -1;
391 
392 		for (i = 0; i < nr; i++)
393 			env->numa_map[i] = -1;
394 
395 		env->nr_numa_map = nr;
396 
397 		for (i = 0; i < env->nr_numa_nodes; i++) {
398 			int tmp, j;
399 
400 			nn = &env->numa_nodes[i];
401 			perf_cpu_map__for_each_cpu(j, tmp, nn->map)
402 				env->numa_map[j] = i;
403 		}
404 	}
405 
406 	return cpu >= 0 && cpu < env->nr_numa_map ? env->numa_map[cpu] : -1;
407 }
408