]> nv-tegra.nvidia Code Review - linux-2.6.git/blobdiff - tools/perf/builtin-report.c
perf session: Move the hist_entries rb tree to perf_session
[linux-2.6.git] / tools / perf / builtin-report.c
index 400bef981c6ca4a76c9768fb5049b5e60740bd23..854427f0e57edce4523f1c750fbb3345ddf144b8 100644 (file)
 #include "perf.h"
 #include "util/debug.h"
 #include "util/header.h"
+#include "util/session.h"
 
 #include "util/parse-options.h"
 #include "util/parse-events.h"
 
-#include "util/data_map.h"
 #include "util/thread.h"
 #include "util/sort.h"
 #include "util/hist.h"
-#include "util/process_events.h"
 
 static char            const *input_name = "perf.data";
 
@@ -39,8 +38,8 @@ static char           *dso_list_str, *comm_list_str, *sym_list_str,
 static struct strlist  *dso_list, *comm_list, *sym_list;
 
 static int             force;
+static bool            use_callchain;
 
-static int             full_paths;
 static int             show_nr_samples;
 
 static int             show_threads;
@@ -53,8 +52,6 @@ static int            exclude_other = 1;
 
 static char            callchain_default_opt[] = "fractal,0.5";
 
-static struct perf_header *header;
-
 static u64             sample_type;
 
 struct symbol_conf     symbol_conf;
@@ -316,8 +313,9 @@ hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
        return ret;
 }
 
