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"
29 struct perf_pmu perf_pmu__fake;
31 struct perf_pmu_format {
34 DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
35 struct list_head list;
38 int perf_pmu_parse(struct list_head *list, char *name);
39 extern FILE *perf_pmu_in;
41 static LIST_HEAD(pmus);
44 * Parse & process all the sysfs attributes located under
45 * the directory specified in 'dir' parameter.
47 int perf_pmu__format_parse(char *dir, struct list_head *head)
49 struct dirent *evt_ent;
53 format_dir = opendir(dir);
57 while (!ret && (evt_ent = readdir(format_dir))) {
59 char *name = evt_ent->d_name;
62 if (!strcmp(name, ".") || !strcmp(name, ".."))
65 snprintf(path, PATH_MAX, "%s/%s", dir, name);
68 file = fopen(path, "r");
73 ret = perf_pmu_parse(head, name);
82 * Reading/parsing the default pmu format definition, which should be
84 * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
86 static int pmu_format(const char *name, struct list_head *format)
89 const char *sysfs = sysfs__mountpoint();
94 snprintf(path, PATH_MAX,
95 "%s" EVENT_SOURCE_DEVICE_PATH "%s/format", sysfs, name);
97 if (!file_available(path))
100 if (perf_pmu__format_parse(path, format))
106 int perf_pmu__convert_scale(const char *scale, char **end, double *sval)
112 * save current locale
114 lc = setlocale(LC_NUMERIC, NULL);
117 * The lc string may be allocated in static storage,
118 * so get a dynamic copy to make it survive setlocale
128 * force to C locale to ensure kernel
129 * scale string is converted correctly.
130 * kernel uses default C locale.
132 setlocale(LC_NUMERIC, "C");
134 *sval = strtod(scale, end);
138 setlocale(LC_NUMERIC, lc);
143 static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *name)
151 scnprintf(path, PATH_MAX, "%s/%s.scale", dir, name);
153 fd = open(path, O_RDONLY);
157 if (fstat(fd, &st) < 0)
160 sret = read(fd, scale, sizeof(scale)-1);
164 if (scale[sret - 1] == '\n')
165 scale[sret - 1] = '\0';
169 ret = perf_pmu__convert_scale(scale, NULL, &alias->scale);
175 static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *name)
181 scnprintf(path, PATH_MAX, "%s/%s.unit", dir, name);
183 fd = open(path, O_RDONLY);
187 sret = read(fd, alias->unit, UNIT_MAX_LEN);
193 if (alias->unit[sret - 1] == '\n')
194 alias->unit[sret - 1] = '\0';
196 alias->unit[sret] = '\0';
201 alias->unit[0] = '\0';
206 perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, char *dir, char *name)
211 scnprintf(path, PATH_MAX, "%s/%s.per-pkg", dir, name);
213 fd = open(path, O_RDONLY);
219 alias->per_pkg = true;
223 static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias,
224 char *dir, char *name)
229 scnprintf(path, PATH_MAX, "%s/%s.snapshot", dir, name);
231 fd = open(path, O_RDONLY);
235 alias->snapshot = true;
240 static void perf_pmu_assign_str(char *name, const char *field, char **old_str,
246 if (*new_str) { /* Have new string, check with old */
247 if (strcasecmp(*old_str, *new_str))
248 pr_debug("alias %s differs in field '%s'\n",
251 } else /* Nothing new --> keep old string */
258 static void perf_pmu_update_alias(struct perf_pmu_alias *old,
259 struct perf_pmu_alias *newalias)
261 perf_pmu_assign_str(old->name, "desc", &old->desc, &newalias->desc);
262 perf_pmu_assign_str(old->name, "long_desc", &old->long_desc,
263 &newalias->long_desc);
264 perf_pmu_assign_str(old->name, "topic", &old->topic, &newalias->topic);
265 perf_pmu_assign_str(old->name, "metric_expr", &old->metric_expr,
266 &newalias->metric_expr);
267 perf_pmu_assign_str(old->name, "metric_name", &old->metric_name,
268 &newalias->metric_name);
269 perf_pmu_assign_str(old->name, "value", &old->str, &newalias->str);
270 old->scale = newalias->scale;
271 old->per_pkg = newalias->per_pkg;
272 old->snapshot = newalias->snapshot;
273 memcpy(old->unit, newalias->unit, sizeof(old->unit));
276 /* Delete an alias entry. */
277 void perf_pmu_free_alias(struct perf_pmu_alias *newalias)
279 zfree(&newalias->name);
280 zfree(&newalias->desc);
281 zfree(&newalias->long_desc);
282 zfree(&newalias->topic);
283 zfree(&newalias->str);
284 zfree(&newalias->metric_expr);
285 zfree(&newalias->metric_name);
286 parse_events_terms__purge(&newalias->terms);
290 /* Merge an alias, search in alias list. If this name is already
291 * present merge both of them to combine all information.
293 static bool perf_pmu_merge_alias(struct perf_pmu_alias *newalias,
294 struct list_head *alist)
296 struct perf_pmu_alias *a;
298 list_for_each_entry(a, alist, list) {
299 if (!strcasecmp(newalias->name, a->name)) {
300 perf_pmu_update_alias(a, newalias);
301 perf_pmu_free_alias(newalias);
308 static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name,
309 char *desc, char *val,
310 char *long_desc, char *topic,
311 char *unit, char *perpkg,
316 struct parse_events_term *term;
317 struct perf_pmu_alias *alias;
322 alias = malloc(sizeof(*alias));
326 INIT_LIST_HEAD(&alias->terms);
328 alias->unit[0] = '\0';
329 alias->per_pkg = false;
330 alias->snapshot = false;
331 alias->deprecated = false;
333 ret = parse_events_terms(&alias->terms, val);
335 pr_err("Cannot parse alias %s: %d\n", val, ret);
340 /* Scan event and remove leading zeroes, spaces, newlines, some
341 * platforms have terms specified as
342 * event=0x0091 (read from files ../<PMU>/events/<FILE>
343 * and terms specified as event=0x91 (read from JSON files).
345 * Rebuild string to make alias->str member comparable.
347 memset(newval, 0, sizeof(newval));
349 list_for_each_entry(term, &alias->terms, list) {
351 ret += scnprintf(newval + ret, sizeof(newval) - ret,
353 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM)
354 ret += scnprintf(newval + ret, sizeof(newval) - ret,
355 "%s=%#x", term->config, term->val.num);
356 else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
357 ret += scnprintf(newval + ret, sizeof(newval) - ret,
358 "%s=%s", term->config, term->val.str);
361 alias->name = strdup(name);
364 * load unit name and scale if available
366 perf_pmu__parse_unit(alias, dir, name);
367 perf_pmu__parse_scale(alias, dir, name);
368 perf_pmu__parse_per_pkg(alias, dir, name);
369 perf_pmu__parse_snapshot(alias, dir, name);
372 alias->metric_expr = metric_expr ? strdup(metric_expr) : NULL;
373 alias->metric_name = metric_name ? strdup(metric_name): NULL;
374 alias->desc = desc ? strdup(desc) : NULL;
375 alias->long_desc = long_desc ? strdup(long_desc) :
376 desc ? strdup(desc) : NULL;
377 alias->topic = topic ? strdup(topic) : NULL;
379 if (perf_pmu__convert_scale(unit, &unit, &alias->scale) < 0)
381 snprintf(alias->unit, sizeof(alias->unit), "%s", unit);
383 alias->per_pkg = perpkg && sscanf(perpkg, "%d", &num) == 1 && num == 1;
384 alias->str = strdup(newval);
387 alias->deprecated = true;
389 if (!perf_pmu_merge_alias(alias, list))
390 list_add_tail(&alias->list, list);
395 static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file)
400 ret = fread(buf, 1, sizeof(buf), file);
406 /* Remove trailing newline from sysfs file */
409 return __perf_pmu__new_alias(list, dir, name, NULL, buf, NULL, NULL, NULL,
410 NULL, NULL, NULL, NULL);
413 static inline bool pmu_alias_info_file(char *name)
418 if (len > 5 && !strcmp(name + len - 5, ".unit"))
420 if (len > 6 && !strcmp(name + len - 6, ".scale"))
422 if (len > 8 && !strcmp(name + len - 8, ".per-pkg"))
424 if (len > 9 && !strcmp(name + len - 9, ".snapshot"))
431 * Process all the sysfs attributes located under the directory
432 * specified in 'dir' parameter.
434 static int pmu_aliases_parse(char *dir, struct list_head *head)
436 struct dirent *evt_ent;
439 event_dir = opendir(dir);
443 while ((evt_ent = readdir(event_dir))) {
445 char *name = evt_ent->d_name;
448 if (!strcmp(name, ".") || !strcmp(name, ".."))
452 * skip info files parsed in perf_pmu__new_alias()
454 if (pmu_alias_info_file(name))
457 scnprintf(path, PATH_MAX, "%s/%s", dir, name);
459 file = fopen(path, "r");
461 pr_debug("Cannot open %s\n", path);
465 if (perf_pmu__new_alias(head, dir, name, file) < 0)
466 pr_debug("Cannot set up %s\n", name);
475 * Reading the pmu event aliases definition, which should be located at:
476 * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
478 static int pmu_aliases(const char *name, struct list_head *head)
481 const char *sysfs = sysfs__mountpoint();
486 snprintf(path, PATH_MAX,
487 "%s/bus/event_source/devices/%s/events", sysfs, name);
489 if (!file_available(path))
492 if (pmu_aliases_parse(path, head))
498 static int pmu_alias_terms(struct perf_pmu_alias *alias,
499 struct list_head *terms)
501 struct parse_events_term *term, *cloned;
505 list_for_each_entry(term, &alias->terms, list) {
506 ret = parse_events_term__clone(&cloned, term);
508 parse_events_terms__purge(&list);
512 * Weak terms don't override command line options,
513 * which we don't want for implicit terms in aliases.
516 list_add_tail(&cloned->list, &list);
518 list_splice(&list, terms);
523 * Reading/parsing the default pmu type value, which should be
525 * /sys/bus/event_source/devices/<dev>/type as sysfs attribute.
527 static int pmu_type(const char *name, __u32 *type)
532 const char *sysfs = sysfs__mountpoint();
537 snprintf(path, PATH_MAX,
538 "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name);
540 if (access(path, R_OK) < 0)
543 file = fopen(path, "r");
547 if (1 != fscanf(file, "%u", type))
554 /* Add all pmus in sysfs to pmu list: */
555 static void pmu_read_sysfs(void)
560 const char *sysfs = sysfs__mountpoint();
565 snprintf(path, PATH_MAX,
566 "%s" EVENT_SOURCE_DEVICE_PATH, sysfs);
572 while ((dent = readdir(dir))) {
573 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
575 /* add to static LIST_HEAD(pmus): */
576 perf_pmu__find(dent->d_name);
582 static struct perf_cpu_map *__pmu_cpumask(const char *path)
585 struct perf_cpu_map *cpus;
587 file = fopen(path, "r");
591 cpus = perf_cpu_map__read(file);
597 * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64)
598 * may have a "cpus" file.
600 #define SYS_TEMPLATE_ID "./bus/event_source/devices/%s/identifier"
601 #define CPUS_TEMPLATE_UNCORE "%s/bus/event_source/devices/%s/cpumask"
602 #define CPUS_TEMPLATE_CPU "%s/bus/event_source/devices/%s/cpus"
604 static struct perf_cpu_map *pmu_cpumask(const char *name)
607 struct perf_cpu_map *cpus;
608 const char *sysfs = sysfs__mountpoint();
609 const char *templates[] = {
610 CPUS_TEMPLATE_UNCORE,
614 const char **template;
619 for (template = templates; *template; template++) {
620 snprintf(path, PATH_MAX, *template, sysfs, name);
621 cpus = __pmu_cpumask(path);
629 static bool pmu_is_uncore(const char *name)
634 sysfs = sysfs__mountpoint();
635 snprintf(path, PATH_MAX, CPUS_TEMPLATE_UNCORE, sysfs, name);
636 return file_available(path);
639 static char *pmu_id(const char *name)
641 char path[PATH_MAX], *str;
644 snprintf(path, PATH_MAX, SYS_TEMPLATE_ID, name);
646 if (sysfs__read_str(path, &str, &len) < 0)
649 str[len - 1] = 0; /* remove line feed */
655 * PMU CORE devices have different name other than cpu in sysfs on some
657 * Looking for possible sysfs files to identify the arm core device.
659 static int is_arm_pmu_core(const char *name)
662 const char *sysfs = sysfs__mountpoint();
667 /* Look for cpu sysfs (specific to arm) */
668 scnprintf(path, PATH_MAX, "%s/bus/event_source/devices/%s/cpus",
670 return file_available(path);
673 static char *perf_pmu__getcpuid(struct perf_pmu *pmu)
678 cpuid = getenv("PERF_CPUID");
680 cpuid = strdup(cpuid);
682 cpuid = get_cpuid_str(pmu);
687 pr_debug("Using CPUID %s\n", cpuid);
693 struct pmu_events_map *perf_pmu__find_map(struct perf_pmu *pmu)
695 struct pmu_events_map *map;
696 char *cpuid = perf_pmu__getcpuid(pmu);
699 /* on some platforms which uses cpus map, cpuid can be NULL for
700 * PMUs other than CORE PMUs.
707 map = &pmu_events_map[i++];
713 if (!strcmp_cpuid_str(map->cpuid, cpuid))
720 bool pmu_uncore_alias_match(const char *pmu_name, const char *name)
722 char *tmp = NULL, *tok, *str;
725 str = strdup(pmu_name);
730 * uncore alias may be from different PMU with common prefix
732 tok = strtok_r(str, ",", &tmp);
733 if (strncmp(pmu_name, tok, strlen(tok))) {
739 * Match more complex aliases where the alias name is a comma-delimited
740 * list of tokens, orderly contained in the matching PMU name.
742 * Example: For alias "socket,pmuname" and PMU "socketX_pmunameY", we
743 * match "socket" in "socketX_pmunameY" and then "pmuname" in
746 for (; tok; name += strlen(tok), tok = strtok_r(NULL, ",", &tmp)) {
747 name = strstr(name, tok);
761 * From the pmu_events_map, find the table of PMU events that corresponds
762 * to the current running CPU. Then, add all PMU events from that table
765 void pmu_add_cpu_aliases_map(struct list_head *head, struct perf_pmu *pmu,
766 struct pmu_events_map *map)
769 const char *name = pmu->name;
771 * Found a matching PMU events table. Create aliases
775 const char *cpu_name = is_arm_pmu_core(name) ? name : "cpu";
776 struct pmu_event *pe = &map->table[i++];
777 const char *pname = pe->pmu ? pe->pmu : cpu_name;
780 if (pe->metric_group || pe->metric_name)
785 if (pmu_is_uncore(name) &&
786 pmu_uncore_alias_match(pname, name))
789 if (strcmp(pname, name))
793 /* need type casts to override 'const' */
794 __perf_pmu__new_alias(head, NULL, (char *)pe->name,
795 (char *)pe->desc, (char *)pe->event,
796 (char *)pe->long_desc, (char *)pe->topic,
797 (char *)pe->unit, (char *)pe->perpkg,
798 (char *)pe->metric_expr,
799 (char *)pe->metric_name,
800 (char *)pe->deprecated);
804 static void pmu_add_cpu_aliases(struct list_head *head, struct perf_pmu *pmu)
806 struct pmu_events_map *map;
808 map = perf_pmu__find_map(pmu);
812 pmu_add_cpu_aliases_map(head, pmu, map);
815 struct perf_event_attr * __weak
816 perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
821 static int pmu_max_precise(const char *name)
824 int max_precise = -1;
826 scnprintf(path, PATH_MAX,
827 "bus/event_source/devices/%s/caps/max_precise",
830 sysfs__read_int(path, &max_precise);
834 static struct perf_pmu *pmu_lookup(const char *name)
836 struct perf_pmu *pmu;
842 * The pmu data we store & need consists of the pmu
843 * type value and format definitions. Load both right
846 if (pmu_format(name, &format))
850 * Check the type first to avoid unnecessary work.
852 if (pmu_type(name, &type))
855 if (pmu_aliases(name, &aliases))
858 pmu = zalloc(sizeof(*pmu));
862 pmu->cpus = pmu_cpumask(name);
863 pmu->name = strdup(name);
865 pmu->is_uncore = pmu_is_uncore(name);
867 pmu->id = pmu_id(name);
868 pmu->max_precise = pmu_max_precise(name);
869 pmu_add_cpu_aliases(&aliases, pmu);
871 INIT_LIST_HEAD(&pmu->format);
872 INIT_LIST_HEAD(&pmu->aliases);
873 INIT_LIST_HEAD(&pmu->caps);
874 list_splice(&format, &pmu->format);
875 list_splice(&aliases, &pmu->aliases);
876 list_add_tail(&pmu->list, &pmus);
878 pmu->default_config = perf_pmu__get_default_config(pmu);
883 static struct perf_pmu *pmu_find(const char *name)
885 struct perf_pmu *pmu;
887 list_for_each_entry(pmu, &pmus, list)
888 if (!strcmp(pmu->name, name))
894 struct perf_pmu *perf_pmu__find_by_type(unsigned int type)
896 struct perf_pmu *pmu;
898 list_for_each_entry(pmu, &pmus, list)
899 if (pmu->type == type)
905 struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
908 * pmu iterator: If pmu is NULL, we start at the begin,
909 * otherwise return the next pmu. Returns NULL on end.
913 pmu = list_prepare_entry(pmu, &pmus, list);
915 list_for_each_entry_continue(pmu, &pmus, list)
920 struct perf_pmu *evsel__find_pmu(struct evsel *evsel)
922 struct perf_pmu *pmu = NULL;
924 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
925 if (pmu->type == evsel->core.attr.type)
932 bool evsel__is_aux_event(struct evsel *evsel)
934 struct perf_pmu *pmu = evsel__find_pmu(evsel);
936 return pmu && pmu->auxtrace;
939 struct perf_pmu *perf_pmu__find(const char *name)
941 struct perf_pmu *pmu;
944 * Once PMU is loaded it stays in the list,
945 * so we keep us from multiple reading/parsing
946 * the pmu format definitions.
948 pmu = pmu_find(name);
952 return pmu_lookup(name);
955 static struct perf_pmu_format *
956 pmu_find_format(struct list_head *formats, const char *name)
958 struct perf_pmu_format *format;
960 list_for_each_entry(format, formats, list)
961 if (!strcmp(format->name, name))
967 __u64 perf_pmu__format_bits(struct list_head *formats, const char *name)
969 struct perf_pmu_format *format = pmu_find_format(formats, name);
976 for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
977 bits |= 1ULL << fbit;
982 int perf_pmu__format_type(struct list_head *formats, const char *name)
984 struct perf_pmu_format *format = pmu_find_format(formats, name);
989 return format->value;
993 * Sets value based on the format definition (format parameter)
994 * and unformated value (value parameter).
996 static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
999 unsigned long fbit, vbit;
1001 for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
1003 if (!test_bit(fbit, format))
1006 if (value & (1llu << vbit++))
1007 *v |= (1llu << fbit);
1009 *v &= ~(1llu << fbit);
1013 static __u64 pmu_format_max_value(const unsigned long *format)
1017 w = bitmap_weight(format, PERF_PMU_FORMAT_BITS);
1021 return (1ULL << w) - 1;
1026 * Term is a string term, and might be a param-term. Try to look up it's value
1027 * in the remaining terms.
1028 * - We have a term like "base-or-format-term=param-term",
1029 * - We need to find the value supplied for "param-term" (with param-term named
1030 * in a config string) later on in the term list.
1032 static int pmu_resolve_param_term(struct parse_events_term *term,
1033 struct list_head *head_terms,
1036 struct parse_events_term *t;
1038 list_for_each_entry(t, head_terms, list) {
1039 if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM &&
1040 t->config && !strcmp(t->config, term->config)) {
1042 *value = t->val.num;
1048 printf("Required parameter '%s' not specified\n", term->config);
1053 static char *pmu_formats_string(struct list_head *formats)
1055 struct perf_pmu_format *format;
1057 struct strbuf buf = STRBUF_INIT;
1063 /* sysfs exported terms */
1064 list_for_each_entry(format, formats, list)
1065 if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
1068 str = strbuf_detach(&buf, NULL);
1070 strbuf_release(&buf);
1076 * Setup one of config[12] attr members based on the
1077 * user input data - term parameter.
1079 static int pmu_config_term(const char *pmu_name,
1080 struct list_head *formats,
1081 struct perf_event_attr *attr,
1082 struct parse_events_term *term,
1083 struct list_head *head_terms,
1084 bool zero, struct parse_events_error *err)
1086 struct perf_pmu_format *format;
1091 * If this is a parameter we've already used for parameterized-eval,
1092 * skip it in normal eval.
1098 * Hardcoded terms should be already in, so nothing
1099 * to be done for them.
1101 if (parse_events__is_hardcoded_term(term))
1104 format = pmu_find_format(formats, term->config);
1106 char *pmu_term = pmu_formats_string(formats);
1110 if (asprintf(&unknown_term,
1111 "unknown term '%s' for pmu '%s'",
1112 term->config, pmu_name) < 0)
1113 unknown_term = NULL;
1114 help_msg = parse_events_formats_error_string(pmu_term);
1116 parse_events__handle_error(err, term->err_term,
1120 pr_debug("%s (%s)\n", unknown_term, help_msg);
1127 switch (format->value) {
1128 case PERF_PMU_FORMAT_VALUE_CONFIG:
1131 case PERF_PMU_FORMAT_VALUE_CONFIG1:
1132 vp = &attr->config1;
1134 case PERF_PMU_FORMAT_VALUE_CONFIG2:
1135 vp = &attr->config2;
1142 * Either directly use a numeric term, or try to translate string terms
1143 * using event parameters.
1145 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1146 if (term->no_value &&
1147 bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) {
1149 parse_events__handle_error(err, term->err_val,
1150 strdup("no value assigned for term"),
1156 val = term->val.num;
1157 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1158 if (strcmp(term->val.str, "?")) {
1160 pr_info("Invalid sysfs entry %s=%s\n",
1161 term->config, term->val.str);
1164 parse_events__handle_error(err, term->err_val,
1165 strdup("expected numeric value"),
1171 if (pmu_resolve_param_term(term, head_terms, &val))
1176 max_val = pmu_format_max_value(format->bits);
1177 if (val > max_val) {
1181 parse_events__handle_error(err, term->err_val,
1183 "value too big for format, maximum is %llu",
1184 (unsigned long long)max_val) < 0
1185 ? strdup("value too big for format")
1191 * Assume we don't care if !err, in which case the value will be
1192 * silently truncated.
1196 pmu_format_value(format->bits, val, vp, zero);
1200 int perf_pmu__config_terms(const char *pmu_name, struct list_head *formats,
1201 struct perf_event_attr *attr,
1202 struct list_head *head_terms,
1203 bool zero, struct parse_events_error *err)
1205 struct parse_events_term *term;
1207 list_for_each_entry(term, head_terms, list) {
1208 if (pmu_config_term(pmu_name, formats, attr, term, head_terms,
1217 * Configures event's 'attr' parameter based on the:
1218 * 1) users input - specified in terms parameter
1219 * 2) pmu format definitions - specified by pmu parameter
1221 int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
1222 struct list_head *head_terms,
1223 struct parse_events_error *err)
1225 bool zero = !!pmu->default_config;
1227 attr->type = pmu->type;
1228 return perf_pmu__config_terms(pmu->name, &pmu->format, attr,
1229 head_terms, zero, err);
1232 static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
1233 struct parse_events_term *term)
1235 struct perf_pmu_alias *alias;
1238 if (parse_events__is_hardcoded_term(term))
1241 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1242 if (term->val.num != 1)
1244 if (pmu_find_format(&pmu->format, term->config))
1246 name = term->config;
1247 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1248 if (strcasecmp(term->config, "event"))
1250 name = term->val.str;
1255 list_for_each_entry(alias, &pmu->aliases, list) {
1256 if (!strcasecmp(alias->name, name))
1263 static int check_info_data(struct perf_pmu_alias *alias,
1264 struct perf_pmu_info *info)
1267 * Only one term in event definition can
1268 * define unit, scale and snapshot, fail
1269 * if there's more than one.
1271 if ((info->unit && alias->unit[0]) ||
1272 (info->scale && alias->scale) ||
1273 (info->snapshot && alias->snapshot))
1277 info->unit = alias->unit;
1280 info->scale = alias->scale;
1282 if (alias->snapshot)
1283 info->snapshot = alias->snapshot;
1289 * Find alias in the terms list and replace it with the terms
1290 * defined for the alias
1292 int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
1293 struct perf_pmu_info *info)
1295 struct parse_events_term *term, *h;
1296 struct perf_pmu_alias *alias;
1299 info->per_pkg = false;
1302 * Mark unit and scale as not set
1303 * (different from default values, see below)
1307 info->snapshot = false;
1308 info->metric_expr = NULL;
1309 info->metric_name = NULL;
1311 list_for_each_entry_safe(term, h, head_terms, list) {
1312 alias = pmu_find_alias(pmu, term);
1315 ret = pmu_alias_terms(alias, &term->list);
1319 ret = check_info_data(alias, info);
1324 info->per_pkg = true;
1325 info->metric_expr = alias->metric_expr;
1326 info->metric_name = alias->metric_name;
1328 list_del_init(&term->list);
1329 parse_events_term__delete(term);
1333 * if no unit or scale foundin aliases, then
1334 * set defaults as for evsel
1335 * unit cannot left to NULL
1337 if (info->unit == NULL)
1340 if (info->scale == 0.0)
1346 int perf_pmu__new_format(struct list_head *list, char *name,
1347 int config, unsigned long *bits)
1349 struct perf_pmu_format *format;
1351 format = zalloc(sizeof(*format));
1355 format->name = strdup(name);
1356 format->value = config;
1357 memcpy(format->bits, bits, sizeof(format->bits));
1359 list_add_tail(&format->list, list);
1363 void perf_pmu__set_format(unsigned long *bits, long from, long to)
1370 memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
1371 for (b = from; b <= to; b++)
1375 void perf_pmu__del_formats(struct list_head *formats)
1377 struct perf_pmu_format *fmt, *tmp;
1379 list_for_each_entry_safe(fmt, tmp, formats, list) {
1380 list_del(&fmt->list);
1386 static int sub_non_neg(int a, int b)
1393 static char *format_alias(char *buf, int len, struct perf_pmu *pmu,
1394 struct perf_pmu_alias *alias)
1396 struct parse_events_term *term;
1397 int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name);
1399 list_for_each_entry(term, &alias->terms, list) {
1400 if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
1401 used += snprintf(buf + used, sub_non_neg(len, used),
1402 ",%s=%s", term->config,
1406 if (sub_non_neg(len, used) > 0) {
1410 if (sub_non_neg(len, used) > 0) {
1414 buf[len - 1] = '\0';
1419 static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu,
1420 struct perf_pmu_alias *alias)
1422 snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name);
1437 static int cmp_sevent(const void *a, const void *b)
1439 const struct sevent *as = a;
1440 const struct sevent *bs = b;
1442 /* Put extra events last */
1443 if (!!as->desc != !!bs->desc)
1444 return !!as->desc - !!bs->desc;
1445 if (as->topic && bs->topic) {
1446 int n = strcmp(as->topic, bs->topic);
1452 /* Order CPU core events to be first */
1453 if (as->is_cpu != bs->is_cpu)
1454 return bs->is_cpu - as->is_cpu;
1456 return strcmp(as->name, bs->name);
1459 static void wordwrap(char *s, int start, int max, int corr)
1465 int wlen = strcspn(s, " \t");
1467 if (column + wlen >= max && column > start) {
1468 printf("\n%*s", start, "");
1469 column = start + corr;
1471 n = printf("%s%.*s", column > start ? " " : "", wlen, s);
1480 bool is_pmu_core(const char *name)
1482 return !strcmp(name, "cpu") || is_arm_pmu_core(name);
1485 void print_pmu_events(const char *event_glob, bool name_only, bool quiet_flag,
1486 bool long_desc, bool details_flag, bool deprecated)
1488 struct perf_pmu *pmu;
1489 struct perf_pmu_alias *alias;
1493 struct sevent *aliases;
1495 int columns = pager_get_columns();
1500 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1501 list_for_each_entry(alias, &pmu->aliases, list)
1503 if (pmu->selectable)
1506 aliases = zalloc(sizeof(struct sevent) * len);
1511 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1512 list_for_each_entry(alias, &pmu->aliases, list) {
1513 char *name = alias->desc ? alias->name :
1514 format_alias(buf, sizeof(buf), pmu, alias);
1515 bool is_cpu = is_pmu_core(pmu->name);
1517 if (alias->deprecated && !deprecated)
1520 if (event_glob != NULL &&
1521 !(strglobmatch_nocase(name, event_glob) ||
1522 (!is_cpu && strglobmatch_nocase(alias->name,
1525 strglobmatch_nocase(alias->topic, event_glob))))
1528 if (is_cpu && !name_only && !alias->desc)
1529 name = format_alias_or(buf, sizeof(buf), pmu, alias);
1531 aliases[j].name = name;
1532 if (is_cpu && !name_only && !alias->desc)
1533 aliases[j].name = format_alias_or(buf,
1536 aliases[j].name = strdup(aliases[j].name);
1537 if (!aliases[j].name)
1540 aliases[j].desc = long_desc ? alias->long_desc :
1542 aliases[j].topic = alias->topic;
1543 aliases[j].str = alias->str;
1544 aliases[j].pmu = pmu->name;
1545 aliases[j].metric_expr = alias->metric_expr;
1546 aliases[j].metric_name = alias->metric_name;
1547 aliases[j].is_cpu = is_cpu;
1550 if (pmu->selectable &&
1551 (event_glob == NULL || strglobmatch(pmu->name, event_glob))) {
1553 if (asprintf(&s, "%s//", pmu->name) < 0)
1555 aliases[j].name = s;
1560 qsort(aliases, len, sizeof(struct sevent), cmp_sevent);
1561 for (j = 0; j < len; j++) {
1562 /* Skip duplicates */
1563 if (j > 0 && !strcmp(aliases[j].name, aliases[j - 1].name))
1566 printf("%s ", aliases[j].name);
1569 if (aliases[j].desc && !quiet_flag) {
1572 if (aliases[j].topic && (!topic ||
1573 strcmp(topic, aliases[j].topic))) {
1574 printf("%s%s:\n", topic ? "\n" : "",
1576 topic = aliases[j].topic;
1578 printf(" %-50s\n", aliases[j].name);
1579 printf("%*s", 8, "[");
1580 wordwrap(aliases[j].desc, 8, columns, 0);
1583 printf("%*s%s/%s/ ", 8, "", aliases[j].pmu, aliases[j].str);
1584 if (aliases[j].metric_name)
1585 printf(" MetricName: %s", aliases[j].metric_name);
1586 if (aliases[j].metric_expr)
1587 printf(" MetricExpr: %s", aliases[j].metric_expr);
1591 printf(" %-50s [Kernel PMU event]\n", aliases[j].name);
1594 if (printed && pager_in_use())
1597 for (j = 0; j < len; j++)
1598 zfree(&aliases[j].name);
1603 printf("FATAL: not enough memory to print PMU events\n");
1608 bool pmu_have_event(const char *pname, const char *name)
1610 struct perf_pmu *pmu;
1611 struct perf_pmu_alias *alias;
1614 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1615 if (strcmp(pname, pmu->name))
1617 list_for_each_entry(alias, &pmu->aliases, list)
1618 if (!strcmp(alias->name, name))
1624 static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
1626 char path[PATH_MAX];
1629 sysfs = sysfs__mountpoint();
1633 snprintf(path, PATH_MAX,
1634 "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name);
1635 if (!file_available(path))
1637 return fopen(path, "r");
1640 int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
1647 va_start(args, fmt);
1648 file = perf_pmu__open_file(pmu, name);
1650 ret = vfscanf(file, fmt, args);
1657 static int perf_pmu__new_caps(struct list_head *list, char *name, char *value)
1659 struct perf_pmu_caps *caps = zalloc(sizeof(*caps));
1664 caps->name = strdup(name);
1667 caps->value = strndup(value, strlen(value) - 1);
1670 list_add_tail(&caps->list, list);
1682 * Reading/parsing the given pmu capabilities, which should be located at:
1683 * /sys/bus/event_source/devices/<dev>/caps as sysfs group attributes.
1684 * Return the number of capabilities
1686 int perf_pmu__caps_parse(struct perf_pmu *pmu)
1689 char caps_path[PATH_MAX];
1690 const char *sysfs = sysfs__mountpoint();
1692 struct dirent *evt_ent;
1698 snprintf(caps_path, PATH_MAX,
1699 "%s" EVENT_SOURCE_DEVICE_PATH "%s/caps", sysfs, pmu->name);
1701 if (stat(caps_path, &st) < 0)
1702 return 0; /* no error if caps does not exist */
1704 caps_dir = opendir(caps_path);
1708 while ((evt_ent = readdir(caps_dir)) != NULL) {
1709 char path[PATH_MAX + NAME_MAX + 1];
1710 char *name = evt_ent->d_name;
1714 if (!strcmp(name, ".") || !strcmp(name, ".."))
1717 snprintf(path, sizeof(path), "%s/%s", caps_path, name);
1719 file = fopen(path, "r");
1723 if (!fgets(value, sizeof(value), file) ||
1724 (perf_pmu__new_caps(&pmu->caps, name, value) < 0)) {