blob: 154e088588bc4de4334309131aea4d24d72ba327 [file] [log] [blame]
Ingo Molnar07800602009-04-20 15:00:56 +02001/*
Ingo Molnarbf9e1872009-06-02 23:37:05 +02002 * builtin-top.c
3 *
4 * Builtin top command: Display a continuously updated profile of
5 * any workload, CPU or specific PID.
6 *
7 * Copyright (C) 2008, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
8 *
9 * Improvements and fixes by:
10 *
11 * Arjan van de Ven <arjan@linux.intel.com>
12 * Yanmin Zhang <yanmin.zhang@intel.com>
13 * Wu Fengguang <fengguang.wu@intel.com>
14 * Mike Galbraith <efault@gmx.de>
15 * Paul Mackerras <paulus@samba.org>
16 *
17 * Released under the GPL v2. (and only v2, not any later version)
Ingo Molnar07800602009-04-20 15:00:56 +020018 */
Ingo Molnarbf9e1872009-06-02 23:37:05 +020019#include "builtin.h"
Ingo Molnar07800602009-04-20 15:00:56 +020020
Peter Zijlstra1a482f32009-05-23 18:28:58 +020021#include "perf.h"
Ingo Molnarbf9e1872009-06-02 23:37:05 +020022
Arnaldo Carvalho de Meloc0443df2011-01-31 18:19:33 -020023#include "util/cache.h"
Ingo Molnar8fc03212009-06-04 15:19:47 +020024#include "util/color.h"
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020025#include "util/evlist.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020026#include "util/evsel.h"
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -020027#include "util/session.h"
28#include "util/symbol.h"
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030029#include "util/thread.h"
Arnaldo Carvalho de Melofd782602011-01-18 15:15:24 -020030#include "util/thread_map.h"
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -020031#include "util/top.h"
Ingo Molnar148be2c2009-04-27 08:02:14 +020032#include "util/util.h"
Arnaldo Carvalho de Melo43cbcd82009-07-01 12:28:37 -030033#include <linux/rbtree.h>
Ingo Molnarb456bae2009-05-26 09:17:18 +020034#include "util/parse-options.h"
35#include "util/parse-events.h"
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110036#include "util/cpumap.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020037#include "util/xyarray.h"
Ingo Molnar07800602009-04-20 15:00:56 +020038
Frederic Weisbecker8f288272009-08-16 22:05:48 +020039#include "util/debug.h"
40
Ingo Molnar07800602009-04-20 15:00:56 +020041#include <assert.h>
42#include <fcntl.h>
Ingo Molnar0e9b20b2009-05-26 09:17:18 +020043
Ingo Molnar07800602009-04-20 15:00:56 +020044#include <stdio.h>
Mike Galbraith923c42c2009-07-22 20:36:03 +020045#include <termios.h>
46#include <unistd.h>
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -020047#include <inttypes.h>
Ingo Molnar0e9b20b2009-05-26 09:17:18 +020048
Ingo Molnar07800602009-04-20 15:00:56 +020049#include <errno.h>
Ingo Molnar07800602009-04-20 15:00:56 +020050#include <time.h>
51#include <sched.h>
Ingo Molnar07800602009-04-20 15:00:56 +020052
53#include <sys/syscall.h>
54#include <sys/ioctl.h>
55#include <sys/poll.h>
56#include <sys/prctl.h>
57#include <sys/wait.h>
58#include <sys/uio.h>
59#include <sys/mman.h>
60
61#include <linux/unistd.h>
62#include <linux/types.h>
63
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020064#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
Ingo Molnara21ca2c2009-06-06 09:58:57 +020065
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -020066static struct perf_top top = {
67 .count_filter = 5,
68 .delay_secs = 2,
69 .display_weighted = -1,
70 .target_pid = -1,
71 .target_tid = -1,
72 .active_symbols = LIST_HEAD_INIT(top.active_symbols),
73 .active_symbols_lock = PTHREAD_MUTEX_INITIALIZER,
74 .freq = 1000, /* 1 KHz */
75};
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020076
Ian Munsiec0555642010-04-13 18:37:33 +100077static bool system_wide = false;
Ingo Molnar07800602009-04-20 15:00:56 +020078
Arnaldo Carvalho de Meloc0443df2011-01-31 18:19:33 -020079static bool use_tui, use_stdio;
80
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +020081static int default_interval = 0;
Ingo Molnar07800602009-04-20 15:00:56 +020082
Ian Munsiec0555642010-04-13 18:37:33 +100083static bool inherit = false;
Arnaldo Carvalho de Melo19679362010-05-17 15:39:16 -030084static int realtime_prio = 0;
Ian Munsiec0555642010-04-13 18:37:33 +100085static bool group = false;
Ingo Molnar07800602009-04-20 15:00:56 +020086static unsigned int page_size;
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -020087static unsigned int mmap_pages = 128;
Ingo Molnar07800602009-04-20 15:00:56 +020088
Ian Munsiec0555642010-04-13 18:37:33 +100089static bool dump_symtab = false;
Ingo Molnar07800602009-04-20 15:00:56 +020090
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -020091static struct winsize winsize;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -020092
Arnaldo Carvalho de Meloedb7c602010-05-17 16:22:41 -030093static const char *sym_filter = NULL;
Ingo Molnar42e59d72009-10-06 15:14:21 +020094struct sym_entry *sym_filter_entry = NULL;
Kirill Smelkov6cff0e82010-02-03 16:52:08 -020095struct sym_entry *sym_filter_entry_sched = NULL;
Ingo Molnar42e59d72009-10-06 15:14:21 +020096static int sym_pcnt_filter = 5;
Ingo Molnar07800602009-04-20 15:00:56 +020097
Mike Galbraith923c42c2009-07-22 20:36:03 +020098/*
99 * Source functions
100 */
101
Arnaldo Carvalho de Melo895f0ed2010-03-11 20:12:41 -0300102void get_term_dimensions(struct winsize *ws)
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200103{
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200104 char *s = getenv("LINES");
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200105
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200106 if (s != NULL) {
107 ws->ws_row = atoi(s);
108 s = getenv("COLUMNS");
109 if (s != NULL) {
110 ws->ws_col = atoi(s);
111 if (ws->ws_row && ws->ws_col)
112 return;
113 }
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200114 }
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200115#ifdef TIOCGWINSZ
116 if (ioctl(1, TIOCGWINSZ, ws) == 0 &&
117 ws->ws_row && ws->ws_col)
118 return;
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200119#endif
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200120 ws->ws_row = 25;
121 ws->ws_col = 80;
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200122}
123
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200124static void update_print_entries(struct winsize *ws)
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200125{
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200126 top.print_entries = ws->ws_row;
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200127
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200128 if (top.print_entries > 9)
129 top.print_entries -= 9;
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200130}
131
132static void sig_winch_handler(int sig __used)
133{
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200134 get_term_dimensions(&winsize);
135 update_print_entries(&winsize);
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200136}
137
Arnaldo Carvalho de Melob0a9ab62010-03-15 11:46:58 -0300138static int parse_source(struct sym_entry *syme)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200139{
140 struct symbol *sym;
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200141 struct sym_entry_source *source;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300142 struct map *map;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200143 FILE *file;
Ingo Molnar83a09442009-08-15 12:26:57 +0200144 char command[PATH_MAX*2];
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300145 const char *path;
146 u64 len;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200147
148 if (!syme)
Arnaldo Carvalho de Melob0a9ab62010-03-15 11:46:58 -0300149 return -1;
150
151 sym = sym_entry__symbol(syme);
152 map = syme->map;
153
154 /*
155 * We can't annotate with just /proc/kallsyms
156 */
157 if (map->dso->origin == DSO__ORIG_KERNEL)
158 return -1;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200159
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200160 if (syme->src == NULL) {
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200161 syme->src = zalloc(sizeof(*source));
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200162 if (syme->src == NULL)
Arnaldo Carvalho de Melob0a9ab62010-03-15 11:46:58 -0300163 return -1;
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200164 pthread_mutex_init(&syme->src->lock, NULL);
165 }
166
167 source = syme->src;
168
169 if (source->lines) {
170 pthread_mutex_lock(&source->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200171 goto out_assign;
172 }
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300173 path = map->dso->long_name;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200174
Mike Galbraith923c42c2009-07-22 20:36:03 +0200175 len = sym->end - sym->start;
176
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300177 sprintf(command,
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200178 "objdump --start-address=%#0*" PRIx64 " --stop-address=%#0*" PRIx64 " -dS %s",
Arnaldo Carvalho de Melo5f485362010-02-07 11:46:16 -0200179 BITS_PER_LONG / 4, map__rip_2objdump(map, sym->start),
180 BITS_PER_LONG / 4, map__rip_2objdump(map, sym->end), path);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200181
182 file = popen(command, "r");
183 if (!file)
Arnaldo Carvalho de Melob0a9ab62010-03-15 11:46:58 -0300184 return -1;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200185
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200186 pthread_mutex_lock(&source->lock);
187 source->lines_tail = &source->lines;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200188 while (!feof(file)) {
189 struct source_line *src;
190 size_t dummy = 0;
Kirill Smelkovee11b902010-02-07 11:46:15 -0200191 char *c, *sep;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200192
193 src = malloc(sizeof(struct source_line));
194 assert(src != NULL);
195 memset(src, 0, sizeof(struct source_line));
196
197 if (getline(&src->line, &dummy, file) < 0)
198 break;
199 if (!src->line)
200 break;
201
202 c = strchr(src->line, '\n');
203 if (c)
204 *c = 0;
205
206 src->next = NULL;
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200207 *source->lines_tail = src;
208 source->lines_tail = &src->next;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200209
Kirill Smelkovee11b902010-02-07 11:46:15 -0200210 src->eip = strtoull(src->line, &sep, 16);
211 if (*sep == ':')
212 src->eip = map__objdump_2ip(map, src->eip);
213 else /* this line has no ip info (e.g. source line) */
214 src->eip = 0;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200215 }
216 pclose(file);
217out_assign:
218 sym_filter_entry = syme;
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200219 pthread_mutex_unlock(&source->lock);
Arnaldo Carvalho de Melob0a9ab62010-03-15 11:46:58 -0300220 return 0;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200221}
222
223static void __zero_source_counters(struct sym_entry *syme)
224{
225 int i;
226 struct source_line *line;
227
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200228 line = syme->src->lines;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200229 while (line) {
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200230 for (i = 0; i < top.evlist->nr_entries; i++)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200231 line->count[i] = 0;
232 line = line->next;
233 }
234}
235
236static void record_precise_ip(struct sym_entry *syme, int counter, u64 ip)
237{
238 struct source_line *line;
239
240 if (syme != sym_filter_entry)
241 return;
242
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200243 if (pthread_mutex_trylock(&syme->src->lock))
Mike Galbraith923c42c2009-07-22 20:36:03 +0200244 return;
245
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200246 if (syme->src == NULL || syme->src->source == NULL)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200247 goto out_unlock;
248
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200249 for (line = syme->src->lines; line; line = line->next) {
Kirill Smelkovee11b902010-02-07 11:46:15 -0200250 /* skip lines without IP info */
251 if (line->eip == 0)
252 continue;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200253 if (line->eip == ip) {
254 line->count[counter]++;
255 break;
256 }
257 if (line->eip > ip)
258 break;
259 }
260out_unlock:
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200261 pthread_mutex_unlock(&syme->src->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200262}
263
Arnaldo Carvalho de Meloc7ad21a2010-02-22 16:14:22 -0300264#define PATTERN_LEN (BITS_PER_LONG / 4 + 2)
265
Mike Galbraith923c42c2009-07-22 20:36:03 +0200266static void lookup_sym_source(struct sym_entry *syme)
267{
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200268 struct symbol *symbol = sym_entry__symbol(syme);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200269 struct source_line *line;
Arnaldo Carvalho de Meloc7ad21a2010-02-22 16:14:22 -0300270 char pattern[PATTERN_LEN + 1];
Mike Galbraith923c42c2009-07-22 20:36:03 +0200271
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200272 sprintf(pattern, "%0*" PRIx64 " <", BITS_PER_LONG / 4,
Arnaldo Carvalho de Melo5f485362010-02-07 11:46:16 -0200273 map__rip_2objdump(syme->map, symbol->start));
Mike Galbraith923c42c2009-07-22 20:36:03 +0200274
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200275 pthread_mutex_lock(&syme->src->lock);
276 for (line = syme->src->lines; line; line = line->next) {
Arnaldo Carvalho de Meloc7ad21a2010-02-22 16:14:22 -0300277 if (memcmp(line->line, pattern, PATTERN_LEN) == 0) {
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200278 syme->src->source = line;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200279 break;
280 }
281 }
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200282 pthread_mutex_unlock(&syme->src->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200283}
284
285static void show_lines(struct source_line *queue, int count, int total)
286{
287 int i;
288 struct source_line *line;
289
290 line = queue;
291 for (i = 0; i < count; i++) {
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200292 float pcnt = 100.0*(float)line->count[top.sym_counter]/(float)total;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200293
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200294 printf("%8li %4.1f%%\t%s\n", line->count[top.sym_counter], pcnt, line->line);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200295 line = line->next;
296 }
297}
298
299#define TRACE_COUNT 3
300
301static void show_details(struct sym_entry *syme)
302{
303 struct symbol *symbol;
304 struct source_line *line;
305 struct source_line *line_queue = NULL;
306 int displayed = 0;
307 int line_queue_count = 0, total = 0, more = 0;
308
309 if (!syme)
310 return;
311
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200312 if (!syme->src->source)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200313 lookup_sym_source(syme);
314
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200315 if (!syme->src->source)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200316 return;
317
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200318 symbol = sym_entry__symbol(syme);
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200319 printf("Showing %s for %s\n", event_name(top.sym_evsel), symbol->name);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200320 printf(" Events Pcnt (>=%d%%)\n", sym_pcnt_filter);
321
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200322 pthread_mutex_lock(&syme->src->lock);
323 line = syme->src->source;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200324 while (line) {
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200325 total += line->count[top.sym_counter];
Mike Galbraith923c42c2009-07-22 20:36:03 +0200326 line = line->next;
327 }
328
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200329 line = syme->src->source;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200330 while (line) {
331 float pcnt = 0.0;
332
333 if (!line_queue_count)
334 line_queue = line;
335 line_queue_count++;
336
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200337 if (line->count[top.sym_counter])
338 pcnt = 100.0 * line->count[top.sym_counter] / (float)total;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200339 if (pcnt >= (float)sym_pcnt_filter) {
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200340 if (displayed <= top.print_entries)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200341 show_lines(line_queue, line_queue_count, total);
342 else more++;
343 displayed += line_queue_count;
344 line_queue_count = 0;
345 line_queue = NULL;
346 } else if (line_queue_count > TRACE_COUNT) {
347 line_queue = line_queue->next;
348 line_queue_count--;
349 }
350
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200351 line->count[top.sym_counter] = top.zero ? 0 : line->count[top.sym_counter] * 7 / 8;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200352 line = line->next;
353 }
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200354 pthread_mutex_unlock(&syme->src->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200355 if (more)
356 printf("%d lines not displayed, maybe increase display entries [e]\n", more);
357}
Ingo Molnar07800602009-04-20 15:00:56 +0200358
Ingo Molnar07800602009-04-20 15:00:56 +0200359static const char CONSOLE_CLEAR[] = "";
360
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300361static void __list_insert_active_sym(struct sym_entry *syme)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300362{
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200363 list_add(&syme->node, &top.active_symbols);
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300364}
Ingo Molnar07800602009-04-20 15:00:56 +0200365
Arnaldo Carvalho de Melo93fc64f2011-01-29 12:08:00 -0200366static void print_sym_table(struct perf_session *session)
Ingo Molnar07800602009-04-20 15:00:56 +0200367{
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200368 char bf[160];
369 int printed = 0;
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300370 struct rb_node *nd;
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200371 struct sym_entry *syme;
372 struct rb_root tmp = RB_ROOT;
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200373 const int win_width = winsize.ws_col - 1;
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200374 int sym_width, dso_width, dso_short_width;
375 float sum_ksamples = perf_top__decay_samples(&top, &tmp);
Mike Galbraithd94b9432009-05-25 09:57:56 +0200376
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200377 puts(CONSOLE_CLEAR);
Ingo Molnar07800602009-04-20 15:00:56 +0200378
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200379 perf_top__header_snprintf(&top, bf, sizeof(bf));
380 printf("%s\n", bf);
Ingo Molnar07800602009-04-20 15:00:56 +0200381
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200382 perf_top__reset_sample_counters(&top);
Ingo Molnar07800602009-04-20 15:00:56 +0200383
Arnaldo Carvalho de Melo1a105f72009-11-17 15:40:55 -0200384 printf("%-*.*s\n", win_width, win_width, graph_dotted_line);
Ingo Molnar07800602009-04-20 15:00:56 +0200385
Arnaldo Carvalho de Melo93fc64f2011-01-29 12:08:00 -0200386 if (session->hists.stats.total_lost != 0) {
387 color_fprintf(stdout, PERF_COLOR_RED, "WARNING:");
388 printf(" LOST %" PRIu64 " events, Check IO/CPU overload\n",
389 session->hists.stats.total_lost);
390 }
391
Mike Galbraith923c42c2009-07-22 20:36:03 +0200392 if (sym_filter_entry) {
393 show_details(sym_filter_entry);
394 return;
395 }
396
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200397 perf_top__find_widths(&top, &tmp, &dso_width, &dso_short_width,
398 &sym_width);
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200399
Arnaldo Carvalho de Melob63be8d2010-03-15 15:03:50 -0300400 if (sym_width + dso_width > winsize.ws_col - 29) {
401 dso_width = dso_short_width;
402 if (sym_width + dso_width > winsize.ws_col - 29)
403 sym_width = winsize.ws_col - dso_width - 29;
404 }
Arnaldo Carvalho de Melo7cc017e2009-11-24 12:05:14 -0200405 putchar('\n');
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200406 if (top.evlist->nr_entries == 1)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200407 printf(" samples pcnt");
Ingo Molnar07800602009-04-20 15:00:56 +0200408 else
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200409 printf(" weight samples pcnt");
Ingo Molnar07800602009-04-20 15:00:56 +0200410
Arnaldo Carvalho de Melo7ced1562009-08-26 11:51:26 -0300411 if (verbose)
412 printf(" RIP ");
Arnaldo Carvalho de Melo7cc017e2009-11-24 12:05:14 -0200413 printf(" %-*.*s DSO\n", sym_width, sym_width, "function");
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200414 printf(" %s _______ _____",
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200415 top.evlist->nr_entries == 1 ? " " : "______");
Arnaldo Carvalho de Melo7ced1562009-08-26 11:51:26 -0300416 if (verbose)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200417 printf(" ________________");
Arnaldo Carvalho de Melo1a105f72009-11-17 15:40:55 -0200418 printf(" %-*.*s", sym_width, sym_width, graph_line);
Arnaldo Carvalho de Melo7cc017e2009-11-24 12:05:14 -0200419 printf(" %-*.*s", dso_width, dso_width, graph_line);
Arnaldo Carvalho de Melo1a105f72009-11-17 15:40:55 -0200420 puts("\n");
Ingo Molnar07800602009-04-20 15:00:56 +0200421
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300422 for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
Ingo Molnar83a09442009-08-15 12:26:57 +0200423 struct symbol *sym;
Ingo Molnar8fc03212009-06-04 15:19:47 +0200424 double pcnt;
Ingo Molnar07800602009-04-20 15:00:56 +0200425
Ingo Molnar83a09442009-08-15 12:26:57 +0200426 syme = rb_entry(nd, struct sym_entry, rb_node);
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200427 sym = sym_entry__symbol(syme);
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200428 if (++printed > top.print_entries ||
429 (int)syme->snap_count < top.count_filter)
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300430 continue;
Ingo Molnar07800602009-04-20 15:00:56 +0200431
Ingo Molnar2debbc82009-06-05 14:29:10 +0200432 pcnt = 100.0 - (100.0 * ((sum_ksamples - syme->snap_count) /
433 sum_ksamples));
Mike Galbraithd94b9432009-05-25 09:57:56 +0200434
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200435 if (top.evlist->nr_entries == 1 || !top.display_weighted)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200436 printf("%20.2f ", syme->weight);
Mike Galbraithd94b9432009-05-25 09:57:56 +0200437 else
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200438 printf("%9.1f %10ld ", syme->weight, syme->snap_count);
Ingo Molnar8fc03212009-06-04 15:19:47 +0200439
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200440 percent_color_fprintf(stdout, "%4.1f%%", pcnt);
Arnaldo Carvalho de Melo7ced1562009-08-26 11:51:26 -0300441 if (verbose)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200442 printf(" %016" PRIx64, sym->start);
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200443 printf(" %-*.*s", sym_width, sym_width, sym->name);
Arnaldo Carvalho de Melo7cc017e2009-11-24 12:05:14 -0200444 printf(" %-*.*s\n", dso_width, dso_width,
445 dso_width >= syme->map->dso->long_name_len ?
446 syme->map->dso->long_name :
447 syme->map->dso->short_name);
Ingo Molnar07800602009-04-20 15:00:56 +0200448 }
Ingo Molnar07800602009-04-20 15:00:56 +0200449}
450
Mike Galbraith923c42c2009-07-22 20:36:03 +0200451static void prompt_integer(int *target, const char *msg)
452{
453 char *buf = malloc(0), *p;
454 size_t dummy = 0;
455 int tmp;
456
457 fprintf(stdout, "\n%s: ", msg);
458 if (getline(&buf, &dummy, stdin) < 0)
459 return;
460
461 p = strchr(buf, '\n');
462 if (p)
463 *p = 0;
464
465 p = buf;
466 while(*p) {
467 if (!isdigit(*p))
468 goto out_free;
469 p++;
470 }
471 tmp = strtoul(buf, NULL, 10);
472 *target = tmp;
473out_free:
474 free(buf);
475}
476
477static void prompt_percent(int *target, const char *msg)
478{
479 int tmp = 0;
480
481 prompt_integer(&tmp, msg);
482 if (tmp >= 0 && tmp <= 100)
483 *target = tmp;
484}
485
486static void prompt_symbol(struct sym_entry **target, const char *msg)
487{
488 char *buf = malloc(0), *p;
489 struct sym_entry *syme = *target, *n, *found = NULL;
490 size_t dummy = 0;
491
492 /* zero counters of active symbol */
493 if (syme) {
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200494 pthread_mutex_lock(&syme->src->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200495 __zero_source_counters(syme);
496 *target = NULL;
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200497 pthread_mutex_unlock(&syme->src->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200498 }
499
500 fprintf(stdout, "\n%s: ", msg);
501 if (getline(&buf, &dummy, stdin) < 0)
502 goto out_free;
503
504 p = strchr(buf, '\n');
505 if (p)
506 *p = 0;
507
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200508 pthread_mutex_lock(&top.active_symbols_lock);
509 syme = list_entry(top.active_symbols.next, struct sym_entry, node);
510 pthread_mutex_unlock(&top.active_symbols_lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200511
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200512 list_for_each_entry_safe_from(syme, n, &top.active_symbols, node) {
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200513 struct symbol *sym = sym_entry__symbol(syme);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200514
515 if (!strcmp(buf, sym->name)) {
516 found = syme;
517 break;
518 }
519 }
520
521 if (!found) {
Kirill Smelkov66aeb6d2010-01-13 13:22:19 -0200522 fprintf(stderr, "Sorry, %s is not active.\n", buf);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200523 sleep(1);
524 return;
525 } else
526 parse_source(found);
527
528out_free:
529 free(buf);
530}
531
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200532static void print_mapped_keys(void)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200533{
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200534 char *name = NULL;
535
536 if (sym_filter_entry) {
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200537 struct symbol *sym = sym_entry__symbol(sym_filter_entry);
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200538 name = sym->name;
539 }
540
541 fprintf(stdout, "\nMapped keys:\n");
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200542 fprintf(stdout, "\t[d] display refresh delay. \t(%d)\n", top.delay_secs);
543 fprintf(stdout, "\t[e] display entries (lines). \t(%d)\n", top.print_entries);
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200544
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200545 if (top.evlist->nr_entries > 1)
546 fprintf(stdout, "\t[E] active event counter. \t(%s)\n", event_name(top.sym_evsel));
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200547
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200548 fprintf(stdout, "\t[f] profile display filter (count). \t(%d)\n", top.count_filter);
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200549
Kirill Smelkov6cff0e82010-02-03 16:52:08 -0200550 fprintf(stdout, "\t[F] annotate display filter (percent). \t(%d%%)\n", sym_pcnt_filter);
551 fprintf(stdout, "\t[s] annotate symbol. \t(%s)\n", name?: "NULL");
552 fprintf(stdout, "\t[S] stop annotation.\n");
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200553
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200554 if (top.evlist->nr_entries > 1)
555 fprintf(stdout, "\t[w] toggle display weighted/count[E]r. \t(%d)\n", top.display_weighted ? 1 : 0);
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200556
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200557 fprintf(stdout,
Kirill Smelkov1a72cfa2010-02-12 19:20:59 +0300558 "\t[K] hide kernel_symbols symbols. \t(%s)\n",
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200559 top.hide_kernel_symbols ? "yes" : "no");
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200560 fprintf(stdout,
561 "\t[U] hide user symbols. \t(%s)\n",
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200562 top.hide_user_symbols ? "yes" : "no");
563 fprintf(stdout, "\t[z] toggle sample zeroing. \t(%d)\n", top.zero ? 1 : 0);
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200564 fprintf(stdout, "\t[qQ] quit.\n");
565}
566
567static int key_mapped(int c)
568{
569 switch (c) {
570 case 'd':
571 case 'e':
572 case 'f':
573 case 'z':
574 case 'q':
575 case 'Q':
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200576 case 'K':
577 case 'U':
Kirill Smelkov6cff0e82010-02-03 16:52:08 -0200578 case 'F':
579 case 's':
580 case 'S':
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200581 return 1;
582 case 'E':
583 case 'w':
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200584 return top.evlist->nr_entries > 1 ? 1 : 0;
Ingo Molnar83a09442009-08-15 12:26:57 +0200585 default:
586 break;
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200587 }
588
589 return 0;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200590}
591
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800592static void handle_keypress(struct perf_session *session, int c)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200593{
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200594 if (!key_mapped(c)) {
595 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
596 struct termios tc, save;
597
598 print_mapped_keys();
599 fprintf(stdout, "\nEnter selection, or unmapped key to continue: ");
600 fflush(stdout);
601
602 tcgetattr(0, &save);
603 tc = save;
604 tc.c_lflag &= ~(ICANON | ECHO);
605 tc.c_cc[VMIN] = 0;
606 tc.c_cc[VTIME] = 0;
607 tcsetattr(0, TCSANOW, &tc);
608
609 poll(&stdin_poll, 1, -1);
610 c = getc(stdin);
611
612 tcsetattr(0, TCSAFLUSH, &save);
613 if (!key_mapped(c))
614 return;
615 }
616
Mike Galbraith923c42c2009-07-22 20:36:03 +0200617 switch (c) {
618 case 'd':
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200619 prompt_integer(&top.delay_secs, "Enter display delay");
620 if (top.delay_secs < 1)
621 top.delay_secs = 1;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200622 break;
623 case 'e':
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200624 prompt_integer(&top.print_entries, "Enter display entries (lines)");
625 if (top.print_entries == 0) {
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200626 sig_winch_handler(SIGWINCH);
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200627 signal(SIGWINCH, sig_winch_handler);
628 } else
629 signal(SIGWINCH, SIG_DFL);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200630 break;
631 case 'E':
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200632 if (top.evlist->nr_entries > 1) {
Mike Galbraith923c42c2009-07-22 20:36:03 +0200633 fprintf(stderr, "\nAvailable events:");
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200634
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200635 list_for_each_entry(top.sym_evsel, &top.evlist->entries, node)
636 fprintf(stderr, "\n\t%d %s", top.sym_evsel->idx, event_name(top.sym_evsel));
Mike Galbraith923c42c2009-07-22 20:36:03 +0200637
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200638 prompt_integer(&top.sym_counter, "Enter details event counter");
Mike Galbraith923c42c2009-07-22 20:36:03 +0200639
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200640 if (top.sym_counter >= top.evlist->nr_entries) {
641 top.sym_evsel = list_entry(top.evlist->entries.next, struct perf_evsel, node);
642 top.sym_counter = 0;
643 fprintf(stderr, "Sorry, no such event, using %s.\n", event_name(top.sym_evsel));
Mike Galbraith923c42c2009-07-22 20:36:03 +0200644 sleep(1);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200645 break;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200646 }
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200647 list_for_each_entry(top.sym_evsel, &top.evlist->entries, node)
648 if (top.sym_evsel->idx == top.sym_counter)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200649 break;
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200650 } else top.sym_counter = 0;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200651 break;
652 case 'f':
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200653 prompt_integer(&top.count_filter, "Enter display event count filter");
Mike Galbraith923c42c2009-07-22 20:36:03 +0200654 break;
655 case 'F':
656 prompt_percent(&sym_pcnt_filter, "Enter details display event filter (percent)");
657 break;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200658 case 'K':
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200659 top.hide_kernel_symbols = !top.hide_kernel_symbols;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200660 break;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200661 case 'q':
662 case 'Q':
663 printf("exiting.\n");
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200664 if (dump_symtab)
Arnaldo Carvalho de Melocbf69682010-04-27 21:22:44 -0300665 perf_session__fprintf_dsos(session, stderr);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200666 exit(0);
667 case 's':
668 prompt_symbol(&sym_filter_entry, "Enter details symbol");
669 break;
670 case 'S':
671 if (!sym_filter_entry)
672 break;
673 else {
674 struct sym_entry *syme = sym_filter_entry;
675
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200676 pthread_mutex_lock(&syme->src->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200677 sym_filter_entry = NULL;
678 __zero_source_counters(syme);
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200679 pthread_mutex_unlock(&syme->src->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200680 }
681 break;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200682 case 'U':
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200683 top.hide_user_symbols = !top.hide_user_symbols;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200684 break;
Mike Galbraith46ab9762009-07-24 10:09:50 +0200685 case 'w':
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200686 top.display_weighted = ~top.display_weighted;
Mike Galbraith46ab9762009-07-24 10:09:50 +0200687 break;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200688 case 'z':
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200689 top.zero = !top.zero;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200690 break;
Ingo Molnar83a09442009-08-15 12:26:57 +0200691 default:
692 break;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200693 }
694}
695
Arnaldo Carvalho de Meloc0443df2011-01-31 18:19:33 -0200696static void *display_thread_tui(void *arg __used)
697{
698 perf_top__tui_browser(&top);
699 exit_browser(0);
700 exit(0);
701 return NULL;
702}
703
Ingo Molnarf37a2912009-07-01 12:37:06 +0200704static void *display_thread(void *arg __used)
Ingo Molnar07800602009-04-20 15:00:56 +0200705{
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200706 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
Mike Galbraith923c42c2009-07-22 20:36:03 +0200707 struct termios tc, save;
708 int delay_msecs, c;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800709 struct perf_session *session = (struct perf_session *) arg;
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200710
Mike Galbraith923c42c2009-07-22 20:36:03 +0200711 tcgetattr(0, &save);
712 tc = save;
713 tc.c_lflag &= ~(ICANON | ECHO);
714 tc.c_cc[VMIN] = 0;
715 tc.c_cc[VTIME] = 0;
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200716
Mike Galbraith923c42c2009-07-22 20:36:03 +0200717repeat:
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200718 delay_msecs = top.delay_secs * 1000;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200719 tcsetattr(0, TCSANOW, &tc);
720 /* trash return*/
721 getc(stdin);
Ingo Molnar07800602009-04-20 15:00:56 +0200722
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200723 do {
Arnaldo Carvalho de Melo93fc64f2011-01-29 12:08:00 -0200724 print_sym_table(session);
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200725 } while (!poll(&stdin_poll, 1, delay_msecs) == 1);
726
Mike Galbraith923c42c2009-07-22 20:36:03 +0200727 c = getc(stdin);
728 tcsetattr(0, TCSAFLUSH, &save);
729
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800730 handle_keypress(session, c);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200731 goto repeat;
Ingo Molnar07800602009-04-20 15:00:56 +0200732
733 return NULL;
734}
735
Anton Blanchard2ab52082009-07-01 09:00:46 +1000736/* Tag samples to be skipped. */
Ingo Molnarf37a2912009-07-01 12:37:06 +0200737static const char *skip_symbols[] = {
Anton Blanchard2ab52082009-07-01 09:00:46 +1000738 "default_idle",
Arnaldo Carvalho de Melob0e85722011-01-16 17:39:15 -0200739 "native_safe_halt",
Anton Blanchard2ab52082009-07-01 09:00:46 +1000740 "cpu_idle",
741 "enter_idle",
742 "exit_idle",
743 "mwait_idle",
Arnaldo Carvalho de Melo59b90052009-07-26 19:06:19 -0300744 "mwait_idle_with_hints",
Arnaldo Carvalho de Melo83572752009-09-25 15:02:39 -0700745 "poll_idle",
Anton Blanchard3a3393e2009-07-01 09:00:47 +1000746 "ppc64_runlatch_off",
747 "pseries_dedicated_idle_sleep",
Anton Blanchard2ab52082009-07-01 09:00:46 +1000748 NULL
749};
750
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300751static int symbol_filter(struct map *map, struct symbol *sym)
Ingo Molnar07800602009-04-20 15:00:56 +0200752{
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300753 struct sym_entry *syme;
754 const char *name = sym->name;
Anton Blanchard2ab52082009-07-01 09:00:46 +1000755 int i;
Ingo Molnar07800602009-04-20 15:00:56 +0200756
Anton Blanchard3a3393e2009-07-01 09:00:47 +1000757 /*
758 * ppc64 uses function descriptors and appends a '.' to the
759 * start of every instruction address. Remove it.
760 */
761 if (name[0] == '.')
762 name++;
763
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300764 if (!strcmp(name, "_text") ||
765 !strcmp(name, "_etext") ||
766 !strcmp(name, "_sinittext") ||
767 !strncmp("init_module", name, 11) ||
768 !strncmp("cleanup_module", name, 14) ||
769 strstr(name, "_text_start") ||
770 strstr(name, "_text_end"))
Ingo Molnar07800602009-04-20 15:00:56 +0200771 return 1;
772
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200773 syme = symbol__priv(sym);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300774 syme->map = map;
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200775 syme->src = NULL;
Kirill Smelkov6cff0e82010-02-03 16:52:08 -0200776
777 if (!sym_filter_entry && sym_filter && !strcmp(name, sym_filter)) {
778 /* schedule initial sym_filter_entry setup */
779 sym_filter_entry_sched = syme;
780 sym_filter = NULL;
781 }
Mike Galbraith923c42c2009-07-22 20:36:03 +0200782
Anton Blanchard2ab52082009-07-01 09:00:46 +1000783 for (i = 0; skip_symbols[i]; i++) {
784 if (!strcmp(skip_symbols[i], name)) {
785 syme->skip = 1;
786 break;
787 }
788 }
Ingo Molnar07800602009-04-20 15:00:56 +0200789
Ingo Molnar07800602009-04-20 15:00:56 +0200790 return 0;
791}
792
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200793static void perf_event__process_sample(const union perf_event *event,
794 struct perf_sample *sample,
795 struct perf_session *session)
Ingo Molnar07800602009-04-20 15:00:56 +0200796{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200797 u64 ip = event->ip.ip;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200798 struct sym_entry *syme;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200799 struct addr_location al;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300800 struct machine *machine;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200801 u8 origin = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Ingo Molnar07800602009-04-20 15:00:56 +0200802
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200803 ++top.samples;
Arnaldo Carvalho de Melo24bfef02010-01-21 13:04:43 -0200804
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200805 switch (origin) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200806 case PERF_RECORD_MISC_USER:
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200807 ++top.us_samples;
808 if (top.hide_user_symbols)
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200809 return;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300810 machine = perf_session__find_host_machine(session);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200811 break;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200812 case PERF_RECORD_MISC_KERNEL:
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200813 ++top.kernel_samples;
814 if (top.hide_kernel_symbols)
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200815 return;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300816 machine = perf_session__find_host_machine(session);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200817 break;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800818 case PERF_RECORD_MISC_GUEST_KERNEL:
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200819 ++top.guest_kernel_samples;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200820 machine = perf_session__find_machine(session, event->ip.pid);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800821 break;
822 case PERF_RECORD_MISC_GUEST_USER:
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200823 ++top.guest_us_samples;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800824 /*
825 * TODO: we don't process guest user from host side
826 * except simple counting.
827 */
828 return;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200829 default:
Ingo Molnar07800602009-04-20 15:00:56 +0200830 return;
831 }
832
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300833 if (!machine && perf_guest) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800834 pr_err("Can't find guest [%d]'s kernel information\n",
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200835 event->ip.pid);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800836 return;
837 }
838
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200839 if (event->header.misc & PERF_RECORD_MISC_EXACT_IP)
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200840 top.exact_samples++;
Peter Zijlstra1676b8a2010-03-04 14:19:36 +0100841
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200842 if (perf_event__preprocess_sample(event, session, &al, sample,
843 symbol_filter) < 0 ||
Arnaldo Carvalho de Melo72b8fa12010-01-27 21:05:49 -0200844 al.filtered)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200845 return;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200846
Arnaldo Carvalho de Melo72b8fa12010-01-27 21:05:49 -0200847 if (al.sym == NULL) {
848 /*
849 * As we do lazy loading of symtabs we only will know if the
850 * specified vmlinux file is invalid when we actually have a
851 * hit in kernel space and then try to load it. So if we get
852 * here and there are _no_ symbols in the DSO backing the
853 * kernel map, bail out.
854 *
855 * We may never get here, for instance, if we use -K/
856 * --hide-kernel-symbols, even if the user specifies an
857 * invalid --vmlinux ;-)
858 */
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300859 if (al.map == machine->vmlinux_maps[MAP__FUNCTION] &&
Arnaldo Carvalho de Melo72b8fa12010-01-27 21:05:49 -0200860 RB_EMPTY_ROOT(&al.map->dso->symbols[MAP__FUNCTION])) {
861 pr_err("The %s file can't be used\n",
862 symbol_conf.vmlinux_name);
863 exit(1);
864 }
865
866 return;
867 }
868
Kirill Smelkov6cff0e82010-02-03 16:52:08 -0200869 /* let's see, whether we need to install initial sym_filter_entry */
870 if (sym_filter_entry_sched) {
871 sym_filter_entry = sym_filter_entry_sched;
872 sym_filter_entry_sched = NULL;
Arnaldo Carvalho de Melob0a9ab62010-03-15 11:46:58 -0300873 if (parse_source(sym_filter_entry) < 0) {
874 struct symbol *sym = sym_entry__symbol(sym_filter_entry);
875
876 pr_err("Can't annotate %s", sym->name);
877 if (sym_filter_entry->map->dso->origin == DSO__ORIG_KERNEL) {
878 pr_err(": No vmlinux file was found in the path:\n");
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -0300879 machine__fprintf_vmlinux_path(machine, stderr);
Arnaldo Carvalho de Melob0a9ab62010-03-15 11:46:58 -0300880 } else
881 pr_err(".\n");
882 exit(1);
883 }
Kirill Smelkov6cff0e82010-02-03 16:52:08 -0200884 }
885
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200886 syme = symbol__priv(al.sym);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200887 if (!syme->skip) {
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200888 struct perf_evsel *evsel;
889
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200890 syme->origin = origin;
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200891 evsel = perf_evlist__id2evsel(top.evlist, sample->id);
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200892 assert(evsel != NULL);
893 syme->count[evsel->idx]++;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200894 record_precise_ip(syme, evsel->idx, ip);
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200895 pthread_mutex_lock(&top.active_symbols_lock);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200896 if (list_empty(&syme->node) || !syme->node.next)
897 __list_insert_active_sym(syme);
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200898 pthread_mutex_unlock(&top.active_symbols_lock);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200899 }
900}
901
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200902static void perf_session__mmap_read_cpu(struct perf_session *self, int cpu)
Ingo Molnar07800602009-04-20 15:00:56 +0200903{
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200904 struct perf_sample sample;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200905 union perf_event *event;
Ingo Molnar07800602009-04-20 15:00:56 +0200906
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200907 while ((event = perf_evlist__read_on_cpu(top.evlist, cpu)) != NULL) {
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200908 perf_session__parse_sample(self, event, &sample);
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200909
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200910 if (event->header.type == PERF_RECORD_SAMPLE)
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200911 perf_event__process_sample(event, &sample, self);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200912 else
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200913 perf_event__process(event, &sample, self);
Ingo Molnar07800602009-04-20 15:00:56 +0200914 }
Ingo Molnar07800602009-04-20 15:00:56 +0200915}
916
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200917static void perf_session__mmap_read(struct perf_session *self)
Frederic Weisbecker2f011902009-06-06 23:10:43 +0200918{
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200919 int i;
Frederic Weisbecker2f011902009-06-06 23:10:43 +0200920
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200921 for (i = 0; i < top.evlist->cpus->nr; i++)
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200922 perf_session__mmap_read_cpu(self, i);
Frederic Weisbecker2f011902009-06-06 23:10:43 +0200923}
924
Arnaldo Carvalho de Melo72cb7012011-01-12 10:52:47 -0200925static void start_counters(struct perf_evlist *evlist)
926{
927 struct perf_evsel *counter;
Arnaldo Carvalho de Melo72cb7012011-01-12 10:52:47 -0200928
929 list_for_each_entry(counter, &evlist->entries, node) {
930 struct perf_event_attr *attr = &counter->attr;
931
932 attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
933
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200934 if (top.freq) {
Arnaldo Carvalho de Melo72cb7012011-01-12 10:52:47 -0200935 attr->sample_type |= PERF_SAMPLE_PERIOD;
936 attr->freq = 1;
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200937 attr->sample_freq = top.freq;
Arnaldo Carvalho de Melo72cb7012011-01-12 10:52:47 -0200938 }
939
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200940 if (evlist->nr_entries > 1) {
941 attr->sample_type |= PERF_SAMPLE_ID;
942 attr->read_format |= PERF_FORMAT_ID;
943 }
944
Arnaldo Carvalho de Melo72cb7012011-01-12 10:52:47 -0200945 attr->mmap = 1;
946try_again:
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200947 if (perf_evsel__open(counter, top.evlist->cpus,
948 top.evlist->threads, group, inherit) < 0) {
Arnaldo Carvalho de Melo72cb7012011-01-12 10:52:47 -0200949 int err = errno;
950
951 if (err == EPERM || err == EACCES)
952 die("Permission error - are you root?\n"
953 "\t Consider tweaking"
954 " /proc/sys/kernel/perf_event_paranoid.\n");
955 /*
956 * If it's cycles then fall back to hrtimer
957 * based cpu-clock-tick sw counter, which
958 * is always available even if no PMU support:
959 */
960 if (attr->type == PERF_TYPE_HARDWARE &&
961 attr->config == PERF_COUNT_HW_CPU_CYCLES) {
962
963 if (verbose)
964 warning(" ... trying to fall back to cpu-clock-ticks\n");
965
966 attr->type = PERF_TYPE_SOFTWARE;
967 attr->config = PERF_COUNT_SW_CPU_CLOCK;
968 goto try_again;
969 }
970 printf("\n");
971 error("sys_perf_event_open() syscall returned with %d "
972 "(%s). /bin/dmesg may provide additional information.\n",
973 err, strerror(err));
974 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
975 exit(-1);
976 }
Arnaldo Carvalho de Melo72cb7012011-01-12 10:52:47 -0200977 }
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200978
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200979 if (perf_evlist__mmap(evlist, mmap_pages, false) < 0)
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200980 die("failed to mmap with %d (%s)\n", errno, strerror(errno));
Arnaldo Carvalho de Melo72cb7012011-01-12 10:52:47 -0200981}
982
Ingo Molnar716c69f2009-06-07 17:31:52 +0200983static int __cmd_top(void)
984{
985 pthread_t thread;
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200986 struct perf_evsel *first;
Arnaldo Carvalho de Melo72cb7012011-01-12 10:52:47 -0200987 int ret;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200988 /*
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200989 * FIXME: perf_session__new should allow passing a O_MMAP, so that all this
990 * mmap reading, etc is encapsulated in it. Use O_WRONLY for now.
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200991 */
Ian Munsie21ef97f2010-12-10 14:09:16 +1100992 struct perf_session *session = perf_session__new(NULL, O_WRONLY, false, false, NULL);
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200993 if (session == NULL)
994 return -ENOMEM;
Ingo Molnar07800602009-04-20 15:00:56 +0200995
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200996 if (top.target_tid != -1)
997 perf_event__synthesize_thread(top.target_tid, perf_event__process,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200998 session);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200999 else
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001000 perf_event__synthesize_threads(perf_event__process, session);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001001
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001002 start_counters(top.evlist);
1003 first = list_entry(top.evlist->entries.next, struct perf_evsel, node);
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -02001004 perf_session__set_sample_type(session, first->attr.sample_type);
Ingo Molnar07800602009-04-20 15:00:56 +02001005
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001006 /* Wait for a minimal set of events before starting the snapshot */
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001007 poll(top.evlist->pollfd, top.evlist->nr_fds, 100);
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001008
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001009 perf_session__mmap_read(session);
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001010
Arnaldo Carvalho de Meloc0443df2011-01-31 18:19:33 -02001011 if (pthread_create(&thread, NULL, (use_browser > 0 ? display_thread_tui :
1012 display_thread), session)) {
Ingo Molnar07800602009-04-20 15:00:56 +02001013 printf("Could not create display thread.\n");
1014 exit(-1);
1015 }
1016
1017 if (realtime_prio) {
1018 struct sched_param param;
1019
1020 param.sched_priority = realtime_prio;
1021 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
1022 printf("Could not set realtime priority.\n");
1023 exit(-1);
1024 }
1025 }
1026
1027 while (1) {
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001028 u64 hits = top.samples;
Ingo Molnar07800602009-04-20 15:00:56 +02001029
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001030 perf_session__mmap_read(session);
Ingo Molnar07800602009-04-20 15:00:56 +02001031
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001032 if (hits == top.samples)
1033 ret = poll(top.evlist->pollfd, top.evlist->nr_fds, 100);
Ingo Molnar07800602009-04-20 15:00:56 +02001034 }
1035
1036 return 0;
1037}
Ingo Molnarb456bae2009-05-26 09:17:18 +02001038
1039static const char * const top_usage[] = {
1040 "perf top [<options>]",
1041 NULL
1042};
1043
Ingo Molnarb456bae2009-05-26 09:17:18 +02001044static const struct option options[] = {
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001045 OPT_CALLBACK('e', "event", &top.evlist, "event",
Thomas Gleixner86847b62009-06-06 12:24:17 +02001046 "event selector. use 'perf list' to list available events",
1047 parse_events),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001048 OPT_INTEGER('c', "count", &default_interval,
1049 "event period to sample"),
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001050 OPT_INTEGER('p', "pid", &top.target_pid,
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001051 "profile events on existing process id"),
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001052 OPT_INTEGER('t', "tid", &top.target_tid,
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001053 "profile events on existing thread id"),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001054 OPT_BOOLEAN('a', "all-cpus", &system_wide,
1055 "system-wide collection from all CPUs"),
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001056 OPT_STRING('C', "cpu", &top.cpu_list, "cpu",
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02001057 "list of cpus to monitor"),
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02001058 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1059 "file", "vmlinux pathname"),
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001060 OPT_BOOLEAN('K', "hide_kernel_symbols", &top.hide_kernel_symbols,
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -02001061 "hide kernel symbols"),
Arnaldo Carvalho de Melo19679362010-05-17 15:39:16 -03001062 OPT_UINTEGER('m', "mmap-pages", &mmap_pages, "number of mmap data pages"),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001063 OPT_INTEGER('r', "realtime", &realtime_prio,
1064 "collect data with this RT SCHED_FIFO priority"),
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001065 OPT_INTEGER('d', "delay", &top.delay_secs,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001066 "number of seconds to delay between refreshes"),
1067 OPT_BOOLEAN('D', "dump-symtab", &dump_symtab,
1068 "dump the symbol table used for profiling"),
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001069 OPT_INTEGER('f', "count-filter", &top.count_filter,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001070 "only display functions with more events than this"),
1071 OPT_BOOLEAN('g', "group", &group,
1072 "put the counters into a counter group"),
Mike Galbraith0fdc7e62009-07-21 10:30:36 +02001073 OPT_BOOLEAN('i', "inherit", &inherit,
1074 "child tasks inherit counters"),
Mike Galbraith923c42c2009-07-22 20:36:03 +02001075 OPT_STRING('s', "sym-annotate", &sym_filter, "symbol name",
Kirill Smelkov6cff0e82010-02-03 16:52:08 -02001076 "symbol to annotate"),
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001077 OPT_BOOLEAN('z', "zero", &top.zero,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001078 "zero history across updates"),
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001079 OPT_INTEGER('F', "freq", &top.freq,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001080 "profile at this frequency"),
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001081 OPT_INTEGER('E', "entries", &top.print_entries,
Ingo Molnar6e53cdf2009-06-04 08:53:05 +02001082 "display this many functions"),
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001083 OPT_BOOLEAN('U', "hide_user_symbols", &top.hide_user_symbols,
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -02001084 "hide user symbols"),
Arnaldo Carvalho de Meloc0443df2011-01-31 18:19:33 -02001085 OPT_BOOLEAN(0, "tui", &use_tui, "Use the TUI interface"),
1086 OPT_BOOLEAN(0, "stdio", &use_stdio, "Use the stdio interface"),
Ian Munsiec0555642010-04-13 18:37:33 +10001087 OPT_INCR('v', "verbose", &verbose,
Ingo Molnar3da297a2009-06-07 17:39:02 +02001088 "be more verbose (show counter open errors, etc)"),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001089 OPT_END()
1090};
1091
Ingo Molnarf37a2912009-07-01 12:37:06 +02001092int cmd_top(int argc, const char **argv, const char *prefix __used)
Ingo Molnarb456bae2009-05-26 09:17:18 +02001093{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001094 struct perf_evsel *pos;
1095 int status = -ENOMEM;
Ingo Molnarb456bae2009-05-26 09:17:18 +02001096
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001097 top.evlist = perf_evlist__new(NULL, NULL);
1098 if (top.evlist == NULL)
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001099 return -ENOMEM;
1100
Ingo Molnarb456bae2009-05-26 09:17:18 +02001101 page_size = sysconf(_SC_PAGE_SIZE);
1102
Ingo Molnarb456bae2009-05-26 09:17:18 +02001103 argc = parse_options(argc, argv, options, top_usage, 0);
1104 if (argc)
1105 usage_with_options(top_usage, options);
1106
Arnaldo Carvalho de Meloc0443df2011-01-31 18:19:33 -02001107 /*
1108 * XXX For now start disabled, only using TUI if explicitely asked for.
1109 * Change that when handle_keys equivalent gets written, live annotation
1110 * done, etc.
1111 */
1112 use_browser = 0;
1113
1114 if (use_stdio)
1115 use_browser = 0;
1116 else if (use_tui)
1117 use_browser = 1;
1118
1119 setup_browser(false);
1120
Ingo Molnarb456bae2009-05-26 09:17:18 +02001121 /* CPU and PID are mutually exclusive */
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001122 if (top.target_tid > 0 && top.cpu_list) {
Ingo Molnarb456bae2009-05-26 09:17:18 +02001123 printf("WARNING: PID switch overriding CPU\n");
1124 sleep(1);
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001125 top.cpu_list = NULL;
Ingo Molnarb456bae2009-05-26 09:17:18 +02001126 }
1127
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001128 if (top.target_pid != -1)
1129 top.target_tid = top.target_pid;
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -02001130
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001131 if (perf_evlist__create_maps(top.evlist, top.target_pid,
1132 top.target_tid, top.cpu_list) < 0)
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -02001133 usage_with_options(top_usage, options);
1134
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001135 if (!top.evlist->nr_entries &&
1136 perf_evlist__add_default(top.evlist) < 0) {
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001137 pr_err("Not enough memory for event selector list\n");
1138 return -ENOMEM;
1139 }
Arnaldo Carvalho de Melo5a8e5a32009-11-17 18:38:01 -02001140
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001141 if (top.delay_secs < 1)
1142 top.delay_secs = 1;
Frederic Weisbecker2f335a02009-06-05 19:31:01 +02001143
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +02001144 /*
1145 * User specified count overrides default frequency.
1146 */
1147 if (default_interval)
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001148 top.freq = 0;
1149 else if (top.freq) {
1150 default_interval = top.freq;
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +02001151 } else {
1152 fprintf(stderr, "frequency and count are zero, aborting\n");
1153 exit(EXIT_FAILURE);
1154 }
1155
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001156 list_for_each_entry(pos, &top.evlist->entries, node) {
1157 if (perf_evsel__alloc_fd(pos, top.evlist->cpus->nr,
1158 top.evlist->threads->nr) < 0)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001159 goto out_free_fd;
1160 /*
1161 * Fill in the ones not specifically initialized via -c:
1162 */
1163 if (pos->attr.sample_period)
1164 continue;
1165
1166 pos->attr.sample_period = default_interval;
1167 }
1168
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001169 if (perf_evlist__alloc_pollfd(top.evlist) < 0 ||
1170 perf_evlist__alloc_mmap(top.evlist) < 0)
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -02001171 goto out_free_fd;
1172
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001173 top.sym_evsel = list_entry(top.evlist->entries.next, struct perf_evsel, node);
Arnaldo Carvalho de Melocc841582011-01-11 15:16:52 -02001174
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001175 symbol_conf.priv_size = (sizeof(struct sym_entry) +
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001176 (top.evlist->nr_entries + 1) * sizeof(unsigned long));
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001177
1178 symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL);
1179 if (symbol__init() < 0)
1180 return -1;
1181
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -02001182 get_term_dimensions(&winsize);
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001183 if (top.print_entries == 0) {
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -02001184 update_print_entries(&winsize);
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -02001185 signal(SIGWINCH, sig_winch_handler);
1186 }
1187
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001188 status = __cmd_top();
1189out_free_fd:
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001190 perf_evlist__delete(top.evlist);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001191
1192 return status;
Ingo Molnarb456bae2009-05-26 09:17:18 +02001193}