cregit-Linux how code gets into the kernel

Release 4.14 tools/perf/builtin-top.c

Directory: tools/perf
 * builtin-top.c
 * Builtin top command: Display a continuously updated profile of
 * any workload, CPU or specific PID.
 * Copyright (C) 2008, Red Hat Inc, Ingo Molnar <>
 *               2011, Red Hat Inc, Arnaldo Carvalho de Melo <>
 * Improvements and fixes by:
 *   Arjan van de Ven <>
 *   Yanmin Zhang <>
 *   Wu Fengguang <>
 *   Mike Galbraith <>
 *   Paul Mackerras <>
 * Released under the GPL v2. (and only v2, not any later version)
#include "builtin.h"

#include "perf.h"

#include "util/annotate.h"
#include "util/config.h"
#include "util/color.h"
#include "util/drv_configs.h"
#include "util/evlist.h"
#include "util/evsel.h"
#include "util/event.h"
#include "util/machine.h"
#include "util/session.h"
#include "util/symbol.h"
#include "util/thread.h"
#include "util/thread_map.h"
#include "util/top.h"
#include <linux/rbtree.h>
#include <subcmd/parse-options.h>
#include "util/parse-events.h"
#include "util/cpumap.h"
#include "util/xyarray.h"
#include "util/sort.h"
#include "util/term.h"
#include "util/intlist.h"
#include "util/parse-branch-options.h"
#include "arch/common.h"

#include "util/debug.h"

#include <assert.h>
#include <elf.h>
#include <fcntl.h>

#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <inttypes.h>

#include <errno.h>
#include <time.h>
#include <sched.h>
#include <signal.h>

#include <sys/syscall.h>
#include <sys/ioctl.h>
#include <poll.h>
#include <sys/prctl.h>
#include <sys/wait.h>
#include <sys/uio.h>
#include <sys/utsname.h>
#include <sys/mman.h>

#include <linux/stringify.h>
#include <linux/time64.h>
#include <linux/types.h>

#include "sane_ctype.h"

static volatile int done;

#define HEADER_LINE_NR  5

static void perf_top__update_print_entries(struct perf_top *top) { top->print_entries = top->winsize.ws_row - HEADER_LINE_NR; }


Arnaldo Carvalho de Melo1773.91%480.00%
Namhyung Kim626.09%120.00%

static void perf_top__sig_winch(int sig __maybe_unused, siginfo_t *info __maybe_unused, void *arg) { struct perf_top *top = arg; get_term_dimensions(&top->winsize); perf_top__update_print_entries(top); }


Arnaldo Carvalho de Melo3794.87%375.00%
Irina Tirdea25.13%125.00%

static int perf_top__parse_source(struct perf_top *top, struct hist_entry *he) { struct symbol *sym; struct annotation *notes; struct map *map; int err = -1; if (!he || !he->ms.sym) return -1; sym = he->ms.sym; map = he->; /* * We can't annotate with just /proc/kallsyms */ if (map->dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS && !dso__is_kcore(map->dso)) { pr_err("Can't annotate %s: No vmlinux file was found in the " "path\n", sym->name); sleep(1); return -1; } notes = symbol__annotation(sym); if (notes->src != NULL) { pthread_mutex_lock(&notes->lock); goto out_assign; } pthread_mutex_lock(&notes->lock); if (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 err; } err = symbol__disassemble(sym, map, NULL, 0, NULL, NULL); if (err == 0) { out_assign: top->sym_filter_entry = he; } else { char msg[BUFSIZ]; symbol__strerror_disassemble(sym, map, err, msg, sizeof(msg)); pr_err("Couldn't annotate %s: %s\n", sym->name, msg); } pthread_mutex_unlock(&notes->lock); return err; }


Arnaldo Carvalho de Melo18068.97%1361.90%
Mike Galbraith5922.61%14.76%
Adrian Hunter83.07%14.76%
Ingo Molnar83.07%29.52%
Jin Yao41.53%29.52%
Jiri Olsa10.38%14.76%
Kirill Smelkov10.38%14.76%

