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