blob: 6b3448f6eb94720e6b0dff295ce8b357f4d52769 [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"
Yan, Zheng7ae92e72012-09-10 15:53:50 +080023#include "cpumap.h"
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -070024#include "header.h"
25#include "pmu-events/pmu-events.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030026#include "string2.h"
Arnaldo Carvalho de Melofa0d9842019-08-30 12:52:25 -030027#include "strbuf.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{
86 struct stat st;
87 char path[PATH_MAX];
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -030088 const char *sysfs = sysfs__mountpoint();
Jiri Olsacd82a322012-03-15 20:09:17 +010089
Jiri Olsacd82a322012-03-15 20:09:17 +010090 if (!sysfs)
91 return -1;
92
93 snprintf(path, PATH_MAX,
Robert Richter50a96672012-08-16 21:10:24 +020094 "%s" EVENT_SOURCE_DEVICE_PATH "%s/format", sysfs, name);
Jiri Olsacd82a322012-03-15 20:09:17 +010095
96 if (stat(path, &st) < 0)
Robert Richter9bc8f9f2012-06-14 22:38:37 +020097 return 0; /* no error if format does not exist */
Jiri Olsacd82a322012-03-15 20:09:17 +010098
Jiri Olsacff7f952012-11-10 01:46:50 +010099 if (perf_pmu__format_parse(path, format))
Jiri Olsacd82a322012-03-15 20:09:17 +0100100 return -1;
101
102 return 0;
103}
104
Andi Kleend02fc6b2017-01-03 07:08:23 -0800105static int convert_scale(const char *scale, char **end, double *sval)
106{
107 char *lc;
108 int ret = 0;
109
110 /*
111 * save current locale
112 */
113 lc = setlocale(LC_NUMERIC, NULL);
114
115 /*
116 * The lc string may be allocated in static storage,
117 * so get a dynamic copy to make it survive setlocale
118 * call below.
119 */
120 lc = strdup(lc);
121 if (!lc) {
122 ret = -ENOMEM;
123 goto out;
124 }
125
126 /*
127 * force to C locale to ensure kernel
128 * scale string is converted correctly.
129 * kernel uses default C locale.
130 */
131 setlocale(LC_NUMERIC, "C");
132
133 *sval = strtod(scale, end);
134
135out:
136 /* restore locale */
137 setlocale(LC_NUMERIC, lc);
138 free(lc);
139 return ret;
140}
141
Stephane Eranian410136f2013-11-12 17:58:49 +0100142static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *name)
143{
144 struct stat st;
145 ssize_t sret;
146 char scale[128];
147 int fd, ret = -1;
148 char path[PATH_MAX];
Stephane Eranian410136f2013-11-12 17:58:49 +0100149
Ben Hutchings11a64a02018-11-11 18:45:24 +0000150 scnprintf(path, PATH_MAX, "%s/%s.scale", dir, name);
Stephane Eranian410136f2013-11-12 17:58:49 +0100151
152 fd = open(path, O_RDONLY);
153 if (fd == -1)
154 return -1;
155
156 if (fstat(fd, &st) < 0)
157 goto error;
158
159 sret = read(fd, scale, sizeof(scale)-1);
160 if (sret < 0)
161 goto error;
162
Madhavan Srinivasan9ecae062015-05-31 11:36:23 +0530163 if (scale[sret - 1] == '\n')
164 scale[sret - 1] = '\0';
165 else
166 scale[sret] = '\0';
167
Andi Kleend02fc6b2017-01-03 07:08:23 -0800168 ret = convert_scale(scale, NULL, &alias->scale);
Stephane Eranian410136f2013-11-12 17:58:49 +0100169error:
170 close(fd);
171 return ret;
172}
173
174static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *name)
175{
176 char path[PATH_MAX];
177 ssize_t sret;
178 int fd;
179
Ben Hutchings11a64a02018-11-11 18:45:24 +0000180 scnprintf(path, PATH_MAX, "%s/%s.unit", dir, name);
Stephane Eranian410136f2013-11-12 17:58:49 +0100181
182 fd = open(path, O_RDONLY);
183 if (fd == -1)
184 return -1;
185
Markus Trippelsdorfd85ce832015-12-14 16:44:40 +0100186 sret = read(fd, alias->unit, UNIT_MAX_LEN);
Stephane Eranian410136f2013-11-12 17:58:49 +0100187 if (sret < 0)
188 goto error;
189
190 close(fd);
191
Madhavan Srinivasan9ecae062015-05-31 11:36:23 +0530192 if (alias->unit[sret - 1] == '\n')
193 alias->unit[sret - 1] = '\0';
194 else
195 alias->unit[sret] = '\0';
Stephane Eranian410136f2013-11-12 17:58:49 +0100196
197 return 0;
198error:
199 close(fd);
200 alias->unit[0] = '\0';
201 return -1;
202}
203
Matt Fleming044330c2014-11-21 10:31:12 +0100204static int
205perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, char *dir, char *name)
206{
207 char path[PATH_MAX];
208 int fd;
209
Ben Hutchings11a64a02018-11-11 18:45:24 +0000210 scnprintf(path, PATH_MAX, "%s/%s.per-pkg", dir, name);
Matt Fleming044330c2014-11-21 10:31:12 +0100211
212 fd = open(path, O_RDONLY);
213 if (fd == -1)
214 return -1;
215
216 close(fd);
217
218 alias->per_pkg = true;
219 return 0;
220}
221
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100222static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias,
223 char *dir, char *name)
224{
225 char path[PATH_MAX];
226 int fd;
227
Ben Hutchings11a64a02018-11-11 18:45:24 +0000228 scnprintf(path, PATH_MAX, "%s/%s.snapshot", dir, name);
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100229
230 fd = open(path, O_RDONLY);
231 if (fd == -1)
232 return -1;
233
234 alias->snapshot = true;
235 close(fd);
236 return 0;
237}
238
Thomas Richter6dde6422018-06-15 12:11:05 +0200239static void perf_pmu_assign_str(char *name, const char *field, char **old_str,
240 char **new_str)
241{
242 if (!*old_str)
243 goto set_new;
244
245 if (*new_str) { /* Have new string, check with old */
246 if (strcasecmp(*old_str, *new_str))
247 pr_debug("alias %s differs in field '%s'\n",
248 name, field);
249 zfree(old_str);
250 } else /* Nothing new --> keep old string */
251 return;
252set_new:
253 *old_str = *new_str;
254 *new_str = NULL;
255}
256
257static void perf_pmu_update_alias(struct perf_pmu_alias *old,
258 struct perf_pmu_alias *newalias)
259{
260 perf_pmu_assign_str(old->name, "desc", &old->desc, &newalias->desc);
261 perf_pmu_assign_str(old->name, "long_desc", &old->long_desc,
262 &newalias->long_desc);
263 perf_pmu_assign_str(old->name, "topic", &old->topic, &newalias->topic);
264 perf_pmu_assign_str(old->name, "metric_expr", &old->metric_expr,
265 &newalias->metric_expr);
266 perf_pmu_assign_str(old->name, "metric_name", &old->metric_name,
267 &newalias->metric_name);
268 perf_pmu_assign_str(old->name, "value", &old->str, &newalias->str);
269 old->scale = newalias->scale;
270 old->per_pkg = newalias->per_pkg;
271 old->snapshot = newalias->snapshot;
272 memcpy(old->unit, newalias->unit, sizeof(old->unit));
273}
274
275/* Delete an alias entry. */
276static void perf_pmu_free_alias(struct perf_pmu_alias *newalias)
277{
278 zfree(&newalias->name);
279 zfree(&newalias->desc);
280 zfree(&newalias->long_desc);
281 zfree(&newalias->topic);
282 zfree(&newalias->str);
283 zfree(&newalias->metric_expr);
284 zfree(&newalias->metric_name);
285 parse_events_terms__purge(&newalias->terms);
286 free(newalias);
287}
288
289/* Merge an alias, search in alias list. If this name is already
290 * present merge both of them to combine all information.
291 */
292static bool perf_pmu_merge_alias(struct perf_pmu_alias *newalias,
293 struct list_head *alist)
294{
295 struct perf_pmu_alias *a;
296
297 list_for_each_entry(a, alist, list) {
298 if (!strcasecmp(newalias->name, a->name)) {
299 perf_pmu_update_alias(a, newalias);
300 perf_pmu_free_alias(newalias);
301 return true;
302 }
303 }
304 return false;
305}
306
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700307static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name,
Andi Kleenfedb2b52017-01-27 18:03:37 -0800308 char *desc, char *val,
309 char *long_desc, char *topic,
Andi Kleen00636c32017-03-20 13:17:07 -0700310 char *unit, char *perpkg,
Andi Kleen96284812017-03-20 13:17:10 -0700311 char *metric_expr,
312 char *metric_name)
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;
Stephane Eranian410136f2013-11-12 17:58:49 +0100329
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700330 ret = parse_events_terms(&alias->terms, val);
Zheng Yana6146d52012-06-15 14:31:41 +0800331 if (ret) {
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700332 pr_err("Cannot parse alias %s: %d\n", val, ret);
Zheng Yana6146d52012-06-15 14:31:41 +0800333 free(alias);
334 return ret;
335 }
336
Thomas Richter0c24d6f2018-06-15 12:11:04 +0200337 /* Scan event and remove leading zeroes, spaces, newlines, some
338 * platforms have terms specified as
339 * event=0x0091 (read from files ../<PMU>/events/<FILE>
340 * and terms specified as event=0x91 (read from JSON files).
341 *
342 * Rebuild string to make alias->str member comparable.
343 */
344 memset(newval, 0, sizeof(newval));
345 ret = 0;
346 list_for_each_entry(term, &alias->terms, list) {
347 if (ret)
348 ret += scnprintf(newval + ret, sizeof(newval) - ret,
349 ",");
350 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM)
351 ret += scnprintf(newval + ret, sizeof(newval) - ret,
352 "%s=%#x", term->config, term->val.num);
353 else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
354 ret += scnprintf(newval + ret, sizeof(newval) - ret,
355 "%s=%s", term->config, term->val.str);
356 }
357
Zheng Yana6146d52012-06-15 14:31:41 +0800358 alias->name = strdup(name);
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700359 if (dir) {
360 /*
361 * load unit name and scale if available
362 */
363 perf_pmu__parse_unit(alias, dir, name);
364 perf_pmu__parse_scale(alias, dir, name);
365 perf_pmu__parse_per_pkg(alias, dir, name);
366 perf_pmu__parse_snapshot(alias, dir, name);
367 }
Stephane Eranian410136f2013-11-12 17:58:49 +0100368
Andi Kleen00636c32017-03-20 13:17:07 -0700369 alias->metric_expr = metric_expr ? strdup(metric_expr) : NULL;
Andi Kleen96284812017-03-20 13:17:10 -0700370 alias->metric_name = metric_name ? strdup(metric_name): NULL;
Andi Kleen08e60ed2016-09-15 15:24:43 -0700371 alias->desc = desc ? strdup(desc) : NULL;
Sukadev Bhattiproluc8d68282016-09-15 15:24:48 -0700372 alias->long_desc = long_desc ? strdup(long_desc) :
373 desc ? strdup(desc) : NULL;
Andi Kleendd5f1032016-09-15 15:24:50 -0700374 alias->topic = topic ? strdup(topic) : NULL;
Andi Kleenfedb2b52017-01-27 18:03:37 -0800375 if (unit) {
376 if (convert_scale(unit, &unit, &alias->scale) < 0)
377 return -1;
378 snprintf(alias->unit, sizeof(alias->unit), "%s", unit);
379 }
380 alias->per_pkg = perpkg && sscanf(perpkg, "%d", &num) == 1 && num == 1;
Thomas Richter0c24d6f2018-06-15 12:11:04 +0200381 alias->str = strdup(newval);
Andi Kleenf2361022017-01-27 18:03:40 -0800382
Thomas Richter6dde6422018-06-15 12:11:05 +0200383 if (!perf_pmu_merge_alias(alias, list))
384 list_add_tail(&alias->list, list);
Stephane Eranian410136f2013-11-12 17:58:49 +0100385
Zheng Yana6146d52012-06-15 14:31:41 +0800386 return 0;
387}
388
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700389static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file)
390{
391 char buf[256];
392 int ret;
393
394 ret = fread(buf, 1, sizeof(buf), file);
395 if (ret == 0)
396 return -EINVAL;
397
398 buf[ret] = 0;
399
Thomas Richterea23ac72018-06-15 12:11:03 +0200400 /* Remove trailing newline from sysfs file */
Arnaldo Carvalho de Melo13c230a2019-06-26 12:13:13 -0300401 strim(buf);
Thomas Richterea23ac72018-06-15 12:11:03 +0200402
Andi Kleenfedb2b52017-01-27 18:03:37 -0800403 return __perf_pmu__new_alias(list, dir, name, NULL, buf, NULL, NULL, NULL,
Andi Kleen96284812017-03-20 13:17:10 -0700404 NULL, NULL, NULL);
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700405}
406
Matt Fleming46441bd2014-09-24 15:04:06 +0100407static inline bool pmu_alias_info_file(char *name)
408{
409 size_t len;
410
411 len = strlen(name);
412 if (len > 5 && !strcmp(name + len - 5, ".unit"))
413 return true;
414 if (len > 6 && !strcmp(name + len - 6, ".scale"))
415 return true;
Matt Fleming044330c2014-11-21 10:31:12 +0100416 if (len > 8 && !strcmp(name + len - 8, ".per-pkg"))
417 return true;
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100418 if (len > 9 && !strcmp(name + len - 9, ".snapshot"))
419 return true;
Matt Fleming46441bd2014-09-24 15:04:06 +0100420
421 return false;
422}
423
Zheng Yana6146d52012-06-15 14:31:41 +0800424/*
425 * Process all the sysfs attributes located under the directory
426 * specified in 'dir' parameter.
427 */
428static int pmu_aliases_parse(char *dir, struct list_head *head)
429{
430 struct dirent *evt_ent;
431 DIR *event_dir;
Zheng Yana6146d52012-06-15 14:31:41 +0800432
433 event_dir = opendir(dir);
434 if (!event_dir)
435 return -EINVAL;
436
Andi Kleen940db6d2016-02-17 14:44:55 -0800437 while ((evt_ent = readdir(event_dir))) {
Zheng Yana6146d52012-06-15 14:31:41 +0800438 char path[PATH_MAX];
439 char *name = evt_ent->d_name;
440 FILE *file;
441
442 if (!strcmp(name, ".") || !strcmp(name, ".."))
443 continue;
444
Stephane Eranian410136f2013-11-12 17:58:49 +0100445 /*
Matt Fleming46441bd2014-09-24 15:04:06 +0100446 * skip info files parsed in perf_pmu__new_alias()
Stephane Eranian410136f2013-11-12 17:58:49 +0100447 */
Matt Fleming46441bd2014-09-24 15:04:06 +0100448 if (pmu_alias_info_file(name))
Stephane Eranian410136f2013-11-12 17:58:49 +0100449 continue;
450
Jiri Olsa77f18152018-03-19 09:29:01 +0100451 scnprintf(path, PATH_MAX, "%s/%s", dir, name);
Zheng Yana6146d52012-06-15 14:31:41 +0800452
Zheng Yana6146d52012-06-15 14:31:41 +0800453 file = fopen(path, "r");
Andi Kleen940db6d2016-02-17 14:44:55 -0800454 if (!file) {
455 pr_debug("Cannot open %s\n", path);
456 continue;
457 }
Stephane Eranian410136f2013-11-12 17:58:49 +0100458
Andi Kleen940db6d2016-02-17 14:44:55 -0800459 if (perf_pmu__new_alias(head, dir, name, file) < 0)
460 pr_debug("Cannot set up %s\n", name);
Zheng Yana6146d52012-06-15 14:31:41 +0800461 fclose(file);
462 }
463
464 closedir(event_dir);
Andi Kleen940db6d2016-02-17 14:44:55 -0800465 return 0;
Zheng Yana6146d52012-06-15 14:31:41 +0800466}
467
468/*
469 * Reading the pmu event aliases definition, which should be located at:
470 * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
471 */
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300472static int pmu_aliases(const char *name, struct list_head *head)
Zheng Yana6146d52012-06-15 14:31:41 +0800473{
474 struct stat st;
475 char path[PATH_MAX];
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -0300476 const char *sysfs = sysfs__mountpoint();
Zheng Yana6146d52012-06-15 14:31:41 +0800477
Zheng Yana6146d52012-06-15 14:31:41 +0800478 if (!sysfs)
479 return -1;
480
481 snprintf(path, PATH_MAX,
482 "%s/bus/event_source/devices/%s/events", sysfs, name);
483
484 if (stat(path, &st) < 0)
Jiri Olsa3fded962012-10-10 14:53:16 +0200485 return 0; /* no error if 'events' does not exist */
Zheng Yana6146d52012-06-15 14:31:41 +0800486
487 if (pmu_aliases_parse(path, head))
488 return -1;
489
490 return 0;
491}
492
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300493static int pmu_alias_terms(struct perf_pmu_alias *alias,
Zheng Yana6146d52012-06-15 14:31:41 +0800494 struct list_head *terms)
495{
Jiri Olsa7c2f8162014-04-16 20:49:02 +0200496 struct parse_events_term *term, *cloned;
Zheng Yana6146d52012-06-15 14:31:41 +0800497 LIST_HEAD(list);
498 int ret;
499
500 list_for_each_entry(term, &alias->terms, list) {
Jiri Olsa7c2f8162014-04-16 20:49:02 +0200501 ret = parse_events_term__clone(&cloned, term);
Zheng Yana6146d52012-06-15 14:31:41 +0800502 if (ret) {
Arnaldo Carvalho de Melo682dc242016-02-12 16:48:00 -0300503 parse_events_terms__purge(&list);
Zheng Yana6146d52012-06-15 14:31:41 +0800504 return ret;
505 }
Andi Kleenc2f1cea2017-10-20 13:27:55 -0700506 /*
507 * Weak terms don't override command line options,
508 * which we don't want for implicit terms in aliases.
509 */
510 cloned->weak = true;
Jiri Olsa7c2f8162014-04-16 20:49:02 +0200511 list_add_tail(&cloned->list, &list);
Zheng Yana6146d52012-06-15 14:31:41 +0800512 }
513 list_splice(&list, terms);
514 return 0;
515}
516
Jiri Olsacd82a322012-03-15 20:09:17 +0100517/*
518 * Reading/parsing the default pmu type value, which should be
519 * located at:
520 * /sys/bus/event_source/devices/<dev>/type as sysfs attribute.
521 */
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300522static int pmu_type(const char *name, __u32 *type)
Jiri Olsacd82a322012-03-15 20:09:17 +0100523{
524 struct stat st;
525 char path[PATH_MAX];
Jiri Olsacd82a322012-03-15 20:09:17 +0100526 FILE *file;
527 int ret = 0;
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -0300528 const char *sysfs = sysfs__mountpoint();
Jiri Olsacd82a322012-03-15 20:09:17 +0100529
Jiri Olsacd82a322012-03-15 20:09:17 +0100530 if (!sysfs)
531 return -1;
532
533 snprintf(path, PATH_MAX,
Robert Richter50a96672012-08-16 21:10:24 +0200534 "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name);
Jiri Olsacd82a322012-03-15 20:09:17 +0100535
536 if (stat(path, &st) < 0)
537 return -1;
538
539 file = fopen(path, "r");
540 if (!file)
541 return -EINVAL;
542
543 if (1 != fscanf(file, "%u", type))
544 ret = -1;
545
546 fclose(file);
547 return ret;
548}
549
Robert Richter50a96672012-08-16 21:10:24 +0200550/* Add all pmus in sysfs to pmu list: */
551static void pmu_read_sysfs(void)
552{
553 char path[PATH_MAX];
Robert Richter50a96672012-08-16 21:10:24 +0200554 DIR *dir;
555 struct dirent *dent;
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -0300556 const char *sysfs = sysfs__mountpoint();
Robert Richter50a96672012-08-16 21:10:24 +0200557
Robert Richter50a96672012-08-16 21:10:24 +0200558 if (!sysfs)
559 return;
560
561 snprintf(path, PATH_MAX,
562 "%s" EVENT_SOURCE_DEVICE_PATH, sysfs);
563
564 dir = opendir(path);
565 if (!dir)
566 return;
567
568 while ((dent = readdir(dir))) {
569 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
570 continue;
571 /* add to static LIST_HEAD(pmus): */
572 perf_pmu__find(dent->d_name);
573 }
574
575 closedir(dir);
576}
577
Jiri Olsaf8548392019-07-21 13:23:49 +0200578static struct perf_cpu_map *__pmu_cpumask(const char *path)
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800579{
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800580 FILE *file;
Jiri Olsaf8548392019-07-21 13:23:49 +0200581 struct perf_cpu_map *cpus;
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800582
583 file = fopen(path, "r");
584 if (!file)
585 return NULL;
586
Jiri Olsa9c3516d2019-07-21 13:24:30 +0200587 cpus = perf_cpu_map__read(file);
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800588 fclose(file);
589 return cpus;
590}
591
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700592/*
Mark Rutland66ec11912017-10-06 19:38:22 +0100593 * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64)
594 * may have a "cpus" file.
595 */
596#define CPUS_TEMPLATE_UNCORE "%s/bus/event_source/devices/%s/cpumask"
597#define CPUS_TEMPLATE_CPU "%s/bus/event_source/devices/%s/cpus"
598
Jiri Olsaf8548392019-07-21 13:23:49 +0200599static struct perf_cpu_map *pmu_cpumask(const char *name)
Mark Rutland66ec11912017-10-06 19:38:22 +0100600{
601 char path[PATH_MAX];
Jiri Olsaf8548392019-07-21 13:23:49 +0200602 struct perf_cpu_map *cpus;
Mark Rutland66ec11912017-10-06 19:38:22 +0100603 const char *sysfs = sysfs__mountpoint();
604 const char *templates[] = {
605 CPUS_TEMPLATE_UNCORE,
606 CPUS_TEMPLATE_CPU,
607 NULL
608 };
609 const char **template;
610
611 if (!sysfs)
612 return NULL;
613
614 for (template = templates; *template; template++) {
615 snprintf(path, PATH_MAX, *template, sysfs, name);
616 cpus = __pmu_cpumask(path);
617 if (cpus)
618 return cpus;
619 }
620
621 return NULL;
622}
623
624static bool pmu_is_uncore(const char *name)
625{
626 char path[PATH_MAX];
Jiri Olsaf8548392019-07-21 13:23:49 +0200627 struct perf_cpu_map *cpus;
Mark Rutland66ec11912017-10-06 19:38:22 +0100628 const char *sysfs = sysfs__mountpoint();
629
630 snprintf(path, PATH_MAX, CPUS_TEMPLATE_UNCORE, sysfs, name);
631 cpus = __pmu_cpumask(path);
Jiri Olsa38f01d82019-07-21 13:24:17 +0200632 perf_cpu_map__put(cpus);
Mark Rutland66ec11912017-10-06 19:38:22 +0100633
634 return !!cpus;
635}
636
637/*
Ganapatrao Kulkarni14b22ae2017-08-24 16:30:58 +0530638 * PMU CORE devices have different name other than cpu in sysfs on some
Kan Liang292c34c2018-04-24 11:20:10 -0700639 * platforms.
640 * Looking for possible sysfs files to identify the arm core device.
Ganapatrao Kulkarni14b22ae2017-08-24 16:30:58 +0530641 */
Kan Liang292c34c2018-04-24 11:20:10 -0700642static int is_arm_pmu_core(const char *name)
Ganapatrao Kulkarni14b22ae2017-08-24 16:30:58 +0530643{
644 struct stat st;
645 char path[PATH_MAX];
646 const char *sysfs = sysfs__mountpoint();
647
648 if (!sysfs)
649 return 0;
650
Ganapatrao Kulkarni14b22ae2017-08-24 16:30:58 +0530651 /* Look for cpu sysfs (specific to arm) */
652 scnprintf(path, PATH_MAX, "%s/bus/event_source/devices/%s/cpus",
653 sysfs, name);
654 if (stat(path, &st) == 0)
655 return 1;
656
657 return 0;
658}
659
Ganapatrao Kulkarni54e32dc2017-10-17 00:02:18 +0530660static char *perf_pmu__getcpuid(struct perf_pmu *pmu)
Andi Kleend77ade92017-08-31 12:40:30 -0700661{
662 char *cpuid;
663 static bool printed;
664
665 cpuid = getenv("PERF_CPUID");
666 if (cpuid)
667 cpuid = strdup(cpuid);
668 if (!cpuid)
Ganapatrao Kulkarni54e32dc2017-10-17 00:02:18 +0530669 cpuid = get_cpuid_str(pmu);
Andi Kleend77ade92017-08-31 12:40:30 -0700670 if (!cpuid)
671 return NULL;
672
673 if (!printed) {
674 pr_debug("Using CPUID %s\n", cpuid);
675 printed = true;
676 }
677 return cpuid;
678}
679
Ganapatrao Kulkarni54e32dc2017-10-17 00:02:18 +0530680struct pmu_events_map *perf_pmu__find_map(struct perf_pmu *pmu)
Andi Kleend77ade92017-08-31 12:40:30 -0700681{
682 struct pmu_events_map *map;
Ganapatrao Kulkarni54e32dc2017-10-17 00:02:18 +0530683 char *cpuid = perf_pmu__getcpuid(pmu);
Andi Kleend77ade92017-08-31 12:40:30 -0700684 int i;
685
Ganapatrao Kulkarnide3d0f12017-10-17 00:02:22 +0530686 /* on some platforms which uses cpus map, cpuid can be NULL for
687 * PMUs other than CORE PMUs.
688 */
689 if (!cpuid)
690 return NULL;
691
Andi Kleend77ade92017-08-31 12:40:30 -0700692 i = 0;
693 for (;;) {
694 map = &pmu_events_map[i++];
695 if (!map->table) {
696 map = NULL;
697 break;
698 }
699
Thomas Richter4cb7d3e2018-02-13 16:14:18 +0100700 if (!strcmp_cpuid_str(map->cpuid, cpuid))
Andi Kleend77ade92017-08-31 12:40:30 -0700701 break;
702 }
703 free(cpuid);
704 return map;
705}
706
John Garry730670b12019-06-28 22:35:49 +0800707static bool pmu_uncore_alias_match(const char *pmu_name, const char *name)
708{
709 char *tmp = NULL, *tok, *str;
710 bool res;
711
712 str = strdup(pmu_name);
713 if (!str)
714 return false;
715
716 /*
717 * uncore alias may be from different PMU with common prefix
718 */
719 tok = strtok_r(str, ",", &tmp);
720 if (strncmp(pmu_name, tok, strlen(tok))) {
721 res = false;
722 goto out;
723 }
724
725 /*
726 * Match more complex aliases where the alias name is a comma-delimited
727 * list of tokens, orderly contained in the matching PMU name.
728 *
729 * Example: For alias "socket,pmuname" and PMU "socketX_pmunameY", we
730 * match "socket" in "socketX_pmunameY" and then "pmuname" in
731 * "pmunameY".
732 */
733 for (; tok; name += strlen(tok), tok = strtok_r(NULL, ",", &tmp)) {
734 name = strstr(name, tok);
735 if (!name) {
736 res = false;
737 goto out;
738 }
739 }
740
741 res = true;
742out:
743 free(str);
744 return res;
745}
746
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700747/*
748 * From the pmu_events_map, find the table of PMU events that corresponds
749 * to the current running CPU. Then, add all PMU events from that table
750 * as aliases.
751 */
Ganapatrao Kulkarni54e32dc2017-10-17 00:02:18 +0530752static void pmu_add_cpu_aliases(struct list_head *head, struct perf_pmu *pmu)
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700753{
754 int i;
755 struct pmu_events_map *map;
Ganapatrao Kulkarni54e32dc2017-10-17 00:02:18 +0530756 const char *name = pmu->name;
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700757
Ganapatrao Kulkarni54e32dc2017-10-17 00:02:18 +0530758 map = perf_pmu__find_map(pmu);
Andi Kleend77ade92017-08-31 12:40:30 -0700759 if (!map)
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700760 return;
761
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700762 /*
763 * Found a matching PMU events table. Create aliases
764 */
765 i = 0;
766 while (1) {
John Garry599ee182019-06-14 22:07:59 +0800767 const char *cpu_name = is_arm_pmu_core(name) ? name : "cpu";
768 struct pmu_event *pe = &map->table[i++];
769 const char *pname = pe->pmu ? pe->pmu : cpu_name;
Andi Kleenfedb2b52017-01-27 18:03:37 -0800770
Andi Kleenb18f3e32017-08-31 12:40:31 -0700771 if (!pe->name) {
772 if (pe->metric_group || pe->metric_name)
773 continue;
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700774 break;
Andi Kleenb18f3e32017-08-31 12:40:31 -0700775 }
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700776
John Garry599ee182019-06-14 22:07:59 +0800777 if (pmu_is_uncore(name) &&
John Garry730670b12019-06-28 22:35:49 +0800778 pmu_uncore_alias_match(pname, name))
John Garry599ee182019-06-14 22:07:59 +0800779 goto new_alias;
Kan Liange94d6b72019-03-15 11:00:14 -0700780
John Garry599ee182019-06-14 22:07:59 +0800781 if (strcmp(pname, name))
782 continue;
Andi Kleenfedb2b52017-01-27 18:03:37 -0800783
Kan Liange94d6b72019-03-15 11:00:14 -0700784new_alias:
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700785 /* need type casts to override 'const' */
786 __perf_pmu__new_alias(head, NULL, (char *)pe->name,
Sukadev Bhattiproluc8d68282016-09-15 15:24:48 -0700787 (char *)pe->desc, (char *)pe->event,
Andi Kleenfedb2b52017-01-27 18:03:37 -0800788 (char *)pe->long_desc, (char *)pe->topic,
Andi Kleen00636c32017-03-20 13:17:07 -0700789 (char *)pe->unit, (char *)pe->perpkg,
Andi Kleen96284812017-03-20 13:17:10 -0700790 (char *)pe->metric_expr,
791 (char *)pe->metric_name);
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700792 }
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700793}
794
Sukadev Bhattiproluc5de47f2015-06-10 00:25:07 -0700795struct perf_event_attr * __weak
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300796perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
797{
798 return NULL;
799}
800
Jiri Olsa90a86bd2019-03-05 16:25:32 +0100801static int pmu_max_precise(const char *name)
802{
803 char path[PATH_MAX];
804 int max_precise = -1;
805
806 scnprintf(path, PATH_MAX,
807 "bus/event_source/devices/%s/caps/max_precise",
808 name);
809
810 sysfs__read_int(path, &max_precise);
811 return max_precise;
812}
813
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300814static struct perf_pmu *pmu_lookup(const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +0100815{
816 struct perf_pmu *pmu;
817 LIST_HEAD(format);
Zheng Yana6146d52012-06-15 14:31:41 +0800818 LIST_HEAD(aliases);
Jiri Olsacd82a322012-03-15 20:09:17 +0100819 __u32 type;
820
821 /*
822 * The pmu data we store & need consists of the pmu
823 * type value and format definitions. Load both right
824 * now.
825 */
826 if (pmu_format(name, &format))
827 return NULL;
828
Andi Kleen15b22ed2017-01-27 18:03:38 -0800829 /*
830 * Check the type first to avoid unnecessary work.
831 */
832 if (pmu_type(name, &type))
833 return NULL;
834
Jiri Olsa3fded962012-10-10 14:53:16 +0200835 if (pmu_aliases(name, &aliases))
836 return NULL;
837
Jiri Olsacd82a322012-03-15 20:09:17 +0100838 pmu = zalloc(sizeof(*pmu));
839 if (!pmu)
840 return NULL;
841
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800842 pmu->cpus = pmu_cpumask(name);
Ganapatrao Kulkarni54e32dc2017-10-17 00:02:18 +0530843 pmu->name = strdup(name);
844 pmu->type = type;
Mark Rutland66ec11912017-10-06 19:38:22 +0100845 pmu->is_uncore = pmu_is_uncore(name);
Jiri Olsa90a86bd2019-03-05 16:25:32 +0100846 pmu->max_precise = pmu_max_precise(name);
Ganapatrao Kulkarni54e32dc2017-10-17 00:02:18 +0530847 pmu_add_cpu_aliases(&aliases, pmu);
Mark Rutland66ec11912017-10-06 19:38:22 +0100848
Jiri Olsacd82a322012-03-15 20:09:17 +0100849 INIT_LIST_HEAD(&pmu->format);
Zheng Yana6146d52012-06-15 14:31:41 +0800850 INIT_LIST_HEAD(&pmu->aliases);
Jiri Olsacd82a322012-03-15 20:09:17 +0100851 list_splice(&format, &pmu->format);
Zheng Yana6146d52012-06-15 14:31:41 +0800852 list_splice(&aliases, &pmu->aliases);
Robert Richter9bc8f9f2012-06-14 22:38:37 +0200853 list_add_tail(&pmu->list, &pmus);
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300854
855 pmu->default_config = perf_pmu__get_default_config(pmu);
856
Jiri Olsacd82a322012-03-15 20:09:17 +0100857 return pmu;
858}
859
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300860static struct perf_pmu *pmu_find(const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +0100861{
862 struct perf_pmu *pmu;
863
864 list_for_each_entry(pmu, &pmus, list)
865 if (!strcmp(pmu->name, name))
866 return pmu;
867
868 return NULL;
869}
870
Robert Richter50a96672012-08-16 21:10:24 +0200871struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
872{
873 /*
874 * pmu iterator: If pmu is NULL, we start at the begin,
875 * otherwise return the next pmu. Returns NULL on end.
876 */
877 if (!pmu) {
878 pmu_read_sysfs();
879 pmu = list_prepare_entry(pmu, &pmus, list);
880 }
881 list_for_each_entry_continue(pmu, &pmus, list)
882 return pmu;
883 return NULL;
884}
885
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300886struct perf_pmu *perf_pmu__find(const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +0100887{
888 struct perf_pmu *pmu;
889
890 /*
891 * Once PMU is loaded it stays in the list,
892 * so we keep us from multiple reading/parsing
893 * the pmu format definitions.
894 */
895 pmu = pmu_find(name);
896 if (pmu)
897 return pmu;
898
899 return pmu_lookup(name);
900}
901
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300902static struct perf_pmu_format *
Adrian Hunter09ff6072015-07-17 19:33:49 +0300903pmu_find_format(struct list_head *formats, const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +0100904{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300905 struct perf_pmu_format *format;
Jiri Olsacd82a322012-03-15 20:09:17 +0100906
907 list_for_each_entry(format, formats, list)
908 if (!strcmp(format->name, name))
909 return format;
910
911 return NULL;
912}
913
Adrian Hunter09ff6072015-07-17 19:33:49 +0300914__u64 perf_pmu__format_bits(struct list_head *formats, const char *name)
915{
916 struct perf_pmu_format *format = pmu_find_format(formats, name);
917 __u64 bits = 0;
918 int fbit;
919
920 if (!format)
921 return 0;
922
923 for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
924 bits |= 1ULL << fbit;
925
926 return bits;
927}
928
Jiri Olsacd82a322012-03-15 20:09:17 +0100929/*
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300930 * Sets value based on the format definition (format parameter)
Jiri Olsacd82a322012-03-15 20:09:17 +0100931 * and unformated value (value parameter).
Jiri Olsacd82a322012-03-15 20:09:17 +0100932 */
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300933static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
934 bool zero)
Jiri Olsacd82a322012-03-15 20:09:17 +0100935{
936 unsigned long fbit, vbit;
Jiri Olsacd82a322012-03-15 20:09:17 +0100937
938 for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
939
940 if (!test_bit(fbit, format))
941 continue;
942
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300943 if (value & (1llu << vbit++))
944 *v |= (1llu << fbit);
945 else if (zero)
946 *v &= ~(1llu << fbit);
Jiri Olsacd82a322012-03-15 20:09:17 +0100947 }
Jiri Olsacd82a322012-03-15 20:09:17 +0100948}
949
Adrian Hunter0efe6b62015-07-17 19:33:50 +0300950static __u64 pmu_format_max_value(const unsigned long *format)
951{
Jiri Olsa1b9caa12018-10-03 09:20:46 +0200952 int w;
Adrian Hunter0efe6b62015-07-17 19:33:50 +0300953
Jiri Olsa1b9caa12018-10-03 09:20:46 +0200954 w = bitmap_weight(format, PERF_PMU_FORMAT_BITS);
955 if (!w)
956 return 0;
957 if (w < 64)
958 return (1ULL << w) - 1;
959 return -1;
Adrian Hunter0efe6b62015-07-17 19:33:50 +0300960}
961
Jiri Olsacd82a322012-03-15 20:09:17 +0100962/*
Cody P Schafer688d4df2015-01-07 17:13:50 -0800963 * Term is a string term, and might be a param-term. Try to look up it's value
964 * in the remaining terms.
965 * - We have a term like "base-or-format-term=param-term",
966 * - We need to find the value supplied for "param-term" (with param-term named
967 * in a config string) later on in the term list.
968 */
969static int pmu_resolve_param_term(struct parse_events_term *term,
970 struct list_head *head_terms,
971 __u64 *value)
972{
973 struct parse_events_term *t;
974
975 list_for_each_entry(t, head_terms, list) {
976 if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
977 if (!strcmp(t->config, term->config)) {
978 t->used = true;
979 *value = t->val.num;
980 return 0;
981 }
982 }
983 }
984
Namhyung Kimbb963e12017-02-17 17:17:38 +0900985 if (verbose > 0)
Cody P Schafer688d4df2015-01-07 17:13:50 -0800986 printf("Required parameter '%s' not specified\n", term->config);
987
988 return -1;
989}
990
He Kuangffeb8832015-09-28 03:52:14 +0000991static char *pmu_formats_string(struct list_head *formats)
Jiri Olsae64b0202015-04-22 21:10:21 +0200992{
993 struct perf_pmu_format *format;
Masami Hiramatsu11db4e22016-05-10 14:47:44 +0900994 char *str = NULL;
995 struct strbuf buf = STRBUF_INIT;
Jiri Olsae64b0202015-04-22 21:10:21 +0200996 unsigned i = 0;
997
He Kuangffeb8832015-09-28 03:52:14 +0000998 if (!formats)
Jiri Olsae64b0202015-04-22 21:10:21 +0200999 return NULL;
1000
1001 /* sysfs exported terms */
He Kuangffeb8832015-09-28 03:52:14 +00001002 list_for_each_entry(format, formats, list)
Masami Hiramatsu11db4e22016-05-10 14:47:44 +09001003 if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
1004 goto error;
Jiri Olsae64b0202015-04-22 21:10:21 +02001005
He Kuangffeb8832015-09-28 03:52:14 +00001006 str = strbuf_detach(&buf, NULL);
Masami Hiramatsu11db4e22016-05-10 14:47:44 +09001007error:
He Kuangffeb8832015-09-28 03:52:14 +00001008 strbuf_release(&buf);
Jiri Olsae64b0202015-04-22 21:10:21 +02001009
Jiri Olsae64b0202015-04-22 21:10:21 +02001010 return str;
Jiri Olsae64b0202015-04-22 21:10:21 +02001011}
1012
Cody P Schafer688d4df2015-01-07 17:13:50 -08001013/*
Jiri Olsacd82a322012-03-15 20:09:17 +01001014 * Setup one of config[12] attr members based on the
Cody P Schafer88aca8d2014-01-08 08:43:51 -08001015 * user input data - term parameter.
Jiri Olsacd82a322012-03-15 20:09:17 +01001016 */
1017static int pmu_config_term(struct list_head *formats,
1018 struct perf_event_attr *attr,
Adrian Hunterdc0a6202014-07-31 09:00:49 +03001019 struct parse_events_term *term,
Cody P Schafer688d4df2015-01-07 17:13:50 -08001020 struct list_head *head_terms,
Jiri Olsae64b0202015-04-22 21:10:21 +02001021 bool zero, struct parse_events_error *err)
Jiri Olsacd82a322012-03-15 20:09:17 +01001022{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -03001023 struct perf_pmu_format *format;
Jiri Olsacd82a322012-03-15 20:09:17 +01001024 __u64 *vp;
Adrian Hunter0efe6b62015-07-17 19:33:50 +03001025 __u64 val, max_val;
Jiri Olsacd82a322012-03-15 20:09:17 +01001026
1027 /*
Cody P Schafer688d4df2015-01-07 17:13:50 -08001028 * If this is a parameter we've already used for parameterized-eval,
1029 * skip it in normal eval.
1030 */
1031 if (term->used)
1032 return 0;
1033
1034 /*
Jiri Olsacd82a322012-03-15 20:09:17 +01001035 * Hardcoded terms should be already in, so nothing
1036 * to be done for them.
1037 */
1038 if (parse_events__is_hardcoded_term(term))
1039 return 0;
1040
Jiri Olsacd82a322012-03-15 20:09:17 +01001041 format = pmu_find_format(formats, term->config);
Cody P Schafer688d4df2015-01-07 17:13:50 -08001042 if (!format) {
Namhyung Kimbb963e12017-02-17 17:17:38 +09001043 if (verbose > 0)
Cody P Schafer688d4df2015-01-07 17:13:50 -08001044 printf("Invalid event/parameter '%s'\n", term->config);
Jiri Olsae64b0202015-04-22 21:10:21 +02001045 if (err) {
He Kuangffeb8832015-09-28 03:52:14 +00001046 char *pmu_term = pmu_formats_string(formats);
1047
Jiri Olsae64b0202015-04-22 21:10:21 +02001048 err->idx = term->err_term;
1049 err->str = strdup("unknown term");
He Kuangffeb8832015-09-28 03:52:14 +00001050 err->help = parse_events_formats_error_string(pmu_term);
1051 free(pmu_term);
Jiri Olsae64b0202015-04-22 21:10:21 +02001052 }
Jiri Olsacd82a322012-03-15 20:09:17 +01001053 return -EINVAL;
Cody P Schafer688d4df2015-01-07 17:13:50 -08001054 }
Jiri Olsacd82a322012-03-15 20:09:17 +01001055
1056 switch (format->value) {
1057 case PERF_PMU_FORMAT_VALUE_CONFIG:
1058 vp = &attr->config;
1059 break;
1060 case PERF_PMU_FORMAT_VALUE_CONFIG1:
1061 vp = &attr->config1;
1062 break;
1063 case PERF_PMU_FORMAT_VALUE_CONFIG2:
1064 vp = &attr->config2;
1065 break;
1066 default:
1067 return -EINVAL;
1068 }
1069
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001070 /*
Cody P Schafer688d4df2015-01-07 17:13:50 -08001071 * Either directly use a numeric term, or try to translate string terms
1072 * using event parameters.
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001073 */
Jiri Olsa99e71382017-02-17 15:00:56 +01001074 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1075 if (term->no_value &&
1076 bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) {
1077 if (err) {
1078 err->idx = term->err_val;
1079 err->str = strdup("no value assigned for term");
1080 }
1081 return -EINVAL;
1082 }
1083
Cody P Schafer688d4df2015-01-07 17:13:50 -08001084 val = term->val.num;
Jiri Olsa99e71382017-02-17 15:00:56 +01001085 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
Cody P Schafer688d4df2015-01-07 17:13:50 -08001086 if (strcmp(term->val.str, "?")) {
Namhyung Kimbb963e12017-02-17 17:17:38 +09001087 if (verbose > 0) {
Cody P Schafer688d4df2015-01-07 17:13:50 -08001088 pr_info("Invalid sysfs entry %s=%s\n",
1089 term->config, term->val.str);
Jiri Olsae64b0202015-04-22 21:10:21 +02001090 }
1091 if (err) {
1092 err->idx = term->err_val;
1093 err->str = strdup("expected numeric value");
1094 }
Cody P Schafer688d4df2015-01-07 17:13:50 -08001095 return -EINVAL;
1096 }
1097
1098 if (pmu_resolve_param_term(term, head_terms, &val))
1099 return -EINVAL;
1100 } else
1101 return -EINVAL;
1102
Adrian Hunter0efe6b62015-07-17 19:33:50 +03001103 max_val = pmu_format_max_value(format->bits);
1104 if (val > max_val) {
1105 if (err) {
1106 err->idx = term->err_val;
1107 if (asprintf(&err->str,
1108 "value too big for format, maximum is %llu",
1109 (unsigned long long)max_val) < 0)
1110 err->str = strdup("value too big for format");
1111 return -EINVAL;
1112 }
1113 /*
1114 * Assume we don't care if !err, in which case the value will be
1115 * silently truncated.
1116 */
1117 }
1118
Cody P Schafer688d4df2015-01-07 17:13:50 -08001119 pmu_format_value(format->bits, val, vp, zero);
Jiri Olsacd82a322012-03-15 20:09:17 +01001120 return 0;
1121}
1122
Jiri Olsacff7f952012-11-10 01:46:50 +01001123int perf_pmu__config_terms(struct list_head *formats,
1124 struct perf_event_attr *attr,
Adrian Hunterdc0a6202014-07-31 09:00:49 +03001125 struct list_head *head_terms,
Jiri Olsae64b0202015-04-22 21:10:21 +02001126 bool zero, struct parse_events_error *err)
Jiri Olsacd82a322012-03-15 20:09:17 +01001127{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001128 struct parse_events_term *term;
Jiri Olsacd82a322012-03-15 20:09:17 +01001129
Cody P Schafer688d4df2015-01-07 17:13:50 -08001130 list_for_each_entry(term, head_terms, list) {
Jiri Olsae64b0202015-04-22 21:10:21 +02001131 if (pmu_config_term(formats, attr, term, head_terms,
1132 zero, err))
Jiri Olsacd82a322012-03-15 20:09:17 +01001133 return -EINVAL;
Cody P Schafer688d4df2015-01-07 17:13:50 -08001134 }
Jiri Olsacd82a322012-03-15 20:09:17 +01001135
1136 return 0;
1137}
1138
1139/*
1140 * Configures event's 'attr' parameter based on the:
1141 * 1) users input - specified in terms parameter
1142 * 2) pmu format definitions - specified by pmu parameter
1143 */
1144int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
Jiri Olsae64b0202015-04-22 21:10:21 +02001145 struct list_head *head_terms,
1146 struct parse_events_error *err)
Jiri Olsacd82a322012-03-15 20:09:17 +01001147{
Adrian Hunterdc0a6202014-07-31 09:00:49 +03001148 bool zero = !!pmu->default_config;
1149
Jiri Olsacd82a322012-03-15 20:09:17 +01001150 attr->type = pmu->type;
Jiri Olsae64b0202015-04-22 21:10:21 +02001151 return perf_pmu__config_terms(&pmu->format, attr, head_terms,
1152 zero, err);
Jiri Olsacd82a322012-03-15 20:09:17 +01001153}
1154
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -03001155static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
1156 struct parse_events_term *term)
Zheng Yana6146d52012-06-15 14:31:41 +08001157{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -03001158 struct perf_pmu_alias *alias;
Zheng Yana6146d52012-06-15 14:31:41 +08001159 char *name;
1160
1161 if (parse_events__is_hardcoded_term(term))
1162 return NULL;
1163
1164 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1165 if (term->val.num != 1)
1166 return NULL;
1167 if (pmu_find_format(&pmu->format, term->config))
1168 return NULL;
1169 name = term->config;
1170 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1171 if (strcasecmp(term->config, "event"))
1172 return NULL;
1173 name = term->val.str;
1174 } else {
1175 return NULL;
1176 }
1177
1178 list_for_each_entry(alias, &pmu->aliases, list) {
1179 if (!strcasecmp(alias->name, name))
1180 return alias;
1181 }
1182 return NULL;
1183}
1184
Stephane Eranian410136f2013-11-12 17:58:49 +01001185
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001186static int check_info_data(struct perf_pmu_alias *alias,
1187 struct perf_pmu_info *info)
Stephane Eranian410136f2013-11-12 17:58:49 +01001188{
1189 /*
1190 * Only one term in event definition can
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001191 * define unit, scale and snapshot, fail
1192 * if there's more than one.
Stephane Eranian410136f2013-11-12 17:58:49 +01001193 */
Arnaldo Carvalho de Melob30a7d12017-02-15 10:06:20 -03001194 if ((info->unit && alias->unit[0]) ||
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001195 (info->scale && alias->scale) ||
1196 (info->snapshot && alias->snapshot))
Stephane Eranian410136f2013-11-12 17:58:49 +01001197 return -EINVAL;
1198
Arnaldo Carvalho de Melob30a7d12017-02-15 10:06:20 -03001199 if (alias->unit[0])
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001200 info->unit = alias->unit;
Stephane Eranian410136f2013-11-12 17:58:49 +01001201
1202 if (alias->scale)
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001203 info->scale = alias->scale;
1204
1205 if (alias->snapshot)
1206 info->snapshot = alias->snapshot;
Stephane Eranian410136f2013-11-12 17:58:49 +01001207
1208 return 0;
1209}
1210
Zheng Yana6146d52012-06-15 14:31:41 +08001211/*
1212 * Find alias in the terms list and replace it with the terms
1213 * defined for the alias
1214 */
Stephane Eranian410136f2013-11-12 17:58:49 +01001215int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
Matt Fleming46441bd2014-09-24 15:04:06 +01001216 struct perf_pmu_info *info)
Zheng Yana6146d52012-06-15 14:31:41 +08001217{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001218 struct parse_events_term *term, *h;
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -03001219 struct perf_pmu_alias *alias;
Zheng Yana6146d52012-06-15 14:31:41 +08001220 int ret;
1221
Matt Fleming044330c2014-11-21 10:31:12 +01001222 info->per_pkg = false;
1223
Stephane Eranian8a398892014-01-17 16:34:05 +01001224 /*
1225 * Mark unit and scale as not set
1226 * (different from default values, see below)
1227 */
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001228 info->unit = NULL;
1229 info->scale = 0.0;
1230 info->snapshot = false;
Andi Kleen37932c12017-03-20 13:17:08 -07001231 info->metric_expr = NULL;
Andi Kleen96284812017-03-20 13:17:10 -07001232 info->metric_name = NULL;
Stephane Eranian410136f2013-11-12 17:58:49 +01001233
Zheng Yana6146d52012-06-15 14:31:41 +08001234 list_for_each_entry_safe(term, h, head_terms, list) {
1235 alias = pmu_find_alias(pmu, term);
1236 if (!alias)
1237 continue;
1238 ret = pmu_alias_terms(alias, &term->list);
1239 if (ret)
1240 return ret;
Stephane Eranian410136f2013-11-12 17:58:49 +01001241
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001242 ret = check_info_data(alias, info);
Stephane Eranian410136f2013-11-12 17:58:49 +01001243 if (ret)
1244 return ret;
1245
Matt Fleming044330c2014-11-21 10:31:12 +01001246 if (alias->per_pkg)
1247 info->per_pkg = true;
Andi Kleen37932c12017-03-20 13:17:08 -07001248 info->metric_expr = alias->metric_expr;
Andi Kleen96284812017-03-20 13:17:10 -07001249 info->metric_name = alias->metric_name;
Matt Fleming044330c2014-11-21 10:31:12 +01001250
Arnaldo Carvalho de Meloe56fbc92019-07-04 12:13:46 -03001251 list_del_init(&term->list);
Zheng Yana6146d52012-06-15 14:31:41 +08001252 free(term);
1253 }
Stephane Eranian8a398892014-01-17 16:34:05 +01001254
1255 /*
1256 * if no unit or scale foundin aliases, then
1257 * set defaults as for evsel
1258 * unit cannot left to NULL
1259 */
Matt Fleming46441bd2014-09-24 15:04:06 +01001260 if (info->unit == NULL)
1261 info->unit = "";
Stephane Eranian8a398892014-01-17 16:34:05 +01001262
Matt Fleming46441bd2014-09-24 15:04:06 +01001263 if (info->scale == 0.0)
1264 info->scale = 1.0;
Stephane Eranian8a398892014-01-17 16:34:05 +01001265
Zheng Yana6146d52012-06-15 14:31:41 +08001266 return 0;
1267}
1268
Jiri Olsacd82a322012-03-15 20:09:17 +01001269int perf_pmu__new_format(struct list_head *list, char *name,
1270 int config, unsigned long *bits)
1271{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -03001272 struct perf_pmu_format *format;
Jiri Olsacd82a322012-03-15 20:09:17 +01001273
1274 format = zalloc(sizeof(*format));
1275 if (!format)
1276 return -ENOMEM;
1277
1278 format->name = strdup(name);
1279 format->value = config;
1280 memcpy(format->bits, bits, sizeof(format->bits));
1281
1282 list_add_tail(&format->list, list);
1283 return 0;
1284}
1285
1286void perf_pmu__set_format(unsigned long *bits, long from, long to)
1287{
1288 long b;
1289
1290 if (!to)
1291 to = from;
1292
Sukadev Bhattiprolu15268132013-01-17 09:11:30 -08001293 memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
Jiri Olsacd82a322012-03-15 20:09:17 +01001294 for (b = from; b <= to; b++)
1295 set_bit(b, bits);
1296}
Andi Kleendc098b32013-04-20 11:02:29 -07001297
Cody P Schaferaaea3612015-01-07 17:13:51 -08001298static int sub_non_neg(int a, int b)
1299{
1300 if (b > a)
1301 return 0;
1302 return a - b;
1303}
1304
Andi Kleendc098b32013-04-20 11:02:29 -07001305static char *format_alias(char *buf, int len, struct perf_pmu *pmu,
1306 struct perf_pmu_alias *alias)
1307{
Cody P Schaferaaea3612015-01-07 17:13:51 -08001308 struct parse_events_term *term;
1309 int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name);
1310
1311 list_for_each_entry(term, &alias->terms, list) {
1312 if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
1313 used += snprintf(buf + used, sub_non_neg(len, used),
1314 ",%s=%s", term->config,
1315 term->val.str);
1316 }
1317
1318 if (sub_non_neg(len, used) > 0) {
1319 buf[used] = '/';
1320 used++;
1321 }
1322 if (sub_non_neg(len, used) > 0) {
1323 buf[used] = '\0';
1324 used++;
1325 } else
1326 buf[len - 1] = '\0';
1327
Andi Kleendc098b32013-04-20 11:02:29 -07001328 return buf;
1329}
1330
1331static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu,
1332 struct perf_pmu_alias *alias)
1333{
1334 snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name);
1335 return buf;
1336}
1337
Andi Kleendd5f1032016-09-15 15:24:50 -07001338struct sevent {
Andi Kleen08e60ed2016-09-15 15:24:43 -07001339 char *name;
1340 char *desc;
Andi Kleendd5f1032016-09-15 15:24:50 -07001341 char *topic;
Andi Kleenf2361022017-01-27 18:03:40 -08001342 char *str;
1343 char *pmu;
Andi Kleen7f372a62017-03-20 13:17:09 -07001344 char *metric_expr;
Andi Kleen96284812017-03-20 13:17:10 -07001345 char *metric_name;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001346};
1347
Andi Kleendd5f1032016-09-15 15:24:50 -07001348static int cmp_sevent(const void *a, const void *b)
Andi Kleendc098b32013-04-20 11:02:29 -07001349{
Andi Kleendd5f1032016-09-15 15:24:50 -07001350 const struct sevent *as = a;
1351 const struct sevent *bs = b;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001352
1353 /* Put extra events last */
1354 if (!!as->desc != !!bs->desc)
1355 return !!as->desc - !!bs->desc;
Andi Kleendd5f1032016-09-15 15:24:50 -07001356 if (as->topic && bs->topic) {
1357 int n = strcmp(as->topic, bs->topic);
1358
1359 if (n)
1360 return n;
1361 }
Andi Kleen08e60ed2016-09-15 15:24:43 -07001362 return strcmp(as->name, bs->name);
1363}
1364
1365static void wordwrap(char *s, int start, int max, int corr)
1366{
1367 int column = start;
1368 int n;
1369
1370 while (*s) {
1371 int wlen = strcspn(s, " \t");
1372
1373 if (column + wlen >= max && column > start) {
1374 printf("\n%*s", start, "");
1375 column = start + corr;
1376 }
1377 n = printf("%s%.*s", column > start ? " " : "", wlen, s);
1378 if (n <= 0)
1379 break;
1380 s += wlen;
1381 column += n;
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03001382 s = skip_spaces(s);
Andi Kleen08e60ed2016-09-15 15:24:43 -07001383 }
Andi Kleendc098b32013-04-20 11:02:29 -07001384}
1385
Sukadev Bhattiproluc8d68282016-09-15 15:24:48 -07001386void print_pmu_events(const char *event_glob, bool name_only, bool quiet_flag,
Andi Kleenbf874fc2017-03-20 13:17:11 -07001387 bool long_desc, bool details_flag)
Andi Kleendc098b32013-04-20 11:02:29 -07001388{
1389 struct perf_pmu *pmu;
1390 struct perf_pmu_alias *alias;
1391 char buf[1024];
1392 int printed = 0;
1393 int len, j;
Andi Kleendd5f1032016-09-15 15:24:50 -07001394 struct sevent *aliases;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001395 int numdesc = 0;
Andi Kleen61eb2eb2016-09-15 15:24:44 -07001396 int columns = pager_get_columns();
Andi Kleendd5f1032016-09-15 15:24:50 -07001397 char *topic = NULL;
Andi Kleendc098b32013-04-20 11:02:29 -07001398
1399 pmu = NULL;
1400 len = 0;
Adrian Hunter42634bc2014-10-23 13:45:10 +03001401 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
Andi Kleendc098b32013-04-20 11:02:29 -07001402 list_for_each_entry(alias, &pmu->aliases, list)
1403 len++;
Adrian Hunter42634bc2014-10-23 13:45:10 +03001404 if (pmu->selectable)
1405 len++;
1406 }
Andi Kleendd5f1032016-09-15 15:24:50 -07001407 aliases = zalloc(sizeof(struct sevent) * len);
Andi Kleendc098b32013-04-20 11:02:29 -07001408 if (!aliases)
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001409 goto out_enomem;
Andi Kleendc098b32013-04-20 11:02:29 -07001410 pmu = NULL;
1411 j = 0;
Adrian Hunter42634bc2014-10-23 13:45:10 +03001412 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
Andi Kleendc098b32013-04-20 11:02:29 -07001413 list_for_each_entry(alias, &pmu->aliases, list) {
Andi Kleen08e60ed2016-09-15 15:24:43 -07001414 char *name = alias->desc ? alias->name :
1415 format_alias(buf, sizeof(buf), pmu, alias);
Andi Kleendc098b32013-04-20 11:02:29 -07001416 bool is_cpu = !strcmp(pmu->name, "cpu");
1417
1418 if (event_glob != NULL &&
Andi Kleen38d14f02016-10-19 10:50:01 -07001419 !(strglobmatch_nocase(name, event_glob) ||
1420 (!is_cpu && strglobmatch_nocase(alias->name,
Andi Kleen67bdc352016-10-19 11:45:23 -07001421 event_glob)) ||
1422 (alias->topic &&
1423 strglobmatch_nocase(alias->topic, event_glob))))
Andi Kleendc098b32013-04-20 11:02:29 -07001424 continue;
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001425
Andi Kleen08e60ed2016-09-15 15:24:43 -07001426 if (is_cpu && !name_only && !alias->desc)
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001427 name = format_alias_or(buf, sizeof(buf), pmu, alias);
1428
Andi Kleen08e60ed2016-09-15 15:24:43 -07001429 aliases[j].name = name;
1430 if (is_cpu && !name_only && !alias->desc)
1431 aliases[j].name = format_alias_or(buf,
1432 sizeof(buf),
1433 pmu, alias);
1434 aliases[j].name = strdup(aliases[j].name);
1435 if (!aliases[j].name)
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001436 goto out_enomem;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001437
Sukadev Bhattiproluc8d68282016-09-15 15:24:48 -07001438 aliases[j].desc = long_desc ? alias->long_desc :
1439 alias->desc;
Andi Kleendd5f1032016-09-15 15:24:50 -07001440 aliases[j].topic = alias->topic;
Andi Kleenf2361022017-01-27 18:03:40 -08001441 aliases[j].str = alias->str;
1442 aliases[j].pmu = pmu->name;
Andi Kleen7f372a62017-03-20 13:17:09 -07001443 aliases[j].metric_expr = alias->metric_expr;
Andi Kleen96284812017-03-20 13:17:10 -07001444 aliases[j].metric_name = alias->metric_name;
Andi Kleendc098b32013-04-20 11:02:29 -07001445 j++;
1446 }
Arnaldo Carvalho de Melofa52cea2015-10-02 15:28:16 -03001447 if (pmu->selectable &&
1448 (event_glob == NULL || strglobmatch(pmu->name, event_glob))) {
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001449 char *s;
1450 if (asprintf(&s, "%s//", pmu->name) < 0)
1451 goto out_enomem;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001452 aliases[j].name = s;
Adrian Hunter42634bc2014-10-23 13:45:10 +03001453 j++;
1454 }
1455 }
Andi Kleendc098b32013-04-20 11:02:29 -07001456 len = j;
Andi Kleendd5f1032016-09-15 15:24:50 -07001457 qsort(aliases, len, sizeof(struct sevent), cmp_sevent);
Andi Kleendc098b32013-04-20 11:02:29 -07001458 for (j = 0; j < len; j++) {
Andi Kleen15b22ed2017-01-27 18:03:38 -08001459 /* Skip duplicates */
1460 if (j > 0 && !strcmp(aliases[j].name, aliases[j - 1].name))
1461 continue;
Andi Kleendc098b32013-04-20 11:02:29 -07001462 if (name_only) {
Andi Kleen08e60ed2016-09-15 15:24:43 -07001463 printf("%s ", aliases[j].name);
Andi Kleendc098b32013-04-20 11:02:29 -07001464 continue;
1465 }
Andi Kleen1c5f01f2016-09-15 15:24:45 -07001466 if (aliases[j].desc && !quiet_flag) {
Andi Kleen08e60ed2016-09-15 15:24:43 -07001467 if (numdesc++ == 0)
1468 printf("\n");
Andi Kleendd5f1032016-09-15 15:24:50 -07001469 if (aliases[j].topic && (!topic ||
1470 strcmp(topic, aliases[j].topic))) {
1471 printf("%s%s:\n", topic ? "\n" : "",
1472 aliases[j].topic);
1473 topic = aliases[j].topic;
1474 }
Andi Kleen08e60ed2016-09-15 15:24:43 -07001475 printf(" %-50s\n", aliases[j].name);
1476 printf("%*s", 8, "[");
1477 wordwrap(aliases[j].desc, 8, columns, 0);
1478 printf("]\n");
Andi Kleenbf874fc2017-03-20 13:17:11 -07001479 if (details_flag) {
Andi Kleen7f372a62017-03-20 13:17:09 -07001480 printf("%*s%s/%s/ ", 8, "", aliases[j].pmu, aliases[j].str);
Andi Kleen96284812017-03-20 13:17:10 -07001481 if (aliases[j].metric_name)
1482 printf(" MetricName: %s", aliases[j].metric_name);
Andi Kleen7f372a62017-03-20 13:17:09 -07001483 if (aliases[j].metric_expr)
1484 printf(" MetricExpr: %s", aliases[j].metric_expr);
1485 putchar('\n');
1486 }
Andi Kleen08e60ed2016-09-15 15:24:43 -07001487 } else
1488 printf(" %-50s [Kernel PMU event]\n", aliases[j].name);
Andi Kleendc098b32013-04-20 11:02:29 -07001489 printed++;
1490 }
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03001491 if (printed && pager_in_use())
Andi Kleendc098b32013-04-20 11:02:29 -07001492 printf("\n");
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001493out_free:
1494 for (j = 0; j < len; j++)
Andi Kleen08e60ed2016-09-15 15:24:43 -07001495 zfree(&aliases[j].name);
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001496 zfree(&aliases);
1497 return;
1498
1499out_enomem:
1500 printf("FATAL: not enough memory to print PMU events\n");
1501 if (aliases)
1502 goto out_free;
Andi Kleendc098b32013-04-20 11:02:29 -07001503}
Andi Kleen4cabc3d2013-08-21 16:47:26 -07001504
1505bool pmu_have_event(const char *pname, const char *name)
1506{
1507 struct perf_pmu *pmu;
1508 struct perf_pmu_alias *alias;
1509
1510 pmu = NULL;
1511 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1512 if (strcmp(pname, pmu->name))
1513 continue;
1514 list_for_each_entry(alias, &pmu->aliases, list)
1515 if (!strcmp(alias->name, name))
1516 return true;
1517 }
1518 return false;
1519}
Adrian Hunter7d4bdab2014-07-31 09:00:50 +03001520
1521static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
1522{
1523 struct stat st;
1524 char path[PATH_MAX];
1525 const char *sysfs;
1526
1527 sysfs = sysfs__mountpoint();
1528 if (!sysfs)
1529 return NULL;
1530
1531 snprintf(path, PATH_MAX,
1532 "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name);
1533
1534 if (stat(path, &st) < 0)
1535 return NULL;
1536
1537 return fopen(path, "r");
1538}
1539
1540int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
1541 ...)
1542{
1543 va_list args;
1544 FILE *file;
1545 int ret = EOF;
1546
1547 va_start(args, fmt);
1548 file = perf_pmu__open_file(pmu, name);
1549 if (file) {
1550 ret = vfscanf(file, fmt, args);
1551 fclose(file);
1552 }
1553 va_end(args);
1554 return ret;
1555}