blob: 11c752561c558c0fccf41f0e403d48eb6b38ba2c [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>
Jiri Olsacd82a322012-03-15 20:09:17 +01004#include <unistd.h>
5#include <stdio.h>
Adrian Hunterdc0a6202014-07-31 09:00:49 +03006#include <stdbool.h>
Adrian Hunter7d4bdab2014-07-31 09:00:50 +03007#include <stdarg.h>
Jiri Olsacd82a322012-03-15 20:09:17 +01008#include <dirent.h>
Borislav Petkovcd0cfad2013-12-09 17:14:24 +01009#include <api/fs/fs.h>
Stephane Eranian410136f2013-11-12 17:58:49 +010010#include <locale.h>
Jiri Olsacd82a322012-03-15 20:09:17 +010011#include "util.h"
12#include "pmu.h"
13#include "parse-events.h"
Yan, Zheng7ae92e72012-09-10 15:53:50 +080014#include "cpumap.h"
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -070015#include "header.h"
16#include "pmu-events/pmu-events.h"
Andi Kleen61eb2eb2016-09-15 15:24:44 -070017#include "cache.h"
Jiri Olsacd82a322012-03-15 20:09:17 +010018
Arnaldo Carvalho de Meloab1bf6532013-01-18 17:05:09 -030019struct perf_pmu_format {
20 char *name;
21 int value;
22 DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
23 struct list_head list;
24};
25
Robert Richter50a96672012-08-16 21:10:24 +020026#define EVENT_SOURCE_DEVICE_PATH "/bus/event_source/devices/"
27
Jiri Olsacd82a322012-03-15 20:09:17 +010028int perf_pmu_parse(struct list_head *list, char *name);
29extern FILE *perf_pmu_in;
30
31static LIST_HEAD(pmus);
32
33/*
34 * Parse & process all the sysfs attributes located under
35 * the directory specified in 'dir' parameter.
36 */
Jiri Olsacff7f952012-11-10 01:46:50 +010037int perf_pmu__format_parse(char *dir, struct list_head *head)
Jiri Olsacd82a322012-03-15 20:09:17 +010038{
39 struct dirent *evt_ent;
40 DIR *format_dir;
41 int ret = 0;
42
43 format_dir = opendir(dir);
44 if (!format_dir)
45 return -EINVAL;
46
47 while (!ret && (evt_ent = readdir(format_dir))) {
48 char path[PATH_MAX];
49 char *name = evt_ent->d_name;
50 FILE *file;
51
52 if (!strcmp(name, ".") || !strcmp(name, ".."))
53 continue;
54
55 snprintf(path, PATH_MAX, "%s/%s", dir, name);
56
57 ret = -EINVAL;
58 file = fopen(path, "r");
59 if (!file)
60 break;
61
62 perf_pmu_in = file;
63 ret = perf_pmu_parse(head, name);
64 fclose(file);
65 }
66
67 closedir(format_dir);
68 return ret;
69}
70
71/*
72 * Reading/parsing the default pmu format definition, which should be
73 * located at:
74 * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
75 */
Adrian Hunterb6b96fb2013-07-04 16:20:25 +030076static int pmu_format(const char *name, struct list_head *format)
Jiri Olsacd82a322012-03-15 20:09:17 +010077{
78 struct stat st;
79 char path[PATH_MAX];
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -030080 const char *sysfs = sysfs__mountpoint();
Jiri Olsacd82a322012-03-15 20:09:17 +010081
Jiri Olsacd82a322012-03-15 20:09:17 +010082 if (!sysfs)
83 return -1;
84
85 snprintf(path, PATH_MAX,
Robert Richter50a96672012-08-16 21:10:24 +020086 "%s" EVENT_SOURCE_DEVICE_PATH "%s/format", sysfs, name);
Jiri Olsacd82a322012-03-15 20:09:17 +010087
88 if (stat(path, &st) < 0)
Robert Richter9bc8f9f2012-06-14 22:38:37 +020089 return 0; /* no error if format does not exist */
Jiri Olsacd82a322012-03-15 20:09:17 +010090
Jiri Olsacff7f952012-11-10 01:46:50 +010091 if (perf_pmu__format_parse(path, format))
Jiri Olsacd82a322012-03-15 20:09:17 +010092 return -1;
93
94 return 0;
95}
96
Andi Kleend02fc6b2017-01-03 07:08:23 -080097static int convert_scale(const char *scale, char **end, double *sval)
98{
99 char *lc;
100 int ret = 0;
101
102 /*
103 * save current locale
104 */
105 lc = setlocale(LC_NUMERIC, NULL);
106
107 /*
108 * The lc string may be allocated in static storage,
109 * so get a dynamic copy to make it survive setlocale
110 * call below.
111 */
112 lc = strdup(lc);
113 if (!lc) {
114 ret = -ENOMEM;
115 goto out;
116 }
117
118 /*
119 * force to C locale to ensure kernel
120 * scale string is converted correctly.
121 * kernel uses default C locale.
122 */
123 setlocale(LC_NUMERIC, "C");
124
125 *sval = strtod(scale, end);
126
127out:
128 /* restore locale */
129 setlocale(LC_NUMERIC, lc);
130 free(lc);
131 return ret;
132}
133
Stephane Eranian410136f2013-11-12 17:58:49 +0100134static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *name)
135{
136 struct stat st;
137 ssize_t sret;
138 char scale[128];
139 int fd, ret = -1;
140 char path[PATH_MAX];
Stephane Eranian410136f2013-11-12 17:58:49 +0100141
142 snprintf(path, PATH_MAX, "%s/%s.scale", dir, name);
143
144 fd = open(path, O_RDONLY);
145 if (fd == -1)
146 return -1;
147
148 if (fstat(fd, &st) < 0)
149 goto error;
150
151 sret = read(fd, scale, sizeof(scale)-1);
152 if (sret < 0)
153 goto error;
154
Madhavan Srinivasan9ecae062015-05-31 11:36:23 +0530155 if (scale[sret - 1] == '\n')
156 scale[sret - 1] = '\0';
157 else
158 scale[sret] = '\0';
159
Andi Kleend02fc6b2017-01-03 07:08:23 -0800160 ret = convert_scale(scale, NULL, &alias->scale);
Stephane Eranian410136f2013-11-12 17:58:49 +0100161error:
162 close(fd);
163 return ret;
164}
165
166static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *name)
167{
168 char path[PATH_MAX];
169 ssize_t sret;
170 int fd;
171
172 snprintf(path, PATH_MAX, "%s/%s.unit", dir, name);
173
174 fd = open(path, O_RDONLY);
175 if (fd == -1)
176 return -1;
177
Markus Trippelsdorfd85ce832015-12-14 16:44:40 +0100178 sret = read(fd, alias->unit, UNIT_MAX_LEN);
Stephane Eranian410136f2013-11-12 17:58:49 +0100179 if (sret < 0)
180 goto error;
181
182 close(fd);
183
Madhavan Srinivasan9ecae062015-05-31 11:36:23 +0530184 if (alias->unit[sret - 1] == '\n')
185 alias->unit[sret - 1] = '\0';
186 else
187 alias->unit[sret] = '\0';
Stephane Eranian410136f2013-11-12 17:58:49 +0100188
189 return 0;
190error:
191 close(fd);
192 alias->unit[0] = '\0';
193 return -1;
194}
195
Matt Fleming044330c2014-11-21 10:31:12 +0100196static int
197perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, char *dir, char *name)
198{
199 char path[PATH_MAX];
200 int fd;
201
202 snprintf(path, PATH_MAX, "%s/%s.per-pkg", dir, name);
203
204 fd = open(path, O_RDONLY);
205 if (fd == -1)
206 return -1;
207
208 close(fd);
209
210 alias->per_pkg = true;
211 return 0;
212}
213
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100214static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias,
215 char *dir, char *name)
216{
217 char path[PATH_MAX];
218 int fd;
219
220 snprintf(path, PATH_MAX, "%s/%s.snapshot", dir, name);
221
222 fd = open(path, O_RDONLY);
223 if (fd == -1)
224 return -1;
225
226 alias->snapshot = true;
227 close(fd);
228 return 0;
229}
230
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700231static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name,
Andi Kleenfedb2b52017-01-27 18:03:37 -0800232 char *desc, char *val,
233 char *long_desc, char *topic,
Andi Kleen00636c32017-03-20 13:17:07 -0700234 char *unit, char *perpkg,
Andi Kleen96284812017-03-20 13:17:10 -0700235 char *metric_expr,
236 char *metric_name)
Zheng Yana6146d52012-06-15 14:31:41 +0800237{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300238 struct perf_pmu_alias *alias;
Zheng Yana6146d52012-06-15 14:31:41 +0800239 int ret;
Andi Kleenfedb2b52017-01-27 18:03:37 -0800240 int num;
Zheng Yana6146d52012-06-15 14:31:41 +0800241
Zheng Yana6146d52012-06-15 14:31:41 +0800242 alias = malloc(sizeof(*alias));
243 if (!alias)
244 return -ENOMEM;
245
246 INIT_LIST_HEAD(&alias->terms);
Stephane Eranian410136f2013-11-12 17:58:49 +0100247 alias->scale = 1.0;
248 alias->unit[0] = '\0';
Matt Fleming044330c2014-11-21 10:31:12 +0100249 alias->per_pkg = false;
Stephane Eranian84530922016-01-06 19:50:01 +0100250 alias->snapshot = false;
Stephane Eranian410136f2013-11-12 17:58:49 +0100251
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700252 ret = parse_events_terms(&alias->terms, val);
Zheng Yana6146d52012-06-15 14:31:41 +0800253 if (ret) {
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700254 pr_err("Cannot parse alias %s: %d\n", val, ret);
Zheng Yana6146d52012-06-15 14:31:41 +0800255 free(alias);
256 return ret;
257 }
258
259 alias->name = strdup(name);
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700260 if (dir) {
261 /*
262 * load unit name and scale if available
263 */
264 perf_pmu__parse_unit(alias, dir, name);
265 perf_pmu__parse_scale(alias, dir, name);
266 perf_pmu__parse_per_pkg(alias, dir, name);
267 perf_pmu__parse_snapshot(alias, dir, name);
268 }
Stephane Eranian410136f2013-11-12 17:58:49 +0100269
Andi Kleen00636c32017-03-20 13:17:07 -0700270 alias->metric_expr = metric_expr ? strdup(metric_expr) : NULL;
Andi Kleen96284812017-03-20 13:17:10 -0700271 alias->metric_name = metric_name ? strdup(metric_name): NULL;
Andi Kleen08e60ed2016-09-15 15:24:43 -0700272 alias->desc = desc ? strdup(desc) : NULL;
Sukadev Bhattiproluc8d68282016-09-15 15:24:48 -0700273 alias->long_desc = long_desc ? strdup(long_desc) :
274 desc ? strdup(desc) : NULL;
Andi Kleendd5f1032016-09-15 15:24:50 -0700275 alias->topic = topic ? strdup(topic) : NULL;
Andi Kleenfedb2b52017-01-27 18:03:37 -0800276 if (unit) {
277 if (convert_scale(unit, &unit, &alias->scale) < 0)
278 return -1;
279 snprintf(alias->unit, sizeof(alias->unit), "%s", unit);
280 }
281 alias->per_pkg = perpkg && sscanf(perpkg, "%d", &num) == 1 && num == 1;
Andi Kleenf2361022017-01-27 18:03:40 -0800282 alias->str = strdup(val);
283
Zheng Yana6146d52012-06-15 14:31:41 +0800284 list_add_tail(&alias->list, list);
Stephane Eranian410136f2013-11-12 17:58:49 +0100285
Zheng Yana6146d52012-06-15 14:31:41 +0800286 return 0;
287}
288
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700289static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file)
290{
291 char buf[256];
292 int ret;
293
294 ret = fread(buf, 1, sizeof(buf), file);
295 if (ret == 0)
296 return -EINVAL;
297
298 buf[ret] = 0;
299
Andi Kleenfedb2b52017-01-27 18:03:37 -0800300 return __perf_pmu__new_alias(list, dir, name, NULL, buf, NULL, NULL, NULL,
Andi Kleen96284812017-03-20 13:17:10 -0700301 NULL, NULL, NULL);
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700302}
303
Matt Fleming46441bd2014-09-24 15:04:06 +0100304static inline bool pmu_alias_info_file(char *name)
305{
306 size_t len;
307
308 len = strlen(name);
309 if (len > 5 && !strcmp(name + len - 5, ".unit"))
310 return true;
311 if (len > 6 && !strcmp(name + len - 6, ".scale"))
312 return true;
Matt Fleming044330c2014-11-21 10:31:12 +0100313 if (len > 8 && !strcmp(name + len - 8, ".per-pkg"))
314 return true;
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100315 if (len > 9 && !strcmp(name + len - 9, ".snapshot"))
316 return true;
Matt Fleming46441bd2014-09-24 15:04:06 +0100317
318 return false;
319}
320
Zheng Yana6146d52012-06-15 14:31:41 +0800321/*
322 * Process all the sysfs attributes located under the directory
323 * specified in 'dir' parameter.
324 */
325static int pmu_aliases_parse(char *dir, struct list_head *head)
326{
327 struct dirent *evt_ent;
328 DIR *event_dir;
Zheng Yana6146d52012-06-15 14:31:41 +0800329
330 event_dir = opendir(dir);
331 if (!event_dir)
332 return -EINVAL;
333
Andi Kleen940db6d2016-02-17 14:44:55 -0800334 while ((evt_ent = readdir(event_dir))) {
Zheng Yana6146d52012-06-15 14:31:41 +0800335 char path[PATH_MAX];
336 char *name = evt_ent->d_name;
337 FILE *file;
338
339 if (!strcmp(name, ".") || !strcmp(name, ".."))
340 continue;
341
Stephane Eranian410136f2013-11-12 17:58:49 +0100342 /*
Matt Fleming46441bd2014-09-24 15:04:06 +0100343 * skip info files parsed in perf_pmu__new_alias()
Stephane Eranian410136f2013-11-12 17:58:49 +0100344 */
Matt Fleming46441bd2014-09-24 15:04:06 +0100345 if (pmu_alias_info_file(name))
Stephane Eranian410136f2013-11-12 17:58:49 +0100346 continue;
347
Zheng Yana6146d52012-06-15 14:31:41 +0800348 snprintf(path, PATH_MAX, "%s/%s", dir, name);
349
Zheng Yana6146d52012-06-15 14:31:41 +0800350 file = fopen(path, "r");
Andi Kleen940db6d2016-02-17 14:44:55 -0800351 if (!file) {
352 pr_debug("Cannot open %s\n", path);
353 continue;
354 }
Stephane Eranian410136f2013-11-12 17:58:49 +0100355
Andi Kleen940db6d2016-02-17 14:44:55 -0800356 if (perf_pmu__new_alias(head, dir, name, file) < 0)
357 pr_debug("Cannot set up %s\n", name);
Zheng Yana6146d52012-06-15 14:31:41 +0800358 fclose(file);
359 }
360
361 closedir(event_dir);
Andi Kleen940db6d2016-02-17 14:44:55 -0800362 return 0;
Zheng Yana6146d52012-06-15 14:31:41 +0800363}
364
365/*
366 * Reading the pmu event aliases definition, which should be located at:
367 * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
368 */
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300369static int pmu_aliases(const char *name, struct list_head *head)
Zheng Yana6146d52012-06-15 14:31:41 +0800370{
371 struct stat st;
372 char path[PATH_MAX];
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -0300373 const char *sysfs = sysfs__mountpoint();
Zheng Yana6146d52012-06-15 14:31:41 +0800374
Zheng Yana6146d52012-06-15 14:31:41 +0800375 if (!sysfs)
376 return -1;
377
378 snprintf(path, PATH_MAX,
379 "%s/bus/event_source/devices/%s/events", sysfs, name);
380
381 if (stat(path, &st) < 0)
Jiri Olsa3fded962012-10-10 14:53:16 +0200382 return 0; /* no error if 'events' does not exist */
Zheng Yana6146d52012-06-15 14:31:41 +0800383
384 if (pmu_aliases_parse(path, head))
385 return -1;
386
387 return 0;
388}
389
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300390static int pmu_alias_terms(struct perf_pmu_alias *alias,
Zheng Yana6146d52012-06-15 14:31:41 +0800391 struct list_head *terms)
392{
Jiri Olsa7c2f8162014-04-16 20:49:02 +0200393 struct parse_events_term *term, *cloned;
Zheng Yana6146d52012-06-15 14:31:41 +0800394 LIST_HEAD(list);
395 int ret;
396
397 list_for_each_entry(term, &alias->terms, list) {
Jiri Olsa7c2f8162014-04-16 20:49:02 +0200398 ret = parse_events_term__clone(&cloned, term);
Zheng Yana6146d52012-06-15 14:31:41 +0800399 if (ret) {
Arnaldo Carvalho de Melo682dc242016-02-12 16:48:00 -0300400 parse_events_terms__purge(&list);
Zheng Yana6146d52012-06-15 14:31:41 +0800401 return ret;
402 }
Jiri Olsa7c2f8162014-04-16 20:49:02 +0200403 list_add_tail(&cloned->list, &list);
Zheng Yana6146d52012-06-15 14:31:41 +0800404 }
405 list_splice(&list, terms);
406 return 0;
407}
408
Jiri Olsacd82a322012-03-15 20:09:17 +0100409/*
410 * Reading/parsing the default pmu type value, which should be
411 * located at:
412 * /sys/bus/event_source/devices/<dev>/type as sysfs attribute.
413 */
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300414static int pmu_type(const char *name, __u32 *type)
Jiri Olsacd82a322012-03-15 20:09:17 +0100415{
416 struct stat st;
417 char path[PATH_MAX];
Jiri Olsacd82a322012-03-15 20:09:17 +0100418 FILE *file;
419 int ret = 0;
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -0300420 const char *sysfs = sysfs__mountpoint();
Jiri Olsacd82a322012-03-15 20:09:17 +0100421
Jiri Olsacd82a322012-03-15 20:09:17 +0100422 if (!sysfs)
423 return -1;
424
425 snprintf(path, PATH_MAX,
Robert Richter50a96672012-08-16 21:10:24 +0200426 "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name);
Jiri Olsacd82a322012-03-15 20:09:17 +0100427
428 if (stat(path, &st) < 0)
429 return -1;
430
431 file = fopen(path, "r");
432 if (!file)
433 return -EINVAL;
434
435 if (1 != fscanf(file, "%u", type))
436 ret = -1;
437
438 fclose(file);
439 return ret;
440}
441
Robert Richter50a96672012-08-16 21:10:24 +0200442/* Add all pmus in sysfs to pmu list: */
443static void pmu_read_sysfs(void)
444{
445 char path[PATH_MAX];
Robert Richter50a96672012-08-16 21:10:24 +0200446 DIR *dir;
447 struct dirent *dent;
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -0300448 const char *sysfs = sysfs__mountpoint();
Robert Richter50a96672012-08-16 21:10:24 +0200449
Robert Richter50a96672012-08-16 21:10:24 +0200450 if (!sysfs)
451 return;
452
453 snprintf(path, PATH_MAX,
454 "%s" EVENT_SOURCE_DEVICE_PATH, sysfs);
455
456 dir = opendir(path);
457 if (!dir)
458 return;
459
460 while ((dent = readdir(dir))) {
461 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
462 continue;
463 /* add to static LIST_HEAD(pmus): */
464 perf_pmu__find(dent->d_name);
465 }
466
467 closedir(dir);
468}
469
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300470static struct cpu_map *pmu_cpumask(const char *name)
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800471{
472 struct stat st;
473 char path[PATH_MAX];
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800474 FILE *file;
475 struct cpu_map *cpus;
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -0300476 const char *sysfs = sysfs__mountpoint();
Mark Rutland7e3fcffe2016-09-08 11:21:52 +0100477 const char *templates[] = {
478 "%s/bus/event_source/devices/%s/cpumask",
479 "%s/bus/event_source/devices/%s/cpus",
480 NULL
481 };
482 const char **template;
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800483
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800484 if (!sysfs)
485 return NULL;
486
Mark Rutland7e3fcffe2016-09-08 11:21:52 +0100487 for (template = templates; *template; template++) {
488 snprintf(path, PATH_MAX, *template, sysfs, name);
489 if (stat(path, &st) == 0)
490 break;
491 }
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800492
Mark Rutland7e3fcffe2016-09-08 11:21:52 +0100493 if (!*template)
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800494 return NULL;
495
496 file = fopen(path, "r");
497 if (!file)
498 return NULL;
499
500 cpus = cpu_map__read(file);
501 fclose(file);
502 return cpus;
503}
504
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700505/*
506 * Return the CPU id as a raw string.
507 *
508 * Each architecture should provide a more precise id string that
509 * can be use to match the architecture's "mapfile".
510 */
511char * __weak get_cpuid_str(void)
512{
513 return NULL;
514}
515
516/*
517 * From the pmu_events_map, find the table of PMU events that corresponds
518 * to the current running CPU. Then, add all PMU events from that table
519 * as aliases.
520 */
Andi Kleenfedb2b52017-01-27 18:03:37 -0800521static void pmu_add_cpu_aliases(struct list_head *head, const char *name)
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700522{
523 int i;
524 struct pmu_events_map *map;
525 struct pmu_event *pe;
526 char *cpuid;
Andi Kleenfb967062016-10-13 14:15:24 -0700527 static bool printed;
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700528
Andi Kleenfc06e2a2016-09-15 15:24:46 -0700529 cpuid = getenv("PERF_CPUID");
530 if (cpuid)
531 cpuid = strdup(cpuid);
532 if (!cpuid)
533 cpuid = get_cpuid_str();
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700534 if (!cpuid)
535 return;
536
Andi Kleenfb967062016-10-13 14:15:24 -0700537 if (!printed) {
538 pr_debug("Using CPUID %s\n", cpuid);
539 printed = true;
540 }
Andi Kleenfc06e2a2016-09-15 15:24:46 -0700541
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700542 i = 0;
543 while (1) {
544 map = &pmu_events_map[i++];
545 if (!map->table)
546 goto out;
547
548 if (!strcmp(map->cpuid, cpuid))
549 break;
550 }
551
552 /*
553 * Found a matching PMU events table. Create aliases
554 */
555 i = 0;
556 while (1) {
Andi Kleenfedb2b52017-01-27 18:03:37 -0800557 const char *pname;
558
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700559 pe = &map->table[i++];
560 if (!pe->name)
561 break;
562
Andi Kleenfedb2b52017-01-27 18:03:37 -0800563 pname = pe->pmu ? pe->pmu : "cpu";
564 if (strncmp(pname, name, strlen(pname)))
565 continue;
566
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700567 /* need type casts to override 'const' */
568 __perf_pmu__new_alias(head, NULL, (char *)pe->name,
Sukadev Bhattiproluc8d68282016-09-15 15:24:48 -0700569 (char *)pe->desc, (char *)pe->event,
Andi Kleenfedb2b52017-01-27 18:03:37 -0800570 (char *)pe->long_desc, (char *)pe->topic,
Andi Kleen00636c32017-03-20 13:17:07 -0700571 (char *)pe->unit, (char *)pe->perpkg,
Andi Kleen96284812017-03-20 13:17:10 -0700572 (char *)pe->metric_expr,
573 (char *)pe->metric_name);
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700574 }
575
576out:
577 free(cpuid);
578}
579
Sukadev Bhattiproluc5de47f2015-06-10 00:25:07 -0700580struct perf_event_attr * __weak
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300581perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
582{
583 return NULL;
584}
585
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300586static struct perf_pmu *pmu_lookup(const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +0100587{
588 struct perf_pmu *pmu;
589 LIST_HEAD(format);
Zheng Yana6146d52012-06-15 14:31:41 +0800590 LIST_HEAD(aliases);
Jiri Olsacd82a322012-03-15 20:09:17 +0100591 __u32 type;
592
593 /*
594 * The pmu data we store & need consists of the pmu
595 * type value and format definitions. Load both right
596 * now.
597 */
598 if (pmu_format(name, &format))
599 return NULL;
600
Andi Kleen15b22ed2017-01-27 18:03:38 -0800601 /*
602 * Check the type first to avoid unnecessary work.
603 */
604 if (pmu_type(name, &type))
605 return NULL;
606
Jiri Olsa3fded962012-10-10 14:53:16 +0200607 if (pmu_aliases(name, &aliases))
608 return NULL;
609
Andi Kleenfedb2b52017-01-27 18:03:37 -0800610 pmu_add_cpu_aliases(&aliases, name);
Jiri Olsacd82a322012-03-15 20:09:17 +0100611 pmu = zalloc(sizeof(*pmu));
612 if (!pmu)
613 return NULL;
614
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800615 pmu->cpus = pmu_cpumask(name);
616
Jiri Olsacd82a322012-03-15 20:09:17 +0100617 INIT_LIST_HEAD(&pmu->format);
Zheng Yana6146d52012-06-15 14:31:41 +0800618 INIT_LIST_HEAD(&pmu->aliases);
Jiri Olsacd82a322012-03-15 20:09:17 +0100619 list_splice(&format, &pmu->format);
Zheng Yana6146d52012-06-15 14:31:41 +0800620 list_splice(&aliases, &pmu->aliases);
Jiri Olsacd82a322012-03-15 20:09:17 +0100621 pmu->name = strdup(name);
622 pmu->type = type;
Robert Richter9bc8f9f2012-06-14 22:38:37 +0200623 list_add_tail(&pmu->list, &pmus);
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300624
625 pmu->default_config = perf_pmu__get_default_config(pmu);
626
Jiri Olsacd82a322012-03-15 20:09:17 +0100627 return pmu;
628}
629
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300630static struct perf_pmu *pmu_find(const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +0100631{
632 struct perf_pmu *pmu;
633
634 list_for_each_entry(pmu, &pmus, list)
635 if (!strcmp(pmu->name, name))
636 return pmu;
637
638 return NULL;
639}
640
Robert Richter50a96672012-08-16 21:10:24 +0200641struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
642{
643 /*
644 * pmu iterator: If pmu is NULL, we start at the begin,
645 * otherwise return the next pmu. Returns NULL on end.
646 */
647 if (!pmu) {
648 pmu_read_sysfs();
649 pmu = list_prepare_entry(pmu, &pmus, list);
650 }
651 list_for_each_entry_continue(pmu, &pmus, list)
652 return pmu;
653 return NULL;
654}
655
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300656struct perf_pmu *perf_pmu__find(const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +0100657{
658 struct perf_pmu *pmu;
659
660 /*
661 * Once PMU is loaded it stays in the list,
662 * so we keep us from multiple reading/parsing
663 * the pmu format definitions.
664 */
665 pmu = pmu_find(name);
666 if (pmu)
667 return pmu;
668
669 return pmu_lookup(name);
670}
671
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300672static struct perf_pmu_format *
Adrian Hunter09ff6072015-07-17 19:33:49 +0300673pmu_find_format(struct list_head *formats, const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +0100674{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300675 struct perf_pmu_format *format;
Jiri Olsacd82a322012-03-15 20:09:17 +0100676
677 list_for_each_entry(format, formats, list)
678 if (!strcmp(format->name, name))
679 return format;
680
681 return NULL;
682}
683
Adrian Hunter09ff6072015-07-17 19:33:49 +0300684__u64 perf_pmu__format_bits(struct list_head *formats, const char *name)
685{
686 struct perf_pmu_format *format = pmu_find_format(formats, name);
687 __u64 bits = 0;
688 int fbit;
689
690 if (!format)
691 return 0;
692
693 for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
694 bits |= 1ULL << fbit;
695
696 return bits;
697}
698
Jiri Olsacd82a322012-03-15 20:09:17 +0100699/*
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300700 * Sets value based on the format definition (format parameter)
Jiri Olsacd82a322012-03-15 20:09:17 +0100701 * and unformated value (value parameter).
Jiri Olsacd82a322012-03-15 20:09:17 +0100702 */
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300703static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
704 bool zero)
Jiri Olsacd82a322012-03-15 20:09:17 +0100705{
706 unsigned long fbit, vbit;
Jiri Olsacd82a322012-03-15 20:09:17 +0100707
708 for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
709
710 if (!test_bit(fbit, format))
711 continue;
712
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300713 if (value & (1llu << vbit++))
714 *v |= (1llu << fbit);
715 else if (zero)
716 *v &= ~(1llu << fbit);
Jiri Olsacd82a322012-03-15 20:09:17 +0100717 }
Jiri Olsacd82a322012-03-15 20:09:17 +0100718}
719
Adrian Hunter0efe6b62015-07-17 19:33:50 +0300720static __u64 pmu_format_max_value(const unsigned long *format)
721{
Kan Liangac0e2cd2016-03-30 12:16:15 -0700722 __u64 w = 0;
723 int fbit;
Adrian Hunter0efe6b62015-07-17 19:33:50 +0300724
Kan Liangac0e2cd2016-03-30 12:16:15 -0700725 for_each_set_bit(fbit, format, PERF_PMU_FORMAT_BITS)
726 w |= (1ULL << fbit);
727
728 return w;
Adrian Hunter0efe6b62015-07-17 19:33:50 +0300729}
730
Jiri Olsacd82a322012-03-15 20:09:17 +0100731/*
Cody P Schafer688d4df2015-01-07 17:13:50 -0800732 * Term is a string term, and might be a param-term. Try to look up it's value
733 * in the remaining terms.
734 * - We have a term like "base-or-format-term=param-term",
735 * - We need to find the value supplied for "param-term" (with param-term named
736 * in a config string) later on in the term list.
737 */
738static int pmu_resolve_param_term(struct parse_events_term *term,
739 struct list_head *head_terms,
740 __u64 *value)
741{
742 struct parse_events_term *t;
743
744 list_for_each_entry(t, head_terms, list) {
745 if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
746 if (!strcmp(t->config, term->config)) {
747 t->used = true;
748 *value = t->val.num;
749 return 0;
750 }
751 }
752 }
753
Namhyung Kimbb963e12017-02-17 17:17:38 +0900754 if (verbose > 0)
Cody P Schafer688d4df2015-01-07 17:13:50 -0800755 printf("Required parameter '%s' not specified\n", term->config);
756
757 return -1;
758}
759
He Kuangffeb8832015-09-28 03:52:14 +0000760static char *pmu_formats_string(struct list_head *formats)
Jiri Olsae64b0202015-04-22 21:10:21 +0200761{
762 struct perf_pmu_format *format;
Masami Hiramatsu11db4e22016-05-10 14:47:44 +0900763 char *str = NULL;
764 struct strbuf buf = STRBUF_INIT;
Jiri Olsae64b0202015-04-22 21:10:21 +0200765 unsigned i = 0;
766
He Kuangffeb8832015-09-28 03:52:14 +0000767 if (!formats)
Jiri Olsae64b0202015-04-22 21:10:21 +0200768 return NULL;
769
770 /* sysfs exported terms */
He Kuangffeb8832015-09-28 03:52:14 +0000771 list_for_each_entry(format, formats, list)
Masami Hiramatsu11db4e22016-05-10 14:47:44 +0900772 if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
773 goto error;
Jiri Olsae64b0202015-04-22 21:10:21 +0200774
He Kuangffeb8832015-09-28 03:52:14 +0000775 str = strbuf_detach(&buf, NULL);
Masami Hiramatsu11db4e22016-05-10 14:47:44 +0900776error:
He Kuangffeb8832015-09-28 03:52:14 +0000777 strbuf_release(&buf);
Jiri Olsae64b0202015-04-22 21:10:21 +0200778
Jiri Olsae64b0202015-04-22 21:10:21 +0200779 return str;
Jiri Olsae64b0202015-04-22 21:10:21 +0200780}
781
Cody P Schafer688d4df2015-01-07 17:13:50 -0800782/*
Jiri Olsacd82a322012-03-15 20:09:17 +0100783 * Setup one of config[12] attr members based on the
Cody P Schafer88aca8d2014-01-08 08:43:51 -0800784 * user input data - term parameter.
Jiri Olsacd82a322012-03-15 20:09:17 +0100785 */
786static int pmu_config_term(struct list_head *formats,
787 struct perf_event_attr *attr,
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300788 struct parse_events_term *term,
Cody P Schafer688d4df2015-01-07 17:13:50 -0800789 struct list_head *head_terms,
Jiri Olsae64b0202015-04-22 21:10:21 +0200790 bool zero, struct parse_events_error *err)
Jiri Olsacd82a322012-03-15 20:09:17 +0100791{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300792 struct perf_pmu_format *format;
Jiri Olsacd82a322012-03-15 20:09:17 +0100793 __u64 *vp;
Adrian Hunter0efe6b62015-07-17 19:33:50 +0300794 __u64 val, max_val;
Jiri Olsacd82a322012-03-15 20:09:17 +0100795
796 /*
Cody P Schafer688d4df2015-01-07 17:13:50 -0800797 * If this is a parameter we've already used for parameterized-eval,
798 * skip it in normal eval.
799 */
800 if (term->used)
801 return 0;
802
803 /*
Jiri Olsacd82a322012-03-15 20:09:17 +0100804 * Hardcoded terms should be already in, so nothing
805 * to be done for them.
806 */
807 if (parse_events__is_hardcoded_term(term))
808 return 0;
809
Jiri Olsacd82a322012-03-15 20:09:17 +0100810 format = pmu_find_format(formats, term->config);
Cody P Schafer688d4df2015-01-07 17:13:50 -0800811 if (!format) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900812 if (verbose > 0)
Cody P Schafer688d4df2015-01-07 17:13:50 -0800813 printf("Invalid event/parameter '%s'\n", term->config);
Jiri Olsae64b0202015-04-22 21:10:21 +0200814 if (err) {
He Kuangffeb8832015-09-28 03:52:14 +0000815 char *pmu_term = pmu_formats_string(formats);
816
Jiri Olsae64b0202015-04-22 21:10:21 +0200817 err->idx = term->err_term;
818 err->str = strdup("unknown term");
He Kuangffeb8832015-09-28 03:52:14 +0000819 err->help = parse_events_formats_error_string(pmu_term);
820 free(pmu_term);
Jiri Olsae64b0202015-04-22 21:10:21 +0200821 }
Jiri Olsacd82a322012-03-15 20:09:17 +0100822 return -EINVAL;
Cody P Schafer688d4df2015-01-07 17:13:50 -0800823 }
Jiri Olsacd82a322012-03-15 20:09:17 +0100824
825 switch (format->value) {
826 case PERF_PMU_FORMAT_VALUE_CONFIG:
827 vp = &attr->config;
828 break;
829 case PERF_PMU_FORMAT_VALUE_CONFIG1:
830 vp = &attr->config1;
831 break;
832 case PERF_PMU_FORMAT_VALUE_CONFIG2:
833 vp = &attr->config2;
834 break;
835 default:
836 return -EINVAL;
837 }
838
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200839 /*
Cody P Schafer688d4df2015-01-07 17:13:50 -0800840 * Either directly use a numeric term, or try to translate string terms
841 * using event parameters.
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200842 */
Jiri Olsa99e71382017-02-17 15:00:56 +0100843 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
844 if (term->no_value &&
845 bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) {
846 if (err) {
847 err->idx = term->err_val;
848 err->str = strdup("no value assigned for term");
849 }
850 return -EINVAL;
851 }
852
Cody P Schafer688d4df2015-01-07 17:13:50 -0800853 val = term->val.num;
Jiri Olsa99e71382017-02-17 15:00:56 +0100854 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
Cody P Schafer688d4df2015-01-07 17:13:50 -0800855 if (strcmp(term->val.str, "?")) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900856 if (verbose > 0) {
Cody P Schafer688d4df2015-01-07 17:13:50 -0800857 pr_info("Invalid sysfs entry %s=%s\n",
858 term->config, term->val.str);
Jiri Olsae64b0202015-04-22 21:10:21 +0200859 }
860 if (err) {
861 err->idx = term->err_val;
862 err->str = strdup("expected numeric value");
863 }
Cody P Schafer688d4df2015-01-07 17:13:50 -0800864 return -EINVAL;
865 }
866
867 if (pmu_resolve_param_term(term, head_terms, &val))
868 return -EINVAL;
869 } else
870 return -EINVAL;
871
Adrian Hunter0efe6b62015-07-17 19:33:50 +0300872 max_val = pmu_format_max_value(format->bits);
873 if (val > max_val) {
874 if (err) {
875 err->idx = term->err_val;
876 if (asprintf(&err->str,
877 "value too big for format, maximum is %llu",
878 (unsigned long long)max_val) < 0)
879 err->str = strdup("value too big for format");
880 return -EINVAL;
881 }
882 /*
883 * Assume we don't care if !err, in which case the value will be
884 * silently truncated.
885 */
886 }
887
Cody P Schafer688d4df2015-01-07 17:13:50 -0800888 pmu_format_value(format->bits, val, vp, zero);
Jiri Olsacd82a322012-03-15 20:09:17 +0100889 return 0;
890}
891
Jiri Olsacff7f952012-11-10 01:46:50 +0100892int perf_pmu__config_terms(struct list_head *formats,
893 struct perf_event_attr *attr,
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300894 struct list_head *head_terms,
Jiri Olsae64b0202015-04-22 21:10:21 +0200895 bool zero, struct parse_events_error *err)
Jiri Olsacd82a322012-03-15 20:09:17 +0100896{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -0300897 struct parse_events_term *term;
Jiri Olsacd82a322012-03-15 20:09:17 +0100898
Cody P Schafer688d4df2015-01-07 17:13:50 -0800899 list_for_each_entry(term, head_terms, list) {
Jiri Olsae64b0202015-04-22 21:10:21 +0200900 if (pmu_config_term(formats, attr, term, head_terms,
901 zero, err))
Jiri Olsacd82a322012-03-15 20:09:17 +0100902 return -EINVAL;
Cody P Schafer688d4df2015-01-07 17:13:50 -0800903 }
Jiri Olsacd82a322012-03-15 20:09:17 +0100904
905 return 0;
906}
907
908/*
909 * Configures event's 'attr' parameter based on the:
910 * 1) users input - specified in terms parameter
911 * 2) pmu format definitions - specified by pmu parameter
912 */
913int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
Jiri Olsae64b0202015-04-22 21:10:21 +0200914 struct list_head *head_terms,
915 struct parse_events_error *err)
Jiri Olsacd82a322012-03-15 20:09:17 +0100916{
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300917 bool zero = !!pmu->default_config;
918
Jiri Olsacd82a322012-03-15 20:09:17 +0100919 attr->type = pmu->type;
Jiri Olsae64b0202015-04-22 21:10:21 +0200920 return perf_pmu__config_terms(&pmu->format, attr, head_terms,
921 zero, err);
Jiri Olsacd82a322012-03-15 20:09:17 +0100922}
923
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300924static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
925 struct parse_events_term *term)
Zheng Yana6146d52012-06-15 14:31:41 +0800926{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300927 struct perf_pmu_alias *alias;
Zheng Yana6146d52012-06-15 14:31:41 +0800928 char *name;
929
930 if (parse_events__is_hardcoded_term(term))
931 return NULL;
932
933 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
934 if (term->val.num != 1)
935 return NULL;
936 if (pmu_find_format(&pmu->format, term->config))
937 return NULL;
938 name = term->config;
939 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
940 if (strcasecmp(term->config, "event"))
941 return NULL;
942 name = term->val.str;
943 } else {
944 return NULL;
945 }
946
947 list_for_each_entry(alias, &pmu->aliases, list) {
948 if (!strcasecmp(alias->name, name))
949 return alias;
950 }
951 return NULL;
952}
953
Stephane Eranian410136f2013-11-12 17:58:49 +0100954
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100955static int check_info_data(struct perf_pmu_alias *alias,
956 struct perf_pmu_info *info)
Stephane Eranian410136f2013-11-12 17:58:49 +0100957{
958 /*
959 * Only one term in event definition can
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100960 * define unit, scale and snapshot, fail
961 * if there's more than one.
Stephane Eranian410136f2013-11-12 17:58:49 +0100962 */
Arnaldo Carvalho de Melob30a7d12017-02-15 10:06:20 -0300963 if ((info->unit && alias->unit[0]) ||
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100964 (info->scale && alias->scale) ||
965 (info->snapshot && alias->snapshot))
Stephane Eranian410136f2013-11-12 17:58:49 +0100966 return -EINVAL;
967
Arnaldo Carvalho de Melob30a7d12017-02-15 10:06:20 -0300968 if (alias->unit[0])
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100969 info->unit = alias->unit;
Stephane Eranian410136f2013-11-12 17:58:49 +0100970
971 if (alias->scale)
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100972 info->scale = alias->scale;
973
974 if (alias->snapshot)
975 info->snapshot = alias->snapshot;
Stephane Eranian410136f2013-11-12 17:58:49 +0100976
977 return 0;
978}
979
Zheng Yana6146d52012-06-15 14:31:41 +0800980/*
981 * Find alias in the terms list and replace it with the terms
982 * defined for the alias
983 */
Stephane Eranian410136f2013-11-12 17:58:49 +0100984int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
Matt Fleming46441bd2014-09-24 15:04:06 +0100985 struct perf_pmu_info *info)
Zheng Yana6146d52012-06-15 14:31:41 +0800986{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -0300987 struct parse_events_term *term, *h;
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300988 struct perf_pmu_alias *alias;
Zheng Yana6146d52012-06-15 14:31:41 +0800989 int ret;
990
Matt Fleming044330c2014-11-21 10:31:12 +0100991 info->per_pkg = false;
992
Stephane Eranian8a398892014-01-17 16:34:05 +0100993 /*
994 * Mark unit and scale as not set
995 * (different from default values, see below)
996 */
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100997 info->unit = NULL;
998 info->scale = 0.0;
999 info->snapshot = false;
Andi Kleen37932c12017-03-20 13:17:08 -07001000 info->metric_expr = NULL;
Andi Kleen96284812017-03-20 13:17:10 -07001001 info->metric_name = NULL;
Stephane Eranian410136f2013-11-12 17:58:49 +01001002
Zheng Yana6146d52012-06-15 14:31:41 +08001003 list_for_each_entry_safe(term, h, head_terms, list) {
1004 alias = pmu_find_alias(pmu, term);
1005 if (!alias)
1006 continue;
1007 ret = pmu_alias_terms(alias, &term->list);
1008 if (ret)
1009 return ret;
Stephane Eranian410136f2013-11-12 17:58:49 +01001010
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001011 ret = check_info_data(alias, info);
Stephane Eranian410136f2013-11-12 17:58:49 +01001012 if (ret)
1013 return ret;
1014
Matt Fleming044330c2014-11-21 10:31:12 +01001015 if (alias->per_pkg)
1016 info->per_pkg = true;
Andi Kleen37932c12017-03-20 13:17:08 -07001017 info->metric_expr = alias->metric_expr;
Andi Kleen96284812017-03-20 13:17:10 -07001018 info->metric_name = alias->metric_name;
Matt Fleming044330c2014-11-21 10:31:12 +01001019
Zheng Yana6146d52012-06-15 14:31:41 +08001020 list_del(&term->list);
1021 free(term);
1022 }
Stephane Eranian8a398892014-01-17 16:34:05 +01001023
1024 /*
1025 * if no unit or scale foundin aliases, then
1026 * set defaults as for evsel
1027 * unit cannot left to NULL
1028 */
Matt Fleming46441bd2014-09-24 15:04:06 +01001029 if (info->unit == NULL)
1030 info->unit = "";
Stephane Eranian8a398892014-01-17 16:34:05 +01001031
Matt Fleming46441bd2014-09-24 15:04:06 +01001032 if (info->scale == 0.0)
1033 info->scale = 1.0;
Stephane Eranian8a398892014-01-17 16:34:05 +01001034
Zheng Yana6146d52012-06-15 14:31:41 +08001035 return 0;
1036}
1037
Jiri Olsacd82a322012-03-15 20:09:17 +01001038int perf_pmu__new_format(struct list_head *list, char *name,
1039 int config, unsigned long *bits)
1040{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -03001041 struct perf_pmu_format *format;
Jiri Olsacd82a322012-03-15 20:09:17 +01001042
1043 format = zalloc(sizeof(*format));
1044 if (!format)
1045 return -ENOMEM;
1046
1047 format->name = strdup(name);
1048 format->value = config;
1049 memcpy(format->bits, bits, sizeof(format->bits));
1050
1051 list_add_tail(&format->list, list);
1052 return 0;
1053}
1054
1055void perf_pmu__set_format(unsigned long *bits, long from, long to)
1056{
1057 long b;
1058
1059 if (!to)
1060 to = from;
1061
Sukadev Bhattiprolu15268132013-01-17 09:11:30 -08001062 memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
Jiri Olsacd82a322012-03-15 20:09:17 +01001063 for (b = from; b <= to; b++)
1064 set_bit(b, bits);
1065}
Andi Kleendc098b32013-04-20 11:02:29 -07001066
Cody P Schaferaaea3612015-01-07 17:13:51 -08001067static int sub_non_neg(int a, int b)
1068{
1069 if (b > a)
1070 return 0;
1071 return a - b;
1072}
1073
Andi Kleendc098b32013-04-20 11:02:29 -07001074static char *format_alias(char *buf, int len, struct perf_pmu *pmu,
1075 struct perf_pmu_alias *alias)
1076{
Cody P Schaferaaea3612015-01-07 17:13:51 -08001077 struct parse_events_term *term;
1078 int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name);
1079
1080 list_for_each_entry(term, &alias->terms, list) {
1081 if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
1082 used += snprintf(buf + used, sub_non_neg(len, used),
1083 ",%s=%s", term->config,
1084 term->val.str);
1085 }
1086
1087 if (sub_non_neg(len, used) > 0) {
1088 buf[used] = '/';
1089 used++;
1090 }
1091 if (sub_non_neg(len, used) > 0) {
1092 buf[used] = '\0';
1093 used++;
1094 } else
1095 buf[len - 1] = '\0';
1096
Andi Kleendc098b32013-04-20 11:02:29 -07001097 return buf;
1098}
1099
1100static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu,
1101 struct perf_pmu_alias *alias)
1102{
1103 snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name);
1104 return buf;
1105}
1106
Andi Kleendd5f1032016-09-15 15:24:50 -07001107struct sevent {
Andi Kleen08e60ed2016-09-15 15:24:43 -07001108 char *name;
1109 char *desc;
Andi Kleendd5f1032016-09-15 15:24:50 -07001110 char *topic;
Andi Kleenf2361022017-01-27 18:03:40 -08001111 char *str;
1112 char *pmu;
Andi Kleen7f372a62017-03-20 13:17:09 -07001113 char *metric_expr;
Andi Kleen96284812017-03-20 13:17:10 -07001114 char *metric_name;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001115};
1116
Andi Kleendd5f1032016-09-15 15:24:50 -07001117static int cmp_sevent(const void *a, const void *b)
Andi Kleendc098b32013-04-20 11:02:29 -07001118{
Andi Kleendd5f1032016-09-15 15:24:50 -07001119 const struct sevent *as = a;
1120 const struct sevent *bs = b;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001121
1122 /* Put extra events last */
1123 if (!!as->desc != !!bs->desc)
1124 return !!as->desc - !!bs->desc;
Andi Kleendd5f1032016-09-15 15:24:50 -07001125 if (as->topic && bs->topic) {
1126 int n = strcmp(as->topic, bs->topic);
1127
1128 if (n)
1129 return n;
1130 }
Andi Kleen08e60ed2016-09-15 15:24:43 -07001131 return strcmp(as->name, bs->name);
1132}
1133
1134static void wordwrap(char *s, int start, int max, int corr)
1135{
1136 int column = start;
1137 int n;
1138
1139 while (*s) {
1140 int wlen = strcspn(s, " \t");
1141
1142 if (column + wlen >= max && column > start) {
1143 printf("\n%*s", start, "");
1144 column = start + corr;
1145 }
1146 n = printf("%s%.*s", column > start ? " " : "", wlen, s);
1147 if (n <= 0)
1148 break;
1149 s += wlen;
1150 column += n;
Taeung Songaa4beb12017-04-07 23:24:20 +09001151 s = ltrim(s);
Andi Kleen08e60ed2016-09-15 15:24:43 -07001152 }
Andi Kleendc098b32013-04-20 11:02:29 -07001153}
1154
Sukadev Bhattiproluc8d68282016-09-15 15:24:48 -07001155void print_pmu_events(const char *event_glob, bool name_only, bool quiet_flag,
Andi Kleenbf874fc2017-03-20 13:17:11 -07001156 bool long_desc, bool details_flag)
Andi Kleendc098b32013-04-20 11:02:29 -07001157{
1158 struct perf_pmu *pmu;
1159 struct perf_pmu_alias *alias;
1160 char buf[1024];
1161 int printed = 0;
1162 int len, j;
Andi Kleendd5f1032016-09-15 15:24:50 -07001163 struct sevent *aliases;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001164 int numdesc = 0;
Andi Kleen61eb2eb2016-09-15 15:24:44 -07001165 int columns = pager_get_columns();
Andi Kleendd5f1032016-09-15 15:24:50 -07001166 char *topic = NULL;
Andi Kleendc098b32013-04-20 11:02:29 -07001167
1168 pmu = NULL;
1169 len = 0;
Adrian Hunter42634bc2014-10-23 13:45:10 +03001170 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
Andi Kleendc098b32013-04-20 11:02:29 -07001171 list_for_each_entry(alias, &pmu->aliases, list)
1172 len++;
Adrian Hunter42634bc2014-10-23 13:45:10 +03001173 if (pmu->selectable)
1174 len++;
1175 }
Andi Kleendd5f1032016-09-15 15:24:50 -07001176 aliases = zalloc(sizeof(struct sevent) * len);
Andi Kleendc098b32013-04-20 11:02:29 -07001177 if (!aliases)
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001178 goto out_enomem;
Andi Kleendc098b32013-04-20 11:02:29 -07001179 pmu = NULL;
1180 j = 0;
Adrian Hunter42634bc2014-10-23 13:45:10 +03001181 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
Andi Kleendc098b32013-04-20 11:02:29 -07001182 list_for_each_entry(alias, &pmu->aliases, list) {
Andi Kleen08e60ed2016-09-15 15:24:43 -07001183 char *name = alias->desc ? alias->name :
1184 format_alias(buf, sizeof(buf), pmu, alias);
Andi Kleendc098b32013-04-20 11:02:29 -07001185 bool is_cpu = !strcmp(pmu->name, "cpu");
1186
1187 if (event_glob != NULL &&
Andi Kleen38d14f02016-10-19 10:50:01 -07001188 !(strglobmatch_nocase(name, event_glob) ||
1189 (!is_cpu && strglobmatch_nocase(alias->name,
Andi Kleen67bdc352016-10-19 11:45:23 -07001190 event_glob)) ||
1191 (alias->topic &&
1192 strglobmatch_nocase(alias->topic, event_glob))))
Andi Kleendc098b32013-04-20 11:02:29 -07001193 continue;
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001194
Andi Kleen08e60ed2016-09-15 15:24:43 -07001195 if (is_cpu && !name_only && !alias->desc)
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001196 name = format_alias_or(buf, sizeof(buf), pmu, alias);
1197
Andi Kleen08e60ed2016-09-15 15:24:43 -07001198 aliases[j].name = name;
1199 if (is_cpu && !name_only && !alias->desc)
1200 aliases[j].name = format_alias_or(buf,
1201 sizeof(buf),
1202 pmu, alias);
1203 aliases[j].name = strdup(aliases[j].name);
1204 if (!aliases[j].name)
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001205 goto out_enomem;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001206
Sukadev Bhattiproluc8d68282016-09-15 15:24:48 -07001207 aliases[j].desc = long_desc ? alias->long_desc :
1208 alias->desc;
Andi Kleendd5f1032016-09-15 15:24:50 -07001209 aliases[j].topic = alias->topic;
Andi Kleenf2361022017-01-27 18:03:40 -08001210 aliases[j].str = alias->str;
1211 aliases[j].pmu = pmu->name;
Andi Kleen7f372a62017-03-20 13:17:09 -07001212 aliases[j].metric_expr = alias->metric_expr;
Andi Kleen96284812017-03-20 13:17:10 -07001213 aliases[j].metric_name = alias->metric_name;
Andi Kleendc098b32013-04-20 11:02:29 -07001214 j++;
1215 }
Arnaldo Carvalho de Melofa52cea2015-10-02 15:28:16 -03001216 if (pmu->selectable &&
1217 (event_glob == NULL || strglobmatch(pmu->name, event_glob))) {
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001218 char *s;
1219 if (asprintf(&s, "%s//", pmu->name) < 0)
1220 goto out_enomem;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001221 aliases[j].name = s;
Adrian Hunter42634bc2014-10-23 13:45:10 +03001222 j++;
1223 }
1224 }
Andi Kleendc098b32013-04-20 11:02:29 -07001225 len = j;
Andi Kleendd5f1032016-09-15 15:24:50 -07001226 qsort(aliases, len, sizeof(struct sevent), cmp_sevent);
Andi Kleendc098b32013-04-20 11:02:29 -07001227 for (j = 0; j < len; j++) {
Andi Kleen15b22ed2017-01-27 18:03:38 -08001228 /* Skip duplicates */
1229 if (j > 0 && !strcmp(aliases[j].name, aliases[j - 1].name))
1230 continue;
Andi Kleendc098b32013-04-20 11:02:29 -07001231 if (name_only) {
Andi Kleen08e60ed2016-09-15 15:24:43 -07001232 printf("%s ", aliases[j].name);
Andi Kleendc098b32013-04-20 11:02:29 -07001233 continue;
1234 }
Andi Kleen1c5f01f2016-09-15 15:24:45 -07001235 if (aliases[j].desc && !quiet_flag) {
Andi Kleen08e60ed2016-09-15 15:24:43 -07001236 if (numdesc++ == 0)
1237 printf("\n");
Andi Kleendd5f1032016-09-15 15:24:50 -07001238 if (aliases[j].topic && (!topic ||
1239 strcmp(topic, aliases[j].topic))) {
1240 printf("%s%s:\n", topic ? "\n" : "",
1241 aliases[j].topic);
1242 topic = aliases[j].topic;
1243 }
Andi Kleen08e60ed2016-09-15 15:24:43 -07001244 printf(" %-50s\n", aliases[j].name);
1245 printf("%*s", 8, "[");
1246 wordwrap(aliases[j].desc, 8, columns, 0);
1247 printf("]\n");
Andi Kleenbf874fc2017-03-20 13:17:11 -07001248 if (details_flag) {
Andi Kleen7f372a62017-03-20 13:17:09 -07001249 printf("%*s%s/%s/ ", 8, "", aliases[j].pmu, aliases[j].str);
Andi Kleen96284812017-03-20 13:17:10 -07001250 if (aliases[j].metric_name)
1251 printf(" MetricName: %s", aliases[j].metric_name);
Andi Kleen7f372a62017-03-20 13:17:09 -07001252 if (aliases[j].metric_expr)
1253 printf(" MetricExpr: %s", aliases[j].metric_expr);
1254 putchar('\n');
1255 }
Andi Kleen08e60ed2016-09-15 15:24:43 -07001256 } else
1257 printf(" %-50s [Kernel PMU event]\n", aliases[j].name);
Andi Kleendc098b32013-04-20 11:02:29 -07001258 printed++;
1259 }
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03001260 if (printed && pager_in_use())
Andi Kleendc098b32013-04-20 11:02:29 -07001261 printf("\n");
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001262out_free:
1263 for (j = 0; j < len; j++)
Andi Kleen08e60ed2016-09-15 15:24:43 -07001264 zfree(&aliases[j].name);
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001265 zfree(&aliases);
1266 return;
1267
1268out_enomem:
1269 printf("FATAL: not enough memory to print PMU events\n");
1270 if (aliases)
1271 goto out_free;
Andi Kleendc098b32013-04-20 11:02:29 -07001272}
Andi Kleen4cabc3d2013-08-21 16:47:26 -07001273
1274bool pmu_have_event(const char *pname, const char *name)
1275{
1276 struct perf_pmu *pmu;
1277 struct perf_pmu_alias *alias;
1278
1279 pmu = NULL;
1280 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1281 if (strcmp(pname, pmu->name))
1282 continue;
1283 list_for_each_entry(alias, &pmu->aliases, list)
1284 if (!strcmp(alias->name, name))
1285 return true;
1286 }
1287 return false;
1288}
Adrian Hunter7d4bdab2014-07-31 09:00:50 +03001289
1290static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
1291{
1292 struct stat st;
1293 char path[PATH_MAX];
1294 const char *sysfs;
1295
1296 sysfs = sysfs__mountpoint();
1297 if (!sysfs)
1298 return NULL;
1299
1300 snprintf(path, PATH_MAX,
1301 "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name);
1302
1303 if (stat(path, &st) < 0)
1304 return NULL;
1305
1306 return fopen(path, "r");
1307}
1308
1309int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
1310 ...)
1311{
1312 va_list args;
1313 FILE *file;
1314 int ret = EOF;
1315
1316 va_start(args, fmt);
1317 file = perf_pmu__open_file(pmu, name);
1318 if (file) {
1319 ret = vfscanf(file, fmt, args);
1320 fclose(file);
1321 }
1322 va_end(args);
1323 return ret;
1324}