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