1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/list.h>
3 #include <linux/compiler.h>
13 #include <api/fs/fs.h>
17 #include "parse-events.h"
20 #include "pmu-events/pmu-events.h"
24 struct perf_pmu_format {
27 DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
28 struct list_head list;
31 #define EVENT_SOURCE_DEVICE_PATH "/bus/event_source/devices/"
33 int perf_pmu_parse(struct list_head *list, char *name);
34 extern FILE *perf_pmu_in;
36 static LIST_HEAD(pmus);
39 * Parse & process all the sysfs attributes located under
40 * the directory specified in 'dir' parameter.
42 int perf_pmu__format_parse(char *dir, struct list_head *head)
44 struct dirent *evt_ent;
48 format_dir = opendir(dir);
52 while (!ret && (evt_ent = readdir(format_dir))) {
54 char *name = evt_ent->d_name;
57 if (!strcmp(name, ".") || !strcmp(name, ".."))
60 snprintf(path, PATH_MAX, "%s/%s", dir, name);
63 file = fopen(path, "r");
68 ret = perf_pmu_parse(head, name);
77 * Reading/parsing the default pmu format definition, which should be
79 * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
81 static int pmu_format(const char *name, struct list_head *format)
85 const char *sysfs = sysfs__mountpoint();
90 snprintf(path, PATH_MAX,
91 "%s" EVENT_SOURCE_DEVICE_PATH "%s/format", sysfs, name);
93 if (stat(path, &st) < 0)
94 return 0; /* no error if format does not exist */
96 if (perf_pmu__format_parse(path, format))
102 static int convert_scale(const char *scale, char **end, double *sval)
108 * save current locale
110 lc = setlocale(LC_NUMERIC, NULL);
113 * The lc string may be allocated in static storage,
114 * so get a dynamic copy to make it survive setlocale
124 * force to C locale to ensure kernel
125 * scale string is converted correctly.
126 * kernel uses default C locale.
128 setlocale(LC_NUMERIC, "C");
130 *sval = strtod(scale, end);
134 setlocale(LC_NUMERIC, lc);
139 static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *name)
147 snprintf(path, PATH_MAX, "%s/%s.scale", dir, name);
149 fd = open(path, O_RDONLY);
153 if (fstat(fd, &st) < 0)
156 sret = read(fd, scale, sizeof(scale)-1);
160 if (scale[sret - 1] == '\n')
161 scale[sret - 1] = '\0';
165 ret = convert_scale(scale, NULL, &alias->scale);
171 static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *name)
177 snprintf(path, PATH_MAX, "%s/%s.unit", dir, name);
179 fd = open(path, O_RDONLY);
183 sret = read(fd, alias->unit, UNIT_MAX_LEN);
189 if (alias->unit[sret - 1] == '\n')
190 alias->unit[sret - 1] = '\0';
192 alias->unit[sret] = '\0';
197 alias->unit[0] = '\0';
202 perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, char *dir, char *name)
207 snprintf(path, PATH_MAX, "%s/%s.per-pkg", dir, name);
209 fd = open(path, O_RDONLY);
215 alias->per_pkg = true;
219 static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias,
220 char *dir, char *name)
225 snprintf(path, PATH_MAX, "%s/%s.snapshot", dir, name);
227 fd = open(path, O_RDONLY);
231 alias->snapshot = true;
236 static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name,
237 char *desc, char *val,
238 char *long_desc, char *topic,
239 char *unit, char *perpkg,
243 struct perf_pmu_alias *alias;
247 alias = malloc(sizeof(*alias));
251 INIT_LIST_HEAD(&alias->terms);
253 alias->unit[0] = '\0';
254 alias->per_pkg = false;
255 alias->snapshot = false;
257 ret = parse_events_terms(&alias->terms, val);
259 pr_err("Cannot parse alias %s: %d\n", val, ret);
264 alias->name = strdup(name);
267 * load unit name and scale if available
269 perf_pmu__parse_unit(alias, dir, name);
270 perf_pmu__parse_scale(alias, dir, name);
271 perf_pmu__parse_per_pkg(alias, dir, name);
272 perf_pmu__parse_snapshot(alias, dir, name);
275 alias->metric_expr = metric_expr ? strdup(metric_expr) : NULL;
276 alias->metric_name = metric_name ? strdup(metric_name): NULL;
277 alias->desc = desc ? strdup(desc) : NULL;
278 alias->long_desc = long_desc ? strdup(long_desc) :
279 desc ? strdup(desc) : NULL;
280 alias->topic = topic ? strdup(topic) : NULL;
282 if (convert_scale(unit, &unit, &alias->scale) < 0)
284 snprintf(alias->unit, sizeof(alias->unit), "%s", unit);
286 alias->per_pkg = perpkg && sscanf(perpkg, "%d", &num) == 1 && num == 1;
287 alias->str = strdup(val);
289 list_add_tail(&alias->list, list);
294 static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file)
299 ret = fread(buf, 1, sizeof(buf), file);
305 return __perf_pmu__new_alias(list, dir, name, NULL, buf, NULL, NULL, NULL,
309 static inline bool pmu_alias_info_file(char *name)
314 if (len > 5 && !strcmp(name + len - 5, ".unit"))
316 if (len > 6 && !strcmp(name + len - 6, ".scale"))
318 if (len > 8 && !strcmp(name + len - 8, ".per-pkg"))
320 if (len > 9 && !strcmp(name + len - 9, ".snapshot"))
327 * Process all the sysfs attributes located under the directory
328 * specified in 'dir' parameter.
330 static int pmu_aliases_parse(char *dir, struct list_head *head)
332 struct dirent *evt_ent;
335 event_dir = opendir(dir);
339 while ((evt_ent = readdir(event_dir))) {
341 char *name = evt_ent->d_name;
344 if (!strcmp(name, ".") || !strcmp(name, ".."))
348 * skip info files parsed in perf_pmu__new_alias()
350 if (pmu_alias_info_file(name))
353 snprintf(path, PATH_MAX, "%s/%s", dir, name);
355 file = fopen(path, "r");
357 pr_debug("Cannot open %s\n", path);
361 if (perf_pmu__new_alias(head, dir, name, file) < 0)
362 pr_debug("Cannot set up %s\n", name);
371 * Reading the pmu event aliases definition, which should be located at:
372 * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
374 static int pmu_aliases(const char *name, struct list_head *head)
378 const char *sysfs = sysfs__mountpoint();
383 snprintf(path, PATH_MAX,
384 "%s/bus/event_source/devices/%s/events", sysfs, name);
386 if (stat(path, &st) < 0)
387 return 0; /* no error if 'events' does not exist */
389 if (pmu_aliases_parse(path, head))
395 static int pmu_alias_terms(struct perf_pmu_alias *alias,
396 struct list_head *terms)
398 struct parse_events_term *term, *cloned;
402 list_for_each_entry(term, &alias->terms, list) {
403 ret = parse_events_term__clone(&cloned, term);
405 parse_events_terms__purge(&list);
408 list_add_tail(&cloned->list, &list);
410 list_splice(&list, terms);
415 * Reading/parsing the default pmu type value, which should be
417 * /sys/bus/event_source/devices/<dev>/type as sysfs attribute.
419 static int pmu_type(const char *name, __u32 *type)
425 const char *sysfs = sysfs__mountpoint();
430 snprintf(path, PATH_MAX,
431 "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name);
433 if (stat(path, &st) < 0)
436 file = fopen(path, "r");
440 if (1 != fscanf(file, "%u", type))
447 /* Add all pmus in sysfs to pmu list: */
448 static void pmu_read_sysfs(void)
453 const char *sysfs = sysfs__mountpoint();
458 snprintf(path, PATH_MAX,
459 "%s" EVENT_SOURCE_DEVICE_PATH, sysfs);
465 while ((dent = readdir(dir))) {
466 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
468 /* add to static LIST_HEAD(pmus): */
469 perf_pmu__find(dent->d_name);
475 static struct cpu_map *__pmu_cpumask(const char *path)
478 struct cpu_map *cpus;
480 file = fopen(path, "r");
484 cpus = cpu_map__read(file);
490 * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64)
491 * may have a "cpus" file.
493 #define CPUS_TEMPLATE_UNCORE "%s/bus/event_source/devices/%s/cpumask"
494 #define CPUS_TEMPLATE_CPU "%s/bus/event_source/devices/%s/cpus"
496 static struct cpu_map *pmu_cpumask(const char *name)
499 struct cpu_map *cpus;
500 const char *sysfs = sysfs__mountpoint();
501 const char *templates[] = {
502 CPUS_TEMPLATE_UNCORE,
506 const char **template;
511 for (template = templates; *template; template++) {
512 snprintf(path, PATH_MAX, *template, sysfs, name);
513 cpus = __pmu_cpumask(path);
521 static bool pmu_is_uncore(const char *name)
524 struct cpu_map *cpus;
525 const char *sysfs = sysfs__mountpoint();
527 snprintf(path, PATH_MAX, CPUS_TEMPLATE_UNCORE, sysfs, name);
528 cpus = __pmu_cpumask(path);
535 * Return the CPU id as a raw string.
537 * Each architecture should provide a more precise id string that
538 * can be use to match the architecture's "mapfile".
540 char * __weak get_cpuid_str(void)
545 static char *perf_pmu__getcpuid(void)
550 cpuid = getenv("PERF_CPUID");
552 cpuid = strdup(cpuid);
554 cpuid = get_cpuid_str();
559 pr_debug("Using CPUID %s\n", cpuid);
565 struct pmu_events_map *perf_pmu__find_map(void)
567 struct pmu_events_map *map;
568 char *cpuid = perf_pmu__getcpuid();
573 map = &pmu_events_map[i++];
579 if (!strcmp(map->cpuid, cpuid))
587 * From the pmu_events_map, find the table of PMU events that corresponds
588 * to the current running CPU. Then, add all PMU events from that table
591 static void pmu_add_cpu_aliases(struct list_head *head, const char *name)
594 struct pmu_events_map *map;
595 struct pmu_event *pe;
597 map = perf_pmu__find_map();
602 * Found a matching PMU events table. Create aliases
608 pe = &map->table[i++];
610 if (pe->metric_group || pe->metric_name)
615 pname = pe->pmu ? pe->pmu : "cpu";
616 if (strncmp(pname, name, strlen(pname)))
619 /* need type casts to override 'const' */
620 __perf_pmu__new_alias(head, NULL, (char *)pe->name,
621 (char *)pe->desc, (char *)pe->event,
622 (char *)pe->long_desc, (char *)pe->topic,
623 (char *)pe->unit, (char *)pe->perpkg,
624 (char *)pe->metric_expr,
625 (char *)pe->metric_name);
629 struct perf_event_attr * __weak
630 perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
635 static struct perf_pmu *pmu_lookup(const char *name)
637 struct perf_pmu *pmu;
643 * The pmu data we store & need consists of the pmu
644 * type value and format definitions. Load both right
647 if (pmu_format(name, &format))
651 * Check the type first to avoid unnecessary work.
653 if (pmu_type(name, &type))
656 if (pmu_aliases(name, &aliases))
659 pmu_add_cpu_aliases(&aliases, name);
660 pmu = zalloc(sizeof(*pmu));
664 pmu->cpus = pmu_cpumask(name);
666 pmu->is_uncore = pmu_is_uncore(name);
668 INIT_LIST_HEAD(&pmu->format);
669 INIT_LIST_HEAD(&pmu->aliases);
670 list_splice(&format, &pmu->format);
671 list_splice(&aliases, &pmu->aliases);
672 pmu->name = strdup(name);
674 list_add_tail(&pmu->list, &pmus);
676 pmu->default_config = perf_pmu__get_default_config(pmu);
681 static struct perf_pmu *pmu_find(const char *name)
683 struct perf_pmu *pmu;
685 list_for_each_entry(pmu, &pmus, list)
686 if (!strcmp(pmu->name, name))
692 struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
695 * pmu iterator: If pmu is NULL, we start at the begin,
696 * otherwise return the next pmu. Returns NULL on end.
700 pmu = list_prepare_entry(pmu, &pmus, list);
702 list_for_each_entry_continue(pmu, &pmus, list)
707 struct perf_pmu *perf_pmu__find(const char *name)
709 struct perf_pmu *pmu;
712 * Once PMU is loaded it stays in the list,
713 * so we keep us from multiple reading/parsing
714 * the pmu format definitions.
716 pmu = pmu_find(name);
720 return pmu_lookup(name);
723 static struct perf_pmu_format *
724 pmu_find_format(struct list_head *formats, const char *name)
726 struct perf_pmu_format *format;
728 list_for_each_entry(format, formats, list)
729 if (!strcmp(format->name, name))
735 __u64 perf_pmu__format_bits(struct list_head *formats, const char *name)
737 struct perf_pmu_format *format = pmu_find_format(formats, name);
744 for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
745 bits |= 1ULL << fbit;
751 * Sets value based on the format definition (format parameter)
752 * and unformated value (value parameter).
754 static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
757 unsigned long fbit, vbit;
759 for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
761 if (!test_bit(fbit, format))
764 if (value & (1llu << vbit++))
765 *v |= (1llu << fbit);
767 *v &= ~(1llu << fbit);
771 static __u64 pmu_format_max_value(const unsigned long *format)
776 for_each_set_bit(fbit, format, PERF_PMU_FORMAT_BITS)
783 * Term is a string term, and might be a param-term. Try to look up it's value
784 * in the remaining terms.
785 * - We have a term like "base-or-format-term=param-term",
786 * - We need to find the value supplied for "param-term" (with param-term named
787 * in a config string) later on in the term list.
789 static int pmu_resolve_param_term(struct parse_events_term *term,
790 struct list_head *head_terms,
793 struct parse_events_term *t;
795 list_for_each_entry(t, head_terms, list) {
796 if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
797 if (!strcmp(t->config, term->config)) {
806 printf("Required parameter '%s' not specified\n", term->config);
811 static char *pmu_formats_string(struct list_head *formats)
813 struct perf_pmu_format *format;
815 struct strbuf buf = STRBUF_INIT;
821 /* sysfs exported terms */
822 list_for_each_entry(format, formats, list)
823 if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
826 str = strbuf_detach(&buf, NULL);
828 strbuf_release(&buf);
834 * Setup one of config[12] attr members based on the
835 * user input data - term parameter.
837 static int pmu_config_term(struct list_head *formats,
838 struct perf_event_attr *attr,
839 struct parse_events_term *term,
840 struct list_head *head_terms,
841 bool zero, struct parse_events_error *err)
843 struct perf_pmu_format *format;
848 * If this is a parameter we've already used for parameterized-eval,
849 * skip it in normal eval.
855 * Hardcoded terms should be already in, so nothing
856 * to be done for them.
858 if (parse_events__is_hardcoded_term(term))
861 format = pmu_find_format(formats, term->config);
864 printf("Invalid event/parameter '%s'\n", term->config);
866 char *pmu_term = pmu_formats_string(formats);
868 err->idx = term->err_term;
869 err->str = strdup("unknown term");
870 err->help = parse_events_formats_error_string(pmu_term);
876 switch (format->value) {
877 case PERF_PMU_FORMAT_VALUE_CONFIG:
880 case PERF_PMU_FORMAT_VALUE_CONFIG1:
883 case PERF_PMU_FORMAT_VALUE_CONFIG2:
891 * Either directly use a numeric term, or try to translate string terms
892 * using event parameters.
894 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
895 if (term->no_value &&
896 bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) {
898 err->idx = term->err_val;
899 err->str = strdup("no value assigned for term");
905 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
906 if (strcmp(term->val.str, "?")) {
908 pr_info("Invalid sysfs entry %s=%s\n",
909 term->config, term->val.str);
912 err->idx = term->err_val;
913 err->str = strdup("expected numeric value");
918 if (pmu_resolve_param_term(term, head_terms, &val))
923 max_val = pmu_format_max_value(format->bits);
926 err->idx = term->err_val;
927 if (asprintf(&err->str,
928 "value too big for format, maximum is %llu",
929 (unsigned long long)max_val) < 0)
930 err->str = strdup("value too big for format");
934 * Assume we don't care if !err, in which case the value will be
935 * silently truncated.
939 pmu_format_value(format->bits, val, vp, zero);
943 int perf_pmu__config_terms(struct list_head *formats,
944 struct perf_event_attr *attr,
945 struct list_head *head_terms,
946 bool zero, struct parse_events_error *err)
948 struct parse_events_term *term;
950 list_for_each_entry(term, head_terms, list) {
951 if (pmu_config_term(formats, attr, term, head_terms,
960 * Configures event's 'attr' parameter based on the:
961 * 1) users input - specified in terms parameter
962 * 2) pmu format definitions - specified by pmu parameter
964 int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
965 struct list_head *head_terms,
966 struct parse_events_error *err)
968 bool zero = !!pmu->default_config;
970 attr->type = pmu->type;
971 return perf_pmu__config_terms(&pmu->format, attr, head_terms,
975 static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
976 struct parse_events_term *term)
978 struct perf_pmu_alias *alias;
981 if (parse_events__is_hardcoded_term(term))
984 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
985 if (term->val.num != 1)
987 if (pmu_find_format(&pmu->format, term->config))
990 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
991 if (strcasecmp(term->config, "event"))
993 name = term->val.str;
998 list_for_each_entry(alias, &pmu->aliases, list) {
999 if (!strcasecmp(alias->name, name))
1006 static int check_info_data(struct perf_pmu_alias *alias,
1007 struct perf_pmu_info *info)
1010 * Only one term in event definition can
1011 * define unit, scale and snapshot, fail
1012 * if there's more than one.
1014 if ((info->unit && alias->unit[0]) ||
1015 (info->scale && alias->scale) ||
1016 (info->snapshot && alias->snapshot))
1020 info->unit = alias->unit;
1023 info->scale = alias->scale;
1025 if (alias->snapshot)
1026 info->snapshot = alias->snapshot;
1032 * Find alias in the terms list and replace it with the terms
1033 * defined for the alias
1035 int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
1036 struct perf_pmu_info *info)
1038 struct parse_events_term *term, *h;
1039 struct perf_pmu_alias *alias;
1042 info->per_pkg = false;
1045 * Mark unit and scale as not set
1046 * (different from default values, see below)
1050 info->snapshot = false;
1051 info->metric_expr = NULL;
1052 info->metric_name = NULL;
1054 list_for_each_entry_safe(term, h, head_terms, list) {
1055 alias = pmu_find_alias(pmu, term);
1058 ret = pmu_alias_terms(alias, &term->list);
1062 ret = check_info_data(alias, info);
1067 info->per_pkg = true;
1068 info->metric_expr = alias->metric_expr;
1069 info->metric_name = alias->metric_name;
1071 list_del(&term->list);
1076 * if no unit or scale foundin aliases, then
1077 * set defaults as for evsel
1078 * unit cannot left to NULL
1080 if (info->unit == NULL)
1083 if (info->scale == 0.0)
1089 int perf_pmu__new_format(struct list_head *list, char *name,
1090 int config, unsigned long *bits)
1092 struct perf_pmu_format *format;
1094 format = zalloc(sizeof(*format));
1098 format->name = strdup(name);
1099 format->value = config;
1100 memcpy(format->bits, bits, sizeof(format->bits));
1102 list_add_tail(&format->list, list);
1106 void perf_pmu__set_format(unsigned long *bits, long from, long to)
1113 memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
1114 for (b = from; b <= to; b++)
1118 static int sub_non_neg(int a, int b)
1125 static char *format_alias(char *buf, int len, struct perf_pmu *pmu,
1126 struct perf_pmu_alias *alias)
1128 struct parse_events_term *term;
1129 int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name);
1131 list_for_each_entry(term, &alias->terms, list) {
1132 if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
1133 used += snprintf(buf + used, sub_non_neg(len, used),
1134 ",%s=%s", term->config,
1138 if (sub_non_neg(len, used) > 0) {
1142 if (sub_non_neg(len, used) > 0) {
1146 buf[len - 1] = '\0';
1151 static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu,
1152 struct perf_pmu_alias *alias)
1154 snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name);
1168 static int cmp_sevent(const void *a, const void *b)
1170 const struct sevent *as = a;
1171 const struct sevent *bs = b;
1173 /* Put extra events last */
1174 if (!!as->desc != !!bs->desc)
1175 return !!as->desc - !!bs->desc;
1176 if (as->topic && bs->topic) {
1177 int n = strcmp(as->topic, bs->topic);
1182 return strcmp(as->name, bs->name);
1185 static void wordwrap(char *s, int start, int max, int corr)
1191 int wlen = strcspn(s, " \t");
1193 if (column + wlen >= max && column > start) {
1194 printf("\n%*s", start, "");
1195 column = start + corr;
1197 n = printf("%s%.*s", column > start ? " " : "", wlen, s);
1206 void print_pmu_events(const char *event_glob, bool name_only, bool quiet_flag,
1207 bool long_desc, bool details_flag)
1209 struct perf_pmu *pmu;
1210 struct perf_pmu_alias *alias;
1214 struct sevent *aliases;
1216 int columns = pager_get_columns();
1221 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1222 list_for_each_entry(alias, &pmu->aliases, list)
1224 if (pmu->selectable)
1227 aliases = zalloc(sizeof(struct sevent) * len);
1232 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1233 list_for_each_entry(alias, &pmu->aliases, list) {
1234 char *name = alias->desc ? alias->name :
1235 format_alias(buf, sizeof(buf), pmu, alias);
1236 bool is_cpu = !strcmp(pmu->name, "cpu");
1238 if (event_glob != NULL &&
1239 !(strglobmatch_nocase(name, event_glob) ||
1240 (!is_cpu && strglobmatch_nocase(alias->name,
1243 strglobmatch_nocase(alias->topic, event_glob))))
1246 if (is_cpu && !name_only && !alias->desc)
1247 name = format_alias_or(buf, sizeof(buf), pmu, alias);
1249 aliases[j].name = name;
1250 if (is_cpu && !name_only && !alias->desc)
1251 aliases[j].name = format_alias_or(buf,
1254 aliases[j].name = strdup(aliases[j].name);
1255 if (!aliases[j].name)
1258 aliases[j].desc = long_desc ? alias->long_desc :
1260 aliases[j].topic = alias->topic;
1261 aliases[j].str = alias->str;
1262 aliases[j].pmu = pmu->name;
1263 aliases[j].metric_expr = alias->metric_expr;
1264 aliases[j].metric_name = alias->metric_name;
1267 if (pmu->selectable &&
1268 (event_glob == NULL || strglobmatch(pmu->name, event_glob))) {
1270 if (asprintf(&s, "%s//", pmu->name) < 0)
1272 aliases[j].name = s;
1277 qsort(aliases, len, sizeof(struct sevent), cmp_sevent);
1278 for (j = 0; j < len; j++) {
1279 /* Skip duplicates */
1280 if (j > 0 && !strcmp(aliases[j].name, aliases[j - 1].name))
1283 printf("%s ", aliases[j].name);
1286 if (aliases[j].desc && !quiet_flag) {
1289 if (aliases[j].topic && (!topic ||
1290 strcmp(topic, aliases[j].topic))) {
1291 printf("%s%s:\n", topic ? "\n" : "",
1293 topic = aliases[j].topic;
1295 printf(" %-50s\n", aliases[j].name);
1296 printf("%*s", 8, "[");
1297 wordwrap(aliases[j].desc, 8, columns, 0);
1300 printf("%*s%s/%s/ ", 8, "", aliases[j].pmu, aliases[j].str);
1301 if (aliases[j].metric_name)
1302 printf(" MetricName: %s", aliases[j].metric_name);
1303 if (aliases[j].metric_expr)
1304 printf(" MetricExpr: %s", aliases[j].metric_expr);
1308 printf(" %-50s [Kernel PMU event]\n", aliases[j].name);
1311 if (printed && pager_in_use())
1314 for (j = 0; j < len; j++)
1315 zfree(&aliases[j].name);
1320 printf("FATAL: not enough memory to print PMU events\n");
1325 bool pmu_have_event(const char *pname, const char *name)
1327 struct perf_pmu *pmu;
1328 struct perf_pmu_alias *alias;
1331 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1332 if (strcmp(pname, pmu->name))
1334 list_for_each_entry(alias, &pmu->aliases, list)
1335 if (!strcmp(alias->name, name))
1341 static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
1344 char path[PATH_MAX];
1347 sysfs = sysfs__mountpoint();
1351 snprintf(path, PATH_MAX,
1352 "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name);
1354 if (stat(path, &st) < 0)
1357 return fopen(path, "r");
1360 int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
1367 va_start(args, fmt);
1368 file = perf_pmu__open_file(pmu, name);
1370 ret = vfscanf(file, fmt, args);