4 #include "parse-options.h"
5 #include "parse-events.h"
13 struct perf_event_attr attrs
[MAX_COUNTERS
];
14 char *filters
[MAX_COUNTERS
];
29 char debugfs_path
[MAXPATHLEN
];
31 #define CHW(x) .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_##x
32 #define CSW(x) .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_##x
34 static struct event_symbol event_symbols
[] = {
35 { CHW(CPU_CYCLES
), "cpu-cycles", "cycles" },
36 { CHW(INSTRUCTIONS
), "instructions", "" },
37 { CHW(CACHE_REFERENCES
), "cache-references", "" },
38 { CHW(CACHE_MISSES
), "cache-misses", "" },
39 { CHW(BRANCH_INSTRUCTIONS
), "branch-instructions", "branches" },
40 { CHW(BRANCH_MISSES
), "branch-misses", "" },
41 { CHW(BUS_CYCLES
), "bus-cycles", "" },
43 { CSW(CPU_CLOCK
), "cpu-clock", "" },
44 { CSW(TASK_CLOCK
), "task-clock", "" },
45 { CSW(PAGE_FAULTS
), "page-faults", "faults" },
46 { CSW(PAGE_FAULTS_MIN
), "minor-faults", "" },
47 { CSW(PAGE_FAULTS_MAJ
), "major-faults", "" },
48 { CSW(CONTEXT_SWITCHES
), "context-switches", "cs" },
49 { CSW(CPU_MIGRATIONS
), "cpu-migrations", "migrations" },
52 #define __PERF_EVENT_FIELD(config, name) \
53 ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
55 #define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
56 #define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
57 #define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
58 #define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
60 static const char *hw_event_names
[] = {
70 static const char *sw_event_names
[] = {
82 static const char *hw_cache
[][MAX_ALIASES
] = {
83 { "L1-dcache", "l1-d", "l1d", "L1-data", },
84 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
86 { "dTLB", "d-tlb", "Data-TLB", },
87 { "iTLB", "i-tlb", "Instruction-TLB", },
88 { "branch", "branches", "bpu", "btb", "bpc", },
91 static const char *hw_cache_op
[][MAX_ALIASES
] = {
92 { "load", "loads", "read", },
93 { "store", "stores", "write", },
94 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
97 static const char *hw_cache_result
[][MAX_ALIASES
] = {
98 { "refs", "Reference", "ops", "access", },
99 { "misses", "miss", },
102 #define C(x) PERF_COUNT_HW_CACHE_##x
103 #define CACHE_READ (1 << C(OP_READ))
104 #define CACHE_WRITE (1 << C(OP_WRITE))
105 #define CACHE_PREFETCH (1 << C(OP_PREFETCH))
106 #define COP(x) (1 << x)
109 * cache operartion stat
110 * L1I : Read and prefetch only
111 * ITLB and BPU : Read-only
113 static unsigned long hw_cache_stat
[C(MAX
)] = {
114 [C(L1D
)] = (CACHE_READ
| CACHE_WRITE
| CACHE_PREFETCH
),
115 [C(L1I
)] = (CACHE_READ
| CACHE_PREFETCH
),
116 [C(LL
)] = (CACHE_READ
| CACHE_WRITE
| CACHE_PREFETCH
),
117 [C(DTLB
)] = (CACHE_READ
| CACHE_WRITE
| CACHE_PREFETCH
),
118 [C(ITLB
)] = (CACHE_READ
),
119 [C(BPU
)] = (CACHE_READ
),
122 #define for_each_subsystem(sys_dir, sys_dirent, sys_next) \
123 while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \
124 if (sys_dirent.d_type == DT_DIR && \
125 (strcmp(sys_dirent.d_name, ".")) && \
126 (strcmp(sys_dirent.d_name, "..")))
128 static int tp_event_has_id(struct dirent
*sys_dir
, struct dirent
*evt_dir
)
130 char evt_path
[MAXPATHLEN
];
133 snprintf(evt_path
, MAXPATHLEN
, "%s/%s/%s/id", debugfs_path
,
134 sys_dir
->d_name
, evt_dir
->d_name
);
135 fd
= open(evt_path
, O_RDONLY
);
143 #define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) \
144 while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \
145 if (evt_dirent.d_type == DT_DIR && \
146 (strcmp(evt_dirent.d_name, ".")) && \
147 (strcmp(evt_dirent.d_name, "..")) && \
148 (!tp_event_has_id(&sys_dirent, &evt_dirent)))
150 #define MAX_EVENT_LENGTH 512
152 int valid_debugfs_mount(const char *debugfs
)
156 if (statfs(debugfs
, &st_fs
) < 0)
158 else if (st_fs
.f_type
!= (long) DEBUGFS_MAGIC
)
163 struct tracepoint_path
*tracepoint_id_to_path(u64 config
)
165 struct tracepoint_path
*path
= NULL
;
166 DIR *sys_dir
, *evt_dir
;
167 struct dirent
*sys_next
, *evt_next
, sys_dirent
, evt_dirent
;
171 char evt_path
[MAXPATHLEN
];
172 char dir_path
[MAXPATHLEN
];
174 if (valid_debugfs_mount(debugfs_path
))
177 sys_dir
= opendir(debugfs_path
);
181 for_each_subsystem(sys_dir
, sys_dirent
, sys_next
) {
183 snprintf(dir_path
, MAXPATHLEN
, "%s/%s", debugfs_path
,
185 evt_dir
= opendir(dir_path
);
189 for_each_event(sys_dirent
, evt_dir
, evt_dirent
, evt_next
) {
191 snprintf(evt_path
, MAXPATHLEN
, "%s/%s/id", dir_path
,
193 fd
= open(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
,
231 #define TP_PATH_LEN (MAX_EVENT_LENGTH * 2 + 1)
232 static const char *tracepoint_id_to_name(u64 config
)
234 static char buf
[TP_PATH_LEN
];
235 struct tracepoint_path
*path
;
237 path
= tracepoint_id_to_path(config
);
239 snprintf(buf
, TP_PATH_LEN
, "%s:%s", path
->system
, path
->name
);
244 snprintf(buf
, TP_PATH_LEN
, "%s:%s", "unknown", "unknown");
249 static int is_cache_op_valid(u8 cache_type
, u8 cache_op
)
251 if (hw_cache_stat
[cache_type
] & COP(cache_op
))
252 return 1; /* valid */
254 return 0; /* invalid */
257 static char *event_cache_name(u8 cache_type
, u8 cache_op
, u8 cache_result
)
259 static char name
[50];
262 sprintf(name
, "%s-%s-%s", hw_cache
[cache_type
][0],
263 hw_cache_op
[cache_op
][0],
264 hw_cache_result
[cache_result
][0]);
266 sprintf(name
, "%s-%s", hw_cache
[cache_type
][0],
267 hw_cache_op
[cache_op
][1]);
273 const char *event_name(int counter
)
275 u64 config
= attrs
[counter
].config
;
276 int type
= attrs
[counter
].type
;
278 return __event_name(type
, config
);
281 const char *__event_name(int type
, u64 config
)
285 if (type
== PERF_TYPE_RAW
) {
286 sprintf(buf
, "raw 0x%llx", config
);
291 case PERF_TYPE_HARDWARE
:
292 if (config
< PERF_COUNT_HW_MAX
)
293 return hw_event_names
[config
];
294 return "unknown-hardware";
296 case PERF_TYPE_HW_CACHE
: {
297 u8 cache_type
, cache_op
, cache_result
;
299 cache_type
= (config
>> 0) & 0xff;
300 if (cache_type
> PERF_COUNT_HW_CACHE_MAX
)
301 return "unknown-ext-hardware-cache-type";
303 cache_op
= (config
>> 8) & 0xff;
304 if (cache_op
> PERF_COUNT_HW_CACHE_OP_MAX
)
305 return "unknown-ext-hardware-cache-op";
307 cache_result
= (config
>> 16) & 0xff;
308 if (cache_result
> PERF_COUNT_HW_CACHE_RESULT_MAX
)
309 return "unknown-ext-hardware-cache-result";
311 if (!is_cache_op_valid(cache_type
, cache_op
))
312 return "invalid-cache";
314 return event_cache_name(cache_type
, cache_op
, cache_result
);
317 case PERF_TYPE_SOFTWARE
:
318 if (config
< PERF_COUNT_SW_MAX
)
319 return sw_event_names
[config
];
320 return "unknown-software";
322 case PERF_TYPE_TRACEPOINT
:
323 return tracepoint_id_to_name(config
);
332 static int parse_aliases(const char **str
, const char *names
[][MAX_ALIASES
], int size
)
337 for (i
= 0; i
< size
; i
++) {
338 for (j
= 0; j
< MAX_ALIASES
&& names
[i
][j
]; j
++) {
339 n
= strlen(names
[i
][j
]);
340 if (n
> longest
&& !strncasecmp(*str
, names
[i
][j
], n
))
352 static enum event_result
353 parse_generic_hw_event(const char **str
, struct perf_event_attr
*attr
)
355 const char *s
= *str
;
356 int cache_type
= -1, cache_op
= -1, cache_result
= -1;
358 cache_type
= parse_aliases(&s
, hw_cache
, PERF_COUNT_HW_CACHE_MAX
);
360 * No fallback - if we cannot get a clear cache type
363 if (cache_type
== -1)
366 while ((cache_op
== -1 || cache_result
== -1) && *s
== '-') {
369 if (cache_op
== -1) {
370 cache_op
= parse_aliases(&s
, hw_cache_op
,
371 PERF_COUNT_HW_CACHE_OP_MAX
);
373 if (!is_cache_op_valid(cache_type
, cache_op
))
379 if (cache_result
== -1) {
380 cache_result
= parse_aliases(&s
, hw_cache_result
,
381 PERF_COUNT_HW_CACHE_RESULT_MAX
);
382 if (cache_result
>= 0)
387 * Can't parse this as a cache op or result, so back up
395 * Fall back to reads:
398 cache_op
= PERF_COUNT_HW_CACHE_OP_READ
;
401 * Fall back to accesses:
403 if (cache_result
== -1)
404 cache_result
= PERF_COUNT_HW_CACHE_RESULT_ACCESS
;
406 attr
->config
= cache_type
| (cache_op
<< 8) | (cache_result
<< 16);
407 attr
->type
= PERF_TYPE_HW_CACHE
;
413 static enum event_result
414 parse_single_tracepoint_event(char *sys_name
,
415 const char *evt_name
,
416 unsigned int evt_length
,
418 struct perf_event_attr
*attr
,
421 char evt_path
[MAXPATHLEN
];
427 if (!strncmp(flags
, "record", strlen(flags
))) {
428 attr
->sample_type
|= PERF_SAMPLE_RAW
;
429 attr
->sample_type
|= PERF_SAMPLE_TIME
;
430 attr
->sample_type
|= PERF_SAMPLE_CPU
;
434 snprintf(evt_path
, MAXPATHLEN
, "%s/%s/%s/id", debugfs_path
,
437 fd
= open(evt_path
, O_RDONLY
);
441 if (read(fd
, id_buf
, sizeof(id_buf
)) < 0) {
449 attr
->type
= PERF_TYPE_TRACEPOINT
;
450 *strp
= evt_name
+ evt_length
;
455 /* sys + ':' + event + ':' + flags*/
456 #define MAX_EVOPT_LEN (MAX_EVENT_LENGTH * 2 + 2 + 128)
457 static enum event_result
458 parse_subsystem_tracepoint_event(char *sys_name
, char *flags
)
460 char evt_path
[MAXPATHLEN
];
461 struct dirent
*evt_ent
;
464 snprintf(evt_path
, MAXPATHLEN
, "%s/%s", debugfs_path
, sys_name
);
465 evt_dir
= opendir(evt_path
);
468 perror("Can't open event dir");
472 while ((evt_ent
= readdir(evt_dir
))) {
473 char event_opt
[MAX_EVOPT_LEN
+ 1];
475 unsigned int rem
= MAX_EVOPT_LEN
;
477 if (!strcmp(evt_ent
->d_name
, ".")
478 || !strcmp(evt_ent
->d_name
, "..")
479 || !strcmp(evt_ent
->d_name
, "enable")
480 || !strcmp(evt_ent
->d_name
, "filter"))
483 len
= snprintf(event_opt
, MAX_EVOPT_LEN
, "%s:%s", sys_name
,
490 if (rem
< strlen(flags
) + 1)
493 strcat(event_opt
, ":");
494 strcat(event_opt
, flags
);
497 if (parse_events(NULL
, event_opt
, 0))
501 return EVT_HANDLED_ALL
;
505 static enum event_result
parse_tracepoint_event(const char **strp
,
506 struct perf_event_attr
*attr
)
508 const char *evt_name
;
510 char sys_name
[MAX_EVENT_LENGTH
];
511 unsigned int sys_length
, evt_length
;
513 if (valid_debugfs_mount(debugfs_path
))
516 evt_name
= strchr(*strp
, ':');
520 sys_length
= evt_name
- *strp
;
521 if (sys_length
>= MAX_EVENT_LENGTH
)
524 strncpy(sys_name
, *strp
, sys_length
);
525 sys_name
[sys_length
] = '\0';
526 evt_name
= evt_name
+ 1;
528 flags
= strchr(evt_name
, ':');
531 evt_name
= strndup(evt_name
, flags
- evt_name
);
535 evt_length
= strlen(evt_name
);
536 if (evt_length
>= MAX_EVENT_LENGTH
)
539 if (!strcmp(evt_name
, "*")) {
540 *strp
= evt_name
+ evt_length
;
541 return parse_subsystem_tracepoint_event(sys_name
, flags
);
543 return parse_single_tracepoint_event(sys_name
, evt_name
,
548 static int check_events(const char *str
, unsigned int i
)
552 n
= strlen(event_symbols
[i
].symbol
);
553 if (!strncmp(str
, event_symbols
[i
].symbol
, n
))
556 n
= strlen(event_symbols
[i
].alias
);
558 if (!strncmp(str
, event_symbols
[i
].alias
, n
))
563 static enum event_result
564 parse_symbolic_event(const char **strp
, struct perf_event_attr
*attr
)
566 const char *str
= *strp
;
570 for (i
= 0; i
< ARRAY_SIZE(event_symbols
); i
++) {
571 n
= check_events(str
, i
);
573 attr
->type
= event_symbols
[i
].type
;
574 attr
->config
= event_symbols
[i
].config
;
582 static enum event_result
583 parse_raw_event(const char **strp
, struct perf_event_attr
*attr
)
585 const char *str
= *strp
;
591 n
= hex2u64(str
+ 1, &config
);
594 attr
->type
= PERF_TYPE_RAW
;
595 attr
->config
= config
;
601 static enum event_result
602 parse_numeric_event(const char **strp
, struct perf_event_attr
*attr
)
604 const char *str
= *strp
;
609 type
= strtoul(str
, &endp
, 0);
610 if (endp
> str
&& type
< PERF_TYPE_MAX
&& *endp
== ':') {
612 config
= strtoul(str
, &endp
, 0);
615 attr
->config
= config
;
623 static enum event_result
624 parse_event_modifier(const char **strp
, struct perf_event_attr
*attr
)
626 const char *str
= *strp
;
627 int eu
= 1, ek
= 1, eh
= 1;
634 else if (*str
== 'k')
636 else if (*str
== 'h')
642 if (str
>= *strp
+ 2) {
644 attr
->exclude_user
= eu
;
645 attr
->exclude_kernel
= ek
;
646 attr
->exclude_hv
= eh
;
653 * Each event can have multiple symbolic names.
654 * Symbolic names are (almost) exactly matched.
656 static enum event_result
657 parse_event_symbols(const char **str
, struct perf_event_attr
*attr
)
659 enum event_result ret
;
661 ret
= parse_tracepoint_event(str
, attr
);
662 if (ret
!= EVT_FAILED
)
665 ret
= parse_raw_event(str
, attr
);
666 if (ret
!= EVT_FAILED
)
669 ret
= parse_numeric_event(str
, attr
);
670 if (ret
!= EVT_FAILED
)
673 ret
= parse_symbolic_event(str
, attr
);
674 if (ret
!= EVT_FAILED
)
677 ret
= parse_generic_hw_event(str
, attr
);
678 if (ret
!= EVT_FAILED
)
684 parse_event_modifier(str
, attr
);
689 static void store_event_type(const char *orgname
)
691 char filename
[PATH_MAX
], *c
;
695 sprintf(filename
, "%s/", debugfs_path
);
696 strncat(filename
, orgname
, strlen(orgname
));
697 strcat(filename
, "/id");
699 c
= strchr(filename
, ':');
703 file
= fopen(filename
, "r");
706 if (fscanf(file
, "%i", &id
) < 1)
707 die("cannot store event ID");
709 perf_header__push_event(id
, orgname
);
712 int parse_events(const struct option
*opt __used
, const char *str
, int unset __used
)
714 struct perf_event_attr attr
;
715 enum event_result ret
;
717 if (strchr(str
, ':'))
718 store_event_type(str
);
721 if (nr_counters
== MAX_COUNTERS
)
724 memset(&attr
, 0, sizeof(attr
));
725 ret
= parse_event_symbols(&str
, &attr
);
726 if (ret
== EVT_FAILED
)
729 if (!(*str
== 0 || *str
== ',' || isspace(*str
)))
732 if (ret
!= EVT_HANDLED_ALL
) {
733 attrs
[nr_counters
] = attr
;
741 while (isspace(*str
))
748 int parse_filter(const struct option
*opt __used
, const char *str
,
751 int i
= nr_counters
- 1;
752 int len
= strlen(str
);
754 if (i
< 0 || attrs
[i
].type
!= PERF_TYPE_TRACEPOINT
) {
756 "-F option should follow a -e tracepoint option\n");
760 filters
[i
] = malloc(len
+ 1);
762 fprintf(stderr
, "not enough memory to hold filter string\n");
765 strcpy(filters
[i
], str
);
770 static const char * const event_type_descriptors
[] = {
775 "Hardware cache event",
779 * Print the events from <debugfs_mount_point>/tracing/events
782 static void print_tracepoint_events(void)
784 DIR *sys_dir
, *evt_dir
;
785 struct dirent
*sys_next
, *evt_next
, sys_dirent
, evt_dirent
;
786 char evt_path
[MAXPATHLEN
];
787 char dir_path
[MAXPATHLEN
];
789 if (valid_debugfs_mount(debugfs_path
))
792 sys_dir
= opendir(debugfs_path
);
796 for_each_subsystem(sys_dir
, sys_dirent
, sys_next
) {
798 snprintf(dir_path
, MAXPATHLEN
, "%s/%s", debugfs_path
,
800 evt_dir
= opendir(dir_path
);
804 for_each_event(sys_dirent
, evt_dir
, evt_dirent
, evt_next
) {
805 snprintf(evt_path
, MAXPATHLEN
, "%s:%s",
806 sys_dirent
.d_name
, evt_dirent
.d_name
);
807 fprintf(stderr
, " %-42s [%s]\n", evt_path
,
808 event_type_descriptors
[PERF_TYPE_TRACEPOINT
+1]);
816 * Print the help text for the event symbols:
818 void print_events(void)
820 struct event_symbol
*syms
= event_symbols
;
821 unsigned int i
, type
, op
, prev_type
= -1;
824 fprintf(stderr
, "\n");
825 fprintf(stderr
, "List of pre-defined events (to be used in -e):\n");
827 for (i
= 0; i
< ARRAY_SIZE(event_symbols
); i
++, syms
++) {
828 type
= syms
->type
+ 1;
829 if (type
>= ARRAY_SIZE(event_type_descriptors
))
832 if (type
!= prev_type
)
833 fprintf(stderr
, "\n");
835 if (strlen(syms
->alias
))
836 sprintf(name
, "%s OR %s", syms
->symbol
, syms
->alias
);
838 strcpy(name
, syms
->symbol
);
839 fprintf(stderr
, " %-42s [%s]\n", name
,
840 event_type_descriptors
[type
]);
845 fprintf(stderr
, "\n");
846 for (type
= 0; type
< PERF_COUNT_HW_CACHE_MAX
; type
++) {
847 for (op
= 0; op
< PERF_COUNT_HW_CACHE_OP_MAX
; op
++) {
848 /* skip invalid cache type */
849 if (!is_cache_op_valid(type
, op
))
852 for (i
= 0; i
< PERF_COUNT_HW_CACHE_RESULT_MAX
; i
++) {
853 fprintf(stderr
, " %-42s [%s]\n",
854 event_cache_name(type
, op
, i
),
855 event_type_descriptors
[4]);
860 fprintf(stderr
, "\n");
861 fprintf(stderr
, " %-42s [raw hardware event descriptor]\n",
863 fprintf(stderr
, "\n");
865 print_tracepoint_events();