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