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>
14 struct perf_env perf_env
;
16 #ifdef HAVE_LIBBPF_SUPPORT
17 #include "bpf-event.h"
18 #include <bpf/libbpf.h>
20 void perf_env__insert_bpf_prog_info(struct perf_env
*env
,
21 struct bpf_prog_info_node
*info_node
)
23 __u32 prog_id
= info_node
->info_linear
->info
.id
;
24 struct bpf_prog_info_node
*node
;
25 struct rb_node
*parent
= NULL
;
28 down_write(&env
->bpf_progs
.lock
);
29 p
= &env
->bpf_progs
.infos
.rb_node
;
33 node
= rb_entry(parent
, struct bpf_prog_info_node
, rb_node
);
34 if (prog_id
< node
->info_linear
->info
.id
) {
36 } else if (prog_id
> node
->info_linear
->info
.id
) {
39 pr_debug("duplicated bpf prog info %u\n", prog_id
);
44 rb_link_node(&info_node
->rb_node
, parent
, p
);
45 rb_insert_color(&info_node
->rb_node
, &env
->bpf_progs
.infos
);
46 env
->bpf_progs
.infos_cnt
++;
48 up_write(&env
->bpf_progs
.lock
);
51 struct bpf_prog_info_node
*perf_env__find_bpf_prog_info(struct perf_env
*env
,
54 struct bpf_prog_info_node
*node
= NULL
;
57 down_read(&env
->bpf_progs
.lock
);
58 n
= env
->bpf_progs
.infos
.rb_node
;
61 node
= rb_entry(n
, struct bpf_prog_info_node
, rb_node
);
62 if (prog_id
< node
->info_linear
->info
.id
)
64 else if (prog_id
> node
->info_linear
->info
.id
)
72 up_read(&env
->bpf_progs
.lock
);
76 void perf_env__insert_btf(struct perf_env
*env
, struct btf_node
*btf_node
)
78 struct rb_node
*parent
= NULL
;
79 __u32 btf_id
= btf_node
->id
;
80 struct btf_node
*node
;
83 down_write(&env
->bpf_progs
.lock
);
84 p
= &env
->bpf_progs
.btfs
.rb_node
;
88 node
= rb_entry(parent
, struct btf_node
, rb_node
);
89 if (btf_id
< node
->id
) {
91 } else if (btf_id
> node
->id
) {
94 pr_debug("duplicated btf %u\n", btf_id
);
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
++;
103 up_write(&env
->bpf_progs
.lock
);
106 struct btf_node
*perf_env__find_btf(struct perf_env
*env
, __u32 btf_id
)
108 struct btf_node
*node
= NULL
;
111 down_read(&env
->bpf_progs
.lock
);
112 n
= env
->bpf_progs
.btfs
.rb_node
;
115 node
= rb_entry(n
, struct btf_node
, rb_node
);
116 if (btf_id
< node
->id
)
118 else if (btf_id
> node
->id
)
126 up_read(&env
->bpf_progs
.lock
);
130 /* purge data in bpf_progs.infos tree */
131 static void perf_env__purge_bpf(struct perf_env
*env
)
133 struct rb_root
*root
;
134 struct rb_node
*next
;
136 down_write(&env
->bpf_progs
.lock
);
138 root
= &env
->bpf_progs
.infos
;
139 next
= rb_first(root
);
142 struct bpf_prog_info_node
*node
;
144 node
= rb_entry(next
, struct bpf_prog_info_node
, rb_node
);
145 next
= rb_next(&node
->rb_node
);
146 rb_erase(&node
->rb_node
, root
);
150 env
->bpf_progs
.infos_cnt
= 0;
152 root
= &env
->bpf_progs
.btfs
;
153 next
= rb_first(root
);
156 struct btf_node
*node
;
158 node
= rb_entry(next
, struct btf_node
, rb_node
);
159 next
= rb_next(&node
->rb_node
);
160 rb_erase(&node
->rb_node
, root
);
164 env
->bpf_progs
.btfs_cnt
= 0;
166 up_write(&env
->bpf_progs
.lock
);
168 #else // HAVE_LIBBPF_SUPPORT
169 static void perf_env__purge_bpf(struct perf_env
*env __maybe_unused
)
172 #endif // HAVE_LIBBPF_SUPPORT
174 void perf_env__exit(struct perf_env
*env
)
178 perf_env__purge_bpf(env
);
179 perf_env__purge_cgroups(env
);
180 zfree(&env
->hostname
);
181 zfree(&env
->os_release
);
182 zfree(&env
->version
);
184 zfree(&env
->cpu_desc
);
186 zfree(&env
->cmdline
);
187 zfree(&env
->cmdline_argv
);
188 zfree(&env
->sibling_cores
);
189 zfree(&env
->sibling_threads
);
190 zfree(&env
->pmu_mappings
);
192 zfree(&env
->numa_map
);
194 for (i
= 0; i
< env
->nr_numa_nodes
; i
++)
195 perf_cpu_map__put(env
->numa_nodes
[i
].map
);
196 zfree(&env
->numa_nodes
);
198 for (i
= 0; i
< env
->caches_cnt
; i
++)
199 cpu_cache_level__free(&env
->caches
[i
]);
202 for (i
= 0; i
< env
->nr_memory_nodes
; i
++)
203 zfree(&env
->memory_nodes
[i
].set
);
204 zfree(&env
->memory_nodes
);
207 void perf_env__init(struct perf_env
*env __maybe_unused
)
209 #ifdef HAVE_LIBBPF_SUPPORT
210 env
->bpf_progs
.infos
= RB_ROOT
;
211 env
->bpf_progs
.btfs
= RB_ROOT
;
212 init_rwsem(&env
->bpf_progs
.lock
);
216 int perf_env__set_cmdline(struct perf_env
*env
, int argc
, const char *argv
[])
220 /* do not include NULL termination */
221 env
->cmdline_argv
= calloc(argc
, sizeof(char *));
222 if (env
->cmdline_argv
== NULL
)
226 * Must copy argv contents because it gets moved around during option
229 for (i
= 0; i
< argc
; i
++) {
230 env
->cmdline_argv
[i
] = argv
[i
];
231 if (env
->cmdline_argv
[i
] == NULL
)
235 env
->nr_cmdline
= argc
;
239 zfree(&env
->cmdline_argv
);
244 int perf_env__read_cpu_topology_map(struct perf_env
*env
)
248 if (env
->cpu
!= NULL
)
251 if (env
->nr_cpus_avail
== 0)
252 env
->nr_cpus_avail
= cpu__max_present_cpu();
254 nr_cpus
= env
->nr_cpus_avail
;
258 env
->cpu
= calloc(nr_cpus
, sizeof(env
->cpu
[0]));
259 if (env
->cpu
== NULL
)
262 for (cpu
= 0; cpu
< nr_cpus
; ++cpu
) {
263 env
->cpu
[cpu
].core_id
= cpu_map__get_core_id(cpu
);
264 env
->cpu
[cpu
].socket_id
= cpu_map__get_socket_id(cpu
);
265 env
->cpu
[cpu
].die_id
= cpu_map__get_die_id(cpu
);
268 env
->nr_cpus_avail
= nr_cpus
;
272 int perf_env__read_cpuid(struct perf_env
*env
)
275 int err
= get_cpuid(cpuid
, sizeof(cpuid
));
281 env
->cpuid
= strdup(cpuid
);
282 if (env
->cpuid
== NULL
)
287 static int perf_env__read_arch(struct perf_env
*env
)
295 env
->arch
= strdup(uts
.machine
);
297 return env
->arch
? 0 : -ENOMEM
;
300 static int perf_env__read_nr_cpus_avail(struct perf_env
*env
)
302 if (env
->nr_cpus_avail
== 0)
303 env
->nr_cpus_avail
= cpu__max_present_cpu();
305 return env
->nr_cpus_avail
? 0 : -ENOENT
;
308 const char *perf_env__raw_arch(struct perf_env
*env
)
310 return env
&& !perf_env__read_arch(env
) ? env
->arch
: "unknown";
313 int perf_env__nr_cpus_avail(struct perf_env
*env
)
315 return env
&& !perf_env__read_nr_cpus_avail(env
) ? env
->nr_cpus_avail
: 0;
318 void cpu_cache_level__free(struct cpu_cache_level
*cache
)
326 * Return architecture name in a normalized form.
327 * The conversion logic comes from the Makefile.
329 static const char *normalize_arch(char *arch
)
331 if (!strcmp(arch
, "x86_64"))
333 if (arch
[0] == 'i' && arch
[2] == '8' && arch
[3] == '6')
335 if (!strcmp(arch
, "sun4u") || !strncmp(arch
, "sparc", 5))
337 if (!strcmp(arch
, "aarch64") || !strcmp(arch
, "arm64"))
339 if (!strncmp(arch
, "arm", 3) || !strcmp(arch
, "sa110"))
341 if (!strncmp(arch
, "s390", 4))
343 if (!strncmp(arch
, "parisc", 6))
345 if (!strncmp(arch
, "powerpc", 7) || !strncmp(arch
, "ppc", 3))
347 if (!strncmp(arch
, "mips", 4))
349 if (!strncmp(arch
, "sh", 2) && isdigit(arch
[2]))
355 const char *perf_env__arch(struct perf_env
*env
)
359 if (!env
|| !env
->arch
) { /* Assume local operation */
360 static struct utsname uts
= { .machine
[0] = '\0', };
361 if (uts
.machine
[0] == '\0' && uname(&uts
) < 0)
363 arch_name
= uts
.machine
;
365 arch_name
= env
->arch
;
367 return normalize_arch(arch_name
);
371 int perf_env__numa_node(struct perf_env
*env
, int cpu
)
373 if (!env
->nr_numa_map
) {
374 struct numa_node
*nn
;
377 for (i
= 0; i
< env
->nr_numa_nodes
; i
++) {
378 nn
= &env
->numa_nodes
[i
];
379 nr
= max(nr
, perf_cpu_map__max(nn
->map
));
385 * We initialize the numa_map array to prepare
386 * it for missing cpus, which return node -1
388 env
->numa_map
= malloc(nr
* sizeof(int));
392 for (i
= 0; i
< nr
; i
++)
393 env
->numa_map
[i
] = -1;
395 env
->nr_numa_map
= nr
;
397 for (i
= 0; i
< env
->nr_numa_nodes
; i
++) {
400 nn
= &env
->numa_nodes
[i
];
401 perf_cpu_map__for_each_cpu(j
, tmp
, nn
->map
)
402 env
->numa_map
[j
] = i
;
406 return cpu
>= 0 && cpu
< env
->nr_numa_map
? env
->numa_map
[cpu
] : -1;