1 #include "../../../include/linux/hw_breakpoint.h"
5 #include "parse-options.h"
6 #include "parse-events.h"
16 LIST_HEAD(evsel_list);
31 char debugfs_path[MAXPATHLEN];
33 #define CHW(x) .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_##x
34 #define CSW(x) .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_##x
36 static struct event_symbol event_symbols[] = {
37 { CHW(CPU_CYCLES), "cpu-cycles", "cycles" },
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", "" },
45 { CSW(CPU_CLOCK), "cpu-clock", "" },
46 { CSW(TASK_CLOCK), "task-clock", "" },
47 { CSW(PAGE_FAULTS), "page-faults", "faults" },
48 { CSW(PAGE_FAULTS_MIN), "minor-faults", "" },
49 { CSW(PAGE_FAULTS_MAJ), "major-faults", "" },
50 { CSW(CONTEXT_SWITCHES), "context-switches", "cs" },
51 { CSW(CPU_MIGRATIONS), "cpu-migrations", "migrations" },
52 { CSW(ALIGNMENT_FAULTS), "alignment-faults", "" },
53 { CSW(EMULATION_FAULTS), "emulation-faults", "" },
56 #define __PERF_EVENT_FIELD(config, name) \
57 ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
59 #define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
60 #define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
61 #define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
62 #define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
64 static const char *hw_event_names[] = {
74 static const char *sw_event_names[] = {
88 static const char *hw_cache[][MAX_ALIASES] = {
89 { "L1-dcache", "l1-d", "l1d", "L1-data", },
90 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
92 { "dTLB", "d-tlb", "Data-TLB", },
93 { "iTLB", "i-tlb", "Instruction-TLB", },
94 { "branch", "branches", "bpu", "btb", "bpc", },
97 static const char *hw_cache_op[][MAX_ALIASES] = {
98 { "load", "loads", "read", },
99 { "store", "stores", "write", },
100 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
103 static const char *hw_cache_result[][MAX_ALIASES] = {
104 { "refs", "Reference", "ops", "access", },
105 { "misses", "miss", },
108 #define C(x) PERF_COUNT_HW_CACHE_##x
109 #define CACHE_READ (1 << C(OP_READ))
110 #define CACHE_WRITE (1 << C(OP_WRITE))
111 #define CACHE_PREFETCH (1 << C(OP_PREFETCH))
112 #define COP(x) (1 << x)
115 * cache operartion stat
116 * L1I : Read and prefetch only
117 * ITLB and BPU : Read-only
119 static unsigned long hw_cache_stat[C(MAX)] = {
120 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
121 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
122 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
123 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
124 [C(ITLB)] = (CACHE_READ),
125 [C(BPU)] = (CACHE_READ),
128 #define for_each_subsystem(sys_dir, sys_dirent, sys_next) \
129 while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \
130 if (sys_dirent.d_type == DT_DIR && \
131 (strcmp(sys_dirent.d_name, ".")) && \
132 (strcmp(sys_dirent.d_name, "..")))
134 static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
136 char evt_path[MAXPATHLEN];
139 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", debugfs_path,
140 sys_dir->d_name, evt_dir->d_name);
141 fd = open(evt_path, O_RDONLY);
149 #define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) \
150 while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \
151 if (evt_dirent.d_type == DT_DIR && \
152 (strcmp(evt_dirent.d_name, ".")) && \
153 (strcmp(evt_dirent.d_name, "..")) && \
154 (!tp_event_has_id(&sys_dirent, &evt_dirent)))
156 #define MAX_EVENT_LENGTH 512
159 struct tracepoint_path *tracepoint_id_to_path(u64 config)
161 struct tracepoint_path *path = NULL;
162 DIR *sys_dir, *evt_dir;
163 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
167 char evt_path[MAXPATHLEN];
168 char dir_path[MAXPATHLEN];
170 if (debugfs_valid_mountpoint(debugfs_path))
173 sys_dir = opendir(debugfs_path);
177 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
179 snprintf(dir_path, MAXPATHLEN, "%s/%s", debugfs_path,
181 evt_dir = opendir(dir_path);
185 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
187 snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
189 fd = open(evt_path, O_RDONLY);
192 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
201 path = zalloc(sizeof(*path));
202 path->system = malloc(MAX_EVENT_LENGTH);
207 path->name = malloc(MAX_EVENT_LENGTH);
213 strncpy(path->system, sys_dirent.d_name,
215 strncpy(path->name, evt_dirent.d_name,
227 #define TP_PATH_LEN (MAX_EVENT_LENGTH * 2 + 1)
228 static const char *tracepoint_id_to_name(u64 config)
230 static char buf[TP_PATH_LEN];
231 struct tracepoint_path *path;
233 path = tracepoint_id_to_path(config);
235 snprintf(buf, TP_PATH_LEN, "%s:%s", path->system, path->name);
240 snprintf(buf, TP_PATH_LEN, "%s:%s", "unknown", "unknown");
245 static int is_cache_op_valid(u8 cache_type, u8 cache_op)
247 if (hw_cache_stat[cache_type] & COP(cache_op))
248 return 1; /* valid */
250 return 0; /* invalid */
253 static char *event_cache_name(u8 cache_type, u8 cache_op, u8 cache_result)
255 static char name[50];
258 sprintf(name, "%s-%s-%s", hw_cache[cache_type][0],
259 hw_cache_op[cache_op][0],
260 hw_cache_result[cache_result][0]);
262 sprintf(name, "%s-%s", hw_cache[cache_type][0],
263 hw_cache_op[cache_op][1]);
269 const char *event_name(struct perf_evsel *evsel)
271 u64 config = evsel->attr.config;
272 int type = evsel->attr.type;
274 return __event_name(type, config);
277 const char *__event_name(int type, u64 config)
281 if (type == PERF_TYPE_RAW) {
282 sprintf(buf, "raw 0x%llx", config);
287 case PERF_TYPE_HARDWARE:
288 if (config < PERF_COUNT_HW_MAX)
289 return hw_event_names[config];
290 return "unknown-hardware";
292 case PERF_TYPE_HW_CACHE: {
293 u8 cache_type, cache_op, cache_result;
295 cache_type = (config >> 0) & 0xff;
296 if (cache_type > PERF_COUNT_HW_CACHE_MAX)
297 return "unknown-ext-hardware-cache-type";
299 cache_op = (config >> 8) & 0xff;
300 if (cache_op > PERF_COUNT_HW_CACHE_OP_MAX)
301 return "unknown-ext-hardware-cache-op";
303 cache_result = (config >> 16) & 0xff;
304 if (cache_result > PERF_COUNT_HW_CACHE_RESULT_MAX)
305 return "unknown-ext-hardware-cache-result";
307 if (!is_cache_op_valid(cache_type, cache_op))
308 return "invalid-cache";
310 return event_cache_name(cache_type, cache_op, cache_result);
313 case PERF_TYPE_SOFTWARE:
314 if (config < PERF_COUNT_SW_MAX)
315 return sw_event_names[config];
316 return "unknown-software";
318 case PERF_TYPE_TRACEPOINT:
319 return tracepoint_id_to_name(config);
328 static int parse_aliases(const char **str, const char *names[][MAX_ALIASES], int size)
333 for (i = 0; i < size; i++) {
334 for (j = 0; j < MAX_ALIASES && names[i][j]; j++) {
335 n = strlen(names[i][j]);
336 if (n > longest && !strncasecmp(*str, names[i][j], n))
348 static enum event_result
349 parse_generic_hw_event(const char **str, struct perf_event_attr *attr)
351 const char *s = *str;
352 int cache_type = -1, cache_op = -1, cache_result = -1;
354 cache_type = parse_aliases(&s, hw_cache, PERF_COUNT_HW_CACHE_MAX);
356 * No fallback - if we cannot get a clear cache type
359 if (cache_type == -1)
362 while ((cache_op == -1 || cache_result == -1) && *s == '-') {
365 if (cache_op == -1) {
366 cache_op = parse_aliases(&s, hw_cache_op,
367 PERF_COUNT_HW_CACHE_OP_MAX);
369 if (!is_cache_op_valid(cache_type, cache_op))
375 if (cache_result == -1) {
376 cache_result = parse_aliases(&s, hw_cache_result,
377 PERF_COUNT_HW_CACHE_RESULT_MAX);
378 if (cache_result >= 0)
383 * Can't parse this as a cache op or result, so back up
391 * Fall back to reads:
394 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
397 * Fall back to accesses:
399 if (cache_result == -1)
400 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
402 attr->config = cache_type | (cache_op << 8) | (cache_result << 16);
403 attr->type = PERF_TYPE_HW_CACHE;
409 static enum event_result
410 parse_single_tracepoint_event(char *sys_name,
411 const char *evt_name,
412 unsigned int evt_length,
413 struct perf_event_attr *attr,
416 char evt_path[MAXPATHLEN];
421 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", debugfs_path,
424 fd = open(evt_path, O_RDONLY);
428 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
436 attr->type = PERF_TYPE_TRACEPOINT;
437 *strp += strlen(sys_name) + evt_length + 1; /* + 1 for the ':' */
439 attr->sample_type |= PERF_SAMPLE_RAW;
440 attr->sample_type |= PERF_SAMPLE_TIME;
441 attr->sample_type |= PERF_SAMPLE_CPU;
443 attr->sample_period = 1;
449 /* sys + ':' + event + ':' + flags*/
450 #define MAX_EVOPT_LEN (MAX_EVENT_LENGTH * 2 + 2 + 128)
451 static enum event_result
452 parse_multiple_tracepoint_event(char *sys_name, const char *evt_exp,
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];
471 if (!strcmp(evt_ent->d_name, ".")
472 || !strcmp(evt_ent->d_name, "..")
473 || !strcmp(evt_ent->d_name, "enable")
474 || !strcmp(evt_ent->d_name, "filter"))
477 if (!strglobmatch(evt_ent->d_name, evt_exp))
480 len = snprintf(event_opt, MAX_EVOPT_LEN, "%s:%s%s%s", sys_name,
481 evt_ent->d_name, flags ? ":" : "",
486 if (parse_events(NULL, event_opt, 0))
490 return EVT_HANDLED_ALL;
493 static int store_event_type(const char *orgname)
495 char filename[PATH_MAX], *c;
499 sprintf(filename, "%s/", debugfs_path);
500 strncat(filename, orgname, strlen(orgname));
501 strcat(filename, "/id");
503 c = strchr(filename, ':');
507 file = fopen(filename, "r");
510 n = fscanf(file, "%i", &id);
513 pr_err("cannot store event ID\n");
516 return perf_header__push_event(id, orgname);
519 static enum event_result parse_tracepoint_event(const char **strp,
520 struct perf_event_attr *attr)
522 const char *evt_name;
523 char *flags = NULL, *comma_loc;
524 char sys_name[MAX_EVENT_LENGTH];
525 unsigned int sys_length, evt_length;
527 if (debugfs_valid_mountpoint(debugfs_path))
530 evt_name = strchr(*strp, ':');
534 sys_length = evt_name - *strp;
535 if (sys_length >= MAX_EVENT_LENGTH)
538 strncpy(sys_name, *strp, sys_length);
539 sys_name[sys_length] = '\0';
540 evt_name = evt_name + 1;
542 comma_loc = strchr(evt_name, ',');
544 /* take the event name up to the comma */
545 evt_name = strndup(evt_name, comma_loc - evt_name);
547 flags = strchr(evt_name, ':');
550 evt_name = strndup(evt_name, flags - evt_name);
554 evt_length = strlen(evt_name);
555 if (evt_length >= MAX_EVENT_LENGTH)
557 if (strpbrk(evt_name, "*?")) {
558 *strp += strlen(sys_name) + evt_length;
559 return parse_multiple_tracepoint_event(sys_name, evt_name,
562 if (store_event_type(evt_name) < 0)
565 return parse_single_tracepoint_event(sys_name, evt_name,
566 evt_length, attr, strp);
570 static enum event_result
571 parse_breakpoint_type(const char *type, const char **strp,
572 struct perf_event_attr *attr)
576 for (i = 0; i < 3; i++) {
582 attr->bp_type |= HW_BREAKPOINT_R;
585 attr->bp_type |= HW_BREAKPOINT_W;
588 attr->bp_type |= HW_BREAKPOINT_X;
594 if (!attr->bp_type) /* Default */
595 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
602 static enum event_result
603 parse_breakpoint_event(const char **strp, struct perf_event_attr *attr)
609 enum event_result err;
611 target = strchr(*strp, ':');
615 if (strncmp(*strp, "mem", target - *strp) != 0)
620 addr = strtoull(target, &endaddr, 0);
621 if (target == endaddr)
624 attr->bp_addr = addr;
627 type = strchr(target, ':');
629 /* If no type is defined, just rw as default */
631 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
633 err = parse_breakpoint_type(++type, strp, attr);
634 if (err == EVT_FAILED)
639 * We should find a nice way to override the access length
640 * Provide some defaults for now
642 if (attr->bp_type == HW_BREAKPOINT_X)
643 attr->bp_len = sizeof(long);
645 attr->bp_len = HW_BREAKPOINT_LEN_4;
647 attr->type = PERF_TYPE_BREAKPOINT;
652 static int check_events(const char *str, unsigned int i)
656 n = strlen(event_symbols[i].symbol);
657 if (!strncmp(str, event_symbols[i].symbol, n))
660 n = strlen(event_symbols[i].alias);
662 if (!strncmp(str, event_symbols[i].alias, n))
667 static enum event_result
668 parse_symbolic_event(const char **strp, struct perf_event_attr *attr)
670 const char *str = *strp;
674 for (i = 0; i < ARRAY_SIZE(event_symbols); i++) {
675 n = check_events(str, i);
677 attr->type = event_symbols[i].type;
678 attr->config = event_symbols[i].config;
686 static enum event_result
687 parse_raw_event(const char **strp, struct perf_event_attr *attr)
689 const char *str = *strp;
695 n = hex2u64(str + 1, &config);
698 attr->type = PERF_TYPE_RAW;
699 attr->config = config;
705 static enum event_result
706 parse_numeric_event(const char **strp, struct perf_event_attr *attr)
708 const char *str = *strp;
713 type = strtoul(str, &endp, 0);
714 if (endp > str && type < PERF_TYPE_MAX && *endp == ':') {
716 config = strtoul(str, &endp, 0);
719 attr->config = config;
727 static enum event_result
728 parse_event_modifier(const char **strp, struct perf_event_attr *attr)
730 const char *str = *strp;
732 int eu = 0, ek = 0, eh = 0, precise = 0;
739 exclude = eu = ek = eh = 1;
741 } else if (*str == 'k') {
743 exclude = eu = ek = eh = 1;
745 } else if (*str == 'h') {
747 exclude = eu = ek = eh = 1;
749 } else if (*str == 'p') {
756 if (str >= *strp + 2) {
758 attr->exclude_user = eu;
759 attr->exclude_kernel = ek;
760 attr->exclude_hv = eh;
761 attr->precise_ip = precise;
768 * Each event can have multiple symbolic names.
769 * Symbolic names are (almost) exactly matched.
771 static enum event_result
772 parse_event_symbols(const char **str, struct perf_event_attr *attr)
774 enum event_result ret;
776 ret = parse_tracepoint_event(str, attr);
777 if (ret != EVT_FAILED)
780 ret = parse_raw_event(str, attr);
781 if (ret != EVT_FAILED)
784 ret = parse_numeric_event(str, attr);
785 if (ret != EVT_FAILED)
788 ret = parse_symbolic_event(str, attr);
789 if (ret != EVT_FAILED)
792 ret = parse_generic_hw_event(str, attr);
793 if (ret != EVT_FAILED)
796 ret = parse_breakpoint_event(str, attr);
797 if (ret != EVT_FAILED)
800 fprintf(stderr, "invalid or unsupported event: '%s'\n", *str);
801 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
805 parse_event_modifier(str, attr);
810 int parse_events(const struct option *opt __used, const char *str, int unset __used)
812 struct perf_event_attr attr;
813 enum event_result ret;
816 memset(&attr, 0, sizeof(attr));
817 ret = parse_event_symbols(&str, &attr);
818 if (ret == EVT_FAILED)
821 if (!(*str == 0 || *str == ',' || isspace(*str)))
824 if (ret != EVT_HANDLED_ALL) {
825 struct perf_evsel *evsel;
826 evsel = perf_evsel__new(&attr,
830 list_add_tail(&evsel->node, &evsel_list);
838 while (isspace(*str))
845 int parse_filter(const struct option *opt __used, const char *str,
848 struct perf_evsel *last = NULL;
850 if (!list_empty(&evsel_list))
851 last = list_entry(evsel_list.prev, struct perf_evsel, node);
853 if (last == NULL || last->attr.type != PERF_TYPE_TRACEPOINT) {
855 "-F option should follow a -e tracepoint option\n");
859 last->filter = strdup(str);
860 if (last->filter == NULL) {
861 fprintf(stderr, "not enough memory to hold filter string\n");
868 static const char * const event_type_descriptors[] = {
872 "Hardware cache event",
873 "Raw hardware event descriptor",
874 "Hardware breakpoint",
878 * Print the events from <debugfs_mount_point>/tracing/events
881 static void print_tracepoint_events(void)
883 DIR *sys_dir, *evt_dir;
884 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
885 char evt_path[MAXPATHLEN];
886 char dir_path[MAXPATHLEN];
888 if (debugfs_valid_mountpoint(debugfs_path))
891 sys_dir = opendir(debugfs_path);
895 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
897 snprintf(dir_path, MAXPATHLEN, "%s/%s", debugfs_path,
899 evt_dir = opendir(dir_path);
903 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
904 snprintf(evt_path, MAXPATHLEN, "%s:%s",
905 sys_dirent.d_name, evt_dirent.d_name);
906 printf(" %-42s [%s]\n", evt_path,
907 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
915 * Check whether event is in <debugfs_mount_point>/tracing/events
918 int is_valid_tracepoint(const char *event_string)
920 DIR *sys_dir, *evt_dir;
921 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
922 char evt_path[MAXPATHLEN];
923 char dir_path[MAXPATHLEN];
925 if (debugfs_valid_mountpoint(debugfs_path))
928 sys_dir = opendir(debugfs_path);
932 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
934 snprintf(dir_path, MAXPATHLEN, "%s/%s", debugfs_path,
936 evt_dir = opendir(dir_path);
940 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
941 snprintf(evt_path, MAXPATHLEN, "%s:%s",
942 sys_dirent.d_name, evt_dirent.d_name);
943 if (!strcmp(evt_path, event_string)) {
956 * Print the help text for the event symbols:
958 void print_events(void)
960 struct event_symbol *syms = event_symbols;
961 unsigned int i, type, op, prev_type = -1;
965 printf("List of pre-defined events (to be used in -e):\n");
967 for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
970 if (type != prev_type)
973 if (strlen(syms->alias))
974 sprintf(name, "%s OR %s", syms->symbol, syms->alias);
976 strcpy(name, syms->symbol);
977 printf(" %-42s [%s]\n", name,
978 event_type_descriptors[type]);
984 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
985 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
986 /* skip invalid cache type */
987 if (!is_cache_op_valid(type, op))
990 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
991 printf(" %-42s [%s]\n",
992 event_cache_name(type, op, i),
993 event_type_descriptors[PERF_TYPE_HW_CACHE]);
999 printf(" %-42s [%s]\n",
1000 "rNNN (see 'perf list --help' on how to encode it)",
1001 event_type_descriptors[PERF_TYPE_RAW]);
1004 printf(" %-42s [%s]\n",
1005 "mem:<addr>[:access]",
1006 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
1009 print_tracepoint_events();
1014 int perf_evsel_list__create_default(void)
1016 struct perf_evsel *evsel;
1017 struct perf_event_attr attr;
1019 memset(&attr, 0, sizeof(attr));
1020 attr.type = PERF_TYPE_HARDWARE;
1021 attr.config = PERF_COUNT_HW_CPU_CYCLES;
1023 evsel = perf_evsel__new(&attr, 0);
1028 list_add(&evsel->node, &evsel_list);
1033 void perf_evsel_list__delete(void)
1035 struct perf_evsel *pos, *n;
1037 list_for_each_entry_safe(pos, n, &evsel_list, node) {
1038 list_del_init(&pos->node);
1039 perf_evsel__delete(pos);