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