1 // SPDX-License-Identifier: GPL-2.0
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>
20 #include "metricgroup.h"
21 #include "parse-events.h"
24 #include "print-events.h"
25 #include "probe-file.h"
28 #include "tracepoint.h"
30 #include "thread_map.h"
33 #define MAX_NAME_LEN 100
35 /** Strings corresponding to enum perf_type_id. */
36 static const char * const event_type_descriptors[] = {
40 "Hardware cache event",
41 "Raw hardware event descriptor",
42 "Hardware breakpoint",
45 static const struct event_symbol event_symbols_tool[PERF_TOOL_MAX] = {
46 [PERF_TOOL_DURATION_TIME] = {
47 .symbol = "duration_time",
50 [PERF_TOOL_USER_TIME] = {
51 .symbol = "user_time",
54 [PERF_TOOL_SYSTEM_TIME] = {
55 .symbol = "system_time",
61 * Print the events from <debugfs_mount_point>/tracing/events
63 void print_tracepoint_events(const struct print_callbacks *print_cb __maybe_unused, void *print_state __maybe_unused)
65 char *events_path = get_tracing_file("events");
66 int events_fd = open(events_path, O_PATH);
67 struct dirent **sys_namelist = NULL;
70 put_tracing_file(events_path);
72 pr_err("Error: failed to open tracing events directory\n");
76 sys_items = tracing_events__scandir_alphasort(&sys_namelist);
78 for (int i = 0; i < sys_items; i++) {
79 struct dirent *sys_dirent = sys_namelist[i];
80 struct dirent **evt_namelist = NULL;
84 if (sys_dirent->d_type != DT_DIR ||
85 !strcmp(sys_dirent->d_name, ".") ||
86 !strcmp(sys_dirent->d_name, ".."))
89 dir_fd = openat(events_fd, sys_dirent->d_name, O_PATH);
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];
99 if (evt_dirent->d_type != DT_DIR ||
100 !strcmp(evt_dirent->d_name, ".") ||
101 !strcmp(evt_dirent->d_name, ".."))
104 snprintf(evt_path, sizeof(evt_path), "%s/id", evt_dirent->d_name);
105 evt_fd = openat(dir_fd, evt_path, O_RDONLY);
110 snprintf(evt_path, MAXPATHLEN, "%s:%s",
111 sys_dirent->d_name, evt_dirent->d_name);
112 print_cb->print_event(print_state,
116 /*event_alias=*/NULL,
118 /*deprecated=*/false,
122 /*encoding_desc=*/NULL);
124 free(evt_namelist[j]);
129 free(sys_namelist[i]);
136 void print_sdt_events(const struct print_callbacks *print_cb, void *print_state)
138 struct strlist *bidlist, *sdtlist;
139 struct str_node *bid_nd, *sdt_name, *next_sdt_name;
140 const char *last_sdt_name = NULL;
143 * The implicitly sorted sdtlist will hold the tracepoint name followed
144 * by @<buildid>. If the tracepoint name is unique (determined by
145 * looking at the adjacent nodes) the @<buildid> is dropped otherwise
146 * the executable path and buildid are added to the name.
148 sdtlist = strlist__new(NULL, NULL);
150 pr_debug("Failed to allocate new strlist for SDT\n");
153 bidlist = build_id_cache__list_all(true);
155 pr_debug("Failed to get buildids: %d\n", errno);
158 strlist__for_each_entry(bid_nd, bidlist) {
159 struct probe_cache *pcache;
160 struct probe_cache_entry *ent;
162 pcache = probe_cache__new(bid_nd->s, NULL);
165 list_for_each_entry(ent, &pcache->entries, node) {
168 snprintf(buf, sizeof(buf), "%s:%s@%s",
169 ent->pev.group, ent->pev.event, bid_nd->s);
170 strlist__add(sdtlist, buf);
172 probe_cache__delete(pcache);
174 strlist__delete(bidlist);
176 strlist__for_each_entry(sdt_name, sdtlist) {
177 bool show_detail = false;
178 char *bid = strchr(sdt_name->s, '@');
179 char *evt_name = NULL;
184 if (last_sdt_name && !strcmp(last_sdt_name, sdt_name->s)) {
187 next_sdt_name = strlist__next(sdt_name);
189 char *bid2 = strchr(next_sdt_name->s, '@');
193 if (strcmp(sdt_name->s, next_sdt_name->s) == 0)
199 last_sdt_name = sdt_name->s;
202 char *path = build_id_cache__origname(bid);
205 if (asprintf(&evt_name, "%s@%s(%.12s)", sdt_name->s, path, bid) < 0)
210 print_cb->print_event(print_state,
213 evt_name ?: sdt_name->s,
214 /*event_alias=*/NULL,
215 /*deprecated=*/false,
220 /*encoding_desc=*/NULL);
224 strlist__delete(sdtlist);
227 bool is_event_supported(u8 type, u64 config)
231 struct perf_event_attr attr = {
236 struct perf_thread_map *tmap = thread_map__new_by_tid(0);
241 evsel = evsel__new(&attr);
243 ret = evsel__open(evsel, NULL, tmap) >= 0;
247 * The event may fail to open if the paranoid value
248 * /proc/sys/kernel/perf_event_paranoid is set to 2
249 * Re-run with exclude_kernel set; we don't do that by
250 * default as some ARM machines do not support it.
252 evsel->core.attr.exclude_kernel = 1;
253 ret = evsel__open(evsel, NULL, tmap) >= 0;
258 * The event may fail to open if the PMU requires
259 * exclude_guest to be set (e.g. as the Apple M1 PMU
261 * Re-run with exclude_guest set; we don't do that by
262 * default as it's equally legitimate for another PMU
263 * driver to require that exclude_guest is clear.
265 evsel->core.attr.exclude_guest = 1;
266 ret = evsel__open(evsel, NULL, tmap) >= 0;
269 evsel__delete(evsel);
272 perf_thread_map__put(tmap);
276 int print_hwcache_events(const struct print_callbacks *print_cb, void *print_state)
278 struct perf_pmu *pmu = NULL;
279 const char *event_type_descriptor = event_type_descriptors[PERF_TYPE_HW_CACHE];
282 * Only print core PMUs, skipping uncore for performance and
283 * PERF_TYPE_SOFTWARE that can succeed in opening legacy cache evenst.
285 while ((pmu = perf_pmus__scan_core(pmu)) != NULL) {
286 if (pmu->is_uncore || pmu->type == PERF_TYPE_SOFTWARE)
289 for (int type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
290 for (int op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
291 /* skip invalid cache type */
292 if (!evsel__is_cache_op_valid(type, op))
295 for (int res = 0; res < PERF_COUNT_HW_CACHE_RESULT_MAX; res++) {
297 char alias_name[128];
301 __evsel__hw_cache_type_op_res_name(type, op, res,
304 ret = parse_events__decode_legacy_cache(name, pmu->type,
306 if (ret || !is_event_supported(PERF_TYPE_HW_CACHE, config))
308 snprintf(alias_name, sizeof(alias_name), "%s/%s/",
310 print_cb->print_event(print_state,
316 /*deprecated=*/false,
317 event_type_descriptor,
320 /*encoding_desc=*/NULL);
328 void print_tool_events(const struct print_callbacks *print_cb, void *print_state)
330 // Start at 1 because the first enum entry means no tool event.
331 for (int i = 1; i < PERF_TOOL_MAX; ++i) {
332 print_cb->print_event(print_state,
335 event_symbols_tool[i].symbol,
336 event_symbols_tool[i].alias,
338 /*deprecated=*/false,
342 /*encoding_desc=*/NULL);
346 void print_symbol_events(const struct print_callbacks *print_cb, void *print_state,
347 unsigned int type, const struct event_symbol *syms,
350 struct strlist *evt_name_list = strlist__new(NULL, NULL);
353 if (!evt_name_list) {
354 pr_debug("Failed to allocate new strlist for symbol events\n");
357 for (unsigned int i = 0; i < max; i++) {
359 * New attr.config still not supported here, the latest
360 * example was PERF_COUNT_SW_CGROUP_SWITCHES
362 if (syms[i].symbol == NULL)
365 if (!is_event_supported(type, i))
368 if (strlen(syms[i].alias)) {
369 char name[MAX_NAME_LEN];
371 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms[i].symbol, syms[i].alias);
372 strlist__add(evt_name_list, name);
374 strlist__add(evt_name_list, syms[i].symbol);
377 strlist__for_each_entry(nd, evt_name_list) {
378 char *alias = strstr(nd->s, " OR ");
384 print_cb->print_event(print_state,
390 /*deprecated=*/false,
391 event_type_descriptors[type],
394 /*encoding_desc=*/NULL);
396 strlist__delete(evt_name_list);
400 * Print the help text for the event symbols:
402 void print_events(const struct print_callbacks *print_cb, void *print_state)
406 print_symbol_events(print_cb, print_state, PERF_TYPE_HARDWARE,
407 event_symbols_hw, PERF_COUNT_HW_MAX);
408 print_symbol_events(print_cb, print_state, PERF_TYPE_SOFTWARE,
409 event_symbols_sw, PERF_COUNT_SW_MAX);
411 print_tool_events(print_cb, print_state);
413 print_hwcache_events(print_cb, print_state);
415 perf_pmus__print_pmu_events(print_cb, print_state);
417 print_cb->print_event(print_state,
421 /*event_alias=*/NULL,
423 /*deprecated=*/false,
424 event_type_descriptors[PERF_TYPE_RAW],
427 /*encoding_desc=*/NULL);
429 if (asprintf(&tmp, "%s/t1=v1[,t2=v2,t3 ...]/modifier",
430 perf_pmus__scan_core(/*pmu=*/NULL)->name) > 0) {
431 print_cb->print_event(print_state,
435 /*event_alias=*/NULL,
437 /*deprecated=*/false,
438 event_type_descriptors[PERF_TYPE_RAW],
439 "(see 'man perf-list' on how to encode it)",
441 /*encoding_desc=*/NULL);
445 print_cb->print_event(print_state,
448 "mem:<addr>[/len][:access]",
450 /*event_alias=*/NULL,
451 /*deprecated=*/false,
452 event_type_descriptors[PERF_TYPE_BREAKPOINT],
455 /*encoding_desc=*/NULL);
457 print_tracepoint_events(print_cb, print_state);
459 print_sdt_events(print_cb, print_state);
461 metricgroup__print(print_cb, print_state);
463 print_libpfm_events(print_cb, print_state);