4 #include "parse-options.h"
5 #include "parse-events.h"
14 struct perf_event_attr attrs
[MAX_COUNTERS
];
15 char *filters
[MAX_COUNTERS
];
30 char debugfs_path
[MAXPATHLEN
];
32 #define CHW(x) .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_##x
33 #define CSW(x) .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_##x
35 static struct event_symbol event_symbols
[] = {
36 { CHW(CPU_CYCLES
), "cpu-cycles", "cycles" },
37 { CHW(INSTRUCTIONS
), "instructions", "" },
38 { CHW(CACHE_REFERENCES
), "cache-references", "" },
39 { CHW(CACHE_MISSES
), "cache-misses", "" },
40 { CHW(BRANCH_INSTRUCTIONS
), "branch-instructions", "branches" },
41 { CHW(BRANCH_MISSES
), "branch-misses", "" },
42 { CHW(BUS_CYCLES
), "bus-cycles", "" },
44 { CSW(CPU_CLOCK
), "cpu-clock", "" },
45 { CSW(TASK_CLOCK
), "task-clock", "" },
46 { CSW(PAGE_FAULTS
), "page-faults", "faults" },
47 { CSW(PAGE_FAULTS_MIN
), "minor-faults", "" },
48 { CSW(PAGE_FAULTS_MAJ
), "major-faults", "" },
49 { CSW(CONTEXT_SWITCHES
), "context-switches", "cs" },
50 { CSW(CPU_MIGRATIONS
), "cpu-migrations", "migrations" },
51 { CSW(ALIGNMENT_FAULTS
), "alignment-faults", "" },
52 { CSW(EMULATION_FAULTS
), "emulation-faults", "" },
55 #define __PERF_EVENT_FIELD(config, name) \
56 ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
58 #define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
59 #define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
60 #define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
61 #define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
63 static const char *hw_event_names
[] = {
73 static const char *sw_event_names
[] = {
87 static const char *hw_cache
[][MAX_ALIASES
] = {
88 { "L1-dcache", "l1-d", "l1d", "L1-data", },
89 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
91 { "dTLB", "d-tlb", "Data-TLB", },
92 { "iTLB", "i-tlb", "Instruction-TLB", },
93 { "branch", "branches", "bpu", "btb", "bpc", },
96 static const char *hw_cache_op
[][MAX_ALIASES
] = {
97 { "load", "loads", "read", },
98 { "store", "stores", "write", },
99 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
102 static const char *hw_cache_result
[][MAX_ALIASES
] = {
103 { "refs", "Reference", "ops", "access", },
104 { "misses", "miss", },
107 #define C(x) PERF_COUNT_HW_CACHE_##x
108 #define CACHE_READ (1 << C(OP_READ))
109 #define CACHE_WRITE (1 << C(OP_WRITE))
110 #define CACHE_PREFETCH (1 << C(OP_PREFETCH))
111 #define COP(x) (1 << x)
114 * cache operartion stat
115 * L1I : Read and prefetch only
116 * ITLB and BPU : Read-only
118 static unsigned long hw_cache_stat
[C(MAX
)] = {
119 [C(L1D
)] = (CACHE_READ
| CACHE_WRITE
| CACHE_PREFETCH
),
120 [C(L1I
)] = (CACHE_READ
| CACHE_PREFETCH
),
121 [C(LL
)] = (CACHE_READ
| CACHE_WRITE
| CACHE_PREFETCH
),
122 [C(DTLB
)] = (CACHE_READ
| CACHE_WRITE
| CACHE_PREFETCH
),
123 [C(ITLB
)] = (CACHE_READ
),
124 [C(BPU
)] = (CACHE_READ
),
127 #define for_each_subsystem(sys_dir, sys_dirent, sys_next) \
128 while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \
129 if (sys_dirent.d_type == DT_DIR && \
130 (strcmp(sys_dirent.d_name, ".")) && \
131 (strcmp(sys_dirent.d_name, "..")))
133 static int tp_event_has_id(struct dirent
*sys_dir
, struct dirent
*evt_dir
)
135 char evt_path
[MAXPATHLEN
];
138 snprintf(evt_path
, MAXPATHLEN
, "%s/%s/%s/id", debugfs_path
,
139 sys_dir
->d_name
, evt_dir
->d_name
);
140 fd
= open(evt_path
, O_RDONLY
);
148 #define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) \
149 while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \
150 if (evt_dirent.d_type == DT_DIR && \
151 (strcmp(evt_dirent.d_name, ".")) && \
152 (strcmp(evt_dirent.d_name, "..")) && \
153 (!tp_event_has_id(&sys_dirent, &evt_dirent)))
155 #define MAX_EVENT_LENGTH 512
158 struct tracepoint_path
*tracepoint_id_to_path(u64 config
)
160 struct tracepoint_path
*path
= NULL
;
161 DIR *sys_dir
, *evt_dir
;
162 struct dirent
*sys_next
, *evt_next
, sys_dirent
, evt_dirent
;
166 char evt_path
[MAXPATHLEN
];
167 char dir_path
[MAXPATHLEN
];
169 if (debugfs_valid_mountpoint(debugfs_path
))
172 sys_dir
= opendir(debugfs_path
);
176 for_each_subsystem(sys_dir
, sys_dirent
, sys_next
) {
178 snprintf(dir_path
, MAXPATHLEN
, "%s/%s", debugfs_path
,
180 evt_dir
= opendir(dir_path
);
184 for_each_event(sys_dirent
, evt_dir
, evt_dirent
, evt_next
) {
186 snprintf(evt_path
, MAXPATHLEN
, "%s/%s/id", dir_path
,
188 fd
= open(evt_path
, O_RDONLY
);
191 if (read(fd
, id_buf
, sizeof(id_buf
)) < 0) {
200 path
= calloc(1, sizeof(path
));
201 path
->system
= malloc(MAX_EVENT_LENGTH
);
206 path
->name
= malloc(MAX_EVENT_LENGTH
);
212 strncpy(path
->system
, sys_dirent
.d_name
,
214 strncpy(path
->name
, evt_dirent
.d_name
,
226 #define TP_PATH_LEN (MAX_EVENT_LENGTH * 2 + 1)
227 static const char *tracepoint_id_to_name(u64 config
)
229 static char buf
[TP_PATH_LEN
];
230 struct tracepoint_path
*path
;
232 path
= tracepoint_id_to_path(config
);
234 snprintf(buf
, TP_PATH_LEN
, "%s:%s", path
->system
, path
->name
);
239 snprintf(buf
, TP_PATH_LEN
, "%s:%s", "unknown", "unknown");
244 static int is_cache_op_valid(u8 cache_type
, u8 cache_op
)
246 if (hw_cache_stat
[cache_type
] & COP(cache_op
))
247 return 1; /* valid */
249 return 0; /* invalid */
252 static char *event_cache_name(u8 cache_type
, u8 cache_op
, u8 cache_result
)
254 static char name
[50];
257 sprintf(name
, "%s-%s-%s", hw_cache
[cache_type
][0],
258 hw_cache_op
[cache_op
][0],
259 hw_cache_result
[cache_result
][0]);
261 sprintf(name
, "%s-%s", hw_cache
[cache_type
][0],
262 hw_cache_op
[cache_op
][1]);
268 const char *event_name(int counter
)
270 u64 config
= attrs
[counter
].config
;
271 int type
= attrs
[counter
].type
;
273 return __event_name(type
, config
);
276 const char *__event_name(int type
, u64 config
)
280 if (type
== PERF_TYPE_RAW
) {
281 sprintf(buf
, "raw 0x%llx", config
);
286 case PERF_TYPE_HARDWARE
:
287 if (config
< PERF_COUNT_HW_MAX
)
288 return hw_event_names
[config
];
289 return "unknown-hardware";
291 case PERF_TYPE_HW_CACHE
: {
292 u8 cache_type
, cache_op
, cache_result
;
294 cache_type
= (config
>> 0) & 0xff;
295 if (cache_type
> PERF_COUNT_HW_CACHE_MAX
)
296 return "unknown-ext-hardware-cache-type";
298 cache_op
= (config
>> 8) & 0xff;
299 if (cache_op
> PERF_COUNT_HW_CACHE_OP_MAX
)
300 return "unknown-ext-hardware-cache-op";
302 cache_result
= (config
>> 16) & 0xff;
303 if (cache_result
> PERF_COUNT_HW_CACHE_RESULT_MAX
)
304 return "unknown-ext-hardware-cache-result";
306 if (!is_cache_op_valid(cache_type
, cache_op
))
307 return "invalid-cache";
309 return event_cache_name(cache_type
, cache_op
, cache_result
);
312 case PERF_TYPE_SOFTWARE
:
313 if (config
< PERF_COUNT_SW_MAX
)
314 return sw_event_names
[config
];
315 return "unknown-software";
317 case PERF_TYPE_TRACEPOINT
:
318 return tracepoint_id_to_name(config
);
327 static int parse_aliases(const char **str
, const char *names
[][MAX_ALIASES
], int size
)
332 for (i
= 0; i
< size
; i
++) {
333 for (j
= 0; j
< MAX_ALIASES
&& names
[i
][j
]; j
++) {
334 n
= strlen(names
[i
][j
]);
335 if (n
> longest
&& !strncasecmp(*str
, names
[i
][j
], n
))
347 static enum event_result
348 parse_generic_hw_event(const char **str
, struct perf_event_attr
*attr
)
350 const char *s
= *str
;
351 int cache_type
= -1, cache_op
= -1, cache_result
= -1;
353 cache_type
= parse_aliases(&s
, hw_cache
, PERF_COUNT_HW_CACHE_MAX
);
355 * No fallback - if we cannot get a clear cache type
358 if (cache_type
== -1)
361 while ((cache_op
== -1 || cache_result
== -1) && *s
== '-') {
364 if (cache_op
== -1) {
365 cache_op
= parse_aliases(&s
, hw_cache_op
,
366 PERF_COUNT_HW_CACHE_OP_MAX
);
368 if (!is_cache_op_valid(cache_type
, cache_op
))
374 if (cache_result
== -1) {
375 cache_result
= parse_aliases(&s
, hw_cache_result
,
376 PERF_COUNT_HW_CACHE_RESULT_MAX
);
377 if (cache_result
>= 0)
382 * Can't parse this as a cache op or result, so back up
390 * Fall back to reads:
393 cache_op
= PERF_COUNT_HW_CACHE_OP_READ
;
396 * Fall back to accesses:
398 if (cache_result
== -1)
399 cache_result
= PERF_COUNT_HW_CACHE_RESULT_ACCESS
;
401 attr
->config
= cache_type
| (cache_op
<< 8) | (cache_result
<< 16);
402 attr
->type
= PERF_TYPE_HW_CACHE
;
408 static enum event_result
409 parse_single_tracepoint_event(char *sys_name
,
410 const char *evt_name
,
411 unsigned int evt_length
,
413 struct perf_event_attr
*attr
,
416 char evt_path
[MAXPATHLEN
];
422 if (!strncmp(flags
, "record", strlen(flags
))) {
423 attr
->sample_type
|= PERF_SAMPLE_RAW
;
424 attr
->sample_type
|= PERF_SAMPLE_TIME
;
425 attr
->sample_type
|= PERF_SAMPLE_CPU
;
429 snprintf(evt_path
, MAXPATHLEN
, "%s/%s/%s/id", debugfs_path
,
432 fd
= open(evt_path
, O_RDONLY
);
436 if (read(fd
, id_buf
, sizeof(id_buf
)) < 0) {
444 attr
->type
= PERF_TYPE_TRACEPOINT
;
445 *strp
= evt_name
+ evt_length
;
450 /* sys + ':' + event + ':' + flags*/
451 #define MAX_EVOPT_LEN (MAX_EVENT_LENGTH * 2 + 2 + 128)
452 static enum event_result
453 parse_subsystem_tracepoint_event(char *sys_name
, char *flags
)
455 char evt_path
[MAXPATHLEN
];
456 struct dirent
*evt_ent
;
459 snprintf(evt_path
, MAXPATHLEN
, "%s/%s", debugfs_path
, sys_name
);
460 evt_dir
= opendir(evt_path
);
463 perror("Can't open event dir");
467 while ((evt_ent
= readdir(evt_dir
))) {
468 char event_opt
[MAX_EVOPT_LEN
+ 1];
470 unsigned int rem
= MAX_EVOPT_LEN
;
472 if (!strcmp(evt_ent
->d_name
, ".")
473 || !strcmp(evt_ent
->d_name
, "..")
474 || !strcmp(evt_ent
->d_name
, "enable")
475 || !strcmp(evt_ent
->d_name
, "filter"))
478 len
= snprintf(event_opt
, MAX_EVOPT_LEN
, "%s:%s", sys_name
,
485 if (rem
< strlen(flags
) + 1)
488 strcat(event_opt
, ":");
489 strcat(event_opt
, flags
);
492 if (parse_events(NULL
, event_opt
, 0))
496 return EVT_HANDLED_ALL
;
500 static enum event_result
parse_tracepoint_event(const char **strp
,
501 struct perf_event_attr
*attr
)
503 const char *evt_name
;
505 char sys_name
[MAX_EVENT_LENGTH
];
506 unsigned int sys_length
, evt_length
;
508 if (debugfs_valid_mountpoint(debugfs_path
))
511 evt_name
= strchr(*strp
, ':');
515 sys_length
= evt_name
- *strp
;
516 if (sys_length
>= MAX_EVENT_LENGTH
)
519 strncpy(sys_name
, *strp
, sys_length
);
520 sys_name
[sys_length
] = '\0';
521 evt_name
= evt_name
+ 1;
523 flags
= strchr(evt_name
, ':');
526 evt_name
= strndup(evt_name
, flags
- evt_name
);
530 evt_length
= strlen(evt_name
);
531 if (evt_length
>= MAX_EVENT_LENGTH
)
534 if (!strcmp(evt_name
, "*")) {
535 *strp
= evt_name
+ evt_length
;
536 return parse_subsystem_tracepoint_event(sys_name
, flags
);
538 return parse_single_tracepoint_event(sys_name
, evt_name
,
543 static int check_events(const char *str
, unsigned int i
)
547 n
= strlen(event_symbols
[i
].symbol
);
548 if (!strncmp(str
, event_symbols
[i
].symbol
, n
))
551 n
= strlen(event_symbols
[i
].alias
);
553 if (!strncmp(str
, event_symbols
[i
].alias
, n
))
558 static enum event_result
559 parse_symbolic_event(const char **strp
, struct perf_event_attr
*attr
)
561 const char *str
= *strp
;
565 for (i
= 0; i
< ARRAY_SIZE(event_symbols
); i
++) {
566 n
= check_events(str
, i
);
568 attr
->type
= event_symbols
[i
].type
;
569 attr
->config
= event_symbols
[i
].config
;
577 static enum event_result
578 parse_raw_event(const char **strp
, struct perf_event_attr
*attr
)
580 const char *str
= *strp
;
586 n
= hex2u64(str
+ 1, &config
);
589 attr
->type
= PERF_TYPE_RAW
;
590 attr
->config
= config
;
596 static enum event_result
597 parse_numeric_event(const char **strp
, struct perf_event_attr
*attr
)
599 const char *str
= *strp
;
604 type
= strtoul(str
, &endp
, 0);
605 if (endp
> str
&& type
< PERF_TYPE_MAX
&& *endp
== ':') {
607 config
= strtoul(str
, &endp
, 0);
610 attr
->config
= config
;
618 static enum event_result
619 parse_event_modifier(const char **strp
, struct perf_event_attr
*attr
)
621 const char *str
= *strp
;
622 int eu
= 1, ek
= 1, eh
= 1;
629 else if (*str
== 'k')
631 else if (*str
== 'h')
637 if (str
>= *strp
+ 2) {
639 attr
->exclude_user
= eu
;
640 attr
->exclude_kernel
= ek
;
641 attr
->exclude_hv
= eh
;
648 * Each event can have multiple symbolic names.
649 * Symbolic names are (almost) exactly matched.
651 static enum event_result
652 parse_event_symbols(const char **str
, struct perf_event_attr
*attr
)
654 enum event_result ret
;
656 ret
= parse_tracepoint_event(str
, attr
);
657 if (ret
!= EVT_FAILED
)
660 ret
= parse_raw_event(str
, attr
);
661 if (ret
!= EVT_FAILED
)
664 ret
= parse_numeric_event(str
, attr
);
665 if (ret
!= EVT_FAILED
)
668 ret
= parse_symbolic_event(str
, attr
);
669 if (ret
!= EVT_FAILED
)
672 ret
= parse_generic_hw_event(str
, attr
);
673 if (ret
!= EVT_FAILED
)
676 fprintf(stderr
, "invalid or unsupported event: '%s'\n", *str
);
677 fprintf(stderr
, "Run 'perf list' for a list of valid events\n");
681 parse_event_modifier(str
, attr
);
686 static void store_event_type(const char *orgname
)
688 char filename
[PATH_MAX
], *c
;
692 sprintf(filename
, "%s/", debugfs_path
);
693 strncat(filename
, orgname
, strlen(orgname
));
694 strcat(filename
, "/id");
696 c
= strchr(filename
, ':');
700 file
= fopen(filename
, "r");
703 if (fscanf(file
, "%i", &id
) < 1)
704 die("cannot store event ID");
706 perf_header__push_event(id
, orgname
);
709 int parse_events(const struct option
*opt __used
, const char *str
, int unset __used
)
711 struct perf_event_attr attr
;
712 enum event_result ret
;
714 if (strchr(str
, ':'))
715 store_event_type(str
);
718 if (nr_counters
== MAX_COUNTERS
)
721 memset(&attr
, 0, sizeof(attr
));
722 ret
= parse_event_symbols(&str
, &attr
);
723 if (ret
== EVT_FAILED
)
726 if (!(*str
== 0 || *str
== ',' || isspace(*str
)))
729 if (ret
!= EVT_HANDLED_ALL
) {
730 attrs
[nr_counters
] = attr
;
738 while (isspace(*str
))
745 int parse_filter(const struct option
*opt __used
, const char *str
,
748 int i
= nr_counters
- 1;
749 int len
= strlen(str
);
751 if (i
< 0 || attrs
[i
].type
!= PERF_TYPE_TRACEPOINT
) {
753 "-F option should follow a -e tracepoint option\n");
757 filters
[i
] = malloc(len
+ 1);
759 fprintf(stderr
, "not enough memory to hold filter string\n");
762 strcpy(filters
[i
], str
);
767 static const char * const event_type_descriptors
[] = {
772 "Hardware cache event",
776 * Print the events from <debugfs_mount_point>/tracing/events
779 static void print_tracepoint_events(void)
781 DIR *sys_dir
, *evt_dir
;
782 struct dirent
*sys_next
, *evt_next
, sys_dirent
, evt_dirent
;
783 char evt_path
[MAXPATHLEN
];
784 char dir_path
[MAXPATHLEN
];
786 if (debugfs_valid_mountpoint(debugfs_path
))
789 sys_dir
= opendir(debugfs_path
);
793 for_each_subsystem(sys_dir
, sys_dirent
, sys_next
) {
795 snprintf(dir_path
, MAXPATHLEN
, "%s/%s", debugfs_path
,
797 evt_dir
= opendir(dir_path
);
801 for_each_event(sys_dirent
, evt_dir
, evt_dirent
, evt_next
) {
802 snprintf(evt_path
, MAXPATHLEN
, "%s:%s",
803 sys_dirent
.d_name
, evt_dirent
.d_name
);
804 printf(" %-42s [%s]\n", evt_path
,
805 event_type_descriptors
[PERF_TYPE_TRACEPOINT
+1]);
813 * Print the help text for the event symbols:
815 void print_events(void)
817 struct event_symbol
*syms
= event_symbols
;
818 unsigned int i
, type
, op
, prev_type
= -1;
822 printf("List of pre-defined events (to be used in -e):\n");
824 for (i
= 0; i
< ARRAY_SIZE(event_symbols
); i
++, syms
++) {
825 type
= syms
->type
+ 1;
826 if (type
>= ARRAY_SIZE(event_type_descriptors
))
829 if (type
!= prev_type
)
832 if (strlen(syms
->alias
))
833 sprintf(name
, "%s OR %s", syms
->symbol
, syms
->alias
);
835 strcpy(name
, syms
->symbol
);
836 printf(" %-42s [%s]\n", name
,
837 event_type_descriptors
[type
]);
843 for (type
= 0; type
< PERF_COUNT_HW_CACHE_MAX
; type
++) {
844 for (op
= 0; op
< PERF_COUNT_HW_CACHE_OP_MAX
; op
++) {
845 /* skip invalid cache type */
846 if (!is_cache_op_valid(type
, op
))
849 for (i
= 0; i
< PERF_COUNT_HW_CACHE_RESULT_MAX
; i
++) {
850 printf(" %-42s [%s]\n",
851 event_cache_name(type
, op
, i
),
852 event_type_descriptors
[4]);
858 printf(" %-42s [raw hardware event descriptor]\n",
862 print_tracepoint_events();