perf tools: Parse the pmu event prefix and suffix
[cascardo/linux.git] / tools / perf / util / pmu.c
1 #include <linux/list.h>
2 #include <sys/types.h>
3 #include <unistd.h>
4 #include <stdio.h>
5 #include <stdbool.h>
6 #include <stdarg.h>
7 #include <dirent.h>
8 #include <api/fs/fs.h>
9 #include <locale.h>
10 #include "util.h"
11 #include "pmu.h"
12 #include "parse-events.h"
13 #include "cpumap.h"
14
15 struct perf_pmu_format {
16         char *name;
17         int value;
18         DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
19         struct list_head list;
20 };
21
22 #define EVENT_SOURCE_DEVICE_PATH "/bus/event_source/devices/"
23
24 int perf_pmu_parse(struct list_head *list, char *name);
25 extern FILE *perf_pmu_in;
26
27 static LIST_HEAD(pmus);
28
29 /*
30  * Parse & process all the sysfs attributes located under
31  * the directory specified in 'dir' parameter.
32  */
33 int perf_pmu__format_parse(char *dir, struct list_head *head)
34 {
35         struct dirent *evt_ent;
36         DIR *format_dir;
37         int ret = 0;
38
39         format_dir = opendir(dir);
40         if (!format_dir)
41                 return -EINVAL;
42
43         while (!ret && (evt_ent = readdir(format_dir))) {
44                 char path[PATH_MAX];
45                 char *name = evt_ent->d_name;
46                 FILE *file;
47
48                 if (!strcmp(name, ".") || !strcmp(name, ".."))
49                         continue;
50
51                 snprintf(path, PATH_MAX, "%s/%s", dir, name);
52
53                 ret = -EINVAL;
54                 file = fopen(path, "r");
55                 if (!file)
56                         break;
57
58                 perf_pmu_in = file;
59                 ret = perf_pmu_parse(head, name);
60                 fclose(file);
61         }
62
63         closedir(format_dir);
64         return ret;
65 }
66
67 /*
68  * Reading/parsing the default pmu format definition, which should be
69  * located at:
70  * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
71  */
72 static int pmu_format(const char *name, struct list_head *format)
73 {
74         struct stat st;
75         char path[PATH_MAX];
76         const char *sysfs = sysfs__mountpoint();
77
78         if (!sysfs)
79                 return -1;
80
81         snprintf(path, PATH_MAX,
82                  "%s" EVENT_SOURCE_DEVICE_PATH "%s/format", sysfs, name);
83
84         if (stat(path, &st) < 0)
85                 return 0;       /* no error if format does not exist */
86
87         if (perf_pmu__format_parse(path, format))
88                 return -1;
89
90         return 0;
91 }
92
93 static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *name)
94 {
95         struct stat st;
96         ssize_t sret;
97         char scale[128];
98         int fd, ret = -1;
99         char path[PATH_MAX];
100         const char *lc;
101
102         snprintf(path, PATH_MAX, "%s/%s.scale", dir, name);
103
104         fd = open(path, O_RDONLY);
105         if (fd == -1)
106                 return -1;
107
108         if (fstat(fd, &st) < 0)
109                 goto error;
110
111         sret = read(fd, scale, sizeof(scale)-1);
112         if (sret < 0)
113                 goto error;
114
115         scale[sret] = '\0';
116         /*
117          * save current locale
118          */
119         lc = setlocale(LC_NUMERIC, NULL);
120
121         /*
122          * force to C locale to ensure kernel
123          * scale string is converted correctly.
124          * kernel uses default C locale.
125          */
126         setlocale(LC_NUMERIC, "C");
127
128         alias->scale = strtod(scale, NULL);
129
130         /* restore locale */
131         setlocale(LC_NUMERIC, lc);
132
133         ret = 0;
134 error:
135         close(fd);
136         return ret;
137 }
138
139 static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *name)
140 {
141         char path[PATH_MAX];
142         ssize_t sret;
143         int fd;
144
145         snprintf(path, PATH_MAX, "%s/%s.unit", dir, name);
146
147         fd = open(path, O_RDONLY);
148         if (fd == -1)
149                 return -1;
150
151                 sret = read(fd, alias->unit, UNIT_MAX_LEN);
152         if (sret < 0)
153                 goto error;
154
155         close(fd);
156
157         alias->unit[sret] = '\0';
158
159         return 0;
160 error:
161         close(fd);
162         alias->unit[0] = '\0';
163         return -1;
164 }
165
166 static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file)
167 {
168         struct perf_pmu_alias *alias;
169         char buf[256];
170         int ret;
171
172         ret = fread(buf, 1, sizeof(buf), file);
173         if (ret == 0)
174                 return -EINVAL;
175         buf[ret] = 0;
176
177         alias = malloc(sizeof(*alias));
178         if (!alias)
179                 return -ENOMEM;
180
181         INIT_LIST_HEAD(&alias->terms);
182         alias->scale = 1.0;
183         alias->unit[0] = '\0';
184
185         ret = parse_events_terms(&alias->terms, buf);
186         if (ret) {
187                 free(alias);
188                 return ret;
189         }
190
191         alias->name = strdup(name);
192         /*
193          * load unit name and scale if available
194          */
195         perf_pmu__parse_unit(alias, dir, name);
196         perf_pmu__parse_scale(alias, dir, name);
197
198         list_add_tail(&alias->list, list);
199
200         return 0;
201 }
202
203 static inline bool pmu_alias_info_file(char *name)
204 {
205         size_t len;
206
207         len = strlen(name);
208         if (len > 5 && !strcmp(name + len - 5, ".unit"))
209                 return true;
210         if (len > 6 && !strcmp(name + len - 6, ".scale"))
211                 return true;
212
213         return false;
214 }
215
216 /*
217  * Process all the sysfs attributes located under the directory
218  * specified in 'dir' parameter.
219  */
220 static int pmu_aliases_parse(char *dir, struct list_head *head)
221 {
222         struct dirent *evt_ent;
223         DIR *event_dir;
224         int ret = 0;
225
226         event_dir = opendir(dir);
227         if (!event_dir)
228                 return -EINVAL;
229
230         while (!ret && (evt_ent = readdir(event_dir))) {
231                 char path[PATH_MAX];
232                 char *name = evt_ent->d_name;
233                 FILE *file;
234
235                 if (!strcmp(name, ".") || !strcmp(name, ".."))
236                         continue;
237
238                 /*
239                  * skip info files parsed in perf_pmu__new_alias()
240                  */
241                 if (pmu_alias_info_file(name))
242                         continue;
243
244                 snprintf(path, PATH_MAX, "%s/%s", dir, name);
245
246                 ret = -EINVAL;
247                 file = fopen(path, "r");
248                 if (!file)
249                         break;
250
251                 ret = perf_pmu__new_alias(head, dir, name, file);
252                 fclose(file);
253         }
254
255         closedir(event_dir);
256         return ret;
257 }
258
259 /*
260  * Reading the pmu event aliases definition, which should be located at:
261  * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
262  */
263 static int pmu_aliases(const char *name, struct list_head *head)
264 {
265         struct stat st;
266         char path[PATH_MAX];
267         const char *sysfs = sysfs__mountpoint();
268
269         if (!sysfs)
270                 return -1;
271
272         snprintf(path, PATH_MAX,
273                  "%s/bus/event_source/devices/%s/events", sysfs, name);
274
275         if (stat(path, &st) < 0)
276                 return 0;        /* no error if 'events' does not exist */
277
278         if (pmu_aliases_parse(path, head))
279                 return -1;
280
281         return 0;
282 }
283
284 static int pmu_alias_terms(struct perf_pmu_alias *alias,
285                            struct list_head *terms)
286 {
287         struct parse_events_term *term, *cloned;
288         LIST_HEAD(list);
289         int ret;
290
291         list_for_each_entry(term, &alias->terms, list) {
292                 ret = parse_events_term__clone(&cloned, term);
293                 if (ret) {
294                         parse_events__free_terms(&list);
295                         return ret;
296                 }
297                 list_add_tail(&cloned->list, &list);
298         }
299         list_splice(&list, terms);
300         return 0;
301 }
302
303 /*
304  * Reading/parsing the default pmu type value, which should be
305  * located at:
306  * /sys/bus/event_source/devices/<dev>/type as sysfs attribute.
307  */
308 static int pmu_type(const char *name, __u32 *type)
309 {
310         struct stat st;
311         char path[PATH_MAX];
312         FILE *file;
313         int ret = 0;
314         const char *sysfs = sysfs__mountpoint();
315
316         if (!sysfs)
317                 return -1;
318
319         snprintf(path, PATH_MAX,
320                  "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name);
321
322         if (stat(path, &st) < 0)
323                 return -1;
324
325         file = fopen(path, "r");
326         if (!file)
327                 return -EINVAL;
328
329         if (1 != fscanf(file, "%u", type))
330                 ret = -1;
331
332         fclose(file);
333         return ret;
334 }
335
336 /* Add all pmus in sysfs to pmu list: */
337 static void pmu_read_sysfs(void)
338 {
339         char path[PATH_MAX];
340         DIR *dir;
341         struct dirent *dent;
342         const char *sysfs = sysfs__mountpoint();
343
344         if (!sysfs)
345                 return;
346
347         snprintf(path, PATH_MAX,
348                  "%s" EVENT_SOURCE_DEVICE_PATH, sysfs);
349
350         dir = opendir(path);
351         if (!dir)
352                 return;
353
354         while ((dent = readdir(dir))) {
355                 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
356                         continue;
357                 /* add to static LIST_HEAD(pmus): */
358                 perf_pmu__find(dent->d_name);
359         }
360
361         closedir(dir);
362 }
363
364 static struct cpu_map *pmu_cpumask(const char *name)
365 {
366         struct stat st;
367         char path[PATH_MAX];
368         FILE *file;
369         struct cpu_map *cpus;
370         const char *sysfs = sysfs__mountpoint();
371
372         if (!sysfs)
373                 return NULL;
374
375         snprintf(path, PATH_MAX,
376                  "%s/bus/event_source/devices/%s/cpumask", sysfs, name);
377
378         if (stat(path, &st) < 0)
379                 return NULL;
380
381         file = fopen(path, "r");
382         if (!file)
383                 return NULL;
384
385         cpus = cpu_map__read(file);
386         fclose(file);
387         return cpus;
388 }
389
390 struct perf_event_attr *__attribute__((weak))
391 perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
392 {
393         return NULL;
394 }
395
396 static struct perf_pmu *pmu_lookup(const char *name)
397 {
398         struct perf_pmu *pmu;
399         LIST_HEAD(format);
400         LIST_HEAD(aliases);
401         __u32 type;
402
403         /*
404          * The pmu data we store & need consists of the pmu
405          * type value and format definitions. Load both right
406          * now.
407          */
408         if (pmu_format(name, &format))
409                 return NULL;
410
411         if (pmu_aliases(name, &aliases))
412                 return NULL;
413
414         if (pmu_type(name, &type))
415                 return NULL;
416
417         pmu = zalloc(sizeof(*pmu));
418         if (!pmu)
419                 return NULL;
420
421         pmu->cpus = pmu_cpumask(name);
422
423         INIT_LIST_HEAD(&pmu->format);
424         INIT_LIST_HEAD(&pmu->aliases);
425         list_splice(&format, &pmu->format);
426         list_splice(&aliases, &pmu->aliases);
427         pmu->name = strdup(name);
428         pmu->type = type;
429         list_add_tail(&pmu->list, &pmus);
430
431         pmu->default_config = perf_pmu__get_default_config(pmu);
432
433         return pmu;
434 }
435
436 static struct perf_pmu *pmu_find(const char *name)
437 {
438         struct perf_pmu *pmu;
439
440         list_for_each_entry(pmu, &pmus, list)
441                 if (!strcmp(pmu->name, name))
442                         return pmu;
443
444         return NULL;
445 }
446
447 struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
448 {
449         /*
450          * pmu iterator: If pmu is NULL, we start at the begin,
451          * otherwise return the next pmu. Returns NULL on end.
452          */
453         if (!pmu) {
454                 pmu_read_sysfs();
455                 pmu = list_prepare_entry(pmu, &pmus, list);
456         }
457         list_for_each_entry_continue(pmu, &pmus, list)
458                 return pmu;
459         return NULL;
460 }
461
462 struct perf_pmu *perf_pmu__find(const char *name)
463 {
464         struct perf_pmu *pmu;
465
466         /*
467          * Once PMU is loaded it stays in the list,
468          * so we keep us from multiple reading/parsing
469          * the pmu format definitions.
470          */
471         pmu = pmu_find(name);
472         if (pmu)
473                 return pmu;
474
475         return pmu_lookup(name);
476 }
477
478 static struct perf_pmu_format *
479 pmu_find_format(struct list_head *formats, char *name)
480 {
481         struct perf_pmu_format *format;
482
483         list_for_each_entry(format, formats, list)
484                 if (!strcmp(format->name, name))
485                         return format;
486
487         return NULL;
488 }
489
490 /*
491  * Sets value based on the format definition (format parameter)
492  * and unformated value (value parameter).
493  */
494 static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
495                              bool zero)
496 {
497         unsigned long fbit, vbit;
498
499         for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
500
501                 if (!test_bit(fbit, format))
502                         continue;
503
504                 if (value & (1llu << vbit++))
505                         *v |= (1llu << fbit);
506                 else if (zero)
507                         *v &= ~(1llu << fbit);
508         }
509 }
510
511 /*
512  * Setup one of config[12] attr members based on the
513  * user input data - term parameter.
514  */
515 static int pmu_config_term(struct list_head *formats,
516                            struct perf_event_attr *attr,
517                            struct parse_events_term *term,
518                            bool zero)
519 {
520         struct perf_pmu_format *format;
521         __u64 *vp;
522
523         /*
524          * Support only for hardcoded and numnerial terms.
525          * Hardcoded terms should be already in, so nothing
526          * to be done for them.
527          */
528         if (parse_events__is_hardcoded_term(term))
529                 return 0;
530
531         if (term->type_val != PARSE_EVENTS__TERM_TYPE_NUM)
532                 return -EINVAL;
533
534         format = pmu_find_format(formats, term->config);
535         if (!format)
536                 return -EINVAL;
537
538         switch (format->value) {
539         case PERF_PMU_FORMAT_VALUE_CONFIG:
540                 vp = &attr->config;
541                 break;
542         case PERF_PMU_FORMAT_VALUE_CONFIG1:
543                 vp = &attr->config1;
544                 break;
545         case PERF_PMU_FORMAT_VALUE_CONFIG2:
546                 vp = &attr->config2;
547                 break;
548         default:
549                 return -EINVAL;
550         }
551
552         /*
553          * XXX If we ever decide to go with string values for
554          * non-hardcoded terms, here's the place to translate
555          * them into value.
556          */
557         pmu_format_value(format->bits, term->val.num, vp, zero);
558         return 0;
559 }
560
561 int perf_pmu__config_terms(struct list_head *formats,
562                            struct perf_event_attr *attr,
563                            struct list_head *head_terms,
564                            bool zero)
565 {
566         struct parse_events_term *term;
567
568         list_for_each_entry(term, head_terms, list)
569                 if (pmu_config_term(formats, attr, term, zero))
570                         return -EINVAL;
571
572         return 0;
573 }
574
575 /*
576  * Configures event's 'attr' parameter based on the:
577  * 1) users input - specified in terms parameter
578  * 2) pmu format definitions - specified by pmu parameter
579  */
580 int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
581                      struct list_head *head_terms)
582 {
583         bool zero = !!pmu->default_config;
584
585         attr->type = pmu->type;
586         return perf_pmu__config_terms(&pmu->format, attr, head_terms, zero);
587 }
588
589 static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
590                                              struct parse_events_term *term)
591 {
592         struct perf_pmu_alias *alias;
593         char *name;
594
595         if (parse_events__is_hardcoded_term(term))
596                 return NULL;
597
598         if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
599                 if (term->val.num != 1)
600                         return NULL;
601                 if (pmu_find_format(&pmu->format, term->config))
602                         return NULL;
603                 name = term->config;
604         } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
605                 if (strcasecmp(term->config, "event"))
606                         return NULL;
607                 name = term->val.str;
608         } else {
609                 return NULL;
610         }
611
612         list_for_each_entry(alias, &pmu->aliases, list) {
613                 if (!strcasecmp(alias->name, name))
614                         return alias;
615         }
616         return NULL;
617 }
618
619
620 static int check_unit_scale(struct perf_pmu_alias *alias,
621                             const char **unit, double *scale)
622 {
623         /*
624          * Only one term in event definition can
625          * define unit and scale, fail if there's
626          * more than one.
627          */
628         if ((*unit && alias->unit) ||
629             (*scale && alias->scale))
630                 return -EINVAL;
631
632         if (alias->unit)
633                 *unit = alias->unit;
634
635         if (alias->scale)
636                 *scale = alias->scale;
637
638         return 0;
639 }
640
641 /*
642  * Find alias in the terms list and replace it with the terms
643  * defined for the alias
644  */
645 int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
646                           struct perf_pmu_info *info)
647 {
648         struct parse_events_term *term, *h;
649         struct perf_pmu_alias *alias;
650         int ret;
651
652         /*
653          * Mark unit and scale as not set
654          * (different from default values, see below)
655          */
656         info->unit   = NULL;
657         info->scale  = 0.0;
658
659         list_for_each_entry_safe(term, h, head_terms, list) {
660                 alias = pmu_find_alias(pmu, term);
661                 if (!alias)
662                         continue;
663                 ret = pmu_alias_terms(alias, &term->list);
664                 if (ret)
665                         return ret;
666
667                 ret = check_unit_scale(alias, &info->unit, &info->scale);
668                 if (ret)
669                         return ret;
670
671                 list_del(&term->list);
672                 free(term);
673         }
674
675         /*
676          * if no unit or scale foundin aliases, then
677          * set defaults as for evsel
678          * unit cannot left to NULL
679          */
680         if (info->unit == NULL)
681                 info->unit   = "";
682
683         if (info->scale == 0.0)
684                 info->scale  = 1.0;
685
686         return 0;
687 }
688
689 int perf_pmu__new_format(struct list_head *list, char *name,
690                          int config, unsigned long *bits)
691 {
692         struct perf_pmu_format *format;
693
694         format = zalloc(sizeof(*format));
695         if (!format)
696                 return -ENOMEM;
697
698         format->name = strdup(name);
699         format->value = config;
700         memcpy(format->bits, bits, sizeof(format->bits));
701
702         list_add_tail(&format->list, list);
703         return 0;
704 }
705
706 void perf_pmu__set_format(unsigned long *bits, long from, long to)
707 {
708         long b;
709
710         if (!to)
711                 to = from;
712
713         memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
714         for (b = from; b <= to; b++)
715                 set_bit(b, bits);
716 }
717
718 static char *format_alias(char *buf, int len, struct perf_pmu *pmu,
719                           struct perf_pmu_alias *alias)
720 {
721         snprintf(buf, len, "%s/%s/", pmu->name, alias->name);
722         return buf;
723 }
724
725 static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu,
726                              struct perf_pmu_alias *alias)
727 {
728         snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name);
729         return buf;
730 }
731
732 static int cmp_string(const void *a, const void *b)
733 {
734         const char * const *as = a;
735         const char * const *bs = b;
736         return strcmp(*as, *bs);
737 }
738
739 void print_pmu_events(const char *event_glob, bool name_only)
740 {
741         struct perf_pmu *pmu;
742         struct perf_pmu_alias *alias;
743         char buf[1024];
744         int printed = 0;
745         int len, j;
746         char **aliases;
747
748         pmu = NULL;
749         len = 0;
750         while ((pmu = perf_pmu__scan(pmu)) != NULL)
751                 list_for_each_entry(alias, &pmu->aliases, list)
752                         len++;
753         aliases = malloc(sizeof(char *) * len);
754         if (!aliases)
755                 return;
756         pmu = NULL;
757         j = 0;
758         while ((pmu = perf_pmu__scan(pmu)) != NULL)
759                 list_for_each_entry(alias, &pmu->aliases, list) {
760                         char *name = format_alias(buf, sizeof(buf), pmu, alias);
761                         bool is_cpu = !strcmp(pmu->name, "cpu");
762
763                         if (event_glob != NULL &&
764                             !(strglobmatch(name, event_glob) ||
765                               (!is_cpu && strglobmatch(alias->name,
766                                                        event_glob))))
767                                 continue;
768                         aliases[j] = name;
769                         if (is_cpu && !name_only)
770                                 aliases[j] = format_alias_or(buf, sizeof(buf),
771                                                               pmu, alias);
772                         aliases[j] = strdup(aliases[j]);
773                         j++;
774                 }
775         len = j;
776         qsort(aliases, len, sizeof(char *), cmp_string);
777         for (j = 0; j < len; j++) {
778                 if (name_only) {
779                         printf("%s ", aliases[j]);
780                         continue;
781                 }
782                 printf("  %-50s [Kernel PMU event]\n", aliases[j]);
783                 zfree(&aliases[j]);
784                 printed++;
785         }
786         if (printed)
787                 printf("\n");
788         free(aliases);
789 }
790
791 bool pmu_have_event(const char *pname, const char *name)
792 {
793         struct perf_pmu *pmu;
794         struct perf_pmu_alias *alias;
795
796         pmu = NULL;
797         while ((pmu = perf_pmu__scan(pmu)) != NULL) {
798                 if (strcmp(pname, pmu->name))
799                         continue;
800                 list_for_each_entry(alias, &pmu->aliases, list)
801                         if (!strcmp(alias->name, name))
802                                 return true;
803         }
804         return false;
805 }
806
807 static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
808 {
809         struct stat st;
810         char path[PATH_MAX];
811         const char *sysfs;
812
813         sysfs = sysfs__mountpoint();
814         if (!sysfs)
815                 return NULL;
816
817         snprintf(path, PATH_MAX,
818                  "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name);
819
820         if (stat(path, &st) < 0)
821                 return NULL;
822
823         return fopen(path, "r");
824 }
825
826 int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
827                         ...)
828 {
829         va_list args;
830         FILE *file;
831         int ret = EOF;
832
833         va_start(args, fmt);
834         file = perf_pmu__open_file(pmu, name);
835         if (file) {
836                 ret = vfscanf(file, fmt, args);
837                 fclose(file);
838         }
839         va_end(args);
840         return ret;
841 }