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 int perf_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 (perf_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)
167 return 0; /* no error if 'events' does not exist */
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_aliases(name, &aliases))
302 if (pmu_type(name, &type))
305 pmu = zalloc(sizeof(*pmu));
309 pmu->cpus = pmu_cpumask(name);
311 INIT_LIST_HEAD(&pmu->format);
312 INIT_LIST_HEAD(&pmu->aliases);
313 list_splice(&format, &pmu->format);
314 list_splice(&aliases, &pmu->aliases);
315 pmu->name = strdup(name);
317 list_add_tail(&pmu->list, &pmus);
321 static struct perf_pmu *pmu_find(char *name)
323 struct perf_pmu *pmu;
325 list_for_each_entry(pmu, &pmus, list)
326 if (!strcmp(pmu->name, name))
332 struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
335 * pmu iterator: If pmu is NULL, we start at the begin,
336 * otherwise return the next pmu. Returns NULL on end.
340 pmu = list_prepare_entry(pmu, &pmus, list);
342 list_for_each_entry_continue(pmu, &pmus, list)
347 struct perf_pmu *perf_pmu__find(char *name)
349 struct perf_pmu *pmu;
352 * Once PMU is loaded it stays in the list,
353 * so we keep us from multiple reading/parsing
354 * the pmu format definitions.
356 pmu = pmu_find(name);
360 return pmu_lookup(name);
363 static struct perf_pmu__format*
364 pmu_find_format(struct list_head *formats, char *name)
366 struct perf_pmu__format *format;
368 list_for_each_entry(format, formats, list)
369 if (!strcmp(format->name, name))
376 * Returns value based on the format definition (format parameter)
377 * and unformated value (value parameter).
379 * TODO maybe optimize a little ;)
381 static __u64 pmu_format_value(unsigned long *format, __u64 value)
383 unsigned long fbit, vbit;
386 for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
388 if (!test_bit(fbit, format))
391 if (!(value & (1llu << vbit++)))
401 * Setup one of config[12] attr members based on the
402 * user input data - temr parameter.
404 static int pmu_config_term(struct list_head *formats,
405 struct perf_event_attr *attr,
406 struct parse_events__term *term)
408 struct perf_pmu__format *format;
412 * Support only for hardcoded and numnerial terms.
413 * Hardcoded terms should be already in, so nothing
414 * to be done for them.
416 if (parse_events__is_hardcoded_term(term))
419 if (term->type_val != PARSE_EVENTS__TERM_TYPE_NUM)
422 format = pmu_find_format(formats, term->config);
426 switch (format->value) {
427 case PERF_PMU_FORMAT_VALUE_CONFIG:
430 case PERF_PMU_FORMAT_VALUE_CONFIG1:
433 case PERF_PMU_FORMAT_VALUE_CONFIG2:
441 * XXX If we ever decide to go with string values for
442 * non-hardcoded terms, here's the place to translate
445 *vp |= pmu_format_value(format->bits, term->val.num);
449 int perf_pmu__config_terms(struct list_head *formats,
450 struct perf_event_attr *attr,
451 struct list_head *head_terms)
453 struct parse_events__term *term;
455 list_for_each_entry(term, head_terms, list)
456 if (pmu_config_term(formats, attr, term))
463 * Configures event's 'attr' parameter based on the:
464 * 1) users input - specified in terms parameter
465 * 2) pmu format definitions - specified by pmu parameter
467 int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
468 struct list_head *head_terms)
470 attr->type = pmu->type;
471 return perf_pmu__config_terms(&pmu->format, attr, head_terms);
474 static struct perf_pmu__alias *pmu_find_alias(struct perf_pmu *pmu,
475 struct parse_events__term *term)
477 struct perf_pmu__alias *alias;
480 if (parse_events__is_hardcoded_term(term))
483 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
484 if (term->val.num != 1)
486 if (pmu_find_format(&pmu->format, term->config))
489 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
490 if (strcasecmp(term->config, "event"))
492 name = term->val.str;
497 list_for_each_entry(alias, &pmu->aliases, list) {
498 if (!strcasecmp(alias->name, name))
505 * Find alias in the terms list and replace it with the terms
506 * defined for the alias
508 int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms)
510 struct parse_events__term *term, *h;
511 struct perf_pmu__alias *alias;
514 list_for_each_entry_safe(term, h, head_terms, list) {
515 alias = pmu_find_alias(pmu, term);
518 ret = pmu_alias_terms(alias, &term->list);
521 list_del(&term->list);
527 int perf_pmu__new_format(struct list_head *list, char *name,
528 int config, unsigned long *bits)
530 struct perf_pmu__format *format;
532 format = zalloc(sizeof(*format));
536 format->name = strdup(name);
537 format->value = config;
538 memcpy(format->bits, bits, sizeof(format->bits));
540 list_add_tail(&format->list, list);
544 void perf_pmu__set_format(unsigned long *bits, long from, long to)
551 memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
552 for (b = from; b <= to; b++)