blob: 80fb1593913a9227a742a1a2ed56330c877facad [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>
Jiri Olsacd82a322012-03-15 20:09:17 +01004#include <sys/types.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03005#include <errno.h>
Arnaldo Carvalho de Meloc23c2a02017-09-11 10:50:26 -03006#include <fcntl.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -03007#include <sys/stat.h>
Jiri Olsacd82a322012-03-15 20:09:17 +01008#include <unistd.h>
9#include <stdio.h>
Adrian Hunterdc0a6202014-07-31 09:00:49 +030010#include <stdbool.h>
Adrian Hunter7d4bdab2014-07-31 09:00:50 +030011#include <stdarg.h>
Jiri Olsacd82a322012-03-15 20:09:17 +010012#include <dirent.h>
Borislav Petkovcd0cfad2013-12-09 17:14:24 +010013#include <api/fs/fs.h>
Stephane Eranian410136f2013-11-12 17:58:49 +010014#include <locale.h>
Jiri Olsacd82a322012-03-15 20:09:17 +010015#include "util.h"
16#include "pmu.h"
17#include "parse-events.h"
Yan, Zheng7ae92e72012-09-10 15:53:50 +080018#include "cpumap.h"
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -070019#include "header.h"
20#include "pmu-events/pmu-events.h"
Andi Kleen61eb2eb2016-09-15 15:24:44 -070021#include "cache.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030022#include "string2.h"
Jiri Olsacd82a322012-03-15 20:09:17 +010023
Arnaldo Carvalho de Meloab1bf6532013-01-18 17:05:09 -030024struct perf_pmu_format {
25 char *name;
26 int value;
27 DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
28 struct list_head list;
29};
30
Robert Richter50a96672012-08-16 21:10:24 +020031#define EVENT_SOURCE_DEVICE_PATH "/bus/event_source/devices/"
32
Jiri Olsacd82a322012-03-15 20:09:17 +010033int perf_pmu_parse(struct list_head *list, char *name);
34extern FILE *perf_pmu_in;
35
36static LIST_HEAD(pmus);
37
38/*
39 * Parse & process all the sysfs attributes located under
40 * the directory specified in 'dir' parameter.
41 */
Jiri Olsacff7f952012-11-10 01:46:50 +010042int perf_pmu__format_parse(char *dir, struct list_head *head)
Jiri Olsacd82a322012-03-15 20:09:17 +010043{
44 struct dirent *evt_ent;
45 DIR *format_dir;
46 int ret = 0;
47
48 format_dir = opendir(dir);
49 if (!format_dir)
50 return -EINVAL;
51
52 while (!ret && (evt_ent = readdir(format_dir))) {
53 char path[PATH_MAX];
54 char *name = evt_ent->d_name;
55 FILE *file;
56
57 if (!strcmp(name, ".") || !strcmp(name, ".."))
58 continue;
59
60 snprintf(path, PATH_MAX, "%s/%s", dir, name);
61
62 ret = -EINVAL;
63 file = fopen(path, "r");
64 if (!file)
65 break;
66
67 perf_pmu_in = file;
68 ret = perf_pmu_parse(head, name);
69 fclose(file);
70 }
71
72 closedir(format_dir);
73 return ret;
74}
75
76/*
77 * Reading/parsing the default pmu format definition, which should be
78 * located at:
79 * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
80 */
Adrian Hunterb6b96fb2013-07-04 16:20:25 +030081static int pmu_format(const char *name, struct list_head *format)
Jiri Olsacd82a322012-03-15 20:09:17 +010082{
83 struct stat st;
84 char path[PATH_MAX];
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -030085 const char *sysfs = sysfs__mountpoint();
Jiri Olsacd82a322012-03-15 20:09:17 +010086
Jiri Olsacd82a322012-03-15 20:09:17 +010087 if (!sysfs)
88 return -1;
89
90 snprintf(path, PATH_MAX,
Robert Richter50a96672012-08-16 21:10:24 +020091 "%s" EVENT_SOURCE_DEVICE_PATH "%s/format", sysfs, name);
Jiri Olsacd82a322012-03-15 20:09:17 +010092
93 if (stat(path, &st) < 0)
Robert Richter9bc8f9f2012-06-14 22:38:37 +020094 return 0; /* no error if format does not exist */
Jiri Olsacd82a322012-03-15 20:09:17 +010095
Jiri Olsacff7f952012-11-10 01:46:50 +010096 if (perf_pmu__format_parse(path, format))
Jiri Olsacd82a322012-03-15 20:09:17 +010097 return -1;
98
99 return 0;
100}
101
Andi Kleend02fc6b2017-01-03 07:08:23 -0800102static int convert_scale(const char *scale, char **end, double *sval)
103{
104 char *lc;
105 int ret = 0;
106
107 /*
108 * save current locale
109 */
110 lc = setlocale(LC_NUMERIC, NULL);
111
112 /*
113 * The lc string may be allocated in static storage,
114 * so get a dynamic copy to make it survive setlocale
115 * call below.
116 */
117 lc = strdup(lc);
118 if (!lc) {
119 ret = -ENOMEM;
120 goto out;
121 }
122
123 /*
124 * force to C locale to ensure kernel
125 * scale string is converted correctly.
126 * kernel uses default C locale.
127 */
128 setlocale(LC_NUMERIC, "C");
129
130 *sval = strtod(scale, end);
131
132out:
133 /* restore locale */
134 setlocale(LC_NUMERIC, lc);
135 free(lc);
136 return ret;
137}
138
Stephane Eranian410136f2013-11-12 17:58:49 +0100139static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *name)
140{
141 struct stat st;
142 ssize_t sret;
143 char scale[128];
144 int fd, ret = -1;
145 char path[PATH_MAX];
Stephane Eranian410136f2013-11-12 17:58:49 +0100146
147 snprintf(path, PATH_MAX, "%s/%s.scale", dir, name);
148
149 fd = open(path, O_RDONLY);
150 if (fd == -1)
151 return -1;
152
153 if (fstat(fd, &st) < 0)
154 goto error;
155
156 sret = read(fd, scale, sizeof(scale)-1);
157 if (sret < 0)
158 goto error;
159
Madhavan Srinivasan9ecae062015-05-31 11:36:23 +0530160 if (scale[sret - 1] == '\n')
161 scale[sret - 1] = '\0';
162 else
163 scale[sret] = '\0';
164
Andi Kleend02fc6b2017-01-03 07:08:23 -0800165 ret = convert_scale(scale, NULL, &alias->scale);
Stephane Eranian410136f2013-11-12 17:58:49 +0100166error:
167 close(fd);
168 return ret;
169}
170
171static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *name)
172{
173 char path[PATH_MAX];
174 ssize_t sret;
175 int fd;
176
177 snprintf(path, PATH_MAX, "%s/%s.unit", dir, name);
178
179 fd = open(path, O_RDONLY);
180 if (fd == -1)
181 return -1;
182
Markus Trippelsdorfd85ce832015-12-14 16:44:40 +0100183 sret = read(fd, alias->unit, UNIT_MAX_LEN);
Stephane Eranian410136f2013-11-12 17:58:49 +0100184 if (sret < 0)
185 goto error;
186
187 close(fd);
188
Madhavan Srinivasan9ecae062015-05-31 11:36:23 +0530189 if (alias->unit[sret - 1] == '\n')
190 alias->unit[sret - 1] = '\0';
191 else
192 alias->unit[sret] = '\0';
Stephane Eranian410136f2013-11-12 17:58:49 +0100193
194 return 0;
195error:
196 close(fd);
197 alias->unit[0] = '\0';
198 return -1;
199}
200
Matt Fleming044330c2014-11-21 10:31:12 +0100201static int
202perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, char *dir, char *name)
203{
204 char path[PATH_MAX];
205 int fd;
206
207 snprintf(path, PATH_MAX, "%s/%s.per-pkg", dir, name);
208
209 fd = open(path, O_RDONLY);
210 if (fd == -1)
211 return -1;
212
213 close(fd);
214
215 alias->per_pkg = true;
216 return 0;
217}
218
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100219static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias,
220 char *dir, char *name)
221{
222 char path[PATH_MAX];
223 int fd;
224
225 snprintf(path, PATH_MAX, "%s/%s.snapshot", dir, name);
226
227 fd = open(path, O_RDONLY);
228 if (fd == -1)
229 return -1;
230
231 alias->snapshot = true;
232 close(fd);
233 return 0;
234}
235
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700236static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name,
Andi Kleenfedb2b52017-01-27 18:03:37 -0800237 char *desc, char *val,
238 char *long_desc, char *topic,
Andi Kleen00636c32017-03-20 13:17:07 -0700239 char *unit, char *perpkg,
Andi Kleen96284812017-03-20 13:17:10 -0700240 char *metric_expr,
241 char *metric_name)
Zheng Yana6146d52012-06-15 14:31:41 +0800242{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300243 struct perf_pmu_alias *alias;
Zheng Yana6146d52012-06-15 14:31:41 +0800244 int ret;
Andi Kleenfedb2b52017-01-27 18:03:37 -0800245 int num;
Zheng Yana6146d52012-06-15 14:31:41 +0800246
Zheng Yana6146d52012-06-15 14:31:41 +0800247 alias = malloc(sizeof(*alias));
248 if (!alias)
249 return -ENOMEM;
250
251 INIT_LIST_HEAD(&alias->terms);
Stephane Eranian410136f2013-11-12 17:58:49 +0100252 alias->scale = 1.0;
253 alias->unit[0] = '\0';
Matt Fleming044330c2014-11-21 10:31:12 +0100254 alias->per_pkg = false;
Stephane Eranian84530922016-01-06 19:50:01 +0100255 alias->snapshot = false;
Stephane Eranian410136f2013-11-12 17:58:49 +0100256
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700257 ret = parse_events_terms(&alias->terms, val);
Zheng Yana6146d52012-06-15 14:31:41 +0800258 if (ret) {
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700259 pr_err("Cannot parse alias %s: %d\n", val, ret);
Zheng Yana6146d52012-06-15 14:31:41 +0800260 free(alias);
261 return ret;
262 }
263
264 alias->name = strdup(name);
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700265 if (dir) {
266 /*
267 * load unit name and scale if available
268 */
269 perf_pmu__parse_unit(alias, dir, name);
270 perf_pmu__parse_scale(alias, dir, name);
271 perf_pmu__parse_per_pkg(alias, dir, name);
272 perf_pmu__parse_snapshot(alias, dir, name);
273 }
Stephane Eranian410136f2013-11-12 17:58:49 +0100274
Andi Kleen00636c32017-03-20 13:17:07 -0700275 alias->metric_expr = metric_expr ? strdup(metric_expr) : NULL;
Andi Kleen96284812017-03-20 13:17:10 -0700276 alias->metric_name = metric_name ? strdup(metric_name): NULL;
Andi Kleen08e60ed2016-09-15 15:24:43 -0700277 alias->desc = desc ? strdup(desc) : NULL;
Sukadev Bhattiproluc8d68282016-09-15 15:24:48 -0700278 alias->long_desc = long_desc ? strdup(long_desc) :
279 desc ? strdup(desc) : NULL;
Andi Kleendd5f1032016-09-15 15:24:50 -0700280 alias->topic = topic ? strdup(topic) : NULL;
Andi Kleenfedb2b52017-01-27 18:03:37 -0800281 if (unit) {
282 if (convert_scale(unit, &unit, &alias->scale) < 0)
283 return -1;
284 snprintf(alias->unit, sizeof(alias->unit), "%s", unit);
285 }
286 alias->per_pkg = perpkg && sscanf(perpkg, "%d", &num) == 1 && num == 1;
Andi Kleenf2361022017-01-27 18:03:40 -0800287 alias->str = strdup(val);
288
Zheng Yana6146d52012-06-15 14:31:41 +0800289 list_add_tail(&alias->list, list);
Stephane Eranian410136f2013-11-12 17:58:49 +0100290
Zheng Yana6146d52012-06-15 14:31:41 +0800291 return 0;
292}
293
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700294static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file)
295{
296 char buf[256];
297 int ret;
298
299 ret = fread(buf, 1, sizeof(buf), file);
300 if (ret == 0)
301 return -EINVAL;
302
303 buf[ret] = 0;
304
Andi Kleenfedb2b52017-01-27 18:03:37 -0800305 return __perf_pmu__new_alias(list, dir, name, NULL, buf, NULL, NULL, NULL,
Andi Kleen96284812017-03-20 13:17:10 -0700306 NULL, NULL, NULL);
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700307}
308
Matt Fleming46441bd2014-09-24 15:04:06 +0100309static inline bool pmu_alias_info_file(char *name)
310{
311 size_t len;
312
313 len = strlen(name);
314 if (len > 5 && !strcmp(name + len - 5, ".unit"))
315 return true;
316 if (len > 6 && !strcmp(name + len - 6, ".scale"))
317 return true;
Matt Fleming044330c2014-11-21 10:31:12 +0100318 if (len > 8 && !strcmp(name + len - 8, ".per-pkg"))
319 return true;
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100320 if (len > 9 && !strcmp(name + len - 9, ".snapshot"))
321 return true;
Matt Fleming46441bd2014-09-24 15:04:06 +0100322
323 return false;
324}
325
Zheng Yana6146d52012-06-15 14:31:41 +0800326/*
327 * Process all the sysfs attributes located under the directory
328 * specified in 'dir' parameter.
329 */
330static int pmu_aliases_parse(char *dir, struct list_head *head)
331{
332 struct dirent *evt_ent;
333 DIR *event_dir;
Zheng Yana6146d52012-06-15 14:31:41 +0800334
335 event_dir = opendir(dir);
336 if (!event_dir)
337 return -EINVAL;
338
Andi Kleen940db6d2016-02-17 14:44:55 -0800339 while ((evt_ent = readdir(event_dir))) {
Zheng Yana6146d52012-06-15 14:31:41 +0800340 char path[PATH_MAX];
341 char *name = evt_ent->d_name;
342 FILE *file;
343
344 if (!strcmp(name, ".") || !strcmp(name, ".."))
345 continue;
346
Stephane Eranian410136f2013-11-12 17:58:49 +0100347 /*
Matt Fleming46441bd2014-09-24 15:04:06 +0100348 * skip info files parsed in perf_pmu__new_alias()
Stephane Eranian410136f2013-11-12 17:58:49 +0100349 */
Matt Fleming46441bd2014-09-24 15:04:06 +0100350 if (pmu_alias_info_file(name))
Stephane Eranian410136f2013-11-12 17:58:49 +0100351 continue;
352
Zheng Yana6146d52012-06-15 14:31:41 +0800353 snprintf(path, PATH_MAX, "%s/%s", dir, name);
354
Zheng Yana6146d52012-06-15 14:31:41 +0800355 file = fopen(path, "r");
Andi Kleen940db6d2016-02-17 14:44:55 -0800356 if (!file) {
357 pr_debug("Cannot open %s\n", path);
358 continue;
359 }
Stephane Eranian410136f2013-11-12 17:58:49 +0100360
Andi Kleen940db6d2016-02-17 14:44:55 -0800361 if (perf_pmu__new_alias(head, dir, name, file) < 0)
362 pr_debug("Cannot set up %s\n", name);
Zheng Yana6146d52012-06-15 14:31:41 +0800363 fclose(file);
364 }
365
366 closedir(event_dir);
Andi Kleen940db6d2016-02-17 14:44:55 -0800367 return 0;
Zheng Yana6146d52012-06-15 14:31:41 +0800368}
369
370/*
371 * Reading the pmu event aliases definition, which should be located at:
372 * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
373 */
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300374static int pmu_aliases(const char *name, struct list_head *head)
Zheng Yana6146d52012-06-15 14:31:41 +0800375{
376 struct stat st;
377 char path[PATH_MAX];
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -0300378 const char *sysfs = sysfs__mountpoint();
Zheng Yana6146d52012-06-15 14:31:41 +0800379
Zheng Yana6146d52012-06-15 14:31:41 +0800380 if (!sysfs)
381 return -1;
382
383 snprintf(path, PATH_MAX,
384 "%s/bus/event_source/devices/%s/events", sysfs, name);
385
386 if (stat(path, &st) < 0)
Jiri Olsa3fded962012-10-10 14:53:16 +0200387 return 0; /* no error if 'events' does not exist */
Zheng Yana6146d52012-06-15 14:31:41 +0800388
389 if (pmu_aliases_parse(path, head))
390 return -1;
391
392 return 0;
393}
394
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300395static int pmu_alias_terms(struct perf_pmu_alias *alias,
Zheng Yana6146d52012-06-15 14:31:41 +0800396 struct list_head *terms)
397{
Jiri Olsa7c2f8162014-04-16 20:49:02 +0200398 struct parse_events_term *term, *cloned;
Zheng Yana6146d52012-06-15 14:31:41 +0800399 LIST_HEAD(list);
400 int ret;
401
402 list_for_each_entry(term, &alias->terms, list) {
Jiri Olsa7c2f8162014-04-16 20:49:02 +0200403 ret = parse_events_term__clone(&cloned, term);
Zheng Yana6146d52012-06-15 14:31:41 +0800404 if (ret) {
Arnaldo Carvalho de Melo682dc242016-02-12 16:48:00 -0300405 parse_events_terms__purge(&list);
Zheng Yana6146d52012-06-15 14:31:41 +0800406 return ret;
407 }
Andi Kleen59622fd2017-10-20 13:27:55 -0700408 /*
409 * Weak terms don't override command line options,
410 * which we don't want for implicit terms in aliases.
411 */
412 cloned->weak = true;
Jiri Olsa7c2f8162014-04-16 20:49:02 +0200413 list_add_tail(&cloned->list, &list);
Zheng Yana6146d52012-06-15 14:31:41 +0800414 }
415 list_splice(&list, terms);
416 return 0;
417}
418
Jiri Olsacd82a322012-03-15 20:09:17 +0100419/*
420 * Reading/parsing the default pmu type value, which should be
421 * located at:
422 * /sys/bus/event_source/devices/<dev>/type as sysfs attribute.
423 */
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300424static int pmu_type(const char *name, __u32 *type)
Jiri Olsacd82a322012-03-15 20:09:17 +0100425{
426 struct stat st;
427 char path[PATH_MAX];
Jiri Olsacd82a322012-03-15 20:09:17 +0100428 FILE *file;
429 int ret = 0;
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -0300430 const char *sysfs = sysfs__mountpoint();
Jiri Olsacd82a322012-03-15 20:09:17 +0100431
Jiri Olsacd82a322012-03-15 20:09:17 +0100432 if (!sysfs)
433 return -1;
434
435 snprintf(path, PATH_MAX,
Robert Richter50a96672012-08-16 21:10:24 +0200436 "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name);
Jiri Olsacd82a322012-03-15 20:09:17 +0100437
438 if (stat(path, &st) < 0)
439 return -1;
440
441 file = fopen(path, "r");
442 if (!file)
443 return -EINVAL;
444
445 if (1 != fscanf(file, "%u", type))
446 ret = -1;
447
448 fclose(file);
449 return ret;
450}
451
Robert Richter50a96672012-08-16 21:10:24 +0200452/* Add all pmus in sysfs to pmu list: */
453static void pmu_read_sysfs(void)
454{
455 char path[PATH_MAX];
Robert Richter50a96672012-08-16 21:10:24 +0200456 DIR *dir;
457 struct dirent *dent;
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -0300458 const char *sysfs = sysfs__mountpoint();
Robert Richter50a96672012-08-16 21:10:24 +0200459
Robert Richter50a96672012-08-16 21:10:24 +0200460 if (!sysfs)
461 return;
462
463 snprintf(path, PATH_MAX,
464 "%s" EVENT_SOURCE_DEVICE_PATH, sysfs);
465
466 dir = opendir(path);
467 if (!dir)
468 return;
469
470 while ((dent = readdir(dir))) {
471 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
472 continue;
473 /* add to static LIST_HEAD(pmus): */
474 perf_pmu__find(dent->d_name);
475 }
476
477 closedir(dir);
478}
479
Mark Rutland66ec11912017-10-06 19:38:22 +0100480static struct cpu_map *__pmu_cpumask(const char *path)
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800481{
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800482 FILE *file;
483 struct cpu_map *cpus;
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800484
485 file = fopen(path, "r");
486 if (!file)
487 return NULL;
488
489 cpus = cpu_map__read(file);
490 fclose(file);
491 return cpus;
492}
493
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700494/*
Mark Rutland66ec11912017-10-06 19:38:22 +0100495 * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64)
496 * may have a "cpus" file.
497 */
498#define CPUS_TEMPLATE_UNCORE "%s/bus/event_source/devices/%s/cpumask"
499#define CPUS_TEMPLATE_CPU "%s/bus/event_source/devices/%s/cpus"
500
501static struct cpu_map *pmu_cpumask(const char *name)
502{
503 char path[PATH_MAX];
504 struct cpu_map *cpus;
505 const char *sysfs = sysfs__mountpoint();
506 const char *templates[] = {
507 CPUS_TEMPLATE_UNCORE,
508 CPUS_TEMPLATE_CPU,
509 NULL
510 };
511 const char **template;
512
513 if (!sysfs)
514 return NULL;
515
516 for (template = templates; *template; template++) {
517 snprintf(path, PATH_MAX, *template, sysfs, name);
518 cpus = __pmu_cpumask(path);
519 if (cpus)
520 return cpus;
521 }
522
523 return NULL;
524}
525
526static bool pmu_is_uncore(const char *name)
527{
528 char path[PATH_MAX];
529 struct cpu_map *cpus;
530 const char *sysfs = sysfs__mountpoint();
531
532 snprintf(path, PATH_MAX, CPUS_TEMPLATE_UNCORE, sysfs, name);
533 cpus = __pmu_cpumask(path);
534 cpu_map__put(cpus);
535
536 return !!cpus;
537}
538
539/*
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700540 * Return the CPU id as a raw string.
541 *
542 * Each architecture should provide a more precise id string that
543 * can be use to match the architecture's "mapfile".
544 */
545char * __weak get_cpuid_str(void)
546{
547 return NULL;
548}
549
Andi Kleend77ade92017-08-31 12:40:30 -0700550static char *perf_pmu__getcpuid(void)
551{
552 char *cpuid;
553 static bool printed;
554
555 cpuid = getenv("PERF_CPUID");
556 if (cpuid)
557 cpuid = strdup(cpuid);
558 if (!cpuid)
559 cpuid = get_cpuid_str();
560 if (!cpuid)
561 return NULL;
562
563 if (!printed) {
564 pr_debug("Using CPUID %s\n", cpuid);
565 printed = true;
566 }
567 return cpuid;
568}
569
570struct pmu_events_map *perf_pmu__find_map(void)
571{
572 struct pmu_events_map *map;
573 char *cpuid = perf_pmu__getcpuid();
574 int i;
575
576 i = 0;
577 for (;;) {
578 map = &pmu_events_map[i++];
579 if (!map->table) {
580 map = NULL;
581 break;
582 }
583
584 if (!strcmp(map->cpuid, cpuid))
585 break;
586 }
587 free(cpuid);
588 return map;
589}
590
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700591/*
592 * From the pmu_events_map, find the table of PMU events that corresponds
593 * to the current running CPU. Then, add all PMU events from that table
594 * as aliases.
595 */
Andi Kleenfedb2b52017-01-27 18:03:37 -0800596static void pmu_add_cpu_aliases(struct list_head *head, const char *name)
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700597{
598 int i;
599 struct pmu_events_map *map;
600 struct pmu_event *pe;
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700601
Andi Kleend77ade92017-08-31 12:40:30 -0700602 map = perf_pmu__find_map();
603 if (!map)
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700604 return;
605
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700606 /*
607 * Found a matching PMU events table. Create aliases
608 */
609 i = 0;
610 while (1) {
Andi Kleenfedb2b52017-01-27 18:03:37 -0800611 const char *pname;
612
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700613 pe = &map->table[i++];
Andi Kleenb18f3e32017-08-31 12:40:31 -0700614 if (!pe->name) {
615 if (pe->metric_group || pe->metric_name)
616 continue;
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700617 break;
Andi Kleenb18f3e32017-08-31 12:40:31 -0700618 }
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700619
Andi Kleenfedb2b52017-01-27 18:03:37 -0800620 pname = pe->pmu ? pe->pmu : "cpu";
621 if (strncmp(pname, name, strlen(pname)))
622 continue;
623
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700624 /* need type casts to override 'const' */
625 __perf_pmu__new_alias(head, NULL, (char *)pe->name,
Sukadev Bhattiproluc8d68282016-09-15 15:24:48 -0700626 (char *)pe->desc, (char *)pe->event,
Andi Kleenfedb2b52017-01-27 18:03:37 -0800627 (char *)pe->long_desc, (char *)pe->topic,
Andi Kleen00636c32017-03-20 13:17:07 -0700628 (char *)pe->unit, (char *)pe->perpkg,
Andi Kleen96284812017-03-20 13:17:10 -0700629 (char *)pe->metric_expr,
630 (char *)pe->metric_name);
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700631 }
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700632}
633
Sukadev Bhattiproluc5de47f2015-06-10 00:25:07 -0700634struct perf_event_attr * __weak
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300635perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
636{
637 return NULL;
638}
639
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300640static struct perf_pmu *pmu_lookup(const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +0100641{
642 struct perf_pmu *pmu;
643 LIST_HEAD(format);
Zheng Yana6146d52012-06-15 14:31:41 +0800644 LIST_HEAD(aliases);
Jiri Olsacd82a322012-03-15 20:09:17 +0100645 __u32 type;
646
647 /*
648 * The pmu data we store & need consists of the pmu
649 * type value and format definitions. Load both right
650 * now.
651 */
652 if (pmu_format(name, &format))
653 return NULL;
654
Andi Kleen15b22ed2017-01-27 18:03:38 -0800655 /*
656 * Check the type first to avoid unnecessary work.
657 */
658 if (pmu_type(name, &type))
659 return NULL;
660
Jiri Olsa3fded962012-10-10 14:53:16 +0200661 if (pmu_aliases(name, &aliases))
662 return NULL;
663
Andi Kleenfedb2b52017-01-27 18:03:37 -0800664 pmu_add_cpu_aliases(&aliases, name);
Jiri Olsacd82a322012-03-15 20:09:17 +0100665 pmu = zalloc(sizeof(*pmu));
666 if (!pmu)
667 return NULL;
668
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800669 pmu->cpus = pmu_cpumask(name);
670
Mark Rutland66ec11912017-10-06 19:38:22 +0100671 pmu->is_uncore = pmu_is_uncore(name);
672
Jiri Olsacd82a322012-03-15 20:09:17 +0100673 INIT_LIST_HEAD(&pmu->format);
Zheng Yana6146d52012-06-15 14:31:41 +0800674 INIT_LIST_HEAD(&pmu->aliases);
Jiri Olsacd82a322012-03-15 20:09:17 +0100675 list_splice(&format, &pmu->format);
Zheng Yana6146d52012-06-15 14:31:41 +0800676 list_splice(&aliases, &pmu->aliases);
Jiri Olsacd82a322012-03-15 20:09:17 +0100677 pmu->name = strdup(name);
678 pmu->type = type;
Robert Richter9bc8f9f2012-06-14 22:38:37 +0200679 list_add_tail(&pmu->list, &pmus);
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300680
681 pmu->default_config = perf_pmu__get_default_config(pmu);
682
Jiri Olsacd82a322012-03-15 20:09:17 +0100683 return pmu;
684}
685
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300686static struct perf_pmu *pmu_find(const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +0100687{
688 struct perf_pmu *pmu;
689
690 list_for_each_entry(pmu, &pmus, list)
691 if (!strcmp(pmu->name, name))
692 return pmu;
693
694 return NULL;
695}
696
Robert Richter50a96672012-08-16 21:10:24 +0200697struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
698{
699 /*
700 * pmu iterator: If pmu is NULL, we start at the begin,
701 * otherwise return the next pmu. Returns NULL on end.
702 */
703 if (!pmu) {
704 pmu_read_sysfs();
705 pmu = list_prepare_entry(pmu, &pmus, list);
706 }
707 list_for_each_entry_continue(pmu, &pmus, list)
708 return pmu;
709 return NULL;
710}
711
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300712struct perf_pmu *perf_pmu__find(const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +0100713{
714 struct perf_pmu *pmu;
715
716 /*
717 * Once PMU is loaded it stays in the list,
718 * so we keep us from multiple reading/parsing
719 * the pmu format definitions.
720 */
721 pmu = pmu_find(name);
722 if (pmu)
723 return pmu;
724
725 return pmu_lookup(name);
726}
727
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300728static struct perf_pmu_format *
Adrian Hunter09ff6072015-07-17 19:33:49 +0300729pmu_find_format(struct list_head *formats, const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +0100730{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300731 struct perf_pmu_format *format;
Jiri Olsacd82a322012-03-15 20:09:17 +0100732
733 list_for_each_entry(format, formats, list)
734 if (!strcmp(format->name, name))
735 return format;
736
737 return NULL;
738}
739
Adrian Hunter09ff6072015-07-17 19:33:49 +0300740__u64 perf_pmu__format_bits(struct list_head *formats, const char *name)
741{
742 struct perf_pmu_format *format = pmu_find_format(formats, name);
743 __u64 bits = 0;
744 int fbit;
745
746 if (!format)
747 return 0;
748
749 for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
750 bits |= 1ULL << fbit;
751
752 return bits;
753}
754
Jiri Olsacd82a322012-03-15 20:09:17 +0100755/*
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300756 * Sets value based on the format definition (format parameter)
Jiri Olsacd82a322012-03-15 20:09:17 +0100757 * and unformated value (value parameter).
Jiri Olsacd82a322012-03-15 20:09:17 +0100758 */
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300759static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
760 bool zero)
Jiri Olsacd82a322012-03-15 20:09:17 +0100761{
762 unsigned long fbit, vbit;
Jiri Olsacd82a322012-03-15 20:09:17 +0100763
764 for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
765
766 if (!test_bit(fbit, format))
767 continue;
768
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300769 if (value & (1llu << vbit++))
770 *v |= (1llu << fbit);
771 else if (zero)
772 *v &= ~(1llu << fbit);
Jiri Olsacd82a322012-03-15 20:09:17 +0100773 }
Jiri Olsacd82a322012-03-15 20:09:17 +0100774}
775
Adrian Hunter0efe6b62015-07-17 19:33:50 +0300776static __u64 pmu_format_max_value(const unsigned long *format)
777{
Kan Liangac0e2cd2016-03-30 12:16:15 -0700778 __u64 w = 0;
779 int fbit;
Adrian Hunter0efe6b62015-07-17 19:33:50 +0300780
Kan Liangac0e2cd2016-03-30 12:16:15 -0700781 for_each_set_bit(fbit, format, PERF_PMU_FORMAT_BITS)
782 w |= (1ULL << fbit);
783
784 return w;
Adrian Hunter0efe6b62015-07-17 19:33:50 +0300785}
786
Jiri Olsacd82a322012-03-15 20:09:17 +0100787/*
Cody P Schafer688d4df2015-01-07 17:13:50 -0800788 * Term is a string term, and might be a param-term. Try to look up it's value
789 * in the remaining terms.
790 * - We have a term like "base-or-format-term=param-term",
791 * - We need to find the value supplied for "param-term" (with param-term named
792 * in a config string) later on in the term list.
793 */
794static int pmu_resolve_param_term(struct parse_events_term *term,
795 struct list_head *head_terms,
796 __u64 *value)
797{
798 struct parse_events_term *t;
799
800 list_for_each_entry(t, head_terms, list) {
801 if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
802 if (!strcmp(t->config, term->config)) {
803 t->used = true;
804 *value = t->val.num;
805 return 0;
806 }
807 }
808 }
809
Namhyung Kimbb963e12017-02-17 17:17:38 +0900810 if (verbose > 0)
Cody P Schafer688d4df2015-01-07 17:13:50 -0800811 printf("Required parameter '%s' not specified\n", term->config);
812
813 return -1;
814}
815
He Kuangffeb8832015-09-28 03:52:14 +0000816static char *pmu_formats_string(struct list_head *formats)
Jiri Olsae64b0202015-04-22 21:10:21 +0200817{
818 struct perf_pmu_format *format;
Masami Hiramatsu11db4e22016-05-10 14:47:44 +0900819 char *str = NULL;
820 struct strbuf buf = STRBUF_INIT;
Jiri Olsae64b0202015-04-22 21:10:21 +0200821 unsigned i = 0;
822
He Kuangffeb8832015-09-28 03:52:14 +0000823 if (!formats)
Jiri Olsae64b0202015-04-22 21:10:21 +0200824 return NULL;
825
826 /* sysfs exported terms */
He Kuangffeb8832015-09-28 03:52:14 +0000827 list_for_each_entry(format, formats, list)
Masami Hiramatsu11db4e22016-05-10 14:47:44 +0900828 if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
829 goto error;
Jiri Olsae64b0202015-04-22 21:10:21 +0200830
He Kuangffeb8832015-09-28 03:52:14 +0000831 str = strbuf_detach(&buf, NULL);
Masami Hiramatsu11db4e22016-05-10 14:47:44 +0900832error:
He Kuangffeb8832015-09-28 03:52:14 +0000833 strbuf_release(&buf);
Jiri Olsae64b0202015-04-22 21:10:21 +0200834
Jiri Olsae64b0202015-04-22 21:10:21 +0200835 return str;
Jiri Olsae64b0202015-04-22 21:10:21 +0200836}
837
Cody P Schafer688d4df2015-01-07 17:13:50 -0800838/*
Jiri Olsacd82a322012-03-15 20:09:17 +0100839 * Setup one of config[12] attr members based on the
Cody P Schafer88aca8d2014-01-08 08:43:51 -0800840 * user input data - term parameter.
Jiri Olsacd82a322012-03-15 20:09:17 +0100841 */
842static int pmu_config_term(struct list_head *formats,
843 struct perf_event_attr *attr,
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300844 struct parse_events_term *term,
Cody P Schafer688d4df2015-01-07 17:13:50 -0800845 struct list_head *head_terms,
Jiri Olsae64b0202015-04-22 21:10:21 +0200846 bool zero, struct parse_events_error *err)
Jiri Olsacd82a322012-03-15 20:09:17 +0100847{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300848 struct perf_pmu_format *format;
Jiri Olsacd82a322012-03-15 20:09:17 +0100849 __u64 *vp;
Adrian Hunter0efe6b62015-07-17 19:33:50 +0300850 __u64 val, max_val;
Jiri Olsacd82a322012-03-15 20:09:17 +0100851
852 /*
Cody P Schafer688d4df2015-01-07 17:13:50 -0800853 * If this is a parameter we've already used for parameterized-eval,
854 * skip it in normal eval.
855 */
856 if (term->used)
857 return 0;
858
859 /*
Jiri Olsacd82a322012-03-15 20:09:17 +0100860 * Hardcoded terms should be already in, so nothing
861 * to be done for them.
862 */
863 if (parse_events__is_hardcoded_term(term))
864 return 0;
865
Jiri Olsacd82a322012-03-15 20:09:17 +0100866 format = pmu_find_format(formats, term->config);
Cody P Schafer688d4df2015-01-07 17:13:50 -0800867 if (!format) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900868 if (verbose > 0)
Cody P Schafer688d4df2015-01-07 17:13:50 -0800869 printf("Invalid event/parameter '%s'\n", term->config);
Jiri Olsae64b0202015-04-22 21:10:21 +0200870 if (err) {
He Kuangffeb8832015-09-28 03:52:14 +0000871 char *pmu_term = pmu_formats_string(formats);
872
Jiri Olsae64b0202015-04-22 21:10:21 +0200873 err->idx = term->err_term;
874 err->str = strdup("unknown term");
He Kuangffeb8832015-09-28 03:52:14 +0000875 err->help = parse_events_formats_error_string(pmu_term);
876 free(pmu_term);
Jiri Olsae64b0202015-04-22 21:10:21 +0200877 }
Jiri Olsacd82a322012-03-15 20:09:17 +0100878 return -EINVAL;
Cody P Schafer688d4df2015-01-07 17:13:50 -0800879 }
Jiri Olsacd82a322012-03-15 20:09:17 +0100880
881 switch (format->value) {
882 case PERF_PMU_FORMAT_VALUE_CONFIG:
883 vp = &attr->config;
884 break;
885 case PERF_PMU_FORMAT_VALUE_CONFIG1:
886 vp = &attr->config1;
887 break;
888 case PERF_PMU_FORMAT_VALUE_CONFIG2:
889 vp = &attr->config2;
890 break;
891 default:
892 return -EINVAL;
893 }
894
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200895 /*
Cody P Schafer688d4df2015-01-07 17:13:50 -0800896 * Either directly use a numeric term, or try to translate string terms
897 * using event parameters.
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200898 */
Jiri Olsa99e71382017-02-17 15:00:56 +0100899 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
900 if (term->no_value &&
901 bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) {
902 if (err) {
903 err->idx = term->err_val;
904 err->str = strdup("no value assigned for term");
905 }
906 return -EINVAL;
907 }
908
Cody P Schafer688d4df2015-01-07 17:13:50 -0800909 val = term->val.num;
Jiri Olsa99e71382017-02-17 15:00:56 +0100910 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
Cody P Schafer688d4df2015-01-07 17:13:50 -0800911 if (strcmp(term->val.str, "?")) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900912 if (verbose > 0) {
Cody P Schafer688d4df2015-01-07 17:13:50 -0800913 pr_info("Invalid sysfs entry %s=%s\n",
914 term->config, term->val.str);
Jiri Olsae64b0202015-04-22 21:10:21 +0200915 }
916 if (err) {
917 err->idx = term->err_val;
918 err->str = strdup("expected numeric value");
919 }
Cody P Schafer688d4df2015-01-07 17:13:50 -0800920 return -EINVAL;
921 }
922
923 if (pmu_resolve_param_term(term, head_terms, &val))
924 return -EINVAL;
925 } else
926 return -EINVAL;
927
Adrian Hunter0efe6b62015-07-17 19:33:50 +0300928 max_val = pmu_format_max_value(format->bits);
929 if (val > max_val) {
930 if (err) {
931 err->idx = term->err_val;
932 if (asprintf(&err->str,
933 "value too big for format, maximum is %llu",
934 (unsigned long long)max_val) < 0)
935 err->str = strdup("value too big for format");
936 return -EINVAL;
937 }
938 /*
939 * Assume we don't care if !err, in which case the value will be
940 * silently truncated.
941 */
942 }
943
Cody P Schafer688d4df2015-01-07 17:13:50 -0800944 pmu_format_value(format->bits, val, vp, zero);
Jiri Olsacd82a322012-03-15 20:09:17 +0100945 return 0;
946}
947
Jiri Olsacff7f952012-11-10 01:46:50 +0100948int perf_pmu__config_terms(struct list_head *formats,
949 struct perf_event_attr *attr,
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300950 struct list_head *head_terms,
Jiri Olsae64b0202015-04-22 21:10:21 +0200951 bool zero, struct parse_events_error *err)
Jiri Olsacd82a322012-03-15 20:09:17 +0100952{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -0300953 struct parse_events_term *term;
Jiri Olsacd82a322012-03-15 20:09:17 +0100954
Cody P Schafer688d4df2015-01-07 17:13:50 -0800955 list_for_each_entry(term, head_terms, list) {
Jiri Olsae64b0202015-04-22 21:10:21 +0200956 if (pmu_config_term(formats, attr, term, head_terms,
957 zero, err))
Jiri Olsacd82a322012-03-15 20:09:17 +0100958 return -EINVAL;
Cody P Schafer688d4df2015-01-07 17:13:50 -0800959 }
Jiri Olsacd82a322012-03-15 20:09:17 +0100960
961 return 0;
962}
963
964/*
965 * Configures event's 'attr' parameter based on the:
966 * 1) users input - specified in terms parameter
967 * 2) pmu format definitions - specified by pmu parameter
968 */
969int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
Jiri Olsae64b0202015-04-22 21:10:21 +0200970 struct list_head *head_terms,
971 struct parse_events_error *err)
Jiri Olsacd82a322012-03-15 20:09:17 +0100972{
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300973 bool zero = !!pmu->default_config;
974
Jiri Olsacd82a322012-03-15 20:09:17 +0100975 attr->type = pmu->type;
Jiri Olsae64b0202015-04-22 21:10:21 +0200976 return perf_pmu__config_terms(&pmu->format, attr, head_terms,
977 zero, err);
Jiri Olsacd82a322012-03-15 20:09:17 +0100978}
979
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300980static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
981 struct parse_events_term *term)
Zheng Yana6146d52012-06-15 14:31:41 +0800982{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300983 struct perf_pmu_alias *alias;
Zheng Yana6146d52012-06-15 14:31:41 +0800984 char *name;
985
986 if (parse_events__is_hardcoded_term(term))
987 return NULL;
988
989 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
990 if (term->val.num != 1)
991 return NULL;
992 if (pmu_find_format(&pmu->format, term->config))
993 return NULL;
994 name = term->config;
995 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
996 if (strcasecmp(term->config, "event"))
997 return NULL;
998 name = term->val.str;
999 } else {
1000 return NULL;
1001 }
1002
1003 list_for_each_entry(alias, &pmu->aliases, list) {
1004 if (!strcasecmp(alias->name, name))
1005 return alias;
1006 }
1007 return NULL;
1008}
1009
Stephane Eranian410136f2013-11-12 17:58:49 +01001010
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001011static int check_info_data(struct perf_pmu_alias *alias,
1012 struct perf_pmu_info *info)
Stephane Eranian410136f2013-11-12 17:58:49 +01001013{
1014 /*
1015 * Only one term in event definition can
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001016 * define unit, scale and snapshot, fail
1017 * if there's more than one.
Stephane Eranian410136f2013-11-12 17:58:49 +01001018 */
Arnaldo Carvalho de Melob30a7d12017-02-15 10:06:20 -03001019 if ((info->unit && alias->unit[0]) ||
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001020 (info->scale && alias->scale) ||
1021 (info->snapshot && alias->snapshot))
Stephane Eranian410136f2013-11-12 17:58:49 +01001022 return -EINVAL;
1023
Arnaldo Carvalho de Melob30a7d12017-02-15 10:06:20 -03001024 if (alias->unit[0])
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001025 info->unit = alias->unit;
Stephane Eranian410136f2013-11-12 17:58:49 +01001026
1027 if (alias->scale)
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001028 info->scale = alias->scale;
1029
1030 if (alias->snapshot)
1031 info->snapshot = alias->snapshot;
Stephane Eranian410136f2013-11-12 17:58:49 +01001032
1033 return 0;
1034}
1035
Zheng Yana6146d52012-06-15 14:31:41 +08001036/*
1037 * Find alias in the terms list and replace it with the terms
1038 * defined for the alias
1039 */
Stephane Eranian410136f2013-11-12 17:58:49 +01001040int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
Matt Fleming46441bd2014-09-24 15:04:06 +01001041 struct perf_pmu_info *info)
Zheng Yana6146d52012-06-15 14:31:41 +08001042{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001043 struct parse_events_term *term, *h;
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -03001044 struct perf_pmu_alias *alias;
Zheng Yana6146d52012-06-15 14:31:41 +08001045 int ret;
1046
Matt Fleming044330c2014-11-21 10:31:12 +01001047 info->per_pkg = false;
1048
Stephane Eranian8a398892014-01-17 16:34:05 +01001049 /*
1050 * Mark unit and scale as not set
1051 * (different from default values, see below)
1052 */
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001053 info->unit = NULL;
1054 info->scale = 0.0;
1055 info->snapshot = false;
Andi Kleen37932c12017-03-20 13:17:08 -07001056 info->metric_expr = NULL;
Andi Kleen96284812017-03-20 13:17:10 -07001057 info->metric_name = NULL;
Stephane Eranian410136f2013-11-12 17:58:49 +01001058
Zheng Yana6146d52012-06-15 14:31:41 +08001059 list_for_each_entry_safe(term, h, head_terms, list) {
1060 alias = pmu_find_alias(pmu, term);
1061 if (!alias)
1062 continue;
1063 ret = pmu_alias_terms(alias, &term->list);
1064 if (ret)
1065 return ret;
Stephane Eranian410136f2013-11-12 17:58:49 +01001066
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001067 ret = check_info_data(alias, info);
Stephane Eranian410136f2013-11-12 17:58:49 +01001068 if (ret)
1069 return ret;
1070
Matt Fleming044330c2014-11-21 10:31:12 +01001071 if (alias->per_pkg)
1072 info->per_pkg = true;
Andi Kleen37932c12017-03-20 13:17:08 -07001073 info->metric_expr = alias->metric_expr;
Andi Kleen96284812017-03-20 13:17:10 -07001074 info->metric_name = alias->metric_name;
Matt Fleming044330c2014-11-21 10:31:12 +01001075
Zheng Yana6146d52012-06-15 14:31:41 +08001076 list_del(&term->list);
1077 free(term);
1078 }
Stephane Eranian8a398892014-01-17 16:34:05 +01001079
1080 /*
1081 * if no unit or scale foundin aliases, then
1082 * set defaults as for evsel
1083 * unit cannot left to NULL
1084 */
Matt Fleming46441bd2014-09-24 15:04:06 +01001085 if (info->unit == NULL)
1086 info->unit = "";
Stephane Eranian8a398892014-01-17 16:34:05 +01001087
Matt Fleming46441bd2014-09-24 15:04:06 +01001088 if (info->scale == 0.0)
1089 info->scale = 1.0;
Stephane Eranian8a398892014-01-17 16:34:05 +01001090
Zheng Yana6146d52012-06-15 14:31:41 +08001091 return 0;
1092}
1093
Jiri Olsacd82a322012-03-15 20:09:17 +01001094int perf_pmu__new_format(struct list_head *list, char *name,
1095 int config, unsigned long *bits)
1096{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -03001097 struct perf_pmu_format *format;
Jiri Olsacd82a322012-03-15 20:09:17 +01001098
1099 format = zalloc(sizeof(*format));
1100 if (!format)
1101 return -ENOMEM;
1102
1103 format->name = strdup(name);
1104 format->value = config;
1105 memcpy(format->bits, bits, sizeof(format->bits));
1106
1107 list_add_tail(&format->list, list);
1108 return 0;
1109}
1110
1111void perf_pmu__set_format(unsigned long *bits, long from, long to)
1112{
1113 long b;
1114
1115 if (!to)
1116 to = from;
1117
Sukadev Bhattiprolu15268132013-01-17 09:11:30 -08001118 memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
Jiri Olsacd82a322012-03-15 20:09:17 +01001119 for (b = from; b <= to; b++)
1120 set_bit(b, bits);
1121}
Andi Kleendc098b32013-04-20 11:02:29 -07001122
Cody P Schaferaaea3612015-01-07 17:13:51 -08001123static int sub_non_neg(int a, int b)
1124{
1125 if (b > a)
1126 return 0;
1127 return a - b;
1128}
1129
Andi Kleendc098b32013-04-20 11:02:29 -07001130static char *format_alias(char *buf, int len, struct perf_pmu *pmu,
1131 struct perf_pmu_alias *alias)
1132{
Cody P Schaferaaea3612015-01-07 17:13:51 -08001133 struct parse_events_term *term;
1134 int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name);
1135
1136 list_for_each_entry(term, &alias->terms, list) {
1137 if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
1138 used += snprintf(buf + used, sub_non_neg(len, used),
1139 ",%s=%s", term->config,
1140 term->val.str);
1141 }
1142
1143 if (sub_non_neg(len, used) > 0) {
1144 buf[used] = '/';
1145 used++;
1146 }
1147 if (sub_non_neg(len, used) > 0) {
1148 buf[used] = '\0';
1149 used++;
1150 } else
1151 buf[len - 1] = '\0';
1152
Andi Kleendc098b32013-04-20 11:02:29 -07001153 return buf;
1154}
1155
1156static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu,
1157 struct perf_pmu_alias *alias)
1158{
1159 snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name);
1160 return buf;
1161}
1162
Andi Kleendd5f1032016-09-15 15:24:50 -07001163struct sevent {
Andi Kleen08e60ed2016-09-15 15:24:43 -07001164 char *name;
1165 char *desc;
Andi Kleendd5f1032016-09-15 15:24:50 -07001166 char *topic;
Andi Kleenf2361022017-01-27 18:03:40 -08001167 char *str;
1168 char *pmu;
Andi Kleen7f372a62017-03-20 13:17:09 -07001169 char *metric_expr;
Andi Kleen96284812017-03-20 13:17:10 -07001170 char *metric_name;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001171};
1172
Andi Kleendd5f1032016-09-15 15:24:50 -07001173static int cmp_sevent(const void *a, const void *b)
Andi Kleendc098b32013-04-20 11:02:29 -07001174{
Andi Kleendd5f1032016-09-15 15:24:50 -07001175 const struct sevent *as = a;
1176 const struct sevent *bs = b;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001177
1178 /* Put extra events last */
1179 if (!!as->desc != !!bs->desc)
1180 return !!as->desc - !!bs->desc;
Andi Kleendd5f1032016-09-15 15:24:50 -07001181 if (as->topic && bs->topic) {
1182 int n = strcmp(as->topic, bs->topic);
1183
1184 if (n)
1185 return n;
1186 }
Andi Kleen08e60ed2016-09-15 15:24:43 -07001187 return strcmp(as->name, bs->name);
1188}
1189
1190static void wordwrap(char *s, int start, int max, int corr)
1191{
1192 int column = start;
1193 int n;
1194
1195 while (*s) {
1196 int wlen = strcspn(s, " \t");
1197
1198 if (column + wlen >= max && column > start) {
1199 printf("\n%*s", start, "");
1200 column = start + corr;
1201 }
1202 n = printf("%s%.*s", column > start ? " " : "", wlen, s);
1203 if (n <= 0)
1204 break;
1205 s += wlen;
1206 column += n;
Taeung Songaa4beb12017-04-07 23:24:20 +09001207 s = ltrim(s);
Andi Kleen08e60ed2016-09-15 15:24:43 -07001208 }
Andi Kleendc098b32013-04-20 11:02:29 -07001209}
1210
Sukadev Bhattiproluc8d68282016-09-15 15:24:48 -07001211void print_pmu_events(const char *event_glob, bool name_only, bool quiet_flag,
Andi Kleenbf874fc2017-03-20 13:17:11 -07001212 bool long_desc, bool details_flag)
Andi Kleendc098b32013-04-20 11:02:29 -07001213{
1214 struct perf_pmu *pmu;
1215 struct perf_pmu_alias *alias;
1216 char buf[1024];
1217 int printed = 0;
1218 int len, j;
Andi Kleendd5f1032016-09-15 15:24:50 -07001219 struct sevent *aliases;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001220 int numdesc = 0;
Andi Kleen61eb2eb2016-09-15 15:24:44 -07001221 int columns = pager_get_columns();
Andi Kleendd5f1032016-09-15 15:24:50 -07001222 char *topic = NULL;
Andi Kleendc098b32013-04-20 11:02:29 -07001223
1224 pmu = NULL;
1225 len = 0;
Adrian Hunter42634bc2014-10-23 13:45:10 +03001226 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
Andi Kleendc098b32013-04-20 11:02:29 -07001227 list_for_each_entry(alias, &pmu->aliases, list)
1228 len++;
Adrian Hunter42634bc2014-10-23 13:45:10 +03001229 if (pmu->selectable)
1230 len++;
1231 }
Andi Kleendd5f1032016-09-15 15:24:50 -07001232 aliases = zalloc(sizeof(struct sevent) * len);
Andi Kleendc098b32013-04-20 11:02:29 -07001233 if (!aliases)
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001234 goto out_enomem;
Andi Kleendc098b32013-04-20 11:02:29 -07001235 pmu = NULL;
1236 j = 0;
Adrian Hunter42634bc2014-10-23 13:45:10 +03001237 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
Andi Kleendc098b32013-04-20 11:02:29 -07001238 list_for_each_entry(alias, &pmu->aliases, list) {
Andi Kleen08e60ed2016-09-15 15:24:43 -07001239 char *name = alias->desc ? alias->name :
1240 format_alias(buf, sizeof(buf), pmu, alias);
Andi Kleendc098b32013-04-20 11:02:29 -07001241 bool is_cpu = !strcmp(pmu->name, "cpu");
1242
1243 if (event_glob != NULL &&
Andi Kleen38d14f02016-10-19 10:50:01 -07001244 !(strglobmatch_nocase(name, event_glob) ||
1245 (!is_cpu && strglobmatch_nocase(alias->name,
Andi Kleen67bdc352016-10-19 11:45:23 -07001246 event_glob)) ||
1247 (alias->topic &&
1248 strglobmatch_nocase(alias->topic, event_glob))))
Andi Kleendc098b32013-04-20 11:02:29 -07001249 continue;
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001250
Andi Kleen08e60ed2016-09-15 15:24:43 -07001251 if (is_cpu && !name_only && !alias->desc)
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001252 name = format_alias_or(buf, sizeof(buf), pmu, alias);
1253
Andi Kleen08e60ed2016-09-15 15:24:43 -07001254 aliases[j].name = name;
1255 if (is_cpu && !name_only && !alias->desc)
1256 aliases[j].name = format_alias_or(buf,
1257 sizeof(buf),
1258 pmu, alias);
1259 aliases[j].name = strdup(aliases[j].name);
1260 if (!aliases[j].name)
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001261 goto out_enomem;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001262
Sukadev Bhattiproluc8d68282016-09-15 15:24:48 -07001263 aliases[j].desc = long_desc ? alias->long_desc :
1264 alias->desc;
Andi Kleendd5f1032016-09-15 15:24:50 -07001265 aliases[j].topic = alias->topic;
Andi Kleenf2361022017-01-27 18:03:40 -08001266 aliases[j].str = alias->str;
1267 aliases[j].pmu = pmu->name;
Andi Kleen7f372a62017-03-20 13:17:09 -07001268 aliases[j].metric_expr = alias->metric_expr;
Andi Kleen96284812017-03-20 13:17:10 -07001269 aliases[j].metric_name = alias->metric_name;
Andi Kleendc098b32013-04-20 11:02:29 -07001270 j++;
1271 }
Arnaldo Carvalho de Melofa52cea2015-10-02 15:28:16 -03001272 if (pmu->selectable &&
1273 (event_glob == NULL || strglobmatch(pmu->name, event_glob))) {
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001274 char *s;
1275 if (asprintf(&s, "%s//", pmu->name) < 0)
1276 goto out_enomem;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001277 aliases[j].name = s;
Adrian Hunter42634bc2014-10-23 13:45:10 +03001278 j++;
1279 }
1280 }
Andi Kleendc098b32013-04-20 11:02:29 -07001281 len = j;
Andi Kleendd5f1032016-09-15 15:24:50 -07001282 qsort(aliases, len, sizeof(struct sevent), cmp_sevent);
Andi Kleendc098b32013-04-20 11:02:29 -07001283 for (j = 0; j < len; j++) {
Andi Kleen15b22ed2017-01-27 18:03:38 -08001284 /* Skip duplicates */
1285 if (j > 0 && !strcmp(aliases[j].name, aliases[j - 1].name))
1286 continue;
Andi Kleendc098b32013-04-20 11:02:29 -07001287 if (name_only) {
Andi Kleen08e60ed2016-09-15 15:24:43 -07001288 printf("%s ", aliases[j].name);
Andi Kleendc098b32013-04-20 11:02:29 -07001289 continue;
1290 }
Andi Kleen1c5f01f2016-09-15 15:24:45 -07001291 if (aliases[j].desc && !quiet_flag) {
Andi Kleen08e60ed2016-09-15 15:24:43 -07001292 if (numdesc++ == 0)
1293 printf("\n");
Andi Kleendd5f1032016-09-15 15:24:50 -07001294 if (aliases[j].topic && (!topic ||
1295 strcmp(topic, aliases[j].topic))) {
1296 printf("%s%s:\n", topic ? "\n" : "",
1297 aliases[j].topic);
1298 topic = aliases[j].topic;
1299 }
Andi Kleen08e60ed2016-09-15 15:24:43 -07001300 printf(" %-50s\n", aliases[j].name);
1301 printf("%*s", 8, "[");
1302 wordwrap(aliases[j].desc, 8, columns, 0);
1303 printf("]\n");
Andi Kleenbf874fc2017-03-20 13:17:11 -07001304 if (details_flag) {
Andi Kleen7f372a62017-03-20 13:17:09 -07001305 printf("%*s%s/%s/ ", 8, "", aliases[j].pmu, aliases[j].str);
Andi Kleen96284812017-03-20 13:17:10 -07001306 if (aliases[j].metric_name)
1307 printf(" MetricName: %s", aliases[j].metric_name);
Andi Kleen7f372a62017-03-20 13:17:09 -07001308 if (aliases[j].metric_expr)
1309 printf(" MetricExpr: %s", aliases[j].metric_expr);
1310 putchar('\n');
1311 }
Andi Kleen08e60ed2016-09-15 15:24:43 -07001312 } else
1313 printf(" %-50s [Kernel PMU event]\n", aliases[j].name);
Andi Kleendc098b32013-04-20 11:02:29 -07001314 printed++;
1315 }
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03001316 if (printed && pager_in_use())
Andi Kleendc098b32013-04-20 11:02:29 -07001317 printf("\n");
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001318out_free:
1319 for (j = 0; j < len; j++)
Andi Kleen08e60ed2016-09-15 15:24:43 -07001320 zfree(&aliases[j].name);
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001321 zfree(&aliases);
1322 return;
1323
1324out_enomem:
1325 printf("FATAL: not enough memory to print PMU events\n");
1326 if (aliases)
1327 goto out_free;
Andi Kleendc098b32013-04-20 11:02:29 -07001328}
Andi Kleen4cabc3d2013-08-21 16:47:26 -07001329
1330bool pmu_have_event(const char *pname, const char *name)
1331{
1332 struct perf_pmu *pmu;
1333 struct perf_pmu_alias *alias;
1334
1335 pmu = NULL;
1336 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1337 if (strcmp(pname, pmu->name))
1338 continue;
1339 list_for_each_entry(alias, &pmu->aliases, list)
1340 if (!strcmp(alias->name, name))
1341 return true;
1342 }
1343 return false;
1344}
Adrian Hunter7d4bdab2014-07-31 09:00:50 +03001345
1346static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
1347{
1348 struct stat st;
1349 char path[PATH_MAX];
1350 const char *sysfs;
1351
1352 sysfs = sysfs__mountpoint();
1353 if (!sysfs)
1354 return NULL;
1355
1356 snprintf(path, PATH_MAX,
1357 "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name);
1358
1359 if (stat(path, &st) < 0)
1360 return NULL;
1361
1362 return fopen(path, "r");
1363}
1364
1365int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
1366 ...)
1367{
1368 va_list args;
1369 FILE *file;
1370 int ret = EOF;
1371
1372 va_start(args, fmt);
1373 file = perf_pmu__open_file(pmu, name);
1374 if (file) {
1375 ret = vfscanf(file, fmt, args);
1376 fclose(file);
1377 }
1378 va_end(args);
1379 return ret;
1380}