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>
7 #include <subcmd/pager.h>
17 #include <api/fs/fs.h>
20 #include <perf/cpumap.h>
27 #include "parse-events.h"
28 #include "print-events.h"
33 #include "pmu-hybrid.h"
35 struct perf_pmu perf_pmu__fake;
38 * struct perf_pmu_format - Values from a format file read from
39 * <sysfs>/devices/cpu/format/ held in struct perf_pmu.
41 * For example, the contents of <sysfs>/devices/cpu/format/event may be
42 * "config:0-7" and will be represented here as name="event",
43 * value=PERF_PMU_FORMAT_VALUE_CONFIG and bits 0 to 7 will be set.
45 struct perf_pmu_format {
46 /** @name: The modifier/file name. */
49 * @value : Which config value the format relates to. Supported values
50 * are from PERF_PMU_FORMAT_VALUE_CONFIG to
51 * PERF_PMU_FORMAT_VALUE_CONFIG_END.
54 /** @bits: Which config bits are set by this format value. */
55 DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
56 /** @list: Element on list within struct perf_pmu. */
57 struct list_head list;
60 int perf_pmu_parse(struct list_head *list, char *name);
61 extern FILE *perf_pmu_in;
63 static bool hybrid_scanned;
66 * Parse & process all the sysfs attributes located under
67 * the directory specified in 'dir' parameter.
69 int perf_pmu__format_parse(char *dir, struct list_head *head)
71 struct dirent *evt_ent;
75 format_dir = opendir(dir);
79 while (!ret && (evt_ent = readdir(format_dir))) {
81 char *name = evt_ent->d_name;
84 if (!strcmp(name, ".") || !strcmp(name, ".."))
87 snprintf(path, PATH_MAX, "%s/%s", dir, name);
90 file = fopen(path, "r");
95 ret = perf_pmu_parse(head, name);
104 * Reading/parsing the default pmu format definition, which should be
106 * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
108 static int pmu_format(const char *name, struct list_head *format)
112 if (!perf_pmu__pathname_scnprintf(path, sizeof(path), name, "format"))
115 if (!file_available(path))
118 if (perf_pmu__format_parse(path, format))
124 int perf_pmu__convert_scale(const char *scale, char **end, double *sval)
130 * save current locale
132 lc = setlocale(LC_NUMERIC, NULL);
135 * The lc string may be allocated in static storage,
136 * so get a dynamic copy to make it survive setlocale
146 * force to C locale to ensure kernel
147 * scale string is converted correctly.
148 * kernel uses default C locale.
150 setlocale(LC_NUMERIC, "C");
152 *sval = strtod(scale, end);
156 setlocale(LC_NUMERIC, lc);
161 static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *name)
169 scnprintf(path, PATH_MAX, "%s/%s.scale", dir, name);
171 fd = open(path, O_RDONLY);
175 if (fstat(fd, &st) < 0)
178 sret = read(fd, scale, sizeof(scale)-1);
182 if (scale[sret - 1] == '\n')
183 scale[sret - 1] = '\0';
187 ret = perf_pmu__convert_scale(scale, NULL, &alias->scale);
193 static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *name)
199 scnprintf(path, PATH_MAX, "%s/%s.unit", dir, name);
201 fd = open(path, O_RDONLY);
205 sret = read(fd, alias->unit, UNIT_MAX_LEN);
211 if (alias->unit[sret - 1] == '\n')
212 alias->unit[sret - 1] = '\0';
214 alias->unit[sret] = '\0';
219 alias->unit[0] = '\0';
224 perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, char *dir, char *name)
229 scnprintf(path, PATH_MAX, "%s/%s.per-pkg", dir, name);
231 fd = open(path, O_RDONLY);
237 alias->per_pkg = true;
241 static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias,
242 char *dir, char *name)
247 scnprintf(path, PATH_MAX, "%s/%s.snapshot", dir, name);
249 fd = open(path, O_RDONLY);
253 alias->snapshot = true;
258 static void perf_pmu_assign_str(char *name, const char *field, char **old_str,
264 if (*new_str) { /* Have new string, check with old */
265 if (strcasecmp(*old_str, *new_str))
266 pr_debug("alias %s differs in field '%s'\n",
269 } else /* Nothing new --> keep old string */
276 static void perf_pmu_update_alias(struct perf_pmu_alias *old,
277 struct perf_pmu_alias *newalias)
279 perf_pmu_assign_str(old->name, "desc", &old->desc, &newalias->desc);
280 perf_pmu_assign_str(old->name, "long_desc", &old->long_desc,
281 &newalias->long_desc);
282 perf_pmu_assign_str(old->name, "topic", &old->topic, &newalias->topic);
283 perf_pmu_assign_str(old->name, "value", &old->str, &newalias->str);
284 old->scale = newalias->scale;
285 old->per_pkg = newalias->per_pkg;
286 old->snapshot = newalias->snapshot;
287 memcpy(old->unit, newalias->unit, sizeof(old->unit));
290 /* Delete an alias entry. */
291 void perf_pmu_free_alias(struct perf_pmu_alias *newalias)
293 zfree(&newalias->name);
294 zfree(&newalias->desc);
295 zfree(&newalias->long_desc);
296 zfree(&newalias->topic);
297 zfree(&newalias->str);
298 zfree(&newalias->pmu_name);
299 parse_events_terms__purge(&newalias->terms);
303 static void perf_pmu__del_aliases(struct perf_pmu *pmu)
305 struct perf_pmu_alias *alias, *tmp;
307 list_for_each_entry_safe(alias, tmp, &pmu->aliases, list) {
308 list_del(&alias->list);
309 perf_pmu_free_alias(alias);
313 /* Merge an alias, search in alias list. If this name is already
314 * present merge both of them to combine all information.
316 static bool perf_pmu_merge_alias(struct perf_pmu_alias *newalias,
317 struct list_head *alist)
319 struct perf_pmu_alias *a;
321 list_for_each_entry(a, alist, list) {
322 if (!strcasecmp(newalias->name, a->name)) {
323 if (newalias->pmu_name && a->pmu_name &&
324 !strcasecmp(newalias->pmu_name, a->pmu_name)) {
327 perf_pmu_update_alias(a, newalias);
328 perf_pmu_free_alias(newalias);
335 static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name,
336 char *desc, char *val, const struct pmu_event *pe)
338 struct parse_events_term *term;
339 struct perf_pmu_alias *alias;
342 char *long_desc = NULL, *topic = NULL, *unit = NULL, *pmu_name = NULL;
343 bool deprecated = false, perpkg = false;
346 long_desc = (char *)pe->long_desc;
347 topic = (char *)pe->topic;
348 unit = (char *)pe->unit;
350 deprecated = pe->deprecated;
351 pmu_name = (char *)pe->pmu;
354 alias = malloc(sizeof(*alias));
358 INIT_LIST_HEAD(&alias->terms);
360 alias->unit[0] = '\0';
361 alias->per_pkg = perpkg;
362 alias->snapshot = false;
363 alias->deprecated = deprecated;
365 ret = parse_events_terms(&alias->terms, val);
367 pr_err("Cannot parse alias %s: %d\n", val, ret);
372 /* Scan event and remove leading zeroes, spaces, newlines, some
373 * platforms have terms specified as
374 * event=0x0091 (read from files ../<PMU>/events/<FILE>
375 * and terms specified as event=0x91 (read from JSON files).
377 * Rebuild string to make alias->str member comparable.
379 memset(newval, 0, sizeof(newval));
381 list_for_each_entry(term, &alias->terms, list) {
383 ret += scnprintf(newval + ret, sizeof(newval) - ret,
385 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM)
386 ret += scnprintf(newval + ret, sizeof(newval) - ret,
387 "%s=%#x", term->config, term->val.num);
388 else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
389 ret += scnprintf(newval + ret, sizeof(newval) - ret,
390 "%s=%s", term->config, term->val.str);
393 alias->name = strdup(name);
396 * load unit name and scale if available
398 perf_pmu__parse_unit(alias, dir, name);
399 perf_pmu__parse_scale(alias, dir, name);
400 perf_pmu__parse_per_pkg(alias, dir, name);
401 perf_pmu__parse_snapshot(alias, dir, name);
404 alias->desc = desc ? strdup(desc) : NULL;
405 alias->long_desc = long_desc ? strdup(long_desc) :
406 desc ? strdup(desc) : NULL;
407 alias->topic = topic ? strdup(topic) : NULL;
409 if (perf_pmu__convert_scale(unit, &unit, &alias->scale) < 0)
411 snprintf(alias->unit, sizeof(alias->unit), "%s", unit);
413 alias->str = strdup(newval);
414 alias->pmu_name = pmu_name ? strdup(pmu_name) : NULL;
416 if (!perf_pmu_merge_alias(alias, list))
417 list_add_tail(&alias->list, list);
422 static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file)
427 ret = fread(buf, 1, sizeof(buf), file);
433 /* Remove trailing newline from sysfs file */
436 return __perf_pmu__new_alias(list, dir, name, NULL, buf, NULL);
439 static inline bool pmu_alias_info_file(char *name)
444 if (len > 5 && !strcmp(name + len - 5, ".unit"))
446 if (len > 6 && !strcmp(name + len - 6, ".scale"))
448 if (len > 8 && !strcmp(name + len - 8, ".per-pkg"))
450 if (len > 9 && !strcmp(name + len - 9, ".snapshot"))
457 * Process all the sysfs attributes located under the directory
458 * specified in 'dir' parameter.
460 static int pmu_aliases_parse(char *dir, struct list_head *head)
462 struct dirent *evt_ent;
465 event_dir = opendir(dir);
469 while ((evt_ent = readdir(event_dir))) {
471 char *name = evt_ent->d_name;
474 if (!strcmp(name, ".") || !strcmp(name, ".."))
478 * skip info files parsed in perf_pmu__new_alias()
480 if (pmu_alias_info_file(name))
483 scnprintf(path, PATH_MAX, "%s/%s", dir, name);
485 file = fopen(path, "r");
487 pr_debug("Cannot open %s\n", path);
491 if (perf_pmu__new_alias(head, dir, name, file) < 0)
492 pr_debug("Cannot set up %s\n", name);
501 * Reading the pmu event aliases definition, which should be located at:
502 * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
504 static int pmu_aliases(const char *name, struct list_head *head)
508 if (!perf_pmu__pathname_scnprintf(path, sizeof(path), name, "events"))
511 if (!file_available(path))
514 if (pmu_aliases_parse(path, head))
520 static int pmu_alias_terms(struct perf_pmu_alias *alias,
521 struct list_head *terms)
523 struct parse_events_term *term, *cloned;
527 list_for_each_entry(term, &alias->terms, list) {
528 ret = parse_events_term__clone(&cloned, term);
530 parse_events_terms__purge(&list);
534 * Weak terms don't override command line options,
535 * which we don't want for implicit terms in aliases.
538 list_add_tail(&cloned->list, &list);
540 list_splice(&list, terms);
544 /* Add all pmus in sysfs to pmu list: */
545 static void pmu_read_sysfs(void)
551 if (!perf_pmu__event_source_devices_scnprintf(path, sizeof(path)))
558 while ((dent = readdir(dir))) {
559 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
561 /* add to static LIST_HEAD(pmus): */
562 perf_pmu__find(dent->d_name);
569 * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64)
570 * may have a "cpus" file.
572 static struct perf_cpu_map *pmu_cpumask(const char *name)
574 struct perf_cpu_map *cpus;
575 const char *templates[] = {
580 const char **template;
581 char pmu_name[PATH_MAX];
582 struct perf_pmu pmu = {.name = pmu_name};
585 strlcpy(pmu_name, name, sizeof(pmu_name));
586 for (template = templates; *template; template++) {
587 file = perf_pmu__open_file(&pmu, *template);
590 cpus = perf_cpu_map__read(file);
598 static bool pmu_is_uncore(const char *name)
602 if (perf_pmu__hybrid_mounted(name))
605 perf_pmu__pathname_scnprintf(path, sizeof(path), name, "cpumask");
606 return file_available(path);
609 static char *pmu_id(const char *name)
611 char path[PATH_MAX], *str;
614 perf_pmu__pathname_scnprintf(path, sizeof(path), name, "identifier");
616 if (filename__read_str(path, &str, &len) < 0)
619 str[len - 1] = 0; /* remove line feed */
625 * PMU CORE devices have different name other than cpu in sysfs on some
627 * Looking for possible sysfs files to identify the arm core device.
629 static int is_arm_pmu_core(const char *name)
633 if (!perf_pmu__pathname_scnprintf(path, sizeof(path), name, "cpus"))
635 return file_available(path);
638 char *perf_pmu__getcpuid(struct perf_pmu *pmu)
643 cpuid = getenv("PERF_CPUID");
645 cpuid = strdup(cpuid);
647 cpuid = get_cpuid_str(pmu);
652 pr_debug("Using CPUID %s\n", cpuid);
658 __weak const struct pmu_events_table *pmu_events_table__find(void)
660 return perf_pmu__find_events_table(NULL);
663 __weak const struct pmu_metrics_table *pmu_metrics_table__find(void)
665 return perf_pmu__find_metrics_table(NULL);
669 * Suffix must be in form tok_{digits}, or tok{digits}, or same as pmu_name
672 static bool perf_pmu__valid_suffix(const char *pmu_name, char *tok)
676 if (strncmp(pmu_name, tok, strlen(tok)))
679 p = pmu_name + strlen(tok);
686 /* Ensure we end in a number */
697 bool pmu_uncore_alias_match(const char *pmu_name, const char *name)
699 char *tmp = NULL, *tok, *str;
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__valid_suffix(name, tok))) {
744 struct pmu_add_cpu_aliases_map_data {
745 struct list_head *head;
747 const char *cpu_name;
748 struct perf_pmu *pmu;
751 static int pmu_add_cpu_aliases_map_callback(const struct pmu_event *pe,
752 const struct pmu_events_table *table __maybe_unused,
755 struct pmu_add_cpu_aliases_map_data *data = vdata;
756 const char *pname = pe->pmu ? pe->pmu : data->cpu_name;
758 if (data->pmu->is_uncore && pmu_uncore_alias_match(pname, data->name))
761 if (strcmp(pname, data->name))
765 /* need type casts to override 'const' */
766 __perf_pmu__new_alias(data->head, NULL, (char *)pe->name, (char *)pe->desc,
767 (char *)pe->event, pe);
772 * From the pmu_events_map, find the table of PMU events that corresponds
773 * to the current running CPU. Then, add all PMU events from that table
776 void pmu_add_cpu_aliases_table(struct list_head *head, struct perf_pmu *pmu,
777 const struct pmu_events_table *table)
779 struct pmu_add_cpu_aliases_map_data data = {
782 .cpu_name = is_arm_pmu_core(pmu->name) ? pmu->name : "cpu",
786 pmu_events_table_for_each_event(table, pmu_add_cpu_aliases_map_callback, &data);
789 static void pmu_add_cpu_aliases(struct list_head *head, struct perf_pmu *pmu)
791 const struct pmu_events_table *table;
793 table = perf_pmu__find_events_table(pmu);
797 pmu_add_cpu_aliases_table(head, pmu, table);
800 struct pmu_sys_event_iter_data {
801 struct list_head *head;
802 struct perf_pmu *pmu;
805 static int pmu_add_sys_aliases_iter_fn(const struct pmu_event *pe,
806 const struct pmu_events_table *table __maybe_unused,
809 struct pmu_sys_event_iter_data *idata = data;
810 struct perf_pmu *pmu = idata->pmu;
812 if (!pe->compat || !pe->pmu)
815 if (!strcmp(pmu->id, pe->compat) &&
816 pmu_uncore_alias_match(pe->pmu, pmu->name)) {
817 __perf_pmu__new_alias(idata->head, NULL,
827 void pmu_add_sys_aliases(struct list_head *head, struct perf_pmu *pmu)
829 struct pmu_sys_event_iter_data idata = {
837 pmu_for_each_sys_event(pmu_add_sys_aliases_iter_fn, &idata);
840 struct perf_event_attr * __weak
841 perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
847 pmu_find_real_name(const char *name)
853 pmu_find_alias_name(const char *name __maybe_unused)
858 static int pmu_max_precise(struct perf_pmu *pmu)
860 int max_precise = -1;
862 perf_pmu__scan_file(pmu, "caps/max_precise", "%d", &max_precise);
866 static struct perf_pmu *pmu_lookup(const char *lookup_name)
868 struct perf_pmu *pmu;
872 char *name = pmu_find_real_name(lookup_name);
873 bool is_hybrid = perf_pmu__hybrid_mounted(name);
877 * Check pmu name for hybrid and the pmu may be invalid in sysfs
879 if (!strncmp(name, "cpu_", 4) && !is_hybrid)
883 * The pmu data we store & need consists of the pmu
884 * type value and format definitions. Load both right
887 if (pmu_format(name, &format))
891 * Check the aliases first to avoid unnecessary work.
893 if (pmu_aliases(name, &aliases))
896 pmu = zalloc(sizeof(*pmu));
900 pmu->cpus = pmu_cpumask(name);
901 pmu->name = strdup(name);
906 /* Read type, and ensure that type value is successfully assigned (return 1) */
907 if (perf_pmu__scan_file(pmu, "type", "%u", &type) != 1)
910 alias_name = pmu_find_alias_name(name);
912 pmu->alias_name = strdup(alias_name);
913 if (!pmu->alias_name)
918 pmu->is_uncore = pmu_is_uncore(name);
920 pmu->id = pmu_id(name);
921 pmu->max_precise = pmu_max_precise(pmu);
922 pmu_add_cpu_aliases(&aliases, pmu);
923 pmu_add_sys_aliases(&aliases, pmu);
925 INIT_LIST_HEAD(&pmu->format);
926 INIT_LIST_HEAD(&pmu->aliases);
927 INIT_LIST_HEAD(&pmu->caps);
928 list_splice(&format, &pmu->format);
929 list_splice(&aliases, &pmu->aliases);
930 list_add_tail(&pmu->list, &pmus);
933 list_add_tail(&pmu->hybrid_list, &perf_pmu__hybrid_pmus);
935 INIT_LIST_HEAD(&pmu->hybrid_list);
937 pmu->default_config = perf_pmu__get_default_config(pmu);
947 void perf_pmu__warn_invalid_formats(struct perf_pmu *pmu)
949 struct perf_pmu_format *format;
951 /* fake pmu doesn't have format list */
952 if (pmu == &perf_pmu__fake)
955 list_for_each_entry(format, &pmu->format, list)
956 if (format->value >= PERF_PMU_FORMAT_VALUE_CONFIG_END) {
957 pr_warning("WARNING: '%s' format '%s' requires 'perf_event_attr::config%d'"
958 "which is not supported by this version of perf!\n",
959 pmu->name, format->name, format->value);
964 static struct perf_pmu *pmu_find(const char *name)
966 struct perf_pmu *pmu;
968 list_for_each_entry(pmu, &pmus, list) {
969 if (!strcmp(pmu->name, name) ||
970 (pmu->alias_name && !strcmp(pmu->alias_name, name)))
977 struct perf_pmu *perf_pmu__find_by_type(unsigned int type)
979 struct perf_pmu *pmu;
981 list_for_each_entry(pmu, &pmus, list)
982 if (pmu->type == type)
988 struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
991 * pmu iterator: If pmu is NULL, we start at the begin,
992 * otherwise return the next pmu. Returns NULL on end.
996 pmu = list_prepare_entry(pmu, &pmus, list);
998 list_for_each_entry_continue(pmu, &pmus, list)
1003 struct perf_pmu *evsel__find_pmu(const struct evsel *evsel)
1005 struct perf_pmu *pmu = NULL;
1010 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1011 if (pmu->type == evsel->core.attr.type)
1015 ((struct evsel *)evsel)->pmu = pmu;
1019 bool evsel__is_aux_event(const struct evsel *evsel)
1021 struct perf_pmu *pmu = evsel__find_pmu(evsel);
1023 return pmu && pmu->auxtrace;
1026 struct perf_pmu *perf_pmu__find(const char *name)
1028 struct perf_pmu *pmu;
1031 * Once PMU is loaded it stays in the list,
1032 * so we keep us from multiple reading/parsing
1033 * the pmu format definitions.
1035 pmu = pmu_find(name);
1039 return pmu_lookup(name);
1042 static struct perf_pmu_format *
1043 pmu_find_format(struct list_head *formats, const char *name)
1045 struct perf_pmu_format *format;
1047 list_for_each_entry(format, formats, list)
1048 if (!strcmp(format->name, name))
1054 __u64 perf_pmu__format_bits(struct list_head *formats, const char *name)
1056 struct perf_pmu_format *format = pmu_find_format(formats, name);
1063 for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
1064 bits |= 1ULL << fbit;
1069 int perf_pmu__format_type(struct list_head *formats, const char *name)
1071 struct perf_pmu_format *format = pmu_find_format(formats, name);
1076 return format->value;
1080 * Sets value based on the format definition (format parameter)
1081 * and unformatted value (value parameter).
1083 static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
1086 unsigned long fbit, vbit;
1088 for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
1090 if (!test_bit(fbit, format))
1093 if (value & (1llu << vbit++))
1094 *v |= (1llu << fbit);
1096 *v &= ~(1llu << fbit);
1100 static __u64 pmu_format_max_value(const unsigned long *format)
1104 w = bitmap_weight(format, PERF_PMU_FORMAT_BITS);
1108 return (1ULL << w) - 1;
1113 * Term is a string term, and might be a param-term. Try to look up it's value
1114 * in the remaining terms.
1115 * - We have a term like "base-or-format-term=param-term",
1116 * - We need to find the value supplied for "param-term" (with param-term named
1117 * in a config string) later on in the term list.
1119 static int pmu_resolve_param_term(struct parse_events_term *term,
1120 struct list_head *head_terms,
1123 struct parse_events_term *t;
1125 list_for_each_entry(t, head_terms, list) {
1126 if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM &&
1127 t->config && !strcmp(t->config, term->config)) {
1129 *value = t->val.num;
1135 printf("Required parameter '%s' not specified\n", term->config);
1140 static char *pmu_formats_string(struct list_head *formats)
1142 struct perf_pmu_format *format;
1144 struct strbuf buf = STRBUF_INIT;
1150 /* sysfs exported terms */
1151 list_for_each_entry(format, formats, list)
1152 if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
1155 str = strbuf_detach(&buf, NULL);
1157 strbuf_release(&buf);
1163 * Setup one of config[12] attr members based on the
1164 * user input data - term parameter.
1166 static int pmu_config_term(const char *pmu_name,
1167 struct list_head *formats,
1168 struct perf_event_attr *attr,
1169 struct parse_events_term *term,
1170 struct list_head *head_terms,
1171 bool zero, struct parse_events_error *err)
1173 struct perf_pmu_format *format;
1178 * If this is a parameter we've already used for parameterized-eval,
1179 * skip it in normal eval.
1185 * Hardcoded terms should be already in, so nothing
1186 * to be done for them.
1188 if (parse_events__is_hardcoded_term(term))
1191 format = pmu_find_format(formats, term->config);
1193 char *pmu_term = pmu_formats_string(formats);
1197 if (asprintf(&unknown_term,
1198 "unknown term '%s' for pmu '%s'",
1199 term->config, pmu_name) < 0)
1200 unknown_term = NULL;
1201 help_msg = parse_events_formats_error_string(pmu_term);
1203 parse_events_error__handle(err, term->err_term,
1207 pr_debug("%s (%s)\n", unknown_term, help_msg);
1214 switch (format->value) {
1215 case PERF_PMU_FORMAT_VALUE_CONFIG:
1218 case PERF_PMU_FORMAT_VALUE_CONFIG1:
1219 vp = &attr->config1;
1221 case PERF_PMU_FORMAT_VALUE_CONFIG2:
1222 vp = &attr->config2;
1224 case PERF_PMU_FORMAT_VALUE_CONFIG3:
1225 vp = &attr->config3;
1232 * Either directly use a numeric term, or try to translate string terms
1233 * using event parameters.
1235 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1236 if (term->no_value &&
1237 bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) {
1239 parse_events_error__handle(err, term->err_val,
1240 strdup("no value assigned for term"),
1246 val = term->val.num;
1247 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1248 if (strcmp(term->val.str, "?")) {
1250 pr_info("Invalid sysfs entry %s=%s\n",
1251 term->config, term->val.str);
1254 parse_events_error__handle(err, term->err_val,
1255 strdup("expected numeric value"),
1261 if (pmu_resolve_param_term(term, head_terms, &val))
1266 max_val = pmu_format_max_value(format->bits);
1267 if (val > max_val) {
1271 parse_events_error__handle(err, term->err_val,
1273 "value too big for format, maximum is %llu",
1274 (unsigned long long)max_val) < 0
1275 ? strdup("value too big for format")
1281 * Assume we don't care if !err, in which case the value will be
1282 * silently truncated.
1286 pmu_format_value(format->bits, val, vp, zero);
1290 int perf_pmu__config_terms(const char *pmu_name, struct list_head *formats,
1291 struct perf_event_attr *attr,
1292 struct list_head *head_terms,
1293 bool zero, struct parse_events_error *err)
1295 struct parse_events_term *term;
1297 list_for_each_entry(term, head_terms, list) {
1298 if (pmu_config_term(pmu_name, formats, attr, term, head_terms,
1307 * Configures event's 'attr' parameter based on the:
1308 * 1) users input - specified in terms parameter
1309 * 2) pmu format definitions - specified by pmu parameter
1311 int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
1312 struct list_head *head_terms,
1313 struct parse_events_error *err)
1315 bool zero = !!pmu->default_config;
1317 attr->type = pmu->type;
1318 return perf_pmu__config_terms(pmu->name, &pmu->format, attr,
1319 head_terms, zero, err);
1322 static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
1323 struct parse_events_term *term)
1325 struct perf_pmu_alias *alias;
1328 if (parse_events__is_hardcoded_term(term))
1331 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1332 if (term->val.num != 1)
1334 if (pmu_find_format(&pmu->format, term->config))
1336 name = term->config;
1337 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1338 if (strcasecmp(term->config, "event"))
1340 name = term->val.str;
1345 list_for_each_entry(alias, &pmu->aliases, list) {
1346 if (!strcasecmp(alias->name, name))
1353 static int check_info_data(struct perf_pmu_alias *alias,
1354 struct perf_pmu_info *info)
1357 * Only one term in event definition can
1358 * define unit, scale and snapshot, fail
1359 * if there's more than one.
1361 if ((info->unit && alias->unit[0]) ||
1362 (info->scale && alias->scale) ||
1363 (info->snapshot && alias->snapshot))
1367 info->unit = alias->unit;
1370 info->scale = alias->scale;
1372 if (alias->snapshot)
1373 info->snapshot = alias->snapshot;
1379 * Find alias in the terms list and replace it with the terms
1380 * defined for the alias
1382 int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
1383 struct perf_pmu_info *info)
1385 struct parse_events_term *term, *h;
1386 struct perf_pmu_alias *alias;
1389 info->per_pkg = false;
1392 * Mark unit and scale as not set
1393 * (different from default values, see below)
1397 info->snapshot = false;
1399 list_for_each_entry_safe(term, h, head_terms, list) {
1400 alias = pmu_find_alias(pmu, term);
1403 ret = pmu_alias_terms(alias, &term->list);
1407 ret = check_info_data(alias, info);
1412 info->per_pkg = true;
1414 list_del_init(&term->list);
1415 parse_events_term__delete(term);
1419 * if no unit or scale found in aliases, then
1420 * set defaults as for evsel
1421 * unit cannot left to NULL
1423 if (info->unit == NULL)
1426 if (info->scale == 0.0)
1432 int perf_pmu__new_format(struct list_head *list, char *name,
1433 int config, unsigned long *bits)
1435 struct perf_pmu_format *format;
1437 format = zalloc(sizeof(*format));
1441 format->name = strdup(name);
1442 format->value = config;
1443 memcpy(format->bits, bits, sizeof(format->bits));
1445 list_add_tail(&format->list, list);
1449 void perf_pmu__set_format(unsigned long *bits, long from, long to)
1456 memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
1457 for (b = from; b <= to; b++)
1461 void perf_pmu__del_formats(struct list_head *formats)
1463 struct perf_pmu_format *fmt, *tmp;
1465 list_for_each_entry_safe(fmt, tmp, formats, list) {
1466 list_del(&fmt->list);
1472 static int sub_non_neg(int a, int b)
1479 static char *format_alias(char *buf, int len, const struct perf_pmu *pmu,
1480 const struct perf_pmu_alias *alias)
1482 struct parse_events_term *term;
1483 int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name);
1485 list_for_each_entry(term, &alias->terms, list) {
1486 if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
1487 used += snprintf(buf + used, sub_non_neg(len, used),
1488 ",%s=%s", term->config,
1492 if (sub_non_neg(len, used) > 0) {
1496 if (sub_non_neg(len, used) > 0) {
1500 buf[len - 1] = '\0';
1505 /** Struct for ordering events as output in perf list. */
1507 /** PMU for event. */
1508 const struct perf_pmu *pmu;
1510 * Optional event for name, desc, etc. If not present then this is a
1511 * selectable PMU and the event name is shown as "//".
1513 const struct perf_pmu_alias *event;
1514 /** Is the PMU for the CPU? */
1518 static int cmp_sevent(const void *a, const void *b)
1520 const struct sevent *as = a;
1521 const struct sevent *bs = b;
1522 const char *a_pmu_name, *b_pmu_name;
1523 const char *a_name = "//", *a_desc = NULL, *a_topic = "";
1524 const char *b_name = "//", *b_desc = NULL, *b_topic = "";
1528 a_name = as->event->name;
1529 a_desc = as->event->desc;
1530 a_topic = as->event->topic ?: "";
1533 b_name = bs->event->name;
1534 b_desc = bs->event->desc;
1535 b_topic = bs->event->topic ?: "";
1537 /* Put extra events last. */
1538 if (!!a_desc != !!b_desc)
1539 return !!a_desc - !!b_desc;
1541 /* Order by topics. */
1542 ret = strcmp(a_topic, b_topic);
1546 /* Order CPU core events to be first */
1547 if (as->is_cpu != bs->is_cpu)
1548 return as->is_cpu ? -1 : 1;
1550 /* Order by PMU name. */
1551 a_pmu_name = as->pmu->name ?: "";
1552 b_pmu_name = bs->pmu->name ?: "";
1553 ret = strcmp(a_pmu_name, b_pmu_name);
1557 /* Order by event name. */
1558 return strcmp(a_name, b_name);
1561 bool is_pmu_core(const char *name)
1563 return !strcmp(name, "cpu") || is_arm_pmu_core(name);
1566 static bool pmu_alias_is_duplicate(struct sevent *alias_a,
1567 struct sevent *alias_b)
1569 const char *a_pmu_name, *b_pmu_name;
1570 const char *a_name = alias_a->event ? alias_a->event->name : "//";
1571 const char *b_name = alias_b->event ? alias_b->event->name : "//";
1573 /* Different names -> never duplicates */
1574 if (strcmp(a_name, b_name))
1577 /* Don't remove duplicates for different PMUs */
1578 a_pmu_name = alias_a->pmu->name ?: "";
1579 b_pmu_name = alias_b->pmu->name ?: "";
1580 return strcmp(a_pmu_name, b_pmu_name) == 0;
1583 void print_pmu_events(const struct print_callbacks *print_cb, void *print_state)
1585 struct perf_pmu *pmu;
1586 struct perf_pmu_alias *event;
1590 struct sevent *aliases;
1594 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1595 list_for_each_entry(event, &pmu->aliases, list)
1597 if (pmu->selectable)
1600 aliases = zalloc(sizeof(struct sevent) * len);
1602 pr_err("FATAL: not enough memory to print PMU events\n");
1607 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1608 bool is_cpu = is_pmu_core(pmu->name) || perf_pmu__is_hybrid(pmu->name);
1610 list_for_each_entry(event, &pmu->aliases, list) {
1611 aliases[j].event = event;
1612 aliases[j].pmu = pmu;
1613 aliases[j].is_cpu = is_cpu;
1616 if (pmu->selectable) {
1617 aliases[j].event = NULL;
1618 aliases[j].pmu = pmu;
1619 aliases[j].is_cpu = is_cpu;
1624 qsort(aliases, len, sizeof(struct sevent), cmp_sevent);
1625 for (j = 0; j < len; j++) {
1626 const char *name, *alias = NULL, *scale_unit = NULL,
1627 *desc = NULL, *long_desc = NULL,
1628 *encoding_desc = NULL, *topic = NULL;
1629 bool deprecated = false;
1632 /* Skip duplicates */
1633 if (j > 0 && pmu_alias_is_duplicate(&aliases[j], &aliases[j - 1]))
1636 if (!aliases[j].event) {
1637 /* A selectable event. */
1638 buf_used = snprintf(buf, sizeof(buf), "%s//", aliases[j].pmu->name) + 1;
1641 if (aliases[j].event->desc) {
1642 name = aliases[j].event->name;
1645 name = format_alias(buf, sizeof(buf), aliases[j].pmu,
1647 if (aliases[j].is_cpu) {
1649 name = aliases[j].event->name;
1651 buf_used = strlen(buf) + 1;
1653 if (strlen(aliases[j].event->unit) || aliases[j].event->scale != 1.0) {
1654 scale_unit = buf + buf_used;
1655 buf_used += snprintf(buf + buf_used, sizeof(buf) - buf_used,
1656 "%G%s", aliases[j].event->scale,
1657 aliases[j].event->unit) + 1;
1659 desc = aliases[j].event->desc;
1660 long_desc = aliases[j].event->long_desc;
1661 topic = aliases[j].event->topic;
1662 encoding_desc = buf + buf_used;
1663 buf_used += snprintf(buf + buf_used, sizeof(buf) - buf_used,
1664 "%s/%s/", aliases[j].pmu->name,
1665 aliases[j].event->str) + 1;
1666 deprecated = aliases[j].event->deprecated;
1668 print_cb->print_event(print_state,
1669 aliases[j].pmu->name,
1680 if (printed && pager_in_use())
1687 bool pmu_have_event(const char *pname, const char *name)
1689 struct perf_pmu *pmu;
1690 struct perf_pmu_alias *alias;
1693 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1694 if (strcmp(pname, pmu->name))
1696 list_for_each_entry(alias, &pmu->aliases, list)
1697 if (!strcmp(alias->name, name))
1703 FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
1705 char path[PATH_MAX];
1707 if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, name) ||
1708 !file_available(path))
1711 return fopen(path, "r");
1714 int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
1721 va_start(args, fmt);
1722 file = perf_pmu__open_file(pmu, name);
1724 ret = vfscanf(file, fmt, args);
1731 bool perf_pmu__file_exists(struct perf_pmu *pmu, const char *name)
1733 char path[PATH_MAX];
1735 if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, name))
1738 return file_available(path);
1741 static int perf_pmu__new_caps(struct list_head *list, char *name, char *value)
1743 struct perf_pmu_caps *caps = zalloc(sizeof(*caps));
1748 caps->name = strdup(name);
1751 caps->value = strndup(value, strlen(value) - 1);
1754 list_add_tail(&caps->list, list);
1765 static void perf_pmu__del_caps(struct perf_pmu *pmu)
1767 struct perf_pmu_caps *caps, *tmp;
1769 list_for_each_entry_safe(caps, tmp, &pmu->caps, list) {
1770 list_del(&caps->list);
1778 * Reading/parsing the given pmu capabilities, which should be located at:
1779 * /sys/bus/event_source/devices/<dev>/caps as sysfs group attributes.
1780 * Return the number of capabilities
1782 int perf_pmu__caps_parse(struct perf_pmu *pmu)
1785 char caps_path[PATH_MAX];
1787 struct dirent *evt_ent;
1789 if (pmu->caps_initialized)
1790 return pmu->nr_caps;
1794 if (!perf_pmu__pathname_scnprintf(caps_path, sizeof(caps_path), pmu->name, "caps"))
1797 if (stat(caps_path, &st) < 0) {
1798 pmu->caps_initialized = true;
1799 return 0; /* no error if caps does not exist */
1802 caps_dir = opendir(caps_path);
1806 while ((evt_ent = readdir(caps_dir)) != NULL) {
1807 char path[PATH_MAX + NAME_MAX + 1];
1808 char *name = evt_ent->d_name;
1812 if (!strcmp(name, ".") || !strcmp(name, ".."))
1815 snprintf(path, sizeof(path), "%s/%s", caps_path, name);
1817 file = fopen(path, "r");
1821 if (!fgets(value, sizeof(value), file) ||
1822 (perf_pmu__new_caps(&pmu->caps, name, value) < 0)) {
1833 pmu->caps_initialized = true;
1834 return pmu->nr_caps;
1837 void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config,
1840 struct perf_pmu_format *format;
1841 __u64 masks = 0, bits;
1845 list_for_each_entry(format, &pmu->format, list) {
1846 if (format->value != PERF_PMU_FORMAT_VALUE_CONFIG)
1849 for_each_set_bit(i, format->bits, PERF_PMU_FORMAT_BITS)
1854 * Kernel doesn't export any valid format bits.
1859 bits = config & ~masks;
1863 bitmap_scnprintf((unsigned long *)&bits, sizeof(bits) * 8, buf, sizeof(buf));
1865 pr_warning("WARNING: event '%s' not valid (bits %s of config "
1866 "'%llx' not supported by kernel)!\n",
1867 name ?: "N/A", buf, config);
1870 bool perf_pmu__has_hybrid(void)
1872 if (!hybrid_scanned) {
1873 hybrid_scanned = true;
1874 perf_pmu__scan(NULL);
1877 return !list_empty(&perf_pmu__hybrid_pmus);
1880 int perf_pmu__match(char *pattern, char *name, char *tok)
1885 if (fnmatch(pattern, name, 0))
1888 if (tok && !perf_pmu__valid_suffix(name, tok))
1894 int perf_pmu__cpus_match(struct perf_pmu *pmu, struct perf_cpu_map *cpus,
1895 struct perf_cpu_map **mcpus_ptr,
1896 struct perf_cpu_map **ucpus_ptr)
1898 struct perf_cpu_map *pmu_cpus = pmu->cpus;
1899 struct perf_cpu_map *matched_cpus, *unmatched_cpus;
1900 struct perf_cpu cpu;
1901 int i, matched_nr = 0, unmatched_nr = 0;
1903 matched_cpus = perf_cpu_map__default_new();
1907 unmatched_cpus = perf_cpu_map__default_new();
1908 if (!unmatched_cpus) {
1909 perf_cpu_map__put(matched_cpus);
1913 perf_cpu_map__for_each_cpu(cpu, i, cpus) {
1914 if (!perf_cpu_map__has(pmu_cpus, cpu))
1915 unmatched_cpus->map[unmatched_nr++] = cpu;
1917 matched_cpus->map[matched_nr++] = cpu;
1920 unmatched_cpus->nr = unmatched_nr;
1921 matched_cpus->nr = matched_nr;
1922 *mcpus_ptr = matched_cpus;
1923 *ucpus_ptr = unmatched_cpus;
1927 double __weak perf_pmu__cpu_slots_per_cycle(void)
1932 int perf_pmu__event_source_devices_scnprintf(char *pathname, size_t size)
1934 const char *sysfs = sysfs__mountpoint();
1938 return scnprintf(pathname, size, "%s/bus/event_source/devices/", sysfs);
1942 * Fill 'buf' with the path to a file or folder in 'pmu_name' in
1943 * sysfs. For example if pmu_name = "cs_etm" and 'filename' = "format"
1944 * then pathname will be filled with
1945 * "/sys/bus/event_source/devices/cs_etm/format"
1947 * Return 0 if the sysfs mountpoint couldn't be found or if no
1948 * characters were written.
1950 int perf_pmu__pathname_scnprintf(char *buf, size_t size,
1951 const char *pmu_name, const char *filename)
1953 char base_path[PATH_MAX];
1955 if (!perf_pmu__event_source_devices_scnprintf(base_path, sizeof(base_path)))
1957 return scnprintf(buf, size, "%s%s/%s", base_path, pmu_name, filename);
1960 static void perf_pmu__delete(struct perf_pmu *pmu)
1962 perf_pmu__del_formats(&pmu->format);
1963 perf_pmu__del_aliases(pmu);
1964 perf_pmu__del_caps(pmu);
1966 perf_cpu_map__put(pmu->cpus);
1968 free(pmu->default_config);
1970 free(pmu->alias_name);
1974 void perf_pmu__destroy(void)
1976 struct perf_pmu *pmu, *tmp;
1978 list_for_each_entry_safe(pmu, tmp, &pmus, list) {
1979 list_del(&pmu->list);
1980 list_del(&pmu->hybrid_list);
1982 perf_pmu__delete(pmu);