1 #include <linux/list.h> 2 #include <linux/compiler.h> 3 #include <sys/types.h> 4 #include <unistd.h> 5 #include <stdio.h> 6 #include <stdbool.h> 7 #include <stdarg.h> 8 #include <dirent.h> 9 #include <api/fs/fs.h> 10 #include <locale.h> 11 #include "util.h" 12 #include "pmu.h" 13 #include "parse-events.h" 14 #include "cpumap.h" 15 #include "header.h" 16 #include "pmu-events/pmu-events.h" 17 #include "cache.h" 18 19 struct perf_pmu_format { 20 char *name; 21 int value; 22 DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS); 23 struct list_head list; 24 }; 25 26 #define EVENT_SOURCE_DEVICE_PATH "/bus/event_source/devices/" 27 28 int perf_pmu_parse(struct list_head *list, char *name); 29 extern FILE *perf_pmu_in; 30 31 static LIST_HEAD(pmus); 32 33 /* 34 * Parse & process all the sysfs attributes located under 35 * the directory specified in 'dir' parameter. 36 */ 37 int perf_pmu__format_parse(char *dir, struct list_head *head) 38 { 39 struct dirent *evt_ent; 40 DIR *format_dir; 41 int ret = 0; 42 43 format_dir = opendir(dir); 44 if (!format_dir) 45 return -EINVAL; 46 47 while (!ret && (evt_ent = readdir(format_dir))) { 48 char path[PATH_MAX]; 49 char *name = evt_ent->d_name; 50 FILE *file; 51 52 if (!strcmp(name, ".") || !strcmp(name, "..")) 53 continue; 54 55 snprintf(path, PATH_MAX, "%s/%s", dir, name); 56 57 ret = -EINVAL; 58 file = fopen(path, "r"); 59 if (!file) 60 break; 61 62 perf_pmu_in = file; 63 ret = perf_pmu_parse(head, name); 64 fclose(file); 65 } 66 67 closedir(format_dir); 68 return ret; 69 } 70 71 /* 72 * Reading/parsing the default pmu format definition, which should be 73 * located at: 74 * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes. 75 */ 76 static int pmu_format(const char *name, struct list_head *format) 77 { 78 struct stat st; 79 char path[PATH_MAX]; 80 const char *sysfs = sysfs__mountpoint(); 81 82 if (!sysfs) 83 return -1; 84 85 snprintf(path, PATH_MAX, 86 "%s" EVENT_SOURCE_DEVICE_PATH "%s/format", sysfs, name); 87 88 if (stat(path, &st) < 0) 89 return 0; /* no error if format does not exist */ 90 91 if (perf_pmu__format_parse(path, format)) 92 return -1; 93 94 return 0; 95 } 96 97 static int convert_scale(const char *scale, char **end, double *sval) 98 { 99 char *lc; 100 int ret = 0; 101 102 /* 103 * save current locale 104 */ 105 lc = setlocale(LC_NUMERIC, NULL); 106 107 /* 108 * The lc string may be allocated in static storage, 109 * so get a dynamic copy to make it survive setlocale 110 * call below. 111 */ 112 lc = strdup(lc); 113 if (!lc) { 114 ret = -ENOMEM; 115 goto out; 116 } 117 118 /* 119 * force to C locale to ensure kernel 120 * scale string is converted correctly. 121 * kernel uses default C locale. 122 */ 123 setlocale(LC_NUMERIC, "C"); 124 125 *sval = strtod(scale, end); 126 127 out: 128 /* restore locale */ 129 setlocale(LC_NUMERIC, lc); 130 free(lc); 131 return ret; 132 } 133 134 static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *name) 135 { 136 struct stat st; 137 ssize_t sret; 138 char scale[128]; 139 int fd, ret = -1; 140 char path[PATH_MAX]; 141 142 snprintf(path, PATH_MAX, "%s/%s.scale", dir, name); 143 144 fd = open(path, O_RDONLY); 145 if (fd == -1) 146 return -1; 147 148 if (fstat(fd, &st) < 0) 149 goto error; 150 151 sret = read(fd, scale, sizeof(scale)-1); 152 if (sret < 0) 153 goto error; 154 155 if (scale[sret - 1] == '\n') 156 scale[sret - 1] = '\0'; 157 else 158 scale[sret] = '\0'; 159 160 ret = convert_scale(scale, NULL, &alias->scale); 161 error: 162 close(fd); 163 return ret; 164 } 165 166 static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *name) 167 { 168 char path[PATH_MAX]; 169 ssize_t sret; 170 int fd; 171 172 snprintf(path, PATH_MAX, "%s/%s.unit", dir, name); 173 174 fd = open(path, O_RDONLY); 175 if (fd == -1) 176 return -1; 177 178 sret = read(fd, alias->unit, UNIT_MAX_LEN); 179 if (sret < 0) 180 goto error; 181 182 close(fd); 183 184 if (alias->unit[sret - 1] == '\n') 185 alias->unit[sret - 1] = '\0'; 186 else 187 alias->unit[sret] = '\0'; 188 189 return 0; 190 error: 191 close(fd); 192 alias->unit[0] = '\0'; 193 return -1; 194 } 195 196 static int 197 perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, char *dir, char *name) 198 { 199 char path[PATH_MAX]; 200 int fd; 201 202 snprintf(path, PATH_MAX, "%s/%s.per-pkg", dir, name); 203 204 fd = open(path, O_RDONLY); 205 if (fd == -1) 206 return -1; 207 208 close(fd); 209 210 alias->per_pkg = true; 211 return 0; 212 } 213 214 static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias, 215 char *dir, char *name) 216 { 217 char path[PATH_MAX]; 218 int fd; 219 220 snprintf(path, PATH_MAX, "%s/%s.snapshot", dir, name); 221 222 fd = open(path, O_RDONLY); 223 if (fd == -1) 224 return -1; 225 226 alias->snapshot = true; 227 close(fd); 228 return 0; 229 } 230 231 static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name, 232 char *desc, char *val, 233 char *long_desc, char *topic, 234 char *unit, char *perpkg, 235 char *metric_expr, 236 char *metric_name) 237 { 238 struct perf_pmu_alias *alias; 239 int ret; 240 int num; 241 242 alias = malloc(sizeof(*alias)); 243 if (!alias) 244 return -ENOMEM; 245 246 INIT_LIST_HEAD(&alias->terms); 247 alias->scale = 1.0; 248 alias->unit[0] = '\0'; 249 alias->per_pkg = false; 250 alias->snapshot = false; 251 252 ret = parse_events_terms(&alias->terms, val); 253 if (ret) { 254 pr_err("Cannot parse alias %s: %d\n", val, ret); 255 free(alias); 256 return ret; 257 } 258 259 alias->name = strdup(name); 260 if (dir) { 261 /* 262 * load unit name and scale if available 263 */ 264 perf_pmu__parse_unit(alias, dir, name); 265 perf_pmu__parse_scale(alias, dir, name); 266 perf_pmu__parse_per_pkg(alias, dir, name); 267 perf_pmu__parse_snapshot(alias, dir, name); 268 } 269 270 alias->metric_expr = metric_expr ? strdup(metric_expr) : NULL; 271 alias->metric_name = metric_name ? strdup(metric_name): NULL; 272 alias->desc = desc ? strdup(desc) : NULL; 273 alias->long_desc = long_desc ? strdup(long_desc) : 274 desc ? strdup(desc) : NULL; 275 alias->topic = topic ? strdup(topic) : NULL; 276 if (unit) { 277 if (convert_scale(unit, &unit, &alias->scale) < 0) 278 return -1; 279 snprintf(alias->unit, sizeof(alias->unit), "%s", unit); 280 } 281 alias->per_pkg = perpkg && sscanf(perpkg, "%d", &num) == 1 && num == 1; 282 alias->str = strdup(val); 283 284 list_add_tail(&alias->list, list); 285 286 return 0; 287 } 288 289 static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file) 290 { 291 char buf[256]; 292 int ret; 293 294 ret = fread(buf, 1, sizeof(buf), file); 295 if (ret == 0) 296 return -EINVAL; 297 298 buf[ret] = 0; 299 300 return __perf_pmu__new_alias(list, dir, name, NULL, buf, NULL, NULL, NULL, 301 NULL, NULL, NULL); 302 } 303 304 static inline bool pmu_alias_info_file(char *name) 305 { 306 size_t len; 307 308 len = strlen(name); 309 if (len > 5 && !strcmp(name + len - 5, ".unit")) 310 return true; 311 if (len > 6 && !strcmp(name + len - 6, ".scale")) 312 return true; 313 if (len > 8 && !strcmp(name + len - 8, ".per-pkg")) 314 return true; 315 if (len > 9 && !strcmp(name + len - 9, ".snapshot")) 316 return true; 317 318 return false; 319 } 320 321 /* 322 * Process all the sysfs attributes located under the directory 323 * specified in 'dir' parameter. 324 */ 325 static int pmu_aliases_parse(char *dir, struct list_head *head) 326 { 327 struct dirent *evt_ent; 328 DIR *event_dir; 329 330 event_dir = opendir(dir); 331 if (!event_dir) 332 return -EINVAL; 333 334 while ((evt_ent = readdir(event_dir))) { 335 char path[PATH_MAX]; 336 char *name = evt_ent->d_name; 337 FILE *file; 338 339 if (!strcmp(name, ".") || !strcmp(name, "..")) 340 continue; 341 342 /* 343 * skip info files parsed in perf_pmu__new_alias() 344 */ 345 if (pmu_alias_info_file(name)) 346 continue; 347 348 snprintf(path, PATH_MAX, "%s/%s", dir, name); 349 350 file = fopen(path, "r"); 351 if (!file) { 352 pr_debug("Cannot open %s\n", path); 353 continue; 354 } 355 356 if (perf_pmu__new_alias(head, dir, name, file) < 0) 357 pr_debug("Cannot set up %s\n", name); 358 fclose(file); 359 } 360 361 closedir(event_dir); 362 return 0; 363 } 364 365 /* 366 * Reading the pmu event aliases definition, which should be located at: 367 * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes. 368 */ 369 static int pmu_aliases(const char *name, struct list_head *head) 370 { 371 struct stat st; 372 char path[PATH_MAX]; 373 const char *sysfs = sysfs__mountpoint(); 374 375 if (!sysfs) 376 return -1; 377 378 snprintf(path, PATH_MAX, 379 "%s/bus/event_source/devices/%s/events", sysfs, name); 380 381 if (stat(path, &st) < 0) 382 return 0; /* no error if 'events' does not exist */ 383 384 if (pmu_aliases_parse(path, head)) 385 return -1; 386 387 return 0; 388 } 389 390 static int pmu_alias_terms(struct perf_pmu_alias *alias, 391 struct list_head *terms) 392 { 393 struct parse_events_term *term, *cloned; 394 LIST_HEAD(list); 395 int ret; 396 397 list_for_each_entry(term, &alias->terms, list) { 398 ret = parse_events_term__clone(&cloned, term); 399 if (ret) { 400 parse_events_terms__purge(&list); 401 return ret; 402 } 403 list_add_tail(&cloned->list, &list); 404 } 405 list_splice(&list, terms); 406 return 0; 407 } 408 409 /* 410 * Reading/parsing the default pmu type value, which should be 411 * located at: 412 * /sys/bus/event_source/devices/<dev>/type as sysfs attribute. 413 */ 414 static int pmu_type(const char *name, __u32 *type) 415 { 416 struct stat st; 417 char path[PATH_MAX]; 418 FILE *file; 419 int ret = 0; 420 const char *sysfs = sysfs__mountpoint(); 421 422 if (!sysfs) 423 return -1; 424 425 snprintf(path, PATH_MAX, 426 "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name); 427 428 if (stat(path, &st) < 0) 429 return -1; 430 431 file = fopen(path, "r"); 432 if (!file) 433 return -EINVAL; 434 435 if (1 != fscanf(file, "%u", type)) 436 ret = -1; 437 438 fclose(file); 439 return ret; 440 } 441 442 /* Add all pmus in sysfs to pmu list: */ 443 static void pmu_read_sysfs(void) 444 { 445 char path[PATH_MAX]; 446 DIR *dir; 447 struct dirent *dent; 448 const char *sysfs = sysfs__mountpoint(); 449 450 if (!sysfs) 451 return; 452 453 snprintf(path, PATH_MAX, 454 "%s" EVENT_SOURCE_DEVICE_PATH, sysfs); 455 456 dir = opendir(path); 457 if (!dir) 458 return; 459 460 while ((dent = readdir(dir))) { 461 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) 462 continue; 463 /* add to static LIST_HEAD(pmus): */ 464 perf_pmu__find(dent->d_name); 465 } 466 467 closedir(dir); 468 } 469 470 static struct cpu_map *pmu_cpumask(const char *name) 471 { 472 struct stat st; 473 char path[PATH_MAX]; 474 FILE *file; 475 struct cpu_map *cpus; 476 const char *sysfs = sysfs__mountpoint(); 477 const char *templates[] = { 478 "%s/bus/event_source/devices/%s/cpumask", 479 "%s/bus/event_source/devices/%s/cpus", 480 NULL 481 }; 482 const char **template; 483 484 if (!sysfs) 485 return NULL; 486 487 for (template = templates; *template; template++) { 488 snprintf(path, PATH_MAX, *template, sysfs, name); 489 if (stat(path, &st) == 0) 490 break; 491 } 492 493 if (!*template) 494 return NULL; 495 496 file = fopen(path, "r"); 497 if (!file) 498 return NULL; 499 500 cpus = cpu_map__read(file); 501 fclose(file); 502 return cpus; 503 } 504 505 /* 506 * Return the CPU id as a raw string. 507 * 508 * Each architecture should provide a more precise id string that 509 * can be use to match the architecture's "mapfile". 510 */ 511 char * __weak get_cpuid_str(void) 512 { 513 return NULL; 514 } 515 516 /* 517 * From the pmu_events_map, find the table of PMU events that corresponds 518 * to the current running CPU. Then, add all PMU events from that table 519 * as aliases. 520 */ 521 static void pmu_add_cpu_aliases(struct list_head *head, const char *name) 522 { 523 int i; 524 struct pmu_events_map *map; 525 struct pmu_event *pe; 526 char *cpuid; 527 static bool printed; 528 529 cpuid = getenv("PERF_CPUID"); 530 if (cpuid) 531 cpuid = strdup(cpuid); 532 if (!cpuid) 533 cpuid = get_cpuid_str(); 534 if (!cpuid) 535 return; 536 537 if (!printed) { 538 pr_debug("Using CPUID %s\n", cpuid); 539 printed = true; 540 } 541 542 i = 0; 543 while (1) { 544 map = &pmu_events_map[i++]; 545 if (!map->table) 546 goto out; 547 548 if (!strcmp(map->cpuid, cpuid)) 549 break; 550 } 551 552 /* 553 * Found a matching PMU events table. Create aliases 554 */ 555 i = 0; 556 while (1) { 557 const char *pname; 558 559 pe = &map->table[i++]; 560 if (!pe->name) 561 break; 562 563 pname = pe->pmu ? pe->pmu : "cpu"; 564 if (strncmp(pname, name, strlen(pname))) 565 continue; 566 567 /* need type casts to override 'const' */ 568 __perf_pmu__new_alias(head, NULL, (char *)pe->name, 569 (char *)pe->desc, (char *)pe->event, 570 (char *)pe->long_desc, (char *)pe->topic, 571 (char *)pe->unit, (char *)pe->perpkg, 572 (char *)pe->metric_expr, 573 (char *)pe->metric_name); 574 } 575 576 out: 577 free(cpuid); 578 } 579 580 struct perf_event_attr * __weak 581 perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused) 582 { 583 return NULL; 584 } 585 586 static struct perf_pmu *pmu_lookup(const char *name) 587 { 588 struct perf_pmu *pmu; 589 LIST_HEAD(format); 590 LIST_HEAD(aliases); 591 __u32 type; 592 593 /* 594 * The pmu data we store & need consists of the pmu 595 * type value and format definitions. Load both right 596 * now. 597 */ 598 if (pmu_format(name, &format)) 599 return NULL; 600 601 /* 602 * Check the type first to avoid unnecessary work. 603 */ 604 if (pmu_type(name, &type)) 605 return NULL; 606 607 if (pmu_aliases(name, &aliases)) 608 return NULL; 609 610 pmu_add_cpu_aliases(&aliases, name); 611 pmu = zalloc(sizeof(*pmu)); 612 if (!pmu) 613 return NULL; 614 615 pmu->cpus = pmu_cpumask(name); 616 617 INIT_LIST_HEAD(&pmu->format); 618 INIT_LIST_HEAD(&pmu->aliases); 619 list_splice(&format, &pmu->format); 620 list_splice(&aliases, &pmu->aliases); 621 pmu->name = strdup(name); 622 pmu->type = type; 623 list_add_tail(&pmu->list, &pmus); 624 625 pmu->default_config = perf_pmu__get_default_config(pmu); 626 627 return pmu; 628 } 629 630 static struct perf_pmu *pmu_find(const char *name) 631 { 632 struct perf_pmu *pmu; 633 634 list_for_each_entry(pmu, &pmus, list) 635 if (!strcmp(pmu->name, name)) 636 return pmu; 637 638 return NULL; 639 } 640 641 struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu) 642 { 643 /* 644 * pmu iterator: If pmu is NULL, we start at the begin, 645 * otherwise return the next pmu. Returns NULL on end. 646 */ 647 if (!pmu) { 648 pmu_read_sysfs(); 649 pmu = list_prepare_entry(pmu, &pmus, list); 650 } 651 list_for_each_entry_continue(pmu, &pmus, list) 652 return pmu; 653 return NULL; 654 } 655 656 struct perf_pmu *perf_pmu__find(const char *name) 657 { 658 struct perf_pmu *pmu; 659 660 /* 661 * Once PMU is loaded it stays in the list, 662 * so we keep us from multiple reading/parsing 663 * the pmu format definitions. 664 */ 665 pmu = pmu_find(name); 666 if (pmu) 667 return pmu; 668 669 return pmu_lookup(name); 670 } 671 672 static struct perf_pmu_format * 673 pmu_find_format(struct list_head *formats, const char *name) 674 { 675 struct perf_pmu_format *format; 676 677 list_for_each_entry(format, formats, list) 678 if (!strcmp(format->name, name)) 679 return format; 680 681 return NULL; 682 } 683 684 __u64 perf_pmu__format_bits(struct list_head *formats, const char *name) 685 { 686 struct perf_pmu_format *format = pmu_find_format(formats, name); 687 __u64 bits = 0; 688 int fbit; 689 690 if (!format) 691 return 0; 692 693 for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS) 694 bits |= 1ULL << fbit; 695 696 return bits; 697 } 698 699 /* 700 * Sets value based on the format definition (format parameter) 701 * and unformated value (value parameter). 702 */ 703 static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v, 704 bool zero) 705 { 706 unsigned long fbit, vbit; 707 708 for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) { 709 710 if (!test_bit(fbit, format)) 711 continue; 712 713 if (value & (1llu << vbit++)) 714 *v |= (1llu << fbit); 715 else if (zero) 716 *v &= ~(1llu << fbit); 717 } 718 } 719 720 static __u64 pmu_format_max_value(const unsigned long *format) 721 { 722 __u64 w = 0; 723 int fbit; 724 725 for_each_set_bit(fbit, format, PERF_PMU_FORMAT_BITS) 726 w |= (1ULL << fbit); 727 728 return w; 729 } 730 731 /* 732 * Term is a string term, and might be a param-term. Try to look up it's value 733 * in the remaining terms. 734 * - We have a term like "base-or-format-term=param-term", 735 * - We need to find the value supplied for "param-term" (with param-term named 736 * in a config string) later on in the term list. 737 */ 738 static int pmu_resolve_param_term(struct parse_events_term *term, 739 struct list_head *head_terms, 740 __u64 *value) 741 { 742 struct parse_events_term *t; 743 744 list_for_each_entry(t, head_terms, list) { 745 if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { 746 if (!strcmp(t->config, term->config)) { 747 t->used = true; 748 *value = t->val.num; 749 return 0; 750 } 751 } 752 } 753 754 if (verbose > 0) 755 printf("Required parameter '%s' not specified\n", term->config); 756 757 return -1; 758 } 759 760 static char *pmu_formats_string(struct list_head *formats) 761 { 762 struct perf_pmu_format *format; 763 char *str = NULL; 764 struct strbuf buf = STRBUF_INIT; 765 unsigned i = 0; 766 767 if (!formats) 768 return NULL; 769 770 /* sysfs exported terms */ 771 list_for_each_entry(format, formats, list) 772 if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0) 773 goto error; 774 775 str = strbuf_detach(&buf, NULL); 776 error: 777 strbuf_release(&buf); 778 779 return str; 780 } 781 782 /* 783 * Setup one of config[12] attr members based on the 784 * user input data - term parameter. 785 */ 786 static int pmu_config_term(struct list_head *formats, 787 struct perf_event_attr *attr, 788 struct parse_events_term *term, 789 struct list_head *head_terms, 790 bool zero, struct parse_events_error *err) 791 { 792 struct perf_pmu_format *format; 793 __u64 *vp; 794 __u64 val, max_val; 795 796 /* 797 * If this is a parameter we've already used for parameterized-eval, 798 * skip it in normal eval. 799 */ 800 if (term->used) 801 return 0; 802 803 /* 804 * Hardcoded terms should be already in, so nothing 805 * to be done for them. 806 */ 807 if (parse_events__is_hardcoded_term(term)) 808 return 0; 809 810 format = pmu_find_format(formats, term->config); 811 if (!format) { 812 if (verbose > 0) 813 printf("Invalid event/parameter '%s'\n", term->config); 814 if (err) { 815 char *pmu_term = pmu_formats_string(formats); 816 817 err->idx = term->err_term; 818 err->str = strdup("unknown term"); 819 err->help = parse_events_formats_error_string(pmu_term); 820 free(pmu_term); 821 } 822 return -EINVAL; 823 } 824 825 switch (format->value) { 826 case PERF_PMU_FORMAT_VALUE_CONFIG: 827 vp = &attr->config; 828 break; 829 case PERF_PMU_FORMAT_VALUE_CONFIG1: 830 vp = &attr->config1; 831 break; 832 case PERF_PMU_FORMAT_VALUE_CONFIG2: 833 vp = &attr->config2; 834 break; 835 default: 836 return -EINVAL; 837 } 838 839 /* 840 * Either directly use a numeric term, or try to translate string terms 841 * using event parameters. 842 */ 843 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { 844 if (term->no_value && 845 bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) { 846 if (err) { 847 err->idx = term->err_val; 848 err->str = strdup("no value assigned for term"); 849 } 850 return -EINVAL; 851 } 852 853 val = term->val.num; 854 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) { 855 if (strcmp(term->val.str, "?")) { 856 if (verbose > 0) { 857 pr_info("Invalid sysfs entry %s=%s\n", 858 term->config, term->val.str); 859 } 860 if (err) { 861 err->idx = term->err_val; 862 err->str = strdup("expected numeric value"); 863 } 864 return -EINVAL; 865 } 866 867 if (pmu_resolve_param_term(term, head_terms, &val)) 868 return -EINVAL; 869 } else 870 return -EINVAL; 871 872 max_val = pmu_format_max_value(format->bits); 873 if (val > max_val) { 874 if (err) { 875 err->idx = term->err_val; 876 if (asprintf(&err->str, 877 "value too big for format, maximum is %llu", 878 (unsigned long long)max_val) < 0) 879 err->str = strdup("value too big for format"); 880 return -EINVAL; 881 } 882 /* 883 * Assume we don't care if !err, in which case the value will be 884 * silently truncated. 885 */ 886 } 887 888 pmu_format_value(format->bits, val, vp, zero); 889 return 0; 890 } 891 892 int perf_pmu__config_terms(struct list_head *formats, 893 struct perf_event_attr *attr, 894 struct list_head *head_terms, 895 bool zero, struct parse_events_error *err) 896 { 897 struct parse_events_term *term; 898 899 list_for_each_entry(term, head_terms, list) { 900 if (pmu_config_term(formats, attr, term, head_terms, 901 zero, err)) 902 return -EINVAL; 903 } 904 905 return 0; 906 } 907 908 /* 909 * Configures event's 'attr' parameter based on the: 910 * 1) users input - specified in terms parameter 911 * 2) pmu format definitions - specified by pmu parameter 912 */ 913 int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr, 914 struct list_head *head_terms, 915 struct parse_events_error *err) 916 { 917 bool zero = !!pmu->default_config; 918 919 attr->type = pmu->type; 920 return perf_pmu__config_terms(&pmu->format, attr, head_terms, 921 zero, err); 922 } 923 924 static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu, 925 struct parse_events_term *term) 926 { 927 struct perf_pmu_alias *alias; 928 char *name; 929 930 if (parse_events__is_hardcoded_term(term)) 931 return NULL; 932 933 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { 934 if (term->val.num != 1) 935 return NULL; 936 if (pmu_find_format(&pmu->format, term->config)) 937 return NULL; 938 name = term->config; 939 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) { 940 if (strcasecmp(term->config, "event")) 941 return NULL; 942 name = term->val.str; 943 } else { 944 return NULL; 945 } 946 947 list_for_each_entry(alias, &pmu->aliases, list) { 948 if (!strcasecmp(alias->name, name)) 949 return alias; 950 } 951 return NULL; 952 } 953 954 955 static int check_info_data(struct perf_pmu_alias *alias, 956 struct perf_pmu_info *info) 957 { 958 /* 959 * Only one term in event definition can 960 * define unit, scale and snapshot, fail 961 * if there's more than one. 962 */ 963 if ((info->unit && alias->unit[0]) || 964 (info->scale && alias->scale) || 965 (info->snapshot && alias->snapshot)) 966 return -EINVAL; 967 968 if (alias->unit[0]) 969 info->unit = alias->unit; 970 971 if (alias->scale) 972 info->scale = alias->scale; 973 974 if (alias->snapshot) 975 info->snapshot = alias->snapshot; 976 977 return 0; 978 } 979 980 /* 981 * Find alias in the terms list and replace it with the terms 982 * defined for the alias 983 */ 984 int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms, 985 struct perf_pmu_info *info) 986 { 987 struct parse_events_term *term, *h; 988 struct perf_pmu_alias *alias; 989 int ret; 990 991 info->per_pkg = false; 992 993 /* 994 * Mark unit and scale as not set 995 * (different from default values, see below) 996 */ 997 info->unit = NULL; 998 info->scale = 0.0; 999 info->snapshot = false; 1000 info->metric_expr = NULL; 1001 info->metric_name = NULL; 1002 1003 list_for_each_entry_safe(term, h, head_terms, list) { 1004 alias = pmu_find_alias(pmu, term); 1005 if (!alias) 1006 continue; 1007 ret = pmu_alias_terms(alias, &term->list); 1008 if (ret) 1009 return ret; 1010 1011 ret = check_info_data(alias, info); 1012 if (ret) 1013 return ret; 1014 1015 if (alias->per_pkg) 1016 info->per_pkg = true; 1017 info->metric_expr = alias->metric_expr; 1018 info->metric_name = alias->metric_name; 1019 1020 list_del(&term->list); 1021 free(term); 1022 } 1023 1024 /* 1025 * if no unit or scale foundin aliases, then 1026 * set defaults as for evsel 1027 * unit cannot left to NULL 1028 */ 1029 if (info->unit == NULL) 1030 info->unit = ""; 1031 1032 if (info->scale == 0.0) 1033 info->scale = 1.0; 1034 1035 return 0; 1036 } 1037 1038 int perf_pmu__new_format(struct list_head *list, char *name, 1039 int config, unsigned long *bits) 1040 { 1041 struct perf_pmu_format *format; 1042 1043 format = zalloc(sizeof(*format)); 1044 if (!format) 1045 return -ENOMEM; 1046 1047 format->name = strdup(name); 1048 format->value = config; 1049 memcpy(format->bits, bits, sizeof(format->bits)); 1050 1051 list_add_tail(&format->list, list); 1052 return 0; 1053 } 1054 1055 void perf_pmu__set_format(unsigned long *bits, long from, long to) 1056 { 1057 long b; 1058 1059 if (!to) 1060 to = from; 1061 1062 memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS)); 1063 for (b = from; b <= to; b++) 1064 set_bit(b, bits); 1065 } 1066 1067 static int sub_non_neg(int a, int b) 1068 { 1069 if (b > a) 1070 return 0; 1071 return a - b; 1072 } 1073 1074 static char *format_alias(char *buf, int len, struct perf_pmu *pmu, 1075 struct perf_pmu_alias *alias) 1076 { 1077 struct parse_events_term *term; 1078 int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name); 1079 1080 list_for_each_entry(term, &alias->terms, list) { 1081 if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) 1082 used += snprintf(buf + used, sub_non_neg(len, used), 1083 ",%s=%s", term->config, 1084 term->val.str); 1085 } 1086 1087 if (sub_non_neg(len, used) > 0) { 1088 buf[used] = '/'; 1089 used++; 1090 } 1091 if (sub_non_neg(len, used) > 0) { 1092 buf[used] = '\0'; 1093 used++; 1094 } else 1095 buf[len - 1] = '\0'; 1096 1097 return buf; 1098 } 1099 1100 static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu, 1101 struct perf_pmu_alias *alias) 1102 { 1103 snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name); 1104 return buf; 1105 } 1106 1107 struct sevent { 1108 char *name; 1109 char *desc; 1110 char *topic; 1111 char *str; 1112 char *pmu; 1113 char *metric_expr; 1114 char *metric_name; 1115 }; 1116 1117 static int cmp_sevent(const void *a, const void *b) 1118 { 1119 const struct sevent *as = a; 1120 const struct sevent *bs = b; 1121 1122 /* Put extra events last */ 1123 if (!!as->desc != !!bs->desc) 1124 return !!as->desc - !!bs->desc; 1125 if (as->topic && bs->topic) { 1126 int n = strcmp(as->topic, bs->topic); 1127 1128 if (n) 1129 return n; 1130 } 1131 return strcmp(as->name, bs->name); 1132 } 1133 1134 static void wordwrap(char *s, int start, int max, int corr) 1135 { 1136 int column = start; 1137 int n; 1138 1139 while (*s) { 1140 int wlen = strcspn(s, " \t"); 1141 1142 if (column + wlen >= max && column > start) { 1143 printf("\n%*s", start, ""); 1144 column = start + corr; 1145 } 1146 n = printf("%s%.*s", column > start ? " " : "", wlen, s); 1147 if (n <= 0) 1148 break; 1149 s += wlen; 1150 column += n; 1151 while (isspace(*s)) 1152 s++; 1153 } 1154 } 1155 1156 void print_pmu_events(const char *event_glob, bool name_only, bool quiet_flag, 1157 bool long_desc) 1158 { 1159 struct perf_pmu *pmu; 1160 struct perf_pmu_alias *alias; 1161 char buf[1024]; 1162 int printed = 0; 1163 int len, j; 1164 struct sevent *aliases; 1165 int numdesc = 0; 1166 int columns = pager_get_columns(); 1167 char *topic = NULL; 1168 1169 pmu = NULL; 1170 len = 0; 1171 while ((pmu = perf_pmu__scan(pmu)) != NULL) { 1172 list_for_each_entry(alias, &pmu->aliases, list) 1173 len++; 1174 if (pmu->selectable) 1175 len++; 1176 } 1177 aliases = zalloc(sizeof(struct sevent) * len); 1178 if (!aliases) 1179 goto out_enomem; 1180 pmu = NULL; 1181 j = 0; 1182 while ((pmu = perf_pmu__scan(pmu)) != NULL) { 1183 list_for_each_entry(alias, &pmu->aliases, list) { 1184 char *name = alias->desc ? alias->name : 1185 format_alias(buf, sizeof(buf), pmu, alias); 1186 bool is_cpu = !strcmp(pmu->name, "cpu"); 1187 1188 if (event_glob != NULL && 1189 !(strglobmatch_nocase(name, event_glob) || 1190 (!is_cpu && strglobmatch_nocase(alias->name, 1191 event_glob)) || 1192 (alias->topic && 1193 strglobmatch_nocase(alias->topic, event_glob)))) 1194 continue; 1195 1196 if (is_cpu && !name_only && !alias->desc) 1197 name = format_alias_or(buf, sizeof(buf), pmu, alias); 1198 1199 aliases[j].name = name; 1200 if (is_cpu && !name_only && !alias->desc) 1201 aliases[j].name = format_alias_or(buf, 1202 sizeof(buf), 1203 pmu, alias); 1204 aliases[j].name = strdup(aliases[j].name); 1205 if (!aliases[j].name) 1206 goto out_enomem; 1207 1208 aliases[j].desc = long_desc ? alias->long_desc : 1209 alias->desc; 1210 aliases[j].topic = alias->topic; 1211 aliases[j].str = alias->str; 1212 aliases[j].pmu = pmu->name; 1213 aliases[j].metric_expr = alias->metric_expr; 1214 aliases[j].metric_name = alias->metric_name; 1215 j++; 1216 } 1217 if (pmu->selectable && 1218 (event_glob == NULL || strglobmatch(pmu->name, event_glob))) { 1219 char *s; 1220 if (asprintf(&s, "%s//", pmu->name) < 0) 1221 goto out_enomem; 1222 aliases[j].name = s; 1223 j++; 1224 } 1225 } 1226 len = j; 1227 qsort(aliases, len, sizeof(struct sevent), cmp_sevent); 1228 for (j = 0; j < len; j++) { 1229 /* Skip duplicates */ 1230 if (j > 0 && !strcmp(aliases[j].name, aliases[j - 1].name)) 1231 continue; 1232 if (name_only) { 1233 printf("%s ", aliases[j].name); 1234 continue; 1235 } 1236 if (aliases[j].desc && !quiet_flag) { 1237 if (numdesc++ == 0) 1238 printf("\n"); 1239 if (aliases[j].topic && (!topic || 1240 strcmp(topic, aliases[j].topic))) { 1241 printf("%s%s:\n", topic ? "\n" : "", 1242 aliases[j].topic); 1243 topic = aliases[j].topic; 1244 } 1245 printf(" %-50s\n", aliases[j].name); 1246 printf("%*s", 8, "["); 1247 wordwrap(aliases[j].desc, 8, columns, 0); 1248 printf("]\n"); 1249 if (verbose > 0) { 1250 printf("%*s%s/%s/ ", 8, "", aliases[j].pmu, aliases[j].str); 1251 if (aliases[j].metric_name) 1252 printf(" MetricName: %s", aliases[j].metric_name); 1253 if (aliases[j].metric_expr) 1254 printf(" MetricExpr: %s", aliases[j].metric_expr); 1255 putchar('\n'); 1256 } 1257 } else 1258 printf(" %-50s [Kernel PMU event]\n", aliases[j].name); 1259 printed++; 1260 } 1261 if (printed && pager_in_use()) 1262 printf("\n"); 1263 out_free: 1264 for (j = 0; j < len; j++) 1265 zfree(&aliases[j].name); 1266 zfree(&aliases); 1267 return; 1268 1269 out_enomem: 1270 printf("FATAL: not enough memory to print PMU events\n"); 1271 if (aliases) 1272 goto out_free; 1273 } 1274 1275 bool pmu_have_event(const char *pname, const char *name) 1276 { 1277 struct perf_pmu *pmu; 1278 struct perf_pmu_alias *alias; 1279 1280 pmu = NULL; 1281 while ((pmu = perf_pmu__scan(pmu)) != NULL) { 1282 if (strcmp(pname, pmu->name)) 1283 continue; 1284 list_for_each_entry(alias, &pmu->aliases, list) 1285 if (!strcmp(alias->name, name)) 1286 return true; 1287 } 1288 return false; 1289 } 1290 1291 static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name) 1292 { 1293 struct stat st; 1294 char path[PATH_MAX]; 1295 const char *sysfs; 1296 1297 sysfs = sysfs__mountpoint(); 1298 if (!sysfs) 1299 return NULL; 1300 1301 snprintf(path, PATH_MAX, 1302 "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name); 1303 1304 if (stat(path, &st) < 0) 1305 return NULL; 1306 1307 return fopen(path, "r"); 1308 } 1309 1310 int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt, 1311 ...) 1312 { 1313 va_list args; 1314 FILE *file; 1315 int ret = EOF; 1316 1317 va_start(args, fmt); 1318 file = perf_pmu__open_file(pmu, name); 1319 if (file) { 1320 ret = vfscanf(file, fmt, args); 1321 fclose(file); 1322 } 1323 va_end(args); 1324 return ret; 1325 } 1326