blob: 57e232f13bc11cec0feb8d8feb7a100a46dbe007 [file] [log] [blame]
Ingo Molnar07800602009-04-20 15:00:56 +02001/*
Ingo Molnarbf9e1872009-06-02 23:37:05 +02002 * builtin-top.c
3 *
4 * Builtin top command: Display a continuously updated profile of
5 * any workload, CPU or specific PID.
6 *
7 * Copyright (C) 2008, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
8 *
9 * Improvements and fixes by:
10 *
11 * Arjan van de Ven <arjan@linux.intel.com>
12 * Yanmin Zhang <yanmin.zhang@intel.com>
13 * Wu Fengguang <fengguang.wu@intel.com>
14 * Mike Galbraith <efault@gmx.de>
15 * Paul Mackerras <paulus@samba.org>
16 *
17 * Released under the GPL v2. (and only v2, not any later version)
Ingo Molnar07800602009-04-20 15:00:56 +020018 */
Ingo Molnarbf9e1872009-06-02 23:37:05 +020019#include "builtin.h"
Ingo Molnar07800602009-04-20 15:00:56 +020020
Peter Zijlstra1a482f32009-05-23 18:28:58 +020021#include "perf.h"
Ingo Molnarbf9e1872009-06-02 23:37:05 +020022
Ingo Molnar8fc03212009-06-04 15:19:47 +020023#include "util/color.h"
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -020024#include "util/session.h"
25#include "util/symbol.h"
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030026#include "util/thread.h"
Ingo Molnar148be2c2009-04-27 08:02:14 +020027#include "util/util.h"
Arnaldo Carvalho de Melo43cbcd82009-07-01 12:28:37 -030028#include <linux/rbtree.h>
Ingo Molnarb456bae2009-05-26 09:17:18 +020029#include "util/parse-options.h"
30#include "util/parse-events.h"
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110031#include "util/cpumap.h"
Ingo Molnar07800602009-04-20 15:00:56 +020032
Frederic Weisbecker8f288272009-08-16 22:05:48 +020033#include "util/debug.h"
34
Ingo Molnar07800602009-04-20 15:00:56 +020035#include <assert.h>
36#include <fcntl.h>
Ingo Molnar0e9b20b2009-05-26 09:17:18 +020037
Ingo Molnar07800602009-04-20 15:00:56 +020038#include <stdio.h>
Mike Galbraith923c42c2009-07-22 20:36:03 +020039#include <termios.h>
40#include <unistd.h>
Ingo Molnar0e9b20b2009-05-26 09:17:18 +020041
Ingo Molnar07800602009-04-20 15:00:56 +020042#include <errno.h>
Ingo Molnar07800602009-04-20 15:00:56 +020043#include <time.h>
44#include <sched.h>
45#include <pthread.h>
46
47#include <sys/syscall.h>
48#include <sys/ioctl.h>
49#include <sys/poll.h>
50#include <sys/prctl.h>
51#include <sys/wait.h>
52#include <sys/uio.h>
53#include <sys/mman.h>
54
55#include <linux/unistd.h>
56#include <linux/types.h>
57
Ingo Molnara21ca2c2009-06-06 09:58:57 +020058static int fd[MAX_NR_CPUS][MAX_COUNTERS];
59
Ingo Molnar42e59d72009-10-06 15:14:21 +020060static int system_wide = 0;
Ingo Molnar07800602009-04-20 15:00:56 +020061
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +020062static int default_interval = 0;
Ingo Molnar07800602009-04-20 15:00:56 +020063
Ingo Molnar42e59d72009-10-06 15:14:21 +020064static int count_filter = 5;
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -020065static int print_entries;
Ingo Molnar07800602009-04-20 15:00:56 +020066
Ingo Molnar42e59d72009-10-06 15:14:21 +020067static int target_pid = -1;
68static int inherit = 0;
69static int profile_cpu = -1;
70static int nr_cpus = 0;
71static unsigned int realtime_prio = 0;
72static int group = 0;
Ingo Molnar07800602009-04-20 15:00:56 +020073static unsigned int page_size;
Ingo Molnar42e59d72009-10-06 15:14:21 +020074static unsigned int mmap_pages = 16;
75static int freq = 1000; /* 1 KHz */
Ingo Molnar07800602009-04-20 15:00:56 +020076
Ingo Molnar42e59d72009-10-06 15:14:21 +020077static int delay_secs = 2;
78static int zero = 0;
79static int dump_symtab = 0;
Ingo Molnar07800602009-04-20 15:00:56 +020080
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -020081static bool hide_kernel_symbols = false;
82static bool hide_user_symbols = false;
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -020083static struct winsize winsize;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -020084
Ingo Molnar07800602009-04-20 15:00:56 +020085/*
Mike Galbraith923c42c2009-07-22 20:36:03 +020086 * Source
87 */
88
89struct source_line {
90 u64 eip;
91 unsigned long count[MAX_COUNTERS];
92 char *line;
93 struct source_line *next;
94};
95
Ingo Molnar42e59d72009-10-06 15:14:21 +020096static char *sym_filter = NULL;
97struct sym_entry *sym_filter_entry = NULL;
Kirill Smelkov6cff0e82010-02-03 16:52:08 -020098struct sym_entry *sym_filter_entry_sched = NULL;
Ingo Molnar42e59d72009-10-06 15:14:21 +020099static int sym_pcnt_filter = 5;
100static int sym_counter = 0;
101static int display_weighted = -1;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200102
103/*
Ingo Molnar07800602009-04-20 15:00:56 +0200104 * Symbols
105 */
106
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200107struct sym_entry_source {
108 struct source_line *source;
109 struct source_line *lines;
110 struct source_line **lines_tail;
111 pthread_mutex_t lock;
112};
113
Ingo Molnar07800602009-04-20 15:00:56 +0200114struct sym_entry {
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300115 struct rb_node rb_node;
116 struct list_head node;
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300117 unsigned long snap_count;
118 double weight;
Ingo Molnar07800602009-04-20 15:00:56 +0200119 int skip;
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200120 u16 name_len;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200121 u8 origin;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300122 struct map *map;
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200123 struct sym_entry_source *src;
Arnaldo Carvalho de Melo5a8e5a32009-11-17 18:38:01 -0200124 unsigned long count[0];
Ingo Molnar07800602009-04-20 15:00:56 +0200125};
126
Mike Galbraith923c42c2009-07-22 20:36:03 +0200127/*
128 * Source functions
129 */
130
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200131static inline struct symbol *sym_entry__symbol(struct sym_entry *self)
132{
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -0200133 return ((void *)self) + symbol_conf.priv_size;
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200134}
135
Arnaldo Carvalho de Melo895f0ed2010-03-11 20:12:41 -0300136void get_term_dimensions(struct winsize *ws)
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200137{
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200138 char *s = getenv("LINES");
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200139
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200140 if (s != NULL) {
141 ws->ws_row = atoi(s);
142 s = getenv("COLUMNS");
143 if (s != NULL) {
144 ws->ws_col = atoi(s);
145 if (ws->ws_row && ws->ws_col)
146 return;
147 }
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200148 }
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200149#ifdef TIOCGWINSZ
150 if (ioctl(1, TIOCGWINSZ, ws) == 0 &&
151 ws->ws_row && ws->ws_col)
152 return;
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200153#endif
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200154 ws->ws_row = 25;
155 ws->ws_col = 80;
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200156}
157
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200158static void update_print_entries(struct winsize *ws)
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200159{
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200160 print_entries = ws->ws_row;
161
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200162 if (print_entries > 9)
163 print_entries -= 9;
164}
165
166static void sig_winch_handler(int sig __used)
167{
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200168 get_term_dimensions(&winsize);
169 update_print_entries(&winsize);
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200170}
171
Mike Galbraith923c42c2009-07-22 20:36:03 +0200172static void parse_source(struct sym_entry *syme)
173{
174 struct symbol *sym;
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200175 struct sym_entry_source *source;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300176 struct map *map;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200177 FILE *file;
Ingo Molnar83a09442009-08-15 12:26:57 +0200178 char command[PATH_MAX*2];
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300179 const char *path;
180 u64 len;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200181
182 if (!syme)
183 return;
184
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200185 if (syme->src == NULL) {
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200186 syme->src = zalloc(sizeof(*source));
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200187 if (syme->src == NULL)
188 return;
189 pthread_mutex_init(&syme->src->lock, NULL);
190 }
191
192 source = syme->src;
193
194 if (source->lines) {
195 pthread_mutex_lock(&source->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200196 goto out_assign;
197 }
198
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200199 sym = sym_entry__symbol(syme);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300200 map = syme->map;
201 path = map->dso->long_name;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200202
Mike Galbraith923c42c2009-07-22 20:36:03 +0200203 len = sym->end - sym->start;
204
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300205 sprintf(command,
Arnaldo Carvalho de Melo5f485362010-02-07 11:46:16 -0200206 "objdump --start-address=%#0*Lx --stop-address=%#0*Lx -dS %s",
207 BITS_PER_LONG / 4, map__rip_2objdump(map, sym->start),
208 BITS_PER_LONG / 4, map__rip_2objdump(map, sym->end), path);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200209
210 file = popen(command, "r");
211 if (!file)
212 return;
213
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200214 pthread_mutex_lock(&source->lock);
215 source->lines_tail = &source->lines;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200216 while (!feof(file)) {
217 struct source_line *src;
218 size_t dummy = 0;
Kirill Smelkovee11b902010-02-07 11:46:15 -0200219 char *c, *sep;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200220
221 src = malloc(sizeof(struct source_line));
222 assert(src != NULL);
223 memset(src, 0, sizeof(struct source_line));
224
225 if (getline(&src->line, &dummy, file) < 0)
226 break;
227 if (!src->line)
228 break;
229
230 c = strchr(src->line, '\n');
231 if (c)
232 *c = 0;
233
234 src->next = NULL;
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200235 *source->lines_tail = src;
236 source->lines_tail = &src->next;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200237
Kirill Smelkovee11b902010-02-07 11:46:15 -0200238 src->eip = strtoull(src->line, &sep, 16);
239 if (*sep == ':')
240 src->eip = map__objdump_2ip(map, src->eip);
241 else /* this line has no ip info (e.g. source line) */
242 src->eip = 0;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200243 }
244 pclose(file);
245out_assign:
246 sym_filter_entry = syme;
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200247 pthread_mutex_unlock(&source->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200248}
249
250static void __zero_source_counters(struct sym_entry *syme)
251{
252 int i;
253 struct source_line *line;
254
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200255 line = syme->src->lines;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200256 while (line) {
257 for (i = 0; i < nr_counters; i++)
258 line->count[i] = 0;
259 line = line->next;
260 }
261}
262
263static void record_precise_ip(struct sym_entry *syme, int counter, u64 ip)
264{
265 struct source_line *line;
266
267 if (syme != sym_filter_entry)
268 return;
269
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200270 if (pthread_mutex_trylock(&syme->src->lock))
Mike Galbraith923c42c2009-07-22 20:36:03 +0200271 return;
272
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200273 if (syme->src == NULL || syme->src->source == NULL)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200274 goto out_unlock;
275
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200276 for (line = syme->src->lines; line; line = line->next) {
Kirill Smelkovee11b902010-02-07 11:46:15 -0200277 /* skip lines without IP info */
278 if (line->eip == 0)
279 continue;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200280 if (line->eip == ip) {
281 line->count[counter]++;
282 break;
283 }
284 if (line->eip > ip)
285 break;
286 }
287out_unlock:
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200288 pthread_mutex_unlock(&syme->src->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200289}
290
Arnaldo Carvalho de Meloc7ad21a2010-02-22 16:14:22 -0300291#define PATTERN_LEN (BITS_PER_LONG / 4 + 2)
292
Mike Galbraith923c42c2009-07-22 20:36:03 +0200293static void lookup_sym_source(struct sym_entry *syme)
294{
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200295 struct symbol *symbol = sym_entry__symbol(syme);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200296 struct source_line *line;
Arnaldo Carvalho de Meloc7ad21a2010-02-22 16:14:22 -0300297 char pattern[PATTERN_LEN + 1];
Mike Galbraith923c42c2009-07-22 20:36:03 +0200298
Arnaldo Carvalho de Melo5f485362010-02-07 11:46:16 -0200299 sprintf(pattern, "%0*Lx <", BITS_PER_LONG / 4,
300 map__rip_2objdump(syme->map, symbol->start));
Mike Galbraith923c42c2009-07-22 20:36:03 +0200301
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200302 pthread_mutex_lock(&syme->src->lock);
303 for (line = syme->src->lines; line; line = line->next) {
Arnaldo Carvalho de Meloc7ad21a2010-02-22 16:14:22 -0300304 if (memcmp(line->line, pattern, PATTERN_LEN) == 0) {
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200305 syme->src->source = line;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200306 break;
307 }
308 }
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200309 pthread_mutex_unlock(&syme->src->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200310}
311
312static void show_lines(struct source_line *queue, int count, int total)
313{
314 int i;
315 struct source_line *line;
316
317 line = queue;
318 for (i = 0; i < count; i++) {
319 float pcnt = 100.0*(float)line->count[sym_counter]/(float)total;
320
321 printf("%8li %4.1f%%\t%s\n", line->count[sym_counter], pcnt, line->line);
322 line = line->next;
323 }
324}
325
326#define TRACE_COUNT 3
327
328static void show_details(struct sym_entry *syme)
329{
330 struct symbol *symbol;
331 struct source_line *line;
332 struct source_line *line_queue = NULL;
333 int displayed = 0;
334 int line_queue_count = 0, total = 0, more = 0;
335
336 if (!syme)
337 return;
338
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200339 if (!syme->src->source)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200340 lookup_sym_source(syme);
341
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200342 if (!syme->src->source)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200343 return;
344
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200345 symbol = sym_entry__symbol(syme);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200346 printf("Showing %s for %s\n", event_name(sym_counter), symbol->name);
347 printf(" Events Pcnt (>=%d%%)\n", sym_pcnt_filter);
348
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200349 pthread_mutex_lock(&syme->src->lock);
350 line = syme->src->source;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200351 while (line) {
352 total += line->count[sym_counter];
353 line = line->next;
354 }
355
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200356 line = syme->src->source;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200357 while (line) {
358 float pcnt = 0.0;
359
360 if (!line_queue_count)
361 line_queue = line;
362 line_queue_count++;
363
364 if (line->count[sym_counter])
365 pcnt = 100.0 * line->count[sym_counter] / (float)total;
366 if (pcnt >= (float)sym_pcnt_filter) {
367 if (displayed <= print_entries)
368 show_lines(line_queue, line_queue_count, total);
369 else more++;
370 displayed += line_queue_count;
371 line_queue_count = 0;
372 line_queue = NULL;
373 } else if (line_queue_count > TRACE_COUNT) {
374 line_queue = line_queue->next;
375 line_queue_count--;
376 }
377
378 line->count[sym_counter] = zero ? 0 : line->count[sym_counter] * 7 / 8;
379 line = line->next;
380 }
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200381 pthread_mutex_unlock(&syme->src->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200382 if (more)
383 printf("%d lines not displayed, maybe increase display entries [e]\n", more);
384}
Ingo Molnar07800602009-04-20 15:00:56 +0200385
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300386/*
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200387 * Symbols will be added here in event__process_sample and will get out
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300388 * after decayed.
389 */
390static LIST_HEAD(active_symbols);
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300391static pthread_mutex_t active_symbols_lock = PTHREAD_MUTEX_INITIALIZER;
Ingo Molnar07800602009-04-20 15:00:56 +0200392
Ingo Molnar07800602009-04-20 15:00:56 +0200393/*
394 * Ordering weight: count-1 * count-2 * ... / count-n
395 */
396static double sym_weight(const struct sym_entry *sym)
397{
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300398 double weight = sym->snap_count;
Ingo Molnar07800602009-04-20 15:00:56 +0200399 int counter;
400
Mike Galbraith46ab9762009-07-24 10:09:50 +0200401 if (!display_weighted)
402 return weight;
403
Ingo Molnar07800602009-04-20 15:00:56 +0200404 for (counter = 1; counter < nr_counters-1; counter++)
405 weight *= sym->count[counter];
406
407 weight /= (sym->count[counter] + 1);
408
409 return weight;
410}
411
Ingo Molnar2debbc82009-06-05 14:29:10 +0200412static long samples;
413static long userspace_samples;
Peter Zijlstra1676b8a2010-03-04 14:19:36 +0100414static long exact_samples;
Ingo Molnar07800602009-04-20 15:00:56 +0200415static const char CONSOLE_CLEAR[] = "";
416
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300417static void __list_insert_active_sym(struct sym_entry *syme)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300418{
419 list_add(&syme->node, &active_symbols);
420}
421
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300422static void list_remove_active_sym(struct sym_entry *syme)
423{
424 pthread_mutex_lock(&active_symbols_lock);
425 list_del_init(&syme->node);
426 pthread_mutex_unlock(&active_symbols_lock);
427}
428
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300429static void rb_insert_active_sym(struct rb_root *tree, struct sym_entry *se)
430{
431 struct rb_node **p = &tree->rb_node;
432 struct rb_node *parent = NULL;
433 struct sym_entry *iter;
434
435 while (*p != NULL) {
436 parent = *p;
437 iter = rb_entry(parent, struct sym_entry, rb_node);
438
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300439 if (se->weight > iter->weight)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300440 p = &(*p)->rb_left;
441 else
442 p = &(*p)->rb_right;
443 }
444
445 rb_link_node(&se->rb_node, parent, p);
446 rb_insert_color(&se->rb_node, tree);
447}
Ingo Molnar07800602009-04-20 15:00:56 +0200448
449static void print_sym_table(void)
450{
Ingo Molnar233f0b92009-06-03 21:48:40 +0200451 int printed = 0, j;
Mike Galbraith46ab9762009-07-24 10:09:50 +0200452 int counter, snap = !display_weighted ? sym_counter : 0;
Ingo Molnar2debbc82009-06-05 14:29:10 +0200453 float samples_per_sec = samples/delay_secs;
454 float ksamples_per_sec = (samples-userspace_samples)/delay_secs;
Peter Zijlstra1676b8a2010-03-04 14:19:36 +0100455 float esamples_percent = (100.0*exact_samples)/samples;
Ingo Molnar2debbc82009-06-05 14:29:10 +0200456 float sum_ksamples = 0.0;
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300457 struct sym_entry *syme, *n;
458 struct rb_root tmp = RB_ROOT;
459 struct rb_node *nd;
Arnaldo Carvalho de Melo7cc017e2009-11-24 12:05:14 -0200460 int sym_width = 0, dso_width = 0, max_dso_width;
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200461 const int win_width = winsize.ws_col - 1;
Ingo Molnar07800602009-04-20 15:00:56 +0200462
Peter Zijlstra1676b8a2010-03-04 14:19:36 +0100463 samples = userspace_samples = exact_samples = 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200464
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300465 /* Sort the active symbols */
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300466 pthread_mutex_lock(&active_symbols_lock);
467 syme = list_entry(active_symbols.next, struct sym_entry, node);
468 pthread_mutex_unlock(&active_symbols_lock);
469
470 list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
Mike Galbraith46ab9762009-07-24 10:09:50 +0200471 syme->snap_count = syme->count[snap];
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300472 if (syme->snap_count != 0) {
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200473
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200474 if ((hide_user_symbols &&
475 syme->origin == PERF_RECORD_MISC_USER) ||
476 (hide_kernel_symbols &&
477 syme->origin == PERF_RECORD_MISC_KERNEL)) {
478 list_remove_active_sym(syme);
479 continue;
480 }
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300481 syme->weight = sym_weight(syme);
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300482 rb_insert_active_sym(&tmp, syme);
Ingo Molnar2debbc82009-06-05 14:29:10 +0200483 sum_ksamples += syme->snap_count;
Mike Galbraithd94b9432009-05-25 09:57:56 +0200484
485 for (j = 0; j < nr_counters; j++)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300486 syme->count[j] = zero ? 0 : syme->count[j] * 7 / 8;
487 } else
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300488 list_remove_active_sym(syme);
Mike Galbraithd94b9432009-05-25 09:57:56 +0200489 }
490
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200491 puts(CONSOLE_CLEAR);
Ingo Molnar07800602009-04-20 15:00:56 +0200492
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200493 printf("%-*.*s\n", win_width, win_width, graph_dotted_line);
Peter Zijlstra1676b8a2010-03-04 14:19:36 +0100494 printf( " PerfTop:%8.0f irqs/sec kernel:%4.1f%% exact: %4.1f%% [",
Ingo Molnar2debbc82009-06-05 14:29:10 +0200495 samples_per_sec,
Peter Zijlstra1676b8a2010-03-04 14:19:36 +0100496 100.0 - (100.0*((samples_per_sec-ksamples_per_sec)/samples_per_sec)),
497 esamples_percent);
Ingo Molnar07800602009-04-20 15:00:56 +0200498
Mike Galbraith46ab9762009-07-24 10:09:50 +0200499 if (nr_counters == 1 || !display_weighted) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000500 printf("%Ld", (u64)attrs[0].sample_period);
Ingo Molnarcf1f4572009-06-05 13:27:02 +0200501 if (freq)
502 printf("Hz ");
503 else
504 printf(" ");
505 }
Ingo Molnar07800602009-04-20 15:00:56 +0200506
Mike Galbraith46ab9762009-07-24 10:09:50 +0200507 if (!display_weighted)
508 printf("%s", event_name(sym_counter));
509 else for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnar07800602009-04-20 15:00:56 +0200510 if (counter)
511 printf("/");
512
513 printf("%s", event_name(counter));
514 }
515
516 printf( "], ");
517
Ingo Molnarb456bae2009-05-26 09:17:18 +0200518 if (target_pid != -1)
519 printf(" (target_pid: %d", target_pid);
Ingo Molnar07800602009-04-20 15:00:56 +0200520 else
521 printf(" (all");
522
523 if (profile_cpu != -1)
524 printf(", cpu: %d)\n", profile_cpu);
525 else {
Ingo Molnarb456bae2009-05-26 09:17:18 +0200526 if (target_pid != -1)
Ingo Molnar07800602009-04-20 15:00:56 +0200527 printf(")\n");
528 else
529 printf(", %d CPUs)\n", nr_cpus);
530 }
531
Arnaldo Carvalho de Melo1a105f72009-11-17 15:40:55 -0200532 printf("%-*.*s\n", win_width, win_width, graph_dotted_line);
Ingo Molnar07800602009-04-20 15:00:56 +0200533
Mike Galbraith923c42c2009-07-22 20:36:03 +0200534 if (sym_filter_entry) {
535 show_details(sym_filter_entry);
536 return;
537 }
538
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200539 /*
540 * Find the longest symbol name that will be displayed
541 */
542 for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
543 syme = rb_entry(nd, struct sym_entry, rb_node);
544 if (++printed > print_entries ||
545 (int)syme->snap_count < count_filter)
546 continue;
547
Arnaldo Carvalho de Melo1a105f72009-11-17 15:40:55 -0200548 if (syme->map->dso->long_name_len > dso_width)
549 dso_width = syme->map->dso->long_name_len;
550
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200551 if (syme->name_len > sym_width)
552 sym_width = syme->name_len;
553 }
554
555 printed = 0;
556
Arnaldo Carvalho de Melo7cc017e2009-11-24 12:05:14 -0200557 max_dso_width = winsize.ws_col - sym_width - 29;
558 if (dso_width > max_dso_width)
559 dso_width = max_dso_width;
560 putchar('\n');
Ingo Molnar07800602009-04-20 15:00:56 +0200561 if (nr_counters == 1)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200562 printf(" samples pcnt");
Ingo Molnar07800602009-04-20 15:00:56 +0200563 else
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200564 printf(" weight samples pcnt");
Ingo Molnar07800602009-04-20 15:00:56 +0200565
Arnaldo Carvalho de Melo7ced1562009-08-26 11:51:26 -0300566 if (verbose)
567 printf(" RIP ");
Arnaldo Carvalho de Melo7cc017e2009-11-24 12:05:14 -0200568 printf(" %-*.*s DSO\n", sym_width, sym_width, "function");
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200569 printf(" %s _______ _____",
Arnaldo Carvalho de Melo7ced1562009-08-26 11:51:26 -0300570 nr_counters == 1 ? " " : "______");
571 if (verbose)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200572 printf(" ________________");
Arnaldo Carvalho de Melo1a105f72009-11-17 15:40:55 -0200573 printf(" %-*.*s", sym_width, sym_width, graph_line);
Arnaldo Carvalho de Melo7cc017e2009-11-24 12:05:14 -0200574 printf(" %-*.*s", dso_width, dso_width, graph_line);
Arnaldo Carvalho de Melo1a105f72009-11-17 15:40:55 -0200575 puts("\n");
Ingo Molnar07800602009-04-20 15:00:56 +0200576
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300577 for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
Ingo Molnar83a09442009-08-15 12:26:57 +0200578 struct symbol *sym;
Ingo Molnar8fc03212009-06-04 15:19:47 +0200579 double pcnt;
Ingo Molnar07800602009-04-20 15:00:56 +0200580
Ingo Molnar83a09442009-08-15 12:26:57 +0200581 syme = rb_entry(nd, struct sym_entry, rb_node);
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200582 sym = sym_entry__symbol(syme);
Ingo Molnar83a09442009-08-15 12:26:57 +0200583
Mike Galbraith923c42c2009-07-22 20:36:03 +0200584 if (++printed > print_entries || (int)syme->snap_count < count_filter)
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300585 continue;
Ingo Molnar07800602009-04-20 15:00:56 +0200586
Ingo Molnar2debbc82009-06-05 14:29:10 +0200587 pcnt = 100.0 - (100.0 * ((sum_ksamples - syme->snap_count) /
588 sum_ksamples));
Mike Galbraithd94b9432009-05-25 09:57:56 +0200589
Mike Galbraith46ab9762009-07-24 10:09:50 +0200590 if (nr_counters == 1 || !display_weighted)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200591 printf("%20.2f ", syme->weight);
Mike Galbraithd94b9432009-05-25 09:57:56 +0200592 else
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200593 printf("%9.1f %10ld ", syme->weight, syme->snap_count);
Ingo Molnar8fc03212009-06-04 15:19:47 +0200594
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200595 percent_color_fprintf(stdout, "%4.1f%%", pcnt);
Arnaldo Carvalho de Melo7ced1562009-08-26 11:51:26 -0300596 if (verbose)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200597 printf(" %016llx", sym->start);
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200598 printf(" %-*.*s", sym_width, sym_width, sym->name);
Arnaldo Carvalho de Melo7cc017e2009-11-24 12:05:14 -0200599 printf(" %-*.*s\n", dso_width, dso_width,
600 dso_width >= syme->map->dso->long_name_len ?
601 syme->map->dso->long_name :
602 syme->map->dso->short_name);
Ingo Molnar07800602009-04-20 15:00:56 +0200603 }
Ingo Molnar07800602009-04-20 15:00:56 +0200604}
605
Mike Galbraith923c42c2009-07-22 20:36:03 +0200606static void prompt_integer(int *target, const char *msg)
607{
608 char *buf = malloc(0), *p;
609 size_t dummy = 0;
610 int tmp;
611
612 fprintf(stdout, "\n%s: ", msg);
613 if (getline(&buf, &dummy, stdin) < 0)
614 return;
615
616 p = strchr(buf, '\n');
617 if (p)
618 *p = 0;
619
620 p = buf;
621 while(*p) {
622 if (!isdigit(*p))
623 goto out_free;
624 p++;
625 }
626 tmp = strtoul(buf, NULL, 10);
627 *target = tmp;
628out_free:
629 free(buf);
630}
631
632static void prompt_percent(int *target, const char *msg)
633{
634 int tmp = 0;
635
636 prompt_integer(&tmp, msg);
637 if (tmp >= 0 && tmp <= 100)
638 *target = tmp;
639}
640
641static void prompt_symbol(struct sym_entry **target, const char *msg)
642{
643 char *buf = malloc(0), *p;
644 struct sym_entry *syme = *target, *n, *found = NULL;
645 size_t dummy = 0;
646
647 /* zero counters of active symbol */
648 if (syme) {
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200649 pthread_mutex_lock(&syme->src->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200650 __zero_source_counters(syme);
651 *target = NULL;
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200652 pthread_mutex_unlock(&syme->src->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200653 }
654
655 fprintf(stdout, "\n%s: ", msg);
656 if (getline(&buf, &dummy, stdin) < 0)
657 goto out_free;
658
659 p = strchr(buf, '\n');
660 if (p)
661 *p = 0;
662
663 pthread_mutex_lock(&active_symbols_lock);
664 syme = list_entry(active_symbols.next, struct sym_entry, node);
665 pthread_mutex_unlock(&active_symbols_lock);
666
667 list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200668 struct symbol *sym = sym_entry__symbol(syme);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200669
670 if (!strcmp(buf, sym->name)) {
671 found = syme;
672 break;
673 }
674 }
675
676 if (!found) {
Kirill Smelkov66aeb6d2010-01-13 13:22:19 -0200677 fprintf(stderr, "Sorry, %s is not active.\n", buf);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200678 sleep(1);
679 return;
680 } else
681 parse_source(found);
682
683out_free:
684 free(buf);
685}
686
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200687static void print_mapped_keys(void)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200688{
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200689 char *name = NULL;
690
691 if (sym_filter_entry) {
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200692 struct symbol *sym = sym_entry__symbol(sym_filter_entry);
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200693 name = sym->name;
694 }
695
696 fprintf(stdout, "\nMapped keys:\n");
697 fprintf(stdout, "\t[d] display refresh delay. \t(%d)\n", delay_secs);
698 fprintf(stdout, "\t[e] display entries (lines). \t(%d)\n", print_entries);
699
700 if (nr_counters > 1)
701 fprintf(stdout, "\t[E] active event counter. \t(%s)\n", event_name(sym_counter));
702
703 fprintf(stdout, "\t[f] profile display filter (count). \t(%d)\n", count_filter);
704
Kirill Smelkov6cff0e82010-02-03 16:52:08 -0200705 fprintf(stdout, "\t[F] annotate display filter (percent). \t(%d%%)\n", sym_pcnt_filter);
706 fprintf(stdout, "\t[s] annotate symbol. \t(%s)\n", name?: "NULL");
707 fprintf(stdout, "\t[S] stop annotation.\n");
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200708
709 if (nr_counters > 1)
710 fprintf(stdout, "\t[w] toggle display weighted/count[E]r. \t(%d)\n", display_weighted ? 1 : 0);
711
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200712 fprintf(stdout,
Kirill Smelkov1a72cfa2010-02-12 19:20:59 +0300713 "\t[K] hide kernel_symbols symbols. \t(%s)\n",
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200714 hide_kernel_symbols ? "yes" : "no");
715 fprintf(stdout,
716 "\t[U] hide user symbols. \t(%s)\n",
717 hide_user_symbols ? "yes" : "no");
Mike Galbraith46ab9762009-07-24 10:09:50 +0200718 fprintf(stdout, "\t[z] toggle sample zeroing. \t(%d)\n", zero ? 1 : 0);
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200719 fprintf(stdout, "\t[qQ] quit.\n");
720}
721
722static int key_mapped(int c)
723{
724 switch (c) {
725 case 'd':
726 case 'e':
727 case 'f':
728 case 'z':
729 case 'q':
730 case 'Q':
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200731 case 'K':
732 case 'U':
Kirill Smelkov6cff0e82010-02-03 16:52:08 -0200733 case 'F':
734 case 's':
735 case 'S':
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200736 return 1;
737 case 'E':
738 case 'w':
739 return nr_counters > 1 ? 1 : 0;
Ingo Molnar83a09442009-08-15 12:26:57 +0200740 default:
741 break;
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200742 }
743
744 return 0;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200745}
746
747static void handle_keypress(int c)
748{
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200749 if (!key_mapped(c)) {
750 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
751 struct termios tc, save;
752
753 print_mapped_keys();
754 fprintf(stdout, "\nEnter selection, or unmapped key to continue: ");
755 fflush(stdout);
756
757 tcgetattr(0, &save);
758 tc = save;
759 tc.c_lflag &= ~(ICANON | ECHO);
760 tc.c_cc[VMIN] = 0;
761 tc.c_cc[VTIME] = 0;
762 tcsetattr(0, TCSANOW, &tc);
763
764 poll(&stdin_poll, 1, -1);
765 c = getc(stdin);
766
767 tcsetattr(0, TCSAFLUSH, &save);
768 if (!key_mapped(c))
769 return;
770 }
771
Mike Galbraith923c42c2009-07-22 20:36:03 +0200772 switch (c) {
773 case 'd':
774 prompt_integer(&delay_secs, "Enter display delay");
Tim Blechmanndc799592009-10-17 18:08:29 +0200775 if (delay_secs < 1)
776 delay_secs = 1;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200777 break;
778 case 'e':
779 prompt_integer(&print_entries, "Enter display entries (lines)");
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200780 if (print_entries == 0) {
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200781 sig_winch_handler(SIGWINCH);
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200782 signal(SIGWINCH, sig_winch_handler);
783 } else
784 signal(SIGWINCH, SIG_DFL);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200785 break;
786 case 'E':
787 if (nr_counters > 1) {
788 int i;
789
790 fprintf(stderr, "\nAvailable events:");
791 for (i = 0; i < nr_counters; i++)
792 fprintf(stderr, "\n\t%d %s", i, event_name(i));
793
794 prompt_integer(&sym_counter, "Enter details event counter");
795
796 if (sym_counter >= nr_counters) {
797 fprintf(stderr, "Sorry, no such event, using %s.\n", event_name(0));
798 sym_counter = 0;
799 sleep(1);
800 }
801 } else sym_counter = 0;
802 break;
803 case 'f':
804 prompt_integer(&count_filter, "Enter display event count filter");
805 break;
806 case 'F':
807 prompt_percent(&sym_pcnt_filter, "Enter details display event filter (percent)");
808 break;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200809 case 'K':
810 hide_kernel_symbols = !hide_kernel_symbols;
811 break;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200812 case 'q':
813 case 'Q':
814 printf("exiting.\n");
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200815 if (dump_symtab)
816 dsos__fprintf(stderr);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200817 exit(0);
818 case 's':
819 prompt_symbol(&sym_filter_entry, "Enter details symbol");
820 break;
821 case 'S':
822 if (!sym_filter_entry)
823 break;
824 else {
825 struct sym_entry *syme = sym_filter_entry;
826
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200827 pthread_mutex_lock(&syme->src->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200828 sym_filter_entry = NULL;
829 __zero_source_counters(syme);
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200830 pthread_mutex_unlock(&syme->src->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200831 }
832 break;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200833 case 'U':
834 hide_user_symbols = !hide_user_symbols;
835 break;
Mike Galbraith46ab9762009-07-24 10:09:50 +0200836 case 'w':
837 display_weighted = ~display_weighted;
838 break;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200839 case 'z':
840 zero = ~zero;
841 break;
Ingo Molnar83a09442009-08-15 12:26:57 +0200842 default:
843 break;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200844 }
845}
846
Ingo Molnarf37a2912009-07-01 12:37:06 +0200847static void *display_thread(void *arg __used)
Ingo Molnar07800602009-04-20 15:00:56 +0200848{
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200849 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
Mike Galbraith923c42c2009-07-22 20:36:03 +0200850 struct termios tc, save;
851 int delay_msecs, c;
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200852
Mike Galbraith923c42c2009-07-22 20:36:03 +0200853 tcgetattr(0, &save);
854 tc = save;
855 tc.c_lflag &= ~(ICANON | ECHO);
856 tc.c_cc[VMIN] = 0;
857 tc.c_cc[VTIME] = 0;
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200858
Mike Galbraith923c42c2009-07-22 20:36:03 +0200859repeat:
860 delay_msecs = delay_secs * 1000;
861 tcsetattr(0, TCSANOW, &tc);
862 /* trash return*/
863 getc(stdin);
Ingo Molnar07800602009-04-20 15:00:56 +0200864
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200865 do {
Ingo Molnar07800602009-04-20 15:00:56 +0200866 print_sym_table();
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200867 } while (!poll(&stdin_poll, 1, delay_msecs) == 1);
868
Mike Galbraith923c42c2009-07-22 20:36:03 +0200869 c = getc(stdin);
870 tcsetattr(0, TCSAFLUSH, &save);
871
872 handle_keypress(c);
873 goto repeat;
Ingo Molnar07800602009-04-20 15:00:56 +0200874
875 return NULL;
876}
877
Anton Blanchard2ab52082009-07-01 09:00:46 +1000878/* Tag samples to be skipped. */
Ingo Molnarf37a2912009-07-01 12:37:06 +0200879static const char *skip_symbols[] = {
Anton Blanchard2ab52082009-07-01 09:00:46 +1000880 "default_idle",
881 "cpu_idle",
882 "enter_idle",
883 "exit_idle",
884 "mwait_idle",
Arnaldo Carvalho de Melo59b90052009-07-26 19:06:19 -0300885 "mwait_idle_with_hints",
Arnaldo Carvalho de Melo83572752009-09-25 15:02:39 -0700886 "poll_idle",
Anton Blanchard3a3393e2009-07-01 09:00:47 +1000887 "ppc64_runlatch_off",
888 "pseries_dedicated_idle_sleep",
Anton Blanchard2ab52082009-07-01 09:00:46 +1000889 NULL
890};
891
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300892static int symbol_filter(struct map *map, struct symbol *sym)
Ingo Molnar07800602009-04-20 15:00:56 +0200893{
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300894 struct sym_entry *syme;
895 const char *name = sym->name;
Anton Blanchard2ab52082009-07-01 09:00:46 +1000896 int i;
Ingo Molnar07800602009-04-20 15:00:56 +0200897
Anton Blanchard3a3393e2009-07-01 09:00:47 +1000898 /*
899 * ppc64 uses function descriptors and appends a '.' to the
900 * start of every instruction address. Remove it.
901 */
902 if (name[0] == '.')
903 name++;
904
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300905 if (!strcmp(name, "_text") ||
906 !strcmp(name, "_etext") ||
907 !strcmp(name, "_sinittext") ||
908 !strncmp("init_module", name, 11) ||
909 !strncmp("cleanup_module", name, 14) ||
910 strstr(name, "_text_start") ||
911 strstr(name, "_text_end"))
Ingo Molnar07800602009-04-20 15:00:56 +0200912 return 1;
913
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200914 syme = symbol__priv(sym);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300915 syme->map = map;
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200916 syme->src = NULL;
Kirill Smelkov6cff0e82010-02-03 16:52:08 -0200917
918 if (!sym_filter_entry && sym_filter && !strcmp(name, sym_filter)) {
919 /* schedule initial sym_filter_entry setup */
920 sym_filter_entry_sched = syme;
921 sym_filter = NULL;
922 }
Mike Galbraith923c42c2009-07-22 20:36:03 +0200923
Anton Blanchard2ab52082009-07-01 09:00:46 +1000924 for (i = 0; skip_symbols[i]; i++) {
925 if (!strcmp(skip_symbols[i], name)) {
926 syme->skip = 1;
927 break;
928 }
929 }
Ingo Molnar07800602009-04-20 15:00:56 +0200930
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200931 if (!syme->skip)
932 syme->name_len = strlen(sym->name);
933
Ingo Molnar07800602009-04-20 15:00:56 +0200934 return 0;
935}
936
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200937static void event__process_sample(const event_t *self,
938 struct perf_session *session, int counter)
Ingo Molnar07800602009-04-20 15:00:56 +0200939{
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200940 u64 ip = self->ip.ip;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200941 struct sym_entry *syme;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200942 struct addr_location al;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200943 u8 origin = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Ingo Molnar07800602009-04-20 15:00:56 +0200944
Arnaldo Carvalho de Melo24bfef02010-01-21 13:04:43 -0200945 ++samples;
946
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200947 switch (origin) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200948 case PERF_RECORD_MISC_USER:
Arnaldo Carvalho de Melo24bfef02010-01-21 13:04:43 -0200949 ++userspace_samples;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200950 if (hide_user_symbols)
951 return;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200952 break;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200953 case PERF_RECORD_MISC_KERNEL:
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200954 if (hide_kernel_symbols)
955 return;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200956 break;
957 default:
Ingo Molnar07800602009-04-20 15:00:56 +0200958 return;
959 }
960
Peter Zijlstra1676b8a2010-03-04 14:19:36 +0100961 if (self->header.misc & PERF_RECORD_MISC_EXACT)
962 exact_samples++;
963
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200964 if (event__preprocess_sample(self, session, &al, symbol_filter) < 0 ||
Arnaldo Carvalho de Melo72b8fa12010-01-27 21:05:49 -0200965 al.filtered)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200966 return;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200967
Arnaldo Carvalho de Melo72b8fa12010-01-27 21:05:49 -0200968 if (al.sym == NULL) {
969 /*
970 * As we do lazy loading of symtabs we only will know if the
971 * specified vmlinux file is invalid when we actually have a
972 * hit in kernel space and then try to load it. So if we get
973 * here and there are _no_ symbols in the DSO backing the
974 * kernel map, bail out.
975 *
976 * We may never get here, for instance, if we use -K/
977 * --hide-kernel-symbols, even if the user specifies an
978 * invalid --vmlinux ;-)
979 */
980 if (al.map == session->vmlinux_maps[MAP__FUNCTION] &&
981 RB_EMPTY_ROOT(&al.map->dso->symbols[MAP__FUNCTION])) {
982 pr_err("The %s file can't be used\n",
983 symbol_conf.vmlinux_name);
984 exit(1);
985 }
986
987 return;
988 }
989
Kirill Smelkov6cff0e82010-02-03 16:52:08 -0200990 /* let's see, whether we need to install initial sym_filter_entry */
991 if (sym_filter_entry_sched) {
992 sym_filter_entry = sym_filter_entry_sched;
993 sym_filter_entry_sched = NULL;
994 parse_source(sym_filter_entry);
995 }
996
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200997 syme = symbol__priv(al.sym);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200998 if (!syme->skip) {
999 syme->count[counter]++;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -02001000 syme->origin = origin;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001001 record_precise_ip(syme, counter, ip);
1002 pthread_mutex_lock(&active_symbols_lock);
1003 if (list_empty(&syme->node) || !syme->node.next)
1004 __list_insert_active_sym(syme);
1005 pthread_mutex_unlock(&active_symbols_lock);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001006 }
1007}
1008
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001009static int event__process(event_t *event, struct perf_session *session)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001010{
1011 switch (event->header.type) {
1012 case PERF_RECORD_COMM:
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001013 event__process_comm(event, session);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001014 break;
1015 case PERF_RECORD_MMAP:
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001016 event__process_mmap(event, session);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001017 break;
Arnaldo Carvalho de Melo0f35cd42010-01-21 13:04:44 -02001018 case PERF_RECORD_FORK:
1019 case PERF_RECORD_EXIT:
1020 event__process_task(event, session);
1021 break;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001022 default:
1023 break;
1024 }
1025
1026 return 0;
Ingo Molnar07800602009-04-20 15:00:56 +02001027}
1028
Ingo Molnar07800602009-04-20 15:00:56 +02001029struct mmap_data {
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001030 int counter;
1031 void *base;
Ingo Molnarf37a2912009-07-01 12:37:06 +02001032 int mask;
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001033 unsigned int prev;
Ingo Molnar07800602009-04-20 15:00:56 +02001034};
1035
1036static unsigned int mmap_read_head(struct mmap_data *md)
1037{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001038 struct perf_event_mmap_page *pc = md->base;
Ingo Molnar07800602009-04-20 15:00:56 +02001039 int head;
1040
1041 head = pc->data_head;
1042 rmb();
1043
1044 return head;
1045}
1046
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001047static void perf_session__mmap_read_counter(struct perf_session *self,
1048 struct mmap_data *md)
Ingo Molnar07800602009-04-20 15:00:56 +02001049{
1050 unsigned int head = mmap_read_head(md);
1051 unsigned int old = md->prev;
1052 unsigned char *data = md->base + page_size;
1053 int diff;
1054
Ingo Molnar07800602009-04-20 15:00:56 +02001055 /*
1056 * If we're further behind than half the buffer, there's a chance
Ingo Molnar2debbc82009-06-05 14:29:10 +02001057 * the writer will bite our tail and mess up the samples under us.
Ingo Molnar07800602009-04-20 15:00:56 +02001058 *
1059 * If we somehow ended up ahead of the head, we got messed up.
1060 *
1061 * In either case, truncate and restart at head.
1062 */
1063 diff = head - old;
1064 if (diff > md->mask / 2 || diff < 0) {
Mike Galbraithf4f0b412009-10-13 14:57:20 +02001065 fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
Ingo Molnar07800602009-04-20 15:00:56 +02001066
1067 /*
1068 * head points to a known good entry, start there.
1069 */
1070 old = head;
1071 }
1072
Ingo Molnar07800602009-04-20 15:00:56 +02001073 for (; old != head;) {
Ingo Molnar07800602009-04-20 15:00:56 +02001074 event_t *event = (event_t *)&data[old & md->mask];
1075
1076 event_t event_copy;
1077
Ingo Molnar6f06ccb2009-04-20 15:22:22 +02001078 size_t size = event->header.size;
Ingo Molnar07800602009-04-20 15:00:56 +02001079
1080 /*
1081 * Event straddles the mmap boundary -- header should always
1082 * be inside due to u64 alignment of output.
1083 */
1084 if ((old & md->mask) + size != ((old + size) & md->mask)) {
1085 unsigned int offset = old;
1086 unsigned int len = min(sizeof(*event), size), cpy;
1087 void *dst = &event_copy;
1088
1089 do {
1090 cpy = min(md->mask + 1 - (offset & md->mask), len);
1091 memcpy(dst, &data[offset & md->mask], cpy);
1092 offset += cpy;
1093 dst += cpy;
1094 len -= cpy;
1095 } while (len);
1096
1097 event = &event_copy;
1098 }
1099
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001100 if (event->header.type == PERF_RECORD_SAMPLE)
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001101 event__process_sample(event, self, md->counter);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001102 else
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001103 event__process(event, self);
Ingo Molnar07800602009-04-20 15:00:56 +02001104 old += size;
Ingo Molnar07800602009-04-20 15:00:56 +02001105 }
1106
1107 md->prev = old;
1108}
1109
Mike Galbraithc2990a22009-05-24 08:35:49 +02001110static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
1111static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
1112
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001113static void perf_session__mmap_read(struct perf_session *self)
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001114{
1115 int i, counter;
1116
1117 for (i = 0; i < nr_cpus; i++) {
1118 for (counter = 0; counter < nr_counters; counter++)
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001119 perf_session__mmap_read_counter(self, &mmap_array[i][counter]);
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001120 }
1121}
1122
Ingo Molnar716c69f2009-06-07 17:31:52 +02001123int nr_poll;
1124int group_fd;
1125
1126static void start_counter(int i, int counter)
Ingo Molnar07800602009-04-20 15:00:56 +02001127{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001128 struct perf_event_attr *attr;
Mike Galbraith0fdc7e62009-07-21 10:30:36 +02001129 int cpu;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001130
1131 cpu = profile_cpu;
1132 if (target_pid == -1 && profile_cpu == -1)
Paul Mackerrasa12b51c2010-03-10 20:36:09 +11001133 cpu = cpumap[i];
Ingo Molnar716c69f2009-06-07 17:31:52 +02001134
1135 attr = attrs + counter;
1136
1137 attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +02001138
1139 if (freq) {
1140 attr->sample_type |= PERF_SAMPLE_PERIOD;
1141 attr->freq = 1;
1142 attr->sample_freq = freq;
1143 }
1144
Mike Galbraith0fdc7e62009-07-21 10:30:36 +02001145 attr->inherit = (cpu < 0) && inherit;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001146 attr->mmap = 1;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001147
1148try_again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001149 fd[i][counter] = sys_perf_event_open(attr, target_pid, cpu, group_fd, 0);
Ingo Molnar716c69f2009-06-07 17:31:52 +02001150
1151 if (fd[i][counter] < 0) {
1152 int err = errno;
1153
Pekka Enbergc10edee2009-11-08 18:01:06 +02001154 if (err == EPERM || err == EACCES)
Ingo Molnar3da297a2009-06-07 17:39:02 +02001155 die("No permission - are you root?\n");
Ingo Molnar716c69f2009-06-07 17:31:52 +02001156 /*
1157 * If it's cycles then fall back to hrtimer
1158 * based cpu-clock-tick sw counter, which
1159 * is always available even if no PMU support:
1160 */
1161 if (attr->type == PERF_TYPE_HARDWARE
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02001162 && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
Ingo Molnar716c69f2009-06-07 17:31:52 +02001163
Ingo Molnar3da297a2009-06-07 17:39:02 +02001164 if (verbose)
1165 warning(" ... trying to fall back to cpu-clock-ticks\n");
1166
Ingo Molnar716c69f2009-06-07 17:31:52 +02001167 attr->type = PERF_TYPE_SOFTWARE;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02001168 attr->config = PERF_COUNT_SW_CPU_CLOCK;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001169 goto try_again;
1170 }
Ingo Molnar30c806a2009-06-07 17:46:24 +02001171 printf("\n");
1172 error("perfcounter syscall returned with %d (%s)\n",
1173 fd[i][counter], strerror(err));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001174 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
Ingo Molnar716c69f2009-06-07 17:31:52 +02001175 exit(-1);
1176 }
1177 assert(fd[i][counter] >= 0);
1178 fcntl(fd[i][counter], F_SETFL, O_NONBLOCK);
1179
1180 /*
1181 * First counter acts as the group leader:
1182 */
1183 if (group && group_fd == -1)
1184 group_fd = fd[i][counter];
1185
1186 event_array[nr_poll].fd = fd[i][counter];
1187 event_array[nr_poll].events = POLLIN;
1188 nr_poll++;
1189
1190 mmap_array[i][counter].counter = counter;
1191 mmap_array[i][counter].prev = 0;
1192 mmap_array[i][counter].mask = mmap_pages*page_size - 1;
1193 mmap_array[i][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
1194 PROT_READ, MAP_SHARED, fd[i][counter], 0);
1195 if (mmap_array[i][counter].base == MAP_FAILED)
1196 die("failed to mmap with %d (%s)\n", errno, strerror(errno));
1197}
1198
1199static int __cmd_top(void)
1200{
1201 pthread_t thread;
1202 int i, counter;
Ingo Molnar07800602009-04-20 15:00:56 +02001203 int ret;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001204 /*
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001205 * FIXME: perf_session__new should allow passing a O_MMAP, so that all this
1206 * mmap reading, etc is encapsulated in it. Use O_WRONLY for now.
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001207 */
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02001208 struct perf_session *session = perf_session__new(NULL, O_WRONLY, false);
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001209 if (session == NULL)
1210 return -ENOMEM;
Ingo Molnar07800602009-04-20 15:00:56 +02001211
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001212 if (target_pid != -1)
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001213 event__synthesize_thread(target_pid, event__process, session);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001214 else
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001215 event__synthesize_threads(event__process, session);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001216
Ingo Molnar07800602009-04-20 15:00:56 +02001217 for (i = 0; i < nr_cpus; i++) {
1218 group_fd = -1;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001219 for (counter = 0; counter < nr_counters; counter++)
1220 start_counter(i, counter);
Ingo Molnar07800602009-04-20 15:00:56 +02001221 }
1222
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001223 /* Wait for a minimal set of events before starting the snapshot */
1224 poll(event_array, nr_poll, 100);
1225
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001226 perf_session__mmap_read(session);
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001227
Ingo Molnar07800602009-04-20 15:00:56 +02001228 if (pthread_create(&thread, NULL, display_thread, NULL)) {
1229 printf("Could not create display thread.\n");
1230 exit(-1);
1231 }
1232
1233 if (realtime_prio) {
1234 struct sched_param param;
1235
1236 param.sched_priority = realtime_prio;
1237 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
1238 printf("Could not set realtime priority.\n");
1239 exit(-1);
1240 }
1241 }
1242
1243 while (1) {
Ingo Molnar2debbc82009-06-05 14:29:10 +02001244 int hits = samples;
Ingo Molnar07800602009-04-20 15:00:56 +02001245
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001246 perf_session__mmap_read(session);
Ingo Molnar07800602009-04-20 15:00:56 +02001247
Ingo Molnar2debbc82009-06-05 14:29:10 +02001248 if (hits == samples)
Ingo Molnar07800602009-04-20 15:00:56 +02001249 ret = poll(event_array, nr_poll, 100);
1250 }
1251
1252 return 0;
1253}
Ingo Molnarb456bae2009-05-26 09:17:18 +02001254
1255static const char * const top_usage[] = {
1256 "perf top [<options>]",
1257 NULL
1258};
1259
Ingo Molnarb456bae2009-05-26 09:17:18 +02001260static const struct option options[] = {
1261 OPT_CALLBACK('e', "event", NULL, "event",
Thomas Gleixner86847b62009-06-06 12:24:17 +02001262 "event selector. use 'perf list' to list available events",
1263 parse_events),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001264 OPT_INTEGER('c', "count", &default_interval,
1265 "event period to sample"),
1266 OPT_INTEGER('p', "pid", &target_pid,
1267 "profile events on existing pid"),
1268 OPT_BOOLEAN('a', "all-cpus", &system_wide,
1269 "system-wide collection from all CPUs"),
1270 OPT_INTEGER('C', "CPU", &profile_cpu,
1271 "CPU to profile on"),
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02001272 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1273 "file", "vmlinux pathname"),
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -02001274 OPT_BOOLEAN('K', "hide_kernel_symbols", &hide_kernel_symbols,
1275 "hide kernel symbols"),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001276 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
1277 "number of mmap data pages"),
1278 OPT_INTEGER('r', "realtime", &realtime_prio,
1279 "collect data with this RT SCHED_FIFO priority"),
Mike Galbraithdb20c002009-05-26 15:25:34 +02001280 OPT_INTEGER('d', "delay", &delay_secs,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001281 "number of seconds to delay between refreshes"),
1282 OPT_BOOLEAN('D', "dump-symtab", &dump_symtab,
1283 "dump the symbol table used for profiling"),
Ingo Molnar6e53cdf2009-06-04 08:53:05 +02001284 OPT_INTEGER('f', "count-filter", &count_filter,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001285 "only display functions with more events than this"),
1286 OPT_BOOLEAN('g', "group", &group,
1287 "put the counters into a counter group"),
Mike Galbraith0fdc7e62009-07-21 10:30:36 +02001288 OPT_BOOLEAN('i', "inherit", &inherit,
1289 "child tasks inherit counters"),
Mike Galbraith923c42c2009-07-22 20:36:03 +02001290 OPT_STRING('s', "sym-annotate", &sym_filter, "symbol name",
Kirill Smelkov6cff0e82010-02-03 16:52:08 -02001291 "symbol to annotate"),
Anton Blanchard1f208ea2009-07-01 09:00:44 +10001292 OPT_BOOLEAN('z', "zero", &zero,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001293 "zero history across updates"),
Ingo Molnar6e53cdf2009-06-04 08:53:05 +02001294 OPT_INTEGER('F', "freq", &freq,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001295 "profile at this frequency"),
Ingo Molnar6e53cdf2009-06-04 08:53:05 +02001296 OPT_INTEGER('E', "entries", &print_entries,
1297 "display this many functions"),
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -02001298 OPT_BOOLEAN('U', "hide_user_symbols", &hide_user_symbols,
1299 "hide user symbols"),
Ingo Molnar3da297a2009-06-07 17:39:02 +02001300 OPT_BOOLEAN('v', "verbose", &verbose,
1301 "be more verbose (show counter open errors, etc)"),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001302 OPT_END()
1303};
1304
Ingo Molnarf37a2912009-07-01 12:37:06 +02001305int cmd_top(int argc, const char **argv, const char *prefix __used)
Ingo Molnarb456bae2009-05-26 09:17:18 +02001306{
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02001307 int counter;
Ingo Molnarb456bae2009-05-26 09:17:18 +02001308
1309 page_size = sysconf(_SC_PAGE_SIZE);
1310
Ingo Molnarb456bae2009-05-26 09:17:18 +02001311 argc = parse_options(argc, argv, options, top_usage, 0);
1312 if (argc)
1313 usage_with_options(top_usage, options);
1314
Ingo Molnarb456bae2009-05-26 09:17:18 +02001315 /* CPU and PID are mutually exclusive */
1316 if (target_pid != -1 && profile_cpu != -1) {
1317 printf("WARNING: PID switch overriding CPU\n");
1318 sleep(1);
1319 profile_cpu = -1;
1320 }
1321
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001322 if (!nr_counters)
Ingo Molnarb456bae2009-05-26 09:17:18 +02001323 nr_counters = 1;
Ingo Molnarb456bae2009-05-26 09:17:18 +02001324
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02001325 symbol_conf.priv_size = (sizeof(struct sym_entry) +
1326 (nr_counters + 1) * sizeof(unsigned long));
Kirill Smelkov6cff0e82010-02-03 16:52:08 -02001327
1328 symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL);
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02001329 if (symbol__init() < 0)
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02001330 return -1;
Arnaldo Carvalho de Melo5a8e5a32009-11-17 18:38:01 -02001331
Frederic Weisbecker2f335a02009-06-05 19:31:01 +02001332 if (delay_secs < 1)
1333 delay_secs = 1;
1334
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +02001335 /*
1336 * User specified count overrides default frequency.
1337 */
1338 if (default_interval)
1339 freq = 0;
1340 else if (freq) {
1341 default_interval = freq;
1342 } else {
1343 fprintf(stderr, "frequency and count are zero, aborting\n");
1344 exit(EXIT_FAILURE);
1345 }
1346
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001347 /*
1348 * Fill in the ones not specifically initialized via -c:
1349 */
Ingo Molnarb456bae2009-05-26 09:17:18 +02001350 for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001351 if (attrs[counter].sample_period)
Ingo Molnarb456bae2009-05-26 09:17:18 +02001352 continue;
1353
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001354 attrs[counter].sample_period = default_interval;
Ingo Molnarb456bae2009-05-26 09:17:18 +02001355 }
1356
Ingo Molnarb456bae2009-05-26 09:17:18 +02001357 if (target_pid != -1 || profile_cpu != -1)
1358 nr_cpus = 1;
Paul Mackerrasa12b51c2010-03-10 20:36:09 +11001359 else
1360 nr_cpus = read_cpu_map();
Ingo Molnarb456bae2009-05-26 09:17:18 +02001361
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -02001362 get_term_dimensions(&winsize);
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -02001363 if (print_entries == 0) {
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -02001364 update_print_entries(&winsize);
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -02001365 signal(SIGWINCH, sig_winch_handler);
1366 }
1367
Ingo Molnarb456bae2009-05-26 09:17:18 +02001368 return __cmd_top();
1369}