WIP FPC-III support
[linux/fpc-iii.git] / tools / perf / util / env.c
blob9130f6fad8d54d63a080c95bd5bb97c925e203a0
1 // SPDX-License-Identifier: GPL-2.0
2 #include "cpumap.h"
3 #include "debug.h"
4 #include "env.h"
5 #include "util/header.h"
6 #include <linux/ctype.h>
7 #include <linux/zalloc.h>
8 #include "cgroup.h"
9 #include <errno.h>
10 #include <sys/utsname.h>
11 #include <stdlib.h>
12 #include <string.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;
26 struct rb_node **p;
28 down_write(&env->bpf_progs.lock);
29 p = &env->bpf_progs.infos.rb_node;
31 while (*p != NULL) {
32 parent = *p;
33 node = rb_entry(parent, struct bpf_prog_info_node, rb_node);
34 if (prog_id < node->info_linear->info.id) {
35 p = &(*p)->rb_left;
36 } else if (prog_id > node->info_linear->info.id) {
37 p = &(*p)->rb_right;
38 } else {
39 pr_debug("duplicated bpf prog info %u\n", prog_id);
40 goto out;
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++;
47 out:
48 up_write(&env->bpf_progs.lock);
51 struct bpf_prog_info_node *perf_env__find_bpf_prog_info(struct perf_env *env,
52 __u32 prog_id)
54 struct bpf_prog_info_node *node = NULL;
55 struct rb_node *n;
57 down_read(&env->bpf_progs.lock);
58 n = env->bpf_progs.infos.rb_node;
60 while (n) {
61 node = rb_entry(n, struct bpf_prog_info_node, rb_node);
62 if (prog_id < node->info_linear->info.id)
63 n = n->rb_left;
64 else if (prog_id > node->info_linear->info.id)
65 n = n->rb_right;
66 else
67 goto out;
69 node = NULL;
71 out:
72 up_read(&env->bpf_progs.lock);
73 return node;
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;
81 struct rb_node **p;
83 down_write(&env->bpf_progs.lock);
84 p = &env->bpf_progs.btfs.rb_node;
86 while (*p != NULL) {
87 parent = *p;
88 node = rb_entry(parent, struct btf_node, rb_node);
89 if (btf_id < node->id) {
90 p = &(*p)->rb_left;
91 } else if (btf_id > node->id) {
92 p = &(*p)->rb_right;
93 } else {
94 pr_debug("duplicated btf %u\n", btf_id);
95 goto out;
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++;
102 out:
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;
109 struct rb_node *n;
111 down_read(&env->bpf_progs.lock);
112 n = env->bpf_progs.btfs.rb_node;
114 while (n) {
115 node = rb_entry(n, struct btf_node, rb_node);
116 if (btf_id < node->id)
117 n = n->rb_left;
118 else if (btf_id > node->id)
119 n = n->rb_right;
120 else
121 goto out;
123 node = NULL;
125 out:
126 up_read(&env->bpf_progs.lock);
127 return node;
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);
141 while (next) {
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);
147 free(node);
150 env->bpf_progs.infos_cnt = 0;
152 root = &env->bpf_progs.btfs;
153 next = rb_first(root);
155 while (next) {
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);
161 free(node);
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)
176 int i;
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);
183 zfree(&env->arch);
184 zfree(&env->cpu_desc);
185 zfree(&env->cpuid);
186 zfree(&env->cmdline);
187 zfree(&env->cmdline_argv);
188 zfree(&env->sibling_cores);
189 zfree(&env->sibling_threads);
190 zfree(&env->pmu_mappings);
191 zfree(&env->cpu);
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]);
200 zfree(&env->caches);
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);
213 #endif
216 int perf_env__set_cmdline(struct perf_env *env, int argc, const char *argv[])
218 int i;
220 /* do not include NULL termination */
221 env->cmdline_argv = calloc(argc, sizeof(char *));
222 if (env->cmdline_argv == NULL)
223 goto out_enomem;
226 * Must copy argv contents because it gets moved around during option
227 * parsing:
229 for (i = 0; i < argc ; i++) {
230 env->cmdline_argv[i] = argv[i];
231 if (env->cmdline_argv[i] == NULL)
232 goto out_free;
235 env->nr_cmdline = argc;
237 return 0;
238 out_free:
239 zfree(&env->cmdline_argv);
240 out_enomem:
241 return -ENOMEM;
244 int perf_env__read_cpu_topology_map(struct perf_env *env)
246 int cpu, nr_cpus;
248 if (env->cpu != NULL)
249 return 0;
251 if (env->nr_cpus_avail == 0)
252 env->nr_cpus_avail = cpu__max_present_cpu();
254 nr_cpus = env->nr_cpus_avail;
255 if (nr_cpus == -1)
256 return -EINVAL;
258 env->cpu = calloc(nr_cpus, sizeof(env->cpu[0]));
259 if (env->cpu == NULL)
260 return -ENOMEM;
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;
269 return 0;
272 int perf_env__read_cpuid(struct perf_env *env)
274 char cpuid[128];
275 int err = get_cpuid(cpuid, sizeof(cpuid));
277 if (err)
278 return err;
280 free(env->cpuid);
281 env->cpuid = strdup(cpuid);
282 if (env->cpuid == NULL)
283 return ENOMEM;
284 return 0;
287 static int perf_env__read_arch(struct perf_env *env)
289 struct utsname uts;
291 if (env->arch)
292 return 0;
294 if (!uname(&uts))
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)
320 zfree(&cache->type);
321 zfree(&cache->map);
322 zfree(&cache->size);
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"))
332 return "x86";
333 if (arch[0] == 'i' && arch[2] == '8' && arch[3] == '6')
334 return "x86";
335 if (!strcmp(arch, "sun4u") || !strncmp(arch, "sparc", 5))
336 return "sparc";
337 if (!strcmp(arch, "aarch64") || !strcmp(arch, "arm64"))
338 return "arm64";
339 if (!strncmp(arch, "arm", 3) || !strcmp(arch, "sa110"))
340 return "arm";
341 if (!strncmp(arch, "s390", 4))
342 return "s390";
343 if (!strncmp(arch, "parisc", 6))
344 return "parisc";
345 if (!strncmp(arch, "powerpc", 7) || !strncmp(arch, "ppc", 3))
346 return "powerpc";
347 if (!strncmp(arch, "mips", 4))
348 return "mips";
349 if (!strncmp(arch, "sh", 2) && isdigit(arch[2]))
350 return "sh";
352 return arch;
355 const char *perf_env__arch(struct perf_env *env)
357 char *arch_name;
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)
362 return NULL;
363 arch_name = uts.machine;
364 } else
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;
375 int i, nr = 0;
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));
382 nr++;
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));
389 if (!env->numa_map)
390 return -1;
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++) {
398 int tmp, j;
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;