1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/list.h>
3 #include <linux/compiler.h>
4 #include <linux/string.h>
14 #include <api/fs/fs.h>
19 #include "parse-events.h"
22 #include "pmu-events/pmu-events.h"
26 struct perf_pmu_format {
29 DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
30 struct list_head list;
33 int perf_pmu_parse(struct list_head *list, char *name);
34 extern FILE *perf_pmu_in;
36 static LIST_HEAD(pmus);
39 * Parse & process all the sysfs attributes located under
40 * the directory specified in 'dir' parameter.
42 int perf_pmu__format_parse(char *dir, struct list_head *head)
44 struct dirent *evt_ent;
48 format_dir = opendir(dir);
52 while (!ret && (evt_ent = readdir(format_dir))) {
54 char *name = evt_ent->d_name;
57 if (!strcmp(name, ".") || !strcmp(name, ".."))
60 snprintf(path, PATH_MAX, "%s/%s", dir, name);
63 file = fopen(path, "r");
68 ret = perf_pmu_parse(head, name);
77 * Reading/parsing the default pmu format definition, which should be
79 * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
81 static int pmu_format(const char *name, struct list_head *format)
85 const char *sysfs = sysfs__mountpoint();
90 snprintf(path, PATH_MAX,
91 "%s" EVENT_SOURCE_DEVICE_PATH "%s/format", sysfs, name);
93 if (stat(path, &st) < 0)
94 return 0; /* no error if format does not exist */
96 if (perf_pmu__format_parse(path, format))
102 static int convert_scale(const char *scale, char **end, double *sval)
108 * save current locale
110 lc = setlocale(LC_NUMERIC, NULL);
113 * The lc string may be allocated in static storage,
114 * so get a dynamic copy to make it survive setlocale
124 * force to C locale to ensure kernel
125 * scale string is converted correctly.
126 * kernel uses default C locale.
128 setlocale(LC_NUMERIC, "C");
130 *sval = strtod(scale, end);
134 setlocale(LC_NUMERIC, lc);
139 static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *name)
147 scnprintf(path, PATH_MAX, "%s/%s.scale", dir, name);
149 fd = open(path, O_RDONLY);
153 if (fstat(fd, &st) < 0)
156 sret = read(fd, scale, sizeof(scale)-1);
160 if (scale[sret - 1] == '\n')
161 scale[sret - 1] = '\0';
165 ret = convert_scale(scale, NULL, &alias->scale);
171 static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *name)
177 scnprintf(path, PATH_MAX, "%s/%s.unit", dir, name);
179 fd = open(path, O_RDONLY);
183 sret = read(fd, alias->unit, UNIT_MAX_LEN);
189 if (alias->unit[sret - 1] == '\n')
190 alias->unit[sret - 1] = '\0';
192 alias->unit[sret] = '\0';
197 alias->unit[0] = '\0';
202 perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, char *dir, char *name)
207 scnprintf(path, PATH_MAX, "%s/%s.per-pkg", dir, name);
209 fd = open(path, O_RDONLY);
215 alias->per_pkg = true;
219 static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias,
220 char *dir, char *name)
225 scnprintf(path, PATH_MAX, "%s/%s.snapshot", dir, name);
227 fd = open(path, O_RDONLY);
231 alias->snapshot = true;
236 static void perf_pmu_assign_str(char *name, const char *field, char **old_str,
242 if (*new_str) { /* Have new string, check with old */
243 if (strcasecmp(*old_str, *new_str))
244 pr_debug("alias %s differs in field '%s'\n",
247 } else /* Nothing new --> keep old string */
254 static void perf_pmu_update_alias(struct perf_pmu_alias *old,
255 struct perf_pmu_alias *newalias)
257 perf_pmu_assign_str(old->name, "desc", &old->desc, &newalias->desc);
258 perf_pmu_assign_str(old->name, "long_desc", &old->long_desc,
259 &newalias->long_desc);
260 perf_pmu_assign_str(old->name, "topic", &old->topic, &newalias->topic);
261 perf_pmu_assign_str(old->name, "metric_expr", &old->metric_expr,
262 &newalias->metric_expr);
263 perf_pmu_assign_str(old->name, "metric_name", &old->metric_name,
264 &newalias->metric_name);
265 perf_pmu_assign_str(old->name, "value", &old->str, &newalias->str);
266 old->scale = newalias->scale;
267 old->per_pkg = newalias->per_pkg;
268 old->snapshot = newalias->snapshot;
269 memcpy(old->unit, newalias->unit, sizeof(old->unit));
272 /* Delete an alias entry. */
273 static void perf_pmu_free_alias(struct perf_pmu_alias *newalias)
275 zfree(&newalias->name);
276 zfree(&newalias->desc);
277 zfree(&newalias->long_desc);
278 zfree(&newalias->topic);
279 zfree(&newalias->str);
280 zfree(&newalias->metric_expr);
281 zfree(&newalias->metric_name);
282 parse_events_terms__purge(&newalias->terms);
286 /* Merge an alias, search in alias list. If this name is already
287 * present merge both of them to combine all information.
289 static bool perf_pmu_merge_alias(struct perf_pmu_alias *newalias,
290 struct list_head *alist)
292 struct perf_pmu_alias *a;
294 list_for_each_entry(a, alist, list) {
295 if (!strcasecmp(newalias->name, a->name)) {
296 perf_pmu_update_alias(a, newalias);
297 perf_pmu_free_alias(newalias);
304 static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name,
305 char *desc, char *val,
306 char *long_desc, char *topic,
307 char *unit, char *perpkg,
311 struct parse_events_term *term;
312 struct perf_pmu_alias *alias;
317 alias = malloc(sizeof(*alias));
321 INIT_LIST_HEAD(&alias->terms);
323 alias->unit[0] = '\0';
324 alias->per_pkg = false;
325 alias->snapshot = false;
327 ret = parse_events_terms(&alias->terms, val);
329 pr_err("Cannot parse alias %s: %d\n", val, ret);
334 /* Scan event and remove leading zeroes, spaces, newlines, some
335 * platforms have terms specified as
336 * event=0x0091 (read from files ../<PMU>/events/<FILE>
337 * and terms specified as event=0x91 (read from JSON files).
339 * Rebuild string to make alias->str member comparable.
341 memset(newval, 0, sizeof(newval));
343 list_for_each_entry(term, &alias->terms, list) {
345 ret += scnprintf(newval + ret, sizeof(newval) - ret,
347 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM)
348 ret += scnprintf(newval + ret, sizeof(newval) - ret,
349 "%s=%#x", term->config, term->val.num);
350 else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
351 ret += scnprintf(newval + ret, sizeof(newval) - ret,
352 "%s=%s", term->config, term->val.str);
355 alias->name = strdup(name);
358 * load unit name and scale if available
360 perf_pmu__parse_unit(alias, dir, name);
361 perf_pmu__parse_scale(alias, dir, name);
362 perf_pmu__parse_per_pkg(alias, dir, name);
363 perf_pmu__parse_snapshot(alias, dir, name);
366 alias->metric_expr = metric_expr ? strdup(metric_expr) : NULL;
367 alias->metric_name = metric_name ? strdup(metric_name): NULL;
368 alias->desc = desc ? strdup(desc) : NULL;
369 alias->long_desc = long_desc ? strdup(long_desc) :
370 desc ? strdup(desc) : NULL;
371 alias->topic = topic ? strdup(topic) : NULL;
373 if (convert_scale(unit, &unit, &alias->scale) < 0)
375 snprintf(alias->unit, sizeof(alias->unit), "%s", unit);
377 alias->per_pkg = perpkg && sscanf(perpkg, "%d", &num) == 1 && num == 1;
378 alias->str = strdup(newval);
380 if (!perf_pmu_merge_alias(alias, list))
381 list_add_tail(&alias->list, list);
386 static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file)
391 ret = fread(buf, 1, sizeof(buf), file);
397 /* Remove trailing newline from sysfs file */
400 return __perf_pmu__new_alias(list, dir, name, NULL, buf, NULL, NULL, NULL,
404 static inline bool pmu_alias_info_file(char *name)
409 if (len > 5 && !strcmp(name + len - 5, ".unit"))
411 if (len > 6 && !strcmp(name + len - 6, ".scale"))
413 if (len > 8 && !strcmp(name + len - 8, ".per-pkg"))
415 if (len > 9 && !strcmp(name + len - 9, ".snapshot"))
422 * Process all the sysfs attributes located under the directory
423 * specified in 'dir' parameter.
425 static int pmu_aliases_parse(char *dir, struct list_head *head)
427 struct dirent *evt_ent;
430 event_dir = opendir(dir);
434 while ((evt_ent = readdir(event_dir))) {
436 char *name = evt_ent->d_name;
439 if (!strcmp(name, ".") || !strcmp(name, ".."))
443 * skip info files parsed in perf_pmu__new_alias()
445 if (pmu_alias_info_file(name))
448 scnprintf(path, PATH_MAX, "%s/%s", dir, name);
450 file = fopen(path, "r");
452 pr_debug("Cannot open %s\n", path);
456 if (perf_pmu__new_alias(head, dir, name, file) < 0)
457 pr_debug("Cannot set up %s\n", name);
466 * Reading the pmu event aliases definition, which should be located at:
467 * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
469 static int pmu_aliases(const char *name, struct list_head *head)
473 const char *sysfs = sysfs__mountpoint();
478 snprintf(path, PATH_MAX,
479 "%s/bus/event_source/devices/%s/events", sysfs, name);
481 if (stat(path, &st) < 0)
482 return 0; /* no error if 'events' does not exist */
484 if (pmu_aliases_parse(path, head))
490 static int pmu_alias_terms(struct perf_pmu_alias *alias,
491 struct list_head *terms)
493 struct parse_events_term *term, *cloned;
497 list_for_each_entry(term, &alias->terms, list) {
498 ret = parse_events_term__clone(&cloned, term);
500 parse_events_terms__purge(&list);
504 * Weak terms don't override command line options,
505 * which we don't want for implicit terms in aliases.
508 list_add_tail(&cloned->list, &list);
510 list_splice(&list, terms);
515 * Reading/parsing the default pmu type value, which should be
517 * /sys/bus/event_source/devices/<dev>/type as sysfs attribute.
519 static int pmu_type(const char *name, __u32 *type)
525 const char *sysfs = sysfs__mountpoint();
530 snprintf(path, PATH_MAX,
531 "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name);
533 if (stat(path, &st) < 0)
536 file = fopen(path, "r");
540 if (1 != fscanf(file, "%u", type))
547 /* Add all pmus in sysfs to pmu list: */
548 static void pmu_read_sysfs(void)
553 const char *sysfs = sysfs__mountpoint();
558 snprintf(path, PATH_MAX,
559 "%s" EVENT_SOURCE_DEVICE_PATH, sysfs);
565 while ((dent = readdir(dir))) {
566 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
568 /* add to static LIST_HEAD(pmus): */
569 perf_pmu__find(dent->d_name);
575 static struct cpu_map *__pmu_cpumask(const char *path)
578 struct cpu_map *cpus;
580 file = fopen(path, "r");
584 cpus = cpu_map__read(file);
590 * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64)
591 * may have a "cpus" file.
593 #define CPUS_TEMPLATE_UNCORE "%s/bus/event_source/devices/%s/cpumask"
594 #define CPUS_TEMPLATE_CPU "%s/bus/event_source/devices/%s/cpus"
596 static struct cpu_map *pmu_cpumask(const char *name)
599 struct cpu_map *cpus;
600 const char *sysfs = sysfs__mountpoint();
601 const char *templates[] = {
602 CPUS_TEMPLATE_UNCORE,
606 const char **template;
611 for (template = templates; *template; template++) {
612 snprintf(path, PATH_MAX, *template, sysfs, name);
613 cpus = __pmu_cpumask(path);
621 static bool pmu_is_uncore(const char *name)
624 struct cpu_map *cpus;
625 const char *sysfs = sysfs__mountpoint();
627 snprintf(path, PATH_MAX, CPUS_TEMPLATE_UNCORE, sysfs, name);
628 cpus = __pmu_cpumask(path);
635 * PMU CORE devices have different name other than cpu in sysfs on some
637 * Looking for possible sysfs files to identify the arm core device.
639 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 if (stat(path, &st) == 0)
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 static 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 static void pmu_add_cpu_aliases(struct list_head *head, struct perf_pmu *pmu)
752 struct pmu_events_map *map;
753 const char *name = pmu->name;
755 map = perf_pmu__find_map(pmu);
760 * Found a matching PMU events table. Create aliases
764 const char *cpu_name = is_arm_pmu_core(name) ? name : "cpu";
765 struct pmu_event *pe = &map->table[i++];
766 const char *pname = pe->pmu ? pe->pmu : cpu_name;
769 if (pe->metric_group || pe->metric_name)
774 if (pmu_is_uncore(name) &&
775 pmu_uncore_alias_match(pname, name))
778 if (strcmp(pname, name))
782 /* need type casts to override 'const' */
783 __perf_pmu__new_alias(head, NULL, (char *)pe->name,
784 (char *)pe->desc, (char *)pe->event,
785 (char *)pe->long_desc, (char *)pe->topic,
786 (char *)pe->unit, (char *)pe->perpkg,
787 (char *)pe->metric_expr,
788 (char *)pe->metric_name);
792 struct perf_event_attr * __weak
793 perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
798 static int pmu_max_precise(const char *name)
801 int max_precise = -1;
803 scnprintf(path, PATH_MAX,
804 "bus/event_source/devices/%s/caps/max_precise",
807 sysfs__read_int(path, &max_precise);
811 static struct perf_pmu *pmu_lookup(const char *name)
813 struct perf_pmu *pmu;
819 * The pmu data we store & need consists of the pmu
820 * type value and format definitions. Load both right
823 if (pmu_format(name, &format))
827 * Check the type first to avoid unnecessary work.
829 if (pmu_type(name, &type))
832 if (pmu_aliases(name, &aliases))
835 pmu = zalloc(sizeof(*pmu));
839 pmu->cpus = pmu_cpumask(name);
840 pmu->name = strdup(name);
842 pmu->is_uncore = pmu_is_uncore(name);
843 pmu->max_precise = pmu_max_precise(name);
844 pmu_add_cpu_aliases(&aliases, pmu);
846 INIT_LIST_HEAD(&pmu->format);
847 INIT_LIST_HEAD(&pmu->aliases);
848 list_splice(&format, &pmu->format);
849 list_splice(&aliases, &pmu->aliases);
850 list_add_tail(&pmu->list, &pmus);
852 pmu->default_config = perf_pmu__get_default_config(pmu);
857 static struct perf_pmu *pmu_find(const char *name)
859 struct perf_pmu *pmu;
861 list_for_each_entry(pmu, &pmus, list)
862 if (!strcmp(pmu->name, name))
868 struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
871 * pmu iterator: If pmu is NULL, we start at the begin,
872 * otherwise return the next pmu. Returns NULL on end.
876 pmu = list_prepare_entry(pmu, &pmus, list);
878 list_for_each_entry_continue(pmu, &pmus, list)
883 struct perf_pmu *perf_pmu__find(const char *name)
885 struct perf_pmu *pmu;
888 * Once PMU is loaded it stays in the list,
889 * so we keep us from multiple reading/parsing
890 * the pmu format definitions.
892 pmu = pmu_find(name);
896 return pmu_lookup(name);
899 static struct perf_pmu_format *
900 pmu_find_format(struct list_head *formats, const char *name)
902 struct perf_pmu_format *format;
904 list_for_each_entry(format, formats, list)
905 if (!strcmp(format->name, name))
911 __u64 perf_pmu__format_bits(struct list_head *formats, const char *name)
913 struct perf_pmu_format *format = pmu_find_format(formats, name);
920 for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
921 bits |= 1ULL << fbit;
927 * Sets value based on the format definition (format parameter)
928 * and unformated value (value parameter).
930 static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
933 unsigned long fbit, vbit;
935 for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
937 if (!test_bit(fbit, format))
940 if (value & (1llu << vbit++))
941 *v |= (1llu << fbit);
943 *v &= ~(1llu << fbit);
947 static __u64 pmu_format_max_value(const unsigned long *format)
951 w = bitmap_weight(format, PERF_PMU_FORMAT_BITS);
955 return (1ULL << w) - 1;
960 * Term is a string term, and might be a param-term. Try to look up it's value
961 * in the remaining terms.
962 * - We have a term like "base-or-format-term=param-term",
963 * - We need to find the value supplied for "param-term" (with param-term named
964 * in a config string) later on in the term list.
966 static int pmu_resolve_param_term(struct parse_events_term *term,
967 struct list_head *head_terms,
970 struct parse_events_term *t;
972 list_for_each_entry(t, head_terms, list) {
973 if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
974 if (!strcmp(t->config, term->config)) {
983 printf("Required parameter '%s' not specified\n", term->config);
988 static char *pmu_formats_string(struct list_head *formats)
990 struct perf_pmu_format *format;
992 struct strbuf buf = STRBUF_INIT;
998 /* sysfs exported terms */
999 list_for_each_entry(format, formats, list)
1000 if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
1003 str = strbuf_detach(&buf, NULL);
1005 strbuf_release(&buf);
1011 * Setup one of config[12] attr members based on the
1012 * user input data - term parameter.
1014 static int pmu_config_term(struct list_head *formats,
1015 struct perf_event_attr *attr,
1016 struct parse_events_term *term,
1017 struct list_head *head_terms,
1018 bool zero, struct parse_events_error *err)
1020 struct perf_pmu_format *format;
1025 * If this is a parameter we've already used for parameterized-eval,
1026 * skip it in normal eval.
1032 * Hardcoded terms should be already in, so nothing
1033 * to be done for them.
1035 if (parse_events__is_hardcoded_term(term))
1038 format = pmu_find_format(formats, term->config);
1041 printf("Invalid event/parameter '%s'\n", term->config);
1043 char *pmu_term = pmu_formats_string(formats);
1045 err->idx = term->err_term;
1046 err->str = strdup("unknown term");
1047 err->help = parse_events_formats_error_string(pmu_term);
1053 switch (format->value) {
1054 case PERF_PMU_FORMAT_VALUE_CONFIG:
1057 case PERF_PMU_FORMAT_VALUE_CONFIG1:
1058 vp = &attr->config1;
1060 case PERF_PMU_FORMAT_VALUE_CONFIG2:
1061 vp = &attr->config2;
1068 * Either directly use a numeric term, or try to translate string terms
1069 * using event parameters.
1071 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1072 if (term->no_value &&
1073 bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) {
1075 err->idx = term->err_val;
1076 err->str = strdup("no value assigned for term");
1081 val = term->val.num;
1082 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1083 if (strcmp(term->val.str, "?")) {
1085 pr_info("Invalid sysfs entry %s=%s\n",
1086 term->config, term->val.str);
1089 err->idx = term->err_val;
1090 err->str = strdup("expected numeric value");
1095 if (pmu_resolve_param_term(term, head_terms, &val))
1100 max_val = pmu_format_max_value(format->bits);
1101 if (val > max_val) {
1103 err->idx = term->err_val;
1104 if (asprintf(&err->str,
1105 "value too big for format, maximum is %llu",
1106 (unsigned long long)max_val) < 0)
1107 err->str = strdup("value too big for format");
1111 * Assume we don't care if !err, in which case the value will be
1112 * silently truncated.
1116 pmu_format_value(format->bits, val, vp, zero);
1120 int perf_pmu__config_terms(struct list_head *formats,
1121 struct perf_event_attr *attr,
1122 struct list_head *head_terms,
1123 bool zero, struct parse_events_error *err)
1125 struct parse_events_term *term;
1127 list_for_each_entry(term, head_terms, list) {
1128 if (pmu_config_term(formats, attr, term, head_terms,
1137 * Configures event's 'attr' parameter based on the:
1138 * 1) users input - specified in terms parameter
1139 * 2) pmu format definitions - specified by pmu parameter
1141 int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
1142 struct list_head *head_terms,
1143 struct parse_events_error *err)
1145 bool zero = !!pmu->default_config;
1147 attr->type = pmu->type;
1148 return perf_pmu__config_terms(&pmu->format, attr, head_terms,
1152 static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
1153 struct parse_events_term *term)
1155 struct perf_pmu_alias *alias;
1158 if (parse_events__is_hardcoded_term(term))
1161 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1162 if (term->val.num != 1)
1164 if (pmu_find_format(&pmu->format, term->config))
1166 name = term->config;
1167 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1168 if (strcasecmp(term->config, "event"))
1170 name = term->val.str;
1175 list_for_each_entry(alias, &pmu->aliases, list) {
1176 if (!strcasecmp(alias->name, name))
1183 static int check_info_data(struct perf_pmu_alias *alias,
1184 struct perf_pmu_info *info)
1187 * Only one term in event definition can
1188 * define unit, scale and snapshot, fail
1189 * if there's more than one.
1191 if ((info->unit && alias->unit[0]) ||
1192 (info->scale && alias->scale) ||
1193 (info->snapshot && alias->snapshot))
1197 info->unit = alias->unit;
1200 info->scale = alias->scale;
1202 if (alias->snapshot)
1203 info->snapshot = alias->snapshot;
1209 * Find alias in the terms list and replace it with the terms
1210 * defined for the alias
1212 int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
1213 struct perf_pmu_info *info)
1215 struct parse_events_term *term, *h;
1216 struct perf_pmu_alias *alias;
1219 info->per_pkg = false;
1222 * Mark unit and scale as not set
1223 * (different from default values, see below)
1227 info->snapshot = false;
1228 info->metric_expr = NULL;
1229 info->metric_name = NULL;
1231 list_for_each_entry_safe(term, h, head_terms, list) {
1232 alias = pmu_find_alias(pmu, term);
1235 ret = pmu_alias_terms(alias, &term->list);
1239 ret = check_info_data(alias, info);
1244 info->per_pkg = true;
1245 info->metric_expr = alias->metric_expr;
1246 info->metric_name = alias->metric_name;
1248 list_del(&term->list);
1253 * if no unit or scale foundin aliases, then
1254 * set defaults as for evsel
1255 * unit cannot left to NULL
1257 if (info->unit == NULL)
1260 if (info->scale == 0.0)
1266 int perf_pmu__new_format(struct list_head *list, char *name,
1267 int config, unsigned long *bits)
1269 struct perf_pmu_format *format;
1271 format = zalloc(sizeof(*format));
1275 format->name = strdup(name);
1276 format->value = config;
1277 memcpy(format->bits, bits, sizeof(format->bits));
1279 list_add_tail(&format->list, list);
1283 void perf_pmu__set_format(unsigned long *bits, long from, long to)
1290 memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
1291 for (b = from; b <= to; b++)
1295 static int sub_non_neg(int a, int b)
1302 static char *format_alias(char *buf, int len, struct perf_pmu *pmu,
1303 struct perf_pmu_alias *alias)
1305 struct parse_events_term *term;
1306 int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name);
1308 list_for_each_entry(term, &alias->terms, list) {
1309 if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
1310 used += snprintf(buf + used, sub_non_neg(len, used),
1311 ",%s=%s", term->config,
1315 if (sub_non_neg(len, used) > 0) {
1319 if (sub_non_neg(len, used) > 0) {
1323 buf[len - 1] = '\0';
1328 static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu,
1329 struct perf_pmu_alias *alias)
1331 snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name);
1345 static int cmp_sevent(const void *a, const void *b)
1347 const struct sevent *as = a;
1348 const struct sevent *bs = b;
1350 /* Put extra events last */
1351 if (!!as->desc != !!bs->desc)
1352 return !!as->desc - !!bs->desc;
1353 if (as->topic && bs->topic) {
1354 int n = strcmp(as->topic, bs->topic);
1359 return strcmp(as->name, bs->name);
1362 static void wordwrap(char *s, int start, int max, int corr)
1368 int wlen = strcspn(s, " \t");
1370 if (column + wlen >= max && column > start) {
1371 printf("\n%*s", start, "");
1372 column = start + corr;
1374 n = printf("%s%.*s", column > start ? " " : "", wlen, s);
1383 void print_pmu_events(const char *event_glob, bool name_only, bool quiet_flag,
1384 bool long_desc, bool details_flag)
1386 struct perf_pmu *pmu;
1387 struct perf_pmu_alias *alias;
1391 struct sevent *aliases;
1393 int columns = pager_get_columns();
1398 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1399 list_for_each_entry(alias, &pmu->aliases, list)
1401 if (pmu->selectable)
1404 aliases = zalloc(sizeof(struct sevent) * len);
1409 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1410 list_for_each_entry(alias, &pmu->aliases, list) {
1411 char *name = alias->desc ? alias->name :
1412 format_alias(buf, sizeof(buf), pmu, alias);
1413 bool is_cpu = !strcmp(pmu->name, "cpu");
1415 if (event_glob != NULL &&
1416 !(strglobmatch_nocase(name, event_glob) ||
1417 (!is_cpu && strglobmatch_nocase(alias->name,
1420 strglobmatch_nocase(alias->topic, event_glob))))
1423 if (is_cpu && !name_only && !alias->desc)
1424 name = format_alias_or(buf, sizeof(buf), pmu, alias);
1426 aliases[j].name = name;
1427 if (is_cpu && !name_only && !alias->desc)
1428 aliases[j].name = format_alias_or(buf,
1431 aliases[j].name = strdup(aliases[j].name);
1432 if (!aliases[j].name)
1435 aliases[j].desc = long_desc ? alias->long_desc :
1437 aliases[j].topic = alias->topic;
1438 aliases[j].str = alias->str;
1439 aliases[j].pmu = pmu->name;
1440 aliases[j].metric_expr = alias->metric_expr;
1441 aliases[j].metric_name = alias->metric_name;
1444 if (pmu->selectable &&
1445 (event_glob == NULL || strglobmatch(pmu->name, event_glob))) {
1447 if (asprintf(&s, "%s//", pmu->name) < 0)
1449 aliases[j].name = s;
1454 qsort(aliases, len, sizeof(struct sevent), cmp_sevent);
1455 for (j = 0; j < len; j++) {
1456 /* Skip duplicates */
1457 if (j > 0 && !strcmp(aliases[j].name, aliases[j - 1].name))
1460 printf("%s ", aliases[j].name);
1463 if (aliases[j].desc && !quiet_flag) {
1466 if (aliases[j].topic && (!topic ||
1467 strcmp(topic, aliases[j].topic))) {
1468 printf("%s%s:\n", topic ? "\n" : "",
1470 topic = aliases[j].topic;
1472 printf(" %-50s\n", aliases[j].name);
1473 printf("%*s", 8, "[");
1474 wordwrap(aliases[j].desc, 8, columns, 0);
1477 printf("%*s%s/%s/ ", 8, "", aliases[j].pmu, aliases[j].str);
1478 if (aliases[j].metric_name)
1479 printf(" MetricName: %s", aliases[j].metric_name);
1480 if (aliases[j].metric_expr)
1481 printf(" MetricExpr: %s", aliases[j].metric_expr);
1485 printf(" %-50s [Kernel PMU event]\n", aliases[j].name);
1488 if (printed && pager_in_use())
1491 for (j = 0; j < len; j++)
1492 zfree(&aliases[j].name);
1497 printf("FATAL: not enough memory to print PMU events\n");
1502 bool pmu_have_event(const char *pname, const char *name)
1504 struct perf_pmu *pmu;
1505 struct perf_pmu_alias *alias;
1508 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1509 if (strcmp(pname, pmu->name))
1511 list_for_each_entry(alias, &pmu->aliases, list)
1512 if (!strcmp(alias->name, name))
1518 static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
1521 char path[PATH_MAX];
1524 sysfs = sysfs__mountpoint();
1528 snprintf(path, PATH_MAX,
1529 "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name);
1531 if (stat(path, &st) < 0)
1534 return fopen(path, "r");
1537 int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
1544 va_start(args, fmt);
1545 file = perf_pmu__open_file(pmu, name);
1547 ret = vfscanf(file, fmt, args);