xref: /openbmc/linux/tools/perf/builtin-top.c (revision 091bd2e9)
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 
3086470930SIngo Molnar #include <assert.h>
3186470930SIngo Molnar #include <fcntl.h>
3286470930SIngo Molnar 
3386470930SIngo Molnar #include <stdio.h>
34923c42c1SMike Galbraith #include <termios.h>
35923c42c1SMike Galbraith #include <unistd.h>
3686470930SIngo Molnar 
3786470930SIngo Molnar #include <errno.h>
3886470930SIngo Molnar #include <time.h>
3986470930SIngo Molnar #include <sched.h>
4086470930SIngo Molnar #include <pthread.h>
4186470930SIngo Molnar 
4286470930SIngo Molnar #include <sys/syscall.h>
4386470930SIngo Molnar #include <sys/ioctl.h>
4486470930SIngo Molnar #include <sys/poll.h>
4586470930SIngo Molnar #include <sys/prctl.h>
4686470930SIngo Molnar #include <sys/wait.h>
4786470930SIngo Molnar #include <sys/uio.h>
4886470930SIngo Molnar #include <sys/mman.h>
4986470930SIngo Molnar 
5086470930SIngo Molnar #include <linux/unistd.h>
5186470930SIngo Molnar #include <linux/types.h>
5286470930SIngo Molnar 
5386470930SIngo Molnar static int			fd[MAX_NR_CPUS][MAX_COUNTERS];
5486470930SIngo Molnar 
5586470930SIngo Molnar static int			system_wide			=  0;
5686470930SIngo Molnar 
5786470930SIngo Molnar static int			default_interval		= 100000;
5886470930SIngo Molnar 
59923c42c1SMike Galbraith static int			count_filter			=  5;
6086470930SIngo Molnar static int			print_entries			= 15;
6186470930SIngo Molnar 
6286470930SIngo Molnar static int			target_pid			= -1;
630fdc7e67SMike Galbraith static int			inherit				=  0;
6486470930SIngo Molnar static int			profile_cpu			= -1;
6586470930SIngo Molnar static int			nr_cpus				=  0;
6686470930SIngo Molnar static unsigned int		realtime_prio			=  0;
6786470930SIngo Molnar static int			group				=  0;
6886470930SIngo Molnar static unsigned int		page_size;
6986470930SIngo Molnar static unsigned int		mmap_pages			= 16;
7086470930SIngo Molnar static int			freq				=  0;
713da297a6SIngo Molnar static int			verbose				=  0;
7242976487SMike Galbraith static char			*vmlinux			=  NULL;
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;
125923c42c1SMike Galbraith 	char command[PATH_MAX*2], *path = vmlinux;
126923c42c1SMike Galbraith 	u64 start, end, len;
127923c42c1SMike Galbraith 
128923c42c1SMike Galbraith 	if (!syme)
129923c42c1SMike Galbraith 		return;
130923c42c1SMike Galbraith 
131923c42c1SMike Galbraith 	if (syme->lines) {
132923c42c1SMike Galbraith 		pthread_mutex_lock(&syme->source_lock);
133923c42c1SMike Galbraith 		goto out_assign;
134923c42c1SMike Galbraith 	}
135923c42c1SMike Galbraith 
136923c42c1SMike Galbraith 	sym = (struct symbol *)(syme + 1);
137923c42c1SMike Galbraith 	module = sym->module;
138923c42c1SMike Galbraith 
139923c42c1SMike Galbraith 	if (module)
140923c42c1SMike Galbraith 		path = module->path;
141923c42c1SMike Galbraith 	if (!path)
142923c42c1SMike Galbraith 		return;
143923c42c1SMike Galbraith 
144923c42c1SMike Galbraith 	start = sym->obj_start;
145923c42c1SMike Galbraith 	if (!start)
146923c42c1SMike Galbraith 		start = sym->start;
147923c42c1SMike Galbraith 
148923c42c1SMike Galbraith 	if (module) {
149923c42c1SMike Galbraith 		section = module->sections->find_section(module->sections, ".text");
150923c42c1SMike Galbraith 		if (section)
151923c42c1SMike Galbraith 			start -= section->vma;
152923c42c1SMike Galbraith 	}
153923c42c1SMike Galbraith 
154923c42c1SMike Galbraith 	end = start + sym->end - sym->start + 1;
155923c42c1SMike Galbraith 	len = sym->end - sym->start;
156923c42c1SMike Galbraith 
157923c42c1SMike Galbraith 	sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s", start, end, path);
158923c42c1SMike Galbraith 
159923c42c1SMike Galbraith 	file = popen(command, "r");
160923c42c1SMike Galbraith 	if (!file)
161923c42c1SMike Galbraith 		return;
162923c42c1SMike Galbraith 
163923c42c1SMike Galbraith 	pthread_mutex_lock(&syme->source_lock);
164923c42c1SMike Galbraith 	syme->lines_tail = &syme->lines;
165923c42c1SMike Galbraith 	while (!feof(file)) {
166923c42c1SMike Galbraith 		struct source_line *src;
167923c42c1SMike Galbraith 		size_t dummy = 0;
168923c42c1SMike Galbraith 		char *c;
169923c42c1SMike Galbraith 
170923c42c1SMike Galbraith 		src = malloc(sizeof(struct source_line));
171923c42c1SMike Galbraith 		assert(src != NULL);
172923c42c1SMike Galbraith 		memset(src, 0, sizeof(struct source_line));
173923c42c1SMike Galbraith 
174923c42c1SMike Galbraith 		if (getline(&src->line, &dummy, file) < 0)
175923c42c1SMike Galbraith 			break;
176923c42c1SMike Galbraith 		if (!src->line)
177923c42c1SMike Galbraith 			break;
178923c42c1SMike Galbraith 
179923c42c1SMike Galbraith 		c = strchr(src->line, '\n');
180923c42c1SMike Galbraith 		if (c)
181923c42c1SMike Galbraith 			*c = 0;
182923c42c1SMike Galbraith 
183923c42c1SMike Galbraith 		src->next = NULL;
184923c42c1SMike Galbraith 		*syme->lines_tail = src;
185923c42c1SMike Galbraith 		syme->lines_tail = &src->next;
186923c42c1SMike Galbraith 
187923c42c1SMike Galbraith 		if (strlen(src->line)>8 && src->line[8] == ':') {
188923c42c1SMike Galbraith 			src->eip = strtoull(src->line, NULL, 16);
189923c42c1SMike Galbraith 			if (section)
190923c42c1SMike Galbraith 				src->eip += section->vma;
191923c42c1SMike Galbraith 		}
192923c42c1SMike Galbraith 		if (strlen(src->line)>8 && src->line[16] == ':') {
193923c42c1SMike Galbraith 			src->eip = strtoull(src->line, NULL, 16);
194923c42c1SMike Galbraith 			if (section)
195923c42c1SMike Galbraith 				src->eip += section->vma;
196923c42c1SMike Galbraith 		}
197923c42c1SMike Galbraith 	}
198923c42c1SMike Galbraith 	pclose(file);
199923c42c1SMike Galbraith out_assign:
200923c42c1SMike Galbraith 	sym_filter_entry = syme;
201923c42c1SMike Galbraith 	pthread_mutex_unlock(&syme->source_lock);
202923c42c1SMike Galbraith }
203923c42c1SMike Galbraith 
204923c42c1SMike Galbraith static void __zero_source_counters(struct sym_entry *syme)
205923c42c1SMike Galbraith {
206923c42c1SMike Galbraith 	int i;
207923c42c1SMike Galbraith 	struct source_line *line;
208923c42c1SMike Galbraith 
209923c42c1SMike Galbraith 	line = syme->lines;
210923c42c1SMike Galbraith 	while (line) {
211923c42c1SMike Galbraith 		for (i = 0; i < nr_counters; i++)
212923c42c1SMike Galbraith 			line->count[i] = 0;
213923c42c1SMike Galbraith 		line = line->next;
214923c42c1SMike Galbraith 	}
215923c42c1SMike Galbraith }
216923c42c1SMike Galbraith 
217923c42c1SMike Galbraith static void record_precise_ip(struct sym_entry *syme, int counter, u64 ip)
218923c42c1SMike Galbraith {
219923c42c1SMike Galbraith 	struct source_line *line;
220923c42c1SMike Galbraith 
221923c42c1SMike Galbraith 	if (syme != sym_filter_entry)
222923c42c1SMike Galbraith 		return;
223923c42c1SMike Galbraith 
224923c42c1SMike Galbraith 	if (pthread_mutex_trylock(&syme->source_lock))
225923c42c1SMike Galbraith 		return;
226923c42c1SMike Galbraith 
227923c42c1SMike Galbraith 	if (!syme->source)
228923c42c1SMike Galbraith 		goto out_unlock;
229923c42c1SMike Galbraith 
230923c42c1SMike Galbraith 	for (line = syme->lines; line; line = line->next) {
231923c42c1SMike Galbraith 		if (line->eip == ip) {
232923c42c1SMike Galbraith 			line->count[counter]++;
233923c42c1SMike Galbraith 			break;
234923c42c1SMike Galbraith 		}
235923c42c1SMike Galbraith 		if (line->eip > ip)
236923c42c1SMike Galbraith 			break;
237923c42c1SMike Galbraith 	}
238923c42c1SMike Galbraith out_unlock:
239923c42c1SMike Galbraith 	pthread_mutex_unlock(&syme->source_lock);
240923c42c1SMike Galbraith }
241923c42c1SMike Galbraith 
242923c42c1SMike Galbraith static void lookup_sym_source(struct sym_entry *syme)
243923c42c1SMike Galbraith {
244923c42c1SMike Galbraith 	struct symbol *symbol = (struct symbol *)(syme + 1);
245923c42c1SMike Galbraith 	struct source_line *line;
246923c42c1SMike Galbraith 	char pattern[PATH_MAX];
247923c42c1SMike Galbraith 	char *idx;
248923c42c1SMike Galbraith 
249923c42c1SMike Galbraith 	sprintf(pattern, "<%s>:", symbol->name);
250923c42c1SMike Galbraith 
251923c42c1SMike Galbraith 	if (symbol->module) {
252923c42c1SMike Galbraith 		idx = strstr(pattern, "\t");
253923c42c1SMike Galbraith 		if (idx)
254923c42c1SMike Galbraith 			*idx = 0;
255923c42c1SMike Galbraith 	}
256923c42c1SMike Galbraith 
257923c42c1SMike Galbraith 	pthread_mutex_lock(&syme->source_lock);
258923c42c1SMike Galbraith 	for (line = syme->lines; line; line = line->next) {
259923c42c1SMike Galbraith 		if (strstr(line->line, pattern)) {
260923c42c1SMike Galbraith 			syme->source = line;
261923c42c1SMike Galbraith 			break;
262923c42c1SMike Galbraith 		}
263923c42c1SMike Galbraith 	}
264923c42c1SMike Galbraith 	pthread_mutex_unlock(&syme->source_lock);
265923c42c1SMike Galbraith }
266923c42c1SMike Galbraith 
267923c42c1SMike Galbraith static void show_lines(struct source_line *queue, int count, int total)
268923c42c1SMike Galbraith {
269923c42c1SMike Galbraith 	int i;
270923c42c1SMike Galbraith 	struct source_line *line;
271923c42c1SMike Galbraith 
272923c42c1SMike Galbraith 	line = queue;
273923c42c1SMike Galbraith 	for (i = 0; i < count; i++) {
274923c42c1SMike Galbraith 		float pcnt = 100.0*(float)line->count[sym_counter]/(float)total;
275923c42c1SMike Galbraith 
276923c42c1SMike Galbraith 		printf("%8li %4.1f%%\t%s\n", line->count[sym_counter], pcnt, line->line);
277923c42c1SMike Galbraith 		line = line->next;
278923c42c1SMike Galbraith 	}
279923c42c1SMike Galbraith }
280923c42c1SMike Galbraith 
281923c42c1SMike Galbraith #define TRACE_COUNT     3
282923c42c1SMike Galbraith 
283923c42c1SMike Galbraith static void show_details(struct sym_entry *syme)
284923c42c1SMike Galbraith {
285923c42c1SMike Galbraith 	struct symbol *symbol;
286923c42c1SMike Galbraith 	struct source_line *line;
287923c42c1SMike Galbraith 	struct source_line *line_queue = NULL;
288923c42c1SMike Galbraith 	int displayed = 0;
289923c42c1SMike Galbraith 	int line_queue_count = 0, total = 0, more = 0;
290923c42c1SMike Galbraith 
291923c42c1SMike Galbraith 	if (!syme)
292923c42c1SMike Galbraith 		return;
293923c42c1SMike Galbraith 
294923c42c1SMike Galbraith 	if (!syme->source)
295923c42c1SMike Galbraith 		lookup_sym_source(syme);
296923c42c1SMike Galbraith 
297923c42c1SMike Galbraith 	if (!syme->source)
298923c42c1SMike Galbraith 		return;
299923c42c1SMike Galbraith 
300923c42c1SMike Galbraith 	symbol = (struct symbol *)(syme + 1);
301923c42c1SMike Galbraith 	printf("Showing %s for %s\n", event_name(sym_counter), symbol->name);
302923c42c1SMike Galbraith 	printf("  Events  Pcnt (>=%d%%)\n", sym_pcnt_filter);
303923c42c1SMike Galbraith 
304923c42c1SMike Galbraith 	pthread_mutex_lock(&syme->source_lock);
305923c42c1SMike Galbraith 	line = syme->source;
306923c42c1SMike Galbraith 	while (line) {
307923c42c1SMike Galbraith 		total += line->count[sym_counter];
308923c42c1SMike Galbraith 		line = line->next;
309923c42c1SMike Galbraith 	}
310923c42c1SMike Galbraith 
311923c42c1SMike Galbraith 	line = syme->source;
312923c42c1SMike Galbraith 	while (line) {
313923c42c1SMike Galbraith 		float pcnt = 0.0;
314923c42c1SMike Galbraith 
315923c42c1SMike Galbraith 		if (!line_queue_count)
316923c42c1SMike Galbraith 			line_queue = line;
317923c42c1SMike Galbraith 		line_queue_count++;
318923c42c1SMike Galbraith 
319923c42c1SMike Galbraith 		if (line->count[sym_counter])
320923c42c1SMike Galbraith 			pcnt = 100.0 * line->count[sym_counter] / (float)total;
321923c42c1SMike Galbraith 		if (pcnt >= (float)sym_pcnt_filter) {
322923c42c1SMike Galbraith 			if (displayed <= print_entries)
323923c42c1SMike Galbraith 				show_lines(line_queue, line_queue_count, total);
324923c42c1SMike Galbraith 			else more++;
325923c42c1SMike Galbraith 			displayed += line_queue_count;
326923c42c1SMike Galbraith 			line_queue_count = 0;
327923c42c1SMike Galbraith 			line_queue = NULL;
328923c42c1SMike Galbraith 		} else if (line_queue_count > TRACE_COUNT) {
329923c42c1SMike Galbraith 			line_queue = line_queue->next;
330923c42c1SMike Galbraith 			line_queue_count--;
331923c42c1SMike Galbraith 		}
332923c42c1SMike Galbraith 
333923c42c1SMike Galbraith 		line->count[sym_counter] = zero ? 0 : line->count[sym_counter] * 7 / 8;
334923c42c1SMike Galbraith 		line = line->next;
335923c42c1SMike Galbraith 	}
336923c42c1SMike Galbraith 	pthread_mutex_unlock(&syme->source_lock);
337923c42c1SMike Galbraith 	if (more)
338923c42c1SMike Galbraith 		printf("%d lines not displayed, maybe increase display entries [e]\n", more);
339923c42c1SMike Galbraith }
34086470930SIngo Molnar 
34186470930SIngo Molnar struct dso			*kernel_dso;
34286470930SIngo Molnar 
34386470930SIngo Molnar /*
34486470930SIngo Molnar  * Symbols will be added here in record_ip and will get out
34586470930SIngo Molnar  * after decayed.
34686470930SIngo Molnar  */
34786470930SIngo Molnar static LIST_HEAD(active_symbols);
34886470930SIngo Molnar static pthread_mutex_t active_symbols_lock = PTHREAD_MUTEX_INITIALIZER;
34986470930SIngo Molnar 
35086470930SIngo Molnar /*
35186470930SIngo Molnar  * Ordering weight: count-1 * count-2 * ... / count-n
35286470930SIngo Molnar  */
35386470930SIngo Molnar static double sym_weight(const struct sym_entry *sym)
35486470930SIngo Molnar {
35586470930SIngo Molnar 	double weight = sym->snap_count;
35686470930SIngo Molnar 	int counter;
35786470930SIngo Molnar 
35846ab9764SMike Galbraith 	if (!display_weighted)
35946ab9764SMike Galbraith 		return weight;
36046ab9764SMike Galbraith 
36186470930SIngo Molnar 	for (counter = 1; counter < nr_counters-1; counter++)
36286470930SIngo Molnar 		weight *= sym->count[counter];
36386470930SIngo Molnar 
36486470930SIngo Molnar 	weight /= (sym->count[counter] + 1);
36586470930SIngo Molnar 
36686470930SIngo Molnar 	return weight;
36786470930SIngo Molnar }
36886470930SIngo Molnar 
36986470930SIngo Molnar static long			samples;
37086470930SIngo Molnar static long			userspace_samples;
37186470930SIngo Molnar static const char		CONSOLE_CLEAR[] = "";
37286470930SIngo Molnar 
37386470930SIngo Molnar static void __list_insert_active_sym(struct sym_entry *syme)
37486470930SIngo Molnar {
37586470930SIngo Molnar 	list_add(&syme->node, &active_symbols);
37686470930SIngo Molnar }
37786470930SIngo Molnar 
37886470930SIngo Molnar static void list_remove_active_sym(struct sym_entry *syme)
37986470930SIngo Molnar {
38086470930SIngo Molnar 	pthread_mutex_lock(&active_symbols_lock);
38186470930SIngo Molnar 	list_del_init(&syme->node);
38286470930SIngo Molnar 	pthread_mutex_unlock(&active_symbols_lock);
38386470930SIngo Molnar }
38486470930SIngo Molnar 
38586470930SIngo Molnar static void rb_insert_active_sym(struct rb_root *tree, struct sym_entry *se)
38686470930SIngo Molnar {
38786470930SIngo Molnar 	struct rb_node **p = &tree->rb_node;
38886470930SIngo Molnar 	struct rb_node *parent = NULL;
38986470930SIngo Molnar 	struct sym_entry *iter;
39086470930SIngo Molnar 
39186470930SIngo Molnar 	while (*p != NULL) {
39286470930SIngo Molnar 		parent = *p;
39386470930SIngo Molnar 		iter = rb_entry(parent, struct sym_entry, rb_node);
39486470930SIngo Molnar 
39586470930SIngo Molnar 		if (se->weight > iter->weight)
39686470930SIngo Molnar 			p = &(*p)->rb_left;
39786470930SIngo Molnar 		else
39886470930SIngo Molnar 			p = &(*p)->rb_right;
39986470930SIngo Molnar 	}
40086470930SIngo Molnar 
40186470930SIngo Molnar 	rb_link_node(&se->rb_node, parent, p);
40286470930SIngo Molnar 	rb_insert_color(&se->rb_node, tree);
40386470930SIngo Molnar }
40486470930SIngo Molnar 
40586470930SIngo Molnar static void print_sym_table(void)
40686470930SIngo Molnar {
40786470930SIngo Molnar 	int printed = 0, j;
40846ab9764SMike Galbraith 	int counter, snap = !display_weighted ? sym_counter : 0;
40986470930SIngo Molnar 	float samples_per_sec = samples/delay_secs;
41086470930SIngo Molnar 	float ksamples_per_sec = (samples-userspace_samples)/delay_secs;
41186470930SIngo Molnar 	float sum_ksamples = 0.0;
41286470930SIngo Molnar 	struct sym_entry *syme, *n;
41386470930SIngo Molnar 	struct rb_root tmp = RB_ROOT;
41486470930SIngo Molnar 	struct rb_node *nd;
41586470930SIngo Molnar 
41686470930SIngo Molnar 	samples = userspace_samples = 0;
41786470930SIngo Molnar 
41886470930SIngo Molnar 	/* Sort the active symbols */
41986470930SIngo Molnar 	pthread_mutex_lock(&active_symbols_lock);
42086470930SIngo Molnar 	syme = list_entry(active_symbols.next, struct sym_entry, node);
42186470930SIngo Molnar 	pthread_mutex_unlock(&active_symbols_lock);
42286470930SIngo Molnar 
42386470930SIngo Molnar 	list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
42446ab9764SMike Galbraith 		syme->snap_count = syme->count[snap];
42586470930SIngo Molnar 		if (syme->snap_count != 0) {
42686470930SIngo Molnar 			syme->weight = sym_weight(syme);
42786470930SIngo Molnar 			rb_insert_active_sym(&tmp, syme);
42886470930SIngo Molnar 			sum_ksamples += syme->snap_count;
42986470930SIngo Molnar 
43086470930SIngo Molnar 			for (j = 0; j < nr_counters; j++)
43186470930SIngo Molnar 				syme->count[j] = zero ? 0 : syme->count[j] * 7 / 8;
43286470930SIngo Molnar 		} else
43386470930SIngo Molnar 			list_remove_active_sym(syme);
43486470930SIngo Molnar 	}
43586470930SIngo Molnar 
43686470930SIngo Molnar 	puts(CONSOLE_CLEAR);
43786470930SIngo Molnar 
43886470930SIngo Molnar 	printf(
43986470930SIngo Molnar "------------------------------------------------------------------------------\n");
44086470930SIngo Molnar 	printf( "   PerfTop:%8.0f irqs/sec  kernel:%4.1f%% [",
44186470930SIngo Molnar 		samples_per_sec,
44286470930SIngo Molnar 		100.0 - (100.0*((samples_per_sec-ksamples_per_sec)/samples_per_sec)));
44386470930SIngo Molnar 
44446ab9764SMike Galbraith 	if (nr_counters == 1 || !display_weighted) {
4459cffa8d5SPaul Mackerras 		printf("%Ld", (u64)attrs[0].sample_period);
44686470930SIngo Molnar 		if (freq)
44786470930SIngo Molnar 			printf("Hz ");
44886470930SIngo Molnar 		else
44986470930SIngo Molnar 			printf(" ");
45086470930SIngo Molnar 	}
45186470930SIngo Molnar 
45246ab9764SMike Galbraith 	if (!display_weighted)
45346ab9764SMike Galbraith 		printf("%s", event_name(sym_counter));
45446ab9764SMike Galbraith 	else for (counter = 0; counter < nr_counters; counter++) {
45586470930SIngo Molnar 		if (counter)
45686470930SIngo Molnar 			printf("/");
45786470930SIngo Molnar 
45886470930SIngo Molnar 		printf("%s", event_name(counter));
45986470930SIngo Molnar 	}
46086470930SIngo Molnar 
46186470930SIngo Molnar 	printf( "], ");
46286470930SIngo Molnar 
46386470930SIngo Molnar 	if (target_pid != -1)
46486470930SIngo Molnar 		printf(" (target_pid: %d", target_pid);
46586470930SIngo Molnar 	else
46686470930SIngo Molnar 		printf(" (all");
46786470930SIngo Molnar 
46886470930SIngo Molnar 	if (profile_cpu != -1)
46986470930SIngo Molnar 		printf(", cpu: %d)\n", profile_cpu);
47086470930SIngo Molnar 	else {
47186470930SIngo Molnar 		if (target_pid != -1)
47286470930SIngo Molnar 			printf(")\n");
47386470930SIngo Molnar 		else
47486470930SIngo Molnar 			printf(", %d CPUs)\n", nr_cpus);
47586470930SIngo Molnar 	}
47686470930SIngo Molnar 
47786470930SIngo Molnar 	printf("------------------------------------------------------------------------------\n\n");
47886470930SIngo Molnar 
479923c42c1SMike Galbraith 	if (sym_filter_entry) {
480923c42c1SMike Galbraith 		show_details(sym_filter_entry);
481923c42c1SMike Galbraith 		return;
482923c42c1SMike Galbraith 	}
483923c42c1SMike Galbraith 
48486470930SIngo Molnar 	if (nr_counters == 1)
48586470930SIngo Molnar 		printf("             samples    pcnt");
48686470930SIngo Molnar 	else
48786470930SIngo Molnar 		printf("  weight     samples    pcnt");
48886470930SIngo Molnar 
48986470930SIngo Molnar 	printf("         RIP          kernel function\n"
49086470930SIngo Molnar 	       	       "  ______     _______   _____   ________________   _______________\n\n"
49186470930SIngo Molnar 	);
49286470930SIngo Molnar 
49386470930SIngo Molnar 	for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
49486470930SIngo Molnar 		struct sym_entry *syme = rb_entry(nd, struct sym_entry, rb_node);
49586470930SIngo Molnar 		struct symbol *sym = (struct symbol *)(syme + 1);
49686470930SIngo Molnar 		double pcnt;
49786470930SIngo Molnar 
498923c42c1SMike Galbraith 		if (++printed > print_entries || (int)syme->snap_count < count_filter)
49986470930SIngo Molnar 			continue;
50086470930SIngo Molnar 
50186470930SIngo Molnar 		pcnt = 100.0 - (100.0 * ((sum_ksamples - syme->snap_count) /
50286470930SIngo Molnar 					 sum_ksamples));
50386470930SIngo Molnar 
50446ab9764SMike Galbraith 		if (nr_counters == 1 || !display_weighted)
50586470930SIngo Molnar 			printf("%20.2f - ", syme->weight);
50686470930SIngo Molnar 		else
50786470930SIngo Molnar 			printf("%9.1f %10ld - ", syme->weight, syme->snap_count);
50886470930SIngo Molnar 
5091e11fd82SFrederic Weisbecker 		percent_color_fprintf(stdout, "%4.1f%%", pcnt);
51042976487SMike Galbraith 		printf(" - %016llx : %s", sym->start, sym->name);
51142976487SMike Galbraith 		if (sym->module)
51242976487SMike Galbraith 			printf("\t[%s]", sym->module->name);
51342976487SMike Galbraith 		printf("\n");
51486470930SIngo Molnar 	}
51586470930SIngo Molnar }
51686470930SIngo Molnar 
517923c42c1SMike Galbraith static void prompt_integer(int *target, const char *msg)
518923c42c1SMike Galbraith {
519923c42c1SMike Galbraith 	char *buf = malloc(0), *p;
520923c42c1SMike Galbraith 	size_t dummy = 0;
521923c42c1SMike Galbraith 	int tmp;
522923c42c1SMike Galbraith 
523923c42c1SMike Galbraith 	fprintf(stdout, "\n%s: ", msg);
524923c42c1SMike Galbraith 	if (getline(&buf, &dummy, stdin) < 0)
525923c42c1SMike Galbraith 		return;
526923c42c1SMike Galbraith 
527923c42c1SMike Galbraith 	p = strchr(buf, '\n');
528923c42c1SMike Galbraith 	if (p)
529923c42c1SMike Galbraith 		*p = 0;
530923c42c1SMike Galbraith 
531923c42c1SMike Galbraith 	p = buf;
532923c42c1SMike Galbraith 	while(*p) {
533923c42c1SMike Galbraith 		if (!isdigit(*p))
534923c42c1SMike Galbraith 			goto out_free;
535923c42c1SMike Galbraith 		p++;
536923c42c1SMike Galbraith 	}
537923c42c1SMike Galbraith 	tmp = strtoul(buf, NULL, 10);
538923c42c1SMike Galbraith 	*target = tmp;
539923c42c1SMike Galbraith out_free:
540923c42c1SMike Galbraith 	free(buf);
541923c42c1SMike Galbraith }
542923c42c1SMike Galbraith 
543923c42c1SMike Galbraith static void prompt_percent(int *target, const char *msg)
544923c42c1SMike Galbraith {
545923c42c1SMike Galbraith 	int tmp = 0;
546923c42c1SMike Galbraith 
547923c42c1SMike Galbraith 	prompt_integer(&tmp, msg);
548923c42c1SMike Galbraith 	if (tmp >= 0 && tmp <= 100)
549923c42c1SMike Galbraith 		*target = tmp;
550923c42c1SMike Galbraith }
551923c42c1SMike Galbraith 
552923c42c1SMike Galbraith static void prompt_symbol(struct sym_entry **target, const char *msg)
553923c42c1SMike Galbraith {
554923c42c1SMike Galbraith 	char *buf = malloc(0), *p;
555923c42c1SMike Galbraith 	struct sym_entry *syme = *target, *n, *found = NULL;
556923c42c1SMike Galbraith 	size_t dummy = 0;
557923c42c1SMike Galbraith 
558923c42c1SMike Galbraith 	/* zero counters of active symbol */
559923c42c1SMike Galbraith 	if (syme) {
560923c42c1SMike Galbraith 		pthread_mutex_lock(&syme->source_lock);
561923c42c1SMike Galbraith 		__zero_source_counters(syme);
562923c42c1SMike Galbraith 		*target = NULL;
563923c42c1SMike Galbraith 		pthread_mutex_unlock(&syme->source_lock);
564923c42c1SMike Galbraith 	}
565923c42c1SMike Galbraith 
566923c42c1SMike Galbraith 	fprintf(stdout, "\n%s: ", msg);
567923c42c1SMike Galbraith 	if (getline(&buf, &dummy, stdin) < 0)
568923c42c1SMike Galbraith 		goto out_free;
569923c42c1SMike Galbraith 
570923c42c1SMike Galbraith 	p = strchr(buf, '\n');
571923c42c1SMike Galbraith 	if (p)
572923c42c1SMike Galbraith 		*p = 0;
573923c42c1SMike Galbraith 
574923c42c1SMike Galbraith 	pthread_mutex_lock(&active_symbols_lock);
575923c42c1SMike Galbraith 	syme = list_entry(active_symbols.next, struct sym_entry, node);
576923c42c1SMike Galbraith 	pthread_mutex_unlock(&active_symbols_lock);
577923c42c1SMike Galbraith 
578923c42c1SMike Galbraith 	list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
579923c42c1SMike Galbraith 		struct symbol *sym = (struct symbol *)(syme + 1);
580923c42c1SMike Galbraith 
581923c42c1SMike Galbraith 		if (!strcmp(buf, sym->name)) {
582923c42c1SMike Galbraith 			found = syme;
583923c42c1SMike Galbraith 			break;
584923c42c1SMike Galbraith 		}
585923c42c1SMike Galbraith 	}
586923c42c1SMike Galbraith 
587923c42c1SMike Galbraith 	if (!found) {
588923c42c1SMike Galbraith 		fprintf(stderr, "Sorry, %s is not active.\n", sym_filter);
589923c42c1SMike Galbraith 		sleep(1);
590923c42c1SMike Galbraith 		return;
591923c42c1SMike Galbraith 	} else
592923c42c1SMike Galbraith 		parse_source(found);
593923c42c1SMike Galbraith 
594923c42c1SMike Galbraith out_free:
595923c42c1SMike Galbraith 	free(buf);
596923c42c1SMike Galbraith }
597923c42c1SMike Galbraith 
598091bd2e9SMike Galbraith static void print_mapped_keys(void)
599923c42c1SMike Galbraith {
600091bd2e9SMike Galbraith 	char *name = NULL;
601091bd2e9SMike Galbraith 
602091bd2e9SMike Galbraith 	if (sym_filter_entry) {
603091bd2e9SMike Galbraith 		struct symbol *sym = (struct symbol *)(sym_filter_entry+1);
604091bd2e9SMike Galbraith 		name = sym->name;
605091bd2e9SMike Galbraith 	}
606091bd2e9SMike Galbraith 
607091bd2e9SMike Galbraith 	fprintf(stdout, "\nMapped keys:\n");
608091bd2e9SMike Galbraith 	fprintf(stdout, "\t[d]     display refresh delay.             \t(%d)\n", delay_secs);
609091bd2e9SMike Galbraith 	fprintf(stdout, "\t[e]     display entries (lines).           \t(%d)\n", print_entries);
610091bd2e9SMike Galbraith 
611091bd2e9SMike Galbraith 	if (nr_counters > 1)
61246ab9764SMike Galbraith 		fprintf(stdout, "\t[E]     active event counter.              \t(%s)\n", event_name(sym_counter));
613091bd2e9SMike Galbraith 
614091bd2e9SMike Galbraith 	fprintf(stdout, "\t[f]     profile display filter (count).    \t(%d)\n", count_filter);
615091bd2e9SMike Galbraith 
616091bd2e9SMike Galbraith 	if (vmlinux) {
617091bd2e9SMike Galbraith 		fprintf(stdout, "\t[F]     annotate display filter (percent). \t(%d%%)\n", sym_pcnt_filter);
618091bd2e9SMike Galbraith 		fprintf(stdout, "\t[s]     annotate symbol.                   \t(%s)\n", name?: "NULL");
619091bd2e9SMike Galbraith 		fprintf(stdout, "\t[S]     stop annotation.\n");
620091bd2e9SMike Galbraith 	}
621091bd2e9SMike Galbraith 
622091bd2e9SMike Galbraith 	if (nr_counters > 1)
62346ab9764SMike Galbraith 		fprintf(stdout, "\t[w]     toggle display weighted/count[E]r. \t(%d)\n", display_weighted ? 1 : 0);
624091bd2e9SMike Galbraith 
62546ab9764SMike Galbraith 	fprintf(stdout, "\t[z]     toggle sample zeroing.             \t(%d)\n", zero ? 1 : 0);
626091bd2e9SMike Galbraith 	fprintf(stdout, "\t[qQ]    quit.\n");
627091bd2e9SMike Galbraith }
628091bd2e9SMike Galbraith 
629091bd2e9SMike Galbraith static int key_mapped(int c)
630091bd2e9SMike Galbraith {
631091bd2e9SMike Galbraith 	switch (c) {
632091bd2e9SMike Galbraith 		case 'd':
633091bd2e9SMike Galbraith 		case 'e':
634091bd2e9SMike Galbraith 		case 'f':
635091bd2e9SMike Galbraith 		case 'z':
636091bd2e9SMike Galbraith 		case 'q':
637091bd2e9SMike Galbraith 		case 'Q':
638091bd2e9SMike Galbraith 			return 1;
639091bd2e9SMike Galbraith 		case 'E':
640091bd2e9SMike Galbraith 		case 'w':
641091bd2e9SMike Galbraith 			return nr_counters > 1 ? 1 : 0;
642091bd2e9SMike Galbraith 		case 'F':
643091bd2e9SMike Galbraith 		case 's':
644091bd2e9SMike Galbraith 		case 'S':
645091bd2e9SMike Galbraith 			return vmlinux ? 1 : 0;
646091bd2e9SMike Galbraith 	}
647091bd2e9SMike Galbraith 
648091bd2e9SMike Galbraith 	return 0;
649923c42c1SMike Galbraith }
650923c42c1SMike Galbraith 
651923c42c1SMike Galbraith static void handle_keypress(int c)
652923c42c1SMike Galbraith {
653091bd2e9SMike Galbraith 	if (!key_mapped(c)) {
654091bd2e9SMike Galbraith 		struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
655091bd2e9SMike Galbraith 		struct termios tc, save;
656091bd2e9SMike Galbraith 
657091bd2e9SMike Galbraith 		print_mapped_keys();
658091bd2e9SMike Galbraith 		fprintf(stdout, "\nEnter selection, or unmapped key to continue: ");
659091bd2e9SMike Galbraith 		fflush(stdout);
660091bd2e9SMike Galbraith 
661091bd2e9SMike Galbraith 		tcgetattr(0, &save);
662091bd2e9SMike Galbraith 		tc = save;
663091bd2e9SMike Galbraith 		tc.c_lflag &= ~(ICANON | ECHO);
664091bd2e9SMike Galbraith 		tc.c_cc[VMIN] = 0;
665091bd2e9SMike Galbraith 		tc.c_cc[VTIME] = 0;
666091bd2e9SMike Galbraith 		tcsetattr(0, TCSANOW, &tc);
667091bd2e9SMike Galbraith 
668091bd2e9SMike Galbraith 		poll(&stdin_poll, 1, -1);
669091bd2e9SMike Galbraith 		c = getc(stdin);
670091bd2e9SMike Galbraith 
671091bd2e9SMike Galbraith 		tcsetattr(0, TCSAFLUSH, &save);
672091bd2e9SMike Galbraith 		if (!key_mapped(c))
673091bd2e9SMike Galbraith 			return;
674091bd2e9SMike Galbraith 	}
675091bd2e9SMike Galbraith 
676923c42c1SMike Galbraith 	switch (c) {
677923c42c1SMike Galbraith 		case 'd':
678923c42c1SMike Galbraith 			prompt_integer(&delay_secs, "Enter display delay");
679923c42c1SMike Galbraith 			break;
680923c42c1SMike Galbraith 		case 'e':
681923c42c1SMike Galbraith 			prompt_integer(&print_entries, "Enter display entries (lines)");
682923c42c1SMike Galbraith 			break;
683923c42c1SMike Galbraith 		case 'E':
684923c42c1SMike Galbraith 			if (nr_counters > 1) {
685923c42c1SMike Galbraith 				int i;
686923c42c1SMike Galbraith 
687923c42c1SMike Galbraith 				fprintf(stderr, "\nAvailable events:");
688923c42c1SMike Galbraith 				for (i = 0; i < nr_counters; i++)
689923c42c1SMike Galbraith 					fprintf(stderr, "\n\t%d %s", i, event_name(i));
690923c42c1SMike Galbraith 
691923c42c1SMike Galbraith 				prompt_integer(&sym_counter, "Enter details event counter");
692923c42c1SMike Galbraith 
693923c42c1SMike Galbraith 				if (sym_counter >= nr_counters) {
694923c42c1SMike Galbraith 					fprintf(stderr, "Sorry, no such event, using %s.\n", event_name(0));
695923c42c1SMike Galbraith 					sym_counter = 0;
696923c42c1SMike Galbraith 					sleep(1);
697923c42c1SMike Galbraith 				}
698923c42c1SMike Galbraith 			} else sym_counter = 0;
699923c42c1SMike Galbraith 			break;
700923c42c1SMike Galbraith 		case 'f':
701923c42c1SMike Galbraith 			prompt_integer(&count_filter, "Enter display event count filter");
702923c42c1SMike Galbraith 			break;
703923c42c1SMike Galbraith 		case 'F':
704923c42c1SMike Galbraith 			prompt_percent(&sym_pcnt_filter, "Enter details display event filter (percent)");
705923c42c1SMike Galbraith 			break;
706923c42c1SMike Galbraith 		case 'q':
707923c42c1SMike Galbraith 		case 'Q':
708923c42c1SMike Galbraith 			printf("exiting.\n");
709923c42c1SMike Galbraith 			exit(0);
710923c42c1SMike Galbraith 		case 's':
711923c42c1SMike Galbraith 			prompt_symbol(&sym_filter_entry, "Enter details symbol");
712923c42c1SMike Galbraith 			break;
713923c42c1SMike Galbraith 		case 'S':
714923c42c1SMike Galbraith 			if (!sym_filter_entry)
715923c42c1SMike Galbraith 				break;
716923c42c1SMike Galbraith 			else {
717923c42c1SMike Galbraith 				struct sym_entry *syme = sym_filter_entry;
718923c42c1SMike Galbraith 
719923c42c1SMike Galbraith 				pthread_mutex_lock(&syme->source_lock);
720923c42c1SMike Galbraith 				sym_filter_entry = NULL;
721923c42c1SMike Galbraith 				__zero_source_counters(syme);
722923c42c1SMike Galbraith 				pthread_mutex_unlock(&syme->source_lock);
723923c42c1SMike Galbraith 			}
724923c42c1SMike Galbraith 			break;
72546ab9764SMike Galbraith 		case 'w':
72646ab9764SMike Galbraith 			display_weighted = ~display_weighted;
72746ab9764SMike Galbraith 			break;
728923c42c1SMike Galbraith 		case 'z':
729923c42c1SMike Galbraith 			zero = ~zero;
730923c42c1SMike Galbraith 			break;
731923c42c1SMike Galbraith 	}
732923c42c1SMike Galbraith }
733923c42c1SMike Galbraith 
734f37a291cSIngo Molnar static void *display_thread(void *arg __used)
73586470930SIngo Molnar {
73686470930SIngo Molnar 	struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
737923c42c1SMike Galbraith 	struct termios tc, save;
738923c42c1SMike Galbraith 	int delay_msecs, c;
73986470930SIngo Molnar 
740923c42c1SMike Galbraith 	tcgetattr(0, &save);
741923c42c1SMike Galbraith 	tc = save;
742923c42c1SMike Galbraith 	tc.c_lflag &= ~(ICANON | ECHO);
743923c42c1SMike Galbraith 	tc.c_cc[VMIN] = 0;
744923c42c1SMike Galbraith 	tc.c_cc[VTIME] = 0;
745091bd2e9SMike Galbraith 
746923c42c1SMike Galbraith repeat:
747923c42c1SMike Galbraith 	delay_msecs = delay_secs * 1000;
748923c42c1SMike Galbraith 	tcsetattr(0, TCSANOW, &tc);
749923c42c1SMike Galbraith 	/* trash return*/
750923c42c1SMike Galbraith 	getc(stdin);
75186470930SIngo Molnar 
75286470930SIngo Molnar 	do {
75386470930SIngo Molnar 		print_sym_table();
75486470930SIngo Molnar 	} while (!poll(&stdin_poll, 1, delay_msecs) == 1);
75586470930SIngo Molnar 
756923c42c1SMike Galbraith 	c = getc(stdin);
757923c42c1SMike Galbraith 	tcsetattr(0, TCSAFLUSH, &save);
758923c42c1SMike Galbraith 
759923c42c1SMike Galbraith 	handle_keypress(c);
760923c42c1SMike Galbraith 	goto repeat;
76186470930SIngo Molnar 
76286470930SIngo Molnar 	return NULL;
76386470930SIngo Molnar }
76486470930SIngo Molnar 
7652ab52083SAnton Blanchard /* Tag samples to be skipped. */
766f37a291cSIngo Molnar static const char *skip_symbols[] = {
7672ab52083SAnton Blanchard 	"default_idle",
7682ab52083SAnton Blanchard 	"cpu_idle",
7692ab52083SAnton Blanchard 	"enter_idle",
7702ab52083SAnton Blanchard 	"exit_idle",
7712ab52083SAnton Blanchard 	"mwait_idle",
77259b90056SArnaldo Carvalho de Melo 	"mwait_idle_with_hints",
7733a3393efSAnton Blanchard 	"ppc64_runlatch_off",
7743a3393efSAnton Blanchard 	"pseries_dedicated_idle_sleep",
7752ab52083SAnton Blanchard 	NULL
7762ab52083SAnton Blanchard };
7772ab52083SAnton Blanchard 
77886470930SIngo Molnar static int symbol_filter(struct dso *self, struct symbol *sym)
77986470930SIngo Molnar {
78086470930SIngo Molnar 	struct sym_entry *syme;
78186470930SIngo Molnar 	const char *name = sym->name;
7822ab52083SAnton Blanchard 	int i;
78386470930SIngo Molnar 
7843a3393efSAnton Blanchard 	/*
7853a3393efSAnton Blanchard 	 * ppc64 uses function descriptors and appends a '.' to the
7863a3393efSAnton Blanchard 	 * start of every instruction address. Remove it.
7873a3393efSAnton Blanchard 	 */
7883a3393efSAnton Blanchard 	if (name[0] == '.')
7893a3393efSAnton Blanchard 		name++;
7903a3393efSAnton Blanchard 
79186470930SIngo Molnar 	if (!strcmp(name, "_text") ||
79286470930SIngo Molnar 	    !strcmp(name, "_etext") ||
79386470930SIngo Molnar 	    !strcmp(name, "_sinittext") ||
79486470930SIngo Molnar 	    !strncmp("init_module", name, 11) ||
79586470930SIngo Molnar 	    !strncmp("cleanup_module", name, 14) ||
79686470930SIngo Molnar 	    strstr(name, "_text_start") ||
79786470930SIngo Molnar 	    strstr(name, "_text_end"))
79886470930SIngo Molnar 		return 1;
79986470930SIngo Molnar 
80086470930SIngo Molnar 	syme = dso__sym_priv(self, sym);
801923c42c1SMike Galbraith 	pthread_mutex_init(&syme->source_lock, NULL);
802923c42c1SMike Galbraith 	if (!sym_filter_entry && sym_filter && !strcmp(name, sym_filter))
803923c42c1SMike Galbraith 		sym_filter_entry = syme;
804923c42c1SMike Galbraith 
8052ab52083SAnton Blanchard 	for (i = 0; skip_symbols[i]; i++) {
8062ab52083SAnton Blanchard 		if (!strcmp(skip_symbols[i], name)) {
80786470930SIngo Molnar 			syme->skip = 1;
8082ab52083SAnton Blanchard 			break;
8092ab52083SAnton Blanchard 		}
8102ab52083SAnton Blanchard 	}
81186470930SIngo Molnar 
81286470930SIngo Molnar 	return 0;
81386470930SIngo Molnar }
81486470930SIngo Molnar 
81586470930SIngo Molnar static int parse_symbols(void)
81686470930SIngo Molnar {
81786470930SIngo Molnar 	struct rb_node *node;
81886470930SIngo Molnar 	struct symbol  *sym;
81942976487SMike Galbraith 	int modules = vmlinux ? 1 : 0;
82086470930SIngo Molnar 
82186470930SIngo Molnar 	kernel_dso = dso__new("[kernel]", sizeof(struct sym_entry));
82286470930SIngo Molnar 	if (kernel_dso == NULL)
82386470930SIngo Molnar 		return -1;
82486470930SIngo Molnar 
82542976487SMike Galbraith 	if (dso__load_kernel(kernel_dso, vmlinux, symbol_filter, verbose, modules) <= 0)
82686470930SIngo Molnar 		goto out_delete_dso;
82786470930SIngo Molnar 
82886470930SIngo Molnar 	node = rb_first(&kernel_dso->syms);
82986470930SIngo Molnar 	sym = rb_entry(node, struct symbol, rb_node);
83086470930SIngo Molnar 	min_ip = sym->start;
83186470930SIngo Molnar 
83286470930SIngo Molnar 	node = rb_last(&kernel_dso->syms);
83386470930SIngo Molnar 	sym = rb_entry(node, struct symbol, rb_node);
83486470930SIngo Molnar 	max_ip = sym->end;
83586470930SIngo Molnar 
83686470930SIngo Molnar 	if (dump_symtab)
83786470930SIngo Molnar 		dso__fprintf(kernel_dso, stderr);
83886470930SIngo Molnar 
83986470930SIngo Molnar 	return 0;
84086470930SIngo Molnar 
84186470930SIngo Molnar out_delete_dso:
84286470930SIngo Molnar 	dso__delete(kernel_dso);
84386470930SIngo Molnar 	kernel_dso = NULL;
84486470930SIngo Molnar 	return -1;
84586470930SIngo Molnar }
84686470930SIngo Molnar 
84786470930SIngo Molnar /*
84886470930SIngo Molnar  * Binary search in the histogram table and record the hit:
84986470930SIngo Molnar  */
8509cffa8d5SPaul Mackerras static void record_ip(u64 ip, int counter)
85186470930SIngo Molnar {
85286470930SIngo Molnar 	struct symbol *sym = dso__find_symbol(kernel_dso, ip);
85386470930SIngo Molnar 
85486470930SIngo Molnar 	if (sym != NULL) {
85586470930SIngo Molnar 		struct sym_entry *syme = dso__sym_priv(kernel_dso, sym);
85686470930SIngo Molnar 
85786470930SIngo Molnar 		if (!syme->skip) {
85886470930SIngo Molnar 			syme->count[counter]++;
859923c42c1SMike Galbraith 			record_precise_ip(syme, counter, ip);
86086470930SIngo Molnar 			pthread_mutex_lock(&active_symbols_lock);
86186470930SIngo Molnar 			if (list_empty(&syme->node) || !syme->node.next)
86286470930SIngo Molnar 				__list_insert_active_sym(syme);
86386470930SIngo Molnar 			pthread_mutex_unlock(&active_symbols_lock);
86486470930SIngo Molnar 			return;
86586470930SIngo Molnar 		}
86686470930SIngo Molnar 	}
86786470930SIngo Molnar 
86886470930SIngo Molnar 	samples--;
86986470930SIngo Molnar }
87086470930SIngo Molnar 
871e6e18ec7SPeter Zijlstra static void process_event(u64 ip, int counter, int user)
87286470930SIngo Molnar {
87386470930SIngo Molnar 	samples++;
87486470930SIngo Molnar 
875e6e18ec7SPeter Zijlstra 	if (user) {
87686470930SIngo Molnar 		userspace_samples++;
87786470930SIngo Molnar 		return;
87886470930SIngo Molnar 	}
87986470930SIngo Molnar 
88086470930SIngo Molnar 	record_ip(ip, counter);
88186470930SIngo Molnar }
88286470930SIngo Molnar 
88386470930SIngo Molnar struct mmap_data {
88486470930SIngo Molnar 	int			counter;
88586470930SIngo Molnar 	void			*base;
886f37a291cSIngo Molnar 	int			mask;
88786470930SIngo Molnar 	unsigned int		prev;
88886470930SIngo Molnar };
88986470930SIngo Molnar 
89086470930SIngo Molnar static unsigned int mmap_read_head(struct mmap_data *md)
89186470930SIngo Molnar {
89286470930SIngo Molnar 	struct perf_counter_mmap_page *pc = md->base;
89386470930SIngo Molnar 	int head;
89486470930SIngo Molnar 
89586470930SIngo Molnar 	head = pc->data_head;
89686470930SIngo Molnar 	rmb();
89786470930SIngo Molnar 
89886470930SIngo Molnar 	return head;
89986470930SIngo Molnar }
90086470930SIngo Molnar 
90186470930SIngo Molnar struct timeval last_read, this_read;
90286470930SIngo Molnar 
9032f01190aSFrederic Weisbecker static void mmap_read_counter(struct mmap_data *md)
90486470930SIngo Molnar {
90586470930SIngo Molnar 	unsigned int head = mmap_read_head(md);
90686470930SIngo Molnar 	unsigned int old = md->prev;
90786470930SIngo Molnar 	unsigned char *data = md->base + page_size;
90886470930SIngo Molnar 	int diff;
90986470930SIngo Molnar 
91086470930SIngo Molnar 	gettimeofday(&this_read, NULL);
91186470930SIngo Molnar 
91286470930SIngo Molnar 	/*
91386470930SIngo Molnar 	 * If we're further behind than half the buffer, there's a chance
91486470930SIngo Molnar 	 * the writer will bite our tail and mess up the samples under us.
91586470930SIngo Molnar 	 *
91686470930SIngo Molnar 	 * If we somehow ended up ahead of the head, we got messed up.
91786470930SIngo Molnar 	 *
91886470930SIngo Molnar 	 * In either case, truncate and restart at head.
91986470930SIngo Molnar 	 */
92086470930SIngo Molnar 	diff = head - old;
92186470930SIngo Molnar 	if (diff > md->mask / 2 || diff < 0) {
92286470930SIngo Molnar 		struct timeval iv;
92386470930SIngo Molnar 		unsigned long msecs;
92486470930SIngo Molnar 
92586470930SIngo Molnar 		timersub(&this_read, &last_read, &iv);
92686470930SIngo Molnar 		msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
92786470930SIngo Molnar 
92886470930SIngo Molnar 		fprintf(stderr, "WARNING: failed to keep up with mmap data."
92986470930SIngo Molnar 				"  Last read %lu msecs ago.\n", msecs);
93086470930SIngo Molnar 
93186470930SIngo Molnar 		/*
93286470930SIngo Molnar 		 * head points to a known good entry, start there.
93386470930SIngo Molnar 		 */
93486470930SIngo Molnar 		old = head;
93586470930SIngo Molnar 	}
93686470930SIngo Molnar 
93786470930SIngo Molnar 	last_read = this_read;
93886470930SIngo Molnar 
93986470930SIngo Molnar 	for (; old != head;) {
94086470930SIngo Molnar 		struct ip_event {
94186470930SIngo Molnar 			struct perf_event_header header;
9429cffa8d5SPaul Mackerras 			u64 ip;
9439cffa8d5SPaul Mackerras 			u32 pid, target_pid;
94486470930SIngo Molnar 		};
94586470930SIngo Molnar 		struct mmap_event {
94686470930SIngo Molnar 			struct perf_event_header header;
9479cffa8d5SPaul Mackerras 			u32 pid, target_pid;
9489cffa8d5SPaul Mackerras 			u64 start;
9499cffa8d5SPaul Mackerras 			u64 len;
9509cffa8d5SPaul Mackerras 			u64 pgoff;
95186470930SIngo Molnar 			char filename[PATH_MAX];
95286470930SIngo Molnar 		};
95386470930SIngo Molnar 
95486470930SIngo Molnar 		typedef union event_union {
95586470930SIngo Molnar 			struct perf_event_header header;
95686470930SIngo Molnar 			struct ip_event ip;
95786470930SIngo Molnar 			struct mmap_event mmap;
95886470930SIngo Molnar 		} event_t;
95986470930SIngo Molnar 
96086470930SIngo Molnar 		event_t *event = (event_t *)&data[old & md->mask];
96186470930SIngo Molnar 
96286470930SIngo Molnar 		event_t event_copy;
96386470930SIngo Molnar 
96486470930SIngo Molnar 		size_t size = event->header.size;
96586470930SIngo Molnar 
96686470930SIngo Molnar 		/*
96786470930SIngo Molnar 		 * Event straddles the mmap boundary -- header should always
96886470930SIngo Molnar 		 * be inside due to u64 alignment of output.
96986470930SIngo Molnar 		 */
97086470930SIngo Molnar 		if ((old & md->mask) + size != ((old + size) & md->mask)) {
97186470930SIngo Molnar 			unsigned int offset = old;
97286470930SIngo Molnar 			unsigned int len = min(sizeof(*event), size), cpy;
97386470930SIngo Molnar 			void *dst = &event_copy;
97486470930SIngo Molnar 
97586470930SIngo Molnar 			do {
97686470930SIngo Molnar 				cpy = min(md->mask + 1 - (offset & md->mask), len);
97786470930SIngo Molnar 				memcpy(dst, &data[offset & md->mask], cpy);
97886470930SIngo Molnar 				offset += cpy;
97986470930SIngo Molnar 				dst += cpy;
98086470930SIngo Molnar 				len -= cpy;
98186470930SIngo Molnar 			} while (len);
98286470930SIngo Molnar 
98386470930SIngo Molnar 			event = &event_copy;
98486470930SIngo Molnar 		}
98586470930SIngo Molnar 
98686470930SIngo Molnar 		old += size;
98786470930SIngo Molnar 
988e6e18ec7SPeter Zijlstra 		if (event->header.type == PERF_EVENT_SAMPLE) {
989e6e18ec7SPeter Zijlstra 			int user =
990e6e18ec7SPeter Zijlstra 	(event->header.misc & PERF_EVENT_MISC_CPUMODE_MASK) == PERF_EVENT_MISC_USER;
991e6e18ec7SPeter Zijlstra 			process_event(event->ip.ip, md->counter, user);
99286470930SIngo Molnar 		}
99386470930SIngo Molnar 	}
99486470930SIngo Molnar 
99586470930SIngo Molnar 	md->prev = old;
99686470930SIngo Molnar }
99786470930SIngo Molnar 
99886470930SIngo Molnar static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
99986470930SIngo Molnar static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
100086470930SIngo Molnar 
10012f01190aSFrederic Weisbecker static void mmap_read(void)
10022f01190aSFrederic Weisbecker {
10032f01190aSFrederic Weisbecker 	int i, counter;
10042f01190aSFrederic Weisbecker 
10052f01190aSFrederic Weisbecker 	for (i = 0; i < nr_cpus; i++) {
10062f01190aSFrederic Weisbecker 		for (counter = 0; counter < nr_counters; counter++)
10072f01190aSFrederic Weisbecker 			mmap_read_counter(&mmap_array[i][counter]);
10082f01190aSFrederic Weisbecker 	}
10092f01190aSFrederic Weisbecker }
10102f01190aSFrederic Weisbecker 
1011716c69feSIngo Molnar int nr_poll;
1012716c69feSIngo Molnar int group_fd;
1013716c69feSIngo Molnar 
1014716c69feSIngo Molnar static void start_counter(int i, int counter)
101586470930SIngo Molnar {
101686470930SIngo Molnar 	struct perf_counter_attr *attr;
10170fdc7e67SMike Galbraith 	int cpu;
101886470930SIngo Molnar 
101986470930SIngo Molnar 	cpu = profile_cpu;
102086470930SIngo Molnar 	if (target_pid == -1 && profile_cpu == -1)
102186470930SIngo Molnar 		cpu = i;
102286470930SIngo Molnar 
102386470930SIngo Molnar 	attr = attrs + counter;
102486470930SIngo Molnar 
102586470930SIngo Molnar 	attr->sample_type	= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
102686470930SIngo Molnar 	attr->freq		= freq;
10270fdc7e67SMike Galbraith 	attr->inherit		= (cpu < 0) && inherit;
102886470930SIngo Molnar 
1029716c69feSIngo Molnar try_again:
103086470930SIngo Molnar 	fd[i][counter] = sys_perf_counter_open(attr, target_pid, cpu, group_fd, 0);
1031716c69feSIngo Molnar 
103286470930SIngo Molnar 	if (fd[i][counter] < 0) {
103386470930SIngo Molnar 		int err = errno;
103486470930SIngo Molnar 
103586470930SIngo Molnar 		if (err == EPERM)
1036716c69feSIngo Molnar 			die("No permission - are you root?\n");
1037716c69feSIngo Molnar 		/*
1038716c69feSIngo Molnar 		 * If it's cycles then fall back to hrtimer
1039716c69feSIngo Molnar 		 * based cpu-clock-tick sw counter, which
1040716c69feSIngo Molnar 		 * is always available even if no PMU support:
1041716c69feSIngo Molnar 		 */
1042716c69feSIngo Molnar 		if (attr->type == PERF_TYPE_HARDWARE
1043f4dbfa8fSPeter Zijlstra 			&& attr->config == PERF_COUNT_HW_CPU_CYCLES) {
1044716c69feSIngo Molnar 
10453da297a6SIngo Molnar 			if (verbose)
1046716c69feSIngo Molnar 				warning(" ... trying to fall back to cpu-clock-ticks\n");
10473da297a6SIngo Molnar 
1048716c69feSIngo Molnar 			attr->type = PERF_TYPE_SOFTWARE;
1049f4dbfa8fSPeter Zijlstra 			attr->config = PERF_COUNT_SW_CPU_CLOCK;
1050716c69feSIngo Molnar 			goto try_again;
1051716c69feSIngo Molnar 		}
105230c806a0SIngo Molnar 		printf("\n");
105330c806a0SIngo Molnar 		error("perfcounter syscall returned with %d (%s)\n",
105430c806a0SIngo Molnar 			fd[i][counter], strerror(err));
105530c806a0SIngo Molnar 		die("No CONFIG_PERF_COUNTERS=y kernel support configured?\n");
105686470930SIngo Molnar 		exit(-1);
105786470930SIngo Molnar 	}
105886470930SIngo Molnar 	assert(fd[i][counter] >= 0);
105986470930SIngo Molnar 	fcntl(fd[i][counter], F_SETFL, O_NONBLOCK);
106086470930SIngo Molnar 
106186470930SIngo Molnar 	/*
106286470930SIngo Molnar 	 * First counter acts as the group leader:
106386470930SIngo Molnar 	 */
106486470930SIngo Molnar 	if (group && group_fd == -1)
106586470930SIngo Molnar 		group_fd = fd[i][counter];
106686470930SIngo Molnar 
106786470930SIngo Molnar 	event_array[nr_poll].fd = fd[i][counter];
106886470930SIngo Molnar 	event_array[nr_poll].events = POLLIN;
106986470930SIngo Molnar 	nr_poll++;
107086470930SIngo Molnar 
107186470930SIngo Molnar 	mmap_array[i][counter].counter = counter;
107286470930SIngo Molnar 	mmap_array[i][counter].prev = 0;
107386470930SIngo Molnar 	mmap_array[i][counter].mask = mmap_pages*page_size - 1;
107486470930SIngo Molnar 	mmap_array[i][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
107586470930SIngo Molnar 			PROT_READ, MAP_SHARED, fd[i][counter], 0);
107686470930SIngo Molnar 	if (mmap_array[i][counter].base == MAP_FAILED)
107786470930SIngo Molnar 		die("failed to mmap with %d (%s)\n", errno, strerror(errno));
107886470930SIngo Molnar }
1079716c69feSIngo Molnar 
1080716c69feSIngo Molnar static int __cmd_top(void)
1081716c69feSIngo Molnar {
1082716c69feSIngo Molnar 	pthread_t thread;
1083716c69feSIngo Molnar 	int i, counter;
1084716c69feSIngo Molnar 	int ret;
1085716c69feSIngo Molnar 
1086716c69feSIngo Molnar 	for (i = 0; i < nr_cpus; i++) {
1087716c69feSIngo Molnar 		group_fd = -1;
1088716c69feSIngo Molnar 		for (counter = 0; counter < nr_counters; counter++)
1089716c69feSIngo Molnar 			start_counter(i, counter);
109086470930SIngo Molnar 	}
109186470930SIngo Molnar 
10922f01190aSFrederic Weisbecker 	/* Wait for a minimal set of events before starting the snapshot */
10932f01190aSFrederic Weisbecker 	poll(event_array, nr_poll, 100);
10942f01190aSFrederic Weisbecker 
10952f01190aSFrederic Weisbecker 	mmap_read();
10962f01190aSFrederic Weisbecker 
109786470930SIngo Molnar 	if (pthread_create(&thread, NULL, display_thread, NULL)) {
109886470930SIngo Molnar 		printf("Could not create display thread.\n");
109986470930SIngo Molnar 		exit(-1);
110086470930SIngo Molnar 	}
110186470930SIngo Molnar 
110286470930SIngo Molnar 	if (realtime_prio) {
110386470930SIngo Molnar 		struct sched_param param;
110486470930SIngo Molnar 
110586470930SIngo Molnar 		param.sched_priority = realtime_prio;
110686470930SIngo Molnar 		if (sched_setscheduler(0, SCHED_FIFO, &param)) {
110786470930SIngo Molnar 			printf("Could not set realtime priority.\n");
110886470930SIngo Molnar 			exit(-1);
110986470930SIngo Molnar 		}
111086470930SIngo Molnar 	}
111186470930SIngo Molnar 
111286470930SIngo Molnar 	while (1) {
111386470930SIngo Molnar 		int hits = samples;
111486470930SIngo Molnar 
11152f01190aSFrederic Weisbecker 		mmap_read();
111686470930SIngo Molnar 
111786470930SIngo Molnar 		if (hits == samples)
111886470930SIngo Molnar 			ret = poll(event_array, nr_poll, 100);
111986470930SIngo Molnar 	}
112086470930SIngo Molnar 
112186470930SIngo Molnar 	return 0;
112286470930SIngo Molnar }
112386470930SIngo Molnar 
112486470930SIngo Molnar static const char * const top_usage[] = {
112586470930SIngo Molnar 	"perf top [<options>]",
112686470930SIngo Molnar 	NULL
112786470930SIngo Molnar };
112886470930SIngo Molnar 
112986470930SIngo Molnar static const struct option options[] = {
113086470930SIngo Molnar 	OPT_CALLBACK('e', "event", NULL, "event",
113186470930SIngo Molnar 		     "event selector. use 'perf list' to list available events",
113286470930SIngo Molnar 		     parse_events),
113386470930SIngo Molnar 	OPT_INTEGER('c', "count", &default_interval,
113486470930SIngo Molnar 		    "event period to sample"),
113586470930SIngo Molnar 	OPT_INTEGER('p', "pid", &target_pid,
113686470930SIngo Molnar 		    "profile events on existing pid"),
113786470930SIngo Molnar 	OPT_BOOLEAN('a', "all-cpus", &system_wide,
113886470930SIngo Molnar 			    "system-wide collection from all CPUs"),
113986470930SIngo Molnar 	OPT_INTEGER('C', "CPU", &profile_cpu,
114086470930SIngo Molnar 		    "CPU to profile on"),
114142976487SMike Galbraith 	OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
114286470930SIngo Molnar 	OPT_INTEGER('m', "mmap-pages", &mmap_pages,
114386470930SIngo Molnar 		    "number of mmap data pages"),
114486470930SIngo Molnar 	OPT_INTEGER('r', "realtime", &realtime_prio,
114586470930SIngo Molnar 		    "collect data with this RT SCHED_FIFO priority"),
114686470930SIngo Molnar 	OPT_INTEGER('d', "delay", &delay_secs,
114786470930SIngo Molnar 		    "number of seconds to delay between refreshes"),
114886470930SIngo Molnar 	OPT_BOOLEAN('D', "dump-symtab", &dump_symtab,
114986470930SIngo Molnar 			    "dump the symbol table used for profiling"),
115086470930SIngo Molnar 	OPT_INTEGER('f', "count-filter", &count_filter,
115186470930SIngo Molnar 		    "only display functions with more events than this"),
115286470930SIngo Molnar 	OPT_BOOLEAN('g', "group", &group,
115386470930SIngo Molnar 			    "put the counters into a counter group"),
11540fdc7e67SMike Galbraith 	OPT_BOOLEAN('i', "inherit", &inherit,
11550fdc7e67SMike Galbraith 		    "child tasks inherit counters"),
1156923c42c1SMike Galbraith 	OPT_STRING('s', "sym-annotate", &sym_filter, "symbol name",
1157923c42c1SMike Galbraith 		    "symbol to annotate - requires -k option"),
11581f208ea6SAnton Blanchard 	OPT_BOOLEAN('z', "zero", &zero,
115986470930SIngo Molnar 		    "zero history across updates"),
116086470930SIngo Molnar 	OPT_INTEGER('F', "freq", &freq,
116186470930SIngo Molnar 		    "profile at this frequency"),
116286470930SIngo Molnar 	OPT_INTEGER('E', "entries", &print_entries,
116386470930SIngo Molnar 		    "display this many functions"),
11643da297a6SIngo Molnar 	OPT_BOOLEAN('v', "verbose", &verbose,
11653da297a6SIngo Molnar 		    "be more verbose (show counter open errors, etc)"),
116686470930SIngo Molnar 	OPT_END()
116786470930SIngo Molnar };
116886470930SIngo Molnar 
1169f37a291cSIngo Molnar int cmd_top(int argc, const char **argv, const char *prefix __used)
117086470930SIngo Molnar {
117186470930SIngo Molnar 	int counter;
117286470930SIngo Molnar 
117342976487SMike Galbraith 	symbol__init();
117442976487SMike Galbraith 
117586470930SIngo Molnar 	page_size = sysconf(_SC_PAGE_SIZE);
117686470930SIngo Molnar 
117786470930SIngo Molnar 	argc = parse_options(argc, argv, options, top_usage, 0);
117886470930SIngo Molnar 	if (argc)
117986470930SIngo Molnar 		usage_with_options(top_usage, options);
118086470930SIngo Molnar 
118186470930SIngo Molnar 	if (freq) {
118286470930SIngo Molnar 		default_interval = freq;
118386470930SIngo Molnar 		freq = 1;
118486470930SIngo Molnar 	}
118586470930SIngo Molnar 
118686470930SIngo Molnar 	/* CPU and PID are mutually exclusive */
118786470930SIngo Molnar 	if (target_pid != -1 && profile_cpu != -1) {
118886470930SIngo Molnar 		printf("WARNING: PID switch overriding CPU\n");
118986470930SIngo Molnar 		sleep(1);
119086470930SIngo Molnar 		profile_cpu = -1;
119186470930SIngo Molnar 	}
119286470930SIngo Molnar 
119386470930SIngo Molnar 	if (!nr_counters)
119486470930SIngo Molnar 		nr_counters = 1;
119586470930SIngo Molnar 
119686470930SIngo Molnar 	if (delay_secs < 1)
119786470930SIngo Molnar 		delay_secs = 1;
119886470930SIngo Molnar 
119986470930SIngo Molnar 	parse_symbols();
1200923c42c1SMike Galbraith 	parse_source(sym_filter_entry);
120186470930SIngo Molnar 
120286470930SIngo Molnar 	/*
120386470930SIngo Molnar 	 * Fill in the ones not specifically initialized via -c:
120486470930SIngo Molnar 	 */
120586470930SIngo Molnar 	for (counter = 0; counter < nr_counters; counter++) {
120686470930SIngo Molnar 		if (attrs[counter].sample_period)
120786470930SIngo Molnar 			continue;
120886470930SIngo Molnar 
120986470930SIngo Molnar 		attrs[counter].sample_period = default_interval;
121086470930SIngo Molnar 	}
121186470930SIngo Molnar 
121286470930SIngo Molnar 	nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
121386470930SIngo Molnar 	assert(nr_cpus <= MAX_NR_CPUS);
121486470930SIngo Molnar 	assert(nr_cpus >= 0);
121586470930SIngo Molnar 
121686470930SIngo Molnar 	if (target_pid != -1 || profile_cpu != -1)
121786470930SIngo Molnar 		nr_cpus = 1;
121886470930SIngo Molnar 
121986470930SIngo Molnar 	return __cmd_top();
122086470930SIngo Molnar }
1221