2 #include <linux/list.h>
11 #include "parse-events.h"
14 #define EVENT_SOURCE_DEVICE_PATH "/bus/event_source/devices/"
16 int perf_pmu_parse(struct list_head
*list
, char *name
);
17 extern FILE *perf_pmu_in
;
19 static LIST_HEAD(pmus
);
22 * Parse & process all the sysfs attributes located under
23 * the directory specified in 'dir' parameter.
25 static int pmu_format_parse(char *dir
, struct list_head
*head
)
27 struct dirent
*evt_ent
;
31 format_dir
= opendir(dir
);
35 while (!ret
&& (evt_ent
= readdir(format_dir
))) {
37 char *name
= evt_ent
->d_name
;
40 if (!strcmp(name
, ".") || !strcmp(name
, ".."))
43 snprintf(path
, PATH_MAX
, "%s/%s", dir
, name
);
46 file
= fopen(path
, "r");
51 ret
= perf_pmu_parse(head
, name
);
60 * Reading/parsing the default pmu format definition, which should be
62 * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
64 static int pmu_format(char *name
, struct list_head
*format
)
70 sysfs
= sysfs_find_mountpoint();
74 snprintf(path
, PATH_MAX
,
75 "%s" EVENT_SOURCE_DEVICE_PATH
"%s/format", sysfs
, name
);
77 if (stat(path
, &st
) < 0)
78 return 0; /* no error if format does not exist */
80 if (pmu_format_parse(path
, format
))
86 static int perf_pmu__new_alias(struct list_head
*list
, char *name
, FILE *file
)
88 struct perf_pmu__alias
*alias
;
92 ret
= fread(buf
, 1, sizeof(buf
), file
);
97 alias
= malloc(sizeof(*alias
));
101 INIT_LIST_HEAD(&alias
->terms
);
102 ret
= parse_events_terms(&alias
->terms
, buf
);
108 alias
->name
= strdup(name
);
109 list_add_tail(&alias
->list
, list
);
114 * Process all the sysfs attributes located under the directory
115 * specified in 'dir' parameter.
117 static int pmu_aliases_parse(char *dir
, struct list_head
*head
)
119 struct dirent
*evt_ent
;
123 event_dir
= opendir(dir
);
127 while (!ret
&& (evt_ent
= readdir(event_dir
))) {
129 char *name
= evt_ent
->d_name
;
132 if (!strcmp(name
, ".") || !strcmp(name
, ".."))
135 snprintf(path
, PATH_MAX
, "%s/%s", dir
, name
);
138 file
= fopen(path
, "r");
141 ret
= perf_pmu__new_alias(head
, name
, file
);
150 * Reading the pmu event aliases definition, which should be located at:
151 * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
153 static int pmu_aliases(char *name
, struct list_head
*head
)
159 sysfs
= sysfs_find_mountpoint();
163 snprintf(path
, PATH_MAX
,
164 "%s/bus/event_source/devices/%s/events", sysfs
, name
);
166 if (stat(path
, &st
) < 0)
169 if (pmu_aliases_parse(path
, head
))
175 static int pmu_alias_terms(struct perf_pmu__alias
*alias
,
176 struct list_head
*terms
)
178 struct parse_events__term
*term
, *clone
;
182 list_for_each_entry(term
, &alias
->terms
, list
) {
183 ret
= parse_events__term_clone(&clone
, term
);
185 parse_events__free_terms(&list
);
188 list_add_tail(&clone
->list
, &list
);
190 list_splice(&list
, terms
);
195 * Reading/parsing the default pmu type value, which should be
197 * /sys/bus/event_source/devices/<dev>/type as sysfs attribute.
199 static int pmu_type(char *name
, __u32
*type
)
207 sysfs
= sysfs_find_mountpoint();
211 snprintf(path
, PATH_MAX
,
212 "%s" EVENT_SOURCE_DEVICE_PATH
"%s/type", sysfs
, name
);
214 if (stat(path
, &st
) < 0)
217 file
= fopen(path
, "r");
221 if (1 != fscanf(file
, "%u", type
))
228 /* Add all pmus in sysfs to pmu list: */
229 static void pmu_read_sysfs(void)
236 sysfs
= sysfs_find_mountpoint();
240 snprintf(path
, PATH_MAX
,
241 "%s" EVENT_SOURCE_DEVICE_PATH
, sysfs
);
247 while ((dent
= readdir(dir
))) {
248 if (!strcmp(dent
->d_name
, ".") || !strcmp(dent
->d_name
, ".."))
250 /* add to static LIST_HEAD(pmus): */
251 perf_pmu__find(dent
->d_name
);
257 static struct cpu_map
*pmu_cpumask(char *name
)
263 struct cpu_map
*cpus
;
265 sysfs
= sysfs_find_mountpoint();
269 snprintf(path
, PATH_MAX
,
270 "%s/bus/event_source/devices/%s/cpumask", sysfs
, name
);
272 if (stat(path
, &st
) < 0)
275 file
= fopen(path
, "r");
279 cpus
= cpu_map__read(file
);
284 static struct perf_pmu
*pmu_lookup(char *name
)
286 struct perf_pmu
*pmu
;
292 * The pmu data we store & need consists of the pmu
293 * type value and format definitions. Load both right
296 if (pmu_format(name
, &format
))
299 if (pmu_type(name
, &type
))
302 pmu
= zalloc(sizeof(*pmu
));
306 pmu
->cpus
= pmu_cpumask(name
);
308 pmu_aliases(name
, &aliases
);
310 INIT_LIST_HEAD(&pmu
->format
);
311 INIT_LIST_HEAD(&pmu
->aliases
);
312 list_splice(&format
, &pmu
->format
);
313 list_splice(&aliases
, &pmu
->aliases
);
314 pmu
->name
= strdup(name
);
316 list_add_tail(&pmu
->list
, &pmus
);
320 static struct perf_pmu
*pmu_find(char *name
)
322 struct perf_pmu
*pmu
;
324 list_for_each_entry(pmu
, &pmus
, list
)
325 if (!strcmp(pmu
->name
, name
))
331 struct perf_pmu
*perf_pmu__scan(struct perf_pmu
*pmu
)
334 * pmu iterator: If pmu is NULL, we start at the begin,
335 * otherwise return the next pmu. Returns NULL on end.
339 pmu
= list_prepare_entry(pmu
, &pmus
, list
);
341 list_for_each_entry_continue(pmu
, &pmus
, list
)
346 struct perf_pmu
*perf_pmu__find(char *name
)
348 struct perf_pmu
*pmu
;
351 * Once PMU is loaded it stays in the list,
352 * so we keep us from multiple reading/parsing
353 * the pmu format definitions.
355 pmu
= pmu_find(name
);
359 return pmu_lookup(name
);
362 static struct perf_pmu__format
*
363 pmu_find_format(struct list_head
*formats
, char *name
)
365 struct perf_pmu__format
*format
;
367 list_for_each_entry(format
, formats
, list
)
368 if (!strcmp(format
->name
, name
))
375 * Returns value based on the format definition (format parameter)
376 * and unformated value (value parameter).
378 * TODO maybe optimize a little ;)
380 static __u64
pmu_format_value(unsigned long *format
, __u64 value
)
382 unsigned long fbit
, vbit
;
385 for (fbit
= 0, vbit
= 0; fbit
< PERF_PMU_FORMAT_BITS
; fbit
++) {
387 if (!test_bit(fbit
, format
))
390 if (!(value
& (1llu << vbit
++)))
400 * Setup one of config[12] attr members based on the
401 * user input data - temr parameter.
403 static int pmu_config_term(struct list_head
*formats
,
404 struct perf_event_attr
*attr
,
405 struct parse_events__term
*term
)
407 struct perf_pmu__format
*format
;
411 * Support only for hardcoded and numnerial terms.
412 * Hardcoded terms should be already in, so nothing
413 * to be done for them.
415 if (parse_events__is_hardcoded_term(term
))
418 if (term
->type_val
!= PARSE_EVENTS__TERM_TYPE_NUM
)
421 format
= pmu_find_format(formats
, term
->config
);
425 switch (format
->value
) {
426 case PERF_PMU_FORMAT_VALUE_CONFIG
:
429 case PERF_PMU_FORMAT_VALUE_CONFIG1
:
432 case PERF_PMU_FORMAT_VALUE_CONFIG2
:
440 * XXX If we ever decide to go with string values for
441 * non-hardcoded terms, here's the place to translate
444 *vp
|= pmu_format_value(format
->bits
, term
->val
.num
);
448 static int pmu_config(struct list_head
*formats
, struct perf_event_attr
*attr
,
449 struct list_head
*head_terms
)
451 struct parse_events__term
*term
;
453 list_for_each_entry(term
, head_terms
, list
)
454 if (pmu_config_term(formats
, attr
, term
))
461 * Configures event's 'attr' parameter based on the:
462 * 1) users input - specified in terms parameter
463 * 2) pmu format definitions - specified by pmu parameter
465 int perf_pmu__config(struct perf_pmu
*pmu
, struct perf_event_attr
*attr
,
466 struct list_head
*head_terms
)
468 attr
->type
= pmu
->type
;
469 return pmu_config(&pmu
->format
, attr
, head_terms
);
472 static struct perf_pmu__alias
*pmu_find_alias(struct perf_pmu
*pmu
,
473 struct parse_events__term
*term
)
475 struct perf_pmu__alias
*alias
;
478 if (parse_events__is_hardcoded_term(term
))
481 if (term
->type_val
== PARSE_EVENTS__TERM_TYPE_NUM
) {
482 if (term
->val
.num
!= 1)
484 if (pmu_find_format(&pmu
->format
, term
->config
))
487 } else if (term
->type_val
== PARSE_EVENTS__TERM_TYPE_STR
) {
488 if (strcasecmp(term
->config
, "event"))
490 name
= term
->val
.str
;
495 list_for_each_entry(alias
, &pmu
->aliases
, list
) {
496 if (!strcasecmp(alias
->name
, name
))
503 * Find alias in the terms list and replace it with the terms
504 * defined for the alias
506 int perf_pmu__check_alias(struct perf_pmu
*pmu
, struct list_head
*head_terms
)
508 struct parse_events__term
*term
, *h
;
509 struct perf_pmu__alias
*alias
;
512 list_for_each_entry_safe(term
, h
, head_terms
, list
) {
513 alias
= pmu_find_alias(pmu
, term
);
516 ret
= pmu_alias_terms(alias
, &term
->list
);
519 list_del(&term
->list
);
525 int perf_pmu__new_format(struct list_head
*list
, char *name
,
526 int config
, unsigned long *bits
)
528 struct perf_pmu__format
*format
;
530 format
= zalloc(sizeof(*format
));
534 format
->name
= strdup(name
);
535 format
->value
= config
;
536 memcpy(format
->bits
, bits
, sizeof(format
->bits
));
538 list_add_tail(&format
->list
, list
);
542 void perf_pmu__set_format(unsigned long *bits
, long from
, long to
)
549 memset(bits
, 0, BITS_TO_LONGS(PERF_PMU_FORMAT_BITS
));
550 for (b
= from
; b
<= to
; b
++)
554 /* Simulated format definitions. */
555 static struct test_format
{
559 { "krava01", "config:0-1,62-63\n", },
560 { "krava02", "config:10-17\n", },
561 { "krava03", "config:5\n", },
562 { "krava11", "config1:0,2,4,6,8,20-28\n", },
563 { "krava12", "config1:63\n", },
564 { "krava13", "config1:45-47\n", },
565 { "krava21", "config2:0-3,10-13,20-23,30-33,40-43,50-53,60-63\n", },
566 { "krava22", "config2:8,18,48,58\n", },
567 { "krava23", "config2:28-29,38\n", },
570 #define TEST_FORMATS_CNT (sizeof(test_formats) / sizeof(struct test_format))
572 /* Simulated users input. */
573 static struct parse_events__term test_terms
[] = {
575 .config
= (char *) "krava01",
577 .type_val
= PARSE_EVENTS__TERM_TYPE_NUM
,
578 .type_term
= PARSE_EVENTS__TERM_TYPE_USER
,
581 .config
= (char *) "krava02",
583 .type_val
= PARSE_EVENTS__TERM_TYPE_NUM
,
584 .type_term
= PARSE_EVENTS__TERM_TYPE_USER
,
587 .config
= (char *) "krava03",
589 .type_val
= PARSE_EVENTS__TERM_TYPE_NUM
,
590 .type_term
= PARSE_EVENTS__TERM_TYPE_USER
,
593 .config
= (char *) "krava11",
595 .type_val
= PARSE_EVENTS__TERM_TYPE_NUM
,
596 .type_term
= PARSE_EVENTS__TERM_TYPE_USER
,
599 .config
= (char *) "krava12",
601 .type_val
= PARSE_EVENTS__TERM_TYPE_NUM
,
602 .type_term
= PARSE_EVENTS__TERM_TYPE_USER
,
605 .config
= (char *) "krava13",
607 .type_val
= PARSE_EVENTS__TERM_TYPE_NUM
,
608 .type_term
= PARSE_EVENTS__TERM_TYPE_USER
,
611 .config
= (char *) "krava21",
613 .type_val
= PARSE_EVENTS__TERM_TYPE_NUM
,
614 .type_term
= PARSE_EVENTS__TERM_TYPE_USER
,
617 .config
= (char *) "krava22",
619 .type_val
= PARSE_EVENTS__TERM_TYPE_NUM
,
620 .type_term
= PARSE_EVENTS__TERM_TYPE_USER
,
623 .config
= (char *) "krava23",
625 .type_val
= PARSE_EVENTS__TERM_TYPE_NUM
,
626 .type_term
= PARSE_EVENTS__TERM_TYPE_USER
,
629 #define TERMS_CNT (sizeof(test_terms) / sizeof(struct parse_events__term))
632 * Prepare format directory data, exported by kernel
633 * at /sys/bus/event_source/devices/<dev>/format.
635 static char *test_format_dir_get(void)
637 static char dir
[PATH_MAX
];
640 snprintf(dir
, PATH_MAX
, "/tmp/perf-pmu-test-format-XXXXXX");
644 for (i
= 0; i
< TEST_FORMATS_CNT
; i
++) {
645 static char name
[PATH_MAX
];
646 struct test_format
*format
= &test_formats
[i
];
649 snprintf(name
, PATH_MAX
, "%s/%s", dir
, format
->name
);
651 file
= fopen(name
, "w");
655 if (1 != fwrite(format
->value
, strlen(format
->value
), 1, file
))
664 /* Cleanup format directory. */
665 static int test_format_dir_put(char *dir
)
668 snprintf(buf
, PATH_MAX
, "rm -f %s/*\n", dir
);
672 snprintf(buf
, PATH_MAX
, "rmdir %s\n", dir
);
676 static struct list_head
*test_terms_list(void)
678 static LIST_HEAD(terms
);
681 for (i
= 0; i
< TERMS_CNT
; i
++)
682 list_add_tail(&test_terms
[i
].list
, &terms
);
689 int perf_pmu__test(void)
691 char *format
= test_format_dir_get();
693 struct list_head
*terms
= test_terms_list();
700 struct perf_event_attr attr
;
702 memset(&attr
, 0, sizeof(attr
));
704 ret
= pmu_format_parse(format
, &formats
);
708 ret
= pmu_config(&formats
, &attr
, terms
);
714 if (attr
.config
!= 0xc00000000002a823)
716 if (attr
.config1
!= 0x8000400000000145)
718 if (attr
.config2
!= 0x0400000020041d07)
724 test_format_dir_put(format
);