blob: 74d0948ec3680908baeae018d74bd2eae0468a86 [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"
12
13int perf_pmu_parse(struct list_head *list, char *name);
14extern FILE *perf_pmu_in;
15
16static LIST_HEAD(pmus);
17
18/*
19 * Parse & process all the sysfs attributes located under
20 * the directory specified in 'dir' parameter.
21 */
22static int pmu_format_parse(char *dir, struct list_head *head)
23{
24 struct dirent *evt_ent;
25 DIR *format_dir;
26 int ret = 0;
27
28 format_dir = opendir(dir);
29 if (!format_dir)
30 return -EINVAL;
31
32 while (!ret && (evt_ent = readdir(format_dir))) {
33 char path[PATH_MAX];
34 char *name = evt_ent->d_name;
35 FILE *file;
36
37 if (!strcmp(name, ".") || !strcmp(name, ".."))
38 continue;
39
40 snprintf(path, PATH_MAX, "%s/%s", dir, name);
41
42 ret = -EINVAL;
43 file = fopen(path, "r");
44 if (!file)
45 break;
46
47 perf_pmu_in = file;
48 ret = perf_pmu_parse(head, name);
49 fclose(file);
50 }
51
52 closedir(format_dir);
53 return ret;
54}
55
56/*
57 * Reading/parsing the default pmu format definition, which should be
58 * located at:
59 * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
60 */
61static int pmu_format(char *name, struct list_head *format)
62{
63 struct stat st;
64 char path[PATH_MAX];
65 const char *sysfs;
66
67 sysfs = sysfs_find_mountpoint();
68 if (!sysfs)
69 return -1;
70
71 snprintf(path, PATH_MAX,
72 "%s/bus/event_source/devices/%s/format", sysfs, name);
73
74 if (stat(path, &st) < 0)
75 return -1;
76
77 if (pmu_format_parse(path, format))
78 return -1;
79
80 return 0;
81}
82
Zheng Yana6146d52012-06-15 14:31:41 +080083static int perf_pmu__new_alias(struct list_head *list, char *name, FILE *file)
84{
85 struct perf_pmu__alias *alias;
86 char buf[256];
87 int ret;
88
89 ret = fread(buf, 1, sizeof(buf), file);
90 if (ret == 0)
91 return -EINVAL;
92 buf[ret] = 0;
93
94 alias = malloc(sizeof(*alias));
95 if (!alias)
96 return -ENOMEM;
97
98 INIT_LIST_HEAD(&alias->terms);
99 ret = parse_events_terms(&alias->terms, buf);
100 if (ret) {
101 free(alias);
102 return ret;
103 }
104
105 alias->name = strdup(name);
106 list_add_tail(&alias->list, list);
107 return 0;
108}
109
110/*
111 * Process all the sysfs attributes located under the directory
112 * specified in 'dir' parameter.
113 */
114static int pmu_aliases_parse(char *dir, struct list_head *head)
115{
116 struct dirent *evt_ent;
117 DIR *event_dir;
118 int ret = 0;
119
120 event_dir = opendir(dir);
121 if (!event_dir)
122 return -EINVAL;
123
124 while (!ret && (evt_ent = readdir(event_dir))) {
125 char path[PATH_MAX];
126 char *name = evt_ent->d_name;
127 FILE *file;
128
129 if (!strcmp(name, ".") || !strcmp(name, ".."))
130 continue;
131
132 snprintf(path, PATH_MAX, "%s/%s", dir, name);
133
134 ret = -EINVAL;
135 file = fopen(path, "r");
136 if (!file)
137 break;
138 ret = perf_pmu__new_alias(head, name, file);
139 fclose(file);
140 }
141
142 closedir(event_dir);
143 return ret;
144}
145
146/*
147 * Reading the pmu event aliases definition, which should be located at:
148 * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
149 */
150static int pmu_aliases(char *name, struct list_head *head)
151{
152 struct stat st;
153 char path[PATH_MAX];
154 const char *sysfs;
155
156 sysfs = sysfs_find_mountpoint();
157 if (!sysfs)
158 return -1;
159
160 snprintf(path, PATH_MAX,
161 "%s/bus/event_source/devices/%s/events", sysfs, name);
162
163 if (stat(path, &st) < 0)
164 return -1;
165
166 if (pmu_aliases_parse(path, head))
167 return -1;
168
169 return 0;
170}
171
172static int pmu_alias_terms(struct perf_pmu__alias *alias,
173 struct list_head *terms)
174{
175 struct parse_events__term *term, *clone;
176 LIST_HEAD(list);
177 int ret;
178
179 list_for_each_entry(term, &alias->terms, list) {
180 ret = parse_events__term_clone(&clone, term);
181 if (ret) {
182 parse_events__free_terms(&list);
183 return ret;
184 }
185 list_add_tail(&clone->list, &list);
186 }
187 list_splice(&list, terms);
188 return 0;
189}
190
Jiri Olsacd82a322012-03-15 20:09:17 +0100191/*
192 * Reading/parsing the default pmu type value, which should be
193 * located at:
194 * /sys/bus/event_source/devices/<dev>/type as sysfs attribute.
195 */
196static int pmu_type(char *name, __u32 *type)
197{
198 struct stat st;
199 char path[PATH_MAX];
200 const char *sysfs;
201 FILE *file;
202 int ret = 0;
203
204 sysfs = sysfs_find_mountpoint();
205 if (!sysfs)
206 return -1;
207
208 snprintf(path, PATH_MAX,
209 "%s/bus/event_source/devices/%s/type", sysfs, name);
210
211 if (stat(path, &st) < 0)
212 return -1;
213
214 file = fopen(path, "r");
215 if (!file)
216 return -EINVAL;
217
218 if (1 != fscanf(file, "%u", type))
219 ret = -1;
220
221 fclose(file);
222 return ret;
223}
224
225static struct perf_pmu *pmu_lookup(char *name)
226{
227 struct perf_pmu *pmu;
228 LIST_HEAD(format);
Zheng Yana6146d52012-06-15 14:31:41 +0800229 LIST_HEAD(aliases);
Jiri Olsacd82a322012-03-15 20:09:17 +0100230 __u32 type;
231
232 /*
233 * The pmu data we store & need consists of the pmu
234 * type value and format definitions. Load both right
235 * now.
236 */
237 if (pmu_format(name, &format))
238 return NULL;
239
240 if (pmu_type(name, &type))
241 return NULL;
242
243 pmu = zalloc(sizeof(*pmu));
244 if (!pmu)
245 return NULL;
246
Zheng Yana6146d52012-06-15 14:31:41 +0800247 pmu_aliases(name, &aliases);
248
Jiri Olsacd82a322012-03-15 20:09:17 +0100249 INIT_LIST_HEAD(&pmu->format);
Zheng Yana6146d52012-06-15 14:31:41 +0800250 INIT_LIST_HEAD(&pmu->aliases);
Jiri Olsacd82a322012-03-15 20:09:17 +0100251 list_splice(&format, &pmu->format);
Zheng Yana6146d52012-06-15 14:31:41 +0800252 list_splice(&aliases, &pmu->aliases);
Jiri Olsacd82a322012-03-15 20:09:17 +0100253 pmu->name = strdup(name);
254 pmu->type = type;
255 return pmu;
256}
257
258static struct perf_pmu *pmu_find(char *name)
259{
260 struct perf_pmu *pmu;
261
262 list_for_each_entry(pmu, &pmus, list)
263 if (!strcmp(pmu->name, name))
264 return pmu;
265
266 return NULL;
267}
268
269struct perf_pmu *perf_pmu__find(char *name)
270{
271 struct perf_pmu *pmu;
272
273 /*
274 * Once PMU is loaded it stays in the list,
275 * so we keep us from multiple reading/parsing
276 * the pmu format definitions.
277 */
278 pmu = pmu_find(name);
279 if (pmu)
280 return pmu;
281
282 return pmu_lookup(name);
283}
284
285static struct perf_pmu__format*
286pmu_find_format(struct list_head *formats, char *name)
287{
288 struct perf_pmu__format *format;
289
290 list_for_each_entry(format, formats, list)
291 if (!strcmp(format->name, name))
292 return format;
293
294 return NULL;
295}
296
297/*
298 * Returns value based on the format definition (format parameter)
299 * and unformated value (value parameter).
300 *
301 * TODO maybe optimize a little ;)
302 */
303static __u64 pmu_format_value(unsigned long *format, __u64 value)
304{
305 unsigned long fbit, vbit;
306 __u64 v = 0;
307
308 for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
309
310 if (!test_bit(fbit, format))
311 continue;
312
313 if (!(value & (1llu << vbit++)))
314 continue;
315
316 v |= (1llu << fbit);
317 }
318
319 return v;
320}
321
322/*
323 * Setup one of config[12] attr members based on the
324 * user input data - temr parameter.
325 */
326static int pmu_config_term(struct list_head *formats,
327 struct perf_event_attr *attr,
328 struct parse_events__term *term)
329{
330 struct perf_pmu__format *format;
331 __u64 *vp;
332
333 /*
334 * Support only for hardcoded and numnerial terms.
335 * Hardcoded terms should be already in, so nothing
336 * to be done for them.
337 */
338 if (parse_events__is_hardcoded_term(term))
339 return 0;
340
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200341 if (term->type_val != PARSE_EVENTS__TERM_TYPE_NUM)
Jiri Olsacd82a322012-03-15 20:09:17 +0100342 return -EINVAL;
343
344 format = pmu_find_format(formats, term->config);
345 if (!format)
346 return -EINVAL;
347
348 switch (format->value) {
349 case PERF_PMU_FORMAT_VALUE_CONFIG:
350 vp = &attr->config;
351 break;
352 case PERF_PMU_FORMAT_VALUE_CONFIG1:
353 vp = &attr->config1;
354 break;
355 case PERF_PMU_FORMAT_VALUE_CONFIG2:
356 vp = &attr->config2;
357 break;
358 default:
359 return -EINVAL;
360 }
361
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200362 /*
363 * XXX If we ever decide to go with string values for
364 * non-hardcoded terms, here's the place to translate
365 * them into value.
366 */
Jiri Olsacd82a322012-03-15 20:09:17 +0100367 *vp |= pmu_format_value(format->bits, term->val.num);
368 return 0;
369}
370
371static int pmu_config(struct list_head *formats, struct perf_event_attr *attr,
372 struct list_head *head_terms)
373{
Jiri Olsa6b5fc392012-05-21 09:12:53 +0200374 struct parse_events__term *term;
Jiri Olsacd82a322012-03-15 20:09:17 +0100375
Jiri Olsa6b5fc392012-05-21 09:12:53 +0200376 list_for_each_entry(term, head_terms, list)
Jiri Olsacd82a322012-03-15 20:09:17 +0100377 if (pmu_config_term(formats, attr, term))
378 return -EINVAL;
379
380 return 0;
381}
382
383/*
384 * Configures event's 'attr' parameter based on the:
385 * 1) users input - specified in terms parameter
386 * 2) pmu format definitions - specified by pmu parameter
387 */
388int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
389 struct list_head *head_terms)
390{
391 attr->type = pmu->type;
392 return pmu_config(&pmu->format, attr, head_terms);
393}
394
Zheng Yana6146d52012-06-15 14:31:41 +0800395static struct perf_pmu__alias *pmu_find_alias(struct perf_pmu *pmu,
396 struct parse_events__term *term)
397{
398 struct perf_pmu__alias *alias;
399 char *name;
400
401 if (parse_events__is_hardcoded_term(term))
402 return NULL;
403
404 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
405 if (term->val.num != 1)
406 return NULL;
407 if (pmu_find_format(&pmu->format, term->config))
408 return NULL;
409 name = term->config;
410 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
411 if (strcasecmp(term->config, "event"))
412 return NULL;
413 name = term->val.str;
414 } else {
415 return NULL;
416 }
417
418 list_for_each_entry(alias, &pmu->aliases, list) {
419 if (!strcasecmp(alias->name, name))
420 return alias;
421 }
422 return NULL;
423}
424
425/*
426 * Find alias in the terms list and replace it with the terms
427 * defined for the alias
428 */
429int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms)
430{
431 struct parse_events__term *term, *h;
432 struct perf_pmu__alias *alias;
433 int ret;
434
435 list_for_each_entry_safe(term, h, head_terms, list) {
436 alias = pmu_find_alias(pmu, term);
437 if (!alias)
438 continue;
439 ret = pmu_alias_terms(alias, &term->list);
440 if (ret)
441 return ret;
442 list_del(&term->list);
443 free(term);
444 }
445 return 0;
446}
447
Jiri Olsacd82a322012-03-15 20:09:17 +0100448int perf_pmu__new_format(struct list_head *list, char *name,
449 int config, unsigned long *bits)
450{
451 struct perf_pmu__format *format;
452
453 format = zalloc(sizeof(*format));
454 if (!format)
455 return -ENOMEM;
456
457 format->name = strdup(name);
458 format->value = config;
459 memcpy(format->bits, bits, sizeof(format->bits));
460
461 list_add_tail(&format->list, list);
462 return 0;
463}
464
465void perf_pmu__set_format(unsigned long *bits, long from, long to)
466{
467 long b;
468
469 if (!to)
470 to = from;
471
472 memset(bits, 0, BITS_TO_LONGS(PERF_PMU_FORMAT_BITS));
473 for (b = from; b <= to; b++)
474 set_bit(b, bits);
475}
476
477/* Simulated format definitions. */
478static struct test_format {
479 const char *name;
480 const char *value;
481} test_formats[] = {
482 { "krava01", "config:0-1,62-63\n", },
483 { "krava02", "config:10-17\n", },
484 { "krava03", "config:5\n", },
485 { "krava11", "config1:0,2,4,6,8,20-28\n", },
486 { "krava12", "config1:63\n", },
487 { "krava13", "config1:45-47\n", },
488 { "krava21", "config2:0-3,10-13,20-23,30-33,40-43,50-53,60-63\n", },
489 { "krava22", "config2:8,18,48,58\n", },
490 { "krava23", "config2:28-29,38\n", },
491};
492
493#define TEST_FORMATS_CNT (sizeof(test_formats) / sizeof(struct test_format))
494
495/* Simulated users input. */
496static struct parse_events__term test_terms[] = {
497 {
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200498 .config = (char *) "krava01",
499 .val.num = 15,
500 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
501 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
Jiri Olsacd82a322012-03-15 20:09:17 +0100502 },
503 {
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200504 .config = (char *) "krava02",
505 .val.num = 170,
506 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
507 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
Jiri Olsacd82a322012-03-15 20:09:17 +0100508 },
509 {
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200510 .config = (char *) "krava03",
511 .val.num = 1,
512 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
513 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
Jiri Olsacd82a322012-03-15 20:09:17 +0100514 },
515 {
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200516 .config = (char *) "krava11",
517 .val.num = 27,
518 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
519 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
Jiri Olsacd82a322012-03-15 20:09:17 +0100520 },
521 {
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200522 .config = (char *) "krava12",
523 .val.num = 1,
524 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
525 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
Jiri Olsacd82a322012-03-15 20:09:17 +0100526 },
527 {
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200528 .config = (char *) "krava13",
529 .val.num = 2,
530 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
531 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
Jiri Olsacd82a322012-03-15 20:09:17 +0100532 },
533 {
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200534 .config = (char *) "krava21",
535 .val.num = 119,
536 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
537 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
Jiri Olsacd82a322012-03-15 20:09:17 +0100538 },
539 {
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200540 .config = (char *) "krava22",
541 .val.num = 11,
542 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
543 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
Jiri Olsacd82a322012-03-15 20:09:17 +0100544 },
545 {
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200546 .config = (char *) "krava23",
547 .val.num = 2,
548 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
549 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
Jiri Olsacd82a322012-03-15 20:09:17 +0100550 },
551};
552#define TERMS_CNT (sizeof(test_terms) / sizeof(struct parse_events__term))
553
554/*
555 * Prepare format directory data, exported by kernel
556 * at /sys/bus/event_source/devices/<dev>/format.
557 */
558static char *test_format_dir_get(void)
559{
560 static char dir[PATH_MAX];
561 unsigned int i;
562
563 snprintf(dir, PATH_MAX, "/tmp/perf-pmu-test-format-XXXXXX");
564 if (!mkdtemp(dir))
565 return NULL;
566
567 for (i = 0; i < TEST_FORMATS_CNT; i++) {
568 static char name[PATH_MAX];
569 struct test_format *format = &test_formats[i];
570 FILE *file;
571
572 snprintf(name, PATH_MAX, "%s/%s", dir, format->name);
573
574 file = fopen(name, "w");
575 if (!file)
576 return NULL;
577
578 if (1 != fwrite(format->value, strlen(format->value), 1, file))
579 break;
580
581 fclose(file);
582 }
583
584 return dir;
585}
586
587/* Cleanup format directory. */
588static int test_format_dir_put(char *dir)
589{
590 char buf[PATH_MAX];
591 snprintf(buf, PATH_MAX, "rm -f %s/*\n", dir);
592 if (system(buf))
593 return -1;
594
595 snprintf(buf, PATH_MAX, "rmdir %s\n", dir);
596 return system(buf);
597}
598
599static struct list_head *test_terms_list(void)
600{
601 static LIST_HEAD(terms);
602 unsigned int i;
603
604 for (i = 0; i < TERMS_CNT; i++)
605 list_add_tail(&test_terms[i].list, &terms);
606
607 return &terms;
608}
609
610#undef TERMS_CNT
611
612int perf_pmu__test(void)
613{
614 char *format = test_format_dir_get();
615 LIST_HEAD(formats);
616 struct list_head *terms = test_terms_list();
617 int ret;
618
619 if (!format)
620 return -EINVAL;
621
622 do {
623 struct perf_event_attr attr;
624
625 memset(&attr, 0, sizeof(attr));
626
627 ret = pmu_format_parse(format, &formats);
628 if (ret)
629 break;
630
631 ret = pmu_config(&formats, &attr, terms);
632 if (ret)
633 break;
634
635 ret = -EINVAL;
636
637 if (attr.config != 0xc00000000002a823)
638 break;
639 if (attr.config1 != 0x8000400000000145)
640 break;
641 if (attr.config2 != 0x0400000020041d07)
642 break;
643
644 ret = 0;
645 } while (0);
646
647 test_format_dir_put(format);
648 return ret;
649}