blob: bc9d8069d37637835758d02e13ec3bc751d0f5e3 [file] [log] [blame]
Jiri Olsacd82a322012-03-15 20:09:17 +01001#include <linux/list.h>
2#include <sys/types.h>
3#include <sys/stat.h>
4#include <unistd.h>
5#include <stdio.h>
6#include <dirent.h>
7#include "sysfs.h"
8#include "util.h"
9#include "pmu.h"
10#include "parse-events.h"
Yan, Zheng7ae92e72012-09-10 15:53:50 +080011#include "cpumap.h"
Jiri Olsacd82a322012-03-15 20:09:17 +010012
Arnaldo Carvalho de Meloab1bf6532013-01-18 17:05:09 -030013struct perf_pmu_alias {
14 char *name;
15 struct list_head terms;
16 struct list_head list;
17};
18
19struct perf_pmu_format {
20 char *name;
21 int value;
22 DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
23 struct list_head list;
24};
25
Robert Richter50a96672012-08-16 21:10:24 +020026#define EVENT_SOURCE_DEVICE_PATH "/bus/event_source/devices/"
27
Jiri Olsacd82a322012-03-15 20:09:17 +010028int perf_pmu_parse(struct list_head *list, char *name);
29extern FILE *perf_pmu_in;
30
31static LIST_HEAD(pmus);
32
33/*
34 * Parse & process all the sysfs attributes located under
35 * the directory specified in 'dir' parameter.
36 */
Jiri Olsacff7f952012-11-10 01:46:50 +010037int perf_pmu__format_parse(char *dir, struct list_head *head)
Jiri Olsacd82a322012-03-15 20:09:17 +010038{
39 struct dirent *evt_ent;
40 DIR *format_dir;
41 int ret = 0;
42
43 format_dir = opendir(dir);
44 if (!format_dir)
45 return -EINVAL;
46
47 while (!ret && (evt_ent = readdir(format_dir))) {
48 char path[PATH_MAX];
49 char *name = evt_ent->d_name;
50 FILE *file;
51
52 if (!strcmp(name, ".") || !strcmp(name, ".."))
53 continue;
54
55 snprintf(path, PATH_MAX, "%s/%s", dir, name);
56
57 ret = -EINVAL;
58 file = fopen(path, "r");
59 if (!file)
60 break;
61
62 perf_pmu_in = file;
63 ret = perf_pmu_parse(head, name);
64 fclose(file);
65 }
66
67 closedir(format_dir);
68 return ret;
69}
70
71/*
72 * Reading/parsing the default pmu format definition, which should be
73 * located at:
74 * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
75 */
Adrian Hunterb6b96fb2013-07-04 16:20:25 +030076static int pmu_format(const char *name, struct list_head *format)
Jiri Olsacd82a322012-03-15 20:09:17 +010077{
78 struct stat st;
79 char path[PATH_MAX];
80 const char *sysfs;
81
82 sysfs = sysfs_find_mountpoint();
83 if (!sysfs)
84 return -1;
85
86 snprintf(path, PATH_MAX,
Robert Richter50a96672012-08-16 21:10:24 +020087 "%s" EVENT_SOURCE_DEVICE_PATH "%s/format", sysfs, name);
Jiri Olsacd82a322012-03-15 20:09:17 +010088
89 if (stat(path, &st) < 0)
Robert Richter9bc8f9f2012-06-14 22:38:37 +020090 return 0; /* no error if format does not exist */
Jiri Olsacd82a322012-03-15 20:09:17 +010091
Jiri Olsacff7f952012-11-10 01:46:50 +010092 if (perf_pmu__format_parse(path, format))
Jiri Olsacd82a322012-03-15 20:09:17 +010093 return -1;
94
95 return 0;
96}
97
Zheng Yana6146d52012-06-15 14:31:41 +080098static int perf_pmu__new_alias(struct list_head *list, char *name, FILE *file)
99{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300100 struct perf_pmu_alias *alias;
Zheng Yana6146d52012-06-15 14:31:41 +0800101 char buf[256];
102 int ret;
103
104 ret = fread(buf, 1, sizeof(buf), file);
105 if (ret == 0)
106 return -EINVAL;
107 buf[ret] = 0;
108
109 alias = malloc(sizeof(*alias));
110 if (!alias)
111 return -ENOMEM;
112
113 INIT_LIST_HEAD(&alias->terms);
114 ret = parse_events_terms(&alias->terms, buf);
115 if (ret) {
116 free(alias);
117 return ret;
118 }
119
120 alias->name = strdup(name);
121 list_add_tail(&alias->list, list);
122 return 0;
123}
124
125/*
126 * Process all the sysfs attributes located under the directory
127 * specified in 'dir' parameter.
128 */
129static int pmu_aliases_parse(char *dir, struct list_head *head)
130{
131 struct dirent *evt_ent;
132 DIR *event_dir;
133 int ret = 0;
134
135 event_dir = opendir(dir);
136 if (!event_dir)
137 return -EINVAL;
138
139 while (!ret && (evt_ent = readdir(event_dir))) {
140 char path[PATH_MAX];
141 char *name = evt_ent->d_name;
142 FILE *file;
143
144 if (!strcmp(name, ".") || !strcmp(name, ".."))
145 continue;
146
147 snprintf(path, PATH_MAX, "%s/%s", dir, name);
148
149 ret = -EINVAL;
150 file = fopen(path, "r");
151 if (!file)
152 break;
153 ret = perf_pmu__new_alias(head, name, file);
154 fclose(file);
155 }
156
157 closedir(event_dir);
158 return ret;
159}
160
161/*
162 * Reading the pmu event aliases definition, which should be located at:
163 * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
164 */
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300165static int pmu_aliases(const char *name, struct list_head *head)
Zheng Yana6146d52012-06-15 14:31:41 +0800166{
167 struct stat st;
168 char path[PATH_MAX];
169 const char *sysfs;
170
171 sysfs = sysfs_find_mountpoint();
172 if (!sysfs)
173 return -1;
174
175 snprintf(path, PATH_MAX,
176 "%s/bus/event_source/devices/%s/events", sysfs, name);
177
178 if (stat(path, &st) < 0)
Jiri Olsa3fded962012-10-10 14:53:16 +0200179 return 0; /* no error if 'events' does not exist */
Zheng Yana6146d52012-06-15 14:31:41 +0800180
181 if (pmu_aliases_parse(path, head))
182 return -1;
183
184 return 0;
185}
186
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300187static int pmu_alias_terms(struct perf_pmu_alias *alias,
Zheng Yana6146d52012-06-15 14:31:41 +0800188 struct list_head *terms)
189{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -0300190 struct parse_events_term *term, *clone;
Zheng Yana6146d52012-06-15 14:31:41 +0800191 LIST_HEAD(list);
192 int ret;
193
194 list_for_each_entry(term, &alias->terms, list) {
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -0300195 ret = parse_events_term__clone(&clone, term);
Zheng Yana6146d52012-06-15 14:31:41 +0800196 if (ret) {
197 parse_events__free_terms(&list);
198 return ret;
199 }
200 list_add_tail(&clone->list, &list);
201 }
202 list_splice(&list, terms);
203 return 0;
204}
205
Jiri Olsacd82a322012-03-15 20:09:17 +0100206/*
207 * Reading/parsing the default pmu type value, which should be
208 * located at:
209 * /sys/bus/event_source/devices/<dev>/type as sysfs attribute.
210 */
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300211static int pmu_type(const char *name, __u32 *type)
Jiri Olsacd82a322012-03-15 20:09:17 +0100212{
213 struct stat st;
214 char path[PATH_MAX];
215 const char *sysfs;
216 FILE *file;
217 int ret = 0;
218
219 sysfs = sysfs_find_mountpoint();
220 if (!sysfs)
221 return -1;
222
223 snprintf(path, PATH_MAX,
Robert Richter50a96672012-08-16 21:10:24 +0200224 "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name);
Jiri Olsacd82a322012-03-15 20:09:17 +0100225
226 if (stat(path, &st) < 0)
227 return -1;
228
229 file = fopen(path, "r");
230 if (!file)
231 return -EINVAL;
232
233 if (1 != fscanf(file, "%u", type))
234 ret = -1;
235
236 fclose(file);
237 return ret;
238}
239
Robert Richter50a96672012-08-16 21:10:24 +0200240/* Add all pmus in sysfs to pmu list: */
241static void pmu_read_sysfs(void)
242{
243 char path[PATH_MAX];
244 const char *sysfs;
245 DIR *dir;
246 struct dirent *dent;
247
248 sysfs = sysfs_find_mountpoint();
249 if (!sysfs)
250 return;
251
252 snprintf(path, PATH_MAX,
253 "%s" EVENT_SOURCE_DEVICE_PATH, sysfs);
254
255 dir = opendir(path);
256 if (!dir)
257 return;
258
259 while ((dent = readdir(dir))) {
260 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
261 continue;
262 /* add to static LIST_HEAD(pmus): */
263 perf_pmu__find(dent->d_name);
264 }
265
266 closedir(dir);
267}
268
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300269static struct cpu_map *pmu_cpumask(const char *name)
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800270{
271 struct stat st;
272 char path[PATH_MAX];
273 const char *sysfs;
274 FILE *file;
275 struct cpu_map *cpus;
276
277 sysfs = sysfs_find_mountpoint();
278 if (!sysfs)
279 return NULL;
280
281 snprintf(path, PATH_MAX,
282 "%s/bus/event_source/devices/%s/cpumask", sysfs, name);
283
284 if (stat(path, &st) < 0)
285 return NULL;
286
287 file = fopen(path, "r");
288 if (!file)
289 return NULL;
290
291 cpus = cpu_map__read(file);
292 fclose(file);
293 return cpus;
294}
295
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300296static struct perf_pmu *pmu_lookup(const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +0100297{
298 struct perf_pmu *pmu;
299 LIST_HEAD(format);
Zheng Yana6146d52012-06-15 14:31:41 +0800300 LIST_HEAD(aliases);
Jiri Olsacd82a322012-03-15 20:09:17 +0100301 __u32 type;
302
303 /*
304 * The pmu data we store & need consists of the pmu
305 * type value and format definitions. Load both right
306 * now.
307 */
308 if (pmu_format(name, &format))
309 return NULL;
310
Jiri Olsa3fded962012-10-10 14:53:16 +0200311 if (pmu_aliases(name, &aliases))
312 return NULL;
313
Jiri Olsacd82a322012-03-15 20:09:17 +0100314 if (pmu_type(name, &type))
315 return NULL;
316
317 pmu = zalloc(sizeof(*pmu));
318 if (!pmu)
319 return NULL;
320
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800321 pmu->cpus = pmu_cpumask(name);
322
Jiri Olsacd82a322012-03-15 20:09:17 +0100323 INIT_LIST_HEAD(&pmu->format);
Zheng Yana6146d52012-06-15 14:31:41 +0800324 INIT_LIST_HEAD(&pmu->aliases);
Jiri Olsacd82a322012-03-15 20:09:17 +0100325 list_splice(&format, &pmu->format);
Zheng Yana6146d52012-06-15 14:31:41 +0800326 list_splice(&aliases, &pmu->aliases);
Jiri Olsacd82a322012-03-15 20:09:17 +0100327 pmu->name = strdup(name);
328 pmu->type = type;
Robert Richter9bc8f9f2012-06-14 22:38:37 +0200329 list_add_tail(&pmu->list, &pmus);
Jiri Olsacd82a322012-03-15 20:09:17 +0100330 return pmu;
331}
332
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300333static struct perf_pmu *pmu_find(const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +0100334{
335 struct perf_pmu *pmu;
336
337 list_for_each_entry(pmu, &pmus, list)
338 if (!strcmp(pmu->name, name))
339 return pmu;
340
341 return NULL;
342}
343
Robert Richter50a96672012-08-16 21:10:24 +0200344struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
345{
346 /*
347 * pmu iterator: If pmu is NULL, we start at the begin,
348 * otherwise return the next pmu. Returns NULL on end.
349 */
350 if (!pmu) {
351 pmu_read_sysfs();
352 pmu = list_prepare_entry(pmu, &pmus, list);
353 }
354 list_for_each_entry_continue(pmu, &pmus, list)
355 return pmu;
356 return NULL;
357}
358
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300359struct perf_pmu *perf_pmu__find(const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +0100360{
361 struct perf_pmu *pmu;
362
363 /*
364 * Once PMU is loaded it stays in the list,
365 * so we keep us from multiple reading/parsing
366 * the pmu format definitions.
367 */
368 pmu = pmu_find(name);
369 if (pmu)
370 return pmu;
371
372 return pmu_lookup(name);
373}
374
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300375static struct perf_pmu_format *
Jiri Olsacd82a322012-03-15 20:09:17 +0100376pmu_find_format(struct list_head *formats, char *name)
377{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300378 struct perf_pmu_format *format;
Jiri Olsacd82a322012-03-15 20:09:17 +0100379
380 list_for_each_entry(format, formats, list)
381 if (!strcmp(format->name, name))
382 return format;
383
384 return NULL;
385}
386
387/*
388 * Returns value based on the format definition (format parameter)
389 * and unformated value (value parameter).
390 *
391 * TODO maybe optimize a little ;)
392 */
393static __u64 pmu_format_value(unsigned long *format, __u64 value)
394{
395 unsigned long fbit, vbit;
396 __u64 v = 0;
397
398 for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
399
400 if (!test_bit(fbit, format))
401 continue;
402
403 if (!(value & (1llu << vbit++)))
404 continue;
405
406 v |= (1llu << fbit);
407 }
408
409 return v;
410}
411
412/*
413 * Setup one of config[12] attr members based on the
414 * user input data - temr parameter.
415 */
416static int pmu_config_term(struct list_head *formats,
417 struct perf_event_attr *attr,
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -0300418 struct parse_events_term *term)
Jiri Olsacd82a322012-03-15 20:09:17 +0100419{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300420 struct perf_pmu_format *format;
Jiri Olsacd82a322012-03-15 20:09:17 +0100421 __u64 *vp;
422
423 /*
424 * Support only for hardcoded and numnerial terms.
425 * Hardcoded terms should be already in, so nothing
426 * to be done for them.
427 */
428 if (parse_events__is_hardcoded_term(term))
429 return 0;
430
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200431 if (term->type_val != PARSE_EVENTS__TERM_TYPE_NUM)
Jiri Olsacd82a322012-03-15 20:09:17 +0100432 return -EINVAL;
433
434 format = pmu_find_format(formats, term->config);
435 if (!format)
436 return -EINVAL;
437
438 switch (format->value) {
439 case PERF_PMU_FORMAT_VALUE_CONFIG:
440 vp = &attr->config;
441 break;
442 case PERF_PMU_FORMAT_VALUE_CONFIG1:
443 vp = &attr->config1;
444 break;
445 case PERF_PMU_FORMAT_VALUE_CONFIG2:
446 vp = &attr->config2;
447 break;
448 default:
449 return -EINVAL;
450 }
451
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200452 /*
453 * XXX If we ever decide to go with string values for
454 * non-hardcoded terms, here's the place to translate
455 * them into value.
456 */
Jiri Olsacd82a322012-03-15 20:09:17 +0100457 *vp |= pmu_format_value(format->bits, term->val.num);
458 return 0;
459}
460
Jiri Olsacff7f952012-11-10 01:46:50 +0100461int perf_pmu__config_terms(struct list_head *formats,
462 struct perf_event_attr *attr,
463 struct list_head *head_terms)
Jiri Olsacd82a322012-03-15 20:09:17 +0100464{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -0300465 struct parse_events_term *term;
Jiri Olsacd82a322012-03-15 20:09:17 +0100466
Jiri Olsa6b5fc392012-05-21 09:12:53 +0200467 list_for_each_entry(term, head_terms, list)
Jiri Olsacd82a322012-03-15 20:09:17 +0100468 if (pmu_config_term(formats, attr, term))
469 return -EINVAL;
470
471 return 0;
472}
473
474/*
475 * Configures event's 'attr' parameter based on the:
476 * 1) users input - specified in terms parameter
477 * 2) pmu format definitions - specified by pmu parameter
478 */
479int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
480 struct list_head *head_terms)
481{
482 attr->type = pmu->type;
Jiri Olsacff7f952012-11-10 01:46:50 +0100483 return perf_pmu__config_terms(&pmu->format, attr, head_terms);
Jiri Olsacd82a322012-03-15 20:09:17 +0100484}
485
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300486static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
487 struct parse_events_term *term)
Zheng Yana6146d52012-06-15 14:31:41 +0800488{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300489 struct perf_pmu_alias *alias;
Zheng Yana6146d52012-06-15 14:31:41 +0800490 char *name;
491
492 if (parse_events__is_hardcoded_term(term))
493 return NULL;
494
495 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
496 if (term->val.num != 1)
497 return NULL;
498 if (pmu_find_format(&pmu->format, term->config))
499 return NULL;
500 name = term->config;
501 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
502 if (strcasecmp(term->config, "event"))
503 return NULL;
504 name = term->val.str;
505 } else {
506 return NULL;
507 }
508
509 list_for_each_entry(alias, &pmu->aliases, list) {
510 if (!strcasecmp(alias->name, name))
511 return alias;
512 }
513 return NULL;
514}
515
516/*
517 * Find alias in the terms list and replace it with the terms
518 * defined for the alias
519 */
520int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms)
521{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -0300522 struct parse_events_term *term, *h;
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300523 struct perf_pmu_alias *alias;
Zheng Yana6146d52012-06-15 14:31:41 +0800524 int ret;
525
526 list_for_each_entry_safe(term, h, head_terms, list) {
527 alias = pmu_find_alias(pmu, term);
528 if (!alias)
529 continue;
530 ret = pmu_alias_terms(alias, &term->list);
531 if (ret)
532 return ret;
533 list_del(&term->list);
534 free(term);
535 }
536 return 0;
537}
538
Jiri Olsacd82a322012-03-15 20:09:17 +0100539int perf_pmu__new_format(struct list_head *list, char *name,
540 int config, unsigned long *bits)
541{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300542 struct perf_pmu_format *format;
Jiri Olsacd82a322012-03-15 20:09:17 +0100543
544 format = zalloc(sizeof(*format));
545 if (!format)
546 return -ENOMEM;
547
548 format->name = strdup(name);
549 format->value = config;
550 memcpy(format->bits, bits, sizeof(format->bits));
551
552 list_add_tail(&format->list, list);
553 return 0;
554}
555
556void perf_pmu__set_format(unsigned long *bits, long from, long to)
557{
558 long b;
559
560 if (!to)
561 to = from;
562
Sukadev Bhattiprolu15268132013-01-17 09:11:30 -0800563 memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
Jiri Olsacd82a322012-03-15 20:09:17 +0100564 for (b = from; b <= to; b++)
565 set_bit(b, bits);
566}
Andi Kleendc098b32013-04-20 11:02:29 -0700567
568static char *format_alias(char *buf, int len, struct perf_pmu *pmu,
569 struct perf_pmu_alias *alias)
570{
571 snprintf(buf, len, "%s/%s/", pmu->name, alias->name);
572 return buf;
573}
574
575static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu,
576 struct perf_pmu_alias *alias)
577{
578 snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name);
579 return buf;
580}
581
582static int cmp_string(const void *a, const void *b)
583{
584 const char * const *as = a;
585 const char * const *bs = b;
586 return strcmp(*as, *bs);
587}
588
589void print_pmu_events(const char *event_glob, bool name_only)
590{
591 struct perf_pmu *pmu;
592 struct perf_pmu_alias *alias;
593 char buf[1024];
594 int printed = 0;
595 int len, j;
596 char **aliases;
597
598 pmu = NULL;
599 len = 0;
600 while ((pmu = perf_pmu__scan(pmu)) != NULL)
601 list_for_each_entry(alias, &pmu->aliases, list)
602 len++;
603 aliases = malloc(sizeof(char *) * len);
604 if (!aliases)
605 return;
606 pmu = NULL;
607 j = 0;
608 while ((pmu = perf_pmu__scan(pmu)) != NULL)
609 list_for_each_entry(alias, &pmu->aliases, list) {
610 char *name = format_alias(buf, sizeof(buf), pmu, alias);
611 bool is_cpu = !strcmp(pmu->name, "cpu");
612
613 if (event_glob != NULL &&
614 !(strglobmatch(name, event_glob) ||
615 (!is_cpu && strglobmatch(alias->name,
616 event_glob))))
617 continue;
618 aliases[j] = name;
619 if (is_cpu && !name_only)
620 aliases[j] = format_alias_or(buf, sizeof(buf),
621 pmu, alias);
622 aliases[j] = strdup(aliases[j]);
623 j++;
624 }
625 len = j;
626 qsort(aliases, len, sizeof(char *), cmp_string);
627 for (j = 0; j < len; j++) {
628 if (name_only) {
629 printf("%s ", aliases[j]);
630 continue;
631 }
632 printf(" %-50s [Kernel PMU event]\n", aliases[j]);
633 free(aliases[j]);
634 printed++;
635 }
636 if (printed)
637 printf("\n");
638 free(aliases);
639}