xref: /openbmc/linux/tools/perf/util/print-events.c (revision 1504b6f9)
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 <sys/param.h>
8 
9 #include <api/fs/tracing_path.h>
10 #include <linux/stddef.h>
11 #include <linux/perf_event.h>
12 #include <linux/zalloc.h>
13 #include <subcmd/pager.h>
14 
15 #include "build-id.h"
16 #include "debug.h"
17 #include "evsel.h"
18 #include "metricgroup.h"
19 #include "parse-events.h"
20 #include "pmu.h"
21 #include "print-events.h"
22 #include "probe-file.h"
23 #include "string2.h"
24 #include "strlist.h"
25 #include "tracepoint.h"
26 #include "pfm.h"
27 #include "pmu-hybrid.h"
28 
29 #define MAX_NAME_LEN 100
30 
31 static const char * const event_type_descriptors[] = {
32 	"Hardware event",
33 	"Software event",
34 	"Tracepoint event",
35 	"Hardware cache event",
36 	"Raw hardware event descriptor",
37 	"Hardware breakpoint",
38 };
39 
40 static const struct event_symbol event_symbols_tool[PERF_TOOL_MAX] = {
41 	[PERF_TOOL_DURATION_TIME] = {
42 		.symbol = "duration_time",
43 		.alias  = "",
44 	},
45 	[PERF_TOOL_USER_TIME] = {
46 		.symbol = "user_time",
47 		.alias  = "",
48 	},
49 	[PERF_TOOL_SYSTEM_TIME] = {
50 		.symbol = "system_time",
51 		.alias  = "",
52 	},
53 };
54 
55 static int cmp_string(const void *a, const void *b)
56 {
57 	const char * const *as = a;
58 	const char * const *bs = b;
59 
60 	return strcmp(*as, *bs);
61 }
62 
63 /*
64  * Print the events from <debugfs_mount_point>/tracing/events
65  */
66 void print_tracepoint_events(const char *subsys_glob,
67 			     const char *event_glob, bool name_only)
68 {
69 	DIR *sys_dir, *evt_dir;
70 	struct dirent *sys_dirent, *evt_dirent;
71 	char evt_path[MAXPATHLEN];
72 	char *dir_path;
73 	char **evt_list = NULL;
74 	unsigned int evt_i = 0, evt_num = 0;
75 	bool evt_num_known = false;
76 
77 restart:
78 	sys_dir = tracing_events__opendir();
79 	if (!sys_dir)
80 		return;
81 
82 	if (evt_num_known) {
83 		evt_list = zalloc(sizeof(char *) * evt_num);
84 		if (!evt_list)
85 			goto out_close_sys_dir;
86 	}
87 
88 	for_each_subsystem(sys_dir, sys_dirent) {
89 		if (subsys_glob != NULL &&
90 		    !strglobmatch(sys_dirent->d_name, subsys_glob))
91 			continue;
92 
93 		dir_path = get_events_file(sys_dirent->d_name);
94 		if (!dir_path)
95 			continue;
96 		evt_dir = opendir(dir_path);
97 		if (!evt_dir)
98 			goto next;
99 
100 		for_each_event(dir_path, evt_dir, evt_dirent) {
101 			if (event_glob != NULL &&
102 			    !strglobmatch(evt_dirent->d_name, event_glob))
103 				continue;
104 
105 			if (!evt_num_known) {
106 				evt_num++;
107 				continue;
108 			}
109 
110 			snprintf(evt_path, MAXPATHLEN, "%s:%s",
111 				 sys_dirent->d_name, evt_dirent->d_name);
112 
113 			evt_list[evt_i] = strdup(evt_path);
114 			if (evt_list[evt_i] == NULL) {
115 				put_events_file(dir_path);
116 				goto out_close_evt_dir;
117 			}
118 			evt_i++;
119 		}
120 		closedir(evt_dir);
121 next:
122 		put_events_file(dir_path);
123 	}
124 	closedir(sys_dir);
125 
126 	if (!evt_num_known) {
127 		evt_num_known = true;
128 		goto restart;
129 	}
130 	qsort(evt_list, evt_num, sizeof(char *), cmp_string);
131 	evt_i = 0;
132 	while (evt_i < evt_num) {
133 		if (name_only) {
134 			printf("%s ", evt_list[evt_i++]);
135 			continue;
136 		}
137 		printf("  %-50s [%s]\n", evt_list[evt_i++],
138 				event_type_descriptors[PERF_TYPE_TRACEPOINT]);
139 	}
140 	if (evt_num && pager_in_use())
141 		printf("\n");
142 
143 out_free:
144 	evt_num = evt_i;
145 	for (evt_i = 0; evt_i < evt_num; evt_i++)
146 		zfree(&evt_list[evt_i]);
147 	zfree(&evt_list);
148 	return;
149 
150 out_close_evt_dir:
151 	closedir(evt_dir);
152 out_close_sys_dir:
153 	closedir(sys_dir);
154 
155 	printf("FATAL: not enough memory to print %s\n",
156 			event_type_descriptors[PERF_TYPE_TRACEPOINT]);
157 	if (evt_list)
158 		goto out_free;
159 }
160 
161 void print_sdt_events(const char *subsys_glob, const char *event_glob,
162 		      bool name_only)
163 {
164 	struct probe_cache *pcache;
165 	struct probe_cache_entry *ent;
166 	struct strlist *bidlist, *sdtlist;
167 	struct strlist_config cfg = {.dont_dupstr = true};
168 	struct str_node *nd, *nd2;
169 	char *buf, *path, *ptr = NULL;
170 	bool show_detail = false;
171 	int ret;
172 
173 	sdtlist = strlist__new(NULL, &cfg);
174 	if (!sdtlist) {
175 		pr_debug("Failed to allocate new strlist for SDT\n");
176 		return;
177 	}
178 	bidlist = build_id_cache__list_all(true);
179 	if (!bidlist) {
180 		pr_debug("Failed to get buildids: %d\n", errno);
181 		return;
182 	}
183 	strlist__for_each_entry(nd, bidlist) {
184 		pcache = probe_cache__new(nd->s, NULL);
185 		if (!pcache)
186 			continue;
187 		list_for_each_entry(ent, &pcache->entries, node) {
188 			if (!ent->sdt)
189 				continue;
190 			if (subsys_glob &&
191 			    !strglobmatch(ent->pev.group, subsys_glob))
192 				continue;
193 			if (event_glob &&
194 			    !strglobmatch(ent->pev.event, event_glob))
195 				continue;
196 			ret = asprintf(&buf, "%s:%s@%s", ent->pev.group,
197 					ent->pev.event, nd->s);
198 			if (ret > 0)
199 				strlist__add(sdtlist, buf);
200 		}
201 		probe_cache__delete(pcache);
202 	}
203 	strlist__delete(bidlist);
204 
205 	strlist__for_each_entry(nd, sdtlist) {
206 		buf = strchr(nd->s, '@');
207 		if (buf)
208 			*(buf++) = '\0';
209 		if (name_only) {
210 			printf("%s ", nd->s);
211 			continue;
212 		}
213 		nd2 = strlist__next(nd);
214 		if (nd2) {
215 			ptr = strchr(nd2->s, '@');
216 			if (ptr)
217 				*ptr = '\0';
218 			if (strcmp(nd->s, nd2->s) == 0)
219 				show_detail = true;
220 		}
221 		if (show_detail) {
222 			path = build_id_cache__origname(buf);
223 			ret = asprintf(&buf, "%s@%s(%.12s)", nd->s, path, buf);
224 			if (ret > 0) {
225 				printf("  %-50s [%s]\n", buf, "SDT event");
226 				free(buf);
227 			}
228 			free(path);
229 		} else
230 			printf("  %-50s [%s]\n", nd->s, "SDT event");
231 		if (nd2) {
232 			if (strcmp(nd->s, nd2->s) != 0)
233 				show_detail = false;
234 			if (ptr)
235 				*ptr = '@';
236 		}
237 	}
238 	strlist__delete(sdtlist);
239 }
240 
241 int print_hwcache_events(const char *event_glob, bool name_only)
242 {
243 	unsigned int type, op, i, evt_i = 0, evt_num = 0, npmus = 0;
244 	char name[64], new_name[128];
245 	char **evt_list = NULL, **evt_pmus = NULL;
246 	bool evt_num_known = false;
247 	struct perf_pmu *pmu = NULL;
248 
249 	if (perf_pmu__has_hybrid()) {
250 		npmus = perf_pmu__hybrid_pmu_num();
251 		evt_pmus = zalloc(sizeof(char *) * npmus);
252 		if (!evt_pmus)
253 			goto out_enomem;
254 	}
255 
256 restart:
257 	if (evt_num_known) {
258 		evt_list = zalloc(sizeof(char *) * evt_num);
259 		if (!evt_list)
260 			goto out_enomem;
261 	}
262 
263 	for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
264 		for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
265 			/* skip invalid cache type */
266 			if (!evsel__is_cache_op_valid(type, op))
267 				continue;
268 
269 			for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
270 				unsigned int hybrid_supported = 0, j;
271 				bool supported;
272 
273 				__evsel__hw_cache_type_op_res_name(type, op, i, name, sizeof(name));
274 				if (event_glob != NULL && !strglobmatch(name, event_glob))
275 					continue;
276 
277 				if (!perf_pmu__has_hybrid()) {
278 					if (!is_event_supported(PERF_TYPE_HW_CACHE,
279 								type | (op << 8) | (i << 16))) {
280 						continue;
281 					}
282 				} else {
283 					perf_pmu__for_each_hybrid_pmu(pmu) {
284 						if (!evt_num_known) {
285 							evt_num++;
286 							continue;
287 						}
288 
289 						supported = is_event_supported(
290 							PERF_TYPE_HW_CACHE,
291 							type | (op << 8) | (i << 16) |
292 							((__u64)pmu->type << PERF_PMU_TYPE_SHIFT));
293 						if (supported) {
294 							snprintf(new_name, sizeof(new_name),
295 								 "%s/%s/", pmu->name, name);
296 							evt_pmus[hybrid_supported] =
297 								strdup(new_name);
298 							hybrid_supported++;
299 						}
300 					}
301 
302 					if (hybrid_supported == 0)
303 						continue;
304 				}
305 
306 				if (!evt_num_known) {
307 					evt_num++;
308 					continue;
309 				}
310 
311 				if ((hybrid_supported == 0) ||
312 				    (hybrid_supported == npmus)) {
313 					evt_list[evt_i] = strdup(name);
314 					for (j = 0; j < npmus; j++)
315 						zfree(&evt_pmus[j]);
316 				} else {
317 					for (j = 0; j < hybrid_supported; j++) {
318 						evt_list[evt_i++] = evt_pmus[j];
319 						evt_pmus[j] = NULL;
320 					}
321 					continue;
322 				}
323 
324 				if (evt_list[evt_i] == NULL)
325 					goto out_enomem;
326 				evt_i++;
327 			}
328 		}
329 	}
330 
331 	if (!evt_num_known) {
332 		evt_num_known = true;
333 		goto restart;
334 	}
335 
336 	for (evt_i = 0; evt_i < evt_num; evt_i++) {
337 		if (!evt_list[evt_i])
338 			break;
339 	}
340 
341 	evt_num = evt_i;
342 	qsort(evt_list, evt_num, sizeof(char *), cmp_string);
343 	evt_i = 0;
344 	while (evt_i < evt_num) {
345 		if (name_only) {
346 			printf("%s ", evt_list[evt_i++]);
347 			continue;
348 		}
349 		printf("  %-50s [%s]\n", evt_list[evt_i++],
350 				event_type_descriptors[PERF_TYPE_HW_CACHE]);
351 	}
352 	if (evt_num && pager_in_use())
353 		printf("\n");
354 
355 out_free:
356 	evt_num = evt_i;
357 	for (evt_i = 0; evt_i < evt_num; evt_i++)
358 		zfree(&evt_list[evt_i]);
359 	zfree(&evt_list);
360 
361 	for (evt_i = 0; evt_i < npmus; evt_i++)
362 		zfree(&evt_pmus[evt_i]);
363 	zfree(&evt_pmus);
364 	return evt_num;
365 
366 out_enomem:
367 	printf("FATAL: not enough memory to print %s\n",
368 		event_type_descriptors[PERF_TYPE_HW_CACHE]);
369 	if (evt_list)
370 		goto out_free;
371 	return evt_num;
372 }
373 
374 static void print_tool_event(const struct event_symbol *syms, const char *event_glob,
375 			     bool name_only)
376 {
377 	if (syms->symbol == NULL)
378 		return;
379 
380 	if (event_glob && !(strglobmatch(syms->symbol, event_glob) ||
381 	      (syms->alias && strglobmatch(syms->alias, event_glob))))
382 		return;
383 
384 	if (name_only)
385 		printf("%s ", syms->symbol);
386 	else {
387 		char name[MAX_NAME_LEN];
388 
389 		if (syms->alias && strlen(syms->alias))
390 			snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
391 		else
392 			strlcpy(name, syms->symbol, MAX_NAME_LEN);
393 		printf("  %-50s [%s]\n", name, "Tool event");
394 	}
395 }
396 
397 void print_tool_events(const char *event_glob, bool name_only)
398 {
399 	// Start at 1 because the first enum entry means no tool event.
400 	for (int i = 1; i < PERF_TOOL_MAX; ++i)
401 		print_tool_event(event_symbols_tool + i, event_glob, name_only);
402 
403 	if (pager_in_use())
404 		printf("\n");
405 }
406 
407 void print_symbol_events(const char *event_glob, unsigned int type,
408 			 struct event_symbol *syms, unsigned int max,
409 			 bool name_only)
410 {
411 	unsigned int i, evt_i = 0, evt_num = 0;
412 	char name[MAX_NAME_LEN];
413 	char **evt_list = NULL;
414 	bool evt_num_known = false;
415 
416 restart:
417 	if (evt_num_known) {
418 		evt_list = zalloc(sizeof(char *) * evt_num);
419 		if (!evt_list)
420 			goto out_enomem;
421 		syms -= max;
422 	}
423 
424 	for (i = 0; i < max; i++, syms++) {
425 		/*
426 		 * New attr.config still not supported here, the latest
427 		 * example was PERF_COUNT_SW_CGROUP_SWITCHES
428 		 */
429 		if (syms->symbol == NULL)
430 			continue;
431 
432 		if (event_glob != NULL && !(strglobmatch(syms->symbol, event_glob) ||
433 		      (syms->alias && strglobmatch(syms->alias, event_glob))))
434 			continue;
435 
436 		if (!is_event_supported(type, i))
437 			continue;
438 
439 		if (!evt_num_known) {
440 			evt_num++;
441 			continue;
442 		}
443 
444 		if (!name_only && strlen(syms->alias))
445 			snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
446 		else
447 			strlcpy(name, syms->symbol, MAX_NAME_LEN);
448 
449 		evt_list[evt_i] = strdup(name);
450 		if (evt_list[evt_i] == NULL)
451 			goto out_enomem;
452 		evt_i++;
453 	}
454 
455 	if (!evt_num_known) {
456 		evt_num_known = true;
457 		goto restart;
458 	}
459 	qsort(evt_list, evt_num, sizeof(char *), cmp_string);
460 	evt_i = 0;
461 	while (evt_i < evt_num) {
462 		if (name_only) {
463 			printf("%s ", evt_list[evt_i++]);
464 			continue;
465 		}
466 		printf("  %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
467 	}
468 	if (evt_num && pager_in_use())
469 		printf("\n");
470 
471 out_free:
472 	evt_num = evt_i;
473 	for (evt_i = 0; evt_i < evt_num; evt_i++)
474 		zfree(&evt_list[evt_i]);
475 	zfree(&evt_list);
476 	return;
477 
478 out_enomem:
479 	printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
480 	if (evt_list)
481 		goto out_free;
482 }
483 
484 /*
485  * Print the help text for the event symbols:
486  */
487 void print_events(const char *event_glob, bool name_only, bool quiet_flag,
488 			bool long_desc, bool details_flag, bool deprecated,
489 			const char *pmu_name)
490 {
491 	print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
492 			    event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
493 
494 	print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
495 			    event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
496 	print_tool_events(event_glob, name_only);
497 
498 	print_hwcache_events(event_glob, name_only);
499 
500 	print_pmu_events(event_glob, name_only, quiet_flag, long_desc,
501 			details_flag, deprecated, pmu_name);
502 
503 	if (event_glob != NULL)
504 		return;
505 
506 	if (!name_only) {
507 		printf("  %-50s [%s]\n",
508 		       "rNNN",
509 		       event_type_descriptors[PERF_TYPE_RAW]);
510 		printf("  %-50s [%s]\n",
511 		       "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
512 		       event_type_descriptors[PERF_TYPE_RAW]);
513 		if (pager_in_use())
514 			printf("   (see 'man perf-list' on how to encode it)\n\n");
515 
516 		printf("  %-50s [%s]\n",
517 		       "mem:<addr>[/len][:access]",
518 			event_type_descriptors[PERF_TYPE_BREAKPOINT]);
519 		if (pager_in_use())
520 			printf("\n");
521 	}
522 
523 	print_tracepoint_events(NULL, NULL, name_only);
524 
525 	print_sdt_events(NULL, NULL, name_only);
526 
527 	metricgroup__print(true, true, NULL, name_only, details_flag,
528 			   pmu_name);
529 
530 	print_libpfm_events(name_only, long_desc);
531 }
532