1 // SPDX-License-Identifier: GPL-2.0
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>
18 #include "metricgroup.h"
19 #include "parse-events.h"
21 #include "print-events.h"
22 #include "probe-file.h"
25 #include "thread_map.h"
26 #include "tracepoint.h"
28 #include "pmu-hybrid.h"
30 #define MAX_NAME_LEN 100
32 static const char * const event_type_descriptors[] = {
36 "Hardware cache event",
37 "Raw hardware event descriptor",
38 "Hardware breakpoint",
41 static const struct event_symbol event_symbols_tool[PERF_TOOL_MAX] = {
42 [PERF_TOOL_DURATION_TIME] = {
43 .symbol = "duration_time",
46 [PERF_TOOL_USER_TIME] = {
47 .symbol = "user_time",
50 [PERF_TOOL_SYSTEM_TIME] = {
51 .symbol = "system_time",
56 static int cmp_string(const void *a, const void *b)
58 const char * const *as = a;
59 const char * const *bs = b;
61 return strcmp(*as, *bs);
65 * Print the events from <debugfs_mount_point>/tracing/events
67 void print_tracepoint_events(const char *subsys_glob,
68 const char *event_glob, bool name_only)
70 DIR *sys_dir, *evt_dir;
71 struct dirent *sys_dirent, *evt_dirent;
72 char evt_path[MAXPATHLEN];
74 char **evt_list = NULL;
75 unsigned int evt_i = 0, evt_num = 0;
76 bool evt_num_known = false;
79 sys_dir = tracing_events__opendir();
84 evt_list = zalloc(sizeof(char *) * evt_num);
86 goto out_close_sys_dir;
89 for_each_subsystem(sys_dir, sys_dirent) {
90 if (subsys_glob != NULL &&
91 !strglobmatch(sys_dirent->d_name, subsys_glob))
94 dir_path = get_events_file(sys_dirent->d_name);
97 evt_dir = opendir(dir_path);
101 for_each_event(dir_path, evt_dir, evt_dirent) {
102 if (event_glob != NULL &&
103 !strglobmatch(evt_dirent->d_name, event_glob))
106 if (!evt_num_known) {
111 snprintf(evt_path, MAXPATHLEN, "%s:%s",
112 sys_dirent->d_name, evt_dirent->d_name);
114 evt_list[evt_i] = strdup(evt_path);
115 if (evt_list[evt_i] == NULL) {
116 put_events_file(dir_path);
117 goto out_close_evt_dir;
123 put_events_file(dir_path);
127 if (!evt_num_known) {
128 evt_num_known = true;
131 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
133 while (evt_i < evt_num) {
135 printf("%s ", evt_list[evt_i++]);
138 printf(" %-50s [%s]\n", evt_list[evt_i++],
139 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
141 if (evt_num && pager_in_use())
146 for (evt_i = 0; evt_i < evt_num; evt_i++)
147 zfree(&evt_list[evt_i]);
156 printf("FATAL: not enough memory to print %s\n",
157 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
162 void print_sdt_events(const char *subsys_glob, const char *event_glob,
165 struct probe_cache *pcache;
166 struct probe_cache_entry *ent;
167 struct strlist *bidlist, *sdtlist;
168 struct strlist_config cfg = {.dont_dupstr = true};
169 struct str_node *nd, *nd2;
170 char *buf, *path, *ptr = NULL;
171 bool show_detail = false;
174 sdtlist = strlist__new(NULL, &cfg);
176 pr_debug("Failed to allocate new strlist for SDT\n");
179 bidlist = build_id_cache__list_all(true);
181 pr_debug("Failed to get buildids: %d\n", errno);
184 strlist__for_each_entry(nd, bidlist) {
185 pcache = probe_cache__new(nd->s, NULL);
188 list_for_each_entry(ent, &pcache->entries, node) {
192 !strglobmatch(ent->pev.group, subsys_glob))
195 !strglobmatch(ent->pev.event, event_glob))
197 ret = asprintf(&buf, "%s:%s@%s", ent->pev.group,
198 ent->pev.event, nd->s);
200 strlist__add(sdtlist, buf);
202 probe_cache__delete(pcache);
204 strlist__delete(bidlist);
206 strlist__for_each_entry(nd, sdtlist) {
207 buf = strchr(nd->s, '@');
211 printf("%s ", nd->s);
214 nd2 = strlist__next(nd);
216 ptr = strchr(nd2->s, '@');
219 if (strcmp(nd->s, nd2->s) == 0)
223 path = build_id_cache__origname(buf);
224 ret = asprintf(&buf, "%s@%s(%.12s)", nd->s, path, buf);
226 printf(" %-50s [%s]\n", buf, "SDT event");
231 printf(" %-50s [%s]\n", nd->s, "SDT event");
233 if (strcmp(nd->s, nd2->s) != 0)
239 strlist__delete(sdtlist);
242 static bool is_event_supported(u8 type, u64 config)
247 struct perf_event_attr attr = {
252 struct perf_thread_map *tmap = thread_map__new_by_tid(0);
257 evsel = evsel__new(&attr);
259 open_return = evsel__open(evsel, NULL, tmap);
260 ret = open_return >= 0;
262 if (open_return == -EACCES) {
264 * This happens if the paranoid value
265 * /proc/sys/kernel/perf_event_paranoid is set to 2
266 * Re-run with exclude_kernel set; we don't do that
267 * by default as some ARM machines do not support it.
270 evsel->core.attr.exclude_kernel = 1;
271 ret = evsel__open(evsel, NULL, tmap) >= 0;
273 evsel__delete(evsel);
276 perf_thread_map__put(tmap);
280 int print_hwcache_events(const char *event_glob, bool name_only)
282 unsigned int type, op, i, evt_i = 0, evt_num = 0, npmus = 0;
283 char name[64], new_name[128];
284 char **evt_list = NULL, **evt_pmus = NULL;
285 bool evt_num_known = false;
286 struct perf_pmu *pmu = NULL;
288 if (perf_pmu__has_hybrid()) {
289 npmus = perf_pmu__hybrid_pmu_num();
290 evt_pmus = zalloc(sizeof(char *) * npmus);
297 evt_list = zalloc(sizeof(char *) * evt_num);
302 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
303 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
304 /* skip invalid cache type */
305 if (!evsel__is_cache_op_valid(type, op))
308 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
309 unsigned int hybrid_supported = 0, j;
312 __evsel__hw_cache_type_op_res_name(type, op, i, name, sizeof(name));
313 if (event_glob != NULL && !strglobmatch(name, event_glob))
316 if (!perf_pmu__has_hybrid()) {
317 if (!is_event_supported(PERF_TYPE_HW_CACHE,
318 type | (op << 8) | (i << 16))) {
322 perf_pmu__for_each_hybrid_pmu(pmu) {
323 if (!evt_num_known) {
328 supported = is_event_supported(
330 type | (op << 8) | (i << 16) |
331 ((__u64)pmu->type << PERF_PMU_TYPE_SHIFT));
333 snprintf(new_name, sizeof(new_name),
334 "%s/%s/", pmu->name, name);
335 evt_pmus[hybrid_supported] =
341 if (hybrid_supported == 0)
345 if (!evt_num_known) {
350 if ((hybrid_supported == 0) ||
351 (hybrid_supported == npmus)) {
352 evt_list[evt_i] = strdup(name);
354 for (j = 0; j < npmus; j++)
358 for (j = 0; j < hybrid_supported; j++) {
359 evt_list[evt_i++] = evt_pmus[j];
365 if (evt_list[evt_i] == NULL)
372 if (!evt_num_known) {
373 evt_num_known = true;
377 for (evt_i = 0; evt_i < evt_num; evt_i++) {
378 if (!evt_list[evt_i])
383 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
385 while (evt_i < evt_num) {
387 printf("%s ", evt_list[evt_i++]);
390 printf(" %-50s [%s]\n", evt_list[evt_i++],
391 event_type_descriptors[PERF_TYPE_HW_CACHE]);
393 if (evt_num && pager_in_use())
398 for (evt_i = 0; evt_i < evt_num; evt_i++)
399 zfree(&evt_list[evt_i]);
402 for (evt_i = 0; evt_i < npmus; evt_i++)
403 zfree(&evt_pmus[evt_i]);
408 printf("FATAL: not enough memory to print %s\n",
409 event_type_descriptors[PERF_TYPE_HW_CACHE]);
415 static void print_tool_event(const struct event_symbol *syms, const char *event_glob,
418 if (syms->symbol == NULL)
421 if (event_glob && !(strglobmatch(syms->symbol, event_glob) ||
422 (syms->alias && strglobmatch(syms->alias, event_glob))))
426 printf("%s ", syms->symbol);
428 char name[MAX_NAME_LEN];
430 if (syms->alias && strlen(syms->alias))
431 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
433 strlcpy(name, syms->symbol, MAX_NAME_LEN);
434 printf(" %-50s [%s]\n", name, "Tool event");
438 void print_tool_events(const char *event_glob, bool name_only)
440 // Start at 1 because the first enum entry means no tool event.
441 for (int i = 1; i < PERF_TOOL_MAX; ++i)
442 print_tool_event(event_symbols_tool + i, event_glob, name_only);
448 void print_symbol_events(const char *event_glob, unsigned int type,
449 struct event_symbol *syms, unsigned int max,
452 unsigned int i, evt_i = 0, evt_num = 0;
453 char name[MAX_NAME_LEN];
454 char **evt_list = NULL;
455 bool evt_num_known = false;
459 evt_list = zalloc(sizeof(char *) * evt_num);
465 for (i = 0; i < max; i++, syms++) {
467 * New attr.config still not supported here, the latest
468 * example was PERF_COUNT_SW_CGROUP_SWITCHES
470 if (syms->symbol == NULL)
473 if (event_glob != NULL && !(strglobmatch(syms->symbol, event_glob) ||
474 (syms->alias && strglobmatch(syms->alias, event_glob))))
477 if (!is_event_supported(type, i))
480 if (!evt_num_known) {
485 if (!name_only && strlen(syms->alias))
486 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
488 strlcpy(name, syms->symbol, MAX_NAME_LEN);
490 evt_list[evt_i] = strdup(name);
491 if (evt_list[evt_i] == NULL)
496 if (!evt_num_known) {
497 evt_num_known = true;
500 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
502 while (evt_i < evt_num) {
504 printf("%s ", evt_list[evt_i++]);
507 printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
509 if (evt_num && pager_in_use())
514 for (evt_i = 0; evt_i < evt_num; evt_i++)
515 zfree(&evt_list[evt_i]);
520 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
526 * Print the help text for the event symbols:
528 void print_events(const char *event_glob, bool name_only, bool quiet_flag,
529 bool long_desc, bool details_flag, bool deprecated,
530 const char *pmu_name)
532 print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
533 event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
535 print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
536 event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
537 print_tool_events(event_glob, name_only);
539 print_hwcache_events(event_glob, name_only);
541 print_pmu_events(event_glob, name_only, quiet_flag, long_desc,
542 details_flag, deprecated, pmu_name);
544 if (event_glob != NULL)
548 printf(" %-50s [%s]\n",
550 event_type_descriptors[PERF_TYPE_RAW]);
551 printf(" %-50s [%s]\n",
552 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
553 event_type_descriptors[PERF_TYPE_RAW]);
555 printf(" (see 'man perf-list' on how to encode it)\n\n");
557 printf(" %-50s [%s]\n",
558 "mem:<addr>[/len][:access]",
559 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
564 print_tracepoint_events(NULL, NULL, name_only);
566 print_sdt_events(NULL, NULL, name_only);
568 metricgroup__print(true, true, NULL, name_only, details_flag,
571 print_libpfm_events(name_only, long_desc);