-static size_t
-hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
+static size_t hist_entry__fprintf(FILE *fp, struct hist_entry *self,
+                                 struct perf_session *session,
+                                 u64 total_samples)
 {
        struct sort_entry *se;
        size_t ret;
@@ -349,7 +347,7 @@ hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
 
        ret += fprintf(fp, "\n");
 
-       if (callchain) {
+       if (session->use_callchain) {
                int left_margin = 0;
 
                if (sort__first_dimension == SORT_COMM) {
@@ -409,55 +407,6 @@ static int thread__set_comm_adjust(struct thread *self, const char *comm)
        return 0;
 }
 
-
-static struct symbol *
-resolve_symbol(struct thread *thread, struct map **mapp, u64 *ipp)
-{
-       struct map *map = mapp ? *mapp : NULL;
-       u64 ip = *ipp;
-
-       if (map)
-               goto got_map;
-
-       if (!thread)
-               return NULL;
-
-       map = thread__find_map(thread, ip);
-       if (map != NULL) {
-               /*
-                * We have to do this here as we may have a dso
-                * with no symbol hit that has a name longer than
-                * the ones with symbols sampled.
-                */
-               if (!sort_dso.elide && !map->dso->slen_calculated)
-                       dso__calc_col_width(map->dso);
-
-               if (mapp)
-                       *mapp = map;
-got_map:
-               ip = map->map_ip(map, ip);
-       } 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)
-                       return kernel_maps__find_function(ip, mapp, NULL);
-       }
-       dump_printf(" ...... dso: %s\n",
-                   map ? map->dso->long_name : "<not found>");
-       dump_printf(" ...... map: %Lx -> %Lx\n", *ipp, ip);
-       *ipp  = ip;
-
-       return map ? map__find_symbol(map, ip, NULL) : NULL;
-}
-
 static int call__match(struct symbol *sym)
 {
        if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
@@ -467,14 +416,15 @@ static int call__match(struct symbol *sym)
 }
 
 static struct symbol **resolve_callchain(struct thread *thread,
+                                        struct perf_session *session,
                                         struct ip_callchain *chain,
                                         struct symbol **parent)
 {
-       u64 context = PERF_CONTEXT_MAX;
+       u8 cpumode = PERF_RECORD_MISC_USER;
        struct symbol **syms = NULL;
        unsigned int i;
 
-       if (callchain) {
+       if (session->use_callchain) {
                syms = calloc(chain->nr, sizeof(*syms));
                if (!syms) {
                        fprintf(stderr, "Can't allocate memory for symbols\n");
@@ -484,30 +434,31 @@ static struct symbol **resolve_callchain(struct thread *thread,
 
        for (i = 0; i < chain->nr; i++) {
                u64 ip = chain->ips[i];
-               struct symbol *sym = NULL;
+               struct addr_location al;
 
                if (ip >= PERF_CONTEXT_MAX) {
-                       context = ip;
+                       switch (ip) {
+                       case PERF_CONTEXT_HV:
+                               cpumode = PERF_RECORD_MISC_HYPERVISOR;  break;
+                       case PERF_CONTEXT_KERNEL:
+                               cpumode = PERF_RECORD_MISC_KERNEL;      break;
+                       case PERF_CONTEXT_USER:
+                               cpumode = PERF_RECORD_MISC_USER;        break;
+                       default:
+                               break;
+                       }
                        continue;
                }
 
-               switch (context) {
-               case PERF_CONTEXT_HV:
-                       break;
-               case PERF_CONTEXT_KERNEL:
-                       sym = kernel_maps__find_function(ip, NULL, NULL);
-                       break;
-               default:
-                       sym = resolve_symbol(thread, NULL, &ip);
-                       break;
-               }
-
-               if (sym) {
-                       if (sort__has_parent && !*parent && call__match(sym))
-                               *parent = sym;
-                       if (!callchain)
+               thread__find_addr_location(thread, session, cpumode,
+                                          MAP__FUNCTION, ip, &al, NULL);
+               if (al.sym != NULL) {
+                       if (sort__has_parent && !*parent &&
+                           call__match(al.sym))
+                               *parent = al.sym;
+                       if (!session->use_callchain)
                                break;
-                       syms[i] = sym;
+                       syms[i] = al.sym;
                }
        }
 
@@ -518,27 +469,25 @@ static struct symbol **resolve_callchain(struct thread *thread,
  * collect histogram counts
  */
 
-static int
-hist_entry__add(struct thread *thread, struct map *map,
-               struct symbol *sym, u64 ip, struct ip_callchain *chain,
-               char level, u64 count)
+static int perf_session__add_hist_entry(struct perf_session *self,
+                                       struct addr_location *al,
+                                       struct ip_callchain *chain, u64 count)
 {
        struct symbol **syms = NULL, *parent = NULL;
        bool hit;
        struct hist_entry *he;
 
-       if ((sort__has_parent || callchain) && chain)
-               syms = resolve_callchain(thread, chain, &parent);
+       if ((sort__has_parent || self->use_callchain) && chain)
+               syms = resolve_callchain(al->thread, self, chain, &parent);
 
-       he = __hist_entry__add(thread, map, sym, parent,
-                              ip, count, level, &hit);
+       he = __perf_session__add_hist_entry(self, al, parent, count, &hit);
        if (he == NULL)
                return -ENOMEM;
 
        if (hit)
                he->count += count;
 
-       if (callchain) {
+       if (self->use_callchain) {
                if (!hit)
                        callchain_init(&he->callchain);
                append_chain(&he->callchain, chain, syms);
@@ -548,7 +497,8 @@ hist_entry__add(struct thread *thread, struct map *map,
        return 0;
 }
 
-static size_t output__fprintf(FILE *fp, u64 total_samples)
+static size_t perf_session__fprintf_hist_entries(struct perf_session *self,
+                                                u64 total_samples, FILE *fp)
 {
        struct hist_entry *pos;
        struct sort_entry *se;
@@ -620,9 +570,9 @@ static size_t output__fprintf(FILE *fp, u64 total_samples)
        fprintf(fp, "#\n");
 
 print_entries:
-       for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
+       for (nd = rb_first(&self->hists); nd; nd = rb_next(nd)) {
                pos = rb_entry(nd, struct hist_entry, rb_node);
-               ret += hist_entry__fprintf(fp, pos, total_samples);
+               ret += hist_entry__fprintf(fp, pos, self, total_samples);
        }
 
        if (sort_order == default_sort_order &&
@@ -655,51 +605,43 @@ static int validate_chain(struct ip_callchain *chain, event_t *event)
        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)
 {
-       char level;
-       struct symbol *sym = NULL;
-       u64 ip = event->ip.ip;
-       u64 period = 1;
-       struct map *map = NULL;
-       void *more_data = event->ip.__more_data;
-       struct ip_callchain *chain = NULL;
+       struct sample_data data;
        int cpumode;
-       struct thread *thread = threads__findnew(event->ip.pid);
+       struct addr_location al;
+       struct thread *thread;
 
-       if (sample_type & PERF_SAMPLE_PERIOD) {
-               period = *(u64 *)more_data;
-               more_data += sizeof(u64);
-       }
+       memset(&data, 0, sizeof(data));
+       data.period = 1;
+
+       event__parse_sample(event, sample_type, &data);
 
-       dump_printf("%p [%p]: PERF_RECORD_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n",
-               (void *)(offset + head),
-               (void *)(long)(event->header.size),
+       dump_printf("(IP, %d): %d/%d: %p period: %Ld\n",
                event->header.misc,
-               event->ip.pid, event->ip.tid,
-               (void *)(long)ip,
-               (long long)period);
+               data.pid, data.tid,
+               (void *)(long)data.ip,
+               (long long)data.period);
 
        if (sample_type & PERF_SAMPLE_CALLCHAIN) {
                unsigned int i;
 
-               chain = (void *)more_data;
+               dump_printf("... chain: nr:%Lu\n", data.callchain->nr);
 
-               dump_printf("... chain: nr:%Lu\n", chain->nr);
-
-               if (validate_chain(chain, event) < 0) {
+               if (validate_chain(data.callchain, event) < 0) {
                        pr_debug("call-chain problem with event, "
                                 "skipping it.\n");
                        return 0;
                }
 
                if (dump_trace) {
-                       for (i = 0; i < chain->nr; i++)
-                               dump_printf("..... %2d: %016Lx\n", i, chain->ips[i]);
+                       for (i = 0; i < data.callchain->nr; i++)
+                               dump_printf("..... %2d: %016Lx\n",
+                                           i, data.callchain->ips[i]);
                }
        }
 
+       thread = perf_session__findnew(session, data.pid);
        if (thread == NULL) {
                pr_debug("problem processing %d event, skipping it.\n",
                        event->header.type);
@@ -713,81 +655,55 @@ process_sample_event(event_t *event, unsigned long offset, unsigned long head)
 
        cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
 
-       if (cpumode == PERF_RECORD_MISC_KERNEL) {
-               level = 'k';
-               sym = kernel_maps__find_function(ip, &map, NULL);
-               dump_printf(" ...... dso: %s\n",
-                           map ? map->dso->long_name : "<not found>");
-       } else if (cpumode == PERF_RECORD_MISC_USER) {
-               level = '.';
-               sym = resolve_symbol(thread, &map, &ip);
-
-       } else {
-               level = 'H';
-               dump_printf(" ...... dso: [hypervisor]\n");
-       }
+       thread__find_addr_location(thread, session, cpumode,
+                                  MAP__FUNCTION, data.ip, &al, NULL);
+       /*
+        * We have to do this here as we may have a dso with no symbol hit that
+        * has a name longer than the ones with symbols sampled.
+        */
+       if (al.map && !sort_dso.elide && !al.map->dso->slen_calculated)
+               dso__calc_col_width(al.map->dso);
 
        if (dso_list &&
-           (!map || !map->dso ||
-            !(strlist__has_entry(dso_list, map->dso->short_name) ||
-              (map->dso->short_name != map->dso->long_name &&
-               strlist__has_entry(dso_list, map->dso->long_name)))))
+           (!al.map || !al.map->dso ||
+            !(strlist__has_entry(dso_list, al.map->dso->short_name) ||
+              (al.map->dso->short_name != al.map->dso->long_name &&
+               strlist__has_entry(dso_list, al.map->dso->long_name)))))
                return 0;
 
-       if (sym_list && sym && !strlist__has_entry(sym_list, sym->name))
+       if (sym_list && al.sym && !strlist__has_entry(sym_list, al.sym->name))
                return 0;
 
-       if (hist_entry__add(thread, map, sym, ip,
-                           chain, level, period)) {
+       if (perf_session__add_hist_entry(session, &al, data.callchain, data.period)) {
                pr_debug("problem incrementing symbol count, skipping event\n");
                return -1;
        }
 
-       total += period;
+       event__stats.total += data.period;
 
        return 0;
 }
 
-static int
-process_comm_event(event_t *event, unsigned long offset, unsigned long head)
+static int process_comm_event(event_t *event, struct perf_session *session)
 {
-       struct thread *thread = threads__findnew(event->comm.pid);
+       struct thread *thread = perf_session__findnew(session, 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);
+       dump_printf(": %s:%d\n", event->comm.comm, event->comm.pid);
 
        if (thread == NULL ||
            thread__set_comm_adjust(thread, event->comm.comm)) {
                dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
                return -1;
        }
-       total_comm++;
-
-       return 0;
-}
-
-static int
-process_lost_event(event_t *event, unsigned long offset, unsigned long head)
-{
-       dump_printf("%p [%p]: PERF_RECORD_LOST: id:%Ld: lost:%Ld\n",
-               (void *)(offset + head),
-               (void *)(long)(event->header.size),
-               event->lost.id,
-               event->lost.lost);
-
-       total_lost += event->lost.lost;
 
        return 0;
 }
 
-static int
-process_read_event(event_t *event, unsigned long offset, unsigned long head)
+static int process_read_event(event_t *event, struct perf_session *session __used)
 {
        struct perf_event_attr *attr;
 
-       attr = perf_header__find_attr(event->read.id, header);
+       attr = perf_header__find_attr(event->read.id, &session->header);
 
        if (show_threads) {
                const char *name = attr ? __event_name(attr->type, attr->config)
@@ -799,19 +715,14 @@ process_read_event(event_t *event, unsigned long offset, unsigned long head)
                                           event->read.value);
        }
 
-       dump_printf("%p [%p]: PERF_RECORD_READ: %d %d %s %Lu\n",
-                       (void *)(offset + head),
-                       (void *)(long)(event->header.size),
-                       event->read.pid,
-                       event->read.tid,
-                       attr ? __event_name(attr->type, attr->config)
-                            : "FAIL",
-                       event->read.value);
+       dump_printf(": %d %d %s %Lu\n", event->read.pid, event->read.tid,
+                   attr ? __event_name(attr->type, attr->config) : "FAIL",
+                   event->read.value);
 
        return 0;
 }
 
-static int sample_type_check(u64 type)
+static int sample_type_check(u64 type, struct perf_session *session)
 {
        sample_type = type;
 
@@ -822,14 +733,14 @@ static int sample_type_check(u64 type)
                                        " perf record without -g?\n");
                        return -1;
                }
-               if (callchain) {
+               if (session->use_callchain) {
                        fprintf(stderr, "selected -g but no callchain data."
                                        " Did you call perf record without"
                                        " -g?\n");
                        return -1;
                }
-       } else if (callchain_param.mode != CHAIN_NONE && !callchain) {
-                       callchain = 1;
+       } else if (callchain_param.mode != CHAIN_NONE && !session->use_callchain) {
+                       session->use_callchain = true;
                        if (register_callchain_param(&callchain_param) < 0) {
                                fprintf(stderr, "Can't register callchain"
                                                " params\n");
@@ -840,13 +751,13 @@ static int sample_type_check(u64 type)
        return 0;
 }
 
-static struct perf_file_handler file_handler = {
+static struct perf_event_ops event_ops = {
        .process_sample_event   = process_sample_event,
-       .process_mmap_event     = process_mmap_event,
+       .process_mmap_event     = event__process_mmap,
        .process_comm_event     = process_comm_event,
-       .process_exit_event     = process_task_event,
-       .process_fork_event     = process_task_event,
-       .process_lost_event     = process_lost_event,
+       .process_exit_event     = event__process_task,
+       .process_fork_event     = event__process_task,
+       .process_lost_event     = event__process_lost,
        .process_read_event     = process_read_event,
        .sample_type_check      = sample_type_check,
 };
@@ -854,45 +765,41 @@ static struct perf_file_handler file_handler = {
 
 static int __cmd_report(void)
 {
-       struct thread *idle;
        int ret;
+       struct perf_session *session;
+
+       session = perf_session__new(input_name, O_RDONLY, force, &symbol_conf);
+       if (session == NULL)
+               return -ENOMEM;
 
-       idle = register_idle_thread();
-       thread__comm_adjust(idle);
+       session->use_callchain = use_callchain;
 
        if (show_threads)
                perf_read_values_init(&show_threads_values);
 
-       register_perf_file_handler(&file_handler);
-
-       ret = mmap_dispatch_perf_file(&header, input_name, force,
-                                     full_paths, &cwdlen, &cwd);
+       ret = perf_session__process_events(session, &event_ops);
        if (ret)
-               return ret;
-
-       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("    lost events: %10ld\n", total_lost);
-       dump_printf(" unknown events: %10ld\n", file_handler.total_unknown);
+               goto out_delete;
 
-       if (dump_trace)
-               return 0;
+       if (dump_trace) {
+               event__print_totals();
+               goto out_delete;
+       }
 
        if (verbose > 3)
-               threads__fprintf(stdout);
+               perf_session__fprintf(session, stdout);
 
        if (verbose > 2)
                dsos__fprintf(stdout);
 
-       collapse__resort();
-       output__resort(total);
-       output__fprintf(stdout, total);
+       perf_session__collapse_resort(session);
+       perf_session__output_resort(session, event__stats.total);
+       perf_session__fprintf_hist_entries(session, event__stats.total, stdout);
 
        if (show_threads)
                perf_read_values_destroy(&show_threads_values);
-
+out_delete:
+       perf_session__delete(session);
        return ret;
 }
 
@@ -903,7 +810,7 @@ parse_callchain_opt(const struct option *opt __used, const char *arg,
        char *tok;
        char *endptr;
 
-       callchain = 1;
+       use_callchain = true;
 
        if (!arg)
                return 0;
@@ -924,7 +831,7 @@ parse_callchain_opt(const struct option *opt __used, const char *arg,
 
        else if (!strncmp(tok, "none", strlen(arg))) {
                callchain_param.mode = CHAIN_NONE;
-               callchain = 0;
+               use_callchain = true;
 
                return 0;
        }
@@ -975,7 +882,7 @@ static const struct option options[] = {
                   "pretty printing style key: normal raw"),
        OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
                   "sort by key(s): pid, comm, dso, symbol, parent"),
-       OPT_BOOLEAN('P', "full-paths", &full_paths,
+       OPT_BOOLEAN('P', "full-paths", &event_ops.full_paths,
                    "Don't shorten the pathnames taking into account the cwd"),
        OPT_STRING('p', "parent", &parent_pattern, "regex",
                   "regex filter to identify parent, see: '--sort parent'"),