1#include <linux/list.h>
2#include <linux/compiler.h>
3#include <sys/types.h>
4#include <errno.h>
5#include <fcntl.h>
6#include <sys/stat.h>
7#include <unistd.h>
8#include <stdio.h>
9#include <stdbool.h>
10#include <stdarg.h>
11#include <dirent.h>
12#include <api/fs/fs.h>
13#include <locale.h>
14#include <regex.h>
15#include "util.h"
16#include "pmu.h"
17#include "parse-events.h"
18#include "cpumap.h"
19#include "header.h"
20#include "pmu-events/pmu-events.h"
21#include "cache.h"
22#include "string2.h"
23
24struct perf_pmu_format {
25 char *name;
26 int value;
27 DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
28 struct list_head list;
29};
30
31#define EVENT_SOURCE_DEVICE_PATH "/bus/event_source/devices/"
32
33int perf_pmu_parse(struct list_head *list, char *name);
34extern FILE *perf_pmu_in;
35
36static LIST_HEAD(pmus);
37
38
39
40
41
42int perf_pmu__format_parse(char *dir, struct list_head *head)
43{
44 struct dirent *evt_ent;
45 DIR *format_dir;
46 int ret = 0;
47
48 format_dir = opendir(dir);
49 if (!format_dir)
50 return -EINVAL;
51
52 while (!ret && (evt_ent = readdir(format_dir))) {
53 char path[PATH_MAX];
54 char *name = evt_ent->d_name;
55 FILE *file;
56
57 if (!strcmp(name, ".") || !strcmp(name, ".."))
58 continue;
59
60 snprintf(path, PATH_MAX, "%s/%s", dir, name);
61
62 ret = -EINVAL;
63 file = fopen(path, "r");
64 if (!file)
65 break;
66
67 perf_pmu_in = file;
68 ret = perf_pmu_parse(head, name);
69 fclose(file);
70 }
71
72 closedir(format_dir);
73 return ret;
74}
75
76
77
78
79
80
81static int pmu_format(const char *name, struct list_head *format)
82{
83 struct stat st;
84 char path[PATH_MAX];
85 const char *sysfs = sysfs__mountpoint();
86
87 if (!sysfs)
88 return -1;
89
90 snprintf(path, PATH_MAX,
91 "%s" EVENT_SOURCE_DEVICE_PATH "%s/format", sysfs, name);
92
93 if (stat(path, &st) < 0)
94 return 0;
95
96 if (perf_pmu__format_parse(path, format))
97 return -1;
98
99 return 0;
100}
101
102static int convert_scale(const char *scale, char **end, double *sval)
103{
104 char *lc;
105 int ret = 0;
106
107
108
109
110 lc = setlocale(LC_NUMERIC, NULL);
111
112
113
114
115
116
117 lc = strdup(lc);
118 if (!lc) {
119 ret = -ENOMEM;
120 goto out;
121 }
122
123
124
125
126
127
128 setlocale(LC_NUMERIC, "C");
129
130 *sval = strtod(scale, end);
131
132out:
133
134 setlocale(LC_NUMERIC, lc);
135 free(lc);
136 return ret;
137}
138
139static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *name)
140{
141 struct stat st;
142 ssize_t sret;
143 char scale[128];
144 int fd, ret = -1;
145 char path[PATH_MAX];
146
147 scnprintf(path, PATH_MAX, "%s/%s.scale", dir, name);
148
149 fd = open(path, O_RDONLY);
150 if (fd == -1)
151 return -1;
152
153 if (fstat(fd, &st) < 0)
154 goto error;
155
156 sret = read(fd, scale, sizeof(scale)-1);
157 if (sret < 0)
158 goto error;
159
160 if (scale[sret - 1] == '\n')
161 scale[sret - 1] = '\0';
162 else
163 scale[sret] = '\0';
164
165 ret = convert_scale(scale, NULL, &alias->scale);
166error:
167 close(fd);
168 return ret;
169}
170
171static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *name)
172{
173 char path[PATH_MAX];
174 ssize_t sret;
175 int fd;
176
177 scnprintf(path, PATH_MAX, "%s/%s.unit", dir, name);
178
179 fd = open(path, O_RDONLY);
180 if (fd == -1)
181 return -1;
182
183 sret = read(fd, alias->unit, UNIT_MAX_LEN);
184 if (sret < 0)
185 goto error;
186
187 close(fd);
188
189 if (alias->unit[sret - 1] == '\n')
190 alias->unit[sret - 1] = '\0';
191 else
192 alias->unit[sret] = '\0';
193
194 return 0;
195error:
196 close(fd);
197 alias->unit[0] = '\0';
198 return -1;
199}
200
201static int
202perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, char *dir, char *name)
203{
204 char path[PATH_MAX];
205 int fd;
206
207 scnprintf(path, PATH_MAX, "%s/%s.per-pkg", dir, name);
208
209 fd = open(path, O_RDONLY);
210 if (fd == -1)
211 return -1;
212
213 close(fd);
214
215 alias->per_pkg = true;
216 return 0;
217}
218
219static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias,
220 char *dir, char *name)
221{
222 char path[PATH_MAX];
223 int fd;
224
225 scnprintf(path, PATH_MAX, "%s/%s.snapshot", dir, name);
226
227 fd = open(path, O_RDONLY);
228 if (fd == -1)
229 return -1;
230
231 alias->snapshot = true;
232 close(fd);
233 return 0;
234}
235
236static void perf_pmu_assign_str(char *name, const char *field, char **old_str,
237 char **new_str)
238{
239 if (!*old_str)
240 goto set_new;
241
242 if (*new_str) {
243 if (strcasecmp(*old_str, *new_str))
244 pr_debug("alias %s differs in field '%s'\n",
245 name, field);
246 zfree(old_str);
247 } else
248 return;
249set_new:
250 *old_str = *new_str;
251 *new_str = NULL;
252}
253
254static void perf_pmu_update_alias(struct perf_pmu_alias *old,
255 struct perf_pmu_alias *newalias)
256{
257 perf_pmu_assign_str(old->name, "desc", &old->desc, &newalias->desc);
258 perf_pmu_assign_str(old->name, "long_desc", &old->long_desc,
259 &newalias->long_desc);
260 perf_pmu_assign_str(old->name, "topic", &old->topic, &newalias->topic);
261 perf_pmu_assign_str(old->name, "metric_expr", &old->metric_expr,
262 &newalias->metric_expr);
263 perf_pmu_assign_str(old->name, "metric_name", &old->metric_name,
264 &newalias->metric_name);
265 perf_pmu_assign_str(old->name, "value", &old->str, &newalias->str);
266 old->scale = newalias->scale;
267 old->per_pkg = newalias->per_pkg;
268 old->snapshot = newalias->snapshot;
269 memcpy(old->unit, newalias->unit, sizeof(old->unit));
270}
271
272
273static void perf_pmu_free_alias(struct perf_pmu_alias *newalias)
274{
275 zfree(&newalias->name);
276 zfree(&newalias->desc);
277 zfree(&newalias->long_desc);
278 zfree(&newalias->topic);
279 zfree(&newalias->str);
280 zfree(&newalias->metric_expr);
281 zfree(&newalias->metric_name);
282 parse_events_terms__purge(&newalias->terms);
283 free(newalias);
284}
285
286
287
288
289static bool perf_pmu_merge_alias(struct perf_pmu_alias *newalias,
290 struct list_head *alist)
291{
292 struct perf_pmu_alias *a;
293
294 list_for_each_entry(a, alist, list) {
295 if (!strcasecmp(newalias->name, a->name)) {
296 perf_pmu_update_alias(a, newalias);
297 perf_pmu_free_alias(newalias);
298 return true;
299 }
300 }
301 return false;
302}
303
304static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name,
305 char *desc, char *val,
306 char *long_desc, char *topic,
307 char *unit, char *perpkg,
308 char *metric_expr,
309 char *metric_name)
310{
311 struct parse_events_term *term;
312 struct perf_pmu_alias *alias;
313 int ret;
314 int num;
315 char newval[256];
316
317 alias = malloc(sizeof(*alias));
318 if (!alias)
319 return -ENOMEM;
320
321 INIT_LIST_HEAD(&alias->terms);
322 alias->scale = 1.0;
323 alias->unit[0] = '\0';
324 alias->per_pkg = false;
325 alias->snapshot = false;
326
327 ret = parse_events_terms(&alias->terms, val);
328 if (ret) {
329 pr_err("Cannot parse alias %s: %d\n", val, ret);
330 free(alias);
331 return ret;
332 }
333
334
335
336
337
338
339
340
341 memset(newval, 0, sizeof(newval));
342 ret = 0;
343 list_for_each_entry(term, &alias->terms, list) {
344 if (ret)
345 ret += scnprintf(newval + ret, sizeof(newval) - ret,
346 ",");
347 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM)
348 ret += scnprintf(newval + ret, sizeof(newval) - ret,
349 "%s=%#x", term->config, term->val.num);
350 else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
351 ret += scnprintf(newval + ret, sizeof(newval) - ret,
352 "%s=%s", term->config, term->val.str);
353 }
354
355 alias->name = strdup(name);
356 if (dir) {
357
358
359
360 perf_pmu__parse_unit(alias, dir, name);
361 perf_pmu__parse_scale(alias, dir, name);
362 perf_pmu__parse_per_pkg(alias, dir, name);
363 perf_pmu__parse_snapshot(alias, dir, name);
364 }
365
366 alias->metric_expr = metric_expr ? strdup(metric_expr) : NULL;
367 alias->metric_name = metric_name ? strdup(metric_name): NULL;
368 alias->desc = desc ? strdup(desc) : NULL;
369 alias->long_desc = long_desc ? strdup(long_desc) :
370 desc ? strdup(desc) : NULL;
371 alias->topic = topic ? strdup(topic) : NULL;
372 if (unit) {
373 if (convert_scale(unit, &unit, &alias->scale) < 0)
374 return -1;
375 snprintf(alias->unit, sizeof(alias->unit), "%s", unit);
376 }
377 alias->per_pkg = perpkg && sscanf(perpkg, "%d", &num) == 1 && num == 1;
378 alias->str = strdup(newval);
379
380 if (!perf_pmu_merge_alias(alias, list))
381 list_add_tail(&alias->list, list);
382
383 return 0;
384}
385
386static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file)
387{
388 char buf[256];
389 int ret;
390
391 ret = fread(buf, 1, sizeof(buf), file);
392 if (ret == 0)
393 return -EINVAL;
394
395 buf[ret] = 0;
396
397
398 rtrim(buf);
399
400 return __perf_pmu__new_alias(list, dir, name, NULL, buf, NULL, NULL, NULL,
401 NULL, NULL, NULL);
402}
403
404static inline bool pmu_alias_info_file(char *name)
405{
406 size_t len;
407
408 len = strlen(name);
409 if (len > 5 && !strcmp(name + len - 5, ".unit"))
410 return true;
411 if (len > 6 && !strcmp(name + len - 6, ".scale"))
412 return true;
413 if (len > 8 && !strcmp(name + len - 8, ".per-pkg"))
414 return true;
415 if (len > 9 && !strcmp(name + len - 9, ".snapshot"))
416 return true;
417
418 return false;
419}
420
421
422
423
424
425static int pmu_aliases_parse(char *dir, struct list_head *head)
426{
427 struct dirent *evt_ent;
428 DIR *event_dir;
429
430 event_dir = opendir(dir);
431 if (!event_dir)
432 return -EINVAL;
433
434 while ((evt_ent = readdir(event_dir))) {
435 char path[PATH_MAX];
436 char *name = evt_ent->d_name;
437 FILE *file;
438
439 if (!strcmp(name, ".") || !strcmp(name, ".."))
440 continue;
441
442
443
444
445 if (pmu_alias_info_file(name))
446 continue;
447
448 scnprintf(path, PATH_MAX, "%s/%s", dir, name);
449
450 file = fopen(path, "r");
451 if (!file) {
452 pr_debug("Cannot open %s\n", path);
453 continue;
454 }
455
456 if (perf_pmu__new_alias(head, dir, name, file) < 0)
457 pr_debug("Cannot set up %s\n", name);
458 fclose(file);
459 }
460
461 closedir(event_dir);
462 return 0;
463}
464
465
466
467
468
469static int pmu_aliases(const char *name, struct list_head *head)
470{
471 struct stat st;
472 char path[PATH_MAX];
473 const char *sysfs = sysfs__mountpoint();
474
475 if (!sysfs)
476 return -1;
477
478 snprintf(path, PATH_MAX,
479 "%s/bus/event_source/devices/%s/events", sysfs, name);
480
481 if (stat(path, &st) < 0)
482 return 0;
483
484 if (pmu_aliases_parse(path, head))
485 return -1;
486
487 return 0;
488}
489
490static int pmu_alias_terms(struct perf_pmu_alias *alias,
491 struct list_head *terms)
492{
493 struct parse_events_term *term, *cloned;
494 LIST_HEAD(list);
495 int ret;
496
497 list_for_each_entry(term, &alias->terms, list) {
498 ret = parse_events_term__clone(&cloned, term);
499 if (ret) {
500 parse_events_terms__purge(&list);
501 return ret;
502 }
503
504
505
506
507 cloned->weak = true;
508 list_add_tail(&cloned->list, &list);
509 }
510 list_splice(&list, terms);
511 return 0;
512}
513
514
515
516
517
518
519static int pmu_type(const char *name, __u32 *type)
520{
521 struct stat st;
522 char path[PATH_MAX];
523 FILE *file;
524 int ret = 0;
525 const char *sysfs = sysfs__mountpoint();
526
527 if (!sysfs)
528 return -1;
529
530 snprintf(path, PATH_MAX,
531 "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name);
532
533 if (stat(path, &st) < 0)
534 return -1;
535
536 file = fopen(path, "r");
537 if (!file)
538 return -EINVAL;
539
540 if (1 != fscanf(file, "%u", type))
541 ret = -1;
542
543 fclose(file);
544 return ret;
545}
546
547
548static void pmu_read_sysfs(void)
549{
550 char path[PATH_MAX];
551 DIR *dir;
552 struct dirent *dent;
553 const char *sysfs = sysfs__mountpoint();
554
555 if (!sysfs)
556 return;
557
558 snprintf(path, PATH_MAX,
559 "%s" EVENT_SOURCE_DEVICE_PATH, sysfs);
560
561 dir = opendir(path);
562 if (!dir)
563 return;
564
565 while ((dent = readdir(dir))) {
566 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
567 continue;
568
569 perf_pmu__find(dent->d_name);
570 }
571
572 closedir(dir);
573}
574
575static struct cpu_map *__pmu_cpumask(const char *path)
576{
577 FILE *file;
578 struct cpu_map *cpus;
579
580 file = fopen(path, "r");
581 if (!file)
582 return NULL;
583
584 cpus = cpu_map__read(file);
585 fclose(file);
586 return cpus;
587}
588
589
590
591
592
593#define CPUS_TEMPLATE_UNCORE "%s/bus/event_source/devices/%s/cpumask"
594#define CPUS_TEMPLATE_CPU "%s/bus/event_source/devices/%s/cpus"
595
596static struct cpu_map *pmu_cpumask(const char *name)
597{
598 char path[PATH_MAX];
599 struct cpu_map *cpus;
600 const char *sysfs = sysfs__mountpoint();
601 const char *templates[] = {
602 CPUS_TEMPLATE_UNCORE,
603 CPUS_TEMPLATE_CPU,
604 NULL
605 };
606 const char **template;
607
608 if (!sysfs)
609 return NULL;
610
611 for (template = templates; *template; template++) {
612 snprintf(path, PATH_MAX, *template, sysfs, name);
613 cpus = __pmu_cpumask(path);
614 if (cpus)
615 return cpus;
616 }
617
618 return NULL;
619}
620
621static bool pmu_is_uncore(const char *name)
622{
623 char path[PATH_MAX];
624 struct cpu_map *cpus;
625 const char *sysfs = sysfs__mountpoint();
626
627 snprintf(path, PATH_MAX, CPUS_TEMPLATE_UNCORE, sysfs, name);
628 cpus = __pmu_cpumask(path);
629 cpu_map__put(cpus);
630
631 return !!cpus;
632}
633
634
635
636
637
638
639static int is_arm_pmu_core(const char *name)
640{
641 struct stat st;
642 char path[PATH_MAX];
643 const char *sysfs = sysfs__mountpoint();
644
645 if (!sysfs)
646 return 0;
647
648
649 scnprintf(path, PATH_MAX, "%s/bus/event_source/devices/%s/cpus",
650 sysfs, name);
651 if (stat(path, &st) == 0)
652 return 1;
653
654
655 scnprintf(path, PATH_MAX, "%s/bus/event_source/devices/%s",
656 sysfs, name);
657 if (stat(path, &st) == 0 && !strncmp(name, "cpum_", 5))
658 return 1;
659
660 return 0;
661}
662
663
664
665
666
667
668
669char * __weak get_cpuid_str(struct perf_pmu *pmu __maybe_unused)
670{
671 return NULL;
672}
673
674
675
676
677
678int __weak strcmp_cpuid_str(const char *mapcpuid, const char *cpuid)
679{
680 regex_t re;
681 regmatch_t pmatch[1];
682 int match;
683
684 if (regcomp(&re, mapcpuid, REG_EXTENDED) != 0) {
685
686 pr_info("Invalid regular expression %s\n", mapcpuid);
687 return 1;
688 }
689
690 match = !regexec(&re, cpuid, 1, pmatch, 0);
691 regfree(&re);
692 if (match) {
693 size_t match_len = (pmatch[0].rm_eo - pmatch[0].rm_so);
694
695
696 if (match_len == strlen(cpuid))
697 return 0;
698 }
699 return 1;
700}
701
702static char *perf_pmu__getcpuid(struct perf_pmu *pmu)
703{
704 char *cpuid;
705 static bool printed;
706
707 cpuid = getenv("PERF_CPUID");
708 if (cpuid)
709 cpuid = strdup(cpuid);
710 if (!cpuid)
711 cpuid = get_cpuid_str(pmu);
712 if (!cpuid)
713 return NULL;
714
715 if (!printed) {
716 pr_debug("Using CPUID %s\n", cpuid);
717 printed = true;
718 }
719 return cpuid;
720}
721
722struct pmu_events_map *perf_pmu__find_map(struct perf_pmu *pmu)
723{
724 struct pmu_events_map *map;
725 char *cpuid = perf_pmu__getcpuid(pmu);
726 int i;
727
728
729
730
731 if (!cpuid)
732 return NULL;
733
734 i = 0;
735 for (;;) {
736 map = &pmu_events_map[i++];
737 if (!map->table) {
738 map = NULL;
739 break;
740 }
741
742 if (!strcmp_cpuid_str(map->cpuid, cpuid))
743 break;
744 }
745 free(cpuid);
746 return map;
747}
748
749
750
751
752
753
754static void pmu_add_cpu_aliases(struct list_head *head, struct perf_pmu *pmu)
755{
756 int i;
757 struct pmu_events_map *map;
758 struct pmu_event *pe;
759 const char *name = pmu->name;
760 const char *pname;
761
762 map = perf_pmu__find_map(pmu);
763 if (!map)
764 return;
765
766
767
768
769 i = 0;
770 while (1) {
771
772 pe = &map->table[i++];
773 if (!pe->name) {
774 if (pe->metric_group || pe->metric_name)
775 continue;
776 break;
777 }
778
779 if (!is_arm_pmu_core(name)) {
780 pname = pe->pmu ? pe->pmu : "cpu";
781 if (strcmp(pname, name))
782 continue;
783 }
784
785
786 __perf_pmu__new_alias(head, NULL, (char *)pe->name,
787 (char *)pe->desc, (char *)pe->event,
788 (char *)pe->long_desc, (char *)pe->topic,
789 (char *)pe->unit, (char *)pe->perpkg,
790 (char *)pe->metric_expr,
791 (char *)pe->metric_name);
792 }
793}
794
795struct perf_event_attr * __weak
796perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
797{
798 return NULL;
799}
800
801static struct perf_pmu *pmu_lookup(const char *name)
802{
803 struct perf_pmu *pmu;
804 LIST_HEAD(format);
805 LIST_HEAD(aliases);
806 __u32 type;
807
808
809
810
811
812
813 if (pmu_format(name, &format))
814 return NULL;
815
816
817
818
819 if (pmu_type(name, &type))
820 return NULL;
821
822 if (pmu_aliases(name, &aliases))
823 return NULL;
824
825 pmu = zalloc(sizeof(*pmu));
826 if (!pmu)
827 return NULL;
828
829 pmu->cpus = pmu_cpumask(name);
830 pmu->name = strdup(name);
831 pmu->type = type;
832 pmu->is_uncore = pmu_is_uncore(name);
833 pmu_add_cpu_aliases(&aliases, pmu);
834
835 INIT_LIST_HEAD(&pmu->format);
836 INIT_LIST_HEAD(&pmu->aliases);
837 list_splice(&format, &pmu->format);
838 list_splice(&aliases, &pmu->aliases);
839 list_add_tail(&pmu->list, &pmus);
840
841 pmu->default_config = perf_pmu__get_default_config(pmu);
842
843 return pmu;
844}
845
846static struct perf_pmu *pmu_find(const char *name)
847{
848 struct perf_pmu *pmu;
849
850 list_for_each_entry(pmu, &pmus, list)
851 if (!strcmp(pmu->name, name))
852 return pmu;
853
854 return NULL;
855}
856
857struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
858{
859
860
861
862
863 if (!pmu) {
864 pmu_read_sysfs();
865 pmu = list_prepare_entry(pmu, &pmus, list);
866 }
867 list_for_each_entry_continue(pmu, &pmus, list)
868 return pmu;
869 return NULL;
870}
871
872struct perf_pmu *perf_pmu__find(const char *name)
873{
874 struct perf_pmu *pmu;
875
876
877
878
879
880
881 pmu = pmu_find(name);
882 if (pmu)
883 return pmu;
884
885 return pmu_lookup(name);
886}
887
888static struct perf_pmu_format *
889pmu_find_format(struct list_head *formats, const char *name)
890{
891 struct perf_pmu_format *format;
892
893 list_for_each_entry(format, formats, list)
894 if (!strcmp(format->name, name))
895 return format;
896
897 return NULL;
898}
899
900__u64 perf_pmu__format_bits(struct list_head *formats, const char *name)
901{
902 struct perf_pmu_format *format = pmu_find_format(formats, name);
903 __u64 bits = 0;
904 int fbit;
905
906 if (!format)
907 return 0;
908
909 for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
910 bits |= 1ULL << fbit;
911
912 return bits;
913}
914
915
916
917
918
919static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
920 bool zero)
921{
922 unsigned long fbit, vbit;
923
924 for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
925
926 if (!test_bit(fbit, format))
927 continue;
928
929 if (value & (1llu << vbit++))
930 *v |= (1llu << fbit);
931 else if (zero)
932 *v &= ~(1llu << fbit);
933 }
934}
935
936static __u64 pmu_format_max_value(const unsigned long *format)
937{
938 int w;
939
940 w = bitmap_weight(format, PERF_PMU_FORMAT_BITS);
941 if (!w)
942 return 0;
943 if (w < 64)
944 return (1ULL << w) - 1;
945 return -1;
946}
947
948
949
950
951
952
953
954
955static int pmu_resolve_param_term(struct parse_events_term *term,
956 struct list_head *head_terms,
957 __u64 *value)
958{
959 struct parse_events_term *t;
960
961 list_for_each_entry(t, head_terms, list) {
962 if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
963 if (!strcmp(t->config, term->config)) {
964 t->used = true;
965 *value = t->val.num;
966 return 0;
967 }
968 }
969 }
970
971 if (verbose > 0)
972 printf("Required parameter '%s' not specified\n", term->config);
973
974 return -1;
975}
976
977static char *pmu_formats_string(struct list_head *formats)
978{
979 struct perf_pmu_format *format;
980 char *str = NULL;
981 struct strbuf buf = STRBUF_INIT;
982 unsigned i = 0;
983
984 if (!formats)
985 return NULL;
986
987
988 list_for_each_entry(format, formats, list)
989 if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
990 goto error;
991
992 str = strbuf_detach(&buf, NULL);
993error:
994 strbuf_release(&buf);
995
996 return str;
997}
998
999
1000
1001
1002
1003static int pmu_config_term(struct list_head *formats,
1004 struct perf_event_attr *attr,
1005 struct parse_events_term *term,
1006 struct list_head *head_terms,
1007 bool zero, struct parse_events_error *err)
1008{
1009 struct perf_pmu_format *format;
1010 __u64 *vp;
1011 __u64 val, max_val;
1012
1013
1014
1015
1016
1017 if (term->used)
1018 return 0;
1019
1020
1021
1022
1023
1024 if (parse_events__is_hardcoded_term(term))
1025 return 0;
1026
1027 format = pmu_find_format(formats, term->config);
1028 if (!format) {
1029 if (verbose > 0)
1030 printf("Invalid event/parameter '%s'\n", term->config);
1031 if (err) {
1032 char *pmu_term = pmu_formats_string(formats);
1033
1034 err->idx = term->err_term;
1035 err->str = strdup("unknown term");
1036 err->help = parse_events_formats_error_string(pmu_term);
1037 free(pmu_term);
1038 }
1039 return -EINVAL;
1040 }
1041
1042 switch (format->value) {
1043 case PERF_PMU_FORMAT_VALUE_CONFIG:
1044 vp = &attr->config;
1045 break;
1046 case PERF_PMU_FORMAT_VALUE_CONFIG1:
1047 vp = &attr->config1;
1048 break;
1049 case PERF_PMU_FORMAT_VALUE_CONFIG2:
1050 vp = &attr->config2;
1051 break;
1052 default:
1053 return -EINVAL;
1054 }
1055
1056
1057
1058
1059
1060 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1061 if (term->no_value &&
1062 bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) {
1063 if (err) {
1064 err->idx = term->err_val;
1065 err->str = strdup("no value assigned for term");
1066 }
1067 return -EINVAL;
1068 }
1069
1070 val = term->val.num;
1071 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1072 if (strcmp(term->val.str, "?")) {
1073 if (verbose > 0) {
1074 pr_info("Invalid sysfs entry %s=%s\n",
1075 term->config, term->val.str);
1076 }
1077 if (err) {
1078 err->idx = term->err_val;
1079 err->str = strdup("expected numeric value");
1080 }
1081 return -EINVAL;
1082 }
1083
1084 if (pmu_resolve_param_term(term, head_terms, &val))
1085 return -EINVAL;
1086 } else
1087 return -EINVAL;
1088
1089 max_val = pmu_format_max_value(format->bits);
1090 if (val > max_val) {
1091 if (err) {
1092 err->idx = term->err_val;
1093 if (asprintf(&err->str,
1094 "value too big for format, maximum is %llu",
1095 (unsigned long long)max_val) < 0)
1096 err->str = strdup("value too big for format");
1097 return -EINVAL;
1098 }
1099
1100
1101
1102
1103 }
1104
1105 pmu_format_value(format->bits, val, vp, zero);
1106 return 0;
1107}
1108
1109int perf_pmu__config_terms(struct list_head *formats,
1110 struct perf_event_attr *attr,
1111 struct list_head *head_terms,
1112 bool zero, struct parse_events_error *err)
1113{
1114 struct parse_events_term *term;
1115
1116 list_for_each_entry(term, head_terms, list) {
1117 if (pmu_config_term(formats, attr, term, head_terms,
1118 zero, err))
1119 return -EINVAL;
1120 }
1121
1122 return 0;
1123}
1124
1125
1126
1127
1128
1129
1130int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
1131 struct list_head *head_terms,
1132 struct parse_events_error *err)
1133{
1134 bool zero = !!pmu->default_config;
1135
1136 attr->type = pmu->type;
1137 return perf_pmu__config_terms(&pmu->format, attr, head_terms,
1138 zero, err);
1139}
1140
1141static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
1142 struct parse_events_term *term)
1143{
1144 struct perf_pmu_alias *alias;
1145 char *name;
1146
1147 if (parse_events__is_hardcoded_term(term))
1148 return NULL;
1149
1150 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1151 if (term->val.num != 1)
1152 return NULL;
1153 if (pmu_find_format(&pmu->format, term->config))
1154 return NULL;
1155 name = term->config;
1156 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1157 if (strcasecmp(term->config, "event"))
1158 return NULL;
1159 name = term->val.str;
1160 } else {
1161 return NULL;
1162 }
1163
1164 list_for_each_entry(alias, &pmu->aliases, list) {
1165 if (!strcasecmp(alias->name, name))
1166 return alias;
1167 }
1168 return NULL;
1169}
1170
1171
1172static int check_info_data(struct perf_pmu_alias *alias,
1173 struct perf_pmu_info *info)
1174{
1175
1176
1177
1178
1179
1180 if ((info->unit && alias->unit[0]) ||
1181 (info->scale && alias->scale) ||
1182 (info->snapshot && alias->snapshot))
1183 return -EINVAL;
1184
1185 if (alias->unit[0])
1186 info->unit = alias->unit;
1187
1188 if (alias->scale)
1189 info->scale = alias->scale;
1190
1191 if (alias->snapshot)
1192 info->snapshot = alias->snapshot;
1193
1194 return 0;
1195}
1196
1197
1198
1199
1200
1201int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
1202 struct perf_pmu_info *info)
1203{
1204 struct parse_events_term *term, *h;
1205 struct perf_pmu_alias *alias;
1206 int ret;
1207
1208 info->per_pkg = false;
1209
1210
1211
1212
1213
1214 info->unit = NULL;
1215 info->scale = 0.0;
1216 info->snapshot = false;
1217 info->metric_expr = NULL;
1218 info->metric_name = NULL;
1219
1220 list_for_each_entry_safe(term, h, head_terms, list) {
1221 alias = pmu_find_alias(pmu, term);
1222 if (!alias)
1223 continue;
1224 ret = pmu_alias_terms(alias, &term->list);
1225 if (ret)
1226 return ret;
1227
1228 ret = check_info_data(alias, info);
1229 if (ret)
1230 return ret;
1231
1232 if (alias->per_pkg)
1233 info->per_pkg = true;
1234 info->metric_expr = alias->metric_expr;
1235 info->metric_name = alias->metric_name;
1236
1237 list_del(&term->list);
1238 free(term);
1239 }
1240
1241
1242
1243
1244
1245
1246 if (info->unit == NULL)
1247 info->unit = "";
1248
1249 if (info->scale == 0.0)
1250 info->scale = 1.0;
1251
1252 return 0;
1253}
1254
1255int perf_pmu__new_format(struct list_head *list, char *name,
1256 int config, unsigned long *bits)
1257{
1258 struct perf_pmu_format *format;
1259
1260 format = zalloc(sizeof(*format));
1261 if (!format)
1262 return -ENOMEM;
1263
1264 format->name = strdup(name);
1265 format->value = config;
1266 memcpy(format->bits, bits, sizeof(format->bits));
1267
1268 list_add_tail(&format->list, list);
1269 return 0;
1270}
1271
1272void perf_pmu__set_format(unsigned long *bits, long from, long to)
1273{
1274 long b;
1275
1276 if (!to)
1277 to = from;
1278
1279 memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
1280 for (b = from; b <= to; b++)
1281 set_bit(b, bits);
1282}
1283
1284static int sub_non_neg(int a, int b)
1285{
1286 if (b > a)
1287 return 0;
1288 return a - b;
1289}
1290
1291static char *format_alias(char *buf, int len, struct perf_pmu *pmu,
1292 struct perf_pmu_alias *alias)
1293{
1294 struct parse_events_term *term;
1295 int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name);
1296
1297 list_for_each_entry(term, &alias->terms, list) {
1298 if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
1299 used += snprintf(buf + used, sub_non_neg(len, used),
1300 ",%s=%s", term->config,
1301 term->val.str);
1302 }
1303
1304 if (sub_non_neg(len, used) > 0) {
1305 buf[used] = '/';
1306 used++;
1307 }
1308 if (sub_non_neg(len, used) > 0) {
1309 buf[used] = '\0';
1310 used++;
1311 } else
1312 buf[len - 1] = '\0';
1313
1314 return buf;
1315}
1316
1317static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu,
1318 struct perf_pmu_alias *alias)
1319{
1320 snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name);
1321 return buf;
1322}
1323
1324struct sevent {
1325 char *name;
1326 char *desc;
1327 char *topic;
1328 char *str;
1329 char *pmu;
1330 char *metric_expr;
1331 char *metric_name;
1332};
1333
1334static int cmp_sevent(const void *a, const void *b)
1335{
1336 const struct sevent *as = a;
1337 const struct sevent *bs = b;
1338
1339
1340 if (!!as->desc != !!bs->desc)
1341 return !!as->desc - !!bs->desc;
1342 if (as->topic && bs->topic) {
1343 int n = strcmp(as->topic, bs->topic);
1344
1345 if (n)
1346 return n;
1347 }
1348 return strcmp(as->name, bs->name);
1349}
1350
1351static void wordwrap(char *s, int start, int max, int corr)
1352{
1353 int column = start;
1354 int n;
1355
1356 while (*s) {
1357 int wlen = strcspn(s, " \t");
1358
1359 if (column + wlen >= max && column > start) {
1360 printf("\n%*s", start, "");
1361 column = start + corr;
1362 }
1363 n = printf("%s%.*s", column > start ? " " : "", wlen, s);
1364 if (n <= 0)
1365 break;
1366 s += wlen;
1367 column += n;
1368 s = ltrim(s);
1369 }
1370}
1371
1372void print_pmu_events(const char *event_glob, bool name_only, bool quiet_flag,
1373 bool long_desc, bool details_flag)
1374{
1375 struct perf_pmu *pmu;
1376 struct perf_pmu_alias *alias;
1377 char buf[1024];
1378 int printed = 0;
1379 int len, j;
1380 struct sevent *aliases;
1381 int numdesc = 0;
1382 int columns = pager_get_columns();
1383 char *topic = NULL;
1384
1385 pmu = NULL;
1386 len = 0;
1387 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1388 list_for_each_entry(alias, &pmu->aliases, list)
1389 len++;
1390 if (pmu->selectable)
1391 len++;
1392 }
1393 aliases = zalloc(sizeof(struct sevent) * len);
1394 if (!aliases)
1395 goto out_enomem;
1396 pmu = NULL;
1397 j = 0;
1398 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1399 list_for_each_entry(alias, &pmu->aliases, list) {
1400 char *name = alias->desc ? alias->name :
1401 format_alias(buf, sizeof(buf), pmu, alias);
1402 bool is_cpu = !strcmp(pmu->name, "cpu");
1403
1404 if (event_glob != NULL &&
1405 !(strglobmatch_nocase(name, event_glob) ||
1406 (!is_cpu && strglobmatch_nocase(alias->name,
1407 event_glob)) ||
1408 (alias->topic &&
1409 strglobmatch_nocase(alias->topic, event_glob))))
1410 continue;
1411
1412 if (is_cpu && !name_only && !alias->desc)
1413 name = format_alias_or(buf, sizeof(buf), pmu, alias);
1414
1415 aliases[j].name = name;
1416 if (is_cpu && !name_only && !alias->desc)
1417 aliases[j].name = format_alias_or(buf,
1418 sizeof(buf),
1419 pmu, alias);
1420 aliases[j].name = strdup(aliases[j].name);
1421 if (!aliases[j].name)
1422 goto out_enomem;
1423
1424 aliases[j].desc = long_desc ? alias->long_desc :
1425 alias->desc;
1426 aliases[j].topic = alias->topic;
1427 aliases[j].str = alias->str;
1428 aliases[j].pmu = pmu->name;
1429 aliases[j].metric_expr = alias->metric_expr;
1430 aliases[j].metric_name = alias->metric_name;
1431 j++;
1432 }
1433 if (pmu->selectable &&
1434 (event_glob == NULL || strglobmatch(pmu->name, event_glob))) {
1435 char *s;
1436 if (asprintf(&s, "%s//", pmu->name) < 0)
1437 goto out_enomem;
1438 aliases[j].name = s;
1439 j++;
1440 }
1441 }
1442 len = j;
1443 qsort(aliases, len, sizeof(struct sevent), cmp_sevent);
1444 for (j = 0; j < len; j++) {
1445
1446 if (j > 0 && !strcmp(aliases[j].name, aliases[j - 1].name))
1447 continue;
1448 if (name_only) {
1449 printf("%s ", aliases[j].name);
1450 continue;
1451 }
1452 if (aliases[j].desc && !quiet_flag) {
1453 if (numdesc++ == 0)
1454 printf("\n");
1455 if (aliases[j].topic && (!topic ||
1456 strcmp(topic, aliases[j].topic))) {
1457 printf("%s%s:\n", topic ? "\n" : "",
1458 aliases[j].topic);
1459 topic = aliases[j].topic;
1460 }
1461 printf(" %-50s\n", aliases[j].name);
1462 printf("%*s", 8, "[");
1463 wordwrap(aliases[j].desc, 8, columns, 0);
1464 printf("]\n");
1465 if (details_flag) {
1466 printf("%*s%s/%s/ ", 8, "", aliases[j].pmu, aliases[j].str);
1467 if (aliases[j].metric_name)
1468 printf(" MetricName: %s", aliases[j].metric_name);
1469 if (aliases[j].metric_expr)
1470 printf(" MetricExpr: %s", aliases[j].metric_expr);
1471 putchar('\n');
1472 }
1473 } else
1474 printf(" %-50s [Kernel PMU event]\n", aliases[j].name);
1475 printed++;
1476 }
1477 if (printed && pager_in_use())
1478 printf("\n");
1479out_free:
1480 for (j = 0; j < len; j++)
1481 zfree(&aliases[j].name);
1482 zfree(&aliases);
1483 return;
1484
1485out_enomem:
1486 printf("FATAL: not enough memory to print PMU events\n");
1487 if (aliases)
1488 goto out_free;
1489}
1490
1491bool pmu_have_event(const char *pname, const char *name)
1492{
1493 struct perf_pmu *pmu;
1494 struct perf_pmu_alias *alias;
1495
1496 pmu = NULL;
1497 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1498 if (strcmp(pname, pmu->name))
1499 continue;
1500 list_for_each_entry(alias, &pmu->aliases, list)
1501 if (!strcmp(alias->name, name))
1502 return true;
1503 }
1504 return false;
1505}
1506
1507static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
1508{
1509 struct stat st;
1510 char path[PATH_MAX];
1511 const char *sysfs;
1512
1513 sysfs = sysfs__mountpoint();
1514 if (!sysfs)
1515 return NULL;
1516
1517 snprintf(path, PATH_MAX,
1518 "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name);
1519
1520 if (stat(path, &st) < 0)
1521 return NULL;
1522
1523 return fopen(path, "r");
1524}
1525
1526int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
1527 ...)
1528{
1529 va_list args;
1530 FILE *file;
1531 int ret = EOF;
1532
1533 va_start(args, fmt);
1534 file = perf_pmu__open_file(pmu, name);
1535 if (file) {
1536 ret = vfscanf(file, fmt, args);
1537 fclose(file);
1538 }
1539 va_end(args);
1540 return ret;
1541}
1542