Merge tag 'cgroup-for-6.11-rc4-fixes' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-block.git] / tools / perf / util / pmu.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
cd82a32e 2#include <linux/list.h>
c5de47f2 3#include <linux/compiler.h>
32858480 4#include <linux/string.h>
7f7c536f 5#include <linux/zalloc.h>
c47a5599 6#include <linux/ctype.h>
cd82a32e 7#include <sys/types.h>
c23c2a0f 8#include <fcntl.h>
7a8ef4c4 9#include <sys/stat.h>
cd82a32e
JO
10#include <unistd.h>
11#include <stdio.h>
dc0a6202 12#include <stdbool.h>
cd82a32e 13#include <dirent.h>
cd0cfad7 14#include <api/fs/fs.h>
410136f5 15#include <locale.h>
c47a5599 16#include <fnmatch.h>
acef233b 17#include <math.h>
5e51b0bb 18#include "debug.h"
e12ee9f7 19#include "evsel.h"
cd82a32e 20#include "pmu.h"
336b92da 21#include "pmus.h"
c7e97f21
NK
22#include <util/pmu-bison.h>
23#include <util/pmu-flex.h>
cd82a32e 24#include "parse-events.h"
e5c6109f 25#include "print-events.h"
933f82ff 26#include "header.h"
a067558e 27#include "string2.h"
fa0d9846 28#include "strbuf.h"
d9664582 29#include "fncache.h"
6593f019 30#include "util/evsel_config.h"
2879ff36 31#include <regex.h>
cd82a32e 32
c091ee90
IR
33struct perf_pmu perf_pmu__fake = {
34 .name = "fake",
35};
e46fc8d9 36
c3245d20
IR
37#define UNIT_MAX_LEN 31 /* max length for event unit name */
38
d9c5f5f9
IR
39enum event_source {
40 /* An event loaded from /sys/devices/<pmu>/events. */
41 EVENT_SRC_SYSFS,
42 /* An event loaded from a CPUID matched json file. */
43 EVENT_SRC_CPU_JSON,
44 /*
45 * An event loaded from a /sys/devices/<pmu>/identifier matched json
46 * file.
47 */
48 EVENT_SRC_SYS_JSON,
49};
50
c3245d20
IR
51/**
52 * struct perf_pmu_alias - An event either read from sysfs or builtin in
53 * pmu-events.c, created by parsing the pmu-events json files.
54 */
55struct perf_pmu_alias {
56 /** @name: Name of the event like "mem-loads". */
57 char *name;
58 /** @desc: Optional short description of the event. */
59 char *desc;
60 /** @long_desc: Optional long description. */
61 char *long_desc;
62 /**
63 * @topic: Optional topic such as cache or pipeline, particularly for
64 * json events.
65 */
66 char *topic;
c3245d20 67 /** @terms: Owned list of the original parsed parameters. */
0d3f0e6f 68 struct parse_events_terms terms;
c3245d20
IR
69 /** @list: List element of struct perf_pmu aliases. */
70 struct list_head list;
7b723dbb
IR
71 /**
72 * @pmu_name: The name copied from the json struct pmu_event. This can
73 * differ from the PMU name as it won't have suffixes.
74 */
75 char *pmu_name;
c3245d20
IR
76 /** @unit: Units for the event, such as bytes or cache lines. */
77 char unit[UNIT_MAX_LEN+1];
78 /** @scale: Value to scale read counter values by. */
79 double scale;
80 /**
81 * @per_pkg: Does the file
82 * <sysfs>/bus/event_source/devices/<pmu_name>/events/<name>.per-pkg or
83 * equivalent json value exist and have the value 1.
84 */
85 bool per_pkg;
86 /**
87 * @snapshot: Does the file
88 * <sysfs>/bus/event_source/devices/<pmu_name>/events/<name>.snapshot
89 * exist and have the value 1.
90 */
91 bool snapshot;
92 /**
93 * @deprecated: Is the event hidden and so not shown in perf list by
94 * default.
95 */
96 bool deprecated;
7b723dbb
IR
97 /** @from_sysfs: Was the alias from sysfs or a json event? */
98 bool from_sysfs;
99 /** @info_loaded: Have the scale, unit and other values been read from disk? */
100 bool info_loaded;
c3245d20
IR
101};
102
fe13d43d
IR
103/**
104 * struct perf_pmu_format - Values from a format file read from
105 * <sysfs>/devices/cpu/format/ held in struct perf_pmu.
106 *
107 * For example, the contents of <sysfs>/devices/cpu/format/event may be
108 * "config:0-7" and will be represented here as name="event",
109 * value=PERF_PMU_FORMAT_VALUE_CONFIG and bits 0 to 7 will be set.
110 */
ab1bf653 111struct perf_pmu_format {
50402641
IR
112 /** @list: Element on list within struct perf_pmu. */
113 struct list_head list;
114 /** @bits: Which config bits are set by this format value. */
115 DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
fe13d43d 116 /** @name: The modifier/file name. */
ab1bf653 117 char *name;
fe13d43d
IR
118 /**
119 * @value : Which config value the format relates to. Supported values
120 * are from PERF_PMU_FORMAT_VALUE_CONFIG to
121 * PERF_PMU_FORMAT_VALUE_CONFIG_END.
122 */
50402641
IR
123 u16 value;
124 /** @loaded: Has the contents been loaded/parsed. */
125 bool loaded;
ab1bf653
ACM
126};
127
8d4b6d37
IR
128static int pmu_aliases_parse(struct perf_pmu *pmu);
129
50402641
IR
130static struct perf_pmu_format *perf_pmu__new_format(struct list_head *list, char *name)
131{
132 struct perf_pmu_format *format;
133
134 format = zalloc(sizeof(*format));
135 if (!format)
136 return NULL;
137
138 format->name = strdup(name);
139 if (!format->name) {
140 free(format);
141 return NULL;
142 }
143 list_add_tail(&format->list, list);
144 return format;
145}
146
147/* Called at the end of parsing a format. */
148void perf_pmu_format__set_value(void *vformat, int config, unsigned long *bits)
149{
150 struct perf_pmu_format *format = vformat;
151
152 format->value = config;
153 memcpy(format->bits, bits, sizeof(format->bits));
154}
155
156static void __perf_pmu_format__load(struct perf_pmu_format *format, FILE *file)
157{
158 void *scanner;
159 int ret;
160
161 ret = perf_pmu_lex_init(&scanner);
162 if (ret)
163 return;
164
165 perf_pmu_set_in(file, scanner);
166 ret = perf_pmu_parse(format, scanner);
167 perf_pmu_lex_destroy(scanner);
168 format->loaded = true;
169}
170
63883cb0 171static void perf_pmu_format__load(const struct perf_pmu *pmu, struct perf_pmu_format *format)
50402641
IR
172{
173 char path[PATH_MAX];
174 FILE *file = NULL;
175
176 if (format->loaded)
177 return;
178
179 if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, "format"))
180 return;
181
182 assert(strlen(path) + strlen(format->name) + 2 < sizeof(path));
183 strcat(path, "/");
184 strcat(path, format->name);
185
186 file = fopen(path, "r");
187 if (!file)
188 return;
189 __perf_pmu_format__load(format, file);
190 fclose(file);
191}
192
cd82a32e
JO
193/*
194 * Parse & process all the sysfs attributes located under
195 * the directory specified in 'dir' parameter.
196 */
aa1551f2 197static int perf_pmu__format_parse(struct perf_pmu *pmu, int dirfd, bool eager_load)
cd82a32e
JO
198{
199 struct dirent *evt_ent;
200 DIR *format_dir;
201 int ret = 0;
202
e293a5e8 203 format_dir = fdopendir(dirfd);
cd82a32e
JO
204 if (!format_dir)
205 return -EINVAL;
206
50402641
IR
207 while ((evt_ent = readdir(format_dir)) != NULL) {
208 struct perf_pmu_format *format;
cd82a32e 209 char *name = evt_ent->d_name;
cd82a32e
JO
210
211 if (!strcmp(name, ".") || !strcmp(name, ".."))
212 continue;
213
50402641
IR
214 format = perf_pmu__new_format(&pmu->format, name);
215 if (!format) {
216 ret = -ENOMEM;
3d88aec0
IR
217 break;
218 }
219
50402641
IR
220 if (eager_load) {
221 FILE *file;
222 int fd = openat(dirfd, name, O_RDONLY);
223
224 if (fd < 0) {
225 ret = -errno;
226 break;
227 }
228 file = fdopen(fd, "r");
229 if (!file) {
230 close(fd);
231 break;
232 }
233 __perf_pmu_format__load(format, file);
3d88aec0 234 fclose(file);
3d88aec0 235 }
cd82a32e
JO
236 }
237
238 closedir(format_dir);
239 return ret;
240}
241
242/*
243 * Reading/parsing the default pmu format definition, which should be
244 * located at:
245 * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
246 */
aa1551f2 247static int pmu_format(struct perf_pmu *pmu, int dirfd, const char *name, bool eager_load)
cd82a32e 248{
e293a5e8 249 int fd;
cd82a32e 250
e293a5e8
NK
251 fd = perf_pmu__pathname_fd(dirfd, name, "format", O_DIRECTORY);
252 if (fd < 0)
d9664582 253 return 0;
cd82a32e 254
e293a5e8 255 /* it'll close the fd */
aa1551f2 256 if (perf_pmu__format_parse(pmu, fd, eager_load))
cd82a32e
JO
257 return -1;
258
259 return 0;
260}
261
a55ab7c4 262int perf_pmu__convert_scale(const char *scale, char **end, double *sval)
410136f5 263{
ea8f75f9 264 char *lc;
d02fc6bc 265 int ret = 0;
9ecae065 266
410136f5
SE
267 /*
268 * save current locale
269 */
270 lc = setlocale(LC_NUMERIC, NULL);
271
f9a5978a
JO
272 /*
273 * The lc string may be allocated in static storage,
274 * so get a dynamic copy to make it survive setlocale
275 * call below.
276 */
277 lc = strdup(lc);
278 if (!lc) {
279 ret = -ENOMEM;
d02fc6bc 280 goto out;
f9a5978a
JO
281 }
282
410136f5
SE
283 /*
284 * force to C locale to ensure kernel
285 * scale string is converted correctly.
286 * kernel uses default C locale.
287 */
288 setlocale(LC_NUMERIC, "C");
289
d02fc6bc 290 *sval = strtod(scale, end);
410136f5 291
d02fc6bc 292out:
410136f5
SE
293 /* restore locale */
294 setlocale(LC_NUMERIC, lc);
ea8f75f9 295 free(lc);
d02fc6bc
AK
296 return ret;
297}
298
7b723dbb 299static int perf_pmu__parse_scale(struct perf_pmu *pmu, struct perf_pmu_alias *alias)
d02fc6bc
AK
300{
301 struct stat st;
302 ssize_t sret;
7b723dbb 303 size_t len;
d02fc6bc
AK
304 char scale[128];
305 int fd, ret = -1;
306 char path[PATH_MAX];
307
7b723dbb
IR
308 len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path));
309 if (!len)
310 return 0;
48a3adcf 311 scnprintf(path + len, sizeof(path) - len, "%s/events/%s.scale", pmu->name, alias->name);
d02fc6bc 312
7b723dbb 313 fd = open(path, O_RDONLY);
d02fc6bc
AK
314 if (fd == -1)
315 return -1;
316
317 if (fstat(fd, &st) < 0)
318 goto error;
319
320 sret = read(fd, scale, sizeof(scale)-1);
321 if (sret < 0)
322 goto error;
323
324 if (scale[sret - 1] == '\n')
325 scale[sret - 1] = '\0';
326 else
327 scale[sret] = '\0';
f9a5978a 328
a55ab7c4 329 ret = perf_pmu__convert_scale(scale, NULL, &alias->scale);
410136f5
SE
330error:
331 close(fd);
332 return ret;
333}
334
7b723dbb 335static int perf_pmu__parse_unit(struct perf_pmu *pmu, struct perf_pmu_alias *alias)
410136f5
SE
336{
337 char path[PATH_MAX];
7b723dbb 338 size_t len;
410136f5
SE
339 ssize_t sret;
340 int fd;
341
410136f5 342
7b723dbb
IR
343 len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path));
344 if (!len)
345 return 0;
48a3adcf 346 scnprintf(path + len, sizeof(path) - len, "%s/events/%s.unit", pmu->name, alias->name);
7b723dbb
IR
347
348 fd = open(path, O_RDONLY);
410136f5
SE
349 if (fd == -1)
350 return -1;
351
d85ce830 352 sret = read(fd, alias->unit, UNIT_MAX_LEN);
410136f5
SE
353 if (sret < 0)
354 goto error;
355
356 close(fd);
357
9ecae065
MS
358 if (alias->unit[sret - 1] == '\n')
359 alias->unit[sret - 1] = '\0';
360 else
361 alias->unit[sret] = '\0';
410136f5
SE
362
363 return 0;
364error:
365 close(fd);
366 alias->unit[0] = '\0';
367 return -1;
368}
369
044330c1 370static int
7b723dbb 371perf_pmu__parse_per_pkg(struct perf_pmu *pmu, struct perf_pmu_alias *alias)
044330c1
MF
372{
373 char path[PATH_MAX];
7b723dbb 374 size_t len;
044330c1
MF
375 int fd;
376
7b723dbb
IR
377 len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path));
378 if (!len)
379 return 0;
48a3adcf 380 scnprintf(path + len, sizeof(path) - len, "%s/events/%s.per-pkg", pmu->name, alias->name);
044330c1 381
7b723dbb 382 fd = open(path, O_RDONLY);
044330c1
MF
383 if (fd == -1)
384 return -1;
385
386 close(fd);
387
388 alias->per_pkg = true;
389 return 0;
390}
391
7b723dbb 392static int perf_pmu__parse_snapshot(struct perf_pmu *pmu, struct perf_pmu_alias *alias)
1d9e446b
JO
393{
394 char path[PATH_MAX];
7b723dbb 395 size_t len;
1d9e446b
JO
396 int fd;
397
7b723dbb
IR
398 len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path));
399 if (!len)
400 return 0;
48a3adcf 401 scnprintf(path + len, sizeof(path) - len, "%s/events/%s.snapshot", pmu->name, alias->name);
1d9e446b 402
7b723dbb 403 fd = open(path, O_RDONLY);
1d9e446b
JO
404 if (fd == -1)
405 return -1;
406
407 alias->snapshot = true;
408 close(fd);
409 return 0;
410}
411
6dde6429 412/* Delete an alias entry. */
c3245d20 413static void perf_pmu_free_alias(struct perf_pmu_alias *newalias)
6dde6429
TR
414{
415 zfree(&newalias->name);
416 zfree(&newalias->desc);
417 zfree(&newalias->long_desc);
418 zfree(&newalias->topic);
32705de7 419 zfree(&newalias->pmu_name);
0d3f0e6f 420 parse_events_terms__exit(&newalias->terms);
6dde6429
TR
421 free(newalias);
422}
423
eec11310
NK
424static void perf_pmu__del_aliases(struct perf_pmu *pmu)
425{
426 struct perf_pmu_alias *alias, *tmp;
427
428 list_for_each_entry_safe(alias, tmp, &pmu->aliases, list) {
429 list_del(&alias->list);
430 perf_pmu_free_alias(alias);
431 }
432}
433
8d4b6d37
IR
434static struct perf_pmu_alias *perf_pmu__find_alias(struct perf_pmu *pmu,
435 const char *name,
436 bool load)
6dde6429 437{
f63a536f 438 struct perf_pmu_alias *alias;
6dde6429 439
7b6dd7a9
IR
440 if (load && !pmu->sysfs_aliases_loaded) {
441 bool has_sysfs_event;
442 char event_file_name[FILENAME_MAX + 8];
8d4b6d37 443
7b6dd7a9
IR
444 /*
445 * Test if alias/event 'name' exists in the PMU's sysfs/events
446 * directory. If not skip parsing the sysfs aliases. Sysfs event
447 * name must be all lower or all upper case.
448 */
449 scnprintf(event_file_name, sizeof(event_file_name), "events/%s", name);
450 for (size_t i = 7, n = 7 + strlen(name); i < n; i++)
451 event_file_name[i] = tolower(event_file_name[i]);
452
453 has_sysfs_event = perf_pmu__file_exists(pmu, event_file_name);
454 if (!has_sysfs_event) {
455 for (size_t i = 7, n = 7 + strlen(name); i < n; i++)
456 event_file_name[i] = toupper(event_file_name[i]);
457
458 has_sysfs_event = perf_pmu__file_exists(pmu, event_file_name);
459 }
460 if (has_sysfs_event)
461 pmu_aliases_parse(pmu);
462
463 }
f63a536f
IR
464 list_for_each_entry(alias, &pmu->aliases, list) {
465 if (!strcasecmp(alias->name, name))
466 return alias;
6dde6429 467 }
f63a536f
IR
468 return NULL;
469}
470
471static bool assign_str(const char *name, const char *field, char **old_str,
472 const char *new_str)
473{
474 if (!*old_str && new_str) {
475 *old_str = strdup(new_str);
476 return true;
477 }
478
479 if (!new_str || !strcasecmp(*old_str, new_str))
480 return false; /* Nothing to update. */
481
482 pr_debug("alias %s differs in field '%s' ('%s' != '%s')\n",
483 name, field, *old_str, new_str);
484 zfree(old_str);
485 *old_str = strdup(new_str);
486 return true;
487}
488
7b723dbb
IR
489static void read_alias_info(struct perf_pmu *pmu, struct perf_pmu_alias *alias)
490{
491 if (!alias->from_sysfs || alias->info_loaded)
492 return;
493
494 /*
495 * load unit name and scale if available
496 */
497 perf_pmu__parse_unit(pmu, alias);
498 perf_pmu__parse_scale(pmu, alias);
499 perf_pmu__parse_per_pkg(pmu, alias);
500 perf_pmu__parse_snapshot(pmu, alias);
501}
502
503struct update_alias_data {
504 struct perf_pmu *pmu;
505 struct perf_pmu_alias *alias;
506};
507
f63a536f
IR
508static int update_alias(const struct pmu_event *pe,
509 const struct pmu_events_table *table __maybe_unused,
510 void *vdata)
511{
7b723dbb 512 struct update_alias_data *data = vdata;
f63a536f
IR
513 int ret = 0;
514
7b723dbb
IR
515 read_alias_info(data->pmu, data->alias);
516 assign_str(pe->name, "desc", &data->alias->desc, pe->desc);
517 assign_str(pe->name, "long_desc", &data->alias->long_desc, pe->long_desc);
518 assign_str(pe->name, "topic", &data->alias->topic, pe->topic);
519 data->alias->per_pkg = pe->perpkg;
30f0b435 520 if (pe->event) {
0d3f0e6f 521 parse_events_terms__exit(&data->alias->terms);
7b723dbb 522 ret = parse_events_terms(&data->alias->terms, pe->event, /*input=*/NULL);
f63a536f
IR
523 }
524 if (!ret && pe->unit) {
525 char *unit;
526
7b723dbb 527 ret = perf_pmu__convert_scale(pe->unit, &unit, &data->alias->scale);
f63a536f 528 if (!ret)
7b723dbb 529 snprintf(data->alias->unit, sizeof(data->alias->unit), "%s", unit);
f63a536f
IR
530 }
531 return ret;
6dde6429
TR
532}
533
7b723dbb 534static int perf_pmu__new_alias(struct perf_pmu *pmu, const char *name,
edb217ff 535 const char *desc, const char *val, FILE *val_fd,
d9c5f5f9 536 const struct pmu_event *pe, enum event_source src)
a6146d50 537{
5c6ccc37 538 struct perf_pmu_alias *alias;
a6146d50 539 int ret;
330f40a0 540 const char *long_desc = NULL, *topic = NULL, *unit = NULL, *pmu_name = NULL;
bd680861 541 bool deprecated = false, perpkg = false;
eab35953 542
8d4b6d37 543 if (perf_pmu__find_alias(pmu, name, /*load=*/ false)) {
f63a536f
IR
544 /* Alias was already created/loaded. */
545 return 0;
546 }
547
eab35953 548 if (pe) {
330f40a0
IR
549 long_desc = pe->long_desc;
550 topic = pe->topic;
551 unit = pe->unit;
bd680861 552 perpkg = pe->perpkg;
9ed8b7dc 553 deprecated = pe->deprecated;
39aa4ff6
IR
554 if (pe->pmu && strcmp(pe->pmu, "default_core"))
555 pmu_name = pe->pmu;
eab35953 556 }
a6146d50 557
eaaebb01 558 alias = zalloc(sizeof(*alias));
a6146d50
ZY
559 if (!alias)
560 return -ENOMEM;
561
0d3f0e6f 562 parse_events_terms__init(&alias->terms);
410136f5
SE
563 alias->scale = 1.0;
564 alias->unit[0] = '\0';
bd680861 565 alias->per_pkg = perpkg;
84530920 566 alias->snapshot = false;
9ed8b7dc 567 alias->deprecated = deprecated;
410136f5 568
edb217ff 569 ret = parse_events_terms(&alias->terms, val, val_fd);
a6146d50 570 if (ret) {
70c646e0 571 pr_err("Cannot parse alias %s: %d\n", val, ret);
a6146d50
ZY
572 free(alias);
573 return ret;
574 }
575
576 alias->name = strdup(name);
08e60ed1 577 alias->desc = desc ? strdup(desc) : NULL;
c8d6828a
SB
578 alias->long_desc = long_desc ? strdup(long_desc) :
579 desc ? strdup(desc) : NULL;
dd5f1036 580 alias->topic = topic ? strdup(topic) : NULL;
f63a536f 581 alias->pmu_name = pmu_name ? strdup(pmu_name) : NULL;
fedb2b51 582 if (unit) {
f63a536f
IR
583 if (perf_pmu__convert_scale(unit, (char **)&unit, &alias->scale) < 0) {
584 perf_pmu_free_alias(alias);
fedb2b51 585 return -1;
f63a536f 586 }
fedb2b51
AK
587 snprintf(alias->unit, sizeof(alias->unit), "%s", unit);
588 }
d9c5f5f9
IR
589 switch (src) {
590 default:
591 case EVENT_SRC_SYSFS:
7b723dbb 592 alias->from_sysfs = true;
7c52f10c 593 if (pmu->events_table) {
d9c5f5f9
IR
594 /* Update an event from sysfs with json data. */
595 struct update_alias_data data = {
596 .pmu = pmu,
597 .alias = alias,
598 };
e6ff1eed 599 if (pmu_events_table__find_event(pmu->events_table, pmu, name,
7b723dbb 600 update_alias, &data) == 0)
d9c5f5f9 601 pmu->cpu_json_aliases++;
7c52f10c 602 }
e6ff1eed 603 pmu->sysfs_aliases++;
d9c5f5f9
IR
604 break;
605 case EVENT_SRC_CPU_JSON:
606 pmu->cpu_json_aliases++;
607 break;
608 case EVENT_SRC_SYS_JSON:
609 pmu->sys_json_aliases++;
610 break;
611
612 }
f63a536f 613 list_add_tail(&alias->list, &pmu->aliases);
a6146d50
ZY
614 return 0;
615}
616
3a42f4c7 617static inline bool pmu_alias_info_file(const char *name)
46441bdc
MF
618{
619 size_t len;
620
621 len = strlen(name);
622 if (len > 5 && !strcmp(name + len - 5, ".unit"))
623 return true;
624 if (len > 6 && !strcmp(name + len - 6, ".scale"))
625 return true;
044330c1
MF
626 if (len > 8 && !strcmp(name + len - 8, ".per-pkg"))
627 return true;
1d9e446b
JO
628 if (len > 9 && !strcmp(name + len - 9, ".snapshot"))
629 return true;
46441bdc
MF
630
631 return false;
632}
633
a6146d50 634/*
8d4b6d37
IR
635 * Reading the pmu event aliases definition, which should be located at:
636 * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
a6146d50 637 */
18eb2ca8 638static int __pmu_aliases_parse(struct perf_pmu *pmu, int events_dir_fd)
a6146d50
ZY
639{
640 struct dirent *evt_ent;
641 DIR *event_dir;
8d4b6d37 642
18eb2ca8
IR
643 event_dir = fdopendir(events_dir_fd);
644 if (!event_dir)
a6146d50
ZY
645 return -EINVAL;
646
940db6dc 647 while ((evt_ent = readdir(event_dir))) {
a6146d50 648 char *name = evt_ent->d_name;
18eb2ca8 649 int fd;
a6146d50
ZY
650 FILE *file;
651
652 if (!strcmp(name, ".") || !strcmp(name, ".."))
653 continue;
654
410136f5 655 /*
46441bdc 656 * skip info files parsed in perf_pmu__new_alias()
410136f5 657 */
46441bdc 658 if (pmu_alias_info_file(name))
410136f5
SE
659 continue;
660
18eb2ca8 661 fd = openat(events_dir_fd, name, O_RDONLY);
0ea8920e
IR
662 if (fd == -1) {
663 pr_debug("Cannot open %s\n", name);
664 continue;
665 }
e293a5e8 666 file = fdopen(fd, "r");
940db6dc 667 if (!file) {
0ea8920e 668 close(fd);
940db6dc
AK
669 continue;
670 }
410136f5 671
7b723dbb 672 if (perf_pmu__new_alias(pmu, name, /*desc=*/ NULL,
d9c5f5f9
IR
673 /*val=*/ NULL, file, /*pe=*/ NULL,
674 EVENT_SRC_SYSFS) < 0)
940db6dc 675 pr_debug("Cannot set up %s\n", name);
a6146d50
ZY
676 fclose(file);
677 }
678
679 closedir(event_dir);
8d4b6d37 680 pmu->sysfs_aliases_loaded = true;
a6146d50
ZY
681 return 0;
682}
683
18eb2ca8
IR
684static int pmu_aliases_parse(struct perf_pmu *pmu)
685{
686 char path[PATH_MAX];
687 size_t len;
688 int events_dir_fd, ret;
689
690 if (pmu->sysfs_aliases_loaded)
691 return 0;
692
693 len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path));
694 if (!len)
695 return 0;
696 scnprintf(path + len, sizeof(path) - len, "%s/events", pmu->name);
697
698 events_dir_fd = open(path, O_DIRECTORY);
699 if (events_dir_fd == -1) {
700 pmu->sysfs_aliases_loaded = true;
701 return 0;
702 }
703 ret = __pmu_aliases_parse(pmu, events_dir_fd);
704 close(events_dir_fd);
705 return ret;
706}
707
708static int pmu_aliases_parse_eager(struct perf_pmu *pmu, int sysfs_fd)
709{
710 char path[FILENAME_MAX + 7];
711 int ret, events_dir_fd;
712
713 scnprintf(path, sizeof(path), "%s/events", pmu->name);
714 events_dir_fd = openat(sysfs_fd, path, O_DIRECTORY, 0);
715 if (events_dir_fd == -1) {
716 pmu->sysfs_aliases_loaded = true;
717 return 0;
718 }
719 ret = __pmu_aliases_parse(pmu, events_dir_fd);
720 close(events_dir_fd);
721 return ret;
722}
723
f5144eca 724static int pmu_alias_terms(struct perf_pmu_alias *alias, int err_loc, struct list_head *terms)
a6146d50 725{
7c2f8164 726 struct parse_events_term *term, *cloned;
0d3f0e6f
IR
727 struct parse_events_terms clone_terms;
728
729 parse_events_terms__init(&clone_terms);
730 list_for_each_entry(term, &alias->terms.terms, list) {
731 int ret = parse_events_term__clone(&cloned, term);
a6146d50 732
a6146d50 733 if (ret) {
0d3f0e6f 734 parse_events_terms__exit(&clone_terms);
a6146d50
ZY
735 return ret;
736 }
c2f1cead
AK
737 /*
738 * Weak terms don't override command line options,
739 * which we don't want for implicit terms in aliases.
740 */
741 cloned->weak = true;
f5144eca 742 cloned->err_term = cloned->err_val = err_loc;
0d3f0e6f 743 list_add_tail(&cloned->list, &clone_terms.terms);
a6146d50 744 }
0d3f0e6f
IR
745 list_splice_init(&clone_terms.terms, terms);
746 parse_events_terms__exit(&clone_terms);
a6146d50
ZY
747 return 0;
748}
749
66ec1191
MR
750/*
751 * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64)
752 * may have a "cpus" file.
753 */
d06593aa 754static struct perf_cpu_map *pmu_cpumask(int dirfd, const char *name, bool is_core)
7ae92e74 755{
f854839b 756 struct perf_cpu_map *cpus;
7e3fcffe 757 const char *templates[] = {
d50a79cd
JC
758 "cpumask",
759 "cpus",
66ec1191 760 NULL
7e3fcffe
MR
761 };
762 const char **template;
d50a79cd
JC
763 char pmu_name[PATH_MAX];
764 struct perf_pmu pmu = {.name = pmu_name};
765 FILE *file;
7ae92e74 766
d50a79cd 767 strlcpy(pmu_name, name, sizeof(pmu_name));
7e3fcffe 768 for (template = templates; *template; template++) {
3a69672e 769 file = perf_pmu__open_file_at(&pmu, dirfd, *template);
d50a79cd
JC
770 if (!file)
771 continue;
772 cpus = perf_cpu_map__read(file);
3a69672e 773 fclose(file);
66ec1191
MR
774 if (cpus)
775 return cpus;
7e3fcffe 776 }
7ae92e74 777
d06593aa
IR
778 /* Nothing found, for core PMUs assume this means all CPUs. */
779 return is_core ? perf_cpu_map__get(cpu_map__online()) : NULL;
66ec1191 780}
7ae92e74 781
3a69672e 782static bool pmu_is_uncore(int dirfd, const char *name)
66ec1191 783{
3a69672e 784 int fd;
7ae92e74 785
3a69672e
NK
786 fd = perf_pmu__pathname_fd(dirfd, name, "cpumask", O_PATH);
787 if (fd < 0)
788 return false;
789
790 close(fd);
791 return true;
7ae92e74
YZ
792}
793
51d54847
JG
794static char *pmu_id(const char *name)
795{
796 char path[PATH_MAX], *str;
797 size_t len;
798
5f2c8efa 799 perf_pmu__pathname_scnprintf(path, sizeof(path), name, "identifier");
51d54847 800
5f2c8efa 801 if (filename__read_str(path, &str, &len) < 0)
51d54847
JG
802 return NULL;
803
804 str[len - 1] = 0; /* remove line feed */
805
806 return str;
807}
808
4bf7e81a
IR
809/**
810 * is_sysfs_pmu_core() - PMU CORE devices have different name other than cpu in
811 * sysfs on some platforms like ARM or Intel hybrid. Looking for
812 * possible the cpus file in sysfs files to identify whether this is a
813 * core device.
814 * @name: The PMU name such as "cpu_atom".
14b22ae0 815 */
4bf7e81a 816static int is_sysfs_pmu_core(const char *name)
14b22ae0 817{
14b22ae0 818 char path[PATH_MAX];
14b22ae0 819
f8ad6018 820 if (!perf_pmu__pathname_scnprintf(path, sizeof(path), name, "cpus"))
14b22ae0 821 return 0;
d9664582 822 return file_available(path);
14b22ae0
GK
823}
824
29be2fe0 825char *perf_pmu__getcpuid(struct perf_pmu *pmu)
933f82ff 826{
933f82ff 827 char *cpuid;
fb967063 828 static bool printed;
933f82ff 829
fc06e2a5
AK
830 cpuid = getenv("PERF_CPUID");
831 if (cpuid)
832 cpuid = strdup(cpuid);
833 if (!cpuid)
54e32dc0 834 cpuid = get_cpuid_str(pmu);
933f82ff 835 if (!cpuid)
d77ade9f 836 return NULL;
933f82ff 837
fb967063
AK
838 if (!printed) {
839 pr_debug("Using CPUID %s\n", cpuid);
840 printed = true;
841 }
d77ade9f
AK
842 return cpuid;
843}
844
f8ea2c15
IR
845__weak const struct pmu_metrics_table *pmu_metrics_table__find(void)
846{
847 return perf_pmu__find_metrics_table(NULL);
848}
849
7afbf90e
JC
850/**
851 * Return the length of the PMU name not including the suffix for uncore PMUs.
852 *
853 * We want to deduplicate many similar uncore PMUs by stripping their suffixes,
854 * but there are never going to be too many core PMUs and the suffixes might be
855 * interesting. "arm_cortex_a53" vs "arm_cortex_a57" or "cpum_cf" for example.
856 *
857 * @skip_duplicate_pmus: False in verbose mode so all uncore PMUs are visible
858 */
859static size_t pmu_deduped_name_len(const struct perf_pmu *pmu, const char *name,
860 bool skip_duplicate_pmus)
861{
862 return skip_duplicate_pmus && !pmu->is_core
863 ? pmu_name_len_no_suffix(name)
864 : strlen(name);
865}
866
240e6fd0
IR
867/**
868 * perf_pmu__match_ignoring_suffix - Does the pmu_name match tok ignoring any
869 * trailing suffix? The Suffix must be in form
870 * tok_{digits}, or tok{digits}.
871 * @pmu_name: The pmu_name with possible suffix.
872 * @tok: The possible match to pmu_name without suffix.
c07d5c92 873 */
240e6fd0 874static bool perf_pmu__match_ignoring_suffix(const char *pmu_name, const char *tok)
c47a5599 875{
3241d46f
IR
876 const char *p, *suffix;
877 bool has_hex = false;
c47a5599
JY
878
879 if (strncmp(pmu_name, tok, strlen(tok)))
880 return false;
881
3241d46f 882 suffix = p = pmu_name + strlen(tok);
c47a5599
JY
883 if (*p == 0)
884 return true;
885
3241d46f 886 if (*p == '_') {
c07d5c92 887 ++p;
3241d46f
IR
888 ++suffix;
889 }
c47a5599 890
c07d5c92
JG
891 /* Ensure we end in a number */
892 while (1) {
3241d46f 893 if (!isxdigit(*p))
c07d5c92 894 return false;
3241d46f
IR
895 if (!has_hex)
896 has_hex = !isdigit(*p);
c07d5c92
JG
897 if (*(++p) == 0)
898 break;
899 }
c47a5599 900
3241d46f
IR
901 if (has_hex)
902 return (p - suffix) > 2;
903
c47a5599
JY
904 return true;
905}
906
240e6fd0
IR
907/**
908 * pmu_uncore_alias_match - does name match the PMU name?
909 * @pmu_name: the json struct pmu_event name. This may lack a suffix (which
910 * matches) or be of the form "socket,pmuname" which will match
911 * "socketX_pmunameY".
912 * @name: a real full PMU name as from sysfs.
913 */
914static bool pmu_uncore_alias_match(const char *pmu_name, const char *name)
730670b1
JG
915{
916 char *tmp = NULL, *tok, *str;
917 bool res;
918
240e6fd0
IR
919 if (strchr(pmu_name, ',') == NULL)
920 return perf_pmu__match_ignoring_suffix(name, pmu_name);
921
730670b1
JG
922 str = strdup(pmu_name);
923 if (!str)
924 return false;
925
926 /*
927 * uncore alias may be from different PMU with common prefix
928 */
929 tok = strtok_r(str, ",", &tmp);
930 if (strncmp(pmu_name, tok, strlen(tok))) {
931 res = false;
932 goto out;
933 }
934
935 /*
936 * Match more complex aliases where the alias name is a comma-delimited
937 * list of tokens, orderly contained in the matching PMU name.
938 *
939 * Example: For alias "socket,pmuname" and PMU "socketX_pmunameY", we
940 * match "socket" in "socketX_pmunameY" and then "pmuname" in
941 * "pmunameY".
942 */
c07d5c92
JG
943 while (1) {
944 char *next_tok = strtok_r(NULL, ",", &tmp);
945
730670b1 946 name = strstr(name, tok);
c07d5c92 947 if (!name ||
240e6fd0 948 (!next_tok && !perf_pmu__match_ignoring_suffix(name, tok))) {
730670b1
JG
949 res = false;
950 goto out;
951 }
c07d5c92
JG
952 if (!next_tok)
953 break;
954 tok = next_tok;
955 name += strlen(tok);
730670b1
JG
956 }
957
958 res = true;
959out:
960 free(str);
961 return res;
962}
963
2879ff36
JZ
964bool pmu_uncore_identifier_match(const char *compat, const char *id)
965{
966 regex_t re;
967 regmatch_t pmatch[1];
968 int match;
969
970 if (regcomp(&re, compat, REG_EXTENDED) != 0) {
971 /* Warn unable to generate match particular string. */
972 pr_info("Invalid regular expression %s\n", compat);
973 return false;
974 }
975
976 match = !regexec(&re, id, 1, pmatch, 0);
977 if (match) {
978 /* Ensure a full match. */
979 match = pmatch[0].rm_so == 0 && (size_t)pmatch[0].rm_eo == strlen(id);
980 }
981 regfree(&re);
982
983 return match;
984}
985
660842e4 986static int pmu_add_cpu_aliases_map_callback(const struct pmu_event *pe,
1ba3752a 987 const struct pmu_events_table *table __maybe_unused,
660842e4
IR
988 void *vdata)
989{
f26d22f1 990 struct perf_pmu *pmu = vdata;
660842e4 991
d9c5f5f9
IR
992 perf_pmu__new_alias(pmu, pe->name, pe->desc, pe->event, /*val_fd=*/ NULL,
993 pe, EVENT_SRC_CPU_JSON);
660842e4
IR
994 return 0;
995}
996
d77ade9f 997/*
d685819b
IR
998 * From the pmu_events_table, find the events that correspond to the given
999 * PMU and add them to the list 'head'.
d77ade9f 1000 */
838a8c5f 1001void pmu_add_cpu_aliases_table(struct perf_pmu *pmu, const struct pmu_events_table *table)
d77ade9f 1002{
f26d22f1 1003 pmu_events_table__for_each_event(table, pmu, pmu_add_cpu_aliases_map_callback, pmu);
933f82ff
SB
1004}
1005
838a8c5f 1006static void pmu_add_cpu_aliases(struct perf_pmu *pmu)
e45ad701 1007{
7c52f10c 1008 if (!pmu->events_table)
e45ad701
JG
1009 return;
1010
e6ff1eed
IR
1011 if (pmu->cpu_aliases_added)
1012 return;
1013
7c52f10c 1014 pmu_add_cpu_aliases_table(pmu, pmu->events_table);
e6ff1eed 1015 pmu->cpu_aliases_added = true;
e45ad701
JG
1016}
1017
29be2fe0 1018static int pmu_add_sys_aliases_iter_fn(const struct pmu_event *pe,
1ba3752a 1019 const struct pmu_events_table *table __maybe_unused,
f26d22f1 1020 void *vdata)
4513c719 1021{
f26d22f1 1022 struct perf_pmu *pmu = vdata;
4513c719 1023
4513c719
JG
1024 if (!pe->compat || !pe->pmu)
1025 return 0;
1026
2879ff36 1027 if (pmu_uncore_alias_match(pe->pmu, pmu->name) &&
d9c5f5f9 1028 pmu_uncore_identifier_match(pe->compat, pmu->id)) {
7b723dbb 1029 perf_pmu__new_alias(pmu,
edb217ff
IR
1030 pe->name,
1031 pe->desc,
1032 pe->event,
1033 /*val_fd=*/ NULL,
d9c5f5f9
IR
1034 pe,
1035 EVENT_SRC_SYS_JSON);
4513c719
JG
1036 }
1037
1038 return 0;
1039}
1040
838a8c5f 1041void pmu_add_sys_aliases(struct perf_pmu *pmu)
4513c719 1042{
4513c719
JG
1043 if (!pmu->id)
1044 return;
1045
f26d22f1 1046 pmu_for_each_sys_event(pmu_add_sys_aliases_iter_fn, pmu);
4513c719
JG
1047}
1048
b1f05622 1049static char *pmu_find_alias_name(struct perf_pmu *pmu, int dirfd)
13d60ba0 1050{
b1f05622
IR
1051 FILE *file = perf_pmu__open_file_at(pmu, dirfd, "alias");
1052 char *line = NULL;
1053 size_t line_len = 0;
1054 ssize_t ret;
13d60ba0 1055
b1f05622
IR
1056 if (!file)
1057 return NULL;
1058
1059 ret = getline(&line, &line_len, file);
1060 if (ret < 0) {
1061 fclose(file);
1062 return NULL;
1063 }
1064 /* Remove trailing newline. */
1065 if (ret > 0 && line[ret - 1] == '\n')
1066 line[--ret] = '\0';
1067
1068 fclose(file);
1069 return line;
13d60ba0
KL
1070}
1071
3a69672e 1072static int pmu_max_precise(int dirfd, struct perf_pmu *pmu)
90a86bde 1073{
90a86bde
JO
1074 int max_precise = -1;
1075
3a69672e 1076 perf_pmu__scan_file_at(pmu, dirfd, "caps/max_precise", "%d", &max_precise);
90a86bde
JO
1077 return max_precise;
1078}
1079
aa613601 1080void __weak
bb65acdc 1081perf_pmu__arch_init(struct perf_pmu *pmu)
aa613601 1082{
bb65acdc
KL
1083 if (pmu->is_core)
1084 pmu->mem_events = perf_mem_events;
aa613601
IR
1085}
1086
aa1551f2
IR
1087struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char *name,
1088 bool eager_load)
cd82a32e
JO
1089{
1090 struct perf_pmu *pmu;
cd82a32e 1091 __u32 type;
49afa7f6 1092
838a8c5f
IR
1093 pmu = zalloc(sizeof(*pmu));
1094 if (!pmu)
1095 return NULL;
1096
f63a536f
IR
1097 pmu->name = strdup(name);
1098 if (!pmu->name)
1099 goto err;
88ed9184
IR
1100
1101 /*
1102 * Read type early to fail fast if a lookup name isn't a PMU. Ensure
1103 * that type value is successfully assigned (return 1).
1104 */
1105 if (perf_pmu__scan_file_at(pmu, dirfd, "type", "%u", &type) != 1)
1106 goto err;
1107
1108 INIT_LIST_HEAD(&pmu->format);
1109 INIT_LIST_HEAD(&pmu->aliases);
1110 INIT_LIST_HEAD(&pmu->caps);
1111
cd82a32e
JO
1112 /*
1113 * The pmu data we store & need consists of the pmu
1114 * type value and format definitions. Load both right
1115 * now.
1116 */
aa1551f2 1117 if (pmu_format(pmu, dirfd, name, eager_load))
ef5de161
CJ
1118 goto err;
1119
d06593aa
IR
1120 pmu->is_core = is_pmu_core(name);
1121 pmu->cpus = pmu_cpumask(dirfd, name, pmu->is_core);
13d60ba0 1122
54e32dc0 1123 pmu->type = type;
3a69672e 1124 pmu->is_uncore = pmu_is_uncore(dirfd, name);
51d54847
JG
1125 if (pmu->is_uncore)
1126 pmu->id = pmu_id(name);
3a69672e 1127 pmu->max_precise = pmu_max_precise(dirfd, pmu);
b1f05622 1128 pmu->alias_name = pmu_find_alias_name(pmu, dirfd);
7c52f10c 1129 pmu->events_table = perf_pmu__find_events_table(pmu);
d9c5f5f9
IR
1130 /*
1131 * Load the sys json events/aliases when loading the PMU as each event
1132 * may have a different compat regular expression. We therefore can't
1133 * know the number of sys json events/aliases without computing the
1134 * regular expressions for them all.
1135 */
838a8c5f 1136 pmu_add_sys_aliases(pmu);
1eaf496e 1137 list_add_tail(&pmu->list, pmus);
dc0a6202 1138
aa613601 1139 perf_pmu__arch_init(pmu);
dc0a6202 1140
18eb2ca8
IR
1141 if (eager_load)
1142 pmu_aliases_parse_eager(pmu, dirfd);
1143
cd82a32e 1144 return pmu;
13d60ba0 1145err:
efe98a7a 1146 zfree(&pmu->name);
13d60ba0
KL
1147 free(pmu);
1148 return NULL;
cd82a32e
JO
1149}
1150
628eaa4e
IR
1151/* Creates the PMU when sysfs scanning fails. */
1152struct perf_pmu *perf_pmu__create_placeholder_core_pmu(struct list_head *core_pmus)
1153{
1154 struct perf_pmu *pmu = zalloc(sizeof(*pmu));
1155
1156 if (!pmu)
1157 return NULL;
1158
1159 pmu->name = strdup("cpu");
1160 if (!pmu->name) {
1161 free(pmu);
1162 return NULL;
1163 }
1164
1165 pmu->is_core = true;
1166 pmu->type = PERF_TYPE_RAW;
1167 pmu->cpus = cpu_map__online();
1168
1169 INIT_LIST_HEAD(&pmu->format);
1170 INIT_LIST_HEAD(&pmu->aliases);
1171 INIT_LIST_HEAD(&pmu->caps);
1172 list_add_tail(&pmu->list, core_pmus);
1173 return pmu;
1174}
1175
e552b7be
RH
1176void perf_pmu__warn_invalid_formats(struct perf_pmu *pmu)
1177{
1178 struct perf_pmu_format *format;
1179
68c25043
IR
1180 if (pmu->formats_checked)
1181 return;
1182
1183 pmu->formats_checked = true;
1184
e552b7be
RH
1185 /* fake pmu doesn't have format list */
1186 if (pmu == &perf_pmu__fake)
1187 return;
1188
50402641
IR
1189 list_for_each_entry(format, &pmu->format, list) {
1190 perf_pmu_format__load(pmu, format);
e552b7be
RH
1191 if (format->value >= PERF_PMU_FORMAT_VALUE_CONFIG_END) {
1192 pr_warning("WARNING: '%s' format '%s' requires 'perf_event_attr::config%d'"
1193 "which is not supported by this version of perf!\n",
1194 pmu->name, format->name, format->value);
1195 return;
1196 }
50402641 1197 }
e552b7be
RH
1198}
1199
c6d616fe 1200bool evsel__is_aux_event(const struct evsel *evsel)
e12ee9f7 1201{
e76026bd 1202 struct perf_pmu *pmu = evsel__find_pmu(evsel);
e12ee9f7
AH
1203
1204 return pmu && pmu->auxtrace;
1205}
1206
6593f019
JC
1207/*
1208 * Set @config_name to @val as long as the user hasn't already set or cleared it
1209 * by passing a config term on the command line.
1210 *
1211 * @val is the value to put into the bits specified by @config_name rather than
1212 * the bit pattern. It is shifted into position by this function, so to set
1213 * something to true, pass 1 for val rather than a pre shifted value.
1214 */
1215#define field_prep(_mask, _val) (((_val) << (ffsll(_mask) - 1)) & (_mask))
1216void evsel__set_config_if_unset(struct perf_pmu *pmu, struct evsel *evsel,
1217 const char *config_name, u64 val)
1218{
1219 u64 user_bits = 0, bits;
1220 struct evsel_config_term *term = evsel__get_config_term(evsel, CFG_CHG);
1221
1222 if (term)
1223 user_bits = term->val.cfg_chg;
1224
da6a5afd 1225 bits = perf_pmu__format_bits(pmu, config_name);
6593f019
JC
1226
1227 /* Do nothing if the user changed the value */
1228 if (bits & user_bits)
1229 return;
1230
1231 /* Otherwise replace it */
1232 evsel->core.attr.config &= ~bits;
1233 evsel->core.attr.config |= field_prep(bits, val);
1234}
1235
5c6ccc37 1236static struct perf_pmu_format *
63883cb0 1237pmu_find_format(const struct list_head *formats, const char *name)
cd82a32e 1238{
5c6ccc37 1239 struct perf_pmu_format *format;
cd82a32e
JO
1240
1241 list_for_each_entry(format, formats, list)
1242 if (!strcmp(format->name, name))
1243 return format;
1244
1245 return NULL;
1246}
1247
da6a5afd 1248__u64 perf_pmu__format_bits(struct perf_pmu *pmu, const char *name)
09ff6071 1249{
da6a5afd 1250 struct perf_pmu_format *format = pmu_find_format(&pmu->format, name);
09ff6071
AH
1251 __u64 bits = 0;
1252 int fbit;
1253
1254 if (!format)
1255 return 0;
1256
1257 for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
1258 bits |= 1ULL << fbit;
1259
1260 return bits;
1261}
1262
7eb54733 1263int perf_pmu__format_type(struct perf_pmu *pmu, const char *name)
a1ac7de6 1264{
7eb54733 1265 struct perf_pmu_format *format = pmu_find_format(&pmu->format, name);
a1ac7de6
AH
1266
1267 if (!format)
1268 return -1;
1269
50402641 1270 perf_pmu_format__load(pmu, format);
a1ac7de6
AH
1271 return format->value;
1272}
1273
cd82a32e 1274/*
dc0a6202 1275 * Sets value based on the format definition (format parameter)
4d39c89f 1276 * and unformatted value (value parameter).
cd82a32e 1277 */
dc0a6202
AH
1278static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
1279 bool zero)
cd82a32e
JO
1280{
1281 unsigned long fbit, vbit;
cd82a32e
JO
1282
1283 for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
1284
1285 if (!test_bit(fbit, format))
1286 continue;
1287
dc0a6202
AH
1288 if (value & (1llu << vbit++))
1289 *v |= (1llu << fbit);
1290 else if (zero)
1291 *v &= ~(1llu << fbit);
cd82a32e 1292 }
cd82a32e
JO
1293}
1294
0efe6b67
AH
1295static __u64 pmu_format_max_value(const unsigned long *format)
1296{
1b9caa10 1297 int w;
ac0e2cd5 1298
1b9caa10
JO
1299 w = bitmap_weight(format, PERF_PMU_FORMAT_BITS);
1300 if (!w)
1301 return 0;
1302 if (w < 64)
1303 return (1ULL << w) - 1;
1304 return -1;
0efe6b67
AH
1305}
1306
688d4dfc
CS
1307/*
1308 * Term is a string term, and might be a param-term. Try to look up it's value
1309 * in the remaining terms.
1310 * - We have a term like "base-or-format-term=param-term",
1311 * - We need to find the value supplied for "param-term" (with param-term named
1312 * in a config string) later on in the term list.
1313 */
1314static int pmu_resolve_param_term(struct parse_events_term *term,
0d3f0e6f 1315 struct parse_events_terms *head_terms,
688d4dfc
CS
1316 __u64 *value)
1317{
1318 struct parse_events_term *t;
1319
0d3f0e6f 1320 list_for_each_entry(t, &head_terms->terms, list) {
2a3d252d
IR
1321 if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM &&
1322 t->config && !strcmp(t->config, term->config)) {
1323 t->used = true;
1324 *value = t->val.num;
1325 return 0;
688d4dfc
CS
1326 }
1327 }
1328
bb963e16 1329 if (verbose > 0)
688d4dfc
CS
1330 printf("Required parameter '%s' not specified\n", term->config);
1331
1332 return -1;
1333}
1334
63883cb0 1335static char *pmu_formats_string(const struct list_head *formats)
e64b020b
JO
1336{
1337 struct perf_pmu_format *format;
11db4e29
MH
1338 char *str = NULL;
1339 struct strbuf buf = STRBUF_INIT;
f1417cea 1340 unsigned int i = 0;
e64b020b 1341
ffeb883e 1342 if (!formats)
e64b020b
JO
1343 return NULL;
1344
1345 /* sysfs exported terms */
ffeb883e 1346 list_for_each_entry(format, formats, list)
11db4e29
MH
1347 if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
1348 goto error;
e64b020b 1349
ffeb883e 1350 str = strbuf_detach(&buf, NULL);
11db4e29 1351error:
ffeb883e 1352 strbuf_release(&buf);
e64b020b 1353
e64b020b 1354 return str;
e64b020b
JO
1355}
1356
cd82a32e
JO
1357/*
1358 * Setup one of config[12] attr members based on the
88aca8d9 1359 * user input data - term parameter.
cd82a32e 1360 */
63883cb0 1361static int pmu_config_term(const struct perf_pmu *pmu,
cd82a32e 1362 struct perf_event_attr *attr,
dc0a6202 1363 struct parse_events_term *term,
0d3f0e6f 1364 struct parse_events_terms *head_terms,
e64b020b 1365 bool zero, struct parse_events_error *err)
cd82a32e 1366{
5c6ccc37 1367 struct perf_pmu_format *format;
cd82a32e 1368 __u64 *vp;
0efe6b67 1369 __u64 val, max_val;
688d4dfc
CS
1370
1371 /*
1372 * If this is a parameter we've already used for parameterized-eval,
1373 * skip it in normal eval.
1374 */
1375 if (term->used)
1376 return 0;
cd82a32e
JO
1377
1378 /*
cd82a32e
JO
1379 * Hardcoded terms should be already in, so nothing
1380 * to be done for them.
1381 */
1382 if (parse_events__is_hardcoded_term(term))
1383 return 0;
1384
804fee5d 1385 format = pmu_find_format(&pmu->format, term->config);
688d4dfc 1386 if (!format) {
804fee5d 1387 char *pmu_term = pmu_formats_string(&pmu->format);
4ac22b48
IR
1388 char *unknown_term;
1389 char *help_msg;
1390
1391 if (asprintf(&unknown_term,
1392 "unknown term '%s' for pmu '%s'",
804fee5d 1393 term->config, pmu->name) < 0)
4ac22b48
IR
1394 unknown_term = NULL;
1395 help_msg = parse_events_formats_error_string(pmu_term);
e64b020b 1396 if (err) {
6c191289 1397 parse_events_error__handle(err, term->err_term,
4ac22b48
IR
1398 unknown_term,
1399 help_msg);
1400 } else {
1401 pr_debug("%s (%s)\n", unknown_term, help_msg);
1402 free(unknown_term);
e64b020b 1403 }
4ac22b48 1404 free(pmu_term);
cd82a32e 1405 return -EINVAL;
688d4dfc 1406 }
50402641 1407 perf_pmu_format__load(pmu, format);
cd82a32e
JO
1408 switch (format->value) {
1409 case PERF_PMU_FORMAT_VALUE_CONFIG:
1410 vp = &attr->config;
1411 break;
1412 case PERF_PMU_FORMAT_VALUE_CONFIG1:
1413 vp = &attr->config1;
1414 break;
1415 case PERF_PMU_FORMAT_VALUE_CONFIG2:
1416 vp = &attr->config2;
1417 break;
204e7c49
RH
1418 case PERF_PMU_FORMAT_VALUE_CONFIG3:
1419 vp = &attr->config3;
1420 break;
cd82a32e
JO
1421 default:
1422 return -EINVAL;
1423 }
1424
16fa7e82 1425 /*
688d4dfc
CS
1426 * Either directly use a numeric term, or try to translate string terms
1427 * using event parameters.
16fa7e82 1428 */
99e7138e
JO
1429 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1430 if (term->no_value &&
1431 bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) {
1432 if (err) {
6c191289 1433 parse_events_error__handle(err, term->err_val,
448d732c
IR
1434 strdup("no value assigned for term"),
1435 NULL);
99e7138e
JO
1436 }
1437 return -EINVAL;
1438 }
1439
688d4dfc 1440 val = term->val.num;
99e7138e 1441 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
688d4dfc 1442 if (strcmp(term->val.str, "?")) {
bb963e16 1443 if (verbose > 0) {
688d4dfc
CS
1444 pr_info("Invalid sysfs entry %s=%s\n",
1445 term->config, term->val.str);
e64b020b
JO
1446 }
1447 if (err) {
6c191289 1448 parse_events_error__handle(err, term->err_val,
448d732c
IR
1449 strdup("expected numeric value"),
1450 NULL);
e64b020b 1451 }
688d4dfc
CS
1452 return -EINVAL;
1453 }
1454
1455 if (pmu_resolve_param_term(term, head_terms, &val))
1456 return -EINVAL;
1457 } else
1458 return -EINVAL;
1459
0efe6b67
AH
1460 max_val = pmu_format_max_value(format->bits);
1461 if (val > max_val) {
1462 if (err) {
448d732c
IR
1463 char *err_str;
1464
6c191289 1465 parse_events_error__handle(err, term->err_val,
448d732c 1466 asprintf(&err_str,
f5144eca
IR
1467 "value too big for format (%s), maximum is %llu",
1468 format->name, (unsigned long long)max_val) < 0
448d732c
IR
1469 ? strdup("value too big for format")
1470 : err_str,
1471 NULL);
0efe6b67
AH
1472 return -EINVAL;
1473 }
1474 /*
1475 * Assume we don't care if !err, in which case the value will be
1476 * silently truncated.
1477 */
1478 }
1479
688d4dfc 1480 pmu_format_value(format->bits, val, vp, zero);
cd82a32e
JO
1481 return 0;
1482}
1483
63883cb0 1484int perf_pmu__config_terms(const struct perf_pmu *pmu,
cff7f956 1485 struct perf_event_attr *attr,
0d3f0e6f 1486 struct parse_events_terms *terms,
e64b020b 1487 bool zero, struct parse_events_error *err)
cd82a32e 1488{
6cee6cd3 1489 struct parse_events_term *term;
cd82a32e 1490
0d3f0e6f
IR
1491 list_for_each_entry(term, &terms->terms, list) {
1492 if (pmu_config_term(pmu, attr, term, terms, zero, err))
cd82a32e 1493 return -EINVAL;
688d4dfc 1494 }
cd82a32e
JO
1495
1496 return 0;
1497}
1498
1499/*
1500 * Configures event's 'attr' parameter based on the:
1501 * 1) users input - specified in terms parameter
1502 * 2) pmu format definitions - specified by pmu parameter
1503 */
1504int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
0d3f0e6f 1505 struct parse_events_terms *head_terms,
e64b020b 1506 struct parse_events_error *err)
cd82a32e 1507{
0197da7a 1508 bool zero = !!pmu->perf_event_attr_init_default;
dc0a6202 1509
804fee5d 1510 return perf_pmu__config_terms(pmu, attr, head_terms, zero, err);
cd82a32e
JO
1511}
1512
5c6ccc37
ACM
1513static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
1514 struct parse_events_term *term)
a6146d50 1515{
e6ff1eed 1516 struct perf_pmu_alias *alias;
970ef02e 1517 const char *name;
a6146d50
ZY
1518
1519 if (parse_events__is_hardcoded_term(term))
1520 return NULL;
1521
1522 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
64199ae4 1523 if (!term->no_value)
a6146d50
ZY
1524 return NULL;
1525 if (pmu_find_format(&pmu->format, term->config))
1526 return NULL;
1527 name = term->config;
c3245d20 1528
a6146d50
ZY
1529 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1530 if (strcasecmp(term->config, "event"))
1531 return NULL;
1532 name = term->val.str;
1533 } else {
1534 return NULL;
1535 }
1536
8d4b6d37 1537 alias = perf_pmu__find_alias(pmu, name, /*load=*/ true);
e6ff1eed
IR
1538 if (alias || pmu->cpu_aliases_added)
1539 return alias;
1540
1541 /* Alias doesn't exist, try to get it from the json events. */
1542 if (pmu->events_table &&
1543 pmu_events_table__find_event(pmu->events_table, pmu, name,
1544 pmu_add_cpu_aliases_map_callback,
1545 pmu) == 0) {
8d4b6d37 1546 alias = perf_pmu__find_alias(pmu, name, /*load=*/ false);
e6ff1eed
IR
1547 }
1548 return alias;
a6146d50
ZY
1549}
1550
410136f5 1551
7b723dbb
IR
1552static int check_info_data(struct perf_pmu *pmu,
1553 struct perf_pmu_alias *alias,
9d31cb93
IR
1554 struct perf_pmu_info *info,
1555 struct parse_events_error *err,
1556 int column)
410136f5 1557{
7b723dbb 1558 read_alias_info(pmu, alias);
410136f5
SE
1559 /*
1560 * Only one term in event definition can
1d9e446b
JO
1561 * define unit, scale and snapshot, fail
1562 * if there's more than one.
410136f5 1563 */
9d31cb93
IR
1564 if (info->unit && alias->unit[0]) {
1565 parse_events_error__handle(err, column,
1566 strdup("Attempt to set event's unit twice"),
1567 NULL);
1568 return -EINVAL;
1569 }
1570 if (info->scale && alias->scale) {
1571 parse_events_error__handle(err, column,
1572 strdup("Attempt to set event's scale twice"),
1573 NULL);
1574 return -EINVAL;
1575 }
1576 if (info->snapshot && alias->snapshot) {
1577 parse_events_error__handle(err, column,
1578 strdup("Attempt to set event snapshot twice"),
1579 NULL);
410136f5 1580 return -EINVAL;
9d31cb93 1581 }
410136f5 1582
b30a7d1f 1583 if (alias->unit[0])
1d9e446b 1584 info->unit = alias->unit;
410136f5
SE
1585
1586 if (alias->scale)
1d9e446b
JO
1587 info->scale = alias->scale;
1588
1589 if (alias->snapshot)
1590 info->snapshot = alias->snapshot;
410136f5
SE
1591
1592 return 0;
1593}
1594
a6146d50
ZY
1595/*
1596 * Find alias in the terms list and replace it with the terms
1597 * defined for the alias
1598 */
0d3f0e6f 1599int perf_pmu__check_alias(struct perf_pmu *pmu, struct parse_events_terms *head_terms,
a24d9d9d
IR
1600 struct perf_pmu_info *info, bool *rewrote_terms,
1601 struct parse_events_error *err)
a6146d50 1602{
6cee6cd3 1603 struct parse_events_term *term, *h;
5c6ccc37 1604 struct perf_pmu_alias *alias;
a6146d50
ZY
1605 int ret;
1606
a24d9d9d 1607 *rewrote_terms = false;
044330c1
MF
1608 info->per_pkg = false;
1609
8a398897
SE
1610 /*
1611 * Mark unit and scale as not set
1612 * (different from default values, see below)
1613 */
1d9e446b
JO
1614 info->unit = NULL;
1615 info->scale = 0.0;
1616 info->snapshot = false;
410136f5 1617
0d3f0e6f 1618 list_for_each_entry_safe(term, h, &head_terms->terms, list) {
a6146d50
ZY
1619 alias = pmu_find_alias(pmu, term);
1620 if (!alias)
1621 continue;
f5144eca 1622 ret = pmu_alias_terms(alias, term->err_term, &term->list);
9d31cb93
IR
1623 if (ret) {
1624 parse_events_error__handle(err, term->err_term,
1625 strdup("Failure to duplicate terms"),
1626 NULL);
a6146d50 1627 return ret;
9d31cb93 1628 }
a24d9d9d 1629 *rewrote_terms = true;
7b723dbb 1630 ret = check_info_data(pmu, alias, info, err, term->err_term);
410136f5
SE
1631 if (ret)
1632 return ret;
1633
044330c1
MF
1634 if (alias->per_pkg)
1635 info->per_pkg = true;
1636
e56fbc9d 1637 list_del_init(&term->list);
1dc92556 1638 parse_events_term__delete(term);
a6146d50 1639 }
8a398897
SE
1640
1641 /*
4d39c89f 1642 * if no unit or scale found in aliases, then
8a398897
SE
1643 * set defaults as for evsel
1644 * unit cannot left to NULL
1645 */
46441bdc
MF
1646 if (info->unit == NULL)
1647 info->unit = "";
8a398897 1648
46441bdc
MF
1649 if (info->scale == 0.0)
1650 info->scale = 1.0;
8a398897 1651
a6146d50
ZY
1652 return 0;
1653}
1654
c3245d20
IR
1655struct find_event_args {
1656 const char *event;
1657 void *state;
1658 pmu_event_callback cb;
1659};
1660
1661static int find_event_callback(void *state, struct pmu_event_info *info)
838a8c5f 1662{
c3245d20 1663 struct find_event_args *args = state;
838a8c5f 1664
c3245d20
IR
1665 if (!strcmp(args->event, info->name))
1666 return args->cb(args->state, info);
838a8c5f 1667
c3245d20 1668 return 0;
838a8c5f 1669}
c3245d20
IR
1670
1671int perf_pmu__find_event(struct perf_pmu *pmu, const char *event, void *state, pmu_event_callback cb)
1672{
1673 struct find_event_args args = {
1674 .event = event,
1675 .state = state,
1676 .cb = cb,
1677 };
1678
cd4e1efb
IR
1679 /* Sub-optimal, but function is only used by tests. */
1680 return perf_pmu__for_each_event(pmu, /*skip_duplicate_pmus=*/ false,
1681 &args, find_event_callback);
c3245d20
IR
1682}
1683
804fee5d 1684static void perf_pmu__del_formats(struct list_head *formats)
d26383dc
NK
1685{
1686 struct perf_pmu_format *fmt, *tmp;
1687
1688 list_for_each_entry_safe(fmt, tmp, formats, list) {
1689 list_del(&fmt->list);
efe98a7a 1690 zfree(&fmt->name);
d26383dc
NK
1691 free(fmt);
1692 }
1693}
1694
07d2b820
IR
1695bool perf_pmu__has_format(const struct perf_pmu *pmu, const char *name)
1696{
1697 struct perf_pmu_format *format;
1698
1699 list_for_each_entry(format, &pmu->format, list) {
1700 if (!strcmp(format->name, name))
1701 return true;
1702 }
1703 return false;
1704}
1705
4ccf3bb7
IR
1706int perf_pmu__for_each_format(struct perf_pmu *pmu, void *state, pmu_format_callback cb)
1707{
1708 static const char *const terms[] = {
1709 "config=0..0xffffffffffffffff",
1710 "config1=0..0xffffffffffffffff",
1711 "config2=0..0xffffffffffffffff",
1712 "config3=0..0xffffffffffffffff",
1713 "name=string",
1714 "period=number",
1715 "freq=number",
1716 "branch_type=(u|k|hv|any|...)",
1717 "time",
1718 "call-graph=(fp|dwarf|lbr)",
1719 "stack-size=number",
1720 "max-stack=number",
1721 "nr=number",
1722 "inherit",
1723 "no-inherit",
1724 "overwrite",
1725 "no-overwrite",
1726 "percore",
1727 "aux-output",
1728 "aux-sample-size=number",
1729 };
1730 struct perf_pmu_format *format;
1731 int ret;
1732
1733 /*
1734 * max-events and driver-config are missing above as are the internal
1735 * types user, metric-id, raw, legacy cache and hardware. Assert against
1736 * the enum parse_events__term_type so they are kept in sync.
1737 */
1738 _Static_assert(ARRAY_SIZE(terms) == __PARSE_EVENTS__TERM_TYPE_NR - 6,
1739 "perf_pmu__for_each_format()'s terms must be kept in sync with enum parse_events__term_type");
1740 list_for_each_entry(format, &pmu->format, list) {
1741 perf_pmu_format__load(pmu, format);
1742 ret = cb(state, format->name, (int)format->value, format->bits);
1743 if (ret)
1744 return ret;
1745 }
1746 if (!pmu->is_core)
1747 return 0;
1748
1749 for (size_t i = 0; i < ARRAY_SIZE(terms); i++) {
1750 int config = PERF_PMU_FORMAT_VALUE_CONFIG;
1751
1752 if (i < PERF_PMU_FORMAT_VALUE_CONFIG_END)
1753 config = i;
1754
1755 ret = cb(state, terms[i], config, /*bits=*/NULL);
1756 if (ret)
1757 return ret;
1758 }
1759 return 0;
1760}
1761
d504fae9
JG
1762bool is_pmu_core(const char *name)
1763{
6fbd67b0 1764 return !strcmp(name, "cpu") || !strcmp(name, "cpum_cf") || is_sysfs_pmu_core(name);
4bf7e81a
IR
1765}
1766
6fd1e519
IR
1767bool perf_pmu__supports_legacy_cache(const struct perf_pmu *pmu)
1768{
e20d1f2f 1769 return pmu->is_core;
6fd1e519
IR
1770}
1771
52c7b4d3
IR
1772bool perf_pmu__auto_merge_stats(const struct perf_pmu *pmu)
1773{
710dffc9 1774 return !pmu->is_core || perf_pmus__num_core_pmus() == 1;
52c7b4d3
IR
1775}
1776
e6ff1eed 1777bool perf_pmu__have_event(struct perf_pmu *pmu, const char *name)
4cabc3d1 1778{
a24d9d9d
IR
1779 if (!name)
1780 return false;
8d4b6d37 1781 if (perf_pmu__find_alias(pmu, name, /*load=*/ true) != NULL)
e6ff1eed
IR
1782 return true;
1783 if (pmu->cpu_aliases_added || !pmu->events_table)
1784 return false;
1785 return pmu_events_table__find_event(pmu->events_table, pmu, name, NULL, NULL) == 0;
c3245d20 1786}
4cabc3d1 1787
e6ff1eed 1788size_t perf_pmu__num_events(struct perf_pmu *pmu)
c3245d20 1789{
8d4b6d37
IR
1790 size_t nr;
1791
7b6dd7a9 1792 pmu_aliases_parse(pmu);
5484fd27 1793 nr = pmu->sysfs_aliases + pmu->sys_json_aliases;
c3245d20 1794
e6ff1eed 1795 if (pmu->cpu_aliases_added)
d9c5f5f9 1796 nr += pmu->cpu_json_aliases;
e6ff1eed 1797 else if (pmu->events_table)
d9c5f5f9
IR
1798 nr += pmu_events_table__num_events(pmu->events_table, pmu) - pmu->cpu_json_aliases;
1799 else
1800 assert(pmu->cpu_json_aliases == 0);
c3245d20
IR
1801
1802 return pmu->selectable ? nr + 1 : nr;
1803}
1804
1805static int sub_non_neg(int a, int b)
1806{
1807 if (b > a)
1808 return 0;
1809 return a - b;
1810}
1811
1812static char *format_alias(char *buf, int len, const struct perf_pmu *pmu,
cd4e1efb 1813 const struct perf_pmu_alias *alias, bool skip_duplicate_pmus)
c3245d20
IR
1814{
1815 struct parse_events_term *term;
7afbf90e
JC
1816 size_t pmu_name_len = pmu_deduped_name_len(pmu, pmu->name,
1817 skip_duplicate_pmus);
3241d46f 1818 int used = snprintf(buf, len, "%.*s/%s", (int)pmu_name_len, pmu->name, alias->name);
c3245d20 1819
0d3f0e6f 1820 list_for_each_entry(term, &alias->terms.terms, list) {
c3245d20
IR
1821 if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
1822 used += snprintf(buf + used, sub_non_neg(len, used),
1823 ",%s=%s", term->config,
1824 term->val.str);
4cabc3d1 1825 }
c3245d20
IR
1826
1827 if (sub_non_neg(len, used) > 0) {
1828 buf[used] = '/';
1829 used++;
1830 }
1831 if (sub_non_neg(len, used) > 0) {
1832 buf[used] = '\0';
1833 used++;
1834 } else
1835 buf[len - 1] = '\0';
1836
1837 return buf;
1838}
1839
cd4e1efb
IR
1840int perf_pmu__for_each_event(struct perf_pmu *pmu, bool skip_duplicate_pmus,
1841 void *state, pmu_event_callback cb)
c3245d20
IR
1842{
1843 char buf[1024];
1844 struct perf_pmu_alias *event;
1845 struct pmu_event_info info = {
1846 .pmu = pmu,
1847 };
1848 int ret = 0;
30f0b435 1849 struct strbuf sb;
c3245d20 1850
30f0b435 1851 strbuf_init(&sb, /*hint=*/ 0);
7b6dd7a9 1852 pmu_aliases_parse(pmu);
e6ff1eed 1853 pmu_add_cpu_aliases(pmu);
c3245d20 1854 list_for_each_entry(event, &pmu->aliases, list) {
3241d46f 1855 size_t buf_used, pmu_name_len;
c3245d20
IR
1856
1857 info.pmu_name = event->pmu_name ?: pmu->name;
7afbf90e
JC
1858 pmu_name_len = pmu_deduped_name_len(pmu, info.pmu_name,
1859 skip_duplicate_pmus);
c3245d20
IR
1860 info.alias = NULL;
1861 if (event->desc) {
1862 info.name = event->name;
1863 buf_used = 0;
1864 } else {
cd4e1efb
IR
1865 info.name = format_alias(buf, sizeof(buf), pmu, event,
1866 skip_duplicate_pmus);
c3245d20
IR
1867 if (pmu->is_core) {
1868 info.alias = info.name;
1869 info.name = event->name;
1870 }
1871 buf_used = strlen(buf) + 1;
1872 }
1873 info.scale_unit = NULL;
1874 if (strlen(event->unit) || event->scale != 1.0) {
1875 info.scale_unit = buf + buf_used;
1876 buf_used += snprintf(buf + buf_used, sizeof(buf) - buf_used,
1877 "%G%s", event->scale, event->unit) + 1;
1878 }
1879 info.desc = event->desc;
1880 info.long_desc = event->long_desc;
1881 info.encoding_desc = buf + buf_used;
0d3f0e6f 1882 parse_events_terms__to_strbuf(&event->terms, &sb);
c3245d20 1883 buf_used += snprintf(buf + buf_used, sizeof(buf) - buf_used,
3241d46f 1884 "%.*s/%s/", (int)pmu_name_len, info.pmu_name, sb.buf) + 1;
c3245d20 1885 info.topic = event->topic;
30f0b435 1886 info.str = sb.buf;
c3245d20
IR
1887 info.deprecated = event->deprecated;
1888 ret = cb(state, &info);
1889 if (ret)
30f0b435
IR
1890 goto out;
1891 strbuf_setlen(&sb, /*len=*/ 0);
c3245d20
IR
1892 }
1893 if (pmu->selectable) {
1894 info.name = buf;
1895 snprintf(buf, sizeof(buf), "%s//", pmu->name);
1896 info.alias = NULL;
1897 info.scale_unit = NULL;
1898 info.desc = NULL;
1899 info.long_desc = NULL;
1900 info.encoding_desc = NULL;
1901 info.topic = NULL;
1902 info.pmu_name = pmu->name;
1903 info.deprecated = false;
1904 ret = cb(state, &info);
1905 }
30f0b435
IR
1906out:
1907 strbuf_release(&sb);
c3245d20 1908 return ret;
4cabc3d1 1909}
7d4bdab5 1910
e3edd6cf
IR
1911bool pmu__name_match(const struct perf_pmu *pmu, const char *pmu_name)
1912{
1913 return !strcmp(pmu->name, pmu_name) ||
d2045f87
IR
1914 (pmu->is_uncore && pmu_uncore_alias_match(pmu_name, pmu->name)) ||
1915 /*
1916 * jevents and tests use default_core as a marker for any core
1917 * PMU as the PMU name varies across architectures.
1918 */
1919 (pmu->is_core && !strcmp(pmu_name, "default_core"));
e3edd6cf
IR
1920}
1921
251aa040
IR
1922bool perf_pmu__is_software(const struct perf_pmu *pmu)
1923{
24852ef2
IR
1924 const char *known_sw_pmus[] = {
1925 "kprobe",
1926 "msr",
1927 "uprobe",
1928 };
1929
251aa040
IR
1930 if (pmu->is_core || pmu->is_uncore || pmu->auxtrace)
1931 return false;
1932 switch (pmu->type) {
1933 case PERF_TYPE_HARDWARE: return false;
1934 case PERF_TYPE_SOFTWARE: return true;
1935 case PERF_TYPE_TRACEPOINT: return true;
1936 case PERF_TYPE_HW_CACHE: return false;
1937 case PERF_TYPE_RAW: return false;
1938 case PERF_TYPE_BREAKPOINT: return true;
1939 default: break;
1940 }
24852ef2
IR
1941 for (size_t i = 0; i < ARRAY_SIZE(known_sw_pmus); i++) {
1942 if (!strcmp(pmu->name, known_sw_pmus[i]))
1943 return true;
1944 }
1945 return false;
251aa040
IR
1946}
1947
3a42f4c7 1948FILE *perf_pmu__open_file(const struct perf_pmu *pmu, const char *name)
7d4bdab5 1949{
7d4bdab5 1950 char path[PATH_MAX];
7d4bdab5 1951
f8ad6018
JC
1952 if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, name) ||
1953 !file_available(path))
7d4bdab5
AH
1954 return NULL;
1955
7d4bdab5
AH
1956 return fopen(path, "r");
1957}
1958
3a42f4c7 1959FILE *perf_pmu__open_file_at(const struct perf_pmu *pmu, int dirfd, const char *name)
3a69672e
NK
1960{
1961 int fd;
1962
1963 fd = perf_pmu__pathname_fd(dirfd, pmu->name, name, O_RDONLY);
1964 if (fd < 0)
1965 return NULL;
1966
1967 return fdopen(fd, "r");
1968}
1969
3a42f4c7 1970int perf_pmu__scan_file(const struct perf_pmu *pmu, const char *name, const char *fmt,
7d4bdab5
AH
1971 ...)
1972{
1973 va_list args;
1974 FILE *file;
1975 int ret = EOF;
1976
1977 va_start(args, fmt);
1978 file = perf_pmu__open_file(pmu, name);
1979 if (file) {
1980 ret = vfscanf(file, fmt, args);
1981 fclose(file);
1982 }
1983 va_end(args);
1984 return ret;
1985}
9fbc61f8 1986
3a42f4c7 1987int perf_pmu__scan_file_at(const struct perf_pmu *pmu, int dirfd, const char *name,
3a69672e
NK
1988 const char *fmt, ...)
1989{
1990 va_list args;
1991 FILE *file;
1992 int ret = EOF;
1993
1994 va_start(args, fmt);
1995 file = perf_pmu__open_file_at(pmu, dirfd, name);
1996 if (file) {
1997 ret = vfscanf(file, fmt, args);
1998 fclose(file);
1999 }
2000 va_end(args);
2001 return ret;
2002}
2003
3a42f4c7 2004bool perf_pmu__file_exists(const struct perf_pmu *pmu, const char *name)
c2b6a896
GG
2005{
2006 char path[PATH_MAX];
2007
2008 if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, name))
2009 return false;
2010
2011 return file_available(path);
2012}
2013
9fbc61f8
KL
2014static int perf_pmu__new_caps(struct list_head *list, char *name, char *value)
2015{
2016 struct perf_pmu_caps *caps = zalloc(sizeof(*caps));
2017
2018 if (!caps)
2019 return -ENOMEM;
2020
2021 caps->name = strdup(name);
2022 if (!caps->name)
2023 goto free_caps;
2024 caps->value = strndup(value, strlen(value) - 1);
2025 if (!caps->value)
2026 goto free_name;
2027 list_add_tail(&caps->list, list);
2028 return 0;
2029
2030free_name:
57f14b5a 2031 zfree(&caps->name);
9fbc61f8
KL
2032free_caps:
2033 free(caps);
2034
2035 return -ENOMEM;
2036}
2037
eec11310
NK
2038static void perf_pmu__del_caps(struct perf_pmu *pmu)
2039{
2040 struct perf_pmu_caps *caps, *tmp;
2041
2042 list_for_each_entry_safe(caps, tmp, &pmu->caps, list) {
2043 list_del(&caps->list);
efe98a7a
ACM
2044 zfree(&caps->name);
2045 zfree(&caps->value);
eec11310
NK
2046 free(caps);
2047 }
2048}
2049
9fbc61f8
KL
2050/*
2051 * Reading/parsing the given pmu capabilities, which should be located at:
2052 * /sys/bus/event_source/devices/<dev>/caps as sysfs group attributes.
2053 * Return the number of capabilities
2054 */
2055int perf_pmu__caps_parse(struct perf_pmu *pmu)
2056{
2057 struct stat st;
2058 char caps_path[PATH_MAX];
9fbc61f8
KL
2059 DIR *caps_dir;
2060 struct dirent *evt_ent;
b39094d3 2061 int caps_fd;
3339ec44
RB
2062
2063 if (pmu->caps_initialized)
2064 return pmu->nr_caps;
2065
2066 pmu->nr_caps = 0;
9fbc61f8 2067
f8ad6018 2068 if (!perf_pmu__pathname_scnprintf(caps_path, sizeof(caps_path), pmu->name, "caps"))
9fbc61f8
KL
2069 return -1;
2070
3339ec44
RB
2071 if (stat(caps_path, &st) < 0) {
2072 pmu->caps_initialized = true;
9fbc61f8 2073 return 0; /* no error if caps does not exist */
3339ec44 2074 }
9fbc61f8
KL
2075
2076 caps_dir = opendir(caps_path);
2077 if (!caps_dir)
2078 return -EINVAL;
2079
b39094d3
NK
2080 caps_fd = dirfd(caps_dir);
2081
9fbc61f8 2082 while ((evt_ent = readdir(caps_dir)) != NULL) {
9fbc61f8
KL
2083 char *name = evt_ent->d_name;
2084 char value[128];
2085 FILE *file;
b39094d3 2086 int fd;
9fbc61f8
KL
2087
2088 if (!strcmp(name, ".") || !strcmp(name, ".."))
2089 continue;
2090
b39094d3 2091 fd = openat(caps_fd, name, O_RDONLY);
0ea8920e
IR
2092 if (fd == -1)
2093 continue;
b39094d3 2094 file = fdopen(fd, "r");
0ea8920e
IR
2095 if (!file) {
2096 close(fd);
9fbc61f8 2097 continue;
0ea8920e 2098 }
9fbc61f8
KL
2099
2100 if (!fgets(value, sizeof(value), file) ||
2101 (perf_pmu__new_caps(&pmu->caps, name, value) < 0)) {
2102 fclose(file);
2103 continue;
2104 }
2105
3339ec44 2106 pmu->nr_caps++;
9fbc61f8
KL
2107 fclose(file);
2108 }
2109
2110 closedir(caps_dir);
2111
3339ec44
RB
2112 pmu->caps_initialized = true;
2113 return pmu->nr_caps;
9fbc61f8 2114}
e4064776 2115
b9f01032 2116static void perf_pmu__compute_config_masks(struct perf_pmu *pmu)
e4064776
JY
2117{
2118 struct perf_pmu_format *format;
b9f01032
IR
2119
2120 if (pmu->config_masks_computed)
2121 return;
e4064776
JY
2122
2123 list_for_each_entry(format, &pmu->format, list) {
b9f01032
IR
2124 unsigned int i;
2125 __u64 *mask;
2126
2127 if (format->value >= PERF_PMU_FORMAT_VALUE_CONFIG_END)
e4064776
JY
2128 continue;
2129
b9f01032
IR
2130 pmu->config_masks_present = true;
2131 mask = &pmu->config_masks[format->value];
2132
e4064776 2133 for_each_set_bit(i, format->bits, PERF_PMU_FORMAT_BITS)
b9f01032 2134 *mask |= 1ULL << i;
e4064776 2135 }
b9f01032
IR
2136 pmu->config_masks_computed = true;
2137}
2138
2139void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config,
2140 const char *name, int config_num,
2141 const char *config_name)
2142{
2143 __u64 bits;
2144 char buf[100];
2145
2146 perf_pmu__compute_config_masks(pmu);
e4064776
JY
2147
2148 /*
2149 * Kernel doesn't export any valid format bits.
2150 */
b9f01032 2151 if (!pmu->config_masks_present)
e4064776
JY
2152 return;
2153
b9f01032 2154 bits = config & ~pmu->config_masks[config_num];
e4064776
JY
2155 if (bits == 0)
2156 return;
2157
2158 bitmap_scnprintf((unsigned long *)&bits, sizeof(bits) * 8, buf, sizeof(buf));
2159
b9f01032 2160 pr_warning("WARNING: event '%s' not valid (bits %s of %s "
e4064776 2161 "'%llx' not supported by kernel)!\n",
b9f01032 2162 name ?: "N/A", buf, config_name, config);
e4064776 2163}
c5a26ea4 2164
f91fa2ae 2165bool perf_pmu__match(const struct perf_pmu *pmu, const char *tok)
c47a5599 2166{
f91fa2ae 2167 const char *name = pmu->name;
3e0bf9fd 2168 bool need_fnmatch = strisglob(tok);
13d60ba0 2169
f91fa2ae
IR
2170 if (!strncmp(tok, "uncore_", 7))
2171 tok += 7;
2172 if (!strncmp(name, "uncore_", 7))
2173 name += 7;
c47a5599 2174
f91fa2ae
IR
2175 if (perf_pmu__match_ignoring_suffix(name, tok) ||
2176 (need_fnmatch && !fnmatch(tok, name, 0)))
2177 return true;
c47a5599 2178
f91fa2ae
IR
2179 name = pmu->alias_name;
2180 if (!name)
2181 return false;
2182
2183 if (!strncmp(name, "uncore_", 7))
2184 name += 7;
2185
2186 return perf_pmu__match_ignoring_suffix(name, tok) ||
2187 (need_fnmatch && !fnmatch(tok, name, 0));
c47a5599 2188}
1d3351e6 2189
acef233b
JZ
2190double __weak perf_pmu__cpu_slots_per_cycle(void)
2191{
2192 return NAN;
2193}
f8ad6018
JC
2194
2195int perf_pmu__event_source_devices_scnprintf(char *pathname, size_t size)
2196{
2197 const char *sysfs = sysfs__mountpoint();
2198
2199 if (!sysfs)
2200 return 0;
2201 return scnprintf(pathname, size, "%s/bus/event_source/devices/", sysfs);
2202}
2203
e293a5e8
NK
2204int perf_pmu__event_source_devices_fd(void)
2205{
2206 char path[PATH_MAX];
2207 const char *sysfs = sysfs__mountpoint();
2208
2209 if (!sysfs)
2210 return -1;
2211
2212 scnprintf(path, sizeof(path), "%s/bus/event_source/devices/", sysfs);
2213 return open(path, O_DIRECTORY);
2214}
2215
f8ad6018
JC
2216/*
2217 * Fill 'buf' with the path to a file or folder in 'pmu_name' in
2218 * sysfs. For example if pmu_name = "cs_etm" and 'filename' = "format"
2219 * then pathname will be filled with
2220 * "/sys/bus/event_source/devices/cs_etm/format"
2221 *
e1a3aad3
IR
2222 * Return 0 if the sysfs mountpoint couldn't be found, if no characters were
2223 * written or if the buffer size is exceeded.
f8ad6018
JC
2224 */
2225int perf_pmu__pathname_scnprintf(char *buf, size_t size,
2226 const char *pmu_name, const char *filename)
2227{
e1a3aad3 2228 size_t len;
f8ad6018 2229
e1a3aad3
IR
2230 len = perf_pmu__event_source_devices_scnprintf(buf, size);
2231 if (!len || (len + strlen(pmu_name) + strlen(filename) + 1) >= size)
f8ad6018 2232 return 0;
e1a3aad3
IR
2233
2234 return scnprintf(buf + len, size - len, "%s/%s", pmu_name, filename);
f8ad6018 2235}
eec11310 2236
e293a5e8
NK
2237int perf_pmu__pathname_fd(int dirfd, const char *pmu_name, const char *filename, int flags)
2238{
2239 char path[PATH_MAX];
2240
2241 scnprintf(path, sizeof(path), "%s/%s", pmu_name, filename);
2242 return openat(dirfd, path, flags);
2243}
2244
1eaf496e 2245void perf_pmu__delete(struct perf_pmu *pmu)
eec11310
NK
2246{
2247 perf_pmu__del_formats(&pmu->format);
2248 perf_pmu__del_aliases(pmu);
2249 perf_pmu__del_caps(pmu);
2250
2251 perf_cpu_map__put(pmu->cpus);
2252
efe98a7a
ACM
2253 zfree(&pmu->name);
2254 zfree(&pmu->alias_name);
b7823045 2255 zfree(&pmu->id);
eec11310
NK
2256 free(pmu);
2257}
67ee8e71
IR
2258
2259const char *perf_pmu__name_from_config(struct perf_pmu *pmu, u64 config)
2260{
2261 struct perf_pmu_alias *event;
2262
2263 if (!pmu)
2264 return NULL;
2265
7b6dd7a9 2266 pmu_aliases_parse(pmu);
67ee8e71
IR
2267 pmu_add_cpu_aliases(pmu);
2268 list_for_each_entry(event, &pmu->aliases, list) {
2269 struct perf_event_attr attr = {.config = 0,};
2270 int ret = perf_pmu__config(pmu, &attr, &event->terms, NULL);
2271
2272 if (ret == 0 && config == attr.config)
2273 return event->name;
2274 }
2275 return NULL;
2276}