xref: /linux/tools/perf/util/print-events.c (revision 9e906a9dead17d81d6c2687f65e159231d0e3286)
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 "tool_pmu.h"
33 #include "util.h"
34 
35 #define MAX_NAME_LEN 100
36 
37 /** Strings corresponding to enum perf_type_id. */
38 static const char * const event_type_descriptors[] = {
39 	"Hardware event",
40 	"Software event",
41 	"Tracepoint event",
42 	"Hardware cache event",
43 	"Raw event descriptor",
44 	"Hardware breakpoint",
45 };
46 
print_sdt_events(const struct print_callbacks * print_cb,void * print_state)47 void print_sdt_events(const struct print_callbacks *print_cb, void *print_state)
48 {
49 	struct strlist *bidlist, *sdtlist;
50 	struct str_node *bid_nd, *sdt_name, *next_sdt_name;
51 	const char *last_sdt_name = NULL;
52 
53 	/*
54 	 * The implicitly sorted sdtlist will hold the tracepoint name followed
55 	 * by @<buildid>. If the tracepoint name is unique (determined by
56 	 * looking at the adjacent nodes) the @<buildid> is dropped otherwise
57 	 * the executable path and buildid are added to the name.
58 	 */
59 	sdtlist = strlist__new(NULL, NULL);
60 	if (!sdtlist) {
61 		pr_debug("Failed to allocate new strlist for SDT\n");
62 		return;
63 	}
64 	bidlist = build_id_cache__list_all(true);
65 	if (!bidlist) {
66 		pr_debug("Failed to get buildids: %d\n", errno);
67 		return;
68 	}
69 	strlist__for_each_entry(bid_nd, bidlist) {
70 		struct probe_cache *pcache;
71 		struct probe_cache_entry *ent;
72 
73 		pcache = probe_cache__new(bid_nd->s, NULL);
74 		if (!pcache)
75 			continue;
76 		list_for_each_entry(ent, &pcache->entries, node) {
77 			char buf[1024];
78 
79 			snprintf(buf, sizeof(buf), "%s:%s@%s",
80 				 ent->pev.group, ent->pev.event, bid_nd->s);
81 			strlist__add(sdtlist, buf);
82 		}
83 		probe_cache__delete(pcache);
84 	}
85 	strlist__delete(bidlist);
86 
87 	strlist__for_each_entry(sdt_name, sdtlist) {
88 		bool show_detail = false;
89 		char *bid = strchr(sdt_name->s, '@');
90 		char *evt_name = NULL;
91 
92 		if (bid)
93 			*(bid++) = '\0';
94 
95 		if (last_sdt_name && !strcmp(last_sdt_name, sdt_name->s)) {
96 			show_detail = true;
97 		} else {
98 			next_sdt_name = strlist__next(sdt_name);
99 			if (next_sdt_name) {
100 				char *bid2 = strchr(next_sdt_name->s, '@');
101 
102 				if (bid2)
103 					*bid2 = '\0';
104 				if (strcmp(sdt_name->s, next_sdt_name->s) == 0)
105 					show_detail = true;
106 				if (bid2)
107 					*bid2 = '@';
108 			}
109 		}
110 		last_sdt_name = sdt_name->s;
111 
112 		if (show_detail) {
113 			char *path = build_id_cache__origname(bid);
114 
115 			if (path) {
116 				if (asprintf(&evt_name, "%s@%s(%.12s)", sdt_name->s, path, bid) < 0)
117 					evt_name = NULL;
118 				free(path);
119 			}
120 		}
121 		print_cb->print_event(print_state,
122 				/*topic=*/NULL,
123 				/*pmu_name=*/NULL,
124 				PERF_TYPE_TRACEPOINT,
125 				evt_name ?: sdt_name->s,
126 				/*event_alias=*/NULL,
127 				/*deprecated=*/false,
128 				/*scale_unit=*/NULL,
129 				"SDT event",
130 				/*desc=*/NULL,
131 				/*long_desc=*/NULL,
132 				/*encoding_desc=*/NULL);
133 
134 		free(evt_name);
135 	}
136 	strlist__delete(sdtlist);
137 }
138 
is_event_supported(u8 type,u64 config)139 bool is_event_supported(u8 type, u64 config)
140 {
141 	bool ret = true;
142 	struct evsel *evsel;
143 	struct perf_event_attr attr = {
144 		.type = type,
145 		.config = config,
146 		.disabled = 1,
147 	};
148 	struct perf_thread_map *tmap = thread_map__new_by_tid(0);
149 
150 	if (tmap == NULL)
151 		return false;
152 
153 	evsel = evsel__new(&attr);
154 	if (evsel) {
155 		ret = evsel__open(evsel, NULL, tmap) >= 0;
156 
157 		if (!ret) {
158 			/*
159 			 * The event may fail to open if the paranoid value
160 			 * /proc/sys/kernel/perf_event_paranoid is set to 2
161 			 * Re-run with exclude_kernel set; we don't do that by
162 			 * default as some ARM machines do not support it.
163 			 */
164 			evsel->core.attr.exclude_kernel = 1;
165 			ret = evsel__open(evsel, NULL, tmap) >= 0;
166 		}
167 
168 		if (!ret) {
169 			/*
170 			 * The event may fail to open if the PMU requires
171 			 * exclude_guest to be set (e.g. as the Apple M1 PMU
172 			 * requires).
173 			 * Re-run with exclude_guest set; we don't do that by
174 			 * default as it's equally legitimate for another PMU
175 			 * driver to require that exclude_guest is clear.
176 			 */
177 			evsel->core.attr.exclude_guest = 1;
178 			ret = evsel__open(evsel, NULL, tmap) >= 0;
179 		}
180 
181 		evsel__close(evsel);
182 		evsel__delete(evsel);
183 	}
184 
185 	perf_thread_map__put(tmap);
186 	return ret;
187 }
188 
189 /** struct mep - RB-tree node for building printing information. */
190 struct mep {
191 	/** nd - RB-tree element. */
192 	struct rb_node nd;
193 	/** @metric_group: Owned metric group name, separated others with ';'. */
194 	char *metric_group;
195 	const char *metric_name;
196 	const char *metric_desc;
197 	const char *metric_long_desc;
198 	const char *metric_expr;
199 	const char *metric_threshold;
200 	const char *metric_unit;
201 	const char *pmu_name;
202 };
203 
mep_cmp(struct rb_node * rb_node,const void * entry)204 static int mep_cmp(struct rb_node *rb_node, const void *entry)
205 {
206 	struct mep *a = container_of(rb_node, struct mep, nd);
207 	struct mep *b = (struct mep *)entry;
208 	int ret;
209 
210 	ret = strcmp(a->metric_group, b->metric_group);
211 	if (ret)
212 		return ret;
213 
214 	return strcmp(a->metric_name, b->metric_name);
215 }
216 
mep_new(struct rblist * rl __maybe_unused,const void * entry)217 static struct rb_node *mep_new(struct rblist *rl __maybe_unused, const void *entry)
218 {
219 	struct mep *me = malloc(sizeof(struct mep));
220 
221 	if (!me)
222 		return NULL;
223 
224 	memcpy(me, entry, sizeof(struct mep));
225 	return &me->nd;
226 }
227 
mep_delete(struct rblist * rl __maybe_unused,struct rb_node * nd)228 static void mep_delete(struct rblist *rl __maybe_unused,
229 		       struct rb_node *nd)
230 {
231 	struct mep *me = container_of(nd, struct mep, nd);
232 
233 	zfree(&me->metric_group);
234 	free(me);
235 }
236 
mep_lookup(struct rblist * groups,const char * metric_group,const char * metric_name)237 static struct mep *mep_lookup(struct rblist *groups, const char *metric_group,
238 			      const char *metric_name)
239 {
240 	struct rb_node *nd;
241 	struct mep me = {
242 		.metric_group = strdup(metric_group),
243 		.metric_name = metric_name,
244 	};
245 	nd = rblist__find(groups, &me);
246 	if (nd) {
247 		free(me.metric_group);
248 		return container_of(nd, struct mep, nd);
249 	}
250 	rblist__add_node(groups, &me);
251 	nd = rblist__find(groups, &me);
252 	if (nd)
253 		return container_of(nd, struct mep, nd);
254 	return NULL;
255 }
256 
metricgroup__add_to_mep_groups_callback(const struct pmu_metric * pm,const struct pmu_metrics_table * table __maybe_unused,void * vdata)257 static int metricgroup__add_to_mep_groups_callback(const struct pmu_metric *pm,
258 					const struct pmu_metrics_table *table __maybe_unused,
259 					void *vdata)
260 {
261 	struct rblist *groups = vdata;
262 	const char *g;
263 	char *omg, *mg;
264 
265 	mg = strdup(pm->metric_group ?: pm->metric_name);
266 	if (!mg)
267 		return -ENOMEM;
268 	omg = mg;
269 	while ((g = strsep(&mg, ";")) != NULL) {
270 		struct mep *me;
271 
272 		g = skip_spaces(g);
273 		if (strlen(g))
274 			me = mep_lookup(groups, g, pm->metric_name);
275 		else
276 			me = mep_lookup(groups, pm->metric_name, pm->metric_name);
277 
278 		if (me) {
279 			me->metric_desc = pm->desc;
280 			me->metric_long_desc = pm->long_desc;
281 			me->metric_expr = pm->metric_expr;
282 			me->metric_threshold = pm->metric_threshold;
283 			me->metric_unit = pm->unit;
284 			me->pmu_name = pm->pmu;
285 		}
286 	}
287 	free(omg);
288 
289 	return 0;
290 }
291 
metricgroup__print(const struct print_callbacks * print_cb,void * print_state)292 void metricgroup__print(const struct print_callbacks *print_cb, void *print_state)
293 {
294 	struct rblist groups;
295 	struct rb_node *node, *next;
296 	const struct pmu_metrics_table *table = pmu_metrics_table__find();
297 
298 	rblist__init(&groups);
299 	groups.node_new = mep_new;
300 	groups.node_cmp = mep_cmp;
301 	groups.node_delete = mep_delete;
302 
303 	metricgroup__for_each_metric(table, metricgroup__add_to_mep_groups_callback, &groups);
304 
305 	for (node = rb_first_cached(&groups.entries); node; node = next) {
306 		struct mep *me = container_of(node, struct mep, nd);
307 
308 		print_cb->print_metric(print_state,
309 				me->metric_group,
310 				me->metric_name,
311 				me->metric_desc,
312 				me->metric_long_desc,
313 				me->metric_expr,
314 				me->metric_threshold,
315 				me->metric_unit,
316 				me->pmu_name);
317 		next = rb_next(node);
318 		rblist__remove_node(&groups, node);
319 	}
320 }
321 
322 /*
323  * Print the help text for the event symbols:
324  */
print_events(const struct print_callbacks * print_cb,void * print_state)325 void print_events(const struct print_callbacks *print_cb, void *print_state)
326 {
327 	perf_pmus__print_pmu_events(print_cb, print_state);
328 
329 	print_cb->print_event(print_state,
330 			/*topic=*/NULL,
331 			/*pmu_name=*/NULL,
332 			PERF_TYPE_RAW,
333 			"rNNN",
334 			/*event_alias=*/NULL,
335 			/*scale_unit=*/NULL,
336 			/*deprecated=*/false,
337 			event_type_descriptors[PERF_TYPE_RAW],
338 			/*desc=*/NULL,
339 			/*long_desc=*/NULL,
340 			/*encoding_desc=*/NULL);
341 
342 	perf_pmus__print_raw_pmu_events(print_cb, print_state);
343 
344 	print_cb->print_event(print_state,
345 			/*topic=*/NULL,
346 			/*pmu_name=*/NULL,
347 			PERF_TYPE_BREAKPOINT,
348 			"mem:<addr>[/len][:access]",
349 			/*scale_unit=*/NULL,
350 			/*event_alias=*/NULL,
351 			/*deprecated=*/false,
352 			event_type_descriptors[PERF_TYPE_BREAKPOINT],
353 			/*desc=*/NULL,
354 			/*long_desc=*/NULL,
355 			/*encoding_desc=*/NULL);
356 
357 	print_sdt_events(print_cb, print_state);
358 
359 	metricgroup__print(print_cb, print_state);
360 
361 	print_libpfm_events(print_cb, print_state);
362 }
363