1 // SPDX-License-Identifier: GPL-2.0
2 #include <dirent.h>
3 #include <errno.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <fcntl.h>
8 #include <sys/param.h>
9 #include <unistd.h>
10
11 #include <api/fs/tracing_path.h>
12 #include <linux/stddef.h>
13 #include <linux/perf_event.h>
14 #include <linux/zalloc.h>
15 #include <subcmd/pager.h>
16
17 #include "build-id.h"
18 #include "debug.h"
19 #include "evsel.h"
20 #include "metricgroup.h"
21 #include "parse-events.h"
22 #include "pmu.h"
23 #include "pmus.h"
24 #include "print-events.h"
25 #include "probe-file.h"
26 #include "string2.h"
27 #include "strlist.h"
28 #include "tracepoint.h"
29 #include "pfm.h"
30 #include "thread_map.h"
31
32 #define MAX_NAME_LEN 100
33
34 /** Strings corresponding to enum perf_type_id. */
35 static const char * const event_type_descriptors[] = {
36 "Hardware event",
37 "Software event",
38 "Tracepoint event",
39 "Hardware cache event",
40 "Raw hardware event descriptor",
41 "Hardware breakpoint",
42 };
43
44 static const struct event_symbol event_symbols_tool[PERF_TOOL_MAX] = {
45 [PERF_TOOL_DURATION_TIME] = {
46 .symbol = "duration_time",
47 .alias = "",
48 },
49 [PERF_TOOL_USER_TIME] = {
50 .symbol = "user_time",
51 .alias = "",
52 },
53 [PERF_TOOL_SYSTEM_TIME] = {
54 .symbol = "system_time",
55 .alias = "",
56 },
57 };
58
59 /*
60 * Print the events from <debugfs_mount_point>/tracing/events
61 */
print_tracepoint_events(const struct print_callbacks * print_cb __maybe_unused,void * print_state __maybe_unused)62 void print_tracepoint_events(const struct print_callbacks *print_cb __maybe_unused, void *print_state __maybe_unused)
63 {
64 char *events_path = get_tracing_file("events");
65 int events_fd = open(events_path, O_PATH);
66
67 put_tracing_file(events_path);
68 if (events_fd < 0) {
69 printf("Error: failed to open tracing events directory\n");
70 return;
71 }
72
73 #ifdef HAVE_SCANDIRAT_SUPPORT
74 {
75 struct dirent **sys_namelist = NULL;
76 int sys_items = tracing_events__scandir_alphasort(&sys_namelist);
77
78 for (int i = 0; i < sys_items; i++) {
79 struct dirent *sys_dirent = sys_namelist[i];
80 struct dirent **evt_namelist = NULL;
81 int dir_fd;
82 int evt_items;
83
84 if (sys_dirent->d_type != DT_DIR ||
85 !strcmp(sys_dirent->d_name, ".") ||
86 !strcmp(sys_dirent->d_name, ".."))
87 goto next_sys;
88
89 dir_fd = openat(events_fd, sys_dirent->d_name, O_PATH);
90 if (dir_fd < 0)
91 goto next_sys;
92
93 evt_items = scandirat(events_fd, sys_dirent->d_name, &evt_namelist, NULL, alphasort);
94 for (int j = 0; j < evt_items; j++) {
95 struct dirent *evt_dirent = evt_namelist[j];
96 char evt_path[MAXPATHLEN];
97 int evt_fd;
98
99 if (evt_dirent->d_type != DT_DIR ||
100 !strcmp(evt_dirent->d_name, ".") ||
101 !strcmp(evt_dirent->d_name, ".."))
102 goto next_evt;
103
104 snprintf(evt_path, sizeof(evt_path), "%s/id", evt_dirent->d_name);
105 evt_fd = openat(dir_fd, evt_path, O_RDONLY);
106 if (evt_fd < 0)
107 goto next_evt;
108 close(evt_fd);
109
110 snprintf(evt_path, MAXPATHLEN, "%s:%s",
111 sys_dirent->d_name, evt_dirent->d_name);
112 print_cb->print_event(print_state,
113 /*topic=*/NULL,
114 /*pmu_name=*/NULL,
115 evt_path,
116 /*event_alias=*/NULL,
117 /*scale_unit=*/NULL,
118 /*deprecated=*/false,
119 "Tracepoint event",
120 /*desc=*/NULL,
121 /*long_desc=*/NULL,
122 /*encoding_desc=*/NULL);
123 next_evt:
124 free(evt_namelist[j]);
125 }
126 close(dir_fd);
127 free(evt_namelist);
128 next_sys:
129 free(sys_namelist[i]);
130 }
131
132 free(sys_namelist);
133 }
134 #else
135 printf("\nWARNING: Your libc doesn't have the scandirat function, please ask its maintainers to implement it.\n"
136 " As a rough fallback, please do 'ls %s' to see the available tracepoint events.\n", events_path);
137 #endif
138 close(events_fd);
139 }
140
print_sdt_events(const struct print_callbacks * print_cb,void * print_state)141 void print_sdt_events(const struct print_callbacks *print_cb, void *print_state)
142 {
143 struct strlist *bidlist, *sdtlist;
144 struct str_node *bid_nd, *sdt_name, *next_sdt_name;
145 const char *last_sdt_name = NULL;
146
147 /*
148 * The implicitly sorted sdtlist will hold the tracepoint name followed
149 * by @<buildid>. If the tracepoint name is unique (determined by
150 * looking at the adjacent nodes) the @<buildid> is dropped otherwise
151 * the executable path and buildid are added to the name.
152 */
153 sdtlist = strlist__new(NULL, NULL);
154 if (!sdtlist) {
155 pr_debug("Failed to allocate new strlist for SDT\n");
156 return;
157 }
158 bidlist = build_id_cache__list_all(true);
159 if (!bidlist) {
160 pr_debug("Failed to get buildids: %d\n", errno);
161 return;
162 }
163 strlist__for_each_entry(bid_nd, bidlist) {
164 struct probe_cache *pcache;
165 struct probe_cache_entry *ent;
166
167 pcache = probe_cache__new(bid_nd->s, NULL);
168 if (!pcache)
169 continue;
170 list_for_each_entry(ent, &pcache->entries, node) {
171 char buf[1024];
172
173 snprintf(buf, sizeof(buf), "%s:%s@%s",
174 ent->pev.group, ent->pev.event, bid_nd->s);
175 strlist__add(sdtlist, buf);
176 }
177 probe_cache__delete(pcache);
178 }
179 strlist__delete(bidlist);
180
181 strlist__for_each_entry(sdt_name, sdtlist) {
182 bool show_detail = false;
183 char *bid = strchr(sdt_name->s, '@');
184 char *evt_name = NULL;
185
186 if (bid)
187 *(bid++) = '\0';
188
189 if (last_sdt_name && !strcmp(last_sdt_name, sdt_name->s)) {
190 show_detail = true;
191 } else {
192 next_sdt_name = strlist__next(sdt_name);
193 if (next_sdt_name) {
194 char *bid2 = strchr(next_sdt_name->s, '@');
195
196 if (bid2)
197 *bid2 = '\0';
198 if (strcmp(sdt_name->s, next_sdt_name->s) == 0)
199 show_detail = true;
200 if (bid2)
201 *bid2 = '@';
202 }
203 }
204 last_sdt_name = sdt_name->s;
205
206 if (show_detail) {
207 char *path = build_id_cache__origname(bid);
208
209 if (path) {
210 if (asprintf(&evt_name, "%s@%s(%.12s)", sdt_name->s, path, bid) < 0)
211 evt_name = NULL;
212 free(path);
213 }
214 }
215 print_cb->print_event(print_state,
216 /*topic=*/NULL,
217 /*pmu_name=*/NULL,
218 evt_name ?: sdt_name->s,
219 /*event_alias=*/NULL,
220 /*deprecated=*/false,
221 /*scale_unit=*/NULL,
222 "SDT event",
223 /*desc=*/NULL,
224 /*long_desc=*/NULL,
225 /*encoding_desc=*/NULL);
226
227 free(evt_name);
228 }
229 strlist__delete(sdtlist);
230 }
231
is_event_supported(u8 type,u64 config)232 bool is_event_supported(u8 type, u64 config)
233 {
234 bool ret = true;
235 struct evsel *evsel;
236 struct perf_event_attr attr = {
237 .type = type,
238 .config = config,
239 .disabled = 1,
240 };
241 struct perf_thread_map *tmap = thread_map__new_by_tid(0);
242
243 if (tmap == NULL)
244 return false;
245
246 evsel = evsel__new(&attr);
247 if (evsel) {
248 ret = evsel__open(evsel, NULL, tmap) >= 0;
249
250 if (!ret) {
251 /*
252 * The event may fail to open if the paranoid value
253 * /proc/sys/kernel/perf_event_paranoid is set to 2
254 * Re-run with exclude_kernel set; we don't do that by
255 * default as some ARM machines do not support it.
256 */
257 evsel->core.attr.exclude_kernel = 1;
258 ret = evsel__open(evsel, NULL, tmap) >= 0;
259 }
260
261 if (!ret) {
262 /*
263 * The event may fail to open if the PMU requires
264 * exclude_guest to be set (e.g. as the Apple M1 PMU
265 * requires).
266 * Re-run with exclude_guest set; we don't do that by
267 * default as it's equally legitimate for another PMU
268 * driver to require that exclude_guest is clear.
269 */
270 evsel->core.attr.exclude_guest = 1;
271 ret = evsel__open(evsel, NULL, tmap) >= 0;
272 }
273
274 evsel__close(evsel);
275 evsel__delete(evsel);
276 }
277
278 perf_thread_map__put(tmap);
279 return ret;
280 }
281
print_hwcache_events(const struct print_callbacks * print_cb,void * print_state)282 int print_hwcache_events(const struct print_callbacks *print_cb, void *print_state)
283 {
284 struct perf_pmu *pmu = NULL;
285 const char *event_type_descriptor = event_type_descriptors[PERF_TYPE_HW_CACHE];
286
287 /*
288 * Only print core PMUs, skipping uncore for performance and
289 * PERF_TYPE_SOFTWARE that can succeed in opening legacy cache evenst.
290 */
291 while ((pmu = perf_pmus__scan_core(pmu)) != NULL) {
292 if (pmu->is_uncore || pmu->type == PERF_TYPE_SOFTWARE)
293 continue;
294
295 for (int type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
296 for (int op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
297 /* skip invalid cache type */
298 if (!evsel__is_cache_op_valid(type, op))
299 continue;
300
301 for (int res = 0; res < PERF_COUNT_HW_CACHE_RESULT_MAX; res++) {
302 char name[64];
303 char alias_name[128];
304 __u64 config;
305 int ret;
306
307 __evsel__hw_cache_type_op_res_name(type, op, res,
308 name, sizeof(name));
309
310 ret = parse_events__decode_legacy_cache(name, pmu->type,
311 &config);
312 if (ret || !is_event_supported(PERF_TYPE_HW_CACHE, config))
313 continue;
314 snprintf(alias_name, sizeof(alias_name), "%s/%s/",
315 pmu->name, name);
316 print_cb->print_event(print_state,
317 "cache",
318 pmu->name,
319 name,
320 alias_name,
321 /*scale_unit=*/NULL,
322 /*deprecated=*/false,
323 event_type_descriptor,
324 /*desc=*/NULL,
325 /*long_desc=*/NULL,
326 /*encoding_desc=*/NULL);
327 }
328 }
329 }
330 }
331 return 0;
332 }
333
print_tool_events(const struct print_callbacks * print_cb,void * print_state)334 void print_tool_events(const struct print_callbacks *print_cb, void *print_state)
335 {
336 // Start at 1 because the first enum entry means no tool event.
337 for (int i = 1; i < PERF_TOOL_MAX; ++i) {
338 print_cb->print_event(print_state,
339 "tool",
340 /*pmu_name=*/NULL,
341 event_symbols_tool[i].symbol,
342 event_symbols_tool[i].alias,
343 /*scale_unit=*/NULL,
344 /*deprecated=*/false,
345 "Tool event",
346 /*desc=*/NULL,
347 /*long_desc=*/NULL,
348 /*encoding_desc=*/NULL);
349 }
350 }
351
print_symbol_events(const struct print_callbacks * print_cb,void * print_state,unsigned int type,const struct event_symbol * syms,unsigned int max)352 void print_symbol_events(const struct print_callbacks *print_cb, void *print_state,
353 unsigned int type, const struct event_symbol *syms,
354 unsigned int max)
355 {
356 struct strlist *evt_name_list = strlist__new(NULL, NULL);
357 struct str_node *nd;
358
359 if (!evt_name_list) {
360 pr_debug("Failed to allocate new strlist for symbol events\n");
361 return;
362 }
363 for (unsigned int i = 0; i < max; i++) {
364 /*
365 * New attr.config still not supported here, the latest
366 * example was PERF_COUNT_SW_CGROUP_SWITCHES
367 */
368 if (syms[i].symbol == NULL)
369 continue;
370
371 if (!is_event_supported(type, i))
372 continue;
373
374 if (strlen(syms[i].alias)) {
375 char name[MAX_NAME_LEN];
376
377 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms[i].symbol, syms[i].alias);
378 strlist__add(evt_name_list, name);
379 } else
380 strlist__add(evt_name_list, syms[i].symbol);
381 }
382
383 strlist__for_each_entry(nd, evt_name_list) {
384 char *alias = strstr(nd->s, " OR ");
385
386 if (alias) {
387 *alias = '\0';
388 alias += 4;
389 }
390 print_cb->print_event(print_state,
391 /*topic=*/NULL,
392 /*pmu_name=*/NULL,
393 nd->s,
394 alias,
395 /*scale_unit=*/NULL,
396 /*deprecated=*/false,
397 event_type_descriptors[type],
398 /*desc=*/NULL,
399 /*long_desc=*/NULL,
400 /*encoding_desc=*/NULL);
401 }
402 strlist__delete(evt_name_list);
403 }
404
405 /*
406 * Print the help text for the event symbols:
407 */
print_events(const struct print_callbacks * print_cb,void * print_state)408 void print_events(const struct print_callbacks *print_cb, void *print_state)
409 {
410 print_symbol_events(print_cb, print_state, PERF_TYPE_HARDWARE,
411 event_symbols_hw, PERF_COUNT_HW_MAX);
412 print_symbol_events(print_cb, print_state, PERF_TYPE_SOFTWARE,
413 event_symbols_sw, PERF_COUNT_SW_MAX);
414
415 print_tool_events(print_cb, print_state);
416
417 print_hwcache_events(print_cb, print_state);
418
419 perf_pmus__print_pmu_events(print_cb, print_state);
420
421 print_cb->print_event(print_state,
422 /*topic=*/NULL,
423 /*pmu_name=*/NULL,
424 "rNNN",
425 /*event_alias=*/NULL,
426 /*scale_unit=*/NULL,
427 /*deprecated=*/false,
428 event_type_descriptors[PERF_TYPE_RAW],
429 /*desc=*/NULL,
430 /*long_desc=*/NULL,
431 /*encoding_desc=*/NULL);
432
433 print_cb->print_event(print_state,
434 /*topic=*/NULL,
435 /*pmu_name=*/NULL,
436 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
437 /*event_alias=*/NULL,
438 /*scale_unit=*/NULL,
439 /*deprecated=*/false,
440 event_type_descriptors[PERF_TYPE_RAW],
441 "(see 'man perf-list' on how to encode it)",
442 /*long_desc=*/NULL,
443 /*encoding_desc=*/NULL);
444
445 print_cb->print_event(print_state,
446 /*topic=*/NULL,
447 /*pmu_name=*/NULL,
448 "mem:<addr>[/len][:access]",
449 /*scale_unit=*/NULL,
450 /*event_alias=*/NULL,
451 /*deprecated=*/false,
452 event_type_descriptors[PERF_TYPE_BREAKPOINT],
453 /*desc=*/NULL,
454 /*long_desc=*/NULL,
455 /*encoding_desc=*/NULL);
456
457 print_tracepoint_events(print_cb, print_state);
458
459 print_sdt_events(print_cb, print_state);
460
461 metricgroup__print(print_cb, print_state);
462
463 print_libpfm_events(print_cb, print_state);
464 }
465