blob: 31f2e597800c7f4331a9aa97ec9045f04a52306b [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"
Ingo Molnar07800602009-04-20 15:00:56 +020031
Frederic Weisbecker8f288272009-08-16 22:05:48 +020032#include "util/debug.h"
33
Ingo Molnar07800602009-04-20 15:00:56 +020034#include <assert.h>
35#include <fcntl.h>
Ingo Molnar0e9b20b2009-05-26 09:17:18 +020036
Ingo Molnar07800602009-04-20 15:00:56 +020037#include <stdio.h>
Mike Galbraith923c42c2009-07-22 20:36:03 +020038#include <termios.h>
39#include <unistd.h>
Ingo Molnar0e9b20b2009-05-26 09:17:18 +020040
Ingo Molnar07800602009-04-20 15:00:56 +020041#include <errno.h>
Ingo Molnar07800602009-04-20 15:00:56 +020042#include <time.h>
43#include <sched.h>
44#include <pthread.h>
45
46#include <sys/syscall.h>
47#include <sys/ioctl.h>
48#include <sys/poll.h>
49#include <sys/prctl.h>
50#include <sys/wait.h>
51#include <sys/uio.h>
52#include <sys/mman.h>
53
54#include <linux/unistd.h>
55#include <linux/types.h>
56
Ingo Molnara21ca2c2009-06-06 09:58:57 +020057static int fd[MAX_NR_CPUS][MAX_COUNTERS];
58
Ingo Molnar42e59d72009-10-06 15:14:21 +020059static int system_wide = 0;
Ingo Molnar07800602009-04-20 15:00:56 +020060
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +020061static int default_interval = 0;
Ingo Molnar07800602009-04-20 15:00:56 +020062
Ingo Molnar42e59d72009-10-06 15:14:21 +020063static int count_filter = 5;
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -020064static int print_entries;
Ingo Molnar07800602009-04-20 15:00:56 +020065
Ingo Molnar42e59d72009-10-06 15:14:21 +020066static int target_pid = -1;
67static int inherit = 0;
68static int profile_cpu = -1;
69static int nr_cpus = 0;
70static unsigned int realtime_prio = 0;
71static int group = 0;
Ingo Molnar07800602009-04-20 15:00:56 +020072static unsigned int page_size;
Ingo Molnar42e59d72009-10-06 15:14:21 +020073static unsigned int mmap_pages = 16;
74static int freq = 1000; /* 1 KHz */
Ingo Molnar07800602009-04-20 15:00:56 +020075
Ingo Molnar42e59d72009-10-06 15:14:21 +020076static int delay_secs = 2;
77static int zero = 0;
78static int dump_symtab = 0;
Ingo Molnar07800602009-04-20 15:00:56 +020079
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -020080static bool hide_kernel_symbols = false;
81static bool hide_user_symbols = false;
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -020082static struct winsize winsize;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -020083
Ingo Molnar07800602009-04-20 15:00:56 +020084/*
Mike Galbraith923c42c2009-07-22 20:36:03 +020085 * Source
86 */
87
88struct source_line {
89 u64 eip;
90 unsigned long count[MAX_COUNTERS];
91 char *line;
92 struct source_line *next;
93};
94
Ingo Molnar42e59d72009-10-06 15:14:21 +020095static char *sym_filter = NULL;
96struct sym_entry *sym_filter_entry = NULL;
Kirill Smelkov6cff0e82010-02-03 16:52:08 -020097struct sym_entry *sym_filter_entry_sched = NULL;
Ingo Molnar42e59d72009-10-06 15:14:21 +020098static int sym_pcnt_filter = 5;
99static int sym_counter = 0;
100static int display_weighted = -1;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200101
102/*
Ingo Molnar07800602009-04-20 15:00:56 +0200103 * Symbols
104 */
105
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200106struct sym_entry_source {
107 struct source_line *source;
108 struct source_line *lines;
109 struct source_line **lines_tail;
110 pthread_mutex_t lock;
111};
112
Ingo Molnar07800602009-04-20 15:00:56 +0200113struct sym_entry {
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300114 struct rb_node rb_node;
115 struct list_head node;
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300116 unsigned long snap_count;
117 double weight;
Ingo Molnar07800602009-04-20 15:00:56 +0200118 int skip;
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200119 u16 name_len;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200120 u8 origin;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300121 struct map *map;
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200122 struct sym_entry_source *src;
Arnaldo Carvalho de Melo5a8e5a32009-11-17 18:38:01 -0200123 unsigned long count[0];
Ingo Molnar07800602009-04-20 15:00:56 +0200124};
125
Mike Galbraith923c42c2009-07-22 20:36:03 +0200126/*
127 * Source functions
128 */
129
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200130static inline struct symbol *sym_entry__symbol(struct sym_entry *self)
131{
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -0200132 return ((void *)self) + symbol_conf.priv_size;
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200133}
134
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200135static void get_term_dimensions(struct winsize *ws)
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200136{
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200137 char *s = getenv("LINES");
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200138
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200139 if (s != NULL) {
140 ws->ws_row = atoi(s);
141 s = getenv("COLUMNS");
142 if (s != NULL) {
143 ws->ws_col = atoi(s);
144 if (ws->ws_row && ws->ws_col)
145 return;
146 }
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200147 }
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200148#ifdef TIOCGWINSZ
149 if (ioctl(1, TIOCGWINSZ, ws) == 0 &&
150 ws->ws_row && ws->ws_col)
151 return;
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200152#endif
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200153 ws->ws_row = 25;
154 ws->ws_col = 80;
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200155}
156
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200157static void update_print_entries(struct winsize *ws)
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200158{
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200159 print_entries = ws->ws_row;
160
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200161 if (print_entries > 9)
162 print_entries -= 9;
163}
164
165static void sig_winch_handler(int sig __used)
166{
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200167 get_term_dimensions(&winsize);
168 update_print_entries(&winsize);
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200169}
170
Mike Galbraith923c42c2009-07-22 20:36:03 +0200171static void parse_source(struct sym_entry *syme)
172{
173 struct symbol *sym;
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200174 struct sym_entry_source *source;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300175 struct map *map;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200176 FILE *file;
Ingo Molnar83a09442009-08-15 12:26:57 +0200177 char command[PATH_MAX*2];
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300178 const char *path;
179 u64 len;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200180
181 if (!syme)
182 return;
183
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200184 if (syme->src == NULL) {
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200185 syme->src = zalloc(sizeof(*source));
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200186 if (syme->src == NULL)
187 return;
188 pthread_mutex_init(&syme->src->lock, NULL);
189 }
190
191 source = syme->src;
192
193 if (source->lines) {
194 pthread_mutex_lock(&source->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200195 goto out_assign;
196 }
197
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200198 sym = sym_entry__symbol(syme);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300199 map = syme->map;
200 path = map->dso->long_name;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200201
Mike Galbraith923c42c2009-07-22 20:36:03 +0200202 len = sym->end - sym->start;
203
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300204 sprintf(command,
Arnaldo Carvalho de Melo5f485362010-02-07 11:46:16 -0200205 "objdump --start-address=%#0*Lx --stop-address=%#0*Lx -dS %s",
206 BITS_PER_LONG / 4, map__rip_2objdump(map, sym->start),
207 BITS_PER_LONG / 4, map__rip_2objdump(map, sym->end), path);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200208
209 file = popen(command, "r");
210 if (!file)
211 return;
212
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200213 pthread_mutex_lock(&source->lock);
214 source->lines_tail = &source->lines;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200215 while (!feof(file)) {
216 struct source_line *src;
217 size_t dummy = 0;
Kirill Smelkovee11b902010-02-07 11:46:15 -0200218 char *c, *sep;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200219
220 src = malloc(sizeof(struct source_line));
221 assert(src != NULL);
222 memset(src, 0, sizeof(struct source_line));
223
224 if (getline(&src->line, &dummy, file) < 0)
225 break;
226 if (!src->line)
227 break;
228
229 c = strchr(src->line, '\n');
230 if (c)
231 *c = 0;
232
233 src->next = NULL;
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200234 *source->lines_tail = src;
235 source->lines_tail = &src->next;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200236
Kirill Smelkovee11b902010-02-07 11:46:15 -0200237 src->eip = strtoull(src->line, &sep, 16);
238 if (*sep == ':')
239 src->eip = map__objdump_2ip(map, src->eip);
240 else /* this line has no ip info (e.g. source line) */
241 src->eip = 0;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200242 }
243 pclose(file);
244out_assign:
245 sym_filter_entry = syme;
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200246 pthread_mutex_unlock(&source->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200247}
248
249static void __zero_source_counters(struct sym_entry *syme)
250{
251 int i;
252 struct source_line *line;
253
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200254 line = syme->src->lines;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200255 while (line) {
256 for (i = 0; i < nr_counters; i++)
257 line->count[i] = 0;
258 line = line->next;
259 }
260}
261
262static void record_precise_ip(struct sym_entry *syme, int counter, u64 ip)
263{
264 struct source_line *line;
265
266 if (syme != sym_filter_entry)
267 return;
268
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200269 if (pthread_mutex_trylock(&syme->src->lock))
Mike Galbraith923c42c2009-07-22 20:36:03 +0200270 return;
271
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200272 if (syme->src == NULL || syme->src->source == NULL)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200273 goto out_unlock;
274
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200275 for (line = syme->src->lines; line; line = line->next) {
Kirill Smelkovee11b902010-02-07 11:46:15 -0200276 /* skip lines without IP info */
277 if (line->eip == 0)
278 continue;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200279 if (line->eip == ip) {
280 line->count[counter]++;
281 break;
282 }
283 if (line->eip > ip)
284 break;
285 }
286out_unlock:
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200287 pthread_mutex_unlock(&syme->src->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200288}
289
Arnaldo Carvalho de Meloc7ad21a2010-02-22 16:14:22 -0300290#define PATTERN_LEN (BITS_PER_LONG / 4 + 2)
291
Mike Galbraith923c42c2009-07-22 20:36:03 +0200292static void lookup_sym_source(struct sym_entry *syme)
293{
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200294 struct symbol *symbol = sym_entry__symbol(syme);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200295 struct source_line *line;
Arnaldo Carvalho de Meloc7ad21a2010-02-22 16:14:22 -0300296 char pattern[PATTERN_LEN + 1];
Mike Galbraith923c42c2009-07-22 20:36:03 +0200297
Arnaldo Carvalho de Melo5f485362010-02-07 11:46:16 -0200298 sprintf(pattern, "%0*Lx <", BITS_PER_LONG / 4,
299 map__rip_2objdump(syme->map, symbol->start));
Mike Galbraith923c42c2009-07-22 20:36:03 +0200300
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200301 pthread_mutex_lock(&syme->src->lock);
302 for (line = syme->src->lines; line; line = line->next) {
Arnaldo Carvalho de Meloc7ad21a2010-02-22 16:14:22 -0300303 if (memcmp(line->line, pattern, PATTERN_LEN) == 0) {
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200304 syme->src->source = line;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200305 break;
306 }
307 }
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200308 pthread_mutex_unlock(&syme->src->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200309}
310
311static void show_lines(struct source_line *queue, int count, int total)
312{
313 int i;
314 struct source_line *line;
315
316 line = queue;
317 for (i = 0; i < count; i++) {
318 float pcnt = 100.0*(float)line->count[sym_counter]/(float)total;
319
320 printf("%8li %4.1f%%\t%s\n", line->count[sym_counter], pcnt, line->line);
321 line = line->next;
322 }
323}
324
325#define TRACE_COUNT 3
326
327static void show_details(struct sym_entry *syme)
328{
329 struct symbol *symbol;
330 struct source_line *line;
331 struct source_line *line_queue = NULL;
332 int displayed = 0;
333 int line_queue_count = 0, total = 0, more = 0;
334
335 if (!syme)
336 return;
337
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200338 if (!syme->src->source)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200339 lookup_sym_source(syme);
340
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200341 if (!syme->src->source)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200342 return;
343
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200344 symbol = sym_entry__symbol(syme);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200345 printf("Showing %s for %s\n", event_name(sym_counter), symbol->name);
346 printf(" Events Pcnt (>=%d%%)\n", sym_pcnt_filter);
347
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200348 pthread_mutex_lock(&syme->src->lock);
349 line = syme->src->source;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200350 while (line) {
351 total += line->count[sym_counter];
352 line = line->next;
353 }
354
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200355 line = syme->src->source;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200356 while (line) {
357 float pcnt = 0.0;
358
359 if (!line_queue_count)
360 line_queue = line;
361 line_queue_count++;
362
363 if (line->count[sym_counter])
364 pcnt = 100.0 * line->count[sym_counter] / (float)total;
365 if (pcnt >= (float)sym_pcnt_filter) {
366 if (displayed <= print_entries)
367 show_lines(line_queue, line_queue_count, total);
368 else more++;
369 displayed += line_queue_count;
370 line_queue_count = 0;
371 line_queue = NULL;
372 } else if (line_queue_count > TRACE_COUNT) {
373 line_queue = line_queue->next;
374 line_queue_count--;
375 }
376
377 line->count[sym_counter] = zero ? 0 : line->count[sym_counter] * 7 / 8;
378 line = line->next;
379 }
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200380 pthread_mutex_unlock(&syme->src->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200381 if (more)
382 printf("%d lines not displayed, maybe increase display entries [e]\n", more);
383}
Ingo Molnar07800602009-04-20 15:00:56 +0200384
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300385/*
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200386 * Symbols will be added here in event__process_sample and will get out
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300387 * after decayed.
388 */
389static LIST_HEAD(active_symbols);
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300390static pthread_mutex_t active_symbols_lock = PTHREAD_MUTEX_INITIALIZER;
Ingo Molnar07800602009-04-20 15:00:56 +0200391
Ingo Molnar07800602009-04-20 15:00:56 +0200392/*
393 * Ordering weight: count-1 * count-2 * ... / count-n
394 */
395static double sym_weight(const struct sym_entry *sym)
396{
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300397 double weight = sym->snap_count;
Ingo Molnar07800602009-04-20 15:00:56 +0200398 int counter;
399
Mike Galbraith46ab9762009-07-24 10:09:50 +0200400 if (!display_weighted)
401 return weight;
402
Ingo Molnar07800602009-04-20 15:00:56 +0200403 for (counter = 1; counter < nr_counters-1; counter++)
404 weight *= sym->count[counter];
405
406 weight /= (sym->count[counter] + 1);
407
408 return weight;
409}
410
Ingo Molnar2debbc82009-06-05 14:29:10 +0200411static long samples;
412static long userspace_samples;
Ingo Molnar07800602009-04-20 15:00:56 +0200413static const char CONSOLE_CLEAR[] = "";
414
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300415static void __list_insert_active_sym(struct sym_entry *syme)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300416{
417 list_add(&syme->node, &active_symbols);
418}
419
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300420static void list_remove_active_sym(struct sym_entry *syme)
421{
422 pthread_mutex_lock(&active_symbols_lock);
423 list_del_init(&syme->node);
424 pthread_mutex_unlock(&active_symbols_lock);
425}
426
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300427static void rb_insert_active_sym(struct rb_root *tree, struct sym_entry *se)
428{
429 struct rb_node **p = &tree->rb_node;
430 struct rb_node *parent = NULL;
431 struct sym_entry *iter;
432
433 while (*p != NULL) {
434 parent = *p;
435 iter = rb_entry(parent, struct sym_entry, rb_node);
436
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300437 if (se->weight > iter->weight)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300438 p = &(*p)->rb_left;
439 else
440 p = &(*p)->rb_right;
441 }
442
443 rb_link_node(&se->rb_node, parent, p);
444 rb_insert_color(&se->rb_node, tree);
445}
Ingo Molnar07800602009-04-20 15:00:56 +0200446
447static void print_sym_table(void)
448{
Ingo Molnar233f0b92009-06-03 21:48:40 +0200449 int printed = 0, j;
Mike Galbraith46ab9762009-07-24 10:09:50 +0200450 int counter, snap = !display_weighted ? sym_counter : 0;
Ingo Molnar2debbc82009-06-05 14:29:10 +0200451 float samples_per_sec = samples/delay_secs;
452 float ksamples_per_sec = (samples-userspace_samples)/delay_secs;
453 float sum_ksamples = 0.0;
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300454 struct sym_entry *syme, *n;
455 struct rb_root tmp = RB_ROOT;
456 struct rb_node *nd;
Arnaldo Carvalho de Melo7cc017e2009-11-24 12:05:14 -0200457 int sym_width = 0, dso_width = 0, max_dso_width;
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200458 const int win_width = winsize.ws_col - 1;
Ingo Molnar07800602009-04-20 15:00:56 +0200459
Ingo Molnar2debbc82009-06-05 14:29:10 +0200460 samples = userspace_samples = 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200461
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300462 /* Sort the active symbols */
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300463 pthread_mutex_lock(&active_symbols_lock);
464 syme = list_entry(active_symbols.next, struct sym_entry, node);
465 pthread_mutex_unlock(&active_symbols_lock);
466
467 list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
Mike Galbraith46ab9762009-07-24 10:09:50 +0200468 syme->snap_count = syme->count[snap];
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300469 if (syme->snap_count != 0) {
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200470
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200471 if ((hide_user_symbols &&
472 syme->origin == PERF_RECORD_MISC_USER) ||
473 (hide_kernel_symbols &&
474 syme->origin == PERF_RECORD_MISC_KERNEL)) {
475 list_remove_active_sym(syme);
476 continue;
477 }
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300478 syme->weight = sym_weight(syme);
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300479 rb_insert_active_sym(&tmp, syme);
Ingo Molnar2debbc82009-06-05 14:29:10 +0200480 sum_ksamples += syme->snap_count;
Mike Galbraithd94b9432009-05-25 09:57:56 +0200481
482 for (j = 0; j < nr_counters; j++)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300483 syme->count[j] = zero ? 0 : syme->count[j] * 7 / 8;
484 } else
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300485 list_remove_active_sym(syme);
Mike Galbraithd94b9432009-05-25 09:57:56 +0200486 }
487
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200488 puts(CONSOLE_CLEAR);
Ingo Molnar07800602009-04-20 15:00:56 +0200489
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200490 printf("%-*.*s\n", win_width, win_width, graph_dotted_line);
Ingo Molnarf2521b62009-06-03 19:17:25 +0200491 printf( " PerfTop:%8.0f irqs/sec kernel:%4.1f%% [",
Ingo Molnar2debbc82009-06-05 14:29:10 +0200492 samples_per_sec,
493 100.0 - (100.0*((samples_per_sec-ksamples_per_sec)/samples_per_sec)));
Ingo Molnar07800602009-04-20 15:00:56 +0200494
Mike Galbraith46ab9762009-07-24 10:09:50 +0200495 if (nr_counters == 1 || !display_weighted) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000496 printf("%Ld", (u64)attrs[0].sample_period);
Ingo Molnarcf1f4572009-06-05 13:27:02 +0200497 if (freq)
498 printf("Hz ");
499 else
500 printf(" ");
501 }
Ingo Molnar07800602009-04-20 15:00:56 +0200502
Mike Galbraith46ab9762009-07-24 10:09:50 +0200503 if (!display_weighted)
504 printf("%s", event_name(sym_counter));
505 else for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnar07800602009-04-20 15:00:56 +0200506 if (counter)
507 printf("/");
508
509 printf("%s", event_name(counter));
510 }
511
512 printf( "], ");
513
Ingo Molnarb456bae2009-05-26 09:17:18 +0200514 if (target_pid != -1)
515 printf(" (target_pid: %d", target_pid);
Ingo Molnar07800602009-04-20 15:00:56 +0200516 else
517 printf(" (all");
518
519 if (profile_cpu != -1)
520 printf(", cpu: %d)\n", profile_cpu);
521 else {
Ingo Molnarb456bae2009-05-26 09:17:18 +0200522 if (target_pid != -1)
Ingo Molnar07800602009-04-20 15:00:56 +0200523 printf(")\n");
524 else
525 printf(", %d CPUs)\n", nr_cpus);
526 }
527
Arnaldo Carvalho de Melo1a105f72009-11-17 15:40:55 -0200528 printf("%-*.*s\n", win_width, win_width, graph_dotted_line);
Ingo Molnar07800602009-04-20 15:00:56 +0200529
Mike Galbraith923c42c2009-07-22 20:36:03 +0200530 if (sym_filter_entry) {
531 show_details(sym_filter_entry);
532 return;
533 }
534
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200535 /*
536 * Find the longest symbol name that will be displayed
537 */
538 for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
539 syme = rb_entry(nd, struct sym_entry, rb_node);
540 if (++printed > print_entries ||
541 (int)syme->snap_count < count_filter)
542 continue;
543
Arnaldo Carvalho de Melo1a105f72009-11-17 15:40:55 -0200544 if (syme->map->dso->long_name_len > dso_width)
545 dso_width = syme->map->dso->long_name_len;
546
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200547 if (syme->name_len > sym_width)
548 sym_width = syme->name_len;
549 }
550
551 printed = 0;
552
Arnaldo Carvalho de Melo7cc017e2009-11-24 12:05:14 -0200553 max_dso_width = winsize.ws_col - sym_width - 29;
554 if (dso_width > max_dso_width)
555 dso_width = max_dso_width;
556 putchar('\n');
Ingo Molnar07800602009-04-20 15:00:56 +0200557 if (nr_counters == 1)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200558 printf(" samples pcnt");
Ingo Molnar07800602009-04-20 15:00:56 +0200559 else
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200560 printf(" weight samples pcnt");
Ingo Molnar07800602009-04-20 15:00:56 +0200561
Arnaldo Carvalho de Melo7ced1562009-08-26 11:51:26 -0300562 if (verbose)
563 printf(" RIP ");
Arnaldo Carvalho de Melo7cc017e2009-11-24 12:05:14 -0200564 printf(" %-*.*s DSO\n", sym_width, sym_width, "function");
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200565 printf(" %s _______ _____",
Arnaldo Carvalho de Melo7ced1562009-08-26 11:51:26 -0300566 nr_counters == 1 ? " " : "______");
567 if (verbose)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200568 printf(" ________________");
Arnaldo Carvalho de Melo1a105f72009-11-17 15:40:55 -0200569 printf(" %-*.*s", sym_width, sym_width, graph_line);
Arnaldo Carvalho de Melo7cc017e2009-11-24 12:05:14 -0200570 printf(" %-*.*s", dso_width, dso_width, graph_line);
Arnaldo Carvalho de Melo1a105f72009-11-17 15:40:55 -0200571 puts("\n");
Ingo Molnar07800602009-04-20 15:00:56 +0200572
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300573 for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
Ingo Molnar83a09442009-08-15 12:26:57 +0200574 struct symbol *sym;
Ingo Molnar8fc03212009-06-04 15:19:47 +0200575 double pcnt;
Ingo Molnar07800602009-04-20 15:00:56 +0200576
Ingo Molnar83a09442009-08-15 12:26:57 +0200577 syme = rb_entry(nd, struct sym_entry, rb_node);
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200578 sym = sym_entry__symbol(syme);
Ingo Molnar83a09442009-08-15 12:26:57 +0200579
Mike Galbraith923c42c2009-07-22 20:36:03 +0200580 if (++printed > print_entries || (int)syme->snap_count < count_filter)
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300581 continue;
Ingo Molnar07800602009-04-20 15:00:56 +0200582
Ingo Molnar2debbc82009-06-05 14:29:10 +0200583 pcnt = 100.0 - (100.0 * ((sum_ksamples - syme->snap_count) /
584 sum_ksamples));
Mike Galbraithd94b9432009-05-25 09:57:56 +0200585
Mike Galbraith46ab9762009-07-24 10:09:50 +0200586 if (nr_counters == 1 || !display_weighted)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200587 printf("%20.2f ", syme->weight);
Mike Galbraithd94b9432009-05-25 09:57:56 +0200588 else
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200589 printf("%9.1f %10ld ", syme->weight, syme->snap_count);
Ingo Molnar8fc03212009-06-04 15:19:47 +0200590
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200591 percent_color_fprintf(stdout, "%4.1f%%", pcnt);
Arnaldo Carvalho de Melo7ced1562009-08-26 11:51:26 -0300592 if (verbose)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200593 printf(" %016llx", sym->start);
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200594 printf(" %-*.*s", sym_width, sym_width, sym->name);
Arnaldo Carvalho de Melo7cc017e2009-11-24 12:05:14 -0200595 printf(" %-*.*s\n", dso_width, dso_width,
596 dso_width >= syme->map->dso->long_name_len ?
597 syme->map->dso->long_name :
598 syme->map->dso->short_name);
Ingo Molnar07800602009-04-20 15:00:56 +0200599 }
Ingo Molnar07800602009-04-20 15:00:56 +0200600}
601
Mike Galbraith923c42c2009-07-22 20:36:03 +0200602static void prompt_integer(int *target, const char *msg)
603{
604 char *buf = malloc(0), *p;
605 size_t dummy = 0;
606 int tmp;
607
608 fprintf(stdout, "\n%s: ", msg);
609 if (getline(&buf, &dummy, stdin) < 0)
610 return;
611
612 p = strchr(buf, '\n');
613 if (p)
614 *p = 0;
615
616 p = buf;
617 while(*p) {
618 if (!isdigit(*p))
619 goto out_free;
620 p++;
621 }
622 tmp = strtoul(buf, NULL, 10);
623 *target = tmp;
624out_free:
625 free(buf);
626}
627
628static void prompt_percent(int *target, const char *msg)
629{
630 int tmp = 0;
631
632 prompt_integer(&tmp, msg);
633 if (tmp >= 0 && tmp <= 100)
634 *target = tmp;
635}
636
637static void prompt_symbol(struct sym_entry **target, const char *msg)
638{
639 char *buf = malloc(0), *p;
640 struct sym_entry *syme = *target, *n, *found = NULL;
641 size_t dummy = 0;
642
643 /* zero counters of active symbol */
644 if (syme) {
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200645 pthread_mutex_lock(&syme->src->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200646 __zero_source_counters(syme);
647 *target = NULL;
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200648 pthread_mutex_unlock(&syme->src->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200649 }
650
651 fprintf(stdout, "\n%s: ", msg);
652 if (getline(&buf, &dummy, stdin) < 0)
653 goto out_free;
654
655 p = strchr(buf, '\n');
656 if (p)
657 *p = 0;
658
659 pthread_mutex_lock(&active_symbols_lock);
660 syme = list_entry(active_symbols.next, struct sym_entry, node);
661 pthread_mutex_unlock(&active_symbols_lock);
662
663 list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200664 struct symbol *sym = sym_entry__symbol(syme);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200665
666 if (!strcmp(buf, sym->name)) {
667 found = syme;
668 break;
669 }
670 }
671
672 if (!found) {
Kirill Smelkov66aeb6d2010-01-13 13:22:19 -0200673 fprintf(stderr, "Sorry, %s is not active.\n", buf);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200674 sleep(1);
675 return;
676 } else
677 parse_source(found);
678
679out_free:
680 free(buf);
681}
682
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200683static void print_mapped_keys(void)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200684{
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200685 char *name = NULL;
686
687 if (sym_filter_entry) {
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200688 struct symbol *sym = sym_entry__symbol(sym_filter_entry);
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200689 name = sym->name;
690 }
691
692 fprintf(stdout, "\nMapped keys:\n");
693 fprintf(stdout, "\t[d] display refresh delay. \t(%d)\n", delay_secs);
694 fprintf(stdout, "\t[e] display entries (lines). \t(%d)\n", print_entries);
695
696 if (nr_counters > 1)
697 fprintf(stdout, "\t[E] active event counter. \t(%s)\n", event_name(sym_counter));
698
699 fprintf(stdout, "\t[f] profile display filter (count). \t(%d)\n", count_filter);
700
Kirill Smelkov6cff0e82010-02-03 16:52:08 -0200701 fprintf(stdout, "\t[F] annotate display filter (percent). \t(%d%%)\n", sym_pcnt_filter);
702 fprintf(stdout, "\t[s] annotate symbol. \t(%s)\n", name?: "NULL");
703 fprintf(stdout, "\t[S] stop annotation.\n");
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200704
705 if (nr_counters > 1)
706 fprintf(stdout, "\t[w] toggle display weighted/count[E]r. \t(%d)\n", display_weighted ? 1 : 0);
707
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200708 fprintf(stdout,
Kirill Smelkov1a72cfa2010-02-12 19:20:59 +0300709 "\t[K] hide kernel_symbols symbols. \t(%s)\n",
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200710 hide_kernel_symbols ? "yes" : "no");
711 fprintf(stdout,
712 "\t[U] hide user symbols. \t(%s)\n",
713 hide_user_symbols ? "yes" : "no");
Mike Galbraith46ab9762009-07-24 10:09:50 +0200714 fprintf(stdout, "\t[z] toggle sample zeroing. \t(%d)\n", zero ? 1 : 0);
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200715 fprintf(stdout, "\t[qQ] quit.\n");
716}
717
718static int key_mapped(int c)
719{
720 switch (c) {
721 case 'd':
722 case 'e':
723 case 'f':
724 case 'z':
725 case 'q':
726 case 'Q':
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200727 case 'K':
728 case 'U':
Kirill Smelkov6cff0e82010-02-03 16:52:08 -0200729 case 'F':
730 case 's':
731 case 'S':
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200732 return 1;
733 case 'E':
734 case 'w':
735 return nr_counters > 1 ? 1 : 0;
Ingo Molnar83a09442009-08-15 12:26:57 +0200736 default:
737 break;
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200738 }
739
740 return 0;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200741}
742
743static void handle_keypress(int c)
744{
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200745 if (!key_mapped(c)) {
746 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
747 struct termios tc, save;
748
749 print_mapped_keys();
750 fprintf(stdout, "\nEnter selection, or unmapped key to continue: ");
751 fflush(stdout);
752
753 tcgetattr(0, &save);
754 tc = save;
755 tc.c_lflag &= ~(ICANON | ECHO);
756 tc.c_cc[VMIN] = 0;
757 tc.c_cc[VTIME] = 0;
758 tcsetattr(0, TCSANOW, &tc);
759
760 poll(&stdin_poll, 1, -1);
761 c = getc(stdin);
762
763 tcsetattr(0, TCSAFLUSH, &save);
764 if (!key_mapped(c))
765 return;
766 }
767
Mike Galbraith923c42c2009-07-22 20:36:03 +0200768 switch (c) {
769 case 'd':
770 prompt_integer(&delay_secs, "Enter display delay");
Tim Blechmanndc799592009-10-17 18:08:29 +0200771 if (delay_secs < 1)
772 delay_secs = 1;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200773 break;
774 case 'e':
775 prompt_integer(&print_entries, "Enter display entries (lines)");
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200776 if (print_entries == 0) {
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200777 sig_winch_handler(SIGWINCH);
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200778 signal(SIGWINCH, sig_winch_handler);
779 } else
780 signal(SIGWINCH, SIG_DFL);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200781 break;
782 case 'E':
783 if (nr_counters > 1) {
784 int i;
785
786 fprintf(stderr, "\nAvailable events:");
787 for (i = 0; i < nr_counters; i++)
788 fprintf(stderr, "\n\t%d %s", i, event_name(i));
789
790 prompt_integer(&sym_counter, "Enter details event counter");
791
792 if (sym_counter >= nr_counters) {
793 fprintf(stderr, "Sorry, no such event, using %s.\n", event_name(0));
794 sym_counter = 0;
795 sleep(1);
796 }
797 } else sym_counter = 0;
798 break;
799 case 'f':
800 prompt_integer(&count_filter, "Enter display event count filter");
801 break;
802 case 'F':
803 prompt_percent(&sym_pcnt_filter, "Enter details display event filter (percent)");
804 break;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200805 case 'K':
806 hide_kernel_symbols = !hide_kernel_symbols;
807 break;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200808 case 'q':
809 case 'Q':
810 printf("exiting.\n");
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200811 if (dump_symtab)
812 dsos__fprintf(stderr);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200813 exit(0);
814 case 's':
815 prompt_symbol(&sym_filter_entry, "Enter details symbol");
816 break;
817 case 'S':
818 if (!sym_filter_entry)
819 break;
820 else {
821 struct sym_entry *syme = sym_filter_entry;
822
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200823 pthread_mutex_lock(&syme->src->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200824 sym_filter_entry = NULL;
825 __zero_source_counters(syme);
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200826 pthread_mutex_unlock(&syme->src->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200827 }
828 break;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200829 case 'U':
830 hide_user_symbols = !hide_user_symbols;
831 break;
Mike Galbraith46ab9762009-07-24 10:09:50 +0200832 case 'w':
833 display_weighted = ~display_weighted;
834 break;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200835 case 'z':
836 zero = ~zero;
837 break;
Ingo Molnar83a09442009-08-15 12:26:57 +0200838 default:
839 break;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200840 }
841}
842
Ingo Molnarf37a2912009-07-01 12:37:06 +0200843static void *display_thread(void *arg __used)
Ingo Molnar07800602009-04-20 15:00:56 +0200844{
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200845 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
Mike Galbraith923c42c2009-07-22 20:36:03 +0200846 struct termios tc, save;
847 int delay_msecs, c;
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200848
Mike Galbraith923c42c2009-07-22 20:36:03 +0200849 tcgetattr(0, &save);
850 tc = save;
851 tc.c_lflag &= ~(ICANON | ECHO);
852 tc.c_cc[VMIN] = 0;
853 tc.c_cc[VTIME] = 0;
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200854
Mike Galbraith923c42c2009-07-22 20:36:03 +0200855repeat:
856 delay_msecs = delay_secs * 1000;
857 tcsetattr(0, TCSANOW, &tc);
858 /* trash return*/
859 getc(stdin);
Ingo Molnar07800602009-04-20 15:00:56 +0200860
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200861 do {
Ingo Molnar07800602009-04-20 15:00:56 +0200862 print_sym_table();
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200863 } while (!poll(&stdin_poll, 1, delay_msecs) == 1);
864
Mike Galbraith923c42c2009-07-22 20:36:03 +0200865 c = getc(stdin);
866 tcsetattr(0, TCSAFLUSH, &save);
867
868 handle_keypress(c);
869 goto repeat;
Ingo Molnar07800602009-04-20 15:00:56 +0200870
871 return NULL;
872}
873
Anton Blanchard2ab52082009-07-01 09:00:46 +1000874/* Tag samples to be skipped. */
Ingo Molnarf37a2912009-07-01 12:37:06 +0200875static const char *skip_symbols[] = {
Anton Blanchard2ab52082009-07-01 09:00:46 +1000876 "default_idle",
877 "cpu_idle",
878 "enter_idle",
879 "exit_idle",
880 "mwait_idle",
Arnaldo Carvalho de Melo59b90052009-07-26 19:06:19 -0300881 "mwait_idle_with_hints",
Arnaldo Carvalho de Melo83572752009-09-25 15:02:39 -0700882 "poll_idle",
Anton Blanchard3a3393e2009-07-01 09:00:47 +1000883 "ppc64_runlatch_off",
884 "pseries_dedicated_idle_sleep",
Anton Blanchard2ab52082009-07-01 09:00:46 +1000885 NULL
886};
887
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300888static int symbol_filter(struct map *map, struct symbol *sym)
Ingo Molnar07800602009-04-20 15:00:56 +0200889{
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300890 struct sym_entry *syme;
891 const char *name = sym->name;
Anton Blanchard2ab52082009-07-01 09:00:46 +1000892 int i;
Ingo Molnar07800602009-04-20 15:00:56 +0200893
Anton Blanchard3a3393e2009-07-01 09:00:47 +1000894 /*
895 * ppc64 uses function descriptors and appends a '.' to the
896 * start of every instruction address. Remove it.
897 */
898 if (name[0] == '.')
899 name++;
900
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300901 if (!strcmp(name, "_text") ||
902 !strcmp(name, "_etext") ||
903 !strcmp(name, "_sinittext") ||
904 !strncmp("init_module", name, 11) ||
905 !strncmp("cleanup_module", name, 14) ||
906 strstr(name, "_text_start") ||
907 strstr(name, "_text_end"))
Ingo Molnar07800602009-04-20 15:00:56 +0200908 return 1;
909
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200910 syme = symbol__priv(sym);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300911 syme->map = map;
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200912 syme->src = NULL;
Kirill Smelkov6cff0e82010-02-03 16:52:08 -0200913
914 if (!sym_filter_entry && sym_filter && !strcmp(name, sym_filter)) {
915 /* schedule initial sym_filter_entry setup */
916 sym_filter_entry_sched = syme;
917 sym_filter = NULL;
918 }
Mike Galbraith923c42c2009-07-22 20:36:03 +0200919
Anton Blanchard2ab52082009-07-01 09:00:46 +1000920 for (i = 0; skip_symbols[i]; i++) {
921 if (!strcmp(skip_symbols[i], name)) {
922 syme->skip = 1;
923 break;
924 }
925 }
Ingo Molnar07800602009-04-20 15:00:56 +0200926
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200927 if (!syme->skip)
928 syme->name_len = strlen(sym->name);
929
Ingo Molnar07800602009-04-20 15:00:56 +0200930 return 0;
931}
932
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200933static void event__process_sample(const event_t *self,
934 struct perf_session *session, int counter)
Ingo Molnar07800602009-04-20 15:00:56 +0200935{
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200936 u64 ip = self->ip.ip;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200937 struct sym_entry *syme;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200938 struct addr_location al;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200939 u8 origin = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Ingo Molnar07800602009-04-20 15:00:56 +0200940
Arnaldo Carvalho de Melo24bfef02010-01-21 13:04:43 -0200941 ++samples;
942
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200943 switch (origin) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200944 case PERF_RECORD_MISC_USER:
Arnaldo Carvalho de Melo24bfef02010-01-21 13:04:43 -0200945 ++userspace_samples;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200946 if (hide_user_symbols)
947 return;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200948 break;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200949 case PERF_RECORD_MISC_KERNEL:
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200950 if (hide_kernel_symbols)
951 return;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200952 break;
953 default:
Ingo Molnar07800602009-04-20 15:00:56 +0200954 return;
955 }
956
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200957 if (event__preprocess_sample(self, session, &al, symbol_filter) < 0 ||
Arnaldo Carvalho de Melo72b8fa12010-01-27 21:05:49 -0200958 al.filtered)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200959 return;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200960
Arnaldo Carvalho de Melo72b8fa12010-01-27 21:05:49 -0200961 if (al.sym == NULL) {
962 /*
963 * As we do lazy loading of symtabs we only will know if the
964 * specified vmlinux file is invalid when we actually have a
965 * hit in kernel space and then try to load it. So if we get
966 * here and there are _no_ symbols in the DSO backing the
967 * kernel map, bail out.
968 *
969 * We may never get here, for instance, if we use -K/
970 * --hide-kernel-symbols, even if the user specifies an
971 * invalid --vmlinux ;-)
972 */
973 if (al.map == session->vmlinux_maps[MAP__FUNCTION] &&
974 RB_EMPTY_ROOT(&al.map->dso->symbols[MAP__FUNCTION])) {
975 pr_err("The %s file can't be used\n",
976 symbol_conf.vmlinux_name);
977 exit(1);
978 }
979
980 return;
981 }
982
Kirill Smelkov6cff0e82010-02-03 16:52:08 -0200983 /* let's see, whether we need to install initial sym_filter_entry */
984 if (sym_filter_entry_sched) {
985 sym_filter_entry = sym_filter_entry_sched;
986 sym_filter_entry_sched = NULL;
987 parse_source(sym_filter_entry);
988 }
989
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200990 syme = symbol__priv(al.sym);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200991 if (!syme->skip) {
992 syme->count[counter]++;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200993 syme->origin = origin;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200994 record_precise_ip(syme, counter, ip);
995 pthread_mutex_lock(&active_symbols_lock);
996 if (list_empty(&syme->node) || !syme->node.next)
997 __list_insert_active_sym(syme);
998 pthread_mutex_unlock(&active_symbols_lock);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200999 }
1000}
1001
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001002static int event__process(event_t *event, struct perf_session *session)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001003{
1004 switch (event->header.type) {
1005 case PERF_RECORD_COMM:
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001006 event__process_comm(event, session);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001007 break;
1008 case PERF_RECORD_MMAP:
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001009 event__process_mmap(event, session);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001010 break;
Arnaldo Carvalho de Melo0f35cd42010-01-21 13:04:44 -02001011 case PERF_RECORD_FORK:
1012 case PERF_RECORD_EXIT:
1013 event__process_task(event, session);
1014 break;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001015 default:
1016 break;
1017 }
1018
1019 return 0;
Ingo Molnar07800602009-04-20 15:00:56 +02001020}
1021
Ingo Molnar07800602009-04-20 15:00:56 +02001022struct mmap_data {
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001023 int counter;
1024 void *base;
Ingo Molnarf37a2912009-07-01 12:37:06 +02001025 int mask;
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001026 unsigned int prev;
Ingo Molnar07800602009-04-20 15:00:56 +02001027};
1028
1029static unsigned int mmap_read_head(struct mmap_data *md)
1030{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001031 struct perf_event_mmap_page *pc = md->base;
Ingo Molnar07800602009-04-20 15:00:56 +02001032 int head;
1033
1034 head = pc->data_head;
1035 rmb();
1036
1037 return head;
1038}
1039
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001040static void perf_session__mmap_read_counter(struct perf_session *self,
1041 struct mmap_data *md)
Ingo Molnar07800602009-04-20 15:00:56 +02001042{
1043 unsigned int head = mmap_read_head(md);
1044 unsigned int old = md->prev;
1045 unsigned char *data = md->base + page_size;
1046 int diff;
1047
Ingo Molnar07800602009-04-20 15:00:56 +02001048 /*
1049 * If we're further behind than half the buffer, there's a chance
Ingo Molnar2debbc82009-06-05 14:29:10 +02001050 * the writer will bite our tail and mess up the samples under us.
Ingo Molnar07800602009-04-20 15:00:56 +02001051 *
1052 * If we somehow ended up ahead of the head, we got messed up.
1053 *
1054 * In either case, truncate and restart at head.
1055 */
1056 diff = head - old;
1057 if (diff > md->mask / 2 || diff < 0) {
Mike Galbraithf4f0b412009-10-13 14:57:20 +02001058 fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
Ingo Molnar07800602009-04-20 15:00:56 +02001059
1060 /*
1061 * head points to a known good entry, start there.
1062 */
1063 old = head;
1064 }
1065
Ingo Molnar07800602009-04-20 15:00:56 +02001066 for (; old != head;) {
Ingo Molnar07800602009-04-20 15:00:56 +02001067 event_t *event = (event_t *)&data[old & md->mask];
1068
1069 event_t event_copy;
1070
Ingo Molnar6f06ccb2009-04-20 15:22:22 +02001071 size_t size = event->header.size;
Ingo Molnar07800602009-04-20 15:00:56 +02001072
1073 /*
1074 * Event straddles the mmap boundary -- header should always
1075 * be inside due to u64 alignment of output.
1076 */
1077 if ((old & md->mask) + size != ((old + size) & md->mask)) {
1078 unsigned int offset = old;
1079 unsigned int len = min(sizeof(*event), size), cpy;
1080 void *dst = &event_copy;
1081
1082 do {
1083 cpy = min(md->mask + 1 - (offset & md->mask), len);
1084 memcpy(dst, &data[offset & md->mask], cpy);
1085 offset += cpy;
1086 dst += cpy;
1087 len -= cpy;
1088 } while (len);
1089
1090 event = &event_copy;
1091 }
1092
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001093 if (event->header.type == PERF_RECORD_SAMPLE)
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001094 event__process_sample(event, self, md->counter);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001095 else
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001096 event__process(event, self);
Ingo Molnar07800602009-04-20 15:00:56 +02001097 old += size;
Ingo Molnar07800602009-04-20 15:00:56 +02001098 }
1099
1100 md->prev = old;
1101}
1102
Mike Galbraithc2990a22009-05-24 08:35:49 +02001103static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
1104static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
1105
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001106static void perf_session__mmap_read(struct perf_session *self)
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001107{
1108 int i, counter;
1109
1110 for (i = 0; i < nr_cpus; i++) {
1111 for (counter = 0; counter < nr_counters; counter++)
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001112 perf_session__mmap_read_counter(self, &mmap_array[i][counter]);
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001113 }
1114}
1115
Ingo Molnar716c69f2009-06-07 17:31:52 +02001116int nr_poll;
1117int group_fd;
1118
1119static void start_counter(int i, int counter)
Ingo Molnar07800602009-04-20 15:00:56 +02001120{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001121 struct perf_event_attr *attr;
Mike Galbraith0fdc7e62009-07-21 10:30:36 +02001122 int cpu;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001123
1124 cpu = profile_cpu;
1125 if (target_pid == -1 && profile_cpu == -1)
1126 cpu = i;
1127
1128 attr = attrs + counter;
1129
1130 attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +02001131
1132 if (freq) {
1133 attr->sample_type |= PERF_SAMPLE_PERIOD;
1134 attr->freq = 1;
1135 attr->sample_freq = freq;
1136 }
1137
Mike Galbraith0fdc7e62009-07-21 10:30:36 +02001138 attr->inherit = (cpu < 0) && inherit;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001139 attr->mmap = 1;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001140
1141try_again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001142 fd[i][counter] = sys_perf_event_open(attr, target_pid, cpu, group_fd, 0);
Ingo Molnar716c69f2009-06-07 17:31:52 +02001143
1144 if (fd[i][counter] < 0) {
1145 int err = errno;
1146
Pekka Enbergc10edee2009-11-08 18:01:06 +02001147 if (err == EPERM || err == EACCES)
Ingo Molnar3da297a2009-06-07 17:39:02 +02001148 die("No permission - are you root?\n");
Ingo Molnar716c69f2009-06-07 17:31:52 +02001149 /*
1150 * If it's cycles then fall back to hrtimer
1151 * based cpu-clock-tick sw counter, which
1152 * is always available even if no PMU support:
1153 */
1154 if (attr->type == PERF_TYPE_HARDWARE
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02001155 && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
Ingo Molnar716c69f2009-06-07 17:31:52 +02001156
Ingo Molnar3da297a2009-06-07 17:39:02 +02001157 if (verbose)
1158 warning(" ... trying to fall back to cpu-clock-ticks\n");
1159
Ingo Molnar716c69f2009-06-07 17:31:52 +02001160 attr->type = PERF_TYPE_SOFTWARE;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02001161 attr->config = PERF_COUNT_SW_CPU_CLOCK;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001162 goto try_again;
1163 }
Ingo Molnar30c806a2009-06-07 17:46:24 +02001164 printf("\n");
1165 error("perfcounter syscall returned with %d (%s)\n",
1166 fd[i][counter], strerror(err));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001167 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
Ingo Molnar716c69f2009-06-07 17:31:52 +02001168 exit(-1);
1169 }
1170 assert(fd[i][counter] >= 0);
1171 fcntl(fd[i][counter], F_SETFL, O_NONBLOCK);
1172
1173 /*
1174 * First counter acts as the group leader:
1175 */
1176 if (group && group_fd == -1)
1177 group_fd = fd[i][counter];
1178
1179 event_array[nr_poll].fd = fd[i][counter];
1180 event_array[nr_poll].events = POLLIN;
1181 nr_poll++;
1182
1183 mmap_array[i][counter].counter = counter;
1184 mmap_array[i][counter].prev = 0;
1185 mmap_array[i][counter].mask = mmap_pages*page_size - 1;
1186 mmap_array[i][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
1187 PROT_READ, MAP_SHARED, fd[i][counter], 0);
1188 if (mmap_array[i][counter].base == MAP_FAILED)
1189 die("failed to mmap with %d (%s)\n", errno, strerror(errno));
1190}
1191
1192static int __cmd_top(void)
1193{
1194 pthread_t thread;
1195 int i, counter;
Ingo Molnar07800602009-04-20 15:00:56 +02001196 int ret;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001197 /*
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001198 * FIXME: perf_session__new should allow passing a O_MMAP, so that all this
1199 * mmap reading, etc is encapsulated in it. Use O_WRONLY for now.
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001200 */
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02001201 struct perf_session *session = perf_session__new(NULL, O_WRONLY, false);
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001202 if (session == NULL)
1203 return -ENOMEM;
Ingo Molnar07800602009-04-20 15:00:56 +02001204
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001205 if (target_pid != -1)
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001206 event__synthesize_thread(target_pid, event__process, session);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001207 else
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001208 event__synthesize_threads(event__process, session);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001209
Ingo Molnar07800602009-04-20 15:00:56 +02001210 for (i = 0; i < nr_cpus; i++) {
1211 group_fd = -1;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001212 for (counter = 0; counter < nr_counters; counter++)
1213 start_counter(i, counter);
Ingo Molnar07800602009-04-20 15:00:56 +02001214 }
1215
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001216 /* Wait for a minimal set of events before starting the snapshot */
1217 poll(event_array, nr_poll, 100);
1218
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001219 perf_session__mmap_read(session);
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001220
Ingo Molnar07800602009-04-20 15:00:56 +02001221 if (pthread_create(&thread, NULL, display_thread, NULL)) {
1222 printf("Could not create display thread.\n");
1223 exit(-1);
1224 }
1225
1226 if (realtime_prio) {
1227 struct sched_param param;
1228
1229 param.sched_priority = realtime_prio;
1230 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
1231 printf("Could not set realtime priority.\n");
1232 exit(-1);
1233 }
1234 }
1235
1236 while (1) {
Ingo Molnar2debbc82009-06-05 14:29:10 +02001237 int hits = samples;
Ingo Molnar07800602009-04-20 15:00:56 +02001238
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001239 perf_session__mmap_read(session);
Ingo Molnar07800602009-04-20 15:00:56 +02001240
Ingo Molnar2debbc82009-06-05 14:29:10 +02001241 if (hits == samples)
Ingo Molnar07800602009-04-20 15:00:56 +02001242 ret = poll(event_array, nr_poll, 100);
1243 }
1244
1245 return 0;
1246}
Ingo Molnarb456bae2009-05-26 09:17:18 +02001247
1248static const char * const top_usage[] = {
1249 "perf top [<options>]",
1250 NULL
1251};
1252
Ingo Molnarb456bae2009-05-26 09:17:18 +02001253static const struct option options[] = {
1254 OPT_CALLBACK('e', "event", NULL, "event",
Thomas Gleixner86847b62009-06-06 12:24:17 +02001255 "event selector. use 'perf list' to list available events",
1256 parse_events),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001257 OPT_INTEGER('c', "count", &default_interval,
1258 "event period to sample"),
1259 OPT_INTEGER('p', "pid", &target_pid,
1260 "profile events on existing pid"),
1261 OPT_BOOLEAN('a', "all-cpus", &system_wide,
1262 "system-wide collection from all CPUs"),
1263 OPT_INTEGER('C', "CPU", &profile_cpu,
1264 "CPU to profile on"),
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02001265 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1266 "file", "vmlinux pathname"),
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -02001267 OPT_BOOLEAN('K', "hide_kernel_symbols", &hide_kernel_symbols,
1268 "hide kernel symbols"),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001269 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
1270 "number of mmap data pages"),
1271 OPT_INTEGER('r', "realtime", &realtime_prio,
1272 "collect data with this RT SCHED_FIFO priority"),
Mike Galbraithdb20c002009-05-26 15:25:34 +02001273 OPT_INTEGER('d', "delay", &delay_secs,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001274 "number of seconds to delay between refreshes"),
1275 OPT_BOOLEAN('D', "dump-symtab", &dump_symtab,
1276 "dump the symbol table used for profiling"),
Ingo Molnar6e53cdf2009-06-04 08:53:05 +02001277 OPT_INTEGER('f', "count-filter", &count_filter,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001278 "only display functions with more events than this"),
1279 OPT_BOOLEAN('g', "group", &group,
1280 "put the counters into a counter group"),
Mike Galbraith0fdc7e62009-07-21 10:30:36 +02001281 OPT_BOOLEAN('i', "inherit", &inherit,
1282 "child tasks inherit counters"),
Mike Galbraith923c42c2009-07-22 20:36:03 +02001283 OPT_STRING('s', "sym-annotate", &sym_filter, "symbol name",
Kirill Smelkov6cff0e82010-02-03 16:52:08 -02001284 "symbol to annotate"),
Anton Blanchard1f208ea2009-07-01 09:00:44 +10001285 OPT_BOOLEAN('z', "zero", &zero,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001286 "zero history across updates"),
Ingo Molnar6e53cdf2009-06-04 08:53:05 +02001287 OPT_INTEGER('F', "freq", &freq,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001288 "profile at this frequency"),
Ingo Molnar6e53cdf2009-06-04 08:53:05 +02001289 OPT_INTEGER('E', "entries", &print_entries,
1290 "display this many functions"),
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -02001291 OPT_BOOLEAN('U', "hide_user_symbols", &hide_user_symbols,
1292 "hide user symbols"),
Ingo Molnar3da297a2009-06-07 17:39:02 +02001293 OPT_BOOLEAN('v', "verbose", &verbose,
1294 "be more verbose (show counter open errors, etc)"),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001295 OPT_END()
1296};
1297
Ingo Molnarf37a2912009-07-01 12:37:06 +02001298int cmd_top(int argc, const char **argv, const char *prefix __used)
Ingo Molnarb456bae2009-05-26 09:17:18 +02001299{
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02001300 int counter;
Ingo Molnarb456bae2009-05-26 09:17:18 +02001301
1302 page_size = sysconf(_SC_PAGE_SIZE);
1303
Ingo Molnarb456bae2009-05-26 09:17:18 +02001304 argc = parse_options(argc, argv, options, top_usage, 0);
1305 if (argc)
1306 usage_with_options(top_usage, options);
1307
Ingo Molnarb456bae2009-05-26 09:17:18 +02001308 /* CPU and PID are mutually exclusive */
1309 if (target_pid != -1 && profile_cpu != -1) {
1310 printf("WARNING: PID switch overriding CPU\n");
1311 sleep(1);
1312 profile_cpu = -1;
1313 }
1314
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001315 if (!nr_counters)
Ingo Molnarb456bae2009-05-26 09:17:18 +02001316 nr_counters = 1;
Ingo Molnarb456bae2009-05-26 09:17:18 +02001317
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02001318 symbol_conf.priv_size = (sizeof(struct sym_entry) +
1319 (nr_counters + 1) * sizeof(unsigned long));
Kirill Smelkov6cff0e82010-02-03 16:52:08 -02001320
1321 symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL);
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02001322 if (symbol__init() < 0)
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02001323 return -1;
Arnaldo Carvalho de Melo5a8e5a32009-11-17 18:38:01 -02001324
Frederic Weisbecker2f335a02009-06-05 19:31:01 +02001325 if (delay_secs < 1)
1326 delay_secs = 1;
1327
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +02001328 /*
1329 * User specified count overrides default frequency.
1330 */
1331 if (default_interval)
1332 freq = 0;
1333 else if (freq) {
1334 default_interval = freq;
1335 } else {
1336 fprintf(stderr, "frequency and count are zero, aborting\n");
1337 exit(EXIT_FAILURE);
1338 }
1339
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001340 /*
1341 * Fill in the ones not specifically initialized via -c:
1342 */
Ingo Molnarb456bae2009-05-26 09:17:18 +02001343 for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001344 if (attrs[counter].sample_period)
Ingo Molnarb456bae2009-05-26 09:17:18 +02001345 continue;
1346
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001347 attrs[counter].sample_period = default_interval;
Ingo Molnarb456bae2009-05-26 09:17:18 +02001348 }
1349
1350 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
1351 assert(nr_cpus <= MAX_NR_CPUS);
1352 assert(nr_cpus >= 0);
1353
1354 if (target_pid != -1 || profile_cpu != -1)
1355 nr_cpus = 1;
1356
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -02001357 get_term_dimensions(&winsize);
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -02001358 if (print_entries == 0) {
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -02001359 update_print_entries(&winsize);
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -02001360 signal(SIGWINCH, sig_winch_handler);
1361 }
1362
Ingo Molnarb456bae2009-05-26 09:17:18 +02001363 return __cmd_top();
1364}