blob: 8b99fd312aae642ed3ca9a422ca3bef8473e47ad [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"
Jiri Olsacd82a322012-03-15 20:09:17 +010021#include "pmu.h"
22#include "parse-events.h"
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -070023#include "header.h"
24#include "pmu-events/pmu-events.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 Meloab1bf6532013-01-18 17:05:09 -030029struct perf_pmu_format {
30 char *name;
31 int value;
32 DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
33 struct list_head list;
34};
35
Jiri Olsacd82a322012-03-15 20:09:17 +010036int perf_pmu_parse(struct list_head *list, char *name);
37extern FILE *perf_pmu_in;
38
39static LIST_HEAD(pmus);
40
41/*
42 * Parse & process all the sysfs attributes located under
43 * the directory specified in 'dir' parameter.
44 */
Jiri Olsacff7f952012-11-10 01:46:50 +010045int perf_pmu__format_parse(char *dir, struct list_head *head)
Jiri Olsacd82a322012-03-15 20:09:17 +010046{
47 struct dirent *evt_ent;
48 DIR *format_dir;
49 int ret = 0;
50
51 format_dir = opendir(dir);
52 if (!format_dir)
53 return -EINVAL;
54
55 while (!ret && (evt_ent = readdir(format_dir))) {
56 char path[PATH_MAX];
57 char *name = evt_ent->d_name;
58 FILE *file;
59
60 if (!strcmp(name, ".") || !strcmp(name, ".."))
61 continue;
62
63 snprintf(path, PATH_MAX, "%s/%s", dir, name);
64
65 ret = -EINVAL;
66 file = fopen(path, "r");
67 if (!file)
68 break;
69
70 perf_pmu_in = file;
71 ret = perf_pmu_parse(head, name);
72 fclose(file);
73 }
74
75 closedir(format_dir);
76 return ret;
77}
78
79/*
80 * Reading/parsing the default pmu format definition, which should be
81 * located at:
82 * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
83 */
Adrian Hunterb6b96fb2013-07-04 16:20:25 +030084static int pmu_format(const char *name, struct list_head *format)
Jiri Olsacd82a322012-03-15 20:09:17 +010085{
Jiri Olsacd82a322012-03-15 20:09:17 +010086 char path[PATH_MAX];
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -030087 const char *sysfs = sysfs__mountpoint();
Jiri Olsacd82a322012-03-15 20:09:17 +010088
Jiri Olsacd82a322012-03-15 20:09:17 +010089 if (!sysfs)
90 return -1;
91
92 snprintf(path, PATH_MAX,
Robert Richter50a96672012-08-16 21:10:24 +020093 "%s" EVENT_SOURCE_DEVICE_PATH "%s/format", sysfs, name);
Jiri Olsacd82a322012-03-15 20:09:17 +010094
Andi Kleend9664582019-11-20 16:15:11 -080095 if (!file_available(path))
96 return 0;
Jiri Olsacd82a322012-03-15 20:09:17 +010097
Jiri Olsacff7f952012-11-10 01:46:50 +010098 if (perf_pmu__format_parse(path, format))
Jiri Olsacd82a322012-03-15 20:09:17 +010099 return -1;
100
101 return 0;
102}
103
Jin Yaoa55ab7c2019-08-28 13:59:29 +0800104int perf_pmu__convert_scale(const char *scale, char **end, double *sval)
Andi Kleend02fc6b2017-01-03 07:08:23 -0800105{
106 char *lc;
107 int ret = 0;
108
109 /*
110 * save current locale
111 */
112 lc = setlocale(LC_NUMERIC, NULL);
113
114 /*
115 * The lc string may be allocated in static storage,
116 * so get a dynamic copy to make it survive setlocale
117 * call below.
118 */
119 lc = strdup(lc);
120 if (!lc) {
121 ret = -ENOMEM;
122 goto out;
123 }
124
125 /*
126 * force to C locale to ensure kernel
127 * scale string is converted correctly.
128 * kernel uses default C locale.
129 */
130 setlocale(LC_NUMERIC, "C");
131
132 *sval = strtod(scale, end);
133
134out:
135 /* restore locale */
136 setlocale(LC_NUMERIC, lc);
137 free(lc);
138 return ret;
139}
140
Stephane Eranian410136f2013-11-12 17:58:49 +0100141static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *name)
142{
143 struct stat st;
144 ssize_t sret;
145 char scale[128];
146 int fd, ret = -1;
147 char path[PATH_MAX];
Stephane Eranian410136f2013-11-12 17:58:49 +0100148
Ben Hutchings11a64a02018-11-11 18:45:24 +0000149 scnprintf(path, PATH_MAX, "%s/%s.scale", dir, name);
Stephane Eranian410136f2013-11-12 17:58:49 +0100150
151 fd = open(path, O_RDONLY);
152 if (fd == -1)
153 return -1;
154
155 if (fstat(fd, &st) < 0)
156 goto error;
157
158 sret = read(fd, scale, sizeof(scale)-1);
159 if (sret < 0)
160 goto error;
161
Madhavan Srinivasan9ecae062015-05-31 11:36:23 +0530162 if (scale[sret - 1] == '\n')
163 scale[sret - 1] = '\0';
164 else
165 scale[sret] = '\0';
166
Jin Yaoa55ab7c2019-08-28 13:59:29 +0800167 ret = perf_pmu__convert_scale(scale, NULL, &alias->scale);
Stephane Eranian410136f2013-11-12 17:58:49 +0100168error:
169 close(fd);
170 return ret;
171}
172
173static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *name)
174{
175 char path[PATH_MAX];
176 ssize_t sret;
177 int fd;
178
Ben Hutchings11a64a02018-11-11 18:45:24 +0000179 scnprintf(path, PATH_MAX, "%s/%s.unit", dir, name);
Stephane Eranian410136f2013-11-12 17:58:49 +0100180
181 fd = open(path, O_RDONLY);
182 if (fd == -1)
183 return -1;
184
Markus Trippelsdorfd85ce832015-12-14 16:44:40 +0100185 sret = read(fd, alias->unit, UNIT_MAX_LEN);
Stephane Eranian410136f2013-11-12 17:58:49 +0100186 if (sret < 0)
187 goto error;
188
189 close(fd);
190
Madhavan Srinivasan9ecae062015-05-31 11:36:23 +0530191 if (alias->unit[sret - 1] == '\n')
192 alias->unit[sret - 1] = '\0';
193 else
194 alias->unit[sret] = '\0';
Stephane Eranian410136f2013-11-12 17:58:49 +0100195
196 return 0;
197error:
198 close(fd);
199 alias->unit[0] = '\0';
200 return -1;
201}
202
Matt Fleming044330c2014-11-21 10:31:12 +0100203static int
204perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, char *dir, char *name)
205{
206 char path[PATH_MAX];
207 int fd;
208
Ben Hutchings11a64a02018-11-11 18:45:24 +0000209 scnprintf(path, PATH_MAX, "%s/%s.per-pkg", dir, name);
Matt Fleming044330c2014-11-21 10:31:12 +0100210
211 fd = open(path, O_RDONLY);
212 if (fd == -1)
213 return -1;
214
215 close(fd);
216
217 alias->per_pkg = true;
218 return 0;
219}
220
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100221static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias,
222 char *dir, char *name)
223{
224 char path[PATH_MAX];
225 int fd;
226
Ben Hutchings11a64a02018-11-11 18:45:24 +0000227 scnprintf(path, PATH_MAX, "%s/%s.snapshot", dir, name);
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100228
229 fd = open(path, O_RDONLY);
230 if (fd == -1)
231 return -1;
232
233 alias->snapshot = true;
234 close(fd);
235 return 0;
236}
237
Thomas Richter6dde6422018-06-15 12:11:05 +0200238static void perf_pmu_assign_str(char *name, const char *field, char **old_str,
239 char **new_str)
240{
241 if (!*old_str)
242 goto set_new;
243
244 if (*new_str) { /* Have new string, check with old */
245 if (strcasecmp(*old_str, *new_str))
246 pr_debug("alias %s differs in field '%s'\n",
247 name, field);
248 zfree(old_str);
249 } else /* Nothing new --> keep old string */
250 return;
251set_new:
252 *old_str = *new_str;
253 *new_str = NULL;
254}
255
256static void perf_pmu_update_alias(struct perf_pmu_alias *old,
257 struct perf_pmu_alias *newalias)
258{
259 perf_pmu_assign_str(old->name, "desc", &old->desc, &newalias->desc);
260 perf_pmu_assign_str(old->name, "long_desc", &old->long_desc,
261 &newalias->long_desc);
262 perf_pmu_assign_str(old->name, "topic", &old->topic, &newalias->topic);
263 perf_pmu_assign_str(old->name, "metric_expr", &old->metric_expr,
264 &newalias->metric_expr);
265 perf_pmu_assign_str(old->name, "metric_name", &old->metric_name,
266 &newalias->metric_name);
267 perf_pmu_assign_str(old->name, "value", &old->str, &newalias->str);
268 old->scale = newalias->scale;
269 old->per_pkg = newalias->per_pkg;
270 old->snapshot = newalias->snapshot;
271 memcpy(old->unit, newalias->unit, sizeof(old->unit));
272}
273
274/* Delete an alias entry. */
275static void perf_pmu_free_alias(struct perf_pmu_alias *newalias)
276{
277 zfree(&newalias->name);
278 zfree(&newalias->desc);
279 zfree(&newalias->long_desc);
280 zfree(&newalias->topic);
281 zfree(&newalias->str);
282 zfree(&newalias->metric_expr);
283 zfree(&newalias->metric_name);
284 parse_events_terms__purge(&newalias->terms);
285 free(newalias);
286}
287
288/* Merge an alias, search in alias list. If this name is already
289 * present merge both of them to combine all information.
290 */
291static bool perf_pmu_merge_alias(struct perf_pmu_alias *newalias,
292 struct list_head *alist)
293{
294 struct perf_pmu_alias *a;
295
296 list_for_each_entry(a, alist, list) {
297 if (!strcasecmp(newalias->name, a->name)) {
298 perf_pmu_update_alias(a, newalias);
299 perf_pmu_free_alias(newalias);
300 return true;
301 }
302 }
303 return false;
304}
305
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700306static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name,
Andi Kleenfedb2b52017-01-27 18:03:37 -0800307 char *desc, char *val,
308 char *long_desc, char *topic,
Andi Kleen00636c32017-03-20 13:17:07 -0700309 char *unit, char *perpkg,
Andi Kleen96284812017-03-20 13:17:10 -0700310 char *metric_expr,
Jin Yaoa7f6c8c2019-10-15 10:53:57 +0800311 char *metric_name,
312 char *deprecated)
Zheng Yana6146d52012-06-15 14:31:41 +0800313{
Thomas Richter0c24d6f2018-06-15 12:11:04 +0200314 struct parse_events_term *term;
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300315 struct perf_pmu_alias *alias;
Zheng Yana6146d52012-06-15 14:31:41 +0800316 int ret;
Andi Kleenfedb2b52017-01-27 18:03:37 -0800317 int num;
Thomas Richter0c24d6f2018-06-15 12:11:04 +0200318 char newval[256];
Zheng Yana6146d52012-06-15 14:31:41 +0800319
Zheng Yana6146d52012-06-15 14:31:41 +0800320 alias = malloc(sizeof(*alias));
321 if (!alias)
322 return -ENOMEM;
323
324 INIT_LIST_HEAD(&alias->terms);
Stephane Eranian410136f2013-11-12 17:58:49 +0100325 alias->scale = 1.0;
326 alias->unit[0] = '\0';
Matt Fleming044330c2014-11-21 10:31:12 +0100327 alias->per_pkg = false;
Stephane Eranian84530922016-01-06 19:50:01 +0100328 alias->snapshot = false;
Jin Yaoa7f6c8c2019-10-15 10:53:57 +0800329 alias->deprecated = false;
Stephane Eranian410136f2013-11-12 17:58:49 +0100330
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700331 ret = parse_events_terms(&alias->terms, val);
Zheng Yana6146d52012-06-15 14:31:41 +0800332 if (ret) {
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700333 pr_err("Cannot parse alias %s: %d\n", val, ret);
Zheng Yana6146d52012-06-15 14:31:41 +0800334 free(alias);
335 return ret;
336 }
337
Thomas Richter0c24d6f2018-06-15 12:11:04 +0200338 /* Scan event and remove leading zeroes, spaces, newlines, some
339 * platforms have terms specified as
340 * event=0x0091 (read from files ../<PMU>/events/<FILE>
341 * and terms specified as event=0x91 (read from JSON files).
342 *
343 * Rebuild string to make alias->str member comparable.
344 */
345 memset(newval, 0, sizeof(newval));
346 ret = 0;
347 list_for_each_entry(term, &alias->terms, list) {
348 if (ret)
349 ret += scnprintf(newval + ret, sizeof(newval) - ret,
350 ",");
351 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM)
352 ret += scnprintf(newval + ret, sizeof(newval) - ret,
353 "%s=%#x", term->config, term->val.num);
354 else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
355 ret += scnprintf(newval + ret, sizeof(newval) - ret,
356 "%s=%s", term->config, term->val.str);
357 }
358
Zheng Yana6146d52012-06-15 14:31:41 +0800359 alias->name = strdup(name);
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700360 if (dir) {
361 /*
362 * load unit name and scale if available
363 */
364 perf_pmu__parse_unit(alias, dir, name);
365 perf_pmu__parse_scale(alias, dir, name);
366 perf_pmu__parse_per_pkg(alias, dir, name);
367 perf_pmu__parse_snapshot(alias, dir, name);
368 }
Stephane Eranian410136f2013-11-12 17:58:49 +0100369
Andi Kleen00636c32017-03-20 13:17:07 -0700370 alias->metric_expr = metric_expr ? strdup(metric_expr) : NULL;
Andi Kleen96284812017-03-20 13:17:10 -0700371 alias->metric_name = metric_name ? strdup(metric_name): NULL;
Andi Kleen08e60ed2016-09-15 15:24:43 -0700372 alias->desc = desc ? strdup(desc) : NULL;
Sukadev Bhattiproluc8d68282016-09-15 15:24:48 -0700373 alias->long_desc = long_desc ? strdup(long_desc) :
374 desc ? strdup(desc) : NULL;
Andi Kleendd5f1032016-09-15 15:24:50 -0700375 alias->topic = topic ? strdup(topic) : NULL;
Andi Kleenfedb2b52017-01-27 18:03:37 -0800376 if (unit) {
Jin Yaoa55ab7c2019-08-28 13:59:29 +0800377 if (perf_pmu__convert_scale(unit, &unit, &alias->scale) < 0)
Andi Kleenfedb2b52017-01-27 18:03:37 -0800378 return -1;
379 snprintf(alias->unit, sizeof(alias->unit), "%s", unit);
380 }
381 alias->per_pkg = perpkg && sscanf(perpkg, "%d", &num) == 1 && num == 1;
Thomas Richter0c24d6f2018-06-15 12:11:04 +0200382 alias->str = strdup(newval);
Andi Kleenf2361022017-01-27 18:03:40 -0800383
Jin Yaoa7f6c8c2019-10-15 10:53:57 +0800384 if (deprecated)
385 alias->deprecated = true;
386
Thomas Richter6dde6422018-06-15 12:11:05 +0200387 if (!perf_pmu_merge_alias(alias, list))
388 list_add_tail(&alias->list, list);
Stephane Eranian410136f2013-11-12 17:58:49 +0100389
Zheng Yana6146d52012-06-15 14:31:41 +0800390 return 0;
391}
392
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700393static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file)
394{
395 char buf[256];
396 int ret;
397
398 ret = fread(buf, 1, sizeof(buf), file);
399 if (ret == 0)
400 return -EINVAL;
401
402 buf[ret] = 0;
403
Thomas Richterea23ac72018-06-15 12:11:03 +0200404 /* Remove trailing newline from sysfs file */
Arnaldo Carvalho de Melo13c230a2019-06-26 12:13:13 -0300405 strim(buf);
Thomas Richterea23ac72018-06-15 12:11:03 +0200406
Andi Kleenfedb2b52017-01-27 18:03:37 -0800407 return __perf_pmu__new_alias(list, dir, name, NULL, buf, NULL, NULL, NULL,
Jin Yaoa7f6c8c2019-10-15 10:53:57 +0800408 NULL, NULL, NULL, NULL);
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700409}
410
Matt Fleming46441bd2014-09-24 15:04:06 +0100411static inline bool pmu_alias_info_file(char *name)
412{
413 size_t len;
414
415 len = strlen(name);
416 if (len > 5 && !strcmp(name + len - 5, ".unit"))
417 return true;
418 if (len > 6 && !strcmp(name + len - 6, ".scale"))
419 return true;
Matt Fleming044330c2014-11-21 10:31:12 +0100420 if (len > 8 && !strcmp(name + len - 8, ".per-pkg"))
421 return true;
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100422 if (len > 9 && !strcmp(name + len - 9, ".snapshot"))
423 return true;
Matt Fleming46441bd2014-09-24 15:04:06 +0100424
425 return false;
426}
427
Zheng Yana6146d52012-06-15 14:31:41 +0800428/*
429 * Process all the sysfs attributes located under the directory
430 * specified in 'dir' parameter.
431 */
432static int pmu_aliases_parse(char *dir, struct list_head *head)
433{
434 struct dirent *evt_ent;
435 DIR *event_dir;
Zheng Yana6146d52012-06-15 14:31:41 +0800436
437 event_dir = opendir(dir);
438 if (!event_dir)
439 return -EINVAL;
440
Andi Kleen940db6d2016-02-17 14:44:55 -0800441 while ((evt_ent = readdir(event_dir))) {
Zheng Yana6146d52012-06-15 14:31:41 +0800442 char path[PATH_MAX];
443 char *name = evt_ent->d_name;
444 FILE *file;
445
446 if (!strcmp(name, ".") || !strcmp(name, ".."))
447 continue;
448
Stephane Eranian410136f2013-11-12 17:58:49 +0100449 /*
Matt Fleming46441bd2014-09-24 15:04:06 +0100450 * skip info files parsed in perf_pmu__new_alias()
Stephane Eranian410136f2013-11-12 17:58:49 +0100451 */
Matt Fleming46441bd2014-09-24 15:04:06 +0100452 if (pmu_alias_info_file(name))
Stephane Eranian410136f2013-11-12 17:58:49 +0100453 continue;
454
Jiri Olsa77f18152018-03-19 09:29:01 +0100455 scnprintf(path, PATH_MAX, "%s/%s", dir, name);
Zheng Yana6146d52012-06-15 14:31:41 +0800456
Zheng Yana6146d52012-06-15 14:31:41 +0800457 file = fopen(path, "r");
Andi Kleen940db6d2016-02-17 14:44:55 -0800458 if (!file) {
459 pr_debug("Cannot open %s\n", path);
460 continue;
461 }
Stephane Eranian410136f2013-11-12 17:58:49 +0100462
Andi Kleen940db6d2016-02-17 14:44:55 -0800463 if (perf_pmu__new_alias(head, dir, name, file) < 0)
464 pr_debug("Cannot set up %s\n", name);
Zheng Yana6146d52012-06-15 14:31:41 +0800465 fclose(file);
466 }
467
468 closedir(event_dir);
Andi Kleen940db6d2016-02-17 14:44:55 -0800469 return 0;
Zheng Yana6146d52012-06-15 14:31:41 +0800470}
471
472/*
473 * Reading the pmu event aliases definition, which should be located at:
474 * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
475 */
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300476static int pmu_aliases(const char *name, struct list_head *head)
Zheng Yana6146d52012-06-15 14:31:41 +0800477{
Zheng Yana6146d52012-06-15 14:31:41 +0800478 char path[PATH_MAX];
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -0300479 const char *sysfs = sysfs__mountpoint();
Zheng Yana6146d52012-06-15 14:31:41 +0800480
Zheng Yana6146d52012-06-15 14:31:41 +0800481 if (!sysfs)
482 return -1;
483
484 snprintf(path, PATH_MAX,
485 "%s/bus/event_source/devices/%s/events", sysfs, name);
486
Andi Kleend9664582019-11-20 16:15:11 -0800487 if (!file_available(path))
488 return 0;
Zheng Yana6146d52012-06-15 14:31:41 +0800489
490 if (pmu_aliases_parse(path, head))
491 return -1;
492
493 return 0;
494}
495
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300496static int pmu_alias_terms(struct perf_pmu_alias *alias,
Zheng Yana6146d52012-06-15 14:31:41 +0800497 struct list_head *terms)
498{
Jiri Olsa7c2f8162014-04-16 20:49:02 +0200499 struct parse_events_term *term, *cloned;
Zheng Yana6146d52012-06-15 14:31:41 +0800500 LIST_HEAD(list);
501 int ret;
502
503 list_for_each_entry(term, &alias->terms, list) {
Jiri Olsa7c2f8162014-04-16 20:49:02 +0200504 ret = parse_events_term__clone(&cloned, term);
Zheng Yana6146d52012-06-15 14:31:41 +0800505 if (ret) {
Arnaldo Carvalho de Melo682dc242016-02-12 16:48:00 -0300506 parse_events_terms__purge(&list);
Zheng Yana6146d52012-06-15 14:31:41 +0800507 return ret;
508 }
Andi Kleenc2f1cea2017-10-20 13:27:55 -0700509 /*
510 * Weak terms don't override command line options,
511 * which we don't want for implicit terms in aliases.
512 */
513 cloned->weak = true;
Jiri Olsa7c2f8162014-04-16 20:49:02 +0200514 list_add_tail(&cloned->list, &list);
Zheng Yana6146d52012-06-15 14:31:41 +0800515 }
516 list_splice(&list, terms);
517 return 0;
518}
519
Jiri Olsacd82a322012-03-15 20:09:17 +0100520/*
521 * Reading/parsing the default pmu type value, which should be
522 * located at:
523 * /sys/bus/event_source/devices/<dev>/type as sysfs attribute.
524 */
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300525static int pmu_type(const char *name, __u32 *type)
Jiri Olsacd82a322012-03-15 20:09:17 +0100526{
Jiri Olsacd82a322012-03-15 20:09:17 +0100527 char path[PATH_MAX];
Jiri Olsacd82a322012-03-15 20:09:17 +0100528 FILE *file;
529 int ret = 0;
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -0300530 const char *sysfs = sysfs__mountpoint();
Jiri Olsacd82a322012-03-15 20:09:17 +0100531
Jiri Olsacd82a322012-03-15 20:09:17 +0100532 if (!sysfs)
533 return -1;
534
535 snprintf(path, PATH_MAX,
Robert Richter50a96672012-08-16 21:10:24 +0200536 "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name);
Jiri Olsacd82a322012-03-15 20:09:17 +0100537
Andi Kleend9664582019-11-20 16:15:11 -0800538 if (access(path, R_OK) < 0)
Jiri Olsacd82a322012-03-15 20:09:17 +0100539 return -1;
540
541 file = fopen(path, "r");
542 if (!file)
543 return -EINVAL;
544
545 if (1 != fscanf(file, "%u", type))
546 ret = -1;
547
548 fclose(file);
549 return ret;
550}
551
Robert Richter50a96672012-08-16 21:10:24 +0200552/* Add all pmus in sysfs to pmu list: */
553static void pmu_read_sysfs(void)
554{
555 char path[PATH_MAX];
Robert Richter50a96672012-08-16 21:10:24 +0200556 DIR *dir;
557 struct dirent *dent;
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -0300558 const char *sysfs = sysfs__mountpoint();
Robert Richter50a96672012-08-16 21:10:24 +0200559
Robert Richter50a96672012-08-16 21:10:24 +0200560 if (!sysfs)
561 return;
562
563 snprintf(path, PATH_MAX,
564 "%s" EVENT_SOURCE_DEVICE_PATH, sysfs);
565
566 dir = opendir(path);
567 if (!dir)
568 return;
569
570 while ((dent = readdir(dir))) {
571 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
572 continue;
573 /* add to static LIST_HEAD(pmus): */
574 perf_pmu__find(dent->d_name);
575 }
576
577 closedir(dir);
578}
579
Jiri Olsaf8548392019-07-21 13:23:49 +0200580static struct perf_cpu_map *__pmu_cpumask(const char *path)
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800581{
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800582 FILE *file;
Jiri Olsaf8548392019-07-21 13:23:49 +0200583 struct perf_cpu_map *cpus;
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800584
585 file = fopen(path, "r");
586 if (!file)
587 return NULL;
588
Jiri Olsa9c3516d2019-07-21 13:24:30 +0200589 cpus = perf_cpu_map__read(file);
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800590 fclose(file);
591 return cpus;
592}
593
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700594/*
Mark Rutland66ec11912017-10-06 19:38:22 +0100595 * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64)
596 * may have a "cpus" file.
597 */
598#define CPUS_TEMPLATE_UNCORE "%s/bus/event_source/devices/%s/cpumask"
599#define CPUS_TEMPLATE_CPU "%s/bus/event_source/devices/%s/cpus"
600
Jiri Olsaf8548392019-07-21 13:23:49 +0200601static struct perf_cpu_map *pmu_cpumask(const char *name)
Mark Rutland66ec11912017-10-06 19:38:22 +0100602{
603 char path[PATH_MAX];
Jiri Olsaf8548392019-07-21 13:23:49 +0200604 struct perf_cpu_map *cpus;
Mark Rutland66ec11912017-10-06 19:38:22 +0100605 const char *sysfs = sysfs__mountpoint();
606 const char *templates[] = {
607 CPUS_TEMPLATE_UNCORE,
608 CPUS_TEMPLATE_CPU,
609 NULL
610 };
611 const char **template;
612
613 if (!sysfs)
614 return NULL;
615
616 for (template = templates; *template; template++) {
617 snprintf(path, PATH_MAX, *template, sysfs, name);
618 cpus = __pmu_cpumask(path);
619 if (cpus)
620 return cpus;
621 }
622
623 return NULL;
624}
625
626static bool pmu_is_uncore(const char *name)
627{
628 char path[PATH_MAX];
Andi Kleend9664582019-11-20 16:15:11 -0800629 const char *sysfs;
Mark Rutland66ec11912017-10-06 19:38:22 +0100630
Andi Kleend9664582019-11-20 16:15:11 -0800631 sysfs = sysfs__mountpoint();
Mark Rutland66ec11912017-10-06 19:38:22 +0100632 snprintf(path, PATH_MAX, CPUS_TEMPLATE_UNCORE, sysfs, name);
Andi Kleend9664582019-11-20 16:15:11 -0800633 return file_available(path);
Mark Rutland66ec11912017-10-06 19:38:22 +0100634}
635
636/*
Ganapatrao Kulkarni14b22ae2017-08-24 16:30:58 +0530637 * PMU CORE devices have different name other than cpu in sysfs on some
Kan Liang292c34c2018-04-24 11:20:10 -0700638 * platforms.
639 * Looking for possible sysfs files to identify the arm core device.
Ganapatrao Kulkarni14b22ae2017-08-24 16:30:58 +0530640 */
Kan Liang292c34c2018-04-24 11:20:10 -0700641static int is_arm_pmu_core(const char *name)
Ganapatrao Kulkarni14b22ae2017-08-24 16:30:58 +0530642{
Ganapatrao Kulkarni14b22ae2017-08-24 16:30:58 +0530643 char path[PATH_MAX];
644 const char *sysfs = sysfs__mountpoint();
645
646 if (!sysfs)
647 return 0;
648
Ganapatrao Kulkarni14b22ae2017-08-24 16:30:58 +0530649 /* Look for cpu sysfs (specific to arm) */
650 scnprintf(path, PATH_MAX, "%s/bus/event_source/devices/%s/cpus",
651 sysfs, name);
Andi Kleend9664582019-11-20 16:15:11 -0800652 return file_available(path);
Ganapatrao Kulkarni14b22ae2017-08-24 16:30:58 +0530653}
654
Ganapatrao Kulkarni54e32dc2017-10-17 00:02:18 +0530655static char *perf_pmu__getcpuid(struct perf_pmu *pmu)
Andi Kleend77ade92017-08-31 12:40:30 -0700656{
657 char *cpuid;
658 static bool printed;
659
660 cpuid = getenv("PERF_CPUID");
661 if (cpuid)
662 cpuid = strdup(cpuid);
663 if (!cpuid)
Ganapatrao Kulkarni54e32dc2017-10-17 00:02:18 +0530664 cpuid = get_cpuid_str(pmu);
Andi Kleend77ade92017-08-31 12:40:30 -0700665 if (!cpuid)
666 return NULL;
667
668 if (!printed) {
669 pr_debug("Using CPUID %s\n", cpuid);
670 printed = true;
671 }
672 return cpuid;
673}
674
Ganapatrao Kulkarni54e32dc2017-10-17 00:02:18 +0530675struct pmu_events_map *perf_pmu__find_map(struct perf_pmu *pmu)
Andi Kleend77ade92017-08-31 12:40:30 -0700676{
677 struct pmu_events_map *map;
Ganapatrao Kulkarni54e32dc2017-10-17 00:02:18 +0530678 char *cpuid = perf_pmu__getcpuid(pmu);
Andi Kleend77ade92017-08-31 12:40:30 -0700679 int i;
680
Ganapatrao Kulkarnide3d0f12017-10-17 00:02:22 +0530681 /* on some platforms which uses cpus map, cpuid can be NULL for
682 * PMUs other than CORE PMUs.
683 */
684 if (!cpuid)
685 return NULL;
686
Andi Kleend77ade92017-08-31 12:40:30 -0700687 i = 0;
688 for (;;) {
689 map = &pmu_events_map[i++];
690 if (!map->table) {
691 map = NULL;
692 break;
693 }
694
Thomas Richter4cb7d3e2018-02-13 16:14:18 +0100695 if (!strcmp_cpuid_str(map->cpuid, cpuid))
Andi Kleend77ade92017-08-31 12:40:30 -0700696 break;
697 }
698 free(cpuid);
699 return map;
700}
701
John Garry730670b12019-06-28 22:35:49 +0800702static bool pmu_uncore_alias_match(const char *pmu_name, const char *name)
703{
704 char *tmp = NULL, *tok, *str;
705 bool res;
706
707 str = strdup(pmu_name);
708 if (!str)
709 return false;
710
711 /*
712 * uncore alias may be from different PMU with common prefix
713 */
714 tok = strtok_r(str, ",", &tmp);
715 if (strncmp(pmu_name, tok, strlen(tok))) {
716 res = false;
717 goto out;
718 }
719
720 /*
721 * Match more complex aliases where the alias name is a comma-delimited
722 * list of tokens, orderly contained in the matching PMU name.
723 *
724 * Example: For alias "socket,pmuname" and PMU "socketX_pmunameY", we
725 * match "socket" in "socketX_pmunameY" and then "pmuname" in
726 * "pmunameY".
727 */
728 for (; tok; name += strlen(tok), tok = strtok_r(NULL, ",", &tmp)) {
729 name = strstr(name, tok);
730 if (!name) {
731 res = false;
732 goto out;
733 }
734 }
735
736 res = true;
737out:
738 free(str);
739 return res;
740}
741
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700742/*
743 * From the pmu_events_map, find the table of PMU events that corresponds
744 * to the current running CPU. Then, add all PMU events from that table
745 * as aliases.
746 */
Ganapatrao Kulkarni54e32dc2017-10-17 00:02:18 +0530747static void pmu_add_cpu_aliases(struct list_head *head, struct perf_pmu *pmu)
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700748{
749 int i;
750 struct pmu_events_map *map;
Ganapatrao Kulkarni54e32dc2017-10-17 00:02:18 +0530751 const char *name = pmu->name;
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700752
Ganapatrao Kulkarni54e32dc2017-10-17 00:02:18 +0530753 map = perf_pmu__find_map(pmu);
Andi Kleend77ade92017-08-31 12:40:30 -0700754 if (!map)
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700755 return;
756
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700757 /*
758 * Found a matching PMU events table. Create aliases
759 */
760 i = 0;
761 while (1) {
John Garry599ee182019-06-14 22:07:59 +0800762 const char *cpu_name = is_arm_pmu_core(name) ? name : "cpu";
763 struct pmu_event *pe = &map->table[i++];
764 const char *pname = pe->pmu ? pe->pmu : cpu_name;
Andi Kleenfedb2b52017-01-27 18:03:37 -0800765
Andi Kleenb18f3e32017-08-31 12:40:31 -0700766 if (!pe->name) {
767 if (pe->metric_group || pe->metric_name)
768 continue;
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700769 break;
Andi Kleenb18f3e32017-08-31 12:40:31 -0700770 }
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700771
John Garry599ee182019-06-14 22:07:59 +0800772 if (pmu_is_uncore(name) &&
John Garry730670b12019-06-28 22:35:49 +0800773 pmu_uncore_alias_match(pname, name))
John Garry599ee182019-06-14 22:07:59 +0800774 goto new_alias;
Kan Liange94d6b72019-03-15 11:00:14 -0700775
John Garry599ee182019-06-14 22:07:59 +0800776 if (strcmp(pname, name))
777 continue;
Andi Kleenfedb2b52017-01-27 18:03:37 -0800778
Kan Liange94d6b72019-03-15 11:00:14 -0700779new_alias:
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700780 /* need type casts to override 'const' */
781 __perf_pmu__new_alias(head, NULL, (char *)pe->name,
Sukadev Bhattiproluc8d68282016-09-15 15:24:48 -0700782 (char *)pe->desc, (char *)pe->event,
Andi Kleenfedb2b52017-01-27 18:03:37 -0800783 (char *)pe->long_desc, (char *)pe->topic,
Andi Kleen00636c32017-03-20 13:17:07 -0700784 (char *)pe->unit, (char *)pe->perpkg,
Andi Kleen96284812017-03-20 13:17:10 -0700785 (char *)pe->metric_expr,
Jin Yaoa7f6c8c2019-10-15 10:53:57 +0800786 (char *)pe->metric_name,
787 (char *)pe->deprecated);
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700788 }
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700789}
790
Sukadev Bhattiproluc5de47f2015-06-10 00:25:07 -0700791struct perf_event_attr * __weak
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300792perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
793{
794 return NULL;
795}
796
Jiri Olsa90a86bd2019-03-05 16:25:32 +0100797static int pmu_max_precise(const char *name)
798{
799 char path[PATH_MAX];
800 int max_precise = -1;
801
802 scnprintf(path, PATH_MAX,
803 "bus/event_source/devices/%s/caps/max_precise",
804 name);
805
806 sysfs__read_int(path, &max_precise);
807 return max_precise;
808}
809
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300810static struct perf_pmu *pmu_lookup(const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +0100811{
812 struct perf_pmu *pmu;
813 LIST_HEAD(format);
Zheng Yana6146d52012-06-15 14:31:41 +0800814 LIST_HEAD(aliases);
Jiri Olsacd82a322012-03-15 20:09:17 +0100815 __u32 type;
816
817 /*
818 * The pmu data we store & need consists of the pmu
819 * type value and format definitions. Load both right
820 * now.
821 */
822 if (pmu_format(name, &format))
823 return NULL;
824
Andi Kleen15b22ed2017-01-27 18:03:38 -0800825 /*
826 * Check the type first to avoid unnecessary work.
827 */
828 if (pmu_type(name, &type))
829 return NULL;
830
Jiri Olsa3fded962012-10-10 14:53:16 +0200831 if (pmu_aliases(name, &aliases))
832 return NULL;
833
Jiri Olsacd82a322012-03-15 20:09:17 +0100834 pmu = zalloc(sizeof(*pmu));
835 if (!pmu)
836 return NULL;
837
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800838 pmu->cpus = pmu_cpumask(name);
Ganapatrao Kulkarni54e32dc2017-10-17 00:02:18 +0530839 pmu->name = strdup(name);
840 pmu->type = type;
Mark Rutland66ec11912017-10-06 19:38:22 +0100841 pmu->is_uncore = pmu_is_uncore(name);
Jiri Olsa90a86bd2019-03-05 16:25:32 +0100842 pmu->max_precise = pmu_max_precise(name);
Ganapatrao Kulkarni54e32dc2017-10-17 00:02:18 +0530843 pmu_add_cpu_aliases(&aliases, pmu);
Mark Rutland66ec11912017-10-06 19:38:22 +0100844
Jiri Olsacd82a322012-03-15 20:09:17 +0100845 INIT_LIST_HEAD(&pmu->format);
Zheng Yana6146d52012-06-15 14:31:41 +0800846 INIT_LIST_HEAD(&pmu->aliases);
Jiri Olsacd82a322012-03-15 20:09:17 +0100847 list_splice(&format, &pmu->format);
Zheng Yana6146d52012-06-15 14:31:41 +0800848 list_splice(&aliases, &pmu->aliases);
Robert Richter9bc8f9f2012-06-14 22:38:37 +0200849 list_add_tail(&pmu->list, &pmus);
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300850
851 pmu->default_config = perf_pmu__get_default_config(pmu);
852
Jiri Olsacd82a322012-03-15 20:09:17 +0100853 return pmu;
854}
855
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300856static struct perf_pmu *pmu_find(const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +0100857{
858 struct perf_pmu *pmu;
859
860 list_for_each_entry(pmu, &pmus, list)
861 if (!strcmp(pmu->name, name))
862 return pmu;
863
864 return NULL;
865}
866
Robert Richter50a96672012-08-16 21:10:24 +0200867struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
868{
869 /*
870 * pmu iterator: If pmu is NULL, we start at the begin,
871 * otherwise return the next pmu. Returns NULL on end.
872 */
873 if (!pmu) {
874 pmu_read_sysfs();
875 pmu = list_prepare_entry(pmu, &pmus, list);
876 }
877 list_for_each_entry_continue(pmu, &pmus, list)
878 return pmu;
879 return NULL;
880}
881
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300882struct perf_pmu *perf_pmu__find(const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +0100883{
884 struct perf_pmu *pmu;
885
886 /*
887 * Once PMU is loaded it stays in the list,
888 * so we keep us from multiple reading/parsing
889 * the pmu format definitions.
890 */
891 pmu = pmu_find(name);
892 if (pmu)
893 return pmu;
894
895 return pmu_lookup(name);
896}
897
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300898static struct perf_pmu_format *
Adrian Hunter09ff6072015-07-17 19:33:49 +0300899pmu_find_format(struct list_head *formats, const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +0100900{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300901 struct perf_pmu_format *format;
Jiri Olsacd82a322012-03-15 20:09:17 +0100902
903 list_for_each_entry(format, formats, list)
904 if (!strcmp(format->name, name))
905 return format;
906
907 return NULL;
908}
909
Adrian Hunter09ff6072015-07-17 19:33:49 +0300910__u64 perf_pmu__format_bits(struct list_head *formats, const char *name)
911{
912 struct perf_pmu_format *format = pmu_find_format(formats, name);
913 __u64 bits = 0;
914 int fbit;
915
916 if (!format)
917 return 0;
918
919 for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
920 bits |= 1ULL << fbit;
921
922 return bits;
923}
924
Adrian Huntera1ac7de2019-11-15 14:42:22 +0200925int perf_pmu__format_type(struct list_head *formats, const char *name)
926{
927 struct perf_pmu_format *format = pmu_find_format(formats, name);
928
929 if (!format)
930 return -1;
931
932 return format->value;
933}
934
Jiri Olsacd82a322012-03-15 20:09:17 +0100935/*
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300936 * Sets value based on the format definition (format parameter)
Jiri Olsacd82a322012-03-15 20:09:17 +0100937 * and unformated value (value parameter).
Jiri Olsacd82a322012-03-15 20:09:17 +0100938 */
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300939static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
940 bool zero)
Jiri Olsacd82a322012-03-15 20:09:17 +0100941{
942 unsigned long fbit, vbit;
Jiri Olsacd82a322012-03-15 20:09:17 +0100943
944 for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
945
946 if (!test_bit(fbit, format))
947 continue;
948
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300949 if (value & (1llu << vbit++))
950 *v |= (1llu << fbit);
951 else if (zero)
952 *v &= ~(1llu << fbit);
Jiri Olsacd82a322012-03-15 20:09:17 +0100953 }
Jiri Olsacd82a322012-03-15 20:09:17 +0100954}
955
Adrian Hunter0efe6b62015-07-17 19:33:50 +0300956static __u64 pmu_format_max_value(const unsigned long *format)
957{
Jiri Olsa1b9caa12018-10-03 09:20:46 +0200958 int w;
Adrian Hunter0efe6b62015-07-17 19:33:50 +0300959
Jiri Olsa1b9caa12018-10-03 09:20:46 +0200960 w = bitmap_weight(format, PERF_PMU_FORMAT_BITS);
961 if (!w)
962 return 0;
963 if (w < 64)
964 return (1ULL << w) - 1;
965 return -1;
Adrian Hunter0efe6b62015-07-17 19:33:50 +0300966}
967
Jiri Olsacd82a322012-03-15 20:09:17 +0100968/*
Cody P Schafer688d4df2015-01-07 17:13:50 -0800969 * Term is a string term, and might be a param-term. Try to look up it's value
970 * in the remaining terms.
971 * - We have a term like "base-or-format-term=param-term",
972 * - We need to find the value supplied for "param-term" (with param-term named
973 * in a config string) later on in the term list.
974 */
975static int pmu_resolve_param_term(struct parse_events_term *term,
976 struct list_head *head_terms,
977 __u64 *value)
978{
979 struct parse_events_term *t;
980
981 list_for_each_entry(t, head_terms, list) {
982 if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
983 if (!strcmp(t->config, term->config)) {
984 t->used = true;
985 *value = t->val.num;
986 return 0;
987 }
988 }
989 }
990
Namhyung Kimbb963e12017-02-17 17:17:38 +0900991 if (verbose > 0)
Cody P Schafer688d4df2015-01-07 17:13:50 -0800992 printf("Required parameter '%s' not specified\n", term->config);
993
994 return -1;
995}
996
He Kuangffeb8832015-09-28 03:52:14 +0000997static char *pmu_formats_string(struct list_head *formats)
Jiri Olsae64b0202015-04-22 21:10:21 +0200998{
999 struct perf_pmu_format *format;
Masami Hiramatsu11db4e22016-05-10 14:47:44 +09001000 char *str = NULL;
1001 struct strbuf buf = STRBUF_INIT;
Jiri Olsae64b0202015-04-22 21:10:21 +02001002 unsigned i = 0;
1003
He Kuangffeb8832015-09-28 03:52:14 +00001004 if (!formats)
Jiri Olsae64b0202015-04-22 21:10:21 +02001005 return NULL;
1006
1007 /* sysfs exported terms */
He Kuangffeb8832015-09-28 03:52:14 +00001008 list_for_each_entry(format, formats, list)
Masami Hiramatsu11db4e22016-05-10 14:47:44 +09001009 if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
1010 goto error;
Jiri Olsae64b0202015-04-22 21:10:21 +02001011
He Kuangffeb8832015-09-28 03:52:14 +00001012 str = strbuf_detach(&buf, NULL);
Masami Hiramatsu11db4e22016-05-10 14:47:44 +09001013error:
He Kuangffeb8832015-09-28 03:52:14 +00001014 strbuf_release(&buf);
Jiri Olsae64b0202015-04-22 21:10:21 +02001015
Jiri Olsae64b0202015-04-22 21:10:21 +02001016 return str;
Jiri Olsae64b0202015-04-22 21:10:21 +02001017}
1018
Cody P Schafer688d4df2015-01-07 17:13:50 -08001019/*
Jiri Olsacd82a322012-03-15 20:09:17 +01001020 * Setup one of config[12] attr members based on the
Cody P Schafer88aca8d2014-01-08 08:43:51 -08001021 * user input data - term parameter.
Jiri Olsacd82a322012-03-15 20:09:17 +01001022 */
1023static int pmu_config_term(struct list_head *formats,
1024 struct perf_event_attr *attr,
Adrian Hunterdc0a6202014-07-31 09:00:49 +03001025 struct parse_events_term *term,
Cody P Schafer688d4df2015-01-07 17:13:50 -08001026 struct list_head *head_terms,
Jiri Olsae64b0202015-04-22 21:10:21 +02001027 bool zero, struct parse_events_error *err)
Jiri Olsacd82a322012-03-15 20:09:17 +01001028{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -03001029 struct perf_pmu_format *format;
Jiri Olsacd82a322012-03-15 20:09:17 +01001030 __u64 *vp;
Adrian Hunter0efe6b62015-07-17 19:33:50 +03001031 __u64 val, max_val;
Jiri Olsacd82a322012-03-15 20:09:17 +01001032
1033 /*
Cody P Schafer688d4df2015-01-07 17:13:50 -08001034 * If this is a parameter we've already used for parameterized-eval,
1035 * skip it in normal eval.
1036 */
1037 if (term->used)
1038 return 0;
1039
1040 /*
Jiri Olsacd82a322012-03-15 20:09:17 +01001041 * Hardcoded terms should be already in, so nothing
1042 * to be done for them.
1043 */
1044 if (parse_events__is_hardcoded_term(term))
1045 return 0;
1046
Jiri Olsacd82a322012-03-15 20:09:17 +01001047 format = pmu_find_format(formats, term->config);
Cody P Schafer688d4df2015-01-07 17:13:50 -08001048 if (!format) {
Namhyung Kimbb963e12017-02-17 17:17:38 +09001049 if (verbose > 0)
Cody P Schafer688d4df2015-01-07 17:13:50 -08001050 printf("Invalid event/parameter '%s'\n", term->config);
Jiri Olsae64b0202015-04-22 21:10:21 +02001051 if (err) {
He Kuangffeb8832015-09-28 03:52:14 +00001052 char *pmu_term = pmu_formats_string(formats);
1053
Ian Rogers448d7322019-10-30 15:34:39 -07001054 parse_events__handle_error(err, term->err_term,
1055 strdup("unknown term"),
1056 parse_events_formats_error_string(pmu_term));
He Kuangffeb8832015-09-28 03:52:14 +00001057 free(pmu_term);
Jiri Olsae64b0202015-04-22 21:10:21 +02001058 }
Jiri Olsacd82a322012-03-15 20:09:17 +01001059 return -EINVAL;
Cody P Schafer688d4df2015-01-07 17:13:50 -08001060 }
Jiri Olsacd82a322012-03-15 20:09:17 +01001061
1062 switch (format->value) {
1063 case PERF_PMU_FORMAT_VALUE_CONFIG:
1064 vp = &attr->config;
1065 break;
1066 case PERF_PMU_FORMAT_VALUE_CONFIG1:
1067 vp = &attr->config1;
1068 break;
1069 case PERF_PMU_FORMAT_VALUE_CONFIG2:
1070 vp = &attr->config2;
1071 break;
1072 default:
1073 return -EINVAL;
1074 }
1075
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001076 /*
Cody P Schafer688d4df2015-01-07 17:13:50 -08001077 * Either directly use a numeric term, or try to translate string terms
1078 * using event parameters.
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001079 */
Jiri Olsa99e71382017-02-17 15:00:56 +01001080 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1081 if (term->no_value &&
1082 bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) {
1083 if (err) {
Ian Rogers448d7322019-10-30 15:34:39 -07001084 parse_events__handle_error(err, term->err_val,
1085 strdup("no value assigned for term"),
1086 NULL);
Jiri Olsa99e71382017-02-17 15:00:56 +01001087 }
1088 return -EINVAL;
1089 }
1090
Cody P Schafer688d4df2015-01-07 17:13:50 -08001091 val = term->val.num;
Jiri Olsa99e71382017-02-17 15:00:56 +01001092 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
Cody P Schafer688d4df2015-01-07 17:13:50 -08001093 if (strcmp(term->val.str, "?")) {
Namhyung Kimbb963e12017-02-17 17:17:38 +09001094 if (verbose > 0) {
Cody P Schafer688d4df2015-01-07 17:13:50 -08001095 pr_info("Invalid sysfs entry %s=%s\n",
1096 term->config, term->val.str);
Jiri Olsae64b0202015-04-22 21:10:21 +02001097 }
1098 if (err) {
Ian Rogers448d7322019-10-30 15:34:39 -07001099 parse_events__handle_error(err, term->err_val,
1100 strdup("expected numeric value"),
1101 NULL);
Jiri Olsae64b0202015-04-22 21:10:21 +02001102 }
Cody P Schafer688d4df2015-01-07 17:13:50 -08001103 return -EINVAL;
1104 }
1105
1106 if (pmu_resolve_param_term(term, head_terms, &val))
1107 return -EINVAL;
1108 } else
1109 return -EINVAL;
1110
Adrian Hunter0efe6b62015-07-17 19:33:50 +03001111 max_val = pmu_format_max_value(format->bits);
1112 if (val > max_val) {
1113 if (err) {
Ian Rogers448d7322019-10-30 15:34:39 -07001114 char *err_str;
1115
1116 parse_events__handle_error(err, term->err_val,
1117 asprintf(&err_str,
1118 "value too big for format, maximum is %llu",
1119 (unsigned long long)max_val) < 0
1120 ? strdup("value too big for format")
1121 : err_str,
1122 NULL);
Adrian Hunter0efe6b62015-07-17 19:33:50 +03001123 return -EINVAL;
1124 }
1125 /*
1126 * Assume we don't care if !err, in which case the value will be
1127 * silently truncated.
1128 */
1129 }
1130
Cody P Schafer688d4df2015-01-07 17:13:50 -08001131 pmu_format_value(format->bits, val, vp, zero);
Jiri Olsacd82a322012-03-15 20:09:17 +01001132 return 0;
1133}
1134
Jiri Olsacff7f952012-11-10 01:46:50 +01001135int perf_pmu__config_terms(struct list_head *formats,
1136 struct perf_event_attr *attr,
Adrian Hunterdc0a6202014-07-31 09:00:49 +03001137 struct list_head *head_terms,
Jiri Olsae64b0202015-04-22 21:10:21 +02001138 bool zero, struct parse_events_error *err)
Jiri Olsacd82a322012-03-15 20:09:17 +01001139{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001140 struct parse_events_term *term;
Jiri Olsacd82a322012-03-15 20:09:17 +01001141
Cody P Schafer688d4df2015-01-07 17:13:50 -08001142 list_for_each_entry(term, head_terms, list) {
Jiri Olsae64b0202015-04-22 21:10:21 +02001143 if (pmu_config_term(formats, attr, term, head_terms,
1144 zero, err))
Jiri Olsacd82a322012-03-15 20:09:17 +01001145 return -EINVAL;
Cody P Schafer688d4df2015-01-07 17:13:50 -08001146 }
Jiri Olsacd82a322012-03-15 20:09:17 +01001147
1148 return 0;
1149}
1150
1151/*
1152 * Configures event's 'attr' parameter based on the:
1153 * 1) users input - specified in terms parameter
1154 * 2) pmu format definitions - specified by pmu parameter
1155 */
1156int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
Jiri Olsae64b0202015-04-22 21:10:21 +02001157 struct list_head *head_terms,
1158 struct parse_events_error *err)
Jiri Olsacd82a322012-03-15 20:09:17 +01001159{
Adrian Hunterdc0a6202014-07-31 09:00:49 +03001160 bool zero = !!pmu->default_config;
1161
Jiri Olsacd82a322012-03-15 20:09:17 +01001162 attr->type = pmu->type;
Jiri Olsae64b0202015-04-22 21:10:21 +02001163 return perf_pmu__config_terms(&pmu->format, attr, head_terms,
1164 zero, err);
Jiri Olsacd82a322012-03-15 20:09:17 +01001165}
1166
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -03001167static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
1168 struct parse_events_term *term)
Zheng Yana6146d52012-06-15 14:31:41 +08001169{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -03001170 struct perf_pmu_alias *alias;
Zheng Yana6146d52012-06-15 14:31:41 +08001171 char *name;
1172
1173 if (parse_events__is_hardcoded_term(term))
1174 return NULL;
1175
1176 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1177 if (term->val.num != 1)
1178 return NULL;
1179 if (pmu_find_format(&pmu->format, term->config))
1180 return NULL;
1181 name = term->config;
1182 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1183 if (strcasecmp(term->config, "event"))
1184 return NULL;
1185 name = term->val.str;
1186 } else {
1187 return NULL;
1188 }
1189
1190 list_for_each_entry(alias, &pmu->aliases, list) {
1191 if (!strcasecmp(alias->name, name))
1192 return alias;
1193 }
1194 return NULL;
1195}
1196
Stephane Eranian410136f2013-11-12 17:58:49 +01001197
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001198static int check_info_data(struct perf_pmu_alias *alias,
1199 struct perf_pmu_info *info)
Stephane Eranian410136f2013-11-12 17:58:49 +01001200{
1201 /*
1202 * Only one term in event definition can
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001203 * define unit, scale and snapshot, fail
1204 * if there's more than one.
Stephane Eranian410136f2013-11-12 17:58:49 +01001205 */
Arnaldo Carvalho de Melob30a7d12017-02-15 10:06:20 -03001206 if ((info->unit && alias->unit[0]) ||
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001207 (info->scale && alias->scale) ||
1208 (info->snapshot && alias->snapshot))
Stephane Eranian410136f2013-11-12 17:58:49 +01001209 return -EINVAL;
1210
Arnaldo Carvalho de Melob30a7d12017-02-15 10:06:20 -03001211 if (alias->unit[0])
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001212 info->unit = alias->unit;
Stephane Eranian410136f2013-11-12 17:58:49 +01001213
1214 if (alias->scale)
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001215 info->scale = alias->scale;
1216
1217 if (alias->snapshot)
1218 info->snapshot = alias->snapshot;
Stephane Eranian410136f2013-11-12 17:58:49 +01001219
1220 return 0;
1221}
1222
Zheng Yana6146d52012-06-15 14:31:41 +08001223/*
1224 * Find alias in the terms list and replace it with the terms
1225 * defined for the alias
1226 */
Stephane Eranian410136f2013-11-12 17:58:49 +01001227int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
Matt Fleming46441bd2014-09-24 15:04:06 +01001228 struct perf_pmu_info *info)
Zheng Yana6146d52012-06-15 14:31:41 +08001229{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001230 struct parse_events_term *term, *h;
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -03001231 struct perf_pmu_alias *alias;
Zheng Yana6146d52012-06-15 14:31:41 +08001232 int ret;
1233
Matt Fleming044330c2014-11-21 10:31:12 +01001234 info->per_pkg = false;
1235
Stephane Eranian8a398892014-01-17 16:34:05 +01001236 /*
1237 * Mark unit and scale as not set
1238 * (different from default values, see below)
1239 */
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001240 info->unit = NULL;
1241 info->scale = 0.0;
1242 info->snapshot = false;
Andi Kleen37932c12017-03-20 13:17:08 -07001243 info->metric_expr = NULL;
Andi Kleen96284812017-03-20 13:17:10 -07001244 info->metric_name = NULL;
Stephane Eranian410136f2013-11-12 17:58:49 +01001245
Zheng Yana6146d52012-06-15 14:31:41 +08001246 list_for_each_entry_safe(term, h, head_terms, list) {
1247 alias = pmu_find_alias(pmu, term);
1248 if (!alias)
1249 continue;
1250 ret = pmu_alias_terms(alias, &term->list);
1251 if (ret)
1252 return ret;
Stephane Eranian410136f2013-11-12 17:58:49 +01001253
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001254 ret = check_info_data(alias, info);
Stephane Eranian410136f2013-11-12 17:58:49 +01001255 if (ret)
1256 return ret;
1257
Matt Fleming044330c2014-11-21 10:31:12 +01001258 if (alias->per_pkg)
1259 info->per_pkg = true;
Andi Kleen37932c12017-03-20 13:17:08 -07001260 info->metric_expr = alias->metric_expr;
Andi Kleen96284812017-03-20 13:17:10 -07001261 info->metric_name = alias->metric_name;
Matt Fleming044330c2014-11-21 10:31:12 +01001262
Arnaldo Carvalho de Meloe56fbc92019-07-04 12:13:46 -03001263 list_del_init(&term->list);
Ian Rogers1dc92552019-10-30 15:34:47 -07001264 parse_events_term__delete(term);
Zheng Yana6146d52012-06-15 14:31:41 +08001265 }
Stephane Eranian8a398892014-01-17 16:34:05 +01001266
1267 /*
1268 * if no unit or scale foundin aliases, then
1269 * set defaults as for evsel
1270 * unit cannot left to NULL
1271 */
Matt Fleming46441bd2014-09-24 15:04:06 +01001272 if (info->unit == NULL)
1273 info->unit = "";
Stephane Eranian8a398892014-01-17 16:34:05 +01001274
Matt Fleming46441bd2014-09-24 15:04:06 +01001275 if (info->scale == 0.0)
1276 info->scale = 1.0;
Stephane Eranian8a398892014-01-17 16:34:05 +01001277
Zheng Yana6146d52012-06-15 14:31:41 +08001278 return 0;
1279}
1280
Jiri Olsacd82a322012-03-15 20:09:17 +01001281int perf_pmu__new_format(struct list_head *list, char *name,
1282 int config, unsigned long *bits)
1283{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -03001284 struct perf_pmu_format *format;
Jiri Olsacd82a322012-03-15 20:09:17 +01001285
1286 format = zalloc(sizeof(*format));
1287 if (!format)
1288 return -ENOMEM;
1289
1290 format->name = strdup(name);
1291 format->value = config;
1292 memcpy(format->bits, bits, sizeof(format->bits));
1293
1294 list_add_tail(&format->list, list);
1295 return 0;
1296}
1297
1298void perf_pmu__set_format(unsigned long *bits, long from, long to)
1299{
1300 long b;
1301
1302 if (!to)
1303 to = from;
1304
Sukadev Bhattiprolu15268132013-01-17 09:11:30 -08001305 memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
Jiri Olsacd82a322012-03-15 20:09:17 +01001306 for (b = from; b <= to; b++)
1307 set_bit(b, bits);
1308}
Andi Kleendc098b32013-04-20 11:02:29 -07001309
Cody P Schaferaaea3612015-01-07 17:13:51 -08001310static int sub_non_neg(int a, int b)
1311{
1312 if (b > a)
1313 return 0;
1314 return a - b;
1315}
1316
Andi Kleendc098b32013-04-20 11:02:29 -07001317static char *format_alias(char *buf, int len, struct perf_pmu *pmu,
1318 struct perf_pmu_alias *alias)
1319{
Cody P Schaferaaea3612015-01-07 17:13:51 -08001320 struct parse_events_term *term;
1321 int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name);
1322
1323 list_for_each_entry(term, &alias->terms, list) {
1324 if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
1325 used += snprintf(buf + used, sub_non_neg(len, used),
1326 ",%s=%s", term->config,
1327 term->val.str);
1328 }
1329
1330 if (sub_non_neg(len, used) > 0) {
1331 buf[used] = '/';
1332 used++;
1333 }
1334 if (sub_non_neg(len, used) > 0) {
1335 buf[used] = '\0';
1336 used++;
1337 } else
1338 buf[len - 1] = '\0';
1339
Andi Kleendc098b32013-04-20 11:02:29 -07001340 return buf;
1341}
1342
1343static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu,
1344 struct perf_pmu_alias *alias)
1345{
1346 snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name);
1347 return buf;
1348}
1349
Andi Kleendd5f1032016-09-15 15:24:50 -07001350struct sevent {
Andi Kleen08e60ed2016-09-15 15:24:43 -07001351 char *name;
1352 char *desc;
Andi Kleendd5f1032016-09-15 15:24:50 -07001353 char *topic;
Andi Kleenf2361022017-01-27 18:03:40 -08001354 char *str;
1355 char *pmu;
Andi Kleen7f372a62017-03-20 13:17:09 -07001356 char *metric_expr;
Andi Kleen96284812017-03-20 13:17:10 -07001357 char *metric_name;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001358};
1359
Andi Kleendd5f1032016-09-15 15:24:50 -07001360static int cmp_sevent(const void *a, const void *b)
Andi Kleendc098b32013-04-20 11:02:29 -07001361{
Andi Kleendd5f1032016-09-15 15:24:50 -07001362 const struct sevent *as = a;
1363 const struct sevent *bs = b;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001364
1365 /* Put extra events last */
1366 if (!!as->desc != !!bs->desc)
1367 return !!as->desc - !!bs->desc;
Andi Kleendd5f1032016-09-15 15:24:50 -07001368 if (as->topic && bs->topic) {
1369 int n = strcmp(as->topic, bs->topic);
1370
1371 if (n)
1372 return n;
1373 }
Andi Kleen08e60ed2016-09-15 15:24:43 -07001374 return strcmp(as->name, bs->name);
1375}
1376
1377static void wordwrap(char *s, int start, int max, int corr)
1378{
1379 int column = start;
1380 int n;
1381
1382 while (*s) {
1383 int wlen = strcspn(s, " \t");
1384
1385 if (column + wlen >= max && column > start) {
1386 printf("\n%*s", start, "");
1387 column = start + corr;
1388 }
1389 n = printf("%s%.*s", column > start ? " " : "", wlen, s);
1390 if (n <= 0)
1391 break;
1392 s += wlen;
1393 column += n;
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03001394 s = skip_spaces(s);
Andi Kleen08e60ed2016-09-15 15:24:43 -07001395 }
Andi Kleendc098b32013-04-20 11:02:29 -07001396}
1397
Sukadev Bhattiproluc8d68282016-09-15 15:24:48 -07001398void print_pmu_events(const char *event_glob, bool name_only, bool quiet_flag,
Jin Yaoa7f6c8c2019-10-15 10:53:57 +08001399 bool long_desc, bool details_flag, bool deprecated)
Andi Kleendc098b32013-04-20 11:02:29 -07001400{
1401 struct perf_pmu *pmu;
1402 struct perf_pmu_alias *alias;
1403 char buf[1024];
1404 int printed = 0;
1405 int len, j;
Andi Kleendd5f1032016-09-15 15:24:50 -07001406 struct sevent *aliases;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001407 int numdesc = 0;
Andi Kleen61eb2eb2016-09-15 15:24:44 -07001408 int columns = pager_get_columns();
Andi Kleendd5f1032016-09-15 15:24:50 -07001409 char *topic = NULL;
Andi Kleendc098b32013-04-20 11:02:29 -07001410
1411 pmu = NULL;
1412 len = 0;
Adrian Hunter42634bc2014-10-23 13:45:10 +03001413 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
Andi Kleendc098b32013-04-20 11:02:29 -07001414 list_for_each_entry(alias, &pmu->aliases, list)
1415 len++;
Adrian Hunter42634bc2014-10-23 13:45:10 +03001416 if (pmu->selectable)
1417 len++;
1418 }
Andi Kleendd5f1032016-09-15 15:24:50 -07001419 aliases = zalloc(sizeof(struct sevent) * len);
Andi Kleendc098b32013-04-20 11:02:29 -07001420 if (!aliases)
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001421 goto out_enomem;
Andi Kleendc098b32013-04-20 11:02:29 -07001422 pmu = NULL;
1423 j = 0;
Adrian Hunter42634bc2014-10-23 13:45:10 +03001424 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
Andi Kleendc098b32013-04-20 11:02:29 -07001425 list_for_each_entry(alias, &pmu->aliases, list) {
Andi Kleen08e60ed2016-09-15 15:24:43 -07001426 char *name = alias->desc ? alias->name :
1427 format_alias(buf, sizeof(buf), pmu, alias);
Andi Kleendc098b32013-04-20 11:02:29 -07001428 bool is_cpu = !strcmp(pmu->name, "cpu");
1429
Jin Yaoa7f6c8c2019-10-15 10:53:57 +08001430 if (alias->deprecated && !deprecated)
1431 continue;
1432
Andi Kleendc098b32013-04-20 11:02:29 -07001433 if (event_glob != NULL &&
Andi Kleen38d14f02016-10-19 10:50:01 -07001434 !(strglobmatch_nocase(name, event_glob) ||
1435 (!is_cpu && strglobmatch_nocase(alias->name,
Andi Kleen67bdc352016-10-19 11:45:23 -07001436 event_glob)) ||
1437 (alias->topic &&
1438 strglobmatch_nocase(alias->topic, event_glob))))
Andi Kleendc098b32013-04-20 11:02:29 -07001439 continue;
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001440
Andi Kleen08e60ed2016-09-15 15:24:43 -07001441 if (is_cpu && !name_only && !alias->desc)
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001442 name = format_alias_or(buf, sizeof(buf), pmu, alias);
1443
Andi Kleen08e60ed2016-09-15 15:24:43 -07001444 aliases[j].name = name;
1445 if (is_cpu && !name_only && !alias->desc)
1446 aliases[j].name = format_alias_or(buf,
1447 sizeof(buf),
1448 pmu, alias);
1449 aliases[j].name = strdup(aliases[j].name);
1450 if (!aliases[j].name)
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001451 goto out_enomem;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001452
Sukadev Bhattiproluc8d68282016-09-15 15:24:48 -07001453 aliases[j].desc = long_desc ? alias->long_desc :
1454 alias->desc;
Andi Kleendd5f1032016-09-15 15:24:50 -07001455 aliases[j].topic = alias->topic;
Andi Kleenf2361022017-01-27 18:03:40 -08001456 aliases[j].str = alias->str;
1457 aliases[j].pmu = pmu->name;
Andi Kleen7f372a62017-03-20 13:17:09 -07001458 aliases[j].metric_expr = alias->metric_expr;
Andi Kleen96284812017-03-20 13:17:10 -07001459 aliases[j].metric_name = alias->metric_name;
Andi Kleendc098b32013-04-20 11:02:29 -07001460 j++;
1461 }
Arnaldo Carvalho de Melofa52cea2015-10-02 15:28:16 -03001462 if (pmu->selectable &&
1463 (event_glob == NULL || strglobmatch(pmu->name, event_glob))) {
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001464 char *s;
1465 if (asprintf(&s, "%s//", pmu->name) < 0)
1466 goto out_enomem;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001467 aliases[j].name = s;
Adrian Hunter42634bc2014-10-23 13:45:10 +03001468 j++;
1469 }
1470 }
Andi Kleendc098b32013-04-20 11:02:29 -07001471 len = j;
Andi Kleendd5f1032016-09-15 15:24:50 -07001472 qsort(aliases, len, sizeof(struct sevent), cmp_sevent);
Andi Kleendc098b32013-04-20 11:02:29 -07001473 for (j = 0; j < len; j++) {
Andi Kleen15b22ed2017-01-27 18:03:38 -08001474 /* Skip duplicates */
1475 if (j > 0 && !strcmp(aliases[j].name, aliases[j - 1].name))
1476 continue;
Andi Kleendc098b32013-04-20 11:02:29 -07001477 if (name_only) {
Andi Kleen08e60ed2016-09-15 15:24:43 -07001478 printf("%s ", aliases[j].name);
Andi Kleendc098b32013-04-20 11:02:29 -07001479 continue;
1480 }
Andi Kleen1c5f01f2016-09-15 15:24:45 -07001481 if (aliases[j].desc && !quiet_flag) {
Andi Kleen08e60ed2016-09-15 15:24:43 -07001482 if (numdesc++ == 0)
1483 printf("\n");
Andi Kleendd5f1032016-09-15 15:24:50 -07001484 if (aliases[j].topic && (!topic ||
1485 strcmp(topic, aliases[j].topic))) {
1486 printf("%s%s:\n", topic ? "\n" : "",
1487 aliases[j].topic);
1488 topic = aliases[j].topic;
1489 }
Andi Kleen08e60ed2016-09-15 15:24:43 -07001490 printf(" %-50s\n", aliases[j].name);
1491 printf("%*s", 8, "[");
1492 wordwrap(aliases[j].desc, 8, columns, 0);
1493 printf("]\n");
Andi Kleenbf874fc2017-03-20 13:17:11 -07001494 if (details_flag) {
Andi Kleen7f372a62017-03-20 13:17:09 -07001495 printf("%*s%s/%s/ ", 8, "", aliases[j].pmu, aliases[j].str);
Andi Kleen96284812017-03-20 13:17:10 -07001496 if (aliases[j].metric_name)
1497 printf(" MetricName: %s", aliases[j].metric_name);
Andi Kleen7f372a62017-03-20 13:17:09 -07001498 if (aliases[j].metric_expr)
1499 printf(" MetricExpr: %s", aliases[j].metric_expr);
1500 putchar('\n');
1501 }
Andi Kleen08e60ed2016-09-15 15:24:43 -07001502 } else
1503 printf(" %-50s [Kernel PMU event]\n", aliases[j].name);
Andi Kleendc098b32013-04-20 11:02:29 -07001504 printed++;
1505 }
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03001506 if (printed && pager_in_use())
Andi Kleendc098b32013-04-20 11:02:29 -07001507 printf("\n");
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001508out_free:
1509 for (j = 0; j < len; j++)
Andi Kleen08e60ed2016-09-15 15:24:43 -07001510 zfree(&aliases[j].name);
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001511 zfree(&aliases);
1512 return;
1513
1514out_enomem:
1515 printf("FATAL: not enough memory to print PMU events\n");
1516 if (aliases)
1517 goto out_free;
Andi Kleendc098b32013-04-20 11:02:29 -07001518}
Andi Kleen4cabc3d2013-08-21 16:47:26 -07001519
1520bool pmu_have_event(const char *pname, const char *name)
1521{
1522 struct perf_pmu *pmu;
1523 struct perf_pmu_alias *alias;
1524
1525 pmu = NULL;
1526 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1527 if (strcmp(pname, pmu->name))
1528 continue;
1529 list_for_each_entry(alias, &pmu->aliases, list)
1530 if (!strcmp(alias->name, name))
1531 return true;
1532 }
1533 return false;
1534}
Adrian Hunter7d4bdab2014-07-31 09:00:50 +03001535
1536static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
1537{
Adrian Hunter7d4bdab2014-07-31 09:00:50 +03001538 char path[PATH_MAX];
1539 const char *sysfs;
1540
1541 sysfs = sysfs__mountpoint();
1542 if (!sysfs)
1543 return NULL;
1544
1545 snprintf(path, PATH_MAX,
1546 "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name);
Andi Kleend9664582019-11-20 16:15:11 -08001547 if (!file_available(path))
Adrian Hunter7d4bdab2014-07-31 09:00:50 +03001548 return NULL;
Adrian Hunter7d4bdab2014-07-31 09:00:50 +03001549 return fopen(path, "r");
1550}
1551
1552int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
1553 ...)
1554{
1555 va_list args;
1556 FILE *file;
1557 int ret = EOF;
1558
1559 va_start(args, fmt);
1560 file = perf_pmu__open_file(pmu, name);
1561 if (file) {
1562 ret = vfscanf(file, fmt, args);
1563 fclose(file);
1564 }
1565 va_end(args);
1566 return ret;
1567}