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;
35 * struct perf_pmu_format - Values from a format file read from
36 * <sysfs>/devices/cpu/format/ held in struct perf_pmu.
38 * For example, the contents of <sysfs>/devices/cpu/format/event may be
39 * "config:0-7" and will be represented here as name="event",
40 * value=PERF_PMU_FORMAT_VALUE_CONFIG and bits 0 to 7 will be set.
42 struct perf_pmu_format {
43 /** @name: The modifier/file name. */
46 * @value : Which config value the format relates to. Supported values
47 * are from PERF_PMU_FORMAT_VALUE_CONFIG to
48 * PERF_PMU_FORMAT_VALUE_CONFIG_END.
51 /** @bits: Which config bits are set by this format value. */
52 DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
53 /** @list: Element on list within struct perf_pmu. */
54 struct list_head list;
58 * Parse & process all the sysfs attributes located under
59 * the directory specified in 'dir' parameter.
61 int perf_pmu__format_parse(struct perf_pmu *pmu, int dirfd)
63 struct dirent *evt_ent;
67 format_dir = fdopendir(dirfd);
71 while (!ret && (evt_ent = readdir(format_dir))) {
72 char *name = evt_ent->d_name;
77 if (!strcmp(name, ".") || !strcmp(name, ".."))
82 fd = openat(dirfd, name, O_RDONLY);
86 file = fdopen(fd, "r");
92 ret = perf_pmu_lex_init(&scanner);
98 perf_pmu_set_in(file, scanner);
99 ret = perf_pmu_parse(&pmu->format, name, scanner);
100 perf_pmu_lex_destroy(scanner);
104 closedir(format_dir);
109 * Reading/parsing the default pmu format definition, which should be
111 * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
113 static int pmu_format(struct perf_pmu *pmu, int dirfd, const char *name)
117 fd = perf_pmu__pathname_fd(dirfd, name, "format", O_DIRECTORY);
121 /* it'll close the fd */
122 if (perf_pmu__format_parse(pmu, fd))
128 int perf_pmu__convert_scale(const char *scale, char **end, double *sval)
134 * save current locale
136 lc = setlocale(LC_NUMERIC, NULL);
139 * The lc string may be allocated in static storage,
140 * so get a dynamic copy to make it survive setlocale
150 * force to C locale to ensure kernel
151 * scale string is converted correctly.
152 * kernel uses default C locale.
154 setlocale(LC_NUMERIC, "C");
156 *sval = strtod(scale, end);
160 setlocale(LC_NUMERIC, lc);
165 static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, int dirfd, char *name)
173 scnprintf(path, PATH_MAX, "%s.scale", name);
175 fd = openat(dirfd, path, O_RDONLY);
179 if (fstat(fd, &st) < 0)
182 sret = read(fd, scale, sizeof(scale)-1);
186 if (scale[sret - 1] == '\n')
187 scale[sret - 1] = '\0';
191 ret = perf_pmu__convert_scale(scale, NULL, &alias->scale);
197 static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, int dirfd, char *name)
203 scnprintf(path, PATH_MAX, "%s.unit", name);
205 fd = openat(dirfd, path, O_RDONLY);
209 sret = read(fd, alias->unit, UNIT_MAX_LEN);
215 if (alias->unit[sret - 1] == '\n')
216 alias->unit[sret - 1] = '\0';
218 alias->unit[sret] = '\0';
223 alias->unit[0] = '\0';
228 perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, int dirfd, char *name)
233 scnprintf(path, PATH_MAX, "%s.per-pkg", name);
235 fd = openat(dirfd, path, O_RDONLY);
241 alias->per_pkg = true;
245 static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias,
246 int dirfd, char *name)
251 scnprintf(path, PATH_MAX, "%s.snapshot", name);
253 fd = openat(dirfd, path, O_RDONLY);
257 alias->snapshot = true;
262 static void perf_pmu_assign_str(char *name, const char *field, char **old_str,
268 if (*new_str) { /* Have new string, check with old */
269 if (strcasecmp(*old_str, *new_str))
270 pr_debug("alias %s differs in field '%s'\n",
273 } else /* Nothing new --> keep old string */
280 static void perf_pmu_update_alias(struct perf_pmu_alias *old,
281 struct perf_pmu_alias *newalias)
283 perf_pmu_assign_str(old->name, "desc", &old->desc, &newalias->desc);
284 perf_pmu_assign_str(old->name, "long_desc", &old->long_desc,
285 &newalias->long_desc);
286 perf_pmu_assign_str(old->name, "topic", &old->topic, &newalias->topic);
287 perf_pmu_assign_str(old->name, "value", &old->str, &newalias->str);
288 old->scale = newalias->scale;
289 old->per_pkg = newalias->per_pkg;
290 old->snapshot = newalias->snapshot;
291 memcpy(old->unit, newalias->unit, sizeof(old->unit));
294 /* Delete an alias entry. */
295 void perf_pmu_free_alias(struct perf_pmu_alias *newalias)
297 zfree(&newalias->name);
298 zfree(&newalias->desc);
299 zfree(&newalias->long_desc);
300 zfree(&newalias->topic);
301 zfree(&newalias->str);
302 zfree(&newalias->pmu_name);
303 parse_events_terms__purge(&newalias->terms);
307 static void perf_pmu__del_aliases(struct perf_pmu *pmu)
309 struct perf_pmu_alias *alias, *tmp;
311 list_for_each_entry_safe(alias, tmp, &pmu->aliases, list) {
312 list_del(&alias->list);
313 perf_pmu_free_alias(alias);
317 /* Merge an alias, search in alias list. If this name is already
318 * present merge both of them to combine all information.
320 static bool perf_pmu_merge_alias(struct perf_pmu_alias *newalias,
321 struct list_head *alist)
323 struct perf_pmu_alias *a;
325 list_for_each_entry(a, alist, list) {
326 if (!strcasecmp(newalias->name, a->name)) {
327 if (newalias->pmu_name && a->pmu_name &&
328 !strcasecmp(newalias->pmu_name, a->pmu_name)) {
331 perf_pmu_update_alias(a, newalias);
332 perf_pmu_free_alias(newalias);
339 static int __perf_pmu__new_alias(struct list_head *list, int dirfd, char *name,
340 char *desc, char *val, const struct pmu_event *pe)
342 struct parse_events_term *term;
343 struct perf_pmu_alias *alias;
346 const char *long_desc = NULL, *topic = NULL, *unit = NULL, *pmu_name = NULL;
347 bool deprecated = false, perpkg = false;
350 long_desc = pe->long_desc;
354 deprecated = pe->deprecated;
358 alias = malloc(sizeof(*alias));
362 INIT_LIST_HEAD(&alias->terms);
364 alias->unit[0] = '\0';
365 alias->per_pkg = perpkg;
366 alias->snapshot = false;
367 alias->deprecated = deprecated;
369 ret = parse_events_terms(&alias->terms, val);
371 pr_err("Cannot parse alias %s: %d\n", val, ret);
376 /* Scan event and remove leading zeroes, spaces, newlines, some
377 * platforms have terms specified as
378 * event=0x0091 (read from files ../<PMU>/events/<FILE>
379 * and terms specified as event=0x91 (read from JSON files).
381 * Rebuild string to make alias->str member comparable.
383 memset(newval, 0, sizeof(newval));
385 list_for_each_entry(term, &alias->terms, list) {
387 ret += scnprintf(newval + ret, sizeof(newval) - ret,
389 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM)
390 ret += scnprintf(newval + ret, sizeof(newval) - ret,
391 "%s=%#x", term->config, term->val.num);
392 else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
393 ret += scnprintf(newval + ret, sizeof(newval) - ret,
394 "%s=%s", term->config, term->val.str);
397 alias->name = strdup(name);
400 * load unit name and scale if available
402 perf_pmu__parse_unit(alias, dirfd, name);
403 perf_pmu__parse_scale(alias, dirfd, name);
404 perf_pmu__parse_per_pkg(alias, dirfd, name);
405 perf_pmu__parse_snapshot(alias, dirfd, name);
408 alias->desc = desc ? strdup(desc) : NULL;
409 alias->long_desc = long_desc ? strdup(long_desc) :
410 desc ? strdup(desc) : NULL;
411 alias->topic = topic ? strdup(topic) : NULL;
413 if (perf_pmu__convert_scale(unit, (char **)&unit, &alias->scale) < 0)
415 snprintf(alias->unit, sizeof(alias->unit), "%s", unit);
417 alias->str = strdup(newval);
418 alias->pmu_name = pmu_name ? strdup(pmu_name) : NULL;
420 if (!perf_pmu_merge_alias(alias, list))
421 list_add_tail(&alias->list, list);
426 static int perf_pmu__new_alias(struct list_head *list, int dirfd, char *name, FILE *file)
431 ret = fread(buf, 1, sizeof(buf), file);
437 /* Remove trailing newline from sysfs file */
440 return __perf_pmu__new_alias(list, dirfd, name, NULL, buf, NULL);
443 static inline bool pmu_alias_info_file(char *name)
448 if (len > 5 && !strcmp(name + len - 5, ".unit"))
450 if (len > 6 && !strcmp(name + len - 6, ".scale"))
452 if (len > 8 && !strcmp(name + len - 8, ".per-pkg"))
454 if (len > 9 && !strcmp(name + len - 9, ".snapshot"))
461 * Process all the sysfs attributes located under the directory
462 * specified in 'dir' parameter.
464 static int pmu_aliases_parse(int dirfd, struct list_head *head)
466 struct dirent *evt_ent;
470 event_dir = fdopendir(dirfd);
474 while ((evt_ent = readdir(event_dir))) {
475 char *name = evt_ent->d_name;
478 if (!strcmp(name, ".") || !strcmp(name, ".."))
482 * skip info files parsed in perf_pmu__new_alias()
484 if (pmu_alias_info_file(name))
487 fd = openat(dirfd, name, O_RDONLY);
489 pr_debug("Cannot open %s\n", name);
492 file = fdopen(fd, "r");
498 if (perf_pmu__new_alias(head, dirfd, name, file) < 0)
499 pr_debug("Cannot set up %s\n", name);
508 * Reading the pmu event aliases definition, which should be located at:
509 * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
511 static int pmu_aliases(struct perf_pmu *pmu, int dirfd, const char *name)
515 fd = perf_pmu__pathname_fd(dirfd, name, "events", O_DIRECTORY);
519 /* it'll close the fd */
520 if (pmu_aliases_parse(fd, &pmu->aliases))
526 static int pmu_alias_terms(struct perf_pmu_alias *alias,
527 struct list_head *terms)
529 struct parse_events_term *term, *cloned;
533 list_for_each_entry(term, &alias->terms, list) {
534 ret = parse_events_term__clone(&cloned, term);
536 parse_events_terms__purge(&list);
540 * Weak terms don't override command line options,
541 * which we don't want for implicit terms in aliases.
544 list_add_tail(&cloned->list, &list);
546 list_splice(&list, terms);
551 * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64)
552 * may have a "cpus" file.
554 static struct perf_cpu_map *pmu_cpumask(int dirfd, const char *name, bool is_core)
556 struct perf_cpu_map *cpus;
557 const char *templates[] = {
562 const char **template;
563 char pmu_name[PATH_MAX];
564 struct perf_pmu pmu = {.name = pmu_name};
567 strlcpy(pmu_name, name, sizeof(pmu_name));
568 for (template = templates; *template; template++) {
569 file = perf_pmu__open_file_at(&pmu, dirfd, *template);
572 cpus = perf_cpu_map__read(file);
578 /* Nothing found, for core PMUs assume this means all CPUs. */
579 return is_core ? perf_cpu_map__get(cpu_map__online()) : NULL;
582 static bool pmu_is_uncore(int dirfd, const char *name)
586 fd = perf_pmu__pathname_fd(dirfd, name, "cpumask", O_PATH);
594 static char *pmu_id(const char *name)
596 char path[PATH_MAX], *str;
599 perf_pmu__pathname_scnprintf(path, sizeof(path), name, "identifier");
601 if (filename__read_str(path, &str, &len) < 0)
604 str[len - 1] = 0; /* remove line feed */
610 * is_sysfs_pmu_core() - PMU CORE devices have different name other than cpu in
611 * sysfs on some platforms like ARM or Intel hybrid. Looking for
612 * possible the cpus file in sysfs files to identify whether this is a
614 * @name: The PMU name such as "cpu_atom".
616 static int is_sysfs_pmu_core(const char *name)
620 if (!perf_pmu__pathname_scnprintf(path, sizeof(path), name, "cpus"))
622 return file_available(path);
625 char *perf_pmu__getcpuid(struct perf_pmu *pmu)
630 cpuid = getenv("PERF_CPUID");
632 cpuid = strdup(cpuid);
634 cpuid = get_cpuid_str(pmu);
639 pr_debug("Using CPUID %s\n", cpuid);
645 __weak const struct pmu_events_table *pmu_events_table__find(void)
647 return perf_pmu__find_events_table(NULL);
650 __weak const struct pmu_metrics_table *pmu_metrics_table__find(void)
652 return perf_pmu__find_metrics_table(NULL);
656 * perf_pmu__match_ignoring_suffix - Does the pmu_name match tok ignoring any
657 * trailing suffix? The Suffix must be in form
658 * tok_{digits}, or tok{digits}.
659 * @pmu_name: The pmu_name with possible suffix.
660 * @tok: The possible match to pmu_name without suffix.
662 static bool perf_pmu__match_ignoring_suffix(const char *pmu_name, const char *tok)
666 if (strncmp(pmu_name, tok, strlen(tok)))
669 p = pmu_name + strlen(tok);
676 /* Ensure we end in a number */
688 * pmu_uncore_alias_match - does name match the PMU name?
689 * @pmu_name: the json struct pmu_event name. This may lack a suffix (which
690 * matches) or be of the form "socket,pmuname" which will match
691 * "socketX_pmunameY".
692 * @name: a real full PMU name as from sysfs.
694 static bool pmu_uncore_alias_match(const char *pmu_name, const char *name)
696 char *tmp = NULL, *tok, *str;
699 if (strchr(pmu_name, ',') == NULL)
700 return perf_pmu__match_ignoring_suffix(name, pmu_name);
702 str = strdup(pmu_name);
707 * uncore alias may be from different PMU with common prefix
709 tok = strtok_r(str, ",", &tmp);
710 if (strncmp(pmu_name, tok, strlen(tok))) {
716 * Match more complex aliases where the alias name is a comma-delimited
717 * list of tokens, orderly contained in the matching PMU name.
719 * Example: For alias "socket,pmuname" and PMU "socketX_pmunameY", we
720 * match "socket" in "socketX_pmunameY" and then "pmuname" in
724 char *next_tok = strtok_r(NULL, ",", &tmp);
726 name = strstr(name, tok);
728 (!next_tok && !perf_pmu__match_ignoring_suffix(name, tok))) {
744 struct pmu_add_cpu_aliases_map_data {
745 /* List being added to. */
746 struct list_head *head;
747 /* If a pmu_event lacks a given PMU the default used. */
748 char *default_pmu_name;
749 /* The PMU that we're searching for events for. */
750 struct perf_pmu *pmu;
753 static int pmu_add_cpu_aliases_map_callback(const struct pmu_event *pe,
754 const struct pmu_events_table *table __maybe_unused,
757 struct pmu_add_cpu_aliases_map_data *data = vdata;
758 const char *pname = pe->pmu ?: data->default_pmu_name;
760 if (!strcmp(pname, data->pmu->name) ||
761 (data->pmu->is_uncore && pmu_uncore_alias_match(pname, data->pmu->name))) {
762 /* need type casts to override 'const' */
763 __perf_pmu__new_alias(data->head, -1, (char *)pe->name, (char *)pe->desc,
764 (char *)pe->event, pe);
770 * From the pmu_events_table, find the events that correspond to the given
771 * PMU and add them to the list 'head'.
773 void pmu_add_cpu_aliases_table(struct perf_pmu *pmu, const struct pmu_events_table *table)
775 struct pmu_add_cpu_aliases_map_data data = {
776 .head = &pmu->aliases,
777 .default_pmu_name = perf_pmus__default_pmu_name(),
781 pmu_events_table_for_each_event(table, pmu_add_cpu_aliases_map_callback, &data);
782 free(data.default_pmu_name);
785 static void pmu_add_cpu_aliases(struct perf_pmu *pmu)
787 const struct pmu_events_table *table;
789 table = perf_pmu__find_events_table(pmu);
793 pmu_add_cpu_aliases_table(pmu, table);
796 struct pmu_sys_event_iter_data {
797 struct list_head *head;
798 struct perf_pmu *pmu;
801 static int pmu_add_sys_aliases_iter_fn(const struct pmu_event *pe,
802 const struct pmu_events_table *table __maybe_unused,
805 struct pmu_sys_event_iter_data *idata = data;
806 struct perf_pmu *pmu = idata->pmu;
808 if (!pe->compat || !pe->pmu)
811 if (!strcmp(pmu->id, pe->compat) &&
812 pmu_uncore_alias_match(pe->pmu, pmu->name)) {
813 __perf_pmu__new_alias(idata->head, -1,
823 void pmu_add_sys_aliases(struct perf_pmu *pmu)
825 struct pmu_sys_event_iter_data idata = {
826 .head = &pmu->aliases,
833 pmu_for_each_sys_event(pmu_add_sys_aliases_iter_fn, &idata);
836 struct perf_event_attr * __weak
837 perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
843 pmu_find_real_name(const char *name)
849 pmu_find_alias_name(const char *name __maybe_unused)
854 static int pmu_max_precise(int dirfd, struct perf_pmu *pmu)
856 int max_precise = -1;
858 perf_pmu__scan_file_at(pmu, dirfd, "caps/max_precise", "%d", &max_precise);
862 struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char *lookup_name)
864 struct perf_pmu *pmu;
866 char *name = pmu_find_real_name(lookup_name);
869 pmu = zalloc(sizeof(*pmu));
873 INIT_LIST_HEAD(&pmu->format);
874 INIT_LIST_HEAD(&pmu->aliases);
875 INIT_LIST_HEAD(&pmu->caps);
877 * The pmu data we store & need consists of the pmu
878 * type value and format definitions. Load both right
881 if (pmu_format(pmu, dirfd, name)) {
886 * Check the aliases first to avoid unnecessary work.
888 if (pmu_aliases(pmu, dirfd, name)) {
892 pmu->is_core = is_pmu_core(name);
893 pmu->cpus = pmu_cpumask(dirfd, name, pmu->is_core);
894 pmu->name = strdup(name);
898 /* Read type, and ensure that type value is successfully assigned (return 1) */
899 if (perf_pmu__scan_file_at(pmu, dirfd, "type", "%u", &type) != 1)
902 alias_name = pmu_find_alias_name(name);
904 pmu->alias_name = strdup(alias_name);
905 if (!pmu->alias_name)
910 pmu->is_uncore = pmu_is_uncore(dirfd, name);
912 pmu->id = pmu_id(name);
913 pmu->max_precise = pmu_max_precise(dirfd, pmu);
914 pmu_add_cpu_aliases(pmu);
915 pmu_add_sys_aliases(pmu);
916 list_add_tail(&pmu->list, pmus);
918 pmu->default_config = perf_pmu__get_default_config(pmu);
927 /* Creates the PMU when sysfs scanning fails. */
928 struct perf_pmu *perf_pmu__create_placeholder_core_pmu(struct list_head *core_pmus)
930 struct perf_pmu *pmu = zalloc(sizeof(*pmu));
935 pmu->name = strdup("cpu");
942 pmu->type = PERF_TYPE_RAW;
943 pmu->cpus = cpu_map__online();
945 INIT_LIST_HEAD(&pmu->format);
946 INIT_LIST_HEAD(&pmu->aliases);
947 INIT_LIST_HEAD(&pmu->caps);
948 list_add_tail(&pmu->list, core_pmus);
952 void perf_pmu__warn_invalid_formats(struct perf_pmu *pmu)
954 struct perf_pmu_format *format;
956 if (pmu->formats_checked)
959 pmu->formats_checked = true;
961 /* fake pmu doesn't have format list */
962 if (pmu == &perf_pmu__fake)
965 list_for_each_entry(format, &pmu->format, list)
966 if (format->value >= PERF_PMU_FORMAT_VALUE_CONFIG_END) {
967 pr_warning("WARNING: '%s' format '%s' requires 'perf_event_attr::config%d'"
968 "which is not supported by this version of perf!\n",
969 pmu->name, format->name, format->value);
974 bool evsel__is_aux_event(const struct evsel *evsel)
976 struct perf_pmu *pmu = evsel__find_pmu(evsel);
978 return pmu && pmu->auxtrace;
982 * Set @config_name to @val as long as the user hasn't already set or cleared it
983 * by passing a config term on the command line.
985 * @val is the value to put into the bits specified by @config_name rather than
986 * the bit pattern. It is shifted into position by this function, so to set
987 * something to true, pass 1 for val rather than a pre shifted value.
989 #define field_prep(_mask, _val) (((_val) << (ffsll(_mask) - 1)) & (_mask))
990 void evsel__set_config_if_unset(struct perf_pmu *pmu, struct evsel *evsel,
991 const char *config_name, u64 val)
993 u64 user_bits = 0, bits;
994 struct evsel_config_term *term = evsel__get_config_term(evsel, CFG_CHG);
997 user_bits = term->val.cfg_chg;
999 bits = perf_pmu__format_bits(pmu, config_name);
1001 /* Do nothing if the user changed the value */
1002 if (bits & user_bits)
1005 /* Otherwise replace it */
1006 evsel->core.attr.config &= ~bits;
1007 evsel->core.attr.config |= field_prep(bits, val);
1010 static struct perf_pmu_format *
1011 pmu_find_format(struct list_head *formats, const char *name)
1013 struct perf_pmu_format *format;
1015 list_for_each_entry(format, formats, list)
1016 if (!strcmp(format->name, name))
1022 __u64 perf_pmu__format_bits(struct perf_pmu *pmu, const char *name)
1024 struct perf_pmu_format *format = pmu_find_format(&pmu->format, name);
1031 for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
1032 bits |= 1ULL << fbit;
1037 int perf_pmu__format_type(struct perf_pmu *pmu, const char *name)
1039 struct perf_pmu_format *format = pmu_find_format(&pmu->format, name);
1044 return format->value;
1048 * Sets value based on the format definition (format parameter)
1049 * and unformatted value (value parameter).
1051 static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
1054 unsigned long fbit, vbit;
1056 for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
1058 if (!test_bit(fbit, format))
1061 if (value & (1llu << vbit++))
1062 *v |= (1llu << fbit);
1064 *v &= ~(1llu << fbit);
1068 static __u64 pmu_format_max_value(const unsigned long *format)
1072 w = bitmap_weight(format, PERF_PMU_FORMAT_BITS);
1076 return (1ULL << w) - 1;
1081 * Term is a string term, and might be a param-term. Try to look up it's value
1082 * in the remaining terms.
1083 * - We have a term like "base-or-format-term=param-term",
1084 * - We need to find the value supplied for "param-term" (with param-term named
1085 * in a config string) later on in the term list.
1087 static int pmu_resolve_param_term(struct parse_events_term *term,
1088 struct list_head *head_terms,
1091 struct parse_events_term *t;
1093 list_for_each_entry(t, head_terms, list) {
1094 if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM &&
1095 t->config && !strcmp(t->config, term->config)) {
1097 *value = t->val.num;
1103 printf("Required parameter '%s' not specified\n", term->config);
1108 static char *pmu_formats_string(struct list_head *formats)
1110 struct perf_pmu_format *format;
1112 struct strbuf buf = STRBUF_INIT;
1118 /* sysfs exported terms */
1119 list_for_each_entry(format, formats, list)
1120 if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
1123 str = strbuf_detach(&buf, NULL);
1125 strbuf_release(&buf);
1131 * Setup one of config[12] attr members based on the
1132 * user input data - term parameter.
1134 static int pmu_config_term(struct perf_pmu *pmu,
1135 struct perf_event_attr *attr,
1136 struct parse_events_term *term,
1137 struct list_head *head_terms,
1138 bool zero, struct parse_events_error *err)
1140 struct perf_pmu_format *format;
1145 * If this is a parameter we've already used for parameterized-eval,
1146 * skip it in normal eval.
1152 * Hardcoded terms should be already in, so nothing
1153 * to be done for them.
1155 if (parse_events__is_hardcoded_term(term))
1158 format = pmu_find_format(&pmu->format, term->config);
1160 char *pmu_term = pmu_formats_string(&pmu->format);
1164 if (asprintf(&unknown_term,
1165 "unknown term '%s' for pmu '%s'",
1166 term->config, pmu->name) < 0)
1167 unknown_term = NULL;
1168 help_msg = parse_events_formats_error_string(pmu_term);
1170 parse_events_error__handle(err, term->err_term,
1174 pr_debug("%s (%s)\n", unknown_term, help_msg);
1181 switch (format->value) {
1182 case PERF_PMU_FORMAT_VALUE_CONFIG:
1185 case PERF_PMU_FORMAT_VALUE_CONFIG1:
1186 vp = &attr->config1;
1188 case PERF_PMU_FORMAT_VALUE_CONFIG2:
1189 vp = &attr->config2;
1191 case PERF_PMU_FORMAT_VALUE_CONFIG3:
1192 vp = &attr->config3;
1199 * Either directly use a numeric term, or try to translate string terms
1200 * using event parameters.
1202 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1203 if (term->no_value &&
1204 bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) {
1206 parse_events_error__handle(err, term->err_val,
1207 strdup("no value assigned for term"),
1213 val = term->val.num;
1214 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1215 if (strcmp(term->val.str, "?")) {
1217 pr_info("Invalid sysfs entry %s=%s\n",
1218 term->config, term->val.str);
1221 parse_events_error__handle(err, term->err_val,
1222 strdup("expected numeric value"),
1228 if (pmu_resolve_param_term(term, head_terms, &val))
1233 max_val = pmu_format_max_value(format->bits);
1234 if (val > max_val) {
1238 parse_events_error__handle(err, term->err_val,
1240 "value too big for format, maximum is %llu",
1241 (unsigned long long)max_val) < 0
1242 ? strdup("value too big for format")
1248 * Assume we don't care if !err, in which case the value will be
1249 * silently truncated.
1253 pmu_format_value(format->bits, val, vp, zero);
1257 int perf_pmu__config_terms(struct perf_pmu *pmu,
1258 struct perf_event_attr *attr,
1259 struct list_head *head_terms,
1260 bool zero, struct parse_events_error *err)
1262 struct parse_events_term *term;
1264 list_for_each_entry(term, head_terms, list) {
1265 if (pmu_config_term(pmu, attr, term, head_terms, zero, err))
1273 * Configures event's 'attr' parameter based on the:
1274 * 1) users input - specified in terms parameter
1275 * 2) pmu format definitions - specified by pmu parameter
1277 int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
1278 struct list_head *head_terms,
1279 struct parse_events_error *err)
1281 bool zero = !!pmu->default_config;
1283 return perf_pmu__config_terms(pmu, attr, head_terms, zero, err);
1286 static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
1287 struct parse_events_term *term)
1289 struct perf_pmu_alias *alias;
1292 if (parse_events__is_hardcoded_term(term))
1295 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1296 if (term->val.num != 1)
1298 if (pmu_find_format(&pmu->format, term->config))
1300 name = term->config;
1301 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1302 if (strcasecmp(term->config, "event"))
1304 name = term->val.str;
1309 list_for_each_entry(alias, &pmu->aliases, list) {
1310 if (!strcasecmp(alias->name, name))
1317 static int check_info_data(struct perf_pmu_alias *alias,
1318 struct perf_pmu_info *info)
1321 * Only one term in event definition can
1322 * define unit, scale and snapshot, fail
1323 * if there's more than one.
1325 if ((info->unit && alias->unit[0]) ||
1326 (info->scale && alias->scale) ||
1327 (info->snapshot && alias->snapshot))
1331 info->unit = alias->unit;
1334 info->scale = alias->scale;
1336 if (alias->snapshot)
1337 info->snapshot = alias->snapshot;
1343 * Find alias in the terms list and replace it with the terms
1344 * defined for the alias
1346 int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
1347 struct perf_pmu_info *info)
1349 struct parse_events_term *term, *h;
1350 struct perf_pmu_alias *alias;
1353 info->per_pkg = false;
1356 * Mark unit and scale as not set
1357 * (different from default values, see below)
1361 info->snapshot = false;
1363 list_for_each_entry_safe(term, h, head_terms, list) {
1364 alias = pmu_find_alias(pmu, term);
1367 ret = pmu_alias_terms(alias, &term->list);
1371 ret = check_info_data(alias, info);
1376 info->per_pkg = true;
1378 list_del_init(&term->list);
1379 parse_events_term__delete(term);
1383 * if no unit or scale found in aliases, then
1384 * set defaults as for evsel
1385 * unit cannot left to NULL
1387 if (info->unit == NULL)
1390 if (info->scale == 0.0)
1396 struct perf_pmu_alias *perf_pmu__find_alias(struct perf_pmu *pmu, const char *event)
1398 struct perf_pmu_alias *alias;
1400 list_for_each_entry(alias, &pmu->aliases, list)
1401 if (!strcmp(event, alias->name))
1407 int perf_pmu__new_format(struct list_head *list, char *name,
1408 int config, unsigned long *bits)
1410 struct perf_pmu_format *format;
1412 format = zalloc(sizeof(*format));
1416 format->name = strdup(name);
1417 format->value = config;
1418 memcpy(format->bits, bits, sizeof(format->bits));
1420 list_add_tail(&format->list, list);
1424 static void perf_pmu__del_formats(struct list_head *formats)
1426 struct perf_pmu_format *fmt, *tmp;
1428 list_for_each_entry_safe(fmt, tmp, formats, list) {
1429 list_del(&fmt->list);
1435 bool perf_pmu__has_format(const struct perf_pmu *pmu, const char *name)
1437 struct perf_pmu_format *format;
1439 list_for_each_entry(format, &pmu->format, list) {
1440 if (!strcmp(format->name, name))
1446 bool is_pmu_core(const char *name)
1448 return !strcmp(name, "cpu") || !strcmp(name, "cpum_cf") || is_sysfs_pmu_core(name);
1451 bool perf_pmu__supports_legacy_cache(const struct perf_pmu *pmu)
1453 return pmu->is_core;
1456 bool perf_pmu__auto_merge_stats(const struct perf_pmu *pmu)
1458 return !pmu->is_core || perf_pmus__num_core_pmus() == 1;
1461 bool perf_pmu__have_event(const struct perf_pmu *pmu, const char *name)
1463 struct perf_pmu_alias *alias;
1465 list_for_each_entry(alias, &pmu->aliases, list) {
1466 if (!strcmp(alias->name, name))
1472 bool perf_pmu__is_software(const struct perf_pmu *pmu)
1474 if (pmu->is_core || pmu->is_uncore || pmu->auxtrace)
1476 switch (pmu->type) {
1477 case PERF_TYPE_HARDWARE: return false;
1478 case PERF_TYPE_SOFTWARE: return true;
1479 case PERF_TYPE_TRACEPOINT: return true;
1480 case PERF_TYPE_HW_CACHE: return false;
1481 case PERF_TYPE_RAW: return false;
1482 case PERF_TYPE_BREAKPOINT: return true;
1485 return !strcmp(pmu->name, "kprobe") || !strcmp(pmu->name, "uprobe");
1488 FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
1490 char path[PATH_MAX];
1492 if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, name) ||
1493 !file_available(path))
1496 return fopen(path, "r");
1499 FILE *perf_pmu__open_file_at(struct perf_pmu *pmu, int dirfd, const char *name)
1503 fd = perf_pmu__pathname_fd(dirfd, pmu->name, name, O_RDONLY);
1507 return fdopen(fd, "r");
1510 int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
1517 va_start(args, fmt);
1518 file = perf_pmu__open_file(pmu, name);
1520 ret = vfscanf(file, fmt, args);
1527 int perf_pmu__scan_file_at(struct perf_pmu *pmu, int dirfd, const char *name,
1528 const char *fmt, ...)
1534 va_start(args, fmt);
1535 file = perf_pmu__open_file_at(pmu, dirfd, name);
1537 ret = vfscanf(file, fmt, args);
1544 bool perf_pmu__file_exists(struct perf_pmu *pmu, const char *name)
1546 char path[PATH_MAX];
1548 if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, name))
1551 return file_available(path);
1554 static int perf_pmu__new_caps(struct list_head *list, char *name, char *value)
1556 struct perf_pmu_caps *caps = zalloc(sizeof(*caps));
1561 caps->name = strdup(name);
1564 caps->value = strndup(value, strlen(value) - 1);
1567 list_add_tail(&caps->list, list);
1578 static void perf_pmu__del_caps(struct perf_pmu *pmu)
1580 struct perf_pmu_caps *caps, *tmp;
1582 list_for_each_entry_safe(caps, tmp, &pmu->caps, list) {
1583 list_del(&caps->list);
1585 zfree(&caps->value);
1591 * Reading/parsing the given pmu capabilities, which should be located at:
1592 * /sys/bus/event_source/devices/<dev>/caps as sysfs group attributes.
1593 * Return the number of capabilities
1595 int perf_pmu__caps_parse(struct perf_pmu *pmu)
1598 char caps_path[PATH_MAX];
1600 struct dirent *evt_ent;
1603 if (pmu->caps_initialized)
1604 return pmu->nr_caps;
1608 if (!perf_pmu__pathname_scnprintf(caps_path, sizeof(caps_path), pmu->name, "caps"))
1611 if (stat(caps_path, &st) < 0) {
1612 pmu->caps_initialized = true;
1613 return 0; /* no error if caps does not exist */
1616 caps_dir = opendir(caps_path);
1620 caps_fd = dirfd(caps_dir);
1622 while ((evt_ent = readdir(caps_dir)) != NULL) {
1623 char *name = evt_ent->d_name;
1628 if (!strcmp(name, ".") || !strcmp(name, ".."))
1631 fd = openat(caps_fd, name, O_RDONLY);
1634 file = fdopen(fd, "r");
1640 if (!fgets(value, sizeof(value), file) ||
1641 (perf_pmu__new_caps(&pmu->caps, name, value) < 0)) {
1652 pmu->caps_initialized = true;
1653 return pmu->nr_caps;
1656 static void perf_pmu__compute_config_masks(struct perf_pmu *pmu)
1658 struct perf_pmu_format *format;
1660 if (pmu->config_masks_computed)
1663 list_for_each_entry(format, &pmu->format, list) {
1667 if (format->value >= PERF_PMU_FORMAT_VALUE_CONFIG_END)
1670 pmu->config_masks_present = true;
1671 mask = &pmu->config_masks[format->value];
1673 for_each_set_bit(i, format->bits, PERF_PMU_FORMAT_BITS)
1676 pmu->config_masks_computed = true;
1679 void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config,
1680 const char *name, int config_num,
1681 const char *config_name)
1686 perf_pmu__compute_config_masks(pmu);
1689 * Kernel doesn't export any valid format bits.
1691 if (!pmu->config_masks_present)
1694 bits = config & ~pmu->config_masks[config_num];
1698 bitmap_scnprintf((unsigned long *)&bits, sizeof(bits) * 8, buf, sizeof(buf));
1700 pr_warning("WARNING: event '%s' not valid (bits %s of %s "
1701 "'%llx' not supported by kernel)!\n",
1702 name ?: "N/A", buf, config_name, config);
1705 int perf_pmu__match(char *pattern, char *name, char *tok)
1710 if (fnmatch(pattern, name, 0))
1713 if (tok && !perf_pmu__match_ignoring_suffix(name, tok))
1719 double __weak perf_pmu__cpu_slots_per_cycle(void)
1724 int perf_pmu__event_source_devices_scnprintf(char *pathname, size_t size)
1726 const char *sysfs = sysfs__mountpoint();
1730 return scnprintf(pathname, size, "%s/bus/event_source/devices/", sysfs);
1733 int perf_pmu__event_source_devices_fd(void)
1735 char path[PATH_MAX];
1736 const char *sysfs = sysfs__mountpoint();
1741 scnprintf(path, sizeof(path), "%s/bus/event_source/devices/", sysfs);
1742 return open(path, O_DIRECTORY);
1746 * Fill 'buf' with the path to a file or folder in 'pmu_name' in
1747 * sysfs. For example if pmu_name = "cs_etm" and 'filename' = "format"
1748 * then pathname will be filled with
1749 * "/sys/bus/event_source/devices/cs_etm/format"
1751 * Return 0 if the sysfs mountpoint couldn't be found, if no characters were
1752 * written or if the buffer size is exceeded.
1754 int perf_pmu__pathname_scnprintf(char *buf, size_t size,
1755 const char *pmu_name, const char *filename)
1759 len = perf_pmu__event_source_devices_scnprintf(buf, size);
1760 if (!len || (len + strlen(pmu_name) + strlen(filename) + 1) >= size)
1763 return scnprintf(buf + len, size - len, "%s/%s", pmu_name, filename);
1766 int perf_pmu__pathname_fd(int dirfd, const char *pmu_name, const char *filename, int flags)
1768 char path[PATH_MAX];
1770 scnprintf(path, sizeof(path), "%s/%s", pmu_name, filename);
1771 return openat(dirfd, path, flags);
1774 void perf_pmu__delete(struct perf_pmu *pmu)
1776 perf_pmu__del_formats(&pmu->format);
1777 perf_pmu__del_aliases(pmu);
1778 perf_pmu__del_caps(pmu);
1780 perf_cpu_map__put(pmu->cpus);
1782 zfree(&pmu->default_config);
1784 zfree(&pmu->alias_name);
1788 struct perf_pmu *pmu__find_core_pmu(void)
1790 struct perf_pmu *pmu = NULL;
1792 while ((pmu = perf_pmus__scan_core(pmu))) {
1794 * The cpumap should cover all CPUs. Otherwise, some CPUs may
1795 * not support some events or have different event IDs.
1797 if (RC_CHK_ACCESS(pmu->cpus)->nr != cpu__max_cpu().cpu)