xref: /openbmc/linux/tools/perf/builtin-top.c (revision b2b7e9eb)
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>
4586470930SIngo Molnar #include <fcntl.h>
4686470930SIngo Molnar 
4786470930SIngo Molnar #include <stdio.h>
48923c42c1SMike Galbraith #include <termios.h>
49923c42c1SMike Galbraith #include <unistd.h>
509486aa38SArnaldo Carvalho de Melo #include <inttypes.h>
5186470930SIngo Molnar 
5286470930SIngo Molnar #include <errno.h>
5386470930SIngo Molnar #include <time.h>
5486470930SIngo Molnar #include <sched.h>
5586470930SIngo Molnar 
5686470930SIngo Molnar #include <sys/syscall.h>
5786470930SIngo Molnar #include <sys/ioctl.h>
5886470930SIngo Molnar #include <sys/poll.h>
5986470930SIngo Molnar #include <sys/prctl.h>
6086470930SIngo Molnar #include <sys/wait.h>
6186470930SIngo Molnar #include <sys/uio.h>
6286470930SIngo Molnar #include <sys/mman.h>
6386470930SIngo Molnar 
6486470930SIngo Molnar #include <linux/unistd.h>
6586470930SIngo Molnar #include <linux/types.h>
6686470930SIngo Molnar 
678c3e10ebSArnaldo Carvalho de Melo static struct perf_top top = {
688c3e10ebSArnaldo Carvalho de Melo 	.count_filter		= 5,
698c3e10ebSArnaldo Carvalho de Melo 	.delay_secs		= 2,
708c3e10ebSArnaldo Carvalho de Melo 	.target_pid		= -1,
718c3e10ebSArnaldo Carvalho de Melo 	.target_tid		= -1,
728c3e10ebSArnaldo Carvalho de Melo 	.freq			= 1000, /* 1 KHz */
738c3e10ebSArnaldo Carvalho de Melo };
74361c99a6SArnaldo Carvalho de Melo 
75c0555642SIan Munsie static bool			system_wide			=  false;
7686470930SIngo Molnar 
77c0443df1SArnaldo Carvalho de Melo static bool			use_tui, use_stdio;
78c0443df1SArnaldo Carvalho de Melo 
7919d4ac3cSArnaldo Carvalho de Melo static bool			sort_has_symbols;
8019d4ac3cSArnaldo Carvalho de Melo 
8119d4ac3cSArnaldo Carvalho de Melo static bool			dont_use_callchains;
8219d4ac3cSArnaldo Carvalho de Melo static char			callchain_default_opt[]		= "fractal,0.5,callee";
8319d4ac3cSArnaldo Carvalho de Melo 
8419d4ac3cSArnaldo Carvalho de Melo 
857e4ff9e3SMike Galbraith static int			default_interval		=      0;
8686470930SIngo Molnar 
875f6f5580SArnaldo Carvalho de Melo static bool			kptr_restrict_warned;
88e4a338d0SArnaldo Carvalho de Melo static bool			vmlinux_warned;
89c0555642SIan Munsie static bool			inherit				=  false;
901967936dSArnaldo Carvalho de Melo static int			realtime_prio			=      0;
91c0555642SIan Munsie static bool			group				=  false;
9270db7533SArnaldo Carvalho de Melo static unsigned int		mmap_pages			=    128;
9386470930SIngo Molnar 
94c0555642SIan Munsie static bool			dump_symtab                     =  false;
9586470930SIngo Molnar 
9613cc5079SArnaldo Carvalho de Melo static struct winsize		winsize;
978ffcda17SArnaldo Carvalho de Melo 
98edb7c60eSArnaldo Carvalho de Melo static const char		*sym_filter			=   NULL;
99923c42c1SMike Galbraith static int			sym_pcnt_filter			=      5;
10086470930SIngo Molnar 
101923c42c1SMike Galbraith /*
102923c42c1SMike Galbraith  * Source functions
103923c42c1SMike Galbraith  */
104923c42c1SMike Galbraith 
105895f0edcSArnaldo Carvalho de Melo void get_term_dimensions(struct winsize *ws)
1063b6ed988SArnaldo Carvalho de Melo {
10713cc5079SArnaldo Carvalho de Melo 	char *s = getenv("LINES");
1083b6ed988SArnaldo Carvalho de Melo 
10913cc5079SArnaldo Carvalho de Melo 	if (s != NULL) {
11013cc5079SArnaldo Carvalho de Melo 		ws->ws_row = atoi(s);
11113cc5079SArnaldo Carvalho de Melo 		s = getenv("COLUMNS");
11213cc5079SArnaldo Carvalho de Melo 		if (s != NULL) {
11313cc5079SArnaldo Carvalho de Melo 			ws->ws_col = atoi(s);
11413cc5079SArnaldo Carvalho de Melo 			if (ws->ws_row && ws->ws_col)
11513cc5079SArnaldo Carvalho de Melo 				return;
11613cc5079SArnaldo Carvalho de Melo 		}
11713cc5079SArnaldo Carvalho de Melo 	}
1183b6ed988SArnaldo Carvalho de Melo #ifdef TIOCGWINSZ
11913cc5079SArnaldo Carvalho de Melo 	if (ioctl(1, TIOCGWINSZ, ws) == 0 &&
12013cc5079SArnaldo Carvalho de Melo 	    ws->ws_row && ws->ws_col)
12113cc5079SArnaldo Carvalho de Melo 		return;
1223b6ed988SArnaldo Carvalho de Melo #endif
12313cc5079SArnaldo Carvalho de Melo 	ws->ws_row = 25;
12413cc5079SArnaldo Carvalho de Melo 	ws->ws_col = 80;
1253b6ed988SArnaldo Carvalho de Melo }
1263b6ed988SArnaldo Carvalho de Melo 
12713cc5079SArnaldo Carvalho de Melo static void update_print_entries(struct winsize *ws)
1283b6ed988SArnaldo Carvalho de Melo {
1298c3e10ebSArnaldo Carvalho de Melo 	top.print_entries = ws->ws_row;
13013cc5079SArnaldo Carvalho de Melo 
1318c3e10ebSArnaldo Carvalho de Melo 	if (top.print_entries > 9)
1328c3e10ebSArnaldo Carvalho de Melo 		top.print_entries -= 9;
1333b6ed988SArnaldo Carvalho de Melo }
1343b6ed988SArnaldo Carvalho de Melo 
1353b6ed988SArnaldo Carvalho de Melo static void sig_winch_handler(int sig __used)
1363b6ed988SArnaldo Carvalho de Melo {
13713cc5079SArnaldo Carvalho de Melo 	get_term_dimensions(&winsize);
13813cc5079SArnaldo Carvalho de Melo 	update_print_entries(&winsize);
1393b6ed988SArnaldo Carvalho de Melo }
1403b6ed988SArnaldo Carvalho de Melo 
141ab81f3fdSArnaldo Carvalho de Melo static int parse_source(struct hist_entry *he)
142923c42c1SMike Galbraith {
143923c42c1SMike Galbraith 	struct symbol *sym;
144ce6f4fabSArnaldo Carvalho de Melo 	struct annotation *notes;
145439d473bSArnaldo Carvalho de Melo 	struct map *map;
14636532461SArnaldo Carvalho de Melo 	int err = -1;
147923c42c1SMike Galbraith 
148ab81f3fdSArnaldo Carvalho de Melo 	if (!he || !he->ms.sym)
149b0a9ab62SArnaldo Carvalho de Melo 		return -1;
150b0a9ab62SArnaldo Carvalho de Melo 
151ab81f3fdSArnaldo Carvalho de Melo 	sym = he->ms.sym;
152ab81f3fdSArnaldo Carvalho de Melo 	map = he->ms.map;
153b0a9ab62SArnaldo Carvalho de Melo 
154b0a9ab62SArnaldo Carvalho de Melo 	/*
155b0a9ab62SArnaldo Carvalho de Melo 	 * We can't annotate with just /proc/kallsyms
156b0a9ab62SArnaldo Carvalho de Melo 	 */
157878b439dSArnaldo Carvalho de Melo 	if (map->dso->symtab_type == SYMTAB__KALLSYMS) {
158ce6f4fabSArnaldo Carvalho de Melo 		pr_err("Can't annotate %s: No vmlinux file was found in the "
159ce6f4fabSArnaldo Carvalho de Melo 		       "path\n", sym->name);
160ce6f4fabSArnaldo Carvalho de Melo 		sleep(1);
161b0a9ab62SArnaldo Carvalho de Melo 		return -1;
162b269876cSArnaldo Carvalho de Melo 	}
163b269876cSArnaldo Carvalho de Melo 
164ce6f4fabSArnaldo Carvalho de Melo 	notes = symbol__annotation(sym);
165ce6f4fabSArnaldo Carvalho de Melo 	if (notes->src != NULL) {
166ce6f4fabSArnaldo Carvalho de Melo 		pthread_mutex_lock(&notes->lock);
167923c42c1SMike Galbraith 		goto out_assign;
168923c42c1SMike Galbraith 	}
169923c42c1SMike Galbraith 
170ce6f4fabSArnaldo Carvalho de Melo 	pthread_mutex_lock(&notes->lock);
171923c42c1SMike Galbraith 
17236532461SArnaldo Carvalho de Melo 	if (symbol__alloc_hist(sym, top.evlist->nr_entries) < 0) {
173c97cf422SArnaldo Carvalho de Melo 		pthread_mutex_unlock(&notes->lock);
17436532461SArnaldo Carvalho de Melo 		pr_err("Not enough memory for annotating '%s' symbol!\n",
17536532461SArnaldo Carvalho de Melo 		       sym->name);
176ce6f4fabSArnaldo Carvalho de Melo 		sleep(1);
177c97cf422SArnaldo Carvalho de Melo 		return err;
178923c42c1SMike Galbraith 	}
17936532461SArnaldo Carvalho de Melo 
180ab81f3fdSArnaldo Carvalho de Melo 	err = symbol__annotate(sym, map, 0);
18136532461SArnaldo Carvalho de Melo 	if (err == 0) {
182923c42c1SMike Galbraith out_assign:
183ab81f3fdSArnaldo Carvalho de Melo 		top.sym_filter_entry = he;
18436532461SArnaldo Carvalho de Melo 	}
185c97cf422SArnaldo Carvalho de Melo 
186ce6f4fabSArnaldo Carvalho de Melo 	pthread_mutex_unlock(&notes->lock);
18736532461SArnaldo Carvalho de Melo 	return err;
188923c42c1SMike Galbraith }
189923c42c1SMike Galbraith 
190ab81f3fdSArnaldo Carvalho de Melo static void __zero_source_counters(struct hist_entry *he)
191923c42c1SMike Galbraith {
192ab81f3fdSArnaldo Carvalho de Melo 	struct symbol *sym = he->ms.sym;
19336532461SArnaldo Carvalho de Melo 	symbol__annotate_zero_histograms(sym);
194923c42c1SMike Galbraith }
195923c42c1SMike Galbraith 
196ab81f3fdSArnaldo Carvalho de Melo static void record_precise_ip(struct hist_entry *he, int counter, u64 ip)
197923c42c1SMike Galbraith {
198ce6f4fabSArnaldo Carvalho de Melo 	struct annotation *notes;
199ce6f4fabSArnaldo Carvalho de Melo 	struct symbol *sym;
200ce6f4fabSArnaldo Carvalho de Melo 
201ab81f3fdSArnaldo Carvalho de Melo 	if (he == NULL || he->ms.sym == NULL ||
202ab81f3fdSArnaldo Carvalho de Melo 	    (he != top.sym_filter_entry && use_browser != 1))
203923c42c1SMike Galbraith 		return;
204923c42c1SMike Galbraith 
205ab81f3fdSArnaldo Carvalho de Melo 	sym = he->ms.sym;
206ce6f4fabSArnaldo Carvalho de Melo 	notes = symbol__annotation(sym);
207ce6f4fabSArnaldo Carvalho de Melo 
208ce6f4fabSArnaldo Carvalho de Melo 	if (pthread_mutex_trylock(&notes->lock))
209923c42c1SMike Galbraith 		return;
210923c42c1SMike Galbraith 
211ab81f3fdSArnaldo Carvalho de Melo 	if (notes->src == NULL &&
212ab81f3fdSArnaldo Carvalho de Melo 	    symbol__alloc_hist(sym, top.evlist->nr_entries) < 0) {
213ab81f3fdSArnaldo Carvalho de Melo 		pthread_mutex_unlock(&notes->lock);
214ab81f3fdSArnaldo Carvalho de Melo 		pr_err("Not enough memory for annotating '%s' symbol!\n",
215ab81f3fdSArnaldo Carvalho de Melo 		       sym->name);
216ab81f3fdSArnaldo Carvalho de Melo 		sleep(1);
217ab81f3fdSArnaldo Carvalho de Melo 		return;
218ab81f3fdSArnaldo Carvalho de Melo 	}
219ab81f3fdSArnaldo Carvalho de Melo 
220ab81f3fdSArnaldo Carvalho de Melo 	ip = he->ms.map->map_ip(he->ms.map, ip);
221ab81f3fdSArnaldo Carvalho de Melo 	symbol__inc_addr_samples(sym, he->ms.map, counter, ip);
222923c42c1SMike Galbraith 
223ce6f4fabSArnaldo Carvalho de Melo 	pthread_mutex_unlock(&notes->lock);
224923c42c1SMike Galbraith }
225923c42c1SMike Galbraith 
226ab81f3fdSArnaldo Carvalho de Melo static void show_details(struct hist_entry *he)
227923c42c1SMike Galbraith {
228ce6f4fabSArnaldo Carvalho de Melo 	struct annotation *notes;
229923c42c1SMike Galbraith 	struct symbol *symbol;
23036532461SArnaldo Carvalho de Melo 	int more;
231923c42c1SMike Galbraith 
232ab81f3fdSArnaldo Carvalho de Melo 	if (!he)
233923c42c1SMike Galbraith 		return;
234923c42c1SMike Galbraith 
235ab81f3fdSArnaldo Carvalho de Melo 	symbol = he->ms.sym;
236ce6f4fabSArnaldo Carvalho de Melo 	notes = symbol__annotation(symbol);
237ce6f4fabSArnaldo Carvalho de Melo 
238ce6f4fabSArnaldo Carvalho de Melo 	pthread_mutex_lock(&notes->lock);
239ce6f4fabSArnaldo Carvalho de Melo 
240ce6f4fabSArnaldo Carvalho de Melo 	if (notes->src == NULL)
241ce6f4fabSArnaldo Carvalho de Melo 		goto out_unlock;
242923c42c1SMike Galbraith 
2438c3e10ebSArnaldo Carvalho de Melo 	printf("Showing %s for %s\n", event_name(top.sym_evsel), symbol->name);
244923c42c1SMike Galbraith 	printf("  Events  Pcnt (>=%d%%)\n", sym_pcnt_filter);
245923c42c1SMike Galbraith 
246ab81f3fdSArnaldo Carvalho de Melo 	more = symbol__annotate_printf(symbol, he->ms.map, top.sym_evsel->idx,
247d5e3d747SArnaldo Carvalho de Melo 				       0, sym_pcnt_filter, top.print_entries, 4);
24836532461SArnaldo Carvalho de Melo 	if (top.zero)
24936532461SArnaldo Carvalho de Melo 		symbol__annotate_zero_histogram(symbol, top.sym_evsel->idx);
25036532461SArnaldo Carvalho de Melo 	else
251ce6f4fabSArnaldo Carvalho de Melo 		symbol__annotate_decay_histogram(symbol, top.sym_evsel->idx);
25236532461SArnaldo Carvalho de Melo 	if (more != 0)
253923c42c1SMike Galbraith 		printf("%d lines not displayed, maybe increase display entries [e]\n", more);
254ce6f4fabSArnaldo Carvalho de Melo out_unlock:
255ce6f4fabSArnaldo Carvalho de Melo 	pthread_mutex_unlock(&notes->lock);
256923c42c1SMike Galbraith }
25786470930SIngo Molnar 
25886470930SIngo Molnar static const char		CONSOLE_CLEAR[] = "";
25986470930SIngo Molnar 
260ab81f3fdSArnaldo Carvalho de Melo static struct hist_entry *
261ab81f3fdSArnaldo Carvalho de Melo 	perf_session__add_hist_entry(struct perf_session *session,
262ab81f3fdSArnaldo Carvalho de Melo 				     struct addr_location *al,
263ab81f3fdSArnaldo Carvalho de Melo 				     struct perf_sample *sample,
264ab81f3fdSArnaldo Carvalho de Melo 				     struct perf_evsel *evsel)
26586470930SIngo Molnar {
266ab81f3fdSArnaldo Carvalho de Melo 	struct hist_entry *he;
267ab81f3fdSArnaldo Carvalho de Melo 
268ab81f3fdSArnaldo Carvalho de Melo 	he = __hists__add_entry(&evsel->hists, al, NULL, sample->period);
269ab81f3fdSArnaldo Carvalho de Melo 	if (he == NULL)
270ab81f3fdSArnaldo Carvalho de Melo 		return NULL;
271ab81f3fdSArnaldo Carvalho de Melo 
272ab81f3fdSArnaldo Carvalho de Melo 	session->hists.stats.total_period += sample->period;
273ab81f3fdSArnaldo Carvalho de Melo 	hists__inc_nr_events(&evsel->hists, PERF_RECORD_SAMPLE);
274ab81f3fdSArnaldo Carvalho de Melo 	return he;
27586470930SIngo Molnar }
27686470930SIngo Molnar 
277dcc101d1SArnaldo Carvalho de Melo static void print_sym_table(void)
27886470930SIngo Molnar {
2798c3e10ebSArnaldo Carvalho de Melo 	char bf[160];
2808c3e10ebSArnaldo Carvalho de Melo 	int printed = 0;
28113cc5079SArnaldo Carvalho de Melo 	const int win_width = winsize.ws_col - 1;
28286470930SIngo Molnar 
28386470930SIngo Molnar 	puts(CONSOLE_CLEAR);
28486470930SIngo Molnar 
2858c3e10ebSArnaldo Carvalho de Melo 	perf_top__header_snprintf(&top, bf, sizeof(bf));
2868c3e10ebSArnaldo Carvalho de Melo 	printf("%s\n", bf);
28786470930SIngo Molnar 
2888c3e10ebSArnaldo Carvalho de Melo 	perf_top__reset_sample_counters(&top);
28986470930SIngo Molnar 
2901a105f74SArnaldo Carvalho de Melo 	printf("%-*.*s\n", win_width, win_width, graph_dotted_line);
29186470930SIngo Molnar 
292dcc101d1SArnaldo Carvalho de Melo 	if (top.total_lost_warned != top.session->hists.stats.total_lost) {
293dcc101d1SArnaldo Carvalho de Melo 		top.total_lost_warned = top.session->hists.stats.total_lost;
29493fc64f1SArnaldo Carvalho de Melo 		color_fprintf(stdout, PERF_COLOR_RED, "WARNING:");
29593fc64f1SArnaldo Carvalho de Melo 		printf(" LOST %" PRIu64 " events, Check IO/CPU overload\n",
296dcc101d1SArnaldo Carvalho de Melo 		       top.total_lost_warned);
297ab81f3fdSArnaldo Carvalho de Melo 		++printed;
29893fc64f1SArnaldo Carvalho de Melo 	}
29993fc64f1SArnaldo Carvalho de Melo 
300c97cf422SArnaldo Carvalho de Melo 	if (top.sym_filter_entry) {
301c97cf422SArnaldo Carvalho de Melo 		show_details(top.sym_filter_entry);
302923c42c1SMike Galbraith 		return;
303923c42c1SMike Galbraith 	}
304923c42c1SMike Galbraith 
305ab81f3fdSArnaldo Carvalho de Melo 	hists__collapse_resort_threaded(&top.sym_evsel->hists);
306ab81f3fdSArnaldo Carvalho de Melo 	hists__output_resort_threaded(&top.sym_evsel->hists);
307b079d4e9SArnaldo Carvalho de Melo 	hists__decay_entries_threaded(&top.sym_evsel->hists,
308b079d4e9SArnaldo Carvalho de Melo 				      top.hide_user_symbols,
309b079d4e9SArnaldo Carvalho de Melo 				      top.hide_kernel_symbols);
310ab81f3fdSArnaldo Carvalho de Melo 	hists__output_recalc_col_len(&top.sym_evsel->hists, winsize.ws_row - 3);
3111a105f74SArnaldo Carvalho de Melo 	putchar('\n');
312ab81f3fdSArnaldo Carvalho de Melo 	hists__fprintf(&top.sym_evsel->hists, NULL, false, false,
313ab81f3fdSArnaldo Carvalho de Melo 		       winsize.ws_row - 4 - printed, win_width, stdout);
31486470930SIngo Molnar }
31586470930SIngo Molnar 
316923c42c1SMike Galbraith static void prompt_integer(int *target, const char *msg)
317923c42c1SMike Galbraith {
318923c42c1SMike Galbraith 	char *buf = malloc(0), *p;
319923c42c1SMike Galbraith 	size_t dummy = 0;
320923c42c1SMike Galbraith 	int tmp;
321923c42c1SMike Galbraith 
322923c42c1SMike Galbraith 	fprintf(stdout, "\n%s: ", msg);
323923c42c1SMike Galbraith 	if (getline(&buf, &dummy, stdin) < 0)
324923c42c1SMike Galbraith 		return;
325923c42c1SMike Galbraith 
326923c42c1SMike Galbraith 	p = strchr(buf, '\n');
327923c42c1SMike Galbraith 	if (p)
328923c42c1SMike Galbraith 		*p = 0;
329923c42c1SMike Galbraith 
330923c42c1SMike Galbraith 	p = buf;
331923c42c1SMike Galbraith 	while(*p) {
332923c42c1SMike Galbraith 		if (!isdigit(*p))
333923c42c1SMike Galbraith 			goto out_free;
334923c42c1SMike Galbraith 		p++;
335923c42c1SMike Galbraith 	}
336923c42c1SMike Galbraith 	tmp = strtoul(buf, NULL, 10);
337923c42c1SMike Galbraith 	*target = tmp;
338923c42c1SMike Galbraith out_free:
339923c42c1SMike Galbraith 	free(buf);
340923c42c1SMike Galbraith }
341923c42c1SMike Galbraith 
342923c42c1SMike Galbraith static void prompt_percent(int *target, const char *msg)
343923c42c1SMike Galbraith {
344923c42c1SMike Galbraith 	int tmp = 0;
345923c42c1SMike Galbraith 
346923c42c1SMike Galbraith 	prompt_integer(&tmp, msg);
347923c42c1SMike Galbraith 	if (tmp >= 0 && tmp <= 100)
348923c42c1SMike Galbraith 		*target = tmp;
349923c42c1SMike Galbraith }
350923c42c1SMike Galbraith 
351ab81f3fdSArnaldo Carvalho de Melo static void prompt_symbol(struct hist_entry **target, const char *msg)
352923c42c1SMike Galbraith {
353923c42c1SMike Galbraith 	char *buf = malloc(0), *p;
354ab81f3fdSArnaldo Carvalho de Melo 	struct hist_entry *syme = *target, *n, *found = NULL;
355ab81f3fdSArnaldo Carvalho de Melo 	struct rb_node *next;
356923c42c1SMike Galbraith 	size_t dummy = 0;
357923c42c1SMike Galbraith 
358923c42c1SMike Galbraith 	/* zero counters of active symbol */
359923c42c1SMike Galbraith 	if (syme) {
360923c42c1SMike Galbraith 		__zero_source_counters(syme);
361923c42c1SMike Galbraith 		*target = NULL;
362923c42c1SMike Galbraith 	}
363923c42c1SMike Galbraith 
364923c42c1SMike Galbraith 	fprintf(stdout, "\n%s: ", msg);
365923c42c1SMike Galbraith 	if (getline(&buf, &dummy, stdin) < 0)
366923c42c1SMike Galbraith 		goto out_free;
367923c42c1SMike Galbraith 
368923c42c1SMike Galbraith 	p = strchr(buf, '\n');
369923c42c1SMike Galbraith 	if (p)
370923c42c1SMike Galbraith 		*p = 0;
371923c42c1SMike Galbraith 
372ab81f3fdSArnaldo Carvalho de Melo 	next = rb_first(&top.sym_evsel->hists.entries);
373ab81f3fdSArnaldo Carvalho de Melo 	while (next) {
374ab81f3fdSArnaldo Carvalho de Melo 		n = rb_entry(next, struct hist_entry, rb_node);
375ab81f3fdSArnaldo Carvalho de Melo 		if (n->ms.sym && !strcmp(buf, n->ms.sym->name)) {
376ab81f3fdSArnaldo Carvalho de Melo 			found = n;
377923c42c1SMike Galbraith 			break;
378923c42c1SMike Galbraith 		}
379ab81f3fdSArnaldo Carvalho de Melo 		next = rb_next(&n->rb_node);
380923c42c1SMike Galbraith 	}
381923c42c1SMike Galbraith 
382923c42c1SMike Galbraith 	if (!found) {
38366aeb6d5SKirill Smelkov 		fprintf(stderr, "Sorry, %s is not active.\n", buf);
384923c42c1SMike Galbraith 		sleep(1);
385923c42c1SMike Galbraith 		return;
386923c42c1SMike Galbraith 	} else
387923c42c1SMike Galbraith 		parse_source(found);
388923c42c1SMike Galbraith 
389923c42c1SMike Galbraith out_free:
390923c42c1SMike Galbraith 	free(buf);
391923c42c1SMike Galbraith }
392923c42c1SMike Galbraith 
393091bd2e9SMike Galbraith static void print_mapped_keys(void)
394923c42c1SMike Galbraith {
395091bd2e9SMike Galbraith 	char *name = NULL;
396091bd2e9SMike Galbraith 
397c97cf422SArnaldo Carvalho de Melo 	if (top.sym_filter_entry) {
398ab81f3fdSArnaldo Carvalho de Melo 		struct symbol *sym = top.sym_filter_entry->ms.sym;
399091bd2e9SMike Galbraith 		name = sym->name;
400091bd2e9SMike Galbraith 	}
401091bd2e9SMike Galbraith 
402091bd2e9SMike Galbraith 	fprintf(stdout, "\nMapped keys:\n");
4038c3e10ebSArnaldo Carvalho de Melo 	fprintf(stdout, "\t[d]     display refresh delay.             \t(%d)\n", top.delay_secs);
4048c3e10ebSArnaldo Carvalho de Melo 	fprintf(stdout, "\t[e]     display entries (lines).           \t(%d)\n", top.print_entries);
405091bd2e9SMike Galbraith 
4068c3e10ebSArnaldo Carvalho de Melo 	if (top.evlist->nr_entries > 1)
4078c3e10ebSArnaldo Carvalho de Melo 		fprintf(stdout, "\t[E]     active event counter.              \t(%s)\n", event_name(top.sym_evsel));
408091bd2e9SMike Galbraith 
4098c3e10ebSArnaldo Carvalho de Melo 	fprintf(stdout, "\t[f]     profile display filter (count).    \t(%d)\n", top.count_filter);
410091bd2e9SMike Galbraith 
411091bd2e9SMike Galbraith 	fprintf(stdout, "\t[F]     annotate display filter (percent). \t(%d%%)\n", sym_pcnt_filter);
412091bd2e9SMike Galbraith 	fprintf(stdout, "\t[s]     annotate symbol.                   \t(%s)\n", name?: "NULL");
413091bd2e9SMike Galbraith 	fprintf(stdout, "\t[S]     stop annotation.\n");
414091bd2e9SMike Galbraith 
4158ffcda17SArnaldo Carvalho de Melo 	fprintf(stdout,
4168ffcda17SArnaldo Carvalho de Melo 		"\t[K]     hide kernel_symbols symbols.     \t(%s)\n",
4178c3e10ebSArnaldo Carvalho de Melo 		top.hide_kernel_symbols ? "yes" : "no");
4188ffcda17SArnaldo Carvalho de Melo 	fprintf(stdout,
4198ffcda17SArnaldo Carvalho de Melo 		"\t[U]     hide user symbols.               \t(%s)\n",
4208c3e10ebSArnaldo Carvalho de Melo 		top.hide_user_symbols ? "yes" : "no");
4218c3e10ebSArnaldo Carvalho de Melo 	fprintf(stdout, "\t[z]     toggle sample zeroing.             \t(%d)\n", top.zero ? 1 : 0);
422091bd2e9SMike Galbraith 	fprintf(stdout, "\t[qQ]    quit.\n");
423091bd2e9SMike Galbraith }
424091bd2e9SMike Galbraith 
425091bd2e9SMike Galbraith static int key_mapped(int c)
426091bd2e9SMike Galbraith {
427091bd2e9SMike Galbraith 	switch (c) {
428091bd2e9SMike Galbraith 		case 'd':
429091bd2e9SMike Galbraith 		case 'e':
430091bd2e9SMike Galbraith 		case 'f':
431091bd2e9SMike Galbraith 		case 'z':
432091bd2e9SMike Galbraith 		case 'q':
433091bd2e9SMike Galbraith 		case 'Q':
4348ffcda17SArnaldo Carvalho de Melo 		case 'K':
4358ffcda17SArnaldo Carvalho de Melo 		case 'U':
4366cff0e8dSKirill Smelkov 		case 'F':
4376cff0e8dSKirill Smelkov 		case 's':
4386cff0e8dSKirill Smelkov 		case 'S':
439091bd2e9SMike Galbraith 			return 1;
440091bd2e9SMike Galbraith 		case 'E':
441*b2b7e9ebSArnaldo Carvalho de Melo 			return top.evlist->nr_entries > 1 ? 1 : 0;
44283a0944fSIngo Molnar 		default:
44383a0944fSIngo Molnar 			break;
444091bd2e9SMike Galbraith 	}
445091bd2e9SMike Galbraith 
446091bd2e9SMike Galbraith 	return 0;
447923c42c1SMike Galbraith }
448923c42c1SMike Galbraith 
449dcc101d1SArnaldo Carvalho de Melo static void handle_keypress(int c)
450923c42c1SMike Galbraith {
451091bd2e9SMike Galbraith 	if (!key_mapped(c)) {
452091bd2e9SMike Galbraith 		struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
453091bd2e9SMike Galbraith 		struct termios tc, save;
454091bd2e9SMike Galbraith 
455091bd2e9SMike Galbraith 		print_mapped_keys();
456091bd2e9SMike Galbraith 		fprintf(stdout, "\nEnter selection, or unmapped key to continue: ");
457091bd2e9SMike Galbraith 		fflush(stdout);
458091bd2e9SMike Galbraith 
459091bd2e9SMike Galbraith 		tcgetattr(0, &save);
460091bd2e9SMike Galbraith 		tc = save;
461091bd2e9SMike Galbraith 		tc.c_lflag &= ~(ICANON | ECHO);
462091bd2e9SMike Galbraith 		tc.c_cc[VMIN] = 0;
463091bd2e9SMike Galbraith 		tc.c_cc[VTIME] = 0;
464091bd2e9SMike Galbraith 		tcsetattr(0, TCSANOW, &tc);
465091bd2e9SMike Galbraith 
466091bd2e9SMike Galbraith 		poll(&stdin_poll, 1, -1);
467091bd2e9SMike Galbraith 		c = getc(stdin);
468091bd2e9SMike Galbraith 
469091bd2e9SMike Galbraith 		tcsetattr(0, TCSAFLUSH, &save);
470091bd2e9SMike Galbraith 		if (!key_mapped(c))
471091bd2e9SMike Galbraith 			return;
472091bd2e9SMike Galbraith 	}
473091bd2e9SMike Galbraith 
474923c42c1SMike Galbraith 	switch (c) {
475923c42c1SMike Galbraith 		case 'd':
4768c3e10ebSArnaldo Carvalho de Melo 			prompt_integer(&top.delay_secs, "Enter display delay");
4778c3e10ebSArnaldo Carvalho de Melo 			if (top.delay_secs < 1)
4788c3e10ebSArnaldo Carvalho de Melo 				top.delay_secs = 1;
479923c42c1SMike Galbraith 			break;
480923c42c1SMike Galbraith 		case 'e':
4818c3e10ebSArnaldo Carvalho de Melo 			prompt_integer(&top.print_entries, "Enter display entries (lines)");
4828c3e10ebSArnaldo Carvalho de Melo 			if (top.print_entries == 0) {
48313cc5079SArnaldo Carvalho de Melo 				sig_winch_handler(SIGWINCH);
4843b6ed988SArnaldo Carvalho de Melo 				signal(SIGWINCH, sig_winch_handler);
4853b6ed988SArnaldo Carvalho de Melo 			} else
4863b6ed988SArnaldo Carvalho de Melo 				signal(SIGWINCH, SIG_DFL);
487923c42c1SMike Galbraith 			break;
488923c42c1SMike Galbraith 		case 'E':
4898c3e10ebSArnaldo Carvalho de Melo 			if (top.evlist->nr_entries > 1) {
490ce2d17caSAkihiro Nagai 				/* Select 0 as the default event: */
491ce2d17caSAkihiro Nagai 				int counter = 0;
492ce2d17caSAkihiro Nagai 
493923c42c1SMike Galbraith 				fprintf(stderr, "\nAvailable events:");
49469aad6f1SArnaldo Carvalho de Melo 
4958c3e10ebSArnaldo Carvalho de Melo 				list_for_each_entry(top.sym_evsel, &top.evlist->entries, node)
4968c3e10ebSArnaldo Carvalho de Melo 					fprintf(stderr, "\n\t%d %s", top.sym_evsel->idx, event_name(top.sym_evsel));
497923c42c1SMike Galbraith 
498ec52d976SArnaldo Carvalho de Melo 				prompt_integer(&counter, "Enter details event counter");
499923c42c1SMike Galbraith 
500ec52d976SArnaldo Carvalho de Melo 				if (counter >= top.evlist->nr_entries) {
5018c3e10ebSArnaldo Carvalho de Melo 					top.sym_evsel = list_entry(top.evlist->entries.next, struct perf_evsel, node);
5028c3e10ebSArnaldo Carvalho de Melo 					fprintf(stderr, "Sorry, no such event, using %s.\n", event_name(top.sym_evsel));
503923c42c1SMike Galbraith 					sleep(1);
50469aad6f1SArnaldo Carvalho de Melo 					break;
505923c42c1SMike Galbraith 				}
5068c3e10ebSArnaldo Carvalho de Melo 				list_for_each_entry(top.sym_evsel, &top.evlist->entries, node)
507ec52d976SArnaldo Carvalho de Melo 					if (top.sym_evsel->idx == counter)
50869aad6f1SArnaldo Carvalho de Melo 						break;
509ec52d976SArnaldo Carvalho de Melo 			} else
510ec52d976SArnaldo Carvalho de Melo 				top.sym_evsel = list_entry(top.evlist->entries.next, struct perf_evsel, node);
511923c42c1SMike Galbraith 			break;
512923c42c1SMike Galbraith 		case 'f':
5138c3e10ebSArnaldo Carvalho de Melo 			prompt_integer(&top.count_filter, "Enter display event count filter");
514923c42c1SMike Galbraith 			break;
515923c42c1SMike Galbraith 		case 'F':
516923c42c1SMike Galbraith 			prompt_percent(&sym_pcnt_filter, "Enter details display event filter (percent)");
517923c42c1SMike Galbraith 			break;
5188ffcda17SArnaldo Carvalho de Melo 		case 'K':
5198c3e10ebSArnaldo Carvalho de Melo 			top.hide_kernel_symbols = !top.hide_kernel_symbols;
5208ffcda17SArnaldo Carvalho de Melo 			break;
521923c42c1SMike Galbraith 		case 'q':
522923c42c1SMike Galbraith 		case 'Q':
523923c42c1SMike Galbraith 			printf("exiting.\n");
524c338aee8SArnaldo Carvalho de Melo 			if (dump_symtab)
525dcc101d1SArnaldo Carvalho de Melo 				perf_session__fprintf_dsos(top.session, stderr);
526923c42c1SMike Galbraith 			exit(0);
527923c42c1SMike Galbraith 		case 's':
528c97cf422SArnaldo Carvalho de Melo 			prompt_symbol(&top.sym_filter_entry, "Enter details symbol");
529923c42c1SMike Galbraith 			break;
530923c42c1SMike Galbraith 		case 'S':
531c97cf422SArnaldo Carvalho de Melo 			if (!top.sym_filter_entry)
532923c42c1SMike Galbraith 				break;
533923c42c1SMike Galbraith 			else {
534ab81f3fdSArnaldo Carvalho de Melo 				struct hist_entry *syme = top.sym_filter_entry;
535923c42c1SMike Galbraith 
536c97cf422SArnaldo Carvalho de Melo 				top.sym_filter_entry = NULL;
537923c42c1SMike Galbraith 				__zero_source_counters(syme);
538923c42c1SMike Galbraith 			}
539923c42c1SMike Galbraith 			break;
5408ffcda17SArnaldo Carvalho de Melo 		case 'U':
5418c3e10ebSArnaldo Carvalho de Melo 			top.hide_user_symbols = !top.hide_user_symbols;
5428ffcda17SArnaldo Carvalho de Melo 			break;
543923c42c1SMike Galbraith 		case 'z':
5448c3e10ebSArnaldo Carvalho de Melo 			top.zero = !top.zero;
545923c42c1SMike Galbraith 			break;
54683a0944fSIngo Molnar 		default:
54783a0944fSIngo Molnar 			break;
548923c42c1SMike Galbraith 	}
549923c42c1SMike Galbraith }
550923c42c1SMike Galbraith 
551ab81f3fdSArnaldo Carvalho de Melo static void perf_top__sort_new_samples(void *arg)
552ab81f3fdSArnaldo Carvalho de Melo {
553ab81f3fdSArnaldo Carvalho de Melo 	struct perf_top *t = arg;
554ab81f3fdSArnaldo Carvalho de Melo 	perf_top__reset_sample_counters(t);
555ab81f3fdSArnaldo Carvalho de Melo 
556ab81f3fdSArnaldo Carvalho de Melo 	if (t->evlist->selected != NULL)
557ab81f3fdSArnaldo Carvalho de Melo 		t->sym_evsel = t->evlist->selected;
558ab81f3fdSArnaldo Carvalho de Melo 
559ab81f3fdSArnaldo Carvalho de Melo 	hists__collapse_resort_threaded(&t->sym_evsel->hists);
560ab81f3fdSArnaldo Carvalho de Melo 	hists__output_resort_threaded(&t->sym_evsel->hists);
561b079d4e9SArnaldo Carvalho de Melo 	hists__decay_entries_threaded(&t->sym_evsel->hists,
562b079d4e9SArnaldo Carvalho de Melo 				      top.hide_user_symbols,
563b079d4e9SArnaldo Carvalho de Melo 				      top.hide_kernel_symbols);
564ab81f3fdSArnaldo Carvalho de Melo 	hists__output_recalc_col_len(&t->sym_evsel->hists, winsize.ws_row - 3);
565ab81f3fdSArnaldo Carvalho de Melo }
566ab81f3fdSArnaldo Carvalho de Melo 
567c0443df1SArnaldo Carvalho de Melo static void *display_thread_tui(void *arg __used)
568c0443df1SArnaldo Carvalho de Melo {
569ab81f3fdSArnaldo Carvalho de Melo 	const char *help = "For a higher level overview, try: perf top --sort comm,dso";
570ab81f3fdSArnaldo Carvalho de Melo 
571ab81f3fdSArnaldo Carvalho de Melo 	perf_top__sort_new_samples(&top);
572ab81f3fdSArnaldo Carvalho de Melo 	perf_evlist__tui_browse_hists(top.evlist, help,
573ab81f3fdSArnaldo Carvalho de Melo 				      perf_top__sort_new_samples,
574ab81f3fdSArnaldo Carvalho de Melo 				      &top, top.delay_secs);
575ab81f3fdSArnaldo Carvalho de Melo 
576c0443df1SArnaldo Carvalho de Melo 	exit_browser(0);
577c0443df1SArnaldo Carvalho de Melo 	exit(0);
578c0443df1SArnaldo Carvalho de Melo 	return NULL;
579c0443df1SArnaldo Carvalho de Melo }
580c0443df1SArnaldo Carvalho de Melo 
581f37a291cSIngo Molnar static void *display_thread(void *arg __used)
58286470930SIngo Molnar {
58386470930SIngo Molnar 	struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
584923c42c1SMike Galbraith 	struct termios tc, save;
585923c42c1SMike Galbraith 	int delay_msecs, c;
58686470930SIngo Molnar 
587923c42c1SMike Galbraith 	tcgetattr(0, &save);
588923c42c1SMike Galbraith 	tc = save;
589923c42c1SMike Galbraith 	tc.c_lflag &= ~(ICANON | ECHO);
590923c42c1SMike Galbraith 	tc.c_cc[VMIN] = 0;
591923c42c1SMike Galbraith 	tc.c_cc[VTIME] = 0;
592091bd2e9SMike Galbraith 
5933af6e338SArnaldo Carvalho de Melo 	pthread__unblock_sigwinch();
594923c42c1SMike Galbraith repeat:
5958c3e10ebSArnaldo Carvalho de Melo 	delay_msecs = top.delay_secs * 1000;
596923c42c1SMike Galbraith 	tcsetattr(0, TCSANOW, &tc);
597923c42c1SMike Galbraith 	/* trash return*/
598923c42c1SMike Galbraith 	getc(stdin);
59986470930SIngo Molnar 
6003af6e338SArnaldo Carvalho de Melo 	while (1) {
601dcc101d1SArnaldo Carvalho de Melo 		print_sym_table();
6023af6e338SArnaldo Carvalho de Melo 		/*
6033af6e338SArnaldo Carvalho de Melo 		 * Either timeout expired or we got an EINTR due to SIGWINCH,
6043af6e338SArnaldo Carvalho de Melo 		 * refresh screen in both cases.
6053af6e338SArnaldo Carvalho de Melo 		 */
6063af6e338SArnaldo Carvalho de Melo 		switch (poll(&stdin_poll, 1, delay_msecs)) {
6073af6e338SArnaldo Carvalho de Melo 		case 0:
6083af6e338SArnaldo Carvalho de Melo 			continue;
6093af6e338SArnaldo Carvalho de Melo 		case -1:
6103af6e338SArnaldo Carvalho de Melo 			if (errno == EINTR)
6113af6e338SArnaldo Carvalho de Melo 				continue;
6123af6e338SArnaldo Carvalho de Melo 			/* Fall trhu */
6133af6e338SArnaldo Carvalho de Melo 		default:
6143af6e338SArnaldo Carvalho de Melo 			goto process_hotkey;
6153af6e338SArnaldo Carvalho de Melo 		}
6163af6e338SArnaldo Carvalho de Melo 	}
6173af6e338SArnaldo Carvalho de Melo process_hotkey:
618923c42c1SMike Galbraith 	c = getc(stdin);
619923c42c1SMike Galbraith 	tcsetattr(0, TCSAFLUSH, &save);
620923c42c1SMike Galbraith 
621dcc101d1SArnaldo Carvalho de Melo 	handle_keypress(c);
622923c42c1SMike Galbraith 	goto repeat;
62386470930SIngo Molnar 
62486470930SIngo Molnar 	return NULL;
62586470930SIngo Molnar }
62686470930SIngo Molnar 
6272ab52083SAnton Blanchard /* Tag samples to be skipped. */
628f37a291cSIngo Molnar static const char *skip_symbols[] = {
6292ab52083SAnton Blanchard 	"default_idle",
630b0e8572fSArnaldo Carvalho de Melo 	"native_safe_halt",
6312ab52083SAnton Blanchard 	"cpu_idle",
6322ab52083SAnton Blanchard 	"enter_idle",
6332ab52083SAnton Blanchard 	"exit_idle",
6342ab52083SAnton Blanchard 	"mwait_idle",
63559b90056SArnaldo Carvalho de Melo 	"mwait_idle_with_hints",
6368357275bSArnaldo Carvalho de Melo 	"poll_idle",
6373a3393efSAnton Blanchard 	"ppc64_runlatch_off",
6383a3393efSAnton Blanchard 	"pseries_dedicated_idle_sleep",
6392ab52083SAnton Blanchard 	NULL
6402ab52083SAnton Blanchard };
6412ab52083SAnton Blanchard 
642ab81f3fdSArnaldo Carvalho de Melo static int symbol_filter(struct map *map __used, struct symbol *sym)
64386470930SIngo Molnar {
64486470930SIngo Molnar 	const char *name = sym->name;
6452ab52083SAnton Blanchard 	int i;
64686470930SIngo Molnar 
6473a3393efSAnton Blanchard 	/*
6483a3393efSAnton Blanchard 	 * ppc64 uses function descriptors and appends a '.' to the
6493a3393efSAnton Blanchard 	 * start of every instruction address. Remove it.
6503a3393efSAnton Blanchard 	 */
6513a3393efSAnton Blanchard 	if (name[0] == '.')
6523a3393efSAnton Blanchard 		name++;
6533a3393efSAnton Blanchard 
65486470930SIngo Molnar 	if (!strcmp(name, "_text") ||
65586470930SIngo Molnar 	    !strcmp(name, "_etext") ||
65686470930SIngo Molnar 	    !strcmp(name, "_sinittext") ||
65786470930SIngo Molnar 	    !strncmp("init_module", name, 11) ||
65886470930SIngo Molnar 	    !strncmp("cleanup_module", name, 14) ||
65986470930SIngo Molnar 	    strstr(name, "_text_start") ||
66086470930SIngo Molnar 	    strstr(name, "_text_end"))
66186470930SIngo Molnar 		return 1;
66286470930SIngo Molnar 
6632ab52083SAnton Blanchard 	for (i = 0; skip_symbols[i]; i++) {
6642ab52083SAnton Blanchard 		if (!strcmp(skip_symbols[i], name)) {
665171b3be9SArnaldo Carvalho de Melo 			sym->ignore = true;
6662ab52083SAnton Blanchard 			break;
6672ab52083SAnton Blanchard 		}
6682ab52083SAnton Blanchard 	}
66986470930SIngo Molnar 
67086470930SIngo Molnar 	return 0;
67186470930SIngo Molnar }
67286470930SIngo Molnar 
6738115d60cSArnaldo Carvalho de Melo static void perf_event__process_sample(const union perf_event *event,
6748d50e5b4SArnaldo Carvalho de Melo 				       struct perf_sample *sample,
67570db7533SArnaldo Carvalho de Melo 				       struct perf_session *session)
67686470930SIngo Molnar {
67719d4ac3cSArnaldo Carvalho de Melo 	struct symbol *parent = NULL;
6788115d60cSArnaldo Carvalho de Melo 	u64 ip = event->ip.ip;
6791ed091c4SArnaldo Carvalho de Melo 	struct addr_location al;
68023346f21SArnaldo Carvalho de Melo 	struct machine *machine;
68119d4ac3cSArnaldo Carvalho de Melo 	int err;
6828115d60cSArnaldo Carvalho de Melo 	u8 origin = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
68386470930SIngo Molnar 
6848c3e10ebSArnaldo Carvalho de Melo 	++top.samples;
68524bfef0fSArnaldo Carvalho de Melo 
6868ffcda17SArnaldo Carvalho de Melo 	switch (origin) {
6871ed091c4SArnaldo Carvalho de Melo 	case PERF_RECORD_MISC_USER:
6888c3e10ebSArnaldo Carvalho de Melo 		++top.us_samples;
6898c3e10ebSArnaldo Carvalho de Melo 		if (top.hide_user_symbols)
6908ffcda17SArnaldo Carvalho de Melo 			return;
69123346f21SArnaldo Carvalho de Melo 		machine = perf_session__find_host_machine(session);
6925b2bb75aSArnaldo Carvalho de Melo 		break;
6935b2bb75aSArnaldo Carvalho de Melo 	case PERF_RECORD_MISC_KERNEL:
6948c3e10ebSArnaldo Carvalho de Melo 		++top.kernel_samples;
6958c3e10ebSArnaldo Carvalho de Melo 		if (top.hide_kernel_symbols)
6968ffcda17SArnaldo Carvalho de Melo 			return;
69723346f21SArnaldo Carvalho de Melo 		machine = perf_session__find_host_machine(session);
6985b2bb75aSArnaldo Carvalho de Melo 		break;
699a1645ce1SZhang, Yanmin 	case PERF_RECORD_MISC_GUEST_KERNEL:
7008c3e10ebSArnaldo Carvalho de Melo 		++top.guest_kernel_samples;
7018115d60cSArnaldo Carvalho de Melo 		machine = perf_session__find_machine(session, event->ip.pid);
702a1645ce1SZhang, Yanmin 		break;
703a1645ce1SZhang, Yanmin 	case PERF_RECORD_MISC_GUEST_USER:
7048c3e10ebSArnaldo Carvalho de Melo 		++top.guest_us_samples;
705a1645ce1SZhang, Yanmin 		/*
706a1645ce1SZhang, Yanmin 		 * TODO: we don't process guest user from host side
707a1645ce1SZhang, Yanmin 		 * except simple counting.
708a1645ce1SZhang, Yanmin 		 */
709a1645ce1SZhang, Yanmin 		return;
7105b2bb75aSArnaldo Carvalho de Melo 	default:
7115b2bb75aSArnaldo Carvalho de Melo 		return;
7125b2bb75aSArnaldo Carvalho de Melo 	}
7135b2bb75aSArnaldo Carvalho de Melo 
71423346f21SArnaldo Carvalho de Melo 	if (!machine && perf_guest) {
715a1645ce1SZhang, Yanmin 		pr_err("Can't find guest [%d]'s kernel information\n",
7168115d60cSArnaldo Carvalho de Melo 			event->ip.pid);
717a1645ce1SZhang, Yanmin 		return;
718a1645ce1SZhang, Yanmin 	}
719a1645ce1SZhang, Yanmin 
7208115d60cSArnaldo Carvalho de Melo 	if (event->header.misc & PERF_RECORD_MISC_EXACT_IP)
7218c3e10ebSArnaldo Carvalho de Melo 		top.exact_samples++;
7221676b8a0SPeter Zijlstra 
7238115d60cSArnaldo Carvalho de Melo 	if (perf_event__preprocess_sample(event, session, &al, sample,
72441a37e20SArnaldo Carvalho de Melo 					  symbol_filter) < 0 ||
72572b8fa17SArnaldo Carvalho de Melo 	    al.filtered)
7261ed091c4SArnaldo Carvalho de Melo 		return;
72786470930SIngo Molnar 
7285f6f5580SArnaldo Carvalho de Melo 	if (!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);
7395f6f5580SArnaldo Carvalho de Melo 		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 		 */
755e4a338d0SArnaldo Carvalho de Melo 		if (!kptr_restrict_warned && !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);
768e4a338d0SArnaldo Carvalho de Melo 			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) {
77370db7533SArnaldo Carvalho de Melo 		struct perf_evsel *evsel;
774ab81f3fdSArnaldo Carvalho de Melo 		struct hist_entry *he;
77570db7533SArnaldo Carvalho de Melo 
7768c3e10ebSArnaldo Carvalho de Melo 		evsel = perf_evlist__id2evsel(top.evlist, sample->id);
77770db7533SArnaldo Carvalho de Melo 		assert(evsel != NULL);
778ab81f3fdSArnaldo Carvalho de Melo 
77919d4ac3cSArnaldo Carvalho de Melo 		if ((sort__has_parent || symbol_conf.use_callchain) &&
78019d4ac3cSArnaldo Carvalho de Melo 		    sample->callchain) {
78119d4ac3cSArnaldo Carvalho de Melo 			err = perf_session__resolve_callchain(session, al.thread,
78219d4ac3cSArnaldo Carvalho de Melo 							      sample->callchain, &parent);
78319d4ac3cSArnaldo Carvalho de Melo 			if (err)
78419d4ac3cSArnaldo Carvalho de Melo 				return;
78519d4ac3cSArnaldo Carvalho de Melo 		}
78619d4ac3cSArnaldo Carvalho de Melo 
787ab81f3fdSArnaldo Carvalho de Melo 		he = perf_session__add_hist_entry(session, &al, sample, evsel);
788ab81f3fdSArnaldo Carvalho de Melo 		if (he == NULL) {
789ab81f3fdSArnaldo Carvalho de Melo 			pr_err("Problem incrementing symbol period, skipping event\n");
790ab81f3fdSArnaldo Carvalho de Melo 			return;
7915807806aSArnaldo Carvalho de Melo 		}
792ab81f3fdSArnaldo Carvalho de Melo 
79319d4ac3cSArnaldo Carvalho de Melo 		if (symbol_conf.use_callchain) {
79419d4ac3cSArnaldo Carvalho de Melo 			err = callchain_append(he->callchain, &session->callchain_cursor,
79519d4ac3cSArnaldo Carvalho de Melo 					       sample->period);
79619d4ac3cSArnaldo Carvalho de Melo 			if (err)
79719d4ac3cSArnaldo Carvalho de Melo 				return;
79819d4ac3cSArnaldo Carvalho de Melo 		}
79919d4ac3cSArnaldo Carvalho de Melo 
80019d4ac3cSArnaldo Carvalho de Melo 		if (sort_has_symbols)
801ab81f3fdSArnaldo Carvalho de Melo 			record_precise_ip(he, evsel->idx, ip);
8025807806aSArnaldo Carvalho de Melo 	}
803ab81f3fdSArnaldo Carvalho de Melo 
804ab81f3fdSArnaldo Carvalho de Melo 	return;
80586470930SIngo Molnar }
80686470930SIngo Molnar 
807aece948fSArnaldo Carvalho de Melo static void perf_session__mmap_read_idx(struct perf_session *self, int idx)
80886470930SIngo Molnar {
8098d50e5b4SArnaldo Carvalho de Melo 	struct perf_sample sample;
8108115d60cSArnaldo Carvalho de Melo 	union perf_event *event;
8115538becaSFrederic Weisbecker 	int ret;
81286470930SIngo Molnar 
813aece948fSArnaldo Carvalho de Melo 	while ((event = perf_evlist__mmap_read(top.evlist, idx)) != NULL) {
8145538becaSFrederic Weisbecker 		ret = perf_session__parse_sample(self, 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 
8205b2bb75aSArnaldo Carvalho de Melo 		if (event->header.type == PERF_RECORD_SAMPLE)
8218115d60cSArnaldo Carvalho de Melo 			perf_event__process_sample(event, &sample, self);
8225b2bb75aSArnaldo Carvalho de Melo 		else
8238115d60cSArnaldo Carvalho de Melo 			perf_event__process(event, &sample, self);
82486470930SIngo Molnar 	}
82586470930SIngo Molnar }
82686470930SIngo Molnar 
827d8f66248SArnaldo Carvalho de Melo static void perf_session__mmap_read(struct perf_session *self)
8282f01190aSFrederic Weisbecker {
82970db7533SArnaldo Carvalho de Melo 	int i;
8302f01190aSFrederic Weisbecker 
831aece948fSArnaldo Carvalho de Melo 	for (i = 0; i < top.evlist->nr_mmaps; i++)
832aece948fSArnaldo Carvalho de Melo 		perf_session__mmap_read_idx(self, i);
8332f01190aSFrederic Weisbecker }
8342f01190aSFrederic Weisbecker 
83572cb7013SArnaldo Carvalho de Melo static void start_counters(struct perf_evlist *evlist)
83672cb7013SArnaldo Carvalho de Melo {
83772cb7013SArnaldo Carvalho de Melo 	struct perf_evsel *counter;
83872cb7013SArnaldo Carvalho de Melo 
83972cb7013SArnaldo Carvalho de Melo 	list_for_each_entry(counter, &evlist->entries, node) {
84072cb7013SArnaldo Carvalho de Melo 		struct perf_event_attr *attr = &counter->attr;
84172cb7013SArnaldo Carvalho de Melo 
84272cb7013SArnaldo Carvalho de Melo 		attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
84372cb7013SArnaldo Carvalho de Melo 
8448c3e10ebSArnaldo Carvalho de Melo 		if (top.freq) {
84572cb7013SArnaldo Carvalho de Melo 			attr->sample_type |= PERF_SAMPLE_PERIOD;
84672cb7013SArnaldo Carvalho de Melo 			attr->freq	  = 1;
8478c3e10ebSArnaldo Carvalho de Melo 			attr->sample_freq = top.freq;
84872cb7013SArnaldo Carvalho de Melo 		}
84972cb7013SArnaldo Carvalho de Melo 
85070db7533SArnaldo Carvalho de Melo 		if (evlist->nr_entries > 1) {
85170db7533SArnaldo Carvalho de Melo 			attr->sample_type |= PERF_SAMPLE_ID;
85270db7533SArnaldo Carvalho de Melo 			attr->read_format |= PERF_FORMAT_ID;
85370db7533SArnaldo Carvalho de Melo 		}
85470db7533SArnaldo Carvalho de Melo 
85519d4ac3cSArnaldo Carvalho de Melo 		if (symbol_conf.use_callchain)
85619d4ac3cSArnaldo Carvalho de Melo 			attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
85719d4ac3cSArnaldo Carvalho de Melo 
85872cb7013SArnaldo Carvalho de Melo 		attr->mmap = 1;
859ab81f3fdSArnaldo Carvalho de Melo 		attr->comm = 1;
8605d2cd909SArnaldo Carvalho de Melo 		attr->inherit = inherit;
86172cb7013SArnaldo Carvalho de Melo try_again:
8628c3e10ebSArnaldo Carvalho de Melo 		if (perf_evsel__open(counter, top.evlist->cpus,
8635d2cd909SArnaldo Carvalho de Melo 				     top.evlist->threads, group) < 0) {
86472cb7013SArnaldo Carvalho de Melo 			int err = errno;
86572cb7013SArnaldo Carvalho de Melo 
866c286c419SArnaldo Carvalho de Melo 			if (err == EPERM || err == EACCES) {
867c286c419SArnaldo Carvalho de Melo 				ui__warning_paranoid();
868c286c419SArnaldo Carvalho de Melo 				goto out_err;
869c286c419SArnaldo Carvalho de Melo 			}
87072cb7013SArnaldo Carvalho de Melo 			/*
87172cb7013SArnaldo Carvalho de Melo 			 * If it's cycles then fall back to hrtimer
87272cb7013SArnaldo Carvalho de Melo 			 * based cpu-clock-tick sw counter, which
87372cb7013SArnaldo Carvalho de Melo 			 * is always available even if no PMU support:
87472cb7013SArnaldo Carvalho de Melo 			 */
87572cb7013SArnaldo Carvalho de Melo 			if (attr->type == PERF_TYPE_HARDWARE &&
87672cb7013SArnaldo Carvalho de Melo 			    attr->config == PERF_COUNT_HW_CPU_CYCLES) {
87772cb7013SArnaldo Carvalho de Melo 				if (verbose)
878c286c419SArnaldo Carvalho de Melo 					ui__warning("Cycles event not supported,\n"
879c286c419SArnaldo Carvalho de Melo 						    "trying to fall back to cpu-clock-ticks\n");
88072cb7013SArnaldo Carvalho de Melo 
88172cb7013SArnaldo Carvalho de Melo 				attr->type = PERF_TYPE_SOFTWARE;
88272cb7013SArnaldo Carvalho de Melo 				attr->config = PERF_COUNT_SW_CPU_CLOCK;
88372cb7013SArnaldo Carvalho de Melo 				goto try_again;
88472cb7013SArnaldo Carvalho de Melo 			}
885c286c419SArnaldo Carvalho de Melo 
886ca6a4258SDavid Ahern 			if (err == ENOENT) {
887ca6a4258SDavid Ahern 				ui__warning("The %s event is not supported.\n",
888ca6a4258SDavid Ahern 					    event_name(counter));
889ca6a4258SDavid Ahern 				goto out_err;
890ca6a4258SDavid Ahern 			}
891ca6a4258SDavid Ahern 
892c286c419SArnaldo Carvalho de Melo 			ui__warning("The sys_perf_event_open() syscall "
893c286c419SArnaldo Carvalho de Melo 				    "returned with %d (%s).  /bin/dmesg "
894c286c419SArnaldo Carvalho de Melo 				    "may provide additional information.\n"
895c286c419SArnaldo Carvalho de Melo 				    "No CONFIG_PERF_EVENTS=y kernel support "
896c286c419SArnaldo Carvalho de Melo 				    "configured?\n", err, strerror(err));
897c286c419SArnaldo Carvalho de Melo 			goto out_err;
89872cb7013SArnaldo Carvalho de Melo 		}
89972cb7013SArnaldo Carvalho de Melo 	}
90070db7533SArnaldo Carvalho de Melo 
901c286c419SArnaldo Carvalho de Melo 	if (perf_evlist__mmap(evlist, mmap_pages, false) < 0) {
902c286c419SArnaldo Carvalho de Melo 		ui__warning("Failed to mmap with %d (%s)\n",
903c286c419SArnaldo Carvalho de Melo 			    errno, strerror(errno));
904c286c419SArnaldo Carvalho de Melo 		goto out_err;
905c286c419SArnaldo Carvalho de Melo 	}
906c286c419SArnaldo Carvalho de Melo 
907c286c419SArnaldo Carvalho de Melo 	return;
908c286c419SArnaldo Carvalho de Melo 
909c286c419SArnaldo Carvalho de Melo out_err:
910c286c419SArnaldo Carvalho de Melo 	exit_browser(0);
911c286c419SArnaldo Carvalho de Melo 	exit(0);
91272cb7013SArnaldo Carvalho de Melo }
91372cb7013SArnaldo Carvalho de Melo 
91419d4ac3cSArnaldo Carvalho de Melo static int setup_sample_type(void)
91519d4ac3cSArnaldo Carvalho de Melo {
91619d4ac3cSArnaldo Carvalho de Melo 	if (!sort_has_symbols) {
91719d4ac3cSArnaldo Carvalho de Melo 		if (symbol_conf.use_callchain) {
91819d4ac3cSArnaldo Carvalho de Melo 			ui__warning("Selected -g but \"sym\" not present in --sort/-s.");
91919d4ac3cSArnaldo Carvalho de Melo 			return -EINVAL;
92019d4ac3cSArnaldo Carvalho de Melo 		}
92119d4ac3cSArnaldo Carvalho de Melo 	} else if (!dont_use_callchains && callchain_param.mode != CHAIN_NONE) {
92219d4ac3cSArnaldo Carvalho de Melo 		if (callchain_register_param(&callchain_param) < 0) {
92319d4ac3cSArnaldo Carvalho de Melo 			ui__warning("Can't register callchain params.\n");
92419d4ac3cSArnaldo Carvalho de Melo 			return -EINVAL;
92519d4ac3cSArnaldo Carvalho de Melo 		}
92619d4ac3cSArnaldo Carvalho de Melo 	}
92719d4ac3cSArnaldo Carvalho de Melo 
92819d4ac3cSArnaldo Carvalho de Melo 	return 0;
92919d4ac3cSArnaldo Carvalho de Melo }
93019d4ac3cSArnaldo Carvalho de Melo 
931716c69feSIngo Molnar static int __cmd_top(void)
932716c69feSIngo Molnar {
933716c69feSIngo Molnar 	pthread_t thread;
93419d4ac3cSArnaldo Carvalho de Melo 	int ret;
935d8f66248SArnaldo Carvalho de Melo 	/*
936b3165f41SArnaldo Carvalho de Melo 	 * FIXME: perf_session__new should allow passing a O_MMAP, so that all this
937b3165f41SArnaldo Carvalho de Melo 	 * mmap reading, etc is encapsulated in it. Use O_WRONLY for now.
938d8f66248SArnaldo Carvalho de Melo 	 */
939dcc101d1SArnaldo Carvalho de Melo 	top.session = perf_session__new(NULL, O_WRONLY, false, false, NULL);
940dcc101d1SArnaldo Carvalho de Melo 	if (top.session == NULL)
941b3165f41SArnaldo Carvalho de Melo 		return -ENOMEM;
942716c69feSIngo Molnar 
94319d4ac3cSArnaldo Carvalho de Melo 	ret = setup_sample_type();
94419d4ac3cSArnaldo Carvalho de Melo 	if (ret)
94519d4ac3cSArnaldo Carvalho de Melo 		goto out_delete;
94619d4ac3cSArnaldo Carvalho de Melo 
9478c3e10ebSArnaldo Carvalho de Melo 	if (top.target_tid != -1)
9487c940c18SArnaldo Carvalho de Melo 		perf_event__synthesize_thread_map(top.evlist->threads,
949dcc101d1SArnaldo Carvalho de Melo 						  perf_event__process, top.session);
9505b2bb75aSArnaldo Carvalho de Melo 	else
951dcc101d1SArnaldo Carvalho de Melo 		perf_event__synthesize_threads(perf_event__process, top.session);
9525b2bb75aSArnaldo Carvalho de Melo 
9538c3e10ebSArnaldo Carvalho de Melo 	start_counters(top.evlist);
954dcc101d1SArnaldo Carvalho de Melo 	top.session->evlist = top.evlist;
955dcc101d1SArnaldo Carvalho de Melo 	perf_session__update_sample_type(top.session);
95686470930SIngo Molnar 
9572f01190aSFrederic Weisbecker 	/* Wait for a minimal set of events before starting the snapshot */
9588c3e10ebSArnaldo Carvalho de Melo 	poll(top.evlist->pollfd, top.evlist->nr_fds, 100);
9592f01190aSFrederic Weisbecker 
960dcc101d1SArnaldo Carvalho de Melo 	perf_session__mmap_read(top.session);
9612f01190aSFrederic Weisbecker 
962c0443df1SArnaldo Carvalho de Melo 	if (pthread_create(&thread, NULL, (use_browser > 0 ? display_thread_tui :
963dcc101d1SArnaldo Carvalho de Melo 							     display_thread), NULL)) {
96486470930SIngo Molnar 		printf("Could not create display thread.\n");
96586470930SIngo Molnar 		exit(-1);
96686470930SIngo Molnar 	}
96786470930SIngo Molnar 
96886470930SIngo Molnar 	if (realtime_prio) {
96986470930SIngo Molnar 		struct sched_param param;
97086470930SIngo Molnar 
97186470930SIngo Molnar 		param.sched_priority = realtime_prio;
97286470930SIngo Molnar 		if (sched_setscheduler(0, SCHED_FIFO, &param)) {
97386470930SIngo Molnar 			printf("Could not set realtime priority.\n");
97486470930SIngo Molnar 			exit(-1);
97586470930SIngo Molnar 		}
97686470930SIngo Molnar 	}
97786470930SIngo Molnar 
97886470930SIngo Molnar 	while (1) {
9798c3e10ebSArnaldo Carvalho de Melo 		u64 hits = top.samples;
98086470930SIngo Molnar 
981dcc101d1SArnaldo Carvalho de Melo 		perf_session__mmap_read(top.session);
98286470930SIngo Molnar 
9838c3e10ebSArnaldo Carvalho de Melo 		if (hits == top.samples)
9848c3e10ebSArnaldo Carvalho de Melo 			ret = poll(top.evlist->pollfd, top.evlist->nr_fds, 100);
98586470930SIngo Molnar 	}
98686470930SIngo Molnar 
98719d4ac3cSArnaldo Carvalho de Melo out_delete:
98819d4ac3cSArnaldo Carvalho de Melo 	perf_session__delete(top.session);
98919d4ac3cSArnaldo Carvalho de Melo 	top.session = NULL;
99019d4ac3cSArnaldo Carvalho de Melo 
99119d4ac3cSArnaldo Carvalho de Melo 	return 0;
99219d4ac3cSArnaldo Carvalho de Melo }
99319d4ac3cSArnaldo Carvalho de Melo 
99419d4ac3cSArnaldo Carvalho de Melo static int
99519d4ac3cSArnaldo Carvalho de Melo parse_callchain_opt(const struct option *opt __used, const char *arg,
99619d4ac3cSArnaldo Carvalho de Melo 		    int unset)
99719d4ac3cSArnaldo Carvalho de Melo {
99819d4ac3cSArnaldo Carvalho de Melo 	char *tok, *tok2;
99919d4ac3cSArnaldo Carvalho de Melo 	char *endptr;
100019d4ac3cSArnaldo Carvalho de Melo 
100119d4ac3cSArnaldo Carvalho de Melo 	/*
100219d4ac3cSArnaldo Carvalho de Melo 	 * --no-call-graph
100319d4ac3cSArnaldo Carvalho de Melo 	 */
100419d4ac3cSArnaldo Carvalho de Melo 	if (unset) {
100519d4ac3cSArnaldo Carvalho de Melo 		dont_use_callchains = true;
100619d4ac3cSArnaldo Carvalho de Melo 		return 0;
100719d4ac3cSArnaldo Carvalho de Melo 	}
100819d4ac3cSArnaldo Carvalho de Melo 
100919d4ac3cSArnaldo Carvalho de Melo 	symbol_conf.use_callchain = true;
101019d4ac3cSArnaldo Carvalho de Melo 
101119d4ac3cSArnaldo Carvalho de Melo 	if (!arg)
101219d4ac3cSArnaldo Carvalho de Melo 		return 0;
101319d4ac3cSArnaldo Carvalho de Melo 
101419d4ac3cSArnaldo Carvalho de Melo 	tok = strtok((char *)arg, ",");
101519d4ac3cSArnaldo Carvalho de Melo 	if (!tok)
101619d4ac3cSArnaldo Carvalho de Melo 		return -1;
101719d4ac3cSArnaldo Carvalho de Melo 
101819d4ac3cSArnaldo Carvalho de Melo 	/* get the output mode */
101919d4ac3cSArnaldo Carvalho de Melo 	if (!strncmp(tok, "graph", strlen(arg)))
102019d4ac3cSArnaldo Carvalho de Melo 		callchain_param.mode = CHAIN_GRAPH_ABS;
102119d4ac3cSArnaldo Carvalho de Melo 
102219d4ac3cSArnaldo Carvalho de Melo 	else if (!strncmp(tok, "flat", strlen(arg)))
102319d4ac3cSArnaldo Carvalho de Melo 		callchain_param.mode = CHAIN_FLAT;
102419d4ac3cSArnaldo Carvalho de Melo 
102519d4ac3cSArnaldo Carvalho de Melo 	else if (!strncmp(tok, "fractal", strlen(arg)))
102619d4ac3cSArnaldo Carvalho de Melo 		callchain_param.mode = CHAIN_GRAPH_REL;
102719d4ac3cSArnaldo Carvalho de Melo 
102819d4ac3cSArnaldo Carvalho de Melo 	else if (!strncmp(tok, "none", strlen(arg))) {
102919d4ac3cSArnaldo Carvalho de Melo 		callchain_param.mode = CHAIN_NONE;
103019d4ac3cSArnaldo Carvalho de Melo 		symbol_conf.use_callchain = false;
103119d4ac3cSArnaldo Carvalho de Melo 
103219d4ac3cSArnaldo Carvalho de Melo 		return 0;
103319d4ac3cSArnaldo Carvalho de Melo 	}
103419d4ac3cSArnaldo Carvalho de Melo 
103519d4ac3cSArnaldo Carvalho de Melo 	else
103619d4ac3cSArnaldo Carvalho de Melo 		return -1;
103719d4ac3cSArnaldo Carvalho de Melo 
103819d4ac3cSArnaldo Carvalho de Melo 	/* get the min percentage */
103919d4ac3cSArnaldo Carvalho de Melo 	tok = strtok(NULL, ",");
104019d4ac3cSArnaldo Carvalho de Melo 	if (!tok)
104119d4ac3cSArnaldo Carvalho de Melo 		goto setup;
104219d4ac3cSArnaldo Carvalho de Melo 
104319d4ac3cSArnaldo Carvalho de Melo 	callchain_param.min_percent = strtod(tok, &endptr);
104419d4ac3cSArnaldo Carvalho de Melo 	if (tok == endptr)
104519d4ac3cSArnaldo Carvalho de Melo 		return -1;
104619d4ac3cSArnaldo Carvalho de Melo 
104719d4ac3cSArnaldo Carvalho de Melo 	/* get the print limit */
104819d4ac3cSArnaldo Carvalho de Melo 	tok2 = strtok(NULL, ",");
104919d4ac3cSArnaldo Carvalho de Melo 	if (!tok2)
105019d4ac3cSArnaldo Carvalho de Melo 		goto setup;
105119d4ac3cSArnaldo Carvalho de Melo 
105219d4ac3cSArnaldo Carvalho de Melo 	if (tok2[0] != 'c') {
105319d4ac3cSArnaldo Carvalho de Melo 		callchain_param.print_limit = strtod(tok2, &endptr);
105419d4ac3cSArnaldo Carvalho de Melo 		tok2 = strtok(NULL, ",");
105519d4ac3cSArnaldo Carvalho de Melo 		if (!tok2)
105619d4ac3cSArnaldo Carvalho de Melo 			goto setup;
105719d4ac3cSArnaldo Carvalho de Melo 	}
105819d4ac3cSArnaldo Carvalho de Melo 
105919d4ac3cSArnaldo Carvalho de Melo 	/* get the call chain order */
106019d4ac3cSArnaldo Carvalho de Melo 	if (!strcmp(tok2, "caller"))
106119d4ac3cSArnaldo Carvalho de Melo 		callchain_param.order = ORDER_CALLER;
106219d4ac3cSArnaldo Carvalho de Melo 	else if (!strcmp(tok2, "callee"))
106319d4ac3cSArnaldo Carvalho de Melo 		callchain_param.order = ORDER_CALLEE;
106419d4ac3cSArnaldo Carvalho de Melo 	else
106519d4ac3cSArnaldo Carvalho de Melo 		return -1;
106619d4ac3cSArnaldo Carvalho de Melo setup:
106719d4ac3cSArnaldo Carvalho de Melo 	if (callchain_register_param(&callchain_param) < 0) {
106819d4ac3cSArnaldo Carvalho de Melo 		fprintf(stderr, "Can't register callchain params\n");
106919d4ac3cSArnaldo Carvalho de Melo 		return -1;
107019d4ac3cSArnaldo Carvalho de Melo 	}
107186470930SIngo Molnar 	return 0;
107286470930SIngo Molnar }
107386470930SIngo Molnar 
107486470930SIngo Molnar static const char * const top_usage[] = {
107586470930SIngo Molnar 	"perf top [<options>]",
107686470930SIngo Molnar 	NULL
107786470930SIngo Molnar };
107886470930SIngo Molnar 
107986470930SIngo Molnar static const struct option options[] = {
10808c3e10ebSArnaldo Carvalho de Melo 	OPT_CALLBACK('e', "event", &top.evlist, "event",
108186470930SIngo Molnar 		     "event selector. use 'perf list' to list available events",
1082f120f9d5SJiri Olsa 		     parse_events_option),
108386470930SIngo Molnar 	OPT_INTEGER('c', "count", &default_interval,
108486470930SIngo Molnar 		    "event period to sample"),
10858c3e10ebSArnaldo Carvalho de Melo 	OPT_INTEGER('p', "pid", &top.target_pid,
1086d6d901c2SZhang, Yanmin 		    "profile events on existing process id"),
10878c3e10ebSArnaldo Carvalho de Melo 	OPT_INTEGER('t', "tid", &top.target_tid,
1088d6d901c2SZhang, Yanmin 		    "profile events on existing thread id"),
108986470930SIngo Molnar 	OPT_BOOLEAN('a', "all-cpus", &system_wide,
109086470930SIngo Molnar 			    "system-wide collection from all CPUs"),
10918c3e10ebSArnaldo Carvalho de Melo 	OPT_STRING('C', "cpu", &top.cpu_list, "cpu",
1092c45c6ea2SStephane Eranian 		    "list of cpus to monitor"),
1093b32d133aSArnaldo Carvalho de Melo 	OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1094b32d133aSArnaldo Carvalho de Melo 		   "file", "vmlinux pathname"),
10958c3e10ebSArnaldo Carvalho de Melo 	OPT_BOOLEAN('K', "hide_kernel_symbols", &top.hide_kernel_symbols,
10968ffcda17SArnaldo Carvalho de Melo 		    "hide kernel symbols"),
10971967936dSArnaldo Carvalho de Melo 	OPT_UINTEGER('m', "mmap-pages", &mmap_pages, "number of mmap data pages"),
109886470930SIngo Molnar 	OPT_INTEGER('r', "realtime", &realtime_prio,
109986470930SIngo Molnar 		    "collect data with this RT SCHED_FIFO priority"),
11008c3e10ebSArnaldo Carvalho de Melo 	OPT_INTEGER('d', "delay", &top.delay_secs,
110186470930SIngo Molnar 		    "number of seconds to delay between refreshes"),
110286470930SIngo Molnar 	OPT_BOOLEAN('D', "dump-symtab", &dump_symtab,
110386470930SIngo Molnar 			    "dump the symbol table used for profiling"),
11048c3e10ebSArnaldo Carvalho de Melo 	OPT_INTEGER('f', "count-filter", &top.count_filter,
110586470930SIngo Molnar 		    "only display functions with more events than this"),
110686470930SIngo Molnar 	OPT_BOOLEAN('g', "group", &group,
110786470930SIngo Molnar 			    "put the counters into a counter group"),
11080fdc7e67SMike Galbraith 	OPT_BOOLEAN('i', "inherit", &inherit,
11090fdc7e67SMike Galbraith 		    "child tasks inherit counters"),
1110ab81f3fdSArnaldo Carvalho de Melo 	OPT_STRING(0, "sym-annotate", &sym_filter, "symbol name",
11116cff0e8dSKirill Smelkov 		    "symbol to annotate"),
11128c3e10ebSArnaldo Carvalho de Melo 	OPT_BOOLEAN('z', "zero", &top.zero,
111386470930SIngo Molnar 		    "zero history across updates"),
11148c3e10ebSArnaldo Carvalho de Melo 	OPT_INTEGER('F', "freq", &top.freq,
111586470930SIngo Molnar 		    "profile at this frequency"),
11168c3e10ebSArnaldo Carvalho de Melo 	OPT_INTEGER('E', "entries", &top.print_entries,
111786470930SIngo Molnar 		    "display this many functions"),
11188c3e10ebSArnaldo Carvalho de Melo 	OPT_BOOLEAN('U', "hide_user_symbols", &top.hide_user_symbols,
11198ffcda17SArnaldo Carvalho de Melo 		    "hide user symbols"),
1120c0443df1SArnaldo Carvalho de Melo 	OPT_BOOLEAN(0, "tui", &use_tui, "Use the TUI interface"),
1121c0443df1SArnaldo Carvalho de Melo 	OPT_BOOLEAN(0, "stdio", &use_stdio, "Use the stdio interface"),
1122c0555642SIan Munsie 	OPT_INCR('v', "verbose", &verbose,
11233da297a6SIngo Molnar 		    "be more verbose (show counter open errors, etc)"),
1124ab81f3fdSArnaldo Carvalho de Melo 	OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1125ab81f3fdSArnaldo Carvalho de Melo 		   "sort by key(s): pid, comm, dso, symbol, parent"),
1126ab81f3fdSArnaldo Carvalho de Melo 	OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
1127ab81f3fdSArnaldo Carvalho de Melo 		    "Show a column with the number of samples"),
112819d4ac3cSArnaldo Carvalho de Melo 	OPT_CALLBACK_DEFAULT('G', "call-graph", NULL, "output_type,min_percent, call_order",
112919d4ac3cSArnaldo Carvalho de Melo 		     "Display callchains using output_type (graph, flat, fractal, or none), min percent threshold and callchain order. "
113019d4ac3cSArnaldo Carvalho de Melo 		     "Default: fractal,0.5,callee", &parse_callchain_opt,
113119d4ac3cSArnaldo Carvalho de Melo 		     callchain_default_opt),
1132ab81f3fdSArnaldo Carvalho de Melo 	OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period,
1133ab81f3fdSArnaldo Carvalho de Melo 		    "Show a column with the sum of periods"),
1134ab81f3fdSArnaldo Carvalho de Melo 	OPT_STRING(0, "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
1135ab81f3fdSArnaldo Carvalho de Melo 		   "only consider symbols in these dsos"),
1136ab81f3fdSArnaldo Carvalho de Melo 	OPT_STRING(0, "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
1137ab81f3fdSArnaldo Carvalho de Melo 		   "only consider symbols in these comms"),
1138ab81f3fdSArnaldo Carvalho de Melo 	OPT_STRING(0, "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
1139ab81f3fdSArnaldo Carvalho de Melo 		   "only consider these symbols"),
114064c6f0c7SArnaldo Carvalho de Melo 	OPT_BOOLEAN(0, "source", &symbol_conf.annotate_src,
114164c6f0c7SArnaldo Carvalho de Melo 		    "Interleave source code with assembly code (default)"),
114264c6f0c7SArnaldo Carvalho de Melo 	OPT_BOOLEAN(0, "asm-raw", &symbol_conf.annotate_asm_raw,
114364c6f0c7SArnaldo Carvalho de Melo 		    "Display raw encoding of assembly instructions (default)"),
114464c6f0c7SArnaldo Carvalho de Melo 	OPT_STRING('M', "disassembler-style", &disassembler_style, "disassembler style",
114564c6f0c7SArnaldo Carvalho de Melo 		   "Specify disassembler style (e.g. -M intel for intel syntax)"),
114686470930SIngo Molnar 	OPT_END()
114786470930SIngo Molnar };
114886470930SIngo Molnar 
1149f37a291cSIngo Molnar int cmd_top(int argc, const char **argv, const char *prefix __used)
115086470930SIngo Molnar {
115169aad6f1SArnaldo Carvalho de Melo 	struct perf_evsel *pos;
115269aad6f1SArnaldo Carvalho de Melo 	int status = -ENOMEM;
115386470930SIngo Molnar 
11548c3e10ebSArnaldo Carvalho de Melo 	top.evlist = perf_evlist__new(NULL, NULL);
11558c3e10ebSArnaldo Carvalho de Melo 	if (top.evlist == NULL)
1156361c99a6SArnaldo Carvalho de Melo 		return -ENOMEM;
1157361c99a6SArnaldo Carvalho de Melo 
1158ab81f3fdSArnaldo Carvalho de Melo 	symbol_conf.exclude_other = false;
115986470930SIngo Molnar 
116086470930SIngo Molnar 	argc = parse_options(argc, argv, options, top_usage, 0);
116186470930SIngo Molnar 	if (argc)
116286470930SIngo Molnar 		usage_with_options(top_usage, options);
116386470930SIngo Molnar 
1164ab81f3fdSArnaldo Carvalho de Melo 	if (sort_order == default_sort_order)
1165ab81f3fdSArnaldo Carvalho de Melo 		sort_order = "dso,symbol";
1166ab81f3fdSArnaldo Carvalho de Melo 
1167ab81f3fdSArnaldo Carvalho de Melo 	setup_sorting(top_usage, options);
1168ab81f3fdSArnaldo Carvalho de Melo 
1169c0443df1SArnaldo Carvalho de Melo 	if (use_stdio)
1170c0443df1SArnaldo Carvalho de Melo 		use_browser = 0;
1171c0443df1SArnaldo Carvalho de Melo 	else if (use_tui)
1172c0443df1SArnaldo Carvalho de Melo 		use_browser = 1;
1173c0443df1SArnaldo Carvalho de Melo 
1174c0443df1SArnaldo Carvalho de Melo 	setup_browser(false);
1175c0443df1SArnaldo Carvalho de Melo 
117686470930SIngo Molnar 	/* CPU and PID are mutually exclusive */
11778c3e10ebSArnaldo Carvalho de Melo 	if (top.target_tid > 0 && top.cpu_list) {
117886470930SIngo Molnar 		printf("WARNING: PID switch overriding CPU\n");
117986470930SIngo Molnar 		sleep(1);
11808c3e10ebSArnaldo Carvalho de Melo 		top.cpu_list = NULL;
118186470930SIngo Molnar 	}
118286470930SIngo Molnar 
11838c3e10ebSArnaldo Carvalho de Melo 	if (top.target_pid != -1)
11848c3e10ebSArnaldo Carvalho de Melo 		top.target_tid = top.target_pid;
11857e2ed097SArnaldo Carvalho de Melo 
11868c3e10ebSArnaldo Carvalho de Melo 	if (perf_evlist__create_maps(top.evlist, top.target_pid,
11878c3e10ebSArnaldo Carvalho de Melo 				     top.target_tid, top.cpu_list) < 0)
11887e2ed097SArnaldo Carvalho de Melo 		usage_with_options(top_usage, options);
11897e2ed097SArnaldo Carvalho de Melo 
11908c3e10ebSArnaldo Carvalho de Melo 	if (!top.evlist->nr_entries &&
11918c3e10ebSArnaldo Carvalho de Melo 	    perf_evlist__add_default(top.evlist) < 0) {
119269aad6f1SArnaldo Carvalho de Melo 		pr_err("Not enough memory for event selector list\n");
119369aad6f1SArnaldo Carvalho de Melo 		return -ENOMEM;
119469aad6f1SArnaldo Carvalho de Melo 	}
11955a8e5a30SArnaldo Carvalho de Melo 
11968c3e10ebSArnaldo Carvalho de Melo 	if (top.delay_secs < 1)
11978c3e10ebSArnaldo Carvalho de Melo 		top.delay_secs = 1;
119886470930SIngo Molnar 
11997e4ff9e3SMike Galbraith 	/*
12007e4ff9e3SMike Galbraith 	 * User specified count overrides default frequency.
12017e4ff9e3SMike Galbraith 	 */
12027e4ff9e3SMike Galbraith 	if (default_interval)
12038c3e10ebSArnaldo Carvalho de Melo 		top.freq = 0;
12048c3e10ebSArnaldo Carvalho de Melo 	else if (top.freq) {
12058c3e10ebSArnaldo Carvalho de Melo 		default_interval = top.freq;
12067e4ff9e3SMike Galbraith 	} else {
12077e4ff9e3SMike Galbraith 		fprintf(stderr, "frequency and count are zero, aborting\n");
12087e4ff9e3SMike Galbraith 		exit(EXIT_FAILURE);
12097e4ff9e3SMike Galbraith 	}
12107e4ff9e3SMike Galbraith 
12118c3e10ebSArnaldo Carvalho de Melo 	list_for_each_entry(pos, &top.evlist->entries, node) {
12128c3e10ebSArnaldo Carvalho de Melo 		if (perf_evsel__alloc_fd(pos, top.evlist->cpus->nr,
12138c3e10ebSArnaldo Carvalho de Melo 					 top.evlist->threads->nr) < 0)
121469aad6f1SArnaldo Carvalho de Melo 			goto out_free_fd;
121569aad6f1SArnaldo Carvalho de Melo 		/*
121669aad6f1SArnaldo Carvalho de Melo 		 * Fill in the ones not specifically initialized via -c:
121769aad6f1SArnaldo Carvalho de Melo 		 */
121869aad6f1SArnaldo Carvalho de Melo 		if (pos->attr.sample_period)
121969aad6f1SArnaldo Carvalho de Melo 			continue;
122069aad6f1SArnaldo Carvalho de Melo 
122169aad6f1SArnaldo Carvalho de Melo 		pos->attr.sample_period = default_interval;
122269aad6f1SArnaldo Carvalho de Melo 	}
122369aad6f1SArnaldo Carvalho de Melo 
12248c3e10ebSArnaldo Carvalho de Melo 	if (perf_evlist__alloc_pollfd(top.evlist) < 0 ||
12258c3e10ebSArnaldo Carvalho de Melo 	    perf_evlist__alloc_mmap(top.evlist) < 0)
12265c581041SArnaldo Carvalho de Melo 		goto out_free_fd;
12275c581041SArnaldo Carvalho de Melo 
12288c3e10ebSArnaldo Carvalho de Melo 	top.sym_evsel = list_entry(top.evlist->entries.next, struct perf_evsel, node);
1229cc841580SArnaldo Carvalho de Melo 
1230ab81f3fdSArnaldo Carvalho de Melo 	symbol_conf.priv_size = sizeof(struct annotation);
123169aad6f1SArnaldo Carvalho de Melo 
123269aad6f1SArnaldo Carvalho de Melo 	symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL);
123369aad6f1SArnaldo Carvalho de Melo 	if (symbol__init() < 0)
123469aad6f1SArnaldo Carvalho de Melo 		return -1;
123569aad6f1SArnaldo Carvalho de Melo 
1236ab81f3fdSArnaldo Carvalho de Melo 	sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list, "dso", stdout);
1237ab81f3fdSArnaldo Carvalho de Melo 	sort_entry__setup_elide(&sort_comm, symbol_conf.comm_list, "comm", stdout);
1238ab81f3fdSArnaldo Carvalho de Melo 	sort_entry__setup_elide(&sort_sym, symbol_conf.sym_list, "symbol", stdout);
1239ab81f3fdSArnaldo Carvalho de Melo 
124019d4ac3cSArnaldo Carvalho de Melo 	/*
124119d4ac3cSArnaldo Carvalho de Melo 	 * Avoid annotation data structures overhead when symbols aren't on the
124219d4ac3cSArnaldo Carvalho de Melo 	 * sort list.
124319d4ac3cSArnaldo Carvalho de Melo 	 */
124419d4ac3cSArnaldo Carvalho de Melo 	sort_has_symbols = sort_sym.list.next != NULL;
124519d4ac3cSArnaldo Carvalho de Melo 
124613cc5079SArnaldo Carvalho de Melo 	get_term_dimensions(&winsize);
12478c3e10ebSArnaldo Carvalho de Melo 	if (top.print_entries == 0) {
124813cc5079SArnaldo Carvalho de Melo 		update_print_entries(&winsize);
12493b6ed988SArnaldo Carvalho de Melo 		signal(SIGWINCH, sig_winch_handler);
12503b6ed988SArnaldo Carvalho de Melo 	}
12513b6ed988SArnaldo Carvalho de Melo 
125269aad6f1SArnaldo Carvalho de Melo 	status = __cmd_top();
125369aad6f1SArnaldo Carvalho de Melo out_free_fd:
12548c3e10ebSArnaldo Carvalho de Melo 	perf_evlist__delete(top.evlist);
125569aad6f1SArnaldo Carvalho de Melo 
125669aad6f1SArnaldo Carvalho de Melo 	return status;
125786470930SIngo Molnar }
1258