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