xref: /openbmc/linux/tools/perf/builtin-top.c (revision a9c34a9f)
186470930SIngo Molnar /*
286470930SIngo Molnar  * builtin-top.c
386470930SIngo Molnar  *
486470930SIngo Molnar  * Builtin top command: Display a continuously updated profile of
586470930SIngo Molnar  * any workload, CPU or specific PID.
686470930SIngo Molnar  *
786470930SIngo Molnar  * Copyright (C) 2008, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
8ab81f3fdSArnaldo Carvalho de Melo  *		 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
986470930SIngo Molnar  *
1086470930SIngo Molnar  * Improvements and fixes by:
1186470930SIngo Molnar  *
1286470930SIngo Molnar  *   Arjan van de Ven <arjan@linux.intel.com>
1386470930SIngo Molnar  *   Yanmin Zhang <yanmin.zhang@intel.com>
1486470930SIngo Molnar  *   Wu Fengguang <fengguang.wu@intel.com>
1586470930SIngo Molnar  *   Mike Galbraith <efault@gmx.de>
1686470930SIngo Molnar  *   Paul Mackerras <paulus@samba.org>
1786470930SIngo Molnar  *
1886470930SIngo Molnar  * Released under the GPL v2. (and only v2, not any later version)
1986470930SIngo Molnar  */
2086470930SIngo Molnar #include "builtin.h"
2186470930SIngo Molnar 
2286470930SIngo Molnar #include "perf.h"
2386470930SIngo Molnar 
2436532461SArnaldo Carvalho de Melo #include "util/annotate.h"
25c0443df1SArnaldo Carvalho de Melo #include "util/cache.h"
2686470930SIngo Molnar #include "util/color.h"
27361c99a6SArnaldo Carvalho de Melo #include "util/evlist.h"
2869aad6f1SArnaldo Carvalho de Melo #include "util/evsel.h"
29b3165f41SArnaldo Carvalho de Melo #include "util/session.h"
30b3165f41SArnaldo Carvalho de Melo #include "util/symbol.h"
31439d473bSArnaldo Carvalho de Melo #include "util/thread.h"
32fd78260bSArnaldo Carvalho de Melo #include "util/thread_map.h"
338c3e10ebSArnaldo Carvalho de Melo #include "util/top.h"
3486470930SIngo Molnar #include "util/util.h"
3543cbcd8aSArnaldo Carvalho de Melo #include <linux/rbtree.h>
3686470930SIngo Molnar #include "util/parse-options.h"
3786470930SIngo Molnar #include "util/parse-events.h"
38a12b51c4SPaul Mackerras #include "util/cpumap.h"
3969aad6f1SArnaldo Carvalho de Melo #include "util/xyarray.h"
40ab81f3fdSArnaldo Carvalho de Melo #include "util/sort.h"
4186470930SIngo Molnar 
428f28827aSFrederic Weisbecker #include "util/debug.h"
438f28827aSFrederic Weisbecker 
4486470930SIngo Molnar #include <assert.h>
4531d68e7bSArnaldo Carvalho de Melo #include <elf.h>
4686470930SIngo Molnar #include <fcntl.h>
4786470930SIngo Molnar 
4886470930SIngo Molnar #include <stdio.h>
49923c42c1SMike Galbraith #include <termios.h>
50923c42c1SMike Galbraith #include <unistd.h>
519486aa38SArnaldo Carvalho de Melo #include <inttypes.h>
5286470930SIngo Molnar 
5386470930SIngo Molnar #include <errno.h>
5486470930SIngo Molnar #include <time.h>
5586470930SIngo Molnar #include <sched.h>
5686470930SIngo Molnar 
5786470930SIngo Molnar #include <sys/syscall.h>
5886470930SIngo Molnar #include <sys/ioctl.h>
5986470930SIngo Molnar #include <sys/poll.h>
6086470930SIngo Molnar #include <sys/prctl.h>
6186470930SIngo Molnar #include <sys/wait.h>
6286470930SIngo Molnar #include <sys/uio.h>
6331d68e7bSArnaldo Carvalho de Melo #include <sys/utsname.h>
6486470930SIngo Molnar #include <sys/mman.h>
6586470930SIngo Molnar 
6686470930SIngo Molnar #include <linux/unistd.h>
6786470930SIngo Molnar #include <linux/types.h>
6886470930SIngo Molnar 
69895f0edcSArnaldo Carvalho de Melo void get_term_dimensions(struct winsize *ws)
703b6ed988SArnaldo Carvalho de Melo {
7113cc5079SArnaldo Carvalho de Melo 	char *s = getenv("LINES");
723b6ed988SArnaldo Carvalho de Melo 
7313cc5079SArnaldo Carvalho de Melo 	if (s != NULL) {
7413cc5079SArnaldo Carvalho de Melo 		ws->ws_row = atoi(s);
7513cc5079SArnaldo Carvalho de Melo 		s = getenv("COLUMNS");
7613cc5079SArnaldo Carvalho de Melo 		if (s != NULL) {
7713cc5079SArnaldo Carvalho de Melo 			ws->ws_col = atoi(s);
7813cc5079SArnaldo Carvalho de Melo 			if (ws->ws_row && ws->ws_col)
7913cc5079SArnaldo Carvalho de Melo 				return;
8013cc5079SArnaldo Carvalho de Melo 		}
8113cc5079SArnaldo Carvalho de Melo 	}
823b6ed988SArnaldo Carvalho de Melo #ifdef TIOCGWINSZ
8313cc5079SArnaldo Carvalho de Melo 	if (ioctl(1, TIOCGWINSZ, ws) == 0 &&
8413cc5079SArnaldo Carvalho de Melo 	    ws->ws_row && ws->ws_col)
8513cc5079SArnaldo Carvalho de Melo 		return;
863b6ed988SArnaldo Carvalho de Melo #endif
8713cc5079SArnaldo Carvalho de Melo 	ws->ws_row = 25;
8813cc5079SArnaldo Carvalho de Melo 	ws->ws_col = 80;
893b6ed988SArnaldo Carvalho de Melo }
903b6ed988SArnaldo Carvalho de Melo 
911758af10SArnaldo Carvalho de Melo static void perf_top__update_print_entries(struct perf_top *top)
923b6ed988SArnaldo Carvalho de Melo {
931758af10SArnaldo Carvalho de Melo 	if (top->print_entries > 9)
941758af10SArnaldo Carvalho de Melo 		top->print_entries -= 9;
953b6ed988SArnaldo Carvalho de Melo }
963b6ed988SArnaldo Carvalho de Melo 
971758af10SArnaldo Carvalho de Melo static void perf_top__sig_winch(int sig __used, siginfo_t *info __used, void *arg)
983b6ed988SArnaldo Carvalho de Melo {
991758af10SArnaldo Carvalho de Melo 	struct perf_top *top = arg;
1001758af10SArnaldo Carvalho de Melo 
1011758af10SArnaldo Carvalho de Melo 	get_term_dimensions(&top->winsize);
102509605dbSStephane Eranian 	if (!top->print_entries
103509605dbSStephane Eranian 	    || (top->print_entries+4) > top->winsize.ws_row) {
104509605dbSStephane Eranian 		top->print_entries = top->winsize.ws_row;
105509605dbSStephane Eranian 	} else {
106509605dbSStephane Eranian 		top->print_entries += 4;
107509605dbSStephane Eranian 		top->winsize.ws_row = top->print_entries;
108509605dbSStephane Eranian 	}
1091758af10SArnaldo Carvalho de Melo 	perf_top__update_print_entries(top);
1103b6ed988SArnaldo Carvalho de Melo }
1113b6ed988SArnaldo Carvalho de Melo 
1121758af10SArnaldo Carvalho de Melo static int perf_top__parse_source(struct perf_top *top, struct hist_entry *he)
113923c42c1SMike Galbraith {
114923c42c1SMike Galbraith 	struct symbol *sym;
115ce6f4fabSArnaldo Carvalho de Melo 	struct annotation *notes;
116439d473bSArnaldo Carvalho de Melo 	struct map *map;
11736532461SArnaldo Carvalho de Melo 	int err = -1;
118923c42c1SMike Galbraith 
119ab81f3fdSArnaldo Carvalho de Melo 	if (!he || !he->ms.sym)
120b0a9ab62SArnaldo Carvalho de Melo 		return -1;
121b0a9ab62SArnaldo Carvalho de Melo 
122ab81f3fdSArnaldo Carvalho de Melo 	sym = he->ms.sym;
123ab81f3fdSArnaldo Carvalho de Melo 	map = he->ms.map;
124b0a9ab62SArnaldo Carvalho de Melo 
125b0a9ab62SArnaldo Carvalho de Melo 	/*
126b0a9ab62SArnaldo Carvalho de Melo 	 * We can't annotate with just /proc/kallsyms
127b0a9ab62SArnaldo Carvalho de Melo 	 */
128878b439dSArnaldo Carvalho de Melo 	if (map->dso->symtab_type == SYMTAB__KALLSYMS) {
129ce6f4fabSArnaldo Carvalho de Melo 		pr_err("Can't annotate %s: No vmlinux file was found in the "
130ce6f4fabSArnaldo Carvalho de Melo 		       "path\n", sym->name);
131ce6f4fabSArnaldo Carvalho de Melo 		sleep(1);
132b0a9ab62SArnaldo Carvalho de Melo 		return -1;
133b269876cSArnaldo Carvalho de Melo 	}
134b269876cSArnaldo Carvalho de Melo 
135ce6f4fabSArnaldo Carvalho de Melo 	notes = symbol__annotation(sym);
136ce6f4fabSArnaldo Carvalho de Melo 	if (notes->src != NULL) {
137ce6f4fabSArnaldo Carvalho de Melo 		pthread_mutex_lock(&notes->lock);
138923c42c1SMike Galbraith 		goto out_assign;
139923c42c1SMike Galbraith 	}
140923c42c1SMike Galbraith 
141ce6f4fabSArnaldo Carvalho de Melo 	pthread_mutex_lock(&notes->lock);
142923c42c1SMike Galbraith 
143d04b35f8SArnaldo Carvalho de Melo 	if (symbol__alloc_hist(sym) < 0) {
144c97cf422SArnaldo Carvalho de Melo 		pthread_mutex_unlock(&notes->lock);
14536532461SArnaldo Carvalho de Melo 		pr_err("Not enough memory for annotating '%s' symbol!\n",
14636532461SArnaldo Carvalho de Melo 		       sym->name);
147ce6f4fabSArnaldo Carvalho de Melo 		sleep(1);
148c97cf422SArnaldo Carvalho de Melo 		return err;
149923c42c1SMike Galbraith 	}
15036532461SArnaldo Carvalho de Melo 
151ab81f3fdSArnaldo Carvalho de Melo 	err = symbol__annotate(sym, map, 0);
15236532461SArnaldo Carvalho de Melo 	if (err == 0) {
153923c42c1SMike Galbraith out_assign:
1541758af10SArnaldo Carvalho de Melo 		top->sym_filter_entry = he;
15536532461SArnaldo Carvalho de Melo 	}
156c97cf422SArnaldo Carvalho de Melo 
157ce6f4fabSArnaldo Carvalho de Melo 	pthread_mutex_unlock(&notes->lock);
15836532461SArnaldo Carvalho de Melo 	return err;
159923c42c1SMike Galbraith }
160923c42c1SMike Galbraith 
161ab81f3fdSArnaldo Carvalho de Melo static void __zero_source_counters(struct hist_entry *he)
162923c42c1SMike Galbraith {
163ab81f3fdSArnaldo Carvalho de Melo 	struct symbol *sym = he->ms.sym;
16436532461SArnaldo Carvalho de Melo 	symbol__annotate_zero_histograms(sym);
165923c42c1SMike Galbraith }
166923c42c1SMike Galbraith 
16731d68e7bSArnaldo Carvalho de Melo static void ui__warn_map_erange(struct map *map, struct symbol *sym, u64 ip)
16831d68e7bSArnaldo Carvalho de Melo {
16931d68e7bSArnaldo Carvalho de Melo 	struct utsname uts;
17031d68e7bSArnaldo Carvalho de Melo 	int err = uname(&uts);
17131d68e7bSArnaldo Carvalho de Melo 
17231d68e7bSArnaldo Carvalho de Melo 	ui__warning("Out of bounds address found:\n\n"
17331d68e7bSArnaldo Carvalho de Melo 		    "Addr:   %" PRIx64 "\n"
17431d68e7bSArnaldo Carvalho de Melo 		    "DSO:    %s %c\n"
17531d68e7bSArnaldo Carvalho de Melo 		    "Map:    %" PRIx64 "-%" PRIx64 "\n"
17631d68e7bSArnaldo Carvalho de Melo 		    "Symbol: %" PRIx64 "-%" PRIx64 " %c %s\n"
17731d68e7bSArnaldo Carvalho de Melo 		    "Arch:   %s\n"
17831d68e7bSArnaldo Carvalho de Melo 		    "Kernel: %s\n"
17931d68e7bSArnaldo Carvalho de Melo 		    "Tools:  %s\n\n"
18031d68e7bSArnaldo Carvalho de Melo 		    "Not all samples will be on the annotation output.\n\n"
18131d68e7bSArnaldo Carvalho de Melo 		    "Please report to linux-kernel@vger.kernel.org\n",
18231d68e7bSArnaldo Carvalho de Melo 		    ip, map->dso->long_name, dso__symtab_origin(map->dso),
18331d68e7bSArnaldo Carvalho de Melo 		    map->start, map->end, sym->start, sym->end,
18431d68e7bSArnaldo Carvalho de Melo 		    sym->binding == STB_GLOBAL ? 'g' :
18531d68e7bSArnaldo Carvalho de Melo 		    sym->binding == STB_LOCAL  ? 'l' : 'w', sym->name,
18631d68e7bSArnaldo Carvalho de Melo 		    err ? "[unknown]" : uts.machine,
18731d68e7bSArnaldo Carvalho de Melo 		    err ? "[unknown]" : uts.release, perf_version_string);
18831d68e7bSArnaldo Carvalho de Melo 	if (use_browser <= 0)
18931d68e7bSArnaldo Carvalho de Melo 		sleep(5);
19031d68e7bSArnaldo Carvalho de Melo 
19131d68e7bSArnaldo Carvalho de Melo 	map->erange_warned = true;
19231d68e7bSArnaldo Carvalho de Melo }
19331d68e7bSArnaldo Carvalho de Melo 
1941758af10SArnaldo Carvalho de Melo static void perf_top__record_precise_ip(struct perf_top *top,
1951758af10SArnaldo Carvalho de Melo 					struct hist_entry *he,
1961758af10SArnaldo Carvalho de Melo 					int counter, u64 ip)
197923c42c1SMike Galbraith {
198ce6f4fabSArnaldo Carvalho de Melo 	struct annotation *notes;
199ce6f4fabSArnaldo Carvalho de Melo 	struct symbol *sym;
20031d68e7bSArnaldo Carvalho de Melo 	int err;
201ce6f4fabSArnaldo Carvalho de Melo 
202ab81f3fdSArnaldo Carvalho de Melo 	if (he == NULL || he->ms.sym == NULL ||
2031758af10SArnaldo Carvalho de Melo 	    ((top->sym_filter_entry == NULL ||
2041758af10SArnaldo Carvalho de Melo 	      top->sym_filter_entry->ms.sym != he->ms.sym) && use_browser != 1))
205923c42c1SMike Galbraith 		return;
206923c42c1SMike Galbraith 
207ab81f3fdSArnaldo Carvalho de Melo 	sym = he->ms.sym;
208ce6f4fabSArnaldo Carvalho de Melo 	notes = symbol__annotation(sym);
209ce6f4fabSArnaldo Carvalho de Melo 
210ce6f4fabSArnaldo Carvalho de Melo 	if (pthread_mutex_trylock(&notes->lock))
211923c42c1SMike Galbraith 		return;
212923c42c1SMike Galbraith 
213d04b35f8SArnaldo Carvalho de Melo 	if (notes->src == NULL && symbol__alloc_hist(sym) < 0) {
214ab81f3fdSArnaldo Carvalho de Melo 		pthread_mutex_unlock(&notes->lock);
215ab81f3fdSArnaldo Carvalho de Melo 		pr_err("Not enough memory for annotating '%s' symbol!\n",
216ab81f3fdSArnaldo Carvalho de Melo 		       sym->name);
217ab81f3fdSArnaldo Carvalho de Melo 		sleep(1);
218ab81f3fdSArnaldo Carvalho de Melo 		return;
219ab81f3fdSArnaldo Carvalho de Melo 	}
220ab81f3fdSArnaldo Carvalho de Melo 
221ab81f3fdSArnaldo Carvalho de Melo 	ip = he->ms.map->map_ip(he->ms.map, ip);
22231d68e7bSArnaldo Carvalho de Melo 	err = symbol__inc_addr_samples(sym, he->ms.map, counter, ip);
223923c42c1SMike Galbraith 
224ce6f4fabSArnaldo Carvalho de Melo 	pthread_mutex_unlock(&notes->lock);
22531d68e7bSArnaldo Carvalho de Melo 
22631d68e7bSArnaldo Carvalho de Melo 	if (err == -ERANGE && !he->ms.map->erange_warned)
22731d68e7bSArnaldo Carvalho de Melo 		ui__warn_map_erange(he->ms.map, sym, ip);
228923c42c1SMike Galbraith }
229923c42c1SMike Galbraith 
2301758af10SArnaldo Carvalho de Melo static void perf_top__show_details(struct perf_top *top)
231923c42c1SMike Galbraith {
2321758af10SArnaldo Carvalho de Melo 	struct hist_entry *he = top->sym_filter_entry;
233ce6f4fabSArnaldo Carvalho de Melo 	struct annotation *notes;
234923c42c1SMike Galbraith 	struct symbol *symbol;
23536532461SArnaldo Carvalho de Melo 	int more;
236923c42c1SMike Galbraith 
237ab81f3fdSArnaldo Carvalho de Melo 	if (!he)
238923c42c1SMike Galbraith 		return;
239923c42c1SMike Galbraith 
240ab81f3fdSArnaldo Carvalho de Melo 	symbol = he->ms.sym;
241ce6f4fabSArnaldo Carvalho de Melo 	notes = symbol__annotation(symbol);
242ce6f4fabSArnaldo Carvalho de Melo 
243ce6f4fabSArnaldo Carvalho de Melo 	pthread_mutex_lock(&notes->lock);
244ce6f4fabSArnaldo Carvalho de Melo 
245ce6f4fabSArnaldo Carvalho de Melo 	if (notes->src == NULL)
246ce6f4fabSArnaldo Carvalho de Melo 		goto out_unlock;
247923c42c1SMike Galbraith 
2487289f83cSArnaldo Carvalho de Melo 	printf("Showing %s for %s\n", perf_evsel__name(top->sym_evsel), symbol->name);
2491758af10SArnaldo Carvalho de Melo 	printf("  Events  Pcnt (>=%d%%)\n", top->sym_pcnt_filter);
250923c42c1SMike Galbraith 
2511758af10SArnaldo Carvalho de Melo 	more = symbol__annotate_printf(symbol, he->ms.map, top->sym_evsel->idx,
2521758af10SArnaldo Carvalho de Melo 				       0, top->sym_pcnt_filter, top->print_entries, 4);
2531758af10SArnaldo Carvalho de Melo 	if (top->zero)
2541758af10SArnaldo Carvalho de Melo 		symbol__annotate_zero_histogram(symbol, top->sym_evsel->idx);
25536532461SArnaldo Carvalho de Melo 	else
2561758af10SArnaldo Carvalho de Melo 		symbol__annotate_decay_histogram(symbol, top->sym_evsel->idx);
25736532461SArnaldo Carvalho de Melo 	if (more != 0)
258923c42c1SMike Galbraith 		printf("%d lines not displayed, maybe increase display entries [e]\n", more);
259ce6f4fabSArnaldo Carvalho de Melo out_unlock:
260ce6f4fabSArnaldo Carvalho de Melo 	pthread_mutex_unlock(&notes->lock);
261923c42c1SMike Galbraith }
26286470930SIngo Molnar 
26386470930SIngo Molnar static const char		CONSOLE_CLEAR[] = "";
26486470930SIngo Molnar 
265743eb868SArnaldo Carvalho de Melo static struct hist_entry *perf_evsel__add_hist_entry(struct perf_evsel *evsel,
266ab81f3fdSArnaldo Carvalho de Melo 						     struct addr_location *al,
267743eb868SArnaldo Carvalho de Melo 						     struct perf_sample *sample)
26886470930SIngo Molnar {
269ab81f3fdSArnaldo Carvalho de Melo 	struct hist_entry *he;
270ab81f3fdSArnaldo Carvalho de Melo 
271ab81f3fdSArnaldo Carvalho de Melo 	he = __hists__add_entry(&evsel->hists, al, NULL, sample->period);
272ab81f3fdSArnaldo Carvalho de Melo 	if (he == NULL)
273ab81f3fdSArnaldo Carvalho de Melo 		return NULL;
274ab81f3fdSArnaldo Carvalho de Melo 
275ab81f3fdSArnaldo Carvalho de Melo 	hists__inc_nr_events(&evsel->hists, PERF_RECORD_SAMPLE);
276ab81f3fdSArnaldo Carvalho de Melo 	return he;
27786470930SIngo Molnar }
27886470930SIngo Molnar 
2791758af10SArnaldo Carvalho de Melo static void perf_top__print_sym_table(struct perf_top *top)
28086470930SIngo Molnar {
2818c3e10ebSArnaldo Carvalho de Melo 	char bf[160];
2828c3e10ebSArnaldo Carvalho de Melo 	int printed = 0;
2831758af10SArnaldo Carvalho de Melo 	const int win_width = top->winsize.ws_col - 1;
28486470930SIngo Molnar 
28586470930SIngo Molnar 	puts(CONSOLE_CLEAR);
28686470930SIngo Molnar 
2871758af10SArnaldo Carvalho de Melo 	perf_top__header_snprintf(top, bf, sizeof(bf));
2888c3e10ebSArnaldo Carvalho de Melo 	printf("%s\n", bf);
28986470930SIngo Molnar 
2901758af10SArnaldo Carvalho de Melo 	perf_top__reset_sample_counters(top);
29186470930SIngo Molnar 
2921a105f74SArnaldo Carvalho de Melo 	printf("%-*.*s\n", win_width, win_width, graph_dotted_line);
29386470930SIngo Molnar 
2941758af10SArnaldo Carvalho de Melo 	if (top->sym_evsel->hists.stats.nr_lost_warned !=
2951758af10SArnaldo Carvalho de Melo 	    top->sym_evsel->hists.stats.nr_events[PERF_RECORD_LOST]) {
2961758af10SArnaldo Carvalho de Melo 		top->sym_evsel->hists.stats.nr_lost_warned =
2971758af10SArnaldo Carvalho de Melo 			top->sym_evsel->hists.stats.nr_events[PERF_RECORD_LOST];
2987b27509fSArnaldo Carvalho de Melo 		color_fprintf(stdout, PERF_COLOR_RED,
2997b27509fSArnaldo Carvalho de Melo 			      "WARNING: LOST %d chunks, Check IO/CPU overload",
3001758af10SArnaldo Carvalho de Melo 			      top->sym_evsel->hists.stats.nr_lost_warned);
301ab81f3fdSArnaldo Carvalho de Melo 		++printed;
30293fc64f1SArnaldo Carvalho de Melo 	}
30393fc64f1SArnaldo Carvalho de Melo 
3041758af10SArnaldo Carvalho de Melo 	if (top->sym_filter_entry) {
3051758af10SArnaldo Carvalho de Melo 		perf_top__show_details(top);
306923c42c1SMike Galbraith 		return;
307923c42c1SMike Galbraith 	}
308923c42c1SMike Galbraith 
3091758af10SArnaldo Carvalho de Melo 	hists__collapse_resort_threaded(&top->sym_evsel->hists);
3101758af10SArnaldo Carvalho de Melo 	hists__output_resort_threaded(&top->sym_evsel->hists);
3111758af10SArnaldo Carvalho de Melo 	hists__decay_entries_threaded(&top->sym_evsel->hists,
3121758af10SArnaldo Carvalho de Melo 				      top->hide_user_symbols,
3131758af10SArnaldo Carvalho de Melo 				      top->hide_kernel_symbols);
3141758af10SArnaldo Carvalho de Melo 	hists__output_recalc_col_len(&top->sym_evsel->hists,
3151758af10SArnaldo Carvalho de Melo 				     top->winsize.ws_row - 3);
3161a105f74SArnaldo Carvalho de Melo 	putchar('\n');
3171758af10SArnaldo Carvalho de Melo 	hists__fprintf(&top->sym_evsel->hists, NULL, false, false,
3181758af10SArnaldo Carvalho de Melo 		       top->winsize.ws_row - 4 - printed, win_width, stdout);
31986470930SIngo Molnar }
32086470930SIngo Molnar 
321923c42c1SMike Galbraith static void prompt_integer(int *target, const char *msg)
322923c42c1SMike Galbraith {
323923c42c1SMike Galbraith 	char *buf = malloc(0), *p;
324923c42c1SMike Galbraith 	size_t dummy = 0;
325923c42c1SMike Galbraith 	int tmp;
326923c42c1SMike Galbraith 
327923c42c1SMike Galbraith 	fprintf(stdout, "\n%s: ", msg);
328923c42c1SMike Galbraith 	if (getline(&buf, &dummy, stdin) < 0)
329923c42c1SMike Galbraith 		return;
330923c42c1SMike Galbraith 
331923c42c1SMike Galbraith 	p = strchr(buf, '\n');
332923c42c1SMike Galbraith 	if (p)
333923c42c1SMike Galbraith 		*p = 0;
334923c42c1SMike Galbraith 
335923c42c1SMike Galbraith 	p = buf;
336923c42c1SMike Galbraith 	while(*p) {
337923c42c1SMike Galbraith 		if (!isdigit(*p))
338923c42c1SMike Galbraith 			goto out_free;
339923c42c1SMike Galbraith 		p++;
340923c42c1SMike Galbraith 	}
341923c42c1SMike Galbraith 	tmp = strtoul(buf, NULL, 10);
342923c42c1SMike Galbraith 	*target = tmp;
343923c42c1SMike Galbraith out_free:
344923c42c1SMike Galbraith 	free(buf);
345923c42c1SMike Galbraith }
346923c42c1SMike Galbraith 
347923c42c1SMike Galbraith static void prompt_percent(int *target, const char *msg)
348923c42c1SMike Galbraith {
349923c42c1SMike Galbraith 	int tmp = 0;
350923c42c1SMike Galbraith 
351923c42c1SMike Galbraith 	prompt_integer(&tmp, msg);
352923c42c1SMike Galbraith 	if (tmp >= 0 && tmp <= 100)
353923c42c1SMike Galbraith 		*target = tmp;
354923c42c1SMike Galbraith }
355923c42c1SMike Galbraith 
3561758af10SArnaldo Carvalho de Melo static void perf_top__prompt_symbol(struct perf_top *top, const char *msg)
357923c42c1SMike Galbraith {
358923c42c1SMike Galbraith 	char *buf = malloc(0), *p;
3591758af10SArnaldo Carvalho de Melo 	struct hist_entry *syme = top->sym_filter_entry, *n, *found = NULL;
360ab81f3fdSArnaldo Carvalho de Melo 	struct rb_node *next;
361923c42c1SMike Galbraith 	size_t dummy = 0;
362923c42c1SMike Galbraith 
363923c42c1SMike Galbraith 	/* zero counters of active symbol */
364923c42c1SMike Galbraith 	if (syme) {
365923c42c1SMike Galbraith 		__zero_source_counters(syme);
3661758af10SArnaldo Carvalho de Melo 		top->sym_filter_entry = NULL;
367923c42c1SMike Galbraith 	}
368923c42c1SMike Galbraith 
369923c42c1SMike Galbraith 	fprintf(stdout, "\n%s: ", msg);
370923c42c1SMike Galbraith 	if (getline(&buf, &dummy, stdin) < 0)
371923c42c1SMike Galbraith 		goto out_free;
372923c42c1SMike Galbraith 
373923c42c1SMike Galbraith 	p = strchr(buf, '\n');
374923c42c1SMike Galbraith 	if (p)
375923c42c1SMike Galbraith 		*p = 0;
376923c42c1SMike Galbraith 
3771758af10SArnaldo Carvalho de Melo 	next = rb_first(&top->sym_evsel->hists.entries);
378ab81f3fdSArnaldo Carvalho de Melo 	while (next) {
379ab81f3fdSArnaldo Carvalho de Melo 		n = rb_entry(next, struct hist_entry, rb_node);
380ab81f3fdSArnaldo Carvalho de Melo 		if (n->ms.sym && !strcmp(buf, n->ms.sym->name)) {
381ab81f3fdSArnaldo Carvalho de Melo 			found = n;
382923c42c1SMike Galbraith 			break;
383923c42c1SMike Galbraith 		}
384ab81f3fdSArnaldo Carvalho de Melo 		next = rb_next(&n->rb_node);
385923c42c1SMike Galbraith 	}
386923c42c1SMike Galbraith 
387923c42c1SMike Galbraith 	if (!found) {
38866aeb6d5SKirill Smelkov 		fprintf(stderr, "Sorry, %s is not active.\n", buf);
389923c42c1SMike Galbraith 		sleep(1);
390923c42c1SMike Galbraith 	} else
3911758af10SArnaldo Carvalho de Melo 		perf_top__parse_source(top, found);
392923c42c1SMike Galbraith 
393923c42c1SMike Galbraith out_free:
394923c42c1SMike Galbraith 	free(buf);
395923c42c1SMike Galbraith }
396923c42c1SMike Galbraith 
3971758af10SArnaldo Carvalho de Melo static void perf_top__print_mapped_keys(struct perf_top *top)
398923c42c1SMike Galbraith {
399091bd2e9SMike Galbraith 	char *name = NULL;
400091bd2e9SMike Galbraith 
4011758af10SArnaldo Carvalho de Melo 	if (top->sym_filter_entry) {
4021758af10SArnaldo Carvalho de Melo 		struct symbol *sym = top->sym_filter_entry->ms.sym;
403091bd2e9SMike Galbraith 		name = sym->name;
404091bd2e9SMike Galbraith 	}
405091bd2e9SMike Galbraith 
406091bd2e9SMike Galbraith 	fprintf(stdout, "\nMapped keys:\n");
4071758af10SArnaldo Carvalho de Melo 	fprintf(stdout, "\t[d]     display refresh delay.             \t(%d)\n", top->delay_secs);
4081758af10SArnaldo Carvalho de Melo 	fprintf(stdout, "\t[e]     display entries (lines).           \t(%d)\n", top->print_entries);
409091bd2e9SMike Galbraith 
4101758af10SArnaldo Carvalho de Melo 	if (top->evlist->nr_entries > 1)
4117289f83cSArnaldo Carvalho de Melo 		fprintf(stdout, "\t[E]     active event counter.              \t(%s)\n", perf_evsel__name(top->sym_evsel));
412091bd2e9SMike Galbraith 
4131758af10SArnaldo Carvalho de Melo 	fprintf(stdout, "\t[f]     profile display filter (count).    \t(%d)\n", top->count_filter);
414091bd2e9SMike Galbraith 
4151758af10SArnaldo Carvalho de Melo 	fprintf(stdout, "\t[F]     annotate display filter (percent). \t(%d%%)\n", top->sym_pcnt_filter);
416091bd2e9SMike Galbraith 	fprintf(stdout, "\t[s]     annotate symbol.                   \t(%s)\n", name?: "NULL");
417091bd2e9SMike Galbraith 	fprintf(stdout, "\t[S]     stop annotation.\n");
418091bd2e9SMike Galbraith 
4198ffcda17SArnaldo Carvalho de Melo 	fprintf(stdout,
4208ffcda17SArnaldo Carvalho de Melo 		"\t[K]     hide kernel_symbols symbols.     \t(%s)\n",
4211758af10SArnaldo Carvalho de Melo 		top->hide_kernel_symbols ? "yes" : "no");
4228ffcda17SArnaldo Carvalho de Melo 	fprintf(stdout,
4238ffcda17SArnaldo Carvalho de Melo 		"\t[U]     hide user symbols.               \t(%s)\n",
4241758af10SArnaldo Carvalho de Melo 		top->hide_user_symbols ? "yes" : "no");
4251758af10SArnaldo Carvalho de Melo 	fprintf(stdout, "\t[z]     toggle sample zeroing.             \t(%d)\n", top->zero ? 1 : 0);
426091bd2e9SMike Galbraith 	fprintf(stdout, "\t[qQ]    quit.\n");
427091bd2e9SMike Galbraith }
428091bd2e9SMike Galbraith 
4291758af10SArnaldo Carvalho de Melo static int perf_top__key_mapped(struct perf_top *top, int c)
430091bd2e9SMike Galbraith {
431091bd2e9SMike Galbraith 	switch (c) {
432091bd2e9SMike Galbraith 		case 'd':
433091bd2e9SMike Galbraith 		case 'e':
434091bd2e9SMike Galbraith 		case 'f':
435091bd2e9SMike Galbraith 		case 'z':
436091bd2e9SMike Galbraith 		case 'q':
437091bd2e9SMike Galbraith 		case 'Q':
4388ffcda17SArnaldo Carvalho de Melo 		case 'K':
4398ffcda17SArnaldo Carvalho de Melo 		case 'U':
4406cff0e8dSKirill Smelkov 		case 'F':
4416cff0e8dSKirill Smelkov 		case 's':
4426cff0e8dSKirill Smelkov 		case 'S':
443091bd2e9SMike Galbraith 			return 1;
444091bd2e9SMike Galbraith 		case 'E':
4451758af10SArnaldo Carvalho de Melo 			return top->evlist->nr_entries > 1 ? 1 : 0;
44683a0944fSIngo Molnar 		default:
44783a0944fSIngo Molnar 			break;
448091bd2e9SMike Galbraith 	}
449091bd2e9SMike Galbraith 
450091bd2e9SMike Galbraith 	return 0;
451923c42c1SMike Galbraith }
452923c42c1SMike Galbraith 
4531758af10SArnaldo Carvalho de Melo static void perf_top__handle_keypress(struct perf_top *top, int c)
454923c42c1SMike Galbraith {
4551758af10SArnaldo Carvalho de Melo 	if (!perf_top__key_mapped(top, c)) {
456091bd2e9SMike Galbraith 		struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
457091bd2e9SMike Galbraith 		struct termios tc, save;
458091bd2e9SMike Galbraith 
4591758af10SArnaldo Carvalho de Melo 		perf_top__print_mapped_keys(top);
460091bd2e9SMike Galbraith 		fprintf(stdout, "\nEnter selection, or unmapped key to continue: ");
461091bd2e9SMike Galbraith 		fflush(stdout);
462091bd2e9SMike Galbraith 
463091bd2e9SMike Galbraith 		tcgetattr(0, &save);
464091bd2e9SMike Galbraith 		tc = save;
465091bd2e9SMike Galbraith 		tc.c_lflag &= ~(ICANON | ECHO);
466091bd2e9SMike Galbraith 		tc.c_cc[VMIN] = 0;
467091bd2e9SMike Galbraith 		tc.c_cc[VTIME] = 0;
468091bd2e9SMike Galbraith 		tcsetattr(0, TCSANOW, &tc);
469091bd2e9SMike Galbraith 
470091bd2e9SMike Galbraith 		poll(&stdin_poll, 1, -1);
471091bd2e9SMike Galbraith 		c = getc(stdin);
472091bd2e9SMike Galbraith 
473091bd2e9SMike Galbraith 		tcsetattr(0, TCSAFLUSH, &save);
4741758af10SArnaldo Carvalho de Melo 		if (!perf_top__key_mapped(top, c))
475091bd2e9SMike Galbraith 			return;
476091bd2e9SMike Galbraith 	}
477091bd2e9SMike Galbraith 
478923c42c1SMike Galbraith 	switch (c) {
479923c42c1SMike Galbraith 		case 'd':
4801758af10SArnaldo Carvalho de Melo 			prompt_integer(&top->delay_secs, "Enter display delay");
4811758af10SArnaldo Carvalho de Melo 			if (top->delay_secs < 1)
4821758af10SArnaldo Carvalho de Melo 				top->delay_secs = 1;
483923c42c1SMike Galbraith 			break;
484923c42c1SMike Galbraith 		case 'e':
4851758af10SArnaldo Carvalho de Melo 			prompt_integer(&top->print_entries, "Enter display entries (lines)");
4861758af10SArnaldo Carvalho de Melo 			if (top->print_entries == 0) {
4871758af10SArnaldo Carvalho de Melo 				struct sigaction act = {
4881758af10SArnaldo Carvalho de Melo 					.sa_sigaction = perf_top__sig_winch,
4891758af10SArnaldo Carvalho de Melo 					.sa_flags     = SA_SIGINFO,
4901758af10SArnaldo Carvalho de Melo 				};
4911758af10SArnaldo Carvalho de Melo 				perf_top__sig_winch(SIGWINCH, NULL, top);
4921758af10SArnaldo Carvalho de Melo 				sigaction(SIGWINCH, &act, NULL);
493509605dbSStephane Eranian 			} else {
494509605dbSStephane Eranian 				perf_top__sig_winch(SIGWINCH, NULL, top);
4953b6ed988SArnaldo Carvalho de Melo 				signal(SIGWINCH, SIG_DFL);
496509605dbSStephane Eranian 			}
497923c42c1SMike Galbraith 			break;
498923c42c1SMike Galbraith 		case 'E':
4991758af10SArnaldo Carvalho de Melo 			if (top->evlist->nr_entries > 1) {
500ce2d17caSAkihiro Nagai 				/* Select 0 as the default event: */
501ce2d17caSAkihiro Nagai 				int counter = 0;
502ce2d17caSAkihiro Nagai 
503923c42c1SMike Galbraith 				fprintf(stderr, "\nAvailable events:");
50469aad6f1SArnaldo Carvalho de Melo 
5051758af10SArnaldo Carvalho de Melo 				list_for_each_entry(top->sym_evsel, &top->evlist->entries, node)
5067289f83cSArnaldo Carvalho de Melo 					fprintf(stderr, "\n\t%d %s", top->sym_evsel->idx, perf_evsel__name(top->sym_evsel));
507923c42c1SMike Galbraith 
508ec52d976SArnaldo Carvalho de Melo 				prompt_integer(&counter, "Enter details event counter");
509923c42c1SMike Galbraith 
5101758af10SArnaldo Carvalho de Melo 				if (counter >= top->evlist->nr_entries) {
5111758af10SArnaldo Carvalho de Melo 					top->sym_evsel = list_entry(top->evlist->entries.next, struct perf_evsel, node);
5127289f83cSArnaldo Carvalho de Melo 					fprintf(stderr, "Sorry, no such event, using %s.\n", perf_evsel__name(top->sym_evsel));
513923c42c1SMike Galbraith 					sleep(1);
51469aad6f1SArnaldo Carvalho de Melo 					break;
515923c42c1SMike Galbraith 				}
5161758af10SArnaldo Carvalho de Melo 				list_for_each_entry(top->sym_evsel, &top->evlist->entries, node)
5171758af10SArnaldo Carvalho de Melo 					if (top->sym_evsel->idx == counter)
51869aad6f1SArnaldo Carvalho de Melo 						break;
519ec52d976SArnaldo Carvalho de Melo 			} else
5201758af10SArnaldo Carvalho de Melo 				top->sym_evsel = list_entry(top->evlist->entries.next, struct perf_evsel, node);
521923c42c1SMike Galbraith 			break;
522923c42c1SMike Galbraith 		case 'f':
5231758af10SArnaldo Carvalho de Melo 			prompt_integer(&top->count_filter, "Enter display event count filter");
524923c42c1SMike Galbraith 			break;
525923c42c1SMike Galbraith 		case 'F':
5261758af10SArnaldo Carvalho de Melo 			prompt_percent(&top->sym_pcnt_filter,
5271758af10SArnaldo Carvalho de Melo 				       "Enter details display event filter (percent)");
528923c42c1SMike Galbraith 			break;
5298ffcda17SArnaldo Carvalho de Melo 		case 'K':
5301758af10SArnaldo Carvalho de Melo 			top->hide_kernel_symbols = !top->hide_kernel_symbols;
5318ffcda17SArnaldo Carvalho de Melo 			break;
532923c42c1SMike Galbraith 		case 'q':
533923c42c1SMike Galbraith 		case 'Q':
534923c42c1SMike Galbraith 			printf("exiting.\n");
5351758af10SArnaldo Carvalho de Melo 			if (top->dump_symtab)
5361758af10SArnaldo Carvalho de Melo 				perf_session__fprintf_dsos(top->session, stderr);
537923c42c1SMike Galbraith 			exit(0);
538923c42c1SMike Galbraith 		case 's':
5391758af10SArnaldo Carvalho de Melo 			perf_top__prompt_symbol(top, "Enter details symbol");
540923c42c1SMike Galbraith 			break;
541923c42c1SMike Galbraith 		case 'S':
5421758af10SArnaldo Carvalho de Melo 			if (!top->sym_filter_entry)
543923c42c1SMike Galbraith 				break;
544923c42c1SMike Galbraith 			else {
5451758af10SArnaldo Carvalho de Melo 				struct hist_entry *syme = top->sym_filter_entry;
546923c42c1SMike Galbraith 
5471758af10SArnaldo Carvalho de Melo 				top->sym_filter_entry = NULL;
548923c42c1SMike Galbraith 				__zero_source_counters(syme);
549923c42c1SMike Galbraith 			}
550923c42c1SMike Galbraith 			break;
5518ffcda17SArnaldo Carvalho de Melo 		case 'U':
5521758af10SArnaldo Carvalho de Melo 			top->hide_user_symbols = !top->hide_user_symbols;
5538ffcda17SArnaldo Carvalho de Melo 			break;
554923c42c1SMike Galbraith 		case 'z':
5551758af10SArnaldo Carvalho de Melo 			top->zero = !top->zero;
556923c42c1SMike Galbraith 			break;
55783a0944fSIngo Molnar 		default:
55883a0944fSIngo Molnar 			break;
559923c42c1SMike Galbraith 	}
560923c42c1SMike Galbraith }
561923c42c1SMike Galbraith 
562ab81f3fdSArnaldo Carvalho de Melo static void perf_top__sort_new_samples(void *arg)
563ab81f3fdSArnaldo Carvalho de Melo {
564ab81f3fdSArnaldo Carvalho de Melo 	struct perf_top *t = arg;
565ab81f3fdSArnaldo Carvalho de Melo 	perf_top__reset_sample_counters(t);
566ab81f3fdSArnaldo Carvalho de Melo 
567ab81f3fdSArnaldo Carvalho de Melo 	if (t->evlist->selected != NULL)
568ab81f3fdSArnaldo Carvalho de Melo 		t->sym_evsel = t->evlist->selected;
569ab81f3fdSArnaldo Carvalho de Melo 
570ab81f3fdSArnaldo Carvalho de Melo 	hists__collapse_resort_threaded(&t->sym_evsel->hists);
571ab81f3fdSArnaldo Carvalho de Melo 	hists__output_resort_threaded(&t->sym_evsel->hists);
572b079d4e9SArnaldo Carvalho de Melo 	hists__decay_entries_threaded(&t->sym_evsel->hists,
5731758af10SArnaldo Carvalho de Melo 				      t->hide_user_symbols,
5741758af10SArnaldo Carvalho de Melo 				      t->hide_kernel_symbols);
575ab81f3fdSArnaldo Carvalho de Melo }
576ab81f3fdSArnaldo Carvalho de Melo 
5771758af10SArnaldo Carvalho de Melo static void *display_thread_tui(void *arg)
578c0443df1SArnaldo Carvalho de Melo {
5790d37aa34SArnaldo Carvalho de Melo 	struct perf_evsel *pos;
5801758af10SArnaldo Carvalho de Melo 	struct perf_top *top = arg;
581ab81f3fdSArnaldo Carvalho de Melo 	const char *help = "For a higher level overview, try: perf top --sort comm,dso";
582ab81f3fdSArnaldo Carvalho de Melo 
5831758af10SArnaldo Carvalho de Melo 	perf_top__sort_new_samples(top);
5840d37aa34SArnaldo Carvalho de Melo 
5850d37aa34SArnaldo Carvalho de Melo 	/*
5860d37aa34SArnaldo Carvalho de Melo 	 * Initialize the uid_filter_str, in the future the TUI will allow
5870d37aa34SArnaldo Carvalho de Melo 	 * Zooming in/out UIDs. For now juse use whatever the user passed
5880d37aa34SArnaldo Carvalho de Melo 	 * via --uid.
5890d37aa34SArnaldo Carvalho de Melo 	 */
5900d37aa34SArnaldo Carvalho de Melo 	list_for_each_entry(pos, &top->evlist->entries, node)
591fe9d18a7SNamhyung Kim 		pos->hists.uid_filter_str = top->target.uid_str;
5920d37aa34SArnaldo Carvalho de Melo 
5931758af10SArnaldo Carvalho de Melo 	perf_evlist__tui_browse_hists(top->evlist, help,
594ab81f3fdSArnaldo Carvalho de Melo 				      perf_top__sort_new_samples,
5951758af10SArnaldo Carvalho de Melo 				      top, top->delay_secs);
596ab81f3fdSArnaldo Carvalho de Melo 
597c0443df1SArnaldo Carvalho de Melo 	exit_browser(0);
598c0443df1SArnaldo Carvalho de Melo 	exit(0);
599c0443df1SArnaldo Carvalho de Melo 	return NULL;
600c0443df1SArnaldo Carvalho de Melo }
601c0443df1SArnaldo Carvalho de Melo 
6021758af10SArnaldo Carvalho de Melo static void *display_thread(void *arg)
60386470930SIngo Molnar {
60486470930SIngo Molnar 	struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
605923c42c1SMike Galbraith 	struct termios tc, save;
6061758af10SArnaldo Carvalho de Melo 	struct perf_top *top = arg;
607923c42c1SMike Galbraith 	int delay_msecs, c;
60886470930SIngo Molnar 
609923c42c1SMike Galbraith 	tcgetattr(0, &save);
610923c42c1SMike Galbraith 	tc = save;
611923c42c1SMike Galbraith 	tc.c_lflag &= ~(ICANON | ECHO);
612923c42c1SMike Galbraith 	tc.c_cc[VMIN] = 0;
613923c42c1SMike Galbraith 	tc.c_cc[VTIME] = 0;
614091bd2e9SMike Galbraith 
6153af6e338SArnaldo Carvalho de Melo 	pthread__unblock_sigwinch();
616923c42c1SMike Galbraith repeat:
6171758af10SArnaldo Carvalho de Melo 	delay_msecs = top->delay_secs * 1000;
618923c42c1SMike Galbraith 	tcsetattr(0, TCSANOW, &tc);
619923c42c1SMike Galbraith 	/* trash return*/
620923c42c1SMike Galbraith 	getc(stdin);
62186470930SIngo Molnar 
6223af6e338SArnaldo Carvalho de Melo 	while (1) {
6231758af10SArnaldo Carvalho de Melo 		perf_top__print_sym_table(top);
6243af6e338SArnaldo Carvalho de Melo 		/*
6253af6e338SArnaldo Carvalho de Melo 		 * Either timeout expired or we got an EINTR due to SIGWINCH,
6263af6e338SArnaldo Carvalho de Melo 		 * refresh screen in both cases.
6273af6e338SArnaldo Carvalho de Melo 		 */
6283af6e338SArnaldo Carvalho de Melo 		switch (poll(&stdin_poll, 1, delay_msecs)) {
6293af6e338SArnaldo Carvalho de Melo 		case 0:
6303af6e338SArnaldo Carvalho de Melo 			continue;
6313af6e338SArnaldo Carvalho de Melo 		case -1:
6323af6e338SArnaldo Carvalho de Melo 			if (errno == EINTR)
6333af6e338SArnaldo Carvalho de Melo 				continue;
6343af6e338SArnaldo Carvalho de Melo 			/* Fall trhu */
6353af6e338SArnaldo Carvalho de Melo 		default:
6363af6e338SArnaldo Carvalho de Melo 			goto process_hotkey;
6373af6e338SArnaldo Carvalho de Melo 		}
6383af6e338SArnaldo Carvalho de Melo 	}
6393af6e338SArnaldo Carvalho de Melo process_hotkey:
640923c42c1SMike Galbraith 	c = getc(stdin);
641923c42c1SMike Galbraith 	tcsetattr(0, TCSAFLUSH, &save);
642923c42c1SMike Galbraith 
6431758af10SArnaldo Carvalho de Melo 	perf_top__handle_keypress(top, c);
644923c42c1SMike Galbraith 	goto repeat;
64586470930SIngo Molnar 
64686470930SIngo Molnar 	return NULL;
64786470930SIngo Molnar }
64886470930SIngo Molnar 
6492ab52083SAnton Blanchard /* Tag samples to be skipped. */
650f37a291cSIngo Molnar static const char *skip_symbols[] = {
6514bea8b5cSArnaldo Carvalho de Melo 	"intel_idle",
6522ab52083SAnton Blanchard 	"default_idle",
653b0e8572fSArnaldo Carvalho de Melo 	"native_safe_halt",
6542ab52083SAnton Blanchard 	"cpu_idle",
6552ab52083SAnton Blanchard 	"enter_idle",
6562ab52083SAnton Blanchard 	"exit_idle",
6572ab52083SAnton Blanchard 	"mwait_idle",
65859b90056SArnaldo Carvalho de Melo 	"mwait_idle_with_hints",
6598357275bSArnaldo Carvalho de Melo 	"poll_idle",
6603a3393efSAnton Blanchard 	"ppc64_runlatch_off",
6613a3393efSAnton Blanchard 	"pseries_dedicated_idle_sleep",
6622ab52083SAnton Blanchard 	NULL
6632ab52083SAnton Blanchard };
6642ab52083SAnton Blanchard 
665ab81f3fdSArnaldo Carvalho de Melo static int symbol_filter(struct map *map __used, struct symbol *sym)
66686470930SIngo Molnar {
66786470930SIngo Molnar 	const char *name = sym->name;
6682ab52083SAnton Blanchard 	int i;
66986470930SIngo Molnar 
6703a3393efSAnton Blanchard 	/*
6713a3393efSAnton Blanchard 	 * ppc64 uses function descriptors and appends a '.' to the
6723a3393efSAnton Blanchard 	 * start of every instruction address. Remove it.
6733a3393efSAnton Blanchard 	 */
6743a3393efSAnton Blanchard 	if (name[0] == '.')
6753a3393efSAnton Blanchard 		name++;
6763a3393efSAnton Blanchard 
67786470930SIngo Molnar 	if (!strcmp(name, "_text") ||
67886470930SIngo Molnar 	    !strcmp(name, "_etext") ||
67986470930SIngo Molnar 	    !strcmp(name, "_sinittext") ||
68086470930SIngo Molnar 	    !strncmp("init_module", name, 11) ||
68186470930SIngo Molnar 	    !strncmp("cleanup_module", name, 14) ||
68286470930SIngo Molnar 	    strstr(name, "_text_start") ||
68386470930SIngo Molnar 	    strstr(name, "_text_end"))
68486470930SIngo Molnar 		return 1;
68586470930SIngo Molnar 
6862ab52083SAnton Blanchard 	for (i = 0; skip_symbols[i]; i++) {
6872ab52083SAnton Blanchard 		if (!strcmp(skip_symbols[i], name)) {
688171b3be9SArnaldo Carvalho de Melo 			sym->ignore = true;
6892ab52083SAnton Blanchard 			break;
6902ab52083SAnton Blanchard 		}
6912ab52083SAnton Blanchard 	}
69286470930SIngo Molnar 
69386470930SIngo Molnar 	return 0;
69486470930SIngo Molnar }
69586470930SIngo Molnar 
6961758af10SArnaldo Carvalho de Melo static void perf_event__process_sample(struct perf_tool *tool,
6971758af10SArnaldo Carvalho de Melo 				       const union perf_event *event,
6987b27509fSArnaldo Carvalho de Melo 				       struct perf_evsel *evsel,
6998d50e5b4SArnaldo Carvalho de Melo 				       struct perf_sample *sample,
700743eb868SArnaldo Carvalho de Melo 				       struct machine *machine)
70186470930SIngo Molnar {
7021758af10SArnaldo Carvalho de Melo 	struct perf_top *top = container_of(tool, struct perf_top, tool);
70319d4ac3cSArnaldo Carvalho de Melo 	struct symbol *parent = NULL;
7048115d60cSArnaldo Carvalho de Melo 	u64 ip = event->ip.ip;
7051ed091c4SArnaldo Carvalho de Melo 	struct addr_location al;
70619d4ac3cSArnaldo Carvalho de Melo 	int err;
7075b2bb75aSArnaldo Carvalho de Melo 
70823346f21SArnaldo Carvalho de Melo 	if (!machine && perf_guest) {
709a1645ce1SZhang, Yanmin 		pr_err("Can't find guest [%d]'s kernel information\n",
7108115d60cSArnaldo Carvalho de Melo 			event->ip.pid);
711a1645ce1SZhang, Yanmin 		return;
712a1645ce1SZhang, Yanmin 	}
713a1645ce1SZhang, Yanmin 
7140c095715SJoerg Roedel 	if (!machine) {
7150c095715SJoerg Roedel 		pr_err("%u unprocessable samples recorded.",
7160c095715SJoerg Roedel 		       top->session->hists.stats.nr_unprocessable_samples++);
7170c095715SJoerg Roedel 		return;
7180c095715SJoerg Roedel 	}
7190c095715SJoerg Roedel 
7208115d60cSArnaldo Carvalho de Melo 	if (event->header.misc & PERF_RECORD_MISC_EXACT_IP)
7211758af10SArnaldo Carvalho de Melo 		top->exact_samples++;
7221676b8a0SPeter Zijlstra 
723743eb868SArnaldo Carvalho de Melo 	if (perf_event__preprocess_sample(event, machine, &al, sample,
72441a37e20SArnaldo Carvalho de Melo 					  symbol_filter) < 0 ||
72572b8fa17SArnaldo Carvalho de Melo 	    al.filtered)
7261ed091c4SArnaldo Carvalho de Melo 		return;
72786470930SIngo Molnar 
7281758af10SArnaldo Carvalho de Melo 	if (!top->kptr_restrict_warned &&
7295f6f5580SArnaldo Carvalho de Melo 	    symbol_conf.kptr_restrict &&
7305f6f5580SArnaldo Carvalho de Melo 	    al.cpumode == PERF_RECORD_MISC_KERNEL) {
7315f6f5580SArnaldo Carvalho de Melo 		ui__warning(
7325f6f5580SArnaldo Carvalho de Melo "Kernel address maps (/proc/{kallsyms,modules}) are restricted.\n\n"
7335f6f5580SArnaldo Carvalho de Melo "Check /proc/sys/kernel/kptr_restrict.\n\n"
7345f6f5580SArnaldo Carvalho de Melo "Kernel%s samples will not be resolved.\n",
7355f6f5580SArnaldo Carvalho de Melo 			  !RB_EMPTY_ROOT(&al.map->dso->symbols[MAP__FUNCTION]) ?
7365f6f5580SArnaldo Carvalho de Melo 			  " modules" : "");
7375f6f5580SArnaldo Carvalho de Melo 		if (use_browser <= 0)
7385f6f5580SArnaldo Carvalho de Melo 			sleep(5);
7391758af10SArnaldo Carvalho de Melo 		top->kptr_restrict_warned = true;
7405f6f5580SArnaldo Carvalho de Melo 	}
7415f6f5580SArnaldo Carvalho de Melo 
74272b8fa17SArnaldo Carvalho de Melo 	if (al.sym == NULL) {
743e4a338d0SArnaldo Carvalho de Melo 		const char *msg = "Kernel samples will not be resolved.\n";
74472b8fa17SArnaldo Carvalho de Melo 		/*
74572b8fa17SArnaldo Carvalho de Melo 		 * As we do lazy loading of symtabs we only will know if the
74672b8fa17SArnaldo Carvalho de Melo 		 * specified vmlinux file is invalid when we actually have a
74772b8fa17SArnaldo Carvalho de Melo 		 * hit in kernel space and then try to load it. So if we get
74872b8fa17SArnaldo Carvalho de Melo 		 * here and there are _no_ symbols in the DSO backing the
74972b8fa17SArnaldo Carvalho de Melo 		 * kernel map, bail out.
75072b8fa17SArnaldo Carvalho de Melo 		 *
75172b8fa17SArnaldo Carvalho de Melo 		 * We may never get here, for instance, if we use -K/
75272b8fa17SArnaldo Carvalho de Melo 		 * --hide-kernel-symbols, even if the user specifies an
75372b8fa17SArnaldo Carvalho de Melo 		 * invalid --vmlinux ;-)
75472b8fa17SArnaldo Carvalho de Melo 		 */
7551758af10SArnaldo Carvalho de Melo 		if (!top->kptr_restrict_warned && !top->vmlinux_warned &&
756e4a338d0SArnaldo Carvalho de Melo 		    al.map == machine->vmlinux_maps[MAP__FUNCTION] &&
75772b8fa17SArnaldo Carvalho de Melo 		    RB_EMPTY_ROOT(&al.map->dso->symbols[MAP__FUNCTION])) {
758e4a338d0SArnaldo Carvalho de Melo 			if (symbol_conf.vmlinux_name) {
759e4a338d0SArnaldo Carvalho de Melo 				ui__warning("The %s file can't be used.\n%s",
760e4a338d0SArnaldo Carvalho de Melo 					    symbol_conf.vmlinux_name, msg);
761e4a338d0SArnaldo Carvalho de Melo 			} else {
762e4a338d0SArnaldo Carvalho de Melo 				ui__warning("A vmlinux file was not found.\n%s",
763e4a338d0SArnaldo Carvalho de Melo 					    msg);
764e4a338d0SArnaldo Carvalho de Melo 			}
765e4a338d0SArnaldo Carvalho de Melo 
766e4a338d0SArnaldo Carvalho de Melo 			if (use_browser <= 0)
767e4a338d0SArnaldo Carvalho de Melo 				sleep(5);
7681758af10SArnaldo Carvalho de Melo 			top->vmlinux_warned = true;
76972b8fa17SArnaldo Carvalho de Melo 		}
77072b8fa17SArnaldo Carvalho de Melo 	}
77172b8fa17SArnaldo Carvalho de Melo 
772ab81f3fdSArnaldo Carvalho de Melo 	if (al.sym == NULL || !al.sym->ignore) {
773ab81f3fdSArnaldo Carvalho de Melo 		struct hist_entry *he;
77470db7533SArnaldo Carvalho de Melo 
77519d4ac3cSArnaldo Carvalho de Melo 		if ((sort__has_parent || symbol_conf.use_callchain) &&
77619d4ac3cSArnaldo Carvalho de Melo 		    sample->callchain) {
777*a9c34a9fSJiri Olsa 			err = machine__resolve_callchain(machine, al.thread,
77819d4ac3cSArnaldo Carvalho de Melo 							 sample->callchain, &parent);
77919d4ac3cSArnaldo Carvalho de Melo 			if (err)
78019d4ac3cSArnaldo Carvalho de Melo 				return;
78119d4ac3cSArnaldo Carvalho de Melo 		}
78219d4ac3cSArnaldo Carvalho de Melo 
783743eb868SArnaldo Carvalho de Melo 		he = perf_evsel__add_hist_entry(evsel, &al, sample);
784ab81f3fdSArnaldo Carvalho de Melo 		if (he == NULL) {
785ab81f3fdSArnaldo Carvalho de Melo 			pr_err("Problem incrementing symbol period, skipping event\n");
786ab81f3fdSArnaldo Carvalho de Melo 			return;
7875807806aSArnaldo Carvalho de Melo 		}
788ab81f3fdSArnaldo Carvalho de Melo 
78919d4ac3cSArnaldo Carvalho de Melo 		if (symbol_conf.use_callchain) {
79047260645SNamhyung Kim 			err = callchain_append(he->callchain, &callchain_cursor,
79119d4ac3cSArnaldo Carvalho de Melo 					       sample->period);
79219d4ac3cSArnaldo Carvalho de Melo 			if (err)
79319d4ac3cSArnaldo Carvalho de Melo 				return;
79419d4ac3cSArnaldo Carvalho de Melo 		}
79519d4ac3cSArnaldo Carvalho de Melo 
7961758af10SArnaldo Carvalho de Melo 		if (top->sort_has_symbols)
7971758af10SArnaldo Carvalho de Melo 			perf_top__record_precise_ip(top, he, evsel->idx, ip);
7985807806aSArnaldo Carvalho de Melo 	}
799ab81f3fdSArnaldo Carvalho de Melo 
800ab81f3fdSArnaldo Carvalho de Melo 	return;
80186470930SIngo Molnar }
80286470930SIngo Molnar 
8031758af10SArnaldo Carvalho de Melo static void perf_top__mmap_read_idx(struct perf_top *top, int idx)
80486470930SIngo Molnar {
8058d50e5b4SArnaldo Carvalho de Melo 	struct perf_sample sample;
8067b27509fSArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
8071758af10SArnaldo Carvalho de Melo 	struct perf_session *session = top->session;
8088115d60cSArnaldo Carvalho de Melo 	union perf_event *event;
809743eb868SArnaldo Carvalho de Melo 	struct machine *machine;
810743eb868SArnaldo Carvalho de Melo 	u8 origin;
8115538becaSFrederic Weisbecker 	int ret;
81286470930SIngo Molnar 
8131758af10SArnaldo Carvalho de Melo 	while ((event = perf_evlist__mmap_read(top->evlist, idx)) != NULL) {
8141758af10SArnaldo Carvalho de Melo 		ret = perf_session__parse_sample(session, event, &sample);
8155538becaSFrederic Weisbecker 		if (ret) {
8165538becaSFrederic Weisbecker 			pr_err("Can't parse sample, err = %d\n", ret);
8175538becaSFrederic Weisbecker 			continue;
8185538becaSFrederic Weisbecker 		}
81904391debSArnaldo Carvalho de Melo 
8201758af10SArnaldo Carvalho de Melo 		evsel = perf_evlist__id2evsel(session->evlist, sample.id);
8217b27509fSArnaldo Carvalho de Melo 		assert(evsel != NULL);
8227b27509fSArnaldo Carvalho de Melo 
823743eb868SArnaldo Carvalho de Melo 		origin = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
824743eb868SArnaldo Carvalho de Melo 
8255b2bb75aSArnaldo Carvalho de Melo 		if (event->header.type == PERF_RECORD_SAMPLE)
8261758af10SArnaldo Carvalho de Melo 			++top->samples;
827743eb868SArnaldo Carvalho de Melo 
828743eb868SArnaldo Carvalho de Melo 		switch (origin) {
829743eb868SArnaldo Carvalho de Melo 		case PERF_RECORD_MISC_USER:
8301758af10SArnaldo Carvalho de Melo 			++top->us_samples;
8311758af10SArnaldo Carvalho de Melo 			if (top->hide_user_symbols)
832743eb868SArnaldo Carvalho de Melo 				continue;
8331758af10SArnaldo Carvalho de Melo 			machine = perf_session__find_host_machine(session);
834743eb868SArnaldo Carvalho de Melo 			break;
835743eb868SArnaldo Carvalho de Melo 		case PERF_RECORD_MISC_KERNEL:
8361758af10SArnaldo Carvalho de Melo 			++top->kernel_samples;
8371758af10SArnaldo Carvalho de Melo 			if (top->hide_kernel_symbols)
838743eb868SArnaldo Carvalho de Melo 				continue;
8391758af10SArnaldo Carvalho de Melo 			machine = perf_session__find_host_machine(session);
840743eb868SArnaldo Carvalho de Melo 			break;
841743eb868SArnaldo Carvalho de Melo 		case PERF_RECORD_MISC_GUEST_KERNEL:
8421758af10SArnaldo Carvalho de Melo 			++top->guest_kernel_samples;
8431758af10SArnaldo Carvalho de Melo 			machine = perf_session__find_machine(session, event->ip.pid);
844743eb868SArnaldo Carvalho de Melo 			break;
845743eb868SArnaldo Carvalho de Melo 		case PERF_RECORD_MISC_GUEST_USER:
8461758af10SArnaldo Carvalho de Melo 			++top->guest_us_samples;
847743eb868SArnaldo Carvalho de Melo 			/*
848743eb868SArnaldo Carvalho de Melo 			 * TODO: we don't process guest user from host side
849743eb868SArnaldo Carvalho de Melo 			 * except simple counting.
850743eb868SArnaldo Carvalho de Melo 			 */
851743eb868SArnaldo Carvalho de Melo 			/* Fall thru */
852743eb868SArnaldo Carvalho de Melo 		default:
853743eb868SArnaldo Carvalho de Melo 			continue;
854743eb868SArnaldo Carvalho de Melo 		}
855743eb868SArnaldo Carvalho de Melo 
856743eb868SArnaldo Carvalho de Melo 
8571758af10SArnaldo Carvalho de Melo 		if (event->header.type == PERF_RECORD_SAMPLE) {
8581758af10SArnaldo Carvalho de Melo 			perf_event__process_sample(&top->tool, event, evsel,
8591758af10SArnaldo Carvalho de Melo 						   &sample, machine);
8601758af10SArnaldo Carvalho de Melo 		} else if (event->header.type < PERF_RECORD_MAX) {
8617b27509fSArnaldo Carvalho de Melo 			hists__inc_nr_events(&evsel->hists, event->header.type);
8621758af10SArnaldo Carvalho de Melo 			perf_event__process(&top->tool, event, &sample, machine);
8637b27509fSArnaldo Carvalho de Melo 		} else
8641758af10SArnaldo Carvalho de Melo 			++session->hists.stats.nr_unknown_events;
86586470930SIngo Molnar 	}
86686470930SIngo Molnar }
86786470930SIngo Molnar 
8681758af10SArnaldo Carvalho de Melo static void perf_top__mmap_read(struct perf_top *top)
8692f01190aSFrederic Weisbecker {
87070db7533SArnaldo Carvalho de Melo 	int i;
8712f01190aSFrederic Weisbecker 
8721758af10SArnaldo Carvalho de Melo 	for (i = 0; i < top->evlist->nr_mmaps; i++)
8731758af10SArnaldo Carvalho de Melo 		perf_top__mmap_read_idx(top, i);
8742f01190aSFrederic Weisbecker }
8752f01190aSFrederic Weisbecker 
8761758af10SArnaldo Carvalho de Melo static void perf_top__start_counters(struct perf_top *top)
87772cb7013SArnaldo Carvalho de Melo {
878727ab04eSArnaldo Carvalho de Melo 	struct perf_evsel *counter, *first;
8791758af10SArnaldo Carvalho de Melo 	struct perf_evlist *evlist = top->evlist;
880727ab04eSArnaldo Carvalho de Melo 
881727ab04eSArnaldo Carvalho de Melo 	first = list_entry(evlist->entries.next, struct perf_evsel, node);
88272cb7013SArnaldo Carvalho de Melo 
88372cb7013SArnaldo Carvalho de Melo 	list_for_each_entry(counter, &evlist->entries, node) {
88472cb7013SArnaldo Carvalho de Melo 		struct perf_event_attr *attr = &counter->attr;
885727ab04eSArnaldo Carvalho de Melo 		struct xyarray *group_fd = NULL;
886727ab04eSArnaldo Carvalho de Melo 
8871758af10SArnaldo Carvalho de Melo 		if (top->group && counter != first)
888727ab04eSArnaldo Carvalho de Melo 			group_fd = first->fd;
88972cb7013SArnaldo Carvalho de Melo 
89072cb7013SArnaldo Carvalho de Melo 		attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
89172cb7013SArnaldo Carvalho de Melo 
8921758af10SArnaldo Carvalho de Melo 		if (top->freq) {
89372cb7013SArnaldo Carvalho de Melo 			attr->sample_type |= PERF_SAMPLE_PERIOD;
89472cb7013SArnaldo Carvalho de Melo 			attr->freq	  = 1;
8951758af10SArnaldo Carvalho de Melo 			attr->sample_freq = top->freq;
89672cb7013SArnaldo Carvalho de Melo 		}
89772cb7013SArnaldo Carvalho de Melo 
89870db7533SArnaldo Carvalho de Melo 		if (evlist->nr_entries > 1) {
89970db7533SArnaldo Carvalho de Melo 			attr->sample_type |= PERF_SAMPLE_ID;
90070db7533SArnaldo Carvalho de Melo 			attr->read_format |= PERF_FORMAT_ID;
90170db7533SArnaldo Carvalho de Melo 		}
90270db7533SArnaldo Carvalho de Melo 
903e40ee742SNamhyung Kim 		if (perf_target__has_cpu(&top->target))
904e40ee742SNamhyung Kim 			attr->sample_type |= PERF_SAMPLE_CPU;
905e40ee742SNamhyung Kim 
90619d4ac3cSArnaldo Carvalho de Melo 		if (symbol_conf.use_callchain)
90719d4ac3cSArnaldo Carvalho de Melo 			attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
90819d4ac3cSArnaldo Carvalho de Melo 
90972cb7013SArnaldo Carvalho de Melo 		attr->mmap = 1;
910ab81f3fdSArnaldo Carvalho de Melo 		attr->comm = 1;
9111758af10SArnaldo Carvalho de Melo 		attr->inherit = top->inherit;
9120c978128SArnaldo Carvalho de Melo fallback_missing_features:
9130c978128SArnaldo Carvalho de Melo 		if (top->exclude_guest_missing)
9140c978128SArnaldo Carvalho de Melo 			attr->exclude_guest = attr->exclude_host = 0;
9157b27509fSArnaldo Carvalho de Melo retry_sample_id:
916808e1226SArnaldo Carvalho de Melo 		attr->sample_id_all = top->sample_id_all_missing ? 0 : 1;
91772cb7013SArnaldo Carvalho de Melo try_again:
9181758af10SArnaldo Carvalho de Melo 		if (perf_evsel__open(counter, top->evlist->cpus,
9191758af10SArnaldo Carvalho de Melo 				     top->evlist->threads, top->group,
920727ab04eSArnaldo Carvalho de Melo 				     group_fd) < 0) {
92172cb7013SArnaldo Carvalho de Melo 			int err = errno;
92272cb7013SArnaldo Carvalho de Melo 
923c286c419SArnaldo Carvalho de Melo 			if (err == EPERM || err == EACCES) {
924b8631e6eSArnaldo Carvalho de Melo 				ui__error_paranoid();
925c286c419SArnaldo Carvalho de Melo 				goto out_err;
9260c978128SArnaldo Carvalho de Melo 			} else if (err == EINVAL) {
9270c978128SArnaldo Carvalho de Melo 				if (!top->exclude_guest_missing &&
9280c978128SArnaldo Carvalho de Melo 				    (attr->exclude_guest || attr->exclude_host)) {
9290c978128SArnaldo Carvalho de Melo 					pr_debug("Old kernel, cannot exclude "
9300c978128SArnaldo Carvalho de Melo 						 "guest or host samples.\n");
9310c978128SArnaldo Carvalho de Melo 					top->exclude_guest_missing = true;
9320c978128SArnaldo Carvalho de Melo 					goto fallback_missing_features;
933808e1226SArnaldo Carvalho de Melo 				} else if (!top->sample_id_all_missing) {
9347b27509fSArnaldo Carvalho de Melo 					/*
9357b27509fSArnaldo Carvalho de Melo 					 * Old kernel, no attr->sample_id_type_all field
9367b27509fSArnaldo Carvalho de Melo 					 */
937808e1226SArnaldo Carvalho de Melo 					top->sample_id_all_missing = true;
9387b27509fSArnaldo Carvalho de Melo 					goto retry_sample_id;
939c286c419SArnaldo Carvalho de Melo 				}
9400c978128SArnaldo Carvalho de Melo 			}
94172cb7013SArnaldo Carvalho de Melo 			/*
94272cb7013SArnaldo Carvalho de Melo 			 * If it's cycles then fall back to hrtimer
94372cb7013SArnaldo Carvalho de Melo 			 * based cpu-clock-tick sw counter, which
94472cb7013SArnaldo Carvalho de Melo 			 * is always available even if no PMU support:
94572cb7013SArnaldo Carvalho de Melo 			 */
94672cb7013SArnaldo Carvalho de Melo 			if (attr->type == PERF_TYPE_HARDWARE &&
94772cb7013SArnaldo Carvalho de Melo 			    attr->config == PERF_COUNT_HW_CPU_CYCLES) {
94872cb7013SArnaldo Carvalho de Melo 				if (verbose)
949c286c419SArnaldo Carvalho de Melo 					ui__warning("Cycles event not supported,\n"
950c286c419SArnaldo Carvalho de Melo 						    "trying to fall back to cpu-clock-ticks\n");
95172cb7013SArnaldo Carvalho de Melo 
95272cb7013SArnaldo Carvalho de Melo 				attr->type = PERF_TYPE_SOFTWARE;
95372cb7013SArnaldo Carvalho de Melo 				attr->config = PERF_COUNT_SW_CPU_CLOCK;
95440491eaaSDavid Ahern 				if (counter->name) {
95540491eaaSDavid Ahern 					free(counter->name);
956895d9766SArnaldo Carvalho de Melo 					counter->name = NULL;
95740491eaaSDavid Ahern 				}
95872cb7013SArnaldo Carvalho de Melo 				goto try_again;
95972cb7013SArnaldo Carvalho de Melo 			}
960c286c419SArnaldo Carvalho de Melo 
961ca6a4258SDavid Ahern 			if (err == ENOENT) {
9623780f488SNamhyung Kim 				ui__error("The %s event is not supported.\n",
9637289f83cSArnaldo Carvalho de Melo 					  perf_evsel__name(counter));
964ca6a4258SDavid Ahern 				goto out_err;
965cdce4459SNamhyung Kim 			} else if (err == EMFILE) {
9663780f488SNamhyung Kim 				ui__error("Too many events are opened.\n"
967cdce4459SNamhyung Kim 					    "Try again after reducing the number of events\n");
968cdce4459SNamhyung Kim 				goto out_err;
969ca6a4258SDavid Ahern 			}
970ca6a4258SDavid Ahern 
9713780f488SNamhyung Kim 			ui__error("The sys_perf_event_open() syscall "
972c286c419SArnaldo Carvalho de Melo 				    "returned with %d (%s).  /bin/dmesg "
973c286c419SArnaldo Carvalho de Melo 				    "may provide additional information.\n"
974c286c419SArnaldo Carvalho de Melo 				    "No CONFIG_PERF_EVENTS=y kernel support "
975c286c419SArnaldo Carvalho de Melo 				    "configured?\n", err, strerror(err));
976c286c419SArnaldo Carvalho de Melo 			goto out_err;
97772cb7013SArnaldo Carvalho de Melo 		}
97872cb7013SArnaldo Carvalho de Melo 	}
97970db7533SArnaldo Carvalho de Melo 
9801758af10SArnaldo Carvalho de Melo 	if (perf_evlist__mmap(evlist, top->mmap_pages, false) < 0) {
9813780f488SNamhyung Kim 		ui__error("Failed to mmap with %d (%s)\n",
982c286c419SArnaldo Carvalho de Melo 			    errno, strerror(errno));
983c286c419SArnaldo Carvalho de Melo 		goto out_err;
984c286c419SArnaldo Carvalho de Melo 	}
985c286c419SArnaldo Carvalho de Melo 
986c286c419SArnaldo Carvalho de Melo 	return;
987c286c419SArnaldo Carvalho de Melo 
988c286c419SArnaldo Carvalho de Melo out_err:
989c286c419SArnaldo Carvalho de Melo 	exit_browser(0);
990c286c419SArnaldo Carvalho de Melo 	exit(0);
99172cb7013SArnaldo Carvalho de Melo }
99272cb7013SArnaldo Carvalho de Melo 
9931758af10SArnaldo Carvalho de Melo static int perf_top__setup_sample_type(struct perf_top *top)
99419d4ac3cSArnaldo Carvalho de Melo {
9951758af10SArnaldo Carvalho de Melo 	if (!top->sort_has_symbols) {
99619d4ac3cSArnaldo Carvalho de Melo 		if (symbol_conf.use_callchain) {
9973780f488SNamhyung Kim 			ui__error("Selected -g but \"sym\" not present in --sort/-s.");
99819d4ac3cSArnaldo Carvalho de Melo 			return -EINVAL;
99919d4ac3cSArnaldo Carvalho de Melo 		}
10001758af10SArnaldo Carvalho de Melo 	} else if (!top->dont_use_callchains && callchain_param.mode != CHAIN_NONE) {
100119d4ac3cSArnaldo Carvalho de Melo 		if (callchain_register_param(&callchain_param) < 0) {
10023780f488SNamhyung Kim 			ui__error("Can't register callchain params.\n");
100319d4ac3cSArnaldo Carvalho de Melo 			return -EINVAL;
100419d4ac3cSArnaldo Carvalho de Melo 		}
100519d4ac3cSArnaldo Carvalho de Melo 	}
100619d4ac3cSArnaldo Carvalho de Melo 
100719d4ac3cSArnaldo Carvalho de Melo 	return 0;
100819d4ac3cSArnaldo Carvalho de Melo }
100919d4ac3cSArnaldo Carvalho de Melo 
10101758af10SArnaldo Carvalho de Melo static int __cmd_top(struct perf_top *top)
1011716c69feSIngo Molnar {
1012716c69feSIngo Molnar 	pthread_t thread;
101319d4ac3cSArnaldo Carvalho de Melo 	int ret;
1014d8f66248SArnaldo Carvalho de Melo 	/*
1015b3165f41SArnaldo Carvalho de Melo 	 * FIXME: perf_session__new should allow passing a O_MMAP, so that all this
1016b3165f41SArnaldo Carvalho de Melo 	 * mmap reading, etc is encapsulated in it. Use O_WRONLY for now.
1017d8f66248SArnaldo Carvalho de Melo 	 */
10181758af10SArnaldo Carvalho de Melo 	top->session = perf_session__new(NULL, O_WRONLY, false, false, NULL);
10191758af10SArnaldo Carvalho de Melo 	if (top->session == NULL)
1020b3165f41SArnaldo Carvalho de Melo 		return -ENOMEM;
1021716c69feSIngo Molnar 
10221758af10SArnaldo Carvalho de Melo 	ret = perf_top__setup_sample_type(top);
102319d4ac3cSArnaldo Carvalho de Melo 	if (ret)
102419d4ac3cSArnaldo Carvalho de Melo 		goto out_delete;
102519d4ac3cSArnaldo Carvalho de Melo 
1026aa22dd49SNamhyung Kim 	if (perf_target__has_task(&top->target))
10271758af10SArnaldo Carvalho de Melo 		perf_event__synthesize_thread_map(&top->tool, top->evlist->threads,
1028743eb868SArnaldo Carvalho de Melo 						  perf_event__process,
10291758af10SArnaldo Carvalho de Melo 						  &top->session->host_machine);
10305b2bb75aSArnaldo Carvalho de Melo 	else
10311758af10SArnaldo Carvalho de Melo 		perf_event__synthesize_threads(&top->tool, perf_event__process,
10321758af10SArnaldo Carvalho de Melo 					       &top->session->host_machine);
10331758af10SArnaldo Carvalho de Melo 	perf_top__start_counters(top);
10341758af10SArnaldo Carvalho de Melo 	top->session->evlist = top->evlist;
10351758af10SArnaldo Carvalho de Melo 	perf_session__update_sample_type(top->session);
103686470930SIngo Molnar 
10372f01190aSFrederic Weisbecker 	/* Wait for a minimal set of events before starting the snapshot */
10381758af10SArnaldo Carvalho de Melo 	poll(top->evlist->pollfd, top->evlist->nr_fds, 100);
10392f01190aSFrederic Weisbecker 
10401758af10SArnaldo Carvalho de Melo 	perf_top__mmap_read(top);
10412f01190aSFrederic Weisbecker 
1042c0443df1SArnaldo Carvalho de Melo 	if (pthread_create(&thread, NULL, (use_browser > 0 ? display_thread_tui :
10431758af10SArnaldo Carvalho de Melo 							    display_thread), top)) {
10443780f488SNamhyung Kim 		ui__error("Could not create display thread.\n");
104586470930SIngo Molnar 		exit(-1);
104686470930SIngo Molnar 	}
104786470930SIngo Molnar 
10481758af10SArnaldo Carvalho de Melo 	if (top->realtime_prio) {
104986470930SIngo Molnar 		struct sched_param param;
105086470930SIngo Molnar 
10511758af10SArnaldo Carvalho de Melo 		param.sched_priority = top->realtime_prio;
105286470930SIngo Molnar 		if (sched_setscheduler(0, SCHED_FIFO, &param)) {
10533780f488SNamhyung Kim 			ui__error("Could not set realtime priority.\n");
105486470930SIngo Molnar 			exit(-1);
105586470930SIngo Molnar 		}
105686470930SIngo Molnar 	}
105786470930SIngo Molnar 
105886470930SIngo Molnar 	while (1) {
10591758af10SArnaldo Carvalho de Melo 		u64 hits = top->samples;
106086470930SIngo Molnar 
10611758af10SArnaldo Carvalho de Melo 		perf_top__mmap_read(top);
106286470930SIngo Molnar 
10631758af10SArnaldo Carvalho de Melo 		if (hits == top->samples)
10641758af10SArnaldo Carvalho de Melo 			ret = poll(top->evlist->pollfd, top->evlist->nr_fds, 100);
106586470930SIngo Molnar 	}
106686470930SIngo Molnar 
106719d4ac3cSArnaldo Carvalho de Melo out_delete:
10681758af10SArnaldo Carvalho de Melo 	perf_session__delete(top->session);
10691758af10SArnaldo Carvalho de Melo 	top->session = NULL;
107019d4ac3cSArnaldo Carvalho de Melo 
107119d4ac3cSArnaldo Carvalho de Melo 	return 0;
107219d4ac3cSArnaldo Carvalho de Melo }
107319d4ac3cSArnaldo Carvalho de Melo 
107419d4ac3cSArnaldo Carvalho de Melo static int
10751758af10SArnaldo Carvalho de Melo parse_callchain_opt(const struct option *opt, const char *arg, int unset)
107619d4ac3cSArnaldo Carvalho de Melo {
10771758af10SArnaldo Carvalho de Melo 	struct perf_top *top = (struct perf_top *)opt->value;
107819d4ac3cSArnaldo Carvalho de Melo 	char *tok, *tok2;
107919d4ac3cSArnaldo Carvalho de Melo 	char *endptr;
108019d4ac3cSArnaldo Carvalho de Melo 
108119d4ac3cSArnaldo Carvalho de Melo 	/*
108219d4ac3cSArnaldo Carvalho de Melo 	 * --no-call-graph
108319d4ac3cSArnaldo Carvalho de Melo 	 */
108419d4ac3cSArnaldo Carvalho de Melo 	if (unset) {
10851758af10SArnaldo Carvalho de Melo 		top->dont_use_callchains = true;
108619d4ac3cSArnaldo Carvalho de Melo 		return 0;
108719d4ac3cSArnaldo Carvalho de Melo 	}
108819d4ac3cSArnaldo Carvalho de Melo 
108919d4ac3cSArnaldo Carvalho de Melo 	symbol_conf.use_callchain = true;
109019d4ac3cSArnaldo Carvalho de Melo 
109119d4ac3cSArnaldo Carvalho de Melo 	if (!arg)
109219d4ac3cSArnaldo Carvalho de Melo 		return 0;
109319d4ac3cSArnaldo Carvalho de Melo 
109419d4ac3cSArnaldo Carvalho de Melo 	tok = strtok((char *)arg, ",");
109519d4ac3cSArnaldo Carvalho de Melo 	if (!tok)
109619d4ac3cSArnaldo Carvalho de Melo 		return -1;
109719d4ac3cSArnaldo Carvalho de Melo 
109819d4ac3cSArnaldo Carvalho de Melo 	/* get the output mode */
109919d4ac3cSArnaldo Carvalho de Melo 	if (!strncmp(tok, "graph", strlen(arg)))
110019d4ac3cSArnaldo Carvalho de Melo 		callchain_param.mode = CHAIN_GRAPH_ABS;
110119d4ac3cSArnaldo Carvalho de Melo 
110219d4ac3cSArnaldo Carvalho de Melo 	else if (!strncmp(tok, "flat", strlen(arg)))
110319d4ac3cSArnaldo Carvalho de Melo 		callchain_param.mode = CHAIN_FLAT;
110419d4ac3cSArnaldo Carvalho de Melo 
110519d4ac3cSArnaldo Carvalho de Melo 	else if (!strncmp(tok, "fractal", strlen(arg)))
110619d4ac3cSArnaldo Carvalho de Melo 		callchain_param.mode = CHAIN_GRAPH_REL;
110719d4ac3cSArnaldo Carvalho de Melo 
110819d4ac3cSArnaldo Carvalho de Melo 	else if (!strncmp(tok, "none", strlen(arg))) {
110919d4ac3cSArnaldo Carvalho de Melo 		callchain_param.mode = CHAIN_NONE;
111019d4ac3cSArnaldo Carvalho de Melo 		symbol_conf.use_callchain = false;
111119d4ac3cSArnaldo Carvalho de Melo 
111219d4ac3cSArnaldo Carvalho de Melo 		return 0;
1113806fb630SArnaldo Carvalho de Melo 	} else
111419d4ac3cSArnaldo Carvalho de Melo 		return -1;
111519d4ac3cSArnaldo Carvalho de Melo 
111619d4ac3cSArnaldo Carvalho de Melo 	/* get the min percentage */
111719d4ac3cSArnaldo Carvalho de Melo 	tok = strtok(NULL, ",");
111819d4ac3cSArnaldo Carvalho de Melo 	if (!tok)
111919d4ac3cSArnaldo Carvalho de Melo 		goto setup;
112019d4ac3cSArnaldo Carvalho de Melo 
112119d4ac3cSArnaldo Carvalho de Melo 	callchain_param.min_percent = strtod(tok, &endptr);
112219d4ac3cSArnaldo Carvalho de Melo 	if (tok == endptr)
112319d4ac3cSArnaldo Carvalho de Melo 		return -1;
112419d4ac3cSArnaldo Carvalho de Melo 
112519d4ac3cSArnaldo Carvalho de Melo 	/* get the print limit */
112619d4ac3cSArnaldo Carvalho de Melo 	tok2 = strtok(NULL, ",");
112719d4ac3cSArnaldo Carvalho de Melo 	if (!tok2)
112819d4ac3cSArnaldo Carvalho de Melo 		goto setup;
112919d4ac3cSArnaldo Carvalho de Melo 
113019d4ac3cSArnaldo Carvalho de Melo 	if (tok2[0] != 'c') {
113119d4ac3cSArnaldo Carvalho de Melo 		callchain_param.print_limit = strtod(tok2, &endptr);
113219d4ac3cSArnaldo Carvalho de Melo 		tok2 = strtok(NULL, ",");
113319d4ac3cSArnaldo Carvalho de Melo 		if (!tok2)
113419d4ac3cSArnaldo Carvalho de Melo 			goto setup;
113519d4ac3cSArnaldo Carvalho de Melo 	}
113619d4ac3cSArnaldo Carvalho de Melo 
113719d4ac3cSArnaldo Carvalho de Melo 	/* get the call chain order */
113819d4ac3cSArnaldo Carvalho de Melo 	if (!strcmp(tok2, "caller"))
113919d4ac3cSArnaldo Carvalho de Melo 		callchain_param.order = ORDER_CALLER;
114019d4ac3cSArnaldo Carvalho de Melo 	else if (!strcmp(tok2, "callee"))
114119d4ac3cSArnaldo Carvalho de Melo 		callchain_param.order = ORDER_CALLEE;
114219d4ac3cSArnaldo Carvalho de Melo 	else
114319d4ac3cSArnaldo Carvalho de Melo 		return -1;
114419d4ac3cSArnaldo Carvalho de Melo setup:
114519d4ac3cSArnaldo Carvalho de Melo 	if (callchain_register_param(&callchain_param) < 0) {
114619d4ac3cSArnaldo Carvalho de Melo 		fprintf(stderr, "Can't register callchain params\n");
114719d4ac3cSArnaldo Carvalho de Melo 		return -1;
114819d4ac3cSArnaldo Carvalho de Melo 	}
114986470930SIngo Molnar 	return 0;
115086470930SIngo Molnar }
115186470930SIngo Molnar 
115286470930SIngo Molnar static const char * const top_usage[] = {
115386470930SIngo Molnar 	"perf top [<options>]",
115486470930SIngo Molnar 	NULL
115586470930SIngo Molnar };
115686470930SIngo Molnar 
11571758af10SArnaldo Carvalho de Melo int cmd_top(int argc, const char **argv, const char *prefix __used)
11581758af10SArnaldo Carvalho de Melo {
11591758af10SArnaldo Carvalho de Melo 	struct perf_evsel *pos;
116016ad2ffbSNamhyung Kim 	int status;
116116ad2ffbSNamhyung Kim 	char errbuf[BUFSIZ];
11621758af10SArnaldo Carvalho de Melo 	struct perf_top top = {
11631758af10SArnaldo Carvalho de Melo 		.count_filter	     = 5,
11641758af10SArnaldo Carvalho de Melo 		.delay_secs	     = 2,
1165447a6013SArnaldo Carvalho de Melo 		.freq		     = 4000, /* 4 KHz */
11661758af10SArnaldo Carvalho de Melo 		.mmap_pages	     = 128,
11671758af10SArnaldo Carvalho de Melo 		.sym_pcnt_filter     = 5,
1168d1cb9fceSNamhyung Kim 		.target		     = {
1169d1cb9fceSNamhyung Kim 			.uses_mmap   = true,
1170d1cb9fceSNamhyung Kim 		},
11711758af10SArnaldo Carvalho de Melo 	};
11721758af10SArnaldo Carvalho de Melo 	char callchain_default_opt[] = "fractal,0.5,callee";
11731758af10SArnaldo Carvalho de Melo 	const struct option options[] = {
11748c3e10ebSArnaldo Carvalho de Melo 	OPT_CALLBACK('e', "event", &top.evlist, "event",
117586470930SIngo Molnar 		     "event selector. use 'perf list' to list available events",
1176f120f9d5SJiri Olsa 		     parse_events_option),
11771758af10SArnaldo Carvalho de Melo 	OPT_INTEGER('c', "count", &top.default_interval,
117886470930SIngo Molnar 		    "event period to sample"),
1179fe9d18a7SNamhyung Kim 	OPT_STRING('p', "pid", &top.target.pid, "pid",
1180d6d901c2SZhang, Yanmin 		    "profile events on existing process id"),
1181fe9d18a7SNamhyung Kim 	OPT_STRING('t', "tid", &top.target.tid, "tid",
1182d6d901c2SZhang, Yanmin 		    "profile events on existing thread id"),
1183fe9d18a7SNamhyung Kim 	OPT_BOOLEAN('a', "all-cpus", &top.target.system_wide,
118486470930SIngo Molnar 			    "system-wide collection from all CPUs"),
1185fe9d18a7SNamhyung Kim 	OPT_STRING('C', "cpu", &top.target.cpu_list, "cpu",
1186c45c6ea2SStephane Eranian 		    "list of cpus to monitor"),
1187b32d133aSArnaldo Carvalho de Melo 	OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1188b32d133aSArnaldo Carvalho de Melo 		   "file", "vmlinux pathname"),
11898c3e10ebSArnaldo Carvalho de Melo 	OPT_BOOLEAN('K', "hide_kernel_symbols", &top.hide_kernel_symbols,
11908ffcda17SArnaldo Carvalho de Melo 		    "hide kernel symbols"),
11911758af10SArnaldo Carvalho de Melo 	OPT_UINTEGER('m', "mmap-pages", &top.mmap_pages, "number of mmap data pages"),
11921758af10SArnaldo Carvalho de Melo 	OPT_INTEGER('r', "realtime", &top.realtime_prio,
119386470930SIngo Molnar 		    "collect data with this RT SCHED_FIFO priority"),
11948c3e10ebSArnaldo Carvalho de Melo 	OPT_INTEGER('d', "delay", &top.delay_secs,
119586470930SIngo Molnar 		    "number of seconds to delay between refreshes"),
11961758af10SArnaldo Carvalho de Melo 	OPT_BOOLEAN('D', "dump-symtab", &top.dump_symtab,
119786470930SIngo Molnar 			    "dump the symbol table used for profiling"),
11988c3e10ebSArnaldo Carvalho de Melo 	OPT_INTEGER('f', "count-filter", &top.count_filter,
119986470930SIngo Molnar 		    "only display functions with more events than this"),
12001758af10SArnaldo Carvalho de Melo 	OPT_BOOLEAN('g', "group", &top.group,
120186470930SIngo Molnar 			    "put the counters into a counter group"),
12021758af10SArnaldo Carvalho de Melo 	OPT_BOOLEAN('i', "inherit", &top.inherit,
12030fdc7e67SMike Galbraith 		    "child tasks inherit counters"),
12041758af10SArnaldo Carvalho de Melo 	OPT_STRING(0, "sym-annotate", &top.sym_filter, "symbol name",
12056cff0e8dSKirill Smelkov 		    "symbol to annotate"),
12068c3e10ebSArnaldo Carvalho de Melo 	OPT_BOOLEAN('z', "zero", &top.zero,
120786470930SIngo Molnar 		    "zero history across updates"),
12088c3e10ebSArnaldo Carvalho de Melo 	OPT_INTEGER('F', "freq", &top.freq,
120986470930SIngo Molnar 		    "profile at this frequency"),
12108c3e10ebSArnaldo Carvalho de Melo 	OPT_INTEGER('E', "entries", &top.print_entries,
121186470930SIngo Molnar 		    "display this many functions"),
12128c3e10ebSArnaldo Carvalho de Melo 	OPT_BOOLEAN('U', "hide_user_symbols", &top.hide_user_symbols,
12138ffcda17SArnaldo Carvalho de Melo 		    "hide user symbols"),
12141758af10SArnaldo Carvalho de Melo 	OPT_BOOLEAN(0, "tui", &top.use_tui, "Use the TUI interface"),
12151758af10SArnaldo Carvalho de Melo 	OPT_BOOLEAN(0, "stdio", &top.use_stdio, "Use the stdio interface"),
1216c0555642SIan Munsie 	OPT_INCR('v', "verbose", &verbose,
12173da297a6SIngo Molnar 		    "be more verbose (show counter open errors, etc)"),
1218ab81f3fdSArnaldo Carvalho de Melo 	OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1219ab81f3fdSArnaldo Carvalho de Melo 		   "sort by key(s): pid, comm, dso, symbol, parent"),
1220ab81f3fdSArnaldo Carvalho de Melo 	OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
1221ab81f3fdSArnaldo Carvalho de Melo 		    "Show a column with the number of samples"),
12221758af10SArnaldo Carvalho de Melo 	OPT_CALLBACK_DEFAULT('G', "call-graph", &top, "output_type,min_percent, call_order",
122319d4ac3cSArnaldo Carvalho de Melo 		     "Display callchains using output_type (graph, flat, fractal, or none), min percent threshold and callchain order. "
122419d4ac3cSArnaldo Carvalho de Melo 		     "Default: fractal,0.5,callee", &parse_callchain_opt,
122519d4ac3cSArnaldo Carvalho de Melo 		     callchain_default_opt),
1226ab81f3fdSArnaldo Carvalho de Melo 	OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period,
1227ab81f3fdSArnaldo Carvalho de Melo 		    "Show a column with the sum of periods"),
1228ab81f3fdSArnaldo Carvalho de Melo 	OPT_STRING(0, "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
1229ab81f3fdSArnaldo Carvalho de Melo 		   "only consider symbols in these dsos"),
1230ab81f3fdSArnaldo Carvalho de Melo 	OPT_STRING(0, "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
1231ab81f3fdSArnaldo Carvalho de Melo 		   "only consider symbols in these comms"),
1232ab81f3fdSArnaldo Carvalho de Melo 	OPT_STRING(0, "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
1233ab81f3fdSArnaldo Carvalho de Melo 		   "only consider these symbols"),
123464c6f0c7SArnaldo Carvalho de Melo 	OPT_BOOLEAN(0, "source", &symbol_conf.annotate_src,
123564c6f0c7SArnaldo Carvalho de Melo 		    "Interleave source code with assembly code (default)"),
123664c6f0c7SArnaldo Carvalho de Melo 	OPT_BOOLEAN(0, "asm-raw", &symbol_conf.annotate_asm_raw,
123764c6f0c7SArnaldo Carvalho de Melo 		    "Display raw encoding of assembly instructions (default)"),
123864c6f0c7SArnaldo Carvalho de Melo 	OPT_STRING('M', "disassembler-style", &disassembler_style, "disassembler style",
123964c6f0c7SArnaldo Carvalho de Melo 		   "Specify disassembler style (e.g. -M intel for intel syntax)"),
1240fe9d18a7SNamhyung Kim 	OPT_STRING('u', "uid", &top.target.uid_str, "user", "user to profile"),
124186470930SIngo Molnar 	OPT_END()
124286470930SIngo Molnar 	};
124386470930SIngo Molnar 
12448c3e10ebSArnaldo Carvalho de Melo 	top.evlist = perf_evlist__new(NULL, NULL);
12458c3e10ebSArnaldo Carvalho de Melo 	if (top.evlist == NULL)
1246361c99a6SArnaldo Carvalho de Melo 		return -ENOMEM;
1247361c99a6SArnaldo Carvalho de Melo 
1248ab81f3fdSArnaldo Carvalho de Melo 	symbol_conf.exclude_other = false;
124986470930SIngo Molnar 
125086470930SIngo Molnar 	argc = parse_options(argc, argv, options, top_usage, 0);
125186470930SIngo Molnar 	if (argc)
125286470930SIngo Molnar 		usage_with_options(top_usage, options);
125386470930SIngo Molnar 
1254ab81f3fdSArnaldo Carvalho de Melo 	if (sort_order == default_sort_order)
1255ab81f3fdSArnaldo Carvalho de Melo 		sort_order = "dso,symbol";
1256ab81f3fdSArnaldo Carvalho de Melo 
1257ab81f3fdSArnaldo Carvalho de Melo 	setup_sorting(top_usage, options);
1258ab81f3fdSArnaldo Carvalho de Melo 
12591758af10SArnaldo Carvalho de Melo 	if (top.use_stdio)
1260c0443df1SArnaldo Carvalho de Melo 		use_browser = 0;
12611758af10SArnaldo Carvalho de Melo 	else if (top.use_tui)
1262c0443df1SArnaldo Carvalho de Melo 		use_browser = 1;
1263c0443df1SArnaldo Carvalho de Melo 
1264c0443df1SArnaldo Carvalho de Melo 	setup_browser(false);
1265c0443df1SArnaldo Carvalho de Melo 
126616ad2ffbSNamhyung Kim 	status = perf_target__validate(&top.target);
126716ad2ffbSNamhyung Kim 	if (status) {
126816ad2ffbSNamhyung Kim 		perf_target__strerror(&top.target, status, errbuf, BUFSIZ);
126916ad2ffbSNamhyung Kim 		ui__warning("%s", errbuf);
127016ad2ffbSNamhyung Kim 	}
12714bd0f2d2SNamhyung Kim 
127216ad2ffbSNamhyung Kim 	status = perf_target__parse_uid(&top.target);
127316ad2ffbSNamhyung Kim 	if (status) {
127416ad2ffbSNamhyung Kim 		int saved_errno = errno;
127516ad2ffbSNamhyung Kim 
127616ad2ffbSNamhyung Kim 		perf_target__strerror(&top.target, status, errbuf, BUFSIZ);
12773780f488SNamhyung Kim 		ui__error("%s", errbuf);
127816ad2ffbSNamhyung Kim 
127916ad2ffbSNamhyung Kim 		status = -saved_errno;
12800d37aa34SArnaldo Carvalho de Melo 		goto out_delete_evlist;
128116ad2ffbSNamhyung Kim 	}
12820d37aa34SArnaldo Carvalho de Melo 
128380eebd94SArnaldo Carvalho de Melo 	if (perf_target__none(&top.target))
128410b47d54SArnaldo Carvalho de Melo 		top.target.system_wide = true;
128510b47d54SArnaldo Carvalho de Melo 
1286b809ac10SNamhyung Kim 	if (perf_evlist__create_maps(top.evlist, &top.target) < 0)
12877e2ed097SArnaldo Carvalho de Melo 		usage_with_options(top_usage, options);
12887e2ed097SArnaldo Carvalho de Melo 
12898c3e10ebSArnaldo Carvalho de Melo 	if (!top.evlist->nr_entries &&
12908c3e10ebSArnaldo Carvalho de Melo 	    perf_evlist__add_default(top.evlist) < 0) {
12913780f488SNamhyung Kim 		ui__error("Not enough memory for event selector list\n");
129269aad6f1SArnaldo Carvalho de Melo 		return -ENOMEM;
129369aad6f1SArnaldo Carvalho de Melo 	}
12945a8e5a30SArnaldo Carvalho de Melo 
1295d04b35f8SArnaldo Carvalho de Melo 	symbol_conf.nr_events = top.evlist->nr_entries;
1296d04b35f8SArnaldo Carvalho de Melo 
12978c3e10ebSArnaldo Carvalho de Melo 	if (top.delay_secs < 1)
12988c3e10ebSArnaldo Carvalho de Melo 		top.delay_secs = 1;
129986470930SIngo Molnar 
13007e4ff9e3SMike Galbraith 	/*
13017e4ff9e3SMike Galbraith 	 * User specified count overrides default frequency.
13027e4ff9e3SMike Galbraith 	 */
13031758af10SArnaldo Carvalho de Melo 	if (top.default_interval)
13048c3e10ebSArnaldo Carvalho de Melo 		top.freq = 0;
13058c3e10ebSArnaldo Carvalho de Melo 	else if (top.freq) {
13061758af10SArnaldo Carvalho de Melo 		top.default_interval = top.freq;
13077e4ff9e3SMike Galbraith 	} else {
13083780f488SNamhyung Kim 		ui__error("frequency and count are zero, aborting\n");
13097e4ff9e3SMike Galbraith 		exit(EXIT_FAILURE);
13107e4ff9e3SMike Galbraith 	}
13117e4ff9e3SMike Galbraith 
13128c3e10ebSArnaldo Carvalho de Melo 	list_for_each_entry(pos, &top.evlist->entries, node) {
131369aad6f1SArnaldo Carvalho de Melo 		/*
131469aad6f1SArnaldo Carvalho de Melo 		 * Fill in the ones not specifically initialized via -c:
131569aad6f1SArnaldo Carvalho de Melo 		 */
1316806fb630SArnaldo Carvalho de Melo 		if (!pos->attr.sample_period)
13171758af10SArnaldo Carvalho de Melo 			pos->attr.sample_period = top.default_interval;
131869aad6f1SArnaldo Carvalho de Melo 	}
131969aad6f1SArnaldo Carvalho de Melo 
13208c3e10ebSArnaldo Carvalho de Melo 	top.sym_evsel = list_entry(top.evlist->entries.next, struct perf_evsel, node);
1321cc841580SArnaldo Carvalho de Melo 
1322ab81f3fdSArnaldo Carvalho de Melo 	symbol_conf.priv_size = sizeof(struct annotation);
132369aad6f1SArnaldo Carvalho de Melo 
132469aad6f1SArnaldo Carvalho de Melo 	symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL);
132569aad6f1SArnaldo Carvalho de Melo 	if (symbol__init() < 0)
132669aad6f1SArnaldo Carvalho de Melo 		return -1;
132769aad6f1SArnaldo Carvalho de Melo 
1328ab81f3fdSArnaldo Carvalho de Melo 	sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list, "dso", stdout);
1329ab81f3fdSArnaldo Carvalho de Melo 	sort_entry__setup_elide(&sort_comm, symbol_conf.comm_list, "comm", stdout);
1330ab81f3fdSArnaldo Carvalho de Melo 	sort_entry__setup_elide(&sort_sym, symbol_conf.sym_list, "symbol", stdout);
1331ab81f3fdSArnaldo Carvalho de Melo 
133219d4ac3cSArnaldo Carvalho de Melo 	/*
133319d4ac3cSArnaldo Carvalho de Melo 	 * Avoid annotation data structures overhead when symbols aren't on the
133419d4ac3cSArnaldo Carvalho de Melo 	 * sort list.
133519d4ac3cSArnaldo Carvalho de Melo 	 */
13361758af10SArnaldo Carvalho de Melo 	top.sort_has_symbols = sort_sym.list.next != NULL;
133719d4ac3cSArnaldo Carvalho de Melo 
13381758af10SArnaldo Carvalho de Melo 	get_term_dimensions(&top.winsize);
13398c3e10ebSArnaldo Carvalho de Melo 	if (top.print_entries == 0) {
13401758af10SArnaldo Carvalho de Melo 		struct sigaction act = {
13411758af10SArnaldo Carvalho de Melo 			.sa_sigaction = perf_top__sig_winch,
13421758af10SArnaldo Carvalho de Melo 			.sa_flags     = SA_SIGINFO,
13431758af10SArnaldo Carvalho de Melo 		};
13441758af10SArnaldo Carvalho de Melo 		perf_top__update_print_entries(&top);
13451758af10SArnaldo Carvalho de Melo 		sigaction(SIGWINCH, &act, NULL);
13463b6ed988SArnaldo Carvalho de Melo 	}
13473b6ed988SArnaldo Carvalho de Melo 
13481758af10SArnaldo Carvalho de Melo 	status = __cmd_top(&top);
1349806fb630SArnaldo Carvalho de Melo 
13500d37aa34SArnaldo Carvalho de Melo out_delete_evlist:
13518c3e10ebSArnaldo Carvalho de Melo 	perf_evlist__delete(top.evlist);
135269aad6f1SArnaldo Carvalho de Melo 
135369aad6f1SArnaldo Carvalho de Melo 	return status;
135486470930SIngo Molnar }
1355