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>
11 #include <bpf/libbpf.h>
15 struct perf_env perf_env
;
17 void perf_env__insert_bpf_prog_info(struct perf_env
*env
,
18 struct bpf_prog_info_node
*info_node
)
20 __u32 prog_id
= info_node
->info_linear
->info
.id
;
21 struct bpf_prog_info_node
*node
;
22 struct rb_node
*parent
= NULL
;
25 down_write(&env
->bpf_progs
.lock
);
26 p
= &env
->bpf_progs
.infos
.rb_node
;
30 node
= rb_entry(parent
, struct bpf_prog_info_node
, rb_node
);
31 if (prog_id
< node
->info_linear
->info
.id
) {
33 } else if (prog_id
> node
->info_linear
->info
.id
) {
36 pr_debug("duplicated bpf prog info %u\n", prog_id
);
41 rb_link_node(&info_node
->rb_node
, parent
, p
);
42 rb_insert_color(&info_node
->rb_node
, &env
->bpf_progs
.infos
);
43 env
->bpf_progs
.infos_cnt
++;
45 up_write(&env
->bpf_progs
.lock
);
48 struct bpf_prog_info_node
*perf_env__find_bpf_prog_info(struct perf_env
*env
,
51 struct bpf_prog_info_node
*node
= NULL
;
54 down_read(&env
->bpf_progs
.lock
);
55 n
= env
->bpf_progs
.infos
.rb_node
;
58 node
= rb_entry(n
, struct bpf_prog_info_node
, rb_node
);
59 if (prog_id
< node
->info_linear
->info
.id
)
61 else if (prog_id
> node
->info_linear
->info
.id
)
69 up_read(&env
->bpf_progs
.lock
);
73 void perf_env__insert_btf(struct perf_env
*env
, struct btf_node
*btf_node
)
75 struct rb_node
*parent
= NULL
;
76 __u32 btf_id
= btf_node
->id
;
77 struct btf_node
*node
;
80 down_write(&env
->bpf_progs
.lock
);
81 p
= &env
->bpf_progs
.btfs
.rb_node
;
85 node
= rb_entry(parent
, struct btf_node
, rb_node
);
86 if (btf_id
< node
->id
) {
88 } else if (btf_id
> node
->id
) {
91 pr_debug("duplicated btf %u\n", btf_id
);
96 rb_link_node(&btf_node
->rb_node
, parent
, p
);
97 rb_insert_color(&btf_node
->rb_node
, &env
->bpf_progs
.btfs
);
98 env
->bpf_progs
.btfs_cnt
++;
100 up_write(&env
->bpf_progs
.lock
);
103 struct btf_node
*perf_env__find_btf(struct perf_env
*env
, __u32 btf_id
)
105 struct btf_node
*node
= NULL
;
108 down_read(&env
->bpf_progs
.lock
);
109 n
= env
->bpf_progs
.btfs
.rb_node
;
112 node
= rb_entry(n
, struct btf_node
, rb_node
);
113 if (btf_id
< node
->id
)
115 else if (btf_id
> node
->id
)
123 up_read(&env
->bpf_progs
.lock
);
127 /* purge data in bpf_progs.infos tree */
128 static void perf_env__purge_bpf(struct perf_env
*env
)
130 struct rb_root
*root
;
131 struct rb_node
*next
;
133 down_write(&env
->bpf_progs
.lock
);
135 root
= &env
->bpf_progs
.infos
;
136 next
= rb_first(root
);
139 struct bpf_prog_info_node
*node
;
141 node
= rb_entry(next
, struct bpf_prog_info_node
, rb_node
);
142 next
= rb_next(&node
->rb_node
);
143 rb_erase(&node
->rb_node
, root
);
147 env
->bpf_progs
.infos_cnt
= 0;
149 root
= &env
->bpf_progs
.btfs
;
150 next
= rb_first(root
);
153 struct btf_node
*node
;
155 node
= rb_entry(next
, struct btf_node
, rb_node
);
156 next
= rb_next(&node
->rb_node
);
157 rb_erase(&node
->rb_node
, root
);
161 env
->bpf_progs
.btfs_cnt
= 0;
163 up_write(&env
->bpf_progs
.lock
);
166 void perf_env__exit(struct perf_env
*env
)
170 perf_env__purge_bpf(env
);
171 zfree(&env
->hostname
);
172 zfree(&env
->os_release
);
173 zfree(&env
->version
);
175 zfree(&env
->cpu_desc
);
177 zfree(&env
->cmdline
);
178 zfree(&env
->cmdline_argv
);
179 zfree(&env
->sibling_cores
);
180 zfree(&env
->sibling_threads
);
181 zfree(&env
->pmu_mappings
);
183 zfree(&env
->numa_map
);
185 for (i
= 0; i
< env
->nr_numa_nodes
; i
++)
186 perf_cpu_map__put(env
->numa_nodes
[i
].map
);
187 zfree(&env
->numa_nodes
);
189 for (i
= 0; i
< env
->caches_cnt
; i
++)
190 cpu_cache_level__free(&env
->caches
[i
]);
193 for (i
= 0; i
< env
->nr_memory_nodes
; i
++)
194 zfree(&env
->memory_nodes
[i
].set
);
195 zfree(&env
->memory_nodes
);
198 void perf_env__init(struct perf_env
*env
)
200 env
->bpf_progs
.infos
= RB_ROOT
;
201 env
->bpf_progs
.btfs
= RB_ROOT
;
202 init_rwsem(&env
->bpf_progs
.lock
);
205 int perf_env__set_cmdline(struct perf_env
*env
, int argc
, const char *argv
[])
209 /* do not include NULL termination */
210 env
->cmdline_argv
= calloc(argc
, sizeof(char *));
211 if (env
->cmdline_argv
== NULL
)
215 * Must copy argv contents because it gets moved around during option
218 for (i
= 0; i
< argc
; i
++) {
219 env
->cmdline_argv
[i
] = argv
[i
];
220 if (env
->cmdline_argv
[i
] == NULL
)
224 env
->nr_cmdline
= argc
;
228 zfree(&env
->cmdline_argv
);
233 int perf_env__read_cpu_topology_map(struct perf_env
*env
)
237 if (env
->cpu
!= NULL
)
240 if (env
->nr_cpus_avail
== 0)
241 env
->nr_cpus_avail
= cpu__max_present_cpu();
243 nr_cpus
= env
->nr_cpus_avail
;
247 env
->cpu
= calloc(nr_cpus
, sizeof(env
->cpu
[0]));
248 if (env
->cpu
== NULL
)
251 for (cpu
= 0; cpu
< nr_cpus
; ++cpu
) {
252 env
->cpu
[cpu
].core_id
= cpu_map__get_core_id(cpu
);
253 env
->cpu
[cpu
].socket_id
= cpu_map__get_socket_id(cpu
);
254 env
->cpu
[cpu
].die_id
= cpu_map__get_die_id(cpu
);
257 env
->nr_cpus_avail
= nr_cpus
;
261 int perf_env__read_cpuid(struct perf_env
*env
)
264 int err
= get_cpuid(cpuid
, sizeof(cpuid
));
270 env
->cpuid
= strdup(cpuid
);
271 if (env
->cpuid
== NULL
)
276 static int perf_env__read_arch(struct perf_env
*env
)
284 env
->arch
= strdup(uts
.machine
);
286 return env
->arch
? 0 : -ENOMEM
;
289 static int perf_env__read_nr_cpus_avail(struct perf_env
*env
)
291 if (env
->nr_cpus_avail
== 0)
292 env
->nr_cpus_avail
= cpu__max_present_cpu();
294 return env
->nr_cpus_avail
? 0 : -ENOENT
;
297 const char *perf_env__raw_arch(struct perf_env
*env
)
299 return env
&& !perf_env__read_arch(env
) ? env
->arch
: "unknown";
302 int perf_env__nr_cpus_avail(struct perf_env
*env
)
304 return env
&& !perf_env__read_nr_cpus_avail(env
) ? env
->nr_cpus_avail
: 0;
307 void cpu_cache_level__free(struct cpu_cache_level
*cache
)
315 * Return architecture name in a normalized form.
316 * The conversion logic comes from the Makefile.
318 static const char *normalize_arch(char *arch
)
320 if (!strcmp(arch
, "x86_64"))
322 if (arch
[0] == 'i' && arch
[2] == '8' && arch
[3] == '6')
324 if (!strcmp(arch
, "sun4u") || !strncmp(arch
, "sparc", 5))
326 if (!strcmp(arch
, "aarch64") || !strcmp(arch
, "arm64"))
328 if (!strncmp(arch
, "arm", 3) || !strcmp(arch
, "sa110"))
330 if (!strncmp(arch
, "s390", 4))
332 if (!strncmp(arch
, "parisc", 6))
334 if (!strncmp(arch
, "powerpc", 7) || !strncmp(arch
, "ppc", 3))
336 if (!strncmp(arch
, "mips", 4))
338 if (!strncmp(arch
, "sh", 2) && isdigit(arch
[2]))
344 const char *perf_env__arch(struct perf_env
*env
)
349 if (!env
|| !env
->arch
) { /* Assume local operation */
352 arch_name
= uts
.machine
;
354 arch_name
= env
->arch
;
356 return normalize_arch(arch_name
);
360 int perf_env__numa_node(struct perf_env
*env
, int cpu
)
362 if (!env
->nr_numa_map
) {
363 struct numa_node
*nn
;
366 for (i
= 0; i
< env
->nr_numa_nodes
; i
++) {
367 nn
= &env
->numa_nodes
[i
];
368 nr
= max(nr
, perf_cpu_map__max(nn
->map
));
374 * We initialize the numa_map array to prepare
375 * it for missing cpus, which return node -1
377 env
->numa_map
= malloc(nr
* sizeof(int));
381 for (i
= 0; i
< nr
; i
++)
382 env
->numa_map
[i
] = -1;
384 env
->nr_numa_map
= nr
;
386 for (i
= 0; i
< env
->nr_numa_nodes
; i
++) {
389 nn
= &env
->numa_nodes
[i
];
390 perf_cpu_map__for_each_cpu(j
, tmp
, nn
->map
)
391 env
->numa_map
[j
] = i
;
395 return cpu
>= 0 && cpu
< env
->nr_numa_map
? env
->numa_map
[cpu
] : -1;