static void __zero_source_counters(struct hist_entry *he) { struct symbol *sym = he->ms.sym; symbol__annotate_zero_histograms(sym); }


Arnaldo Carvalho de Melo1555.56%266.67%
Mike Galbraith1244.44%133.33%

static void ui__warn_map_erange(struct map *map, struct symbol *sym, u64 ip) { struct utsname uts; int err = uname(&uts); ui__warning("Out of bounds address found:\n\n" "Addr: %" PRIx64 "\n" "DSO: %s %c\n" "Map: %" PRIx64 "-%" PRIx64 "\n" "Symbol: %" PRIx64 "-%" PRIx64 " %c %s\n" "Arch: %s\n" "Kernel: %s\n" "Tools: %s\n\n" "Not all samples will be on the annotation output.\n\n" "Please report to\n", ip, map->dso->long_name, dso__symtab_origin(map->dso), map->start, map->end, sym->start, sym->end, sym->binding == STB_GLOBAL ? 'g' : sym->binding == STB_LOCAL ? 'l' : 'w', sym->name, err ? "[unknown]" : uts.machine, err ? "[unknown]" : uts.release, perf_version_string); if (use_browser <= 0) sleep(5); map->erange_warned = true; }


Arnaldo Carvalho de Melo144100.00%1100.00%

static void perf_top__record_precise_ip(struct perf_top *top, struct hist_entry *he, struct perf_sample *sample, int counter, u64 ip) { struct annotation *notes; struct symbol *sym = he->ms.sym; int err = 0; if (sym == NULL || (use_browser == 0 && (top->sym_filter_entry == NULL || top->sym_filter_entry->ms.sym != sym))) return; notes = symbol__annotation(sym); if (pthread_mutex_trylock(&notes->lock)) return; err = hist_entry__inc_addr_samples(he, sample, counter, ip); pthread_mutex_unlock(&notes->lock); if (unlikely(err)) { /* * This function is now called with he->hists->lock held. * Release it before going to sleep. */ pthread_mutex_unlock(&he->hists->lock); if (err == -ERANGE && !he->>erange_warned) ui__warn_map_erange(he->, sym, ip); else if (err == -ENOMEM) { pr_err("Not enough memory for annotating '%s' symbol!\n", sym->name); sleep(1); } pthread_mutex_lock(&he->hists->lock); } }


Arnaldo Carvalho de Melo12260.70%1164.71%
Namhyung Kim4220.90%423.53%
Mike Galbraith3014.93%15.88%
Taeung Song73.48%15.88%

static void perf_top__show_details(struct perf_top *top) { struct hist_entry *he = top->sym_filter_entry; struct annotation *notes; struct symbol *symbol; int more; if (!he) return; symbol = he->ms.sym; notes = symbol__annotation(symbol); pthread_mutex_lock(&notes->lock); if (notes->src == NULL) goto out_unlock; printf("Showing %s for %s\n", perf_evsel__name(top->sym_evsel), symbol->name); printf(" Events Pcnt (>=%d%%)\n", top->sym_pcnt_filter); more = symbol__annotate_printf(symbol, he->, top->sym_evsel, 0, top->sym_pcnt_filter, top->print_entries, 4); if (top->evlist->enabled) { if (top->zero) symbol__annotate_zero_histogram(symbol, top->sym_evsel->idx); else symbol__annotate_decay_histogram(symbol, top->sym_evsel->idx); } if (more != 0) printf("%d lines not displayed, maybe increase display entries [e]\n", more); out_unlock: pthread_mutex_unlock(&notes->lock); }


Arnaldo Carvalho de Melo12365.08%1090.91%
Mike Galbraith6634.92%19.09%

