2 #include <linux/list.h>
11 #include "parse-events.h"
13 int perf_pmu_parse(struct list_head
*list
, char *name
);
14 extern FILE *perf_pmu_in
;
16 static LIST_HEAD(pmus
);
19 * Parse & process all the sysfs attributes located under
20 * the directory specified in 'dir' parameter.
22 static int pmu_format_parse(char *dir
, struct list_head
*head
)
24 struct dirent
*evt_ent
;
28 format_dir
= opendir(dir
);
32 while (!ret
&& (evt_ent
= readdir(format_dir
))) {
34 char *name
= evt_ent
->d_name
;
37 if (!strcmp(name
, ".") || !strcmp(name
, ".."))
40 snprintf(path
, PATH_MAX
, "%s/%s", dir
, name
);
43 file
= fopen(path
, "r");
48 ret
= perf_pmu_parse(head
, name
);
57 * Reading/parsing the default pmu format definition, which should be
59 * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
61 static int pmu_format(char *name
, struct list_head
*format
)
67 sysfs
= sysfs_find_mountpoint();
71 snprintf(path
, PATH_MAX
,
72 "%s/bus/event_source/devices/%s/format", sysfs
, name
);
74 if (stat(path
, &st
) < 0)
75 return 0; /* no error if format does not exist */
77 if (pmu_format_parse(path
, format
))
83 static int perf_pmu__new_alias(struct list_head
*list
, char *name
, FILE *file
)
85 struct perf_pmu__alias
*alias
;
89 ret
= fread(buf
, 1, sizeof(buf
), file
);
94 alias
= malloc(sizeof(*alias
));
98 INIT_LIST_HEAD(&alias
->terms
);
99 ret
= parse_events_terms(&alias
->terms
, buf
);
105 alias
->name
= strdup(name
);
106 list_add_tail(&alias
->list
, list
);
111 * Process all the sysfs attributes located under the directory
112 * specified in 'dir' parameter.
114 static int pmu_aliases_parse(char *dir
, struct list_head
*head
)
116 struct dirent
*evt_ent
;
120 event_dir
= opendir(dir
);
124 while (!ret
&& (evt_ent
= readdir(event_dir
))) {
126 char *name
= evt_ent
->d_name
;
129 if (!strcmp(name
, ".") || !strcmp(name
, ".."))
132 snprintf(path
, PATH_MAX
, "%s/%s", dir
, name
);
135 file
= fopen(path
, "r");
138 ret
= perf_pmu__new_alias(head
, name
, file
);
147 * Reading the pmu event aliases definition, which should be located at:
148 * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
150 static int pmu_aliases(char *name
, struct list_head
*head
)
156 sysfs
= sysfs_find_mountpoint();
160 snprintf(path
, PATH_MAX
,
161 "%s/bus/event_source/devices/%s/events", sysfs
, name
);
163 if (stat(path
, &st
) < 0)
166 if (pmu_aliases_parse(path
, head
))
172 static int pmu_alias_terms(struct perf_pmu__alias
*alias
,
173 struct list_head
*terms
)
175 struct parse_events__term
*term
, *clone
;
179 list_for_each_entry(term
, &alias
->terms
, list
) {
180 ret
= parse_events__term_clone(&clone
, term
);
182 parse_events__free_terms(&list
);
185 list_add_tail(&clone
->list
, &list
);
187 list_splice(&list
, terms
);
192 * Reading/parsing the default pmu type value, which should be
194 * /sys/bus/event_source/devices/<dev>/type as sysfs attribute.
196 static int pmu_type(char *name
, __u32
*type
)
204 sysfs
= sysfs_find_mountpoint();
208 snprintf(path
, PATH_MAX
,
209 "%s/bus/event_source/devices/%s/type", sysfs
, name
);
211 if (stat(path
, &st
) < 0)
214 file
= fopen(path
, "r");
218 if (1 != fscanf(file
, "%u", type
))
225 static struct perf_pmu
*pmu_lookup(char *name
)
227 struct perf_pmu
*pmu
;
233 * The pmu data we store & need consists of the pmu
234 * type value and format definitions. Load both right
237 if (pmu_format(name
, &format
))
240 if (pmu_type(name
, &type
))
243 pmu
= zalloc(sizeof(*pmu
));
247 pmu_aliases(name
, &aliases
);
249 INIT_LIST_HEAD(&pmu
->format
);
250 INIT_LIST_HEAD(&pmu
->aliases
);
251 list_splice(&format
, &pmu
->format
);
252 list_splice(&aliases
, &pmu
->aliases
);
253 pmu
->name
= strdup(name
);
255 list_add_tail(&pmu
->list
, &pmus
);
259 static struct perf_pmu
*pmu_find(char *name
)
261 struct perf_pmu
*pmu
;
263 list_for_each_entry(pmu
, &pmus
, list
)
264 if (!strcmp(pmu
->name
, name
))
270 struct perf_pmu
*perf_pmu__find(char *name
)
272 struct perf_pmu
*pmu
;
275 * Once PMU is loaded it stays in the list,
276 * so we keep us from multiple reading/parsing
277 * the pmu format definitions.
279 pmu
= pmu_find(name
);
283 return pmu_lookup(name
);
286 static struct perf_pmu__format
*
287 pmu_find_format(struct list_head
*formats
, char *name
)
289 struct perf_pmu__format
*format
;
291 list_for_each_entry(format
, formats
, list
)
292 if (!strcmp(format
->name
, name
))
299 * Returns value based on the format definition (format parameter)
300 * and unformated value (value parameter).
302 * TODO maybe optimize a little ;)
304 static __u64
pmu_format_value(unsigned long *format
, __u64 value
)
306 unsigned long fbit
, vbit
;
309 for (fbit
= 0, vbit
= 0; fbit
< PERF_PMU_FORMAT_BITS
; fbit
++) {
311 if (!test_bit(fbit
, format
))
314 if (!(value
& (1llu << vbit
++)))
324 * Setup one of config[12] attr members based on the
325 * user input data - temr parameter.
327 static int pmu_config_term(struct list_head
*formats
,
328 struct perf_event_attr
*attr
,
329 struct parse_events__term
*term
)
331 struct perf_pmu__format
*format
;
335 * Support only for hardcoded and numnerial terms.
336 * Hardcoded terms should be already in, so nothing
337 * to be done for them.
339 if (parse_events__is_hardcoded_term(term
))
342 if (term
->type_val
!= PARSE_EVENTS__TERM_TYPE_NUM
)
345 format
= pmu_find_format(formats
, term
->config
);
349 switch (format
->value
) {
350 case PERF_PMU_FORMAT_VALUE_CONFIG
:
353 case PERF_PMU_FORMAT_VALUE_CONFIG1
:
356 case PERF_PMU_FORMAT_VALUE_CONFIG2
:
364 * XXX If we ever decide to go with string values for
365 * non-hardcoded terms, here's the place to translate
368 *vp
|= pmu_format_value(format
->bits
, term
->val
.num
);
372 static int pmu_config(struct list_head
*formats
, struct perf_event_attr
*attr
,
373 struct list_head
*head_terms
)
375 struct parse_events__term
*term
;
377 list_for_each_entry(term
, head_terms
, list
)
378 if (pmu_config_term(formats
, attr
, term
))
385 * Configures event's 'attr' parameter based on the:
386 * 1) users input - specified in terms parameter
387 * 2) pmu format definitions - specified by pmu parameter
389 int perf_pmu__config(struct perf_pmu
*pmu
, struct perf_event_attr
*attr
,
390 struct list_head
*head_terms
)
392 attr
->type
= pmu
->type
;
393 return pmu_config(&pmu
->format
, attr
, head_terms
);
396 static struct perf_pmu__alias
*pmu_find_alias(struct perf_pmu
*pmu
,
397 struct parse_events__term
*term
)
399 struct perf_pmu__alias
*alias
;
402 if (parse_events__is_hardcoded_term(term
))
405 if (term
->type_val
== PARSE_EVENTS__TERM_TYPE_NUM
) {
406 if (term
->val
.num
!= 1)
408 if (pmu_find_format(&pmu
->format
, term
->config
))
411 } else if (term
->type_val
== PARSE_EVENTS__TERM_TYPE_STR
) {
412 if (strcasecmp(term
->config
, "event"))
414 name
= term
->val
.str
;
419 list_for_each_entry(alias
, &pmu
->aliases
, list
) {
420 if (!strcasecmp(alias
->name
, name
))
427 * Find alias in the terms list and replace it with the terms
428 * defined for the alias
430 int perf_pmu__check_alias(struct perf_pmu
*pmu
, struct list_head
*head_terms
)
432 struct parse_events__term
*term
, *h
;
433 struct perf_pmu__alias
*alias
;
436 list_for_each_entry_safe(term
, h
, head_terms
, list
) {
437 alias
= pmu_find_alias(pmu
, term
);
440 ret
= pmu_alias_terms(alias
, &term
->list
);
443 list_del(&term
->list
);
449 int perf_pmu__new_format(struct list_head
*list
, char *name
,
450 int config
, unsigned long *bits
)
452 struct perf_pmu__format
*format
;
454 format
= zalloc(sizeof(*format
));
458 format
->name
= strdup(name
);
459 format
->value
= config
;
460 memcpy(format
->bits
, bits
, sizeof(format
->bits
));
462 list_add_tail(&format
->list
, list
);
466 void perf_pmu__set_format(unsigned long *bits
, long from
, long to
)
473 memset(bits
, 0, BITS_TO_LONGS(PERF_PMU_FORMAT_BITS
));
474 for (b
= from
; b
<= to
; b
++)
478 /* Simulated format definitions. */
479 static struct test_format
{
483 { "krava01", "config:0-1,62-63\n", },
484 { "krava02", "config:10-17\n", },
485 { "krava03", "config:5\n", },
486 { "krava11", "config1:0,2,4,6,8,20-28\n", },
487 { "krava12", "config1:63\n", },
488 { "krava13", "config1:45-47\n", },
489 { "krava21", "config2:0-3,10-13,20-23,30-33,40-43,50-53,60-63\n", },
490 { "krava22", "config2:8,18,48,58\n", },
491 { "krava23", "config2:28-29,38\n", },
494 #define TEST_FORMATS_CNT (sizeof(test_formats) / sizeof(struct test_format))
496 /* Simulated users input. */
497 static struct parse_events__term test_terms
[] = {
499 .config
= (char *) "krava01",
501 .type_val
= PARSE_EVENTS__TERM_TYPE_NUM
,
502 .type_term
= PARSE_EVENTS__TERM_TYPE_USER
,
505 .config
= (char *) "krava02",
507 .type_val
= PARSE_EVENTS__TERM_TYPE_NUM
,
508 .type_term
= PARSE_EVENTS__TERM_TYPE_USER
,
511 .config
= (char *) "krava03",
513 .type_val
= PARSE_EVENTS__TERM_TYPE_NUM
,
514 .type_term
= PARSE_EVENTS__TERM_TYPE_USER
,
517 .config
= (char *) "krava11",
519 .type_val
= PARSE_EVENTS__TERM_TYPE_NUM
,
520 .type_term
= PARSE_EVENTS__TERM_TYPE_USER
,
523 .config
= (char *) "krava12",
525 .type_val
= PARSE_EVENTS__TERM_TYPE_NUM
,
526 .type_term
= PARSE_EVENTS__TERM_TYPE_USER
,
529 .config
= (char *) "krava13",
531 .type_val
= PARSE_EVENTS__TERM_TYPE_NUM
,
532 .type_term
= PARSE_EVENTS__TERM_TYPE_USER
,
535 .config
= (char *) "krava21",
537 .type_val
= PARSE_EVENTS__TERM_TYPE_NUM
,
538 .type_term
= PARSE_EVENTS__TERM_TYPE_USER
,
541 .config
= (char *) "krava22",
543 .type_val
= PARSE_EVENTS__TERM_TYPE_NUM
,
544 .type_term
= PARSE_EVENTS__TERM_TYPE_USER
,
547 .config
= (char *) "krava23",
549 .type_val
= PARSE_EVENTS__TERM_TYPE_NUM
,
550 .type_term
= PARSE_EVENTS__TERM_TYPE_USER
,
553 #define TERMS_CNT (sizeof(test_terms) / sizeof(struct parse_events__term))
556 * Prepare format directory data, exported by kernel
557 * at /sys/bus/event_source/devices/<dev>/format.
559 static char *test_format_dir_get(void)
561 static char dir
[PATH_MAX
];
564 snprintf(dir
, PATH_MAX
, "/tmp/perf-pmu-test-format-XXXXXX");
568 for (i
= 0; i
< TEST_FORMATS_CNT
; i
++) {
569 static char name
[PATH_MAX
];
570 struct test_format
*format
= &test_formats
[i
];
573 snprintf(name
, PATH_MAX
, "%s/%s", dir
, format
->name
);
575 file
= fopen(name
, "w");
579 if (1 != fwrite(format
->value
, strlen(format
->value
), 1, file
))
588 /* Cleanup format directory. */
589 static int test_format_dir_put(char *dir
)
592 snprintf(buf
, PATH_MAX
, "rm -f %s/*\n", dir
);
596 snprintf(buf
, PATH_MAX
, "rmdir %s\n", dir
);
600 static struct list_head
*test_terms_list(void)
602 static LIST_HEAD(terms
);
605 for (i
= 0; i
< TERMS_CNT
; i
++)
606 list_add_tail(&test_terms
[i
].list
, &terms
);
613 int perf_pmu__test(void)
615 char *format
= test_format_dir_get();
617 struct list_head
*terms
= test_terms_list();
624 struct perf_event_attr attr
;
626 memset(&attr
, 0, sizeof(attr
));
628 ret
= pmu_format_parse(format
, &formats
);
632 ret
= pmu_config(&formats
, &attr
, terms
);
638 if (attr
.config
!= 0xc00000000002a823)
640 if (attr
.config1
!= 0x8000400000000145)
642 if (attr
.config2
!= 0x0400000020041d07)
648 test_format_dir_put(format
);