perf tools: Factor out sample__resolve_callchain()
[firefly-linux-kernel-4.4.55.git] / tools / perf / builtin-top.c
index 5a11f13e56f9f186cc7aa0926f4ee63008327358..76cd510d34d023bf3c580ec2b3e2e217d8831cba 100644 (file)
@@ -189,21 +189,18 @@ static void perf_top__record_precise_ip(struct perf_top *top,
        if (pthread_mutex_trylock(&notes->lock))
                return;
 
-       if (notes->src == NULL && symbol__alloc_hist(sym) < 0) {
-               pthread_mutex_unlock(&notes->lock);
-               pr_err("Not enough memory for annotating '%s' symbol!\n",
-                      sym->name);
-               sleep(1);
-               return;
-       }
-
        ip = he->ms.map->map_ip(he->ms.map, ip);
-       err = symbol__inc_addr_samples(sym, he->ms.map, counter, ip);
+       err = hist_entry__inc_addr_samples(he, counter, ip);
 
        pthread_mutex_unlock(&notes->lock);
 
        if (err == -ERANGE && !he->ms.map->erange_warned)
                ui__warn_map_erange(he->ms.map, sym, ip);
+       else if (err == -ENOMEM) {
+               pr_err("Not enough memory for annotating '%s' symbol!\n",
+                      sym->name);
+               sleep(1);
+       }
 }
 
 static void perf_top__show_details(struct perf_top *top)
@@ -246,10 +243,10 @@ static struct hist_entry *perf_evsel__add_hist_entry(struct perf_evsel *evsel,
        struct hist_entry *he;
 
        pthread_mutex_lock(&evsel->hists.lock);
-       he = __hists__add_entry(&evsel->hists, al, NULL, sample->period,
-                               sample->weight);
+       he = __hists__add_entry(&evsel->hists, al, NULL, NULL, NULL,
+                               sample->period, sample->weight,
+                               sample->transaction);
        pthread_mutex_unlock(&evsel->hists.lock);
-
        if (he == NULL)
                return NULL;
 
@@ -287,7 +284,7 @@ static void perf_top__print_sym_table(struct perf_top *top)
                return;
        }
 
-       hists__collapse_resort(&top->sym_evsel->hists);
+       hists__collapse_resort(&top->sym_evsel->hists, NULL);
        hists__output_resort(&top->sym_evsel->hists);
        hists__decay_entries(&top->sym_evsel->hists,
                             top->hide_user_symbols,
@@ -485,7 +482,7 @@ static bool perf_top__handle_keypress(struct perf_top *top, int c)
 
                                fprintf(stderr, "\nAvailable events:");
 
-                               list_for_each_entry(top->sym_evsel, &top->evlist->entries, node)
+                               evlist__for_each(top->evlist, top->sym_evsel)
                                        fprintf(stderr, "\n\t%d %s", top->sym_evsel->idx, perf_evsel__name(top->sym_evsel));
 
                                prompt_integer(&counter, "Enter details event counter");
@@ -496,7 +493,7 @@ static bool perf_top__handle_keypress(struct perf_top *top, int c)
                                        sleep(1);
                                        break;
                                }
-                               list_for_each_entry(top->sym_evsel, &top->evlist->entries, node)
+                               evlist__for_each(top->evlist, top->sym_evsel)
                                        if (top->sym_evsel->idx == counter)
                                                break;
                        } else
@@ -553,7 +550,7 @@ static void perf_top__sort_new_samples(void *arg)
        if (t->evlist->selected != NULL)
                t->sym_evsel = t->evlist->selected;
 