static void perf_top__print_sym_table(struct perf_top *top) { char bf[160]; int printed = 0; const int win_width = top->winsize.ws_col - 1; struct perf_evsel *evsel = top->sym_evsel; struct hists *hists = evsel__hists(evsel); puts(CONSOLE_CLEAR); perf_top__header_snprintf(top, bf, sizeof(bf)); printf("%s\n", bf); perf_top__reset_sample_counters(top); printf("%-*.*s\n", win_width, win_width, graph_dotted_line); if (hists->stats.nr_lost_warned != hists->stats.nr_events[PERF_RECORD_LOST]) { hists->stats.nr_lost_warned = hists->stats.nr_events[PERF_RECORD_LOST]; color_fprintf(stdout, PERF_COLOR_RED, "WARNING: LOST %d chunks, Check IO/CPU overload", hists->stats.nr_lost_warned); ++printed; } if (top->sym_filter_entry) { perf_top__show_details(top); return; } if (top->evlist->enabled) { if (top->zero) { hists__delete_entries(hists); } else { hists__decay_entries(hists, top->hide_user_symbols, top->hide_kernel_symbols); } } hists__collapse_resort(hists, NULL); perf_evsel__output_resort(evsel, NULL); hists__output_recalc_col_len(hists, top->print_entries - printed); putchar('\n'); hists__fprintf(hists, false, top->print_entries - printed, win_width, top->min_percent, stdout, symbol_conf.use_callchain); }


Arnaldo Carvalho de Melo17870.36%1260.00%
Namhyung Kim3513.83%525.00%
Mike Galbraith249.49%15.00%
Jiri Olsa166.32%210.00%

static void prompt_integer(int *target, const char *msg) { char *buf = malloc(0), *p; size_t dummy = 0; int tmp; fprintf(stdout, "\n%s: ", msg); if (getline(&buf, &dummy, stdin) < 0) return; p = strchr(buf, '\n'); if (p) *p = 0; p = buf; while(*p) { if (!isdigit(*p)) goto out_free; p++; } tmp = strtoul(buf, NULL, 10); *target = tmp; out_free: free(buf); }


Mike Galbraith11489.76%133.33%
Arnaldo Carvalho de Melo129.45%133.33%
Ingo Molnar10.79%133.33%

static void prompt_percent(int *target, const char *msg) { int tmp = 0; prompt_integer(&tmp, msg); if (tmp >= 0 && tmp <= 100) *target = tmp; }


Mike Galbraith3172.09%150.00%
Arnaldo Carvalho de Melo1227.91%150.00%

static void perf_top__prompt_symbol(struct perf_top *top, const char *msg) { char *buf = malloc(0), *p; struct hist_entry *syme = top->sym_filter_entry, *n, *found = NULL; struct hists *hists = evsel__hists(top->sym_evsel); struct rb_node *next; size_t dummy = 0; /* zero counters of active symbol */ if (syme) { __zero_source_counters(syme); top->sym_filter_entry = NULL; } fprintf(stdout, "\n%s: ", msg); if (getline(&buf, &dummy, stdin) < 0) goto out_free; p = strchr(buf, '\n'); if (p) *p = 0; next = rb_first(&hists->entries); while (next) { n = rb_entry(next, struct hist_entry, rb_node); if (n->ms.sym && !strcmp(buf, n->ms.sym->name)) { found = n; break; } next = rb_next(&n->rb_node); } if (!found) { fprintf(stderr, "Sorry, %s is not active.\n", buf); sleep(1); } else perf_top__parse_source(top, found); out_free: free(buf); }


Arnaldo Carvalho de Melo11951.07%562.50%
Mike Galbraith11047.21%112.50%
Ingo Molnar31.29%112.50%
Kirill Smelkov10.43%112.50%

