1 // SPDX-License-Identifier: GPL-2.0
5 * Builtin list command: list all event types
7 * Copyright (C) 2009, Thomas Gleixner <tglx@linutronix.de>
8 * Copyright (C) 2008-2009, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
9 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
13 #include "util/print-events.h"
14 #include "util/pmus.h"
16 #include "util/debug.h"
17 #include "util/metricgroup.h"
19 #include "util/string2.h"
20 #include "util/strlist.h"
21 #include "util/strbuf.h"
22 #include <subcmd/pager.h>
23 #include <subcmd/parse-options.h>
24 #include <linux/zalloc.h>
29 * struct print_state - State and configuration passed to the default_print
34 * @pmu_glob: Optionally restrict PMU and metric matching to PMU or
35 * debugfs subsystem name.
38 /** @event_glob: Optional pattern matching glob. */
40 /** @name_only: Print event or metric names only. */
42 /** @desc: Print the event or metric description. */
44 /** @long_desc: Print longer event or metric description. */
46 /** @deprecated: Print deprecated events or metrics. */
49 * @detailed: Print extra information on the perf event such as names
50 * and expressions used internally by events.
53 /** @metrics: Controls printing of metric and metric groups. */
55 /** @metricgroups: Controls printing of metric and metric groups. */
57 /** @last_topic: The last printed event topic. */
59 /** @last_metricgroups: The last printed metric group. */
60 char *last_metricgroups;
61 /** @visited_metrics: Metrics that are printed to avoid duplicates. */
62 struct strlist *visited_metrics;
65 static void default_print_start(void *ps)
67 struct print_state *print_state = ps;
69 if (!print_state->name_only && pager_in_use())
70 printf("\nList of pre-defined events (to be used in -e or -M):\n\n");
73 static void default_print_end(void *print_state __maybe_unused) {}
75 static void wordwrap(const char *s, int start, int max, int corr)
79 bool saw_newline = false;
82 int wlen = strcspn(s, " \t\n");
84 if ((column + wlen >= max && column > start) || saw_newline) {
85 printf("\n%*s", start, "");
86 column = start + corr;
88 n = printf("%s%.*s", column > start ? " " : "", wlen, s);
91 saw_newline = s[wlen] == '\n';
98 static void default_print_event(void *ps, const char *pmu_name, const char *topic,
99 const char *event_name, const char *event_alias,
100 const char *scale_unit __maybe_unused,
101 bool deprecated, const char *event_type_desc,
102 const char *desc, const char *long_desc,
103 const char *encoding_desc)
105 struct print_state *print_state = ps;
108 if (deprecated && !print_state->deprecated)
111 if (print_state->pmu_glob && pmu_name && !strglobmatch(pmu_name, print_state->pmu_glob))
114 if (print_state->event_glob &&
115 (!event_name || !strglobmatch(event_name, print_state->event_glob)) &&
116 (!event_alias || !strglobmatch(event_alias, print_state->event_glob)) &&
117 (!topic || !strglobmatch_nocase(topic, print_state->event_glob)))
120 if (print_state->name_only) {
121 if (event_alias && strlen(event_alias))
122 printf("%s ", event_alias);
124 printf("%s ", event_name);
128 if (strcmp(print_state->last_topic, topic ?: "")) {
130 printf("\n%s:\n", topic);
131 zfree(&print_state->last_topic);
132 print_state->last_topic = strdup(topic ?: "");
135 if (event_alias && strlen(event_alias))
136 pos = printf(" %s OR %s", event_name, event_alias);
138 pos = printf(" %s", event_name);
140 if (!topic && event_type_desc) {
141 for (; pos < 53; pos++)
143 printf("[%s]\n", event_type_desc);
147 if (desc && print_state->desc) {
148 printf("%*s", 8, "[");
149 wordwrap(desc, 8, pager_get_columns(), 0);
152 long_desc = long_desc ?: desc;
153 if (long_desc && print_state->long_desc) {
154 printf("%*s", 8, "[");
155 wordwrap(long_desc, 8, pager_get_columns(), 0);
159 if (print_state->detailed && encoding_desc) {
160 printf("%*s", 8, "");
161 wordwrap(encoding_desc, 8, pager_get_columns(), 0);
166 static void default_print_metric(void *ps,
170 const char *long_desc,
172 const char *threshold,
173 const char *unit __maybe_unused)
175 struct print_state *print_state = ps;
177 if (print_state->event_glob &&
178 (!print_state->metrics || !name || !strglobmatch(name, print_state->event_glob)) &&
179 (!print_state->metricgroups || !group || !strglobmatch(group, print_state->event_glob)))
182 if (!print_state->name_only && !print_state->last_metricgroups) {
183 if (print_state->metricgroups) {
184 printf("\nMetric Groups:\n");
185 if (!print_state->metrics)
188 printf("\nMetrics:\n\n");
191 if (!print_state->last_metricgroups ||
192 strcmp(print_state->last_metricgroups, group ?: "")) {
193 if (group && print_state->metricgroups) {
194 if (print_state->name_only)
195 printf("%s ", group);
196 else if (print_state->metrics) {
197 const char *gdesc = describe_metricgroup(group);
200 printf("\n%s: [%s]\n", group, gdesc);
202 printf("\n%s:\n", group);
204 printf("%s\n", group);
206 zfree(&print_state->last_metricgroups);
207 print_state->last_metricgroups = strdup(group ?: "");
209 if (!print_state->metrics)
212 if (print_state->name_only) {
213 if (print_state->metrics &&
214 !strlist__has_entry(print_state->visited_metrics, name)) {
216 strlist__add(print_state->visited_metrics, name);
220 printf(" %s\n", name);
222 if (desc && print_state->desc) {
223 printf("%*s", 8, "[");
224 wordwrap(desc, 8, pager_get_columns(), 0);
227 if (long_desc && print_state->long_desc) {
228 printf("%*s", 8, "[");
229 wordwrap(long_desc, 8, pager_get_columns(), 0);
232 if (expr && print_state->detailed) {
233 printf("%*s", 8, "[");
234 wordwrap(expr, 8, pager_get_columns(), 0);
237 if (threshold && print_state->detailed) {
238 printf("%*s", 8, "[");
239 wordwrap(threshold, 8, pager_get_columns(), 0);
244 struct json_print_state {
245 /** Should a separator be printed prior to the next item? */
249 static void json_print_start(void *print_state __maybe_unused)
254 static void json_print_end(void *ps)
256 struct json_print_state *print_state = ps;
258 printf("%s]\n", print_state->need_sep ? "\n" : "");
261 static void fix_escape_printf(struct strbuf *buf, const char *fmt, ...)
266 strbuf_setlen(buf, 0);
267 for (size_t fmt_pos = 0; fmt_pos < strlen(fmt); fmt_pos++) {
268 switch (fmt[fmt_pos]) {
271 switch (fmt[fmt_pos]) {
273 const char *s = va_arg(args, const char*);
275 strbuf_addstr(buf, s);
279 const char *s = va_arg(args, const char*);
281 for (size_t s_pos = 0; s_pos < strlen(s); s_pos++) {
284 strbuf_addstr(buf, "\\n");
289 strbuf_addch(buf, '\\');
292 strbuf_addch(buf, s[s_pos]);
299 pr_err("Unexpected format character '%c'\n", fmt[fmt_pos]);
300 strbuf_addch(buf, '%');
301 strbuf_addch(buf, fmt[fmt_pos]);
305 strbuf_addch(buf, fmt[fmt_pos]);
310 fputs(buf->buf, stdout);
313 static void json_print_event(void *ps, const char *pmu_name, const char *topic,
314 const char *event_name, const char *event_alias,
315 const char *scale_unit,
316 bool deprecated, const char *event_type_desc,
317 const char *desc, const char *long_desc,
318 const char *encoding_desc)
320 struct json_print_state *print_state = ps;
321 bool need_sep = false;
324 strbuf_init(&buf, 0);
325 printf("%s{\n", print_state->need_sep ? ",\n" : "");
326 print_state->need_sep = true;
328 fix_escape_printf(&buf, "\t\"Unit\": \"%S\"", pmu_name);
332 fix_escape_printf(&buf, "%s\t\"Topic\": \"%S\"", need_sep ? ",\n" : "", topic);
336 fix_escape_printf(&buf, "%s\t\"EventName\": \"%S\"", need_sep ? ",\n" : "",
340 if (event_alias && strlen(event_alias)) {
341 fix_escape_printf(&buf, "%s\t\"EventAlias\": \"%S\"", need_sep ? ",\n" : "",
345 if (scale_unit && strlen(scale_unit)) {
346 fix_escape_printf(&buf, "%s\t\"ScaleUnit\": \"%S\"", need_sep ? ",\n" : "",
350 if (event_type_desc) {
351 fix_escape_printf(&buf, "%s\t\"EventType\": \"%S\"", need_sep ? ",\n" : "",
356 fix_escape_printf(&buf, "%s\t\"Deprecated\": \"%S\"", need_sep ? ",\n" : "",
357 deprecated ? "1" : "0");
361 fix_escape_printf(&buf, "%s\t\"BriefDescription\": \"%S\"", need_sep ? ",\n" : "",
366 fix_escape_printf(&buf, "%s\t\"PublicDescription\": \"%S\"", need_sep ? ",\n" : "",
371 fix_escape_printf(&buf, "%s\t\"Encoding\": \"%S\"", need_sep ? ",\n" : "",
375 printf("%s}", need_sep ? "\n" : "");
376 strbuf_release(&buf);
379 static void json_print_metric(void *ps __maybe_unused, const char *group,
380 const char *name, const char *desc,
381 const char *long_desc, const char *expr,
382 const char *threshold, const char *unit)
384 struct json_print_state *print_state = ps;
385 bool need_sep = false;
388 strbuf_init(&buf, 0);
389 printf("%s{\n", print_state->need_sep ? ",\n" : "");
390 print_state->need_sep = true;
392 fix_escape_printf(&buf, "\t\"MetricGroup\": \"%S\"", group);
396 fix_escape_printf(&buf, "%s\t\"MetricName\": \"%S\"", need_sep ? ",\n" : "", name);
400 fix_escape_printf(&buf, "%s\t\"MetricExpr\": \"%S\"", need_sep ? ",\n" : "", expr);
404 fix_escape_printf(&buf, "%s\t\"MetricThreshold\": \"%S\"", need_sep ? ",\n" : "",
409 fix_escape_printf(&buf, "%s\t\"ScaleUnit\": \"%S\"", need_sep ? ",\n" : "", unit);
413 fix_escape_printf(&buf, "%s\t\"BriefDescription\": \"%S\"", need_sep ? ",\n" : "",
418 fix_escape_printf(&buf, "%s\t\"PublicDescription\": \"%S\"", need_sep ? ",\n" : "",
422 printf("%s}", need_sep ? "\n" : "");
423 strbuf_release(&buf);
426 int cmd_list(int argc, const char **argv)
429 struct print_state default_ps = {};
430 struct print_state json_ps = {};
431 void *ps = &default_ps;
432 struct print_callbacks print_cb = {
433 .print_start = default_print_start,
434 .print_end = default_print_end,
435 .print_event = default_print_event,
436 .print_metric = default_print_metric,
438 const char *cputype = NULL;
439 const char *unit_name = NULL;
441 struct option list_options[] = {
442 OPT_BOOLEAN(0, "raw-dump", &default_ps.name_only, "Dump raw events"),
443 OPT_BOOLEAN('j', "json", &json, "JSON encode events and metrics"),
444 OPT_BOOLEAN('d', "desc", &default_ps.desc,
445 "Print extra event descriptions. --no-desc to not print."),
446 OPT_BOOLEAN('v', "long-desc", &default_ps.long_desc,
447 "Print longer event descriptions."),
448 OPT_BOOLEAN(0, "details", &default_ps.detailed,
449 "Print information on the perf event names and expressions used internally by events."),
450 OPT_BOOLEAN(0, "deprecated", &default_ps.deprecated,
451 "Print deprecated events."),
452 OPT_STRING(0, "cputype", &cputype, "cpu type",
453 "Limit PMU or metric printing to the given PMU (e.g. cpu, core or atom)."),
454 OPT_STRING(0, "unit", &unit_name, "PMU name",
455 "Limit PMU or metric printing to the specified PMU."),
456 OPT_INCR(0, "debug", &verbose,
457 "Enable debugging output"),
460 const char * const list_usage[] = {
462 "perf list [<options>] [hw|sw|cache|tracepoint|pmu|sdt|metric|metricgroup|event_glob|pfm]",
464 "perf list [<options>] [hw|sw|cache|tracepoint|pmu|sdt|metric|metricgroup|event_glob]",
469 set_option_flag(list_options, 0, "raw-dump", PARSE_OPT_HIDDEN);
470 /* Hide hybrid flag for the more generic 'unit' flag. */
471 set_option_flag(list_options, 0, "cputype", PARSE_OPT_HIDDEN);
473 argc = parse_options(argc, argv, list_options, list_usage,
474 PARSE_OPT_STOP_AT_NON_OPTION);
478 if (!default_ps.name_only)
482 print_cb = (struct print_callbacks){
483 .print_start = json_print_start,
484 .print_end = json_print_end,
485 .print_event = json_print_event,
486 .print_metric = json_print_metric,
490 default_ps.desc = !default_ps.long_desc;
491 default_ps.last_topic = strdup("");
492 assert(default_ps.last_topic);
493 default_ps.visited_metrics = strlist__new(NULL, NULL);
494 assert(default_ps.visited_metrics);
496 default_ps.pmu_glob = strdup(unit_name);
498 const struct perf_pmu *pmu = perf_pmus__pmu_for_pmu_filter(cputype);
501 pr_err("ERROR: cputype is not supported!\n");
505 default_ps.pmu_glob = pmu->name;
508 print_cb.print_start(ps);
511 default_ps.metrics = true;
512 default_ps.metricgroups = true;
513 print_events(&print_cb, ps);
517 for (i = 0; i < argc; ++i) {
520 if (strcmp(argv[i], "tracepoint") == 0)
521 print_tracepoint_events(&print_cb, ps);
522 else if (strcmp(argv[i], "hw") == 0 ||
523 strcmp(argv[i], "hardware") == 0)
524 print_symbol_events(&print_cb, ps, PERF_TYPE_HARDWARE,
525 event_symbols_hw, PERF_COUNT_HW_MAX);
526 else if (strcmp(argv[i], "sw") == 0 ||
527 strcmp(argv[i], "software") == 0) {
528 print_symbol_events(&print_cb, ps, PERF_TYPE_SOFTWARE,
529 event_symbols_sw, PERF_COUNT_SW_MAX);
530 print_tool_events(&print_cb, ps);
531 } else if (strcmp(argv[i], "cache") == 0 ||
532 strcmp(argv[i], "hwcache") == 0)
533 print_hwcache_events(&print_cb, ps);
534 else if (strcmp(argv[i], "pmu") == 0)
535 perf_pmus__print_pmu_events(&print_cb, ps);
536 else if (strcmp(argv[i], "sdt") == 0)
537 print_sdt_events(&print_cb, ps);
538 else if (strcmp(argv[i], "metric") == 0 || strcmp(argv[i], "metrics") == 0) {
539 default_ps.metricgroups = false;
540 default_ps.metrics = true;
541 metricgroup__print(&print_cb, ps);
542 } else if (strcmp(argv[i], "metricgroup") == 0 ||
543 strcmp(argv[i], "metricgroups") == 0) {
544 default_ps.metricgroups = true;
545 default_ps.metrics = false;
546 metricgroup__print(&print_cb, ps);
549 else if (strcmp(argv[i], "pfm") == 0)
550 print_libpfm_events(&print_cb, ps);
552 else if ((sep = strchr(argv[i], ':')) != NULL) {
553 char *old_pmu_glob = default_ps.pmu_glob;
555 default_ps.event_glob = strdup(argv[i]);
556 if (!default_ps.event_glob) {
561 print_tracepoint_events(&print_cb, ps);
562 print_sdt_events(&print_cb, ps);
563 default_ps.metrics = true;
564 default_ps.metricgroups = true;
565 metricgroup__print(&print_cb, ps);
566 zfree(&default_ps.event_glob);
567 default_ps.pmu_glob = old_pmu_glob;
569 if (asprintf(&s, "*%s*", argv[i]) < 0) {
570 printf("Critical: Not enough memory! Trying to continue...\n");
573 default_ps.event_glob = s;
574 print_symbol_events(&print_cb, ps, PERF_TYPE_HARDWARE,
575 event_symbols_hw, PERF_COUNT_HW_MAX);
576 print_symbol_events(&print_cb, ps, PERF_TYPE_SOFTWARE,
577 event_symbols_sw, PERF_COUNT_SW_MAX);
578 print_tool_events(&print_cb, ps);
579 print_hwcache_events(&print_cb, ps);
580 perf_pmus__print_pmu_events(&print_cb, ps);
581 print_tracepoint_events(&print_cb, ps);
582 print_sdt_events(&print_cb, ps);
583 default_ps.metrics = true;
584 default_ps.metricgroups = true;
585 metricgroup__print(&print_cb, ps);
591 print_cb.print_end(ps);
592 free(default_ps.pmu_glob);
593 free(default_ps.last_topic);
594 free(default_ps.last_metricgroups);
595 strlist__delete(default_ps.visited_metrics);