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
);
409 * Weak terms don't override command line options,
410 * which we don't want for implicit terms in aliases.
413 list_add_tail(&cloned
->list
, &list
);
415 list_splice(&list
, terms
);
420 * Reading/parsing the default pmu type value, which should be
422 * /sys/bus/event_source/devices/<dev>/type as sysfs attribute.
424 static int pmu_type(const char *name
, __u32
*type
)
430 const char *sysfs
= sysfs__mountpoint();
435 snprintf(path
, PATH_MAX
,
436 "%s" EVENT_SOURCE_DEVICE_PATH
"%s/type", sysfs
, name
);
438 if (stat(path
, &st
) < 0)
441 file
= fopen(path
, "r");
445 if (1 != fscanf(file
, "%u", type
))
452 /* Add all pmus in sysfs to pmu list: */
453 static void pmu_read_sysfs(void)
458 const char *sysfs
= sysfs__mountpoint();
463 snprintf(path
, PATH_MAX
,
464 "%s" EVENT_SOURCE_DEVICE_PATH
, sysfs
);
470 while ((dent
= readdir(dir
))) {
471 if (!strcmp(dent
->d_name
, ".") || !strcmp(dent
->d_name
, ".."))
473 /* add to static LIST_HEAD(pmus): */
474 perf_pmu__find(dent
->d_name
);
480 static struct cpu_map
*__pmu_cpumask(const char *path
)
483 struct cpu_map
*cpus
;
485 file
= fopen(path
, "r");
489 cpus
= cpu_map__read(file
);
495 * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64)
496 * may have a "cpus" file.
498 #define CPUS_TEMPLATE_UNCORE "%s/bus/event_source/devices/%s/cpumask"
499 #define CPUS_TEMPLATE_CPU "%s/bus/event_source/devices/%s/cpus"
501 static struct cpu_map
*pmu_cpumask(const char *name
)
504 struct cpu_map
*cpus
;
505 const char *sysfs
= sysfs__mountpoint();
506 const char *templates
[] = {
507 CPUS_TEMPLATE_UNCORE
,
511 const char **template;
516 for (template = templates
; *template; template++) {
517 snprintf(path
, PATH_MAX
, *template, sysfs
, name
);
518 cpus
= __pmu_cpumask(path
);
526 static bool pmu_is_uncore(const char *name
)
529 struct cpu_map
*cpus
;
530 const char *sysfs
= sysfs__mountpoint();
532 snprintf(path
, PATH_MAX
, CPUS_TEMPLATE_UNCORE
, sysfs
, name
);
533 cpus
= __pmu_cpumask(path
);
540 * Return the CPU id as a raw string.
542 * Each architecture should provide a more precise id string that
543 * can be use to match the architecture's "mapfile".
545 char * __weak
get_cpuid_str(void)
550 static char *perf_pmu__getcpuid(void)
555 cpuid
= getenv("PERF_CPUID");
557 cpuid
= strdup(cpuid
);
559 cpuid
= get_cpuid_str();
564 pr_debug("Using CPUID %s\n", cpuid
);
570 struct pmu_events_map
*perf_pmu__find_map(void)
572 struct pmu_events_map
*map
;
573 char *cpuid
= perf_pmu__getcpuid();
578 map
= &pmu_events_map
[i
++];
584 if (!strcmp(map
->cpuid
, cpuid
))
592 * From the pmu_events_map, find the table of PMU events that corresponds
593 * to the current running CPU. Then, add all PMU events from that table
596 static void pmu_add_cpu_aliases(struct list_head
*head
, const char *name
)
599 struct pmu_events_map
*map
;
600 struct pmu_event
*pe
;
602 map
= perf_pmu__find_map();
607 * Found a matching PMU events table. Create aliases
613 pe
= &map
->table
[i
++];
615 if (pe
->metric_group
|| pe
->metric_name
)
620 pname
= pe
->pmu
? pe
->pmu
: "cpu";
621 if (strncmp(pname
, name
, strlen(pname
)))
624 /* need type casts to override 'const' */
625 __perf_pmu__new_alias(head
, NULL
, (char *)pe
->name
,
626 (char *)pe
->desc
, (char *)pe
->event
,
627 (char *)pe
->long_desc
, (char *)pe
->topic
,
628 (char *)pe
->unit
, (char *)pe
->perpkg
,
629 (char *)pe
->metric_expr
,
630 (char *)pe
->metric_name
);
634 struct perf_event_attr
* __weak
635 perf_pmu__get_default_config(struct perf_pmu
*pmu __maybe_unused
)
640 static struct perf_pmu
*pmu_lookup(const char *name
)
642 struct perf_pmu
*pmu
;
648 * The pmu data we store & need consists of the pmu
649 * type value and format definitions. Load both right
652 if (pmu_format(name
, &format
))
656 * Check the type first to avoid unnecessary work.
658 if (pmu_type(name
, &type
))
661 if (pmu_aliases(name
, &aliases
))
664 pmu_add_cpu_aliases(&aliases
, name
);
665 pmu
= zalloc(sizeof(*pmu
));
669 pmu
->cpus
= pmu_cpumask(name
);
671 pmu
->is_uncore
= pmu_is_uncore(name
);
673 INIT_LIST_HEAD(&pmu
->format
);
674 INIT_LIST_HEAD(&pmu
->aliases
);
675 list_splice(&format
, &pmu
->format
);
676 list_splice(&aliases
, &pmu
->aliases
);
677 pmu
->name
= strdup(name
);
679 list_add_tail(&pmu
->list
, &pmus
);
681 pmu
->default_config
= perf_pmu__get_default_config(pmu
);
686 static struct perf_pmu
*pmu_find(const char *name
)
688 struct perf_pmu
*pmu
;
690 list_for_each_entry(pmu
, &pmus
, list
)
691 if (!strcmp(pmu
->name
, name
))
697 struct perf_pmu
*perf_pmu__scan(struct perf_pmu
*pmu
)
700 * pmu iterator: If pmu is NULL, we start at the begin,
701 * otherwise return the next pmu. Returns NULL on end.
705 pmu
= list_prepare_entry(pmu
, &pmus
, list
);
707 list_for_each_entry_continue(pmu
, &pmus
, list
)
712 struct perf_pmu
*perf_pmu__find(const char *name
)
714 struct perf_pmu
*pmu
;
717 * Once PMU is loaded it stays in the list,
718 * so we keep us from multiple reading/parsing
719 * the pmu format definitions.
721 pmu
= pmu_find(name
);
725 return pmu_lookup(name
);
728 static struct perf_pmu_format
*
729 pmu_find_format(struct list_head
*formats
, const char *name
)
731 struct perf_pmu_format
*format
;
733 list_for_each_entry(format
, formats
, list
)
734 if (!strcmp(format
->name
, name
))
740 __u64
perf_pmu__format_bits(struct list_head
*formats
, const char *name
)
742 struct perf_pmu_format
*format
= pmu_find_format(formats
, name
);
749 for_each_set_bit(fbit
, format
->bits
, PERF_PMU_FORMAT_BITS
)
750 bits
|= 1ULL << fbit
;
756 * Sets value based on the format definition (format parameter)
757 * and unformated value (value parameter).
759 static void pmu_format_value(unsigned long *format
, __u64 value
, __u64
*v
,
762 unsigned long fbit
, vbit
;
764 for (fbit
= 0, vbit
= 0; fbit
< PERF_PMU_FORMAT_BITS
; fbit
++) {
766 if (!test_bit(fbit
, format
))
769 if (value
& (1llu << vbit
++))
770 *v
|= (1llu << fbit
);
772 *v
&= ~(1llu << fbit
);
776 static __u64
pmu_format_max_value(const unsigned long *format
)
781 for_each_set_bit(fbit
, format
, PERF_PMU_FORMAT_BITS
)
788 * Term is a string term, and might be a param-term. Try to look up it's value
789 * in the remaining terms.
790 * - We have a term like "base-or-format-term=param-term",
791 * - We need to find the value supplied for "param-term" (with param-term named
792 * in a config string) later on in the term list.
794 static int pmu_resolve_param_term(struct parse_events_term
*term
,
795 struct list_head
*head_terms
,
798 struct parse_events_term
*t
;
800 list_for_each_entry(t
, head_terms
, list
) {
801 if (t
->type_val
== PARSE_EVENTS__TERM_TYPE_NUM
) {
802 if (!strcmp(t
->config
, term
->config
)) {
811 printf("Required parameter '%s' not specified\n", term
->config
);
816 static char *pmu_formats_string(struct list_head
*formats
)
818 struct perf_pmu_format
*format
;
820 struct strbuf buf
= STRBUF_INIT
;
826 /* sysfs exported terms */
827 list_for_each_entry(format
, formats
, list
)
828 if (strbuf_addf(&buf
, i
++ ? ",%s" : "%s", format
->name
) < 0)
831 str
= strbuf_detach(&buf
, NULL
);
833 strbuf_release(&buf
);
839 * Setup one of config[12] attr members based on the
840 * user input data - term parameter.
842 static int pmu_config_term(struct list_head
*formats
,
843 struct perf_event_attr
*attr
,
844 struct parse_events_term
*term
,
845 struct list_head
*head_terms
,
846 bool zero
, struct parse_events_error
*err
)
848 struct perf_pmu_format
*format
;
853 * If this is a parameter we've already used for parameterized-eval,
854 * skip it in normal eval.
860 * Hardcoded terms should be already in, so nothing
861 * to be done for them.
863 if (parse_events__is_hardcoded_term(term
))
866 format
= pmu_find_format(formats
, term
->config
);
869 printf("Invalid event/parameter '%s'\n", term
->config
);
871 char *pmu_term
= pmu_formats_string(formats
);
873 err
->idx
= term
->err_term
;
874 err
->str
= strdup("unknown term");
875 err
->help
= parse_events_formats_error_string(pmu_term
);
881 switch (format
->value
) {
882 case PERF_PMU_FORMAT_VALUE_CONFIG
:
885 case PERF_PMU_FORMAT_VALUE_CONFIG1
:
888 case PERF_PMU_FORMAT_VALUE_CONFIG2
:
896 * Either directly use a numeric term, or try to translate string terms
897 * using event parameters.
899 if (term
->type_val
== PARSE_EVENTS__TERM_TYPE_NUM
) {
900 if (term
->no_value
&&
901 bitmap_weight(format
->bits
, PERF_PMU_FORMAT_BITS
) > 1) {
903 err
->idx
= term
->err_val
;
904 err
->str
= strdup("no value assigned for term");
910 } else if (term
->type_val
== PARSE_EVENTS__TERM_TYPE_STR
) {
911 if (strcmp(term
->val
.str
, "?")) {
913 pr_info("Invalid sysfs entry %s=%s\n",
914 term
->config
, term
->val
.str
);
917 err
->idx
= term
->err_val
;
918 err
->str
= strdup("expected numeric value");
923 if (pmu_resolve_param_term(term
, head_terms
, &val
))
928 max_val
= pmu_format_max_value(format
->bits
);
931 err
->idx
= term
->err_val
;
932 if (asprintf(&err
->str
,
933 "value too big for format, maximum is %llu",
934 (unsigned long long)max_val
) < 0)
935 err
->str
= strdup("value too big for format");
939 * Assume we don't care if !err, in which case the value will be
940 * silently truncated.
944 pmu_format_value(format
->bits
, val
, vp
, zero
);
948 int perf_pmu__config_terms(struct list_head
*formats
,
949 struct perf_event_attr
*attr
,
950 struct list_head
*head_terms
,
951 bool zero
, struct parse_events_error
*err
)
953 struct parse_events_term
*term
;
955 list_for_each_entry(term
, head_terms
, list
) {
956 if (pmu_config_term(formats
, attr
, term
, head_terms
,
965 * Configures event's 'attr' parameter based on the:
966 * 1) users input - specified in terms parameter
967 * 2) pmu format definitions - specified by pmu parameter
969 int perf_pmu__config(struct perf_pmu
*pmu
, struct perf_event_attr
*attr
,
970 struct list_head
*head_terms
,
971 struct parse_events_error
*err
)
973 bool zero
= !!pmu
->default_config
;
975 attr
->type
= pmu
->type
;
976 return perf_pmu__config_terms(&pmu
->format
, attr
, head_terms
,
980 static struct perf_pmu_alias
*pmu_find_alias(struct perf_pmu
*pmu
,
981 struct parse_events_term
*term
)
983 struct perf_pmu_alias
*alias
;
986 if (parse_events__is_hardcoded_term(term
))
989 if (term
->type_val
== PARSE_EVENTS__TERM_TYPE_NUM
) {
990 if (term
->val
.num
!= 1)
992 if (pmu_find_format(&pmu
->format
, term
->config
))
995 } else if (term
->type_val
== PARSE_EVENTS__TERM_TYPE_STR
) {
996 if (strcasecmp(term
->config
, "event"))
998 name
= term
->val
.str
;
1003 list_for_each_entry(alias
, &pmu
->aliases
, list
) {
1004 if (!strcasecmp(alias
->name
, name
))
1011 static int check_info_data(struct perf_pmu_alias
*alias
,
1012 struct perf_pmu_info
*info
)
1015 * Only one term in event definition can
1016 * define unit, scale and snapshot, fail
1017 * if there's more than one.
1019 if ((info
->unit
&& alias
->unit
[0]) ||
1020 (info
->scale
&& alias
->scale
) ||
1021 (info
->snapshot
&& alias
->snapshot
))
1025 info
->unit
= alias
->unit
;
1028 info
->scale
= alias
->scale
;
1030 if (alias
->snapshot
)
1031 info
->snapshot
= alias
->snapshot
;
1037 * Find alias in the terms list and replace it with the terms
1038 * defined for the alias
1040 int perf_pmu__check_alias(struct perf_pmu
*pmu
, struct list_head
*head_terms
,
1041 struct perf_pmu_info
*info
)
1043 struct parse_events_term
*term
, *h
;
1044 struct perf_pmu_alias
*alias
;
1047 info
->per_pkg
= false;
1050 * Mark unit and scale as not set
1051 * (different from default values, see below)
1055 info
->snapshot
= false;
1056 info
->metric_expr
= NULL
;
1057 info
->metric_name
= NULL
;
1059 list_for_each_entry_safe(term
, h
, head_terms
, list
) {
1060 alias
= pmu_find_alias(pmu
, term
);
1063 ret
= pmu_alias_terms(alias
, &term
->list
);
1067 ret
= check_info_data(alias
, info
);
1072 info
->per_pkg
= true;
1073 info
->metric_expr
= alias
->metric_expr
;
1074 info
->metric_name
= alias
->metric_name
;
1076 list_del(&term
->list
);
1081 * if no unit or scale foundin aliases, then
1082 * set defaults as for evsel
1083 * unit cannot left to NULL
1085 if (info
->unit
== NULL
)
1088 if (info
->scale
== 0.0)
1094 int perf_pmu__new_format(struct list_head
*list
, char *name
,
1095 int config
, unsigned long *bits
)
1097 struct perf_pmu_format
*format
;
1099 format
= zalloc(sizeof(*format
));
1103 format
->name
= strdup(name
);
1104 format
->value
= config
;
1105 memcpy(format
->bits
, bits
, sizeof(format
->bits
));
1107 list_add_tail(&format
->list
, list
);
1111 void perf_pmu__set_format(unsigned long *bits
, long from
, long to
)
1118 memset(bits
, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS
));
1119 for (b
= from
; b
<= to
; b
++)
1123 static int sub_non_neg(int a
, int b
)
1130 static char *format_alias(char *buf
, int len
, struct perf_pmu
*pmu
,
1131 struct perf_pmu_alias
*alias
)
1133 struct parse_events_term
*term
;
1134 int used
= snprintf(buf
, len
, "%s/%s", pmu
->name
, alias
->name
);
1136 list_for_each_entry(term
, &alias
->terms
, list
) {
1137 if (term
->type_val
== PARSE_EVENTS__TERM_TYPE_STR
)
1138 used
+= snprintf(buf
+ used
, sub_non_neg(len
, used
),
1139 ",%s=%s", term
->config
,
1143 if (sub_non_neg(len
, used
) > 0) {
1147 if (sub_non_neg(len
, used
) > 0) {
1151 buf
[len
- 1] = '\0';
1156 static char *format_alias_or(char *buf
, int len
, struct perf_pmu
*pmu
,
1157 struct perf_pmu_alias
*alias
)
1159 snprintf(buf
, len
, "%s OR %s/%s/", alias
->name
, pmu
->name
, alias
->name
);
1173 static int cmp_sevent(const void *a
, const void *b
)
1175 const struct sevent
*as
= a
;
1176 const struct sevent
*bs
= b
;
1178 /* Put extra events last */
1179 if (!!as
->desc
!= !!bs
->desc
)
1180 return !!as
->desc
- !!bs
->desc
;
1181 if (as
->topic
&& bs
->topic
) {
1182 int n
= strcmp(as
->topic
, bs
->topic
);
1187 return strcmp(as
->name
, bs
->name
);
1190 static void wordwrap(char *s
, int start
, int max
, int corr
)
1196 int wlen
= strcspn(s
, " \t");
1198 if (column
+ wlen
>= max
&& column
> start
) {
1199 printf("\n%*s", start
, "");
1200 column
= start
+ corr
;
1202 n
= printf("%s%.*s", column
> start
? " " : "", wlen
, s
);
1211 void print_pmu_events(const char *event_glob
, bool name_only
, bool quiet_flag
,
1212 bool long_desc
, bool details_flag
)
1214 struct perf_pmu
*pmu
;
1215 struct perf_pmu_alias
*alias
;
1219 struct sevent
*aliases
;
1221 int columns
= pager_get_columns();
1226 while ((pmu
= perf_pmu__scan(pmu
)) != NULL
) {
1227 list_for_each_entry(alias
, &pmu
->aliases
, list
)
1229 if (pmu
->selectable
)
1232 aliases
= zalloc(sizeof(struct sevent
) * len
);
1237 while ((pmu
= perf_pmu__scan(pmu
)) != NULL
) {
1238 list_for_each_entry(alias
, &pmu
->aliases
, list
) {
1239 char *name
= alias
->desc
? alias
->name
:
1240 format_alias(buf
, sizeof(buf
), pmu
, alias
);
1241 bool is_cpu
= !strcmp(pmu
->name
, "cpu");
1243 if (event_glob
!= NULL
&&
1244 !(strglobmatch_nocase(name
, event_glob
) ||
1245 (!is_cpu
&& strglobmatch_nocase(alias
->name
,
1248 strglobmatch_nocase(alias
->topic
, event_glob
))))
1251 if (is_cpu
&& !name_only
&& !alias
->desc
)
1252 name
= format_alias_or(buf
, sizeof(buf
), pmu
, alias
);
1254 aliases
[j
].name
= name
;
1255 if (is_cpu
&& !name_only
&& !alias
->desc
)
1256 aliases
[j
].name
= format_alias_or(buf
,
1259 aliases
[j
].name
= strdup(aliases
[j
].name
);
1260 if (!aliases
[j
].name
)
1263 aliases
[j
].desc
= long_desc
? alias
->long_desc
:
1265 aliases
[j
].topic
= alias
->topic
;
1266 aliases
[j
].str
= alias
->str
;
1267 aliases
[j
].pmu
= pmu
->name
;
1268 aliases
[j
].metric_expr
= alias
->metric_expr
;
1269 aliases
[j
].metric_name
= alias
->metric_name
;
1272 if (pmu
->selectable
&&
1273 (event_glob
== NULL
|| strglobmatch(pmu
->name
, event_glob
))) {
1275 if (asprintf(&s
, "%s//", pmu
->name
) < 0)
1277 aliases
[j
].name
= s
;
1282 qsort(aliases
, len
, sizeof(struct sevent
), cmp_sevent
);
1283 for (j
= 0; j
< len
; j
++) {
1284 /* Skip duplicates */
1285 if (j
> 0 && !strcmp(aliases
[j
].name
, aliases
[j
- 1].name
))
1288 printf("%s ", aliases
[j
].name
);
1291 if (aliases
[j
].desc
&& !quiet_flag
) {
1294 if (aliases
[j
].topic
&& (!topic
||
1295 strcmp(topic
, aliases
[j
].topic
))) {
1296 printf("%s%s:\n", topic
? "\n" : "",
1298 topic
= aliases
[j
].topic
;
1300 printf(" %-50s\n", aliases
[j
].name
);
1301 printf("%*s", 8, "[");
1302 wordwrap(aliases
[j
].desc
, 8, columns
, 0);
1305 printf("%*s%s/%s/ ", 8, "", aliases
[j
].pmu
, aliases
[j
].str
);
1306 if (aliases
[j
].metric_name
)
1307 printf(" MetricName: %s", aliases
[j
].metric_name
);
1308 if (aliases
[j
].metric_expr
)
1309 printf(" MetricExpr: %s", aliases
[j
].metric_expr
);
1313 printf(" %-50s [Kernel PMU event]\n", aliases
[j
].name
);
1316 if (printed
&& pager_in_use())
1319 for (j
= 0; j
< len
; j
++)
1320 zfree(&aliases
[j
].name
);
1325 printf("FATAL: not enough memory to print PMU events\n");
1330 bool pmu_have_event(const char *pname
, const char *name
)
1332 struct perf_pmu
*pmu
;
1333 struct perf_pmu_alias
*alias
;
1336 while ((pmu
= perf_pmu__scan(pmu
)) != NULL
) {
1337 if (strcmp(pname
, pmu
->name
))
1339 list_for_each_entry(alias
, &pmu
->aliases
, list
)
1340 if (!strcmp(alias
->name
, name
))
1346 static FILE *perf_pmu__open_file(struct perf_pmu
*pmu
, const char *name
)
1349 char path
[PATH_MAX
];
1352 sysfs
= sysfs__mountpoint();
1356 snprintf(path
, PATH_MAX
,
1357 "%s" EVENT_SOURCE_DEVICE_PATH
"%s/%s", sysfs
, pmu
->name
, name
);
1359 if (stat(path
, &st
) < 0)
1362 return fopen(path
, "r");
1365 int perf_pmu__scan_file(struct perf_pmu
*pmu
, const char *name
, const char *fmt
,
1372 va_start(args
, fmt
);
1373 file
= perf_pmu__open_file(pmu
, name
);
1375 ret
= vfscanf(file
, fmt
, args
);