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"
32 struct perf_pmu perf_pmu__fake = {
36 #define UNIT_MAX_LEN 31 /* max length for event unit name */
39 * struct perf_pmu_alias - An event either read from sysfs or builtin in
40 * pmu-events.c, created by parsing the pmu-events json files.
42 struct perf_pmu_alias {
43 /** @name: Name of the event like "mem-loads". */
45 /** @desc: Optional short description of the event. */
47 /** @long_desc: Optional long description. */
50 * @topic: Optional topic such as cache or pipeline, particularly for
54 /** @terms: Owned list of the original parsed parameters. */
55 struct list_head terms;
56 /** @list: List element of struct perf_pmu aliases. */
57 struct list_head list;
59 * @pmu_name: The name copied from the json struct pmu_event. This can
60 * differ from the PMU name as it won't have suffixes.
63 /** @unit: Units for the event, such as bytes or cache lines. */
64 char unit[UNIT_MAX_LEN+1];
65 /** @scale: Value to scale read counter values by. */
68 * @per_pkg: Does the file
69 * <sysfs>/bus/event_source/devices/<pmu_name>/events/<name>.per-pkg or
70 * equivalent json value exist and have the value 1.
74 * @snapshot: Does the file
75 * <sysfs>/bus/event_source/devices/<pmu_name>/events/<name>.snapshot
76 * exist and have the value 1.
80 * @deprecated: Is the event hidden and so not shown in perf list by
84 /** @from_sysfs: Was the alias from sysfs or a json event? */
86 /** @info_loaded: Have the scale, unit and other values been read from disk? */
91 * struct perf_pmu_format - Values from a format file read from
92 * <sysfs>/devices/cpu/format/ held in struct perf_pmu.
94 * For example, the contents of <sysfs>/devices/cpu/format/event may be
95 * "config:0-7" and will be represented here as name="event",
96 * value=PERF_PMU_FORMAT_VALUE_CONFIG and bits 0 to 7 will be set.
98 struct perf_pmu_format {
99 /** @list: Element on list within struct perf_pmu. */
100 struct list_head list;
101 /** @bits: Which config bits are set by this format value. */
102 DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
103 /** @name: The modifier/file name. */
106 * @value : Which config value the format relates to. Supported values
107 * are from PERF_PMU_FORMAT_VALUE_CONFIG to
108 * PERF_PMU_FORMAT_VALUE_CONFIG_END.
111 /** @loaded: Has the contents been loaded/parsed. */
115 static int pmu_aliases_parse(struct perf_pmu *pmu);
117 static struct perf_pmu_format *perf_pmu__new_format(struct list_head *list, char *name)
119 struct perf_pmu_format *format;
121 format = zalloc(sizeof(*format));
125 format->name = strdup(name);
130 list_add_tail(&format->list, list);
134 /* Called at the end of parsing a format. */
135 void perf_pmu_format__set_value(void *vformat, int config, unsigned long *bits)
137 struct perf_pmu_format *format = vformat;
139 format->value = config;
140 memcpy(format->bits, bits, sizeof(format->bits));
143 static void __perf_pmu_format__load(struct perf_pmu_format *format, FILE *file)
148 ret = perf_pmu_lex_init(&scanner);
152 perf_pmu_set_in(file, scanner);
153 ret = perf_pmu_parse(format, scanner);
154 perf_pmu_lex_destroy(scanner);
155 format->loaded = true;
158 static void perf_pmu_format__load(struct perf_pmu *pmu, struct perf_pmu_format *format)
166 if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, "format"))
169 assert(strlen(path) + strlen(format->name) + 2 < sizeof(path));
171 strcat(path, format->name);
173 file = fopen(path, "r");
176 __perf_pmu_format__load(format, file);
181 * Parse & process all the sysfs attributes located under
182 * the directory specified in 'dir' parameter.
184 int perf_pmu__format_parse(struct perf_pmu *pmu, int dirfd, bool eager_load)
186 struct dirent *evt_ent;
190 format_dir = fdopendir(dirfd);
194 while ((evt_ent = readdir(format_dir)) != NULL) {
195 struct perf_pmu_format *format;
196 char *name = evt_ent->d_name;
198 if (!strcmp(name, ".") || !strcmp(name, ".."))
201 format = perf_pmu__new_format(&pmu->format, name);
209 int fd = openat(dirfd, name, O_RDONLY);
215 file = fdopen(fd, "r");
220 __perf_pmu_format__load(format, file);
225 closedir(format_dir);
230 * Reading/parsing the default pmu format definition, which should be
232 * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
234 static int pmu_format(struct perf_pmu *pmu, int dirfd, const char *name)
238 fd = perf_pmu__pathname_fd(dirfd, name, "format", O_DIRECTORY);
242 /* it'll close the fd */
243 if (perf_pmu__format_parse(pmu, fd, /*eager_load=*/false))
249 int perf_pmu__convert_scale(const char *scale, char **end, double *sval)
255 * save current locale
257 lc = setlocale(LC_NUMERIC, NULL);
260 * The lc string may be allocated in static storage,
261 * so get a dynamic copy to make it survive setlocale
271 * force to C locale to ensure kernel
272 * scale string is converted correctly.
273 * kernel uses default C locale.
275 setlocale(LC_NUMERIC, "C");
277 *sval = strtod(scale, end);
281 setlocale(LC_NUMERIC, lc);
286 static int perf_pmu__parse_scale(struct perf_pmu *pmu, struct perf_pmu_alias *alias)
295 len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path));
298 scnprintf(path + len, sizeof(path) - len, "%s/events/%s.scale", pmu->name, alias->name);
300 fd = open(path, O_RDONLY);
304 if (fstat(fd, &st) < 0)
307 sret = read(fd, scale, sizeof(scale)-1);
311 if (scale[sret - 1] == '\n')
312 scale[sret - 1] = '\0';
316 ret = perf_pmu__convert_scale(scale, NULL, &alias->scale);
322 static int perf_pmu__parse_unit(struct perf_pmu *pmu, struct perf_pmu_alias *alias)
330 len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path));
333 scnprintf(path + len, sizeof(path) - len, "%s/events/%s.unit", pmu->name, alias->name);
335 fd = open(path, O_RDONLY);
339 sret = read(fd, alias->unit, UNIT_MAX_LEN);
345 if (alias->unit[sret - 1] == '\n')
346 alias->unit[sret - 1] = '\0';
348 alias->unit[sret] = '\0';
353 alias->unit[0] = '\0';
358 perf_pmu__parse_per_pkg(struct perf_pmu *pmu, struct perf_pmu_alias *alias)
364 len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path));
367 scnprintf(path + len, sizeof(path) - len, "%s/events/%s.per-pkg", pmu->name, alias->name);
369 fd = open(path, O_RDONLY);
375 alias->per_pkg = true;
379 static int perf_pmu__parse_snapshot(struct perf_pmu *pmu, struct perf_pmu_alias *alias)
385 len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path));
388 scnprintf(path + len, sizeof(path) - len, "%s/events/%s.snapshot", pmu->name, alias->name);
390 fd = open(path, O_RDONLY);
394 alias->snapshot = true;
399 /* Delete an alias entry. */
400 static void perf_pmu_free_alias(struct perf_pmu_alias *newalias)
402 zfree(&newalias->name);
403 zfree(&newalias->desc);
404 zfree(&newalias->long_desc);
405 zfree(&newalias->topic);
406 zfree(&newalias->pmu_name);
407 parse_events_terms__purge(&newalias->terms);
411 static void perf_pmu__del_aliases(struct perf_pmu *pmu)
413 struct perf_pmu_alias *alias, *tmp;
415 list_for_each_entry_safe(alias, tmp, &pmu->aliases, list) {
416 list_del(&alias->list);
417 perf_pmu_free_alias(alias);
421 static struct perf_pmu_alias *perf_pmu__find_alias(struct perf_pmu *pmu,
425 struct perf_pmu_alias *alias;
427 if (load && !pmu->sysfs_aliases_loaded)
428 pmu_aliases_parse(pmu);
430 list_for_each_entry(alias, &pmu->aliases, list) {
431 if (!strcasecmp(alias->name, name))
437 static bool assign_str(const char *name, const char *field, char **old_str,
440 if (!*old_str && new_str) {
441 *old_str = strdup(new_str);
445 if (!new_str || !strcasecmp(*old_str, new_str))
446 return false; /* Nothing to update. */
448 pr_debug("alias %s differs in field '%s' ('%s' != '%s')\n",
449 name, field, *old_str, new_str);
451 *old_str = strdup(new_str);
455 static void read_alias_info(struct perf_pmu *pmu, struct perf_pmu_alias *alias)
457 if (!alias->from_sysfs || alias->info_loaded)
461 * load unit name and scale if available
463 perf_pmu__parse_unit(pmu, alias);
464 perf_pmu__parse_scale(pmu, alias);
465 perf_pmu__parse_per_pkg(pmu, alias);
466 perf_pmu__parse_snapshot(pmu, alias);
469 struct update_alias_data {
470 struct perf_pmu *pmu;
471 struct perf_pmu_alias *alias;
474 static int update_alias(const struct pmu_event *pe,
475 const struct pmu_events_table *table __maybe_unused,
478 struct update_alias_data *data = vdata;
481 read_alias_info(data->pmu, data->alias);
482 assign_str(pe->name, "desc", &data->alias->desc, pe->desc);
483 assign_str(pe->name, "long_desc", &data->alias->long_desc, pe->long_desc);
484 assign_str(pe->name, "topic", &data->alias->topic, pe->topic);
485 data->alias->per_pkg = pe->perpkg;
487 parse_events_terms__purge(&data->alias->terms);
488 ret = parse_events_terms(&data->alias->terms, pe->event, /*input=*/NULL);
490 if (!ret && pe->unit) {
493 ret = perf_pmu__convert_scale(pe->unit, &unit, &data->alias->scale);
495 snprintf(data->alias->unit, sizeof(data->alias->unit), "%s", unit);
500 static int perf_pmu__new_alias(struct perf_pmu *pmu, const char *name,
501 const char *desc, const char *val, FILE *val_fd,
502 const struct pmu_event *pe)
504 struct perf_pmu_alias *alias;
506 const char *long_desc = NULL, *topic = NULL, *unit = NULL, *pmu_name = NULL;
507 bool deprecated = false, perpkg = false;
509 if (perf_pmu__find_alias(pmu, name, /*load=*/ false)) {
510 /* Alias was already created/loaded. */
515 long_desc = pe->long_desc;
519 deprecated = pe->deprecated;
523 alias = zalloc(sizeof(*alias));
527 INIT_LIST_HEAD(&alias->terms);
529 alias->unit[0] = '\0';
530 alias->per_pkg = perpkg;
531 alias->snapshot = false;
532 alias->deprecated = deprecated;
534 ret = parse_events_terms(&alias->terms, val, val_fd);
536 pr_err("Cannot parse alias %s: %d\n", val, ret);
541 alias->name = strdup(name);
542 alias->desc = desc ? strdup(desc) : NULL;
543 alias->long_desc = long_desc ? strdup(long_desc) :
544 desc ? strdup(desc) : NULL;
545 alias->topic = topic ? strdup(topic) : NULL;
546 alias->pmu_name = pmu_name ? strdup(pmu_name) : NULL;
548 if (perf_pmu__convert_scale(unit, (char **)&unit, &alias->scale) < 0) {
549 perf_pmu_free_alias(alias);
552 snprintf(alias->unit, sizeof(alias->unit), "%s", unit);
555 /* Update an event from sysfs with json data. */
556 struct update_alias_data data = {
561 alias->from_sysfs = true;
562 if (pmu->events_table) {
563 if (pmu_events_table__find_event(pmu->events_table, pmu, name,
564 update_alias, &data) == 0)
565 pmu->loaded_json_aliases++;
570 pmu->sysfs_aliases++;
572 pmu->loaded_json_aliases++;
573 list_add_tail(&alias->list, &pmu->aliases);
577 static inline bool pmu_alias_info_file(char *name)
582 if (len > 5 && !strcmp(name + len - 5, ".unit"))
584 if (len > 6 && !strcmp(name + len - 6, ".scale"))
586 if (len > 8 && !strcmp(name + len - 8, ".per-pkg"))
588 if (len > 9 && !strcmp(name + len - 9, ".snapshot"))
595 * Reading the pmu event aliases definition, which should be located at:
596 * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
598 static int pmu_aliases_parse(struct perf_pmu *pmu)
601 struct dirent *evt_ent;
606 len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path));
609 scnprintf(path + len, sizeof(path) - len, "%s/events", pmu->name);
611 dir_fd = open(path, O_DIRECTORY);
613 pmu->sysfs_aliases_loaded = true;
617 event_dir = fdopendir(dir_fd);
623 while ((evt_ent = readdir(event_dir))) {
624 char *name = evt_ent->d_name;
627 if (!strcmp(name, ".") || !strcmp(name, ".."))
631 * skip info files parsed in perf_pmu__new_alias()
633 if (pmu_alias_info_file(name))
636 fd = openat(dir_fd, name, O_RDONLY);
638 pr_debug("Cannot open %s\n", name);
641 file = fdopen(fd, "r");
647 if (perf_pmu__new_alias(pmu, name, /*desc=*/ NULL,
648 /*val=*/ NULL, file, /*pe=*/ NULL) < 0)
649 pr_debug("Cannot set up %s\n", name);
655 pmu->sysfs_aliases_loaded = true;
659 static int pmu_alias_terms(struct perf_pmu_alias *alias,
660 struct list_head *terms)
662 struct parse_events_term *term, *cloned;
666 list_for_each_entry(term, &alias->terms, list) {
667 ret = parse_events_term__clone(&cloned, term);
669 parse_events_terms__purge(&list);
673 * Weak terms don't override command line options,
674 * which we don't want for implicit terms in aliases.
677 list_add_tail(&cloned->list, &list);
679 list_splice(&list, terms);
684 * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64)
685 * may have a "cpus" file.
687 static struct perf_cpu_map *pmu_cpumask(int dirfd, const char *name, bool is_core)
689 struct perf_cpu_map *cpus;
690 const char *templates[] = {
695 const char **template;
696 char pmu_name[PATH_MAX];
697 struct perf_pmu pmu = {.name = pmu_name};
700 strlcpy(pmu_name, name, sizeof(pmu_name));
701 for (template = templates; *template; template++) {
702 file = perf_pmu__open_file_at(&pmu, dirfd, *template);
705 cpus = perf_cpu_map__read(file);
711 /* Nothing found, for core PMUs assume this means all CPUs. */
712 return is_core ? perf_cpu_map__get(cpu_map__online()) : NULL;
715 static bool pmu_is_uncore(int dirfd, const char *name)
719 fd = perf_pmu__pathname_fd(dirfd, name, "cpumask", O_PATH);
727 static char *pmu_id(const char *name)
729 char path[PATH_MAX], *str;
732 perf_pmu__pathname_scnprintf(path, sizeof(path), name, "identifier");
734 if (filename__read_str(path, &str, &len) < 0)
737 str[len - 1] = 0; /* remove line feed */
743 * is_sysfs_pmu_core() - PMU CORE devices have different name other than cpu in
744 * sysfs on some platforms like ARM or Intel hybrid. Looking for
745 * possible the cpus file in sysfs files to identify whether this is a
747 * @name: The PMU name such as "cpu_atom".
749 static int is_sysfs_pmu_core(const char *name)
753 if (!perf_pmu__pathname_scnprintf(path, sizeof(path), name, "cpus"))
755 return file_available(path);
758 char *perf_pmu__getcpuid(struct perf_pmu *pmu)
763 cpuid = getenv("PERF_CPUID");
765 cpuid = strdup(cpuid);
767 cpuid = get_cpuid_str(pmu);
772 pr_debug("Using CPUID %s\n", cpuid);
778 __weak const struct pmu_events_table *pmu_events_table__find(void)
780 return perf_pmu__find_events_table(NULL);
783 __weak const struct pmu_metrics_table *pmu_metrics_table__find(void)
785 return perf_pmu__find_metrics_table(NULL);
789 * perf_pmu__match_ignoring_suffix - Does the pmu_name match tok ignoring any
790 * trailing suffix? The Suffix must be in form
791 * tok_{digits}, or tok{digits}.
792 * @pmu_name: The pmu_name with possible suffix.
793 * @tok: The possible match to pmu_name without suffix.
795 static bool perf_pmu__match_ignoring_suffix(const char *pmu_name, const char *tok)
799 if (strncmp(pmu_name, tok, strlen(tok)))
802 p = pmu_name + strlen(tok);
809 /* Ensure we end in a number */
821 * pmu_uncore_alias_match - does name match the PMU name?
822 * @pmu_name: the json struct pmu_event name. This may lack a suffix (which
823 * matches) or be of the form "socket,pmuname" which will match
824 * "socketX_pmunameY".
825 * @name: a real full PMU name as from sysfs.
827 static bool pmu_uncore_alias_match(const char *pmu_name, const char *name)
829 char *tmp = NULL, *tok, *str;
832 if (strchr(pmu_name, ',') == NULL)
833 return perf_pmu__match_ignoring_suffix(name, pmu_name);
835 str = strdup(pmu_name);
840 * uncore alias may be from different PMU with common prefix
842 tok = strtok_r(str, ",", &tmp);
843 if (strncmp(pmu_name, tok, strlen(tok))) {
849 * Match more complex aliases where the alias name is a comma-delimited
850 * list of tokens, orderly contained in the matching PMU name.
852 * Example: For alias "socket,pmuname" and PMU "socketX_pmunameY", we
853 * match "socket" in "socketX_pmunameY" and then "pmuname" in
857 char *next_tok = strtok_r(NULL, ",", &tmp);
859 name = strstr(name, tok);
861 (!next_tok && !perf_pmu__match_ignoring_suffix(name, tok))) {
877 static int pmu_add_cpu_aliases_map_callback(const struct pmu_event *pe,
878 const struct pmu_events_table *table __maybe_unused,
881 struct perf_pmu *pmu = vdata;
883 perf_pmu__new_alias(pmu, pe->name, pe->desc, pe->event, /*val_fd=*/ NULL, pe);
888 * From the pmu_events_table, find the events that correspond to the given
889 * PMU and add them to the list 'head'.
891 void pmu_add_cpu_aliases_table(struct perf_pmu *pmu, const struct pmu_events_table *table)
893 pmu_events_table__for_each_event(table, pmu, pmu_add_cpu_aliases_map_callback, pmu);
896 static void pmu_add_cpu_aliases(struct perf_pmu *pmu)
898 if (!pmu->events_table)
901 if (pmu->cpu_aliases_added)
904 pmu_add_cpu_aliases_table(pmu, pmu->events_table);
905 pmu->cpu_aliases_added = true;
908 static int pmu_add_sys_aliases_iter_fn(const struct pmu_event *pe,
909 const struct pmu_events_table *table __maybe_unused,
912 struct perf_pmu *pmu = vdata;
914 if (!pe->compat || !pe->pmu)
917 if (!strcmp(pmu->id, pe->compat) &&
918 pmu_uncore_alias_match(pe->pmu, pmu->name)) {
919 perf_pmu__new_alias(pmu,
930 void pmu_add_sys_aliases(struct perf_pmu *pmu)
935 pmu_for_each_sys_event(pmu_add_sys_aliases_iter_fn, pmu);
938 struct perf_event_attr * __weak
939 perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
945 pmu_find_real_name(const char *name)
951 pmu_find_alias_name(const char *name __maybe_unused)
956 static int pmu_max_precise(int dirfd, struct perf_pmu *pmu)
958 int max_precise = -1;
960 perf_pmu__scan_file_at(pmu, dirfd, "caps/max_precise", "%d", &max_precise);
964 struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char *lookup_name)
966 struct perf_pmu *pmu;
968 const char *name = pmu_find_real_name(lookup_name);
969 const char *alias_name;
971 pmu = zalloc(sizeof(*pmu));
975 pmu->name = strdup(name);
980 * Read type early to fail fast if a lookup name isn't a PMU. Ensure
981 * that type value is successfully assigned (return 1).
983 if (perf_pmu__scan_file_at(pmu, dirfd, "type", "%u", &type) != 1)
986 INIT_LIST_HEAD(&pmu->format);
987 INIT_LIST_HEAD(&pmu->aliases);
988 INIT_LIST_HEAD(&pmu->caps);
991 * The pmu data we store & need consists of the pmu
992 * type value and format definitions. Load both right
995 if (pmu_format(pmu, dirfd, name)) {
999 pmu->is_core = is_pmu_core(name);
1000 pmu->cpus = pmu_cpumask(dirfd, name, pmu->is_core);
1002 alias_name = pmu_find_alias_name(name);
1004 pmu->alias_name = strdup(alias_name);
1005 if (!pmu->alias_name)
1010 pmu->is_uncore = pmu_is_uncore(dirfd, name);
1012 pmu->id = pmu_id(name);
1013 pmu->max_precise = pmu_max_precise(dirfd, pmu);
1014 pmu->events_table = perf_pmu__find_events_table(pmu);
1015 pmu_add_sys_aliases(pmu);
1016 list_add_tail(&pmu->list, pmus);
1018 pmu->default_config = perf_pmu__get_default_config(pmu);
1027 /* Creates the PMU when sysfs scanning fails. */
1028 struct perf_pmu *perf_pmu__create_placeholder_core_pmu(struct list_head *core_pmus)
1030 struct perf_pmu *pmu = zalloc(sizeof(*pmu));
1035 pmu->name = strdup("cpu");
1041 pmu->is_core = true;
1042 pmu->type = PERF_TYPE_RAW;
1043 pmu->cpus = cpu_map__online();
1045 INIT_LIST_HEAD(&pmu->format);
1046 INIT_LIST_HEAD(&pmu->aliases);
1047 INIT_LIST_HEAD(&pmu->caps);
1048 list_add_tail(&pmu->list, core_pmus);
1052 void perf_pmu__warn_invalid_formats(struct perf_pmu *pmu)
1054 struct perf_pmu_format *format;
1056 if (pmu->formats_checked)
1059 pmu->formats_checked = true;
1061 /* fake pmu doesn't have format list */
1062 if (pmu == &perf_pmu__fake)
1065 list_for_each_entry(format, &pmu->format, list) {
1066 perf_pmu_format__load(pmu, format);
1067 if (format->value >= PERF_PMU_FORMAT_VALUE_CONFIG_END) {
1068 pr_warning("WARNING: '%s' format '%s' requires 'perf_event_attr::config%d'"
1069 "which is not supported by this version of perf!\n",
1070 pmu->name, format->name, format->value);
1076 bool evsel__is_aux_event(const struct evsel *evsel)
1078 struct perf_pmu *pmu = evsel__find_pmu(evsel);
1080 return pmu && pmu->auxtrace;
1084 * Set @config_name to @val as long as the user hasn't already set or cleared it
1085 * by passing a config term on the command line.
1087 * @val is the value to put into the bits specified by @config_name rather than
1088 * the bit pattern. It is shifted into position by this function, so to set
1089 * something to true, pass 1 for val rather than a pre shifted value.
1091 #define field_prep(_mask, _val) (((_val) << (ffsll(_mask) - 1)) & (_mask))
1092 void evsel__set_config_if_unset(struct perf_pmu *pmu, struct evsel *evsel,
1093 const char *config_name, u64 val)
1095 u64 user_bits = 0, bits;
1096 struct evsel_config_term *term = evsel__get_config_term(evsel, CFG_CHG);
1099 user_bits = term->val.cfg_chg;
1101 bits = perf_pmu__format_bits(pmu, config_name);
1103 /* Do nothing if the user changed the value */
1104 if (bits & user_bits)
1107 /* Otherwise replace it */
1108 evsel->core.attr.config &= ~bits;
1109 evsel->core.attr.config |= field_prep(bits, val);
1112 static struct perf_pmu_format *
1113 pmu_find_format(struct list_head *formats, const char *name)
1115 struct perf_pmu_format *format;
1117 list_for_each_entry(format, formats, list)
1118 if (!strcmp(format->name, name))
1124 __u64 perf_pmu__format_bits(struct perf_pmu *pmu, const char *name)
1126 struct perf_pmu_format *format = pmu_find_format(&pmu->format, name);
1133 for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
1134 bits |= 1ULL << fbit;
1139 int perf_pmu__format_type(struct perf_pmu *pmu, const char *name)
1141 struct perf_pmu_format *format = pmu_find_format(&pmu->format, name);
1146 perf_pmu_format__load(pmu, format);
1147 return format->value;
1151 * Sets value based on the format definition (format parameter)
1152 * and unformatted value (value parameter).
1154 static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
1157 unsigned long fbit, vbit;
1159 for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
1161 if (!test_bit(fbit, format))
1164 if (value & (1llu << vbit++))
1165 *v |= (1llu << fbit);
1167 *v &= ~(1llu << fbit);
1171 static __u64 pmu_format_max_value(const unsigned long *format)
1175 w = bitmap_weight(format, PERF_PMU_FORMAT_BITS);
1179 return (1ULL << w) - 1;
1184 * Term is a string term, and might be a param-term. Try to look up it's value
1185 * in the remaining terms.
1186 * - We have a term like "base-or-format-term=param-term",
1187 * - We need to find the value supplied for "param-term" (with param-term named
1188 * in a config string) later on in the term list.
1190 static int pmu_resolve_param_term(struct parse_events_term *term,
1191 struct list_head *head_terms,
1194 struct parse_events_term *t;
1196 list_for_each_entry(t, head_terms, list) {
1197 if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM &&
1198 t->config && !strcmp(t->config, term->config)) {
1200 *value = t->val.num;
1206 printf("Required parameter '%s' not specified\n", term->config);
1211 static char *pmu_formats_string(struct list_head *formats)
1213 struct perf_pmu_format *format;
1215 struct strbuf buf = STRBUF_INIT;
1221 /* sysfs exported terms */
1222 list_for_each_entry(format, formats, list)
1223 if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
1226 str = strbuf_detach(&buf, NULL);
1228 strbuf_release(&buf);
1234 * Setup one of config[12] attr members based on the
1235 * user input data - term parameter.
1237 static int pmu_config_term(struct perf_pmu *pmu,
1238 struct perf_event_attr *attr,
1239 struct parse_events_term *term,
1240 struct list_head *head_terms,
1241 bool zero, struct parse_events_error *err)
1243 struct perf_pmu_format *format;
1248 * If this is a parameter we've already used for parameterized-eval,
1249 * skip it in normal eval.
1255 * Hardcoded terms should be already in, so nothing
1256 * to be done for them.
1258 if (parse_events__is_hardcoded_term(term))
1261 format = pmu_find_format(&pmu->format, term->config);
1263 char *pmu_term = pmu_formats_string(&pmu->format);
1267 if (asprintf(&unknown_term,
1268 "unknown term '%s' for pmu '%s'",
1269 term->config, pmu->name) < 0)
1270 unknown_term = NULL;
1271 help_msg = parse_events_formats_error_string(pmu_term);
1273 parse_events_error__handle(err, term->err_term,
1277 pr_debug("%s (%s)\n", unknown_term, help_msg);
1283 perf_pmu_format__load(pmu, format);
1284 switch (format->value) {
1285 case PERF_PMU_FORMAT_VALUE_CONFIG:
1288 case PERF_PMU_FORMAT_VALUE_CONFIG1:
1289 vp = &attr->config1;
1291 case PERF_PMU_FORMAT_VALUE_CONFIG2:
1292 vp = &attr->config2;
1294 case PERF_PMU_FORMAT_VALUE_CONFIG3:
1295 vp = &attr->config3;
1302 * Either directly use a numeric term, or try to translate string terms
1303 * using event parameters.
1305 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1306 if (term->no_value &&
1307 bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) {
1309 parse_events_error__handle(err, term->err_val,
1310 strdup("no value assigned for term"),
1316 val = term->val.num;
1317 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1318 if (strcmp(term->val.str, "?")) {
1320 pr_info("Invalid sysfs entry %s=%s\n",
1321 term->config, term->val.str);
1324 parse_events_error__handle(err, term->err_val,
1325 strdup("expected numeric value"),
1331 if (pmu_resolve_param_term(term, head_terms, &val))
1336 max_val = pmu_format_max_value(format->bits);
1337 if (val > max_val) {
1341 parse_events_error__handle(err, term->err_val,
1343 "value too big for format, maximum is %llu",
1344 (unsigned long long)max_val) < 0
1345 ? strdup("value too big for format")
1351 * Assume we don't care if !err, in which case the value will be
1352 * silently truncated.
1356 pmu_format_value(format->bits, val, vp, zero);
1360 int perf_pmu__config_terms(struct perf_pmu *pmu,
1361 struct perf_event_attr *attr,
1362 struct list_head *head_terms,
1363 bool zero, struct parse_events_error *err)
1365 struct parse_events_term *term;
1367 list_for_each_entry(term, head_terms, list) {
1368 if (pmu_config_term(pmu, attr, term, head_terms, zero, err))
1376 * Configures event's 'attr' parameter based on the:
1377 * 1) users input - specified in terms parameter
1378 * 2) pmu format definitions - specified by pmu parameter
1380 int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
1381 struct list_head *head_terms,
1382 struct parse_events_error *err)
1384 bool zero = !!pmu->default_config;
1386 return perf_pmu__config_terms(pmu, attr, head_terms, zero, err);
1389 static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
1390 struct parse_events_term *term)
1392 struct perf_pmu_alias *alias;
1395 if (parse_events__is_hardcoded_term(term))
1398 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1399 if (!term->no_value)
1401 if (pmu_find_format(&pmu->format, term->config))
1403 name = term->config;
1405 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1406 if (strcasecmp(term->config, "event"))
1408 name = term->val.str;
1413 alias = perf_pmu__find_alias(pmu, name, /*load=*/ true);
1414 if (alias || pmu->cpu_aliases_added)
1417 /* Alias doesn't exist, try to get it from the json events. */
1418 if (pmu->events_table &&
1419 pmu_events_table__find_event(pmu->events_table, pmu, name,
1420 pmu_add_cpu_aliases_map_callback,
1422 alias = perf_pmu__find_alias(pmu, name, /*load=*/ false);
1428 static int check_info_data(struct perf_pmu *pmu,
1429 struct perf_pmu_alias *alias,
1430 struct perf_pmu_info *info,
1431 struct parse_events_error *err,
1434 read_alias_info(pmu, alias);
1436 * Only one term in event definition can
1437 * define unit, scale and snapshot, fail
1438 * if there's more than one.
1440 if (info->unit && alias->unit[0]) {
1441 parse_events_error__handle(err, column,
1442 strdup("Attempt to set event's unit twice"),
1446 if (info->scale && alias->scale) {
1447 parse_events_error__handle(err, column,
1448 strdup("Attempt to set event's scale twice"),
1452 if (info->snapshot && alias->snapshot) {
1453 parse_events_error__handle(err, column,
1454 strdup("Attempt to set event snapshot twice"),
1460 info->unit = alias->unit;
1463 info->scale = alias->scale;
1465 if (alias->snapshot)
1466 info->snapshot = alias->snapshot;
1472 * Find alias in the terms list and replace it with the terms
1473 * defined for the alias
1475 int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
1476 struct perf_pmu_info *info, struct parse_events_error *err)
1478 struct parse_events_term *term, *h;
1479 struct perf_pmu_alias *alias;
1482 info->per_pkg = false;
1485 * Mark unit and scale as not set
1486 * (different from default values, see below)
1490 info->snapshot = false;
1492 list_for_each_entry_safe(term, h, head_terms, list) {
1493 alias = pmu_find_alias(pmu, term);
1496 ret = pmu_alias_terms(alias, &term->list);
1498 parse_events_error__handle(err, term->err_term,
1499 strdup("Failure to duplicate terms"),
1504 ret = check_info_data(pmu, alias, info, err, term->err_term);
1509 info->per_pkg = true;
1511 list_del_init(&term->list);
1512 parse_events_term__delete(term);
1516 * if no unit or scale found in aliases, then
1517 * set defaults as for evsel
1518 * unit cannot left to NULL
1520 if (info->unit == NULL)
1523 if (info->scale == 0.0)
1529 struct find_event_args {
1532 pmu_event_callback cb;
1535 static int find_event_callback(void *state, struct pmu_event_info *info)
1537 struct find_event_args *args = state;
1539 if (!strcmp(args->event, info->name))
1540 return args->cb(args->state, info);
1545 int perf_pmu__find_event(struct perf_pmu *pmu, const char *event, void *state, pmu_event_callback cb)
1547 struct find_event_args args = {
1553 /* Sub-optimal, but function is only used by tests. */
1554 return perf_pmu__for_each_event(pmu, /*skip_duplicate_pmus=*/ false,
1555 &args, find_event_callback);
1558 static void perf_pmu__del_formats(struct list_head *formats)
1560 struct perf_pmu_format *fmt, *tmp;
1562 list_for_each_entry_safe(fmt, tmp, formats, list) {
1563 list_del(&fmt->list);
1569 bool perf_pmu__has_format(const struct perf_pmu *pmu, const char *name)
1571 struct perf_pmu_format *format;
1573 list_for_each_entry(format, &pmu->format, list) {
1574 if (!strcmp(format->name, name))
1580 bool is_pmu_core(const char *name)
1582 return !strcmp(name, "cpu") || !strcmp(name, "cpum_cf") || is_sysfs_pmu_core(name);
1585 bool perf_pmu__supports_legacy_cache(const struct perf_pmu *pmu)
1587 return pmu->is_core;
1590 bool perf_pmu__auto_merge_stats(const struct perf_pmu *pmu)
1592 return !pmu->is_core || perf_pmus__num_core_pmus() == 1;
1595 bool perf_pmu__have_event(struct perf_pmu *pmu, const char *name)
1597 if (perf_pmu__find_alias(pmu, name, /*load=*/ true) != NULL)
1599 if (pmu->cpu_aliases_added || !pmu->events_table)
1601 return pmu_events_table__find_event(pmu->events_table, pmu, name, NULL, NULL) == 0;
1604 size_t perf_pmu__num_events(struct perf_pmu *pmu)
1608 if (!pmu->sysfs_aliases_loaded)
1609 pmu_aliases_parse(pmu);
1611 nr = pmu->sysfs_aliases;
1613 if (pmu->cpu_aliases_added)
1614 nr += pmu->loaded_json_aliases;
1615 else if (pmu->events_table)
1616 nr += pmu_events_table__num_events(pmu->events_table, pmu) - pmu->loaded_json_aliases;
1618 return pmu->selectable ? nr + 1 : nr;
1621 static int sub_non_neg(int a, int b)
1628 static char *format_alias(char *buf, int len, const struct perf_pmu *pmu,
1629 const struct perf_pmu_alias *alias, bool skip_duplicate_pmus)
1631 struct parse_events_term *term;
1632 int pmu_name_len = skip_duplicate_pmus
1633 ? pmu_name_len_no_suffix(pmu->name, /*num=*/NULL)
1634 : (int)strlen(pmu->name);
1635 int used = snprintf(buf, len, "%.*s/%s", pmu_name_len, pmu->name, alias->name);
1637 list_for_each_entry(term, &alias->terms, list) {
1638 if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
1639 used += snprintf(buf + used, sub_non_neg(len, used),
1640 ",%s=%s", term->config,
1644 if (sub_non_neg(len, used) > 0) {
1648 if (sub_non_neg(len, used) > 0) {
1652 buf[len - 1] = '\0';
1657 int perf_pmu__for_each_event(struct perf_pmu *pmu, bool skip_duplicate_pmus,
1658 void *state, pmu_event_callback cb)
1661 struct perf_pmu_alias *event;
1662 struct pmu_event_info info = {
1668 strbuf_init(&sb, /*hint=*/ 0);
1669 pmu_add_cpu_aliases(pmu);
1670 list_for_each_entry(event, &pmu->aliases, list) {
1673 info.pmu_name = event->pmu_name ?: pmu->name;
1676 info.name = event->name;
1679 info.name = format_alias(buf, sizeof(buf), pmu, event,
1680 skip_duplicate_pmus);
1682 info.alias = info.name;
1683 info.name = event->name;
1685 buf_used = strlen(buf) + 1;
1687 info.scale_unit = NULL;
1688 if (strlen(event->unit) || event->scale != 1.0) {
1689 info.scale_unit = buf + buf_used;
1690 buf_used += snprintf(buf + buf_used, sizeof(buf) - buf_used,
1691 "%G%s", event->scale, event->unit) + 1;
1693 info.desc = event->desc;
1694 info.long_desc = event->long_desc;
1695 info.encoding_desc = buf + buf_used;
1696 parse_events_term__to_strbuf(&event->terms, &sb);
1697 buf_used += snprintf(buf + buf_used, sizeof(buf) - buf_used,
1698 "%s/%s/", info.pmu_name, sb.buf) + 1;
1699 info.topic = event->topic;
1701 info.deprecated = event->deprecated;
1702 ret = cb(state, &info);
1705 strbuf_setlen(&sb, /*len=*/ 0);
1707 if (pmu->selectable) {
1709 snprintf(buf, sizeof(buf), "%s//", pmu->name);
1711 info.scale_unit = NULL;
1713 info.long_desc = NULL;
1714 info.encoding_desc = NULL;
1716 info.pmu_name = pmu->name;
1717 info.deprecated = false;
1718 ret = cb(state, &info);
1721 strbuf_release(&sb);
1725 bool pmu__name_match(const struct perf_pmu *pmu, const char *pmu_name)
1727 return !strcmp(pmu->name, pmu_name) ||
1728 (pmu->is_uncore && pmu_uncore_alias_match(pmu_name, pmu->name)) ||
1730 * jevents and tests use default_core as a marker for any core
1731 * PMU as the PMU name varies across architectures.
1733 (pmu->is_core && !strcmp(pmu_name, "default_core"));
1736 bool perf_pmu__is_software(const struct perf_pmu *pmu)
1738 if (pmu->is_core || pmu->is_uncore || pmu->auxtrace)
1740 switch (pmu->type) {
1741 case PERF_TYPE_HARDWARE: return false;
1742 case PERF_TYPE_SOFTWARE: return true;
1743 case PERF_TYPE_TRACEPOINT: return true;
1744 case PERF_TYPE_HW_CACHE: return false;
1745 case PERF_TYPE_RAW: return false;
1746 case PERF_TYPE_BREAKPOINT: return true;
1749 return !strcmp(pmu->name, "kprobe") || !strcmp(pmu->name, "uprobe");
1752 FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
1754 char path[PATH_MAX];
1756 if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, name) ||
1757 !file_available(path))
1760 return fopen(path, "r");
1763 FILE *perf_pmu__open_file_at(struct perf_pmu *pmu, int dirfd, const char *name)
1767 fd = perf_pmu__pathname_fd(dirfd, pmu->name, name, O_RDONLY);
1771 return fdopen(fd, "r");
1774 int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
1781 va_start(args, fmt);
1782 file = perf_pmu__open_file(pmu, name);
1784 ret = vfscanf(file, fmt, args);
1791 int perf_pmu__scan_file_at(struct perf_pmu *pmu, int dirfd, const char *name,
1792 const char *fmt, ...)
1798 va_start(args, fmt);
1799 file = perf_pmu__open_file_at(pmu, dirfd, name);
1801 ret = vfscanf(file, fmt, args);
1808 bool perf_pmu__file_exists(struct perf_pmu *pmu, const char *name)
1810 char path[PATH_MAX];
1812 if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, name))
1815 return file_available(path);
1818 static int perf_pmu__new_caps(struct list_head *list, char *name, char *value)
1820 struct perf_pmu_caps *caps = zalloc(sizeof(*caps));
1825 caps->name = strdup(name);
1828 caps->value = strndup(value, strlen(value) - 1);
1831 list_add_tail(&caps->list, list);
1842 static void perf_pmu__del_caps(struct perf_pmu *pmu)
1844 struct perf_pmu_caps *caps, *tmp;
1846 list_for_each_entry_safe(caps, tmp, &pmu->caps, list) {
1847 list_del(&caps->list);
1849 zfree(&caps->value);
1855 * Reading/parsing the given pmu capabilities, which should be located at:
1856 * /sys/bus/event_source/devices/<dev>/caps as sysfs group attributes.
1857 * Return the number of capabilities
1859 int perf_pmu__caps_parse(struct perf_pmu *pmu)
1862 char caps_path[PATH_MAX];
1864 struct dirent *evt_ent;
1867 if (pmu->caps_initialized)
1868 return pmu->nr_caps;
1872 if (!perf_pmu__pathname_scnprintf(caps_path, sizeof(caps_path), pmu->name, "caps"))
1875 if (stat(caps_path, &st) < 0) {
1876 pmu->caps_initialized = true;
1877 return 0; /* no error if caps does not exist */
1880 caps_dir = opendir(caps_path);
1884 caps_fd = dirfd(caps_dir);
1886 while ((evt_ent = readdir(caps_dir)) != NULL) {
1887 char *name = evt_ent->d_name;
1892 if (!strcmp(name, ".") || !strcmp(name, ".."))
1895 fd = openat(caps_fd, name, O_RDONLY);
1898 file = fdopen(fd, "r");
1904 if (!fgets(value, sizeof(value), file) ||
1905 (perf_pmu__new_caps(&pmu->caps, name, value) < 0)) {
1916 pmu->caps_initialized = true;
1917 return pmu->nr_caps;
1920 static void perf_pmu__compute_config_masks(struct perf_pmu *pmu)
1922 struct perf_pmu_format *format;
1924 if (pmu->config_masks_computed)
1927 list_for_each_entry(format, &pmu->format, list) {
1931 if (format->value >= PERF_PMU_FORMAT_VALUE_CONFIG_END)
1934 pmu->config_masks_present = true;
1935 mask = &pmu->config_masks[format->value];
1937 for_each_set_bit(i, format->bits, PERF_PMU_FORMAT_BITS)
1940 pmu->config_masks_computed = true;
1943 void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config,
1944 const char *name, int config_num,
1945 const char *config_name)
1950 perf_pmu__compute_config_masks(pmu);
1953 * Kernel doesn't export any valid format bits.
1955 if (!pmu->config_masks_present)
1958 bits = config & ~pmu->config_masks[config_num];
1962 bitmap_scnprintf((unsigned long *)&bits, sizeof(bits) * 8, buf, sizeof(buf));
1964 pr_warning("WARNING: event '%s' not valid (bits %s of %s "
1965 "'%llx' not supported by kernel)!\n",
1966 name ?: "N/A", buf, config_name, config);
1969 int perf_pmu__match(const char *pattern, const char *name, const char *tok)
1974 if (fnmatch(pattern, name, 0))
1977 if (tok && !perf_pmu__match_ignoring_suffix(name, tok))
1983 double __weak perf_pmu__cpu_slots_per_cycle(void)
1988 int perf_pmu__event_source_devices_scnprintf(char *pathname, size_t size)
1990 const char *sysfs = sysfs__mountpoint();
1994 return scnprintf(pathname, size, "%s/bus/event_source/devices/", sysfs);
1997 int perf_pmu__event_source_devices_fd(void)
1999 char path[PATH_MAX];
2000 const char *sysfs = sysfs__mountpoint();
2005 scnprintf(path, sizeof(path), "%s/bus/event_source/devices/", sysfs);
2006 return open(path, O_DIRECTORY);
2010 * Fill 'buf' with the path to a file or folder in 'pmu_name' in
2011 * sysfs. For example if pmu_name = "cs_etm" and 'filename' = "format"
2012 * then pathname will be filled with
2013 * "/sys/bus/event_source/devices/cs_etm/format"
2015 * Return 0 if the sysfs mountpoint couldn't be found, if no characters were
2016 * written or if the buffer size is exceeded.
2018 int perf_pmu__pathname_scnprintf(char *buf, size_t size,
2019 const char *pmu_name, const char *filename)
2023 len = perf_pmu__event_source_devices_scnprintf(buf, size);
2024 if (!len || (len + strlen(pmu_name) + strlen(filename) + 1) >= size)
2027 return scnprintf(buf + len, size - len, "%s/%s", pmu_name, filename);
2030 int perf_pmu__pathname_fd(int dirfd, const char *pmu_name, const char *filename, int flags)
2032 char path[PATH_MAX];
2034 scnprintf(path, sizeof(path), "%s/%s", pmu_name, filename);
2035 return openat(dirfd, path, flags);
2038 void perf_pmu__delete(struct perf_pmu *pmu)
2040 perf_pmu__del_formats(&pmu->format);
2041 perf_pmu__del_aliases(pmu);
2042 perf_pmu__del_caps(pmu);
2044 perf_cpu_map__put(pmu->cpus);
2046 zfree(&pmu->default_config);
2048 zfree(&pmu->alias_name);
2053 struct perf_pmu *pmu__find_core_pmu(void)
2055 struct perf_pmu *pmu = NULL;
2057 while ((pmu = perf_pmus__scan_core(pmu))) {
2059 * The cpumap should cover all CPUs. Otherwise, some CPUs may
2060 * not support some events or have different event IDs.
2062 if (RC_CHK_ACCESS(pmu->cpus)->nr != cpu__max_cpu().cpu)