1 // SPDX-License-Identifier: GPL-2.0
4 #include "sane_ctype.h"
8 #include <sys/utsname.h>
9 #include <bpf/libbpf.h>
11 struct perf_env perf_env
;
13 void perf_env__insert_bpf_prog_info(struct perf_env
*env
,
14 struct bpf_prog_info_node
*info_node
)
16 __u32 prog_id
= info_node
->info_linear
->info
.id
;
17 struct bpf_prog_info_node
*node
;
18 struct rb_node
*parent
= NULL
;
21 down_write(&env
->bpf_progs
.lock
);
22 p
= &env
->bpf_progs
.infos
.rb_node
;
26 node
= rb_entry(parent
, struct bpf_prog_info_node
, rb_node
);
27 if (prog_id
< node
->info_linear
->info
.id
) {
29 } else if (prog_id
> node
->info_linear
->info
.id
) {
32 pr_debug("duplicated bpf prog info %u\n", prog_id
);
37 rb_link_node(&info_node
->rb_node
, parent
, p
);
38 rb_insert_color(&info_node
->rb_node
, &env
->bpf_progs
.infos
);
39 env
->bpf_progs
.infos_cnt
++;
41 up_write(&env
->bpf_progs
.lock
);
44 struct bpf_prog_info_node
*perf_env__find_bpf_prog_info(struct perf_env
*env
,
47 struct bpf_prog_info_node
*node
= NULL
;
50 down_read(&env
->bpf_progs
.lock
);
51 n
= env
->bpf_progs
.infos
.rb_node
;
54 node
= rb_entry(n
, struct bpf_prog_info_node
, rb_node
);
55 if (prog_id
< node
->info_linear
->info
.id
)
57 else if (prog_id
> node
->info_linear
->info
.id
)
65 up_read(&env
->bpf_progs
.lock
);
69 void perf_env__insert_btf(struct perf_env
*env
, struct btf_node
*btf_node
)
71 struct rb_node
*parent
= NULL
;
72 __u32 btf_id
= btf_node
->id
;
73 struct btf_node
*node
;
76 down_write(&env
->bpf_progs
.lock
);
77 p
= &env
->bpf_progs
.btfs
.rb_node
;
81 node
= rb_entry(parent
, struct btf_node
, rb_node
);
82 if (btf_id
< node
->id
) {
84 } else if (btf_id
> node
->id
) {
87 pr_debug("duplicated btf %u\n", btf_id
);
92 rb_link_node(&btf_node
->rb_node
, parent
, p
);
93 rb_insert_color(&btf_node
->rb_node
, &env
->bpf_progs
.btfs
);
94 env
->bpf_progs
.btfs_cnt
++;
96 up_write(&env
->bpf_progs
.lock
);
99 struct btf_node
*perf_env__find_btf(struct perf_env
*env
, __u32 btf_id
)
101 struct btf_node
*node
= NULL
;
104 down_read(&env
->bpf_progs
.lock
);
105 n
= env
->bpf_progs
.btfs
.rb_node
;
108 node
= rb_entry(n
, struct btf_node
, rb_node
);
109 if (btf_id
< node
->id
)
111 else if (btf_id
> node
->id
)
119 up_read(&env
->bpf_progs
.lock
);
123 /* purge data in bpf_progs.infos tree */
124 static void perf_env__purge_bpf(struct perf_env
*env
)
126 struct rb_root
*root
;
127 struct rb_node
*next
;
129 down_write(&env
->bpf_progs
.lock
);
131 root
= &env
->bpf_progs
.infos
;
132 next
= rb_first(root
);
135 struct bpf_prog_info_node
*node
;
137 node
= rb_entry(next
, struct bpf_prog_info_node
, rb_node
);
138 next
= rb_next(&node
->rb_node
);
139 rb_erase(&node
->rb_node
, root
);
143 env
->bpf_progs
.infos_cnt
= 0;
145 root
= &env
->bpf_progs
.btfs
;
146 next
= rb_first(root
);
149 struct btf_node
*node
;
151 node
= rb_entry(next
, struct btf_node
, rb_node
);
152 next
= rb_next(&node
->rb_node
);
153 rb_erase(&node
->rb_node
, root
);
157 env
->bpf_progs
.btfs_cnt
= 0;
159 up_write(&env
->bpf_progs
.lock
);
162 void perf_env__exit(struct perf_env
*env
)
166 perf_env__purge_bpf(env
);
167 zfree(&env
->hostname
);
168 zfree(&env
->os_release
);
169 zfree(&env
->version
);
171 zfree(&env
->cpu_desc
);
173 zfree(&env
->cmdline
);
174 zfree(&env
->cmdline_argv
);
175 zfree(&env
->sibling_cores
);
176 zfree(&env
->sibling_threads
);
177 zfree(&env
->pmu_mappings
);
180 for (i
= 0; i
< env
->nr_numa_nodes
; i
++)
181 cpu_map__put(env
->numa_nodes
[i
].map
);
182 zfree(&env
->numa_nodes
);
184 for (i
= 0; i
< env
->caches_cnt
; i
++)
185 cpu_cache_level__free(&env
->caches
[i
]);
188 for (i
= 0; i
< env
->nr_memory_nodes
; i
++)
189 free(env
->memory_nodes
[i
].set
);
190 zfree(&env
->memory_nodes
);
193 void perf_env__init(struct perf_env
*env
)
195 env
->bpf_progs
.infos
= RB_ROOT
;
196 env
->bpf_progs
.btfs
= RB_ROOT
;
197 init_rwsem(&env
->bpf_progs
.lock
);
200 int perf_env__set_cmdline(struct perf_env
*env
, int argc
, const char *argv
[])
204 /* do not include NULL termination */
205 env
->cmdline_argv
= calloc(argc
, sizeof(char *));
206 if (env
->cmdline_argv
== NULL
)
210 * Must copy argv contents because it gets moved around during option
213 for (i
= 0; i
< argc
; i
++) {
214 env
->cmdline_argv
[i
] = argv
[i
];
215 if (env
->cmdline_argv
[i
] == NULL
)
219 env
->nr_cmdline
= argc
;
223 zfree(&env
->cmdline_argv
);
228 int perf_env__read_cpu_topology_map(struct perf_env
*env
)
232 if (env
->cpu
!= NULL
)
235 if (env
->nr_cpus_avail
== 0)
236 env
->nr_cpus_avail
= cpu__max_present_cpu();
238 nr_cpus
= env
->nr_cpus_avail
;
242 env
->cpu
= calloc(nr_cpus
, sizeof(env
->cpu
[0]));
243 if (env
->cpu
== NULL
)
246 for (cpu
= 0; cpu
< nr_cpus
; ++cpu
) {
247 env
->cpu
[cpu
].core_id
= cpu_map__get_core_id(cpu
);
248 env
->cpu
[cpu
].socket_id
= cpu_map__get_socket_id(cpu
);
251 env
->nr_cpus_avail
= nr_cpus
;
255 static int perf_env__read_arch(struct perf_env
*env
)
263 env
->arch
= strdup(uts
.machine
);
265 return env
->arch
? 0 : -ENOMEM
;
268 static int perf_env__read_nr_cpus_avail(struct perf_env
*env
)
270 if (env
->nr_cpus_avail
== 0)
271 env
->nr_cpus_avail
= cpu__max_present_cpu();
273 return env
->nr_cpus_avail
? 0 : -ENOENT
;
276 const char *perf_env__raw_arch(struct perf_env
*env
)
278 return env
&& !perf_env__read_arch(env
) ? env
->arch
: "unknown";
281 int perf_env__nr_cpus_avail(struct perf_env
*env
)
283 return env
&& !perf_env__read_nr_cpus_avail(env
) ? env
->nr_cpus_avail
: 0;
286 void cpu_cache_level__free(struct cpu_cache_level
*cache
)
294 * Return architecture name in a normalized form.
295 * The conversion logic comes from the Makefile.
297 static const char *normalize_arch(char *arch
)
299 if (!strcmp(arch
, "x86_64"))
301 if (arch
[0] == 'i' && arch
[2] == '8' && arch
[3] == '6')
303 if (!strcmp(arch
, "sun4u") || !strncmp(arch
, "sparc", 5))
305 if (!strcmp(arch
, "aarch64") || !strcmp(arch
, "arm64"))
307 if (!strncmp(arch
, "arm", 3) || !strcmp(arch
, "sa110"))
309 if (!strncmp(arch
, "s390", 4))
311 if (!strncmp(arch
, "parisc", 6))
313 if (!strncmp(arch
, "powerpc", 7) || !strncmp(arch
, "ppc", 3))
315 if (!strncmp(arch
, "mips", 4))
317 if (!strncmp(arch
, "sh", 2) && isdigit(arch
[2]))
323 const char *perf_env__arch(struct perf_env
*env
)
328 if (!env
|| !env
->arch
) { /* Assume local operation */
331 arch_name
= uts
.machine
;
333 arch_name
= env
->arch
;
335 return normalize_arch(arch_name
);