static void perf_top__print_mapped_keys(struct perf_top *top) { char *name = NULL; if (top->sym_filter_entry) { struct symbol *sym = top->sym_filter_entry->ms.sym; name = sym->name; } fprintf(stdout, "\nMapped keys:\n"); fprintf(stdout, "\t[d] display refresh delay. \t(%d)\n", top->delay_secs); fprintf(stdout, "\t[e] display entries (lines). \t(%d)\n", top->print_entries); if (top->evlist->nr_entries > 1) fprintf(stdout, "\t[E] active event counter. \t(%s)\n", perf_evsel__name(top->sym_evsel)); fprintf(stdout, "\t[f] profile display filter (count). \t(%d)\n", top->count_filter); fprintf(stdout, "\t[F] annotate display filter (percent). \t(%d%%)\n", top->sym_pcnt_filter); fprintf(stdout, "\t[s] annotate symbol. \t(%s)\n", name?: "NULL"); fprintf(stdout, "\t[S] stop annotation.\n"); fprintf(stdout, "\t[K] hide kernel_symbols symbols. \t(%s)\n", top->hide_kernel_symbols ? "yes" : "no"); fprintf(stdout, "\t[U] hide user symbols. \t(%s)\n", top->hide_user_symbols ? "yes" : "no"); fprintf(stdout, "\t[z] toggle sample zeroing. \t(%d)\n", top->zero ? 1 : 0); fprintf(stdout, "\t[qQ] quit.\n"); }


Mike Galbraith10555.26%318.75%
Arnaldo Carvalho de Melo6835.79%1062.50%
Ingo Molnar168.42%212.50%
Kirill Smelkov10.53%16.25%

static int perf_top__key_mapped(struct perf_top *top, int c) { switch (c) { case 'd': case 'e': case 'f': case 'z': case 'q': case 'Q': case 'K': case 'U': case 'F': case 's': case 'S': return 1; case 'E': return top->evlist->nr_entries > 1 ? 1 : 0; default: break; } return 0; }


Mike Galbraith4153.25%228.57%
Arnaldo Carvalho de Melo2532.47%342.86%
Kirill Smelkov911.69%114.29%
Ingo Molnar22.60%114.29%

