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>
15 struct perf_env perf_env;
17 #ifdef HAVE_LIBBPF_SUPPORT
18 #include "bpf-event.h"
19 #include "bpf-utils.h"
20 #include <bpf/libbpf.h>
22 void perf_env__insert_bpf_prog_info(struct perf_env *env,
23 struct bpf_prog_info_node *info_node)
25 __u32 prog_id = info_node->info_linear->info.id;
26 struct bpf_prog_info_node *node;
27 struct rb_node *parent = NULL;
30 down_write(&env->bpf_progs.lock);
31 p = &env->bpf_progs.infos.rb_node;
35 node = rb_entry(parent, struct bpf_prog_info_node, rb_node);
36 if (prog_id < node->info_linear->info.id) {
38 } else if (prog_id > node->info_linear->info.id) {
41 pr_debug("duplicated bpf prog info %u\n", prog_id);
46 rb_link_node(&info_node->rb_node, parent, p);
47 rb_insert_color(&info_node->rb_node, &env->bpf_progs.infos);
48 env->bpf_progs.infos_cnt++;
50 up_write(&env->bpf_progs.lock);
53 struct bpf_prog_info_node *perf_env__find_bpf_prog_info(struct perf_env *env,
56 struct bpf_prog_info_node *node = NULL;
59 down_read(&env->bpf_progs.lock);
60 n = env->bpf_progs.infos.rb_node;
63 node = rb_entry(n, struct bpf_prog_info_node, rb_node);
64 if (prog_id < node->info_linear->info.id)
66 else if (prog_id > node->info_linear->info.id)
74 up_read(&env->bpf_progs.lock);
78 bool perf_env__insert_btf(struct perf_env *env, struct btf_node *btf_node)
80 struct rb_node *parent = NULL;
81 __u32 btf_id = btf_node->id;
82 struct btf_node *node;
86 down_write(&env->bpf_progs.lock);
87 p = &env->bpf_progs.btfs.rb_node;
91 node = rb_entry(parent, struct btf_node, rb_node);
92 if (btf_id < node->id) {
94 } else if (btf_id > node->id) {
97 pr_debug("duplicated btf %u\n", btf_id);
103 rb_link_node(&btf_node->rb_node, parent, p);
104 rb_insert_color(&btf_node->rb_node, &env->bpf_progs.btfs);
105 env->bpf_progs.btfs_cnt++;
107 up_write(&env->bpf_progs.lock);
111 struct btf_node *perf_env__find_btf(struct perf_env *env, __u32 btf_id)
113 struct btf_node *node = NULL;
116 down_read(&env->bpf_progs.lock);
117 n = env->bpf_progs.btfs.rb_node;
120 node = rb_entry(n, struct btf_node, rb_node);
121 if (btf_id < node->id)
123 else if (btf_id > node->id)
131 up_read(&env->bpf_progs.lock);
135 /* purge data in bpf_progs.infos tree */
136 static void perf_env__purge_bpf(struct perf_env *env)
138 struct rb_root *root;
139 struct rb_node *next;
141 down_write(&env->bpf_progs.lock);
143 root = &env->bpf_progs.infos;
144 next = rb_first(root);
147 struct bpf_prog_info_node *node;
149 node = rb_entry(next, struct bpf_prog_info_node, rb_node);
150 next = rb_next(&node->rb_node);
151 rb_erase(&node->rb_node, root);
152 zfree(&node->info_linear);
156 env->bpf_progs.infos_cnt = 0;
158 root = &env->bpf_progs.btfs;
159 next = rb_first(root);
162 struct btf_node *node;
164 node = rb_entry(next, struct btf_node, rb_node);
165 next = rb_next(&node->rb_node);
166 rb_erase(&node->rb_node, root);
170 env->bpf_progs.btfs_cnt = 0;
172 up_write(&env->bpf_progs.lock);
174 #else // HAVE_LIBBPF_SUPPORT
175 static void perf_env__purge_bpf(struct perf_env *env __maybe_unused)
178 #endif // HAVE_LIBBPF_SUPPORT
180 void perf_env__exit(struct perf_env *env)
184 perf_env__purge_bpf(env);
185 perf_env__purge_cgroups(env);
186 zfree(&env->hostname);
187 zfree(&env->os_release);
188 zfree(&env->version);
190 zfree(&env->cpu_desc);
192 zfree(&env->cmdline);
193 zfree(&env->cmdline_argv);
194 zfree(&env->sibling_dies);
195 zfree(&env->sibling_cores);
196 zfree(&env->sibling_threads);
197 zfree(&env->pmu_mappings);
199 for (i = 0; i < env->nr_cpu_pmu_caps; i++)
200 zfree(&env->cpu_pmu_caps[i]);
201 zfree(&env->cpu_pmu_caps);
202 zfree(&env->numa_map);
204 for (i = 0; i < env->nr_numa_nodes; i++)
205 perf_cpu_map__put(env->numa_nodes[i].map);
206 zfree(&env->numa_nodes);
208 for (i = 0; i < env->caches_cnt; i++)
209 cpu_cache_level__free(&env->caches[i]);
212 for (i = 0; i < env->nr_memory_nodes; i++)
213 zfree(&env->memory_nodes[i].set);
214 zfree(&env->memory_nodes);
216 for (i = 0; i < env->nr_hybrid_nodes; i++) {
217 zfree(&env->hybrid_nodes[i].pmu_name);
218 zfree(&env->hybrid_nodes[i].cpus);
220 zfree(&env->hybrid_nodes);
222 for (i = 0; i < env->nr_pmus_with_caps; i++) {
223 for (j = 0; j < env->pmu_caps[i].nr_caps; j++)
224 zfree(&env->pmu_caps[i].caps[j]);
225 zfree(&env->pmu_caps[i].caps);
226 zfree(&env->pmu_caps[i].pmu_name);
228 zfree(&env->pmu_caps);
231 void perf_env__init(struct perf_env *env)
233 #ifdef HAVE_LIBBPF_SUPPORT
234 env->bpf_progs.infos = RB_ROOT;
235 env->bpf_progs.btfs = RB_ROOT;
236 init_rwsem(&env->bpf_progs.lock);
238 env->kernel_is_64_bit = -1;
241 static void perf_env__init_kernel_mode(struct perf_env *env)
243 const char *arch = perf_env__raw_arch(env);
245 if (!strncmp(arch, "x86_64", 6) || !strncmp(arch, "aarch64", 7) ||
246 !strncmp(arch, "arm64", 5) || !strncmp(arch, "mips64", 6) ||
247 !strncmp(arch, "parisc64", 8) || !strncmp(arch, "riscv64", 7) ||
248 !strncmp(arch, "s390x", 5) || !strncmp(arch, "sparc64", 7))
249 env->kernel_is_64_bit = 1;
251 env->kernel_is_64_bit = 0;
254 int perf_env__kernel_is_64_bit(struct perf_env *env)
256 if (env->kernel_is_64_bit == -1)
257 perf_env__init_kernel_mode(env);
259 return env->kernel_is_64_bit;
262 int perf_env__set_cmdline(struct perf_env *env, int argc, const char *argv[])
266 /* do not include NULL termination */
267 env->cmdline_argv = calloc(argc, sizeof(char *));
268 if (env->cmdline_argv == NULL)
272 * Must copy argv contents because it gets moved around during option
275 for (i = 0; i < argc ; i++) {
276 env->cmdline_argv[i] = argv[i];
277 if (env->cmdline_argv[i] == NULL)
281 env->nr_cmdline = argc;
285 zfree(&env->cmdline_argv);
290 int perf_env__read_cpu_topology_map(struct perf_env *env)
294 if (env->cpu != NULL)
297 if (env->nr_cpus_avail == 0)
298 env->nr_cpus_avail = cpu__max_present_cpu().cpu;
300 nr_cpus = env->nr_cpus_avail;
304 env->cpu = calloc(nr_cpus, sizeof(env->cpu[0]));
305 if (env->cpu == NULL)
308 for (idx = 0; idx < nr_cpus; ++idx) {
309 struct perf_cpu cpu = { .cpu = idx };
311 env->cpu[idx].core_id = cpu__get_core_id(cpu);
312 env->cpu[idx].socket_id = cpu__get_socket_id(cpu);
313 env->cpu[idx].die_id = cpu__get_die_id(cpu);
316 env->nr_cpus_avail = nr_cpus;
320 int perf_env__read_pmu_mappings(struct perf_env *env)
322 struct perf_pmu *pmu = NULL;
326 while ((pmu = perf_pmu__scan(pmu))) {
332 pr_debug("pmu mappings not available\n");
335 env->nr_pmu_mappings = pmu_num;
337 if (strbuf_init(&sb, 128 * pmu_num) < 0)
340 while ((pmu = perf_pmu__scan(pmu))) {
343 if (strbuf_addf(&sb, "%u:%s", pmu->type, pmu->name) < 0)
345 /* include a NULL character at the end */
346 if (strbuf_add(&sb, "", 1) < 0)
350 env->pmu_mappings = strbuf_detach(&sb, NULL);
359 int perf_env__read_cpuid(struct perf_env *env)
362 int err = get_cpuid(cpuid, sizeof(cpuid));
368 env->cpuid = strdup(cpuid);
369 if (env->cpuid == NULL)
374 static int perf_env__read_arch(struct perf_env *env)
382 env->arch = strdup(uts.machine);
384 return env->arch ? 0 : -ENOMEM;
387 static int perf_env__read_nr_cpus_avail(struct perf_env *env)
389 if (env->nr_cpus_avail == 0)
390 env->nr_cpus_avail = cpu__max_present_cpu().cpu;
392 return env->nr_cpus_avail ? 0 : -ENOENT;
395 const char *perf_env__raw_arch(struct perf_env *env)
397 return env && !perf_env__read_arch(env) ? env->arch : "unknown";
400 int perf_env__nr_cpus_avail(struct perf_env *env)
402 return env && !perf_env__read_nr_cpus_avail(env) ? env->nr_cpus_avail : 0;
405 void cpu_cache_level__free(struct cpu_cache_level *cache)
413 * Return architecture name in a normalized form.
414 * The conversion logic comes from the Makefile.
416 static const char *normalize_arch(char *arch)
418 if (!strcmp(arch, "x86_64"))
420 if (arch[0] == 'i' && arch[2] == '8' && arch[3] == '6')
422 if (!strcmp(arch, "sun4u") || !strncmp(arch, "sparc", 5))
424 if (!strncmp(arch, "aarch64", 7) || !strncmp(arch, "arm64", 5))
426 if (!strncmp(arch, "arm", 3) || !strcmp(arch, "sa110"))
428 if (!strncmp(arch, "s390", 4))
430 if (!strncmp(arch, "parisc", 6))
432 if (!strncmp(arch, "powerpc", 7) || !strncmp(arch, "ppc", 3))
434 if (!strncmp(arch, "mips", 4))
436 if (!strncmp(arch, "sh", 2) && isdigit(arch[2]))
438 if (!strncmp(arch, "loongarch", 9))
444 const char *perf_env__arch(struct perf_env *env)
448 if (!env || !env->arch) { /* Assume local operation */
449 static struct utsname uts = { .machine[0] = '\0', };
450 if (uts.machine[0] == '\0' && uname(&uts) < 0)
452 arch_name = uts.machine;
454 arch_name = env->arch;
456 return normalize_arch(arch_name);
459 const char *perf_env__cpuid(struct perf_env *env)
463 if (!env || !env->cpuid) { /* Assume local operation */
464 status = perf_env__read_cpuid(env);
472 int perf_env__nr_pmu_mappings(struct perf_env *env)
476 if (!env || !env->nr_pmu_mappings) { /* Assume local operation */
477 status = perf_env__read_pmu_mappings(env);
482 return env->nr_pmu_mappings;
485 const char *perf_env__pmu_mappings(struct perf_env *env)
489 if (!env || !env->pmu_mappings) { /* Assume local operation */
490 status = perf_env__read_pmu_mappings(env);
495 return env->pmu_mappings;
498 int perf_env__numa_node(struct perf_env *env, struct perf_cpu cpu)
500 if (!env->nr_numa_map) {
501 struct numa_node *nn;
504 for (i = 0; i < env->nr_numa_nodes; i++) {
505 nn = &env->numa_nodes[i];
506 nr = max(nr, perf_cpu_map__max(nn->map).cpu);
512 * We initialize the numa_map array to prepare
513 * it for missing cpus, which return node -1
515 env->numa_map = malloc(nr * sizeof(int));
519 for (i = 0; i < nr; i++)
520 env->numa_map[i] = -1;
522 env->nr_numa_map = nr;
524 for (i = 0; i < env->nr_numa_nodes; i++) {
528 nn = &env->numa_nodes[i];
529 perf_cpu_map__for_each_cpu(tmp, j, nn->map)
530 env->numa_map[tmp.cpu] = i;
534 return cpu.cpu >= 0 && cpu.cpu < env->nr_numa_map ? env->numa_map[cpu.cpu] : -1;
537 char *perf_env__find_pmu_cap(struct perf_env *env, const char *pmu_name,
545 if (!pmu_name || !cap)
548 cap_size = strlen(cap);
549 cap_eq = zalloc(cap_size + 2);
553 memcpy(cap_eq, cap, cap_size);
554 cap_eq[cap_size] = '=';
556 if (!strcmp(pmu_name, "cpu")) {
557 for (i = 0; i < env->nr_cpu_pmu_caps; i++) {
558 if (!strncmp(env->cpu_pmu_caps[i], cap_eq, cap_size + 1)) {
560 return &env->cpu_pmu_caps[i][cap_size + 1];
566 for (i = 0; i < env->nr_pmus_with_caps; i++) {
567 if (strcmp(env->pmu_caps[i].pmu_name, pmu_name))
570 ptr = env->pmu_caps[i].caps;
572 for (j = 0; j < env->pmu_caps[i].nr_caps; j++) {
573 if (!strncmp(ptr[j], cap_eq, cap_size + 1)) {
575 return &ptr[j][cap_size + 1];