]>
git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blob - tools/perf/util/env.c
1 // SPDX-License-Identifier: GPL-2.0
4 #include <linux/ctype.h>
5 #include <linux/zalloc.h>
8 #include <sys/utsname.h>
9 #include <bpf/libbpf.h>
12 struct perf_env perf_env
;
14 void perf_env__insert_bpf_prog_info(struct perf_env
*env
,
15 struct bpf_prog_info_node
*info_node
)
17 __u32 prog_id
= info_node
->info_linear
->info
.id
;
18 struct bpf_prog_info_node
*node
;
19 struct rb_node
*parent
= NULL
;
22 down_write(&env
->bpf_progs
.lock
);
23 p
= &env
->bpf_progs
.infos
.rb_node
;
27 node
= rb_entry(parent
, struct bpf_prog_info_node
, rb_node
);
28 if (prog_id
< node
->info_linear
->info
.id
) {
30 } else if (prog_id
> node
->info_linear
->info
.id
) {
33 pr_debug("duplicated bpf prog info %u\n", prog_id
);
38 rb_link_node(&info_node
->rb_node
, parent
, p
);
39 rb_insert_color(&info_node
->rb_node
, &env
->bpf_progs
.infos
);
40 env
->bpf_progs
.infos_cnt
++;
42 up_write(&env
->bpf_progs
.lock
);
45 struct bpf_prog_info_node
*perf_env__find_bpf_prog_info(struct perf_env
*env
,
48 struct bpf_prog_info_node
*node
= NULL
;
51 down_read(&env
->bpf_progs
.lock
);
52 n
= env
->bpf_progs
.infos
.rb_node
;
55 node
= rb_entry(n
, struct bpf_prog_info_node
, rb_node
);
56 if (prog_id
< node
->info_linear
->info
.id
)
58 else if (prog_id
> node
->info_linear
->info
.id
)
66 up_read(&env
->bpf_progs
.lock
);
70 void perf_env__insert_btf(struct perf_env
*env
, struct btf_node
*btf_node
)
72 struct rb_node
*parent
= NULL
;
73 __u32 btf_id
= btf_node
->id
;
74 struct btf_node
*node
;
77 down_write(&env
->bpf_progs
.lock
);
78 p
= &env
->bpf_progs
.btfs
.rb_node
;
82 node
= rb_entry(parent
, struct btf_node
, rb_node
);
83 if (btf_id
< node
->id
) {
85 } else if (btf_id
> node
->id
) {
88 pr_debug("duplicated btf %u\n", btf_id
);
93 rb_link_node(&btf_node
->rb_node
, parent
, p
);
94 rb_insert_color(&btf_node
->rb_node
, &env
->bpf_progs
.btfs
);
95 env
->bpf_progs
.btfs_cnt
++;
97 up_write(&env
->bpf_progs
.lock
);
100 struct btf_node
*perf_env__find_btf(struct perf_env
*env
, __u32 btf_id
)
102 struct btf_node
*node
= NULL
;
105 down_read(&env
->bpf_progs
.lock
);
106 n
= env
->bpf_progs
.btfs
.rb_node
;
109 node
= rb_entry(n
, struct btf_node
, rb_node
);
110 if (btf_id
< node
->id
)
112 else if (btf_id
> node
->id
)
120 up_read(&env
->bpf_progs
.lock
);
124 /* purge data in bpf_progs.infos tree */
125 static void perf_env__purge_bpf(struct perf_env
*env
)
127 struct rb_root
*root
;
128 struct rb_node
*next
;
130 down_write(&env
->bpf_progs
.lock
);
132 root
= &env
->bpf_progs
.infos
;
133 next
= rb_first(root
);
136 struct bpf_prog_info_node
*node
;
138 node
= rb_entry(next
, struct bpf_prog_info_node
, rb_node
);
139 next
= rb_next(&node
->rb_node
);
140 rb_erase(&node
->rb_node
, root
);
144 env
->bpf_progs
.infos_cnt
= 0;
146 root
= &env
->bpf_progs
.btfs
;
147 next
= rb_first(root
);
150 struct btf_node
*node
;
152 node
= rb_entry(next
, struct btf_node
, rb_node
);
153 next
= rb_next(&node
->rb_node
);
154 rb_erase(&node
->rb_node
, root
);
158 env
->bpf_progs
.btfs_cnt
= 0;
160 up_write(&env
->bpf_progs
.lock
);
163 void perf_env__exit(struct perf_env
*env
)
167 perf_env__purge_bpf(env
);
168 zfree(&env
->hostname
);
169 zfree(&env
->os_release
);
170 zfree(&env
->version
);
172 zfree(&env
->cpu_desc
);
174 zfree(&env
->cmdline
);
175 zfree(&env
->cmdline_argv
);
176 zfree(&env
->sibling_cores
);
177 zfree(&env
->sibling_threads
);
178 zfree(&env
->pmu_mappings
);
181 for (i
= 0; i
< env
->nr_numa_nodes
; i
++)
182 cpu_map__put(env
->numa_nodes
[i
].map
);
183 zfree(&env
->numa_nodes
);
185 for (i
= 0; i
< env
->caches_cnt
; i
++)
186 cpu_cache_level__free(&env
->caches
[i
]);
189 for (i
= 0; i
< env
->nr_memory_nodes
; i
++)
190 zfree(&env
->memory_nodes
[i
].set
);
191 zfree(&env
->memory_nodes
);
194 void perf_env__init(struct perf_env
*env
)
196 env
->bpf_progs
.infos
= RB_ROOT
;
197 env
->bpf_progs
.btfs
= RB_ROOT
;
198 init_rwsem(&env
->bpf_progs
.lock
);
201 int perf_env__set_cmdline(struct perf_env
*env
, int argc
, const char *argv
[])
205 /* do not include NULL termination */
206 env
->cmdline_argv
= calloc(argc
, sizeof(char *));
207 if (env
->cmdline_argv
== NULL
)
211 * Must copy argv contents because it gets moved around during option
214 for (i
= 0; i
< argc
; i
++) {
215 env
->cmdline_argv
[i
] = argv
[i
];
216 if (env
->cmdline_argv
[i
] == NULL
)
220 env
->nr_cmdline
= argc
;
224 zfree(&env
->cmdline_argv
);
229 int perf_env__read_cpu_topology_map(struct perf_env
*env
)
233 if (env
->cpu
!= NULL
)
236 if (env
->nr_cpus_avail
== 0)
237 env
->nr_cpus_avail
= cpu__max_present_cpu();
239 nr_cpus
= env
->nr_cpus_avail
;
243 env
->cpu
= calloc(nr_cpus
, sizeof(env
->cpu
[0]));
244 if (env
->cpu
== NULL
)
247 for (cpu
= 0; cpu
< nr_cpus
; ++cpu
) {
248 env
->cpu
[cpu
].core_id
= cpu_map__get_core_id(cpu
);
249 env
->cpu
[cpu
].socket_id
= cpu_map__get_socket_id(cpu
);
250 env
->cpu
[cpu
].die_id
= cpu_map__get_die_id(cpu
);
253 env
->nr_cpus_avail
= nr_cpus
;
257 static int perf_env__read_arch(struct perf_env
*env
)
265 env
->arch
= strdup(uts
.machine
);
267 return env
->arch
? 0 : -ENOMEM
;
270 static int perf_env__read_nr_cpus_avail(struct perf_env
*env
)
272 if (env
->nr_cpus_avail
== 0)
273 env
->nr_cpus_avail
= cpu__max_present_cpu();
275 return env
->nr_cpus_avail
? 0 : -ENOENT
;
278 const char *perf_env__raw_arch(struct perf_env
*env
)
280 return env
&& !perf_env__read_arch(env
) ? env
->arch
: "unknown";
283 int perf_env__nr_cpus_avail(struct perf_env
*env
)
285 return env
&& !perf_env__read_nr_cpus_avail(env
) ? env
->nr_cpus_avail
: 0;
288 void cpu_cache_level__free(struct cpu_cache_level
*cache
)
296 * Return architecture name in a normalized form.
297 * The conversion logic comes from the Makefile.
299 static const char *normalize_arch(char *arch
)
301 if (!strcmp(arch
, "x86_64"))
303 if (arch
[0] == 'i' && arch
[2] == '8' && arch
[3] == '6')
305 if (!strcmp(arch
, "sun4u") || !strncmp(arch
, "sparc", 5))
307 if (!strcmp(arch
, "aarch64") || !strcmp(arch
, "arm64"))
309 if (!strncmp(arch
, "arm", 3) || !strcmp(arch
, "sa110"))
311 if (!strncmp(arch
, "s390", 4))
313 if (!strncmp(arch
, "parisc", 6))
315 if (!strncmp(arch
, "powerpc", 7) || !strncmp(arch
, "ppc", 3))
317 if (!strncmp(arch
, "mips", 4))
319 if (!strncmp(arch
, "sh", 2) && isdigit(arch
[2]))
325 const char *perf_env__arch(struct perf_env
*env
)
330 if (!env
|| !env
->arch
) { /* Assume local operation */
333 arch_name
= uts
.machine
;
335 arch_name
= env
->arch
;
337 return normalize_arch(arch_name
);