perf top: Fix window dimensions change handling
[sfrench/cifs-2.6.git] / tools / perf / builtin-top.c
1 /*
2  * builtin-top.c
3  *
4  * Builtin top command: Display a continuously updated profile of
5  * any workload, CPU or specific PID.
6  *
7  * Copyright (C) 2008, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
8  *               2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
9  *
10  * Improvements and fixes by:
11  *
12  *   Arjan van de Ven <arjan@linux.intel.com>
13  *   Yanmin Zhang <yanmin.zhang@intel.com>
14  *   Wu Fengguang <fengguang.wu@intel.com>
15  *   Mike Galbraith <efault@gmx.de>
16  *   Paul Mackerras <paulus@samba.org>
17  *
18  * Released under the GPL v2. (and only v2, not any later version)
19  */
20 #include "builtin.h"
21
22 #include "perf.h"
23
24 #include "util/annotate.h"
25 #include "util/config.h"
26 #include "util/color.h"
27 #include "util/drv_configs.h"
28 #include "util/evlist.h"
29 #include "util/evsel.h"
30 #include "util/event.h"
31 #include "util/machine.h"
32 #include "util/session.h"
33 #include "util/symbol.h"
34 #include "util/thread.h"
35 #include "util/thread_map.h"
36 #include "util/top.h"
37 #include <linux/rbtree.h>
38 #include <subcmd/parse-options.h>
39 #include "util/parse-events.h"
40 #include "util/cpumap.h"
41 #include "util/xyarray.h"
42 #include "util/sort.h"
43 #include "util/term.h"
44 #include "util/intlist.h"
45 #include "util/parse-branch-options.h"
46 #include "arch/common.h"
47
48 #include "util/debug.h"
49
50 #include <assert.h>
51 #include <elf.h>
52 #include <fcntl.h>
53
54 #include <stdio.h>
55 #include <termios.h>
56 #include <unistd.h>
57 #include <inttypes.h>
58
59 #include <errno.h>
60 #include <time.h>
61 #include <sched.h>
62 #include <signal.h>
63
64 #include <sys/syscall.h>
65 #include <sys/ioctl.h>
66 #include <poll.h>
67 #include <sys/prctl.h>
68 #include <sys/wait.h>
69 #include <sys/uio.h>
70 #include <sys/utsname.h>
71 #include <sys/mman.h>
72
73 #include <linux/stringify.h>
74 #include <linux/time64.h>
75 #include <linux/types.h>
76
77 #include "sane_ctype.h"
78
79 static volatile int done;
80 static volatile int resize;
81
82 #define HEADER_LINE_NR  5
83
84 static void perf_top__update_print_entries(struct perf_top *top)
85 {
86         top->print_entries = top->winsize.ws_row - HEADER_LINE_NR;
87 }
88
89 static void perf_top__sig_winch(int sig __maybe_unused,
90                                 siginfo_t *info __maybe_unused, void *arg __maybe_unused)
91 {
92         resize = 1;
93 }
94
95 static void perf_top__resize(struct perf_top *top)
96 {
97         get_term_dimensions(&top->winsize);
98         perf_top__update_print_entries(top);
99 }
100
101 static int perf_top__parse_source(struct perf_top *top, struct hist_entry *he)
102 {
103         struct symbol *sym;
104         struct annotation *notes;
105         struct map *map;
106         int err = -1;
107
108         if (!he || !he->ms.sym)
109                 return -1;
110
111         sym = he->ms.sym;
112         map = he->ms.map;
113
114         /*
115          * We can't annotate with just /proc/kallsyms
116          */
117         if (map->dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS &&
118             !dso__is_kcore(map->dso)) {
119                 pr_err("Can't annotate %s: No vmlinux file was found in the "
120                        "path\n", sym->name);
121                 sleep(1);
122                 return -1;
123         }
124
125         notes = symbol__annotation(sym);
126         if (notes->src != NULL) {
127                 pthread_mutex_lock(&notes->lock);
128                 goto out_assign;
129         }
130
131         pthread_mutex_lock(&notes->lock);
132
133         if (symbol__alloc_hist(sym) < 0) {
134                 pthread_mutex_unlock(&notes->lock);
135                 pr_err("Not enough memory for annotating '%s' symbol!\n",
136                        sym->name);
137                 sleep(1);
138                 return err;
139         }
140
141         err = symbol__disassemble(sym, map, NULL, 0, NULL, NULL);
142         if (err == 0) {
143 out_assign:
144                 top->sym_filter_entry = he;
145         } else {
146                 char msg[BUFSIZ];
147                 symbol__strerror_disassemble(sym, map, err, msg, sizeof(msg));
148                 pr_err("Couldn't annotate %s: %s\n", sym->name, msg);
149         }
150
151         pthread_mutex_unlock(&notes->lock);
152         return err;
153 }
154
155 static void __zero_source_counters(struct hist_entry *he)
156 {
157         struct symbol *sym = he->ms.sym;
158         symbol__annotate_zero_histograms(sym);
159 }
160
161 static void ui__warn_map_erange(struct map *map, struct symbol *sym, u64 ip)
162 {
163         struct utsname uts;
164         int err = uname(&uts);
165
166         ui__warning("Out of bounds address found:\n\n"
167                     "Addr:   %" PRIx64 "\n"
168                     "DSO:    %s %c\n"
169                     "Map:    %" PRIx64 "-%" PRIx64 "\n"
170                     "Symbol: %" PRIx64 "-%" PRIx64 " %c %s\n"
171                     "Arch:   %s\n"
172                     "Kernel: %s\n"
173                     "Tools:  %s\n\n"
174                     "Not all samples will be on the annotation output.\n\n"
175                     "Please report to linux-kernel@vger.kernel.org\n",
176                     ip, map->dso->long_name, dso__symtab_origin(map->dso),
177                     map->start, map->end, sym->start, sym->end,
178                     sym->binding == STB_GLOBAL ? 'g' :
179                     sym->binding == STB_LOCAL  ? 'l' : 'w', sym->name,
180                     err ? "[unknown]" : uts.machine,
181                     err ? "[unknown]" : uts.release, perf_version_string);
182         if (use_browser <= 0)
183                 sleep(5);
184
185         map->erange_warned = true;
186 }
187
188 static void perf_top__record_precise_ip(struct perf_top *top,
189                                         struct hist_entry *he,
190                                         struct perf_sample *sample,
191                                         int counter, u64 ip)
192 {
193         struct annotation *notes;
194         struct symbol *sym = he->ms.sym;
195         int err = 0;
196
197         if (sym == NULL || (use_browser == 0 &&
198                             (top->sym_filter_entry == NULL ||
199                              top->sym_filter_entry->ms.sym != sym)))
200                 return;
201
202         notes = symbol__annotation(sym);
203
204         if (pthread_mutex_trylock(&notes->lock))
205                 return;
206
207         err = hist_entry__inc_addr_samples(he, sample, counter, ip);
208
209         pthread_mutex_unlock(&notes->lock);
210
211         if (unlikely(err)) {
212                 /*
213                  * This function is now called with he->hists->lock held.
214                  * Release it before going to sleep.
215                  */
216                 pthread_mutex_unlock(&he->hists->lock);
217
218                 if (err == -ERANGE && !he->ms.map->erange_warned)
219                         ui__warn_map_erange(he->ms.map, sym, ip);
220                 else if (err == -ENOMEM) {
221                         pr_err("Not enough memory for annotating '%s' symbol!\n",
222                                sym->name);
223                         sleep(1);
224                 }
225
226                 pthread_mutex_lock(&he->hists->lock);
227         }
228 }
229
230 static void perf_top__show_details(struct perf_top *top)
231 {
232         struct hist_entry *he = top->sym_filter_entry;
233         struct annotation *notes;
234         struct symbol *symbol;
235         int more;
236
237         if (!he)
238                 return;
239
240         symbol = he->ms.sym;
241         notes = symbol__annotation(symbol);
242
243         pthread_mutex_lock(&notes->lock);
244
245         if (notes->src == NULL)
246                 goto out_unlock;
247
248         printf("Showing %s for %s\n", perf_evsel__name(top->sym_evsel), symbol->name);
249         printf("  Events  Pcnt (>=%d%%)\n", top->sym_pcnt_filter);
250
251         more = symbol__annotate_printf(symbol, he->ms.map, top->sym_evsel,
252                                        0, top->sym_pcnt_filter, top->print_entries, 4);
253
254         if (top->evlist->enabled) {
255                 if (top->zero)
256                         symbol__annotate_zero_histogram(symbol, top->sym_evsel->idx);
257                 else
258                         symbol__annotate_decay_histogram(symbol, top->sym_evsel->idx);
259         }
260         if (more != 0)
261                 printf("%d lines not displayed, maybe increase display entries [e]\n", more);
262 out_unlock:
263         pthread_mutex_unlock(&notes->lock);
264 }
265
266 static void perf_top__print_sym_table(struct perf_top *top)
267 {
268         char bf[160];
269         int printed = 0;
270         const int win_width = top->winsize.ws_col - 1;
271         struct perf_evsel *evsel = top->sym_evsel;
272         struct hists *hists = evsel__hists(evsel);
273
274         puts(CONSOLE_CLEAR);
275
276         perf_top__header_snprintf(top, bf, sizeof(bf));
277         printf("%s\n", bf);
278
279         perf_top__reset_sample_counters(top);
280
281         printf("%-*.*s\n", win_width, win_width, graph_dotted_line);
282
283         if (hists->stats.nr_lost_warned !=
284             hists->stats.nr_events[PERF_RECORD_LOST]) {
285                 hists->stats.nr_lost_warned =
286                               hists->stats.nr_events[PERF_RECORD_LOST];
287                 color_fprintf(stdout, PERF_COLOR_RED,
288                               "WARNING: LOST %d chunks, Check IO/CPU overload",
289                               hists->stats.nr_lost_warned);
290                 ++printed;
291         }
292
293         if (top->sym_filter_entry) {
294                 perf_top__show_details(top);
295                 return;
296         }
297
298         if (top->evlist->enabled) {
299                 if (top->zero) {
300                         hists__delete_entries(hists);
301                 } else {
302                         hists__decay_entries(hists, top->hide_user_symbols,
303                                              top->hide_kernel_symbols);
304                 }
305         }
306
307         hists__collapse_resort(hists, NULL);
308         perf_evsel__output_resort(evsel, NULL);
309
310         hists__output_recalc_col_len(hists, top->print_entries - printed);
311         putchar('\n');
312         hists__fprintf(hists, false, top->print_entries - printed, win_width,
313                        top->min_percent, stdout, symbol_conf.use_callchain);
314 }
315
316 static void prompt_integer(int *target, const char *msg)
317 {
318         char *buf = malloc(0), *p;
319         size_t dummy = 0;
320         int tmp;
321
322         fprintf(stdout, "\n%s: ", msg);
323         if (getline(&buf, &dummy, stdin) < 0)
324                 return;
325
326         p = strchr(buf, '\n');
327         if (p)
328                 *p = 0;
329
330         p = buf;
331         while(*p) {
332                 if (!isdigit(*p))
333                         goto out_free;
334                 p++;
335         }
336         tmp = strtoul(buf, NULL, 10);
337         *target = tmp;
338 out_free:
339         free(buf);
340 }
341
342 static void prompt_percent(int *target, const char *msg)
343 {
344         int tmp = 0;
345
346         prompt_integer(&tmp, msg);
347         if (tmp >= 0 && tmp <= 100)
348                 *target = tmp;
349 }
350
351 static void perf_top__prompt_symbol(struct perf_top *top, const char *msg)
352 {
353         char *buf = malloc(0), *p;
354         struct hist_entry *syme = top->sym_filter_entry, *n, *found = NULL;
355         struct hists *hists = evsel__hists(top->sym_evsel);
356         struct rb_node *next;
357         size_t dummy = 0;
358
359         /* zero counters of active symbol */
360         if (syme) {
361                 __zero_source_counters(syme);
362                 top->sym_filter_entry = NULL;
363         }
364
365         fprintf(stdout, "\n%s: ", msg);
366         if (getline(&buf, &dummy, stdin) < 0)
367                 goto out_free;
368
369         p = strchr(buf, '\n');
370         if (p)
371                 *p = 0;
372
373         next = rb_first(&hists->entries);
374         while (next) {
375                 n = rb_entry(next, struct hist_entry, rb_node);
376                 if (n->ms.sym && !strcmp(buf, n->ms.sym->name)) {
377                         found = n;
378                         break;
379                 }
380                 next = rb_next(&n->rb_node);
381         }
382
383         if (!found) {
384                 fprintf(stderr, "Sorry, %s is not active.\n", buf);
385                 sleep(1);
386         } else
387                 perf_top__parse_source(top, found);
388
389 out_free:
390         free(buf);
391 }
392
393 static void perf_top__print_mapped_keys(struct perf_top *top)
394 {
395         char *name = NULL;
396
397         if (top->sym_filter_entry) {
398                 struct symbol *sym = top->sym_filter_entry->ms.sym;
399                 name = sym->name;
400         }
401
402         fprintf(stdout, "\nMapped keys:\n");
403         fprintf(stdout, "\t[d]     display refresh delay.             \t(%d)\n", top->delay_secs);
404         fprintf(stdout, "\t[e]     display entries (lines).           \t(%d)\n", top->print_entries);
405
406         if (top->evlist->nr_entries > 1)
407                 fprintf(stdout, "\t[E]     active event counter.              \t(%s)\n", perf_evsel__name(top->sym_evsel));
408
409         fprintf(stdout, "\t[f]     profile display filter (count).    \t(%d)\n", top->count_filter);
410
411         fprintf(stdout, "\t[F]     annotate display filter (percent). \t(%d%%)\n", top->sym_pcnt_filter);
412         fprintf(stdout, "\t[s]     annotate symbol.                   \t(%s)\n", name?: "NULL");
413         fprintf(stdout, "\t[S]     stop annotation.\n");
414
415         fprintf(stdout,
416                 "\t[K]     hide kernel_symbols symbols.     \t(%s)\n",
417                 top->hide_kernel_symbols ? "yes" : "no");
418         fprintf(stdout,
419                 "\t[U]     hide user symbols.               \t(%s)\n",
420                 top->hide_user_symbols ? "yes" : "no");
421         fprintf(stdout, "\t[z]     toggle sample zeroing.             \t(%d)\n", top->zero ? 1 : 0);
422         fprintf(stdout, "\t[qQ]    quit.\n");
423 }
424
425 static int perf_top__key_mapped(struct perf_top *top, int c)
426 {
427         switch (c) {
428                 case 'd':
429                 case 'e':
430                 case 'f':
431                 case 'z':
432                 case 'q':
433                 case 'Q':
434                 case 'K':
435                 case 'U':
436                 case 'F':
437                 case 's':
438                 case 'S':
439                         return 1;
440                 case 'E':
441                         return top->evlist->nr_entries > 1 ? 1 : 0;
442                 default:
443                         break;
444         }
445
446         return 0;
447 }
448
449 static bool perf_top__handle_keypress(struct perf_top *top, int c)
450 {
451         bool ret = true;
452
453         if (!perf_top__key_mapped(top, c)) {
454                 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
455                 struct termios save;
456
457                 perf_top__print_mapped_keys(top);
458                 fprintf(stdout, "\nEnter selection, or unmapped key to continue: ");
459                 fflush(stdout);
460
461                 set_term_quiet_input(&save);
462
463                 poll(&stdin_poll, 1, -1);
464                 c = getc(stdin);
465
466                 tcsetattr(0, TCSAFLUSH, &save);
467                 if (!perf_top__key_mapped(top, c))
468                         return ret;
469         }
470
471         switch (c) {
472                 case 'd':
473                         prompt_integer(&top->delay_secs, "Enter display delay");
474                         if (top->delay_secs < 1)
475                                 top->delay_secs = 1;
476                         break;
477                 case 'e':
478                         prompt_integer(&top->print_entries, "Enter display entries (lines)");
479                         if (top->print_entries == 0) {
480                                 struct sigaction act = {
481                                         .sa_sigaction = perf_top__sig_winch,
482                                         .sa_flags     = SA_SIGINFO,
483                                 };
484                                 perf_top__resize(top);
485                                 sigaction(SIGWINCH, &act, NULL);
486                         } else {
487                                 signal(SIGWINCH, SIG_DFL);
488                         }
489                         break;
490                 case 'E':
491                         if (top->evlist->nr_entries > 1) {
492                                 /* Select 0 as the default event: */
493                                 int counter = 0;
494
495                                 fprintf(stderr, "\nAvailable events:");
496
497                                 evlist__for_each_entry(top->evlist, top->sym_evsel)
498                                         fprintf(stderr, "\n\t%d %s", top->sym_evsel->idx, perf_evsel__name(top->sym_evsel));
499
500                                 prompt_integer(&counter, "Enter details event counter");
501
502                                 if (counter >= top->evlist->nr_entries) {
503                                         top->sym_evsel = perf_evlist__first(top->evlist);
504                                         fprintf(stderr, "Sorry, no such event, using %s.\n", perf_evsel__name(top->sym_evsel));
505                                         sleep(1);
506                                         break;
507                                 }
508                                 evlist__for_each_entry(top->evlist, top->sym_evsel)
509                                         if (top->sym_evsel->idx == counter)
510                                                 break;
511                         } else
512                                 top->sym_evsel = perf_evlist__first(top->evlist);
513                         break;
514                 case 'f':
515                         prompt_integer(&top->count_filter, "Enter display event count filter");
516                         break;
517                 case 'F':
518                         prompt_percent(&top->sym_pcnt_filter,
519                                        "Enter details display event filter (percent)");
520                         break;
521                 case 'K':
522                         top->hide_kernel_symbols = !top->hide_kernel_symbols;
523                         break;
524                 case 'q':
525                 case 'Q':
526                         printf("exiting.\n");
527                         if (top->dump_symtab)
528                                 perf_session__fprintf_dsos(top->session, stderr);
529                         ret = false;
530                         break;
531                 case 's':
532                         perf_top__prompt_symbol(top, "Enter details symbol");
533                         break;
534                 case 'S':
535                         if (!top->sym_filter_entry)
536                                 break;
537                         else {
538                                 struct hist_entry *syme = top->sym_filter_entry;
539
540                                 top->sym_filter_entry = NULL;
541                                 __zero_source_counters(syme);
542                         }
543                         break;
544                 case 'U':
545                         top->hide_user_symbols = !top->hide_user_symbols;
546                         break;
547                 case 'z':
548                         top->zero = !top->zero;
549                         break;
550                 default:
551                         break;
552         }
553
554         return ret;
555 }
556
557 static void perf_top__sort_new_samples(void *arg)
558 {
559         struct perf_top *t = arg;
560         struct perf_evsel *evsel = t->sym_evsel;
561         struct hists *hists;
562
563         perf_top__reset_sample_counters(t);
564
565         if (t->evlist->selected != NULL)
566                 t->sym_evsel = t->evlist->selected;
567
568         hists = evsel__hists(evsel);
569
570         if (t->evlist->enabled) {
571                 if (t->zero) {
572                         hists__delete_entries(hists);
573                 } else {
574                         hists__decay_entries(hists, t->hide_user_symbols,
575                                              t->hide_kernel_symbols);
576                 }
577         }
578
579         hists__collapse_resort(hists, NULL);
580         perf_evsel__output_resort(evsel, NULL);
581 }
582
583 static void *display_thread_tui(void *arg)
584 {
585         struct perf_evsel *pos;
586         struct perf_top *top = arg;
587         const char *help = "For a higher level overview, try: perf top --sort comm,dso";
588         struct hist_browser_timer hbt = {
589                 .timer          = perf_top__sort_new_samples,
590                 .arg            = top,
591                 .refresh        = top->delay_secs,
592         };
593
594         /* In order to read symbols from other namespaces perf to  needs to call
595          * setns(2).  This isn't permitted if the struct_fs has multiple users.
596          * unshare(2) the fs so that we may continue to setns into namespaces
597          * that we're observing.
598          */
599         unshare(CLONE_FS);
600
601         perf_top__sort_new_samples(top);
602
603         /*
604          * Initialize the uid_filter_str, in the future the TUI will allow
605          * Zooming in/out UIDs. For now juse use whatever the user passed
606          * via --uid.
607          */
608         evlist__for_each_entry(top->evlist, pos) {
609                 struct hists *hists = evsel__hists(pos);
610                 hists->uid_filter_str = top->record_opts.target.uid_str;
611         }
612
613         perf_evlist__tui_browse_hists(top->evlist, help, &hbt,
614                                       top->min_percent,
615                                       &top->session->header.env);
616
617         done = 1;
618         return NULL;
619 }
620
621 static void display_sig(int sig __maybe_unused)
622 {
623         done = 1;
624 }
625
626 static void display_setup_sig(void)
627 {
628         signal(SIGSEGV, sighandler_dump_stack);
629         signal(SIGFPE, sighandler_dump_stack);
630         signal(SIGINT,  display_sig);
631         signal(SIGQUIT, display_sig);
632         signal(SIGTERM, display_sig);
633 }
634
635 static void *display_thread(void *arg)
636 {
637         struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
638         struct termios save;
639         struct perf_top *top = arg;
640         int delay_msecs, c;
641
642         /* In order to read symbols from other namespaces perf to  needs to call
643          * setns(2).  This isn't permitted if the struct_fs has multiple users.
644          * unshare(2) the fs so that we may continue to setns into namespaces
645          * that we're observing.
646          */
647         unshare(CLONE_FS);
648
649         display_setup_sig();
650         pthread__unblock_sigwinch();
651 repeat:
652         delay_msecs = top->delay_secs * MSEC_PER_SEC;
653         set_term_quiet_input(&save);
654         /* trash return*/
655         getc(stdin);
656
657         while (!done) {
658                 perf_top__print_sym_table(top);
659                 /*
660                  * Either timeout expired or we got an EINTR due to SIGWINCH,
661                  * refresh screen in both cases.
662                  */
663                 switch (poll(&stdin_poll, 1, delay_msecs)) {
664                 case 0:
665                         continue;
666                 case -1:
667                         if (errno == EINTR)
668                                 continue;
669                         __fallthrough;
670                 default:
671                         c = getc(stdin);
672                         tcsetattr(0, TCSAFLUSH, &save);
673
674                         if (perf_top__handle_keypress(top, c))
675                                 goto repeat;
676                         done = 1;
677                 }
678         }
679
680         tcsetattr(0, TCSAFLUSH, &save);
681         return NULL;
682 }
683
684 static int hist_iter__top_callback(struct hist_entry_iter *iter,
685                                    struct addr_location *al, bool single,
686                                    void *arg)
687 {
688         struct perf_top *top = arg;
689         struct hist_entry *he = iter->he;
690         struct perf_evsel *evsel = iter->evsel;
691
692         if (perf_hpp_list.sym && single)
693                 perf_top__record_precise_ip(top, he, iter->sample, evsel->idx, al->addr);
694
695         hist__account_cycles(iter->sample->branch_stack, al, iter->sample,
696                      !(top->record_opts.branch_stack & PERF_SAMPLE_BRANCH_ANY));
697         return 0;
698 }
699
700 static void perf_event__process_sample(struct perf_tool *tool,
701                                        const union perf_event *event,
702                                        struct perf_evsel *evsel,
703                                        struct perf_sample *sample,
704                                        struct machine *machine)
705 {
706         struct perf_top *top = container_of(tool, struct perf_top, tool);
707         struct addr_location al;
708         int err;
709
710         if (!machine && perf_guest) {
711                 static struct intlist *seen;
712
713                 if (!seen)
714                         seen = intlist__new(NULL);
715
716                 if (!intlist__has_entry(seen, sample->pid)) {
717                         pr_err("Can't find guest [%d]'s kernel information\n",
718                                 sample->pid);
719                         intlist__add(seen, sample->pid);
720                 }
721                 return;
722         }
723
724         if (!machine) {
725                 pr_err("%u unprocessable samples recorded.\r",
726                        top->session->evlist->stats.nr_unprocessable_samples++);
727                 return;
728         }
729
730         if (event->header.misc & PERF_RECORD_MISC_EXACT_IP)
731                 top->exact_samples++;
732
733         if (machine__resolve(machine, &al, sample) < 0)
734                 return;
735
736         if (!machine->kptr_restrict_warned &&
737             symbol_conf.kptr_restrict &&
738             al.cpumode == PERF_RECORD_MISC_KERNEL) {
739                 if (!perf_evlist__exclude_kernel(top->session->evlist)) {
740                         ui__warning(
741 "Kernel address maps (/proc/{kallsyms,modules}) are restricted.\n\n"
742 "Check /proc/sys/kernel/kptr_restrict.\n\n"
743 "Kernel%s samples will not be resolved.\n",
744                           al.map && !RB_EMPTY_ROOT(&al.map->dso->symbols[MAP__FUNCTION]) ?
745                           " modules" : "");
746                         if (use_browser <= 0)
747                                 sleep(5);
748                 }
749                 machine->kptr_restrict_warned = true;
750         }
751
752         if (al.sym == NULL) {
753                 const char *msg = "Kernel samples will not be resolved.\n";
754                 /*
755                  * As we do lazy loading of symtabs we only will know if the
756                  * specified vmlinux file is invalid when we actually have a
757                  * hit in kernel space and then try to load it. So if we get
758                  * here and there are _no_ symbols in the DSO backing the
759                  * kernel map, bail out.
760                  *
761                  * We may never get here, for instance, if we use -K/
762                  * --hide-kernel-symbols, even if the user specifies an
763                  * invalid --vmlinux ;-)
764                  */
765                 if (!machine->kptr_restrict_warned && !top->vmlinux_warned &&
766                     al.map == machine->vmlinux_maps[MAP__FUNCTION] &&
767                     RB_EMPTY_ROOT(&al.map->dso->symbols[MAP__FUNCTION])) {
768                         if (symbol_conf.vmlinux_name) {
769                                 char serr[256];
770                                 dso__strerror_load(al.map->dso, serr, sizeof(serr));
771                                 ui__warning("The %s file can't be used: %s\n%s",
772                                             symbol_conf.vmlinux_name, serr, msg);
773                         } else {
774                                 ui__warning("A vmlinux file was not found.\n%s",
775                                             msg);
776                         }
777
778                         if (use_browser <= 0)
779                                 sleep(5);
780                         top->vmlinux_warned = true;
781                 }
782         }
783
784         if (al.sym == NULL || !al.sym->idle) {
785                 struct hists *hists = evsel__hists(evsel);
786                 struct hist_entry_iter iter = {
787                         .evsel          = evsel,
788                         .sample         = sample,
789                         .add_entry_cb   = hist_iter__top_callback,
790                 };
791
792                 if (symbol_conf.cumulate_callchain)
793                         iter.ops = &hist_iter_cumulative;
794                 else
795                         iter.ops = &hist_iter_normal;
796
797                 pthread_mutex_lock(&hists->lock);
798
799                 err = hist_entry_iter__add(&iter, &al, top->max_stack, top);
800                 if (err < 0)
801                         pr_err("Problem incrementing symbol period, skipping event\n");
802
803                 pthread_mutex_unlock(&hists->lock);
804         }
805
806         addr_location__put(&al);
807 }
808
809 static void perf_top__mmap_read_idx(struct perf_top *top, int idx)
810 {
811         struct perf_sample sample;
812         struct perf_evsel *evsel;
813         struct perf_session *session = top->session;
814         union perf_event *event;
815         struct machine *machine;
816         int ret;
817
818         while ((event = perf_evlist__mmap_read(top->evlist, idx)) != NULL) {
819                 ret = perf_evlist__parse_sample(top->evlist, event, &sample);
820                 if (ret) {
821                         pr_err("Can't parse sample, err = %d\n", ret);
822                         goto next_event;
823                 }
824
825                 evsel = perf_evlist__id2evsel(session->evlist, sample.id);
826                 assert(evsel != NULL);
827
828                 if (event->header.type == PERF_RECORD_SAMPLE)
829                         ++top->samples;
830
831                 switch (sample.cpumode) {
832                 case PERF_RECORD_MISC_USER:
833                         ++top->us_samples;
834                         if (top->hide_user_symbols)
835                                 goto next_event;
836                         machine = &session->machines.host;
837                         break;
838                 case PERF_RECORD_MISC_KERNEL:
839                         ++top->kernel_samples;
840                         if (top->hide_kernel_symbols)
841                                 goto next_event;
842                         machine = &session->machines.host;
843                         break;
844                 case PERF_RECORD_MISC_GUEST_KERNEL:
845                         ++top->guest_kernel_samples;
846                         machine = perf_session__find_machine(session,
847                                                              sample.pid);
848                         break;
849                 case PERF_RECORD_MISC_GUEST_USER:
850                         ++top->guest_us_samples;
851                         /*
852                          * TODO: we don't process guest user from host side
853                          * except simple counting.
854                          */
855                         goto next_event;
856                 default:
857                         if (event->header.type == PERF_RECORD_SAMPLE)
858                                 goto next_event;
859                         machine = &session->machines.host;
860                         break;
861                 }
862
863
864                 if (event->header.type == PERF_RECORD_SAMPLE) {
865                         perf_event__process_sample(&top->tool, event, evsel,
866                                                    &sample, machine);
867                 } else if (event->header.type < PERF_RECORD_MAX) {
868                         hists__inc_nr_events(evsel__hists(evsel), event->header.type);
869                         machine__process_event(machine, event, &sample);
870                 } else
871                         ++session->evlist->stats.nr_unknown_events;
872 next_event:
873                 perf_evlist__mmap_consume(top->evlist, idx);
874         }
875 }
876
877 static void perf_top__mmap_read(struct perf_top *top)
878 {
879         int i;
880
881         for (i = 0; i < top->evlist->nr_mmaps; i++)
882                 perf_top__mmap_read_idx(top, i);
883 }
884
885 static int perf_top__start_counters(struct perf_top *top)
886 {
887         char msg[BUFSIZ];
888         struct perf_evsel *counter;
889         struct perf_evlist *evlist = top->evlist;
890         struct record_opts *opts = &top->record_opts;
891
892         perf_evlist__config(evlist, opts, &callchain_param);
893
894         evlist__for_each_entry(evlist, counter) {
895 try_again:
896                 if (perf_evsel__open(counter, top->evlist->cpus,
897                                      top->evlist->threads) < 0) {
898                         if (perf_evsel__fallback(counter, errno, msg, sizeof(msg))) {
899                                 if (verbose > 0)
900                                         ui__warning("%s\n", msg);
901                                 goto try_again;
902                         }
903
904                         perf_evsel__open_strerror(counter, &opts->target,
905                                                   errno, msg, sizeof(msg));
906                         ui__error("%s\n", msg);
907                         goto out_err;
908                 }
909         }
910
911         if (perf_evlist__mmap(evlist, opts->mmap_pages, false) < 0) {
912                 ui__error("Failed to mmap with %d (%s)\n",
913                             errno, str_error_r(errno, msg, sizeof(msg)));
914                 goto out_err;
915         }
916
917         return 0;
918
919 out_err:
920         return -1;
921 }
922
923 static int callchain_param__setup_sample_type(struct callchain_param *callchain)
924 {
925         if (!perf_hpp_list.sym) {
926                 if (callchain->enabled) {
927                         ui__error("Selected -g but \"sym\" not present in --sort/-s.");
928                         return -EINVAL;
929                 }
930         } else if (callchain->mode != CHAIN_NONE) {
931                 if (callchain_register_param(callchain) < 0) {
932                         ui__error("Can't register callchain params.\n");
933                         return -EINVAL;
934                 }
935         }
936
937         return 0;
938 }
939
940 static int __cmd_top(struct perf_top *top)
941 {
942         char msg[512];
943         struct perf_evsel *pos;
944         struct perf_evsel_config_term *err_term;
945         struct perf_evlist *evlist = top->evlist;
946         struct record_opts *opts = &top->record_opts;
947         pthread_t thread;
948         int ret;
949
950         top->session = perf_session__new(NULL, false, NULL);
951         if (top->session == NULL)
952                 return -1;
953
954         if (!objdump_path) {
955                 ret = perf_env__lookup_objdump(&top->session->header.env);
956                 if (ret)
957                         goto out_delete;
958         }
959
960         ret = callchain_param__setup_sample_type(&callchain_param);
961         if (ret)
962                 goto out_delete;
963
964         if (perf_session__register_idle_thread(top->session) < 0)
965                 goto out_delete;
966
967         if (top->nr_threads_synthesize > 1)
968                 perf_set_multithreaded();
969
970         machine__synthesize_threads(&top->session->machines.host, &opts->target,
971                                     top->evlist->threads, false,
972                                     opts->proc_map_timeout,
973                                     top->nr_threads_synthesize);
974
975         if (top->nr_threads_synthesize > 1)
976                 perf_set_singlethreaded();
977
978         if (perf_hpp_list.socket) {
979                 ret = perf_env__read_cpu_topology_map(&perf_env);
980                 if (ret < 0)
981                         goto out_err_cpu_topo;
982         }
983
984         ret = perf_top__start_counters(top);
985         if (ret)
986                 goto out_delete;
987
988         ret = perf_evlist__apply_drv_configs(evlist, &pos, &err_term);
989         if (ret) {
990                 pr_err("failed to set config \"%s\" on event %s with %d (%s)\n",
991                         err_term->val.drv_cfg, perf_evsel__name(pos), errno,
992                         str_error_r(errno, msg, sizeof(msg)));
993                 goto out_delete;
994         }
995
996         top->session->evlist = top->evlist;
997         perf_session__set_id_hdr_size(top->session);
998
999         /*
1000          * When perf is starting the traced process, all the events (apart from
1001          * group members) have enable_on_exec=1 set, so don't spoil it by
1002          * prematurely enabling them.
1003          *
1004          * XXX 'top' still doesn't start workloads like record, trace, but should,
1005          * so leave the check here.
1006          */
1007         if (!target__none(&opts->target))
1008                 perf_evlist__enable(top->evlist);
1009
1010         /* Wait for a minimal set of events before starting the snapshot */
1011         perf_evlist__poll(top->evlist, 100);
1012
1013         perf_top__mmap_read(top);
1014
1015         ret = -1;
1016         if (pthread_create(&thread, NULL, (use_browser > 0 ? display_thread_tui :
1017                                                             display_thread), top)) {
1018                 ui__error("Could not create display thread.\n");
1019                 goto out_delete;
1020         }
1021
1022         if (top->realtime_prio) {
1023                 struct sched_param param;
1024
1025                 param.sched_priority = top->realtime_prio;
1026                 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
1027                         ui__error("Could not set realtime priority.\n");
1028                         goto out_join;
1029                 }
1030         }
1031
1032         while (!done) {
1033                 u64 hits = top->samples;
1034
1035                 perf_top__mmap_read(top);
1036
1037                 if (hits == top->samples)
1038                         ret = perf_evlist__poll(top->evlist, 100);
1039
1040                 if (resize) {
1041                         perf_top__resize(top);
1042                         resize = 0;
1043                 }
1044         }
1045
1046         ret = 0;
1047 out_join:
1048         pthread_join(thread, NULL);
1049 out_delete:
1050         perf_session__delete(top->session);
1051         top->session = NULL;
1052
1053         return ret;
1054
1055 out_err_cpu_topo: {
1056         char errbuf[BUFSIZ];
1057         const char *err = str_error_r(-ret, errbuf, sizeof(errbuf));
1058
1059         ui__error("Could not read the CPU topology map: %s\n", err);
1060         goto out_delete;
1061 }
1062 }
1063
1064 static int
1065 callchain_opt(const struct option *opt, const char *arg, int unset)
1066 {
1067         symbol_conf.use_callchain = true;
1068         return record_callchain_opt(opt, arg, unset);
1069 }
1070
1071 static int
1072 parse_callchain_opt(const struct option *opt, const char *arg, int unset)
1073 {
1074         struct callchain_param *callchain = opt->value;
1075
1076         callchain->enabled = !unset;
1077         callchain->record_mode = CALLCHAIN_FP;
1078
1079         /*
1080          * --no-call-graph
1081          */
1082         if (unset) {
1083                 symbol_conf.use_callchain = false;
1084                 callchain->record_mode = CALLCHAIN_NONE;
1085                 return 0;
1086         }
1087
1088         return parse_callchain_top_opt(arg);
1089 }
1090
1091 static int perf_top_config(const char *var, const char *value, void *cb __maybe_unused)
1092 {
1093         if (!strcmp(var, "top.call-graph"))
1094                 var = "call-graph.record-mode"; /* fall-through */
1095         if (!strcmp(var, "top.children")) {
1096                 symbol_conf.cumulate_callchain = perf_config_bool(var, value);
1097                 return 0;
1098         }
1099
1100         return 0;
1101 }
1102
1103 static int
1104 parse_percent_limit(const struct option *opt, const char *arg,
1105                     int unset __maybe_unused)
1106 {
1107         struct perf_top *top = opt->value;
1108
1109         top->min_percent = strtof(arg, NULL);
1110         return 0;
1111 }
1112
1113 const char top_callchain_help[] = CALLCHAIN_RECORD_HELP CALLCHAIN_REPORT_HELP
1114         "\n\t\t\t\tDefault: fp,graph,0.5,caller,function";
1115
1116 int cmd_top(int argc, const char **argv)
1117 {
1118         char errbuf[BUFSIZ];
1119         struct perf_top top = {
1120                 .count_filter        = 5,
1121                 .delay_secs          = 2,
1122                 .record_opts = {
1123                         .mmap_pages     = UINT_MAX,
1124                         .user_freq      = UINT_MAX,
1125                         .user_interval  = ULLONG_MAX,
1126                         .freq           = 4000, /* 4 KHz */
1127                         .target         = {
1128                                 .uses_mmap   = true,
1129                         },
1130                         .proc_map_timeout    = 500,
1131                 },
1132                 .max_stack           = sysctl_perf_event_max_stack,
1133                 .sym_pcnt_filter     = 5,
1134                 .nr_threads_synthesize = UINT_MAX,
1135         };
1136         struct record_opts *opts = &top.record_opts;
1137         struct target *target = &opts->target;
1138         const struct option options[] = {
1139         OPT_CALLBACK('e', "event", &top.evlist, "event",
1140                      "event selector. use 'perf list' to list available events",
1141                      parse_events_option),
1142         OPT_U64('c', "count", &opts->user_interval, "event period to sample"),
1143         OPT_STRING('p', "pid", &target->pid, "pid",
1144                     "profile events on existing process id"),
1145         OPT_STRING('t', "tid", &target->tid, "tid",
1146                     "profile events on existing thread id"),
1147         OPT_BOOLEAN('a', "all-cpus", &target->system_wide,
1148                             "system-wide collection from all CPUs"),
1149         OPT_STRING('C', "cpu", &target->cpu_list, "cpu",
1150                     "list of cpus to monitor"),
1151         OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1152                    "file", "vmlinux pathname"),
1153         OPT_BOOLEAN(0, "ignore-vmlinux", &symbol_conf.ignore_vmlinux,
1154                     "don't load vmlinux even if found"),
1155         OPT_BOOLEAN('K', "hide_kernel_symbols", &top.hide_kernel_symbols,
1156                     "hide kernel symbols"),
1157         OPT_CALLBACK('m', "mmap-pages", &opts->mmap_pages, "pages",
1158                      "number of mmap data pages",
1159                      perf_evlist__parse_mmap_pages),
1160         OPT_INTEGER('r', "realtime", &top.realtime_prio,
1161                     "collect data with this RT SCHED_FIFO priority"),
1162         OPT_INTEGER('d', "delay", &top.delay_secs,
1163                     "number of seconds to delay between refreshes"),
1164         OPT_BOOLEAN('D', "dump-symtab", &top.dump_symtab,
1165                             "dump the symbol table used for profiling"),
1166         OPT_INTEGER('f', "count-filter", &top.count_filter,
1167                     "only display functions with more events than this"),
1168         OPT_BOOLEAN(0, "group", &opts->group,
1169                             "put the counters into a counter group"),
1170         OPT_BOOLEAN('i', "no-inherit", &opts->no_inherit,
1171                     "child tasks do not inherit counters"),
1172         OPT_STRING(0, "sym-annotate", &top.sym_filter, "symbol name",
1173                     "symbol to annotate"),
1174         OPT_BOOLEAN('z', "zero", &top.zero, "zero history across updates"),
1175         OPT_UINTEGER('F', "freq", &opts->user_freq, "profile at this frequency"),
1176         OPT_INTEGER('E', "entries", &top.print_entries,
1177                     "display this many functions"),
1178         OPT_BOOLEAN('U', "hide_user_symbols", &top.hide_user_symbols,
1179                     "hide user symbols"),
1180         OPT_BOOLEAN(0, "tui", &top.use_tui, "Use the TUI interface"),
1181         OPT_BOOLEAN(0, "stdio", &top.use_stdio, "Use the stdio interface"),
1182         OPT_INCR('v', "verbose", &verbose,
1183                     "be more verbose (show counter open errors, etc)"),
1184         OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1185                    "sort by key(s): pid, comm, dso, symbol, parent, cpu, srcline, ..."
1186                    " Please refer the man page for the complete list."),
1187         OPT_STRING(0, "fields", &field_order, "key[,keys...]",
1188                    "output field(s): overhead, period, sample plus all of sort keys"),
1189         OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
1190                     "Show a column with the number of samples"),
1191         OPT_CALLBACK_NOOPT('g', NULL, &callchain_param,
1192                            NULL, "enables call-graph recording and display",
1193                            &callchain_opt),
1194         OPT_CALLBACK(0, "call-graph", &callchain_param,
1195                      "record_mode[,record_size],print_type,threshold[,print_limit],order,sort_key[,branch]",
1196                      top_callchain_help, &parse_callchain_opt),
1197         OPT_BOOLEAN(0, "children", &symbol_conf.cumulate_callchain,
1198                     "Accumulate callchains of children and show total overhead as well"),
1199         OPT_INTEGER(0, "max-stack", &top.max_stack,
1200                     "Set the maximum stack depth when parsing the callchain. "
1201                     "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
1202         OPT_CALLBACK(0, "ignore-callees", NULL, "regex",
1203                    "ignore callees of these functions in call graphs",
1204                    report_parse_ignore_callees_opt),
1205         OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period,
1206                     "Show a column with the sum of periods"),
1207         OPT_STRING(0, "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
1208                    "only consider symbols in these dsos"),
1209         OPT_STRING(0, "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
1210                    "only consider symbols in these comms"),
1211         OPT_STRING(0, "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
1212                    "only consider these symbols"),
1213         OPT_BOOLEAN(0, "source", &symbol_conf.annotate_src,
1214                     "Interleave source code with assembly code (default)"),
1215         OPT_BOOLEAN(0, "asm-raw", &symbol_conf.annotate_asm_raw,
1216                     "Display raw encoding of assembly instructions (default)"),
1217         OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
1218                     "Enable kernel symbol demangling"),
1219         OPT_STRING(0, "objdump", &objdump_path, "path",
1220                     "objdump binary to use for disassembly and annotations"),
1221         OPT_STRING('M', "disassembler-style", &disassembler_style, "disassembler style",
1222                    "Specify disassembler style (e.g. -M intel for intel syntax)"),
1223         OPT_STRING('u', "uid", &target->uid_str, "user", "user to profile"),
1224         OPT_CALLBACK(0, "percent-limit", &top, "percent",
1225                      "Don't show entries under that percent", parse_percent_limit),
1226         OPT_CALLBACK(0, "percentage", NULL, "relative|absolute",
1227                      "How to display percentage of filtered entries", parse_filter_percentage),
1228         OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str,
1229                    "width[,width...]",
1230                    "don't try to adjust column width, use these fixed values"),
1231         OPT_UINTEGER(0, "proc-map-timeout", &opts->proc_map_timeout,
1232                         "per thread proc mmap processing timeout in ms"),
1233         OPT_CALLBACK_NOOPT('b', "branch-any", &opts->branch_stack,
1234                      "branch any", "sample any taken branches",
1235                      parse_branch_stack),
1236         OPT_CALLBACK('j', "branch-filter", &opts->branch_stack,
1237                      "branch filter mask", "branch stack filter modes",
1238                      parse_branch_stack),
1239         OPT_BOOLEAN(0, "raw-trace", &symbol_conf.raw_trace,
1240                     "Show raw trace event output (do not use print fmt or plugins)"),
1241         OPT_BOOLEAN(0, "hierarchy", &symbol_conf.report_hierarchy,
1242                     "Show entries in a hierarchy"),
1243         OPT_BOOLEAN(0, "force", &symbol_conf.force, "don't complain, do it"),
1244         OPT_UINTEGER(0, "num-thread-synthesize", &top.nr_threads_synthesize,
1245                         "number of thread to run event synthesize"),
1246         OPT_END()
1247         };
1248         const char * const top_usage[] = {
1249                 "perf top [<options>]",
1250                 NULL
1251         };
1252         int status = hists__init();
1253
1254         if (status < 0)
1255                 return status;
1256
1257         top.evlist = perf_evlist__new();
1258         if (top.evlist == NULL)
1259                 return -ENOMEM;
1260
1261         status = perf_config(perf_top_config, &top);
1262         if (status)
1263                 return status;
1264
1265         argc = parse_options(argc, argv, options, top_usage, 0);
1266         if (argc)
1267                 usage_with_options(top_usage, options);
1268
1269         if (!top.evlist->nr_entries &&
1270             perf_evlist__add_default(top.evlist) < 0) {
1271                 pr_err("Not enough memory for event selector list\n");
1272                 goto out_delete_evlist;
1273         }
1274
1275         if (symbol_conf.report_hierarchy) {
1276                 /* disable incompatible options */
1277                 symbol_conf.event_group = false;
1278                 symbol_conf.cumulate_callchain = false;
1279
1280                 if (field_order) {
1281                         pr_err("Error: --hierarchy and --fields options cannot be used together\n");
1282                         parse_options_usage(top_usage, options, "fields", 0);
1283                         parse_options_usage(NULL, options, "hierarchy", 0);
1284                         goto out_delete_evlist;
1285                 }
1286         }
1287
1288         sort__mode = SORT_MODE__TOP;
1289         /* display thread wants entries to be collapsed in a different tree */
1290         perf_hpp_list.need_collapse = 1;
1291
1292         if (top.use_stdio)
1293                 use_browser = 0;
1294         else if (top.use_tui)
1295                 use_browser = 1;
1296
1297         setup_browser(false);
1298
1299         if (setup_sorting(top.evlist) < 0) {
1300                 if (sort_order)
1301                         parse_options_usage(top_usage, options, "s", 1);
1302                 if (field_order)
1303                         parse_options_usage(sort_order ? NULL : top_usage,
1304                                             options, "fields", 0);
1305                 goto out_delete_evlist;
1306         }
1307
1308         status = target__validate(target);
1309         if (status) {
1310                 target__strerror(target, status, errbuf, BUFSIZ);
1311                 ui__warning("%s\n", errbuf);
1312         }
1313
1314         status = target__parse_uid(target);
1315         if (status) {
1316                 int saved_errno = errno;
1317
1318                 target__strerror(target, status, errbuf, BUFSIZ);
1319                 ui__error("%s\n", errbuf);
1320
1321                 status = -saved_errno;
1322                 goto out_delete_evlist;
1323         }
1324
1325         if (target__none(target))
1326                 target->system_wide = true;
1327
1328         if (perf_evlist__create_maps(top.evlist, target) < 0) {
1329                 ui__error("Couldn't create thread/CPU maps: %s\n",
1330                           errno == ENOENT ? "No such process" : str_error_r(errno, errbuf, sizeof(errbuf)));
1331                 goto out_delete_evlist;
1332         }
1333
1334         symbol_conf.nr_events = top.evlist->nr_entries;
1335
1336         if (top.delay_secs < 1)
1337                 top.delay_secs = 1;
1338
1339         if (record_opts__config(opts)) {
1340                 status = -EINVAL;
1341                 goto out_delete_evlist;
1342         }
1343
1344         top.sym_evsel = perf_evlist__first(top.evlist);
1345
1346         if (!callchain_param.enabled) {
1347                 symbol_conf.cumulate_callchain = false;
1348                 perf_hpp__cancel_cumulate();
1349         }
1350
1351         if (symbol_conf.cumulate_callchain && !callchain_param.order_set)
1352                 callchain_param.order = ORDER_CALLER;
1353
1354         status = symbol__annotation_init();
1355         if (status < 0)
1356                 goto out_delete_evlist;
1357
1358         symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL);
1359         if (symbol__init(NULL) < 0)
1360                 return -1;
1361
1362         sort__setup_elide(stdout);
1363
1364         get_term_dimensions(&top.winsize);
1365         if (top.print_entries == 0) {
1366                 struct sigaction act = {
1367                         .sa_sigaction = perf_top__sig_winch,
1368                         .sa_flags     = SA_SIGINFO,
1369                 };
1370                 perf_top__update_print_entries(&top);
1371                 sigaction(SIGWINCH, &act, NULL);
1372         }
1373
1374         status = __cmd_top(&top);
1375
1376 out_delete_evlist:
1377         perf_evlist__delete(top.evlist);
1378
1379         return status;
1380 }