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 CPUS_TEMPLATE_UNCORE "%s/bus/event_source/devices/%s/cpumask"
601 #define CPUS_TEMPLATE_CPU "%s/bus/event_source/devices/%s/cpus"
603 static struct perf_cpu_map *pmu_cpumask(const char *name)
606 struct perf_cpu_map *cpus;
607 const char *sysfs = sysfs__mountpoint();
608 const char *templates[] = {
609 CPUS_TEMPLATE_UNCORE,
613 const char **template;
618 for (template = templates; *template; template++) {
619 snprintf(path, PATH_MAX, *template, sysfs, name);
620 cpus = __pmu_cpumask(path);
628 static bool pmu_is_uncore(const char *name)
633 sysfs = sysfs__mountpoint();
634 snprintf(path, PATH_MAX, CPUS_TEMPLATE_UNCORE, sysfs, name);
635 return file_available(path);
639 * PMU CORE devices have different name other than cpu in sysfs on some
641 * Looking for possible sysfs files to identify the arm core device.
643 static int is_arm_pmu_core(const char *name)
646 const char *sysfs = sysfs__mountpoint();
651 /* Look for cpu sysfs (specific to arm) */
652 scnprintf(path, PATH_MAX, "%s/bus/event_source/devices/%s/cpus",
654 return file_available(path);
657 static char *perf_pmu__getcpuid(struct perf_pmu *pmu)
662 cpuid = getenv("PERF_CPUID");
664 cpuid = strdup(cpuid);
666 cpuid = get_cpuid_str(pmu);
671 pr_debug("Using CPUID %s\n", cpuid);
677 struct pmu_events_map *perf_pmu__find_map(struct perf_pmu *pmu)
679 struct pmu_events_map *map;
680 char *cpuid = perf_pmu__getcpuid(pmu);
683 /* on some platforms which uses cpus map, cpuid can be NULL for
684 * PMUs other than CORE PMUs.
691 map = &pmu_events_map[i++];
697 if (!strcmp_cpuid_str(map->cpuid, cpuid))
704 bool pmu_uncore_alias_match(const char *pmu_name, const char *name)
706 char *tmp = NULL, *tok, *str;
709 str = strdup(pmu_name);
714 * uncore alias may be from different PMU with common prefix
716 tok = strtok_r(str, ",", &tmp);
717 if (strncmp(pmu_name, tok, strlen(tok))) {
723 * Match more complex aliases where the alias name is a comma-delimited
724 * list of tokens, orderly contained in the matching PMU name.
726 * Example: For alias "socket,pmuname" and PMU "socketX_pmunameY", we
727 * match "socket" in "socketX_pmunameY" and then "pmuname" in
730 for (; tok; name += strlen(tok), tok = strtok_r(NULL, ",", &tmp)) {
731 name = strstr(name, tok);
745 * From the pmu_events_map, find the table of PMU events that corresponds
746 * to the current running CPU. Then, add all PMU events from that table
749 void pmu_add_cpu_aliases_map(struct list_head *head, struct perf_pmu *pmu,
750 struct pmu_events_map *map)
753 const char *name = pmu->name;
755 * Found a matching PMU events table. Create aliases
759 const char *cpu_name = is_arm_pmu_core(name) ? name : "cpu";
760 struct pmu_event *pe = &map->table[i++];
761 const char *pname = pe->pmu ? pe->pmu : cpu_name;
764 if (pe->metric_group || pe->metric_name)
769 if (pmu_is_uncore(name) &&
770 pmu_uncore_alias_match(pname, name))
773 if (strcmp(pname, name))
777 /* need type casts to override 'const' */
778 __perf_pmu__new_alias(head, NULL, (char *)pe->name,
779 (char *)pe->desc, (char *)pe->event,
780 (char *)pe->long_desc, (char *)pe->topic,
781 (char *)pe->unit, (char *)pe->perpkg,
782 (char *)pe->metric_expr,
783 (char *)pe->metric_name,
784 (char *)pe->deprecated);
788 static void pmu_add_cpu_aliases(struct list_head *head, struct perf_pmu *pmu)
790 struct pmu_events_map *map;
792 map = perf_pmu__find_map(pmu);
796 pmu_add_cpu_aliases_map(head, pmu, map);
799 struct perf_event_attr * __weak
800 perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
805 static int pmu_max_precise(const char *name)
808 int max_precise = -1;
810 scnprintf(path, PATH_MAX,
811 "bus/event_source/devices/%s/caps/max_precise",
814 sysfs__read_int(path, &max_precise);
818 static struct perf_pmu *pmu_lookup(const char *name)
820 struct perf_pmu *pmu;
826 * The pmu data we store & need consists of the pmu
827 * type value and format definitions. Load both right
830 if (pmu_format(name, &format))
834 * Check the type first to avoid unnecessary work.
836 if (pmu_type(name, &type))
839 if (pmu_aliases(name, &aliases))
842 pmu = zalloc(sizeof(*pmu));
846 pmu->cpus = pmu_cpumask(name);
847 pmu->name = strdup(name);
849 pmu->is_uncore = pmu_is_uncore(name);
850 pmu->max_precise = pmu_max_precise(name);
851 pmu_add_cpu_aliases(&aliases, pmu);
853 INIT_LIST_HEAD(&pmu->format);
854 INIT_LIST_HEAD(&pmu->aliases);
855 INIT_LIST_HEAD(&pmu->caps);
856 list_splice(&format, &pmu->format);
857 list_splice(&aliases, &pmu->aliases);
858 list_add_tail(&pmu->list, &pmus);
860 pmu->default_config = perf_pmu__get_default_config(pmu);
865 static struct perf_pmu *pmu_find(const char *name)
867 struct perf_pmu *pmu;
869 list_for_each_entry(pmu, &pmus, list)
870 if (!strcmp(pmu->name, name))
876 struct perf_pmu *perf_pmu__find_by_type(unsigned int type)
878 struct perf_pmu *pmu;
880 list_for_each_entry(pmu, &pmus, list)
881 if (pmu->type == type)
887 struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
890 * pmu iterator: If pmu is NULL, we start at the begin,
891 * otherwise return the next pmu. Returns NULL on end.
895 pmu = list_prepare_entry(pmu, &pmus, list);
897 list_for_each_entry_continue(pmu, &pmus, list)
902 struct perf_pmu *evsel__find_pmu(struct evsel *evsel)
904 struct perf_pmu *pmu = NULL;
906 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
907 if (pmu->type == evsel->core.attr.type)
914 bool evsel__is_aux_event(struct evsel *evsel)
916 struct perf_pmu *pmu = evsel__find_pmu(evsel);
918 return pmu && pmu->auxtrace;
921 struct perf_pmu *perf_pmu__find(const char *name)
923 struct perf_pmu *pmu;
926 * Once PMU is loaded it stays in the list,
927 * so we keep us from multiple reading/parsing
928 * the pmu format definitions.
930 pmu = pmu_find(name);
934 return pmu_lookup(name);
937 static struct perf_pmu_format *
938 pmu_find_format(struct list_head *formats, const char *name)
940 struct perf_pmu_format *format;
942 list_for_each_entry(format, formats, list)
943 if (!strcmp(format->name, name))
949 __u64 perf_pmu__format_bits(struct list_head *formats, const char *name)
951 struct perf_pmu_format *format = pmu_find_format(formats, name);
958 for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
959 bits |= 1ULL << fbit;
964 int perf_pmu__format_type(struct list_head *formats, const char *name)
966 struct perf_pmu_format *format = pmu_find_format(formats, name);
971 return format->value;
975 * Sets value based on the format definition (format parameter)
976 * and unformated value (value parameter).
978 static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
981 unsigned long fbit, vbit;
983 for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
985 if (!test_bit(fbit, format))
988 if (value & (1llu << vbit++))
989 *v |= (1llu << fbit);
991 *v &= ~(1llu << fbit);
995 static __u64 pmu_format_max_value(const unsigned long *format)
999 w = bitmap_weight(format, PERF_PMU_FORMAT_BITS);
1003 return (1ULL << w) - 1;
1008 * Term is a string term, and might be a param-term. Try to look up it's value
1009 * in the remaining terms.
1010 * - We have a term like "base-or-format-term=param-term",
1011 * - We need to find the value supplied for "param-term" (with param-term named
1012 * in a config string) later on in the term list.
1014 static int pmu_resolve_param_term(struct parse_events_term *term,
1015 struct list_head *head_terms,
1018 struct parse_events_term *t;
1020 list_for_each_entry(t, head_terms, list) {
1021 if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM &&
1022 t->config && !strcmp(t->config, term->config)) {
1024 *value = t->val.num;
1030 printf("Required parameter '%s' not specified\n", term->config);
1035 static char *pmu_formats_string(struct list_head *formats)
1037 struct perf_pmu_format *format;
1039 struct strbuf buf = STRBUF_INIT;
1045 /* sysfs exported terms */
1046 list_for_each_entry(format, formats, list)
1047 if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
1050 str = strbuf_detach(&buf, NULL);
1052 strbuf_release(&buf);
1058 * Setup one of config[12] attr members based on the
1059 * user input data - term parameter.
1061 static int pmu_config_term(const char *pmu_name,
1062 struct list_head *formats,
1063 struct perf_event_attr *attr,
1064 struct parse_events_term *term,
1065 struct list_head *head_terms,
1066 bool zero, struct parse_events_error *err)
1068 struct perf_pmu_format *format;
1073 * If this is a parameter we've already used for parameterized-eval,
1074 * skip it in normal eval.
1080 * Hardcoded terms should be already in, so nothing
1081 * to be done for them.
1083 if (parse_events__is_hardcoded_term(term))
1086 format = pmu_find_format(formats, term->config);
1088 char *pmu_term = pmu_formats_string(formats);
1092 if (asprintf(&unknown_term,
1093 "unknown term '%s' for pmu '%s'",
1094 term->config, pmu_name) < 0)
1095 unknown_term = NULL;
1096 help_msg = parse_events_formats_error_string(pmu_term);
1098 parse_events__handle_error(err, term->err_term,
1102 pr_debug("%s (%s)\n", unknown_term, help_msg);
1109 switch (format->value) {
1110 case PERF_PMU_FORMAT_VALUE_CONFIG:
1113 case PERF_PMU_FORMAT_VALUE_CONFIG1:
1114 vp = &attr->config1;
1116 case PERF_PMU_FORMAT_VALUE_CONFIG2:
1117 vp = &attr->config2;
1124 * Either directly use a numeric term, or try to translate string terms
1125 * using event parameters.
1127 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1128 if (term->no_value &&
1129 bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) {
1131 parse_events__handle_error(err, term->err_val,
1132 strdup("no value assigned for term"),
1138 val = term->val.num;
1139 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1140 if (strcmp(term->val.str, "?")) {
1142 pr_info("Invalid sysfs entry %s=%s\n",
1143 term->config, term->val.str);
1146 parse_events__handle_error(err, term->err_val,
1147 strdup("expected numeric value"),
1153 if (pmu_resolve_param_term(term, head_terms, &val))
1158 max_val = pmu_format_max_value(format->bits);
1159 if (val > max_val) {
1163 parse_events__handle_error(err, term->err_val,
1165 "value too big for format, maximum is %llu",
1166 (unsigned long long)max_val) < 0
1167 ? strdup("value too big for format")
1173 * Assume we don't care if !err, in which case the value will be
1174 * silently truncated.
1178 pmu_format_value(format->bits, val, vp, zero);
1182 int perf_pmu__config_terms(const char *pmu_name, struct list_head *formats,
1183 struct perf_event_attr *attr,
1184 struct list_head *head_terms,
1185 bool zero, struct parse_events_error *err)
1187 struct parse_events_term *term;
1189 list_for_each_entry(term, head_terms, list) {
1190 if (pmu_config_term(pmu_name, formats, attr, term, head_terms,
1199 * Configures event's 'attr' parameter based on the:
1200 * 1) users input - specified in terms parameter
1201 * 2) pmu format definitions - specified by pmu parameter
1203 int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
1204 struct list_head *head_terms,
1205 struct parse_events_error *err)
1207 bool zero = !!pmu->default_config;
1209 attr->type = pmu->type;
1210 return perf_pmu__config_terms(pmu->name, &pmu->format, attr,
1211 head_terms, zero, err);
1214 static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
1215 struct parse_events_term *term)
1217 struct perf_pmu_alias *alias;
1220 if (parse_events__is_hardcoded_term(term))
1223 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1224 if (term->val.num != 1)
1226 if (pmu_find_format(&pmu->format, term->config))
1228 name = term->config;
1229 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1230 if (strcasecmp(term->config, "event"))
1232 name = term->val.str;
1237 list_for_each_entry(alias, &pmu->aliases, list) {
1238 if (!strcasecmp(alias->name, name))
1245 static int check_info_data(struct perf_pmu_alias *alias,
1246 struct perf_pmu_info *info)
1249 * Only one term in event definition can
1250 * define unit, scale and snapshot, fail
1251 * if there's more than one.
1253 if ((info->unit && alias->unit[0]) ||
1254 (info->scale && alias->scale) ||
1255 (info->snapshot && alias->snapshot))
1259 info->unit = alias->unit;
1262 info->scale = alias->scale;
1264 if (alias->snapshot)
1265 info->snapshot = alias->snapshot;
1271 * Find alias in the terms list and replace it with the terms
1272 * defined for the alias
1274 int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
1275 struct perf_pmu_info *info)
1277 struct parse_events_term *term, *h;
1278 struct perf_pmu_alias *alias;
1281 info->per_pkg = false;
1284 * Mark unit and scale as not set
1285 * (different from default values, see below)
1289 info->snapshot = false;
1290 info->metric_expr = NULL;
1291 info->metric_name = NULL;
1293 list_for_each_entry_safe(term, h, head_terms, list) {
1294 alias = pmu_find_alias(pmu, term);
1297 ret = pmu_alias_terms(alias, &term->list);
1301 ret = check_info_data(alias, info);
1306 info->per_pkg = true;
1307 info->metric_expr = alias->metric_expr;
1308 info->metric_name = alias->metric_name;
1310 list_del_init(&term->list);
1311 parse_events_term__delete(term);
1315 * if no unit or scale foundin aliases, then
1316 * set defaults as for evsel
1317 * unit cannot left to NULL
1319 if (info->unit == NULL)
1322 if (info->scale == 0.0)
1328 int perf_pmu__new_format(struct list_head *list, char *name,
1329 int config, unsigned long *bits)
1331 struct perf_pmu_format *format;
1333 format = zalloc(sizeof(*format));
1337 format->name = strdup(name);
1338 format->value = config;
1339 memcpy(format->bits, bits, sizeof(format->bits));
1341 list_add_tail(&format->list, list);
1345 void perf_pmu__set_format(unsigned long *bits, long from, long to)
1352 memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
1353 for (b = from; b <= to; b++)
1357 void perf_pmu__del_formats(struct list_head *formats)
1359 struct perf_pmu_format *fmt, *tmp;
1361 list_for_each_entry_safe(fmt, tmp, formats, list) {
1362 list_del(&fmt->list);
1368 static int sub_non_neg(int a, int b)
1375 static char *format_alias(char *buf, int len, struct perf_pmu *pmu,
1376 struct perf_pmu_alias *alias)
1378 struct parse_events_term *term;
1379 int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name);
1381 list_for_each_entry(term, &alias->terms, list) {
1382 if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
1383 used += snprintf(buf + used, sub_non_neg(len, used),
1384 ",%s=%s", term->config,
1388 if (sub_non_neg(len, used) > 0) {
1392 if (sub_non_neg(len, used) > 0) {
1396 buf[len - 1] = '\0';
1401 static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu,
1402 struct perf_pmu_alias *alias)
1404 snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name);
1419 static int cmp_sevent(const void *a, const void *b)
1421 const struct sevent *as = a;
1422 const struct sevent *bs = b;
1424 /* Put extra events last */
1425 if (!!as->desc != !!bs->desc)
1426 return !!as->desc - !!bs->desc;
1427 if (as->topic && bs->topic) {
1428 int n = strcmp(as->topic, bs->topic);
1434 /* Order CPU core events to be first */
1435 if (as->is_cpu != bs->is_cpu)
1436 return bs->is_cpu - as->is_cpu;
1438 return strcmp(as->name, bs->name);
1441 static void wordwrap(char *s, int start, int max, int corr)
1447 int wlen = strcspn(s, " \t");
1449 if (column + wlen >= max && column > start) {
1450 printf("\n%*s", start, "");
1451 column = start + corr;
1453 n = printf("%s%.*s", column > start ? " " : "", wlen, s);
1462 bool is_pmu_core(const char *name)
1464 return !strcmp(name, "cpu") || is_arm_pmu_core(name);
1467 void print_pmu_events(const char *event_glob, bool name_only, bool quiet_flag,
1468 bool long_desc, bool details_flag, bool deprecated)
1470 struct perf_pmu *pmu;
1471 struct perf_pmu_alias *alias;
1475 struct sevent *aliases;
1477 int columns = pager_get_columns();
1482 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1483 list_for_each_entry(alias, &pmu->aliases, list)
1485 if (pmu->selectable)
1488 aliases = zalloc(sizeof(struct sevent) * len);
1493 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1494 list_for_each_entry(alias, &pmu->aliases, list) {
1495 char *name = alias->desc ? alias->name :
1496 format_alias(buf, sizeof(buf), pmu, alias);
1497 bool is_cpu = is_pmu_core(pmu->name);
1499 if (alias->deprecated && !deprecated)
1502 if (event_glob != NULL &&
1503 !(strglobmatch_nocase(name, event_glob) ||
1504 (!is_cpu && strglobmatch_nocase(alias->name,
1507 strglobmatch_nocase(alias->topic, event_glob))))
1510 if (is_cpu && !name_only && !alias->desc)
1511 name = format_alias_or(buf, sizeof(buf), pmu, alias);
1513 aliases[j].name = name;
1514 if (is_cpu && !name_only && !alias->desc)
1515 aliases[j].name = format_alias_or(buf,
1518 aliases[j].name = strdup(aliases[j].name);
1519 if (!aliases[j].name)
1522 aliases[j].desc = long_desc ? alias->long_desc :
1524 aliases[j].topic = alias->topic;
1525 aliases[j].str = alias->str;
1526 aliases[j].pmu = pmu->name;
1527 aliases[j].metric_expr = alias->metric_expr;
1528 aliases[j].metric_name = alias->metric_name;
1529 aliases[j].is_cpu = is_cpu;
1532 if (pmu->selectable &&
1533 (event_glob == NULL || strglobmatch(pmu->name, event_glob))) {
1535 if (asprintf(&s, "%s//", pmu->name) < 0)
1537 aliases[j].name = s;
1542 qsort(aliases, len, sizeof(struct sevent), cmp_sevent);
1543 for (j = 0; j < len; j++) {
1544 /* Skip duplicates */
1545 if (j > 0 && !strcmp(aliases[j].name, aliases[j - 1].name))
1548 printf("%s ", aliases[j].name);
1551 if (aliases[j].desc && !quiet_flag) {
1554 if (aliases[j].topic && (!topic ||
1555 strcmp(topic, aliases[j].topic))) {
1556 printf("%s%s:\n", topic ? "\n" : "",
1558 topic = aliases[j].topic;
1560 printf(" %-50s\n", aliases[j].name);
1561 printf("%*s", 8, "[");
1562 wordwrap(aliases[j].desc, 8, columns, 0);
1565 printf("%*s%s/%s/ ", 8, "", aliases[j].pmu, aliases[j].str);
1566 if (aliases[j].metric_name)
1567 printf(" MetricName: %s", aliases[j].metric_name);
1568 if (aliases[j].metric_expr)
1569 printf(" MetricExpr: %s", aliases[j].metric_expr);
1573 printf(" %-50s [Kernel PMU event]\n", aliases[j].name);
1576 if (printed && pager_in_use())
1579 for (j = 0; j < len; j++)
1580 zfree(&aliases[j].name);
1585 printf("FATAL: not enough memory to print PMU events\n");
1590 bool pmu_have_event(const char *pname, const char *name)
1592 struct perf_pmu *pmu;
1593 struct perf_pmu_alias *alias;
1596 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1597 if (strcmp(pname, pmu->name))
1599 list_for_each_entry(alias, &pmu->aliases, list)
1600 if (!strcmp(alias->name, name))
1606 static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
1608 char path[PATH_MAX];
1611 sysfs = sysfs__mountpoint();
1615 snprintf(path, PATH_MAX,
1616 "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name);
1617 if (!file_available(path))
1619 return fopen(path, "r");
1622 int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
1629 va_start(args, fmt);
1630 file = perf_pmu__open_file(pmu, name);
1632 ret = vfscanf(file, fmt, args);
1639 static int perf_pmu__new_caps(struct list_head *list, char *name, char *value)
1641 struct perf_pmu_caps *caps = zalloc(sizeof(*caps));
1646 caps->name = strdup(name);
1649 caps->value = strndup(value, strlen(value) - 1);
1652 list_add_tail(&caps->list, list);
1664 * Reading/parsing the given pmu capabilities, which should be located at:
1665 * /sys/bus/event_source/devices/<dev>/caps as sysfs group attributes.
1666 * Return the number of capabilities
1668 int perf_pmu__caps_parse(struct perf_pmu *pmu)
1671 char caps_path[PATH_MAX];
1672 const char *sysfs = sysfs__mountpoint();
1674 struct dirent *evt_ent;
1680 snprintf(caps_path, PATH_MAX,
1681 "%s" EVENT_SOURCE_DEVICE_PATH "%s/caps", sysfs, pmu->name);
1683 if (stat(caps_path, &st) < 0)
1684 return 0; /* no error if caps does not exist */
1686 caps_dir = opendir(caps_path);
1690 while ((evt_ent = readdir(caps_dir)) != NULL) {
1691 char path[PATH_MAX + NAME_MAX + 1];
1692 char *name = evt_ent->d_name;
1696 if (!strcmp(name, ".") || !strcmp(name, ".."))
1699 snprintf(path, sizeof(path), "%s/%s", caps_path, name);
1701 file = fopen(path, "r");
1705 if (!fgets(value, sizeof(value), file) ||
1706 (perf_pmu__new_caps(&pmu->caps, name, value) < 0)) {