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> 13 #include "strbuf.h" 14 15 struct perf_env perf_env; 16 17 #ifdef HAVE_LIBBPF_SUPPORT 18 #include "bpf-event.h" 19 #include "bpf-utils.h" 20 #include <bpf/libbpf.h> 21 22 void perf_env__insert_bpf_prog_info(struct perf_env *env, 23 struct bpf_prog_info_node *info_node) 24 { 25 __u32 prog_id = info_node->info_linear->info.id; 26 struct bpf_prog_info_node *node; 27 struct rb_node *parent = NULL; 28 struct rb_node **p; 29 30 down_write(&env->bpf_progs.lock); 31 p = &env->bpf_progs.infos.rb_node; 32 33 while (*p != NULL) { 34 parent = *p; 35 node = rb_entry(parent, struct bpf_prog_info_node, rb_node); 36 if (prog_id < node->info_linear->info.id) { 37 p = &(*p)->rb_left; 38 } else if (prog_id > node->info_linear->info.id) { 39 p = &(*p)->rb_right; 40 } else { 41 pr_debug("duplicated bpf prog info %u\n", prog_id); 42 goto out; 43 } 44 } 45 46 rb_link_node(&info_node->rb_node, parent, p); 47 rb_insert_color(&info_node->rb_node, &env->bpf_progs.infos); 48 env->bpf_progs.infos_cnt++; 49 out: 50 up_write(&env->bpf_progs.lock); 51 } 52 53 struct bpf_prog_info_node *perf_env__find_bpf_prog_info(struct perf_env *env, 54 __u32 prog_id) 55 { 56 struct bpf_prog_info_node *node = NULL; 57 struct rb_node *n; 58 59 down_read(&env->bpf_progs.lock); 60 n = env->bpf_progs.infos.rb_node; 61 62 while (n) { 63 node = rb_entry(n, struct bpf_prog_info_node, rb_node); 64 if (prog_id < node->info_linear->info.id) 65 n = n->rb_left; 66 else if (prog_id > node->info_linear->info.id) 67 n = n->rb_right; 68 else 69 goto out; 70 } 71 node = NULL; 72 73 out: 74 up_read(&env->bpf_progs.lock); 75 return node; 76 } 77 78 void perf_env__insert_btf(struct perf_env *env, struct btf_node *btf_node) 79 { 80 struct rb_node *parent = NULL; 81 __u32 btf_id = btf_node->id; 82 struct btf_node *node; 83 struct rb_node **p; 84 85 down_write(&env->bpf_progs.lock); 86 p = &env->bpf_progs.btfs.rb_node; 87 88 while (*p != NULL) { 89 parent = *p; 90 node = rb_entry(parent, struct btf_node, rb_node); 91 if (btf_id < node->id) { 92 p = &(*p)->rb_left; 93 } else if (btf_id > node->id) { 94 p = &(*p)->rb_right; 95 } else { 96 pr_debug("duplicated btf %u\n", btf_id); 97 goto out; 98 } 99 } 100 101 rb_link_node(&btf_node->rb_node, parent, p); 102 rb_insert_color(&btf_node->rb_node, &env->bpf_progs.btfs); 103 env->bpf_progs.btfs_cnt++; 104 out: 105 up_write(&env->bpf_progs.lock); 106 } 107 108 struct btf_node *perf_env__find_btf(struct perf_env *env, __u32 btf_id) 109 { 110 struct btf_node *node = NULL; 111 struct rb_node *n; 112 113 down_read(&env->bpf_progs.lock); 114 n = env->bpf_progs.btfs.rb_node; 115 116 while (n) { 117 node = rb_entry(n, struct btf_node, rb_node); 118 if (btf_id < node->id) 119 n = n->rb_left; 120 else if (btf_id > node->id) 121 n = n->rb_right; 122 else 123 goto out; 124 } 125 node = NULL; 126 127 out: 128 up_read(&env->bpf_progs.lock); 129 return node; 130 } 131 132 /* purge data in bpf_progs.infos tree */ 133 static void perf_env__purge_bpf(struct perf_env *env) 134 { 135 struct rb_root *root; 136 struct rb_node *next; 137 138 down_write(&env->bpf_progs.lock); 139 140 root = &env->bpf_progs.infos; 141 next = rb_first(root); 142 143 while (next) { 144 struct bpf_prog_info_node *node; 145 146 node = rb_entry(next, struct bpf_prog_info_node, rb_node); 147 next = rb_next(&node->rb_node); 148 rb_erase(&node->rb_node, root); 149 free(node->info_linear); 150 free(node); 151 } 152 153 env->bpf_progs.infos_cnt = 0; 154 155 root = &env->bpf_progs.btfs; 156 next = rb_first(root); 157 158 while (next) { 159 struct btf_node *node; 160 161 node = rb_entry(next, struct btf_node, rb_node); 162 next = rb_next(&node->rb_node); 163 rb_erase(&node->rb_node, root); 164 free(node); 165 } 166 167 env->bpf_progs.btfs_cnt = 0; 168 169 up_write(&env->bpf_progs.lock); 170 } 171 #else // HAVE_LIBBPF_SUPPORT 172 static void perf_env__purge_bpf(struct perf_env *env __maybe_unused) 173 { 174 } 175 #endif // HAVE_LIBBPF_SUPPORT 176 177 void perf_env__exit(struct perf_env *env) 178 { 179 int i; 180 181 perf_env__purge_bpf(env); 182 perf_env__purge_cgroups(env); 183 zfree(&env->hostname); 184 zfree(&env->os_release); 185 zfree(&env->version); 186 zfree(&env->arch); 187 zfree(&env->cpu_desc); 188 zfree(&env->cpuid); 189 zfree(&env->cmdline); 190 zfree(&env->cmdline_argv); 191 zfree(&env->sibling_dies); 192 zfree(&env->sibling_cores); 193 zfree(&env->sibling_threads); 194 zfree(&env->pmu_mappings); 195 zfree(&env->cpu); 196 zfree(&env->cpu_pmu_caps); 197 zfree(&env->numa_map); 198 199 for (i = 0; i < env->nr_numa_nodes; i++) 200 perf_cpu_map__put(env->numa_nodes[i].map); 201 zfree(&env->numa_nodes); 202 203 for (i = 0; i < env->caches_cnt; i++) 204 cpu_cache_level__free(&env->caches[i]); 205 zfree(&env->caches); 206 207 for (i = 0; i < env->nr_memory_nodes; i++) 208 zfree(&env->memory_nodes[i].set); 209 zfree(&env->memory_nodes); 210 211 for (i = 0; i < env->nr_hybrid_nodes; i++) { 212 zfree(&env->hybrid_nodes[i].pmu_name); 213 zfree(&env->hybrid_nodes[i].cpus); 214 } 215 zfree(&env->hybrid_nodes); 216 217 for (i = 0; i < env->nr_hybrid_cpc_nodes; i++) { 218 zfree(&env->hybrid_cpc_nodes[i].cpu_pmu_caps); 219 zfree(&env->hybrid_cpc_nodes[i].pmu_name); 220 } 221 zfree(&env->hybrid_cpc_nodes); 222 } 223 224 void perf_env__init(struct perf_env *env) 225 { 226 #ifdef HAVE_LIBBPF_SUPPORT 227 env->bpf_progs.infos = RB_ROOT; 228 env->bpf_progs.btfs = RB_ROOT; 229 init_rwsem(&env->bpf_progs.lock); 230 #endif 231 env->kernel_is_64_bit = -1; 232 } 233 234 static void perf_env__init_kernel_mode(struct perf_env *env) 235 { 236 const char *arch = perf_env__raw_arch(env); 237 238 if (!strncmp(arch, "x86_64", 6) || !strncmp(arch, "aarch64", 7) || 239 !strncmp(arch, "arm64", 5) || !strncmp(arch, "mips64", 6) || 240 !strncmp(arch, "parisc64", 8) || !strncmp(arch, "riscv64", 7) || 241 !strncmp(arch, "s390x", 5) || !strncmp(arch, "sparc64", 7)) 242 env->kernel_is_64_bit = 1; 243 else 244 env->kernel_is_64_bit = 0; 245 } 246 247 int perf_env__kernel_is_64_bit(struct perf_env *env) 248 { 249 if (env->kernel_is_64_bit == -1) 250 perf_env__init_kernel_mode(env); 251 252 return env->kernel_is_64_bit; 253 } 254 255 int perf_env__set_cmdline(struct perf_env *env, int argc, const char *argv[]) 256 { 257 int i; 258 259 /* do not include NULL termination */ 260 env->cmdline_argv = calloc(argc, sizeof(char *)); 261 if (env->cmdline_argv == NULL) 262 goto out_enomem; 263 264 /* 265 * Must copy argv contents because it gets moved around during option 266 * parsing: 267 */ 268 for (i = 0; i < argc ; i++) { 269 env->cmdline_argv[i] = argv[i]; 270 if (env->cmdline_argv[i] == NULL) 271 goto out_free; 272 } 273 274 env->nr_cmdline = argc; 275 276 return 0; 277 out_free: 278 zfree(&env->cmdline_argv); 279 out_enomem: 280 return -ENOMEM; 281 } 282 283 int perf_env__read_cpu_topology_map(struct perf_env *env) 284 { 285 int cpu, nr_cpus; 286 287 if (env->cpu != NULL) 288 return 0; 289 290 if (env->nr_cpus_avail == 0) 291 env->nr_cpus_avail = cpu__max_present_cpu(); 292 293 nr_cpus = env->nr_cpus_avail; 294 if (nr_cpus == -1) 295 return -EINVAL; 296 297 env->cpu = calloc(nr_cpus, sizeof(env->cpu[0])); 298 if (env->cpu == NULL) 299 return -ENOMEM; 300 301 for (cpu = 0; cpu < nr_cpus; ++cpu) { 302 env->cpu[cpu].core_id = cpu_map__get_core_id(cpu); 303 env->cpu[cpu].socket_id = cpu_map__get_socket_id(cpu); 304 env->cpu[cpu].die_id = cpu_map__get_die_id(cpu); 305 } 306 307 env->nr_cpus_avail = nr_cpus; 308 return 0; 309 } 310 311 int perf_env__read_pmu_mappings(struct perf_env *env) 312 { 313 struct perf_pmu *pmu = NULL; 314 u32 pmu_num = 0; 315 struct strbuf sb; 316 317 while ((pmu = perf_pmu__scan(pmu))) { 318 if (!pmu->name) 319 continue; 320 pmu_num++; 321 } 322 if (!pmu_num) { 323 pr_debug("pmu mappings not available\n"); 324 return -ENOENT; 325 } 326 env->nr_pmu_mappings = pmu_num; 327 328 if (strbuf_init(&sb, 128 * pmu_num) < 0) 329 return -ENOMEM; 330 331 while ((pmu = perf_pmu__scan(pmu))) { 332 if (!pmu->name) 333 continue; 334 if (strbuf_addf(&sb, "%u:%s", pmu->type, pmu->name) < 0) 335 goto error; 336 /* include a NULL character at the end */ 337 if (strbuf_add(&sb, "", 1) < 0) 338 goto error; 339 } 340 341 env->pmu_mappings = strbuf_detach(&sb, NULL); 342 343 return 0; 344 345 error: 346 strbuf_release(&sb); 347 return -1; 348 } 349 350 int perf_env__read_cpuid(struct perf_env *env) 351 { 352 char cpuid[128]; 353 int err = get_cpuid(cpuid, sizeof(cpuid)); 354 355 if (err) 356 return err; 357 358 free(env->cpuid); 359 env->cpuid = strdup(cpuid); 360 if (env->cpuid == NULL) 361 return ENOMEM; 362 return 0; 363 } 364 365 static int perf_env__read_arch(struct perf_env *env) 366 { 367 struct utsname uts; 368 369 if (env->arch) 370 return 0; 371 372 if (!uname(&uts)) 373 env->arch = strdup(uts.machine); 374 375 return env->arch ? 0 : -ENOMEM; 376 } 377 378 static int perf_env__read_nr_cpus_avail(struct perf_env *env) 379 { 380 if (env->nr_cpus_avail == 0) 381 env->nr_cpus_avail = cpu__max_present_cpu(); 382 383 return env->nr_cpus_avail ? 0 : -ENOENT; 384 } 385 386 const char *perf_env__raw_arch(struct perf_env *env) 387 { 388 return env && !perf_env__read_arch(env) ? env->arch : "unknown"; 389 } 390 391 int perf_env__nr_cpus_avail(struct perf_env *env) 392 { 393 return env && !perf_env__read_nr_cpus_avail(env) ? env->nr_cpus_avail : 0; 394 } 395 396 void cpu_cache_level__free(struct cpu_cache_level *cache) 397 { 398 zfree(&cache->type); 399 zfree(&cache->map); 400 zfree(&cache->size); 401 } 402 403 /* 404 * Return architecture name in a normalized form. 405 * The conversion logic comes from the Makefile. 406 */ 407 static const char *normalize_arch(char *arch) 408 { 409 if (!strcmp(arch, "x86_64")) 410 return "x86"; 411 if (arch[0] == 'i' && arch[2] == '8' && arch[3] == '6') 412 return "x86"; 413 if (!strcmp(arch, "sun4u") || !strncmp(arch, "sparc", 5)) 414 return "sparc"; 415 if (!strncmp(arch, "aarch64", 7) || !strncmp(arch, "arm64", 5)) 416 return "arm64"; 417 if (!strncmp(arch, "arm", 3) || !strcmp(arch, "sa110")) 418 return "arm"; 419 if (!strncmp(arch, "s390", 4)) 420 return "s390"; 421 if (!strncmp(arch, "parisc", 6)) 422 return "parisc"; 423 if (!strncmp(arch, "powerpc", 7) || !strncmp(arch, "ppc", 3)) 424 return "powerpc"; 425 if (!strncmp(arch, "mips", 4)) 426 return "mips"; 427 if (!strncmp(arch, "sh", 2) && isdigit(arch[2])) 428 return "sh"; 429 430 return arch; 431 } 432 433 const char *perf_env__arch(struct perf_env *env) 434 { 435 char *arch_name; 436 437 if (!env || !env->arch) { /* Assume local operation */ 438 static struct utsname uts = { .machine[0] = '\0', }; 439 if (uts.machine[0] == '\0' && uname(&uts) < 0) 440 return NULL; 441 arch_name = uts.machine; 442 } else 443 arch_name = env->arch; 444 445 return normalize_arch(arch_name); 446 } 447 448 const char *perf_env__cpuid(struct perf_env *env) 449 { 450 int status; 451 452 if (!env || !env->cpuid) { /* Assume local operation */ 453 status = perf_env__read_cpuid(env); 454 if (status) 455 return NULL; 456 } 457 458 return env->cpuid; 459 } 460 461 int perf_env__nr_pmu_mappings(struct perf_env *env) 462 { 463 int status; 464 465 if (!env || !env->nr_pmu_mappings) { /* Assume local operation */ 466 status = perf_env__read_pmu_mappings(env); 467 if (status) 468 return 0; 469 } 470 471 return env->nr_pmu_mappings; 472 } 473 474 const char *perf_env__pmu_mappings(struct perf_env *env) 475 { 476 int status; 477 478 if (!env || !env->pmu_mappings) { /* Assume local operation */ 479 status = perf_env__read_pmu_mappings(env); 480 if (status) 481 return NULL; 482 } 483 484 return env->pmu_mappings; 485 } 486 487 int perf_env__numa_node(struct perf_env *env, int cpu) 488 { 489 if (!env->nr_numa_map) { 490 struct numa_node *nn; 491 int i, nr = 0; 492 493 for (i = 0; i < env->nr_numa_nodes; i++) { 494 nn = &env->numa_nodes[i]; 495 nr = max(nr, perf_cpu_map__max(nn->map)); 496 } 497 498 nr++; 499 500 /* 501 * We initialize the numa_map array to prepare 502 * it for missing cpus, which return node -1 503 */ 504 env->numa_map = malloc(nr * sizeof(int)); 505 if (!env->numa_map) 506 return -1; 507 508 for (i = 0; i < nr; i++) 509 env->numa_map[i] = -1; 510 511 env->nr_numa_map = nr; 512 513 for (i = 0; i < env->nr_numa_nodes; i++) { 514 int tmp, j; 515 516 nn = &env->numa_nodes[i]; 517 perf_cpu_map__for_each_cpu(j, tmp, nn->map) 518 env->numa_map[j] = i; 519 } 520 } 521 522 return cpu >= 0 && cpu < env->nr_numa_map ? env->numa_map[cpu] : -1; 523 } 524