1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/list.h>
3 #include <linux/compiler.h>
13 #include <api/fs/fs.h>
18 #include "parse-events.h"
21 #include "pmu-events/pmu-events.h"
25 struct perf_pmu_format
{
28 DECLARE_BITMAP(bits
, PERF_PMU_FORMAT_BITS
);
29 struct list_head list
;
32 int perf_pmu_parse(struct list_head
*list
, char *name
);
33 extern FILE *perf_pmu_in
;
35 static LIST_HEAD(pmus
);
38 * Parse & process all the sysfs attributes located under
39 * the directory specified in 'dir' parameter.
41 int perf_pmu__format_parse(char *dir
, struct list_head
*head
)
43 struct dirent
*evt_ent
;
47 format_dir
= opendir(dir
);
51 while (!ret
&& (evt_ent
= readdir(format_dir
))) {
53 char *name
= evt_ent
->d_name
;
56 if (!strcmp(name
, ".") || !strcmp(name
, ".."))
59 snprintf(path
, PATH_MAX
, "%s/%s", dir
, name
);
62 file
= fopen(path
, "r");
67 ret
= perf_pmu_parse(head
, name
);
76 * Reading/parsing the default pmu format definition, which should be
78 * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
80 static int pmu_format(const char *name
, struct list_head
*format
)
84 const char *sysfs
= sysfs__mountpoint();
89 snprintf(path
, PATH_MAX
,
90 "%s" EVENT_SOURCE_DEVICE_PATH
"%s/format", sysfs
, name
);
92 if (stat(path
, &st
) < 0)
93 return 0; /* no error if format does not exist */
95 if (perf_pmu__format_parse(path
, format
))
101 static int convert_scale(const char *scale
, char **end
, double *sval
)
107 * save current locale
109 lc
= setlocale(LC_NUMERIC
, NULL
);
112 * The lc string may be allocated in static storage,
113 * so get a dynamic copy to make it survive setlocale
123 * force to C locale to ensure kernel
124 * scale string is converted correctly.
125 * kernel uses default C locale.
127 setlocale(LC_NUMERIC
, "C");
129 *sval
= strtod(scale
, end
);
133 setlocale(LC_NUMERIC
, lc
);
138 static int perf_pmu__parse_scale(struct perf_pmu_alias
*alias
, char *dir
, char *name
)
146 scnprintf(path
, PATH_MAX
, "%s/%s.scale", dir
, name
);
148 fd
= open(path
, O_RDONLY
);
152 if (fstat(fd
, &st
) < 0)
155 sret
= read(fd
, scale
, sizeof(scale
)-1);
159 if (scale
[sret
- 1] == '\n')
160 scale
[sret
- 1] = '\0';
164 ret
= convert_scale(scale
, NULL
, &alias
->scale
);
170 static int perf_pmu__parse_unit(struct perf_pmu_alias
*alias
, char *dir
, char *name
)
176 scnprintf(path
, PATH_MAX
, "%s/%s.unit", dir
, name
);
178 fd
= open(path
, O_RDONLY
);
182 sret
= read(fd
, alias
->unit
, UNIT_MAX_LEN
);
188 if (alias
->unit
[sret
- 1] == '\n')
189 alias
->unit
[sret
- 1] = '\0';
191 alias
->unit
[sret
] = '\0';
196 alias
->unit
[0] = '\0';
201 perf_pmu__parse_per_pkg(struct perf_pmu_alias
*alias
, char *dir
, char *name
)
206 scnprintf(path
, PATH_MAX
, "%s/%s.per-pkg", dir
, name
);
208 fd
= open(path
, O_RDONLY
);
214 alias
->per_pkg
= true;
218 static int perf_pmu__parse_snapshot(struct perf_pmu_alias
*alias
,
219 char *dir
, char *name
)
224 scnprintf(path
, PATH_MAX
, "%s/%s.snapshot", dir
, name
);
226 fd
= open(path
, O_RDONLY
);
230 alias
->snapshot
= true;
235 static void perf_pmu_assign_str(char *name
, const char *field
, char **old_str
,
241 if (*new_str
) { /* Have new string, check with old */
242 if (strcasecmp(*old_str
, *new_str
))
243 pr_debug("alias %s differs in field '%s'\n",
246 } else /* Nothing new --> keep old string */
253 static void perf_pmu_update_alias(struct perf_pmu_alias
*old
,
254 struct perf_pmu_alias
*newalias
)
256 perf_pmu_assign_str(old
->name
, "desc", &old
->desc
, &newalias
->desc
);
257 perf_pmu_assign_str(old
->name
, "long_desc", &old
->long_desc
,
258 &newalias
->long_desc
);
259 perf_pmu_assign_str(old
->name
, "topic", &old
->topic
, &newalias
->topic
);
260 perf_pmu_assign_str(old
->name
, "metric_expr", &old
->metric_expr
,
261 &newalias
->metric_expr
);
262 perf_pmu_assign_str(old
->name
, "metric_name", &old
->metric_name
,
263 &newalias
->metric_name
);
264 perf_pmu_assign_str(old
->name
, "value", &old
->str
, &newalias
->str
);
265 old
->scale
= newalias
->scale
;
266 old
->per_pkg
= newalias
->per_pkg
;
267 old
->snapshot
= newalias
->snapshot
;
268 memcpy(old
->unit
, newalias
->unit
, sizeof(old
->unit
));
271 /* Delete an alias entry. */
272 static void perf_pmu_free_alias(struct perf_pmu_alias
*newalias
)
274 zfree(&newalias
->name
);
275 zfree(&newalias
->desc
);
276 zfree(&newalias
->long_desc
);
277 zfree(&newalias
->topic
);
278 zfree(&newalias
->str
);
279 zfree(&newalias
->metric_expr
);
280 zfree(&newalias
->metric_name
);
281 parse_events_terms__purge(&newalias
->terms
);
285 /* Merge an alias, search in alias list. If this name is already
286 * present merge both of them to combine all information.
288 static bool perf_pmu_merge_alias(struct perf_pmu_alias
*newalias
,
289 struct list_head
*alist
)
291 struct perf_pmu_alias
*a
;
293 list_for_each_entry(a
, alist
, list
) {
294 if (!strcasecmp(newalias
->name
, a
->name
)) {
295 perf_pmu_update_alias(a
, newalias
);
296 perf_pmu_free_alias(newalias
);
303 static int __perf_pmu__new_alias(struct list_head
*list
, char *dir
, char *name
,
304 char *desc
, char *val
,
305 char *long_desc
, char *topic
,
306 char *unit
, char *perpkg
,
310 struct parse_events_term
*term
;
311 struct perf_pmu_alias
*alias
;
316 alias
= malloc(sizeof(*alias
));
320 INIT_LIST_HEAD(&alias
->terms
);
322 alias
->unit
[0] = '\0';
323 alias
->per_pkg
= false;
324 alias
->snapshot
= false;
326 ret
= parse_events_terms(&alias
->terms
, val
);
328 pr_err("Cannot parse alias %s: %d\n", val
, ret
);
333 /* Scan event and remove leading zeroes, spaces, newlines, some
334 * platforms have terms specified as
335 * event=0x0091 (read from files ../<PMU>/events/<FILE>
336 * and terms specified as event=0x91 (read from JSON files).
338 * Rebuild string to make alias->str member comparable.
340 memset(newval
, 0, sizeof(newval
));
342 list_for_each_entry(term
, &alias
->terms
, list
) {
344 ret
+= scnprintf(newval
+ ret
, sizeof(newval
) - ret
,
346 if (term
->type_val
== PARSE_EVENTS__TERM_TYPE_NUM
)
347 ret
+= scnprintf(newval
+ ret
, sizeof(newval
) - ret
,
348 "%s=%#x", term
->config
, term
->val
.num
);
349 else if (term
->type_val
== PARSE_EVENTS__TERM_TYPE_STR
)
350 ret
+= scnprintf(newval
+ ret
, sizeof(newval
) - ret
,
351 "%s=%s", term
->config
, term
->val
.str
);
354 alias
->name
= strdup(name
);
357 * load unit name and scale if available
359 perf_pmu__parse_unit(alias
, dir
, name
);
360 perf_pmu__parse_scale(alias
, dir
, name
);
361 perf_pmu__parse_per_pkg(alias
, dir
, name
);
362 perf_pmu__parse_snapshot(alias
, dir
, name
);
365 alias
->metric_expr
= metric_expr
? strdup(metric_expr
) : NULL
;
366 alias
->metric_name
= metric_name
? strdup(metric_name
): NULL
;
367 alias
->desc
= desc
? strdup(desc
) : NULL
;
368 alias
->long_desc
= long_desc
? strdup(long_desc
) :
369 desc
? strdup(desc
) : NULL
;
370 alias
->topic
= topic
? strdup(topic
) : NULL
;
372 if (convert_scale(unit
, &unit
, &alias
->scale
) < 0)
374 snprintf(alias
->unit
, sizeof(alias
->unit
), "%s", unit
);
376 alias
->per_pkg
= perpkg
&& sscanf(perpkg
, "%d", &num
) == 1 && num
== 1;
377 alias
->str
= strdup(newval
);
379 if (!perf_pmu_merge_alias(alias
, list
))
380 list_add_tail(&alias
->list
, list
);
385 static int perf_pmu__new_alias(struct list_head
*list
, char *dir
, char *name
, FILE *file
)
390 ret
= fread(buf
, 1, sizeof(buf
), file
);
396 /* Remove trailing newline from sysfs file */
399 return __perf_pmu__new_alias(list
, dir
, name
, NULL
, buf
, NULL
, NULL
, NULL
,
403 static inline bool pmu_alias_info_file(char *name
)
408 if (len
> 5 && !strcmp(name
+ len
- 5, ".unit"))
410 if (len
> 6 && !strcmp(name
+ len
- 6, ".scale"))
412 if (len
> 8 && !strcmp(name
+ len
- 8, ".per-pkg"))
414 if (len
> 9 && !strcmp(name
+ len
- 9, ".snapshot"))
421 * Process all the sysfs attributes located under the directory
422 * specified in 'dir' parameter.
424 static int pmu_aliases_parse(char *dir
, struct list_head
*head
)
426 struct dirent
*evt_ent
;
429 event_dir
= opendir(dir
);
433 while ((evt_ent
= readdir(event_dir
))) {
435 char *name
= evt_ent
->d_name
;
438 if (!strcmp(name
, ".") || !strcmp(name
, ".."))
442 * skip info files parsed in perf_pmu__new_alias()
444 if (pmu_alias_info_file(name
))
447 scnprintf(path
, PATH_MAX
, "%s/%s", dir
, name
);
449 file
= fopen(path
, "r");
451 pr_debug("Cannot open %s\n", path
);
455 if (perf_pmu__new_alias(head
, dir
, name
, file
) < 0)
456 pr_debug("Cannot set up %s\n", name
);
465 * Reading the pmu event aliases definition, which should be located at:
466 * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
468 static int pmu_aliases(const char *name
, struct list_head
*head
)
472 const char *sysfs
= sysfs__mountpoint();
477 snprintf(path
, PATH_MAX
,
478 "%s/bus/event_source/devices/%s/events", sysfs
, name
);
480 if (stat(path
, &st
) < 0)
481 return 0; /* no error if 'events' does not exist */
483 if (pmu_aliases_parse(path
, head
))
489 static int pmu_alias_terms(struct perf_pmu_alias
*alias
,
490 struct list_head
*terms
)
492 struct parse_events_term
*term
, *cloned
;
496 list_for_each_entry(term
, &alias
->terms
, list
) {
497 ret
= parse_events_term__clone(&cloned
, term
);
499 parse_events_terms__purge(&list
);
503 * Weak terms don't override command line options,
504 * which we don't want for implicit terms in aliases.
507 list_add_tail(&cloned
->list
, &list
);
509 list_splice(&list
, terms
);
514 * Reading/parsing the default pmu type value, which should be
516 * /sys/bus/event_source/devices/<dev>/type as sysfs attribute.
518 static int pmu_type(const char *name
, __u32
*type
)
524 const char *sysfs
= sysfs__mountpoint();
529 snprintf(path
, PATH_MAX
,
530 "%s" EVENT_SOURCE_DEVICE_PATH
"%s/type", sysfs
, name
);
532 if (stat(path
, &st
) < 0)
535 file
= fopen(path
, "r");
539 if (1 != fscanf(file
, "%u", type
))
546 /* Add all pmus in sysfs to pmu list: */
547 static void pmu_read_sysfs(void)
552 const char *sysfs
= sysfs__mountpoint();
557 snprintf(path
, PATH_MAX
,
558 "%s" EVENT_SOURCE_DEVICE_PATH
, sysfs
);
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 cpu_map
*__pmu_cpumask(const char *path
)
577 struct cpu_map
*cpus
;
579 file
= fopen(path
, "r");
583 cpus
= 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 CPUS_TEMPLATE_UNCORE "%s/bus/event_source/devices/%s/cpumask"
593 #define CPUS_TEMPLATE_CPU "%s/bus/event_source/devices/%s/cpus"
595 static struct cpu_map
*pmu_cpumask(const char *name
)
598 struct 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
)
623 struct cpu_map
*cpus
;
624 const char *sysfs
= sysfs__mountpoint();
626 snprintf(path
, PATH_MAX
, CPUS_TEMPLATE_UNCORE
, sysfs
, name
);
627 cpus
= __pmu_cpumask(path
);
634 * PMU CORE devices have different name other than cpu in sysfs on some
636 * Looking for possible sysfs files to identify the arm core device.
638 static int is_arm_pmu_core(const char *name
)
642 const char *sysfs
= sysfs__mountpoint();
647 /* Look for cpu sysfs (specific to arm) */
648 scnprintf(path
, PATH_MAX
, "%s/bus/event_source/devices/%s/cpus",
650 if (stat(path
, &st
) == 0)
656 static char *perf_pmu__getcpuid(struct perf_pmu
*pmu
)
661 cpuid
= getenv("PERF_CPUID");
663 cpuid
= strdup(cpuid
);
665 cpuid
= get_cpuid_str(pmu
);
670 pr_debug("Using CPUID %s\n", cpuid
);
676 struct pmu_events_map
*perf_pmu__find_map(struct perf_pmu
*pmu
)
678 struct pmu_events_map
*map
;
679 char *cpuid
= perf_pmu__getcpuid(pmu
);
682 /* on some platforms which uses cpus map, cpuid can be NULL for
683 * PMUs other than CORE PMUs.
690 map
= &pmu_events_map
[i
++];
696 if (!strcmp_cpuid_str(map
->cpuid
, cpuid
))
704 * From the pmu_events_map, find the table of PMU events that corresponds
705 * to the current running CPU. Then, add all PMU events from that table
708 static void pmu_add_cpu_aliases(struct list_head
*head
, struct perf_pmu
*pmu
)
711 struct pmu_events_map
*map
;
712 struct pmu_event
*pe
;
713 const char *name
= pmu
->name
;
716 map
= perf_pmu__find_map(pmu
);
721 * Found a matching PMU events table. Create aliases
726 pe
= &map
->table
[i
++];
728 if (pe
->metric_group
|| pe
->metric_name
)
733 if (!is_arm_pmu_core(name
)) {
734 pname
= pe
->pmu
? pe
->pmu
: "cpu";
737 * uncore alias may be from different PMU
740 if (pmu_is_uncore(name
) &&
741 !strncmp(pname
, name
, strlen(pname
)))
744 if (strcmp(pname
, name
))
749 /* need type casts to override 'const' */
750 __perf_pmu__new_alias(head
, NULL
, (char *)pe
->name
,
751 (char *)pe
->desc
, (char *)pe
->event
,
752 (char *)pe
->long_desc
, (char *)pe
->topic
,
753 (char *)pe
->unit
, (char *)pe
->perpkg
,
754 (char *)pe
->metric_expr
,
755 (char *)pe
->metric_name
);
759 struct perf_event_attr
* __weak
760 perf_pmu__get_default_config(struct perf_pmu
*pmu __maybe_unused
)
765 static int pmu_max_precise(const char *name
)
768 int max_precise
= -1;
770 scnprintf(path
, PATH_MAX
,
771 "bus/event_source/devices/%s/caps/max_precise",
774 sysfs__read_int(path
, &max_precise
);
778 static struct perf_pmu
*pmu_lookup(const char *name
)
780 struct perf_pmu
*pmu
;
786 * The pmu data we store & need consists of the pmu
787 * type value and format definitions. Load both right
790 if (pmu_format(name
, &format
))
794 * Check the type first to avoid unnecessary work.
796 if (pmu_type(name
, &type
))
799 if (pmu_aliases(name
, &aliases
))
802 pmu
= zalloc(sizeof(*pmu
));
806 pmu
->cpus
= pmu_cpumask(name
);
807 pmu
->name
= strdup(name
);
809 pmu
->is_uncore
= pmu_is_uncore(name
);
810 pmu
->max_precise
= pmu_max_precise(name
);
811 pmu_add_cpu_aliases(&aliases
, pmu
);
813 INIT_LIST_HEAD(&pmu
->format
);
814 INIT_LIST_HEAD(&pmu
->aliases
);
815 list_splice(&format
, &pmu
->format
);
816 list_splice(&aliases
, &pmu
->aliases
);
817 list_add_tail(&pmu
->list
, &pmus
);
819 pmu
->default_config
= perf_pmu__get_default_config(pmu
);
824 static struct perf_pmu
*pmu_find(const char *name
)
826 struct perf_pmu
*pmu
;
828 list_for_each_entry(pmu
, &pmus
, list
)
829 if (!strcmp(pmu
->name
, name
))
835 struct perf_pmu
*perf_pmu__scan(struct perf_pmu
*pmu
)
838 * pmu iterator: If pmu is NULL, we start at the begin,
839 * otherwise return the next pmu. Returns NULL on end.
843 pmu
= list_prepare_entry(pmu
, &pmus
, list
);
845 list_for_each_entry_continue(pmu
, &pmus
, list
)
850 struct perf_pmu
*perf_pmu__find(const char *name
)
852 struct perf_pmu
*pmu
;
855 * Once PMU is loaded it stays in the list,
856 * so we keep us from multiple reading/parsing
857 * the pmu format definitions.
859 pmu
= pmu_find(name
);
863 return pmu_lookup(name
);
866 static struct perf_pmu_format
*
867 pmu_find_format(struct list_head
*formats
, const char *name
)
869 struct perf_pmu_format
*format
;
871 list_for_each_entry(format
, formats
, list
)
872 if (!strcmp(format
->name
, name
))
878 __u64
perf_pmu__format_bits(struct list_head
*formats
, const char *name
)
880 struct perf_pmu_format
*format
= pmu_find_format(formats
, name
);
887 for_each_set_bit(fbit
, format
->bits
, PERF_PMU_FORMAT_BITS
)
888 bits
|= 1ULL << fbit
;
894 * Sets value based on the format definition (format parameter)
895 * and unformated value (value parameter).
897 static void pmu_format_value(unsigned long *format
, __u64 value
, __u64
*v
,
900 unsigned long fbit
, vbit
;
902 for (fbit
= 0, vbit
= 0; fbit
< PERF_PMU_FORMAT_BITS
; fbit
++) {
904 if (!test_bit(fbit
, format
))
907 if (value
& (1llu << vbit
++))
908 *v
|= (1llu << fbit
);
910 *v
&= ~(1llu << fbit
);
914 static __u64
pmu_format_max_value(const unsigned long *format
)
918 w
= bitmap_weight(format
, PERF_PMU_FORMAT_BITS
);
922 return (1ULL << w
) - 1;
927 * Term is a string term, and might be a param-term. Try to look up it's value
928 * in the remaining terms.
929 * - We have a term like "base-or-format-term=param-term",
930 * - We need to find the value supplied for "param-term" (with param-term named
931 * in a config string) later on in the term list.
933 static int pmu_resolve_param_term(struct parse_events_term
*term
,
934 struct list_head
*head_terms
,
937 struct parse_events_term
*t
;
939 list_for_each_entry(t
, head_terms
, list
) {
940 if (t
->type_val
== PARSE_EVENTS__TERM_TYPE_NUM
) {
941 if (!strcmp(t
->config
, term
->config
)) {
950 printf("Required parameter '%s' not specified\n", term
->config
);
955 static char *pmu_formats_string(struct list_head
*formats
)
957 struct perf_pmu_format
*format
;
959 struct strbuf buf
= STRBUF_INIT
;
965 /* sysfs exported terms */
966 list_for_each_entry(format
, formats
, list
)
967 if (strbuf_addf(&buf
, i
++ ? ",%s" : "%s", format
->name
) < 0)
970 str
= strbuf_detach(&buf
, NULL
);
972 strbuf_release(&buf
);
978 * Setup one of config[12] attr members based on the
979 * user input data - term parameter.
981 static int pmu_config_term(struct list_head
*formats
,
982 struct perf_event_attr
*attr
,
983 struct parse_events_term
*term
,
984 struct list_head
*head_terms
,
985 bool zero
, struct parse_events_error
*err
)
987 struct perf_pmu_format
*format
;
992 * If this is a parameter we've already used for parameterized-eval,
993 * skip it in normal eval.
999 * Hardcoded terms should be already in, so nothing
1000 * to be done for them.
1002 if (parse_events__is_hardcoded_term(term
))
1005 format
= pmu_find_format(formats
, term
->config
);
1008 printf("Invalid event/parameter '%s'\n", term
->config
);
1010 char *pmu_term
= pmu_formats_string(formats
);
1012 err
->idx
= term
->err_term
;
1013 err
->str
= strdup("unknown term");
1014 err
->help
= parse_events_formats_error_string(pmu_term
);
1020 switch (format
->value
) {
1021 case PERF_PMU_FORMAT_VALUE_CONFIG
:
1024 case PERF_PMU_FORMAT_VALUE_CONFIG1
:
1025 vp
= &attr
->config1
;
1027 case PERF_PMU_FORMAT_VALUE_CONFIG2
:
1028 vp
= &attr
->config2
;
1035 * Either directly use a numeric term, or try to translate string terms
1036 * using event parameters.
1038 if (term
->type_val
== PARSE_EVENTS__TERM_TYPE_NUM
) {
1039 if (term
->no_value
&&
1040 bitmap_weight(format
->bits
, PERF_PMU_FORMAT_BITS
) > 1) {
1042 err
->idx
= term
->err_val
;
1043 err
->str
= strdup("no value assigned for term");
1048 val
= term
->val
.num
;
1049 } else if (term
->type_val
== PARSE_EVENTS__TERM_TYPE_STR
) {
1050 if (strcmp(term
->val
.str
, "?")) {
1052 pr_info("Invalid sysfs entry %s=%s\n",
1053 term
->config
, term
->val
.str
);
1056 err
->idx
= term
->err_val
;
1057 err
->str
= strdup("expected numeric value");
1062 if (pmu_resolve_param_term(term
, head_terms
, &val
))
1067 max_val
= pmu_format_max_value(format
->bits
);
1068 if (val
> max_val
) {
1070 err
->idx
= term
->err_val
;
1071 if (asprintf(&err
->str
,
1072 "value too big for format, maximum is %llu",
1073 (unsigned long long)max_val
) < 0)
1074 err
->str
= strdup("value too big for format");
1078 * Assume we don't care if !err, in which case the value will be
1079 * silently truncated.
1083 pmu_format_value(format
->bits
, val
, vp
, zero
);
1087 int perf_pmu__config_terms(struct list_head
*formats
,
1088 struct perf_event_attr
*attr
,
1089 struct list_head
*head_terms
,
1090 bool zero
, struct parse_events_error
*err
)
1092 struct parse_events_term
*term
;
1094 list_for_each_entry(term
, head_terms
, list
) {
1095 if (pmu_config_term(formats
, attr
, term
, head_terms
,
1104 * Configures event's 'attr' parameter based on the:
1105 * 1) users input - specified in terms parameter
1106 * 2) pmu format definitions - specified by pmu parameter
1108 int perf_pmu__config(struct perf_pmu
*pmu
, struct perf_event_attr
*attr
,
1109 struct list_head
*head_terms
,
1110 struct parse_events_error
*err
)
1112 bool zero
= !!pmu
->default_config
;
1114 attr
->type
= pmu
->type
;
1115 return perf_pmu__config_terms(&pmu
->format
, attr
, head_terms
,
1119 static struct perf_pmu_alias
*pmu_find_alias(struct perf_pmu
*pmu
,
1120 struct parse_events_term
*term
)
1122 struct perf_pmu_alias
*alias
;
1125 if (parse_events__is_hardcoded_term(term
))
1128 if (term
->type_val
== PARSE_EVENTS__TERM_TYPE_NUM
) {
1129 if (term
->val
.num
!= 1)
1131 if (pmu_find_format(&pmu
->format
, term
->config
))
1133 name
= term
->config
;
1134 } else if (term
->type_val
== PARSE_EVENTS__TERM_TYPE_STR
) {
1135 if (strcasecmp(term
->config
, "event"))
1137 name
= term
->val
.str
;
1142 list_for_each_entry(alias
, &pmu
->aliases
, list
) {
1143 if (!strcasecmp(alias
->name
, name
))
1150 static int check_info_data(struct perf_pmu_alias
*alias
,
1151 struct perf_pmu_info
*info
)
1154 * Only one term in event definition can
1155 * define unit, scale and snapshot, fail
1156 * if there's more than one.
1158 if ((info
->unit
&& alias
->unit
[0]) ||
1159 (info
->scale
&& alias
->scale
) ||
1160 (info
->snapshot
&& alias
->snapshot
))
1164 info
->unit
= alias
->unit
;
1167 info
->scale
= alias
->scale
;
1169 if (alias
->snapshot
)
1170 info
->snapshot
= alias
->snapshot
;
1176 * Find alias in the terms list and replace it with the terms
1177 * defined for the alias
1179 int perf_pmu__check_alias(struct perf_pmu
*pmu
, struct list_head
*head_terms
,
1180 struct perf_pmu_info
*info
)
1182 struct parse_events_term
*term
, *h
;
1183 struct perf_pmu_alias
*alias
;
1186 info
->per_pkg
= false;
1189 * Mark unit and scale as not set
1190 * (different from default values, see below)
1194 info
->snapshot
= false;
1195 info
->metric_expr
= NULL
;
1196 info
->metric_name
= NULL
;
1198 list_for_each_entry_safe(term
, h
, head_terms
, list
) {
1199 alias
= pmu_find_alias(pmu
, term
);
1202 ret
= pmu_alias_terms(alias
, &term
->list
);
1206 ret
= check_info_data(alias
, info
);
1211 info
->per_pkg
= true;
1212 info
->metric_expr
= alias
->metric_expr
;
1213 info
->metric_name
= alias
->metric_name
;
1215 list_del(&term
->list
);
1220 * if no unit or scale foundin aliases, then
1221 * set defaults as for evsel
1222 * unit cannot left to NULL
1224 if (info
->unit
== NULL
)
1227 if (info
->scale
== 0.0)
1233 int perf_pmu__new_format(struct list_head
*list
, char *name
,
1234 int config
, unsigned long *bits
)
1236 struct perf_pmu_format
*format
;
1238 format
= zalloc(sizeof(*format
));
1242 format
->name
= strdup(name
);
1243 format
->value
= config
;
1244 memcpy(format
->bits
, bits
, sizeof(format
->bits
));
1246 list_add_tail(&format
->list
, list
);
1250 void perf_pmu__set_format(unsigned long *bits
, long from
, long to
)
1257 memset(bits
, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS
));
1258 for (b
= from
; b
<= to
; b
++)
1262 static int sub_non_neg(int a
, int b
)
1269 static char *format_alias(char *buf
, int len
, struct perf_pmu
*pmu
,
1270 struct perf_pmu_alias
*alias
)
1272 struct parse_events_term
*term
;
1273 int used
= snprintf(buf
, len
, "%s/%s", pmu
->name
, alias
->name
);
1275 list_for_each_entry(term
, &alias
->terms
, list
) {
1276 if (term
->type_val
== PARSE_EVENTS__TERM_TYPE_STR
)
1277 used
+= snprintf(buf
+ used
, sub_non_neg(len
, used
),
1278 ",%s=%s", term
->config
,
1282 if (sub_non_neg(len
, used
) > 0) {
1286 if (sub_non_neg(len
, used
) > 0) {
1290 buf
[len
- 1] = '\0';
1295 static char *format_alias_or(char *buf
, int len
, struct perf_pmu
*pmu
,
1296 struct perf_pmu_alias
*alias
)
1298 snprintf(buf
, len
, "%s OR %s/%s/", alias
->name
, pmu
->name
, alias
->name
);
1312 static int cmp_sevent(const void *a
, const void *b
)
1314 const struct sevent
*as
= a
;
1315 const struct sevent
*bs
= b
;
1317 /* Put extra events last */
1318 if (!!as
->desc
!= !!bs
->desc
)
1319 return !!as
->desc
- !!bs
->desc
;
1320 if (as
->topic
&& bs
->topic
) {
1321 int n
= strcmp(as
->topic
, bs
->topic
);
1326 return strcmp(as
->name
, bs
->name
);
1329 static void wordwrap(char *s
, int start
, int max
, int corr
)
1335 int wlen
= strcspn(s
, " \t");
1337 if (column
+ wlen
>= max
&& column
> start
) {
1338 printf("\n%*s", start
, "");
1339 column
= start
+ corr
;
1341 n
= printf("%s%.*s", column
> start
? " " : "", wlen
, s
);
1350 void print_pmu_events(const char *event_glob
, bool name_only
, bool quiet_flag
,
1351 bool long_desc
, bool details_flag
)
1353 struct perf_pmu
*pmu
;
1354 struct perf_pmu_alias
*alias
;
1358 struct sevent
*aliases
;
1360 int columns
= pager_get_columns();
1365 while ((pmu
= perf_pmu__scan(pmu
)) != NULL
) {
1366 list_for_each_entry(alias
, &pmu
->aliases
, list
)
1368 if (pmu
->selectable
)
1371 aliases
= zalloc(sizeof(struct sevent
) * len
);
1376 while ((pmu
= perf_pmu__scan(pmu
)) != NULL
) {
1377 list_for_each_entry(alias
, &pmu
->aliases
, list
) {
1378 char *name
= alias
->desc
? alias
->name
:
1379 format_alias(buf
, sizeof(buf
), pmu
, alias
);
1380 bool is_cpu
= !strcmp(pmu
->name
, "cpu");
1382 if (event_glob
!= NULL
&&
1383 !(strglobmatch_nocase(name
, event_glob
) ||
1384 (!is_cpu
&& strglobmatch_nocase(alias
->name
,
1387 strglobmatch_nocase(alias
->topic
, event_glob
))))
1390 if (is_cpu
&& !name_only
&& !alias
->desc
)
1391 name
= format_alias_or(buf
, sizeof(buf
), pmu
, alias
);
1393 aliases
[j
].name
= name
;
1394 if (is_cpu
&& !name_only
&& !alias
->desc
)
1395 aliases
[j
].name
= format_alias_or(buf
,
1398 aliases
[j
].name
= strdup(aliases
[j
].name
);
1399 if (!aliases
[j
].name
)
1402 aliases
[j
].desc
= long_desc
? alias
->long_desc
:
1404 aliases
[j
].topic
= alias
->topic
;
1405 aliases
[j
].str
= alias
->str
;
1406 aliases
[j
].pmu
= pmu
->name
;
1407 aliases
[j
].metric_expr
= alias
->metric_expr
;
1408 aliases
[j
].metric_name
= alias
->metric_name
;
1411 if (pmu
->selectable
&&
1412 (event_glob
== NULL
|| strglobmatch(pmu
->name
, event_glob
))) {
1414 if (asprintf(&s
, "%s//", pmu
->name
) < 0)
1416 aliases
[j
].name
= s
;
1421 qsort(aliases
, len
, sizeof(struct sevent
), cmp_sevent
);
1422 for (j
= 0; j
< len
; j
++) {
1423 /* Skip duplicates */
1424 if (j
> 0 && !strcmp(aliases
[j
].name
, aliases
[j
- 1].name
))
1427 printf("%s ", aliases
[j
].name
);
1430 if (aliases
[j
].desc
&& !quiet_flag
) {
1433 if (aliases
[j
].topic
&& (!topic
||
1434 strcmp(topic
, aliases
[j
].topic
))) {
1435 printf("%s%s:\n", topic
? "\n" : "",
1437 topic
= aliases
[j
].topic
;
1439 printf(" %-50s\n", aliases
[j
].name
);
1440 printf("%*s", 8, "[");
1441 wordwrap(aliases
[j
].desc
, 8, columns
, 0);
1444 printf("%*s%s/%s/ ", 8, "", aliases
[j
].pmu
, aliases
[j
].str
);
1445 if (aliases
[j
].metric_name
)
1446 printf(" MetricName: %s", aliases
[j
].metric_name
);
1447 if (aliases
[j
].metric_expr
)
1448 printf(" MetricExpr: %s", aliases
[j
].metric_expr
);
1452 printf(" %-50s [Kernel PMU event]\n", aliases
[j
].name
);
1455 if (printed
&& pager_in_use())
1458 for (j
= 0; j
< len
; j
++)
1459 zfree(&aliases
[j
].name
);
1464 printf("FATAL: not enough memory to print PMU events\n");
1469 bool pmu_have_event(const char *pname
, const char *name
)
1471 struct perf_pmu
*pmu
;
1472 struct perf_pmu_alias
*alias
;
1475 while ((pmu
= perf_pmu__scan(pmu
)) != NULL
) {
1476 if (strcmp(pname
, pmu
->name
))
1478 list_for_each_entry(alias
, &pmu
->aliases
, list
)
1479 if (!strcmp(alias
->name
, name
))
1485 static FILE *perf_pmu__open_file(struct perf_pmu
*pmu
, const char *name
)
1488 char path
[PATH_MAX
];
1491 sysfs
= sysfs__mountpoint();
1495 snprintf(path
, PATH_MAX
,
1496 "%s" EVENT_SOURCE_DEVICE_PATH
"%s/%s", sysfs
, pmu
->name
, name
);
1498 if (stat(path
, &st
) < 0)
1501 return fopen(path
, "r");
1504 int perf_pmu__scan_file(struct perf_pmu
*pmu
, const char *name
, const char *fmt
,
1511 va_start(args
, fmt
);
1512 file
= perf_pmu__open_file(pmu
, name
);
1514 ret
= vfscanf(file
, fmt
, args
);