4 #include "parse-options.h"
5 #include "parse-events.h"
12 struct perf_counter_attr attrs
[MAX_COUNTERS
];
27 char debugfs_path
[MAXPATHLEN
];
29 #define CHW(x) .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_##x
30 #define CSW(x) .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_##x
32 static struct event_symbol event_symbols
[] = {
33 { CHW(CPU_CYCLES
), "cpu-cycles", "cycles" },
34 { CHW(INSTRUCTIONS
), "instructions", "" },
35 { CHW(CACHE_REFERENCES
), "cache-references", "" },
36 { CHW(CACHE_MISSES
), "cache-misses", "" },
37 { CHW(BRANCH_INSTRUCTIONS
), "branch-instructions", "branches" },
38 { CHW(BRANCH_MISSES
), "branch-misses", "" },
39 { CHW(BUS_CYCLES
), "bus-cycles", "" },
41 { CSW(CPU_CLOCK
), "cpu-clock", "" },
42 { CSW(TASK_CLOCK
), "task-clock", "" },
43 { CSW(PAGE_FAULTS
), "page-faults", "faults" },
44 { CSW(PAGE_FAULTS_MIN
), "minor-faults", "" },
45 { CSW(PAGE_FAULTS_MAJ
), "major-faults", "" },
46 { CSW(CONTEXT_SWITCHES
), "context-switches", "cs" },
47 { CSW(CPU_MIGRATIONS
), "cpu-migrations", "migrations" },
50 #define __PERF_COUNTER_FIELD(config, name) \
51 ((config & PERF_COUNTER_##name##_MASK) >> PERF_COUNTER_##name##_SHIFT)
53 #define PERF_COUNTER_RAW(config) __PERF_COUNTER_FIELD(config, RAW)
54 #define PERF_COUNTER_CONFIG(config) __PERF_COUNTER_FIELD(config, CONFIG)
55 #define PERF_COUNTER_TYPE(config) __PERF_COUNTER_FIELD(config, TYPE)
56 #define PERF_COUNTER_ID(config) __PERF_COUNTER_FIELD(config, EVENT)
58 static const char *hw_event_names
[] = {
68 static const char *sw_event_names
[] = {
80 static const char *hw_cache
[][MAX_ALIASES
] = {
81 { "L1-dcache", "l1-d", "l1d", "L1-data", },
82 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
84 { "dTLB", "d-tlb", "Data-TLB", },
85 { "iTLB", "i-tlb", "Instruction-TLB", },
86 { "branch", "branches", "bpu", "btb", "bpc", },
89 static const char *hw_cache_op
[][MAX_ALIASES
] = {
90 { "load", "loads", "read", },
91 { "store", "stores", "write", },
92 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
95 static const char *hw_cache_result
[][MAX_ALIASES
] = {
96 { "refs", "Reference", "ops", "access", },
97 { "misses", "miss", },
100 #define C(x) PERF_COUNT_HW_CACHE_##x
101 #define CACHE_READ (1 << C(OP_READ))
102 #define CACHE_WRITE (1 << C(OP_WRITE))
103 #define CACHE_PREFETCH (1 << C(OP_PREFETCH))
104 #define COP(x) (1 << x)
107 * cache operartion stat
108 * L1I : Read and prefetch only
109 * ITLB and BPU : Read-only
111 static unsigned long hw_cache_stat
[C(MAX
)] = {
112 [C(L1D
)] = (CACHE_READ
| CACHE_WRITE
| CACHE_PREFETCH
),
113 [C(L1I
)] = (CACHE_READ
| CACHE_PREFETCH
),
114 [C(LL
)] = (CACHE_READ
| CACHE_WRITE
| CACHE_PREFETCH
),
115 [C(DTLB
)] = (CACHE_READ
| CACHE_WRITE
| CACHE_PREFETCH
),
116 [C(ITLB
)] = (CACHE_READ
),
117 [C(BPU
)] = (CACHE_READ
),
120 #define for_each_subsystem(sys_dir, sys_dirent, sys_next) \
121 while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \
122 if (sys_dirent.d_type == DT_DIR && \
123 (strcmp(sys_dirent.d_name, ".")) && \
124 (strcmp(sys_dirent.d_name, "..")))
126 static int tp_event_has_id(struct dirent
*sys_dir
, struct dirent
*evt_dir
)
128 char evt_path
[MAXPATHLEN
];
131 snprintf(evt_path
, MAXPATHLEN
, "%s/%s/%s/id", debugfs_path
,
132 sys_dir
->d_name
, evt_dir
->d_name
);
133 fd
= open(evt_path
, O_RDONLY
);
141 #define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) \
142 while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \
143 if (evt_dirent.d_type == DT_DIR && \
144 (strcmp(evt_dirent.d_name, ".")) && \
145 (strcmp(evt_dirent.d_name, "..")) && \
146 (!tp_event_has_id(&sys_dirent, &evt_dirent)))
148 #define MAX_EVENT_LENGTH 512
150 int valid_debugfs_mount(const char *debugfs
)
154 if (statfs(debugfs
, &st_fs
) < 0)
156 else if (st_fs
.f_type
!= (long) DEBUGFS_MAGIC
)
161 struct tracepoint_path
*tracepoint_id_to_path(u64 config
)
163 struct tracepoint_path
*path
= NULL
;
164 DIR *sys_dir
, *evt_dir
;
165 struct dirent
*sys_next
, *evt_next
, sys_dirent
, evt_dirent
;
169 char evt_path
[MAXPATHLEN
];
171 if (valid_debugfs_mount(debugfs_path
))
174 sys_dir
= opendir(debugfs_path
);
177 sys_dir_fd
= dirfd(sys_dir
);
179 for_each_subsystem(sys_dir
, sys_dirent
, sys_next
) {
180 int dfd
= openat(sys_dir_fd
, sys_dirent
.d_name
,
181 O_RDONLY
|O_DIRECTORY
), evt_dir_fd
;
184 evt_dir
= fdopendir(dfd
);
189 evt_dir_fd
= dirfd(evt_dir
);
190 for_each_event(sys_dirent
, evt_dir
, evt_dirent
, evt_next
) {
191 snprintf(evt_path
, MAXPATHLEN
, "%s/id",
193 fd
= openat(evt_dir_fd
, evt_path
, O_RDONLY
);
196 if (read(fd
, id_buf
, sizeof(id_buf
)) < 0) {
205 path
= calloc(1, sizeof(path
));
206 path
->system
= malloc(MAX_EVENT_LENGTH
);
211 path
->name
= malloc(MAX_EVENT_LENGTH
);
217 strncpy(path
->system
, sys_dirent
.d_name
,
219 strncpy(path
->name
, evt_dirent
.d_name
,
232 #define TP_PATH_LEN (MAX_EVENT_LENGTH * 2 + 1)
233 static const char *tracepoint_id_to_name(u64 config
)
235 static char buf
[TP_PATH_LEN
];
236 struct tracepoint_path
*path
;
238 path
= tracepoint_id_to_path(config
);
240 snprintf(buf
, TP_PATH_LEN
, "%s:%s", path
->system
, path
->name
);
245 snprintf(buf
, TP_PATH_LEN
, "%s:%s", "unknown", "unknown");
250 static int is_cache_op_valid(u8 cache_type
, u8 cache_op
)
252 if (hw_cache_stat
[cache_type
] & COP(cache_op
))
253 return 1; /* valid */
255 return 0; /* invalid */
258 static char *event_cache_name(u8 cache_type
, u8 cache_op
, u8 cache_result
)
260 static char name
[50];
263 sprintf(name
, "%s-%s-%s", hw_cache
[cache_type
][0],
264 hw_cache_op
[cache_op
][0],
265 hw_cache_result
[cache_result
][0]);
267 sprintf(name
, "%s-%s", hw_cache
[cache_type
][0],
268 hw_cache_op
[cache_op
][1]);
274 const char *event_name(int counter
)
276 u64 config
= attrs
[counter
].config
;
277 int type
= attrs
[counter
].type
;
279 return __event_name(type
, config
);
282 const char *__event_name(int type
, u64 config
)
286 if (type
== PERF_TYPE_RAW
) {
287 sprintf(buf
, "raw 0x%llx", config
);
292 case PERF_TYPE_HARDWARE
:
293 if (config
< PERF_COUNT_HW_MAX
)
294 return hw_event_names
[config
];
295 return "unknown-hardware";
297 case PERF_TYPE_HW_CACHE
: {
298 u8 cache_type
, cache_op
, cache_result
;
300 cache_type
= (config
>> 0) & 0xff;
301 if (cache_type
> PERF_COUNT_HW_CACHE_MAX
)
302 return "unknown-ext-hardware-cache-type";
304 cache_op
= (config
>> 8) & 0xff;
305 if (cache_op
> PERF_COUNT_HW_CACHE_OP_MAX
)
306 return "unknown-ext-hardware-cache-op";
308 cache_result
= (config
>> 16) & 0xff;
309 if (cache_result
> PERF_COUNT_HW_CACHE_RESULT_MAX
)
310 return "unknown-ext-hardware-cache-result";
312 if (!is_cache_op_valid(cache_type
, cache_op
))
313 return "invalid-cache";
315 return event_cache_name(cache_type
, cache_op
, cache_result
);
318 case PERF_TYPE_SOFTWARE
:
319 if (config
< PERF_COUNT_SW_MAX
)
320 return sw_event_names
[config
];
321 return "unknown-software";
323 case PERF_TYPE_TRACEPOINT
:
324 return tracepoint_id_to_name(config
);
333 static int parse_aliases(const char **str
, const char *names
[][MAX_ALIASES
], int size
)
338 for (i
= 0; i
< size
; i
++) {
339 for (j
= 0; j
< MAX_ALIASES
&& names
[i
][j
]; j
++) {
340 n
= strlen(names
[i
][j
]);
341 if (n
> longest
&& !strncasecmp(*str
, names
[i
][j
], n
))
353 static enum event_result
354 parse_generic_hw_event(const char **str
, struct perf_counter_attr
*attr
)
356 const char *s
= *str
;
357 int cache_type
= -1, cache_op
= -1, cache_result
= -1;
359 cache_type
= parse_aliases(&s
, hw_cache
, PERF_COUNT_HW_CACHE_MAX
);
361 * No fallback - if we cannot get a clear cache type
364 if (cache_type
== -1)
367 while ((cache_op
== -1 || cache_result
== -1) && *s
== '-') {
370 if (cache_op
== -1) {
371 cache_op
= parse_aliases(&s
, hw_cache_op
,
372 PERF_COUNT_HW_CACHE_OP_MAX
);
374 if (!is_cache_op_valid(cache_type
, cache_op
))
380 if (cache_result
== -1) {
381 cache_result
= parse_aliases(&s
, hw_cache_result
,
382 PERF_COUNT_HW_CACHE_RESULT_MAX
);
383 if (cache_result
>= 0)
388 * Can't parse this as a cache op or result, so back up
396 * Fall back to reads:
399 cache_op
= PERF_COUNT_HW_CACHE_OP_READ
;
402 * Fall back to accesses:
404 if (cache_result
== -1)
405 cache_result
= PERF_COUNT_HW_CACHE_RESULT_ACCESS
;
407 attr
->config
= cache_type
| (cache_op
<< 8) | (cache_result
<< 16);
408 attr
->type
= PERF_TYPE_HW_CACHE
;
414 static enum event_result
415 parse_single_tracepoint_event(char *sys_name
,
416 const char *evt_name
,
417 unsigned int evt_length
,
419 struct perf_counter_attr
*attr
,
422 char evt_path
[MAXPATHLEN
];
428 if (!strncmp(flags
, "record", strlen(flags
))) {
429 attr
->sample_type
|= PERF_SAMPLE_RAW
;
430 attr
->sample_type
|= PERF_SAMPLE_TIME
;
431 attr
->sample_type
|= PERF_SAMPLE_CPU
;
435 snprintf(evt_path
, MAXPATHLEN
, "%s/%s/%s/id", debugfs_path
,
438 fd
= open(evt_path
, O_RDONLY
);
442 if (read(fd
, id_buf
, sizeof(id_buf
)) < 0) {
450 attr
->type
= PERF_TYPE_TRACEPOINT
;
451 *strp
= evt_name
+ evt_length
;
456 /* sys + ':' + event + ':' + flags*/
457 #define MAX_EVOPT_LEN (MAX_EVENT_LENGTH * 2 + 2 + 128)
458 static enum event_result
459 parse_subsystem_tracepoint_event(char *sys_name
, char *flags
)
461 char evt_path
[MAXPATHLEN
];
462 struct dirent
*evt_ent
;
465 snprintf(evt_path
, MAXPATHLEN
, "%s/%s", debugfs_path
, sys_name
);
466 evt_dir
= opendir(evt_path
);
469 perror("Can't open event dir");
473 while ((evt_ent
= readdir(evt_dir
))) {
474 char event_opt
[MAX_EVOPT_LEN
+ 1];
476 unsigned int rem
= MAX_EVOPT_LEN
;
478 if (!strcmp(evt_ent
->d_name
, ".")
479 || !strcmp(evt_ent
->d_name
, "..")
480 || !strcmp(evt_ent
->d_name
, "enable")
481 || !strcmp(evt_ent
->d_name
, "filter"))
484 len
= snprintf(event_opt
, MAX_EVOPT_LEN
, "%s:%s", sys_name
,
491 if (rem
< strlen(flags
) + 1)
494 strcat(event_opt
, ":");
495 strcat(event_opt
, flags
);
498 if (parse_events(NULL
, event_opt
, 0))
502 return EVT_HANDLED_ALL
;
506 static enum event_result
parse_tracepoint_event(const char **strp
,
507 struct perf_counter_attr
*attr
)
509 const char *evt_name
;
511 char sys_name
[MAX_EVENT_LENGTH
];
512 unsigned int sys_length
, evt_length
;
514 if (valid_debugfs_mount(debugfs_path
))
517 evt_name
= strchr(*strp
, ':');
521 sys_length
= evt_name
- *strp
;
522 if (sys_length
>= MAX_EVENT_LENGTH
)
525 strncpy(sys_name
, *strp
, sys_length
);
526 sys_name
[sys_length
] = '\0';
527 evt_name
= evt_name
+ 1;
529 flags
= strchr(evt_name
, ':');
532 evt_name
= strndup(evt_name
, flags
- evt_name
);
536 evt_length
= strlen(evt_name
);
537 if (evt_length
>= MAX_EVENT_LENGTH
)
540 if (!strcmp(evt_name
, "*")) {
541 *strp
= evt_name
+ evt_length
;
542 return parse_subsystem_tracepoint_event(sys_name
, flags
);
544 return parse_single_tracepoint_event(sys_name
, evt_name
,
549 static int check_events(const char *str
, unsigned int i
)
553 n
= strlen(event_symbols
[i
].symbol
);
554 if (!strncmp(str
, event_symbols
[i
].symbol
, n
))
557 n
= strlen(event_symbols
[i
].alias
);
559 if (!strncmp(str
, event_symbols
[i
].alias
, n
))
564 static enum event_result
565 parse_symbolic_event(const char **strp
, struct perf_counter_attr
*attr
)
567 const char *str
= *strp
;
571 for (i
= 0; i
< ARRAY_SIZE(event_symbols
); i
++) {
572 n
= check_events(str
, i
);
574 attr
->type
= event_symbols
[i
].type
;
575 attr
->config
= event_symbols
[i
].config
;
583 static enum event_result
584 parse_raw_event(const char **strp
, struct perf_counter_attr
*attr
)
586 const char *str
= *strp
;
592 n
= hex2u64(str
+ 1, &config
);
595 attr
->type
= PERF_TYPE_RAW
;
596 attr
->config
= config
;
602 static enum event_result
603 parse_numeric_event(const char **strp
, struct perf_counter_attr
*attr
)
605 const char *str
= *strp
;
610 type
= strtoul(str
, &endp
, 0);
611 if (endp
> str
&& type
< PERF_TYPE_MAX
&& *endp
== ':') {
613 config
= strtoul(str
, &endp
, 0);
616 attr
->config
= config
;
624 static enum event_result
625 parse_event_modifier(const char **strp
, struct perf_counter_attr
*attr
)
627 const char *str
= *strp
;
628 int eu
= 1, ek
= 1, eh
= 1;
635 else if (*str
== 'k')
637 else if (*str
== 'h')
643 if (str
>= *strp
+ 2) {
645 attr
->exclude_user
= eu
;
646 attr
->exclude_kernel
= ek
;
647 attr
->exclude_hv
= eh
;
654 * Each event can have multiple symbolic names.
655 * Symbolic names are (almost) exactly matched.
657 static enum event_result
658 parse_event_symbols(const char **str
, struct perf_counter_attr
*attr
)
660 enum event_result ret
;
662 ret
= parse_tracepoint_event(str
, attr
);
663 if (ret
!= EVT_FAILED
)
666 ret
= parse_raw_event(str
, attr
);
667 if (ret
!= EVT_FAILED
)
670 ret
= parse_numeric_event(str
, attr
);
671 if (ret
!= EVT_FAILED
)
674 ret
= parse_symbolic_event(str
, attr
);
675 if (ret
!= EVT_FAILED
)
678 ret
= parse_generic_hw_event(str
, attr
);
679 if (ret
!= EVT_FAILED
)
685 parse_event_modifier(str
, attr
);
690 int parse_events(const struct option
*opt __used
, const char *str
, int unset __used
)
692 struct perf_counter_attr attr
;
693 enum event_result ret
;
696 if (nr_counters
== MAX_COUNTERS
)
699 memset(&attr
, 0, sizeof(attr
));
700 ret
= parse_event_symbols(&str
, &attr
);
701 if (ret
== EVT_FAILED
)
704 if (!(*str
== 0 || *str
== ',' || isspace(*str
)))
707 if (ret
!= EVT_HANDLED_ALL
) {
708 attrs
[nr_counters
] = attr
;
716 while (isspace(*str
))
723 static const char * const event_type_descriptors
[] = {
728 "Hardware cache event",
732 * Print the events from <debugfs_mount_point>/tracing/events
735 static void print_tracepoint_events(void)
737 DIR *sys_dir
, *evt_dir
;
738 struct dirent
*sys_next
, *evt_next
, sys_dirent
, evt_dirent
;
740 char evt_path
[MAXPATHLEN
];
742 if (valid_debugfs_mount(debugfs_path
))
745 sys_dir
= opendir(debugfs_path
);
748 sys_dir_fd
= dirfd(sys_dir
);
750 for_each_subsystem(sys_dir
, sys_dirent
, sys_next
) {
751 int dfd
= openat(sys_dir_fd
, sys_dirent
.d_name
,
752 O_RDONLY
|O_DIRECTORY
), evt_dir_fd
;
755 evt_dir
= fdopendir(dfd
);
760 evt_dir_fd
= dirfd(evt_dir
);
761 for_each_event(sys_dirent
, evt_dir
, evt_dirent
, evt_next
) {
762 snprintf(evt_path
, MAXPATHLEN
, "%s:%s",
763 sys_dirent
.d_name
, evt_dirent
.d_name
);
764 fprintf(stderr
, " %-42s [%s]\n", evt_path
,
765 event_type_descriptors
[PERF_TYPE_TRACEPOINT
+1]);
775 * Print the help text for the event symbols:
777 void print_events(void)
779 struct event_symbol
*syms
= event_symbols
;
780 unsigned int i
, type
, op
, prev_type
= -1;
783 fprintf(stderr
, "\n");
784 fprintf(stderr
, "List of pre-defined events (to be used in -e):\n");
786 for (i
= 0; i
< ARRAY_SIZE(event_symbols
); i
++, syms
++) {
787 type
= syms
->type
+ 1;
788 if (type
>= ARRAY_SIZE(event_type_descriptors
))
791 if (type
!= prev_type
)
792 fprintf(stderr
, "\n");
794 if (strlen(syms
->alias
))
795 sprintf(name
, "%s OR %s", syms
->symbol
, syms
->alias
);
797 strcpy(name
, syms
->symbol
);
798 fprintf(stderr
, " %-42s [%s]\n", name
,
799 event_type_descriptors
[type
]);
804 fprintf(stderr
, "\n");
805 for (type
= 0; type
< PERF_COUNT_HW_CACHE_MAX
; type
++) {
806 for (op
= 0; op
< PERF_COUNT_HW_CACHE_OP_MAX
; op
++) {
807 /* skip invalid cache type */
808 if (!is_cache_op_valid(type
, op
))
811 for (i
= 0; i
< PERF_COUNT_HW_CACHE_RESULT_MAX
; i
++) {
812 fprintf(stderr
, " %-42s [%s]\n",
813 event_cache_name(type
, op
, i
),
814 event_type_descriptors
[4]);
819 fprintf(stderr
, "\n");
820 fprintf(stderr
, " %-42s [raw hardware event descriptor]\n",
822 fprintf(stderr
, "\n");
824 print_tracepoint_events();