blob: bca1844594d0028daea56d6dac42311ef9e6e14b [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>
Jiri Olsacd82a322012-03-15 20:09:17 +01005#include <unistd.h>
6#include <stdio.h>
Adrian Hunterdc0a6202014-07-31 09:00:49 +03007#include <stdbool.h>
Adrian Hunter7d4bdab2014-07-31 09:00:50 +03008#include <stdarg.h>
Jiri Olsacd82a322012-03-15 20:09:17 +01009#include <dirent.h>
Borislav Petkovcd0cfad2013-12-09 17:14:24 +010010#include <api/fs/fs.h>
Stephane Eranian410136f2013-11-12 17:58:49 +010011#include <locale.h>
Jiri Olsacd82a322012-03-15 20:09:17 +010012#include "util.h"
13#include "pmu.h"
14#include "parse-events.h"
Yan, Zheng7ae92e72012-09-10 15:53:50 +080015#include "cpumap.h"
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -070016#include "header.h"
17#include "pmu-events/pmu-events.h"
Andi Kleen61eb2eb2016-09-15 15:24:44 -070018#include "cache.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030019#include "string2.h"
Jiri Olsacd82a322012-03-15 20:09:17 +010020
Arnaldo Carvalho de Meloab1bf6532013-01-18 17:05:09 -030021struct perf_pmu_format {
22 char *name;
23 int value;
24 DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
25 struct list_head list;
26};
27
Robert Richter50a96672012-08-16 21:10:24 +020028#define EVENT_SOURCE_DEVICE_PATH "/bus/event_source/devices/"
29
Jiri Olsacd82a322012-03-15 20:09:17 +010030int perf_pmu_parse(struct list_head *list, char *name);
31extern FILE *perf_pmu_in;
32
33static LIST_HEAD(pmus);
34
35/*
36 * Parse & process all the sysfs attributes located under
37 * the directory specified in 'dir' parameter.
38 */
Jiri Olsacff7f952012-11-10 01:46:50 +010039int perf_pmu__format_parse(char *dir, struct list_head *head)
Jiri Olsacd82a322012-03-15 20:09:17 +010040{
41 struct dirent *evt_ent;
42 DIR *format_dir;
43 int ret = 0;
44
45 format_dir = opendir(dir);
46 if (!format_dir)
47 return -EINVAL;
48
49 while (!ret && (evt_ent = readdir(format_dir))) {
50 char path[PATH_MAX];
51 char *name = evt_ent->d_name;
52 FILE *file;
53
54 if (!strcmp(name, ".") || !strcmp(name, ".."))
55 continue;
56
57 snprintf(path, PATH_MAX, "%s/%s", dir, name);
58
59 ret = -EINVAL;
60 file = fopen(path, "r");
61 if (!file)
62 break;
63
64 perf_pmu_in = file;
65 ret = perf_pmu_parse(head, name);
66 fclose(file);
67 }
68
69 closedir(format_dir);
70 return ret;
71}
72
73/*
74 * Reading/parsing the default pmu format definition, which should be
75 * located at:
76 * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
77 */
Adrian Hunterb6b96fb2013-07-04 16:20:25 +030078static int pmu_format(const char *name, struct list_head *format)
Jiri Olsacd82a322012-03-15 20:09:17 +010079{
80 struct stat st;
81 char path[PATH_MAX];
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -030082 const char *sysfs = sysfs__mountpoint();
Jiri Olsacd82a322012-03-15 20:09:17 +010083
Jiri Olsacd82a322012-03-15 20:09:17 +010084 if (!sysfs)
85 return -1;
86
87 snprintf(path, PATH_MAX,
Robert Richter50a96672012-08-16 21:10:24 +020088 "%s" EVENT_SOURCE_DEVICE_PATH "%s/format", sysfs, name);
Jiri Olsacd82a322012-03-15 20:09:17 +010089
90 if (stat(path, &st) < 0)
Robert Richter9bc8f9f2012-06-14 22:38:37 +020091 return 0; /* no error if format does not exist */
Jiri Olsacd82a322012-03-15 20:09:17 +010092
Jiri Olsacff7f952012-11-10 01:46:50 +010093 if (perf_pmu__format_parse(path, format))
Jiri Olsacd82a322012-03-15 20:09:17 +010094 return -1;
95
96 return 0;
97}
98
Andi Kleend02fc6b2017-01-03 07:08:23 -080099static int convert_scale(const char *scale, char **end, double *sval)
100{
101 char *lc;
102 int ret = 0;
103
104 /*
105 * save current locale
106 */
107 lc = setlocale(LC_NUMERIC, NULL);
108
109 /*
110 * The lc string may be allocated in static storage,
111 * so get a dynamic copy to make it survive setlocale
112 * call below.
113 */
114 lc = strdup(lc);
115 if (!lc) {
116 ret = -ENOMEM;
117 goto out;
118 }
119
120 /*
121 * force to C locale to ensure kernel
122 * scale string is converted correctly.
123 * kernel uses default C locale.
124 */
125 setlocale(LC_NUMERIC, "C");
126
127 *sval = strtod(scale, end);
128
129out:
130 /* restore locale */
131 setlocale(LC_NUMERIC, lc);
132 free(lc);
133 return ret;
134}
135
Stephane Eranian410136f2013-11-12 17:58:49 +0100136static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *name)
137{
138 struct stat st;
139 ssize_t sret;
140 char scale[128];
141 int fd, ret = -1;
142 char path[PATH_MAX];
Stephane Eranian410136f2013-11-12 17:58:49 +0100143
144 snprintf(path, PATH_MAX, "%s/%s.scale", dir, name);
145
146 fd = open(path, O_RDONLY);
147 if (fd == -1)
148 return -1;
149
150 if (fstat(fd, &st) < 0)
151 goto error;
152
153 sret = read(fd, scale, sizeof(scale)-1);
154 if (sret < 0)
155 goto error;
156
Madhavan Srinivasan9ecae062015-05-31 11:36:23 +0530157 if (scale[sret - 1] == '\n')
158 scale[sret - 1] = '\0';
159 else
160 scale[sret] = '\0';
161
Andi Kleend02fc6b2017-01-03 07:08:23 -0800162 ret = convert_scale(scale, NULL, &alias->scale);
Stephane Eranian410136f2013-11-12 17:58:49 +0100163error:
164 close(fd);
165 return ret;
166}
167
168static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *name)
169{
170 char path[PATH_MAX];
171 ssize_t sret;
172 int fd;
173
174 snprintf(path, PATH_MAX, "%s/%s.unit", dir, name);
175
176 fd = open(path, O_RDONLY);
177 if (fd == -1)
178 return -1;
179
Markus Trippelsdorfd85ce832015-12-14 16:44:40 +0100180 sret = read(fd, alias->unit, UNIT_MAX_LEN);
Stephane Eranian410136f2013-11-12 17:58:49 +0100181 if (sret < 0)
182 goto error;
183
184 close(fd);
185
Madhavan Srinivasan9ecae062015-05-31 11:36:23 +0530186 if (alias->unit[sret - 1] == '\n')
187 alias->unit[sret - 1] = '\0';
188 else
189 alias->unit[sret] = '\0';
Stephane Eranian410136f2013-11-12 17:58:49 +0100190
191 return 0;
192error:
193 close(fd);
194 alias->unit[0] = '\0';
195 return -1;
196}
197
Matt Fleming044330c2014-11-21 10:31:12 +0100198static int
199perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, char *dir, char *name)
200{
201 char path[PATH_MAX];
202 int fd;
203
204 snprintf(path, PATH_MAX, "%s/%s.per-pkg", dir, name);
205
206 fd = open(path, O_RDONLY);
207 if (fd == -1)
208 return -1;
209
210 close(fd);
211
212 alias->per_pkg = true;
213 return 0;
214}
215
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100216static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias,
217 char *dir, char *name)
218{
219 char path[PATH_MAX];
220 int fd;
221
222 snprintf(path, PATH_MAX, "%s/%s.snapshot", dir, name);
223
224 fd = open(path, O_RDONLY);
225 if (fd == -1)
226 return -1;
227
228 alias->snapshot = true;
229 close(fd);
230 return 0;
231}
232
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700233static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name,
Andi Kleenfedb2b52017-01-27 18:03:37 -0800234 char *desc, char *val,
235 char *long_desc, char *topic,
Andi Kleen00636c32017-03-20 13:17:07 -0700236 char *unit, char *perpkg,
Andi Kleen96284812017-03-20 13:17:10 -0700237 char *metric_expr,
238 char *metric_name)
Zheng Yana6146d52012-06-15 14:31:41 +0800239{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300240 struct perf_pmu_alias *alias;
Zheng Yana6146d52012-06-15 14:31:41 +0800241 int ret;
Andi Kleenfedb2b52017-01-27 18:03:37 -0800242 int num;
Zheng Yana6146d52012-06-15 14:31:41 +0800243
Zheng Yana6146d52012-06-15 14:31:41 +0800244 alias = malloc(sizeof(*alias));
245 if (!alias)
246 return -ENOMEM;
247
248 INIT_LIST_HEAD(&alias->terms);
Stephane Eranian410136f2013-11-12 17:58:49 +0100249 alias->scale = 1.0;
250 alias->unit[0] = '\0';
Matt Fleming044330c2014-11-21 10:31:12 +0100251 alias->per_pkg = false;
Stephane Eranian84530922016-01-06 19:50:01 +0100252 alias->snapshot = false;
Stephane Eranian410136f2013-11-12 17:58:49 +0100253
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700254 ret = parse_events_terms(&alias->terms, val);
Zheng Yana6146d52012-06-15 14:31:41 +0800255 if (ret) {
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700256 pr_err("Cannot parse alias %s: %d\n", val, ret);
Zheng Yana6146d52012-06-15 14:31:41 +0800257 free(alias);
258 return ret;
259 }
260
261 alias->name = strdup(name);
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700262 if (dir) {
263 /*
264 * load unit name and scale if available
265 */
266 perf_pmu__parse_unit(alias, dir, name);
267 perf_pmu__parse_scale(alias, dir, name);
268 perf_pmu__parse_per_pkg(alias, dir, name);
269 perf_pmu__parse_snapshot(alias, dir, name);
270 }
Stephane Eranian410136f2013-11-12 17:58:49 +0100271
Andi Kleen00636c32017-03-20 13:17:07 -0700272 alias->metric_expr = metric_expr ? strdup(metric_expr) : NULL;
Andi Kleen96284812017-03-20 13:17:10 -0700273 alias->metric_name = metric_name ? strdup(metric_name): NULL;
Andi Kleen08e60ed2016-09-15 15:24:43 -0700274 alias->desc = desc ? strdup(desc) : NULL;
Sukadev Bhattiproluc8d68282016-09-15 15:24:48 -0700275 alias->long_desc = long_desc ? strdup(long_desc) :
276 desc ? strdup(desc) : NULL;
Andi Kleendd5f1032016-09-15 15:24:50 -0700277 alias->topic = topic ? strdup(topic) : NULL;
Andi Kleenfedb2b52017-01-27 18:03:37 -0800278 if (unit) {
279 if (convert_scale(unit, &unit, &alias->scale) < 0)
280 return -1;
281 snprintf(alias->unit, sizeof(alias->unit), "%s", unit);
282 }
283 alias->per_pkg = perpkg && sscanf(perpkg, "%d", &num) == 1 && num == 1;
Andi Kleenf2361022017-01-27 18:03:40 -0800284 alias->str = strdup(val);
285
Zheng Yana6146d52012-06-15 14:31:41 +0800286 list_add_tail(&alias->list, list);
Stephane Eranian410136f2013-11-12 17:58:49 +0100287
Zheng Yana6146d52012-06-15 14:31:41 +0800288 return 0;
289}
290
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700291static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file)
292{
293 char buf[256];
294 int ret;
295
296 ret = fread(buf, 1, sizeof(buf), file);
297 if (ret == 0)
298 return -EINVAL;
299
300 buf[ret] = 0;
301
Andi Kleenfedb2b52017-01-27 18:03:37 -0800302 return __perf_pmu__new_alias(list, dir, name, NULL, buf, NULL, NULL, NULL,
Andi Kleen96284812017-03-20 13:17:10 -0700303 NULL, NULL, NULL);
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700304}
305
Matt Fleming46441bd2014-09-24 15:04:06 +0100306static inline bool pmu_alias_info_file(char *name)
307{
308 size_t len;
309
310 len = strlen(name);
311 if (len > 5 && !strcmp(name + len - 5, ".unit"))
312 return true;
313 if (len > 6 && !strcmp(name + len - 6, ".scale"))
314 return true;
Matt Fleming044330c2014-11-21 10:31:12 +0100315 if (len > 8 && !strcmp(name + len - 8, ".per-pkg"))
316 return true;
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100317 if (len > 9 && !strcmp(name + len - 9, ".snapshot"))
318 return true;
Matt Fleming46441bd2014-09-24 15:04:06 +0100319
320 return false;
321}
322
Zheng Yana6146d52012-06-15 14:31:41 +0800323/*
324 * Process all the sysfs attributes located under the directory
325 * specified in 'dir' parameter.
326 */
327static int pmu_aliases_parse(char *dir, struct list_head *head)
328{
329 struct dirent *evt_ent;
330 DIR *event_dir;
Zheng Yana6146d52012-06-15 14:31:41 +0800331
332 event_dir = opendir(dir);
333 if (!event_dir)
334 return -EINVAL;
335
Andi Kleen940db6d2016-02-17 14:44:55 -0800336 while ((evt_ent = readdir(event_dir))) {
Zheng Yana6146d52012-06-15 14:31:41 +0800337 char path[PATH_MAX];
338 char *name = evt_ent->d_name;
339 FILE *file;
340
341 if (!strcmp(name, ".") || !strcmp(name, ".."))
342 continue;
343
Stephane Eranian410136f2013-11-12 17:58:49 +0100344 /*
Matt Fleming46441bd2014-09-24 15:04:06 +0100345 * skip info files parsed in perf_pmu__new_alias()
Stephane Eranian410136f2013-11-12 17:58:49 +0100346 */
Matt Fleming46441bd2014-09-24 15:04:06 +0100347 if (pmu_alias_info_file(name))
Stephane Eranian410136f2013-11-12 17:58:49 +0100348 continue;
349
Zheng Yana6146d52012-06-15 14:31:41 +0800350 snprintf(path, PATH_MAX, "%s/%s", dir, name);
351
Zheng Yana6146d52012-06-15 14:31:41 +0800352 file = fopen(path, "r");
Andi Kleen940db6d2016-02-17 14:44:55 -0800353 if (!file) {
354 pr_debug("Cannot open %s\n", path);
355 continue;
356 }
Stephane Eranian410136f2013-11-12 17:58:49 +0100357
Andi Kleen940db6d2016-02-17 14:44:55 -0800358 if (perf_pmu__new_alias(head, dir, name, file) < 0)
359 pr_debug("Cannot set up %s\n", name);
Zheng Yana6146d52012-06-15 14:31:41 +0800360 fclose(file);
361 }
362
363 closedir(event_dir);
Andi Kleen940db6d2016-02-17 14:44:55 -0800364 return 0;
Zheng Yana6146d52012-06-15 14:31:41 +0800365}
366
367/*
368 * Reading the pmu event aliases definition, which should be located at:
369 * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
370 */
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300371static int pmu_aliases(const char *name, struct list_head *head)
Zheng Yana6146d52012-06-15 14:31:41 +0800372{
373 struct stat st;
374 char path[PATH_MAX];
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -0300375 const char *sysfs = sysfs__mountpoint();
Zheng Yana6146d52012-06-15 14:31:41 +0800376
Zheng Yana6146d52012-06-15 14:31:41 +0800377 if (!sysfs)
378 return -1;
379
380 snprintf(path, PATH_MAX,
381 "%s/bus/event_source/devices/%s/events", sysfs, name);
382
383 if (stat(path, &st) < 0)
Jiri Olsa3fded962012-10-10 14:53:16 +0200384 return 0; /* no error if 'events' does not exist */
Zheng Yana6146d52012-06-15 14:31:41 +0800385
386 if (pmu_aliases_parse(path, head))
387 return -1;
388
389 return 0;
390}
391
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300392static int pmu_alias_terms(struct perf_pmu_alias *alias,
Zheng Yana6146d52012-06-15 14:31:41 +0800393 struct list_head *terms)
394{
Jiri Olsa7c2f8162014-04-16 20:49:02 +0200395 struct parse_events_term *term, *cloned;
Zheng Yana6146d52012-06-15 14:31:41 +0800396 LIST_HEAD(list);
397 int ret;
398
399 list_for_each_entry(term, &alias->terms, list) {
Jiri Olsa7c2f8162014-04-16 20:49:02 +0200400 ret = parse_events_term__clone(&cloned, term);
Zheng Yana6146d52012-06-15 14:31:41 +0800401 if (ret) {
Arnaldo Carvalho de Melo682dc242016-02-12 16:48:00 -0300402 parse_events_terms__purge(&list);
Zheng Yana6146d52012-06-15 14:31:41 +0800403 return ret;
404 }
Jiri Olsa7c2f8162014-04-16 20:49:02 +0200405 list_add_tail(&cloned->list, &list);
Zheng Yana6146d52012-06-15 14:31:41 +0800406 }
407 list_splice(&list, terms);
408 return 0;
409}
410
Jiri Olsacd82a322012-03-15 20:09:17 +0100411/*
412 * Reading/parsing the default pmu type value, which should be
413 * located at:
414 * /sys/bus/event_source/devices/<dev>/type as sysfs attribute.
415 */
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300416static int pmu_type(const char *name, __u32 *type)
Jiri Olsacd82a322012-03-15 20:09:17 +0100417{
418 struct stat st;
419 char path[PATH_MAX];
Jiri Olsacd82a322012-03-15 20:09:17 +0100420 FILE *file;
421 int ret = 0;
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -0300422 const char *sysfs = sysfs__mountpoint();
Jiri Olsacd82a322012-03-15 20:09:17 +0100423
Jiri Olsacd82a322012-03-15 20:09:17 +0100424 if (!sysfs)
425 return -1;
426
427 snprintf(path, PATH_MAX,
Robert Richter50a96672012-08-16 21:10:24 +0200428 "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name);
Jiri Olsacd82a322012-03-15 20:09:17 +0100429
430 if (stat(path, &st) < 0)
431 return -1;
432
433 file = fopen(path, "r");
434 if (!file)
435 return -EINVAL;
436
437 if (1 != fscanf(file, "%u", type))
438 ret = -1;
439
440 fclose(file);
441 return ret;
442}
443
Robert Richter50a96672012-08-16 21:10:24 +0200444/* Add all pmus in sysfs to pmu list: */
445static void pmu_read_sysfs(void)
446{
447 char path[PATH_MAX];
Robert Richter50a96672012-08-16 21:10:24 +0200448 DIR *dir;
449 struct dirent *dent;
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -0300450 const char *sysfs = sysfs__mountpoint();
Robert Richter50a96672012-08-16 21:10:24 +0200451
Robert Richter50a96672012-08-16 21:10:24 +0200452 if (!sysfs)
453 return;
454
455 snprintf(path, PATH_MAX,
456 "%s" EVENT_SOURCE_DEVICE_PATH, sysfs);
457
458 dir = opendir(path);
459 if (!dir)
460 return;
461
462 while ((dent = readdir(dir))) {
463 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
464 continue;
465 /* add to static LIST_HEAD(pmus): */
466 perf_pmu__find(dent->d_name);
467 }
468
469 closedir(dir);
470}
471
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300472static struct cpu_map *pmu_cpumask(const char *name)
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800473{
474 struct stat st;
475 char path[PATH_MAX];
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800476 FILE *file;
477 struct cpu_map *cpus;
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -0300478 const char *sysfs = sysfs__mountpoint();
Mark Rutland7e3fcffe2016-09-08 11:21:52 +0100479 const char *templates[] = {
480 "%s/bus/event_source/devices/%s/cpumask",
481 "%s/bus/event_source/devices/%s/cpus",
482 NULL
483 };
484 const char **template;
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800485
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800486 if (!sysfs)
487 return NULL;
488
Mark Rutland7e3fcffe2016-09-08 11:21:52 +0100489 for (template = templates; *template; template++) {
490 snprintf(path, PATH_MAX, *template, sysfs, name);
491 if (stat(path, &st) == 0)
492 break;
493 }
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800494
Mark Rutland7e3fcffe2016-09-08 11:21:52 +0100495 if (!*template)
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800496 return NULL;
497
498 file = fopen(path, "r");
499 if (!file)
500 return NULL;
501
502 cpus = cpu_map__read(file);
503 fclose(file);
504 return cpus;
505}
506
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700507/*
508 * Return the CPU id as a raw string.
509 *
510 * Each architecture should provide a more precise id string that
511 * can be use to match the architecture's "mapfile".
512 */
513char * __weak get_cpuid_str(void)
514{
515 return NULL;
516}
517
518/*
519 * From the pmu_events_map, find the table of PMU events that corresponds
520 * to the current running CPU. Then, add all PMU events from that table
521 * as aliases.
522 */
Andi Kleenfedb2b52017-01-27 18:03:37 -0800523static void pmu_add_cpu_aliases(struct list_head *head, const char *name)
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700524{
525 int i;
526 struct pmu_events_map *map;
527 struct pmu_event *pe;
528 char *cpuid;
Andi Kleenfb967062016-10-13 14:15:24 -0700529 static bool printed;
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700530
Andi Kleenfc06e2a2016-09-15 15:24:46 -0700531 cpuid = getenv("PERF_CPUID");
532 if (cpuid)
533 cpuid = strdup(cpuid);
534 if (!cpuid)
535 cpuid = get_cpuid_str();
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700536 if (!cpuid)
537 return;
538
Andi Kleenfb967062016-10-13 14:15:24 -0700539 if (!printed) {
540 pr_debug("Using CPUID %s\n", cpuid);
541 printed = true;
542 }
Andi Kleenfc06e2a2016-09-15 15:24:46 -0700543
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700544 i = 0;
545 while (1) {
546 map = &pmu_events_map[i++];
547 if (!map->table)
548 goto out;
549
550 if (!strcmp(map->cpuid, cpuid))
551 break;
552 }
553
554 /*
555 * Found a matching PMU events table. Create aliases
556 */
557 i = 0;
558 while (1) {
Andi Kleenfedb2b52017-01-27 18:03:37 -0800559 const char *pname;
560
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700561 pe = &map->table[i++];
562 if (!pe->name)
563 break;
564
Andi Kleenfedb2b52017-01-27 18:03:37 -0800565 pname = pe->pmu ? pe->pmu : "cpu";
566 if (strncmp(pname, name, strlen(pname)))
567 continue;
568
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700569 /* need type casts to override 'const' */
570 __perf_pmu__new_alias(head, NULL, (char *)pe->name,
Sukadev Bhattiproluc8d68282016-09-15 15:24:48 -0700571 (char *)pe->desc, (char *)pe->event,
Andi Kleenfedb2b52017-01-27 18:03:37 -0800572 (char *)pe->long_desc, (char *)pe->topic,
Andi Kleen00636c32017-03-20 13:17:07 -0700573 (char *)pe->unit, (char *)pe->perpkg,
Andi Kleen96284812017-03-20 13:17:10 -0700574 (char *)pe->metric_expr,
575 (char *)pe->metric_name);
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700576 }
577
578out:
579 free(cpuid);
580}
581
Sukadev Bhattiproluc5de47f2015-06-10 00:25:07 -0700582struct perf_event_attr * __weak
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300583perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
584{
585 return NULL;
586}
587
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300588static struct perf_pmu *pmu_lookup(const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +0100589{
590 struct perf_pmu *pmu;
591 LIST_HEAD(format);
Zheng Yana6146d52012-06-15 14:31:41 +0800592 LIST_HEAD(aliases);
Jiri Olsacd82a322012-03-15 20:09:17 +0100593 __u32 type;
594
595 /*
596 * The pmu data we store & need consists of the pmu
597 * type value and format definitions. Load both right
598 * now.
599 */
600 if (pmu_format(name, &format))
601 return NULL;
602
Andi Kleen15b22ed2017-01-27 18:03:38 -0800603 /*
604 * Check the type first to avoid unnecessary work.
605 */
606 if (pmu_type(name, &type))
607 return NULL;
608
Jiri Olsa3fded962012-10-10 14:53:16 +0200609 if (pmu_aliases(name, &aliases))
610 return NULL;
611
Andi Kleenfedb2b52017-01-27 18:03:37 -0800612 pmu_add_cpu_aliases(&aliases, name);
Jiri Olsacd82a322012-03-15 20:09:17 +0100613 pmu = zalloc(sizeof(*pmu));
614 if (!pmu)
615 return NULL;
616
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800617 pmu->cpus = pmu_cpumask(name);
618
Jiri Olsacd82a322012-03-15 20:09:17 +0100619 INIT_LIST_HEAD(&pmu->format);
Zheng Yana6146d52012-06-15 14:31:41 +0800620 INIT_LIST_HEAD(&pmu->aliases);
Jiri Olsacd82a322012-03-15 20:09:17 +0100621 list_splice(&format, &pmu->format);
Zheng Yana6146d52012-06-15 14:31:41 +0800622 list_splice(&aliases, &pmu->aliases);
Jiri Olsacd82a322012-03-15 20:09:17 +0100623 pmu->name = strdup(name);
624 pmu->type = type;
Robert Richter9bc8f9f2012-06-14 22:38:37 +0200625 list_add_tail(&pmu->list, &pmus);
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300626
627 pmu->default_config = perf_pmu__get_default_config(pmu);
628
Jiri Olsacd82a322012-03-15 20:09:17 +0100629 return pmu;
630}
631
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300632static struct perf_pmu *pmu_find(const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +0100633{
634 struct perf_pmu *pmu;
635
636 list_for_each_entry(pmu, &pmus, list)
637 if (!strcmp(pmu->name, name))
638 return pmu;
639
640 return NULL;
641}
642
Robert Richter50a96672012-08-16 21:10:24 +0200643struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
644{
645 /*
646 * pmu iterator: If pmu is NULL, we start at the begin,
647 * otherwise return the next pmu. Returns NULL on end.
648 */
649 if (!pmu) {
650 pmu_read_sysfs();
651 pmu = list_prepare_entry(pmu, &pmus, list);
652 }
653 list_for_each_entry_continue(pmu, &pmus, list)
654 return pmu;
655 return NULL;
656}
657
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300658struct perf_pmu *perf_pmu__find(const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +0100659{
660 struct perf_pmu *pmu;
661
662 /*
663 * Once PMU is loaded it stays in the list,
664 * so we keep us from multiple reading/parsing
665 * the pmu format definitions.
666 */
667 pmu = pmu_find(name);
668 if (pmu)
669 return pmu;
670
671 return pmu_lookup(name);
672}
673
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300674static struct perf_pmu_format *
Adrian Hunter09ff6072015-07-17 19:33:49 +0300675pmu_find_format(struct list_head *formats, const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +0100676{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300677 struct perf_pmu_format *format;
Jiri Olsacd82a322012-03-15 20:09:17 +0100678
679 list_for_each_entry(format, formats, list)
680 if (!strcmp(format->name, name))
681 return format;
682
683 return NULL;
684}
685
Adrian Hunter09ff6072015-07-17 19:33:49 +0300686__u64 perf_pmu__format_bits(struct list_head *formats, const char *name)
687{
688 struct perf_pmu_format *format = pmu_find_format(formats, name);
689 __u64 bits = 0;
690 int fbit;
691
692 if (!format)
693 return 0;
694
695 for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
696 bits |= 1ULL << fbit;
697
698 return bits;
699}
700
Jiri Olsacd82a322012-03-15 20:09:17 +0100701/*
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300702 * Sets value based on the format definition (format parameter)
Jiri Olsacd82a322012-03-15 20:09:17 +0100703 * and unformated value (value parameter).
Jiri Olsacd82a322012-03-15 20:09:17 +0100704 */
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300705static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
706 bool zero)
Jiri Olsacd82a322012-03-15 20:09:17 +0100707{
708 unsigned long fbit, vbit;
Jiri Olsacd82a322012-03-15 20:09:17 +0100709
710 for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
711
712 if (!test_bit(fbit, format))
713 continue;
714
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300715 if (value & (1llu << vbit++))
716 *v |= (1llu << fbit);
717 else if (zero)
718 *v &= ~(1llu << fbit);
Jiri Olsacd82a322012-03-15 20:09:17 +0100719 }
Jiri Olsacd82a322012-03-15 20:09:17 +0100720}
721
Adrian Hunter0efe6b62015-07-17 19:33:50 +0300722static __u64 pmu_format_max_value(const unsigned long *format)
723{
Kan Liangac0e2cd2016-03-30 12:16:15 -0700724 __u64 w = 0;
725 int fbit;
Adrian Hunter0efe6b62015-07-17 19:33:50 +0300726
Kan Liangac0e2cd2016-03-30 12:16:15 -0700727 for_each_set_bit(fbit, format, PERF_PMU_FORMAT_BITS)
728 w |= (1ULL << fbit);
729
730 return w;
Adrian Hunter0efe6b62015-07-17 19:33:50 +0300731}
732
Jiri Olsacd82a322012-03-15 20:09:17 +0100733/*
Cody P Schafer688d4df2015-01-07 17:13:50 -0800734 * Term is a string term, and might be a param-term. Try to look up it's value
735 * in the remaining terms.
736 * - We have a term like "base-or-format-term=param-term",
737 * - We need to find the value supplied for "param-term" (with param-term named
738 * in a config string) later on in the term list.
739 */
740static int pmu_resolve_param_term(struct parse_events_term *term,
741 struct list_head *head_terms,
742 __u64 *value)
743{
744 struct parse_events_term *t;
745
746 list_for_each_entry(t, head_terms, list) {
747 if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
748 if (!strcmp(t->config, term->config)) {
749 t->used = true;
750 *value = t->val.num;
751 return 0;
752 }
753 }
754 }
755
Namhyung Kimbb963e12017-02-17 17:17:38 +0900756 if (verbose > 0)
Cody P Schafer688d4df2015-01-07 17:13:50 -0800757 printf("Required parameter '%s' not specified\n", term->config);
758
759 return -1;
760}
761
He Kuangffeb8832015-09-28 03:52:14 +0000762static char *pmu_formats_string(struct list_head *formats)
Jiri Olsae64b0202015-04-22 21:10:21 +0200763{
764 struct perf_pmu_format *format;
Masami Hiramatsu11db4e22016-05-10 14:47:44 +0900765 char *str = NULL;
766 struct strbuf buf = STRBUF_INIT;
Jiri Olsae64b0202015-04-22 21:10:21 +0200767 unsigned i = 0;
768
He Kuangffeb8832015-09-28 03:52:14 +0000769 if (!formats)
Jiri Olsae64b0202015-04-22 21:10:21 +0200770 return NULL;
771
772 /* sysfs exported terms */
He Kuangffeb8832015-09-28 03:52:14 +0000773 list_for_each_entry(format, formats, list)
Masami Hiramatsu11db4e22016-05-10 14:47:44 +0900774 if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
775 goto error;
Jiri Olsae64b0202015-04-22 21:10:21 +0200776
He Kuangffeb8832015-09-28 03:52:14 +0000777 str = strbuf_detach(&buf, NULL);
Masami Hiramatsu11db4e22016-05-10 14:47:44 +0900778error:
He Kuangffeb8832015-09-28 03:52:14 +0000779 strbuf_release(&buf);
Jiri Olsae64b0202015-04-22 21:10:21 +0200780
Jiri Olsae64b0202015-04-22 21:10:21 +0200781 return str;
Jiri Olsae64b0202015-04-22 21:10:21 +0200782}
783
Cody P Schafer688d4df2015-01-07 17:13:50 -0800784/*
Jiri Olsacd82a322012-03-15 20:09:17 +0100785 * Setup one of config[12] attr members based on the
Cody P Schafer88aca8d2014-01-08 08:43:51 -0800786 * user input data - term parameter.
Jiri Olsacd82a322012-03-15 20:09:17 +0100787 */
788static int pmu_config_term(struct list_head *formats,
789 struct perf_event_attr *attr,
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300790 struct parse_events_term *term,
Cody P Schafer688d4df2015-01-07 17:13:50 -0800791 struct list_head *head_terms,
Jiri Olsae64b0202015-04-22 21:10:21 +0200792 bool zero, struct parse_events_error *err)
Jiri Olsacd82a322012-03-15 20:09:17 +0100793{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300794 struct perf_pmu_format *format;
Jiri Olsacd82a322012-03-15 20:09:17 +0100795 __u64 *vp;
Adrian Hunter0efe6b62015-07-17 19:33:50 +0300796 __u64 val, max_val;
Jiri Olsacd82a322012-03-15 20:09:17 +0100797
798 /*
Cody P Schafer688d4df2015-01-07 17:13:50 -0800799 * If this is a parameter we've already used for parameterized-eval,
800 * skip it in normal eval.
801 */
802 if (term->used)
803 return 0;
804
805 /*
Jiri Olsacd82a322012-03-15 20:09:17 +0100806 * Hardcoded terms should be already in, so nothing
807 * to be done for them.
808 */
809 if (parse_events__is_hardcoded_term(term))
810 return 0;
811
Jiri Olsacd82a322012-03-15 20:09:17 +0100812 format = pmu_find_format(formats, term->config);
Cody P Schafer688d4df2015-01-07 17:13:50 -0800813 if (!format) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900814 if (verbose > 0)
Cody P Schafer688d4df2015-01-07 17:13:50 -0800815 printf("Invalid event/parameter '%s'\n", term->config);
Jiri Olsae64b0202015-04-22 21:10:21 +0200816 if (err) {
He Kuangffeb8832015-09-28 03:52:14 +0000817 char *pmu_term = pmu_formats_string(formats);
818
Jiri Olsae64b0202015-04-22 21:10:21 +0200819 err->idx = term->err_term;
820 err->str = strdup("unknown term");
He Kuangffeb8832015-09-28 03:52:14 +0000821 err->help = parse_events_formats_error_string(pmu_term);
822 free(pmu_term);
Jiri Olsae64b0202015-04-22 21:10:21 +0200823 }
Jiri Olsacd82a322012-03-15 20:09:17 +0100824 return -EINVAL;
Cody P Schafer688d4df2015-01-07 17:13:50 -0800825 }
Jiri Olsacd82a322012-03-15 20:09:17 +0100826
827 switch (format->value) {
828 case PERF_PMU_FORMAT_VALUE_CONFIG:
829 vp = &attr->config;
830 break;
831 case PERF_PMU_FORMAT_VALUE_CONFIG1:
832 vp = &attr->config1;
833 break;
834 case PERF_PMU_FORMAT_VALUE_CONFIG2:
835 vp = &attr->config2;
836 break;
837 default:
838 return -EINVAL;
839 }
840
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200841 /*
Cody P Schafer688d4df2015-01-07 17:13:50 -0800842 * Either directly use a numeric term, or try to translate string terms
843 * using event parameters.
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200844 */
Jiri Olsa99e71382017-02-17 15:00:56 +0100845 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
846 if (term->no_value &&
847 bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) {
848 if (err) {
849 err->idx = term->err_val;
850 err->str = strdup("no value assigned for term");
851 }
852 return -EINVAL;
853 }
854
Cody P Schafer688d4df2015-01-07 17:13:50 -0800855 val = term->val.num;
Jiri Olsa99e71382017-02-17 15:00:56 +0100856 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
Cody P Schafer688d4df2015-01-07 17:13:50 -0800857 if (strcmp(term->val.str, "?")) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900858 if (verbose > 0) {
Cody P Schafer688d4df2015-01-07 17:13:50 -0800859 pr_info("Invalid sysfs entry %s=%s\n",
860 term->config, term->val.str);
Jiri Olsae64b0202015-04-22 21:10:21 +0200861 }
862 if (err) {
863 err->idx = term->err_val;
864 err->str = strdup("expected numeric value");
865 }
Cody P Schafer688d4df2015-01-07 17:13:50 -0800866 return -EINVAL;
867 }
868
869 if (pmu_resolve_param_term(term, head_terms, &val))
870 return -EINVAL;
871 } else
872 return -EINVAL;
873
Adrian Hunter0efe6b62015-07-17 19:33:50 +0300874 max_val = pmu_format_max_value(format->bits);
875 if (val > max_val) {
876 if (err) {
877 err->idx = term->err_val;
878 if (asprintf(&err->str,
879 "value too big for format, maximum is %llu",
880 (unsigned long long)max_val) < 0)
881 err->str = strdup("value too big for format");
882 return -EINVAL;
883 }
884 /*
885 * Assume we don't care if !err, in which case the value will be
886 * silently truncated.
887 */
888 }
889
Cody P Schafer688d4df2015-01-07 17:13:50 -0800890 pmu_format_value(format->bits, val, vp, zero);
Jiri Olsacd82a322012-03-15 20:09:17 +0100891 return 0;
892}
893
Jiri Olsacff7f952012-11-10 01:46:50 +0100894int perf_pmu__config_terms(struct list_head *formats,
895 struct perf_event_attr *attr,
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300896 struct list_head *head_terms,
Jiri Olsae64b0202015-04-22 21:10:21 +0200897 bool zero, struct parse_events_error *err)
Jiri Olsacd82a322012-03-15 20:09:17 +0100898{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -0300899 struct parse_events_term *term;
Jiri Olsacd82a322012-03-15 20:09:17 +0100900
Cody P Schafer688d4df2015-01-07 17:13:50 -0800901 list_for_each_entry(term, head_terms, list) {
Jiri Olsae64b0202015-04-22 21:10:21 +0200902 if (pmu_config_term(formats, attr, term, head_terms,
903 zero, err))
Jiri Olsacd82a322012-03-15 20:09:17 +0100904 return -EINVAL;
Cody P Schafer688d4df2015-01-07 17:13:50 -0800905 }
Jiri Olsacd82a322012-03-15 20:09:17 +0100906
907 return 0;
908}
909
910/*
911 * Configures event's 'attr' parameter based on the:
912 * 1) users input - specified in terms parameter
913 * 2) pmu format definitions - specified by pmu parameter
914 */
915int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
Jiri Olsae64b0202015-04-22 21:10:21 +0200916 struct list_head *head_terms,
917 struct parse_events_error *err)
Jiri Olsacd82a322012-03-15 20:09:17 +0100918{
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300919 bool zero = !!pmu->default_config;
920
Jiri Olsacd82a322012-03-15 20:09:17 +0100921 attr->type = pmu->type;
Jiri Olsae64b0202015-04-22 21:10:21 +0200922 return perf_pmu__config_terms(&pmu->format, attr, head_terms,
923 zero, err);
Jiri Olsacd82a322012-03-15 20:09:17 +0100924}
925
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300926static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
927 struct parse_events_term *term)
Zheng Yana6146d52012-06-15 14:31:41 +0800928{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300929 struct perf_pmu_alias *alias;
Zheng Yana6146d52012-06-15 14:31:41 +0800930 char *name;
931
932 if (parse_events__is_hardcoded_term(term))
933 return NULL;
934
935 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
936 if (term->val.num != 1)
937 return NULL;
938 if (pmu_find_format(&pmu->format, term->config))
939 return NULL;
940 name = term->config;
941 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
942 if (strcasecmp(term->config, "event"))
943 return NULL;
944 name = term->val.str;
945 } else {
946 return NULL;
947 }
948
949 list_for_each_entry(alias, &pmu->aliases, list) {
950 if (!strcasecmp(alias->name, name))
951 return alias;
952 }
953 return NULL;
954}
955
Stephane Eranian410136f2013-11-12 17:58:49 +0100956
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100957static int check_info_data(struct perf_pmu_alias *alias,
958 struct perf_pmu_info *info)
Stephane Eranian410136f2013-11-12 17:58:49 +0100959{
960 /*
961 * Only one term in event definition can
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100962 * define unit, scale and snapshot, fail
963 * if there's more than one.
Stephane Eranian410136f2013-11-12 17:58:49 +0100964 */
Arnaldo Carvalho de Melob30a7d12017-02-15 10:06:20 -0300965 if ((info->unit && alias->unit[0]) ||
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100966 (info->scale && alias->scale) ||
967 (info->snapshot && alias->snapshot))
Stephane Eranian410136f2013-11-12 17:58:49 +0100968 return -EINVAL;
969
Arnaldo Carvalho de Melob30a7d12017-02-15 10:06:20 -0300970 if (alias->unit[0])
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100971 info->unit = alias->unit;
Stephane Eranian410136f2013-11-12 17:58:49 +0100972
973 if (alias->scale)
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100974 info->scale = alias->scale;
975
976 if (alias->snapshot)
977 info->snapshot = alias->snapshot;
Stephane Eranian410136f2013-11-12 17:58:49 +0100978
979 return 0;
980}
981
Zheng Yana6146d52012-06-15 14:31:41 +0800982/*
983 * Find alias in the terms list and replace it with the terms
984 * defined for the alias
985 */
Stephane Eranian410136f2013-11-12 17:58:49 +0100986int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
Matt Fleming46441bd2014-09-24 15:04:06 +0100987 struct perf_pmu_info *info)
Zheng Yana6146d52012-06-15 14:31:41 +0800988{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -0300989 struct parse_events_term *term, *h;
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300990 struct perf_pmu_alias *alias;
Zheng Yana6146d52012-06-15 14:31:41 +0800991 int ret;
992
Matt Fleming044330c2014-11-21 10:31:12 +0100993 info->per_pkg = false;
994
Stephane Eranian8a398892014-01-17 16:34:05 +0100995 /*
996 * Mark unit and scale as not set
997 * (different from default values, see below)
998 */
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100999 info->unit = NULL;
1000 info->scale = 0.0;
1001 info->snapshot = false;
Andi Kleen37932c12017-03-20 13:17:08 -07001002 info->metric_expr = NULL;
Andi Kleen96284812017-03-20 13:17:10 -07001003 info->metric_name = NULL;
Stephane Eranian410136f2013-11-12 17:58:49 +01001004
Zheng Yana6146d52012-06-15 14:31:41 +08001005 list_for_each_entry_safe(term, h, head_terms, list) {
1006 alias = pmu_find_alias(pmu, term);
1007 if (!alias)
1008 continue;
1009 ret = pmu_alias_terms(alias, &term->list);
1010 if (ret)
1011 return ret;
Stephane Eranian410136f2013-11-12 17:58:49 +01001012
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001013 ret = check_info_data(alias, info);
Stephane Eranian410136f2013-11-12 17:58:49 +01001014 if (ret)
1015 return ret;
1016
Matt Fleming044330c2014-11-21 10:31:12 +01001017 if (alias->per_pkg)
1018 info->per_pkg = true;
Andi Kleen37932c12017-03-20 13:17:08 -07001019 info->metric_expr = alias->metric_expr;
Andi Kleen96284812017-03-20 13:17:10 -07001020 info->metric_name = alias->metric_name;
Matt Fleming044330c2014-11-21 10:31:12 +01001021
Zheng Yana6146d52012-06-15 14:31:41 +08001022 list_del(&term->list);
1023 free(term);
1024 }
Stephane Eranian8a398892014-01-17 16:34:05 +01001025
1026 /*
1027 * if no unit or scale foundin aliases, then
1028 * set defaults as for evsel
1029 * unit cannot left to NULL
1030 */
Matt Fleming46441bd2014-09-24 15:04:06 +01001031 if (info->unit == NULL)
1032 info->unit = "";
Stephane Eranian8a398892014-01-17 16:34:05 +01001033
Matt Fleming46441bd2014-09-24 15:04:06 +01001034 if (info->scale == 0.0)
1035 info->scale = 1.0;
Stephane Eranian8a398892014-01-17 16:34:05 +01001036
Zheng Yana6146d52012-06-15 14:31:41 +08001037 return 0;
1038}
1039
Jiri Olsacd82a322012-03-15 20:09:17 +01001040int perf_pmu__new_format(struct list_head *list, char *name,
1041 int config, unsigned long *bits)
1042{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -03001043 struct perf_pmu_format *format;
Jiri Olsacd82a322012-03-15 20:09:17 +01001044
1045 format = zalloc(sizeof(*format));
1046 if (!format)
1047 return -ENOMEM;
1048
1049 format->name = strdup(name);
1050 format->value = config;
1051 memcpy(format->bits, bits, sizeof(format->bits));
1052
1053 list_add_tail(&format->list, list);
1054 return 0;
1055}
1056
1057void perf_pmu__set_format(unsigned long *bits, long from, long to)
1058{
1059 long b;
1060
1061 if (!to)
1062 to = from;
1063
Sukadev Bhattiprolu15268132013-01-17 09:11:30 -08001064 memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
Jiri Olsacd82a322012-03-15 20:09:17 +01001065 for (b = from; b <= to; b++)
1066 set_bit(b, bits);
1067}
Andi Kleendc098b32013-04-20 11:02:29 -07001068
Cody P Schaferaaea3612015-01-07 17:13:51 -08001069static int sub_non_neg(int a, int b)
1070{
1071 if (b > a)
1072 return 0;
1073 return a - b;
1074}
1075
Andi Kleendc098b32013-04-20 11:02:29 -07001076static char *format_alias(char *buf, int len, struct perf_pmu *pmu,
1077 struct perf_pmu_alias *alias)
1078{
Cody P Schaferaaea3612015-01-07 17:13:51 -08001079 struct parse_events_term *term;
1080 int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name);
1081
1082 list_for_each_entry(term, &alias->terms, list) {
1083 if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
1084 used += snprintf(buf + used, sub_non_neg(len, used),
1085 ",%s=%s", term->config,
1086 term->val.str);
1087 }
1088
1089 if (sub_non_neg(len, used) > 0) {
1090 buf[used] = '/';
1091 used++;
1092 }
1093 if (sub_non_neg(len, used) > 0) {
1094 buf[used] = '\0';
1095 used++;
1096 } else
1097 buf[len - 1] = '\0';
1098
Andi Kleendc098b32013-04-20 11:02:29 -07001099 return buf;
1100}
1101
1102static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu,
1103 struct perf_pmu_alias *alias)
1104{
1105 snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name);
1106 return buf;
1107}
1108
Andi Kleendd5f1032016-09-15 15:24:50 -07001109struct sevent {
Andi Kleen08e60ed2016-09-15 15:24:43 -07001110 char *name;
1111 char *desc;
Andi Kleendd5f1032016-09-15 15:24:50 -07001112 char *topic;
Andi Kleenf2361022017-01-27 18:03:40 -08001113 char *str;
1114 char *pmu;
Andi Kleen7f372a62017-03-20 13:17:09 -07001115 char *metric_expr;
Andi Kleen96284812017-03-20 13:17:10 -07001116 char *metric_name;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001117};
1118
Andi Kleendd5f1032016-09-15 15:24:50 -07001119static int cmp_sevent(const void *a, const void *b)
Andi Kleendc098b32013-04-20 11:02:29 -07001120{
Andi Kleendd5f1032016-09-15 15:24:50 -07001121 const struct sevent *as = a;
1122 const struct sevent *bs = b;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001123
1124 /* Put extra events last */
1125 if (!!as->desc != !!bs->desc)
1126 return !!as->desc - !!bs->desc;
Andi Kleendd5f1032016-09-15 15:24:50 -07001127 if (as->topic && bs->topic) {
1128 int n = strcmp(as->topic, bs->topic);
1129
1130 if (n)
1131 return n;
1132 }
Andi Kleen08e60ed2016-09-15 15:24:43 -07001133 return strcmp(as->name, bs->name);
1134}
1135
1136static void wordwrap(char *s, int start, int max, int corr)
1137{
1138 int column = start;
1139 int n;
1140
1141 while (*s) {
1142 int wlen = strcspn(s, " \t");
1143
1144 if (column + wlen >= max && column > start) {
1145 printf("\n%*s", start, "");
1146 column = start + corr;
1147 }
1148 n = printf("%s%.*s", column > start ? " " : "", wlen, s);
1149 if (n <= 0)
1150 break;
1151 s += wlen;
1152 column += n;
Taeung Songaa4beb12017-04-07 23:24:20 +09001153 s = ltrim(s);
Andi Kleen08e60ed2016-09-15 15:24:43 -07001154 }
Andi Kleendc098b32013-04-20 11:02:29 -07001155}
1156
Sukadev Bhattiproluc8d68282016-09-15 15:24:48 -07001157void print_pmu_events(const char *event_glob, bool name_only, bool quiet_flag,
Andi Kleenbf874fc2017-03-20 13:17:11 -07001158 bool long_desc, bool details_flag)
Andi Kleendc098b32013-04-20 11:02:29 -07001159{
1160 struct perf_pmu *pmu;
1161 struct perf_pmu_alias *alias;
1162 char buf[1024];
1163 int printed = 0;
1164 int len, j;
Andi Kleendd5f1032016-09-15 15:24:50 -07001165 struct sevent *aliases;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001166 int numdesc = 0;
Andi Kleen61eb2eb2016-09-15 15:24:44 -07001167 int columns = pager_get_columns();
Andi Kleendd5f1032016-09-15 15:24:50 -07001168 char *topic = NULL;
Andi Kleendc098b32013-04-20 11:02:29 -07001169
1170 pmu = NULL;
1171 len = 0;
Adrian Hunter42634bc2014-10-23 13:45:10 +03001172 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
Andi Kleendc098b32013-04-20 11:02:29 -07001173 list_for_each_entry(alias, &pmu->aliases, list)
1174 len++;
Adrian Hunter42634bc2014-10-23 13:45:10 +03001175 if (pmu->selectable)
1176 len++;
1177 }
Andi Kleendd5f1032016-09-15 15:24:50 -07001178 aliases = zalloc(sizeof(struct sevent) * len);
Andi Kleendc098b32013-04-20 11:02:29 -07001179 if (!aliases)
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001180 goto out_enomem;
Andi Kleendc098b32013-04-20 11:02:29 -07001181 pmu = NULL;
1182 j = 0;
Adrian Hunter42634bc2014-10-23 13:45:10 +03001183 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
Andi Kleendc098b32013-04-20 11:02:29 -07001184 list_for_each_entry(alias, &pmu->aliases, list) {
Andi Kleen08e60ed2016-09-15 15:24:43 -07001185 char *name = alias->desc ? alias->name :
1186 format_alias(buf, sizeof(buf), pmu, alias);
Andi Kleendc098b32013-04-20 11:02:29 -07001187 bool is_cpu = !strcmp(pmu->name, "cpu");
1188
1189 if (event_glob != NULL &&
Andi Kleen38d14f02016-10-19 10:50:01 -07001190 !(strglobmatch_nocase(name, event_glob) ||
1191 (!is_cpu && strglobmatch_nocase(alias->name,
Andi Kleen67bdc352016-10-19 11:45:23 -07001192 event_glob)) ||
1193 (alias->topic &&
1194 strglobmatch_nocase(alias->topic, event_glob))))
Andi Kleendc098b32013-04-20 11:02:29 -07001195 continue;
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001196
Andi Kleen08e60ed2016-09-15 15:24:43 -07001197 if (is_cpu && !name_only && !alias->desc)
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001198 name = format_alias_or(buf, sizeof(buf), pmu, alias);
1199
Andi Kleen08e60ed2016-09-15 15:24:43 -07001200 aliases[j].name = name;
1201 if (is_cpu && !name_only && !alias->desc)
1202 aliases[j].name = format_alias_or(buf,
1203 sizeof(buf),
1204 pmu, alias);
1205 aliases[j].name = strdup(aliases[j].name);
1206 if (!aliases[j].name)
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001207 goto out_enomem;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001208
Sukadev Bhattiproluc8d68282016-09-15 15:24:48 -07001209 aliases[j].desc = long_desc ? alias->long_desc :
1210 alias->desc;
Andi Kleendd5f1032016-09-15 15:24:50 -07001211 aliases[j].topic = alias->topic;
Andi Kleenf2361022017-01-27 18:03:40 -08001212 aliases[j].str = alias->str;
1213 aliases[j].pmu = pmu->name;
Andi Kleen7f372a62017-03-20 13:17:09 -07001214 aliases[j].metric_expr = alias->metric_expr;
Andi Kleen96284812017-03-20 13:17:10 -07001215 aliases[j].metric_name = alias->metric_name;
Andi Kleendc098b32013-04-20 11:02:29 -07001216 j++;
1217 }
Arnaldo Carvalho de Melofa52cea2015-10-02 15:28:16 -03001218 if (pmu->selectable &&
1219 (event_glob == NULL || strglobmatch(pmu->name, event_glob))) {
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001220 char *s;
1221 if (asprintf(&s, "%s//", pmu->name) < 0)
1222 goto out_enomem;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001223 aliases[j].name = s;
Adrian Hunter42634bc2014-10-23 13:45:10 +03001224 j++;
1225 }
1226 }
Andi Kleendc098b32013-04-20 11:02:29 -07001227 len = j;
Andi Kleendd5f1032016-09-15 15:24:50 -07001228 qsort(aliases, len, sizeof(struct sevent), cmp_sevent);
Andi Kleendc098b32013-04-20 11:02:29 -07001229 for (j = 0; j < len; j++) {
Andi Kleen15b22ed2017-01-27 18:03:38 -08001230 /* Skip duplicates */
1231 if (j > 0 && !strcmp(aliases[j].name, aliases[j - 1].name))
1232 continue;
Andi Kleendc098b32013-04-20 11:02:29 -07001233 if (name_only) {
Andi Kleen08e60ed2016-09-15 15:24:43 -07001234 printf("%s ", aliases[j].name);
Andi Kleendc098b32013-04-20 11:02:29 -07001235 continue;
1236 }
Andi Kleen1c5f01f2016-09-15 15:24:45 -07001237 if (aliases[j].desc && !quiet_flag) {
Andi Kleen08e60ed2016-09-15 15:24:43 -07001238 if (numdesc++ == 0)
1239 printf("\n");
Andi Kleendd5f1032016-09-15 15:24:50 -07001240 if (aliases[j].topic && (!topic ||
1241 strcmp(topic, aliases[j].topic))) {
1242 printf("%s%s:\n", topic ? "\n" : "",
1243 aliases[j].topic);
1244 topic = aliases[j].topic;
1245 }
Andi Kleen08e60ed2016-09-15 15:24:43 -07001246 printf(" %-50s\n", aliases[j].name);
1247 printf("%*s", 8, "[");
1248 wordwrap(aliases[j].desc, 8, columns, 0);
1249 printf("]\n");
Andi Kleenbf874fc2017-03-20 13:17:11 -07001250 if (details_flag) {
Andi Kleen7f372a62017-03-20 13:17:09 -07001251 printf("%*s%s/%s/ ", 8, "", aliases[j].pmu, aliases[j].str);
Andi Kleen96284812017-03-20 13:17:10 -07001252 if (aliases[j].metric_name)
1253 printf(" MetricName: %s", aliases[j].metric_name);
Andi Kleen7f372a62017-03-20 13:17:09 -07001254 if (aliases[j].metric_expr)
1255 printf(" MetricExpr: %s", aliases[j].metric_expr);
1256 putchar('\n');
1257 }
Andi Kleen08e60ed2016-09-15 15:24:43 -07001258 } else
1259 printf(" %-50s [Kernel PMU event]\n", aliases[j].name);
Andi Kleendc098b32013-04-20 11:02:29 -07001260 printed++;
1261 }
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03001262 if (printed && pager_in_use())
Andi Kleendc098b32013-04-20 11:02:29 -07001263 printf("\n");
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001264out_free:
1265 for (j = 0; j < len; j++)
Andi Kleen08e60ed2016-09-15 15:24:43 -07001266 zfree(&aliases[j].name);
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001267 zfree(&aliases);
1268 return;
1269
1270out_enomem:
1271 printf("FATAL: not enough memory to print PMU events\n");
1272 if (aliases)
1273 goto out_free;
Andi Kleendc098b32013-04-20 11:02:29 -07001274}
Andi Kleen4cabc3d2013-08-21 16:47:26 -07001275
1276bool pmu_have_event(const char *pname, const char *name)
1277{
1278 struct perf_pmu *pmu;
1279 struct perf_pmu_alias *alias;
1280
1281 pmu = NULL;
1282 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1283 if (strcmp(pname, pmu->name))
1284 continue;
1285 list_for_each_entry(alias, &pmu->aliases, list)
1286 if (!strcmp(alias->name, name))
1287 return true;
1288 }
1289 return false;
1290}
Adrian Hunter7d4bdab2014-07-31 09:00:50 +03001291
1292static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
1293{
1294 struct stat st;
1295 char path[PATH_MAX];
1296 const char *sysfs;
1297
1298 sysfs = sysfs__mountpoint();
1299 if (!sysfs)
1300 return NULL;
1301
1302 snprintf(path, PATH_MAX,
1303 "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name);
1304
1305 if (stat(path, &st) < 0)
1306 return NULL;
1307
1308 return fopen(path, "r");
1309}
1310
1311int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
1312 ...)
1313{
1314 va_list args;
1315 FILE *file;
1316 int ret = EOF;
1317
1318 va_start(args, fmt);
1319 file = perf_pmu__open_file(pmu, name);
1320 if (file) {
1321 ret = vfscanf(file, fmt, args);
1322 fclose(file);
1323 }
1324 va_end(args);
1325 return ret;
1326}