1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/list.h>
3 #include <linux/compiler.h>
13 #include <api/fs/fs.h>
18 #include "parse-events.h"
21 #include "pmu-events/pmu-events.h"
25 struct perf_pmu_format {
28 DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
29 struct list_head list;
32 #define EVENT_SOURCE_DEVICE_PATH "/bus/event_source/devices/"
34 int perf_pmu_parse(struct list_head *list, char *name);
35 extern FILE *perf_pmu_in;
37 static LIST_HEAD(pmus);
40 * Parse & process all the sysfs attributes located under
41 * the directory specified in 'dir' parameter.
43 int perf_pmu__format_parse(char *dir, struct list_head *head)
45 struct dirent *evt_ent;
49 format_dir = opendir(dir);
53 while (!ret && (evt_ent = readdir(format_dir))) {
55 char *name = evt_ent->d_name;
58 if (!strcmp(name, ".") || !strcmp(name, ".."))
61 snprintf(path, PATH_MAX, "%s/%s", dir, name);
64 file = fopen(path, "r");
69 ret = perf_pmu_parse(head, name);
78 * Reading/parsing the default pmu format definition, which should be
80 * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
82 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 (stat(path, &st) < 0)
95 return 0; /* no error if format does not exist */
97 if (perf_pmu__format_parse(path, format))
103 static int 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 snprintf(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 = convert_scale(scale, NULL, &alias->scale);
172 static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *name)
178 snprintf(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 snprintf(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 snprintf(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,
312 struct parse_events_term *term;
313 struct perf_pmu_alias *alias;
318 alias = malloc(sizeof(*alias));
322 INIT_LIST_HEAD(&alias->terms);
324 alias->unit[0] = '\0';
325 alias->per_pkg = false;
326 alias->snapshot = false;
328 ret = parse_events_terms(&alias->terms, val);
330 pr_err("Cannot parse alias %s: %d\n", val, ret);
335 /* Scan event and remove leading zeroes, spaces, newlines, some
336 * platforms have terms specified as
337 * event=0x0091 (read from files ../<PMU>/events/<FILE>
338 * and terms specified as event=0x91 (read from JSON files).
340 * Rebuild string to make alias->str member comparable.
342 memset(newval, 0, sizeof(newval));
344 list_for_each_entry(term, &alias->terms, list) {
346 ret += scnprintf(newval + ret, sizeof(newval) - ret,
348 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM)
349 ret += scnprintf(newval + ret, sizeof(newval) - ret,
350 "%s=%#x", term->config, term->val.num);
351 else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
352 ret += scnprintf(newval + ret, sizeof(newval) - ret,
353 "%s=%s", term->config, term->val.str);
356 alias->name = strdup(name);
359 * load unit name and scale if available
361 perf_pmu__parse_unit(alias, dir, name);
362 perf_pmu__parse_scale(alias, dir, name);
363 perf_pmu__parse_per_pkg(alias, dir, name);
364 perf_pmu__parse_snapshot(alias, dir, name);
367 alias->metric_expr = metric_expr ? strdup(metric_expr) : NULL;
368 alias->metric_name = metric_name ? strdup(metric_name): NULL;
369 alias->desc = desc ? strdup(desc) : NULL;
370 alias->long_desc = long_desc ? strdup(long_desc) :
371 desc ? strdup(desc) : NULL;
372 alias->topic = topic ? strdup(topic) : NULL;
374 if (convert_scale(unit, &unit, &alias->scale) < 0)
376 snprintf(alias->unit, sizeof(alias->unit), "%s", unit);
378 alias->per_pkg = perpkg && sscanf(perpkg, "%d", &num) == 1 && num == 1;
379 alias->str = strdup(newval);
381 if (!perf_pmu_merge_alias(alias, list))
382 list_add_tail(&alias->list, list);
387 static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file)
392 ret = fread(buf, 1, sizeof(buf), file);
398 /* Remove trailing newline from sysfs file */
401 return __perf_pmu__new_alias(list, dir, name, NULL, buf, NULL, NULL, NULL,
405 static inline bool pmu_alias_info_file(char *name)
410 if (len > 5 && !strcmp(name + len - 5, ".unit"))
412 if (len > 6 && !strcmp(name + len - 6, ".scale"))
414 if (len > 8 && !strcmp(name + len - 8, ".per-pkg"))
416 if (len > 9 && !strcmp(name + len - 9, ".snapshot"))
423 * Process all the sysfs attributes located under the directory
424 * specified in 'dir' parameter.
426 static int pmu_aliases_parse(char *dir, struct list_head *head)
428 struct dirent *evt_ent;
431 event_dir = opendir(dir);
435 while ((evt_ent = readdir(event_dir))) {
437 char *name = evt_ent->d_name;
440 if (!strcmp(name, ".") || !strcmp(name, ".."))
444 * skip info files parsed in perf_pmu__new_alias()
446 if (pmu_alias_info_file(name))
449 scnprintf(path, PATH_MAX, "%s/%s", dir, name);
451 file = fopen(path, "r");
453 pr_debug("Cannot open %s\n", path);
457 if (perf_pmu__new_alias(head, dir, name, file) < 0)
458 pr_debug("Cannot set up %s\n", name);
467 * Reading the pmu event aliases definition, which should be located at:
468 * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
470 static int pmu_aliases(const char *name, struct list_head *head)
474 const char *sysfs = sysfs__mountpoint();
479 snprintf(path, PATH_MAX,
480 "%s/bus/event_source/devices/%s/events", sysfs, name);
482 if (stat(path, &st) < 0)
483 return 0; /* no error if 'events' does not exist */
485 if (pmu_aliases_parse(path, head))
491 static int pmu_alias_terms(struct perf_pmu_alias *alias,
492 struct list_head *terms)
494 struct parse_events_term *term, *cloned;
498 list_for_each_entry(term, &alias->terms, list) {
499 ret = parse_events_term__clone(&cloned, term);
501 parse_events_terms__purge(&list);
505 * Weak terms don't override command line options,
506 * which we don't want for implicit terms in aliases.
509 list_add_tail(&cloned->list, &list);
511 list_splice(&list, terms);
516 * Reading/parsing the default pmu type value, which should be
518 * /sys/bus/event_source/devices/<dev>/type as sysfs attribute.
520 static int pmu_type(const char *name, __u32 *type)
526 const char *sysfs = sysfs__mountpoint();
531 snprintf(path, PATH_MAX,
532 "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name);
534 if (stat(path, &st) < 0)
537 file = fopen(path, "r");
541 if (1 != fscanf(file, "%u", type))
548 /* Add all pmus in sysfs to pmu list: */
549 static void pmu_read_sysfs(void)
554 const char *sysfs = sysfs__mountpoint();
559 snprintf(path, PATH_MAX,
560 "%s" EVENT_SOURCE_DEVICE_PATH, sysfs);
566 while ((dent = readdir(dir))) {
567 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
569 /* add to static LIST_HEAD(pmus): */
570 perf_pmu__find(dent->d_name);
576 static struct cpu_map *__pmu_cpumask(const char *path)
579 struct cpu_map *cpus;
581 file = fopen(path, "r");
585 cpus = cpu_map__read(file);
591 * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64)
592 * may have a "cpus" file.
594 #define CPUS_TEMPLATE_UNCORE "%s/bus/event_source/devices/%s/cpumask"
595 #define CPUS_TEMPLATE_CPU "%s/bus/event_source/devices/%s/cpus"
597 static struct cpu_map *pmu_cpumask(const char *name)
600 struct cpu_map *cpus;
601 const char *sysfs = sysfs__mountpoint();
602 const char *templates[] = {
603 CPUS_TEMPLATE_UNCORE,
607 const char **template;
612 for (template = templates; *template; template++) {
613 snprintf(path, PATH_MAX, *template, sysfs, name);
614 cpus = __pmu_cpumask(path);
622 static bool pmu_is_uncore(const char *name)
625 struct cpu_map *cpus;
626 const char *sysfs = sysfs__mountpoint();
628 snprintf(path, PATH_MAX, CPUS_TEMPLATE_UNCORE, sysfs, name);
629 cpus = __pmu_cpumask(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)
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 if (stat(path, &st) == 0)
659 * Return the CPU id as a raw string.
661 * Each architecture should provide a more precise id string that
662 * can be use to match the architecture's "mapfile".
664 char * __weak get_cpuid_str(struct perf_pmu *pmu __maybe_unused)
669 /* Return zero when the cpuid from the mapfile.csv matches the
670 * cpuid string generated on this platform.
671 * Otherwise return non-zero.
673 int strcmp_cpuid_str(const char *mapcpuid, const char *cpuid)
676 regmatch_t pmatch[1];
679 if (regcomp(&re, mapcpuid, REG_EXTENDED) != 0) {
680 /* Warn unable to generate match particular string. */
681 pr_info("Invalid regular expression %s\n", mapcpuid);
685 match = !regexec(&re, cpuid, 1, pmatch, 0);
688 size_t match_len = (pmatch[0].rm_eo - pmatch[0].rm_so);
690 /* Verify the entire string matched. */
691 if (match_len == strlen(cpuid))
697 static char *perf_pmu__getcpuid(struct perf_pmu *pmu)
702 cpuid = getenv("PERF_CPUID");
704 cpuid = strdup(cpuid);
706 cpuid = get_cpuid_str(pmu);
711 pr_debug("Using CPUID %s\n", cpuid);
717 struct pmu_events_map *perf_pmu__find_map(struct perf_pmu *pmu)
719 struct pmu_events_map *map;
720 char *cpuid = perf_pmu__getcpuid(pmu);
723 /* on some platforms which uses cpus map, cpuid can be NULL for
724 * PMUs other than CORE PMUs.
731 map = &pmu_events_map[i++];
737 if (!strcmp_cpuid_str(map->cpuid, cpuid))
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 static void pmu_add_cpu_aliases(struct list_head *head, struct perf_pmu *pmu)
752 struct pmu_events_map *map;
753 struct pmu_event *pe;
754 const char *name = pmu->name;
757 map = perf_pmu__find_map(pmu);
762 * Found a matching PMU events table. Create aliases
767 pe = &map->table[i++];
769 if (pe->metric_group || pe->metric_name)
774 if (!is_arm_pmu_core(name)) {
775 pname = pe->pmu ? pe->pmu : "cpu";
776 if (strcmp(pname, name))
780 /* need type casts to override 'const' */
781 __perf_pmu__new_alias(head, NULL, (char *)pe->name,
782 (char *)pe->desc, (char *)pe->event,
783 (char *)pe->long_desc, (char *)pe->topic,
784 (char *)pe->unit, (char *)pe->perpkg,
785 (char *)pe->metric_expr,
786 (char *)pe->metric_name);
790 struct perf_event_attr * __weak
791 perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
796 static struct perf_pmu *pmu_lookup(const char *name)
798 struct perf_pmu *pmu;
804 * The pmu data we store & need consists of the pmu
805 * type value and format definitions. Load both right
808 if (pmu_format(name, &format))
812 * Check the type first to avoid unnecessary work.
814 if (pmu_type(name, &type))
817 if (pmu_aliases(name, &aliases))
820 pmu = zalloc(sizeof(*pmu));
824 pmu->cpus = pmu_cpumask(name);
825 pmu->name = strdup(name);
827 pmu->is_uncore = pmu_is_uncore(name);
828 pmu_add_cpu_aliases(&aliases, pmu);
830 INIT_LIST_HEAD(&pmu->format);
831 INIT_LIST_HEAD(&pmu->aliases);
832 list_splice(&format, &pmu->format);
833 list_splice(&aliases, &pmu->aliases);
834 list_add_tail(&pmu->list, &pmus);
836 pmu->default_config = perf_pmu__get_default_config(pmu);
841 static struct perf_pmu *pmu_find(const char *name)
843 struct perf_pmu *pmu;
845 list_for_each_entry(pmu, &pmus, list)
846 if (!strcmp(pmu->name, name))
852 struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
855 * pmu iterator: If pmu is NULL, we start at the begin,
856 * otherwise return the next pmu. Returns NULL on end.
860 pmu = list_prepare_entry(pmu, &pmus, list);
862 list_for_each_entry_continue(pmu, &pmus, list)
867 struct perf_pmu *perf_pmu__find(const char *name)
869 struct perf_pmu *pmu;
872 * Once PMU is loaded it stays in the list,
873 * so we keep us from multiple reading/parsing
874 * the pmu format definitions.
876 pmu = pmu_find(name);
880 return pmu_lookup(name);
883 static struct perf_pmu_format *
884 pmu_find_format(struct list_head *formats, const char *name)
886 struct perf_pmu_format *format;
888 list_for_each_entry(format, formats, list)
889 if (!strcmp(format->name, name))
895 __u64 perf_pmu__format_bits(struct list_head *formats, const char *name)
897 struct perf_pmu_format *format = pmu_find_format(formats, name);
904 for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
905 bits |= 1ULL << fbit;
911 * Sets value based on the format definition (format parameter)
912 * and unformated value (value parameter).
914 static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
917 unsigned long fbit, vbit;
919 for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
921 if (!test_bit(fbit, format))
924 if (value & (1llu << vbit++))
925 *v |= (1llu << fbit);
927 *v &= ~(1llu << fbit);
931 static __u64 pmu_format_max_value(const unsigned long *format)
935 w = bitmap_weight(format, PERF_PMU_FORMAT_BITS);
939 return (1ULL << w) - 1;
944 * Term is a string term, and might be a param-term. Try to look up it's value
945 * in the remaining terms.
946 * - We have a term like "base-or-format-term=param-term",
947 * - We need to find the value supplied for "param-term" (with param-term named
948 * in a config string) later on in the term list.
950 static int pmu_resolve_param_term(struct parse_events_term *term,
951 struct list_head *head_terms,
954 struct parse_events_term *t;
956 list_for_each_entry(t, head_terms, list) {
957 if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
958 if (!strcmp(t->config, term->config)) {
967 printf("Required parameter '%s' not specified\n", term->config);
972 static char *pmu_formats_string(struct list_head *formats)
974 struct perf_pmu_format *format;
976 struct strbuf buf = STRBUF_INIT;
982 /* sysfs exported terms */
983 list_for_each_entry(format, formats, list)
984 if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
987 str = strbuf_detach(&buf, NULL);
989 strbuf_release(&buf);
995 * Setup one of config[12] attr members based on the
996 * user input data - term parameter.
998 static int pmu_config_term(struct list_head *formats,
999 struct perf_event_attr *attr,
1000 struct parse_events_term *term,
1001 struct list_head *head_terms,
1002 bool zero, struct parse_events_error *err)
1004 struct perf_pmu_format *format;
1009 * If this is a parameter we've already used for parameterized-eval,
1010 * skip it in normal eval.
1016 * Hardcoded terms should be already in, so nothing
1017 * to be done for them.
1019 if (parse_events__is_hardcoded_term(term))
1022 format = pmu_find_format(formats, term->config);
1025 printf("Invalid event/parameter '%s'\n", term->config);
1027 char *pmu_term = pmu_formats_string(formats);
1029 err->idx = term->err_term;
1030 err->str = strdup("unknown term");
1031 err->help = parse_events_formats_error_string(pmu_term);
1037 switch (format->value) {
1038 case PERF_PMU_FORMAT_VALUE_CONFIG:
1041 case PERF_PMU_FORMAT_VALUE_CONFIG1:
1042 vp = &attr->config1;
1044 case PERF_PMU_FORMAT_VALUE_CONFIG2:
1045 vp = &attr->config2;
1052 * Either directly use a numeric term, or try to translate string terms
1053 * using event parameters.
1055 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1056 if (term->no_value &&
1057 bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) {
1059 err->idx = term->err_val;
1060 err->str = strdup("no value assigned for term");
1065 val = term->val.num;
1066 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1067 if (strcmp(term->val.str, "?")) {
1069 pr_info("Invalid sysfs entry %s=%s\n",
1070 term->config, term->val.str);
1073 err->idx = term->err_val;
1074 err->str = strdup("expected numeric value");
1079 if (pmu_resolve_param_term(term, head_terms, &val))
1084 max_val = pmu_format_max_value(format->bits);
1085 if (val > max_val) {
1087 err->idx = term->err_val;
1088 if (asprintf(&err->str,
1089 "value too big for format, maximum is %llu",
1090 (unsigned long long)max_val) < 0)
1091 err->str = strdup("value too big for format");
1095 * Assume we don't care if !err, in which case the value will be
1096 * silently truncated.
1100 pmu_format_value(format->bits, val, vp, zero);
1104 int perf_pmu__config_terms(struct list_head *formats,
1105 struct perf_event_attr *attr,
1106 struct list_head *head_terms,
1107 bool zero, struct parse_events_error *err)
1109 struct parse_events_term *term;
1111 list_for_each_entry(term, head_terms, list) {
1112 if (pmu_config_term(formats, attr, term, head_terms,
1121 * Configures event's 'attr' parameter based on the:
1122 * 1) users input - specified in terms parameter
1123 * 2) pmu format definitions - specified by pmu parameter
1125 int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
1126 struct list_head *head_terms,
1127 struct parse_events_error *err)
1129 bool zero = !!pmu->default_config;
1131 attr->type = pmu->type;
1132 return perf_pmu__config_terms(&pmu->format, attr, head_terms,
1136 static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
1137 struct parse_events_term *term)
1139 struct perf_pmu_alias *alias;
1142 if (parse_events__is_hardcoded_term(term))
1145 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1146 if (term->val.num != 1)
1148 if (pmu_find_format(&pmu->format, term->config))
1150 name = term->config;
1151 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1152 if (strcasecmp(term->config, "event"))
1154 name = term->val.str;
1159 list_for_each_entry(alias, &pmu->aliases, list) {
1160 if (!strcasecmp(alias->name, name))
1167 static int check_info_data(struct perf_pmu_alias *alias,
1168 struct perf_pmu_info *info)
1171 * Only one term in event definition can
1172 * define unit, scale and snapshot, fail
1173 * if there's more than one.
1175 if ((info->unit && alias->unit[0]) ||
1176 (info->scale && alias->scale) ||
1177 (info->snapshot && alias->snapshot))
1181 info->unit = alias->unit;
1184 info->scale = alias->scale;
1186 if (alias->snapshot)
1187 info->snapshot = alias->snapshot;
1193 * Find alias in the terms list and replace it with the terms
1194 * defined for the alias
1196 int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
1197 struct perf_pmu_info *info)
1199 struct parse_events_term *term, *h;
1200 struct perf_pmu_alias *alias;
1203 info->per_pkg = false;
1206 * Mark unit and scale as not set
1207 * (different from default values, see below)
1211 info->snapshot = false;
1212 info->metric_expr = NULL;
1213 info->metric_name = NULL;
1215 list_for_each_entry_safe(term, h, head_terms, list) {
1216 alias = pmu_find_alias(pmu, term);
1219 ret = pmu_alias_terms(alias, &term->list);
1223 ret = check_info_data(alias, info);
1228 info->per_pkg = true;
1229 info->metric_expr = alias->metric_expr;
1230 info->metric_name = alias->metric_name;
1232 list_del(&term->list);
1237 * if no unit or scale foundin aliases, then
1238 * set defaults as for evsel
1239 * unit cannot left to NULL
1241 if (info->unit == NULL)
1244 if (info->scale == 0.0)
1250 int perf_pmu__new_format(struct list_head *list, char *name,
1251 int config, unsigned long *bits)
1253 struct perf_pmu_format *format;
1255 format = zalloc(sizeof(*format));
1259 format->name = strdup(name);
1260 format->value = config;
1261 memcpy(format->bits, bits, sizeof(format->bits));
1263 list_add_tail(&format->list, list);
1267 void perf_pmu__set_format(unsigned long *bits, long from, long to)
1274 memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
1275 for (b = from; b <= to; b++)
1279 static int sub_non_neg(int a, int b)
1286 static char *format_alias(char *buf, int len, struct perf_pmu *pmu,
1287 struct perf_pmu_alias *alias)
1289 struct parse_events_term *term;
1290 int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name);
1292 list_for_each_entry(term, &alias->terms, list) {
1293 if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
1294 used += snprintf(buf + used, sub_non_neg(len, used),
1295 ",%s=%s", term->config,
1299 if (sub_non_neg(len, used) > 0) {
1303 if (sub_non_neg(len, used) > 0) {
1307 buf[len - 1] = '\0';
1312 static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu,
1313 struct perf_pmu_alias *alias)
1315 snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name);
1329 static int cmp_sevent(const void *a, const void *b)
1331 const struct sevent *as = a;
1332 const struct sevent *bs = b;
1334 /* Put extra events last */
1335 if (!!as->desc != !!bs->desc)
1336 return !!as->desc - !!bs->desc;
1337 if (as->topic && bs->topic) {
1338 int n = strcmp(as->topic, bs->topic);
1343 return strcmp(as->name, bs->name);
1346 static void wordwrap(char *s, int start, int max, int corr)
1352 int wlen = strcspn(s, " \t");
1354 if (column + wlen >= max && column > start) {
1355 printf("\n%*s", start, "");
1356 column = start + corr;
1358 n = printf("%s%.*s", column > start ? " " : "", wlen, s);
1367 void print_pmu_events(const char *event_glob, bool name_only, bool quiet_flag,
1368 bool long_desc, bool details_flag)
1370 struct perf_pmu *pmu;
1371 struct perf_pmu_alias *alias;
1375 struct sevent *aliases;
1377 int columns = pager_get_columns();
1382 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1383 list_for_each_entry(alias, &pmu->aliases, list)
1385 if (pmu->selectable)
1388 aliases = zalloc(sizeof(struct sevent) * len);
1393 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1394 list_for_each_entry(alias, &pmu->aliases, list) {
1395 char *name = alias->desc ? alias->name :
1396 format_alias(buf, sizeof(buf), pmu, alias);
1397 bool is_cpu = !strcmp(pmu->name, "cpu");
1399 if (event_glob != NULL &&
1400 !(strglobmatch_nocase(name, event_glob) ||
1401 (!is_cpu && strglobmatch_nocase(alias->name,
1404 strglobmatch_nocase(alias->topic, event_glob))))
1407 if (is_cpu && !name_only && !alias->desc)
1408 name = format_alias_or(buf, sizeof(buf), pmu, alias);
1410 aliases[j].name = name;
1411 if (is_cpu && !name_only && !alias->desc)
1412 aliases[j].name = format_alias_or(buf,
1415 aliases[j].name = strdup(aliases[j].name);
1416 if (!aliases[j].name)
1419 aliases[j].desc = long_desc ? alias->long_desc :
1421 aliases[j].topic = alias->topic;
1422 aliases[j].str = alias->str;
1423 aliases[j].pmu = pmu->name;
1424 aliases[j].metric_expr = alias->metric_expr;
1425 aliases[j].metric_name = alias->metric_name;
1428 if (pmu->selectable &&
1429 (event_glob == NULL || strglobmatch(pmu->name, event_glob))) {
1431 if (asprintf(&s, "%s//", pmu->name) < 0)
1433 aliases[j].name = s;
1438 qsort(aliases, len, sizeof(struct sevent), cmp_sevent);
1439 for (j = 0; j < len; j++) {
1440 /* Skip duplicates */
1441 if (j > 0 && !strcmp(aliases[j].name, aliases[j - 1].name))
1444 printf("%s ", aliases[j].name);
1447 if (aliases[j].desc && !quiet_flag) {
1450 if (aliases[j].topic && (!topic ||
1451 strcmp(topic, aliases[j].topic))) {
1452 printf("%s%s:\n", topic ? "\n" : "",
1454 topic = aliases[j].topic;
1456 printf(" %-50s\n", aliases[j].name);
1457 printf("%*s", 8, "[");
1458 wordwrap(aliases[j].desc, 8, columns, 0);
1461 printf("%*s%s/%s/ ", 8, "", aliases[j].pmu, aliases[j].str);
1462 if (aliases[j].metric_name)
1463 printf(" MetricName: %s", aliases[j].metric_name);
1464 if (aliases[j].metric_expr)
1465 printf(" MetricExpr: %s", aliases[j].metric_expr);
1469 printf(" %-50s [Kernel PMU event]\n", aliases[j].name);
1472 if (printed && pager_in_use())
1475 for (j = 0; j < len; j++)
1476 zfree(&aliases[j].name);
1481 printf("FATAL: not enough memory to print PMU events\n");
1486 bool pmu_have_event(const char *pname, const char *name)
1488 struct perf_pmu *pmu;
1489 struct perf_pmu_alias *alias;
1492 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1493 if (strcmp(pname, pmu->name))
1495 list_for_each_entry(alias, &pmu->aliases, list)
1496 if (!strcmp(alias->name, name))
1502 static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
1505 char path[PATH_MAX];
1508 sysfs = sysfs__mountpoint();
1512 snprintf(path, PATH_MAX,
1513 "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name);
1515 if (stat(path, &st) < 0)
1518 return fopen(path, "r");
1521 int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
1528 va_start(args, fmt);
1529 file = perf_pmu__open_file(pmu, name);
1531 ret = vfscanf(file, fmt, args);