xref: /openbmc/linux/tools/perf/builtin-top.c (revision 46ab9764)
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 
598923c42c1SMike Galbraith static void print_known_keys(void)
599923c42c1SMike Galbraith {
600923c42c1SMike Galbraith 	fprintf(stdout, "\nknown keys:\n");
601923c42c1SMike Galbraith 	fprintf(stdout, "\t[d]     select display delay.\n");
602923c42c1SMike Galbraith 	fprintf(stdout, "\t[e]     select display entries (lines).\n");
60346ab9764SMike Galbraith 	fprintf(stdout, "\t[E]     active event counter.              \t(%s)\n", event_name(sym_counter));
604923c42c1SMike Galbraith 	fprintf(stdout, "\t[f]     select normal display count filter.\n");
605923c42c1SMike Galbraith 	fprintf(stdout, "\t[F]     select annotation display count filter (percentage).\n");
606923c42c1SMike Galbraith 	fprintf(stdout, "\t[qQ]    quit.\n");
607923c42c1SMike Galbraith 	fprintf(stdout, "\t[s]     select annotation symbol and start annotation.\n");
608923c42c1SMike Galbraith 	fprintf(stdout, "\t[S]     stop annotation, revert to normal display.\n");
60946ab9764SMike Galbraith 	fprintf(stdout, "\t[w]     toggle display weighted/count[E]r. \t(%d)\n", display_weighted ? 1 : 0);
61046ab9764SMike Galbraith 	fprintf(stdout, "\t[z]     toggle sample zeroing.             \t(%d)\n", zero ? 1 : 0);
611923c42c1SMike Galbraith }
612923c42c1SMike Galbraith 
613923c42c1SMike Galbraith static void handle_keypress(int c)
614923c42c1SMike Galbraith {
615923c42c1SMike Galbraith 	int once = 0;
616923c42c1SMike Galbraith repeat:
617923c42c1SMike Galbraith 	switch (c) {
618923c42c1SMike Galbraith 		case 'd':
619923c42c1SMike Galbraith 			prompt_integer(&delay_secs, "Enter display delay");
620923c42c1SMike Galbraith 			break;
621923c42c1SMike Galbraith 		case 'e':
622923c42c1SMike Galbraith 			prompt_integer(&print_entries, "Enter display entries (lines)");
623923c42c1SMike Galbraith 			break;
624923c42c1SMike Galbraith 		case 'E':
625923c42c1SMike Galbraith 			if (nr_counters > 1) {
626923c42c1SMike Galbraith 				int i;
627923c42c1SMike Galbraith 
628923c42c1SMike Galbraith 				fprintf(stderr, "\nAvailable events:");
629923c42c1SMike Galbraith 				for (i = 0; i < nr_counters; i++)
630923c42c1SMike Galbraith 					fprintf(stderr, "\n\t%d %s", i, event_name(i));
631923c42c1SMike Galbraith 
632923c42c1SMike Galbraith 				prompt_integer(&sym_counter, "Enter details event counter");
633923c42c1SMike Galbraith 
634923c42c1SMike Galbraith 				if (sym_counter >= nr_counters) {
635923c42c1SMike Galbraith 					fprintf(stderr, "Sorry, no such event, using %s.\n", event_name(0));
636923c42c1SMike Galbraith 					sym_counter = 0;
637923c42c1SMike Galbraith 					sleep(1);
638923c42c1SMike Galbraith 				}
639923c42c1SMike Galbraith 			} else sym_counter = 0;
640923c42c1SMike Galbraith 			break;
641923c42c1SMike Galbraith 		case 'f':
642923c42c1SMike Galbraith 			prompt_integer(&count_filter, "Enter display event count filter");
643923c42c1SMike Galbraith 			break;
644923c42c1SMike Galbraith 		case 'F':
645923c42c1SMike Galbraith 			prompt_percent(&sym_pcnt_filter, "Enter details display event filter (percent)");
646923c42c1SMike Galbraith 			break;
647923c42c1SMike Galbraith 		case 'q':
648923c42c1SMike Galbraith 		case 'Q':
649923c42c1SMike Galbraith 			printf("exiting.\n");
650923c42c1SMike Galbraith 			exit(0);
651923c42c1SMike Galbraith 		case 's':
652923c42c1SMike Galbraith 			prompt_symbol(&sym_filter_entry, "Enter details symbol");
653923c42c1SMike Galbraith 			break;
654923c42c1SMike Galbraith 		case 'S':
655923c42c1SMike Galbraith 			if (!sym_filter_entry)
656923c42c1SMike Galbraith 				break;
657923c42c1SMike Galbraith 			else {
658923c42c1SMike Galbraith 				struct sym_entry *syme = sym_filter_entry;
659923c42c1SMike Galbraith 
660923c42c1SMike Galbraith 				pthread_mutex_lock(&syme->source_lock);
661923c42c1SMike Galbraith 				sym_filter_entry = NULL;
662923c42c1SMike Galbraith 				__zero_source_counters(syme);
663923c42c1SMike Galbraith 				pthread_mutex_unlock(&syme->source_lock);
664923c42c1SMike Galbraith 			}
665923c42c1SMike Galbraith 			break;
66646ab9764SMike Galbraith 		case 'w':
66746ab9764SMike Galbraith 			display_weighted = ~display_weighted;
66846ab9764SMike Galbraith 			break;
669923c42c1SMike Galbraith 		case 'z':
670923c42c1SMike Galbraith 			zero = ~zero;
671923c42c1SMike Galbraith 			break;
672923c42c1SMike Galbraith 		default: {
673923c42c1SMike Galbraith 			struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
674923c42c1SMike Galbraith 			struct termios tc, save;
675923c42c1SMike Galbraith 
676923c42c1SMike Galbraith 			if (!once) {
677923c42c1SMike Galbraith 				print_known_keys();
678923c42c1SMike Galbraith 				once++;
679923c42c1SMike Galbraith 			}
680923c42c1SMike Galbraith 
681923c42c1SMike Galbraith 			tcgetattr(0, &save);
682923c42c1SMike Galbraith 			tc = save;
683923c42c1SMike Galbraith 			tc.c_lflag &= ~(ICANON | ECHO);
684923c42c1SMike Galbraith 			tc.c_cc[VMIN] = 0;
685923c42c1SMike Galbraith 			tc.c_cc[VTIME] = 0;
686923c42c1SMike Galbraith 			tcsetattr(0, TCSANOW, &tc);
687923c42c1SMike Galbraith 
688923c42c1SMike Galbraith 			poll(&stdin_poll, 1, -1);
689923c42c1SMike Galbraith 			c = getc(stdin);
690923c42c1SMike Galbraith 
691923c42c1SMike Galbraith 			tcsetattr(0, TCSAFLUSH, &save);
692923c42c1SMike Galbraith 			goto repeat;
693923c42c1SMike Galbraith 		}
694923c42c1SMike Galbraith 	}
695923c42c1SMike Galbraith }
696923c42c1SMike Galbraith 
697f37a291cSIngo Molnar static void *display_thread(void *arg __used)
69886470930SIngo Molnar {
69986470930SIngo Molnar 	struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
700923c42c1SMike Galbraith 	struct termios tc, save;
701923c42c1SMike Galbraith 	int delay_msecs, c;
70286470930SIngo Molnar 
703923c42c1SMike Galbraith 	tcgetattr(0, &save);
704923c42c1SMike Galbraith 	tc = save;
705923c42c1SMike Galbraith 	tc.c_lflag &= ~(ICANON | ECHO);
706923c42c1SMike Galbraith 	tc.c_cc[VMIN] = 0;
707923c42c1SMike Galbraith 	tc.c_cc[VTIME] = 0;
708923c42c1SMike Galbraith repeat:
709923c42c1SMike Galbraith 	delay_msecs = delay_secs * 1000;
710923c42c1SMike Galbraith 	tcsetattr(0, TCSANOW, &tc);
711923c42c1SMike Galbraith 	/* trash return*/
712923c42c1SMike Galbraith 	getc(stdin);
71386470930SIngo Molnar 
71486470930SIngo Molnar 	do {
71586470930SIngo Molnar 		print_sym_table();
71686470930SIngo Molnar 	} while (!poll(&stdin_poll, 1, delay_msecs) == 1);
71786470930SIngo Molnar 
718923c42c1SMike Galbraith 	c = getc(stdin);
719923c42c1SMike Galbraith 	tcsetattr(0, TCSAFLUSH, &save);
720923c42c1SMike Galbraith 
721923c42c1SMike Galbraith 	handle_keypress(c);
722923c42c1SMike Galbraith 	goto repeat;
72386470930SIngo Molnar 
72486470930SIngo Molnar 	return NULL;
72586470930SIngo Molnar }
72686470930SIngo Molnar 
7272ab52083SAnton Blanchard /* Tag samples to be skipped. */
728f37a291cSIngo Molnar static const char *skip_symbols[] = {
7292ab52083SAnton Blanchard 	"default_idle",
7302ab52083SAnton Blanchard 	"cpu_idle",
7312ab52083SAnton Blanchard 	"enter_idle",
7322ab52083SAnton Blanchard 	"exit_idle",
7332ab52083SAnton Blanchard 	"mwait_idle",
73459b90056SArnaldo Carvalho de Melo 	"mwait_idle_with_hints",
7353a3393efSAnton Blanchard 	"ppc64_runlatch_off",
7363a3393efSAnton Blanchard 	"pseries_dedicated_idle_sleep",
7372ab52083SAnton Blanchard 	NULL
7382ab52083SAnton Blanchard };
7392ab52083SAnton Blanchard 
74086470930SIngo Molnar static int symbol_filter(struct dso *self, struct symbol *sym)
74186470930SIngo Molnar {
74286470930SIngo Molnar 	struct sym_entry *syme;
74386470930SIngo Molnar 	const char *name = sym->name;
7442ab52083SAnton Blanchard 	int i;
74586470930SIngo Molnar 
7463a3393efSAnton Blanchard 	/*
7473a3393efSAnton Blanchard 	 * ppc64 uses function descriptors and appends a '.' to the
7483a3393efSAnton Blanchard 	 * start of every instruction address. Remove it.
7493a3393efSAnton Blanchard 	 */
7503a3393efSAnton Blanchard 	if (name[0] == '.')
7513a3393efSAnton Blanchard 		name++;
7523a3393efSAnton Blanchard 
75386470930SIngo Molnar 	if (!strcmp(name, "_text") ||
75486470930SIngo Molnar 	    !strcmp(name, "_etext") ||
75586470930SIngo Molnar 	    !strcmp(name, "_sinittext") ||
75686470930SIngo Molnar 	    !strncmp("init_module", name, 11) ||
75786470930SIngo Molnar 	    !strncmp("cleanup_module", name, 14) ||
75886470930SIngo Molnar 	    strstr(name, "_text_start") ||
75986470930SIngo Molnar 	    strstr(name, "_text_end"))
76086470930SIngo Molnar 		return 1;
76186470930SIngo Molnar 
76286470930SIngo Molnar 	syme = dso__sym_priv(self, sym);
763923c42c1SMike Galbraith 	pthread_mutex_init(&syme->source_lock, NULL);
764923c42c1SMike Galbraith 	if (!sym_filter_entry && sym_filter && !strcmp(name, sym_filter))
765923c42c1SMike Galbraith 		sym_filter_entry = syme;
766923c42c1SMike Galbraith 
7672ab52083SAnton Blanchard 	for (i = 0; skip_symbols[i]; i++) {
7682ab52083SAnton Blanchard 		if (!strcmp(skip_symbols[i], name)) {
76986470930SIngo Molnar 			syme->skip = 1;
7702ab52083SAnton Blanchard 			break;
7712ab52083SAnton Blanchard 		}
7722ab52083SAnton Blanchard 	}
77386470930SIngo Molnar 
77486470930SIngo Molnar 	return 0;
77586470930SIngo Molnar }
77686470930SIngo Molnar 
77786470930SIngo Molnar static int parse_symbols(void)
77886470930SIngo Molnar {
77986470930SIngo Molnar 	struct rb_node *node;
78086470930SIngo Molnar 	struct symbol  *sym;
78142976487SMike Galbraith 	int modules = vmlinux ? 1 : 0;
78286470930SIngo Molnar 
78386470930SIngo Molnar 	kernel_dso = dso__new("[kernel]", sizeof(struct sym_entry));
78486470930SIngo Molnar 	if (kernel_dso == NULL)
78586470930SIngo Molnar 		return -1;
78686470930SIngo Molnar 
78742976487SMike Galbraith 	if (dso__load_kernel(kernel_dso, vmlinux, symbol_filter, verbose, modules) <= 0)
78886470930SIngo Molnar 		goto out_delete_dso;
78986470930SIngo Molnar 
79086470930SIngo Molnar 	node = rb_first(&kernel_dso->syms);
79186470930SIngo Molnar 	sym = rb_entry(node, struct symbol, rb_node);
79286470930SIngo Molnar 	min_ip = sym->start;
79386470930SIngo Molnar 
79486470930SIngo Molnar 	node = rb_last(&kernel_dso->syms);
79586470930SIngo Molnar 	sym = rb_entry(node, struct symbol, rb_node);
79686470930SIngo Molnar 	max_ip = sym->end;
79786470930SIngo Molnar 
79886470930SIngo Molnar 	if (dump_symtab)
79986470930SIngo Molnar 		dso__fprintf(kernel_dso, stderr);
80086470930SIngo Molnar 
80186470930SIngo Molnar 	return 0;
80286470930SIngo Molnar 
80386470930SIngo Molnar out_delete_dso:
80486470930SIngo Molnar 	dso__delete(kernel_dso);
80586470930SIngo Molnar 	kernel_dso = NULL;
80686470930SIngo Molnar 	return -1;
80786470930SIngo Molnar }
80886470930SIngo Molnar 
80986470930SIngo Molnar /*
81086470930SIngo Molnar  * Binary search in the histogram table and record the hit:
81186470930SIngo Molnar  */
8129cffa8d5SPaul Mackerras static void record_ip(u64 ip, int counter)
81386470930SIngo Molnar {
81486470930SIngo Molnar 	struct symbol *sym = dso__find_symbol(kernel_dso, ip);
81586470930SIngo Molnar 
81686470930SIngo Molnar 	if (sym != NULL) {
81786470930SIngo Molnar 		struct sym_entry *syme = dso__sym_priv(kernel_dso, sym);
81886470930SIngo Molnar 
81986470930SIngo Molnar 		if (!syme->skip) {
82086470930SIngo Molnar 			syme->count[counter]++;
821923c42c1SMike Galbraith 			record_precise_ip(syme, counter, ip);
82286470930SIngo Molnar 			pthread_mutex_lock(&active_symbols_lock);
82386470930SIngo Molnar 			if (list_empty(&syme->node) || !syme->node.next)
82486470930SIngo Molnar 				__list_insert_active_sym(syme);
82586470930SIngo Molnar 			pthread_mutex_unlock(&active_symbols_lock);
82686470930SIngo Molnar 			return;
82786470930SIngo Molnar 		}
82886470930SIngo Molnar 	}
82986470930SIngo Molnar 
83086470930SIngo Molnar 	samples--;
83186470930SIngo Molnar }
83286470930SIngo Molnar 
833e6e18ec7SPeter Zijlstra static void process_event(u64 ip, int counter, int user)
83486470930SIngo Molnar {
83586470930SIngo Molnar 	samples++;
83686470930SIngo Molnar 
837e6e18ec7SPeter Zijlstra 	if (user) {
83886470930SIngo Molnar 		userspace_samples++;
83986470930SIngo Molnar 		return;
84086470930SIngo Molnar 	}
84186470930SIngo Molnar 
84286470930SIngo Molnar 	record_ip(ip, counter);
84386470930SIngo Molnar }
84486470930SIngo Molnar 
84586470930SIngo Molnar struct mmap_data {
84686470930SIngo Molnar 	int			counter;
84786470930SIngo Molnar 	void			*base;
848f37a291cSIngo Molnar 	int			mask;
84986470930SIngo Molnar 	unsigned int		prev;
85086470930SIngo Molnar };
85186470930SIngo Molnar 
85286470930SIngo Molnar static unsigned int mmap_read_head(struct mmap_data *md)
85386470930SIngo Molnar {
85486470930SIngo Molnar 	struct perf_counter_mmap_page *pc = md->base;
85586470930SIngo Molnar 	int head;
85686470930SIngo Molnar 
85786470930SIngo Molnar 	head = pc->data_head;
85886470930SIngo Molnar 	rmb();
85986470930SIngo Molnar 
86086470930SIngo Molnar 	return head;
86186470930SIngo Molnar }
86286470930SIngo Molnar 
86386470930SIngo Molnar struct timeval last_read, this_read;
86486470930SIngo Molnar 
8652f01190aSFrederic Weisbecker static void mmap_read_counter(struct mmap_data *md)
86686470930SIngo Molnar {
86786470930SIngo Molnar 	unsigned int head = mmap_read_head(md);
86886470930SIngo Molnar 	unsigned int old = md->prev;
86986470930SIngo Molnar 	unsigned char *data = md->base + page_size;
87086470930SIngo Molnar 	int diff;
87186470930SIngo Molnar 
87286470930SIngo Molnar 	gettimeofday(&this_read, NULL);
87386470930SIngo Molnar 
87486470930SIngo Molnar 	/*
87586470930SIngo Molnar 	 * If we're further behind than half the buffer, there's a chance
87686470930SIngo Molnar 	 * the writer will bite our tail and mess up the samples under us.
87786470930SIngo Molnar 	 *
87886470930SIngo Molnar 	 * If we somehow ended up ahead of the head, we got messed up.
87986470930SIngo Molnar 	 *
88086470930SIngo Molnar 	 * In either case, truncate and restart at head.
88186470930SIngo Molnar 	 */
88286470930SIngo Molnar 	diff = head - old;
88386470930SIngo Molnar 	if (diff > md->mask / 2 || diff < 0) {
88486470930SIngo Molnar 		struct timeval iv;
88586470930SIngo Molnar 		unsigned long msecs;
88686470930SIngo Molnar 
88786470930SIngo Molnar 		timersub(&this_read, &last_read, &iv);
88886470930SIngo Molnar 		msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
88986470930SIngo Molnar 
89086470930SIngo Molnar 		fprintf(stderr, "WARNING: failed to keep up with mmap data."
89186470930SIngo Molnar 				"  Last read %lu msecs ago.\n", msecs);
89286470930SIngo Molnar 
89386470930SIngo Molnar 		/*
89486470930SIngo Molnar 		 * head points to a known good entry, start there.
89586470930SIngo Molnar 		 */
89686470930SIngo Molnar 		old = head;
89786470930SIngo Molnar 	}
89886470930SIngo Molnar 
89986470930SIngo Molnar 	last_read = this_read;
90086470930SIngo Molnar 
90186470930SIngo Molnar 	for (; old != head;) {
90286470930SIngo Molnar 		struct ip_event {
90386470930SIngo Molnar 			struct perf_event_header header;
9049cffa8d5SPaul Mackerras 			u64 ip;
9059cffa8d5SPaul Mackerras 			u32 pid, target_pid;
90686470930SIngo Molnar 		};
90786470930SIngo Molnar 		struct mmap_event {
90886470930SIngo Molnar 			struct perf_event_header header;
9099cffa8d5SPaul Mackerras 			u32 pid, target_pid;
9109cffa8d5SPaul Mackerras 			u64 start;
9119cffa8d5SPaul Mackerras 			u64 len;
9129cffa8d5SPaul Mackerras 			u64 pgoff;
91386470930SIngo Molnar 			char filename[PATH_MAX];
91486470930SIngo Molnar 		};
91586470930SIngo Molnar 
91686470930SIngo Molnar 		typedef union event_union {
91786470930SIngo Molnar 			struct perf_event_header header;
91886470930SIngo Molnar 			struct ip_event ip;
91986470930SIngo Molnar 			struct mmap_event mmap;
92086470930SIngo Molnar 		} event_t;
92186470930SIngo Molnar 
92286470930SIngo Molnar 		event_t *event = (event_t *)&data[old & md->mask];
92386470930SIngo Molnar 
92486470930SIngo Molnar 		event_t event_copy;
92586470930SIngo Molnar 
92686470930SIngo Molnar 		size_t size = event->header.size;
92786470930SIngo Molnar 
92886470930SIngo Molnar 		/*
92986470930SIngo Molnar 		 * Event straddles the mmap boundary -- header should always
93086470930SIngo Molnar 		 * be inside due to u64 alignment of output.
93186470930SIngo Molnar 		 */
93286470930SIngo Molnar 		if ((old & md->mask) + size != ((old + size) & md->mask)) {
93386470930SIngo Molnar 			unsigned int offset = old;
93486470930SIngo Molnar 			unsigned int len = min(sizeof(*event), size), cpy;
93586470930SIngo Molnar 			void *dst = &event_copy;
93686470930SIngo Molnar 
93786470930SIngo Molnar 			do {
93886470930SIngo Molnar 				cpy = min(md->mask + 1 - (offset & md->mask), len);
93986470930SIngo Molnar 				memcpy(dst, &data[offset & md->mask], cpy);
94086470930SIngo Molnar 				offset += cpy;
94186470930SIngo Molnar 				dst += cpy;
94286470930SIngo Molnar 				len -= cpy;
94386470930SIngo Molnar 			} while (len);
94486470930SIngo Molnar 
94586470930SIngo Molnar 			event = &event_copy;
94686470930SIngo Molnar 		}
94786470930SIngo Molnar 
94886470930SIngo Molnar 		old += size;
94986470930SIngo Molnar 
950e6e18ec7SPeter Zijlstra 		if (event->header.type == PERF_EVENT_SAMPLE) {
951e6e18ec7SPeter Zijlstra 			int user =
952e6e18ec7SPeter Zijlstra 	(event->header.misc & PERF_EVENT_MISC_CPUMODE_MASK) == PERF_EVENT_MISC_USER;
953e6e18ec7SPeter Zijlstra 			process_event(event->ip.ip, md->counter, user);
95486470930SIngo Molnar 		}
95586470930SIngo Molnar 	}
95686470930SIngo Molnar 
95786470930SIngo Molnar 	md->prev = old;
95886470930SIngo Molnar }
95986470930SIngo Molnar 
96086470930SIngo Molnar static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
96186470930SIngo Molnar static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
96286470930SIngo Molnar 
9632f01190aSFrederic Weisbecker static void mmap_read(void)
9642f01190aSFrederic Weisbecker {
9652f01190aSFrederic Weisbecker 	int i, counter;
9662f01190aSFrederic Weisbecker 
9672f01190aSFrederic Weisbecker 	for (i = 0; i < nr_cpus; i++) {
9682f01190aSFrederic Weisbecker 		for (counter = 0; counter < nr_counters; counter++)
9692f01190aSFrederic Weisbecker 			mmap_read_counter(&mmap_array[i][counter]);
9702f01190aSFrederic Weisbecker 	}
9712f01190aSFrederic Weisbecker }
9722f01190aSFrederic Weisbecker 
973716c69feSIngo Molnar int nr_poll;
974716c69feSIngo Molnar int group_fd;
975716c69feSIngo Molnar 
976716c69feSIngo Molnar static void start_counter(int i, int counter)
97786470930SIngo Molnar {
97886470930SIngo Molnar 	struct perf_counter_attr *attr;
9790fdc7e67SMike Galbraith 	int cpu;
98086470930SIngo Molnar 
98186470930SIngo Molnar 	cpu = profile_cpu;
98286470930SIngo Molnar 	if (target_pid == -1 && profile_cpu == -1)
98386470930SIngo Molnar 		cpu = i;
98486470930SIngo Molnar 
98586470930SIngo Molnar 	attr = attrs + counter;
98686470930SIngo Molnar 
98786470930SIngo Molnar 	attr->sample_type	= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
98886470930SIngo Molnar 	attr->freq		= freq;
9890fdc7e67SMike Galbraith 	attr->inherit		= (cpu < 0) && inherit;
99086470930SIngo Molnar 
991716c69feSIngo Molnar try_again:
99286470930SIngo Molnar 	fd[i][counter] = sys_perf_counter_open(attr, target_pid, cpu, group_fd, 0);
993716c69feSIngo Molnar 
99486470930SIngo Molnar 	if (fd[i][counter] < 0) {
99586470930SIngo Molnar 		int err = errno;
99686470930SIngo Molnar 
99786470930SIngo Molnar 		if (err == EPERM)
998716c69feSIngo Molnar 			die("No permission - are you root?\n");
999716c69feSIngo Molnar 		/*
1000716c69feSIngo Molnar 		 * If it's cycles then fall back to hrtimer
1001716c69feSIngo Molnar 		 * based cpu-clock-tick sw counter, which
1002716c69feSIngo Molnar 		 * is always available even if no PMU support:
1003716c69feSIngo Molnar 		 */
1004716c69feSIngo Molnar 		if (attr->type == PERF_TYPE_HARDWARE
1005f4dbfa8fSPeter Zijlstra 			&& attr->config == PERF_COUNT_HW_CPU_CYCLES) {
1006716c69feSIngo Molnar 
10073da297a6SIngo Molnar 			if (verbose)
1008716c69feSIngo Molnar 				warning(" ... trying to fall back to cpu-clock-ticks\n");
10093da297a6SIngo Molnar 
1010716c69feSIngo Molnar 			attr->type = PERF_TYPE_SOFTWARE;
1011f4dbfa8fSPeter Zijlstra 			attr->config = PERF_COUNT_SW_CPU_CLOCK;
1012716c69feSIngo Molnar 			goto try_again;
1013716c69feSIngo Molnar 		}
101430c806a0SIngo Molnar 		printf("\n");
101530c806a0SIngo Molnar 		error("perfcounter syscall returned with %d (%s)\n",
101630c806a0SIngo Molnar 			fd[i][counter], strerror(err));
101730c806a0SIngo Molnar 		die("No CONFIG_PERF_COUNTERS=y kernel support configured?\n");
101886470930SIngo Molnar 		exit(-1);
101986470930SIngo Molnar 	}
102086470930SIngo Molnar 	assert(fd[i][counter] >= 0);
102186470930SIngo Molnar 	fcntl(fd[i][counter], F_SETFL, O_NONBLOCK);
102286470930SIngo Molnar 
102386470930SIngo Molnar 	/*
102486470930SIngo Molnar 	 * First counter acts as the group leader:
102586470930SIngo Molnar 	 */
102686470930SIngo Molnar 	if (group && group_fd == -1)
102786470930SIngo Molnar 		group_fd = fd[i][counter];
102886470930SIngo Molnar 
102986470930SIngo Molnar 	event_array[nr_poll].fd = fd[i][counter];
103086470930SIngo Molnar 	event_array[nr_poll].events = POLLIN;
103186470930SIngo Molnar 	nr_poll++;
103286470930SIngo Molnar 
103386470930SIngo Molnar 	mmap_array[i][counter].counter = counter;
103486470930SIngo Molnar 	mmap_array[i][counter].prev = 0;
103586470930SIngo Molnar 	mmap_array[i][counter].mask = mmap_pages*page_size - 1;
103686470930SIngo Molnar 	mmap_array[i][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
103786470930SIngo Molnar 			PROT_READ, MAP_SHARED, fd[i][counter], 0);
103886470930SIngo Molnar 	if (mmap_array[i][counter].base == MAP_FAILED)
103986470930SIngo Molnar 		die("failed to mmap with %d (%s)\n", errno, strerror(errno));
104086470930SIngo Molnar }
1041716c69feSIngo Molnar 
1042716c69feSIngo Molnar static int __cmd_top(void)
1043716c69feSIngo Molnar {
1044716c69feSIngo Molnar 	pthread_t thread;
1045716c69feSIngo Molnar 	int i, counter;
1046716c69feSIngo Molnar 	int ret;
1047716c69feSIngo Molnar 
1048716c69feSIngo Molnar 	for (i = 0; i < nr_cpus; i++) {
1049716c69feSIngo Molnar 		group_fd = -1;
1050716c69feSIngo Molnar 		for (counter = 0; counter < nr_counters; counter++)
1051716c69feSIngo Molnar 			start_counter(i, counter);
105286470930SIngo Molnar 	}
105386470930SIngo Molnar 
10542f01190aSFrederic Weisbecker 	/* Wait for a minimal set of events before starting the snapshot */
10552f01190aSFrederic Weisbecker 	poll(event_array, nr_poll, 100);
10562f01190aSFrederic Weisbecker 
10572f01190aSFrederic Weisbecker 	mmap_read();
10582f01190aSFrederic Weisbecker 
105986470930SIngo Molnar 	if (pthread_create(&thread, NULL, display_thread, NULL)) {
106086470930SIngo Molnar 		printf("Could not create display thread.\n");
106186470930SIngo Molnar 		exit(-1);
106286470930SIngo Molnar 	}
106386470930SIngo Molnar 
106486470930SIngo Molnar 	if (realtime_prio) {
106586470930SIngo Molnar 		struct sched_param param;
106686470930SIngo Molnar 
106786470930SIngo Molnar 		param.sched_priority = realtime_prio;
106886470930SIngo Molnar 		if (sched_setscheduler(0, SCHED_FIFO, &param)) {
106986470930SIngo Molnar 			printf("Could not set realtime priority.\n");
107086470930SIngo Molnar 			exit(-1);
107186470930SIngo Molnar 		}
107286470930SIngo Molnar 	}
107386470930SIngo Molnar 
107486470930SIngo Molnar 	while (1) {
107586470930SIngo Molnar 		int hits = samples;
107686470930SIngo Molnar 
10772f01190aSFrederic Weisbecker 		mmap_read();
107886470930SIngo Molnar 
107986470930SIngo Molnar 		if (hits == samples)
108086470930SIngo Molnar 			ret = poll(event_array, nr_poll, 100);
108186470930SIngo Molnar 	}
108286470930SIngo Molnar 
108386470930SIngo Molnar 	return 0;
108486470930SIngo Molnar }
108586470930SIngo Molnar 
108686470930SIngo Molnar static const char * const top_usage[] = {
108786470930SIngo Molnar 	"perf top [<options>]",
108886470930SIngo Molnar 	NULL
108986470930SIngo Molnar };
109086470930SIngo Molnar 
109186470930SIngo Molnar static const struct option options[] = {
109286470930SIngo Molnar 	OPT_CALLBACK('e', "event", NULL, "event",
109386470930SIngo Molnar 		     "event selector. use 'perf list' to list available events",
109486470930SIngo Molnar 		     parse_events),
109586470930SIngo Molnar 	OPT_INTEGER('c', "count", &default_interval,
109686470930SIngo Molnar 		    "event period to sample"),
109786470930SIngo Molnar 	OPT_INTEGER('p', "pid", &target_pid,
109886470930SIngo Molnar 		    "profile events on existing pid"),
109986470930SIngo Molnar 	OPT_BOOLEAN('a', "all-cpus", &system_wide,
110086470930SIngo Molnar 			    "system-wide collection from all CPUs"),
110186470930SIngo Molnar 	OPT_INTEGER('C', "CPU", &profile_cpu,
110286470930SIngo Molnar 		    "CPU to profile on"),
110342976487SMike Galbraith 	OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
110486470930SIngo Molnar 	OPT_INTEGER('m', "mmap-pages", &mmap_pages,
110586470930SIngo Molnar 		    "number of mmap data pages"),
110686470930SIngo Molnar 	OPT_INTEGER('r', "realtime", &realtime_prio,
110786470930SIngo Molnar 		    "collect data with this RT SCHED_FIFO priority"),
110886470930SIngo Molnar 	OPT_INTEGER('d', "delay", &delay_secs,
110986470930SIngo Molnar 		    "number of seconds to delay between refreshes"),
111086470930SIngo Molnar 	OPT_BOOLEAN('D', "dump-symtab", &dump_symtab,
111186470930SIngo Molnar 			    "dump the symbol table used for profiling"),
111286470930SIngo Molnar 	OPT_INTEGER('f', "count-filter", &count_filter,
111386470930SIngo Molnar 		    "only display functions with more events than this"),
111486470930SIngo Molnar 	OPT_BOOLEAN('g', "group", &group,
111586470930SIngo Molnar 			    "put the counters into a counter group"),
11160fdc7e67SMike Galbraith 	OPT_BOOLEAN('i', "inherit", &inherit,
11170fdc7e67SMike Galbraith 		    "child tasks inherit counters"),
1118923c42c1SMike Galbraith 	OPT_STRING('s', "sym-annotate", &sym_filter, "symbol name",
1119923c42c1SMike Galbraith 		    "symbol to annotate - requires -k option"),
11201f208ea6SAnton Blanchard 	OPT_BOOLEAN('z', "zero", &zero,
112186470930SIngo Molnar 		    "zero history across updates"),
112286470930SIngo Molnar 	OPT_INTEGER('F', "freq", &freq,
112386470930SIngo Molnar 		    "profile at this frequency"),
112486470930SIngo Molnar 	OPT_INTEGER('E', "entries", &print_entries,
112586470930SIngo Molnar 		    "display this many functions"),
11263da297a6SIngo Molnar 	OPT_BOOLEAN('v', "verbose", &verbose,
11273da297a6SIngo Molnar 		    "be more verbose (show counter open errors, etc)"),
112886470930SIngo Molnar 	OPT_END()
112986470930SIngo Molnar };
113086470930SIngo Molnar 
1131f37a291cSIngo Molnar int cmd_top(int argc, const char **argv, const char *prefix __used)
113286470930SIngo Molnar {
113386470930SIngo Molnar 	int counter;
113486470930SIngo Molnar 
113542976487SMike Galbraith 	symbol__init();
113642976487SMike Galbraith 
113786470930SIngo Molnar 	page_size = sysconf(_SC_PAGE_SIZE);
113886470930SIngo Molnar 
113986470930SIngo Molnar 	argc = parse_options(argc, argv, options, top_usage, 0);
114086470930SIngo Molnar 	if (argc)
114186470930SIngo Molnar 		usage_with_options(top_usage, options);
114286470930SIngo Molnar 
114386470930SIngo Molnar 	if (freq) {
114486470930SIngo Molnar 		default_interval = freq;
114586470930SIngo Molnar 		freq = 1;
114686470930SIngo Molnar 	}
114786470930SIngo Molnar 
114886470930SIngo Molnar 	/* CPU and PID are mutually exclusive */
114986470930SIngo Molnar 	if (target_pid != -1 && profile_cpu != -1) {
115086470930SIngo Molnar 		printf("WARNING: PID switch overriding CPU\n");
115186470930SIngo Molnar 		sleep(1);
115286470930SIngo Molnar 		profile_cpu = -1;
115386470930SIngo Molnar 	}
115486470930SIngo Molnar 
115586470930SIngo Molnar 	if (!nr_counters)
115686470930SIngo Molnar 		nr_counters = 1;
115786470930SIngo Molnar 
115886470930SIngo Molnar 	if (delay_secs < 1)
115986470930SIngo Molnar 		delay_secs = 1;
116086470930SIngo Molnar 
116186470930SIngo Molnar 	parse_symbols();
1162923c42c1SMike Galbraith 	parse_source(sym_filter_entry);
116386470930SIngo Molnar 
116486470930SIngo Molnar 	/*
116586470930SIngo Molnar 	 * Fill in the ones not specifically initialized via -c:
116686470930SIngo Molnar 	 */
116786470930SIngo Molnar 	for (counter = 0; counter < nr_counters; counter++) {
116886470930SIngo Molnar 		if (attrs[counter].sample_period)
116986470930SIngo Molnar 			continue;
117086470930SIngo Molnar 
117186470930SIngo Molnar 		attrs[counter].sample_period = default_interval;
117286470930SIngo Molnar 	}
117386470930SIngo Molnar 
117486470930SIngo Molnar 	nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
117586470930SIngo Molnar 	assert(nr_cpus <= MAX_NR_CPUS);
117686470930SIngo Molnar 	assert(nr_cpus >= 0);
117786470930SIngo Molnar 
117886470930SIngo Molnar 	if (target_pid != -1 || profile_cpu != -1)
117986470930SIngo Molnar 		nr_cpus = 1;
118086470930SIngo Molnar 
118186470930SIngo Molnar 	return __cmd_top();
118286470930SIngo Molnar }
1183