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"
28 #include "pmu-hybrid.h"
30 struct perf_pmu perf_pmu__fake;
32 struct perf_pmu_format {
35 DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
36 struct list_head list;
39 int perf_pmu_parse(struct list_head *list, char *name);
40 extern FILE *perf_pmu_in;
42 static LIST_HEAD(pmus);
43 static bool hybrid_scanned;
46 * Parse & process all the sysfs attributes located under
47 * the directory specified in 'dir' parameter.
49 int perf_pmu__format_parse(char *dir, struct list_head *head)
51 struct dirent *evt_ent;
55 format_dir = opendir(dir);
59 while (!ret && (evt_ent = readdir(format_dir))) {
61 char *name = evt_ent->d_name;
64 if (!strcmp(name, ".") || !strcmp(name, ".."))
67 snprintf(path, PATH_MAX, "%s/%s", dir, name);
70 file = fopen(path, "r");
75 ret = perf_pmu_parse(head, name);
84 * Reading/parsing the default pmu format definition, which should be
86 * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
88 static int pmu_format(const char *name, struct list_head *format)
91 const char *sysfs = sysfs__mountpoint();
96 snprintf(path, PATH_MAX,
97 "%s" EVENT_SOURCE_DEVICE_PATH "%s/format", sysfs, name);
99 if (!file_available(path))
102 if (perf_pmu__format_parse(path, format))
108 int perf_pmu__convert_scale(const char *scale, char **end, double *sval)
114 * save current locale
116 lc = setlocale(LC_NUMERIC, NULL);
119 * The lc string may be allocated in static storage,
120 * so get a dynamic copy to make it survive setlocale
130 * force to C locale to ensure kernel
131 * scale string is converted correctly.
132 * kernel uses default C locale.
134 setlocale(LC_NUMERIC, "C");
136 *sval = strtod(scale, end);
140 setlocale(LC_NUMERIC, lc);
145 static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *name)
153 scnprintf(path, PATH_MAX, "%s/%s.scale", dir, name);
155 fd = open(path, O_RDONLY);
159 if (fstat(fd, &st) < 0)
162 sret = read(fd, scale, sizeof(scale)-1);
166 if (scale[sret - 1] == '\n')
167 scale[sret - 1] = '\0';
171 ret = perf_pmu__convert_scale(scale, NULL, &alias->scale);
177 static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *name)
183 scnprintf(path, PATH_MAX, "%s/%s.unit", dir, name);
185 fd = open(path, O_RDONLY);
189 sret = read(fd, alias->unit, UNIT_MAX_LEN);
195 if (alias->unit[sret - 1] == '\n')
196 alias->unit[sret - 1] = '\0';
198 alias->unit[sret] = '\0';
203 alias->unit[0] = '\0';
208 perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, char *dir, char *name)
213 scnprintf(path, PATH_MAX, "%s/%s.per-pkg", dir, name);
215 fd = open(path, O_RDONLY);
221 alias->per_pkg = true;
225 static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias,
226 char *dir, char *name)
231 scnprintf(path, PATH_MAX, "%s/%s.snapshot", dir, name);
233 fd = open(path, O_RDONLY);
237 alias->snapshot = true;
242 static void perf_pmu_assign_str(char *name, const char *field, char **old_str,
248 if (*new_str) { /* Have new string, check with old */
249 if (strcasecmp(*old_str, *new_str))
250 pr_debug("alias %s differs in field '%s'\n",
253 } else /* Nothing new --> keep old string */
260 static void perf_pmu_update_alias(struct perf_pmu_alias *old,
261 struct perf_pmu_alias *newalias)
263 perf_pmu_assign_str(old->name, "desc", &old->desc, &newalias->desc);
264 perf_pmu_assign_str(old->name, "long_desc", &old->long_desc,
265 &newalias->long_desc);
266 perf_pmu_assign_str(old->name, "topic", &old->topic, &newalias->topic);
267 perf_pmu_assign_str(old->name, "metric_expr", &old->metric_expr,
268 &newalias->metric_expr);
269 perf_pmu_assign_str(old->name, "metric_name", &old->metric_name,
270 &newalias->metric_name);
271 perf_pmu_assign_str(old->name, "value", &old->str, &newalias->str);
272 old->scale = newalias->scale;
273 old->per_pkg = newalias->per_pkg;
274 old->snapshot = newalias->snapshot;
275 memcpy(old->unit, newalias->unit, sizeof(old->unit));
278 /* Delete an alias entry. */
279 void perf_pmu_free_alias(struct perf_pmu_alias *newalias)
281 zfree(&newalias->name);
282 zfree(&newalias->desc);
283 zfree(&newalias->long_desc);
284 zfree(&newalias->topic);
285 zfree(&newalias->str);
286 zfree(&newalias->metric_expr);
287 zfree(&newalias->metric_name);
288 zfree(&newalias->pmu_name);
289 parse_events_terms__purge(&newalias->terms);
293 /* Merge an alias, search in alias list. If this name is already
294 * present merge both of them to combine all information.
296 static bool perf_pmu_merge_alias(struct perf_pmu_alias *newalias,
297 struct list_head *alist)
299 struct perf_pmu_alias *a;
301 list_for_each_entry(a, alist, list) {
302 if (!strcasecmp(newalias->name, a->name)) {
303 if (newalias->pmu_name && a->pmu_name &&
304 !strcasecmp(newalias->pmu_name, a->pmu_name)) {
307 perf_pmu_update_alias(a, newalias);
308 perf_pmu_free_alias(newalias);
315 static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name,
316 char *desc, char *val, struct pmu_event *pe)
318 struct parse_events_term *term;
319 struct perf_pmu_alias *alias;
323 char *long_desc = NULL, *topic = NULL, *unit = NULL, *perpkg = NULL,
324 *metric_expr = NULL, *metric_name = NULL, *deprecated = NULL,
328 long_desc = (char *)pe->long_desc;
329 topic = (char *)pe->topic;
330 unit = (char *)pe->unit;
331 perpkg = (char *)pe->perpkg;
332 metric_expr = (char *)pe->metric_expr;
333 metric_name = (char *)pe->metric_name;
334 deprecated = (char *)pe->deprecated;
335 pmu_name = (char *)pe->pmu;
338 alias = malloc(sizeof(*alias));
342 INIT_LIST_HEAD(&alias->terms);
344 alias->unit[0] = '\0';
345 alias->per_pkg = false;
346 alias->snapshot = false;
347 alias->deprecated = false;
349 ret = parse_events_terms(&alias->terms, val);
351 pr_err("Cannot parse alias %s: %d\n", val, ret);
356 /* Scan event and remove leading zeroes, spaces, newlines, some
357 * platforms have terms specified as
358 * event=0x0091 (read from files ../<PMU>/events/<FILE>
359 * and terms specified as event=0x91 (read from JSON files).
361 * Rebuild string to make alias->str member comparable.
363 memset(newval, 0, sizeof(newval));
365 list_for_each_entry(term, &alias->terms, list) {
367 ret += scnprintf(newval + ret, sizeof(newval) - ret,
369 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM)
370 ret += scnprintf(newval + ret, sizeof(newval) - ret,
371 "%s=%#x", term->config, term->val.num);
372 else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
373 ret += scnprintf(newval + ret, sizeof(newval) - ret,
374 "%s=%s", term->config, term->val.str);
377 alias->name = strdup(name);
380 * load unit name and scale if available
382 perf_pmu__parse_unit(alias, dir, name);
383 perf_pmu__parse_scale(alias, dir, name);
384 perf_pmu__parse_per_pkg(alias, dir, name);
385 perf_pmu__parse_snapshot(alias, dir, name);
388 alias->metric_expr = metric_expr ? strdup(metric_expr) : NULL;
389 alias->metric_name = metric_name ? strdup(metric_name): NULL;
390 alias->desc = desc ? strdup(desc) : NULL;
391 alias->long_desc = long_desc ? strdup(long_desc) :
392 desc ? strdup(desc) : NULL;
393 alias->topic = topic ? strdup(topic) : NULL;
395 if (perf_pmu__convert_scale(unit, &unit, &alias->scale) < 0)
397 snprintf(alias->unit, sizeof(alias->unit), "%s", unit);
399 alias->per_pkg = perpkg && sscanf(perpkg, "%d", &num) == 1 && num == 1;
400 alias->str = strdup(newval);
401 alias->pmu_name = pmu_name ? strdup(pmu_name) : NULL;
404 alias->deprecated = true;
406 if (!perf_pmu_merge_alias(alias, list))
407 list_add_tail(&alias->list, list);
412 static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file)
417 ret = fread(buf, 1, sizeof(buf), file);
423 /* Remove trailing newline from sysfs file */
426 return __perf_pmu__new_alias(list, dir, name, NULL, buf, NULL);
429 static inline bool pmu_alias_info_file(char *name)
434 if (len > 5 && !strcmp(name + len - 5, ".unit"))
436 if (len > 6 && !strcmp(name + len - 6, ".scale"))
438 if (len > 8 && !strcmp(name + len - 8, ".per-pkg"))
440 if (len > 9 && !strcmp(name + len - 9, ".snapshot"))
447 * Process all the sysfs attributes located under the directory
448 * specified in 'dir' parameter.
450 static int pmu_aliases_parse(char *dir, struct list_head *head)
452 struct dirent *evt_ent;
455 event_dir = opendir(dir);
459 while ((evt_ent = readdir(event_dir))) {
461 char *name = evt_ent->d_name;
464 if (!strcmp(name, ".") || !strcmp(name, ".."))
468 * skip info files parsed in perf_pmu__new_alias()
470 if (pmu_alias_info_file(name))
473 scnprintf(path, PATH_MAX, "%s/%s", dir, name);
475 file = fopen(path, "r");
477 pr_debug("Cannot open %s\n", path);
481 if (perf_pmu__new_alias(head, dir, name, file) < 0)
482 pr_debug("Cannot set up %s\n", name);
491 * Reading the pmu event aliases definition, which should be located at:
492 * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
494 static int pmu_aliases(const char *name, struct list_head *head)
497 const char *sysfs = sysfs__mountpoint();
502 snprintf(path, PATH_MAX,
503 "%s/bus/event_source/devices/%s/events", sysfs, name);
505 if (!file_available(path))
508 if (pmu_aliases_parse(path, head))
514 static int pmu_alias_terms(struct perf_pmu_alias *alias,
515 struct list_head *terms)
517 struct parse_events_term *term, *cloned;
521 list_for_each_entry(term, &alias->terms, list) {
522 ret = parse_events_term__clone(&cloned, term);
524 parse_events_terms__purge(&list);
528 * Weak terms don't override command line options,
529 * which we don't want for implicit terms in aliases.
532 list_add_tail(&cloned->list, &list);
534 list_splice(&list, terms);
539 * Reading/parsing the default pmu type value, which should be
541 * /sys/bus/event_source/devices/<dev>/type as sysfs attribute.
543 static int pmu_type(const char *name, __u32 *type)
548 const char *sysfs = sysfs__mountpoint();
553 snprintf(path, PATH_MAX,
554 "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name);
556 if (access(path, R_OK) < 0)
559 file = fopen(path, "r");
563 if (1 != fscanf(file, "%u", type))
570 /* Add all pmus in sysfs to pmu list: */
571 static void pmu_read_sysfs(void)
576 const char *sysfs = sysfs__mountpoint();
581 snprintf(path, PATH_MAX,
582 "%s" EVENT_SOURCE_DEVICE_PATH, sysfs);
588 while ((dent = readdir(dir))) {
589 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
591 /* add to static LIST_HEAD(pmus): */
592 perf_pmu__find(dent->d_name);
598 static struct perf_cpu_map *__pmu_cpumask(const char *path)
601 struct perf_cpu_map *cpus;
603 file = fopen(path, "r");
607 cpus = perf_cpu_map__read(file);
613 * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64)
614 * may have a "cpus" file.
616 #define SYS_TEMPLATE_ID "./bus/event_source/devices/%s/identifier"
617 #define CPUS_TEMPLATE_UNCORE "%s/bus/event_source/devices/%s/cpumask"
619 static struct perf_cpu_map *pmu_cpumask(const char *name)
622 struct perf_cpu_map *cpus;
623 const char *sysfs = sysfs__mountpoint();
624 const char *templates[] = {
625 CPUS_TEMPLATE_UNCORE,
629 const char **template;
634 for (template = templates; *template; template++) {
635 snprintf(path, PATH_MAX, *template, sysfs, name);
636 cpus = __pmu_cpumask(path);
644 static bool pmu_is_uncore(const char *name)
649 if (perf_pmu__hybrid_mounted(name))
652 sysfs = sysfs__mountpoint();
653 snprintf(path, PATH_MAX, CPUS_TEMPLATE_UNCORE, sysfs, name);
654 return file_available(path);
657 static char *pmu_id(const char *name)
659 char path[PATH_MAX], *str;
662 snprintf(path, PATH_MAX, SYS_TEMPLATE_ID, name);
664 if (sysfs__read_str(path, &str, &len) < 0)
667 str[len - 1] = 0; /* remove line feed */
673 * PMU CORE devices have different name other than cpu in sysfs on some
675 * Looking for possible sysfs files to identify the arm core device.
677 static int is_arm_pmu_core(const char *name)
680 const char *sysfs = sysfs__mountpoint();
685 /* Look for cpu sysfs (specific to arm) */
686 scnprintf(path, PATH_MAX, "%s/bus/event_source/devices/%s/cpus",
688 return file_available(path);
691 static char *perf_pmu__getcpuid(struct perf_pmu *pmu)
696 cpuid = getenv("PERF_CPUID");
698 cpuid = strdup(cpuid);
700 cpuid = get_cpuid_str(pmu);
705 pr_debug("Using CPUID %s\n", cpuid);
711 struct pmu_events_map *perf_pmu__find_map(struct perf_pmu *pmu)
713 struct pmu_events_map *map;
714 char *cpuid = perf_pmu__getcpuid(pmu);
717 /* on some platforms which uses cpus map, cpuid can be NULL for
718 * PMUs other than CORE PMUs.
725 map = &pmu_events_map[i++];
731 if (!strcmp_cpuid_str(map->cpuid, cpuid))
738 struct pmu_events_map *__weak pmu_events_map__find(void)
740 return perf_pmu__find_map(NULL);
743 bool pmu_uncore_alias_match(const char *pmu_name, const char *name)
745 char *tmp = NULL, *tok, *str;
748 str = strdup(pmu_name);
753 * uncore alias may be from different PMU with common prefix
755 tok = strtok_r(str, ",", &tmp);
756 if (strncmp(pmu_name, tok, strlen(tok))) {
762 * Match more complex aliases where the alias name is a comma-delimited
763 * list of tokens, orderly contained in the matching PMU name.
765 * Example: For alias "socket,pmuname" and PMU "socketX_pmunameY", we
766 * match "socket" in "socketX_pmunameY" and then "pmuname" in
769 for (; tok; name += strlen(tok), tok = strtok_r(NULL, ",", &tmp)) {
770 name = strstr(name, tok);
784 * From the pmu_events_map, find the table of PMU events that corresponds
785 * to the current running CPU. Then, add all PMU events from that table
788 void pmu_add_cpu_aliases_map(struct list_head *head, struct perf_pmu *pmu,
789 struct pmu_events_map *map)
792 const char *name = pmu->name;
794 * Found a matching PMU events table. Create aliases
798 const char *cpu_name = is_arm_pmu_core(name) ? name : "cpu";
799 struct pmu_event *pe = &map->table[i++];
800 const char *pname = pe->pmu ? pe->pmu : cpu_name;
803 if (pe->metric_group || pe->metric_name)
808 if (pmu_is_uncore(name) &&
809 pmu_uncore_alias_match(pname, name))
812 if (strcmp(pname, name))
816 /* need type casts to override 'const' */
817 __perf_pmu__new_alias(head, NULL, (char *)pe->name,
818 (char *)pe->desc, (char *)pe->event,
823 static void pmu_add_cpu_aliases(struct list_head *head, struct perf_pmu *pmu)
825 struct pmu_events_map *map;
827 map = perf_pmu__find_map(pmu);
831 pmu_add_cpu_aliases_map(head, pmu, map);
834 void pmu_for_each_sys_event(pmu_sys_event_iter_fn fn, void *data)
839 struct pmu_sys_events *event_table;
842 event_table = &pmu_sys_event_tables[i++];
844 if (!event_table->table)
848 struct pmu_event *pe = &event_table->table[j++];
851 if (!pe->name && !pe->metric_group && !pe->metric_name)
861 struct pmu_sys_event_iter_data {
862 struct list_head *head;
863 struct perf_pmu *pmu;
866 static int pmu_add_sys_aliases_iter_fn(struct pmu_event *pe, void *data)
868 struct pmu_sys_event_iter_data *idata = data;
869 struct perf_pmu *pmu = idata->pmu;
872 if (pe->metric_group || pe->metric_name)
877 if (!pe->compat || !pe->pmu)
880 if (!strcmp(pmu->id, pe->compat) &&
881 pmu_uncore_alias_match(pe->pmu, pmu->name)) {
882 __perf_pmu__new_alias(idata->head, NULL,
892 static void pmu_add_sys_aliases(struct list_head *head, struct perf_pmu *pmu)
894 struct pmu_sys_event_iter_data idata = {
902 pmu_for_each_sys_event(pmu_add_sys_aliases_iter_fn, &idata);
905 struct perf_event_attr * __weak
906 perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
911 static int pmu_max_precise(const char *name)
914 int max_precise = -1;
916 scnprintf(path, PATH_MAX,
917 "bus/event_source/devices/%s/caps/max_precise",
920 sysfs__read_int(path, &max_precise);
924 static struct perf_pmu *pmu_lookup(const char *name)
926 struct perf_pmu *pmu;
932 * The pmu data we store & need consists of the pmu
933 * type value and format definitions. Load both right
936 if (pmu_format(name, &format))
940 * Check the type first to avoid unnecessary work.
942 if (pmu_type(name, &type))
945 if (pmu_aliases(name, &aliases))
948 pmu = zalloc(sizeof(*pmu));
952 pmu->cpus = pmu_cpumask(name);
953 pmu->name = strdup(name);
955 pmu->is_uncore = pmu_is_uncore(name);
957 pmu->id = pmu_id(name);
958 pmu->is_hybrid = perf_pmu__hybrid_mounted(name);
959 pmu->max_precise = pmu_max_precise(name);
960 pmu_add_cpu_aliases(&aliases, pmu);
961 pmu_add_sys_aliases(&aliases, pmu);
963 INIT_LIST_HEAD(&pmu->format);
964 INIT_LIST_HEAD(&pmu->aliases);
965 INIT_LIST_HEAD(&pmu->caps);
966 list_splice(&format, &pmu->format);
967 list_splice(&aliases, &pmu->aliases);
968 list_add_tail(&pmu->list, &pmus);
971 list_add_tail(&pmu->hybrid_list, &perf_pmu__hybrid_pmus);
973 pmu->default_config = perf_pmu__get_default_config(pmu);
978 static struct perf_pmu *pmu_find(const char *name)
980 struct perf_pmu *pmu;
982 list_for_each_entry(pmu, &pmus, list)
983 if (!strcmp(pmu->name, name))
989 struct perf_pmu *perf_pmu__find_by_type(unsigned int type)
991 struct perf_pmu *pmu;
993 list_for_each_entry(pmu, &pmus, list)
994 if (pmu->type == type)
1000 struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
1003 * pmu iterator: If pmu is NULL, we start at the begin,
1004 * otherwise return the next pmu. Returns NULL on end.
1008 pmu = list_prepare_entry(pmu, &pmus, list);
1010 list_for_each_entry_continue(pmu, &pmus, list)
1015 struct perf_pmu *evsel__find_pmu(struct evsel *evsel)
1017 struct perf_pmu *pmu = NULL;
1019 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1020 if (pmu->type == evsel->core.attr.type)
1027 bool evsel__is_aux_event(struct evsel *evsel)
1029 struct perf_pmu *pmu = evsel__find_pmu(evsel);
1031 return pmu && pmu->auxtrace;
1034 struct perf_pmu *perf_pmu__find(const char *name)
1036 struct perf_pmu *pmu;
1039 * Once PMU is loaded it stays in the list,
1040 * so we keep us from multiple reading/parsing
1041 * the pmu format definitions.
1043 pmu = pmu_find(name);
1047 return pmu_lookup(name);
1050 static struct perf_pmu_format *
1051 pmu_find_format(struct list_head *formats, const char *name)
1053 struct perf_pmu_format *format;
1055 list_for_each_entry(format, formats, list)
1056 if (!strcmp(format->name, name))
1062 __u64 perf_pmu__format_bits(struct list_head *formats, const char *name)
1064 struct perf_pmu_format *format = pmu_find_format(formats, name);
1071 for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
1072 bits |= 1ULL << fbit;
1077 int perf_pmu__format_type(struct list_head *formats, const char *name)
1079 struct perf_pmu_format *format = pmu_find_format(formats, name);
1084 return format->value;
1088 * Sets value based on the format definition (format parameter)
1089 * and unformatted value (value parameter).
1091 static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
1094 unsigned long fbit, vbit;
1096 for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
1098 if (!test_bit(fbit, format))
1101 if (value & (1llu << vbit++))
1102 *v |= (1llu << fbit);
1104 *v &= ~(1llu << fbit);
1108 static __u64 pmu_format_max_value(const unsigned long *format)
1112 w = bitmap_weight(format, PERF_PMU_FORMAT_BITS);
1116 return (1ULL << w) - 1;
1121 * Term is a string term, and might be a param-term. Try to look up it's value
1122 * in the remaining terms.
1123 * - We have a term like "base-or-format-term=param-term",
1124 * - We need to find the value supplied for "param-term" (with param-term named
1125 * in a config string) later on in the term list.
1127 static int pmu_resolve_param_term(struct parse_events_term *term,
1128 struct list_head *head_terms,
1131 struct parse_events_term *t;
1133 list_for_each_entry(t, head_terms, list) {
1134 if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM &&
1135 t->config && !strcmp(t->config, term->config)) {
1137 *value = t->val.num;
1143 printf("Required parameter '%s' not specified\n", term->config);
1148 static char *pmu_formats_string(struct list_head *formats)
1150 struct perf_pmu_format *format;
1152 struct strbuf buf = STRBUF_INIT;
1158 /* sysfs exported terms */
1159 list_for_each_entry(format, formats, list)
1160 if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
1163 str = strbuf_detach(&buf, NULL);
1165 strbuf_release(&buf);
1171 * Setup one of config[12] attr members based on the
1172 * user input data - term parameter.
1174 static int pmu_config_term(const char *pmu_name,
1175 struct list_head *formats,
1176 struct perf_event_attr *attr,
1177 struct parse_events_term *term,
1178 struct list_head *head_terms,
1179 bool zero, struct parse_events_error *err)
1181 struct perf_pmu_format *format;
1186 * If this is a parameter we've already used for parameterized-eval,
1187 * skip it in normal eval.
1193 * Hardcoded terms should be already in, so nothing
1194 * to be done for them.
1196 if (parse_events__is_hardcoded_term(term))
1199 format = pmu_find_format(formats, term->config);
1201 char *pmu_term = pmu_formats_string(formats);
1205 if (asprintf(&unknown_term,
1206 "unknown term '%s' for pmu '%s'",
1207 term->config, pmu_name) < 0)
1208 unknown_term = NULL;
1209 help_msg = parse_events_formats_error_string(pmu_term);
1211 parse_events__handle_error(err, term->err_term,
1215 pr_debug("%s (%s)\n", unknown_term, help_msg);
1222 switch (format->value) {
1223 case PERF_PMU_FORMAT_VALUE_CONFIG:
1226 case PERF_PMU_FORMAT_VALUE_CONFIG1:
1227 vp = &attr->config1;
1229 case PERF_PMU_FORMAT_VALUE_CONFIG2:
1230 vp = &attr->config2;
1237 * Either directly use a numeric term, or try to translate string terms
1238 * using event parameters.
1240 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1241 if (term->no_value &&
1242 bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) {
1244 parse_events__handle_error(err, term->err_val,
1245 strdup("no value assigned for term"),
1251 val = term->val.num;
1252 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1253 if (strcmp(term->val.str, "?")) {
1255 pr_info("Invalid sysfs entry %s=%s\n",
1256 term->config, term->val.str);
1259 parse_events__handle_error(err, term->err_val,
1260 strdup("expected numeric value"),
1266 if (pmu_resolve_param_term(term, head_terms, &val))
1271 max_val = pmu_format_max_value(format->bits);
1272 if (val > max_val) {
1276 parse_events__handle_error(err, term->err_val,
1278 "value too big for format, maximum is %llu",
1279 (unsigned long long)max_val) < 0
1280 ? strdup("value too big for format")
1286 * Assume we don't care if !err, in which case the value will be
1287 * silently truncated.
1291 pmu_format_value(format->bits, val, vp, zero);
1295 int perf_pmu__config_terms(const char *pmu_name, struct list_head *formats,
1296 struct perf_event_attr *attr,
1297 struct list_head *head_terms,
1298 bool zero, struct parse_events_error *err)
1300 struct parse_events_term *term;
1302 list_for_each_entry(term, head_terms, list) {
1303 if (pmu_config_term(pmu_name, formats, attr, term, head_terms,
1312 * Configures event's 'attr' parameter based on the:
1313 * 1) users input - specified in terms parameter
1314 * 2) pmu format definitions - specified by pmu parameter
1316 int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
1317 struct list_head *head_terms,
1318 struct parse_events_error *err)
1320 bool zero = !!pmu->default_config;
1322 attr->type = pmu->type;
1323 return perf_pmu__config_terms(pmu->name, &pmu->format, attr,
1324 head_terms, zero, err);
1327 static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
1328 struct parse_events_term *term)
1330 struct perf_pmu_alias *alias;
1333 if (parse_events__is_hardcoded_term(term))
1336 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1337 if (term->val.num != 1)
1339 if (pmu_find_format(&pmu->format, term->config))
1341 name = term->config;
1342 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1343 if (strcasecmp(term->config, "event"))
1345 name = term->val.str;
1350 list_for_each_entry(alias, &pmu->aliases, list) {
1351 if (!strcasecmp(alias->name, name))
1358 static int check_info_data(struct perf_pmu_alias *alias,
1359 struct perf_pmu_info *info)
1362 * Only one term in event definition can
1363 * define unit, scale and snapshot, fail
1364 * if there's more than one.
1366 if ((info->unit && alias->unit[0]) ||
1367 (info->scale && alias->scale) ||
1368 (info->snapshot && alias->snapshot))
1372 info->unit = alias->unit;
1375 info->scale = alias->scale;
1377 if (alias->snapshot)
1378 info->snapshot = alias->snapshot;
1384 * Find alias in the terms list and replace it with the terms
1385 * defined for the alias
1387 int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
1388 struct perf_pmu_info *info)
1390 struct parse_events_term *term, *h;
1391 struct perf_pmu_alias *alias;
1394 info->per_pkg = false;
1397 * Mark unit and scale as not set
1398 * (different from default values, see below)
1402 info->snapshot = false;
1403 info->metric_expr = NULL;
1404 info->metric_name = NULL;
1406 list_for_each_entry_safe(term, h, head_terms, list) {
1407 alias = pmu_find_alias(pmu, term);
1410 ret = pmu_alias_terms(alias, &term->list);
1414 ret = check_info_data(alias, info);
1419 info->per_pkg = true;
1420 info->metric_expr = alias->metric_expr;
1421 info->metric_name = alias->metric_name;
1423 list_del_init(&term->list);
1424 parse_events_term__delete(term);
1428 * if no unit or scale found in aliases, then
1429 * set defaults as for evsel
1430 * unit cannot left to NULL
1432 if (info->unit == NULL)
1435 if (info->scale == 0.0)
1441 int perf_pmu__new_format(struct list_head *list, char *name,
1442 int config, unsigned long *bits)
1444 struct perf_pmu_format *format;
1446 format = zalloc(sizeof(*format));
1450 format->name = strdup(name);
1451 format->value = config;
1452 memcpy(format->bits, bits, sizeof(format->bits));
1454 list_add_tail(&format->list, list);
1458 void perf_pmu__set_format(unsigned long *bits, long from, long to)
1465 memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
1466 for (b = from; b <= to; b++)
1470 void perf_pmu__del_formats(struct list_head *formats)
1472 struct perf_pmu_format *fmt, *tmp;
1474 list_for_each_entry_safe(fmt, tmp, formats, list) {
1475 list_del(&fmt->list);
1481 static int sub_non_neg(int a, int b)
1488 static char *format_alias(char *buf, int len, struct perf_pmu *pmu,
1489 struct perf_pmu_alias *alias)
1491 struct parse_events_term *term;
1492 int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name);
1494 list_for_each_entry(term, &alias->terms, list) {
1495 if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
1496 used += snprintf(buf + used, sub_non_neg(len, used),
1497 ",%s=%s", term->config,
1501 if (sub_non_neg(len, used) > 0) {
1505 if (sub_non_neg(len, used) > 0) {
1509 buf[len - 1] = '\0';
1514 static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu,
1515 struct perf_pmu_alias *alias)
1517 snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name);
1532 static int cmp_sevent(const void *a, const void *b)
1534 const struct sevent *as = a;
1535 const struct sevent *bs = b;
1537 /* Put extra events last */
1538 if (!!as->desc != !!bs->desc)
1539 return !!as->desc - !!bs->desc;
1540 if (as->topic && bs->topic) {
1541 int n = strcmp(as->topic, bs->topic);
1547 /* Order CPU core events to be first */
1548 if (as->is_cpu != bs->is_cpu)
1549 return bs->is_cpu - as->is_cpu;
1551 return strcmp(as->name, bs->name);
1554 static void wordwrap(char *s, int start, int max, int corr)
1560 int wlen = strcspn(s, " \t");
1562 if (column + wlen >= max && column > start) {
1563 printf("\n%*s", start, "");
1564 column = start + corr;
1566 n = printf("%s%.*s", column > start ? " " : "", wlen, s);
1575 bool is_pmu_core(const char *name)
1577 return !strcmp(name, "cpu") || is_arm_pmu_core(name);
1580 void print_pmu_events(const char *event_glob, bool name_only, bool quiet_flag,
1581 bool long_desc, bool details_flag, bool deprecated)
1583 struct perf_pmu *pmu;
1584 struct perf_pmu_alias *alias;
1588 struct sevent *aliases;
1590 int columns = pager_get_columns();
1595 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1596 list_for_each_entry(alias, &pmu->aliases, list)
1598 if (pmu->selectable)
1601 aliases = zalloc(sizeof(struct sevent) * len);
1606 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1607 list_for_each_entry(alias, &pmu->aliases, list) {
1608 char *name = alias->desc ? alias->name :
1609 format_alias(buf, sizeof(buf), pmu, alias);
1610 bool is_cpu = is_pmu_core(pmu->name);
1612 if (alias->deprecated && !deprecated)
1615 if (event_glob != NULL &&
1616 !(strglobmatch_nocase(name, event_glob) ||
1617 (!is_cpu && strglobmatch_nocase(alias->name,
1620 strglobmatch_nocase(alias->topic, event_glob))))
1623 if (is_cpu && !name_only && !alias->desc)
1624 name = format_alias_or(buf, sizeof(buf), pmu, alias);
1626 aliases[j].name = name;
1627 if (is_cpu && !name_only && !alias->desc)
1628 aliases[j].name = format_alias_or(buf,
1631 aliases[j].name = strdup(aliases[j].name);
1632 if (!aliases[j].name)
1635 aliases[j].desc = long_desc ? alias->long_desc :
1637 aliases[j].topic = alias->topic;
1638 aliases[j].str = alias->str;
1639 aliases[j].pmu = pmu->name;
1640 aliases[j].metric_expr = alias->metric_expr;
1641 aliases[j].metric_name = alias->metric_name;
1642 aliases[j].is_cpu = is_cpu;
1645 if (pmu->selectable &&
1646 (event_glob == NULL || strglobmatch(pmu->name, event_glob))) {
1648 if (asprintf(&s, "%s//", pmu->name) < 0)
1650 aliases[j].name = s;
1655 qsort(aliases, len, sizeof(struct sevent), cmp_sevent);
1656 for (j = 0; j < len; j++) {
1657 /* Skip duplicates */
1658 if (j > 0 && !strcmp(aliases[j].name, aliases[j - 1].name))
1661 printf("%s ", aliases[j].name);
1664 if (aliases[j].desc && !quiet_flag) {
1667 if (aliases[j].topic && (!topic ||
1668 strcmp(topic, aliases[j].topic))) {
1669 printf("%s%s:\n", topic ? "\n" : "",
1671 topic = aliases[j].topic;
1673 printf(" %-50s\n", aliases[j].name);
1674 printf("%*s", 8, "[");
1675 wordwrap(aliases[j].desc, 8, columns, 0);
1678 printf("%*s%s/%s/ ", 8, "", aliases[j].pmu, aliases[j].str);
1679 if (aliases[j].metric_name)
1680 printf(" MetricName: %s", aliases[j].metric_name);
1681 if (aliases[j].metric_expr)
1682 printf(" MetricExpr: %s", aliases[j].metric_expr);
1686 printf(" %-50s [Kernel PMU event]\n", aliases[j].name);
1689 if (printed && pager_in_use())
1692 for (j = 0; j < len; j++)
1693 zfree(&aliases[j].name);
1698 printf("FATAL: not enough memory to print PMU events\n");
1703 bool pmu_have_event(const char *pname, const char *name)
1705 struct perf_pmu *pmu;
1706 struct perf_pmu_alias *alias;
1709 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1710 if (strcmp(pname, pmu->name))
1712 list_for_each_entry(alias, &pmu->aliases, list)
1713 if (!strcmp(alias->name, name))
1719 static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
1721 char path[PATH_MAX];
1724 sysfs = sysfs__mountpoint();
1728 snprintf(path, PATH_MAX,
1729 "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name);
1730 if (!file_available(path))
1732 return fopen(path, "r");
1735 int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
1742 va_start(args, fmt);
1743 file = perf_pmu__open_file(pmu, name);
1745 ret = vfscanf(file, fmt, args);
1752 static int perf_pmu__new_caps(struct list_head *list, char *name, char *value)
1754 struct perf_pmu_caps *caps = zalloc(sizeof(*caps));
1759 caps->name = strdup(name);
1762 caps->value = strndup(value, strlen(value) - 1);
1765 list_add_tail(&caps->list, list);
1777 * Reading/parsing the given pmu capabilities, which should be located at:
1778 * /sys/bus/event_source/devices/<dev>/caps as sysfs group attributes.
1779 * Return the number of capabilities
1781 int perf_pmu__caps_parse(struct perf_pmu *pmu)
1784 char caps_path[PATH_MAX];
1785 const char *sysfs = sysfs__mountpoint();
1787 struct dirent *evt_ent;
1793 snprintf(caps_path, PATH_MAX,
1794 "%s" EVENT_SOURCE_DEVICE_PATH "%s/caps", sysfs, pmu->name);
1796 if (stat(caps_path, &st) < 0)
1797 return 0; /* no error if caps does not exist */
1799 caps_dir = opendir(caps_path);
1803 while ((evt_ent = readdir(caps_dir)) != NULL) {
1804 char path[PATH_MAX + NAME_MAX + 1];
1805 char *name = evt_ent->d_name;
1809 if (!strcmp(name, ".") || !strcmp(name, ".."))
1812 snprintf(path, sizeof(path), "%s/%s", caps_path, name);
1814 file = fopen(path, "r");
1818 if (!fgets(value, sizeof(value), file) ||
1819 (perf_pmu__new_caps(&pmu->caps, name, value) < 0)) {
1833 void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config,
1836 struct perf_pmu_format *format;
1837 __u64 masks = 0, bits;
1841 list_for_each_entry(format, &pmu->format, list) {
1842 if (format->value != PERF_PMU_FORMAT_VALUE_CONFIG)
1845 for_each_set_bit(i, format->bits, PERF_PMU_FORMAT_BITS)
1850 * Kernel doesn't export any valid format bits.
1855 bits = config & ~masks;
1859 bitmap_scnprintf((unsigned long *)&bits, sizeof(bits) * 8, buf, sizeof(buf));
1861 pr_warning("WARNING: event '%s' not valid (bits %s of config "
1862 "'%llx' not supported by kernel)!\n",
1863 name ?: "N/A", buf, config);
1866 bool perf_pmu__has_hybrid(void)
1868 if (!hybrid_scanned) {
1869 hybrid_scanned = true;
1870 perf_pmu__scan(NULL);
1873 return !list_empty(&perf_pmu__hybrid_pmus);