1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
// SPDX-License-Identifier: GPL-2.0
/*
* builtin-list.c
*
* Builtin list command: list all event types
*
* Copyright (C) 2009, Thomas Gleixner <tglx@linutronix.de>
* Copyright (C) 2008-2009, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
* Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
*/
#include "builtin.h"
#include "util/print-events.h"
#include "util/pmu.h"
#include "util/pmu-hybrid.h"
#include "util/debug.h"
#include "util/metricgroup.h"
#include "util/string2.h"
#include "util/strlist.h"
#include <subcmd/pager.h>
#include <subcmd/parse-options.h>
#include <stdio.h>
/**
* struct print_state - State and configuration passed to the default_print
* functions.
*/
struct print_state {
/**
* @pmu_glob: Optionally restrict PMU and metric matching to PMU or
* debugfs subsystem name.
*/
char *pmu_glob;
/** @event_glob: Optional pattern matching glob. */
char *event_glob;
/** @name_only: Print event or metric names only. */
bool name_only;
/** @desc: Print the event or metric description. */
bool desc;
/** @long_desc: Print longer event or metric description. */
bool long_desc;
/** @deprecated: Print deprecated events or metrics. */
bool deprecated;
/**
* @detailed: Print extra information on the perf event such as names
* and expressions used internally by events.
*/
bool detailed;
/** @metrics: Controls printing of metric and metric groups. */
bool metrics;
/** @metricgroups: Controls printing of metric and metric groups. */
bool metricgroups;
/** @last_topic: The last printed event topic. */
char *last_topic;
/** @last_metricgroups: The last printed metric group. */
char *last_metricgroups;
/** @visited_metrics: Metrics that are printed to avoid duplicates. */
struct strlist *visited_metrics;
};
static void default_print_start(void *ps)
{
struct print_state *print_state = ps;
if (!print_state->name_only && pager_in_use())
printf("\nList of pre-defined events (to be used in -e or -M):\n\n");
}
static void default_print_end(void *print_state __maybe_unused) {}
static void wordwrap(const char *s, int start, int max, int corr)
{
int column = start;
int n;
while (*s) {
int wlen = strcspn(s, " \t");
if (column + wlen >= max && column > start) {
printf("\n%*s", start, "");
column = start + corr;
}
n = printf("%s%.*s", column > start ? " " : "", wlen, s);
if (n <= 0)
break;
s += wlen;
column += n;
s = skip_spaces(s);
}
}
static void default_print_event(void *ps, const char *pmu_name, const char *topic,
const char *event_name, const char *event_alias,
const char *scale_unit __maybe_unused,
bool deprecated, const char *event_type_desc,
const char *desc, const char *long_desc,
const char *encoding_desc,
const char *metric_name, const char *metric_expr)
{
struct print_state *print_state = ps;
int pos;
if (deprecated && !print_state->deprecated)
return;
if (print_state->pmu_glob && pmu_name && !strglobmatch(pmu_name, print_state->pmu_glob))
return;
if (print_state->event_glob &&
(!event_name || !strglobmatch(event_name, print_state->event_glob)) &&
(!event_alias || !strglobmatch(event_alias, print_state->event_glob)) &&
(!topic || !strglobmatch_nocase(topic, print_state->event_glob)))
return;
if (print_state->name_only) {
if (event_alias && strlen(event_alias))
printf("%s ", event_alias);
else
printf("%s ", event_name);
return;
}
if (strcmp(print_state->last_topic, topic ?: "")) {
if (topic)
printf("\n%s:\n", topic);
free(print_state->last_topic);
print_state->last_topic = strdup(topic ?: "");
}
if (event_alias && strlen(event_alias))
pos = printf(" %s OR %s", event_name, event_alias);
else
pos = printf(" %s", event_name);
if (!topic && event_type_desc) {
for (; pos < 53; pos++)
putchar(' ');
printf("[%s]\n", event_type_desc);
} else
putchar('\n');
if (desc && print_state->desc) {
printf("%*s", 8, "[");
wordwrap(desc, 8, pager_get_columns(), 0);
printf("]\n");
}
if (long_desc && print_state->long_desc) {
printf("%*s", 8, "[");
wordwrap(long_desc, 8, pager_get_columns(), 0);
printf("]\n");
}
if (print_state->detailed && encoding_desc) {
printf("%*s%s", 8, "", encoding_desc);
if (metric_name)
printf(" MetricName: %s", metric_name);
if (metric_expr)
printf(" MetricExpr: %s", metric_expr);
putchar('\n');
}
}
static void default_print_metric(void *ps,
const char *group,
const char *name,
const char *desc,
const char *long_desc,
const char *expr,
const char *unit __maybe_unused)
{
struct print_state *print_state = ps;
if (print_state->event_glob &&
(!print_state->metrics || !name || !strglobmatch(name, print_state->event_glob)) &&
(!print_state->metricgroups || !group || !strglobmatch(group, print_state->event_glob)))
return;
if (!print_state->name_only && !print_state->last_metricgroups) {
if (print_state->metricgroups) {
printf("\nMetric Groups:\n");
if (!print_state->metrics)
putchar('\n');
} else {
printf("\nMetrics:\n\n");
}
}
if (!print_state->last_metricgroups ||
strcmp(print_state->last_metricgroups, group ?: "")) {
if (group && print_state->metricgroups) {
if (print_state->name_only)
printf("%s ", group);
else if (print_state->metrics)
printf("\n%s:\n", group);
else
printf("%s\n", group);
}
free(print_state->last_metricgroups);
print_state->last_metricgroups = strdup(group ?: "");
}
if (!print_state->metrics)
return;
if (print_state->name_only) {
if (print_state->metrics &&
!strlist__has_entry(print_state->visited_metrics, name)) {
printf("%s ", name);
strlist__add(print_state->visited_metrics, name);
}
return;
}
printf(" %s\n", name);
if (desc && print_state->desc) {
printf("%*s", 8, "[");
wordwrap(desc, 8, pager_get_columns(), 0);
printf("]\n");
}
if (long_desc && print_state->long_desc) {
printf("%*s", 8, "[");
wordwrap(long_desc, 8, pager_get_columns(), 0);
printf("]\n");
}
if (expr && print_state->detailed) {
printf("%*s", 8, "[");
wordwrap(expr, 8, pager_get_columns(), 0);
printf("]\n");
}
}
int cmd_list(int argc, const char **argv)
{
int i, ret = 0;
struct print_state ps = {};
struct print_callbacks print_cb = {
.print_start = default_print_start,
.print_end = default_print_end,
.print_event = default_print_event,
.print_metric = default_print_metric,
};
const char *hybrid_name = NULL;
const char *unit_name = NULL;
struct option list_options[] = {
OPT_BOOLEAN(0, "raw-dump", &ps.name_only, "Dump raw events"),
OPT_BOOLEAN('d', "desc", &ps.desc,
"Print extra event descriptions. --no-desc to not print."),
OPT_BOOLEAN('v', "long-desc", &ps.long_desc,
"Print longer event descriptions."),
OPT_BOOLEAN(0, "details", &ps.detailed,
"Print information on the perf event names and expressions used internally by events."),
OPT_BOOLEAN(0, "deprecated", &ps.deprecated,
"Print deprecated events."),
OPT_STRING(0, "cputype", &hybrid_name, "hybrid cpu type",
"Limit PMU or metric printing to the given hybrid PMU (e.g. core or atom)."),
OPT_STRING(0, "unit", &unit_name, "PMU name",
"Limit PMU or metric printing to the specified PMU."),
OPT_INCR(0, "debug", &verbose,
"Enable debugging output"),
OPT_END()
};
const char * const list_usage[] = {
"perf list [<options>] [hw|sw|cache|tracepoint|pmu|sdt|metric|metricgroup|event_glob]",
NULL
};
set_option_flag(list_options, 0, "raw-dump", PARSE_OPT_HIDDEN);
/* Hide hybrid flag for the more generic 'unit' flag. */
set_option_flag(list_options, 0, "cputype", PARSE_OPT_HIDDEN);
argc = parse_options(argc, argv, list_options, list_usage,
PARSE_OPT_STOP_AT_NON_OPTION);
setup_pager();
if (!ps.name_only)
setup_pager();
ps.desc = !ps.long_desc;
ps.last_topic = strdup("");
assert(ps.last_topic);
ps.visited_metrics = strlist__new(NULL, NULL);
assert(ps.visited_metrics);
if (unit_name)
ps.pmu_glob = strdup(unit_name);
else if (hybrid_name) {
ps.pmu_glob = perf_pmu__hybrid_type_to_pmu(hybrid_name);
if (!ps.pmu_glob)
pr_warning("WARNING: hybrid cputype is not supported!\n");
}
print_cb.print_start(&ps);
if (argc == 0) {
ps.metrics = true;
ps.metricgroups = true;
print_events(&print_cb, &ps);
goto out;
}
for (i = 0; i < argc; ++i) {
char *sep, *s;
if (strcmp(argv[i], "tracepoint") == 0)
print_tracepoint_events(&print_cb, &ps);
else if (strcmp(argv[i], "hw") == 0 ||
strcmp(argv[i], "hardware") == 0)
print_symbol_events(&print_cb, &ps, PERF_TYPE_HARDWARE,
event_symbols_hw, PERF_COUNT_HW_MAX);
else if (strcmp(argv[i], "sw") == 0 ||
strcmp(argv[i], "software") == 0) {
print_symbol_events(&print_cb, &ps, PERF_TYPE_SOFTWARE,
event_symbols_sw, PERF_COUNT_SW_MAX);
print_tool_events(&print_cb, &ps);
} else if (strcmp(argv[i], "cache") == 0 ||
strcmp(argv[i], "hwcache") == 0)
print_hwcache_events(&print_cb, &ps);
else if (strcmp(argv[i], "pmu") == 0)
print_pmu_events(&print_cb, &ps);
else if (strcmp(argv[i], "sdt") == 0)
print_sdt_events(&print_cb, &ps);
else if (strcmp(argv[i], "metric") == 0 || strcmp(argv[i], "metrics") == 0) {
ps.metricgroups = false;
ps.metrics = true;
metricgroup__print(&print_cb, &ps);
} else if (strcmp(argv[i], "metricgroup") == 0 ||
strcmp(argv[i], "metricgroups") == 0) {
ps.metricgroups = true;
ps.metrics = false;
metricgroup__print(&print_cb, &ps);
} else if ((sep = strchr(argv[i], ':')) != NULL) {
int sep_idx;
char *old_pmu_glob = ps.pmu_glob;
sep_idx = sep - argv[i];
s = strdup(argv[i]);
if (s == NULL) {
ret = -1;
goto out;
}
s[sep_idx] = '\0';
ps.pmu_glob = s;
ps.event_glob = s + sep_idx + 1;
print_tracepoint_events(&print_cb, &ps);
print_sdt_events(&print_cb, &ps);
ps.metrics = true;
ps.metricgroups = true;
metricgroup__print(&print_cb, &ps);
free(s);
ps.pmu_glob = old_pmu_glob;
} else {
if (asprintf(&s, "*%s*", argv[i]) < 0) {
printf("Critical: Not enough memory! Trying to continue...\n");
continue;
}
ps.event_glob = s;
print_symbol_events(&print_cb, &ps, PERF_TYPE_HARDWARE,
event_symbols_hw, PERF_COUNT_HW_MAX);
print_symbol_events(&print_cb, &ps, PERF_TYPE_SOFTWARE,
event_symbols_sw, PERF_COUNT_SW_MAX);
print_tool_events(&print_cb, &ps);
print_hwcache_events(&print_cb, &ps);
print_pmu_events(&print_cb, &ps);
print_tracepoint_events(&print_cb, &ps);
print_sdt_events(&print_cb, &ps);
ps.metrics = true;
ps.metricgroups = true;
metricgroup__print(&print_cb, &ps);
free(s);
}
}
out:
print_cb.print_end(&ps);
free(ps.pmu_glob);
free(ps.last_topic);
free(ps.last_metricgroups);
strlist__delete(ps.visited_metrics);
return ret;
}
|