blob: c616a06a34a8d80478dc8c87de0d8f008bae3ef5 [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>
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03004#include <linux/string.h>
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -03005#include <linux/zalloc.h>
Arnaldo Carvalho de Melofa0d9842019-08-30 12:52:25 -03006#include <subcmd/pager.h>
Jiri Olsacd82a322012-03-15 20:09:17 +01007#include <sys/types.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03008#include <errno.h>
Arnaldo Carvalho de Meloc23c2a02017-09-11 10:50:26 -03009#include <fcntl.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030010#include <sys/stat.h>
Jiri Olsacd82a322012-03-15 20:09:17 +010011#include <unistd.h>
12#include <stdio.h>
Adrian Hunterdc0a6202014-07-31 09:00:49 +030013#include <stdbool.h>
Adrian Hunter7d4bdab2014-07-31 09:00:50 +030014#include <stdarg.h>
Jiri Olsacd82a322012-03-15 20:09:17 +010015#include <dirent.h>
Borislav Petkovcd0cfad2013-12-09 17:14:24 +010016#include <api/fs/fs.h>
Stephane Eranian410136f2013-11-12 17:58:49 +010017#include <locale.h>
William Cohenfbc28442017-12-04 09:57:28 -050018#include <regex.h>
Jiri Olsa9c3516d2019-07-21 13:24:30 +020019#include <perf/cpumap.h>
Arnaldo Carvalho de Melo5e51b0b2019-08-22 10:48:31 -030020#include "debug.h"
Jiri Olsacd82a322012-03-15 20:09:17 +010021#include "pmu.h"
22#include "parse-events.h"
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -070023#include "header.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030024#include "string2.h"
Arnaldo Carvalho de Melofa0d9842019-08-30 12:52:25 -030025#include "strbuf.h"
Andi Kleend9664582019-11-20 16:15:11 -080026#include "fncache.h"
Jiri Olsacd82a322012-03-15 20:09:17 +010027
Arnaldo Carvalho de Meloab1bf6532013-01-18 17:05:09 -030028struct perf_pmu_format {
29 char *name;
30 int value;
31 DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
32 struct list_head list;
33};
34
Jiri Olsacd82a322012-03-15 20:09:17 +010035int perf_pmu_parse(struct list_head *list, char *name);
36extern FILE *perf_pmu_in;
37
38static LIST_HEAD(pmus);
39
40/*
41 * Parse & process all the sysfs attributes located under
42 * the directory specified in 'dir' parameter.
43 */
Jiri Olsacff7f952012-11-10 01:46:50 +010044int perf_pmu__format_parse(char *dir, struct list_head *head)
Jiri Olsacd82a322012-03-15 20:09:17 +010045{
46 struct dirent *evt_ent;
47 DIR *format_dir;
48 int ret = 0;
49
50 format_dir = opendir(dir);
51 if (!format_dir)
52 return -EINVAL;
53
54 while (!ret && (evt_ent = readdir(format_dir))) {
55 char path[PATH_MAX];
56 char *name = evt_ent->d_name;
57 FILE *file;
58
59 if (!strcmp(name, ".") || !strcmp(name, ".."))
60 continue;
61
62 snprintf(path, PATH_MAX, "%s/%s", dir, name);
63
64 ret = -EINVAL;
65 file = fopen(path, "r");
66 if (!file)
67 break;
68
69 perf_pmu_in = file;
70 ret = perf_pmu_parse(head, name);
71 fclose(file);
72 }
73
74 closedir(format_dir);
75 return ret;
76}
77
78/*
79 * Reading/parsing the default pmu format definition, which should be
80 * located at:
81 * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
82 */
Adrian Hunterb6b96fb2013-07-04 16:20:25 +030083static int pmu_format(const char *name, struct list_head *format)
Jiri Olsacd82a322012-03-15 20:09:17 +010084{
Jiri Olsacd82a322012-03-15 20:09:17 +010085 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
Andi Kleend9664582019-11-20 16:15:11 -080094 if (!file_available(path))
95 return 0;
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
Jin Yaoa55ab7c2019-08-28 13:59:29 +0800103int perf_pmu__convert_scale(const char *scale, char **end, double *sval)
Andi Kleend02fc6b2017-01-03 07:08:23 -0800104{
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
Ben Hutchings11a64a02018-11-11 18:45:24 +0000148 scnprintf(path, PATH_MAX, "%s/%s.scale", dir, name);
Stephane Eranian410136f2013-11-12 17:58:49 +0100149
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
Jin Yaoa55ab7c2019-08-28 13:59:29 +0800166 ret = perf_pmu__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
Ben Hutchings11a64a02018-11-11 18:45:24 +0000178 scnprintf(path, PATH_MAX, "%s/%s.unit", dir, name);
Stephane Eranian410136f2013-11-12 17:58:49 +0100179
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
Ben Hutchings11a64a02018-11-11 18:45:24 +0000208 scnprintf(path, PATH_MAX, "%s/%s.per-pkg", dir, name);
Matt Fleming044330c2014-11-21 10:31:12 +0100209
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
Ben Hutchings11a64a02018-11-11 18:45:24 +0000226 scnprintf(path, PATH_MAX, "%s/%s.snapshot", dir, name);
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100227
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
Thomas Richter6dde6422018-06-15 12:11:05 +0200237static void perf_pmu_assign_str(char *name, const char *field, char **old_str,
238 char **new_str)
239{
240 if (!*old_str)
241 goto set_new;
242
243 if (*new_str) { /* Have new string, check with old */
244 if (strcasecmp(*old_str, *new_str))
245 pr_debug("alias %s differs in field '%s'\n",
246 name, field);
247 zfree(old_str);
248 } else /* Nothing new --> keep old string */
249 return;
250set_new:
251 *old_str = *new_str;
252 *new_str = NULL;
253}
254
255static void perf_pmu_update_alias(struct perf_pmu_alias *old,
256 struct perf_pmu_alias *newalias)
257{
258 perf_pmu_assign_str(old->name, "desc", &old->desc, &newalias->desc);
259 perf_pmu_assign_str(old->name, "long_desc", &old->long_desc,
260 &newalias->long_desc);
261 perf_pmu_assign_str(old->name, "topic", &old->topic, &newalias->topic);
262 perf_pmu_assign_str(old->name, "metric_expr", &old->metric_expr,
263 &newalias->metric_expr);
264 perf_pmu_assign_str(old->name, "metric_name", &old->metric_name,
265 &newalias->metric_name);
266 perf_pmu_assign_str(old->name, "value", &old->str, &newalias->str);
267 old->scale = newalias->scale;
268 old->per_pkg = newalias->per_pkg;
269 old->snapshot = newalias->snapshot;
270 memcpy(old->unit, newalias->unit, sizeof(old->unit));
271}
272
273/* Delete an alias entry. */
274static void perf_pmu_free_alias(struct perf_pmu_alias *newalias)
275{
276 zfree(&newalias->name);
277 zfree(&newalias->desc);
278 zfree(&newalias->long_desc);
279 zfree(&newalias->topic);
280 zfree(&newalias->str);
281 zfree(&newalias->metric_expr);
282 zfree(&newalias->metric_name);
283 parse_events_terms__purge(&newalias->terms);
284 free(newalias);
285}
286
287/* Merge an alias, search in alias list. If this name is already
288 * present merge both of them to combine all information.
289 */
290static bool perf_pmu_merge_alias(struct perf_pmu_alias *newalias,
291 struct list_head *alist)
292{
293 struct perf_pmu_alias *a;
294
295 list_for_each_entry(a, alist, list) {
296 if (!strcasecmp(newalias->name, a->name)) {
297 perf_pmu_update_alias(a, newalias);
298 perf_pmu_free_alias(newalias);
299 return true;
300 }
301 }
302 return false;
303}
304
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700305static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name,
Andi Kleenfedb2b52017-01-27 18:03:37 -0800306 char *desc, char *val,
307 char *long_desc, char *topic,
Andi Kleen00636c32017-03-20 13:17:07 -0700308 char *unit, char *perpkg,
Andi Kleen96284812017-03-20 13:17:10 -0700309 char *metric_expr,
Jin Yaoa7f6c8c2019-10-15 10:53:57 +0800310 char *metric_name,
311 char *deprecated)
Zheng Yana6146d52012-06-15 14:31:41 +0800312{
Thomas Richter0c24d6f2018-06-15 12:11:04 +0200313 struct parse_events_term *term;
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300314 struct perf_pmu_alias *alias;
Zheng Yana6146d52012-06-15 14:31:41 +0800315 int ret;
Andi Kleenfedb2b52017-01-27 18:03:37 -0800316 int num;
Thomas Richter0c24d6f2018-06-15 12:11:04 +0200317 char newval[256];
Zheng Yana6146d52012-06-15 14:31:41 +0800318
Zheng Yana6146d52012-06-15 14:31:41 +0800319 alias = malloc(sizeof(*alias));
320 if (!alias)
321 return -ENOMEM;
322
323 INIT_LIST_HEAD(&alias->terms);
Stephane Eranian410136f2013-11-12 17:58:49 +0100324 alias->scale = 1.0;
325 alias->unit[0] = '\0';
Matt Fleming044330c2014-11-21 10:31:12 +0100326 alias->per_pkg = false;
Stephane Eranian84530922016-01-06 19:50:01 +0100327 alias->snapshot = false;
Jin Yaoa7f6c8c2019-10-15 10:53:57 +0800328 alias->deprecated = false;
Stephane Eranian410136f2013-11-12 17:58:49 +0100329
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700330 ret = parse_events_terms(&alias->terms, val);
Zheng Yana6146d52012-06-15 14:31:41 +0800331 if (ret) {
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700332 pr_err("Cannot parse alias %s: %d\n", val, ret);
Zheng Yana6146d52012-06-15 14:31:41 +0800333 free(alias);
334 return ret;
335 }
336
Thomas Richter0c24d6f2018-06-15 12:11:04 +0200337 /* Scan event and remove leading zeroes, spaces, newlines, some
338 * platforms have terms specified as
339 * event=0x0091 (read from files ../<PMU>/events/<FILE>
340 * and terms specified as event=0x91 (read from JSON files).
341 *
342 * Rebuild string to make alias->str member comparable.
343 */
344 memset(newval, 0, sizeof(newval));
345 ret = 0;
346 list_for_each_entry(term, &alias->terms, list) {
347 if (ret)
348 ret += scnprintf(newval + ret, sizeof(newval) - ret,
349 ",");
350 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM)
351 ret += scnprintf(newval + ret, sizeof(newval) - ret,
352 "%s=%#x", term->config, term->val.num);
353 else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
354 ret += scnprintf(newval + ret, sizeof(newval) - ret,
355 "%s=%s", term->config, term->val.str);
356 }
357
Zheng Yana6146d52012-06-15 14:31:41 +0800358 alias->name = strdup(name);
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700359 if (dir) {
360 /*
361 * load unit name and scale if available
362 */
363 perf_pmu__parse_unit(alias, dir, name);
364 perf_pmu__parse_scale(alias, dir, name);
365 perf_pmu__parse_per_pkg(alias, dir, name);
366 perf_pmu__parse_snapshot(alias, dir, name);
367 }
Stephane Eranian410136f2013-11-12 17:58:49 +0100368
Andi Kleen00636c32017-03-20 13:17:07 -0700369 alias->metric_expr = metric_expr ? strdup(metric_expr) : NULL;
Andi Kleen96284812017-03-20 13:17:10 -0700370 alias->metric_name = metric_name ? strdup(metric_name): NULL;
Andi Kleen08e60ed2016-09-15 15:24:43 -0700371 alias->desc = desc ? strdup(desc) : NULL;
Sukadev Bhattiproluc8d68282016-09-15 15:24:48 -0700372 alias->long_desc = long_desc ? strdup(long_desc) :
373 desc ? strdup(desc) : NULL;
Andi Kleendd5f1032016-09-15 15:24:50 -0700374 alias->topic = topic ? strdup(topic) : NULL;
Andi Kleenfedb2b52017-01-27 18:03:37 -0800375 if (unit) {
Jin Yaoa55ab7c2019-08-28 13:59:29 +0800376 if (perf_pmu__convert_scale(unit, &unit, &alias->scale) < 0)
Andi Kleenfedb2b52017-01-27 18:03:37 -0800377 return -1;
378 snprintf(alias->unit, sizeof(alias->unit), "%s", unit);
379 }
380 alias->per_pkg = perpkg && sscanf(perpkg, "%d", &num) == 1 && num == 1;
Thomas Richter0c24d6f2018-06-15 12:11:04 +0200381 alias->str = strdup(newval);
Andi Kleenf2361022017-01-27 18:03:40 -0800382
Jin Yaoa7f6c8c2019-10-15 10:53:57 +0800383 if (deprecated)
384 alias->deprecated = true;
385
Thomas Richter6dde6422018-06-15 12:11:05 +0200386 if (!perf_pmu_merge_alias(alias, list))
387 list_add_tail(&alias->list, list);
Stephane Eranian410136f2013-11-12 17:58:49 +0100388
Zheng Yana6146d52012-06-15 14:31:41 +0800389 return 0;
390}
391
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700392static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file)
393{
394 char buf[256];
395 int ret;
396
397 ret = fread(buf, 1, sizeof(buf), file);
398 if (ret == 0)
399 return -EINVAL;
400
401 buf[ret] = 0;
402
Thomas Richterea23ac72018-06-15 12:11:03 +0200403 /* Remove trailing newline from sysfs file */
Arnaldo Carvalho de Melo13c230a2019-06-26 12:13:13 -0300404 strim(buf);
Thomas Richterea23ac72018-06-15 12:11:03 +0200405
Andi Kleenfedb2b52017-01-27 18:03:37 -0800406 return __perf_pmu__new_alias(list, dir, name, NULL, buf, NULL, NULL, NULL,
Jin Yaoa7f6c8c2019-10-15 10:53:57 +0800407 NULL, NULL, NULL, NULL);
Sukadev Bhattiprolu70c646e2015-06-10 00:25:08 -0700408}
409
Matt Fleming46441bd2014-09-24 15:04:06 +0100410static inline bool pmu_alias_info_file(char *name)
411{
412 size_t len;
413
414 len = strlen(name);
415 if (len > 5 && !strcmp(name + len - 5, ".unit"))
416 return true;
417 if (len > 6 && !strcmp(name + len - 6, ".scale"))
418 return true;
Matt Fleming044330c2014-11-21 10:31:12 +0100419 if (len > 8 && !strcmp(name + len - 8, ".per-pkg"))
420 return true;
Jiri Olsa1d9e4462014-11-21 10:31:13 +0100421 if (len > 9 && !strcmp(name + len - 9, ".snapshot"))
422 return true;
Matt Fleming46441bd2014-09-24 15:04:06 +0100423
424 return false;
425}
426
Zheng Yana6146d52012-06-15 14:31:41 +0800427/*
428 * Process all the sysfs attributes located under the directory
429 * specified in 'dir' parameter.
430 */
431static int pmu_aliases_parse(char *dir, struct list_head *head)
432{
433 struct dirent *evt_ent;
434 DIR *event_dir;
Zheng Yana6146d52012-06-15 14:31:41 +0800435
436 event_dir = opendir(dir);
437 if (!event_dir)
438 return -EINVAL;
439
Andi Kleen940db6d2016-02-17 14:44:55 -0800440 while ((evt_ent = readdir(event_dir))) {
Zheng Yana6146d52012-06-15 14:31:41 +0800441 char path[PATH_MAX];
442 char *name = evt_ent->d_name;
443 FILE *file;
444
445 if (!strcmp(name, ".") || !strcmp(name, ".."))
446 continue;
447
Stephane Eranian410136f2013-11-12 17:58:49 +0100448 /*
Matt Fleming46441bd2014-09-24 15:04:06 +0100449 * skip info files parsed in perf_pmu__new_alias()
Stephane Eranian410136f2013-11-12 17:58:49 +0100450 */
Matt Fleming46441bd2014-09-24 15:04:06 +0100451 if (pmu_alias_info_file(name))
Stephane Eranian410136f2013-11-12 17:58:49 +0100452 continue;
453
Jiri Olsa77f18152018-03-19 09:29:01 +0100454 scnprintf(path, PATH_MAX, "%s/%s", dir, name);
Zheng Yana6146d52012-06-15 14:31:41 +0800455
Zheng Yana6146d52012-06-15 14:31:41 +0800456 file = fopen(path, "r");
Andi Kleen940db6d2016-02-17 14:44:55 -0800457 if (!file) {
458 pr_debug("Cannot open %s\n", path);
459 continue;
460 }
Stephane Eranian410136f2013-11-12 17:58:49 +0100461
Andi Kleen940db6d2016-02-17 14:44:55 -0800462 if (perf_pmu__new_alias(head, dir, name, file) < 0)
463 pr_debug("Cannot set up %s\n", name);
Zheng Yana6146d52012-06-15 14:31:41 +0800464 fclose(file);
465 }
466
467 closedir(event_dir);
Andi Kleen940db6d2016-02-17 14:44:55 -0800468 return 0;
Zheng Yana6146d52012-06-15 14:31:41 +0800469}
470
471/*
472 * Reading the pmu event aliases definition, which should be located at:
473 * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
474 */
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300475static int pmu_aliases(const char *name, struct list_head *head)
Zheng Yana6146d52012-06-15 14:31:41 +0800476{
Zheng Yana6146d52012-06-15 14:31:41 +0800477 char path[PATH_MAX];
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -0300478 const char *sysfs = sysfs__mountpoint();
Zheng Yana6146d52012-06-15 14:31:41 +0800479
Zheng Yana6146d52012-06-15 14:31:41 +0800480 if (!sysfs)
481 return -1;
482
483 snprintf(path, PATH_MAX,
484 "%s/bus/event_source/devices/%s/events", sysfs, name);
485
Andi Kleend9664582019-11-20 16:15:11 -0800486 if (!file_available(path))
487 return 0;
Zheng Yana6146d52012-06-15 14:31:41 +0800488
489 if (pmu_aliases_parse(path, head))
490 return -1;
491
492 return 0;
493}
494
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300495static int pmu_alias_terms(struct perf_pmu_alias *alias,
Zheng Yana6146d52012-06-15 14:31:41 +0800496 struct list_head *terms)
497{
Jiri Olsa7c2f8162014-04-16 20:49:02 +0200498 struct parse_events_term *term, *cloned;
Zheng Yana6146d52012-06-15 14:31:41 +0800499 LIST_HEAD(list);
500 int ret;
501
502 list_for_each_entry(term, &alias->terms, list) {
Jiri Olsa7c2f8162014-04-16 20:49:02 +0200503 ret = parse_events_term__clone(&cloned, term);
Zheng Yana6146d52012-06-15 14:31:41 +0800504 if (ret) {
Arnaldo Carvalho de Melo682dc242016-02-12 16:48:00 -0300505 parse_events_terms__purge(&list);
Zheng Yana6146d52012-06-15 14:31:41 +0800506 return ret;
507 }
Andi Kleenc2f1cea2017-10-20 13:27:55 -0700508 /*
509 * Weak terms don't override command line options,
510 * which we don't want for implicit terms in aliases.
511 */
512 cloned->weak = true;
Jiri Olsa7c2f8162014-04-16 20:49:02 +0200513 list_add_tail(&cloned->list, &list);
Zheng Yana6146d52012-06-15 14:31:41 +0800514 }
515 list_splice(&list, terms);
516 return 0;
517}
518
Jiri Olsacd82a322012-03-15 20:09:17 +0100519/*
520 * Reading/parsing the default pmu type value, which should be
521 * located at:
522 * /sys/bus/event_source/devices/<dev>/type as sysfs attribute.
523 */
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300524static int pmu_type(const char *name, __u32 *type)
Jiri Olsacd82a322012-03-15 20:09:17 +0100525{
Jiri Olsacd82a322012-03-15 20:09:17 +0100526 char path[PATH_MAX];
Jiri Olsacd82a322012-03-15 20:09:17 +0100527 FILE *file;
528 int ret = 0;
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -0300529 const char *sysfs = sysfs__mountpoint();
Jiri Olsacd82a322012-03-15 20:09:17 +0100530
Jiri Olsacd82a322012-03-15 20:09:17 +0100531 if (!sysfs)
532 return -1;
533
534 snprintf(path, PATH_MAX,
Robert Richter50a96672012-08-16 21:10:24 +0200535 "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name);
Jiri Olsacd82a322012-03-15 20:09:17 +0100536
Andi Kleend9664582019-11-20 16:15:11 -0800537 if (access(path, R_OK) < 0)
Jiri Olsacd82a322012-03-15 20:09:17 +0100538 return -1;
539
540 file = fopen(path, "r");
541 if (!file)
542 return -EINVAL;
543
544 if (1 != fscanf(file, "%u", type))
545 ret = -1;
546
547 fclose(file);
548 return ret;
549}
550
Robert Richter50a96672012-08-16 21:10:24 +0200551/* Add all pmus in sysfs to pmu list: */
552static void pmu_read_sysfs(void)
553{
554 char path[PATH_MAX];
Robert Richter50a96672012-08-16 21:10:24 +0200555 DIR *dir;
556 struct dirent *dent;
Arnaldo Carvalho de Melocf38fad2013-11-05 14:48:50 -0300557 const char *sysfs = sysfs__mountpoint();
Robert Richter50a96672012-08-16 21:10:24 +0200558
Robert Richter50a96672012-08-16 21:10:24 +0200559 if (!sysfs)
560 return;
561
562 snprintf(path, PATH_MAX,
563 "%s" EVENT_SOURCE_DEVICE_PATH, sysfs);
564
565 dir = opendir(path);
566 if (!dir)
567 return;
568
569 while ((dent = readdir(dir))) {
570 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
571 continue;
572 /* add to static LIST_HEAD(pmus): */
573 perf_pmu__find(dent->d_name);
574 }
575
576 closedir(dir);
577}
578
Jiri Olsaf8548392019-07-21 13:23:49 +0200579static struct perf_cpu_map *__pmu_cpumask(const char *path)
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800580{
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800581 FILE *file;
Jiri Olsaf8548392019-07-21 13:23:49 +0200582 struct perf_cpu_map *cpus;
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800583
584 file = fopen(path, "r");
585 if (!file)
586 return NULL;
587
Jiri Olsa9c3516d2019-07-21 13:24:30 +0200588 cpus = perf_cpu_map__read(file);
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800589 fclose(file);
590 return cpus;
591}
592
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700593/*
Mark Rutland66ec11912017-10-06 19:38:22 +0100594 * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64)
595 * may have a "cpus" file.
596 */
597#define CPUS_TEMPLATE_UNCORE "%s/bus/event_source/devices/%s/cpumask"
598#define CPUS_TEMPLATE_CPU "%s/bus/event_source/devices/%s/cpus"
599
Jiri Olsaf8548392019-07-21 13:23:49 +0200600static struct perf_cpu_map *pmu_cpumask(const char *name)
Mark Rutland66ec11912017-10-06 19:38:22 +0100601{
602 char path[PATH_MAX];
Jiri Olsaf8548392019-07-21 13:23:49 +0200603 struct perf_cpu_map *cpus;
Mark Rutland66ec11912017-10-06 19:38:22 +0100604 const char *sysfs = sysfs__mountpoint();
605 const char *templates[] = {
606 CPUS_TEMPLATE_UNCORE,
607 CPUS_TEMPLATE_CPU,
608 NULL
609 };
610 const char **template;
611
612 if (!sysfs)
613 return NULL;
614
615 for (template = templates; *template; template++) {
616 snprintf(path, PATH_MAX, *template, sysfs, name);
617 cpus = __pmu_cpumask(path);
618 if (cpus)
619 return cpus;
620 }
621
622 return NULL;
623}
624
625static bool pmu_is_uncore(const char *name)
626{
627 char path[PATH_MAX];
Andi Kleend9664582019-11-20 16:15:11 -0800628 const char *sysfs;
Mark Rutland66ec11912017-10-06 19:38:22 +0100629
Andi Kleend9664582019-11-20 16:15:11 -0800630 sysfs = sysfs__mountpoint();
Mark Rutland66ec11912017-10-06 19:38:22 +0100631 snprintf(path, PATH_MAX, CPUS_TEMPLATE_UNCORE, sysfs, name);
Andi Kleend9664582019-11-20 16:15:11 -0800632 return file_available(path);
Mark Rutland66ec11912017-10-06 19:38:22 +0100633}
634
635/*
Ganapatrao Kulkarni14b22ae2017-08-24 16:30:58 +0530636 * PMU CORE devices have different name other than cpu in sysfs on some
Kan Liang292c34c2018-04-24 11:20:10 -0700637 * platforms.
638 * Looking for possible sysfs files to identify the arm core device.
Ganapatrao Kulkarni14b22ae2017-08-24 16:30:58 +0530639 */
Kan Liang292c34c2018-04-24 11:20:10 -0700640static int is_arm_pmu_core(const char *name)
Ganapatrao Kulkarni14b22ae2017-08-24 16:30:58 +0530641{
Ganapatrao Kulkarni14b22ae2017-08-24 16:30:58 +0530642 char path[PATH_MAX];
643 const char *sysfs = sysfs__mountpoint();
644
645 if (!sysfs)
646 return 0;
647
Ganapatrao Kulkarni14b22ae2017-08-24 16:30:58 +0530648 /* Look for cpu sysfs (specific to arm) */
649 scnprintf(path, PATH_MAX, "%s/bus/event_source/devices/%s/cpus",
650 sysfs, name);
Andi Kleend9664582019-11-20 16:15:11 -0800651 return file_available(path);
Ganapatrao Kulkarni14b22ae2017-08-24 16:30:58 +0530652}
653
Ganapatrao Kulkarni54e32dc2017-10-17 00:02:18 +0530654static char *perf_pmu__getcpuid(struct perf_pmu *pmu)
Andi Kleend77ade92017-08-31 12:40:30 -0700655{
656 char *cpuid;
657 static bool printed;
658
659 cpuid = getenv("PERF_CPUID");
660 if (cpuid)
661 cpuid = strdup(cpuid);
662 if (!cpuid)
Ganapatrao Kulkarni54e32dc2017-10-17 00:02:18 +0530663 cpuid = get_cpuid_str(pmu);
Andi Kleend77ade92017-08-31 12:40:30 -0700664 if (!cpuid)
665 return NULL;
666
667 if (!printed) {
668 pr_debug("Using CPUID %s\n", cpuid);
669 printed = true;
670 }
671 return cpuid;
672}
673
Ganapatrao Kulkarni54e32dc2017-10-17 00:02:18 +0530674struct pmu_events_map *perf_pmu__find_map(struct perf_pmu *pmu)
Andi Kleend77ade92017-08-31 12:40:30 -0700675{
676 struct pmu_events_map *map;
Ganapatrao Kulkarni54e32dc2017-10-17 00:02:18 +0530677 char *cpuid = perf_pmu__getcpuid(pmu);
Andi Kleend77ade92017-08-31 12:40:30 -0700678 int i;
679
Ganapatrao Kulkarnide3d0f12017-10-17 00:02:22 +0530680 /* on some platforms which uses cpus map, cpuid can be NULL for
681 * PMUs other than CORE PMUs.
682 */
683 if (!cpuid)
684 return NULL;
685
Andi Kleend77ade92017-08-31 12:40:30 -0700686 i = 0;
687 for (;;) {
688 map = &pmu_events_map[i++];
689 if (!map->table) {
690 map = NULL;
691 break;
692 }
693
Thomas Richter4cb7d3e2018-02-13 16:14:18 +0100694 if (!strcmp_cpuid_str(map->cpuid, cpuid))
Andi Kleend77ade92017-08-31 12:40:30 -0700695 break;
696 }
697 free(cpuid);
698 return map;
699}
700
John Garry730670b12019-06-28 22:35:49 +0800701static bool pmu_uncore_alias_match(const char *pmu_name, const char *name)
702{
703 char *tmp = NULL, *tok, *str;
704 bool res;
705
706 str = strdup(pmu_name);
707 if (!str)
708 return false;
709
710 /*
711 * uncore alias may be from different PMU with common prefix
712 */
713 tok = strtok_r(str, ",", &tmp);
714 if (strncmp(pmu_name, tok, strlen(tok))) {
715 res = false;
716 goto out;
717 }
718
719 /*
720 * Match more complex aliases where the alias name is a comma-delimited
721 * list of tokens, orderly contained in the matching PMU name.
722 *
723 * Example: For alias "socket,pmuname" and PMU "socketX_pmunameY", we
724 * match "socket" in "socketX_pmunameY" and then "pmuname" in
725 * "pmunameY".
726 */
727 for (; tok; name += strlen(tok), tok = strtok_r(NULL, ",", &tmp)) {
728 name = strstr(name, tok);
729 if (!name) {
730 res = false;
731 goto out;
732 }
733 }
734
735 res = true;
736out:
737 free(str);
738 return res;
739}
740
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700741/*
742 * From the pmu_events_map, find the table of PMU events that corresponds
743 * to the current running CPU. Then, add all PMU events from that table
744 * as aliases.
745 */
John Garrye45ad702020-03-17 19:02:15 +0800746void pmu_add_cpu_aliases_map(struct list_head *head, struct perf_pmu *pmu,
747 struct pmu_events_map *map)
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700748{
749 int i;
Ganapatrao Kulkarni54e32dc2017-10-17 00:02:18 +0530750 const char *name = pmu->name;
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700751 /*
752 * Found a matching PMU events table. Create aliases
753 */
754 i = 0;
755 while (1) {
John Garry599ee182019-06-14 22:07:59 +0800756 const char *cpu_name = is_arm_pmu_core(name) ? name : "cpu";
757 struct pmu_event *pe = &map->table[i++];
758 const char *pname = pe->pmu ? pe->pmu : cpu_name;
Andi Kleenfedb2b52017-01-27 18:03:37 -0800759
Andi Kleenb18f3e32017-08-31 12:40:31 -0700760 if (!pe->name) {
761 if (pe->metric_group || pe->metric_name)
762 continue;
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700763 break;
Andi Kleenb18f3e32017-08-31 12:40:31 -0700764 }
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700765
John Garry599ee182019-06-14 22:07:59 +0800766 if (pmu_is_uncore(name) &&
John Garry730670b12019-06-28 22:35:49 +0800767 pmu_uncore_alias_match(pname, name))
John Garry599ee182019-06-14 22:07:59 +0800768 goto new_alias;
Kan Liange94d6b72019-03-15 11:00:14 -0700769
John Garry599ee182019-06-14 22:07:59 +0800770 if (strcmp(pname, name))
771 continue;
Andi Kleenfedb2b52017-01-27 18:03:37 -0800772
Kan Liange94d6b72019-03-15 11:00:14 -0700773new_alias:
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700774 /* need type casts to override 'const' */
775 __perf_pmu__new_alias(head, NULL, (char *)pe->name,
Sukadev Bhattiproluc8d68282016-09-15 15:24:48 -0700776 (char *)pe->desc, (char *)pe->event,
Andi Kleenfedb2b52017-01-27 18:03:37 -0800777 (char *)pe->long_desc, (char *)pe->topic,
Andi Kleen00636c32017-03-20 13:17:07 -0700778 (char *)pe->unit, (char *)pe->perpkg,
Andi Kleen96284812017-03-20 13:17:10 -0700779 (char *)pe->metric_expr,
Jin Yaoa7f6c8c2019-10-15 10:53:57 +0800780 (char *)pe->metric_name,
781 (char *)pe->deprecated);
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700782 }
Sukadev Bhattiprolu933f82f2016-09-15 15:24:40 -0700783}
784
John Garrye45ad702020-03-17 19:02:15 +0800785static void pmu_add_cpu_aliases(struct list_head *head, struct perf_pmu *pmu)
786{
787 struct pmu_events_map *map;
788
789 map = perf_pmu__find_map(pmu);
790 if (!map)
791 return;
792
793 pmu_add_cpu_aliases_map(head, pmu, map);
794}
795
Sukadev Bhattiproluc5de47f2015-06-10 00:25:07 -0700796struct perf_event_attr * __weak
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300797perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
798{
799 return NULL;
800}
801
Jiri Olsa90a86bd2019-03-05 16:25:32 +0100802static int pmu_max_precise(const char *name)
803{
804 char path[PATH_MAX];
805 int max_precise = -1;
806
807 scnprintf(path, PATH_MAX,
808 "bus/event_source/devices/%s/caps/max_precise",
809 name);
810
811 sysfs__read_int(path, &max_precise);
812 return max_precise;
813}
814
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300815static struct perf_pmu *pmu_lookup(const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +0100816{
817 struct perf_pmu *pmu;
818 LIST_HEAD(format);
Zheng Yana6146d52012-06-15 14:31:41 +0800819 LIST_HEAD(aliases);
Jiri Olsacd82a322012-03-15 20:09:17 +0100820 __u32 type;
821
822 /*
823 * The pmu data we store & need consists of the pmu
824 * type value and format definitions. Load both right
825 * now.
826 */
827 if (pmu_format(name, &format))
828 return NULL;
829
Andi Kleen15b22ed2017-01-27 18:03:38 -0800830 /*
831 * Check the type first to avoid unnecessary work.
832 */
833 if (pmu_type(name, &type))
834 return NULL;
835
Jiri Olsa3fded962012-10-10 14:53:16 +0200836 if (pmu_aliases(name, &aliases))
837 return NULL;
838
Jiri Olsacd82a322012-03-15 20:09:17 +0100839 pmu = zalloc(sizeof(*pmu));
840 if (!pmu)
841 return NULL;
842
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800843 pmu->cpus = pmu_cpumask(name);
Ganapatrao Kulkarni54e32dc2017-10-17 00:02:18 +0530844 pmu->name = strdup(name);
845 pmu->type = type;
Mark Rutland66ec11912017-10-06 19:38:22 +0100846 pmu->is_uncore = pmu_is_uncore(name);
Jiri Olsa90a86bd2019-03-05 16:25:32 +0100847 pmu->max_precise = pmu_max_precise(name);
Ganapatrao Kulkarni54e32dc2017-10-17 00:02:18 +0530848 pmu_add_cpu_aliases(&aliases, pmu);
Mark Rutland66ec11912017-10-06 19:38:22 +0100849
Jiri Olsacd82a322012-03-15 20:09:17 +0100850 INIT_LIST_HEAD(&pmu->format);
Zheng Yana6146d52012-06-15 14:31:41 +0800851 INIT_LIST_HEAD(&pmu->aliases);
Jiri Olsacd82a322012-03-15 20:09:17 +0100852 list_splice(&format, &pmu->format);
Zheng Yana6146d52012-06-15 14:31:41 +0800853 list_splice(&aliases, &pmu->aliases);
Robert Richter9bc8f9f2012-06-14 22:38:37 +0200854 list_add_tail(&pmu->list, &pmus);
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300855
856 pmu->default_config = perf_pmu__get_default_config(pmu);
857
Jiri Olsacd82a322012-03-15 20:09:17 +0100858 return pmu;
859}
860
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300861static struct perf_pmu *pmu_find(const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +0100862{
863 struct perf_pmu *pmu;
864
865 list_for_each_entry(pmu, &pmus, list)
866 if (!strcmp(pmu->name, name))
867 return pmu;
868
869 return NULL;
870}
871
Robert Richter50a96672012-08-16 21:10:24 +0200872struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
873{
874 /*
875 * pmu iterator: If pmu is NULL, we start at the begin,
876 * otherwise return the next pmu. Returns NULL on end.
877 */
878 if (!pmu) {
879 pmu_read_sysfs();
880 pmu = list_prepare_entry(pmu, &pmus, list);
881 }
882 list_for_each_entry_continue(pmu, &pmus, list)
883 return pmu;
884 return NULL;
885}
886
Adrian Hunterb6b96fb2013-07-04 16:20:25 +0300887struct perf_pmu *perf_pmu__find(const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +0100888{
889 struct perf_pmu *pmu;
890
891 /*
892 * Once PMU is loaded it stays in the list,
893 * so we keep us from multiple reading/parsing
894 * the pmu format definitions.
895 */
896 pmu = pmu_find(name);
897 if (pmu)
898 return pmu;
899
900 return pmu_lookup(name);
901}
902
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300903static struct perf_pmu_format *
Adrian Hunter09ff6072015-07-17 19:33:49 +0300904pmu_find_format(struct list_head *formats, const char *name)
Jiri Olsacd82a322012-03-15 20:09:17 +0100905{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -0300906 struct perf_pmu_format *format;
Jiri Olsacd82a322012-03-15 20:09:17 +0100907
908 list_for_each_entry(format, formats, list)
909 if (!strcmp(format->name, name))
910 return format;
911
912 return NULL;
913}
914
Adrian Hunter09ff6072015-07-17 19:33:49 +0300915__u64 perf_pmu__format_bits(struct list_head *formats, const char *name)
916{
917 struct perf_pmu_format *format = pmu_find_format(formats, name);
918 __u64 bits = 0;
919 int fbit;
920
921 if (!format)
922 return 0;
923
924 for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
925 bits |= 1ULL << fbit;
926
927 return bits;
928}
929
Adrian Huntera1ac7de2019-11-15 14:42:22 +0200930int perf_pmu__format_type(struct list_head *formats, const char *name)
931{
932 struct perf_pmu_format *format = pmu_find_format(formats, name);
933
934 if (!format)
935 return -1;
936
937 return format->value;
938}
939
Jiri Olsacd82a322012-03-15 20:09:17 +0100940/*
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300941 * Sets value based on the format definition (format parameter)
Jiri Olsacd82a322012-03-15 20:09:17 +0100942 * and unformated value (value parameter).
Jiri Olsacd82a322012-03-15 20:09:17 +0100943 */
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300944static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
945 bool zero)
Jiri Olsacd82a322012-03-15 20:09:17 +0100946{
947 unsigned long fbit, vbit;
Jiri Olsacd82a322012-03-15 20:09:17 +0100948
949 for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
950
951 if (!test_bit(fbit, format))
952 continue;
953
Adrian Hunterdc0a6202014-07-31 09:00:49 +0300954 if (value & (1llu << vbit++))
955 *v |= (1llu << fbit);
956 else if (zero)
957 *v &= ~(1llu << fbit);
Jiri Olsacd82a322012-03-15 20:09:17 +0100958 }
Jiri Olsacd82a322012-03-15 20:09:17 +0100959}
960
Adrian Hunter0efe6b62015-07-17 19:33:50 +0300961static __u64 pmu_format_max_value(const unsigned long *format)
962{
Jiri Olsa1b9caa12018-10-03 09:20:46 +0200963 int w;
Adrian Hunter0efe6b62015-07-17 19:33:50 +0300964
Jiri Olsa1b9caa12018-10-03 09:20:46 +0200965 w = bitmap_weight(format, PERF_PMU_FORMAT_BITS);
966 if (!w)
967 return 0;
968 if (w < 64)
969 return (1ULL << w) - 1;
970 return -1;
Adrian Hunter0efe6b62015-07-17 19:33:50 +0300971}
972
Jiri Olsacd82a322012-03-15 20:09:17 +0100973/*
Cody P Schafer688d4df2015-01-07 17:13:50 -0800974 * Term is a string term, and might be a param-term. Try to look up it's value
975 * in the remaining terms.
976 * - We have a term like "base-or-format-term=param-term",
977 * - We need to find the value supplied for "param-term" (with param-term named
978 * in a config string) later on in the term list.
979 */
980static int pmu_resolve_param_term(struct parse_events_term *term,
981 struct list_head *head_terms,
982 __u64 *value)
983{
984 struct parse_events_term *t;
985
986 list_for_each_entry(t, head_terms, list) {
987 if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
988 if (!strcmp(t->config, term->config)) {
989 t->used = true;
990 *value = t->val.num;
991 return 0;
992 }
993 }
994 }
995
Namhyung Kimbb963e12017-02-17 17:17:38 +0900996 if (verbose > 0)
Cody P Schafer688d4df2015-01-07 17:13:50 -0800997 printf("Required parameter '%s' not specified\n", term->config);
998
999 return -1;
1000}
1001
He Kuangffeb8832015-09-28 03:52:14 +00001002static char *pmu_formats_string(struct list_head *formats)
Jiri Olsae64b0202015-04-22 21:10:21 +02001003{
1004 struct perf_pmu_format *format;
Masami Hiramatsu11db4e22016-05-10 14:47:44 +09001005 char *str = NULL;
1006 struct strbuf buf = STRBUF_INIT;
Jiri Olsae64b0202015-04-22 21:10:21 +02001007 unsigned i = 0;
1008
He Kuangffeb8832015-09-28 03:52:14 +00001009 if (!formats)
Jiri Olsae64b0202015-04-22 21:10:21 +02001010 return NULL;
1011
1012 /* sysfs exported terms */
He Kuangffeb8832015-09-28 03:52:14 +00001013 list_for_each_entry(format, formats, list)
Masami Hiramatsu11db4e22016-05-10 14:47:44 +09001014 if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
1015 goto error;
Jiri Olsae64b0202015-04-22 21:10:21 +02001016
He Kuangffeb8832015-09-28 03:52:14 +00001017 str = strbuf_detach(&buf, NULL);
Masami Hiramatsu11db4e22016-05-10 14:47:44 +09001018error:
He Kuangffeb8832015-09-28 03:52:14 +00001019 strbuf_release(&buf);
Jiri Olsae64b0202015-04-22 21:10:21 +02001020
Jiri Olsae64b0202015-04-22 21:10:21 +02001021 return str;
Jiri Olsae64b0202015-04-22 21:10:21 +02001022}
1023
Cody P Schafer688d4df2015-01-07 17:13:50 -08001024/*
Jiri Olsacd82a322012-03-15 20:09:17 +01001025 * Setup one of config[12] attr members based on the
Cody P Schafer88aca8d2014-01-08 08:43:51 -08001026 * user input data - term parameter.
Jiri Olsacd82a322012-03-15 20:09:17 +01001027 */
1028static int pmu_config_term(struct list_head *formats,
1029 struct perf_event_attr *attr,
Adrian Hunterdc0a6202014-07-31 09:00:49 +03001030 struct parse_events_term *term,
Cody P Schafer688d4df2015-01-07 17:13:50 -08001031 struct list_head *head_terms,
Jiri Olsae64b0202015-04-22 21:10:21 +02001032 bool zero, struct parse_events_error *err)
Jiri Olsacd82a322012-03-15 20:09:17 +01001033{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -03001034 struct perf_pmu_format *format;
Jiri Olsacd82a322012-03-15 20:09:17 +01001035 __u64 *vp;
Adrian Hunter0efe6b62015-07-17 19:33:50 +03001036 __u64 val, max_val;
Jiri Olsacd82a322012-03-15 20:09:17 +01001037
1038 /*
Cody P Schafer688d4df2015-01-07 17:13:50 -08001039 * If this is a parameter we've already used for parameterized-eval,
1040 * skip it in normal eval.
1041 */
1042 if (term->used)
1043 return 0;
1044
1045 /*
Jiri Olsacd82a322012-03-15 20:09:17 +01001046 * Hardcoded terms should be already in, so nothing
1047 * to be done for them.
1048 */
1049 if (parse_events__is_hardcoded_term(term))
1050 return 0;
1051
Jiri Olsacd82a322012-03-15 20:09:17 +01001052 format = pmu_find_format(formats, term->config);
Cody P Schafer688d4df2015-01-07 17:13:50 -08001053 if (!format) {
Namhyung Kimbb963e12017-02-17 17:17:38 +09001054 if (verbose > 0)
Cody P Schafer688d4df2015-01-07 17:13:50 -08001055 printf("Invalid event/parameter '%s'\n", term->config);
Jiri Olsae64b0202015-04-22 21:10:21 +02001056 if (err) {
He Kuangffeb8832015-09-28 03:52:14 +00001057 char *pmu_term = pmu_formats_string(formats);
1058
Ian Rogers448d7322019-10-30 15:34:39 -07001059 parse_events__handle_error(err, term->err_term,
1060 strdup("unknown term"),
1061 parse_events_formats_error_string(pmu_term));
He Kuangffeb8832015-09-28 03:52:14 +00001062 free(pmu_term);
Jiri Olsae64b0202015-04-22 21:10:21 +02001063 }
Jiri Olsacd82a322012-03-15 20:09:17 +01001064 return -EINVAL;
Cody P Schafer688d4df2015-01-07 17:13:50 -08001065 }
Jiri Olsacd82a322012-03-15 20:09:17 +01001066
1067 switch (format->value) {
1068 case PERF_PMU_FORMAT_VALUE_CONFIG:
1069 vp = &attr->config;
1070 break;
1071 case PERF_PMU_FORMAT_VALUE_CONFIG1:
1072 vp = &attr->config1;
1073 break;
1074 case PERF_PMU_FORMAT_VALUE_CONFIG2:
1075 vp = &attr->config2;
1076 break;
1077 default:
1078 return -EINVAL;
1079 }
1080
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001081 /*
Cody P Schafer688d4df2015-01-07 17:13:50 -08001082 * Either directly use a numeric term, or try to translate string terms
1083 * using event parameters.
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001084 */
Jiri Olsa99e71382017-02-17 15:00:56 +01001085 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1086 if (term->no_value &&
1087 bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) {
1088 if (err) {
Ian Rogers448d7322019-10-30 15:34:39 -07001089 parse_events__handle_error(err, term->err_val,
1090 strdup("no value assigned for term"),
1091 NULL);
Jiri Olsa99e71382017-02-17 15:00:56 +01001092 }
1093 return -EINVAL;
1094 }
1095
Cody P Schafer688d4df2015-01-07 17:13:50 -08001096 val = term->val.num;
Jiri Olsa99e71382017-02-17 15:00:56 +01001097 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
Cody P Schafer688d4df2015-01-07 17:13:50 -08001098 if (strcmp(term->val.str, "?")) {
Namhyung Kimbb963e12017-02-17 17:17:38 +09001099 if (verbose > 0) {
Cody P Schafer688d4df2015-01-07 17:13:50 -08001100 pr_info("Invalid sysfs entry %s=%s\n",
1101 term->config, term->val.str);
Jiri Olsae64b0202015-04-22 21:10:21 +02001102 }
1103 if (err) {
Ian Rogers448d7322019-10-30 15:34:39 -07001104 parse_events__handle_error(err, term->err_val,
1105 strdup("expected numeric value"),
1106 NULL);
Jiri Olsae64b0202015-04-22 21:10:21 +02001107 }
Cody P Schafer688d4df2015-01-07 17:13:50 -08001108 return -EINVAL;
1109 }
1110
1111 if (pmu_resolve_param_term(term, head_terms, &val))
1112 return -EINVAL;
1113 } else
1114 return -EINVAL;
1115
Adrian Hunter0efe6b62015-07-17 19:33:50 +03001116 max_val = pmu_format_max_value(format->bits);
1117 if (val > max_val) {
1118 if (err) {
Ian Rogers448d7322019-10-30 15:34:39 -07001119 char *err_str;
1120
1121 parse_events__handle_error(err, term->err_val,
1122 asprintf(&err_str,
1123 "value too big for format, maximum is %llu",
1124 (unsigned long long)max_val) < 0
1125 ? strdup("value too big for format")
1126 : err_str,
1127 NULL);
Adrian Hunter0efe6b62015-07-17 19:33:50 +03001128 return -EINVAL;
1129 }
1130 /*
1131 * Assume we don't care if !err, in which case the value will be
1132 * silently truncated.
1133 */
1134 }
1135
Cody P Schafer688d4df2015-01-07 17:13:50 -08001136 pmu_format_value(format->bits, val, vp, zero);
Jiri Olsacd82a322012-03-15 20:09:17 +01001137 return 0;
1138}
1139
Jiri Olsacff7f952012-11-10 01:46:50 +01001140int perf_pmu__config_terms(struct list_head *formats,
1141 struct perf_event_attr *attr,
Adrian Hunterdc0a6202014-07-31 09:00:49 +03001142 struct list_head *head_terms,
Jiri Olsae64b0202015-04-22 21:10:21 +02001143 bool zero, struct parse_events_error *err)
Jiri Olsacd82a322012-03-15 20:09:17 +01001144{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001145 struct parse_events_term *term;
Jiri Olsacd82a322012-03-15 20:09:17 +01001146
Cody P Schafer688d4df2015-01-07 17:13:50 -08001147 list_for_each_entry(term, head_terms, list) {
Jiri Olsae64b0202015-04-22 21:10:21 +02001148 if (pmu_config_term(formats, attr, term, head_terms,
1149 zero, err))
Jiri Olsacd82a322012-03-15 20:09:17 +01001150 return -EINVAL;
Cody P Schafer688d4df2015-01-07 17:13:50 -08001151 }
Jiri Olsacd82a322012-03-15 20:09:17 +01001152
1153 return 0;
1154}
1155
1156/*
1157 * Configures event's 'attr' parameter based on the:
1158 * 1) users input - specified in terms parameter
1159 * 2) pmu format definitions - specified by pmu parameter
1160 */
1161int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
Jiri Olsae64b0202015-04-22 21:10:21 +02001162 struct list_head *head_terms,
1163 struct parse_events_error *err)
Jiri Olsacd82a322012-03-15 20:09:17 +01001164{
Adrian Hunterdc0a6202014-07-31 09:00:49 +03001165 bool zero = !!pmu->default_config;
1166
Jiri Olsacd82a322012-03-15 20:09:17 +01001167 attr->type = pmu->type;
Jiri Olsae64b0202015-04-22 21:10:21 +02001168 return perf_pmu__config_terms(&pmu->format, attr, head_terms,
1169 zero, err);
Jiri Olsacd82a322012-03-15 20:09:17 +01001170}
1171
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -03001172static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
1173 struct parse_events_term *term)
Zheng Yana6146d52012-06-15 14:31:41 +08001174{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -03001175 struct perf_pmu_alias *alias;
Zheng Yana6146d52012-06-15 14:31:41 +08001176 char *name;
1177
1178 if (parse_events__is_hardcoded_term(term))
1179 return NULL;
1180
1181 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1182 if (term->val.num != 1)
1183 return NULL;
1184 if (pmu_find_format(&pmu->format, term->config))
1185 return NULL;
1186 name = term->config;
1187 } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1188 if (strcasecmp(term->config, "event"))
1189 return NULL;
1190 name = term->val.str;
1191 } else {
1192 return NULL;
1193 }
1194
1195 list_for_each_entry(alias, &pmu->aliases, list) {
1196 if (!strcasecmp(alias->name, name))
1197 return alias;
1198 }
1199 return NULL;
1200}
1201
Stephane Eranian410136f2013-11-12 17:58:49 +01001202
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001203static int check_info_data(struct perf_pmu_alias *alias,
1204 struct perf_pmu_info *info)
Stephane Eranian410136f2013-11-12 17:58:49 +01001205{
1206 /*
1207 * Only one term in event definition can
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001208 * define unit, scale and snapshot, fail
1209 * if there's more than one.
Stephane Eranian410136f2013-11-12 17:58:49 +01001210 */
Arnaldo Carvalho de Melob30a7d12017-02-15 10:06:20 -03001211 if ((info->unit && alias->unit[0]) ||
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001212 (info->scale && alias->scale) ||
1213 (info->snapshot && alias->snapshot))
Stephane Eranian410136f2013-11-12 17:58:49 +01001214 return -EINVAL;
1215
Arnaldo Carvalho de Melob30a7d12017-02-15 10:06:20 -03001216 if (alias->unit[0])
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001217 info->unit = alias->unit;
Stephane Eranian410136f2013-11-12 17:58:49 +01001218
1219 if (alias->scale)
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001220 info->scale = alias->scale;
1221
1222 if (alias->snapshot)
1223 info->snapshot = alias->snapshot;
Stephane Eranian410136f2013-11-12 17:58:49 +01001224
1225 return 0;
1226}
1227
Zheng Yana6146d52012-06-15 14:31:41 +08001228/*
1229 * Find alias in the terms list and replace it with the terms
1230 * defined for the alias
1231 */
Stephane Eranian410136f2013-11-12 17:58:49 +01001232int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
Matt Fleming46441bd2014-09-24 15:04:06 +01001233 struct perf_pmu_info *info)
Zheng Yana6146d52012-06-15 14:31:41 +08001234{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001235 struct parse_events_term *term, *h;
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -03001236 struct perf_pmu_alias *alias;
Zheng Yana6146d52012-06-15 14:31:41 +08001237 int ret;
1238
Matt Fleming044330c2014-11-21 10:31:12 +01001239 info->per_pkg = false;
1240
Stephane Eranian8a398892014-01-17 16:34:05 +01001241 /*
1242 * Mark unit and scale as not set
1243 * (different from default values, see below)
1244 */
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001245 info->unit = NULL;
1246 info->scale = 0.0;
1247 info->snapshot = false;
Andi Kleen37932c12017-03-20 13:17:08 -07001248 info->metric_expr = NULL;
Andi Kleen96284812017-03-20 13:17:10 -07001249 info->metric_name = NULL;
Stephane Eranian410136f2013-11-12 17:58:49 +01001250
Zheng Yana6146d52012-06-15 14:31:41 +08001251 list_for_each_entry_safe(term, h, head_terms, list) {
1252 alias = pmu_find_alias(pmu, term);
1253 if (!alias)
1254 continue;
1255 ret = pmu_alias_terms(alias, &term->list);
1256 if (ret)
1257 return ret;
Stephane Eranian410136f2013-11-12 17:58:49 +01001258
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001259 ret = check_info_data(alias, info);
Stephane Eranian410136f2013-11-12 17:58:49 +01001260 if (ret)
1261 return ret;
1262
Matt Fleming044330c2014-11-21 10:31:12 +01001263 if (alias->per_pkg)
1264 info->per_pkg = true;
Andi Kleen37932c12017-03-20 13:17:08 -07001265 info->metric_expr = alias->metric_expr;
Andi Kleen96284812017-03-20 13:17:10 -07001266 info->metric_name = alias->metric_name;
Matt Fleming044330c2014-11-21 10:31:12 +01001267
Arnaldo Carvalho de Meloe56fbc92019-07-04 12:13:46 -03001268 list_del_init(&term->list);
Ian Rogers1dc92552019-10-30 15:34:47 -07001269 parse_events_term__delete(term);
Zheng Yana6146d52012-06-15 14:31:41 +08001270 }
Stephane Eranian8a398892014-01-17 16:34:05 +01001271
1272 /*
1273 * if no unit or scale foundin aliases, then
1274 * set defaults as for evsel
1275 * unit cannot left to NULL
1276 */
Matt Fleming46441bd2014-09-24 15:04:06 +01001277 if (info->unit == NULL)
1278 info->unit = "";
Stephane Eranian8a398892014-01-17 16:34:05 +01001279
Matt Fleming46441bd2014-09-24 15:04:06 +01001280 if (info->scale == 0.0)
1281 info->scale = 1.0;
Stephane Eranian8a398892014-01-17 16:34:05 +01001282
Zheng Yana6146d52012-06-15 14:31:41 +08001283 return 0;
1284}
1285
Jiri Olsacd82a322012-03-15 20:09:17 +01001286int perf_pmu__new_format(struct list_head *list, char *name,
1287 int config, unsigned long *bits)
1288{
Arnaldo Carvalho de Melo5c6ccc32013-01-18 16:54:00 -03001289 struct perf_pmu_format *format;
Jiri Olsacd82a322012-03-15 20:09:17 +01001290
1291 format = zalloc(sizeof(*format));
1292 if (!format)
1293 return -ENOMEM;
1294
1295 format->name = strdup(name);
1296 format->value = config;
1297 memcpy(format->bits, bits, sizeof(format->bits));
1298
1299 list_add_tail(&format->list, list);
1300 return 0;
1301}
1302
1303void perf_pmu__set_format(unsigned long *bits, long from, long to)
1304{
1305 long b;
1306
1307 if (!to)
1308 to = from;
1309
Sukadev Bhattiprolu15268132013-01-17 09:11:30 -08001310 memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
Jiri Olsacd82a322012-03-15 20:09:17 +01001311 for (b = from; b <= to; b++)
1312 set_bit(b, bits);
1313}
Andi Kleendc098b32013-04-20 11:02:29 -07001314
Cody P Schaferaaea3612015-01-07 17:13:51 -08001315static int sub_non_neg(int a, int b)
1316{
1317 if (b > a)
1318 return 0;
1319 return a - b;
1320}
1321
Andi Kleendc098b32013-04-20 11:02:29 -07001322static char *format_alias(char *buf, int len, struct perf_pmu *pmu,
1323 struct perf_pmu_alias *alias)
1324{
Cody P Schaferaaea3612015-01-07 17:13:51 -08001325 struct parse_events_term *term;
1326 int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name);
1327
1328 list_for_each_entry(term, &alias->terms, list) {
1329 if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
1330 used += snprintf(buf + used, sub_non_neg(len, used),
1331 ",%s=%s", term->config,
1332 term->val.str);
1333 }
1334
1335 if (sub_non_neg(len, used) > 0) {
1336 buf[used] = '/';
1337 used++;
1338 }
1339 if (sub_non_neg(len, used) > 0) {
1340 buf[used] = '\0';
1341 used++;
1342 } else
1343 buf[len - 1] = '\0';
1344
Andi Kleendc098b32013-04-20 11:02:29 -07001345 return buf;
1346}
1347
1348static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu,
1349 struct perf_pmu_alias *alias)
1350{
1351 snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name);
1352 return buf;
1353}
1354
Andi Kleendd5f1032016-09-15 15:24:50 -07001355struct sevent {
Andi Kleen08e60ed2016-09-15 15:24:43 -07001356 char *name;
1357 char *desc;
Andi Kleendd5f1032016-09-15 15:24:50 -07001358 char *topic;
Andi Kleenf2361022017-01-27 18:03:40 -08001359 char *str;
1360 char *pmu;
Andi Kleen7f372a62017-03-20 13:17:09 -07001361 char *metric_expr;
Andi Kleen96284812017-03-20 13:17:10 -07001362 char *metric_name;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001363};
1364
Andi Kleendd5f1032016-09-15 15:24:50 -07001365static int cmp_sevent(const void *a, const void *b)
Andi Kleendc098b32013-04-20 11:02:29 -07001366{
Andi Kleendd5f1032016-09-15 15:24:50 -07001367 const struct sevent *as = a;
1368 const struct sevent *bs = b;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001369
1370 /* Put extra events last */
1371 if (!!as->desc != !!bs->desc)
1372 return !!as->desc - !!bs->desc;
Andi Kleendd5f1032016-09-15 15:24:50 -07001373 if (as->topic && bs->topic) {
1374 int n = strcmp(as->topic, bs->topic);
1375
1376 if (n)
1377 return n;
1378 }
Andi Kleen08e60ed2016-09-15 15:24:43 -07001379 return strcmp(as->name, bs->name);
1380}
1381
1382static void wordwrap(char *s, int start, int max, int corr)
1383{
1384 int column = start;
1385 int n;
1386
1387 while (*s) {
1388 int wlen = strcspn(s, " \t");
1389
1390 if (column + wlen >= max && column > start) {
1391 printf("\n%*s", start, "");
1392 column = start + corr;
1393 }
1394 n = printf("%s%.*s", column > start ? " " : "", wlen, s);
1395 if (n <= 0)
1396 break;
1397 s += wlen;
1398 column += n;
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03001399 s = skip_spaces(s);
Andi Kleen08e60ed2016-09-15 15:24:43 -07001400 }
Andi Kleendc098b32013-04-20 11:02:29 -07001401}
1402
Sukadev Bhattiproluc8d68282016-09-15 15:24:48 -07001403void print_pmu_events(const char *event_glob, bool name_only, bool quiet_flag,
Jin Yaoa7f6c8c2019-10-15 10:53:57 +08001404 bool long_desc, bool details_flag, bool deprecated)
Andi Kleendc098b32013-04-20 11:02:29 -07001405{
1406 struct perf_pmu *pmu;
1407 struct perf_pmu_alias *alias;
1408 char buf[1024];
1409 int printed = 0;
1410 int len, j;
Andi Kleendd5f1032016-09-15 15:24:50 -07001411 struct sevent *aliases;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001412 int numdesc = 0;
Andi Kleen61eb2eb2016-09-15 15:24:44 -07001413 int columns = pager_get_columns();
Andi Kleendd5f1032016-09-15 15:24:50 -07001414 char *topic = NULL;
Andi Kleendc098b32013-04-20 11:02:29 -07001415
1416 pmu = NULL;
1417 len = 0;
Adrian Hunter42634bc2014-10-23 13:45:10 +03001418 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
Andi Kleendc098b32013-04-20 11:02:29 -07001419 list_for_each_entry(alias, &pmu->aliases, list)
1420 len++;
Adrian Hunter42634bc2014-10-23 13:45:10 +03001421 if (pmu->selectable)
1422 len++;
1423 }
Andi Kleendd5f1032016-09-15 15:24:50 -07001424 aliases = zalloc(sizeof(struct sevent) * len);
Andi Kleendc098b32013-04-20 11:02:29 -07001425 if (!aliases)
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001426 goto out_enomem;
Andi Kleendc098b32013-04-20 11:02:29 -07001427 pmu = NULL;
1428 j = 0;
Adrian Hunter42634bc2014-10-23 13:45:10 +03001429 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
Andi Kleendc098b32013-04-20 11:02:29 -07001430 list_for_each_entry(alias, &pmu->aliases, list) {
Andi Kleen08e60ed2016-09-15 15:24:43 -07001431 char *name = alias->desc ? alias->name :
1432 format_alias(buf, sizeof(buf), pmu, alias);
Andi Kleendc098b32013-04-20 11:02:29 -07001433 bool is_cpu = !strcmp(pmu->name, "cpu");
1434
Jin Yaoa7f6c8c2019-10-15 10:53:57 +08001435 if (alias->deprecated && !deprecated)
1436 continue;
1437
Andi Kleendc098b32013-04-20 11:02:29 -07001438 if (event_glob != NULL &&
Andi Kleen38d14f02016-10-19 10:50:01 -07001439 !(strglobmatch_nocase(name, event_glob) ||
1440 (!is_cpu && strglobmatch_nocase(alias->name,
Andi Kleen67bdc352016-10-19 11:45:23 -07001441 event_glob)) ||
1442 (alias->topic &&
1443 strglobmatch_nocase(alias->topic, event_glob))))
Andi Kleendc098b32013-04-20 11:02:29 -07001444 continue;
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001445
Andi Kleen08e60ed2016-09-15 15:24:43 -07001446 if (is_cpu && !name_only && !alias->desc)
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001447 name = format_alias_or(buf, sizeof(buf), pmu, alias);
1448
Andi Kleen08e60ed2016-09-15 15:24:43 -07001449 aliases[j].name = name;
1450 if (is_cpu && !name_only && !alias->desc)
1451 aliases[j].name = format_alias_or(buf,
1452 sizeof(buf),
1453 pmu, alias);
1454 aliases[j].name = strdup(aliases[j].name);
1455 if (!aliases[j].name)
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001456 goto out_enomem;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001457
Sukadev Bhattiproluc8d68282016-09-15 15:24:48 -07001458 aliases[j].desc = long_desc ? alias->long_desc :
1459 alias->desc;
Andi Kleendd5f1032016-09-15 15:24:50 -07001460 aliases[j].topic = alias->topic;
Andi Kleenf2361022017-01-27 18:03:40 -08001461 aliases[j].str = alias->str;
1462 aliases[j].pmu = pmu->name;
Andi Kleen7f372a62017-03-20 13:17:09 -07001463 aliases[j].metric_expr = alias->metric_expr;
Andi Kleen96284812017-03-20 13:17:10 -07001464 aliases[j].metric_name = alias->metric_name;
Andi Kleendc098b32013-04-20 11:02:29 -07001465 j++;
1466 }
Arnaldo Carvalho de Melofa52cea2015-10-02 15:28:16 -03001467 if (pmu->selectable &&
1468 (event_glob == NULL || strglobmatch(pmu->name, event_glob))) {
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001469 char *s;
1470 if (asprintf(&s, "%s//", pmu->name) < 0)
1471 goto out_enomem;
Andi Kleen08e60ed2016-09-15 15:24:43 -07001472 aliases[j].name = s;
Adrian Hunter42634bc2014-10-23 13:45:10 +03001473 j++;
1474 }
1475 }
Andi Kleendc098b32013-04-20 11:02:29 -07001476 len = j;
Andi Kleendd5f1032016-09-15 15:24:50 -07001477 qsort(aliases, len, sizeof(struct sevent), cmp_sevent);
Andi Kleendc098b32013-04-20 11:02:29 -07001478 for (j = 0; j < len; j++) {
Andi Kleen15b22ed2017-01-27 18:03:38 -08001479 /* Skip duplicates */
1480 if (j > 0 && !strcmp(aliases[j].name, aliases[j - 1].name))
1481 continue;
Andi Kleendc098b32013-04-20 11:02:29 -07001482 if (name_only) {
Andi Kleen08e60ed2016-09-15 15:24:43 -07001483 printf("%s ", aliases[j].name);
Andi Kleendc098b32013-04-20 11:02:29 -07001484 continue;
1485 }
Andi Kleen1c5f01f2016-09-15 15:24:45 -07001486 if (aliases[j].desc && !quiet_flag) {
Andi Kleen08e60ed2016-09-15 15:24:43 -07001487 if (numdesc++ == 0)
1488 printf("\n");
Andi Kleendd5f1032016-09-15 15:24:50 -07001489 if (aliases[j].topic && (!topic ||
1490 strcmp(topic, aliases[j].topic))) {
1491 printf("%s%s:\n", topic ? "\n" : "",
1492 aliases[j].topic);
1493 topic = aliases[j].topic;
1494 }
Andi Kleen08e60ed2016-09-15 15:24:43 -07001495 printf(" %-50s\n", aliases[j].name);
1496 printf("%*s", 8, "[");
1497 wordwrap(aliases[j].desc, 8, columns, 0);
1498 printf("]\n");
Andi Kleenbf874fc2017-03-20 13:17:11 -07001499 if (details_flag) {
Andi Kleen7f372a62017-03-20 13:17:09 -07001500 printf("%*s%s/%s/ ", 8, "", aliases[j].pmu, aliases[j].str);
Andi Kleen96284812017-03-20 13:17:10 -07001501 if (aliases[j].metric_name)
1502 printf(" MetricName: %s", aliases[j].metric_name);
Andi Kleen7f372a62017-03-20 13:17:09 -07001503 if (aliases[j].metric_expr)
1504 printf(" MetricExpr: %s", aliases[j].metric_expr);
1505 putchar('\n');
1506 }
Andi Kleen08e60ed2016-09-15 15:24:43 -07001507 } else
1508 printf(" %-50s [Kernel PMU event]\n", aliases[j].name);
Andi Kleendc098b32013-04-20 11:02:29 -07001509 printed++;
1510 }
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03001511 if (printed && pager_in_use())
Andi Kleendc098b32013-04-20 11:02:29 -07001512 printf("\n");
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001513out_free:
1514 for (j = 0; j < len; j++)
Andi Kleen08e60ed2016-09-15 15:24:43 -07001515 zfree(&aliases[j].name);
Arnaldo Carvalho de Melo7e4772d2014-10-24 10:25:09 -03001516 zfree(&aliases);
1517 return;
1518
1519out_enomem:
1520 printf("FATAL: not enough memory to print PMU events\n");
1521 if (aliases)
1522 goto out_free;
Andi Kleendc098b32013-04-20 11:02:29 -07001523}
Andi Kleen4cabc3d2013-08-21 16:47:26 -07001524
1525bool pmu_have_event(const char *pname, const char *name)
1526{
1527 struct perf_pmu *pmu;
1528 struct perf_pmu_alias *alias;
1529
1530 pmu = NULL;
1531 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1532 if (strcmp(pname, pmu->name))
1533 continue;
1534 list_for_each_entry(alias, &pmu->aliases, list)
1535 if (!strcmp(alias->name, name))
1536 return true;
1537 }
1538 return false;
1539}
Adrian Hunter7d4bdab2014-07-31 09:00:50 +03001540
1541static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
1542{
Adrian Hunter7d4bdab2014-07-31 09:00:50 +03001543 char path[PATH_MAX];
1544 const char *sysfs;
1545
1546 sysfs = sysfs__mountpoint();
1547 if (!sysfs)
1548 return NULL;
1549
1550 snprintf(path, PATH_MAX,
1551 "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name);
Andi Kleend9664582019-11-20 16:15:11 -08001552 if (!file_available(path))
Adrian Hunter7d4bdab2014-07-31 09:00:50 +03001553 return NULL;
Adrian Hunter7d4bdab2014-07-31 09:00:50 +03001554 return fopen(path, "r");
1555}
1556
1557int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
1558 ...)
1559{
1560 va_list args;
1561 FILE *file;
1562 int ret = EOF;
1563
1564 va_start(args, fmt);
1565 file = perf_pmu__open_file(pmu, name);
1566 if (file) {
1567 ret = vfscanf(file, fmt, args);
1568 fclose(file);
1569 }
1570 va_end(args);
1571 return ret;
1572}