blob: a66f4272b994b546b0c2a811b0f1f613a4100a9a [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 Weisbecker8f28827a2009-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
Ian Munsiec0555642010-04-13 18:37:33 +100060static bool system_wide = false;
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;
Ian Munsiec0555642010-04-13 18:37:33 +100071static bool inherit = false;
Ingo Molnar42e59d72009-10-06 15:14:21 +020072static int profile_cpu = -1;
73static int nr_cpus = 0;
Arnaldo Carvalho de Melo19679362010-05-17 15:39:16 -030074static int realtime_prio = 0;
Ian Munsiec0555642010-04-13 18:37:33 +100075static bool group = false;
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;
Ian Munsiec0555642010-04-13 18:37:33 +100081static bool zero = false;
82static bool dump_symtab = false;
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
Arnaldo Carvalho de Meloedb7c602010-05-17 16:22:41 -030099static const char *sym_filter = NULL;
Ingo Molnar42e59d72009-10-06 15:14:21 +0200100struct 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;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800423static long kernel_samples, us_samples;
Peter Zijlstra1676b8a2010-03-04 14:19:36 +0100424static long exact_samples;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800425static long guest_us_samples, guest_kernel_samples;
Ingo Molnar07800602009-04-20 15:00:56 +0200426static const char CONSOLE_CLEAR[] = "";
427
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300428static void __list_insert_active_sym(struct sym_entry *syme)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300429{
430 list_add(&syme->node, &active_symbols);
431}
432
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300433static void list_remove_active_sym(struct sym_entry *syme)
434{
435 pthread_mutex_lock(&active_symbols_lock);
436 list_del_init(&syme->node);
437 pthread_mutex_unlock(&active_symbols_lock);
438}
439
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300440static void rb_insert_active_sym(struct rb_root *tree, struct sym_entry *se)
441{
442 struct rb_node **p = &tree->rb_node;
443 struct rb_node *parent = NULL;
444 struct sym_entry *iter;
445
446 while (*p != NULL) {
447 parent = *p;
448 iter = rb_entry(parent, struct sym_entry, rb_node);
449
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300450 if (se->weight > iter->weight)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300451 p = &(*p)->rb_left;
452 else
453 p = &(*p)->rb_right;
454 }
455
456 rb_link_node(&se->rb_node, parent, p);
457 rb_insert_color(&se->rb_node, tree);
458}
Ingo Molnar07800602009-04-20 15:00:56 +0200459
460static void print_sym_table(void)
461{
Ingo Molnar233f0b92009-06-03 21:48:40 +0200462 int printed = 0, j;
Mike Galbraith46ab9762009-07-24 10:09:50 +0200463 int counter, snap = !display_weighted ? sym_counter : 0;
Ingo Molnar2debbc82009-06-05 14:29:10 +0200464 float samples_per_sec = samples/delay_secs;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800465 float ksamples_per_sec = kernel_samples/delay_secs;
466 float us_samples_per_sec = (us_samples)/delay_secs;
467 float guest_kernel_samples_per_sec = (guest_kernel_samples)/delay_secs;
468 float guest_us_samples_per_sec = (guest_us_samples)/delay_secs;
Peter Zijlstra1676b8a2010-03-04 14:19:36 +0100469 float esamples_percent = (100.0*exact_samples)/samples;
Ingo Molnar2debbc82009-06-05 14:29:10 +0200470 float sum_ksamples = 0.0;
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300471 struct sym_entry *syme, *n;
472 struct rb_root tmp = RB_ROOT;
473 struct rb_node *nd;
Arnaldo Carvalho de Melo00909e92010-03-16 18:28:46 -0300474 int sym_width = 0, dso_width = 0, dso_short_width = 0;
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200475 const int win_width = winsize.ws_col - 1;
Ingo Molnar07800602009-04-20 15:00:56 +0200476
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800477 samples = us_samples = kernel_samples = exact_samples = 0;
478 guest_kernel_samples = guest_us_samples = 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200479
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300480 /* Sort the active symbols */
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300481 pthread_mutex_lock(&active_symbols_lock);
482 syme = list_entry(active_symbols.next, struct sym_entry, node);
483 pthread_mutex_unlock(&active_symbols_lock);
484
485 list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
Mike Galbraith46ab9762009-07-24 10:09:50 +0200486 syme->snap_count = syme->count[snap];
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300487 if (syme->snap_count != 0) {
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200488
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200489 if ((hide_user_symbols &&
490 syme->origin == PERF_RECORD_MISC_USER) ||
491 (hide_kernel_symbols &&
492 syme->origin == PERF_RECORD_MISC_KERNEL)) {
493 list_remove_active_sym(syme);
494 continue;
495 }
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300496 syme->weight = sym_weight(syme);
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300497 rb_insert_active_sym(&tmp, syme);
Ingo Molnar2debbc82009-06-05 14:29:10 +0200498 sum_ksamples += syme->snap_count;
Mike Galbraithd94b9432009-05-25 09:57:56 +0200499
500 for (j = 0; j < nr_counters; j++)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300501 syme->count[j] = zero ? 0 : syme->count[j] * 7 / 8;
502 } else
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300503 list_remove_active_sym(syme);
Mike Galbraithd94b9432009-05-25 09:57:56 +0200504 }
505
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200506 puts(CONSOLE_CLEAR);
Ingo Molnar07800602009-04-20 15:00:56 +0200507
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200508 printf("%-*.*s\n", win_width, win_width, graph_dotted_line);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800509 if (!perf_guest) {
510 printf(" PerfTop:%8.0f irqs/sec kernel:%4.1f%%"
511 " exact: %4.1f%% [",
512 samples_per_sec,
513 100.0 - (100.0 * ((samples_per_sec - ksamples_per_sec) /
514 samples_per_sec)),
515 esamples_percent);
516 } else {
517 printf(" PerfTop:%8.0f irqs/sec kernel:%4.1f%% us:%4.1f%%"
518 " guest kernel:%4.1f%% guest us:%4.1f%%"
519 " exact: %4.1f%% [",
520 samples_per_sec,
521 100.0 - (100.0 * ((samples_per_sec-ksamples_per_sec) /
522 samples_per_sec)),
523 100.0 - (100.0 * ((samples_per_sec-us_samples_per_sec) /
524 samples_per_sec)),
525 100.0 - (100.0 * ((samples_per_sec -
526 guest_kernel_samples_per_sec) /
527 samples_per_sec)),
528 100.0 - (100.0 * ((samples_per_sec -
529 guest_us_samples_per_sec) /
530 samples_per_sec)),
531 esamples_percent);
532 }
Ingo Molnar07800602009-04-20 15:00:56 +0200533
Mike Galbraith46ab9762009-07-24 10:09:50 +0200534 if (nr_counters == 1 || !display_weighted) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000535 printf("%Ld", (u64)attrs[0].sample_period);
Ingo Molnarcf1f4572009-06-05 13:27:02 +0200536 if (freq)
537 printf("Hz ");
538 else
539 printf(" ");
540 }
Ingo Molnar07800602009-04-20 15:00:56 +0200541
Mike Galbraith46ab9762009-07-24 10:09:50 +0200542 if (!display_weighted)
543 printf("%s", event_name(sym_counter));
544 else for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnar07800602009-04-20 15:00:56 +0200545 if (counter)
546 printf("/");
547
548 printf("%s", event_name(counter));
549 }
550
551 printf( "], ");
552
Ingo Molnarb456bae2009-05-26 09:17:18 +0200553 if (target_pid != -1)
554 printf(" (target_pid: %d", target_pid);
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300555 else if (target_tid != -1)
556 printf(" (target_tid: %d", target_tid);
Ingo Molnar07800602009-04-20 15:00:56 +0200557 else
558 printf(" (all");
559
560 if (profile_cpu != -1)
561 printf(", cpu: %d)\n", profile_cpu);
562 else {
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300563 if (target_tid != -1)
Ingo Molnar07800602009-04-20 15:00:56 +0200564 printf(")\n");
565 else
566 printf(", %d CPUs)\n", nr_cpus);
567 }
568
Arnaldo Carvalho de Melo1a105f72009-11-17 15:40:55 -0200569 printf("%-*.*s\n", win_width, win_width, graph_dotted_line);
Ingo Molnar07800602009-04-20 15:00:56 +0200570
Mike Galbraith923c42c2009-07-22 20:36:03 +0200571 if (sym_filter_entry) {
572 show_details(sym_filter_entry);
573 return;
574 }
575
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200576 /*
577 * Find the longest symbol name that will be displayed
578 */
579 for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
580 syme = rb_entry(nd, struct sym_entry, rb_node);
581 if (++printed > print_entries ||
582 (int)syme->snap_count < count_filter)
583 continue;
584
Arnaldo Carvalho de Melo1a105f72009-11-17 15:40:55 -0200585 if (syme->map->dso->long_name_len > dso_width)
586 dso_width = syme->map->dso->long_name_len;
587
Arnaldo Carvalho de Melob63be8d2010-03-15 15:03:50 -0300588 if (syme->map->dso->short_name_len > dso_short_width)
589 dso_short_width = syme->map->dso->short_name_len;
590
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200591 if (syme->name_len > sym_width)
592 sym_width = syme->name_len;
593 }
594
595 printed = 0;
596
Arnaldo Carvalho de Melob63be8d2010-03-15 15:03:50 -0300597 if (sym_width + dso_width > winsize.ws_col - 29) {
598 dso_width = dso_short_width;
599 if (sym_width + dso_width > winsize.ws_col - 29)
600 sym_width = winsize.ws_col - dso_width - 29;
601 }
Arnaldo Carvalho de Melo7cc017e2009-11-24 12:05:14 -0200602 putchar('\n');
Ingo Molnar07800602009-04-20 15:00:56 +0200603 if (nr_counters == 1)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200604 printf(" samples pcnt");
Ingo Molnar07800602009-04-20 15:00:56 +0200605 else
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200606 printf(" weight samples pcnt");
Ingo Molnar07800602009-04-20 15:00:56 +0200607
Arnaldo Carvalho de Melo7ced1562009-08-26 11:51:26 -0300608 if (verbose)
609 printf(" RIP ");
Arnaldo Carvalho de Melo7cc017e2009-11-24 12:05:14 -0200610 printf(" %-*.*s DSO\n", sym_width, sym_width, "function");
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200611 printf(" %s _______ _____",
Arnaldo Carvalho de Melo7ced1562009-08-26 11:51:26 -0300612 nr_counters == 1 ? " " : "______");
613 if (verbose)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200614 printf(" ________________");
Arnaldo Carvalho de Melo1a105f72009-11-17 15:40:55 -0200615 printf(" %-*.*s", sym_width, sym_width, graph_line);
Arnaldo Carvalho de Melo7cc017e2009-11-24 12:05:14 -0200616 printf(" %-*.*s", dso_width, dso_width, graph_line);
Arnaldo Carvalho de Melo1a105f72009-11-17 15:40:55 -0200617 puts("\n");
Ingo Molnar07800602009-04-20 15:00:56 +0200618
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300619 for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
Ingo Molnar83a09442009-08-15 12:26:57 +0200620 struct symbol *sym;
Ingo Molnar8fc03212009-06-04 15:19:47 +0200621 double pcnt;
Ingo Molnar07800602009-04-20 15:00:56 +0200622
Ingo Molnar83a09442009-08-15 12:26:57 +0200623 syme = rb_entry(nd, struct sym_entry, rb_node);
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200624 sym = sym_entry__symbol(syme);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200625 if (++printed > print_entries || (int)syme->snap_count < count_filter)
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300626 continue;
Ingo Molnar07800602009-04-20 15:00:56 +0200627
Ingo Molnar2debbc82009-06-05 14:29:10 +0200628 pcnt = 100.0 - (100.0 * ((sum_ksamples - syme->snap_count) /
629 sum_ksamples));
Mike Galbraithd94b9432009-05-25 09:57:56 +0200630
Mike Galbraith46ab9762009-07-24 10:09:50 +0200631 if (nr_counters == 1 || !display_weighted)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200632 printf("%20.2f ", syme->weight);
Mike Galbraithd94b9432009-05-25 09:57:56 +0200633 else
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200634 printf("%9.1f %10ld ", syme->weight, syme->snap_count);
Ingo Molnar8fc03212009-06-04 15:19:47 +0200635
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200636 percent_color_fprintf(stdout, "%4.1f%%", pcnt);
Arnaldo Carvalho de Melo7ced1562009-08-26 11:51:26 -0300637 if (verbose)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200638 printf(" %016llx", sym->start);
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200639 printf(" %-*.*s", sym_width, sym_width, sym->name);
Arnaldo Carvalho de Melo7cc017e2009-11-24 12:05:14 -0200640 printf(" %-*.*s\n", dso_width, dso_width,
641 dso_width >= syme->map->dso->long_name_len ?
642 syme->map->dso->long_name :
643 syme->map->dso->short_name);
Ingo Molnar07800602009-04-20 15:00:56 +0200644 }
Ingo Molnar07800602009-04-20 15:00:56 +0200645}
646
Mike Galbraith923c42c2009-07-22 20:36:03 +0200647static void prompt_integer(int *target, const char *msg)
648{
649 char *buf = malloc(0), *p;
650 size_t dummy = 0;
651 int tmp;
652
653 fprintf(stdout, "\n%s: ", msg);
654 if (getline(&buf, &dummy, stdin) < 0)
655 return;
656
657 p = strchr(buf, '\n');
658 if (p)
659 *p = 0;
660
661 p = buf;
662 while(*p) {
663 if (!isdigit(*p))
664 goto out_free;
665 p++;
666 }
667 tmp = strtoul(buf, NULL, 10);
668 *target = tmp;
669out_free:
670 free(buf);
671}
672
673static void prompt_percent(int *target, const char *msg)
674{
675 int tmp = 0;
676
677 prompt_integer(&tmp, msg);
678 if (tmp >= 0 && tmp <= 100)
679 *target = tmp;
680}
681
682static void prompt_symbol(struct sym_entry **target, const char *msg)
683{
684 char *buf = malloc(0), *p;
685 struct sym_entry *syme = *target, *n, *found = NULL;
686 size_t dummy = 0;
687
688 /* zero counters of active symbol */
689 if (syme) {
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200690 pthread_mutex_lock(&syme->src->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200691 __zero_source_counters(syme);
692 *target = NULL;
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200693 pthread_mutex_unlock(&syme->src->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200694 }
695
696 fprintf(stdout, "\n%s: ", msg);
697 if (getline(&buf, &dummy, stdin) < 0)
698 goto out_free;
699
700 p = strchr(buf, '\n');
701 if (p)
702 *p = 0;
703
704 pthread_mutex_lock(&active_symbols_lock);
705 syme = list_entry(active_symbols.next, struct sym_entry, node);
706 pthread_mutex_unlock(&active_symbols_lock);
707
708 list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200709 struct symbol *sym = sym_entry__symbol(syme);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200710
711 if (!strcmp(buf, sym->name)) {
712 found = syme;
713 break;
714 }
715 }
716
717 if (!found) {
Kirill Smelkov66aeb6d2010-01-13 13:22:19 -0200718 fprintf(stderr, "Sorry, %s is not active.\n", buf);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200719 sleep(1);
720 return;
721 } else
722 parse_source(found);
723
724out_free:
725 free(buf);
726}
727
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200728static void print_mapped_keys(void)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200729{
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200730 char *name = NULL;
731
732 if (sym_filter_entry) {
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200733 struct symbol *sym = sym_entry__symbol(sym_filter_entry);
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200734 name = sym->name;
735 }
736
737 fprintf(stdout, "\nMapped keys:\n");
738 fprintf(stdout, "\t[d] display refresh delay. \t(%d)\n", delay_secs);
739 fprintf(stdout, "\t[e] display entries (lines). \t(%d)\n", print_entries);
740
741 if (nr_counters > 1)
742 fprintf(stdout, "\t[E] active event counter. \t(%s)\n", event_name(sym_counter));
743
744 fprintf(stdout, "\t[f] profile display filter (count). \t(%d)\n", count_filter);
745
Kirill Smelkov6cff0e82010-02-03 16:52:08 -0200746 fprintf(stdout, "\t[F] annotate display filter (percent). \t(%d%%)\n", sym_pcnt_filter);
747 fprintf(stdout, "\t[s] annotate symbol. \t(%s)\n", name?: "NULL");
748 fprintf(stdout, "\t[S] stop annotation.\n");
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200749
750 if (nr_counters > 1)
751 fprintf(stdout, "\t[w] toggle display weighted/count[E]r. \t(%d)\n", display_weighted ? 1 : 0);
752
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200753 fprintf(stdout,
Kirill Smelkov1a72cfa2010-02-12 19:20:59 +0300754 "\t[K] hide kernel_symbols symbols. \t(%s)\n",
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200755 hide_kernel_symbols ? "yes" : "no");
756 fprintf(stdout,
757 "\t[U] hide user symbols. \t(%s)\n",
758 hide_user_symbols ? "yes" : "no");
Mike Galbraith46ab9762009-07-24 10:09:50 +0200759 fprintf(stdout, "\t[z] toggle sample zeroing. \t(%d)\n", zero ? 1 : 0);
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200760 fprintf(stdout, "\t[qQ] quit.\n");
761}
762
763static int key_mapped(int c)
764{
765 switch (c) {
766 case 'd':
767 case 'e':
768 case 'f':
769 case 'z':
770 case 'q':
771 case 'Q':
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200772 case 'K':
773 case 'U':
Kirill Smelkov6cff0e82010-02-03 16:52:08 -0200774 case 'F':
775 case 's':
776 case 'S':
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200777 return 1;
778 case 'E':
779 case 'w':
780 return nr_counters > 1 ? 1 : 0;
Ingo Molnar83a09442009-08-15 12:26:57 +0200781 default:
782 break;
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200783 }
784
785 return 0;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200786}
787
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800788static void handle_keypress(struct perf_session *session, int c)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200789{
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200790 if (!key_mapped(c)) {
791 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
792 struct termios tc, save;
793
794 print_mapped_keys();
795 fprintf(stdout, "\nEnter selection, or unmapped key to continue: ");
796 fflush(stdout);
797
798 tcgetattr(0, &save);
799 tc = save;
800 tc.c_lflag &= ~(ICANON | ECHO);
801 tc.c_cc[VMIN] = 0;
802 tc.c_cc[VTIME] = 0;
803 tcsetattr(0, TCSANOW, &tc);
804
805 poll(&stdin_poll, 1, -1);
806 c = getc(stdin);
807
808 tcsetattr(0, TCSAFLUSH, &save);
809 if (!key_mapped(c))
810 return;
811 }
812
Mike Galbraith923c42c2009-07-22 20:36:03 +0200813 switch (c) {
814 case 'd':
815 prompt_integer(&delay_secs, "Enter display delay");
Tim Blechmanndc799592009-10-17 18:08:29 +0200816 if (delay_secs < 1)
817 delay_secs = 1;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200818 break;
819 case 'e':
820 prompt_integer(&print_entries, "Enter display entries (lines)");
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200821 if (print_entries == 0) {
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200822 sig_winch_handler(SIGWINCH);
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200823 signal(SIGWINCH, sig_winch_handler);
824 } else
825 signal(SIGWINCH, SIG_DFL);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200826 break;
827 case 'E':
828 if (nr_counters > 1) {
829 int i;
830
831 fprintf(stderr, "\nAvailable events:");
832 for (i = 0; i < nr_counters; i++)
833 fprintf(stderr, "\n\t%d %s", i, event_name(i));
834
835 prompt_integer(&sym_counter, "Enter details event counter");
836
837 if (sym_counter >= nr_counters) {
838 fprintf(stderr, "Sorry, no such event, using %s.\n", event_name(0));
839 sym_counter = 0;
840 sleep(1);
841 }
842 } else sym_counter = 0;
843 break;
844 case 'f':
845 prompt_integer(&count_filter, "Enter display event count filter");
846 break;
847 case 'F':
848 prompt_percent(&sym_pcnt_filter, "Enter details display event filter (percent)");
849 break;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200850 case 'K':
851 hide_kernel_symbols = !hide_kernel_symbols;
852 break;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200853 case 'q':
854 case 'Q':
855 printf("exiting.\n");
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200856 if (dump_symtab)
Arnaldo Carvalho de Melocbf69682010-04-27 21:22:44 -0300857 perf_session__fprintf_dsos(session, stderr);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200858 exit(0);
859 case 's':
860 prompt_symbol(&sym_filter_entry, "Enter details symbol");
861 break;
862 case 'S':
863 if (!sym_filter_entry)
864 break;
865 else {
866 struct sym_entry *syme = sym_filter_entry;
867
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200868 pthread_mutex_lock(&syme->src->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200869 sym_filter_entry = NULL;
870 __zero_source_counters(syme);
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200871 pthread_mutex_unlock(&syme->src->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200872 }
873 break;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200874 case 'U':
875 hide_user_symbols = !hide_user_symbols;
876 break;
Mike Galbraith46ab9762009-07-24 10:09:50 +0200877 case 'w':
878 display_weighted = ~display_weighted;
879 break;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200880 case 'z':
Ian Munsiec0555642010-04-13 18:37:33 +1000881 zero = !zero;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200882 break;
Ingo Molnar83a09442009-08-15 12:26:57 +0200883 default:
884 break;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200885 }
886}
887
Ingo Molnarf37a2912009-07-01 12:37:06 +0200888static void *display_thread(void *arg __used)
Ingo Molnar07800602009-04-20 15:00:56 +0200889{
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200890 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
Mike Galbraith923c42c2009-07-22 20:36:03 +0200891 struct termios tc, save;
892 int delay_msecs, c;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800893 struct perf_session *session = (struct perf_session *) arg;
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200894
Mike Galbraith923c42c2009-07-22 20:36:03 +0200895 tcgetattr(0, &save);
896 tc = save;
897 tc.c_lflag &= ~(ICANON | ECHO);
898 tc.c_cc[VMIN] = 0;
899 tc.c_cc[VTIME] = 0;
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200900
Mike Galbraith923c42c2009-07-22 20:36:03 +0200901repeat:
902 delay_msecs = delay_secs * 1000;
903 tcsetattr(0, TCSANOW, &tc);
904 /* trash return*/
905 getc(stdin);
Ingo Molnar07800602009-04-20 15:00:56 +0200906
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200907 do {
Ingo Molnar07800602009-04-20 15:00:56 +0200908 print_sym_table();
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200909 } while (!poll(&stdin_poll, 1, delay_msecs) == 1);
910
Mike Galbraith923c42c2009-07-22 20:36:03 +0200911 c = getc(stdin);
912 tcsetattr(0, TCSAFLUSH, &save);
913
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800914 handle_keypress(session, c);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200915 goto repeat;
Ingo Molnar07800602009-04-20 15:00:56 +0200916
917 return NULL;
918}
919
Anton Blanchard2ab52082009-07-01 09:00:46 +1000920/* Tag samples to be skipped. */
Ingo Molnarf37a2912009-07-01 12:37:06 +0200921static const char *skip_symbols[] = {
Anton Blanchard2ab52082009-07-01 09:00:46 +1000922 "default_idle",
923 "cpu_idle",
924 "enter_idle",
925 "exit_idle",
926 "mwait_idle",
Arnaldo Carvalho de Melo59b90052009-07-26 19:06:19 -0300927 "mwait_idle_with_hints",
Arnaldo Carvalho de Melo83572752009-09-25 15:02:39 -0700928 "poll_idle",
Anton Blanchard3a3393e2009-07-01 09:00:47 +1000929 "ppc64_runlatch_off",
930 "pseries_dedicated_idle_sleep",
Anton Blanchard2ab52082009-07-01 09:00:46 +1000931 NULL
932};
933
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300934static int symbol_filter(struct map *map, struct symbol *sym)
Ingo Molnar07800602009-04-20 15:00:56 +0200935{
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300936 struct sym_entry *syme;
937 const char *name = sym->name;
Anton Blanchard2ab52082009-07-01 09:00:46 +1000938 int i;
Ingo Molnar07800602009-04-20 15:00:56 +0200939
Anton Blanchard3a3393e2009-07-01 09:00:47 +1000940 /*
941 * ppc64 uses function descriptors and appends a '.' to the
942 * start of every instruction address. Remove it.
943 */
944 if (name[0] == '.')
945 name++;
946
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300947 if (!strcmp(name, "_text") ||
948 !strcmp(name, "_etext") ||
949 !strcmp(name, "_sinittext") ||
950 !strncmp("init_module", name, 11) ||
951 !strncmp("cleanup_module", name, 14) ||
952 strstr(name, "_text_start") ||
953 strstr(name, "_text_end"))
Ingo Molnar07800602009-04-20 15:00:56 +0200954 return 1;
955
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200956 syme = symbol__priv(sym);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300957 syme->map = map;
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200958 syme->src = NULL;
Kirill Smelkov6cff0e82010-02-03 16:52:08 -0200959
960 if (!sym_filter_entry && sym_filter && !strcmp(name, sym_filter)) {
961 /* schedule initial sym_filter_entry setup */
962 sym_filter_entry_sched = syme;
963 sym_filter = NULL;
964 }
Mike Galbraith923c42c2009-07-22 20:36:03 +0200965
Anton Blanchard2ab52082009-07-01 09:00:46 +1000966 for (i = 0; skip_symbols[i]; i++) {
967 if (!strcmp(skip_symbols[i], name)) {
968 syme->skip = 1;
969 break;
970 }
971 }
Ingo Molnar07800602009-04-20 15:00:56 +0200972
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200973 if (!syme->skip)
974 syme->name_len = strlen(sym->name);
975
Ingo Molnar07800602009-04-20 15:00:56 +0200976 return 0;
977}
978
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200979static void event__process_sample(const event_t *self,
980 struct perf_session *session, int counter)
Ingo Molnar07800602009-04-20 15:00:56 +0200981{
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200982 u64 ip = self->ip.ip;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200983 struct sym_entry *syme;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200984 struct addr_location al;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300985 struct machine *machine;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200986 u8 origin = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Ingo Molnar07800602009-04-20 15:00:56 +0200987
Arnaldo Carvalho de Melo24bfef02010-01-21 13:04:43 -0200988 ++samples;
989
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200990 switch (origin) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200991 case PERF_RECORD_MISC_USER:
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800992 ++us_samples;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200993 if (hide_user_symbols)
994 return;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300995 machine = perf_session__find_host_machine(session);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200996 break;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200997 case PERF_RECORD_MISC_KERNEL:
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800998 ++kernel_samples;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200999 if (hide_kernel_symbols)
1000 return;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001001 machine = perf_session__find_host_machine(session);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001002 break;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001003 case PERF_RECORD_MISC_GUEST_KERNEL:
1004 ++guest_kernel_samples;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001005 machine = perf_session__find_machine(session, self->ip.pid);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001006 break;
1007 case PERF_RECORD_MISC_GUEST_USER:
1008 ++guest_us_samples;
1009 /*
1010 * TODO: we don't process guest user from host side
1011 * except simple counting.
1012 */
1013 return;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001014 default:
Ingo Molnar07800602009-04-20 15:00:56 +02001015 return;
1016 }
1017
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001018 if (!machine && perf_guest) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001019 pr_err("Can't find guest [%d]'s kernel information\n",
1020 self->ip.pid);
1021 return;
1022 }
1023
Peter Zijlstraab608342010-04-08 23:03:20 +02001024 if (self->header.misc & PERF_RECORD_MISC_EXACT_IP)
Peter Zijlstra1676b8a2010-03-04 14:19:36 +01001025 exact_samples++;
1026
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001027 if (event__preprocess_sample(self, session, &al, symbol_filter) < 0 ||
Arnaldo Carvalho de Melo72b8fa12010-01-27 21:05:49 -02001028 al.filtered)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -02001029 return;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001030
Arnaldo Carvalho de Melo72b8fa12010-01-27 21:05:49 -02001031 if (al.sym == NULL) {
1032 /*
1033 * As we do lazy loading of symtabs we only will know if the
1034 * specified vmlinux file is invalid when we actually have a
1035 * hit in kernel space and then try to load it. So if we get
1036 * here and there are _no_ symbols in the DSO backing the
1037 * kernel map, bail out.
1038 *
1039 * We may never get here, for instance, if we use -K/
1040 * --hide-kernel-symbols, even if the user specifies an
1041 * invalid --vmlinux ;-)
1042 */
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001043 if (al.map == machine->vmlinux_maps[MAP__FUNCTION] &&
Arnaldo Carvalho de Melo72b8fa12010-01-27 21:05:49 -02001044 RB_EMPTY_ROOT(&al.map->dso->symbols[MAP__FUNCTION])) {
1045 pr_err("The %s file can't be used\n",
1046 symbol_conf.vmlinux_name);
1047 exit(1);
1048 }
1049
1050 return;
1051 }
1052
Kirill Smelkov6cff0e82010-02-03 16:52:08 -02001053 /* let's see, whether we need to install initial sym_filter_entry */
1054 if (sym_filter_entry_sched) {
1055 sym_filter_entry = sym_filter_entry_sched;
1056 sym_filter_entry_sched = NULL;
Arnaldo Carvalho de Melob0a9ab62010-03-15 11:46:58 -03001057 if (parse_source(sym_filter_entry) < 0) {
1058 struct symbol *sym = sym_entry__symbol(sym_filter_entry);
1059
1060 pr_err("Can't annotate %s", sym->name);
1061 if (sym_filter_entry->map->dso->origin == DSO__ORIG_KERNEL) {
1062 pr_err(": No vmlinux file was found in the path:\n");
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03001063 machine__fprintf_vmlinux_path(machine, stderr);
Arnaldo Carvalho de Melob0a9ab62010-03-15 11:46:58 -03001064 } else
1065 pr_err(".\n");
1066 exit(1);
1067 }
Kirill Smelkov6cff0e82010-02-03 16:52:08 -02001068 }
1069
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -02001070 syme = symbol__priv(al.sym);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001071 if (!syme->skip) {
1072 syme->count[counter]++;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -02001073 syme->origin = origin;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001074 record_precise_ip(syme, counter, ip);
1075 pthread_mutex_lock(&active_symbols_lock);
1076 if (list_empty(&syme->node) || !syme->node.next)
1077 __list_insert_active_sym(syme);
1078 pthread_mutex_unlock(&active_symbols_lock);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001079 }
1080}
1081
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001082static int event__process(event_t *event, struct perf_session *session)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001083{
1084 switch (event->header.type) {
1085 case PERF_RECORD_COMM:
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001086 event__process_comm(event, session);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001087 break;
1088 case PERF_RECORD_MMAP:
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001089 event__process_mmap(event, session);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001090 break;
Arnaldo Carvalho de Melo0f35cd42010-01-21 13:04:44 -02001091 case PERF_RECORD_FORK:
1092 case PERF_RECORD_EXIT:
1093 event__process_task(event, session);
1094 break;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001095 default:
1096 break;
1097 }
1098
1099 return 0;
Ingo Molnar07800602009-04-20 15:00:56 +02001100}
1101
Ingo Molnar07800602009-04-20 15:00:56 +02001102struct mmap_data {
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001103 int counter;
1104 void *base;
Ingo Molnarf37a2912009-07-01 12:37:06 +02001105 int mask;
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001106 unsigned int prev;
Ingo Molnar07800602009-04-20 15:00:56 +02001107};
1108
1109static unsigned int mmap_read_head(struct mmap_data *md)
1110{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001111 struct perf_event_mmap_page *pc = md->base;
Ingo Molnar07800602009-04-20 15:00:56 +02001112 int head;
1113
1114 head = pc->data_head;
1115 rmb();
1116
1117 return head;
1118}
1119
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001120static void perf_session__mmap_read_counter(struct perf_session *self,
1121 struct mmap_data *md)
Ingo Molnar07800602009-04-20 15:00:56 +02001122{
1123 unsigned int head = mmap_read_head(md);
1124 unsigned int old = md->prev;
1125 unsigned char *data = md->base + page_size;
1126 int diff;
1127
Ingo Molnar07800602009-04-20 15:00:56 +02001128 /*
1129 * If we're further behind than half the buffer, there's a chance
Ingo Molnar2debbc82009-06-05 14:29:10 +02001130 * the writer will bite our tail and mess up the samples under us.
Ingo Molnar07800602009-04-20 15:00:56 +02001131 *
1132 * If we somehow ended up ahead of the head, we got messed up.
1133 *
1134 * In either case, truncate and restart at head.
1135 */
1136 diff = head - old;
1137 if (diff > md->mask / 2 || diff < 0) {
Mike Galbraithf4f0b412009-10-13 14:57:20 +02001138 fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
Ingo Molnar07800602009-04-20 15:00:56 +02001139
1140 /*
1141 * head points to a known good entry, start there.
1142 */
1143 old = head;
1144 }
1145
Ingo Molnar07800602009-04-20 15:00:56 +02001146 for (; old != head;) {
Ingo Molnar07800602009-04-20 15:00:56 +02001147 event_t *event = (event_t *)&data[old & md->mask];
1148
1149 event_t event_copy;
1150
Ingo Molnar6f06ccb2009-04-20 15:22:22 +02001151 size_t size = event->header.size;
Ingo Molnar07800602009-04-20 15:00:56 +02001152
1153 /*
1154 * Event straddles the mmap boundary -- header should always
1155 * be inside due to u64 alignment of output.
1156 */
1157 if ((old & md->mask) + size != ((old + size) & md->mask)) {
1158 unsigned int offset = old;
1159 unsigned int len = min(sizeof(*event), size), cpy;
1160 void *dst = &event_copy;
1161
1162 do {
1163 cpy = min(md->mask + 1 - (offset & md->mask), len);
1164 memcpy(dst, &data[offset & md->mask], cpy);
1165 offset += cpy;
1166 dst += cpy;
1167 len -= cpy;
1168 } while (len);
1169
1170 event = &event_copy;
1171 }
1172
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001173 if (event->header.type == PERF_RECORD_SAMPLE)
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001174 event__process_sample(event, self, md->counter);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001175 else
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001176 event__process(event, self);
Ingo Molnar07800602009-04-20 15:00:56 +02001177 old += size;
Ingo Molnar07800602009-04-20 15:00:56 +02001178 }
1179
1180 md->prev = old;
1181}
1182
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001183static struct pollfd *event_array;
1184static struct mmap_data *mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
Mike Galbraithc2990a22009-05-24 08:35:49 +02001185
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001186static void perf_session__mmap_read(struct perf_session *self)
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001187{
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001188 int i, counter, thread_index;
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001189
1190 for (i = 0; i < nr_cpus; i++) {
1191 for (counter = 0; counter < nr_counters; counter++)
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001192 for (thread_index = 0;
1193 thread_index < thread_num;
1194 thread_index++) {
1195 perf_session__mmap_read_counter(self,
1196 &mmap_array[i][counter][thread_index]);
1197 }
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001198 }
1199}
1200
Ingo Molnar716c69f2009-06-07 17:31:52 +02001201int nr_poll;
1202int group_fd;
1203
1204static void start_counter(int i, int counter)
Ingo Molnar07800602009-04-20 15:00:56 +02001205{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001206 struct perf_event_attr *attr;
Mike Galbraith0fdc7e62009-07-21 10:30:36 +02001207 int cpu;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001208 int thread_index;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001209
1210 cpu = profile_cpu;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001211 if (target_tid == -1 && profile_cpu == -1)
Paul Mackerrasa12b51c2010-03-10 20:36:09 +11001212 cpu = cpumap[i];
Ingo Molnar716c69f2009-06-07 17:31:52 +02001213
1214 attr = attrs + counter;
1215
1216 attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +02001217
1218 if (freq) {
1219 attr->sample_type |= PERF_SAMPLE_PERIOD;
1220 attr->freq = 1;
1221 attr->sample_freq = freq;
1222 }
1223
Mike Galbraith0fdc7e62009-07-21 10:30:36 +02001224 attr->inherit = (cpu < 0) && inherit;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001225 attr->mmap = 1;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001226
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001227 for (thread_index = 0; thread_index < thread_num; thread_index++) {
Ingo Molnar716c69f2009-06-07 17:31:52 +02001228try_again:
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001229 fd[i][counter][thread_index] = sys_perf_event_open(attr,
1230 all_tids[thread_index], cpu, group_fd, 0);
Ingo Molnar716c69f2009-06-07 17:31:52 +02001231
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001232 if (fd[i][counter][thread_index] < 0) {
1233 int err = errno;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001234
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001235 if (err == EPERM || err == EACCES)
1236 die("No permission - are you root?\n");
1237 /*
1238 * If it's cycles then fall back to hrtimer
1239 * based cpu-clock-tick sw counter, which
1240 * is always available even if no PMU support:
1241 */
1242 if (attr->type == PERF_TYPE_HARDWARE
1243 && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
Ingo Molnar716c69f2009-06-07 17:31:52 +02001244
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001245 if (verbose)
1246 warning(" ... trying to fall back to cpu-clock-ticks\n");
Ingo Molnar3da297a2009-06-07 17:39:02 +02001247
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001248 attr->type = PERF_TYPE_SOFTWARE;
1249 attr->config = PERF_COUNT_SW_CPU_CLOCK;
1250 goto try_again;
1251 }
1252 printf("\n");
1253 error("perfcounter syscall returned with %d (%s)\n",
1254 fd[i][counter][thread_index], strerror(err));
1255 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
1256 exit(-1);
Ingo Molnar716c69f2009-06-07 17:31:52 +02001257 }
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001258 assert(fd[i][counter][thread_index] >= 0);
1259 fcntl(fd[i][counter][thread_index], F_SETFL, O_NONBLOCK);
1260
1261 /*
1262 * First counter acts as the group leader:
1263 */
1264 if (group && group_fd == -1)
1265 group_fd = fd[i][counter][thread_index];
1266
1267 event_array[nr_poll].fd = fd[i][counter][thread_index];
1268 event_array[nr_poll].events = POLLIN;
1269 nr_poll++;
1270
1271 mmap_array[i][counter][thread_index].counter = counter;
1272 mmap_array[i][counter][thread_index].prev = 0;
1273 mmap_array[i][counter][thread_index].mask = mmap_pages*page_size - 1;
1274 mmap_array[i][counter][thread_index].base = mmap(NULL, (mmap_pages+1)*page_size,
1275 PROT_READ, MAP_SHARED, fd[i][counter][thread_index], 0);
1276 if (mmap_array[i][counter][thread_index].base == MAP_FAILED)
1277 die("failed to mmap with %d (%s)\n", errno, strerror(errno));
Ingo Molnar716c69f2009-06-07 17:31:52 +02001278 }
Ingo Molnar716c69f2009-06-07 17:31:52 +02001279}
1280
1281static int __cmd_top(void)
1282{
1283 pthread_t thread;
1284 int i, counter;
Ingo Molnar07800602009-04-20 15:00:56 +02001285 int ret;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001286 /*
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001287 * FIXME: perf_session__new should allow passing a O_MMAP, so that all this
1288 * mmap reading, etc is encapsulated in it. Use O_WRONLY for now.
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001289 */
Tom Zanussi454c4072010-05-01 01:41:20 -05001290 struct perf_session *session = perf_session__new(NULL, O_WRONLY, false, false);
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001291 if (session == NULL)
1292 return -ENOMEM;
Ingo Molnar07800602009-04-20 15:00:56 +02001293
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001294 if (target_tid != -1)
1295 event__synthesize_thread(target_tid, event__process, session);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001296 else
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001297 event__synthesize_threads(event__process, session);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001298
Ingo Molnar07800602009-04-20 15:00:56 +02001299 for (i = 0; i < nr_cpus; i++) {
1300 group_fd = -1;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001301 for (counter = 0; counter < nr_counters; counter++)
1302 start_counter(i, counter);
Ingo Molnar07800602009-04-20 15:00:56 +02001303 }
1304
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001305 /* Wait for a minimal set of events before starting the snapshot */
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001306 poll(&event_array[0], nr_poll, 100);
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001307
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001308 perf_session__mmap_read(session);
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001309
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001310 if (pthread_create(&thread, NULL, display_thread, session)) {
Ingo Molnar07800602009-04-20 15:00:56 +02001311 printf("Could not create display thread.\n");
1312 exit(-1);
1313 }
1314
1315 if (realtime_prio) {
1316 struct sched_param param;
1317
1318 param.sched_priority = realtime_prio;
1319 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
1320 printf("Could not set realtime priority.\n");
1321 exit(-1);
1322 }
1323 }
1324
1325 while (1) {
Ingo Molnar2debbc82009-06-05 14:29:10 +02001326 int hits = samples;
Ingo Molnar07800602009-04-20 15:00:56 +02001327
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001328 perf_session__mmap_read(session);
Ingo Molnar07800602009-04-20 15:00:56 +02001329
Ingo Molnar2debbc82009-06-05 14:29:10 +02001330 if (hits == samples)
Ingo Molnar07800602009-04-20 15:00:56 +02001331 ret = poll(event_array, nr_poll, 100);
1332 }
1333
1334 return 0;
1335}
Ingo Molnarb456bae2009-05-26 09:17:18 +02001336
1337static const char * const top_usage[] = {
1338 "perf top [<options>]",
1339 NULL
1340};
1341
Ingo Molnarb456bae2009-05-26 09:17:18 +02001342static const struct option options[] = {
1343 OPT_CALLBACK('e', "event", NULL, "event",
Thomas Gleixner86847b62009-06-06 12:24:17 +02001344 "event selector. use 'perf list' to list available events",
1345 parse_events),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001346 OPT_INTEGER('c', "count", &default_interval,
1347 "event period to sample"),
1348 OPT_INTEGER('p', "pid", &target_pid,
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001349 "profile events on existing process id"),
1350 OPT_INTEGER('t', "tid", &target_tid,
1351 "profile events on existing thread id"),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001352 OPT_BOOLEAN('a', "all-cpus", &system_wide,
1353 "system-wide collection from all CPUs"),
1354 OPT_INTEGER('C', "CPU", &profile_cpu,
1355 "CPU to profile on"),
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02001356 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1357 "file", "vmlinux pathname"),
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -02001358 OPT_BOOLEAN('K', "hide_kernel_symbols", &hide_kernel_symbols,
1359 "hide kernel symbols"),
Arnaldo Carvalho de Melo19679362010-05-17 15:39:16 -03001360 OPT_UINTEGER('m', "mmap-pages", &mmap_pages, "number of mmap data pages"),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001361 OPT_INTEGER('r', "realtime", &realtime_prio,
1362 "collect data with this RT SCHED_FIFO priority"),
Mike Galbraithdb20c002009-05-26 15:25:34 +02001363 OPT_INTEGER('d', "delay", &delay_secs,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001364 "number of seconds to delay between refreshes"),
1365 OPT_BOOLEAN('D', "dump-symtab", &dump_symtab,
1366 "dump the symbol table used for profiling"),
Ingo Molnar6e53cdf2009-06-04 08:53:05 +02001367 OPT_INTEGER('f', "count-filter", &count_filter,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001368 "only display functions with more events than this"),
1369 OPT_BOOLEAN('g', "group", &group,
1370 "put the counters into a counter group"),
Mike Galbraith0fdc7e62009-07-21 10:30:36 +02001371 OPT_BOOLEAN('i', "inherit", &inherit,
1372 "child tasks inherit counters"),
Mike Galbraith923c42c2009-07-22 20:36:03 +02001373 OPT_STRING('s', "sym-annotate", &sym_filter, "symbol name",
Kirill Smelkov6cff0e82010-02-03 16:52:08 -02001374 "symbol to annotate"),
Anton Blanchard1f208ea2009-07-01 09:00:44 +10001375 OPT_BOOLEAN('z', "zero", &zero,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001376 "zero history across updates"),
Ingo Molnar6e53cdf2009-06-04 08:53:05 +02001377 OPT_INTEGER('F', "freq", &freq,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001378 "profile at this frequency"),
Ingo Molnar6e53cdf2009-06-04 08:53:05 +02001379 OPT_INTEGER('E', "entries", &print_entries,
1380 "display this many functions"),
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -02001381 OPT_BOOLEAN('U', "hide_user_symbols", &hide_user_symbols,
1382 "hide user symbols"),
Ian Munsiec0555642010-04-13 18:37:33 +10001383 OPT_INCR('v', "verbose", &verbose,
Ingo Molnar3da297a2009-06-07 17:39:02 +02001384 "be more verbose (show counter open errors, etc)"),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001385 OPT_END()
1386};
1387
Ingo Molnarf37a2912009-07-01 12:37:06 +02001388int cmd_top(int argc, const char **argv, const char *prefix __used)
Ingo Molnarb456bae2009-05-26 09:17:18 +02001389{
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02001390 int counter;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001391 int i,j;
Ingo Molnarb456bae2009-05-26 09:17:18 +02001392
1393 page_size = sysconf(_SC_PAGE_SIZE);
1394
Ingo Molnarb456bae2009-05-26 09:17:18 +02001395 argc = parse_options(argc, argv, options, top_usage, 0);
1396 if (argc)
1397 usage_with_options(top_usage, options);
1398
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001399 if (target_pid != -1) {
1400 target_tid = target_pid;
1401 thread_num = find_all_tid(target_pid, &all_tids);
1402 if (thread_num <= 0) {
1403 fprintf(stderr, "Can't find all threads of pid %d\n",
1404 target_pid);
1405 usage_with_options(top_usage, options);
1406 }
1407 } else {
1408 all_tids=malloc(sizeof(pid_t));
1409 if (!all_tids)
1410 return -ENOMEM;
1411
1412 all_tids[0] = target_tid;
1413 thread_num = 1;
1414 }
1415
1416 for (i = 0; i < MAX_NR_CPUS; i++) {
1417 for (j = 0; j < MAX_COUNTERS; j++) {
1418 fd[i][j] = malloc(sizeof(int)*thread_num);
Zhang, Yanmin5a103172010-03-25 19:59:01 -03001419 mmap_array[i][j] = zalloc(
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001420 sizeof(struct mmap_data)*thread_num);
1421 if (!fd[i][j] || !mmap_array[i][j])
1422 return -ENOMEM;
1423 }
1424 }
1425 event_array = malloc(
1426 sizeof(struct pollfd)*MAX_NR_CPUS*MAX_COUNTERS*thread_num);
1427 if (!event_array)
1428 return -ENOMEM;
1429
Ingo Molnarb456bae2009-05-26 09:17:18 +02001430 /* CPU and PID are mutually exclusive */
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001431 if (target_tid > 0 && profile_cpu != -1) {
Ingo Molnarb456bae2009-05-26 09:17:18 +02001432 printf("WARNING: PID switch overriding CPU\n");
1433 sleep(1);
1434 profile_cpu = -1;
1435 }
1436
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001437 if (!nr_counters)
Ingo Molnarb456bae2009-05-26 09:17:18 +02001438 nr_counters = 1;
Ingo Molnarb456bae2009-05-26 09:17:18 +02001439
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02001440 symbol_conf.priv_size = (sizeof(struct sym_entry) +
1441 (nr_counters + 1) * sizeof(unsigned long));
Kirill Smelkov6cff0e82010-02-03 16:52:08 -02001442
1443 symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL);
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02001444 if (symbol__init() < 0)
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02001445 return -1;
Arnaldo Carvalho de Melo5a8e5a32009-11-17 18:38:01 -02001446
Frederic Weisbecker2f335a02009-06-05 19:31:01 +02001447 if (delay_secs < 1)
1448 delay_secs = 1;
1449
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +02001450 /*
1451 * User specified count overrides default frequency.
1452 */
1453 if (default_interval)
1454 freq = 0;
1455 else if (freq) {
1456 default_interval = freq;
1457 } else {
1458 fprintf(stderr, "frequency and count are zero, aborting\n");
1459 exit(EXIT_FAILURE);
1460 }
1461
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001462 /*
1463 * Fill in the ones not specifically initialized via -c:
1464 */
Ingo Molnarb456bae2009-05-26 09:17:18 +02001465 for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001466 if (attrs[counter].sample_period)
Ingo Molnarb456bae2009-05-26 09:17:18 +02001467 continue;
1468
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001469 attrs[counter].sample_period = default_interval;
Ingo Molnarb456bae2009-05-26 09:17:18 +02001470 }
1471
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001472 if (target_tid != -1 || profile_cpu != -1)
Ingo Molnarb456bae2009-05-26 09:17:18 +02001473 nr_cpus = 1;
Paul Mackerrasa12b51c2010-03-10 20:36:09 +11001474 else
1475 nr_cpus = read_cpu_map();
Ingo Molnarb456bae2009-05-26 09:17:18 +02001476
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -02001477 get_term_dimensions(&winsize);
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -02001478 if (print_entries == 0) {
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -02001479 update_print_entries(&winsize);
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -02001480 signal(SIGWINCH, sig_winch_handler);
1481 }
1482
Ingo Molnarb456bae2009-05-26 09:17:18 +02001483 return __cmd_top();
1484}