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>
14 #include <api/fs/fs.h>
22 #include <util/pmu-bison.h>
23 #include <util/pmu-flex.h>
24 #include "parse-events.h"
25 #include "print-events.h"
30 #include "util/evsel_config.h"
33 struct perf_pmu perf_pmu__fake = {
37 #define UNIT_MAX_LEN 31 /* max length for event unit name */
40 /* An event loaded from /sys/devices/<pmu>/events. */
42 /* An event loaded from a CPUID matched json file. */
45 * An event loaded from a /sys/devices/<pmu>/identifier matched json
52 * struct perf_pmu_alias - An event either read from sysfs or builtin in
53 * pmu-events.c, created by parsing the pmu-events json files.
55 struct perf_pmu_alias {
56 /** @name: Name of the event like "mem-loads". */
58 /** @desc: Optional short description of the event. */
60 /** @long_desc: Optional long description. */
63 * @topic: Optional topic such as cache or pipeline, particularly for
67 /** @terms: Owned list of the original parsed parameters. */
68 struct parse_events_terms terms;
69 /** @list: List element of struct perf_pmu aliases. */
70 struct list_head list;
72 * @pmu_name: The name copied from the json struct pmu_event. This can
73 * differ from the PMU name as it won't have suffixes.
76 /** @unit: Units for the event, such as bytes or cache lines. */
77 char unit[UNIT_MAX_LEN+1];
78 /** @scale: Value to scale read counter values by. */
81 * @per_pkg: Does the file
82 * <sysfs>/bus/event_source/devices/<pmu_name>/events/<name>.per-pkg or
83 * equivalent json value exist and have the value 1.
87 * @snapshot: Does the file
88 * <sysfs>/bus/event_source/devices/<pmu_name>/events/<name>.snapshot
89 * exist and have the value 1.
93 * @deprecated: Is the event hidden and so not shown in perf list by
97 /** @from_sysfs: Was the alias from sysfs or a json event? */
99 /** @info_loaded: Have the scale, unit and other values been read from disk? */
104 * struct perf_pmu_format - Values from a format file read from
105 * <sysfs>/devices/cpu/format/ held in struct perf_pmu.
107 * For example, the contents of <sysfs>/devices/cpu/format/event may be
108 * "config:0-7" and will be represented here as name="event",
109 * value=PERF_PMU_FORMAT_VALUE_CONFIG and bits 0 to 7 will be set.
111 struct perf_pmu_format {
112 /** @list: Element on list within struct perf_pmu. */
113 struct list_head list;
114 /** @bits: Which config bits are set by this format value. */
115 DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
116 /** @name: The modifier/file name. */
119 * @value : Which config value the format relates to. Supported values
120 * are from PERF_PMU_FORMAT_VALUE_CONFIG to
121 * PERF_PMU_FORMAT_VALUE_CONFIG_END.
124 /** @loaded: Has the contents been loaded/parsed. */
128 static int pmu_aliases_parse(struct perf_pmu *pmu);
130 static struct perf_pmu_format *perf_pmu__new_format(struct list_head *list, char *name)
132 struct perf_pmu_format *format;
134 format = zalloc(sizeof(*format));
138 format->name = strdup(name);
143 list_add_tail(&format->list, list);
147 /* Called at the end of parsing a format. */
148 void perf_pmu_format__set_value(void *vformat, int config, unsigned long *bits)
150 struct perf_pmu_format *format = vformat;
152 format->value = config;
153 memcpy(format->bits, bits, sizeof(format->bits));
156 static void __perf_pmu_format__load(struct perf_pmu_format *format, FILE *file)
161 ret = perf_pmu_lex_init(&scanner);
165 perf_pmu_set_in(file, scanner);
166 ret = perf_pmu_parse(format, scanner);
167 perf_pmu_lex_destroy(scanner);
168 format->loaded = true;
171 static void perf_pmu_format__load(const struct perf_pmu *pmu, struct perf_pmu_format *format)
179 if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, "format"))
182 assert(strlen(path) + strlen(format->name) + 2 < sizeof(path));
184 strcat(path, format->name);
186 file = fopen(path, "r");
189 __perf_pmu_format__load(format, file);
194 * Parse & process all the sysfs attributes located under
195 * the directory specified in 'dir' parameter.
197 static int perf_pmu__format_parse(struct perf_pmu *pmu, int dirfd, bool eager_load)
199 struct dirent *evt_ent;
203 format_dir = fdopendir(dirfd);
207 while ((evt_ent = readdir(format_dir)) != NULL) {
208 struct perf_pmu_format *format;
209 char *name = evt_ent->d_name;
211 if (!strcmp(name, ".") || !strcmp(name, ".."))
214 format = perf_pmu__new_format(&pmu->format, name);
222 int fd = openat(dirfd, name, O_RDONLY);
228 file = fdopen(fd, "r");
233 __perf_pmu_format__load(format, file);
238 closedir(format_dir);
243 * Reading/parsing the default pmu format definition, which should be
245 * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
247 static int pmu_format(struct perf_pmu *pmu, int dirfd, const char *name, bool eager_load)
251 fd = perf_pmu__pathname_fd(dirfd, name, "format", O_DIRECTORY);
255 /* it'll close the fd */
256 if (perf_pmu__format_parse(pmu, fd, eager_load))
262 int perf_pmu__convert_scale(const char *scale, char **end, double *sval)
268 * save current locale
270 lc = setlocale(LC_NUMERIC, NULL);
273 * The lc string may be allocated in static storage,
274 * so get a dynamic copy to make it survive setlocale
284 * force to C locale to ensure kernel
285 * scale string is converted correctly.
286 * kernel uses default C locale.
288 setlocale(LC_NUMERIC, "C");
290 *sval = strtod(scale, end);
294 setlocale(LC_NUMERIC, lc);
299 static int perf_pmu__parse_scale(struct perf_pmu *pmu, struct perf_pmu_alias *alias)
308 len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path));
311 scnprintf(path + len, sizeof(path) - len, "%s/events/%s.scale", pmu->name, alias->name);
313 fd = open(path, O_RDONLY);
317 if (fstat(fd, &st) < 0)
320 sret = read(fd, scale, sizeof(scale)-1);
324 if (scale[sret - 1] == '\n')
325 scale[sret - 1] = '\0';
329 ret = perf_pmu__convert_scale(scale, NULL, &alias->scale);
335 static int perf_pmu__parse_unit(struct perf_pmu *pmu, struct perf_pmu_alias *alias)
343 len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path));
346 scnprintf(path + len, sizeof(path) - len, "%s/events/%s.unit", pmu->name, alias->name);
348 fd = open(path, O_RDONLY);
352 sret = read(fd, alias->unit, UNIT_MAX_LEN);
358 if (alias->unit[sret - 1] == '\n')
359 alias->unit[sret - 1] = '\0';
361 alias->unit[sret] = '\0';
366 alias->unit[0] = '\0';
371 perf_pmu__parse_per_pkg(struct perf_pmu *pmu, struct perf_pmu_alias *alias)
377 len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path));
380 scnprintf(path + len, sizeof(path) - len, "%s/events/%s.per-pkg", pmu->name, alias->name);
382 fd = open(path, O_RDONLY);
388 alias->per_pkg = true;
392 static int perf_pmu__parse_snapshot(struct perf_pmu *pmu, struct perf_pmu_alias *alias)
398 len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path));
401 scnprintf(path + len, sizeof(path) - len, "%s/events/%s.snapshot", pmu->name, alias->name);
403 fd = open(path, O_RDONLY);
407 alias->snapshot = true;
412 /* Delete an alias entry. */
413 static void perf_pmu_free_alias(struct perf_pmu_alias *newalias)
415 zfree(&newalias->name);
416 zfree(&newalias->desc);
417 zfree(&newalias->long_desc);
418 zfree(&newalias->topic);
419 zfree(&newalias->pmu_name);
420 parse_events_terms__exit(&newalias->terms);
424 static void perf_pmu__del_aliases(struct perf_pmu *pmu)
426 struct perf_pmu_alias *alias, *tmp;
428 list_for_each_entry_safe(alias, tmp, &pmu->aliases, list) {
429 list_del(&alias->list);
430 perf_pmu_free_alias(alias);
434 static struct perf_pmu_alias *perf_pmu__find_alias(struct perf_pmu *pmu,
438 struct perf_pmu_alias *alias;
440 if (load && !pmu->sysfs_aliases_loaded) {
441 bool has_sysfs_event;
442 char event_file_name[FILENAME_MAX + 8];
445 * Test if alias/event 'name' exists in the PMU's sysfs/events
446 * directory. If not skip parsing the sysfs aliases. Sysfs event
447 * name must be all lower or all upper case.
449 scnprintf(event_file_name, sizeof(event_file_name), "events/%s", name);
450 for (size_t i = 7, n = 7 + strlen(name); i < n; i++)
451 event_file_name[i] = tolower(event_file_name[i]);
453 has_sysfs_event = perf_pmu__file_exists(pmu, event_file_name);
454 if (!has_sysfs_event) {
455 for (size_t i = 7, n = 7 + strlen(name); i < n; i++)
456 event_file_name[i] = toupper(event_file_name[i]);
458 has_sysfs_event = perf_pmu__file_exists(pmu, event_file_name);
461 pmu_aliases_parse(pmu);
464 list_for_each_entry(alias, &pmu->aliases, list) {
465 if (!strcasecmp(alias->name, name))
471 static bool assign_str(const char *name, const char *field, char **old_str,
474 if (!*old_str && new_str) {
475 *old_str = strdup(new_str);
479 if (!new_str || !strcasecmp(*old_str, new_str))
480 return false; /* Nothing to update. */
482 pr_debug("alias %s differs in field '%s' ('%s' != '%s')\n",
483 name, field, *old_str, new_str);
485 *old_str = strdup(new_str);
489 static void read_alias_info(struct perf_pmu *pmu, struct perf_pmu_alias *alias)
491 if (!alias->from_sysfs || alias->info_loaded)
495 * load unit name and scale if available
497 perf_pmu__parse_unit(pmu, alias);
498 perf_pmu__parse_scale(pmu, alias);
499 perf_pmu__parse_per_pkg(pmu, alias);
500 perf_pmu__parse_snapshot(pmu, alias);
503 struct update_alias_data {
504 struct perf_pmu *pmu;
505 struct perf_pmu_alias *alias;
508 static int update_alias(const struct pmu_event *pe,
509 const struct pmu_events_table *table __maybe_unused,
512 struct update_alias_data *data = vdata;
515 read_alias_info(data->pmu, data->alias);
516 assign_str(pe->name, "desc", &data->alias->desc, pe->desc);
517 assign_str(pe->name, "long_desc", &data->alias->long_desc, pe->long_desc);
518 assign_str(pe->name, "topic", &data->alias->topic, pe->topic);
519 data->alias->per_pkg = pe->perpkg;
521 parse_events_terms__exit(&data->alias->terms);
522 ret = parse_events_terms(&data->alias->terms, pe->event, /*input=*/NULL);
524 if (!ret && pe->unit) {
527 ret = perf_pmu__convert_scale(pe->unit, &unit, &data->alias->scale);
529 snprintf(data->alias->unit, sizeof(data->alias->unit), "%s", unit);
534 static int perf_pmu__new_alias(struct perf_pmu *pmu, const char *name,
535 const char *desc, const char *val, FILE *val_fd,
536 const struct pmu_event *pe, enum event_source src)
538 struct perf_pmu_alias *alias;
540 const char *long_desc = NULL, *topic = NULL, *unit = NULL, *pmu_name = NULL;
541 bool deprecated = false, perpkg = false;
543 if (perf_pmu__find_alias(pmu, name, /*load=*/ false)) {
544 /* Alias was already created/loaded. */
549 long_desc = pe->long_desc;
553 deprecated = pe->deprecated;
554 if (pe->pmu && strcmp(pe->pmu, "default_core"))
558 alias = zalloc(sizeof(*alias));
562 parse_events_terms__init(&alias->terms);
564 alias->unit[0] = '\0';
565 alias->per_pkg = perpkg;
566 alias->snapshot = false;
567 alias->deprecated = deprecated;
569 ret = parse_events_terms(&alias->terms, val, val_fd);
571 pr_err("Cannot parse alias %s: %d\n", val, ret);
576 alias->name = strdup(name);
577 alias->desc = desc ? strdup(desc) : NULL;
578 alias->long_desc = long_desc ? strdup(long_desc) :
579 desc ? strdup(desc) : NULL;
580 alias->topic = topic ? strdup(topic) : NULL;
581 alias->pmu_name = pmu_name ? strdup(pmu_name) : NULL;
583 if (perf_pmu__convert_scale(unit, (char **)&unit, &alias->scale) < 0) {
584 perf_pmu_free_alias(alias);
587 snprintf(alias->unit, sizeof(alias->unit), "%s", unit);
591 case EVENT_SRC_SYSFS:
592 alias->from_sysfs = true;
593 if (pmu->events_table) {
594 /* Update an event from sysfs with json data. */
595 struct update_alias_data data = {
599 if (pmu_events_table__find_event(pmu->events_table, pmu, name,
600 update_alias, &data) == 0)
601 pmu->cpu_json_aliases++;
603 pmu->sysfs_aliases++;
605 case EVENT_SRC_CPU_JSON:
606 pmu->cpu_json_aliases++;
608 case EVENT_SRC_SYS_JSON:
609 pmu->sys_json_aliases++;
613 list_add_tail(&alias->list, &pmu->aliases);
617 static inline bool pmu_alias_info_file(const char *name)
622 if (len > 5 && !strcmp(name + len - 5, ".unit"))
624 if (len > 6 && !strcmp(name + len - 6, ".scale"))
626 if (len > 8 && !strcmp(name + len - 8, ".per-pkg"))
628 if (len > 9 && !strcmp(name + len - 9, ".snapshot"))
635 * Reading the pmu event aliases definition, which should be located at:
636 * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
638 static int __pmu_aliases_parse(struct perf_pmu *pmu, int events_dir_fd)
640 struct dirent *evt_ent;
643 event_dir = fdopendir(events_dir_fd);
647 while ((evt_ent = readdir(event_dir))) {
648 char *name = evt_ent->d_name;
652 if (!strcmp(name, ".") || !strcmp(name, ".."))
656 * skip info files parsed in perf_pmu__new_alias()
658 if (pmu_alias_info_file(name))
661 fd = openat(events_dir_fd, name, O_RDONLY);
663 pr_debug("Cannot open %s\n", name);
666 file = fdopen(fd, "r");
672 if (perf_pmu__new_alias(pmu, name, /*desc=*/ NULL,
673 /*val=*/ NULL, file, /*pe=*/ NULL,
674 EVENT_SRC_SYSFS) < 0)
675 pr_debug("Cannot set up %s\n", name);
680 pmu->sysfs_aliases_loaded = true;
684 static int pmu_aliases_parse(struct perf_pmu *pmu)
688 int events_dir_fd, ret;
690 if (pmu->sysfs_aliases_loaded)
693 len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path));
696 scnprintf(path + len, sizeof(path) - len, "%s/events", pmu->name);
698 events_dir_fd = open(path, O_DIRECTORY);
699 if (events_dir_fd == -1) {
700 pmu->sysfs_aliases_loaded = true;
703 ret = __pmu_aliases_parse(pmu, events_dir_fd);
704 close(events_dir_fd);
708 static int pmu_aliases_parse_eager(struct perf_pmu *pmu, int sysfs_fd)
710 char path[FILENAME_MAX + 7];
711 int ret, events_dir_fd;
713 scnprintf(path, sizeof(path), "%s/events", pmu->name);
714 events_dir_fd = openat(sysfs_fd, path, O_DIRECTORY, 0);
715 if (events_dir_fd == -1) {
716 pmu->sysfs_aliases_loaded = true;
719 ret = __pmu_aliases_parse(pmu, events_dir_fd);
720 close(events_dir_fd);
724 static int pmu_alias_terms(struct perf_pmu_alias *alias, int err_loc, struct list_head *terms)
726 struct parse_events_term *term, *cloned;
727 struct parse_events_terms clone_terms;
729 parse_events_terms__init(&clone_terms);
730 list_for_each_entry(term, &alias->terms.terms, list) {
731 int ret = parse_events_term__clone(&cloned, term);
734 parse_events_terms__exit(&clone_terms);
738 * Weak terms don't override command line options,
739 * which we don't want for implicit terms in aliases.
742 cloned->err_term = cloned->err_val = err_loc;
743 list_add_tail(&cloned->list, &clone_terms.terms);
745 list_splice_init(&clone_terms.terms, terms);
746 parse_events_terms__exit(&clone_terms);
751 * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64)
752 * may have a "cpus" file.
754 static struct perf_cpu_map *pmu_cpumask(int dirfd, const char *name, bool is_core)
756 struct perf_cpu_map *cpus;
757 const char *templates[] = {
762 const char **template;
763 char pmu_name[PATH_MAX];
764 struct perf_pmu pmu = {.name = pmu_name};
767 strlcpy(pmu_name, name, sizeof(pmu_name));
768 for (template = templates; *template; template++) {
769 file = perf_pmu__open_file_at(&pmu, dirfd, *template);
772 cpus = perf_cpu_map__read(file);
778 /* Nothing found, for core PMUs assume this means all CPUs. */
779 return is_core ? perf_cpu_map__get(cpu_map__online()) : NULL;
782 static bool pmu_is_uncore(int dirfd, const char *name)
786 fd = perf_pmu__pathname_fd(dirfd, name, "cpumask", O_PATH);
794 static char *pmu_id(const char *name)
796 char path[PATH_MAX], *str;
799 perf_pmu__pathname_scnprintf(path, sizeof(path), name, "identifier");
801 if (filename__read_str(path, &str, &len) < 0)
804 str[len - 1] = 0; /* remove line feed */
810 * is_sysfs_pmu_core() - PMU CORE devices have different name other than cpu in
811 * sysfs on some platforms like ARM or Intel hybrid. Looking for
812 * possible the cpus file in sysfs files to identify whether this is a
814 * @name: The PMU name such as "cpu_atom".
816 static int is_sysfs_pmu_core(const char *name)
820 if (!perf_pmu__pathname_scnprintf(path, sizeof(path), name, "cpus"))
822 return file_available(path);
825 char *perf_pmu__getcpuid(struct perf_pmu *pmu)
830 cpuid = getenv("PERF_CPUID");
832 cpuid = strdup(cpuid);
834 cpuid = get_cpuid_str(pmu);
839 pr_debug("Using CPUID %s\n", cpuid);
845 __weak const struct pmu_metrics_table *pmu_metrics_table__find(void)
847 return perf_pmu__find_metrics_table(NULL);
851 * perf_pmu__match_ignoring_suffix - Does the pmu_name match tok ignoring any
852 * trailing suffix? The Suffix must be in form
853 * tok_{digits}, or tok{digits}.
854 * @pmu_name: The pmu_name with possible suffix.
855 * @tok: The possible match to pmu_name without suffix.
857 static bool perf_pmu__match_ignoring_suffix(const char *pmu_name, const char *tok)
861 if (strncmp(pmu_name, tok, strlen(tok)))
864 p = pmu_name + strlen(tok);
871 /* Ensure we end in a number */
883 * pmu_uncore_alias_match - does name match the PMU name?
884 * @pmu_name: the json struct pmu_event name. This may lack a suffix (which
885 * matches) or be of the form "socket,pmuname" which will match
886 * "socketX_pmunameY".
887 * @name: a real full PMU name as from sysfs.
889 static bool pmu_uncore_alias_match(const char *pmu_name, const char *name)
891 char *tmp = NULL, *tok, *str;
894 if (strchr(pmu_name, ',') == NULL)
895 return perf_pmu__match_ignoring_suffix(name, pmu_name);
897 str = strdup(pmu_name);
902 * uncore alias may be from different PMU with common prefix
904 tok = strtok_r(str, ",", &tmp);
905 if (strncmp(pmu_name, tok, strlen(tok))) {
911 * Match more complex aliases where the alias name is a comma-delimited
912 * list of tokens, orderly contained in the matching PMU name.
914 * Example: For alias "socket,pmuname" and PMU "socketX_pmunameY", we
915 * match "socket" in "socketX_pmunameY" and then "pmuname" in
919 char *next_tok = strtok_r(NULL, ",", &tmp);
921 name = strstr(name, tok);
923 (!next_tok && !perf_pmu__match_ignoring_suffix(name, tok))) {
939 bool pmu_uncore_identifier_match(const char *compat, const char *id)
942 regmatch_t pmatch[1];
945 if (regcomp(&re, compat, REG_EXTENDED) != 0) {
946 /* Warn unable to generate match particular string. */
947 pr_info("Invalid regular expression %s\n", compat);
951 match = !regexec(&re, id, 1, pmatch, 0);
953 /* Ensure a full match. */
954 match = pmatch[0].rm_so == 0 && (size_t)pmatch[0].rm_eo == strlen(id);
961 static int pmu_add_cpu_aliases_map_callback(const struct pmu_event *pe,
962 const struct pmu_events_table *table __maybe_unused,
965 struct perf_pmu *pmu = vdata;
967 perf_pmu__new_alias(pmu, pe->name, pe->desc, pe->event, /*val_fd=*/ NULL,
968 pe, EVENT_SRC_CPU_JSON);
973 * From the pmu_events_table, find the events that correspond to the given
974 * PMU and add them to the list 'head'.
976 void pmu_add_cpu_aliases_table(struct perf_pmu *pmu, const struct pmu_events_table *table)
978 pmu_events_table__for_each_event(table, pmu, pmu_add_cpu_aliases_map_callback, pmu);
981 static void pmu_add_cpu_aliases(struct perf_pmu *pmu)
983 if (!pmu->events_table)
986 if (pmu->cpu_aliases_added)
989 pmu_add_cpu_aliases_table(pmu, pmu->events_table);
990 pmu->cpu_aliases_added = true;
993 static int pmu_add_sys_aliases_iter_fn(const struct pmu_event *pe,
994 const struct pmu_events_table *table __maybe_unused,
997 struct perf_pmu *pmu = vdata;
999 if (!pe->compat || !pe->pmu)
1002 if (pmu_uncore_alias_match(pe->pmu, pmu->name) &&
1003 pmu_uncore_identifier_match(pe->compat, pmu->id)) {
1004 perf_pmu__new_alias(pmu,
1010 EVENT_SRC_SYS_JSON);
1016 void pmu_add_sys_aliases(struct perf_pmu *pmu)
1021 pmu_for_each_sys_event(pmu_add_sys_aliases_iter_fn, pmu);
1024 static char *pmu_find_alias_name(struct perf_pmu *pmu, int dirfd)
1026 FILE *file = perf_pmu__open_file_at(pmu, dirfd, "alias");
1028 size_t line_len = 0;
1034 ret = getline(&line, &line_len, file);
1039 /* Remove trailing newline. */
1040 if (ret > 0 && line[ret - 1] == '\n')
1047 static int pmu_max_precise(int dirfd, struct perf_pmu *pmu)
1049 int max_precise = -1;
1051 perf_pmu__scan_file_at(pmu, dirfd, "caps/max_precise", "%d", &max_precise);
1056 perf_pmu__arch_init(struct perf_pmu *pmu)
1059 pmu->mem_events = perf_mem_events;
1062 struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char *name,
1065 struct perf_pmu *pmu;
1068 pmu = zalloc(sizeof(*pmu));
1072 pmu->name = strdup(name);
1077 * Read type early to fail fast if a lookup name isn't a PMU. Ensure
1078 * that type value is successfully assigned (return 1).
1080 if (perf_pmu__scan_file_at(pmu, dirfd, "type", "%u", &type) != 1)
1083 INIT_LIST_HEAD(&pmu->format);
1084 INIT_LIST_HEAD(&pmu->aliases);
1085 INIT_LIST_HEAD(&pmu->caps);
1088 * The pmu data we store & need consists of the pmu
1089 * type value and format definitions. Load both right
1092 if (pmu_format(pmu, dirfd, name, eager_load))
1095 pmu->is_core = is_pmu_core(name);
1096 pmu->cpus = pmu_cpumask(dirfd, name, pmu->is_core);
1099 pmu->is_uncore = pmu_is_uncore(dirfd, name);
1101 pmu->id = pmu_id(name);
1102 pmu->max_precise = pmu_max_precise(dirfd, pmu);
1103 pmu->alias_name = pmu_find_alias_name(pmu, dirfd);
1104 pmu->events_table = perf_pmu__find_events_table(pmu);
1106 * Load the sys json events/aliases when loading the PMU as each event
1107 * may have a different compat regular expression. We therefore can't
1108 * know the number of sys json events/aliases without computing the
1109 * regular expressions for them all.
1111 pmu_add_sys_aliases(pmu);
1112 list_add_tail(&pmu->list, pmus);
1114 perf_pmu__arch_init(pmu);
1117 pmu_aliases_parse_eager(pmu, dirfd);
1126 /* Creates the PMU when sysfs scanning fails. */
1127 struct perf_pmu *perf_pmu__create_placeholder_core_pmu(struct list_head *core_pmus)
1129 struct perf_pmu *pmu = zalloc(sizeof(*pmu));
1134 pmu->name = strdup("cpu");
1140 pmu->is_core = true;
1141 pmu->type = PERF_TYPE_RAW;
1142 pmu->cpus = cpu_map__online();
1144 INIT_LIST_HEAD(&pmu->format);
1145 INIT_LIST_HEAD(&pmu->aliases);
1146 INIT_LIST_HEAD(&pmu->caps);
1147 list_add_tail(&pmu->list, core_pmus);
1151 void perf_pmu__warn_invalid_formats(struct perf_pmu *pmu)
1153 struct perf_pmu_format *format;
1155 if (pmu->formats_checked)
1158 pmu->formats_checked = true;
1160 /* fake pmu doesn't have format list */
1161 if (pmu == &perf_pmu__fake)
1164 list_for_each_entry(format, &pmu->format, list) {
1165 perf_pmu_format__load(pmu, format);
1166 if (format->value >= PERF_PMU_FORMAT_VALUE_CONFIG_END) {
1167 pr_warning("WARNING: '%s' format '%s' requires 'perf_event_attr::config%d'"
1168 "which is not supported by this version of perf!\n",
1169 pmu->name, format->name, format->value);
1175 bool evsel__is_aux_event(const struct evsel *evsel)
1177 struct perf_pmu *pmu = evsel__find_pmu(evsel);
1179 return pmu && pmu->auxtrace;
1183 * Set @config_name to @val as long as the user hasn't already set or cleared it
1184 * by passing a config term on the command line.
1186 * @val is the value to put into the bits specified by @config_name rather than
1187 * the bit pattern. It is shifted into position by this function, so to set
1188 * something to true, pass 1 for val rather than a pre shifted value.
1190 #define field_prep(_mask, _val) (((_val) << (ffsll(_mask) - 1)) & (_mask))
1191 void evsel__set_config_if_unset(struct perf_pmu *pmu, struct evsel *evsel,
1192 const char *config_name, u64 val)
1194 u64 user_bits = 0, bits;
1195 struct evsel_config_term *term = evsel__get_config_term(evsel, CFG_CHG);
1198 user_bits = term->val.cfg_chg;
1200 bits = perf_pmu__format_bits(pmu, config_name);
1202 /* Do nothing if the user changed the value */
1203 if (bits & user_bits)
1206 /* Otherwise replace it */
1207 evsel->core.attr.config &= ~bits;
1208 evsel->core.attr.config |= field_prep(bits, val);
1211 static struct perf_pmu_format *
1212 pmu_find_format(const struct list_head *formats, const char *name)
1214 struct perf_pmu_format *format;
1216 list_for_each_entry(format, formats, list)
1217 if (!strcmp(format->name, name))
1223 __u64 perf_pmu__format_bits(struct perf_pmu *pmu, const char *name)
1225 struct perf_pmu_format *format = pmu_find_format(&pmu->format, name);
1232 for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
1233 bits |= 1ULL << fbit;
1238 int perf_pmu__format_type(struct perf_pmu *pmu, const char *name)
1240 struct perf_pmu_format *format = pmu_find_format(&pmu->format, name);
1245 perf_pmu_format__load(pmu, format);
1246 return format->value;
1250 * Sets value based on the format definition (format parameter)
1251 * and unformatted value (value parameter).
1253 static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
1256 unsigned long fbit, vbit;
1258 for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
1260 if (!test_bit(fbit, format))
1263 if (value & (1llu << vbit++))
1264 *v |= (1llu << fbit);
1266 *v &= ~(1llu << fbit);
1270 static __u64 pmu_format_max_value(const unsigned long *format)
1274 w = bitmap_weight(format, PERF_PMU_FORMAT_BITS);
1278 return (1ULL << w) - 1;
1283 * Term is a string term, and might be a param-term. Try to look up it's value
1284 * in the remaining terms.
1285 * - We have a term like "base-or-format-term=param-term",
1286 * - We need to find the value supplied for "param-term" (with param-term named
1287 * in a config string) later on in the term list.
1289 static int pmu_resolve_param_term(struct parse_events_term *term,
1290 struct parse_events_terms *head_terms,
1293 struct parse_events_term *t;
1295 list_for_each_entry(t, &head_terms->terms, list) {
1296 if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM &&
1297 t->config && !strcmp(t->config, term->config)) {
1299 *value = t->val.num;
1305 printf("Required parameter '%s' not specified\n", term->config);
1310 static char *pmu_formats_string(const struct list_head *formats)
1312 struct perf_pmu_format *format;
1314 struct strbuf buf = STRBUF_INIT;
1320 /* sysfs exported terms */
1321 list_for_each_entry(format, formats, list)
1322 if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
1325 str = strbuf_detach(&buf, NULL);
1327 strbuf_release(&buf);
1333 * Setup one of config[12] attr members based on the
1334 * user input data - term parameter.
1336 static int pmu_config_term(const struct perf_pmu *pmu,
1337 struct perf_event_attr *attr,
1338 struct parse_events_term *term,
1339 struct parse_events_terms *head_terms,
1340 bool zero, struct parse_events_error *err)
1342 struct perf_pmu_format *format;
1347 * If this is a parameter we've already used for parameterized-eval,
1348 * skip it in normal eval.
1354 * Hardcoded terms should be already in, so nothing
1355 * to be done for them.
1357 if (parse_events__is_hardcoded_term(term))
1360 format = pmu_find_format(&pmu->format, term->config);
1362 char *pmu_term = pmu_formats_string(&pmu->format);
1366 if (asprintf(&unknown_term,
1367 "unknown term '%s' for pmu '%s'",
1368 term->config, pmu->name) < 0)
1369 unknown_term = NULL;
1370 help_msg = parse_events_formats_error_string(pmu_term);
1372 parse_events_error__handle(err, term->err_term,
1376 pr_debug("%s (%s)\n", unknown_term, help_msg);
1382 perf_pmu_format__load(pmu, format);
1383 switch (format->value) {
1384 case PERF_PMU_FORMAT_VALUE_CONFIG:
1387 case PERF_PMU_FORMAT_VALUE_CONFIG1:
1388 vp = &attr->config1;
1390 case PERF_PMU_FORMAT_VALUE_CONFIG2:
1391 vp = &attr->config2;
1393 case PERF_PMU_FORMAT_VALUE_CONFIG3:
1394 vp = &attr->config3;
1401 * Either directly use a numeric term, or try to translate string terms
1402 * using event parameters.
1404 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1405 if (term->no_value &&
1406 bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) {
1408 parse_events_error__handle(err, term->err_val,
1409 strdup("no value assigned for term"),
1415 val = term->val.num;
1416 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1417 if (strcmp(term->val.str, "?")) {
1419 pr_info("Invalid sysfs entry %s=%s\n",
1420 term->config, term->val.str);
1423 parse_events_error__handle(err, term->err_val,
1424 strdup("expected numeric value"),
1430 if (pmu_resolve_param_term(term, head_terms, &val))
1435 max_val = pmu_format_max_value(format->bits);
1436 if (val > max_val) {
1440 parse_events_error__handle(err, term->err_val,
1442 "value too big for format (%s), maximum is %llu",
1443 format->name, (unsigned long long)max_val) < 0
1444 ? strdup("value too big for format")
1450 * Assume we don't care if !err, in which case the value will be
1451 * silently truncated.
1455 pmu_format_value(format->bits, val, vp, zero);
1459 int perf_pmu__config_terms(const struct perf_pmu *pmu,
1460 struct perf_event_attr *attr,
1461 struct parse_events_terms *terms,
1462 bool zero, struct parse_events_error *err)
1464 struct parse_events_term *term;
1466 list_for_each_entry(term, &terms->terms, list) {
1467 if (pmu_config_term(pmu, attr, term, terms, zero, err))
1475 * Configures event's 'attr' parameter based on the:
1476 * 1) users input - specified in terms parameter
1477 * 2) pmu format definitions - specified by pmu parameter
1479 int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
1480 struct parse_events_terms *head_terms,
1481 struct parse_events_error *err)
1483 bool zero = !!pmu->perf_event_attr_init_default;
1485 return perf_pmu__config_terms(pmu, attr, head_terms, zero, err);
1488 static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
1489 struct parse_events_term *term)
1491 struct perf_pmu_alias *alias;
1494 if (parse_events__is_hardcoded_term(term))
1497 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1498 if (!term->no_value)
1500 if (pmu_find_format(&pmu->format, term->config))
1502 name = term->config;
1504 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1505 if (strcasecmp(term->config, "event"))
1507 name = term->val.str;
1512 alias = perf_pmu__find_alias(pmu, name, /*load=*/ true);
1513 if (alias || pmu->cpu_aliases_added)
1516 /* Alias doesn't exist, try to get it from the json events. */
1517 if (pmu->events_table &&
1518 pmu_events_table__find_event(pmu->events_table, pmu, name,
1519 pmu_add_cpu_aliases_map_callback,
1521 alias = perf_pmu__find_alias(pmu, name, /*load=*/ false);
1527 static int check_info_data(struct perf_pmu *pmu,
1528 struct perf_pmu_alias *alias,
1529 struct perf_pmu_info *info,
1530 struct parse_events_error *err,
1533 read_alias_info(pmu, alias);
1535 * Only one term in event definition can
1536 * define unit, scale and snapshot, fail
1537 * if there's more than one.
1539 if (info->unit && alias->unit[0]) {
1540 parse_events_error__handle(err, column,
1541 strdup("Attempt to set event's unit twice"),
1545 if (info->scale && alias->scale) {
1546 parse_events_error__handle(err, column,
1547 strdup("Attempt to set event's scale twice"),
1551 if (info->snapshot && alias->snapshot) {
1552 parse_events_error__handle(err, column,
1553 strdup("Attempt to set event snapshot twice"),
1559 info->unit = alias->unit;
1562 info->scale = alias->scale;
1564 if (alias->snapshot)
1565 info->snapshot = alias->snapshot;
1571 * Find alias in the terms list and replace it with the terms
1572 * defined for the alias
1574 int perf_pmu__check_alias(struct perf_pmu *pmu, struct parse_events_terms *head_terms,
1575 struct perf_pmu_info *info, bool *rewrote_terms,
1576 struct parse_events_error *err)
1578 struct parse_events_term *term, *h;
1579 struct perf_pmu_alias *alias;
1582 *rewrote_terms = false;
1583 info->per_pkg = false;
1586 * Mark unit and scale as not set
1587 * (different from default values, see below)
1591 info->snapshot = false;
1593 list_for_each_entry_safe(term, h, &head_terms->terms, list) {
1594 alias = pmu_find_alias(pmu, term);
1597 ret = pmu_alias_terms(alias, term->err_term, &term->list);
1599 parse_events_error__handle(err, term->err_term,
1600 strdup("Failure to duplicate terms"),
1604 *rewrote_terms = true;
1605 ret = check_info_data(pmu, alias, info, err, term->err_term);
1610 info->per_pkg = true;
1612 list_del_init(&term->list);
1613 parse_events_term__delete(term);
1617 * if no unit or scale found in aliases, then
1618 * set defaults as for evsel
1619 * unit cannot left to NULL
1621 if (info->unit == NULL)
1624 if (info->scale == 0.0)
1630 struct find_event_args {
1633 pmu_event_callback cb;
1636 static int find_event_callback(void *state, struct pmu_event_info *info)
1638 struct find_event_args *args = state;
1640 if (!strcmp(args->event, info->name))
1641 return args->cb(args->state, info);
1646 int perf_pmu__find_event(struct perf_pmu *pmu, const char *event, void *state, pmu_event_callback cb)
1648 struct find_event_args args = {
1654 /* Sub-optimal, but function is only used by tests. */
1655 return perf_pmu__for_each_event(pmu, /*skip_duplicate_pmus=*/ false,
1656 &args, find_event_callback);
1659 static void perf_pmu__del_formats(struct list_head *formats)
1661 struct perf_pmu_format *fmt, *tmp;
1663 list_for_each_entry_safe(fmt, tmp, formats, list) {
1664 list_del(&fmt->list);
1670 bool perf_pmu__has_format(const struct perf_pmu *pmu, const char *name)
1672 struct perf_pmu_format *format;
1674 list_for_each_entry(format, &pmu->format, list) {
1675 if (!strcmp(format->name, name))
1681 int perf_pmu__for_each_format(struct perf_pmu *pmu, void *state, pmu_format_callback cb)
1683 static const char *const terms[] = {
1684 "config=0..0xffffffffffffffff",
1685 "config1=0..0xffffffffffffffff",
1686 "config2=0..0xffffffffffffffff",
1687 "config3=0..0xffffffffffffffff",
1691 "branch_type=(u|k|hv|any|...)",
1693 "call-graph=(fp|dwarf|lbr)",
1694 "stack-size=number",
1703 "aux-sample-size=number",
1705 struct perf_pmu_format *format;
1709 * max-events and driver-config are missing above as are the internal
1710 * types user, metric-id, raw, legacy cache and hardware. Assert against
1711 * the enum parse_events__term_type so they are kept in sync.
1713 _Static_assert(ARRAY_SIZE(terms) == __PARSE_EVENTS__TERM_TYPE_NR - 6,
1714 "perf_pmu__for_each_format()'s terms must be kept in sync with enum parse_events__term_type");
1715 list_for_each_entry(format, &pmu->format, list) {
1716 perf_pmu_format__load(pmu, format);
1717 ret = cb(state, format->name, (int)format->value, format->bits);
1724 for (size_t i = 0; i < ARRAY_SIZE(terms); i++) {
1725 int config = PERF_PMU_FORMAT_VALUE_CONFIG;
1727 if (i < PERF_PMU_FORMAT_VALUE_CONFIG_END)
1730 ret = cb(state, terms[i], config, /*bits=*/NULL);
1737 bool is_pmu_core(const char *name)
1739 return !strcmp(name, "cpu") || !strcmp(name, "cpum_cf") || is_sysfs_pmu_core(name);
1742 bool perf_pmu__supports_legacy_cache(const struct perf_pmu *pmu)
1744 return pmu->is_core;
1747 bool perf_pmu__auto_merge_stats(const struct perf_pmu *pmu)
1749 return !pmu->is_core || perf_pmus__num_core_pmus() == 1;
1752 bool perf_pmu__have_event(struct perf_pmu *pmu, const char *name)
1756 if (perf_pmu__find_alias(pmu, name, /*load=*/ true) != NULL)
1758 if (pmu->cpu_aliases_added || !pmu->events_table)
1760 return pmu_events_table__find_event(pmu->events_table, pmu, name, NULL, NULL) == 0;
1763 size_t perf_pmu__num_events(struct perf_pmu *pmu)
1767 pmu_aliases_parse(pmu);
1768 nr = pmu->sysfs_aliases + pmu->sys_json_aliases;;
1770 if (pmu->cpu_aliases_added)
1771 nr += pmu->cpu_json_aliases;
1772 else if (pmu->events_table)
1773 nr += pmu_events_table__num_events(pmu->events_table, pmu) - pmu->cpu_json_aliases;
1775 assert(pmu->cpu_json_aliases == 0);
1777 return pmu->selectable ? nr + 1 : nr;
1780 static int sub_non_neg(int a, int b)
1787 static char *format_alias(char *buf, int len, const struct perf_pmu *pmu,
1788 const struct perf_pmu_alias *alias, bool skip_duplicate_pmus)
1790 struct parse_events_term *term;
1791 int pmu_name_len = skip_duplicate_pmus
1792 ? pmu_name_len_no_suffix(pmu->name, /*num=*/NULL)
1793 : (int)strlen(pmu->name);
1794 int used = snprintf(buf, len, "%.*s/%s", pmu_name_len, pmu->name, alias->name);
1796 list_for_each_entry(term, &alias->terms.terms, list) {
1797 if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
1798 used += snprintf(buf + used, sub_non_neg(len, used),
1799 ",%s=%s", term->config,
1803 if (sub_non_neg(len, used) > 0) {
1807 if (sub_non_neg(len, used) > 0) {
1811 buf[len - 1] = '\0';
1816 int perf_pmu__for_each_event(struct perf_pmu *pmu, bool skip_duplicate_pmus,
1817 void *state, pmu_event_callback cb)
1820 struct perf_pmu_alias *event;
1821 struct pmu_event_info info = {
1827 strbuf_init(&sb, /*hint=*/ 0);
1828 pmu_aliases_parse(pmu);
1829 pmu_add_cpu_aliases(pmu);
1830 list_for_each_entry(event, &pmu->aliases, list) {
1834 info.pmu_name = event->pmu_name ?: pmu->name;
1835 pmu_name_len = skip_duplicate_pmus
1836 ? pmu_name_len_no_suffix(info.pmu_name, /*num=*/NULL)
1837 : (int)strlen(info.pmu_name);
1840 info.name = event->name;
1843 info.name = format_alias(buf, sizeof(buf), pmu, event,
1844 skip_duplicate_pmus);
1846 info.alias = info.name;
1847 info.name = event->name;
1849 buf_used = strlen(buf) + 1;
1851 info.scale_unit = NULL;
1852 if (strlen(event->unit) || event->scale != 1.0) {
1853 info.scale_unit = buf + buf_used;
1854 buf_used += snprintf(buf + buf_used, sizeof(buf) - buf_used,
1855 "%G%s", event->scale, event->unit) + 1;
1857 info.desc = event->desc;
1858 info.long_desc = event->long_desc;
1859 info.encoding_desc = buf + buf_used;
1860 parse_events_terms__to_strbuf(&event->terms, &sb);
1861 buf_used += snprintf(buf + buf_used, sizeof(buf) - buf_used,
1862 "%.*s/%s/", pmu_name_len, info.pmu_name, sb.buf) + 1;
1863 info.topic = event->topic;
1865 info.deprecated = event->deprecated;
1866 ret = cb(state, &info);
1869 strbuf_setlen(&sb, /*len=*/ 0);
1871 if (pmu->selectable) {
1873 snprintf(buf, sizeof(buf), "%s//", pmu->name);
1875 info.scale_unit = NULL;
1877 info.long_desc = NULL;
1878 info.encoding_desc = NULL;
1880 info.pmu_name = pmu->name;
1881 info.deprecated = false;
1882 ret = cb(state, &info);
1885 strbuf_release(&sb);
1889 bool pmu__name_match(const struct perf_pmu *pmu, const char *pmu_name)
1891 return !strcmp(pmu->name, pmu_name) ||
1892 (pmu->is_uncore && pmu_uncore_alias_match(pmu_name, pmu->name)) ||
1894 * jevents and tests use default_core as a marker for any core
1895 * PMU as the PMU name varies across architectures.
1897 (pmu->is_core && !strcmp(pmu_name, "default_core"));
1900 bool perf_pmu__is_software(const struct perf_pmu *pmu)
1902 const char *known_sw_pmus[] = {
1908 if (pmu->is_core || pmu->is_uncore || pmu->auxtrace)
1910 switch (pmu->type) {
1911 case PERF_TYPE_HARDWARE: return false;
1912 case PERF_TYPE_SOFTWARE: return true;
1913 case PERF_TYPE_TRACEPOINT: return true;
1914 case PERF_TYPE_HW_CACHE: return false;
1915 case PERF_TYPE_RAW: return false;
1916 case PERF_TYPE_BREAKPOINT: return true;
1919 for (size_t i = 0; i < ARRAY_SIZE(known_sw_pmus); i++) {
1920 if (!strcmp(pmu->name, known_sw_pmus[i]))
1926 FILE *perf_pmu__open_file(const struct perf_pmu *pmu, const char *name)
1928 char path[PATH_MAX];
1930 if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, name) ||
1931 !file_available(path))
1934 return fopen(path, "r");
1937 FILE *perf_pmu__open_file_at(const struct perf_pmu *pmu, int dirfd, const char *name)
1941 fd = perf_pmu__pathname_fd(dirfd, pmu->name, name, O_RDONLY);
1945 return fdopen(fd, "r");
1948 int perf_pmu__scan_file(const struct perf_pmu *pmu, const char *name, const char *fmt,
1955 va_start(args, fmt);
1956 file = perf_pmu__open_file(pmu, name);
1958 ret = vfscanf(file, fmt, args);
1965 int perf_pmu__scan_file_at(const struct perf_pmu *pmu, int dirfd, const char *name,
1966 const char *fmt, ...)
1972 va_start(args, fmt);
1973 file = perf_pmu__open_file_at(pmu, dirfd, name);
1975 ret = vfscanf(file, fmt, args);
1982 bool perf_pmu__file_exists(const struct perf_pmu *pmu, const char *name)
1984 char path[PATH_MAX];
1986 if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, name))
1989 return file_available(path);
1992 static int perf_pmu__new_caps(struct list_head *list, char *name, char *value)
1994 struct perf_pmu_caps *caps = zalloc(sizeof(*caps));
1999 caps->name = strdup(name);
2002 caps->value = strndup(value, strlen(value) - 1);
2005 list_add_tail(&caps->list, list);
2016 static void perf_pmu__del_caps(struct perf_pmu *pmu)
2018 struct perf_pmu_caps *caps, *tmp;
2020 list_for_each_entry_safe(caps, tmp, &pmu->caps, list) {
2021 list_del(&caps->list);
2023 zfree(&caps->value);
2029 * Reading/parsing the given pmu capabilities, which should be located at:
2030 * /sys/bus/event_source/devices/<dev>/caps as sysfs group attributes.
2031 * Return the number of capabilities
2033 int perf_pmu__caps_parse(struct perf_pmu *pmu)
2036 char caps_path[PATH_MAX];
2038 struct dirent *evt_ent;
2041 if (pmu->caps_initialized)
2042 return pmu->nr_caps;
2046 if (!perf_pmu__pathname_scnprintf(caps_path, sizeof(caps_path), pmu->name, "caps"))
2049 if (stat(caps_path, &st) < 0) {
2050 pmu->caps_initialized = true;
2051 return 0; /* no error if caps does not exist */
2054 caps_dir = opendir(caps_path);
2058 caps_fd = dirfd(caps_dir);
2060 while ((evt_ent = readdir(caps_dir)) != NULL) {
2061 char *name = evt_ent->d_name;
2066 if (!strcmp(name, ".") || !strcmp(name, ".."))
2069 fd = openat(caps_fd, name, O_RDONLY);
2072 file = fdopen(fd, "r");
2078 if (!fgets(value, sizeof(value), file) ||
2079 (perf_pmu__new_caps(&pmu->caps, name, value) < 0)) {
2090 pmu->caps_initialized = true;
2091 return pmu->nr_caps;
2094 static void perf_pmu__compute_config_masks(struct perf_pmu *pmu)
2096 struct perf_pmu_format *format;
2098 if (pmu->config_masks_computed)
2101 list_for_each_entry(format, &pmu->format, list) {
2105 if (format->value >= PERF_PMU_FORMAT_VALUE_CONFIG_END)
2108 pmu->config_masks_present = true;
2109 mask = &pmu->config_masks[format->value];
2111 for_each_set_bit(i, format->bits, PERF_PMU_FORMAT_BITS)
2114 pmu->config_masks_computed = true;
2117 void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config,
2118 const char *name, int config_num,
2119 const char *config_name)
2124 perf_pmu__compute_config_masks(pmu);
2127 * Kernel doesn't export any valid format bits.
2129 if (!pmu->config_masks_present)
2132 bits = config & ~pmu->config_masks[config_num];
2136 bitmap_scnprintf((unsigned long *)&bits, sizeof(bits) * 8, buf, sizeof(buf));
2138 pr_warning("WARNING: event '%s' not valid (bits %s of %s "
2139 "'%llx' not supported by kernel)!\n",
2140 name ?: "N/A", buf, config_name, config);
2143 bool perf_pmu__match(const struct perf_pmu *pmu, const char *tok)
2145 const char *name = pmu->name;
2146 bool need_fnmatch = strchr(tok, '*') != NULL;
2148 if (!strncmp(tok, "uncore_", 7))
2150 if (!strncmp(name, "uncore_", 7))
2153 if (perf_pmu__match_ignoring_suffix(name, tok) ||
2154 (need_fnmatch && !fnmatch(tok, name, 0)))
2157 name = pmu->alias_name;
2161 if (!strncmp(name, "uncore_", 7))
2164 return perf_pmu__match_ignoring_suffix(name, tok) ||
2165 (need_fnmatch && !fnmatch(tok, name, 0));
2168 double __weak perf_pmu__cpu_slots_per_cycle(void)
2173 int perf_pmu__event_source_devices_scnprintf(char *pathname, size_t size)
2175 const char *sysfs = sysfs__mountpoint();
2179 return scnprintf(pathname, size, "%s/bus/event_source/devices/", sysfs);
2182 int perf_pmu__event_source_devices_fd(void)
2184 char path[PATH_MAX];
2185 const char *sysfs = sysfs__mountpoint();
2190 scnprintf(path, sizeof(path), "%s/bus/event_source/devices/", sysfs);
2191 return open(path, O_DIRECTORY);
2195 * Fill 'buf' with the path to a file or folder in 'pmu_name' in
2196 * sysfs. For example if pmu_name = "cs_etm" and 'filename' = "format"
2197 * then pathname will be filled with
2198 * "/sys/bus/event_source/devices/cs_etm/format"
2200 * Return 0 if the sysfs mountpoint couldn't be found, if no characters were
2201 * written or if the buffer size is exceeded.
2203 int perf_pmu__pathname_scnprintf(char *buf, size_t size,
2204 const char *pmu_name, const char *filename)
2208 len = perf_pmu__event_source_devices_scnprintf(buf, size);
2209 if (!len || (len + strlen(pmu_name) + strlen(filename) + 1) >= size)
2212 return scnprintf(buf + len, size - len, "%s/%s", pmu_name, filename);
2215 int perf_pmu__pathname_fd(int dirfd, const char *pmu_name, const char *filename, int flags)
2217 char path[PATH_MAX];
2219 scnprintf(path, sizeof(path), "%s/%s", pmu_name, filename);
2220 return openat(dirfd, path, flags);
2223 void perf_pmu__delete(struct perf_pmu *pmu)
2225 perf_pmu__del_formats(&pmu->format);
2226 perf_pmu__del_aliases(pmu);
2227 perf_pmu__del_caps(pmu);
2229 perf_cpu_map__put(pmu->cpus);
2232 zfree(&pmu->alias_name);
2237 const char *perf_pmu__name_from_config(struct perf_pmu *pmu, u64 config)
2239 struct perf_pmu_alias *event;
2244 pmu_aliases_parse(pmu);
2245 pmu_add_cpu_aliases(pmu);
2246 list_for_each_entry(event, &pmu->aliases, list) {
2247 struct perf_event_attr attr = {.config = 0,};
2248 int ret = perf_pmu__config(pmu, &attr, &event->terms, NULL);
2250 if (ret == 0 && config == attr.config)