perf events: Add stalled cycles generic event - PERF_COUNT_HW_STALLED_CYCLES
[linux-2.6.git] / tools / perf / util / parse-events.c
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"
14
15 struct event_symbol {
16         u8              type;
17         u64             config;
18         const char      *symbol;
19         const char      *alias;
20 };
21
22 enum event_result {
23         EVT_FAILED,
24         EVT_HANDLED,
25         EVT_HANDLED_ALL
26 };
27
28 char debugfs_path[MAXPATHLEN];
29
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
32
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",       ""              },
42
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",     ""              },
52 };
53
54 #define __PERF_EVENT_FIELD(config, name) \
55         ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
56
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)
61
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",
70 };
71
72 static const char *sw_event_names[] = {
73         "cpu-clock-msecs",
74         "task-clock-msecs",
75         "page-faults",
76         "context-switches",
77         "CPU-migrations",
78         "minor-faults",
79         "major-faults",
80         "alignment-faults",
81         "emulation-faults",
82 };
83
84 #define MAX_ALIASES 8
85
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",  },
93 };
94
95 static const char *hw_cache_op[][MAX_ALIASES] = {
96  { "load",      "loads",        "read",                                 },
97  { "store",     "stores",       "write",                                },
98  { "prefetch",  "prefetches",   "speculative-read", "speculative-load", },
99 };
100
101 static const char *hw_cache_result[][MAX_ALIASES] = {
102  { "refs",      "Reference",    "ops",          "access",               },
103  { "misses",    "miss",                                                 },
104 };
105
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)
111
112 /*
113  * cache operartion stat
114  * L1I : Read and prefetch only
115  * ITLB and BPU : Read-only
116  */
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),
124 };
125
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, "..")))
131
132 static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
133 {
134         char evt_path[MAXPATHLEN];
135         int fd;
136
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);
143
144         return 0;
145 }
146
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)))
153
154 #define MAX_EVENT_LENGTH 512
155
156
157 struct tracepoint_path *tracepoint_id_to_path(u64 config)
158 {
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];
167
168         if (debugfs_valid_mountpoint(debugfs_path))
169                 return NULL;
170
171         sys_dir = opendir(debugfs_path);
172         if (!sys_dir)
173                 return NULL;
174
175         for_each_subsystem(sys_dir, sys_dirent, sys_next) {
176
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;
182
183                 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
184
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;
193                         }
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;
204                                 }
205                                 path->name = malloc(MAX_EVENT_LENGTH);
206                                 if (!path->name) {
207                                         free(path->system);
208                                         free(path);
209                                         return NULL;
210                                 }
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;
216                         }
217                 }
218                 closedir(evt_dir);
219         }
220
221         closedir(sys_dir);
222         return NULL;
223 }
224
225 #define TP_PATH_LEN (MAX_EVENT_LENGTH * 2 + 1)
226 static const char *tracepoint_id_to_name(u64 config)
227 {
228         static char buf[TP_PATH_LEN];
229         struct tracepoint_path *path;
230
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");
239
240         return buf;
241 }
242
243 static int is_cache_op_valid(u8 cache_type, u8 cache_op)
244 {
245         if (hw_cache_stat[cache_type] & COP(cache_op))
246                 return 1;       /* valid */
247         else
248                 return 0;       /* invalid */
249 }
250
251 static char *event_cache_name(u8 cache_type, u8 cache_op, u8 cache_result)
252 {
253         static char name[50];
254
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]);
262         }
263
264         return name;
265 }
266
267 const char *event_type(int type)
268 {
269         switch (type) {
270         case PERF_TYPE_HARDWARE:
271                 return "hardware";
272
273         case PERF_TYPE_SOFTWARE:
274                 return "software";
275
276         case PERF_TYPE_TRACEPOINT:
277                 return "tracepoint";
278
279         case PERF_TYPE_HW_CACHE:
280                 return "hardware-cache";
281
282         default:
283                 break;
284         }
285
286         return "unknown";
287 }
288
289 const char *event_name(struct perf_evsel *evsel)
290 {
291         u64 config = evsel->attr.config;
292         int type = evsel->attr.type;
293
294         if (evsel->name)
295                 return evsel->name;
296
297         return __event_name(type, config);
298 }
299
300 const char *__event_name(int type, u64 config)
301 {
302         static char buf[32];
303
304         if (type == PERF_TYPE_RAW) {
305                 sprintf(buf, "raw 0x%" PRIx64, config);
306                 return buf;
307         }
308
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";
314
315         case PERF_TYPE_HW_CACHE: {
316                 u8 cache_type, cache_op, cache_result;
317
318                 cache_type   = (config >>  0) & 0xff;
319                 if (cache_type > PERF_COUNT_HW_CACHE_MAX)
320                         return "unknown-ext-hardware-cache-type";
321
322                 cache_op     = (config >>  8) & 0xff;
323                 if (cache_op > PERF_COUNT_HW_CACHE_OP_MAX)
324                         return "unknown-ext-hardware-cache-op";
325
326                 cache_result = (config >> 16) & 0xff;
327                 if (cache_result > PERF_COUNT_HW_CACHE_RESULT_MAX)
328                         return "unknown-ext-hardware-cache-result";
329
330                 if (!is_cache_op_valid(cache_type, cache_op))
331                         return "invalid-cache";
332
333                 return event_cache_name(cache_type, cache_op, cache_result);
334         }
335
336         case PERF_TYPE_SOFTWARE:
337                 if (config < PERF_COUNT_SW_MAX)
338                         return sw_event_names[config];
339                 return "unknown-software";
340
341         case PERF_TYPE_TRACEPOINT:
342                 return tracepoint_id_to_name(config);
343
344         default:
345                 break;
346         }
347
348         return "unknown";
349 }
350
351 static int parse_aliases(const char **str, const char *names[][MAX_ALIASES], int size)
352 {
353         int i, j;
354         int n, longest = -1;
355
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;
361                 }
362                 if (longest > 0) {
363                         *str += longest;
364                         return i;
365                 }
366         }
367
368         return -1;
369 }
370
371 static enum event_result
372 parse_generic_hw_event(const char **str, struct perf_event_attr *attr)
373 {
374         const char *s = *str;
375         int cache_type = -1, cache_op = -1, cache_result = -1;
376
377         cache_type = parse_aliases(&s, hw_cache, PERF_COUNT_HW_CACHE_MAX);
378         /*
379          * No fallback - if we cannot get a clear cache type
380          * then bail out:
381          */
382         if (cache_type == -1)
383                 return EVT_FAILED;
384
385         while ((cache_op == -1 || cache_result == -1) && *s == '-') {
386                 ++s;
387
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;
395                         }
396                 }
397
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;
403                 }
404
405                 /*
406                  * Can't parse this as a cache op or result, so back up
407                  * to the '-'.
408                  */
409                 --s;
410                 break;
411         }
412
413         /*
414          * Fall back to reads:
415          */
416         if (cache_op == -1)
417                 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
418
419         /*
420          * Fall back to accesses:
421          */
422         if (cache_result == -1)
423                 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
424
425         attr->config = cache_type | (cache_op << 8) | (cache_result << 16);
426         attr->type = PERF_TYPE_HW_CACHE;
427
428         *str = s;
429         return EVT_HANDLED;
430 }
431
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)
438 {
439         char evt_path[MAXPATHLEN];
440         char id_buf[4];
441         u64 id;
442         int fd;
443
444         snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", debugfs_path,
445                  sys_name, evt_name);
446
447         fd = open(evt_path, O_RDONLY);
448         if (fd < 0)
449                 return EVT_FAILED;
450
451         if (read(fd, id_buf, sizeof(id_buf)) < 0) {
452                 close(fd);
453                 return EVT_FAILED;
454         }
455
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 ':' */
461
462         attr->sample_type |= PERF_SAMPLE_RAW;
463         attr->sample_type |= PERF_SAMPLE_TIME;
464         attr->sample_type |= PERF_SAMPLE_CPU;
465
466         attr->sample_period = 1;
467
468
469         return EVT_HANDLED;
470 }
471
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)
477 {
478         char evt_path[MAXPATHLEN];
479         struct dirent *evt_ent;
480         DIR *evt_dir;
481
482         snprintf(evt_path, MAXPATHLEN, "%s/%s", debugfs_path, sys_name);
483         evt_dir = opendir(evt_path);
484
485         if (!evt_dir) {
486                 perror("Can't open event dir");
487                 return EVT_FAILED;
488         }
489
490         while ((evt_ent = readdir(evt_dir))) {
491                 char event_opt[MAX_EVOPT_LEN + 1];
492                 int len;
493
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;
499
500                 if (!strglobmatch(evt_ent->d_name, evt_exp))
501                         continue;
502
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;
508
509                 if (parse_events(opt, event_opt, 0))
510                         return EVT_FAILED;
511         }
512
513         return EVT_HANDLED_ALL;
514 }
515
516 static enum event_result
517 parse_tracepoint_event(const struct option *opt, const char **strp,
518                        struct perf_event_attr *attr)
519 {
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;
524
525         if (debugfs_valid_mountpoint(debugfs_path))
526                 return 0;
527
528         evt_name = strchr(*strp, ':');
529         if (!evt_name)
530                 return EVT_FAILED;
531
532         sys_length = evt_name - *strp;
533         if (sys_length >= MAX_EVENT_LENGTH)
534                 return 0;
535
536         strncpy(sys_name, *strp, sys_length);
537         sys_name[sys_length] = '\0';
538         evt_name = evt_name + 1;
539
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);
544         }
545         flags = strchr(evt_name, ':');
546         if (flags) {
547                 /* split it out: */
548                 evt_name = strndup(evt_name, flags - evt_name);
549                 flags++;
550         }
551
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);
562         }
563 }
564
565 static enum event_result
566 parse_breakpoint_type(const char *type, const char **strp,
567                       struct perf_event_attr *attr)
568 {
569         int i;
570
571         for (i = 0; i < 3; i++) {
572                 if (!type[i])
573                         break;
574
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;
587                 }
588         }
589         if (!attr->bp_type) /* Default */
590                 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
591
592         *strp = type + i;
593
594         return EVT_HANDLED;
595 }
596
597 static enum event_result
598 parse_breakpoint_event(const char **strp, struct perf_event_attr *attr)
599 {
600         const char *target;
601         const char *type;
602         char *endaddr;
603         u64 addr;
604         enum event_result err;
605
606         target = strchr(*strp, ':');
607         if (!target)
608                 return EVT_FAILED;
609
610         if (strncmp(*strp, "mem", target - *strp) != 0)
611                 return EVT_FAILED;
612
613         target++;
614
615         addr = strtoull(target, &endaddr, 0);
616         if (target == endaddr)
617                 return EVT_FAILED;
618
619         attr->bp_addr = addr;
620         *strp = endaddr;
621
622         type = strchr(target, ':');
623
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;
631         }
632
633         /*
634          * We should find a nice way to override the access length
635          * Provide some defaults for now
636          */
637         if (attr->bp_type == HW_BREAKPOINT_X)
638                 attr->bp_len = sizeof(long);
639         else
640                 attr->bp_len = HW_BREAKPOINT_LEN_4;
641
642         attr->type = PERF_TYPE_BREAKPOINT;
643
644         return EVT_HANDLED;
645 }
646
647 static int check_events(const char *str, unsigned int i)
648 {
649         int n;
650
651         n = strlen(event_symbols[i].symbol);
652         if (!strncmp(str, event_symbols[i].symbol, n))
653                 return n;
654
655         n = strlen(event_symbols[i].alias);
656         if (n)
657                 if (!strncmp(str, event_symbols[i].alias, n))
658                         return n;
659         return 0;
660 }
661
662 static enum event_result
663 parse_symbolic_event(const char **strp, struct perf_event_attr *attr)
664 {
665         const char *str = *strp;
666         unsigned int i;
667         int n;
668
669         for (i = 0; i < ARRAY_SIZE(event_symbols); i++) {
670                 n = check_events(str, i);
671                 if (n > 0) {
672                         attr->type = event_symbols[i].type;
673                         attr->config = event_symbols[i].config;
674                         *strp = str + n;
675                         return EVT_HANDLED;
676                 }
677         }
678         return EVT_FAILED;
679 }
680
681 static enum event_result
682 parse_raw_event(const char **strp, struct perf_event_attr *attr)
683 {
684         const char *str = *strp;
685         u64 config;
686         int n;
687
688         if (*str != 'r')
689                 return EVT_FAILED;
690         n = hex2u64(str + 1, &config);
691         if (n > 0) {
692                 *strp = str + n + 1;
693                 attr->type = PERF_TYPE_RAW;
694                 attr->config = config;
695                 return EVT_HANDLED;
696         }
697         return EVT_FAILED;
698 }
699
700 static enum event_result
701 parse_numeric_event(const char **strp, struct perf_event_attr *attr)
702 {
703         const char *str = *strp;
704         char *endp;
705         unsigned long type;
706         u64 config;
707
708         type = strtoul(str, &endp, 0);
709         if (endp > str && type < PERF_TYPE_MAX && *endp == ':') {
710                 str = endp + 1;
711                 config = strtoul(str, &endp, 0);
712                 if (endp > str) {
713                         attr->type = type;
714                         attr->config = config;
715                         *strp = endp;
716                         return EVT_HANDLED;
717                 }
718         }
719         return EVT_FAILED;
720 }
721
722 static enum event_result
723 parse_event_modifier(const char **strp, struct perf_event_attr *attr)
724 {
725         const char *str = *strp;
726         int exclude = 0;
727         int eu = 0, ek = 0, eh = 0, precise = 0;
728
729         if (*str++ != ':')
730                 return 0;
731         while (*str) {
732                 if (*str == 'u') {
733                         if (!exclude)
734                                 exclude = eu = ek = eh = 1;
735                         eu = 0;
736                 } else if (*str == 'k') {
737                         if (!exclude)
738                                 exclude = eu = ek = eh = 1;
739                         ek = 0;
740                 } else if (*str == 'h') {
741                         if (!exclude)
742                                 exclude = eu = ek = eh = 1;
743                         eh = 0;
744                 } else if (*str == 'p') {
745                         precise++;
746                 } else
747                         break;
748
749                 ++str;
750         }
751         if (str >= *strp + 2) {
752                 *strp = str;
753                 attr->exclude_user   = eu;
754                 attr->exclude_kernel = ek;
755                 attr->exclude_hv     = eh;
756                 attr->precise_ip     = precise;
757                 return 1;
758         }
759         return 0;
760 }
761
762 /*
763  * Each event can have multiple symbolic names.
764  * Symbolic names are (almost) exactly matched.
765  */
766 static enum event_result
767 parse_event_symbols(const struct option *opt, const char **str,
768                     struct perf_event_attr *attr)
769 {
770         enum event_result ret;
771
772         ret = parse_tracepoint_event(opt, str, attr);
773         if (ret != EVT_FAILED)
774                 goto modifier;
775
776         ret = parse_raw_event(str, attr);
777         if (ret != EVT_FAILED)
778                 goto modifier;
779
780         ret = parse_numeric_event(str, attr);
781         if (ret != EVT_FAILED)
782                 goto modifier;
783
784         ret = parse_symbolic_event(str, attr);
785         if (ret != EVT_FAILED)
786                 goto modifier;
787
788         ret = parse_generic_hw_event(str, attr);
789         if (ret != EVT_FAILED)
790                 goto modifier;
791
792         ret = parse_breakpoint_event(str, attr);
793         if (ret != EVT_FAILED)
794                 goto modifier;
795
796         fprintf(stderr, "invalid or unsupported event: '%s'\n", *str);
797         fprintf(stderr, "Run 'perf list' for a list of valid events\n");
798         return EVT_FAILED;
799
800 modifier:
801         parse_event_modifier(str, attr);
802
803         return ret;
804 }
805
806 int parse_events(const struct option *opt, const char *str, int unset __used)
807 {
808         struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
809         struct perf_event_attr attr;
810         enum event_result ret;
811         const char *ostr;
812
813         for (;;) {
814                 ostr = str;
815                 memset(&attr, 0, sizeof(attr));
816                 ret = parse_event_symbols(opt, &str, &attr);
817                 if (ret == EVT_FAILED)
818                         return -1;
819
820                 if (!(*str == 0 || *str == ',' || isspace(*str)))
821                         return -1;
822
823                 if (ret != EVT_HANDLED_ALL) {
824                         struct perf_evsel *evsel;
825                         evsel = perf_evsel__new(&attr, evlist->nr_entries);
826                         if (evsel == NULL)
827                                 return -1;
828                         perf_evlist__add(evlist, evsel);
829
830                         evsel->name = calloc(str - ostr + 1, 1);
831                         if (!evsel->name)
832                                 return -1;
833                         strncpy(evsel->name, ostr, str - ostr);
834                 }
835
836                 if (*str == 0)
837                         break;
838                 if (*str == ',')
839                         ++str;
840                 while (isspace(*str))
841                         ++str;
842         }
843
844         return 0;
845 }
846
847 int parse_filter(const struct option *opt, const char *str,
848                  int unset __used)
849 {
850         struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
851         struct perf_evsel *last = NULL;
852
853         if (evlist->nr_entries > 0)
854                 last = list_entry(evlist->entries.prev, struct perf_evsel, node);
855
856         if (last == NULL || last->attr.type != PERF_TYPE_TRACEPOINT) {
857                 fprintf(stderr,
858                         "-F option should follow a -e tracepoint option\n");
859                 return -1;
860         }
861
862         last->filter = strdup(str);
863         if (last->filter == NULL) {
864                 fprintf(stderr, "not enough memory to hold filter string\n");
865                 return -1;
866         }
867
868         return 0;
869 }
870
871 static const char * const event_type_descriptors[] = {
872         "Hardware event",
873         "Software event",
874         "Tracepoint event",
875         "Hardware cache event",
876         "Raw hardware event descriptor",
877         "Hardware breakpoint",
878 };
879
880 /*
881  * Print the events from <debugfs_mount_point>/tracing/events
882  */
883
884 void print_tracepoint_events(const char *subsys_glob, const char *event_glob)
885 {
886         DIR *sys_dir, *evt_dir;
887         struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
888         char evt_path[MAXPATHLEN];
889         char dir_path[MAXPATHLEN];
890
891         if (debugfs_valid_mountpoint(debugfs_path))
892                 return;
893
894         sys_dir = opendir(debugfs_path);
895         if (!sys_dir)
896                 return;
897
898         for_each_subsystem(sys_dir, sys_dirent, sys_next) {
899                 if (subsys_glob != NULL && 
900                     !strglobmatch(sys_dirent.d_name, subsys_glob))
901                         continue;
902
903                 snprintf(dir_path, MAXPATHLEN, "%s/%s", debugfs_path,
904                          sys_dirent.d_name);
905                 evt_dir = opendir(dir_path);
906                 if (!evt_dir)
907                         continue;
908
909                 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
910                         if (event_glob != NULL && 
911                             !strglobmatch(evt_dirent.d_name, event_glob))
912                                 continue;
913
914                         snprintf(evt_path, MAXPATHLEN, "%s:%s",
915                                  sys_dirent.d_name, evt_dirent.d_name);
916                         printf("  %-42s [%s]\n", evt_path,
917                                 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
918                 }
919                 closedir(evt_dir);
920         }
921         closedir(sys_dir);
922 }
923
924 /*
925  * Check whether event is in <debugfs_mount_point>/tracing/events
926  */
927
928 int is_valid_tracepoint(const char *event_string)
929 {
930         DIR *sys_dir, *evt_dir;
931         struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
932         char evt_path[MAXPATHLEN];
933         char dir_path[MAXPATHLEN];
934
935         if (debugfs_valid_mountpoint(debugfs_path))
936                 return 0;
937
938         sys_dir = opendir(debugfs_path);
939         if (!sys_dir)
940                 return 0;
941
942         for_each_subsystem(sys_dir, sys_dirent, sys_next) {
943
944                 snprintf(dir_path, MAXPATHLEN, "%s/%s", debugfs_path,
945                          sys_dirent.d_name);
946                 evt_dir = opendir(dir_path);
947                 if (!evt_dir)
948                         continue;
949
950                 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
951                         snprintf(evt_path, MAXPATHLEN, "%s:%s",
952                                  sys_dirent.d_name, evt_dirent.d_name);
953                         if (!strcmp(evt_path, event_string)) {
954                                 closedir(evt_dir);
955                                 closedir(sys_dir);
956                                 return 1;
957                         }
958                 }
959                 closedir(evt_dir);
960         }
961         closedir(sys_dir);
962         return 0;
963 }
964
965 void print_events_type(u8 type)
966 {
967         struct event_symbol *syms = event_symbols;
968         unsigned int i;
969         char name[64];
970
971         for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
972                 if (type != syms->type)
973                         continue;
974
975                 if (strlen(syms->alias))
976                         snprintf(name, sizeof(name),  "%s OR %s",
977                                  syms->symbol, syms->alias);
978                 else
979                         snprintf(name, sizeof(name), "%s", syms->symbol);
980
981                 printf("  %-42s [%s]\n", name,
982                         event_type_descriptors[type]);
983         }
984 }
985
986 int print_hwcache_events(const char *event_glob)
987 {
988         unsigned int type, op, i, printed = 0;
989
990         for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
991                 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
992                         /* skip invalid cache type */
993                         if (!is_cache_op_valid(type, op))
994                                 continue;
995
996                         for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
997                                 char *name = event_cache_name(type, op, i);
998
999                                 if (event_glob != NULL && 
1000                                     !strglobmatch(name, event_glob))
1001                                         continue;
1002
1003                                 printf("  %-42s [%s]\n", name,
1004                                         event_type_descriptors[PERF_TYPE_HW_CACHE]);
1005                                 ++printed;
1006                         }
1007                 }
1008         }
1009
1010         return printed;
1011 }
1012
1013 /*
1014  * Print the help text for the event symbols:
1015  */
1016 void print_events(const char *event_glob)
1017 {
1018         struct event_symbol *syms = event_symbols;
1019         unsigned int i, type, prev_type = -1, printed = 0, ntypes_printed = 0;
1020         char name[40];
1021
1022         printf("\n");
1023         printf("List of pre-defined events (to be used in -e):\n");
1024
1025         for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
1026                 type = syms->type;
1027
1028                 if (type != prev_type && printed) {
1029                         printf("\n");
1030                         printed = 0;
1031                         ntypes_printed++;
1032                 }
1033
1034                 if (event_glob != NULL && 
1035                     !(strglobmatch(syms->symbol, event_glob) ||
1036                       (syms->alias && strglobmatch(syms->alias, event_glob))))
1037                         continue;
1038
1039                 if (strlen(syms->alias))
1040                         sprintf(name, "%s OR %s", syms->symbol, syms->alias);
1041                 else
1042                         strcpy(name, syms->symbol);
1043                 printf("  %-42s [%s]\n", name,
1044                         event_type_descriptors[type]);
1045
1046                 prev_type = type;
1047                 ++printed;
1048         }
1049
1050         if (ntypes_printed) {
1051                 printed = 0;
1052                 printf("\n");
1053         }
1054         print_hwcache_events(event_glob);
1055
1056         if (event_glob != NULL)
1057                 return;
1058
1059         printf("\n");
1060         printf("  %-42s [%s]\n",
1061                 "rNNN (see 'perf list --help' on how to encode it)",
1062                event_type_descriptors[PERF_TYPE_RAW]);
1063         printf("\n");
1064
1065         printf("  %-42s [%s]\n",
1066                         "mem:<addr>[:access]",
1067                         event_type_descriptors[PERF_TYPE_BREAKPOINT]);
1068         printf("\n");
1069
1070         print_tracepoint_events(NULL, NULL);
1071
1072         exit(129);
1073 }