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