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;
34 #define UNIT_MAX_LEN 31 /* max length for event unit name */
37 * struct perf_pmu_alias - An event either read from sysfs or builtin in
38 * pmu-events.c, created by parsing the pmu-events json files.
40 struct perf_pmu_alias {
41 /** @name: Name of the event like "mem-loads". */
43 /** @desc: Optional short description of the event. */
45 /** @long_desc: Optional long description. */
48 * @topic: Optional topic such as cache or pipeline, particularly for
53 * @str: Comma separated parameter list like
54 * "event=0xcd,umask=0x1,ldlat=0x3".
57 /** @terms: Owned list of the original parsed parameters. */
58 struct list_head terms;
59 /** @list: List element of struct perf_pmu aliases. */
60 struct list_head list;
61 /** @unit: Units for the event, such as bytes or cache lines. */
62 char unit[UNIT_MAX_LEN+1];
63 /** @scale: Value to scale read counter values by. */
66 * @per_pkg: Does the file
67 * <sysfs>/bus/event_source/devices/<pmu_name>/events/<name>.per-pkg or
68 * equivalent json value exist and have the value 1.
72 * @snapshot: Does the file
73 * <sysfs>/bus/event_source/devices/<pmu_name>/events/<name>.snapshot
74 * exist and have the value 1.
78 * @deprecated: Is the event hidden and so not shown in perf list by
83 * @pmu_name: The name copied from the json struct pmu_event. This can
84 * differ from the PMU name as it won't have suffixes.
90 * struct perf_pmu_format - Values from a format file read from
91 * <sysfs>/devices/cpu/format/ held in struct perf_pmu.
93 * For example, the contents of <sysfs>/devices/cpu/format/event may be
94 * "config:0-7" and will be represented here as name="event",
95 * value=PERF_PMU_FORMAT_VALUE_CONFIG and bits 0 to 7 will be set.
97 struct perf_pmu_format {
98 /** @list: Element on list within struct perf_pmu. */
99 struct list_head list;
100 /** @bits: Which config bits are set by this format value. */
101 DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
102 /** @name: The modifier/file name. */
105 * @value : Which config value the format relates to. Supported values
106 * are from PERF_PMU_FORMAT_VALUE_CONFIG to
107 * PERF_PMU_FORMAT_VALUE_CONFIG_END.
110 /** @loaded: Has the contents been loaded/parsed. */
114 static struct perf_pmu_format *perf_pmu__new_format(struct list_head *list, char *name)
116 struct perf_pmu_format *format;
118 format = zalloc(sizeof(*format));
122 format->name = strdup(name);
127 list_add_tail(&format->list, list);
131 /* Called at the end of parsing a format. */
132 void perf_pmu_format__set_value(void *vformat, int config, unsigned long *bits)
134 struct perf_pmu_format *format = vformat;
136 format->value = config;
137 memcpy(format->bits, bits, sizeof(format->bits));
140 static void __perf_pmu_format__load(struct perf_pmu_format *format, FILE *file)
145 ret = perf_pmu_lex_init(&scanner);
149 perf_pmu_set_in(file, scanner);
150 ret = perf_pmu_parse(format, scanner);
151 perf_pmu_lex_destroy(scanner);
152 format->loaded = true;
155 static void perf_pmu_format__load(struct perf_pmu *pmu, struct perf_pmu_format *format)
163 if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, "format"))
166 assert(strlen(path) + strlen(format->name) + 2 < sizeof(path));
168 strcat(path, format->name);
170 file = fopen(path, "r");
173 __perf_pmu_format__load(format, file);
178 * Parse & process all the sysfs attributes located under
179 * the directory specified in 'dir' parameter.
181 int perf_pmu__format_parse(struct perf_pmu *pmu, int dirfd, bool eager_load)
183 struct dirent *evt_ent;
187 format_dir = fdopendir(dirfd);
191 while ((evt_ent = readdir(format_dir)) != NULL) {
192 struct perf_pmu_format *format;
193 char *name = evt_ent->d_name;
195 if (!strcmp(name, ".") || !strcmp(name, ".."))
198 format = perf_pmu__new_format(&pmu->format, name);
206 int fd = openat(dirfd, name, O_RDONLY);
212 file = fdopen(fd, "r");
217 __perf_pmu_format__load(format, file);
222 closedir(format_dir);
227 * Reading/parsing the default pmu format definition, which should be
229 * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
231 static int pmu_format(struct perf_pmu *pmu, int dirfd, const char *name)
235 fd = perf_pmu__pathname_fd(dirfd, name, "format", O_DIRECTORY);
239 /* it'll close the fd */
240 if (perf_pmu__format_parse(pmu, fd, /*eager_load=*/false))
246 int perf_pmu__convert_scale(const char *scale, char **end, double *sval)
252 * save current locale
254 lc = setlocale(LC_NUMERIC, NULL);
257 * The lc string may be allocated in static storage,
258 * so get a dynamic copy to make it survive setlocale
268 * force to C locale to ensure kernel
269 * scale string is converted correctly.
270 * kernel uses default C locale.
272 setlocale(LC_NUMERIC, "C");
274 *sval = strtod(scale, end);
278 setlocale(LC_NUMERIC, lc);
283 static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, int dirfd, char *name)
291 scnprintf(path, PATH_MAX, "%s.scale", name);
293 fd = openat(dirfd, path, O_RDONLY);
297 if (fstat(fd, &st) < 0)
300 sret = read(fd, scale, sizeof(scale)-1);
304 if (scale[sret - 1] == '\n')
305 scale[sret - 1] = '\0';
309 ret = perf_pmu__convert_scale(scale, NULL, &alias->scale);
315 static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, int dirfd, char *name)
321 scnprintf(path, PATH_MAX, "%s.unit", name);
323 fd = openat(dirfd, path, O_RDONLY);
327 sret = read(fd, alias->unit, UNIT_MAX_LEN);
333 if (alias->unit[sret - 1] == '\n')
334 alias->unit[sret - 1] = '\0';
336 alias->unit[sret] = '\0';
341 alias->unit[0] = '\0';
346 perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, int dirfd, char *name)
351 scnprintf(path, PATH_MAX, "%s.per-pkg", name);
353 fd = openat(dirfd, path, O_RDONLY);
359 alias->per_pkg = true;
363 static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias,
364 int dirfd, char *name)
369 scnprintf(path, PATH_MAX, "%s.snapshot", name);
371 fd = openat(dirfd, path, O_RDONLY);
375 alias->snapshot = true;
380 static void perf_pmu_assign_str(char *name, const char *field, char **old_str,
386 if (*new_str) { /* Have new string, check with old */
387 if (strcasecmp(*old_str, *new_str))
388 pr_debug("alias %s differs in field '%s'\n",
391 } else /* Nothing new --> keep old string */
398 static void perf_pmu_update_alias(struct perf_pmu_alias *old,
399 struct perf_pmu_alias *newalias)
401 perf_pmu_assign_str(old->name, "desc", &old->desc, &newalias->desc);
402 perf_pmu_assign_str(old->name, "long_desc", &old->long_desc,
403 &newalias->long_desc);
404 perf_pmu_assign_str(old->name, "topic", &old->topic, &newalias->topic);
405 perf_pmu_assign_str(old->name, "value", &old->str, &newalias->str);
406 old->scale = newalias->scale;
407 old->per_pkg = newalias->per_pkg;
408 old->snapshot = newalias->snapshot;
409 memcpy(old->unit, newalias->unit, sizeof(old->unit));
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->str);
420 zfree(&newalias->pmu_name);
421 parse_events_terms__purge(&newalias->terms);
425 static void perf_pmu__del_aliases(struct perf_pmu *pmu)
427 struct perf_pmu_alias *alias, *tmp;
429 list_for_each_entry_safe(alias, tmp, &pmu->aliases, list) {
430 list_del(&alias->list);
431 perf_pmu_free_alias(alias);
435 /* Merge an alias, search in alias list. If this name is already
436 * present merge both of them to combine all information.
438 static bool perf_pmu_merge_alias(struct perf_pmu_alias *newalias,
439 struct list_head *alist)
441 struct perf_pmu_alias *a;
443 list_for_each_entry(a, alist, list) {
444 if (!strcasecmp(newalias->name, a->name)) {
445 if (newalias->pmu_name && a->pmu_name &&
446 !strcasecmp(newalias->pmu_name, a->pmu_name)) {
449 perf_pmu_update_alias(a, newalias);
450 perf_pmu_free_alias(newalias);
457 static int __perf_pmu__new_alias(struct list_head *list, int dirfd, char *name,
458 char *desc, char *val, const struct pmu_event *pe)
460 struct parse_events_term *term;
461 struct perf_pmu_alias *alias;
464 const char *long_desc = NULL, *topic = NULL, *unit = NULL, *pmu_name = NULL;
465 bool deprecated = false, perpkg = false;
468 long_desc = pe->long_desc;
472 deprecated = pe->deprecated;
476 alias = malloc(sizeof(*alias));
480 INIT_LIST_HEAD(&alias->terms);
482 alias->unit[0] = '\0';
483 alias->per_pkg = perpkg;
484 alias->snapshot = false;
485 alias->deprecated = deprecated;
487 ret = parse_events_terms(&alias->terms, val);
489 pr_err("Cannot parse alias %s: %d\n", val, ret);
494 /* Scan event and remove leading zeroes, spaces, newlines, some
495 * platforms have terms specified as
496 * event=0x0091 (read from files ../<PMU>/events/<FILE>
497 * and terms specified as event=0x91 (read from JSON files).
499 * Rebuild string to make alias->str member comparable.
501 memset(newval, 0, sizeof(newval));
503 list_for_each_entry(term, &alias->terms, list) {
505 ret += scnprintf(newval + ret, sizeof(newval) - ret,
507 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM)
508 ret += scnprintf(newval + ret, sizeof(newval) - ret,
509 "%s=%#x", term->config, term->val.num);
510 else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
511 ret += scnprintf(newval + ret, sizeof(newval) - ret,
512 "%s=%s", term->config, term->val.str);
515 alias->name = strdup(name);
518 * load unit name and scale if available
520 perf_pmu__parse_unit(alias, dirfd, name);
521 perf_pmu__parse_scale(alias, dirfd, name);
522 perf_pmu__parse_per_pkg(alias, dirfd, name);
523 perf_pmu__parse_snapshot(alias, dirfd, name);
526 alias->desc = desc ? strdup(desc) : NULL;
527 alias->long_desc = long_desc ? strdup(long_desc) :
528 desc ? strdup(desc) : NULL;
529 alias->topic = topic ? strdup(topic) : NULL;
531 if (perf_pmu__convert_scale(unit, (char **)&unit, &alias->scale) < 0)
533 snprintf(alias->unit, sizeof(alias->unit), "%s", unit);
535 alias->str = strdup(newval);
536 alias->pmu_name = pmu_name ? strdup(pmu_name) : NULL;
538 if (!perf_pmu_merge_alias(alias, list))
539 list_add_tail(&alias->list, list);
544 static int perf_pmu__new_alias(struct list_head *list, int dirfd, char *name, FILE *file)
549 ret = fread(buf, 1, sizeof(buf), file);
555 /* Remove trailing newline from sysfs file */
558 return __perf_pmu__new_alias(list, dirfd, name, NULL, buf, NULL);
561 static inline bool pmu_alias_info_file(char *name)
566 if (len > 5 && !strcmp(name + len - 5, ".unit"))
568 if (len > 6 && !strcmp(name + len - 6, ".scale"))
570 if (len > 8 && !strcmp(name + len - 8, ".per-pkg"))
572 if (len > 9 && !strcmp(name + len - 9, ".snapshot"))
579 * Process all the sysfs attributes located under the directory
580 * specified in 'dir' parameter.
582 static int pmu_aliases_parse(int dirfd, struct list_head *head)
584 struct dirent *evt_ent;
588 event_dir = fdopendir(dirfd);
592 while ((evt_ent = readdir(event_dir))) {
593 char *name = evt_ent->d_name;
596 if (!strcmp(name, ".") || !strcmp(name, ".."))
600 * skip info files parsed in perf_pmu__new_alias()
602 if (pmu_alias_info_file(name))
605 fd = openat(dirfd, name, O_RDONLY);
607 pr_debug("Cannot open %s\n", name);
610 file = fdopen(fd, "r");
616 if (perf_pmu__new_alias(head, dirfd, name, file) < 0)
617 pr_debug("Cannot set up %s\n", name);
626 * Reading the pmu event aliases definition, which should be located at:
627 * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
629 static int pmu_aliases(struct perf_pmu *pmu, int dirfd, const char *name)
633 fd = perf_pmu__pathname_fd(dirfd, name, "events", O_DIRECTORY);
637 /* it'll close the fd */
638 if (pmu_aliases_parse(fd, &pmu->aliases))
644 static int pmu_alias_terms(struct perf_pmu_alias *alias,
645 struct list_head *terms)
647 struct parse_events_term *term, *cloned;
651 list_for_each_entry(term, &alias->terms, list) {
652 ret = parse_events_term__clone(&cloned, term);
654 parse_events_terms__purge(&list);
658 * Weak terms don't override command line options,
659 * which we don't want for implicit terms in aliases.
662 list_add_tail(&cloned->list, &list);
664 list_splice(&list, terms);
669 * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64)
670 * may have a "cpus" file.
672 static struct perf_cpu_map *pmu_cpumask(int dirfd, const char *name, bool is_core)
674 struct perf_cpu_map *cpus;
675 const char *templates[] = {
680 const char **template;
681 char pmu_name[PATH_MAX];
682 struct perf_pmu pmu = {.name = pmu_name};
685 strlcpy(pmu_name, name, sizeof(pmu_name));
686 for (template = templates; *template; template++) {
687 file = perf_pmu__open_file_at(&pmu, dirfd, *template);
690 cpus = perf_cpu_map__read(file);
696 /* Nothing found, for core PMUs assume this means all CPUs. */
697 return is_core ? perf_cpu_map__get(cpu_map__online()) : NULL;
700 static bool pmu_is_uncore(int dirfd, const char *name)
704 fd = perf_pmu__pathname_fd(dirfd, name, "cpumask", O_PATH);
712 static char *pmu_id(const char *name)
714 char path[PATH_MAX], *str;
717 perf_pmu__pathname_scnprintf(path, sizeof(path), name, "identifier");
719 if (filename__read_str(path, &str, &len) < 0)
722 str[len - 1] = 0; /* remove line feed */
728 * is_sysfs_pmu_core() - PMU CORE devices have different name other than cpu in
729 * sysfs on some platforms like ARM or Intel hybrid. Looking for
730 * possible the cpus file in sysfs files to identify whether this is a
732 * @name: The PMU name such as "cpu_atom".
734 static int is_sysfs_pmu_core(const char *name)
738 if (!perf_pmu__pathname_scnprintf(path, sizeof(path), name, "cpus"))
740 return file_available(path);
743 char *perf_pmu__getcpuid(struct perf_pmu *pmu)
748 cpuid = getenv("PERF_CPUID");
750 cpuid = strdup(cpuid);
752 cpuid = get_cpuid_str(pmu);
757 pr_debug("Using CPUID %s\n", cpuid);
763 __weak const struct pmu_events_table *pmu_events_table__find(void)
765 return perf_pmu__find_events_table(NULL);
768 __weak const struct pmu_metrics_table *pmu_metrics_table__find(void)
770 return perf_pmu__find_metrics_table(NULL);
774 * perf_pmu__match_ignoring_suffix - Does the pmu_name match tok ignoring any
775 * trailing suffix? The Suffix must be in form
776 * tok_{digits}, or tok{digits}.
777 * @pmu_name: The pmu_name with possible suffix.
778 * @tok: The possible match to pmu_name without suffix.
780 static bool perf_pmu__match_ignoring_suffix(const char *pmu_name, const char *tok)
784 if (strncmp(pmu_name, tok, strlen(tok)))
787 p = pmu_name + strlen(tok);
794 /* Ensure we end in a number */
806 * pmu_uncore_alias_match - does name match the PMU name?
807 * @pmu_name: the json struct pmu_event name. This may lack a suffix (which
808 * matches) or be of the form "socket,pmuname" which will match
809 * "socketX_pmunameY".
810 * @name: a real full PMU name as from sysfs.
812 static bool pmu_uncore_alias_match(const char *pmu_name, const char *name)
814 char *tmp = NULL, *tok, *str;
817 if (strchr(pmu_name, ',') == NULL)
818 return perf_pmu__match_ignoring_suffix(name, pmu_name);
820 str = strdup(pmu_name);
825 * uncore alias may be from different PMU with common prefix
827 tok = strtok_r(str, ",", &tmp);
828 if (strncmp(pmu_name, tok, strlen(tok))) {
834 * Match more complex aliases where the alias name is a comma-delimited
835 * list of tokens, orderly contained in the matching PMU name.
837 * Example: For alias "socket,pmuname" and PMU "socketX_pmunameY", we
838 * match "socket" in "socketX_pmunameY" and then "pmuname" in
842 char *next_tok = strtok_r(NULL, ",", &tmp);
844 name = strstr(name, tok);
846 (!next_tok && !perf_pmu__match_ignoring_suffix(name, tok))) {
862 struct pmu_add_cpu_aliases_map_data {
863 /* List being added to. */
864 struct list_head *head;
865 /* If a pmu_event lacks a given PMU the default used. */
866 char *default_pmu_name;
867 /* The PMU that we're searching for events for. */
868 struct perf_pmu *pmu;
871 static int pmu_add_cpu_aliases_map_callback(const struct pmu_event *pe,
872 const struct pmu_events_table *table __maybe_unused,
875 struct pmu_add_cpu_aliases_map_data *data = vdata;
876 const char *pname = pe->pmu ?: data->default_pmu_name;
878 if (!strcmp(pname, data->pmu->name) ||
879 (data->pmu->is_uncore && pmu_uncore_alias_match(pname, data->pmu->name))) {
880 /* need type casts to override 'const' */
881 __perf_pmu__new_alias(data->head, -1, (char *)pe->name, (char *)pe->desc,
882 (char *)pe->event, 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 struct pmu_add_cpu_aliases_map_data data = {
894 .head = &pmu->aliases,
895 .default_pmu_name = perf_pmus__default_pmu_name(),
899 pmu_events_table__for_each_event(table, pmu_add_cpu_aliases_map_callback, &data);
900 free(data.default_pmu_name);
903 static void pmu_add_cpu_aliases(struct perf_pmu *pmu)
905 const struct pmu_events_table *table;
907 table = perf_pmu__find_events_table(pmu);
911 pmu_add_cpu_aliases_table(pmu, table);
914 struct pmu_sys_event_iter_data {
915 struct list_head *head;
916 struct perf_pmu *pmu;
919 static int pmu_add_sys_aliases_iter_fn(const struct pmu_event *pe,
920 const struct pmu_events_table *table __maybe_unused,
923 struct pmu_sys_event_iter_data *idata = data;
924 struct perf_pmu *pmu = idata->pmu;
926 if (!pe->compat || !pe->pmu)
929 if (!strcmp(pmu->id, pe->compat) &&
930 pmu_uncore_alias_match(pe->pmu, pmu->name)) {
931 __perf_pmu__new_alias(idata->head, -1,
941 void pmu_add_sys_aliases(struct perf_pmu *pmu)
943 struct pmu_sys_event_iter_data idata = {
944 .head = &pmu->aliases,
951 pmu_for_each_sys_event(pmu_add_sys_aliases_iter_fn, &idata);
954 struct perf_event_attr * __weak
955 perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
961 pmu_find_real_name(const char *name)
967 pmu_find_alias_name(const char *name __maybe_unused)
972 static int pmu_max_precise(int dirfd, struct perf_pmu *pmu)
974 int max_precise = -1;
976 perf_pmu__scan_file_at(pmu, dirfd, "caps/max_precise", "%d", &max_precise);
980 struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char *lookup_name)
982 struct perf_pmu *pmu;
984 char *name = pmu_find_real_name(lookup_name);
987 pmu = zalloc(sizeof(*pmu));
991 INIT_LIST_HEAD(&pmu->format);
992 INIT_LIST_HEAD(&pmu->aliases);
993 INIT_LIST_HEAD(&pmu->caps);
995 * The pmu data we store & need consists of the pmu
996 * type value and format definitions. Load both right
999 if (pmu_format(pmu, dirfd, name)) {
1004 * Check the aliases first to avoid unnecessary work.
1006 if (pmu_aliases(pmu, dirfd, name)) {
1010 pmu->is_core = is_pmu_core(name);
1011 pmu->cpus = pmu_cpumask(dirfd, name, pmu->is_core);
1012 pmu->name = strdup(name);
1016 /* Read type, and ensure that type value is successfully assigned (return 1) */
1017 if (perf_pmu__scan_file_at(pmu, dirfd, "type", "%u", &type) != 1)
1020 alias_name = pmu_find_alias_name(name);
1022 pmu->alias_name = strdup(alias_name);
1023 if (!pmu->alias_name)
1028 pmu->is_uncore = pmu_is_uncore(dirfd, name);
1030 pmu->id = pmu_id(name);
1031 pmu->max_precise = pmu_max_precise(dirfd, pmu);
1032 pmu_add_cpu_aliases(pmu);
1033 pmu_add_sys_aliases(pmu);
1034 list_add_tail(&pmu->list, pmus);
1036 pmu->default_config = perf_pmu__get_default_config(pmu);
1045 /* Creates the PMU when sysfs scanning fails. */
1046 struct perf_pmu *perf_pmu__create_placeholder_core_pmu(struct list_head *core_pmus)
1048 struct perf_pmu *pmu = zalloc(sizeof(*pmu));
1053 pmu->name = strdup("cpu");
1059 pmu->is_core = true;
1060 pmu->type = PERF_TYPE_RAW;
1061 pmu->cpus = cpu_map__online();
1063 INIT_LIST_HEAD(&pmu->format);
1064 INIT_LIST_HEAD(&pmu->aliases);
1065 INIT_LIST_HEAD(&pmu->caps);
1066 list_add_tail(&pmu->list, core_pmus);
1070 void perf_pmu__warn_invalid_formats(struct perf_pmu *pmu)
1072 struct perf_pmu_format *format;
1074 if (pmu->formats_checked)
1077 pmu->formats_checked = true;
1079 /* fake pmu doesn't have format list */
1080 if (pmu == &perf_pmu__fake)
1083 list_for_each_entry(format, &pmu->format, list) {
1084 perf_pmu_format__load(pmu, format);
1085 if (format->value >= PERF_PMU_FORMAT_VALUE_CONFIG_END) {
1086 pr_warning("WARNING: '%s' format '%s' requires 'perf_event_attr::config%d'"
1087 "which is not supported by this version of perf!\n",
1088 pmu->name, format->name, format->value);
1094 bool evsel__is_aux_event(const struct evsel *evsel)
1096 struct perf_pmu *pmu = evsel__find_pmu(evsel);
1098 return pmu && pmu->auxtrace;
1102 * Set @config_name to @val as long as the user hasn't already set or cleared it
1103 * by passing a config term on the command line.
1105 * @val is the value to put into the bits specified by @config_name rather than
1106 * the bit pattern. It is shifted into position by this function, so to set
1107 * something to true, pass 1 for val rather than a pre shifted value.
1109 #define field_prep(_mask, _val) (((_val) << (ffsll(_mask) - 1)) & (_mask))
1110 void evsel__set_config_if_unset(struct perf_pmu *pmu, struct evsel *evsel,
1111 const char *config_name, u64 val)
1113 u64 user_bits = 0, bits;
1114 struct evsel_config_term *term = evsel__get_config_term(evsel, CFG_CHG);
1117 user_bits = term->val.cfg_chg;
1119 bits = perf_pmu__format_bits(pmu, config_name);
1121 /* Do nothing if the user changed the value */
1122 if (bits & user_bits)
1125 /* Otherwise replace it */
1126 evsel->core.attr.config &= ~bits;
1127 evsel->core.attr.config |= field_prep(bits, val);
1130 static struct perf_pmu_format *
1131 pmu_find_format(struct list_head *formats, const char *name)
1133 struct perf_pmu_format *format;
1135 list_for_each_entry(format, formats, list)
1136 if (!strcmp(format->name, name))
1142 __u64 perf_pmu__format_bits(struct perf_pmu *pmu, const char *name)
1144 struct perf_pmu_format *format = pmu_find_format(&pmu->format, name);
1151 for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
1152 bits |= 1ULL << fbit;
1157 int perf_pmu__format_type(struct perf_pmu *pmu, const char *name)
1159 struct perf_pmu_format *format = pmu_find_format(&pmu->format, name);
1164 perf_pmu_format__load(pmu, format);
1165 return format->value;
1169 * Sets value based on the format definition (format parameter)
1170 * and unformatted value (value parameter).
1172 static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
1175 unsigned long fbit, vbit;
1177 for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
1179 if (!test_bit(fbit, format))
1182 if (value & (1llu << vbit++))
1183 *v |= (1llu << fbit);
1185 *v &= ~(1llu << fbit);
1189 static __u64 pmu_format_max_value(const unsigned long *format)
1193 w = bitmap_weight(format, PERF_PMU_FORMAT_BITS);
1197 return (1ULL << w) - 1;
1202 * Term is a string term, and might be a param-term. Try to look up it's value
1203 * in the remaining terms.
1204 * - We have a term like "base-or-format-term=param-term",
1205 * - We need to find the value supplied for "param-term" (with param-term named
1206 * in a config string) later on in the term list.
1208 static int pmu_resolve_param_term(struct parse_events_term *term,
1209 struct list_head *head_terms,
1212 struct parse_events_term *t;
1214 list_for_each_entry(t, head_terms, list) {
1215 if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM &&
1216 t->config && !strcmp(t->config, term->config)) {
1218 *value = t->val.num;
1224 printf("Required parameter '%s' not specified\n", term->config);
1229 static char *pmu_formats_string(struct list_head *formats)
1231 struct perf_pmu_format *format;
1233 struct strbuf buf = STRBUF_INIT;
1239 /* sysfs exported terms */
1240 list_for_each_entry(format, formats, list)
1241 if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
1244 str = strbuf_detach(&buf, NULL);
1246 strbuf_release(&buf);
1252 * Setup one of config[12] attr members based on the
1253 * user input data - term parameter.
1255 static int pmu_config_term(struct perf_pmu *pmu,
1256 struct perf_event_attr *attr,
1257 struct parse_events_term *term,
1258 struct list_head *head_terms,
1259 bool zero, struct parse_events_error *err)
1261 struct perf_pmu_format *format;
1266 * If this is a parameter we've already used for parameterized-eval,
1267 * skip it in normal eval.
1273 * Hardcoded terms should be already in, so nothing
1274 * to be done for them.
1276 if (parse_events__is_hardcoded_term(term))
1279 format = pmu_find_format(&pmu->format, term->config);
1281 char *pmu_term = pmu_formats_string(&pmu->format);
1285 if (asprintf(&unknown_term,
1286 "unknown term '%s' for pmu '%s'",
1287 term->config, pmu->name) < 0)
1288 unknown_term = NULL;
1289 help_msg = parse_events_formats_error_string(pmu_term);
1291 parse_events_error__handle(err, term->err_term,
1295 pr_debug("%s (%s)\n", unknown_term, help_msg);
1301 perf_pmu_format__load(pmu, format);
1302 switch (format->value) {
1303 case PERF_PMU_FORMAT_VALUE_CONFIG:
1306 case PERF_PMU_FORMAT_VALUE_CONFIG1:
1307 vp = &attr->config1;
1309 case PERF_PMU_FORMAT_VALUE_CONFIG2:
1310 vp = &attr->config2;
1312 case PERF_PMU_FORMAT_VALUE_CONFIG3:
1313 vp = &attr->config3;
1320 * Either directly use a numeric term, or try to translate string terms
1321 * using event parameters.
1323 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1324 if (term->no_value &&
1325 bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) {
1327 parse_events_error__handle(err, term->err_val,
1328 strdup("no value assigned for term"),
1334 val = term->val.num;
1335 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1336 if (strcmp(term->val.str, "?")) {
1338 pr_info("Invalid sysfs entry %s=%s\n",
1339 term->config, term->val.str);
1342 parse_events_error__handle(err, term->err_val,
1343 strdup("expected numeric value"),
1349 if (pmu_resolve_param_term(term, head_terms, &val))
1354 max_val = pmu_format_max_value(format->bits);
1355 if (val > max_val) {
1359 parse_events_error__handle(err, term->err_val,
1361 "value too big for format, maximum is %llu",
1362 (unsigned long long)max_val) < 0
1363 ? strdup("value too big for format")
1369 * Assume we don't care if !err, in which case the value will be
1370 * silently truncated.
1374 pmu_format_value(format->bits, val, vp, zero);
1378 int perf_pmu__config_terms(struct perf_pmu *pmu,
1379 struct perf_event_attr *attr,
1380 struct list_head *head_terms,
1381 bool zero, struct parse_events_error *err)
1383 struct parse_events_term *term;
1385 list_for_each_entry(term, head_terms, list) {
1386 if (pmu_config_term(pmu, attr, term, head_terms, zero, err))
1394 * Configures event's 'attr' parameter based on the:
1395 * 1) users input - specified in terms parameter
1396 * 2) pmu format definitions - specified by pmu parameter
1398 int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
1399 struct list_head *head_terms,
1400 struct parse_events_error *err)
1402 bool zero = !!pmu->default_config;
1404 return perf_pmu__config_terms(pmu, attr, head_terms, zero, err);
1407 static struct perf_pmu_alias *perf_pmu__find_alias(const struct perf_pmu *pmu, const char *str)
1409 struct perf_pmu_alias *alias;
1411 list_for_each_entry(alias, &pmu->aliases, list) {
1412 if (!strcasecmp(alias->name, str))
1418 static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
1419 struct parse_events_term *term)
1423 if (parse_events__is_hardcoded_term(term))
1426 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1427 if (term->val.num != 1)
1429 if (pmu_find_format(&pmu->format, term->config))
1431 name = term->config;
1433 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1434 if (strcasecmp(term->config, "event"))
1436 name = term->val.str;
1441 return perf_pmu__find_alias(pmu, name);
1445 static int check_info_data(struct perf_pmu_alias *alias,
1446 struct perf_pmu_info *info)
1449 * Only one term in event definition can
1450 * define unit, scale and snapshot, fail
1451 * if there's more than one.
1453 if ((info->unit && alias->unit[0]) ||
1454 (info->scale && alias->scale) ||
1455 (info->snapshot && alias->snapshot))
1459 info->unit = alias->unit;
1462 info->scale = alias->scale;
1464 if (alias->snapshot)
1465 info->snapshot = alias->snapshot;
1471 * Find alias in the terms list and replace it with the terms
1472 * defined for the alias
1474 int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
1475 struct perf_pmu_info *info)
1477 struct parse_events_term *term, *h;
1478 struct perf_pmu_alias *alias;
1481 info->per_pkg = false;
1484 * Mark unit and scale as not set
1485 * (different from default values, see below)
1489 info->snapshot = false;
1491 list_for_each_entry_safe(term, h, head_terms, list) {
1492 alias = pmu_find_alias(pmu, term);
1495 ret = pmu_alias_terms(alias, &term->list);
1499 ret = check_info_data(alias, info);
1504 info->per_pkg = true;
1506 list_del_init(&term->list);
1507 parse_events_term__delete(term);
1511 * if no unit or scale found in aliases, then
1512 * set defaults as for evsel
1513 * unit cannot left to NULL
1515 if (info->unit == NULL)
1518 if (info->scale == 0.0)
1524 struct find_event_args {
1527 pmu_event_callback cb;
1530 static int find_event_callback(void *state, struct pmu_event_info *info)
1532 struct find_event_args *args = state;
1534 if (!strcmp(args->event, info->name))
1535 return args->cb(args->state, info);
1540 int perf_pmu__find_event(struct perf_pmu *pmu, const char *event, void *state, pmu_event_callback cb)
1542 struct find_event_args args = {
1548 return perf_pmu__for_each_event(pmu, &args, find_event_callback);
1551 static void perf_pmu__del_formats(struct list_head *formats)
1553 struct perf_pmu_format *fmt, *tmp;
1555 list_for_each_entry_safe(fmt, tmp, formats, list) {
1556 list_del(&fmt->list);
1562 bool perf_pmu__has_format(const struct perf_pmu *pmu, const char *name)
1564 struct perf_pmu_format *format;
1566 list_for_each_entry(format, &pmu->format, list) {
1567 if (!strcmp(format->name, name))
1573 bool is_pmu_core(const char *name)
1575 return !strcmp(name, "cpu") || !strcmp(name, "cpum_cf") || is_sysfs_pmu_core(name);
1578 bool perf_pmu__supports_legacy_cache(const struct perf_pmu *pmu)
1580 return pmu->is_core;
1583 bool perf_pmu__auto_merge_stats(const struct perf_pmu *pmu)
1585 return !pmu->is_core || perf_pmus__num_core_pmus() == 1;
1588 bool perf_pmu__have_event(const struct perf_pmu *pmu, const char *name)
1590 return perf_pmu__find_alias(pmu, name) != NULL;
1593 size_t perf_pmu__num_events(const struct perf_pmu *pmu)
1595 struct list_head *list;
1598 list_for_each(list, &pmu->aliases)
1601 return pmu->selectable ? nr + 1 : nr;
1604 static int sub_non_neg(int a, int b)
1611 static char *format_alias(char *buf, int len, const struct perf_pmu *pmu,
1612 const struct perf_pmu_alias *alias)
1614 struct parse_events_term *term;
1615 int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name);
1617 list_for_each_entry(term, &alias->terms, list) {
1618 if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
1619 used += snprintf(buf + used, sub_non_neg(len, used),
1620 ",%s=%s", term->config,
1624 if (sub_non_neg(len, used) > 0) {
1628 if (sub_non_neg(len, used) > 0) {
1632 buf[len - 1] = '\0';
1637 int perf_pmu__for_each_event(const struct perf_pmu *pmu, void *state, pmu_event_callback cb)
1640 struct perf_pmu_alias *event;
1641 struct pmu_event_info info = {
1646 list_for_each_entry(event, &pmu->aliases, list) {
1649 info.pmu_name = event->pmu_name ?: pmu->name;
1652 info.name = event->name;
1655 info.name = format_alias(buf, sizeof(buf), pmu, event);
1657 info.alias = info.name;
1658 info.name = event->name;
1660 buf_used = strlen(buf) + 1;
1662 info.scale_unit = NULL;
1663 if (strlen(event->unit) || event->scale != 1.0) {
1664 info.scale_unit = buf + buf_used;
1665 buf_used += snprintf(buf + buf_used, sizeof(buf) - buf_used,
1666 "%G%s", event->scale, event->unit) + 1;
1668 info.desc = event->desc;
1669 info.long_desc = event->long_desc;
1670 info.encoding_desc = buf + buf_used;
1671 buf_used += snprintf(buf + buf_used, sizeof(buf) - buf_used,
1672 "%s/%s/", info.pmu_name, event->str) + 1;
1673 info.topic = event->topic;
1674 info.str = event->str;
1675 info.deprecated = event->deprecated;
1676 ret = cb(state, &info);
1680 if (pmu->selectable) {
1682 snprintf(buf, sizeof(buf), "%s//", pmu->name);
1684 info.scale_unit = NULL;
1686 info.long_desc = NULL;
1687 info.encoding_desc = NULL;
1689 info.pmu_name = pmu->name;
1690 info.deprecated = false;
1691 ret = cb(state, &info);
1696 bool perf_pmu__is_software(const struct perf_pmu *pmu)
1698 if (pmu->is_core || pmu->is_uncore || pmu->auxtrace)
1700 switch (pmu->type) {
1701 case PERF_TYPE_HARDWARE: return false;
1702 case PERF_TYPE_SOFTWARE: return true;
1703 case PERF_TYPE_TRACEPOINT: return true;
1704 case PERF_TYPE_HW_CACHE: return false;
1705 case PERF_TYPE_RAW: return false;
1706 case PERF_TYPE_BREAKPOINT: return true;
1709 return !strcmp(pmu->name, "kprobe") || !strcmp(pmu->name, "uprobe");
1712 FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
1714 char path[PATH_MAX];
1716 if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, name) ||
1717 !file_available(path))
1720 return fopen(path, "r");
1723 FILE *perf_pmu__open_file_at(struct perf_pmu *pmu, int dirfd, const char *name)
1727 fd = perf_pmu__pathname_fd(dirfd, pmu->name, name, O_RDONLY);
1731 return fdopen(fd, "r");
1734 int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
1741 va_start(args, fmt);
1742 file = perf_pmu__open_file(pmu, name);
1744 ret = vfscanf(file, fmt, args);
1751 int perf_pmu__scan_file_at(struct perf_pmu *pmu, int dirfd, const char *name,
1752 const char *fmt, ...)
1758 va_start(args, fmt);
1759 file = perf_pmu__open_file_at(pmu, dirfd, name);
1761 ret = vfscanf(file, fmt, args);
1768 bool perf_pmu__file_exists(struct perf_pmu *pmu, const char *name)
1770 char path[PATH_MAX];
1772 if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, name))
1775 return file_available(path);
1778 static int perf_pmu__new_caps(struct list_head *list, char *name, char *value)
1780 struct perf_pmu_caps *caps = zalloc(sizeof(*caps));
1785 caps->name = strdup(name);
1788 caps->value = strndup(value, strlen(value) - 1);
1791 list_add_tail(&caps->list, list);
1802 static void perf_pmu__del_caps(struct perf_pmu *pmu)
1804 struct perf_pmu_caps *caps, *tmp;
1806 list_for_each_entry_safe(caps, tmp, &pmu->caps, list) {
1807 list_del(&caps->list);
1809 zfree(&caps->value);
1815 * Reading/parsing the given pmu capabilities, which should be located at:
1816 * /sys/bus/event_source/devices/<dev>/caps as sysfs group attributes.
1817 * Return the number of capabilities
1819 int perf_pmu__caps_parse(struct perf_pmu *pmu)
1822 char caps_path[PATH_MAX];
1824 struct dirent *evt_ent;
1827 if (pmu->caps_initialized)
1828 return pmu->nr_caps;
1832 if (!perf_pmu__pathname_scnprintf(caps_path, sizeof(caps_path), pmu->name, "caps"))
1835 if (stat(caps_path, &st) < 0) {
1836 pmu->caps_initialized = true;
1837 return 0; /* no error if caps does not exist */
1840 caps_dir = opendir(caps_path);
1844 caps_fd = dirfd(caps_dir);
1846 while ((evt_ent = readdir(caps_dir)) != NULL) {
1847 char *name = evt_ent->d_name;
1852 if (!strcmp(name, ".") || !strcmp(name, ".."))
1855 fd = openat(caps_fd, name, O_RDONLY);
1858 file = fdopen(fd, "r");
1864 if (!fgets(value, sizeof(value), file) ||
1865 (perf_pmu__new_caps(&pmu->caps, name, value) < 0)) {
1876 pmu->caps_initialized = true;
1877 return pmu->nr_caps;
1880 static void perf_pmu__compute_config_masks(struct perf_pmu *pmu)
1882 struct perf_pmu_format *format;
1884 if (pmu->config_masks_computed)
1887 list_for_each_entry(format, &pmu->format, list) {
1891 if (format->value >= PERF_PMU_FORMAT_VALUE_CONFIG_END)
1894 pmu->config_masks_present = true;
1895 mask = &pmu->config_masks[format->value];
1897 for_each_set_bit(i, format->bits, PERF_PMU_FORMAT_BITS)
1900 pmu->config_masks_computed = true;
1903 void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config,
1904 const char *name, int config_num,
1905 const char *config_name)
1910 perf_pmu__compute_config_masks(pmu);
1913 * Kernel doesn't export any valid format bits.
1915 if (!pmu->config_masks_present)
1918 bits = config & ~pmu->config_masks[config_num];
1922 bitmap_scnprintf((unsigned long *)&bits, sizeof(bits) * 8, buf, sizeof(buf));
1924 pr_warning("WARNING: event '%s' not valid (bits %s of %s "
1925 "'%llx' not supported by kernel)!\n",
1926 name ?: "N/A", buf, config_name, config);
1929 int perf_pmu__match(char *pattern, char *name, char *tok)
1934 if (fnmatch(pattern, name, 0))
1937 if (tok && !perf_pmu__match_ignoring_suffix(name, tok))
1943 double __weak perf_pmu__cpu_slots_per_cycle(void)
1948 int perf_pmu__event_source_devices_scnprintf(char *pathname, size_t size)
1950 const char *sysfs = sysfs__mountpoint();
1954 return scnprintf(pathname, size, "%s/bus/event_source/devices/", sysfs);
1957 int perf_pmu__event_source_devices_fd(void)
1959 char path[PATH_MAX];
1960 const char *sysfs = sysfs__mountpoint();
1965 scnprintf(path, sizeof(path), "%s/bus/event_source/devices/", sysfs);
1966 return open(path, O_DIRECTORY);
1970 * Fill 'buf' with the path to a file or folder in 'pmu_name' in
1971 * sysfs. For example if pmu_name = "cs_etm" and 'filename' = "format"
1972 * then pathname will be filled with
1973 * "/sys/bus/event_source/devices/cs_etm/format"
1975 * Return 0 if the sysfs mountpoint couldn't be found, if no characters were
1976 * written or if the buffer size is exceeded.
1978 int perf_pmu__pathname_scnprintf(char *buf, size_t size,
1979 const char *pmu_name, const char *filename)
1983 len = perf_pmu__event_source_devices_scnprintf(buf, size);
1984 if (!len || (len + strlen(pmu_name) + strlen(filename) + 1) >= size)
1987 return scnprintf(buf + len, size - len, "%s/%s", pmu_name, filename);
1990 int perf_pmu__pathname_fd(int dirfd, const char *pmu_name, const char *filename, int flags)
1992 char path[PATH_MAX];
1994 scnprintf(path, sizeof(path), "%s/%s", pmu_name, filename);
1995 return openat(dirfd, path, flags);
1998 void perf_pmu__delete(struct perf_pmu *pmu)
2000 perf_pmu__del_formats(&pmu->format);
2001 perf_pmu__del_aliases(pmu);
2002 perf_pmu__del_caps(pmu);
2004 perf_cpu_map__put(pmu->cpus);
2006 zfree(&pmu->default_config);
2008 zfree(&pmu->alias_name);
2012 struct perf_pmu *pmu__find_core_pmu(void)
2014 struct perf_pmu *pmu = NULL;
2016 while ((pmu = perf_pmus__scan_core(pmu))) {
2018 * The cpumap should cover all CPUs. Otherwise, some CPUs may
2019 * not support some events or have different event IDs.
2021 if (RC_CHK_ACCESS(pmu->cpus)->nr != cpu__max_cpu().cpu)