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