blob: 8e9d00fd418ef26f8e380ebab2612e6745a7b393 [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
Andi Kleen15b22ed2017-01-27 18:03:38 -0800593 /*
594 * Check the type first to avoid unnecessary work.
595 */
596 if (pmu_type(name, &type))
597 return NULL;
598
Jiri Olsa3fded962012-10-10 14:53:16 +0200599 if (pmu_aliases(name, &aliases))
600 return NULL;
601
Andi Kleenfedb2b52017-01-27 18:03:37 -0800602 pmu_add_cpu_aliases(&aliases, name);
Jiri Olsacd82a322012-03-15 20:09:17 +0100603 pmu = zalloc(sizeof(*pmu));
604 if (!pmu)
605 return NULL;
606
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800607 pmu->cpus = pmu_cpumask(name);
608
Jiri Olsacd82a322012-03-15 20:09:17 +0100609 INIT_LIST_HEAD(&pmu->format);
Zheng Yana6146d52012-06-15 14:31:41 +0800610 INIT_LIST_HEAD(&pmu->aliases);
Jiri Olsacd82a322012-03-15 20:09:17 +0100611 list_splice(&format, &pmu->format);
Zheng Yana6146d52012-06-15 14:31:41 +0800612 list_splice(&aliases, &pmu->aliases);
Jiri Olsacd82a322012-03-15 20:09:17 +0100613 pmu->name = strdup(name);
614 pmu->type = type;
Robert Richter9bc8f9f2012-06-14 22:38:37 +0200615 list_add_tail(&pmu->list, &pmus);
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300616
617 pmu->default_config = perf_pmu__get_default_config(pmu);
618
Jiri Olsacd82a322012-03-15 20:09:17 +0100619 return pmu;
620}
621
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300622static struct perf_pmu *pmu_find(const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +0100623{
624 struct perf_pmu *pmu;
625
626 list_for_each_entry(pmu, &pmus, list)
627 if (!strcmp(pmu->name, name))
628 return pmu;
629
630 return NULL;
631}
632
Robert Richter50a96672012-08-16 21:10:24 +0200633struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
634{
635 /*
636 * pmu iterator: If pmu is NULL, we start at the begin,
637 * otherwise return the next pmu. Returns NULL on end.
638 */
639 if (!pmu) {
640 pmu_read_sysfs();
641 pmu = list_prepare_entry(pmu, &pmus, list);
642 }
643 list_for_each_entry_continue(pmu, &pmus, list)
644 return pmu;
645 return NULL;
646}
647
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300648struct perf_pmu *perf_pmu__find(const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +0100649{
650 struct perf_pmu *pmu;
651
652 /*
653 * Once PMU is loaded it stays in the list,
654 * so we keep us from multiple reading/parsing
655 * the pmu format definitions.
656 */
657 pmu = pmu_find(name);
658 if (pmu)
659 return pmu;
660
661 return pmu_lookup(name);
662}
663
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300664static struct perf_pmu_format *
Adrian Hunter09ff6072015-07-17 19:33:49 +0300665pmu_find_format(struct list_head *formats, const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +0100666{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300667 struct perf_pmu_format *format;
Jiri Olsacd82a322012-03-15 20:09:17 +0100668
669 list_for_each_entry(format, formats, list)
670 if (!strcmp(format->name, name))
671 return format;
672
673 return NULL;
674}
675
Adrian Hunter09ff6072015-07-17 19:33:49 +0300676__u64 perf_pmu__format_bits(struct list_head *formats, const char *name)
677{
678 struct perf_pmu_format *format = pmu_find_format(formats, name);
679 __u64 bits = 0;
680 int fbit;
681
682 if (!format)
683 return 0;
684
685 for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
686 bits |= 1ULL << fbit;
687
688 return bits;
689}
690
Jiri Olsacd82a322012-03-15 20:09:17 +0100691/*
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300692 * Sets value based on the format definition (format parameter)
Jiri Olsacd82a322012-03-15 20:09:17 +0100693 * and unformated value (value parameter).
Jiri Olsacd82a322012-03-15 20:09:17 +0100694 */
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300695static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
696 bool zero)
Jiri Olsacd82a322012-03-15 20:09:17 +0100697{
698 unsigned long fbit, vbit;
Jiri Olsacd82a322012-03-15 20:09:17 +0100699
700 for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
701
702 if (!test_bit(fbit, format))
703 continue;
704
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300705 if (value & (1llu << vbit++))
706 *v |= (1llu << fbit);
707 else if (zero)
708 *v &= ~(1llu << fbit);
Jiri Olsacd82a322012-03-15 20:09:17 +0100709 }
Jiri Olsacd82a322012-03-15 20:09:17 +0100710}
711
Adrian Hunter0efe6b62015-07-17 19:33:50 +0300712static __u64 pmu_format_max_value(const unsigned long *format)
713{
Kan Liangac0e2cd2016-03-30 12:16:15 -0700714 __u64 w = 0;
715 int fbit;
Adrian Hunter0efe6b62015-07-17 19:33:50 +0300716
Kan Liangac0e2cd2016-03-30 12:16:15 -0700717 for_each_set_bit(fbit, format, PERF_PMU_FORMAT_BITS)
718 w |= (1ULL << fbit);
719
720 return w;
Adrian Hunter0efe6b62015-07-17 19:33:50 +0300721}
722
Jiri Olsacd82a322012-03-15 20:09:17 +0100723/*
Cody P Schafer688d4df2015-01-07 17:13:50 -0800724 * Term is a string term, and might be a param-term. Try to look up it's value
725 * in the remaining terms.
726 * - We have a term like "base-or-format-term=param-term",
727 * - We need to find the value supplied for "param-term" (with param-term named
728 * in a config string) later on in the term list.
729 */
730static int pmu_resolve_param_term(struct parse_events_term *term,
731 struct list_head *head_terms,
732 __u64 *value)
733{
734 struct parse_events_term *t;
735
736 list_for_each_entry(t, head_terms, list) {
737 if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
738 if (!strcmp(t->config, term->config)) {
739 t->used = true;
740 *value = t->val.num;
741 return 0;
742 }
743 }
744 }
745
746 if (verbose)
747 printf("Required parameter '%s' not specified\n", term->config);
748
749 return -1;
750}
751
He Kuangffeb8832015-09-28 03:52:14 +0000752static char *pmu_formats_string(struct list_head *formats)
Jiri Olsae64b0202015-04-22 21:10:21 +0200753{
754 struct perf_pmu_format *format;
Masami Hiramatsu11db4e22016-05-10 14:47:44 +0900755 char *str = NULL;
756 struct strbuf buf = STRBUF_INIT;
Jiri Olsae64b0202015-04-22 21:10:21 +0200757 unsigned i = 0;
758
He Kuangffeb8832015-09-28 03:52:14 +0000759 if (!formats)
Jiri Olsae64b0202015-04-22 21:10:21 +0200760 return NULL;
761
762 /* sysfs exported terms */
He Kuangffeb8832015-09-28 03:52:14 +0000763 list_for_each_entry(format, formats, list)
Masami Hiramatsu11db4e22016-05-10 14:47:44 +0900764 if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
765 goto error;
Jiri Olsae64b0202015-04-22 21:10:21 +0200766
He Kuangffeb8832015-09-28 03:52:14 +0000767 str = strbuf_detach(&buf, NULL);
Masami Hiramatsu11db4e22016-05-10 14:47:44 +0900768error:
He Kuangffeb8832015-09-28 03:52:14 +0000769 strbuf_release(&buf);
Jiri Olsae64b0202015-04-22 21:10:21 +0200770
Jiri Olsae64b0202015-04-22 21:10:21 +0200771 return str;
Jiri Olsae64b0202015-04-22 21:10:21 +0200772}
773
Cody P Schafer688d4df2015-01-07 17:13:50 -0800774/*
Jiri Olsacd82a322012-03-15 20:09:17 +0100775 * Setup one of config[12] attr members based on the
Cody P Schafer88aca8d2014-01-08 08:43:51 -0800776 * user input data - term parameter.
Jiri Olsacd82a322012-03-15 20:09:17 +0100777 */
778static int pmu_config_term(struct list_head *formats,
779 struct perf_event_attr *attr,
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300780 struct parse_events_term *term,
Cody P Schafer688d4df2015-01-07 17:13:50 -0800781 struct list_head *head_terms,
Jiri Olsae64b0202015-04-22 21:10:21 +0200782 bool zero, struct parse_events_error *err)
Jiri Olsacd82a322012-03-15 20:09:17 +0100783{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300784 struct perf_pmu_format *format;
Jiri Olsacd82a322012-03-15 20:09:17 +0100785 __u64 *vp;
Adrian Hunter0efe6b62015-07-17 19:33:50 +0300786 __u64 val, max_val;
Jiri Olsacd82a322012-03-15 20:09:17 +0100787
788 /*
Cody P Schafer688d4df2015-01-07 17:13:50 -0800789 * If this is a parameter we've already used for parameterized-eval,
790 * skip it in normal eval.
791 */
792 if (term->used)
793 return 0;
794
795 /*
Jiri Olsacd82a322012-03-15 20:09:17 +0100796 * Hardcoded terms should be already in, so nothing
797 * to be done for them.
798 */
799 if (parse_events__is_hardcoded_term(term))
800 return 0;
801
Jiri Olsacd82a322012-03-15 20:09:17 +0100802 format = pmu_find_format(formats, term->config);
Cody P Schafer688d4df2015-01-07 17:13:50 -0800803 if (!format) {
804 if (verbose)
805 printf("Invalid event/parameter '%s'\n", term->config);
Jiri Olsae64b0202015-04-22 21:10:21 +0200806 if (err) {
He Kuangffeb8832015-09-28 03:52:14 +0000807 char *pmu_term = pmu_formats_string(formats);
808
Jiri Olsae64b0202015-04-22 21:10:21 +0200809 err->idx = term->err_term;
810 err->str = strdup("unknown term");
He Kuangffeb8832015-09-28 03:52:14 +0000811 err->help = parse_events_formats_error_string(pmu_term);
812 free(pmu_term);
Jiri Olsae64b0202015-04-22 21:10:21 +0200813 }
Jiri Olsacd82a322012-03-15 20:09:17 +0100814 return -EINVAL;
Cody P Schafer688d4df2015-01-07 17:13:50 -0800815 }
Jiri Olsacd82a322012-03-15 20:09:17 +0100816
817 switch (format->value) {
818 case PERF_PMU_FORMAT_VALUE_CONFIG:
819 vp = &attr->config;
820 break;
821 case PERF_PMU_FORMAT_VALUE_CONFIG1:
822 vp = &attr->config1;
823 break;
824 case PERF_PMU_FORMAT_VALUE_CONFIG2:
825 vp = &attr->config2;
826 break;
827 default:
828 return -EINVAL;
829 }
830
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200831 /*
Cody P Schafer688d4df2015-01-07 17:13:50 -0800832 * Either directly use a numeric term, or try to translate string terms
833 * using event parameters.
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200834 */
Cody P Schafer688d4df2015-01-07 17:13:50 -0800835 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM)
836 val = term->val.num;
837 else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
838 if (strcmp(term->val.str, "?")) {
Jiri Olsae64b0202015-04-22 21:10:21 +0200839 if (verbose) {
Cody P Schafer688d4df2015-01-07 17:13:50 -0800840 pr_info("Invalid sysfs entry %s=%s\n",
841 term->config, term->val.str);
Jiri Olsae64b0202015-04-22 21:10:21 +0200842 }
843 if (err) {
844 err->idx = term->err_val;
845 err->str = strdup("expected numeric value");
846 }
Cody P Schafer688d4df2015-01-07 17:13:50 -0800847 return -EINVAL;
848 }
849
850 if (pmu_resolve_param_term(term, head_terms, &val))
851 return -EINVAL;
852 } else
853 return -EINVAL;
854
Adrian Hunter0efe6b62015-07-17 19:33:50 +0300855 max_val = pmu_format_max_value(format->bits);
856 if (val > max_val) {
857 if (err) {
858 err->idx = term->err_val;
859 if (asprintf(&err->str,
860 "value too big for format, maximum is %llu",
861 (unsigned long long)max_val) < 0)
862 err->str = strdup("value too big for format");
863 return -EINVAL;
864 }
865 /*
866 * Assume we don't care if !err, in which case the value will be
867 * silently truncated.
868 */
869 }
870
Cody P Schafer688d4df2015-01-07 17:13:50 -0800871 pmu_format_value(format->bits, val, vp, zero);
Jiri Olsacd82a322012-03-15 20:09:17 +0100872 return 0;
873}
874
Jiri Olsacff7f952012-11-10 01:46:50 +0100875int perf_pmu__config_terms(struct list_head *formats,
876 struct perf_event_attr *attr,
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300877 struct list_head *head_terms,
Jiri Olsae64b0202015-04-22 21:10:21 +0200878 bool zero, struct parse_events_error *err)
Jiri Olsacd82a322012-03-15 20:09:17 +0100879{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -0300880 struct parse_events_term *term;
Jiri Olsacd82a322012-03-15 20:09:17 +0100881
Cody P Schafer688d4df2015-01-07 17:13:50 -0800882 list_for_each_entry(term, head_terms, list) {
Jiri Olsae64b0202015-04-22 21:10:21 +0200883 if (pmu_config_term(formats, attr, term, head_terms,
884 zero, err))
Jiri Olsacd82a322012-03-15 20:09:17 +0100885 return -EINVAL;
Cody P Schafer688d4df2015-01-07 17:13:50 -0800886 }
Jiri Olsacd82a322012-03-15 20:09:17 +0100887
888 return 0;
889}
890
891/*
892 * Configures event's 'attr' parameter based on the:
893 * 1) users input - specified in terms parameter
894 * 2) pmu format definitions - specified by pmu parameter
895 */
896int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
Jiri Olsae64b0202015-04-22 21:10:21 +0200897 struct list_head *head_terms,
898 struct parse_events_error *err)
Jiri Olsacd82a322012-03-15 20:09:17 +0100899{
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300900 bool zero = !!pmu->default_config;
901
Jiri Olsacd82a322012-03-15 20:09:17 +0100902 attr->type = pmu->type;
Jiri Olsae64b0202015-04-22 21:10:21 +0200903 return perf_pmu__config_terms(&pmu->format, attr, head_terms,
904 zero, err);
Jiri Olsacd82a322012-03-15 20:09:17 +0100905}
906
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300907static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
908 struct parse_events_term *term)
Zheng Yana6146d52012-06-15 14:31:41 +0800909{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300910 struct perf_pmu_alias *alias;
Zheng Yana6146d52012-06-15 14:31:41 +0800911 char *name;
912
913 if (parse_events__is_hardcoded_term(term))
914 return NULL;
915
916 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
917 if (term->val.num != 1)
918 return NULL;
919 if (pmu_find_format(&pmu->format, term->config))
920 return NULL;
921 name = term->config;
922 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
923 if (strcasecmp(term->config, "event"))
924 return NULL;
925 name = term->val.str;
926 } else {
927 return NULL;
928 }
929
930 list_for_each_entry(alias, &pmu->aliases, list) {
931 if (!strcasecmp(alias->name, name))
932 return alias;
933 }
934 return NULL;
935}
936
Stephane Eranian410136f2013-11-12 17:58:49 +0100937
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100938static int check_info_data(struct perf_pmu_alias *alias,
939 struct perf_pmu_info *info)
Stephane Eranian410136f2013-11-12 17:58:49 +0100940{
941 /*
942 * Only one term in event definition can
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100943 * define unit, scale and snapshot, fail
944 * if there's more than one.
Stephane Eranian410136f2013-11-12 17:58:49 +0100945 */
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100946 if ((info->unit && alias->unit) ||
947 (info->scale && alias->scale) ||
948 (info->snapshot && alias->snapshot))
Stephane Eranian410136f2013-11-12 17:58:49 +0100949 return -EINVAL;
950
951 if (alias->unit)
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100952 info->unit = alias->unit;
Stephane Eranian410136f2013-11-12 17:58:49 +0100953
954 if (alias->scale)
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100955 info->scale = alias->scale;
956
957 if (alias->snapshot)
958 info->snapshot = alias->snapshot;
Stephane Eranian410136f2013-11-12 17:58:49 +0100959
960 return 0;
961}
962
Zheng Yana6146d52012-06-15 14:31:41 +0800963/*
964 * Find alias in the terms list and replace it with the terms
965 * defined for the alias
966 */
Stephane Eranian410136f2013-11-12 17:58:49 +0100967int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
Matt Fleming46441bd2014-09-24 15:04:06 +0100968 struct perf_pmu_info *info)
Zheng Yana6146d52012-06-15 14:31:41 +0800969{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -0300970 struct parse_events_term *term, *h;
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300971 struct perf_pmu_alias *alias;
Zheng Yana6146d52012-06-15 14:31:41 +0800972 int ret;
973
Matt Fleming044330c2014-11-21 10:31:12 +0100974 info->per_pkg = false;
975
Stephane Eranian8a398892014-01-17 16:34:05 +0100976 /*
977 * Mark unit and scale as not set
978 * (different from default values, see below)
979 */
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100980 info->unit = NULL;
981 info->scale = 0.0;
982 info->snapshot = false;
Stephane Eranian410136f2013-11-12 17:58:49 +0100983
Zheng Yana6146d52012-06-15 14:31:41 +0800984 list_for_each_entry_safe(term, h, head_terms, list) {
985 alias = pmu_find_alias(pmu, term);
986 if (!alias)
987 continue;
988 ret = pmu_alias_terms(alias, &term->list);
989 if (ret)
990 return ret;
Stephane Eranian410136f2013-11-12 17:58:49 +0100991
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100992 ret = check_info_data(alias, info);
Stephane Eranian410136f2013-11-12 17:58:49 +0100993 if (ret)
994 return ret;
995
Matt Fleming044330c2014-11-21 10:31:12 +0100996 if (alias->per_pkg)
997 info->per_pkg = true;
998
Zheng Yana6146d52012-06-15 14:31:41 +0800999 list_del(&term->list);
1000 free(term);
1001 }
Stephane Eranian8a398892014-01-17 16:34:05 +01001002
1003 /*
1004 * if no unit or scale foundin aliases, then
1005 * set defaults as for evsel
1006 * unit cannot left to NULL
1007 */
Matt Fleming46441bd2014-09-24 15:04:06 +01001008 if (info->unit == NULL)
1009 info->unit = "";
Stephane Eranian8a398892014-01-17 16:34:05 +01001010
Matt Fleming46441bd2014-09-24 15:04:06 +01001011 if (info->scale == 0.0)
1012 info->scale = 1.0;
Stephane Eranian8a398892014-01-17 16:34:05 +01001013
Zheng Yana6146d52012-06-15 14:31:41 +08001014 return 0;
1015}
1016
Jiri Olsacd82a322012-03-15 20:09:17 +01001017int perf_pmu__new_format(struct list_head *list, char *name,
1018 int config, unsigned long *bits)
1019{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -03001020 struct perf_pmu_format *format;
Jiri Olsacd82a322012-03-15 20:09:17 +01001021
1022 format = zalloc(sizeof(*format));
1023 if (!format)
1024 return -ENOMEM;
1025
1026 format->name = strdup(name);
1027 format->value = config;
1028 memcpy(format->bits, bits, sizeof(format->bits));
1029
1030 list_add_tail(&format->list, list);
1031 return 0;
1032}
1033
1034void perf_pmu__set_format(unsigned long *bits, long from, long to)
1035{
1036 long b;
1037
1038 if (!to)
1039 to = from;
1040
Sukadev Bhattiprolu15268132013-01-17 09:11:30 -08001041 memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
Jiri Olsacd82a322012-03-15 20:09:17 +01001042 for (b = from; b <= to; b++)
1043 set_bit(b, bits);
1044}
Andi Kleendc098b32013-04-20 11:02:29 -07001045
Cody P Schaferaaea3612015-01-07 17:13:51 -08001046static int sub_non_neg(int a, int b)
1047{
1048 if (b > a)
1049 return 0;
1050 return a - b;
1051}
1052
Andi Kleendc098b32013-04-20 11:02:29 -07001053static char *format_alias(char *buf, int len, struct perf_pmu *pmu,
1054 struct perf_pmu_alias *alias)
1055{
Cody P Schaferaaea3612015-01-07 17:13:51 -08001056 struct parse_events_term *term;
1057 int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name);
1058
1059 list_for_each_entry(term, &alias->terms, list) {
1060 if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
1061 used += snprintf(buf + used, sub_non_neg(len, used),
1062 ",%s=%s", term->config,
1063 term->val.str);
1064 }
1065
1066 if (sub_non_neg(len, used) > 0) {
1067 buf[used] = '/';
1068 used++;
1069 }
1070 if (sub_non_neg(len, used) > 0) {
1071 buf[used] = '\0';
1072 used++;
1073 } else
1074 buf[len - 1] = '\0';
1075
Andi Kleendc098b32013-04-20 11:02:29 -07001076 return buf;
1077}
1078
1079static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu,
1080 struct perf_pmu_alias *alias)
1081{
1082 snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name);
1083 return buf;
1084}
1085
Andi Kleendd5f1032016-09-15 15:24:50 -07001086struct sevent {
Andi Kleen08e60ed2016-09-15 15:24:43 -07001087 char *name;
1088 char *desc;
Andi Kleendd5f1032016-09-15 15:24:50 -07001089 char *topic;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001090};
1091
Andi Kleendd5f1032016-09-15 15:24:50 -07001092static int cmp_sevent(const void *a, const void *b)
Andi Kleendc098b32013-04-20 11:02:29 -07001093{
Andi Kleendd5f1032016-09-15 15:24:50 -07001094 const struct sevent *as = a;
1095 const struct sevent *bs = b;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001096
1097 /* Put extra events last */
1098 if (!!as->desc != !!bs->desc)
1099 return !!as->desc - !!bs->desc;
Andi Kleendd5f1032016-09-15 15:24:50 -07001100 if (as->topic && bs->topic) {
1101 int n = strcmp(as->topic, bs->topic);
1102
1103 if (n)
1104 return n;
1105 }
Andi Kleen08e60ed2016-09-15 15:24:43 -07001106 return strcmp(as->name, bs->name);
1107}
1108
1109static void wordwrap(char *s, int start, int max, int corr)
1110{
1111 int column = start;
1112 int n;
1113
1114 while (*s) {
1115 int wlen = strcspn(s, " \t");
1116
1117 if (column + wlen >= max && column > start) {
1118 printf("\n%*s", start, "");
1119 column = start + corr;
1120 }
1121 n = printf("%s%.*s", column > start ? " " : "", wlen, s);
1122 if (n <= 0)
1123 break;
1124 s += wlen;
1125 column += n;
1126 while (isspace(*s))
1127 s++;
1128 }
Andi Kleendc098b32013-04-20 11:02:29 -07001129}
1130
Sukadev Bhattiproluc8d68282016-09-15 15:24:48 -07001131void print_pmu_events(const char *event_glob, bool name_only, bool quiet_flag,
1132 bool long_desc)
Andi Kleendc098b32013-04-20 11:02:29 -07001133{
1134 struct perf_pmu *pmu;
1135 struct perf_pmu_alias *alias;
1136 char buf[1024];
1137 int printed = 0;
1138 int len, j;
Andi Kleendd5f1032016-09-15 15:24:50 -07001139 struct sevent *aliases;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001140 int numdesc = 0;
Andi Kleen61eb2eb2016-09-15 15:24:44 -07001141 int columns = pager_get_columns();
Andi Kleendd5f1032016-09-15 15:24:50 -07001142 char *topic = NULL;
Andi Kleendc098b32013-04-20 11:02:29 -07001143
1144 pmu = NULL;
1145 len = 0;
Adrian Hunter42634bc2014-10-23 13:45:10 +03001146 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
Andi Kleendc098b32013-04-20 11:02:29 -07001147 list_for_each_entry(alias, &pmu->aliases, list)
1148 len++;
Adrian Hunter42634bc2014-10-23 13:45:10 +03001149 if (pmu->selectable)
1150 len++;
1151 }
Andi Kleendd5f1032016-09-15 15:24:50 -07001152 aliases = zalloc(sizeof(struct sevent) * len);
Andi Kleendc098b32013-04-20 11:02:29 -07001153 if (!aliases)
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001154 goto out_enomem;
Andi Kleendc098b32013-04-20 11:02:29 -07001155 pmu = NULL;
1156 j = 0;
Adrian Hunter42634bc2014-10-23 13:45:10 +03001157 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
Andi Kleendc098b32013-04-20 11:02:29 -07001158 list_for_each_entry(alias, &pmu->aliases, list) {
Andi Kleen08e60ed2016-09-15 15:24:43 -07001159 char *name = alias->desc ? alias->name :
1160 format_alias(buf, sizeof(buf), pmu, alias);
Andi Kleendc098b32013-04-20 11:02:29 -07001161 bool is_cpu = !strcmp(pmu->name, "cpu");
1162
1163 if (event_glob != NULL &&
Andi Kleen38d14f02016-10-19 10:50:01 -07001164 !(strglobmatch_nocase(name, event_glob) ||
1165 (!is_cpu && strglobmatch_nocase(alias->name,
Andi Kleen67bdc352016-10-19 11:45:23 -07001166 event_glob)) ||
1167 (alias->topic &&
1168 strglobmatch_nocase(alias->topic, event_glob))))
Andi Kleendc098b32013-04-20 11:02:29 -07001169 continue;
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001170
Andi Kleen08e60ed2016-09-15 15:24:43 -07001171 if (is_cpu && !name_only && !alias->desc)
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001172 name = format_alias_or(buf, sizeof(buf), pmu, alias);
1173
Andi Kleen08e60ed2016-09-15 15:24:43 -07001174 aliases[j].name = name;
1175 if (is_cpu && !name_only && !alias->desc)
1176 aliases[j].name = format_alias_or(buf,
1177 sizeof(buf),
1178 pmu, alias);
1179 aliases[j].name = strdup(aliases[j].name);
1180 if (!aliases[j].name)
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001181 goto out_enomem;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001182
Sukadev Bhattiproluc8d68282016-09-15 15:24:48 -07001183 aliases[j].desc = long_desc ? alias->long_desc :
1184 alias->desc;
Andi Kleendd5f1032016-09-15 15:24:50 -07001185 aliases[j].topic = alias->topic;
Andi Kleendc098b32013-04-20 11:02:29 -07001186 j++;
1187 }
Arnaldo Carvalho de Melofa52cea2015-10-02 15:28:16 -03001188 if (pmu->selectable &&
1189 (event_glob == NULL || strglobmatch(pmu->name, event_glob))) {
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001190 char *s;
1191 if (asprintf(&s, "%s//", pmu->name) < 0)
1192 goto out_enomem;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001193 aliases[j].name = s;
Adrian Hunter42634bc2014-10-23 13:45:10 +03001194 j++;
1195 }
1196 }
Andi Kleendc098b32013-04-20 11:02:29 -07001197 len = j;
Andi Kleendd5f1032016-09-15 15:24:50 -07001198 qsort(aliases, len, sizeof(struct sevent), cmp_sevent);
Andi Kleendc098b32013-04-20 11:02:29 -07001199 for (j = 0; j < len; j++) {
Andi Kleen15b22ed2017-01-27 18:03:38 -08001200 /* Skip duplicates */
1201 if (j > 0 && !strcmp(aliases[j].name, aliases[j - 1].name))
1202 continue;
Andi Kleendc098b32013-04-20 11:02:29 -07001203 if (name_only) {
Andi Kleen08e60ed2016-09-15 15:24:43 -07001204 printf("%s ", aliases[j].name);
Andi Kleendc098b32013-04-20 11:02:29 -07001205 continue;
1206 }
Andi Kleen1c5f01f2016-09-15 15:24:45 -07001207 if (aliases[j].desc && !quiet_flag) {
Andi Kleen08e60ed2016-09-15 15:24:43 -07001208 if (numdesc++ == 0)
1209 printf("\n");
Andi Kleendd5f1032016-09-15 15:24:50 -07001210 if (aliases[j].topic && (!topic ||
1211 strcmp(topic, aliases[j].topic))) {
1212 printf("%s%s:\n", topic ? "\n" : "",
1213 aliases[j].topic);
1214 topic = aliases[j].topic;
1215 }
Andi Kleen08e60ed2016-09-15 15:24:43 -07001216 printf(" %-50s\n", aliases[j].name);
1217 printf("%*s", 8, "[");
1218 wordwrap(aliases[j].desc, 8, columns, 0);
1219 printf("]\n");
1220 } else
1221 printf(" %-50s [Kernel PMU event]\n", aliases[j].name);
Andi Kleendc098b32013-04-20 11:02:29 -07001222 printed++;
1223 }
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03001224 if (printed && pager_in_use())
Andi Kleendc098b32013-04-20 11:02:29 -07001225 printf("\n");
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001226out_free:
1227 for (j = 0; j < len; j++)
Andi Kleen08e60ed2016-09-15 15:24:43 -07001228 zfree(&aliases[j].name);
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001229 zfree(&aliases);
1230 return;
1231
1232out_enomem:
1233 printf("FATAL: not enough memory to print PMU events\n");
1234 if (aliases)
1235 goto out_free;
Andi Kleendc098b32013-04-20 11:02:29 -07001236}
Andi Kleen4cabc3d2013-08-21 16:47:26 -07001237
1238bool pmu_have_event(const char *pname, const char *name)
1239{
1240 struct perf_pmu *pmu;
1241 struct perf_pmu_alias *alias;
1242
1243 pmu = NULL;
1244 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1245 if (strcmp(pname, pmu->name))
1246 continue;
1247 list_for_each_entry(alias, &pmu->aliases, list)
1248 if (!strcmp(alias->name, name))
1249 return true;
1250 }
1251 return false;
1252}
Adrian Hunter7d4bdab2014-07-31 09:00:50 +03001253
1254static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
1255{
1256 struct stat st;
1257 char path[PATH_MAX];
1258 const char *sysfs;
1259
1260 sysfs = sysfs__mountpoint();
1261 if (!sysfs)
1262 return NULL;
1263
1264 snprintf(path, PATH_MAX,
1265 "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name);
1266
1267 if (stat(path, &st) < 0)
1268 return NULL;
1269
1270 return fopen(path, "r");
1271}
1272
1273int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
1274 ...)
1275{
1276 va_list args;
1277 FILE *file;
1278 int ret = EOF;
1279
1280 va_start(args, fmt);
1281 file = perf_pmu__open_file(pmu, name);
1282 if (file) {
1283 ret = vfscanf(file, fmt, args);
1284 fclose(file);
1285 }
1286 va_end(args);
1287 return ret;
1288}