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>
22 #include "parse-events.h"
28 struct perf_pmu_format {
31 DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
32 struct list_head list;
35 int perf_pmu_parse(struct list_head *list, char *name);
36 extern FILE *perf_pmu_in;
38 static LIST_HEAD(pmus);
41 * Parse & process all the sysfs attributes located under
42 * the directory specified in 'dir' parameter.
44 int perf_pmu__format_parse(char *dir, struct list_head *head)
46 struct dirent *evt_ent;
50 format_dir = opendir(dir);
54 while (!ret && (evt_ent = readdir(format_dir))) {
56 char *name = evt_ent->d_name;
59 if (!strcmp(name, ".") || !strcmp(name, ".."))
62 snprintf(path, PATH_MAX, "%s/%s", dir, name);
65 file = fopen(path, "r");
70 ret = perf_pmu_parse(head, name);
79 * Reading/parsing the default pmu format definition, which should be
81 * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
83 static int pmu_format(const char *name, struct list_head *format)
86 const char *sysfs = sysfs__mountpoint();
91 snprintf(path, PATH_MAX,
92 "%s" EVENT_SOURCE_DEVICE_PATH "%s/format", sysfs, name);
94 if (!file_available(path))
97 if (perf_pmu__format_parse(path, format))
103 int perf_pmu__convert_scale(const char *scale, char **end, double *sval)
109 * save current locale
111 lc = setlocale(LC_NUMERIC, NULL);
114 * The lc string may be allocated in static storage,
115 * so get a dynamic copy to make it survive setlocale
125 * force to C locale to ensure kernel
126 * scale string is converted correctly.
127 * kernel uses default C locale.
129 setlocale(LC_NUMERIC, "C");
131 *sval = strtod(scale, end);
135 setlocale(LC_NUMERIC, lc);
140 static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *name)
148 scnprintf(path, PATH_MAX, "%s/%s.scale", dir, name);
150 fd = open(path, O_RDONLY);
154 if (fstat(fd, &st) < 0)
157 sret = read(fd, scale, sizeof(scale)-1);
161 if (scale[sret - 1] == '\n')
162 scale[sret - 1] = '\0';
166 ret = perf_pmu__convert_scale(scale, NULL, &alias->scale);
172 static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *name)
178 scnprintf(path, PATH_MAX, "%s/%s.unit", dir, name);
180 fd = open(path, O_RDONLY);
184 sret = read(fd, alias->unit, UNIT_MAX_LEN);
190 if (alias->unit[sret - 1] == '\n')
191 alias->unit[sret - 1] = '\0';
193 alias->unit[sret] = '\0';
198 alias->unit[0] = '\0';
203 perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, char *dir, char *name)
208 scnprintf(path, PATH_MAX, "%s/%s.per-pkg", dir, name);
210 fd = open(path, O_RDONLY);
216 alias->per_pkg = true;
220 static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias,
221 char *dir, char *name)
226 scnprintf(path, PATH_MAX, "%s/%s.snapshot", dir, name);
228 fd = open(path, O_RDONLY);
232 alias->snapshot = true;
237 static void perf_pmu_assign_str(char *name, const char *field, char **old_str,
243 if (*new_str) { /* Have new string, check with old */
244 if (strcasecmp(*old_str, *new_str))
245 pr_debug("alias %s differs in field '%s'\n",
248 } else /* Nothing new --> keep old string */
255 static void perf_pmu_update_alias(struct perf_pmu_alias *old,
256 struct perf_pmu_alias *newalias)
258 perf_pmu_assign_str(old->name, "desc", &old->desc, &newalias->desc);
259 perf_pmu_assign_str(old->name, "long_desc", &old->long_desc,
260 &newalias->long_desc);
261 perf_pmu_assign_str(old->name, "topic", &old->topic, &newalias->topic);
262 perf_pmu_assign_str(old->name, "metric_expr", &old->metric_expr,
263 &newalias->metric_expr);
264 perf_pmu_assign_str(old->name, "metric_name", &old->metric_name,
265 &newalias->metric_name);
266 perf_pmu_assign_str(old->name, "value", &old->str, &newalias->str);
267 old->scale = newalias->scale;
268 old->per_pkg = newalias->per_pkg;
269 old->snapshot = newalias->snapshot;
270 memcpy(old->unit, newalias->unit, sizeof(old->unit));
273 /* Delete an alias entry. */
274 static void perf_pmu_free_alias(struct perf_pmu_alias *newalias)
276 zfree(&newalias->name);
277 zfree(&newalias->desc);
278 zfree(&newalias->long_desc);
279 zfree(&newalias->topic);
280 zfree(&newalias->str);
281 zfree(&newalias->metric_expr);
282 zfree(&newalias->metric_name);
283 parse_events_terms__purge(&newalias->terms);
287 /* Merge an alias, search in alias list. If this name is already
288 * present merge both of them to combine all information.
290 static bool perf_pmu_merge_alias(struct perf_pmu_alias *newalias,
291 struct list_head *alist)
293 struct perf_pmu_alias *a;
295 list_for_each_entry(a, alist, list) {
296 if (!strcasecmp(newalias->name, a->name)) {
297 perf_pmu_update_alias(a, newalias);
298 perf_pmu_free_alias(newalias);
305 static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name,
306 char *desc, char *val,
307 char *long_desc, char *topic,
308 char *unit, char *perpkg,
313 struct parse_events_term *term;
314 struct perf_pmu_alias *alias;
319 alias = malloc(sizeof(*alias));
323 INIT_LIST_HEAD(&alias->terms);
325 alias->unit[0] = '\0';
326 alias->per_pkg = false;
327 alias->snapshot = false;
328 alias->deprecated = false;
330 ret = parse_events_terms(&alias->terms, val);
332 pr_err("Cannot parse alias %s: %d\n", val, ret);
337 /* Scan event and remove leading zeroes, spaces, newlines, some
338 * platforms have terms specified as
339 * event=0x0091 (read from files ../<PMU>/events/<FILE>
340 * and terms specified as event=0x91 (read from JSON files).
342 * Rebuild string to make alias->str member comparable.
344 memset(newval, 0, sizeof(newval));
346 list_for_each_entry(term, &alias->terms, list) {
348 ret += scnprintf(newval + ret, sizeof(newval) - ret,
350 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM)
351 ret += scnprintf(newval + ret, sizeof(newval) - ret,
352 "%s=%#x", term->config, term->val.num);
353 else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
354 ret += scnprintf(newval + ret, sizeof(newval) - ret,
355 "%s=%s", term->config, term->val.str);
358 alias->name = strdup(name);
361 * load unit name and scale if available
363 perf_pmu__parse_unit(alias, dir, name);
364 perf_pmu__parse_scale(alias, dir, name);
365 perf_pmu__parse_per_pkg(alias, dir, name);
366 perf_pmu__parse_snapshot(alias, dir, name);
369 alias->metric_expr = metric_expr ? strdup(metric_expr) : NULL;
370 alias->metric_name = metric_name ? strdup(metric_name): NULL;
371 alias->desc = desc ? strdup(desc) : NULL;
372 alias->long_desc = long_desc ? strdup(long_desc) :
373 desc ? strdup(desc) : NULL;
374 alias->topic = topic ? strdup(topic) : NULL;
376 if (perf_pmu__convert_scale(unit, &unit, &alias->scale) < 0)
378 snprintf(alias->unit, sizeof(alias->unit), "%s", unit);
380 alias->per_pkg = perpkg && sscanf(perpkg, "%d", &num) == 1 && num == 1;
381 alias->str = strdup(newval);
384 alias->deprecated = true;
386 if (!perf_pmu_merge_alias(alias, list))
387 list_add_tail(&alias->list, list);
392 static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file)
397 ret = fread(buf, 1, sizeof(buf), file);
403 /* Remove trailing newline from sysfs file */
406 return __perf_pmu__new_alias(list, dir, name, NULL, buf, NULL, NULL, NULL,
407 NULL, NULL, NULL, NULL);
410 static inline bool pmu_alias_info_file(char *name)
415 if (len > 5 && !strcmp(name + len - 5, ".unit"))
417 if (len > 6 && !strcmp(name + len - 6, ".scale"))
419 if (len > 8 && !strcmp(name + len - 8, ".per-pkg"))
421 if (len > 9 && !strcmp(name + len - 9, ".snapshot"))
428 * Process all the sysfs attributes located under the directory
429 * specified in 'dir' parameter.
431 static int pmu_aliases_parse(char *dir, struct list_head *head)
433 struct dirent *evt_ent;
436 event_dir = opendir(dir);
440 while ((evt_ent = readdir(event_dir))) {
442 char *name = evt_ent->d_name;
445 if (!strcmp(name, ".") || !strcmp(name, ".."))
449 * skip info files parsed in perf_pmu__new_alias()
451 if (pmu_alias_info_file(name))
454 scnprintf(path, PATH_MAX, "%s/%s", dir, name);
456 file = fopen(path, "r");
458 pr_debug("Cannot open %s\n", path);
462 if (perf_pmu__new_alias(head, dir, name, file) < 0)
463 pr_debug("Cannot set up %s\n", name);
472 * Reading the pmu event aliases definition, which should be located at:
473 * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
475 static int pmu_aliases(const char *name, struct list_head *head)
478 const char *sysfs = sysfs__mountpoint();
483 snprintf(path, PATH_MAX,
484 "%s/bus/event_source/devices/%s/events", sysfs, name);
486 if (!file_available(path))
489 if (pmu_aliases_parse(path, head))
495 static int pmu_alias_terms(struct perf_pmu_alias *alias,
496 struct list_head *terms)
498 struct parse_events_term *term, *cloned;
502 list_for_each_entry(term, &alias->terms, list) {
503 ret = parse_events_term__clone(&cloned, term);
505 parse_events_terms__purge(&list);
509 * Weak terms don't override command line options,
510 * which we don't want for implicit terms in aliases.
513 list_add_tail(&cloned->list, &list);
515 list_splice(&list, terms);
520 * Reading/parsing the default pmu type value, which should be
522 * /sys/bus/event_source/devices/<dev>/type as sysfs attribute.
524 static int pmu_type(const char *name, __u32 *type)
529 const char *sysfs = sysfs__mountpoint();
534 snprintf(path, PATH_MAX,
535 "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name);
537 if (access(path, R_OK) < 0)
540 file = fopen(path, "r");
544 if (1 != fscanf(file, "%u", type))
551 /* Add all pmus in sysfs to pmu list: */
552 static void pmu_read_sysfs(void)
557 const char *sysfs = sysfs__mountpoint();
562 snprintf(path, PATH_MAX,
563 "%s" EVENT_SOURCE_DEVICE_PATH, sysfs);
569 while ((dent = readdir(dir))) {
570 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
572 /* add to static LIST_HEAD(pmus): */
573 perf_pmu__find(dent->d_name);
579 static struct perf_cpu_map *__pmu_cpumask(const char *path)
582 struct perf_cpu_map *cpus;
584 file = fopen(path, "r");
588 cpus = perf_cpu_map__read(file);
594 * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64)
595 * may have a "cpus" file.
597 #define CPUS_TEMPLATE_UNCORE "%s/bus/event_source/devices/%s/cpumask"
598 #define CPUS_TEMPLATE_CPU "%s/bus/event_source/devices/%s/cpus"
600 static struct perf_cpu_map *pmu_cpumask(const char *name)
603 struct perf_cpu_map *cpus;
604 const char *sysfs = sysfs__mountpoint();
605 const char *templates[] = {
606 CPUS_TEMPLATE_UNCORE,
610 const char **template;
615 for (template = templates; *template; template++) {
616 snprintf(path, PATH_MAX, *template, sysfs, name);
617 cpus = __pmu_cpumask(path);
625 static bool pmu_is_uncore(const char *name)
630 sysfs = sysfs__mountpoint();
631 snprintf(path, PATH_MAX, CPUS_TEMPLATE_UNCORE, sysfs, name);
632 return file_available(path);
636 * PMU CORE devices have different name other than cpu in sysfs on some
638 * Looking for possible sysfs files to identify the arm core device.
640 static int is_arm_pmu_core(const char *name)
643 const char *sysfs = sysfs__mountpoint();
648 /* Look for cpu sysfs (specific to arm) */
649 scnprintf(path, PATH_MAX, "%s/bus/event_source/devices/%s/cpus",
651 return file_available(path);
654 static char *perf_pmu__getcpuid(struct perf_pmu *pmu)
659 cpuid = getenv("PERF_CPUID");
661 cpuid = strdup(cpuid);
663 cpuid = get_cpuid_str(pmu);
668 pr_debug("Using CPUID %s\n", cpuid);
674 struct pmu_events_map *perf_pmu__find_map(struct perf_pmu *pmu)
676 struct pmu_events_map *map;
677 char *cpuid = perf_pmu__getcpuid(pmu);
680 /* on some platforms which uses cpus map, cpuid can be NULL for
681 * PMUs other than CORE PMUs.
688 map = &pmu_events_map[i++];
694 if (!strcmp_cpuid_str(map->cpuid, cpuid))
701 bool pmu_uncore_alias_match(const char *pmu_name, const char *name)
703 char *tmp = NULL, *tok, *str;
706 str = strdup(pmu_name);
711 * uncore alias may be from different PMU with common prefix
713 tok = strtok_r(str, ",", &tmp);
714 if (strncmp(pmu_name, tok, strlen(tok))) {
720 * Match more complex aliases where the alias name is a comma-delimited
721 * list of tokens, orderly contained in the matching PMU name.
723 * Example: For alias "socket,pmuname" and PMU "socketX_pmunameY", we
724 * match "socket" in "socketX_pmunameY" and then "pmuname" in
727 for (; tok; name += strlen(tok), tok = strtok_r(NULL, ",", &tmp)) {
728 name = strstr(name, tok);
742 * From the pmu_events_map, find the table of PMU events that corresponds
743 * to the current running CPU. Then, add all PMU events from that table
746 void pmu_add_cpu_aliases_map(struct list_head *head, struct perf_pmu *pmu,
747 struct pmu_events_map *map)
750 const char *name = pmu->name;
752 * Found a matching PMU events table. Create aliases
756 const char *cpu_name = is_arm_pmu_core(name) ? name : "cpu";
757 struct pmu_event *pe = &map->table[i++];
758 const char *pname = pe->pmu ? pe->pmu : cpu_name;
761 if (pe->metric_group || pe->metric_name)
766 if (pmu_is_uncore(name) &&
767 pmu_uncore_alias_match(pname, name))
770 if (strcmp(pname, name))
774 /* need type casts to override 'const' */
775 __perf_pmu__new_alias(head, NULL, (char *)pe->name,
776 (char *)pe->desc, (char *)pe->event,
777 (char *)pe->long_desc, (char *)pe->topic,
778 (char *)pe->unit, (char *)pe->perpkg,
779 (char *)pe->metric_expr,
780 (char *)pe->metric_name,
781 (char *)pe->deprecated);
785 static void pmu_add_cpu_aliases(struct list_head *head, struct perf_pmu *pmu)
787 struct pmu_events_map *map;
789 map = perf_pmu__find_map(pmu);
793 pmu_add_cpu_aliases_map(head, pmu, map);
796 struct perf_event_attr * __weak
797 perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
802 static int pmu_max_precise(const char *name)
805 int max_precise = -1;
807 scnprintf(path, PATH_MAX,
808 "bus/event_source/devices/%s/caps/max_precise",
811 sysfs__read_int(path, &max_precise);
815 static struct perf_pmu *pmu_lookup(const char *name)
817 struct perf_pmu *pmu;
823 * The pmu data we store & need consists of the pmu
824 * type value and format definitions. Load both right
827 if (pmu_format(name, &format))
831 * Check the type first to avoid unnecessary work.
833 if (pmu_type(name, &type))
836 if (pmu_aliases(name, &aliases))
839 pmu = zalloc(sizeof(*pmu));
843 pmu->cpus = pmu_cpumask(name);
844 pmu->name = strdup(name);
846 pmu->is_uncore = pmu_is_uncore(name);
847 pmu->max_precise = pmu_max_precise(name);
848 pmu_add_cpu_aliases(&aliases, pmu);
850 INIT_LIST_HEAD(&pmu->format);
851 INIT_LIST_HEAD(&pmu->aliases);
852 list_splice(&format, &pmu->format);
853 list_splice(&aliases, &pmu->aliases);
854 list_add_tail(&pmu->list, &pmus);
856 pmu->default_config = perf_pmu__get_default_config(pmu);
861 static struct perf_pmu *pmu_find(const char *name)
863 struct perf_pmu *pmu;
865 list_for_each_entry(pmu, &pmus, list)
866 if (!strcmp(pmu->name, name))
872 struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
875 * pmu iterator: If pmu is NULL, we start at the begin,
876 * otherwise return the next pmu. Returns NULL on end.
880 pmu = list_prepare_entry(pmu, &pmus, list);
882 list_for_each_entry_continue(pmu, &pmus, list)
887 struct perf_pmu *perf_pmu__find(const char *name)
889 struct perf_pmu *pmu;
892 * Once PMU is loaded it stays in the list,
893 * so we keep us from multiple reading/parsing
894 * the pmu format definitions.
896 pmu = pmu_find(name);
900 return pmu_lookup(name);
903 static struct perf_pmu_format *
904 pmu_find_format(struct list_head *formats, const char *name)
906 struct perf_pmu_format *format;
908 list_for_each_entry(format, formats, list)
909 if (!strcmp(format->name, name))
915 __u64 perf_pmu__format_bits(struct list_head *formats, const char *name)
917 struct perf_pmu_format *format = pmu_find_format(formats, name);
924 for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
925 bits |= 1ULL << fbit;
930 int perf_pmu__format_type(struct list_head *formats, const char *name)
932 struct perf_pmu_format *format = pmu_find_format(formats, name);
937 return format->value;
941 * Sets value based on the format definition (format parameter)
942 * and unformated value (value parameter).
944 static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
947 unsigned long fbit, vbit;
949 for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
951 if (!test_bit(fbit, format))
954 if (value & (1llu << vbit++))
955 *v |= (1llu << fbit);
957 *v &= ~(1llu << fbit);
961 static __u64 pmu_format_max_value(const unsigned long *format)
965 w = bitmap_weight(format, PERF_PMU_FORMAT_BITS);
969 return (1ULL << w) - 1;
974 * Term is a string term, and might be a param-term. Try to look up it's value
975 * in the remaining terms.
976 * - We have a term like "base-or-format-term=param-term",
977 * - We need to find the value supplied for "param-term" (with param-term named
978 * in a config string) later on in the term list.
980 static int pmu_resolve_param_term(struct parse_events_term *term,
981 struct list_head *head_terms,
984 struct parse_events_term *t;
986 list_for_each_entry(t, head_terms, list) {
987 if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM &&
988 t->config && !strcmp(t->config, term->config)) {
996 printf("Required parameter '%s' not specified\n", term->config);
1001 static char *pmu_formats_string(struct list_head *formats)
1003 struct perf_pmu_format *format;
1005 struct strbuf buf = STRBUF_INIT;
1011 /* sysfs exported terms */
1012 list_for_each_entry(format, formats, list)
1013 if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
1016 str = strbuf_detach(&buf, NULL);
1018 strbuf_release(&buf);
1024 * Setup one of config[12] attr members based on the
1025 * user input data - term parameter.
1027 static int pmu_config_term(struct list_head *formats,
1028 struct perf_event_attr *attr,
1029 struct parse_events_term *term,
1030 struct list_head *head_terms,
1031 bool zero, struct parse_events_error *err)
1033 struct perf_pmu_format *format;
1038 * If this is a parameter we've already used for parameterized-eval,
1039 * skip it in normal eval.
1045 * Hardcoded terms should be already in, so nothing
1046 * to be done for them.
1048 if (parse_events__is_hardcoded_term(term))
1051 format = pmu_find_format(formats, term->config);
1054 printf("Invalid event/parameter '%s'\n", term->config);
1056 char *pmu_term = pmu_formats_string(formats);
1058 parse_events__handle_error(err, term->err_term,
1059 strdup("unknown term"),
1060 parse_events_formats_error_string(pmu_term));
1066 switch (format->value) {
1067 case PERF_PMU_FORMAT_VALUE_CONFIG:
1070 case PERF_PMU_FORMAT_VALUE_CONFIG1:
1071 vp = &attr->config1;
1073 case PERF_PMU_FORMAT_VALUE_CONFIG2:
1074 vp = &attr->config2;
1081 * Either directly use a numeric term, or try to translate string terms
1082 * using event parameters.
1084 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1085 if (term->no_value &&
1086 bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) {
1088 parse_events__handle_error(err, term->err_val,
1089 strdup("no value assigned for term"),
1095 val = term->val.num;
1096 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1097 if (strcmp(term->val.str, "?")) {
1099 pr_info("Invalid sysfs entry %s=%s\n",
1100 term->config, term->val.str);
1103 parse_events__handle_error(err, term->err_val,
1104 strdup("expected numeric value"),
1110 if (pmu_resolve_param_term(term, head_terms, &val))
1115 max_val = pmu_format_max_value(format->bits);
1116 if (val > max_val) {
1120 parse_events__handle_error(err, term->err_val,
1122 "value too big for format, maximum is %llu",
1123 (unsigned long long)max_val) < 0
1124 ? strdup("value too big for format")
1130 * Assume we don't care if !err, in which case the value will be
1131 * silently truncated.
1135 pmu_format_value(format->bits, val, vp, zero);
1139 int perf_pmu__config_terms(struct list_head *formats,
1140 struct perf_event_attr *attr,
1141 struct list_head *head_terms,
1142 bool zero, struct parse_events_error *err)
1144 struct parse_events_term *term;
1146 list_for_each_entry(term, head_terms, list) {
1147 if (pmu_config_term(formats, attr, term, head_terms,
1156 * Configures event's 'attr' parameter based on the:
1157 * 1) users input - specified in terms parameter
1158 * 2) pmu format definitions - specified by pmu parameter
1160 int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
1161 struct list_head *head_terms,
1162 struct parse_events_error *err)
1164 bool zero = !!pmu->default_config;
1166 attr->type = pmu->type;
1167 return perf_pmu__config_terms(&pmu->format, attr, head_terms,
1171 static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
1172 struct parse_events_term *term)
1174 struct perf_pmu_alias *alias;
1177 if (parse_events__is_hardcoded_term(term))
1180 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1181 if (term->val.num != 1)
1183 if (pmu_find_format(&pmu->format, term->config))
1185 name = term->config;
1186 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1187 if (strcasecmp(term->config, "event"))
1189 name = term->val.str;
1194 list_for_each_entry(alias, &pmu->aliases, list) {
1195 if (!strcasecmp(alias->name, name))
1202 static int check_info_data(struct perf_pmu_alias *alias,
1203 struct perf_pmu_info *info)
1206 * Only one term in event definition can
1207 * define unit, scale and snapshot, fail
1208 * if there's more than one.
1210 if ((info->unit && alias->unit[0]) ||
1211 (info->scale && alias->scale) ||
1212 (info->snapshot && alias->snapshot))
1216 info->unit = alias->unit;
1219 info->scale = alias->scale;
1221 if (alias->snapshot)
1222 info->snapshot = alias->snapshot;
1228 * Find alias in the terms list and replace it with the terms
1229 * defined for the alias
1231 int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
1232 struct perf_pmu_info *info)
1234 struct parse_events_term *term, *h;
1235 struct perf_pmu_alias *alias;
1238 info->per_pkg = false;
1241 * Mark unit and scale as not set
1242 * (different from default values, see below)
1246 info->snapshot = false;
1247 info->metric_expr = NULL;
1248 info->metric_name = NULL;
1250 list_for_each_entry_safe(term, h, head_terms, list) {
1251 alias = pmu_find_alias(pmu, term);
1254 ret = pmu_alias_terms(alias, &term->list);
1258 ret = check_info_data(alias, info);
1263 info->per_pkg = true;
1264 info->metric_expr = alias->metric_expr;
1265 info->metric_name = alias->metric_name;
1267 list_del_init(&term->list);
1268 parse_events_term__delete(term);
1272 * if no unit or scale foundin aliases, then
1273 * set defaults as for evsel
1274 * unit cannot left to NULL
1276 if (info->unit == NULL)
1279 if (info->scale == 0.0)
1285 int perf_pmu__new_format(struct list_head *list, char *name,
1286 int config, unsigned long *bits)
1288 struct perf_pmu_format *format;
1290 format = zalloc(sizeof(*format));
1294 format->name = strdup(name);
1295 format->value = config;
1296 memcpy(format->bits, bits, sizeof(format->bits));
1298 list_add_tail(&format->list, list);
1302 void perf_pmu__set_format(unsigned long *bits, long from, long to)
1309 memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
1310 for (b = from; b <= to; b++)
1314 static int sub_non_neg(int a, int b)
1321 static char *format_alias(char *buf, int len, struct perf_pmu *pmu,
1322 struct perf_pmu_alias *alias)
1324 struct parse_events_term *term;
1325 int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name);
1327 list_for_each_entry(term, &alias->terms, list) {
1328 if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
1329 used += snprintf(buf + used, sub_non_neg(len, used),
1330 ",%s=%s", term->config,
1334 if (sub_non_neg(len, used) > 0) {
1338 if (sub_non_neg(len, used) > 0) {
1342 buf[len - 1] = '\0';
1347 static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu,
1348 struct perf_pmu_alias *alias)
1350 snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name);
1364 static int cmp_sevent(const void *a, const void *b)
1366 const struct sevent *as = a;
1367 const struct sevent *bs = b;
1369 /* Put extra events last */
1370 if (!!as->desc != !!bs->desc)
1371 return !!as->desc - !!bs->desc;
1372 if (as->topic && bs->topic) {
1373 int n = strcmp(as->topic, bs->topic);
1378 return strcmp(as->name, bs->name);
1381 static void wordwrap(char *s, int start, int max, int corr)
1387 int wlen = strcspn(s, " \t");
1389 if (column + wlen >= max && column > start) {
1390 printf("\n%*s", start, "");
1391 column = start + corr;
1393 n = printf("%s%.*s", column > start ? " " : "", wlen, s);
1402 bool is_pmu_core(const char *name)
1404 return !strcmp(name, "cpu") || is_arm_pmu_core(name);
1407 void print_pmu_events(const char *event_glob, bool name_only, bool quiet_flag,
1408 bool long_desc, bool details_flag, bool deprecated)
1410 struct perf_pmu *pmu;
1411 struct perf_pmu_alias *alias;
1415 struct sevent *aliases;
1417 int columns = pager_get_columns();
1422 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1423 list_for_each_entry(alias, &pmu->aliases, list)
1425 if (pmu->selectable)
1428 aliases = zalloc(sizeof(struct sevent) * len);
1433 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1434 list_for_each_entry(alias, &pmu->aliases, list) {
1435 char *name = alias->desc ? alias->name :
1436 format_alias(buf, sizeof(buf), pmu, alias);
1437 bool is_cpu = !strcmp(pmu->name, "cpu");
1439 if (alias->deprecated && !deprecated)
1442 if (event_glob != NULL &&
1443 !(strglobmatch_nocase(name, event_glob) ||
1444 (!is_cpu && strglobmatch_nocase(alias->name,
1447 strglobmatch_nocase(alias->topic, event_glob))))
1450 if (is_cpu && !name_only && !alias->desc)
1451 name = format_alias_or(buf, sizeof(buf), pmu, alias);
1453 aliases[j].name = name;
1454 if (is_cpu && !name_only && !alias->desc)
1455 aliases[j].name = format_alias_or(buf,
1458 aliases[j].name = strdup(aliases[j].name);
1459 if (!aliases[j].name)
1462 aliases[j].desc = long_desc ? alias->long_desc :
1464 aliases[j].topic = alias->topic;
1465 aliases[j].str = alias->str;
1466 aliases[j].pmu = pmu->name;
1467 aliases[j].metric_expr = alias->metric_expr;
1468 aliases[j].metric_name = alias->metric_name;
1471 if (pmu->selectable &&
1472 (event_glob == NULL || strglobmatch(pmu->name, event_glob))) {
1474 if (asprintf(&s, "%s//", pmu->name) < 0)
1476 aliases[j].name = s;
1481 qsort(aliases, len, sizeof(struct sevent), cmp_sevent);
1482 for (j = 0; j < len; j++) {
1483 /* Skip duplicates */
1484 if (j > 0 && !strcmp(aliases[j].name, aliases[j - 1].name))
1487 printf("%s ", aliases[j].name);
1490 if (aliases[j].desc && !quiet_flag) {
1493 if (aliases[j].topic && (!topic ||
1494 strcmp(topic, aliases[j].topic))) {
1495 printf("%s%s:\n", topic ? "\n" : "",
1497 topic = aliases[j].topic;
1499 printf(" %-50s\n", aliases[j].name);
1500 printf("%*s", 8, "[");
1501 wordwrap(aliases[j].desc, 8, columns, 0);
1504 printf("%*s%s/%s/ ", 8, "", aliases[j].pmu, aliases[j].str);
1505 if (aliases[j].metric_name)
1506 printf(" MetricName: %s", aliases[j].metric_name);
1507 if (aliases[j].metric_expr)
1508 printf(" MetricExpr: %s", aliases[j].metric_expr);
1512 printf(" %-50s [Kernel PMU event]\n", aliases[j].name);
1515 if (printed && pager_in_use())
1518 for (j = 0; j < len; j++)
1519 zfree(&aliases[j].name);
1524 printf("FATAL: not enough memory to print PMU events\n");
1529 bool pmu_have_event(const char *pname, const char *name)
1531 struct perf_pmu *pmu;
1532 struct perf_pmu_alias *alias;
1535 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1536 if (strcmp(pname, pmu->name))
1538 list_for_each_entry(alias, &pmu->aliases, list)
1539 if (!strcmp(alias->name, name))
1545 static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
1547 char path[PATH_MAX];
1550 sysfs = sysfs__mountpoint();
1554 snprintf(path, PATH_MAX,
1555 "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name);
1556 if (!file_available(path))
1558 return fopen(path, "r");
1561 int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
1568 va_start(args, fmt);
1569 file = perf_pmu__open_file(pmu, name);
1571 ret = vfscanf(file, fmt, args);