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 <linux/ctype.h>
7 #include <subcmd/pager.h>
17 #include <api/fs/fs.h>
20 #include <perf/cpumap.h>
27 #include "parse-events.h"
28 #include "print-events.h"
33 #include "pmu-hybrid.h"
35 struct perf_pmu perf_pmu__fake;
38 * struct perf_pmu_format - Values from a format file read from
39 * <sysfs>/devices/cpu/format/ held in struct perf_pmu.
41 * For example, the contents of <sysfs>/devices/cpu/format/event may be
42 * "config:0-7" and will be represented here as name="event",
43 * value=PERF_PMU_FORMAT_VALUE_CONFIG and bits 0 to 7 will be set.
45 struct perf_pmu_format {
46 /** @name: The modifier/file name. */
49 * @value : Which config value the format relates to. Supported values
50 * are from PERF_PMU_FORMAT_VALUE_CONFIG to
51 * PERF_PMU_FORMAT_VALUE_CONFIG_END.
54 /** @bits: Which config bits are set by this format value. */
55 DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
56 /** @list: Element on list within struct perf_pmu. */
57 struct list_head list;
60 int perf_pmu_parse(struct list_head *list, char *name);
61 extern FILE *perf_pmu_in;
63 static bool hybrid_scanned;
65 static struct perf_pmu *perf_pmu__find2(int dirfd, const char *name);
68 * Parse & process all the sysfs attributes located under
69 * the directory specified in 'dir' parameter.
71 int perf_pmu__format_parse(int dirfd, struct list_head *head)
73 struct dirent *evt_ent;
77 format_dir = fdopendir(dirfd);
81 while (!ret && (evt_ent = readdir(format_dir))) {
82 char *name = evt_ent->d_name;
85 if (!strcmp(name, ".") || !strcmp(name, ".."))
90 fd = openat(dirfd, name, O_RDONLY);
94 perf_pmu_in = fdopen(fd, "r");
95 ret = perf_pmu_parse(head, name);
104 * Reading/parsing the default pmu format definition, which should be
106 * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
108 static int pmu_format(int dirfd, const char *name, struct list_head *format)
112 fd = perf_pmu__pathname_fd(dirfd, name, "format", O_DIRECTORY);
116 /* it'll close the fd */
117 if (perf_pmu__format_parse(fd, format))
123 int perf_pmu__convert_scale(const char *scale, char **end, double *sval)
129 * save current locale
131 lc = setlocale(LC_NUMERIC, NULL);
134 * The lc string may be allocated in static storage,
135 * so get a dynamic copy to make it survive setlocale
145 * force to C locale to ensure kernel
146 * scale string is converted correctly.
147 * kernel uses default C locale.
149 setlocale(LC_NUMERIC, "C");
151 *sval = strtod(scale, end);
155 setlocale(LC_NUMERIC, lc);
160 static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, int dirfd, char *name)
168 scnprintf(path, PATH_MAX, "%s.scale", name);
170 fd = openat(dirfd, path, O_RDONLY);
174 if (fstat(fd, &st) < 0)
177 sret = read(fd, scale, sizeof(scale)-1);
181 if (scale[sret - 1] == '\n')
182 scale[sret - 1] = '\0';
186 ret = perf_pmu__convert_scale(scale, NULL, &alias->scale);
192 static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, int dirfd, char *name)
198 scnprintf(path, PATH_MAX, "%s.unit", name);
200 fd = openat(dirfd, path, O_RDONLY);
204 sret = read(fd, alias->unit, UNIT_MAX_LEN);
210 if (alias->unit[sret - 1] == '\n')
211 alias->unit[sret - 1] = '\0';
213 alias->unit[sret] = '\0';
218 alias->unit[0] = '\0';
223 perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, int dirfd, char *name)
228 scnprintf(path, PATH_MAX, "%s.per-pkg", name);
230 fd = openat(dirfd, path, O_RDONLY);
236 alias->per_pkg = true;
240 static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias,
241 int dirfd, char *name)
246 scnprintf(path, PATH_MAX, "%s.snapshot", name);
248 fd = openat(dirfd, path, O_RDONLY);
252 alias->snapshot = true;
257 static void perf_pmu_assign_str(char *name, const char *field, char **old_str,
263 if (*new_str) { /* Have new string, check with old */
264 if (strcasecmp(*old_str, *new_str))
265 pr_debug("alias %s differs in field '%s'\n",
268 } else /* Nothing new --> keep old string */
275 static void perf_pmu_update_alias(struct perf_pmu_alias *old,
276 struct perf_pmu_alias *newalias)
278 perf_pmu_assign_str(old->name, "desc", &old->desc, &newalias->desc);
279 perf_pmu_assign_str(old->name, "long_desc", &old->long_desc,
280 &newalias->long_desc);
281 perf_pmu_assign_str(old->name, "topic", &old->topic, &newalias->topic);
282 perf_pmu_assign_str(old->name, "value", &old->str, &newalias->str);
283 old->scale = newalias->scale;
284 old->per_pkg = newalias->per_pkg;
285 old->snapshot = newalias->snapshot;
286 memcpy(old->unit, newalias->unit, sizeof(old->unit));
289 /* Delete an alias entry. */
290 void perf_pmu_free_alias(struct perf_pmu_alias *newalias)
292 zfree(&newalias->name);
293 zfree(&newalias->desc);
294 zfree(&newalias->long_desc);
295 zfree(&newalias->topic);
296 zfree(&newalias->str);
297 zfree(&newalias->pmu_name);
298 parse_events_terms__purge(&newalias->terms);
302 static void perf_pmu__del_aliases(struct perf_pmu *pmu)
304 struct perf_pmu_alias *alias, *tmp;
306 list_for_each_entry_safe(alias, tmp, &pmu->aliases, list) {
307 list_del(&alias->list);
308 perf_pmu_free_alias(alias);
312 /* Merge an alias, search in alias list. If this name is already
313 * present merge both of them to combine all information.
315 static bool perf_pmu_merge_alias(struct perf_pmu_alias *newalias,
316 struct list_head *alist)
318 struct perf_pmu_alias *a;
320 list_for_each_entry(a, alist, list) {
321 if (!strcasecmp(newalias->name, a->name)) {
322 if (newalias->pmu_name && a->pmu_name &&
323 !strcasecmp(newalias->pmu_name, a->pmu_name)) {
326 perf_pmu_update_alias(a, newalias);
327 perf_pmu_free_alias(newalias);
334 static int __perf_pmu__new_alias(struct list_head *list, int dirfd, char *name,
335 char *desc, char *val, const struct pmu_event *pe)
337 struct parse_events_term *term;
338 struct perf_pmu_alias *alias;
341 char *long_desc = NULL, *topic = NULL, *unit = NULL, *pmu_name = NULL;
342 bool deprecated = false, perpkg = false;
345 long_desc = (char *)pe->long_desc;
346 topic = (char *)pe->topic;
347 unit = (char *)pe->unit;
349 deprecated = pe->deprecated;
350 pmu_name = (char *)pe->pmu;
353 alias = malloc(sizeof(*alias));
357 INIT_LIST_HEAD(&alias->terms);
359 alias->unit[0] = '\0';
360 alias->per_pkg = perpkg;
361 alias->snapshot = false;
362 alias->deprecated = deprecated;
364 ret = parse_events_terms(&alias->terms, val);
366 pr_err("Cannot parse alias %s: %d\n", val, ret);
371 /* Scan event and remove leading zeroes, spaces, newlines, some
372 * platforms have terms specified as
373 * event=0x0091 (read from files ../<PMU>/events/<FILE>
374 * and terms specified as event=0x91 (read from JSON files).
376 * Rebuild string to make alias->str member comparable.
378 memset(newval, 0, sizeof(newval));
380 list_for_each_entry(term, &alias->terms, list) {
382 ret += scnprintf(newval + ret, sizeof(newval) - ret,
384 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM)
385 ret += scnprintf(newval + ret, sizeof(newval) - ret,
386 "%s=%#x", term->config, term->val.num);
387 else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
388 ret += scnprintf(newval + ret, sizeof(newval) - ret,
389 "%s=%s", term->config, term->val.str);
392 alias->name = strdup(name);
395 * load unit name and scale if available
397 perf_pmu__parse_unit(alias, dirfd, name);
398 perf_pmu__parse_scale(alias, dirfd, name);
399 perf_pmu__parse_per_pkg(alias, dirfd, name);
400 perf_pmu__parse_snapshot(alias, dirfd, name);
403 alias->desc = desc ? strdup(desc) : NULL;
404 alias->long_desc = long_desc ? strdup(long_desc) :
405 desc ? strdup(desc) : NULL;
406 alias->topic = topic ? strdup(topic) : NULL;
408 if (perf_pmu__convert_scale(unit, &unit, &alias->scale) < 0)
410 snprintf(alias->unit, sizeof(alias->unit), "%s", unit);
412 alias->str = strdup(newval);
413 alias->pmu_name = pmu_name ? strdup(pmu_name) : NULL;
415 if (!perf_pmu_merge_alias(alias, list))
416 list_add_tail(&alias->list, list);
421 static int perf_pmu__new_alias(struct list_head *list, int dirfd, char *name, FILE *file)
426 ret = fread(buf, 1, sizeof(buf), file);
432 /* Remove trailing newline from sysfs file */
435 return __perf_pmu__new_alias(list, dirfd, name, NULL, buf, NULL);
438 static inline bool pmu_alias_info_file(char *name)
443 if (len > 5 && !strcmp(name + len - 5, ".unit"))
445 if (len > 6 && !strcmp(name + len - 6, ".scale"))
447 if (len > 8 && !strcmp(name + len - 8, ".per-pkg"))
449 if (len > 9 && !strcmp(name + len - 9, ".snapshot"))
456 * Process all the sysfs attributes located under the directory
457 * specified in 'dir' parameter.
459 static int pmu_aliases_parse(int dirfd, struct list_head *head)
461 struct dirent *evt_ent;
465 event_dir = fdopendir(dirfd);
469 while ((evt_ent = readdir(event_dir))) {
470 char *name = evt_ent->d_name;
473 if (!strcmp(name, ".") || !strcmp(name, ".."))
477 * skip info files parsed in perf_pmu__new_alias()
479 if (pmu_alias_info_file(name))
482 fd = openat(dirfd, name, O_RDONLY);
483 file = fdopen(fd, "r");
485 pr_debug("Cannot open %s\n", name);
489 if (perf_pmu__new_alias(head, dirfd, name, file) < 0)
490 pr_debug("Cannot set up %s\n", name);
499 * Reading the pmu event aliases definition, which should be located at:
500 * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
502 static int pmu_aliases(int dirfd, const char *name, struct list_head *head)
506 fd = perf_pmu__pathname_fd(dirfd, name, "events", O_DIRECTORY);
510 /* it'll close the fd */
511 if (pmu_aliases_parse(fd, head))
517 static int pmu_alias_terms(struct perf_pmu_alias *alias,
518 struct list_head *terms)
520 struct parse_events_term *term, *cloned;
524 list_for_each_entry(term, &alias->terms, list) {
525 ret = parse_events_term__clone(&cloned, term);
527 parse_events_terms__purge(&list);
531 * Weak terms don't override command line options,
532 * which we don't want for implicit terms in aliases.
535 list_add_tail(&cloned->list, &list);
537 list_splice(&list, terms);
541 /* Add all pmus in sysfs to pmu list: */
542 static void pmu_read_sysfs(void)
548 fd = perf_pmu__event_source_devices_fd();
556 while ((dent = readdir(dir))) {
557 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
559 /* add to static LIST_HEAD(pmus): */
560 perf_pmu__find2(fd, dent->d_name);
567 * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64)
568 * may have a "cpus" file.
570 static struct perf_cpu_map *pmu_cpumask(const char *name)
572 struct perf_cpu_map *cpus;
573 const char *templates[] = {
578 const char **template;
579 char pmu_name[PATH_MAX];
580 struct perf_pmu pmu = {.name = pmu_name};
583 strlcpy(pmu_name, name, sizeof(pmu_name));
584 for (template = templates; *template; template++) {
585 file = perf_pmu__open_file(&pmu, *template);
588 cpus = perf_cpu_map__read(file);
596 static bool pmu_is_uncore(const char *name)
600 if (perf_pmu__hybrid_mounted(name))
603 perf_pmu__pathname_scnprintf(path, sizeof(path), name, "cpumask");
604 return file_available(path);
607 static char *pmu_id(const char *name)
609 char path[PATH_MAX], *str;
612 perf_pmu__pathname_scnprintf(path, sizeof(path), name, "identifier");
614 if (filename__read_str(path, &str, &len) < 0)
617 str[len - 1] = 0; /* remove line feed */
623 * PMU CORE devices have different name other than cpu in sysfs on some
625 * Looking for possible sysfs files to identify the arm core device.
627 static int is_arm_pmu_core(const char *name)
631 if (!perf_pmu__pathname_scnprintf(path, sizeof(path), name, "cpus"))
633 return file_available(path);
636 char *perf_pmu__getcpuid(struct perf_pmu *pmu)
641 cpuid = getenv("PERF_CPUID");
643 cpuid = strdup(cpuid);
645 cpuid = get_cpuid_str(pmu);
650 pr_debug("Using CPUID %s\n", cpuid);
656 __weak const struct pmu_events_table *pmu_events_table__find(void)
658 return perf_pmu__find_events_table(NULL);
661 __weak const struct pmu_metrics_table *pmu_metrics_table__find(void)
663 return perf_pmu__find_metrics_table(NULL);
667 * Suffix must be in form tok_{digits}, or tok{digits}, or same as pmu_name
670 static bool perf_pmu__valid_suffix(const char *pmu_name, char *tok)
674 if (strncmp(pmu_name, tok, strlen(tok)))
677 p = pmu_name + strlen(tok);
684 /* Ensure we end in a number */
695 bool pmu_uncore_alias_match(const char *pmu_name, const char *name)
697 char *tmp = NULL, *tok, *str;
700 str = strdup(pmu_name);
705 * uncore alias may be from different PMU with common prefix
707 tok = strtok_r(str, ",", &tmp);
708 if (strncmp(pmu_name, tok, strlen(tok))) {
714 * Match more complex aliases where the alias name is a comma-delimited
715 * list of tokens, orderly contained in the matching PMU name.
717 * Example: For alias "socket,pmuname" and PMU "socketX_pmunameY", we
718 * match "socket" in "socketX_pmunameY" and then "pmuname" in
722 char *next_tok = strtok_r(NULL, ",", &tmp);
724 name = strstr(name, tok);
726 (!next_tok && !perf_pmu__valid_suffix(name, tok))) {
742 struct pmu_add_cpu_aliases_map_data {
743 struct list_head *head;
745 const char *cpu_name;
746 struct perf_pmu *pmu;
749 static int pmu_add_cpu_aliases_map_callback(const struct pmu_event *pe,
750 const struct pmu_events_table *table __maybe_unused,
753 struct pmu_add_cpu_aliases_map_data *data = vdata;
754 const char *pname = pe->pmu ? pe->pmu : data->cpu_name;
756 if (data->pmu->is_uncore && pmu_uncore_alias_match(pname, data->name))
759 if (strcmp(pname, data->name))
763 /* need type casts to override 'const' */
764 __perf_pmu__new_alias(data->head, -1, (char *)pe->name, (char *)pe->desc,
765 (char *)pe->event, pe);
770 * From the pmu_events_map, find the table of PMU events that corresponds
771 * to the current running CPU. Then, add all PMU events from that table
774 void pmu_add_cpu_aliases_table(struct list_head *head, struct perf_pmu *pmu,
775 const struct pmu_events_table *table)
777 struct pmu_add_cpu_aliases_map_data data = {
780 .cpu_name = is_arm_pmu_core(pmu->name) ? pmu->name : "cpu",
784 pmu_events_table_for_each_event(table, pmu_add_cpu_aliases_map_callback, &data);
787 static void pmu_add_cpu_aliases(struct list_head *head, struct perf_pmu *pmu)
789 const struct pmu_events_table *table;
791 table = perf_pmu__find_events_table(pmu);
795 pmu_add_cpu_aliases_table(head, pmu, table);
798 struct pmu_sys_event_iter_data {
799 struct list_head *head;
800 struct perf_pmu *pmu;
803 static int pmu_add_sys_aliases_iter_fn(const struct pmu_event *pe,
804 const struct pmu_events_table *table __maybe_unused,
807 struct pmu_sys_event_iter_data *idata = data;
808 struct perf_pmu *pmu = idata->pmu;
810 if (!pe->compat || !pe->pmu)
813 if (!strcmp(pmu->id, pe->compat) &&
814 pmu_uncore_alias_match(pe->pmu, pmu->name)) {
815 __perf_pmu__new_alias(idata->head, -1,
825 void pmu_add_sys_aliases(struct list_head *head, struct perf_pmu *pmu)
827 struct pmu_sys_event_iter_data idata = {
835 pmu_for_each_sys_event(pmu_add_sys_aliases_iter_fn, &idata);
838 struct perf_event_attr * __weak
839 perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
845 pmu_find_real_name(const char *name)
851 pmu_find_alias_name(const char *name __maybe_unused)
856 static int pmu_max_precise(struct perf_pmu *pmu)
858 int max_precise = -1;
860 perf_pmu__scan_file(pmu, "caps/max_precise", "%d", &max_precise);
864 static struct perf_pmu *pmu_lookup(int dirfd, const char *lookup_name)
866 struct perf_pmu *pmu;
870 char *name = pmu_find_real_name(lookup_name);
871 bool is_hybrid = perf_pmu__hybrid_mounted(name);
875 * Check pmu name for hybrid and the pmu may be invalid in sysfs
877 if (!strncmp(name, "cpu_", 4) && !is_hybrid)
881 * The pmu data we store & need consists of the pmu
882 * type value and format definitions. Load both right
885 if (pmu_format(dirfd, name, &format))
889 * Check the aliases first to avoid unnecessary work.
891 if (pmu_aliases(dirfd, name, &aliases))
894 pmu = zalloc(sizeof(*pmu));
898 pmu->cpus = pmu_cpumask(name);
899 pmu->name = strdup(name);
904 /* Read type, and ensure that type value is successfully assigned (return 1) */
905 if (perf_pmu__scan_file(pmu, "type", "%u", &type) != 1)
908 alias_name = pmu_find_alias_name(name);
910 pmu->alias_name = strdup(alias_name);
911 if (!pmu->alias_name)
916 pmu->is_uncore = pmu_is_uncore(name);
918 pmu->id = pmu_id(name);
919 pmu->max_precise = pmu_max_precise(pmu);
920 pmu_add_cpu_aliases(&aliases, pmu);
921 pmu_add_sys_aliases(&aliases, pmu);
923 INIT_LIST_HEAD(&pmu->format);
924 INIT_LIST_HEAD(&pmu->aliases);
925 INIT_LIST_HEAD(&pmu->caps);
926 list_splice(&format, &pmu->format);
927 list_splice(&aliases, &pmu->aliases);
928 list_add_tail(&pmu->list, &pmus);
931 list_add_tail(&pmu->hybrid_list, &perf_pmu__hybrid_pmus);
933 INIT_LIST_HEAD(&pmu->hybrid_list);
935 pmu->default_config = perf_pmu__get_default_config(pmu);
945 void perf_pmu__warn_invalid_formats(struct perf_pmu *pmu)
947 struct perf_pmu_format *format;
949 /* fake pmu doesn't have format list */
950 if (pmu == &perf_pmu__fake)
953 list_for_each_entry(format, &pmu->format, list)
954 if (format->value >= PERF_PMU_FORMAT_VALUE_CONFIG_END) {
955 pr_warning("WARNING: '%s' format '%s' requires 'perf_event_attr::config%d'"
956 "which is not supported by this version of perf!\n",
957 pmu->name, format->name, format->value);
962 static struct perf_pmu *pmu_find(const char *name)
964 struct perf_pmu *pmu;
966 list_for_each_entry(pmu, &pmus, list) {
967 if (!strcmp(pmu->name, name) ||
968 (pmu->alias_name && !strcmp(pmu->alias_name, name)))
975 struct perf_pmu *perf_pmu__find_by_type(unsigned int type)
977 struct perf_pmu *pmu;
979 list_for_each_entry(pmu, &pmus, list)
980 if (pmu->type == type)
986 struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
989 * pmu iterator: If pmu is NULL, we start at the begin,
990 * otherwise return the next pmu. Returns NULL on end.
994 pmu = list_prepare_entry(pmu, &pmus, list);
996 list_for_each_entry_continue(pmu, &pmus, list)
1001 struct perf_pmu *evsel__find_pmu(const struct evsel *evsel)
1003 struct perf_pmu *pmu = NULL;
1008 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1009 if (pmu->type == evsel->core.attr.type)
1013 ((struct evsel *)evsel)->pmu = pmu;
1017 bool evsel__is_aux_event(const struct evsel *evsel)
1019 struct perf_pmu *pmu = evsel__find_pmu(evsel);
1021 return pmu && pmu->auxtrace;
1024 struct perf_pmu *perf_pmu__find(const char *name)
1026 struct perf_pmu *pmu;
1030 * Once PMU is loaded it stays in the list,
1031 * so we keep us from multiple reading/parsing
1032 * the pmu format definitions.
1034 pmu = pmu_find(name);
1038 dirfd = perf_pmu__event_source_devices_fd();
1039 pmu = pmu_lookup(dirfd, name);
1045 static struct perf_pmu *perf_pmu__find2(int dirfd, const char *name)
1047 struct perf_pmu *pmu;
1050 * Once PMU is loaded it stays in the list,
1051 * so we keep us from multiple reading/parsing
1052 * the pmu format definitions.
1054 pmu = pmu_find(name);
1058 return pmu_lookup(dirfd, name);
1061 static struct perf_pmu_format *
1062 pmu_find_format(struct list_head *formats, const char *name)
1064 struct perf_pmu_format *format;
1066 list_for_each_entry(format, formats, list)
1067 if (!strcmp(format->name, name))
1073 __u64 perf_pmu__format_bits(struct list_head *formats, const char *name)
1075 struct perf_pmu_format *format = pmu_find_format(formats, name);
1082 for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
1083 bits |= 1ULL << fbit;
1088 int perf_pmu__format_type(struct list_head *formats, const char *name)
1090 struct perf_pmu_format *format = pmu_find_format(formats, name);
1095 return format->value;
1099 * Sets value based on the format definition (format parameter)
1100 * and unformatted value (value parameter).
1102 static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
1105 unsigned long fbit, vbit;
1107 for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
1109 if (!test_bit(fbit, format))
1112 if (value & (1llu << vbit++))
1113 *v |= (1llu << fbit);
1115 *v &= ~(1llu << fbit);
1119 static __u64 pmu_format_max_value(const unsigned long *format)
1123 w = bitmap_weight(format, PERF_PMU_FORMAT_BITS);
1127 return (1ULL << w) - 1;
1132 * Term is a string term, and might be a param-term. Try to look up it's value
1133 * in the remaining terms.
1134 * - We have a term like "base-or-format-term=param-term",
1135 * - We need to find the value supplied for "param-term" (with param-term named
1136 * in a config string) later on in the term list.
1138 static int pmu_resolve_param_term(struct parse_events_term *term,
1139 struct list_head *head_terms,
1142 struct parse_events_term *t;
1144 list_for_each_entry(t, head_terms, list) {
1145 if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM &&
1146 t->config && !strcmp(t->config, term->config)) {
1148 *value = t->val.num;
1154 printf("Required parameter '%s' not specified\n", term->config);
1159 static char *pmu_formats_string(struct list_head *formats)
1161 struct perf_pmu_format *format;
1163 struct strbuf buf = STRBUF_INIT;
1169 /* sysfs exported terms */
1170 list_for_each_entry(format, formats, list)
1171 if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
1174 str = strbuf_detach(&buf, NULL);
1176 strbuf_release(&buf);
1182 * Setup one of config[12] attr members based on the
1183 * user input data - term parameter.
1185 static int pmu_config_term(const char *pmu_name,
1186 struct list_head *formats,
1187 struct perf_event_attr *attr,
1188 struct parse_events_term *term,
1189 struct list_head *head_terms,
1190 bool zero, struct parse_events_error *err)
1192 struct perf_pmu_format *format;
1197 * If this is a parameter we've already used for parameterized-eval,
1198 * skip it in normal eval.
1204 * Hardcoded terms should be already in, so nothing
1205 * to be done for them.
1207 if (parse_events__is_hardcoded_term(term))
1210 format = pmu_find_format(formats, term->config);
1212 char *pmu_term = pmu_formats_string(formats);
1216 if (asprintf(&unknown_term,
1217 "unknown term '%s' for pmu '%s'",
1218 term->config, pmu_name) < 0)
1219 unknown_term = NULL;
1220 help_msg = parse_events_formats_error_string(pmu_term);
1222 parse_events_error__handle(err, term->err_term,
1226 pr_debug("%s (%s)\n", unknown_term, help_msg);
1233 switch (format->value) {
1234 case PERF_PMU_FORMAT_VALUE_CONFIG:
1237 case PERF_PMU_FORMAT_VALUE_CONFIG1:
1238 vp = &attr->config1;
1240 case PERF_PMU_FORMAT_VALUE_CONFIG2:
1241 vp = &attr->config2;
1243 case PERF_PMU_FORMAT_VALUE_CONFIG3:
1244 vp = &attr->config3;
1251 * Either directly use a numeric term, or try to translate string terms
1252 * using event parameters.
1254 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1255 if (term->no_value &&
1256 bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) {
1258 parse_events_error__handle(err, term->err_val,
1259 strdup("no value assigned for term"),
1265 val = term->val.num;
1266 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1267 if (strcmp(term->val.str, "?")) {
1269 pr_info("Invalid sysfs entry %s=%s\n",
1270 term->config, term->val.str);
1273 parse_events_error__handle(err, term->err_val,
1274 strdup("expected numeric value"),
1280 if (pmu_resolve_param_term(term, head_terms, &val))
1285 max_val = pmu_format_max_value(format->bits);
1286 if (val > max_val) {
1290 parse_events_error__handle(err, term->err_val,
1292 "value too big for format, maximum is %llu",
1293 (unsigned long long)max_val) < 0
1294 ? strdup("value too big for format")
1300 * Assume we don't care if !err, in which case the value will be
1301 * silently truncated.
1305 pmu_format_value(format->bits, val, vp, zero);
1309 int perf_pmu__config_terms(const char *pmu_name, struct list_head *formats,
1310 struct perf_event_attr *attr,
1311 struct list_head *head_terms,
1312 bool zero, struct parse_events_error *err)
1314 struct parse_events_term *term;
1316 list_for_each_entry(term, head_terms, list) {
1317 if (pmu_config_term(pmu_name, formats, attr, term, head_terms,
1326 * Configures event's 'attr' parameter based on the:
1327 * 1) users input - specified in terms parameter
1328 * 2) pmu format definitions - specified by pmu parameter
1330 int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
1331 struct list_head *head_terms,
1332 struct parse_events_error *err)
1334 bool zero = !!pmu->default_config;
1336 attr->type = pmu->type;
1337 return perf_pmu__config_terms(pmu->name, &pmu->format, attr,
1338 head_terms, zero, err);
1341 static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
1342 struct parse_events_term *term)
1344 struct perf_pmu_alias *alias;
1347 if (parse_events__is_hardcoded_term(term))
1350 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1351 if (term->val.num != 1)
1353 if (pmu_find_format(&pmu->format, term->config))
1355 name = term->config;
1356 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1357 if (strcasecmp(term->config, "event"))
1359 name = term->val.str;
1364 list_for_each_entry(alias, &pmu->aliases, list) {
1365 if (!strcasecmp(alias->name, name))
1372 static int check_info_data(struct perf_pmu_alias *alias,
1373 struct perf_pmu_info *info)
1376 * Only one term in event definition can
1377 * define unit, scale and snapshot, fail
1378 * if there's more than one.
1380 if ((info->unit && alias->unit[0]) ||
1381 (info->scale && alias->scale) ||
1382 (info->snapshot && alias->snapshot))
1386 info->unit = alias->unit;
1389 info->scale = alias->scale;
1391 if (alias->snapshot)
1392 info->snapshot = alias->snapshot;
1398 * Find alias in the terms list and replace it with the terms
1399 * defined for the alias
1401 int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
1402 struct perf_pmu_info *info)
1404 struct parse_events_term *term, *h;
1405 struct perf_pmu_alias *alias;
1408 info->per_pkg = false;
1411 * Mark unit and scale as not set
1412 * (different from default values, see below)
1416 info->snapshot = false;
1418 list_for_each_entry_safe(term, h, head_terms, list) {
1419 alias = pmu_find_alias(pmu, term);
1422 ret = pmu_alias_terms(alias, &term->list);
1426 ret = check_info_data(alias, info);
1431 info->per_pkg = true;
1433 list_del_init(&term->list);
1434 parse_events_term__delete(term);
1438 * if no unit or scale found in aliases, then
1439 * set defaults as for evsel
1440 * unit cannot left to NULL
1442 if (info->unit == NULL)
1445 if (info->scale == 0.0)
1451 int perf_pmu__new_format(struct list_head *list, char *name,
1452 int config, unsigned long *bits)
1454 struct perf_pmu_format *format;
1456 format = zalloc(sizeof(*format));
1460 format->name = strdup(name);
1461 format->value = config;
1462 memcpy(format->bits, bits, sizeof(format->bits));
1464 list_add_tail(&format->list, list);
1468 void perf_pmu__set_format(unsigned long *bits, long from, long to)
1475 memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
1476 for (b = from; b <= to; b++)
1480 void perf_pmu__del_formats(struct list_head *formats)
1482 struct perf_pmu_format *fmt, *tmp;
1484 list_for_each_entry_safe(fmt, tmp, formats, list) {
1485 list_del(&fmt->list);
1491 static int sub_non_neg(int a, int b)
1498 static char *format_alias(char *buf, int len, const struct perf_pmu *pmu,
1499 const struct perf_pmu_alias *alias)
1501 struct parse_events_term *term;
1502 int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name);
1504 list_for_each_entry(term, &alias->terms, list) {
1505 if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
1506 used += snprintf(buf + used, sub_non_neg(len, used),
1507 ",%s=%s", term->config,
1511 if (sub_non_neg(len, used) > 0) {
1515 if (sub_non_neg(len, used) > 0) {
1519 buf[len - 1] = '\0';
1524 /** Struct for ordering events as output in perf list. */
1526 /** PMU for event. */
1527 const struct perf_pmu *pmu;
1529 * Optional event for name, desc, etc. If not present then this is a
1530 * selectable PMU and the event name is shown as "//".
1532 const struct perf_pmu_alias *event;
1533 /** Is the PMU for the CPU? */
1537 static int cmp_sevent(const void *a, const void *b)
1539 const struct sevent *as = a;
1540 const struct sevent *bs = b;
1541 const char *a_pmu_name, *b_pmu_name;
1542 const char *a_name = "//", *a_desc = NULL, *a_topic = "";
1543 const char *b_name = "//", *b_desc = NULL, *b_topic = "";
1547 a_name = as->event->name;
1548 a_desc = as->event->desc;
1549 a_topic = as->event->topic ?: "";
1552 b_name = bs->event->name;
1553 b_desc = bs->event->desc;
1554 b_topic = bs->event->topic ?: "";
1556 /* Put extra events last. */
1557 if (!!a_desc != !!b_desc)
1558 return !!a_desc - !!b_desc;
1560 /* Order by topics. */
1561 ret = strcmp(a_topic, b_topic);
1565 /* Order CPU core events to be first */
1566 if (as->is_cpu != bs->is_cpu)
1567 return as->is_cpu ? -1 : 1;
1569 /* Order by PMU name. */
1570 a_pmu_name = as->pmu->name ?: "";
1571 b_pmu_name = bs->pmu->name ?: "";
1572 ret = strcmp(a_pmu_name, b_pmu_name);
1576 /* Order by event name. */
1577 return strcmp(a_name, b_name);
1580 bool is_pmu_core(const char *name)
1582 return !strcmp(name, "cpu") || is_arm_pmu_core(name);
1585 static bool pmu_alias_is_duplicate(struct sevent *alias_a,
1586 struct sevent *alias_b)
1588 const char *a_pmu_name, *b_pmu_name;
1589 const char *a_name = alias_a->event ? alias_a->event->name : "//";
1590 const char *b_name = alias_b->event ? alias_b->event->name : "//";
1592 /* Different names -> never duplicates */
1593 if (strcmp(a_name, b_name))
1596 /* Don't remove duplicates for different PMUs */
1597 a_pmu_name = alias_a->pmu->name ?: "";
1598 b_pmu_name = alias_b->pmu->name ?: "";
1599 return strcmp(a_pmu_name, b_pmu_name) == 0;
1602 void print_pmu_events(const struct print_callbacks *print_cb, void *print_state)
1604 struct perf_pmu *pmu;
1605 struct perf_pmu_alias *event;
1609 struct sevent *aliases;
1613 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1614 list_for_each_entry(event, &pmu->aliases, list)
1616 if (pmu->selectable)
1619 aliases = zalloc(sizeof(struct sevent) * len);
1621 pr_err("FATAL: not enough memory to print PMU events\n");
1626 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1627 bool is_cpu = is_pmu_core(pmu->name) || perf_pmu__is_hybrid(pmu->name);
1629 list_for_each_entry(event, &pmu->aliases, list) {
1630 aliases[j].event = event;
1631 aliases[j].pmu = pmu;
1632 aliases[j].is_cpu = is_cpu;
1635 if (pmu->selectable) {
1636 aliases[j].event = NULL;
1637 aliases[j].pmu = pmu;
1638 aliases[j].is_cpu = is_cpu;
1643 qsort(aliases, len, sizeof(struct sevent), cmp_sevent);
1644 for (j = 0; j < len; j++) {
1645 const char *name, *alias = NULL, *scale_unit = NULL,
1646 *desc = NULL, *long_desc = NULL,
1647 *encoding_desc = NULL, *topic = NULL;
1648 bool deprecated = false;
1651 /* Skip duplicates */
1652 if (j > 0 && pmu_alias_is_duplicate(&aliases[j], &aliases[j - 1]))
1655 if (!aliases[j].event) {
1656 /* A selectable event. */
1657 buf_used = snprintf(buf, sizeof(buf), "%s//", aliases[j].pmu->name) + 1;
1660 if (aliases[j].event->desc) {
1661 name = aliases[j].event->name;
1664 name = format_alias(buf, sizeof(buf), aliases[j].pmu,
1666 if (aliases[j].is_cpu) {
1668 name = aliases[j].event->name;
1670 buf_used = strlen(buf) + 1;
1672 if (strlen(aliases[j].event->unit) || aliases[j].event->scale != 1.0) {
1673 scale_unit = buf + buf_used;
1674 buf_used += snprintf(buf + buf_used, sizeof(buf) - buf_used,
1675 "%G%s", aliases[j].event->scale,
1676 aliases[j].event->unit) + 1;
1678 desc = aliases[j].event->desc;
1679 long_desc = aliases[j].event->long_desc;
1680 topic = aliases[j].event->topic;
1681 encoding_desc = buf + buf_used;
1682 buf_used += snprintf(buf + buf_used, sizeof(buf) - buf_used,
1683 "%s/%s/", aliases[j].pmu->name,
1684 aliases[j].event->str) + 1;
1685 deprecated = aliases[j].event->deprecated;
1687 print_cb->print_event(print_state,
1688 aliases[j].pmu->name,
1699 if (printed && pager_in_use())
1706 bool pmu_have_event(const char *pname, const char *name)
1708 struct perf_pmu *pmu;
1709 struct perf_pmu_alias *alias;
1712 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1713 if (strcmp(pname, pmu->name))
1715 list_for_each_entry(alias, &pmu->aliases, list)
1716 if (!strcmp(alias->name, name))
1722 FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
1724 char path[PATH_MAX];
1726 if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, name) ||
1727 !file_available(path))
1730 return fopen(path, "r");
1733 int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
1740 va_start(args, fmt);
1741 file = perf_pmu__open_file(pmu, name);
1743 ret = vfscanf(file, fmt, args);
1750 bool perf_pmu__file_exists(struct perf_pmu *pmu, const char *name)
1752 char path[PATH_MAX];
1754 if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, name))
1757 return file_available(path);
1760 static int perf_pmu__new_caps(struct list_head *list, char *name, char *value)
1762 struct perf_pmu_caps *caps = zalloc(sizeof(*caps));
1767 caps->name = strdup(name);
1770 caps->value = strndup(value, strlen(value) - 1);
1773 list_add_tail(&caps->list, list);
1784 static void perf_pmu__del_caps(struct perf_pmu *pmu)
1786 struct perf_pmu_caps *caps, *tmp;
1788 list_for_each_entry_safe(caps, tmp, &pmu->caps, list) {
1789 list_del(&caps->list);
1797 * Reading/parsing the given pmu capabilities, which should be located at:
1798 * /sys/bus/event_source/devices/<dev>/caps as sysfs group attributes.
1799 * Return the number of capabilities
1801 int perf_pmu__caps_parse(struct perf_pmu *pmu)
1804 char caps_path[PATH_MAX];
1806 struct dirent *evt_ent;
1809 if (pmu->caps_initialized)
1810 return pmu->nr_caps;
1814 if (!perf_pmu__pathname_scnprintf(caps_path, sizeof(caps_path), pmu->name, "caps"))
1817 if (stat(caps_path, &st) < 0) {
1818 pmu->caps_initialized = true;
1819 return 0; /* no error if caps does not exist */
1822 caps_dir = opendir(caps_path);
1826 caps_fd = dirfd(caps_dir);
1828 while ((evt_ent = readdir(caps_dir)) != NULL) {
1829 char *name = evt_ent->d_name;
1834 if (!strcmp(name, ".") || !strcmp(name, ".."))
1837 fd = openat(caps_fd, name, O_RDONLY);
1838 file = fdopen(fd, "r");
1842 if (!fgets(value, sizeof(value), file) ||
1843 (perf_pmu__new_caps(&pmu->caps, name, value) < 0)) {
1854 pmu->caps_initialized = true;
1855 return pmu->nr_caps;
1858 void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config,
1861 struct perf_pmu_format *format;
1862 __u64 masks = 0, bits;
1866 list_for_each_entry(format, &pmu->format, list) {
1867 if (format->value != PERF_PMU_FORMAT_VALUE_CONFIG)
1870 for_each_set_bit(i, format->bits, PERF_PMU_FORMAT_BITS)
1875 * Kernel doesn't export any valid format bits.
1880 bits = config & ~masks;
1884 bitmap_scnprintf((unsigned long *)&bits, sizeof(bits) * 8, buf, sizeof(buf));
1886 pr_warning("WARNING: event '%s' not valid (bits %s of config "
1887 "'%llx' not supported by kernel)!\n",
1888 name ?: "N/A", buf, config);
1891 bool perf_pmu__has_hybrid(void)
1893 if (!hybrid_scanned) {
1894 hybrid_scanned = true;
1895 perf_pmu__scan(NULL);
1898 return !list_empty(&perf_pmu__hybrid_pmus);
1901 int perf_pmu__match(char *pattern, char *name, char *tok)
1906 if (fnmatch(pattern, name, 0))
1909 if (tok && !perf_pmu__valid_suffix(name, tok))
1915 int perf_pmu__cpus_match(struct perf_pmu *pmu, struct perf_cpu_map *cpus,
1916 struct perf_cpu_map **mcpus_ptr,
1917 struct perf_cpu_map **ucpus_ptr)
1919 struct perf_cpu_map *pmu_cpus = pmu->cpus;
1920 struct perf_cpu_map *matched_cpus, *unmatched_cpus;
1921 struct perf_cpu cpu;
1922 int i, matched_nr = 0, unmatched_nr = 0;
1924 matched_cpus = perf_cpu_map__default_new();
1928 unmatched_cpus = perf_cpu_map__default_new();
1929 if (!unmatched_cpus) {
1930 perf_cpu_map__put(matched_cpus);
1934 perf_cpu_map__for_each_cpu(cpu, i, cpus) {
1935 if (!perf_cpu_map__has(pmu_cpus, cpu))
1936 unmatched_cpus->map[unmatched_nr++] = cpu;
1938 matched_cpus->map[matched_nr++] = cpu;
1941 unmatched_cpus->nr = unmatched_nr;
1942 matched_cpus->nr = matched_nr;
1943 *mcpus_ptr = matched_cpus;
1944 *ucpus_ptr = unmatched_cpus;
1948 double __weak perf_pmu__cpu_slots_per_cycle(void)
1953 int perf_pmu__event_source_devices_scnprintf(char *pathname, size_t size)
1955 const char *sysfs = sysfs__mountpoint();
1959 return scnprintf(pathname, size, "%s/bus/event_source/devices/", sysfs);
1962 int perf_pmu__event_source_devices_fd(void)
1964 char path[PATH_MAX];
1965 const char *sysfs = sysfs__mountpoint();
1970 scnprintf(path, sizeof(path), "%s/bus/event_source/devices/", sysfs);
1971 return open(path, O_DIRECTORY);
1975 * Fill 'buf' with the path to a file or folder in 'pmu_name' in
1976 * sysfs. For example if pmu_name = "cs_etm" and 'filename' = "format"
1977 * then pathname will be filled with
1978 * "/sys/bus/event_source/devices/cs_etm/format"
1980 * Return 0 if the sysfs mountpoint couldn't be found or if no
1981 * characters were written.
1983 int perf_pmu__pathname_scnprintf(char *buf, size_t size,
1984 const char *pmu_name, const char *filename)
1986 char base_path[PATH_MAX];
1988 if (!perf_pmu__event_source_devices_scnprintf(base_path, sizeof(base_path)))
1990 return scnprintf(buf, size, "%s%s/%s", base_path, pmu_name, filename);
1993 int perf_pmu__pathname_fd(int dirfd, const char *pmu_name, const char *filename, int flags)
1995 char path[PATH_MAX];
1997 scnprintf(path, sizeof(path), "%s/%s", pmu_name, filename);
1998 return openat(dirfd, path, flags);
2001 static void perf_pmu__delete(struct perf_pmu *pmu)
2003 perf_pmu__del_formats(&pmu->format);
2004 perf_pmu__del_aliases(pmu);
2005 perf_pmu__del_caps(pmu);
2007 perf_cpu_map__put(pmu->cpus);
2009 free(pmu->default_config);
2011 free(pmu->alias_name);
2015 void perf_pmu__destroy(void)
2017 struct perf_pmu *pmu, *tmp;
2019 list_for_each_entry_safe(pmu, tmp, &pmus, list) {
2020 list_del(&pmu->list);
2021 list_del(&pmu->hybrid_list);
2023 perf_pmu__delete(pmu);