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