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