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