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