blob: 44225838eb039d51afc1be4da3a709cbfbc2ccec [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Jiri Olsacd82a322012-03-15 20:09:17 +01002#include <linux/list.h>
Sukadev Bhattiproluc5de47f2015-06-10 00:25:07 -07003#include <linux/compiler.h>
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03004#include <linux/string.h>
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -03005#include <linux/zalloc.h>
Arnaldo Carvalho de Melofa0d9842019-08-30 12:52:25 -03006#include <subcmd/pager.h>
Jiri Olsacd82a322012-03-15 20:09:17 +01007#include <sys/types.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03008#include <errno.h>
Arnaldo Carvalho de Meloc23c2a02017-09-11 10:50:26 -03009#include <fcntl.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030010#include <sys/stat.h>
Jiri Olsacd82a322012-03-15 20:09:17 +010011#include <unistd.h>
12#include <stdio.h>
Adrian Hunterdc0a6202014-07-31 09:00:49 +030013#include <stdbool.h>
Adrian Hunter7d4bdab2014-07-31 09:00:50 +030014#include <stdarg.h>
Jiri Olsacd82a322012-03-15 20:09:17 +010015#include <dirent.h>
Borislav Petkovcd0cfad2013-12-09 17:14:24 +010016#include <api/fs/fs.h>
Stephane Eranian410136f2013-11-12 17:58:49 +010017#include <locale.h>
William Cohenfbc28442017-12-04 09:57:28 -050018#include <regex.h>
Jiri Olsa9c3516d2019-07-21 13:24:30 +020019#include <perf/cpumap.h>
Arnaldo Carvalho de Melo5e51b0b2019-08-22 10:48:31 -030020#include "debug.h"
Adrian Huntere12ee9f2020-04-01 13:16:09 +030021#include "evsel.h"
Jiri Olsacd82a322012-03-15 20:09:17 +010022#include "pmu.h"
23#include "parse-events.h"
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -070024#include "header.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030025#include "string2.h"
Arnaldo Carvalho de Melofa0d9842019-08-30 12:52:25 -030026#include "strbuf.h"
Andi Kleend9664582019-11-20 16:15:11 -080027#include "fncache.h"
Jiri Olsacd82a322012-03-15 20:09:17 +010028
Arnaldo Carvalho de Meloe46fc8d2020-06-09 13:23:24 -030029struct perf_pmu perf_pmu__fake;
30
Arnaldo Carvalho de Meloab1bf6532013-01-18 17:05:09 -030031struct perf_pmu_format {
32 char *name;
33 int value;
34 DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
35 struct list_head list;
36};
37
Jiri Olsacd82a322012-03-15 20:09:17 +010038int perf_pmu_parse(struct list_head *list, char *name);
39extern FILE *perf_pmu_in;
40
41static LIST_HEAD(pmus);
42
43/*
44 * Parse & process all the sysfs attributes located under
45 * the directory specified in 'dir' parameter.
46 */
Jiri Olsacff7f952012-11-10 01:46:50 +010047int perf_pmu__format_parse(char *dir, struct list_head *head)
Jiri Olsacd82a322012-03-15 20:09:17 +010048{
49 struct dirent *evt_ent;
50 DIR *format_dir;
51 int ret = 0;
52
53 format_dir = opendir(dir);
54 if (!format_dir)
55 return -EINVAL;
56
57 while (!ret && (evt_ent = readdir(format_dir))) {
58 char path[PATH_MAX];
59 char *name = evt_ent->d_name;
60 FILE *file;
61
62 if (!strcmp(name, ".") || !strcmp(name, ".."))
63 continue;
64
65 snprintf(path, PATH_MAX, "%s/%s", dir, name);
66
67 ret = -EINVAL;
68 file = fopen(path, "r");
69 if (!file)
70 break;
71
72 perf_pmu_in = file;
73 ret = perf_pmu_parse(head, name);
74 fclose(file);
75 }
76
77 closedir(format_dir);
78 return ret;
79}
80
81/*
82 * Reading/parsing the default pmu format definition, which should be
83 * located at:
84 * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
85 */
Adrian Hunterb6b96fb2013-07-04 16:20:25 +030086static int pmu_format(const char *name, struct list_head *format)
Jiri Olsacd82a322012-03-15 20:09:17 +010087{
Jiri Olsacd82a322012-03-15 20:09:17 +010088 char path[PATH_MAX];
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -030089 const char *sysfs = sysfs__mountpoint();
Jiri Olsacd82a322012-03-15 20:09:17 +010090
Jiri Olsacd82a322012-03-15 20:09:17 +010091 if (!sysfs)
92 return -1;
93
94 snprintf(path, PATH_MAX,
Robert Richter50a96672012-08-16 21:10:24 +020095 "%s" EVENT_SOURCE_DEVICE_PATH "%s/format", sysfs, name);
Jiri Olsacd82a322012-03-15 20:09:17 +010096
Andi Kleend9664582019-11-20 16:15:11 -080097 if (!file_available(path))
98 return 0;
Jiri Olsacd82a322012-03-15 20:09:17 +010099
Jiri Olsacff7f952012-11-10 01:46:50 +0100100 if (perf_pmu__format_parse(path, format))
Jiri Olsacd82a322012-03-15 20:09:17 +0100101 return -1;
102
103 return 0;
104}
105
Jin Yaoa55ab7c2019-08-28 13:59:29 +0800106int perf_pmu__convert_scale(const char *scale, char **end, double *sval)
Andi Kleend02fc6b2017-01-03 07:08:23 -0800107{
108 char *lc;
109 int ret = 0;
110
111 /*
112 * save current locale
113 */
114 lc = setlocale(LC_NUMERIC, NULL);
115
116 /*
117 * The lc string may be allocated in static storage,
118 * so get a dynamic copy to make it survive setlocale
119 * call below.
120 */
121 lc = strdup(lc);
122 if (!lc) {
123 ret = -ENOMEM;
124 goto out;
125 }
126
127 /*
128 * force to C locale to ensure kernel
129 * scale string is converted correctly.
130 * kernel uses default C locale.
131 */
132 setlocale(LC_NUMERIC, "C");
133
134 *sval = strtod(scale, end);
135
136out:
137 /* restore locale */
138 setlocale(LC_NUMERIC, lc);
139 free(lc);
140 return ret;
141}
142
Stephane Eranian410136f2013-11-12 17:58:49 +0100143static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *name)
144{
145 struct stat st;
146 ssize_t sret;
147 char scale[128];
148 int fd, ret = -1;
149 char path[PATH_MAX];
Stephane Eranian410136f2013-11-12 17:58:49 +0100150
Ben Hutchings11a64a02018-11-11 18:45:24 +0000151 scnprintf(path, PATH_MAX, "%s/%s.scale", dir, name);
Stephane Eranian410136f2013-11-12 17:58:49 +0100152
153 fd = open(path, O_RDONLY);
154 if (fd == -1)
155 return -1;
156
157 if (fstat(fd, &st) < 0)
158 goto error;
159
160 sret = read(fd, scale, sizeof(scale)-1);
161 if (sret < 0)
162 goto error;
163
Madhavan Srinivasan9ecae062015-05-31 11:36:23 +0530164 if (scale[sret - 1] == '\n')
165 scale[sret - 1] = '\0';
166 else
167 scale[sret] = '\0';
168
Jin Yaoa55ab7c2019-08-28 13:59:29 +0800169 ret = perf_pmu__convert_scale(scale, NULL, &alias->scale);
Stephane Eranian410136f2013-11-12 17:58:49 +0100170error:
171 close(fd);
172 return ret;
173}
174
175static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *name)
176{
177 char path[PATH_MAX];
178 ssize_t sret;
179 int fd;
180
Ben Hutchings11a64a02018-11-11 18:45:24 +0000181 scnprintf(path, PATH_MAX, "%s/%s.unit", dir, name);
Stephane Eranian410136f2013-11-12 17:58:49 +0100182
183 fd = open(path, O_RDONLY);
184 if (fd == -1)
185 return -1;
186
Markus Trippelsdorfd85ce832015-12-14 16:44:40 +0100187 sret = read(fd, alias->unit, UNIT_MAX_LEN);
Stephane Eranian410136f2013-11-12 17:58:49 +0100188 if (sret < 0)
189 goto error;
190
191 close(fd);
192
Madhavan Srinivasan9ecae062015-05-31 11:36:23 +0530193 if (alias->unit[sret - 1] == '\n')
194 alias->unit[sret - 1] = '\0';
195 else
196 alias->unit[sret] = '\0';
Stephane Eranian410136f2013-11-12 17:58:49 +0100197
198 return 0;
199error:
200 close(fd);
201 alias->unit[0] = '\0';
202 return -1;
203}
204
Matt Fleming044330c2014-11-21 10:31:12 +0100205static int
206perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, char *dir, char *name)
207{
208 char path[PATH_MAX];
209 int fd;
210
Ben Hutchings11a64a02018-11-11 18:45:24 +0000211 scnprintf(path, PATH_MAX, "%s/%s.per-pkg", dir, name);
Matt Fleming044330c2014-11-21 10:31:12 +0100212
213 fd = open(path, O_RDONLY);
214 if (fd == -1)
215 return -1;
216
217 close(fd);
218
219 alias->per_pkg = true;
220 return 0;
221}
222
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100223static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias,
224 char *dir, char *name)
225{
226 char path[PATH_MAX];
227 int fd;
228
Ben Hutchings11a64a02018-11-11 18:45:24 +0000229 scnprintf(path, PATH_MAX, "%s/%s.snapshot", dir, name);
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100230
231 fd = open(path, O_RDONLY);
232 if (fd == -1)
233 return -1;
234
235 alias->snapshot = true;
236 close(fd);
237 return 0;
238}
239
Thomas Richter6dde6422018-06-15 12:11:05 +0200240static void perf_pmu_assign_str(char *name, const char *field, char **old_str,
241 char **new_str)
242{
243 if (!*old_str)
244 goto set_new;
245
246 if (*new_str) { /* Have new string, check with old */
247 if (strcasecmp(*old_str, *new_str))
248 pr_debug("alias %s differs in field '%s'\n",
249 name, field);
250 zfree(old_str);
251 } else /* Nothing new --> keep old string */
252 return;
253set_new:
254 *old_str = *new_str;
255 *new_str = NULL;
256}
257
258static void perf_pmu_update_alias(struct perf_pmu_alias *old,
259 struct perf_pmu_alias *newalias)
260{
261 perf_pmu_assign_str(old->name, "desc", &old->desc, &newalias->desc);
262 perf_pmu_assign_str(old->name, "long_desc", &old->long_desc,
263 &newalias->long_desc);
264 perf_pmu_assign_str(old->name, "topic", &old->topic, &newalias->topic);
265 perf_pmu_assign_str(old->name, "metric_expr", &old->metric_expr,
266 &newalias->metric_expr);
267 perf_pmu_assign_str(old->name, "metric_name", &old->metric_name,
268 &newalias->metric_name);
269 perf_pmu_assign_str(old->name, "value", &old->str, &newalias->str);
270 old->scale = newalias->scale;
271 old->per_pkg = newalias->per_pkg;
272 old->snapshot = newalias->snapshot;
273 memcpy(old->unit, newalias->unit, sizeof(old->unit));
274}
275
276/* Delete an alias entry. */
Namhyung Kim22fe5a22020-09-15 12:18:18 +0900277void perf_pmu_free_alias(struct perf_pmu_alias *newalias)
Thomas Richter6dde6422018-06-15 12:11:05 +0200278{
279 zfree(&newalias->name);
280 zfree(&newalias->desc);
281 zfree(&newalias->long_desc);
282 zfree(&newalias->topic);
283 zfree(&newalias->str);
284 zfree(&newalias->metric_expr);
285 zfree(&newalias->metric_name);
Jin Yao32705de2021-04-27 15:01:17 +0800286 zfree(&newalias->pmu_name);
Thomas Richter6dde6422018-06-15 12:11:05 +0200287 parse_events_terms__purge(&newalias->terms);
288 free(newalias);
289}
290
291/* Merge an alias, search in alias list. If this name is already
292 * present merge both of them to combine all information.
293 */
294static bool perf_pmu_merge_alias(struct perf_pmu_alias *newalias,
295 struct list_head *alist)
296{
297 struct perf_pmu_alias *a;
298
299 list_for_each_entry(a, alist, list) {
300 if (!strcasecmp(newalias->name, a->name)) {
Jin Yao32705de2021-04-27 15:01:17 +0800301 if (newalias->pmu_name && a->pmu_name &&
302 !strcasecmp(newalias->pmu_name, a->pmu_name)) {
303 continue;
304 }
Thomas Richter6dde6422018-06-15 12:11:05 +0200305 perf_pmu_update_alias(a, newalias);
306 perf_pmu_free_alias(newalias);
307 return true;
308 }
309 }
310 return false;
311}
312
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700313static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name,
Jin Yaoeab35952021-04-27 15:01:16 +0800314 char *desc, char *val, struct pmu_event *pe)
Zheng Yana6146d52012-06-15 14:31:41 +0800315{
Thomas Richter0c24d6f2018-06-15 12:11:04 +0200316 struct parse_events_term *term;
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300317 struct perf_pmu_alias *alias;
Zheng Yana6146d52012-06-15 14:31:41 +0800318 int ret;
Andi Kleenfedb2b52017-01-27 18:03:37 -0800319 int num;
Thomas Richter0c24d6f2018-06-15 12:11:04 +0200320 char newval[256];
Jin Yaoeab35952021-04-27 15:01:16 +0800321 char *long_desc = NULL, *topic = NULL, *unit = NULL, *perpkg = NULL,
Jin Yao32705de2021-04-27 15:01:17 +0800322 *metric_expr = NULL, *metric_name = NULL, *deprecated = NULL,
323 *pmu_name = NULL;
Jin Yaoeab35952021-04-27 15:01:16 +0800324
325 if (pe) {
326 long_desc = (char *)pe->long_desc;
327 topic = (char *)pe->topic;
328 unit = (char *)pe->unit;
329 perpkg = (char *)pe->perpkg;
330 metric_expr = (char *)pe->metric_expr;
331 metric_name = (char *)pe->metric_name;
332 deprecated = (char *)pe->deprecated;
Jin Yao32705de2021-04-27 15:01:17 +0800333 pmu_name = (char *)pe->pmu;
Jin Yaoeab35952021-04-27 15:01:16 +0800334 }
Zheng Yana6146d52012-06-15 14:31:41 +0800335
Zheng Yana6146d52012-06-15 14:31:41 +0800336 alias = malloc(sizeof(*alias));
337 if (!alias)
338 return -ENOMEM;
339
340 INIT_LIST_HEAD(&alias->terms);
Stephane Eranian410136f2013-11-12 17:58:49 +0100341 alias->scale = 1.0;
342 alias->unit[0] = '\0';
Matt Fleming044330c2014-11-21 10:31:12 +0100343 alias->per_pkg = false;
Stephane Eranian84530922016-01-06 19:50:01 +0100344 alias->snapshot = false;
Jin Yaoa7f6c8c2019-10-15 10:53:57 +0800345 alias->deprecated = false;
Stephane Eranian410136f2013-11-12 17:58:49 +0100346
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700347 ret = parse_events_terms(&alias->terms, val);
Zheng Yana6146d52012-06-15 14:31:41 +0800348 if (ret) {
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700349 pr_err("Cannot parse alias %s: %d\n", val, ret);
Zheng Yana6146d52012-06-15 14:31:41 +0800350 free(alias);
351 return ret;
352 }
353
Thomas Richter0c24d6f2018-06-15 12:11:04 +0200354 /* Scan event and remove leading zeroes, spaces, newlines, some
355 * platforms have terms specified as
356 * event=0x0091 (read from files ../<PMU>/events/<FILE>
357 * and terms specified as event=0x91 (read from JSON files).
358 *
359 * Rebuild string to make alias->str member comparable.
360 */
361 memset(newval, 0, sizeof(newval));
362 ret = 0;
363 list_for_each_entry(term, &alias->terms, list) {
364 if (ret)
365 ret += scnprintf(newval + ret, sizeof(newval) - ret,
366 ",");
367 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM)
368 ret += scnprintf(newval + ret, sizeof(newval) - ret,
369 "%s=%#x", term->config, term->val.num);
370 else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
371 ret += scnprintf(newval + ret, sizeof(newval) - ret,
372 "%s=%s", term->config, term->val.str);
373 }
374
Zheng Yana6146d52012-06-15 14:31:41 +0800375 alias->name = strdup(name);
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700376 if (dir) {
377 /*
378 * load unit name and scale if available
379 */
380 perf_pmu__parse_unit(alias, dir, name);
381 perf_pmu__parse_scale(alias, dir, name);
382 perf_pmu__parse_per_pkg(alias, dir, name);
383 perf_pmu__parse_snapshot(alias, dir, name);
384 }
Stephane Eranian410136f2013-11-12 17:58:49 +0100385
Andi Kleen00636c32017-03-20 13:17:07 -0700386 alias->metric_expr = metric_expr ? strdup(metric_expr) : NULL;
Andi Kleen96284812017-03-20 13:17:10 -0700387 alias->metric_name = metric_name ? strdup(metric_name): NULL;
Andi Kleen08e60ed2016-09-15 15:24:43 -0700388 alias->desc = desc ? strdup(desc) : NULL;
Sukadev Bhattiproluc8d68282016-09-15 15:24:48 -0700389 alias->long_desc = long_desc ? strdup(long_desc) :
390 desc ? strdup(desc) : NULL;
Andi Kleendd5f1032016-09-15 15:24:50 -0700391 alias->topic = topic ? strdup(topic) : NULL;
Andi Kleenfedb2b52017-01-27 18:03:37 -0800392 if (unit) {
Jin Yaoa55ab7c2019-08-28 13:59:29 +0800393 if (perf_pmu__convert_scale(unit, &unit, &alias->scale) < 0)
Andi Kleenfedb2b52017-01-27 18:03:37 -0800394 return -1;
395 snprintf(alias->unit, sizeof(alias->unit), "%s", unit);
396 }
397 alias->per_pkg = perpkg && sscanf(perpkg, "%d", &num) == 1 && num == 1;
Thomas Richter0c24d6f2018-06-15 12:11:04 +0200398 alias->str = strdup(newval);
Jin Yao32705de2021-04-27 15:01:17 +0800399 alias->pmu_name = pmu_name ? strdup(pmu_name) : NULL;
Andi Kleenf2361022017-01-27 18:03:40 -0800400
Jin Yaoa7f6c8c2019-10-15 10:53:57 +0800401 if (deprecated)
402 alias->deprecated = true;
403
Thomas Richter6dde6422018-06-15 12:11:05 +0200404 if (!perf_pmu_merge_alias(alias, list))
405 list_add_tail(&alias->list, list);
Stephane Eranian410136f2013-11-12 17:58:49 +0100406
Zheng Yana6146d52012-06-15 14:31:41 +0800407 return 0;
408}
409
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700410static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file)
411{
412 char buf[256];
413 int ret;
414
415 ret = fread(buf, 1, sizeof(buf), file);
416 if (ret == 0)
417 return -EINVAL;
418
419 buf[ret] = 0;
420
Thomas Richterea23ac72018-06-15 12:11:03 +0200421 /* Remove trailing newline from sysfs file */
Arnaldo Carvalho de Melo13c230a2019-06-26 12:13:13 -0300422 strim(buf);
Thomas Richterea23ac72018-06-15 12:11:03 +0200423
Jin Yaoeab35952021-04-27 15:01:16 +0800424 return __perf_pmu__new_alias(list, dir, name, NULL, buf, NULL);
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700425}
426
Matt Fleming46441bd2014-09-24 15:04:06 +0100427static inline bool pmu_alias_info_file(char *name)
428{
429 size_t len;
430
431 len = strlen(name);
432 if (len > 5 && !strcmp(name + len - 5, ".unit"))
433 return true;
434 if (len > 6 && !strcmp(name + len - 6, ".scale"))
435 return true;
Matt Fleming044330c2014-11-21 10:31:12 +0100436 if (len > 8 && !strcmp(name + len - 8, ".per-pkg"))
437 return true;
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100438 if (len > 9 && !strcmp(name + len - 9, ".snapshot"))
439 return true;
Matt Fleming46441bd2014-09-24 15:04:06 +0100440
441 return false;
442}
443
Zheng Yana6146d52012-06-15 14:31:41 +0800444/*
445 * Process all the sysfs attributes located under the directory
446 * specified in 'dir' parameter.
447 */
448static int pmu_aliases_parse(char *dir, struct list_head *head)
449{
450 struct dirent *evt_ent;
451 DIR *event_dir;
Zheng Yana6146d52012-06-15 14:31:41 +0800452
453 event_dir = opendir(dir);
454 if (!event_dir)
455 return -EINVAL;
456
Andi Kleen940db6d2016-02-17 14:44:55 -0800457 while ((evt_ent = readdir(event_dir))) {
Zheng Yana6146d52012-06-15 14:31:41 +0800458 char path[PATH_MAX];
459 char *name = evt_ent->d_name;
460 FILE *file;
461
462 if (!strcmp(name, ".") || !strcmp(name, ".."))
463 continue;
464
Stephane Eranian410136f2013-11-12 17:58:49 +0100465 /*
Matt Fleming46441bd2014-09-24 15:04:06 +0100466 * skip info files parsed in perf_pmu__new_alias()
Stephane Eranian410136f2013-11-12 17:58:49 +0100467 */
Matt Fleming46441bd2014-09-24 15:04:06 +0100468 if (pmu_alias_info_file(name))
Stephane Eranian410136f2013-11-12 17:58:49 +0100469 continue;
470
Jiri Olsa77f18152018-03-19 09:29:01 +0100471 scnprintf(path, PATH_MAX, "%s/%s", dir, name);
Zheng Yana6146d52012-06-15 14:31:41 +0800472
Zheng Yana6146d52012-06-15 14:31:41 +0800473 file = fopen(path, "r");
Andi Kleen940db6d2016-02-17 14:44:55 -0800474 if (!file) {
475 pr_debug("Cannot open %s\n", path);
476 continue;
477 }
Stephane Eranian410136f2013-11-12 17:58:49 +0100478
Andi Kleen940db6d2016-02-17 14:44:55 -0800479 if (perf_pmu__new_alias(head, dir, name, file) < 0)
480 pr_debug("Cannot set up %s\n", name);
Zheng Yana6146d52012-06-15 14:31:41 +0800481 fclose(file);
482 }
483
484 closedir(event_dir);
Andi Kleen940db6d2016-02-17 14:44:55 -0800485 return 0;
Zheng Yana6146d52012-06-15 14:31:41 +0800486}
487
488/*
489 * Reading the pmu event aliases definition, which should be located at:
490 * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
491 */
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300492static int pmu_aliases(const char *name, struct list_head *head)
Zheng Yana6146d52012-06-15 14:31:41 +0800493{
Zheng Yana6146d52012-06-15 14:31:41 +0800494 char path[PATH_MAX];
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -0300495 const char *sysfs = sysfs__mountpoint();
Zheng Yana6146d52012-06-15 14:31:41 +0800496
Zheng Yana6146d52012-06-15 14:31:41 +0800497 if (!sysfs)
498 return -1;
499
500 snprintf(path, PATH_MAX,
501 "%s/bus/event_source/devices/%s/events", sysfs, name);
502
Andi Kleend9664582019-11-20 16:15:11 -0800503 if (!file_available(path))
504 return 0;
Zheng Yana6146d52012-06-15 14:31:41 +0800505
506 if (pmu_aliases_parse(path, head))
507 return -1;
508
509 return 0;
510}
511
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300512static int pmu_alias_terms(struct perf_pmu_alias *alias,
Zheng Yana6146d52012-06-15 14:31:41 +0800513 struct list_head *terms)
514{
Jiri Olsa7c2f8162014-04-16 20:49:02 +0200515 struct parse_events_term *term, *cloned;
Zheng Yana6146d52012-06-15 14:31:41 +0800516 LIST_HEAD(list);
517 int ret;
518
519 list_for_each_entry(term, &alias->terms, list) {
Jiri Olsa7c2f8162014-04-16 20:49:02 +0200520 ret = parse_events_term__clone(&cloned, term);
Zheng Yana6146d52012-06-15 14:31:41 +0800521 if (ret) {
Arnaldo Carvalho de Melo682dc242016-02-12 16:48:00 -0300522 parse_events_terms__purge(&list);
Zheng Yana6146d52012-06-15 14:31:41 +0800523 return ret;
524 }
Andi Kleenc2f1cea2017-10-20 13:27:55 -0700525 /*
526 * Weak terms don't override command line options,
527 * which we don't want for implicit terms in aliases.
528 */
529 cloned->weak = true;
Jiri Olsa7c2f8162014-04-16 20:49:02 +0200530 list_add_tail(&cloned->list, &list);
Zheng Yana6146d52012-06-15 14:31:41 +0800531 }
532 list_splice(&list, terms);
533 return 0;
534}
535
Jiri Olsacd82a322012-03-15 20:09:17 +0100536/*
537 * Reading/parsing the default pmu type value, which should be
538 * located at:
539 * /sys/bus/event_source/devices/<dev>/type as sysfs attribute.
540 */
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300541static int pmu_type(const char *name, __u32 *type)
Jiri Olsacd82a322012-03-15 20:09:17 +0100542{
Jiri Olsacd82a322012-03-15 20:09:17 +0100543 char path[PATH_MAX];
Jiri Olsacd82a322012-03-15 20:09:17 +0100544 FILE *file;
545 int ret = 0;
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -0300546 const char *sysfs = sysfs__mountpoint();
Jiri Olsacd82a322012-03-15 20:09:17 +0100547
Jiri Olsacd82a322012-03-15 20:09:17 +0100548 if (!sysfs)
549 return -1;
550
551 snprintf(path, PATH_MAX,
Robert Richter50a96672012-08-16 21:10:24 +0200552 "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name);
Jiri Olsacd82a322012-03-15 20:09:17 +0100553
Andi Kleend9664582019-11-20 16:15:11 -0800554 if (access(path, R_OK) < 0)
Jiri Olsacd82a322012-03-15 20:09:17 +0100555 return -1;
556
557 file = fopen(path, "r");
558 if (!file)
559 return -EINVAL;
560
561 if (1 != fscanf(file, "%u", type))
562 ret = -1;
563
564 fclose(file);
565 return ret;
566}
567
Robert Richter50a96672012-08-16 21:10:24 +0200568/* Add all pmus in sysfs to pmu list: */
569static void pmu_read_sysfs(void)
570{
571 char path[PATH_MAX];
Robert Richter50a96672012-08-16 21:10:24 +0200572 DIR *dir;
573 struct dirent *dent;
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -0300574 const char *sysfs = sysfs__mountpoint();
Robert Richter50a96672012-08-16 21:10:24 +0200575
Robert Richter50a96672012-08-16 21:10:24 +0200576 if (!sysfs)
577 return;
578
579 snprintf(path, PATH_MAX,
580 "%s" EVENT_SOURCE_DEVICE_PATH, sysfs);
581
582 dir = opendir(path);
583 if (!dir)
584 return;
585
586 while ((dent = readdir(dir))) {
587 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
588 continue;
589 /* add to static LIST_HEAD(pmus): */
590 perf_pmu__find(dent->d_name);
591 }
592
593 closedir(dir);
594}
595
Jiri Olsaf8548392019-07-21 13:23:49 +0200596static struct perf_cpu_map *__pmu_cpumask(const char *path)
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800597{
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800598 FILE *file;
Jiri Olsaf8548392019-07-21 13:23:49 +0200599 struct perf_cpu_map *cpus;
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800600
601 file = fopen(path, "r");
602 if (!file)
603 return NULL;
604
Jiri Olsa9c3516d2019-07-21 13:24:30 +0200605 cpus = perf_cpu_map__read(file);
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800606 fclose(file);
607 return cpus;
608}
609
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700610/*
Mark Rutland66ec11912017-10-06 19:38:22 +0100611 * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64)
612 * may have a "cpus" file.
613 */
John Garry51d54842020-12-04 19:10:09 +0800614#define SYS_TEMPLATE_ID "./bus/event_source/devices/%s/identifier"
Mark Rutland66ec11912017-10-06 19:38:22 +0100615#define CPUS_TEMPLATE_UNCORE "%s/bus/event_source/devices/%s/cpumask"
616#define CPUS_TEMPLATE_CPU "%s/bus/event_source/devices/%s/cpus"
617
Jiri Olsaf8548392019-07-21 13:23:49 +0200618static struct perf_cpu_map *pmu_cpumask(const char *name)
Mark Rutland66ec11912017-10-06 19:38:22 +0100619{
620 char path[PATH_MAX];
Jiri Olsaf8548392019-07-21 13:23:49 +0200621 struct perf_cpu_map *cpus;
Mark Rutland66ec11912017-10-06 19:38:22 +0100622 const char *sysfs = sysfs__mountpoint();
623 const char *templates[] = {
624 CPUS_TEMPLATE_UNCORE,
625 CPUS_TEMPLATE_CPU,
626 NULL
627 };
628 const char **template;
629
630 if (!sysfs)
631 return NULL;
632
633 for (template = templates; *template; template++) {
634 snprintf(path, PATH_MAX, *template, sysfs, name);
635 cpus = __pmu_cpumask(path);
636 if (cpus)
637 return cpus;
638 }
639
640 return NULL;
641}
642
643static bool pmu_is_uncore(const char *name)
644{
645 char path[PATH_MAX];
Andi Kleend9664582019-11-20 16:15:11 -0800646 const char *sysfs;
Mark Rutland66ec11912017-10-06 19:38:22 +0100647
Andi Kleend9664582019-11-20 16:15:11 -0800648 sysfs = sysfs__mountpoint();
Mark Rutland66ec11912017-10-06 19:38:22 +0100649 snprintf(path, PATH_MAX, CPUS_TEMPLATE_UNCORE, sysfs, name);
Andi Kleend9664582019-11-20 16:15:11 -0800650 return file_available(path);
Mark Rutland66ec11912017-10-06 19:38:22 +0100651}
652
John Garry51d54842020-12-04 19:10:09 +0800653static char *pmu_id(const char *name)
654{
655 char path[PATH_MAX], *str;
656 size_t len;
657
658 snprintf(path, PATH_MAX, SYS_TEMPLATE_ID, name);
659
660 if (sysfs__read_str(path, &str, &len) < 0)
661 return NULL;
662
663 str[len - 1] = 0; /* remove line feed */
664
665 return str;
666}
667
Mark Rutland66ec11912017-10-06 19:38:22 +0100668/*
Ganapatrao Kulkarni14b22ae2017-08-24 16:30:58 +0530669 * PMU CORE devices have different name other than cpu in sysfs on some
Kan Liang292c34c2018-04-24 11:20:10 -0700670 * platforms.
671 * Looking for possible sysfs files to identify the arm core device.
Ganapatrao Kulkarni14b22ae2017-08-24 16:30:58 +0530672 */
Kan Liang292c34c2018-04-24 11:20:10 -0700673static int is_arm_pmu_core(const char *name)
Ganapatrao Kulkarni14b22ae2017-08-24 16:30:58 +0530674{
Ganapatrao Kulkarni14b22ae2017-08-24 16:30:58 +0530675 char path[PATH_MAX];
676 const char *sysfs = sysfs__mountpoint();
677
678 if (!sysfs)
679 return 0;
680
Ganapatrao Kulkarni14b22ae2017-08-24 16:30:58 +0530681 /* Look for cpu sysfs (specific to arm) */
682 scnprintf(path, PATH_MAX, "%s/bus/event_source/devices/%s/cpus",
683 sysfs, name);
Andi Kleend9664582019-11-20 16:15:11 -0800684 return file_available(path);
Ganapatrao Kulkarni14b22ae2017-08-24 16:30:58 +0530685}
686
Ganapatrao Kulkarni54e32dc2017-10-17 00:02:18 +0530687static char *perf_pmu__getcpuid(struct perf_pmu *pmu)
Andi Kleend77ade92017-08-31 12:40:30 -0700688{
689 char *cpuid;
690 static bool printed;
691
692 cpuid = getenv("PERF_CPUID");
693 if (cpuid)
694 cpuid = strdup(cpuid);
695 if (!cpuid)
Ganapatrao Kulkarni54e32dc2017-10-17 00:02:18 +0530696 cpuid = get_cpuid_str(pmu);
Andi Kleend77ade92017-08-31 12:40:30 -0700697 if (!cpuid)
698 return NULL;
699
700 if (!printed) {
701 pr_debug("Using CPUID %s\n", cpuid);
702 printed = true;
703 }
704 return cpuid;
705}
706
Ganapatrao Kulkarni54e32dc2017-10-17 00:02:18 +0530707struct pmu_events_map *perf_pmu__find_map(struct perf_pmu *pmu)
Andi Kleend77ade92017-08-31 12:40:30 -0700708{
709 struct pmu_events_map *map;
Ganapatrao Kulkarni54e32dc2017-10-17 00:02:18 +0530710 char *cpuid = perf_pmu__getcpuid(pmu);
Andi Kleend77ade92017-08-31 12:40:30 -0700711 int i;
712
Ganapatrao Kulkarnide3d0f12017-10-17 00:02:22 +0530713 /* on some platforms which uses cpus map, cpuid can be NULL for
714 * PMUs other than CORE PMUs.
715 */
716 if (!cpuid)
717 return NULL;
718
Andi Kleend77ade92017-08-31 12:40:30 -0700719 i = 0;
720 for (;;) {
721 map = &pmu_events_map[i++];
722 if (!map->table) {
723 map = NULL;
724 break;
725 }
726
Thomas Richter4cb7d3e2018-02-13 16:14:18 +0100727 if (!strcmp_cpuid_str(map->cpuid, cpuid))
Andi Kleend77ade92017-08-31 12:40:30 -0700728 break;
729 }
730 free(cpuid);
731 return map;
732}
733
John Garrye126bef2021-04-07 18:32:47 +0800734struct pmu_events_map *__weak pmu_events_map__find(void)
735{
736 return perf_pmu__find_map(NULL);
737}
738
John Garry5b9a5002020-03-17 19:02:18 +0800739bool pmu_uncore_alias_match(const char *pmu_name, const char *name)
John Garry730670b12019-06-28 22:35:49 +0800740{
741 char *tmp = NULL, *tok, *str;
742 bool res;
743
744 str = strdup(pmu_name);
745 if (!str)
746 return false;
747
748 /*
749 * uncore alias may be from different PMU with common prefix
750 */
751 tok = strtok_r(str, ",", &tmp);
752 if (strncmp(pmu_name, tok, strlen(tok))) {
753 res = false;
754 goto out;
755 }
756
757 /*
758 * Match more complex aliases where the alias name is a comma-delimited
759 * list of tokens, orderly contained in the matching PMU name.
760 *
761 * Example: For alias "socket,pmuname" and PMU "socketX_pmunameY", we
762 * match "socket" in "socketX_pmunameY" and then "pmuname" in
763 * "pmunameY".
764 */
765 for (; tok; name += strlen(tok), tok = strtok_r(NULL, ",", &tmp)) {
766 name = strstr(name, tok);
767 if (!name) {
768 res = false;
769 goto out;
770 }
771 }
772
773 res = true;
774out:
775 free(str);
776 return res;
777}
778
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700779/*
780 * From the pmu_events_map, find the table of PMU events that corresponds
781 * to the current running CPU. Then, add all PMU events from that table
782 * as aliases.
783 */
John Garrye45ad702020-03-17 19:02:15 +0800784void pmu_add_cpu_aliases_map(struct list_head *head, struct perf_pmu *pmu,
785 struct pmu_events_map *map)
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700786{
787 int i;
Ganapatrao Kulkarni54e32dc2017-10-17 00:02:18 +0530788 const char *name = pmu->name;
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700789 /*
790 * Found a matching PMU events table. Create aliases
791 */
792 i = 0;
793 while (1) {
John Garry599ee182019-06-14 22:07:59 +0800794 const char *cpu_name = is_arm_pmu_core(name) ? name : "cpu";
795 struct pmu_event *pe = &map->table[i++];
796 const char *pname = pe->pmu ? pe->pmu : cpu_name;
Andi Kleenfedb2b52017-01-27 18:03:37 -0800797
Andi Kleenb18f3e32017-08-31 12:40:31 -0700798 if (!pe->name) {
799 if (pe->metric_group || pe->metric_name)
800 continue;
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700801 break;
Andi Kleenb18f3e32017-08-31 12:40:31 -0700802 }
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700803
John Garry599ee182019-06-14 22:07:59 +0800804 if (pmu_is_uncore(name) &&
John Garry730670b12019-06-28 22:35:49 +0800805 pmu_uncore_alias_match(pname, name))
John Garry599ee182019-06-14 22:07:59 +0800806 goto new_alias;
Kan Liange94d6b72019-03-15 11:00:14 -0700807
John Garry599ee182019-06-14 22:07:59 +0800808 if (strcmp(pname, name))
809 continue;
Andi Kleenfedb2b52017-01-27 18:03:37 -0800810
Kan Liange94d6b72019-03-15 11:00:14 -0700811new_alias:
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700812 /* need type casts to override 'const' */
813 __perf_pmu__new_alias(head, NULL, (char *)pe->name,
Sukadev Bhattiproluc8d68282016-09-15 15:24:48 -0700814 (char *)pe->desc, (char *)pe->event,
Jin Yaoeab35952021-04-27 15:01:16 +0800815 pe);
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700816 }
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700817}
818
John Garrye45ad702020-03-17 19:02:15 +0800819static void pmu_add_cpu_aliases(struct list_head *head, struct perf_pmu *pmu)
820{
821 struct pmu_events_map *map;
822
823 map = perf_pmu__find_map(pmu);
824 if (!map)
825 return;
826
827 pmu_add_cpu_aliases_map(head, pmu, map);
828}
829
John Garry4513c712020-12-04 19:10:10 +0800830void pmu_for_each_sys_event(pmu_sys_event_iter_fn fn, void *data)
831{
832 int i = 0;
833
834 while (1) {
835 struct pmu_sys_events *event_table;
836 int j = 0;
837
838 event_table = &pmu_sys_event_tables[i++];
839
840 if (!event_table->table)
841 break;
842
843 while (1) {
844 struct pmu_event *pe = &event_table->table[j++];
845 int ret;
846
847 if (!pe->name && !pe->metric_group && !pe->metric_name)
848 break;
849
850 ret = fn(pe, data);
851 if (ret)
852 break;
853 }
854 }
855}
856
857struct pmu_sys_event_iter_data {
858 struct list_head *head;
859 struct perf_pmu *pmu;
860};
861
862static int pmu_add_sys_aliases_iter_fn(struct pmu_event *pe, void *data)
863{
864 struct pmu_sys_event_iter_data *idata = data;
865 struct perf_pmu *pmu = idata->pmu;
866
867 if (!pe->name) {
868 if (pe->metric_group || pe->metric_name)
869 return 0;
870 return -EINVAL;
871 }
872
873 if (!pe->compat || !pe->pmu)
874 return 0;
875
876 if (!strcmp(pmu->id, pe->compat) &&
877 pmu_uncore_alias_match(pe->pmu, pmu->name)) {
878 __perf_pmu__new_alias(idata->head, NULL,
879 (char *)pe->name,
880 (char *)pe->desc,
881 (char *)pe->event,
Jin Yaoeab35952021-04-27 15:01:16 +0800882 pe);
John Garry4513c712020-12-04 19:10:10 +0800883 }
884
885 return 0;
886}
887
888static void pmu_add_sys_aliases(struct list_head *head, struct perf_pmu *pmu)
889{
890 struct pmu_sys_event_iter_data idata = {
891 .head = head,
892 .pmu = pmu,
893 };
894
895 if (!pmu->id)
896 return;
897
898 pmu_for_each_sys_event(pmu_add_sys_aliases_iter_fn, &idata);
899}
900
Sukadev Bhattiproluc5de47f2015-06-10 00:25:07 -0700901struct perf_event_attr * __weak
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300902perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
903{
904 return NULL;
905}
906
Jiri Olsa90a86bd2019-03-05 16:25:32 +0100907static int pmu_max_precise(const char *name)
908{
909 char path[PATH_MAX];
910 int max_precise = -1;
911
912 scnprintf(path, PATH_MAX,
913 "bus/event_source/devices/%s/caps/max_precise",
914 name);
915
916 sysfs__read_int(path, &max_precise);
917 return max_precise;
918}
919
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300920static struct perf_pmu *pmu_lookup(const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +0100921{
922 struct perf_pmu *pmu;
923 LIST_HEAD(format);
Zheng Yana6146d52012-06-15 14:31:41 +0800924 LIST_HEAD(aliases);
Jiri Olsacd82a322012-03-15 20:09:17 +0100925 __u32 type;
926
927 /*
928 * The pmu data we store & need consists of the pmu
929 * type value and format definitions. Load both right
930 * now.
931 */
932 if (pmu_format(name, &format))
933 return NULL;
934
Andi Kleen15b22ed2017-01-27 18:03:38 -0800935 /*
936 * Check the type first to avoid unnecessary work.
937 */
938 if (pmu_type(name, &type))
939 return NULL;
940
Jiri Olsa3fded962012-10-10 14:53:16 +0200941 if (pmu_aliases(name, &aliases))
942 return NULL;
943
Jiri Olsacd82a322012-03-15 20:09:17 +0100944 pmu = zalloc(sizeof(*pmu));
945 if (!pmu)
946 return NULL;
947
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800948 pmu->cpus = pmu_cpumask(name);
Ganapatrao Kulkarni54e32dc2017-10-17 00:02:18 +0530949 pmu->name = strdup(name);
950 pmu->type = type;
Mark Rutland66ec11912017-10-06 19:38:22 +0100951 pmu->is_uncore = pmu_is_uncore(name);
John Garry51d54842020-12-04 19:10:09 +0800952 if (pmu->is_uncore)
953 pmu->id = pmu_id(name);
Jiri Olsa90a86bd2019-03-05 16:25:32 +0100954 pmu->max_precise = pmu_max_precise(name);
Ganapatrao Kulkarni54e32dc2017-10-17 00:02:18 +0530955 pmu_add_cpu_aliases(&aliases, pmu);
John Garry4513c712020-12-04 19:10:10 +0800956 pmu_add_sys_aliases(&aliases, pmu);
Mark Rutland66ec11912017-10-06 19:38:22 +0100957
Jiri Olsacd82a322012-03-15 20:09:17 +0100958 INIT_LIST_HEAD(&pmu->format);
Zheng Yana6146d52012-06-15 14:31:41 +0800959 INIT_LIST_HEAD(&pmu->aliases);
Kan Liang9fbc61f2020-03-19 13:25:01 -0700960 INIT_LIST_HEAD(&pmu->caps);
Jiri Olsacd82a322012-03-15 20:09:17 +0100961 list_splice(&format, &pmu->format);
Zheng Yana6146d52012-06-15 14:31:41 +0800962 list_splice(&aliases, &pmu->aliases);
Robert Richter9bc8f9f2012-06-14 22:38:37 +0200963 list_add_tail(&pmu->list, &pmus);
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300964
965 pmu->default_config = perf_pmu__get_default_config(pmu);
966
Jiri Olsacd82a322012-03-15 20:09:17 +0100967 return pmu;
968}
969
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300970static struct perf_pmu *pmu_find(const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +0100971{
972 struct perf_pmu *pmu;
973
974 list_for_each_entry(pmu, &pmus, list)
975 if (!strcmp(pmu->name, name))
976 return pmu;
977
978 return NULL;
979}
980
Stephane Eranian3a50dc72020-04-29 16:14:42 -0700981struct perf_pmu *perf_pmu__find_by_type(unsigned int type)
982{
983 struct perf_pmu *pmu;
984
985 list_for_each_entry(pmu, &pmus, list)
986 if (pmu->type == type)
987 return pmu;
988
989 return NULL;
990}
991
Robert Richter50a96672012-08-16 21:10:24 +0200992struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
993{
994 /*
995 * pmu iterator: If pmu is NULL, we start at the begin,
996 * otherwise return the next pmu. Returns NULL on end.
997 */
998 if (!pmu) {
999 pmu_read_sysfs();
1000 pmu = list_prepare_entry(pmu, &pmus, list);
1001 }
1002 list_for_each_entry_continue(pmu, &pmus, list)
1003 return pmu;
1004 return NULL;
1005}
1006
Arnaldo Carvalho de Meloe76026b2020-04-29 15:50:10 -03001007struct perf_pmu *evsel__find_pmu(struct evsel *evsel)
Adrian Huntere12ee9f2020-04-01 13:16:09 +03001008{
1009 struct perf_pmu *pmu = NULL;
1010
1011 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1012 if (pmu->type == evsel->core.attr.type)
1013 break;
1014 }
1015
1016 return pmu;
1017}
1018
Arnaldo Carvalho de Melo39453ed2020-04-29 15:51:38 -03001019bool evsel__is_aux_event(struct evsel *evsel)
Adrian Huntere12ee9f2020-04-01 13:16:09 +03001020{
Arnaldo Carvalho de Meloe76026b2020-04-29 15:50:10 -03001021 struct perf_pmu *pmu = evsel__find_pmu(evsel);
Adrian Huntere12ee9f2020-04-01 13:16:09 +03001022
1023 return pmu && pmu->auxtrace;
1024}
1025
Adrian Hunterb6b96fb2013-07-04 16:20:25 +03001026struct perf_pmu *perf_pmu__find(const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +01001027{
1028 struct perf_pmu *pmu;
1029
1030 /*
1031 * Once PMU is loaded it stays in the list,
1032 * so we keep us from multiple reading/parsing
1033 * the pmu format definitions.
1034 */
1035 pmu = pmu_find(name);
1036 if (pmu)
1037 return pmu;
1038
1039 return pmu_lookup(name);
1040}
1041
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -03001042static struct perf_pmu_format *
Adrian Hunter09ff6072015-07-17 19:33:49 +03001043pmu_find_format(struct list_head *formats, const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +01001044{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -03001045 struct perf_pmu_format *format;
Jiri Olsacd82a322012-03-15 20:09:17 +01001046
1047 list_for_each_entry(format, formats, list)
1048 if (!strcmp(format->name, name))
1049 return format;
1050
1051 return NULL;
1052}
1053
Adrian Hunter09ff6072015-07-17 19:33:49 +03001054__u64 perf_pmu__format_bits(struct list_head *formats, const char *name)
1055{
1056 struct perf_pmu_format *format = pmu_find_format(formats, name);
1057 __u64 bits = 0;
1058 int fbit;
1059
1060 if (!format)
1061 return 0;
1062
1063 for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
1064 bits |= 1ULL << fbit;
1065
1066 return bits;
1067}
1068
Adrian Huntera1ac7de2019-11-15 14:42:22 +02001069int perf_pmu__format_type(struct list_head *formats, const char *name)
1070{
1071 struct perf_pmu_format *format = pmu_find_format(formats, name);
1072
1073 if (!format)
1074 return -1;
1075
1076 return format->value;
1077}
1078
Jiri Olsacd82a322012-03-15 20:09:17 +01001079/*
Adrian Hunterdc0a6202014-07-31 09:00:49 +03001080 * Sets value based on the format definition (format parameter)
Ingo Molnar4d39c892021-03-23 17:09:15 +01001081 * and unformatted value (value parameter).
Jiri Olsacd82a322012-03-15 20:09:17 +01001082 */
Adrian Hunterdc0a6202014-07-31 09:00:49 +03001083static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
1084 bool zero)
Jiri Olsacd82a322012-03-15 20:09:17 +01001085{
1086 unsigned long fbit, vbit;
Jiri Olsacd82a322012-03-15 20:09:17 +01001087
1088 for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
1089
1090 if (!test_bit(fbit, format))
1091 continue;
1092
Adrian Hunterdc0a6202014-07-31 09:00:49 +03001093 if (value & (1llu << vbit++))
1094 *v |= (1llu << fbit);
1095 else if (zero)
1096 *v &= ~(1llu << fbit);
Jiri Olsacd82a322012-03-15 20:09:17 +01001097 }
Jiri Olsacd82a322012-03-15 20:09:17 +01001098}
1099
Adrian Hunter0efe6b62015-07-17 19:33:50 +03001100static __u64 pmu_format_max_value(const unsigned long *format)
1101{
Jiri Olsa1b9caa12018-10-03 09:20:46 +02001102 int w;
Adrian Hunter0efe6b62015-07-17 19:33:50 +03001103
Jiri Olsa1b9caa12018-10-03 09:20:46 +02001104 w = bitmap_weight(format, PERF_PMU_FORMAT_BITS);
1105 if (!w)
1106 return 0;
1107 if (w < 64)
1108 return (1ULL << w) - 1;
1109 return -1;
Adrian Hunter0efe6b62015-07-17 19:33:50 +03001110}
1111
Jiri Olsacd82a322012-03-15 20:09:17 +01001112/*
Cody P Schafer688d4df2015-01-07 17:13:50 -08001113 * Term is a string term, and might be a param-term. Try to look up it's value
1114 * in the remaining terms.
1115 * - We have a term like "base-or-format-term=param-term",
1116 * - We need to find the value supplied for "param-term" (with param-term named
1117 * in a config string) later on in the term list.
1118 */
1119static int pmu_resolve_param_term(struct parse_events_term *term,
1120 struct list_head *head_terms,
1121 __u64 *value)
1122{
1123 struct parse_events_term *t;
1124
1125 list_for_each_entry(t, head_terms, list) {
Ian Rogers2a3d2522020-03-25 09:40:22 -07001126 if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM &&
1127 t->config && !strcmp(t->config, term->config)) {
1128 t->used = true;
1129 *value = t->val.num;
1130 return 0;
Cody P Schafer688d4df2015-01-07 17:13:50 -08001131 }
1132 }
1133
Namhyung Kimbb963e12017-02-17 17:17:38 +09001134 if (verbose > 0)
Cody P Schafer688d4df2015-01-07 17:13:50 -08001135 printf("Required parameter '%s' not specified\n", term->config);
1136
1137 return -1;
1138}
1139
He Kuangffeb8832015-09-28 03:52:14 +00001140static char *pmu_formats_string(struct list_head *formats)
Jiri Olsae64b0202015-04-22 21:10:21 +02001141{
1142 struct perf_pmu_format *format;
Masami Hiramatsu11db4e22016-05-10 14:47:44 +09001143 char *str = NULL;
1144 struct strbuf buf = STRBUF_INIT;
Jiri Olsae64b0202015-04-22 21:10:21 +02001145 unsigned i = 0;
1146
He Kuangffeb8832015-09-28 03:52:14 +00001147 if (!formats)
Jiri Olsae64b0202015-04-22 21:10:21 +02001148 return NULL;
1149
1150 /* sysfs exported terms */
He Kuangffeb8832015-09-28 03:52:14 +00001151 list_for_each_entry(format, formats, list)
Masami Hiramatsu11db4e22016-05-10 14:47:44 +09001152 if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
1153 goto error;
Jiri Olsae64b0202015-04-22 21:10:21 +02001154
He Kuangffeb8832015-09-28 03:52:14 +00001155 str = strbuf_detach(&buf, NULL);
Masami Hiramatsu11db4e22016-05-10 14:47:44 +09001156error:
He Kuangffeb8832015-09-28 03:52:14 +00001157 strbuf_release(&buf);
Jiri Olsae64b0202015-04-22 21:10:21 +02001158
Jiri Olsae64b0202015-04-22 21:10:21 +02001159 return str;
Jiri Olsae64b0202015-04-22 21:10:21 +02001160}
1161
Cody P Schafer688d4df2015-01-07 17:13:50 -08001162/*
Jiri Olsacd82a322012-03-15 20:09:17 +01001163 * Setup one of config[12] attr members based on the
Cody P Schafer88aca8d2014-01-08 08:43:51 -08001164 * user input data - term parameter.
Jiri Olsacd82a322012-03-15 20:09:17 +01001165 */
Ian Rogers4ac22b42020-05-13 15:06:35 -07001166static int pmu_config_term(const char *pmu_name,
1167 struct list_head *formats,
Jiri Olsacd82a322012-03-15 20:09:17 +01001168 struct perf_event_attr *attr,
Adrian Hunterdc0a6202014-07-31 09:00:49 +03001169 struct parse_events_term *term,
Cody P Schafer688d4df2015-01-07 17:13:50 -08001170 struct list_head *head_terms,
Jiri Olsae64b0202015-04-22 21:10:21 +02001171 bool zero, struct parse_events_error *err)
Jiri Olsacd82a322012-03-15 20:09:17 +01001172{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -03001173 struct perf_pmu_format *format;
Jiri Olsacd82a322012-03-15 20:09:17 +01001174 __u64 *vp;
Adrian Hunter0efe6b62015-07-17 19:33:50 +03001175 __u64 val, max_val;
Jiri Olsacd82a322012-03-15 20:09:17 +01001176
1177 /*
Cody P Schafer688d4df2015-01-07 17:13:50 -08001178 * If this is a parameter we've already used for parameterized-eval,
1179 * skip it in normal eval.
1180 */
1181 if (term->used)
1182 return 0;
1183
1184 /*
Jiri Olsacd82a322012-03-15 20:09:17 +01001185 * Hardcoded terms should be already in, so nothing
1186 * to be done for them.
1187 */
1188 if (parse_events__is_hardcoded_term(term))
1189 return 0;
1190
Jiri Olsacd82a322012-03-15 20:09:17 +01001191 format = pmu_find_format(formats, term->config);
Cody P Schafer688d4df2015-01-07 17:13:50 -08001192 if (!format) {
Ian Rogers4ac22b42020-05-13 15:06:35 -07001193 char *pmu_term = pmu_formats_string(formats);
1194 char *unknown_term;
1195 char *help_msg;
He Kuangffeb8832015-09-28 03:52:14 +00001196
Ian Rogers4ac22b42020-05-13 15:06:35 -07001197 if (asprintf(&unknown_term,
1198 "unknown term '%s' for pmu '%s'",
1199 term->config, pmu_name) < 0)
1200 unknown_term = NULL;
1201 help_msg = parse_events_formats_error_string(pmu_term);
1202 if (err) {
Ian Rogers448d7322019-10-30 15:34:39 -07001203 parse_events__handle_error(err, term->err_term,
Ian Rogers4ac22b42020-05-13 15:06:35 -07001204 unknown_term,
1205 help_msg);
1206 } else {
1207 pr_debug("%s (%s)\n", unknown_term, help_msg);
1208 free(unknown_term);
Jiri Olsae64b0202015-04-22 21:10:21 +02001209 }
Ian Rogers4ac22b42020-05-13 15:06:35 -07001210 free(pmu_term);
Jiri Olsacd82a322012-03-15 20:09:17 +01001211 return -EINVAL;
Cody P Schafer688d4df2015-01-07 17:13:50 -08001212 }
Jiri Olsacd82a322012-03-15 20:09:17 +01001213
1214 switch (format->value) {
1215 case PERF_PMU_FORMAT_VALUE_CONFIG:
1216 vp = &attr->config;
1217 break;
1218 case PERF_PMU_FORMAT_VALUE_CONFIG1:
1219 vp = &attr->config1;
1220 break;
1221 case PERF_PMU_FORMAT_VALUE_CONFIG2:
1222 vp = &attr->config2;
1223 break;
1224 default:
1225 return -EINVAL;
1226 }
1227
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001228 /*
Cody P Schafer688d4df2015-01-07 17:13:50 -08001229 * Either directly use a numeric term, or try to translate string terms
1230 * using event parameters.
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001231 */
Jiri Olsa99e71382017-02-17 15:00:56 +01001232 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1233 if (term->no_value &&
1234 bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) {
1235 if (err) {
Ian Rogers448d7322019-10-30 15:34:39 -07001236 parse_events__handle_error(err, term->err_val,
1237 strdup("no value assigned for term"),
1238 NULL);
Jiri Olsa99e71382017-02-17 15:00:56 +01001239 }
1240 return -EINVAL;
1241 }
1242
Cody P Schafer688d4df2015-01-07 17:13:50 -08001243 val = term->val.num;
Jiri Olsa99e71382017-02-17 15:00:56 +01001244 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
Cody P Schafer688d4df2015-01-07 17:13:50 -08001245 if (strcmp(term->val.str, "?")) {
Namhyung Kimbb963e12017-02-17 17:17:38 +09001246 if (verbose > 0) {
Cody P Schafer688d4df2015-01-07 17:13:50 -08001247 pr_info("Invalid sysfs entry %s=%s\n",
1248 term->config, term->val.str);
Jiri Olsae64b0202015-04-22 21:10:21 +02001249 }
1250 if (err) {
Ian Rogers448d7322019-10-30 15:34:39 -07001251 parse_events__handle_error(err, term->err_val,
1252 strdup("expected numeric value"),
1253 NULL);
Jiri Olsae64b0202015-04-22 21:10:21 +02001254 }
Cody P Schafer688d4df2015-01-07 17:13:50 -08001255 return -EINVAL;
1256 }
1257
1258 if (pmu_resolve_param_term(term, head_terms, &val))
1259 return -EINVAL;
1260 } else
1261 return -EINVAL;
1262
Adrian Hunter0efe6b62015-07-17 19:33:50 +03001263 max_val = pmu_format_max_value(format->bits);
1264 if (val > max_val) {
1265 if (err) {
Ian Rogers448d7322019-10-30 15:34:39 -07001266 char *err_str;
1267
1268 parse_events__handle_error(err, term->err_val,
1269 asprintf(&err_str,
1270 "value too big for format, maximum is %llu",
1271 (unsigned long long)max_val) < 0
1272 ? strdup("value too big for format")
1273 : err_str,
1274 NULL);
Adrian Hunter0efe6b62015-07-17 19:33:50 +03001275 return -EINVAL;
1276 }
1277 /*
1278 * Assume we don't care if !err, in which case the value will be
1279 * silently truncated.
1280 */
1281 }
1282
Cody P Schafer688d4df2015-01-07 17:13:50 -08001283 pmu_format_value(format->bits, val, vp, zero);
Jiri Olsacd82a322012-03-15 20:09:17 +01001284 return 0;
1285}
1286
Ian Rogers4ac22b42020-05-13 15:06:35 -07001287int perf_pmu__config_terms(const char *pmu_name, struct list_head *formats,
Jiri Olsacff7f952012-11-10 01:46:50 +01001288 struct perf_event_attr *attr,
Adrian Hunterdc0a6202014-07-31 09:00:49 +03001289 struct list_head *head_terms,
Jiri Olsae64b0202015-04-22 21:10:21 +02001290 bool zero, struct parse_events_error *err)
Jiri Olsacd82a322012-03-15 20:09:17 +01001291{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001292 struct parse_events_term *term;
Jiri Olsacd82a322012-03-15 20:09:17 +01001293
Cody P Schafer688d4df2015-01-07 17:13:50 -08001294 list_for_each_entry(term, head_terms, list) {
Ian Rogers4ac22b42020-05-13 15:06:35 -07001295 if (pmu_config_term(pmu_name, formats, attr, term, head_terms,
Jiri Olsae64b0202015-04-22 21:10:21 +02001296 zero, err))
Jiri Olsacd82a322012-03-15 20:09:17 +01001297 return -EINVAL;
Cody P Schafer688d4df2015-01-07 17:13:50 -08001298 }
Jiri Olsacd82a322012-03-15 20:09:17 +01001299
1300 return 0;
1301}
1302
1303/*
1304 * Configures event's 'attr' parameter based on the:
1305 * 1) users input - specified in terms parameter
1306 * 2) pmu format definitions - specified by pmu parameter
1307 */
1308int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
Jiri Olsae64b0202015-04-22 21:10:21 +02001309 struct list_head *head_terms,
1310 struct parse_events_error *err)
Jiri Olsacd82a322012-03-15 20:09:17 +01001311{
Adrian Hunterdc0a6202014-07-31 09:00:49 +03001312 bool zero = !!pmu->default_config;
1313
Jiri Olsacd82a322012-03-15 20:09:17 +01001314 attr->type = pmu->type;
Ian Rogers4ac22b42020-05-13 15:06:35 -07001315 return perf_pmu__config_terms(pmu->name, &pmu->format, attr,
1316 head_terms, zero, err);
Jiri Olsacd82a322012-03-15 20:09:17 +01001317}
1318
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -03001319static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
1320 struct parse_events_term *term)
Zheng Yana6146d52012-06-15 14:31:41 +08001321{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -03001322 struct perf_pmu_alias *alias;
Zheng Yana6146d52012-06-15 14:31:41 +08001323 char *name;
1324
1325 if (parse_events__is_hardcoded_term(term))
1326 return NULL;
1327
1328 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1329 if (term->val.num != 1)
1330 return NULL;
1331 if (pmu_find_format(&pmu->format, term->config))
1332 return NULL;
1333 name = term->config;
1334 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1335 if (strcasecmp(term->config, "event"))
1336 return NULL;
1337 name = term->val.str;
1338 } else {
1339 return NULL;
1340 }
1341
1342 list_for_each_entry(alias, &pmu->aliases, list) {
1343 if (!strcasecmp(alias->name, name))
1344 return alias;
1345 }
1346 return NULL;
1347}
1348
Stephane Eranian410136f2013-11-12 17:58:49 +01001349
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001350static int check_info_data(struct perf_pmu_alias *alias,
1351 struct perf_pmu_info *info)
Stephane Eranian410136f2013-11-12 17:58:49 +01001352{
1353 /*
1354 * Only one term in event definition can
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001355 * define unit, scale and snapshot, fail
1356 * if there's more than one.
Stephane Eranian410136f2013-11-12 17:58:49 +01001357 */
Arnaldo Carvalho de Melob30a7d12017-02-15 10:06:20 -03001358 if ((info->unit && alias->unit[0]) ||
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001359 (info->scale && alias->scale) ||
1360 (info->snapshot && alias->snapshot))
Stephane Eranian410136f2013-11-12 17:58:49 +01001361 return -EINVAL;
1362
Arnaldo Carvalho de Melob30a7d12017-02-15 10:06:20 -03001363 if (alias->unit[0])
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001364 info->unit = alias->unit;
Stephane Eranian410136f2013-11-12 17:58:49 +01001365
1366 if (alias->scale)
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001367 info->scale = alias->scale;
1368
1369 if (alias->snapshot)
1370 info->snapshot = alias->snapshot;
Stephane Eranian410136f2013-11-12 17:58:49 +01001371
1372 return 0;
1373}
1374
Zheng Yana6146d52012-06-15 14:31:41 +08001375/*
1376 * Find alias in the terms list and replace it with the terms
1377 * defined for the alias
1378 */
Stephane Eranian410136f2013-11-12 17:58:49 +01001379int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
Matt Fleming46441bd2014-09-24 15:04:06 +01001380 struct perf_pmu_info *info)
Zheng Yana6146d52012-06-15 14:31:41 +08001381{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001382 struct parse_events_term *term, *h;
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -03001383 struct perf_pmu_alias *alias;
Zheng Yana6146d52012-06-15 14:31:41 +08001384 int ret;
1385
Matt Fleming044330c2014-11-21 10:31:12 +01001386 info->per_pkg = false;
1387
Stephane Eranian8a398892014-01-17 16:34:05 +01001388 /*
1389 * Mark unit and scale as not set
1390 * (different from default values, see below)
1391 */
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001392 info->unit = NULL;
1393 info->scale = 0.0;
1394 info->snapshot = false;
Andi Kleen37932c12017-03-20 13:17:08 -07001395 info->metric_expr = NULL;
Andi Kleen96284812017-03-20 13:17:10 -07001396 info->metric_name = NULL;
Stephane Eranian410136f2013-11-12 17:58:49 +01001397
Zheng Yana6146d52012-06-15 14:31:41 +08001398 list_for_each_entry_safe(term, h, head_terms, list) {
1399 alias = pmu_find_alias(pmu, term);
1400 if (!alias)
1401 continue;
1402 ret = pmu_alias_terms(alias, &term->list);
1403 if (ret)
1404 return ret;
Stephane Eranian410136f2013-11-12 17:58:49 +01001405
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001406 ret = check_info_data(alias, info);
Stephane Eranian410136f2013-11-12 17:58:49 +01001407 if (ret)
1408 return ret;
1409
Matt Fleming044330c2014-11-21 10:31:12 +01001410 if (alias->per_pkg)
1411 info->per_pkg = true;
Andi Kleen37932c12017-03-20 13:17:08 -07001412 info->metric_expr = alias->metric_expr;
Andi Kleen96284812017-03-20 13:17:10 -07001413 info->metric_name = alias->metric_name;
Matt Fleming044330c2014-11-21 10:31:12 +01001414
Arnaldo Carvalho de Meloe56fbc92019-07-04 12:13:46 -03001415 list_del_init(&term->list);
Ian Rogers1dc92552019-10-30 15:34:47 -07001416 parse_events_term__delete(term);
Zheng Yana6146d52012-06-15 14:31:41 +08001417 }
Stephane Eranian8a398892014-01-17 16:34:05 +01001418
1419 /*
Ingo Molnar4d39c892021-03-23 17:09:15 +01001420 * if no unit or scale found in aliases, then
Stephane Eranian8a398892014-01-17 16:34:05 +01001421 * set defaults as for evsel
1422 * unit cannot left to NULL
1423 */
Matt Fleming46441bd2014-09-24 15:04:06 +01001424 if (info->unit == NULL)
1425 info->unit = "";
Stephane Eranian8a398892014-01-17 16:34:05 +01001426
Matt Fleming46441bd2014-09-24 15:04:06 +01001427 if (info->scale == 0.0)
1428 info->scale = 1.0;
Stephane Eranian8a398892014-01-17 16:34:05 +01001429
Zheng Yana6146d52012-06-15 14:31:41 +08001430 return 0;
1431}
1432
Jiri Olsacd82a322012-03-15 20:09:17 +01001433int perf_pmu__new_format(struct list_head *list, char *name,
1434 int config, unsigned long *bits)
1435{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -03001436 struct perf_pmu_format *format;
Jiri Olsacd82a322012-03-15 20:09:17 +01001437
1438 format = zalloc(sizeof(*format));
1439 if (!format)
1440 return -ENOMEM;
1441
1442 format->name = strdup(name);
1443 format->value = config;
1444 memcpy(format->bits, bits, sizeof(format->bits));
1445
1446 list_add_tail(&format->list, list);
1447 return 0;
1448}
1449
1450void perf_pmu__set_format(unsigned long *bits, long from, long to)
1451{
1452 long b;
1453
1454 if (!to)
1455 to = from;
1456
Sukadev Bhattiprolu15268132013-01-17 09:11:30 -08001457 memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
Jiri Olsacd82a322012-03-15 20:09:17 +01001458 for (b = from; b <= to; b++)
1459 set_bit(b, bits);
1460}
Andi Kleendc098b32013-04-20 11:02:29 -07001461
Namhyung Kimd26383d2020-09-15 12:18:19 +09001462void perf_pmu__del_formats(struct list_head *formats)
1463{
1464 struct perf_pmu_format *fmt, *tmp;
1465
1466 list_for_each_entry_safe(fmt, tmp, formats, list) {
1467 list_del(&fmt->list);
1468 free(fmt->name);
1469 free(fmt);
1470 }
1471}
1472
Cody P Schaferaaea3612015-01-07 17:13:51 -08001473static int sub_non_neg(int a, int b)
1474{
1475 if (b > a)
1476 return 0;
1477 return a - b;
1478}
1479
Andi Kleendc098b32013-04-20 11:02:29 -07001480static char *format_alias(char *buf, int len, struct perf_pmu *pmu,
1481 struct perf_pmu_alias *alias)
1482{
Cody P Schaferaaea3612015-01-07 17:13:51 -08001483 struct parse_events_term *term;
1484 int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name);
1485
1486 list_for_each_entry(term, &alias->terms, list) {
1487 if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
1488 used += snprintf(buf + used, sub_non_neg(len, used),
1489 ",%s=%s", term->config,
1490 term->val.str);
1491 }
1492
1493 if (sub_non_neg(len, used) > 0) {
1494 buf[used] = '/';
1495 used++;
1496 }
1497 if (sub_non_neg(len, used) > 0) {
1498 buf[used] = '\0';
1499 used++;
1500 } else
1501 buf[len - 1] = '\0';
1502
Andi Kleendc098b32013-04-20 11:02:29 -07001503 return buf;
1504}
1505
1506static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu,
1507 struct perf_pmu_alias *alias)
1508{
1509 snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name);
1510 return buf;
1511}
1512
Andi Kleendd5f1032016-09-15 15:24:50 -07001513struct sevent {
Andi Kleen08e60ed2016-09-15 15:24:43 -07001514 char *name;
1515 char *desc;
Andi Kleendd5f1032016-09-15 15:24:50 -07001516 char *topic;
Andi Kleenf2361022017-01-27 18:03:40 -08001517 char *str;
1518 char *pmu;
Andi Kleen7f372a62017-03-20 13:17:09 -07001519 char *metric_expr;
Andi Kleen96284812017-03-20 13:17:10 -07001520 char *metric_name;
John Garryce0dc7d2020-06-17 17:01:54 +08001521 int is_cpu;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001522};
1523
Andi Kleendd5f1032016-09-15 15:24:50 -07001524static int cmp_sevent(const void *a, const void *b)
Andi Kleendc098b32013-04-20 11:02:29 -07001525{
Andi Kleendd5f1032016-09-15 15:24:50 -07001526 const struct sevent *as = a;
1527 const struct sevent *bs = b;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001528
1529 /* Put extra events last */
1530 if (!!as->desc != !!bs->desc)
1531 return !!as->desc - !!bs->desc;
Andi Kleendd5f1032016-09-15 15:24:50 -07001532 if (as->topic && bs->topic) {
1533 int n = strcmp(as->topic, bs->topic);
1534
1535 if (n)
1536 return n;
1537 }
John Garryce0dc7d2020-06-17 17:01:54 +08001538
1539 /* Order CPU core events to be first */
1540 if (as->is_cpu != bs->is_cpu)
1541 return bs->is_cpu - as->is_cpu;
1542
Andi Kleen08e60ed2016-09-15 15:24:43 -07001543 return strcmp(as->name, bs->name);
1544}
1545
1546static void wordwrap(char *s, int start, int max, int corr)
1547{
1548 int column = start;
1549 int n;
1550
1551 while (*s) {
1552 int wlen = strcspn(s, " \t");
1553
1554 if (column + wlen >= max && column > start) {
1555 printf("\n%*s", start, "");
1556 column = start + corr;
1557 }
1558 n = printf("%s%.*s", column > start ? " " : "", wlen, s);
1559 if (n <= 0)
1560 break;
1561 s += wlen;
1562 column += n;
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03001563 s = skip_spaces(s);
Andi Kleen08e60ed2016-09-15 15:24:43 -07001564 }
Andi Kleendc098b32013-04-20 11:02:29 -07001565}
1566
John Garryd504fae2020-03-17 19:02:17 +08001567bool is_pmu_core(const char *name)
1568{
1569 return !strcmp(name, "cpu") || is_arm_pmu_core(name);
1570}
1571
Sukadev Bhattiproluc8d68282016-09-15 15:24:48 -07001572void print_pmu_events(const char *event_glob, bool name_only, bool quiet_flag,
Jin Yaoa7f6c8c2019-10-15 10:53:57 +08001573 bool long_desc, bool details_flag, bool deprecated)
Andi Kleendc098b32013-04-20 11:02:29 -07001574{
1575 struct perf_pmu *pmu;
1576 struct perf_pmu_alias *alias;
1577 char buf[1024];
1578 int printed = 0;
1579 int len, j;
Andi Kleendd5f1032016-09-15 15:24:50 -07001580 struct sevent *aliases;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001581 int numdesc = 0;
Andi Kleen61eb2eb2016-09-15 15:24:44 -07001582 int columns = pager_get_columns();
Andi Kleendd5f1032016-09-15 15:24:50 -07001583 char *topic = NULL;
Andi Kleendc098b32013-04-20 11:02:29 -07001584
1585 pmu = NULL;
1586 len = 0;
Adrian Hunter42634bc2014-10-23 13:45:10 +03001587 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
Andi Kleendc098b32013-04-20 11:02:29 -07001588 list_for_each_entry(alias, &pmu->aliases, list)
1589 len++;
Adrian Hunter42634bc2014-10-23 13:45:10 +03001590 if (pmu->selectable)
1591 len++;
1592 }
Andi Kleendd5f1032016-09-15 15:24:50 -07001593 aliases = zalloc(sizeof(struct sevent) * len);
Andi Kleendc098b32013-04-20 11:02:29 -07001594 if (!aliases)
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001595 goto out_enomem;
Andi Kleendc098b32013-04-20 11:02:29 -07001596 pmu = NULL;
1597 j = 0;
Adrian Hunter42634bc2014-10-23 13:45:10 +03001598 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
Andi Kleendc098b32013-04-20 11:02:29 -07001599 list_for_each_entry(alias, &pmu->aliases, list) {
Andi Kleen08e60ed2016-09-15 15:24:43 -07001600 char *name = alias->desc ? alias->name :
1601 format_alias(buf, sizeof(buf), pmu, alias);
John Garryc1b47452020-06-17 17:01:53 +08001602 bool is_cpu = is_pmu_core(pmu->name);
Andi Kleendc098b32013-04-20 11:02:29 -07001603
Jin Yaoa7f6c8c2019-10-15 10:53:57 +08001604 if (alias->deprecated && !deprecated)
1605 continue;
1606
Andi Kleendc098b32013-04-20 11:02:29 -07001607 if (event_glob != NULL &&
Andi Kleen38d14f02016-10-19 10:50:01 -07001608 !(strglobmatch_nocase(name, event_glob) ||
1609 (!is_cpu && strglobmatch_nocase(alias->name,
Andi Kleen67bdc352016-10-19 11:45:23 -07001610 event_glob)) ||
1611 (alias->topic &&
1612 strglobmatch_nocase(alias->topic, event_glob))))
Andi Kleendc098b32013-04-20 11:02:29 -07001613 continue;
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001614
Andi Kleen08e60ed2016-09-15 15:24:43 -07001615 if (is_cpu && !name_only && !alias->desc)
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001616 name = format_alias_or(buf, sizeof(buf), pmu, alias);
1617
Andi Kleen08e60ed2016-09-15 15:24:43 -07001618 aliases[j].name = name;
1619 if (is_cpu && !name_only && !alias->desc)
1620 aliases[j].name = format_alias_or(buf,
1621 sizeof(buf),
1622 pmu, alias);
1623 aliases[j].name = strdup(aliases[j].name);
1624 if (!aliases[j].name)
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001625 goto out_enomem;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001626
Sukadev Bhattiproluc8d68282016-09-15 15:24:48 -07001627 aliases[j].desc = long_desc ? alias->long_desc :
1628 alias->desc;
Andi Kleendd5f1032016-09-15 15:24:50 -07001629 aliases[j].topic = alias->topic;
Andi Kleenf2361022017-01-27 18:03:40 -08001630 aliases[j].str = alias->str;
1631 aliases[j].pmu = pmu->name;
Andi Kleen7f372a62017-03-20 13:17:09 -07001632 aliases[j].metric_expr = alias->metric_expr;
Andi Kleen96284812017-03-20 13:17:10 -07001633 aliases[j].metric_name = alias->metric_name;
John Garryce0dc7d2020-06-17 17:01:54 +08001634 aliases[j].is_cpu = is_cpu;
Andi Kleendc098b32013-04-20 11:02:29 -07001635 j++;
1636 }
Arnaldo Carvalho de Melofa52cea2015-10-02 15:28:16 -03001637 if (pmu->selectable &&
1638 (event_glob == NULL || strglobmatch(pmu->name, event_glob))) {
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001639 char *s;
1640 if (asprintf(&s, "%s//", pmu->name) < 0)
1641 goto out_enomem;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001642 aliases[j].name = s;
Adrian Hunter42634bc2014-10-23 13:45:10 +03001643 j++;
1644 }
1645 }
Andi Kleendc098b32013-04-20 11:02:29 -07001646 len = j;
Andi Kleendd5f1032016-09-15 15:24:50 -07001647 qsort(aliases, len, sizeof(struct sevent), cmp_sevent);
Andi Kleendc098b32013-04-20 11:02:29 -07001648 for (j = 0; j < len; j++) {
Andi Kleen15b22ed2017-01-27 18:03:38 -08001649 /* Skip duplicates */
1650 if (j > 0 && !strcmp(aliases[j].name, aliases[j - 1].name))
1651 continue;
Andi Kleendc098b32013-04-20 11:02:29 -07001652 if (name_only) {
Andi Kleen08e60ed2016-09-15 15:24:43 -07001653 printf("%s ", aliases[j].name);
Andi Kleendc098b32013-04-20 11:02:29 -07001654 continue;
1655 }
Andi Kleen1c5f01f2016-09-15 15:24:45 -07001656 if (aliases[j].desc && !quiet_flag) {
Andi Kleen08e60ed2016-09-15 15:24:43 -07001657 if (numdesc++ == 0)
1658 printf("\n");
Andi Kleendd5f1032016-09-15 15:24:50 -07001659 if (aliases[j].topic && (!topic ||
1660 strcmp(topic, aliases[j].topic))) {
1661 printf("%s%s:\n", topic ? "\n" : "",
1662 aliases[j].topic);
1663 topic = aliases[j].topic;
1664 }
Andi Kleen08e60ed2016-09-15 15:24:43 -07001665 printf(" %-50s\n", aliases[j].name);
1666 printf("%*s", 8, "[");
1667 wordwrap(aliases[j].desc, 8, columns, 0);
1668 printf("]\n");
Andi Kleenbf874fc2017-03-20 13:17:11 -07001669 if (details_flag) {
Andi Kleen7f372a62017-03-20 13:17:09 -07001670 printf("%*s%s/%s/ ", 8, "", aliases[j].pmu, aliases[j].str);
Andi Kleen96284812017-03-20 13:17:10 -07001671 if (aliases[j].metric_name)
1672 printf(" MetricName: %s", aliases[j].metric_name);
Andi Kleen7f372a62017-03-20 13:17:09 -07001673 if (aliases[j].metric_expr)
1674 printf(" MetricExpr: %s", aliases[j].metric_expr);
1675 putchar('\n');
1676 }
Andi Kleen08e60ed2016-09-15 15:24:43 -07001677 } else
1678 printf(" %-50s [Kernel PMU event]\n", aliases[j].name);
Andi Kleendc098b32013-04-20 11:02:29 -07001679 printed++;
1680 }
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03001681 if (printed && pager_in_use())
Andi Kleendc098b32013-04-20 11:02:29 -07001682 printf("\n");
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001683out_free:
1684 for (j = 0; j < len; j++)
Andi Kleen08e60ed2016-09-15 15:24:43 -07001685 zfree(&aliases[j].name);
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001686 zfree(&aliases);
1687 return;
1688
1689out_enomem:
1690 printf("FATAL: not enough memory to print PMU events\n");
1691 if (aliases)
1692 goto out_free;
Andi Kleendc098b32013-04-20 11:02:29 -07001693}
Andi Kleen4cabc3d2013-08-21 16:47:26 -07001694
1695bool pmu_have_event(const char *pname, const char *name)
1696{
1697 struct perf_pmu *pmu;
1698 struct perf_pmu_alias *alias;
1699
1700 pmu = NULL;
1701 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1702 if (strcmp(pname, pmu->name))
1703 continue;
1704 list_for_each_entry(alias, &pmu->aliases, list)
1705 if (!strcmp(alias->name, name))
1706 return true;
1707 }
1708 return false;
1709}
Adrian Hunter7d4bdab2014-07-31 09:00:50 +03001710
1711static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
1712{
Adrian Hunter7d4bdab2014-07-31 09:00:50 +03001713 char path[PATH_MAX];
1714 const char *sysfs;
1715
1716 sysfs = sysfs__mountpoint();
1717 if (!sysfs)
1718 return NULL;
1719
1720 snprintf(path, PATH_MAX,
1721 "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name);
Andi Kleend9664582019-11-20 16:15:11 -08001722 if (!file_available(path))
Adrian Hunter7d4bdab2014-07-31 09:00:50 +03001723 return NULL;
Adrian Hunter7d4bdab2014-07-31 09:00:50 +03001724 return fopen(path, "r");
1725}
1726
1727int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
1728 ...)
1729{
1730 va_list args;
1731 FILE *file;
1732 int ret = EOF;
1733
1734 va_start(args, fmt);
1735 file = perf_pmu__open_file(pmu, name);
1736 if (file) {
1737 ret = vfscanf(file, fmt, args);
1738 fclose(file);
1739 }
1740 va_end(args);
1741 return ret;
1742}
Kan Liang9fbc61f2020-03-19 13:25:01 -07001743
1744static int perf_pmu__new_caps(struct list_head *list, char *name, char *value)
1745{
1746 struct perf_pmu_caps *caps = zalloc(sizeof(*caps));
1747
1748 if (!caps)
1749 return -ENOMEM;
1750
1751 caps->name = strdup(name);
1752 if (!caps->name)
1753 goto free_caps;
1754 caps->value = strndup(value, strlen(value) - 1);
1755 if (!caps->value)
1756 goto free_name;
1757 list_add_tail(&caps->list, list);
1758 return 0;
1759
1760free_name:
1761 zfree(caps->name);
1762free_caps:
1763 free(caps);
1764
1765 return -ENOMEM;
1766}
1767
1768/*
1769 * Reading/parsing the given pmu capabilities, which should be located at:
1770 * /sys/bus/event_source/devices/<dev>/caps as sysfs group attributes.
1771 * Return the number of capabilities
1772 */
1773int perf_pmu__caps_parse(struct perf_pmu *pmu)
1774{
1775 struct stat st;
1776 char caps_path[PATH_MAX];
1777 const char *sysfs = sysfs__mountpoint();
1778 DIR *caps_dir;
1779 struct dirent *evt_ent;
1780 int nr_caps = 0;
1781
1782 if (!sysfs)
1783 return -1;
1784
1785 snprintf(caps_path, PATH_MAX,
1786 "%s" EVENT_SOURCE_DEVICE_PATH "%s/caps", sysfs, pmu->name);
1787
1788 if (stat(caps_path, &st) < 0)
1789 return 0; /* no error if caps does not exist */
1790
1791 caps_dir = opendir(caps_path);
1792 if (!caps_dir)
1793 return -EINVAL;
1794
1795 while ((evt_ent = readdir(caps_dir)) != NULL) {
1796 char path[PATH_MAX + NAME_MAX + 1];
1797 char *name = evt_ent->d_name;
1798 char value[128];
1799 FILE *file;
1800
1801 if (!strcmp(name, ".") || !strcmp(name, ".."))
1802 continue;
1803
1804 snprintf(path, sizeof(path), "%s/%s", caps_path, name);
1805
1806 file = fopen(path, "r");
1807 if (!file)
1808 continue;
1809
1810 if (!fgets(value, sizeof(value), file) ||
1811 (perf_pmu__new_caps(&pmu->caps, name, value) < 0)) {
1812 fclose(file);
1813 continue;
1814 }
1815
1816 nr_caps++;
1817 fclose(file);
1818 }
1819
1820 closedir(caps_dir);
1821
1822 return nr_caps;
1823}
Jin Yaoe4064772021-03-10 13:11:38 +08001824
1825void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config,
1826 char *name)
1827{
1828 struct perf_pmu_format *format;
1829 __u64 masks = 0, bits;
1830 char buf[100];
1831 unsigned int i;
1832
1833 list_for_each_entry(format, &pmu->format, list) {
1834 if (format->value != PERF_PMU_FORMAT_VALUE_CONFIG)
1835 continue;
1836
1837 for_each_set_bit(i, format->bits, PERF_PMU_FORMAT_BITS)
1838 masks |= 1ULL << i;
1839 }
1840
1841 /*
1842 * Kernel doesn't export any valid format bits.
1843 */
1844 if (masks == 0)
1845 return;
1846
1847 bits = config & ~masks;
1848 if (bits == 0)
1849 return;
1850
1851 bitmap_scnprintf((unsigned long *)&bits, sizeof(bits) * 8, buf, sizeof(buf));
1852
1853 pr_warning("WARNING: event '%s' not valid (bits %s of config "
1854 "'%llx' not supported by kernel)!\n",
1855 name ?: "N/A", buf, config);
1856}