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/compiler.h" 7 #include <linux/ctype.h> 8 #include <linux/string.h> 9 #include <linux/zalloc.h> 10 #include "cgroup.h" 11 #include <errno.h> 12 #include <sys/utsname.h> 13 #include <stdlib.h> 14 #include <string.h> 15 #include "pmus.h" 16 #include "strbuf.h" 17 #include "trace/beauty/beauty.h" 18 19 struct perf_env perf_env; 20 21 #ifdef HAVE_LIBBPF_SUPPORT 22 #include "bpf-event.h" 23 #include "bpf-utils.h" 24 #include <bpf/libbpf.h> 25 26 void perf_env__insert_bpf_prog_info(struct perf_env *env, 27 struct bpf_prog_info_node *info_node) 28 { 29 down_write(&env->bpf_progs.lock); 30 __perf_env__insert_bpf_prog_info(env, info_node); 31 up_write(&env->bpf_progs.lock); 32 } 33 34 void __perf_env__insert_bpf_prog_info(struct perf_env *env, struct bpf_prog_info_node *info_node) 35 { 36 __u32 prog_id = info_node->info_linear->info.id; 37 struct bpf_prog_info_node *node; 38 struct rb_node *parent = NULL; 39 struct rb_node **p; 40 41 p = &env->bpf_progs.infos.rb_node; 42 43 while (*p != NULL) { 44 parent = *p; 45 node = rb_entry(parent, struct bpf_prog_info_node, rb_node); 46 if (prog_id < node->info_linear->info.id) { 47 p = &(*p)->rb_left; 48 } else if (prog_id > node->info_linear->info.id) { 49 p = &(*p)->rb_right; 50 } else { 51 pr_debug("duplicated bpf prog info %u\n", prog_id); 52 return; 53 } 54 } 55 56 rb_link_node(&info_node->rb_node, parent, p); 57 rb_insert_color(&info_node->rb_node, &env->bpf_progs.infos); 58 env->bpf_progs.infos_cnt++; 59 } 60 61 struct bpf_prog_info_node *perf_env__find_bpf_prog_info(struct perf_env *env, 62 __u32 prog_id) 63 { 64 struct bpf_prog_info_node *node = NULL; 65 struct rb_node *n; 66 67 down_read(&env->bpf_progs.lock); 68 n = env->bpf_progs.infos.rb_node; 69 70 while (n) { 71 node = rb_entry(n, struct bpf_prog_info_node, rb_node); 72 if (prog_id < node->info_linear->info.id) 73 n = n->rb_left; 74 else if (prog_id > node->info_linear->info.id) 75 n = n->rb_right; 76 else 77 goto out; 78 } 79 node = NULL; 80 81 out: 82 up_read(&env->bpf_progs.lock); 83 return node; 84 } 85 86 bool perf_env__insert_btf(struct perf_env *env, struct btf_node *btf_node) 87 { 88 bool ret; 89 90 down_write(&env->bpf_progs.lock); 91 ret = __perf_env__insert_btf(env, btf_node); 92 up_write(&env->bpf_progs.lock); 93 return ret; 94 } 95 96 bool __perf_env__insert_btf(struct perf_env *env, struct btf_node *btf_node) 97 { 98 struct rb_node *parent = NULL; 99 __u32 btf_id = btf_node->id; 100 struct btf_node *node; 101 struct rb_node **p; 102 103 p = &env->bpf_progs.btfs.rb_node; 104 105 while (*p != NULL) { 106 parent = *p; 107 node = rb_entry(parent, struct btf_node, rb_node); 108 if (btf_id < node->id) { 109 p = &(*p)->rb_left; 110 } else if (btf_id > node->id) { 111 p = &(*p)->rb_right; 112 } else { 113 pr_debug("duplicated btf %u\n", btf_id); 114 return false; 115 } 116 } 117 118 rb_link_node(&btf_node->rb_node, parent, p); 119 rb_insert_color(&btf_node->rb_node, &env->bpf_progs.btfs); 120 env->bpf_progs.btfs_cnt++; 121 return true; 122 } 123 124 struct btf_node *perf_env__find_btf(struct perf_env *env, __u32 btf_id) 125 { 126 struct btf_node *res; 127 128 down_read(&env->bpf_progs.lock); 129 res = __perf_env__find_btf(env, btf_id); 130 up_read(&env->bpf_progs.lock); 131 return res; 132 } 133 134 struct btf_node *__perf_env__find_btf(struct perf_env *env, __u32 btf_id) 135 { 136 struct btf_node *node = NULL; 137 struct rb_node *n; 138 139 n = env->bpf_progs.btfs.rb_node; 140 141 while (n) { 142 node = rb_entry(n, struct btf_node, rb_node); 143 if (btf_id < node->id) 144 n = n->rb_left; 145 else if (btf_id > node->id) 146 n = n->rb_right; 147 else 148 return node; 149 } 150 return NULL; 151 } 152 153 /* purge data in bpf_progs.infos tree */ 154 static void perf_env__purge_bpf(struct perf_env *env) 155 { 156 struct rb_root *root; 157 struct rb_node *next; 158 159 down_write(&env->bpf_progs.lock); 160 161 root = &env->bpf_progs.infos; 162 next = rb_first(root); 163 164 while (next) { 165 struct bpf_prog_info_node *node; 166 167 node = rb_entry(next, struct bpf_prog_info_node, rb_node); 168 next = rb_next(&node->rb_node); 169 rb_erase(&node->rb_node, root); 170 zfree(&node->info_linear); 171 free(node); 172 } 173 174 env->bpf_progs.infos_cnt = 0; 175 176 root = &env->bpf_progs.btfs; 177 next = rb_first(root); 178 179 while (next) { 180 struct btf_node *node; 181 182 node = rb_entry(next, struct btf_node, rb_node); 183 next = rb_next(&node->rb_node); 184 rb_erase(&node->rb_node, root); 185 free(node); 186 } 187 188 env->bpf_progs.btfs_cnt = 0; 189 190 up_write(&env->bpf_progs.lock); 191 } 192 #else // HAVE_LIBBPF_SUPPORT 193 static void perf_env__purge_bpf(struct perf_env *env __maybe_unused) 194 { 195 } 196 #endif // HAVE_LIBBPF_SUPPORT 197 198 void perf_env__exit(struct perf_env *env) 199 { 200 int i, j; 201 202 perf_env__purge_bpf(env); 203 perf_env__purge_cgroups(env); 204 zfree(&env->hostname); 205 zfree(&env->os_release); 206 zfree(&env->version); 207 zfree(&env->arch); 208 zfree(&env->cpu_desc); 209 zfree(&env->cpuid); 210 zfree(&env->cmdline); 211 zfree(&env->cmdline_argv); 212 zfree(&env->sibling_dies); 213 zfree(&env->sibling_cores); 214 zfree(&env->sibling_threads); 215 zfree(&env->pmu_mappings); 216 zfree(&env->cpu); 217 for (i = 0; i < env->nr_cpu_pmu_caps; i++) 218 zfree(&env->cpu_pmu_caps[i]); 219 zfree(&env->cpu_pmu_caps); 220 zfree(&env->numa_map); 221 222 for (i = 0; i < env->nr_numa_nodes; i++) 223 perf_cpu_map__put(env->numa_nodes[i].map); 224 zfree(&env->numa_nodes); 225 226 for (i = 0; i < env->caches_cnt; i++) 227 cpu_cache_level__free(&env->caches[i]); 228 zfree(&env->caches); 229 230 for (i = 0; i < env->nr_memory_nodes; i++) 231 zfree(&env->memory_nodes[i].set); 232 zfree(&env->memory_nodes); 233 234 for (i = 0; i < env->nr_hybrid_nodes; i++) { 235 zfree(&env->hybrid_nodes[i].pmu_name); 236 zfree(&env->hybrid_nodes[i].cpus); 237 } 238 zfree(&env->hybrid_nodes); 239 240 for (i = 0; i < env->nr_pmus_with_caps; i++) { 241 for (j = 0; j < env->pmu_caps[i].nr_caps; j++) 242 zfree(&env->pmu_caps[i].caps[j]); 243 zfree(&env->pmu_caps[i].caps); 244 zfree(&env->pmu_caps[i].pmu_name); 245 } 246 zfree(&env->pmu_caps); 247 } 248 249 void perf_env__init(struct perf_env *env) 250 { 251 #ifdef HAVE_LIBBPF_SUPPORT 252 env->bpf_progs.infos = RB_ROOT; 253 env->bpf_progs.btfs = RB_ROOT; 254 init_rwsem(&env->bpf_progs.lock); 255 #endif 256 env->kernel_is_64_bit = -1; 257 } 258 259 static void perf_env__init_kernel_mode(struct perf_env *env) 260 { 261 const char *arch = perf_env__raw_arch(env); 262 263 if (!strncmp(arch, "x86_64", 6) || !strncmp(arch, "aarch64", 7) || 264 !strncmp(arch, "arm64", 5) || !strncmp(arch, "mips64", 6) || 265 !strncmp(arch, "parisc64", 8) || !strncmp(arch, "riscv64", 7) || 266 !strncmp(arch, "s390x", 5) || !strncmp(arch, "sparc64", 7)) 267 env->kernel_is_64_bit = 1; 268 else 269 env->kernel_is_64_bit = 0; 270 } 271 272 int perf_env__kernel_is_64_bit(struct perf_env *env) 273 { 274 if (env->kernel_is_64_bit == -1) 275 perf_env__init_kernel_mode(env); 276 277 return env->kernel_is_64_bit; 278 } 279 280 int perf_env__set_cmdline(struct perf_env *env, int argc, const char *argv[]) 281 { 282 int i; 283 284 /* do not include NULL termination */ 285 env->cmdline_argv = calloc(argc, sizeof(char *)); 286 if (env->cmdline_argv == NULL) 287 goto out_enomem; 288 289 /* 290 * Must copy argv contents because it gets moved around during option 291 * parsing: 292 */ 293 for (i = 0; i < argc ; i++) { 294 env->cmdline_argv[i] = argv[i]; 295 if (env->cmdline_argv[i] == NULL) 296 goto out_free; 297 } 298 299 env->nr_cmdline = argc; 300 301 return 0; 302 out_free: 303 zfree(&env->cmdline_argv); 304 out_enomem: 305 return -ENOMEM; 306 } 307 308 int perf_env__read_cpu_topology_map(struct perf_env *env) 309 { 310 int idx, nr_cpus; 311 312 if (env->cpu != NULL) 313 return 0; 314 315 if (env->nr_cpus_avail == 0) 316 env->nr_cpus_avail = cpu__max_present_cpu().cpu; 317 318 nr_cpus = env->nr_cpus_avail; 319 if (nr_cpus == -1) 320 return -EINVAL; 321 322 env->cpu = calloc(nr_cpus, sizeof(env->cpu[0])); 323 if (env->cpu == NULL) 324 return -ENOMEM; 325 326 for (idx = 0; idx < nr_cpus; ++idx) { 327 struct perf_cpu cpu = { .cpu = idx }; 328 329 env->cpu[idx].core_id = cpu__get_core_id(cpu); 330 env->cpu[idx].socket_id = cpu__get_socket_id(cpu); 331 env->cpu[idx].die_id = cpu__get_die_id(cpu); 332 } 333 334 env->nr_cpus_avail = nr_cpus; 335 return 0; 336 } 337 338 int perf_env__read_pmu_mappings(struct perf_env *env) 339 { 340 struct perf_pmu *pmu = NULL; 341 u32 pmu_num = 0; 342 struct strbuf sb; 343 344 while ((pmu = perf_pmus__scan(pmu))) 345 pmu_num++; 346 347 if (!pmu_num) { 348 pr_debug("pmu mappings not available\n"); 349 return -ENOENT; 350 } 351 env->nr_pmu_mappings = pmu_num; 352 353 if (strbuf_init(&sb, 128 * pmu_num) < 0) 354 return -ENOMEM; 355 356 while ((pmu = perf_pmus__scan(pmu))) { 357 if (strbuf_addf(&sb, "%u:%s", pmu->type, pmu->name) < 0) 358 goto error; 359 /* include a NULL character at the end */ 360 if (strbuf_add(&sb, "", 1) < 0) 361 goto error; 362 } 363 364 env->pmu_mappings = strbuf_detach(&sb, NULL); 365 366 return 0; 367 368 error: 369 strbuf_release(&sb); 370 return -1; 371 } 372 373 int perf_env__read_cpuid(struct perf_env *env) 374 { 375 char cpuid[128]; 376 int err = get_cpuid(cpuid, sizeof(cpuid)); 377 378 if (err) 379 return err; 380 381 free(env->cpuid); 382 env->cpuid = strdup(cpuid); 383 if (env->cpuid == NULL) 384 return ENOMEM; 385 return 0; 386 } 387 388 static int perf_env__read_arch(struct perf_env *env) 389 { 390 struct utsname uts; 391 392 if (env->arch) 393 return 0; 394 395 if (!uname(&uts)) 396 env->arch = strdup(uts.machine); 397 398 return env->arch ? 0 : -ENOMEM; 399 } 400 401 static int perf_env__read_nr_cpus_avail(struct perf_env *env) 402 { 403 if (env->nr_cpus_avail == 0) 404 env->nr_cpus_avail = cpu__max_present_cpu().cpu; 405 406 return env->nr_cpus_avail ? 0 : -ENOENT; 407 } 408 409 const char *perf_env__raw_arch(struct perf_env *env) 410 { 411 return env && !perf_env__read_arch(env) ? env->arch : "unknown"; 412 } 413 414 int perf_env__nr_cpus_avail(struct perf_env *env) 415 { 416 return env && !perf_env__read_nr_cpus_avail(env) ? env->nr_cpus_avail : 0; 417 } 418 419 void cpu_cache_level__free(struct cpu_cache_level *cache) 420 { 421 zfree(&cache->type); 422 zfree(&cache->map); 423 zfree(&cache->size); 424 } 425 426 /* 427 * Return architecture name in a normalized form. 428 * The conversion logic comes from the Makefile. 429 */ 430 static const char *normalize_arch(char *arch) 431 { 432 if (!strcmp(arch, "x86_64")) 433 return "x86"; 434 if (arch[0] == 'i' && arch[2] == '8' && arch[3] == '6') 435 return "x86"; 436 if (!strcmp(arch, "sun4u") || !strncmp(arch, "sparc", 5)) 437 return "sparc"; 438 if (!strncmp(arch, "aarch64", 7) || !strncmp(arch, "arm64", 5)) 439 return "arm64"; 440 if (!strncmp(arch, "arm", 3) || !strcmp(arch, "sa110")) 441 return "arm"; 442 if (!strncmp(arch, "s390", 4)) 443 return "s390"; 444 if (!strncmp(arch, "parisc", 6)) 445 return "parisc"; 446 if (!strncmp(arch, "powerpc", 7) || !strncmp(arch, "ppc", 3)) 447 return "powerpc"; 448 if (!strncmp(arch, "mips", 4)) 449 return "mips"; 450 if (!strncmp(arch, "sh", 2) && isdigit(arch[2])) 451 return "sh"; 452 if (!strncmp(arch, "loongarch", 9)) 453 return "loongarch"; 454 455 return arch; 456 } 457 458 const char *perf_env__arch(struct perf_env *env) 459 { 460 char *arch_name; 461 462 if (!env || !env->arch) { /* Assume local operation */ 463 static struct utsname uts = { .machine[0] = '\0', }; 464 if (uts.machine[0] == '\0' && uname(&uts) < 0) 465 return NULL; 466 arch_name = uts.machine; 467 } else 468 arch_name = env->arch; 469 470 return normalize_arch(arch_name); 471 } 472 473 const char *perf_env__arch_strerrno(struct perf_env *env __maybe_unused, int err __maybe_unused) 474 { 475 #if defined(HAVE_SYSCALL_TABLE_SUPPORT) && defined(HAVE_LIBTRACEEVENT) 476 if (env->arch_strerrno == NULL) 477 env->arch_strerrno = arch_syscalls__strerrno_function(perf_env__arch(env)); 478 479 return env->arch_strerrno ? env->arch_strerrno(err) : "no arch specific strerrno function"; 480 #else 481 return "!(HAVE_SYSCALL_TABLE_SUPPORT && HAVE_LIBTRACEEVENT)"; 482 #endif 483 } 484 485 const char *perf_env__cpuid(struct perf_env *env) 486 { 487 int status; 488 489 if (!env->cpuid) { /* Assume local operation */ 490 status = perf_env__read_cpuid(env); 491 if (status) 492 return NULL; 493 } 494 495 return env->cpuid; 496 } 497 498 int perf_env__nr_pmu_mappings(struct perf_env *env) 499 { 500 int status; 501 502 if (!env->nr_pmu_mappings) { /* Assume local operation */ 503 status = perf_env__read_pmu_mappings(env); 504 if (status) 505 return 0; 506 } 507 508 return env->nr_pmu_mappings; 509 } 510 511 const char *perf_env__pmu_mappings(struct perf_env *env) 512 { 513 int status; 514 515 if (!env->pmu_mappings) { /* Assume local operation */ 516 status = perf_env__read_pmu_mappings(env); 517 if (status) 518 return NULL; 519 } 520 521 return env->pmu_mappings; 522 } 523 524 int perf_env__numa_node(struct perf_env *env, struct perf_cpu cpu) 525 { 526 if (!env->nr_numa_map) { 527 struct numa_node *nn; 528 int i, nr = 0; 529 530 for (i = 0; i < env->nr_numa_nodes; i++) { 531 nn = &env->numa_nodes[i]; 532 nr = max(nr, perf_cpu_map__max(nn->map).cpu); 533 } 534 535 nr++; 536 537 /* 538 * We initialize the numa_map array to prepare 539 * it for missing cpus, which return node -1 540 */ 541 env->numa_map = malloc(nr * sizeof(int)); 542 if (!env->numa_map) 543 return -1; 544 545 for (i = 0; i < nr; i++) 546 env->numa_map[i] = -1; 547 548 env->nr_numa_map = nr; 549 550 for (i = 0; i < env->nr_numa_nodes; i++) { 551 struct perf_cpu tmp; 552 int j; 553 554 nn = &env->numa_nodes[i]; 555 perf_cpu_map__for_each_cpu(tmp, j, nn->map) 556 env->numa_map[tmp.cpu] = i; 557 } 558 } 559 560 return cpu.cpu >= 0 && cpu.cpu < env->nr_numa_map ? env->numa_map[cpu.cpu] : -1; 561 } 562 563 bool perf_env__has_pmu_mapping(struct perf_env *env, const char *pmu_name) 564 { 565 char *pmu_mapping = env->pmu_mappings, *colon; 566 567 for (int i = 0; i < env->nr_pmu_mappings; ++i) { 568 if (strtoul(pmu_mapping, &colon, 0) == ULONG_MAX || *colon != ':') 569 goto out_error; 570 571 pmu_mapping = colon + 1; 572 if (strcmp(pmu_mapping, pmu_name) == 0) 573 return true; 574 575 pmu_mapping += strlen(pmu_mapping) + 1; 576 } 577 out_error: 578 return false; 579 } 580 581 char *perf_env__find_pmu_cap(struct perf_env *env, const char *pmu_name, 582 const char *cap) 583 { 584 char *cap_eq; 585 int cap_size; 586 char **ptr; 587 int i, j; 588 589 if (!pmu_name || !cap) 590 return NULL; 591 592 cap_size = strlen(cap); 593 cap_eq = zalloc(cap_size + 2); 594 if (!cap_eq) 595 return NULL; 596 597 memcpy(cap_eq, cap, cap_size); 598 cap_eq[cap_size] = '='; 599 600 if (!strcmp(pmu_name, "cpu")) { 601 for (i = 0; i < env->nr_cpu_pmu_caps; i++) { 602 if (!strncmp(env->cpu_pmu_caps[i], cap_eq, cap_size + 1)) { 603 free(cap_eq); 604 return &env->cpu_pmu_caps[i][cap_size + 1]; 605 } 606 } 607 goto out; 608 } 609 610 for (i = 0; i < env->nr_pmus_with_caps; i++) { 611 if (strcmp(env->pmu_caps[i].pmu_name, pmu_name)) 612 continue; 613 614 ptr = env->pmu_caps[i].caps; 615 616 for (j = 0; j < env->pmu_caps[i].nr_caps; j++) { 617 if (!strncmp(ptr[j], cap_eq, cap_size + 1)) { 618 free(cap_eq); 619 return &ptr[j][cap_size + 1]; 620 } 621 } 622 } 623 624 out: 625 free(cap_eq); 626 return NULL; 627 } 628 629 void perf_env__find_br_cntr_info(struct perf_env *env, 630 unsigned int *nr, 631 unsigned int *width) 632 { 633 if (nr) { 634 *nr = env->cpu_pmu_caps ? env->br_cntr_nr : 635 env->pmu_caps->br_cntr_nr; 636 } 637 638 if (width) { 639 *width = env->cpu_pmu_caps ? env->br_cntr_width : 640 env->pmu_caps->br_cntr_width; 641 } 642 } 643 644 bool perf_env__is_x86_amd_cpu(struct perf_env *env) 645 { 646 static int is_amd; /* 0: Uninitialized, 1: Yes, -1: No */ 647 648 if (is_amd == 0) 649 is_amd = env->cpuid && strstarts(env->cpuid, "AuthenticAMD") ? 1 : -1; 650 651 return is_amd >= 1 ? true : false; 652 } 653 654 bool x86__is_amd_cpu(void) 655 { 656 struct perf_env env = { .total_mem = 0, }; 657 bool is_amd; 658 659 perf_env__cpuid(&env); 660 is_amd = perf_env__is_x86_amd_cpu(&env); 661 perf_env__exit(&env); 662 663 return is_amd; 664 } 665