1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/list.h>
3 #include <linux/compiler.h>
4 #include <linux/string.h>
5 #include <linux/zalloc.h>
6 #include <subcmd/pager.h>
16 #include <api/fs/fs.h>
19 #include <perf/cpumap.h>
23 #include "parse-events.h"
29 struct perf_pmu perf_pmu__fake
;
31 struct perf_pmu_format
{
34 DECLARE_BITMAP(bits
, PERF_PMU_FORMAT_BITS
);
35 struct list_head list
;
38 int perf_pmu_parse(struct list_head
*list
, char *name
);
39 extern FILE *perf_pmu_in
;
41 static LIST_HEAD(pmus
);
44 * Parse & process all the sysfs attributes located under
45 * the directory specified in 'dir' parameter.
47 int perf_pmu__format_parse(char *dir
, struct list_head
*head
)
49 struct dirent
*evt_ent
;
53 format_dir
= opendir(dir
);
57 while (!ret
&& (evt_ent
= readdir(format_dir
))) {
59 char *name
= evt_ent
->d_name
;
62 if (!strcmp(name
, ".") || !strcmp(name
, ".."))
65 snprintf(path
, PATH_MAX
, "%s/%s", dir
, name
);
68 file
= fopen(path
, "r");
73 ret
= perf_pmu_parse(head
, name
);
82 * Reading/parsing the default pmu format definition, which should be
84 * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
86 static int pmu_format(const char *name
, struct list_head
*format
)
89 const char *sysfs
= sysfs__mountpoint();
94 snprintf(path
, PATH_MAX
,
95 "%s" EVENT_SOURCE_DEVICE_PATH
"%s/format", sysfs
, name
);
97 if (!file_available(path
))
100 if (perf_pmu__format_parse(path
, format
))
106 int perf_pmu__convert_scale(const char *scale
, char **end
, double *sval
)
112 * save current locale
114 lc
= setlocale(LC_NUMERIC
, NULL
);
117 * The lc string may be allocated in static storage,
118 * so get a dynamic copy to make it survive setlocale
128 * force to C locale to ensure kernel
129 * scale string is converted correctly.
130 * kernel uses default C locale.
132 setlocale(LC_NUMERIC
, "C");
134 *sval
= strtod(scale
, end
);
138 setlocale(LC_NUMERIC
, lc
);
143 static int perf_pmu__parse_scale(struct perf_pmu_alias
*alias
, char *dir
, char *name
)
151 scnprintf(path
, PATH_MAX
, "%s/%s.scale", dir
, name
);
153 fd
= open(path
, O_RDONLY
);
157 if (fstat(fd
, &st
) < 0)
160 sret
= read(fd
, scale
, sizeof(scale
)-1);
164 if (scale
[sret
- 1] == '\n')
165 scale
[sret
- 1] = '\0';
169 ret
= perf_pmu__convert_scale(scale
, NULL
, &alias
->scale
);
175 static int perf_pmu__parse_unit(struct perf_pmu_alias
*alias
, char *dir
, char *name
)
181 scnprintf(path
, PATH_MAX
, "%s/%s.unit", dir
, name
);
183 fd
= open(path
, O_RDONLY
);
187 sret
= read(fd
, alias
->unit
, UNIT_MAX_LEN
);
193 if (alias
->unit
[sret
- 1] == '\n')
194 alias
->unit
[sret
- 1] = '\0';
196 alias
->unit
[sret
] = '\0';
201 alias
->unit
[0] = '\0';
206 perf_pmu__parse_per_pkg(struct perf_pmu_alias
*alias
, char *dir
, char *name
)
211 scnprintf(path
, PATH_MAX
, "%s/%s.per-pkg", dir
, name
);
213 fd
= open(path
, O_RDONLY
);
219 alias
->per_pkg
= true;
223 static int perf_pmu__parse_snapshot(struct perf_pmu_alias
*alias
,
224 char *dir
, char *name
)
229 scnprintf(path
, PATH_MAX
, "%s/%s.snapshot", dir
, name
);
231 fd
= open(path
, O_RDONLY
);
235 alias
->snapshot
= true;
240 static void perf_pmu_assign_str(char *name
, const char *field
, char **old_str
,
246 if (*new_str
) { /* Have new string, check with old */
247 if (strcasecmp(*old_str
, *new_str
))
248 pr_debug("alias %s differs in field '%s'\n",
251 } else /* Nothing new --> keep old string */
258 static void perf_pmu_update_alias(struct perf_pmu_alias
*old
,
259 struct perf_pmu_alias
*newalias
)
261 perf_pmu_assign_str(old
->name
, "desc", &old
->desc
, &newalias
->desc
);
262 perf_pmu_assign_str(old
->name
, "long_desc", &old
->long_desc
,
263 &newalias
->long_desc
);
264 perf_pmu_assign_str(old
->name
, "topic", &old
->topic
, &newalias
->topic
);
265 perf_pmu_assign_str(old
->name
, "metric_expr", &old
->metric_expr
,
266 &newalias
->metric_expr
);
267 perf_pmu_assign_str(old
->name
, "metric_name", &old
->metric_name
,
268 &newalias
->metric_name
);
269 perf_pmu_assign_str(old
->name
, "value", &old
->str
, &newalias
->str
);
270 old
->scale
= newalias
->scale
;
271 old
->per_pkg
= newalias
->per_pkg
;
272 old
->snapshot
= newalias
->snapshot
;
273 memcpy(old
->unit
, newalias
->unit
, sizeof(old
->unit
));
276 /* Delete an alias entry. */
277 void perf_pmu_free_alias(struct perf_pmu_alias
*newalias
)
279 zfree(&newalias
->name
);
280 zfree(&newalias
->desc
);
281 zfree(&newalias
->long_desc
);
282 zfree(&newalias
->topic
);
283 zfree(&newalias
->str
);
284 zfree(&newalias
->metric_expr
);
285 zfree(&newalias
->metric_name
);
286 parse_events_terms__purge(&newalias
->terms
);
290 /* Merge an alias, search in alias list. If this name is already
291 * present merge both of them to combine all information.
293 static bool perf_pmu_merge_alias(struct perf_pmu_alias
*newalias
,
294 struct list_head
*alist
)
296 struct perf_pmu_alias
*a
;
298 list_for_each_entry(a
, alist
, list
) {
299 if (!strcasecmp(newalias
->name
, a
->name
)) {
300 perf_pmu_update_alias(a
, newalias
);
301 perf_pmu_free_alias(newalias
);
308 static int __perf_pmu__new_alias(struct list_head
*list
, char *dir
, char *name
,
309 char *desc
, char *val
,
310 char *long_desc
, char *topic
,
311 char *unit
, char *perpkg
,
316 struct parse_events_term
*term
;
317 struct perf_pmu_alias
*alias
;
322 alias
= malloc(sizeof(*alias
));
326 INIT_LIST_HEAD(&alias
->terms
);
328 alias
->unit
[0] = '\0';
329 alias
->per_pkg
= false;
330 alias
->snapshot
= false;
331 alias
->deprecated
= false;
333 ret
= parse_events_terms(&alias
->terms
, val
);
335 pr_err("Cannot parse alias %s: %d\n", val
, ret
);
340 /* Scan event and remove leading zeroes, spaces, newlines, some
341 * platforms have terms specified as
342 * event=0x0091 (read from files ../<PMU>/events/<FILE>
343 * and terms specified as event=0x91 (read from JSON files).
345 * Rebuild string to make alias->str member comparable.
347 memset(newval
, 0, sizeof(newval
));
349 list_for_each_entry(term
, &alias
->terms
, list
) {
351 ret
+= scnprintf(newval
+ ret
, sizeof(newval
) - ret
,
353 if (term
->type_val
== PARSE_EVENTS__TERM_TYPE_NUM
)
354 ret
+= scnprintf(newval
+ ret
, sizeof(newval
) - ret
,
355 "%s=%#x", term
->config
, term
->val
.num
);
356 else if (term
->type_val
== PARSE_EVENTS__TERM_TYPE_STR
)
357 ret
+= scnprintf(newval
+ ret
, sizeof(newval
) - ret
,
358 "%s=%s", term
->config
, term
->val
.str
);
361 alias
->name
= strdup(name
);
364 * load unit name and scale if available
366 perf_pmu__parse_unit(alias
, dir
, name
);
367 perf_pmu__parse_scale(alias
, dir
, name
);
368 perf_pmu__parse_per_pkg(alias
, dir
, name
);
369 perf_pmu__parse_snapshot(alias
, dir
, name
);
372 alias
->metric_expr
= metric_expr
? strdup(metric_expr
) : NULL
;
373 alias
->metric_name
= metric_name
? strdup(metric_name
): NULL
;
374 alias
->desc
= desc
? strdup(desc
) : NULL
;
375 alias
->long_desc
= long_desc
? strdup(long_desc
) :
376 desc
? strdup(desc
) : NULL
;
377 alias
->topic
= topic
? strdup(topic
) : NULL
;
379 if (perf_pmu__convert_scale(unit
, &unit
, &alias
->scale
) < 0)
381 snprintf(alias
->unit
, sizeof(alias
->unit
), "%s", unit
);
383 alias
->per_pkg
= perpkg
&& sscanf(perpkg
, "%d", &num
) == 1 && num
== 1;
384 alias
->str
= strdup(newval
);
387 alias
->deprecated
= true;
389 if (!perf_pmu_merge_alias(alias
, list
))
390 list_add_tail(&alias
->list
, list
);
395 static int perf_pmu__new_alias(struct list_head
*list
, char *dir
, char *name
, FILE *file
)
400 ret
= fread(buf
, 1, sizeof(buf
), file
);
406 /* Remove trailing newline from sysfs file */
409 return __perf_pmu__new_alias(list
, dir
, name
, NULL
, buf
, NULL
, NULL
, NULL
,
410 NULL
, NULL
, NULL
, NULL
);
413 static inline bool pmu_alias_info_file(char *name
)
418 if (len
> 5 && !strcmp(name
+ len
- 5, ".unit"))
420 if (len
> 6 && !strcmp(name
+ len
- 6, ".scale"))
422 if (len
> 8 && !strcmp(name
+ len
- 8, ".per-pkg"))
424 if (len
> 9 && !strcmp(name
+ len
- 9, ".snapshot"))
431 * Process all the sysfs attributes located under the directory
432 * specified in 'dir' parameter.
434 static int pmu_aliases_parse(char *dir
, struct list_head
*head
)
436 struct dirent
*evt_ent
;
439 event_dir
= opendir(dir
);
443 while ((evt_ent
= readdir(event_dir
))) {
445 char *name
= evt_ent
->d_name
;
448 if (!strcmp(name
, ".") || !strcmp(name
, ".."))
452 * skip info files parsed in perf_pmu__new_alias()
454 if (pmu_alias_info_file(name
))
457 scnprintf(path
, PATH_MAX
, "%s/%s", dir
, name
);
459 file
= fopen(path
, "r");
461 pr_debug("Cannot open %s\n", path
);
465 if (perf_pmu__new_alias(head
, dir
, name
, file
) < 0)
466 pr_debug("Cannot set up %s\n", name
);
475 * Reading the pmu event aliases definition, which should be located at:
476 * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
478 static int pmu_aliases(const char *name
, struct list_head
*head
)
481 const char *sysfs
= sysfs__mountpoint();
486 snprintf(path
, PATH_MAX
,
487 "%s/bus/event_source/devices/%s/events", sysfs
, name
);
489 if (!file_available(path
))
492 if (pmu_aliases_parse(path
, head
))
498 static int pmu_alias_terms(struct perf_pmu_alias
*alias
,
499 struct list_head
*terms
)
501 struct parse_events_term
*term
, *cloned
;
505 list_for_each_entry(term
, &alias
->terms
, list
) {
506 ret
= parse_events_term__clone(&cloned
, term
);
508 parse_events_terms__purge(&list
);
512 * Weak terms don't override command line options,
513 * which we don't want for implicit terms in aliases.
516 list_add_tail(&cloned
->list
, &list
);
518 list_splice(&list
, terms
);
523 * Reading/parsing the default pmu type value, which should be
525 * /sys/bus/event_source/devices/<dev>/type as sysfs attribute.
527 static int pmu_type(const char *name
, __u32
*type
)
532 const char *sysfs
= sysfs__mountpoint();
537 snprintf(path
, PATH_MAX
,
538 "%s" EVENT_SOURCE_DEVICE_PATH
"%s/type", sysfs
, name
);
540 if (access(path
, R_OK
) < 0)
543 file
= fopen(path
, "r");
547 if (1 != fscanf(file
, "%u", type
))
554 /* Add all pmus in sysfs to pmu list: */
555 static void pmu_read_sysfs(void)
560 const char *sysfs
= sysfs__mountpoint();
565 snprintf(path
, PATH_MAX
,
566 "%s" EVENT_SOURCE_DEVICE_PATH
, sysfs
);
572 while ((dent
= readdir(dir
))) {
573 if (!strcmp(dent
->d_name
, ".") || !strcmp(dent
->d_name
, ".."))
575 /* add to static LIST_HEAD(pmus): */
576 perf_pmu__find(dent
->d_name
);
582 static struct perf_cpu_map
*__pmu_cpumask(const char *path
)
585 struct perf_cpu_map
*cpus
;
587 file
= fopen(path
, "r");
591 cpus
= perf_cpu_map__read(file
);
597 * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64)
598 * may have a "cpus" file.
600 #define SYS_TEMPLATE_ID "./bus/event_source/devices/%s/identifier"
601 #define CPUS_TEMPLATE_UNCORE "%s/bus/event_source/devices/%s/cpumask"
602 #define CPUS_TEMPLATE_CPU "%s/bus/event_source/devices/%s/cpus"
604 static struct perf_cpu_map
*pmu_cpumask(const char *name
)
607 struct perf_cpu_map
*cpus
;
608 const char *sysfs
= sysfs__mountpoint();
609 const char *templates
[] = {
610 CPUS_TEMPLATE_UNCORE
,
614 const char **template;
619 for (template = templates
; *template; template++) {
620 snprintf(path
, PATH_MAX
, *template, sysfs
, name
);
621 cpus
= __pmu_cpumask(path
);
629 static bool pmu_is_uncore(const char *name
)
634 sysfs
= sysfs__mountpoint();
635 snprintf(path
, PATH_MAX
, CPUS_TEMPLATE_UNCORE
, sysfs
, name
);
636 return file_available(path
);
639 static char *pmu_id(const char *name
)
641 char path
[PATH_MAX
], *str
;
644 snprintf(path
, PATH_MAX
, SYS_TEMPLATE_ID
, name
);
646 if (sysfs__read_str(path
, &str
, &len
) < 0)
649 str
[len
- 1] = 0; /* remove line feed */
655 * PMU CORE devices have different name other than cpu in sysfs on some
657 * Looking for possible sysfs files to identify the arm core device.
659 static int is_arm_pmu_core(const char *name
)
662 const char *sysfs
= sysfs__mountpoint();
667 /* Look for cpu sysfs (specific to arm) */
668 scnprintf(path
, PATH_MAX
, "%s/bus/event_source/devices/%s/cpus",
670 return file_available(path
);
673 static char *perf_pmu__getcpuid(struct perf_pmu
*pmu
)
678 cpuid
= getenv("PERF_CPUID");
680 cpuid
= strdup(cpuid
);
682 cpuid
= get_cpuid_str(pmu
);
687 pr_debug("Using CPUID %s\n", cpuid
);
693 struct pmu_events_map
*perf_pmu__find_map(struct perf_pmu
*pmu
)
695 struct pmu_events_map
*map
;
696 char *cpuid
= perf_pmu__getcpuid(pmu
);
699 /* on some platforms which uses cpus map, cpuid can be NULL for
700 * PMUs other than CORE PMUs.
707 map
= &pmu_events_map
[i
++];
713 if (!strcmp_cpuid_str(map
->cpuid
, cpuid
))
720 bool pmu_uncore_alias_match(const char *pmu_name
, const char *name
)
722 char *tmp
= NULL
, *tok
, *str
;
725 str
= strdup(pmu_name
);
730 * uncore alias may be from different PMU with common prefix
732 tok
= strtok_r(str
, ",", &tmp
);
733 if (strncmp(pmu_name
, tok
, strlen(tok
))) {
739 * Match more complex aliases where the alias name is a comma-delimited
740 * list of tokens, orderly contained in the matching PMU name.
742 * Example: For alias "socket,pmuname" and PMU "socketX_pmunameY", we
743 * match "socket" in "socketX_pmunameY" and then "pmuname" in
746 for (; tok
; name
+= strlen(tok
), tok
= strtok_r(NULL
, ",", &tmp
)) {
747 name
= strstr(name
, tok
);
761 * From the pmu_events_map, find the table of PMU events that corresponds
762 * to the current running CPU. Then, add all PMU events from that table
765 void pmu_add_cpu_aliases_map(struct list_head
*head
, struct perf_pmu
*pmu
,
766 struct pmu_events_map
*map
)
769 const char *name
= pmu
->name
;
771 * Found a matching PMU events table. Create aliases
775 const char *cpu_name
= is_arm_pmu_core(name
) ? name
: "cpu";
776 struct pmu_event
*pe
= &map
->table
[i
++];
777 const char *pname
= pe
->pmu
? pe
->pmu
: cpu_name
;
780 if (pe
->metric_group
|| pe
->metric_name
)
785 if (pmu_is_uncore(name
) &&
786 pmu_uncore_alias_match(pname
, name
))
789 if (strcmp(pname
, name
))
793 /* need type casts to override 'const' */
794 __perf_pmu__new_alias(head
, NULL
, (char *)pe
->name
,
795 (char *)pe
->desc
, (char *)pe
->event
,
796 (char *)pe
->long_desc
, (char *)pe
->topic
,
797 (char *)pe
->unit
, (char *)pe
->perpkg
,
798 (char *)pe
->metric_expr
,
799 (char *)pe
->metric_name
,
800 (char *)pe
->deprecated
);
804 static void pmu_add_cpu_aliases(struct list_head
*head
, struct perf_pmu
*pmu
)
806 struct pmu_events_map
*map
;
808 map
= perf_pmu__find_map(pmu
);
812 pmu_add_cpu_aliases_map(head
, pmu
, map
);
815 void pmu_for_each_sys_event(pmu_sys_event_iter_fn fn
, void *data
)
820 struct pmu_sys_events
*event_table
;
823 event_table
= &pmu_sys_event_tables
[i
++];
825 if (!event_table
->table
)
829 struct pmu_event
*pe
= &event_table
->table
[j
++];
832 if (!pe
->name
&& !pe
->metric_group
&& !pe
->metric_name
)
842 struct pmu_sys_event_iter_data
{
843 struct list_head
*head
;
844 struct perf_pmu
*pmu
;
847 static int pmu_add_sys_aliases_iter_fn(struct pmu_event
*pe
, void *data
)
849 struct pmu_sys_event_iter_data
*idata
= data
;
850 struct perf_pmu
*pmu
= idata
->pmu
;
853 if (pe
->metric_group
|| pe
->metric_name
)
858 if (!pe
->compat
|| !pe
->pmu
)
861 if (!strcmp(pmu
->id
, pe
->compat
) &&
862 pmu_uncore_alias_match(pe
->pmu
, pmu
->name
)) {
863 __perf_pmu__new_alias(idata
->head
, NULL
,
867 (char *)pe
->long_desc
,
871 (char *)pe
->metric_expr
,
872 (char *)pe
->metric_name
,
873 (char *)pe
->deprecated
);
879 static void pmu_add_sys_aliases(struct list_head
*head
, struct perf_pmu
*pmu
)
881 struct pmu_sys_event_iter_data idata
= {
889 pmu_for_each_sys_event(pmu_add_sys_aliases_iter_fn
, &idata
);
892 struct perf_event_attr
* __weak
893 perf_pmu__get_default_config(struct perf_pmu
*pmu __maybe_unused
)
898 static int pmu_max_precise(const char *name
)
901 int max_precise
= -1;
903 scnprintf(path
, PATH_MAX
,
904 "bus/event_source/devices/%s/caps/max_precise",
907 sysfs__read_int(path
, &max_precise
);
911 static struct perf_pmu
*pmu_lookup(const char *name
)
913 struct perf_pmu
*pmu
;
919 * The pmu data we store & need consists of the pmu
920 * type value and format definitions. Load both right
923 if (pmu_format(name
, &format
))
927 * Check the type first to avoid unnecessary work.
929 if (pmu_type(name
, &type
))
932 if (pmu_aliases(name
, &aliases
))
935 pmu
= zalloc(sizeof(*pmu
));
939 pmu
->cpus
= pmu_cpumask(name
);
940 pmu
->name
= strdup(name
);
942 pmu
->is_uncore
= pmu_is_uncore(name
);
944 pmu
->id
= pmu_id(name
);
945 pmu
->max_precise
= pmu_max_precise(name
);
946 pmu_add_cpu_aliases(&aliases
, pmu
);
947 pmu_add_sys_aliases(&aliases
, pmu
);
949 INIT_LIST_HEAD(&pmu
->format
);
950 INIT_LIST_HEAD(&pmu
->aliases
);
951 INIT_LIST_HEAD(&pmu
->caps
);
952 list_splice(&format
, &pmu
->format
);
953 list_splice(&aliases
, &pmu
->aliases
);
954 list_add_tail(&pmu
->list
, &pmus
);
956 pmu
->default_config
= perf_pmu__get_default_config(pmu
);
961 static struct perf_pmu
*pmu_find(const char *name
)
963 struct perf_pmu
*pmu
;
965 list_for_each_entry(pmu
, &pmus
, list
)
966 if (!strcmp(pmu
->name
, name
))
972 struct perf_pmu
*perf_pmu__find_by_type(unsigned int type
)
974 struct perf_pmu
*pmu
;
976 list_for_each_entry(pmu
, &pmus
, list
)
977 if (pmu
->type
== type
)
983 struct perf_pmu
*perf_pmu__scan(struct perf_pmu
*pmu
)
986 * pmu iterator: If pmu is NULL, we start at the begin,
987 * otherwise return the next pmu. Returns NULL on end.
991 pmu
= list_prepare_entry(pmu
, &pmus
, list
);
993 list_for_each_entry_continue(pmu
, &pmus
, list
)
998 struct perf_pmu
*evsel__find_pmu(struct evsel
*evsel
)
1000 struct perf_pmu
*pmu
= NULL
;
1002 while ((pmu
= perf_pmu__scan(pmu
)) != NULL
) {
1003 if (pmu
->type
== evsel
->core
.attr
.type
)
1010 bool evsel__is_aux_event(struct evsel
*evsel
)
1012 struct perf_pmu
*pmu
= evsel__find_pmu(evsel
);
1014 return pmu
&& pmu
->auxtrace
;
1017 struct perf_pmu
*perf_pmu__find(const char *name
)
1019 struct perf_pmu
*pmu
;
1022 * Once PMU is loaded it stays in the list,
1023 * so we keep us from multiple reading/parsing
1024 * the pmu format definitions.
1026 pmu
= pmu_find(name
);
1030 return pmu_lookup(name
);
1033 static struct perf_pmu_format
*
1034 pmu_find_format(struct list_head
*formats
, const char *name
)
1036 struct perf_pmu_format
*format
;
1038 list_for_each_entry(format
, formats
, list
)
1039 if (!strcmp(format
->name
, name
))
1045 __u64
perf_pmu__format_bits(struct list_head
*formats
, const char *name
)
1047 struct perf_pmu_format
*format
= pmu_find_format(formats
, name
);
1054 for_each_set_bit(fbit
, format
->bits
, PERF_PMU_FORMAT_BITS
)
1055 bits
|= 1ULL << fbit
;
1060 int perf_pmu__format_type(struct list_head
*formats
, const char *name
)
1062 struct perf_pmu_format
*format
= pmu_find_format(formats
, name
);
1067 return format
->value
;
1071 * Sets value based on the format definition (format parameter)
1072 * and unformated value (value parameter).
1074 static void pmu_format_value(unsigned long *format
, __u64 value
, __u64
*v
,
1077 unsigned long fbit
, vbit
;
1079 for (fbit
= 0, vbit
= 0; fbit
< PERF_PMU_FORMAT_BITS
; fbit
++) {
1081 if (!test_bit(fbit
, format
))
1084 if (value
& (1llu << vbit
++))
1085 *v
|= (1llu << fbit
);
1087 *v
&= ~(1llu << fbit
);
1091 static __u64
pmu_format_max_value(const unsigned long *format
)
1095 w
= bitmap_weight(format
, PERF_PMU_FORMAT_BITS
);
1099 return (1ULL << w
) - 1;
1104 * Term is a string term, and might be a param-term. Try to look up it's value
1105 * in the remaining terms.
1106 * - We have a term like "base-or-format-term=param-term",
1107 * - We need to find the value supplied for "param-term" (with param-term named
1108 * in a config string) later on in the term list.
1110 static int pmu_resolve_param_term(struct parse_events_term
*term
,
1111 struct list_head
*head_terms
,
1114 struct parse_events_term
*t
;
1116 list_for_each_entry(t
, head_terms
, list
) {
1117 if (t
->type_val
== PARSE_EVENTS__TERM_TYPE_NUM
&&
1118 t
->config
&& !strcmp(t
->config
, term
->config
)) {
1120 *value
= t
->val
.num
;
1126 printf("Required parameter '%s' not specified\n", term
->config
);
1131 static char *pmu_formats_string(struct list_head
*formats
)
1133 struct perf_pmu_format
*format
;
1135 struct strbuf buf
= STRBUF_INIT
;
1141 /* sysfs exported terms */
1142 list_for_each_entry(format
, formats
, list
)
1143 if (strbuf_addf(&buf
, i
++ ? ",%s" : "%s", format
->name
) < 0)
1146 str
= strbuf_detach(&buf
, NULL
);
1148 strbuf_release(&buf
);
1154 * Setup one of config[12] attr members based on the
1155 * user input data - term parameter.
1157 static int pmu_config_term(const char *pmu_name
,
1158 struct list_head
*formats
,
1159 struct perf_event_attr
*attr
,
1160 struct parse_events_term
*term
,
1161 struct list_head
*head_terms
,
1162 bool zero
, struct parse_events_error
*err
)
1164 struct perf_pmu_format
*format
;
1169 * If this is a parameter we've already used for parameterized-eval,
1170 * skip it in normal eval.
1176 * Hardcoded terms should be already in, so nothing
1177 * to be done for them.
1179 if (parse_events__is_hardcoded_term(term
))
1182 format
= pmu_find_format(formats
, term
->config
);
1184 char *pmu_term
= pmu_formats_string(formats
);
1188 if (asprintf(&unknown_term
,
1189 "unknown term '%s' for pmu '%s'",
1190 term
->config
, pmu_name
) < 0)
1191 unknown_term
= NULL
;
1192 help_msg
= parse_events_formats_error_string(pmu_term
);
1194 parse_events__handle_error(err
, term
->err_term
,
1198 pr_debug("%s (%s)\n", unknown_term
, help_msg
);
1205 switch (format
->value
) {
1206 case PERF_PMU_FORMAT_VALUE_CONFIG
:
1209 case PERF_PMU_FORMAT_VALUE_CONFIG1
:
1210 vp
= &attr
->config1
;
1212 case PERF_PMU_FORMAT_VALUE_CONFIG2
:
1213 vp
= &attr
->config2
;
1220 * Either directly use a numeric term, or try to translate string terms
1221 * using event parameters.
1223 if (term
->type_val
== PARSE_EVENTS__TERM_TYPE_NUM
) {
1224 if (term
->no_value
&&
1225 bitmap_weight(format
->bits
, PERF_PMU_FORMAT_BITS
) > 1) {
1227 parse_events__handle_error(err
, term
->err_val
,
1228 strdup("no value assigned for term"),
1234 val
= term
->val
.num
;
1235 } else if (term
->type_val
== PARSE_EVENTS__TERM_TYPE_STR
) {
1236 if (strcmp(term
->val
.str
, "?")) {
1238 pr_info("Invalid sysfs entry %s=%s\n",
1239 term
->config
, term
->val
.str
);
1242 parse_events__handle_error(err
, term
->err_val
,
1243 strdup("expected numeric value"),
1249 if (pmu_resolve_param_term(term
, head_terms
, &val
))
1254 max_val
= pmu_format_max_value(format
->bits
);
1255 if (val
> max_val
) {
1259 parse_events__handle_error(err
, term
->err_val
,
1261 "value too big for format, maximum is %llu",
1262 (unsigned long long)max_val
) < 0
1263 ? strdup("value too big for format")
1269 * Assume we don't care if !err, in which case the value will be
1270 * silently truncated.
1274 pmu_format_value(format
->bits
, val
, vp
, zero
);
1278 int perf_pmu__config_terms(const char *pmu_name
, struct list_head
*formats
,
1279 struct perf_event_attr
*attr
,
1280 struct list_head
*head_terms
,
1281 bool zero
, struct parse_events_error
*err
)
1283 struct parse_events_term
*term
;
1285 list_for_each_entry(term
, head_terms
, list
) {
1286 if (pmu_config_term(pmu_name
, formats
, attr
, term
, head_terms
,
1295 * Configures event's 'attr' parameter based on the:
1296 * 1) users input - specified in terms parameter
1297 * 2) pmu format definitions - specified by pmu parameter
1299 int perf_pmu__config(struct perf_pmu
*pmu
, struct perf_event_attr
*attr
,
1300 struct list_head
*head_terms
,
1301 struct parse_events_error
*err
)
1303 bool zero
= !!pmu
->default_config
;
1305 attr
->type
= pmu
->type
;
1306 return perf_pmu__config_terms(pmu
->name
, &pmu
->format
, attr
,
1307 head_terms
, zero
, err
);
1310 static struct perf_pmu_alias
*pmu_find_alias(struct perf_pmu
*pmu
,
1311 struct parse_events_term
*term
)
1313 struct perf_pmu_alias
*alias
;
1316 if (parse_events__is_hardcoded_term(term
))
1319 if (term
->type_val
== PARSE_EVENTS__TERM_TYPE_NUM
) {
1320 if (term
->val
.num
!= 1)
1322 if (pmu_find_format(&pmu
->format
, term
->config
))
1324 name
= term
->config
;
1325 } else if (term
->type_val
== PARSE_EVENTS__TERM_TYPE_STR
) {
1326 if (strcasecmp(term
->config
, "event"))
1328 name
= term
->val
.str
;
1333 list_for_each_entry(alias
, &pmu
->aliases
, list
) {
1334 if (!strcasecmp(alias
->name
, name
))
1341 static int check_info_data(struct perf_pmu_alias
*alias
,
1342 struct perf_pmu_info
*info
)
1345 * Only one term in event definition can
1346 * define unit, scale and snapshot, fail
1347 * if there's more than one.
1349 if ((info
->unit
&& alias
->unit
[0]) ||
1350 (info
->scale
&& alias
->scale
) ||
1351 (info
->snapshot
&& alias
->snapshot
))
1355 info
->unit
= alias
->unit
;
1358 info
->scale
= alias
->scale
;
1360 if (alias
->snapshot
)
1361 info
->snapshot
= alias
->snapshot
;
1367 * Find alias in the terms list and replace it with the terms
1368 * defined for the alias
1370 int perf_pmu__check_alias(struct perf_pmu
*pmu
, struct list_head
*head_terms
,
1371 struct perf_pmu_info
*info
)
1373 struct parse_events_term
*term
, *h
;
1374 struct perf_pmu_alias
*alias
;
1377 info
->per_pkg
= false;
1380 * Mark unit and scale as not set
1381 * (different from default values, see below)
1385 info
->snapshot
= false;
1386 info
->metric_expr
= NULL
;
1387 info
->metric_name
= NULL
;
1389 list_for_each_entry_safe(term
, h
, head_terms
, list
) {
1390 alias
= pmu_find_alias(pmu
, term
);
1393 ret
= pmu_alias_terms(alias
, &term
->list
);
1397 ret
= check_info_data(alias
, info
);
1402 info
->per_pkg
= true;
1403 info
->metric_expr
= alias
->metric_expr
;
1404 info
->metric_name
= alias
->metric_name
;
1406 list_del_init(&term
->list
);
1407 parse_events_term__delete(term
);
1411 * if no unit or scale foundin aliases, then
1412 * set defaults as for evsel
1413 * unit cannot left to NULL
1415 if (info
->unit
== NULL
)
1418 if (info
->scale
== 0.0)
1424 int perf_pmu__new_format(struct list_head
*list
, char *name
,
1425 int config
, unsigned long *bits
)
1427 struct perf_pmu_format
*format
;
1429 format
= zalloc(sizeof(*format
));
1433 format
->name
= strdup(name
);
1434 format
->value
= config
;
1435 memcpy(format
->bits
, bits
, sizeof(format
->bits
));
1437 list_add_tail(&format
->list
, list
);
1441 void perf_pmu__set_format(unsigned long *bits
, long from
, long to
)
1448 memset(bits
, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS
));
1449 for (b
= from
; b
<= to
; b
++)
1453 void perf_pmu__del_formats(struct list_head
*formats
)
1455 struct perf_pmu_format
*fmt
, *tmp
;
1457 list_for_each_entry_safe(fmt
, tmp
, formats
, list
) {
1458 list_del(&fmt
->list
);
1464 static int sub_non_neg(int a
, int b
)
1471 static char *format_alias(char *buf
, int len
, struct perf_pmu
*pmu
,
1472 struct perf_pmu_alias
*alias
)
1474 struct parse_events_term
*term
;
1475 int used
= snprintf(buf
, len
, "%s/%s", pmu
->name
, alias
->name
);
1477 list_for_each_entry(term
, &alias
->terms
, list
) {
1478 if (term
->type_val
== PARSE_EVENTS__TERM_TYPE_STR
)
1479 used
+= snprintf(buf
+ used
, sub_non_neg(len
, used
),
1480 ",%s=%s", term
->config
,
1484 if (sub_non_neg(len
, used
) > 0) {
1488 if (sub_non_neg(len
, used
) > 0) {
1492 buf
[len
- 1] = '\0';
1497 static char *format_alias_or(char *buf
, int len
, struct perf_pmu
*pmu
,
1498 struct perf_pmu_alias
*alias
)
1500 snprintf(buf
, len
, "%s OR %s/%s/", alias
->name
, pmu
->name
, alias
->name
);
1515 static int cmp_sevent(const void *a
, const void *b
)
1517 const struct sevent
*as
= a
;
1518 const struct sevent
*bs
= b
;
1520 /* Put extra events last */
1521 if (!!as
->desc
!= !!bs
->desc
)
1522 return !!as
->desc
- !!bs
->desc
;
1523 if (as
->topic
&& bs
->topic
) {
1524 int n
= strcmp(as
->topic
, bs
->topic
);
1530 /* Order CPU core events to be first */
1531 if (as
->is_cpu
!= bs
->is_cpu
)
1532 return bs
->is_cpu
- as
->is_cpu
;
1534 return strcmp(as
->name
, bs
->name
);
1537 static void wordwrap(char *s
, int start
, int max
, int corr
)
1543 int wlen
= strcspn(s
, " \t");
1545 if (column
+ wlen
>= max
&& column
> start
) {
1546 printf("\n%*s", start
, "");
1547 column
= start
+ corr
;
1549 n
= printf("%s%.*s", column
> start
? " " : "", wlen
, s
);
1558 bool is_pmu_core(const char *name
)
1560 return !strcmp(name
, "cpu") || is_arm_pmu_core(name
);
1563 void print_pmu_events(const char *event_glob
, bool name_only
, bool quiet_flag
,
1564 bool long_desc
, bool details_flag
, bool deprecated
)
1566 struct perf_pmu
*pmu
;
1567 struct perf_pmu_alias
*alias
;
1571 struct sevent
*aliases
;
1573 int columns
= pager_get_columns();
1578 while ((pmu
= perf_pmu__scan(pmu
)) != NULL
) {
1579 list_for_each_entry(alias
, &pmu
->aliases
, list
)
1581 if (pmu
->selectable
)
1584 aliases
= zalloc(sizeof(struct sevent
) * len
);
1589 while ((pmu
= perf_pmu__scan(pmu
)) != NULL
) {
1590 list_for_each_entry(alias
, &pmu
->aliases
, list
) {
1591 char *name
= alias
->desc
? alias
->name
:
1592 format_alias(buf
, sizeof(buf
), pmu
, alias
);
1593 bool is_cpu
= is_pmu_core(pmu
->name
);
1595 if (alias
->deprecated
&& !deprecated
)
1598 if (event_glob
!= NULL
&&
1599 !(strglobmatch_nocase(name
, event_glob
) ||
1600 (!is_cpu
&& strglobmatch_nocase(alias
->name
,
1603 strglobmatch_nocase(alias
->topic
, event_glob
))))
1606 if (is_cpu
&& !name_only
&& !alias
->desc
)
1607 name
= format_alias_or(buf
, sizeof(buf
), pmu
, alias
);
1609 aliases
[j
].name
= name
;
1610 if (is_cpu
&& !name_only
&& !alias
->desc
)
1611 aliases
[j
].name
= format_alias_or(buf
,
1614 aliases
[j
].name
= strdup(aliases
[j
].name
);
1615 if (!aliases
[j
].name
)
1618 aliases
[j
].desc
= long_desc
? alias
->long_desc
:
1620 aliases
[j
].topic
= alias
->topic
;
1621 aliases
[j
].str
= alias
->str
;
1622 aliases
[j
].pmu
= pmu
->name
;
1623 aliases
[j
].metric_expr
= alias
->metric_expr
;
1624 aliases
[j
].metric_name
= alias
->metric_name
;
1625 aliases
[j
].is_cpu
= is_cpu
;
1628 if (pmu
->selectable
&&
1629 (event_glob
== NULL
|| strglobmatch(pmu
->name
, event_glob
))) {
1631 if (asprintf(&s
, "%s//", pmu
->name
) < 0)
1633 aliases
[j
].name
= s
;
1638 qsort(aliases
, len
, sizeof(struct sevent
), cmp_sevent
);
1639 for (j
= 0; j
< len
; j
++) {
1640 /* Skip duplicates */
1641 if (j
> 0 && !strcmp(aliases
[j
].name
, aliases
[j
- 1].name
))
1644 printf("%s ", aliases
[j
].name
);
1647 if (aliases
[j
].desc
&& !quiet_flag
) {
1650 if (aliases
[j
].topic
&& (!topic
||
1651 strcmp(topic
, aliases
[j
].topic
))) {
1652 printf("%s%s:\n", topic
? "\n" : "",
1654 topic
= aliases
[j
].topic
;
1656 printf(" %-50s\n", aliases
[j
].name
);
1657 printf("%*s", 8, "[");
1658 wordwrap(aliases
[j
].desc
, 8, columns
, 0);
1661 printf("%*s%s/%s/ ", 8, "", aliases
[j
].pmu
, aliases
[j
].str
);
1662 if (aliases
[j
].metric_name
)
1663 printf(" MetricName: %s", aliases
[j
].metric_name
);
1664 if (aliases
[j
].metric_expr
)
1665 printf(" MetricExpr: %s", aliases
[j
].metric_expr
);
1669 printf(" %-50s [Kernel PMU event]\n", aliases
[j
].name
);
1672 if (printed
&& pager_in_use())
1675 for (j
= 0; j
< len
; j
++)
1676 zfree(&aliases
[j
].name
);
1681 printf("FATAL: not enough memory to print PMU events\n");
1686 bool pmu_have_event(const char *pname
, const char *name
)
1688 struct perf_pmu
*pmu
;
1689 struct perf_pmu_alias
*alias
;
1692 while ((pmu
= perf_pmu__scan(pmu
)) != NULL
) {
1693 if (strcmp(pname
, pmu
->name
))
1695 list_for_each_entry(alias
, &pmu
->aliases
, list
)
1696 if (!strcmp(alias
->name
, name
))
1702 static FILE *perf_pmu__open_file(struct perf_pmu
*pmu
, const char *name
)
1704 char path
[PATH_MAX
];
1707 sysfs
= sysfs__mountpoint();
1711 snprintf(path
, PATH_MAX
,
1712 "%s" EVENT_SOURCE_DEVICE_PATH
"%s/%s", sysfs
, pmu
->name
, name
);
1713 if (!file_available(path
))
1715 return fopen(path
, "r");
1718 int perf_pmu__scan_file(struct perf_pmu
*pmu
, const char *name
, const char *fmt
,
1725 va_start(args
, fmt
);
1726 file
= perf_pmu__open_file(pmu
, name
);
1728 ret
= vfscanf(file
, fmt
, args
);
1735 static int perf_pmu__new_caps(struct list_head
*list
, char *name
, char *value
)
1737 struct perf_pmu_caps
*caps
= zalloc(sizeof(*caps
));
1742 caps
->name
= strdup(name
);
1745 caps
->value
= strndup(value
, strlen(value
) - 1);
1748 list_add_tail(&caps
->list
, list
);
1760 * Reading/parsing the given pmu capabilities, which should be located at:
1761 * /sys/bus/event_source/devices/<dev>/caps as sysfs group attributes.
1762 * Return the number of capabilities
1764 int perf_pmu__caps_parse(struct perf_pmu
*pmu
)
1767 char caps_path
[PATH_MAX
];
1768 const char *sysfs
= sysfs__mountpoint();
1770 struct dirent
*evt_ent
;
1776 snprintf(caps_path
, PATH_MAX
,
1777 "%s" EVENT_SOURCE_DEVICE_PATH
"%s/caps", sysfs
, pmu
->name
);
1779 if (stat(caps_path
, &st
) < 0)
1780 return 0; /* no error if caps does not exist */
1782 caps_dir
= opendir(caps_path
);
1786 while ((evt_ent
= readdir(caps_dir
)) != NULL
) {
1787 char path
[PATH_MAX
+ NAME_MAX
+ 1];
1788 char *name
= evt_ent
->d_name
;
1792 if (!strcmp(name
, ".") || !strcmp(name
, ".."))
1795 snprintf(path
, sizeof(path
), "%s/%s", caps_path
, name
);
1797 file
= fopen(path
, "r");
1801 if (!fgets(value
, sizeof(value
), file
) ||
1802 (perf_pmu__new_caps(&pmu
->caps
, name
, value
) < 0)) {