cregit-Linux how code gets into the kernel

Release 4.10 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 <mingo@redhat.com>
 *               2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
 *
 * Improvements and fixes by:
 *
 *   Arjan van de Ven <arjan@linux.intel.com>
 *   Yanmin Zhang <yanmin.zhang@intel.com>
 *   Wu Fengguang <fengguang.wu@intel.com>
 *   Mike Galbraith <efault@gmx.de>
 *   Paul Mackerras <paulus@samba.org>
 *
 * 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/machine.h"
#include "util/session.h"
#include "util/symbol.h"
#include "util/thread.h"
#include "util/thread_map.h"
#include "util/top.h"
#include "util/util.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/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 <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>


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; }

Contributors

PersonTokensPropCommitsCommitProp
arnaldo carvalho de meloarnaldo carvalho de melo1773.91%480.00%
namhyung kimnamhyung kim626.09%120.00%
Total23100.00%5100.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); }

Contributors

PersonTokensPropCommitsCommitProp
arnaldo carvalho de meloarnaldo carvalho de melo3794.87%375.00%
irina tirdeairina tirdea25.13%125.00%
Total39100.00%4100.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->ms.map; /* * 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); 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; }

Contributors

PersonTokensPropCommitsCommitProp
arnaldo carvalho de meloarnaldo carvalho de melo19375.10%1470.00%
mike galbraithmike galbraith4617.90%15.00%
adrian hunteradrian hunter83.11%15.00%
ingo molnaringo molnar83.11%210.00%
jiri olsajiri olsa10.39%15.00%
kirill smelkovkirill smelkov10.39%15.00%
Total257100.00%20100.00%


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

Contributors

PersonTokensPropCommitsCommitProp
arnaldo carvalho de meloarnaldo carvalho de melo1555.56%266.67%
mike galbraithmike galbraith1244.44%133.33%
Total27100.00%3100.00%


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 linux-kernel@vger.kernel.org\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; }

Contributors

PersonTokensPropCommitsCommitProp
arnaldo carvalho de meloarnaldo carvalho de melo144100.00%1100.00%
Total144100.00%1100.00%


static void perf_top__record_precise_ip(struct perf_top *top, struct hist_entry *he, 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, 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->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); } pthread_mutex_lock(&he->hists->lock); } }

Contributors

PersonTokensPropCommitsCommitProp
arnaldo carvalho de meloarnaldo carvalho de melo12262.89%1168.75%
namhyung kimnamhyung kim4221.65%425.00%
mike galbraithmike galbraith3015.46%16.25%
Total194100.00%16100.00%


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->ms.map, 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); }

Contributors

PersonTokensPropCommitsCommitProp
arnaldo carvalho de meloarnaldo carvalho de melo12365.08%1090.91%
mike galbraithmike galbraith6634.92%19.09%
Total189100.00%11100.00%


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); }

Contributors

PersonTokensPropCommitsCommitProp
arnaldo carvalho de meloarnaldo carvalho de melo17970.75%1260.00%
namhyung kimnamhyung kim3513.83%525.00%
mike galbraithmike galbraith239.09%15.00%
jiri olsajiri olsa166.32%210.00%
Total253100.00%20100.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); }

Contributors

PersonTokensPropCommitsCommitProp
mike galbraithmike galbraith127100.00%1100.00%
Total127100.00%1100.00%


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

Contributors

PersonTokensPropCommitsCommitProp
mike galbraithmike galbraith4195.35%150.00%
arnaldo carvalho de meloarnaldo carvalho de melo24.65%150.00%
Total43100.00%2100.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); }

Contributors

PersonTokensPropCommitsCommitProp
arnaldo carvalho de meloarnaldo carvalho de melo11951.07%555.56%
mike galbraithmike galbraith11047.21%111.11%
ingo molnaringo molnar31.29%222.22%
kirill smelkovkirill smelkov10.43%111.11%
Total233100.00%9100.00%


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"); }

Contributors

PersonTokensPropCommitsCommitProp
mike galbraithmike galbraith10555.26%318.75%
arnaldo carvalho de meloarnaldo carvalho de melo6735.26%1062.50%
ingo molnaringo molnar178.95%212.50%
kirill smelkovkirill smelkov10.53%16.25%
Total190100.00%16100.00%


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; }

Contributors

PersonTokensPropCommitsCommitProp
mike galbraithmike galbraith4153.25%228.57%
arnaldo carvalho de meloarnaldo carvalho de melo2532.47%342.86%
kirill smelkovkirill smelkov911.69%114.29%
ingo molnaringo molnar22.60%114.29%
Total77100.00%7100.00%


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; }

Contributors

PersonTokensPropCommitsCommitProp
arnaldo carvalho de meloarnaldo carvalho de melo24148.01%1864.29%
mike galbraithmike galbraith19338.45%27.14%
ingo molnaringo molnar509.96%310.71%
tim blechmanntim blechmann101.99%13.57%
akihiro nagaiakihiro nagai30.60%13.57%
stephane eranianstephane eranian20.40%13.57%
yanmin zhangyanmin zhang20.40%13.57%
jiri olsajiri olsa10.20%13.57%
Total502100.00%28100.00%


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); }

Contributors

PersonTokensPropCommitsCommitProp
arnaldo carvalho de meloarnaldo carvalho de melo7262.07%440.00%
namhyung kimnamhyung kim2824.14%220.00%
jiri olsajiri olsa1210.34%110.00%
ingo molnaringo molnar32.59%220.00%
frederic weisbeckerfrederic weisbecker10.86%110.00%
Total116100.00%10100.00%


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, }; 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->record_opts.target.uid_str; } perf_evlist__tui_browse_hists(top->evlist, help, &hbt, top->min_percent, &top->session->header.env); done = 1; return NULL; }

Contributors

PersonTokensPropCommitsCommitProp
arnaldo carvalho de meloarnaldo carvalho de melo8166.94%1066.67%
namhyung kimnamhyung kim4033.06%533.33%
Total121100.00%15100.00%


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

Contributors

PersonTokensPropCommitsCommitProp
jiri olsajiri olsa14100.00%1100.00%
Total14100.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); }

Contributors

PersonTokensPropCommitsCommitProp
jiri olsajiri olsa4195.35%150.00%
arnaldo carvalho de meloarnaldo carvalho de melo24.65%150.00%
Total43100.00%2100.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; display_setup_sig(); pthread__unblock_sigwinch(); repeat: delay_msecs = top