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