xref: /openbmc/linux/tools/perf/util/print-events.c (revision f88698d6)
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__delete(evsel);
275 	}
276 
277 	perf_thread_map__put(tmap);
278 	return ret;
279 }
280 
print_hwcache_events(const struct print_callbacks * print_cb,void * print_state)281 int print_hwcache_events(const struct print_callbacks *print_cb, void *print_state)
282 {
283 	struct perf_pmu *pmu = NULL;
284 	const char *event_type_descriptor = event_type_descriptors[PERF_TYPE_HW_CACHE];
285 
286 	/*
287 	 * Only print core PMUs, skipping uncore for performance and
288 	 * PERF_TYPE_SOFTWARE that can succeed in opening legacy cache evenst.
289 	 */
290 	while ((pmu = perf_pmus__scan_core(pmu)) != NULL) {
291 		if (pmu->is_uncore || pmu->type == PERF_TYPE_SOFTWARE)
292 			continue;
293 
294 		for (int type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
295 			for (int op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
296 				/* skip invalid cache type */
297 				if (!evsel__is_cache_op_valid(type, op))
298 					continue;
299 
300 				for (int res = 0; res < PERF_COUNT_HW_CACHE_RESULT_MAX; res++) {
301 					char name[64];
302 					char alias_name[128];
303 					__u64 config;
304 					int ret;
305 
306 					__evsel__hw_cache_type_op_res_name(type, op, res,
307 									name, sizeof(name));
308 
309 					ret = parse_events__decode_legacy_cache(name, pmu->type,
310 										&config);
311 					if (ret || !is_event_supported(PERF_TYPE_HW_CACHE, config))
312 						continue;
313 					snprintf(alias_name, sizeof(alias_name), "%s/%s/",
314 						 pmu->name, name);
315 					print_cb->print_event(print_state,
316 							"cache",
317 							pmu->name,
318 							name,
319 							alias_name,
320 							/*scale_unit=*/NULL,
321 							/*deprecated=*/false,
322 							event_type_descriptor,
323 							/*desc=*/NULL,
324 							/*long_desc=*/NULL,
325 							/*encoding_desc=*/NULL);
326 				}
327 			}
328 		}
329 	}
330 	return 0;
331 }
332 
print_tool_events(const struct print_callbacks * print_cb,void * print_state)333 void print_tool_events(const struct print_callbacks *print_cb, void *print_state)
334 {
335 	// Start at 1 because the first enum entry means no tool event.
336 	for (int i = 1; i < PERF_TOOL_MAX; ++i) {
337 		print_cb->print_event(print_state,
338 				"tool",
339 				/*pmu_name=*/NULL,
340 				event_symbols_tool[i].symbol,
341 				event_symbols_tool[i].alias,
342 				/*scale_unit=*/NULL,
343 				/*deprecated=*/false,
344 				"Tool event",
345 				/*desc=*/NULL,
346 				/*long_desc=*/NULL,
347 				/*encoding_desc=*/NULL);
348 	}
349 }
350 
print_symbol_events(const struct print_callbacks * print_cb,void * print_state,unsigned int type,const struct event_symbol * syms,unsigned int max)351 void print_symbol_events(const struct print_callbacks *print_cb, void *print_state,
352 			 unsigned int type, const struct event_symbol *syms,
353 			 unsigned int max)
354 {
355 	struct strlist *evt_name_list = strlist__new(NULL, NULL);
356 	struct str_node *nd;
357 
358 	if (!evt_name_list) {
359 		pr_debug("Failed to allocate new strlist for symbol events\n");
360 		return;
361 	}
362 	for (unsigned int i = 0; i < max; i++) {
363 		/*
364 		 * New attr.config still not supported here, the latest
365 		 * example was PERF_COUNT_SW_CGROUP_SWITCHES
366 		 */
367 		if (syms[i].symbol == NULL)
368 			continue;
369 
370 		if (!is_event_supported(type, i))
371 			continue;
372 
373 		if (strlen(syms[i].alias)) {
374 			char name[MAX_NAME_LEN];
375 
376 			snprintf(name, MAX_NAME_LEN, "%s OR %s", syms[i].symbol, syms[i].alias);
377 			strlist__add(evt_name_list, name);
378 		} else
379 			strlist__add(evt_name_list, syms[i].symbol);
380 	}
381 
382 	strlist__for_each_entry(nd, evt_name_list) {
383 		char *alias = strstr(nd->s, " OR ");
384 
385 		if (alias) {
386 			*alias = '\0';
387 			alias += 4;
388 		}
389 		print_cb->print_event(print_state,
390 				/*topic=*/NULL,
391 				/*pmu_name=*/NULL,
392 				nd->s,
393 				alias,
394 				/*scale_unit=*/NULL,
395 				/*deprecated=*/false,
396 				event_type_descriptors[type],
397 				/*desc=*/NULL,
398 				/*long_desc=*/NULL,
399 				/*encoding_desc=*/NULL);
400 	}
401 	strlist__delete(evt_name_list);
402 }
403 
404 /*
405  * Print the help text for the event symbols:
406  */
print_events(const struct print_callbacks * print_cb,void * print_state)407 void print_events(const struct print_callbacks *print_cb, void *print_state)
408 {
409 	print_symbol_events(print_cb, print_state, PERF_TYPE_HARDWARE,
410 			event_symbols_hw, PERF_COUNT_HW_MAX);
411 	print_symbol_events(print_cb, print_state, PERF_TYPE_SOFTWARE,
412 			event_symbols_sw, PERF_COUNT_SW_MAX);
413 
414 	print_tool_events(print_cb, print_state);
415 
416 	print_hwcache_events(print_cb, print_state);
417 
418 	perf_pmus__print_pmu_events(print_cb, print_state);
419 
420 	print_cb->print_event(print_state,
421 			/*topic=*/NULL,
422 			/*pmu_name=*/NULL,
423 			"rNNN",
424 			/*event_alias=*/NULL,
425 			/*scale_unit=*/NULL,
426 			/*deprecated=*/false,
427 			event_type_descriptors[PERF_TYPE_RAW],
428 			/*desc=*/NULL,
429 			/*long_desc=*/NULL,
430 			/*encoding_desc=*/NULL);
431 
432 	print_cb->print_event(print_state,
433 			/*topic=*/NULL,
434 			/*pmu_name=*/NULL,
435 			"cpu/t1=v1[,t2=v2,t3 ...]/modifier",
436 			/*event_alias=*/NULL,
437 			/*scale_unit=*/NULL,
438 			/*deprecated=*/false,
439 			event_type_descriptors[PERF_TYPE_RAW],
440 			"(see 'man perf-list' on how to encode it)",
441 			/*long_desc=*/NULL,
442 			/*encoding_desc=*/NULL);
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