1 #include <linux/list.h>
2 #include <linux/compiler.h>
13 #include "parse-events.h"
16 #include "pmu-events/pmu-events.h"
19 struct perf_pmu_format {
22 DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
23 struct list_head list;
26 #define EVENT_SOURCE_DEVICE_PATH "/bus/event_source/devices/"
28 int perf_pmu_parse(struct list_head *list, char *name);
29 extern FILE *perf_pmu_in;
31 static LIST_HEAD(pmus);
34 * Parse & process all the sysfs attributes located under
35 * the directory specified in 'dir' parameter.
37 int perf_pmu__format_parse(char *dir, struct list_head *head)
39 struct dirent *evt_ent;
43 format_dir = opendir(dir);
47 while (!ret && (evt_ent = readdir(format_dir))) {
49 char *name = evt_ent->d_name;
52 if (!strcmp(name, ".") || !strcmp(name, ".."))
55 snprintf(path, PATH_MAX, "%s/%s", dir, name);
58 file = fopen(path, "r");
63 ret = perf_pmu_parse(head, name);
72 * Reading/parsing the default pmu format definition, which should be
74 * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
76 static int pmu_format(const char *name, struct list_head *format)
80 const char *sysfs = sysfs__mountpoint();
85 snprintf(path, PATH_MAX,
86 "%s" EVENT_SOURCE_DEVICE_PATH "%s/format", sysfs, name);
88 if (stat(path, &st) < 0)
89 return 0; /* no error if format does not exist */
91 if (perf_pmu__format_parse(path, format))
97 static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *name)
106 snprintf(path, PATH_MAX, "%s/%s.scale", dir, name);
108 fd = open(path, O_RDONLY);
112 if (fstat(fd, &st) < 0)
115 sret = read(fd, scale, sizeof(scale)-1);
119 if (scale[sret - 1] == '\n')
120 scale[sret - 1] = '\0';
125 * save current locale
127 lc = setlocale(LC_NUMERIC, NULL);
130 * The lc string may be allocated in static storage,
131 * so get a dynamic copy to make it survive setlocale
141 * force to C locale to ensure kernel
142 * scale string is converted correctly.
143 * kernel uses default C locale.
145 setlocale(LC_NUMERIC, "C");
147 alias->scale = strtod(scale, NULL);
150 setlocale(LC_NUMERIC, lc);
160 static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *name)
166 snprintf(path, PATH_MAX, "%s/%s.unit", dir, name);
168 fd = open(path, O_RDONLY);
172 sret = read(fd, alias->unit, UNIT_MAX_LEN);
178 if (alias->unit[sret - 1] == '\n')
179 alias->unit[sret - 1] = '\0';
181 alias->unit[sret] = '\0';
186 alias->unit[0] = '\0';
191 perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, char *dir, char *name)
196 snprintf(path, PATH_MAX, "%s/%s.per-pkg", dir, name);
198 fd = open(path, O_RDONLY);
204 alias->per_pkg = true;
208 static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias,
209 char *dir, char *name)
214 snprintf(path, PATH_MAX, "%s/%s.snapshot", dir, name);
216 fd = open(path, O_RDONLY);
220 alias->snapshot = true;
225 static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name,
226 char *desc, char *val)
228 struct perf_pmu_alias *alias;
231 alias = malloc(sizeof(*alias));
235 INIT_LIST_HEAD(&alias->terms);
237 alias->unit[0] = '\0';
238 alias->per_pkg = false;
239 alias->snapshot = false;
241 ret = parse_events_terms(&alias->terms, val);
243 pr_err("Cannot parse alias %s: %d\n", val, ret);
248 alias->name = strdup(name);
251 * load unit name and scale if available
253 perf_pmu__parse_unit(alias, dir, name);
254 perf_pmu__parse_scale(alias, dir, name);
255 perf_pmu__parse_per_pkg(alias, dir, name);
256 perf_pmu__parse_snapshot(alias, dir, name);
259 alias->desc = desc ? strdup(desc) : NULL;
261 list_add_tail(&alias->list, list);
266 static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file)
271 ret = fread(buf, 1, sizeof(buf), file);
277 return __perf_pmu__new_alias(list, dir, name, NULL, buf);
280 static inline bool pmu_alias_info_file(char *name)
285 if (len > 5 && !strcmp(name + len - 5, ".unit"))
287 if (len > 6 && !strcmp(name + len - 6, ".scale"))
289 if (len > 8 && !strcmp(name + len - 8, ".per-pkg"))
291 if (len > 9 && !strcmp(name + len - 9, ".snapshot"))
298 * Process all the sysfs attributes located under the directory
299 * specified in 'dir' parameter.
301 static int pmu_aliases_parse(char *dir, struct list_head *head)
303 struct dirent *evt_ent;
306 event_dir = opendir(dir);
310 while ((evt_ent = readdir(event_dir))) {
312 char *name = evt_ent->d_name;
315 if (!strcmp(name, ".") || !strcmp(name, ".."))
319 * skip info files parsed in perf_pmu__new_alias()
321 if (pmu_alias_info_file(name))
324 snprintf(path, PATH_MAX, "%s/%s", dir, name);
326 file = fopen(path, "r");
328 pr_debug("Cannot open %s\n", path);
332 if (perf_pmu__new_alias(head, dir, name, file) < 0)
333 pr_debug("Cannot set up %s\n", name);
342 * Reading the pmu event aliases definition, which should be located at:
343 * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
345 static int pmu_aliases(const char *name, struct list_head *head)
349 const char *sysfs = sysfs__mountpoint();
354 snprintf(path, PATH_MAX,
355 "%s/bus/event_source/devices/%s/events", sysfs, name);
357 if (stat(path, &st) < 0)
358 return 0; /* no error if 'events' does not exist */
360 if (pmu_aliases_parse(path, head))
366 static int pmu_alias_terms(struct perf_pmu_alias *alias,
367 struct list_head *terms)
369 struct parse_events_term *term, *cloned;
373 list_for_each_entry(term, &alias->terms, list) {
374 ret = parse_events_term__clone(&cloned, term);
376 parse_events_terms__purge(&list);
379 list_add_tail(&cloned->list, &list);
381 list_splice(&list, terms);
386 * Reading/parsing the default pmu type value, which should be
388 * /sys/bus/event_source/devices/<dev>/type as sysfs attribute.
390 static int pmu_type(const char *name, __u32 *type)
396 const char *sysfs = sysfs__mountpoint();
401 snprintf(path, PATH_MAX,
402 "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name);
404 if (stat(path, &st) < 0)
407 file = fopen(path, "r");
411 if (1 != fscanf(file, "%u", type))
418 /* Add all pmus in sysfs to pmu list: */
419 static void pmu_read_sysfs(void)
424 const char *sysfs = sysfs__mountpoint();
429 snprintf(path, PATH_MAX,
430 "%s" EVENT_SOURCE_DEVICE_PATH, sysfs);
436 while ((dent = readdir(dir))) {
437 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
439 /* add to static LIST_HEAD(pmus): */
440 perf_pmu__find(dent->d_name);
446 static struct cpu_map *pmu_cpumask(const char *name)
451 struct cpu_map *cpus;
452 const char *sysfs = sysfs__mountpoint();
453 const char *templates[] = {
454 "%s/bus/event_source/devices/%s/cpumask",
455 "%s/bus/event_source/devices/%s/cpus",
458 const char **template;
463 for (template = templates; *template; template++) {
464 snprintf(path, PATH_MAX, *template, sysfs, name);
465 if (stat(path, &st) == 0)
472 file = fopen(path, "r");
476 cpus = cpu_map__read(file);
482 * Return the CPU id as a raw string.
484 * Each architecture should provide a more precise id string that
485 * can be use to match the architecture's "mapfile".
487 char * __weak get_cpuid_str(void)
493 * From the pmu_events_map, find the table of PMU events that corresponds
494 * to the current running CPU. Then, add all PMU events from that table
497 static void pmu_add_cpu_aliases(struct list_head *head)
500 struct pmu_events_map *map;
501 struct pmu_event *pe;
504 cpuid = getenv("PERF_CPUID");
506 cpuid = strdup(cpuid);
508 cpuid = get_cpuid_str();
512 pr_debug("Using CPUID %s\n", cpuid);
516 map = &pmu_events_map[i++];
520 if (!strcmp(map->cpuid, cpuid))
525 * Found a matching PMU events table. Create aliases
529 pe = &map->table[i++];
533 /* need type casts to override 'const' */
534 __perf_pmu__new_alias(head, NULL, (char *)pe->name,
535 (char *)pe->desc, (char *)pe->event);
542 struct perf_event_attr * __weak
543 perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
548 static struct perf_pmu *pmu_lookup(const char *name)
550 struct perf_pmu *pmu;
556 * The pmu data we store & need consists of the pmu
557 * type value and format definitions. Load both right
560 if (pmu_format(name, &format))
563 if (pmu_aliases(name, &aliases))
566 if (!strcmp(name, "cpu"))
567 pmu_add_cpu_aliases(&aliases);
569 if (pmu_type(name, &type))
572 pmu = zalloc(sizeof(*pmu));
576 pmu->cpus = pmu_cpumask(name);
578 INIT_LIST_HEAD(&pmu->format);
579 INIT_LIST_HEAD(&pmu->aliases);
580 list_splice(&format, &pmu->format);
581 list_splice(&aliases, &pmu->aliases);
582 pmu->name = strdup(name);
584 list_add_tail(&pmu->list, &pmus);
586 pmu->default_config = perf_pmu__get_default_config(pmu);
591 static struct perf_pmu *pmu_find(const char *name)
593 struct perf_pmu *pmu;
595 list_for_each_entry(pmu, &pmus, list)
596 if (!strcmp(pmu->name, name))
602 struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
605 * pmu iterator: If pmu is NULL, we start at the begin,
606 * otherwise return the next pmu. Returns NULL on end.
610 pmu = list_prepare_entry(pmu, &pmus, list);
612 list_for_each_entry_continue(pmu, &pmus, list)
617 struct perf_pmu *perf_pmu__find(const char *name)
619 struct perf_pmu *pmu;
622 * Once PMU is loaded it stays in the list,
623 * so we keep us from multiple reading/parsing
624 * the pmu format definitions.
626 pmu = pmu_find(name);
630 return pmu_lookup(name);
633 static struct perf_pmu_format *
634 pmu_find_format(struct list_head *formats, const char *name)
636 struct perf_pmu_format *format;
638 list_for_each_entry(format, formats, list)
639 if (!strcmp(format->name, name))
645 __u64 perf_pmu__format_bits(struct list_head *formats, const char *name)
647 struct perf_pmu_format *format = pmu_find_format(formats, name);
654 for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
655 bits |= 1ULL << fbit;
661 * Sets value based on the format definition (format parameter)
662 * and unformated value (value parameter).
664 static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
667 unsigned long fbit, vbit;
669 for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
671 if (!test_bit(fbit, format))
674 if (value & (1llu << vbit++))
675 *v |= (1llu << fbit);
677 *v &= ~(1llu << fbit);
681 static __u64 pmu_format_max_value(const unsigned long *format)
686 for_each_set_bit(fbit, format, PERF_PMU_FORMAT_BITS)
693 * Term is a string term, and might be a param-term. Try to look up it's value
694 * in the remaining terms.
695 * - We have a term like "base-or-format-term=param-term",
696 * - We need to find the value supplied for "param-term" (with param-term named
697 * in a config string) later on in the term list.
699 static int pmu_resolve_param_term(struct parse_events_term *term,
700 struct list_head *head_terms,
703 struct parse_events_term *t;
705 list_for_each_entry(t, head_terms, list) {
706 if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
707 if (!strcmp(t->config, term->config)) {
716 printf("Required parameter '%s' not specified\n", term->config);
721 static char *pmu_formats_string(struct list_head *formats)
723 struct perf_pmu_format *format;
725 struct strbuf buf = STRBUF_INIT;
731 /* sysfs exported terms */
732 list_for_each_entry(format, formats, list)
733 if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
736 str = strbuf_detach(&buf, NULL);
738 strbuf_release(&buf);
744 * Setup one of config[12] attr members based on the
745 * user input data - term parameter.
747 static int pmu_config_term(struct list_head *formats,
748 struct perf_event_attr *attr,
749 struct parse_events_term *term,
750 struct list_head *head_terms,
751 bool zero, struct parse_events_error *err)
753 struct perf_pmu_format *format;
758 * If this is a parameter we've already used for parameterized-eval,
759 * skip it in normal eval.
765 * Hardcoded terms should be already in, so nothing
766 * to be done for them.
768 if (parse_events__is_hardcoded_term(term))
771 format = pmu_find_format(formats, term->config);
774 printf("Invalid event/parameter '%s'\n", term->config);
776 char *pmu_term = pmu_formats_string(formats);
778 err->idx = term->err_term;
779 err->str = strdup("unknown term");
780 err->help = parse_events_formats_error_string(pmu_term);
786 switch (format->value) {
787 case PERF_PMU_FORMAT_VALUE_CONFIG:
790 case PERF_PMU_FORMAT_VALUE_CONFIG1:
793 case PERF_PMU_FORMAT_VALUE_CONFIG2:
801 * Either directly use a numeric term, or try to translate string terms
802 * using event parameters.
804 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM)
806 else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
807 if (strcmp(term->val.str, "?")) {
809 pr_info("Invalid sysfs entry %s=%s\n",
810 term->config, term->val.str);
813 err->idx = term->err_val;
814 err->str = strdup("expected numeric value");
819 if (pmu_resolve_param_term(term, head_terms, &val))
824 max_val = pmu_format_max_value(format->bits);
827 err->idx = term->err_val;
828 if (asprintf(&err->str,
829 "value too big for format, maximum is %llu",
830 (unsigned long long)max_val) < 0)
831 err->str = strdup("value too big for format");
835 * Assume we don't care if !err, in which case the value will be
836 * silently truncated.
840 pmu_format_value(format->bits, val, vp, zero);
844 int perf_pmu__config_terms(struct list_head *formats,
845 struct perf_event_attr *attr,
846 struct list_head *head_terms,
847 bool zero, struct parse_events_error *err)
849 struct parse_events_term *term;
851 list_for_each_entry(term, head_terms, list) {
852 if (pmu_config_term(formats, attr, term, head_terms,
861 * Configures event's 'attr' parameter based on the:
862 * 1) users input - specified in terms parameter
863 * 2) pmu format definitions - specified by pmu parameter
865 int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
866 struct list_head *head_terms,
867 struct parse_events_error *err)
869 bool zero = !!pmu->default_config;
871 attr->type = pmu->type;
872 return perf_pmu__config_terms(&pmu->format, attr, head_terms,
876 static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
877 struct parse_events_term *term)
879 struct perf_pmu_alias *alias;
882 if (parse_events__is_hardcoded_term(term))
885 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
886 if (term->val.num != 1)
888 if (pmu_find_format(&pmu->format, term->config))
891 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
892 if (strcasecmp(term->config, "event"))
894 name = term->val.str;
899 list_for_each_entry(alias, &pmu->aliases, list) {
900 if (!strcasecmp(alias->name, name))
907 static int check_info_data(struct perf_pmu_alias *alias,
908 struct perf_pmu_info *info)
911 * Only one term in event definition can
912 * define unit, scale and snapshot, fail
913 * if there's more than one.
915 if ((info->unit && alias->unit) ||
916 (info->scale && alias->scale) ||
917 (info->snapshot && alias->snapshot))
921 info->unit = alias->unit;
924 info->scale = alias->scale;
927 info->snapshot = alias->snapshot;
933 * Find alias in the terms list and replace it with the terms
934 * defined for the alias
936 int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
937 struct perf_pmu_info *info)
939 struct parse_events_term *term, *h;
940 struct perf_pmu_alias *alias;
943 info->per_pkg = false;
946 * Mark unit and scale as not set
947 * (different from default values, see below)
951 info->snapshot = false;
953 list_for_each_entry_safe(term, h, head_terms, list) {
954 alias = pmu_find_alias(pmu, term);
957 ret = pmu_alias_terms(alias, &term->list);
961 ret = check_info_data(alias, info);
966 info->per_pkg = true;
968 list_del(&term->list);
973 * if no unit or scale foundin aliases, then
974 * set defaults as for evsel
975 * unit cannot left to NULL
977 if (info->unit == NULL)
980 if (info->scale == 0.0)
986 int perf_pmu__new_format(struct list_head *list, char *name,
987 int config, unsigned long *bits)
989 struct perf_pmu_format *format;
991 format = zalloc(sizeof(*format));
995 format->name = strdup(name);
996 format->value = config;
997 memcpy(format->bits, bits, sizeof(format->bits));
999 list_add_tail(&format->list, list);
1003 void perf_pmu__set_format(unsigned long *bits, long from, long to)
1010 memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
1011 for (b = from; b <= to; b++)
1015 static int sub_non_neg(int a, int b)
1022 static char *format_alias(char *buf, int len, struct perf_pmu *pmu,
1023 struct perf_pmu_alias *alias)
1025 struct parse_events_term *term;
1026 int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name);
1028 list_for_each_entry(term, &alias->terms, list) {
1029 if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
1030 used += snprintf(buf + used, sub_non_neg(len, used),
1031 ",%s=%s", term->config,
1035 if (sub_non_neg(len, used) > 0) {
1039 if (sub_non_neg(len, used) > 0) {
1043 buf[len - 1] = '\0';
1048 static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu,
1049 struct perf_pmu_alias *alias)
1051 snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name);
1060 static int cmp_pair(const void *a, const void *b)
1062 const struct pair *as = a;
1063 const struct pair *bs = b;
1065 /* Put extra events last */
1066 if (!!as->desc != !!bs->desc)
1067 return !!as->desc - !!bs->desc;
1068 return strcmp(as->name, bs->name);
1071 static void wordwrap(char *s, int start, int max, int corr)
1077 int wlen = strcspn(s, " \t");
1079 if (column + wlen >= max && column > start) {
1080 printf("\n%*s", start, "");
1081 column = start + corr;
1083 n = printf("%s%.*s", column > start ? " " : "", wlen, s);
1093 void print_pmu_events(const char *event_glob, bool name_only, bool quiet_flag)
1095 struct perf_pmu *pmu;
1096 struct perf_pmu_alias *alias;
1100 struct pair *aliases;
1102 int columns = pager_get_columns();
1106 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1107 list_for_each_entry(alias, &pmu->aliases, list)
1109 if (pmu->selectable)
1112 aliases = zalloc(sizeof(struct pair) * len);
1117 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1118 list_for_each_entry(alias, &pmu->aliases, list) {
1119 char *name = alias->desc ? alias->name :
1120 format_alias(buf, sizeof(buf), pmu, alias);
1121 bool is_cpu = !strcmp(pmu->name, "cpu");
1123 if (event_glob != NULL &&
1124 !(strglobmatch(name, event_glob) ||
1125 (!is_cpu && strglobmatch(alias->name,
1129 if (is_cpu && !name_only && !alias->desc)
1130 name = format_alias_or(buf, sizeof(buf), pmu, alias);
1132 aliases[j].name = name;
1133 if (is_cpu && !name_only && !alias->desc)
1134 aliases[j].name = format_alias_or(buf,
1137 aliases[j].name = strdup(aliases[j].name);
1138 if (!aliases[j].name)
1141 aliases[j].desc = alias->desc;
1144 if (pmu->selectable &&
1145 (event_glob == NULL || strglobmatch(pmu->name, event_glob))) {
1147 if (asprintf(&s, "%s//", pmu->name) < 0)
1149 aliases[j].name = s;
1154 qsort(aliases, len, sizeof(struct pair), cmp_pair);
1155 for (j = 0; j < len; j++) {
1157 printf("%s ", aliases[j].name);
1160 if (aliases[j].desc && !quiet_flag) {
1163 printf(" %-50s\n", aliases[j].name);
1164 printf("%*s", 8, "[");
1165 wordwrap(aliases[j].desc, 8, columns, 0);
1168 printf(" %-50s [Kernel PMU event]\n", aliases[j].name);
1171 if (printed && pager_in_use())
1174 for (j = 0; j < len; j++)
1175 zfree(&aliases[j].name);
1180 printf("FATAL: not enough memory to print PMU events\n");
1185 bool pmu_have_event(const char *pname, const char *name)
1187 struct perf_pmu *pmu;
1188 struct perf_pmu_alias *alias;
1191 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1192 if (strcmp(pname, pmu->name))
1194 list_for_each_entry(alias, &pmu->aliases, list)
1195 if (!strcmp(alias->name, name))
1201 static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
1204 char path[PATH_MAX];
1207 sysfs = sysfs__mountpoint();
1211 snprintf(path, PATH_MAX,
1212 "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name);
1214 if (stat(path, &st) < 0)
1217 return fopen(path, "r");
1220 int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
1227 va_start(args, fmt);
1228 file = perf_pmu__open_file(pmu, name);
1230 ret = vfscanf(file, fmt, args);