static bool perf_top__handle_keypress(struct perf_top *top, int c) { bool ret = true; if (!perf_top__key_mapped(top, c)) { struct pollfd stdin_poll = { .fd = 0, .events = POLLIN }; struct termios save; perf_top__print_mapped_keys(top); fprintf(stdout, "\nEnter selection, or unmapped key to continue: "); fflush(stdout); set_term_quiet_input(&save); poll(&stdin_poll, 1, -1); c = getc(stdin); tcsetattr(0, TCSAFLUSH, &save); if (!perf_top__key_mapped(top, c)) return ret; } switch (c) { case 'd': prompt_integer(&top->delay_secs, "Enter display delay"); if (top->delay_secs < 1) top->delay_secs = 1; break; case 'e': prompt_integer(&top->print_entries, "Enter display entries (lines)"); if (top->print_entries == 0) { struct sigaction act = { .sa_sigaction = perf_top__sig_winch, .sa_flags = SA_SIGINFO, }; perf_top__sig_winch(SIGWINCH, NULL, top); sigaction(SIGWINCH, &act, NULL); } else { signal(SIGWINCH, SIG_DFL); } break; case 'E': if (top->evlist->nr_entries > 1) { /* Select 0 as the default event: */ int counter = 0; fprintf(stderr, "\nAvailable events:"); evlist__for_each_entry(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"); if (counter >= top->evlist->nr_entries) { top->sym_evsel = perf_evlist__first(top->evlist); fprintf(stderr, "Sorry, no such event, using %s.\n", perf_evsel__name(top->sym_evsel)); sleep(1); break; } evlist__for_each_entry(top->evlist, top->sym_evsel) if (top->sym_evsel->idx == counter) break; } else top->sym_evsel = perf_evlist__first(top->evlist); break; case 'f': prompt_integer(&top->count_filter, "Enter display event count filter"); break; case 'F': prompt_percent(&top->sym_pcnt_filter, "Enter details display event filter (percent)"); break; case 'K': top->hide_kernel_symbols = !top->hide_kernel_symbols; break; case 'q': case 'Q': printf("exiting.\n"); if (top->dump_symtab) perf_session__fprintf_dsos(top->session, stderr); ret = false; break; case 's': perf_top__prompt_symbol(top, "Enter details symbol"); break; case 'S': if (!top->sym_filter_entry) break; else { struct hist_entry *syme = top->sym_filter_entry; top->sym_filter_entry = NULL; __zero_source_counters(syme); } break; case 'U': top->hide_user_symbols = !top->hide_user_symbols; break; case 'z': top->zero = !top->zero; break; default: break; } return ret; }


Arnaldo Carvalho de Melo24148.01%1864.29%
Mike Galbraith19338.45%27.14%
Ingo Molnar509.96%310.71%
Tim Blechmann101.99%13.57%
Akihiro Nagai30.60%13.57%
Yanmin Zhang20.40%13.57%
Stéphane Eranian20.40%13.57%
Jiri Olsa10.20%13.57%

static void perf_top__sort_new_samples(void *arg) { struct perf_top *t = arg; struct perf_evsel *evsel = t->sym_evsel; struct hists *hists; perf_top__reset_sample_counters(t); if (t->evlist->selected != NULL) t->sym_evsel = t->evlist->selected; hists = evsel__hists(evsel); if (t->evlist->enabled) { if (t->zero) { hists__delete_entries(hists); } else { hists__decay_entries(hists, t->hide_user_symbols, t->hide_kernel_symbols); } } hists__collapse_resort(hists, NULL); perf_evsel__output_resort(evsel, NULL); }


Arnaldo Carvalho de Melo7262.07%444.44%
Namhyung Kim2824.14%222.22%
Jiri Olsa1210.34%111.11%
Ingo Molnar32.59%111.11%
Frédéric Weisbecker10.86%111.11%

static void *display_thread_tui(void *arg) { struct perf_evsel *pos; struct perf_top *top = arg; const char *help = "For a higher level overview, try: perf top --sort comm,dso"; struct hist_browser_timer hbt = { .timer = perf_top__sort_new_samples, .arg = top, .refresh = top->delay_secs, }; /* In order to read symbols from other namespaces perf to needs to call * setns(2). This isn't permitted if the struct_fs has multiple users. * unshare(2) the fs so that we may continue to setns into namespaces * that we're observing. */ unshare(CLONE_FS); perf_top__sort_new_samples(top); /* * Initialize the uid_filter_str, in the future the TUI will allow * Zooming in/out UIDs. For now juse use whatever the user passed * via --uid. */ evlist__for_each_entry(top->evlist, pos) { struct hists *hists = evsel__hists(pos); hists->uid_filter_str = top->; } perf_evlist__tui_browse_hists(top->evlist, help, &hbt, top->min_percent, &top->session->header.env); done = 1; return NULL; }


Arnaldo Carvalho de Melo8163.78%1062.50%
Namhyung Kim4031.50%531.25%
Krister Johansen64.72%16.25%

static void display_sig(int sig __maybe_unused) { done = 1; }


Jiri Olsa14100.00%1100.00%

static void display_setup_sig(void) { signal(SIGSEGV, sighandler_dump_stack); signal(SIGFPE, sighandler_dump_stack); signal(SIGINT, display_sig); signal(SIGQUIT, display_sig); signal(SIGTERM, display_sig); }


Jiri Olsa4195.35%150.00%
Arnaldo Carvalho de Melo24.65%150.00%

static void *display_thread(void *arg) { struct pollfd stdin_poll = { .fd = 0, .events = POLLIN }; struct termios save; struct perf_top *top = arg; int delay_msecs, c; /* In order to read symbols from other namespaces perf to needs to call * setns(2). This isn't permitted if the struct_fs has multiple users. * unshare(2) the fs so that we may continue to setns into namespaces * that we're observing. */ unshare(CLONE_FS); display_setup_sig(); pthread__unblock_sigwinch(); repeat: delay_msecs = top->delay_secs * MSEC_PER_SEC; set_term_quiet_input(&save); /* trash return*/ getc(stdin); while (!done) { perf_top__print_sym_table(top); /* * Either timeout expired or we got an EINTR due to SIGWINCH, * refresh screen in both cases. */ switch (poll(&stdin_poll, 1, delay_msecs)) { case 0: continue; case -1: if (errno<