1 #include <linux/list.h>
12 #include "parse-events.h"
15 struct perf_pmu_format {
18 DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
19 struct list_head list;
22 #define EVENT_SOURCE_DEVICE_PATH "/bus/event_source/devices/"
24 int perf_pmu_parse(struct list_head *list, char *name);
25 extern FILE *perf_pmu_in;
27 static LIST_HEAD(pmus);
30 * Parse & process all the sysfs attributes located under
31 * the directory specified in 'dir' parameter.
33 int perf_pmu__format_parse(char *dir, struct list_head *head)
35 struct dirent *evt_ent;
39 format_dir = opendir(dir);
43 while (!ret && (evt_ent = readdir(format_dir))) {
45 char *name = evt_ent->d_name;
48 if (!strcmp(name, ".") || !strcmp(name, ".."))
51 snprintf(path, PATH_MAX, "%s/%s", dir, name);
54 file = fopen(path, "r");
59 ret = perf_pmu_parse(head, name);
68 * Reading/parsing the default pmu format definition, which should be
70 * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
72 static int pmu_format(const char *name, struct list_head *format)
76 const char *sysfs = sysfs__mountpoint();
81 snprintf(path, PATH_MAX,
82 "%s" EVENT_SOURCE_DEVICE_PATH "%s/format", sysfs, name);
84 if (stat(path, &st) < 0)
85 return 0; /* no error if format does not exist */
87 if (perf_pmu__format_parse(path, format))
93 static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *name)
102 snprintf(path, PATH_MAX, "%s/%s.scale", dir, name);
104 fd = open(path, O_RDONLY);
108 if (fstat(fd, &st) < 0)
111 sret = read(fd, scale, sizeof(scale)-1);
117 * save current locale
119 lc = setlocale(LC_NUMERIC, NULL);
122 * force to C locale to ensure kernel
123 * scale string is converted correctly.
124 * kernel uses default C locale.
126 setlocale(LC_NUMERIC, "C");
128 alias->scale = strtod(scale, NULL);
131 setlocale(LC_NUMERIC, lc);
139 static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *name)
145 snprintf(path, PATH_MAX, "%s/%s.unit", dir, name);
147 fd = open(path, O_RDONLY);
151 sret = read(fd, alias->unit, UNIT_MAX_LEN);
157 alias->unit[sret] = '\0';
162 alias->unit[0] = '\0';
167 perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, char *dir, char *name)
172 snprintf(path, PATH_MAX, "%s/%s.per-pkg", dir, name);
174 fd = open(path, O_RDONLY);
180 alias->per_pkg = true;
184 static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias,
185 char *dir, char *name)
190 snprintf(path, PATH_MAX, "%s/%s.snapshot", dir, name);
192 fd = open(path, O_RDONLY);
196 alias->snapshot = true;
201 static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file)
203 struct perf_pmu_alias *alias;
207 ret = fread(buf, 1, sizeof(buf), file);
212 alias = malloc(sizeof(*alias));
216 INIT_LIST_HEAD(&alias->terms);
218 alias->unit[0] = '\0';
219 alias->per_pkg = false;
221 ret = parse_events_terms(&alias->terms, buf);
227 alias->name = strdup(name);
229 * load unit name and scale if available
231 perf_pmu__parse_unit(alias, dir, name);
232 perf_pmu__parse_scale(alias, dir, name);
233 perf_pmu__parse_per_pkg(alias, dir, name);
234 perf_pmu__parse_snapshot(alias, dir, name);
236 list_add_tail(&alias->list, list);
241 static inline bool pmu_alias_info_file(char *name)
246 if (len > 5 && !strcmp(name + len - 5, ".unit"))
248 if (len > 6 && !strcmp(name + len - 6, ".scale"))
250 if (len > 8 && !strcmp(name + len - 8, ".per-pkg"))
252 if (len > 9 && !strcmp(name + len - 9, ".snapshot"))
259 * Process all the sysfs attributes located under the directory
260 * specified in 'dir' parameter.
262 static int pmu_aliases_parse(char *dir, struct list_head *head)
264 struct dirent *evt_ent;
268 event_dir = opendir(dir);
272 while (!ret && (evt_ent = readdir(event_dir))) {
274 char *name = evt_ent->d_name;
277 if (!strcmp(name, ".") || !strcmp(name, ".."))
281 * skip info files parsed in perf_pmu__new_alias()
283 if (pmu_alias_info_file(name))
286 snprintf(path, PATH_MAX, "%s/%s", dir, name);
289 file = fopen(path, "r");
293 ret = perf_pmu__new_alias(head, dir, name, file);
302 * Reading the pmu event aliases definition, which should be located at:
303 * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
305 static int pmu_aliases(const char *name, struct list_head *head)
309 const char *sysfs = sysfs__mountpoint();
314 snprintf(path, PATH_MAX,
315 "%s/bus/event_source/devices/%s/events", sysfs, name);
317 if (stat(path, &st) < 0)
318 return 0; /* no error if 'events' does not exist */
320 if (pmu_aliases_parse(path, head))
326 static int pmu_alias_terms(struct perf_pmu_alias *alias,
327 struct list_head *terms)
329 struct parse_events_term *term, *cloned;
333 list_for_each_entry(term, &alias->terms, list) {
334 ret = parse_events_term__clone(&cloned, term);
336 parse_events__free_terms(&list);
339 list_add_tail(&cloned->list, &list);
341 list_splice(&list, terms);
346 * Reading/parsing the default pmu type value, which should be
348 * /sys/bus/event_source/devices/<dev>/type as sysfs attribute.
350 static int pmu_type(const char *name, __u32 *type)
356 const char *sysfs = sysfs__mountpoint();
361 snprintf(path, PATH_MAX,
362 "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name);
364 if (stat(path, &st) < 0)
367 file = fopen(path, "r");
371 if (1 != fscanf(file, "%u", type))
378 /* Add all pmus in sysfs to pmu list: */
379 static void pmu_read_sysfs(void)
384 const char *sysfs = sysfs__mountpoint();
389 snprintf(path, PATH_MAX,
390 "%s" EVENT_SOURCE_DEVICE_PATH, sysfs);
396 while ((dent = readdir(dir))) {
397 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
399 /* add to static LIST_HEAD(pmus): */
400 perf_pmu__find(dent->d_name);
406 static struct cpu_map *pmu_cpumask(const char *name)
411 struct cpu_map *cpus;
412 const char *sysfs = sysfs__mountpoint();
417 snprintf(path, PATH_MAX,
418 "%s/bus/event_source/devices/%s/cpumask", sysfs, name);
420 if (stat(path, &st) < 0)
423 file = fopen(path, "r");
427 cpus = cpu_map__read(file);
432 struct perf_event_attr *__attribute__((weak))
433 perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
438 static struct perf_pmu *pmu_lookup(const char *name)
440 struct perf_pmu *pmu;
446 * The pmu data we store & need consists of the pmu
447 * type value and format definitions. Load both right
450 if (pmu_format(name, &format))
453 if (pmu_aliases(name, &aliases))
456 if (pmu_type(name, &type))
459 pmu = zalloc(sizeof(*pmu));
463 pmu->cpus = pmu_cpumask(name);
465 INIT_LIST_HEAD(&pmu->format);
466 INIT_LIST_HEAD(&pmu->aliases);
467 list_splice(&format, &pmu->format);
468 list_splice(&aliases, &pmu->aliases);
469 pmu->name = strdup(name);
471 list_add_tail(&pmu->list, &pmus);
473 pmu->default_config = perf_pmu__get_default_config(pmu);
478 static struct perf_pmu *pmu_find(const char *name)
480 struct perf_pmu *pmu;
482 list_for_each_entry(pmu, &pmus, list)
483 if (!strcmp(pmu->name, name))
489 struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
492 * pmu iterator: If pmu is NULL, we start at the begin,
493 * otherwise return the next pmu. Returns NULL on end.
497 pmu = list_prepare_entry(pmu, &pmus, list);
499 list_for_each_entry_continue(pmu, &pmus, list)
504 struct perf_pmu *perf_pmu__find(const char *name)
506 struct perf_pmu *pmu;
509 * Once PMU is loaded it stays in the list,
510 * so we keep us from multiple reading/parsing
511 * the pmu format definitions.
513 pmu = pmu_find(name);
517 return pmu_lookup(name);
520 static struct perf_pmu_format *
521 pmu_find_format(struct list_head *formats, char *name)
523 struct perf_pmu_format *format;
525 list_for_each_entry(format, formats, list)
526 if (!strcmp(format->name, name))
533 * Sets value based on the format definition (format parameter)
534 * and unformated value (value parameter).
536 static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
539 unsigned long fbit, vbit;
541 for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
543 if (!test_bit(fbit, format))
546 if (value & (1llu << vbit++))
547 *v |= (1llu << fbit);
549 *v &= ~(1llu << fbit);
554 * Term is a string term, and might be a param-term. Try to look up it's value
555 * in the remaining terms.
556 * - We have a term like "base-or-format-term=param-term",
557 * - We need to find the value supplied for "param-term" (with param-term named
558 * in a config string) later on in the term list.
560 static int pmu_resolve_param_term(struct parse_events_term *term,
561 struct list_head *head_terms,
564 struct parse_events_term *t;
566 list_for_each_entry(t, head_terms, list) {
567 if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
568 if (!strcmp(t->config, term->config)) {
577 printf("Required parameter '%s' not specified\n", term->config);
583 * Setup one of config[12] attr members based on the
584 * user input data - term parameter.
586 static int pmu_config_term(struct list_head *formats,
587 struct perf_event_attr *attr,
588 struct parse_events_term *term,
589 struct list_head *head_terms,
592 struct perf_pmu_format *format;
597 * If this is a parameter we've already used for parameterized-eval,
598 * skip it in normal eval.
604 * Hardcoded terms should be already in, so nothing
605 * to be done for them.
607 if (parse_events__is_hardcoded_term(term))
610 format = pmu_find_format(formats, term->config);
613 printf("Invalid event/parameter '%s'\n", term->config);
617 switch (format->value) {
618 case PERF_PMU_FORMAT_VALUE_CONFIG:
621 case PERF_PMU_FORMAT_VALUE_CONFIG1:
624 case PERF_PMU_FORMAT_VALUE_CONFIG2:
632 * Either directly use a numeric term, or try to translate string terms
633 * using event parameters.
635 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM)
637 else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
638 if (strcmp(term->val.str, "?")) {
640 pr_info("Invalid sysfs entry %s=%s\n",
641 term->config, term->val.str);
645 if (pmu_resolve_param_term(term, head_terms, &val))
650 pmu_format_value(format->bits, val, vp, zero);
654 int perf_pmu__config_terms(struct list_head *formats,
655 struct perf_event_attr *attr,
656 struct list_head *head_terms,
659 struct parse_events_term *term;
661 list_for_each_entry(term, head_terms, list) {
662 if (pmu_config_term(formats, attr, term, head_terms, zero))
670 * Configures event's 'attr' parameter based on the:
671 * 1) users input - specified in terms parameter
672 * 2) pmu format definitions - specified by pmu parameter
674 int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
675 struct list_head *head_terms)
677 bool zero = !!pmu->default_config;
679 attr->type = pmu->type;
680 return perf_pmu__config_terms(&pmu->format, attr, head_terms, zero);
683 static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
684 struct parse_events_term *term)
686 struct perf_pmu_alias *alias;
689 if (parse_events__is_hardcoded_term(term))
692 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
693 if (term->val.num != 1)
695 if (pmu_find_format(&pmu->format, term->config))
698 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
699 if (strcasecmp(term->config, "event"))
701 name = term->val.str;
706 list_for_each_entry(alias, &pmu->aliases, list) {
707 if (!strcasecmp(alias->name, name))
714 static int check_info_data(struct perf_pmu_alias *alias,
715 struct perf_pmu_info *info)
718 * Only one term in event definition can
719 * define unit, scale and snapshot, fail
720 * if there's more than one.
722 if ((info->unit && alias->unit) ||
723 (info->scale && alias->scale) ||
724 (info->snapshot && alias->snapshot))
728 info->unit = alias->unit;
731 info->scale = alias->scale;
734 info->snapshot = alias->snapshot;
740 * Find alias in the terms list and replace it with the terms
741 * defined for the alias
743 int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
744 struct perf_pmu_info *info)
746 struct parse_events_term *term, *h;
747 struct perf_pmu_alias *alias;
750 info->per_pkg = false;
753 * Mark unit and scale as not set
754 * (different from default values, see below)
758 info->snapshot = false;
760 list_for_each_entry_safe(term, h, head_terms, list) {
761 alias = pmu_find_alias(pmu, term);
764 ret = pmu_alias_terms(alias, &term->list);
768 ret = check_info_data(alias, info);
773 info->per_pkg = true;
775 list_del(&term->list);
780 * if no unit or scale foundin aliases, then
781 * set defaults as for evsel
782 * unit cannot left to NULL
784 if (info->unit == NULL)
787 if (info->scale == 0.0)
793 int perf_pmu__new_format(struct list_head *list, char *name,
794 int config, unsigned long *bits)
796 struct perf_pmu_format *format;
798 format = zalloc(sizeof(*format));
802 format->name = strdup(name);
803 format->value = config;
804 memcpy(format->bits, bits, sizeof(format->bits));
806 list_add_tail(&format->list, list);
810 void perf_pmu__set_format(unsigned long *bits, long from, long to)
817 memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
818 for (b = from; b <= to; b++)
822 static int sub_non_neg(int a, int b)
829 static char *format_alias(char *buf, int len, struct perf_pmu *pmu,
830 struct perf_pmu_alias *alias)
832 struct parse_events_term *term;
833 int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name);
835 list_for_each_entry(term, &alias->terms, list) {
836 if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
837 used += snprintf(buf + used, sub_non_neg(len, used),
838 ",%s=%s", term->config,
842 if (sub_non_neg(len, used) > 0) {
846 if (sub_non_neg(len, used) > 0) {
855 static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu,
856 struct perf_pmu_alias *alias)
858 snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name);
862 static int cmp_string(const void *a, const void *b)
864 const char * const *as = a;
865 const char * const *bs = b;
866 return strcmp(*as, *bs);
869 void print_pmu_events(const char *event_glob, bool name_only)
871 struct perf_pmu *pmu;
872 struct perf_pmu_alias *alias;
880 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
881 list_for_each_entry(alias, &pmu->aliases, list)
886 aliases = zalloc(sizeof(char *) * len);
891 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
892 list_for_each_entry(alias, &pmu->aliases, list) {
893 char *name = format_alias(buf, sizeof(buf), pmu, alias);
894 bool is_cpu = !strcmp(pmu->name, "cpu");
896 if (event_glob != NULL &&
897 !(strglobmatch(name, event_glob) ||
898 (!is_cpu && strglobmatch(alias->name,
902 if (is_cpu && !name_only)
903 name = format_alias_or(buf, sizeof(buf), pmu, alias);
905 aliases[j] = strdup(name);
906 if (aliases[j] == NULL)
910 if (pmu->selectable) {
912 if (asprintf(&s, "%s//", pmu->name) < 0)
919 qsort(aliases, len, sizeof(char *), cmp_string);
920 for (j = 0; j < len; j++) {
922 printf("%s ", aliases[j]);
925 printf(" %-50s [Kernel PMU event]\n", aliases[j]);
931 for (j = 0; j < len; j++)
937 printf("FATAL: not enough memory to print PMU events\n");
942 bool pmu_have_event(const char *pname, const char *name)
944 struct perf_pmu *pmu;
945 struct perf_pmu_alias *alias;
948 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
949 if (strcmp(pname, pmu->name))
951 list_for_each_entry(alias, &pmu->aliases, list)
952 if (!strcmp(alias->name, name))
958 static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
964 sysfs = sysfs__mountpoint();
968 snprintf(path, PATH_MAX,
969 "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name);
971 if (stat(path, &st) < 0)
974 return fopen(path, "r");
977 int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
985 file = perf_pmu__open_file(pmu, name);
987 ret = vfscanf(file, fmt, args);