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