xref: /openbmc/linux/tools/perf/builtin-top.c (revision dc79959a)
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>
886470930SIngo Molnar  *
986470930SIngo Molnar  * Improvements and fixes by:
1086470930SIngo Molnar  *
1186470930SIngo Molnar  *   Arjan van de Ven <arjan@linux.intel.com>
1286470930SIngo Molnar  *   Yanmin Zhang <yanmin.zhang@intel.com>
1386470930SIngo Molnar  *   Wu Fengguang <fengguang.wu@intel.com>
1486470930SIngo Molnar  *   Mike Galbraith <efault@gmx.de>
1586470930SIngo Molnar  *   Paul Mackerras <paulus@samba.org>
1686470930SIngo Molnar  *
1786470930SIngo Molnar  * Released under the GPL v2. (and only v2, not any later version)
1886470930SIngo Molnar  */
1986470930SIngo Molnar #include "builtin.h"
2086470930SIngo Molnar 
2186470930SIngo Molnar #include "perf.h"
2286470930SIngo Molnar 
2386470930SIngo Molnar #include "util/symbol.h"
2486470930SIngo Molnar #include "util/color.h"
2586470930SIngo Molnar #include "util/util.h"
2643cbcd8aSArnaldo Carvalho de Melo #include <linux/rbtree.h>
2786470930SIngo Molnar #include "util/parse-options.h"
2886470930SIngo Molnar #include "util/parse-events.h"
2986470930SIngo Molnar 
308f28827aSFrederic Weisbecker #include "util/debug.h"
318f28827aSFrederic Weisbecker 
3286470930SIngo Molnar #include <assert.h>
3386470930SIngo Molnar #include <fcntl.h>
3486470930SIngo Molnar 
3586470930SIngo Molnar #include <stdio.h>
36923c42c1SMike Galbraith #include <termios.h>
37923c42c1SMike Galbraith #include <unistd.h>
3886470930SIngo Molnar 
3986470930SIngo Molnar #include <errno.h>
4086470930SIngo Molnar #include <time.h>
4186470930SIngo Molnar #include <sched.h>
4286470930SIngo Molnar #include <pthread.h>
4386470930SIngo Molnar 
4486470930SIngo Molnar #include <sys/syscall.h>
4586470930SIngo Molnar #include <sys/ioctl.h>
4686470930SIngo Molnar #include <sys/poll.h>
4786470930SIngo Molnar #include <sys/prctl.h>
4886470930SIngo Molnar #include <sys/wait.h>
4986470930SIngo Molnar #include <sys/uio.h>
5086470930SIngo Molnar #include <sys/mman.h>
5186470930SIngo Molnar 
5286470930SIngo Molnar #include <linux/unistd.h>
5386470930SIngo Molnar #include <linux/types.h>
5486470930SIngo Molnar 
5586470930SIngo Molnar static int			fd[MAX_NR_CPUS][MAX_COUNTERS];
5686470930SIngo Molnar 
5786470930SIngo Molnar static int			system_wide			=  0;
5886470930SIngo Molnar 
5986470930SIngo Molnar static int			default_interval		= 100000;
6086470930SIngo Molnar 
61923c42c1SMike Galbraith static int			count_filter			=  5;
6286470930SIngo Molnar static int			print_entries			= 15;
6386470930SIngo Molnar 
6486470930SIngo Molnar static int			target_pid			= -1;
650fdc7e67SMike Galbraith static int			inherit				=  0;
6686470930SIngo Molnar static int			profile_cpu			= -1;
6786470930SIngo Molnar static int			nr_cpus				=  0;
6886470930SIngo Molnar static unsigned int		realtime_prio			=  0;
6986470930SIngo Molnar static int			group				=  0;
7086470930SIngo Molnar static unsigned int		page_size;
7186470930SIngo Molnar static unsigned int		mmap_pages			= 16;
7286470930SIngo Molnar static int			freq				=  0;
7386470930SIngo Molnar 
7486470930SIngo Molnar static int			delay_secs			=  2;
7586470930SIngo Molnar static int			zero;
7686470930SIngo Molnar static int			dump_symtab;
7786470930SIngo Molnar 
7886470930SIngo Molnar /*
79923c42c1SMike Galbraith  * Source
80923c42c1SMike Galbraith  */
81923c42c1SMike Galbraith 
82923c42c1SMike Galbraith struct source_line {
83923c42c1SMike Galbraith 	u64			eip;
84923c42c1SMike Galbraith 	unsigned long		count[MAX_COUNTERS];
85923c42c1SMike Galbraith 	char			*line;
86923c42c1SMike Galbraith 	struct source_line	*next;
87923c42c1SMike Galbraith };
88923c42c1SMike Galbraith 
89923c42c1SMike Galbraith static char			*sym_filter			=  NULL;
90923c42c1SMike Galbraith struct sym_entry		*sym_filter_entry		=  NULL;
91923c42c1SMike Galbraith static int			sym_pcnt_filter			=  5;
92923c42c1SMike Galbraith static int			sym_counter			=  0;
9346ab9764SMike Galbraith static int			display_weighted		= -1;
94923c42c1SMike Galbraith 
95923c42c1SMike Galbraith /*
9686470930SIngo Molnar  * Symbols
9786470930SIngo Molnar  */
9886470930SIngo Molnar 
999cffa8d5SPaul Mackerras static u64			min_ip;
1009cffa8d5SPaul Mackerras static u64			max_ip = -1ll;
10186470930SIngo Molnar 
10286470930SIngo Molnar struct sym_entry {
10386470930SIngo Molnar 	struct rb_node		rb_node;
10486470930SIngo Molnar 	struct list_head	node;
10586470930SIngo Molnar 	unsigned long		count[MAX_COUNTERS];
10686470930SIngo Molnar 	unsigned long		snap_count;
10786470930SIngo Molnar 	double			weight;
10886470930SIngo Molnar 	int			skip;
109923c42c1SMike Galbraith 	struct source_line	*source;
110923c42c1SMike Galbraith 	struct source_line	*lines;
111923c42c1SMike Galbraith 	struct source_line	**lines_tail;
112923c42c1SMike Galbraith 	pthread_mutex_t		source_lock;
11386470930SIngo Molnar };
11486470930SIngo Molnar 
115923c42c1SMike Galbraith /*
116923c42c1SMike Galbraith  * Source functions
117923c42c1SMike Galbraith  */
118923c42c1SMike Galbraith 
119923c42c1SMike Galbraith static void parse_source(struct sym_entry *syme)
120923c42c1SMike Galbraith {
121923c42c1SMike Galbraith 	struct symbol *sym;
122923c42c1SMike Galbraith 	struct module *module;
123923c42c1SMike Galbraith 	struct section *section = NULL;
124923c42c1SMike Galbraith 	FILE *file;
12583a0944fSIngo Molnar 	char command[PATH_MAX*2];
12683a0944fSIngo Molnar 	const char *path = vmlinux_name;
127923c42c1SMike Galbraith 	u64 start, end, len;
128923c42c1SMike Galbraith 
129923c42c1SMike Galbraith 	if (!syme)
130923c42c1SMike Galbraith 		return;
131923c42c1SMike Galbraith 
132923c42c1SMike Galbraith 	if (syme->lines) {
133923c42c1SMike Galbraith 		pthread_mutex_lock(&syme->source_lock);
134923c42c1SMike Galbraith 		goto out_assign;
135923c42c1SMike Galbraith 	}
136923c42c1SMike Galbraith 
137923c42c1SMike Galbraith 	sym = (struct symbol *)(syme + 1);
138923c42c1SMike Galbraith 	module = sym->module;
139923c42c1SMike Galbraith 
140923c42c1SMike Galbraith 	if (module)
141923c42c1SMike Galbraith 		path = module->path;
142923c42c1SMike Galbraith 	if (!path)
143923c42c1SMike Galbraith 		return;
144923c42c1SMike Galbraith 
145923c42c1SMike Galbraith 	start = sym->obj_start;
146923c42c1SMike Galbraith 	if (!start)
147923c42c1SMike Galbraith 		start = sym->start;
148923c42c1SMike Galbraith 
149923c42c1SMike Galbraith 	if (module) {
150923c42c1SMike Galbraith 		section = module->sections->find_section(module->sections, ".text");
151923c42c1SMike Galbraith 		if (section)
152923c42c1SMike Galbraith 			start -= section->vma;
153923c42c1SMike Galbraith 	}
154923c42c1SMike Galbraith 
155923c42c1SMike Galbraith 	end = start + sym->end - sym->start + 1;
156923c42c1SMike Galbraith 	len = sym->end - sym->start;
157923c42c1SMike Galbraith 
158923c42c1SMike Galbraith 	sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s", start, end, path);
159923c42c1SMike Galbraith 
160923c42c1SMike Galbraith 	file = popen(command, "r");
161923c42c1SMike Galbraith 	if (!file)
162923c42c1SMike Galbraith 		return;
163923c42c1SMike Galbraith 
164923c42c1SMike Galbraith 	pthread_mutex_lock(&syme->source_lock);
165923c42c1SMike Galbraith 	syme->lines_tail = &syme->lines;
166923c42c1SMike Galbraith 	while (!feof(file)) {
167923c42c1SMike Galbraith 		struct source_line *src;
168923c42c1SMike Galbraith 		size_t dummy = 0;
169923c42c1SMike Galbraith 		char *c;
170923c42c1SMike Galbraith 
171923c42c1SMike Galbraith 		src = malloc(sizeof(struct source_line));
172923c42c1SMike Galbraith 		assert(src != NULL);
173923c42c1SMike Galbraith 		memset(src, 0, sizeof(struct source_line));
174923c42c1SMike Galbraith 
175923c42c1SMike Galbraith 		if (getline(&src->line, &dummy, file) < 0)
176923c42c1SMike Galbraith 			break;
177923c42c1SMike Galbraith 		if (!src->line)
178923c42c1SMike Galbraith 			break;
179923c42c1SMike Galbraith 
180923c42c1SMike Galbraith 		c = strchr(src->line, '\n');
181923c42c1SMike Galbraith 		if (c)
182923c42c1SMike Galbraith 			*c = 0;
183923c42c1SMike Galbraith 
184923c42c1SMike Galbraith 		src->next = NULL;
185923c42c1SMike Galbraith 		*syme->lines_tail = src;
186923c42c1SMike Galbraith 		syme->lines_tail = &src->next;
187923c42c1SMike Galbraith 
188923c42c1SMike Galbraith 		if (strlen(src->line)>8 && src->line[8] == ':') {
189923c42c1SMike Galbraith 			src->eip = strtoull(src->line, NULL, 16);
190923c42c1SMike Galbraith 			if (section)
191923c42c1SMike Galbraith 				src->eip += section->vma;
192923c42c1SMike Galbraith 		}
193923c42c1SMike Galbraith 		if (strlen(src->line)>8 && src->line[16] == ':') {
194923c42c1SMike Galbraith 			src->eip = strtoull(src->line, NULL, 16);
195923c42c1SMike Galbraith 			if (section)
196923c42c1SMike Galbraith 				src->eip += section->vma;
197923c42c1SMike Galbraith 		}
198923c42c1SMike Galbraith 	}
199923c42c1SMike Galbraith 	pclose(file);
200923c42c1SMike Galbraith out_assign:
201923c42c1SMike Galbraith 	sym_filter_entry = syme;
202923c42c1SMike Galbraith 	pthread_mutex_unlock(&syme->source_lock);
203923c42c1SMike Galbraith }
204923c42c1SMike Galbraith 
205923c42c1SMike Galbraith static void __zero_source_counters(struct sym_entry *syme)
206923c42c1SMike Galbraith {
207923c42c1SMike Galbraith 	int i;
208923c42c1SMike Galbraith 	struct source_line *line;
209923c42c1SMike Galbraith 
210923c42c1SMike Galbraith 	line = syme->lines;
211923c42c1SMike Galbraith 	while (line) {
212923c42c1SMike Galbraith 		for (i = 0; i < nr_counters; i++)
213923c42c1SMike Galbraith 			line->count[i] = 0;
214923c42c1SMike Galbraith 		line = line->next;
215923c42c1SMike Galbraith 	}
216923c42c1SMike Galbraith }
217923c42c1SMike Galbraith 
218923c42c1SMike Galbraith static void record_precise_ip(struct sym_entry *syme, int counter, u64 ip)
219923c42c1SMike Galbraith {
220923c42c1SMike Galbraith 	struct source_line *line;
221923c42c1SMike Galbraith 
222923c42c1SMike Galbraith 	if (syme != sym_filter_entry)
223923c42c1SMike Galbraith 		return;
224923c42c1SMike Galbraith 
225923c42c1SMike Galbraith 	if (pthread_mutex_trylock(&syme->source_lock))
226923c42c1SMike Galbraith 		return;
227923c42c1SMike Galbraith 
228923c42c1SMike Galbraith 	if (!syme->source)
229923c42c1SMike Galbraith 		goto out_unlock;
230923c42c1SMike Galbraith 
231923c42c1SMike Galbraith 	for (line = syme->lines; line; line = line->next) {
232923c42c1SMike Galbraith 		if (line->eip == ip) {
233923c42c1SMike Galbraith 			line->count[counter]++;
234923c42c1SMike Galbraith 			break;
235923c42c1SMike Galbraith 		}
236923c42c1SMike Galbraith 		if (line->eip > ip)
237923c42c1SMike Galbraith 			break;
238923c42c1SMike Galbraith 	}
239923c42c1SMike Galbraith out_unlock:
240923c42c1SMike Galbraith 	pthread_mutex_unlock(&syme->source_lock);
241923c42c1SMike Galbraith }
242923c42c1SMike Galbraith 
243923c42c1SMike Galbraith static void lookup_sym_source(struct sym_entry *syme)
244923c42c1SMike Galbraith {
245923c42c1SMike Galbraith 	struct symbol *symbol = (struct symbol *)(syme + 1);
246923c42c1SMike Galbraith 	struct source_line *line;
247923c42c1SMike Galbraith 	char pattern[PATH_MAX];
248923c42c1SMike Galbraith 	char *idx;
249923c42c1SMike Galbraith 
250923c42c1SMike Galbraith 	sprintf(pattern, "<%s>:", symbol->name);
251923c42c1SMike Galbraith 
252923c42c1SMike Galbraith 	if (symbol->module) {
253923c42c1SMike Galbraith 		idx = strstr(pattern, "\t");
254923c42c1SMike Galbraith 		if (idx)
255923c42c1SMike Galbraith 			*idx = 0;
256923c42c1SMike Galbraith 	}
257923c42c1SMike Galbraith 
258923c42c1SMike Galbraith 	pthread_mutex_lock(&syme->source_lock);
259923c42c1SMike Galbraith 	for (line = syme->lines; line; line = line->next) {
260923c42c1SMike Galbraith 		if (strstr(line->line, pattern)) {
261923c42c1SMike Galbraith 			syme->source = line;
262923c42c1SMike Galbraith 			break;
263923c42c1SMike Galbraith 		}
264923c42c1SMike Galbraith 	}
265923c42c1SMike Galbraith 	pthread_mutex_unlock(&syme->source_lock);
266923c42c1SMike Galbraith }
267923c42c1SMike Galbraith 
268923c42c1SMike Galbraith static void show_lines(struct source_line *queue, int count, int total)
269923c42c1SMike Galbraith {
270923c42c1SMike Galbraith 	int i;
271923c42c1SMike Galbraith 	struct source_line *line;
272923c42c1SMike Galbraith 
273923c42c1SMike Galbraith 	line = queue;
274923c42c1SMike Galbraith 	for (i = 0; i < count; i++) {
275923c42c1SMike Galbraith 		float pcnt = 100.0*(float)line->count[sym_counter]/(float)total;
276923c42c1SMike Galbraith 
277923c42c1SMike Galbraith 		printf("%8li %4.1f%%\t%s\n", line->count[sym_counter], pcnt, line->line);
278923c42c1SMike Galbraith 		line = line->next;
279923c42c1SMike Galbraith 	}
280923c42c1SMike Galbraith }
281923c42c1SMike Galbraith 
282923c42c1SMike Galbraith #define TRACE_COUNT     3
283923c42c1SMike Galbraith 
284923c42c1SMike Galbraith static void show_details(struct sym_entry *syme)
285923c42c1SMike Galbraith {
286923c42c1SMike Galbraith 	struct symbol *symbol;
287923c42c1SMike Galbraith 	struct source_line *line;
288923c42c1SMike Galbraith 	struct source_line *line_queue = NULL;
289923c42c1SMike Galbraith 	int displayed = 0;
290923c42c1SMike Galbraith 	int line_queue_count = 0, total = 0, more = 0;
291923c42c1SMike Galbraith 
292923c42c1SMike Galbraith 	if (!syme)
293923c42c1SMike Galbraith 		return;
294923c42c1SMike Galbraith 
295923c42c1SMike Galbraith 	if (!syme->source)
296923c42c1SMike Galbraith 		lookup_sym_source(syme);
297923c42c1SMike Galbraith 
298923c42c1SMike Galbraith 	if (!syme->source)
299923c42c1SMike Galbraith 		return;
300923c42c1SMike Galbraith 
301923c42c1SMike Galbraith 	symbol = (struct symbol *)(syme + 1);
302923c42c1SMike Galbraith 	printf("Showing %s for %s\n", event_name(sym_counter), symbol->name);
303923c42c1SMike Galbraith 	printf("  Events  Pcnt (>=%d%%)\n", sym_pcnt_filter);
304923c42c1SMike Galbraith 
305923c42c1SMike Galbraith 	pthread_mutex_lock(&syme->source_lock);
306923c42c1SMike Galbraith 	line = syme->source;
307923c42c1SMike Galbraith 	while (line) {
308923c42c1SMike Galbraith 		total += line->count[sym_counter];
309923c42c1SMike Galbraith 		line = line->next;
310923c42c1SMike Galbraith 	}
311923c42c1SMike Galbraith 
312923c42c1SMike Galbraith 	line = syme->source;
313923c42c1SMike Galbraith 	while (line) {
314923c42c1SMike Galbraith 		float pcnt = 0.0;
315923c42c1SMike Galbraith 
316923c42c1SMike Galbraith 		if (!line_queue_count)
317923c42c1SMike Galbraith 			line_queue = line;
318923c42c1SMike Galbraith 		line_queue_count++;
319923c42c1SMike Galbraith 
320923c42c1SMike Galbraith 		if (line->count[sym_counter])
321923c42c1SMike Galbraith 			pcnt = 100.0 * line->count[sym_counter] / (float)total;
322923c42c1SMike Galbraith 		if (pcnt >= (float)sym_pcnt_filter) {
323923c42c1SMike Galbraith 			if (displayed <= print_entries)
324923c42c1SMike Galbraith 				show_lines(line_queue, line_queue_count, total);
325923c42c1SMike Galbraith 			else more++;
326923c42c1SMike Galbraith 			displayed += line_queue_count;
327923c42c1SMike Galbraith 			line_queue_count = 0;
328923c42c1SMike Galbraith 			line_queue = NULL;
329923c42c1SMike Galbraith 		} else if (line_queue_count > TRACE_COUNT) {
330923c42c1SMike Galbraith 			line_queue = line_queue->next;
331923c42c1SMike Galbraith 			line_queue_count--;
332923c42c1SMike Galbraith 		}
333923c42c1SMike Galbraith 
334923c42c1SMike Galbraith 		line->count[sym_counter] = zero ? 0 : line->count[sym_counter] * 7 / 8;
335923c42c1SMike Galbraith 		line = line->next;
336923c42c1SMike Galbraith 	}
337923c42c1SMike Galbraith 	pthread_mutex_unlock(&syme->source_lock);
338923c42c1SMike Galbraith 	if (more)
339923c42c1SMike Galbraith 		printf("%d lines not displayed, maybe increase display entries [e]\n", more);
340923c42c1SMike Galbraith }
34186470930SIngo Molnar 
34286470930SIngo Molnar /*
34386470930SIngo Molnar  * Symbols will be added here in record_ip and will get out
34486470930SIngo Molnar  * after decayed.
34586470930SIngo Molnar  */
34686470930SIngo Molnar static LIST_HEAD(active_symbols);
34786470930SIngo Molnar static pthread_mutex_t active_symbols_lock = PTHREAD_MUTEX_INITIALIZER;
34886470930SIngo Molnar 
34986470930SIngo Molnar /*
35086470930SIngo Molnar  * Ordering weight: count-1 * count-2 * ... / count-n
35186470930SIngo Molnar  */
35286470930SIngo Molnar static double sym_weight(const struct sym_entry *sym)
35386470930SIngo Molnar {
35486470930SIngo Molnar 	double weight = sym->snap_count;
35586470930SIngo Molnar 	int counter;
35686470930SIngo Molnar 
35746ab9764SMike Galbraith 	if (!display_weighted)
35846ab9764SMike Galbraith 		return weight;
35946ab9764SMike Galbraith 
36086470930SIngo Molnar 	for (counter = 1; counter < nr_counters-1; counter++)
36186470930SIngo Molnar 		weight *= sym->count[counter];
36286470930SIngo Molnar 
36386470930SIngo Molnar 	weight /= (sym->count[counter] + 1);
36486470930SIngo Molnar 
36586470930SIngo Molnar 	return weight;
36686470930SIngo Molnar }
36786470930SIngo Molnar 
36886470930SIngo Molnar static long			samples;
36986470930SIngo Molnar static long			userspace_samples;
37086470930SIngo Molnar static const char		CONSOLE_CLEAR[] = "";
37186470930SIngo Molnar 
37286470930SIngo Molnar static void __list_insert_active_sym(struct sym_entry *syme)
37386470930SIngo Molnar {
37486470930SIngo Molnar 	list_add(&syme->node, &active_symbols);
37586470930SIngo Molnar }
37686470930SIngo Molnar 
37786470930SIngo Molnar static void list_remove_active_sym(struct sym_entry *syme)
37886470930SIngo Molnar {
37986470930SIngo Molnar 	pthread_mutex_lock(&active_symbols_lock);
38086470930SIngo Molnar 	list_del_init(&syme->node);
38186470930SIngo Molnar 	pthread_mutex_unlock(&active_symbols_lock);
38286470930SIngo Molnar }
38386470930SIngo Molnar 
38486470930SIngo Molnar static void rb_insert_active_sym(struct rb_root *tree, struct sym_entry *se)
38586470930SIngo Molnar {
38686470930SIngo Molnar 	struct rb_node **p = &tree->rb_node;
38786470930SIngo Molnar 	struct rb_node *parent = NULL;
38886470930SIngo Molnar 	struct sym_entry *iter;
38986470930SIngo Molnar 
39086470930SIngo Molnar 	while (*p != NULL) {
39186470930SIngo Molnar 		parent = *p;
39286470930SIngo Molnar 		iter = rb_entry(parent, struct sym_entry, rb_node);
39386470930SIngo Molnar 
39486470930SIngo Molnar 		if (se->weight > iter->weight)
39586470930SIngo Molnar 			p = &(*p)->rb_left;
39686470930SIngo Molnar 		else
39786470930SIngo Molnar 			p = &(*p)->rb_right;
39886470930SIngo Molnar 	}
39986470930SIngo Molnar 
40086470930SIngo Molnar 	rb_link_node(&se->rb_node, parent, p);
40186470930SIngo Molnar 	rb_insert_color(&se->rb_node, tree);
40286470930SIngo Molnar }
40386470930SIngo Molnar 
40486470930SIngo Molnar static void print_sym_table(void)
40586470930SIngo Molnar {
40686470930SIngo Molnar 	int printed = 0, j;
40746ab9764SMike Galbraith 	int counter, snap = !display_weighted ? sym_counter : 0;
40886470930SIngo Molnar 	float samples_per_sec = samples/delay_secs;
40986470930SIngo Molnar 	float ksamples_per_sec = (samples-userspace_samples)/delay_secs;
41086470930SIngo Molnar 	float sum_ksamples = 0.0;
41186470930SIngo Molnar 	struct sym_entry *syme, *n;
41286470930SIngo Molnar 	struct rb_root tmp = RB_ROOT;
41386470930SIngo Molnar 	struct rb_node *nd;
41486470930SIngo Molnar 
41586470930SIngo Molnar 	samples = userspace_samples = 0;
41686470930SIngo Molnar 
41786470930SIngo Molnar 	/* Sort the active symbols */
41886470930SIngo Molnar 	pthread_mutex_lock(&active_symbols_lock);
41986470930SIngo Molnar 	syme = list_entry(active_symbols.next, struct sym_entry, node);
42086470930SIngo Molnar 	pthread_mutex_unlock(&active_symbols_lock);
42186470930SIngo Molnar 
42286470930SIngo Molnar 	list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
42346ab9764SMike Galbraith 		syme->snap_count = syme->count[snap];
42486470930SIngo Molnar 		if (syme->snap_count != 0) {
42586470930SIngo Molnar 			syme->weight = sym_weight(syme);
42686470930SIngo Molnar 			rb_insert_active_sym(&tmp, syme);
42786470930SIngo Molnar 			sum_ksamples += syme->snap_count;
42886470930SIngo Molnar 
42986470930SIngo Molnar 			for (j = 0; j < nr_counters; j++)
43086470930SIngo Molnar 				syme->count[j] = zero ? 0 : syme->count[j] * 7 / 8;
43186470930SIngo Molnar 		} else
43286470930SIngo Molnar 			list_remove_active_sym(syme);
43386470930SIngo Molnar 	}
43486470930SIngo Molnar 
43586470930SIngo Molnar 	puts(CONSOLE_CLEAR);
43686470930SIngo Molnar 
43786470930SIngo Molnar 	printf(
43886470930SIngo Molnar "------------------------------------------------------------------------------\n");
43986470930SIngo Molnar 	printf( "   PerfTop:%8.0f irqs/sec  kernel:%4.1f%% [",
44086470930SIngo Molnar 		samples_per_sec,
44186470930SIngo Molnar 		100.0 - (100.0*((samples_per_sec-ksamples_per_sec)/samples_per_sec)));
44286470930SIngo Molnar 
44346ab9764SMike Galbraith 	if (nr_counters == 1 || !display_weighted) {
4449cffa8d5SPaul Mackerras 		printf("%Ld", (u64)attrs[0].sample_period);
44586470930SIngo Molnar 		if (freq)
44686470930SIngo Molnar 			printf("Hz ");
44786470930SIngo Molnar 		else
44886470930SIngo Molnar 			printf(" ");
44986470930SIngo Molnar 	}
45086470930SIngo Molnar 
45146ab9764SMike Galbraith 	if (!display_weighted)
45246ab9764SMike Galbraith 		printf("%s", event_name(sym_counter));
45346ab9764SMike Galbraith 	else for (counter = 0; counter < nr_counters; counter++) {
45486470930SIngo Molnar 		if (counter)
45586470930SIngo Molnar 			printf("/");
45686470930SIngo Molnar 
45786470930SIngo Molnar 		printf("%s", event_name(counter));
45886470930SIngo Molnar 	}
45986470930SIngo Molnar 
46086470930SIngo Molnar 	printf( "], ");
46186470930SIngo Molnar 
46286470930SIngo Molnar 	if (target_pid != -1)
46386470930SIngo Molnar 		printf(" (target_pid: %d", target_pid);
46486470930SIngo Molnar 	else
46586470930SIngo Molnar 		printf(" (all");
46686470930SIngo Molnar 
46786470930SIngo Molnar 	if (profile_cpu != -1)
46886470930SIngo Molnar 		printf(", cpu: %d)\n", profile_cpu);
46986470930SIngo Molnar 	else {
47086470930SIngo Molnar 		if (target_pid != -1)
47186470930SIngo Molnar 			printf(")\n");
47286470930SIngo Molnar 		else
47386470930SIngo Molnar 			printf(", %d CPUs)\n", nr_cpus);
47486470930SIngo Molnar 	}
47586470930SIngo Molnar 
47686470930SIngo Molnar 	printf("------------------------------------------------------------------------------\n\n");
47786470930SIngo Molnar 
478923c42c1SMike Galbraith 	if (sym_filter_entry) {
479923c42c1SMike Galbraith 		show_details(sym_filter_entry);
480923c42c1SMike Galbraith 		return;
481923c42c1SMike Galbraith 	}
482923c42c1SMike Galbraith 
48386470930SIngo Molnar 	if (nr_counters == 1)
48486470930SIngo Molnar 		printf("             samples    pcnt");
48586470930SIngo Molnar 	else
48686470930SIngo Molnar 		printf("   weight    samples    pcnt");
48786470930SIngo Molnar 
4887ced156bSArnaldo Carvalho de Melo 	if (verbose)
4897ced156bSArnaldo Carvalho de Melo 		printf("         RIP       ");
4907ced156bSArnaldo Carvalho de Melo 	printf("   kernel function\n");
4917ced156bSArnaldo Carvalho de Melo 	printf("   %s    _______   _____",
4927ced156bSArnaldo Carvalho de Melo 	       nr_counters == 1 ? "      " : "______");
4937ced156bSArnaldo Carvalho de Melo 	if (verbose)
4947ced156bSArnaldo Carvalho de Melo 		printf("   ________________");
4957ced156bSArnaldo Carvalho de Melo 	printf("   _______________\n\n");
49686470930SIngo Molnar 
49786470930SIngo Molnar 	for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
49883a0944fSIngo Molnar 		struct symbol *sym;
49986470930SIngo Molnar 		double pcnt;
50086470930SIngo Molnar 
50183a0944fSIngo Molnar 		syme = rb_entry(nd, struct sym_entry, rb_node);
50283a0944fSIngo Molnar 		sym = (struct symbol *)(syme + 1);
50383a0944fSIngo Molnar 
504923c42c1SMike Galbraith 		if (++printed > print_entries || (int)syme->snap_count < count_filter)
50586470930SIngo Molnar 			continue;
50686470930SIngo Molnar 
50786470930SIngo Molnar 		pcnt = 100.0 - (100.0 * ((sum_ksamples - syme->snap_count) /
50886470930SIngo Molnar 					 sum_ksamples));
50986470930SIngo Molnar 
51046ab9764SMike Galbraith 		if (nr_counters == 1 || !display_weighted)
51186470930SIngo Molnar 			printf("%20.2f - ", syme->weight);
51286470930SIngo Molnar 		else
51386470930SIngo Molnar 			printf("%9.1f %10ld - ", syme->weight, syme->snap_count);
51486470930SIngo Molnar 
5151e11fd82SFrederic Weisbecker 		percent_color_fprintf(stdout, "%4.1f%%", pcnt);
5167ced156bSArnaldo Carvalho de Melo 		if (verbose)
5177ced156bSArnaldo Carvalho de Melo 			printf(" - %016llx", sym->start);
5187ced156bSArnaldo Carvalho de Melo 		printf(" : %s", sym->name);
51942976487SMike Galbraith 		if (sym->module)
52042976487SMike Galbraith 			printf("\t[%s]", sym->module->name);
52142976487SMike Galbraith 		printf("\n");
52286470930SIngo Molnar 	}
52386470930SIngo Molnar }
52486470930SIngo Molnar 
525923c42c1SMike Galbraith static void prompt_integer(int *target, const char *msg)
526923c42c1SMike Galbraith {
527923c42c1SMike Galbraith 	char *buf = malloc(0), *p;
528923c42c1SMike Galbraith 	size_t dummy = 0;
529923c42c1SMike Galbraith 	int tmp;
530923c42c1SMike Galbraith 
531923c42c1SMike Galbraith 	fprintf(stdout, "\n%s: ", msg);
532923c42c1SMike Galbraith 	if (getline(&buf, &dummy, stdin) < 0)
533923c42c1SMike Galbraith 		return;
534923c42c1SMike Galbraith 
535923c42c1SMike Galbraith 	p = strchr(buf, '\n');
536923c42c1SMike Galbraith 	if (p)
537923c42c1SMike Galbraith 		*p = 0;
538923c42c1SMike Galbraith 
539923c42c1SMike Galbraith 	p = buf;
540923c42c1SMike Galbraith 	while(*p) {
541923c42c1SMike Galbraith 		if (!isdigit(*p))
542923c42c1SMike Galbraith 			goto out_free;
543923c42c1SMike Galbraith 		p++;
544923c42c1SMike Galbraith 	}
545923c42c1SMike Galbraith 	tmp = strtoul(buf, NULL, 10);
546923c42c1SMike Galbraith 	*target = tmp;
547923c42c1SMike Galbraith out_free:
548923c42c1SMike Galbraith 	free(buf);
549923c42c1SMike Galbraith }
550923c42c1SMike Galbraith 
551923c42c1SMike Galbraith static void prompt_percent(int *target, const char *msg)
552923c42c1SMike Galbraith {
553923c42c1SMike Galbraith 	int tmp = 0;
554923c42c1SMike Galbraith 
555923c42c1SMike Galbraith 	prompt_integer(&tmp, msg);
556923c42c1SMike Galbraith 	if (tmp >= 0 && tmp <= 100)
557923c42c1SMike Galbraith 		*target = tmp;
558923c42c1SMike Galbraith }
559923c42c1SMike Galbraith 
560923c42c1SMike Galbraith static void prompt_symbol(struct sym_entry **target, const char *msg)
561923c42c1SMike Galbraith {
562923c42c1SMike Galbraith 	char *buf = malloc(0), *p;
563923c42c1SMike Galbraith 	struct sym_entry *syme = *target, *n, *found = NULL;
564923c42c1SMike Galbraith 	size_t dummy = 0;
565923c42c1SMike Galbraith 
566923c42c1SMike Galbraith 	/* zero counters of active symbol */
567923c42c1SMike Galbraith 	if (syme) {
568923c42c1SMike Galbraith 		pthread_mutex_lock(&syme->source_lock);
569923c42c1SMike Galbraith 		__zero_source_counters(syme);
570923c42c1SMike Galbraith 		*target = NULL;
571923c42c1SMike Galbraith 		pthread_mutex_unlock(&syme->source_lock);
572923c42c1SMike Galbraith 	}
573923c42c1SMike Galbraith 
574923c42c1SMike Galbraith 	fprintf(stdout, "\n%s: ", msg);
575923c42c1SMike Galbraith 	if (getline(&buf, &dummy, stdin) < 0)
576923c42c1SMike Galbraith 		goto out_free;
577923c42c1SMike Galbraith 
578923c42c1SMike Galbraith 	p = strchr(buf, '\n');
579923c42c1SMike Galbraith 	if (p)
580923c42c1SMike Galbraith 		*p = 0;
581923c42c1SMike Galbraith 
582923c42c1SMike Galbraith 	pthread_mutex_lock(&active_symbols_lock);
583923c42c1SMike Galbraith 	syme = list_entry(active_symbols.next, struct sym_entry, node);
584923c42c1SMike Galbraith 	pthread_mutex_unlock(&active_symbols_lock);
585923c42c1SMike Galbraith 
586923c42c1SMike Galbraith 	list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
587923c42c1SMike Galbraith 		struct symbol *sym = (struct symbol *)(syme + 1);
588923c42c1SMike Galbraith 
589923c42c1SMike Galbraith 		if (!strcmp(buf, sym->name)) {
590923c42c1SMike Galbraith 			found = syme;
591923c42c1SMike Galbraith 			break;
592923c42c1SMike Galbraith 		}
593923c42c1SMike Galbraith 	}
594923c42c1SMike Galbraith 
595923c42c1SMike Galbraith 	if (!found) {
596923c42c1SMike Galbraith 		fprintf(stderr, "Sorry, %s is not active.\n", sym_filter);
597923c42c1SMike Galbraith 		sleep(1);
598923c42c1SMike Galbraith 		return;
599923c42c1SMike Galbraith 	} else
600923c42c1SMike Galbraith 		parse_source(found);
601923c42c1SMike Galbraith 
602923c42c1SMike Galbraith out_free:
603923c42c1SMike Galbraith 	free(buf);
604923c42c1SMike Galbraith }
605923c42c1SMike Galbraith 
606091bd2e9SMike Galbraith static void print_mapped_keys(void)
607923c42c1SMike Galbraith {
608091bd2e9SMike Galbraith 	char *name = NULL;
609091bd2e9SMike Galbraith 
610091bd2e9SMike Galbraith 	if (sym_filter_entry) {
611091bd2e9SMike Galbraith 		struct symbol *sym = (struct symbol *)(sym_filter_entry+1);
612091bd2e9SMike Galbraith 		name = sym->name;
613091bd2e9SMike Galbraith 	}
614091bd2e9SMike Galbraith 
615091bd2e9SMike Galbraith 	fprintf(stdout, "\nMapped keys:\n");
616091bd2e9SMike Galbraith 	fprintf(stdout, "\t[d]     display refresh delay.             \t(%d)\n", delay_secs);
617091bd2e9SMike Galbraith 	fprintf(stdout, "\t[e]     display entries (lines).           \t(%d)\n", print_entries);
618091bd2e9SMike Galbraith 
619091bd2e9SMike Galbraith 	if (nr_counters > 1)
62046ab9764SMike Galbraith 		fprintf(stdout, "\t[E]     active event counter.              \t(%s)\n", event_name(sym_counter));
621091bd2e9SMike Galbraith 
622091bd2e9SMike Galbraith 	fprintf(stdout, "\t[f]     profile display filter (count).    \t(%d)\n", count_filter);
623091bd2e9SMike Galbraith 
62483a0944fSIngo Molnar 	if (vmlinux_name) {
625091bd2e9SMike Galbraith 		fprintf(stdout, "\t[F]     annotate display filter (percent). \t(%d%%)\n", sym_pcnt_filter);
626091bd2e9SMike Galbraith 		fprintf(stdout, "\t[s]     annotate symbol.                   \t(%s)\n", name?: "NULL");
627091bd2e9SMike Galbraith 		fprintf(stdout, "\t[S]     stop annotation.\n");
628091bd2e9SMike Galbraith 	}
629091bd2e9SMike Galbraith 
630091bd2e9SMike Galbraith 	if (nr_counters > 1)
63146ab9764SMike Galbraith 		fprintf(stdout, "\t[w]     toggle display weighted/count[E]r. \t(%d)\n", display_weighted ? 1 : 0);
632091bd2e9SMike Galbraith 
63346ab9764SMike Galbraith 	fprintf(stdout, "\t[z]     toggle sample zeroing.             \t(%d)\n", zero ? 1 : 0);
634091bd2e9SMike Galbraith 	fprintf(stdout, "\t[qQ]    quit.\n");
635091bd2e9SMike Galbraith }
636091bd2e9SMike Galbraith 
637091bd2e9SMike Galbraith static int key_mapped(int c)
638091bd2e9SMike Galbraith {
639091bd2e9SMike Galbraith 	switch (c) {
640091bd2e9SMike Galbraith 		case 'd':
641091bd2e9SMike Galbraith 		case 'e':
642091bd2e9SMike Galbraith 		case 'f':
643091bd2e9SMike Galbraith 		case 'z':
644091bd2e9SMike Galbraith 		case 'q':
645091bd2e9SMike Galbraith 		case 'Q':
646091bd2e9SMike Galbraith 			return 1;
647091bd2e9SMike Galbraith 		case 'E':
648091bd2e9SMike Galbraith 		case 'w':
649091bd2e9SMike Galbraith 			return nr_counters > 1 ? 1 : 0;
650091bd2e9SMike Galbraith 		case 'F':
651091bd2e9SMike Galbraith 		case 's':
652091bd2e9SMike Galbraith 		case 'S':
65383a0944fSIngo Molnar 			return vmlinux_name ? 1 : 0;
65483a0944fSIngo Molnar 		default:
65583a0944fSIngo Molnar 			break;
656091bd2e9SMike Galbraith 	}
657091bd2e9SMike Galbraith 
658091bd2e9SMike Galbraith 	return 0;
659923c42c1SMike Galbraith }
660923c42c1SMike Galbraith 
661923c42c1SMike Galbraith static void handle_keypress(int c)
662923c42c1SMike Galbraith {
663091bd2e9SMike Galbraith 	if (!key_mapped(c)) {
664091bd2e9SMike Galbraith 		struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
665091bd2e9SMike Galbraith 		struct termios tc, save;
666091bd2e9SMike Galbraith 
667091bd2e9SMike Galbraith 		print_mapped_keys();
668091bd2e9SMike Galbraith 		fprintf(stdout, "\nEnter selection, or unmapped key to continue: ");
669091bd2e9SMike Galbraith 		fflush(stdout);
670091bd2e9SMike Galbraith 
671091bd2e9SMike Galbraith 		tcgetattr(0, &save);
672091bd2e9SMike Galbraith 		tc = save;
673091bd2e9SMike Galbraith 		tc.c_lflag &= ~(ICANON | ECHO);
674091bd2e9SMike Galbraith 		tc.c_cc[VMIN] = 0;
675091bd2e9SMike Galbraith 		tc.c_cc[VTIME] = 0;
676091bd2e9SMike Galbraith 		tcsetattr(0, TCSANOW, &tc);
677091bd2e9SMike Galbraith 
678091bd2e9SMike Galbraith 		poll(&stdin_poll, 1, -1);
679091bd2e9SMike Galbraith 		c = getc(stdin);
680091bd2e9SMike Galbraith 
681091bd2e9SMike Galbraith 		tcsetattr(0, TCSAFLUSH, &save);
682091bd2e9SMike Galbraith 		if (!key_mapped(c))
683091bd2e9SMike Galbraith 			return;
684091bd2e9SMike Galbraith 	}
685091bd2e9SMike Galbraith 
686923c42c1SMike Galbraith 	switch (c) {
687923c42c1SMike Galbraith 		case 'd':
688923c42c1SMike Galbraith 			prompt_integer(&delay_secs, "Enter display delay");
689dc79959aSTim Blechmann 			if (delay_secs < 1)
690dc79959aSTim Blechmann 				delay_secs = 1;
691923c42c1SMike Galbraith 			break;
692923c42c1SMike Galbraith 		case 'e':
693923c42c1SMike Galbraith 			prompt_integer(&print_entries, "Enter display entries (lines)");
694923c42c1SMike Galbraith 			break;
695923c42c1SMike Galbraith 		case 'E':
696923c42c1SMike Galbraith 			if (nr_counters > 1) {
697923c42c1SMike Galbraith 				int i;
698923c42c1SMike Galbraith 
699923c42c1SMike Galbraith 				fprintf(stderr, "\nAvailable events:");
700923c42c1SMike Galbraith 				for (i = 0; i < nr_counters; i++)
701923c42c1SMike Galbraith 					fprintf(stderr, "\n\t%d %s", i, event_name(i));
702923c42c1SMike Galbraith 
703923c42c1SMike Galbraith 				prompt_integer(&sym_counter, "Enter details event counter");
704923c42c1SMike Galbraith 
705923c42c1SMike Galbraith 				if (sym_counter >= nr_counters) {
706923c42c1SMike Galbraith 					fprintf(stderr, "Sorry, no such event, using %s.\n", event_name(0));
707923c42c1SMike Galbraith 					sym_counter = 0;
708923c42c1SMike Galbraith 					sleep(1);
709923c42c1SMike Galbraith 				}
710923c42c1SMike Galbraith 			} else sym_counter = 0;
711923c42c1SMike Galbraith 			break;
712923c42c1SMike Galbraith 		case 'f':
713923c42c1SMike Galbraith 			prompt_integer(&count_filter, "Enter display event count filter");
714923c42c1SMike Galbraith 			break;
715923c42c1SMike Galbraith 		case 'F':
716923c42c1SMike Galbraith 			prompt_percent(&sym_pcnt_filter, "Enter details display event filter (percent)");
717923c42c1SMike Galbraith 			break;
718923c42c1SMike Galbraith 		case 'q':
719923c42c1SMike Galbraith 		case 'Q':
720923c42c1SMike Galbraith 			printf("exiting.\n");
721923c42c1SMike Galbraith 			exit(0);
722923c42c1SMike Galbraith 		case 's':
723923c42c1SMike Galbraith 			prompt_symbol(&sym_filter_entry, "Enter details symbol");
724923c42c1SMike Galbraith 			break;
725923c42c1SMike Galbraith 		case 'S':
726923c42c1SMike Galbraith 			if (!sym_filter_entry)
727923c42c1SMike Galbraith 				break;
728923c42c1SMike Galbraith 			else {
729923c42c1SMike Galbraith 				struct sym_entry *syme = sym_filter_entry;
730923c42c1SMike Galbraith 
731923c42c1SMike Galbraith 				pthread_mutex_lock(&syme->source_lock);
732923c42c1SMike Galbraith 				sym_filter_entry = NULL;
733923c42c1SMike Galbraith 				__zero_source_counters(syme);
734923c42c1SMike Galbraith 				pthread_mutex_unlock(&syme->source_lock);
735923c42c1SMike Galbraith 			}
736923c42c1SMike Galbraith 			break;
73746ab9764SMike Galbraith 		case 'w':
73846ab9764SMike Galbraith 			display_weighted = ~display_weighted;
73946ab9764SMike Galbraith 			break;
740923c42c1SMike Galbraith 		case 'z':
741923c42c1SMike Galbraith 			zero = ~zero;
742923c42c1SMike Galbraith 			break;
74383a0944fSIngo Molnar 		default:
74483a0944fSIngo Molnar 			break;
745923c42c1SMike Galbraith 	}
746923c42c1SMike Galbraith }
747923c42c1SMike Galbraith 
748f37a291cSIngo Molnar static void *display_thread(void *arg __used)
74986470930SIngo Molnar {
75086470930SIngo Molnar 	struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
751923c42c1SMike Galbraith 	struct termios tc, save;
752923c42c1SMike Galbraith 	int delay_msecs, c;
75386470930SIngo Molnar 
754923c42c1SMike Galbraith 	tcgetattr(0, &save);
755923c42c1SMike Galbraith 	tc = save;
756923c42c1SMike Galbraith 	tc.c_lflag &= ~(ICANON | ECHO);
757923c42c1SMike Galbraith 	tc.c_cc[VMIN] = 0;
758923c42c1SMike Galbraith 	tc.c_cc[VTIME] = 0;
759091bd2e9SMike Galbraith 
760923c42c1SMike Galbraith repeat:
761923c42c1SMike Galbraith 	delay_msecs = delay_secs * 1000;
762923c42c1SMike Galbraith 	tcsetattr(0, TCSANOW, &tc);
763923c42c1SMike Galbraith 	/* trash return*/
764923c42c1SMike Galbraith 	getc(stdin);
76586470930SIngo Molnar 
76686470930SIngo Molnar 	do {
76786470930SIngo Molnar 		print_sym_table();
76886470930SIngo Molnar 	} while (!poll(&stdin_poll, 1, delay_msecs) == 1);
76986470930SIngo Molnar 
770923c42c1SMike Galbraith 	c = getc(stdin);
771923c42c1SMike Galbraith 	tcsetattr(0, TCSAFLUSH, &save);
772923c42c1SMike Galbraith 
773923c42c1SMike Galbraith 	handle_keypress(c);
774923c42c1SMike Galbraith 	goto repeat;
77586470930SIngo Molnar 
77686470930SIngo Molnar 	return NULL;
77786470930SIngo Molnar }
77886470930SIngo Molnar 
7792ab52083SAnton Blanchard /* Tag samples to be skipped. */
780f37a291cSIngo Molnar static const char *skip_symbols[] = {
7812ab52083SAnton Blanchard 	"default_idle",
7822ab52083SAnton Blanchard 	"cpu_idle",
7832ab52083SAnton Blanchard 	"enter_idle",
7842ab52083SAnton Blanchard 	"exit_idle",
7852ab52083SAnton Blanchard 	"mwait_idle",
78659b90056SArnaldo Carvalho de Melo 	"mwait_idle_with_hints",
7878357275bSArnaldo Carvalho de Melo 	"poll_idle",
7883a3393efSAnton Blanchard 	"ppc64_runlatch_off",
7893a3393efSAnton Blanchard 	"pseries_dedicated_idle_sleep",
7902ab52083SAnton Blanchard 	NULL
7912ab52083SAnton Blanchard };
7922ab52083SAnton Blanchard 
79386470930SIngo Molnar static int symbol_filter(struct dso *self, struct symbol *sym)
79486470930SIngo Molnar {
79586470930SIngo Molnar 	struct sym_entry *syme;
79686470930SIngo Molnar 	const char *name = sym->name;
7972ab52083SAnton Blanchard 	int i;
79886470930SIngo Molnar 
7993a3393efSAnton Blanchard 	/*
8003a3393efSAnton Blanchard 	 * ppc64 uses function descriptors and appends a '.' to the
8013a3393efSAnton Blanchard 	 * start of every instruction address. Remove it.
8023a3393efSAnton Blanchard 	 */
8033a3393efSAnton Blanchard 	if (name[0] == '.')
8043a3393efSAnton Blanchard 		name++;
8053a3393efSAnton Blanchard 
80686470930SIngo Molnar 	if (!strcmp(name, "_text") ||
80786470930SIngo Molnar 	    !strcmp(name, "_etext") ||
80886470930SIngo Molnar 	    !strcmp(name, "_sinittext") ||
80986470930SIngo Molnar 	    !strncmp("init_module", name, 11) ||
81086470930SIngo Molnar 	    !strncmp("cleanup_module", name, 14) ||
81186470930SIngo Molnar 	    strstr(name, "_text_start") ||
81286470930SIngo Molnar 	    strstr(name, "_text_end"))
81386470930SIngo Molnar 		return 1;
81486470930SIngo Molnar 
81586470930SIngo Molnar 	syme = dso__sym_priv(self, sym);
816923c42c1SMike Galbraith 	pthread_mutex_init(&syme->source_lock, NULL);
817923c42c1SMike Galbraith 	if (!sym_filter_entry && sym_filter && !strcmp(name, sym_filter))
818923c42c1SMike Galbraith 		sym_filter_entry = syme;
819923c42c1SMike Galbraith 
8202ab52083SAnton Blanchard 	for (i = 0; skip_symbols[i]; i++) {
8212ab52083SAnton Blanchard 		if (!strcmp(skip_symbols[i], name)) {
82286470930SIngo Molnar 			syme->skip = 1;
8232ab52083SAnton Blanchard 			break;
8242ab52083SAnton Blanchard 		}
8252ab52083SAnton Blanchard 	}
82686470930SIngo Molnar 
82786470930SIngo Molnar 	return 0;
82886470930SIngo Molnar }
82986470930SIngo Molnar 
83086470930SIngo Molnar static int parse_symbols(void)
83186470930SIngo Molnar {
83286470930SIngo Molnar 	struct rb_node *node;
83386470930SIngo Molnar 	struct symbol  *sym;
83483a0944fSIngo Molnar 	int use_modules = vmlinux_name ? 1 : 0;
83586470930SIngo Molnar 
83686470930SIngo Molnar 	kernel_dso = dso__new("[kernel]", sizeof(struct sym_entry));
83786470930SIngo Molnar 	if (kernel_dso == NULL)
83886470930SIngo Molnar 		return -1;
83986470930SIngo Molnar 
84083a0944fSIngo Molnar 	if (dso__load_kernel(kernel_dso, vmlinux_name, symbol_filter, verbose, use_modules) <= 0)
84186470930SIngo Molnar 		goto out_delete_dso;
84286470930SIngo Molnar 
84386470930SIngo Molnar 	node = rb_first(&kernel_dso->syms);
84486470930SIngo Molnar 	sym = rb_entry(node, struct symbol, rb_node);
84586470930SIngo Molnar 	min_ip = sym->start;
84686470930SIngo Molnar 
84786470930SIngo Molnar 	node = rb_last(&kernel_dso->syms);
84886470930SIngo Molnar 	sym = rb_entry(node, struct symbol, rb_node);
84986470930SIngo Molnar 	max_ip = sym->end;
85086470930SIngo Molnar 
85186470930SIngo Molnar 	if (dump_symtab)
85286470930SIngo Molnar 		dso__fprintf(kernel_dso, stderr);
85386470930SIngo Molnar 
85486470930SIngo Molnar 	return 0;
85586470930SIngo Molnar 
85686470930SIngo Molnar out_delete_dso:
85786470930SIngo Molnar 	dso__delete(kernel_dso);
85886470930SIngo Molnar 	kernel_dso = NULL;
85986470930SIngo Molnar 	return -1;
86086470930SIngo Molnar }
86186470930SIngo Molnar 
86286470930SIngo Molnar /*
86386470930SIngo Molnar  * Binary search in the histogram table and record the hit:
86486470930SIngo Molnar  */
8659cffa8d5SPaul Mackerras static void record_ip(u64 ip, int counter)
86686470930SIngo Molnar {
86786470930SIngo Molnar 	struct symbol *sym = dso__find_symbol(kernel_dso, ip);
86886470930SIngo Molnar 
86986470930SIngo Molnar 	if (sym != NULL) {
87086470930SIngo Molnar 		struct sym_entry *syme = dso__sym_priv(kernel_dso, sym);
87186470930SIngo Molnar 
87286470930SIngo Molnar 		if (!syme->skip) {
87386470930SIngo Molnar 			syme->count[counter]++;
874923c42c1SMike Galbraith 			record_precise_ip(syme, counter, ip);
87586470930SIngo Molnar 			pthread_mutex_lock(&active_symbols_lock);
87686470930SIngo Molnar 			if (list_empty(&syme->node) || !syme->node.next)
87786470930SIngo Molnar 				__list_insert_active_sym(syme);
87886470930SIngo Molnar 			pthread_mutex_unlock(&active_symbols_lock);
87986470930SIngo Molnar 			return;
88086470930SIngo Molnar 		}
88186470930SIngo Molnar 	}
88286470930SIngo Molnar 
88386470930SIngo Molnar 	samples--;
88486470930SIngo Molnar }
88586470930SIngo Molnar 
886e6e18ec7SPeter Zijlstra static void process_event(u64 ip, int counter, int user)
88786470930SIngo Molnar {
88886470930SIngo Molnar 	samples++;
88986470930SIngo Molnar 
890e6e18ec7SPeter Zijlstra 	if (user) {
89186470930SIngo Molnar 		userspace_samples++;
89286470930SIngo Molnar 		return;
89386470930SIngo Molnar 	}
89486470930SIngo Molnar 
89586470930SIngo Molnar 	record_ip(ip, counter);
89686470930SIngo Molnar }
89786470930SIngo Molnar 
89886470930SIngo Molnar struct mmap_data {
89986470930SIngo Molnar 	int			counter;
90086470930SIngo Molnar 	void			*base;
901f37a291cSIngo Molnar 	int			mask;
90286470930SIngo Molnar 	unsigned int		prev;
90386470930SIngo Molnar };
90486470930SIngo Molnar 
90586470930SIngo Molnar static unsigned int mmap_read_head(struct mmap_data *md)
90686470930SIngo Molnar {
907cdd6c482SIngo Molnar 	struct perf_event_mmap_page *pc = md->base;
90886470930SIngo Molnar 	int head;
90986470930SIngo Molnar 
91086470930SIngo Molnar 	head = pc->data_head;
91186470930SIngo Molnar 	rmb();
91286470930SIngo Molnar 
91386470930SIngo Molnar 	return head;
91486470930SIngo Molnar }
91586470930SIngo Molnar 
91686470930SIngo Molnar struct timeval last_read, this_read;
91786470930SIngo Molnar 
9182f01190aSFrederic Weisbecker static void mmap_read_counter(struct mmap_data *md)
91986470930SIngo Molnar {
92086470930SIngo Molnar 	unsigned int head = mmap_read_head(md);
92186470930SIngo Molnar 	unsigned int old = md->prev;
92286470930SIngo Molnar 	unsigned char *data = md->base + page_size;
92386470930SIngo Molnar 	int diff;
92486470930SIngo Molnar 
92586470930SIngo Molnar 	gettimeofday(&this_read, NULL);
92686470930SIngo Molnar 
92786470930SIngo Molnar 	/*
92886470930SIngo Molnar 	 * If we're further behind than half the buffer, there's a chance
92986470930SIngo Molnar 	 * the writer will bite our tail and mess up the samples under us.
93086470930SIngo Molnar 	 *
93186470930SIngo Molnar 	 * If we somehow ended up ahead of the head, we got messed up.
93286470930SIngo Molnar 	 *
93386470930SIngo Molnar 	 * In either case, truncate and restart at head.
93486470930SIngo Molnar 	 */
93586470930SIngo Molnar 	diff = head - old;
93686470930SIngo Molnar 	if (diff > md->mask / 2 || diff < 0) {
93786470930SIngo Molnar 		struct timeval iv;
93886470930SIngo Molnar 		unsigned long msecs;
93986470930SIngo Molnar 
94086470930SIngo Molnar 		timersub(&this_read, &last_read, &iv);
94186470930SIngo Molnar 		msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
94286470930SIngo Molnar 
94386470930SIngo Molnar 		fprintf(stderr, "WARNING: failed to keep up with mmap data."
94486470930SIngo Molnar 				"  Last read %lu msecs ago.\n", msecs);
94586470930SIngo Molnar 
94686470930SIngo Molnar 		/*
94786470930SIngo Molnar 		 * head points to a known good entry, start there.
94886470930SIngo Molnar 		 */
94986470930SIngo Molnar 		old = head;
95086470930SIngo Molnar 	}
95186470930SIngo Molnar 
95286470930SIngo Molnar 	last_read = this_read;
95386470930SIngo Molnar 
95486470930SIngo Molnar 	for (; old != head;) {
95586470930SIngo Molnar 		event_t *event = (event_t *)&data[old & md->mask];
95686470930SIngo Molnar 
95786470930SIngo Molnar 		event_t event_copy;
95886470930SIngo Molnar 
95986470930SIngo Molnar 		size_t size = event->header.size;
96086470930SIngo Molnar 
96186470930SIngo Molnar 		/*
96286470930SIngo Molnar 		 * Event straddles the mmap boundary -- header should always
96386470930SIngo Molnar 		 * be inside due to u64 alignment of output.
96486470930SIngo Molnar 		 */
96586470930SIngo Molnar 		if ((old & md->mask) + size != ((old + size) & md->mask)) {
96686470930SIngo Molnar 			unsigned int offset = old;
96786470930SIngo Molnar 			unsigned int len = min(sizeof(*event), size), cpy;
96886470930SIngo Molnar 			void *dst = &event_copy;
96986470930SIngo Molnar 
97086470930SIngo Molnar 			do {
97186470930SIngo Molnar 				cpy = min(md->mask + 1 - (offset & md->mask), len);
97286470930SIngo Molnar 				memcpy(dst, &data[offset & md->mask], cpy);
97386470930SIngo Molnar 				offset += cpy;
97486470930SIngo Molnar 				dst += cpy;
97586470930SIngo Molnar 				len -= cpy;
97686470930SIngo Molnar 			} while (len);
97786470930SIngo Molnar 
97886470930SIngo Molnar 			event = &event_copy;
97986470930SIngo Molnar 		}
98086470930SIngo Molnar 
98186470930SIngo Molnar 		old += size;
98286470930SIngo Molnar 
983cdd6c482SIngo Molnar 		if (event->header.type == PERF_RECORD_SAMPLE) {
984e6e18ec7SPeter Zijlstra 			int user =
985cdd6c482SIngo Molnar 	(event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK) == PERF_RECORD_MISC_USER;
986e6e18ec7SPeter Zijlstra 			process_event(event->ip.ip, md->counter, user);
98786470930SIngo Molnar 		}
98886470930SIngo Molnar 	}
98986470930SIngo Molnar 
99086470930SIngo Molnar 	md->prev = old;
99186470930SIngo Molnar }
99286470930SIngo Molnar 
99386470930SIngo Molnar static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
99486470930SIngo Molnar static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
99586470930SIngo Molnar 
9962f01190aSFrederic Weisbecker static void mmap_read(void)
9972f01190aSFrederic Weisbecker {
9982f01190aSFrederic Weisbecker 	int i, counter;
9992f01190aSFrederic Weisbecker 
10002f01190aSFrederic Weisbecker 	for (i = 0; i < nr_cpus; i++) {
10012f01190aSFrederic Weisbecker 		for (counter = 0; counter < nr_counters; counter++)
10022f01190aSFrederic Weisbecker 			mmap_read_counter(&mmap_array[i][counter]);
10032f01190aSFrederic Weisbecker 	}
10042f01190aSFrederic Weisbecker }
10052f01190aSFrederic Weisbecker 
1006716c69feSIngo Molnar int nr_poll;
1007716c69feSIngo Molnar int group_fd;
1008716c69feSIngo Molnar 
1009716c69feSIngo Molnar static void start_counter(int i, int counter)
101086470930SIngo Molnar {
1011cdd6c482SIngo Molnar 	struct perf_event_attr *attr;
10120fdc7e67SMike Galbraith 	int cpu;
101386470930SIngo Molnar 
101486470930SIngo Molnar 	cpu = profile_cpu;
101586470930SIngo Molnar 	if (target_pid == -1 && profile_cpu == -1)
101686470930SIngo Molnar 		cpu = i;
101786470930SIngo Molnar 
101886470930SIngo Molnar 	attr = attrs + counter;
101986470930SIngo Molnar 
102086470930SIngo Molnar 	attr->sample_type	= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
102186470930SIngo Molnar 	attr->freq		= freq;
10220fdc7e67SMike Galbraith 	attr->inherit		= (cpu < 0) && inherit;
102386470930SIngo Molnar 
1024716c69feSIngo Molnar try_again:
1025cdd6c482SIngo Molnar 	fd[i][counter] = sys_perf_event_open(attr, target_pid, cpu, group_fd, 0);
1026716c69feSIngo Molnar 
102786470930SIngo Molnar 	if (fd[i][counter] < 0) {
102886470930SIngo Molnar 		int err = errno;
102986470930SIngo Molnar 
103086470930SIngo Molnar 		if (err == EPERM)
1031716c69feSIngo Molnar 			die("No permission - are you root?\n");
1032716c69feSIngo Molnar 		/*
1033716c69feSIngo Molnar 		 * If it's cycles then fall back to hrtimer
1034716c69feSIngo Molnar 		 * based cpu-clock-tick sw counter, which
1035716c69feSIngo Molnar 		 * is always available even if no PMU support:
1036716c69feSIngo Molnar 		 */
1037716c69feSIngo Molnar 		if (attr->type == PERF_TYPE_HARDWARE
1038f4dbfa8fSPeter Zijlstra 			&& attr->config == PERF_COUNT_HW_CPU_CYCLES) {
1039716c69feSIngo Molnar 
10403da297a6SIngo Molnar 			if (verbose)
1041716c69feSIngo Molnar 				warning(" ... trying to fall back to cpu-clock-ticks\n");
10423da297a6SIngo Molnar 
1043716c69feSIngo Molnar 			attr->type = PERF_TYPE_SOFTWARE;
1044f4dbfa8fSPeter Zijlstra 			attr->config = PERF_COUNT_SW_CPU_CLOCK;
1045716c69feSIngo Molnar 			goto try_again;
1046716c69feSIngo Molnar 		}
104730c806a0SIngo Molnar 		printf("\n");
104830c806a0SIngo Molnar 		error("perfcounter syscall returned with %d (%s)\n",
104930c806a0SIngo Molnar 			fd[i][counter], strerror(err));
1050cdd6c482SIngo Molnar 		die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
105186470930SIngo Molnar 		exit(-1);
105286470930SIngo Molnar 	}
105386470930SIngo Molnar 	assert(fd[i][counter] >= 0);
105486470930SIngo Molnar 	fcntl(fd[i][counter], F_SETFL, O_NONBLOCK);
105586470930SIngo Molnar 
105686470930SIngo Molnar 	/*
105786470930SIngo Molnar 	 * First counter acts as the group leader:
105886470930SIngo Molnar 	 */
105986470930SIngo Molnar 	if (group && group_fd == -1)
106086470930SIngo Molnar 		group_fd = fd[i][counter];
106186470930SIngo Molnar 
106286470930SIngo Molnar 	event_array[nr_poll].fd = fd[i][counter];
106386470930SIngo Molnar 	event_array[nr_poll].events = POLLIN;
106486470930SIngo Molnar 	nr_poll++;
106586470930SIngo Molnar 
106686470930SIngo Molnar 	mmap_array[i][counter].counter = counter;
106786470930SIngo Molnar 	mmap_array[i][counter].prev = 0;
106886470930SIngo Molnar 	mmap_array[i][counter].mask = mmap_pages*page_size - 1;
106986470930SIngo Molnar 	mmap_array[i][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
107086470930SIngo Molnar 			PROT_READ, MAP_SHARED, fd[i][counter], 0);
107186470930SIngo Molnar 	if (mmap_array[i][counter].base == MAP_FAILED)
107286470930SIngo Molnar 		die("failed to mmap with %d (%s)\n", errno, strerror(errno));
107386470930SIngo Molnar }
1074716c69feSIngo Molnar 
1075716c69feSIngo Molnar static int __cmd_top(void)
1076716c69feSIngo Molnar {
1077716c69feSIngo Molnar 	pthread_t thread;
1078716c69feSIngo Molnar 	int i, counter;
1079716c69feSIngo Molnar 	int ret;
1080716c69feSIngo Molnar 
1081716c69feSIngo Molnar 	for (i = 0; i < nr_cpus; i++) {
1082716c69feSIngo Molnar 		group_fd = -1;
1083716c69feSIngo Molnar 		for (counter = 0; counter < nr_counters; counter++)
1084716c69feSIngo Molnar 			start_counter(i, counter);
108586470930SIngo Molnar 	}
108686470930SIngo Molnar 
10872f01190aSFrederic Weisbecker 	/* Wait for a minimal set of events before starting the snapshot */
10882f01190aSFrederic Weisbecker 	poll(event_array, nr_poll, 100);
10892f01190aSFrederic Weisbecker 
10902f01190aSFrederic Weisbecker 	mmap_read();
10912f01190aSFrederic Weisbecker 
109286470930SIngo Molnar 	if (pthread_create(&thread, NULL, display_thread, NULL)) {
109386470930SIngo Molnar 		printf("Could not create display thread.\n");
109486470930SIngo Molnar 		exit(-1);
109586470930SIngo Molnar 	}
109686470930SIngo Molnar 
109786470930SIngo Molnar 	if (realtime_prio) {
109886470930SIngo Molnar 		struct sched_param param;
109986470930SIngo Molnar 
110086470930SIngo Molnar 		param.sched_priority = realtime_prio;
110186470930SIngo Molnar 		if (sched_setscheduler(0, SCHED_FIFO, &param)) {
110286470930SIngo Molnar 			printf("Could not set realtime priority.\n");
110386470930SIngo Molnar 			exit(-1);
110486470930SIngo Molnar 		}
110586470930SIngo Molnar 	}
110686470930SIngo Molnar 
110786470930SIngo Molnar 	while (1) {
110886470930SIngo Molnar 		int hits = samples;
110986470930SIngo Molnar 
11102f01190aSFrederic Weisbecker 		mmap_read();
111186470930SIngo Molnar 
111286470930SIngo Molnar 		if (hits == samples)
111386470930SIngo Molnar 			ret = poll(event_array, nr_poll, 100);
111486470930SIngo Molnar 	}
111586470930SIngo Molnar 
111686470930SIngo Molnar 	return 0;
111786470930SIngo Molnar }
111886470930SIngo Molnar 
111986470930SIngo Molnar static const char * const top_usage[] = {
112086470930SIngo Molnar 	"perf top [<options>]",
112186470930SIngo Molnar 	NULL
112286470930SIngo Molnar };
112386470930SIngo Molnar 
112486470930SIngo Molnar static const struct option options[] = {
112586470930SIngo Molnar 	OPT_CALLBACK('e', "event", NULL, "event",
112686470930SIngo Molnar 		     "event selector. use 'perf list' to list available events",
112786470930SIngo Molnar 		     parse_events),
112886470930SIngo Molnar 	OPT_INTEGER('c', "count", &default_interval,
112986470930SIngo Molnar 		    "event period to sample"),
113086470930SIngo Molnar 	OPT_INTEGER('p', "pid", &target_pid,
113186470930SIngo Molnar 		    "profile events on existing pid"),
113286470930SIngo Molnar 	OPT_BOOLEAN('a', "all-cpus", &system_wide,
113386470930SIngo Molnar 			    "system-wide collection from all CPUs"),
113486470930SIngo Molnar 	OPT_INTEGER('C', "CPU", &profile_cpu,
113586470930SIngo Molnar 		    "CPU to profile on"),
113683a0944fSIngo Molnar 	OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
113786470930SIngo Molnar 	OPT_INTEGER('m', "mmap-pages", &mmap_pages,
113886470930SIngo Molnar 		    "number of mmap data pages"),
113986470930SIngo Molnar 	OPT_INTEGER('r', "realtime", &realtime_prio,
114086470930SIngo Molnar 		    "collect data with this RT SCHED_FIFO priority"),
114186470930SIngo Molnar 	OPT_INTEGER('d', "delay", &delay_secs,
114286470930SIngo Molnar 		    "number of seconds to delay between refreshes"),
114386470930SIngo Molnar 	OPT_BOOLEAN('D', "dump-symtab", &dump_symtab,
114486470930SIngo Molnar 			    "dump the symbol table used for profiling"),
114586470930SIngo Molnar 	OPT_INTEGER('f', "count-filter", &count_filter,
114686470930SIngo Molnar 		    "only display functions with more events than this"),
114786470930SIngo Molnar 	OPT_BOOLEAN('g', "group", &group,
114886470930SIngo Molnar 			    "put the counters into a counter group"),
11490fdc7e67SMike Galbraith 	OPT_BOOLEAN('i', "inherit", &inherit,
11500fdc7e67SMike Galbraith 		    "child tasks inherit counters"),
1151923c42c1SMike Galbraith 	OPT_STRING('s', "sym-annotate", &sym_filter, "symbol name",
1152923c42c1SMike Galbraith 		    "symbol to annotate - requires -k option"),
11531f208ea6SAnton Blanchard 	OPT_BOOLEAN('z', "zero", &zero,
115486470930SIngo Molnar 		    "zero history across updates"),
115586470930SIngo Molnar 	OPT_INTEGER('F', "freq", &freq,
115686470930SIngo Molnar 		    "profile at this frequency"),
115786470930SIngo Molnar 	OPT_INTEGER('E', "entries", &print_entries,
115886470930SIngo Molnar 		    "display this many functions"),
11593da297a6SIngo Molnar 	OPT_BOOLEAN('v', "verbose", &verbose,
11603da297a6SIngo Molnar 		    "be more verbose (show counter open errors, etc)"),
116186470930SIngo Molnar 	OPT_END()
116286470930SIngo Molnar };
116386470930SIngo Molnar 
1164f37a291cSIngo Molnar int cmd_top(int argc, const char **argv, const char *prefix __used)
116586470930SIngo Molnar {
116686470930SIngo Molnar 	int counter;
116786470930SIngo Molnar 
116842976487SMike Galbraith 	symbol__init();
116942976487SMike Galbraith 
117086470930SIngo Molnar 	page_size = sysconf(_SC_PAGE_SIZE);
117186470930SIngo Molnar 
117286470930SIngo Molnar 	argc = parse_options(argc, argv, options, top_usage, 0);
117386470930SIngo Molnar 	if (argc)
117486470930SIngo Molnar 		usage_with_options(top_usage, options);
117586470930SIngo Molnar 
117686470930SIngo Molnar 	if (freq) {
117786470930SIngo Molnar 		default_interval = freq;
117886470930SIngo Molnar 		freq = 1;
117986470930SIngo Molnar 	}
118086470930SIngo Molnar 
118186470930SIngo Molnar 	/* CPU and PID are mutually exclusive */
118286470930SIngo Molnar 	if (target_pid != -1 && profile_cpu != -1) {
118386470930SIngo Molnar 		printf("WARNING: PID switch overriding CPU\n");
118486470930SIngo Molnar 		sleep(1);
118586470930SIngo Molnar 		profile_cpu = -1;
118686470930SIngo Molnar 	}
118786470930SIngo Molnar 
118886470930SIngo Molnar 	if (!nr_counters)
118986470930SIngo Molnar 		nr_counters = 1;
119086470930SIngo Molnar 
119186470930SIngo Molnar 	if (delay_secs < 1)
119286470930SIngo Molnar 		delay_secs = 1;
119386470930SIngo Molnar 
119486470930SIngo Molnar 	parse_symbols();
1195923c42c1SMike Galbraith 	parse_source(sym_filter_entry);
119686470930SIngo Molnar 
119786470930SIngo Molnar 	/*
119886470930SIngo Molnar 	 * Fill in the ones not specifically initialized via -c:
119986470930SIngo Molnar 	 */
120086470930SIngo Molnar 	for (counter = 0; counter < nr_counters; counter++) {
120186470930SIngo Molnar 		if (attrs[counter].sample_period)
120286470930SIngo Molnar 			continue;
120386470930SIngo Molnar 
120486470930SIngo Molnar 		attrs[counter].sample_period = default_interval;
120586470930SIngo Molnar 	}
120686470930SIngo Molnar 
120786470930SIngo Molnar 	nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
120886470930SIngo Molnar 	assert(nr_cpus <= MAX_NR_CPUS);
120986470930SIngo Molnar 	assert(nr_cpus >= 0);
121086470930SIngo Molnar 
121186470930SIngo Molnar 	if (target_pid != -1 || profile_cpu != -1)
121286470930SIngo Molnar 		nr_cpus = 1;
121386470930SIngo Molnar 
121486470930SIngo Molnar 	return __cmd_top();
121586470930SIngo Molnar }
1216