-       hists__collapse_resort(&t->sym_evsel->hists);
+       hists__collapse_resort(&t->sym_evsel->hists, NULL);
        hists__output_resort(&t->sym_evsel->hists);
        hists__decay_entries(&t->sym_evsel->hists,
                             t->hide_user_symbols,
@@ -578,7 +575,7 @@ static void *display_thread_tui(void *arg)
         * Zooming in/out UIDs. For now juse use whatever the user passed
         * via --uid.
         */
-       list_for_each_entry(pos, &top->evlist->entries, node)
+       evlist__for_each(top->evlist, pos)
                pos->hists.uid_filter_str = top->record_opts.target.uid_str;
 
        perf_evlist__tui_browse_hists(top->evlist, help, &hbt, top->min_percent,
@@ -634,26 +631,9 @@ repeat:
        return NULL;
 }
 
-/* Tag samples to be skipped. */
-static const char *skip_symbols[] = {
-       "intel_idle",
-       "default_idle",
-       "native_safe_halt",
-       "cpu_idle",
-       "enter_idle",
-       "exit_idle",
-       "mwait_idle",
-       "mwait_idle_with_hints",
-       "poll_idle",
-       "ppc64_runlatch_off",
-       "pseries_dedicated_idle_sleep",
-       NULL
-};
-
 static int symbol_filter(struct map *map __maybe_unused, struct symbol *sym)
 {
        const char *name = sym->name;
-       int i;
 
        /*
         * ppc64 uses function descriptors and appends a '.' to the
@@ -671,12 +651,8 @@ static int symbol_filter(struct map *map __maybe_unused, struct symbol *sym)
            strstr(name, "_text_end"))
                return 1;
 
-       for (i = 0; skip_symbols[i]; i++) {
-               if (!strcmp(skip_symbols[i], name)) {
-                       sym->ignore = true;
-                       break;
-               }
-       }
+       if (symbol__is_idle(sym))
+               sym->ignore = true;
 
        return 0;
 }
@@ -767,14 +743,10 @@ static void perf_event__process_sample(struct perf_tool *tool,
        if (al.sym == NULL || !al.sym->ignore) {
                struct hist_entry *he;
 
-               if ((sort__has_parent || symbol_conf.use_callchain) &&
-                   sample->callchain) {
-                       err = machine__resolve_callchain(machine, evsel,
-                                                        al.thread, sample,
-                                                        &parent, &al);
-                       if (err)
-                               return;
-               }
+               err = sample__resolve_callchain(sample, &parent, evsel, &al,
+                                               top->max_stack);
+               if (err)
+                       return;
 
                he = perf_evsel__add_hist_entry(evsel, &al, sample);
                if (he == NULL) {
@@ -782,12 +754,9 @@ static void perf_event__process_sample(struct perf_tool *tool,
                        return;
                }
 
-               if (symbol_conf.use_callchain) {
-                       err = callchain_append(he->callchain, &callchain_cursor,
-                                              sample->period);
-                       if (err)
-                               return;
-               }
+               err = hist_entry__append_callchain(he, sample);
+               if (err)
+                       return;
 
                if (sort__has_sym)
                        perf_top__record_precise_ip(top, he, evsel->idx, ip);
@@ -856,7 +825,7 @@ static void perf_top__mmap_read_idx(struct perf_top *top, int idx)
                                                   &sample, machine);
                } else if (event->header.type < PERF_RECORD_MAX) {
                        hists__inc_nr_events(&evsel->hists, event->header.type);
-                       machine__process_event(machine, event);
+                       machine__process_event(machine, event, &sample);
                } else
                        ++session->stats.nr_unknown_events;
 next_event:
@@ -877,11 +846,11 @@ static int perf_top__start_counters(struct perf_top *top)
        char msg[512];
        struct perf_evsel *counter;
        struct perf_evlist *evlist = top->evlist;
-       struct perf_record_opts *opts = &top->record_opts;
+       struct record_opts *opts = &top->record_opts;
 
        perf_evlist__config(evlist, opts);
 
-       list_for_each_entry(counter, &evlist->entries, node) {
+       evlist__for_each(evlist, counter) {
 try_again:
                if (perf_evsel__open(counter, top->evlist->cpus,
                                     top->evlist->threads) < 0) {
@@ -929,14 +898,11 @@ static int perf_top__setup_sample_type(struct perf_top *top __maybe_unused)
 
 static int __cmd_top(struct perf_top *top)
 {
-       struct perf_record_opts *opts = &top->record_opts;
+       struct record_opts *opts = &top->record_opts;
        pthread_t thread;
        int ret;
-       /*
-        * FIXME: perf_session__new should allow passing a O_MMAP, so that all this
-        * mmap reading, etc is encapsulated in it. Use O_WRONLY for now.
-        */
-       top->session = perf_session__new(NULL, O_WRONLY, false, false, NULL);
+
+       top->session = perf_session__new(NULL, false, NULL);
        if (top->session == NULL)
                return -ENOMEM;
 
@@ -952,14 +918,8 @@ static int __cmd_top(struct perf_top *top)
        if (ret)
                goto out_delete;
 
-       if (perf_target__has_task(&opts->target))
-               perf_event__synthesize_thread_map(&top->tool, top->evlist->threads,
-                                                 perf_event__process,
-                                                 &top->session->machines.host);
-       else
-               perf_event__synthesize_threads(&top->tool, perf_event__process,
-                                              &top->session->machines.host);
-
+       machine__synthesize_threads(&top->session->machines.host, &opts->target,
+                                   top->evlist->threads, false);
        ret = perf_top__start_counters(top);
        if (ret)
                goto out_delete;
@@ -975,7 +935,7 @@ static int __cmd_top(struct perf_top *top)
         * XXX 'top' still doesn't start workloads like record, trace, but should,
         * so leave the check here.
         */
-        if (!perf_target__none(&opts->target))
+        if (!target__none(&opts->target))
                 perf_evlist__enable(top->evlist);
 
        /* Wait for a minimal set of events before starting the snapshot */
@@ -1043,7 +1003,7 @@ parse_percent_limit(const struct option *opt, const char *arg,
 
 int cmd_top(int argc, const char **argv, const char *prefix __maybe_unused)
 {
-       int status;
+       int status = -1;
        char errbuf[BUFSIZ];
        struct perf_top top = {
                .count_filter        = 5,
@@ -1053,14 +1013,15 @@ int cmd_top(int argc, const char **argv, const char *prefix __maybe_unused)
                        .user_freq      = UINT_MAX,
                        .user_interval  = ULLONG_MAX,
                        .freq           = 4000, /* 4 KHz */
-                       .target              = {
+                       .target         = {
                                .uses_mmap   = true,
                        },
                },
+               .max_stack           = PERF_MAX_STACK_DEPTH,
                .sym_pcnt_filter     = 5,
        };
-       struct perf_record_opts *opts = &top.record_opts;
-       struct perf_target *target = &opts->target;
+       struct record_opts *opts = &top.record_opts;
+       struct target *target = &opts->target;
        const struct option options[] = {
        OPT_CALLBACK('e', "event", &top.evlist, "event",
                     "event selector. use 'perf list' to list available events",
@@ -1076,10 +1037,13 @@ int cmd_top(int argc, const char **argv, const char *prefix __maybe_unused)
                    "list of cpus to monitor"),
        OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
                   "file", "vmlinux pathname"),
+       OPT_BOOLEAN(0, "ignore-vmlinux", &symbol_conf.ignore_vmlinux,
+                   "don't load vmlinux even if found"),
        OPT_BOOLEAN('K', "hide_kernel_symbols", &top.hide_kernel_symbols,
                    "hide kernel symbols"),
-       OPT_UINTEGER('m', "mmap-pages", &opts->mmap_pages,
-                    "number of mmap data pages"),
+       OPT_CALLBACK('m', "mmap-pages", &opts->mmap_pages, "pages",
+                    "number of mmap data pages",
+                    perf_evlist__parse_mmap_pages),
        OPT_INTEGER('r', "realtime", &top.realtime_prio,
                    "collect data with this RT SCHED_FIFO priority"),
        OPT_INTEGER('d', "delay", &top.delay_secs,
@@ -1088,7 +1052,7 @@ int cmd_top(int argc, const char **argv, const char *prefix __maybe_unused)
                            "dump the symbol table used for profiling"),
        OPT_INTEGER('f', "count-filter", &top.count_filter,
                    "only display functions with more events than this"),
-       OPT_BOOLEAN('g', "group", &opts->group,
+       OPT_BOOLEAN(0, "group", &opts->group,
                            "put the counters into a counter group"),
        OPT_BOOLEAN('i', "no-inherit", &opts->no_inherit,
                    "child tasks do not inherit counters"),
@@ -1105,15 +1069,19 @@ int cmd_top(int argc, const char **argv, const char *prefix __maybe_unused)
        OPT_INCR('v', "verbose", &verbose,
                    "be more verbose (show counter open errors, etc)"),
        OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
-                  "sort by key(s): pid, comm, dso, symbol, parent, weight, local_weight"),
+                  "sort by key(s): pid, comm, dso, symbol, parent, weight, local_weight,"
+                  " abort, in_tx, transaction"),
        OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
                    "Show a column with the number of samples"),
-       OPT_CALLBACK_NOOPT('G', NULL, &top.record_opts,
+       OPT_CALLBACK_NOOPT('g', NULL, &top.record_opts,
                           NULL, "enables call-graph recording",
                           &callchain_opt),
        OPT_CALLBACK(0, "call-graph", &top.record_opts,
                     "mode[,dump_size]", record_callchain_help,
                     &parse_callchain_opt),
+       OPT_INTEGER(0, "max-stack", &top.max_stack,
+                   "Set the maximum stack depth when parsing the callchain. "
+                   "Default: " __stringify(PERF_MAX_STACK_DEPTH)),
        OPT_CALLBACK(0, "ignore-callees", NULL, "regex",
                   "ignore callees of these functions in call graphs",
                   report_parse_ignore_callees_opt),
@@ -1154,8 +1122,10 @@ int cmd_top(int argc, const char **argv, const char *prefix __maybe_unused)
        if (sort_order == default_sort_order)
                sort_order = "dso,symbol";
 
-       if (setup_sorting() < 0)
-               usage_with_options(top_usage, options);
+       if (setup_sorting() < 0) {
+               parse_options_usage(top_usage, options, "s", 1);
+               goto out_delete_evlist;
+       }
 
        /* display thread wants entries to be collapsed in a different tree */
        sort__need_collapse = 1;
@@ -1167,24 +1137,24 @@ int cmd_top(int argc, const char **argv, const char *prefix __maybe_unused)
 
        setup_browser(false);
 
-       status = perf_target__validate(target);
+       status = target__validate(target);
        if (status) {
-               perf_target__strerror(target, status, errbuf, BUFSIZ);
-               ui__warning("%s", errbuf);
+               target__strerror(target, status, errbuf, BUFSIZ);
+               ui__warning("%s\n", errbuf);
        }
 
-       status = perf_target__parse_uid(target);
+       status = target__parse_uid(target);
        if (status) {
                int saved_errno = errno;
 
-               perf_target__strerror(target, status, errbuf, BUFSIZ);
-               ui__error("%s", errbuf);
+               target__strerror(target, status, errbuf, BUFSIZ);
+               ui__error("%s\n", errbuf);
 
                status = -saved_errno;
                goto out_delete_evlist;
        }
 
-       if (perf_target__none(target))
+       if (target__none(target))
                target->system_wide = true;
 
        if (perf_evlist__create_maps(top.evlist, target) < 0)
@@ -1193,7 +1163,7 @@ int cmd_top(int argc, const char **argv, const char *prefix __maybe_unused)
        if (!top.evlist->nr_entries &&
            perf_evlist__add_default(top.evlist) < 0) {
                ui__error("Not enough memory for event selector list\n");
-               goto out_delete_maps;
+               goto out_delete_evlist;
        }
 
        symbol_conf.nr_events = top.evlist->nr_entries;
@@ -1201,22 +1171,9 @@ int cmd_top(int argc, const char **argv, const char *prefix __maybe_unused)
        if (top.delay_secs < 1)
                top.delay_secs = 1;
 
-       if (opts->user_interval != ULLONG_MAX)
-               opts->default_interval = opts->user_interval;
-       if (opts->user_freq != UINT_MAX)
-               opts->freq = opts->user_freq;
-
-       /*
-        * User specified count overrides default frequency.
-        */
-       if (opts->default_interval)
-               opts->freq = 0;
-       else if (opts->freq) {
-               opts->default_interval = opts->freq;
-       } else {
-               ui__error("frequency and count are zero, aborting\n");
+       if (record_opts__config(opts)) {
                status = -EINVAL;
-               goto out_delete_maps;
+               goto out_delete_evlist;
        }
 
        top.sym_evsel = perf_evlist__first(top.evlist);
@@ -1241,8 +1198,6 @@ int cmd_top(int argc, const char **argv, const char *prefix __maybe_unused)
 
        status = __cmd_top(&top);
 
-out_delete_maps:
-       perf_evlist__delete_maps(top.evlist);
 out_delete_evlist:
        perf_evlist__delete(top.evlist);