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__new_alias(struct list_head
*list
, char *dir
, char *name
, FILE *file
)
186 struct perf_pmu_alias
*alias
;
190 ret
= fread(buf
, 1, sizeof(buf
), file
);
195 alias
= malloc(sizeof(*alias
));
199 INIT_LIST_HEAD(&alias
->terms
);
201 alias
->unit
[0] = '\0';
202 alias
->per_pkg
= false;
204 ret
= parse_events_terms(&alias
->terms
, buf
);
210 alias
->name
= strdup(name
);
212 * load unit name and scale if available
214 perf_pmu__parse_unit(alias
, dir
, name
);
215 perf_pmu__parse_scale(alias
, dir
, name
);
216 perf_pmu__parse_per_pkg(alias
, dir
, name
);
218 list_add_tail(&alias
->list
, list
);
223 static inline bool pmu_alias_info_file(char *name
)
228 if (len
> 5 && !strcmp(name
+ len
- 5, ".unit"))
230 if (len
> 6 && !strcmp(name
+ len
- 6, ".scale"))
232 if (len
> 8 && !strcmp(name
+ len
- 8, ".per-pkg"))
239 * Process all the sysfs attributes located under the directory
240 * specified in 'dir' parameter.
242 static int pmu_aliases_parse(char *dir
, struct list_head
*head
)
244 struct dirent
*evt_ent
;
248 event_dir
= opendir(dir
);
252 while (!ret
&& (evt_ent
= readdir(event_dir
))) {
254 char *name
= evt_ent
->d_name
;
257 if (!strcmp(name
, ".") || !strcmp(name
, ".."))
261 * skip info files parsed in perf_pmu__new_alias()
263 if (pmu_alias_info_file(name
))
266 snprintf(path
, PATH_MAX
, "%s/%s", dir
, name
);
269 file
= fopen(path
, "r");
273 ret
= perf_pmu__new_alias(head
, dir
, name
, file
);
282 * Reading the pmu event aliases definition, which should be located at:
283 * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
285 static int pmu_aliases(const char *name
, struct list_head
*head
)
289 const char *sysfs
= sysfs__mountpoint();
294 snprintf(path
, PATH_MAX
,
295 "%s/bus/event_source/devices/%s/events", sysfs
, name
);
297 if (stat(path
, &st
) < 0)
298 return 0; /* no error if 'events' does not exist */
300 if (pmu_aliases_parse(path
, head
))
306 static int pmu_alias_terms(struct perf_pmu_alias
*alias
,
307 struct list_head
*terms
)
309 struct parse_events_term
*term
, *cloned
;
313 list_for_each_entry(term
, &alias
->terms
, list
) {
314 ret
= parse_events_term__clone(&cloned
, term
);
316 parse_events__free_terms(&list
);
319 list_add_tail(&cloned
->list
, &list
);
321 list_splice(&list
, terms
);
326 * Reading/parsing the default pmu type value, which should be
328 * /sys/bus/event_source/devices/<dev>/type as sysfs attribute.
330 static int pmu_type(const char *name
, __u32
*type
)
336 const char *sysfs
= sysfs__mountpoint();
341 snprintf(path
, PATH_MAX
,
342 "%s" EVENT_SOURCE_DEVICE_PATH
"%s/type", sysfs
, name
);
344 if (stat(path
, &st
) < 0)
347 file
= fopen(path
, "r");
351 if (1 != fscanf(file
, "%u", type
))
358 /* Add all pmus in sysfs to pmu list: */
359 static void pmu_read_sysfs(void)
364 const char *sysfs
= sysfs__mountpoint();
369 snprintf(path
, PATH_MAX
,
370 "%s" EVENT_SOURCE_DEVICE_PATH
, sysfs
);
376 while ((dent
= readdir(dir
))) {
377 if (!strcmp(dent
->d_name
, ".") || !strcmp(dent
->d_name
, ".."))
379 /* add to static LIST_HEAD(pmus): */
380 perf_pmu__find(dent
->d_name
);
386 static struct cpu_map
*pmu_cpumask(const char *name
)
391 struct cpu_map
*cpus
;
392 const char *sysfs
= sysfs__mountpoint();
397 snprintf(path
, PATH_MAX
,
398 "%s/bus/event_source/devices/%s/cpumask", sysfs
, name
);
400 if (stat(path
, &st
) < 0)
403 file
= fopen(path
, "r");
407 cpus
= cpu_map__read(file
);
412 struct perf_event_attr
*__attribute__((weak
))
413 perf_pmu__get_default_config(struct perf_pmu
*pmu __maybe_unused
)
418 static struct perf_pmu
*pmu_lookup(const char *name
)
420 struct perf_pmu
*pmu
;
426 * The pmu data we store & need consists of the pmu
427 * type value and format definitions. Load both right
430 if (pmu_format(name
, &format
))
433 if (pmu_aliases(name
, &aliases
))
436 if (pmu_type(name
, &type
))
439 pmu
= zalloc(sizeof(*pmu
));
443 pmu
->cpus
= pmu_cpumask(name
);
445 INIT_LIST_HEAD(&pmu
->format
);
446 INIT_LIST_HEAD(&pmu
->aliases
);
447 list_splice(&format
, &pmu
->format
);
448 list_splice(&aliases
, &pmu
->aliases
);
449 pmu
->name
= strdup(name
);
451 list_add_tail(&pmu
->list
, &pmus
);
453 pmu
->default_config
= perf_pmu__get_default_config(pmu
);
458 static struct perf_pmu
*pmu_find(const char *name
)
460 struct perf_pmu
*pmu
;
462 list_for_each_entry(pmu
, &pmus
, list
)
463 if (!strcmp(pmu
->name
, name
))
469 struct perf_pmu
*perf_pmu__scan(struct perf_pmu
*pmu
)
472 * pmu iterator: If pmu is NULL, we start at the begin,
473 * otherwise return the next pmu. Returns NULL on end.
477 pmu
= list_prepare_entry(pmu
, &pmus
, list
);
479 list_for_each_entry_continue(pmu
, &pmus
, list
)
484 struct perf_pmu
*perf_pmu__find(const char *name
)
486 struct perf_pmu
*pmu
;
489 * Once PMU is loaded it stays in the list,
490 * so we keep us from multiple reading/parsing
491 * the pmu format definitions.
493 pmu
= pmu_find(name
);
497 return pmu_lookup(name
);
500 static struct perf_pmu_format
*
501 pmu_find_format(struct list_head
*formats
, char *name
)
503 struct perf_pmu_format
*format
;
505 list_for_each_entry(format
, formats
, list
)
506 if (!strcmp(format
->name
, name
))
513 * Sets value based on the format definition (format parameter)
514 * and unformated value (value parameter).
516 static void pmu_format_value(unsigned long *format
, __u64 value
, __u64
*v
,
519 unsigned long fbit
, vbit
;
521 for (fbit
= 0, vbit
= 0; fbit
< PERF_PMU_FORMAT_BITS
; fbit
++) {
523 if (!test_bit(fbit
, format
))
526 if (value
& (1llu << vbit
++))
527 *v
|= (1llu << fbit
);
529 *v
&= ~(1llu << fbit
);
534 * Setup one of config[12] attr members based on the
535 * user input data - term parameter.
537 static int pmu_config_term(struct list_head
*formats
,
538 struct perf_event_attr
*attr
,
539 struct parse_events_term
*term
,
542 struct perf_pmu_format
*format
;
546 * Support only for hardcoded and numnerial terms.
547 * Hardcoded terms should be already in, so nothing
548 * to be done for them.
550 if (parse_events__is_hardcoded_term(term
))
553 if (term
->type_val
!= PARSE_EVENTS__TERM_TYPE_NUM
)
556 format
= pmu_find_format(formats
, term
->config
);
560 switch (format
->value
) {
561 case PERF_PMU_FORMAT_VALUE_CONFIG
:
564 case PERF_PMU_FORMAT_VALUE_CONFIG1
:
567 case PERF_PMU_FORMAT_VALUE_CONFIG2
:
575 * XXX If we ever decide to go with string values for
576 * non-hardcoded terms, here's the place to translate
579 pmu_format_value(format
->bits
, term
->val
.num
, vp
, zero
);
583 int perf_pmu__config_terms(struct list_head
*formats
,
584 struct perf_event_attr
*attr
,
585 struct list_head
*head_terms
,
588 struct parse_events_term
*term
;
590 list_for_each_entry(term
, head_terms
, list
)
591 if (pmu_config_term(formats
, attr
, term
, zero
))
598 * Configures event's 'attr' parameter based on the:
599 * 1) users input - specified in terms parameter
600 * 2) pmu format definitions - specified by pmu parameter
602 int perf_pmu__config(struct perf_pmu
*pmu
, struct perf_event_attr
*attr
,
603 struct list_head
*head_terms
)
605 bool zero
= !!pmu
->default_config
;
607 attr
->type
= pmu
->type
;
608 return perf_pmu__config_terms(&pmu
->format
, attr
, head_terms
, zero
);
611 static struct perf_pmu_alias
*pmu_find_alias(struct perf_pmu
*pmu
,
612 struct parse_events_term
*term
)
614 struct perf_pmu_alias
*alias
;
617 if (parse_events__is_hardcoded_term(term
))
620 if (term
->type_val
== PARSE_EVENTS__TERM_TYPE_NUM
) {
621 if (term
->val
.num
!= 1)
623 if (pmu_find_format(&pmu
->format
, term
->config
))
626 } else if (term
->type_val
== PARSE_EVENTS__TERM_TYPE_STR
) {
627 if (strcasecmp(term
->config
, "event"))
629 name
= term
->val
.str
;
634 list_for_each_entry(alias
, &pmu
->aliases
, list
) {
635 if (!strcasecmp(alias
->name
, name
))
642 static int check_unit_scale(struct perf_pmu_alias
*alias
,
643 const char **unit
, double *scale
)
646 * Only one term in event definition can
647 * define unit and scale, fail if there's
650 if ((*unit
&& alias
->unit
) ||
651 (*scale
&& alias
->scale
))
658 *scale
= alias
->scale
;
664 * Find alias in the terms list and replace it with the terms
665 * defined for the alias
667 int perf_pmu__check_alias(struct perf_pmu
*pmu
, struct list_head
*head_terms
,
668 struct perf_pmu_info
*info
)
670 struct parse_events_term
*term
, *h
;
671 struct perf_pmu_alias
*alias
;
674 info
->per_pkg
= false;
677 * Mark unit and scale as not set
678 * (different from default values, see below)
683 list_for_each_entry_safe(term
, h
, head_terms
, list
) {
684 alias
= pmu_find_alias(pmu
, term
);
687 ret
= pmu_alias_terms(alias
, &term
->list
);
691 ret
= check_unit_scale(alias
, &info
->unit
, &info
->scale
);
696 info
->per_pkg
= true;
698 list_del(&term
->list
);
703 * if no unit or scale foundin aliases, then
704 * set defaults as for evsel
705 * unit cannot left to NULL
707 if (info
->unit
== NULL
)
710 if (info
->scale
== 0.0)
716 int perf_pmu__new_format(struct list_head
*list
, char *name
,
717 int config
, unsigned long *bits
)
719 struct perf_pmu_format
*format
;
721 format
= zalloc(sizeof(*format
));
725 format
->name
= strdup(name
);
726 format
->value
= config
;
727 memcpy(format
->bits
, bits
, sizeof(format
->bits
));
729 list_add_tail(&format
->list
, list
);
733 void perf_pmu__set_format(unsigned long *bits
, long from
, long to
)
740 memset(bits
, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS
));
741 for (b
= from
; b
<= to
; b
++)
745 static char *format_alias(char *buf
, int len
, struct perf_pmu
*pmu
,
746 struct perf_pmu_alias
*alias
)
748 snprintf(buf
, len
, "%s/%s/", pmu
->name
, alias
->name
);
752 static char *format_alias_or(char *buf
, int len
, struct perf_pmu
*pmu
,
753 struct perf_pmu_alias
*alias
)
755 snprintf(buf
, len
, "%s OR %s/%s/", alias
->name
, pmu
->name
, alias
->name
);
759 static int cmp_string(const void *a
, const void *b
)
761 const char * const *as
= a
;
762 const char * const *bs
= b
;
763 return strcmp(*as
, *bs
);
766 void print_pmu_events(const char *event_glob
, bool name_only
)
768 struct perf_pmu
*pmu
;
769 struct perf_pmu_alias
*alias
;
777 while ((pmu
= perf_pmu__scan(pmu
)) != NULL
) {
778 list_for_each_entry(alias
, &pmu
->aliases
, list
)
783 aliases
= zalloc(sizeof(char *) * len
);
788 while ((pmu
= perf_pmu__scan(pmu
)) != NULL
) {
789 list_for_each_entry(alias
, &pmu
->aliases
, list
) {
790 char *name
= format_alias(buf
, sizeof(buf
), pmu
, alias
);
791 bool is_cpu
= !strcmp(pmu
->name
, "cpu");
793 if (event_glob
!= NULL
&&
794 !(strglobmatch(name
, event_glob
) ||
795 (!is_cpu
&& strglobmatch(alias
->name
,
799 if (is_cpu
&& !name_only
)
800 name
= format_alias_or(buf
, sizeof(buf
), pmu
, alias
);
802 aliases
[j
] = strdup(name
);
803 if (aliases
[j
] == NULL
)
807 if (pmu
->selectable
) {
809 if (asprintf(&s
, "%s//", pmu
->name
) < 0)
816 qsort(aliases
, len
, sizeof(char *), cmp_string
);
817 for (j
= 0; j
< len
; j
++) {
819 printf("%s ", aliases
[j
]);
822 printf(" %-50s [Kernel PMU event]\n", aliases
[j
]);
828 for (j
= 0; j
< len
; j
++)
834 printf("FATAL: not enough memory to print PMU events\n");
839 bool pmu_have_event(const char *pname
, const char *name
)
841 struct perf_pmu
*pmu
;
842 struct perf_pmu_alias
*alias
;
845 while ((pmu
= perf_pmu__scan(pmu
)) != NULL
) {
846 if (strcmp(pname
, pmu
->name
))
848 list_for_each_entry(alias
, &pmu
->aliases
, list
)
849 if (!strcmp(alias
->name
, name
))
855 static FILE *perf_pmu__open_file(struct perf_pmu
*pmu
, const char *name
)
861 sysfs
= sysfs__mountpoint();
865 snprintf(path
, PATH_MAX
,
866 "%s" EVENT_SOURCE_DEVICE_PATH
"%s/%s", sysfs
, pmu
->name
, name
);
868 if (stat(path
, &st
) < 0)
871 return fopen(path
, "r");
874 int perf_pmu__scan_file(struct perf_pmu
*pmu
, const char *name
, const char *fmt
,
882 file
= perf_pmu__open_file(pmu
, name
);
884 ret
= vfscanf(file
, fmt
, args
);