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, "metric_expr", &old->metric_expr,
284 &newalias->metric_expr);
285 perf_pmu_assign_str(old->name, "metric_name", &old->metric_name,
286 &newalias->metric_name);
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->metric_expr);
303 zfree(&newalias->metric_name);
304 zfree(&newalias->pmu_name);
305 parse_events_terms__purge(&newalias->terms);
309 /* Merge an alias, search in alias list. If this name is already
310 * present merge both of them to combine all information.
312 static bool perf_pmu_merge_alias(struct perf_pmu_alias *newalias,
313 struct list_head *alist)
315 struct perf_pmu_alias *a;
317 list_for_each_entry(a, alist, list) {
318 if (!strcasecmp(newalias->name, a->name)) {
319 if (newalias->pmu_name && a->pmu_name &&
320 !strcasecmp(newalias->pmu_name, a->pmu_name)) {
323 perf_pmu_update_alias(a, newalias);
324 perf_pmu_free_alias(newalias);
331 static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name,
332 char *desc, char *val, const struct pmu_event *pe)
334 struct parse_events_term *term;
335 struct perf_pmu_alias *alias;
339 char *long_desc = NULL, *topic = NULL, *unit = NULL, *perpkg = NULL,
340 *metric_expr = NULL, *metric_name = NULL, *deprecated = NULL,
344 long_desc = (char *)pe->long_desc;
345 topic = (char *)pe->topic;
346 unit = (char *)pe->unit;
347 perpkg = (char *)pe->perpkg;
348 metric_expr = (char *)pe->metric_expr;
349 metric_name = (char *)pe->metric_name;
350 deprecated = (char *)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 = false;
362 alias->snapshot = false;
363 alias->deprecated = false;
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->metric_expr = metric_expr ? strdup(metric_expr) : NULL;
405 alias->metric_name = metric_name ? strdup(metric_name): NULL;
406 alias->desc = desc ? strdup(desc) : NULL;
407 alias->long_desc = long_desc ? strdup(long_desc) :
408 desc ? strdup(desc) : NULL;
409 alias->topic = topic ? strdup(topic) : NULL;
411 if (perf_pmu__convert_scale(unit, &unit, &alias->scale) < 0)
413 snprintf(alias->unit, sizeof(alias->unit), "%s", unit);
415 alias->per_pkg = perpkg && sscanf(perpkg, "%d", &num) == 1 && num == 1;
416 alias->str = strdup(newval);
417 alias->pmu_name = pmu_name ? strdup(pmu_name) : NULL;
420 alias->deprecated = true;
422 if (!perf_pmu_merge_alias(alias, list))
423 list_add_tail(&alias->list, list);
428 static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file)
433 ret = fread(buf, 1, sizeof(buf), file);
439 /* Remove trailing newline from sysfs file */
442 return __perf_pmu__new_alias(list, dir, name, NULL, buf, NULL);
445 static inline bool pmu_alias_info_file(char *name)
450 if (len > 5 && !strcmp(name + len - 5, ".unit"))
452 if (len > 6 && !strcmp(name + len - 6, ".scale"))
454 if (len > 8 && !strcmp(name + len - 8, ".per-pkg"))
456 if (len > 9 && !strcmp(name + len - 9, ".snapshot"))
463 * Process all the sysfs attributes located under the directory
464 * specified in 'dir' parameter.
466 static int pmu_aliases_parse(char *dir, struct list_head *head)
468 struct dirent *evt_ent;
471 event_dir = opendir(dir);
475 while ((evt_ent = readdir(event_dir))) {
477 char *name = evt_ent->d_name;
480 if (!strcmp(name, ".") || !strcmp(name, ".."))
484 * skip info files parsed in perf_pmu__new_alias()
486 if (pmu_alias_info_file(name))
489 scnprintf(path, PATH_MAX, "%s/%s", dir, name);
491 file = fopen(path, "r");
493 pr_debug("Cannot open %s\n", path);
497 if (perf_pmu__new_alias(head, dir, name, file) < 0)
498 pr_debug("Cannot set up %s\n", name);
507 * Reading the pmu event aliases definition, which should be located at:
508 * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
510 static int pmu_aliases(const char *name, struct list_head *head)
514 if (!perf_pmu__pathname_scnprintf(path, sizeof(path), name, "events"))
517 if (!file_available(path))
520 if (pmu_aliases_parse(path, head))
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);
550 /* Add all pmus in sysfs to pmu list: */
551 static void pmu_read_sysfs(void)
557 if (!perf_pmu__event_source_devices_scnprintf(path, sizeof(path)))
564 while ((dent = readdir(dir))) {
565 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
567 /* add to static LIST_HEAD(pmus): */
568 perf_pmu__find(dent->d_name);
574 static struct perf_cpu_map *__pmu_cpumask(const char *path)
577 struct perf_cpu_map *cpus;
579 file = fopen(path, "r");
583 cpus = perf_cpu_map__read(file);
589 * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64)
590 * may have a "cpus" file.
592 #define SYS_TEMPLATE_ID "./bus/event_source/devices/%s/identifier"
593 #define CPUS_TEMPLATE_UNCORE "%s/bus/event_source/devices/%s/cpumask"
595 static struct perf_cpu_map *pmu_cpumask(const char *name)
598 struct perf_cpu_map *cpus;
599 const char *sysfs = sysfs__mountpoint();
600 const char *templates[] = {
601 CPUS_TEMPLATE_UNCORE,
605 const char **template;
610 for (template = templates; *template; template++) {
611 snprintf(path, PATH_MAX, *template, sysfs, name);
612 cpus = __pmu_cpumask(path);
620 static bool pmu_is_uncore(const char *name)
625 if (perf_pmu__hybrid_mounted(name))
628 sysfs = sysfs__mountpoint();
629 snprintf(path, PATH_MAX, CPUS_TEMPLATE_UNCORE, sysfs, name);
630 return file_available(path);
633 static char *pmu_id(const char *name)
635 char path[PATH_MAX], *str;
638 snprintf(path, PATH_MAX, SYS_TEMPLATE_ID, name);
640 if (sysfs__read_str(path, &str, &len) < 0)
643 str[len - 1] = 0; /* remove line feed */
649 * PMU CORE devices have different name other than cpu in sysfs on some
651 * Looking for possible sysfs files to identify the arm core device.
653 static int is_arm_pmu_core(const char *name)
657 if (!perf_pmu__pathname_scnprintf(path, sizeof(path), name, "cpus"))
659 return file_available(path);
662 char *perf_pmu__getcpuid(struct perf_pmu *pmu)
667 cpuid = getenv("PERF_CPUID");
669 cpuid = strdup(cpuid);
671 cpuid = get_cpuid_str(pmu);
676 pr_debug("Using CPUID %s\n", cpuid);
682 __weak const struct pmu_events_table *pmu_events_table__find(void)
684 return perf_pmu__find_table(NULL);
688 * Suffix must be in form tok_{digits}, or tok{digits}, or same as pmu_name
691 static bool perf_pmu__valid_suffix(const char *pmu_name, char *tok)
695 if (strncmp(pmu_name, tok, strlen(tok)))
698 p = pmu_name + strlen(tok);
705 /* Ensure we end in a number */
716 bool pmu_uncore_alias_match(const char *pmu_name, const char *name)
718 char *tmp = NULL, *tok, *str;
721 str = strdup(pmu_name);
726 * uncore alias may be from different PMU with common prefix
728 tok = strtok_r(str, ",", &tmp);
729 if (strncmp(pmu_name, tok, strlen(tok))) {
735 * Match more complex aliases where the alias name is a comma-delimited
736 * list of tokens, orderly contained in the matching PMU name.
738 * Example: For alias "socket,pmuname" and PMU "socketX_pmunameY", we
739 * match "socket" in "socketX_pmunameY" and then "pmuname" in
743 char *next_tok = strtok_r(NULL, ",", &tmp);
745 name = strstr(name, tok);
747 (!next_tok && !perf_pmu__valid_suffix(name, tok))) {
763 struct pmu_add_cpu_aliases_map_data {
764 struct list_head *head;
766 const char *cpu_name;
767 struct perf_pmu *pmu;
770 static int pmu_add_cpu_aliases_map_callback(const struct pmu_event *pe,
771 const struct pmu_events_table *table __maybe_unused,
774 struct pmu_add_cpu_aliases_map_data *data = vdata;
775 const char *pname = pe->pmu ? pe->pmu : data->cpu_name;
780 if (data->pmu->is_uncore && pmu_uncore_alias_match(pname, data->name))
783 if (strcmp(pname, data->name))
787 /* need type casts to override 'const' */
788 __perf_pmu__new_alias(data->head, NULL, (char *)pe->name, (char *)pe->desc,
789 (char *)pe->event, pe);
794 * From the pmu_events_map, find the table of PMU events that corresponds
795 * to the current running CPU. Then, add all PMU events from that table
798 void pmu_add_cpu_aliases_table(struct list_head *head, struct perf_pmu *pmu,
799 const struct pmu_events_table *table)
801 struct pmu_add_cpu_aliases_map_data data = {
804 .cpu_name = is_arm_pmu_core(pmu->name) ? pmu->name : "cpu",
808 pmu_events_table_for_each_event(table, pmu_add_cpu_aliases_map_callback, &data);
811 static void pmu_add_cpu_aliases(struct list_head *head, struct perf_pmu *pmu)
813 const struct pmu_events_table *table;
815 table = perf_pmu__find_table(pmu);
819 pmu_add_cpu_aliases_table(head, pmu, table);
822 struct pmu_sys_event_iter_data {
823 struct list_head *head;
824 struct perf_pmu *pmu;
827 static int pmu_add_sys_aliases_iter_fn(const struct pmu_event *pe,
828 const struct pmu_events_table *table __maybe_unused,
831 struct pmu_sys_event_iter_data *idata = data;
832 struct perf_pmu *pmu = idata->pmu;
835 if (pe->metric_group || pe->metric_name)
840 if (!pe->compat || !pe->pmu)
843 if (!strcmp(pmu->id, pe->compat) &&
844 pmu_uncore_alias_match(pe->pmu, pmu->name)) {
845 __perf_pmu__new_alias(idata->head, NULL,
855 void pmu_add_sys_aliases(struct list_head *head, struct perf_pmu *pmu)
857 struct pmu_sys_event_iter_data idata = {
865 pmu_for_each_sys_event(pmu_add_sys_aliases_iter_fn, &idata);
868 struct perf_event_attr * __weak
869 perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
875 pmu_find_real_name(const char *name)
881 pmu_find_alias_name(const char *name __maybe_unused)
886 static int pmu_max_precise(const char *name)
889 int max_precise = -1;
891 scnprintf(path, PATH_MAX,
892 "bus/event_source/devices/%s/caps/max_precise",
895 sysfs__read_int(path, &max_precise);
899 static struct perf_pmu *pmu_lookup(const char *lookup_name)
901 struct perf_pmu *pmu;
905 char *name = pmu_find_real_name(lookup_name);
906 bool is_hybrid = perf_pmu__hybrid_mounted(name);
910 * Check pmu name for hybrid and the pmu may be invalid in sysfs
912 if (!strncmp(name, "cpu_", 4) && !is_hybrid)
916 * The pmu data we store & need consists of the pmu
917 * type value and format definitions. Load both right
920 if (pmu_format(name, &format))
924 * Check the aliases first to avoid unnecessary work.
926 if (pmu_aliases(name, &aliases))
929 pmu = zalloc(sizeof(*pmu));
933 pmu->cpus = pmu_cpumask(name);
934 pmu->name = strdup(name);
939 /* Read type, and ensure that type value is successfully assigned (return 1) */
940 if (perf_pmu__scan_file(pmu, "type", "%u", &type) != 1)
943 alias_name = pmu_find_alias_name(name);
945 pmu->alias_name = strdup(alias_name);
946 if (!pmu->alias_name)
951 pmu->is_uncore = pmu_is_uncore(name);
953 pmu->id = pmu_id(name);
954 pmu->max_precise = pmu_max_precise(name);
955 pmu_add_cpu_aliases(&aliases, pmu);
956 pmu_add_sys_aliases(&aliases, pmu);
958 INIT_LIST_HEAD(&pmu->format);
959 INIT_LIST_HEAD(&pmu->aliases);
960 INIT_LIST_HEAD(&pmu->caps);
961 list_splice(&format, &pmu->format);
962 list_splice(&aliases, &pmu->aliases);
963 list_add_tail(&pmu->list, &pmus);
966 list_add_tail(&pmu->hybrid_list, &perf_pmu__hybrid_pmus);
968 pmu->default_config = perf_pmu__get_default_config(pmu);
978 void perf_pmu__warn_invalid_formats(struct perf_pmu *pmu)
980 struct perf_pmu_format *format;
982 /* fake pmu doesn't have format list */
983 if (pmu == &perf_pmu__fake)
986 list_for_each_entry(format, &pmu->format, list)
987 if (format->value >= PERF_PMU_FORMAT_VALUE_CONFIG_END) {
988 pr_warning("WARNING: '%s' format '%s' requires 'perf_event_attr::config%d'"
989 "which is not supported by this version of perf!\n",
990 pmu->name, format->name, format->value);
995 static struct perf_pmu *pmu_find(const char *name)
997 struct perf_pmu *pmu;
999 list_for_each_entry(pmu, &pmus, list) {
1000 if (!strcmp(pmu->name, name) ||
1001 (pmu->alias_name && !strcmp(pmu->alias_name, name)))
1008 struct perf_pmu *perf_pmu__find_by_type(unsigned int type)
1010 struct perf_pmu *pmu;
1012 list_for_each_entry(pmu, &pmus, list)
1013 if (pmu->type == type)
1019 struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
1022 * pmu iterator: If pmu is NULL, we start at the begin,
1023 * otherwise return the next pmu. Returns NULL on end.
1027 pmu = list_prepare_entry(pmu, &pmus, list);
1029 list_for_each_entry_continue(pmu, &pmus, list)
1034 struct perf_pmu *evsel__find_pmu(struct evsel *evsel)
1036 struct perf_pmu *pmu = NULL;
1041 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1042 if (pmu->type == evsel->core.attr.type)
1050 bool evsel__is_aux_event(struct evsel *evsel)
1052 struct perf_pmu *pmu = evsel__find_pmu(evsel);
1054 return pmu && pmu->auxtrace;
1057 struct perf_pmu *perf_pmu__find(const char *name)
1059 struct perf_pmu *pmu;
1062 * Once PMU is loaded it stays in the list,
1063 * so we keep us from multiple reading/parsing
1064 * the pmu format definitions.
1066 pmu = pmu_find(name);
1070 return pmu_lookup(name);
1073 static struct perf_pmu_format *
1074 pmu_find_format(struct list_head *formats, const char *name)
1076 struct perf_pmu_format *format;
1078 list_for_each_entry(format, formats, list)
1079 if (!strcmp(format->name, name))
1085 __u64 perf_pmu__format_bits(struct list_head *formats, const char *name)
1087 struct perf_pmu_format *format = pmu_find_format(formats, name);
1094 for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
1095 bits |= 1ULL << fbit;
1100 int perf_pmu__format_type(struct list_head *formats, const char *name)
1102 struct perf_pmu_format *format = pmu_find_format(formats, name);
1107 return format->value;
1111 * Sets value based on the format definition (format parameter)
1112 * and unformatted value (value parameter).
1114 static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
1117 unsigned long fbit, vbit;
1119 for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
1121 if (!test_bit(fbit, format))
1124 if (value & (1llu << vbit++))
1125 *v |= (1llu << fbit);
1127 *v &= ~(1llu << fbit);
1131 static __u64 pmu_format_max_value(const unsigned long *format)
1135 w = bitmap_weight(format, PERF_PMU_FORMAT_BITS);
1139 return (1ULL << w) - 1;
1144 * Term is a string term, and might be a param-term. Try to look up it's value
1145 * in the remaining terms.
1146 * - We have a term like "base-or-format-term=param-term",
1147 * - We need to find the value supplied for "param-term" (with param-term named
1148 * in a config string) later on in the term list.
1150 static int pmu_resolve_param_term(struct parse_events_term *term,
1151 struct list_head *head_terms,
1154 struct parse_events_term *t;
1156 list_for_each_entry(t, head_terms, list) {
1157 if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM &&
1158 t->config && !strcmp(t->config, term->config)) {
1160 *value = t->val.num;
1166 printf("Required parameter '%s' not specified\n", term->config);
1171 static char *pmu_formats_string(struct list_head *formats)
1173 struct perf_pmu_format *format;
1175 struct strbuf buf = STRBUF_INIT;
1181 /* sysfs exported terms */
1182 list_for_each_entry(format, formats, list)
1183 if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
1186 str = strbuf_detach(&buf, NULL);
1188 strbuf_release(&buf);
1194 * Setup one of config[12] attr members based on the
1195 * user input data - term parameter.
1197 static int pmu_config_term(const char *pmu_name,
1198 struct list_head *formats,
1199 struct perf_event_attr *attr,
1200 struct parse_events_term *term,
1201 struct list_head *head_terms,
1202 bool zero, struct parse_events_error *err)
1204 struct perf_pmu_format *format;
1209 * If this is a parameter we've already used for parameterized-eval,
1210 * skip it in normal eval.
1216 * Hardcoded terms should be already in, so nothing
1217 * to be done for them.
1219 if (parse_events__is_hardcoded_term(term))
1222 format = pmu_find_format(formats, term->config);
1224 char *pmu_term = pmu_formats_string(formats);
1228 if (asprintf(&unknown_term,
1229 "unknown term '%s' for pmu '%s'",
1230 term->config, pmu_name) < 0)
1231 unknown_term = NULL;
1232 help_msg = parse_events_formats_error_string(pmu_term);
1234 parse_events_error__handle(err, term->err_term,
1238 pr_debug("%s (%s)\n", unknown_term, help_msg);
1245 switch (format->value) {
1246 case PERF_PMU_FORMAT_VALUE_CONFIG:
1249 case PERF_PMU_FORMAT_VALUE_CONFIG1:
1250 vp = &attr->config1;
1252 case PERF_PMU_FORMAT_VALUE_CONFIG2:
1253 vp = &attr->config2;
1260 * Either directly use a numeric term, or try to translate string terms
1261 * using event parameters.
1263 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1264 if (term->no_value &&
1265 bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) {
1267 parse_events_error__handle(err, term->err_val,
1268 strdup("no value assigned for term"),
1274 val = term->val.num;
1275 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1276 if (strcmp(term->val.str, "?")) {
1278 pr_info("Invalid sysfs entry %s=%s\n",
1279 term->config, term->val.str);
1282 parse_events_error__handle(err, term->err_val,
1283 strdup("expected numeric value"),
1289 if (pmu_resolve_param_term(term, head_terms, &val))
1294 max_val = pmu_format_max_value(format->bits);
1295 if (val > max_val) {
1299 parse_events_error__handle(err, term->err_val,
1301 "value too big for format, maximum is %llu",
1302 (unsigned long long)max_val) < 0
1303 ? strdup("value too big for format")
1309 * Assume we don't care if !err, in which case the value will be
1310 * silently truncated.
1314 pmu_format_value(format->bits, val, vp, zero);
1318 int perf_pmu__config_terms(const char *pmu_name, struct list_head *formats,
1319 struct perf_event_attr *attr,
1320 struct list_head *head_terms,
1321 bool zero, struct parse_events_error *err)
1323 struct parse_events_term *term;
1325 list_for_each_entry(term, head_terms, list) {
1326 if (pmu_config_term(pmu_name, formats, attr, term, head_terms,
1335 * Configures event's 'attr' parameter based on the:
1336 * 1) users input - specified in terms parameter
1337 * 2) pmu format definitions - specified by pmu parameter
1339 int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
1340 struct list_head *head_terms,
1341 struct parse_events_error *err)
1343 bool zero = !!pmu->default_config;
1345 attr->type = pmu->type;
1346 return perf_pmu__config_terms(pmu->name, &pmu->format, attr,
1347 head_terms, zero, err);
1350 static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
1351 struct parse_events_term *term)
1353 struct perf_pmu_alias *alias;
1356 if (parse_events__is_hardcoded_term(term))
1359 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1360 if (term->val.num != 1)
1362 if (pmu_find_format(&pmu->format, term->config))
1364 name = term->config;
1365 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1366 if (strcasecmp(term->config, "event"))
1368 name = term->val.str;
1373 list_for_each_entry(alias, &pmu->aliases, list) {
1374 if (!strcasecmp(alias->name, name))
1381 static int check_info_data(struct perf_pmu_alias *alias,
1382 struct perf_pmu_info *info)
1385 * Only one term in event definition can
1386 * define unit, scale and snapshot, fail
1387 * if there's more than one.
1389 if ((info->unit && alias->unit[0]) ||
1390 (info->scale && alias->scale) ||
1391 (info->snapshot && alias->snapshot))
1395 info->unit = alias->unit;
1398 info->scale = alias->scale;
1400 if (alias->snapshot)
1401 info->snapshot = alias->snapshot;
1407 * Find alias in the terms list and replace it with the terms
1408 * defined for the alias
1410 int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
1411 struct perf_pmu_info *info)
1413 struct parse_events_term *term, *h;
1414 struct perf_pmu_alias *alias;
1417 info->per_pkg = false;
1420 * Mark unit and scale as not set
1421 * (different from default values, see below)
1425 info->snapshot = false;
1426 info->metric_expr = NULL;
1427 info->metric_name = NULL;
1429 list_for_each_entry_safe(term, h, head_terms, list) {
1430 alias = pmu_find_alias(pmu, term);
1433 ret = pmu_alias_terms(alias, &term->list);
1437 ret = check_info_data(alias, info);
1442 info->per_pkg = true;
1443 info->metric_expr = alias->metric_expr;
1444 info->metric_name = alias->metric_name;
1446 list_del_init(&term->list);
1447 parse_events_term__delete(term);
1451 * if no unit or scale found in aliases, then
1452 * set defaults as for evsel
1453 * unit cannot left to NULL
1455 if (info->unit == NULL)
1458 if (info->scale == 0.0)
1464 int perf_pmu__new_format(struct list_head *list, char *name,
1465 int config, unsigned long *bits)
1467 struct perf_pmu_format *format;
1469 format = zalloc(sizeof(*format));
1473 format->name = strdup(name);
1474 format->value = config;
1475 memcpy(format->bits, bits, sizeof(format->bits));
1477 list_add_tail(&format->list, list);
1481 void perf_pmu__set_format(unsigned long *bits, long from, long to)
1488 memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
1489 for (b = from; b <= to; b++)
1493 void perf_pmu__del_formats(struct list_head *formats)
1495 struct perf_pmu_format *fmt, *tmp;
1497 list_for_each_entry_safe(fmt, tmp, formats, list) {
1498 list_del(&fmt->list);
1504 static int sub_non_neg(int a, int b)
1511 static char *format_alias(char *buf, int len, const struct perf_pmu *pmu,
1512 const struct perf_pmu_alias *alias)
1514 struct parse_events_term *term;
1515 int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name);
1517 list_for_each_entry(term, &alias->terms, list) {
1518 if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
1519 used += snprintf(buf + used, sub_non_neg(len, used),
1520 ",%s=%s", term->config,
1524 if (sub_non_neg(len, used) > 0) {
1528 if (sub_non_neg(len, used) > 0) {
1532 buf[len - 1] = '\0';
1537 /** Struct for ordering events as output in perf list. */
1539 /** PMU for event. */
1540 const struct perf_pmu *pmu;
1542 * Optional event for name, desc, etc. If not present then this is a
1543 * selectable PMU and the event name is shown as "//".
1545 const struct perf_pmu_alias *event;
1546 /** Is the PMU for the CPU? */
1550 static int cmp_sevent(const void *a, const void *b)
1552 const struct sevent *as = a;
1553 const struct sevent *bs = b;
1554 const char *a_pmu_name, *b_pmu_name;
1555 const char *a_name = "//", *a_desc = NULL, *a_topic = "";
1556 const char *b_name = "//", *b_desc = NULL, *b_topic = "";
1560 a_name = as->event->name;
1561 a_desc = as->event->desc;
1562 a_topic = as->event->topic ?: "";
1565 b_name = bs->event->name;
1566 b_desc = bs->event->desc;
1567 b_topic = bs->event->topic ?: "";
1569 /* Put extra events last. */
1570 if (!!a_desc != !!b_desc)
1571 return !!a_desc - !!b_desc;
1573 /* Order by topics. */
1574 ret = strcmp(a_topic, b_topic);
1578 /* Order CPU core events to be first */
1579 if (as->is_cpu != bs->is_cpu)
1580 return as->is_cpu ? -1 : 1;
1582 /* Order by PMU name. */
1583 a_pmu_name = as->pmu->name ?: "";
1584 b_pmu_name = bs->pmu->name ?: "";
1585 ret = strcmp(a_pmu_name, b_pmu_name);
1589 /* Order by event name. */
1590 return strcmp(a_name, b_name);
1593 bool is_pmu_core(const char *name)
1595 return !strcmp(name, "cpu") || is_arm_pmu_core(name);
1598 static bool pmu_alias_is_duplicate(struct sevent *alias_a,
1599 struct sevent *alias_b)
1601 const char *a_pmu_name, *b_pmu_name;
1602 const char *a_name = alias_a->event ? alias_a->event->name : "//";
1603 const char *b_name = alias_b->event ? alias_b->event->name : "//";
1605 /* Different names -> never duplicates */
1606 if (strcmp(a_name, b_name))
1609 /* Don't remove duplicates for different PMUs */
1610 a_pmu_name = alias_a->pmu->name ?: "";
1611 b_pmu_name = alias_b->pmu->name ?: "";
1612 return strcmp(a_pmu_name, b_pmu_name) == 0;
1615 void print_pmu_events(const struct print_callbacks *print_cb, void *print_state)
1617 struct perf_pmu *pmu;
1618 struct perf_pmu_alias *event;
1622 struct sevent *aliases;
1626 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1627 list_for_each_entry(event, &pmu->aliases, list)
1629 if (pmu->selectable)
1632 aliases = zalloc(sizeof(struct sevent) * len);
1634 pr_err("FATAL: not enough memory to print PMU events\n");
1639 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1640 bool is_cpu = is_pmu_core(pmu->name) || perf_pmu__is_hybrid(pmu->name);
1642 list_for_each_entry(event, &pmu->aliases, list) {
1643 aliases[j].event = event;
1644 aliases[j].pmu = pmu;
1645 aliases[j].is_cpu = is_cpu;
1648 if (pmu->selectable) {
1649 aliases[j].event = NULL;
1650 aliases[j].pmu = pmu;
1651 aliases[j].is_cpu = is_cpu;
1656 qsort(aliases, len, sizeof(struct sevent), cmp_sevent);
1657 for (j = 0; j < len; j++) {
1658 const char *name, *alias = NULL, *scale_unit = NULL,
1659 *desc = NULL, *long_desc = NULL,
1660 *encoding_desc = NULL, *topic = NULL,
1661 *metric_name = NULL, *metric_expr = NULL;
1662 bool deprecated = false;
1665 /* Skip duplicates */
1666 if (j > 0 && pmu_alias_is_duplicate(&aliases[j], &aliases[j - 1]))
1669 if (!aliases[j].event) {
1670 /* A selectable event. */
1671 buf_used = snprintf(buf, sizeof(buf), "%s//", aliases[j].pmu->name) + 1;
1674 if (aliases[j].event->desc) {
1675 name = aliases[j].event->name;
1678 name = format_alias(buf, sizeof(buf), aliases[j].pmu,
1680 if (aliases[j].is_cpu) {
1682 name = aliases[j].event->name;
1684 buf_used = strlen(buf) + 1;
1686 if (strlen(aliases[j].event->unit) || aliases[j].event->scale != 1.0) {
1687 scale_unit = buf + buf_used;
1688 buf_used += snprintf(buf + buf_used, sizeof(buf) - buf_used,
1689 "%G%s", aliases[j].event->scale,
1690 aliases[j].event->unit) + 1;
1692 desc = aliases[j].event->desc;
1693 long_desc = aliases[j].event->long_desc;
1694 topic = aliases[j].event->topic;
1695 encoding_desc = buf + buf_used;
1696 buf_used += snprintf(buf + buf_used, sizeof(buf) - buf_used,
1697 "%s/%s/", aliases[j].pmu->name,
1698 aliases[j].event->str) + 1;
1699 metric_name = aliases[j].event->metric_name;
1700 metric_expr = aliases[j].event->metric_expr;
1701 deprecated = aliases[j].event->deprecated;
1703 print_cb->print_event(print_state,
1704 aliases[j].pmu->name,
1717 if (printed && pager_in_use())
1724 bool pmu_have_event(const char *pname, const char *name)
1726 struct perf_pmu *pmu;
1727 struct perf_pmu_alias *alias;
1730 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1731 if (strcmp(pname, pmu->name))
1733 list_for_each_entry(alias, &pmu->aliases, list)
1734 if (!strcmp(alias->name, name))
1740 static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
1742 char path[PATH_MAX];
1744 if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, name) ||
1745 !file_available(path))
1748 return fopen(path, "r");
1751 int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
1758 va_start(args, fmt);
1759 file = perf_pmu__open_file(pmu, name);
1761 ret = vfscanf(file, fmt, args);
1768 static int perf_pmu__new_caps(struct list_head *list, char *name, char *value)
1770 struct perf_pmu_caps *caps = zalloc(sizeof(*caps));
1775 caps->name = strdup(name);
1778 caps->value = strndup(value, strlen(value) - 1);
1781 list_add_tail(&caps->list, list);
1793 * Reading/parsing the given pmu capabilities, which should be located at:
1794 * /sys/bus/event_source/devices/<dev>/caps as sysfs group attributes.
1795 * Return the number of capabilities
1797 int perf_pmu__caps_parse(struct perf_pmu *pmu)
1800 char caps_path[PATH_MAX];
1802 struct dirent *evt_ent;
1804 if (pmu->caps_initialized)
1805 return pmu->nr_caps;
1809 if (!perf_pmu__pathname_scnprintf(caps_path, sizeof(caps_path), pmu->name, "caps"))
1812 if (stat(caps_path, &st) < 0) {
1813 pmu->caps_initialized = true;
1814 return 0; /* no error if caps does not exist */
1817 caps_dir = opendir(caps_path);
1821 while ((evt_ent = readdir(caps_dir)) != NULL) {
1822 char path[PATH_MAX + NAME_MAX + 1];
1823 char *name = evt_ent->d_name;
1827 if (!strcmp(name, ".") || !strcmp(name, ".."))
1830 snprintf(path, sizeof(path), "%s/%s", caps_path, name);
1832 file = fopen(path, "r");
1836 if (!fgets(value, sizeof(value), file) ||
1837 (perf_pmu__new_caps(&pmu->caps, name, value) < 0)) {
1848 pmu->caps_initialized = true;
1849 return pmu->nr_caps;
1852 void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config,
1855 struct perf_pmu_format *format;
1856 __u64 masks = 0, bits;
1860 list_for_each_entry(format, &pmu->format, list) {
1861 if (format->value != PERF_PMU_FORMAT_VALUE_CONFIG)
1864 for_each_set_bit(i, format->bits, PERF_PMU_FORMAT_BITS)
1869 * Kernel doesn't export any valid format bits.
1874 bits = config & ~masks;
1878 bitmap_scnprintf((unsigned long *)&bits, sizeof(bits) * 8, buf, sizeof(buf));
1880 pr_warning("WARNING: event '%s' not valid (bits %s of config "
1881 "'%llx' not supported by kernel)!\n",
1882 name ?: "N/A", buf, config);
1885 bool perf_pmu__has_hybrid(void)
1887 if (!hybrid_scanned) {
1888 hybrid_scanned = true;
1889 perf_pmu__scan(NULL);
1892 return !list_empty(&perf_pmu__hybrid_pmus);
1895 int perf_pmu__match(char *pattern, char *name, char *tok)
1900 if (fnmatch(pattern, name, 0))
1903 if (tok && !perf_pmu__valid_suffix(name, tok))
1909 int perf_pmu__cpus_match(struct perf_pmu *pmu, struct perf_cpu_map *cpus,
1910 struct perf_cpu_map **mcpus_ptr,
1911 struct perf_cpu_map **ucpus_ptr)
1913 struct perf_cpu_map *pmu_cpus = pmu->cpus;
1914 struct perf_cpu_map *matched_cpus, *unmatched_cpus;
1915 struct perf_cpu cpu;
1916 int i, matched_nr = 0, unmatched_nr = 0;
1918 matched_cpus = perf_cpu_map__default_new();
1922 unmatched_cpus = perf_cpu_map__default_new();
1923 if (!unmatched_cpus) {
1924 perf_cpu_map__put(matched_cpus);
1928 perf_cpu_map__for_each_cpu(cpu, i, cpus) {
1929 if (!perf_cpu_map__has(pmu_cpus, cpu))
1930 unmatched_cpus->map[unmatched_nr++] = cpu;
1932 matched_cpus->map[matched_nr++] = cpu;
1935 unmatched_cpus->nr = unmatched_nr;
1936 matched_cpus->nr = matched_nr;
1937 *mcpus_ptr = matched_cpus;
1938 *ucpus_ptr = unmatched_cpus;
1942 double __weak perf_pmu__cpu_slots_per_cycle(void)
1947 int perf_pmu__event_source_devices_scnprintf(char *pathname, size_t size)
1949 const char *sysfs = sysfs__mountpoint();
1953 return scnprintf(pathname, size, "%s/bus/event_source/devices/", sysfs);
1957 * Fill 'buf' with the path to a file or folder in 'pmu_name' in
1958 * sysfs. For example if pmu_name = "cs_etm" and 'filename' = "format"
1959 * then pathname will be filled with
1960 * "/sys/bus/event_source/devices/cs_etm/format"
1962 * Return 0 if the sysfs mountpoint couldn't be found or if no
1963 * characters were written.
1965 int perf_pmu__pathname_scnprintf(char *buf, size_t size,
1966 const char *pmu_name, const char *filename)
1968 char base_path[PATH_MAX];
1970 if (!perf_pmu__event_source_devices_scnprintf(base_path, sizeof(base_path)))
1972 return scnprintf(buf, size, "%s%s/%s", base_path, pmu_name, filename);