1 // SPDX-License-Identifier: GPL-2.0 2 #include <linux/list.h> 3 #include <linux/compiler.h> 4 #include <linux/string.h> 5 #include <linux/zalloc.h> 6 #include <linux/ctype.h> 7 #include <subcmd/pager.h> 8 #include <sys/types.h> 9 #include <errno.h> 10 #include <fcntl.h> 11 #include <sys/stat.h> 12 #include <unistd.h> 13 #include <stdio.h> 14 #include <stdbool.h> 15 #include <stdarg.h> 16 #include <dirent.h> 17 #include <api/fs/fs.h> 18 #include <locale.h> 19 #include <regex.h> 20 #include <perf/cpumap.h> 21 #include <fnmatch.h> 22 #include <math.h> 23 #include "debug.h" 24 #include "evsel.h" 25 #include "pmu.h" 26 #include "pmus.h" 27 #include "parse-events.h" 28 #include "print-events.h" 29 #include "header.h" 30 #include "string2.h" 31 #include "strbuf.h" 32 #include "fncache.h" 33 #include "pmu-hybrid.h" 34 35 struct perf_pmu perf_pmu__fake; 36 37 /** 38 * struct perf_pmu_format - Values from a format file read from 39 * <sysfs>/devices/cpu/format/ held in struct perf_pmu. 40 * 41 * For example, the contents of <sysfs>/devices/cpu/format/event may be 42 * "config:0-7" and will be represented here as name="event", 43 * value=PERF_PMU_FORMAT_VALUE_CONFIG and bits 0 to 7 will be set. 44 */ 45 struct perf_pmu_format { 46 /** @name: The modifier/file name. */ 47 char *name; 48 /** 49 * @value : Which config value the format relates to. Supported values 50 * are from PERF_PMU_FORMAT_VALUE_CONFIG to 51 * PERF_PMU_FORMAT_VALUE_CONFIG_END. 52 */ 53 int value; 54 /** @bits: Which config bits are set by this format value. */ 55 DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS); 56 /** @list: Element on list within struct perf_pmu. */ 57 struct list_head list; 58 }; 59 60 int perf_pmu_parse(struct list_head *list, char *name); 61 extern FILE *perf_pmu_in; 62 63 static bool hybrid_scanned; 64 65 static struct perf_pmu *perf_pmu__find2(int dirfd, const char *name); 66 67 /* 68 * Parse & process all the sysfs attributes located under 69 * the directory specified in 'dir' parameter. 70 */ 71 int perf_pmu__format_parse(int dirfd, struct list_head *head) 72 { 73 struct dirent *evt_ent; 74 DIR *format_dir; 75 int ret = 0; 76 77 format_dir = fdopendir(dirfd); 78 if (!format_dir) 79 return -EINVAL; 80 81 while (!ret && (evt_ent = readdir(format_dir))) { 82 char *name = evt_ent->d_name; 83 int fd; 84 85 if (!strcmp(name, ".") || !strcmp(name, "..")) 86 continue; 87 88 89 ret = -EINVAL; 90 fd = openat(dirfd, name, O_RDONLY); 91 if (fd < 0) 92 break; 93 94 perf_pmu_in = fdopen(fd, "r"); 95 ret = perf_pmu_parse(head, name); 96 fclose(perf_pmu_in); 97 } 98 99 closedir(format_dir); 100 return ret; 101 } 102 103 /* 104 * Reading/parsing the default pmu format definition, which should be 105 * located at: 106 * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes. 107 */ 108 static int pmu_format(int dirfd, const char *name, struct list_head *format) 109 { 110 int fd; 111 112 fd = perf_pmu__pathname_fd(dirfd, name, "format", O_DIRECTORY); 113 if (fd < 0) 114 return 0; 115 116 /* it'll close the fd */ 117 if (perf_pmu__format_parse(fd, format)) 118 return -1; 119 120 return 0; 121 } 122 123 int perf_pmu__convert_scale(const char *scale, char **end, double *sval) 124 { 125 char *lc; 126 int ret = 0; 127 128 /* 129 * save current locale 130 */ 131 lc = setlocale(LC_NUMERIC, NULL); 132 133 /* 134 * The lc string may be allocated in static storage, 135 * so get a dynamic copy to make it survive setlocale 136 * call below. 137 */ 138 lc = strdup(lc); 139 if (!lc) { 140 ret = -ENOMEM; 141 goto out; 142 } 143 144 /* 145 * force to C locale to ensure kernel 146 * scale string is converted correctly. 147 * kernel uses default C locale. 148 */ 149 setlocale(LC_NUMERIC, "C"); 150 151 *sval = strtod(scale, end); 152 153 out: 154 /* restore locale */ 155 setlocale(LC_NUMERIC, lc); 156 free(lc); 157 return ret; 158 } 159 160 static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, int dirfd, char *name) 161 { 162 struct stat st; 163 ssize_t sret; 164 char scale[128]; 165 int fd, ret = -1; 166 char path[PATH_MAX]; 167 168 scnprintf(path, PATH_MAX, "%s.scale", name); 169 170 fd = openat(dirfd, path, O_RDONLY); 171 if (fd == -1) 172 return -1; 173 174 if (fstat(fd, &st) < 0) 175 goto error; 176 177 sret = read(fd, scale, sizeof(scale)-1); 178 if (sret < 0) 179 goto error; 180 181 if (scale[sret - 1] == '\n') 182 scale[sret - 1] = '\0'; 183 else 184 scale[sret] = '\0'; 185 186 ret = perf_pmu__convert_scale(scale, NULL, &alias->scale); 187 error: 188 close(fd); 189 return ret; 190 } 191 192 static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, int dirfd, char *name) 193 { 194 char path[PATH_MAX]; 195 ssize_t sret; 196 int fd; 197 198 scnprintf(path, PATH_MAX, "%s.unit", name); 199 200 fd = openat(dirfd, path, O_RDONLY); 201 if (fd == -1) 202 return -1; 203 204 sret = read(fd, alias->unit, UNIT_MAX_LEN); 205 if (sret < 0) 206 goto error; 207 208 close(fd); 209 210 if (alias->unit[sret - 1] == '\n') 211 alias->unit[sret - 1] = '\0'; 212 else 213 alias->unit[sret] = '\0'; 214 215 return 0; 216 error: 217 close(fd); 218 alias->unit[0] = '\0'; 219 return -1; 220 } 221 222 static int 223 perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, int dirfd, char *name) 224 { 225 char path[PATH_MAX]; 226 int fd; 227 228 scnprintf(path, PATH_MAX, "%s.per-pkg", name); 229 230 fd = openat(dirfd, path, O_RDONLY); 231 if (fd == -1) 232 return -1; 233 234 close(fd); 235 236 alias->per_pkg = true; 237 return 0; 238 } 239 240 static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias, 241 int dirfd, char *name) 242 { 243 char path[PATH_MAX]; 244 int fd; 245 246 scnprintf(path, PATH_MAX, "%s.snapshot", name); 247 248 fd = openat(dirfd, path, O_RDONLY); 249 if (fd == -1) 250 return -1; 251 252 alias->snapshot = true; 253 close(fd); 254 return 0; 255 } 256 257 static void perf_pmu_assign_str(char *name, const char *field, char **old_str, 258 char **new_str) 259 { 260 if (!*old_str) 261 goto set_new; 262 263 if (*new_str) { /* Have new string, check with old */ 264 if (strcasecmp(*old_str, *new_str)) 265 pr_debug("alias %s differs in field '%s'\n", 266 name, field); 267 zfree(old_str); 268 } else /* Nothing new --> keep old string */ 269 return; 270 set_new: 271 *old_str = *new_str; 272 *new_str = NULL; 273 } 274 275 static void perf_pmu_update_alias(struct perf_pmu_alias *old, 276 struct perf_pmu_alias *newalias) 277 { 278 perf_pmu_assign_str(old->name, "desc", &old->desc, &newalias->desc); 279 perf_pmu_assign_str(old->name, "long_desc", &old->long_desc, 280 &newalias->long_desc); 281 perf_pmu_assign_str(old->name, "topic", &old->topic, &newalias->topic); 282 perf_pmu_assign_str(old->name, "value", &old->str, &newalias->str); 283 old->scale = newalias->scale; 284 old->per_pkg = newalias->per_pkg; 285 old->snapshot = newalias->snapshot; 286 memcpy(old->unit, newalias->unit, sizeof(old->unit)); 287 } 288 289 /* Delete an alias entry. */ 290 void perf_pmu_free_alias(struct perf_pmu_alias *newalias) 291 { 292 zfree(&newalias->name); 293 zfree(&newalias->desc); 294 zfree(&newalias->long_desc); 295 zfree(&newalias->topic); 296 zfree(&newalias->str); 297 zfree(&newalias->pmu_name); 298 parse_events_terms__purge(&newalias->terms); 299 free(newalias); 300 } 301 302 static void perf_pmu__del_aliases(struct perf_pmu *pmu) 303 { 304 struct perf_pmu_alias *alias, *tmp; 305 306 list_for_each_entry_safe(alias, tmp, &pmu->aliases, list) { 307 list_del(&alias->list); 308 perf_pmu_free_alias(alias); 309 } 310 } 311 312 /* Merge an alias, search in alias list. If this name is already 313 * present merge both of them to combine all information. 314 */ 315 static bool perf_pmu_merge_alias(struct perf_pmu_alias *newalias, 316 struct list_head *alist) 317 { 318 struct perf_pmu_alias *a; 319 320 list_for_each_entry(a, alist, list) { 321 if (!strcasecmp(newalias->name, a->name)) { 322 if (newalias->pmu_name && a->pmu_name && 323 !strcasecmp(newalias->pmu_name, a->pmu_name)) { 324 continue; 325 } 326 perf_pmu_update_alias(a, newalias); 327 perf_pmu_free_alias(newalias); 328 return true; 329 } 330 } 331 return false; 332 } 333 334 static int __perf_pmu__new_alias(struct list_head *list, int dirfd, char *name, 335 char *desc, char *val, const struct pmu_event *pe) 336 { 337 struct parse_events_term *term; 338 struct perf_pmu_alias *alias; 339 int ret; 340 char newval[256]; 341 char *long_desc = NULL, *topic = NULL, *unit = NULL, *pmu_name = NULL; 342 bool deprecated = false, perpkg = false; 343 344 if (pe) { 345 long_desc = (char *)pe->long_desc; 346 topic = (char *)pe->topic; 347 unit = (char *)pe->unit; 348 perpkg = pe->perpkg; 349 deprecated = pe->deprecated; 350 pmu_name = (char *)pe->pmu; 351 } 352 353 alias = malloc(sizeof(*alias)); 354 if (!alias) 355 return -ENOMEM; 356 357 INIT_LIST_HEAD(&alias->terms); 358 alias->scale = 1.0; 359 alias->unit[0] = '\0'; 360 alias->per_pkg = perpkg; 361 alias->snapshot = false; 362 alias->deprecated = deprecated; 363 364 ret = parse_events_terms(&alias->terms, val); 365 if (ret) { 366 pr_err("Cannot parse alias %s: %d\n", val, ret); 367 free(alias); 368 return ret; 369 } 370 371 /* Scan event and remove leading zeroes, spaces, newlines, some 372 * platforms have terms specified as 373 * event=0x0091 (read from files ../<PMU>/events/<FILE> 374 * and terms specified as event=0x91 (read from JSON files). 375 * 376 * Rebuild string to make alias->str member comparable. 377 */ 378 memset(newval, 0, sizeof(newval)); 379 ret = 0; 380 list_for_each_entry(term, &alias->terms, list) { 381 if (ret) 382 ret += scnprintf(newval + ret, sizeof(newval) - ret, 383 ","); 384 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) 385 ret += scnprintf(newval + ret, sizeof(newval) - ret, 386 "%s=%#x", term->config, term->val.num); 387 else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) 388 ret += scnprintf(newval + ret, sizeof(newval) - ret, 389 "%s=%s", term->config, term->val.str); 390 } 391 392 alias->name = strdup(name); 393 if (dirfd >= 0) { 394 /* 395 * load unit name and scale if available 396 */ 397 perf_pmu__parse_unit(alias, dirfd, name); 398 perf_pmu__parse_scale(alias, dirfd, name); 399 perf_pmu__parse_per_pkg(alias, dirfd, name); 400 perf_pmu__parse_snapshot(alias, dirfd, name); 401 } 402 403 alias->desc = desc ? strdup(desc) : NULL; 404 alias->long_desc = long_desc ? strdup(long_desc) : 405 desc ? strdup(desc) : NULL; 406 alias->topic = topic ? strdup(topic) : NULL; 407 if (unit) { 408 if (perf_pmu__convert_scale(unit, &unit, &alias->scale) < 0) 409 return -1; 410 snprintf(alias->unit, sizeof(alias->unit), "%s", unit); 411 } 412 alias->str = strdup(newval); 413 alias->pmu_name = pmu_name ? strdup(pmu_name) : NULL; 414 415 if (!perf_pmu_merge_alias(alias, list)) 416 list_add_tail(&alias->list, list); 417 418 return 0; 419 } 420 421 static int perf_pmu__new_alias(struct list_head *list, int dirfd, char *name, FILE *file) 422 { 423 char buf[256]; 424 int ret; 425 426 ret = fread(buf, 1, sizeof(buf), file); 427 if (ret == 0) 428 return -EINVAL; 429 430 buf[ret] = 0; 431 432 /* Remove trailing newline from sysfs file */ 433 strim(buf); 434 435 return __perf_pmu__new_alias(list, dirfd, name, NULL, buf, NULL); 436 } 437 438 static inline bool pmu_alias_info_file(char *name) 439 { 440 size_t len; 441 442 len = strlen(name); 443 if (len > 5 && !strcmp(name + len - 5, ".unit")) 444 return true; 445 if (len > 6 && !strcmp(name + len - 6, ".scale")) 446 return true; 447 if (len > 8 && !strcmp(name + len - 8, ".per-pkg")) 448 return true; 449 if (len > 9 && !strcmp(name + len - 9, ".snapshot")) 450 return true; 451 452 return false; 453 } 454 455 /* 456 * Process all the sysfs attributes located under the directory 457 * specified in 'dir' parameter. 458 */ 459 static int pmu_aliases_parse(int dirfd, struct list_head *head) 460 { 461 struct dirent *evt_ent; 462 DIR *event_dir; 463 int fd; 464 465 event_dir = fdopendir(dirfd); 466 if (!event_dir) 467 return -EINVAL; 468 469 while ((evt_ent = readdir(event_dir))) { 470 char *name = evt_ent->d_name; 471 FILE *file; 472 473 if (!strcmp(name, ".") || !strcmp(name, "..")) 474 continue; 475 476 /* 477 * skip info files parsed in perf_pmu__new_alias() 478 */ 479 if (pmu_alias_info_file(name)) 480 continue; 481 482 fd = openat(dirfd, name, O_RDONLY); 483 file = fdopen(fd, "r"); 484 if (!file) { 485 pr_debug("Cannot open %s\n", name); 486 continue; 487 } 488 489 if (perf_pmu__new_alias(head, dirfd, name, file) < 0) 490 pr_debug("Cannot set up %s\n", name); 491 fclose(file); 492 } 493 494 closedir(event_dir); 495 return 0; 496 } 497 498 /* 499 * Reading the pmu event aliases definition, which should be located at: 500 * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes. 501 */ 502 static int pmu_aliases(int dirfd, const char *name, struct list_head *head) 503 { 504 int fd; 505 506 fd = perf_pmu__pathname_fd(dirfd, name, "events", O_DIRECTORY); 507 if (fd < 0) 508 return 0; 509 510 /* it'll close the fd */ 511 if (pmu_aliases_parse(fd, head)) 512 return -1; 513 514 return 0; 515 } 516 517 static int pmu_alias_terms(struct perf_pmu_alias *alias, 518 struct list_head *terms) 519 { 520 struct parse_events_term *term, *cloned; 521 LIST_HEAD(list); 522 int ret; 523 524 list_for_each_entry(term, &alias->terms, list) { 525 ret = parse_events_term__clone(&cloned, term); 526 if (ret) { 527 parse_events_terms__purge(&list); 528 return ret; 529 } 530 /* 531 * Weak terms don't override command line options, 532 * which we don't want for implicit terms in aliases. 533 */ 534 cloned->weak = true; 535 list_add_tail(&cloned->list, &list); 536 } 537 list_splice(&list, terms); 538 return 0; 539 } 540 541 /* Add all pmus in sysfs to pmu list: */ 542 static void pmu_read_sysfs(void) 543 { 544 int fd; 545 DIR *dir; 546 struct dirent *dent; 547 548 fd = perf_pmu__event_source_devices_fd(); 549 if (fd < 0) 550 return; 551 552 dir = fdopendir(fd); 553 if (!dir) 554 return; 555 556 while ((dent = readdir(dir))) { 557 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) 558 continue; 559 /* add to static LIST_HEAD(pmus): */ 560 perf_pmu__find2(fd, dent->d_name); 561 } 562 563 closedir(dir); 564 } 565 566 /* 567 * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64) 568 * may have a "cpus" file. 569 */ 570 static struct perf_cpu_map *pmu_cpumask(int dirfd, const char *name) 571 { 572 struct perf_cpu_map *cpus; 573 const char *templates[] = { 574 "cpumask", 575 "cpus", 576 NULL 577 }; 578 const char **template; 579 char pmu_name[PATH_MAX]; 580 struct perf_pmu pmu = {.name = pmu_name}; 581 FILE *file; 582 583 strlcpy(pmu_name, name, sizeof(pmu_name)); 584 for (template = templates; *template; template++) { 585 file = perf_pmu__open_file_at(&pmu, dirfd, *template); 586 if (!file) 587 continue; 588 cpus = perf_cpu_map__read(file); 589 fclose(file); 590 if (cpus) 591 return cpus; 592 } 593 594 return NULL; 595 } 596 597 static bool pmu_is_uncore(int dirfd, const char *name) 598 { 599 int fd; 600 601 if (perf_pmu__hybrid_mounted(name)) 602 return false; 603 604 fd = perf_pmu__pathname_fd(dirfd, name, "cpumask", O_PATH); 605 if (fd < 0) 606 return false; 607 608 close(fd); 609 return true; 610 } 611 612 static char *pmu_id(const char *name) 613 { 614 char path[PATH_MAX], *str; 615 size_t len; 616 617 perf_pmu__pathname_scnprintf(path, sizeof(path), name, "identifier"); 618 619 if (filename__read_str(path, &str, &len) < 0) 620 return NULL; 621 622 str[len - 1] = 0; /* remove line feed */ 623 624 return str; 625 } 626 627 /* 628 * PMU CORE devices have different name other than cpu in sysfs on some 629 * platforms. 630 * Looking for possible sysfs files to identify the arm core device. 631 */ 632 static int is_arm_pmu_core(const char *name) 633 { 634 char path[PATH_MAX]; 635 636 if (!perf_pmu__pathname_scnprintf(path, sizeof(path), name, "cpus")) 637 return 0; 638 return file_available(path); 639 } 640 641 char *perf_pmu__getcpuid(struct perf_pmu *pmu) 642 { 643 char *cpuid; 644 static bool printed; 645 646 cpuid = getenv("PERF_CPUID"); 647 if (cpuid) 648 cpuid = strdup(cpuid); 649 if (!cpuid) 650 cpuid = get_cpuid_str(pmu); 651 if (!cpuid) 652 return NULL; 653 654 if (!printed) { 655 pr_debug("Using CPUID %s\n", cpuid); 656 printed = true; 657 } 658 return cpuid; 659 } 660 661 __weak const struct pmu_events_table *pmu_events_table__find(void) 662 { 663 return perf_pmu__find_events_table(NULL); 664 } 665 666 __weak const struct pmu_metrics_table *pmu_metrics_table__find(void) 667 { 668 return perf_pmu__find_metrics_table(NULL); 669 } 670 671 /* 672 * Suffix must be in form tok_{digits}, or tok{digits}, or same as pmu_name 673 * to be valid. 674 */ 675 static bool perf_pmu__valid_suffix(const char *pmu_name, char *tok) 676 { 677 const char *p; 678 679 if (strncmp(pmu_name, tok, strlen(tok))) 680 return false; 681 682 p = pmu_name + strlen(tok); 683 if (*p == 0) 684 return true; 685 686 if (*p == '_') 687 ++p; 688 689 /* Ensure we end in a number */ 690 while (1) { 691 if (!isdigit(*p)) 692 return false; 693 if (*(++p) == 0) 694 break; 695 } 696 697 return true; 698 } 699 700 bool pmu_uncore_alias_match(const char *pmu_name, const char *name) 701 { 702 char *tmp = NULL, *tok, *str; 703 bool res; 704 705 str = strdup(pmu_name); 706 if (!str) 707 return false; 708 709 /* 710 * uncore alias may be from different PMU with common prefix 711 */ 712 tok = strtok_r(str, ",", &tmp); 713 if (strncmp(pmu_name, tok, strlen(tok))) { 714 res = false; 715 goto out; 716 } 717 718 /* 719 * Match more complex aliases where the alias name is a comma-delimited 720 * list of tokens, orderly contained in the matching PMU name. 721 * 722 * Example: For alias "socket,pmuname" and PMU "socketX_pmunameY", we 723 * match "socket" in "socketX_pmunameY" and then "pmuname" in 724 * "pmunameY". 725 */ 726 while (1) { 727 char *next_tok = strtok_r(NULL, ",", &tmp); 728 729 name = strstr(name, tok); 730 if (!name || 731 (!next_tok && !perf_pmu__valid_suffix(name, tok))) { 732 res = false; 733 goto out; 734 } 735 if (!next_tok) 736 break; 737 tok = next_tok; 738 name += strlen(tok); 739 } 740 741 res = true; 742 out: 743 free(str); 744 return res; 745 } 746 747 struct pmu_add_cpu_aliases_map_data { 748 struct list_head *head; 749 const char *name; 750 const char *cpu_name; 751 struct perf_pmu *pmu; 752 }; 753 754 static int pmu_add_cpu_aliases_map_callback(const struct pmu_event *pe, 755 const struct pmu_events_table *table __maybe_unused, 756 void *vdata) 757 { 758 struct pmu_add_cpu_aliases_map_data *data = vdata; 759 const char *pname = pe->pmu ? pe->pmu : data->cpu_name; 760 761 if (data->pmu->is_uncore && pmu_uncore_alias_match(pname, data->name)) 762 goto new_alias; 763 764 if (strcmp(pname, data->name)) 765 return 0; 766 767 new_alias: 768 /* need type casts to override 'const' */ 769 __perf_pmu__new_alias(data->head, -1, (char *)pe->name, (char *)pe->desc, 770 (char *)pe->event, pe); 771 return 0; 772 } 773 774 /* 775 * From the pmu_events_map, find the table of PMU events that corresponds 776 * to the current running CPU. Then, add all PMU events from that table 777 * as aliases. 778 */ 779 void pmu_add_cpu_aliases_table(struct list_head *head, struct perf_pmu *pmu, 780 const struct pmu_events_table *table) 781 { 782 struct pmu_add_cpu_aliases_map_data data = { 783 .head = head, 784 .name = pmu->name, 785 .cpu_name = is_arm_pmu_core(pmu->name) ? pmu->name : "cpu", 786 .pmu = pmu, 787 }; 788 789 pmu_events_table_for_each_event(table, pmu_add_cpu_aliases_map_callback, &data); 790 } 791 792 static void pmu_add_cpu_aliases(struct list_head *head, struct perf_pmu *pmu) 793 { 794 const struct pmu_events_table *table; 795 796 table = perf_pmu__find_events_table(pmu); 797 if (!table) 798 return; 799 800 pmu_add_cpu_aliases_table(head, pmu, table); 801 } 802 803 struct pmu_sys_event_iter_data { 804 struct list_head *head; 805 struct perf_pmu *pmu; 806 }; 807 808 static int pmu_add_sys_aliases_iter_fn(const struct pmu_event *pe, 809 const struct pmu_events_table *table __maybe_unused, 810 void *data) 811 { 812 struct pmu_sys_event_iter_data *idata = data; 813 struct perf_pmu *pmu = idata->pmu; 814 815 if (!pe->compat || !pe->pmu) 816 return 0; 817 818 if (!strcmp(pmu->id, pe->compat) && 819 pmu_uncore_alias_match(pe->pmu, pmu->name)) { 820 __perf_pmu__new_alias(idata->head, -1, 821 (char *)pe->name, 822 (char *)pe->desc, 823 (char *)pe->event, 824 pe); 825 } 826 827 return 0; 828 } 829 830 void pmu_add_sys_aliases(struct list_head *head, struct perf_pmu *pmu) 831 { 832 struct pmu_sys_event_iter_data idata = { 833 .head = head, 834 .pmu = pmu, 835 }; 836 837 if (!pmu->id) 838 return; 839 840 pmu_for_each_sys_event(pmu_add_sys_aliases_iter_fn, &idata); 841 } 842 843 struct perf_event_attr * __weak 844 perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused) 845 { 846 return NULL; 847 } 848 849 char * __weak 850 pmu_find_real_name(const char *name) 851 { 852 return (char *)name; 853 } 854 855 char * __weak 856 pmu_find_alias_name(const char *name __maybe_unused) 857 { 858 return NULL; 859 } 860 861 static int pmu_max_precise(int dirfd, struct perf_pmu *pmu) 862 { 863 int max_precise = -1; 864 865 perf_pmu__scan_file_at(pmu, dirfd, "caps/max_precise", "%d", &max_precise); 866 return max_precise; 867 } 868 869 static struct perf_pmu *pmu_lookup(int dirfd, const char *lookup_name) 870 { 871 struct perf_pmu *pmu; 872 LIST_HEAD(format); 873 LIST_HEAD(aliases); 874 __u32 type; 875 char *name = pmu_find_real_name(lookup_name); 876 bool is_hybrid = perf_pmu__hybrid_mounted(name); 877 char *alias_name; 878 879 /* 880 * Check pmu name for hybrid and the pmu may be invalid in sysfs 881 */ 882 if (!strncmp(name, "cpu_", 4) && !is_hybrid) 883 return NULL; 884 885 /* 886 * The pmu data we store & need consists of the pmu 887 * type value and format definitions. Load both right 888 * now. 889 */ 890 if (pmu_format(dirfd, name, &format)) 891 return NULL; 892 893 /* 894 * Check the aliases first to avoid unnecessary work. 895 */ 896 if (pmu_aliases(dirfd, name, &aliases)) 897 return NULL; 898 899 pmu = zalloc(sizeof(*pmu)); 900 if (!pmu) 901 return NULL; 902 903 pmu->cpus = pmu_cpumask(dirfd, name); 904 pmu->name = strdup(name); 905 906 if (!pmu->name) 907 goto err; 908 909 /* Read type, and ensure that type value is successfully assigned (return 1) */ 910 if (perf_pmu__scan_file_at(pmu, dirfd, "type", "%u", &type) != 1) 911 goto err; 912 913 alias_name = pmu_find_alias_name(name); 914 if (alias_name) { 915 pmu->alias_name = strdup(alias_name); 916 if (!pmu->alias_name) 917 goto err; 918 } 919 920 pmu->type = type; 921 pmu->is_uncore = pmu_is_uncore(dirfd, name); 922 if (pmu->is_uncore) 923 pmu->id = pmu_id(name); 924 pmu->max_precise = pmu_max_precise(dirfd, pmu); 925 pmu_add_cpu_aliases(&aliases, pmu); 926 pmu_add_sys_aliases(&aliases, pmu); 927 928 INIT_LIST_HEAD(&pmu->format); 929 INIT_LIST_HEAD(&pmu->aliases); 930 INIT_LIST_HEAD(&pmu->caps); 931 list_splice(&format, &pmu->format); 932 list_splice(&aliases, &pmu->aliases); 933 list_add_tail(&pmu->list, &pmus); 934 935 if (is_hybrid) 936 list_add_tail(&pmu->hybrid_list, &perf_pmu__hybrid_pmus); 937 else 938 INIT_LIST_HEAD(&pmu->hybrid_list); 939 940 pmu->default_config = perf_pmu__get_default_config(pmu); 941 942 return pmu; 943 err: 944 if (pmu->name) 945 free(pmu->name); 946 free(pmu); 947 return NULL; 948 } 949 950 void perf_pmu__warn_invalid_formats(struct perf_pmu *pmu) 951 { 952 struct perf_pmu_format *format; 953 954 /* fake pmu doesn't have format list */ 955 if (pmu == &perf_pmu__fake) 956 return; 957 958 list_for_each_entry(format, &pmu->format, list) 959 if (format->value >= PERF_PMU_FORMAT_VALUE_CONFIG_END) { 960 pr_warning("WARNING: '%s' format '%s' requires 'perf_event_attr::config%d'" 961 "which is not supported by this version of perf!\n", 962 pmu->name, format->name, format->value); 963 return; 964 } 965 } 966 967 static struct perf_pmu *pmu_find(const char *name) 968 { 969 struct perf_pmu *pmu; 970 971 list_for_each_entry(pmu, &pmus, list) { 972 if (!strcmp(pmu->name, name) || 973 (pmu->alias_name && !strcmp(pmu->alias_name, name))) 974 return pmu; 975 } 976 977 return NULL; 978 } 979 980 struct perf_pmu *perf_pmu__find_by_type(unsigned int type) 981 { 982 struct perf_pmu *pmu; 983 984 list_for_each_entry(pmu, &pmus, list) 985 if (pmu->type == type) 986 return pmu; 987 988 return NULL; 989 } 990 991 struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu) 992 { 993 /* 994 * pmu iterator: If pmu is NULL, we start at the begin, 995 * otherwise return the next pmu. Returns NULL on end. 996 */ 997 if (!pmu) { 998 pmu_read_sysfs(); 999 pmu = list_prepare_entry(pmu, &pmus, list); 1000 } 1001 list_for_each_entry_continue(pmu, &pmus, list) 1002 return pmu; 1003 return NULL; 1004 } 1005 1006 struct perf_pmu *evsel__find_pmu(const struct evsel *evsel) 1007 { 1008 struct perf_pmu *pmu = NULL; 1009 1010 if (evsel->pmu) 1011 return evsel->pmu; 1012 1013 while ((pmu = perf_pmu__scan(pmu)) != NULL) { 1014 if (pmu->type == evsel->core.attr.type) 1015 break; 1016 } 1017 1018 ((struct evsel *)evsel)->pmu = pmu; 1019 return pmu; 1020 } 1021 1022 bool evsel__is_aux_event(const struct evsel *evsel) 1023 { 1024 struct perf_pmu *pmu = evsel__find_pmu(evsel); 1025 1026 return pmu && pmu->auxtrace; 1027 } 1028 1029 struct perf_pmu *perf_pmu__find(const char *name) 1030 { 1031 struct perf_pmu *pmu; 1032 int dirfd; 1033 1034 /* 1035 * Once PMU is loaded it stays in the list, 1036 * so we keep us from multiple reading/parsing 1037 * the pmu format definitions. 1038 */ 1039 pmu = pmu_find(name); 1040 if (pmu) 1041 return pmu; 1042 1043 dirfd = perf_pmu__event_source_devices_fd(); 1044 pmu = pmu_lookup(dirfd, name); 1045 close(dirfd); 1046 1047 return pmu; 1048 } 1049 1050 static struct perf_pmu *perf_pmu__find2(int dirfd, const char *name) 1051 { 1052 struct perf_pmu *pmu; 1053 1054 /* 1055 * Once PMU is loaded it stays in the list, 1056 * so we keep us from multiple reading/parsing 1057 * the pmu format definitions. 1058 */ 1059 pmu = pmu_find(name); 1060 if (pmu) 1061 return pmu; 1062 1063 return pmu_lookup(dirfd, name); 1064 } 1065 1066 static struct perf_pmu_format * 1067 pmu_find_format(struct list_head *formats, const char *name) 1068 { 1069 struct perf_pmu_format *format; 1070 1071 list_for_each_entry(format, formats, list) 1072 if (!strcmp(format->name, name)) 1073 return format; 1074 1075 return NULL; 1076 } 1077 1078 __u64 perf_pmu__format_bits(struct list_head *formats, const char *name) 1079 { 1080 struct perf_pmu_format *format = pmu_find_format(formats, name); 1081 __u64 bits = 0; 1082 int fbit; 1083 1084 if (!format) 1085 return 0; 1086 1087 for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS) 1088 bits |= 1ULL << fbit; 1089 1090 return bits; 1091 } 1092 1093 int perf_pmu__format_type(struct list_head *formats, const char *name) 1094 { 1095 struct perf_pmu_format *format = pmu_find_format(formats, name); 1096 1097 if (!format) 1098 return -1; 1099 1100 return format->value; 1101 } 1102 1103 /* 1104 * Sets value based on the format definition (format parameter) 1105 * and unformatted value (value parameter). 1106 */ 1107 static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v, 1108 bool zero) 1109 { 1110 unsigned long fbit, vbit; 1111 1112 for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) { 1113 1114 if (!test_bit(fbit, format)) 1115 continue; 1116 1117 if (value & (1llu << vbit++)) 1118 *v |= (1llu << fbit); 1119 else if (zero) 1120 *v &= ~(1llu << fbit); 1121 } 1122 } 1123 1124 static __u64 pmu_format_max_value(const unsigned long *format) 1125 { 1126 int w; 1127 1128 w = bitmap_weight(format, PERF_PMU_FORMAT_BITS); 1129 if (!w) 1130 return 0; 1131 if (w < 64) 1132 return (1ULL << w) - 1; 1133 return -1; 1134 } 1135 1136 /* 1137 * Term is a string term, and might be a param-term. Try to look up it's value 1138 * in the remaining terms. 1139 * - We have a term like "base-or-format-term=param-term", 1140 * - We need to find the value supplied for "param-term" (with param-term named 1141 * in a config string) later on in the term list. 1142 */ 1143 static int pmu_resolve_param_term(struct parse_events_term *term, 1144 struct list_head *head_terms, 1145 __u64 *value) 1146 { 1147 struct parse_events_term *t; 1148 1149 list_for_each_entry(t, head_terms, list) { 1150 if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM && 1151 t->config && !strcmp(t->config, term->config)) { 1152 t->used = true; 1153 *value = t->val.num; 1154 return 0; 1155 } 1156 } 1157 1158 if (verbose > 0) 1159 printf("Required parameter '%s' not specified\n", term->config); 1160 1161 return -1; 1162 } 1163 1164 static char *pmu_formats_string(struct list_head *formats) 1165 { 1166 struct perf_pmu_format *format; 1167 char *str = NULL; 1168 struct strbuf buf = STRBUF_INIT; 1169 unsigned int i = 0; 1170 1171 if (!formats) 1172 return NULL; 1173 1174 /* sysfs exported terms */ 1175 list_for_each_entry(format, formats, list) 1176 if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0) 1177 goto error; 1178 1179 str = strbuf_detach(&buf, NULL); 1180 error: 1181 strbuf_release(&buf); 1182 1183 return str; 1184 } 1185 1186 /* 1187 * Setup one of config[12] attr members based on the 1188 * user input data - term parameter. 1189 */ 1190 static int pmu_config_term(const char *pmu_name, 1191 struct list_head *formats, 1192 struct perf_event_attr *attr, 1193 struct parse_events_term *term, 1194 struct list_head *head_terms, 1195 bool zero, struct parse_events_error *err) 1196 { 1197 struct perf_pmu_format *format; 1198 __u64 *vp; 1199 __u64 val, max_val; 1200 1201 /* 1202 * If this is a parameter we've already used for parameterized-eval, 1203 * skip it in normal eval. 1204 */ 1205 if (term->used) 1206 return 0; 1207 1208 /* 1209 * Hardcoded terms should be already in, so nothing 1210 * to be done for them. 1211 */ 1212 if (parse_events__is_hardcoded_term(term)) 1213 return 0; 1214 1215 format = pmu_find_format(formats, term->config); 1216 if (!format) { 1217 char *pmu_term = pmu_formats_string(formats); 1218 char *unknown_term; 1219 char *help_msg; 1220 1221 if (asprintf(&unknown_term, 1222 "unknown term '%s' for pmu '%s'", 1223 term->config, pmu_name) < 0) 1224 unknown_term = NULL; 1225 help_msg = parse_events_formats_error_string(pmu_term); 1226 if (err) { 1227 parse_events_error__handle(err, term->err_term, 1228 unknown_term, 1229 help_msg); 1230 } else { 1231 pr_debug("%s (%s)\n", unknown_term, help_msg); 1232 free(unknown_term); 1233 } 1234 free(pmu_term); 1235 return -EINVAL; 1236 } 1237 1238 switch (format->value) { 1239 case PERF_PMU_FORMAT_VALUE_CONFIG: 1240 vp = &attr->config; 1241 break; 1242 case PERF_PMU_FORMAT_VALUE_CONFIG1: 1243 vp = &attr->config1; 1244 break; 1245 case PERF_PMU_FORMAT_VALUE_CONFIG2: 1246 vp = &attr->config2; 1247 break; 1248 case PERF_PMU_FORMAT_VALUE_CONFIG3: 1249 vp = &attr->config3; 1250 break; 1251 default: 1252 return -EINVAL; 1253 } 1254 1255 /* 1256 * Either directly use a numeric term, or try to translate string terms 1257 * using event parameters. 1258 */ 1259 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { 1260 if (term->no_value && 1261 bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) { 1262 if (err) { 1263 parse_events_error__handle(err, term->err_val, 1264 strdup("no value assigned for term"), 1265 NULL); 1266 } 1267 return -EINVAL; 1268 } 1269 1270 val = term->val.num; 1271 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) { 1272 if (strcmp(term->val.str, "?")) { 1273 if (verbose > 0) { 1274 pr_info("Invalid sysfs entry %s=%s\n", 1275 term->config, term->val.str); 1276 } 1277 if (err) { 1278 parse_events_error__handle(err, term->err_val, 1279 strdup("expected numeric value"), 1280 NULL); 1281 } 1282 return -EINVAL; 1283 } 1284 1285 if (pmu_resolve_param_term(term, head_terms, &val)) 1286 return -EINVAL; 1287 } else 1288 return -EINVAL; 1289 1290 max_val = pmu_format_max_value(format->bits); 1291 if (val > max_val) { 1292 if (err) { 1293 char *err_str; 1294 1295 parse_events_error__handle(err, term->err_val, 1296 asprintf(&err_str, 1297 "value too big for format, maximum is %llu", 1298 (unsigned long long)max_val) < 0 1299 ? strdup("value too big for format") 1300 : err_str, 1301 NULL); 1302 return -EINVAL; 1303 } 1304 /* 1305 * Assume we don't care if !err, in which case the value will be 1306 * silently truncated. 1307 */ 1308 } 1309 1310 pmu_format_value(format->bits, val, vp, zero); 1311 return 0; 1312 } 1313 1314 int perf_pmu__config_terms(const char *pmu_name, struct list_head *formats, 1315 struct perf_event_attr *attr, 1316 struct list_head *head_terms, 1317 bool zero, struct parse_events_error *err) 1318 { 1319 struct parse_events_term *term; 1320 1321 list_for_each_entry(term, head_terms, list) { 1322 if (pmu_config_term(pmu_name, formats, attr, term, head_terms, 1323 zero, err)) 1324 return -EINVAL; 1325 } 1326 1327 return 0; 1328 } 1329 1330 /* 1331 * Configures event's 'attr' parameter based on the: 1332 * 1) users input - specified in terms parameter 1333 * 2) pmu format definitions - specified by pmu parameter 1334 */ 1335 int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr, 1336 struct list_head *head_terms, 1337 struct parse_events_error *err) 1338 { 1339 bool zero = !!pmu->default_config; 1340 1341 attr->type = pmu->type; 1342 return perf_pmu__config_terms(pmu->name, &pmu->format, attr, 1343 head_terms, zero, err); 1344 } 1345 1346 static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu, 1347 struct parse_events_term *term) 1348 { 1349 struct perf_pmu_alias *alias; 1350 char *name; 1351 1352 if (parse_events__is_hardcoded_term(term)) 1353 return NULL; 1354 1355 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { 1356 if (term->val.num != 1) 1357 return NULL; 1358 if (pmu_find_format(&pmu->format, term->config)) 1359 return NULL; 1360 name = term->config; 1361 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) { 1362 if (strcasecmp(term->config, "event")) 1363 return NULL; 1364 name = term->val.str; 1365 } else { 1366 return NULL; 1367 } 1368 1369 list_for_each_entry(alias, &pmu->aliases, list) { 1370 if (!strcasecmp(alias->name, name)) 1371 return alias; 1372 } 1373 return NULL; 1374 } 1375 1376 1377 static int check_info_data(struct perf_pmu_alias *alias, 1378 struct perf_pmu_info *info) 1379 { 1380 /* 1381 * Only one term in event definition can 1382 * define unit, scale and snapshot, fail 1383 * if there's more than one. 1384 */ 1385 if ((info->unit && alias->unit[0]) || 1386 (info->scale && alias->scale) || 1387 (info->snapshot && alias->snapshot)) 1388 return -EINVAL; 1389 1390 if (alias->unit[0]) 1391 info->unit = alias->unit; 1392 1393 if (alias->scale) 1394 info->scale = alias->scale; 1395 1396 if (alias->snapshot) 1397 info->snapshot = alias->snapshot; 1398 1399 return 0; 1400 } 1401 1402 /* 1403 * Find alias in the terms list and replace it with the terms 1404 * defined for the alias 1405 */ 1406 int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms, 1407 struct perf_pmu_info *info) 1408 { 1409 struct parse_events_term *term, *h; 1410 struct perf_pmu_alias *alias; 1411 int ret; 1412 1413 info->per_pkg = false; 1414 1415 /* 1416 * Mark unit and scale as not set 1417 * (different from default values, see below) 1418 */ 1419 info->unit = NULL; 1420 info->scale = 0.0; 1421 info->snapshot = false; 1422 1423 list_for_each_entry_safe(term, h, head_terms, list) { 1424 alias = pmu_find_alias(pmu, term); 1425 if (!alias) 1426 continue; 1427 ret = pmu_alias_terms(alias, &term->list); 1428 if (ret) 1429 return ret; 1430 1431 ret = check_info_data(alias, info); 1432 if (ret) 1433 return ret; 1434 1435 if (alias->per_pkg) 1436 info->per_pkg = true; 1437 1438 list_del_init(&term->list); 1439 parse_events_term__delete(term); 1440 } 1441 1442 /* 1443 * if no unit or scale found in aliases, then 1444 * set defaults as for evsel 1445 * unit cannot left to NULL 1446 */ 1447 if (info->unit == NULL) 1448 info->unit = ""; 1449 1450 if (info->scale == 0.0) 1451 info->scale = 1.0; 1452 1453 return 0; 1454 } 1455 1456 int perf_pmu__new_format(struct list_head *list, char *name, 1457 int config, unsigned long *bits) 1458 { 1459 struct perf_pmu_format *format; 1460 1461 format = zalloc(sizeof(*format)); 1462 if (!format) 1463 return -ENOMEM; 1464 1465 format->name = strdup(name); 1466 format->value = config; 1467 memcpy(format->bits, bits, sizeof(format->bits)); 1468 1469 list_add_tail(&format->list, list); 1470 return 0; 1471 } 1472 1473 void perf_pmu__set_format(unsigned long *bits, long from, long to) 1474 { 1475 long b; 1476 1477 if (!to) 1478 to = from; 1479 1480 memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS)); 1481 for (b = from; b <= to; b++) 1482 __set_bit(b, bits); 1483 } 1484 1485 void perf_pmu__del_formats(struct list_head *formats) 1486 { 1487 struct perf_pmu_format *fmt, *tmp; 1488 1489 list_for_each_entry_safe(fmt, tmp, formats, list) { 1490 list_del(&fmt->list); 1491 free(fmt->name); 1492 free(fmt); 1493 } 1494 } 1495 1496 static int sub_non_neg(int a, int b) 1497 { 1498 if (b > a) 1499 return 0; 1500 return a - b; 1501 } 1502 1503 static char *format_alias(char *buf, int len, const struct perf_pmu *pmu, 1504 const struct perf_pmu_alias *alias) 1505 { 1506 struct parse_events_term *term; 1507 int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name); 1508 1509 list_for_each_entry(term, &alias->terms, list) { 1510 if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) 1511 used += snprintf(buf + used, sub_non_neg(len, used), 1512 ",%s=%s", term->config, 1513 term->val.str); 1514 } 1515 1516 if (sub_non_neg(len, used) > 0) { 1517 buf[used] = '/'; 1518 used++; 1519 } 1520 if (sub_non_neg(len, used) > 0) { 1521 buf[used] = '\0'; 1522 used++; 1523 } else 1524 buf[len - 1] = '\0'; 1525 1526 return buf; 1527 } 1528 1529 /** Struct for ordering events as output in perf list. */ 1530 struct sevent { 1531 /** PMU for event. */ 1532 const struct perf_pmu *pmu; 1533 /** 1534 * Optional event for name, desc, etc. If not present then this is a 1535 * selectable PMU and the event name is shown as "//". 1536 */ 1537 const struct perf_pmu_alias *event; 1538 /** Is the PMU for the CPU? */ 1539 bool is_cpu; 1540 }; 1541 1542 static int cmp_sevent(const void *a, const void *b) 1543 { 1544 const struct sevent *as = a; 1545 const struct sevent *bs = b; 1546 const char *a_pmu_name, *b_pmu_name; 1547 const char *a_name = "//", *a_desc = NULL, *a_topic = ""; 1548 const char *b_name = "//", *b_desc = NULL, *b_topic = ""; 1549 int ret; 1550 1551 if (as->event) { 1552 a_name = as->event->name; 1553 a_desc = as->event->desc; 1554 a_topic = as->event->topic ?: ""; 1555 } 1556 if (bs->event) { 1557 b_name = bs->event->name; 1558 b_desc = bs->event->desc; 1559 b_topic = bs->event->topic ?: ""; 1560 } 1561 /* Put extra events last. */ 1562 if (!!a_desc != !!b_desc) 1563 return !!a_desc - !!b_desc; 1564 1565 /* Order by topics. */ 1566 ret = strcmp(a_topic, b_topic); 1567 if (ret) 1568 return ret; 1569 1570 /* Order CPU core events to be first */ 1571 if (as->is_cpu != bs->is_cpu) 1572 return as->is_cpu ? -1 : 1; 1573 1574 /* Order by PMU name. */ 1575 a_pmu_name = as->pmu->name ?: ""; 1576 b_pmu_name = bs->pmu->name ?: ""; 1577 ret = strcmp(a_pmu_name, b_pmu_name); 1578 if (ret) 1579 return ret; 1580 1581 /* Order by event name. */ 1582 return strcmp(a_name, b_name); 1583 } 1584 1585 bool is_pmu_core(const char *name) 1586 { 1587 return !strcmp(name, "cpu") || is_arm_pmu_core(name); 1588 } 1589 1590 static bool pmu_alias_is_duplicate(struct sevent *alias_a, 1591 struct sevent *alias_b) 1592 { 1593 const char *a_pmu_name, *b_pmu_name; 1594 const char *a_name = alias_a->event ? alias_a->event->name : "//"; 1595 const char *b_name = alias_b->event ? alias_b->event->name : "//"; 1596 1597 /* Different names -> never duplicates */ 1598 if (strcmp(a_name, b_name)) 1599 return false; 1600 1601 /* Don't remove duplicates for different PMUs */ 1602 a_pmu_name = alias_a->pmu->name ?: ""; 1603 b_pmu_name = alias_b->pmu->name ?: ""; 1604 return strcmp(a_pmu_name, b_pmu_name) == 0; 1605 } 1606 1607 void print_pmu_events(const struct print_callbacks *print_cb, void *print_state) 1608 { 1609 struct perf_pmu *pmu; 1610 struct perf_pmu_alias *event; 1611 char buf[1024]; 1612 int printed = 0; 1613 int len, j; 1614 struct sevent *aliases; 1615 1616 pmu = NULL; 1617 len = 0; 1618 while ((pmu = perf_pmu__scan(pmu)) != NULL) { 1619 list_for_each_entry(event, &pmu->aliases, list) 1620 len++; 1621 if (pmu->selectable) 1622 len++; 1623 } 1624 aliases = zalloc(sizeof(struct sevent) * len); 1625 if (!aliases) { 1626 pr_err("FATAL: not enough memory to print PMU events\n"); 1627 return; 1628 } 1629 pmu = NULL; 1630 j = 0; 1631 while ((pmu = perf_pmu__scan(pmu)) != NULL) { 1632 bool is_cpu = is_pmu_core(pmu->name) || perf_pmu__is_hybrid(pmu->name); 1633 1634 list_for_each_entry(event, &pmu->aliases, list) { 1635 aliases[j].event = event; 1636 aliases[j].pmu = pmu; 1637 aliases[j].is_cpu = is_cpu; 1638 j++; 1639 } 1640 if (pmu->selectable) { 1641 aliases[j].event = NULL; 1642 aliases[j].pmu = pmu; 1643 aliases[j].is_cpu = is_cpu; 1644 j++; 1645 } 1646 } 1647 len = j; 1648 qsort(aliases, len, sizeof(struct sevent), cmp_sevent); 1649 for (j = 0; j < len; j++) { 1650 const char *name, *alias = NULL, *scale_unit = NULL, 1651 *desc = NULL, *long_desc = NULL, 1652 *encoding_desc = NULL, *topic = NULL; 1653 bool deprecated = false; 1654 size_t buf_used; 1655 1656 /* Skip duplicates */ 1657 if (j > 0 && pmu_alias_is_duplicate(&aliases[j], &aliases[j - 1])) 1658 continue; 1659 1660 if (!aliases[j].event) { 1661 /* A selectable event. */ 1662 buf_used = snprintf(buf, sizeof(buf), "%s//", aliases[j].pmu->name) + 1; 1663 name = buf; 1664 } else { 1665 if (aliases[j].event->desc) { 1666 name = aliases[j].event->name; 1667 buf_used = 0; 1668 } else { 1669 name = format_alias(buf, sizeof(buf), aliases[j].pmu, 1670 aliases[j].event); 1671 if (aliases[j].is_cpu) { 1672 alias = name; 1673 name = aliases[j].event->name; 1674 } 1675 buf_used = strlen(buf) + 1; 1676 } 1677 if (strlen(aliases[j].event->unit) || aliases[j].event->scale != 1.0) { 1678 scale_unit = buf + buf_used; 1679 buf_used += snprintf(buf + buf_used, sizeof(buf) - buf_used, 1680 "%G%s", aliases[j].event->scale, 1681 aliases[j].event->unit) + 1; 1682 } 1683 desc = aliases[j].event->desc; 1684 long_desc = aliases[j].event->long_desc; 1685 topic = aliases[j].event->topic; 1686 encoding_desc = buf + buf_used; 1687 buf_used += snprintf(buf + buf_used, sizeof(buf) - buf_used, 1688 "%s/%s/", aliases[j].pmu->name, 1689 aliases[j].event->str) + 1; 1690 deprecated = aliases[j].event->deprecated; 1691 } 1692 print_cb->print_event(print_state, 1693 aliases[j].pmu->name, 1694 topic, 1695 name, 1696 alias, 1697 scale_unit, 1698 deprecated, 1699 "Kernel PMU event", 1700 desc, 1701 long_desc, 1702 encoding_desc); 1703 } 1704 if (printed && pager_in_use()) 1705 printf("\n"); 1706 1707 zfree(&aliases); 1708 return; 1709 } 1710 1711 bool pmu_have_event(const char *pname, const char *name) 1712 { 1713 struct perf_pmu *pmu; 1714 struct perf_pmu_alias *alias; 1715 1716 pmu = NULL; 1717 while ((pmu = perf_pmu__scan(pmu)) != NULL) { 1718 if (strcmp(pname, pmu->name)) 1719 continue; 1720 list_for_each_entry(alias, &pmu->aliases, list) 1721 if (!strcmp(alias->name, name)) 1722 return true; 1723 } 1724 return false; 1725 } 1726 1727 FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name) 1728 { 1729 char path[PATH_MAX]; 1730 1731 if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, name) || 1732 !file_available(path)) 1733 return NULL; 1734 1735 return fopen(path, "r"); 1736 } 1737 1738 FILE *perf_pmu__open_file_at(struct perf_pmu *pmu, int dirfd, const char *name) 1739 { 1740 int fd; 1741 1742 fd = perf_pmu__pathname_fd(dirfd, pmu->name, name, O_RDONLY); 1743 if (fd < 0) 1744 return NULL; 1745 1746 return fdopen(fd, "r"); 1747 } 1748 1749 int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt, 1750 ...) 1751 { 1752 va_list args; 1753 FILE *file; 1754 int ret = EOF; 1755 1756 va_start(args, fmt); 1757 file = perf_pmu__open_file(pmu, name); 1758 if (file) { 1759 ret = vfscanf(file, fmt, args); 1760 fclose(file); 1761 } 1762 va_end(args); 1763 return ret; 1764 } 1765 1766 int perf_pmu__scan_file_at(struct perf_pmu *pmu, int dirfd, const char *name, 1767 const char *fmt, ...) 1768 { 1769 va_list args; 1770 FILE *file; 1771 int ret = EOF; 1772 1773 va_start(args, fmt); 1774 file = perf_pmu__open_file_at(pmu, dirfd, name); 1775 if (file) { 1776 ret = vfscanf(file, fmt, args); 1777 fclose(file); 1778 } 1779 va_end(args); 1780 return ret; 1781 } 1782 1783 bool perf_pmu__file_exists(struct perf_pmu *pmu, const char *name) 1784 { 1785 char path[PATH_MAX]; 1786 1787 if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, name)) 1788 return false; 1789 1790 return file_available(path); 1791 } 1792 1793 static int perf_pmu__new_caps(struct list_head *list, char *name, char *value) 1794 { 1795 struct perf_pmu_caps *caps = zalloc(sizeof(*caps)); 1796 1797 if (!caps) 1798 return -ENOMEM; 1799 1800 caps->name = strdup(name); 1801 if (!caps->name) 1802 goto free_caps; 1803 caps->value = strndup(value, strlen(value) - 1); 1804 if (!caps->value) 1805 goto free_name; 1806 list_add_tail(&caps->list, list); 1807 return 0; 1808 1809 free_name: 1810 zfree(caps->name); 1811 free_caps: 1812 free(caps); 1813 1814 return -ENOMEM; 1815 } 1816 1817 static void perf_pmu__del_caps(struct perf_pmu *pmu) 1818 { 1819 struct perf_pmu_caps *caps, *tmp; 1820 1821 list_for_each_entry_safe(caps, tmp, &pmu->caps, list) { 1822 list_del(&caps->list); 1823 free(caps->name); 1824 free(caps->value); 1825 free(caps); 1826 } 1827 } 1828 1829 /* 1830 * Reading/parsing the given pmu capabilities, which should be located at: 1831 * /sys/bus/event_source/devices/<dev>/caps as sysfs group attributes. 1832 * Return the number of capabilities 1833 */ 1834 int perf_pmu__caps_parse(struct perf_pmu *pmu) 1835 { 1836 struct stat st; 1837 char caps_path[PATH_MAX]; 1838 DIR *caps_dir; 1839 struct dirent *evt_ent; 1840 int caps_fd; 1841 1842 if (pmu->caps_initialized) 1843 return pmu->nr_caps; 1844 1845 pmu->nr_caps = 0; 1846 1847 if (!perf_pmu__pathname_scnprintf(caps_path, sizeof(caps_path), pmu->name, "caps")) 1848 return -1; 1849 1850 if (stat(caps_path, &st) < 0) { 1851 pmu->caps_initialized = true; 1852 return 0; /* no error if caps does not exist */ 1853 } 1854 1855 caps_dir = opendir(caps_path); 1856 if (!caps_dir) 1857 return -EINVAL; 1858 1859 caps_fd = dirfd(caps_dir); 1860 1861 while ((evt_ent = readdir(caps_dir)) != NULL) { 1862 char *name = evt_ent->d_name; 1863 char value[128]; 1864 FILE *file; 1865 int fd; 1866 1867 if (!strcmp(name, ".") || !strcmp(name, "..")) 1868 continue; 1869 1870 fd = openat(caps_fd, name, O_RDONLY); 1871 file = fdopen(fd, "r"); 1872 if (!file) 1873 continue; 1874 1875 if (!fgets(value, sizeof(value), file) || 1876 (perf_pmu__new_caps(&pmu->caps, name, value) < 0)) { 1877 fclose(file); 1878 continue; 1879 } 1880 1881 pmu->nr_caps++; 1882 fclose(file); 1883 } 1884 1885 closedir(caps_dir); 1886 1887 pmu->caps_initialized = true; 1888 return pmu->nr_caps; 1889 } 1890 1891 void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config, 1892 const char *name) 1893 { 1894 struct perf_pmu_format *format; 1895 __u64 masks = 0, bits; 1896 char buf[100]; 1897 unsigned int i; 1898 1899 list_for_each_entry(format, &pmu->format, list) { 1900 if (format->value != PERF_PMU_FORMAT_VALUE_CONFIG) 1901 continue; 1902 1903 for_each_set_bit(i, format->bits, PERF_PMU_FORMAT_BITS) 1904 masks |= 1ULL << i; 1905 } 1906 1907 /* 1908 * Kernel doesn't export any valid format bits. 1909 */ 1910 if (masks == 0) 1911 return; 1912 1913 bits = config & ~masks; 1914 if (bits == 0) 1915 return; 1916 1917 bitmap_scnprintf((unsigned long *)&bits, sizeof(bits) * 8, buf, sizeof(buf)); 1918 1919 pr_warning("WARNING: event '%s' not valid (bits %s of config " 1920 "'%llx' not supported by kernel)!\n", 1921 name ?: "N/A", buf, config); 1922 } 1923 1924 bool perf_pmu__has_hybrid(void) 1925 { 1926 if (!hybrid_scanned) { 1927 hybrid_scanned = true; 1928 perf_pmu__scan(NULL); 1929 } 1930 1931 return !list_empty(&perf_pmu__hybrid_pmus); 1932 } 1933 1934 int perf_pmu__match(char *pattern, char *name, char *tok) 1935 { 1936 if (!name) 1937 return -1; 1938 1939 if (fnmatch(pattern, name, 0)) 1940 return -1; 1941 1942 if (tok && !perf_pmu__valid_suffix(name, tok)) 1943 return -1; 1944 1945 return 0; 1946 } 1947 1948 int perf_pmu__cpus_match(struct perf_pmu *pmu, struct perf_cpu_map *cpus, 1949 struct perf_cpu_map **mcpus_ptr, 1950 struct perf_cpu_map **ucpus_ptr) 1951 { 1952 struct perf_cpu_map *pmu_cpus = pmu->cpus; 1953 struct perf_cpu_map *matched_cpus, *unmatched_cpus; 1954 struct perf_cpu cpu; 1955 int i, matched_nr = 0, unmatched_nr = 0; 1956 1957 matched_cpus = perf_cpu_map__default_new(); 1958 if (!matched_cpus) 1959 return -1; 1960 1961 unmatched_cpus = perf_cpu_map__default_new(); 1962 if (!unmatched_cpus) { 1963 perf_cpu_map__put(matched_cpus); 1964 return -1; 1965 } 1966 1967 perf_cpu_map__for_each_cpu(cpu, i, cpus) { 1968 if (!perf_cpu_map__has(pmu_cpus, cpu)) 1969 unmatched_cpus->map[unmatched_nr++] = cpu; 1970 else 1971 matched_cpus->map[matched_nr++] = cpu; 1972 } 1973 1974 unmatched_cpus->nr = unmatched_nr; 1975 matched_cpus->nr = matched_nr; 1976 *mcpus_ptr = matched_cpus; 1977 *ucpus_ptr = unmatched_cpus; 1978 return 0; 1979 } 1980 1981 double __weak perf_pmu__cpu_slots_per_cycle(void) 1982 { 1983 return NAN; 1984 } 1985 1986 int perf_pmu__event_source_devices_scnprintf(char *pathname, size_t size) 1987 { 1988 const char *sysfs = sysfs__mountpoint(); 1989 1990 if (!sysfs) 1991 return 0; 1992 return scnprintf(pathname, size, "%s/bus/event_source/devices/", sysfs); 1993 } 1994 1995 int perf_pmu__event_source_devices_fd(void) 1996 { 1997 char path[PATH_MAX]; 1998 const char *sysfs = sysfs__mountpoint(); 1999 2000 if (!sysfs) 2001 return -1; 2002 2003 scnprintf(path, sizeof(path), "%s/bus/event_source/devices/", sysfs); 2004 return open(path, O_DIRECTORY); 2005 } 2006 2007 /* 2008 * Fill 'buf' with the path to a file or folder in 'pmu_name' in 2009 * sysfs. For example if pmu_name = "cs_etm" and 'filename' = "format" 2010 * then pathname will be filled with 2011 * "/sys/bus/event_source/devices/cs_etm/format" 2012 * 2013 * Return 0 if the sysfs mountpoint couldn't be found or if no 2014 * characters were written. 2015 */ 2016 int perf_pmu__pathname_scnprintf(char *buf, size_t size, 2017 const char *pmu_name, const char *filename) 2018 { 2019 char base_path[PATH_MAX]; 2020 2021 if (!perf_pmu__event_source_devices_scnprintf(base_path, sizeof(base_path))) 2022 return 0; 2023 return scnprintf(buf, size, "%s%s/%s", base_path, pmu_name, filename); 2024 } 2025 2026 int perf_pmu__pathname_fd(int dirfd, const char *pmu_name, const char *filename, int flags) 2027 { 2028 char path[PATH_MAX]; 2029 2030 scnprintf(path, sizeof(path), "%s/%s", pmu_name, filename); 2031 return openat(dirfd, path, flags); 2032 } 2033 2034 static void perf_pmu__delete(struct perf_pmu *pmu) 2035 { 2036 perf_pmu__del_formats(&pmu->format); 2037 perf_pmu__del_aliases(pmu); 2038 perf_pmu__del_caps(pmu); 2039 2040 perf_cpu_map__put(pmu->cpus); 2041 2042 free(pmu->default_config); 2043 free(pmu->name); 2044 free(pmu->alias_name); 2045 free(pmu); 2046 } 2047 2048 void perf_pmu__destroy(void) 2049 { 2050 struct perf_pmu *pmu, *tmp; 2051 2052 list_for_each_entry_safe(pmu, tmp, &pmus, list) { 2053 list_del(&pmu->list); 2054 list_del(&pmu->hybrid_list); 2055 2056 perf_pmu__delete(pmu); 2057 } 2058 } 2059