perf session: Reduce the number of parms to perf_session__process_events
[safe/jmp/linux-2.6] / tools / perf / builtin-annotate.c
index cd97c2b..a931b13 100644 (file)
 #include "perf.h"
 #include "util/debug.h"
 
+#include "util/event.h"
 #include "util/parse-options.h"
 #include "util/parse-events.h"
 #include "util/thread.h"
 #include "util/sort.h"
 #include "util/hist.h"
-#include "util/process_events.h"
+#include "util/session.h"
 
 static char            const *input_name = "perf.data";
 
 static int             force;
-static int             input;
 
 static int             full_paths;
 
 static int             print_line;
 
-static unsigned long   page_size;
-static unsigned long   mmap_window = 32;
-
 struct sym_hist {
        u64             sum;
        u64             ip[0];
@@ -124,133 +121,34 @@ static void hist_hit(struct hist_entry *he, u64 ip)
                        h->ip[offset]);
 }
 
-static int hist_entry__add(struct thread *thread, struct map *map,
-                          struct symbol *sym, u64 ip, u64 count, char level)
+static int hist_entry__add(struct addr_location *al, u64 count)
 {
        bool hit;
-       struct hist_entry *he = __hist_entry__add(thread, map, sym, NULL, ip,
-                                                 count, level, &hit);
+       struct hist_entry *he = __hist_entry__add(al, NULL, count, &hit);
        if (he == NULL)
                return -ENOMEM;
-       hist_hit(he, ip);
+       hist_hit(he, al->addr);
        return 0;
 }
 
-static int
-process_sample_event(event_t *event, unsigned long offset, unsigned long head)
+static int process_sample_event(event_t *event, struct perf_session *session __used)
 {
-       char level;
-       u64 ip = event->ip.ip;
-       struct map *map = NULL;
-       struct symbol *sym = NULL;
-       struct thread *thread = threads__findnew(event->ip.pid);
-
-       dump_printf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
-               (void *)(offset + head),
-               (void *)(long)(event->header.size),
-               event->header.misc,
-               event->ip.pid,
-               (void *)(long)ip);
-
-       if (thread == NULL) {
+       struct addr_location al;
+
+       dump_printf("(IP, %d): %d: %p\n", event->header.misc,
+                   event->ip.pid, (void *)(long)event->ip.ip);
+
+       if (event__preprocess_sample(event, &al, symbol_filter) < 0) {
                fprintf(stderr, "problem processing %d event, skipping it.\n",
                        event->header.type);
                return -1;
        }
 
-       dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
-
-       if (event->header.misc & PERF_RECORD_MISC_KERNEL) {
-               level = 'k';
-               sym = kernel_maps__find_symbol(ip, &map, symbol_filter);
-               dump_printf(" ...... dso: %s\n",
-                           map ? map->dso->long_name : "<not found>");
-       } else if (event->header.misc & PERF_RECORD_MISC_USER) {
-               level = '.';
-               map = thread__find_map(thread, ip);
-               if (map != NULL) {
-got_map:
-                       ip = map->map_ip(map, ip);
-                       sym = map__find_symbol(map, ip, symbol_filter);
-               } else {
-                       /*
-                        * If this is outside of all known maps,
-                        * and is a negative address, try to look it
-                        * up in the kernel dso, as it might be a
-                        * vsyscall or vdso (which executes in user-mode).
-                        *
-                        * XXX This is nasty, we should have a symbol list in
-                        * the "[vdso]" dso, but for now lets use the old
-                        * trick of looking in the whole kernel symbol list.
-                        */
-                       if ((long long)ip < 0) {
-                               map = kernel_map;
-                               goto got_map;
-                       }
-               }
-               dump_printf(" ...... dso: %s\n",
-                           map ? map->dso->long_name : "<not found>");
-       } else {
-               level = 'H';
-               dump_printf(" ...... dso: [hypervisor]\n");
-       }
-
-       if (hist_entry__add(thread, map, sym, ip, 1, level)) {
+       if (hist_entry__add(&al, 1)) {
                fprintf(stderr, "problem incrementing symbol count, "
                                "skipping event\n");
                return -1;
        }
-       total++;
-
-       return 0;
-}
-
-static int
-process_comm_event(event_t *event, unsigned long offset, unsigned long head)
-{
-       struct thread *thread = threads__findnew(event->comm.pid);
-
-       dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n",
-               (void *)(offset + head),
-               (void *)(long)(event->header.size),
-               event->comm.comm, event->comm.pid);
-
-       if (thread == NULL ||
-           thread__set_comm(thread, event->comm.comm)) {
-               dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
-               return -1;
-       }
-       total_comm++;
-
-       return 0;
-}
-
-static int
-process_event(event_t *event, unsigned long offset, unsigned long head)
-{
-       switch (event->header.type) {
-       case PERF_RECORD_SAMPLE:
-               return process_sample_event(event, offset, head);
-
-       case PERF_RECORD_MMAP:
-               return process_mmap_event(event, offset, head);
-
-       case PERF_RECORD_COMM:
-               return process_comm_event(event, offset, head);
-
-       case PERF_RECORD_FORK:
-               return process_task_event(event, offset, head);
-       /*
-        * We dont process them right now but they are fine:
-        */
-
-       case PERF_RECORD_THROTTLE:
-       case PERF_RECORD_UNTHROTTLE:
-               return 0;
-
-       default:
-               return -1;
-       }
 
        return 0;
 }
