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 "tracepoint.h"
27 #include "pmu-hybrid.h"
29 #define MAX_NAME_LEN 100
31 static const char * const event_type_descriptors[] = {
35 "Hardware cache event",
36 "Raw hardware event descriptor",
37 "Hardware breakpoint",
40 static const struct event_symbol event_symbols_tool[PERF_TOOL_MAX] = {
41 [PERF_TOOL_DURATION_TIME] = {
42 .symbol = "duration_time",
45 [PERF_TOOL_USER_TIME] = {
46 .symbol = "user_time",
49 [PERF_TOOL_SYSTEM_TIME] = {
50 .symbol = "system_time",
55 static int cmp_string(const void *a, const void *b)
57 const char * const *as = a;
58 const char * const *bs = b;
60 return strcmp(*as, *bs);
64 * Print the events from <debugfs_mount_point>/tracing/events
66 void print_tracepoint_events(const char *subsys_glob,
67 const char *event_glob, bool name_only)
69 DIR *sys_dir, *evt_dir;
70 struct dirent *sys_dirent, *evt_dirent;
71 char evt_path[MAXPATHLEN];
73 char **evt_list = NULL;
74 unsigned int evt_i = 0, evt_num = 0;
75 bool evt_num_known = false;
78 sys_dir = tracing_events__opendir();
83 evt_list = zalloc(sizeof(char *) * evt_num);
85 goto out_close_sys_dir;
88 for_each_subsystem(sys_dir, sys_dirent) {
89 if (subsys_glob != NULL &&
90 !strglobmatch(sys_dirent->d_name, subsys_glob))
93 dir_path = get_events_file(sys_dirent->d_name);
96 evt_dir = opendir(dir_path);
100 for_each_event(dir_path, evt_dir, evt_dirent) {
101 if (event_glob != NULL &&
102 !strglobmatch(evt_dirent->d_name, event_glob))
105 if (!evt_num_known) {
110 snprintf(evt_path, MAXPATHLEN, "%s:%s",
111 sys_dirent->d_name, evt_dirent->d_name);
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;
122 put_events_file(dir_path);
126 if (!evt_num_known) {
127 evt_num_known = true;
130 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
132 while (evt_i < evt_num) {
134 printf("%s ", evt_list[evt_i++]);
137 printf(" %-50s [%s]\n", evt_list[evt_i++],
138 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
140 if (evt_num && pager_in_use())
145 for (evt_i = 0; evt_i < evt_num; evt_i++)
146 zfree(&evt_list[evt_i]);
155 printf("FATAL: not enough memory to print %s\n",
156 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
161 void print_sdt_events(const char *subsys_glob, const char *event_glob,
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;
173 sdtlist = strlist__new(NULL, &cfg);
175 pr_debug("Failed to allocate new strlist for SDT\n");
178 bidlist = build_id_cache__list_all(true);
180 pr_debug("Failed to get buildids: %d\n", errno);
183 strlist__for_each_entry(nd, bidlist) {
184 pcache = probe_cache__new(nd->s, NULL);
187 list_for_each_entry(ent, &pcache->entries, node) {
191 !strglobmatch(ent->pev.group, subsys_glob))
194 !strglobmatch(ent->pev.event, event_glob))
196 ret = asprintf(&buf, "%s:%s@%s", ent->pev.group,
197 ent->pev.event, nd->s);
199 strlist__add(sdtlist, buf);
201 probe_cache__delete(pcache);
203 strlist__delete(bidlist);
205 strlist__for_each_entry(nd, sdtlist) {
206 buf = strchr(nd->s, '@');
210 printf("%s ", nd->s);
213 nd2 = strlist__next(nd);
215 ptr = strchr(nd2->s, '@');
218 if (strcmp(nd->s, nd2->s) == 0)
222 path = build_id_cache__origname(buf);
223 ret = asprintf(&buf, "%s@%s(%.12s)", nd->s, path, buf);
225 printf(" %-50s [%s]\n", buf, "SDT event");
230 printf(" %-50s [%s]\n", nd->s, "SDT event");
232 if (strcmp(nd->s, nd2->s) != 0)
238 strlist__delete(sdtlist);
241 int print_hwcache_events(const char *event_glob, bool name_only)
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;
249 if (perf_pmu__has_hybrid()) {
250 npmus = perf_pmu__hybrid_pmu_num();
251 evt_pmus = zalloc(sizeof(char *) * npmus);
258 evt_list = zalloc(sizeof(char *) * evt_num);
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))
269 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
270 unsigned int hybrid_supported = 0, j;
273 __evsel__hw_cache_type_op_res_name(type, op, i, name, sizeof(name));
274 if (event_glob != NULL && !strglobmatch(name, event_glob))
277 if (!perf_pmu__has_hybrid()) {
278 if (!is_event_supported(PERF_TYPE_HW_CACHE,
279 type | (op << 8) | (i << 16))) {
283 perf_pmu__for_each_hybrid_pmu(pmu) {
284 if (!evt_num_known) {
289 supported = is_event_supported(
291 type | (op << 8) | (i << 16) |
292 ((__u64)pmu->type << PERF_PMU_TYPE_SHIFT));
294 snprintf(new_name, sizeof(new_name),
295 "%s/%s/", pmu->name, name);
296 evt_pmus[hybrid_supported] =
302 if (hybrid_supported == 0)
306 if (!evt_num_known) {
311 if ((hybrid_supported == 0) ||
312 (hybrid_supported == npmus)) {
313 evt_list[evt_i] = strdup(name);
315 for (j = 0; j < npmus; j++)
319 for (j = 0; j < hybrid_supported; j++) {
320 evt_list[evt_i++] = evt_pmus[j];
326 if (evt_list[evt_i] == NULL)
333 if (!evt_num_known) {
334 evt_num_known = true;
338 for (evt_i = 0; evt_i < evt_num; evt_i++) {
339 if (!evt_list[evt_i])
344 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
346 while (evt_i < evt_num) {
348 printf("%s ", evt_list[evt_i++]);
351 printf(" %-50s [%s]\n", evt_list[evt_i++],
352 event_type_descriptors[PERF_TYPE_HW_CACHE]);
354 if (evt_num && pager_in_use())
359 for (evt_i = 0; evt_i < evt_num; evt_i++)
360 zfree(&evt_list[evt_i]);
363 for (evt_i = 0; evt_i < npmus; evt_i++)
364 zfree(&evt_pmus[evt_i]);
369 printf("FATAL: not enough memory to print %s\n",
370 event_type_descriptors[PERF_TYPE_HW_CACHE]);
376 static void print_tool_event(const struct event_symbol *syms, const char *event_glob,
379 if (syms->symbol == NULL)
382 if (event_glob && !(strglobmatch(syms->symbol, event_glob) ||
383 (syms->alias && strglobmatch(syms->alias, event_glob))))
387 printf("%s ", syms->symbol);
389 char name[MAX_NAME_LEN];
391 if (syms->alias && strlen(syms->alias))
392 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
394 strlcpy(name, syms->symbol, MAX_NAME_LEN);
395 printf(" %-50s [%s]\n", name, "Tool event");
399 void print_tool_events(const char *event_glob, bool name_only)
401 // Start at 1 because the first enum entry means no tool event.
402 for (int i = 1; i < PERF_TOOL_MAX; ++i)
403 print_tool_event(event_symbols_tool + i, event_glob, name_only);
409 void print_symbol_events(const char *event_glob, unsigned int type,
410 struct event_symbol *syms, unsigned int max,
413 unsigned int i, evt_i = 0, evt_num = 0;
414 char name[MAX_NAME_LEN];
415 char **evt_list = NULL;
416 bool evt_num_known = false;
420 evt_list = zalloc(sizeof(char *) * evt_num);
426 for (i = 0; i < max; i++, syms++) {
428 * New attr.config still not supported here, the latest
429 * example was PERF_COUNT_SW_CGROUP_SWITCHES
431 if (syms->symbol == NULL)
434 if (event_glob != NULL && !(strglobmatch(syms->symbol, event_glob) ||
435 (syms->alias && strglobmatch(syms->alias, event_glob))))
438 if (!is_event_supported(type, i))
441 if (!evt_num_known) {
446 if (!name_only && strlen(syms->alias))
447 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
449 strlcpy(name, syms->symbol, MAX_NAME_LEN);
451 evt_list[evt_i] = strdup(name);
452 if (evt_list[evt_i] == NULL)
457 if (!evt_num_known) {
458 evt_num_known = true;
461 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
463 while (evt_i < evt_num) {
465 printf("%s ", evt_list[evt_i++]);
468 printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
470 if (evt_num && pager_in_use())
475 for (evt_i = 0; evt_i < evt_num; evt_i++)
476 zfree(&evt_list[evt_i]);
481 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
487 * Print the help text for the event symbols:
489 void print_events(const char *event_glob, bool name_only, bool quiet_flag,
490 bool long_desc, bool details_flag, bool deprecated,
491 const char *pmu_name)
493 print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
494 event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
496 print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
497 event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
498 print_tool_events(event_glob, name_only);
500 print_hwcache_events(event_glob, name_only);
502 print_pmu_events(event_glob, name_only, quiet_flag, long_desc,
503 details_flag, deprecated, pmu_name);
505 if (event_glob != NULL)
509 printf(" %-50s [%s]\n",
511 event_type_descriptors[PERF_TYPE_RAW]);
512 printf(" %-50s [%s]\n",
513 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
514 event_type_descriptors[PERF_TYPE_RAW]);
516 printf(" (see 'man perf-list' on how to encode it)\n\n");
518 printf(" %-50s [%s]\n",
519 "mem:<addr>[/len][:access]",
520 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
525 print_tracepoint_events(NULL, NULL, name_only);
527 print_sdt_events(NULL, NULL, name_only);
529 metricgroup__print(true, true, NULL, name_only, details_flag,
532 print_libpfm_events(name_only, long_desc);