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 <api/io.h>
16 #include <api/io_dir.h>
17 #include <locale.h>
18 #include <fnmatch.h>
19 #include <math.h>
20 #include "debug.h"
21 #include "evsel.h"
22 #include "pmu.h"
23 #include "drm_pmu.h"
24 #include "hwmon_pmu.h"
25 #include "pmus.h"
26 #include "tool_pmu.h"
27 #include "tp_pmu.h"
28 #include <util/pmu-bison.h>
29 #include <util/pmu-flex.h>
30 #include "parse-events.h"
31 #include "print-events.h"
32 #include "hashmap.h"
33 #include "header.h"
34 #include "string2.h"
35 #include "strbuf.h"
36 #include "fncache.h"
37 #include "util/evsel_config.h"
38 #include <regex.h>
39
40 #define UNIT_MAX_LEN 31 /* max length for event unit name */
41
42 enum event_source {
43 /* An event loaded from /sys/bus/event_source/devices/<pmu>/events. */
44 EVENT_SRC_SYSFS,
45 /* An event loaded from a CPUID matched json file. */
46 EVENT_SRC_CPU_JSON,
47 /*
48 * An event loaded from a /sys/bus/event_source/devices/<pmu>/identifier matched json
49 * file.
50 */
51 EVENT_SRC_SYS_JSON,
52 };
53
54 /**
55 * struct perf_pmu_alias - An event either read from sysfs or builtin in
56 * pmu-events.c, created by parsing the pmu-events json files.
57 */
58 struct perf_pmu_alias {
59 /** @name: Name of the event like "mem-loads". */
60 char *name;
61 /** @desc: Optional short description of the event. */
62 char *desc;
63 /** @long_desc: Optional long description. */
64 char *long_desc;
65 /**
66 * @topic: Optional topic such as cache or pipeline, particularly for
67 * json events.
68 */
69 char *topic;
70 /** @terms: Owned list of the original parsed parameters. */
71 struct parse_events_terms terms;
72 /**
73 * @pmu_name: The name copied from the json struct pmu_event. This can
74 * differ from the PMU name as it won't have suffixes.
75 */
76 char *pmu_name;
77 /** @unit: Units for the event, such as bytes or cache lines. */
78 char unit[UNIT_MAX_LEN+1];
79 /** @scale: Value to scale read counter values by. */
80 double scale;
81 /** @retirement_latency_mean: Value to be given for unsampled retirement latency mean. */
82 double retirement_latency_mean;
83 /** @retirement_latency_min: Value to be given for unsampled retirement latency min. */
84 double retirement_latency_min;
85 /** @retirement_latency_max: Value to be given for unsampled retirement latency max. */
86 double retirement_latency_max;
87 /**
88 * @per_pkg: Does the file
89 * <sysfs>/bus/event_source/devices/<pmu_name>/events/<name>.per-pkg or
90 * equivalent json value exist and have the value 1.
91 */
92 bool per_pkg;
93 /**
94 * @snapshot: Does the file
95 * <sysfs>/bus/event_source/devices/<pmu_name>/events/<name>.snapshot
96 * exist and have the value 1.
97 */
98 bool snapshot;
99 /**
100 * @deprecated: Is the event hidden and so not shown in perf list by
101 * default.
102 */
103 bool deprecated;
104 /** @from_sysfs: Was the alias from sysfs or a json event? */
105 bool from_sysfs;
106 /** @info_loaded: Have the scale, unit and other values been read from disk? */
107 bool info_loaded;
108 };
109
110 /**
111 * struct perf_pmu_format - Values from a format file read from
112 * <sysfs>/devices/cpu/format/ held in struct perf_pmu.
113 *
114 * For example, the contents of <sysfs>/devices/cpu/format/event may be
115 * "config:0-7" and will be represented here as name="event",
116 * value=PERF_PMU_FORMAT_VALUE_CONFIG and bits 0 to 7 will be set.
117 */
118 struct perf_pmu_format {
119 /** @list: Element on list within struct perf_pmu. */
120 struct list_head list;
121 /** @bits: Which config bits are set by this format value. */
122 DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
123 /** @name: The modifier/file name. */
124 char *name;
125 /**
126 * @value : Which config value the format relates to. Supported values
127 * are from PERF_PMU_FORMAT_VALUE_CONFIG to
128 * PERF_PMU_FORMAT_VALUE_CONFIG_END.
129 */
130 u16 value;
131 /** @loaded: Has the contents been loaded/parsed. */
132 bool loaded;
133 };
134
135 static int pmu_aliases_parse(struct perf_pmu *pmu);
136
perf_pmu__new_format(struct list_head * list,char * name)137 static struct perf_pmu_format *perf_pmu__new_format(struct list_head *list, char *name)
138 {
139 struct perf_pmu_format *format;
140
141 format = zalloc(sizeof(*format));
142 if (!format)
143 return NULL;
144
145 format->name = strdup(name);
146 if (!format->name) {
147 free(format);
148 return NULL;
149 }
150 list_add_tail(&format->list, list);
151 return format;
152 }
153
154 /* Called at the end of parsing a format. */
perf_pmu_format__set_value(void * vformat,int config,unsigned long * bits)155 void perf_pmu_format__set_value(void *vformat, int config, unsigned long *bits)
156 {
157 struct perf_pmu_format *format = vformat;
158
159 format->value = config;
160 memcpy(format->bits, bits, sizeof(format->bits));
161 }
162
__perf_pmu_format__load(struct perf_pmu_format * format,FILE * file)163 static void __perf_pmu_format__load(struct perf_pmu_format *format, FILE *file)
164 {
165 void *scanner;
166 int ret;
167
168 ret = perf_pmu_lex_init(&scanner);
169 if (ret)
170 return;
171
172 perf_pmu_set_in(file, scanner);
173 ret = perf_pmu_parse(format, scanner);
174 perf_pmu_lex_destroy(scanner);
175 format->loaded = true;
176 }
177
perf_pmu_format__load(const struct perf_pmu * pmu,struct perf_pmu_format * format)178 static void perf_pmu_format__load(const struct perf_pmu *pmu, struct perf_pmu_format *format)
179 {
180 char path[PATH_MAX];
181 FILE *file = NULL;
182
183 if (format->loaded)
184 return;
185
186 if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, "format"))
187 return;
188
189 assert(strlen(path) + strlen(format->name) + 2 < sizeof(path));
190 strcat(path, "/");
191 strcat(path, format->name);
192
193 file = fopen(path, "r");
194 if (!file)
195 return;
196 __perf_pmu_format__load(format, file);
197 fclose(file);
198 }
199
200 /*
201 * Parse & process all the sysfs attributes located under
202 * the directory specified in 'dir' parameter.
203 */
perf_pmu__format_parse(struct perf_pmu * pmu,int dirfd,bool eager_load)204 static int perf_pmu__format_parse(struct perf_pmu *pmu, int dirfd, bool eager_load)
205 {
206 struct io_dirent64 *evt_ent;
207 struct io_dir format_dir;
208 int ret = 0;
209
210 io_dir__init(&format_dir, dirfd);
211
212 while ((evt_ent = io_dir__readdir(&format_dir)) != NULL) {
213 struct perf_pmu_format *format;
214 char *name = evt_ent->d_name;
215
216 if (io_dir__is_dir(&format_dir, evt_ent))
217 continue;
218
219 format = perf_pmu__new_format(&pmu->format, name);
220 if (!format) {
221 ret = -ENOMEM;
222 break;
223 }
224
225 if (eager_load) {
226 FILE *file;
227 int fd = openat(dirfd, name, O_RDONLY);
228
229 if (fd < 0) {
230 ret = -errno;
231 break;
232 }
233 file = fdopen(fd, "r");
234 if (!file) {
235 close(fd);
236 break;
237 }
238 __perf_pmu_format__load(format, file);
239 fclose(file);
240 }
241 }
242
243 close(format_dir.dirfd);
244 return ret;
245 }
246
247 /*
248 * Reading/parsing the default pmu format definition, which should be
249 * located at:
250 * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
251 */
pmu_format(struct perf_pmu * pmu,int dirfd,const char * name,bool eager_load)252 static int pmu_format(struct perf_pmu *pmu, int dirfd, const char *name, bool eager_load)
253 {
254 int fd;
255
256 fd = perf_pmu__pathname_fd(dirfd, name, "format", O_DIRECTORY);
257 if (fd < 0)
258 return 0;
259
260 /* it'll close the fd */
261 if (perf_pmu__format_parse(pmu, fd, eager_load))
262 return -1;
263
264 return 0;
265 }
266
parse_double(const char * scale,char ** end,double * sval)267 static int parse_double(const char *scale, char **end, double *sval)
268 {
269 char *lc;
270 int ret = 0;
271
272 /*
273 * save current locale
274 */
275 lc = setlocale(LC_NUMERIC, NULL);
276
277 /*
278 * The lc string may be allocated in static storage,
279 * so get a dynamic copy to make it survive setlocale
280 * call below.
281 */
282 lc = strdup(lc);
283 if (!lc) {
284 ret = -ENOMEM;
285 goto out;
286 }
287
288 /*
289 * force to C locale to ensure kernel
290 * scale string is converted correctly.
291 * kernel uses default C locale.
292 */
293 setlocale(LC_NUMERIC, "C");
294
295 *sval = strtod(scale, end);
296
297 out:
298 /* restore locale */
299 setlocale(LC_NUMERIC, lc);
300 free(lc);
301 return ret;
302 }
303
perf_pmu__convert_scale(const char * scale,char ** end,double * sval)304 int perf_pmu__convert_scale(const char *scale, char **end, double *sval)
305 {
306 return parse_double(scale, end, sval);
307 }
308
perf_pmu__parse_scale(struct perf_pmu * pmu,struct perf_pmu_alias * alias)309 static int perf_pmu__parse_scale(struct perf_pmu *pmu, struct perf_pmu_alias *alias)
310 {
311 struct stat st;
312 ssize_t sret;
313 size_t len;
314 char scale[128];
315 int fd, ret = -1;
316 char path[PATH_MAX];
317
318 len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path));
319 if (!len)
320 return 0;
321 scnprintf(path + len, sizeof(path) - len, "%s/events/%s.scale", pmu->name, alias->name);
322
323 fd = open(path, O_RDONLY);
324 if (fd == -1)
325 return -1;
326
327 if (fstat(fd, &st) < 0)
328 goto error;
329
330 sret = read(fd, scale, sizeof(scale)-1);
331 if (sret < 0)
332 goto error;
333
334 if (scale[sret - 1] == '\n')
335 scale[sret - 1] = '\0';
336 else
337 scale[sret] = '\0';
338
339 ret = perf_pmu__convert_scale(scale, NULL, &alias->scale);
340 error:
341 close(fd);
342 return ret;
343 }
344
perf_pmu__parse_unit(struct perf_pmu * pmu,struct perf_pmu_alias * alias)345 static int perf_pmu__parse_unit(struct perf_pmu *pmu, struct perf_pmu_alias *alias)
346 {
347 char path[PATH_MAX];
348 size_t len;
349 ssize_t sret;
350 int fd;
351
352
353 len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path));
354 if (!len)
355 return 0;
356 scnprintf(path + len, sizeof(path) - len, "%s/events/%s.unit", pmu->name, alias->name);
357
358 fd = open(path, O_RDONLY);
359 if (fd == -1)
360 return -1;
361
362 sret = read(fd, alias->unit, UNIT_MAX_LEN);
363 if (sret < 0)
364 goto error;
365
366 close(fd);
367
368 if (alias->unit[sret - 1] == '\n')
369 alias->unit[sret - 1] = '\0';
370 else
371 alias->unit[sret] = '\0';
372
373 return 0;
374 error:
375 close(fd);
376 alias->unit[0] = '\0';
377 return -1;
378 }
379
perf_pmu__parse_event_source_bool(const char * pmu_name,const char * event_name,const char * suffix)380 static bool perf_pmu__parse_event_source_bool(const char *pmu_name, const char *event_name,
381 const char *suffix)
382 {
383 char path[PATH_MAX];
384 size_t len;
385 int fd;
386
387 len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path));
388 if (!len)
389 return false;
390
391 scnprintf(path + len, sizeof(path) - len, "%s/events/%s.%s", pmu_name, event_name, suffix);
392
393 fd = open(path, O_RDONLY);
394 if (fd == -1)
395 return false;
396
397 #ifndef NDEBUG
398 {
399 char buf[8];
400
401 len = read(fd, buf, sizeof(buf));
402 assert(len == 1 || len == 2);
403 assert(buf[0] == '1');
404 }
405 #endif
406
407 close(fd);
408 return true;
409 }
410
perf_pmu__parse_per_pkg(struct perf_pmu * pmu,struct perf_pmu_alias * alias)411 static void perf_pmu__parse_per_pkg(struct perf_pmu *pmu, struct perf_pmu_alias *alias)
412 {
413 alias->per_pkg = perf_pmu__parse_event_source_bool(pmu->name, alias->name, "per-pkg");
414 }
415
perf_pmu__parse_snapshot(struct perf_pmu * pmu,struct perf_pmu_alias * alias)416 static void perf_pmu__parse_snapshot(struct perf_pmu *pmu, struct perf_pmu_alias *alias)
417 {
418 alias->snapshot = perf_pmu__parse_event_source_bool(pmu->name, alias->name, "snapshot");
419 }
420
421 /* Delete an alias entry. */
perf_pmu_free_alias(struct perf_pmu_alias * alias)422 static void perf_pmu_free_alias(struct perf_pmu_alias *alias)
423 {
424 if (!alias)
425 return;
426
427 zfree(&alias->name);
428 zfree(&alias->desc);
429 zfree(&alias->long_desc);
430 zfree(&alias->topic);
431 zfree(&alias->pmu_name);
432 parse_events_terms__exit(&alias->terms);
433 free(alias);
434 }
435
perf_pmu__del_aliases(struct perf_pmu * pmu)436 static void perf_pmu__del_aliases(struct perf_pmu *pmu)
437 {
438 struct hashmap_entry *entry;
439 size_t bkt;
440
441 if (!pmu->aliases)
442 return;
443
444 hashmap__for_each_entry(pmu->aliases, entry, bkt)
445 perf_pmu_free_alias(entry->pvalue);
446
447 hashmap__free(pmu->aliases);
448 pmu->aliases = NULL;
449 }
450
perf_pmu__find_alias(struct perf_pmu * pmu,const char * name,bool load)451 static struct perf_pmu_alias *perf_pmu__find_alias(struct perf_pmu *pmu,
452 const char *name,
453 bool load)
454 {
455 struct perf_pmu_alias *alias;
456 bool has_sysfs_event;
457 char event_file_name[NAME_MAX + 8];
458
459 if (hashmap__find(pmu->aliases, name, &alias))
460 return alias;
461
462 if (!load || pmu->sysfs_aliases_loaded)
463 return NULL;
464
465 /*
466 * Test if alias/event 'name' exists in the PMU's sysfs/events
467 * directory. If not skip parsing the sysfs aliases. Sysfs event
468 * name must be all lower or all upper case.
469 */
470 scnprintf(event_file_name, sizeof(event_file_name), "events/%s", name);
471 for (size_t i = 7, n = 7 + strlen(name); i < n; i++)
472 event_file_name[i] = tolower(event_file_name[i]);
473
474 has_sysfs_event = perf_pmu__file_exists(pmu, event_file_name);
475 if (!has_sysfs_event) {
476 for (size_t i = 7, n = 7 + strlen(name); i < n; i++)
477 event_file_name[i] = toupper(event_file_name[i]);
478
479 has_sysfs_event = perf_pmu__file_exists(pmu, event_file_name);
480 }
481 if (has_sysfs_event) {
482 pmu_aliases_parse(pmu);
483 if (hashmap__find(pmu->aliases, name, &alias))
484 return alias;
485 }
486
487 return NULL;
488 }
489
assign_str(const char * name,const char * field,char ** old_str,const char * new_str)490 static bool assign_str(const char *name, const char *field, char **old_str,
491 const char *new_str)
492 {
493 if (!*old_str && new_str) {
494 *old_str = strdup(new_str);
495 return true;
496 }
497
498 if (!new_str || !strcasecmp(*old_str, new_str))
499 return false; /* Nothing to update. */
500
501 pr_debug("alias %s differs in field '%s' ('%s' != '%s')\n",
502 name, field, *old_str, new_str);
503 zfree(old_str);
504 *old_str = strdup(new_str);
505 return true;
506 }
507
read_alias_info(struct perf_pmu * pmu,struct perf_pmu_alias * alias)508 static void read_alias_info(struct perf_pmu *pmu, struct perf_pmu_alias *alias)
509 {
510 if (!alias->from_sysfs || alias->info_loaded)
511 return;
512
513 /*
514 * load unit name and scale if available
515 */
516 perf_pmu__parse_unit(pmu, alias);
517 perf_pmu__parse_scale(pmu, alias);
518 perf_pmu__parse_per_pkg(pmu, alias);
519 perf_pmu__parse_snapshot(pmu, alias);
520 }
521
522 struct update_alias_data {
523 struct perf_pmu *pmu;
524 struct perf_pmu_alias *alias;
525 };
526
update_alias(const struct pmu_event * pe,const struct pmu_events_table * table __maybe_unused,void * vdata)527 static int update_alias(const struct pmu_event *pe,
528 const struct pmu_events_table *table __maybe_unused,
529 void *vdata)
530 {
531 struct update_alias_data *data = vdata;
532 int ret = 0;
533
534 read_alias_info(data->pmu, data->alias);
535 assign_str(pe->name, "desc", &data->alias->desc, pe->desc);
536 assign_str(pe->name, "long_desc", &data->alias->long_desc, pe->long_desc);
537 assign_str(pe->name, "topic", &data->alias->topic, pe->topic);
538 data->alias->per_pkg = pe->perpkg;
539 if (pe->event) {
540 parse_events_terms__exit(&data->alias->terms);
541 ret = parse_events_terms(&data->alias->terms, pe->event, /*input=*/NULL);
542 }
543 if (!ret && pe->unit) {
544 char *unit;
545
546 ret = perf_pmu__convert_scale(pe->unit, &unit, &data->alias->scale);
547 if (!ret)
548 snprintf(data->alias->unit, sizeof(data->alias->unit), "%s", unit);
549 }
550 if (!ret && pe->retirement_latency_mean) {
551 ret = parse_double(pe->retirement_latency_mean, NULL,
552 &data->alias->retirement_latency_mean);
553 }
554 if (!ret && pe->retirement_latency_min) {
555 ret = parse_double(pe->retirement_latency_min, NULL,
556 &data->alias->retirement_latency_min);
557 }
558 if (!ret && pe->retirement_latency_max) {
559 ret = parse_double(pe->retirement_latency_max, NULL,
560 &data->alias->retirement_latency_max);
561 }
562 return ret;
563 }
564
perf_pmu__new_alias(struct perf_pmu * pmu,const char * name,const char * desc,const char * val,FILE * val_fd,const struct pmu_event * pe,enum event_source src)565 static int perf_pmu__new_alias(struct perf_pmu *pmu, const char *name,
566 const char *desc, const char *val, FILE *val_fd,
567 const struct pmu_event *pe, enum event_source src)
568 {
569 struct perf_pmu_alias *alias, *old_alias;
570 int ret = 0;
571 const char *long_desc = NULL, *topic = NULL, *unit = NULL, *pmu_name = NULL;
572 bool deprecated = false, perpkg = false;
573
574 if (perf_pmu__find_alias(pmu, name, /*load=*/ false)) {
575 /* Alias was already created/loaded. */
576 return 0;
577 }
578
579 if (pe) {
580 long_desc = pe->long_desc;
581 topic = pe->topic;
582 unit = pe->unit;
583 perpkg = pe->perpkg;
584 deprecated = pe->deprecated;
585 if (pe->pmu && strcmp(pe->pmu, "default_core"))
586 pmu_name = pe->pmu;
587 }
588
589 alias = zalloc(sizeof(*alias));
590 if (!alias)
591 return -ENOMEM;
592
593 parse_events_terms__init(&alias->terms);
594 alias->scale = 1.0;
595 alias->unit[0] = '\0';
596 alias->per_pkg = perpkg;
597 alias->snapshot = false;
598 alias->deprecated = deprecated;
599 alias->retirement_latency_mean = 0.0;
600 alias->retirement_latency_min = 0.0;
601 alias->retirement_latency_max = 0.0;
602
603 if (!ret && pe && pe->retirement_latency_mean) {
604 ret = parse_double(pe->retirement_latency_mean, NULL,
605 &alias->retirement_latency_mean);
606 }
607 if (!ret && pe && pe->retirement_latency_min) {
608 ret = parse_double(pe->retirement_latency_min, NULL,
609 &alias->retirement_latency_min);
610 }
611 if (!ret && pe && pe->retirement_latency_max) {
612 ret = parse_double(pe->retirement_latency_max, NULL,
613 &alias->retirement_latency_max);
614 }
615 if (ret)
616 return ret;
617
618 ret = parse_events_terms(&alias->terms, val, val_fd);
619 if (ret) {
620 pr_err("Cannot parse alias %s: %d\n", val, ret);
621 free(alias);
622 return ret;
623 }
624
625 alias->name = strdup(name);
626 alias->desc = desc ? strdup(desc) : NULL;
627 alias->long_desc = long_desc ? strdup(long_desc) : NULL;
628 alias->topic = topic ? strdup(topic) : NULL;
629 alias->pmu_name = pmu_name ? strdup(pmu_name) : NULL;
630 if (unit) {
631 if (perf_pmu__convert_scale(unit, (char **)&unit, &alias->scale) < 0) {
632 perf_pmu_free_alias(alias);
633 return -1;
634 }
635 snprintf(alias->unit, sizeof(alias->unit), "%s", unit);
636 }
637 switch (src) {
638 default:
639 case EVENT_SRC_SYSFS:
640 alias->from_sysfs = true;
641 if (pmu->events_table) {
642 /* Update an event from sysfs with json data. */
643 struct update_alias_data data = {
644 .pmu = pmu,
645 .alias = alias,
646 };
647 if (pmu_events_table__find_event(pmu->events_table, pmu, name,
648 update_alias, &data) == 0)
649 pmu->cpu_common_json_aliases++;
650 }
651 pmu->sysfs_aliases++;
652 break;
653 case EVENT_SRC_CPU_JSON:
654 pmu->cpu_json_aliases++;
655 break;
656 case EVENT_SRC_SYS_JSON:
657 pmu->sys_json_aliases++;
658 break;
659
660 }
661 hashmap__set(pmu->aliases, alias->name, alias, /*old_key=*/ NULL, &old_alias);
662 perf_pmu_free_alias(old_alias);
663 return 0;
664 }
665
pmu_alias_info_file(const char * name)666 static inline bool pmu_alias_info_file(const char *name)
667 {
668 size_t len;
669
670 len = strlen(name);
671 if (len > 5 && !strcmp(name + len - 5, ".unit"))
672 return true;
673 if (len > 6 && !strcmp(name + len - 6, ".scale"))
674 return true;
675 if (len > 8 && !strcmp(name + len - 8, ".per-pkg"))
676 return true;
677 if (len > 9 && !strcmp(name + len - 9, ".snapshot"))
678 return true;
679
680 return false;
681 }
682
683 /*
684 * Reading the pmu event aliases definition, which should be located at:
685 * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
686 */
__pmu_aliases_parse(struct perf_pmu * pmu,int events_dir_fd)687 static int __pmu_aliases_parse(struct perf_pmu *pmu, int events_dir_fd)
688 {
689 struct io_dirent64 *evt_ent;
690 struct io_dir event_dir;
691
692 io_dir__init(&event_dir, events_dir_fd);
693
694 while ((evt_ent = io_dir__readdir(&event_dir))) {
695 char *name = evt_ent->d_name;
696 int fd;
697 FILE *file;
698
699 if (!strcmp(name, ".") || !strcmp(name, ".."))
700 continue;
701
702 /*
703 * skip info files parsed in perf_pmu__new_alias()
704 */
705 if (pmu_alias_info_file(name))
706 continue;
707
708 fd = openat(events_dir_fd, name, O_RDONLY);
709 if (fd == -1) {
710 pr_debug("Cannot open %s\n", name);
711 continue;
712 }
713 file = fdopen(fd, "r");
714 if (!file) {
715 close(fd);
716 continue;
717 }
718
719 if (perf_pmu__new_alias(pmu, name, /*desc=*/ NULL,
720 /*val=*/ NULL, file, /*pe=*/ NULL,
721 EVENT_SRC_SYSFS) < 0)
722 pr_debug("Cannot set up %s\n", name);
723 fclose(file);
724 }
725
726 pmu->sysfs_aliases_loaded = true;
727 return 0;
728 }
729
pmu_aliases_parse(struct perf_pmu * pmu)730 static int pmu_aliases_parse(struct perf_pmu *pmu)
731 {
732 char path[PATH_MAX];
733 size_t len;
734 int events_dir_fd, ret;
735
736 if (pmu->sysfs_aliases_loaded)
737 return 0;
738
739 len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path));
740 if (!len)
741 return 0;
742 scnprintf(path + len, sizeof(path) - len, "%s/events", pmu->name);
743
744 events_dir_fd = open(path, O_DIRECTORY);
745 if (events_dir_fd == -1) {
746 pmu->sysfs_aliases_loaded = true;
747 return 0;
748 }
749 ret = __pmu_aliases_parse(pmu, events_dir_fd);
750 close(events_dir_fd);
751 return ret;
752 }
753
pmu_aliases_parse_eager(struct perf_pmu * pmu,int sysfs_fd)754 static int pmu_aliases_parse_eager(struct perf_pmu *pmu, int sysfs_fd)
755 {
756 char path[NAME_MAX + 8];
757 int ret, events_dir_fd;
758
759 scnprintf(path, sizeof(path), "%s/events", pmu->name);
760 events_dir_fd = openat(sysfs_fd, path, O_DIRECTORY, 0);
761 if (events_dir_fd == -1) {
762 pmu->sysfs_aliases_loaded = true;
763 return 0;
764 }
765 ret = __pmu_aliases_parse(pmu, events_dir_fd);
766 close(events_dir_fd);
767 return ret;
768 }
769
pmu_alias_terms(struct perf_pmu_alias * alias,int err_loc,struct list_head * terms)770 static int pmu_alias_terms(struct perf_pmu_alias *alias, int err_loc, struct list_head *terms)
771 {
772 struct parse_events_term *term, *cloned;
773 struct parse_events_terms clone_terms;
774
775 parse_events_terms__init(&clone_terms);
776 list_for_each_entry(term, &alias->terms.terms, list) {
777 int ret = parse_events_term__clone(&cloned, term);
778
779 if (ret) {
780 parse_events_terms__exit(&clone_terms);
781 return ret;
782 }
783 /*
784 * Weak terms don't override command line options,
785 * which we don't want for implicit terms in aliases.
786 */
787 cloned->weak = true;
788 cloned->err_term = cloned->err_val = err_loc;
789 list_add_tail(&cloned->list, &clone_terms.terms);
790 }
791 list_splice_init(&clone_terms.terms, terms);
792 parse_events_terms__exit(&clone_terms);
793 return 0;
794 }
795
796 /*
797 * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64)
798 * may have a "cpus" file.
799 */
pmu_cpumask(int dirfd,const char * pmu_name,bool is_core)800 static struct perf_cpu_map *pmu_cpumask(int dirfd, const char *pmu_name, bool is_core)
801 {
802 const char *templates[] = {
803 "cpumask",
804 "cpus",
805 NULL
806 };
807 const char **template;
808
809 for (template = templates; *template; template++) {
810 struct io io;
811 char buf[128];
812 char *cpumask = NULL;
813 size_t cpumask_len;
814 ssize_t ret;
815 struct perf_cpu_map *cpus;
816
817 io.fd = perf_pmu__pathname_fd(dirfd, pmu_name, *template, O_RDONLY);
818 if (io.fd < 0)
819 continue;
820
821 io__init(&io, io.fd, buf, sizeof(buf));
822 ret = io__getline(&io, &cpumask, &cpumask_len);
823 close(io.fd);
824 if (ret < 0)
825 continue;
826
827 cpus = perf_cpu_map__new(cpumask);
828 free(cpumask);
829 if (cpus)
830 return cpus;
831 }
832
833 /* Nothing found, for core PMUs assume this means all CPUs. */
834 return is_core ? cpu_map__online() : NULL;
835 }
836
pmu_is_uncore(int dirfd,const char * name)837 static bool pmu_is_uncore(int dirfd, const char *name)
838 {
839 int fd;
840
841 fd = perf_pmu__pathname_fd(dirfd, name, "cpumask", O_PATH);
842 if (fd < 0)
843 return false;
844
845 close(fd);
846 return true;
847 }
848
pmu_id(const char * name)849 static char *pmu_id(const char *name)
850 {
851 char path[PATH_MAX], *str;
852 size_t len;
853
854 perf_pmu__pathname_scnprintf(path, sizeof(path), name, "identifier");
855
856 if (filename__read_str(path, &str, &len) < 0)
857 return NULL;
858
859 str[len - 1] = 0; /* remove line feed */
860
861 return str;
862 }
863
864 /**
865 * is_sysfs_pmu_core() - PMU CORE devices have different name other than cpu in
866 * sysfs on some platforms like ARM or Intel hybrid. Looking for
867 * possible the cpus file in sysfs files to identify whether this is a
868 * core device.
869 * @name: The PMU name such as "cpu_atom".
870 */
is_sysfs_pmu_core(const char * name)871 static int is_sysfs_pmu_core(const char *name)
872 {
873 char path[PATH_MAX];
874
875 if (!perf_pmu__pathname_scnprintf(path, sizeof(path), name, "cpus"))
876 return 0;
877 return file_available(path);
878 }
879
880 /**
881 * Return the length of the PMU name not including the suffix for uncore PMUs.
882 *
883 * We want to deduplicate many similar uncore PMUs by stripping their suffixes,
884 * but there are never going to be too many core PMUs and the suffixes might be
885 * interesting. "arm_cortex_a53" vs "arm_cortex_a57" or "cpum_cf" for example.
886 *
887 * @skip_duplicate_pmus: False in verbose mode so all uncore PMUs are visible
888 */
pmu_deduped_name_len(const struct perf_pmu * pmu,const char * name,bool skip_duplicate_pmus)889 static size_t pmu_deduped_name_len(const struct perf_pmu *pmu, const char *name,
890 bool skip_duplicate_pmus)
891 {
892 return skip_duplicate_pmus && !pmu->is_core
893 ? pmu_name_len_no_suffix(name)
894 : strlen(name);
895 }
896
897 /**
898 * perf_pmu__match_wildcard - Does the pmu_name start with tok and is then only
899 * followed by nothing or a suffix? tok may contain
900 * part of a suffix.
901 * @pmu_name: The pmu_name with possible suffix.
902 * @tok: The wildcard argument to match.
903 */
perf_pmu__match_wildcard(const char * pmu_name,const char * tok)904 static bool perf_pmu__match_wildcard(const char *pmu_name, const char *tok)
905 {
906 const char *p, *suffix;
907 bool has_hex = false;
908 size_t tok_len = strlen(tok);
909
910 /* Check start of pmu_name for equality. */
911 if (strncmp(pmu_name, tok, tok_len))
912 return false;
913
914 suffix = p = pmu_name + tok_len;
915 if (*p == 0)
916 return true;
917
918 if (*p == '_') {
919 ++p;
920 ++suffix;
921 }
922
923 /* Ensure we end in a number */
924 while (1) {
925 if (!isxdigit(*p))
926 return false;
927 if (!has_hex)
928 has_hex = !isdigit(*p);
929 if (*(++p) == 0)
930 break;
931 }
932
933 if (has_hex)
934 return (p - suffix) > 2;
935
936 return true;
937 }
938
939 /**
940 * perf_pmu__match_ignoring_suffix_uncore - Does the pmu_name match tok ignoring
941 * any trailing suffix on pmu_name and
942 * tok? The Suffix must be in form
943 * tok_{digits}, or tok{digits}.
944 * @pmu_name: The pmu_name with possible suffix.
945 * @tok: The possible match to pmu_name.
946 */
perf_pmu__match_ignoring_suffix_uncore(const char * pmu_name,const char * tok)947 static bool perf_pmu__match_ignoring_suffix_uncore(const char *pmu_name, const char *tok)
948 {
949 size_t pmu_name_len, tok_len;
950
951 /* For robustness, check for NULL. */
952 if (pmu_name == NULL)
953 return tok == NULL;
954
955 /* uncore_ prefixes are ignored. */
956 if (!strncmp(pmu_name, "uncore_", 7))
957 pmu_name += 7;
958 if (!strncmp(tok, "uncore_", 7))
959 tok += 7;
960
961 pmu_name_len = pmu_name_len_no_suffix(pmu_name);
962 tok_len = pmu_name_len_no_suffix(tok);
963 if (pmu_name_len != tok_len)
964 return false;
965
966 return strncmp(pmu_name, tok, pmu_name_len) == 0;
967 }
968
969
970 /**
971 * perf_pmu__match_wildcard_uncore - does to_match match the PMU's name?
972 * @pmu_name: The pmu->name or pmu->alias to match against.
973 * @to_match: the json struct pmu_event name. This may lack a suffix (which
974 * matches) or be of the form "socket,pmuname" which will match
975 * "socketX_pmunameY".
976 */
perf_pmu__match_wildcard_uncore(const char * pmu_name,const char * to_match)977 static bool perf_pmu__match_wildcard_uncore(const char *pmu_name, const char *to_match)
978 {
979 char *mutable_to_match, *tok, *tmp;
980
981 if (!pmu_name)
982 return false;
983
984 /* uncore_ prefixes are ignored. */
985 if (!strncmp(pmu_name, "uncore_", 7))
986 pmu_name += 7;
987 if (!strncmp(to_match, "uncore_", 7))
988 to_match += 7;
989
990 if (strchr(to_match, ',') == NULL)
991 return perf_pmu__match_wildcard(pmu_name, to_match);
992
993 /* Process comma separated list of PMU name components. */
994 mutable_to_match = strdup(to_match);
995 if (!mutable_to_match)
996 return false;
997
998 tok = strtok_r(mutable_to_match, ",", &tmp);
999 while (tok) {
1000 size_t tok_len = strlen(tok);
1001
1002 if (strncmp(pmu_name, tok, tok_len)) {
1003 /* Mismatch between part of pmu_name and tok. */
1004 free(mutable_to_match);
1005 return false;
1006 }
1007 /* Move pmu_name forward over tok and suffix. */
1008 pmu_name += tok_len;
1009 while (*pmu_name != '\0' && isdigit(*pmu_name))
1010 pmu_name++;
1011 if (*pmu_name == '_')
1012 pmu_name++;
1013
1014 tok = strtok_r(NULL, ",", &tmp);
1015 }
1016 free(mutable_to_match);
1017 return *pmu_name == '\0';
1018 }
1019
pmu_uncore_identifier_match(const char * compat,const char * id)1020 bool pmu_uncore_identifier_match(const char *compat, const char *id)
1021 {
1022 regex_t re;
1023 regmatch_t pmatch[1];
1024 int match;
1025
1026 if (regcomp(&re, compat, REG_EXTENDED) != 0) {
1027 /* Warn unable to generate match particular string. */
1028 pr_info("Invalid regular expression %s\n", compat);
1029 return false;
1030 }
1031
1032 match = !regexec(&re, id, 1, pmatch, 0);
1033 if (match) {
1034 /* Ensure a full match. */
1035 match = pmatch[0].rm_so == 0 && (size_t)pmatch[0].rm_eo == strlen(id);
1036 }
1037 regfree(&re);
1038
1039 return match;
1040 }
1041
pmu_add_cpu_aliases_map_callback(const struct pmu_event * pe,const struct pmu_events_table * table __maybe_unused,void * vdata)1042 static int pmu_add_cpu_aliases_map_callback(const struct pmu_event *pe,
1043 const struct pmu_events_table *table __maybe_unused,
1044 void *vdata)
1045 {
1046 struct perf_pmu *pmu = vdata;
1047
1048 perf_pmu__new_alias(pmu, pe->name, pe->desc, pe->event, /*val_fd=*/ NULL,
1049 pe, EVENT_SRC_CPU_JSON);
1050 return 0;
1051 }
1052
1053 /*
1054 * From the pmu_events_table, find the events that correspond to the given
1055 * PMU and add them to the list 'head'.
1056 */
pmu_add_cpu_aliases_table(struct perf_pmu * pmu,const struct pmu_events_table * table)1057 void pmu_add_cpu_aliases_table(struct perf_pmu *pmu, const struct pmu_events_table *table)
1058 {
1059 pmu_events_table__for_each_event(table, pmu, pmu_add_cpu_aliases_map_callback, pmu);
1060 }
1061
pmu_add_cpu_aliases(struct perf_pmu * pmu)1062 static void pmu_add_cpu_aliases(struct perf_pmu *pmu)
1063 {
1064 if (!pmu->events_table)
1065 return;
1066
1067 if (pmu->cpu_aliases_added)
1068 return;
1069
1070 pmu_add_cpu_aliases_table(pmu, pmu->events_table);
1071 pmu->cpu_aliases_added = true;
1072 }
1073
pmu_add_sys_aliases_iter_fn(const struct pmu_event * pe,const struct pmu_events_table * table __maybe_unused,void * vdata)1074 static int pmu_add_sys_aliases_iter_fn(const struct pmu_event *pe,
1075 const struct pmu_events_table *table __maybe_unused,
1076 void *vdata)
1077 {
1078 struct perf_pmu *pmu = vdata;
1079
1080 if (!pe->compat || !pe->pmu) {
1081 /* No data to match. */
1082 return 0;
1083 }
1084
1085 if (!perf_pmu__match_wildcard_uncore(pmu->name, pe->pmu) &&
1086 !perf_pmu__match_wildcard_uncore(pmu->alias_name, pe->pmu)) {
1087 /* PMU name/alias_name don't match. */
1088 return 0;
1089 }
1090
1091 if (pmu_uncore_identifier_match(pe->compat, pmu->id)) {
1092 /* Id matched. */
1093 perf_pmu__new_alias(pmu,
1094 pe->name,
1095 pe->desc,
1096 pe->event,
1097 /*val_fd=*/ NULL,
1098 pe,
1099 EVENT_SRC_SYS_JSON);
1100 }
1101 return 0;
1102 }
1103
pmu_add_sys_aliases(struct perf_pmu * pmu)1104 void pmu_add_sys_aliases(struct perf_pmu *pmu)
1105 {
1106 if (!pmu->id)
1107 return;
1108
1109 pmu_for_each_sys_event(pmu_add_sys_aliases_iter_fn, pmu);
1110 }
1111
pmu_find_alias_name(struct perf_pmu * pmu,int dirfd)1112 static char *pmu_find_alias_name(struct perf_pmu *pmu, int dirfd)
1113 {
1114 FILE *file = perf_pmu__open_file_at(pmu, dirfd, "alias");
1115 char *line = NULL;
1116 size_t line_len = 0;
1117 ssize_t ret;
1118
1119 if (!file)
1120 return NULL;
1121
1122 ret = getline(&line, &line_len, file);
1123 if (ret < 0) {
1124 fclose(file);
1125 return NULL;
1126 }
1127 /* Remove trailing newline. */
1128 if (ret > 0 && line[ret - 1] == '\n')
1129 line[--ret] = '\0';
1130
1131 fclose(file);
1132 return line;
1133 }
1134
pmu_max_precise(int dirfd,struct perf_pmu * pmu)1135 static int pmu_max_precise(int dirfd, struct perf_pmu *pmu)
1136 {
1137 int max_precise = -1;
1138
1139 perf_pmu__scan_file_at(pmu, dirfd, "caps/max_precise", "%d", &max_precise);
1140 return max_precise;
1141 }
1142
1143 void __weak
perf_pmu__arch_init(struct perf_pmu * pmu)1144 perf_pmu__arch_init(struct perf_pmu *pmu)
1145 {
1146 if (pmu->is_core)
1147 pmu->mem_events = perf_mem_events;
1148 }
1149
1150 /* Variant of str_hash that does tolower on each character. */
aliases__hash(long key,void * ctx __maybe_unused)1151 static size_t aliases__hash(long key, void *ctx __maybe_unused)
1152 {
1153 const char *s = (const char *)key;
1154 size_t h = 0;
1155
1156 while (*s) {
1157 h = h * 31 + tolower(*s);
1158 s++;
1159 }
1160 return h;
1161 }
1162
aliases__equal(long key1,long key2,void * ctx __maybe_unused)1163 static bool aliases__equal(long key1, long key2, void *ctx __maybe_unused)
1164 {
1165 return strcasecmp((const char *)key1, (const char *)key2) == 0;
1166 }
1167
perf_pmu__init(struct perf_pmu * pmu,__u32 type,const char * name)1168 int perf_pmu__init(struct perf_pmu *pmu, __u32 type, const char *name)
1169 {
1170 pmu->type = type;
1171 INIT_LIST_HEAD(&pmu->format);
1172 INIT_LIST_HEAD(&pmu->caps);
1173
1174 pmu->name = strdup(name);
1175 if (!pmu->name)
1176 return -ENOMEM;
1177
1178 pmu->aliases = hashmap__new(aliases__hash, aliases__equal, /*ctx=*/ NULL);
1179 if (!pmu->aliases)
1180 return -ENOMEM;
1181
1182 return 0;
1183 }
1184
wellknown_pmu_type(const char * pmu_name)1185 static __u32 wellknown_pmu_type(const char *pmu_name)
1186 {
1187 struct {
1188 const char *pmu_name;
1189 __u32 type;
1190 } wellknown_pmus[] = {
1191 {
1192 "software",
1193 PERF_TYPE_SOFTWARE
1194 },
1195 {
1196 "tracepoint",
1197 PERF_TYPE_TRACEPOINT
1198 },
1199 {
1200 "breakpoint",
1201 PERF_TYPE_BREAKPOINT
1202 },
1203 };
1204 for (size_t i = 0; i < ARRAY_SIZE(wellknown_pmus); i++) {
1205 if (!strcmp(wellknown_pmus[i].pmu_name, pmu_name))
1206 return wellknown_pmus[i].type;
1207 }
1208 return PERF_TYPE_MAX;
1209 }
1210
perf_pmu__lookup(struct list_head * pmus,int dirfd,const char * name,bool eager_load)1211 struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char *name,
1212 bool eager_load)
1213 {
1214 struct perf_pmu *pmu;
1215
1216 pmu = zalloc(sizeof(*pmu));
1217 if (!pmu)
1218 return NULL;
1219
1220 if (perf_pmu__init(pmu, PERF_PMU_TYPE_FAKE, name) != 0) {
1221 perf_pmu__delete(pmu);
1222 return NULL;
1223 }
1224
1225 /*
1226 * Read type early to fail fast if a lookup name isn't a PMU. Ensure
1227 * that type value is successfully assigned (return 1).
1228 */
1229 if (perf_pmu__scan_file_at(pmu, dirfd, "type", "%u", &pmu->type) != 1) {
1230 /* Double check the PMU's name isn't wellknown. */
1231 pmu->type = wellknown_pmu_type(name);
1232 if (pmu->type == PERF_TYPE_MAX) {
1233 perf_pmu__delete(pmu);
1234 return NULL;
1235 }
1236 }
1237
1238 /*
1239 * The pmu data we store & need consists of the pmu
1240 * type value and format definitions. Load both right
1241 * now.
1242 */
1243 if (pmu_format(pmu, dirfd, name, eager_load)) {
1244 perf_pmu__delete(pmu);
1245 return NULL;
1246 }
1247
1248 pmu->is_core = is_pmu_core(name);
1249 pmu->cpus = pmu_cpumask(dirfd, name, pmu->is_core);
1250
1251 pmu->is_uncore = pmu_is_uncore(dirfd, name);
1252 if (pmu->is_uncore)
1253 pmu->id = pmu_id(name);
1254 pmu->max_precise = pmu_max_precise(dirfd, pmu);
1255 pmu->alias_name = pmu_find_alias_name(pmu, dirfd);
1256 pmu->events_table = perf_pmu__find_events_table(pmu);
1257 /*
1258 * Load the sys json events/aliases when loading the PMU as each event
1259 * may have a different compat regular expression. We therefore can't
1260 * know the number of sys json events/aliases without computing the
1261 * regular expressions for them all.
1262 */
1263 pmu_add_sys_aliases(pmu);
1264 list_add_tail(&pmu->list, pmus);
1265
1266 perf_pmu__arch_init(pmu);
1267
1268 if (eager_load)
1269 pmu_aliases_parse_eager(pmu, dirfd);
1270
1271 return pmu;
1272 }
1273
1274 /* Creates the PMU when sysfs scanning fails. */
perf_pmu__create_placeholder_core_pmu(struct list_head * core_pmus)1275 struct perf_pmu *perf_pmu__create_placeholder_core_pmu(struct list_head *core_pmus)
1276 {
1277 struct perf_pmu *pmu = zalloc(sizeof(*pmu));
1278
1279 if (!pmu)
1280 return NULL;
1281
1282 pmu->name = strdup("cpu");
1283 if (!pmu->name) {
1284 free(pmu);
1285 return NULL;
1286 }
1287
1288 pmu->is_core = true;
1289 pmu->type = PERF_TYPE_RAW;
1290 pmu->cpus = cpu_map__online();
1291
1292 INIT_LIST_HEAD(&pmu->format);
1293 pmu->aliases = hashmap__new(aliases__hash, aliases__equal, /*ctx=*/ NULL);
1294 INIT_LIST_HEAD(&pmu->caps);
1295 list_add_tail(&pmu->list, core_pmus);
1296 return pmu;
1297 }
1298
perf_pmu__is_fake(const struct perf_pmu * pmu)1299 bool perf_pmu__is_fake(const struct perf_pmu *pmu)
1300 {
1301 return pmu->type == PERF_PMU_TYPE_FAKE;
1302 }
1303
perf_pmu__warn_invalid_formats(struct perf_pmu * pmu)1304 void perf_pmu__warn_invalid_formats(struct perf_pmu *pmu)
1305 {
1306 struct perf_pmu_format *format;
1307
1308 if (pmu->formats_checked)
1309 return;
1310
1311 pmu->formats_checked = true;
1312
1313 /* fake pmu doesn't have format list */
1314 if (perf_pmu__is_fake(pmu))
1315 return;
1316
1317 list_for_each_entry(format, &pmu->format, list) {
1318 perf_pmu_format__load(pmu, format);
1319 if (format->value >= PERF_PMU_FORMAT_VALUE_CONFIG_END) {
1320 pr_warning("WARNING: '%s' format '%s' requires 'perf_event_attr::config%d'"
1321 "which is not supported by this version of perf!\n",
1322 pmu->name, format->name, format->value);
1323 return;
1324 }
1325 }
1326 }
1327
evsel__is_aux_event(const struct evsel * evsel)1328 bool evsel__is_aux_event(const struct evsel *evsel)
1329 {
1330 struct perf_pmu *pmu;
1331
1332 if (evsel->needs_auxtrace_mmap)
1333 return true;
1334
1335 pmu = evsel__find_pmu(evsel);
1336 return pmu && pmu->auxtrace;
1337 }
1338
1339 /*
1340 * Set @config_name to @val as long as the user hasn't already set or cleared it
1341 * by passing a config term on the command line.
1342 *
1343 * @val is the value to put into the bits specified by @config_name rather than
1344 * the bit pattern. It is shifted into position by this function, so to set
1345 * something to true, pass 1 for val rather than a pre shifted value.
1346 */
1347 #define field_prep(_mask, _val) (((_val) << (ffsll(_mask) - 1)) & (_mask))
evsel__set_config_if_unset(struct perf_pmu * pmu,struct evsel * evsel,const char * config_name,u64 val)1348 void evsel__set_config_if_unset(struct perf_pmu *pmu, struct evsel *evsel,
1349 const char *config_name, u64 val)
1350 {
1351 u64 user_bits = 0, bits;
1352 struct evsel_config_term *term = evsel__get_config_term(evsel, CFG_CHG);
1353
1354 if (term)
1355 user_bits = term->val.cfg_chg;
1356
1357 bits = perf_pmu__format_bits(pmu, config_name);
1358
1359 /* Do nothing if the user changed the value */
1360 if (bits & user_bits)
1361 return;
1362
1363 /* Otherwise replace it */
1364 evsel->core.attr.config &= ~bits;
1365 evsel->core.attr.config |= field_prep(bits, val);
1366 }
1367
1368 static struct perf_pmu_format *
pmu_find_format(const struct list_head * formats,const char * name)1369 pmu_find_format(const struct list_head *formats, const char *name)
1370 {
1371 struct perf_pmu_format *format;
1372
1373 list_for_each_entry(format, formats, list)
1374 if (!strcmp(format->name, name))
1375 return format;
1376
1377 return NULL;
1378 }
1379
perf_pmu__format_bits(struct perf_pmu * pmu,const char * name)1380 __u64 perf_pmu__format_bits(struct perf_pmu *pmu, const char *name)
1381 {
1382 struct perf_pmu_format *format = pmu_find_format(&pmu->format, name);
1383 __u64 bits = 0;
1384 int fbit;
1385
1386 if (!format)
1387 return 0;
1388
1389 for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
1390 bits |= 1ULL << fbit;
1391
1392 return bits;
1393 }
1394
perf_pmu__format_type(struct perf_pmu * pmu,const char * name)1395 int perf_pmu__format_type(struct perf_pmu *pmu, const char *name)
1396 {
1397 struct perf_pmu_format *format = pmu_find_format(&pmu->format, name);
1398
1399 if (!format)
1400 return -1;
1401
1402 perf_pmu_format__load(pmu, format);
1403 return format->value;
1404 }
1405
1406 /*
1407 * Sets value based on the format definition (format parameter)
1408 * and unformatted value (value parameter).
1409 */
pmu_format_value(unsigned long * format,__u64 value,__u64 * v,bool zero)1410 static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
1411 bool zero)
1412 {
1413 unsigned long fbit, vbit;
1414
1415 for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
1416
1417 if (!test_bit(fbit, format))
1418 continue;
1419
1420 if (value & (1llu << vbit++))
1421 *v |= (1llu << fbit);
1422 else if (zero)
1423 *v &= ~(1llu << fbit);
1424 }
1425 }
1426
pmu_format_max_value(const unsigned long * format)1427 static __u64 pmu_format_max_value(const unsigned long *format)
1428 {
1429 int w;
1430
1431 w = bitmap_weight(format, PERF_PMU_FORMAT_BITS);
1432 if (!w)
1433 return 0;
1434 if (w < 64)
1435 return (1ULL << w) - 1;
1436 return -1;
1437 }
1438
1439 /*
1440 * Term is a string term, and might be a param-term. Try to look up it's value
1441 * in the remaining terms.
1442 * - We have a term like "base-or-format-term=param-term",
1443 * - We need to find the value supplied for "param-term" (with param-term named
1444 * in a config string) later on in the term list.
1445 */
pmu_resolve_param_term(struct parse_events_term * term,struct parse_events_terms * head_terms,__u64 * value)1446 static int pmu_resolve_param_term(struct parse_events_term *term,
1447 struct parse_events_terms *head_terms,
1448 __u64 *value)
1449 {
1450 struct parse_events_term *t;
1451
1452 list_for_each_entry(t, &head_terms->terms, list) {
1453 if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM &&
1454 t->config && !strcmp(t->config, term->config)) {
1455 t->used = true;
1456 *value = t->val.num;
1457 return 0;
1458 }
1459 }
1460
1461 if (verbose > 0)
1462 printf("Required parameter '%s' not specified\n", term->config);
1463
1464 return -1;
1465 }
1466
pmu_formats_string(const struct list_head * formats)1467 static char *pmu_formats_string(const struct list_head *formats)
1468 {
1469 struct perf_pmu_format *format;
1470 char *str = NULL;
1471 struct strbuf buf = STRBUF_INIT;
1472 unsigned int i = 0;
1473
1474 if (!formats)
1475 return NULL;
1476
1477 /* sysfs exported terms */
1478 list_for_each_entry(format, formats, list)
1479 if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
1480 goto error;
1481
1482 str = strbuf_detach(&buf, NULL);
1483 error:
1484 strbuf_release(&buf);
1485
1486 return str;
1487 }
1488
1489 /*
1490 * Setup one of config[12] attr members based on the
1491 * user input data - term parameter.
1492 */
pmu_config_term(const struct perf_pmu * pmu,struct perf_event_attr * attr,struct parse_events_term * term,struct parse_events_terms * head_terms,bool zero,bool apply_hardcoded,struct parse_events_error * err)1493 static int pmu_config_term(const struct perf_pmu *pmu,
1494 struct perf_event_attr *attr,
1495 struct parse_events_term *term,
1496 struct parse_events_terms *head_terms,
1497 bool zero, bool apply_hardcoded,
1498 struct parse_events_error *err)
1499 {
1500 struct perf_pmu_format *format;
1501 __u64 *vp;
1502 __u64 val, max_val;
1503
1504 /*
1505 * If this is a parameter we've already used for parameterized-eval,
1506 * skip it in normal eval.
1507 */
1508 if (term->used)
1509 return 0;
1510
1511 /*
1512 * Hardcoded terms are generally handled in event parsing, which
1513 * traditionally have had to handle not having a PMU. An alias may
1514 * have hard coded config values, optionally apply them below.
1515 */
1516 if (parse_events__is_hardcoded_term(term)) {
1517 /* Config terms set all bits in the config. */
1518 DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
1519
1520 if (!apply_hardcoded)
1521 return 0;
1522
1523 bitmap_fill(bits, PERF_PMU_FORMAT_BITS);
1524
1525 switch (term->type_term) {
1526 case PARSE_EVENTS__TERM_TYPE_CONFIG:
1527 assert(term->type_val == PARSE_EVENTS__TERM_TYPE_NUM);
1528 pmu_format_value(bits, term->val.num, &attr->config, zero);
1529 break;
1530 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
1531 assert(term->type_val == PARSE_EVENTS__TERM_TYPE_NUM);
1532 pmu_format_value(bits, term->val.num, &attr->config1, zero);
1533 break;
1534 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
1535 assert(term->type_val == PARSE_EVENTS__TERM_TYPE_NUM);
1536 pmu_format_value(bits, term->val.num, &attr->config2, zero);
1537 break;
1538 case PARSE_EVENTS__TERM_TYPE_CONFIG3:
1539 assert(term->type_val == PARSE_EVENTS__TERM_TYPE_NUM);
1540 pmu_format_value(bits, term->val.num, &attr->config3, zero);
1541 break;
1542 case PARSE_EVENTS__TERM_TYPE_USER: /* Not hardcoded. */
1543 return -EINVAL;
1544 case PARSE_EVENTS__TERM_TYPE_NAME ... PARSE_EVENTS__TERM_TYPE_CPU:
1545 /* Skip non-config terms. */
1546 break;
1547 default:
1548 break;
1549 }
1550 return 0;
1551 }
1552
1553 format = pmu_find_format(&pmu->format, term->config);
1554 if (!format) {
1555 char *pmu_term = pmu_formats_string(&pmu->format);
1556 char *unknown_term;
1557 char *help_msg;
1558
1559 if (asprintf(&unknown_term,
1560 "unknown term '%s' for pmu '%s'",
1561 term->config, pmu->name) < 0)
1562 unknown_term = NULL;
1563 help_msg = parse_events_formats_error_string(pmu_term);
1564 if (err) {
1565 parse_events_error__handle(err, term->err_term,
1566 unknown_term,
1567 help_msg);
1568 } else {
1569 pr_debug("%s (%s)\n", unknown_term, help_msg);
1570 free(unknown_term);
1571 }
1572 free(pmu_term);
1573 return -EINVAL;
1574 }
1575 perf_pmu_format__load(pmu, format);
1576 switch (format->value) {
1577 case PERF_PMU_FORMAT_VALUE_CONFIG:
1578 vp = &attr->config;
1579 break;
1580 case PERF_PMU_FORMAT_VALUE_CONFIG1:
1581 vp = &attr->config1;
1582 break;
1583 case PERF_PMU_FORMAT_VALUE_CONFIG2:
1584 vp = &attr->config2;
1585 break;
1586 case PERF_PMU_FORMAT_VALUE_CONFIG3:
1587 vp = &attr->config3;
1588 break;
1589 default:
1590 return -EINVAL;
1591 }
1592
1593 /*
1594 * Either directly use a numeric term, or try to translate string terms
1595 * using event parameters.
1596 */
1597 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1598 if (term->no_value &&
1599 bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) {
1600 if (err) {
1601 parse_events_error__handle(err, term->err_val,
1602 strdup("no value assigned for term"),
1603 NULL);
1604 }
1605 return -EINVAL;
1606 }
1607
1608 val = term->val.num;
1609 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1610 if (strcmp(term->val.str, "?")) {
1611 if (verbose > 0) {
1612 pr_info("Invalid sysfs entry %s=%s\n",
1613 term->config, term->val.str);
1614 }
1615 if (err) {
1616 parse_events_error__handle(err, term->err_val,
1617 strdup("expected numeric value"),
1618 NULL);
1619 }
1620 return -EINVAL;
1621 }
1622
1623 if (pmu_resolve_param_term(term, head_terms, &val))
1624 return -EINVAL;
1625 } else
1626 return -EINVAL;
1627
1628 max_val = pmu_format_max_value(format->bits);
1629 if (val > max_val) {
1630 if (err) {
1631 char *err_str;
1632
1633 if (asprintf(&err_str,
1634 "value too big for format (%s), maximum is %llu",
1635 format->name, (unsigned long long)max_val) < 0) {
1636 err_str = strdup("value too big for format");
1637 }
1638 parse_events_error__handle(err, term->err_val, err_str, /*help=*/NULL);
1639 return -EINVAL;
1640 }
1641 /*
1642 * Assume we don't care if !err, in which case the value will be
1643 * silently truncated.
1644 */
1645 }
1646
1647 pmu_format_value(format->bits, val, vp, zero);
1648 return 0;
1649 }
1650
perf_pmu__config_terms(const struct perf_pmu * pmu,struct perf_event_attr * attr,struct parse_events_terms * terms,bool zero,bool apply_hardcoded,struct parse_events_error * err)1651 int perf_pmu__config_terms(const struct perf_pmu *pmu,
1652 struct perf_event_attr *attr,
1653 struct parse_events_terms *terms,
1654 bool zero, bool apply_hardcoded,
1655 struct parse_events_error *err)
1656 {
1657 struct parse_events_term *term;
1658
1659 if (perf_pmu__is_hwmon(pmu))
1660 return hwmon_pmu__config_terms(pmu, attr, terms, err);
1661 if (perf_pmu__is_drm(pmu))
1662 return drm_pmu__config_terms(pmu, attr, terms, err);
1663
1664 list_for_each_entry(term, &terms->terms, list) {
1665 if (pmu_config_term(pmu, attr, term, terms, zero, apply_hardcoded, err))
1666 return -EINVAL;
1667 }
1668
1669 return 0;
1670 }
1671
1672 /*
1673 * Configures event's 'attr' parameter based on the:
1674 * 1) users input - specified in terms parameter
1675 * 2) pmu format definitions - specified by pmu parameter
1676 */
perf_pmu__config(struct perf_pmu * pmu,struct perf_event_attr * attr,struct parse_events_terms * head_terms,bool apply_hardcoded,struct parse_events_error * err)1677 int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
1678 struct parse_events_terms *head_terms,
1679 bool apply_hardcoded,
1680 struct parse_events_error *err)
1681 {
1682 bool zero = !!pmu->perf_event_attr_init_default;
1683
1684 /* Fake PMU doesn't have proper terms so nothing to configure in attr. */
1685 if (perf_pmu__is_fake(pmu))
1686 return 0;
1687
1688 return perf_pmu__config_terms(pmu, attr, head_terms, zero, apply_hardcoded, err);
1689 }
1690
pmu_find_alias(struct perf_pmu * pmu,struct parse_events_term * term)1691 static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
1692 struct parse_events_term *term)
1693 {
1694 struct perf_pmu_alias *alias;
1695 const char *name;
1696
1697 if (parse_events__is_hardcoded_term(term))
1698 return NULL;
1699
1700 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1701 if (!term->no_value)
1702 return NULL;
1703 if (pmu_find_format(&pmu->format, term->config))
1704 return NULL;
1705 name = term->config;
1706
1707 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1708 if (strcasecmp(term->config, "event"))
1709 return NULL;
1710 name = term->val.str;
1711 } else {
1712 return NULL;
1713 }
1714
1715 alias = perf_pmu__find_alias(pmu, name, /*load=*/ true);
1716 if (alias || pmu->cpu_aliases_added)
1717 return alias;
1718
1719 /* Alias doesn't exist, try to get it from the json events. */
1720 if (pmu->events_table &&
1721 pmu_events_table__find_event(pmu->events_table, pmu, name,
1722 pmu_add_cpu_aliases_map_callback,
1723 pmu) == 0) {
1724 alias = perf_pmu__find_alias(pmu, name, /*load=*/ false);
1725 }
1726 return alias;
1727 }
1728
1729
check_info_data(struct perf_pmu * pmu,struct perf_pmu_alias * alias,struct perf_pmu_info * info,struct parse_events_error * err,int column)1730 static int check_info_data(struct perf_pmu *pmu,
1731 struct perf_pmu_alias *alias,
1732 struct perf_pmu_info *info,
1733 struct parse_events_error *err,
1734 int column)
1735 {
1736 read_alias_info(pmu, alias);
1737 /*
1738 * Only one term in event definition can
1739 * define unit, scale and snapshot, fail
1740 * if there's more than one.
1741 */
1742 if (info->unit && alias->unit[0]) {
1743 parse_events_error__handle(err, column,
1744 strdup("Attempt to set event's unit twice"),
1745 NULL);
1746 return -EINVAL;
1747 }
1748 if (info->scale && alias->scale) {
1749 parse_events_error__handle(err, column,
1750 strdup("Attempt to set event's scale twice"),
1751 NULL);
1752 return -EINVAL;
1753 }
1754 if (info->snapshot && alias->snapshot) {
1755 parse_events_error__handle(err, column,
1756 strdup("Attempt to set event snapshot twice"),
1757 NULL);
1758 return -EINVAL;
1759 }
1760
1761 if (alias->unit[0])
1762 info->unit = alias->unit;
1763
1764 if (alias->scale)
1765 info->scale = alias->scale;
1766
1767 if (alias->snapshot)
1768 info->snapshot = alias->snapshot;
1769
1770 return 0;
1771 }
1772
1773 /*
1774 * Find alias in the terms list and replace it with the terms
1775 * defined for the alias
1776 */
perf_pmu__check_alias(struct perf_pmu * pmu,struct parse_events_terms * head_terms,struct perf_pmu_info * info,bool * rewrote_terms,u64 * alternate_hw_config,struct parse_events_error * err)1777 int perf_pmu__check_alias(struct perf_pmu *pmu, struct parse_events_terms *head_terms,
1778 struct perf_pmu_info *info, bool *rewrote_terms,
1779 u64 *alternate_hw_config, struct parse_events_error *err)
1780 {
1781 struct parse_events_term *term, *h;
1782 struct perf_pmu_alias *alias;
1783 int ret;
1784
1785 *rewrote_terms = false;
1786 info->per_pkg = false;
1787
1788 /*
1789 * Mark unit and scale as not set
1790 * (different from default values, see below)
1791 */
1792 info->unit = NULL;
1793 info->scale = 0.0;
1794 info->snapshot = false;
1795 info->retirement_latency_mean = 0.0;
1796 info->retirement_latency_min = 0.0;
1797 info->retirement_latency_max = 0.0;
1798
1799 if (perf_pmu__is_hwmon(pmu)) {
1800 ret = hwmon_pmu__check_alias(head_terms, info, err);
1801 goto out;
1802 }
1803 if (perf_pmu__is_drm(pmu)) {
1804 ret = drm_pmu__check_alias(pmu, head_terms, info, err);
1805 goto out;
1806 }
1807
1808 /* Fake PMU doesn't rewrite terms. */
1809 if (perf_pmu__is_fake(pmu))
1810 goto out;
1811
1812 list_for_each_entry_safe(term, h, &head_terms->terms, list) {
1813 alias = pmu_find_alias(pmu, term);
1814 if (!alias)
1815 continue;
1816 ret = pmu_alias_terms(alias, term->err_term, &term->list);
1817 if (ret) {
1818 parse_events_error__handle(err, term->err_term,
1819 strdup("Failure to duplicate terms"),
1820 NULL);
1821 return ret;
1822 }
1823
1824 *rewrote_terms = true;
1825 ret = check_info_data(pmu, alias, info, err, term->err_term);
1826 if (ret)
1827 return ret;
1828
1829 if (alias->per_pkg)
1830 info->per_pkg = true;
1831
1832 if (term->alternate_hw_config)
1833 *alternate_hw_config = term->val.num;
1834
1835 info->retirement_latency_mean = alias->retirement_latency_mean;
1836 info->retirement_latency_min = alias->retirement_latency_min;
1837 info->retirement_latency_max = alias->retirement_latency_max;
1838
1839 list_del_init(&term->list);
1840 parse_events_term__delete(term);
1841 }
1842 out:
1843 /*
1844 * if no unit or scale found in aliases, then
1845 * set defaults as for evsel
1846 * unit cannot left to NULL
1847 */
1848 if (info->unit == NULL)
1849 info->unit = "";
1850
1851 if (info->scale == 0.0)
1852 info->scale = 1.0;
1853
1854 return 0;
1855 }
1856
1857 struct find_event_args {
1858 const char *event;
1859 void *state;
1860 pmu_event_callback cb;
1861 };
1862
find_event_callback(void * state,struct pmu_event_info * info)1863 static int find_event_callback(void *state, struct pmu_event_info *info)
1864 {
1865 struct find_event_args *args = state;
1866
1867 if (!strcmp(args->event, info->name))
1868 return args->cb(args->state, info);
1869
1870 return 0;
1871 }
1872
perf_pmu__find_event(struct perf_pmu * pmu,const char * event,void * state,pmu_event_callback cb)1873 int perf_pmu__find_event(struct perf_pmu *pmu, const char *event, void *state, pmu_event_callback cb)
1874 {
1875 struct find_event_args args = {
1876 .event = event,
1877 .state = state,
1878 .cb = cb,
1879 };
1880
1881 /* Sub-optimal, but function is only used by tests. */
1882 return perf_pmu__for_each_event(pmu, /*skip_duplicate_pmus=*/ false,
1883 &args, find_event_callback);
1884 }
1885
perf_pmu__del_formats(struct list_head * formats)1886 static void perf_pmu__del_formats(struct list_head *formats)
1887 {
1888 struct perf_pmu_format *fmt, *tmp;
1889
1890 list_for_each_entry_safe(fmt, tmp, formats, list) {
1891 list_del(&fmt->list);
1892 zfree(&fmt->name);
1893 free(fmt);
1894 }
1895 }
1896
perf_pmu__has_format(const struct perf_pmu * pmu,const char * name)1897 bool perf_pmu__has_format(const struct perf_pmu *pmu, const char *name)
1898 {
1899 struct perf_pmu_format *format;
1900
1901 list_for_each_entry(format, &pmu->format, list) {
1902 if (!strcmp(format->name, name))
1903 return true;
1904 }
1905 return false;
1906 }
1907
perf_pmu__for_each_format(struct perf_pmu * pmu,void * state,pmu_format_callback cb)1908 int perf_pmu__for_each_format(struct perf_pmu *pmu, void *state, pmu_format_callback cb)
1909 {
1910 static const char *const terms[] = {
1911 "config=0..0xffffffffffffffff",
1912 "config1=0..0xffffffffffffffff",
1913 "config2=0..0xffffffffffffffff",
1914 "config3=0..0xffffffffffffffff",
1915 "name=string",
1916 "period=number",
1917 "freq=number",
1918 "branch_type=(u|k|hv|any|...)",
1919 "time",
1920 "call-graph=(fp|dwarf|lbr)",
1921 "stack-size=number",
1922 "max-stack=number",
1923 "nr=number",
1924 "inherit",
1925 "no-inherit",
1926 "overwrite",
1927 "no-overwrite",
1928 "percore",
1929 "aux-output",
1930 "aux-action=(pause|resume|start-paused)",
1931 "aux-sample-size=number",
1932 "cpu=number",
1933 };
1934 struct perf_pmu_format *format;
1935 int ret;
1936
1937 /*
1938 * max-events and driver-config are missing above as are the internal
1939 * types user, metric-id, raw, legacy cache and hardware. Assert against
1940 * the enum parse_events__term_type so they are kept in sync.
1941 */
1942 _Static_assert(ARRAY_SIZE(terms) == __PARSE_EVENTS__TERM_TYPE_NR - 6,
1943 "perf_pmu__for_each_format()'s terms must be kept in sync with enum parse_events__term_type");
1944 list_for_each_entry(format, &pmu->format, list) {
1945 perf_pmu_format__load(pmu, format);
1946 ret = cb(state, format->name, (int)format->value, format->bits);
1947 if (ret)
1948 return ret;
1949 }
1950 if (!pmu->is_core)
1951 return 0;
1952
1953 for (size_t i = 0; i < ARRAY_SIZE(terms); i++) {
1954 int config = PERF_PMU_FORMAT_VALUE_CONFIG;
1955
1956 if (i < PERF_PMU_FORMAT_VALUE_CONFIG_END)
1957 config = i;
1958
1959 ret = cb(state, terms[i], config, /*bits=*/NULL);
1960 if (ret)
1961 return ret;
1962 }
1963 return 0;
1964 }
1965
is_pmu_core(const char * name)1966 bool is_pmu_core(const char *name)
1967 {
1968 return !strcmp(name, "cpu") || !strcmp(name, "cpum_cf") || is_sysfs_pmu_core(name);
1969 }
1970
perf_pmu__supports_legacy_cache(const struct perf_pmu * pmu)1971 bool perf_pmu__supports_legacy_cache(const struct perf_pmu *pmu)
1972 {
1973 return pmu->is_core;
1974 }
1975
perf_pmu__auto_merge_stats(const struct perf_pmu * pmu)1976 bool perf_pmu__auto_merge_stats(const struct perf_pmu *pmu)
1977 {
1978 return !pmu->is_core || perf_pmus__num_core_pmus() == 1;
1979 }
1980
perf_pmu__have_event(struct perf_pmu * pmu,const char * name)1981 bool perf_pmu__have_event(struct perf_pmu *pmu, const char *name)
1982 {
1983 if (!name)
1984 return false;
1985 if (perf_pmu__is_tool(pmu) && tool_pmu__skip_event(name))
1986 return false;
1987 if (perf_pmu__is_tracepoint(pmu))
1988 return tp_pmu__have_event(pmu, name);
1989 if (perf_pmu__is_hwmon(pmu))
1990 return hwmon_pmu__have_event(pmu, name);
1991 if (perf_pmu__is_drm(pmu))
1992 return drm_pmu__have_event(pmu, name);
1993 if (perf_pmu__find_alias(pmu, name, /*load=*/ true) != NULL)
1994 return true;
1995 if (pmu->cpu_aliases_added || !pmu->events_table)
1996 return false;
1997 return pmu_events_table__find_event(pmu->events_table, pmu, name, NULL, NULL) == 0;
1998 }
1999
perf_pmu__num_events(struct perf_pmu * pmu)2000 size_t perf_pmu__num_events(struct perf_pmu *pmu)
2001 {
2002 size_t nr;
2003
2004 if (perf_pmu__is_tracepoint(pmu))
2005 return tp_pmu__num_events(pmu);
2006 if (perf_pmu__is_hwmon(pmu))
2007 return hwmon_pmu__num_events(pmu);
2008 if (perf_pmu__is_drm(pmu))
2009 return drm_pmu__num_events(pmu);
2010
2011 pmu_aliases_parse(pmu);
2012 nr = pmu->sysfs_aliases + pmu->sys_json_aliases;
2013
2014 if (pmu->cpu_aliases_added)
2015 nr += pmu->cpu_json_aliases;
2016 else if (pmu->events_table)
2017 nr += pmu_events_table__num_events(pmu->events_table, pmu) -
2018 pmu->cpu_common_json_aliases;
2019 else
2020 assert(pmu->cpu_json_aliases == 0 && pmu->cpu_common_json_aliases == 0);
2021
2022 if (perf_pmu__is_tool(pmu))
2023 nr -= tool_pmu__num_skip_events();
2024
2025 return pmu->selectable ? nr + 1 : nr;
2026 }
2027
sub_non_neg(int a,int b)2028 static int sub_non_neg(int a, int b)
2029 {
2030 if (b > a)
2031 return 0;
2032 return a - b;
2033 }
2034
format_alias(char * buf,int len,const struct perf_pmu * pmu,const struct perf_pmu_alias * alias,bool skip_duplicate_pmus)2035 static char *format_alias(char *buf, int len, const struct perf_pmu *pmu,
2036 const struct perf_pmu_alias *alias, bool skip_duplicate_pmus)
2037 {
2038 struct parse_events_term *term;
2039 size_t pmu_name_len = pmu_deduped_name_len(pmu, pmu->name,
2040 skip_duplicate_pmus);
2041 int used = snprintf(buf, len, "%.*s/%s", (int)pmu_name_len, pmu->name, alias->name);
2042
2043 list_for_each_entry(term, &alias->terms.terms, list) {
2044 if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
2045 used += snprintf(buf + used, sub_non_neg(len, used),
2046 ",%s=%s", term->config,
2047 term->val.str);
2048 }
2049
2050 if (sub_non_neg(len, used) > 0) {
2051 buf[used] = '/';
2052 used++;
2053 }
2054 if (sub_non_neg(len, used) > 0) {
2055 buf[used] = '\0';
2056 used++;
2057 } else
2058 buf[len - 1] = '\0';
2059
2060 return buf;
2061 }
2062
perf_pmu__for_each_event(struct perf_pmu * pmu,bool skip_duplicate_pmus,void * state,pmu_event_callback cb)2063 int perf_pmu__for_each_event(struct perf_pmu *pmu, bool skip_duplicate_pmus,
2064 void *state, pmu_event_callback cb)
2065 {
2066 char buf[1024];
2067 struct pmu_event_info info = {
2068 .pmu = pmu,
2069 .event_type_desc = "Kernel PMU event",
2070 };
2071 int ret = 0;
2072 struct strbuf sb;
2073 struct hashmap_entry *entry;
2074 size_t bkt;
2075
2076 if (perf_pmu__is_tracepoint(pmu))
2077 return tp_pmu__for_each_event(pmu, state, cb);
2078 if (perf_pmu__is_hwmon(pmu))
2079 return hwmon_pmu__for_each_event(pmu, state, cb);
2080 if (perf_pmu__is_drm(pmu))
2081 return drm_pmu__for_each_event(pmu, state, cb);
2082
2083 strbuf_init(&sb, /*hint=*/ 0);
2084 pmu_aliases_parse(pmu);
2085 pmu_add_cpu_aliases(pmu);
2086 hashmap__for_each_entry(pmu->aliases, entry, bkt) {
2087 struct perf_pmu_alias *event = entry->pvalue;
2088 size_t buf_used, pmu_name_len;
2089
2090 if (perf_pmu__is_tool(pmu) && tool_pmu__skip_event(event->name))
2091 continue;
2092
2093 info.pmu_name = event->pmu_name ?: pmu->name;
2094 pmu_name_len = pmu_deduped_name_len(pmu, info.pmu_name,
2095 skip_duplicate_pmus);
2096 info.alias = NULL;
2097 if (event->desc) {
2098 info.name = event->name;
2099 buf_used = 0;
2100 } else {
2101 info.name = format_alias(buf, sizeof(buf), pmu, event,
2102 skip_duplicate_pmus);
2103 if (pmu->is_core) {
2104 info.alias = info.name;
2105 info.name = event->name;
2106 }
2107 buf_used = strlen(buf) + 1;
2108 }
2109 info.scale_unit = NULL;
2110 if (strlen(event->unit) || event->scale != 1.0) {
2111 info.scale_unit = buf + buf_used;
2112 buf_used += snprintf(buf + buf_used, sizeof(buf) - buf_used,
2113 "%G%s", event->scale, event->unit) + 1;
2114 }
2115 info.desc = event->desc;
2116 info.long_desc = event->long_desc;
2117 info.encoding_desc = buf + buf_used;
2118 parse_events_terms__to_strbuf(&event->terms, &sb);
2119 buf_used += snprintf(buf + buf_used, sizeof(buf) - buf_used,
2120 "%.*s/%s/", (int)pmu_name_len, info.pmu_name, sb.buf) + 1;
2121 info.topic = event->topic;
2122 info.str = sb.buf;
2123 info.deprecated = event->deprecated;
2124 ret = cb(state, &info);
2125 if (ret)
2126 goto out;
2127 strbuf_setlen(&sb, /*len=*/ 0);
2128 }
2129 if (pmu->selectable) {
2130 info.name = buf;
2131 snprintf(buf, sizeof(buf), "%s//", pmu->name);
2132 info.alias = NULL;
2133 info.scale_unit = NULL;
2134 info.desc = NULL;
2135 info.long_desc = NULL;
2136 info.encoding_desc = NULL;
2137 info.topic = NULL;
2138 info.pmu_name = pmu->name;
2139 info.deprecated = false;
2140 ret = cb(state, &info);
2141 }
2142 out:
2143 strbuf_release(&sb);
2144 return ret;
2145 }
2146
perf_pmu___name_match(const struct perf_pmu * pmu,const char * to_match,bool wildcard)2147 static bool perf_pmu___name_match(const struct perf_pmu *pmu, const char *to_match, bool wildcard)
2148 {
2149 const char *names[2] = {
2150 pmu->name,
2151 pmu->alias_name,
2152 };
2153 if (pmu->is_core) {
2154 for (size_t i = 0; i < ARRAY_SIZE(names); i++) {
2155 const char *name = names[i];
2156
2157 if (!name)
2158 continue;
2159
2160 if (!strcmp(name, to_match)) {
2161 /* Exact name match. */
2162 return true;
2163 }
2164 }
2165 if (!strcmp(to_match, "default_core")) {
2166 /*
2167 * jevents and tests use default_core as a marker for any core
2168 * PMU as the PMU name varies across architectures.
2169 */
2170 return true;
2171 }
2172 return false;
2173 }
2174 if (!pmu->is_uncore) {
2175 /*
2176 * PMU isn't core or uncore, some kind of broken CPU mask
2177 * situation. Only match exact name.
2178 */
2179 for (size_t i = 0; i < ARRAY_SIZE(names); i++) {
2180 const char *name = names[i];
2181
2182 if (!name)
2183 continue;
2184
2185 if (!strcmp(name, to_match)) {
2186 /* Exact name match. */
2187 return true;
2188 }
2189 }
2190 return false;
2191 }
2192 for (size_t i = 0; i < ARRAY_SIZE(names); i++) {
2193 const char *name = names[i];
2194
2195 if (!name)
2196 continue;
2197
2198 if (wildcard && perf_pmu__match_wildcard_uncore(name, to_match))
2199 return true;
2200 if (!wildcard && perf_pmu__match_ignoring_suffix_uncore(name, to_match))
2201 return true;
2202 }
2203 return false;
2204 }
2205
2206 /**
2207 * perf_pmu__name_wildcard_match - Called by the jevents generated code to see
2208 * if pmu matches the json to_match string.
2209 * @pmu: The pmu whose name/alias to match.
2210 * @to_match: The possible match to pmu_name.
2211 */
perf_pmu__name_wildcard_match(const struct perf_pmu * pmu,const char * to_match)2212 bool perf_pmu__name_wildcard_match(const struct perf_pmu *pmu, const char *to_match)
2213 {
2214 return perf_pmu___name_match(pmu, to_match, /*wildcard=*/true);
2215 }
2216
2217 /**
2218 * perf_pmu__name_no_suffix_match - Does pmu's name match to_match ignoring any
2219 * trailing suffix on the pmu_name and/or tok?
2220 * @pmu: The pmu whose name/alias to match.
2221 * @to_match: The possible match to pmu_name.
2222 */
perf_pmu__name_no_suffix_match(const struct perf_pmu * pmu,const char * to_match)2223 bool perf_pmu__name_no_suffix_match(const struct perf_pmu *pmu, const char *to_match)
2224 {
2225 return perf_pmu___name_match(pmu, to_match, /*wildcard=*/false);
2226 }
2227
perf_pmu__is_software(const struct perf_pmu * pmu)2228 bool perf_pmu__is_software(const struct perf_pmu *pmu)
2229 {
2230 const char *known_sw_pmus[] = {
2231 "kprobe",
2232 "msr",
2233 "uprobe",
2234 };
2235
2236 if (pmu->is_core || pmu->is_uncore || pmu->auxtrace)
2237 return false;
2238 switch (pmu->type) {
2239 case PERF_TYPE_HARDWARE: return false;
2240 case PERF_TYPE_SOFTWARE: return true;
2241 case PERF_TYPE_TRACEPOINT: return true;
2242 case PERF_TYPE_HW_CACHE: return false;
2243 case PERF_TYPE_RAW: return false;
2244 case PERF_TYPE_BREAKPOINT: return true;
2245 case PERF_PMU_TYPE_TOOL: return true;
2246 default: break;
2247 }
2248 for (size_t i = 0; i < ARRAY_SIZE(known_sw_pmus); i++) {
2249 if (!strcmp(pmu->name, known_sw_pmus[i]))
2250 return true;
2251 }
2252 return false;
2253 }
2254
perf_pmu__open_file(const struct perf_pmu * pmu,const char * name)2255 FILE *perf_pmu__open_file(const struct perf_pmu *pmu, const char *name)
2256 {
2257 char path[PATH_MAX];
2258
2259 if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, name) ||
2260 !file_available(path))
2261 return NULL;
2262
2263 return fopen(path, "r");
2264 }
2265
perf_pmu__open_file_at(const struct perf_pmu * pmu,int dirfd,const char * name)2266 FILE *perf_pmu__open_file_at(const struct perf_pmu *pmu, int dirfd, const char *name)
2267 {
2268 int fd;
2269
2270 fd = perf_pmu__pathname_fd(dirfd, pmu->name, name, O_RDONLY);
2271 if (fd < 0)
2272 return NULL;
2273
2274 return fdopen(fd, "r");
2275 }
2276
perf_pmu__scan_file(const struct perf_pmu * pmu,const char * name,const char * fmt,...)2277 int perf_pmu__scan_file(const struct perf_pmu *pmu, const char *name, const char *fmt,
2278 ...)
2279 {
2280 va_list args;
2281 FILE *file;
2282 int ret = EOF;
2283
2284 va_start(args, fmt);
2285 file = perf_pmu__open_file(pmu, name);
2286 if (file) {
2287 ret = vfscanf(file, fmt, args);
2288 fclose(file);
2289 }
2290 va_end(args);
2291 return ret;
2292 }
2293
perf_pmu__scan_file_at(const struct perf_pmu * pmu,int dirfd,const char * name,const char * fmt,...)2294 int perf_pmu__scan_file_at(const struct perf_pmu *pmu, int dirfd, const char *name,
2295 const char *fmt, ...)
2296 {
2297 va_list args;
2298 FILE *file;
2299 int ret = EOF;
2300
2301 va_start(args, fmt);
2302 file = perf_pmu__open_file_at(pmu, dirfd, name);
2303 if (file) {
2304 ret = vfscanf(file, fmt, args);
2305 fclose(file);
2306 }
2307 va_end(args);
2308 return ret;
2309 }
2310
perf_pmu__file_exists(const struct perf_pmu * pmu,const char * name)2311 bool perf_pmu__file_exists(const struct perf_pmu *pmu, const char *name)
2312 {
2313 char path[PATH_MAX];
2314
2315 if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, name))
2316 return false;
2317
2318 return file_available(path);
2319 }
2320
perf_pmu__new_caps(struct list_head * list,char * name,char * value)2321 static int perf_pmu__new_caps(struct list_head *list, char *name, char *value)
2322 {
2323 struct perf_pmu_caps *caps = zalloc(sizeof(*caps));
2324
2325 if (!caps)
2326 return -ENOMEM;
2327
2328 caps->name = strdup(name);
2329 if (!caps->name)
2330 goto free_caps;
2331 caps->value = strndup(value, strlen(value) - 1);
2332 if (!caps->value)
2333 goto free_name;
2334 list_add_tail(&caps->list, list);
2335 return 0;
2336
2337 free_name:
2338 zfree(&caps->name);
2339 free_caps:
2340 free(caps);
2341
2342 return -ENOMEM;
2343 }
2344
perf_pmu__del_caps(struct perf_pmu * pmu)2345 static void perf_pmu__del_caps(struct perf_pmu *pmu)
2346 {
2347 struct perf_pmu_caps *caps, *tmp;
2348
2349 list_for_each_entry_safe(caps, tmp, &pmu->caps, list) {
2350 list_del(&caps->list);
2351 zfree(&caps->name);
2352 zfree(&caps->value);
2353 free(caps);
2354 }
2355 }
2356
perf_pmu__get_cap(struct perf_pmu * pmu,const char * name)2357 struct perf_pmu_caps *perf_pmu__get_cap(struct perf_pmu *pmu, const char *name)
2358 {
2359 struct perf_pmu_caps *caps;
2360
2361 list_for_each_entry(caps, &pmu->caps, list) {
2362 if (!strcmp(caps->name, name))
2363 return caps;
2364 }
2365 return NULL;
2366 }
2367
2368 /*
2369 * Reading/parsing the given pmu capabilities, which should be located at:
2370 * /sys/bus/event_source/devices/<dev>/caps as sysfs group attributes.
2371 * Return the number of capabilities
2372 */
perf_pmu__caps_parse(struct perf_pmu * pmu)2373 int perf_pmu__caps_parse(struct perf_pmu *pmu)
2374 {
2375 char caps_path[PATH_MAX];
2376 struct io_dir caps_dir;
2377 struct io_dirent64 *evt_ent;
2378 int caps_fd;
2379
2380 if (pmu->caps_initialized)
2381 return pmu->nr_caps;
2382
2383 pmu->nr_caps = 0;
2384
2385 if (!perf_pmu__pathname_scnprintf(caps_path, sizeof(caps_path), pmu->name, "caps"))
2386 return -1;
2387
2388 caps_fd = open(caps_path, O_CLOEXEC | O_DIRECTORY | O_RDONLY);
2389 if (caps_fd == -1) {
2390 pmu->caps_initialized = true;
2391 return 0; /* no error if caps does not exist */
2392 }
2393
2394 io_dir__init(&caps_dir, caps_fd);
2395
2396 while ((evt_ent = io_dir__readdir(&caps_dir)) != NULL) {
2397 char *name = evt_ent->d_name;
2398 char value[128];
2399 FILE *file;
2400 int fd;
2401
2402 if (io_dir__is_dir(&caps_dir, evt_ent))
2403 continue;
2404
2405 fd = openat(caps_fd, name, O_RDONLY);
2406 if (fd == -1)
2407 continue;
2408 file = fdopen(fd, "r");
2409 if (!file) {
2410 close(fd);
2411 continue;
2412 }
2413
2414 if (!fgets(value, sizeof(value), file) ||
2415 (perf_pmu__new_caps(&pmu->caps, name, value) < 0)) {
2416 fclose(file);
2417 continue;
2418 }
2419
2420 pmu->nr_caps++;
2421 fclose(file);
2422 }
2423
2424 close(caps_fd);
2425
2426 pmu->caps_initialized = true;
2427 return pmu->nr_caps;
2428 }
2429
perf_pmu__compute_config_masks(struct perf_pmu * pmu)2430 static void perf_pmu__compute_config_masks(struct perf_pmu *pmu)
2431 {
2432 struct perf_pmu_format *format;
2433
2434 if (pmu->config_masks_computed)
2435 return;
2436
2437 list_for_each_entry(format, &pmu->format, list) {
2438 unsigned int i;
2439 __u64 *mask;
2440
2441 if (format->value >= PERF_PMU_FORMAT_VALUE_CONFIG_END)
2442 continue;
2443
2444 pmu->config_masks_present = true;
2445 mask = &pmu->config_masks[format->value];
2446
2447 for_each_set_bit(i, format->bits, PERF_PMU_FORMAT_BITS)
2448 *mask |= 1ULL << i;
2449 }
2450 pmu->config_masks_computed = true;
2451 }
2452
perf_pmu__warn_invalid_config(struct perf_pmu * pmu,__u64 config,const char * name,int config_num,const char * config_name)2453 void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config,
2454 const char *name, int config_num,
2455 const char *config_name)
2456 {
2457 __u64 bits;
2458 char buf[100];
2459
2460 perf_pmu__compute_config_masks(pmu);
2461
2462 /*
2463 * Kernel doesn't export any valid format bits.
2464 */
2465 if (!pmu->config_masks_present)
2466 return;
2467
2468 bits = config & ~pmu->config_masks[config_num];
2469 if (bits == 0)
2470 return;
2471
2472 bitmap_scnprintf((unsigned long *)&bits, sizeof(bits) * 8, buf, sizeof(buf));
2473
2474 pr_warning("WARNING: event '%s' not valid (bits %s of %s "
2475 "'%llx' not supported by kernel)!\n",
2476 name ?: "N/A", buf, config_name, config);
2477 }
2478
perf_pmu__wildcard_match(const struct perf_pmu * pmu,const char * wildcard_to_match)2479 bool perf_pmu__wildcard_match(const struct perf_pmu *pmu, const char *wildcard_to_match)
2480 {
2481 const char *names[2] = {
2482 pmu->name,
2483 pmu->alias_name,
2484 };
2485 bool need_fnmatch = strisglob(wildcard_to_match);
2486
2487 if (!strncmp(wildcard_to_match, "uncore_", 7))
2488 wildcard_to_match += 7;
2489
2490 for (size_t i = 0; i < ARRAY_SIZE(names); i++) {
2491 const char *pmu_name = names[i];
2492
2493 if (!pmu_name)
2494 continue;
2495
2496 if (!strncmp(pmu_name, "uncore_", 7))
2497 pmu_name += 7;
2498
2499 if (perf_pmu__match_wildcard(pmu_name, wildcard_to_match) ||
2500 (need_fnmatch && !fnmatch(wildcard_to_match, pmu_name, 0)))
2501 return true;
2502 }
2503 return false;
2504 }
2505
perf_pmu__event_source_devices_scnprintf(char * pathname,size_t size)2506 int perf_pmu__event_source_devices_scnprintf(char *pathname, size_t size)
2507 {
2508 const char *sysfs = sysfs__mountpoint();
2509
2510 if (!sysfs)
2511 return 0;
2512 return scnprintf(pathname, size, "%s/bus/event_source/devices/", sysfs);
2513 }
2514
perf_pmu__event_source_devices_fd(void)2515 int perf_pmu__event_source_devices_fd(void)
2516 {
2517 char path[PATH_MAX];
2518 const char *sysfs = sysfs__mountpoint();
2519
2520 if (!sysfs)
2521 return -1;
2522
2523 scnprintf(path, sizeof(path), "%s/bus/event_source/devices/", sysfs);
2524 return open(path, O_DIRECTORY);
2525 }
2526
2527 /*
2528 * Fill 'buf' with the path to a file or folder in 'pmu_name' in
2529 * sysfs. For example if pmu_name = "cs_etm" and 'filename' = "format"
2530 * then pathname will be filled with
2531 * "/sys/bus/event_source/devices/cs_etm/format"
2532 *
2533 * Return 0 if the sysfs mountpoint couldn't be found, if no characters were
2534 * written or if the buffer size is exceeded.
2535 */
perf_pmu__pathname_scnprintf(char * buf,size_t size,const char * pmu_name,const char * filename)2536 int perf_pmu__pathname_scnprintf(char *buf, size_t size,
2537 const char *pmu_name, const char *filename)
2538 {
2539 size_t len;
2540
2541 len = perf_pmu__event_source_devices_scnprintf(buf, size);
2542 if (!len || (len + strlen(pmu_name) + strlen(filename) + 1) >= size)
2543 return 0;
2544
2545 return scnprintf(buf + len, size - len, "%s/%s", pmu_name, filename);
2546 }
2547
perf_pmu__pathname_fd(int dirfd,const char * pmu_name,const char * filename,int flags)2548 int perf_pmu__pathname_fd(int dirfd, const char *pmu_name, const char *filename, int flags)
2549 {
2550 char path[PATH_MAX];
2551
2552 scnprintf(path, sizeof(path), "%s/%s", pmu_name, filename);
2553 return openat(dirfd, path, flags);
2554 }
2555
perf_pmu__delete(struct perf_pmu * pmu)2556 void perf_pmu__delete(struct perf_pmu *pmu)
2557 {
2558 if (!pmu)
2559 return;
2560
2561 if (perf_pmu__is_hwmon(pmu))
2562 hwmon_pmu__exit(pmu);
2563 else if (perf_pmu__is_drm(pmu))
2564 drm_pmu__exit(pmu);
2565
2566 perf_pmu__del_formats(&pmu->format);
2567 perf_pmu__del_aliases(pmu);
2568 perf_pmu__del_caps(pmu);
2569
2570 perf_cpu_map__put(pmu->cpus);
2571
2572 zfree(&pmu->name);
2573 zfree(&pmu->alias_name);
2574 zfree(&pmu->id);
2575 free(pmu);
2576 }
2577
perf_pmu__name_from_config(struct perf_pmu * pmu,u64 config)2578 const char *perf_pmu__name_from_config(struct perf_pmu *pmu, u64 config)
2579 {
2580 struct hashmap_entry *entry;
2581 size_t bkt;
2582
2583 if (!pmu)
2584 return NULL;
2585
2586 pmu_aliases_parse(pmu);
2587 pmu_add_cpu_aliases(pmu);
2588 hashmap__for_each_entry(pmu->aliases, entry, bkt) {
2589 struct perf_pmu_alias *event = entry->pvalue;
2590 struct perf_event_attr attr = {.config = 0,};
2591
2592 int ret = perf_pmu__config(pmu, &attr, &event->terms, /*apply_hardcoded=*/true,
2593 /*err=*/NULL);
2594
2595 if (ret == 0 && config == attr.config)
2596 return event->name;
2597 }
2598 return NULL;
2599 }
2600