xref: /openbmc/linux/tools/perf/builtin-list.c (revision 9350a917)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * builtin-list.c
4  *
5  * Builtin list command: list all event types
6  *
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>
10  */
11 #include "builtin.h"
12 
13 #include "util/print-events.h"
14 #include "util/pmus.h"
15 #include "util/pmu.h"
16 #include "util/debug.h"
17 #include "util/metricgroup.h"
18 #include "util/pfm.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>
25 #include <stdarg.h>
26 #include <stdio.h>
27 
28 /**
29  * struct print_state - State and configuration passed to the default_print
30  * functions.
31  */
32 struct print_state {
33 	/**
34 	 * @pmu_glob: Optionally restrict PMU and metric matching to PMU or
35 	 * debugfs subsystem name.
36 	 */
37 	char *pmu_glob;
38 	/** @event_glob: Optional pattern matching glob. */
39 	char *event_glob;
40 	/** @name_only: Print event or metric names only. */
41 	bool name_only;
42 	/** @desc: Print the event or metric description. */
43 	bool desc;
44 	/** @long_desc: Print longer event or metric description. */
45 	bool long_desc;
46 	/** @deprecated: Print deprecated events or metrics. */
47 	bool deprecated;
48 	/**
49 	 * @detailed: Print extra information on the perf event such as names
50 	 * and expressions used internally by events.
51 	 */
52 	bool detailed;
53 	/** @metrics: Controls printing of metric and metric groups. */
54 	bool metrics;
55 	/** @metricgroups: Controls printing of metric and metric groups. */
56 	bool metricgroups;
57 	/** @last_topic: The last printed event topic. */
58 	char *last_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;
63 };
64 
65 static void default_print_start(void *ps)
66 {
67 	struct print_state *print_state = ps;
68 
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");
71 }
72 
73 static void default_print_end(void *print_state __maybe_unused) {}
74 
75 static void wordwrap(const char *s, int start, int max, int corr)
76 {
77 	int column = start;
78 	int n;
79 	bool saw_newline = false;
80 
81 	while (*s) {
82 		int wlen = strcspn(s, " \t\n");
83 
84 		if ((column + wlen >= max && column > start) || saw_newline) {
85 			printf("\n%*s", start, "");
86 			column = start + corr;
87 		}
88 		n = printf("%s%.*s", column > start ? " " : "", wlen, s);
89 		if (n <= 0)
90 			break;
91 		saw_newline = s[wlen] == '\n';
92 		s += wlen;
93 		column += n;
94 		s = skip_spaces(s);
95 	}
96 }
97 
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)
104 {
105 	struct print_state *print_state = ps;
106 	int pos;
107 
108 	if (deprecated && !print_state->deprecated)
109 		return;
110 
111 	if (print_state->pmu_glob && pmu_name && !strglobmatch(pmu_name, print_state->pmu_glob))
112 		return;
113 
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)))
118 		return;
119 
120 	if (print_state->name_only) {
121 		if (event_alias && strlen(event_alias))
122 			printf("%s ", event_alias);
123 		else
124 			printf("%s ", event_name);
125 		return;
126 	}
127 
128 	if (strcmp(print_state->last_topic, topic ?: "")) {
129 		if (topic)
130 			printf("\n%s:\n", topic);
131 		zfree(&print_state->last_topic);
132 		print_state->last_topic = strdup(topic ?: "");
133 	}
134 
135 	if (event_alias && strlen(event_alias))
136 		pos = printf("  %s OR %s", event_name, event_alias);
137 	else
138 		pos = printf("  %s", event_name);
139 
140 	if (!topic && event_type_desc) {
141 		for (; pos < 53; pos++)
142 			putchar(' ');
143 		printf("[%s]\n", event_type_desc);
144 	} else
145 		putchar('\n');
146 
147 	if (desc && print_state->desc) {
148 		printf("%*s", 8, "[");
149 		wordwrap(desc, 8, pager_get_columns(), 0);
150 		printf("]\n");
151 	}
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);
156 		printf("]\n");
157 	}
158 
159 	if (print_state->detailed && encoding_desc) {
160 		printf("%*s", 8, "");
161 		wordwrap(encoding_desc, 8, pager_get_columns(), 0);
162 		putchar('\n');
163 	}
164 }
165 
166 static void default_print_metric(void *ps,
167 				const char *group,
168 				const char *name,
169 				const char *desc,
170 				const char *long_desc,
171 				const char *expr,
172 				const char *threshold,
173 				const char *unit __maybe_unused)
174 {
175 	struct print_state *print_state = ps;
176 
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)))
180 		return;
181 
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)
186 				putchar('\n');
187 		} else {
188 			printf("\nMetrics:\n\n");
189 		}
190 	}
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);
198 
199 				if (gdesc)
200 					printf("\n%s: [%s]\n", group, gdesc);
201 				else
202 					printf("\n%s:\n", group);
203 			} else
204 				printf("%s\n", group);
205 		}
206 		zfree(&print_state->last_metricgroups);
207 		print_state->last_metricgroups = strdup(group ?: "");
208 	}
209 	if (!print_state->metrics)
210 		return;
211 
212 	if (print_state->name_only) {
213 		if (print_state->metrics &&
214 		    !strlist__has_entry(print_state->visited_metrics, name)) {
215 			printf("%s ", name);
216 			strlist__add(print_state->visited_metrics, name);
217 		}
218 		return;
219 	}
220 	printf("  %s\n", name);
221 
222 	if (desc && print_state->desc) {
223 		printf("%*s", 8, "[");
224 		wordwrap(desc, 8, pager_get_columns(), 0);
225 		printf("]\n");
226 	}
227 	if (long_desc && print_state->long_desc) {
228 		printf("%*s", 8, "[");
229 		wordwrap(long_desc, 8, pager_get_columns(), 0);
230 		printf("]\n");
231 	}
232 	if (expr && print_state->detailed) {
233 		printf("%*s", 8, "[");
234 		wordwrap(expr, 8, pager_get_columns(), 0);
235 		printf("]\n");
236 	}
237 	if (threshold && print_state->detailed) {
238 		printf("%*s", 8, "[");
239 		wordwrap(threshold, 8, pager_get_columns(), 0);
240 		printf("]\n");
241 	}
242 }
243 
244 struct json_print_state {
245 	/** Should a separator be printed prior to the next item? */
246 	bool need_sep;
247 };
248 
249 static void json_print_start(void *print_state __maybe_unused)
250 {
251 	printf("[\n");
252 }
253 
254 static void json_print_end(void *ps)
255 {
256 	struct json_print_state *print_state = ps;
257 
258 	printf("%s]\n", print_state->need_sep ? "\n" : "");
259 }
260 
261 static void fix_escape_printf(struct strbuf *buf, const char *fmt, ...)
262 {
263 	va_list args;
264 
265 	va_start(args, fmt);
266 	strbuf_setlen(buf, 0);
267 	for (size_t fmt_pos = 0; fmt_pos < strlen(fmt); fmt_pos++) {
268 		switch (fmt[fmt_pos]) {
269 		case '%':
270 			fmt_pos++;
271 			switch (fmt[fmt_pos]) {
272 			case 's': {
273 				const char *s = va_arg(args, const char*);
274 
275 				strbuf_addstr(buf, s);
276 				break;
277 			}
278 			case 'S': {
279 				const char *s = va_arg(args, const char*);
280 
281 				for (size_t s_pos = 0; s_pos < strlen(s); s_pos++) {
282 					switch (s[s_pos]) {
283 					case '\n':
284 						strbuf_addstr(buf, "\\n");
285 						break;
286 					case '\\':
287 						fallthrough;
288 					case '\"':
289 						strbuf_addch(buf, '\\');
290 						fallthrough;
291 					default:
292 						strbuf_addch(buf, s[s_pos]);
293 						break;
294 					}
295 				}
296 				break;
297 			}
298 			default:
299 				pr_err("Unexpected format character '%c'\n", fmt[fmt_pos]);
300 				strbuf_addch(buf, '%');
301 				strbuf_addch(buf, fmt[fmt_pos]);
302 			}
303 			break;
304 		default:
305 			strbuf_addch(buf, fmt[fmt_pos]);
306 			break;
307 		}
308 	}
309 	va_end(args);
310 	fputs(buf->buf, stdout);
311 }
312 
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)
319 {
320 	struct json_print_state *print_state = ps;
321 	bool need_sep = false;
322 	struct strbuf buf;
323 
324 	strbuf_init(&buf, 0);
325 	printf("%s{\n", print_state->need_sep ? ",\n" : "");
326 	print_state->need_sep = true;
327 	if (pmu_name) {
328 		fix_escape_printf(&buf, "\t\"Unit\": \"%S\"", pmu_name);
329 		need_sep = true;
330 	}
331 	if (topic) {
332 		fix_escape_printf(&buf, "%s\t\"Topic\": \"%S\"", need_sep ? ",\n" : "", topic);
333 		need_sep = true;
334 	}
335 	if (event_name) {
336 		fix_escape_printf(&buf, "%s\t\"EventName\": \"%S\"", need_sep ? ",\n" : "",
337 				  event_name);
338 		need_sep = true;
339 	}
340 	if (event_alias && strlen(event_alias)) {
341 		fix_escape_printf(&buf, "%s\t\"EventAlias\": \"%S\"", need_sep ? ",\n" : "",
342 				  event_alias);
343 		need_sep = true;
344 	}
345 	if (scale_unit && strlen(scale_unit)) {
346 		fix_escape_printf(&buf, "%s\t\"ScaleUnit\": \"%S\"", need_sep ? ",\n" : "",
347 				  scale_unit);
348 		need_sep = true;
349 	}
350 	if (event_type_desc) {
351 		fix_escape_printf(&buf, "%s\t\"EventType\": \"%S\"", need_sep ? ",\n" : "",
352 				  event_type_desc);
353 		need_sep = true;
354 	}
355 	if (deprecated) {
356 		fix_escape_printf(&buf, "%s\t\"Deprecated\": \"%S\"", need_sep ? ",\n" : "",
357 				  deprecated ? "1" : "0");
358 		need_sep = true;
359 	}
360 	if (desc) {
361 		fix_escape_printf(&buf, "%s\t\"BriefDescription\": \"%S\"", need_sep ? ",\n" : "",
362 				  desc);
363 		need_sep = true;
364 	}
365 	if (long_desc) {
366 		fix_escape_printf(&buf, "%s\t\"PublicDescription\": \"%S\"", need_sep ? ",\n" : "",
367 				  long_desc);
368 		need_sep = true;
369 	}
370 	if (encoding_desc) {
371 		fix_escape_printf(&buf, "%s\t\"Encoding\": \"%S\"", need_sep ? ",\n" : "",
372 				  encoding_desc);
373 		need_sep = true;
374 	}
375 	printf("%s}", need_sep ? "\n" : "");
376 	strbuf_release(&buf);
377 }
378 
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)
383 {
384 	struct json_print_state *print_state = ps;
385 	bool need_sep = false;
386 	struct strbuf buf;
387 
388 	strbuf_init(&buf, 0);
389 	printf("%s{\n", print_state->need_sep ? ",\n" : "");
390 	print_state->need_sep = true;
391 	if (group) {
392 		fix_escape_printf(&buf, "\t\"MetricGroup\": \"%S\"", group);
393 		need_sep = true;
394 	}
395 	if (name) {
396 		fix_escape_printf(&buf, "%s\t\"MetricName\": \"%S\"", need_sep ? ",\n" : "", name);
397 		need_sep = true;
398 	}
399 	if (expr) {
400 		fix_escape_printf(&buf, "%s\t\"MetricExpr\": \"%S\"", need_sep ? ",\n" : "", expr);
401 		need_sep = true;
402 	}
403 	if (threshold) {
404 		fix_escape_printf(&buf, "%s\t\"MetricThreshold\": \"%S\"", need_sep ? ",\n" : "",
405 				  threshold);
406 		need_sep = true;
407 	}
408 	if (unit) {
409 		fix_escape_printf(&buf, "%s\t\"ScaleUnit\": \"%S\"", need_sep ? ",\n" : "", unit);
410 		need_sep = true;
411 	}
412 	if (desc) {
413 		fix_escape_printf(&buf, "%s\t\"BriefDescription\": \"%S\"", need_sep ? ",\n" : "",
414 				  desc);
415 		need_sep = true;
416 	}
417 	if (long_desc) {
418 		fix_escape_printf(&buf, "%s\t\"PublicDescription\": \"%S\"", need_sep ? ",\n" : "",
419 				  long_desc);
420 		need_sep = true;
421 	}
422 	printf("%s}", need_sep ? "\n" : "");
423 	strbuf_release(&buf);
424 }
425 
426 int cmd_list(int argc, const char **argv)
427 {
428 	int i, ret = 0;
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,
437 	};
438 	const char *cputype = NULL;
439 	const char *unit_name = NULL;
440 	bool json = false;
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"),
458 		OPT_END()
459 	};
460 	const char * const list_usage[] = {
461 #ifdef HAVE_LIBPFM
462 		"perf list [<options>] [hw|sw|cache|tracepoint|pmu|sdt|metric|metricgroup|event_glob|pfm]",
463 #else
464 		"perf list [<options>] [hw|sw|cache|tracepoint|pmu|sdt|metric|metricgroup|event_glob]",
465 #endif
466 		NULL
467 	};
468 
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);
472 
473 	argc = parse_options(argc, argv, list_options, list_usage,
474 			     PARSE_OPT_STOP_AT_NON_OPTION);
475 
476 	setup_pager();
477 
478 	if (!default_ps.name_only)
479 		setup_pager();
480 
481 	if (json) {
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,
487 		};
488 		ps = &json_ps;
489 	} else {
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);
495 		if (unit_name)
496 			default_ps.pmu_glob = strdup(unit_name);
497 		else if (cputype) {
498 			const struct perf_pmu *pmu = perf_pmus__pmu_for_pmu_filter(cputype);
499 
500 			if (!pmu) {
501 				pr_err("ERROR: cputype is not supported!\n");
502 				ret = -1;
503 				goto out;
504 			}
505 			default_ps.pmu_glob = pmu->name;
506 		}
507 	}
508 	print_cb.print_start(ps);
509 
510 	if (argc == 0) {
511 		default_ps.metrics = true;
512 		default_ps.metricgroups = true;
513 		print_events(&print_cb, ps);
514 		goto out;
515 	}
516 
517 	for (i = 0; i < argc; ++i) {
518 		char *sep, *s;
519 
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);
547 		}
548 #ifdef HAVE_LIBPFM
549 		else if (strcmp(argv[i], "pfm") == 0)
550 			print_libpfm_events(&print_cb, ps);
551 #endif
552 		else if ((sep = strchr(argv[i], ':')) != NULL) {
553 			char *old_pmu_glob = default_ps.pmu_glob;
554 
555 			default_ps.event_glob = strdup(argv[i]);
556 			if (!default_ps.event_glob) {
557 				ret = -1;
558 				goto out;
559 			}
560 
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;
568 		} else {
569 			if (asprintf(&s, "*%s*", argv[i]) < 0) {
570 				printf("Critical: Not enough memory! Trying to continue...\n");
571 				continue;
572 			}
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);
586 			free(s);
587 		}
588 	}
589 
590 out:
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);
596 	return ret;
597 }
598