Linux 5.1.15
[linux/fpc-iii.git] / tools / perf / util / env.c
blob6a3eaf7d9353c5251f8c0d2a7f7ab6b6696ace0d
1 // SPDX-License-Identifier: GPL-2.0
2 #include "cpumap.h"
3 #include "env.h"
4 #include "sane_ctype.h"
5 #include "util.h"
6 #include "bpf-event.h"
7 #include <errno.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;
19 struct rb_node **p;
21 down_write(&env->bpf_progs.lock);
22 p = &env->bpf_progs.infos.rb_node;
24 while (*p != NULL) {
25 parent = *p;
26 node = rb_entry(parent, struct bpf_prog_info_node, rb_node);
27 if (prog_id < node->info_linear->info.id) {
28 p = &(*p)->rb_left;
29 } else if (prog_id > node->info_linear->info.id) {
30 p = &(*p)->rb_right;
31 } else {
32 pr_debug("duplicated bpf prog info %u\n", prog_id);
33 goto out;
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++;
40 out:
41 up_write(&env->bpf_progs.lock);
44 struct bpf_prog_info_node *perf_env__find_bpf_prog_info(struct perf_env *env,
45 __u32 prog_id)
47 struct bpf_prog_info_node *node = NULL;
48 struct rb_node *n;
50 down_read(&env->bpf_progs.lock);
51 n = env->bpf_progs.infos.rb_node;
53 while (n) {
54 node = rb_entry(n, struct bpf_prog_info_node, rb_node);
55 if (prog_id < node->info_linear->info.id)
56 n = n->rb_left;
57 else if (prog_id > node->info_linear->info.id)
58 n = n->rb_right;
59 else
60 goto out;
62 node = NULL;
64 out:
65 up_read(&env->bpf_progs.lock);
66 return node;
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;
74 struct rb_node **p;
76 down_write(&env->bpf_progs.lock);
77 p = &env->bpf_progs.btfs.rb_node;
79 while (*p != NULL) {
80 parent = *p;
81 node = rb_entry(parent, struct btf_node, rb_node);
82 if (btf_id < node->id) {
83 p = &(*p)->rb_left;
84 } else if (btf_id > node->id) {
85 p = &(*p)->rb_right;
86 } else {
87 pr_debug("duplicated btf %u\n", btf_id);
88 goto out;
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++;
95 out:
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;
102 struct rb_node *n;
104 down_read(&env->bpf_progs.lock);
105 n = env->bpf_progs.btfs.rb_node;
107 while (n) {
108 node = rb_entry(n, struct btf_node, rb_node);
109 if (btf_id < node->id)
110 n = n->rb_left;
111 else if (btf_id > node->id)
112 n = n->rb_right;
113 else
114 goto out;
116 node = NULL;
118 out:
119 up_read(&env->bpf_progs.lock);
120 return node;
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);
134 while (next) {
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);
140 free(node);
143 env->bpf_progs.infos_cnt = 0;
145 root = &env->bpf_progs.btfs;
146 next = rb_first(root);
148 while (next) {
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);
154 free(node);
157 env->bpf_progs.btfs_cnt = 0;
159 up_write(&env->bpf_progs.lock);
162 void perf_env__exit(struct perf_env *env)
164 int i;
166 perf_env__purge_bpf(env);
167 zfree(&env->hostname);
168 zfree(&env->os_release);
169 zfree(&env->version);
170 zfree(&env->arch);
171 zfree(&env->cpu_desc);
172 zfree(&env->cpuid);
173 zfree(&env->cmdline);
174 zfree(&env->cmdline_argv);
175 zfree(&env->sibling_cores);
176 zfree(&env->sibling_threads);
177 zfree(&env->pmu_mappings);
178 zfree(&env->cpu);
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]);
186 zfree(&env->caches);
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[])
202 int i;
204 /* do not include NULL termination */
205 env->cmdline_argv = calloc(argc, sizeof(char *));
206 if (env->cmdline_argv == NULL)
207 goto out_enomem;
210 * Must copy argv contents because it gets moved around during option
211 * parsing:
213 for (i = 0; i < argc ; i++) {
214 env->cmdline_argv[i] = argv[i];
215 if (env->cmdline_argv[i] == NULL)
216 goto out_free;
219 env->nr_cmdline = argc;
221 return 0;
222 out_free:
223 zfree(&env->cmdline_argv);
224 out_enomem:
225 return -ENOMEM;
228 int perf_env__read_cpu_topology_map(struct perf_env *env)
230 int cpu, nr_cpus;
232 if (env->cpu != NULL)
233 return 0;
235 if (env->nr_cpus_avail == 0)
236 env->nr_cpus_avail = cpu__max_present_cpu();
238 nr_cpus = env->nr_cpus_avail;
239 if (nr_cpus == -1)
240 return -EINVAL;
242 env->cpu = calloc(nr_cpus, sizeof(env->cpu[0]));
243 if (env->cpu == NULL)
244 return -ENOMEM;
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;
252 return 0;
255 static int perf_env__read_arch(struct perf_env *env)
257 struct utsname uts;
259 if (env->arch)
260 return 0;
262 if (!uname(&uts))
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)
288 free(cache->type);
289 free(cache->map);
290 free(cache->size);
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"))
300 return "x86";
301 if (arch[0] == 'i' && arch[2] == '8' && arch[3] == '6')
302 return "x86";
303 if (!strcmp(arch, "sun4u") || !strncmp(arch, "sparc", 5))
304 return "sparc";
305 if (!strcmp(arch, "aarch64") || !strcmp(arch, "arm64"))
306 return "arm64";
307 if (!strncmp(arch, "arm", 3) || !strcmp(arch, "sa110"))
308 return "arm";
309 if (!strncmp(arch, "s390", 4))
310 return "s390";
311 if (!strncmp(arch, "parisc", 6))
312 return "parisc";
313 if (!strncmp(arch, "powerpc", 7) || !strncmp(arch, "ppc", 3))
314 return "powerpc";
315 if (!strncmp(arch, "mips", 4))
316 return "mips";
317 if (!strncmp(arch, "sh", 2) && isdigit(arch[2]))
318 return "sh";
320 return arch;
323 const char *perf_env__arch(struct perf_env *env)
325 struct utsname uts;
326 char *arch_name;
328 if (!env || !env->arch) { /* Assume local operation */
329 if (uname(&uts) < 0)
330 return NULL;
331 arch_name = uts.machine;
332 } else
333 arch_name = env->arch;
335 return normalize_arch(arch_name);