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 * struct perf_pmu_alias - An event either read from sysfs or builtin in
41 * pmu-events.c, created by parsing the pmu-events json files.
43 struct perf_pmu_alias {
44 /** @name: Name of the event like "mem-loads". */
46 /** @desc: Optional short description of the event. */
48 /** @long_desc: Optional long description. */
51 * @topic: Optional topic such as cache or pipeline, particularly for
55 /** @terms: Owned list of the original parsed parameters. */
56 struct parse_events_terms terms;
57 /** @list: List element of struct perf_pmu aliases. */
58 struct list_head list;
60 * @pmu_name: The name copied from the json struct pmu_event. This can
61 * differ from the PMU name as it won't have suffixes.
64 /** @unit: Units for the event, such as bytes or cache lines. */
65 char unit[UNIT_MAX_LEN+1];
66 /** @scale: Value to scale read counter values by. */
69 * @per_pkg: Does the file
70 * <sysfs>/bus/event_source/devices/<pmu_name>/events/<name>.per-pkg or
71 * equivalent json value exist and have the value 1.
75 * @snapshot: Does the file
76 * <sysfs>/bus/event_source/devices/<pmu_name>/events/<name>.snapshot
77 * exist and have the value 1.
81 * @deprecated: Is the event hidden and so not shown in perf list by
85 /** @from_sysfs: Was the alias from sysfs or a json event? */
87 /** @info_loaded: Have the scale, unit and other values been read from disk? */
92 * struct perf_pmu_format - Values from a format file read from
93 * <sysfs>/devices/cpu/format/ held in struct perf_pmu.
95 * For example, the contents of <sysfs>/devices/cpu/format/event may be
96 * "config:0-7" and will be represented here as name="event",
97 * value=PERF_PMU_FORMAT_VALUE_CONFIG and bits 0 to 7 will be set.
99 struct perf_pmu_format {
100 /** @list: Element on list within struct perf_pmu. */
101 struct list_head list;
102 /** @bits: Which config bits are set by this format value. */
103 DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
104 /** @name: The modifier/file name. */
107 * @value : Which config value the format relates to. Supported values
108 * are from PERF_PMU_FORMAT_VALUE_CONFIG to
109 * PERF_PMU_FORMAT_VALUE_CONFIG_END.
112 /** @loaded: Has the contents been loaded/parsed. */
116 static int pmu_aliases_parse(struct perf_pmu *pmu);
118 static struct perf_pmu_format *perf_pmu__new_format(struct list_head *list, char *name)
120 struct perf_pmu_format *format;
122 format = zalloc(sizeof(*format));
126 format->name = strdup(name);
131 list_add_tail(&format->list, list);
135 /* Called at the end of parsing a format. */
136 void perf_pmu_format__set_value(void *vformat, int config, unsigned long *bits)
138 struct perf_pmu_format *format = vformat;
140 format->value = config;
141 memcpy(format->bits, bits, sizeof(format->bits));
144 static void __perf_pmu_format__load(struct perf_pmu_format *format, FILE *file)
149 ret = perf_pmu_lex_init(&scanner);
153 perf_pmu_set_in(file, scanner);
154 ret = perf_pmu_parse(format, scanner);
155 perf_pmu_lex_destroy(scanner);
156 format->loaded = true;
159 static void perf_pmu_format__load(const struct perf_pmu *pmu, struct perf_pmu_format *format)
167 if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, "format"))
170 assert(strlen(path) + strlen(format->name) + 2 < sizeof(path));
172 strcat(path, format->name);
174 file = fopen(path, "r");
177 __perf_pmu_format__load(format, file);
182 * Parse & process all the sysfs attributes located under
183 * the directory specified in 'dir' parameter.
185 int perf_pmu__format_parse(struct perf_pmu *pmu, int dirfd, bool eager_load)
187 struct dirent *evt_ent;
191 format_dir = fdopendir(dirfd);
195 while ((evt_ent = readdir(format_dir)) != NULL) {
196 struct perf_pmu_format *format;
197 char *name = evt_ent->d_name;
199 if (!strcmp(name, ".") || !strcmp(name, ".."))
202 format = perf_pmu__new_format(&pmu->format, name);
210 int fd = openat(dirfd, name, O_RDONLY);
216 file = fdopen(fd, "r");
221 __perf_pmu_format__load(format, file);
226 closedir(format_dir);
231 * Reading/parsing the default pmu format definition, which should be
233 * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
235 static int pmu_format(struct perf_pmu *pmu, int dirfd, const char *name)
239 fd = perf_pmu__pathname_fd(dirfd, name, "format", O_DIRECTORY);
243 /* it'll close the fd */
244 if (perf_pmu__format_parse(pmu, fd, /*eager_load=*/false))
250 int perf_pmu__convert_scale(const char *scale, char **end, double *sval)
256 * save current locale
258 lc = setlocale(LC_NUMERIC, NULL);
261 * The lc string may be allocated in static storage,
262 * so get a dynamic copy to make it survive setlocale
272 * force to C locale to ensure kernel
273 * scale string is converted correctly.
274 * kernel uses default C locale.
276 setlocale(LC_NUMERIC, "C");
278 *sval = strtod(scale, end);
282 setlocale(LC_NUMERIC, lc);
287 static int perf_pmu__parse_scale(struct perf_pmu *pmu, struct perf_pmu_alias *alias)
296 len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path));
299 scnprintf(path + len, sizeof(path) - len, "%s/events/%s.scale", pmu->name, alias->name);
301 fd = open(path, O_RDONLY);
305 if (fstat(fd, &st) < 0)
308 sret = read(fd, scale, sizeof(scale)-1);
312 if (scale[sret - 1] == '\n')
313 scale[sret - 1] = '\0';
317 ret = perf_pmu__convert_scale(scale, NULL, &alias->scale);
323 static int perf_pmu__parse_unit(struct perf_pmu *pmu, struct perf_pmu_alias *alias)
331 len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path));
334 scnprintf(path + len, sizeof(path) - len, "%s/events/%s.unit", pmu->name, alias->name);
336 fd = open(path, O_RDONLY);
340 sret = read(fd, alias->unit, UNIT_MAX_LEN);
346 if (alias->unit[sret - 1] == '\n')
347 alias->unit[sret - 1] = '\0';
349 alias->unit[sret] = '\0';
354 alias->unit[0] = '\0';
359 perf_pmu__parse_per_pkg(struct perf_pmu *pmu, struct perf_pmu_alias *alias)
365 len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path));
368 scnprintf(path + len, sizeof(path) - len, "%s/events/%s.per-pkg", pmu->name, alias->name);
370 fd = open(path, O_RDONLY);
376 alias->per_pkg = true;
380 static int perf_pmu__parse_snapshot(struct perf_pmu *pmu, struct perf_pmu_alias *alias)
386 len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path));
389 scnprintf(path + len, sizeof(path) - len, "%s/events/%s.snapshot", pmu->name, alias->name);
391 fd = open(path, O_RDONLY);
395 alias->snapshot = true;
400 /* Delete an alias entry. */
401 static void perf_pmu_free_alias(struct perf_pmu_alias *newalias)
403 zfree(&newalias->name);
404 zfree(&newalias->desc);
405 zfree(&newalias->long_desc);
406 zfree(&newalias->topic);
407 zfree(&newalias->pmu_name);
408 parse_events_terms__exit(&newalias->terms);
412 static void perf_pmu__del_aliases(struct perf_pmu *pmu)
414 struct perf_pmu_alias *alias, *tmp;
416 list_for_each_entry_safe(alias, tmp, &pmu->aliases, list) {
417 list_del(&alias->list);
418 perf_pmu_free_alias(alias);
422 static struct perf_pmu_alias *perf_pmu__find_alias(struct perf_pmu *pmu,
426 struct perf_pmu_alias *alias;
428 if (load && !pmu->sysfs_aliases_loaded)
429 pmu_aliases_parse(pmu);
431 list_for_each_entry(alias, &pmu->aliases, list) {
432 if (!strcasecmp(alias->name, name))
438 static bool assign_str(const char *name, const char *field, char **old_str,
441 if (!*old_str && new_str) {
442 *old_str = strdup(new_str);
446 if (!new_str || !strcasecmp(*old_str, new_str))
447 return false; /* Nothing to update. */
449 pr_debug("alias %s differs in field '%s' ('%s' != '%s')\n",
450 name, field, *old_str, new_str);
452 *old_str = strdup(new_str);
456 static void read_alias_info(struct perf_pmu *pmu, struct perf_pmu_alias *alias)
458 if (!alias->from_sysfs || alias->info_loaded)
462 * load unit name and scale if available
464 perf_pmu__parse_unit(pmu, alias);
465 perf_pmu__parse_scale(pmu, alias);
466 perf_pmu__parse_per_pkg(pmu, alias);
467 perf_pmu__parse_snapshot(pmu, alias);
470 struct update_alias_data {
471 struct perf_pmu *pmu;
472 struct perf_pmu_alias *alias;
475 static int update_alias(const struct pmu_event *pe,
476 const struct pmu_events_table *table __maybe_unused,
479 struct update_alias_data *data = vdata;
482 read_alias_info(data->pmu, data->alias);
483 assign_str(pe->name, "desc", &data->alias->desc, pe->desc);
484 assign_str(pe->name, "long_desc", &data->alias->long_desc, pe->long_desc);
485 assign_str(pe->name, "topic", &data->alias->topic, pe->topic);
486 data->alias->per_pkg = pe->perpkg;
488 parse_events_terms__exit(&data->alias->terms);
489 ret = parse_events_terms(&data->alias->terms, pe->event, /*input=*/NULL);
491 if (!ret && pe->unit) {
494 ret = perf_pmu__convert_scale(pe->unit, &unit, &data->alias->scale);
496 snprintf(data->alias->unit, sizeof(data->alias->unit), "%s", unit);
501 static int perf_pmu__new_alias(struct perf_pmu *pmu, const char *name,
502 const char *desc, const char *val, FILE *val_fd,
503 const struct pmu_event *pe)
505 struct perf_pmu_alias *alias;
507 const char *long_desc = NULL, *topic = NULL, *unit = NULL, *pmu_name = NULL;
508 bool deprecated = false, perpkg = false;
510 if (perf_pmu__find_alias(pmu, name, /*load=*/ false)) {
511 /* Alias was already created/loaded. */
516 long_desc = pe->long_desc;
520 deprecated = pe->deprecated;
524 alias = zalloc(sizeof(*alias));
528 parse_events_terms__init(&alias->terms);
530 alias->unit[0] = '\0';
531 alias->per_pkg = perpkg;
532 alias->snapshot = false;
533 alias->deprecated = deprecated;
535 ret = parse_events_terms(&alias->terms, val, val_fd);
537 pr_err("Cannot parse alias %s: %d\n", val, ret);
542 alias->name = strdup(name);
543 alias->desc = desc ? strdup(desc) : NULL;
544 alias->long_desc = long_desc ? strdup(long_desc) :
545 desc ? strdup(desc) : NULL;
546 alias->topic = topic ? strdup(topic) : NULL;
547 alias->pmu_name = pmu_name ? strdup(pmu_name) : NULL;
549 if (perf_pmu__convert_scale(unit, (char **)&unit, &alias->scale) < 0) {
550 perf_pmu_free_alias(alias);
553 snprintf(alias->unit, sizeof(alias->unit), "%s", unit);
556 /* Update an event from sysfs with json data. */
557 struct update_alias_data data = {
562 alias->from_sysfs = true;
563 if (pmu->events_table) {
564 if (pmu_events_table__find_event(pmu->events_table, pmu, name,
565 update_alias, &data) == 0)
566 pmu->loaded_json_aliases++;
571 pmu->sysfs_aliases++;
573 pmu->loaded_json_aliases++;
574 list_add_tail(&alias->list, &pmu->aliases);
578 static inline bool pmu_alias_info_file(const char *name)
583 if (len > 5 && !strcmp(name + len - 5, ".unit"))
585 if (len > 6 && !strcmp(name + len - 6, ".scale"))
587 if (len > 8 && !strcmp(name + len - 8, ".per-pkg"))
589 if (len > 9 && !strcmp(name + len - 9, ".snapshot"))
596 * Reading the pmu event aliases definition, which should be located at:
597 * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
599 static int pmu_aliases_parse(struct perf_pmu *pmu)
602 struct dirent *evt_ent;
607 len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path));
610 scnprintf(path + len, sizeof(path) - len, "%s/events", pmu->name);
612 dir_fd = open(path, O_DIRECTORY);
614 pmu->sysfs_aliases_loaded = true;
618 event_dir = fdopendir(dir_fd);
624 while ((evt_ent = readdir(event_dir))) {
625 char *name = evt_ent->d_name;
628 if (!strcmp(name, ".") || !strcmp(name, ".."))
632 * skip info files parsed in perf_pmu__new_alias()
634 if (pmu_alias_info_file(name))
637 fd = openat(dir_fd, name, O_RDONLY);
639 pr_debug("Cannot open %s\n", name);
642 file = fdopen(fd, "r");
648 if (perf_pmu__new_alias(pmu, name, /*desc=*/ NULL,
649 /*val=*/ NULL, file, /*pe=*/ NULL) < 0)
650 pr_debug("Cannot set up %s\n", name);
656 pmu->sysfs_aliases_loaded = true;
660 static int pmu_alias_terms(struct perf_pmu_alias *alias, int err_loc, struct list_head *terms)
662 struct parse_events_term *term, *cloned;
663 struct parse_events_terms clone_terms;
665 parse_events_terms__init(&clone_terms);
666 list_for_each_entry(term, &alias->terms.terms, list) {
667 int ret = parse_events_term__clone(&cloned, term);
670 parse_events_terms__exit(&clone_terms);
674 * Weak terms don't override command line options,
675 * which we don't want for implicit terms in aliases.
678 cloned->err_term = cloned->err_val = err_loc;
679 list_add_tail(&cloned->list, &clone_terms.terms);
681 list_splice_init(&clone_terms.terms, terms);
682 parse_events_terms__exit(&clone_terms);
687 * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64)
688 * may have a "cpus" file.
690 static struct perf_cpu_map *pmu_cpumask(int dirfd, const char *name, bool is_core)
692 struct perf_cpu_map *cpus;
693 const char *templates[] = {
698 const char **template;
699 char pmu_name[PATH_MAX];
700 struct perf_pmu pmu = {.name = pmu_name};
703 strlcpy(pmu_name, name, sizeof(pmu_name));
704 for (template = templates; *template; template++) {
705 file = perf_pmu__open_file_at(&pmu, dirfd, *template);
708 cpus = perf_cpu_map__read(file);
714 /* Nothing found, for core PMUs assume this means all CPUs. */
715 return is_core ? perf_cpu_map__get(cpu_map__online()) : NULL;
718 static bool pmu_is_uncore(int dirfd, const char *name)
722 fd = perf_pmu__pathname_fd(dirfd, name, "cpumask", O_PATH);
730 static char *pmu_id(const char *name)
732 char path[PATH_MAX], *str;
735 perf_pmu__pathname_scnprintf(path, sizeof(path), name, "identifier");
737 if (filename__read_str(path, &str, &len) < 0)
740 str[len - 1] = 0; /* remove line feed */
746 * is_sysfs_pmu_core() - PMU CORE devices have different name other than cpu in
747 * sysfs on some platforms like ARM or Intel hybrid. Looking for
748 * possible the cpus file in sysfs files to identify whether this is a
750 * @name: The PMU name such as "cpu_atom".
752 static int is_sysfs_pmu_core(const char *name)
756 if (!perf_pmu__pathname_scnprintf(path, sizeof(path), name, "cpus"))
758 return file_available(path);
761 char *perf_pmu__getcpuid(struct perf_pmu *pmu)
766 cpuid = getenv("PERF_CPUID");
768 cpuid = strdup(cpuid);
770 cpuid = get_cpuid_str(pmu);
775 pr_debug("Using CPUID %s\n", cpuid);
781 __weak const struct pmu_metrics_table *pmu_metrics_table__find(void)
783 return perf_pmu__find_metrics_table(NULL);
787 * perf_pmu__match_ignoring_suffix - Does the pmu_name match tok ignoring any
788 * trailing suffix? The Suffix must be in form
789 * tok_{digits}, or tok{digits}.
790 * @pmu_name: The pmu_name with possible suffix.
791 * @tok: The possible match to pmu_name without suffix.
793 static bool perf_pmu__match_ignoring_suffix(const char *pmu_name, const char *tok)
797 if (strncmp(pmu_name, tok, strlen(tok)))
800 p = pmu_name + strlen(tok);
807 /* Ensure we end in a number */
819 * pmu_uncore_alias_match - does name match the PMU name?
820 * @pmu_name: the json struct pmu_event name. This may lack a suffix (which
821 * matches) or be of the form "socket,pmuname" which will match
822 * "socketX_pmunameY".
823 * @name: a real full PMU name as from sysfs.
825 static bool pmu_uncore_alias_match(const char *pmu_name, const char *name)
827 char *tmp = NULL, *tok, *str;
830 if (strchr(pmu_name, ',') == NULL)
831 return perf_pmu__match_ignoring_suffix(name, pmu_name);
833 str = strdup(pmu_name);
838 * uncore alias may be from different PMU with common prefix
840 tok = strtok_r(str, ",", &tmp);
841 if (strncmp(pmu_name, tok, strlen(tok))) {
847 * Match more complex aliases where the alias name is a comma-delimited
848 * list of tokens, orderly contained in the matching PMU name.
850 * Example: For alias "socket,pmuname" and PMU "socketX_pmunameY", we
851 * match "socket" in "socketX_pmunameY" and then "pmuname" in
855 char *next_tok = strtok_r(NULL, ",", &tmp);
857 name = strstr(name, tok);
859 (!next_tok && !perf_pmu__match_ignoring_suffix(name, tok))) {
875 bool pmu_uncore_identifier_match(const char *compat, const char *id)
878 regmatch_t pmatch[1];
881 if (regcomp(&re, compat, REG_EXTENDED) != 0) {
882 /* Warn unable to generate match particular string. */
883 pr_info("Invalid regular expression %s\n", compat);
887 match = !regexec(&re, id, 1, pmatch, 0);
889 /* Ensure a full match. */
890 match = pmatch[0].rm_so == 0 && (size_t)pmatch[0].rm_eo == strlen(id);
897 static int pmu_add_cpu_aliases_map_callback(const struct pmu_event *pe,
898 const struct pmu_events_table *table __maybe_unused,
901 struct perf_pmu *pmu = vdata;
903 perf_pmu__new_alias(pmu, pe->name, pe->desc, pe->event, /*val_fd=*/ NULL, pe);
908 * From the pmu_events_table, find the events that correspond to the given
909 * PMU and add them to the list 'head'.
911 void pmu_add_cpu_aliases_table(struct perf_pmu *pmu, const struct pmu_events_table *table)
913 pmu_events_table__for_each_event(table, pmu, pmu_add_cpu_aliases_map_callback, pmu);
916 static void pmu_add_cpu_aliases(struct perf_pmu *pmu)
918 if (!pmu->events_table)
921 if (pmu->cpu_aliases_added)
924 pmu_add_cpu_aliases_table(pmu, pmu->events_table);
925 pmu->cpu_aliases_added = true;
928 static int pmu_add_sys_aliases_iter_fn(const struct pmu_event *pe,
929 const struct pmu_events_table *table __maybe_unused,
932 struct perf_pmu *pmu = vdata;
934 if (!pe->compat || !pe->pmu)
937 if (pmu_uncore_alias_match(pe->pmu, pmu->name) &&
938 pmu_uncore_identifier_match(pe->compat, pmu->id)) {
939 perf_pmu__new_alias(pmu,
950 void pmu_add_sys_aliases(struct perf_pmu *pmu)
955 pmu_for_each_sys_event(pmu_add_sys_aliases_iter_fn, pmu);
958 static char *pmu_find_alias_name(struct perf_pmu *pmu, int dirfd)
960 FILE *file = perf_pmu__open_file_at(pmu, dirfd, "alias");
968 ret = getline(&line, &line_len, file);
973 /* Remove trailing newline. */
974 if (ret > 0 && line[ret - 1] == '\n')
981 static int pmu_max_precise(int dirfd, struct perf_pmu *pmu)
983 int max_precise = -1;
985 perf_pmu__scan_file_at(pmu, dirfd, "caps/max_precise", "%d", &max_precise);
990 perf_pmu__arch_init(struct perf_pmu *pmu)
993 pmu->mem_events = perf_mem_events;
996 struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char *name)
998 struct perf_pmu *pmu;
1001 pmu = zalloc(sizeof(*pmu));
1005 pmu->name = strdup(name);
1010 * Read type early to fail fast if a lookup name isn't a PMU. Ensure
1011 * that type value is successfully assigned (return 1).
1013 if (perf_pmu__scan_file_at(pmu, dirfd, "type", "%u", &type) != 1)
1016 INIT_LIST_HEAD(&pmu->format);
1017 INIT_LIST_HEAD(&pmu->aliases);
1018 INIT_LIST_HEAD(&pmu->caps);
1021 * The pmu data we store & need consists of the pmu
1022 * type value and format definitions. Load both right
1025 if (pmu_format(pmu, dirfd, name))
1028 pmu->is_core = is_pmu_core(name);
1029 pmu->cpus = pmu_cpumask(dirfd, name, pmu->is_core);
1032 pmu->is_uncore = pmu_is_uncore(dirfd, name);
1034 pmu->id = pmu_id(name);
1035 pmu->max_precise = pmu_max_precise(dirfd, pmu);
1036 pmu->alias_name = pmu_find_alias_name(pmu, dirfd);
1037 pmu->events_table = perf_pmu__find_events_table(pmu);
1038 pmu_add_sys_aliases(pmu);
1039 list_add_tail(&pmu->list, pmus);
1041 perf_pmu__arch_init(pmu);
1050 /* Creates the PMU when sysfs scanning fails. */
1051 struct perf_pmu *perf_pmu__create_placeholder_core_pmu(struct list_head *core_pmus)
1053 struct perf_pmu *pmu = zalloc(sizeof(*pmu));
1058 pmu->name = strdup("cpu");
1064 pmu->is_core = true;
1065 pmu->type = PERF_TYPE_RAW;
1066 pmu->cpus = cpu_map__online();
1068 INIT_LIST_HEAD(&pmu->format);
1069 INIT_LIST_HEAD(&pmu->aliases);
1070 INIT_LIST_HEAD(&pmu->caps);
1071 list_add_tail(&pmu->list, core_pmus);
1075 void perf_pmu__warn_invalid_formats(struct perf_pmu *pmu)
1077 struct perf_pmu_format *format;
1079 if (pmu->formats_checked)
1082 pmu->formats_checked = true;
1084 /* fake pmu doesn't have format list */
1085 if (pmu == &perf_pmu__fake)
1088 list_for_each_entry(format, &pmu->format, list) {
1089 perf_pmu_format__load(pmu, format);
1090 if (format->value >= PERF_PMU_FORMAT_VALUE_CONFIG_END) {
1091 pr_warning("WARNING: '%s' format '%s' requires 'perf_event_attr::config%d'"
1092 "which is not supported by this version of perf!\n",
1093 pmu->name, format->name, format->value);
1099 bool evsel__is_aux_event(const struct evsel *evsel)
1101 struct perf_pmu *pmu = evsel__find_pmu(evsel);
1103 return pmu && pmu->auxtrace;
1107 * Set @config_name to @val as long as the user hasn't already set or cleared it
1108 * by passing a config term on the command line.
1110 * @val is the value to put into the bits specified by @config_name rather than
1111 * the bit pattern. It is shifted into position by this function, so to set
1112 * something to true, pass 1 for val rather than a pre shifted value.
1114 #define field_prep(_mask, _val) (((_val) << (ffsll(_mask) - 1)) & (_mask))
1115 void evsel__set_config_if_unset(struct perf_pmu *pmu, struct evsel *evsel,
1116 const char *config_name, u64 val)
1118 u64 user_bits = 0, bits;
1119 struct evsel_config_term *term = evsel__get_config_term(evsel, CFG_CHG);
1122 user_bits = term->val.cfg_chg;
1124 bits = perf_pmu__format_bits(pmu, config_name);
1126 /* Do nothing if the user changed the value */
1127 if (bits & user_bits)
1130 /* Otherwise replace it */
1131 evsel->core.attr.config &= ~bits;
1132 evsel->core.attr.config |= field_prep(bits, val);
1135 static struct perf_pmu_format *
1136 pmu_find_format(const struct list_head *formats, const char *name)
1138 struct perf_pmu_format *format;
1140 list_for_each_entry(format, formats, list)
1141 if (!strcmp(format->name, name))
1147 __u64 perf_pmu__format_bits(struct perf_pmu *pmu, const char *name)
1149 struct perf_pmu_format *format = pmu_find_format(&pmu->format, name);
1156 for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
1157 bits |= 1ULL << fbit;
1162 int perf_pmu__format_type(struct perf_pmu *pmu, const char *name)
1164 struct perf_pmu_format *format = pmu_find_format(&pmu->format, name);
1169 perf_pmu_format__load(pmu, format);
1170 return format->value;
1174 * Sets value based on the format definition (format parameter)
1175 * and unformatted value (value parameter).
1177 static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
1180 unsigned long fbit, vbit;
1182 for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
1184 if (!test_bit(fbit, format))
1187 if (value & (1llu << vbit++))
1188 *v |= (1llu << fbit);
1190 *v &= ~(1llu << fbit);
1194 static __u64 pmu_format_max_value(const unsigned long *format)
1198 w = bitmap_weight(format, PERF_PMU_FORMAT_BITS);
1202 return (1ULL << w) - 1;
1207 * Term is a string term, and might be a param-term. Try to look up it's value
1208 * in the remaining terms.
1209 * - We have a term like "base-or-format-term=param-term",
1210 * - We need to find the value supplied for "param-term" (with param-term named
1211 * in a config string) later on in the term list.
1213 static int pmu_resolve_param_term(struct parse_events_term *term,
1214 struct parse_events_terms *head_terms,
1217 struct parse_events_term *t;
1219 list_for_each_entry(t, &head_terms->terms, list) {
1220 if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM &&
1221 t->config && !strcmp(t->config, term->config)) {
1223 *value = t->val.num;
1229 printf("Required parameter '%s' not specified\n", term->config);
1234 static char *pmu_formats_string(const struct list_head *formats)
1236 struct perf_pmu_format *format;
1238 struct strbuf buf = STRBUF_INIT;
1244 /* sysfs exported terms */
1245 list_for_each_entry(format, formats, list)
1246 if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
1249 str = strbuf_detach(&buf, NULL);
1251 strbuf_release(&buf);
1257 * Setup one of config[12] attr members based on the
1258 * user input data - term parameter.
1260 static int pmu_config_term(const struct perf_pmu *pmu,
1261 struct perf_event_attr *attr,
1262 struct parse_events_term *term,
1263 struct parse_events_terms *head_terms,
1264 bool zero, struct parse_events_error *err)
1266 struct perf_pmu_format *format;
1271 * If this is a parameter we've already used for parameterized-eval,
1272 * skip it in normal eval.
1278 * Hardcoded terms should be already in, so nothing
1279 * to be done for them.
1281 if (parse_events__is_hardcoded_term(term))
1284 format = pmu_find_format(&pmu->format, term->config);
1286 char *pmu_term = pmu_formats_string(&pmu->format);
1290 if (asprintf(&unknown_term,
1291 "unknown term '%s' for pmu '%s'",
1292 term->config, pmu->name) < 0)
1293 unknown_term = NULL;
1294 help_msg = parse_events_formats_error_string(pmu_term);
1296 parse_events_error__handle(err, term->err_term,
1300 pr_debug("%s (%s)\n", unknown_term, help_msg);
1306 perf_pmu_format__load(pmu, format);
1307 switch (format->value) {
1308 case PERF_PMU_FORMAT_VALUE_CONFIG:
1311 case PERF_PMU_FORMAT_VALUE_CONFIG1:
1312 vp = &attr->config1;
1314 case PERF_PMU_FORMAT_VALUE_CONFIG2:
1315 vp = &attr->config2;
1317 case PERF_PMU_FORMAT_VALUE_CONFIG3:
1318 vp = &attr->config3;
1325 * Either directly use a numeric term, or try to translate string terms
1326 * using event parameters.
1328 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1329 if (term->no_value &&
1330 bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) {
1332 parse_events_error__handle(err, term->err_val,
1333 strdup("no value assigned for term"),
1339 val = term->val.num;
1340 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1341 if (strcmp(term->val.str, "?")) {
1343 pr_info("Invalid sysfs entry %s=%s\n",
1344 term->config, term->val.str);
1347 parse_events_error__handle(err, term->err_val,
1348 strdup("expected numeric value"),
1354 if (pmu_resolve_param_term(term, head_terms, &val))
1359 max_val = pmu_format_max_value(format->bits);
1360 if (val > max_val) {
1364 parse_events_error__handle(err, term->err_val,
1366 "value too big for format (%s), maximum is %llu",
1367 format->name, (unsigned long long)max_val) < 0
1368 ? strdup("value too big for format")
1374 * Assume we don't care if !err, in which case the value will be
1375 * silently truncated.
1379 pmu_format_value(format->bits, val, vp, zero);
1383 int perf_pmu__config_terms(const struct perf_pmu *pmu,
1384 struct perf_event_attr *attr,
1385 struct parse_events_terms *terms,
1386 bool zero, struct parse_events_error *err)
1388 struct parse_events_term *term;
1390 list_for_each_entry(term, &terms->terms, list) {
1391 if (pmu_config_term(pmu, attr, term, terms, zero, err))
1399 * Configures event's 'attr' parameter based on the:
1400 * 1) users input - specified in terms parameter
1401 * 2) pmu format definitions - specified by pmu parameter
1403 int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
1404 struct parse_events_terms *head_terms,
1405 struct parse_events_error *err)
1407 bool zero = !!pmu->perf_event_attr_init_default;
1409 return perf_pmu__config_terms(pmu, attr, head_terms, zero, err);
1412 static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
1413 struct parse_events_term *term)
1415 struct perf_pmu_alias *alias;
1418 if (parse_events__is_hardcoded_term(term))
1421 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1422 if (!term->no_value)
1424 if (pmu_find_format(&pmu->format, term->config))
1426 name = term->config;
1428 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1429 if (strcasecmp(term->config, "event"))
1431 name = term->val.str;
1436 alias = perf_pmu__find_alias(pmu, name, /*load=*/ true);
1437 if (alias || pmu->cpu_aliases_added)
1440 /* Alias doesn't exist, try to get it from the json events. */
1441 if (pmu->events_table &&
1442 pmu_events_table__find_event(pmu->events_table, pmu, name,
1443 pmu_add_cpu_aliases_map_callback,
1445 alias = perf_pmu__find_alias(pmu, name, /*load=*/ false);
1451 static int check_info_data(struct perf_pmu *pmu,
1452 struct perf_pmu_alias *alias,
1453 struct perf_pmu_info *info,
1454 struct parse_events_error *err,
1457 read_alias_info(pmu, alias);
1459 * Only one term in event definition can
1460 * define unit, scale and snapshot, fail
1461 * if there's more than one.
1463 if (info->unit && alias->unit[0]) {
1464 parse_events_error__handle(err, column,
1465 strdup("Attempt to set event's unit twice"),
1469 if (info->scale && alias->scale) {
1470 parse_events_error__handle(err, column,
1471 strdup("Attempt to set event's scale twice"),
1475 if (info->snapshot && alias->snapshot) {
1476 parse_events_error__handle(err, column,
1477 strdup("Attempt to set event snapshot twice"),
1483 info->unit = alias->unit;
1486 info->scale = alias->scale;
1488 if (alias->snapshot)
1489 info->snapshot = alias->snapshot;
1495 * Find alias in the terms list and replace it with the terms
1496 * defined for the alias
1498 int perf_pmu__check_alias(struct perf_pmu *pmu, struct parse_events_terms *head_terms,
1499 struct perf_pmu_info *info, bool *rewrote_terms,
1500 struct parse_events_error *err)
1502 struct parse_events_term *term, *h;
1503 struct perf_pmu_alias *alias;
1506 *rewrote_terms = false;
1507 info->per_pkg = false;
1510 * Mark unit and scale as not set
1511 * (different from default values, see below)
1515 info->snapshot = false;
1517 list_for_each_entry_safe(term, h, &head_terms->terms, list) {
1518 alias = pmu_find_alias(pmu, term);
1521 ret = pmu_alias_terms(alias, term->err_term, &term->list);
1523 parse_events_error__handle(err, term->err_term,
1524 strdup("Failure to duplicate terms"),
1528 *rewrote_terms = true;
1529 ret = check_info_data(pmu, alias, info, err, term->err_term);
1534 info->per_pkg = true;
1536 list_del_init(&term->list);
1537 parse_events_term__delete(term);
1541 * if no unit or scale found in aliases, then
1542 * set defaults as for evsel
1543 * unit cannot left to NULL
1545 if (info->unit == NULL)
1548 if (info->scale == 0.0)
1554 struct find_event_args {
1557 pmu_event_callback cb;
1560 static int find_event_callback(void *state, struct pmu_event_info *info)
1562 struct find_event_args *args = state;
1564 if (!strcmp(args->event, info->name))
1565 return args->cb(args->state, info);
1570 int perf_pmu__find_event(struct perf_pmu *pmu, const char *event, void *state, pmu_event_callback cb)
1572 struct find_event_args args = {
1578 /* Sub-optimal, but function is only used by tests. */
1579 return perf_pmu__for_each_event(pmu, /*skip_duplicate_pmus=*/ false,
1580 &args, find_event_callback);
1583 static void perf_pmu__del_formats(struct list_head *formats)
1585 struct perf_pmu_format *fmt, *tmp;
1587 list_for_each_entry_safe(fmt, tmp, formats, list) {
1588 list_del(&fmt->list);
1594 bool perf_pmu__has_format(const struct perf_pmu *pmu, const char *name)
1596 struct perf_pmu_format *format;
1598 list_for_each_entry(format, &pmu->format, list) {
1599 if (!strcmp(format->name, name))
1605 bool is_pmu_core(const char *name)
1607 return !strcmp(name, "cpu") || !strcmp(name, "cpum_cf") || is_sysfs_pmu_core(name);
1610 bool perf_pmu__supports_legacy_cache(const struct perf_pmu *pmu)
1612 return pmu->is_core;
1615 bool perf_pmu__auto_merge_stats(const struct perf_pmu *pmu)
1617 return !pmu->is_core || perf_pmus__num_core_pmus() == 1;
1620 bool perf_pmu__have_event(struct perf_pmu *pmu, const char *name)
1624 if (perf_pmu__find_alias(pmu, name, /*load=*/ true) != NULL)
1626 if (pmu->cpu_aliases_added || !pmu->events_table)
1628 return pmu_events_table__find_event(pmu->events_table, pmu, name, NULL, NULL) == 0;
1631 size_t perf_pmu__num_events(struct perf_pmu *pmu)
1635 if (!pmu->sysfs_aliases_loaded)
1636 pmu_aliases_parse(pmu);
1638 nr = pmu->sysfs_aliases;
1640 if (pmu->cpu_aliases_added)
1641 nr += pmu->loaded_json_aliases;
1642 else if (pmu->events_table)
1643 nr += pmu_events_table__num_events(pmu->events_table, pmu) - pmu->loaded_json_aliases;
1645 return pmu->selectable ? nr + 1 : nr;
1648 static int sub_non_neg(int a, int b)
1655 static char *format_alias(char *buf, int len, const struct perf_pmu *pmu,
1656 const struct perf_pmu_alias *alias, bool skip_duplicate_pmus)
1658 struct parse_events_term *term;
1659 int pmu_name_len = skip_duplicate_pmus
1660 ? pmu_name_len_no_suffix(pmu->name, /*num=*/NULL)
1661 : (int)strlen(pmu->name);
1662 int used = snprintf(buf, len, "%.*s/%s", pmu_name_len, pmu->name, alias->name);
1664 list_for_each_entry(term, &alias->terms.terms, list) {
1665 if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
1666 used += snprintf(buf + used, sub_non_neg(len, used),
1667 ",%s=%s", term->config,
1671 if (sub_non_neg(len, used) > 0) {
1675 if (sub_non_neg(len, used) > 0) {
1679 buf[len - 1] = '\0';
1684 int perf_pmu__for_each_event(struct perf_pmu *pmu, bool skip_duplicate_pmus,
1685 void *state, pmu_event_callback cb)
1688 struct perf_pmu_alias *event;
1689 struct pmu_event_info info = {
1695 strbuf_init(&sb, /*hint=*/ 0);
1696 pmu_add_cpu_aliases(pmu);
1697 list_for_each_entry(event, &pmu->aliases, list) {
1700 info.pmu_name = event->pmu_name ?: pmu->name;
1703 info.name = event->name;
1706 info.name = format_alias(buf, sizeof(buf), pmu, event,
1707 skip_duplicate_pmus);
1709 info.alias = info.name;
1710 info.name = event->name;
1712 buf_used = strlen(buf) + 1;
1714 info.scale_unit = NULL;
1715 if (strlen(event->unit) || event->scale != 1.0) {
1716 info.scale_unit = buf + buf_used;
1717 buf_used += snprintf(buf + buf_used, sizeof(buf) - buf_used,
1718 "%G%s", event->scale, event->unit) + 1;
1720 info.desc = event->desc;
1721 info.long_desc = event->long_desc;
1722 info.encoding_desc = buf + buf_used;
1723 parse_events_terms__to_strbuf(&event->terms, &sb);
1724 buf_used += snprintf(buf + buf_used, sizeof(buf) - buf_used,
1725 "%s/%s/", info.pmu_name, sb.buf) + 1;
1726 info.topic = event->topic;
1728 info.deprecated = event->deprecated;
1729 ret = cb(state, &info);
1732 strbuf_setlen(&sb, /*len=*/ 0);
1734 if (pmu->selectable) {
1736 snprintf(buf, sizeof(buf), "%s//", pmu->name);
1738 info.scale_unit = NULL;
1740 info.long_desc = NULL;
1741 info.encoding_desc = NULL;
1743 info.pmu_name = pmu->name;
1744 info.deprecated = false;
1745 ret = cb(state, &info);
1748 strbuf_release(&sb);
1752 bool pmu__name_match(const struct perf_pmu *pmu, const char *pmu_name)
1754 return !strcmp(pmu->name, pmu_name) ||
1755 (pmu->is_uncore && pmu_uncore_alias_match(pmu_name, pmu->name)) ||
1757 * jevents and tests use default_core as a marker for any core
1758 * PMU as the PMU name varies across architectures.
1760 (pmu->is_core && !strcmp(pmu_name, "default_core"));
1763 bool perf_pmu__is_software(const struct perf_pmu *pmu)
1765 const char *known_sw_pmus[] = {
1771 if (pmu->is_core || pmu->is_uncore || pmu->auxtrace)
1773 switch (pmu->type) {
1774 case PERF_TYPE_HARDWARE: return false;
1775 case PERF_TYPE_SOFTWARE: return true;
1776 case PERF_TYPE_TRACEPOINT: return true;
1777 case PERF_TYPE_HW_CACHE: return false;
1778 case PERF_TYPE_RAW: return false;
1779 case PERF_TYPE_BREAKPOINT: return true;
1782 for (size_t i = 0; i < ARRAY_SIZE(known_sw_pmus); i++) {
1783 if (!strcmp(pmu->name, known_sw_pmus[i]))
1789 FILE *perf_pmu__open_file(const struct perf_pmu *pmu, const char *name)
1791 char path[PATH_MAX];
1793 if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, name) ||
1794 !file_available(path))
1797 return fopen(path, "r");
1800 FILE *perf_pmu__open_file_at(const struct perf_pmu *pmu, int dirfd, const char *name)
1804 fd = perf_pmu__pathname_fd(dirfd, pmu->name, name, O_RDONLY);
1808 return fdopen(fd, "r");
1811 int perf_pmu__scan_file(const struct perf_pmu *pmu, const char *name, const char *fmt,
1818 va_start(args, fmt);
1819 file = perf_pmu__open_file(pmu, name);
1821 ret = vfscanf(file, fmt, args);
1828 int perf_pmu__scan_file_at(const struct perf_pmu *pmu, int dirfd, const char *name,
1829 const char *fmt, ...)
1835 va_start(args, fmt);
1836 file = perf_pmu__open_file_at(pmu, dirfd, name);
1838 ret = vfscanf(file, fmt, args);
1845 bool perf_pmu__file_exists(const struct perf_pmu *pmu, const char *name)
1847 char path[PATH_MAX];
1849 if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, name))
1852 return file_available(path);
1855 static int perf_pmu__new_caps(struct list_head *list, char *name, char *value)
1857 struct perf_pmu_caps *caps = zalloc(sizeof(*caps));
1862 caps->name = strdup(name);
1865 caps->value = strndup(value, strlen(value) - 1);
1868 list_add_tail(&caps->list, list);
1879 static void perf_pmu__del_caps(struct perf_pmu *pmu)
1881 struct perf_pmu_caps *caps, *tmp;
1883 list_for_each_entry_safe(caps, tmp, &pmu->caps, list) {
1884 list_del(&caps->list);
1886 zfree(&caps->value);
1892 * Reading/parsing the given pmu capabilities, which should be located at:
1893 * /sys/bus/event_source/devices/<dev>/caps as sysfs group attributes.
1894 * Return the number of capabilities
1896 int perf_pmu__caps_parse(struct perf_pmu *pmu)
1899 char caps_path[PATH_MAX];
1901 struct dirent *evt_ent;
1904 if (pmu->caps_initialized)
1905 return pmu->nr_caps;
1909 if (!perf_pmu__pathname_scnprintf(caps_path, sizeof(caps_path), pmu->name, "caps"))
1912 if (stat(caps_path, &st) < 0) {
1913 pmu->caps_initialized = true;
1914 return 0; /* no error if caps does not exist */
1917 caps_dir = opendir(caps_path);
1921 caps_fd = dirfd(caps_dir);
1923 while ((evt_ent = readdir(caps_dir)) != NULL) {
1924 char *name = evt_ent->d_name;
1929 if (!strcmp(name, ".") || !strcmp(name, ".."))
1932 fd = openat(caps_fd, name, O_RDONLY);
1935 file = fdopen(fd, "r");
1941 if (!fgets(value, sizeof(value), file) ||
1942 (perf_pmu__new_caps(&pmu->caps, name, value) < 0)) {
1953 pmu->caps_initialized = true;
1954 return pmu->nr_caps;
1957 static void perf_pmu__compute_config_masks(struct perf_pmu *pmu)
1959 struct perf_pmu_format *format;
1961 if (pmu->config_masks_computed)
1964 list_for_each_entry(format, &pmu->format, list) {
1968 if (format->value >= PERF_PMU_FORMAT_VALUE_CONFIG_END)
1971 pmu->config_masks_present = true;
1972 mask = &pmu->config_masks[format->value];
1974 for_each_set_bit(i, format->bits, PERF_PMU_FORMAT_BITS)
1977 pmu->config_masks_computed = true;
1980 void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config,
1981 const char *name, int config_num,
1982 const char *config_name)
1987 perf_pmu__compute_config_masks(pmu);
1990 * Kernel doesn't export any valid format bits.
1992 if (!pmu->config_masks_present)
1995 bits = config & ~pmu->config_masks[config_num];
1999 bitmap_scnprintf((unsigned long *)&bits, sizeof(bits) * 8, buf, sizeof(buf));
2001 pr_warning("WARNING: event '%s' not valid (bits %s of %s "
2002 "'%llx' not supported by kernel)!\n",
2003 name ?: "N/A", buf, config_name, config);
2006 int perf_pmu__match(const char *pattern, const char *name, const char *tok)
2011 if (fnmatch(pattern, name, 0))
2014 if (tok && !perf_pmu__match_ignoring_suffix(name, tok))
2020 double __weak perf_pmu__cpu_slots_per_cycle(void)
2025 int perf_pmu__event_source_devices_scnprintf(char *pathname, size_t size)
2027 const char *sysfs = sysfs__mountpoint();
2031 return scnprintf(pathname, size, "%s/bus/event_source/devices/", sysfs);
2034 int perf_pmu__event_source_devices_fd(void)
2036 char path[PATH_MAX];
2037 const char *sysfs = sysfs__mountpoint();
2042 scnprintf(path, sizeof(path), "%s/bus/event_source/devices/", sysfs);
2043 return open(path, O_DIRECTORY);
2047 * Fill 'buf' with the path to a file or folder in 'pmu_name' in
2048 * sysfs. For example if pmu_name = "cs_etm" and 'filename' = "format"
2049 * then pathname will be filled with
2050 * "/sys/bus/event_source/devices/cs_etm/format"
2052 * Return 0 if the sysfs mountpoint couldn't be found, if no characters were
2053 * written or if the buffer size is exceeded.
2055 int perf_pmu__pathname_scnprintf(char *buf, size_t size,
2056 const char *pmu_name, const char *filename)
2060 len = perf_pmu__event_source_devices_scnprintf(buf, size);
2061 if (!len || (len + strlen(pmu_name) + strlen(filename) + 1) >= size)
2064 return scnprintf(buf + len, size - len, "%s/%s", pmu_name, filename);
2067 int perf_pmu__pathname_fd(int dirfd, const char *pmu_name, const char *filename, int flags)
2069 char path[PATH_MAX];
2071 scnprintf(path, sizeof(path), "%s/%s", pmu_name, filename);
2072 return openat(dirfd, path, flags);
2075 void perf_pmu__delete(struct perf_pmu *pmu)
2077 perf_pmu__del_formats(&pmu->format);
2078 perf_pmu__del_aliases(pmu);
2079 perf_pmu__del_caps(pmu);
2081 perf_cpu_map__put(pmu->cpus);
2084 zfree(&pmu->alias_name);