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