1 #include <linux/list.h>
10 #include "parse-events.h"
13 struct perf_pmu_alias {
15 struct list_head terms;
16 struct list_head list;
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__new_alias(struct list_head *list, char *name, FILE *file)
99 struct perf_pmu_alias *alias;
103 ret = fread(buf, 1, sizeof(buf), file);
108 alias = malloc(sizeof(*alias));
112 INIT_LIST_HEAD(&alias->terms);
113 ret = parse_events_terms(&alias->terms, buf);
119 alias->name = strdup(name);
120 list_add_tail(&alias->list, list);
125 * Process all the sysfs attributes located under the directory
126 * specified in 'dir' parameter.
128 static int pmu_aliases_parse(char *dir, struct list_head *head)
130 struct dirent *evt_ent;
134 event_dir = opendir(dir);
138 while (!ret && (evt_ent = readdir(event_dir))) {
140 char *name = evt_ent->d_name;
143 if (!strcmp(name, ".") || !strcmp(name, ".."))
146 snprintf(path, PATH_MAX, "%s/%s", dir, name);
149 file = fopen(path, "r");
152 ret = perf_pmu__new_alias(head, name, file);
161 * Reading the pmu event aliases definition, which should be located at:
162 * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
164 static int pmu_aliases(const char *name, struct list_head *head)
168 const char *sysfs = sysfs__mountpoint();
173 snprintf(path, PATH_MAX,
174 "%s/bus/event_source/devices/%s/events", sysfs, name);
176 if (stat(path, &st) < 0)
177 return 0; /* no error if 'events' does not exist */
179 if (pmu_aliases_parse(path, head))
185 static int pmu_alias_terms(struct perf_pmu_alias *alias,
186 struct list_head *terms)
188 struct parse_events_term *term, *clone;
192 list_for_each_entry(term, &alias->terms, list) {
193 ret = parse_events_term__clone(&clone, term);
195 parse_events__free_terms(&list);
198 list_add_tail(&clone->list, &list);
200 list_splice(&list, terms);
205 * Reading/parsing the default pmu type value, which should be
207 * /sys/bus/event_source/devices/<dev>/type as sysfs attribute.
209 static int pmu_type(const char *name, __u32 *type)
215 const char *sysfs = sysfs__mountpoint();
220 snprintf(path, PATH_MAX,
221 "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name);
223 if (stat(path, &st) < 0)
226 file = fopen(path, "r");
230 if (1 != fscanf(file, "%u", type))
237 /* Add all pmus in sysfs to pmu list: */
238 static void pmu_read_sysfs(void)
243 const char *sysfs = sysfs__mountpoint();
248 snprintf(path, PATH_MAX,
249 "%s" EVENT_SOURCE_DEVICE_PATH, sysfs);
255 while ((dent = readdir(dir))) {
256 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
258 /* add to static LIST_HEAD(pmus): */
259 perf_pmu__find(dent->d_name);
265 static struct cpu_map *pmu_cpumask(const char *name)
270 struct cpu_map *cpus;
271 const char *sysfs = sysfs__mountpoint();
276 snprintf(path, PATH_MAX,
277 "%s/bus/event_source/devices/%s/cpumask", sysfs, name);
279 if (stat(path, &st) < 0)
282 file = fopen(path, "r");
286 cpus = cpu_map__read(file);
291 static struct perf_pmu *pmu_lookup(const char *name)
293 struct perf_pmu *pmu;
299 * The pmu data we store & need consists of the pmu
300 * type value and format definitions. Load both right
303 if (pmu_format(name, &format))
306 if (pmu_aliases(name, &aliases))
309 if (pmu_type(name, &type))
312 pmu = zalloc(sizeof(*pmu));
316 pmu->cpus = pmu_cpumask(name);
318 INIT_LIST_HEAD(&pmu->format);
319 INIT_LIST_HEAD(&pmu->aliases);
320 list_splice(&format, &pmu->format);
321 list_splice(&aliases, &pmu->aliases);
322 pmu->name = strdup(name);
324 list_add_tail(&pmu->list, &pmus);
328 static struct perf_pmu *pmu_find(const char *name)
330 struct perf_pmu *pmu;
332 list_for_each_entry(pmu, &pmus, list)
333 if (!strcmp(pmu->name, name))
339 struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
342 * pmu iterator: If pmu is NULL, we start at the begin,
343 * otherwise return the next pmu. Returns NULL on end.
347 pmu = list_prepare_entry(pmu, &pmus, list);
349 list_for_each_entry_continue(pmu, &pmus, list)
354 struct perf_pmu *perf_pmu__find(const char *name)
356 struct perf_pmu *pmu;
359 * Once PMU is loaded it stays in the list,
360 * so we keep us from multiple reading/parsing
361 * the pmu format definitions.
363 pmu = pmu_find(name);
367 return pmu_lookup(name);
370 static struct perf_pmu_format *
371 pmu_find_format(struct list_head *formats, char *name)
373 struct perf_pmu_format *format;
375 list_for_each_entry(format, formats, list)
376 if (!strcmp(format->name, name))
383 * Returns value based on the format definition (format parameter)
384 * and unformated value (value parameter).
386 * TODO maybe optimize a little ;)
388 static __u64 pmu_format_value(unsigned long *format, __u64 value)
390 unsigned long fbit, vbit;
393 for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
395 if (!test_bit(fbit, format))
398 if (!(value & (1llu << vbit++)))
408 * Setup one of config[12] attr members based on the
409 * user input data - temr parameter.
411 static int pmu_config_term(struct list_head *formats,
412 struct perf_event_attr *attr,
413 struct parse_events_term *term)
415 struct perf_pmu_format *format;
419 * Support only for hardcoded and numnerial terms.
420 * Hardcoded terms should be already in, so nothing
421 * to be done for them.
423 if (parse_events__is_hardcoded_term(term))
426 if (term->type_val != PARSE_EVENTS__TERM_TYPE_NUM)
429 format = pmu_find_format(formats, term->config);
433 switch (format->value) {
434 case PERF_PMU_FORMAT_VALUE_CONFIG:
437 case PERF_PMU_FORMAT_VALUE_CONFIG1:
440 case PERF_PMU_FORMAT_VALUE_CONFIG2:
448 * XXX If we ever decide to go with string values for
449 * non-hardcoded terms, here's the place to translate
452 *vp |= pmu_format_value(format->bits, term->val.num);
456 int perf_pmu__config_terms(struct list_head *formats,
457 struct perf_event_attr *attr,
458 struct list_head *head_terms)
460 struct parse_events_term *term;
462 list_for_each_entry(term, head_terms, list)
463 if (pmu_config_term(formats, attr, term))
470 * Configures event's 'attr' parameter based on the:
471 * 1) users input - specified in terms parameter
472 * 2) pmu format definitions - specified by pmu parameter
474 int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
475 struct list_head *head_terms)
477 attr->type = pmu->type;
478 return perf_pmu__config_terms(&pmu->format, attr, head_terms);
481 static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
482 struct parse_events_term *term)
484 struct perf_pmu_alias *alias;
487 if (parse_events__is_hardcoded_term(term))
490 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
491 if (term->val.num != 1)
493 if (pmu_find_format(&pmu->format, term->config))
496 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
497 if (strcasecmp(term->config, "event"))
499 name = term->val.str;
504 list_for_each_entry(alias, &pmu->aliases, list) {
505 if (!strcasecmp(alias->name, name))
512 * Find alias in the terms list and replace it with the terms
513 * defined for the alias
515 int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms)
517 struct parse_events_term *term, *h;
518 struct perf_pmu_alias *alias;
521 list_for_each_entry_safe(term, h, head_terms, list) {
522 alias = pmu_find_alias(pmu, term);
525 ret = pmu_alias_terms(alias, &term->list);
528 list_del(&term->list);
534 int perf_pmu__new_format(struct list_head *list, char *name,
535 int config, unsigned long *bits)
537 struct perf_pmu_format *format;
539 format = zalloc(sizeof(*format));
543 format->name = strdup(name);
544 format->value = config;
545 memcpy(format->bits, bits, sizeof(format->bits));
547 list_add_tail(&format->list, list);
551 void perf_pmu__set_format(unsigned long *bits, long from, long to)
558 memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
559 for (b = from; b <= to; b++)
563 static char *format_alias(char *buf, int len, struct perf_pmu *pmu,
564 struct perf_pmu_alias *alias)
566 snprintf(buf, len, "%s/%s/", pmu->name, alias->name);
570 static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu,
571 struct perf_pmu_alias *alias)
573 snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name);
577 static int cmp_string(const void *a, const void *b)
579 const char * const *as = a;
580 const char * const *bs = b;
581 return strcmp(*as, *bs);
584 void print_pmu_events(const char *event_glob, bool name_only)
586 struct perf_pmu *pmu;
587 struct perf_pmu_alias *alias;
595 while ((pmu = perf_pmu__scan(pmu)) != NULL)
596 list_for_each_entry(alias, &pmu->aliases, list)
598 aliases = malloc(sizeof(char *) * len);
603 while ((pmu = perf_pmu__scan(pmu)) != NULL)
604 list_for_each_entry(alias, &pmu->aliases, list) {
605 char *name = format_alias(buf, sizeof(buf), pmu, alias);
606 bool is_cpu = !strcmp(pmu->name, "cpu");
608 if (event_glob != NULL &&
609 !(strglobmatch(name, event_glob) ||
610 (!is_cpu && strglobmatch(alias->name,
614 if (is_cpu && !name_only)
615 aliases[j] = format_alias_or(buf, sizeof(buf),
617 aliases[j] = strdup(aliases[j]);
621 qsort(aliases, len, sizeof(char *), cmp_string);
622 for (j = 0; j < len; j++) {
624 printf("%s ", aliases[j]);
627 printf(" %-50s [Kernel PMU event]\n", aliases[j]);
636 bool pmu_have_event(const char *pname, const char *name)
638 struct perf_pmu *pmu;
639 struct perf_pmu_alias *alias;
642 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
643 if (strcmp(pname, pmu->name))
645 list_for_each_entry(alias, &pmu->aliases, list)
646 if (!strcmp(alias->name, name))