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_format {
32 DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
33 struct list_head list;
36 int perf_pmu_parse(struct list_head *list, char *name);
37 extern FILE *perf_pmu_in;
39 static LIST_HEAD(pmus);
42 * Parse & process all the sysfs attributes located under
43 * the directory specified in 'dir' parameter.
45 int perf_pmu__format_parse(char *dir, struct list_head *head)
47 struct dirent *evt_ent;
51 format_dir = opendir(dir);
55 while (!ret && (evt_ent = readdir(format_dir))) {
57 char *name = evt_ent->d_name;
60 if (!strcmp(name, ".") || !strcmp(name, ".."))
63 snprintf(path, PATH_MAX, "%s/%s", dir, name);
66 file = fopen(path, "r");
71 ret = perf_pmu_parse(head, name);
80 * Reading/parsing the default pmu format definition, which should be
82 * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
84 static int pmu_format(const char *name, struct list_head *format)
87 const char *sysfs = sysfs__mountpoint();
92 snprintf(path, PATH_MAX,
93 "%s" EVENT_SOURCE_DEVICE_PATH "%s/format", sysfs, name);
95 if (!file_available(path))
98 if (perf_pmu__format_parse(path, format))
104 int perf_pmu__convert_scale(const char *scale, char **end, double *sval)
110 * save current locale
112 lc = setlocale(LC_NUMERIC, NULL);
115 * The lc string may be allocated in static storage,
116 * so get a dynamic copy to make it survive setlocale
126 * force to C locale to ensure kernel
127 * scale string is converted correctly.
128 * kernel uses default C locale.
130 setlocale(LC_NUMERIC, "C");
132 *sval = strtod(scale, end);
136 setlocale(LC_NUMERIC, lc);
141 static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *name)
149 scnprintf(path, PATH_MAX, "%s/%s.scale", dir, name);
151 fd = open(path, O_RDONLY);
155 if (fstat(fd, &st) < 0)
158 sret = read(fd, scale, sizeof(scale)-1);
162 if (scale[sret - 1] == '\n')
163 scale[sret - 1] = '\0';
167 ret = perf_pmu__convert_scale(scale, NULL, &alias->scale);
173 static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *name)
179 scnprintf(path, PATH_MAX, "%s/%s.unit", dir, name);
181 fd = open(path, O_RDONLY);
185 sret = read(fd, alias->unit, UNIT_MAX_LEN);
191 if (alias->unit[sret - 1] == '\n')
192 alias->unit[sret - 1] = '\0';
194 alias->unit[sret] = '\0';
199 alias->unit[0] = '\0';
204 perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, char *dir, char *name)
209 scnprintf(path, PATH_MAX, "%s/%s.per-pkg", dir, name);
211 fd = open(path, O_RDONLY);
217 alias->per_pkg = true;
221 static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias,
222 char *dir, char *name)
227 scnprintf(path, PATH_MAX, "%s/%s.snapshot", dir, name);
229 fd = open(path, O_RDONLY);
233 alias->snapshot = true;
238 static void perf_pmu_assign_str(char *name, const char *field, char **old_str,
244 if (*new_str) { /* Have new string, check with old */
245 if (strcasecmp(*old_str, *new_str))
246 pr_debug("alias %s differs in field '%s'\n",
249 } else /* Nothing new --> keep old string */
256 static void perf_pmu_update_alias(struct perf_pmu_alias *old,
257 struct perf_pmu_alias *newalias)
259 perf_pmu_assign_str(old->name, "desc", &old->desc, &newalias->desc);
260 perf_pmu_assign_str(old->name, "long_desc", &old->long_desc,
261 &newalias->long_desc);
262 perf_pmu_assign_str(old->name, "topic", &old->topic, &newalias->topic);
263 perf_pmu_assign_str(old->name, "metric_expr", &old->metric_expr,
264 &newalias->metric_expr);
265 perf_pmu_assign_str(old->name, "metric_name", &old->metric_name,
266 &newalias->metric_name);
267 perf_pmu_assign_str(old->name, "value", &old->str, &newalias->str);
268 old->scale = newalias->scale;
269 old->per_pkg = newalias->per_pkg;
270 old->snapshot = newalias->snapshot;
271 memcpy(old->unit, newalias->unit, sizeof(old->unit));
274 /* Delete an alias entry. */
275 static void perf_pmu_free_alias(struct perf_pmu_alias *newalias)
277 zfree(&newalias->name);
278 zfree(&newalias->desc);
279 zfree(&newalias->long_desc);
280 zfree(&newalias->topic);
281 zfree(&newalias->str);
282 zfree(&newalias->metric_expr);
283 zfree(&newalias->metric_name);
284 parse_events_terms__purge(&newalias->terms);
288 /* Merge an alias, search in alias list. If this name is already
289 * present merge both of them to combine all information.
291 static bool perf_pmu_merge_alias(struct perf_pmu_alias *newalias,
292 struct list_head *alist)
294 struct perf_pmu_alias *a;
296 list_for_each_entry(a, alist, list) {
297 if (!strcasecmp(newalias->name, a->name)) {
298 perf_pmu_update_alias(a, newalias);
299 perf_pmu_free_alias(newalias);
306 static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name,
307 char *desc, char *val,
308 char *long_desc, char *topic,
309 char *unit, char *perpkg,
314 struct parse_events_term *term;
315 struct perf_pmu_alias *alias;
320 alias = malloc(sizeof(*alias));
324 INIT_LIST_HEAD(&alias->terms);
326 alias->unit[0] = '\0';
327 alias->per_pkg = false;
328 alias->snapshot = false;
329 alias->deprecated = false;
331 ret = parse_events_terms(&alias->terms, val);
333 pr_err("Cannot parse alias %s: %d\n", val, ret);
338 /* Scan event and remove leading zeroes, spaces, newlines, some
339 * platforms have terms specified as
340 * event=0x0091 (read from files ../<PMU>/events/<FILE>
341 * and terms specified as event=0x91 (read from JSON files).
343 * Rebuild string to make alias->str member comparable.
345 memset(newval, 0, sizeof(newval));
347 list_for_each_entry(term, &alias->terms, list) {
349 ret += scnprintf(newval + ret, sizeof(newval) - ret,
351 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM)
352 ret += scnprintf(newval + ret, sizeof(newval) - ret,
353 "%s=%#x", term->config, term->val.num);
354 else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
355 ret += scnprintf(newval + ret, sizeof(newval) - ret,
356 "%s=%s", term->config, term->val.str);
359 alias->name = strdup(name);
362 * load unit name and scale if available
364 perf_pmu__parse_unit(alias, dir, name);
365 perf_pmu__parse_scale(alias, dir, name);
366 perf_pmu__parse_per_pkg(alias, dir, name);
367 perf_pmu__parse_snapshot(alias, dir, name);
370 alias->metric_expr = metric_expr ? strdup(metric_expr) : NULL;
371 alias->metric_name = metric_name ? strdup(metric_name): NULL;
372 alias->desc = desc ? strdup(desc) : NULL;
373 alias->long_desc = long_desc ? strdup(long_desc) :
374 desc ? strdup(desc) : NULL;
375 alias->topic = topic ? strdup(topic) : NULL;
377 if (perf_pmu__convert_scale(unit, &unit, &alias->scale) < 0)
379 snprintf(alias->unit, sizeof(alias->unit), "%s", unit);
381 alias->per_pkg = perpkg && sscanf(perpkg, "%d", &num) == 1 && num == 1;
382 alias->str = strdup(newval);
385 alias->deprecated = true;
387 if (!perf_pmu_merge_alias(alias, list))
388 list_add_tail(&alias->list, list);
393 static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file)
398 ret = fread(buf, 1, sizeof(buf), file);
404 /* Remove trailing newline from sysfs file */
407 return __perf_pmu__new_alias(list, dir, name, NULL, buf, NULL, NULL, NULL,
408 NULL, NULL, NULL, NULL);
411 static inline bool pmu_alias_info_file(char *name)
416 if (len > 5 && !strcmp(name + len - 5, ".unit"))
418 if (len > 6 && !strcmp(name + len - 6, ".scale"))
420 if (len > 8 && !strcmp(name + len - 8, ".per-pkg"))
422 if (len > 9 && !strcmp(name + len - 9, ".snapshot"))
429 * Process all the sysfs attributes located under the directory
430 * specified in 'dir' parameter.
432 static int pmu_aliases_parse(char *dir, struct list_head *head)
434 struct dirent *evt_ent;
437 event_dir = opendir(dir);
441 while ((evt_ent = readdir(event_dir))) {
443 char *name = evt_ent->d_name;
446 if (!strcmp(name, ".") || !strcmp(name, ".."))
450 * skip info files parsed in perf_pmu__new_alias()
452 if (pmu_alias_info_file(name))
455 scnprintf(path, PATH_MAX, "%s/%s", dir, name);
457 file = fopen(path, "r");
459 pr_debug("Cannot open %s\n", path);
463 if (perf_pmu__new_alias(head, dir, name, file) < 0)
464 pr_debug("Cannot set up %s\n", name);
473 * Reading the pmu event aliases definition, which should be located at:
474 * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
476 static int pmu_aliases(const char *name, struct list_head *head)
479 const char *sysfs = sysfs__mountpoint();
484 snprintf(path, PATH_MAX,
485 "%s/bus/event_source/devices/%s/events", sysfs, name);
487 if (!file_available(path))
490 if (pmu_aliases_parse(path, head))
496 static int pmu_alias_terms(struct perf_pmu_alias *alias,
497 struct list_head *terms)
499 struct parse_events_term *term, *cloned;
503 list_for_each_entry(term, &alias->terms, list) {
504 ret = parse_events_term__clone(&cloned, term);
506 parse_events_terms__purge(&list);
510 * Weak terms don't override command line options,
511 * which we don't want for implicit terms in aliases.
514 list_add_tail(&cloned->list, &list);
516 list_splice(&list, terms);
521 * Reading/parsing the default pmu type value, which should be
523 * /sys/bus/event_source/devices/<dev>/type as sysfs attribute.
525 static int pmu_type(const char *name, __u32 *type)
530 const char *sysfs = sysfs__mountpoint();
535 snprintf(path, PATH_MAX,
536 "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name);
538 if (access(path, R_OK) < 0)
541 file = fopen(path, "r");
545 if (1 != fscanf(file, "%u", type))
552 /* Add all pmus in sysfs to pmu list: */
553 static void pmu_read_sysfs(void)
558 const char *sysfs = sysfs__mountpoint();
563 snprintf(path, PATH_MAX,
564 "%s" EVENT_SOURCE_DEVICE_PATH, sysfs);
570 while ((dent = readdir(dir))) {
571 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
573 /* add to static LIST_HEAD(pmus): */
574 perf_pmu__find(dent->d_name);
580 static struct perf_cpu_map *__pmu_cpumask(const char *path)
583 struct perf_cpu_map *cpus;
585 file = fopen(path, "r");
589 cpus = perf_cpu_map__read(file);
595 * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64)
596 * may have a "cpus" file.
598 #define CPUS_TEMPLATE_UNCORE "%s/bus/event_source/devices/%s/cpumask"
599 #define CPUS_TEMPLATE_CPU "%s/bus/event_source/devices/%s/cpus"
601 static struct perf_cpu_map *pmu_cpumask(const char *name)
604 struct perf_cpu_map *cpus;
605 const char *sysfs = sysfs__mountpoint();
606 const char *templates[] = {
607 CPUS_TEMPLATE_UNCORE,
611 const char **template;
616 for (template = templates; *template; template++) {
617 snprintf(path, PATH_MAX, *template, sysfs, name);
618 cpus = __pmu_cpumask(path);
626 static bool pmu_is_uncore(const char *name)
631 sysfs = sysfs__mountpoint();
632 snprintf(path, PATH_MAX, CPUS_TEMPLATE_UNCORE, sysfs, name);
633 return file_available(path);
637 * PMU CORE devices have different name other than cpu in sysfs on some
639 * Looking for possible sysfs files to identify the arm core device.
641 static int is_arm_pmu_core(const char *name)
644 const char *sysfs = sysfs__mountpoint();
649 /* Look for cpu sysfs (specific to arm) */
650 scnprintf(path, PATH_MAX, "%s/bus/event_source/devices/%s/cpus",
652 return file_available(path);
655 static char *perf_pmu__getcpuid(struct perf_pmu *pmu)
660 cpuid = getenv("PERF_CPUID");
662 cpuid = strdup(cpuid);
664 cpuid = get_cpuid_str(pmu);
669 pr_debug("Using CPUID %s\n", cpuid);
675 struct pmu_events_map *perf_pmu__find_map(struct perf_pmu *pmu)
677 struct pmu_events_map *map;
678 char *cpuid = perf_pmu__getcpuid(pmu);
681 /* on some platforms which uses cpus map, cpuid can be NULL for
682 * PMUs other than CORE PMUs.
689 map = &pmu_events_map[i++];
695 if (!strcmp_cpuid_str(map->cpuid, cpuid))
702 bool pmu_uncore_alias_match(const char *pmu_name, const char *name)
704 char *tmp = NULL, *tok, *str;
707 str = strdup(pmu_name);
712 * uncore alias may be from different PMU with common prefix
714 tok = strtok_r(str, ",", &tmp);
715 if (strncmp(pmu_name, tok, strlen(tok))) {
721 * Match more complex aliases where the alias name is a comma-delimited
722 * list of tokens, orderly contained in the matching PMU name.
724 * Example: For alias "socket,pmuname" and PMU "socketX_pmunameY", we
725 * match "socket" in "socketX_pmunameY" and then "pmuname" in
728 for (; tok; name += strlen(tok), tok = strtok_r(NULL, ",", &tmp)) {
729 name = strstr(name, tok);
743 * From the pmu_events_map, find the table of PMU events that corresponds
744 * to the current running CPU. Then, add all PMU events from that table
747 void pmu_add_cpu_aliases_map(struct list_head *head, struct perf_pmu *pmu,
748 struct pmu_events_map *map)
751 const char *name = pmu->name;
753 * Found a matching PMU events table. Create aliases
757 const char *cpu_name = is_arm_pmu_core(name) ? name : "cpu";
758 struct pmu_event *pe = &map->table[i++];
759 const char *pname = pe->pmu ? pe->pmu : cpu_name;
762 if (pe->metric_group || pe->metric_name)
767 if (pmu_is_uncore(name) &&
768 pmu_uncore_alias_match(pname, name))
771 if (strcmp(pname, name))
775 /* need type casts to override 'const' */
776 __perf_pmu__new_alias(head, NULL, (char *)pe->name,
777 (char *)pe->desc, (char *)pe->event,
778 (char *)pe->long_desc, (char *)pe->topic,
779 (char *)pe->unit, (char *)pe->perpkg,
780 (char *)pe->metric_expr,
781 (char *)pe->metric_name,
782 (char *)pe->deprecated);
786 static void pmu_add_cpu_aliases(struct list_head *head, struct perf_pmu *pmu)
788 struct pmu_events_map *map;
790 map = perf_pmu__find_map(pmu);
794 pmu_add_cpu_aliases_map(head, pmu, map);
797 struct perf_event_attr * __weak
798 perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
803 static int pmu_max_precise(const char *name)
806 int max_precise = -1;
808 scnprintf(path, PATH_MAX,
809 "bus/event_source/devices/%s/caps/max_precise",
812 sysfs__read_int(path, &max_precise);
816 static struct perf_pmu *pmu_lookup(const char *name)
818 struct perf_pmu *pmu;
824 * The pmu data we store & need consists of the pmu
825 * type value and format definitions. Load both right
828 if (pmu_format(name, &format))
832 * Check the type first to avoid unnecessary work.
834 if (pmu_type(name, &type))
837 if (pmu_aliases(name, &aliases))
840 pmu = zalloc(sizeof(*pmu));
844 pmu->cpus = pmu_cpumask(name);
845 pmu->name = strdup(name);
847 pmu->is_uncore = pmu_is_uncore(name);
848 pmu->max_precise = pmu_max_precise(name);
849 pmu_add_cpu_aliases(&aliases, pmu);
851 INIT_LIST_HEAD(&pmu->format);
852 INIT_LIST_HEAD(&pmu->aliases);
853 INIT_LIST_HEAD(&pmu->caps);
854 list_splice(&format, &pmu->format);
855 list_splice(&aliases, &pmu->aliases);
856 list_add_tail(&pmu->list, &pmus);
858 pmu->default_config = perf_pmu__get_default_config(pmu);
863 static struct perf_pmu *pmu_find(const char *name)
865 struct perf_pmu *pmu;
867 list_for_each_entry(pmu, &pmus, list)
868 if (!strcmp(pmu->name, name))
874 struct perf_pmu *perf_pmu__find_by_type(unsigned int type)
876 struct perf_pmu *pmu;
878 list_for_each_entry(pmu, &pmus, list)
879 if (pmu->type == type)
885 struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
888 * pmu iterator: If pmu is NULL, we start at the begin,
889 * otherwise return the next pmu. Returns NULL on end.
893 pmu = list_prepare_entry(pmu, &pmus, list);
895 list_for_each_entry_continue(pmu, &pmus, list)
900 struct perf_pmu *evsel__find_pmu(struct evsel *evsel)
902 struct perf_pmu *pmu = NULL;
904 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
905 if (pmu->type == evsel->core.attr.type)
912 bool evsel__is_aux_event(struct evsel *evsel)
914 struct perf_pmu *pmu = evsel__find_pmu(evsel);
916 return pmu && pmu->auxtrace;
919 struct perf_pmu *perf_pmu__find(const char *name)
921 struct perf_pmu *pmu;
924 * Once PMU is loaded it stays in the list,
925 * so we keep us from multiple reading/parsing
926 * the pmu format definitions.
928 pmu = pmu_find(name);
932 return pmu_lookup(name);
935 static struct perf_pmu_format *
936 pmu_find_format(struct list_head *formats, const char *name)
938 struct perf_pmu_format *format;
940 list_for_each_entry(format, formats, list)
941 if (!strcmp(format->name, name))
947 __u64 perf_pmu__format_bits(struct list_head *formats, const char *name)
949 struct perf_pmu_format *format = pmu_find_format(formats, name);
956 for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
957 bits |= 1ULL << fbit;
962 int perf_pmu__format_type(struct list_head *formats, const char *name)
964 struct perf_pmu_format *format = pmu_find_format(formats, name);
969 return format->value;
973 * Sets value based on the format definition (format parameter)
974 * and unformated value (value parameter).
976 static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
979 unsigned long fbit, vbit;
981 for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
983 if (!test_bit(fbit, format))
986 if (value & (1llu << vbit++))
987 *v |= (1llu << fbit);
989 *v &= ~(1llu << fbit);
993 static __u64 pmu_format_max_value(const unsigned long *format)
997 w = bitmap_weight(format, PERF_PMU_FORMAT_BITS);
1001 return (1ULL << w) - 1;
1006 * Term is a string term, and might be a param-term. Try to look up it's value
1007 * in the remaining terms.
1008 * - We have a term like "base-or-format-term=param-term",
1009 * - We need to find the value supplied for "param-term" (with param-term named
1010 * in a config string) later on in the term list.
1012 static int pmu_resolve_param_term(struct parse_events_term *term,
1013 struct list_head *head_terms,
1016 struct parse_events_term *t;
1018 list_for_each_entry(t, head_terms, list) {
1019 if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM &&
1020 t->config && !strcmp(t->config, term->config)) {
1022 *value = t->val.num;
1028 printf("Required parameter '%s' not specified\n", term->config);
1033 static char *pmu_formats_string(struct list_head *formats)
1035 struct perf_pmu_format *format;
1037 struct strbuf buf = STRBUF_INIT;
1043 /* sysfs exported terms */
1044 list_for_each_entry(format, formats, list)
1045 if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
1048 str = strbuf_detach(&buf, NULL);
1050 strbuf_release(&buf);
1056 * Setup one of config[12] attr members based on the
1057 * user input data - term parameter.
1059 static int pmu_config_term(const char *pmu_name,
1060 struct list_head *formats,
1061 struct perf_event_attr *attr,
1062 struct parse_events_term *term,
1063 struct list_head *head_terms,
1064 bool zero, struct parse_events_error *err)
1066 struct perf_pmu_format *format;
1071 * If this is a parameter we've already used for parameterized-eval,
1072 * skip it in normal eval.
1078 * Hardcoded terms should be already in, so nothing
1079 * to be done for them.
1081 if (parse_events__is_hardcoded_term(term))
1084 format = pmu_find_format(formats, term->config);
1086 char *pmu_term = pmu_formats_string(formats);
1090 if (asprintf(&unknown_term,
1091 "unknown term '%s' for pmu '%s'",
1092 term->config, pmu_name) < 0)
1093 unknown_term = NULL;
1094 help_msg = parse_events_formats_error_string(pmu_term);
1096 parse_events__handle_error(err, term->err_term,
1100 pr_debug("%s (%s)\n", unknown_term, help_msg);
1107 switch (format->value) {
1108 case PERF_PMU_FORMAT_VALUE_CONFIG:
1111 case PERF_PMU_FORMAT_VALUE_CONFIG1:
1112 vp = &attr->config1;
1114 case PERF_PMU_FORMAT_VALUE_CONFIG2:
1115 vp = &attr->config2;
1122 * Either directly use a numeric term, or try to translate string terms
1123 * using event parameters.
1125 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1126 if (term->no_value &&
1127 bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) {
1129 parse_events__handle_error(err, term->err_val,
1130 strdup("no value assigned for term"),
1136 val = term->val.num;
1137 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1138 if (strcmp(term->val.str, "?")) {
1140 pr_info("Invalid sysfs entry %s=%s\n",
1141 term->config, term->val.str);
1144 parse_events__handle_error(err, term->err_val,
1145 strdup("expected numeric value"),
1151 if (pmu_resolve_param_term(term, head_terms, &val))
1156 max_val = pmu_format_max_value(format->bits);
1157 if (val > max_val) {
1161 parse_events__handle_error(err, term->err_val,
1163 "value too big for format, maximum is %llu",
1164 (unsigned long long)max_val) < 0
1165 ? strdup("value too big for format")
1171 * Assume we don't care if !err, in which case the value will be
1172 * silently truncated.
1176 pmu_format_value(format->bits, val, vp, zero);
1180 int perf_pmu__config_terms(const char *pmu_name, struct list_head *formats,
1181 struct perf_event_attr *attr,
1182 struct list_head *head_terms,
1183 bool zero, struct parse_events_error *err)
1185 struct parse_events_term *term;
1187 list_for_each_entry(term, head_terms, list) {
1188 if (pmu_config_term(pmu_name, formats, attr, term, head_terms,
1197 * Configures event's 'attr' parameter based on the:
1198 * 1) users input - specified in terms parameter
1199 * 2) pmu format definitions - specified by pmu parameter
1201 int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
1202 struct list_head *head_terms,
1203 struct parse_events_error *err)
1205 bool zero = !!pmu->default_config;
1207 attr->type = pmu->type;
1208 return perf_pmu__config_terms(pmu->name, &pmu->format, attr,
1209 head_terms, zero, err);
1212 static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
1213 struct parse_events_term *term)
1215 struct perf_pmu_alias *alias;
1218 if (parse_events__is_hardcoded_term(term))
1221 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1222 if (term->val.num != 1)
1224 if (pmu_find_format(&pmu->format, term->config))
1226 name = term->config;
1227 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1228 if (strcasecmp(term->config, "event"))
1230 name = term->val.str;
1235 list_for_each_entry(alias, &pmu->aliases, list) {
1236 if (!strcasecmp(alias->name, name))
1243 static int check_info_data(struct perf_pmu_alias *alias,
1244 struct perf_pmu_info *info)
1247 * Only one term in event definition can
1248 * define unit, scale and snapshot, fail
1249 * if there's more than one.
1251 if ((info->unit && alias->unit[0]) ||
1252 (info->scale && alias->scale) ||
1253 (info->snapshot && alias->snapshot))
1257 info->unit = alias->unit;
1260 info->scale = alias->scale;
1262 if (alias->snapshot)
1263 info->snapshot = alias->snapshot;
1269 * Find alias in the terms list and replace it with the terms
1270 * defined for the alias
1272 int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
1273 struct perf_pmu_info *info)
1275 struct parse_events_term *term, *h;
1276 struct perf_pmu_alias *alias;
1279 info->per_pkg = false;
1282 * Mark unit and scale as not set
1283 * (different from default values, see below)
1287 info->snapshot = false;
1288 info->metric_expr = NULL;
1289 info->metric_name = NULL;
1291 list_for_each_entry_safe(term, h, head_terms, list) {
1292 alias = pmu_find_alias(pmu, term);
1295 ret = pmu_alias_terms(alias, &term->list);
1299 ret = check_info_data(alias, info);
1304 info->per_pkg = true;
1305 info->metric_expr = alias->metric_expr;
1306 info->metric_name = alias->metric_name;
1308 list_del_init(&term->list);
1309 parse_events_term__delete(term);
1313 * if no unit or scale foundin aliases, then
1314 * set defaults as for evsel
1315 * unit cannot left to NULL
1317 if (info->unit == NULL)
1320 if (info->scale == 0.0)
1326 int perf_pmu__new_format(struct list_head *list, char *name,
1327 int config, unsigned long *bits)
1329 struct perf_pmu_format *format;
1331 format = zalloc(sizeof(*format));
1335 format->name = strdup(name);
1336 format->value = config;
1337 memcpy(format->bits, bits, sizeof(format->bits));
1339 list_add_tail(&format->list, list);
1343 void perf_pmu__set_format(unsigned long *bits, long from, long to)
1350 memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
1351 for (b = from; b <= to; b++)
1355 static int sub_non_neg(int a, int b)
1362 static char *format_alias(char *buf, int len, struct perf_pmu *pmu,
1363 struct perf_pmu_alias *alias)
1365 struct parse_events_term *term;
1366 int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name);
1368 list_for_each_entry(term, &alias->terms, list) {
1369 if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
1370 used += snprintf(buf + used, sub_non_neg(len, used),
1371 ",%s=%s", term->config,
1375 if (sub_non_neg(len, used) > 0) {
1379 if (sub_non_neg(len, used) > 0) {
1383 buf[len - 1] = '\0';
1388 static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu,
1389 struct perf_pmu_alias *alias)
1391 snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name);
1405 static int cmp_sevent(const void *a, const void *b)
1407 const struct sevent *as = a;
1408 const struct sevent *bs = b;
1410 /* Put extra events last */
1411 if (!!as->desc != !!bs->desc)
1412 return !!as->desc - !!bs->desc;
1413 if (as->topic && bs->topic) {
1414 int n = strcmp(as->topic, bs->topic);
1419 return strcmp(as->name, bs->name);
1422 static void wordwrap(char *s, int start, int max, int corr)
1428 int wlen = strcspn(s, " \t");
1430 if (column + wlen >= max && column > start) {
1431 printf("\n%*s", start, "");
1432 column = start + corr;
1434 n = printf("%s%.*s", column > start ? " " : "", wlen, s);
1443 bool is_pmu_core(const char *name)
1445 return !strcmp(name, "cpu") || is_arm_pmu_core(name);
1448 void print_pmu_events(const char *event_glob, bool name_only, bool quiet_flag,
1449 bool long_desc, bool details_flag, bool deprecated)
1451 struct perf_pmu *pmu;
1452 struct perf_pmu_alias *alias;
1456 struct sevent *aliases;
1458 int columns = pager_get_columns();
1463 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1464 list_for_each_entry(alias, &pmu->aliases, list)
1466 if (pmu->selectable)
1469 aliases = zalloc(sizeof(struct sevent) * len);
1474 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1475 list_for_each_entry(alias, &pmu->aliases, list) {
1476 char *name = alias->desc ? alias->name :
1477 format_alias(buf, sizeof(buf), pmu, alias);
1478 bool is_cpu = !strcmp(pmu->name, "cpu");
1480 if (alias->deprecated && !deprecated)
1483 if (event_glob != NULL &&
1484 !(strglobmatch_nocase(name, event_glob) ||
1485 (!is_cpu && strglobmatch_nocase(alias->name,
1488 strglobmatch_nocase(alias->topic, event_glob))))
1491 if (is_cpu && !name_only && !alias->desc)
1492 name = format_alias_or(buf, sizeof(buf), pmu, alias);
1494 aliases[j].name = name;
1495 if (is_cpu && !name_only && !alias->desc)
1496 aliases[j].name = format_alias_or(buf,
1499 aliases[j].name = strdup(aliases[j].name);
1500 if (!aliases[j].name)
1503 aliases[j].desc = long_desc ? alias->long_desc :
1505 aliases[j].topic = alias->topic;
1506 aliases[j].str = alias->str;
1507 aliases[j].pmu = pmu->name;
1508 aliases[j].metric_expr = alias->metric_expr;
1509 aliases[j].metric_name = alias->metric_name;
1512 if (pmu->selectable &&
1513 (event_glob == NULL || strglobmatch(pmu->name, event_glob))) {
1515 if (asprintf(&s, "%s//", pmu->name) < 0)
1517 aliases[j].name = s;
1522 qsort(aliases, len, sizeof(struct sevent), cmp_sevent);
1523 for (j = 0; j < len; j++) {
1524 /* Skip duplicates */
1525 if (j > 0 && !strcmp(aliases[j].name, aliases[j - 1].name))
1528 printf("%s ", aliases[j].name);
1531 if (aliases[j].desc && !quiet_flag) {
1534 if (aliases[j].topic && (!topic ||
1535 strcmp(topic, aliases[j].topic))) {
1536 printf("%s%s:\n", topic ? "\n" : "",
1538 topic = aliases[j].topic;
1540 printf(" %-50s\n", aliases[j].name);
1541 printf("%*s", 8, "[");
1542 wordwrap(aliases[j].desc, 8, columns, 0);
1545 printf("%*s%s/%s/ ", 8, "", aliases[j].pmu, aliases[j].str);
1546 if (aliases[j].metric_name)
1547 printf(" MetricName: %s", aliases[j].metric_name);
1548 if (aliases[j].metric_expr)
1549 printf(" MetricExpr: %s", aliases[j].metric_expr);
1553 printf(" %-50s [Kernel PMU event]\n", aliases[j].name);
1556 if (printed && pager_in_use())
1559 for (j = 0; j < len; j++)
1560 zfree(&aliases[j].name);
1565 printf("FATAL: not enough memory to print PMU events\n");
1570 bool pmu_have_event(const char *pname, const char *name)
1572 struct perf_pmu *pmu;
1573 struct perf_pmu_alias *alias;
1576 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1577 if (strcmp(pname, pmu->name))
1579 list_for_each_entry(alias, &pmu->aliases, list)
1580 if (!strcmp(alias->name, name))
1586 static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
1588 char path[PATH_MAX];
1591 sysfs = sysfs__mountpoint();
1595 snprintf(path, PATH_MAX,
1596 "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name);
1597 if (!file_available(path))
1599 return fopen(path, "r");
1602 int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
1609 va_start(args, fmt);
1610 file = perf_pmu__open_file(pmu, name);
1612 ret = vfscanf(file, fmt, args);
1619 static int perf_pmu__new_caps(struct list_head *list, char *name, char *value)
1621 struct perf_pmu_caps *caps = zalloc(sizeof(*caps));
1626 caps->name = strdup(name);
1629 caps->value = strndup(value, strlen(value) - 1);
1632 list_add_tail(&caps->list, list);
1644 * Reading/parsing the given pmu capabilities, which should be located at:
1645 * /sys/bus/event_source/devices/<dev>/caps as sysfs group attributes.
1646 * Return the number of capabilities
1648 int perf_pmu__caps_parse(struct perf_pmu *pmu)
1651 char caps_path[PATH_MAX];
1652 const char *sysfs = sysfs__mountpoint();
1654 struct dirent *evt_ent;
1660 snprintf(caps_path, PATH_MAX,
1661 "%s" EVENT_SOURCE_DEVICE_PATH "%s/caps", sysfs, pmu->name);
1663 if (stat(caps_path, &st) < 0)
1664 return 0; /* no error if caps does not exist */
1666 caps_dir = opendir(caps_path);
1670 while ((evt_ent = readdir(caps_dir)) != NULL) {
1671 char path[PATH_MAX + NAME_MAX + 1];
1672 char *name = evt_ent->d_name;
1676 if (!strcmp(name, ".") || !strcmp(name, ".."))
1679 snprintf(path, sizeof(path), "%s/%s", caps_path, name);
1681 file = fopen(path, "r");
1685 if (!fgets(value, sizeof(value), file) ||
1686 (perf_pmu__new_caps(&pmu->caps, name, value) < 0)) {