1 // SPDX-License-Identifier: GPL-2.0 2 #include <linux/list.h> 3 #include <linux/list_sort.h> 4 #include <linux/string.h> 5 #include <linux/zalloc.h> 6 #include <subcmd/pager.h> 7 #include <sys/types.h> 8 #include <ctype.h> 9 #include <dirent.h> 10 #include <pthread.h> 11 #include <string.h> 12 #include <unistd.h> 13 #include "cpumap.h" 14 #include "debug.h" 15 #include "evsel.h" 16 #include "pmus.h" 17 #include "pmu.h" 18 #include "print-events.h" 19 #include "strbuf.h" 20 21 /* 22 * core_pmus: A PMU belongs to core_pmus if it's name is "cpu" or it's sysfs 23 * directory contains "cpus" file. All PMUs belonging to core_pmus 24 * must have pmu->is_core=1. If there are more than one PMU in 25 * this list, perf interprets it as a heterogeneous platform. 26 * (FWIW, certain ARM platforms having heterogeneous cores uses 27 * homogeneous PMU, and thus they are treated as homogeneous 28 * platform by perf because core_pmus will have only one entry) 29 * other_pmus: All other PMUs which are not part of core_pmus list. It doesn't 30 * matter whether PMU is present per SMT-thread or outside of the 31 * core in the hw. For e.g., an instance of AMD ibs_fetch// and 32 * ibs_op// PMUs is present in each hw SMT thread, however they 33 * are captured under other_pmus. PMUs belonging to other_pmus 34 * must have pmu->is_core=0 but pmu->is_uncore could be 0 or 1. 35 */ 36 static LIST_HEAD(core_pmus); 37 static LIST_HEAD(other_pmus); 38 static bool read_sysfs_core_pmus; 39 static bool read_sysfs_all_pmus; 40 41 static void pmu_read_sysfs(bool core_only); 42 43 int pmu_name_len_no_suffix(const char *str, unsigned long *num) 44 { 45 int orig_len, len; 46 47 orig_len = len = strlen(str); 48 49 /* Non-uncore PMUs have their full length, for example, i915. */ 50 if (!strstarts(str, "uncore_")) 51 return len; 52 53 /* 54 * Count trailing digits and '_', if '_{num}' suffix isn't present use 55 * the full length. 56 */ 57 while (len > 0 && isdigit(str[len - 1])) 58 len--; 59 60 if (len > 0 && len != orig_len && str[len - 1] == '_') { 61 if (num) 62 *num = strtoul(&str[len], NULL, 10); 63 return len - 1; 64 } 65 return orig_len; 66 } 67 68 void perf_pmus__destroy(void) 69 { 70 struct perf_pmu *pmu, *tmp; 71 72 list_for_each_entry_safe(pmu, tmp, &core_pmus, list) { 73 list_del(&pmu->list); 74 75 perf_pmu__delete(pmu); 76 } 77 list_for_each_entry_safe(pmu, tmp, &other_pmus, list) { 78 list_del(&pmu->list); 79 80 perf_pmu__delete(pmu); 81 } 82 read_sysfs_core_pmus = false; 83 read_sysfs_all_pmus = false; 84 } 85 86 static struct perf_pmu *pmu_find(const char *name) 87 { 88 struct perf_pmu *pmu; 89 90 list_for_each_entry(pmu, &core_pmus, list) { 91 if (!strcmp(pmu->name, name) || 92 (pmu->alias_name && !strcmp(pmu->alias_name, name))) 93 return pmu; 94 } 95 list_for_each_entry(pmu, &other_pmus, list) { 96 if (!strcmp(pmu->name, name) || 97 (pmu->alias_name && !strcmp(pmu->alias_name, name))) 98 return pmu; 99 } 100 101 return NULL; 102 } 103 104 struct perf_pmu *perf_pmus__find(const char *name) 105 { 106 struct perf_pmu *pmu; 107 int dirfd; 108 bool core_pmu; 109 110 /* 111 * Once PMU is loaded it stays in the list, 112 * so we keep us from multiple reading/parsing 113 * the pmu format definitions. 114 */ 115 pmu = pmu_find(name); 116 if (pmu) 117 return pmu; 118 119 if (read_sysfs_all_pmus) 120 return NULL; 121 122 core_pmu = is_pmu_core(name); 123 if (core_pmu && read_sysfs_core_pmus) 124 return NULL; 125 126 dirfd = perf_pmu__event_source_devices_fd(); 127 pmu = perf_pmu__lookup(core_pmu ? &core_pmus : &other_pmus, dirfd, name); 128 close(dirfd); 129 130 if (!pmu) { 131 /* 132 * Looking up an inidividual PMU failed. This may mean name is 133 * an alias, so read the PMUs from sysfs and try to find again. 134 */ 135 pmu_read_sysfs(core_pmu); 136 pmu = pmu_find(name); 137 } 138 return pmu; 139 } 140 141 static struct perf_pmu *perf_pmu__find2(int dirfd, const char *name) 142 { 143 struct perf_pmu *pmu; 144 bool core_pmu; 145 146 /* 147 * Once PMU is loaded it stays in the list, 148 * so we keep us from multiple reading/parsing 149 * the pmu format definitions. 150 */ 151 pmu = pmu_find(name); 152 if (pmu) 153 return pmu; 154 155 if (read_sysfs_all_pmus) 156 return NULL; 157 158 core_pmu = is_pmu_core(name); 159 if (core_pmu && read_sysfs_core_pmus) 160 return NULL; 161 162 return perf_pmu__lookup(core_pmu ? &core_pmus : &other_pmus, dirfd, name); 163 } 164 165 static int pmus_cmp(void *priv __maybe_unused, 166 const struct list_head *lhs, const struct list_head *rhs) 167 { 168 unsigned long lhs_num = 0, rhs_num = 0; 169 struct perf_pmu *lhs_pmu = container_of(lhs, struct perf_pmu, list); 170 struct perf_pmu *rhs_pmu = container_of(rhs, struct perf_pmu, list); 171 const char *lhs_pmu_name = lhs_pmu->name ?: ""; 172 const char *rhs_pmu_name = rhs_pmu->name ?: ""; 173 int lhs_pmu_name_len = pmu_name_len_no_suffix(lhs_pmu_name, &lhs_num); 174 int rhs_pmu_name_len = pmu_name_len_no_suffix(rhs_pmu_name, &rhs_num); 175 int ret = strncmp(lhs_pmu_name, rhs_pmu_name, 176 lhs_pmu_name_len < rhs_pmu_name_len ? lhs_pmu_name_len : rhs_pmu_name_len); 177 178 if (lhs_pmu_name_len != rhs_pmu_name_len || ret != 0 || lhs_pmu_name_len == 0) 179 return ret; 180 181 return lhs_num < rhs_num ? -1 : (lhs_num > rhs_num ? 1 : 0); 182 } 183 184 /* Add all pmus in sysfs to pmu list: */ 185 static void pmu_read_sysfs(bool core_only) 186 { 187 int fd; 188 DIR *dir; 189 struct dirent *dent; 190 191 if (read_sysfs_all_pmus || (core_only && read_sysfs_core_pmus)) 192 return; 193 194 fd = perf_pmu__event_source_devices_fd(); 195 if (fd < 0) 196 return; 197 198 dir = fdopendir(fd); 199 if (!dir) { 200 close(fd); 201 return; 202 } 203 204 while ((dent = readdir(dir))) { 205 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) 206 continue; 207 if (core_only && !is_pmu_core(dent->d_name)) 208 continue; 209 /* add to static LIST_HEAD(core_pmus) or LIST_HEAD(other_pmus): */ 210 perf_pmu__find2(fd, dent->d_name); 211 } 212 213 closedir(dir); 214 if (list_empty(&core_pmus)) { 215 if (!perf_pmu__create_placeholder_core_pmu(&core_pmus)) 216 pr_err("Failure to set up any core PMUs\n"); 217 } 218 list_sort(NULL, &core_pmus, pmus_cmp); 219 list_sort(NULL, &other_pmus, pmus_cmp); 220 if (!list_empty(&core_pmus)) { 221 read_sysfs_core_pmus = true; 222 if (!core_only) 223 read_sysfs_all_pmus = true; 224 } 225 } 226 227 static struct perf_pmu *__perf_pmus__find_by_type(unsigned int type) 228 { 229 struct perf_pmu *pmu; 230 231 list_for_each_entry(pmu, &core_pmus, list) { 232 if (pmu->type == type) 233 return pmu; 234 } 235 236 list_for_each_entry(pmu, &other_pmus, list) { 237 if (pmu->type == type) 238 return pmu; 239 } 240 return NULL; 241 } 242 243 struct perf_pmu *perf_pmus__find_by_type(unsigned int type) 244 { 245 struct perf_pmu *pmu = __perf_pmus__find_by_type(type); 246 247 if (pmu || read_sysfs_all_pmus) 248 return pmu; 249 250 pmu_read_sysfs(/*core_only=*/false); 251 pmu = __perf_pmus__find_by_type(type); 252 return pmu; 253 } 254 255 /* 256 * pmu iterator: If pmu is NULL, we start at the begin, otherwise return the 257 * next pmu. Returns NULL on end. 258 */ 259 struct perf_pmu *perf_pmus__scan(struct perf_pmu *pmu) 260 { 261 bool use_core_pmus = !pmu || pmu->is_core; 262 263 if (!pmu) { 264 pmu_read_sysfs(/*core_only=*/false); 265 pmu = list_prepare_entry(pmu, &core_pmus, list); 266 } 267 if (use_core_pmus) { 268 list_for_each_entry_continue(pmu, &core_pmus, list) 269 return pmu; 270 271 pmu = NULL; 272 pmu = list_prepare_entry(pmu, &other_pmus, list); 273 } 274 list_for_each_entry_continue(pmu, &other_pmus, list) 275 return pmu; 276 return NULL; 277 } 278 279 struct perf_pmu *perf_pmus__scan_core(struct perf_pmu *pmu) 280 { 281 if (!pmu) { 282 pmu_read_sysfs(/*core_only=*/true); 283 return list_first_entry_or_null(&core_pmus, typeof(*pmu), list); 284 } 285 list_for_each_entry_continue(pmu, &core_pmus, list) 286 return pmu; 287 288 return NULL; 289 } 290 291 static struct perf_pmu *perf_pmus__scan_skip_duplicates(struct perf_pmu *pmu) 292 { 293 bool use_core_pmus = !pmu || pmu->is_core; 294 int last_pmu_name_len = 0; 295 const char *last_pmu_name = (pmu && pmu->name) ? pmu->name : ""; 296 297 if (!pmu) { 298 pmu_read_sysfs(/*core_only=*/false); 299 pmu = list_prepare_entry(pmu, &core_pmus, list); 300 } else 301 last_pmu_name_len = pmu_name_len_no_suffix(pmu->name ?: "", NULL); 302 303 if (use_core_pmus) { 304 list_for_each_entry_continue(pmu, &core_pmus, list) { 305 int pmu_name_len = pmu_name_len_no_suffix(pmu->name ?: "", /*num=*/NULL); 306 307 if (last_pmu_name_len == pmu_name_len && 308 !strncmp(last_pmu_name, pmu->name ?: "", pmu_name_len)) 309 continue; 310 311 return pmu; 312 } 313 pmu = NULL; 314 pmu = list_prepare_entry(pmu, &other_pmus, list); 315 } 316 list_for_each_entry_continue(pmu, &other_pmus, list) { 317 int pmu_name_len = pmu_name_len_no_suffix(pmu->name ?: "", /*num=*/NULL); 318 319 if (last_pmu_name_len == pmu_name_len && 320 !strncmp(last_pmu_name, pmu->name ?: "", pmu_name_len)) 321 continue; 322 323 return pmu; 324 } 325 return NULL; 326 } 327 328 const struct perf_pmu *perf_pmus__pmu_for_pmu_filter(const char *str) 329 { 330 struct perf_pmu *pmu = NULL; 331 332 while ((pmu = perf_pmus__scan(pmu)) != NULL) { 333 if (!strcmp(pmu->name, str)) 334 return pmu; 335 /* Ignore "uncore_" prefix. */ 336 if (!strncmp(pmu->name, "uncore_", 7)) { 337 if (!strcmp(pmu->name + 7, str)) 338 return pmu; 339 } 340 /* Ignore "cpu_" prefix on Intel hybrid PMUs. */ 341 if (!strncmp(pmu->name, "cpu_", 4)) { 342 if (!strcmp(pmu->name + 4, str)) 343 return pmu; 344 } 345 } 346 return NULL; 347 } 348 349 /** Struct for ordering events as output in perf list. */ 350 struct sevent { 351 /** PMU for event. */ 352 const struct perf_pmu *pmu; 353 const char *name; 354 const char* alias; 355 const char *scale_unit; 356 const char *desc; 357 const char *long_desc; 358 const char *encoding_desc; 359 const char *topic; 360 const char *pmu_name; 361 bool deprecated; 362 }; 363 364 static int cmp_sevent(const void *a, const void *b) 365 { 366 const struct sevent *as = a; 367 const struct sevent *bs = b; 368 bool a_iscpu, b_iscpu; 369 int ret; 370 371 /* Put extra events last. */ 372 if (!!as->desc != !!bs->desc) 373 return !!as->desc - !!bs->desc; 374 375 /* Order by topics. */ 376 ret = strcmp(as->topic ?: "", bs->topic ?: ""); 377 if (ret) 378 return ret; 379 380 /* Order CPU core events to be first */ 381 a_iscpu = as->pmu ? as->pmu->is_core : true; 382 b_iscpu = bs->pmu ? bs->pmu->is_core : true; 383 if (a_iscpu != b_iscpu) 384 return a_iscpu ? -1 : 1; 385 386 /* Order by PMU name. */ 387 if (as->pmu != bs->pmu) { 388 ret = strcmp(as->pmu_name ?: "", bs->pmu_name ?: ""); 389 if (ret) 390 return ret; 391 } 392 393 /* Order by event name. */ 394 return strcmp(as->name, bs->name); 395 } 396 397 static bool pmu_alias_is_duplicate(struct sevent *a, struct sevent *b) 398 { 399 /* Different names -> never duplicates */ 400 if (strcmp(a->name ?: "//", b->name ?: "//")) 401 return false; 402 403 /* Don't remove duplicates for different PMUs */ 404 return strcmp(a->pmu_name, b->pmu_name) == 0; 405 } 406 407 struct events_callback_state { 408 struct sevent *aliases; 409 size_t aliases_len; 410 size_t index; 411 }; 412 413 static int perf_pmus__print_pmu_events__callback(void *vstate, 414 struct pmu_event_info *info) 415 { 416 struct events_callback_state *state = vstate; 417 struct sevent *s; 418 419 if (state->index >= state->aliases_len) { 420 pr_err("Unexpected event %s/%s/\n", info->pmu->name, info->name); 421 return 1; 422 } 423 s = &state->aliases[state->index]; 424 s->pmu = info->pmu; 425 #define COPY_STR(str) s->str = info->str ? strdup(info->str) : NULL 426 COPY_STR(name); 427 COPY_STR(alias); 428 COPY_STR(scale_unit); 429 COPY_STR(desc); 430 COPY_STR(long_desc); 431 COPY_STR(encoding_desc); 432 COPY_STR(topic); 433 COPY_STR(pmu_name); 434 #undef COPY_STR 435 s->deprecated = info->deprecated; 436 state->index++; 437 return 0; 438 } 439 440 void perf_pmus__print_pmu_events(const struct print_callbacks *print_cb, void *print_state) 441 { 442 struct perf_pmu *pmu; 443 int printed = 0; 444 int len; 445 struct sevent *aliases; 446 struct events_callback_state state; 447 bool skip_duplicate_pmus = print_cb->skip_duplicate_pmus(print_state); 448 struct perf_pmu *(*scan_fn)(struct perf_pmu *); 449 450 if (skip_duplicate_pmus) 451 scan_fn = perf_pmus__scan_skip_duplicates; 452 else 453 scan_fn = perf_pmus__scan; 454 455 pmu = NULL; 456 len = 0; 457 while ((pmu = scan_fn(pmu)) != NULL) 458 len += perf_pmu__num_events(pmu); 459 460 aliases = zalloc(sizeof(struct sevent) * len); 461 if (!aliases) { 462 pr_err("FATAL: not enough memory to print PMU events\n"); 463 return; 464 } 465 pmu = NULL; 466 state = (struct events_callback_state) { 467 .aliases = aliases, 468 .aliases_len = len, 469 .index = 0, 470 }; 471 while ((pmu = scan_fn(pmu)) != NULL) { 472 perf_pmu__for_each_event(pmu, skip_duplicate_pmus, &state, 473 perf_pmus__print_pmu_events__callback); 474 } 475 qsort(aliases, len, sizeof(struct sevent), cmp_sevent); 476 for (int j = 0; j < len; j++) { 477 /* Skip duplicates */ 478 if (j > 0 && pmu_alias_is_duplicate(&aliases[j], &aliases[j - 1])) 479 continue; 480 481 print_cb->print_event(print_state, 482 aliases[j].pmu_name, 483 aliases[j].topic, 484 aliases[j].name, 485 aliases[j].alias, 486 aliases[j].scale_unit, 487 aliases[j].deprecated, 488 "Kernel PMU event", 489 aliases[j].desc, 490 aliases[j].long_desc, 491 aliases[j].encoding_desc); 492 zfree(&aliases[j].name); 493 zfree(&aliases[j].alias); 494 zfree(&aliases[j].scale_unit); 495 zfree(&aliases[j].desc); 496 zfree(&aliases[j].long_desc); 497 zfree(&aliases[j].encoding_desc); 498 zfree(&aliases[j].topic); 499 zfree(&aliases[j].pmu_name); 500 } 501 if (printed && pager_in_use()) 502 printf("\n"); 503 504 zfree(&aliases); 505 } 506 507 struct build_format_string_args { 508 struct strbuf short_string; 509 struct strbuf long_string; 510 int num_formats; 511 }; 512 513 static int build_format_string(void *state, const char *name, int config, 514 const unsigned long *bits) 515 { 516 struct build_format_string_args *args = state; 517 unsigned int num_bits; 518 int ret1, ret2 = 0; 519 520 (void)config; 521 args->num_formats++; 522 if (args->num_formats > 1) { 523 strbuf_addch(&args->long_string, ','); 524 if (args->num_formats < 4) 525 strbuf_addch(&args->short_string, ','); 526 } 527 num_bits = bits ? bitmap_weight(bits, PERF_PMU_FORMAT_BITS) : 0; 528 if (num_bits <= 1) { 529 ret1 = strbuf_addf(&args->long_string, "%s", name); 530 if (args->num_formats < 4) 531 ret2 = strbuf_addf(&args->short_string, "%s", name); 532 } else if (num_bits > 8) { 533 ret1 = strbuf_addf(&args->long_string, "%s=0..0x%llx", name, 534 ULLONG_MAX >> (64 - num_bits)); 535 if (args->num_formats < 4) { 536 ret2 = strbuf_addf(&args->short_string, "%s=0..0x%llx", name, 537 ULLONG_MAX >> (64 - num_bits)); 538 } 539 } else { 540 ret1 = strbuf_addf(&args->long_string, "%s=0..%llu", name, 541 ULLONG_MAX >> (64 - num_bits)); 542 if (args->num_formats < 4) { 543 ret2 = strbuf_addf(&args->short_string, "%s=0..%llu", name, 544 ULLONG_MAX >> (64 - num_bits)); 545 } 546 } 547 return ret1 < 0 ? ret1 : (ret2 < 0 ? ret2 : 0); 548 } 549 550 void perf_pmus__print_raw_pmu_events(const struct print_callbacks *print_cb, void *print_state) 551 { 552 bool skip_duplicate_pmus = print_cb->skip_duplicate_pmus(print_state); 553 struct perf_pmu *(*scan_fn)(struct perf_pmu *); 554 struct perf_pmu *pmu = NULL; 555 556 if (skip_duplicate_pmus) 557 scan_fn = perf_pmus__scan_skip_duplicates; 558 else 559 scan_fn = perf_pmus__scan; 560 561 while ((pmu = scan_fn(pmu)) != NULL) { 562 struct build_format_string_args format_args = { 563 .short_string = STRBUF_INIT, 564 .long_string = STRBUF_INIT, 565 .num_formats = 0, 566 }; 567 int len = pmu_name_len_no_suffix(pmu->name, /*num=*/NULL); 568 const char *desc = "(see 'man perf-list' or 'man perf-record' on how to encode it)"; 569 570 if (!pmu->is_core) 571 desc = NULL; 572 573 strbuf_addf(&format_args.short_string, "%.*s/", len, pmu->name); 574 strbuf_addf(&format_args.long_string, "%.*s/", len, pmu->name); 575 perf_pmu__for_each_format(pmu, &format_args, build_format_string); 576 577 if (format_args.num_formats > 3) 578 strbuf_addf(&format_args.short_string, ",.../modifier"); 579 else 580 strbuf_addf(&format_args.short_string, "/modifier"); 581 582 strbuf_addf(&format_args.long_string, "/modifier"); 583 print_cb->print_event(print_state, 584 /*topic=*/NULL, 585 /*pmu_name=*/NULL, 586 format_args.short_string.buf, 587 /*event_alias=*/NULL, 588 /*scale_unit=*/NULL, 589 /*deprecated=*/false, 590 "Raw event descriptor", 591 desc, 592 /*long_desc=*/NULL, 593 format_args.long_string.buf); 594 595 strbuf_release(&format_args.short_string); 596 strbuf_release(&format_args.long_string); 597 } 598 } 599 600 bool perf_pmus__have_event(const char *pname, const char *name) 601 { 602 struct perf_pmu *pmu = perf_pmus__find(pname); 603 604 return pmu && perf_pmu__have_event(pmu, name); 605 } 606 607 int perf_pmus__num_core_pmus(void) 608 { 609 static int count; 610 611 if (!count) { 612 struct perf_pmu *pmu = NULL; 613 614 while ((pmu = perf_pmus__scan_core(pmu)) != NULL) 615 count++; 616 } 617 return count; 618 } 619 620 static bool __perf_pmus__supports_extended_type(void) 621 { 622 struct perf_pmu *pmu = NULL; 623 624 if (perf_pmus__num_core_pmus() <= 1) 625 return false; 626 627 while ((pmu = perf_pmus__scan_core(pmu)) != NULL) { 628 if (!is_event_supported(PERF_TYPE_HARDWARE, PERF_COUNT_HW_CPU_CYCLES | ((__u64)pmu->type << PERF_PMU_TYPE_SHIFT))) 629 return false; 630 } 631 632 return true; 633 } 634 635 static bool perf_pmus__do_support_extended_type; 636 637 static void perf_pmus__init_supports_extended_type(void) 638 { 639 perf_pmus__do_support_extended_type = __perf_pmus__supports_extended_type(); 640 } 641 642 bool perf_pmus__supports_extended_type(void) 643 { 644 static pthread_once_t extended_type_once = PTHREAD_ONCE_INIT; 645 646 pthread_once(&extended_type_once, perf_pmus__init_supports_extended_type); 647 648 return perf_pmus__do_support_extended_type; 649 } 650 651 char *perf_pmus__default_pmu_name(void) 652 { 653 int fd; 654 DIR *dir; 655 struct dirent *dent; 656 char *result = NULL; 657 658 if (!list_empty(&core_pmus)) 659 return strdup(list_first_entry(&core_pmus, struct perf_pmu, list)->name); 660 661 fd = perf_pmu__event_source_devices_fd(); 662 if (fd < 0) 663 return strdup("cpu"); 664 665 dir = fdopendir(fd); 666 if (!dir) { 667 close(fd); 668 return strdup("cpu"); 669 } 670 671 while ((dent = readdir(dir))) { 672 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) 673 continue; 674 if (is_pmu_core(dent->d_name)) { 675 result = strdup(dent->d_name); 676 break; 677 } 678 } 679 680 closedir(dir); 681 return result ?: strdup("cpu"); 682 } 683 684 struct perf_pmu *evsel__find_pmu(const struct evsel *evsel) 685 { 686 struct perf_pmu *pmu = evsel->pmu; 687 688 if (!pmu) { 689 pmu = perf_pmus__find_by_type(evsel->core.attr.type); 690 ((struct evsel *)evsel)->pmu = pmu; 691 } 692 return pmu; 693 } 694 695 struct perf_pmu *perf_pmus__find_core_pmu(void) 696 { 697 return perf_pmus__scan_core(NULL); 698 } 699