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" },
53 #define __PERF_EVENT_FIELD(config, name) \
54 ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
56 #define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
57 #define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
58 #define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
59 #define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
61 static const char *hw_event_names[] = {
71 static const char *sw_event_names[] = {
83 static const char *hw_cache[][MAX_ALIASES] = {
84 { "L1-dcache", "l1-d", "l1d", "L1-data", },
85 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
87 { "dTLB", "d-tlb", "Data-TLB", },
88 { "iTLB", "i-tlb", "Instruction-TLB", },
89 { "branch", "branches", "bpu", "btb", "bpc", },
92 static const char *hw_cache_op[][MAX_ALIASES] = {
93 { "load", "loads", "read", },
94 { "store", "stores", "write", },
95 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
98 static const char *hw_cache_result[][MAX_ALIASES] = {
99 { "refs", "Reference", "ops", "access", },
100 { "misses", "miss", },
103 #define C(x) PERF_COUNT_HW_CACHE_##x
104 #define CACHE_READ (1 << C(OP_READ))
105 #define CACHE_WRITE (1 << C(OP_WRITE))
106 #define CACHE_PREFETCH (1 << C(OP_PREFETCH))
107 #define COP(x) (1 << x)
110 * cache operartion stat
111 * L1I : Read and prefetch only
112 * ITLB and BPU : Read-only
114 static unsigned long hw_cache_stat[C(MAX)] = {
115 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
116 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
117 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
118 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
119 [C(ITLB)] = (CACHE_READ),
120 [C(BPU)] = (CACHE_READ),
123 #define for_each_subsystem(sys_dir, sys_dirent, sys_next) \
124 while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \
125 if (sys_dirent.d_type == DT_DIR && \
126 (strcmp(sys_dirent.d_name, ".")) && \
127 (strcmp(sys_dirent.d_name, "..")))
129 static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
131 char evt_path[MAXPATHLEN];
134 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", debugfs_path,
135 sys_dir->d_name, evt_dir->d_name);
136 fd = open(evt_path, O_RDONLY);
144 #define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) \
145 while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \
146 if (evt_dirent.d_type == DT_DIR && \
147 (strcmp(evt_dirent.d_name, ".")) && \
148 (strcmp(evt_dirent.d_name, "..")) && \
149 (!tp_event_has_id(&sys_dirent, &evt_dirent)))
151 #define MAX_EVENT_LENGTH 512
154 struct tracepoint_path *tracepoint_id_to_path(u64 config)
156 struct tracepoint_path *path = NULL;
157 DIR *sys_dir, *evt_dir;
158 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
162 char evt_path[MAXPATHLEN];
163 char dir_path[MAXPATHLEN];
165 if (debugfs_valid_mountpoint(debugfs_path))
168 sys_dir = opendir(debugfs_path);
172 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
174 snprintf(dir_path, MAXPATHLEN, "%s/%s", debugfs_path,
176 evt_dir = opendir(dir_path);
180 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
182 snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
184 fd = open(evt_path, O_RDONLY);
187 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
196 path = calloc(1, sizeof(path));
197 path->system = malloc(MAX_EVENT_LENGTH);
202 path->name = malloc(MAX_EVENT_LENGTH);
208 strncpy(path->system, sys_dirent.d_name,
210 strncpy(path->name, evt_dirent.d_name,
222 #define TP_PATH_LEN (MAX_EVENT_LENGTH * 2 + 1)
223 static const char *tracepoint_id_to_name(u64 config)
225 static char buf[TP_PATH_LEN];
226 struct tracepoint_path *path;
228 path = tracepoint_id_to_path(config);
230 snprintf(buf, TP_PATH_LEN, "%s:%s", path->system, path->name);
235 snprintf(buf, TP_PATH_LEN, "%s:%s", "unknown", "unknown");
240 static int is_cache_op_valid(u8 cache_type, u8 cache_op)
242 if (hw_cache_stat[cache_type] & COP(cache_op))
243 return 1; /* valid */
245 return 0; /* invalid */
248 static char *event_cache_name(u8 cache_type, u8 cache_op, u8 cache_result)
250 static char name[50];
253 sprintf(name, "%s-%s-%s", hw_cache[cache_type][0],
254 hw_cache_op[cache_op][0],
255 hw_cache_result[cache_result][0]);
257 sprintf(name, "%s-%s", hw_cache[cache_type][0],
258 hw_cache_op[cache_op][1]);
264 const char *event_name(int counter)
266 u64 config = attrs[counter].config;
267 int type = attrs[counter].type;
269 return __event_name(type, config);
272 const char *__event_name(int type, u64 config)
276 if (type == PERF_TYPE_RAW) {
277 sprintf(buf, "raw 0x%llx", config);
282 case PERF_TYPE_HARDWARE:
283 if (config < PERF_COUNT_HW_MAX)
284 return hw_event_names[config];
285 return "unknown-hardware";
287 case PERF_TYPE_HW_CACHE: {
288 u8 cache_type, cache_op, cache_result;
290 cache_type = (config >> 0) & 0xff;
291 if (cache_type > PERF_COUNT_HW_CACHE_MAX)
292 return "unknown-ext-hardware-cache-type";
294 cache_op = (config >> 8) & 0xff;
295 if (cache_op > PERF_COUNT_HW_CACHE_OP_MAX)
296 return "unknown-ext-hardware-cache-op";
298 cache_result = (config >> 16) & 0xff;
299 if (cache_result > PERF_COUNT_HW_CACHE_RESULT_MAX)
300 return "unknown-ext-hardware-cache-result";
302 if (!is_cache_op_valid(cache_type, cache_op))
303 return "invalid-cache";
305 return event_cache_name(cache_type, cache_op, cache_result);
308 case PERF_TYPE_SOFTWARE:
309 if (config < PERF_COUNT_SW_MAX)
310 return sw_event_names[config];
311 return "unknown-software";
313 case PERF_TYPE_TRACEPOINT:
314 return tracepoint_id_to_name(config);
323 static int parse_aliases(const char **str, const char *names[][MAX_ALIASES], int size)
328 for (i = 0; i < size; i++) {
329 for (j = 0; j < MAX_ALIASES && names[i][j]; j++) {
330 n = strlen(names[i][j]);
331 if (n > longest && !strncasecmp(*str, names[i][j], n))
343 static enum event_result
344 parse_generic_hw_event(const char **str, struct perf_event_attr *attr)
346 const char *s = *str;
347 int cache_type = -1, cache_op = -1, cache_result = -1;
349 cache_type = parse_aliases(&s, hw_cache, PERF_COUNT_HW_CACHE_MAX);
351 * No fallback - if we cannot get a clear cache type
354 if (cache_type == -1)
357 while ((cache_op == -1 || cache_result == -1) && *s == '-') {
360 if (cache_op == -1) {
361 cache_op = parse_aliases(&s, hw_cache_op,
362 PERF_COUNT_HW_CACHE_OP_MAX);
364 if (!is_cache_op_valid(cache_type, cache_op))
370 if (cache_result == -1) {
371 cache_result = parse_aliases(&s, hw_cache_result,
372 PERF_COUNT_HW_CACHE_RESULT_MAX);
373 if (cache_result >= 0)
378 * Can't parse this as a cache op or result, so back up
386 * Fall back to reads:
389 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
392 * Fall back to accesses:
394 if (cache_result == -1)
395 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
397 attr->config = cache_type | (cache_op << 8) | (cache_result << 16);
398 attr->type = PERF_TYPE_HW_CACHE;
404 static enum event_result
405 parse_single_tracepoint_event(char *sys_name,
406 const char *evt_name,
407 unsigned int evt_length,
409 struct perf_event_attr *attr,
412 char evt_path[MAXPATHLEN];
418 if (!strncmp(flags, "record", strlen(flags))) {
419 attr->sample_type |= PERF_SAMPLE_RAW;
420 attr->sample_type |= PERF_SAMPLE_TIME;
421 attr->sample_type |= PERF_SAMPLE_CPU;
425 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", debugfs_path,
428 fd = open(evt_path, O_RDONLY);
432 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
440 attr->type = PERF_TYPE_TRACEPOINT;
441 *strp = evt_name + evt_length;
446 /* sys + ':' + event + ':' + flags*/
447 #define MAX_EVOPT_LEN (MAX_EVENT_LENGTH * 2 + 2 + 128)
448 static enum event_result
449 parse_subsystem_tracepoint_event(char *sys_name, char *flags)
451 char evt_path[MAXPATHLEN];
452 struct dirent *evt_ent;
455 snprintf(evt_path, MAXPATHLEN, "%s/%s", debugfs_path, sys_name);
456 evt_dir = opendir(evt_path);
459 perror("Can't open event dir");
463 while ((evt_ent = readdir(evt_dir))) {
464 char event_opt[MAX_EVOPT_LEN + 1];
466 unsigned int rem = MAX_EVOPT_LEN;
468 if (!strcmp(evt_ent->d_name, ".")
469 || !strcmp(evt_ent->d_name, "..")
470 || !strcmp(evt_ent->d_name, "enable")
471 || !strcmp(evt_ent->d_name, "filter"))
474 len = snprintf(event_opt, MAX_EVOPT_LEN, "%s:%s", sys_name,
481 if (rem < strlen(flags) + 1)
484 strcat(event_opt, ":");
485 strcat(event_opt, flags);
488 if (parse_events(NULL, event_opt, 0))
492 return EVT_HANDLED_ALL;
496 static enum event_result parse_tracepoint_event(const char **strp,
497 struct perf_event_attr *attr)
499 const char *evt_name;
501 char sys_name[MAX_EVENT_LENGTH];
502 unsigned int sys_length, evt_length;
504 if (debugfs_valid_mountpoint(debugfs_path))
507 evt_name = strchr(*strp, ':');
511 sys_length = evt_name - *strp;
512 if (sys_length >= MAX_EVENT_LENGTH)
515 strncpy(sys_name, *strp, sys_length);
516 sys_name[sys_length] = '\0';
517 evt_name = evt_name + 1;
519 flags = strchr(evt_name, ':');
522 evt_name = strndup(evt_name, flags - evt_name);
526 evt_length = strlen(evt_name);
527 if (evt_length >= MAX_EVENT_LENGTH)
530 if (!strcmp(evt_name, "*")) {
531 *strp = evt_name + evt_length;
532 return parse_subsystem_tracepoint_event(sys_name, flags);
534 return parse_single_tracepoint_event(sys_name, evt_name,
539 static int check_events(const char *str, unsigned int i)
543 n = strlen(event_symbols[i].symbol);
544 if (!strncmp(str, event_symbols[i].symbol, n))
547 n = strlen(event_symbols[i].alias);
549 if (!strncmp(str, event_symbols[i].alias, n))
554 static enum event_result
555 parse_symbolic_event(const char **strp, struct perf_event_attr *attr)
557 const char *str = *strp;
561 for (i = 0; i < ARRAY_SIZE(event_symbols); i++) {
562 n = check_events(str, i);
564 attr->type = event_symbols[i].type;
565 attr->config = event_symbols[i].config;
573 static enum event_result
574 parse_raw_event(const char **strp, struct perf_event_attr *attr)
576 const char *str = *strp;
582 n = hex2u64(str + 1, &config);
585 attr->type = PERF_TYPE_RAW;
586 attr->config = config;
592 static enum event_result
593 parse_numeric_event(const char **strp, struct perf_event_attr *attr)
595 const char *str = *strp;
600 type = strtoul(str, &endp, 0);
601 if (endp > str && type < PERF_TYPE_MAX && *endp == ':') {
603 config = strtoul(str, &endp, 0);
606 attr->config = config;
614 static enum event_result
615 parse_event_modifier(const char **strp, struct perf_event_attr *attr)
617 const char *str = *strp;
618 int eu = 1, ek = 1, eh = 1;
625 else if (*str == 'k')
627 else if (*str == 'h')
633 if (str >= *strp + 2) {
635 attr->exclude_user = eu;
636 attr->exclude_kernel = ek;
637 attr->exclude_hv = eh;
644 * Each event can have multiple symbolic names.
645 * Symbolic names are (almost) exactly matched.
647 static enum event_result
648 parse_event_symbols(const char **str, struct perf_event_attr *attr)
650 enum event_result ret;
652 ret = parse_tracepoint_event(str, attr);
653 if (ret != EVT_FAILED)
656 ret = parse_raw_event(str, attr);
657 if (ret != EVT_FAILED)
660 ret = parse_numeric_event(str, attr);
661 if (ret != EVT_FAILED)
664 ret = parse_symbolic_event(str, attr);
665 if (ret != EVT_FAILED)
668 ret = parse_generic_hw_event(str, attr);
669 if (ret != EVT_FAILED)
672 fprintf(stderr, "invalid or unsupported event: '%s'\n", *str);
673 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
677 parse_event_modifier(str, attr);
682 static void store_event_type(const char *orgname)
684 char filename[PATH_MAX], *c;
688 sprintf(filename, "%s/", debugfs_path);
689 strncat(filename, orgname, strlen(orgname));
690 strcat(filename, "/id");
692 c = strchr(filename, ':');
696 file = fopen(filename, "r");
699 if (fscanf(file, "%i", &id) < 1)
700 die("cannot store event ID");
702 perf_header__push_event(id, orgname);
705 int parse_events(const struct option *opt __used, const char *str, int unset __used)
707 struct perf_event_attr attr;
708 enum event_result ret;
710 if (strchr(str, ':'))
711 store_event_type(str);
714 if (nr_counters == MAX_COUNTERS)
717 memset(&attr, 0, sizeof(attr));
718 ret = parse_event_symbols(&str, &attr);
719 if (ret == EVT_FAILED)
722 if (!(*str == 0 || *str == ',' || isspace(*str)))
725 if (ret != EVT_HANDLED_ALL) {
726 attrs[nr_counters] = attr;
734 while (isspace(*str))
741 int parse_filter(const struct option *opt __used, const char *str,
744 int i = nr_counters - 1;
745 int len = strlen(str);
747 if (i < 0 || attrs[i].type != PERF_TYPE_TRACEPOINT) {
749 "-F option should follow a -e tracepoint option\n");
753 filters[i] = malloc(len + 1);
755 fprintf(stderr, "not enough memory to hold filter string\n");
758 strcpy(filters[i], str);
763 static const char * const event_type_descriptors[] = {
768 "Hardware cache event",
772 * Print the events from <debugfs_mount_point>/tracing/events
775 static void print_tracepoint_events(void)
777 DIR *sys_dir, *evt_dir;
778 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
779 char evt_path[MAXPATHLEN];
780 char dir_path[MAXPATHLEN];
782 if (debugfs_valid_mountpoint(debugfs_path))
785 sys_dir = opendir(debugfs_path);
789 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
791 snprintf(dir_path, MAXPATHLEN, "%s/%s", debugfs_path,
793 evt_dir = opendir(dir_path);
797 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
798 snprintf(evt_path, MAXPATHLEN, "%s:%s",
799 sys_dirent.d_name, evt_dirent.d_name);
800 printf(" %-42s [%s]\n", evt_path,
801 event_type_descriptors[PERF_TYPE_TRACEPOINT+1]);
809 * Print the help text for the event symbols:
811 void print_events(void)
813 struct event_symbol *syms = event_symbols;
814 unsigned int i, type, op, prev_type = -1;
818 printf("List of pre-defined events (to be used in -e):\n");
820 for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
821 type = syms->type + 1;
822 if (type >= ARRAY_SIZE(event_type_descriptors))
825 if (type != prev_type)
828 if (strlen(syms->alias))
829 sprintf(name, "%s OR %s", syms->symbol, syms->alias);
831 strcpy(name, syms->symbol);
832 printf(" %-42s [%s]\n", name,
833 event_type_descriptors[type]);
839 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
840 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
841 /* skip invalid cache type */
842 if (!is_cache_op_valid(type, op))
845 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
846 printf(" %-42s [%s]\n",
847 event_cache_name(type, op, i),
848 event_type_descriptors[4]);
854 printf(" %-42s [raw hardware event descriptor]\n",
858 print_tracepoint_events();