xref: /openbmc/linux/tools/perf/util/print-events.c (revision 313c4281)
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 /** Strings corresponding to enum perf_type_id. */
32 static const char * const event_type_descriptors[] = {
33 	"Hardware event",
34 	"Software event",
35 	"Tracepoint event",
36 	"Hardware cache event",
37 	"Raw hardware event descriptor",
38 	"Hardware breakpoint",
39 };
40 
41 static const struct event_symbol event_symbols_tool[PERF_TOOL_MAX] = {
42 	[PERF_TOOL_DURATION_TIME] = {
43 		.symbol = "duration_time",
44 		.alias  = "",
45 	},
46 	[PERF_TOOL_USER_TIME] = {
47 		.symbol = "user_time",
48 		.alias  = "",
49 	},
50 	[PERF_TOOL_SYSTEM_TIME] = {
51 		.symbol = "system_time",
52 		.alias  = "",
53 	},
54 };
55 
56 /*
57  * Print the events from <debugfs_mount_point>/tracing/events
58  */
59 void print_tracepoint_events(const struct print_callbacks *print_cb, void *print_state)
60 {
61 	struct dirent **sys_namelist = NULL;
62 	int sys_items = tracing_events__scandir_alphasort(&sys_namelist);
63 
64 	for (int i = 0; i < sys_items; i++) {
65 		struct dirent *sys_dirent = sys_namelist[i];
66 		struct dirent **evt_namelist = NULL;
67 		char *dir_path;
68 		int evt_items;
69 
70 		if (sys_dirent->d_type != DT_DIR ||
71 		    !strcmp(sys_dirent->d_name, ".") ||
72 		    !strcmp(sys_dirent->d_name, ".."))
73 			continue;
74 
75 		dir_path = get_events_file(sys_dirent->d_name);
76 		if (!dir_path)
77 			continue;
78 
79 		evt_items = scandir(dir_path, &evt_namelist, NULL, alphasort);
80 		for (int j = 0; j < evt_items; j++) {
81 			struct dirent *evt_dirent = evt_namelist[j];
82 			char evt_path[MAXPATHLEN];
83 
84 			if (evt_dirent->d_type != DT_DIR ||
85 			    !strcmp(evt_dirent->d_name, ".") ||
86 			    !strcmp(evt_dirent->d_name, ".."))
87 				continue;
88 
89 			if (tp_event_has_id(dir_path, evt_dirent) != 0)
90 				continue;
91 
92 			snprintf(evt_path, MAXPATHLEN, "%s:%s",
93 				 sys_dirent->d_name, evt_dirent->d_name);
94 			print_cb->print_event(print_state,
95 					/*topic=*/NULL,
96 					/*pmu_name=*/NULL,
97 					evt_path,
98 					/*event_alias=*/NULL,
99 					/*scale_unit=*/NULL,
100 					/*deprecated=*/false,
101 					"Tracepoint event",
102 					/*desc=*/NULL,
103 					/*long_desc=*/NULL,
104 					/*encoding_desc=*/NULL);
105 		}
106 		free(dir_path);
107 		free(evt_namelist);
108 	}
109 	free(sys_namelist);
110 }
111 
112 void print_sdt_events(const struct print_callbacks *print_cb, void *print_state)
113 {
114 	struct strlist *bidlist, *sdtlist;
115 	struct str_node *bid_nd, *sdt_name, *next_sdt_name;
116 	const char *last_sdt_name = NULL;
117 
118 	/*
119 	 * The implicitly sorted sdtlist will hold the tracepoint name followed
120 	 * by @<buildid>. If the tracepoint name is unique (determined by
121 	 * looking at the adjacent nodes) the @<buildid> is dropped otherwise
122 	 * the executable path and buildid are added to the name.
123 	 */
124 	sdtlist = strlist__new(NULL, NULL);
125 	if (!sdtlist) {
126 		pr_debug("Failed to allocate new strlist for SDT\n");
127 		return;
128 	}
129 	bidlist = build_id_cache__list_all(true);
130 	if (!bidlist) {
131 		pr_debug("Failed to get buildids: %d\n", errno);
132 		return;
133 	}
134 	strlist__for_each_entry(bid_nd, bidlist) {
135 		struct probe_cache *pcache;
136 		struct probe_cache_entry *ent;
137 
138 		pcache = probe_cache__new(bid_nd->s, NULL);
139 		if (!pcache)
140 			continue;
141 		list_for_each_entry(ent, &pcache->entries, node) {
142 			char buf[1024];
143 
144 			snprintf(buf, sizeof(buf), "%s:%s@%s",
145 				 ent->pev.group, ent->pev.event, bid_nd->s);
146 			strlist__add(sdtlist, buf);
147 		}
148 		probe_cache__delete(pcache);
149 	}
150 	strlist__delete(bidlist);
151 
152 	strlist__for_each_entry(sdt_name, sdtlist) {
153 		bool show_detail = false;
154 		char *bid = strchr(sdt_name->s, '@');
155 		char *evt_name = NULL;
156 
157 		if (bid)
158 			*(bid++) = '\0';
159 
160 		if (last_sdt_name && !strcmp(last_sdt_name, sdt_name->s)) {
161 			show_detail = true;
162 		} else {
163 			next_sdt_name = strlist__next(sdt_name);
164 			if (next_sdt_name) {
165 				char *bid2 = strchr(next_sdt_name->s, '@');
166 
167 				if (bid2)
168 					*bid2 = '\0';
169 				if (strcmp(sdt_name->s, next_sdt_name->s) == 0)
170 					show_detail = true;
171 				if (bid2)
172 					*bid2 = '@';
173 			}
174 		}
175 		last_sdt_name = sdt_name->s;
176 
177 		if (show_detail) {
178 			char *path = build_id_cache__origname(bid);
179 
180 			if (path) {
181 				if (asprintf(&evt_name, "%s@%s(%.12s)", sdt_name->s, path, bid) < 0)
182 					evt_name = NULL;
183 				free(path);
184 			}
185 		}
186 		print_cb->print_event(print_state,
187 				/*topic=*/NULL,
188 				/*pmu_name=*/NULL,
189 				evt_name ?: sdt_name->s,
190 				/*event_alias=*/NULL,
191 				/*deprecated=*/false,
192 				/*scale_unit=*/NULL,
193 				"SDT event",
194 				/*desc=*/NULL,
195 				/*long_desc=*/NULL,
196 				/*encoding_desc=*/NULL);
197 
198 		free(evt_name);
199 	}
200 	strlist__delete(sdtlist);
201 }
202 
203 int print_hwcache_events(const struct print_callbacks *print_cb, void *print_state)
204 {
205 	struct strlist *evt_name_list = strlist__new(NULL, NULL);
206 	struct str_node *nd;
207 
208 	if (!evt_name_list) {
209 		pr_debug("Failed to allocate new strlist for hwcache events\n");
210 		return -ENOMEM;
211 	}
212 	for (int type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
213 		for (int op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
214 			/* skip invalid cache type */
215 			if (!evsel__is_cache_op_valid(type, op))
216 				continue;
217 
218 			for (int i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
219 				struct perf_pmu *pmu = NULL;
220 				char name[64];
221 
222 				__evsel__hw_cache_type_op_res_name(type, op, i, name, sizeof(name));
223 				if (!perf_pmu__has_hybrid()) {
224 					if (is_event_supported(PERF_TYPE_HW_CACHE,
225 							       type | (op << 8) | (i << 16)))
226 						strlist__add(evt_name_list, name);
227 					continue;
228 				}
229 				perf_pmu__for_each_hybrid_pmu(pmu) {
230 					if (is_event_supported(PERF_TYPE_HW_CACHE,
231 					    type | (op << 8) | (i << 16) |
232 					    ((__u64)pmu->type << PERF_PMU_TYPE_SHIFT))) {
233 						char new_name[128];
234 							snprintf(new_name, sizeof(new_name),
235 								 "%s/%s/", pmu->name, name);
236 							strlist__add(evt_name_list, new_name);
237 					}
238 				}
239 			}
240 		}
241 	}
242 
243 	strlist__for_each_entry(nd, evt_name_list) {
244 		print_cb->print_event(print_state,
245 				"cache",
246 				/*pmu_name=*/NULL,
247 				nd->s,
248 				/*event_alias=*/NULL,
249 				/*scale_unit=*/NULL,
250 				/*deprecated=*/false,
251 				event_type_descriptors[PERF_TYPE_HW_CACHE],
252 				/*desc=*/NULL,
253 				/*long_desc=*/NULL,
254 				/*encoding_desc=*/NULL);
255 	}
256 	strlist__delete(evt_name_list);
257 	return 0;
258 }
259 
260 void print_tool_events(const struct print_callbacks *print_cb, void *print_state)
261 {
262 	// Start at 1 because the first enum entry means no tool event.
263 	for (int i = 1; i < PERF_TOOL_MAX; ++i) {
264 		print_cb->print_event(print_state,
265 				"tool",
266 				/*pmu_name=*/NULL,
267 				event_symbols_tool[i].symbol,
268 				event_symbols_tool[i].alias,
269 				/*scale_unit=*/NULL,
270 				/*deprecated=*/false,
271 				"Tool event",
272 				/*desc=*/NULL,
273 				/*long_desc=*/NULL,
274 				/*encoding_desc=*/NULL);
275 	}
276 }
277 
278 void print_symbol_events(const struct print_callbacks *print_cb, void *print_state,
279 			 unsigned int type, const struct event_symbol *syms,
280 			 unsigned int max)
281 {
282 	struct strlist *evt_name_list = strlist__new(NULL, NULL);
283 	struct str_node *nd;
284 
285 	if (!evt_name_list) {
286 		pr_debug("Failed to allocate new strlist for symbol events\n");
287 		return;
288 	}
289 	for (unsigned int i = 0; i < max; i++) {
290 		/*
291 		 * New attr.config still not supported here, the latest
292 		 * example was PERF_COUNT_SW_CGROUP_SWITCHES
293 		 */
294 		if (syms[i].symbol == NULL)
295 			continue;
296 
297 		if (!is_event_supported(type, i))
298 			continue;
299 
300 		if (strlen(syms[i].alias)) {
301 			char name[MAX_NAME_LEN];
302 
303 			snprintf(name, MAX_NAME_LEN, "%s OR %s", syms[i].symbol, syms[i].alias);
304 			strlist__add(evt_name_list, name);
305 		} else
306 			strlist__add(evt_name_list, syms[i].symbol);
307 	}
308 
309 	strlist__for_each_entry(nd, evt_name_list) {
310 		char *alias = strstr(nd->s, " OR ");
311 
312 		if (alias) {
313 			*alias = '\0';
314 			alias += 4;
315 		}
316 		print_cb->print_event(print_state,
317 				/*topic=*/NULL,
318 				/*pmu_name=*/NULL,
319 				nd->s,
320 				alias,
321 				/*scale_unit=*/NULL,
322 				/*deprecated=*/false,
323 				event_type_descriptors[type],
324 				/*desc=*/NULL,
325 				/*long_desc=*/NULL,
326 				/*encoding_desc=*/NULL);
327 	}
328 	strlist__delete(evt_name_list);
329 }
330 
331 /*
332  * Print the help text for the event symbols:
333  */
334 void print_events(const struct print_callbacks *print_cb, void *print_state)
335 {
336 	print_symbol_events(print_cb, print_state, PERF_TYPE_HARDWARE,
337 			event_symbols_hw, PERF_COUNT_HW_MAX);
338 	print_symbol_events(print_cb, print_state, PERF_TYPE_SOFTWARE,
339 			event_symbols_sw, PERF_COUNT_SW_MAX);
340 
341 	print_tool_events(print_cb, print_state);
342 
343 	print_hwcache_events(print_cb, print_state);
344 
345 	print_pmu_events(print_cb, print_state);
346 
347 	print_cb->print_event(print_state,
348 			/*topic=*/NULL,
349 			/*pmu_name=*/NULL,
350 			"rNNN",
351 			/*event_alias=*/NULL,
352 			/*scale_unit=*/NULL,
353 			/*deprecated=*/false,
354 			event_type_descriptors[PERF_TYPE_RAW],
355 			/*desc=*/NULL,
356 			/*long_desc=*/NULL,
357 			/*encoding_desc=*/NULL);
358 
359 	print_cb->print_event(print_state,
360 			/*topic=*/NULL,
361 			/*pmu_name=*/NULL,
362 			"cpu/t1=v1[,t2=v2,t3 ...]/modifier",
363 			/*event_alias=*/NULL,
364 			/*scale_unit=*/NULL,
365 			/*deprecated=*/false,
366 			event_type_descriptors[PERF_TYPE_RAW],
367 			"(see 'man perf-list' on how to encode it)",
368 			/*long_desc=*/NULL,
369 			/*encoding_desc=*/NULL);
370 
371 	print_cb->print_event(print_state,
372 			/*topic=*/NULL,
373 			/*pmu_name=*/NULL,
374 			"mem:<addr>[/len][:access]",
375 			/*scale_unit=*/NULL,
376 			/*event_alias=*/NULL,
377 			/*deprecated=*/false,
378 			event_type_descriptors[PERF_TYPE_BREAKPOINT],
379 			/*desc=*/NULL,
380 			/*long_desc=*/NULL,
381 			/*encoding_desc=*/NULL);
382 
383 	print_tracepoint_events(print_cb, print_state);
384 
385 	print_sdt_events(print_cb, print_state);
386 
387 	metricgroup__print(print_cb, print_state);
388 
389 	print_libpfm_events(print_cb, print_state);
390 }
391