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