1 #include "../../../include/linux/hw_breakpoint.h"
6 #include "parse-options.h"
7 #include "parse-events.h"
14 #include "parse-events-flex.h"
17 #define MAX_NAME_LEN 100
27 extern int parse_events_debug;
29 int parse_events_parse(struct list_head *list, int *idx);
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(STALLED_CYCLES_FRONTEND), "stalled-cycles-frontend", "idle-cycles-frontend" },
37 { CHW(STALLED_CYCLES_BACKEND), "stalled-cycles-backend", "idle-cycles-backend" },
38 { CHW(INSTRUCTIONS), "instructions", "" },
39 { CHW(CACHE_REFERENCES), "cache-references", "" },
40 { CHW(CACHE_MISSES), "cache-misses", "" },
41 { CHW(BRANCH_INSTRUCTIONS), "branch-instructions", "branches" },
42 { CHW(BRANCH_MISSES), "branch-misses", "" },
43 { CHW(BUS_CYCLES), "bus-cycles", "" },
44 { CHW(REF_CPU_CYCLES), "ref-cycles", "" },
46 { CSW(CPU_CLOCK), "cpu-clock", "" },
47 { CSW(TASK_CLOCK), "task-clock", "" },
48 { CSW(PAGE_FAULTS), "page-faults", "faults" },
49 { CSW(PAGE_FAULTS_MIN), "minor-faults", "" },
50 { CSW(PAGE_FAULTS_MAJ), "major-faults", "" },
51 { CSW(CONTEXT_SWITCHES), "context-switches", "cs" },
52 { CSW(CPU_MIGRATIONS), "cpu-migrations", "migrations" },
53 { CSW(ALIGNMENT_FAULTS), "alignment-faults", "" },
54 { CSW(EMULATION_FAULTS), "emulation-faults", "" },
57 #define __PERF_EVENT_FIELD(config, name) \
58 ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
60 #define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
61 #define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
62 #define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
63 #define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
65 #define for_each_subsystem(sys_dir, sys_dirent, sys_next) \
66 while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \
67 if (sys_dirent.d_type == DT_DIR && \
68 (strcmp(sys_dirent.d_name, ".")) && \
69 (strcmp(sys_dirent.d_name, "..")))
71 static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
73 char evt_path[MAXPATHLEN];
76 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path,
77 sys_dir->d_name, evt_dir->d_name);
78 fd = open(evt_path, O_RDONLY);
86 #define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) \
87 while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \
88 if (evt_dirent.d_type == DT_DIR && \
89 (strcmp(evt_dirent.d_name, ".")) && \
90 (strcmp(evt_dirent.d_name, "..")) && \
91 (!tp_event_has_id(&sys_dirent, &evt_dirent)))
93 #define MAX_EVENT_LENGTH 512
96 struct tracepoint_path *tracepoint_id_to_path(u64 config)
98 struct tracepoint_path *path = NULL;
99 DIR *sys_dir, *evt_dir;
100 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
104 char evt_path[MAXPATHLEN];
105 char dir_path[MAXPATHLEN];
107 if (debugfs_valid_mountpoint(tracing_events_path))
110 sys_dir = opendir(tracing_events_path);
114 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
116 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
118 evt_dir = opendir(dir_path);
122 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
124 snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
126 fd = open(evt_path, O_RDONLY);
129 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
138 path = zalloc(sizeof(*path));
139 path->system = malloc(MAX_EVENT_LENGTH);
144 path->name = malloc(MAX_EVENT_LENGTH);
150 strncpy(path->system, sys_dirent.d_name,
152 strncpy(path->name, evt_dirent.d_name,
164 #define TP_PATH_LEN (MAX_EVENT_LENGTH * 2 + 1)
165 static const char *tracepoint_id_to_name(u64 config)
167 static char buf[TP_PATH_LEN];
168 struct tracepoint_path *path;
170 path = tracepoint_id_to_path(config);
172 snprintf(buf, TP_PATH_LEN, "%s:%s", path->system, path->name);
177 snprintf(buf, TP_PATH_LEN, "%s:%s", "unknown", "unknown");
182 const char *event_type(int type)
185 case PERF_TYPE_HARDWARE:
188 case PERF_TYPE_SOFTWARE:
191 case PERF_TYPE_TRACEPOINT:
194 case PERF_TYPE_HW_CACHE:
195 return "hardware-cache";
204 const char *event_name(struct perf_evsel *evsel)
207 perf_evsel__name(evsel, bf, sizeof(bf));
211 const char *__event_name(int type, u64 config)
215 if (type == PERF_TYPE_RAW) {
216 sprintf(buf, "raw 0x%" PRIx64, config);
221 case PERF_TYPE_HARDWARE:
222 return __perf_evsel__hw_name(config);
224 case PERF_TYPE_HW_CACHE:
225 __perf_evsel__hw_cache_name(config, buf, sizeof(buf));
228 case PERF_TYPE_SOFTWARE:
229 return __perf_evsel__sw_name(config);
231 case PERF_TYPE_TRACEPOINT:
232 return tracepoint_id_to_name(config);
241 static int add_event(struct list_head **_list, int *idx,
242 struct perf_event_attr *attr, char *name)
244 struct perf_evsel *evsel;
245 struct list_head *list = *_list;
248 list = malloc(sizeof(*list));
251 INIT_LIST_HEAD(list);
254 event_attr_init(attr);
256 evsel = perf_evsel__new(attr, (*idx)++);
262 evsel->name = strdup(name);
263 list_add_tail(&evsel->node, list);
268 static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
273 for (i = 0; i < size; i++) {
274 for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) {
275 n = strlen(names[i][j]);
276 if (n > longest && !strncasecmp(str, names[i][j], n))
286 int parse_events_add_cache(struct list_head **list, int *idx,
287 char *type, char *op_result1, char *op_result2)
289 struct perf_event_attr attr;
290 char name[MAX_NAME_LEN];
291 int cache_type = -1, cache_op = -1, cache_result = -1;
292 char *op_result[2] = { op_result1, op_result2 };
296 * No fallback - if we cannot get a clear cache type
299 cache_type = parse_aliases(type, perf_evsel__hw_cache,
300 PERF_COUNT_HW_CACHE_MAX);
301 if (cache_type == -1)
304 n = snprintf(name, MAX_NAME_LEN, "%s", type);
306 for (i = 0; (i < 2) && (op_result[i]); i++) {
307 char *str = op_result[i];
309 snprintf(name + n, MAX_NAME_LEN - n, "-%s\n", str);
311 if (cache_op == -1) {
312 cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
313 PERF_COUNT_HW_CACHE_OP_MAX);
315 if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
321 if (cache_result == -1) {
322 cache_result = parse_aliases(str, perf_evsel__hw_cache_result,
323 PERF_COUNT_HW_CACHE_RESULT_MAX);
324 if (cache_result >= 0)
330 * Fall back to reads:
333 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
336 * Fall back to accesses:
338 if (cache_result == -1)
339 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
341 memset(&attr, 0, sizeof(attr));
342 attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
343 attr.type = PERF_TYPE_HW_CACHE;
344 return add_event(list, idx, &attr, name);
347 static int add_tracepoint(struct list_head **list, int *idx,
348 char *sys_name, char *evt_name)
350 struct perf_event_attr attr;
351 char name[MAX_NAME_LEN];
352 char evt_path[MAXPATHLEN];
357 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path,
360 fd = open(evt_path, O_RDONLY);
364 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
372 memset(&attr, 0, sizeof(attr));
374 attr.type = PERF_TYPE_TRACEPOINT;
375 attr.sample_type |= PERF_SAMPLE_RAW;
376 attr.sample_type |= PERF_SAMPLE_TIME;
377 attr.sample_type |= PERF_SAMPLE_CPU;
378 attr.sample_period = 1;
380 snprintf(name, MAX_NAME_LEN, "%s:%s", sys_name, evt_name);
381 return add_event(list, idx, &attr, name);
384 static int add_tracepoint_multi(struct list_head **list, int *idx,
385 char *sys_name, char *evt_name)
387 char evt_path[MAXPATHLEN];
388 struct dirent *evt_ent;
392 snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name);
393 evt_dir = opendir(evt_path);
395 perror("Can't open event dir");
399 while (!ret && (evt_ent = readdir(evt_dir))) {
400 if (!strcmp(evt_ent->d_name, ".")
401 || !strcmp(evt_ent->d_name, "..")
402 || !strcmp(evt_ent->d_name, "enable")
403 || !strcmp(evt_ent->d_name, "filter"))
406 if (!strglobmatch(evt_ent->d_name, evt_name))
409 ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name);
415 int parse_events_add_tracepoint(struct list_head **list, int *idx,
416 char *sys, char *event)
420 ret = debugfs_valid_mountpoint(tracing_events_path);
424 return strpbrk(event, "*?") ?
425 add_tracepoint_multi(list, idx, sys, event) :
426 add_tracepoint(list, idx, sys, event);
430 parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
434 for (i = 0; i < 3; i++) {
435 if (!type || !type[i])
440 attr->bp_type |= HW_BREAKPOINT_R;
443 attr->bp_type |= HW_BREAKPOINT_W;
446 attr->bp_type |= HW_BREAKPOINT_X;
453 if (!attr->bp_type) /* Default */
454 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
459 int parse_events_add_breakpoint(struct list_head **list, int *idx,
460 void *ptr, char *type)
462 struct perf_event_attr attr;
463 char name[MAX_NAME_LEN];
465 memset(&attr, 0, sizeof(attr));
466 attr.bp_addr = (unsigned long) ptr;
468 if (parse_breakpoint_type(type, &attr))
472 * We should find a nice way to override the access length
473 * Provide some defaults for now
475 if (attr.bp_type == HW_BREAKPOINT_X)
476 attr.bp_len = sizeof(long);
478 attr.bp_len = HW_BREAKPOINT_LEN_4;
480 attr.type = PERF_TYPE_BREAKPOINT;
482 snprintf(name, MAX_NAME_LEN, "mem:%p:%s", ptr, type ? type : "rw");
483 return add_event(list, idx, &attr, name);
486 static int config_term(struct perf_event_attr *attr,
487 struct parse_events__term *term)
489 #define CHECK_TYPE_VAL(type) \
491 if (PARSE_EVENTS__TERM_TYPE_ ## type != term->type_val) \
495 switch (term->type_term) {
496 case PARSE_EVENTS__TERM_TYPE_CONFIG:
498 attr->config = term->val.num;
500 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
502 attr->config1 = term->val.num;
504 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
506 attr->config2 = term->val.num;
508 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
510 attr->sample_period = term->val.num;
512 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
514 * TODO uncomment when the field is available
515 * attr->branch_sample_type = term->val.num;
518 case PARSE_EVENTS__TERM_TYPE_NAME:
526 #undef CHECK_TYPE_VAL
529 static int config_attr(struct perf_event_attr *attr,
530 struct list_head *head, int fail)
532 struct parse_events__term *term;
534 list_for_each_entry(term, head, list)
535 if (config_term(attr, term) && fail)
541 int parse_events_add_numeric(struct list_head **list, int *idx,
542 unsigned long type, unsigned long config,
543 struct list_head *head_config)
545 struct perf_event_attr attr;
547 memset(&attr, 0, sizeof(attr));
549 attr.config = config;
552 config_attr(&attr, head_config, 1))
555 return add_event(list, idx, &attr,
556 (char *) __event_name(type, config));
559 static int parse_events__is_name_term(struct parse_events__term *term)
561 return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
564 static char *pmu_event_name(struct perf_event_attr *attr,
565 struct list_head *head_terms)
567 struct parse_events__term *term;
569 list_for_each_entry(term, head_terms, list)
570 if (parse_events__is_name_term(term))
571 return term->val.str;
573 return (char *) __event_name(PERF_TYPE_RAW, attr->config);
576 int parse_events_add_pmu(struct list_head **list, int *idx,
577 char *name, struct list_head *head_config)
579 struct perf_event_attr attr;
580 struct perf_pmu *pmu;
582 pmu = perf_pmu__find(name);
586 memset(&attr, 0, sizeof(attr));
589 * Configure hardcoded terms first, no need to check
590 * return value when called with fail == 0 ;)
592 config_attr(&attr, head_config, 0);
594 if (perf_pmu__config(pmu, &attr, head_config))
597 return add_event(list, idx, &attr,
598 pmu_event_name(&attr, head_config));
601 void parse_events_update_lists(struct list_head *list_event,
602 struct list_head *list_all)
605 * Called for single event definition. Update the
606 * 'all event' list, and reinit the 'signle event'
607 * list, for next event definition.
609 list_splice_tail(list_event, list_all);
613 int parse_events_modifier(struct list_head *list, char *str)
615 struct perf_evsel *evsel;
616 int exclude = 0, exclude_GH = 0;
617 int eu = 0, ek = 0, eh = 0, eH = 0, eG = 0, precise = 0;
625 exclude = eu = ek = eh = 1;
627 } else if (*str == 'k') {
629 exclude = eu = ek = eh = 1;
631 } else if (*str == 'h') {
633 exclude = eu = ek = eh = 1;
635 } else if (*str == 'G') {
637 exclude_GH = eG = eH = 1;
639 } else if (*str == 'H') {
641 exclude_GH = eG = eH = 1;
643 } else if (*str == 'p') {
654 * 0 - SAMPLE_IP can have arbitrary skid
655 * 1 - SAMPLE_IP must have constant skid
656 * 2 - SAMPLE_IP requested to have 0 skid
657 * 3 - SAMPLE_IP must have 0 skid
659 * See also PERF_RECORD_MISC_EXACT_IP
664 list_for_each_entry(evsel, list, node) {
665 evsel->attr.exclude_user = eu;
666 evsel->attr.exclude_kernel = ek;
667 evsel->attr.exclude_hv = eh;
668 evsel->attr.precise_ip = precise;
669 evsel->attr.exclude_host = eH;
670 evsel->attr.exclude_guest = eG;
676 int parse_events(struct perf_evlist *evlist, const char *str, int unset __used)
680 YY_BUFFER_STATE buffer;
681 int ret, idx = evlist->nr_entries;
683 buffer = parse_events__scan_string(str);
686 parse_events_debug = 1;
688 ret = parse_events_parse(&list, &idx);
690 parse_events__flush_buffer(buffer);
691 parse_events__delete_buffer(buffer);
692 parse_events_lex_destroy();
695 int entries = idx - evlist->nr_entries;
696 perf_evlist__splice_list_tail(evlist, &list, entries);
701 * There are 2 users - builtin-record and builtin-test objects.
702 * Both call perf_evlist__delete in case of error, so we dont
705 fprintf(stderr, "invalid or unsupported event: '%s'\n", str);
706 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
710 int parse_events_option(const struct option *opt, const char *str,
713 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
714 return parse_events(evlist, str, unset);
717 int parse_filter(const struct option *opt, const char *str,
720 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
721 struct perf_evsel *last = NULL;
723 if (evlist->nr_entries > 0)
724 last = list_entry(evlist->entries.prev, struct perf_evsel, node);
726 if (last == NULL || last->attr.type != PERF_TYPE_TRACEPOINT) {
728 "-F option should follow a -e tracepoint option\n");
732 last->filter = strdup(str);
733 if (last->filter == NULL) {
734 fprintf(stderr, "not enough memory to hold filter string\n");
741 static const char * const event_type_descriptors[] = {
745 "Hardware cache event",
746 "Raw hardware event descriptor",
747 "Hardware breakpoint",
751 * Print the events from <debugfs_mount_point>/tracing/events
754 void print_tracepoint_events(const char *subsys_glob, const char *event_glob)
756 DIR *sys_dir, *evt_dir;
757 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
758 char evt_path[MAXPATHLEN];
759 char dir_path[MAXPATHLEN];
761 if (debugfs_valid_mountpoint(tracing_events_path))
764 sys_dir = opendir(tracing_events_path);
768 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
769 if (subsys_glob != NULL &&
770 !strglobmatch(sys_dirent.d_name, subsys_glob))
773 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
775 evt_dir = opendir(dir_path);
779 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
780 if (event_glob != NULL &&
781 !strglobmatch(evt_dirent.d_name, event_glob))
784 snprintf(evt_path, MAXPATHLEN, "%s:%s",
785 sys_dirent.d_name, evt_dirent.d_name);
786 printf(" %-50s [%s]\n", evt_path,
787 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
795 * Check whether event is in <debugfs_mount_point>/tracing/events
798 int is_valid_tracepoint(const char *event_string)
800 DIR *sys_dir, *evt_dir;
801 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
802 char evt_path[MAXPATHLEN];
803 char dir_path[MAXPATHLEN];
805 if (debugfs_valid_mountpoint(tracing_events_path))
808 sys_dir = opendir(tracing_events_path);
812 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
814 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
816 evt_dir = opendir(dir_path);
820 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
821 snprintf(evt_path, MAXPATHLEN, "%s:%s",
822 sys_dirent.d_name, evt_dirent.d_name);
823 if (!strcmp(evt_path, event_string)) {
835 void print_events_type(u8 type)
837 struct event_symbol *syms = event_symbols;
841 for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
842 if (type != syms->type)
845 if (strlen(syms->alias))
846 snprintf(name, sizeof(name), "%s OR %s",
847 syms->symbol, syms->alias);
849 snprintf(name, sizeof(name), "%s", syms->symbol);
851 printf(" %-50s [%s]\n", name,
852 event_type_descriptors[type]);
856 int print_hwcache_events(const char *event_glob)
858 unsigned int type, op, i, printed = 0;
861 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
862 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
863 /* skip invalid cache type */
864 if (!perf_evsel__is_cache_op_valid(type, op))
867 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
868 __perf_evsel__hw_cache_type_op_res_name(type, op, i,
870 if (event_glob != NULL && !strglobmatch(name, event_glob))
873 printf(" %-50s [%s]\n", name,
874 event_type_descriptors[PERF_TYPE_HW_CACHE]);
884 * Print the help text for the event symbols:
886 void print_events(const char *event_glob)
888 unsigned int i, type, prev_type = -1, printed = 0, ntypes_printed = 0;
889 struct event_symbol *syms = event_symbols;
890 char name[MAX_NAME_LEN];
893 printf("List of pre-defined events (to be used in -e):\n");
895 for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
898 if (type != prev_type && printed) {
904 if (event_glob != NULL &&
905 !(strglobmatch(syms->symbol, event_glob) ||
906 (syms->alias && strglobmatch(syms->alias, event_glob))))
909 if (strlen(syms->alias))
910 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
912 strncpy(name, syms->symbol, MAX_NAME_LEN);
913 printf(" %-50s [%s]\n", name,
914 event_type_descriptors[type]);
920 if (ntypes_printed) {
924 print_hwcache_events(event_glob);
926 if (event_glob != NULL)
930 printf(" %-50s [%s]\n",
932 event_type_descriptors[PERF_TYPE_RAW]);
933 printf(" %-50s [%s]\n",
934 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
935 event_type_descriptors[PERF_TYPE_RAW]);
936 printf(" (see 'perf list --help' on how to encode it)\n");
939 printf(" %-50s [%s]\n",
940 "mem:<addr>[:access]",
941 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
944 print_tracepoint_events(NULL, NULL);
947 int parse_events__is_hardcoded_term(struct parse_events__term *term)
949 return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
952 static int new_term(struct parse_events__term **_term, int type_val,
953 int type_term, char *config,
956 struct parse_events__term *term;
958 term = zalloc(sizeof(*term));
962 INIT_LIST_HEAD(&term->list);
963 term->type_val = type_val;
964 term->type_term = type_term;
965 term->config = config;
968 case PARSE_EVENTS__TERM_TYPE_NUM:
971 case PARSE_EVENTS__TERM_TYPE_STR:
982 int parse_events__term_num(struct parse_events__term **term,
983 int type_term, char *config, long num)
985 return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term,
989 int parse_events__term_str(struct parse_events__term **term,
990 int type_term, char *config, char *str)
992 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term,
996 void parse_events__free_terms(struct list_head *terms)
998 struct parse_events__term *term, *h;
1000 list_for_each_entry_safe(term, h, terms, list)