0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019 #include "builtin.h"
0020
0021 #include "perf.h"
0022
0023 #include "util/annotate.h"
0024 #include "util/bpf-event.h"
0025 #include "util/cgroup.h"
0026 #include "util/config.h"
0027 #include "util/color.h"
0028 #include "util/dso.h"
0029 #include "util/evlist.h"
0030 #include "util/evsel.h"
0031 #include "util/evsel_config.h"
0032 #include "util/event.h"
0033 #include "util/machine.h"
0034 #include "util/map.h"
0035 #include "util/mmap.h"
0036 #include "util/session.h"
0037 #include "util/thread.h"
0038 #include "util/symbol.h"
0039 #include "util/synthetic-events.h"
0040 #include "util/top.h"
0041 #include "util/util.h"
0042 #include <linux/rbtree.h>
0043 #include <subcmd/parse-options.h>
0044 #include "util/parse-events.h"
0045 #include "util/callchain.h"
0046 #include "util/cpumap.h"
0047 #include "util/sort.h"
0048 #include "util/string2.h"
0049 #include "util/term.h"
0050 #include "util/intlist.h"
0051 #include "util/parse-branch-options.h"
0052 #include "arch/common.h"
0053 #include "ui/ui.h"
0054
0055 #include "util/debug.h"
0056 #include "util/ordered-events.h"
0057 #include "util/pfm.h"
0058
0059 #include <assert.h>
0060 #include <elf.h>
0061 #include <fcntl.h>
0062
0063 #include <stdio.h>
0064 #include <termios.h>
0065 #include <unistd.h>
0066 #include <inttypes.h>
0067
0068 #include <errno.h>
0069 #include <time.h>
0070 #include <sched.h>
0071 #include <signal.h>
0072
0073 #include <sys/syscall.h>
0074 #include <sys/ioctl.h>
0075 #include <poll.h>
0076 #include <sys/prctl.h>
0077 #include <sys/wait.h>
0078 #include <sys/uio.h>
0079 #include <sys/utsname.h>
0080 #include <sys/mman.h>
0081
0082 #include <linux/stringify.h>
0083 #include <linux/time64.h>
0084 #include <linux/types.h>
0085 #include <linux/err.h>
0086
0087 #include <linux/ctype.h>
0088 #include <perf/mmap.h>
0089
0090 static volatile int done;
0091 static volatile int resize;
0092
0093 #define HEADER_LINE_NR 5
0094
0095 static void perf_top__update_print_entries(struct perf_top *top)
0096 {
0097 top->print_entries = top->winsize.ws_row - HEADER_LINE_NR;
0098 }
0099
0100 static void winch_sig(int sig __maybe_unused)
0101 {
0102 resize = 1;
0103 }
0104
0105 static void perf_top__resize(struct perf_top *top)
0106 {
0107 get_term_dimensions(&top->winsize);
0108 perf_top__update_print_entries(top);
0109 }
0110
0111 static int perf_top__parse_source(struct perf_top *top, struct hist_entry *he)
0112 {
0113 struct evsel *evsel;
0114 struct symbol *sym;
0115 struct annotation *notes;
0116 struct map *map;
0117 int err = -1;
0118
0119 if (!he || !he->ms.sym)
0120 return -1;
0121
0122 evsel = hists_to_evsel(he->hists);
0123
0124 sym = he->ms.sym;
0125 map = he->ms.map;
0126
0127
0128
0129
0130 if (map->dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS &&
0131 !dso__is_kcore(map->dso)) {
0132 pr_err("Can't annotate %s: No vmlinux file was found in the "
0133 "path\n", sym->name);
0134 sleep(1);
0135 return -1;
0136 }
0137
0138 notes = symbol__annotation(sym);
0139 pthread_mutex_lock(¬es->lock);
0140
0141 if (!symbol__hists(sym, top->evlist->core.nr_entries)) {
0142 pthread_mutex_unlock(¬es->lock);
0143 pr_err("Not enough memory for annotating '%s' symbol!\n",
0144 sym->name);
0145 sleep(1);
0146 return err;
0147 }
0148
0149 err = symbol__annotate(&he->ms, evsel, &top->annotation_opts, NULL);
0150 if (err == 0) {
0151 top->sym_filter_entry = he;
0152 } else {
0153 char msg[BUFSIZ];
0154 symbol__strerror_disassemble(&he->ms, err, msg, sizeof(msg));
0155 pr_err("Couldn't annotate %s: %s\n", sym->name, msg);
0156 }
0157
0158 pthread_mutex_unlock(¬es->lock);
0159 return err;
0160 }
0161
0162 static void __zero_source_counters(struct hist_entry *he)
0163 {
0164 struct symbol *sym = he->ms.sym;
0165 symbol__annotate_zero_histograms(sym);
0166 }
0167
0168 static void ui__warn_map_erange(struct map *map, struct symbol *sym, u64 ip)
0169 {
0170 struct utsname uts;
0171 int err = uname(&uts);
0172
0173 ui__warning("Out of bounds address found:\n\n"
0174 "Addr: %" PRIx64 "\n"
0175 "DSO: %s %c\n"
0176 "Map: %" PRIx64 "-%" PRIx64 "\n"
0177 "Symbol: %" PRIx64 "-%" PRIx64 " %c %s\n"
0178 "Arch: %s\n"
0179 "Kernel: %s\n"
0180 "Tools: %s\n\n"
0181 "Not all samples will be on the annotation output.\n\n"
0182 "Please report to linux-kernel@vger.kernel.org\n",
0183 ip, map->dso->long_name, dso__symtab_origin(map->dso),
0184 map->start, map->end, sym->start, sym->end,
0185 sym->binding == STB_GLOBAL ? 'g' :
0186 sym->binding == STB_LOCAL ? 'l' : 'w', sym->name,
0187 err ? "[unknown]" : uts.machine,
0188 err ? "[unknown]" : uts.release, perf_version_string);
0189 if (use_browser <= 0)
0190 sleep(5);
0191
0192 map->erange_warned = true;
0193 }
0194
0195 static void perf_top__record_precise_ip(struct perf_top *top,
0196 struct hist_entry *he,
0197 struct perf_sample *sample,
0198 struct evsel *evsel, u64 ip)
0199 {
0200 struct annotation *notes;
0201 struct symbol *sym = he->ms.sym;
0202 int err = 0;
0203
0204 if (sym == NULL || (use_browser == 0 &&
0205 (top->sym_filter_entry == NULL ||
0206 top->sym_filter_entry->ms.sym != sym)))
0207 return;
0208
0209 notes = symbol__annotation(sym);
0210
0211 if (pthread_mutex_trylock(¬es->lock))
0212 return;
0213
0214 err = hist_entry__inc_addr_samples(he, sample, evsel, ip);
0215
0216 pthread_mutex_unlock(¬es->lock);
0217
0218 if (unlikely(err)) {
0219
0220
0221
0222
0223 pthread_mutex_unlock(&he->hists->lock);
0224
0225 if (err == -ERANGE && !he->ms.map->erange_warned)
0226 ui__warn_map_erange(he->ms.map, sym, ip);
0227 else if (err == -ENOMEM) {
0228 pr_err("Not enough memory for annotating '%s' symbol!\n",
0229 sym->name);
0230 sleep(1);
0231 }
0232
0233 pthread_mutex_lock(&he->hists->lock);
0234 }
0235 }
0236
0237 static void perf_top__show_details(struct perf_top *top)
0238 {
0239 struct hist_entry *he = top->sym_filter_entry;
0240 struct evsel *evsel;
0241 struct annotation *notes;
0242 struct symbol *symbol;
0243 int more;
0244
0245 if (!he)
0246 return;
0247
0248 evsel = hists_to_evsel(he->hists);
0249
0250 symbol = he->ms.sym;
0251 notes = symbol__annotation(symbol);
0252
0253 pthread_mutex_lock(¬es->lock);
0254
0255 symbol__calc_percent(symbol, evsel);
0256
0257 if (notes->src == NULL)
0258 goto out_unlock;
0259
0260 printf("Showing %s for %s\n", evsel__name(top->sym_evsel), symbol->name);
0261 printf(" Events Pcnt (>=%d%%)\n", top->annotation_opts.min_pcnt);
0262
0263 more = symbol__annotate_printf(&he->ms, top->sym_evsel, &top->annotation_opts);
0264
0265 if (top->evlist->enabled) {
0266 if (top->zero)
0267 symbol__annotate_zero_histogram(symbol, top->sym_evsel->core.idx);
0268 else
0269 symbol__annotate_decay_histogram(symbol, top->sym_evsel->core.idx);
0270 }
0271 if (more != 0)
0272 printf("%d lines not displayed, maybe increase display entries [e]\n", more);
0273 out_unlock:
0274 pthread_mutex_unlock(¬es->lock);
0275 }
0276
0277 static void perf_top__resort_hists(struct perf_top *t)
0278 {
0279 struct evlist *evlist = t->evlist;
0280 struct evsel *pos;
0281
0282 evlist__for_each_entry(evlist, pos) {
0283 struct hists *hists = evsel__hists(pos);
0284
0285
0286
0287
0288
0289 hists__unlink(hists);
0290
0291 if (evlist->enabled) {
0292 if (t->zero) {
0293 hists__delete_entries(hists);
0294 } else {
0295 hists__decay_entries(hists, t->hide_user_symbols,
0296 t->hide_kernel_symbols);
0297 }
0298 }
0299
0300 hists__collapse_resort(hists, NULL);
0301
0302
0303 if (symbol_conf.event_group && !evsel__is_group_leader(pos)) {
0304 struct hists *leader_hists = evsel__hists(evsel__leader(pos));
0305
0306 hists__match(leader_hists, hists);
0307 hists__link(leader_hists, hists);
0308 }
0309 }
0310
0311 evlist__for_each_entry(evlist, pos) {
0312 evsel__output_resort(pos, NULL);
0313 }
0314 }
0315
0316 static void perf_top__print_sym_table(struct perf_top *top)
0317 {
0318 char bf[160];
0319 int printed = 0;
0320 const int win_width = top->winsize.ws_col - 1;
0321 struct evsel *evsel = top->sym_evsel;
0322 struct hists *hists = evsel__hists(evsel);
0323
0324 puts(CONSOLE_CLEAR);
0325
0326 perf_top__header_snprintf(top, bf, sizeof(bf));
0327 printf("%s\n", bf);
0328
0329 printf("%-*.*s\n", win_width, win_width, graph_dotted_line);
0330
0331 if (!top->record_opts.overwrite &&
0332 (top->evlist->stats.nr_lost_warned !=
0333 top->evlist->stats.nr_events[PERF_RECORD_LOST])) {
0334 top->evlist->stats.nr_lost_warned =
0335 top->evlist->stats.nr_events[PERF_RECORD_LOST];
0336 color_fprintf(stdout, PERF_COLOR_RED,
0337 "WARNING: LOST %d chunks, Check IO/CPU overload",
0338 top->evlist->stats.nr_lost_warned);
0339 ++printed;
0340 }
0341
0342 if (top->sym_filter_entry) {
0343 perf_top__show_details(top);
0344 return;
0345 }
0346
0347 perf_top__resort_hists(top);
0348
0349 hists__output_recalc_col_len(hists, top->print_entries - printed);
0350 putchar('\n');
0351 hists__fprintf(hists, false, top->print_entries - printed, win_width,
0352 top->min_percent, stdout, !symbol_conf.use_callchain);
0353 }
0354
0355 static void prompt_integer(int *target, const char *msg)
0356 {
0357 char *buf = malloc(0), *p;
0358 size_t dummy = 0;
0359 int tmp;
0360
0361 fprintf(stdout, "\n%s: ", msg);
0362 if (getline(&buf, &dummy, stdin) < 0)
0363 return;
0364
0365 p = strchr(buf, '\n');
0366 if (p)
0367 *p = 0;
0368
0369 p = buf;
0370 while(*p) {
0371 if (!isdigit(*p))
0372 goto out_free;
0373 p++;
0374 }
0375 tmp = strtoul(buf, NULL, 10);
0376 *target = tmp;
0377 out_free:
0378 free(buf);
0379 }
0380
0381 static void prompt_percent(int *target, const char *msg)
0382 {
0383 int tmp = 0;
0384
0385 prompt_integer(&tmp, msg);
0386 if (tmp >= 0 && tmp <= 100)
0387 *target = tmp;
0388 }
0389
0390 static void perf_top__prompt_symbol(struct perf_top *top, const char *msg)
0391 {
0392 char *buf = malloc(0), *p;
0393 struct hist_entry *syme = top->sym_filter_entry, *n, *found = NULL;
0394 struct hists *hists = evsel__hists(top->sym_evsel);
0395 struct rb_node *next;
0396 size_t dummy = 0;
0397
0398
0399 if (syme) {
0400 __zero_source_counters(syme);
0401 top->sym_filter_entry = NULL;
0402 }
0403
0404 fprintf(stdout, "\n%s: ", msg);
0405 if (getline(&buf, &dummy, stdin) < 0)
0406 goto out_free;
0407
0408 p = strchr(buf, '\n');
0409 if (p)
0410 *p = 0;
0411
0412 next = rb_first_cached(&hists->entries);
0413 while (next) {
0414 n = rb_entry(next, struct hist_entry, rb_node);
0415 if (n->ms.sym && !strcmp(buf, n->ms.sym->name)) {
0416 found = n;
0417 break;
0418 }
0419 next = rb_next(&n->rb_node);
0420 }
0421
0422 if (!found) {
0423 fprintf(stderr, "Sorry, %s is not active.\n", buf);
0424 sleep(1);
0425 } else
0426 perf_top__parse_source(top, found);
0427
0428 out_free:
0429 free(buf);
0430 }
0431
0432 static void perf_top__print_mapped_keys(struct perf_top *top)
0433 {
0434 char *name = NULL;
0435
0436 if (top->sym_filter_entry) {
0437 struct symbol *sym = top->sym_filter_entry->ms.sym;
0438 name = sym->name;
0439 }
0440
0441 fprintf(stdout, "\nMapped keys:\n");
0442 fprintf(stdout, "\t[d] display refresh delay. \t(%d)\n", top->delay_secs);
0443 fprintf(stdout, "\t[e] display entries (lines). \t(%d)\n", top->print_entries);
0444
0445 if (top->evlist->core.nr_entries > 1)
0446 fprintf(stdout, "\t[E] active event counter. \t(%s)\n", evsel__name(top->sym_evsel));
0447
0448 fprintf(stdout, "\t[f] profile display filter (count). \t(%d)\n", top->count_filter);
0449
0450 fprintf(stdout, "\t[F] annotate display filter (percent). \t(%d%%)\n", top->annotation_opts.min_pcnt);
0451 fprintf(stdout, "\t[s] annotate symbol. \t(%s)\n", name?: "NULL");
0452 fprintf(stdout, "\t[S] stop annotation.\n");
0453
0454 fprintf(stdout,
0455 "\t[K] hide kernel symbols. \t(%s)\n",
0456 top->hide_kernel_symbols ? "yes" : "no");
0457 fprintf(stdout,
0458 "\t[U] hide user symbols. \t(%s)\n",
0459 top->hide_user_symbols ? "yes" : "no");
0460 fprintf(stdout, "\t[z] toggle sample zeroing. \t(%d)\n", top->zero ? 1 : 0);
0461 fprintf(stdout, "\t[qQ] quit.\n");
0462 }
0463
0464 static int perf_top__key_mapped(struct perf_top *top, int c)
0465 {
0466 switch (c) {
0467 case 'd':
0468 case 'e':
0469 case 'f':
0470 case 'z':
0471 case 'q':
0472 case 'Q':
0473 case 'K':
0474 case 'U':
0475 case 'F':
0476 case 's':
0477 case 'S':
0478 return 1;
0479 case 'E':
0480 return top->evlist->core.nr_entries > 1 ? 1 : 0;
0481 default:
0482 break;
0483 }
0484
0485 return 0;
0486 }
0487
0488 static bool perf_top__handle_keypress(struct perf_top *top, int c)
0489 {
0490 bool ret = true;
0491
0492 if (!perf_top__key_mapped(top, c)) {
0493 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
0494 struct termios save;
0495
0496 perf_top__print_mapped_keys(top);
0497 fprintf(stdout, "\nEnter selection, or unmapped key to continue: ");
0498 fflush(stdout);
0499
0500 set_term_quiet_input(&save);
0501
0502 poll(&stdin_poll, 1, -1);
0503 c = getc(stdin);
0504
0505 tcsetattr(0, TCSAFLUSH, &save);
0506 if (!perf_top__key_mapped(top, c))
0507 return ret;
0508 }
0509
0510 switch (c) {
0511 case 'd':
0512 prompt_integer(&top->delay_secs, "Enter display delay");
0513 if (top->delay_secs < 1)
0514 top->delay_secs = 1;
0515 break;
0516 case 'e':
0517 prompt_integer(&top->print_entries, "Enter display entries (lines)");
0518 if (top->print_entries == 0) {
0519 perf_top__resize(top);
0520 signal(SIGWINCH, winch_sig);
0521 } else {
0522 signal(SIGWINCH, SIG_DFL);
0523 }
0524 break;
0525 case 'E':
0526 if (top->evlist->core.nr_entries > 1) {
0527
0528 int counter = 0;
0529
0530 fprintf(stderr, "\nAvailable events:");
0531
0532 evlist__for_each_entry(top->evlist, top->sym_evsel)
0533 fprintf(stderr, "\n\t%d %s", top->sym_evsel->core.idx, evsel__name(top->sym_evsel));
0534
0535 prompt_integer(&counter, "Enter details event counter");
0536
0537 if (counter >= top->evlist->core.nr_entries) {
0538 top->sym_evsel = evlist__first(top->evlist);
0539 fprintf(stderr, "Sorry, no such event, using %s.\n", evsel__name(top->sym_evsel));
0540 sleep(1);
0541 break;
0542 }
0543 evlist__for_each_entry(top->evlist, top->sym_evsel)
0544 if (top->sym_evsel->core.idx == counter)
0545 break;
0546 } else
0547 top->sym_evsel = evlist__first(top->evlist);
0548 break;
0549 case 'f':
0550 prompt_integer(&top->count_filter, "Enter display event count filter");
0551 break;
0552 case 'F':
0553 prompt_percent(&top->annotation_opts.min_pcnt,
0554 "Enter details display event filter (percent)");
0555 break;
0556 case 'K':
0557 top->hide_kernel_symbols = !top->hide_kernel_symbols;
0558 break;
0559 case 'q':
0560 case 'Q':
0561 printf("exiting.\n");
0562 if (top->dump_symtab)
0563 perf_session__fprintf_dsos(top->session, stderr);
0564 ret = false;
0565 break;
0566 case 's':
0567 perf_top__prompt_symbol(top, "Enter details symbol");
0568 break;
0569 case 'S':
0570 if (!top->sym_filter_entry)
0571 break;
0572 else {
0573 struct hist_entry *syme = top->sym_filter_entry;
0574
0575 top->sym_filter_entry = NULL;
0576 __zero_source_counters(syme);
0577 }
0578 break;
0579 case 'U':
0580 top->hide_user_symbols = !top->hide_user_symbols;
0581 break;
0582 case 'z':
0583 top->zero = !top->zero;
0584 break;
0585 default:
0586 break;
0587 }
0588
0589 return ret;
0590 }
0591
0592 static void perf_top__sort_new_samples(void *arg)
0593 {
0594 struct perf_top *t = arg;
0595
0596 if (t->evlist->selected != NULL)
0597 t->sym_evsel = t->evlist->selected;
0598
0599 perf_top__resort_hists(t);
0600
0601 if (t->lost || t->drop)
0602 pr_warning("Too slow to read ring buffer (change period (-c/-F) or limit CPUs (-C)\n");
0603 }
0604
0605 static void stop_top(void)
0606 {
0607 session_done = 1;
0608 done = 1;
0609 }
0610
0611 static void *display_thread_tui(void *arg)
0612 {
0613 struct evsel *pos;
0614 struct perf_top *top = arg;
0615 const char *help = "For a higher level overview, try: perf top --sort comm,dso";
0616 struct hist_browser_timer hbt = {
0617 .timer = perf_top__sort_new_samples,
0618 .arg = top,
0619 .refresh = top->delay_secs,
0620 };
0621 int ret;
0622
0623
0624
0625
0626
0627
0628 unshare(CLONE_FS);
0629
0630 prctl(PR_SET_NAME, "perf-top-UI", 0, 0, 0);
0631
0632 repeat:
0633 perf_top__sort_new_samples(top);
0634
0635
0636
0637
0638
0639
0640 evlist__for_each_entry(top->evlist, pos) {
0641 struct hists *hists = evsel__hists(pos);
0642 hists->uid_filter_str = top->record_opts.target.uid_str;
0643 }
0644
0645 ret = evlist__tui_browse_hists(top->evlist, help, &hbt, top->min_percent,
0646 &top->session->header.env, !top->record_opts.overwrite,
0647 &top->annotation_opts);
0648 if (ret == K_RELOAD) {
0649 top->zero = true;
0650 goto repeat;
0651 } else
0652 stop_top();
0653
0654 return NULL;
0655 }
0656
0657 static void display_sig(int sig __maybe_unused)
0658 {
0659 stop_top();
0660 }
0661
0662 static void display_setup_sig(void)
0663 {
0664 signal(SIGSEGV, sighandler_dump_stack);
0665 signal(SIGFPE, sighandler_dump_stack);
0666 signal(SIGINT, display_sig);
0667 signal(SIGQUIT, display_sig);
0668 signal(SIGTERM, display_sig);
0669 }
0670
0671 static void *display_thread(void *arg)
0672 {
0673 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
0674 struct termios save;
0675 struct perf_top *top = arg;
0676 int delay_msecs, c;
0677
0678
0679
0680
0681
0682
0683 unshare(CLONE_FS);
0684
0685 prctl(PR_SET_NAME, "perf-top-UI", 0, 0, 0);
0686
0687 display_setup_sig();
0688 pthread__unblock_sigwinch();
0689 repeat:
0690 delay_msecs = top->delay_secs * MSEC_PER_SEC;
0691 set_term_quiet_input(&save);
0692
0693 clearerr(stdin);
0694 if (poll(&stdin_poll, 1, 0) > 0)
0695 getc(stdin);
0696
0697 while (!done) {
0698 perf_top__print_sym_table(top);
0699
0700
0701
0702
0703 switch (poll(&stdin_poll, 1, delay_msecs)) {
0704 case 0:
0705 continue;
0706 case -1:
0707 if (errno == EINTR)
0708 continue;
0709 __fallthrough;
0710 default:
0711 c = getc(stdin);
0712 tcsetattr(0, TCSAFLUSH, &save);
0713
0714 if (perf_top__handle_keypress(top, c))
0715 goto repeat;
0716 stop_top();
0717 }
0718 }
0719
0720 tcsetattr(0, TCSAFLUSH, &save);
0721 return NULL;
0722 }
0723
0724 static int hist_iter__top_callback(struct hist_entry_iter *iter,
0725 struct addr_location *al, bool single,
0726 void *arg)
0727 {
0728 struct perf_top *top = arg;
0729 struct hist_entry *he = iter->he;
0730 struct evsel *evsel = iter->evsel;
0731
0732 if (perf_hpp_list.sym && single)
0733 perf_top__record_precise_ip(top, he, iter->sample, evsel, al->addr);
0734
0735 hist__account_cycles(iter->sample->branch_stack, al, iter->sample,
0736 !(top->record_opts.branch_stack & PERF_SAMPLE_BRANCH_ANY),
0737 NULL);
0738 return 0;
0739 }
0740
0741 static void perf_event__process_sample(struct perf_tool *tool,
0742 const union perf_event *event,
0743 struct evsel *evsel,
0744 struct perf_sample *sample,
0745 struct machine *machine)
0746 {
0747 struct perf_top *top = container_of(tool, struct perf_top, tool);
0748 struct addr_location al;
0749
0750 if (!machine && perf_guest) {
0751 static struct intlist *seen;
0752
0753 if (!seen)
0754 seen = intlist__new(NULL);
0755
0756 if (!intlist__has_entry(seen, sample->pid)) {
0757 pr_err("Can't find guest [%d]'s kernel information\n",
0758 sample->pid);
0759 intlist__add(seen, sample->pid);
0760 }
0761 return;
0762 }
0763
0764 if (!machine) {
0765 pr_err("%u unprocessable samples recorded.\r",
0766 top->session->evlist->stats.nr_unprocessable_samples++);
0767 return;
0768 }
0769
0770 if (event->header.misc & PERF_RECORD_MISC_EXACT_IP)
0771 top->exact_samples++;
0772
0773 if (machine__resolve(machine, &al, sample) < 0)
0774 return;
0775
0776 if (top->stitch_lbr)
0777 al.thread->lbr_stitch_enable = true;
0778
0779 if (!machine->kptr_restrict_warned &&
0780 symbol_conf.kptr_restrict &&
0781 al.cpumode == PERF_RECORD_MISC_KERNEL) {
0782 if (!evlist__exclude_kernel(top->session->evlist)) {
0783 ui__warning(
0784 "Kernel address maps (/proc/{kallsyms,modules}) are restricted.\n\n"
0785 "Check /proc/sys/kernel/kptr_restrict and /proc/sys/kernel/perf_event_paranoid.\n\n"
0786 "Kernel%s samples will not be resolved.\n",
0787 al.map && map__has_symbols(al.map) ?
0788 " modules" : "");
0789 if (use_browser <= 0)
0790 sleep(5);
0791 }
0792 machine->kptr_restrict_warned = true;
0793 }
0794
0795 if (al.sym == NULL && al.map != NULL) {
0796 const char *msg = "Kernel samples will not be resolved.\n";
0797
0798
0799
0800
0801
0802
0803
0804
0805
0806
0807
0808 if (!machine->kptr_restrict_warned && !top->vmlinux_warned &&
0809 __map__is_kernel(al.map) && map__has_symbols(al.map)) {
0810 if (symbol_conf.vmlinux_name) {
0811 char serr[256];
0812 dso__strerror_load(al.map->dso, serr, sizeof(serr));
0813 ui__warning("The %s file can't be used: %s\n%s",
0814 symbol_conf.vmlinux_name, serr, msg);
0815 } else {
0816 ui__warning("A vmlinux file was not found.\n%s",
0817 msg);
0818 }
0819
0820 if (use_browser <= 0)
0821 sleep(5);
0822 top->vmlinux_warned = true;
0823 }
0824 }
0825
0826 if (al.sym == NULL || !al.sym->idle) {
0827 struct hists *hists = evsel__hists(evsel);
0828 struct hist_entry_iter iter = {
0829 .evsel = evsel,
0830 .sample = sample,
0831 .add_entry_cb = hist_iter__top_callback,
0832 };
0833
0834 if (symbol_conf.cumulate_callchain)
0835 iter.ops = &hist_iter_cumulative;
0836 else
0837 iter.ops = &hist_iter_normal;
0838
0839 pthread_mutex_lock(&hists->lock);
0840
0841 if (hist_entry_iter__add(&iter, &al, top->max_stack, top) < 0)
0842 pr_err("Problem incrementing symbol period, skipping event\n");
0843
0844 pthread_mutex_unlock(&hists->lock);
0845 }
0846
0847 addr_location__put(&al);
0848 }
0849
0850 static void
0851 perf_top__process_lost(struct perf_top *top, union perf_event *event,
0852 struct evsel *evsel)
0853 {
0854 top->lost += event->lost.lost;
0855 top->lost_total += event->lost.lost;
0856 evsel->evlist->stats.total_lost += event->lost.lost;
0857 }
0858
0859 static void
0860 perf_top__process_lost_samples(struct perf_top *top,
0861 union perf_event *event,
0862 struct evsel *evsel)
0863 {
0864 top->lost += event->lost_samples.lost;
0865 top->lost_total += event->lost_samples.lost;
0866 evsel->evlist->stats.total_lost_samples += event->lost_samples.lost;
0867 }
0868
0869 static u64 last_timestamp;
0870
0871 static void perf_top__mmap_read_idx(struct perf_top *top, int idx)
0872 {
0873 struct record_opts *opts = &top->record_opts;
0874 struct evlist *evlist = top->evlist;
0875 struct mmap *md;
0876 union perf_event *event;
0877
0878 md = opts->overwrite ? &evlist->overwrite_mmap[idx] : &evlist->mmap[idx];
0879 if (perf_mmap__read_init(&md->core) < 0)
0880 return;
0881
0882 while ((event = perf_mmap__read_event(&md->core)) != NULL) {
0883 int ret;
0884
0885 ret = evlist__parse_sample_timestamp(evlist, event, &last_timestamp);
0886 if (ret && ret != -1)
0887 break;
0888
0889 ret = ordered_events__queue(top->qe.in, event, last_timestamp, 0, NULL);
0890 if (ret)
0891 break;
0892
0893 perf_mmap__consume(&md->core);
0894
0895 if (top->qe.rotate) {
0896 pthread_mutex_lock(&top->qe.mutex);
0897 top->qe.rotate = false;
0898 pthread_cond_signal(&top->qe.cond);
0899 pthread_mutex_unlock(&top->qe.mutex);
0900 }
0901 }
0902
0903 perf_mmap__read_done(&md->core);
0904 }
0905
0906 static void perf_top__mmap_read(struct perf_top *top)
0907 {
0908 bool overwrite = top->record_opts.overwrite;
0909 struct evlist *evlist = top->evlist;
0910 int i;
0911
0912 if (overwrite)
0913 evlist__toggle_bkw_mmap(evlist, BKW_MMAP_DATA_PENDING);
0914
0915 for (i = 0; i < top->evlist->core.nr_mmaps; i++)
0916 perf_top__mmap_read_idx(top, i);
0917
0918 if (overwrite) {
0919 evlist__toggle_bkw_mmap(evlist, BKW_MMAP_EMPTY);
0920 evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING);
0921 }
0922 }
0923
0924
0925
0926
0927
0928
0929
0930
0931
0932
0933
0934
0935
0936
0937
0938
0939
0940
0941 static int perf_top__overwrite_check(struct perf_top *top)
0942 {
0943 struct record_opts *opts = &top->record_opts;
0944 struct evlist *evlist = top->evlist;
0945 struct evsel_config_term *term;
0946 struct list_head *config_terms;
0947 struct evsel *evsel;
0948 int set, overwrite = -1;
0949
0950 evlist__for_each_entry(evlist, evsel) {
0951 set = -1;
0952 config_terms = &evsel->config_terms;
0953 list_for_each_entry(term, config_terms, list) {
0954 if (term->type == EVSEL__CONFIG_TERM_OVERWRITE)
0955 set = term->val.overwrite ? 1 : 0;
0956 }
0957
0958
0959 if ((overwrite < 0) && (set < 0))
0960 continue;
0961
0962
0963 if ((overwrite >= 0) && (set >= 0) && (overwrite != set))
0964 return -1;
0965
0966
0967 if ((overwrite >= 0) && (set < 0))
0968 return -1;
0969
0970
0971 if ((overwrite < 0) && (set >= 0)) {
0972
0973 if (evsel == evlist__first(evlist))
0974 overwrite = set;
0975 else
0976 return -1;
0977 }
0978 }
0979
0980 if ((overwrite >= 0) && (opts->overwrite != overwrite))
0981 opts->overwrite = overwrite;
0982
0983 return 0;
0984 }
0985
0986 static int perf_top_overwrite_fallback(struct perf_top *top,
0987 struct evsel *evsel)
0988 {
0989 struct record_opts *opts = &top->record_opts;
0990 struct evlist *evlist = top->evlist;
0991 struct evsel *counter;
0992
0993 if (!opts->overwrite)
0994 return 0;
0995
0996
0997 if (evsel != evlist__first(evlist))
0998 return 0;
0999
1000 evlist__for_each_entry(evlist, counter)
1001 counter->core.attr.write_backward = false;
1002 opts->overwrite = false;
1003 pr_debug2("fall back to non-overwrite mode\n");
1004 return 1;
1005 }
1006
1007 static int perf_top__start_counters(struct perf_top *top)
1008 {
1009 char msg[BUFSIZ];
1010 struct evsel *counter;
1011 struct evlist *evlist = top->evlist;
1012 struct record_opts *opts = &top->record_opts;
1013
1014 if (perf_top__overwrite_check(top)) {
1015 ui__error("perf top only support consistent per-event "
1016 "overwrite setting for all events\n");
1017 goto out_err;
1018 }
1019
1020 evlist__config(evlist, opts, &callchain_param);
1021
1022 evlist__for_each_entry(evlist, counter) {
1023 try_again:
1024 if (evsel__open(counter, top->evlist->core.user_requested_cpus,
1025 top->evlist->core.threads) < 0) {
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037 if (perf_missing_features.write_backward &&
1038 perf_top_overwrite_fallback(top, counter))
1039 goto try_again;
1040
1041 if (evsel__fallback(counter, errno, msg, sizeof(msg))) {
1042 if (verbose > 0)
1043 ui__warning("%s\n", msg);
1044 goto try_again;
1045 }
1046
1047 evsel__open_strerror(counter, &opts->target, errno, msg, sizeof(msg));
1048 ui__error("%s\n", msg);
1049 goto out_err;
1050 }
1051 }
1052
1053 if (evlist__mmap(evlist, opts->mmap_pages) < 0) {
1054 ui__error("Failed to mmap with %d (%s)\n",
1055 errno, str_error_r(errno, msg, sizeof(msg)));
1056 goto out_err;
1057 }
1058
1059 return 0;
1060
1061 out_err:
1062 return -1;
1063 }
1064
1065 static int callchain_param__setup_sample_type(struct callchain_param *callchain)
1066 {
1067 if (callchain->mode != CHAIN_NONE) {
1068 if (callchain_register_param(callchain) < 0) {
1069 ui__error("Can't register callchain params.\n");
1070 return -EINVAL;
1071 }
1072 }
1073
1074 return 0;
1075 }
1076
1077 static struct ordered_events *rotate_queues(struct perf_top *top)
1078 {
1079 struct ordered_events *in = top->qe.in;
1080
1081 if (top->qe.in == &top->qe.data[1])
1082 top->qe.in = &top->qe.data[0];
1083 else
1084 top->qe.in = &top->qe.data[1];
1085
1086 return in;
1087 }
1088
1089 static void *process_thread(void *arg)
1090 {
1091 struct perf_top *top = arg;
1092
1093 while (!done) {
1094 struct ordered_events *out, *in = top->qe.in;
1095
1096 if (!in->nr_events) {
1097 usleep(100);
1098 continue;
1099 }
1100
1101 out = rotate_queues(top);
1102
1103 pthread_mutex_lock(&top->qe.mutex);
1104 top->qe.rotate = true;
1105 pthread_cond_wait(&top->qe.cond, &top->qe.mutex);
1106 pthread_mutex_unlock(&top->qe.mutex);
1107
1108 if (ordered_events__flush(out, OE_FLUSH__TOP))
1109 pr_err("failed to process events\n");
1110 }
1111
1112 return NULL;
1113 }
1114
1115
1116
1117
1118 static int should_drop(struct ordered_event *qevent, struct perf_top *top)
1119 {
1120 union perf_event *event = qevent->event;
1121 u64 delay_timestamp;
1122
1123 if (event->header.type != PERF_RECORD_SAMPLE)
1124 return false;
1125
1126 delay_timestamp = qevent->timestamp + top->delay_secs * NSEC_PER_SEC;
1127 return delay_timestamp < last_timestamp;
1128 }
1129
1130 static int deliver_event(struct ordered_events *qe,
1131 struct ordered_event *qevent)
1132 {
1133 struct perf_top *top = qe->data;
1134 struct evlist *evlist = top->evlist;
1135 struct perf_session *session = top->session;
1136 union perf_event *event = qevent->event;
1137 struct perf_sample sample;
1138 struct evsel *evsel;
1139 struct machine *machine;
1140 int ret = -1;
1141
1142 if (should_drop(qevent, top)) {
1143 top->drop++;
1144 top->drop_total++;
1145 return 0;
1146 }
1147
1148 ret = evlist__parse_sample(evlist, event, &sample);
1149 if (ret) {
1150 pr_err("Can't parse sample, err = %d\n", ret);
1151 goto next_event;
1152 }
1153
1154 evsel = evlist__id2evsel(session->evlist, sample.id);
1155 assert(evsel != NULL);
1156
1157 if (event->header.type == PERF_RECORD_SAMPLE) {
1158 if (evswitch__discard(&top->evswitch, evsel))
1159 return 0;
1160 ++top->samples;
1161 }
1162
1163 switch (sample.cpumode) {
1164 case PERF_RECORD_MISC_USER:
1165 ++top->us_samples;
1166 if (top->hide_user_symbols)
1167 goto next_event;
1168 machine = &session->machines.host;
1169 break;
1170 case PERF_RECORD_MISC_KERNEL:
1171 ++top->kernel_samples;
1172 if (top->hide_kernel_symbols)
1173 goto next_event;
1174 machine = &session->machines.host;
1175 break;
1176 case PERF_RECORD_MISC_GUEST_KERNEL:
1177 ++top->guest_kernel_samples;
1178 machine = perf_session__find_machine(session,
1179 sample.pid);
1180 break;
1181 case PERF_RECORD_MISC_GUEST_USER:
1182 ++top->guest_us_samples;
1183
1184
1185
1186
1187 goto next_event;
1188 default:
1189 if (event->header.type == PERF_RECORD_SAMPLE)
1190 goto next_event;
1191 machine = &session->machines.host;
1192 break;
1193 }
1194
1195 if (event->header.type == PERF_RECORD_SAMPLE) {
1196 perf_event__process_sample(&top->tool, event, evsel,
1197 &sample, machine);
1198 } else if (event->header.type == PERF_RECORD_LOST) {
1199 perf_top__process_lost(top, event, evsel);
1200 } else if (event->header.type == PERF_RECORD_LOST_SAMPLES) {
1201 perf_top__process_lost_samples(top, event, evsel);
1202 } else if (event->header.type < PERF_RECORD_MAX) {
1203 events_stats__inc(&session->evlist->stats, event->header.type);
1204 machine__process_event(machine, event, &sample);
1205 } else
1206 ++session->evlist->stats.nr_unknown_events;
1207
1208 ret = 0;
1209 next_event:
1210 return ret;
1211 }
1212
1213 static void init_process_thread(struct perf_top *top)
1214 {
1215 ordered_events__init(&top->qe.data[0], deliver_event, top);
1216 ordered_events__init(&top->qe.data[1], deliver_event, top);
1217 ordered_events__set_copy_on_queue(&top->qe.data[0], true);
1218 ordered_events__set_copy_on_queue(&top->qe.data[1], true);
1219 top->qe.in = &top->qe.data[0];
1220 pthread_mutex_init(&top->qe.mutex, NULL);
1221 pthread_cond_init(&top->qe.cond, NULL);
1222 }
1223
1224 static int __cmd_top(struct perf_top *top)
1225 {
1226 struct record_opts *opts = &top->record_opts;
1227 pthread_t thread, thread_process;
1228 int ret;
1229
1230 if (!top->annotation_opts.objdump_path) {
1231 ret = perf_env__lookup_objdump(&top->session->header.env,
1232 &top->annotation_opts.objdump_path);
1233 if (ret)
1234 return ret;
1235 }
1236
1237 ret = callchain_param__setup_sample_type(&callchain_param);
1238 if (ret)
1239 return ret;
1240
1241 if (perf_session__register_idle_thread(top->session) < 0)
1242 return ret;
1243
1244 if (top->nr_threads_synthesize > 1)
1245 perf_set_multithreaded();
1246
1247 init_process_thread(top);
1248
1249 if (opts->record_namespaces)
1250 top->tool.namespace_events = true;
1251 if (opts->record_cgroup) {
1252 #ifdef HAVE_FILE_HANDLE
1253 top->tool.cgroup_events = true;
1254 #else
1255 pr_err("cgroup tracking is not supported.\n");
1256 return -1;
1257 #endif
1258 }
1259
1260 ret = perf_event__synthesize_bpf_events(top->session, perf_event__process,
1261 &top->session->machines.host,
1262 &top->record_opts);
1263 if (ret < 0)
1264 pr_debug("Couldn't synthesize BPF events: Pre-existing BPF programs won't have symbols resolved.\n");
1265
1266 ret = perf_event__synthesize_cgroups(&top->tool, perf_event__process,
1267 &top->session->machines.host);
1268 if (ret < 0)
1269 pr_debug("Couldn't synthesize cgroup events.\n");
1270
1271 machine__synthesize_threads(&top->session->machines.host, &opts->target,
1272 top->evlist->core.threads, true, false,
1273 top->nr_threads_synthesize);
1274
1275 if (top->nr_threads_synthesize > 1)
1276 perf_set_singlethreaded();
1277
1278 if (perf_hpp_list.socket) {
1279 ret = perf_env__read_cpu_topology_map(&perf_env);
1280 if (ret < 0) {
1281 char errbuf[BUFSIZ];
1282 const char *err = str_error_r(-ret, errbuf, sizeof(errbuf));
1283
1284 ui__error("Could not read the CPU topology map: %s\n", err);
1285 return ret;
1286 }
1287 }
1288
1289 ret = perf_top__start_counters(top);
1290 if (ret)
1291 return ret;
1292
1293 top->session->evlist = top->evlist;
1294 perf_session__set_id_hdr_size(top->session);
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304 if (!target__none(&opts->target))
1305 evlist__enable(top->evlist);
1306
1307 ret = -1;
1308 if (pthread_create(&thread_process, NULL, process_thread, top)) {
1309 ui__error("Could not create process thread.\n");
1310 return ret;
1311 }
1312
1313 if (pthread_create(&thread, NULL, (use_browser > 0 ? display_thread_tui :
1314 display_thread), top)) {
1315 ui__error("Could not create display thread.\n");
1316 goto out_join_thread;
1317 }
1318
1319 if (top->realtime_prio) {
1320 struct sched_param param;
1321
1322 param.sched_priority = top->realtime_prio;
1323 if (sched_setscheduler(0, SCHED_FIFO, ¶m)) {
1324 ui__error("Could not set realtime priority.\n");
1325 goto out_join;
1326 }
1327 }
1328
1329
1330 evlist__poll(top->evlist, 100);
1331
1332 perf_top__mmap_read(top);
1333
1334 while (!done) {
1335 u64 hits = top->samples;
1336
1337 perf_top__mmap_read(top);
1338
1339 if (opts->overwrite || (hits == top->samples))
1340 ret = evlist__poll(top->evlist, 100);
1341
1342 if (resize) {
1343 perf_top__resize(top);
1344 resize = 0;
1345 }
1346 }
1347
1348 ret = 0;
1349 out_join:
1350 pthread_join(thread, NULL);
1351 out_join_thread:
1352 pthread_cond_signal(&top->qe.cond);
1353 pthread_join(thread_process, NULL);
1354 return ret;
1355 }
1356
1357 static int
1358 callchain_opt(const struct option *opt, const char *arg, int unset)
1359 {
1360 symbol_conf.use_callchain = true;
1361 return record_callchain_opt(opt, arg, unset);
1362 }
1363
1364 static int
1365 parse_callchain_opt(const struct option *opt, const char *arg, int unset)
1366 {
1367 struct callchain_param *callchain = opt->value;
1368
1369 callchain->enabled = !unset;
1370 callchain->record_mode = CALLCHAIN_FP;
1371
1372
1373
1374
1375 if (unset) {
1376 symbol_conf.use_callchain = false;
1377 callchain->record_mode = CALLCHAIN_NONE;
1378 return 0;
1379 }
1380
1381 return parse_callchain_top_opt(arg);
1382 }
1383
1384 static int perf_top_config(const char *var, const char *value, void *cb __maybe_unused)
1385 {
1386 if (!strcmp(var, "top.call-graph")) {
1387 var = "call-graph.record-mode";
1388 return perf_default_config(var, value, cb);
1389 }
1390 if (!strcmp(var, "top.children")) {
1391 symbol_conf.cumulate_callchain = perf_config_bool(var, value);
1392 return 0;
1393 }
1394
1395 return 0;
1396 }
1397
1398 static int
1399 parse_percent_limit(const struct option *opt, const char *arg,
1400 int unset __maybe_unused)
1401 {
1402 struct perf_top *top = opt->value;
1403
1404 top->min_percent = strtof(arg, NULL);
1405 return 0;
1406 }
1407
1408 const char top_callchain_help[] = CALLCHAIN_RECORD_HELP CALLCHAIN_REPORT_HELP
1409 "\n\t\t\t\tDefault: fp,graph,0.5,caller,function";
1410
1411 int cmd_top(int argc, const char **argv)
1412 {
1413 char errbuf[BUFSIZ];
1414 struct perf_top top = {
1415 .count_filter = 5,
1416 .delay_secs = 2,
1417 .record_opts = {
1418 .mmap_pages = UINT_MAX,
1419 .user_freq = UINT_MAX,
1420 .user_interval = ULLONG_MAX,
1421 .freq = 4000,
1422 .target = {
1423 .uses_mmap = true,
1424 },
1425
1426
1427
1428
1429
1430
1431
1432 .overwrite = 0,
1433 .sample_time = true,
1434 .sample_time_set = true,
1435 },
1436 .max_stack = sysctl__max_stack(),
1437 .annotation_opts = annotation__default_options,
1438 .nr_threads_synthesize = UINT_MAX,
1439 };
1440 struct record_opts *opts = &top.record_opts;
1441 struct target *target = &opts->target;
1442 const struct option options[] = {
1443 OPT_CALLBACK('e', "event", &top.evlist, "event",
1444 "event selector. use 'perf list' to list available events",
1445 parse_events_option),
1446 OPT_U64('c', "count", &opts->user_interval, "event period to sample"),
1447 OPT_STRING('p', "pid", &target->pid, "pid",
1448 "profile events on existing process id"),
1449 OPT_STRING('t', "tid", &target->tid, "tid",
1450 "profile events on existing thread id"),
1451 OPT_BOOLEAN('a', "all-cpus", &target->system_wide,
1452 "system-wide collection from all CPUs"),
1453 OPT_STRING('C', "cpu", &target->cpu_list, "cpu",
1454 "list of cpus to monitor"),
1455 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1456 "file", "vmlinux pathname"),
1457 OPT_BOOLEAN(0, "ignore-vmlinux", &symbol_conf.ignore_vmlinux,
1458 "don't load vmlinux even if found"),
1459 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
1460 "file", "kallsyms pathname"),
1461 OPT_BOOLEAN('K', "hide_kernel_symbols", &top.hide_kernel_symbols,
1462 "hide kernel symbols"),
1463 OPT_CALLBACK('m', "mmap-pages", &opts->mmap_pages, "pages",
1464 "number of mmap data pages", evlist__parse_mmap_pages),
1465 OPT_INTEGER('r', "realtime", &top.realtime_prio,
1466 "collect data with this RT SCHED_FIFO priority"),
1467 OPT_INTEGER('d', "delay", &top.delay_secs,
1468 "number of seconds to delay between refreshes"),
1469 OPT_BOOLEAN('D', "dump-symtab", &top.dump_symtab,
1470 "dump the symbol table used for profiling"),
1471 OPT_INTEGER('f', "count-filter", &top.count_filter,
1472 "only display functions with more events than this"),
1473 OPT_BOOLEAN(0, "group", &opts->group,
1474 "put the counters into a counter group"),
1475 OPT_BOOLEAN('i', "no-inherit", &opts->no_inherit,
1476 "child tasks do not inherit counters"),
1477 OPT_STRING(0, "sym-annotate", &top.sym_filter, "symbol name",
1478 "symbol to annotate"),
1479 OPT_BOOLEAN('z', "zero", &top.zero, "zero history across updates"),
1480 OPT_CALLBACK('F', "freq", &top.record_opts, "freq or 'max'",
1481 "profile at this frequency",
1482 record__parse_freq),
1483 OPT_INTEGER('E', "entries", &top.print_entries,
1484 "display this many functions"),
1485 OPT_BOOLEAN('U', "hide_user_symbols", &top.hide_user_symbols,
1486 "hide user symbols"),
1487 #ifdef HAVE_SLANG_SUPPORT
1488 OPT_BOOLEAN(0, "tui", &top.use_tui, "Use the TUI interface"),
1489 #endif
1490 OPT_BOOLEAN(0, "stdio", &top.use_stdio, "Use the stdio interface"),
1491 OPT_INCR('v', "verbose", &verbose,
1492 "be more verbose (show counter open errors, etc)"),
1493 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1494 "sort by key(s): pid, comm, dso, symbol, parent, cpu, srcline, ..."
1495 " Please refer the man page for the complete list."),
1496 OPT_STRING(0, "fields", &field_order, "key[,keys...]",
1497 "output field(s): overhead, period, sample plus all of sort keys"),
1498 OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
1499 "Show a column with the number of samples"),
1500 OPT_CALLBACK_NOOPT('g', NULL, &callchain_param,
1501 NULL, "enables call-graph recording and display",
1502 &callchain_opt),
1503 OPT_CALLBACK(0, "call-graph", &callchain_param,
1504 "record_mode[,record_size],print_type,threshold[,print_limit],order,sort_key[,branch]",
1505 top_callchain_help, &parse_callchain_opt),
1506 OPT_BOOLEAN(0, "children", &symbol_conf.cumulate_callchain,
1507 "Accumulate callchains of children and show total overhead as well"),
1508 OPT_INTEGER(0, "max-stack", &top.max_stack,
1509 "Set the maximum stack depth when parsing the callchain. "
1510 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
1511 OPT_CALLBACK(0, "ignore-callees", NULL, "regex",
1512 "ignore callees of these functions in call graphs",
1513 report_parse_ignore_callees_opt),
1514 OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period,
1515 "Show a column with the sum of periods"),
1516 OPT_STRING(0, "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
1517 "only consider symbols in these dsos"),
1518 OPT_STRING(0, "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
1519 "only consider symbols in these comms"),
1520 OPT_STRING(0, "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
1521 "only consider these symbols"),
1522 OPT_BOOLEAN(0, "source", &top.annotation_opts.annotate_src,
1523 "Interleave source code with assembly code (default)"),
1524 OPT_BOOLEAN(0, "asm-raw", &top.annotation_opts.show_asm_raw,
1525 "Display raw encoding of assembly instructions (default)"),
1526 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
1527 "Enable kernel symbol demangling"),
1528 OPT_BOOLEAN(0, "no-bpf-event", &top.record_opts.no_bpf_event, "do not record bpf events"),
1529 OPT_STRING(0, "objdump", &top.annotation_opts.objdump_path, "path",
1530 "objdump binary to use for disassembly and annotations"),
1531 OPT_STRING('M', "disassembler-style", &top.annotation_opts.disassembler_style, "disassembler style",
1532 "Specify disassembler style (e.g. -M intel for intel syntax)"),
1533 OPT_STRING(0, "prefix", &top.annotation_opts.prefix, "prefix",
1534 "Add prefix to source file path names in programs (with --prefix-strip)"),
1535 OPT_STRING(0, "prefix-strip", &top.annotation_opts.prefix_strip, "N",
1536 "Strip first N entries of source file path name in programs (with --prefix)"),
1537 OPT_STRING('u', "uid", &target->uid_str, "user", "user to profile"),
1538 OPT_CALLBACK(0, "percent-limit", &top, "percent",
1539 "Don't show entries under that percent", parse_percent_limit),
1540 OPT_CALLBACK(0, "percentage", NULL, "relative|absolute",
1541 "How to display percentage of filtered entries", parse_filter_percentage),
1542 OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str,
1543 "width[,width...]",
1544 "don't try to adjust column width, use these fixed values"),
1545 OPT_UINTEGER(0, "proc-map-timeout", &proc_map_timeout,
1546 "per thread proc mmap processing timeout in ms"),
1547 OPT_CALLBACK_NOOPT('b', "branch-any", &opts->branch_stack,
1548 "branch any", "sample any taken branches",
1549 parse_branch_stack),
1550 OPT_CALLBACK('j', "branch-filter", &opts->branch_stack,
1551 "branch filter mask", "branch stack filter modes",
1552 parse_branch_stack),
1553 OPT_BOOLEAN(0, "raw-trace", &symbol_conf.raw_trace,
1554 "Show raw trace event output (do not use print fmt or plugins)"),
1555 OPT_BOOLEAN(0, "hierarchy", &symbol_conf.report_hierarchy,
1556 "Show entries in a hierarchy"),
1557 OPT_BOOLEAN(0, "overwrite", &top.record_opts.overwrite,
1558 "Use a backward ring buffer, default: no"),
1559 OPT_BOOLEAN(0, "force", &symbol_conf.force, "don't complain, do it"),
1560 OPT_UINTEGER(0, "num-thread-synthesize", &top.nr_threads_synthesize,
1561 "number of thread to run event synthesize"),
1562 OPT_CALLBACK('G', "cgroup", &top.evlist, "name",
1563 "monitor event in cgroup name only", parse_cgroups),
1564 OPT_BOOLEAN(0, "namespaces", &opts->record_namespaces,
1565 "Record namespaces events"),
1566 OPT_BOOLEAN(0, "all-cgroups", &opts->record_cgroup,
1567 "Record cgroup events"),
1568 OPT_INTEGER(0, "group-sort-idx", &symbol_conf.group_sort_idx,
1569 "Sort the output by the event at the index n in group. "
1570 "If n is invalid, sort by the first event. "
1571 "WARNING: should be used on grouped events."),
1572 OPT_BOOLEAN(0, "stitch-lbr", &top.stitch_lbr,
1573 "Enable LBR callgraph stitching approach"),
1574 #ifdef HAVE_LIBPFM
1575 OPT_CALLBACK(0, "pfm-events", &top.evlist, "event",
1576 "libpfm4 event selector. use 'perf list' to list available events",
1577 parse_libpfm_events_option),
1578 #endif
1579 OPTS_EVSWITCH(&top.evswitch),
1580 OPT_END()
1581 };
1582 const char * const top_usage[] = {
1583 "perf top [<options>]",
1584 NULL
1585 };
1586 int status = hists__init();
1587
1588 if (status < 0)
1589 return status;
1590
1591 top.annotation_opts.min_pcnt = 5;
1592 top.annotation_opts.context = 4;
1593
1594 top.evlist = evlist__new();
1595 if (top.evlist == NULL)
1596 return -ENOMEM;
1597
1598 status = perf_config(perf_top_config, &top);
1599 if (status)
1600 return status;
1601
1602
1603
1604
1605 status = perf_env__read_cpuid(&perf_env);
1606 if (status) {
1607
1608
1609
1610
1611 eprintf(status == ENOSYS ? 1 : 0, verbose,
1612 "Couldn't read the cpuid for this machine: %s\n",
1613 str_error_r(errno, errbuf, sizeof(errbuf)));
1614 }
1615 top.evlist->env = &perf_env;
1616
1617 argc = parse_options(argc, argv, options, top_usage, 0);
1618 if (argc)
1619 usage_with_options(top_usage, options);
1620
1621 status = symbol__validate_sym_arguments();
1622 if (status)
1623 goto out_delete_evlist;
1624
1625 if (annotate_check_args(&top.annotation_opts) < 0)
1626 goto out_delete_evlist;
1627
1628 if (!top.evlist->core.nr_entries &&
1629 evlist__add_default(top.evlist) < 0) {
1630 pr_err("Not enough memory for event selector list\n");
1631 goto out_delete_evlist;
1632 }
1633
1634 status = evswitch__init(&top.evswitch, top.evlist, stderr);
1635 if (status)
1636 goto out_delete_evlist;
1637
1638 if (symbol_conf.report_hierarchy) {
1639
1640 symbol_conf.event_group = false;
1641 symbol_conf.cumulate_callchain = false;
1642
1643 if (field_order) {
1644 pr_err("Error: --hierarchy and --fields options cannot be used together\n");
1645 parse_options_usage(top_usage, options, "fields", 0);
1646 parse_options_usage(NULL, options, "hierarchy", 0);
1647 goto out_delete_evlist;
1648 }
1649 }
1650
1651 if (top.stitch_lbr && !(callchain_param.record_mode == CALLCHAIN_LBR)) {
1652 pr_err("Error: --stitch-lbr must be used with --call-graph lbr\n");
1653 goto out_delete_evlist;
1654 }
1655
1656 if (nr_cgroups > 0 && opts->record_cgroup) {
1657 pr_err("--cgroup and --all-cgroups cannot be used together\n");
1658 goto out_delete_evlist;
1659 }
1660
1661 if (opts->branch_stack && callchain_param.enabled)
1662 symbol_conf.show_branchflag_count = true;
1663
1664 sort__mode = SORT_MODE__TOP;
1665
1666 perf_hpp_list.need_collapse = 1;
1667
1668 if (top.use_stdio)
1669 use_browser = 0;
1670 #ifdef HAVE_SLANG_SUPPORT
1671 else if (top.use_tui)
1672 use_browser = 1;
1673 #endif
1674
1675 setup_browser(false);
1676
1677 if (setup_sorting(top.evlist) < 0) {
1678 if (sort_order)
1679 parse_options_usage(top_usage, options, "s", 1);
1680 if (field_order)
1681 parse_options_usage(sort_order ? NULL : top_usage,
1682 options, "fields", 0);
1683 goto out_delete_evlist;
1684 }
1685
1686 status = target__validate(target);
1687 if (status) {
1688 target__strerror(target, status, errbuf, BUFSIZ);
1689 ui__warning("%s\n", errbuf);
1690 }
1691
1692 status = target__parse_uid(target);
1693 if (status) {
1694 int saved_errno = errno;
1695
1696 target__strerror(target, status, errbuf, BUFSIZ);
1697 ui__error("%s\n", errbuf);
1698
1699 status = -saved_errno;
1700 goto out_delete_evlist;
1701 }
1702
1703 if (target__none(target))
1704 target->system_wide = true;
1705
1706 if (evlist__create_maps(top.evlist, target) < 0) {
1707 ui__error("Couldn't create thread/CPU maps: %s\n",
1708 errno == ENOENT ? "No such process" : str_error_r(errno, errbuf, sizeof(errbuf)));
1709 goto out_delete_evlist;
1710 }
1711
1712 if (top.delay_secs < 1)
1713 top.delay_secs = 1;
1714
1715 if (record_opts__config(opts)) {
1716 status = -EINVAL;
1717 goto out_delete_evlist;
1718 }
1719
1720 top.sym_evsel = evlist__first(top.evlist);
1721
1722 if (!callchain_param.enabled) {
1723 symbol_conf.cumulate_callchain = false;
1724 perf_hpp__cancel_cumulate();
1725 }
1726
1727 if (symbol_conf.cumulate_callchain && !callchain_param.order_set)
1728 callchain_param.order = ORDER_CALLER;
1729
1730 status = symbol__annotation_init();
1731 if (status < 0)
1732 goto out_delete_evlist;
1733
1734 annotation_config__init(&top.annotation_opts);
1735
1736 symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL);
1737 status = symbol__init(NULL);
1738 if (status < 0)
1739 goto out_delete_evlist;
1740
1741 sort__setup_elide(stdout);
1742
1743 get_term_dimensions(&top.winsize);
1744 if (top.print_entries == 0) {
1745 perf_top__update_print_entries(&top);
1746 signal(SIGWINCH, winch_sig);
1747 }
1748
1749 top.session = perf_session__new(NULL, NULL);
1750 if (IS_ERR(top.session)) {
1751 status = PTR_ERR(top.session);
1752 goto out_delete_evlist;
1753 }
1754
1755 #ifdef HAVE_LIBBPF_SUPPORT
1756 if (!top.record_opts.no_bpf_event) {
1757 top.sb_evlist = evlist__new();
1758
1759 if (top.sb_evlist == NULL) {
1760 pr_err("Couldn't create side band evlist.\n.");
1761 goto out_delete_evlist;
1762 }
1763
1764 if (evlist__add_bpf_sb_event(top.sb_evlist, &perf_env)) {
1765 pr_err("Couldn't ask for PERF_RECORD_BPF_EVENT side band events.\n.");
1766 goto out_delete_evlist;
1767 }
1768 }
1769 #endif
1770
1771 if (evlist__start_sb_thread(top.sb_evlist, target)) {
1772 pr_debug("Couldn't start the BPF side band thread:\nBPF programs starting from now on won't be annotatable\n");
1773 opts->no_bpf_event = true;
1774 }
1775
1776 status = __cmd_top(&top);
1777
1778 if (!opts->no_bpf_event)
1779 evlist__stop_sb_thread(top.sb_evlist);
1780
1781 out_delete_evlist:
1782 evlist__delete(top.evlist);
1783 perf_session__delete(top.session);
1784
1785 return status;
1786 }