blob: f321ce97d9ec13f0b961b761470f6b6d2d50e526 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Jiri Olsacd82a322012-03-15 20:09:17 +01002#include <linux/list.h>
Sukadev Bhattiproluc5de47f2015-06-10 00:25:07 -07003#include <linux/compiler.h>
Jiri Olsacd82a322012-03-15 20:09:17 +01004#include <sys/types.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03005#include <errno.h>
Arnaldo Carvalho de Meloc23c2a02017-09-11 10:50:26 -03006#include <fcntl.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -03007#include <sys/stat.h>
Jiri Olsacd82a322012-03-15 20:09:17 +01008#include <unistd.h>
9#include <stdio.h>
Adrian Hunterdc0a6202014-07-31 09:00:49 +030010#include <stdbool.h>
Adrian Hunter7d4bdab2014-07-31 09:00:50 +030011#include <stdarg.h>
Jiri Olsacd82a322012-03-15 20:09:17 +010012#include <dirent.h>
Borislav Petkovcd0cfad2013-12-09 17:14:24 +010013#include <api/fs/fs.h>
Stephane Eranian410136f2013-11-12 17:58:49 +010014#include <locale.h>
William Cohenfbc28442017-12-04 09:57:28 -050015#include <regex.h>
Jiri Olsacd82a322012-03-15 20:09:17 +010016#include "util.h"
17#include "pmu.h"
18#include "parse-events.h"
Yan, Zheng7ae92e72012-09-10 15:53:50 +080019#include "cpumap.h"
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -070020#include "header.h"
21#include "pmu-events/pmu-events.h"
Andi Kleen61eb2eb2016-09-15 15:24:44 -070022#include "cache.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030023#include "string2.h"
Jiri Olsacd82a322012-03-15 20:09:17 +010024
Arnaldo Carvalho de Meloab1bf6532013-01-18 17:05:09 -030025struct perf_pmu_format {
26 char *name;
27 int value;
28 DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
29 struct list_head list;
30};
31
Robert Richter50a96672012-08-16 21:10:24 +020032#define EVENT_SOURCE_DEVICE_PATH "/bus/event_source/devices/"
33
Jiri Olsacd82a322012-03-15 20:09:17 +010034int perf_pmu_parse(struct list_head *list, char *name);
35extern FILE *perf_pmu_in;
36
37static LIST_HEAD(pmus);
38
39/*
40 * Parse & process all the sysfs attributes located under
41 * the directory specified in 'dir' parameter.
42 */
Jiri Olsacff7f952012-11-10 01:46:50 +010043int perf_pmu__format_parse(char *dir, struct list_head *head)
Jiri Olsacd82a322012-03-15 20:09:17 +010044{
45 struct dirent *evt_ent;
46 DIR *format_dir;
47 int ret = 0;
48
49 format_dir = opendir(dir);
50 if (!format_dir)
51 return -EINVAL;
52
53 while (!ret && (evt_ent = readdir(format_dir))) {
54 char path[PATH_MAX];
55 char *name = evt_ent->d_name;
56 FILE *file;
57
58 if (!strcmp(name, ".") || !strcmp(name, ".."))
59 continue;
60
61 snprintf(path, PATH_MAX, "%s/%s", dir, name);
62
63 ret = -EINVAL;
64 file = fopen(path, "r");
65 if (!file)
66 break;
67
68 perf_pmu_in = file;
69 ret = perf_pmu_parse(head, name);
70 fclose(file);
71 }
72
73 closedir(format_dir);
74 return ret;
75}
76
77/*
78 * Reading/parsing the default pmu format definition, which should be
79 * located at:
80 * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
81 */
Adrian Hunterb6b96fb2013-07-04 16:20:25 +030082static int pmu_format(const char *name, struct list_head *format)
Jiri Olsacd82a322012-03-15 20:09:17 +010083{
84 struct stat st;
85 char path[PATH_MAX];
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -030086 const char *sysfs = sysfs__mountpoint();
Jiri Olsacd82a322012-03-15 20:09:17 +010087
Jiri Olsacd82a322012-03-15 20:09:17 +010088 if (!sysfs)
89 return -1;
90
91 snprintf(path, PATH_MAX,
Robert Richter50a96672012-08-16 21:10:24 +020092 "%s" EVENT_SOURCE_DEVICE_PATH "%s/format", sysfs, name);
Jiri Olsacd82a322012-03-15 20:09:17 +010093
94 if (stat(path, &st) < 0)
Robert Richter9bc8f9f2012-06-14 22:38:37 +020095 return 0; /* no error if format does not exist */
Jiri Olsacd82a322012-03-15 20:09:17 +010096
Jiri Olsacff7f952012-11-10 01:46:50 +010097 if (perf_pmu__format_parse(path, format))
Jiri Olsacd82a322012-03-15 20:09:17 +010098 return -1;
99
100 return 0;
101}
102
Andi Kleend02fc6b2017-01-03 07:08:23 -0800103static int convert_scale(const char *scale, char **end, double *sval)
104{
105 char *lc;
106 int ret = 0;
107
108 /*
109 * save current locale
110 */
111 lc = setlocale(LC_NUMERIC, NULL);
112
113 /*
114 * The lc string may be allocated in static storage,
115 * so get a dynamic copy to make it survive setlocale
116 * call below.
117 */
118 lc = strdup(lc);
119 if (!lc) {
120 ret = -ENOMEM;
121 goto out;
122 }
123
124 /*
125 * force to C locale to ensure kernel
126 * scale string is converted correctly.
127 * kernel uses default C locale.
128 */
129 setlocale(LC_NUMERIC, "C");
130
131 *sval = strtod(scale, end);
132
133out:
134 /* restore locale */
135 setlocale(LC_NUMERIC, lc);
136 free(lc);
137 return ret;
138}
139
Stephane Eranian410136f2013-11-12 17:58:49 +0100140static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *name)
141{
142 struct stat st;
143 ssize_t sret;
144 char scale[128];
145 int fd, ret = -1;
146 char path[PATH_MAX];
Stephane Eranian410136f2013-11-12 17:58:49 +0100147
148 snprintf(path, PATH_MAX, "%s/%s.scale", dir, name);
149
150 fd = open(path, O_RDONLY);
151 if (fd == -1)
152 return -1;
153
154 if (fstat(fd, &st) < 0)
155 goto error;
156
157 sret = read(fd, scale, sizeof(scale)-1);
158 if (sret < 0)
159 goto error;
160
Madhavan Srinivasan9ecae062015-05-31 11:36:23 +0530161 if (scale[sret - 1] == '\n')
162 scale[sret - 1] = '\0';
163 else
164 scale[sret] = '\0';
165
Andi Kleend02fc6b2017-01-03 07:08:23 -0800166 ret = convert_scale(scale, NULL, &alias->scale);
Stephane Eranian410136f2013-11-12 17:58:49 +0100167error:
168 close(fd);
169 return ret;
170}
171
172static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *name)
173{
174 char path[PATH_MAX];
175 ssize_t sret;
176 int fd;
177
178 snprintf(path, PATH_MAX, "%s/%s.unit", dir, name);
179
180 fd = open(path, O_RDONLY);
181 if (fd == -1)
182 return -1;
183
Markus Trippelsdorfd85ce832015-12-14 16:44:40 +0100184 sret = read(fd, alias->unit, UNIT_MAX_LEN);
Stephane Eranian410136f2013-11-12 17:58:49 +0100185 if (sret < 0)
186 goto error;
187
188 close(fd);
189
Madhavan Srinivasan9ecae062015-05-31 11:36:23 +0530190 if (alias->unit[sret - 1] == '\n')
191 alias->unit[sret - 1] = '\0';
192 else
193 alias->unit[sret] = '\0';
Stephane Eranian410136f2013-11-12 17:58:49 +0100194
195 return 0;
196error:
197 close(fd);
198 alias->unit[0] = '\0';
199 return -1;
200}
201
Matt Fleming044330c2014-11-21 10:31:12 +0100202static int
203perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, char *dir, char *name)
204{
205 char path[PATH_MAX];
206 int fd;
207
208 snprintf(path, PATH_MAX, "%s/%s.per-pkg", dir, name);
209
210 fd = open(path, O_RDONLY);
211 if (fd == -1)
212 return -1;
213
214 close(fd);
215
216 alias->per_pkg = true;
217 return 0;
218}
219
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100220static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias,
221 char *dir, char *name)
222{
223 char path[PATH_MAX];
224 int fd;
225
226 snprintf(path, PATH_MAX, "%s/%s.snapshot", dir, name);
227
228 fd = open(path, O_RDONLY);
229 if (fd == -1)
230 return -1;
231
232 alias->snapshot = true;
233 close(fd);
234 return 0;
235}
236
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700237static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name,
Andi Kleenfedb2b52017-01-27 18:03:37 -0800238 char *desc, char *val,
239 char *long_desc, char *topic,
Andi Kleen00636c32017-03-20 13:17:07 -0700240 char *unit, char *perpkg,
Andi Kleen96284812017-03-20 13:17:10 -0700241 char *metric_expr,
242 char *metric_name)
Zheng Yana6146d52012-06-15 14:31:41 +0800243{
Thomas Richter0c24d6f2018-06-15 12:11:04 +0200244 struct parse_events_term *term;
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300245 struct perf_pmu_alias *alias;
Zheng Yana6146d52012-06-15 14:31:41 +0800246 int ret;
Andi Kleenfedb2b52017-01-27 18:03:37 -0800247 int num;
Thomas Richter0c24d6f2018-06-15 12:11:04 +0200248 char newval[256];
Zheng Yana6146d52012-06-15 14:31:41 +0800249
Zheng Yana6146d52012-06-15 14:31:41 +0800250 alias = malloc(sizeof(*alias));
251 if (!alias)
252 return -ENOMEM;
253
254 INIT_LIST_HEAD(&alias->terms);
Stephane Eranian410136f2013-11-12 17:58:49 +0100255 alias->scale = 1.0;
256 alias->unit[0] = '\0';
Matt Fleming044330c2014-11-21 10:31:12 +0100257 alias->per_pkg = false;
Stephane Eranian84530922016-01-06 19:50:01 +0100258 alias->snapshot = false;
Stephane Eranian410136f2013-11-12 17:58:49 +0100259
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700260 ret = parse_events_terms(&alias->terms, val);
Zheng Yana6146d52012-06-15 14:31:41 +0800261 if (ret) {
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700262 pr_err("Cannot parse alias %s: %d\n", val, ret);
Zheng Yana6146d52012-06-15 14:31:41 +0800263 free(alias);
264 return ret;
265 }
266
Thomas Richter0c24d6f2018-06-15 12:11:04 +0200267 /* Scan event and remove leading zeroes, spaces, newlines, some
268 * platforms have terms specified as
269 * event=0x0091 (read from files ../<PMU>/events/<FILE>
270 * and terms specified as event=0x91 (read from JSON files).
271 *
272 * Rebuild string to make alias->str member comparable.
273 */
274 memset(newval, 0, sizeof(newval));
275 ret = 0;
276 list_for_each_entry(term, &alias->terms, list) {
277 if (ret)
278 ret += scnprintf(newval + ret, sizeof(newval) - ret,
279 ",");
280 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM)
281 ret += scnprintf(newval + ret, sizeof(newval) - ret,
282 "%s=%#x", term->config, term->val.num);
283 else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
284 ret += scnprintf(newval + ret, sizeof(newval) - ret,
285 "%s=%s", term->config, term->val.str);
286 }
287
Zheng Yana6146d52012-06-15 14:31:41 +0800288 alias->name = strdup(name);
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700289 if (dir) {
290 /*
291 * load unit name and scale if available
292 */
293 perf_pmu__parse_unit(alias, dir, name);
294 perf_pmu__parse_scale(alias, dir, name);
295 perf_pmu__parse_per_pkg(alias, dir, name);
296 perf_pmu__parse_snapshot(alias, dir, name);
297 }
Stephane Eranian410136f2013-11-12 17:58:49 +0100298
Andi Kleen00636c32017-03-20 13:17:07 -0700299 alias->metric_expr = metric_expr ? strdup(metric_expr) : NULL;
Andi Kleen96284812017-03-20 13:17:10 -0700300 alias->metric_name = metric_name ? strdup(metric_name): NULL;
Andi Kleen08e60ed2016-09-15 15:24:43 -0700301 alias->desc = desc ? strdup(desc) : NULL;
Sukadev Bhattiproluc8d68282016-09-15 15:24:48 -0700302 alias->long_desc = long_desc ? strdup(long_desc) :
303 desc ? strdup(desc) : NULL;
Andi Kleendd5f1032016-09-15 15:24:50 -0700304 alias->topic = topic ? strdup(topic) : NULL;
Andi Kleenfedb2b52017-01-27 18:03:37 -0800305 if (unit) {
306 if (convert_scale(unit, &unit, &alias->scale) < 0)
307 return -1;
308 snprintf(alias->unit, sizeof(alias->unit), "%s", unit);
309 }
310 alias->per_pkg = perpkg && sscanf(perpkg, "%d", &num) == 1 && num == 1;
Thomas Richter0c24d6f2018-06-15 12:11:04 +0200311 alias->str = strdup(newval);
Andi Kleenf2361022017-01-27 18:03:40 -0800312
Zheng Yana6146d52012-06-15 14:31:41 +0800313 list_add_tail(&alias->list, list);
Stephane Eranian410136f2013-11-12 17:58:49 +0100314
Zheng Yana6146d52012-06-15 14:31:41 +0800315 return 0;
316}
317
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700318static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file)
319{
320 char buf[256];
321 int ret;
322
323 ret = fread(buf, 1, sizeof(buf), file);
324 if (ret == 0)
325 return -EINVAL;
326
327 buf[ret] = 0;
328
Thomas Richterea23ac72018-06-15 12:11:03 +0200329 /* Remove trailing newline from sysfs file */
330 rtrim(buf);
331
Andi Kleenfedb2b52017-01-27 18:03:37 -0800332 return __perf_pmu__new_alias(list, dir, name, NULL, buf, NULL, NULL, NULL,
Andi Kleen96284812017-03-20 13:17:10 -0700333 NULL, NULL, NULL);
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700334}
335
Matt Fleming46441bd2014-09-24 15:04:06 +0100336static inline bool pmu_alias_info_file(char *name)
337{
338 size_t len;
339
340 len = strlen(name);
341 if (len > 5 && !strcmp(name + len - 5, ".unit"))
342 return true;
343 if (len > 6 && !strcmp(name + len - 6, ".scale"))
344 return true;
Matt Fleming044330c2014-11-21 10:31:12 +0100345 if (len > 8 && !strcmp(name + len - 8, ".per-pkg"))
346 return true;
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100347 if (len > 9 && !strcmp(name + len - 9, ".snapshot"))
348 return true;
Matt Fleming46441bd2014-09-24 15:04:06 +0100349
350 return false;
351}
352
Zheng Yana6146d52012-06-15 14:31:41 +0800353/*
354 * Process all the sysfs attributes located under the directory
355 * specified in 'dir' parameter.
356 */
357static int pmu_aliases_parse(char *dir, struct list_head *head)
358{
359 struct dirent *evt_ent;
360 DIR *event_dir;
Zheng Yana6146d52012-06-15 14:31:41 +0800361
362 event_dir = opendir(dir);
363 if (!event_dir)
364 return -EINVAL;
365
Andi Kleen940db6d2016-02-17 14:44:55 -0800366 while ((evt_ent = readdir(event_dir))) {
Zheng Yana6146d52012-06-15 14:31:41 +0800367 char path[PATH_MAX];
368 char *name = evt_ent->d_name;
369 FILE *file;
370
371 if (!strcmp(name, ".") || !strcmp(name, ".."))
372 continue;
373
Stephane Eranian410136f2013-11-12 17:58:49 +0100374 /*
Matt Fleming46441bd2014-09-24 15:04:06 +0100375 * skip info files parsed in perf_pmu__new_alias()
Stephane Eranian410136f2013-11-12 17:58:49 +0100376 */
Matt Fleming46441bd2014-09-24 15:04:06 +0100377 if (pmu_alias_info_file(name))
Stephane Eranian410136f2013-11-12 17:58:49 +0100378 continue;
379
Jiri Olsa77f18152018-03-19 09:29:01 +0100380 scnprintf(path, PATH_MAX, "%s/%s", dir, name);
Zheng Yana6146d52012-06-15 14:31:41 +0800381
Zheng Yana6146d52012-06-15 14:31:41 +0800382 file = fopen(path, "r");
Andi Kleen940db6d2016-02-17 14:44:55 -0800383 if (!file) {
384 pr_debug("Cannot open %s\n", path);
385 continue;
386 }
Stephane Eranian410136f2013-11-12 17:58:49 +0100387
Andi Kleen940db6d2016-02-17 14:44:55 -0800388 if (perf_pmu__new_alias(head, dir, name, file) < 0)
389 pr_debug("Cannot set up %s\n", name);
Zheng Yana6146d52012-06-15 14:31:41 +0800390 fclose(file);
391 }
392
393 closedir(event_dir);
Andi Kleen940db6d2016-02-17 14:44:55 -0800394 return 0;
Zheng Yana6146d52012-06-15 14:31:41 +0800395}
396
397/*
398 * Reading the pmu event aliases definition, which should be located at:
399 * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
400 */
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300401static int pmu_aliases(const char *name, struct list_head *head)
Zheng Yana6146d52012-06-15 14:31:41 +0800402{
403 struct stat st;
404 char path[PATH_MAX];
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -0300405 const char *sysfs = sysfs__mountpoint();
Zheng Yana6146d52012-06-15 14:31:41 +0800406
Zheng Yana6146d52012-06-15 14:31:41 +0800407 if (!sysfs)
408 return -1;
409
410 snprintf(path, PATH_MAX,
411 "%s/bus/event_source/devices/%s/events", sysfs, name);
412
413 if (stat(path, &st) < 0)
Jiri Olsa3fded962012-10-10 14:53:16 +0200414 return 0; /* no error if 'events' does not exist */
Zheng Yana6146d52012-06-15 14:31:41 +0800415
416 if (pmu_aliases_parse(path, head))
417 return -1;
418
419 return 0;
420}
421
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300422static int pmu_alias_terms(struct perf_pmu_alias *alias,
Zheng Yana6146d52012-06-15 14:31:41 +0800423 struct list_head *terms)
424{
Jiri Olsa7c2f8162014-04-16 20:49:02 +0200425 struct parse_events_term *term, *cloned;
Zheng Yana6146d52012-06-15 14:31:41 +0800426 LIST_HEAD(list);
427 int ret;
428
429 list_for_each_entry(term, &alias->terms, list) {
Jiri Olsa7c2f8162014-04-16 20:49:02 +0200430 ret = parse_events_term__clone(&cloned, term);
Zheng Yana6146d52012-06-15 14:31:41 +0800431 if (ret) {
Arnaldo Carvalho de Melo682dc242016-02-12 16:48:00 -0300432 parse_events_terms__purge(&list);
Zheng Yana6146d52012-06-15 14:31:41 +0800433 return ret;
434 }
Andi Kleenc2f1cea2017-10-20 13:27:55 -0700435 /*
436 * Weak terms don't override command line options,
437 * which we don't want for implicit terms in aliases.
438 */
439 cloned->weak = true;
Jiri Olsa7c2f8162014-04-16 20:49:02 +0200440 list_add_tail(&cloned->list, &list);
Zheng Yana6146d52012-06-15 14:31:41 +0800441 }
442 list_splice(&list, terms);
443 return 0;
444}
445
Jiri Olsacd82a322012-03-15 20:09:17 +0100446/*
447 * Reading/parsing the default pmu type value, which should be
448 * located at:
449 * /sys/bus/event_source/devices/<dev>/type as sysfs attribute.
450 */
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300451static int pmu_type(const char *name, __u32 *type)
Jiri Olsacd82a322012-03-15 20:09:17 +0100452{
453 struct stat st;
454 char path[PATH_MAX];
Jiri Olsacd82a322012-03-15 20:09:17 +0100455 FILE *file;
456 int ret = 0;
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -0300457 const char *sysfs = sysfs__mountpoint();
Jiri Olsacd82a322012-03-15 20:09:17 +0100458
Jiri Olsacd82a322012-03-15 20:09:17 +0100459 if (!sysfs)
460 return -1;
461
462 snprintf(path, PATH_MAX,
Robert Richter50a96672012-08-16 21:10:24 +0200463 "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name);
Jiri Olsacd82a322012-03-15 20:09:17 +0100464
465 if (stat(path, &st) < 0)
466 return -1;
467
468 file = fopen(path, "r");
469 if (!file)
470 return -EINVAL;
471
472 if (1 != fscanf(file, "%u", type))
473 ret = -1;
474
475 fclose(file);
476 return ret;
477}
478
Robert Richter50a96672012-08-16 21:10:24 +0200479/* Add all pmus in sysfs to pmu list: */
480static void pmu_read_sysfs(void)
481{
482 char path[PATH_MAX];
Robert Richter50a96672012-08-16 21:10:24 +0200483 DIR *dir;
484 struct dirent *dent;
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -0300485 const char *sysfs = sysfs__mountpoint();
Robert Richter50a96672012-08-16 21:10:24 +0200486
Robert Richter50a96672012-08-16 21:10:24 +0200487 if (!sysfs)
488 return;
489
490 snprintf(path, PATH_MAX,
491 "%s" EVENT_SOURCE_DEVICE_PATH, sysfs);
492
493 dir = opendir(path);
494 if (!dir)
495 return;
496
497 while ((dent = readdir(dir))) {
498 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
499 continue;
500 /* add to static LIST_HEAD(pmus): */
501 perf_pmu__find(dent->d_name);
502 }
503
504 closedir(dir);
505}
506
Mark Rutland66ec11912017-10-06 19:38:22 +0100507static struct cpu_map *__pmu_cpumask(const char *path)
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800508{
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800509 FILE *file;
510 struct cpu_map *cpus;
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800511
512 file = fopen(path, "r");
513 if (!file)
514 return NULL;
515
516 cpus = cpu_map__read(file);
517 fclose(file);
518 return cpus;
519}
520
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700521/*
Mark Rutland66ec11912017-10-06 19:38:22 +0100522 * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64)
523 * may have a "cpus" file.
524 */
525#define CPUS_TEMPLATE_UNCORE "%s/bus/event_source/devices/%s/cpumask"
526#define CPUS_TEMPLATE_CPU "%s/bus/event_source/devices/%s/cpus"
527
528static struct cpu_map *pmu_cpumask(const char *name)
529{
530 char path[PATH_MAX];
531 struct cpu_map *cpus;
532 const char *sysfs = sysfs__mountpoint();
533 const char *templates[] = {
534 CPUS_TEMPLATE_UNCORE,
535 CPUS_TEMPLATE_CPU,
536 NULL
537 };
538 const char **template;
539
540 if (!sysfs)
541 return NULL;
542
543 for (template = templates; *template; template++) {
544 snprintf(path, PATH_MAX, *template, sysfs, name);
545 cpus = __pmu_cpumask(path);
546 if (cpus)
547 return cpus;
548 }
549
550 return NULL;
551}
552
553static bool pmu_is_uncore(const char *name)
554{
555 char path[PATH_MAX];
556 struct cpu_map *cpus;
557 const char *sysfs = sysfs__mountpoint();
558
559 snprintf(path, PATH_MAX, CPUS_TEMPLATE_UNCORE, sysfs, name);
560 cpus = __pmu_cpumask(path);
561 cpu_map__put(cpus);
562
563 return !!cpus;
564}
565
566/*
Ganapatrao Kulkarni14b22ae2017-08-24 16:30:58 +0530567 * PMU CORE devices have different name other than cpu in sysfs on some
Kan Liang292c34c2018-04-24 11:20:10 -0700568 * platforms.
569 * Looking for possible sysfs files to identify the arm core device.
Ganapatrao Kulkarni14b22ae2017-08-24 16:30:58 +0530570 */
Kan Liang292c34c2018-04-24 11:20:10 -0700571static int is_arm_pmu_core(const char *name)
Ganapatrao Kulkarni14b22ae2017-08-24 16:30:58 +0530572{
573 struct stat st;
574 char path[PATH_MAX];
575 const char *sysfs = sysfs__mountpoint();
576
577 if (!sysfs)
578 return 0;
579
Ganapatrao Kulkarni14b22ae2017-08-24 16:30:58 +0530580 /* Look for cpu sysfs (specific to arm) */
581 scnprintf(path, PATH_MAX, "%s/bus/event_source/devices/%s/cpus",
582 sysfs, name);
583 if (stat(path, &st) == 0)
584 return 1;
585
Thomas Richter038586c2018-04-16 15:23:14 +0200586 /* Look for cpu sysfs (specific to s390) */
587 scnprintf(path, PATH_MAX, "%s/bus/event_source/devices/%s",
588 sysfs, name);
589 if (stat(path, &st) == 0 && !strncmp(name, "cpum_", 5))
590 return 1;
591
Ganapatrao Kulkarni14b22ae2017-08-24 16:30:58 +0530592 return 0;
593}
594
595/*
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700596 * Return the CPU id as a raw string.
597 *
598 * Each architecture should provide a more precise id string that
599 * can be use to match the architecture's "mapfile".
600 */
Ganapatrao Kulkarni54e32dc2017-10-17 00:02:18 +0530601char * __weak get_cpuid_str(struct perf_pmu *pmu __maybe_unused)
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700602{
603 return NULL;
604}
605
Thomas Richter4cb7d3e2018-02-13 16:14:18 +0100606/* Return zero when the cpuid from the mapfile.csv matches the
607 * cpuid string generated on this platform.
608 * Otherwise return non-zero.
609 */
Thomas Richterce04abf2018-04-23 10:17:45 +0200610int strcmp_cpuid_str(const char *mapcpuid, const char *cpuid)
Thomas Richter4cb7d3e2018-02-13 16:14:18 +0100611{
612 regex_t re;
613 regmatch_t pmatch[1];
614 int match;
615
616 if (regcomp(&re, mapcpuid, REG_EXTENDED) != 0) {
617 /* Warn unable to generate match particular string. */
618 pr_info("Invalid regular expression %s\n", mapcpuid);
619 return 1;
620 }
621
622 match = !regexec(&re, cpuid, 1, pmatch, 0);
623 regfree(&re);
624 if (match) {
625 size_t match_len = (pmatch[0].rm_eo - pmatch[0].rm_so);
626
627 /* Verify the entire string matched. */
628 if (match_len == strlen(cpuid))
629 return 0;
630 }
631 return 1;
632}
633
Ganapatrao Kulkarni54e32dc2017-10-17 00:02:18 +0530634static char *perf_pmu__getcpuid(struct perf_pmu *pmu)
Andi Kleend77ade92017-08-31 12:40:30 -0700635{
636 char *cpuid;
637 static bool printed;
638
639 cpuid = getenv("PERF_CPUID");
640 if (cpuid)
641 cpuid = strdup(cpuid);
642 if (!cpuid)
Ganapatrao Kulkarni54e32dc2017-10-17 00:02:18 +0530643 cpuid = get_cpuid_str(pmu);
Andi Kleend77ade92017-08-31 12:40:30 -0700644 if (!cpuid)
645 return NULL;
646
647 if (!printed) {
648 pr_debug("Using CPUID %s\n", cpuid);
649 printed = true;
650 }
651 return cpuid;
652}
653
Ganapatrao Kulkarni54e32dc2017-10-17 00:02:18 +0530654struct pmu_events_map *perf_pmu__find_map(struct perf_pmu *pmu)
Andi Kleend77ade92017-08-31 12:40:30 -0700655{
656 struct pmu_events_map *map;
Ganapatrao Kulkarni54e32dc2017-10-17 00:02:18 +0530657 char *cpuid = perf_pmu__getcpuid(pmu);
Andi Kleend77ade92017-08-31 12:40:30 -0700658 int i;
659
Ganapatrao Kulkarnide3d0f12017-10-17 00:02:22 +0530660 /* on some platforms which uses cpus map, cpuid can be NULL for
661 * PMUs other than CORE PMUs.
662 */
663 if (!cpuid)
664 return NULL;
665
Andi Kleend77ade92017-08-31 12:40:30 -0700666 i = 0;
667 for (;;) {
668 map = &pmu_events_map[i++];
669 if (!map->table) {
670 map = NULL;
671 break;
672 }
673
Thomas Richter4cb7d3e2018-02-13 16:14:18 +0100674 if (!strcmp_cpuid_str(map->cpuid, cpuid))
Andi Kleend77ade92017-08-31 12:40:30 -0700675 break;
676 }
677 free(cpuid);
678 return map;
679}
680
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700681/*
682 * From the pmu_events_map, find the table of PMU events that corresponds
683 * to the current running CPU. Then, add all PMU events from that table
684 * as aliases.
685 */
Ganapatrao Kulkarni54e32dc2017-10-17 00:02:18 +0530686static void pmu_add_cpu_aliases(struct list_head *head, struct perf_pmu *pmu)
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700687{
688 int i;
689 struct pmu_events_map *map;
690 struct pmu_event *pe;
Ganapatrao Kulkarni54e32dc2017-10-17 00:02:18 +0530691 const char *name = pmu->name;
Kan Liang292c34c2018-04-24 11:20:10 -0700692 const char *pname;
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700693
Ganapatrao Kulkarni54e32dc2017-10-17 00:02:18 +0530694 map = perf_pmu__find_map(pmu);
Andi Kleend77ade92017-08-31 12:40:30 -0700695 if (!map)
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700696 return;
697
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700698 /*
699 * Found a matching PMU events table. Create aliases
700 */
701 i = 0;
702 while (1) {
Andi Kleenfedb2b52017-01-27 18:03:37 -0800703
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700704 pe = &map->table[i++];
Andi Kleenb18f3e32017-08-31 12:40:31 -0700705 if (!pe->name) {
706 if (pe->metric_group || pe->metric_name)
707 continue;
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700708 break;
Andi Kleenb18f3e32017-08-31 12:40:31 -0700709 }
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700710
Kan Liang292c34c2018-04-24 11:20:10 -0700711 if (!is_arm_pmu_core(name)) {
712 pname = pe->pmu ? pe->pmu : "cpu";
713 if (strncmp(pname, name, strlen(pname)))
Ganapatrao Kulkarni14b22ae2017-08-24 16:30:58 +0530714 continue;
715 }
Andi Kleenfedb2b52017-01-27 18:03:37 -0800716
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700717 /* need type casts to override 'const' */
718 __perf_pmu__new_alias(head, NULL, (char *)pe->name,
Sukadev Bhattiproluc8d68282016-09-15 15:24:48 -0700719 (char *)pe->desc, (char *)pe->event,
Andi Kleenfedb2b52017-01-27 18:03:37 -0800720 (char *)pe->long_desc, (char *)pe->topic,
Andi Kleen00636c32017-03-20 13:17:07 -0700721 (char *)pe->unit, (char *)pe->perpkg,
Andi Kleen96284812017-03-20 13:17:10 -0700722 (char *)pe->metric_expr,
723 (char *)pe->metric_name);
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700724 }
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700725}
726
Sukadev Bhattiproluc5de47f2015-06-10 00:25:07 -0700727struct perf_event_attr * __weak
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300728perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
729{
730 return NULL;
731}
732
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300733static struct perf_pmu *pmu_lookup(const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +0100734{
735 struct perf_pmu *pmu;
736 LIST_HEAD(format);
Zheng Yana6146d52012-06-15 14:31:41 +0800737 LIST_HEAD(aliases);
Jiri Olsacd82a322012-03-15 20:09:17 +0100738 __u32 type;
739
740 /*
741 * The pmu data we store & need consists of the pmu
742 * type value and format definitions. Load both right
743 * now.
744 */
745 if (pmu_format(name, &format))
746 return NULL;
747
Andi Kleen15b22ed2017-01-27 18:03:38 -0800748 /*
749 * Check the type first to avoid unnecessary work.
750 */
751 if (pmu_type(name, &type))
752 return NULL;
753
Jiri Olsa3fded962012-10-10 14:53:16 +0200754 if (pmu_aliases(name, &aliases))
755 return NULL;
756
Jiri Olsacd82a322012-03-15 20:09:17 +0100757 pmu = zalloc(sizeof(*pmu));
758 if (!pmu)
759 return NULL;
760
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800761 pmu->cpus = pmu_cpumask(name);
Ganapatrao Kulkarni54e32dc2017-10-17 00:02:18 +0530762 pmu->name = strdup(name);
763 pmu->type = type;
Mark Rutland66ec11912017-10-06 19:38:22 +0100764 pmu->is_uncore = pmu_is_uncore(name);
Ganapatrao Kulkarni54e32dc2017-10-17 00:02:18 +0530765 pmu_add_cpu_aliases(&aliases, pmu);
Mark Rutland66ec11912017-10-06 19:38:22 +0100766
Jiri Olsacd82a322012-03-15 20:09:17 +0100767 INIT_LIST_HEAD(&pmu->format);
Zheng Yana6146d52012-06-15 14:31:41 +0800768 INIT_LIST_HEAD(&pmu->aliases);
Jiri Olsacd82a322012-03-15 20:09:17 +0100769 list_splice(&format, &pmu->format);
Zheng Yana6146d52012-06-15 14:31:41 +0800770 list_splice(&aliases, &pmu->aliases);
Robert Richter9bc8f9f2012-06-14 22:38:37 +0200771 list_add_tail(&pmu->list, &pmus);
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300772
773 pmu->default_config = perf_pmu__get_default_config(pmu);
774
Jiri Olsacd82a322012-03-15 20:09:17 +0100775 return pmu;
776}
777
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300778static struct perf_pmu *pmu_find(const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +0100779{
780 struct perf_pmu *pmu;
781
782 list_for_each_entry(pmu, &pmus, list)
783 if (!strcmp(pmu->name, name))
784 return pmu;
785
786 return NULL;
787}
788
Robert Richter50a96672012-08-16 21:10:24 +0200789struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
790{
791 /*
792 * pmu iterator: If pmu is NULL, we start at the begin,
793 * otherwise return the next pmu. Returns NULL on end.
794 */
795 if (!pmu) {
796 pmu_read_sysfs();
797 pmu = list_prepare_entry(pmu, &pmus, list);
798 }
799 list_for_each_entry_continue(pmu, &pmus, list)
800 return pmu;
801 return NULL;
802}
803
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300804struct perf_pmu *perf_pmu__find(const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +0100805{
806 struct perf_pmu *pmu;
807
808 /*
809 * Once PMU is loaded it stays in the list,
810 * so we keep us from multiple reading/parsing
811 * the pmu format definitions.
812 */
813 pmu = pmu_find(name);
814 if (pmu)
815 return pmu;
816
817 return pmu_lookup(name);
818}
819
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300820static struct perf_pmu_format *
Adrian Hunter09ff6072015-07-17 19:33:49 +0300821pmu_find_format(struct list_head *formats, const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +0100822{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300823 struct perf_pmu_format *format;
Jiri Olsacd82a322012-03-15 20:09:17 +0100824
825 list_for_each_entry(format, formats, list)
826 if (!strcmp(format->name, name))
827 return format;
828
829 return NULL;
830}
831
Adrian Hunter09ff6072015-07-17 19:33:49 +0300832__u64 perf_pmu__format_bits(struct list_head *formats, const char *name)
833{
834 struct perf_pmu_format *format = pmu_find_format(formats, name);
835 __u64 bits = 0;
836 int fbit;
837
838 if (!format)
839 return 0;
840
841 for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
842 bits |= 1ULL << fbit;
843
844 return bits;
845}
846
Jiri Olsacd82a322012-03-15 20:09:17 +0100847/*
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300848 * Sets value based on the format definition (format parameter)
Jiri Olsacd82a322012-03-15 20:09:17 +0100849 * and unformated value (value parameter).
Jiri Olsacd82a322012-03-15 20:09:17 +0100850 */
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300851static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
852 bool zero)
Jiri Olsacd82a322012-03-15 20:09:17 +0100853{
854 unsigned long fbit, vbit;
Jiri Olsacd82a322012-03-15 20:09:17 +0100855
856 for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
857
858 if (!test_bit(fbit, format))
859 continue;
860
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300861 if (value & (1llu << vbit++))
862 *v |= (1llu << fbit);
863 else if (zero)
864 *v &= ~(1llu << fbit);
Jiri Olsacd82a322012-03-15 20:09:17 +0100865 }
Jiri Olsacd82a322012-03-15 20:09:17 +0100866}
867
Adrian Hunter0efe6b62015-07-17 19:33:50 +0300868static __u64 pmu_format_max_value(const unsigned long *format)
869{
Kan Liangac0e2cd2016-03-30 12:16:15 -0700870 __u64 w = 0;
871 int fbit;
Adrian Hunter0efe6b62015-07-17 19:33:50 +0300872
Kan Liangac0e2cd2016-03-30 12:16:15 -0700873 for_each_set_bit(fbit, format, PERF_PMU_FORMAT_BITS)
874 w |= (1ULL << fbit);
875
876 return w;
Adrian Hunter0efe6b62015-07-17 19:33:50 +0300877}
878
Jiri Olsacd82a322012-03-15 20:09:17 +0100879/*
Cody P Schafer688d4df2015-01-07 17:13:50 -0800880 * Term is a string term, and might be a param-term. Try to look up it's value
881 * in the remaining terms.
882 * - We have a term like "base-or-format-term=param-term",
883 * - We need to find the value supplied for "param-term" (with param-term named
884 * in a config string) later on in the term list.
885 */
886static int pmu_resolve_param_term(struct parse_events_term *term,
887 struct list_head *head_terms,
888 __u64 *value)
889{
890 struct parse_events_term *t;
891
892 list_for_each_entry(t, head_terms, list) {
893 if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
894 if (!strcmp(t->config, term->config)) {
895 t->used = true;
896 *value = t->val.num;
897 return 0;
898 }
899 }
900 }
901
Namhyung Kimbb963e12017-02-17 17:17:38 +0900902 if (verbose > 0)
Cody P Schafer688d4df2015-01-07 17:13:50 -0800903 printf("Required parameter '%s' not specified\n", term->config);
904
905 return -1;
906}
907
He Kuangffeb8832015-09-28 03:52:14 +0000908static char *pmu_formats_string(struct list_head *formats)
Jiri Olsae64b0202015-04-22 21:10:21 +0200909{
910 struct perf_pmu_format *format;
Masami Hiramatsu11db4e22016-05-10 14:47:44 +0900911 char *str = NULL;
912 struct strbuf buf = STRBUF_INIT;
Jiri Olsae64b0202015-04-22 21:10:21 +0200913 unsigned i = 0;
914
He Kuangffeb8832015-09-28 03:52:14 +0000915 if (!formats)
Jiri Olsae64b0202015-04-22 21:10:21 +0200916 return NULL;
917
918 /* sysfs exported terms */
He Kuangffeb8832015-09-28 03:52:14 +0000919 list_for_each_entry(format, formats, list)
Masami Hiramatsu11db4e22016-05-10 14:47:44 +0900920 if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
921 goto error;
Jiri Olsae64b0202015-04-22 21:10:21 +0200922
He Kuangffeb8832015-09-28 03:52:14 +0000923 str = strbuf_detach(&buf, NULL);
Masami Hiramatsu11db4e22016-05-10 14:47:44 +0900924error:
He Kuangffeb8832015-09-28 03:52:14 +0000925 strbuf_release(&buf);
Jiri Olsae64b0202015-04-22 21:10:21 +0200926
Jiri Olsae64b0202015-04-22 21:10:21 +0200927 return str;
Jiri Olsae64b0202015-04-22 21:10:21 +0200928}
929
Cody P Schafer688d4df2015-01-07 17:13:50 -0800930/*
Jiri Olsacd82a322012-03-15 20:09:17 +0100931 * Setup one of config[12] attr members based on the
Cody P Schafer88aca8d2014-01-08 08:43:51 -0800932 * user input data - term parameter.
Jiri Olsacd82a322012-03-15 20:09:17 +0100933 */
934static int pmu_config_term(struct list_head *formats,
935 struct perf_event_attr *attr,
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300936 struct parse_events_term *term,
Cody P Schafer688d4df2015-01-07 17:13:50 -0800937 struct list_head *head_terms,
Jiri Olsae64b0202015-04-22 21:10:21 +0200938 bool zero, struct parse_events_error *err)
Jiri Olsacd82a322012-03-15 20:09:17 +0100939{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300940 struct perf_pmu_format *format;
Jiri Olsacd82a322012-03-15 20:09:17 +0100941 __u64 *vp;
Adrian Hunter0efe6b62015-07-17 19:33:50 +0300942 __u64 val, max_val;
Jiri Olsacd82a322012-03-15 20:09:17 +0100943
944 /*
Cody P Schafer688d4df2015-01-07 17:13:50 -0800945 * If this is a parameter we've already used for parameterized-eval,
946 * skip it in normal eval.
947 */
948 if (term->used)
949 return 0;
950
951 /*
Jiri Olsacd82a322012-03-15 20:09:17 +0100952 * Hardcoded terms should be already in, so nothing
953 * to be done for them.
954 */
955 if (parse_events__is_hardcoded_term(term))
956 return 0;
957
Jiri Olsacd82a322012-03-15 20:09:17 +0100958 format = pmu_find_format(formats, term->config);
Cody P Schafer688d4df2015-01-07 17:13:50 -0800959 if (!format) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900960 if (verbose > 0)
Cody P Schafer688d4df2015-01-07 17:13:50 -0800961 printf("Invalid event/parameter '%s'\n", term->config);
Jiri Olsae64b0202015-04-22 21:10:21 +0200962 if (err) {
He Kuangffeb8832015-09-28 03:52:14 +0000963 char *pmu_term = pmu_formats_string(formats);
964
Jiri Olsae64b0202015-04-22 21:10:21 +0200965 err->idx = term->err_term;
966 err->str = strdup("unknown term");
He Kuangffeb8832015-09-28 03:52:14 +0000967 err->help = parse_events_formats_error_string(pmu_term);
968 free(pmu_term);
Jiri Olsae64b0202015-04-22 21:10:21 +0200969 }
Jiri Olsacd82a322012-03-15 20:09:17 +0100970 return -EINVAL;
Cody P Schafer688d4df2015-01-07 17:13:50 -0800971 }
Jiri Olsacd82a322012-03-15 20:09:17 +0100972
973 switch (format->value) {
974 case PERF_PMU_FORMAT_VALUE_CONFIG:
975 vp = &attr->config;
976 break;
977 case PERF_PMU_FORMAT_VALUE_CONFIG1:
978 vp = &attr->config1;
979 break;
980 case PERF_PMU_FORMAT_VALUE_CONFIG2:
981 vp = &attr->config2;
982 break;
983 default:
984 return -EINVAL;
985 }
986
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200987 /*
Cody P Schafer688d4df2015-01-07 17:13:50 -0800988 * Either directly use a numeric term, or try to translate string terms
989 * using event parameters.
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200990 */
Jiri Olsa99e71382017-02-17 15:00:56 +0100991 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
992 if (term->no_value &&
993 bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) {
994 if (err) {
995 err->idx = term->err_val;
996 err->str = strdup("no value assigned for term");
997 }
998 return -EINVAL;
999 }
1000
Cody P Schafer688d4df2015-01-07 17:13:50 -08001001 val = term->val.num;
Jiri Olsa99e71382017-02-17 15:00:56 +01001002 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
Cody P Schafer688d4df2015-01-07 17:13:50 -08001003 if (strcmp(term->val.str, "?")) {
Namhyung Kimbb963e12017-02-17 17:17:38 +09001004 if (verbose > 0) {
Cody P Schafer688d4df2015-01-07 17:13:50 -08001005 pr_info("Invalid sysfs entry %s=%s\n",
1006 term->config, term->val.str);
Jiri Olsae64b0202015-04-22 21:10:21 +02001007 }
1008 if (err) {
1009 err->idx = term->err_val;
1010 err->str = strdup("expected numeric value");
1011 }
Cody P Schafer688d4df2015-01-07 17:13:50 -08001012 return -EINVAL;
1013 }
1014
1015 if (pmu_resolve_param_term(term, head_terms, &val))
1016 return -EINVAL;
1017 } else
1018 return -EINVAL;
1019
Adrian Hunter0efe6b62015-07-17 19:33:50 +03001020 max_val = pmu_format_max_value(format->bits);
1021 if (val > max_val) {
1022 if (err) {
1023 err->idx = term->err_val;
1024 if (asprintf(&err->str,
1025 "value too big for format, maximum is %llu",
1026 (unsigned long long)max_val) < 0)
1027 err->str = strdup("value too big for format");
1028 return -EINVAL;
1029 }
1030 /*
1031 * Assume we don't care if !err, in which case the value will be
1032 * silently truncated.
1033 */
1034 }
1035
Cody P Schafer688d4df2015-01-07 17:13:50 -08001036 pmu_format_value(format->bits, val, vp, zero);
Jiri Olsacd82a322012-03-15 20:09:17 +01001037 return 0;
1038}
1039
Jiri Olsacff7f952012-11-10 01:46:50 +01001040int perf_pmu__config_terms(struct list_head *formats,
1041 struct perf_event_attr *attr,
Adrian Hunterdc0a6202014-07-31 09:00:49 +03001042 struct list_head *head_terms,
Jiri Olsae64b0202015-04-22 21:10:21 +02001043 bool zero, struct parse_events_error *err)
Jiri Olsacd82a322012-03-15 20:09:17 +01001044{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001045 struct parse_events_term *term;
Jiri Olsacd82a322012-03-15 20:09:17 +01001046
Cody P Schafer688d4df2015-01-07 17:13:50 -08001047 list_for_each_entry(term, head_terms, list) {
Jiri Olsae64b0202015-04-22 21:10:21 +02001048 if (pmu_config_term(formats, attr, term, head_terms,
1049 zero, err))
Jiri Olsacd82a322012-03-15 20:09:17 +01001050 return -EINVAL;
Cody P Schafer688d4df2015-01-07 17:13:50 -08001051 }
Jiri Olsacd82a322012-03-15 20:09:17 +01001052
1053 return 0;
1054}
1055
1056/*
1057 * Configures event's 'attr' parameter based on the:
1058 * 1) users input - specified in terms parameter
1059 * 2) pmu format definitions - specified by pmu parameter
1060 */
1061int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
Jiri Olsae64b0202015-04-22 21:10:21 +02001062 struct list_head *head_terms,
1063 struct parse_events_error *err)
Jiri Olsacd82a322012-03-15 20:09:17 +01001064{
Adrian Hunterdc0a6202014-07-31 09:00:49 +03001065 bool zero = !!pmu->default_config;
1066
Jiri Olsacd82a322012-03-15 20:09:17 +01001067 attr->type = pmu->type;
Jiri Olsae64b0202015-04-22 21:10:21 +02001068 return perf_pmu__config_terms(&pmu->format, attr, head_terms,
1069 zero, err);
Jiri Olsacd82a322012-03-15 20:09:17 +01001070}
1071
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -03001072static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
1073 struct parse_events_term *term)
Zheng Yana6146d52012-06-15 14:31:41 +08001074{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -03001075 struct perf_pmu_alias *alias;
Zheng Yana6146d52012-06-15 14:31:41 +08001076 char *name;
1077
1078 if (parse_events__is_hardcoded_term(term))
1079 return NULL;
1080
1081 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1082 if (term->val.num != 1)
1083 return NULL;
1084 if (pmu_find_format(&pmu->format, term->config))
1085 return NULL;
1086 name = term->config;
1087 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1088 if (strcasecmp(term->config, "event"))
1089 return NULL;
1090 name = term->val.str;
1091 } else {
1092 return NULL;
1093 }
1094
1095 list_for_each_entry(alias, &pmu->aliases, list) {
1096 if (!strcasecmp(alias->name, name))
1097 return alias;
1098 }
1099 return NULL;
1100}
1101
Stephane Eranian410136f2013-11-12 17:58:49 +01001102
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001103static int check_info_data(struct perf_pmu_alias *alias,
1104 struct perf_pmu_info *info)
Stephane Eranian410136f2013-11-12 17:58:49 +01001105{
1106 /*
1107 * Only one term in event definition can
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001108 * define unit, scale and snapshot, fail
1109 * if there's more than one.
Stephane Eranian410136f2013-11-12 17:58:49 +01001110 */
Arnaldo Carvalho de Melob30a7d12017-02-15 10:06:20 -03001111 if ((info->unit && alias->unit[0]) ||
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001112 (info->scale && alias->scale) ||
1113 (info->snapshot && alias->snapshot))
Stephane Eranian410136f2013-11-12 17:58:49 +01001114 return -EINVAL;
1115
Arnaldo Carvalho de Melob30a7d12017-02-15 10:06:20 -03001116 if (alias->unit[0])
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001117 info->unit = alias->unit;
Stephane Eranian410136f2013-11-12 17:58:49 +01001118
1119 if (alias->scale)
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001120 info->scale = alias->scale;
1121
1122 if (alias->snapshot)
1123 info->snapshot = alias->snapshot;
Stephane Eranian410136f2013-11-12 17:58:49 +01001124
1125 return 0;
1126}
1127
Zheng Yana6146d52012-06-15 14:31:41 +08001128/*
1129 * Find alias in the terms list and replace it with the terms
1130 * defined for the alias
1131 */
Stephane Eranian410136f2013-11-12 17:58:49 +01001132int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
Matt Fleming46441bd2014-09-24 15:04:06 +01001133 struct perf_pmu_info *info)
Zheng Yana6146d52012-06-15 14:31:41 +08001134{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001135 struct parse_events_term *term, *h;
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -03001136 struct perf_pmu_alias *alias;
Zheng Yana6146d52012-06-15 14:31:41 +08001137 int ret;
1138
Matt Fleming044330c2014-11-21 10:31:12 +01001139 info->per_pkg = false;
1140
Stephane Eranian8a398892014-01-17 16:34:05 +01001141 /*
1142 * Mark unit and scale as not set
1143 * (different from default values, see below)
1144 */
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001145 info->unit = NULL;
1146 info->scale = 0.0;
1147 info->snapshot = false;
Andi Kleen37932c12017-03-20 13:17:08 -07001148 info->metric_expr = NULL;
Andi Kleen96284812017-03-20 13:17:10 -07001149 info->metric_name = NULL;
Stephane Eranian410136f2013-11-12 17:58:49 +01001150
Zheng Yana6146d52012-06-15 14:31:41 +08001151 list_for_each_entry_safe(term, h, head_terms, list) {
1152 alias = pmu_find_alias(pmu, term);
1153 if (!alias)
1154 continue;
1155 ret = pmu_alias_terms(alias, &term->list);
1156 if (ret)
1157 return ret;
Stephane Eranian410136f2013-11-12 17:58:49 +01001158
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001159 ret = check_info_data(alias, info);
Stephane Eranian410136f2013-11-12 17:58:49 +01001160 if (ret)
1161 return ret;
1162
Matt Fleming044330c2014-11-21 10:31:12 +01001163 if (alias->per_pkg)
1164 info->per_pkg = true;
Andi Kleen37932c12017-03-20 13:17:08 -07001165 info->metric_expr = alias->metric_expr;
Andi Kleen96284812017-03-20 13:17:10 -07001166 info->metric_name = alias->metric_name;
Matt Fleming044330c2014-11-21 10:31:12 +01001167
Zheng Yana6146d52012-06-15 14:31:41 +08001168 list_del(&term->list);
1169 free(term);
1170 }
Stephane Eranian8a398892014-01-17 16:34:05 +01001171
1172 /*
1173 * if no unit or scale foundin aliases, then
1174 * set defaults as for evsel
1175 * unit cannot left to NULL
1176 */
Matt Fleming46441bd2014-09-24 15:04:06 +01001177 if (info->unit == NULL)
1178 info->unit = "";
Stephane Eranian8a398892014-01-17 16:34:05 +01001179
Matt Fleming46441bd2014-09-24 15:04:06 +01001180 if (info->scale == 0.0)
1181 info->scale = 1.0;
Stephane Eranian8a398892014-01-17 16:34:05 +01001182
Zheng Yana6146d52012-06-15 14:31:41 +08001183 return 0;
1184}
1185
Jiri Olsacd82a322012-03-15 20:09:17 +01001186int perf_pmu__new_format(struct list_head *list, char *name,
1187 int config, unsigned long *bits)
1188{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -03001189 struct perf_pmu_format *format;
Jiri Olsacd82a322012-03-15 20:09:17 +01001190
1191 format = zalloc(sizeof(*format));
1192 if (!format)
1193 return -ENOMEM;
1194
1195 format->name = strdup(name);
1196 format->value = config;
1197 memcpy(format->bits, bits, sizeof(format->bits));
1198
1199 list_add_tail(&format->list, list);
1200 return 0;
1201}
1202
1203void perf_pmu__set_format(unsigned long *bits, long from, long to)
1204{
1205 long b;
1206
1207 if (!to)
1208 to = from;
1209
Sukadev Bhattiprolu15268132013-01-17 09:11:30 -08001210 memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
Jiri Olsacd82a322012-03-15 20:09:17 +01001211 for (b = from; b <= to; b++)
1212 set_bit(b, bits);
1213}
Andi Kleendc098b32013-04-20 11:02:29 -07001214
Cody P Schaferaaea3612015-01-07 17:13:51 -08001215static int sub_non_neg(int a, int b)
1216{
1217 if (b > a)
1218 return 0;
1219 return a - b;
1220}
1221
Andi Kleendc098b32013-04-20 11:02:29 -07001222static char *format_alias(char *buf, int len, struct perf_pmu *pmu,
1223 struct perf_pmu_alias *alias)
1224{
Cody P Schaferaaea3612015-01-07 17:13:51 -08001225 struct parse_events_term *term;
1226 int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name);
1227
1228 list_for_each_entry(term, &alias->terms, list) {
1229 if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
1230 used += snprintf(buf + used, sub_non_neg(len, used),
1231 ",%s=%s", term->config,
1232 term->val.str);
1233 }
1234
1235 if (sub_non_neg(len, used) > 0) {
1236 buf[used] = '/';
1237 used++;
1238 }
1239 if (sub_non_neg(len, used) > 0) {
1240 buf[used] = '\0';
1241 used++;
1242 } else
1243 buf[len - 1] = '\0';
1244
Andi Kleendc098b32013-04-20 11:02:29 -07001245 return buf;
1246}
1247
1248static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu,
1249 struct perf_pmu_alias *alias)
1250{
1251 snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name);
1252 return buf;
1253}
1254
Andi Kleendd5f1032016-09-15 15:24:50 -07001255struct sevent {
Andi Kleen08e60ed2016-09-15 15:24:43 -07001256 char *name;
1257 char *desc;
Andi Kleendd5f1032016-09-15 15:24:50 -07001258 char *topic;
Andi Kleenf2361022017-01-27 18:03:40 -08001259 char *str;
1260 char *pmu;
Andi Kleen7f372a62017-03-20 13:17:09 -07001261 char *metric_expr;
Andi Kleen96284812017-03-20 13:17:10 -07001262 char *metric_name;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001263};
1264
Andi Kleendd5f1032016-09-15 15:24:50 -07001265static int cmp_sevent(const void *a, const void *b)
Andi Kleendc098b32013-04-20 11:02:29 -07001266{
Andi Kleendd5f1032016-09-15 15:24:50 -07001267 const struct sevent *as = a;
1268 const struct sevent *bs = b;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001269
1270 /* Put extra events last */
1271 if (!!as->desc != !!bs->desc)
1272 return !!as->desc - !!bs->desc;
Andi Kleendd5f1032016-09-15 15:24:50 -07001273 if (as->topic && bs->topic) {
1274 int n = strcmp(as->topic, bs->topic);
1275
1276 if (n)
1277 return n;
1278 }
Andi Kleen08e60ed2016-09-15 15:24:43 -07001279 return strcmp(as->name, bs->name);
1280}
1281
1282static void wordwrap(char *s, int start, int max, int corr)
1283{
1284 int column = start;
1285 int n;
1286
1287 while (*s) {
1288 int wlen = strcspn(s, " \t");
1289
1290 if (column + wlen >= max && column > start) {
1291 printf("\n%*s", start, "");
1292 column = start + corr;
1293 }
1294 n = printf("%s%.*s", column > start ? " " : "", wlen, s);
1295 if (n <= 0)
1296 break;
1297 s += wlen;
1298 column += n;
Taeung Songaa4beb12017-04-07 23:24:20 +09001299 s = ltrim(s);
Andi Kleen08e60ed2016-09-15 15:24:43 -07001300 }
Andi Kleendc098b32013-04-20 11:02:29 -07001301}
1302
Sukadev Bhattiproluc8d68282016-09-15 15:24:48 -07001303void print_pmu_events(const char *event_glob, bool name_only, bool quiet_flag,
Andi Kleenbf874fc2017-03-20 13:17:11 -07001304 bool long_desc, bool details_flag)
Andi Kleendc098b32013-04-20 11:02:29 -07001305{
1306 struct perf_pmu *pmu;
1307 struct perf_pmu_alias *alias;
1308 char buf[1024];
1309 int printed = 0;
1310 int len, j;
Andi Kleendd5f1032016-09-15 15:24:50 -07001311 struct sevent *aliases;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001312 int numdesc = 0;
Andi Kleen61eb2eb2016-09-15 15:24:44 -07001313 int columns = pager_get_columns();
Andi Kleendd5f1032016-09-15 15:24:50 -07001314 char *topic = NULL;
Andi Kleendc098b32013-04-20 11:02:29 -07001315
1316 pmu = NULL;
1317 len = 0;
Adrian Hunter42634bc2014-10-23 13:45:10 +03001318 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
Andi Kleendc098b32013-04-20 11:02:29 -07001319 list_for_each_entry(alias, &pmu->aliases, list)
1320 len++;
Adrian Hunter42634bc2014-10-23 13:45:10 +03001321 if (pmu->selectable)
1322 len++;
1323 }
Andi Kleendd5f1032016-09-15 15:24:50 -07001324 aliases = zalloc(sizeof(struct sevent) * len);
Andi Kleendc098b32013-04-20 11:02:29 -07001325 if (!aliases)
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001326 goto out_enomem;
Andi Kleendc098b32013-04-20 11:02:29 -07001327 pmu = NULL;
1328 j = 0;
Adrian Hunter42634bc2014-10-23 13:45:10 +03001329 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
Andi Kleendc098b32013-04-20 11:02:29 -07001330 list_for_each_entry(alias, &pmu->aliases, list) {
Andi Kleen08e60ed2016-09-15 15:24:43 -07001331 char *name = alias->desc ? alias->name :
1332 format_alias(buf, sizeof(buf), pmu, alias);
Andi Kleendc098b32013-04-20 11:02:29 -07001333 bool is_cpu = !strcmp(pmu->name, "cpu");
1334
1335 if (event_glob != NULL &&
Andi Kleen38d14f02016-10-19 10:50:01 -07001336 !(strglobmatch_nocase(name, event_glob) ||
1337 (!is_cpu && strglobmatch_nocase(alias->name,
Andi Kleen67bdc352016-10-19 11:45:23 -07001338 event_glob)) ||
1339 (alias->topic &&
1340 strglobmatch_nocase(alias->topic, event_glob))))
Andi Kleendc098b32013-04-20 11:02:29 -07001341 continue;
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001342
Andi Kleen08e60ed2016-09-15 15:24:43 -07001343 if (is_cpu && !name_only && !alias->desc)
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001344 name = format_alias_or(buf, sizeof(buf), pmu, alias);
1345
Andi Kleen08e60ed2016-09-15 15:24:43 -07001346 aliases[j].name = name;
1347 if (is_cpu && !name_only && !alias->desc)
1348 aliases[j].name = format_alias_or(buf,
1349 sizeof(buf),
1350 pmu, alias);
1351 aliases[j].name = strdup(aliases[j].name);
1352 if (!aliases[j].name)
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001353 goto out_enomem;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001354
Sukadev Bhattiproluc8d68282016-09-15 15:24:48 -07001355 aliases[j].desc = long_desc ? alias->long_desc :
1356 alias->desc;
Andi Kleendd5f1032016-09-15 15:24:50 -07001357 aliases[j].topic = alias->topic;
Andi Kleenf2361022017-01-27 18:03:40 -08001358 aliases[j].str = alias->str;
1359 aliases[j].pmu = pmu->name;
Andi Kleen7f372a62017-03-20 13:17:09 -07001360 aliases[j].metric_expr = alias->metric_expr;
Andi Kleen96284812017-03-20 13:17:10 -07001361 aliases[j].metric_name = alias->metric_name;
Andi Kleendc098b32013-04-20 11:02:29 -07001362 j++;
1363 }
Arnaldo Carvalho de Melofa52cea2015-10-02 15:28:16 -03001364 if (pmu->selectable &&
1365 (event_glob == NULL || strglobmatch(pmu->name, event_glob))) {
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001366 char *s;
1367 if (asprintf(&s, "%s//", pmu->name) < 0)
1368 goto out_enomem;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001369 aliases[j].name = s;
Adrian Hunter42634bc2014-10-23 13:45:10 +03001370 j++;
1371 }
1372 }
Andi Kleendc098b32013-04-20 11:02:29 -07001373 len = j;
Andi Kleendd5f1032016-09-15 15:24:50 -07001374 qsort(aliases, len, sizeof(struct sevent), cmp_sevent);
Andi Kleendc098b32013-04-20 11:02:29 -07001375 for (j = 0; j < len; j++) {
Andi Kleen15b22ed2017-01-27 18:03:38 -08001376 /* Skip duplicates */
1377 if (j > 0 && !strcmp(aliases[j].name, aliases[j - 1].name))
1378 continue;
Andi Kleendc098b32013-04-20 11:02:29 -07001379 if (name_only) {
Andi Kleen08e60ed2016-09-15 15:24:43 -07001380 printf("%s ", aliases[j].name);
Andi Kleendc098b32013-04-20 11:02:29 -07001381 continue;
1382 }
Andi Kleen1c5f01f2016-09-15 15:24:45 -07001383 if (aliases[j].desc && !quiet_flag) {
Andi Kleen08e60ed2016-09-15 15:24:43 -07001384 if (numdesc++ == 0)
1385 printf("\n");
Andi Kleendd5f1032016-09-15 15:24:50 -07001386 if (aliases[j].topic && (!topic ||
1387 strcmp(topic, aliases[j].topic))) {
1388 printf("%s%s:\n", topic ? "\n" : "",
1389 aliases[j].topic);
1390 topic = aliases[j].topic;
1391 }
Andi Kleen08e60ed2016-09-15 15:24:43 -07001392 printf(" %-50s\n", aliases[j].name);
1393 printf("%*s", 8, "[");
1394 wordwrap(aliases[j].desc, 8, columns, 0);
1395 printf("]\n");
Andi Kleenbf874fc2017-03-20 13:17:11 -07001396 if (details_flag) {
Andi Kleen7f372a62017-03-20 13:17:09 -07001397 printf("%*s%s/%s/ ", 8, "", aliases[j].pmu, aliases[j].str);
Andi Kleen96284812017-03-20 13:17:10 -07001398 if (aliases[j].metric_name)
1399 printf(" MetricName: %s", aliases[j].metric_name);
Andi Kleen7f372a62017-03-20 13:17:09 -07001400 if (aliases[j].metric_expr)
1401 printf(" MetricExpr: %s", aliases[j].metric_expr);
1402 putchar('\n');
1403 }
Andi Kleen08e60ed2016-09-15 15:24:43 -07001404 } else
1405 printf(" %-50s [Kernel PMU event]\n", aliases[j].name);
Andi Kleendc098b32013-04-20 11:02:29 -07001406 printed++;
1407 }
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03001408 if (printed && pager_in_use())
Andi Kleendc098b32013-04-20 11:02:29 -07001409 printf("\n");
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001410out_free:
1411 for (j = 0; j < len; j++)
Andi Kleen08e60ed2016-09-15 15:24:43 -07001412 zfree(&aliases[j].name);
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001413 zfree(&aliases);
1414 return;
1415
1416out_enomem:
1417 printf("FATAL: not enough memory to print PMU events\n");
1418 if (aliases)
1419 goto out_free;
Andi Kleendc098b32013-04-20 11:02:29 -07001420}
Andi Kleen4cabc3d2013-08-21 16:47:26 -07001421
1422bool pmu_have_event(const char *pname, const char *name)
1423{
1424 struct perf_pmu *pmu;
1425 struct perf_pmu_alias *alias;
1426
1427 pmu = NULL;
1428 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1429 if (strcmp(pname, pmu->name))
1430 continue;
1431 list_for_each_entry(alias, &pmu->aliases, list)
1432 if (!strcmp(alias->name, name))
1433 return true;
1434 }
1435 return false;
1436}
Adrian Hunter7d4bdab2014-07-31 09:00:50 +03001437
1438static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
1439{
1440 struct stat st;
1441 char path[PATH_MAX];
1442 const char *sysfs;
1443
1444 sysfs = sysfs__mountpoint();
1445 if (!sysfs)
1446 return NULL;
1447
1448 snprintf(path, PATH_MAX,
1449 "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name);
1450
1451 if (stat(path, &st) < 0)
1452 return NULL;
1453
1454 return fopen(path, "r");
1455}
1456
1457int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
1458 ...)
1459{
1460 va_list args;
1461 FILE *file;
1462 int ret = EOF;
1463
1464 va_start(args, fmt);
1465 file = perf_pmu__open_file(pmu, name);
1466 if (file) {
1467 ret = vfscanf(file, fmt, args);
1468 fclose(file);
1469 }
1470 va_end(args);
1471 return ret;
1472}