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