blob: e243ad962a4d8876aecaafccf18858df2e52118f [file] [log] [blame]
Jiri Olsacd82a322012-03-15 20:09:17 +01001#include <linux/list.h>
2#include <sys/types.h>
Jiri Olsacd82a322012-03-15 20:09:17 +01003#include <unistd.h>
4#include <stdio.h>
Adrian Hunterdc0a6202014-07-31 09:00:49 +03005#include <stdbool.h>
Adrian Hunter7d4bdab2014-07-31 09:00:50 +03006#include <stdarg.h>
Jiri Olsacd82a322012-03-15 20:09:17 +01007#include <dirent.h>
Borislav Petkovcd0cfad2013-12-09 17:14:24 +01008#include <api/fs/fs.h>
Stephane Eranian410136f2013-11-12 17:58:49 +01009#include <locale.h>
Jiri Olsacd82a322012-03-15 20:09:17 +010010#include "util.h"
11#include "pmu.h"
12#include "parse-events.h"
Yan, Zheng7ae92e72012-09-10 15:53:50 +080013#include "cpumap.h"
Jiri Olsacd82a322012-03-15 20:09:17 +010014
Arnaldo Carvalho de Meloab1bf6532013-01-18 17:05:09 -030015struct perf_pmu_format {
16 char *name;
17 int value;
18 DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
19 struct list_head list;
20};
21
Robert Richter50a96672012-08-16 21:10:24 +020022#define EVENT_SOURCE_DEVICE_PATH "/bus/event_source/devices/"
23
Jiri Olsacd82a322012-03-15 20:09:17 +010024int perf_pmu_parse(struct list_head *list, char *name);
25extern FILE *perf_pmu_in;
26
27static LIST_HEAD(pmus);
28
29/*
30 * Parse & process all the sysfs attributes located under
31 * the directory specified in 'dir' parameter.
32 */
Jiri Olsacff7f952012-11-10 01:46:50 +010033int perf_pmu__format_parse(char *dir, struct list_head *head)
Jiri Olsacd82a322012-03-15 20:09:17 +010034{
35 struct dirent *evt_ent;
36 DIR *format_dir;
37 int ret = 0;
38
39 format_dir = opendir(dir);
40 if (!format_dir)
41 return -EINVAL;
42
43 while (!ret && (evt_ent = readdir(format_dir))) {
44 char path[PATH_MAX];
45 char *name = evt_ent->d_name;
46 FILE *file;
47
48 if (!strcmp(name, ".") || !strcmp(name, ".."))
49 continue;
50
51 snprintf(path, PATH_MAX, "%s/%s", dir, name);
52
53 ret = -EINVAL;
54 file = fopen(path, "r");
55 if (!file)
56 break;
57
58 perf_pmu_in = file;
59 ret = perf_pmu_parse(head, name);
60 fclose(file);
61 }
62
63 closedir(format_dir);
64 return ret;
65}
66
67/*
68 * Reading/parsing the default pmu format definition, which should be
69 * located at:
70 * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
71 */
Adrian Hunterb6b96fb2013-07-04 16:20:25 +030072static int pmu_format(const char *name, struct list_head *format)
Jiri Olsacd82a322012-03-15 20:09:17 +010073{
74 struct stat st;
75 char path[PATH_MAX];
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -030076 const char *sysfs = sysfs__mountpoint();
Jiri Olsacd82a322012-03-15 20:09:17 +010077
Jiri Olsacd82a322012-03-15 20:09:17 +010078 if (!sysfs)
79 return -1;
80
81 snprintf(path, PATH_MAX,
Robert Richter50a96672012-08-16 21:10:24 +020082 "%s" EVENT_SOURCE_DEVICE_PATH "%s/format", sysfs, name);
Jiri Olsacd82a322012-03-15 20:09:17 +010083
84 if (stat(path, &st) < 0)
Robert Richter9bc8f9f2012-06-14 22:38:37 +020085 return 0; /* no error if format does not exist */
Jiri Olsacd82a322012-03-15 20:09:17 +010086
Jiri Olsacff7f952012-11-10 01:46:50 +010087 if (perf_pmu__format_parse(path, format))
Jiri Olsacd82a322012-03-15 20:09:17 +010088 return -1;
89
90 return 0;
91}
92
Stephane Eranian410136f2013-11-12 17:58:49 +010093static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *name)
94{
95 struct stat st;
96 ssize_t sret;
97 char scale[128];
98 int fd, ret = -1;
99 char path[PATH_MAX];
Stephane Eranian8a398892014-01-17 16:34:05 +0100100 const char *lc;
Stephane Eranian410136f2013-11-12 17:58:49 +0100101
102 snprintf(path, PATH_MAX, "%s/%s.scale", dir, name);
103
104 fd = open(path, O_RDONLY);
105 if (fd == -1)
106 return -1;
107
108 if (fstat(fd, &st) < 0)
109 goto error;
110
111 sret = read(fd, scale, sizeof(scale)-1);
112 if (sret < 0)
113 goto error;
114
115 scale[sret] = '\0';
116 /*
117 * save current locale
118 */
119 lc = setlocale(LC_NUMERIC, NULL);
120
121 /*
122 * force to C locale to ensure kernel
123 * scale string is converted correctly.
124 * kernel uses default C locale.
125 */
126 setlocale(LC_NUMERIC, "C");
127
128 alias->scale = strtod(scale, NULL);
129
130 /* restore locale */
131 setlocale(LC_NUMERIC, lc);
132
133 ret = 0;
134error:
135 close(fd);
136 return ret;
137}
138
139static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *name)
140{
141 char path[PATH_MAX];
142 ssize_t sret;
143 int fd;
144
145 snprintf(path, PATH_MAX, "%s/%s.unit", dir, name);
146
147 fd = open(path, O_RDONLY);
148 if (fd == -1)
149 return -1;
150
151 sret = read(fd, alias->unit, UNIT_MAX_LEN);
152 if (sret < 0)
153 goto error;
154
155 close(fd);
156
157 alias->unit[sret] = '\0';
158
159 return 0;
160error:
161 close(fd);
162 alias->unit[0] = '\0';
163 return -1;
164}
165
166static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file)
Zheng Yana6146d52012-06-15 14:31:41 +0800167{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300168 struct perf_pmu_alias *alias;
Zheng Yana6146d52012-06-15 14:31:41 +0800169 char buf[256];
170 int ret;
171
172 ret = fread(buf, 1, sizeof(buf), file);
173 if (ret == 0)
174 return -EINVAL;
175 buf[ret] = 0;
176
177 alias = malloc(sizeof(*alias));
178 if (!alias)
179 return -ENOMEM;
180
181 INIT_LIST_HEAD(&alias->terms);
Stephane Eranian410136f2013-11-12 17:58:49 +0100182 alias->scale = 1.0;
183 alias->unit[0] = '\0';
184
Zheng Yana6146d52012-06-15 14:31:41 +0800185 ret = parse_events_terms(&alias->terms, buf);
186 if (ret) {
187 free(alias);
188 return ret;
189 }
190
191 alias->name = strdup(name);
Stephane Eranian410136f2013-11-12 17:58:49 +0100192 /*
193 * load unit name and scale if available
194 */
195 perf_pmu__parse_unit(alias, dir, name);
196 perf_pmu__parse_scale(alias, dir, name);
197
Zheng Yana6146d52012-06-15 14:31:41 +0800198 list_add_tail(&alias->list, list);
Stephane Eranian410136f2013-11-12 17:58:49 +0100199
Zheng Yana6146d52012-06-15 14:31:41 +0800200 return 0;
201}
202
Matt Fleming46441bd2014-09-24 15:04:06 +0100203static inline bool pmu_alias_info_file(char *name)
204{
205 size_t len;
206
207 len = strlen(name);
208 if (len > 5 && !strcmp(name + len - 5, ".unit"))
209 return true;
210 if (len > 6 && !strcmp(name + len - 6, ".scale"))
211 return true;
212
213 return false;
214}
215
Zheng Yana6146d52012-06-15 14:31:41 +0800216/*
217 * Process all the sysfs attributes located under the directory
218 * specified in 'dir' parameter.
219 */
220static int pmu_aliases_parse(char *dir, struct list_head *head)
221{
222 struct dirent *evt_ent;
223 DIR *event_dir;
224 int ret = 0;
225
226 event_dir = opendir(dir);
227 if (!event_dir)
228 return -EINVAL;
229
230 while (!ret && (evt_ent = readdir(event_dir))) {
231 char path[PATH_MAX];
232 char *name = evt_ent->d_name;
233 FILE *file;
234
235 if (!strcmp(name, ".") || !strcmp(name, ".."))
236 continue;
237
Stephane Eranian410136f2013-11-12 17:58:49 +0100238 /*
Matt Fleming46441bd2014-09-24 15:04:06 +0100239 * skip info files parsed in perf_pmu__new_alias()
Stephane Eranian410136f2013-11-12 17:58:49 +0100240 */
Matt Fleming46441bd2014-09-24 15:04:06 +0100241 if (pmu_alias_info_file(name))
Stephane Eranian410136f2013-11-12 17:58:49 +0100242 continue;
243
Zheng Yana6146d52012-06-15 14:31:41 +0800244 snprintf(path, PATH_MAX, "%s/%s", dir, name);
245
246 ret = -EINVAL;
247 file = fopen(path, "r");
248 if (!file)
249 break;
Stephane Eranian410136f2013-11-12 17:58:49 +0100250
251 ret = perf_pmu__new_alias(head, dir, name, file);
Zheng Yana6146d52012-06-15 14:31:41 +0800252 fclose(file);
253 }
254
255 closedir(event_dir);
256 return ret;
257}
258
259/*
260 * Reading the pmu event aliases definition, which should be located at:
261 * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
262 */
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300263static int pmu_aliases(const char *name, struct list_head *head)
Zheng Yana6146d52012-06-15 14:31:41 +0800264{
265 struct stat st;
266 char path[PATH_MAX];
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -0300267 const char *sysfs = sysfs__mountpoint();
Zheng Yana6146d52012-06-15 14:31:41 +0800268
Zheng Yana6146d52012-06-15 14:31:41 +0800269 if (!sysfs)
270 return -1;
271
272 snprintf(path, PATH_MAX,
273 "%s/bus/event_source/devices/%s/events", sysfs, name);
274
275 if (stat(path, &st) < 0)
Jiri Olsa3fded962012-10-10 14:53:16 +0200276 return 0; /* no error if 'events' does not exist */
Zheng Yana6146d52012-06-15 14:31:41 +0800277
278 if (pmu_aliases_parse(path, head))
279 return -1;
280
281 return 0;
282}
283
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300284static int pmu_alias_terms(struct perf_pmu_alias *alias,
Zheng Yana6146d52012-06-15 14:31:41 +0800285 struct list_head *terms)
286{
Jiri Olsa7c2f8162014-04-16 20:49:02 +0200287 struct parse_events_term *term, *cloned;
Zheng Yana6146d52012-06-15 14:31:41 +0800288 LIST_HEAD(list);
289 int ret;
290
291 list_for_each_entry(term, &alias->terms, list) {
Jiri Olsa7c2f8162014-04-16 20:49:02 +0200292 ret = parse_events_term__clone(&cloned, term);
Zheng Yana6146d52012-06-15 14:31:41 +0800293 if (ret) {
294 parse_events__free_terms(&list);
295 return ret;
296 }
Jiri Olsa7c2f8162014-04-16 20:49:02 +0200297 list_add_tail(&cloned->list, &list);
Zheng Yana6146d52012-06-15 14:31:41 +0800298 }
299 list_splice(&list, terms);
300 return 0;
301}
302
Jiri Olsacd82a322012-03-15 20:09:17 +0100303/*
304 * Reading/parsing the default pmu type value, which should be
305 * located at:
306 * /sys/bus/event_source/devices/<dev>/type as sysfs attribute.
307 */
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300308static int pmu_type(const char *name, __u32 *type)
Jiri Olsacd82a322012-03-15 20:09:17 +0100309{
310 struct stat st;
311 char path[PATH_MAX];
Jiri Olsacd82a322012-03-15 20:09:17 +0100312 FILE *file;
313 int ret = 0;
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -0300314 const char *sysfs = sysfs__mountpoint();
Jiri Olsacd82a322012-03-15 20:09:17 +0100315
Jiri Olsacd82a322012-03-15 20:09:17 +0100316 if (!sysfs)
317 return -1;
318
319 snprintf(path, PATH_MAX,
Robert Richter50a96672012-08-16 21:10:24 +0200320 "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name);
Jiri Olsacd82a322012-03-15 20:09:17 +0100321
322 if (stat(path, &st) < 0)
323 return -1;
324
325 file = fopen(path, "r");
326 if (!file)
327 return -EINVAL;
328
329 if (1 != fscanf(file, "%u", type))
330 ret = -1;
331
332 fclose(file);
333 return ret;
334}
335
Robert Richter50a96672012-08-16 21:10:24 +0200336/* Add all pmus in sysfs to pmu list: */
337static void pmu_read_sysfs(void)
338{
339 char path[PATH_MAX];
Robert Richter50a96672012-08-16 21:10:24 +0200340 DIR *dir;
341 struct dirent *dent;
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -0300342 const char *sysfs = sysfs__mountpoint();
Robert Richter50a96672012-08-16 21:10:24 +0200343
Robert Richter50a96672012-08-16 21:10:24 +0200344 if (!sysfs)
345 return;
346
347 snprintf(path, PATH_MAX,
348 "%s" EVENT_SOURCE_DEVICE_PATH, sysfs);
349
350 dir = opendir(path);
351 if (!dir)
352 return;
353
354 while ((dent = readdir(dir))) {
355 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
356 continue;
357 /* add to static LIST_HEAD(pmus): */
358 perf_pmu__find(dent->d_name);
359 }
360
361 closedir(dir);
362}
363
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300364static struct cpu_map *pmu_cpumask(const char *name)
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800365{
366 struct stat st;
367 char path[PATH_MAX];
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800368 FILE *file;
369 struct cpu_map *cpus;
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -0300370 const char *sysfs = sysfs__mountpoint();
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800371
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800372 if (!sysfs)
373 return NULL;
374
375 snprintf(path, PATH_MAX,
376 "%s/bus/event_source/devices/%s/cpumask", sysfs, name);
377
378 if (stat(path, &st) < 0)
379 return NULL;
380
381 file = fopen(path, "r");
382 if (!file)
383 return NULL;
384
385 cpus = cpu_map__read(file);
386 fclose(file);
387 return cpus;
388}
389
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300390struct perf_event_attr *__attribute__((weak))
391perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
392{
393 return NULL;
394}
395
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300396static struct perf_pmu *pmu_lookup(const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +0100397{
398 struct perf_pmu *pmu;
399 LIST_HEAD(format);
Zheng Yana6146d52012-06-15 14:31:41 +0800400 LIST_HEAD(aliases);
Jiri Olsacd82a322012-03-15 20:09:17 +0100401 __u32 type;
402
403 /*
404 * The pmu data we store & need consists of the pmu
405 * type value and format definitions. Load both right
406 * now.
407 */
408 if (pmu_format(name, &format))
409 return NULL;
410
Jiri Olsa3fded962012-10-10 14:53:16 +0200411 if (pmu_aliases(name, &aliases))
412 return NULL;
413
Jiri Olsacd82a322012-03-15 20:09:17 +0100414 if (pmu_type(name, &type))
415 return NULL;
416
417 pmu = zalloc(sizeof(*pmu));
418 if (!pmu)
419 return NULL;
420
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800421 pmu->cpus = pmu_cpumask(name);
422
Jiri Olsacd82a322012-03-15 20:09:17 +0100423 INIT_LIST_HEAD(&pmu->format);
Zheng Yana6146d52012-06-15 14:31:41 +0800424 INIT_LIST_HEAD(&pmu->aliases);
Jiri Olsacd82a322012-03-15 20:09:17 +0100425 list_splice(&format, &pmu->format);
Zheng Yana6146d52012-06-15 14:31:41 +0800426 list_splice(&aliases, &pmu->aliases);
Jiri Olsacd82a322012-03-15 20:09:17 +0100427 pmu->name = strdup(name);
428 pmu->type = type;
Robert Richter9bc8f9f2012-06-14 22:38:37 +0200429 list_add_tail(&pmu->list, &pmus);
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300430
431 pmu->default_config = perf_pmu__get_default_config(pmu);
432
Jiri Olsacd82a322012-03-15 20:09:17 +0100433 return pmu;
434}
435
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300436static struct perf_pmu *pmu_find(const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +0100437{
438 struct perf_pmu *pmu;
439
440 list_for_each_entry(pmu, &pmus, list)
441 if (!strcmp(pmu->name, name))
442 return pmu;
443
444 return NULL;
445}
446
Robert Richter50a96672012-08-16 21:10:24 +0200447struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
448{
449 /*
450 * pmu iterator: If pmu is NULL, we start at the begin,
451 * otherwise return the next pmu. Returns NULL on end.
452 */
453 if (!pmu) {
454 pmu_read_sysfs();
455 pmu = list_prepare_entry(pmu, &pmus, list);
456 }
457 list_for_each_entry_continue(pmu, &pmus, list)
458 return pmu;
459 return NULL;
460}
461
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300462struct perf_pmu *perf_pmu__find(const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +0100463{
464 struct perf_pmu *pmu;
465
466 /*
467 * Once PMU is loaded it stays in the list,
468 * so we keep us from multiple reading/parsing
469 * the pmu format definitions.
470 */
471 pmu = pmu_find(name);
472 if (pmu)
473 return pmu;
474
475 return pmu_lookup(name);
476}
477
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300478static struct perf_pmu_format *
Jiri Olsacd82a322012-03-15 20:09:17 +0100479pmu_find_format(struct list_head *formats, char *name)
480{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300481 struct perf_pmu_format *format;
Jiri Olsacd82a322012-03-15 20:09:17 +0100482
483 list_for_each_entry(format, formats, list)
484 if (!strcmp(format->name, name))
485 return format;
486
487 return NULL;
488}
489
490/*
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300491 * Sets value based on the format definition (format parameter)
Jiri Olsacd82a322012-03-15 20:09:17 +0100492 * and unformated value (value parameter).
Jiri Olsacd82a322012-03-15 20:09:17 +0100493 */
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300494static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
495 bool zero)
Jiri Olsacd82a322012-03-15 20:09:17 +0100496{
497 unsigned long fbit, vbit;
Jiri Olsacd82a322012-03-15 20:09:17 +0100498
499 for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
500
501 if (!test_bit(fbit, format))
502 continue;
503
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300504 if (value & (1llu << vbit++))
505 *v |= (1llu << fbit);
506 else if (zero)
507 *v &= ~(1llu << fbit);
Jiri Olsacd82a322012-03-15 20:09:17 +0100508 }
Jiri Olsacd82a322012-03-15 20:09:17 +0100509}
510
511/*
512 * Setup one of config[12] attr members based on the
Cody P Schafer88aca8d2014-01-08 08:43:51 -0800513 * user input data - term parameter.
Jiri Olsacd82a322012-03-15 20:09:17 +0100514 */
515static int pmu_config_term(struct list_head *formats,
516 struct perf_event_attr *attr,
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300517 struct parse_events_term *term,
518 bool zero)
Jiri Olsacd82a322012-03-15 20:09:17 +0100519{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300520 struct perf_pmu_format *format;
Jiri Olsacd82a322012-03-15 20:09:17 +0100521 __u64 *vp;
522
523 /*
524 * Support only for hardcoded and numnerial terms.
525 * Hardcoded terms should be already in, so nothing
526 * to be done for them.
527 */
528 if (parse_events__is_hardcoded_term(term))
529 return 0;
530
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200531 if (term->type_val != PARSE_EVENTS__TERM_TYPE_NUM)
Jiri Olsacd82a322012-03-15 20:09:17 +0100532 return -EINVAL;
533
534 format = pmu_find_format(formats, term->config);
535 if (!format)
536 return -EINVAL;
537
538 switch (format->value) {
539 case PERF_PMU_FORMAT_VALUE_CONFIG:
540 vp = &attr->config;
541 break;
542 case PERF_PMU_FORMAT_VALUE_CONFIG1:
543 vp = &attr->config1;
544 break;
545 case PERF_PMU_FORMAT_VALUE_CONFIG2:
546 vp = &attr->config2;
547 break;
548 default:
549 return -EINVAL;
550 }
551
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200552 /*
553 * XXX If we ever decide to go with string values for
554 * non-hardcoded terms, here's the place to translate
555 * them into value.
556 */
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300557 pmu_format_value(format->bits, term->val.num, vp, zero);
Jiri Olsacd82a322012-03-15 20:09:17 +0100558 return 0;
559}
560
Jiri Olsacff7f952012-11-10 01:46:50 +0100561int perf_pmu__config_terms(struct list_head *formats,
562 struct perf_event_attr *attr,
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300563 struct list_head *head_terms,
564 bool zero)
Jiri Olsacd82a322012-03-15 20:09:17 +0100565{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -0300566 struct parse_events_term *term;
Jiri Olsacd82a322012-03-15 20:09:17 +0100567
Jiri Olsa6b5fc392012-05-21 09:12:53 +0200568 list_for_each_entry(term, head_terms, list)
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300569 if (pmu_config_term(formats, attr, term, zero))
Jiri Olsacd82a322012-03-15 20:09:17 +0100570 return -EINVAL;
571
572 return 0;
573}
574
575/*
576 * Configures event's 'attr' parameter based on the:
577 * 1) users input - specified in terms parameter
578 * 2) pmu format definitions - specified by pmu parameter
579 */
580int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
581 struct list_head *head_terms)
582{
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300583 bool zero = !!pmu->default_config;
584
Jiri Olsacd82a322012-03-15 20:09:17 +0100585 attr->type = pmu->type;
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300586 return perf_pmu__config_terms(&pmu->format, attr, head_terms, zero);
Jiri Olsacd82a322012-03-15 20:09:17 +0100587}
588
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300589static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
590 struct parse_events_term *term)
Zheng Yana6146d52012-06-15 14:31:41 +0800591{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300592 struct perf_pmu_alias *alias;
Zheng Yana6146d52012-06-15 14:31:41 +0800593 char *name;
594
595 if (parse_events__is_hardcoded_term(term))
596 return NULL;
597
598 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
599 if (term->val.num != 1)
600 return NULL;
601 if (pmu_find_format(&pmu->format, term->config))
602 return NULL;
603 name = term->config;
604 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
605 if (strcasecmp(term->config, "event"))
606 return NULL;
607 name = term->val.str;
608 } else {
609 return NULL;
610 }
611
612 list_for_each_entry(alias, &pmu->aliases, list) {
613 if (!strcasecmp(alias->name, name))
614 return alias;
615 }
616 return NULL;
617}
618
Stephane Eranian410136f2013-11-12 17:58:49 +0100619
620static int check_unit_scale(struct perf_pmu_alias *alias,
Stephane Eranian8a398892014-01-17 16:34:05 +0100621 const char **unit, double *scale)
Stephane Eranian410136f2013-11-12 17:58:49 +0100622{
623 /*
624 * Only one term in event definition can
625 * define unit and scale, fail if there's
626 * more than one.
627 */
628 if ((*unit && alias->unit) ||
629 (*scale && alias->scale))
630 return -EINVAL;
631
632 if (alias->unit)
633 *unit = alias->unit;
634
635 if (alias->scale)
636 *scale = alias->scale;
637
638 return 0;
639}
640
Zheng Yana6146d52012-06-15 14:31:41 +0800641/*
642 * Find alias in the terms list and replace it with the terms
643 * defined for the alias
644 */
Stephane Eranian410136f2013-11-12 17:58:49 +0100645int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
Matt Fleming46441bd2014-09-24 15:04:06 +0100646 struct perf_pmu_info *info)
Zheng Yana6146d52012-06-15 14:31:41 +0800647{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -0300648 struct parse_events_term *term, *h;
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300649 struct perf_pmu_alias *alias;
Zheng Yana6146d52012-06-15 14:31:41 +0800650 int ret;
651
Stephane Eranian8a398892014-01-17 16:34:05 +0100652 /*
653 * Mark unit and scale as not set
654 * (different from default values, see below)
655 */
Matt Fleming46441bd2014-09-24 15:04:06 +0100656 info->unit = NULL;
657 info->scale = 0.0;
Stephane Eranian410136f2013-11-12 17:58:49 +0100658
Zheng Yana6146d52012-06-15 14:31:41 +0800659 list_for_each_entry_safe(term, h, head_terms, list) {
660 alias = pmu_find_alias(pmu, term);
661 if (!alias)
662 continue;
663 ret = pmu_alias_terms(alias, &term->list);
664 if (ret)
665 return ret;
Stephane Eranian410136f2013-11-12 17:58:49 +0100666
Matt Fleming46441bd2014-09-24 15:04:06 +0100667 ret = check_unit_scale(alias, &info->unit, &info->scale);
Stephane Eranian410136f2013-11-12 17:58:49 +0100668 if (ret)
669 return ret;
670
Zheng Yana6146d52012-06-15 14:31:41 +0800671 list_del(&term->list);
672 free(term);
673 }
Stephane Eranian8a398892014-01-17 16:34:05 +0100674
675 /*
676 * if no unit or scale foundin aliases, then
677 * set defaults as for evsel
678 * unit cannot left to NULL
679 */
Matt Fleming46441bd2014-09-24 15:04:06 +0100680 if (info->unit == NULL)
681 info->unit = "";
Stephane Eranian8a398892014-01-17 16:34:05 +0100682
Matt Fleming46441bd2014-09-24 15:04:06 +0100683 if (info->scale == 0.0)
684 info->scale = 1.0;
Stephane Eranian8a398892014-01-17 16:34:05 +0100685
Zheng Yana6146d52012-06-15 14:31:41 +0800686 return 0;
687}
688
Jiri Olsacd82a322012-03-15 20:09:17 +0100689int perf_pmu__new_format(struct list_head *list, char *name,
690 int config, unsigned long *bits)
691{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300692 struct perf_pmu_format *format;
Jiri Olsacd82a322012-03-15 20:09:17 +0100693
694 format = zalloc(sizeof(*format));
695 if (!format)
696 return -ENOMEM;
697
698 format->name = strdup(name);
699 format->value = config;
700 memcpy(format->bits, bits, sizeof(format->bits));
701
702 list_add_tail(&format->list, list);
703 return 0;
704}
705
706void perf_pmu__set_format(unsigned long *bits, long from, long to)
707{
708 long b;
709
710 if (!to)
711 to = from;
712
Sukadev Bhattiprolu15268132013-01-17 09:11:30 -0800713 memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
Jiri Olsacd82a322012-03-15 20:09:17 +0100714 for (b = from; b <= to; b++)
715 set_bit(b, bits);
716}
Andi Kleendc098b32013-04-20 11:02:29 -0700717
718static char *format_alias(char *buf, int len, struct perf_pmu *pmu,
719 struct perf_pmu_alias *alias)
720{
721 snprintf(buf, len, "%s/%s/", pmu->name, alias->name);
722 return buf;
723}
724
725static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu,
726 struct perf_pmu_alias *alias)
727{
728 snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name);
729 return buf;
730}
731
732static int cmp_string(const void *a, const void *b)
733{
734 const char * const *as = a;
735 const char * const *bs = b;
736 return strcmp(*as, *bs);
737}
738
739void print_pmu_events(const char *event_glob, bool name_only)
740{
741 struct perf_pmu *pmu;
742 struct perf_pmu_alias *alias;
743 char buf[1024];
744 int printed = 0;
745 int len, j;
746 char **aliases;
747
748 pmu = NULL;
749 len = 0;
750 while ((pmu = perf_pmu__scan(pmu)) != NULL)
751 list_for_each_entry(alias, &pmu->aliases, list)
752 len++;
753 aliases = malloc(sizeof(char *) * len);
754 if (!aliases)
755 return;
756 pmu = NULL;
757 j = 0;
758 while ((pmu = perf_pmu__scan(pmu)) != NULL)
759 list_for_each_entry(alias, &pmu->aliases, list) {
760 char *name = format_alias(buf, sizeof(buf), pmu, alias);
761 bool is_cpu = !strcmp(pmu->name, "cpu");
762
763 if (event_glob != NULL &&
764 !(strglobmatch(name, event_glob) ||
765 (!is_cpu && strglobmatch(alias->name,
766 event_glob))))
767 continue;
768 aliases[j] = name;
769 if (is_cpu && !name_only)
770 aliases[j] = format_alias_or(buf, sizeof(buf),
771 pmu, alias);
772 aliases[j] = strdup(aliases[j]);
773 j++;
774 }
775 len = j;
776 qsort(aliases, len, sizeof(char *), cmp_string);
777 for (j = 0; j < len; j++) {
778 if (name_only) {
779 printf("%s ", aliases[j]);
780 continue;
781 }
782 printf(" %-50s [Kernel PMU event]\n", aliases[j]);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300783 zfree(&aliases[j]);
Andi Kleendc098b32013-04-20 11:02:29 -0700784 printed++;
785 }
786 if (printed)
787 printf("\n");
788 free(aliases);
789}
Andi Kleen4cabc3d2013-08-21 16:47:26 -0700790
791bool pmu_have_event(const char *pname, const char *name)
792{
793 struct perf_pmu *pmu;
794 struct perf_pmu_alias *alias;
795
796 pmu = NULL;
797 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
798 if (strcmp(pname, pmu->name))
799 continue;
800 list_for_each_entry(alias, &pmu->aliases, list)
801 if (!strcmp(alias->name, name))
802 return true;
803 }
804 return false;
805}
Adrian Hunter7d4bdab2014-07-31 09:00:50 +0300806
807static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
808{
809 struct stat st;
810 char path[PATH_MAX];
811 const char *sysfs;
812
813 sysfs = sysfs__mountpoint();
814 if (!sysfs)
815 return NULL;
816
817 snprintf(path, PATH_MAX,
818 "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name);
819
820 if (stat(path, &st) < 0)
821 return NULL;
822
823 return fopen(path, "r");
824}
825
826int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
827 ...)
828{
829 va_list args;
830 FILE *file;
831 int ret = EOF;
832
833 va_start(args, fmt);
834 file = perf_pmu__open_file(pmu, name);
835 if (file) {
836 ret = vfscanf(file, fmt, args);
837 fclose(file);
838 }
839 va_end(args);
840 return ret;
841}