Linux Audio

Check our new training course

Loading...
Note: File does not exist in v5.4.
  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. */
 35static 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
 44static 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 */
 62void 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		pr_err("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);
123next_evt:
124			free(evt_namelist[j]);
125		}
126		close(dir_fd);
127		free(evt_namelist);
128next_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
141void 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
232bool is_event_supported(u8 type, u64 config)
233{
234	bool ret = true;
235	int open_return;
236	struct evsel *evsel;
237	struct perf_event_attr attr = {
238		.type = type,
239		.config = config,
240		.disabled = 1,
241	};
242	struct perf_thread_map *tmap = thread_map__new_by_tid(0);
243
244	if (tmap == NULL)
245		return false;
246
247	evsel = evsel__new(&attr);
248	if (evsel) {
249		open_return = evsel__open(evsel, NULL, tmap);
250		ret = open_return >= 0;
251
252		if (open_return == -EACCES) {
253			/*
254			 * This happens if the paranoid value
255			 * /proc/sys/kernel/perf_event_paranoid is set to 2
256			 * Re-run with exclude_kernel set; we don't do that
257			 * by default as some ARM machines do not support it.
258			 *
259			 */
260			evsel->core.attr.exclude_kernel = 1;
261			ret = evsel__open(evsel, NULL, tmap) >= 0;
262		}
263		evsel__delete(evsel);
264	}
265
266	perf_thread_map__put(tmap);
267	return ret;
268}
269
270int print_hwcache_events(const struct print_callbacks *print_cb, void *print_state)
271{
272	struct perf_pmu *pmu = NULL;
273	const char *event_type_descriptor = event_type_descriptors[PERF_TYPE_HW_CACHE];
274
275	/*
276	 * Only print core PMUs, skipping uncore for performance and
277	 * PERF_TYPE_SOFTWARE that can succeed in opening legacy cache evenst.
278	 */
279	while ((pmu = perf_pmus__scan_core(pmu)) != NULL) {
280		if (pmu->is_uncore || pmu->type == PERF_TYPE_SOFTWARE)
281			continue;
282
283		for (int type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
284			for (int op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
285				/* skip invalid cache type */
286				if (!evsel__is_cache_op_valid(type, op))
287					continue;
288
289				for (int res = 0; res < PERF_COUNT_HW_CACHE_RESULT_MAX; res++) {
290					char name[64];
291					char alias_name[128];
292					__u64 config;
293					int ret;
294
295					__evsel__hw_cache_type_op_res_name(type, op, res,
296									name, sizeof(name));
297
298					ret = parse_events__decode_legacy_cache(name, pmu->type,
299										&config);
300					if (ret || !is_event_supported(PERF_TYPE_HW_CACHE, config))
301						continue;
302					snprintf(alias_name, sizeof(alias_name), "%s/%s/",
303						 pmu->name, name);
304					print_cb->print_event(print_state,
305							"cache",
306							pmu->name,
307							name,
308							alias_name,
309							/*scale_unit=*/NULL,
310							/*deprecated=*/false,
311							event_type_descriptor,
312							/*desc=*/NULL,
313							/*long_desc=*/NULL,
314							/*encoding_desc=*/NULL);
315				}
316			}
317		}
318	}
319	return 0;
320}
321
322void print_tool_events(const struct print_callbacks *print_cb, void *print_state)
323{
324	// Start at 1 because the first enum entry means no tool event.
325	for (int i = 1; i < PERF_TOOL_MAX; ++i) {
326		print_cb->print_event(print_state,
327				"tool",
328				/*pmu_name=*/NULL,
329				event_symbols_tool[i].symbol,
330				event_symbols_tool[i].alias,
331				/*scale_unit=*/NULL,
332				/*deprecated=*/false,
333				"Tool event",
334				/*desc=*/NULL,
335				/*long_desc=*/NULL,
336				/*encoding_desc=*/NULL);
337	}
338}
339
340void print_symbol_events(const struct print_callbacks *print_cb, void *print_state,
341			 unsigned int type, const struct event_symbol *syms,
342			 unsigned int max)
343{
344	struct strlist *evt_name_list = strlist__new(NULL, NULL);
345	struct str_node *nd;
346
347	if (!evt_name_list) {
348		pr_debug("Failed to allocate new strlist for symbol events\n");
349		return;
350	}
351	for (unsigned int i = 0; i < max; i++) {
352		/*
353		 * New attr.config still not supported here, the latest
354		 * example was PERF_COUNT_SW_CGROUP_SWITCHES
355		 */
356		if (syms[i].symbol == NULL)
357			continue;
358
359		if (!is_event_supported(type, i))
360			continue;
361
362		if (strlen(syms[i].alias)) {
363			char name[MAX_NAME_LEN];
364
365			snprintf(name, MAX_NAME_LEN, "%s OR %s", syms[i].symbol, syms[i].alias);
366			strlist__add(evt_name_list, name);
367		} else
368			strlist__add(evt_name_list, syms[i].symbol);
369	}
370
371	strlist__for_each_entry(nd, evt_name_list) {
372		char *alias = strstr(nd->s, " OR ");
373
374		if (alias) {
375			*alias = '\0';
376			alias += 4;
377		}
378		print_cb->print_event(print_state,
379				/*topic=*/NULL,
380				/*pmu_name=*/NULL,
381				nd->s,
382				alias,
383				/*scale_unit=*/NULL,
384				/*deprecated=*/false,
385				event_type_descriptors[type],
386				/*desc=*/NULL,
387				/*long_desc=*/NULL,
388				/*encoding_desc=*/NULL);
389	}
390	strlist__delete(evt_name_list);
391}
392
393/*
394 * Print the help text for the event symbols:
395 */
396void print_events(const struct print_callbacks *print_cb, void *print_state)
397{
398	char *tmp;
399
400	print_symbol_events(print_cb, print_state, PERF_TYPE_HARDWARE,
401			event_symbols_hw, PERF_COUNT_HW_MAX);
402	print_symbol_events(print_cb, print_state, PERF_TYPE_SOFTWARE,
403			event_symbols_sw, PERF_COUNT_SW_MAX);
404
405	print_tool_events(print_cb, print_state);
406
407	print_hwcache_events(print_cb, print_state);
408
409	perf_pmus__print_pmu_events(print_cb, print_state);
410
411	print_cb->print_event(print_state,
412			/*topic=*/NULL,
413			/*pmu_name=*/NULL,
414			"rNNN",
415			/*event_alias=*/NULL,
416			/*scale_unit=*/NULL,
417			/*deprecated=*/false,
418			event_type_descriptors[PERF_TYPE_RAW],
419			/*desc=*/NULL,
420			/*long_desc=*/NULL,
421			/*encoding_desc=*/NULL);
422
423	if (asprintf(&tmp, "%s/t1=v1[,t2=v2,t3 ...]/modifier",
424		     perf_pmus__scan_core(/*pmu=*/NULL)->name) > 0) {
425		print_cb->print_event(print_state,
426				/*topic=*/NULL,
427				/*pmu_name=*/NULL,
428				tmp,
429				/*event_alias=*/NULL,
430				/*scale_unit=*/NULL,
431				/*deprecated=*/false,
432				event_type_descriptors[PERF_TYPE_RAW],
433				"(see 'man perf-list' on how to encode it)",
434				/*long_desc=*/NULL,
435				/*encoding_desc=*/NULL);
436		free(tmp);
437	}
438
439	print_cb->print_event(print_state,
440			/*topic=*/NULL,
441			/*pmu_name=*/NULL,
442			"mem:<addr>[/len][:access]",
443			/*scale_unit=*/NULL,
444			/*event_alias=*/NULL,
445			/*deprecated=*/false,
446			event_type_descriptors[PERF_TYPE_BREAKPOINT],
447			/*desc=*/NULL,
448			/*long_desc=*/NULL,
449			/*encoding_desc=*/NULL);
450
451	print_tracepoint_events(print_cb, print_state);
452
453	print_sdt_events(print_cb, print_state);
454
455	metricgroup__print(print_cb, print_state);
456
457	print_libpfm_events(print_cb, print_state);
458}