perf evsel: Allow const evsel for certain accesses
[platform/kernel/linux-starfive.git] / tools / perf / util / pmu.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/list.h>
3 #include <linux/compiler.h>
4 #include <linux/string.h>
5 #include <linux/zalloc.h>
6 #include <linux/ctype.h>
7 #include <subcmd/pager.h>
8 #include <sys/types.h>
9 #include <errno.h>
10 #include <fcntl.h>
11 #include <sys/stat.h>
12 #include <unistd.h>
13 #include <stdio.h>
14 #include <stdbool.h>
15 #include <stdarg.h>
16 #include <dirent.h>
17 #include <api/fs/fs.h>
18 #include <locale.h>
19 #include <regex.h>
20 #include <perf/cpumap.h>
21 #include <fnmatch.h>
22 #include <math.h>
23 #include "debug.h"
24 #include "evsel.h"
25 #include "pmu.h"
26 #include "pmus.h"
27 #include "parse-events.h"
28 #include "print-events.h"
29 #include "header.h"
30 #include "string2.h"
31 #include "strbuf.h"
32 #include "fncache.h"
33 #include "pmu-hybrid.h"
34
35 struct perf_pmu perf_pmu__fake;
36
37 /**
38  * struct perf_pmu_format - Values from a format file read from
39  * <sysfs>/devices/cpu/format/ held in struct perf_pmu.
40  *
41  * For example, the contents of <sysfs>/devices/cpu/format/event may be
42  * "config:0-7" and will be represented here as name="event",
43  * value=PERF_PMU_FORMAT_VALUE_CONFIG and bits 0 to 7 will be set.
44  */
45 struct perf_pmu_format {
46         /** @name: The modifier/file name. */
47         char *name;
48         /**
49          * @value : Which config value the format relates to. Supported values
50          * are from PERF_PMU_FORMAT_VALUE_CONFIG to
51          * PERF_PMU_FORMAT_VALUE_CONFIG_END.
52          */
53         int value;
54         /** @bits: Which config bits are set by this format value. */
55         DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
56         /** @list: Element on list within struct perf_pmu. */
57         struct list_head list;
58 };
59
60 int perf_pmu_parse(struct list_head *list, char *name);
61 extern FILE *perf_pmu_in;
62
63 static bool hybrid_scanned;
64
65 /*
66  * Parse & process all the sysfs attributes located under
67  * the directory specified in 'dir' parameter.
68  */
69 int perf_pmu__format_parse(char *dir, struct list_head *head)
70 {
71         struct dirent *evt_ent;
72         DIR *format_dir;
73         int ret = 0;
74
75         format_dir = opendir(dir);
76         if (!format_dir)
77                 return -EINVAL;
78
79         while (!ret && (evt_ent = readdir(format_dir))) {
80                 char path[PATH_MAX];
81                 char *name = evt_ent->d_name;
82                 FILE *file;
83
84                 if (!strcmp(name, ".") || !strcmp(name, ".."))
85                         continue;
86
87                 snprintf(path, PATH_MAX, "%s/%s", dir, name);
88
89                 ret = -EINVAL;
90                 file = fopen(path, "r");
91                 if (!file)
92                         break;
93
94                 perf_pmu_in = file;
95                 ret = perf_pmu_parse(head, name);
96                 fclose(file);
97         }
98
99         closedir(format_dir);
100         return ret;
101 }
102
103 /*
104  * Reading/parsing the default pmu format definition, which should be
105  * located at:
106  * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
107  */
108 static int pmu_format(const char *name, struct list_head *format)
109 {
110         char path[PATH_MAX];
111
112         if (!perf_pmu__pathname_scnprintf(path, sizeof(path), name, "format"))
113                 return -1;
114
115         if (!file_available(path))
116                 return 0;
117
118         if (perf_pmu__format_parse(path, format))
119                 return -1;
120
121         return 0;
122 }
123
124 int perf_pmu__convert_scale(const char *scale, char **end, double *sval)
125 {
126         char *lc;
127         int ret = 0;
128
129         /*
130          * save current locale
131          */
132         lc = setlocale(LC_NUMERIC, NULL);
133
134         /*
135          * The lc string may be allocated in static storage,
136          * so get a dynamic copy to make it survive setlocale
137          * call below.
138          */
139         lc = strdup(lc);
140         if (!lc) {
141                 ret = -ENOMEM;
142                 goto out;
143         }
144
145         /*
146          * force to C locale to ensure kernel
147          * scale string is converted correctly.
148          * kernel uses default C locale.
149          */
150         setlocale(LC_NUMERIC, "C");
151
152         *sval = strtod(scale, end);
153
154 out:
155         /* restore locale */
156         setlocale(LC_NUMERIC, lc);
157         free(lc);
158         return ret;
159 }
160
161 static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *name)
162 {
163         struct stat st;
164         ssize_t sret;
165         char scale[128];
166         int fd, ret = -1;
167         char path[PATH_MAX];
168
169         scnprintf(path, PATH_MAX, "%s/%s.scale", dir, name);
170
171         fd = open(path, O_RDONLY);
172         if (fd == -1)
173                 return -1;
174
175         if (fstat(fd, &st) < 0)
176                 goto error;
177
178         sret = read(fd, scale, sizeof(scale)-1);
179         if (sret < 0)
180                 goto error;
181
182         if (scale[sret - 1] == '\n')
183                 scale[sret - 1] = '\0';
184         else
185                 scale[sret] = '\0';
186
187         ret = perf_pmu__convert_scale(scale, NULL, &alias->scale);
188 error:
189         close(fd);
190         return ret;
191 }
192
193 static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *name)
194 {
195         char path[PATH_MAX];
196         ssize_t sret;
197         int fd;
198
199         scnprintf(path, PATH_MAX, "%s/%s.unit", dir, name);
200
201         fd = open(path, O_RDONLY);
202         if (fd == -1)
203                 return -1;
204
205         sret = read(fd, alias->unit, UNIT_MAX_LEN);
206         if (sret < 0)
207                 goto error;
208
209         close(fd);
210
211         if (alias->unit[sret - 1] == '\n')
212                 alias->unit[sret - 1] = '\0';
213         else
214                 alias->unit[sret] = '\0';
215
216         return 0;
217 error:
218         close(fd);
219         alias->unit[0] = '\0';
220         return -1;
221 }
222
223 static int
224 perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, char *dir, char *name)
225 {
226         char path[PATH_MAX];
227         int fd;
228
229         scnprintf(path, PATH_MAX, "%s/%s.per-pkg", dir, name);
230
231         fd = open(path, O_RDONLY);
232         if (fd == -1)
233                 return -1;
234
235         close(fd);
236
237         alias->per_pkg = true;
238         return 0;
239 }
240
241 static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias,
242                                     char *dir, char *name)
243 {
244         char path[PATH_MAX];
245         int fd;
246
247         scnprintf(path, PATH_MAX, "%s/%s.snapshot", dir, name);
248
249         fd = open(path, O_RDONLY);
250         if (fd == -1)
251                 return -1;
252
253         alias->snapshot = true;
254         close(fd);
255         return 0;
256 }
257
258 static void perf_pmu_assign_str(char *name, const char *field, char **old_str,
259                                 char **new_str)
260 {
261         if (!*old_str)
262                 goto set_new;
263
264         if (*new_str) { /* Have new string, check with old */
265                 if (strcasecmp(*old_str, *new_str))
266                         pr_debug("alias %s differs in field '%s'\n",
267                                  name, field);
268                 zfree(old_str);
269         } else          /* Nothing new --> keep old string */
270                 return;
271 set_new:
272         *old_str = *new_str;
273         *new_str = NULL;
274 }
275
276 static void perf_pmu_update_alias(struct perf_pmu_alias *old,
277                                   struct perf_pmu_alias *newalias)
278 {
279         perf_pmu_assign_str(old->name, "desc", &old->desc, &newalias->desc);
280         perf_pmu_assign_str(old->name, "long_desc", &old->long_desc,
281                             &newalias->long_desc);
282         perf_pmu_assign_str(old->name, "topic", &old->topic, &newalias->topic);
283         perf_pmu_assign_str(old->name, "value", &old->str, &newalias->str);
284         old->scale = newalias->scale;
285         old->per_pkg = newalias->per_pkg;
286         old->snapshot = newalias->snapshot;
287         memcpy(old->unit, newalias->unit, sizeof(old->unit));
288 }
289
290 /* Delete an alias entry. */
291 void perf_pmu_free_alias(struct perf_pmu_alias *newalias)
292 {
293         zfree(&newalias->name);
294         zfree(&newalias->desc);
295         zfree(&newalias->long_desc);
296         zfree(&newalias->topic);
297         zfree(&newalias->str);
298         zfree(&newalias->pmu_name);
299         parse_events_terms__purge(&newalias->terms);
300         free(newalias);
301 }
302
303 /* Merge an alias, search in alias list. If this name is already
304  * present merge both of them to combine all information.
305  */
306 static bool perf_pmu_merge_alias(struct perf_pmu_alias *newalias,
307                                  struct list_head *alist)
308 {
309         struct perf_pmu_alias *a;
310
311         list_for_each_entry(a, alist, list) {
312                 if (!strcasecmp(newalias->name, a->name)) {
313                         if (newalias->pmu_name && a->pmu_name &&
314                             !strcasecmp(newalias->pmu_name, a->pmu_name)) {
315                                 continue;
316                         }
317                         perf_pmu_update_alias(a, newalias);
318                         perf_pmu_free_alias(newalias);
319                         return true;
320                 }
321         }
322         return false;
323 }
324
325 static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name,
326                                  char *desc, char *val, const struct pmu_event *pe)
327 {
328         struct parse_events_term *term;
329         struct perf_pmu_alias *alias;
330         int ret;
331         char newval[256];
332         char *long_desc = NULL, *topic = NULL, *unit = NULL, *pmu_name = NULL;
333         bool deprecated = false, perpkg = false;
334
335         if (pe) {
336                 long_desc = (char *)pe->long_desc;
337                 topic = (char *)pe->topic;
338                 unit = (char *)pe->unit;
339                 perpkg = pe->perpkg;
340                 deprecated = pe->deprecated;
341                 pmu_name = (char *)pe->pmu;
342         }
343
344         alias = malloc(sizeof(*alias));
345         if (!alias)
346                 return -ENOMEM;
347
348         INIT_LIST_HEAD(&alias->terms);
349         alias->scale = 1.0;
350         alias->unit[0] = '\0';
351         alias->per_pkg = perpkg;
352         alias->snapshot = false;
353         alias->deprecated = deprecated;
354
355         ret = parse_events_terms(&alias->terms, val);
356         if (ret) {
357                 pr_err("Cannot parse alias %s: %d\n", val, ret);
358                 free(alias);
359                 return ret;
360         }
361
362         /* Scan event and remove leading zeroes, spaces, newlines, some
363          * platforms have terms specified as
364          * event=0x0091 (read from files ../<PMU>/events/<FILE>
365          * and terms specified as event=0x91 (read from JSON files).
366          *
367          * Rebuild string to make alias->str member comparable.
368          */
369         memset(newval, 0, sizeof(newval));
370         ret = 0;
371         list_for_each_entry(term, &alias->terms, list) {
372                 if (ret)
373                         ret += scnprintf(newval + ret, sizeof(newval) - ret,
374                                          ",");
375                 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM)
376                         ret += scnprintf(newval + ret, sizeof(newval) - ret,
377                                          "%s=%#x", term->config, term->val.num);
378                 else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
379                         ret += scnprintf(newval + ret, sizeof(newval) - ret,
380                                          "%s=%s", term->config, term->val.str);
381         }
382
383         alias->name = strdup(name);
384         if (dir) {
385                 /*
386                  * load unit name and scale if available
387                  */
388                 perf_pmu__parse_unit(alias, dir, name);
389                 perf_pmu__parse_scale(alias, dir, name);
390                 perf_pmu__parse_per_pkg(alias, dir, name);
391                 perf_pmu__parse_snapshot(alias, dir, name);
392         }
393
394         alias->desc = desc ? strdup(desc) : NULL;
395         alias->long_desc = long_desc ? strdup(long_desc) :
396                                 desc ? strdup(desc) : NULL;
397         alias->topic = topic ? strdup(topic) : NULL;
398         if (unit) {
399                 if (perf_pmu__convert_scale(unit, &unit, &alias->scale) < 0)
400                         return -1;
401                 snprintf(alias->unit, sizeof(alias->unit), "%s", unit);
402         }
403         alias->str = strdup(newval);
404         alias->pmu_name = pmu_name ? strdup(pmu_name) : NULL;
405
406         if (!perf_pmu_merge_alias(alias, list))
407                 list_add_tail(&alias->list, list);
408
409         return 0;
410 }
411
412 static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file)
413 {
414         char buf[256];
415         int ret;
416
417         ret = fread(buf, 1, sizeof(buf), file);
418         if (ret == 0)
419                 return -EINVAL;
420
421         buf[ret] = 0;
422
423         /* Remove trailing newline from sysfs file */
424         strim(buf);
425
426         return __perf_pmu__new_alias(list, dir, name, NULL, buf, NULL);
427 }
428
429 static inline bool pmu_alias_info_file(char *name)
430 {
431         size_t len;
432
433         len = strlen(name);
434         if (len > 5 && !strcmp(name + len - 5, ".unit"))
435                 return true;
436         if (len > 6 && !strcmp(name + len - 6, ".scale"))
437                 return true;
438         if (len > 8 && !strcmp(name + len - 8, ".per-pkg"))
439                 return true;
440         if (len > 9 && !strcmp(name + len - 9, ".snapshot"))
441                 return true;
442
443         return false;
444 }
445
446 /*
447  * Process all the sysfs attributes located under the directory
448  * specified in 'dir' parameter.
449  */
450 static int pmu_aliases_parse(char *dir, struct list_head *head)
451 {
452         struct dirent *evt_ent;
453         DIR *event_dir;
454
455         event_dir = opendir(dir);
456         if (!event_dir)
457                 return -EINVAL;
458
459         while ((evt_ent = readdir(event_dir))) {
460                 char path[PATH_MAX];
461                 char *name = evt_ent->d_name;
462                 FILE *file;
463
464                 if (!strcmp(name, ".") || !strcmp(name, ".."))
465                         continue;
466
467                 /*
468                  * skip info files parsed in perf_pmu__new_alias()
469                  */
470                 if (pmu_alias_info_file(name))
471                         continue;
472
473                 scnprintf(path, PATH_MAX, "%s/%s", dir, name);
474
475                 file = fopen(path, "r");
476                 if (!file) {
477                         pr_debug("Cannot open %s\n", path);
478                         continue;
479                 }
480
481                 if (perf_pmu__new_alias(head, dir, name, file) < 0)
482                         pr_debug("Cannot set up %s\n", name);
483                 fclose(file);
484         }
485
486         closedir(event_dir);
487         return 0;
488 }
489
490 /*
491  * Reading the pmu event aliases definition, which should be located at:
492  * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
493  */
494 static int pmu_aliases(const char *name, struct list_head *head)
495 {
496         char path[PATH_MAX];
497
498         if (!perf_pmu__pathname_scnprintf(path, sizeof(path), name, "events"))
499                 return -1;
500
501         if (!file_available(path))
502                 return 0;
503
504         if (pmu_aliases_parse(path, head))
505                 return -1;
506
507         return 0;
508 }
509
510 static int pmu_alias_terms(struct perf_pmu_alias *alias,
511                            struct list_head *terms)
512 {
513         struct parse_events_term *term, *cloned;
514         LIST_HEAD(list);
515         int ret;
516
517         list_for_each_entry(term, &alias->terms, list) {
518                 ret = parse_events_term__clone(&cloned, term);
519                 if (ret) {
520                         parse_events_terms__purge(&list);
521                         return ret;
522                 }
523                 /*
524                  * Weak terms don't override command line options,
525                  * which we don't want for implicit terms in aliases.
526                  */
527                 cloned->weak = true;
528                 list_add_tail(&cloned->list, &list);
529         }
530         list_splice(&list, terms);
531         return 0;
532 }
533
534 /* Add all pmus in sysfs to pmu list: */
535 static void pmu_read_sysfs(void)
536 {
537         char path[PATH_MAX];
538         DIR *dir;
539         struct dirent *dent;
540
541         if (!perf_pmu__event_source_devices_scnprintf(path, sizeof(path)))
542                 return;
543
544         dir = opendir(path);
545         if (!dir)
546                 return;
547
548         while ((dent = readdir(dir))) {
549                 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
550                         continue;
551                 /* add to static LIST_HEAD(pmus): */
552                 perf_pmu__find(dent->d_name);
553         }
554
555         closedir(dir);
556 }
557
558 /*
559  * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64)
560  * may have a "cpus" file.
561  */
562 static struct perf_cpu_map *pmu_cpumask(const char *name)
563 {
564         struct perf_cpu_map *cpus;
565         const char *templates[] = {
566                 "cpumask",
567                 "cpus",
568                 NULL
569         };
570         const char **template;
571         char pmu_name[PATH_MAX];
572         struct perf_pmu pmu = {.name = pmu_name};
573         FILE *file;
574
575         strlcpy(pmu_name, name, sizeof(pmu_name));
576         for (template = templates; *template; template++) {
577                 file = perf_pmu__open_file(&pmu, *template);
578                 if (!file)
579                         continue;
580                 cpus = perf_cpu_map__read(file);
581                 if (cpus)
582                         return cpus;
583         }
584
585         return NULL;
586 }
587
588 static bool pmu_is_uncore(const char *name)
589 {
590         char path[PATH_MAX];
591
592         if (perf_pmu__hybrid_mounted(name))
593                 return false;
594
595         perf_pmu__pathname_scnprintf(path, sizeof(path), name, "cpumask");
596         return file_available(path);
597 }
598
599 static char *pmu_id(const char *name)
600 {
601         char path[PATH_MAX], *str;
602         size_t len;
603
604         perf_pmu__pathname_scnprintf(path, sizeof(path), name, "identifier");
605
606         if (filename__read_str(path, &str, &len) < 0)
607                 return NULL;
608
609         str[len - 1] = 0; /* remove line feed */
610
611         return str;
612 }
613
614 /*
615  *  PMU CORE devices have different name other than cpu in sysfs on some
616  *  platforms.
617  *  Looking for possible sysfs files to identify the arm core device.
618  */
619 static int is_arm_pmu_core(const char *name)
620 {
621         char path[PATH_MAX];
622
623         if (!perf_pmu__pathname_scnprintf(path, sizeof(path), name, "cpus"))
624                 return 0;
625         return file_available(path);
626 }
627
628 char *perf_pmu__getcpuid(struct perf_pmu *pmu)
629 {
630         char *cpuid;
631         static bool printed;
632
633         cpuid = getenv("PERF_CPUID");
634         if (cpuid)
635                 cpuid = strdup(cpuid);
636         if (!cpuid)
637                 cpuid = get_cpuid_str(pmu);
638         if (!cpuid)
639                 return NULL;
640
641         if (!printed) {
642                 pr_debug("Using CPUID %s\n", cpuid);
643                 printed = true;
644         }
645         return cpuid;
646 }
647
648 __weak const struct pmu_events_table *pmu_events_table__find(void)
649 {
650         return perf_pmu__find_events_table(NULL);
651 }
652
653 __weak const struct pmu_metrics_table *pmu_metrics_table__find(void)
654 {
655         return perf_pmu__find_metrics_table(NULL);
656 }
657
658 /*
659  * Suffix must be in form tok_{digits}, or tok{digits}, or same as pmu_name
660  * to be valid.
661  */
662 static bool perf_pmu__valid_suffix(const char *pmu_name, char *tok)
663 {
664         const char *p;
665
666         if (strncmp(pmu_name, tok, strlen(tok)))
667                 return false;
668
669         p = pmu_name + strlen(tok);
670         if (*p == 0)
671                 return true;
672
673         if (*p == '_')
674                 ++p;
675
676         /* Ensure we end in a number */
677         while (1) {
678                 if (!isdigit(*p))
679                         return false;
680                 if (*(++p) == 0)
681                         break;
682         }
683
684         return true;
685 }
686
687 bool pmu_uncore_alias_match(const char *pmu_name, const char *name)
688 {
689         char *tmp = NULL, *tok, *str;
690         bool res;
691
692         str = strdup(pmu_name);
693         if (!str)
694                 return false;
695
696         /*
697          * uncore alias may be from different PMU with common prefix
698          */
699         tok = strtok_r(str, ",", &tmp);
700         if (strncmp(pmu_name, tok, strlen(tok))) {
701                 res = false;
702                 goto out;
703         }
704
705         /*
706          * Match more complex aliases where the alias name is a comma-delimited
707          * list of tokens, orderly contained in the matching PMU name.
708          *
709          * Example: For alias "socket,pmuname" and PMU "socketX_pmunameY", we
710          *          match "socket" in "socketX_pmunameY" and then "pmuname" in
711          *          "pmunameY".
712          */
713         while (1) {
714                 char *next_tok = strtok_r(NULL, ",", &tmp);
715
716                 name = strstr(name, tok);
717                 if (!name ||
718                     (!next_tok && !perf_pmu__valid_suffix(name, tok))) {
719                         res = false;
720                         goto out;
721                 }
722                 if (!next_tok)
723                         break;
724                 tok = next_tok;
725                 name += strlen(tok);
726         }
727
728         res = true;
729 out:
730         free(str);
731         return res;
732 }
733
734 struct pmu_add_cpu_aliases_map_data {
735         struct list_head *head;
736         const char *name;
737         const char *cpu_name;
738         struct perf_pmu *pmu;
739 };
740
741 static int pmu_add_cpu_aliases_map_callback(const struct pmu_event *pe,
742                                         const struct pmu_events_table *table __maybe_unused,
743                                         void *vdata)
744 {
745         struct pmu_add_cpu_aliases_map_data *data = vdata;
746         const char *pname = pe->pmu ? pe->pmu : data->cpu_name;
747
748         if (data->pmu->is_uncore && pmu_uncore_alias_match(pname, data->name))
749                 goto new_alias;
750
751         if (strcmp(pname, data->name))
752                 return 0;
753
754 new_alias:
755         /* need type casts to override 'const' */
756         __perf_pmu__new_alias(data->head, NULL, (char *)pe->name, (char *)pe->desc,
757                               (char *)pe->event, pe);
758         return 0;
759 }
760
761 /*
762  * From the pmu_events_map, find the table of PMU events that corresponds
763  * to the current running CPU. Then, add all PMU events from that table
764  * as aliases.
765  */
766 void pmu_add_cpu_aliases_table(struct list_head *head, struct perf_pmu *pmu,
767                                const struct pmu_events_table *table)
768 {
769         struct pmu_add_cpu_aliases_map_data data = {
770                 .head = head,
771                 .name = pmu->name,
772                 .cpu_name = is_arm_pmu_core(pmu->name) ? pmu->name : "cpu",
773                 .pmu = pmu,
774         };
775
776         pmu_events_table_for_each_event(table, pmu_add_cpu_aliases_map_callback, &data);
777 }
778
779 static void pmu_add_cpu_aliases(struct list_head *head, struct perf_pmu *pmu)
780 {
781         const struct pmu_events_table *table;
782
783         table = perf_pmu__find_events_table(pmu);
784         if (!table)
785                 return;
786
787         pmu_add_cpu_aliases_table(head, pmu, table);
788 }
789
790 struct pmu_sys_event_iter_data {
791         struct list_head *head;
792         struct perf_pmu *pmu;
793 };
794
795 static int pmu_add_sys_aliases_iter_fn(const struct pmu_event *pe,
796                                        const struct pmu_events_table *table __maybe_unused,
797                                        void *data)
798 {
799         struct pmu_sys_event_iter_data *idata = data;
800         struct perf_pmu *pmu = idata->pmu;
801
802         if (!pe->compat || !pe->pmu)
803                 return 0;
804
805         if (!strcmp(pmu->id, pe->compat) &&
806             pmu_uncore_alias_match(pe->pmu, pmu->name)) {
807                 __perf_pmu__new_alias(idata->head, NULL,
808                                       (char *)pe->name,
809                                       (char *)pe->desc,
810                                       (char *)pe->event,
811                                       pe);
812         }
813
814         return 0;
815 }
816
817 void pmu_add_sys_aliases(struct list_head *head, struct perf_pmu *pmu)
818 {
819         struct pmu_sys_event_iter_data idata = {
820                 .head = head,
821                 .pmu = pmu,
822         };
823
824         if (!pmu->id)
825                 return;
826
827         pmu_for_each_sys_event(pmu_add_sys_aliases_iter_fn, &idata);
828 }
829
830 struct perf_event_attr * __weak
831 perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
832 {
833         return NULL;
834 }
835
836 char * __weak
837 pmu_find_real_name(const char *name)
838 {
839         return (char *)name;
840 }
841
842 char * __weak
843 pmu_find_alias_name(const char *name __maybe_unused)
844 {
845         return NULL;
846 }
847
848 static int pmu_max_precise(struct perf_pmu *pmu)
849 {
850         int max_precise = -1;
851
852         perf_pmu__scan_file(pmu, "caps/max_precise", "%d", &max_precise);
853         return max_precise;
854 }
855
856 static struct perf_pmu *pmu_lookup(const char *lookup_name)
857 {
858         struct perf_pmu *pmu;
859         LIST_HEAD(format);
860         LIST_HEAD(aliases);
861         __u32 type;
862         char *name = pmu_find_real_name(lookup_name);
863         bool is_hybrid = perf_pmu__hybrid_mounted(name);
864         char *alias_name;
865
866         /*
867          * Check pmu name for hybrid and the pmu may be invalid in sysfs
868          */
869         if (!strncmp(name, "cpu_", 4) && !is_hybrid)
870                 return NULL;
871
872         /*
873          * The pmu data we store & need consists of the pmu
874          * type value and format definitions. Load both right
875          * now.
876          */
877         if (pmu_format(name, &format))
878                 return NULL;
879
880         /*
881          * Check the aliases first to avoid unnecessary work.
882          */
883         if (pmu_aliases(name, &aliases))
884                 return NULL;
885
886         pmu = zalloc(sizeof(*pmu));
887         if (!pmu)
888                 return NULL;
889
890         pmu->cpus = pmu_cpumask(name);
891         pmu->name = strdup(name);
892
893         if (!pmu->name)
894                 goto err;
895
896         /* Read type, and ensure that type value is successfully assigned (return 1) */
897         if (perf_pmu__scan_file(pmu, "type", "%u", &type) != 1)
898                 goto err;
899
900         alias_name = pmu_find_alias_name(name);
901         if (alias_name) {
902                 pmu->alias_name = strdup(alias_name);
903                 if (!pmu->alias_name)
904                         goto err;
905         }
906
907         pmu->type = type;
908         pmu->is_uncore = pmu_is_uncore(name);
909         if (pmu->is_uncore)
910                 pmu->id = pmu_id(name);
911         pmu->max_precise = pmu_max_precise(pmu);
912         pmu_add_cpu_aliases(&aliases, pmu);
913         pmu_add_sys_aliases(&aliases, pmu);
914
915         INIT_LIST_HEAD(&pmu->format);
916         INIT_LIST_HEAD(&pmu->aliases);
917         INIT_LIST_HEAD(&pmu->caps);
918         list_splice(&format, &pmu->format);
919         list_splice(&aliases, &pmu->aliases);
920         list_add_tail(&pmu->list, &pmus);
921
922         if (is_hybrid)
923                 list_add_tail(&pmu->hybrid_list, &perf_pmu__hybrid_pmus);
924
925         pmu->default_config = perf_pmu__get_default_config(pmu);
926
927         return pmu;
928 err:
929         if (pmu->name)
930                 free(pmu->name);
931         free(pmu);
932         return NULL;
933 }
934
935 void perf_pmu__warn_invalid_formats(struct perf_pmu *pmu)
936 {
937         struct perf_pmu_format *format;
938
939         /* fake pmu doesn't have format list */
940         if (pmu == &perf_pmu__fake)
941                 return;
942
943         list_for_each_entry(format, &pmu->format, list)
944                 if (format->value >= PERF_PMU_FORMAT_VALUE_CONFIG_END) {
945                         pr_warning("WARNING: '%s' format '%s' requires 'perf_event_attr::config%d'"
946                                    "which is not supported by this version of perf!\n",
947                                    pmu->name, format->name, format->value);
948                         return;
949                 }
950 }
951
952 static struct perf_pmu *pmu_find(const char *name)
953 {
954         struct perf_pmu *pmu;
955
956         list_for_each_entry(pmu, &pmus, list) {
957                 if (!strcmp(pmu->name, name) ||
958                     (pmu->alias_name && !strcmp(pmu->alias_name, name)))
959                         return pmu;
960         }
961
962         return NULL;
963 }
964
965 struct perf_pmu *perf_pmu__find_by_type(unsigned int type)
966 {
967         struct perf_pmu *pmu;
968
969         list_for_each_entry(pmu, &pmus, list)
970                 if (pmu->type == type)
971                         return pmu;
972
973         return NULL;
974 }
975
976 struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
977 {
978         /*
979          * pmu iterator: If pmu is NULL, we start at the begin,
980          * otherwise return the next pmu. Returns NULL on end.
981          */
982         if (!pmu) {
983                 pmu_read_sysfs();
984                 pmu = list_prepare_entry(pmu, &pmus, list);
985         }
986         list_for_each_entry_continue(pmu, &pmus, list)
987                 return pmu;
988         return NULL;
989 }
990
991 struct perf_pmu *evsel__find_pmu(const struct evsel *evsel)
992 {
993         struct perf_pmu *pmu = NULL;
994
995         if (evsel->pmu)
996                 return evsel->pmu;
997
998         while ((pmu = perf_pmu__scan(pmu)) != NULL) {
999                 if (pmu->type == evsel->core.attr.type)
1000                         break;
1001         }
1002
1003         ((struct evsel *)evsel)->pmu = pmu;
1004         return pmu;
1005 }
1006
1007 bool evsel__is_aux_event(const struct evsel *evsel)
1008 {
1009         struct perf_pmu *pmu = evsel__find_pmu(evsel);
1010
1011         return pmu && pmu->auxtrace;
1012 }
1013
1014 struct perf_pmu *perf_pmu__find(const char *name)
1015 {
1016         struct perf_pmu *pmu;
1017
1018         /*
1019          * Once PMU is loaded it stays in the list,
1020          * so we keep us from multiple reading/parsing
1021          * the pmu format definitions.
1022          */
1023         pmu = pmu_find(name);
1024         if (pmu)
1025                 return pmu;
1026
1027         return pmu_lookup(name);
1028 }
1029
1030 static struct perf_pmu_format *
1031 pmu_find_format(struct list_head *formats, const char *name)
1032 {
1033         struct perf_pmu_format *format;
1034
1035         list_for_each_entry(format, formats, list)
1036                 if (!strcmp(format->name, name))
1037                         return format;
1038
1039         return NULL;
1040 }
1041
1042 __u64 perf_pmu__format_bits(struct list_head *formats, const char *name)
1043 {
1044         struct perf_pmu_format *format = pmu_find_format(formats, name);
1045         __u64 bits = 0;
1046         int fbit;
1047
1048         if (!format)
1049                 return 0;
1050
1051         for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
1052                 bits |= 1ULL << fbit;
1053
1054         return bits;
1055 }
1056
1057 int perf_pmu__format_type(struct list_head *formats, const char *name)
1058 {
1059         struct perf_pmu_format *format = pmu_find_format(formats, name);
1060
1061         if (!format)
1062                 return -1;
1063
1064         return format->value;
1065 }
1066
1067 /*
1068  * Sets value based on the format definition (format parameter)
1069  * and unformatted value (value parameter).
1070  */
1071 static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
1072                              bool zero)
1073 {
1074         unsigned long fbit, vbit;
1075
1076         for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
1077
1078                 if (!test_bit(fbit, format))
1079                         continue;
1080
1081                 if (value & (1llu << vbit++))
1082                         *v |= (1llu << fbit);
1083                 else if (zero)
1084                         *v &= ~(1llu << fbit);
1085         }
1086 }
1087
1088 static __u64 pmu_format_max_value(const unsigned long *format)
1089 {
1090         int w;
1091
1092         w = bitmap_weight(format, PERF_PMU_FORMAT_BITS);
1093         if (!w)
1094                 return 0;
1095         if (w < 64)
1096                 return (1ULL << w) - 1;
1097         return -1;
1098 }
1099
1100 /*
1101  * Term is a string term, and might be a param-term. Try to look up it's value
1102  * in the remaining terms.
1103  * - We have a term like "base-or-format-term=param-term",
1104  * - We need to find the value supplied for "param-term" (with param-term named
1105  *   in a config string) later on in the term list.
1106  */
1107 static int pmu_resolve_param_term(struct parse_events_term *term,
1108                                   struct list_head *head_terms,
1109                                   __u64 *value)
1110 {
1111         struct parse_events_term *t;
1112
1113         list_for_each_entry(t, head_terms, list) {
1114                 if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM &&
1115                     t->config && !strcmp(t->config, term->config)) {
1116                         t->used = true;
1117                         *value = t->val.num;
1118                         return 0;
1119                 }
1120         }
1121
1122         if (verbose > 0)
1123                 printf("Required parameter '%s' not specified\n", term->config);
1124
1125         return -1;
1126 }
1127
1128 static char *pmu_formats_string(struct list_head *formats)
1129 {
1130         struct perf_pmu_format *format;
1131         char *str = NULL;
1132         struct strbuf buf = STRBUF_INIT;
1133         unsigned int i = 0;
1134
1135         if (!formats)
1136                 return NULL;
1137
1138         /* sysfs exported terms */
1139         list_for_each_entry(format, formats, list)
1140                 if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
1141                         goto error;
1142
1143         str = strbuf_detach(&buf, NULL);
1144 error:
1145         strbuf_release(&buf);
1146
1147         return str;
1148 }
1149
1150 /*
1151  * Setup one of config[12] attr members based on the
1152  * user input data - term parameter.
1153  */
1154 static int pmu_config_term(const char *pmu_name,
1155                            struct list_head *formats,
1156                            struct perf_event_attr *attr,
1157                            struct parse_events_term *term,
1158                            struct list_head *head_terms,
1159                            bool zero, struct parse_events_error *err)
1160 {
1161         struct perf_pmu_format *format;
1162         __u64 *vp;
1163         __u64 val, max_val;
1164
1165         /*
1166          * If this is a parameter we've already used for parameterized-eval,
1167          * skip it in normal eval.
1168          */
1169         if (term->used)
1170                 return 0;
1171
1172         /*
1173          * Hardcoded terms should be already in, so nothing
1174          * to be done for them.
1175          */
1176         if (parse_events__is_hardcoded_term(term))
1177                 return 0;
1178
1179         format = pmu_find_format(formats, term->config);
1180         if (!format) {
1181                 char *pmu_term = pmu_formats_string(formats);
1182                 char *unknown_term;
1183                 char *help_msg;
1184
1185                 if (asprintf(&unknown_term,
1186                                 "unknown term '%s' for pmu '%s'",
1187                                 term->config, pmu_name) < 0)
1188                         unknown_term = NULL;
1189                 help_msg = parse_events_formats_error_string(pmu_term);
1190                 if (err) {
1191                         parse_events_error__handle(err, term->err_term,
1192                                                    unknown_term,
1193                                                    help_msg);
1194                 } else {
1195                         pr_debug("%s (%s)\n", unknown_term, help_msg);
1196                         free(unknown_term);
1197                 }
1198                 free(pmu_term);
1199                 return -EINVAL;
1200         }
1201
1202         switch (format->value) {
1203         case PERF_PMU_FORMAT_VALUE_CONFIG:
1204                 vp = &attr->config;
1205                 break;
1206         case PERF_PMU_FORMAT_VALUE_CONFIG1:
1207                 vp = &attr->config1;
1208                 break;
1209         case PERF_PMU_FORMAT_VALUE_CONFIG2:
1210                 vp = &attr->config2;
1211                 break;
1212         default:
1213                 return -EINVAL;
1214         }
1215
1216         /*
1217          * Either directly use a numeric term, or try to translate string terms
1218          * using event parameters.
1219          */
1220         if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1221                 if (term->no_value &&
1222                     bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) {
1223                         if (err) {
1224                                 parse_events_error__handle(err, term->err_val,
1225                                            strdup("no value assigned for term"),
1226                                            NULL);
1227                         }
1228                         return -EINVAL;
1229                 }
1230
1231                 val = term->val.num;
1232         } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1233                 if (strcmp(term->val.str, "?")) {
1234                         if (verbose > 0) {
1235                                 pr_info("Invalid sysfs entry %s=%s\n",
1236                                                 term->config, term->val.str);
1237                         }
1238                         if (err) {
1239                                 parse_events_error__handle(err, term->err_val,
1240                                         strdup("expected numeric value"),
1241                                         NULL);
1242                         }
1243                         return -EINVAL;
1244                 }
1245
1246                 if (pmu_resolve_param_term(term, head_terms, &val))
1247                         return -EINVAL;
1248         } else
1249                 return -EINVAL;
1250
1251         max_val = pmu_format_max_value(format->bits);
1252         if (val > max_val) {
1253                 if (err) {
1254                         char *err_str;
1255
1256                         parse_events_error__handle(err, term->err_val,
1257                                 asprintf(&err_str,
1258                                     "value too big for format, maximum is %llu",
1259                                     (unsigned long long)max_val) < 0
1260                                     ? strdup("value too big for format")
1261                                     : err_str,
1262                                     NULL);
1263                         return -EINVAL;
1264                 }
1265                 /*
1266                  * Assume we don't care if !err, in which case the value will be
1267                  * silently truncated.
1268                  */
1269         }
1270
1271         pmu_format_value(format->bits, val, vp, zero);
1272         return 0;
1273 }
1274
1275 int perf_pmu__config_terms(const char *pmu_name, struct list_head *formats,
1276                            struct perf_event_attr *attr,
1277                            struct list_head *head_terms,
1278                            bool zero, struct parse_events_error *err)
1279 {
1280         struct parse_events_term *term;
1281
1282         list_for_each_entry(term, head_terms, list) {
1283                 if (pmu_config_term(pmu_name, formats, attr, term, head_terms,
1284                                     zero, err))
1285                         return -EINVAL;
1286         }
1287
1288         return 0;
1289 }
1290
1291 /*
1292  * Configures event's 'attr' parameter based on the:
1293  * 1) users input - specified in terms parameter
1294  * 2) pmu format definitions - specified by pmu parameter
1295  */
1296 int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
1297                      struct list_head *head_terms,
1298                      struct parse_events_error *err)
1299 {
1300         bool zero = !!pmu->default_config;
1301
1302         attr->type = pmu->type;
1303         return perf_pmu__config_terms(pmu->name, &pmu->format, attr,
1304                                       head_terms, zero, err);
1305 }
1306
1307 static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
1308                                              struct parse_events_term *term)
1309 {
1310         struct perf_pmu_alias *alias;
1311         char *name;
1312
1313         if (parse_events__is_hardcoded_term(term))
1314                 return NULL;
1315
1316         if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1317                 if (term->val.num != 1)
1318                         return NULL;
1319                 if (pmu_find_format(&pmu->format, term->config))
1320                         return NULL;
1321                 name = term->config;
1322         } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1323                 if (strcasecmp(term->config, "event"))
1324                         return NULL;
1325                 name = term->val.str;
1326         } else {
1327                 return NULL;
1328         }
1329
1330         list_for_each_entry(alias, &pmu->aliases, list) {
1331                 if (!strcasecmp(alias->name, name))
1332                         return alias;
1333         }
1334         return NULL;
1335 }
1336
1337
1338 static int check_info_data(struct perf_pmu_alias *alias,
1339                            struct perf_pmu_info *info)
1340 {
1341         /*
1342          * Only one term in event definition can
1343          * define unit, scale and snapshot, fail
1344          * if there's more than one.
1345          */
1346         if ((info->unit && alias->unit[0]) ||
1347             (info->scale && alias->scale) ||
1348             (info->snapshot && alias->snapshot))
1349                 return -EINVAL;
1350
1351         if (alias->unit[0])
1352                 info->unit = alias->unit;
1353
1354         if (alias->scale)
1355                 info->scale = alias->scale;
1356
1357         if (alias->snapshot)
1358                 info->snapshot = alias->snapshot;
1359
1360         return 0;
1361 }
1362
1363 /*
1364  * Find alias in the terms list and replace it with the terms
1365  * defined for the alias
1366  */
1367 int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
1368                           struct perf_pmu_info *info)
1369 {
1370         struct parse_events_term *term, *h;
1371         struct perf_pmu_alias *alias;
1372         int ret;
1373
1374         info->per_pkg = false;
1375
1376         /*
1377          * Mark unit and scale as not set
1378          * (different from default values, see below)
1379          */
1380         info->unit     = NULL;
1381         info->scale    = 0.0;
1382         info->snapshot = false;
1383
1384         list_for_each_entry_safe(term, h, head_terms, list) {
1385                 alias = pmu_find_alias(pmu, term);
1386                 if (!alias)
1387                         continue;
1388                 ret = pmu_alias_terms(alias, &term->list);
1389                 if (ret)
1390                         return ret;
1391
1392                 ret = check_info_data(alias, info);
1393                 if (ret)
1394                         return ret;
1395
1396                 if (alias->per_pkg)
1397                         info->per_pkg = true;
1398
1399                 list_del_init(&term->list);
1400                 parse_events_term__delete(term);
1401         }
1402
1403         /*
1404          * if no unit or scale found in aliases, then
1405          * set defaults as for evsel
1406          * unit cannot left to NULL
1407          */
1408         if (info->unit == NULL)
1409                 info->unit   = "";
1410
1411         if (info->scale == 0.0)
1412                 info->scale  = 1.0;
1413
1414         return 0;
1415 }
1416
1417 int perf_pmu__new_format(struct list_head *list, char *name,
1418                          int config, unsigned long *bits)
1419 {
1420         struct perf_pmu_format *format;
1421
1422         format = zalloc(sizeof(*format));
1423         if (!format)
1424                 return -ENOMEM;
1425
1426         format->name = strdup(name);
1427         format->value = config;
1428         memcpy(format->bits, bits, sizeof(format->bits));
1429
1430         list_add_tail(&format->list, list);
1431         return 0;
1432 }
1433
1434 void perf_pmu__set_format(unsigned long *bits, long from, long to)
1435 {
1436         long b;
1437
1438         if (!to)
1439                 to = from;
1440
1441         memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
1442         for (b = from; b <= to; b++)
1443                 __set_bit(b, bits);
1444 }
1445
1446 void perf_pmu__del_formats(struct list_head *formats)
1447 {
1448         struct perf_pmu_format *fmt, *tmp;
1449
1450         list_for_each_entry_safe(fmt, tmp, formats, list) {
1451                 list_del(&fmt->list);
1452                 free(fmt->name);
1453                 free(fmt);
1454         }
1455 }
1456
1457 static int sub_non_neg(int a, int b)
1458 {
1459         if (b > a)
1460                 return 0;
1461         return a - b;
1462 }
1463
1464 static char *format_alias(char *buf, int len, const struct perf_pmu *pmu,
1465                           const struct perf_pmu_alias *alias)
1466 {
1467         struct parse_events_term *term;
1468         int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name);
1469
1470         list_for_each_entry(term, &alias->terms, list) {
1471                 if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
1472                         used += snprintf(buf + used, sub_non_neg(len, used),
1473                                         ",%s=%s", term->config,
1474                                         term->val.str);
1475         }
1476
1477         if (sub_non_neg(len, used) > 0) {
1478                 buf[used] = '/';
1479                 used++;
1480         }
1481         if (sub_non_neg(len, used) > 0) {
1482                 buf[used] = '\0';
1483                 used++;
1484         } else
1485                 buf[len - 1] = '\0';
1486
1487         return buf;
1488 }
1489
1490 /** Struct for ordering events as output in perf list. */
1491 struct sevent {
1492         /** PMU for event. */
1493         const struct perf_pmu *pmu;
1494         /**
1495          * Optional event for name, desc, etc. If not present then this is a
1496          * selectable PMU and the event name is shown as "//".
1497          */
1498         const struct perf_pmu_alias *event;
1499         /** Is the PMU for the CPU? */
1500         bool is_cpu;
1501 };
1502
1503 static int cmp_sevent(const void *a, const void *b)
1504 {
1505         const struct sevent *as = a;
1506         const struct sevent *bs = b;
1507         const char *a_pmu_name, *b_pmu_name;
1508         const char *a_name = "//", *a_desc = NULL, *a_topic = "";
1509         const char *b_name = "//", *b_desc = NULL, *b_topic = "";
1510         int ret;
1511
1512         if (as->event) {
1513                 a_name = as->event->name;
1514                 a_desc = as->event->desc;
1515                 a_topic = as->event->topic ?: "";
1516         }
1517         if (bs->event) {
1518                 b_name = bs->event->name;
1519                 b_desc = bs->event->desc;
1520                 b_topic = bs->event->topic ?: "";
1521         }
1522         /* Put extra events last. */
1523         if (!!a_desc != !!b_desc)
1524                 return !!a_desc - !!b_desc;
1525
1526         /* Order by topics. */
1527         ret = strcmp(a_topic, b_topic);
1528         if (ret)
1529                 return ret;
1530
1531         /* Order CPU core events to be first */
1532         if (as->is_cpu != bs->is_cpu)
1533                 return as->is_cpu ? -1 : 1;
1534
1535         /* Order by PMU name. */
1536         a_pmu_name = as->pmu->name ?: "";
1537         b_pmu_name = bs->pmu->name ?: "";
1538         ret = strcmp(a_pmu_name, b_pmu_name);
1539         if (ret)
1540                 return ret;
1541
1542         /* Order by event name. */
1543         return strcmp(a_name, b_name);
1544 }
1545
1546 bool is_pmu_core(const char *name)
1547 {
1548         return !strcmp(name, "cpu") || is_arm_pmu_core(name);
1549 }
1550
1551 static bool pmu_alias_is_duplicate(struct sevent *alias_a,
1552                                    struct sevent *alias_b)
1553 {
1554         const char *a_pmu_name, *b_pmu_name;
1555         const char *a_name = alias_a->event ? alias_a->event->name : "//";
1556         const char *b_name = alias_b->event ? alias_b->event->name : "//";
1557
1558         /* Different names -> never duplicates */
1559         if (strcmp(a_name, b_name))
1560                 return false;
1561
1562         /* Don't remove duplicates for different PMUs */
1563         a_pmu_name = alias_a->pmu->name ?: "";
1564         b_pmu_name = alias_b->pmu->name ?: "";
1565         return strcmp(a_pmu_name, b_pmu_name) == 0;
1566 }
1567
1568 void print_pmu_events(const struct print_callbacks *print_cb, void *print_state)
1569 {
1570         struct perf_pmu *pmu;
1571         struct perf_pmu_alias *event;
1572         char buf[1024];
1573         int printed = 0;
1574         int len, j;
1575         struct sevent *aliases;
1576
1577         pmu = NULL;
1578         len = 0;
1579         while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1580                 list_for_each_entry(event, &pmu->aliases, list)
1581                         len++;
1582                 if (pmu->selectable)
1583                         len++;
1584         }
1585         aliases = zalloc(sizeof(struct sevent) * len);
1586         if (!aliases) {
1587                 pr_err("FATAL: not enough memory to print PMU events\n");
1588                 return;
1589         }
1590         pmu = NULL;
1591         j = 0;
1592         while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1593                 bool is_cpu = is_pmu_core(pmu->name) || perf_pmu__is_hybrid(pmu->name);
1594
1595                 list_for_each_entry(event, &pmu->aliases, list) {
1596                         aliases[j].event = event;
1597                         aliases[j].pmu = pmu;
1598                         aliases[j].is_cpu = is_cpu;
1599                         j++;
1600                 }
1601                 if (pmu->selectable) {
1602                         aliases[j].event = NULL;
1603                         aliases[j].pmu = pmu;
1604                         aliases[j].is_cpu = is_cpu;
1605                         j++;
1606                 }
1607         }
1608         len = j;
1609         qsort(aliases, len, sizeof(struct sevent), cmp_sevent);
1610         for (j = 0; j < len; j++) {
1611                 const char *name, *alias = NULL, *scale_unit = NULL,
1612                         *desc = NULL, *long_desc = NULL,
1613                         *encoding_desc = NULL, *topic = NULL;
1614                 bool deprecated = false;
1615                 size_t buf_used;
1616
1617                 /* Skip duplicates */
1618                 if (j > 0 && pmu_alias_is_duplicate(&aliases[j], &aliases[j - 1]))
1619                         continue;
1620
1621                 if (!aliases[j].event) {
1622                         /* A selectable event. */
1623                         buf_used = snprintf(buf, sizeof(buf), "%s//", aliases[j].pmu->name) + 1;
1624                         name = buf;
1625                 } else {
1626                         if (aliases[j].event->desc) {
1627                                 name = aliases[j].event->name;
1628                                 buf_used = 0;
1629                         } else {
1630                                 name = format_alias(buf, sizeof(buf), aliases[j].pmu,
1631                                                     aliases[j].event);
1632                                 if (aliases[j].is_cpu) {
1633                                         alias = name;
1634                                         name = aliases[j].event->name;
1635                                 }
1636                                 buf_used = strlen(buf) + 1;
1637                         }
1638                         if (strlen(aliases[j].event->unit) || aliases[j].event->scale != 1.0) {
1639                                 scale_unit = buf + buf_used;
1640                                 buf_used += snprintf(buf + buf_used, sizeof(buf) - buf_used,
1641                                                 "%G%s", aliases[j].event->scale,
1642                                                 aliases[j].event->unit) + 1;
1643                         }
1644                         desc = aliases[j].event->desc;
1645                         long_desc = aliases[j].event->long_desc;
1646                         topic = aliases[j].event->topic;
1647                         encoding_desc = buf + buf_used;
1648                         buf_used += snprintf(buf + buf_used, sizeof(buf) - buf_used,
1649                                         "%s/%s/", aliases[j].pmu->name,
1650                                         aliases[j].event->str) + 1;
1651                         deprecated = aliases[j].event->deprecated;
1652                 }
1653                 print_cb->print_event(print_state,
1654                                 aliases[j].pmu->name,
1655                                 topic,
1656                                 name,
1657                                 alias,
1658                                 scale_unit,
1659                                 deprecated,
1660                                 "Kernel PMU event",
1661                                 desc,
1662                                 long_desc,
1663                                 encoding_desc);
1664         }
1665         if (printed && pager_in_use())
1666                 printf("\n");
1667
1668         zfree(&aliases);
1669         return;
1670 }
1671
1672 bool pmu_have_event(const char *pname, const char *name)
1673 {
1674         struct perf_pmu *pmu;
1675         struct perf_pmu_alias *alias;
1676
1677         pmu = NULL;
1678         while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1679                 if (strcmp(pname, pmu->name))
1680                         continue;
1681                 list_for_each_entry(alias, &pmu->aliases, list)
1682                         if (!strcmp(alias->name, name))
1683                                 return true;
1684         }
1685         return false;
1686 }
1687
1688 FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
1689 {
1690         char path[PATH_MAX];
1691
1692         if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, name) ||
1693             !file_available(path))
1694                 return NULL;
1695
1696         return fopen(path, "r");
1697 }
1698
1699 int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
1700                         ...)
1701 {
1702         va_list args;
1703         FILE *file;
1704         int ret = EOF;
1705
1706         va_start(args, fmt);
1707         file = perf_pmu__open_file(pmu, name);
1708         if (file) {
1709                 ret = vfscanf(file, fmt, args);
1710                 fclose(file);
1711         }
1712         va_end(args);
1713         return ret;
1714 }
1715
1716 bool perf_pmu__file_exists(struct perf_pmu *pmu, const char *name)
1717 {
1718         char path[PATH_MAX];
1719
1720         if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, name))
1721                 return false;
1722
1723         return file_available(path);
1724 }
1725
1726 static int perf_pmu__new_caps(struct list_head *list, char *name, char *value)
1727 {
1728         struct perf_pmu_caps *caps = zalloc(sizeof(*caps));
1729
1730         if (!caps)
1731                 return -ENOMEM;
1732
1733         caps->name = strdup(name);
1734         if (!caps->name)
1735                 goto free_caps;
1736         caps->value = strndup(value, strlen(value) - 1);
1737         if (!caps->value)
1738                 goto free_name;
1739         list_add_tail(&caps->list, list);
1740         return 0;
1741
1742 free_name:
1743         zfree(caps->name);
1744 free_caps:
1745         free(caps);
1746
1747         return -ENOMEM;
1748 }
1749
1750 /*
1751  * Reading/parsing the given pmu capabilities, which should be located at:
1752  * /sys/bus/event_source/devices/<dev>/caps as sysfs group attributes.
1753  * Return the number of capabilities
1754  */
1755 int perf_pmu__caps_parse(struct perf_pmu *pmu)
1756 {
1757         struct stat st;
1758         char caps_path[PATH_MAX];
1759         DIR *caps_dir;
1760         struct dirent *evt_ent;
1761
1762         if (pmu->caps_initialized)
1763                 return pmu->nr_caps;
1764
1765         pmu->nr_caps = 0;
1766
1767         if (!perf_pmu__pathname_scnprintf(caps_path, sizeof(caps_path), pmu->name, "caps"))
1768                 return -1;
1769
1770         if (stat(caps_path, &st) < 0) {
1771                 pmu->caps_initialized = true;
1772                 return 0;       /* no error if caps does not exist */
1773         }
1774
1775         caps_dir = opendir(caps_path);
1776         if (!caps_dir)
1777                 return -EINVAL;
1778
1779         while ((evt_ent = readdir(caps_dir)) != NULL) {
1780                 char path[PATH_MAX + NAME_MAX + 1];
1781                 char *name = evt_ent->d_name;
1782                 char value[128];
1783                 FILE *file;
1784
1785                 if (!strcmp(name, ".") || !strcmp(name, ".."))
1786                         continue;
1787
1788                 snprintf(path, sizeof(path), "%s/%s", caps_path, name);
1789
1790                 file = fopen(path, "r");
1791                 if (!file)
1792                         continue;
1793
1794                 if (!fgets(value, sizeof(value), file) ||
1795                     (perf_pmu__new_caps(&pmu->caps, name, value) < 0)) {
1796                         fclose(file);
1797                         continue;
1798                 }
1799
1800                 pmu->nr_caps++;
1801                 fclose(file);
1802         }
1803
1804         closedir(caps_dir);
1805
1806         pmu->caps_initialized = true;
1807         return pmu->nr_caps;
1808 }
1809
1810 void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config,
1811                                    const char *name)
1812 {
1813         struct perf_pmu_format *format;
1814         __u64 masks = 0, bits;
1815         char buf[100];
1816         unsigned int i;
1817
1818         list_for_each_entry(format, &pmu->format, list) {
1819                 if (format->value != PERF_PMU_FORMAT_VALUE_CONFIG)
1820                         continue;
1821
1822                 for_each_set_bit(i, format->bits, PERF_PMU_FORMAT_BITS)
1823                         masks |= 1ULL << i;
1824         }
1825
1826         /*
1827          * Kernel doesn't export any valid format bits.
1828          */
1829         if (masks == 0)
1830                 return;
1831
1832         bits = config & ~masks;
1833         if (bits == 0)
1834                 return;
1835
1836         bitmap_scnprintf((unsigned long *)&bits, sizeof(bits) * 8, buf, sizeof(buf));
1837
1838         pr_warning("WARNING: event '%s' not valid (bits %s of config "
1839                    "'%llx' not supported by kernel)!\n",
1840                    name ?: "N/A", buf, config);
1841 }
1842
1843 bool perf_pmu__has_hybrid(void)
1844 {
1845         if (!hybrid_scanned) {
1846                 hybrid_scanned = true;
1847                 perf_pmu__scan(NULL);
1848         }
1849
1850         return !list_empty(&perf_pmu__hybrid_pmus);
1851 }
1852
1853 int perf_pmu__match(char *pattern, char *name, char *tok)
1854 {
1855         if (!name)
1856                 return -1;
1857
1858         if (fnmatch(pattern, name, 0))
1859                 return -1;
1860
1861         if (tok && !perf_pmu__valid_suffix(name, tok))
1862                 return -1;
1863
1864         return 0;
1865 }
1866
1867 int perf_pmu__cpus_match(struct perf_pmu *pmu, struct perf_cpu_map *cpus,
1868                          struct perf_cpu_map **mcpus_ptr,
1869                          struct perf_cpu_map **ucpus_ptr)
1870 {
1871         struct perf_cpu_map *pmu_cpus = pmu->cpus;
1872         struct perf_cpu_map *matched_cpus, *unmatched_cpus;
1873         struct perf_cpu cpu;
1874         int i, matched_nr = 0, unmatched_nr = 0;
1875
1876         matched_cpus = perf_cpu_map__default_new();
1877         if (!matched_cpus)
1878                 return -1;
1879
1880         unmatched_cpus = perf_cpu_map__default_new();
1881         if (!unmatched_cpus) {
1882                 perf_cpu_map__put(matched_cpus);
1883                 return -1;
1884         }
1885
1886         perf_cpu_map__for_each_cpu(cpu, i, cpus) {
1887                 if (!perf_cpu_map__has(pmu_cpus, cpu))
1888                         unmatched_cpus->map[unmatched_nr++] = cpu;
1889                 else
1890                         matched_cpus->map[matched_nr++] = cpu;
1891         }
1892
1893         unmatched_cpus->nr = unmatched_nr;
1894         matched_cpus->nr = matched_nr;
1895         *mcpus_ptr = matched_cpus;
1896         *ucpus_ptr = unmatched_cpus;
1897         return 0;
1898 }
1899
1900 double __weak perf_pmu__cpu_slots_per_cycle(void)
1901 {
1902         return NAN;
1903 }
1904
1905 int perf_pmu__event_source_devices_scnprintf(char *pathname, size_t size)
1906 {
1907         const char *sysfs = sysfs__mountpoint();
1908
1909         if (!sysfs)
1910                 return 0;
1911         return scnprintf(pathname, size, "%s/bus/event_source/devices/", sysfs);
1912 }
1913
1914 /*
1915  * Fill 'buf' with the path to a file or folder in 'pmu_name' in
1916  * sysfs. For example if pmu_name = "cs_etm" and 'filename' = "format"
1917  * then pathname will be filled with
1918  * "/sys/bus/event_source/devices/cs_etm/format"
1919  *
1920  * Return 0 if the sysfs mountpoint couldn't be found or if no
1921  * characters were written.
1922  */
1923 int perf_pmu__pathname_scnprintf(char *buf, size_t size,
1924                                  const char *pmu_name, const char *filename)
1925 {
1926         char base_path[PATH_MAX];
1927
1928         if (!perf_pmu__event_source_devices_scnprintf(base_path, sizeof(base_path)))
1929                 return 0;
1930         return scnprintf(buf, size, "%s%s/%s", base_path, pmu_name, filename);
1931 }