4 #include "parse-options.h"
5 #include "parse-events.h"
13 struct perf_event_attr attrs[MAX_COUNTERS];
28 char debugfs_path[MAXPATHLEN];
30 #define CHW(x) .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_##x
31 #define CSW(x) .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_##x
33 static struct event_symbol event_symbols[] = {
34 { CHW(CPU_CYCLES), "cpu-cycles", "cycles" },
35 { CHW(INSTRUCTIONS), "instructions", "" },
36 { CHW(CACHE_REFERENCES), "cache-references", "" },
37 { CHW(CACHE_MISSES), "cache-misses", "" },
38 { CHW(BRANCH_INSTRUCTIONS), "branch-instructions", "branches" },
39 { CHW(BRANCH_MISSES), "branch-misses", "" },
40 { CHW(BUS_CYCLES), "bus-cycles", "" },
42 { CSW(CPU_CLOCK), "cpu-clock", "" },
43 { CSW(TASK_CLOCK), "task-clock", "" },
44 { CSW(PAGE_FAULTS), "page-faults", "faults" },
45 { CSW(PAGE_FAULTS_MIN), "minor-faults", "" },
46 { CSW(PAGE_FAULTS_MAJ), "major-faults", "" },
47 { CSW(CONTEXT_SWITCHES), "context-switches", "cs" },
48 { CSW(CPU_MIGRATIONS), "cpu-migrations", "migrations" },
49 { CSW(ALIGNMENT_FAULTS), "alignment-faults", "" },
50 { CSW(EMULATION_FAULTS), "emulation-faults", "" },
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[] = {
85 static const char *hw_cache[][MAX_ALIASES] = {
86 { "L1-dcache", "l1-d", "l1d", "L1-data", },
87 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
89 { "dTLB", "d-tlb", "Data-TLB", },
90 { "iTLB", "i-tlb", "Instruction-TLB", },
91 { "branch", "branches", "bpu", "btb", "bpc", },
94 static const char *hw_cache_op[][MAX_ALIASES] = {
95 { "load", "loads", "read", },
96 { "store", "stores", "write", },
97 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
100 static const char *hw_cache_result[][MAX_ALIASES] = {
101 { "refs", "Reference", "ops", "access", },
102 { "misses", "miss", },
105 #define C(x) PERF_COUNT_HW_CACHE_##x
106 #define CACHE_READ (1 << C(OP_READ))
107 #define CACHE_WRITE (1 << C(OP_WRITE))
108 #define CACHE_PREFETCH (1 << C(OP_PREFETCH))
109 #define COP(x) (1 << x)
112 * cache operartion stat
113 * L1I : Read and prefetch only
114 * ITLB and BPU : Read-only
116 static unsigned long hw_cache_stat[C(MAX)] = {
117 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
118 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
119 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
120 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
121 [C(ITLB)] = (CACHE_READ),
122 [C(BPU)] = (CACHE_READ),
125 #define for_each_subsystem(sys_dir, sys_dirent, sys_next) \
126 while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \
127 if (sys_dirent.d_type == DT_DIR && \
128 (strcmp(sys_dirent.d_name, ".")) && \
129 (strcmp(sys_dirent.d_name, "..")))
131 static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
133 char evt_path[MAXPATHLEN];
136 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", debugfs_path,
137 sys_dir->d_name, evt_dir->d_name);
138 fd = open(evt_path, O_RDONLY);
146 #define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) \
147 while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \
148 if (evt_dirent.d_type == DT_DIR && \
149 (strcmp(evt_dirent.d_name, ".")) && \
150 (strcmp(evt_dirent.d_name, "..")) && \
151 (!tp_event_has_id(&sys_dirent, &evt_dirent)))
153 #define MAX_EVENT_LENGTH 512
155 int valid_debugfs_mount(const char *debugfs)
159 if (statfs(debugfs, &st_fs) < 0)
161 else if (st_fs.f_type != (long) DEBUGFS_MAGIC)
166 struct tracepoint_path *tracepoint_id_to_path(u64 config)
168 struct tracepoint_path *path = NULL;
169 DIR *sys_dir, *evt_dir;
170 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
174 char evt_path[MAXPATHLEN];
175 char dir_path[MAXPATHLEN];
177 if (valid_debugfs_mount(debugfs_path))
180 sys_dir = opendir(debugfs_path);
184 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
186 snprintf(dir_path, MAXPATHLEN, "%s/%s", debugfs_path,
188 evt_dir = opendir(dir_path);
192 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
194 snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
196 fd = open(evt_path, O_RDONLY);
199 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
208 path = calloc(1, sizeof(path));
209 path->system = malloc(MAX_EVENT_LENGTH);
214 path->name = malloc(MAX_EVENT_LENGTH);
220 strncpy(path->system, sys_dirent.d_name,
222 strncpy(path->name, evt_dirent.d_name,
234 #define TP_PATH_LEN (MAX_EVENT_LENGTH * 2 + 1)
235 static const char *tracepoint_id_to_name(u64 config)
237 static char buf[TP_PATH_LEN];
238 struct tracepoint_path *path;
240 path = tracepoint_id_to_path(config);
242 snprintf(buf, TP_PATH_LEN, "%s:%s", path->system, path->name);
247 snprintf(buf, TP_PATH_LEN, "%s:%s", "unknown", "unknown");
252 static int is_cache_op_valid(u8 cache_type, u8 cache_op)
254 if (hw_cache_stat[cache_type] & COP(cache_op))
255 return 1; /* valid */
257 return 0; /* invalid */
260 static char *event_cache_name(u8 cache_type, u8 cache_op, u8 cache_result)
262 static char name[50];
265 sprintf(name, "%s-%s-%s", hw_cache[cache_type][0],
266 hw_cache_op[cache_op][0],
267 hw_cache_result[cache_result][0]);
269 sprintf(name, "%s-%s", hw_cache[cache_type][0],
270 hw_cache_op[cache_op][1]);
276 const char *event_name(int counter)
278 u64 config = attrs[counter].config;
279 int type = attrs[counter].type;
281 return __event_name(type, config);
284 const char *__event_name(int type, u64 config)
288 if (type == PERF_TYPE_RAW) {
289 sprintf(buf, "raw 0x%llx", config);
294 case PERF_TYPE_HARDWARE:
295 if (config < PERF_COUNT_HW_MAX)
296 return hw_event_names[config];
297 return "unknown-hardware";
299 case PERF_TYPE_HW_CACHE: {
300 u8 cache_type, cache_op, cache_result;
302 cache_type = (config >> 0) & 0xff;
303 if (cache_type > PERF_COUNT_HW_CACHE_MAX)
304 return "unknown-ext-hardware-cache-type";
306 cache_op = (config >> 8) & 0xff;
307 if (cache_op > PERF_COUNT_HW_CACHE_OP_MAX)
308 return "unknown-ext-hardware-cache-op";
310 cache_result = (config >> 16) & 0xff;
311 if (cache_result > PERF_COUNT_HW_CACHE_RESULT_MAX)
312 return "unknown-ext-hardware-cache-result";
314 if (!is_cache_op_valid(cache_type, cache_op))
315 return "invalid-cache";
317 return event_cache_name(cache_type, cache_op, cache_result);
320 case PERF_TYPE_SOFTWARE:
321 if (config < PERF_COUNT_SW_MAX)
322 return sw_event_names[config];
323 return "unknown-software";
325 case PERF_TYPE_TRACEPOINT:
326 return tracepoint_id_to_name(config);
335 static int parse_aliases(const char **str, const char *names[][MAX_ALIASES], int size)
340 for (i = 0; i < size; i++) {
341 for (j = 0; j < MAX_ALIASES && names[i][j]; j++) {
342 n = strlen(names[i][j]);
343 if (n > longest && !strncasecmp(*str, names[i][j], n))
355 static enum event_result
356 parse_generic_hw_event(const char **str, struct perf_event_attr *attr)
358 const char *s = *str;
359 int cache_type = -1, cache_op = -1, cache_result = -1;
361 cache_type = parse_aliases(&s, hw_cache, PERF_COUNT_HW_CACHE_MAX);
363 * No fallback - if we cannot get a clear cache type
366 if (cache_type == -1)
369 while ((cache_op == -1 || cache_result == -1) && *s == '-') {
372 if (cache_op == -1) {
373 cache_op = parse_aliases(&s, hw_cache_op,
374 PERF_COUNT_HW_CACHE_OP_MAX);
376 if (!is_cache_op_valid(cache_type, cache_op))
382 if (cache_result == -1) {
383 cache_result = parse_aliases(&s, hw_cache_result,
384 PERF_COUNT_HW_CACHE_RESULT_MAX);
385 if (cache_result >= 0)
390 * Can't parse this as a cache op or result, so back up
398 * Fall back to reads:
401 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
404 * Fall back to accesses:
406 if (cache_result == -1)
407 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
409 attr->config = cache_type | (cache_op << 8) | (cache_result << 16);
410 attr->type = PERF_TYPE_HW_CACHE;
416 static enum event_result
417 parse_single_tracepoint_event(char *sys_name,
418 const char *evt_name,
419 unsigned int evt_length,
421 struct perf_event_attr *attr,
424 char evt_path[MAXPATHLEN];
430 if (!strncmp(flags, "record", strlen(flags))) {
431 attr->sample_type |= PERF_SAMPLE_RAW;
432 attr->sample_type |= PERF_SAMPLE_TIME;
433 attr->sample_type |= PERF_SAMPLE_CPU;
437 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", debugfs_path,
440 fd = open(evt_path, O_RDONLY);
444 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
452 attr->type = PERF_TYPE_TRACEPOINT;
453 *strp = evt_name + evt_length;
458 /* sys + ':' + event + ':' + flags*/
459 #define MAX_EVOPT_LEN (MAX_EVENT_LENGTH * 2 + 2 + 128)
460 static enum event_result
461 parse_subsystem_tracepoint_event(char *sys_name, char *flags)
463 char evt_path[MAXPATHLEN];
464 struct dirent *evt_ent;
467 snprintf(evt_path, MAXPATHLEN, "%s/%s", debugfs_path, sys_name);
468 evt_dir = opendir(evt_path);
471 perror("Can't open event dir");
475 while ((evt_ent = readdir(evt_dir))) {
476 char event_opt[MAX_EVOPT_LEN + 1];
478 unsigned int rem = MAX_EVOPT_LEN;
480 if (!strcmp(evt_ent->d_name, ".")
481 || !strcmp(evt_ent->d_name, "..")
482 || !strcmp(evt_ent->d_name, "enable")
483 || !strcmp(evt_ent->d_name, "filter"))
486 len = snprintf(event_opt, MAX_EVOPT_LEN, "%s:%s", sys_name,
493 if (rem < strlen(flags) + 1)
496 strcat(event_opt, ":");
497 strcat(event_opt, flags);
500 if (parse_events(NULL, event_opt, 0))
504 return EVT_HANDLED_ALL;
508 static enum event_result parse_tracepoint_event(const char **strp,
509 struct perf_event_attr *attr)
511 const char *evt_name;
513 char sys_name[MAX_EVENT_LENGTH];
514 unsigned int sys_length, evt_length;
516 if (valid_debugfs_mount(debugfs_path))
519 evt_name = strchr(*strp, ':');
523 sys_length = evt_name - *strp;
524 if (sys_length >= MAX_EVENT_LENGTH)
527 strncpy(sys_name, *strp, sys_length);
528 sys_name[sys_length] = '\0';
529 evt_name = evt_name + 1;
531 flags = strchr(evt_name, ':');
534 evt_name = strndup(evt_name, flags - evt_name);
538 evt_length = strlen(evt_name);
539 if (evt_length >= MAX_EVENT_LENGTH)
542 if (!strcmp(evt_name, "*")) {
543 *strp = evt_name + evt_length;
544 return parse_subsystem_tracepoint_event(sys_name, flags);
546 return parse_single_tracepoint_event(sys_name, evt_name,
551 static int check_events(const char *str, unsigned int i)
555 n = strlen(event_symbols[i].symbol);
556 if (!strncmp(str, event_symbols[i].symbol, n))
559 n = strlen(event_symbols[i].alias);
561 if (!strncmp(str, event_symbols[i].alias, n))
566 static enum event_result
567 parse_symbolic_event(const char **strp, struct perf_event_attr *attr)
569 const char *str = *strp;
573 for (i = 0; i < ARRAY_SIZE(event_symbols); i++) {
574 n = check_events(str, i);
576 attr->type = event_symbols[i].type;
577 attr->config = event_symbols[i].config;
585 static enum event_result
586 parse_raw_event(const char **strp, struct perf_event_attr *attr)
588 const char *str = *strp;
594 n = hex2u64(str + 1, &config);
597 attr->type = PERF_TYPE_RAW;
598 attr->config = config;
604 static enum event_result
605 parse_numeric_event(const char **strp, struct perf_event_attr *attr)
607 const char *str = *strp;
612 type = strtoul(str, &endp, 0);
613 if (endp > str && type < PERF_TYPE_MAX && *endp == ':') {
615 config = strtoul(str, &endp, 0);
618 attr->config = config;
626 static enum event_result
627 parse_event_modifier(const char **strp, struct perf_event_attr *attr)
629 const char *str = *strp;
630 int eu = 1, ek = 1, eh = 1;
637 else if (*str == 'k')
639 else if (*str == 'h')
645 if (str >= *strp + 2) {
647 attr->exclude_user = eu;
648 attr->exclude_kernel = ek;
649 attr->exclude_hv = eh;
656 * Each event can have multiple symbolic names.
657 * Symbolic names are (almost) exactly matched.
659 static enum event_result
660 parse_event_symbols(const char **str, struct perf_event_attr *attr)
662 enum event_result ret;
664 ret = parse_tracepoint_event(str, attr);
665 if (ret != EVT_FAILED)
668 ret = parse_raw_event(str, attr);
669 if (ret != EVT_FAILED)
672 ret = parse_numeric_event(str, attr);
673 if (ret != EVT_FAILED)
676 ret = parse_symbolic_event(str, attr);
677 if (ret != EVT_FAILED)
680 ret = parse_generic_hw_event(str, attr);
681 if (ret != EVT_FAILED)
687 parse_event_modifier(str, attr);
692 static void store_event_type(const char *orgname)
694 char filename[PATH_MAX], *c;
698 sprintf(filename, "%s/", debugfs_path);
699 strncat(filename, orgname, strlen(orgname));
700 strcat(filename, "/id");
702 c = strchr(filename, ':');
706 file = fopen(filename, "r");
709 if (fscanf(file, "%i", &id) < 1)
710 die("cannot store event ID");
712 perf_header__push_event(id, orgname);
716 int parse_events(const struct option *opt __used, const char *str, int unset __used)
718 struct perf_event_attr attr;
719 enum event_result ret;
721 if (strchr(str, ':'))
722 store_event_type(str);
725 if (nr_counters == MAX_COUNTERS)
728 memset(&attr, 0, sizeof(attr));
729 ret = parse_event_symbols(&str, &attr);
730 if (ret == EVT_FAILED)
733 if (!(*str == 0 || *str == ',' || isspace(*str)))
736 if (ret != EVT_HANDLED_ALL) {
737 attrs[nr_counters] = attr;
745 while (isspace(*str))
752 static const char * const event_type_descriptors[] = {
757 "Hardware cache event",
761 * Print the events from <debugfs_mount_point>/tracing/events
764 static void print_tracepoint_events(void)
766 DIR *sys_dir, *evt_dir;
767 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
768 char evt_path[MAXPATHLEN];
769 char dir_path[MAXPATHLEN];
771 if (valid_debugfs_mount(debugfs_path))
774 sys_dir = opendir(debugfs_path);
778 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
780 snprintf(dir_path, MAXPATHLEN, "%s/%s", debugfs_path,
782 evt_dir = opendir(dir_path);
786 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
787 snprintf(evt_path, MAXPATHLEN, "%s:%s",
788 sys_dirent.d_name, evt_dirent.d_name);
789 fprintf(stderr, " %-42s [%s]\n", evt_path,
790 event_type_descriptors[PERF_TYPE_TRACEPOINT+1]);
798 * Print the help text for the event symbols:
800 void print_events(void)
802 struct event_symbol *syms = event_symbols;
803 unsigned int i, type, op, prev_type = -1;
806 fprintf(stderr, "\n");
807 fprintf(stderr, "List of pre-defined events (to be used in -e):\n");
809 for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
810 type = syms->type + 1;
811 if (type >= ARRAY_SIZE(event_type_descriptors))
814 if (type != prev_type)
815 fprintf(stderr, "\n");
817 if (strlen(syms->alias))
818 sprintf(name, "%s OR %s", syms->symbol, syms->alias);
820 strcpy(name, syms->symbol);
821 fprintf(stderr, " %-42s [%s]\n", name,
822 event_type_descriptors[type]);
827 fprintf(stderr, "\n");
828 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
829 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
830 /* skip invalid cache type */
831 if (!is_cache_op_valid(type, op))
834 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
835 fprintf(stderr, " %-42s [%s]\n",
836 event_cache_name(type, op, i),
837 event_type_descriptors[4]);
842 fprintf(stderr, "\n");
843 fprintf(stderr, " %-42s [raw hardware event descriptor]\n",
845 fprintf(stderr, "\n");
847 print_tracepoint_events();