blob: 18e84801d4d19c6750bd68fedfd66c6469ff7fe3 [file] [log] [blame]
Jiri Olsacd82a322012-03-15 20:09:17 +01001
2#include <linux/list.h>
3#include <sys/types.h>
4#include <sys/stat.h>
5#include <unistd.h>
6#include <stdio.h>
7#include <dirent.h>
8#include "sysfs.h"
9#include "util.h"
10#include "pmu.h"
11#include "parse-events.h"
Yan, Zheng7ae92e72012-09-10 15:53:50 +080012#include "cpumap.h"
Jiri Olsacd82a322012-03-15 20:09:17 +010013
Robert Richter50a96672012-08-16 21:10:24 +020014#define EVENT_SOURCE_DEVICE_PATH "/bus/event_source/devices/"
15
Jiri Olsacd82a322012-03-15 20:09:17 +010016int perf_pmu_parse(struct list_head *list, char *name);
17extern FILE *perf_pmu_in;
18
19static LIST_HEAD(pmus);
20
21/*
22 * Parse & process all the sysfs attributes located under
23 * the directory specified in 'dir' parameter.
24 */
25static int pmu_format_parse(char *dir, struct list_head *head)
26{
27 struct dirent *evt_ent;
28 DIR *format_dir;
29 int ret = 0;
30
31 format_dir = opendir(dir);
32 if (!format_dir)
33 return -EINVAL;
34
35 while (!ret && (evt_ent = readdir(format_dir))) {
36 char path[PATH_MAX];
37 char *name = evt_ent->d_name;
38 FILE *file;
39
40 if (!strcmp(name, ".") || !strcmp(name, ".."))
41 continue;
42
43 snprintf(path, PATH_MAX, "%s/%s", dir, name);
44
45 ret = -EINVAL;
46 file = fopen(path, "r");
47 if (!file)
48 break;
49
50 perf_pmu_in = file;
51 ret = perf_pmu_parse(head, name);
52 fclose(file);
53 }
54
55 closedir(format_dir);
56 return ret;
57}
58
59/*
60 * Reading/parsing the default pmu format definition, which should be
61 * located at:
62 * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
63 */
64static int pmu_format(char *name, struct list_head *format)
65{
66 struct stat st;
67 char path[PATH_MAX];
68 const char *sysfs;
69
70 sysfs = sysfs_find_mountpoint();
71 if (!sysfs)
72 return -1;
73
74 snprintf(path, PATH_MAX,
Robert Richter50a96672012-08-16 21:10:24 +020075 "%s" EVENT_SOURCE_DEVICE_PATH "%s/format", sysfs, name);
Jiri Olsacd82a322012-03-15 20:09:17 +010076
77 if (stat(path, &st) < 0)
Robert Richter9bc8f9f2012-06-14 22:38:37 +020078 return 0; /* no error if format does not exist */
Jiri Olsacd82a322012-03-15 20:09:17 +010079
80 if (pmu_format_parse(path, format))
81 return -1;
82
83 return 0;
84}
85
Zheng Yana6146d52012-06-15 14:31:41 +080086static int perf_pmu__new_alias(struct list_head *list, char *name, FILE *file)
87{
88 struct perf_pmu__alias *alias;
89 char buf[256];
90 int ret;
91
92 ret = fread(buf, 1, sizeof(buf), file);
93 if (ret == 0)
94 return -EINVAL;
95 buf[ret] = 0;
96
97 alias = malloc(sizeof(*alias));
98 if (!alias)
99 return -ENOMEM;
100
101 INIT_LIST_HEAD(&alias->terms);
102 ret = parse_events_terms(&alias->terms, buf);
103 if (ret) {
104 free(alias);
105 return ret;
106 }
107
108 alias->name = strdup(name);
109 list_add_tail(&alias->list, list);
110 return 0;
111}
112
113/*
114 * Process all the sysfs attributes located under the directory
115 * specified in 'dir' parameter.
116 */
117static int pmu_aliases_parse(char *dir, struct list_head *head)
118{
119 struct dirent *evt_ent;
120 DIR *event_dir;
121 int ret = 0;
122
123 event_dir = opendir(dir);
124 if (!event_dir)
125 return -EINVAL;
126
127 while (!ret && (evt_ent = readdir(event_dir))) {
128 char path[PATH_MAX];
129 char *name = evt_ent->d_name;
130 FILE *file;
131
132 if (!strcmp(name, ".") || !strcmp(name, ".."))
133 continue;
134
135 snprintf(path, PATH_MAX, "%s/%s", dir, name);
136
137 ret = -EINVAL;
138 file = fopen(path, "r");
139 if (!file)
140 break;
141 ret = perf_pmu__new_alias(head, name, file);
142 fclose(file);
143 }
144
145 closedir(event_dir);
146 return ret;
147}
148
149/*
150 * Reading the pmu event aliases definition, which should be located at:
151 * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
152 */
153static int pmu_aliases(char *name, struct list_head *head)
154{
155 struct stat st;
156 char path[PATH_MAX];
157 const char *sysfs;
158
159 sysfs = sysfs_find_mountpoint();
160 if (!sysfs)
161 return -1;
162
163 snprintf(path, PATH_MAX,
164 "%s/bus/event_source/devices/%s/events", sysfs, name);
165
166 if (stat(path, &st) < 0)
Jiri Olsa3fded962012-10-10 14:53:16 +0200167 return 0; /* no error if 'events' does not exist */
Zheng Yana6146d52012-06-15 14:31:41 +0800168
169 if (pmu_aliases_parse(path, head))
170 return -1;
171
172 return 0;
173}
174
175static int pmu_alias_terms(struct perf_pmu__alias *alias,
176 struct list_head *terms)
177{
178 struct parse_events__term *term, *clone;
179 LIST_HEAD(list);
180 int ret;
181
182 list_for_each_entry(term, &alias->terms, list) {
183 ret = parse_events__term_clone(&clone, term);
184 if (ret) {
185 parse_events__free_terms(&list);
186 return ret;
187 }
188 list_add_tail(&clone->list, &list);
189 }
190 list_splice(&list, terms);
191 return 0;
192}
193
Jiri Olsacd82a322012-03-15 20:09:17 +0100194/*
195 * Reading/parsing the default pmu type value, which should be
196 * located at:
197 * /sys/bus/event_source/devices/<dev>/type as sysfs attribute.
198 */
199static int pmu_type(char *name, __u32 *type)
200{
201 struct stat st;
202 char path[PATH_MAX];
203 const char *sysfs;
204 FILE *file;
205 int ret = 0;
206
207 sysfs = sysfs_find_mountpoint();
208 if (!sysfs)
209 return -1;
210
211 snprintf(path, PATH_MAX,
Robert Richter50a96672012-08-16 21:10:24 +0200212 "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name);
Jiri Olsacd82a322012-03-15 20:09:17 +0100213
214 if (stat(path, &st) < 0)
215 return -1;
216
217 file = fopen(path, "r");
218 if (!file)
219 return -EINVAL;
220
221 if (1 != fscanf(file, "%u", type))
222 ret = -1;
223
224 fclose(file);
225 return ret;
226}
227
Robert Richter50a96672012-08-16 21:10:24 +0200228/* Add all pmus in sysfs to pmu list: */
229static void pmu_read_sysfs(void)
230{
231 char path[PATH_MAX];
232 const char *sysfs;
233 DIR *dir;
234 struct dirent *dent;
235
236 sysfs = sysfs_find_mountpoint();
237 if (!sysfs)
238 return;
239
240 snprintf(path, PATH_MAX,
241 "%s" EVENT_SOURCE_DEVICE_PATH, sysfs);
242
243 dir = opendir(path);
244 if (!dir)
245 return;
246
247 while ((dent = readdir(dir))) {
248 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
249 continue;
250 /* add to static LIST_HEAD(pmus): */
251 perf_pmu__find(dent->d_name);
252 }
253
254 closedir(dir);
255}
256
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800257static struct cpu_map *pmu_cpumask(char *name)
258{
259 struct stat st;
260 char path[PATH_MAX];
261 const char *sysfs;
262 FILE *file;
263 struct cpu_map *cpus;
264
265 sysfs = sysfs_find_mountpoint();
266 if (!sysfs)
267 return NULL;
268
269 snprintf(path, PATH_MAX,
270 "%s/bus/event_source/devices/%s/cpumask", sysfs, name);
271
272 if (stat(path, &st) < 0)
273 return NULL;
274
275 file = fopen(path, "r");
276 if (!file)
277 return NULL;
278
279 cpus = cpu_map__read(file);
280 fclose(file);
281 return cpus;
282}
283
Jiri Olsacd82a322012-03-15 20:09:17 +0100284static struct perf_pmu *pmu_lookup(char *name)
285{
286 struct perf_pmu *pmu;
287 LIST_HEAD(format);
Zheng Yana6146d52012-06-15 14:31:41 +0800288 LIST_HEAD(aliases);
Jiri Olsacd82a322012-03-15 20:09:17 +0100289 __u32 type;
290
291 /*
292 * The pmu data we store & need consists of the pmu
293 * type value and format definitions. Load both right
294 * now.
295 */
296 if (pmu_format(name, &format))
297 return NULL;
298
Jiri Olsa3fded962012-10-10 14:53:16 +0200299 if (pmu_aliases(name, &aliases))
300 return NULL;
301
Jiri Olsacd82a322012-03-15 20:09:17 +0100302 if (pmu_type(name, &type))
303 return NULL;
304
305 pmu = zalloc(sizeof(*pmu));
306 if (!pmu)
307 return NULL;
308
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800309 pmu->cpus = pmu_cpumask(name);
310
Jiri Olsacd82a322012-03-15 20:09:17 +0100311 INIT_LIST_HEAD(&pmu->format);
Zheng Yana6146d52012-06-15 14:31:41 +0800312 INIT_LIST_HEAD(&pmu->aliases);
Jiri Olsacd82a322012-03-15 20:09:17 +0100313 list_splice(&format, &pmu->format);
Zheng Yana6146d52012-06-15 14:31:41 +0800314 list_splice(&aliases, &pmu->aliases);
Jiri Olsacd82a322012-03-15 20:09:17 +0100315 pmu->name = strdup(name);
316 pmu->type = type;
Robert Richter9bc8f9f2012-06-14 22:38:37 +0200317 list_add_tail(&pmu->list, &pmus);
Jiri Olsacd82a322012-03-15 20:09:17 +0100318 return pmu;
319}
320
321static struct perf_pmu *pmu_find(char *name)
322{
323 struct perf_pmu *pmu;
324
325 list_for_each_entry(pmu, &pmus, list)
326 if (!strcmp(pmu->name, name))
327 return pmu;
328
329 return NULL;
330}
331
Robert Richter50a96672012-08-16 21:10:24 +0200332struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
333{
334 /*
335 * pmu iterator: If pmu is NULL, we start at the begin,
336 * otherwise return the next pmu. Returns NULL on end.
337 */
338 if (!pmu) {
339 pmu_read_sysfs();
340 pmu = list_prepare_entry(pmu, &pmus, list);
341 }
342 list_for_each_entry_continue(pmu, &pmus, list)
343 return pmu;
344 return NULL;
345}
346
Jiri Olsacd82a322012-03-15 20:09:17 +0100347struct perf_pmu *perf_pmu__find(char *name)
348{
349 struct perf_pmu *pmu;
350
351 /*
352 * Once PMU is loaded it stays in the list,
353 * so we keep us from multiple reading/parsing
354 * the pmu format definitions.
355 */
356 pmu = pmu_find(name);
357 if (pmu)
358 return pmu;
359
360 return pmu_lookup(name);
361}
362
363static struct perf_pmu__format*
364pmu_find_format(struct list_head *formats, char *name)
365{
366 struct perf_pmu__format *format;
367
368 list_for_each_entry(format, formats, list)
369 if (!strcmp(format->name, name))
370 return format;
371
372 return NULL;
373}
374
375/*
376 * Returns value based on the format definition (format parameter)
377 * and unformated value (value parameter).
378 *
379 * TODO maybe optimize a little ;)
380 */
381static __u64 pmu_format_value(unsigned long *format, __u64 value)
382{
383 unsigned long fbit, vbit;
384 __u64 v = 0;
385
386 for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
387
388 if (!test_bit(fbit, format))
389 continue;
390
391 if (!(value & (1llu << vbit++)))
392 continue;
393
394 v |= (1llu << fbit);
395 }
396
397 return v;
398}
399
400/*
401 * Setup one of config[12] attr members based on the
402 * user input data - temr parameter.
403 */
404static int pmu_config_term(struct list_head *formats,
405 struct perf_event_attr *attr,
406 struct parse_events__term *term)
407{
408 struct perf_pmu__format *format;
409 __u64 *vp;
410
411 /*
412 * Support only for hardcoded and numnerial terms.
413 * Hardcoded terms should be already in, so nothing
414 * to be done for them.
415 */
416 if (parse_events__is_hardcoded_term(term))
417 return 0;
418
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200419 if (term->type_val != PARSE_EVENTS__TERM_TYPE_NUM)
Jiri Olsacd82a322012-03-15 20:09:17 +0100420 return -EINVAL;
421
422 format = pmu_find_format(formats, term->config);
423 if (!format)
424 return -EINVAL;
425
426 switch (format->value) {
427 case PERF_PMU_FORMAT_VALUE_CONFIG:
428 vp = &attr->config;
429 break;
430 case PERF_PMU_FORMAT_VALUE_CONFIG1:
431 vp = &attr->config1;
432 break;
433 case PERF_PMU_FORMAT_VALUE_CONFIG2:
434 vp = &attr->config2;
435 break;
436 default:
437 return -EINVAL;
438 }
439
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200440 /*
441 * XXX If we ever decide to go with string values for
442 * non-hardcoded terms, here's the place to translate
443 * them into value.
444 */
Jiri Olsacd82a322012-03-15 20:09:17 +0100445 *vp |= pmu_format_value(format->bits, term->val.num);
446 return 0;
447}
448
449static int pmu_config(struct list_head *formats, struct perf_event_attr *attr,
450 struct list_head *head_terms)
451{
Jiri Olsa6b5fc392012-05-21 09:12:53 +0200452 struct parse_events__term *term;
Jiri Olsacd82a322012-03-15 20:09:17 +0100453
Jiri Olsa6b5fc392012-05-21 09:12:53 +0200454 list_for_each_entry(term, head_terms, list)
Jiri Olsacd82a322012-03-15 20:09:17 +0100455 if (pmu_config_term(formats, attr, term))
456 return -EINVAL;
457
458 return 0;
459}
460
461/*
462 * Configures event's 'attr' parameter based on the:
463 * 1) users input - specified in terms parameter
464 * 2) pmu format definitions - specified by pmu parameter
465 */
466int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
467 struct list_head *head_terms)
468{
469 attr->type = pmu->type;
470 return pmu_config(&pmu->format, attr, head_terms);
471}
472
Zheng Yana6146d52012-06-15 14:31:41 +0800473static struct perf_pmu__alias *pmu_find_alias(struct perf_pmu *pmu,
474 struct parse_events__term *term)
475{
476 struct perf_pmu__alias *alias;
477 char *name;
478
479 if (parse_events__is_hardcoded_term(term))
480 return NULL;
481
482 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
483 if (term->val.num != 1)
484 return NULL;
485 if (pmu_find_format(&pmu->format, term->config))
486 return NULL;
487 name = term->config;
488 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
489 if (strcasecmp(term->config, "event"))
490 return NULL;
491 name = term->val.str;
492 } else {
493 return NULL;
494 }
495
496 list_for_each_entry(alias, &pmu->aliases, list) {
497 if (!strcasecmp(alias->name, name))
498 return alias;
499 }
500 return NULL;
501}
502
503/*
504 * Find alias in the terms list and replace it with the terms
505 * defined for the alias
506 */
507int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms)
508{
509 struct parse_events__term *term, *h;
510 struct perf_pmu__alias *alias;
511 int ret;
512
513 list_for_each_entry_safe(term, h, head_terms, list) {
514 alias = pmu_find_alias(pmu, term);
515 if (!alias)
516 continue;
517 ret = pmu_alias_terms(alias, &term->list);
518 if (ret)
519 return ret;
520 list_del(&term->list);
521 free(term);
522 }
523 return 0;
524}
525
Jiri Olsacd82a322012-03-15 20:09:17 +0100526int perf_pmu__new_format(struct list_head *list, char *name,
527 int config, unsigned long *bits)
528{
529 struct perf_pmu__format *format;
530
531 format = zalloc(sizeof(*format));
532 if (!format)
533 return -ENOMEM;
534
535 format->name = strdup(name);
536 format->value = config;
537 memcpy(format->bits, bits, sizeof(format->bits));
538
539 list_add_tail(&format->list, list);
540 return 0;
541}
542
543void perf_pmu__set_format(unsigned long *bits, long from, long to)
544{
545 long b;
546
547 if (!to)
548 to = from;
549
550 memset(bits, 0, BITS_TO_LONGS(PERF_PMU_FORMAT_BITS));
551 for (b = from; b <= to; b++)
552 set_bit(b, bits);
553}
554
555/* Simulated format definitions. */
556static struct test_format {
557 const char *name;
558 const char *value;
559} test_formats[] = {
560 { "krava01", "config:0-1,62-63\n", },
561 { "krava02", "config:10-17\n", },
562 { "krava03", "config:5\n", },
563 { "krava11", "config1:0,2,4,6,8,20-28\n", },
564 { "krava12", "config1:63\n", },
565 { "krava13", "config1:45-47\n", },
566 { "krava21", "config2:0-3,10-13,20-23,30-33,40-43,50-53,60-63\n", },
567 { "krava22", "config2:8,18,48,58\n", },
568 { "krava23", "config2:28-29,38\n", },
569};
570
571#define TEST_FORMATS_CNT (sizeof(test_formats) / sizeof(struct test_format))
572
573/* Simulated users input. */
574static struct parse_events__term test_terms[] = {
575 {
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200576 .config = (char *) "krava01",
577 .val.num = 15,
578 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
579 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
Jiri Olsacd82a322012-03-15 20:09:17 +0100580 },
581 {
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200582 .config = (char *) "krava02",
583 .val.num = 170,
584 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
585 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
Jiri Olsacd82a322012-03-15 20:09:17 +0100586 },
587 {
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200588 .config = (char *) "krava03",
589 .val.num = 1,
590 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
591 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
Jiri Olsacd82a322012-03-15 20:09:17 +0100592 },
593 {
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200594 .config = (char *) "krava11",
595 .val.num = 27,
596 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
597 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
Jiri Olsacd82a322012-03-15 20:09:17 +0100598 },
599 {
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200600 .config = (char *) "krava12",
601 .val.num = 1,
602 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
603 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
Jiri Olsacd82a322012-03-15 20:09:17 +0100604 },
605 {
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200606 .config = (char *) "krava13",
607 .val.num = 2,
608 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
609 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
Jiri Olsacd82a322012-03-15 20:09:17 +0100610 },
611 {
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200612 .config = (char *) "krava21",
613 .val.num = 119,
614 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
615 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
Jiri Olsacd82a322012-03-15 20:09:17 +0100616 },
617 {
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200618 .config = (char *) "krava22",
619 .val.num = 11,
620 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
621 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
Jiri Olsacd82a322012-03-15 20:09:17 +0100622 },
623 {
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200624 .config = (char *) "krava23",
625 .val.num = 2,
626 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
627 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
Jiri Olsacd82a322012-03-15 20:09:17 +0100628 },
629};
630#define TERMS_CNT (sizeof(test_terms) / sizeof(struct parse_events__term))
631
632/*
633 * Prepare format directory data, exported by kernel
634 * at /sys/bus/event_source/devices/<dev>/format.
635 */
636static char *test_format_dir_get(void)
637{
638 static char dir[PATH_MAX];
639 unsigned int i;
640
641 snprintf(dir, PATH_MAX, "/tmp/perf-pmu-test-format-XXXXXX");
642 if (!mkdtemp(dir))
643 return NULL;
644
645 for (i = 0; i < TEST_FORMATS_CNT; i++) {
646 static char name[PATH_MAX];
647 struct test_format *format = &test_formats[i];
648 FILE *file;
649
650 snprintf(name, PATH_MAX, "%s/%s", dir, format->name);
651
652 file = fopen(name, "w");
653 if (!file)
654 return NULL;
655
656 if (1 != fwrite(format->value, strlen(format->value), 1, file))
657 break;
658
659 fclose(file);
660 }
661
662 return dir;
663}
664
665/* Cleanup format directory. */
666static int test_format_dir_put(char *dir)
667{
668 char buf[PATH_MAX];
669 snprintf(buf, PATH_MAX, "rm -f %s/*\n", dir);
670 if (system(buf))
671 return -1;
672
673 snprintf(buf, PATH_MAX, "rmdir %s\n", dir);
674 return system(buf);
675}
676
677static struct list_head *test_terms_list(void)
678{
679 static LIST_HEAD(terms);
680 unsigned int i;
681
682 for (i = 0; i < TERMS_CNT; i++)
683 list_add_tail(&test_terms[i].list, &terms);
684
685 return &terms;
686}
687
688#undef TERMS_CNT
689
690int perf_pmu__test(void)
691{
692 char *format = test_format_dir_get();
693 LIST_HEAD(formats);
694 struct list_head *terms = test_terms_list();
695 int ret;
696
697 if (!format)
698 return -EINVAL;
699
700 do {
701 struct perf_event_attr attr;
702
703 memset(&attr, 0, sizeof(attr));
704
705 ret = pmu_format_parse(format, &formats);
706 if (ret)
707 break;
708
709 ret = pmu_config(&formats, &attr, terms);
710 if (ret)
711 break;
712
713 ret = -EINVAL;
714
715 if (attr.config != 0xc00000000002a823)
716 break;
717 if (attr.config1 != 0x8000400000000145)
718 break;
719 if (attr.config2 != 0x0400000020041d07)
720 break;
721
722 ret = 0;
723 } while (0);
724
725 test_format_dir_put(format);
726 return ret;
727}