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