blob: ed25d7f88731313a1faa495711ed7383c99301c3 [file] [log] [blame]
Jiri Olsacd82a322012-03-15 20:09:17 +01001#include <linux/list.h>
Sukadev Bhattiproluc5de47f2015-06-10 00:25:07 -07002#include <linux/compiler.h>
Jiri Olsacd82a322012-03-15 20:09:17 +01003#include <sys/types.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03004#include <errno.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -03005#include <sys/stat.h>
Jiri Olsacd82a322012-03-15 20:09:17 +01006#include <unistd.h>
7#include <stdio.h>
Adrian Hunterdc0a6202014-07-31 09:00:49 +03008#include <stdbool.h>
Adrian Hunter7d4bdab2014-07-31 09:00:50 +03009#include <stdarg.h>
Jiri Olsacd82a322012-03-15 20:09:17 +010010#include <dirent.h>
Borislav Petkovcd0cfad2013-12-09 17:14:24 +010011#include <api/fs/fs.h>
Stephane Eranian410136f2013-11-12 17:58:49 +010012#include <locale.h>
Jiri Olsacd82a322012-03-15 20:09:17 +010013#include "util.h"
14#include "pmu.h"
15#include "parse-events.h"
Yan, Zheng7ae92e72012-09-10 15:53:50 +080016#include "cpumap.h"
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -070017#include "header.h"
18#include "pmu-events/pmu-events.h"
Andi Kleen61eb2eb2016-09-15 15:24:44 -070019#include "cache.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030020#include "string2.h"
Jiri Olsacd82a322012-03-15 20:09:17 +010021
Arnaldo Carvalho de Meloab1bf6532013-01-18 17:05:09 -030022struct perf_pmu_format {
23 char *name;
24 int value;
25 DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
26 struct list_head list;
27};
28
Robert Richter50a96672012-08-16 21:10:24 +020029#define EVENT_SOURCE_DEVICE_PATH "/bus/event_source/devices/"
30
Jiri Olsacd82a322012-03-15 20:09:17 +010031int perf_pmu_parse(struct list_head *list, char *name);
32extern FILE *perf_pmu_in;
33
34static LIST_HEAD(pmus);
35
36/*
37 * Parse & process all the sysfs attributes located under
38 * the directory specified in 'dir' parameter.
39 */
Jiri Olsacff7f952012-11-10 01:46:50 +010040int perf_pmu__format_parse(char *dir, struct list_head *head)
Jiri Olsacd82a322012-03-15 20:09:17 +010041{
42 struct dirent *evt_ent;
43 DIR *format_dir;
44 int ret = 0;
45
46 format_dir = opendir(dir);
47 if (!format_dir)
48 return -EINVAL;
49
50 while (!ret && (evt_ent = readdir(format_dir))) {
51 char path[PATH_MAX];
52 char *name = evt_ent->d_name;
53 FILE *file;
54
55 if (!strcmp(name, ".") || !strcmp(name, ".."))
56 continue;
57
58 snprintf(path, PATH_MAX, "%s/%s", dir, name);
59
60 ret = -EINVAL;
61 file = fopen(path, "r");
62 if (!file)
63 break;
64
65 perf_pmu_in = file;
66 ret = perf_pmu_parse(head, name);
67 fclose(file);
68 }
69
70 closedir(format_dir);
71 return ret;
72}
73
74/*
75 * Reading/parsing the default pmu format definition, which should be
76 * located at:
77 * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
78 */
Adrian Hunterb6b96fb2013-07-04 16:20:25 +030079static int pmu_format(const char *name, struct list_head *format)
Jiri Olsacd82a322012-03-15 20:09:17 +010080{
81 struct stat st;
82 char path[PATH_MAX];
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -030083 const char *sysfs = sysfs__mountpoint();
Jiri Olsacd82a322012-03-15 20:09:17 +010084
Jiri Olsacd82a322012-03-15 20:09:17 +010085 if (!sysfs)
86 return -1;
87
88 snprintf(path, PATH_MAX,
Robert Richter50a96672012-08-16 21:10:24 +020089 "%s" EVENT_SOURCE_DEVICE_PATH "%s/format", sysfs, name);
Jiri Olsacd82a322012-03-15 20:09:17 +010090
91 if (stat(path, &st) < 0)
Robert Richter9bc8f9f2012-06-14 22:38:37 +020092 return 0; /* no error if format does not exist */
Jiri Olsacd82a322012-03-15 20:09:17 +010093
Jiri Olsacff7f952012-11-10 01:46:50 +010094 if (perf_pmu__format_parse(path, format))
Jiri Olsacd82a322012-03-15 20:09:17 +010095 return -1;
96
97 return 0;
98}
99
Andi Kleend02fc6b2017-01-03 07:08:23 -0800100static int convert_scale(const char *scale, char **end, double *sval)
101{
102 char *lc;
103 int ret = 0;
104
105 /*
106 * save current locale
107 */
108 lc = setlocale(LC_NUMERIC, NULL);
109
110 /*
111 * The lc string may be allocated in static storage,
112 * so get a dynamic copy to make it survive setlocale
113 * call below.
114 */
115 lc = strdup(lc);
116 if (!lc) {
117 ret = -ENOMEM;
118 goto out;
119 }
120
121 /*
122 * force to C locale to ensure kernel
123 * scale string is converted correctly.
124 * kernel uses default C locale.
125 */
126 setlocale(LC_NUMERIC, "C");
127
128 *sval = strtod(scale, end);
129
130out:
131 /* restore locale */
132 setlocale(LC_NUMERIC, lc);
133 free(lc);
134 return ret;
135}
136
Stephane Eranian410136f2013-11-12 17:58:49 +0100137static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *name)
138{
139 struct stat st;
140 ssize_t sret;
141 char scale[128];
142 int fd, ret = -1;
143 char path[PATH_MAX];
Stephane Eranian410136f2013-11-12 17:58:49 +0100144
145 snprintf(path, PATH_MAX, "%s/%s.scale", dir, name);
146
147 fd = open(path, O_RDONLY);
148 if (fd == -1)
149 return -1;
150
151 if (fstat(fd, &st) < 0)
152 goto error;
153
154 sret = read(fd, scale, sizeof(scale)-1);
155 if (sret < 0)
156 goto error;
157
Madhavan Srinivasan9ecae062015-05-31 11:36:23 +0530158 if (scale[sret - 1] == '\n')
159 scale[sret - 1] = '\0';
160 else
161 scale[sret] = '\0';
162
Andi Kleend02fc6b2017-01-03 07:08:23 -0800163 ret = convert_scale(scale, NULL, &alias->scale);
Stephane Eranian410136f2013-11-12 17:58:49 +0100164error:
165 close(fd);
166 return ret;
167}
168
169static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *name)
170{
171 char path[PATH_MAX];
172 ssize_t sret;
173 int fd;
174
175 snprintf(path, PATH_MAX, "%s/%s.unit", dir, name);
176
177 fd = open(path, O_RDONLY);
178 if (fd == -1)
179 return -1;
180
Markus Trippelsdorfd85ce832015-12-14 16:44:40 +0100181 sret = read(fd, alias->unit, UNIT_MAX_LEN);
Stephane Eranian410136f2013-11-12 17:58:49 +0100182 if (sret < 0)
183 goto error;
184
185 close(fd);
186
Madhavan Srinivasan9ecae062015-05-31 11:36:23 +0530187 if (alias->unit[sret - 1] == '\n')
188 alias->unit[sret - 1] = '\0';
189 else
190 alias->unit[sret] = '\0';
Stephane Eranian410136f2013-11-12 17:58:49 +0100191
192 return 0;
193error:
194 close(fd);
195 alias->unit[0] = '\0';
196 return -1;
197}
198
Matt Fleming044330c2014-11-21 10:31:12 +0100199static int
200perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, char *dir, char *name)
201{
202 char path[PATH_MAX];
203 int fd;
204
205 snprintf(path, PATH_MAX, "%s/%s.per-pkg", dir, name);
206
207 fd = open(path, O_RDONLY);
208 if (fd == -1)
209 return -1;
210
211 close(fd);
212
213 alias->per_pkg = true;
214 return 0;
215}
216
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100217static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias,
218 char *dir, char *name)
219{
220 char path[PATH_MAX];
221 int fd;
222
223 snprintf(path, PATH_MAX, "%s/%s.snapshot", dir, name);
224
225 fd = open(path, O_RDONLY);
226 if (fd == -1)
227 return -1;
228
229 alias->snapshot = true;
230 close(fd);
231 return 0;
232}
233
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700234static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name,
Andi Kleenfedb2b52017-01-27 18:03:37 -0800235 char *desc, char *val,
236 char *long_desc, char *topic,
Andi Kleen00636c32017-03-20 13:17:07 -0700237 char *unit, char *perpkg,
Andi Kleen96284812017-03-20 13:17:10 -0700238 char *metric_expr,
239 char *metric_name)
Zheng Yana6146d52012-06-15 14:31:41 +0800240{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300241 struct perf_pmu_alias *alias;
Zheng Yana6146d52012-06-15 14:31:41 +0800242 int ret;
Andi Kleenfedb2b52017-01-27 18:03:37 -0800243 int num;
Zheng Yana6146d52012-06-15 14:31:41 +0800244
Zheng Yana6146d52012-06-15 14:31:41 +0800245 alias = malloc(sizeof(*alias));
246 if (!alias)
247 return -ENOMEM;
248
249 INIT_LIST_HEAD(&alias->terms);
Stephane Eranian410136f2013-11-12 17:58:49 +0100250 alias->scale = 1.0;
251 alias->unit[0] = '\0';
Matt Fleming044330c2014-11-21 10:31:12 +0100252 alias->per_pkg = false;
Stephane Eranian84530922016-01-06 19:50:01 +0100253 alias->snapshot = false;
Stephane Eranian410136f2013-11-12 17:58:49 +0100254
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700255 ret = parse_events_terms(&alias->terms, val);
Zheng Yana6146d52012-06-15 14:31:41 +0800256 if (ret) {
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700257 pr_err("Cannot parse alias %s: %d\n", val, ret);
Zheng Yana6146d52012-06-15 14:31:41 +0800258 free(alias);
259 return ret;
260 }
261
262 alias->name = strdup(name);
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700263 if (dir) {
264 /*
265 * load unit name and scale if available
266 */
267 perf_pmu__parse_unit(alias, dir, name);
268 perf_pmu__parse_scale(alias, dir, name);
269 perf_pmu__parse_per_pkg(alias, dir, name);
270 perf_pmu__parse_snapshot(alias, dir, name);
271 }
Stephane Eranian410136f2013-11-12 17:58:49 +0100272
Andi Kleen00636c32017-03-20 13:17:07 -0700273 alias->metric_expr = metric_expr ? strdup(metric_expr) : NULL;
Andi Kleen96284812017-03-20 13:17:10 -0700274 alias->metric_name = metric_name ? strdup(metric_name): NULL;
Andi Kleen08e60ed2016-09-15 15:24:43 -0700275 alias->desc = desc ? strdup(desc) : NULL;
Sukadev Bhattiproluc8d68282016-09-15 15:24:48 -0700276 alias->long_desc = long_desc ? strdup(long_desc) :
277 desc ? strdup(desc) : NULL;
Andi Kleendd5f1032016-09-15 15:24:50 -0700278 alias->topic = topic ? strdup(topic) : NULL;
Andi Kleenfedb2b52017-01-27 18:03:37 -0800279 if (unit) {
280 if (convert_scale(unit, &unit, &alias->scale) < 0)
281 return -1;
282 snprintf(alias->unit, sizeof(alias->unit), "%s", unit);
283 }
284 alias->per_pkg = perpkg && sscanf(perpkg, "%d", &num) == 1 && num == 1;
Andi Kleenf2361022017-01-27 18:03:40 -0800285 alias->str = strdup(val);
286
Zheng Yana6146d52012-06-15 14:31:41 +0800287 list_add_tail(&alias->list, list);
Stephane Eranian410136f2013-11-12 17:58:49 +0100288
Zheng Yana6146d52012-06-15 14:31:41 +0800289 return 0;
290}
291
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700292static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file)
293{
294 char buf[256];
295 int ret;
296
297 ret = fread(buf, 1, sizeof(buf), file);
298 if (ret == 0)
299 return -EINVAL;
300
301 buf[ret] = 0;
302
Andi Kleenfedb2b52017-01-27 18:03:37 -0800303 return __perf_pmu__new_alias(list, dir, name, NULL, buf, NULL, NULL, NULL,
Andi Kleen96284812017-03-20 13:17:10 -0700304 NULL, NULL, NULL);
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700305}
306
Matt Fleming46441bd2014-09-24 15:04:06 +0100307static inline bool pmu_alias_info_file(char *name)
308{
309 size_t len;
310
311 len = strlen(name);
312 if (len > 5 && !strcmp(name + len - 5, ".unit"))
313 return true;
314 if (len > 6 && !strcmp(name + len - 6, ".scale"))
315 return true;
Matt Fleming044330c2014-11-21 10:31:12 +0100316 if (len > 8 && !strcmp(name + len - 8, ".per-pkg"))
317 return true;
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100318 if (len > 9 && !strcmp(name + len - 9, ".snapshot"))
319 return true;
Matt Fleming46441bd2014-09-24 15:04:06 +0100320
321 return false;
322}
323
Zheng Yana6146d52012-06-15 14:31:41 +0800324/*
325 * Process all the sysfs attributes located under the directory
326 * specified in 'dir' parameter.
327 */
328static int pmu_aliases_parse(char *dir, struct list_head *head)
329{
330 struct dirent *evt_ent;
331 DIR *event_dir;
Zheng Yana6146d52012-06-15 14:31:41 +0800332
333 event_dir = opendir(dir);
334 if (!event_dir)
335 return -EINVAL;
336
Andi Kleen940db6d2016-02-17 14:44:55 -0800337 while ((evt_ent = readdir(event_dir))) {
Zheng Yana6146d52012-06-15 14:31:41 +0800338 char path[PATH_MAX];
339 char *name = evt_ent->d_name;
340 FILE *file;
341
342 if (!strcmp(name, ".") || !strcmp(name, ".."))
343 continue;
344
Stephane Eranian410136f2013-11-12 17:58:49 +0100345 /*
Matt Fleming46441bd2014-09-24 15:04:06 +0100346 * skip info files parsed in perf_pmu__new_alias()
Stephane Eranian410136f2013-11-12 17:58:49 +0100347 */
Matt Fleming46441bd2014-09-24 15:04:06 +0100348 if (pmu_alias_info_file(name))
Stephane Eranian410136f2013-11-12 17:58:49 +0100349 continue;
350
Zheng Yana6146d52012-06-15 14:31:41 +0800351 snprintf(path, PATH_MAX, "%s/%s", dir, name);
352
Zheng Yana6146d52012-06-15 14:31:41 +0800353 file = fopen(path, "r");
Andi Kleen940db6d2016-02-17 14:44:55 -0800354 if (!file) {
355 pr_debug("Cannot open %s\n", path);
356 continue;
357 }
Stephane Eranian410136f2013-11-12 17:58:49 +0100358
Andi Kleen940db6d2016-02-17 14:44:55 -0800359 if (perf_pmu__new_alias(head, dir, name, file) < 0)
360 pr_debug("Cannot set up %s\n", name);
Zheng Yana6146d52012-06-15 14:31:41 +0800361 fclose(file);
362 }
363
364 closedir(event_dir);
Andi Kleen940db6d2016-02-17 14:44:55 -0800365 return 0;
Zheng Yana6146d52012-06-15 14:31:41 +0800366}
367
368/*
369 * Reading the pmu event aliases definition, which should be located at:
370 * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
371 */
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300372static int pmu_aliases(const char *name, struct list_head *head)
Zheng Yana6146d52012-06-15 14:31:41 +0800373{
374 struct stat st;
375 char path[PATH_MAX];
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -0300376 const char *sysfs = sysfs__mountpoint();
Zheng Yana6146d52012-06-15 14:31:41 +0800377
Zheng Yana6146d52012-06-15 14:31:41 +0800378 if (!sysfs)
379 return -1;
380
381 snprintf(path, PATH_MAX,
382 "%s/bus/event_source/devices/%s/events", sysfs, name);
383
384 if (stat(path, &st) < 0)
Jiri Olsa3fded962012-10-10 14:53:16 +0200385 return 0; /* no error if 'events' does not exist */
Zheng Yana6146d52012-06-15 14:31:41 +0800386
387 if (pmu_aliases_parse(path, head))
388 return -1;
389
390 return 0;
391}
392
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300393static int pmu_alias_terms(struct perf_pmu_alias *alias,
Zheng Yana6146d52012-06-15 14:31:41 +0800394 struct list_head *terms)
395{
Jiri Olsa7c2f8162014-04-16 20:49:02 +0200396 struct parse_events_term *term, *cloned;
Zheng Yana6146d52012-06-15 14:31:41 +0800397 LIST_HEAD(list);
398 int ret;
399
400 list_for_each_entry(term, &alias->terms, list) {
Jiri Olsa7c2f8162014-04-16 20:49:02 +0200401 ret = parse_events_term__clone(&cloned, term);
Zheng Yana6146d52012-06-15 14:31:41 +0800402 if (ret) {
Arnaldo Carvalho de Melo682dc242016-02-12 16:48:00 -0300403 parse_events_terms__purge(&list);
Zheng Yana6146d52012-06-15 14:31:41 +0800404 return ret;
405 }
Jiri Olsa7c2f8162014-04-16 20:49:02 +0200406 list_add_tail(&cloned->list, &list);
Zheng Yana6146d52012-06-15 14:31:41 +0800407 }
408 list_splice(&list, terms);
409 return 0;
410}
411
Jiri Olsacd82a322012-03-15 20:09:17 +0100412/*
413 * Reading/parsing the default pmu type value, which should be
414 * located at:
415 * /sys/bus/event_source/devices/<dev>/type as sysfs attribute.
416 */
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300417static int pmu_type(const char *name, __u32 *type)
Jiri Olsacd82a322012-03-15 20:09:17 +0100418{
419 struct stat st;
420 char path[PATH_MAX];
Jiri Olsacd82a322012-03-15 20:09:17 +0100421 FILE *file;
422 int ret = 0;
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -0300423 const char *sysfs = sysfs__mountpoint();
Jiri Olsacd82a322012-03-15 20:09:17 +0100424
Jiri Olsacd82a322012-03-15 20:09:17 +0100425 if (!sysfs)
426 return -1;
427
428 snprintf(path, PATH_MAX,
Robert Richter50a96672012-08-16 21:10:24 +0200429 "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name);
Jiri Olsacd82a322012-03-15 20:09:17 +0100430
431 if (stat(path, &st) < 0)
432 return -1;
433
434 file = fopen(path, "r");
435 if (!file)
436 return -EINVAL;
437
438 if (1 != fscanf(file, "%u", type))
439 ret = -1;
440
441 fclose(file);
442 return ret;
443}
444
Robert Richter50a96672012-08-16 21:10:24 +0200445/* Add all pmus in sysfs to pmu list: */
446static void pmu_read_sysfs(void)
447{
448 char path[PATH_MAX];
Robert Richter50a96672012-08-16 21:10:24 +0200449 DIR *dir;
450 struct dirent *dent;
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -0300451 const char *sysfs = sysfs__mountpoint();
Robert Richter50a96672012-08-16 21:10:24 +0200452
Robert Richter50a96672012-08-16 21:10:24 +0200453 if (!sysfs)
454 return;
455
456 snprintf(path, PATH_MAX,
457 "%s" EVENT_SOURCE_DEVICE_PATH, sysfs);
458
459 dir = opendir(path);
460 if (!dir)
461 return;
462
463 while ((dent = readdir(dir))) {
464 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
465 continue;
466 /* add to static LIST_HEAD(pmus): */
467 perf_pmu__find(dent->d_name);
468 }
469
470 closedir(dir);
471}
472
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300473static struct cpu_map *pmu_cpumask(const char *name)
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800474{
475 struct stat st;
476 char path[PATH_MAX];
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800477 FILE *file;
478 struct cpu_map *cpus;
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -0300479 const char *sysfs = sysfs__mountpoint();
Mark Rutland7e3fcffe2016-09-08 11:21:52 +0100480 const char *templates[] = {
481 "%s/bus/event_source/devices/%s/cpumask",
482 "%s/bus/event_source/devices/%s/cpus",
483 NULL
484 };
485 const char **template;
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800486
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800487 if (!sysfs)
488 return NULL;
489
Mark Rutland7e3fcffe2016-09-08 11:21:52 +0100490 for (template = templates; *template; template++) {
491 snprintf(path, PATH_MAX, *template, sysfs, name);
492 if (stat(path, &st) == 0)
493 break;
494 }
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800495
Mark Rutland7e3fcffe2016-09-08 11:21:52 +0100496 if (!*template)
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800497 return NULL;
498
499 file = fopen(path, "r");
500 if (!file)
501 return NULL;
502
503 cpus = cpu_map__read(file);
504 fclose(file);
505 return cpus;
506}
507
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700508/*
509 * Return the CPU id as a raw string.
510 *
511 * Each architecture should provide a more precise id string that
512 * can be use to match the architecture's "mapfile".
513 */
514char * __weak get_cpuid_str(void)
515{
516 return NULL;
517}
518
Andi Kleend77ade92017-08-31 12:40:30 -0700519static char *perf_pmu__getcpuid(void)
520{
521 char *cpuid;
522 static bool printed;
523
524 cpuid = getenv("PERF_CPUID");
525 if (cpuid)
526 cpuid = strdup(cpuid);
527 if (!cpuid)
528 cpuid = get_cpuid_str();
529 if (!cpuid)
530 return NULL;
531
532 if (!printed) {
533 pr_debug("Using CPUID %s\n", cpuid);
534 printed = true;
535 }
536 return cpuid;
537}
538
539struct pmu_events_map *perf_pmu__find_map(void)
540{
541 struct pmu_events_map *map;
542 char *cpuid = perf_pmu__getcpuid();
543 int i;
544
545 i = 0;
546 for (;;) {
547 map = &pmu_events_map[i++];
548 if (!map->table) {
549 map = NULL;
550 break;
551 }
552
553 if (!strcmp(map->cpuid, cpuid))
554 break;
555 }
556 free(cpuid);
557 return map;
558}
559
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700560/*
561 * From the pmu_events_map, find the table of PMU events that corresponds
562 * to the current running CPU. Then, add all PMU events from that table
563 * as aliases.
564 */
Andi Kleenfedb2b52017-01-27 18:03:37 -0800565static void pmu_add_cpu_aliases(struct list_head *head, const char *name)
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700566{
567 int i;
568 struct pmu_events_map *map;
569 struct pmu_event *pe;
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700570
Andi Kleend77ade92017-08-31 12:40:30 -0700571 map = perf_pmu__find_map();
572 if (!map)
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700573 return;
574
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700575 /*
576 * Found a matching PMU events table. Create aliases
577 */
578 i = 0;
579 while (1) {
Andi Kleenfedb2b52017-01-27 18:03:37 -0800580 const char *pname;
581
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700582 pe = &map->table[i++];
583 if (!pe->name)
584 break;
585
Andi Kleenfedb2b52017-01-27 18:03:37 -0800586 pname = pe->pmu ? pe->pmu : "cpu";
587 if (strncmp(pname, name, strlen(pname)))
588 continue;
589
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700590 /* need type casts to override 'const' */
591 __perf_pmu__new_alias(head, NULL, (char *)pe->name,
Sukadev Bhattiproluc8d68282016-09-15 15:24:48 -0700592 (char *)pe->desc, (char *)pe->event,
Andi Kleenfedb2b52017-01-27 18:03:37 -0800593 (char *)pe->long_desc, (char *)pe->topic,
Andi Kleen00636c32017-03-20 13:17:07 -0700594 (char *)pe->unit, (char *)pe->perpkg,
Andi Kleen96284812017-03-20 13:17:10 -0700595 (char *)pe->metric_expr,
596 (char *)pe->metric_name);
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700597 }
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700598}
599
Sukadev Bhattiproluc5de47f2015-06-10 00:25:07 -0700600struct perf_event_attr * __weak
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300601perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
602{
603 return NULL;
604}
605
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300606static struct perf_pmu *pmu_lookup(const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +0100607{
608 struct perf_pmu *pmu;
609 LIST_HEAD(format);
Zheng Yana6146d52012-06-15 14:31:41 +0800610 LIST_HEAD(aliases);
Jiri Olsacd82a322012-03-15 20:09:17 +0100611 __u32 type;
612
613 /*
614 * The pmu data we store & need consists of the pmu
615 * type value and format definitions. Load both right
616 * now.
617 */
618 if (pmu_format(name, &format))
619 return NULL;
620
Andi Kleen15b22ed2017-01-27 18:03:38 -0800621 /*
622 * Check the type first to avoid unnecessary work.
623 */
624 if (pmu_type(name, &type))
625 return NULL;
626
Jiri Olsa3fded962012-10-10 14:53:16 +0200627 if (pmu_aliases(name, &aliases))
628 return NULL;
629
Andi Kleenfedb2b52017-01-27 18:03:37 -0800630 pmu_add_cpu_aliases(&aliases, name);
Jiri Olsacd82a322012-03-15 20:09:17 +0100631 pmu = zalloc(sizeof(*pmu));
632 if (!pmu)
633 return NULL;
634
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800635 pmu->cpus = pmu_cpumask(name);
636
Jiri Olsacd82a322012-03-15 20:09:17 +0100637 INIT_LIST_HEAD(&pmu->format);
Zheng Yana6146d52012-06-15 14:31:41 +0800638 INIT_LIST_HEAD(&pmu->aliases);
Jiri Olsacd82a322012-03-15 20:09:17 +0100639 list_splice(&format, &pmu->format);
Zheng Yana6146d52012-06-15 14:31:41 +0800640 list_splice(&aliases, &pmu->aliases);
Jiri Olsacd82a322012-03-15 20:09:17 +0100641 pmu->name = strdup(name);
642 pmu->type = type;
Robert Richter9bc8f9f2012-06-14 22:38:37 +0200643 list_add_tail(&pmu->list, &pmus);
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300644
645 pmu->default_config = perf_pmu__get_default_config(pmu);
646
Jiri Olsacd82a322012-03-15 20:09:17 +0100647 return pmu;
648}
649
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300650static struct perf_pmu *pmu_find(const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +0100651{
652 struct perf_pmu *pmu;
653
654 list_for_each_entry(pmu, &pmus, list)
655 if (!strcmp(pmu->name, name))
656 return pmu;
657
658 return NULL;
659}
660
Robert Richter50a96672012-08-16 21:10:24 +0200661struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
662{
663 /*
664 * pmu iterator: If pmu is NULL, we start at the begin,
665 * otherwise return the next pmu. Returns NULL on end.
666 */
667 if (!pmu) {
668 pmu_read_sysfs();
669 pmu = list_prepare_entry(pmu, &pmus, list);
670 }
671 list_for_each_entry_continue(pmu, &pmus, list)
672 return pmu;
673 return NULL;
674}
675
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300676struct perf_pmu *perf_pmu__find(const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +0100677{
678 struct perf_pmu *pmu;
679
680 /*
681 * Once PMU is loaded it stays in the list,
682 * so we keep us from multiple reading/parsing
683 * the pmu format definitions.
684 */
685 pmu = pmu_find(name);
686 if (pmu)
687 return pmu;
688
689 return pmu_lookup(name);
690}
691
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300692static struct perf_pmu_format *
Adrian Hunter09ff6072015-07-17 19:33:49 +0300693pmu_find_format(struct list_head *formats, const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +0100694{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300695 struct perf_pmu_format *format;
Jiri Olsacd82a322012-03-15 20:09:17 +0100696
697 list_for_each_entry(format, formats, list)
698 if (!strcmp(format->name, name))
699 return format;
700
701 return NULL;
702}
703
Adrian Hunter09ff6072015-07-17 19:33:49 +0300704__u64 perf_pmu__format_bits(struct list_head *formats, const char *name)
705{
706 struct perf_pmu_format *format = pmu_find_format(formats, name);
707 __u64 bits = 0;
708 int fbit;
709
710 if (!format)
711 return 0;
712
713 for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
714 bits |= 1ULL << fbit;
715
716 return bits;
717}
718
Jiri Olsacd82a322012-03-15 20:09:17 +0100719/*
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300720 * Sets value based on the format definition (format parameter)
Jiri Olsacd82a322012-03-15 20:09:17 +0100721 * and unformated value (value parameter).
Jiri Olsacd82a322012-03-15 20:09:17 +0100722 */
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300723static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
724 bool zero)
Jiri Olsacd82a322012-03-15 20:09:17 +0100725{
726 unsigned long fbit, vbit;
Jiri Olsacd82a322012-03-15 20:09:17 +0100727
728 for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
729
730 if (!test_bit(fbit, format))
731 continue;
732
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300733 if (value & (1llu << vbit++))
734 *v |= (1llu << fbit);
735 else if (zero)
736 *v &= ~(1llu << fbit);
Jiri Olsacd82a322012-03-15 20:09:17 +0100737 }
Jiri Olsacd82a322012-03-15 20:09:17 +0100738}
739
Adrian Hunter0efe6b62015-07-17 19:33:50 +0300740static __u64 pmu_format_max_value(const unsigned long *format)
741{
Kan Liangac0e2cd2016-03-30 12:16:15 -0700742 __u64 w = 0;
743 int fbit;
Adrian Hunter0efe6b62015-07-17 19:33:50 +0300744
Kan Liangac0e2cd2016-03-30 12:16:15 -0700745 for_each_set_bit(fbit, format, PERF_PMU_FORMAT_BITS)
746 w |= (1ULL << fbit);
747
748 return w;
Adrian Hunter0efe6b62015-07-17 19:33:50 +0300749}
750
Jiri Olsacd82a322012-03-15 20:09:17 +0100751/*
Cody P Schafer688d4df2015-01-07 17:13:50 -0800752 * Term is a string term, and might be a param-term. Try to look up it's value
753 * in the remaining terms.
754 * - We have a term like "base-or-format-term=param-term",
755 * - We need to find the value supplied for "param-term" (with param-term named
756 * in a config string) later on in the term list.
757 */
758static int pmu_resolve_param_term(struct parse_events_term *term,
759 struct list_head *head_terms,
760 __u64 *value)
761{
762 struct parse_events_term *t;
763
764 list_for_each_entry(t, head_terms, list) {
765 if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
766 if (!strcmp(t->config, term->config)) {
767 t->used = true;
768 *value = t->val.num;
769 return 0;
770 }
771 }
772 }
773
Namhyung Kimbb963e12017-02-17 17:17:38 +0900774 if (verbose > 0)
Cody P Schafer688d4df2015-01-07 17:13:50 -0800775 printf("Required parameter '%s' not specified\n", term->config);
776
777 return -1;
778}
779
He Kuangffeb8832015-09-28 03:52:14 +0000780static char *pmu_formats_string(struct list_head *formats)
Jiri Olsae64b0202015-04-22 21:10:21 +0200781{
782 struct perf_pmu_format *format;
Masami Hiramatsu11db4e22016-05-10 14:47:44 +0900783 char *str = NULL;
784 struct strbuf buf = STRBUF_INIT;
Jiri Olsae64b0202015-04-22 21:10:21 +0200785 unsigned i = 0;
786
He Kuangffeb8832015-09-28 03:52:14 +0000787 if (!formats)
Jiri Olsae64b0202015-04-22 21:10:21 +0200788 return NULL;
789
790 /* sysfs exported terms */
He Kuangffeb8832015-09-28 03:52:14 +0000791 list_for_each_entry(format, formats, list)
Masami Hiramatsu11db4e22016-05-10 14:47:44 +0900792 if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
793 goto error;
Jiri Olsae64b0202015-04-22 21:10:21 +0200794
He Kuangffeb8832015-09-28 03:52:14 +0000795 str = strbuf_detach(&buf, NULL);
Masami Hiramatsu11db4e22016-05-10 14:47:44 +0900796error:
He Kuangffeb8832015-09-28 03:52:14 +0000797 strbuf_release(&buf);
Jiri Olsae64b0202015-04-22 21:10:21 +0200798
Jiri Olsae64b0202015-04-22 21:10:21 +0200799 return str;
Jiri Olsae64b0202015-04-22 21:10:21 +0200800}
801
Cody P Schafer688d4df2015-01-07 17:13:50 -0800802/*
Jiri Olsacd82a322012-03-15 20:09:17 +0100803 * Setup one of config[12] attr members based on the
Cody P Schafer88aca8d2014-01-08 08:43:51 -0800804 * user input data - term parameter.
Jiri Olsacd82a322012-03-15 20:09:17 +0100805 */
806static int pmu_config_term(struct list_head *formats,
807 struct perf_event_attr *attr,
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300808 struct parse_events_term *term,
Cody P Schafer688d4df2015-01-07 17:13:50 -0800809 struct list_head *head_terms,
Jiri Olsae64b0202015-04-22 21:10:21 +0200810 bool zero, struct parse_events_error *err)
Jiri Olsacd82a322012-03-15 20:09:17 +0100811{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300812 struct perf_pmu_format *format;
Jiri Olsacd82a322012-03-15 20:09:17 +0100813 __u64 *vp;
Adrian Hunter0efe6b62015-07-17 19:33:50 +0300814 __u64 val, max_val;
Jiri Olsacd82a322012-03-15 20:09:17 +0100815
816 /*
Cody P Schafer688d4df2015-01-07 17:13:50 -0800817 * If this is a parameter we've already used for parameterized-eval,
818 * skip it in normal eval.
819 */
820 if (term->used)
821 return 0;
822
823 /*
Jiri Olsacd82a322012-03-15 20:09:17 +0100824 * Hardcoded terms should be already in, so nothing
825 * to be done for them.
826 */
827 if (parse_events__is_hardcoded_term(term))
828 return 0;
829
Jiri Olsacd82a322012-03-15 20:09:17 +0100830 format = pmu_find_format(formats, term->config);
Cody P Schafer688d4df2015-01-07 17:13:50 -0800831 if (!format) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900832 if (verbose > 0)
Cody P Schafer688d4df2015-01-07 17:13:50 -0800833 printf("Invalid event/parameter '%s'\n", term->config);
Jiri Olsae64b0202015-04-22 21:10:21 +0200834 if (err) {
He Kuangffeb8832015-09-28 03:52:14 +0000835 char *pmu_term = pmu_formats_string(formats);
836
Jiri Olsae64b0202015-04-22 21:10:21 +0200837 err->idx = term->err_term;
838 err->str = strdup("unknown term");
He Kuangffeb8832015-09-28 03:52:14 +0000839 err->help = parse_events_formats_error_string(pmu_term);
840 free(pmu_term);
Jiri Olsae64b0202015-04-22 21:10:21 +0200841 }
Jiri Olsacd82a322012-03-15 20:09:17 +0100842 return -EINVAL;
Cody P Schafer688d4df2015-01-07 17:13:50 -0800843 }
Jiri Olsacd82a322012-03-15 20:09:17 +0100844
845 switch (format->value) {
846 case PERF_PMU_FORMAT_VALUE_CONFIG:
847 vp = &attr->config;
848 break;
849 case PERF_PMU_FORMAT_VALUE_CONFIG1:
850 vp = &attr->config1;
851 break;
852 case PERF_PMU_FORMAT_VALUE_CONFIG2:
853 vp = &attr->config2;
854 break;
855 default:
856 return -EINVAL;
857 }
858
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200859 /*
Cody P Schafer688d4df2015-01-07 17:13:50 -0800860 * Either directly use a numeric term, or try to translate string terms
861 * using event parameters.
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200862 */
Jiri Olsa99e71382017-02-17 15:00:56 +0100863 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
864 if (term->no_value &&
865 bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) {
866 if (err) {
867 err->idx = term->err_val;
868 err->str = strdup("no value assigned for term");
869 }
870 return -EINVAL;
871 }
872
Cody P Schafer688d4df2015-01-07 17:13:50 -0800873 val = term->val.num;
Jiri Olsa99e71382017-02-17 15:00:56 +0100874 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
Cody P Schafer688d4df2015-01-07 17:13:50 -0800875 if (strcmp(term->val.str, "?")) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900876 if (verbose > 0) {
Cody P Schafer688d4df2015-01-07 17:13:50 -0800877 pr_info("Invalid sysfs entry %s=%s\n",
878 term->config, term->val.str);
Jiri Olsae64b0202015-04-22 21:10:21 +0200879 }
880 if (err) {
881 err->idx = term->err_val;
882 err->str = strdup("expected numeric value");
883 }
Cody P Schafer688d4df2015-01-07 17:13:50 -0800884 return -EINVAL;
885 }
886
887 if (pmu_resolve_param_term(term, head_terms, &val))
888 return -EINVAL;
889 } else
890 return -EINVAL;
891
Adrian Hunter0efe6b62015-07-17 19:33:50 +0300892 max_val = pmu_format_max_value(format->bits);
893 if (val > max_val) {
894 if (err) {
895 err->idx = term->err_val;
896 if (asprintf(&err->str,
897 "value too big for format, maximum is %llu",
898 (unsigned long long)max_val) < 0)
899 err->str = strdup("value too big for format");
900 return -EINVAL;
901 }
902 /*
903 * Assume we don't care if !err, in which case the value will be
904 * silently truncated.
905 */
906 }
907
Cody P Schafer688d4df2015-01-07 17:13:50 -0800908 pmu_format_value(format->bits, val, vp, zero);
Jiri Olsacd82a322012-03-15 20:09:17 +0100909 return 0;
910}
911
Jiri Olsacff7f952012-11-10 01:46:50 +0100912int perf_pmu__config_terms(struct list_head *formats,
913 struct perf_event_attr *attr,
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300914 struct list_head *head_terms,
Jiri Olsae64b0202015-04-22 21:10:21 +0200915 bool zero, struct parse_events_error *err)
Jiri Olsacd82a322012-03-15 20:09:17 +0100916{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -0300917 struct parse_events_term *term;
Jiri Olsacd82a322012-03-15 20:09:17 +0100918
Cody P Schafer688d4df2015-01-07 17:13:50 -0800919 list_for_each_entry(term, head_terms, list) {
Jiri Olsae64b0202015-04-22 21:10:21 +0200920 if (pmu_config_term(formats, attr, term, head_terms,
921 zero, err))
Jiri Olsacd82a322012-03-15 20:09:17 +0100922 return -EINVAL;
Cody P Schafer688d4df2015-01-07 17:13:50 -0800923 }
Jiri Olsacd82a322012-03-15 20:09:17 +0100924
925 return 0;
926}
927
928/*
929 * Configures event's 'attr' parameter based on the:
930 * 1) users input - specified in terms parameter
931 * 2) pmu format definitions - specified by pmu parameter
932 */
933int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
Jiri Olsae64b0202015-04-22 21:10:21 +0200934 struct list_head *head_terms,
935 struct parse_events_error *err)
Jiri Olsacd82a322012-03-15 20:09:17 +0100936{
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300937 bool zero = !!pmu->default_config;
938
Jiri Olsacd82a322012-03-15 20:09:17 +0100939 attr->type = pmu->type;
Jiri Olsae64b0202015-04-22 21:10:21 +0200940 return perf_pmu__config_terms(&pmu->format, attr, head_terms,
941 zero, err);
Jiri Olsacd82a322012-03-15 20:09:17 +0100942}
943
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300944static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
945 struct parse_events_term *term)
Zheng Yana6146d52012-06-15 14:31:41 +0800946{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300947 struct perf_pmu_alias *alias;
Zheng Yana6146d52012-06-15 14:31:41 +0800948 char *name;
949
950 if (parse_events__is_hardcoded_term(term))
951 return NULL;
952
953 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
954 if (term->val.num != 1)
955 return NULL;
956 if (pmu_find_format(&pmu->format, term->config))
957 return NULL;
958 name = term->config;
959 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
960 if (strcasecmp(term->config, "event"))
961 return NULL;
962 name = term->val.str;
963 } else {
964 return NULL;
965 }
966
967 list_for_each_entry(alias, &pmu->aliases, list) {
968 if (!strcasecmp(alias->name, name))
969 return alias;
970 }
971 return NULL;
972}
973
Stephane Eranian410136f2013-11-12 17:58:49 +0100974
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100975static int check_info_data(struct perf_pmu_alias *alias,
976 struct perf_pmu_info *info)
Stephane Eranian410136f2013-11-12 17:58:49 +0100977{
978 /*
979 * Only one term in event definition can
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100980 * define unit, scale and snapshot, fail
981 * if there's more than one.
Stephane Eranian410136f2013-11-12 17:58:49 +0100982 */
Arnaldo Carvalho de Melob30a7d12017-02-15 10:06:20 -0300983 if ((info->unit && alias->unit[0]) ||
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100984 (info->scale && alias->scale) ||
985 (info->snapshot && alias->snapshot))
Stephane Eranian410136f2013-11-12 17:58:49 +0100986 return -EINVAL;
987
Arnaldo Carvalho de Melob30a7d12017-02-15 10:06:20 -0300988 if (alias->unit[0])
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100989 info->unit = alias->unit;
Stephane Eranian410136f2013-11-12 17:58:49 +0100990
991 if (alias->scale)
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100992 info->scale = alias->scale;
993
994 if (alias->snapshot)
995 info->snapshot = alias->snapshot;
Stephane Eranian410136f2013-11-12 17:58:49 +0100996
997 return 0;
998}
999
Zheng Yana6146d52012-06-15 14:31:41 +08001000/*
1001 * Find alias in the terms list and replace it with the terms
1002 * defined for the alias
1003 */
Stephane Eranian410136f2013-11-12 17:58:49 +01001004int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
Matt Fleming46441bd2014-09-24 15:04:06 +01001005 struct perf_pmu_info *info)
Zheng Yana6146d52012-06-15 14:31:41 +08001006{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001007 struct parse_events_term *term, *h;
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -03001008 struct perf_pmu_alias *alias;
Zheng Yana6146d52012-06-15 14:31:41 +08001009 int ret;
1010
Matt Fleming044330c2014-11-21 10:31:12 +01001011 info->per_pkg = false;
1012
Stephane Eranian8a398892014-01-17 16:34:05 +01001013 /*
1014 * Mark unit and scale as not set
1015 * (different from default values, see below)
1016 */
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001017 info->unit = NULL;
1018 info->scale = 0.0;
1019 info->snapshot = false;
Andi Kleen37932c12017-03-20 13:17:08 -07001020 info->metric_expr = NULL;
Andi Kleen96284812017-03-20 13:17:10 -07001021 info->metric_name = NULL;
Stephane Eranian410136f2013-11-12 17:58:49 +01001022
Zheng Yana6146d52012-06-15 14:31:41 +08001023 list_for_each_entry_safe(term, h, head_terms, list) {
1024 alias = pmu_find_alias(pmu, term);
1025 if (!alias)
1026 continue;
1027 ret = pmu_alias_terms(alias, &term->list);
1028 if (ret)
1029 return ret;
Stephane Eranian410136f2013-11-12 17:58:49 +01001030
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001031 ret = check_info_data(alias, info);
Stephane Eranian410136f2013-11-12 17:58:49 +01001032 if (ret)
1033 return ret;
1034
Matt Fleming044330c2014-11-21 10:31:12 +01001035 if (alias->per_pkg)
1036 info->per_pkg = true;
Andi Kleen37932c12017-03-20 13:17:08 -07001037 info->metric_expr = alias->metric_expr;
Andi Kleen96284812017-03-20 13:17:10 -07001038 info->metric_name = alias->metric_name;
Matt Fleming044330c2014-11-21 10:31:12 +01001039
Zheng Yana6146d52012-06-15 14:31:41 +08001040 list_del(&term->list);
1041 free(term);
1042 }
Stephane Eranian8a398892014-01-17 16:34:05 +01001043
1044 /*
1045 * if no unit or scale foundin aliases, then
1046 * set defaults as for evsel
1047 * unit cannot left to NULL
1048 */
Matt Fleming46441bd2014-09-24 15:04:06 +01001049 if (info->unit == NULL)
1050 info->unit = "";
Stephane Eranian8a398892014-01-17 16:34:05 +01001051
Matt Fleming46441bd2014-09-24 15:04:06 +01001052 if (info->scale == 0.0)
1053 info->scale = 1.0;
Stephane Eranian8a398892014-01-17 16:34:05 +01001054
Zheng Yana6146d52012-06-15 14:31:41 +08001055 return 0;
1056}
1057
Jiri Olsacd82a322012-03-15 20:09:17 +01001058int perf_pmu__new_format(struct list_head *list, char *name,
1059 int config, unsigned long *bits)
1060{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -03001061 struct perf_pmu_format *format;
Jiri Olsacd82a322012-03-15 20:09:17 +01001062
1063 format = zalloc(sizeof(*format));
1064 if (!format)
1065 return -ENOMEM;
1066
1067 format->name = strdup(name);
1068 format->value = config;
1069 memcpy(format->bits, bits, sizeof(format->bits));
1070
1071 list_add_tail(&format->list, list);
1072 return 0;
1073}
1074
1075void perf_pmu__set_format(unsigned long *bits, long from, long to)
1076{
1077 long b;
1078
1079 if (!to)
1080 to = from;
1081
Sukadev Bhattiprolu15268132013-01-17 09:11:30 -08001082 memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
Jiri Olsacd82a322012-03-15 20:09:17 +01001083 for (b = from; b <= to; b++)
1084 set_bit(b, bits);
1085}
Andi Kleendc098b32013-04-20 11:02:29 -07001086
Cody P Schaferaaea3612015-01-07 17:13:51 -08001087static int sub_non_neg(int a, int b)
1088{
1089 if (b > a)
1090 return 0;
1091 return a - b;
1092}
1093
Andi Kleendc098b32013-04-20 11:02:29 -07001094static char *format_alias(char *buf, int len, struct perf_pmu *pmu,
1095 struct perf_pmu_alias *alias)
1096{
Cody P Schaferaaea3612015-01-07 17:13:51 -08001097 struct parse_events_term *term;
1098 int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name);
1099
1100 list_for_each_entry(term, &alias->terms, list) {
1101 if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
1102 used += snprintf(buf + used, sub_non_neg(len, used),
1103 ",%s=%s", term->config,
1104 term->val.str);
1105 }
1106
1107 if (sub_non_neg(len, used) > 0) {
1108 buf[used] = '/';
1109 used++;
1110 }
1111 if (sub_non_neg(len, used) > 0) {
1112 buf[used] = '\0';
1113 used++;
1114 } else
1115 buf[len - 1] = '\0';
1116
Andi Kleendc098b32013-04-20 11:02:29 -07001117 return buf;
1118}
1119
1120static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu,
1121 struct perf_pmu_alias *alias)
1122{
1123 snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name);
1124 return buf;
1125}
1126
Andi Kleendd5f1032016-09-15 15:24:50 -07001127struct sevent {
Andi Kleen08e60ed2016-09-15 15:24:43 -07001128 char *name;
1129 char *desc;
Andi Kleendd5f1032016-09-15 15:24:50 -07001130 char *topic;
Andi Kleenf2361022017-01-27 18:03:40 -08001131 char *str;
1132 char *pmu;
Andi Kleen7f372a62017-03-20 13:17:09 -07001133 char *metric_expr;
Andi Kleen96284812017-03-20 13:17:10 -07001134 char *metric_name;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001135};
1136
Andi Kleendd5f1032016-09-15 15:24:50 -07001137static int cmp_sevent(const void *a, const void *b)
Andi Kleendc098b32013-04-20 11:02:29 -07001138{
Andi Kleendd5f1032016-09-15 15:24:50 -07001139 const struct sevent *as = a;
1140 const struct sevent *bs = b;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001141
1142 /* Put extra events last */
1143 if (!!as->desc != !!bs->desc)
1144 return !!as->desc - !!bs->desc;
Andi Kleendd5f1032016-09-15 15:24:50 -07001145 if (as->topic && bs->topic) {
1146 int n = strcmp(as->topic, bs->topic);
1147
1148 if (n)
1149 return n;
1150 }
Andi Kleen08e60ed2016-09-15 15:24:43 -07001151 return strcmp(as->name, bs->name);
1152}
1153
1154static void wordwrap(char *s, int start, int max, int corr)
1155{
1156 int column = start;
1157 int n;
1158
1159 while (*s) {
1160 int wlen = strcspn(s, " \t");
1161
1162 if (column + wlen >= max && column > start) {
1163 printf("\n%*s", start, "");
1164 column = start + corr;
1165 }
1166 n = printf("%s%.*s", column > start ? " " : "", wlen, s);
1167 if (n <= 0)
1168 break;
1169 s += wlen;
1170 column += n;
Taeung Songaa4beb12017-04-07 23:24:20 +09001171 s = ltrim(s);
Andi Kleen08e60ed2016-09-15 15:24:43 -07001172 }
Andi Kleendc098b32013-04-20 11:02:29 -07001173}
1174
Sukadev Bhattiproluc8d68282016-09-15 15:24:48 -07001175void print_pmu_events(const char *event_glob, bool name_only, bool quiet_flag,
Andi Kleenbf874fc2017-03-20 13:17:11 -07001176 bool long_desc, bool details_flag)
Andi Kleendc098b32013-04-20 11:02:29 -07001177{
1178 struct perf_pmu *pmu;
1179 struct perf_pmu_alias *alias;
1180 char buf[1024];
1181 int printed = 0;
1182 int len, j;
Andi Kleendd5f1032016-09-15 15:24:50 -07001183 struct sevent *aliases;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001184 int numdesc = 0;
Andi Kleen61eb2eb2016-09-15 15:24:44 -07001185 int columns = pager_get_columns();
Andi Kleendd5f1032016-09-15 15:24:50 -07001186 char *topic = NULL;
Andi Kleendc098b32013-04-20 11:02:29 -07001187
1188 pmu = NULL;
1189 len = 0;
Adrian Hunter42634bc2014-10-23 13:45:10 +03001190 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
Andi Kleendc098b32013-04-20 11:02:29 -07001191 list_for_each_entry(alias, &pmu->aliases, list)
1192 len++;
Adrian Hunter42634bc2014-10-23 13:45:10 +03001193 if (pmu->selectable)
1194 len++;
1195 }
Andi Kleendd5f1032016-09-15 15:24:50 -07001196 aliases = zalloc(sizeof(struct sevent) * len);
Andi Kleendc098b32013-04-20 11:02:29 -07001197 if (!aliases)
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001198 goto out_enomem;
Andi Kleendc098b32013-04-20 11:02:29 -07001199 pmu = NULL;
1200 j = 0;
Adrian Hunter42634bc2014-10-23 13:45:10 +03001201 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
Andi Kleendc098b32013-04-20 11:02:29 -07001202 list_for_each_entry(alias, &pmu->aliases, list) {
Andi Kleen08e60ed2016-09-15 15:24:43 -07001203 char *name = alias->desc ? alias->name :
1204 format_alias(buf, sizeof(buf), pmu, alias);
Andi Kleendc098b32013-04-20 11:02:29 -07001205 bool is_cpu = !strcmp(pmu->name, "cpu");
1206
1207 if (event_glob != NULL &&
Andi Kleen38d14f02016-10-19 10:50:01 -07001208 !(strglobmatch_nocase(name, event_glob) ||
1209 (!is_cpu && strglobmatch_nocase(alias->name,
Andi Kleen67bdc352016-10-19 11:45:23 -07001210 event_glob)) ||
1211 (alias->topic &&
1212 strglobmatch_nocase(alias->topic, event_glob))))
Andi Kleendc098b32013-04-20 11:02:29 -07001213 continue;
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001214
Andi Kleen08e60ed2016-09-15 15:24:43 -07001215 if (is_cpu && !name_only && !alias->desc)
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001216 name = format_alias_or(buf, sizeof(buf), pmu, alias);
1217
Andi Kleen08e60ed2016-09-15 15:24:43 -07001218 aliases[j].name = name;
1219 if (is_cpu && !name_only && !alias->desc)
1220 aliases[j].name = format_alias_or(buf,
1221 sizeof(buf),
1222 pmu, alias);
1223 aliases[j].name = strdup(aliases[j].name);
1224 if (!aliases[j].name)
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001225 goto out_enomem;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001226
Sukadev Bhattiproluc8d68282016-09-15 15:24:48 -07001227 aliases[j].desc = long_desc ? alias->long_desc :
1228 alias->desc;
Andi Kleendd5f1032016-09-15 15:24:50 -07001229 aliases[j].topic = alias->topic;
Andi Kleenf2361022017-01-27 18:03:40 -08001230 aliases[j].str = alias->str;
1231 aliases[j].pmu = pmu->name;
Andi Kleen7f372a62017-03-20 13:17:09 -07001232 aliases[j].metric_expr = alias->metric_expr;
Andi Kleen96284812017-03-20 13:17:10 -07001233 aliases[j].metric_name = alias->metric_name;
Andi Kleendc098b32013-04-20 11:02:29 -07001234 j++;
1235 }
Arnaldo Carvalho de Melofa52cea2015-10-02 15:28:16 -03001236 if (pmu->selectable &&
1237 (event_glob == NULL || strglobmatch(pmu->name, event_glob))) {
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001238 char *s;
1239 if (asprintf(&s, "%s//", pmu->name) < 0)
1240 goto out_enomem;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001241 aliases[j].name = s;
Adrian Hunter42634bc2014-10-23 13:45:10 +03001242 j++;
1243 }
1244 }
Andi Kleendc098b32013-04-20 11:02:29 -07001245 len = j;
Andi Kleendd5f1032016-09-15 15:24:50 -07001246 qsort(aliases, len, sizeof(struct sevent), cmp_sevent);
Andi Kleendc098b32013-04-20 11:02:29 -07001247 for (j = 0; j < len; j++) {
Andi Kleen15b22ed2017-01-27 18:03:38 -08001248 /* Skip duplicates */
1249 if (j > 0 && !strcmp(aliases[j].name, aliases[j - 1].name))
1250 continue;
Andi Kleendc098b32013-04-20 11:02:29 -07001251 if (name_only) {
Andi Kleen08e60ed2016-09-15 15:24:43 -07001252 printf("%s ", aliases[j].name);
Andi Kleendc098b32013-04-20 11:02:29 -07001253 continue;
1254 }
Andi Kleen1c5f01f2016-09-15 15:24:45 -07001255 if (aliases[j].desc && !quiet_flag) {
Andi Kleen08e60ed2016-09-15 15:24:43 -07001256 if (numdesc++ == 0)
1257 printf("\n");
Andi Kleendd5f1032016-09-15 15:24:50 -07001258 if (aliases[j].topic && (!topic ||
1259 strcmp(topic, aliases[j].topic))) {
1260 printf("%s%s:\n", topic ? "\n" : "",
1261 aliases[j].topic);
1262 topic = aliases[j].topic;
1263 }
Andi Kleen08e60ed2016-09-15 15:24:43 -07001264 printf(" %-50s\n", aliases[j].name);
1265 printf("%*s", 8, "[");
1266 wordwrap(aliases[j].desc, 8, columns, 0);
1267 printf("]\n");
Andi Kleenbf874fc2017-03-20 13:17:11 -07001268 if (details_flag) {
Andi Kleen7f372a62017-03-20 13:17:09 -07001269 printf("%*s%s/%s/ ", 8, "", aliases[j].pmu, aliases[j].str);
Andi Kleen96284812017-03-20 13:17:10 -07001270 if (aliases[j].metric_name)
1271 printf(" MetricName: %s", aliases[j].metric_name);
Andi Kleen7f372a62017-03-20 13:17:09 -07001272 if (aliases[j].metric_expr)
1273 printf(" MetricExpr: %s", aliases[j].metric_expr);
1274 putchar('\n');
1275 }
Andi Kleen08e60ed2016-09-15 15:24:43 -07001276 } else
1277 printf(" %-50s [Kernel PMU event]\n", aliases[j].name);
Andi Kleendc098b32013-04-20 11:02:29 -07001278 printed++;
1279 }
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03001280 if (printed && pager_in_use())
Andi Kleendc098b32013-04-20 11:02:29 -07001281 printf("\n");
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001282out_free:
1283 for (j = 0; j < len; j++)
Andi Kleen08e60ed2016-09-15 15:24:43 -07001284 zfree(&aliases[j].name);
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001285 zfree(&aliases);
1286 return;
1287
1288out_enomem:
1289 printf("FATAL: not enough memory to print PMU events\n");
1290 if (aliases)
1291 goto out_free;
Andi Kleendc098b32013-04-20 11:02:29 -07001292}
Andi Kleen4cabc3d2013-08-21 16:47:26 -07001293
1294bool pmu_have_event(const char *pname, const char *name)
1295{
1296 struct perf_pmu *pmu;
1297 struct perf_pmu_alias *alias;
1298
1299 pmu = NULL;
1300 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1301 if (strcmp(pname, pmu->name))
1302 continue;
1303 list_for_each_entry(alias, &pmu->aliases, list)
1304 if (!strcmp(alias->name, name))
1305 return true;
1306 }
1307 return false;
1308}
Adrian Hunter7d4bdab2014-07-31 09:00:50 +03001309
1310static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
1311{
1312 struct stat st;
1313 char path[PATH_MAX];
1314 const char *sysfs;
1315
1316 sysfs = sysfs__mountpoint();
1317 if (!sysfs)
1318 return NULL;
1319
1320 snprintf(path, PATH_MAX,
1321 "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name);
1322
1323 if (stat(path, &st) < 0)
1324 return NULL;
1325
1326 return fopen(path, "r");
1327}
1328
1329int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
1330 ...)
1331{
1332 va_list args;
1333 FILE *file;
1334 int ret = EOF;
1335
1336 va_start(args, fmt);
1337 file = perf_pmu__open_file(pmu, name);
1338 if (file) {
1339 ret = vfscanf(file, fmt, args);
1340 fclose(file);
1341 }
1342 va_end(args);
1343 return ret;
1344}