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> 10 11 struct perf_env perf_env; 12 13 void perf_env__insert_bpf_prog_info(struct perf_env *env, 14 struct bpf_prog_info_node *info_node) 15 { 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; 20 21 down_write(&env->bpf_progs.lock); 22 p = &env->bpf_progs.infos.rb_node; 23 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; 34 } 35 } 36 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); 42 } 43 44 struct bpf_prog_info_node *perf_env__find_bpf_prog_info(struct perf_env *env, 45 __u32 prog_id) 46 { 47 struct bpf_prog_info_node *node = NULL; 48 struct rb_node *n; 49 50 down_read(&env->bpf_progs.lock); 51 n = env->bpf_progs.infos.rb_node; 52 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; 61 } 62 node = NULL; 63 64 out: 65 up_read(&env->bpf_progs.lock); 66 return node; 67 } 68 69 void perf_env__insert_btf(struct perf_env *env, struct btf_node *btf_node) 70 { 71 struct rb_node *parent = NULL; 72 __u32 btf_id = btf_node->id; 73 struct btf_node *node; 74 struct rb_node **p; 75 76 down_write(&env->bpf_progs.lock); 77 p = &env->bpf_progs.btfs.rb_node; 78 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; 89 } 90 } 91 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); 97 } 98 99 struct btf_node *perf_env__find_btf(struct perf_env *env, __u32 btf_id) 100 { 101 struct btf_node *node = NULL; 102 struct rb_node *n; 103 104 down_read(&env->bpf_progs.lock); 105 n = env->bpf_progs.btfs.rb_node; 106 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; 115 } 116 node = NULL; 117 118 up_read(&env->bpf_progs.lock); 119 out: 120 return node; 121 } 122 123 /* purge data in bpf_progs.infos tree */ 124 static void perf_env__purge_bpf(struct perf_env *env) 125 { 126 struct rb_root *root; 127 struct rb_node *next; 128 129 down_write(&env->bpf_progs.lock); 130 131 root = &env->bpf_progs.infos; 132 next = rb_first(root); 133 134 while (next) { 135 struct bpf_prog_info_node *node; 136 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); 141 } 142 143 env->bpf_progs.infos_cnt = 0; 144 145 root = &env->bpf_progs.btfs; 146 next = rb_first(root); 147 148 while (next) { 149 struct btf_node *node; 150 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); 155 } 156 157 env->bpf_progs.btfs_cnt = 0; 158 159 up_write(&env->bpf_progs.lock); 160 } 161 162 void perf_env__exit(struct perf_env *env) 163 { 164 int i; 165 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); 179 180 for (i = 0; i < env->nr_numa_nodes; i++) 181 cpu_map__put(env->numa_nodes[i].map); 182 zfree(&env->numa_nodes); 183 184 for (i = 0; i < env->caches_cnt; i++) 185 cpu_cache_level__free(&env->caches[i]); 186 zfree(&env->caches); 187 188 for (i = 0; i < env->nr_memory_nodes; i++) 189 free(env->memory_nodes[i].set); 190 zfree(&env->memory_nodes); 191 } 192 193 void perf_env__init(struct perf_env *env) 194 { 195 env->bpf_progs.infos = RB_ROOT; 196 env->bpf_progs.btfs = RB_ROOT; 197 init_rwsem(&env->bpf_progs.lock); 198 } 199 200 int perf_env__set_cmdline(struct perf_env *env, int argc, const char *argv[]) 201 { 202 int i; 203 204 /* do not include NULL termination */ 205 env->cmdline_argv = calloc(argc, sizeof(char *)); 206 if (env->cmdline_argv == NULL) 207 goto out_enomem; 208 209 /* 210 * Must copy argv contents because it gets moved around during option 211 * parsing: 212 */ 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; 217 } 218 219 env->nr_cmdline = argc; 220 221 return 0; 222 out_free: 223 zfree(&env->cmdline_argv); 224 out_enomem: 225 return -ENOMEM; 226 } 227 228 int perf_env__read_cpu_topology_map(struct perf_env *env) 229 { 230 int cpu, nr_cpus; 231 232 if (env->cpu != NULL) 233 return 0; 234 235 if (env->nr_cpus_avail == 0) 236 env->nr_cpus_avail = cpu__max_present_cpu(); 237 238 nr_cpus = env->nr_cpus_avail; 239 if (nr_cpus == -1) 240 return -EINVAL; 241 242 env->cpu = calloc(nr_cpus, sizeof(env->cpu[0])); 243 if (env->cpu == NULL) 244 return -ENOMEM; 245 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); 249 } 250 251 env->nr_cpus_avail = nr_cpus; 252 return 0; 253 } 254 255 static int perf_env__read_arch(struct perf_env *env) 256 { 257 struct utsname uts; 258 259 if (env->arch) 260 return 0; 261 262 if (!uname(&uts)) 263 env->arch = strdup(uts.machine); 264 265 return env->arch ? 0 : -ENOMEM; 266 } 267 268 static int perf_env__read_nr_cpus_avail(struct perf_env *env) 269 { 270 if (env->nr_cpus_avail == 0) 271 env->nr_cpus_avail = cpu__max_present_cpu(); 272 273 return env->nr_cpus_avail ? 0 : -ENOENT; 274 } 275 276 const char *perf_env__raw_arch(struct perf_env *env) 277 { 278 return env && !perf_env__read_arch(env) ? env->arch : "unknown"; 279 } 280 281 int perf_env__nr_cpus_avail(struct perf_env *env) 282 { 283 return env && !perf_env__read_nr_cpus_avail(env) ? env->nr_cpus_avail : 0; 284 } 285 286 void cpu_cache_level__free(struct cpu_cache_level *cache) 287 { 288 free(cache->type); 289 free(cache->map); 290 free(cache->size); 291 } 292 293 /* 294 * Return architecture name in a normalized form. 295 * The conversion logic comes from the Makefile. 296 */ 297 static const char *normalize_arch(char *arch) 298 { 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"; 319 320 return arch; 321 } 322 323 const char *perf_env__arch(struct perf_env *env) 324 { 325 struct utsname uts; 326 char *arch_name; 327 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; 334 335 return normalize_arch(arch_name); 336 } 337