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