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