Lines Matching refs:top

3  * builtin-top.c
5 * Builtin top command: Display a continuously updated profile of
39 #include "util/top.h"
94 static void perf_top__update_print_entries(struct perf_top *top)
96 top->print_entries = top->winsize.ws_row - HEADER_LINE_NR;
104 static void perf_top__resize(struct perf_top *top)
106 get_term_dimensions(&top->winsize);
107 perf_top__update_print_entries(top);
110 static int perf_top__parse_source(struct perf_top *top, struct hist_entry *he)
140 if (!symbol__hists(sym, top->evlist->core.nr_entries)) {
148 err = symbol__annotate(&he->ms, evsel, &top->annotation_opts, NULL);
150 top->sym_filter_entry = he;
194 static void perf_top__record_precise_ip(struct perf_top *top,
204 (top->sym_filter_entry == NULL ||
205 top->sym_filter_entry->ms.sym != sym)))
236 static void perf_top__show_details(struct perf_top *top)
238 struct hist_entry *he = top->sym_filter_entry;
259 printf("Showing %s for %s\n", evsel__name(top->sym_evsel), symbol->name);
260 printf(" Events Pcnt (>=%d%%)\n", top->annotation_opts.min_pcnt);
262 more = symbol__annotate_printf(&he->ms, top->sym_evsel, &top->annotation_opts);
264 if (top->evlist->enabled) {
265 if (top->zero)
266 symbol__annotate_zero_histogram(symbol, top->sym_evsel->idx);
268 symbol__annotate_decay_histogram(symbol, top->sym_evsel->idx);
315 static void perf_top__print_sym_table(struct perf_top *top)
319 const int win_width = top->winsize.ws_col - 1;
320 struct evsel *evsel = top->sym_evsel;
325 perf_top__header_snprintf(top, bf, sizeof(bf));
330 if (!top->record_opts.overwrite &&
341 if (top->sym_filter_entry) {
342 perf_top__show_details(top);
346 perf_top__resort_hists(top);
348 hists__output_recalc_col_len(hists, top->print_entries - printed);
350 hists__fprintf(hists, false, top->print_entries - printed, win_width,
351 top->min_percent, stdout, !symbol_conf.use_callchain);
389 static void perf_top__prompt_symbol(struct perf_top *top, const char *msg)
392 struct hist_entry *syme = top->sym_filter_entry, *n, *found = NULL;
393 struct hists *hists = evsel__hists(top->sym_evsel);
400 top->sym_filter_entry = NULL;
425 perf_top__parse_source(top, found);
431 static void perf_top__print_mapped_keys(struct perf_top *top)
435 if (top->sym_filter_entry) {
436 struct symbol *sym = top->sym_filter_entry->ms.sym;
441 fprintf(stdout, "\t[d] display refresh delay. \t(%d)\n", top->delay_secs);
442 fprintf(stdout, "\t[e] display entries (lines). \t(%d)\n", top->print_entries);
444 if (top->evlist->core.nr_entries > 1)
445 fprintf(stdout, "\t[E] active event counter. \t(%s)\n", evsel__name(top->sym_evsel));
447 fprintf(stdout, "\t[f] profile display filter (count). \t(%d)\n", top->count_filter);
449 fprintf(stdout, "\t[F] annotate display filter (percent). \t(%d%%)\n", top->annotation_opts.min_pcnt);
455 top->hide_kernel_symbols ? "yes" : "no");
458 top->hide_user_symbols ? "yes" : "no");
459 fprintf(stdout, "\t[z] toggle sample zeroing. \t(%d)\n", top->zero ? 1 : 0);
463 static int perf_top__key_mapped(struct perf_top *top, int c)
479 return top->evlist->core.nr_entries > 1 ? 1 : 0;
487 static bool perf_top__handle_keypress(struct perf_top *top, int c)
491 if (!perf_top__key_mapped(top, c)) {
495 perf_top__print_mapped_keys(top);
505 if (!perf_top__key_mapped(top, c))
511 prompt_integer(&top->delay_secs, "Enter display delay");
512 if (top->delay_secs < 1)
513 top->delay_secs = 1;
516 prompt_integer(&top->print_entries, "Enter display entries (lines)");
517 if (top->print_entries == 0) {
518 perf_top__resize(top);
525 if (top->evlist->core.nr_entries > 1) {
531 evlist__for_each_entry(top->evlist, top->sym_evsel)
532 fprintf(stderr, "\n\t%d %s", top->sym_evsel->idx, evsel__name(top->sym_evsel));
536 if (counter >= top->evlist->core.nr_entries) {
537 top->sym_evsel = evlist__first(top->evlist);
538 fprintf(stderr, "Sorry, no such event, using %s.\n", evsel__name(top->sym_evsel));
542 evlist__for_each_entry(top->evlist, top->sym_evsel)
543 if (top->sym_evsel->idx == counter)
546 top->sym_evsel = evlist__first(top->evlist);
549 prompt_integer(&top->count_filter, "Enter display event count filter");
552 prompt_percent(&top->annotation_opts.min_pcnt,
556 top->hide_kernel_symbols = !top->hide_kernel_symbols;
561 if (top->dump_symtab)
562 perf_session__fprintf_dsos(top->session, stderr);
566 perf_top__prompt_symbol(top, "Enter details symbol");
569 if (!top->sym_filter_entry)
572 struct hist_entry *syme = top->sym_filter_entry;
574 top->sym_filter_entry = NULL;
579 top->hide_user_symbols = !top->hide_user_symbols;
582 top->zero = !top->zero;
613 struct perf_top *top = arg;
614 const char *help = "For a higher level overview, try: perf top --sort comm,dso";
617 .arg = top,
618 .refresh = top->delay_secs,
629 prctl(PR_SET_NAME, "perf-top-UI", 0, 0, 0);
632 perf_top__sort_new_samples(top);
639 evlist__for_each_entry(top->evlist, pos) {
641 hists->uid_filter_str = top->record_opts.target.uid_str;
644 ret = perf_evlist__tui_browse_hists(top->evlist, help, &hbt,
645 top->min_percent,
646 &top->session->header.env,
647 !top->record_opts.overwrite,
648 &top->annotation_opts);
651 top->zero = true;
677 struct perf_top *top = arg;
687 prctl(PR_SET_NAME, "perf-top-UI", 0, 0, 0);
692 delay_msecs = top->delay_secs * MSEC_PER_SEC;
700 perf_top__print_sym_table(top);
716 if (perf_top__handle_keypress(top, c))
730 struct perf_top *top = arg;
735 perf_top__record_precise_ip(top, he, iter->sample, evsel, al->addr);
738 !(top->record_opts.branch_stack & PERF_SAMPLE_BRANCH_ANY),
749 struct perf_top *top = container_of(tool, struct perf_top, tool);
769 top->session->evlist->stats.nr_unprocessable_samples++);
774 top->exact_samples++;
779 if (top->stitch_lbr)
785 if (!perf_evlist__exclude_kernel(top->session->evlist)) {
811 if (!machine->kptr_restrict_warned && !top->vmlinux_warned &&
825 top->vmlinux_warned = true;
844 err = hist_entry_iter__add(&iter, &al, top->max_stack, top);
855 perf_top__process_lost(struct perf_top *top, union perf_event *event,
860 top->lost += event->lost.lost;
861 top->lost_total += event->lost.lost;
866 perf_top__process_lost_samples(struct perf_top *top,
872 top->lost += event->lost_samples.lost;
873 top->lost_total += event->lost_samples.lost;
879 static void perf_top__mmap_read_idx(struct perf_top *top, int idx)
881 struct record_opts *opts = &top->record_opts;
882 struct evlist *evlist = top->evlist;
897 ret = ordered_events__queue(top->qe.in, event, last_timestamp, 0);
903 if (top->qe.rotate) {
904 pthread_mutex_lock(&top->qe.mutex);
905 top->qe.rotate = false;
906 pthread_cond_signal(&top->qe.cond);
907 pthread_mutex_unlock(&top->qe.mutex);
914 static void perf_top__mmap_read(struct perf_top *top)
916 bool overwrite = top->record_opts.overwrite;
917 struct evlist *evlist = top->evlist;
923 for (i = 0; i < top->evlist->core.nr_mmaps; i++)
924 perf_top__mmap_read_idx(top, i);
934 * perf top should support consistent term for all events.
949 static int perf_top__overwrite_check(struct perf_top *top)
951 struct record_opts *opts = &top->record_opts;
952 struct evlist *evlist = top->evlist;
994 static int perf_top_overwrite_fallback(struct perf_top *top,
997 struct record_opts *opts = &top->record_opts;
998 struct evlist *evlist = top->evlist;
1015 static int perf_top__start_counters(struct perf_top *top)
1019 struct evlist *evlist = top->evlist;
1020 struct record_opts *opts = &top->record_opts;
1022 if (perf_top__overwrite_check(top)) {
1023 ui__error("perf top only support consistent per-event "
1032 if (evsel__open(counter, top->evlist->core.cpus,
1033 top->evlist->core.threads) < 0) {
1037 * Because perf top is the only tool which has
1046 perf_top_overwrite_fallback(top, counter))
1085 static struct ordered_events *rotate_queues(struct perf_top *top)
1087 struct ordered_events *in = top->qe.in;
1089 if (top->qe.in == &top->qe.data[1])
1090 top->qe.in = &top->qe.data[0];
1092 top->qe.in = &top->qe.data[1];
1099 struct perf_top *top = arg;
1102 struct ordered_events *out, *in = top->qe.in;
1109 out = rotate_queues(top);
1111 pthread_mutex_lock(&top->qe.mutex);
1112 top->qe.rotate = true;
1113 pthread_cond_wait(&top->qe.cond, &top->qe.mutex);
1114 pthread_mutex_unlock(&top->qe.mutex);
1124 * Allow only 'top->delay_secs' seconds behind samples.
1126 static int should_drop(struct ordered_event *qevent, struct perf_top *top)
1134 delay_timestamp = qevent->timestamp + top->delay_secs * NSEC_PER_SEC;
1141 struct perf_top *top = qe->data;
1142 struct evlist *evlist = top->evlist;
1143 struct perf_session *session = top->session;
1150 if (should_drop(qevent, top)) {
1151 top->drop++;
1152 top->drop_total++;
1166 if (evswitch__discard(&top->evswitch, evsel))
1168 ++top->samples;
1173 ++top->us_samples;
1174 if (top->hide_user_symbols)
1179 ++top->kernel_samples;
1180 if (top->hide_kernel_symbols)
1185 ++top->guest_kernel_samples;
1190 ++top->guest_us_samples;
1204 perf_event__process_sample(&top->tool, event, evsel,
1207 perf_top__process_lost(top, event, evsel);
1209 perf_top__process_lost_samples(top, event, evsel);
1221 static void init_process_thread(struct perf_top *top)
1223 ordered_events__init(&top->qe.data[0], deliver_event, top);
1224 ordered_events__init(&top->qe.data[1], deliver_event, top);
1225 ordered_events__set_copy_on_queue(&top->qe.data[0], true);
1226 ordered_events__set_copy_on_queue(&top->qe.data[1], true);
1227 top->qe.in = &top->qe.data[0];
1228 pthread_mutex_init(&top->qe.mutex, NULL);
1229 pthread_cond_init(&top->qe.cond, NULL);
1232 static int __cmd_top(struct perf_top *top)
1234 struct record_opts *opts = &top->record_opts;
1238 if (!top->annotation_opts.objdump_path) {
1239 ret = perf_env__lookup_objdump(&top->session->header.env,
1240 &top->annotation_opts.objdump_path);
1249 if (perf_session__register_idle_thread(top->session) < 0)
1252 if (top->nr_threads_synthesize > 1)
1255 init_process_thread(top);
1258 top->tool.namespace_events = true;
1261 top->tool.cgroup_events = true;
1268 ret = perf_event__synthesize_bpf_events(top->session, perf_event__process,
1269 &top->session->machines.host,
1270 &top->record_opts);
1274 ret = perf_event__synthesize_cgroups(&top->tool, perf_event__process,
1275 &top->session->machines.host);
1279 machine__synthesize_threads(&top->session->machines.host, &opts->target,
1280 top->evlist->core.threads, false,
1281 top->nr_threads_synthesize);
1283 if (top->nr_threads_synthesize > 1)
1297 ret = perf_top__start_counters(top);
1301 top->session->evlist = top->evlist;
1302 perf_session__set_id_hdr_size(top->session);
1309 * XXX 'top' still doesn't start workloads like record, trace, but should,
1313 evlist__enable(top->evlist);
1316 if (pthread_create(&thread_process, NULL, process_thread, top)) {
1322 display_thread), top)) {
1327 if (top->realtime_prio) {
1330 param.sched_priority = top->realtime_prio;
1338 evlist__poll(top->evlist, 100);
1340 perf_top__mmap_read(top);
1343 u64 hits = top->samples;
1345 perf_top__mmap_read(top);
1347 if (opts->overwrite || (hits == top->samples))
1348 ret = evlist__poll(top->evlist, 100);
1351 perf_top__resize(top);
1360 pthread_cond_signal(&top->qe.cond);
1394 if (!strcmp(var, "top.call-graph")) {
1398 if (!strcmp(var, "top.children")) {
1410 struct perf_top *top = opt->value;
1412 top->min_percent = strtof(arg, NULL);
1422 struct perf_top top = {
1448 struct record_opts *opts = &top.record_opts;
1451 OPT_CALLBACK('e', "event", &top.evlist, "event",
1469 OPT_BOOLEAN('K', "hide_kernel_symbols", &top.hide_kernel_symbols,
1474 OPT_INTEGER('r', "realtime", &top.realtime_prio,
1476 OPT_INTEGER('d', "delay", &top.delay_secs,
1478 OPT_BOOLEAN('D', "dump-symtab", &top.dump_symtab,
1480 OPT_INTEGER('f', "count-filter", &top.count_filter,
1486 OPT_STRING(0, "sym-annotate", &top.sym_filter, "symbol name",
1488 OPT_BOOLEAN('z', "zero", &top.zero, "zero history across updates"),
1489 OPT_CALLBACK('F', "freq", &top.record_opts, "freq or 'max'",
1492 OPT_INTEGER('E', "entries", &top.print_entries,
1494 OPT_BOOLEAN('U', "hide_user_symbols", &top.hide_user_symbols,
1496 OPT_BOOLEAN(0, "tui", &top.use_tui, "Use the TUI interface"),
1497 OPT_BOOLEAN(0, "stdio", &top.use_stdio, "Use the stdio interface"),
1515 OPT_INTEGER(0, "max-stack", &top.max_stack,
1529 OPT_BOOLEAN(0, "source", &top.annotation_opts.annotate_src,
1531 OPT_BOOLEAN(0, "asm-raw", &top.annotation_opts.show_asm_raw,
1535 OPT_BOOLEAN(0, "no-bpf-event", &top.record_opts.no_bpf_event, "do not record bpf events"),
1536 OPT_STRING(0, "objdump", &top.annotation_opts.objdump_path, "path",
1538 OPT_STRING('M', "disassembler-style", &top.annotation_opts.disassembler_style, "disassembler style",
1540 OPT_STRING(0, "prefix", &top.annotation_opts.prefix, "prefix",
1542 OPT_STRING(0, "prefix-strip", &top.annotation_opts.prefix_strip, "N",
1545 OPT_CALLBACK(0, "percent-limit", &top, "percent",
1564 OPT_BOOLEAN(0, "overwrite", &top.record_opts.overwrite,
1567 OPT_UINTEGER(0, "num-thread-synthesize", &top.nr_threads_synthesize,
1577 OPT_BOOLEAN(0, "stitch-lbr", &top.stitch_lbr,
1580 OPT_CALLBACK(0, "pfm-events", &top.evlist, "event",
1584 OPTS_EVSWITCH(&top.evswitch),
1588 "perf top [<options>]",
1596 top.annotation_opts.min_pcnt = 5;
1597 top.annotation_opts.context = 4;
1599 top.evlist = evlist__new();
1600 if (top.evlist == NULL)
1603 status = perf_config(perf_top_config, &top);
1620 top.evlist->env = &perf_env;
1626 if (annotate_check_args(&top.annotation_opts) < 0)
1629 if (!top.evlist->core.nr_entries &&
1630 evlist__add_default(top.evlist) < 0) {
1635 status = evswitch__init(&top.evswitch, top.evlist, stderr);
1652 if (top.stitch_lbr && !(callchain_param.record_mode == CALLCHAIN_LBR)) {
1664 if (top.use_stdio)
1666 else if (top.use_tui)
1671 if (setup_sorting(top.evlist) < 0) {
1700 if (perf_evlist__create_maps(top.evlist, target) < 0) {
1706 if (top.delay_secs < 1)
1707 top.delay_secs = 1;
1714 top.sym_evsel = evlist__first(top.evlist);
1728 annotation_config__init(&top.annotation_opts);
1737 get_term_dimensions(&top.winsize);
1738 if (top.print_entries == 0) {
1739 perf_top__update_print_entries(&top);
1743 top.session = perf_session__new(NULL, false, NULL);
1744 if (IS_ERR(top.session)) {
1745 status = PTR_ERR(top.session);
1746 top.session = NULL;
1751 if (!top.record_opts.no_bpf_event) {
1752 top.sb_evlist = evlist__new();
1754 if (top.sb_evlist == NULL) {
1759 if (evlist__add_bpf_sb_event(top.sb_evlist, &perf_env)) {
1766 if (perf_evlist__start_sb_thread(top.sb_evlist, target)) {
1771 status = __cmd_top(&top);
1774 perf_evlist__stop_sb_thread(top.sb_evlist);
1777 evlist__delete(top.evlist);
1778 perf_session__delete(top.session);