4 #include "parse-options.h"
5 #include "parse-events.h"
12 struct perf_counter_attr attrs
[MAX_COUNTERS
];
21 char debugfs_path
[MAXPATHLEN
];
23 #define CHW(x) .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_##x
24 #define CSW(x) .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_##x
26 static struct event_symbol event_symbols
[] = {
27 { CHW(CPU_CYCLES
), "cpu-cycles", "cycles" },
28 { CHW(INSTRUCTIONS
), "instructions", "" },
29 { CHW(CACHE_REFERENCES
), "cache-references", "" },
30 { CHW(CACHE_MISSES
), "cache-misses", "" },
31 { CHW(BRANCH_INSTRUCTIONS
), "branch-instructions", "branches" },
32 { CHW(BRANCH_MISSES
), "branch-misses", "" },
33 { CHW(BUS_CYCLES
), "bus-cycles", "" },
35 { CSW(CPU_CLOCK
), "cpu-clock", "" },
36 { CSW(TASK_CLOCK
), "task-clock", "" },
37 { CSW(PAGE_FAULTS
), "page-faults", "faults" },
38 { CSW(PAGE_FAULTS_MIN
), "minor-faults", "" },
39 { CSW(PAGE_FAULTS_MAJ
), "major-faults", "" },
40 { CSW(CONTEXT_SWITCHES
), "context-switches", "cs" },
41 { CSW(CPU_MIGRATIONS
), "cpu-migrations", "migrations" },
44 #define __PERF_COUNTER_FIELD(config, name) \
45 ((config & PERF_COUNTER_##name##_MASK) >> PERF_COUNTER_##name##_SHIFT)
47 #define PERF_COUNTER_RAW(config) __PERF_COUNTER_FIELD(config, RAW)
48 #define PERF_COUNTER_CONFIG(config) __PERF_COUNTER_FIELD(config, CONFIG)
49 #define PERF_COUNTER_TYPE(config) __PERF_COUNTER_FIELD(config, TYPE)
50 #define PERF_COUNTER_ID(config) __PERF_COUNTER_FIELD(config, EVENT)
52 static const char *hw_event_names
[] = {
62 static const char *sw_event_names
[] = {
74 static const char *hw_cache
[][MAX_ALIASES
] = {
75 { "L1-dcache", "l1-d", "l1d", "L1-data", },
76 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
78 { "dTLB", "d-tlb", "Data-TLB", },
79 { "iTLB", "i-tlb", "Instruction-TLB", },
80 { "branch", "branches", "bpu", "btb", "bpc", },
83 static const char *hw_cache_op
[][MAX_ALIASES
] = {
84 { "load", "loads", "read", },
85 { "store", "stores", "write", },
86 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
89 static const char *hw_cache_result
[][MAX_ALIASES
] = {
90 { "refs", "Reference", "ops", "access", },
91 { "misses", "miss", },
94 #define C(x) PERF_COUNT_HW_CACHE_##x
95 #define CACHE_READ (1 << C(OP_READ))
96 #define CACHE_WRITE (1 << C(OP_WRITE))
97 #define CACHE_PREFETCH (1 << C(OP_PREFETCH))
98 #define COP(x) (1 << x)
101 * cache operartion stat
102 * L1I : Read and prefetch only
103 * ITLB and BPU : Read-only
105 static unsigned long hw_cache_stat
[C(MAX
)] = {
106 [C(L1D
)] = (CACHE_READ
| CACHE_WRITE
| CACHE_PREFETCH
),
107 [C(L1I
)] = (CACHE_READ
| CACHE_PREFETCH
),
108 [C(LL
)] = (CACHE_READ
| CACHE_WRITE
| CACHE_PREFETCH
),
109 [C(DTLB
)] = (CACHE_READ
| CACHE_WRITE
| CACHE_PREFETCH
),
110 [C(ITLB
)] = (CACHE_READ
),
111 [C(BPU
)] = (CACHE_READ
),
114 #define for_each_subsystem(sys_dir, sys_dirent, sys_next) \
115 while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \
116 if (sys_dirent.d_type == DT_DIR && \
117 (strcmp(sys_dirent.d_name, ".")) && \
118 (strcmp(sys_dirent.d_name, "..")))
120 static int tp_event_has_id(struct dirent
*sys_dir
, struct dirent
*evt_dir
)
122 char evt_path
[MAXPATHLEN
];
125 snprintf(evt_path
, MAXPATHLEN
, "%s/%s/%s/id", debugfs_path
,
126 sys_dir
->d_name
, evt_dir
->d_name
);
127 fd
= open(evt_path
, O_RDONLY
);
135 #define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) \
136 while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \
137 if (evt_dirent.d_type == DT_DIR && \
138 (strcmp(evt_dirent.d_name, ".")) && \
139 (strcmp(evt_dirent.d_name, "..")) && \
140 (!tp_event_has_id(&sys_dirent, &evt_dirent)))
142 #define MAX_EVENT_LENGTH 30
144 int valid_debugfs_mount(const char *debugfs
)
148 if (statfs(debugfs
, &st_fs
) < 0)
150 else if (st_fs
.f_type
!= (long) DEBUGFS_MAGIC
)
155 struct tracepoint_path
*tracepoint_id_to_path(u64 config
)
157 struct tracepoint_path
*path
= NULL
;
158 DIR *sys_dir
, *evt_dir
;
159 struct dirent
*sys_next
, *evt_next
, sys_dirent
, evt_dirent
;
163 char evt_path
[MAXPATHLEN
];
165 if (valid_debugfs_mount(debugfs_path
))
168 sys_dir
= opendir(debugfs_path
);
171 sys_dir_fd
= dirfd(sys_dir
);
173 for_each_subsystem(sys_dir
, sys_dirent
, sys_next
) {
174 int dfd
= openat(sys_dir_fd
, sys_dirent
.d_name
,
175 O_RDONLY
|O_DIRECTORY
), evt_dir_fd
;
178 evt_dir
= fdopendir(dfd
);
183 evt_dir_fd
= dirfd(evt_dir
);
184 for_each_event(sys_dirent
, evt_dir
, evt_dirent
, evt_next
) {
185 snprintf(evt_path
, MAXPATHLEN
, "%s/id",
187 fd
= openat(evt_dir_fd
, evt_path
, O_RDONLY
);
190 if (read(fd
, id_buf
, sizeof(id_buf
)) < 0) {
199 path
= calloc(1, sizeof(path
));
200 path
->system
= malloc(MAX_EVENT_LENGTH
);
205 path
->name
= malloc(MAX_EVENT_LENGTH
);
211 strncpy(path
->system
, sys_dirent
.d_name
,
213 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
))
348 parse_generic_hw_event(const char **str
, struct perf_counter_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 int parse_tracepoint_event(const char **strp
,
409 struct perf_counter_attr
*attr
)
411 const char *evt_name
;
413 char sys_name
[MAX_EVENT_LENGTH
];
416 unsigned int sys_length
, evt_length
;
418 char evt_path
[MAXPATHLEN
];
420 if (valid_debugfs_mount(debugfs_path
))
423 evt_name
= strchr(*strp
, ':');
427 sys_length
= evt_name
- *strp
;
428 if (sys_length
>= MAX_EVENT_LENGTH
)
431 strncpy(sys_name
, *strp
, sys_length
);
432 sys_name
[sys_length
] = '\0';
433 evt_name
= evt_name
+ 1;
435 flags
= strchr(evt_name
, ':');
439 if (!strncmp(flags
, "record", strlen(flags
)))
440 attr
->sample_type
|= PERF_SAMPLE_RAW
;
443 evt_length
= strlen(evt_name
);
444 if (evt_length
>= MAX_EVENT_LENGTH
)
447 snprintf(evt_path
, MAXPATHLEN
, "%s/%s/%s/id", debugfs_path
,
449 fd
= open(evt_path
, O_RDONLY
);
453 if (read(fd
, id_buf
, sizeof(id_buf
)) < 0) {
460 attr
->type
= PERF_TYPE_TRACEPOINT
;
461 *strp
= evt_name
+ evt_length
;
465 static int check_events(const char *str
, unsigned int i
)
469 n
= strlen(event_symbols
[i
].symbol
);
470 if (!strncmp(str
, event_symbols
[i
].symbol
, n
))
473 n
= strlen(event_symbols
[i
].alias
);
475 if (!strncmp(str
, event_symbols
[i
].alias
, n
))
481 parse_symbolic_event(const char **strp
, struct perf_counter_attr
*attr
)
483 const char *str
= *strp
;
487 for (i
= 0; i
< ARRAY_SIZE(event_symbols
); i
++) {
488 n
= check_events(str
, i
);
490 attr
->type
= event_symbols
[i
].type
;
491 attr
->config
= event_symbols
[i
].config
;
499 static int parse_raw_event(const char **strp
, struct perf_counter_attr
*attr
)
501 const char *str
= *strp
;
507 n
= hex2u64(str
+ 1, &config
);
510 attr
->type
= PERF_TYPE_RAW
;
511 attr
->config
= config
;
518 parse_numeric_event(const char **strp
, struct perf_counter_attr
*attr
)
520 const char *str
= *strp
;
525 type
= strtoul(str
, &endp
, 0);
526 if (endp
> str
&& type
< PERF_TYPE_MAX
&& *endp
== ':') {
528 config
= strtoul(str
, &endp
, 0);
531 attr
->config
= config
;
540 parse_event_modifier(const char **strp
, struct perf_counter_attr
*attr
)
542 const char *str
= *strp
;
543 int eu
= 1, ek
= 1, eh
= 1;
550 else if (*str
== 'k')
552 else if (*str
== 'h')
558 if (str
>= *strp
+ 2) {
560 attr
->exclude_user
= eu
;
561 attr
->exclude_kernel
= ek
;
562 attr
->exclude_hv
= eh
;
569 * Each event can have multiple symbolic names.
570 * Symbolic names are (almost) exactly matched.
572 static int parse_event_symbols(const char **str
, struct perf_counter_attr
*attr
)
574 if (!(parse_tracepoint_event(str
, attr
) ||
575 parse_raw_event(str
, attr
) ||
576 parse_numeric_event(str
, attr
) ||
577 parse_symbolic_event(str
, attr
) ||
578 parse_generic_hw_event(str
, attr
)))
581 parse_event_modifier(str
, attr
);
586 int parse_events(const struct option
*opt __used
, const char *str
, int unset __used
)
588 struct perf_counter_attr attr
;
591 if (nr_counters
== MAX_COUNTERS
)
594 memset(&attr
, 0, sizeof(attr
));
595 if (!parse_event_symbols(&str
, &attr
))
598 if (!(*str
== 0 || *str
== ',' || isspace(*str
)))
601 attrs
[nr_counters
] = attr
;
608 while (isspace(*str
))
615 static const char * const event_type_descriptors
[] = {
620 "Hardware cache event",
624 * Print the events from <debugfs_mount_point>/tracing/events
627 static void print_tracepoint_events(void)
629 DIR *sys_dir
, *evt_dir
;
630 struct dirent
*sys_next
, *evt_next
, sys_dirent
, evt_dirent
;
632 char evt_path
[MAXPATHLEN
];
634 if (valid_debugfs_mount(debugfs_path
))
637 sys_dir
= opendir(debugfs_path
);
640 sys_dir_fd
= dirfd(sys_dir
);
642 for_each_subsystem(sys_dir
, sys_dirent
, sys_next
) {
643 int dfd
= openat(sys_dir_fd
, sys_dirent
.d_name
,
644 O_RDONLY
|O_DIRECTORY
), evt_dir_fd
;
647 evt_dir
= fdopendir(dfd
);
652 evt_dir_fd
= dirfd(evt_dir
);
653 for_each_event(sys_dirent
, evt_dir
, evt_dirent
, evt_next
) {
654 snprintf(evt_path
, MAXPATHLEN
, "%s:%s",
655 sys_dirent
.d_name
, evt_dirent
.d_name
);
656 fprintf(stderr
, " %-42s [%s]\n", evt_path
,
657 event_type_descriptors
[PERF_TYPE_TRACEPOINT
+1]);
667 * Print the help text for the event symbols:
669 void print_events(void)
671 struct event_symbol
*syms
= event_symbols
;
672 unsigned int i
, type
, op
, prev_type
= -1;
675 fprintf(stderr
, "\n");
676 fprintf(stderr
, "List of pre-defined events (to be used in -e):\n");
678 for (i
= 0; i
< ARRAY_SIZE(event_symbols
); i
++, syms
++) {
679 type
= syms
->type
+ 1;
680 if (type
>= ARRAY_SIZE(event_type_descriptors
))
683 if (type
!= prev_type
)
684 fprintf(stderr
, "\n");
686 if (strlen(syms
->alias
))
687 sprintf(name
, "%s OR %s", syms
->symbol
, syms
->alias
);
689 strcpy(name
, syms
->symbol
);
690 fprintf(stderr
, " %-42s [%s]\n", name
,
691 event_type_descriptors
[type
]);
696 fprintf(stderr
, "\n");
697 for (type
= 0; type
< PERF_COUNT_HW_CACHE_MAX
; type
++) {
698 for (op
= 0; op
< PERF_COUNT_HW_CACHE_OP_MAX
; op
++) {
699 /* skip invalid cache type */
700 if (!is_cache_op_valid(type
, op
))
703 for (i
= 0; i
< PERF_COUNT_HW_CACHE_RESULT_MAX
; i
++) {
704 fprintf(stderr
, " %-42s [%s]\n",
705 event_cache_name(type
, op
, i
),
706 event_type_descriptors
[4]);
711 fprintf(stderr
, "\n");
712 fprintf(stderr
, " %-42s [raw hardware event descriptor]\n",
714 fprintf(stderr
, "\n");
716 print_tracepoint_events();