1 // SPDX-License-Identifier: GPL-2.0
5 #include "util/header.h"
6 #include <linux/ctype.h>
7 #include <linux/zalloc.h>
10 #include <sys/utsname.h>
16 struct perf_env perf_env;
18 #ifdef HAVE_LIBBPF_SUPPORT
19 #include "bpf-event.h"
20 #include "bpf-utils.h"
21 #include <bpf/libbpf.h>
23 void perf_env__insert_bpf_prog_info(struct perf_env *env,
24 struct bpf_prog_info_node *info_node)
26 __u32 prog_id = info_node->info_linear->info.id;
27 struct bpf_prog_info_node *node;
28 struct rb_node *parent = NULL;
31 down_write(&env->bpf_progs.lock);
32 p = &env->bpf_progs.infos.rb_node;
36 node = rb_entry(parent, struct bpf_prog_info_node, rb_node);
37 if (prog_id < node->info_linear->info.id) {
39 } else if (prog_id > node->info_linear->info.id) {
42 pr_debug("duplicated bpf prog info %u\n", prog_id);
47 rb_link_node(&info_node->rb_node, parent, p);
48 rb_insert_color(&info_node->rb_node, &env->bpf_progs.infos);
49 env->bpf_progs.infos_cnt++;
51 up_write(&env->bpf_progs.lock);
54 struct bpf_prog_info_node *perf_env__find_bpf_prog_info(struct perf_env *env,
57 struct bpf_prog_info_node *node = NULL;
60 down_read(&env->bpf_progs.lock);
61 n = env->bpf_progs.infos.rb_node;
64 node = rb_entry(n, struct bpf_prog_info_node, rb_node);
65 if (prog_id < node->info_linear->info.id)
67 else if (prog_id > node->info_linear->info.id)
75 up_read(&env->bpf_progs.lock);
79 bool perf_env__insert_btf(struct perf_env *env, struct btf_node *btf_node)
81 struct rb_node *parent = NULL;
82 __u32 btf_id = btf_node->id;
83 struct btf_node *node;
87 down_write(&env->bpf_progs.lock);
88 p = &env->bpf_progs.btfs.rb_node;
92 node = rb_entry(parent, struct btf_node, rb_node);
93 if (btf_id < node->id) {
95 } else if (btf_id > node->id) {
98 pr_debug("duplicated btf %u\n", btf_id);
104 rb_link_node(&btf_node->rb_node, parent, p);
105 rb_insert_color(&btf_node->rb_node, &env->bpf_progs.btfs);
106 env->bpf_progs.btfs_cnt++;
108 up_write(&env->bpf_progs.lock);
112 struct btf_node *perf_env__find_btf(struct perf_env *env, __u32 btf_id)
114 struct btf_node *node = NULL;
117 down_read(&env->bpf_progs.lock);
118 n = env->bpf_progs.btfs.rb_node;
121 node = rb_entry(n, struct btf_node, rb_node);
122 if (btf_id < node->id)
124 else if (btf_id > node->id)
132 up_read(&env->bpf_progs.lock);
136 /* purge data in bpf_progs.infos tree */
137 static void perf_env__purge_bpf(struct perf_env *env)
139 struct rb_root *root;
140 struct rb_node *next;
142 down_write(&env->bpf_progs.lock);
144 root = &env->bpf_progs.infos;
145 next = rb_first(root);
148 struct bpf_prog_info_node *node;
150 node = rb_entry(next, struct bpf_prog_info_node, rb_node);
151 next = rb_next(&node->rb_node);
152 rb_erase(&node->rb_node, root);
153 zfree(&node->info_linear);
157 env->bpf_progs.infos_cnt = 0;
159 root = &env->bpf_progs.btfs;
160 next = rb_first(root);
163 struct btf_node *node;
165 node = rb_entry(next, struct btf_node, rb_node);
166 next = rb_next(&node->rb_node);
167 rb_erase(&node->rb_node, root);
171 env->bpf_progs.btfs_cnt = 0;
173 up_write(&env->bpf_progs.lock);
175 #else // HAVE_LIBBPF_SUPPORT
176 static void perf_env__purge_bpf(struct perf_env *env __maybe_unused)
179 #endif // HAVE_LIBBPF_SUPPORT
181 void perf_env__exit(struct perf_env *env)
185 perf_env__purge_bpf(env);
186 perf_env__purge_cgroups(env);
187 zfree(&env->hostname);
188 zfree(&env->os_release);
189 zfree(&env->version);
191 zfree(&env->cpu_desc);
193 zfree(&env->cmdline);
194 zfree(&env->cmdline_argv);
195 zfree(&env->sibling_dies);
196 zfree(&env->sibling_cores);
197 zfree(&env->sibling_threads);
198 zfree(&env->pmu_mappings);
200 for (i = 0; i < env->nr_cpu_pmu_caps; i++)
201 zfree(&env->cpu_pmu_caps[i]);
202 zfree(&env->cpu_pmu_caps);
203 zfree(&env->numa_map);
205 for (i = 0; i < env->nr_numa_nodes; i++)
206 perf_cpu_map__put(env->numa_nodes[i].map);
207 zfree(&env->numa_nodes);
209 for (i = 0; i < env->caches_cnt; i++)
210 cpu_cache_level__free(&env->caches[i]);
213 for (i = 0; i < env->nr_memory_nodes; i++)
214 zfree(&env->memory_nodes[i].set);
215 zfree(&env->memory_nodes);
217 for (i = 0; i < env->nr_hybrid_nodes; i++) {
218 zfree(&env->hybrid_nodes[i].pmu_name);
219 zfree(&env->hybrid_nodes[i].cpus);
221 zfree(&env->hybrid_nodes);
223 for (i = 0; i < env->nr_pmus_with_caps; i++) {
224 for (j = 0; j < env->pmu_caps[i].nr_caps; j++)
225 zfree(&env->pmu_caps[i].caps[j]);
226 zfree(&env->pmu_caps[i].caps);
227 zfree(&env->pmu_caps[i].pmu_name);
229 zfree(&env->pmu_caps);
232 void perf_env__init(struct perf_env *env)
234 #ifdef HAVE_LIBBPF_SUPPORT
235 env->bpf_progs.infos = RB_ROOT;
236 env->bpf_progs.btfs = RB_ROOT;
237 init_rwsem(&env->bpf_progs.lock);
239 env->kernel_is_64_bit = -1;
242 static void perf_env__init_kernel_mode(struct perf_env *env)
244 const char *arch = perf_env__raw_arch(env);
246 if (!strncmp(arch, "x86_64", 6) || !strncmp(arch, "aarch64", 7) ||
247 !strncmp(arch, "arm64", 5) || !strncmp(arch, "mips64", 6) ||
248 !strncmp(arch, "parisc64", 8) || !strncmp(arch, "riscv64", 7) ||
249 !strncmp(arch, "s390x", 5) || !strncmp(arch, "sparc64", 7))
250 env->kernel_is_64_bit = 1;
252 env->kernel_is_64_bit = 0;
255 int perf_env__kernel_is_64_bit(struct perf_env *env)
257 if (env->kernel_is_64_bit == -1)
258 perf_env__init_kernel_mode(env);
260 return env->kernel_is_64_bit;
263 int perf_env__set_cmdline(struct perf_env *env, int argc, const char *argv[])
267 /* do not include NULL termination */
268 env->cmdline_argv = calloc(argc, sizeof(char *));
269 if (env->cmdline_argv == NULL)
273 * Must copy argv contents because it gets moved around during option
276 for (i = 0; i < argc ; i++) {
277 env->cmdline_argv[i] = argv[i];
278 if (env->cmdline_argv[i] == NULL)
282 env->nr_cmdline = argc;
286 zfree(&env->cmdline_argv);
291 int perf_env__read_cpu_topology_map(struct perf_env *env)
295 if (env->cpu != NULL)
298 if (env->nr_cpus_avail == 0)
299 env->nr_cpus_avail = cpu__max_present_cpu().cpu;
301 nr_cpus = env->nr_cpus_avail;
305 env->cpu = calloc(nr_cpus, sizeof(env->cpu[0]));
306 if (env->cpu == NULL)
309 for (idx = 0; idx < nr_cpus; ++idx) {
310 struct perf_cpu cpu = { .cpu = idx };
312 env->cpu[idx].core_id = cpu__get_core_id(cpu);
313 env->cpu[idx].socket_id = cpu__get_socket_id(cpu);
314 env->cpu[idx].die_id = cpu__get_die_id(cpu);
317 env->nr_cpus_avail = nr_cpus;
321 int perf_env__read_pmu_mappings(struct perf_env *env)
323 struct perf_pmu *pmu = NULL;
327 while ((pmu = perf_pmus__scan(pmu)))
331 pr_debug("pmu mappings not available\n");
334 env->nr_pmu_mappings = pmu_num;
336 if (strbuf_init(&sb, 128 * pmu_num) < 0)
339 while ((pmu = perf_pmus__scan(pmu))) {
340 if (strbuf_addf(&sb, "%u:%s", pmu->type, pmu->name) < 0)
342 /* include a NULL character at the end */
343 if (strbuf_add(&sb, "", 1) < 0)
347 env->pmu_mappings = strbuf_detach(&sb, NULL);
356 int perf_env__read_cpuid(struct perf_env *env)
359 int err = get_cpuid(cpuid, sizeof(cpuid));
365 env->cpuid = strdup(cpuid);
366 if (env->cpuid == NULL)
371 static int perf_env__read_arch(struct perf_env *env)
379 env->arch = strdup(uts.machine);
381 return env->arch ? 0 : -ENOMEM;
384 static int perf_env__read_nr_cpus_avail(struct perf_env *env)
386 if (env->nr_cpus_avail == 0)
387 env->nr_cpus_avail = cpu__max_present_cpu().cpu;
389 return env->nr_cpus_avail ? 0 : -ENOENT;
392 const char *perf_env__raw_arch(struct perf_env *env)
394 return env && !perf_env__read_arch(env) ? env->arch : "unknown";
397 int perf_env__nr_cpus_avail(struct perf_env *env)
399 return env && !perf_env__read_nr_cpus_avail(env) ? env->nr_cpus_avail : 0;
402 void cpu_cache_level__free(struct cpu_cache_level *cache)
410 * Return architecture name in a normalized form.
411 * The conversion logic comes from the Makefile.
413 static const char *normalize_arch(char *arch)
415 if (!strcmp(arch, "x86_64"))
417 if (arch[0] == 'i' && arch[2] == '8' && arch[3] == '6')
419 if (!strcmp(arch, "sun4u") || !strncmp(arch, "sparc", 5))
421 if (!strncmp(arch, "aarch64", 7) || !strncmp(arch, "arm64", 5))
423 if (!strncmp(arch, "arm", 3) || !strcmp(arch, "sa110"))
425 if (!strncmp(arch, "s390", 4))
427 if (!strncmp(arch, "parisc", 6))
429 if (!strncmp(arch, "powerpc", 7) || !strncmp(arch, "ppc", 3))
431 if (!strncmp(arch, "mips", 4))
433 if (!strncmp(arch, "sh", 2) && isdigit(arch[2]))
435 if (!strncmp(arch, "loongarch", 9))
441 const char *perf_env__arch(struct perf_env *env)
445 if (!env || !env->arch) { /* Assume local operation */
446 static struct utsname uts = { .machine[0] = '\0', };
447 if (uts.machine[0] == '\0' && uname(&uts) < 0)
449 arch_name = uts.machine;
451 arch_name = env->arch;
453 return normalize_arch(arch_name);
456 const char *perf_env__cpuid(struct perf_env *env)
460 if (!env || !env->cpuid) { /* Assume local operation */
461 status = perf_env__read_cpuid(env);
469 int perf_env__nr_pmu_mappings(struct perf_env *env)
473 if (!env || !env->nr_pmu_mappings) { /* Assume local operation */
474 status = perf_env__read_pmu_mappings(env);
479 return env->nr_pmu_mappings;
482 const char *perf_env__pmu_mappings(struct perf_env *env)
486 if (!env || !env->pmu_mappings) { /* Assume local operation */
487 status = perf_env__read_pmu_mappings(env);
492 return env->pmu_mappings;
495 int perf_env__numa_node(struct perf_env *env, struct perf_cpu cpu)
497 if (!env->nr_numa_map) {
498 struct numa_node *nn;
501 for (i = 0; i < env->nr_numa_nodes; i++) {
502 nn = &env->numa_nodes[i];
503 nr = max(nr, perf_cpu_map__max(nn->map).cpu);
509 * We initialize the numa_map array to prepare
510 * it for missing cpus, which return node -1
512 env->numa_map = malloc(nr * sizeof(int));
516 for (i = 0; i < nr; i++)
517 env->numa_map[i] = -1;
519 env->nr_numa_map = nr;
521 for (i = 0; i < env->nr_numa_nodes; i++) {
525 nn = &env->numa_nodes[i];
526 perf_cpu_map__for_each_cpu(tmp, j, nn->map)
527 env->numa_map[tmp.cpu] = i;
531 return cpu.cpu >= 0 && cpu.cpu < env->nr_numa_map ? env->numa_map[cpu.cpu] : -1;
534 char *perf_env__find_pmu_cap(struct perf_env *env, const char *pmu_name,
542 if (!pmu_name || !cap)
545 cap_size = strlen(cap);
546 cap_eq = zalloc(cap_size + 2);
550 memcpy(cap_eq, cap, cap_size);
551 cap_eq[cap_size] = '=';
553 if (!strcmp(pmu_name, "cpu")) {
554 for (i = 0; i < env->nr_cpu_pmu_caps; i++) {
555 if (!strncmp(env->cpu_pmu_caps[i], cap_eq, cap_size + 1)) {
557 return &env->cpu_pmu_caps[i][cap_size + 1];
563 for (i = 0; i < env->nr_pmus_with_caps; i++) {
564 if (strcmp(env->pmu_caps[i].pmu_name, pmu_name))
567 ptr = env->pmu_caps[i].caps;
569 for (j = 0; j < env->pmu_caps[i].nr_caps; j++) {
570 if (!strncmp(ptr[j], cap_eq, cap_size + 1)) {
572 return &ptr[j][cap_size + 1];