Merge branch 'tracing-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[pandora-kernel.git] / tools / perf / util / parse-events.c
1
2 #include "util.h"
3 #include "../perf.h"
4 #include "parse-options.h"
5 #include "parse-events.h"
6 #include "exec_cmd.h"
7 #include "string.h"
8 #include "cache.h"
9
10 int                                     nr_counters;
11
12 struct perf_counter_attr                attrs[MAX_COUNTERS];
13
14 struct event_symbol {
15         u8              type;
16         u64             config;
17         const char      *symbol;
18         const char      *alias;
19 };
20
21 char debugfs_path[MAXPATHLEN];
22
23 #define CHW(x) .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_##x
24 #define CSW(x) .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_##x
25
26 static struct event_symbol event_symbols[] = {
27   { CHW(CPU_CYCLES),            "cpu-cycles",           "cycles"        },
28   { CHW(INSTRUCTIONS),          "instructions",         ""              },
29   { CHW(CACHE_REFERENCES),      "cache-references",     ""              },
30   { CHW(CACHE_MISSES),          "cache-misses",         ""              },
31   { CHW(BRANCH_INSTRUCTIONS),   "branch-instructions",  "branches"      },
32   { CHW(BRANCH_MISSES),         "branch-misses",        ""              },
33   { CHW(BUS_CYCLES),            "bus-cycles",           ""              },
34
35   { CSW(CPU_CLOCK),             "cpu-clock",            ""              },
36   { CSW(TASK_CLOCK),            "task-clock",           ""              },
37   { CSW(PAGE_FAULTS),           "page-faults",          "faults"        },
38   { CSW(PAGE_FAULTS_MIN),       "minor-faults",         ""              },
39   { CSW(PAGE_FAULTS_MAJ),       "major-faults",         ""              },
40   { CSW(CONTEXT_SWITCHES),      "context-switches",     "cs"            },
41   { CSW(CPU_MIGRATIONS),        "cpu-migrations",       "migrations"    },
42 };
43
44 #define __PERF_COUNTER_FIELD(config, name) \
45         ((config & PERF_COUNTER_##name##_MASK) >> PERF_COUNTER_##name##_SHIFT)
46
47 #define PERF_COUNTER_RAW(config)        __PERF_COUNTER_FIELD(config, RAW)
48 #define PERF_COUNTER_CONFIG(config)     __PERF_COUNTER_FIELD(config, CONFIG)
49 #define PERF_COUNTER_TYPE(config)       __PERF_COUNTER_FIELD(config, TYPE)
50 #define PERF_COUNTER_ID(config)         __PERF_COUNTER_FIELD(config, EVENT)
51
52 static const char *hw_event_names[] = {
53         "cycles",
54         "instructions",
55         "cache-references",
56         "cache-misses",
57         "branches",
58         "branch-misses",
59         "bus-cycles",
60 };
61
62 static const char *sw_event_names[] = {
63         "cpu-clock-msecs",
64         "task-clock-msecs",
65         "page-faults",
66         "context-switches",
67         "CPU-migrations",
68         "minor-faults",
69         "major-faults",
70 };
71
72 #define MAX_ALIASES 8
73
74 static const char *hw_cache[][MAX_ALIASES] = {
75  { "L1-dcache", "l1-d",         "l1d",          "L1-data",              },
76  { "L1-icache", "l1-i",         "l1i",          "L1-instruction",       },
77  { "LLC",       "L2"                                                    },
78  { "dTLB",      "d-tlb",        "Data-TLB",                             },
79  { "iTLB",      "i-tlb",        "Instruction-TLB",                      },
80  { "branch",    "branches",     "bpu",          "btb",          "bpc",  },
81 };
82
83 static const char *hw_cache_op[][MAX_ALIASES] = {
84  { "load",      "loads",        "read",                                 },
85  { "store",     "stores",       "write",                                },
86  { "prefetch",  "prefetches",   "speculative-read", "speculative-load", },
87 };
88
89 static const char *hw_cache_result[][MAX_ALIASES] = {
90  { "refs",      "Reference",    "ops",          "access",               },
91  { "misses",    "miss",                                                 },
92 };
93
94 #define C(x)            PERF_COUNT_HW_CACHE_##x
95 #define CACHE_READ      (1 << C(OP_READ))
96 #define CACHE_WRITE     (1 << C(OP_WRITE))
97 #define CACHE_PREFETCH  (1 << C(OP_PREFETCH))
98 #define COP(x)          (1 << x)
99
100 /*
101  * cache operartion stat
102  * L1I : Read and prefetch only
103  * ITLB and BPU : Read-only
104  */
105 static unsigned long hw_cache_stat[C(MAX)] = {
106  [C(L1D)]       = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
107  [C(L1I)]       = (CACHE_READ | CACHE_PREFETCH),
108  [C(LL)]        = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
109  [C(DTLB)]      = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
110  [C(ITLB)]      = (CACHE_READ),
111  [C(BPU)]       = (CACHE_READ),
112 };
113
114 #define for_each_subsystem(sys_dir, sys_dirent, sys_next)              \
115         while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next)        \
116         if (sys_dirent.d_type == DT_DIR &&                                     \
117            (strcmp(sys_dirent.d_name, ".")) &&                                 \
118            (strcmp(sys_dirent.d_name, "..")))
119
120 static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
121 {
122         char evt_path[MAXPATHLEN];
123         int fd;
124
125         snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", debugfs_path,
126                         sys_dir->d_name, evt_dir->d_name);
127         fd = open(evt_path, O_RDONLY);
128         if (fd < 0)
129                 return -EINVAL;
130         close(fd);
131
132         return 0;
133 }
134
135 #define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next)              \
136         while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next)        \
137         if (evt_dirent.d_type == DT_DIR &&                                     \
138            (strcmp(evt_dirent.d_name, ".")) &&                                 \
139            (strcmp(evt_dirent.d_name, "..")) &&                                \
140            (!tp_event_has_id(&sys_dirent, &evt_dirent)))
141
142 #define MAX_EVENT_LENGTH 30
143
144 int valid_debugfs_mount(const char *debugfs)
145 {
146         struct statfs st_fs;
147
148         if (statfs(debugfs, &st_fs) < 0)
149                 return -ENOENT;
150         else if (st_fs.f_type != (long) DEBUGFS_MAGIC)
151                 return -ENOENT;
152         return 0;
153 }
154
155 struct tracepoint_path *tracepoint_id_to_path(u64 config)
156 {
157         struct tracepoint_path *path = NULL;
158         DIR *sys_dir, *evt_dir;
159         struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
160         char id_buf[4];
161         int sys_dir_fd, fd;
162         u64 id;
163         char evt_path[MAXPATHLEN];
164
165         if (valid_debugfs_mount(debugfs_path))
166                 return NULL;
167
168         sys_dir = opendir(debugfs_path);
169         if (!sys_dir)
170                 goto cleanup;
171         sys_dir_fd = dirfd(sys_dir);
172
173         for_each_subsystem(sys_dir, sys_dirent, sys_next) {
174                 int dfd = openat(sys_dir_fd, sys_dirent.d_name,
175                                  O_RDONLY|O_DIRECTORY), evt_dir_fd;
176                 if (dfd == -1)
177                         continue;
178                 evt_dir = fdopendir(dfd);
179                 if (!evt_dir) {
180                         close(dfd);
181                         continue;
182                 }
183                 evt_dir_fd = dirfd(evt_dir);
184                 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
185                         snprintf(evt_path, MAXPATHLEN, "%s/id",
186                                  evt_dirent.d_name);
187                         fd = openat(evt_dir_fd, 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 = calloc(1, 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 cleanup:
222         closedir(sys_dir);
223         return NULL;
224 }
225
226 #define TP_PATH_LEN (MAX_EVENT_LENGTH * 2 + 1)
227 static const char *tracepoint_id_to_name(u64 config)
228 {
229         static char buf[TP_PATH_LEN];
230         struct tracepoint_path *path;
231
232         path = tracepoint_id_to_path(config);
233         if (path) {
234                 snprintf(buf, TP_PATH_LEN, "%s:%s", path->system, path->name);
235                 free(path->name);
236                 free(path->system);
237                 free(path);
238         } else
239                 snprintf(buf, TP_PATH_LEN, "%s:%s", "unknown", "unknown");
240
241         return buf;
242 }
243
244 static int is_cache_op_valid(u8 cache_type, u8 cache_op)
245 {
246         if (hw_cache_stat[cache_type] & COP(cache_op))
247                 return 1;       /* valid */
248         else
249                 return 0;       /* invalid */
250 }
251
252 static char *event_cache_name(u8 cache_type, u8 cache_op, u8 cache_result)
253 {
254         static char name[50];
255
256         if (cache_result) {
257                 sprintf(name, "%s-%s-%s", hw_cache[cache_type][0],
258                         hw_cache_op[cache_op][0],
259                         hw_cache_result[cache_result][0]);
260         } else {
261                 sprintf(name, "%s-%s", hw_cache[cache_type][0],
262                         hw_cache_op[cache_op][1]);
263         }
264
265         return name;
266 }
267
268 const char *event_name(int counter)
269 {
270         u64 config = attrs[counter].config;
271         int type = attrs[counter].type;
272
273         return __event_name(type, config);
274 }
275
276 const char *__event_name(int type, u64 config)
277 {
278         static char buf[32];
279
280         if (type == PERF_TYPE_RAW) {
281                 sprintf(buf, "raw 0x%llx", config);
282                 return buf;
283         }
284
285         switch (type) {
286         case PERF_TYPE_HARDWARE:
287                 if (config < PERF_COUNT_HW_MAX)
288                         return hw_event_names[config];
289                 return "unknown-hardware";
290
291         case PERF_TYPE_HW_CACHE: {
292                 u8 cache_type, cache_op, cache_result;
293
294                 cache_type   = (config >>  0) & 0xff;
295                 if (cache_type > PERF_COUNT_HW_CACHE_MAX)
296                         return "unknown-ext-hardware-cache-type";
297
298                 cache_op     = (config >>  8) & 0xff;
299                 if (cache_op > PERF_COUNT_HW_CACHE_OP_MAX)
300                         return "unknown-ext-hardware-cache-op";
301
302                 cache_result = (config >> 16) & 0xff;
303                 if (cache_result > PERF_COUNT_HW_CACHE_RESULT_MAX)
304                         return "unknown-ext-hardware-cache-result";
305
306                 if (!is_cache_op_valid(cache_type, cache_op))
307                         return "invalid-cache";
308
309                 return event_cache_name(cache_type, cache_op, cache_result);
310         }
311
312         case PERF_TYPE_SOFTWARE:
313                 if (config < PERF_COUNT_SW_MAX)
314                         return sw_event_names[config];
315                 return "unknown-software";
316
317         case PERF_TYPE_TRACEPOINT:
318                 return tracepoint_id_to_name(config);
319
320         default:
321                 break;
322         }
323
324         return "unknown";
325 }
326
327 static int parse_aliases(const char **str, const char *names[][MAX_ALIASES], int size)
328 {
329         int i, j;
330         int n, longest = -1;
331
332         for (i = 0; i < size; i++) {
333                 for (j = 0; j < MAX_ALIASES && names[i][j]; j++) {
334                         n = strlen(names[i][j]);
335                         if (n > longest && !strncasecmp(*str, names[i][j], n))
336                                 longest = n;
337                 }
338                 if (longest > 0) {
339                         *str += longest;
340                         return i;
341                 }
342         }
343
344         return -1;
345 }
346
347 static int
348 parse_generic_hw_event(const char **str, struct perf_counter_attr *attr)
349 {
350         const char *s = *str;
351         int cache_type = -1, cache_op = -1, cache_result = -1;
352
353         cache_type = parse_aliases(&s, hw_cache, PERF_COUNT_HW_CACHE_MAX);
354         /*
355          * No fallback - if we cannot get a clear cache type
356          * then bail out:
357          */
358         if (cache_type == -1)
359                 return 0;
360
361         while ((cache_op == -1 || cache_result == -1) && *s == '-') {
362                 ++s;
363
364                 if (cache_op == -1) {
365                         cache_op = parse_aliases(&s, hw_cache_op,
366                                                 PERF_COUNT_HW_CACHE_OP_MAX);
367                         if (cache_op >= 0) {
368                                 if (!is_cache_op_valid(cache_type, cache_op))
369                                         return 0;
370                                 continue;
371                         }
372                 }
373
374                 if (cache_result == -1) {
375                         cache_result = parse_aliases(&s, hw_cache_result,
376                                                 PERF_COUNT_HW_CACHE_RESULT_MAX);
377                         if (cache_result >= 0)
378                                 continue;
379                 }
380
381                 /*
382                  * Can't parse this as a cache op or result, so back up
383                  * to the '-'.
384                  */
385                 --s;
386                 break;
387         }
388
389         /*
390          * Fall back to reads:
391          */
392         if (cache_op == -1)
393                 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
394
395         /*
396          * Fall back to accesses:
397          */
398         if (cache_result == -1)
399                 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
400
401         attr->config = cache_type | (cache_op << 8) | (cache_result << 16);
402         attr->type = PERF_TYPE_HW_CACHE;
403
404         *str = s;
405         return 1;
406 }
407
408 static int parse_tracepoint_event(const char **strp,
409                                     struct perf_counter_attr *attr)
410 {
411         const char *evt_name;
412         char *flags;
413         char sys_name[MAX_EVENT_LENGTH];
414         char id_buf[4];
415         int fd;
416         unsigned int sys_length, evt_length;
417         u64 id;
418         char evt_path[MAXPATHLEN];
419
420         if (valid_debugfs_mount(debugfs_path))
421                 return 0;
422
423         evt_name = strchr(*strp, ':');
424         if (!evt_name)
425                 return 0;
426
427         sys_length = evt_name - *strp;
428         if (sys_length >= MAX_EVENT_LENGTH)
429                 return 0;
430
431         strncpy(sys_name, *strp, sys_length);
432         sys_name[sys_length] = '\0';
433         evt_name = evt_name + 1;
434
435         flags = strchr(evt_name, ':');
436         if (flags) {
437                 *flags = '\0';
438                 flags++;
439                 if (!strncmp(flags, "record", strlen(flags)))
440                         attr->sample_type |= PERF_SAMPLE_RAW;
441         }
442
443         evt_length = strlen(evt_name);
444         if (evt_length >= MAX_EVENT_LENGTH)
445                 return 0;
446
447         snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", debugfs_path,
448                  sys_name, evt_name);
449         fd = open(evt_path, O_RDONLY);
450         if (fd < 0)
451                 return 0;
452
453         if (read(fd, id_buf, sizeof(id_buf)) < 0) {
454                 close(fd);
455                 return 0;
456         }
457         close(fd);
458         id = atoll(id_buf);
459         attr->config = id;
460         attr->type = PERF_TYPE_TRACEPOINT;
461         *strp = evt_name + evt_length;
462         return 1;
463 }
464
465 static int check_events(const char *str, unsigned int i)
466 {
467         int n;
468
469         n = strlen(event_symbols[i].symbol);
470         if (!strncmp(str, event_symbols[i].symbol, n))
471                 return n;
472
473         n = strlen(event_symbols[i].alias);
474         if (n)
475                 if (!strncmp(str, event_symbols[i].alias, n))
476                         return n;
477         return 0;
478 }
479
480 static int
481 parse_symbolic_event(const char **strp, struct perf_counter_attr *attr)
482 {
483         const char *str = *strp;
484         unsigned int i;
485         int n;
486
487         for (i = 0; i < ARRAY_SIZE(event_symbols); i++) {
488                 n = check_events(str, i);
489                 if (n > 0) {
490                         attr->type = event_symbols[i].type;
491                         attr->config = event_symbols[i].config;
492                         *strp = str + n;
493                         return 1;
494                 }
495         }
496         return 0;
497 }
498
499 static int parse_raw_event(const char **strp, struct perf_counter_attr *attr)
500 {
501         const char *str = *strp;
502         u64 config;
503         int n;
504
505         if (*str != 'r')
506                 return 0;
507         n = hex2u64(str + 1, &config);
508         if (n > 0) {
509                 *strp = str + n + 1;
510                 attr->type = PERF_TYPE_RAW;
511                 attr->config = config;
512                 return 1;
513         }
514         return 0;
515 }
516
517 static int
518 parse_numeric_event(const char **strp, struct perf_counter_attr *attr)
519 {
520         const char *str = *strp;
521         char *endp;
522         unsigned long type;
523         u64 config;
524
525         type = strtoul(str, &endp, 0);
526         if (endp > str && type < PERF_TYPE_MAX && *endp == ':') {
527                 str = endp + 1;
528                 config = strtoul(str, &endp, 0);
529                 if (endp > str) {
530                         attr->type = type;
531                         attr->config = config;
532                         *strp = endp;
533                         return 1;
534                 }
535         }
536         return 0;
537 }
538
539 static int
540 parse_event_modifier(const char **strp, struct perf_counter_attr *attr)
541 {
542         const char *str = *strp;
543         int eu = 1, ek = 1, eh = 1;
544
545         if (*str++ != ':')
546                 return 0;
547         while (*str) {
548                 if (*str == 'u')
549                         eu = 0;
550                 else if (*str == 'k')
551                         ek = 0;
552                 else if (*str == 'h')
553                         eh = 0;
554                 else
555                         break;
556                 ++str;
557         }
558         if (str >= *strp + 2) {
559                 *strp = str;
560                 attr->exclude_user   = eu;
561                 attr->exclude_kernel = ek;
562                 attr->exclude_hv     = eh;
563                 return 1;
564         }
565         return 0;
566 }
567
568 /*
569  * Each event can have multiple symbolic names.
570  * Symbolic names are (almost) exactly matched.
571  */
572 static int parse_event_symbols(const char **str, struct perf_counter_attr *attr)
573 {
574         if (!(parse_tracepoint_event(str, attr) ||
575               parse_raw_event(str, attr) ||
576               parse_numeric_event(str, attr) ||
577               parse_symbolic_event(str, attr) ||
578               parse_generic_hw_event(str, attr)))
579                 return 0;
580
581         parse_event_modifier(str, attr);
582
583         return 1;
584 }
585
586 int parse_events(const struct option *opt __used, const char *str, int unset __used)
587 {
588         struct perf_counter_attr attr;
589
590         for (;;) {
591                 if (nr_counters == MAX_COUNTERS)
592                         return -1;
593
594                 memset(&attr, 0, sizeof(attr));
595                 if (!parse_event_symbols(&str, &attr))
596                         return -1;
597
598                 if (!(*str == 0 || *str == ',' || isspace(*str)))
599                         return -1;
600
601                 attrs[nr_counters] = attr;
602                 nr_counters++;
603
604                 if (*str == 0)
605                         break;
606                 if (*str == ',')
607                         ++str;
608                 while (isspace(*str))
609                         ++str;
610         }
611
612         return 0;
613 }
614
615 static const char * const event_type_descriptors[] = {
616         "",
617         "Hardware event",
618         "Software event",
619         "Tracepoint event",
620         "Hardware cache event",
621 };
622
623 /*
624  * Print the events from <debugfs_mount_point>/tracing/events
625  */
626
627 static void print_tracepoint_events(void)
628 {
629         DIR *sys_dir, *evt_dir;
630         struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
631         int sys_dir_fd;
632         char evt_path[MAXPATHLEN];
633
634         if (valid_debugfs_mount(debugfs_path))
635                 return;
636
637         sys_dir = opendir(debugfs_path);
638         if (!sys_dir)
639                 goto cleanup;
640         sys_dir_fd = dirfd(sys_dir);
641
642         for_each_subsystem(sys_dir, sys_dirent, sys_next) {
643                 int dfd = openat(sys_dir_fd, sys_dirent.d_name,
644                                  O_RDONLY|O_DIRECTORY), evt_dir_fd;
645                 if (dfd == -1)
646                         continue;
647                 evt_dir = fdopendir(dfd);
648                 if (!evt_dir) {
649                         close(dfd);
650                         continue;
651                 }
652                 evt_dir_fd = dirfd(evt_dir);
653                 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
654                         snprintf(evt_path, MAXPATHLEN, "%s:%s",
655                                  sys_dirent.d_name, evt_dirent.d_name);
656                         fprintf(stderr, "  %-42s [%s]\n", evt_path,
657                                 event_type_descriptors[PERF_TYPE_TRACEPOINT+1]);
658                 }
659                 closedir(evt_dir);
660         }
661
662 cleanup:
663         closedir(sys_dir);
664 }
665
666 /*
667  * Print the help text for the event symbols:
668  */
669 void print_events(void)
670 {
671         struct event_symbol *syms = event_symbols;
672         unsigned int i, type, op, prev_type = -1;
673         char name[40];
674
675         fprintf(stderr, "\n");
676         fprintf(stderr, "List of pre-defined events (to be used in -e):\n");
677
678         for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
679                 type = syms->type + 1;
680                 if (type >= ARRAY_SIZE(event_type_descriptors))
681                         type = 0;
682
683                 if (type != prev_type)
684                         fprintf(stderr, "\n");
685
686                 if (strlen(syms->alias))
687                         sprintf(name, "%s OR %s", syms->symbol, syms->alias);
688                 else
689                         strcpy(name, syms->symbol);
690                 fprintf(stderr, "  %-42s [%s]\n", name,
691                         event_type_descriptors[type]);
692
693                 prev_type = type;
694         }
695
696         fprintf(stderr, "\n");
697         for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
698                 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
699                         /* skip invalid cache type */
700                         if (!is_cache_op_valid(type, op))
701                                 continue;
702
703                         for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
704                                 fprintf(stderr, "  %-42s [%s]\n",
705                                         event_cache_name(type, op, i),
706                                         event_type_descriptors[4]);
707                         }
708                 }
709         }
710
711         fprintf(stderr, "\n");
712         fprintf(stderr, "  %-42s [raw hardware event descriptor]\n",
713                 "rNNN");
714         fprintf(stderr, "\n");
715
716         print_tracepoint_events();
717
718         exit(129);
719 }