blob: 5f3ac9ff354d6c0f8b5840c0dda840b29ddf7739 [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
Ingo Molnar8fc03212009-06-04 15:19:47 +020023#include "util/color.h"
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -020024#include "util/session.h"
25#include "util/symbol.h"
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030026#include "util/thread.h"
Ingo Molnar148be2c2009-04-27 08:02:14 +020027#include "util/util.h"
Arnaldo Carvalho de Melo43cbcd82009-07-01 12:28:37 -030028#include <linux/rbtree.h>
Ingo Molnarb456bae2009-05-26 09:17:18 +020029#include "util/parse-options.h"
30#include "util/parse-events.h"
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110031#include "util/cpumap.h"
Ingo Molnar07800602009-04-20 15:00:56 +020032
Frederic Weisbecker8f288272009-08-16 22:05:48 +020033#include "util/debug.h"
34
Ingo Molnar07800602009-04-20 15:00:56 +020035#include <assert.h>
36#include <fcntl.h>
Ingo Molnar0e9b20b2009-05-26 09:17:18 +020037
Ingo Molnar07800602009-04-20 15:00:56 +020038#include <stdio.h>
Mike Galbraith923c42c2009-07-22 20:36:03 +020039#include <termios.h>
40#include <unistd.h>
Ingo Molnar0e9b20b2009-05-26 09:17:18 +020041
Ingo Molnar07800602009-04-20 15:00:56 +020042#include <errno.h>
Ingo Molnar07800602009-04-20 15:00:56 +020043#include <time.h>
44#include <sched.h>
45#include <pthread.h>
46
47#include <sys/syscall.h>
48#include <sys/ioctl.h>
49#include <sys/poll.h>
50#include <sys/prctl.h>
51#include <sys/wait.h>
52#include <sys/uio.h>
53#include <sys/mman.h>
54
55#include <linux/unistd.h>
56#include <linux/types.h>
57
Zhang, Yanmind6d901c2010-03-18 11:36:05 -030058static int *fd[MAX_NR_CPUS][MAX_COUNTERS];
Ingo Molnara21ca2c2009-06-06 09:58:57 +020059
Ingo Molnar42e59d72009-10-06 15:14:21 +020060static int system_wide = 0;
Ingo Molnar07800602009-04-20 15:00:56 +020061
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +020062static int default_interval = 0;
Ingo Molnar07800602009-04-20 15:00:56 +020063
Ingo Molnar42e59d72009-10-06 15:14:21 +020064static int count_filter = 5;
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -020065static int print_entries;
Ingo Molnar07800602009-04-20 15:00:56 +020066
Ingo Molnar42e59d72009-10-06 15:14:21 +020067static int target_pid = -1;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -030068static int target_tid = -1;
69static pid_t *all_tids = NULL;
70static int thread_num = 0;
Ingo Molnar42e59d72009-10-06 15:14:21 +020071static int inherit = 0;
72static int profile_cpu = -1;
73static int nr_cpus = 0;
74static unsigned int realtime_prio = 0;
75static int group = 0;
Ingo Molnar07800602009-04-20 15:00:56 +020076static unsigned int page_size;
Ingo Molnar42e59d72009-10-06 15:14:21 +020077static unsigned int mmap_pages = 16;
78static int freq = 1000; /* 1 KHz */
Ingo Molnar07800602009-04-20 15:00:56 +020079
Ingo Molnar42e59d72009-10-06 15:14:21 +020080static int delay_secs = 2;
81static int zero = 0;
82static int dump_symtab = 0;
Ingo Molnar07800602009-04-20 15:00:56 +020083
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -020084static bool hide_kernel_symbols = false;
85static bool hide_user_symbols = false;
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -020086static struct winsize winsize;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -020087
Ingo Molnar07800602009-04-20 15:00:56 +020088/*
Mike Galbraith923c42c2009-07-22 20:36:03 +020089 * Source
90 */
91
92struct source_line {
93 u64 eip;
94 unsigned long count[MAX_COUNTERS];
95 char *line;
96 struct source_line *next;
97};
98
Ingo Molnar42e59d72009-10-06 15:14:21 +020099static char *sym_filter = NULL;
100struct sym_entry *sym_filter_entry = NULL;
Kirill Smelkov6cff0e82010-02-03 16:52:08 -0200101struct sym_entry *sym_filter_entry_sched = NULL;
Ingo Molnar42e59d72009-10-06 15:14:21 +0200102static int sym_pcnt_filter = 5;
103static int sym_counter = 0;
104static int display_weighted = -1;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200105
106/*
Ingo Molnar07800602009-04-20 15:00:56 +0200107 * Symbols
108 */
109
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200110struct sym_entry_source {
111 struct source_line *source;
112 struct source_line *lines;
113 struct source_line **lines_tail;
114 pthread_mutex_t lock;
115};
116
Ingo Molnar07800602009-04-20 15:00:56 +0200117struct sym_entry {
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300118 struct rb_node rb_node;
119 struct list_head node;
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300120 unsigned long snap_count;
121 double weight;
Ingo Molnar07800602009-04-20 15:00:56 +0200122 int skip;
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200123 u16 name_len;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200124 u8 origin;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300125 struct map *map;
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200126 struct sym_entry_source *src;
Arnaldo Carvalho de Melo5a8e5a32009-11-17 18:38:01 -0200127 unsigned long count[0];
Ingo Molnar07800602009-04-20 15:00:56 +0200128};
129
Mike Galbraith923c42c2009-07-22 20:36:03 +0200130/*
131 * Source functions
132 */
133
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200134static inline struct symbol *sym_entry__symbol(struct sym_entry *self)
135{
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -0200136 return ((void *)self) + symbol_conf.priv_size;
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200137}
138
Arnaldo Carvalho de Melo895f0ed2010-03-11 20:12:41 -0300139void get_term_dimensions(struct winsize *ws)
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200140{
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200141 char *s = getenv("LINES");
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200142
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200143 if (s != NULL) {
144 ws->ws_row = atoi(s);
145 s = getenv("COLUMNS");
146 if (s != NULL) {
147 ws->ws_col = atoi(s);
148 if (ws->ws_row && ws->ws_col)
149 return;
150 }
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200151 }
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200152#ifdef TIOCGWINSZ
153 if (ioctl(1, TIOCGWINSZ, ws) == 0 &&
154 ws->ws_row && ws->ws_col)
155 return;
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200156#endif
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200157 ws->ws_row = 25;
158 ws->ws_col = 80;
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200159}
160
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200161static void update_print_entries(struct winsize *ws)
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200162{
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200163 print_entries = ws->ws_row;
164
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200165 if (print_entries > 9)
166 print_entries -= 9;
167}
168
169static void sig_winch_handler(int sig __used)
170{
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200171 get_term_dimensions(&winsize);
172 update_print_entries(&winsize);
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200173}
174
Arnaldo Carvalho de Melob0a9ab62010-03-15 11:46:58 -0300175static int parse_source(struct sym_entry *syme)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200176{
177 struct symbol *sym;
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200178 struct sym_entry_source *source;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300179 struct map *map;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200180 FILE *file;
Ingo Molnar83a09442009-08-15 12:26:57 +0200181 char command[PATH_MAX*2];
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300182 const char *path;
183 u64 len;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200184
185 if (!syme)
Arnaldo Carvalho de Melob0a9ab62010-03-15 11:46:58 -0300186 return -1;
187
188 sym = sym_entry__symbol(syme);
189 map = syme->map;
190
191 /*
192 * We can't annotate with just /proc/kallsyms
193 */
194 if (map->dso->origin == DSO__ORIG_KERNEL)
195 return -1;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200196
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200197 if (syme->src == NULL) {
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200198 syme->src = zalloc(sizeof(*source));
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200199 if (syme->src == NULL)
Arnaldo Carvalho de Melob0a9ab62010-03-15 11:46:58 -0300200 return -1;
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200201 pthread_mutex_init(&syme->src->lock, NULL);
202 }
203
204 source = syme->src;
205
206 if (source->lines) {
207 pthread_mutex_lock(&source->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200208 goto out_assign;
209 }
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300210 path = map->dso->long_name;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200211
Mike Galbraith923c42c2009-07-22 20:36:03 +0200212 len = sym->end - sym->start;
213
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300214 sprintf(command,
Arnaldo Carvalho de Melo5f485362010-02-07 11:46:16 -0200215 "objdump --start-address=%#0*Lx --stop-address=%#0*Lx -dS %s",
216 BITS_PER_LONG / 4, map__rip_2objdump(map, sym->start),
217 BITS_PER_LONG / 4, map__rip_2objdump(map, sym->end), path);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200218
219 file = popen(command, "r");
220 if (!file)
Arnaldo Carvalho de Melob0a9ab62010-03-15 11:46:58 -0300221 return -1;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200222
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200223 pthread_mutex_lock(&source->lock);
224 source->lines_tail = &source->lines;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200225 while (!feof(file)) {
226 struct source_line *src;
227 size_t dummy = 0;
Kirill Smelkovee11b902010-02-07 11:46:15 -0200228 char *c, *sep;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200229
230 src = malloc(sizeof(struct source_line));
231 assert(src != NULL);
232 memset(src, 0, sizeof(struct source_line));
233
234 if (getline(&src->line, &dummy, file) < 0)
235 break;
236 if (!src->line)
237 break;
238
239 c = strchr(src->line, '\n');
240 if (c)
241 *c = 0;
242
243 src->next = NULL;
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200244 *source->lines_tail = src;
245 source->lines_tail = &src->next;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200246
Kirill Smelkovee11b902010-02-07 11:46:15 -0200247 src->eip = strtoull(src->line, &sep, 16);
248 if (*sep == ':')
249 src->eip = map__objdump_2ip(map, src->eip);
250 else /* this line has no ip info (e.g. source line) */
251 src->eip = 0;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200252 }
253 pclose(file);
254out_assign:
255 sym_filter_entry = syme;
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200256 pthread_mutex_unlock(&source->lock);
Arnaldo Carvalho de Melob0a9ab62010-03-15 11:46:58 -0300257 return 0;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200258}
259
260static void __zero_source_counters(struct sym_entry *syme)
261{
262 int i;
263 struct source_line *line;
264
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200265 line = syme->src->lines;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200266 while (line) {
267 for (i = 0; i < nr_counters; i++)
268 line->count[i] = 0;
269 line = line->next;
270 }
271}
272
273static void record_precise_ip(struct sym_entry *syme, int counter, u64 ip)
274{
275 struct source_line *line;
276
277 if (syme != sym_filter_entry)
278 return;
279
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200280 if (pthread_mutex_trylock(&syme->src->lock))
Mike Galbraith923c42c2009-07-22 20:36:03 +0200281 return;
282
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200283 if (syme->src == NULL || syme->src->source == NULL)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200284 goto out_unlock;
285
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200286 for (line = syme->src->lines; line; line = line->next) {
Kirill Smelkovee11b902010-02-07 11:46:15 -0200287 /* skip lines without IP info */
288 if (line->eip == 0)
289 continue;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200290 if (line->eip == ip) {
291 line->count[counter]++;
292 break;
293 }
294 if (line->eip > ip)
295 break;
296 }
297out_unlock:
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200298 pthread_mutex_unlock(&syme->src->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200299}
300
Arnaldo Carvalho de Meloc7ad21a2010-02-22 16:14:22 -0300301#define PATTERN_LEN (BITS_PER_LONG / 4 + 2)
302
Mike Galbraith923c42c2009-07-22 20:36:03 +0200303static void lookup_sym_source(struct sym_entry *syme)
304{
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200305 struct symbol *symbol = sym_entry__symbol(syme);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200306 struct source_line *line;
Arnaldo Carvalho de Meloc7ad21a2010-02-22 16:14:22 -0300307 char pattern[PATTERN_LEN + 1];
Mike Galbraith923c42c2009-07-22 20:36:03 +0200308
Arnaldo Carvalho de Melo5f485362010-02-07 11:46:16 -0200309 sprintf(pattern, "%0*Lx <", BITS_PER_LONG / 4,
310 map__rip_2objdump(syme->map, symbol->start));
Mike Galbraith923c42c2009-07-22 20:36:03 +0200311
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200312 pthread_mutex_lock(&syme->src->lock);
313 for (line = syme->src->lines; line; line = line->next) {
Arnaldo Carvalho de Meloc7ad21a2010-02-22 16:14:22 -0300314 if (memcmp(line->line, pattern, PATTERN_LEN) == 0) {
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200315 syme->src->source = line;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200316 break;
317 }
318 }
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200319 pthread_mutex_unlock(&syme->src->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200320}
321
322static void show_lines(struct source_line *queue, int count, int total)
323{
324 int i;
325 struct source_line *line;
326
327 line = queue;
328 for (i = 0; i < count; i++) {
329 float pcnt = 100.0*(float)line->count[sym_counter]/(float)total;
330
331 printf("%8li %4.1f%%\t%s\n", line->count[sym_counter], pcnt, line->line);
332 line = line->next;
333 }
334}
335
336#define TRACE_COUNT 3
337
338static void show_details(struct sym_entry *syme)
339{
340 struct symbol *symbol;
341 struct source_line *line;
342 struct source_line *line_queue = NULL;
343 int displayed = 0;
344 int line_queue_count = 0, total = 0, more = 0;
345
346 if (!syme)
347 return;
348
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200349 if (!syme->src->source)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200350 lookup_sym_source(syme);
351
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200352 if (!syme->src->source)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200353 return;
354
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200355 symbol = sym_entry__symbol(syme);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200356 printf("Showing %s for %s\n", event_name(sym_counter), symbol->name);
357 printf(" Events Pcnt (>=%d%%)\n", sym_pcnt_filter);
358
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200359 pthread_mutex_lock(&syme->src->lock);
360 line = syme->src->source;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200361 while (line) {
362 total += line->count[sym_counter];
363 line = line->next;
364 }
365
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200366 line = syme->src->source;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200367 while (line) {
368 float pcnt = 0.0;
369
370 if (!line_queue_count)
371 line_queue = line;
372 line_queue_count++;
373
374 if (line->count[sym_counter])
375 pcnt = 100.0 * line->count[sym_counter] / (float)total;
376 if (pcnt >= (float)sym_pcnt_filter) {
377 if (displayed <= print_entries)
378 show_lines(line_queue, line_queue_count, total);
379 else more++;
380 displayed += line_queue_count;
381 line_queue_count = 0;
382 line_queue = NULL;
383 } else if (line_queue_count > TRACE_COUNT) {
384 line_queue = line_queue->next;
385 line_queue_count--;
386 }
387
388 line->count[sym_counter] = zero ? 0 : line->count[sym_counter] * 7 / 8;
389 line = line->next;
390 }
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200391 pthread_mutex_unlock(&syme->src->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200392 if (more)
393 printf("%d lines not displayed, maybe increase display entries [e]\n", more);
394}
Ingo Molnar07800602009-04-20 15:00:56 +0200395
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300396/*
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200397 * Symbols will be added here in event__process_sample and will get out
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300398 * after decayed.
399 */
400static LIST_HEAD(active_symbols);
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300401static pthread_mutex_t active_symbols_lock = PTHREAD_MUTEX_INITIALIZER;
Ingo Molnar07800602009-04-20 15:00:56 +0200402
Ingo Molnar07800602009-04-20 15:00:56 +0200403/*
404 * Ordering weight: count-1 * count-2 * ... / count-n
405 */
406static double sym_weight(const struct sym_entry *sym)
407{
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300408 double weight = sym->snap_count;
Ingo Molnar07800602009-04-20 15:00:56 +0200409 int counter;
410
Mike Galbraith46ab9762009-07-24 10:09:50 +0200411 if (!display_weighted)
412 return weight;
413
Ingo Molnar07800602009-04-20 15:00:56 +0200414 for (counter = 1; counter < nr_counters-1; counter++)
415 weight *= sym->count[counter];
416
417 weight /= (sym->count[counter] + 1);
418
419 return weight;
420}
421
Ingo Molnar2debbc82009-06-05 14:29:10 +0200422static long samples;
423static long userspace_samples;
Peter Zijlstra1676b8a2010-03-04 14:19:36 +0100424static long exact_samples;
Ingo Molnar07800602009-04-20 15:00:56 +0200425static const char CONSOLE_CLEAR[] = "";
426
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300427static void __list_insert_active_sym(struct sym_entry *syme)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300428{
429 list_add(&syme->node, &active_symbols);
430}
431
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300432static void list_remove_active_sym(struct sym_entry *syme)
433{
434 pthread_mutex_lock(&active_symbols_lock);
435 list_del_init(&syme->node);
436 pthread_mutex_unlock(&active_symbols_lock);
437}
438
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300439static void rb_insert_active_sym(struct rb_root *tree, struct sym_entry *se)
440{
441 struct rb_node **p = &tree->rb_node;
442 struct rb_node *parent = NULL;
443 struct sym_entry *iter;
444
445 while (*p != NULL) {
446 parent = *p;
447 iter = rb_entry(parent, struct sym_entry, rb_node);
448
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300449 if (se->weight > iter->weight)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300450 p = &(*p)->rb_left;
451 else
452 p = &(*p)->rb_right;
453 }
454
455 rb_link_node(&se->rb_node, parent, p);
456 rb_insert_color(&se->rb_node, tree);
457}
Ingo Molnar07800602009-04-20 15:00:56 +0200458
459static void print_sym_table(void)
460{
Ingo Molnar233f0b92009-06-03 21:48:40 +0200461 int printed = 0, j;
Mike Galbraith46ab9762009-07-24 10:09:50 +0200462 int counter, snap = !display_weighted ? sym_counter : 0;
Ingo Molnar2debbc82009-06-05 14:29:10 +0200463 float samples_per_sec = samples/delay_secs;
464 float ksamples_per_sec = (samples-userspace_samples)/delay_secs;
Peter Zijlstra1676b8a2010-03-04 14:19:36 +0100465 float esamples_percent = (100.0*exact_samples)/samples;
Ingo Molnar2debbc82009-06-05 14:29:10 +0200466 float sum_ksamples = 0.0;
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300467 struct sym_entry *syme, *n;
468 struct rb_root tmp = RB_ROOT;
469 struct rb_node *nd;
Arnaldo Carvalho de Melo00909e92010-03-16 18:28:46 -0300470 int sym_width = 0, dso_width = 0, dso_short_width = 0;
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200471 const int win_width = winsize.ws_col - 1;
Ingo Molnar07800602009-04-20 15:00:56 +0200472
Peter Zijlstra1676b8a2010-03-04 14:19:36 +0100473 samples = userspace_samples = exact_samples = 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200474
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300475 /* Sort the active symbols */
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300476 pthread_mutex_lock(&active_symbols_lock);
477 syme = list_entry(active_symbols.next, struct sym_entry, node);
478 pthread_mutex_unlock(&active_symbols_lock);
479
480 list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
Mike Galbraith46ab9762009-07-24 10:09:50 +0200481 syme->snap_count = syme->count[snap];
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300482 if (syme->snap_count != 0) {
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200483
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200484 if ((hide_user_symbols &&
485 syme->origin == PERF_RECORD_MISC_USER) ||
486 (hide_kernel_symbols &&
487 syme->origin == PERF_RECORD_MISC_KERNEL)) {
488 list_remove_active_sym(syme);
489 continue;
490 }
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300491 syme->weight = sym_weight(syme);
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300492 rb_insert_active_sym(&tmp, syme);
Ingo Molnar2debbc82009-06-05 14:29:10 +0200493 sum_ksamples += syme->snap_count;
Mike Galbraithd94b9432009-05-25 09:57:56 +0200494
495 for (j = 0; j < nr_counters; j++)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300496 syme->count[j] = zero ? 0 : syme->count[j] * 7 / 8;
497 } else
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300498 list_remove_active_sym(syme);
Mike Galbraithd94b9432009-05-25 09:57:56 +0200499 }
500
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200501 puts(CONSOLE_CLEAR);
Ingo Molnar07800602009-04-20 15:00:56 +0200502
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200503 printf("%-*.*s\n", win_width, win_width, graph_dotted_line);
Peter Zijlstra1676b8a2010-03-04 14:19:36 +0100504 printf( " PerfTop:%8.0f irqs/sec kernel:%4.1f%% exact: %4.1f%% [",
Ingo Molnar2debbc82009-06-05 14:29:10 +0200505 samples_per_sec,
Peter Zijlstra1676b8a2010-03-04 14:19:36 +0100506 100.0 - (100.0*((samples_per_sec-ksamples_per_sec)/samples_per_sec)),
507 esamples_percent);
Ingo Molnar07800602009-04-20 15:00:56 +0200508
Mike Galbraith46ab9762009-07-24 10:09:50 +0200509 if (nr_counters == 1 || !display_weighted) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000510 printf("%Ld", (u64)attrs[0].sample_period);
Ingo Molnarcf1f4572009-06-05 13:27:02 +0200511 if (freq)
512 printf("Hz ");
513 else
514 printf(" ");
515 }
Ingo Molnar07800602009-04-20 15:00:56 +0200516
Mike Galbraith46ab9762009-07-24 10:09:50 +0200517 if (!display_weighted)
518 printf("%s", event_name(sym_counter));
519 else for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnar07800602009-04-20 15:00:56 +0200520 if (counter)
521 printf("/");
522
523 printf("%s", event_name(counter));
524 }
525
526 printf( "], ");
527
Ingo Molnarb456bae2009-05-26 09:17:18 +0200528 if (target_pid != -1)
529 printf(" (target_pid: %d", target_pid);
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300530 else if (target_tid != -1)
531 printf(" (target_tid: %d", target_tid);
Ingo Molnar07800602009-04-20 15:00:56 +0200532 else
533 printf(" (all");
534
535 if (profile_cpu != -1)
536 printf(", cpu: %d)\n", profile_cpu);
537 else {
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300538 if (target_tid != -1)
Ingo Molnar07800602009-04-20 15:00:56 +0200539 printf(")\n");
540 else
541 printf(", %d CPUs)\n", nr_cpus);
542 }
543
Arnaldo Carvalho de Melo1a105f72009-11-17 15:40:55 -0200544 printf("%-*.*s\n", win_width, win_width, graph_dotted_line);
Ingo Molnar07800602009-04-20 15:00:56 +0200545
Mike Galbraith923c42c2009-07-22 20:36:03 +0200546 if (sym_filter_entry) {
547 show_details(sym_filter_entry);
548 return;
549 }
550
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200551 /*
552 * Find the longest symbol name that will be displayed
553 */
554 for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
555 syme = rb_entry(nd, struct sym_entry, rb_node);
556 if (++printed > print_entries ||
557 (int)syme->snap_count < count_filter)
558 continue;
559
Arnaldo Carvalho de Melo1a105f72009-11-17 15:40:55 -0200560 if (syme->map->dso->long_name_len > dso_width)
561 dso_width = syme->map->dso->long_name_len;
562
Arnaldo Carvalho de Melob63be8d2010-03-15 15:03:50 -0300563 if (syme->map->dso->short_name_len > dso_short_width)
564 dso_short_width = syme->map->dso->short_name_len;
565
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200566 if (syme->name_len > sym_width)
567 sym_width = syme->name_len;
568 }
569
570 printed = 0;
571
Arnaldo Carvalho de Melob63be8d2010-03-15 15:03:50 -0300572 if (sym_width + dso_width > winsize.ws_col - 29) {
573 dso_width = dso_short_width;
574 if (sym_width + dso_width > winsize.ws_col - 29)
575 sym_width = winsize.ws_col - dso_width - 29;
576 }
Arnaldo Carvalho de Melo7cc017e2009-11-24 12:05:14 -0200577 putchar('\n');
Ingo Molnar07800602009-04-20 15:00:56 +0200578 if (nr_counters == 1)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200579 printf(" samples pcnt");
Ingo Molnar07800602009-04-20 15:00:56 +0200580 else
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200581 printf(" weight samples pcnt");
Ingo Molnar07800602009-04-20 15:00:56 +0200582
Arnaldo Carvalho de Melo7ced1562009-08-26 11:51:26 -0300583 if (verbose)
584 printf(" RIP ");
Arnaldo Carvalho de Melo7cc017e2009-11-24 12:05:14 -0200585 printf(" %-*.*s DSO\n", sym_width, sym_width, "function");
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200586 printf(" %s _______ _____",
Arnaldo Carvalho de Melo7ced1562009-08-26 11:51:26 -0300587 nr_counters == 1 ? " " : "______");
588 if (verbose)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200589 printf(" ________________");
Arnaldo Carvalho de Melo1a105f72009-11-17 15:40:55 -0200590 printf(" %-*.*s", sym_width, sym_width, graph_line);
Arnaldo Carvalho de Melo7cc017e2009-11-24 12:05:14 -0200591 printf(" %-*.*s", dso_width, dso_width, graph_line);
Arnaldo Carvalho de Melo1a105f72009-11-17 15:40:55 -0200592 puts("\n");
Ingo Molnar07800602009-04-20 15:00:56 +0200593
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300594 for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
Ingo Molnar83a09442009-08-15 12:26:57 +0200595 struct symbol *sym;
Ingo Molnar8fc03212009-06-04 15:19:47 +0200596 double pcnt;
Ingo Molnar07800602009-04-20 15:00:56 +0200597
Ingo Molnar83a09442009-08-15 12:26:57 +0200598 syme = rb_entry(nd, struct sym_entry, rb_node);
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200599 sym = sym_entry__symbol(syme);
Ingo Molnar83a09442009-08-15 12:26:57 +0200600
Mike Galbraith923c42c2009-07-22 20:36:03 +0200601 if (++printed > print_entries || (int)syme->snap_count < count_filter)
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300602 continue;
Ingo Molnar07800602009-04-20 15:00:56 +0200603
Ingo Molnar2debbc82009-06-05 14:29:10 +0200604 pcnt = 100.0 - (100.0 * ((sum_ksamples - syme->snap_count) /
605 sum_ksamples));
Mike Galbraithd94b9432009-05-25 09:57:56 +0200606
Mike Galbraith46ab9762009-07-24 10:09:50 +0200607 if (nr_counters == 1 || !display_weighted)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200608 printf("%20.2f ", syme->weight);
Mike Galbraithd94b9432009-05-25 09:57:56 +0200609 else
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200610 printf("%9.1f %10ld ", syme->weight, syme->snap_count);
Ingo Molnar8fc03212009-06-04 15:19:47 +0200611
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200612 percent_color_fprintf(stdout, "%4.1f%%", pcnt);
Arnaldo Carvalho de Melo7ced1562009-08-26 11:51:26 -0300613 if (verbose)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200614 printf(" %016llx", sym->start);
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200615 printf(" %-*.*s", sym_width, sym_width, sym->name);
Arnaldo Carvalho de Melo7cc017e2009-11-24 12:05:14 -0200616 printf(" %-*.*s\n", dso_width, dso_width,
617 dso_width >= syme->map->dso->long_name_len ?
618 syme->map->dso->long_name :
619 syme->map->dso->short_name);
Ingo Molnar07800602009-04-20 15:00:56 +0200620 }
Ingo Molnar07800602009-04-20 15:00:56 +0200621}
622
Mike Galbraith923c42c2009-07-22 20:36:03 +0200623static void prompt_integer(int *target, const char *msg)
624{
625 char *buf = malloc(0), *p;
626 size_t dummy = 0;
627 int tmp;
628
629 fprintf(stdout, "\n%s: ", msg);
630 if (getline(&buf, &dummy, stdin) < 0)
631 return;
632
633 p = strchr(buf, '\n');
634 if (p)
635 *p = 0;
636
637 p = buf;
638 while(*p) {
639 if (!isdigit(*p))
640 goto out_free;
641 p++;
642 }
643 tmp = strtoul(buf, NULL, 10);
644 *target = tmp;
645out_free:
646 free(buf);
647}
648
649static void prompt_percent(int *target, const char *msg)
650{
651 int tmp = 0;
652
653 prompt_integer(&tmp, msg);
654 if (tmp >= 0 && tmp <= 100)
655 *target = tmp;
656}
657
658static void prompt_symbol(struct sym_entry **target, const char *msg)
659{
660 char *buf = malloc(0), *p;
661 struct sym_entry *syme = *target, *n, *found = NULL;
662 size_t dummy = 0;
663
664 /* zero counters of active symbol */
665 if (syme) {
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200666 pthread_mutex_lock(&syme->src->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200667 __zero_source_counters(syme);
668 *target = NULL;
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200669 pthread_mutex_unlock(&syme->src->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200670 }
671
672 fprintf(stdout, "\n%s: ", msg);
673 if (getline(&buf, &dummy, stdin) < 0)
674 goto out_free;
675
676 p = strchr(buf, '\n');
677 if (p)
678 *p = 0;
679
680 pthread_mutex_lock(&active_symbols_lock);
681 syme = list_entry(active_symbols.next, struct sym_entry, node);
682 pthread_mutex_unlock(&active_symbols_lock);
683
684 list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200685 struct symbol *sym = sym_entry__symbol(syme);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200686
687 if (!strcmp(buf, sym->name)) {
688 found = syme;
689 break;
690 }
691 }
692
693 if (!found) {
Kirill Smelkov66aeb6d2010-01-13 13:22:19 -0200694 fprintf(stderr, "Sorry, %s is not active.\n", buf);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200695 sleep(1);
696 return;
697 } else
698 parse_source(found);
699
700out_free:
701 free(buf);
702}
703
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200704static void print_mapped_keys(void)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200705{
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200706 char *name = NULL;
707
708 if (sym_filter_entry) {
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200709 struct symbol *sym = sym_entry__symbol(sym_filter_entry);
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200710 name = sym->name;
711 }
712
713 fprintf(stdout, "\nMapped keys:\n");
714 fprintf(stdout, "\t[d] display refresh delay. \t(%d)\n", delay_secs);
715 fprintf(stdout, "\t[e] display entries (lines). \t(%d)\n", print_entries);
716
717 if (nr_counters > 1)
718 fprintf(stdout, "\t[E] active event counter. \t(%s)\n", event_name(sym_counter));
719
720 fprintf(stdout, "\t[f] profile display filter (count). \t(%d)\n", count_filter);
721
Kirill Smelkov6cff0e82010-02-03 16:52:08 -0200722 fprintf(stdout, "\t[F] annotate display filter (percent). \t(%d%%)\n", sym_pcnt_filter);
723 fprintf(stdout, "\t[s] annotate symbol. \t(%s)\n", name?: "NULL");
724 fprintf(stdout, "\t[S] stop annotation.\n");
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200725
726 if (nr_counters > 1)
727 fprintf(stdout, "\t[w] toggle display weighted/count[E]r. \t(%d)\n", display_weighted ? 1 : 0);
728
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200729 fprintf(stdout,
Kirill Smelkov1a72cfa2010-02-12 19:20:59 +0300730 "\t[K] hide kernel_symbols symbols. \t(%s)\n",
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200731 hide_kernel_symbols ? "yes" : "no");
732 fprintf(stdout,
733 "\t[U] hide user symbols. \t(%s)\n",
734 hide_user_symbols ? "yes" : "no");
Mike Galbraith46ab9762009-07-24 10:09:50 +0200735 fprintf(stdout, "\t[z] toggle sample zeroing. \t(%d)\n", zero ? 1 : 0);
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200736 fprintf(stdout, "\t[qQ] quit.\n");
737}
738
739static int key_mapped(int c)
740{
741 switch (c) {
742 case 'd':
743 case 'e':
744 case 'f':
745 case 'z':
746 case 'q':
747 case 'Q':
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200748 case 'K':
749 case 'U':
Kirill Smelkov6cff0e82010-02-03 16:52:08 -0200750 case 'F':
751 case 's':
752 case 'S':
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200753 return 1;
754 case 'E':
755 case 'w':
756 return nr_counters > 1 ? 1 : 0;
Ingo Molnar83a09442009-08-15 12:26:57 +0200757 default:
758 break;
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200759 }
760
761 return 0;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200762}
763
764static void handle_keypress(int c)
765{
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200766 if (!key_mapped(c)) {
767 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
768 struct termios tc, save;
769
770 print_mapped_keys();
771 fprintf(stdout, "\nEnter selection, or unmapped key to continue: ");
772 fflush(stdout);
773
774 tcgetattr(0, &save);
775 tc = save;
776 tc.c_lflag &= ~(ICANON | ECHO);
777 tc.c_cc[VMIN] = 0;
778 tc.c_cc[VTIME] = 0;
779 tcsetattr(0, TCSANOW, &tc);
780
781 poll(&stdin_poll, 1, -1);
782 c = getc(stdin);
783
784 tcsetattr(0, TCSAFLUSH, &save);
785 if (!key_mapped(c))
786 return;
787 }
788
Mike Galbraith923c42c2009-07-22 20:36:03 +0200789 switch (c) {
790 case 'd':
791 prompt_integer(&delay_secs, "Enter display delay");
Tim Blechmanndc799592009-10-17 18:08:29 +0200792 if (delay_secs < 1)
793 delay_secs = 1;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200794 break;
795 case 'e':
796 prompt_integer(&print_entries, "Enter display entries (lines)");
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200797 if (print_entries == 0) {
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200798 sig_winch_handler(SIGWINCH);
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200799 signal(SIGWINCH, sig_winch_handler);
800 } else
801 signal(SIGWINCH, SIG_DFL);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200802 break;
803 case 'E':
804 if (nr_counters > 1) {
805 int i;
806
807 fprintf(stderr, "\nAvailable events:");
808 for (i = 0; i < nr_counters; i++)
809 fprintf(stderr, "\n\t%d %s", i, event_name(i));
810
811 prompt_integer(&sym_counter, "Enter details event counter");
812
813 if (sym_counter >= nr_counters) {
814 fprintf(stderr, "Sorry, no such event, using %s.\n", event_name(0));
815 sym_counter = 0;
816 sleep(1);
817 }
818 } else sym_counter = 0;
819 break;
820 case 'f':
821 prompt_integer(&count_filter, "Enter display event count filter");
822 break;
823 case 'F':
824 prompt_percent(&sym_pcnt_filter, "Enter details display event filter (percent)");
825 break;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200826 case 'K':
827 hide_kernel_symbols = !hide_kernel_symbols;
828 break;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200829 case 'q':
830 case 'Q':
831 printf("exiting.\n");
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200832 if (dump_symtab)
833 dsos__fprintf(stderr);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200834 exit(0);
835 case 's':
836 prompt_symbol(&sym_filter_entry, "Enter details symbol");
837 break;
838 case 'S':
839 if (!sym_filter_entry)
840 break;
841 else {
842 struct sym_entry *syme = sym_filter_entry;
843
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200844 pthread_mutex_lock(&syme->src->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200845 sym_filter_entry = NULL;
846 __zero_source_counters(syme);
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200847 pthread_mutex_unlock(&syme->src->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200848 }
849 break;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200850 case 'U':
851 hide_user_symbols = !hide_user_symbols;
852 break;
Mike Galbraith46ab9762009-07-24 10:09:50 +0200853 case 'w':
854 display_weighted = ~display_weighted;
855 break;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200856 case 'z':
857 zero = ~zero;
858 break;
Ingo Molnar83a09442009-08-15 12:26:57 +0200859 default:
860 break;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200861 }
862}
863
Ingo Molnarf37a2912009-07-01 12:37:06 +0200864static void *display_thread(void *arg __used)
Ingo Molnar07800602009-04-20 15:00:56 +0200865{
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200866 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
Mike Galbraith923c42c2009-07-22 20:36:03 +0200867 struct termios tc, save;
868 int delay_msecs, c;
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200869
Mike Galbraith923c42c2009-07-22 20:36:03 +0200870 tcgetattr(0, &save);
871 tc = save;
872 tc.c_lflag &= ~(ICANON | ECHO);
873 tc.c_cc[VMIN] = 0;
874 tc.c_cc[VTIME] = 0;
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200875
Mike Galbraith923c42c2009-07-22 20:36:03 +0200876repeat:
877 delay_msecs = delay_secs * 1000;
878 tcsetattr(0, TCSANOW, &tc);
879 /* trash return*/
880 getc(stdin);
Ingo Molnar07800602009-04-20 15:00:56 +0200881
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200882 do {
Ingo Molnar07800602009-04-20 15:00:56 +0200883 print_sym_table();
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200884 } while (!poll(&stdin_poll, 1, delay_msecs) == 1);
885
Mike Galbraith923c42c2009-07-22 20:36:03 +0200886 c = getc(stdin);
887 tcsetattr(0, TCSAFLUSH, &save);
888
889 handle_keypress(c);
890 goto repeat;
Ingo Molnar07800602009-04-20 15:00:56 +0200891
892 return NULL;
893}
894
Anton Blanchard2ab52082009-07-01 09:00:46 +1000895/* Tag samples to be skipped. */
Ingo Molnarf37a2912009-07-01 12:37:06 +0200896static const char *skip_symbols[] = {
Anton Blanchard2ab52082009-07-01 09:00:46 +1000897 "default_idle",
898 "cpu_idle",
899 "enter_idle",
900 "exit_idle",
901 "mwait_idle",
Arnaldo Carvalho de Melo59b90052009-07-26 19:06:19 -0300902 "mwait_idle_with_hints",
Arnaldo Carvalho de Melo83572752009-09-25 15:02:39 -0700903 "poll_idle",
Anton Blanchard3a3393e2009-07-01 09:00:47 +1000904 "ppc64_runlatch_off",
905 "pseries_dedicated_idle_sleep",
Anton Blanchard2ab52082009-07-01 09:00:46 +1000906 NULL
907};
908
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300909static int symbol_filter(struct map *map, struct symbol *sym)
Ingo Molnar07800602009-04-20 15:00:56 +0200910{
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300911 struct sym_entry *syme;
912 const char *name = sym->name;
Anton Blanchard2ab52082009-07-01 09:00:46 +1000913 int i;
Ingo Molnar07800602009-04-20 15:00:56 +0200914
Anton Blanchard3a3393e2009-07-01 09:00:47 +1000915 /*
916 * ppc64 uses function descriptors and appends a '.' to the
917 * start of every instruction address. Remove it.
918 */
919 if (name[0] == '.')
920 name++;
921
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300922 if (!strcmp(name, "_text") ||
923 !strcmp(name, "_etext") ||
924 !strcmp(name, "_sinittext") ||
925 !strncmp("init_module", name, 11) ||
926 !strncmp("cleanup_module", name, 14) ||
927 strstr(name, "_text_start") ||
928 strstr(name, "_text_end"))
Ingo Molnar07800602009-04-20 15:00:56 +0200929 return 1;
930
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200931 syme = symbol__priv(sym);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300932 syme->map = map;
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200933 syme->src = NULL;
Kirill Smelkov6cff0e82010-02-03 16:52:08 -0200934
935 if (!sym_filter_entry && sym_filter && !strcmp(name, sym_filter)) {
936 /* schedule initial sym_filter_entry setup */
937 sym_filter_entry_sched = syme;
938 sym_filter = NULL;
939 }
Mike Galbraith923c42c2009-07-22 20:36:03 +0200940
Anton Blanchard2ab52082009-07-01 09:00:46 +1000941 for (i = 0; skip_symbols[i]; i++) {
942 if (!strcmp(skip_symbols[i], name)) {
943 syme->skip = 1;
944 break;
945 }
946 }
Ingo Molnar07800602009-04-20 15:00:56 +0200947
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200948 if (!syme->skip)
949 syme->name_len = strlen(sym->name);
950
Ingo Molnar07800602009-04-20 15:00:56 +0200951 return 0;
952}
953
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200954static void event__process_sample(const event_t *self,
955 struct perf_session *session, int counter)
Ingo Molnar07800602009-04-20 15:00:56 +0200956{
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200957 u64 ip = self->ip.ip;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200958 struct sym_entry *syme;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200959 struct addr_location al;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200960 u8 origin = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Ingo Molnar07800602009-04-20 15:00:56 +0200961
Arnaldo Carvalho de Melo24bfef02010-01-21 13:04:43 -0200962 ++samples;
963
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200964 switch (origin) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200965 case PERF_RECORD_MISC_USER:
Arnaldo Carvalho de Melo24bfef02010-01-21 13:04:43 -0200966 ++userspace_samples;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200967 if (hide_user_symbols)
968 return;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200969 break;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200970 case PERF_RECORD_MISC_KERNEL:
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200971 if (hide_kernel_symbols)
972 return;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200973 break;
974 default:
Ingo Molnar07800602009-04-20 15:00:56 +0200975 return;
976 }
977
Peter Zijlstra1676b8a2010-03-04 14:19:36 +0100978 if (self->header.misc & PERF_RECORD_MISC_EXACT)
979 exact_samples++;
980
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200981 if (event__preprocess_sample(self, session, &al, symbol_filter) < 0 ||
Arnaldo Carvalho de Melo72b8fa12010-01-27 21:05:49 -0200982 al.filtered)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200983 return;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200984
Arnaldo Carvalho de Melo72b8fa12010-01-27 21:05:49 -0200985 if (al.sym == NULL) {
986 /*
987 * As we do lazy loading of symtabs we only will know if the
988 * specified vmlinux file is invalid when we actually have a
989 * hit in kernel space and then try to load it. So if we get
990 * here and there are _no_ symbols in the DSO backing the
991 * kernel map, bail out.
992 *
993 * We may never get here, for instance, if we use -K/
994 * --hide-kernel-symbols, even if the user specifies an
995 * invalid --vmlinux ;-)
996 */
997 if (al.map == session->vmlinux_maps[MAP__FUNCTION] &&
998 RB_EMPTY_ROOT(&al.map->dso->symbols[MAP__FUNCTION])) {
999 pr_err("The %s file can't be used\n",
1000 symbol_conf.vmlinux_name);
1001 exit(1);
1002 }
1003
1004 return;
1005 }
1006
Kirill Smelkov6cff0e82010-02-03 16:52:08 -02001007 /* let's see, whether we need to install initial sym_filter_entry */
1008 if (sym_filter_entry_sched) {
1009 sym_filter_entry = sym_filter_entry_sched;
1010 sym_filter_entry_sched = NULL;
Arnaldo Carvalho de Melob0a9ab62010-03-15 11:46:58 -03001011 if (parse_source(sym_filter_entry) < 0) {
1012 struct symbol *sym = sym_entry__symbol(sym_filter_entry);
1013
1014 pr_err("Can't annotate %s", sym->name);
1015 if (sym_filter_entry->map->dso->origin == DSO__ORIG_KERNEL) {
1016 pr_err(": No vmlinux file was found in the path:\n");
1017 vmlinux_path__fprintf(stderr);
1018 } else
1019 pr_err(".\n");
1020 exit(1);
1021 }
Kirill Smelkov6cff0e82010-02-03 16:52:08 -02001022 }
1023
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -02001024 syme = symbol__priv(al.sym);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001025 if (!syme->skip) {
1026 syme->count[counter]++;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -02001027 syme->origin = origin;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001028 record_precise_ip(syme, counter, ip);
1029 pthread_mutex_lock(&active_symbols_lock);
1030 if (list_empty(&syme->node) || !syme->node.next)
1031 __list_insert_active_sym(syme);
1032 pthread_mutex_unlock(&active_symbols_lock);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001033 }
1034}
1035
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001036static int event__process(event_t *event, struct perf_session *session)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001037{
1038 switch (event->header.type) {
1039 case PERF_RECORD_COMM:
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001040 event__process_comm(event, session);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001041 break;
1042 case PERF_RECORD_MMAP:
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001043 event__process_mmap(event, session);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001044 break;
Arnaldo Carvalho de Melo0f35cd42010-01-21 13:04:44 -02001045 case PERF_RECORD_FORK:
1046 case PERF_RECORD_EXIT:
1047 event__process_task(event, session);
1048 break;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001049 default:
1050 break;
1051 }
1052
1053 return 0;
Ingo Molnar07800602009-04-20 15:00:56 +02001054}
1055
Ingo Molnar07800602009-04-20 15:00:56 +02001056struct mmap_data {
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001057 int counter;
1058 void *base;
Ingo Molnarf37a2912009-07-01 12:37:06 +02001059 int mask;
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001060 unsigned int prev;
Ingo Molnar07800602009-04-20 15:00:56 +02001061};
1062
1063static unsigned int mmap_read_head(struct mmap_data *md)
1064{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001065 struct perf_event_mmap_page *pc = md->base;
Ingo Molnar07800602009-04-20 15:00:56 +02001066 int head;
1067
1068 head = pc->data_head;
1069 rmb();
1070
1071 return head;
1072}
1073
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001074static void perf_session__mmap_read_counter(struct perf_session *self,
1075 struct mmap_data *md)
Ingo Molnar07800602009-04-20 15:00:56 +02001076{
1077 unsigned int head = mmap_read_head(md);
1078 unsigned int old = md->prev;
1079 unsigned char *data = md->base + page_size;
1080 int diff;
1081
Ingo Molnar07800602009-04-20 15:00:56 +02001082 /*
1083 * If we're further behind than half the buffer, there's a chance
Ingo Molnar2debbc82009-06-05 14:29:10 +02001084 * the writer will bite our tail and mess up the samples under us.
Ingo Molnar07800602009-04-20 15:00:56 +02001085 *
1086 * If we somehow ended up ahead of the head, we got messed up.
1087 *
1088 * In either case, truncate and restart at head.
1089 */
1090 diff = head - old;
1091 if (diff > md->mask / 2 || diff < 0) {
Mike Galbraithf4f0b412009-10-13 14:57:20 +02001092 fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
Ingo Molnar07800602009-04-20 15:00:56 +02001093
1094 /*
1095 * head points to a known good entry, start there.
1096 */
1097 old = head;
1098 }
1099
Ingo Molnar07800602009-04-20 15:00:56 +02001100 for (; old != head;) {
Ingo Molnar07800602009-04-20 15:00:56 +02001101 event_t *event = (event_t *)&data[old & md->mask];
1102
1103 event_t event_copy;
1104
Ingo Molnar6f06ccb2009-04-20 15:22:22 +02001105 size_t size = event->header.size;
Ingo Molnar07800602009-04-20 15:00:56 +02001106
1107 /*
1108 * Event straddles the mmap boundary -- header should always
1109 * be inside due to u64 alignment of output.
1110 */
1111 if ((old & md->mask) + size != ((old + size) & md->mask)) {
1112 unsigned int offset = old;
1113 unsigned int len = min(sizeof(*event), size), cpy;
1114 void *dst = &event_copy;
1115
1116 do {
1117 cpy = min(md->mask + 1 - (offset & md->mask), len);
1118 memcpy(dst, &data[offset & md->mask], cpy);
1119 offset += cpy;
1120 dst += cpy;
1121 len -= cpy;
1122 } while (len);
1123
1124 event = &event_copy;
1125 }
1126
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001127 if (event->header.type == PERF_RECORD_SAMPLE)
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001128 event__process_sample(event, self, md->counter);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001129 else
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001130 event__process(event, self);
Ingo Molnar07800602009-04-20 15:00:56 +02001131 old += size;
Ingo Molnar07800602009-04-20 15:00:56 +02001132 }
1133
1134 md->prev = old;
1135}
1136
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001137static struct pollfd *event_array;
1138static struct mmap_data *mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
Mike Galbraithc2990a22009-05-24 08:35:49 +02001139
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001140static void perf_session__mmap_read(struct perf_session *self)
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001141{
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001142 int i, counter, thread_index;
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001143
1144 for (i = 0; i < nr_cpus; i++) {
1145 for (counter = 0; counter < nr_counters; counter++)
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001146 for (thread_index = 0;
1147 thread_index < thread_num;
1148 thread_index++) {
1149 perf_session__mmap_read_counter(self,
1150 &mmap_array[i][counter][thread_index]);
1151 }
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001152 }
1153}
1154
Ingo Molnar716c69f2009-06-07 17:31:52 +02001155int nr_poll;
1156int group_fd;
1157
1158static void start_counter(int i, int counter)
Ingo Molnar07800602009-04-20 15:00:56 +02001159{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001160 struct perf_event_attr *attr;
Mike Galbraith0fdc7e62009-07-21 10:30:36 +02001161 int cpu;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001162 int thread_index;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001163
1164 cpu = profile_cpu;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001165 if (target_tid == -1 && profile_cpu == -1)
Paul Mackerrasa12b51c2010-03-10 20:36:09 +11001166 cpu = cpumap[i];
Ingo Molnar716c69f2009-06-07 17:31:52 +02001167
1168 attr = attrs + counter;
1169
1170 attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +02001171
1172 if (freq) {
1173 attr->sample_type |= PERF_SAMPLE_PERIOD;
1174 attr->freq = 1;
1175 attr->sample_freq = freq;
1176 }
1177
Mike Galbraith0fdc7e62009-07-21 10:30:36 +02001178 attr->inherit = (cpu < 0) && inherit;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001179 attr->mmap = 1;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001180
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001181 for (thread_index = 0; thread_index < thread_num; thread_index++) {
Ingo Molnar716c69f2009-06-07 17:31:52 +02001182try_again:
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001183 fd[i][counter][thread_index] = sys_perf_event_open(attr,
1184 all_tids[thread_index], cpu, group_fd, 0);
Ingo Molnar716c69f2009-06-07 17:31:52 +02001185
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001186 if (fd[i][counter][thread_index] < 0) {
1187 int err = errno;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001188
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001189 if (err == EPERM || err == EACCES)
1190 die("No permission - are you root?\n");
1191 /*
1192 * If it's cycles then fall back to hrtimer
1193 * based cpu-clock-tick sw counter, which
1194 * is always available even if no PMU support:
1195 */
1196 if (attr->type == PERF_TYPE_HARDWARE
1197 && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
Ingo Molnar716c69f2009-06-07 17:31:52 +02001198
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001199 if (verbose)
1200 warning(" ... trying to fall back to cpu-clock-ticks\n");
Ingo Molnar3da297a2009-06-07 17:39:02 +02001201
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001202 attr->type = PERF_TYPE_SOFTWARE;
1203 attr->config = PERF_COUNT_SW_CPU_CLOCK;
1204 goto try_again;
1205 }
1206 printf("\n");
1207 error("perfcounter syscall returned with %d (%s)\n",
1208 fd[i][counter][thread_index], strerror(err));
1209 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
1210 exit(-1);
Ingo Molnar716c69f2009-06-07 17:31:52 +02001211 }
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001212 assert(fd[i][counter][thread_index] >= 0);
1213 fcntl(fd[i][counter][thread_index], F_SETFL, O_NONBLOCK);
1214
1215 /*
1216 * First counter acts as the group leader:
1217 */
1218 if (group && group_fd == -1)
1219 group_fd = fd[i][counter][thread_index];
1220
1221 event_array[nr_poll].fd = fd[i][counter][thread_index];
1222 event_array[nr_poll].events = POLLIN;
1223 nr_poll++;
1224
1225 mmap_array[i][counter][thread_index].counter = counter;
1226 mmap_array[i][counter][thread_index].prev = 0;
1227 mmap_array[i][counter][thread_index].mask = mmap_pages*page_size - 1;
1228 mmap_array[i][counter][thread_index].base = mmap(NULL, (mmap_pages+1)*page_size,
1229 PROT_READ, MAP_SHARED, fd[i][counter][thread_index], 0);
1230 if (mmap_array[i][counter][thread_index].base == MAP_FAILED)
1231 die("failed to mmap with %d (%s)\n", errno, strerror(errno));
Ingo Molnar716c69f2009-06-07 17:31:52 +02001232 }
Ingo Molnar716c69f2009-06-07 17:31:52 +02001233}
1234
1235static int __cmd_top(void)
1236{
1237 pthread_t thread;
1238 int i, counter;
Ingo Molnar07800602009-04-20 15:00:56 +02001239 int ret;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001240 /*
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001241 * FIXME: perf_session__new should allow passing a O_MMAP, so that all this
1242 * mmap reading, etc is encapsulated in it. Use O_WRONLY for now.
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001243 */
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02001244 struct perf_session *session = perf_session__new(NULL, O_WRONLY, false);
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001245 if (session == NULL)
1246 return -ENOMEM;
Ingo Molnar07800602009-04-20 15:00:56 +02001247
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001248 if (target_tid != -1)
1249 event__synthesize_thread(target_tid, event__process, session);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001250 else
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001251 event__synthesize_threads(event__process, session);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001252
Ingo Molnar07800602009-04-20 15:00:56 +02001253 for (i = 0; i < nr_cpus; i++) {
1254 group_fd = -1;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001255 for (counter = 0; counter < nr_counters; counter++)
1256 start_counter(i, counter);
Ingo Molnar07800602009-04-20 15:00:56 +02001257 }
1258
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001259 /* Wait for a minimal set of events before starting the snapshot */
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001260 poll(&event_array[0], nr_poll, 100);
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001261
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001262 perf_session__mmap_read(session);
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001263
Ingo Molnar07800602009-04-20 15:00:56 +02001264 if (pthread_create(&thread, NULL, display_thread, NULL)) {
1265 printf("Could not create display thread.\n");
1266 exit(-1);
1267 }
1268
1269 if (realtime_prio) {
1270 struct sched_param param;
1271
1272 param.sched_priority = realtime_prio;
1273 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
1274 printf("Could not set realtime priority.\n");
1275 exit(-1);
1276 }
1277 }
1278
1279 while (1) {
Ingo Molnar2debbc82009-06-05 14:29:10 +02001280 int hits = samples;
Ingo Molnar07800602009-04-20 15:00:56 +02001281
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001282 perf_session__mmap_read(session);
Ingo Molnar07800602009-04-20 15:00:56 +02001283
Ingo Molnar2debbc82009-06-05 14:29:10 +02001284 if (hits == samples)
Ingo Molnar07800602009-04-20 15:00:56 +02001285 ret = poll(event_array, nr_poll, 100);
1286 }
1287
1288 return 0;
1289}
Ingo Molnarb456bae2009-05-26 09:17:18 +02001290
1291static const char * const top_usage[] = {
1292 "perf top [<options>]",
1293 NULL
1294};
1295
Ingo Molnarb456bae2009-05-26 09:17:18 +02001296static const struct option options[] = {
1297 OPT_CALLBACK('e', "event", NULL, "event",
Thomas Gleixner86847b62009-06-06 12:24:17 +02001298 "event selector. use 'perf list' to list available events",
1299 parse_events),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001300 OPT_INTEGER('c', "count", &default_interval,
1301 "event period to sample"),
1302 OPT_INTEGER('p', "pid", &target_pid,
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001303 "profile events on existing process id"),
1304 OPT_INTEGER('t', "tid", &target_tid,
1305 "profile events on existing thread id"),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001306 OPT_BOOLEAN('a', "all-cpus", &system_wide,
1307 "system-wide collection from all CPUs"),
1308 OPT_INTEGER('C', "CPU", &profile_cpu,
1309 "CPU to profile on"),
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02001310 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1311 "file", "vmlinux pathname"),
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -02001312 OPT_BOOLEAN('K', "hide_kernel_symbols", &hide_kernel_symbols,
1313 "hide kernel symbols"),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001314 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
1315 "number of mmap data pages"),
1316 OPT_INTEGER('r', "realtime", &realtime_prio,
1317 "collect data with this RT SCHED_FIFO priority"),
Mike Galbraithdb20c002009-05-26 15:25:34 +02001318 OPT_INTEGER('d', "delay", &delay_secs,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001319 "number of seconds to delay between refreshes"),
1320 OPT_BOOLEAN('D', "dump-symtab", &dump_symtab,
1321 "dump the symbol table used for profiling"),
Ingo Molnar6e53cdf2009-06-04 08:53:05 +02001322 OPT_INTEGER('f', "count-filter", &count_filter,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001323 "only display functions with more events than this"),
1324 OPT_BOOLEAN('g', "group", &group,
1325 "put the counters into a counter group"),
Mike Galbraith0fdc7e62009-07-21 10:30:36 +02001326 OPT_BOOLEAN('i', "inherit", &inherit,
1327 "child tasks inherit counters"),
Mike Galbraith923c42c2009-07-22 20:36:03 +02001328 OPT_STRING('s', "sym-annotate", &sym_filter, "symbol name",
Kirill Smelkov6cff0e82010-02-03 16:52:08 -02001329 "symbol to annotate"),
Anton Blanchard1f208ea2009-07-01 09:00:44 +10001330 OPT_BOOLEAN('z', "zero", &zero,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001331 "zero history across updates"),
Ingo Molnar6e53cdf2009-06-04 08:53:05 +02001332 OPT_INTEGER('F', "freq", &freq,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001333 "profile at this frequency"),
Ingo Molnar6e53cdf2009-06-04 08:53:05 +02001334 OPT_INTEGER('E', "entries", &print_entries,
1335 "display this many functions"),
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -02001336 OPT_BOOLEAN('U', "hide_user_symbols", &hide_user_symbols,
1337 "hide user symbols"),
Ingo Molnar3da297a2009-06-07 17:39:02 +02001338 OPT_BOOLEAN('v', "verbose", &verbose,
1339 "be more verbose (show counter open errors, etc)"),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001340 OPT_END()
1341};
1342
Ingo Molnarf37a2912009-07-01 12:37:06 +02001343int cmd_top(int argc, const char **argv, const char *prefix __used)
Ingo Molnarb456bae2009-05-26 09:17:18 +02001344{
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02001345 int counter;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001346 int i,j;
Ingo Molnarb456bae2009-05-26 09:17:18 +02001347
1348 page_size = sysconf(_SC_PAGE_SIZE);
1349
Ingo Molnarb456bae2009-05-26 09:17:18 +02001350 argc = parse_options(argc, argv, options, top_usage, 0);
1351 if (argc)
1352 usage_with_options(top_usage, options);
1353
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001354 if (target_pid != -1) {
1355 target_tid = target_pid;
1356 thread_num = find_all_tid(target_pid, &all_tids);
1357 if (thread_num <= 0) {
1358 fprintf(stderr, "Can't find all threads of pid %d\n",
1359 target_pid);
1360 usage_with_options(top_usage, options);
1361 }
1362 } else {
1363 all_tids=malloc(sizeof(pid_t));
1364 if (!all_tids)
1365 return -ENOMEM;
1366
1367 all_tids[0] = target_tid;
1368 thread_num = 1;
1369 }
1370
1371 for (i = 0; i < MAX_NR_CPUS; i++) {
1372 for (j = 0; j < MAX_COUNTERS; j++) {
1373 fd[i][j] = malloc(sizeof(int)*thread_num);
1374 mmap_array[i][j] = malloc(
1375 sizeof(struct mmap_data)*thread_num);
1376 if (!fd[i][j] || !mmap_array[i][j])
1377 return -ENOMEM;
1378 }
1379 }
1380 event_array = malloc(
1381 sizeof(struct pollfd)*MAX_NR_CPUS*MAX_COUNTERS*thread_num);
1382 if (!event_array)
1383 return -ENOMEM;
1384
Ingo Molnarb456bae2009-05-26 09:17:18 +02001385 /* CPU and PID are mutually exclusive */
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001386 if (target_tid > 0 && profile_cpu != -1) {
Ingo Molnarb456bae2009-05-26 09:17:18 +02001387 printf("WARNING: PID switch overriding CPU\n");
1388 sleep(1);
1389 profile_cpu = -1;
1390 }
1391
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001392 if (!nr_counters)
Ingo Molnarb456bae2009-05-26 09:17:18 +02001393 nr_counters = 1;
Ingo Molnarb456bae2009-05-26 09:17:18 +02001394
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02001395 symbol_conf.priv_size = (sizeof(struct sym_entry) +
1396 (nr_counters + 1) * sizeof(unsigned long));
Kirill Smelkov6cff0e82010-02-03 16:52:08 -02001397
1398 symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL);
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02001399 if (symbol__init() < 0)
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02001400 return -1;
Arnaldo Carvalho de Melo5a8e5a32009-11-17 18:38:01 -02001401
Frederic Weisbecker2f335a02009-06-05 19:31:01 +02001402 if (delay_secs < 1)
1403 delay_secs = 1;
1404
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +02001405 /*
1406 * User specified count overrides default frequency.
1407 */
1408 if (default_interval)
1409 freq = 0;
1410 else if (freq) {
1411 default_interval = freq;
1412 } else {
1413 fprintf(stderr, "frequency and count are zero, aborting\n");
1414 exit(EXIT_FAILURE);
1415 }
1416
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001417 /*
1418 * Fill in the ones not specifically initialized via -c:
1419 */
Ingo Molnarb456bae2009-05-26 09:17:18 +02001420 for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001421 if (attrs[counter].sample_period)
Ingo Molnarb456bae2009-05-26 09:17:18 +02001422 continue;
1423
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001424 attrs[counter].sample_period = default_interval;
Ingo Molnarb456bae2009-05-26 09:17:18 +02001425 }
1426
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001427 if (target_tid != -1 || profile_cpu != -1)
Ingo Molnarb456bae2009-05-26 09:17:18 +02001428 nr_cpus = 1;
Paul Mackerrasa12b51c2010-03-10 20:36:09 +11001429 else
1430 nr_cpus = read_cpu_map();
Ingo Molnarb456bae2009-05-26 09:17:18 +02001431
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -02001432 get_term_dimensions(&winsize);
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -02001433 if (print_entries == 0) {
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -02001434 update_print_entries(&winsize);
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -02001435 signal(SIGWINCH, sig_winch_handler);
1436 }
1437
Ingo Molnarb456bae2009-05-26 09:17:18 +02001438 return __cmd_top();
1439}