1 #include <linux/list.h>
2 #include <linux/compiler.h>
11 #include <api/fs/fs.h>
15 #include "parse-events.h"
18 #include "pmu-events/pmu-events.h"
22 struct perf_pmu_format {
25 DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
26 struct list_head list;
29 #define EVENT_SOURCE_DEVICE_PATH "/bus/event_source/devices/"
31 int perf_pmu_parse(struct list_head *list, char *name);
32 extern FILE *perf_pmu_in;
34 static LIST_HEAD(pmus);
37 * Parse & process all the sysfs attributes located under
38 * the directory specified in 'dir' parameter.
40 int perf_pmu__format_parse(char *dir, struct list_head *head)
42 struct dirent *evt_ent;
46 format_dir = opendir(dir);
50 while (!ret && (evt_ent = readdir(format_dir))) {
52 char *name = evt_ent->d_name;
55 if (!strcmp(name, ".") || !strcmp(name, ".."))
58 snprintf(path, PATH_MAX, "%s/%s", dir, name);
61 file = fopen(path, "r");
66 ret = perf_pmu_parse(head, name);
75 * Reading/parsing the default pmu format definition, which should be
77 * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
79 static int pmu_format(const char *name, struct list_head *format)
83 const char *sysfs = sysfs__mountpoint();
88 snprintf(path, PATH_MAX,
89 "%s" EVENT_SOURCE_DEVICE_PATH "%s/format", sysfs, name);
91 if (stat(path, &st) < 0)
92 return 0; /* no error if format does not exist */
94 if (perf_pmu__format_parse(path, format))
100 static int convert_scale(const char *scale, char **end, double *sval)
106 * save current locale
108 lc = setlocale(LC_NUMERIC, NULL);
111 * The lc string may be allocated in static storage,
112 * so get a dynamic copy to make it survive setlocale
122 * force to C locale to ensure kernel
123 * scale string is converted correctly.
124 * kernel uses default C locale.
126 setlocale(LC_NUMERIC, "C");
128 *sval = strtod(scale, end);
132 setlocale(LC_NUMERIC, lc);
137 static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *name)
145 snprintf(path, PATH_MAX, "%s/%s.scale", dir, name);
147 fd = open(path, O_RDONLY);
151 if (fstat(fd, &st) < 0)
154 sret = read(fd, scale, sizeof(scale)-1);
158 if (scale[sret - 1] == '\n')
159 scale[sret - 1] = '\0';
163 ret = convert_scale(scale, NULL, &alias->scale);
169 static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *name)
175 snprintf(path, PATH_MAX, "%s/%s.unit", dir, name);
177 fd = open(path, O_RDONLY);
181 sret = read(fd, alias->unit, UNIT_MAX_LEN);
187 if (alias->unit[sret - 1] == '\n')
188 alias->unit[sret - 1] = '\0';
190 alias->unit[sret] = '\0';
195 alias->unit[0] = '\0';
200 perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, char *dir, char *name)
205 snprintf(path, PATH_MAX, "%s/%s.per-pkg", dir, name);
207 fd = open(path, O_RDONLY);
213 alias->per_pkg = true;
217 static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias,
218 char *dir, char *name)
223 snprintf(path, PATH_MAX, "%s/%s.snapshot", dir, name);
225 fd = open(path, O_RDONLY);
229 alias->snapshot = true;
234 static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name,
235 char *desc, char *val,
236 char *long_desc, char *topic,
237 char *unit, char *perpkg,
241 struct perf_pmu_alias *alias;
245 alias = malloc(sizeof(*alias));
249 INIT_LIST_HEAD(&alias->terms);
251 alias->unit[0] = '\0';
252 alias->per_pkg = false;
253 alias->snapshot = false;
255 ret = parse_events_terms(&alias->terms, val);
257 pr_err("Cannot parse alias %s: %d\n", val, ret);
262 alias->name = strdup(name);
265 * load unit name and scale if available
267 perf_pmu__parse_unit(alias, dir, name);
268 perf_pmu__parse_scale(alias, dir, name);
269 perf_pmu__parse_per_pkg(alias, dir, name);
270 perf_pmu__parse_snapshot(alias, dir, name);
273 alias->metric_expr = metric_expr ? strdup(metric_expr) : NULL;
274 alias->metric_name = metric_name ? strdup(metric_name): NULL;
275 alias->desc = desc ? strdup(desc) : NULL;
276 alias->long_desc = long_desc ? strdup(long_desc) :
277 desc ? strdup(desc) : NULL;
278 alias->topic = topic ? strdup(topic) : NULL;
280 if (convert_scale(unit, &unit, &alias->scale) < 0)
282 snprintf(alias->unit, sizeof(alias->unit), "%s", unit);
284 alias->per_pkg = perpkg && sscanf(perpkg, "%d", &num) == 1 && num == 1;
285 alias->str = strdup(val);
287 list_add_tail(&alias->list, list);
292 static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file)
297 ret = fread(buf, 1, sizeof(buf), file);
303 return __perf_pmu__new_alias(list, dir, name, NULL, buf, NULL, NULL, NULL,
307 static inline bool pmu_alias_info_file(char *name)
312 if (len > 5 && !strcmp(name + len - 5, ".unit"))
314 if (len > 6 && !strcmp(name + len - 6, ".scale"))
316 if (len > 8 && !strcmp(name + len - 8, ".per-pkg"))
318 if (len > 9 && !strcmp(name + len - 9, ".snapshot"))
325 * Process all the sysfs attributes located under the directory
326 * specified in 'dir' parameter.
328 static int pmu_aliases_parse(char *dir, struct list_head *head)
330 struct dirent *evt_ent;
333 event_dir = opendir(dir);
337 while ((evt_ent = readdir(event_dir))) {
339 char *name = evt_ent->d_name;
342 if (!strcmp(name, ".") || !strcmp(name, ".."))
346 * skip info files parsed in perf_pmu__new_alias()
348 if (pmu_alias_info_file(name))
351 snprintf(path, PATH_MAX, "%s/%s", dir, name);
353 file = fopen(path, "r");
355 pr_debug("Cannot open %s\n", path);
359 if (perf_pmu__new_alias(head, dir, name, file) < 0)
360 pr_debug("Cannot set up %s\n", name);
369 * Reading the pmu event aliases definition, which should be located at:
370 * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
372 static int pmu_aliases(const char *name, struct list_head *head)
376 const char *sysfs = sysfs__mountpoint();
381 snprintf(path, PATH_MAX,
382 "%s/bus/event_source/devices/%s/events", sysfs, name);
384 if (stat(path, &st) < 0)
385 return 0; /* no error if 'events' does not exist */
387 if (pmu_aliases_parse(path, head))
393 static int pmu_alias_terms(struct perf_pmu_alias *alias,
394 struct list_head *terms)
396 struct parse_events_term *term, *cloned;
400 list_for_each_entry(term, &alias->terms, list) {
401 ret = parse_events_term__clone(&cloned, term);
403 parse_events_terms__purge(&list);
406 list_add_tail(&cloned->list, &list);
408 list_splice(&list, terms);
413 * Reading/parsing the default pmu type value, which should be
415 * /sys/bus/event_source/devices/<dev>/type as sysfs attribute.
417 static int pmu_type(const char *name, __u32 *type)
423 const char *sysfs = sysfs__mountpoint();
428 snprintf(path, PATH_MAX,
429 "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name);
431 if (stat(path, &st) < 0)
434 file = fopen(path, "r");
438 if (1 != fscanf(file, "%u", type))
445 /* Add all pmus in sysfs to pmu list: */
446 static void pmu_read_sysfs(void)
451 const char *sysfs = sysfs__mountpoint();
456 snprintf(path, PATH_MAX,
457 "%s" EVENT_SOURCE_DEVICE_PATH, sysfs);
463 while ((dent = readdir(dir))) {
464 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
466 /* add to static LIST_HEAD(pmus): */
467 perf_pmu__find(dent->d_name);
473 static struct cpu_map *__pmu_cpumask(const char *path)
476 struct cpu_map *cpus;
478 file = fopen(path, "r");
482 cpus = cpu_map__read(file);
488 * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64)
489 * may have a "cpus" file.
491 #define CPUS_TEMPLATE_UNCORE "%s/bus/event_source/devices/%s/cpumask"
492 #define CPUS_TEMPLATE_CPU "%s/bus/event_source/devices/%s/cpus"
494 static struct cpu_map *pmu_cpumask(const char *name)
497 struct cpu_map *cpus;
498 const char *sysfs = sysfs__mountpoint();
499 const char *templates[] = {
500 CPUS_TEMPLATE_UNCORE,
504 const char **template;
509 for (template = templates; *template; template++) {
510 snprintf(path, PATH_MAX, *template, sysfs, name);
511 cpus = __pmu_cpumask(path);
519 static bool pmu_is_uncore(const char *name)
522 struct cpu_map *cpus;
523 const char *sysfs = sysfs__mountpoint();
525 snprintf(path, PATH_MAX, CPUS_TEMPLATE_UNCORE, sysfs, name);
526 cpus = __pmu_cpumask(path);
533 * Return the CPU id as a raw string.
535 * Each architecture should provide a more precise id string that
536 * can be use to match the architecture's "mapfile".
538 char * __weak get_cpuid_str(void)
544 * From the pmu_events_map, find the table of PMU events that corresponds
545 * to the current running CPU. Then, add all PMU events from that table
548 static void pmu_add_cpu_aliases(struct list_head *head, const char *name)
551 struct pmu_events_map *map;
552 struct pmu_event *pe;
556 cpuid = getenv("PERF_CPUID");
558 cpuid = strdup(cpuid);
560 cpuid = get_cpuid_str();
565 pr_debug("Using CPUID %s\n", cpuid);
571 map = &pmu_events_map[i++];
575 if (!strcmp(map->cpuid, cpuid))
580 * Found a matching PMU events table. Create aliases
586 pe = &map->table[i++];
590 pname = pe->pmu ? pe->pmu : "cpu";
591 if (strncmp(pname, name, strlen(pname)))
594 /* need type casts to override 'const' */
595 __perf_pmu__new_alias(head, NULL, (char *)pe->name,
596 (char *)pe->desc, (char *)pe->event,
597 (char *)pe->long_desc, (char *)pe->topic,
598 (char *)pe->unit, (char *)pe->perpkg,
599 (char *)pe->metric_expr,
600 (char *)pe->metric_name);
607 struct perf_event_attr * __weak
608 perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
613 static struct perf_pmu *pmu_lookup(const char *name)
615 struct perf_pmu *pmu;
621 * The pmu data we store & need consists of the pmu
622 * type value and format definitions. Load both right
625 if (pmu_format(name, &format))
629 * Check the type first to avoid unnecessary work.
631 if (pmu_type(name, &type))
634 if (pmu_aliases(name, &aliases))
637 pmu_add_cpu_aliases(&aliases, name);
638 pmu = zalloc(sizeof(*pmu));
642 pmu->cpus = pmu_cpumask(name);
644 pmu->is_uncore = pmu_is_uncore(name);
646 INIT_LIST_HEAD(&pmu->format);
647 INIT_LIST_HEAD(&pmu->aliases);
648 list_splice(&format, &pmu->format);
649 list_splice(&aliases, &pmu->aliases);
650 pmu->name = strdup(name);
652 list_add_tail(&pmu->list, &pmus);
654 pmu->default_config = perf_pmu__get_default_config(pmu);
659 static struct perf_pmu *pmu_find(const char *name)
661 struct perf_pmu *pmu;
663 list_for_each_entry(pmu, &pmus, list)
664 if (!strcmp(pmu->name, name))
670 struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
673 * pmu iterator: If pmu is NULL, we start at the begin,
674 * otherwise return the next pmu. Returns NULL on end.
678 pmu = list_prepare_entry(pmu, &pmus, list);
680 list_for_each_entry_continue(pmu, &pmus, list)
685 struct perf_pmu *perf_pmu__find(const char *name)
687 struct perf_pmu *pmu;
690 * Once PMU is loaded it stays in the list,
691 * so we keep us from multiple reading/parsing
692 * the pmu format definitions.
694 pmu = pmu_find(name);
698 return pmu_lookup(name);
701 static struct perf_pmu_format *
702 pmu_find_format(struct list_head *formats, const char *name)
704 struct perf_pmu_format *format;
706 list_for_each_entry(format, formats, list)
707 if (!strcmp(format->name, name))
713 __u64 perf_pmu__format_bits(struct list_head *formats, const char *name)
715 struct perf_pmu_format *format = pmu_find_format(formats, name);
722 for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
723 bits |= 1ULL << fbit;
729 * Sets value based on the format definition (format parameter)
730 * and unformated value (value parameter).
732 static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
735 unsigned long fbit, vbit;
737 for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
739 if (!test_bit(fbit, format))
742 if (value & (1llu << vbit++))
743 *v |= (1llu << fbit);
745 *v &= ~(1llu << fbit);
749 static __u64 pmu_format_max_value(const unsigned long *format)
754 for_each_set_bit(fbit, format, PERF_PMU_FORMAT_BITS)
761 * Term is a string term, and might be a param-term. Try to look up it's value
762 * in the remaining terms.
763 * - We have a term like "base-or-format-term=param-term",
764 * - We need to find the value supplied for "param-term" (with param-term named
765 * in a config string) later on in the term list.
767 static int pmu_resolve_param_term(struct parse_events_term *term,
768 struct list_head *head_terms,
771 struct parse_events_term *t;
773 list_for_each_entry(t, head_terms, list) {
774 if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
775 if (!strcmp(t->config, term->config)) {
784 printf("Required parameter '%s' not specified\n", term->config);
789 static char *pmu_formats_string(struct list_head *formats)
791 struct perf_pmu_format *format;
793 struct strbuf buf = STRBUF_INIT;
799 /* sysfs exported terms */
800 list_for_each_entry(format, formats, list)
801 if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
804 str = strbuf_detach(&buf, NULL);
806 strbuf_release(&buf);
812 * Setup one of config[12] attr members based on the
813 * user input data - term parameter.
815 static int pmu_config_term(struct list_head *formats,
816 struct perf_event_attr *attr,
817 struct parse_events_term *term,
818 struct list_head *head_terms,
819 bool zero, struct parse_events_error *err)
821 struct perf_pmu_format *format;
826 * If this is a parameter we've already used for parameterized-eval,
827 * skip it in normal eval.
833 * Hardcoded terms should be already in, so nothing
834 * to be done for them.
836 if (parse_events__is_hardcoded_term(term))
839 format = pmu_find_format(formats, term->config);
842 printf("Invalid event/parameter '%s'\n", term->config);
844 char *pmu_term = pmu_formats_string(formats);
846 err->idx = term->err_term;
847 err->str = strdup("unknown term");
848 err->help = parse_events_formats_error_string(pmu_term);
854 switch (format->value) {
855 case PERF_PMU_FORMAT_VALUE_CONFIG:
858 case PERF_PMU_FORMAT_VALUE_CONFIG1:
861 case PERF_PMU_FORMAT_VALUE_CONFIG2:
869 * Either directly use a numeric term, or try to translate string terms
870 * using event parameters.
872 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
873 if (term->no_value &&
874 bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) {
876 err->idx = term->err_val;
877 err->str = strdup("no value assigned for term");
883 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
884 if (strcmp(term->val.str, "?")) {
886 pr_info("Invalid sysfs entry %s=%s\n",
887 term->config, term->val.str);
890 err->idx = term->err_val;
891 err->str = strdup("expected numeric value");
896 if (pmu_resolve_param_term(term, head_terms, &val))
901 max_val = pmu_format_max_value(format->bits);
904 err->idx = term->err_val;
905 if (asprintf(&err->str,
906 "value too big for format, maximum is %llu",
907 (unsigned long long)max_val) < 0)
908 err->str = strdup("value too big for format");
912 * Assume we don't care if !err, in which case the value will be
913 * silently truncated.
917 pmu_format_value(format->bits, val, vp, zero);
921 int perf_pmu__config_terms(struct list_head *formats,
922 struct perf_event_attr *attr,
923 struct list_head *head_terms,
924 bool zero, struct parse_events_error *err)
926 struct parse_events_term *term;
928 list_for_each_entry(term, head_terms, list) {
929 if (pmu_config_term(formats, attr, term, head_terms,
938 * Configures event's 'attr' parameter based on the:
939 * 1) users input - specified in terms parameter
940 * 2) pmu format definitions - specified by pmu parameter
942 int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
943 struct list_head *head_terms,
944 struct parse_events_error *err)
946 bool zero = !!pmu->default_config;
948 attr->type = pmu->type;
949 return perf_pmu__config_terms(&pmu->format, attr, head_terms,
953 static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
954 struct parse_events_term *term)
956 struct perf_pmu_alias *alias;
959 if (parse_events__is_hardcoded_term(term))
962 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
963 if (term->val.num != 1)
965 if (pmu_find_format(&pmu->format, term->config))
968 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
969 if (strcasecmp(term->config, "event"))
971 name = term->val.str;
976 list_for_each_entry(alias, &pmu->aliases, list) {
977 if (!strcasecmp(alias->name, name))
984 static int check_info_data(struct perf_pmu_alias *alias,
985 struct perf_pmu_info *info)
988 * Only one term in event definition can
989 * define unit, scale and snapshot, fail
990 * if there's more than one.
992 if ((info->unit && alias->unit[0]) ||
993 (info->scale && alias->scale) ||
994 (info->snapshot && alias->snapshot))
998 info->unit = alias->unit;
1001 info->scale = alias->scale;
1003 if (alias->snapshot)
1004 info->snapshot = alias->snapshot;
1010 * Find alias in the terms list and replace it with the terms
1011 * defined for the alias
1013 int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
1014 struct perf_pmu_info *info)
1016 struct parse_events_term *term, *h;
1017 struct perf_pmu_alias *alias;
1020 info->per_pkg = false;
1023 * Mark unit and scale as not set
1024 * (different from default values, see below)
1028 info->snapshot = false;
1029 info->metric_expr = NULL;
1030 info->metric_name = NULL;
1032 list_for_each_entry_safe(term, h, head_terms, list) {
1033 alias = pmu_find_alias(pmu, term);
1036 ret = pmu_alias_terms(alias, &term->list);
1040 ret = check_info_data(alias, info);
1045 info->per_pkg = true;
1046 info->metric_expr = alias->metric_expr;
1047 info->metric_name = alias->metric_name;
1049 list_del(&term->list);
1054 * if no unit or scale foundin aliases, then
1055 * set defaults as for evsel
1056 * unit cannot left to NULL
1058 if (info->unit == NULL)
1061 if (info->scale == 0.0)
1067 int perf_pmu__new_format(struct list_head *list, char *name,
1068 int config, unsigned long *bits)
1070 struct perf_pmu_format *format;
1072 format = zalloc(sizeof(*format));
1076 format->name = strdup(name);
1077 format->value = config;
1078 memcpy(format->bits, bits, sizeof(format->bits));
1080 list_add_tail(&format->list, list);
1084 void perf_pmu__set_format(unsigned long *bits, long from, long to)
1091 memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
1092 for (b = from; b <= to; b++)
1096 static int sub_non_neg(int a, int b)
1103 static char *format_alias(char *buf, int len, struct perf_pmu *pmu,
1104 struct perf_pmu_alias *alias)
1106 struct parse_events_term *term;
1107 int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name);
1109 list_for_each_entry(term, &alias->terms, list) {
1110 if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
1111 used += snprintf(buf + used, sub_non_neg(len, used),
1112 ",%s=%s", term->config,
1116 if (sub_non_neg(len, used) > 0) {
1120 if (sub_non_neg(len, used) > 0) {
1124 buf[len - 1] = '\0';
1129 static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu,
1130 struct perf_pmu_alias *alias)
1132 snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name);
1146 static int cmp_sevent(const void *a, const void *b)
1148 const struct sevent *as = a;
1149 const struct sevent *bs = b;
1151 /* Put extra events last */
1152 if (!!as->desc != !!bs->desc)
1153 return !!as->desc - !!bs->desc;
1154 if (as->topic && bs->topic) {
1155 int n = strcmp(as->topic, bs->topic);
1160 return strcmp(as->name, bs->name);
1163 static void wordwrap(char *s, int start, int max, int corr)
1169 int wlen = strcspn(s, " \t");
1171 if (column + wlen >= max && column > start) {
1172 printf("\n%*s", start, "");
1173 column = start + corr;
1175 n = printf("%s%.*s", column > start ? " " : "", wlen, s);
1184 void print_pmu_events(const char *event_glob, bool name_only, bool quiet_flag,
1185 bool long_desc, bool details_flag)
1187 struct perf_pmu *pmu;
1188 struct perf_pmu_alias *alias;
1192 struct sevent *aliases;
1194 int columns = pager_get_columns();
1199 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1200 list_for_each_entry(alias, &pmu->aliases, list)
1202 if (pmu->selectable)
1205 aliases = zalloc(sizeof(struct sevent) * len);
1210 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1211 list_for_each_entry(alias, &pmu->aliases, list) {
1212 char *name = alias->desc ? alias->name :
1213 format_alias(buf, sizeof(buf), pmu, alias);
1214 bool is_cpu = !strcmp(pmu->name, "cpu");
1216 if (event_glob != NULL &&
1217 !(strglobmatch_nocase(name, event_glob) ||
1218 (!is_cpu && strglobmatch_nocase(alias->name,
1221 strglobmatch_nocase(alias->topic, event_glob))))
1224 if (is_cpu && !name_only && !alias->desc)
1225 name = format_alias_or(buf, sizeof(buf), pmu, alias);
1227 aliases[j].name = name;
1228 if (is_cpu && !name_only && !alias->desc)
1229 aliases[j].name = format_alias_or(buf,
1232 aliases[j].name = strdup(aliases[j].name);
1233 if (!aliases[j].name)
1236 aliases[j].desc = long_desc ? alias->long_desc :
1238 aliases[j].topic = alias->topic;
1239 aliases[j].str = alias->str;
1240 aliases[j].pmu = pmu->name;
1241 aliases[j].metric_expr = alias->metric_expr;
1242 aliases[j].metric_name = alias->metric_name;
1245 if (pmu->selectable &&
1246 (event_glob == NULL || strglobmatch(pmu->name, event_glob))) {
1248 if (asprintf(&s, "%s//", pmu->name) < 0)
1250 aliases[j].name = s;
1255 qsort(aliases, len, sizeof(struct sevent), cmp_sevent);
1256 for (j = 0; j < len; j++) {
1257 /* Skip duplicates */
1258 if (j > 0 && !strcmp(aliases[j].name, aliases[j - 1].name))
1261 printf("%s ", aliases[j].name);
1264 if (aliases[j].desc && !quiet_flag) {
1267 if (aliases[j].topic && (!topic ||
1268 strcmp(topic, aliases[j].topic))) {
1269 printf("%s%s:\n", topic ? "\n" : "",
1271 topic = aliases[j].topic;
1273 printf(" %-50s\n", aliases[j].name);
1274 printf("%*s", 8, "[");
1275 wordwrap(aliases[j].desc, 8, columns, 0);
1278 printf("%*s%s/%s/ ", 8, "", aliases[j].pmu, aliases[j].str);
1279 if (aliases[j].metric_name)
1280 printf(" MetricName: %s", aliases[j].metric_name);
1281 if (aliases[j].metric_expr)
1282 printf(" MetricExpr: %s", aliases[j].metric_expr);
1286 printf(" %-50s [Kernel PMU event]\n", aliases[j].name);
1289 if (printed && pager_in_use())
1292 for (j = 0; j < len; j++)
1293 zfree(&aliases[j].name);
1298 printf("FATAL: not enough memory to print PMU events\n");
1303 bool pmu_have_event(const char *pname, const char *name)
1305 struct perf_pmu *pmu;
1306 struct perf_pmu_alias *alias;
1309 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1310 if (strcmp(pname, pmu->name))
1312 list_for_each_entry(alias, &pmu->aliases, list)
1313 if (!strcmp(alias->name, name))
1319 static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
1322 char path[PATH_MAX];
1325 sysfs = sysfs__mountpoint();
1329 snprintf(path, PATH_MAX,
1330 "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name);
1332 if (stat(path, &st) < 0)
1335 return fopen(path, "r");
1338 int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
1345 va_start(args, fmt);
1346 file = perf_pmu__open_file(pmu, name);
1348 ret = vfscanf(file, fmt, args);