blob: 6dc3cc050105f533f08a40c8b767b60a52fe8251 [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,
234 char *unit, char *perpkg)
Zheng Yana6146d52012-06-15 14:31:41 +0800235{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300236 struct perf_pmu_alias *alias;
Zheng Yana6146d52012-06-15 14:31:41 +0800237 int ret;
Andi Kleenfedb2b52017-01-27 18:03:37 -0800238 int num;
Zheng Yana6146d52012-06-15 14:31:41 +0800239
Zheng Yana6146d52012-06-15 14:31:41 +0800240 alias = malloc(sizeof(*alias));
241 if (!alias)
242 return -ENOMEM;
243
244 INIT_LIST_HEAD(&alias->terms);
Stephane Eranian410136f2013-11-12 17:58:49 +0100245 alias->scale = 1.0;
246 alias->unit[0] = '\0';
Matt Fleming044330c2014-11-21 10:31:12 +0100247 alias->per_pkg = false;
Stephane Eranian84530922016-01-06 19:50:01 +0100248 alias->snapshot = false;
Stephane Eranian410136f2013-11-12 17:58:49 +0100249
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700250 ret = parse_events_terms(&alias->terms, val);
Zheng Yana6146d52012-06-15 14:31:41 +0800251 if (ret) {
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700252 pr_err("Cannot parse alias %s: %d\n", val, ret);
Zheng Yana6146d52012-06-15 14:31:41 +0800253 free(alias);
254 return ret;
255 }
256
257 alias->name = strdup(name);
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700258 if (dir) {
259 /*
260 * load unit name and scale if available
261 */
262 perf_pmu__parse_unit(alias, dir, name);
263 perf_pmu__parse_scale(alias, dir, name);
264 perf_pmu__parse_per_pkg(alias, dir, name);
265 perf_pmu__parse_snapshot(alias, dir, name);
266 }
Stephane Eranian410136f2013-11-12 17:58:49 +0100267
Andi Kleen08e60ed2016-09-15 15:24:43 -0700268 alias->desc = desc ? strdup(desc) : NULL;
Sukadev Bhattiproluc8d68282016-09-15 15:24:48 -0700269 alias->long_desc = long_desc ? strdup(long_desc) :
270 desc ? strdup(desc) : NULL;
Andi Kleendd5f1032016-09-15 15:24:50 -0700271 alias->topic = topic ? strdup(topic) : NULL;
Andi Kleenfedb2b52017-01-27 18:03:37 -0800272 if (unit) {
273 if (convert_scale(unit, &unit, &alias->scale) < 0)
274 return -1;
275 snprintf(alias->unit, sizeof(alias->unit), "%s", unit);
276 }
277 alias->per_pkg = perpkg && sscanf(perpkg, "%d", &num) == 1 && num == 1;
Zheng Yana6146d52012-06-15 14:31:41 +0800278 list_add_tail(&alias->list, list);
Stephane Eranian410136f2013-11-12 17:58:49 +0100279
Zheng Yana6146d52012-06-15 14:31:41 +0800280 return 0;
281}
282
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700283static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file)
284{
285 char buf[256];
286 int ret;
287
288 ret = fread(buf, 1, sizeof(buf), file);
289 if (ret == 0)
290 return -EINVAL;
291
292 buf[ret] = 0;
293
Andi Kleenfedb2b52017-01-27 18:03:37 -0800294 return __perf_pmu__new_alias(list, dir, name, NULL, buf, NULL, NULL, NULL,
295 NULL);
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700296}
297
Matt Fleming46441bd2014-09-24 15:04:06 +0100298static inline bool pmu_alias_info_file(char *name)
299{
300 size_t len;
301
302 len = strlen(name);
303 if (len > 5 && !strcmp(name + len - 5, ".unit"))
304 return true;
305 if (len > 6 && !strcmp(name + len - 6, ".scale"))
306 return true;
Matt Fleming044330c2014-11-21 10:31:12 +0100307 if (len > 8 && !strcmp(name + len - 8, ".per-pkg"))
308 return true;
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100309 if (len > 9 && !strcmp(name + len - 9, ".snapshot"))
310 return true;
Matt Fleming46441bd2014-09-24 15:04:06 +0100311
312 return false;
313}
314
Zheng Yana6146d52012-06-15 14:31:41 +0800315/*
316 * Process all the sysfs attributes located under the directory
317 * specified in 'dir' parameter.
318 */
319static int pmu_aliases_parse(char *dir, struct list_head *head)
320{
321 struct dirent *evt_ent;
322 DIR *event_dir;
Zheng Yana6146d52012-06-15 14:31:41 +0800323
324 event_dir = opendir(dir);
325 if (!event_dir)
326 return -EINVAL;
327
Andi Kleen940db6d2016-02-17 14:44:55 -0800328 while ((evt_ent = readdir(event_dir))) {
Zheng Yana6146d52012-06-15 14:31:41 +0800329 char path[PATH_MAX];
330 char *name = evt_ent->d_name;
331 FILE *file;
332
333 if (!strcmp(name, ".") || !strcmp(name, ".."))
334 continue;
335
Stephane Eranian410136f2013-11-12 17:58:49 +0100336 /*
Matt Fleming46441bd2014-09-24 15:04:06 +0100337 * skip info files parsed in perf_pmu__new_alias()
Stephane Eranian410136f2013-11-12 17:58:49 +0100338 */
Matt Fleming46441bd2014-09-24 15:04:06 +0100339 if (pmu_alias_info_file(name))
Stephane Eranian410136f2013-11-12 17:58:49 +0100340 continue;
341
Zheng Yana6146d52012-06-15 14:31:41 +0800342 snprintf(path, PATH_MAX, "%s/%s", dir, name);
343
Zheng Yana6146d52012-06-15 14:31:41 +0800344 file = fopen(path, "r");
Andi Kleen940db6d2016-02-17 14:44:55 -0800345 if (!file) {
346 pr_debug("Cannot open %s\n", path);
347 continue;
348 }
Stephane Eranian410136f2013-11-12 17:58:49 +0100349
Andi Kleen940db6d2016-02-17 14:44:55 -0800350 if (perf_pmu__new_alias(head, dir, name, file) < 0)
351 pr_debug("Cannot set up %s\n", name);
Zheng Yana6146d52012-06-15 14:31:41 +0800352 fclose(file);
353 }
354
355 closedir(event_dir);
Andi Kleen940db6d2016-02-17 14:44:55 -0800356 return 0;
Zheng Yana6146d52012-06-15 14:31:41 +0800357}
358
359/*
360 * Reading the pmu event aliases definition, which should be located at:
361 * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
362 */
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300363static int pmu_aliases(const char *name, struct list_head *head)
Zheng Yana6146d52012-06-15 14:31:41 +0800364{
365 struct stat st;
366 char path[PATH_MAX];
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -0300367 const char *sysfs = sysfs__mountpoint();
Zheng Yana6146d52012-06-15 14:31:41 +0800368
Zheng Yana6146d52012-06-15 14:31:41 +0800369 if (!sysfs)
370 return -1;
371
372 snprintf(path, PATH_MAX,
373 "%s/bus/event_source/devices/%s/events", sysfs, name);
374
375 if (stat(path, &st) < 0)
Jiri Olsa3fded962012-10-10 14:53:16 +0200376 return 0; /* no error if 'events' does not exist */
Zheng Yana6146d52012-06-15 14:31:41 +0800377
378 if (pmu_aliases_parse(path, head))
379 return -1;
380
381 return 0;
382}
383
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300384static int pmu_alias_terms(struct perf_pmu_alias *alias,
Zheng Yana6146d52012-06-15 14:31:41 +0800385 struct list_head *terms)
386{
Jiri Olsa7c2f8162014-04-16 20:49:02 +0200387 struct parse_events_term *term, *cloned;
Zheng Yana6146d52012-06-15 14:31:41 +0800388 LIST_HEAD(list);
389 int ret;
390
391 list_for_each_entry(term, &alias->terms, list) {
Jiri Olsa7c2f8162014-04-16 20:49:02 +0200392 ret = parse_events_term__clone(&cloned, term);
Zheng Yana6146d52012-06-15 14:31:41 +0800393 if (ret) {
Arnaldo Carvalho de Melo682dc242016-02-12 16:48:00 -0300394 parse_events_terms__purge(&list);
Zheng Yana6146d52012-06-15 14:31:41 +0800395 return ret;
396 }
Jiri Olsa7c2f8162014-04-16 20:49:02 +0200397 list_add_tail(&cloned->list, &list);
Zheng Yana6146d52012-06-15 14:31:41 +0800398 }
399 list_splice(&list, terms);
400 return 0;
401}
402
Jiri Olsacd82a322012-03-15 20:09:17 +0100403/*
404 * Reading/parsing the default pmu type value, which should be
405 * located at:
406 * /sys/bus/event_source/devices/<dev>/type as sysfs attribute.
407 */
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300408static int pmu_type(const char *name, __u32 *type)
Jiri Olsacd82a322012-03-15 20:09:17 +0100409{
410 struct stat st;
411 char path[PATH_MAX];
Jiri Olsacd82a322012-03-15 20:09:17 +0100412 FILE *file;
413 int ret = 0;
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -0300414 const char *sysfs = sysfs__mountpoint();
Jiri Olsacd82a322012-03-15 20:09:17 +0100415
Jiri Olsacd82a322012-03-15 20:09:17 +0100416 if (!sysfs)
417 return -1;
418
419 snprintf(path, PATH_MAX,
Robert Richter50a96672012-08-16 21:10:24 +0200420 "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name);
Jiri Olsacd82a322012-03-15 20:09:17 +0100421
422 if (stat(path, &st) < 0)
423 return -1;
424
425 file = fopen(path, "r");
426 if (!file)
427 return -EINVAL;
428
429 if (1 != fscanf(file, "%u", type))
430 ret = -1;
431
432 fclose(file);
433 return ret;
434}
435
Robert Richter50a96672012-08-16 21:10:24 +0200436/* Add all pmus in sysfs to pmu list: */
437static void pmu_read_sysfs(void)
438{
439 char path[PATH_MAX];
Robert Richter50a96672012-08-16 21:10:24 +0200440 DIR *dir;
441 struct dirent *dent;
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -0300442 const char *sysfs = sysfs__mountpoint();
Robert Richter50a96672012-08-16 21:10:24 +0200443
Robert Richter50a96672012-08-16 21:10:24 +0200444 if (!sysfs)
445 return;
446
447 snprintf(path, PATH_MAX,
448 "%s" EVENT_SOURCE_DEVICE_PATH, sysfs);
449
450 dir = opendir(path);
451 if (!dir)
452 return;
453
454 while ((dent = readdir(dir))) {
455 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
456 continue;
457 /* add to static LIST_HEAD(pmus): */
458 perf_pmu__find(dent->d_name);
459 }
460
461 closedir(dir);
462}
463
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300464static struct cpu_map *pmu_cpumask(const char *name)
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800465{
466 struct stat st;
467 char path[PATH_MAX];
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800468 FILE *file;
469 struct cpu_map *cpus;
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -0300470 const char *sysfs = sysfs__mountpoint();
Mark Rutland7e3fcffe2016-09-08 11:21:52 +0100471 const char *templates[] = {
472 "%s/bus/event_source/devices/%s/cpumask",
473 "%s/bus/event_source/devices/%s/cpus",
474 NULL
475 };
476 const char **template;
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800477
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800478 if (!sysfs)
479 return NULL;
480
Mark Rutland7e3fcffe2016-09-08 11:21:52 +0100481 for (template = templates; *template; template++) {
482 snprintf(path, PATH_MAX, *template, sysfs, name);
483 if (stat(path, &st) == 0)
484 break;
485 }
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800486
Mark Rutland7e3fcffe2016-09-08 11:21:52 +0100487 if (!*template)
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800488 return NULL;
489
490 file = fopen(path, "r");
491 if (!file)
492 return NULL;
493
494 cpus = cpu_map__read(file);
495 fclose(file);
496 return cpus;
497}
498
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700499/*
500 * Return the CPU id as a raw string.
501 *
502 * Each architecture should provide a more precise id string that
503 * can be use to match the architecture's "mapfile".
504 */
505char * __weak get_cpuid_str(void)
506{
507 return NULL;
508}
509
510/*
511 * From the pmu_events_map, find the table of PMU events that corresponds
512 * to the current running CPU. Then, add all PMU events from that table
513 * as aliases.
514 */
Andi Kleenfedb2b52017-01-27 18:03:37 -0800515static void pmu_add_cpu_aliases(struct list_head *head, const char *name)
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700516{
517 int i;
518 struct pmu_events_map *map;
519 struct pmu_event *pe;
520 char *cpuid;
Andi Kleenfb967062016-10-13 14:15:24 -0700521 static bool printed;
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700522
Andi Kleenfc06e2a2016-09-15 15:24:46 -0700523 cpuid = getenv("PERF_CPUID");
524 if (cpuid)
525 cpuid = strdup(cpuid);
526 if (!cpuid)
527 cpuid = get_cpuid_str();
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700528 if (!cpuid)
529 return;
530
Andi Kleenfb967062016-10-13 14:15:24 -0700531 if (!printed) {
532 pr_debug("Using CPUID %s\n", cpuid);
533 printed = true;
534 }
Andi Kleenfc06e2a2016-09-15 15:24:46 -0700535
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700536 i = 0;
537 while (1) {
538 map = &pmu_events_map[i++];
539 if (!map->table)
540 goto out;
541
542 if (!strcmp(map->cpuid, cpuid))
543 break;
544 }
545
546 /*
547 * Found a matching PMU events table. Create aliases
548 */
549 i = 0;
550 while (1) {
Andi Kleenfedb2b52017-01-27 18:03:37 -0800551 const char *pname;
552
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700553 pe = &map->table[i++];
554 if (!pe->name)
555 break;
556
Andi Kleenfedb2b52017-01-27 18:03:37 -0800557 pname = pe->pmu ? pe->pmu : "cpu";
558 if (strncmp(pname, name, strlen(pname)))
559 continue;
560
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700561 /* need type casts to override 'const' */
562 __perf_pmu__new_alias(head, NULL, (char *)pe->name,
Sukadev Bhattiproluc8d68282016-09-15 15:24:48 -0700563 (char *)pe->desc, (char *)pe->event,
Andi Kleenfedb2b52017-01-27 18:03:37 -0800564 (char *)pe->long_desc, (char *)pe->topic,
565 (char *)pe->unit, (char *)pe->perpkg);
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700566 }
567
568out:
569 free(cpuid);
570}
571
Sukadev Bhattiproluc5de47f2015-06-10 00:25:07 -0700572struct perf_event_attr * __weak
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300573perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
574{
575 return NULL;
576}
577
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300578static struct perf_pmu *pmu_lookup(const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +0100579{
580 struct perf_pmu *pmu;
581 LIST_HEAD(format);
Zheng Yana6146d52012-06-15 14:31:41 +0800582 LIST_HEAD(aliases);
Jiri Olsacd82a322012-03-15 20:09:17 +0100583 __u32 type;
584
585 /*
586 * The pmu data we store & need consists of the pmu
587 * type value and format definitions. Load both right
588 * now.
589 */
590 if (pmu_format(name, &format))
591 return NULL;
592
Jiri Olsa3fded962012-10-10 14:53:16 +0200593 if (pmu_aliases(name, &aliases))
594 return NULL;
595
Andi Kleenfedb2b52017-01-27 18:03:37 -0800596 pmu_add_cpu_aliases(&aliases, name);
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700597
Jiri Olsacd82a322012-03-15 20:09:17 +0100598 if (pmu_type(name, &type))
599 return NULL;
600
601 pmu = zalloc(sizeof(*pmu));
602 if (!pmu)
603 return NULL;
604
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800605 pmu->cpus = pmu_cpumask(name);
606
Jiri Olsacd82a322012-03-15 20:09:17 +0100607 INIT_LIST_HEAD(&pmu->format);
Zheng Yana6146d52012-06-15 14:31:41 +0800608 INIT_LIST_HEAD(&pmu->aliases);
Jiri Olsacd82a322012-03-15 20:09:17 +0100609 list_splice(&format, &pmu->format);
Zheng Yana6146d52012-06-15 14:31:41 +0800610 list_splice(&aliases, &pmu->aliases);
Jiri Olsacd82a322012-03-15 20:09:17 +0100611 pmu->name = strdup(name);
612 pmu->type = type;
Robert Richter9bc8f9f2012-06-14 22:38:37 +0200613 list_add_tail(&pmu->list, &pmus);
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300614
615 pmu->default_config = perf_pmu__get_default_config(pmu);
616
Jiri Olsacd82a322012-03-15 20:09:17 +0100617 return pmu;
618}
619
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300620static struct perf_pmu *pmu_find(const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +0100621{
622 struct perf_pmu *pmu;
623
624 list_for_each_entry(pmu, &pmus, list)
625 if (!strcmp(pmu->name, name))
626 return pmu;
627
628 return NULL;
629}
630
Robert Richter50a96672012-08-16 21:10:24 +0200631struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
632{
633 /*
634 * pmu iterator: If pmu is NULL, we start at the begin,
635 * otherwise return the next pmu. Returns NULL on end.
636 */
637 if (!pmu) {
638 pmu_read_sysfs();
639 pmu = list_prepare_entry(pmu, &pmus, list);
640 }
641 list_for_each_entry_continue(pmu, &pmus, list)
642 return pmu;
643 return NULL;
644}
645
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300646struct perf_pmu *perf_pmu__find(const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +0100647{
648 struct perf_pmu *pmu;
649
650 /*
651 * Once PMU is loaded it stays in the list,
652 * so we keep us from multiple reading/parsing
653 * the pmu format definitions.
654 */
655 pmu = pmu_find(name);
656 if (pmu)
657 return pmu;
658
659 return pmu_lookup(name);
660}
661
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300662static struct perf_pmu_format *
Adrian Hunter09ff6072015-07-17 19:33:49 +0300663pmu_find_format(struct list_head *formats, const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +0100664{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300665 struct perf_pmu_format *format;
Jiri Olsacd82a322012-03-15 20:09:17 +0100666
667 list_for_each_entry(format, formats, list)
668 if (!strcmp(format->name, name))
669 return format;
670
671 return NULL;
672}
673
Adrian Hunter09ff6072015-07-17 19:33:49 +0300674__u64 perf_pmu__format_bits(struct list_head *formats, const char *name)
675{
676 struct perf_pmu_format *format = pmu_find_format(formats, name);
677 __u64 bits = 0;
678 int fbit;
679
680 if (!format)
681 return 0;
682
683 for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
684 bits |= 1ULL << fbit;
685
686 return bits;
687}
688
Jiri Olsacd82a322012-03-15 20:09:17 +0100689/*
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300690 * Sets value based on the format definition (format parameter)
Jiri Olsacd82a322012-03-15 20:09:17 +0100691 * and unformated value (value parameter).
Jiri Olsacd82a322012-03-15 20:09:17 +0100692 */
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300693static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
694 bool zero)
Jiri Olsacd82a322012-03-15 20:09:17 +0100695{
696 unsigned long fbit, vbit;
Jiri Olsacd82a322012-03-15 20:09:17 +0100697
698 for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
699
700 if (!test_bit(fbit, format))
701 continue;
702
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300703 if (value & (1llu << vbit++))
704 *v |= (1llu << fbit);
705 else if (zero)
706 *v &= ~(1llu << fbit);
Jiri Olsacd82a322012-03-15 20:09:17 +0100707 }
Jiri Olsacd82a322012-03-15 20:09:17 +0100708}
709
Adrian Hunter0efe6b62015-07-17 19:33:50 +0300710static __u64 pmu_format_max_value(const unsigned long *format)
711{
Kan Liangac0e2cd2016-03-30 12:16:15 -0700712 __u64 w = 0;
713 int fbit;
Adrian Hunter0efe6b62015-07-17 19:33:50 +0300714
Kan Liangac0e2cd2016-03-30 12:16:15 -0700715 for_each_set_bit(fbit, format, PERF_PMU_FORMAT_BITS)
716 w |= (1ULL << fbit);
717
718 return w;
Adrian Hunter0efe6b62015-07-17 19:33:50 +0300719}
720
Jiri Olsacd82a322012-03-15 20:09:17 +0100721/*
Cody P Schafer688d4df2015-01-07 17:13:50 -0800722 * Term is a string term, and might be a param-term. Try to look up it's value
723 * in the remaining terms.
724 * - We have a term like "base-or-format-term=param-term",
725 * - We need to find the value supplied for "param-term" (with param-term named
726 * in a config string) later on in the term list.
727 */
728static int pmu_resolve_param_term(struct parse_events_term *term,
729 struct list_head *head_terms,
730 __u64 *value)
731{
732 struct parse_events_term *t;
733
734 list_for_each_entry(t, head_terms, list) {
735 if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
736 if (!strcmp(t->config, term->config)) {
737 t->used = true;
738 *value = t->val.num;
739 return 0;
740 }
741 }
742 }
743
744 if (verbose)
745 printf("Required parameter '%s' not specified\n", term->config);
746
747 return -1;
748}
749
He Kuangffeb8832015-09-28 03:52:14 +0000750static char *pmu_formats_string(struct list_head *formats)
Jiri Olsae64b0202015-04-22 21:10:21 +0200751{
752 struct perf_pmu_format *format;
Masami Hiramatsu11db4e22016-05-10 14:47:44 +0900753 char *str = NULL;
754 struct strbuf buf = STRBUF_INIT;
Jiri Olsae64b0202015-04-22 21:10:21 +0200755 unsigned i = 0;
756
He Kuangffeb8832015-09-28 03:52:14 +0000757 if (!formats)
Jiri Olsae64b0202015-04-22 21:10:21 +0200758 return NULL;
759
760 /* sysfs exported terms */
He Kuangffeb8832015-09-28 03:52:14 +0000761 list_for_each_entry(format, formats, list)
Masami Hiramatsu11db4e22016-05-10 14:47:44 +0900762 if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
763 goto error;
Jiri Olsae64b0202015-04-22 21:10:21 +0200764
He Kuangffeb8832015-09-28 03:52:14 +0000765 str = strbuf_detach(&buf, NULL);
Masami Hiramatsu11db4e22016-05-10 14:47:44 +0900766error:
He Kuangffeb8832015-09-28 03:52:14 +0000767 strbuf_release(&buf);
Jiri Olsae64b0202015-04-22 21:10:21 +0200768
Jiri Olsae64b0202015-04-22 21:10:21 +0200769 return str;
Jiri Olsae64b0202015-04-22 21:10:21 +0200770}
771
Cody P Schafer688d4df2015-01-07 17:13:50 -0800772/*
Jiri Olsacd82a322012-03-15 20:09:17 +0100773 * Setup one of config[12] attr members based on the
Cody P Schafer88aca8d2014-01-08 08:43:51 -0800774 * user input data - term parameter.
Jiri Olsacd82a322012-03-15 20:09:17 +0100775 */
776static int pmu_config_term(struct list_head *formats,
777 struct perf_event_attr *attr,
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300778 struct parse_events_term *term,
Cody P Schafer688d4df2015-01-07 17:13:50 -0800779 struct list_head *head_terms,
Jiri Olsae64b0202015-04-22 21:10:21 +0200780 bool zero, struct parse_events_error *err)
Jiri Olsacd82a322012-03-15 20:09:17 +0100781{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300782 struct perf_pmu_format *format;
Jiri Olsacd82a322012-03-15 20:09:17 +0100783 __u64 *vp;
Adrian Hunter0efe6b62015-07-17 19:33:50 +0300784 __u64 val, max_val;
Jiri Olsacd82a322012-03-15 20:09:17 +0100785
786 /*
Cody P Schafer688d4df2015-01-07 17:13:50 -0800787 * If this is a parameter we've already used for parameterized-eval,
788 * skip it in normal eval.
789 */
790 if (term->used)
791 return 0;
792
793 /*
Jiri Olsacd82a322012-03-15 20:09:17 +0100794 * Hardcoded terms should be already in, so nothing
795 * to be done for them.
796 */
797 if (parse_events__is_hardcoded_term(term))
798 return 0;
799
Jiri Olsacd82a322012-03-15 20:09:17 +0100800 format = pmu_find_format(formats, term->config);
Cody P Schafer688d4df2015-01-07 17:13:50 -0800801 if (!format) {
802 if (verbose)
803 printf("Invalid event/parameter '%s'\n", term->config);
Jiri Olsae64b0202015-04-22 21:10:21 +0200804 if (err) {
He Kuangffeb8832015-09-28 03:52:14 +0000805 char *pmu_term = pmu_formats_string(formats);
806
Jiri Olsae64b0202015-04-22 21:10:21 +0200807 err->idx = term->err_term;
808 err->str = strdup("unknown term");
He Kuangffeb8832015-09-28 03:52:14 +0000809 err->help = parse_events_formats_error_string(pmu_term);
810 free(pmu_term);
Jiri Olsae64b0202015-04-22 21:10:21 +0200811 }
Jiri Olsacd82a322012-03-15 20:09:17 +0100812 return -EINVAL;
Cody P Schafer688d4df2015-01-07 17:13:50 -0800813 }
Jiri Olsacd82a322012-03-15 20:09:17 +0100814
815 switch (format->value) {
816 case PERF_PMU_FORMAT_VALUE_CONFIG:
817 vp = &attr->config;
818 break;
819 case PERF_PMU_FORMAT_VALUE_CONFIG1:
820 vp = &attr->config1;
821 break;
822 case PERF_PMU_FORMAT_VALUE_CONFIG2:
823 vp = &attr->config2;
824 break;
825 default:
826 return -EINVAL;
827 }
828
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200829 /*
Cody P Schafer688d4df2015-01-07 17:13:50 -0800830 * Either directly use a numeric term, or try to translate string terms
831 * using event parameters.
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200832 */
Cody P Schafer688d4df2015-01-07 17:13:50 -0800833 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM)
834 val = term->val.num;
835 else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
836 if (strcmp(term->val.str, "?")) {
Jiri Olsae64b0202015-04-22 21:10:21 +0200837 if (verbose) {
Cody P Schafer688d4df2015-01-07 17:13:50 -0800838 pr_info("Invalid sysfs entry %s=%s\n",
839 term->config, term->val.str);
Jiri Olsae64b0202015-04-22 21:10:21 +0200840 }
841 if (err) {
842 err->idx = term->err_val;
843 err->str = strdup("expected numeric value");
844 }
Cody P Schafer688d4df2015-01-07 17:13:50 -0800845 return -EINVAL;
846 }
847
848 if (pmu_resolve_param_term(term, head_terms, &val))
849 return -EINVAL;
850 } else
851 return -EINVAL;
852
Adrian Hunter0efe6b62015-07-17 19:33:50 +0300853 max_val = pmu_format_max_value(format->bits);
854 if (val > max_val) {
855 if (err) {
856 err->idx = term->err_val;
857 if (asprintf(&err->str,
858 "value too big for format, maximum is %llu",
859 (unsigned long long)max_val) < 0)
860 err->str = strdup("value too big for format");
861 return -EINVAL;
862 }
863 /*
864 * Assume we don't care if !err, in which case the value will be
865 * silently truncated.
866 */
867 }
868
Cody P Schafer688d4df2015-01-07 17:13:50 -0800869 pmu_format_value(format->bits, val, vp, zero);
Jiri Olsacd82a322012-03-15 20:09:17 +0100870 return 0;
871}
872
Jiri Olsacff7f952012-11-10 01:46:50 +0100873int perf_pmu__config_terms(struct list_head *formats,
874 struct perf_event_attr *attr,
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300875 struct list_head *head_terms,
Jiri Olsae64b0202015-04-22 21:10:21 +0200876 bool zero, struct parse_events_error *err)
Jiri Olsacd82a322012-03-15 20:09:17 +0100877{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -0300878 struct parse_events_term *term;
Jiri Olsacd82a322012-03-15 20:09:17 +0100879
Cody P Schafer688d4df2015-01-07 17:13:50 -0800880 list_for_each_entry(term, head_terms, list) {
Jiri Olsae64b0202015-04-22 21:10:21 +0200881 if (pmu_config_term(formats, attr, term, head_terms,
882 zero, err))
Jiri Olsacd82a322012-03-15 20:09:17 +0100883 return -EINVAL;
Cody P Schafer688d4df2015-01-07 17:13:50 -0800884 }
Jiri Olsacd82a322012-03-15 20:09:17 +0100885
886 return 0;
887}
888
889/*
890 * Configures event's 'attr' parameter based on the:
891 * 1) users input - specified in terms parameter
892 * 2) pmu format definitions - specified by pmu parameter
893 */
894int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
Jiri Olsae64b0202015-04-22 21:10:21 +0200895 struct list_head *head_terms,
896 struct parse_events_error *err)
Jiri Olsacd82a322012-03-15 20:09:17 +0100897{
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300898 bool zero = !!pmu->default_config;
899
Jiri Olsacd82a322012-03-15 20:09:17 +0100900 attr->type = pmu->type;
Jiri Olsae64b0202015-04-22 21:10:21 +0200901 return perf_pmu__config_terms(&pmu->format, attr, head_terms,
902 zero, err);
Jiri Olsacd82a322012-03-15 20:09:17 +0100903}
904
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300905static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
906 struct parse_events_term *term)
Zheng Yana6146d52012-06-15 14:31:41 +0800907{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300908 struct perf_pmu_alias *alias;
Zheng Yana6146d52012-06-15 14:31:41 +0800909 char *name;
910
911 if (parse_events__is_hardcoded_term(term))
912 return NULL;
913
914 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
915 if (term->val.num != 1)
916 return NULL;
917 if (pmu_find_format(&pmu->format, term->config))
918 return NULL;
919 name = term->config;
920 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
921 if (strcasecmp(term->config, "event"))
922 return NULL;
923 name = term->val.str;
924 } else {
925 return NULL;
926 }
927
928 list_for_each_entry(alias, &pmu->aliases, list) {
929 if (!strcasecmp(alias->name, name))
930 return alias;
931 }
932 return NULL;
933}
934
Stephane Eranian410136f2013-11-12 17:58:49 +0100935
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100936static int check_info_data(struct perf_pmu_alias *alias,
937 struct perf_pmu_info *info)
Stephane Eranian410136f2013-11-12 17:58:49 +0100938{
939 /*
940 * Only one term in event definition can
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100941 * define unit, scale and snapshot, fail
942 * if there's more than one.
Stephane Eranian410136f2013-11-12 17:58:49 +0100943 */
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100944 if ((info->unit && alias->unit) ||
945 (info->scale && alias->scale) ||
946 (info->snapshot && alias->snapshot))
Stephane Eranian410136f2013-11-12 17:58:49 +0100947 return -EINVAL;
948
949 if (alias->unit)
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100950 info->unit = alias->unit;
Stephane Eranian410136f2013-11-12 17:58:49 +0100951
952 if (alias->scale)
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100953 info->scale = alias->scale;
954
955 if (alias->snapshot)
956 info->snapshot = alias->snapshot;
Stephane Eranian410136f2013-11-12 17:58:49 +0100957
958 return 0;
959}
960
Zheng Yana6146d52012-06-15 14:31:41 +0800961/*
962 * Find alias in the terms list and replace it with the terms
963 * defined for the alias
964 */
Stephane Eranian410136f2013-11-12 17:58:49 +0100965int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
Matt Fleming46441bd2014-09-24 15:04:06 +0100966 struct perf_pmu_info *info)
Zheng Yana6146d52012-06-15 14:31:41 +0800967{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -0300968 struct parse_events_term *term, *h;
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300969 struct perf_pmu_alias *alias;
Zheng Yana6146d52012-06-15 14:31:41 +0800970 int ret;
971
Matt Fleming044330c2014-11-21 10:31:12 +0100972 info->per_pkg = false;
973
Stephane Eranian8a398892014-01-17 16:34:05 +0100974 /*
975 * Mark unit and scale as not set
976 * (different from default values, see below)
977 */
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100978 info->unit = NULL;
979 info->scale = 0.0;
980 info->snapshot = false;
Stephane Eranian410136f2013-11-12 17:58:49 +0100981
Zheng Yana6146d52012-06-15 14:31:41 +0800982 list_for_each_entry_safe(term, h, head_terms, list) {
983 alias = pmu_find_alias(pmu, term);
984 if (!alias)
985 continue;
986 ret = pmu_alias_terms(alias, &term->list);
987 if (ret)
988 return ret;
Stephane Eranian410136f2013-11-12 17:58:49 +0100989
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100990 ret = check_info_data(alias, info);
Stephane Eranian410136f2013-11-12 17:58:49 +0100991 if (ret)
992 return ret;
993
Matt Fleming044330c2014-11-21 10:31:12 +0100994 if (alias->per_pkg)
995 info->per_pkg = true;
996
Zheng Yana6146d52012-06-15 14:31:41 +0800997 list_del(&term->list);
998 free(term);
999 }
Stephane Eranian8a398892014-01-17 16:34:05 +01001000
1001 /*
1002 * if no unit or scale foundin aliases, then
1003 * set defaults as for evsel
1004 * unit cannot left to NULL
1005 */
Matt Fleming46441bd2014-09-24 15:04:06 +01001006 if (info->unit == NULL)
1007 info->unit = "";
Stephane Eranian8a398892014-01-17 16:34:05 +01001008
Matt Fleming46441bd2014-09-24 15:04:06 +01001009 if (info->scale == 0.0)
1010 info->scale = 1.0;
Stephane Eranian8a398892014-01-17 16:34:05 +01001011
Zheng Yana6146d52012-06-15 14:31:41 +08001012 return 0;
1013}
1014
Jiri Olsacd82a322012-03-15 20:09:17 +01001015int perf_pmu__new_format(struct list_head *list, char *name,
1016 int config, unsigned long *bits)
1017{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -03001018 struct perf_pmu_format *format;
Jiri Olsacd82a322012-03-15 20:09:17 +01001019
1020 format = zalloc(sizeof(*format));
1021 if (!format)
1022 return -ENOMEM;
1023
1024 format->name = strdup(name);
1025 format->value = config;
1026 memcpy(format->bits, bits, sizeof(format->bits));
1027
1028 list_add_tail(&format->list, list);
1029 return 0;
1030}
1031
1032void perf_pmu__set_format(unsigned long *bits, long from, long to)
1033{
1034 long b;
1035
1036 if (!to)
1037 to = from;
1038
Sukadev Bhattiprolu15268132013-01-17 09:11:30 -08001039 memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
Jiri Olsacd82a322012-03-15 20:09:17 +01001040 for (b = from; b <= to; b++)
1041 set_bit(b, bits);
1042}
Andi Kleendc098b32013-04-20 11:02:29 -07001043
Cody P Schaferaaea3612015-01-07 17:13:51 -08001044static int sub_non_neg(int a, int b)
1045{
1046 if (b > a)
1047 return 0;
1048 return a - b;
1049}
1050
Andi Kleendc098b32013-04-20 11:02:29 -07001051static char *format_alias(char *buf, int len, struct perf_pmu *pmu,
1052 struct perf_pmu_alias *alias)
1053{
Cody P Schaferaaea3612015-01-07 17:13:51 -08001054 struct parse_events_term *term;
1055 int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name);
1056
1057 list_for_each_entry(term, &alias->terms, list) {
1058 if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
1059 used += snprintf(buf + used, sub_non_neg(len, used),
1060 ",%s=%s", term->config,
1061 term->val.str);
1062 }
1063
1064 if (sub_non_neg(len, used) > 0) {
1065 buf[used] = '/';
1066 used++;
1067 }
1068 if (sub_non_neg(len, used) > 0) {
1069 buf[used] = '\0';
1070 used++;
1071 } else
1072 buf[len - 1] = '\0';
1073
Andi Kleendc098b32013-04-20 11:02:29 -07001074 return buf;
1075}
1076
1077static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu,
1078 struct perf_pmu_alias *alias)
1079{
1080 snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name);
1081 return buf;
1082}
1083
Andi Kleendd5f1032016-09-15 15:24:50 -07001084struct sevent {
Andi Kleen08e60ed2016-09-15 15:24:43 -07001085 char *name;
1086 char *desc;
Andi Kleendd5f1032016-09-15 15:24:50 -07001087 char *topic;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001088};
1089
Andi Kleendd5f1032016-09-15 15:24:50 -07001090static int cmp_sevent(const void *a, const void *b)
Andi Kleendc098b32013-04-20 11:02:29 -07001091{
Andi Kleendd5f1032016-09-15 15:24:50 -07001092 const struct sevent *as = a;
1093 const struct sevent *bs = b;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001094
1095 /* Put extra events last */
1096 if (!!as->desc != !!bs->desc)
1097 return !!as->desc - !!bs->desc;
Andi Kleendd5f1032016-09-15 15:24:50 -07001098 if (as->topic && bs->topic) {
1099 int n = strcmp(as->topic, bs->topic);
1100
1101 if (n)
1102 return n;
1103 }
Andi Kleen08e60ed2016-09-15 15:24:43 -07001104 return strcmp(as->name, bs->name);
1105}
1106
1107static void wordwrap(char *s, int start, int max, int corr)
1108{
1109 int column = start;
1110 int n;
1111
1112 while (*s) {
1113 int wlen = strcspn(s, " \t");
1114
1115 if (column + wlen >= max && column > start) {
1116 printf("\n%*s", start, "");
1117 column = start + corr;
1118 }
1119 n = printf("%s%.*s", column > start ? " " : "", wlen, s);
1120 if (n <= 0)
1121 break;
1122 s += wlen;
1123 column += n;
1124 while (isspace(*s))
1125 s++;
1126 }
Andi Kleendc098b32013-04-20 11:02:29 -07001127}
1128
Sukadev Bhattiproluc8d68282016-09-15 15:24:48 -07001129void print_pmu_events(const char *event_glob, bool name_only, bool quiet_flag,
1130 bool long_desc)
Andi Kleendc098b32013-04-20 11:02:29 -07001131{
1132 struct perf_pmu *pmu;
1133 struct perf_pmu_alias *alias;
1134 char buf[1024];
1135 int printed = 0;
1136 int len, j;
Andi Kleendd5f1032016-09-15 15:24:50 -07001137 struct sevent *aliases;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001138 int numdesc = 0;
Andi Kleen61eb2eb2016-09-15 15:24:44 -07001139 int columns = pager_get_columns();
Andi Kleendd5f1032016-09-15 15:24:50 -07001140 char *topic = NULL;
Andi Kleendc098b32013-04-20 11:02:29 -07001141
1142 pmu = NULL;
1143 len = 0;
Adrian Hunter42634bc2014-10-23 13:45:10 +03001144 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
Andi Kleendc098b32013-04-20 11:02:29 -07001145 list_for_each_entry(alias, &pmu->aliases, list)
1146 len++;
Adrian Hunter42634bc2014-10-23 13:45:10 +03001147 if (pmu->selectable)
1148 len++;
1149 }
Andi Kleendd5f1032016-09-15 15:24:50 -07001150 aliases = zalloc(sizeof(struct sevent) * len);
Andi Kleendc098b32013-04-20 11:02:29 -07001151 if (!aliases)
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001152 goto out_enomem;
Andi Kleendc098b32013-04-20 11:02:29 -07001153 pmu = NULL;
1154 j = 0;
Adrian Hunter42634bc2014-10-23 13:45:10 +03001155 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
Andi Kleendc098b32013-04-20 11:02:29 -07001156 list_for_each_entry(alias, &pmu->aliases, list) {
Andi Kleen08e60ed2016-09-15 15:24:43 -07001157 char *name = alias->desc ? alias->name :
1158 format_alias(buf, sizeof(buf), pmu, alias);
Andi Kleendc098b32013-04-20 11:02:29 -07001159 bool is_cpu = !strcmp(pmu->name, "cpu");
1160
1161 if (event_glob != NULL &&
Andi Kleen38d14f02016-10-19 10:50:01 -07001162 !(strglobmatch_nocase(name, event_glob) ||
1163 (!is_cpu && strglobmatch_nocase(alias->name,
Andi Kleen67bdc352016-10-19 11:45:23 -07001164 event_glob)) ||
1165 (alias->topic &&
1166 strglobmatch_nocase(alias->topic, event_glob))))
Andi Kleendc098b32013-04-20 11:02:29 -07001167 continue;
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001168
Andi Kleen08e60ed2016-09-15 15:24:43 -07001169 if (is_cpu && !name_only && !alias->desc)
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001170 name = format_alias_or(buf, sizeof(buf), pmu, alias);
1171
Andi Kleen08e60ed2016-09-15 15:24:43 -07001172 aliases[j].name = name;
1173 if (is_cpu && !name_only && !alias->desc)
1174 aliases[j].name = format_alias_or(buf,
1175 sizeof(buf),
1176 pmu, alias);
1177 aliases[j].name = strdup(aliases[j].name);
1178 if (!aliases[j].name)
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001179 goto out_enomem;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001180
Sukadev Bhattiproluc8d68282016-09-15 15:24:48 -07001181 aliases[j].desc = long_desc ? alias->long_desc :
1182 alias->desc;
Andi Kleendd5f1032016-09-15 15:24:50 -07001183 aliases[j].topic = alias->topic;
Andi Kleendc098b32013-04-20 11:02:29 -07001184 j++;
1185 }
Arnaldo Carvalho de Melofa52cea2015-10-02 15:28:16 -03001186 if (pmu->selectable &&
1187 (event_glob == NULL || strglobmatch(pmu->name, event_glob))) {
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001188 char *s;
1189 if (asprintf(&s, "%s//", pmu->name) < 0)
1190 goto out_enomem;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001191 aliases[j].name = s;
Adrian Hunter42634bc2014-10-23 13:45:10 +03001192 j++;
1193 }
1194 }
Andi Kleendc098b32013-04-20 11:02:29 -07001195 len = j;
Andi Kleendd5f1032016-09-15 15:24:50 -07001196 qsort(aliases, len, sizeof(struct sevent), cmp_sevent);
Andi Kleendc098b32013-04-20 11:02:29 -07001197 for (j = 0; j < len; j++) {
1198 if (name_only) {
Andi Kleen08e60ed2016-09-15 15:24:43 -07001199 printf("%s ", aliases[j].name);
Andi Kleendc098b32013-04-20 11:02:29 -07001200 continue;
1201 }
Andi Kleen1c5f01f2016-09-15 15:24:45 -07001202 if (aliases[j].desc && !quiet_flag) {
Andi Kleen08e60ed2016-09-15 15:24:43 -07001203 if (numdesc++ == 0)
1204 printf("\n");
Andi Kleendd5f1032016-09-15 15:24:50 -07001205 if (aliases[j].topic && (!topic ||
1206 strcmp(topic, aliases[j].topic))) {
1207 printf("%s%s:\n", topic ? "\n" : "",
1208 aliases[j].topic);
1209 topic = aliases[j].topic;
1210 }
Andi Kleen08e60ed2016-09-15 15:24:43 -07001211 printf(" %-50s\n", aliases[j].name);
1212 printf("%*s", 8, "[");
1213 wordwrap(aliases[j].desc, 8, columns, 0);
1214 printf("]\n");
1215 } else
1216 printf(" %-50s [Kernel PMU event]\n", aliases[j].name);
Andi Kleendc098b32013-04-20 11:02:29 -07001217 printed++;
1218 }
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03001219 if (printed && pager_in_use())
Andi Kleendc098b32013-04-20 11:02:29 -07001220 printf("\n");
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001221out_free:
1222 for (j = 0; j < len; j++)
Andi Kleen08e60ed2016-09-15 15:24:43 -07001223 zfree(&aliases[j].name);
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001224 zfree(&aliases);
1225 return;
1226
1227out_enomem:
1228 printf("FATAL: not enough memory to print PMU events\n");
1229 if (aliases)
1230 goto out_free;
Andi Kleendc098b32013-04-20 11:02:29 -07001231}
Andi Kleen4cabc3d2013-08-21 16:47:26 -07001232
1233bool pmu_have_event(const char *pname, const char *name)
1234{
1235 struct perf_pmu *pmu;
1236 struct perf_pmu_alias *alias;
1237
1238 pmu = NULL;
1239 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1240 if (strcmp(pname, pmu->name))
1241 continue;
1242 list_for_each_entry(alias, &pmu->aliases, list)
1243 if (!strcmp(alias->name, name))
1244 return true;
1245 }
1246 return false;
1247}
Adrian Hunter7d4bdab2014-07-31 09:00:50 +03001248
1249static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
1250{
1251 struct stat st;
1252 char path[PATH_MAX];
1253 const char *sysfs;
1254
1255 sysfs = sysfs__mountpoint();
1256 if (!sysfs)
1257 return NULL;
1258
1259 snprintf(path, PATH_MAX,
1260 "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name);
1261
1262 if (stat(path, &st) < 0)
1263 return NULL;
1264
1265 return fopen(path, "r");
1266}
1267
1268int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
1269 ...)
1270{
1271 va_list args;
1272 FILE *file;
1273 int ret = EOF;
1274
1275 va_start(args, fmt);
1276 file = perf_pmu__open_file(pmu, name);
1277 if (file) {
1278 ret = vfscanf(file, fmt, args);
1279 fclose(file);
1280 }
1281 va_end(args);
1282 return ret;
1283}