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;
66 * Parse & process all the sysfs attributes located under
67 * the directory specified in 'dir' parameter.
69 int perf_pmu__format_parse(char *dir, struct list_head *head)
71 struct dirent *evt_ent;
75 format_dir = opendir(dir);
79 while (!ret && (evt_ent = readdir(format_dir))) {
81 char *name = evt_ent->d_name;
84 if (!strcmp(name, ".") || !strcmp(name, ".."))
87 snprintf(path, PATH_MAX, "%s/%s", dir, name);
90 file = fopen(path, "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(const char *name, struct list_head *format)
112 if (!perf_pmu__pathname_scnprintf(path, sizeof(path), name, "format"))
115 if (!file_available(path))
118 if (perf_pmu__format_parse(path, format))
124 int perf_pmu__convert_scale(const char *scale, char **end, double *sval)
130 * save current locale
132 lc = setlocale(LC_NUMERIC, NULL);
135 * The lc string may be allocated in static storage,
136 * so get a dynamic copy to make it survive setlocale
146 * force to C locale to ensure kernel
147 * scale string is converted correctly.
148 * kernel uses default C locale.
150 setlocale(LC_NUMERIC, "C");
152 *sval = strtod(scale, end);
156 setlocale(LC_NUMERIC, lc);
161 static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *name)
169 scnprintf(path, PATH_MAX, "%s/%s.scale", dir, name);
171 fd = open(path, O_RDONLY);
175 if (fstat(fd, &st) < 0)
178 sret = read(fd, scale, sizeof(scale)-1);
182 if (scale[sret - 1] == '\n')
183 scale[sret - 1] = '\0';
187 ret = perf_pmu__convert_scale(scale, NULL, &alias->scale);
193 static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *name)
199 scnprintf(path, PATH_MAX, "%s/%s.unit", dir, name);
201 fd = open(path, O_RDONLY);
205 sret = read(fd, alias->unit, UNIT_MAX_LEN);
211 if (alias->unit[sret - 1] == '\n')
212 alias->unit[sret - 1] = '\0';
214 alias->unit[sret] = '\0';
219 alias->unit[0] = '\0';
224 perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, char *dir, char *name)
229 scnprintf(path, PATH_MAX, "%s/%s.per-pkg", dir, name);
231 fd = open(path, O_RDONLY);
237 alias->per_pkg = true;
241 static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias,
242 char *dir, char *name)
247 scnprintf(path, PATH_MAX, "%s/%s.snapshot", dir, name);
249 fd = open(path, O_RDONLY);
253 alias->snapshot = true;
258 static void perf_pmu_assign_str(char *name, const char *field, char **old_str,
264 if (*new_str) { /* Have new string, check with old */
265 if (strcasecmp(*old_str, *new_str))
266 pr_debug("alias %s differs in field '%s'\n",
269 } else /* Nothing new --> keep old string */
276 static void perf_pmu_update_alias(struct perf_pmu_alias *old,
277 struct perf_pmu_alias *newalias)
279 perf_pmu_assign_str(old->name, "desc", &old->desc, &newalias->desc);
280 perf_pmu_assign_str(old->name, "long_desc", &old->long_desc,
281 &newalias->long_desc);
282 perf_pmu_assign_str(old->name, "topic", &old->topic, &newalias->topic);
283 perf_pmu_assign_str(old->name, "value", &old->str, &newalias->str);
284 old->scale = newalias->scale;
285 old->per_pkg = newalias->per_pkg;
286 old->snapshot = newalias->snapshot;
287 memcpy(old->unit, newalias->unit, sizeof(old->unit));
290 /* Delete an alias entry. */
291 void perf_pmu_free_alias(struct perf_pmu_alias *newalias)
293 zfree(&newalias->name);
294 zfree(&newalias->desc);
295 zfree(&newalias->long_desc);
296 zfree(&newalias->topic);
297 zfree(&newalias->str);
298 zfree(&newalias->pmu_name);
299 parse_events_terms__purge(&newalias->terms);
303 /* Merge an alias, search in alias list. If this name is already
304 * present merge both of them to combine all information.
306 static bool perf_pmu_merge_alias(struct perf_pmu_alias *newalias,
307 struct list_head *alist)
309 struct perf_pmu_alias *a;
311 list_for_each_entry(a, alist, list) {
312 if (!strcasecmp(newalias->name, a->name)) {
313 if (newalias->pmu_name && a->pmu_name &&
314 !strcasecmp(newalias->pmu_name, a->pmu_name)) {
317 perf_pmu_update_alias(a, newalias);
318 perf_pmu_free_alias(newalias);
325 static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name,
326 char *desc, char *val, const struct pmu_event *pe)
328 struct parse_events_term *term;
329 struct perf_pmu_alias *alias;
332 char *long_desc = NULL, *topic = NULL, *unit = NULL, *pmu_name = NULL;
333 bool deprecated = false, perpkg = false;
336 long_desc = (char *)pe->long_desc;
337 topic = (char *)pe->topic;
338 unit = (char *)pe->unit;
340 deprecated = pe->deprecated;
341 pmu_name = (char *)pe->pmu;
344 alias = malloc(sizeof(*alias));
348 INIT_LIST_HEAD(&alias->terms);
350 alias->unit[0] = '\0';
351 alias->per_pkg = perpkg;
352 alias->snapshot = false;
353 alias->deprecated = deprecated;
355 ret = parse_events_terms(&alias->terms, val);
357 pr_err("Cannot parse alias %s: %d\n", val, ret);
362 /* Scan event and remove leading zeroes, spaces, newlines, some
363 * platforms have terms specified as
364 * event=0x0091 (read from files ../<PMU>/events/<FILE>
365 * and terms specified as event=0x91 (read from JSON files).
367 * Rebuild string to make alias->str member comparable.
369 memset(newval, 0, sizeof(newval));
371 list_for_each_entry(term, &alias->terms, list) {
373 ret += scnprintf(newval + ret, sizeof(newval) - ret,
375 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM)
376 ret += scnprintf(newval + ret, sizeof(newval) - ret,
377 "%s=%#x", term->config, term->val.num);
378 else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
379 ret += scnprintf(newval + ret, sizeof(newval) - ret,
380 "%s=%s", term->config, term->val.str);
383 alias->name = strdup(name);
386 * load unit name and scale if available
388 perf_pmu__parse_unit(alias, dir, name);
389 perf_pmu__parse_scale(alias, dir, name);
390 perf_pmu__parse_per_pkg(alias, dir, name);
391 perf_pmu__parse_snapshot(alias, dir, name);
394 alias->desc = desc ? strdup(desc) : NULL;
395 alias->long_desc = long_desc ? strdup(long_desc) :
396 desc ? strdup(desc) : NULL;
397 alias->topic = topic ? strdup(topic) : NULL;
399 if (perf_pmu__convert_scale(unit, &unit, &alias->scale) < 0)
401 snprintf(alias->unit, sizeof(alias->unit), "%s", unit);
403 alias->str = strdup(newval);
404 alias->pmu_name = pmu_name ? strdup(pmu_name) : NULL;
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)
498 if (!perf_pmu__pathname_scnprintf(path, sizeof(path), name, "events"))
501 if (!file_available(path))
504 if (pmu_aliases_parse(path, head))
510 static int pmu_alias_terms(struct perf_pmu_alias *alias,
511 struct list_head *terms)
513 struct parse_events_term *term, *cloned;
517 list_for_each_entry(term, &alias->terms, list) {
518 ret = parse_events_term__clone(&cloned, term);
520 parse_events_terms__purge(&list);
524 * Weak terms don't override command line options,
525 * which we don't want for implicit terms in aliases.
528 list_add_tail(&cloned->list, &list);
530 list_splice(&list, terms);
534 /* Add all pmus in sysfs to pmu list: */
535 static void pmu_read_sysfs(void)
541 if (!perf_pmu__event_source_devices_scnprintf(path, sizeof(path)))
548 while ((dent = readdir(dir))) {
549 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
551 /* add to static LIST_HEAD(pmus): */
552 perf_pmu__find(dent->d_name);
559 * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64)
560 * may have a "cpus" file.
562 static struct perf_cpu_map *pmu_cpumask(const char *name)
564 struct perf_cpu_map *cpus;
565 const char *templates[] = {
570 const char **template;
571 char pmu_name[PATH_MAX];
572 struct perf_pmu pmu = {.name = pmu_name};
575 strlcpy(pmu_name, name, sizeof(pmu_name));
576 for (template = templates; *template; template++) {
577 file = perf_pmu__open_file(&pmu, *template);
580 cpus = perf_cpu_map__read(file);
588 static bool pmu_is_uncore(const char *name)
592 if (perf_pmu__hybrid_mounted(name))
595 perf_pmu__pathname_scnprintf(path, sizeof(path), name, "cpumask");
596 return file_available(path);
599 static char *pmu_id(const char *name)
601 char path[PATH_MAX], *str;
604 perf_pmu__pathname_scnprintf(path, sizeof(path), name, "identifier");
606 if (filename__read_str(path, &str, &len) < 0)
609 str[len - 1] = 0; /* remove line feed */
615 * PMU CORE devices have different name other than cpu in sysfs on some
617 * Looking for possible sysfs files to identify the arm core device.
619 static int is_arm_pmu_core(const char *name)
623 if (!perf_pmu__pathname_scnprintf(path, sizeof(path), name, "cpus"))
625 return file_available(path);
628 char *perf_pmu__getcpuid(struct perf_pmu *pmu)
633 cpuid = getenv("PERF_CPUID");
635 cpuid = strdup(cpuid);
637 cpuid = get_cpuid_str(pmu);
642 pr_debug("Using CPUID %s\n", cpuid);
648 __weak const struct pmu_events_table *pmu_events_table__find(void)
650 return perf_pmu__find_events_table(NULL);
653 __weak const struct pmu_metrics_table *pmu_metrics_table__find(void)
655 return perf_pmu__find_metrics_table(NULL);
659 * Suffix must be in form tok_{digits}, or tok{digits}, or same as pmu_name
662 static bool perf_pmu__valid_suffix(const char *pmu_name, char *tok)
666 if (strncmp(pmu_name, tok, strlen(tok)))
669 p = pmu_name + strlen(tok);
676 /* Ensure we end in a number */
687 bool pmu_uncore_alias_match(const char *pmu_name, const char *name)
689 char *tmp = NULL, *tok, *str;
692 str = strdup(pmu_name);
697 * uncore alias may be from different PMU with common prefix
699 tok = strtok_r(str, ",", &tmp);
700 if (strncmp(pmu_name, tok, strlen(tok))) {
706 * Match more complex aliases where the alias name is a comma-delimited
707 * list of tokens, orderly contained in the matching PMU name.
709 * Example: For alias "socket,pmuname" and PMU "socketX_pmunameY", we
710 * match "socket" in "socketX_pmunameY" and then "pmuname" in
714 char *next_tok = strtok_r(NULL, ",", &tmp);
716 name = strstr(name, tok);
718 (!next_tok && !perf_pmu__valid_suffix(name, tok))) {
734 struct pmu_add_cpu_aliases_map_data {
735 struct list_head *head;
737 const char *cpu_name;
738 struct perf_pmu *pmu;
741 static int pmu_add_cpu_aliases_map_callback(const struct pmu_event *pe,
742 const struct pmu_events_table *table __maybe_unused,
745 struct pmu_add_cpu_aliases_map_data *data = vdata;
746 const char *pname = pe->pmu ? pe->pmu : data->cpu_name;
748 if (data->pmu->is_uncore && pmu_uncore_alias_match(pname, data->name))
751 if (strcmp(pname, data->name))
755 /* need type casts to override 'const' */
756 __perf_pmu__new_alias(data->head, NULL, (char *)pe->name, (char *)pe->desc,
757 (char *)pe->event, pe);
762 * From the pmu_events_map, find the table of PMU events that corresponds
763 * to the current running CPU. Then, add all PMU events from that table
766 void pmu_add_cpu_aliases_table(struct list_head *head, struct perf_pmu *pmu,
767 const struct pmu_events_table *table)
769 struct pmu_add_cpu_aliases_map_data data = {
772 .cpu_name = is_arm_pmu_core(pmu->name) ? pmu->name : "cpu",
776 pmu_events_table_for_each_event(table, pmu_add_cpu_aliases_map_callback, &data);
779 static void pmu_add_cpu_aliases(struct list_head *head, struct perf_pmu *pmu)
781 const struct pmu_events_table *table;
783 table = perf_pmu__find_events_table(pmu);
787 pmu_add_cpu_aliases_table(head, pmu, table);
790 struct pmu_sys_event_iter_data {
791 struct list_head *head;
792 struct perf_pmu *pmu;
795 static int pmu_add_sys_aliases_iter_fn(const struct pmu_event *pe,
796 const struct pmu_events_table *table __maybe_unused,
799 struct pmu_sys_event_iter_data *idata = data;
800 struct perf_pmu *pmu = idata->pmu;
802 if (!pe->compat || !pe->pmu)
805 if (!strcmp(pmu->id, pe->compat) &&
806 pmu_uncore_alias_match(pe->pmu, pmu->name)) {
807 __perf_pmu__new_alias(idata->head, NULL,
817 void pmu_add_sys_aliases(struct list_head *head, struct perf_pmu *pmu)
819 struct pmu_sys_event_iter_data idata = {
827 pmu_for_each_sys_event(pmu_add_sys_aliases_iter_fn, &idata);
830 struct perf_event_attr * __weak
831 perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
837 pmu_find_real_name(const char *name)
843 pmu_find_alias_name(const char *name __maybe_unused)
848 static int pmu_max_precise(struct perf_pmu *pmu)
850 int max_precise = -1;
852 perf_pmu__scan_file(pmu, "caps/max_precise", "%d", &max_precise);
856 static struct perf_pmu *pmu_lookup(const char *lookup_name)
858 struct perf_pmu *pmu;
862 char *name = pmu_find_real_name(lookup_name);
863 bool is_hybrid = perf_pmu__hybrid_mounted(name);
867 * Check pmu name for hybrid and the pmu may be invalid in sysfs
869 if (!strncmp(name, "cpu_", 4) && !is_hybrid)
873 * The pmu data we store & need consists of the pmu
874 * type value and format definitions. Load both right
877 if (pmu_format(name, &format))
881 * Check the aliases first to avoid unnecessary work.
883 if (pmu_aliases(name, &aliases))
886 pmu = zalloc(sizeof(*pmu));
890 pmu->cpus = pmu_cpumask(name);
891 pmu->name = strdup(name);
896 /* Read type, and ensure that type value is successfully assigned (return 1) */
897 if (perf_pmu__scan_file(pmu, "type", "%u", &type) != 1)
900 alias_name = pmu_find_alias_name(name);
902 pmu->alias_name = strdup(alias_name);
903 if (!pmu->alias_name)
908 pmu->is_uncore = pmu_is_uncore(name);
910 pmu->id = pmu_id(name);
911 pmu->max_precise = pmu_max_precise(pmu);
912 pmu_add_cpu_aliases(&aliases, pmu);
913 pmu_add_sys_aliases(&aliases, pmu);
915 INIT_LIST_HEAD(&pmu->format);
916 INIT_LIST_HEAD(&pmu->aliases);
917 INIT_LIST_HEAD(&pmu->caps);
918 list_splice(&format, &pmu->format);
919 list_splice(&aliases, &pmu->aliases);
920 list_add_tail(&pmu->list, &pmus);
923 list_add_tail(&pmu->hybrid_list, &perf_pmu__hybrid_pmus);
925 pmu->default_config = perf_pmu__get_default_config(pmu);
935 void perf_pmu__warn_invalid_formats(struct perf_pmu *pmu)
937 struct perf_pmu_format *format;
939 /* fake pmu doesn't have format list */
940 if (pmu == &perf_pmu__fake)
943 list_for_each_entry(format, &pmu->format, list)
944 if (format->value >= PERF_PMU_FORMAT_VALUE_CONFIG_END) {
945 pr_warning("WARNING: '%s' format '%s' requires 'perf_event_attr::config%d'"
946 "which is not supported by this version of perf!\n",
947 pmu->name, format->name, format->value);
952 static struct perf_pmu *pmu_find(const char *name)
954 struct perf_pmu *pmu;
956 list_for_each_entry(pmu, &pmus, list) {
957 if (!strcmp(pmu->name, name) ||
958 (pmu->alias_name && !strcmp(pmu->alias_name, name)))
965 struct perf_pmu *perf_pmu__find_by_type(unsigned int type)
967 struct perf_pmu *pmu;
969 list_for_each_entry(pmu, &pmus, list)
970 if (pmu->type == type)
976 struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
979 * pmu iterator: If pmu is NULL, we start at the begin,
980 * otherwise return the next pmu. Returns NULL on end.
984 pmu = list_prepare_entry(pmu, &pmus, list);
986 list_for_each_entry_continue(pmu, &pmus, list)
991 struct perf_pmu *evsel__find_pmu(const struct evsel *evsel)
993 struct perf_pmu *pmu = NULL;
998 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
999 if (pmu->type == evsel->core.attr.type)
1003 ((struct evsel *)evsel)->pmu = pmu;
1007 bool evsel__is_aux_event(const struct evsel *evsel)
1009 struct perf_pmu *pmu = evsel__find_pmu(evsel);
1011 return pmu && pmu->auxtrace;
1014 struct perf_pmu *perf_pmu__find(const char *name)
1016 struct perf_pmu *pmu;
1019 * Once PMU is loaded it stays in the list,
1020 * so we keep us from multiple reading/parsing
1021 * the pmu format definitions.
1023 pmu = pmu_find(name);
1027 return pmu_lookup(name);
1030 static struct perf_pmu_format *
1031 pmu_find_format(struct list_head *formats, const char *name)
1033 struct perf_pmu_format *format;
1035 list_for_each_entry(format, formats, list)
1036 if (!strcmp(format->name, name))
1042 __u64 perf_pmu__format_bits(struct list_head *formats, const char *name)
1044 struct perf_pmu_format *format = pmu_find_format(formats, name);
1051 for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
1052 bits |= 1ULL << fbit;
1057 int perf_pmu__format_type(struct list_head *formats, const char *name)
1059 struct perf_pmu_format *format = pmu_find_format(formats, name);
1064 return format->value;
1068 * Sets value based on the format definition (format parameter)
1069 * and unformatted value (value parameter).
1071 static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
1074 unsigned long fbit, vbit;
1076 for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
1078 if (!test_bit(fbit, format))
1081 if (value & (1llu << vbit++))
1082 *v |= (1llu << fbit);
1084 *v &= ~(1llu << fbit);
1088 static __u64 pmu_format_max_value(const unsigned long *format)
1092 w = bitmap_weight(format, PERF_PMU_FORMAT_BITS);
1096 return (1ULL << w) - 1;
1101 * Term is a string term, and might be a param-term. Try to look up it's value
1102 * in the remaining terms.
1103 * - We have a term like "base-or-format-term=param-term",
1104 * - We need to find the value supplied for "param-term" (with param-term named
1105 * in a config string) later on in the term list.
1107 static int pmu_resolve_param_term(struct parse_events_term *term,
1108 struct list_head *head_terms,
1111 struct parse_events_term *t;
1113 list_for_each_entry(t, head_terms, list) {
1114 if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM &&
1115 t->config && !strcmp(t->config, term->config)) {
1117 *value = t->val.num;
1123 printf("Required parameter '%s' not specified\n", term->config);
1128 static char *pmu_formats_string(struct list_head *formats)
1130 struct perf_pmu_format *format;
1132 struct strbuf buf = STRBUF_INIT;
1138 /* sysfs exported terms */
1139 list_for_each_entry(format, formats, list)
1140 if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
1143 str = strbuf_detach(&buf, NULL);
1145 strbuf_release(&buf);
1151 * Setup one of config[12] attr members based on the
1152 * user input data - term parameter.
1154 static int pmu_config_term(const char *pmu_name,
1155 struct list_head *formats,
1156 struct perf_event_attr *attr,
1157 struct parse_events_term *term,
1158 struct list_head *head_terms,
1159 bool zero, struct parse_events_error *err)
1161 struct perf_pmu_format *format;
1166 * If this is a parameter we've already used for parameterized-eval,
1167 * skip it in normal eval.
1173 * Hardcoded terms should be already in, so nothing
1174 * to be done for them.
1176 if (parse_events__is_hardcoded_term(term))
1179 format = pmu_find_format(formats, term->config);
1181 char *pmu_term = pmu_formats_string(formats);
1185 if (asprintf(&unknown_term,
1186 "unknown term '%s' for pmu '%s'",
1187 term->config, pmu_name) < 0)
1188 unknown_term = NULL;
1189 help_msg = parse_events_formats_error_string(pmu_term);
1191 parse_events_error__handle(err, term->err_term,
1195 pr_debug("%s (%s)\n", unknown_term, help_msg);
1202 switch (format->value) {
1203 case PERF_PMU_FORMAT_VALUE_CONFIG:
1206 case PERF_PMU_FORMAT_VALUE_CONFIG1:
1207 vp = &attr->config1;
1209 case PERF_PMU_FORMAT_VALUE_CONFIG2:
1210 vp = &attr->config2;
1217 * Either directly use a numeric term, or try to translate string terms
1218 * using event parameters.
1220 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1221 if (term->no_value &&
1222 bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) {
1224 parse_events_error__handle(err, term->err_val,
1225 strdup("no value assigned for term"),
1231 val = term->val.num;
1232 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1233 if (strcmp(term->val.str, "?")) {
1235 pr_info("Invalid sysfs entry %s=%s\n",
1236 term->config, term->val.str);
1239 parse_events_error__handle(err, term->err_val,
1240 strdup("expected numeric value"),
1246 if (pmu_resolve_param_term(term, head_terms, &val))
1251 max_val = pmu_format_max_value(format->bits);
1252 if (val > max_val) {
1256 parse_events_error__handle(err, term->err_val,
1258 "value too big for format, maximum is %llu",
1259 (unsigned long long)max_val) < 0
1260 ? strdup("value too big for format")
1266 * Assume we don't care if !err, in which case the value will be
1267 * silently truncated.
1271 pmu_format_value(format->bits, val, vp, zero);
1275 int perf_pmu__config_terms(const char *pmu_name, struct list_head *formats,
1276 struct perf_event_attr *attr,
1277 struct list_head *head_terms,
1278 bool zero, struct parse_events_error *err)
1280 struct parse_events_term *term;
1282 list_for_each_entry(term, head_terms, list) {
1283 if (pmu_config_term(pmu_name, formats, attr, term, head_terms,
1292 * Configures event's 'attr' parameter based on the:
1293 * 1) users input - specified in terms parameter
1294 * 2) pmu format definitions - specified by pmu parameter
1296 int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
1297 struct list_head *head_terms,
1298 struct parse_events_error *err)
1300 bool zero = !!pmu->default_config;
1302 attr->type = pmu->type;
1303 return perf_pmu__config_terms(pmu->name, &pmu->format, attr,
1304 head_terms, zero, err);
1307 static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
1308 struct parse_events_term *term)
1310 struct perf_pmu_alias *alias;
1313 if (parse_events__is_hardcoded_term(term))
1316 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1317 if (term->val.num != 1)
1319 if (pmu_find_format(&pmu->format, term->config))
1321 name = term->config;
1322 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1323 if (strcasecmp(term->config, "event"))
1325 name = term->val.str;
1330 list_for_each_entry(alias, &pmu->aliases, list) {
1331 if (!strcasecmp(alias->name, name))
1338 static int check_info_data(struct perf_pmu_alias *alias,
1339 struct perf_pmu_info *info)
1342 * Only one term in event definition can
1343 * define unit, scale and snapshot, fail
1344 * if there's more than one.
1346 if ((info->unit && alias->unit[0]) ||
1347 (info->scale && alias->scale) ||
1348 (info->snapshot && alias->snapshot))
1352 info->unit = alias->unit;
1355 info->scale = alias->scale;
1357 if (alias->snapshot)
1358 info->snapshot = alias->snapshot;
1364 * Find alias in the terms list and replace it with the terms
1365 * defined for the alias
1367 int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
1368 struct perf_pmu_info *info)
1370 struct parse_events_term *term, *h;
1371 struct perf_pmu_alias *alias;
1374 info->per_pkg = false;
1377 * Mark unit and scale as not set
1378 * (different from default values, see below)
1382 info->snapshot = false;
1384 list_for_each_entry_safe(term, h, head_terms, list) {
1385 alias = pmu_find_alias(pmu, term);
1388 ret = pmu_alias_terms(alias, &term->list);
1392 ret = check_info_data(alias, info);
1397 info->per_pkg = true;
1399 list_del_init(&term->list);
1400 parse_events_term__delete(term);
1404 * if no unit or scale found in aliases, then
1405 * set defaults as for evsel
1406 * unit cannot left to NULL
1408 if (info->unit == NULL)
1411 if (info->scale == 0.0)
1417 int perf_pmu__new_format(struct list_head *list, char *name,
1418 int config, unsigned long *bits)
1420 struct perf_pmu_format *format;
1422 format = zalloc(sizeof(*format));
1426 format->name = strdup(name);
1427 format->value = config;
1428 memcpy(format->bits, bits, sizeof(format->bits));
1430 list_add_tail(&format->list, list);
1434 void perf_pmu__set_format(unsigned long *bits, long from, long to)
1441 memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
1442 for (b = from; b <= to; b++)
1446 void perf_pmu__del_formats(struct list_head *formats)
1448 struct perf_pmu_format *fmt, *tmp;
1450 list_for_each_entry_safe(fmt, tmp, formats, list) {
1451 list_del(&fmt->list);
1457 static int sub_non_neg(int a, int b)
1464 static char *format_alias(char *buf, int len, const struct perf_pmu *pmu,
1465 const struct perf_pmu_alias *alias)
1467 struct parse_events_term *term;
1468 int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name);
1470 list_for_each_entry(term, &alias->terms, list) {
1471 if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
1472 used += snprintf(buf + used, sub_non_neg(len, used),
1473 ",%s=%s", term->config,
1477 if (sub_non_neg(len, used) > 0) {
1481 if (sub_non_neg(len, used) > 0) {
1485 buf[len - 1] = '\0';
1490 /** Struct for ordering events as output in perf list. */
1492 /** PMU for event. */
1493 const struct perf_pmu *pmu;
1495 * Optional event for name, desc, etc. If not present then this is a
1496 * selectable PMU and the event name is shown as "//".
1498 const struct perf_pmu_alias *event;
1499 /** Is the PMU for the CPU? */
1503 static int cmp_sevent(const void *a, const void *b)
1505 const struct sevent *as = a;
1506 const struct sevent *bs = b;
1507 const char *a_pmu_name, *b_pmu_name;
1508 const char *a_name = "//", *a_desc = NULL, *a_topic = "";
1509 const char *b_name = "//", *b_desc = NULL, *b_topic = "";
1513 a_name = as->event->name;
1514 a_desc = as->event->desc;
1515 a_topic = as->event->topic ?: "";
1518 b_name = bs->event->name;
1519 b_desc = bs->event->desc;
1520 b_topic = bs->event->topic ?: "";
1522 /* Put extra events last. */
1523 if (!!a_desc != !!b_desc)
1524 return !!a_desc - !!b_desc;
1526 /* Order by topics. */
1527 ret = strcmp(a_topic, b_topic);
1531 /* Order CPU core events to be first */
1532 if (as->is_cpu != bs->is_cpu)
1533 return as->is_cpu ? -1 : 1;
1535 /* Order by PMU name. */
1536 a_pmu_name = as->pmu->name ?: "";
1537 b_pmu_name = bs->pmu->name ?: "";
1538 ret = strcmp(a_pmu_name, b_pmu_name);
1542 /* Order by event name. */
1543 return strcmp(a_name, b_name);
1546 bool is_pmu_core(const char *name)
1548 return !strcmp(name, "cpu") || is_arm_pmu_core(name);
1551 static bool pmu_alias_is_duplicate(struct sevent *alias_a,
1552 struct sevent *alias_b)
1554 const char *a_pmu_name, *b_pmu_name;
1555 const char *a_name = alias_a->event ? alias_a->event->name : "//";
1556 const char *b_name = alias_b->event ? alias_b->event->name : "//";
1558 /* Different names -> never duplicates */
1559 if (strcmp(a_name, b_name))
1562 /* Don't remove duplicates for different PMUs */
1563 a_pmu_name = alias_a->pmu->name ?: "";
1564 b_pmu_name = alias_b->pmu->name ?: "";
1565 return strcmp(a_pmu_name, b_pmu_name) == 0;
1568 void print_pmu_events(const struct print_callbacks *print_cb, void *print_state)
1570 struct perf_pmu *pmu;
1571 struct perf_pmu_alias *event;
1575 struct sevent *aliases;
1579 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1580 list_for_each_entry(event, &pmu->aliases, list)
1582 if (pmu->selectable)
1585 aliases = zalloc(sizeof(struct sevent) * len);
1587 pr_err("FATAL: not enough memory to print PMU events\n");
1592 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1593 bool is_cpu = is_pmu_core(pmu->name) || perf_pmu__is_hybrid(pmu->name);
1595 list_for_each_entry(event, &pmu->aliases, list) {
1596 aliases[j].event = event;
1597 aliases[j].pmu = pmu;
1598 aliases[j].is_cpu = is_cpu;
1601 if (pmu->selectable) {
1602 aliases[j].event = NULL;
1603 aliases[j].pmu = pmu;
1604 aliases[j].is_cpu = is_cpu;
1609 qsort(aliases, len, sizeof(struct sevent), cmp_sevent);
1610 for (j = 0; j < len; j++) {
1611 const char *name, *alias = NULL, *scale_unit = NULL,
1612 *desc = NULL, *long_desc = NULL,
1613 *encoding_desc = NULL, *topic = NULL;
1614 bool deprecated = false;
1617 /* Skip duplicates */
1618 if (j > 0 && pmu_alias_is_duplicate(&aliases[j], &aliases[j - 1]))
1621 if (!aliases[j].event) {
1622 /* A selectable event. */
1623 buf_used = snprintf(buf, sizeof(buf), "%s//", aliases[j].pmu->name) + 1;
1626 if (aliases[j].event->desc) {
1627 name = aliases[j].event->name;
1630 name = format_alias(buf, sizeof(buf), aliases[j].pmu,
1632 if (aliases[j].is_cpu) {
1634 name = aliases[j].event->name;
1636 buf_used = strlen(buf) + 1;
1638 if (strlen(aliases[j].event->unit) || aliases[j].event->scale != 1.0) {
1639 scale_unit = buf + buf_used;
1640 buf_used += snprintf(buf + buf_used, sizeof(buf) - buf_used,
1641 "%G%s", aliases[j].event->scale,
1642 aliases[j].event->unit) + 1;
1644 desc = aliases[j].event->desc;
1645 long_desc = aliases[j].event->long_desc;
1646 topic = aliases[j].event->topic;
1647 encoding_desc = buf + buf_used;
1648 buf_used += snprintf(buf + buf_used, sizeof(buf) - buf_used,
1649 "%s/%s/", aliases[j].pmu->name,
1650 aliases[j].event->str) + 1;
1651 deprecated = aliases[j].event->deprecated;
1653 print_cb->print_event(print_state,
1654 aliases[j].pmu->name,
1665 if (printed && pager_in_use())
1672 bool pmu_have_event(const char *pname, const char *name)
1674 struct perf_pmu *pmu;
1675 struct perf_pmu_alias *alias;
1678 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1679 if (strcmp(pname, pmu->name))
1681 list_for_each_entry(alias, &pmu->aliases, list)
1682 if (!strcmp(alias->name, name))
1688 FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
1690 char path[PATH_MAX];
1692 if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, name) ||
1693 !file_available(path))
1696 return fopen(path, "r");
1699 int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
1706 va_start(args, fmt);
1707 file = perf_pmu__open_file(pmu, name);
1709 ret = vfscanf(file, fmt, args);
1716 bool perf_pmu__file_exists(struct perf_pmu *pmu, const char *name)
1718 char path[PATH_MAX];
1720 if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, name))
1723 return file_available(path);
1726 static int perf_pmu__new_caps(struct list_head *list, char *name, char *value)
1728 struct perf_pmu_caps *caps = zalloc(sizeof(*caps));
1733 caps->name = strdup(name);
1736 caps->value = strndup(value, strlen(value) - 1);
1739 list_add_tail(&caps->list, list);
1751 * Reading/parsing the given pmu capabilities, which should be located at:
1752 * /sys/bus/event_source/devices/<dev>/caps as sysfs group attributes.
1753 * Return the number of capabilities
1755 int perf_pmu__caps_parse(struct perf_pmu *pmu)
1758 char caps_path[PATH_MAX];
1760 struct dirent *evt_ent;
1762 if (pmu->caps_initialized)
1763 return pmu->nr_caps;
1767 if (!perf_pmu__pathname_scnprintf(caps_path, sizeof(caps_path), pmu->name, "caps"))
1770 if (stat(caps_path, &st) < 0) {
1771 pmu->caps_initialized = true;
1772 return 0; /* no error if caps does not exist */
1775 caps_dir = opendir(caps_path);
1779 while ((evt_ent = readdir(caps_dir)) != NULL) {
1780 char path[PATH_MAX + NAME_MAX + 1];
1781 char *name = evt_ent->d_name;
1785 if (!strcmp(name, ".") || !strcmp(name, ".."))
1788 snprintf(path, sizeof(path), "%s/%s", caps_path, name);
1790 file = fopen(path, "r");
1794 if (!fgets(value, sizeof(value), file) ||
1795 (perf_pmu__new_caps(&pmu->caps, name, value) < 0)) {
1806 pmu->caps_initialized = true;
1807 return pmu->nr_caps;
1810 void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config,
1813 struct perf_pmu_format *format;
1814 __u64 masks = 0, bits;
1818 list_for_each_entry(format, &pmu->format, list) {
1819 if (format->value != PERF_PMU_FORMAT_VALUE_CONFIG)
1822 for_each_set_bit(i, format->bits, PERF_PMU_FORMAT_BITS)
1827 * Kernel doesn't export any valid format bits.
1832 bits = config & ~masks;
1836 bitmap_scnprintf((unsigned long *)&bits, sizeof(bits) * 8, buf, sizeof(buf));
1838 pr_warning("WARNING: event '%s' not valid (bits %s of config "
1839 "'%llx' not supported by kernel)!\n",
1840 name ?: "N/A", buf, config);
1843 bool perf_pmu__has_hybrid(void)
1845 if (!hybrid_scanned) {
1846 hybrid_scanned = true;
1847 perf_pmu__scan(NULL);
1850 return !list_empty(&perf_pmu__hybrid_pmus);
1853 int perf_pmu__match(char *pattern, char *name, char *tok)
1858 if (fnmatch(pattern, name, 0))
1861 if (tok && !perf_pmu__valid_suffix(name, tok))
1867 int perf_pmu__cpus_match(struct perf_pmu *pmu, struct perf_cpu_map *cpus,
1868 struct perf_cpu_map **mcpus_ptr,
1869 struct perf_cpu_map **ucpus_ptr)
1871 struct perf_cpu_map *pmu_cpus = pmu->cpus;
1872 struct perf_cpu_map *matched_cpus, *unmatched_cpus;
1873 struct perf_cpu cpu;
1874 int i, matched_nr = 0, unmatched_nr = 0;
1876 matched_cpus = perf_cpu_map__default_new();
1880 unmatched_cpus = perf_cpu_map__default_new();
1881 if (!unmatched_cpus) {
1882 perf_cpu_map__put(matched_cpus);
1886 perf_cpu_map__for_each_cpu(cpu, i, cpus) {
1887 if (!perf_cpu_map__has(pmu_cpus, cpu))
1888 unmatched_cpus->map[unmatched_nr++] = cpu;
1890 matched_cpus->map[matched_nr++] = cpu;
1893 unmatched_cpus->nr = unmatched_nr;
1894 matched_cpus->nr = matched_nr;
1895 *mcpus_ptr = matched_cpus;
1896 *ucpus_ptr = unmatched_cpus;
1900 double __weak perf_pmu__cpu_slots_per_cycle(void)
1905 int perf_pmu__event_source_devices_scnprintf(char *pathname, size_t size)
1907 const char *sysfs = sysfs__mountpoint();
1911 return scnprintf(pathname, size, "%s/bus/event_source/devices/", sysfs);
1915 * Fill 'buf' with the path to a file or folder in 'pmu_name' in
1916 * sysfs. For example if pmu_name = "cs_etm" and 'filename' = "format"
1917 * then pathname will be filled with
1918 * "/sys/bus/event_source/devices/cs_etm/format"
1920 * Return 0 if the sysfs mountpoint couldn't be found or if no
1921 * characters were written.
1923 int perf_pmu__pathname_scnprintf(char *buf, size_t size,
1924 const char *pmu_name, const char *filename)
1926 char base_path[PATH_MAX];
1928 if (!perf_pmu__event_source_devices_scnprintf(base_path, sizeof(base_path)))
1930 return scnprintf(buf, size, "%s%s/%s", base_path, pmu_name, filename);