perf stat: Make all displayed event names parseable as well
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / tools / perf / util / parse-events.c
blobb5bfef12f399e18363306eb91d18aec1293b1699
1 #include "../../../include/linux/hw_breakpoint.h"
2 #include "util.h"
3 #include "../perf.h"
4 #include "evlist.h"
5 #include "evsel.h"
6 #include "parse-options.h"
7 #include "parse-events.h"
8 #include "exec_cmd.h"
9 #include "string.h"
10 #include "symbol.h"
11 #include "cache.h"
12 #include "header.h"
13 #include "debugfs.h"
15 struct event_symbol {
16 u8 type;
17 u64 config;
18 const char *symbol;
19 const char *alias;
22 enum event_result {
23 EVT_FAILED,
24 EVT_HANDLED,
25 EVT_HANDLED_ALL
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", "" },
41 { CHW(STALLED_CYCLES), "stalled-cycles", "" },
43 { CSW(CPU_CLOCK), "cpu-clock", "" },
44 { CSW(TASK_CLOCK), "task-clock", "" },
45 { CSW(PAGE_FAULTS), "page-faults", "faults" },
46 { CSW(PAGE_FAULTS_MIN), "minor-faults", "" },
47 { CSW(PAGE_FAULTS_MAJ), "major-faults", "" },
48 { CSW(CONTEXT_SWITCHES), "context-switches", "cs" },
49 { CSW(CPU_MIGRATIONS), "cpu-migrations", "migrations" },
50 { CSW(ALIGNMENT_FAULTS), "alignment-faults", "" },
51 { CSW(EMULATION_FAULTS), "emulation-faults", "" },
54 #define __PERF_EVENT_FIELD(config, name) \
55 ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
57 #define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
58 #define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
59 #define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
60 #define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
62 static const char *hw_event_names[] = {
63 "cycles",
64 "instructions",
65 "cache-references",
66 "cache-misses",
67 "branches",
68 "branch-misses",
69 "bus-cycles",
72 static const char *sw_event_names[] = {
73 "cpu-clock",
74 "task-clock",
75 "page-faults",
76 "context-switches",
77 "CPU-migrations",
78 "minor-faults",
79 "major-faults",
80 "alignment-faults",
81 "emulation-faults",
84 #define MAX_ALIASES 8
86 static const char *hw_cache[][MAX_ALIASES] = {
87 { "L1-dcache", "l1-d", "l1d", "L1-data", },
88 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
89 { "LLC", "L2" },
90 { "dTLB", "d-tlb", "Data-TLB", },
91 { "iTLB", "i-tlb", "Instruction-TLB", },
92 { "branch", "branches", "bpu", "btb", "bpc", },
95 static const char *hw_cache_op[][MAX_ALIASES] = {
96 { "load", "loads", "read", },
97 { "store", "stores", "write", },
98 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
101 static const char *hw_cache_result[][MAX_ALIASES] = {
102 { "refs", "Reference", "ops", "access", },
103 { "misses", "miss", },
106 #define C(x) PERF_COUNT_HW_CACHE_##x
107 #define CACHE_READ (1 << C(OP_READ))
108 #define CACHE_WRITE (1 << C(OP_WRITE))
109 #define CACHE_PREFETCH (1 << C(OP_PREFETCH))
110 #define COP(x) (1 << x)
113 * cache operartion stat
114 * L1I : Read and prefetch only
115 * ITLB and BPU : Read-only
117 static unsigned long hw_cache_stat[C(MAX)] = {
118 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
119 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
120 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
121 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
122 [C(ITLB)] = (CACHE_READ),
123 [C(BPU)] = (CACHE_READ),
126 #define for_each_subsystem(sys_dir, sys_dirent, sys_next) \
127 while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \
128 if (sys_dirent.d_type == DT_DIR && \
129 (strcmp(sys_dirent.d_name, ".")) && \
130 (strcmp(sys_dirent.d_name, "..")))
132 static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
134 char evt_path[MAXPATHLEN];
135 int fd;
137 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", debugfs_path,
138 sys_dir->d_name, evt_dir->d_name);
139 fd = open(evt_path, O_RDONLY);
140 if (fd < 0)
141 return -EINVAL;
142 close(fd);
144 return 0;
147 #define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) \
148 while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \
149 if (evt_dirent.d_type == DT_DIR && \
150 (strcmp(evt_dirent.d_name, ".")) && \
151 (strcmp(evt_dirent.d_name, "..")) && \
152 (!tp_event_has_id(&sys_dirent, &evt_dirent)))
154 #define MAX_EVENT_LENGTH 512
157 struct tracepoint_path *tracepoint_id_to_path(u64 config)
159 struct tracepoint_path *path = NULL;
160 DIR *sys_dir, *evt_dir;
161 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
162 char id_buf[4];
163 int fd;
164 u64 id;
165 char evt_path[MAXPATHLEN];
166 char dir_path[MAXPATHLEN];
168 if (debugfs_valid_mountpoint(debugfs_path))
169 return NULL;
171 sys_dir = opendir(debugfs_path);
172 if (!sys_dir)
173 return NULL;
175 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
177 snprintf(dir_path, MAXPATHLEN, "%s/%s", debugfs_path,
178 sys_dirent.d_name);
179 evt_dir = opendir(dir_path);
180 if (!evt_dir)
181 continue;
183 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
185 snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
186 evt_dirent.d_name);
187 fd = open(evt_path, O_RDONLY);
188 if (fd < 0)
189 continue;
190 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
191 close(fd);
192 continue;
194 close(fd);
195 id = atoll(id_buf);
196 if (id == config) {
197 closedir(evt_dir);
198 closedir(sys_dir);
199 path = zalloc(sizeof(*path));
200 path->system = malloc(MAX_EVENT_LENGTH);
201 if (!path->system) {
202 free(path);
203 return NULL;
205 path->name = malloc(MAX_EVENT_LENGTH);
206 if (!path->name) {
207 free(path->system);
208 free(path);
209 return NULL;
211 strncpy(path->system, sys_dirent.d_name,
212 MAX_EVENT_LENGTH);
213 strncpy(path->name, evt_dirent.d_name,
214 MAX_EVENT_LENGTH);
215 return path;
218 closedir(evt_dir);
221 closedir(sys_dir);
222 return NULL;
225 #define TP_PATH_LEN (MAX_EVENT_LENGTH * 2 + 1)
226 static const char *tracepoint_id_to_name(u64 config)
228 static char buf[TP_PATH_LEN];
229 struct tracepoint_path *path;
231 path = tracepoint_id_to_path(config);
232 if (path) {
233 snprintf(buf, TP_PATH_LEN, "%s:%s", path->system, path->name);
234 free(path->name);
235 free(path->system);
236 free(path);
237 } else
238 snprintf(buf, TP_PATH_LEN, "%s:%s", "unknown", "unknown");
240 return buf;
243 static int is_cache_op_valid(u8 cache_type, u8 cache_op)
245 if (hw_cache_stat[cache_type] & COP(cache_op))
246 return 1; /* valid */
247 else
248 return 0; /* invalid */
251 static char *event_cache_name(u8 cache_type, u8 cache_op, u8 cache_result)
253 static char name[50];
255 if (cache_result) {
256 sprintf(name, "%s-%s-%s", hw_cache[cache_type][0],
257 hw_cache_op[cache_op][0],
258 hw_cache_result[cache_result][0]);
259 } else {
260 sprintf(name, "%s-%s", hw_cache[cache_type][0],
261 hw_cache_op[cache_op][1]);
264 return name;
267 const char *event_type(int type)
269 switch (type) {
270 case PERF_TYPE_HARDWARE:
271 return "hardware";
273 case PERF_TYPE_SOFTWARE:
274 return "software";
276 case PERF_TYPE_TRACEPOINT:
277 return "tracepoint";
279 case PERF_TYPE_HW_CACHE:
280 return "hardware-cache";
282 default:
283 break;
286 return "unknown";
289 const char *event_name(struct perf_evsel *evsel)
291 u64 config = evsel->attr.config;
292 int type = evsel->attr.type;
294 if (evsel->name)
295 return evsel->name;
297 return __event_name(type, config);
300 const char *__event_name(int type, u64 config)
302 static char buf[32];
304 if (type == PERF_TYPE_RAW) {
305 sprintf(buf, "raw 0x%" PRIx64, config);
306 return buf;
309 switch (type) {
310 case PERF_TYPE_HARDWARE:
311 if (config < PERF_COUNT_HW_MAX)
312 return hw_event_names[config];
313 return "unknown-hardware";
315 case PERF_TYPE_HW_CACHE: {
316 u8 cache_type, cache_op, cache_result;
318 cache_type = (config >> 0) & 0xff;
319 if (cache_type > PERF_COUNT_HW_CACHE_MAX)
320 return "unknown-ext-hardware-cache-type";
322 cache_op = (config >> 8) & 0xff;
323 if (cache_op > PERF_COUNT_HW_CACHE_OP_MAX)
324 return "unknown-ext-hardware-cache-op";
326 cache_result = (config >> 16) & 0xff;
327 if (cache_result > PERF_COUNT_HW_CACHE_RESULT_MAX)
328 return "unknown-ext-hardware-cache-result";
330 if (!is_cache_op_valid(cache_type, cache_op))
331 return "invalid-cache";
333 return event_cache_name(cache_type, cache_op, cache_result);
336 case PERF_TYPE_SOFTWARE:
337 if (config < PERF_COUNT_SW_MAX)
338 return sw_event_names[config];
339 return "unknown-software";
341 case PERF_TYPE_TRACEPOINT:
342 return tracepoint_id_to_name(config);
344 default:
345 break;
348 return "unknown";
351 static int parse_aliases(const char **str, const char *names[][MAX_ALIASES], int size)
353 int i, j;
354 int n, longest = -1;
356 for (i = 0; i < size; i++) {
357 for (j = 0; j < MAX_ALIASES && names[i][j]; j++) {
358 n = strlen(names[i][j]);
359 if (n > longest && !strncasecmp(*str, names[i][j], n))
360 longest = n;
362 if (longest > 0) {
363 *str += longest;
364 return i;
368 return -1;
371 static enum event_result
372 parse_generic_hw_event(const char **str, struct perf_event_attr *attr)
374 const char *s = *str;
375 int cache_type = -1, cache_op = -1, cache_result = -1;
377 cache_type = parse_aliases(&s, hw_cache, PERF_COUNT_HW_CACHE_MAX);
379 * No fallback - if we cannot get a clear cache type
380 * then bail out:
382 if (cache_type == -1)
383 return EVT_FAILED;
385 while ((cache_op == -1 || cache_result == -1) && *s == '-') {
386 ++s;
388 if (cache_op == -1) {
389 cache_op = parse_aliases(&s, hw_cache_op,
390 PERF_COUNT_HW_CACHE_OP_MAX);
391 if (cache_op >= 0) {
392 if (!is_cache_op_valid(cache_type, cache_op))
393 return 0;
394 continue;
398 if (cache_result == -1) {
399 cache_result = parse_aliases(&s, hw_cache_result,
400 PERF_COUNT_HW_CACHE_RESULT_MAX);
401 if (cache_result >= 0)
402 continue;
406 * Can't parse this as a cache op or result, so back up
407 * to the '-'.
409 --s;
410 break;
414 * Fall back to reads:
416 if (cache_op == -1)
417 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
420 * Fall back to accesses:
422 if (cache_result == -1)
423 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
425 attr->config = cache_type | (cache_op << 8) | (cache_result << 16);
426 attr->type = PERF_TYPE_HW_CACHE;
428 *str = s;
429 return EVT_HANDLED;
432 static enum event_result
433 parse_single_tracepoint_event(char *sys_name,
434 const char *evt_name,
435 unsigned int evt_length,
436 struct perf_event_attr *attr,
437 const char **strp)
439 char evt_path[MAXPATHLEN];
440 char id_buf[4];
441 u64 id;
442 int fd;
444 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", debugfs_path,
445 sys_name, evt_name);
447 fd = open(evt_path, O_RDONLY);
448 if (fd < 0)
449 return EVT_FAILED;
451 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
452 close(fd);
453 return EVT_FAILED;
456 close(fd);
457 id = atoll(id_buf);
458 attr->config = id;
459 attr->type = PERF_TYPE_TRACEPOINT;
460 *strp += strlen(sys_name) + evt_length + 1; /* + 1 for the ':' */
462 attr->sample_type |= PERF_SAMPLE_RAW;
463 attr->sample_type |= PERF_SAMPLE_TIME;
464 attr->sample_type |= PERF_SAMPLE_CPU;
466 attr->sample_period = 1;
469 return EVT_HANDLED;
472 /* sys + ':' + event + ':' + flags*/
473 #define MAX_EVOPT_LEN (MAX_EVENT_LENGTH * 2 + 2 + 128)
474 static enum event_result
475 parse_multiple_tracepoint_event(const struct option *opt, char *sys_name,
476 const char *evt_exp, char *flags)
478 char evt_path[MAXPATHLEN];
479 struct dirent *evt_ent;
480 DIR *evt_dir;
482 snprintf(evt_path, MAXPATHLEN, "%s/%s", debugfs_path, sys_name);
483 evt_dir = opendir(evt_path);
485 if (!evt_dir) {
486 perror("Can't open event dir");
487 return EVT_FAILED;
490 while ((evt_ent = readdir(evt_dir))) {
491 char event_opt[MAX_EVOPT_LEN + 1];
492 int len;
494 if (!strcmp(evt_ent->d_name, ".")
495 || !strcmp(evt_ent->d_name, "..")
496 || !strcmp(evt_ent->d_name, "enable")
497 || !strcmp(evt_ent->d_name, "filter"))
498 continue;
500 if (!strglobmatch(evt_ent->d_name, evt_exp))
501 continue;
503 len = snprintf(event_opt, MAX_EVOPT_LEN, "%s:%s%s%s", sys_name,
504 evt_ent->d_name, flags ? ":" : "",
505 flags ?: "");
506 if (len < 0)
507 return EVT_FAILED;
509 if (parse_events(opt, event_opt, 0))
510 return EVT_FAILED;
513 return EVT_HANDLED_ALL;
516 static enum event_result
517 parse_tracepoint_event(const struct option *opt, const char **strp,
518 struct perf_event_attr *attr)
520 const char *evt_name;
521 char *flags = NULL, *comma_loc;
522 char sys_name[MAX_EVENT_LENGTH];
523 unsigned int sys_length, evt_length;
525 if (debugfs_valid_mountpoint(debugfs_path))
526 return 0;
528 evt_name = strchr(*strp, ':');
529 if (!evt_name)
530 return EVT_FAILED;
532 sys_length = evt_name - *strp;
533 if (sys_length >= MAX_EVENT_LENGTH)
534 return 0;
536 strncpy(sys_name, *strp, sys_length);
537 sys_name[sys_length] = '\0';
538 evt_name = evt_name + 1;
540 comma_loc = strchr(evt_name, ',');
541 if (comma_loc) {
542 /* take the event name up to the comma */
543 evt_name = strndup(evt_name, comma_loc - evt_name);
545 flags = strchr(evt_name, ':');
546 if (flags) {
547 /* split it out: */
548 evt_name = strndup(evt_name, flags - evt_name);
549 flags++;
552 evt_length = strlen(evt_name);
553 if (evt_length >= MAX_EVENT_LENGTH)
554 return EVT_FAILED;
555 if (strpbrk(evt_name, "*?")) {
556 *strp += strlen(sys_name) + evt_length + 1; /* 1 == the ':' */
557 return parse_multiple_tracepoint_event(opt, sys_name, evt_name,
558 flags);
559 } else {
560 return parse_single_tracepoint_event(sys_name, evt_name,
561 evt_length, attr, strp);
565 static enum event_result
566 parse_breakpoint_type(const char *type, const char **strp,
567 struct perf_event_attr *attr)
569 int i;
571 for (i = 0; i < 3; i++) {
572 if (!type[i])
573 break;
575 switch (type[i]) {
576 case 'r':
577 attr->bp_type |= HW_BREAKPOINT_R;
578 break;
579 case 'w':
580 attr->bp_type |= HW_BREAKPOINT_W;
581 break;
582 case 'x':
583 attr->bp_type |= HW_BREAKPOINT_X;
584 break;
585 default:
586 return EVT_FAILED;
589 if (!attr->bp_type) /* Default */
590 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
592 *strp = type + i;
594 return EVT_HANDLED;
597 static enum event_result
598 parse_breakpoint_event(const char **strp, struct perf_event_attr *attr)
600 const char *target;
601 const char *type;
602 char *endaddr;
603 u64 addr;
604 enum event_result err;
606 target = strchr(*strp, ':');
607 if (!target)
608 return EVT_FAILED;
610 if (strncmp(*strp, "mem", target - *strp) != 0)
611 return EVT_FAILED;
613 target++;
615 addr = strtoull(target, &endaddr, 0);
616 if (target == endaddr)
617 return EVT_FAILED;
619 attr->bp_addr = addr;
620 *strp = endaddr;
622 type = strchr(target, ':');
624 /* If no type is defined, just rw as default */
625 if (!type) {
626 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
627 } else {
628 err = parse_breakpoint_type(++type, strp, attr);
629 if (err == EVT_FAILED)
630 return EVT_FAILED;
634 * We should find a nice way to override the access length
635 * Provide some defaults for now
637 if (attr->bp_type == HW_BREAKPOINT_X)
638 attr->bp_len = sizeof(long);
639 else
640 attr->bp_len = HW_BREAKPOINT_LEN_4;
642 attr->type = PERF_TYPE_BREAKPOINT;
644 return EVT_HANDLED;
647 static int check_events(const char *str, unsigned int i)
649 int n;
651 n = strlen(event_symbols[i].symbol);
652 if (!strncasecmp(str, event_symbols[i].symbol, n))
653 return n;
655 n = strlen(event_symbols[i].alias);
656 if (n) {
657 if (!strncasecmp(str, event_symbols[i].alias, n))
658 return n;
661 return 0;
664 static enum event_result
665 parse_symbolic_event(const char **strp, struct perf_event_attr *attr)
667 const char *str = *strp;
668 unsigned int i;
669 int n;
671 for (i = 0; i < ARRAY_SIZE(event_symbols); i++) {
672 n = check_events(str, i);
673 if (n > 0) {
674 attr->type = event_symbols[i].type;
675 attr->config = event_symbols[i].config;
676 *strp = str + n;
677 return EVT_HANDLED;
680 return EVT_FAILED;
683 static enum event_result
684 parse_raw_event(const char **strp, struct perf_event_attr *attr)
686 const char *str = *strp;
687 u64 config;
688 int n;
690 if (*str != 'r')
691 return EVT_FAILED;
692 n = hex2u64(str + 1, &config);
693 if (n > 0) {
694 *strp = str + n + 1;
695 attr->type = PERF_TYPE_RAW;
696 attr->config = config;
697 return EVT_HANDLED;
699 return EVT_FAILED;
702 static enum event_result
703 parse_numeric_event(const char **strp, struct perf_event_attr *attr)
705 const char *str = *strp;
706 char *endp;
707 unsigned long type;
708 u64 config;
710 type = strtoul(str, &endp, 0);
711 if (endp > str && type < PERF_TYPE_MAX && *endp == ':') {
712 str = endp + 1;
713 config = strtoul(str, &endp, 0);
714 if (endp > str) {
715 attr->type = type;
716 attr->config = config;
717 *strp = endp;
718 return EVT_HANDLED;
721 return EVT_FAILED;
724 static int
725 parse_event_modifier(const char **strp, struct perf_event_attr *attr)
727 const char *str = *strp;
728 int exclude = 0;
729 int eu = 0, ek = 0, eh = 0, precise = 0;
731 if (!*str)
732 return 0;
734 if (*str++ != ':')
735 return -1;
737 while (*str) {
738 if (*str == 'u') {
739 if (!exclude)
740 exclude = eu = ek = eh = 1;
741 eu = 0;
742 } else if (*str == 'k') {
743 if (!exclude)
744 exclude = eu = ek = eh = 1;
745 ek = 0;
746 } else if (*str == 'h') {
747 if (!exclude)
748 exclude = eu = ek = eh = 1;
749 eh = 0;
750 } else if (*str == 'p') {
751 precise++;
752 } else
753 break;
755 ++str;
757 if (str < *strp + 2)
758 return -1;
760 *strp = str;
762 attr->exclude_user = eu;
763 attr->exclude_kernel = ek;
764 attr->exclude_hv = eh;
765 attr->precise_ip = precise;
767 return 0;
771 * Each event can have multiple symbolic names.
772 * Symbolic names are (almost) exactly matched.
774 static enum event_result
775 parse_event_symbols(const struct option *opt, const char **str,
776 struct perf_event_attr *attr)
778 enum event_result ret;
780 ret = parse_tracepoint_event(opt, str, attr);
781 if (ret != EVT_FAILED)
782 goto modifier;
784 ret = parse_raw_event(str, attr);
785 if (ret != EVT_FAILED)
786 goto modifier;
788 ret = parse_numeric_event(str, attr);
789 if (ret != EVT_FAILED)
790 goto modifier;
792 ret = parse_symbolic_event(str, attr);
793 if (ret != EVT_FAILED)
794 goto modifier;
796 ret = parse_generic_hw_event(str, attr);
797 if (ret != EVT_FAILED)
798 goto modifier;
800 ret = parse_breakpoint_event(str, attr);
801 if (ret != EVT_FAILED)
802 goto modifier;
804 fprintf(stderr, "invalid or unsupported event: '%s'\n", *str);
805 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
806 return EVT_FAILED;
808 modifier:
809 if (parse_event_modifier(str, attr) < 0) {
810 fprintf(stderr, "invalid event modifier: '%s'\n", *str);
811 fprintf(stderr, "Run 'perf list' for a list of valid events and modifiers\n");
813 return EVT_FAILED;
816 return ret;
819 int parse_events(const struct option *opt, const char *str, int unset __used)
821 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
822 struct perf_event_attr attr;
823 enum event_result ret;
824 const char *ostr;
826 for (;;) {
827 ostr = str;
828 memset(&attr, 0, sizeof(attr));
829 ret = parse_event_symbols(opt, &str, &attr);
830 if (ret == EVT_FAILED)
831 return -1;
833 if (!(*str == 0 || *str == ',' || isspace(*str)))
834 return -1;
836 if (ret != EVT_HANDLED_ALL) {
837 struct perf_evsel *evsel;
838 evsel = perf_evsel__new(&attr, evlist->nr_entries);
839 if (evsel == NULL)
840 return -1;
841 perf_evlist__add(evlist, evsel);
843 evsel->name = calloc(str - ostr + 1, 1);
844 if (!evsel->name)
845 return -1;
846 strncpy(evsel->name, ostr, str - ostr);
849 if (*str == 0)
850 break;
851 if (*str == ',')
852 ++str;
853 while (isspace(*str))
854 ++str;
857 return 0;
860 int parse_filter(const struct option *opt, const char *str,
861 int unset __used)
863 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
864 struct perf_evsel *last = NULL;
866 if (evlist->nr_entries > 0)
867 last = list_entry(evlist->entries.prev, struct perf_evsel, node);
869 if (last == NULL || last->attr.type != PERF_TYPE_TRACEPOINT) {
870 fprintf(stderr,
871 "-F option should follow a -e tracepoint option\n");
872 return -1;
875 last->filter = strdup(str);
876 if (last->filter == NULL) {
877 fprintf(stderr, "not enough memory to hold filter string\n");
878 return -1;
881 return 0;
884 static const char * const event_type_descriptors[] = {
885 "Hardware event",
886 "Software event",
887 "Tracepoint event",
888 "Hardware cache event",
889 "Raw hardware event descriptor",
890 "Hardware breakpoint",
894 * Print the events from <debugfs_mount_point>/tracing/events
897 void print_tracepoint_events(const char *subsys_glob, const char *event_glob)
899 DIR *sys_dir, *evt_dir;
900 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
901 char evt_path[MAXPATHLEN];
902 char dir_path[MAXPATHLEN];
904 if (debugfs_valid_mountpoint(debugfs_path))
905 return;
907 sys_dir = opendir(debugfs_path);
908 if (!sys_dir)
909 return;
911 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
912 if (subsys_glob != NULL &&
913 !strglobmatch(sys_dirent.d_name, subsys_glob))
914 continue;
916 snprintf(dir_path, MAXPATHLEN, "%s/%s", debugfs_path,
917 sys_dirent.d_name);
918 evt_dir = opendir(dir_path);
919 if (!evt_dir)
920 continue;
922 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
923 if (event_glob != NULL &&
924 !strglobmatch(evt_dirent.d_name, event_glob))
925 continue;
927 snprintf(evt_path, MAXPATHLEN, "%s:%s",
928 sys_dirent.d_name, evt_dirent.d_name);
929 printf(" %-42s [%s]\n", evt_path,
930 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
932 closedir(evt_dir);
934 closedir(sys_dir);
938 * Check whether event is in <debugfs_mount_point>/tracing/events
941 int is_valid_tracepoint(const char *event_string)
943 DIR *sys_dir, *evt_dir;
944 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
945 char evt_path[MAXPATHLEN];
946 char dir_path[MAXPATHLEN];
948 if (debugfs_valid_mountpoint(debugfs_path))
949 return 0;
951 sys_dir = opendir(debugfs_path);
952 if (!sys_dir)
953 return 0;
955 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
957 snprintf(dir_path, MAXPATHLEN, "%s/%s", debugfs_path,
958 sys_dirent.d_name);
959 evt_dir = opendir(dir_path);
960 if (!evt_dir)
961 continue;
963 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
964 snprintf(evt_path, MAXPATHLEN, "%s:%s",
965 sys_dirent.d_name, evt_dirent.d_name);
966 if (!strcmp(evt_path, event_string)) {
967 closedir(evt_dir);
968 closedir(sys_dir);
969 return 1;
972 closedir(evt_dir);
974 closedir(sys_dir);
975 return 0;
978 void print_events_type(u8 type)
980 struct event_symbol *syms = event_symbols;
981 unsigned int i;
982 char name[64];
984 for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
985 if (type != syms->type)
986 continue;
988 if (strlen(syms->alias))
989 snprintf(name, sizeof(name), "%s OR %s",
990 syms->symbol, syms->alias);
991 else
992 snprintf(name, sizeof(name), "%s", syms->symbol);
994 printf(" %-42s [%s]\n", name,
995 event_type_descriptors[type]);
999 int print_hwcache_events(const char *event_glob)
1001 unsigned int type, op, i, printed = 0;
1003 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
1004 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
1005 /* skip invalid cache type */
1006 if (!is_cache_op_valid(type, op))
1007 continue;
1009 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
1010 char *name = event_cache_name(type, op, i);
1012 if (event_glob != NULL &&
1013 !strglobmatch(name, event_glob))
1014 continue;
1016 printf(" %-42s [%s]\n", name,
1017 event_type_descriptors[PERF_TYPE_HW_CACHE]);
1018 ++printed;
1023 return printed;
1027 * Print the help text for the event symbols:
1029 void print_events(const char *event_glob)
1031 struct event_symbol *syms = event_symbols;
1032 unsigned int i, type, prev_type = -1, printed = 0, ntypes_printed = 0;
1033 char name[40];
1035 printf("\n");
1036 printf("List of pre-defined events (to be used in -e):\n");
1038 for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
1039 type = syms->type;
1041 if (type != prev_type && printed) {
1042 printf("\n");
1043 printed = 0;
1044 ntypes_printed++;
1047 if (event_glob != NULL &&
1048 !(strglobmatch(syms->symbol, event_glob) ||
1049 (syms->alias && strglobmatch(syms->alias, event_glob))))
1050 continue;
1052 if (strlen(syms->alias))
1053 sprintf(name, "%s OR %s", syms->symbol, syms->alias);
1054 else
1055 strcpy(name, syms->symbol);
1056 printf(" %-42s [%s]\n", name,
1057 event_type_descriptors[type]);
1059 prev_type = type;
1060 ++printed;
1063 if (ntypes_printed) {
1064 printed = 0;
1065 printf("\n");
1067 print_hwcache_events(event_glob);
1069 if (event_glob != NULL)
1070 return;
1072 printf("\n");
1073 printf(" %-42s [%s]\n",
1074 "rNNN (see 'perf list --help' on how to encode it)",
1075 event_type_descriptors[PERF_TYPE_RAW]);
1076 printf("\n");
1078 printf(" %-42s [%s]\n",
1079 "mem:<addr>[:access]",
1080 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
1081 printf("\n");
1083 print_tracepoint_events(NULL, NULL);
1085 exit(129);