@@ -555,111 +453,31 @@ static void find_annotations(void)
        }
 }
 
+static struct perf_event_ops event_ops = {
+       .process_sample_event   = process_sample_event,
+       .process_mmap_event     = event__process_mmap,
+       .process_comm_event     = event__process_comm,
+       .process_fork_event     = event__process_task,
+};
+
 static int __cmd_annotate(void)
 {
-       int ret, rc = EXIT_FAILURE;
-       unsigned long offset = 0;
-       unsigned long head = 0;
-       struct stat input_stat;
-       event_t *event;
-       uint32_t size;
-       char *buf;
-
-       register_idle_thread();
-
-       input = open(input_name, O_RDONLY);
-       if (input < 0) {
-               perror("failed to open file");
-               exit(-1);
-       }
-
-       ret = fstat(input, &input_stat);
-       if (ret < 0) {
-               perror("failed to stat file");
-               exit(-1);
-       }
-
-       if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
-               fprintf(stderr, "file: %s not owned by current user or root\n", input_name);
-               exit(-1);
-       }
-
-       if (!input_stat.st_size) {
-               fprintf(stderr, "zero-sized file, nothing to do!\n");
-               exit(0);
-       }
-
-remap:
-       buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
-                          MAP_SHARED, input, offset);
-       if (buf == MAP_FAILED) {
-               perror("failed to mmap file");
-               exit(-1);
-       }
-
-more:
-       event = (event_t *)(buf + head);
-
-       size = event->header.size;
-       if (!size)
-               size = 8;
-
-       if (head + event->header.size >= page_size * mmap_window) {
-               unsigned long shift = page_size * (head / page_size);
-               int munmap_ret;
-
-               munmap_ret = munmap(buf, page_size * mmap_window);
-               assert(munmap_ret == 0);
-
-               offset += shift;
-               head -= shift;
-               goto remap;
-       }
-
-       size = event->header.size;
-
-       dump_printf("%p [%p]: event: %d\n",
-                       (void *)(offset + head),
-                       (void *)(long)event->header.size,
-                       event->header.type);
-
-       if (!size || process_event(event, offset, head) < 0) {
-
-               dump_printf("%p [%p]: skipping unknown header type: %d\n",
-                       (void *)(offset + head),
-                       (void *)(long)(event->header.size),
-                       event->header.type);
-
-               total_unknown++;
+       struct perf_session *session = perf_session__new(input_name, O_RDONLY,
+                                                        force);
+       int ret;
 
-               /*
-                * assume we lost track of the stream, check alignment, and
-                * increment a single u64 in the hope to catch on again 'soon'.
-                */
+       if (session == NULL)
+               return -ENOMEM;
 
-               if (unlikely(head & 7))
-                       head &= ~7ULL;
+       ret = perf_session__process_events(session, &event_ops);
+       if (ret)
+               goto out_delete;
 
-               size = 8;
+       if (dump_trace) {
+               event__print_totals();
+               goto out_delete;
        }
 
-       head += size;
-
-       if (offset + head < (unsigned long)input_stat.st_size)
-               goto more;
-
-       rc = EXIT_SUCCESS;
-       close(input);
-
-       dump_printf("      IP events: %10ld\n", total);
-       dump_printf("    mmap events: %10ld\n", total_mmap);
-       dump_printf("    comm events: %10ld\n", total_comm);
-       dump_printf("    fork events: %10ld\n", total_fork);
-       dump_printf(" unknown events: %10ld\n", total_unknown);
-
-       if (dump_trace)
-               return 0;
-
        if (verbose > 3)
                threads__fprintf(stdout);
 
@@ -667,11 +485,13 @@ more:
                dsos__fprintf(stdout);
 
        collapse__resort();
-       output__resort(total);
+       output__resort(event__total[0]);
 
        find_annotations();
+out_delete:
+       perf_session__delete(session);
 
-       return rc;
+       return ret;
 }
 
 static const char * const annotate_usage[] = {
@@ -720,8 +540,6 @@ int cmd_annotate(int argc, const char **argv, const char *prefix __used)
        if (symbol__init(&symbol_conf) < 0)
                return -1;
 
-       page_size = getpagesize();
-
        argc = parse_options(argc, argv, options, annotate_usage, 0);
 
        setup_sorting();