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