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