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