blob: e0a374d0e43a8da197177a7ccccbc3ada6349a2e [file] [log] [blame]
Ingo Molnar07800602009-04-20 15:00:56 +02001/*
Ingo Molnarbf9e1872009-06-02 23:37:05 +02002 * builtin-top.c
3 *
4 * Builtin top command: Display a continuously updated profile of
5 * any workload, CPU or specific PID.
6 *
7 * Copyright (C) 2008, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
8 *
9 * Improvements and fixes by:
10 *
11 * Arjan van de Ven <arjan@linux.intel.com>
12 * Yanmin Zhang <yanmin.zhang@intel.com>
13 * Wu Fengguang <fengguang.wu@intel.com>
14 * Mike Galbraith <efault@gmx.de>
15 * Paul Mackerras <paulus@samba.org>
16 *
17 * Released under the GPL v2. (and only v2, not any later version)
Ingo Molnar07800602009-04-20 15:00:56 +020018 */
Ingo Molnarbf9e1872009-06-02 23:37:05 +020019#include "builtin.h"
Ingo Molnar07800602009-04-20 15:00:56 +020020
Peter Zijlstra1a482f32009-05-23 18:28:58 +020021#include "perf.h"
Ingo Molnarbf9e1872009-06-02 23:37:05 +020022
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -030023#include "util/symbol.h"
Ingo Molnar8fc03212009-06-04 15:19:47 +020024#include "util/color.h"
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030025#include "util/thread.h"
Ingo Molnar148be2c2009-04-27 08:02:14 +020026#include "util/util.h"
Arnaldo Carvalho de Melo43cbcd82009-07-01 12:28:37 -030027#include <linux/rbtree.h>
Ingo Molnarb456bae2009-05-26 09:17:18 +020028#include "util/parse-options.h"
29#include "util/parse-events.h"
Ingo Molnar07800602009-04-20 15:00:56 +020030
Frederic Weisbecker8f28827a2009-08-16 22:05:48 +020031#include "util/debug.h"
32
Ingo Molnar07800602009-04-20 15:00:56 +020033#include <assert.h>
34#include <fcntl.h>
Ingo Molnar0e9b20b2009-05-26 09:17:18 +020035
Ingo Molnar07800602009-04-20 15:00:56 +020036#include <stdio.h>
Mike Galbraith923c42c2009-07-22 20:36:03 +020037#include <termios.h>
38#include <unistd.h>
Ingo Molnar0e9b20b2009-05-26 09:17:18 +020039
Ingo Molnar07800602009-04-20 15:00:56 +020040#include <errno.h>
Ingo Molnar07800602009-04-20 15:00:56 +020041#include <time.h>
42#include <sched.h>
43#include <pthread.h>
44
45#include <sys/syscall.h>
46#include <sys/ioctl.h>
47#include <sys/poll.h>
48#include <sys/prctl.h>
49#include <sys/wait.h>
50#include <sys/uio.h>
51#include <sys/mman.h>
52
53#include <linux/unistd.h>
54#include <linux/types.h>
55
Ingo Molnara21ca2c2009-06-06 09:58:57 +020056static int fd[MAX_NR_CPUS][MAX_COUNTERS];
57
Ingo Molnar42e59d72009-10-06 15:14:21 +020058static int system_wide = 0;
Ingo Molnar07800602009-04-20 15:00:56 +020059
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +020060static int default_interval = 0;
Ingo Molnar07800602009-04-20 15:00:56 +020061
Ingo Molnar42e59d72009-10-06 15:14:21 +020062static int count_filter = 5;
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -020063static int print_entries;
Ingo Molnar07800602009-04-20 15:00:56 +020064
Ingo Molnar42e59d72009-10-06 15:14:21 +020065static int target_pid = -1;
66static int inherit = 0;
67static int profile_cpu = -1;
68static int nr_cpus = 0;
69static unsigned int realtime_prio = 0;
70static int group = 0;
Ingo Molnar07800602009-04-20 15:00:56 +020071static unsigned int page_size;
Ingo Molnar42e59d72009-10-06 15:14:21 +020072static unsigned int mmap_pages = 16;
73static int freq = 1000; /* 1 KHz */
Ingo Molnar07800602009-04-20 15:00:56 +020074
Ingo Molnar42e59d72009-10-06 15:14:21 +020075static int delay_secs = 2;
76static int zero = 0;
77static int dump_symtab = 0;
Ingo Molnar07800602009-04-20 15:00:56 +020078
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -020079static bool hide_kernel_symbols = false;
80static bool hide_user_symbols = false;
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -020081static struct winsize winsize;
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -020082struct symbol_conf symbol_conf;
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;
97static int sym_pcnt_filter = 5;
98static int sym_counter = 0;
99static int display_weighted = -1;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200100
101/*
Ingo Molnar07800602009-04-20 15:00:56 +0200102 * Symbols
103 */
104
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200105struct sym_entry_source {
106 struct source_line *source;
107 struct source_line *lines;
108 struct source_line **lines_tail;
109 pthread_mutex_t lock;
110};
111
Ingo Molnar07800602009-04-20 15:00:56 +0200112struct sym_entry {
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300113 struct rb_node rb_node;
114 struct list_head node;
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300115 unsigned long snap_count;
116 double weight;
Ingo Molnar07800602009-04-20 15:00:56 +0200117 int skip;
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200118 u16 name_len;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200119 u8 origin;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300120 struct map *map;
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200121 struct sym_entry_source *src;
Arnaldo Carvalho de Melo5a8e5a32009-11-17 18:38:01 -0200122 unsigned long count[0];
Ingo Molnar07800602009-04-20 15:00:56 +0200123};
124
Mike Galbraith923c42c2009-07-22 20:36:03 +0200125/*
126 * Source functions
127 */
128
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200129static inline struct symbol *sym_entry__symbol(struct sym_entry *self)
130{
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -0200131 return ((void *)self) + symbol_conf.priv_size;
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200132}
133
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200134static void get_term_dimensions(struct winsize *ws)
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200135{
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200136 char *s = getenv("LINES");
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200137
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200138 if (s != NULL) {
139 ws->ws_row = atoi(s);
140 s = getenv("COLUMNS");
141 if (s != NULL) {
142 ws->ws_col = atoi(s);
143 if (ws->ws_row && ws->ws_col)
144 return;
145 }
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200146 }
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200147#ifdef TIOCGWINSZ
148 if (ioctl(1, TIOCGWINSZ, ws) == 0 &&
149 ws->ws_row && ws->ws_col)
150 return;
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200151#endif
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200152 ws->ws_row = 25;
153 ws->ws_col = 80;
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200154}
155
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200156static void update_print_entries(struct winsize *ws)
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200157{
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200158 print_entries = ws->ws_row;
159
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200160 if (print_entries > 9)
161 print_entries -= 9;
162}
163
164static void sig_winch_handler(int sig __used)
165{
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200166 get_term_dimensions(&winsize);
167 update_print_entries(&winsize);
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200168}
169
Mike Galbraith923c42c2009-07-22 20:36:03 +0200170static void parse_source(struct sym_entry *syme)
171{
172 struct symbol *sym;
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200173 struct sym_entry_source *source;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300174 struct map *map;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200175 FILE *file;
Ingo Molnar83a09442009-08-15 12:26:57 +0200176 char command[PATH_MAX*2];
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300177 const char *path;
178 u64 len;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200179
180 if (!syme)
181 return;
182
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200183 if (syme->src == NULL) {
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200184 syme->src = zalloc(sizeof(*source));
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200185 if (syme->src == NULL)
186 return;
187 pthread_mutex_init(&syme->src->lock, NULL);
188 }
189
190 source = syme->src;
191
192 if (source->lines) {
193 pthread_mutex_lock(&source->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200194 goto out_assign;
195 }
196
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200197 sym = sym_entry__symbol(syme);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300198 map = syme->map;
199 path = map->dso->long_name;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200200
Mike Galbraith923c42c2009-07-22 20:36:03 +0200201 len = sym->end - sym->start;
202
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300203 sprintf(command,
204 "objdump --start-address=0x%016Lx "
205 "--stop-address=0x%016Lx -dS %s",
Arnaldo Carvalho de Meloc88e4bf2009-10-20 15:54:55 -0200206 map->unmap_ip(map, sym->start),
207 map->unmap_ip(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;
218 char *c;
219
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
237 if (strlen(src->line)>8 && src->line[8] == ':') {
238 src->eip = strtoull(src->line, NULL, 16);
Arnaldo Carvalho de Meloc88e4bf2009-10-20 15:54:55 -0200239 src->eip = map->unmap_ip(map, src->eip);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200240 }
241 if (strlen(src->line)>8 && src->line[16] == ':') {
242 src->eip = strtoull(src->line, NULL, 16);
Arnaldo Carvalho de Meloc88e4bf2009-10-20 15:54:55 -0200243 src->eip = map->unmap_ip(map, src->eip);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200244 }
245 }
246 pclose(file);
247out_assign:
248 sym_filter_entry = syme;
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200249 pthread_mutex_unlock(&source->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200250}
251
252static void __zero_source_counters(struct sym_entry *syme)
253{
254 int i;
255 struct source_line *line;
256
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200257 line = syme->src->lines;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200258 while (line) {
259 for (i = 0; i < nr_counters; i++)
260 line->count[i] = 0;
261 line = line->next;
262 }
263}
264
265static void record_precise_ip(struct sym_entry *syme, int counter, u64 ip)
266{
267 struct source_line *line;
268
269 if (syme != sym_filter_entry)
270 return;
271
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200272 if (pthread_mutex_trylock(&syme->src->lock))
Mike Galbraith923c42c2009-07-22 20:36:03 +0200273 return;
274
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200275 if (syme->src == NULL || syme->src->source == NULL)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200276 goto out_unlock;
277
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200278 for (line = syme->src->lines; line; line = line->next) {
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
290static void lookup_sym_source(struct sym_entry *syme)
291{
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200292 struct symbol *symbol = sym_entry__symbol(syme);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200293 struct source_line *line;
294 char pattern[PATH_MAX];
Mike Galbraith923c42c2009-07-22 20:36:03 +0200295
296 sprintf(pattern, "<%s>:", symbol->name);
297
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200298 pthread_mutex_lock(&syme->src->lock);
299 for (line = syme->src->lines; line; line = line->next) {
Mike Galbraith923c42c2009-07-22 20:36:03 +0200300 if (strstr(line->line, pattern)) {
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200301 syme->src->source = line;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200302 break;
303 }
304 }
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200305 pthread_mutex_unlock(&syme->src->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200306}
307
308static void show_lines(struct source_line *queue, int count, int total)
309{
310 int i;
311 struct source_line *line;
312
313 line = queue;
314 for (i = 0; i < count; i++) {
315 float pcnt = 100.0*(float)line->count[sym_counter]/(float)total;
316
317 printf("%8li %4.1f%%\t%s\n", line->count[sym_counter], pcnt, line->line);
318 line = line->next;
319 }
320}
321
322#define TRACE_COUNT 3
323
324static void show_details(struct sym_entry *syme)
325{
326 struct symbol *symbol;
327 struct source_line *line;
328 struct source_line *line_queue = NULL;
329 int displayed = 0;
330 int line_queue_count = 0, total = 0, more = 0;
331
332 if (!syme)
333 return;
334
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200335 if (!syme->src->source)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200336 lookup_sym_source(syme);
337
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200338 if (!syme->src->source)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200339 return;
340
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200341 symbol = sym_entry__symbol(syme);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200342 printf("Showing %s for %s\n", event_name(sym_counter), symbol->name);
343 printf(" Events Pcnt (>=%d%%)\n", sym_pcnt_filter);
344
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200345 pthread_mutex_lock(&syme->src->lock);
346 line = syme->src->source;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200347 while (line) {
348 total += line->count[sym_counter];
349 line = line->next;
350 }
351
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200352 line = syme->src->source;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200353 while (line) {
354 float pcnt = 0.0;
355
356 if (!line_queue_count)
357 line_queue = line;
358 line_queue_count++;
359
360 if (line->count[sym_counter])
361 pcnt = 100.0 * line->count[sym_counter] / (float)total;
362 if (pcnt >= (float)sym_pcnt_filter) {
363 if (displayed <= print_entries)
364 show_lines(line_queue, line_queue_count, total);
365 else more++;
366 displayed += line_queue_count;
367 line_queue_count = 0;
368 line_queue = NULL;
369 } else if (line_queue_count > TRACE_COUNT) {
370 line_queue = line_queue->next;
371 line_queue_count--;
372 }
373
374 line->count[sym_counter] = zero ? 0 : line->count[sym_counter] * 7 / 8;
375 line = line->next;
376 }
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200377 pthread_mutex_unlock(&syme->src->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200378 if (more)
379 printf("%d lines not displayed, maybe increase display entries [e]\n", more);
380}
Ingo Molnar07800602009-04-20 15:00:56 +0200381
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300382/*
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200383 * Symbols will be added here in event__process_sample and will get out
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300384 * after decayed.
385 */
386static LIST_HEAD(active_symbols);
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300387static pthread_mutex_t active_symbols_lock = PTHREAD_MUTEX_INITIALIZER;
Ingo Molnar07800602009-04-20 15:00:56 +0200388
Ingo Molnar07800602009-04-20 15:00:56 +0200389/*
390 * Ordering weight: count-1 * count-2 * ... / count-n
391 */
392static double sym_weight(const struct sym_entry *sym)
393{
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300394 double weight = sym->snap_count;
Ingo Molnar07800602009-04-20 15:00:56 +0200395 int counter;
396
Mike Galbraith46ab9762009-07-24 10:09:50 +0200397 if (!display_weighted)
398 return weight;
399
Ingo Molnar07800602009-04-20 15:00:56 +0200400 for (counter = 1; counter < nr_counters-1; counter++)
401 weight *= sym->count[counter];
402
403 weight /= (sym->count[counter] + 1);
404
405 return weight;
406}
407
Ingo Molnar2debbc82009-06-05 14:29:10 +0200408static long samples;
409static long userspace_samples;
Ingo Molnar07800602009-04-20 15:00:56 +0200410static const char CONSOLE_CLEAR[] = "";
411
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300412static void __list_insert_active_sym(struct sym_entry *syme)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300413{
414 list_add(&syme->node, &active_symbols);
415}
416
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300417static void list_remove_active_sym(struct sym_entry *syme)
418{
419 pthread_mutex_lock(&active_symbols_lock);
420 list_del_init(&syme->node);
421 pthread_mutex_unlock(&active_symbols_lock);
422}
423
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300424static void rb_insert_active_sym(struct rb_root *tree, struct sym_entry *se)
425{
426 struct rb_node **p = &tree->rb_node;
427 struct rb_node *parent = NULL;
428 struct sym_entry *iter;
429
430 while (*p != NULL) {
431 parent = *p;
432 iter = rb_entry(parent, struct sym_entry, rb_node);
433
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300434 if (se->weight > iter->weight)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300435 p = &(*p)->rb_left;
436 else
437 p = &(*p)->rb_right;
438 }
439
440 rb_link_node(&se->rb_node, parent, p);
441 rb_insert_color(&se->rb_node, tree);
442}
Ingo Molnar07800602009-04-20 15:00:56 +0200443
444static void print_sym_table(void)
445{
Ingo Molnar233f0b92009-06-03 21:48:40 +0200446 int printed = 0, j;
Mike Galbraith46ab9762009-07-24 10:09:50 +0200447 int counter, snap = !display_weighted ? sym_counter : 0;
Ingo Molnar2debbc82009-06-05 14:29:10 +0200448 float samples_per_sec = samples/delay_secs;
449 float ksamples_per_sec = (samples-userspace_samples)/delay_secs;
450 float sum_ksamples = 0.0;
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300451 struct sym_entry *syme, *n;
452 struct rb_root tmp = RB_ROOT;
453 struct rb_node *nd;
Arnaldo Carvalho de Melo7cc017e2009-11-24 12:05:14 -0200454 int sym_width = 0, dso_width = 0, max_dso_width;
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200455 const int win_width = winsize.ws_col - 1;
Ingo Molnar07800602009-04-20 15:00:56 +0200456
Ingo Molnar2debbc82009-06-05 14:29:10 +0200457 samples = userspace_samples = 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200458
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300459 /* Sort the active symbols */
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300460 pthread_mutex_lock(&active_symbols_lock);
461 syme = list_entry(active_symbols.next, struct sym_entry, node);
462 pthread_mutex_unlock(&active_symbols_lock);
463
464 list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
Mike Galbraith46ab9762009-07-24 10:09:50 +0200465 syme->snap_count = syme->count[snap];
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300466 if (syme->snap_count != 0) {
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200467
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200468 if ((hide_user_symbols &&
469 syme->origin == PERF_RECORD_MISC_USER) ||
470 (hide_kernel_symbols &&
471 syme->origin == PERF_RECORD_MISC_KERNEL)) {
472 list_remove_active_sym(syme);
473 continue;
474 }
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300475 syme->weight = sym_weight(syme);
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300476 rb_insert_active_sym(&tmp, syme);
Ingo Molnar2debbc82009-06-05 14:29:10 +0200477 sum_ksamples += syme->snap_count;
Mike Galbraithd94b9432009-05-25 09:57:56 +0200478
479 for (j = 0; j < nr_counters; j++)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300480 syme->count[j] = zero ? 0 : syme->count[j] * 7 / 8;
481 } else
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300482 list_remove_active_sym(syme);
Mike Galbraithd94b9432009-05-25 09:57:56 +0200483 }
484
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200485 puts(CONSOLE_CLEAR);
Ingo Molnar07800602009-04-20 15:00:56 +0200486
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200487 printf("%-*.*s\n", win_width, win_width, graph_dotted_line);
Ingo Molnarf2521b62009-06-03 19:17:25 +0200488 printf( " PerfTop:%8.0f irqs/sec kernel:%4.1f%% [",
Ingo Molnar2debbc82009-06-05 14:29:10 +0200489 samples_per_sec,
490 100.0 - (100.0*((samples_per_sec-ksamples_per_sec)/samples_per_sec)));
Ingo Molnar07800602009-04-20 15:00:56 +0200491
Mike Galbraith46ab9762009-07-24 10:09:50 +0200492 if (nr_counters == 1 || !display_weighted) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000493 printf("%Ld", (u64)attrs[0].sample_period);
Ingo Molnarcf1f4572009-06-05 13:27:02 +0200494 if (freq)
495 printf("Hz ");
496 else
497 printf(" ");
498 }
Ingo Molnar07800602009-04-20 15:00:56 +0200499
Mike Galbraith46ab9762009-07-24 10:09:50 +0200500 if (!display_weighted)
501 printf("%s", event_name(sym_counter));
502 else for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnar07800602009-04-20 15:00:56 +0200503 if (counter)
504 printf("/");
505
506 printf("%s", event_name(counter));
507 }
508
509 printf( "], ");
510
Ingo Molnarb456bae2009-05-26 09:17:18 +0200511 if (target_pid != -1)
512 printf(" (target_pid: %d", target_pid);
Ingo Molnar07800602009-04-20 15:00:56 +0200513 else
514 printf(" (all");
515
516 if (profile_cpu != -1)
517 printf(", cpu: %d)\n", profile_cpu);
518 else {
Ingo Molnarb456bae2009-05-26 09:17:18 +0200519 if (target_pid != -1)
Ingo Molnar07800602009-04-20 15:00:56 +0200520 printf(")\n");
521 else
522 printf(", %d CPUs)\n", nr_cpus);
523 }
524
Arnaldo Carvalho de Melo1a105f72009-11-17 15:40:55 -0200525 printf("%-*.*s\n", win_width, win_width, graph_dotted_line);
Ingo Molnar07800602009-04-20 15:00:56 +0200526
Mike Galbraith923c42c2009-07-22 20:36:03 +0200527 if (sym_filter_entry) {
528 show_details(sym_filter_entry);
529 return;
530 }
531
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200532 /*
533 * Find the longest symbol name that will be displayed
534 */
535 for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
536 syme = rb_entry(nd, struct sym_entry, rb_node);
537 if (++printed > print_entries ||
538 (int)syme->snap_count < count_filter)
539 continue;
540
Arnaldo Carvalho de Melo1a105f72009-11-17 15:40:55 -0200541 if (syme->map->dso->long_name_len > dso_width)
542 dso_width = syme->map->dso->long_name_len;
543
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200544 if (syme->name_len > sym_width)
545 sym_width = syme->name_len;
546 }
547
548 printed = 0;
549
Arnaldo Carvalho de Melo7cc017e2009-11-24 12:05:14 -0200550 max_dso_width = winsize.ws_col - sym_width - 29;
551 if (dso_width > max_dso_width)
552 dso_width = max_dso_width;
553 putchar('\n');
Ingo Molnar07800602009-04-20 15:00:56 +0200554 if (nr_counters == 1)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200555 printf(" samples pcnt");
Ingo Molnar07800602009-04-20 15:00:56 +0200556 else
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200557 printf(" weight samples pcnt");
Ingo Molnar07800602009-04-20 15:00:56 +0200558
Arnaldo Carvalho de Melo7ced1562009-08-26 11:51:26 -0300559 if (verbose)
560 printf(" RIP ");
Arnaldo Carvalho de Melo7cc017e2009-11-24 12:05:14 -0200561 printf(" %-*.*s DSO\n", sym_width, sym_width, "function");
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200562 printf(" %s _______ _____",
Arnaldo Carvalho de Melo7ced1562009-08-26 11:51:26 -0300563 nr_counters == 1 ? " " : "______");
564 if (verbose)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200565 printf(" ________________");
Arnaldo Carvalho de Melo1a105f72009-11-17 15:40:55 -0200566 printf(" %-*.*s", sym_width, sym_width, graph_line);
Arnaldo Carvalho de Melo7cc017e2009-11-24 12:05:14 -0200567 printf(" %-*.*s", dso_width, dso_width, graph_line);
Arnaldo Carvalho de Melo1a105f72009-11-17 15:40:55 -0200568 puts("\n");
Ingo Molnar07800602009-04-20 15:00:56 +0200569
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300570 for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
Ingo Molnar83a09442009-08-15 12:26:57 +0200571 struct symbol *sym;
Ingo Molnar8fc03212009-06-04 15:19:47 +0200572 double pcnt;
Ingo Molnar07800602009-04-20 15:00:56 +0200573
Ingo Molnar83a09442009-08-15 12:26:57 +0200574 syme = rb_entry(nd, struct sym_entry, rb_node);
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200575 sym = sym_entry__symbol(syme);
Ingo Molnar83a09442009-08-15 12:26:57 +0200576
Mike Galbraith923c42c2009-07-22 20:36:03 +0200577 if (++printed > print_entries || (int)syme->snap_count < count_filter)
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300578 continue;
Ingo Molnar07800602009-04-20 15:00:56 +0200579
Ingo Molnar2debbc82009-06-05 14:29:10 +0200580 pcnt = 100.0 - (100.0 * ((sum_ksamples - syme->snap_count) /
581 sum_ksamples));
Mike Galbraithd94b9432009-05-25 09:57:56 +0200582
Mike Galbraith46ab9762009-07-24 10:09:50 +0200583 if (nr_counters == 1 || !display_weighted)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200584 printf("%20.2f ", syme->weight);
Mike Galbraithd94b9432009-05-25 09:57:56 +0200585 else
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200586 printf("%9.1f %10ld ", syme->weight, syme->snap_count);
Ingo Molnar8fc03212009-06-04 15:19:47 +0200587
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200588 percent_color_fprintf(stdout, "%4.1f%%", pcnt);
Arnaldo Carvalho de Melo7ced1562009-08-26 11:51:26 -0300589 if (verbose)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200590 printf(" %016llx", sym->start);
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200591 printf(" %-*.*s", sym_width, sym_width, sym->name);
Arnaldo Carvalho de Melo7cc017e2009-11-24 12:05:14 -0200592 printf(" %-*.*s\n", dso_width, dso_width,
593 dso_width >= syme->map->dso->long_name_len ?
594 syme->map->dso->long_name :
595 syme->map->dso->short_name);
Ingo Molnar07800602009-04-20 15:00:56 +0200596 }
Ingo Molnar07800602009-04-20 15:00:56 +0200597}
598
Mike Galbraith923c42c2009-07-22 20:36:03 +0200599static void prompt_integer(int *target, const char *msg)
600{
601 char *buf = malloc(0), *p;
602 size_t dummy = 0;
603 int tmp;
604
605 fprintf(stdout, "\n%s: ", msg);
606 if (getline(&buf, &dummy, stdin) < 0)
607 return;
608
609 p = strchr(buf, '\n');
610 if (p)
611 *p = 0;
612
613 p = buf;
614 while(*p) {
615 if (!isdigit(*p))
616 goto out_free;
617 p++;
618 }
619 tmp = strtoul(buf, NULL, 10);
620 *target = tmp;
621out_free:
622 free(buf);
623}
624
625static void prompt_percent(int *target, const char *msg)
626{
627 int tmp = 0;
628
629 prompt_integer(&tmp, msg);
630 if (tmp >= 0 && tmp <= 100)
631 *target = tmp;
632}
633
634static void prompt_symbol(struct sym_entry **target, const char *msg)
635{
636 char *buf = malloc(0), *p;
637 struct sym_entry *syme = *target, *n, *found = NULL;
638 size_t dummy = 0;
639
640 /* zero counters of active symbol */
641 if (syme) {
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200642 pthread_mutex_lock(&syme->src->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200643 __zero_source_counters(syme);
644 *target = NULL;
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200645 pthread_mutex_unlock(&syme->src->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200646 }
647
648 fprintf(stdout, "\n%s: ", msg);
649 if (getline(&buf, &dummy, stdin) < 0)
650 goto out_free;
651
652 p = strchr(buf, '\n');
653 if (p)
654 *p = 0;
655
656 pthread_mutex_lock(&active_symbols_lock);
657 syme = list_entry(active_symbols.next, struct sym_entry, node);
658 pthread_mutex_unlock(&active_symbols_lock);
659
660 list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200661 struct symbol *sym = sym_entry__symbol(syme);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200662
663 if (!strcmp(buf, sym->name)) {
664 found = syme;
665 break;
666 }
667 }
668
669 if (!found) {
670 fprintf(stderr, "Sorry, %s is not active.\n", sym_filter);
671 sleep(1);
672 return;
673 } else
674 parse_source(found);
675
676out_free:
677 free(buf);
678}
679
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200680static void print_mapped_keys(void)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200681{
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200682 char *name = NULL;
683
684 if (sym_filter_entry) {
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200685 struct symbol *sym = sym_entry__symbol(sym_filter_entry);
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200686 name = sym->name;
687 }
688
689 fprintf(stdout, "\nMapped keys:\n");
690 fprintf(stdout, "\t[d] display refresh delay. \t(%d)\n", delay_secs);
691 fprintf(stdout, "\t[e] display entries (lines). \t(%d)\n", print_entries);
692
693 if (nr_counters > 1)
694 fprintf(stdout, "\t[E] active event counter. \t(%s)\n", event_name(sym_counter));
695
696 fprintf(stdout, "\t[f] profile display filter (count). \t(%d)\n", count_filter);
697
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -0200698 if (symbol_conf.vmlinux_name) {
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200699 fprintf(stdout, "\t[F] annotate display filter (percent). \t(%d%%)\n", sym_pcnt_filter);
700 fprintf(stdout, "\t[s] annotate symbol. \t(%s)\n", name?: "NULL");
701 fprintf(stdout, "\t[S] stop annotation.\n");
702 }
703
704 if (nr_counters > 1)
705 fprintf(stdout, "\t[w] toggle display weighted/count[E]r. \t(%d)\n", display_weighted ? 1 : 0);
706
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200707 fprintf(stdout,
708 "\t[K] hide kernel_symbols symbols. \t(%s)\n",
709 hide_kernel_symbols ? "yes" : "no");
710 fprintf(stdout,
711 "\t[U] hide user symbols. \t(%s)\n",
712 hide_user_symbols ? "yes" : "no");
Mike Galbraith46ab9762009-07-24 10:09:50 +0200713 fprintf(stdout, "\t[z] toggle sample zeroing. \t(%d)\n", zero ? 1 : 0);
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200714 fprintf(stdout, "\t[qQ] quit.\n");
715}
716
717static int key_mapped(int c)
718{
719 switch (c) {
720 case 'd':
721 case 'e':
722 case 'f':
723 case 'z':
724 case 'q':
725 case 'Q':
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200726 case 'K':
727 case 'U':
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200728 return 1;
729 case 'E':
730 case 'w':
731 return nr_counters > 1 ? 1 : 0;
732 case 'F':
733 case 's':
734 case 'S':
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -0200735 return symbol_conf.vmlinux_name ? 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;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200913 if (!sym_filter_entry && sym_filter && !strcmp(name, sym_filter))
914 sym_filter_entry = syme;
915
Anton Blanchard2ab52082009-07-01 09:00:46 +1000916 for (i = 0; skip_symbols[i]; i++) {
917 if (!strcmp(skip_symbols[i], name)) {
918 syme->skip = 1;
919 break;
920 }
921 }
Ingo Molnar07800602009-04-20 15:00:56 +0200922
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200923 if (!syme->skip)
924 syme->name_len = strlen(sym->name);
925
Ingo Molnar07800602009-04-20 15:00:56 +0200926 return 0;
927}
928
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200929static void event__process_sample(const event_t *self, int counter)
Ingo Molnar07800602009-04-20 15:00:56 +0200930{
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200931 u64 ip = self->ip.ip;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200932 struct sym_entry *syme;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200933 struct addr_location al;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200934 u8 origin = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Ingo Molnar07800602009-04-20 15:00:56 +0200935
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200936 switch (origin) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200937 case PERF_RECORD_MISC_USER:
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200938 if (hide_user_symbols)
939 return;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200940 break;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200941 case PERF_RECORD_MISC_KERNEL:
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200942 if (hide_kernel_symbols)
943 return;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200944 break;
945 default:
Ingo Molnar07800602009-04-20 15:00:56 +0200946 return;
947 }
948
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200949 if (event__preprocess_sample(self, &al, symbol_filter) < 0 ||
950 al.sym == NULL)
951 return;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200952
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200953 syme = symbol__priv(al.sym);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200954 if (!syme->skip) {
955 syme->count[counter]++;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200956 syme->origin = origin;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200957 record_precise_ip(syme, counter, ip);
958 pthread_mutex_lock(&active_symbols_lock);
959 if (list_empty(&syme->node) || !syme->node.next)
960 __list_insert_active_sym(syme);
961 pthread_mutex_unlock(&active_symbols_lock);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200962 if (origin == PERF_RECORD_MISC_USER)
963 ++userspace_samples;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200964 ++samples;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200965 }
966}
967
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200968static int event__process(event_t *event)
969{
970 switch (event->header.type) {
971 case PERF_RECORD_COMM:
972 event__process_comm(event);
973 break;
974 case PERF_RECORD_MMAP:
975 event__process_mmap(event);
976 break;
977 default:
978 break;
979 }
980
981 return 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200982}
983
Ingo Molnar07800602009-04-20 15:00:56 +0200984struct mmap_data {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200985 int counter;
986 void *base;
Ingo Molnarf37a2912009-07-01 12:37:06 +0200987 int mask;
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200988 unsigned int prev;
Ingo Molnar07800602009-04-20 15:00:56 +0200989};
990
991static unsigned int mmap_read_head(struct mmap_data *md)
992{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200993 struct perf_event_mmap_page *pc = md->base;
Ingo Molnar07800602009-04-20 15:00:56 +0200994 int head;
995
996 head = pc->data_head;
997 rmb();
998
999 return head;
1000}
1001
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001002static void mmap_read_counter(struct mmap_data *md)
Ingo Molnar07800602009-04-20 15:00:56 +02001003{
1004 unsigned int head = mmap_read_head(md);
1005 unsigned int old = md->prev;
1006 unsigned char *data = md->base + page_size;
1007 int diff;
1008
Ingo Molnar07800602009-04-20 15:00:56 +02001009 /*
1010 * If we're further behind than half the buffer, there's a chance
Ingo Molnar2debbc82009-06-05 14:29:10 +02001011 * the writer will bite our tail and mess up the samples under us.
Ingo Molnar07800602009-04-20 15:00:56 +02001012 *
1013 * If we somehow ended up ahead of the head, we got messed up.
1014 *
1015 * In either case, truncate and restart at head.
1016 */
1017 diff = head - old;
1018 if (diff > md->mask / 2 || diff < 0) {
Mike Galbraithf4f0b412009-10-13 14:57:20 +02001019 fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
Ingo Molnar07800602009-04-20 15:00:56 +02001020
1021 /*
1022 * head points to a known good entry, start there.
1023 */
1024 old = head;
1025 }
1026
Ingo Molnar07800602009-04-20 15:00:56 +02001027 for (; old != head;) {
Ingo Molnar07800602009-04-20 15:00:56 +02001028 event_t *event = (event_t *)&data[old & md->mask];
1029
1030 event_t event_copy;
1031
Ingo Molnar6f06ccb2009-04-20 15:22:22 +02001032 size_t size = event->header.size;
Ingo Molnar07800602009-04-20 15:00:56 +02001033
1034 /*
1035 * Event straddles the mmap boundary -- header should always
1036 * be inside due to u64 alignment of output.
1037 */
1038 if ((old & md->mask) + size != ((old + size) & md->mask)) {
1039 unsigned int offset = old;
1040 unsigned int len = min(sizeof(*event), size), cpy;
1041 void *dst = &event_copy;
1042
1043 do {
1044 cpy = min(md->mask + 1 - (offset & md->mask), len);
1045 memcpy(dst, &data[offset & md->mask], cpy);
1046 offset += cpy;
1047 dst += cpy;
1048 len -= cpy;
1049 } while (len);
1050
1051 event = &event_copy;
1052 }
1053
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001054 if (event->header.type == PERF_RECORD_SAMPLE)
1055 event__process_sample(event, md->counter);
1056 else
1057 event__process(event);
Ingo Molnar07800602009-04-20 15:00:56 +02001058 old += size;
Ingo Molnar07800602009-04-20 15:00:56 +02001059 }
1060
1061 md->prev = old;
1062}
1063
Mike Galbraithc2990a22009-05-24 08:35:49 +02001064static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
1065static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
1066
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001067static void mmap_read(void)
1068{
1069 int i, counter;
1070
1071 for (i = 0; i < nr_cpus; i++) {
1072 for (counter = 0; counter < nr_counters; counter++)
1073 mmap_read_counter(&mmap_array[i][counter]);
1074 }
1075}
1076
Ingo Molnar716c69f2009-06-07 17:31:52 +02001077int nr_poll;
1078int group_fd;
1079
1080static void start_counter(int i, int counter)
Ingo Molnar07800602009-04-20 15:00:56 +02001081{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001082 struct perf_event_attr *attr;
Mike Galbraith0fdc7e62009-07-21 10:30:36 +02001083 int cpu;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001084
1085 cpu = profile_cpu;
1086 if (target_pid == -1 && profile_cpu == -1)
1087 cpu = i;
1088
1089 attr = attrs + counter;
1090
1091 attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +02001092
1093 if (freq) {
1094 attr->sample_type |= PERF_SAMPLE_PERIOD;
1095 attr->freq = 1;
1096 attr->sample_freq = freq;
1097 }
1098
Mike Galbraith0fdc7e62009-07-21 10:30:36 +02001099 attr->inherit = (cpu < 0) && inherit;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001100 attr->mmap = 1;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001101
1102try_again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001103 fd[i][counter] = sys_perf_event_open(attr, target_pid, cpu, group_fd, 0);
Ingo Molnar716c69f2009-06-07 17:31:52 +02001104
1105 if (fd[i][counter] < 0) {
1106 int err = errno;
1107
Pekka Enbergc10edee2009-11-08 18:01:06 +02001108 if (err == EPERM || err == EACCES)
Ingo Molnar3da297a2009-06-07 17:39:02 +02001109 die("No permission - are you root?\n");
Ingo Molnar716c69f2009-06-07 17:31:52 +02001110 /*
1111 * If it's cycles then fall back to hrtimer
1112 * based cpu-clock-tick sw counter, which
1113 * is always available even if no PMU support:
1114 */
1115 if (attr->type == PERF_TYPE_HARDWARE
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02001116 && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
Ingo Molnar716c69f2009-06-07 17:31:52 +02001117
Ingo Molnar3da297a2009-06-07 17:39:02 +02001118 if (verbose)
1119 warning(" ... trying to fall back to cpu-clock-ticks\n");
1120
Ingo Molnar716c69f2009-06-07 17:31:52 +02001121 attr->type = PERF_TYPE_SOFTWARE;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02001122 attr->config = PERF_COUNT_SW_CPU_CLOCK;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001123 goto try_again;
1124 }
Ingo Molnar30c806a2009-06-07 17:46:24 +02001125 printf("\n");
1126 error("perfcounter syscall returned with %d (%s)\n",
1127 fd[i][counter], strerror(err));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001128 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
Ingo Molnar716c69f2009-06-07 17:31:52 +02001129 exit(-1);
1130 }
1131 assert(fd[i][counter] >= 0);
1132 fcntl(fd[i][counter], F_SETFL, O_NONBLOCK);
1133
1134 /*
1135 * First counter acts as the group leader:
1136 */
1137 if (group && group_fd == -1)
1138 group_fd = fd[i][counter];
1139
1140 event_array[nr_poll].fd = fd[i][counter];
1141 event_array[nr_poll].events = POLLIN;
1142 nr_poll++;
1143
1144 mmap_array[i][counter].counter = counter;
1145 mmap_array[i][counter].prev = 0;
1146 mmap_array[i][counter].mask = mmap_pages*page_size - 1;
1147 mmap_array[i][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
1148 PROT_READ, MAP_SHARED, fd[i][counter], 0);
1149 if (mmap_array[i][counter].base == MAP_FAILED)
1150 die("failed to mmap with %d (%s)\n", errno, strerror(errno));
1151}
1152
1153static int __cmd_top(void)
1154{
1155 pthread_t thread;
1156 int i, counter;
Ingo Molnar07800602009-04-20 15:00:56 +02001157 int ret;
1158
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001159 if (target_pid != -1)
1160 event__synthesize_thread(target_pid, event__process);
1161 else
1162 event__synthesize_threads(event__process);
1163
Ingo Molnar07800602009-04-20 15:00:56 +02001164 for (i = 0; i < nr_cpus; i++) {
1165 group_fd = -1;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001166 for (counter = 0; counter < nr_counters; counter++)
1167 start_counter(i, counter);
Ingo Molnar07800602009-04-20 15:00:56 +02001168 }
1169
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001170 /* Wait for a minimal set of events before starting the snapshot */
1171 poll(event_array, nr_poll, 100);
1172
1173 mmap_read();
1174
Ingo Molnar07800602009-04-20 15:00:56 +02001175 if (pthread_create(&thread, NULL, display_thread, NULL)) {
1176 printf("Could not create display thread.\n");
1177 exit(-1);
1178 }
1179
1180 if (realtime_prio) {
1181 struct sched_param param;
1182
1183 param.sched_priority = realtime_prio;
1184 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
1185 printf("Could not set realtime priority.\n");
1186 exit(-1);
1187 }
1188 }
1189
1190 while (1) {
Ingo Molnar2debbc82009-06-05 14:29:10 +02001191 int hits = samples;
Ingo Molnar07800602009-04-20 15:00:56 +02001192
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001193 mmap_read();
Ingo Molnar07800602009-04-20 15:00:56 +02001194
Ingo Molnar2debbc82009-06-05 14:29:10 +02001195 if (hits == samples)
Ingo Molnar07800602009-04-20 15:00:56 +02001196 ret = poll(event_array, nr_poll, 100);
1197 }
1198
1199 return 0;
1200}
Ingo Molnarb456bae2009-05-26 09:17:18 +02001201
1202static const char * const top_usage[] = {
1203 "perf top [<options>]",
1204 NULL
1205};
1206
Ingo Molnarb456bae2009-05-26 09:17:18 +02001207static const struct option options[] = {
1208 OPT_CALLBACK('e', "event", NULL, "event",
Thomas Gleixner86847b62009-06-06 12:24:17 +02001209 "event selector. use 'perf list' to list available events",
1210 parse_events),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001211 OPT_INTEGER('c', "count", &default_interval,
1212 "event period to sample"),
1213 OPT_INTEGER('p', "pid", &target_pid,
1214 "profile events on existing pid"),
1215 OPT_BOOLEAN('a', "all-cpus", &system_wide,
1216 "system-wide collection from all CPUs"),
1217 OPT_INTEGER('C', "CPU", &profile_cpu,
1218 "CPU to profile on"),
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02001219 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1220 "file", "vmlinux pathname"),
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -02001221 OPT_BOOLEAN('K', "hide_kernel_symbols", &hide_kernel_symbols,
1222 "hide kernel symbols"),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001223 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
1224 "number of mmap data pages"),
1225 OPT_INTEGER('r', "realtime", &realtime_prio,
1226 "collect data with this RT SCHED_FIFO priority"),
Mike Galbraithdb20c002009-05-26 15:25:34 +02001227 OPT_INTEGER('d', "delay", &delay_secs,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001228 "number of seconds to delay between refreshes"),
1229 OPT_BOOLEAN('D', "dump-symtab", &dump_symtab,
1230 "dump the symbol table used for profiling"),
Ingo Molnar6e53cdf2009-06-04 08:53:05 +02001231 OPT_INTEGER('f', "count-filter", &count_filter,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001232 "only display functions with more events than this"),
1233 OPT_BOOLEAN('g', "group", &group,
1234 "put the counters into a counter group"),
Mike Galbraith0fdc7e62009-07-21 10:30:36 +02001235 OPT_BOOLEAN('i', "inherit", &inherit,
1236 "child tasks inherit counters"),
Mike Galbraith923c42c2009-07-22 20:36:03 +02001237 OPT_STRING('s', "sym-annotate", &sym_filter, "symbol name",
1238 "symbol to annotate - requires -k option"),
Anton Blanchard1f208ea2009-07-01 09:00:44 +10001239 OPT_BOOLEAN('z', "zero", &zero,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001240 "zero history across updates"),
Ingo Molnar6e53cdf2009-06-04 08:53:05 +02001241 OPT_INTEGER('F', "freq", &freq,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001242 "profile at this frequency"),
Ingo Molnar6e53cdf2009-06-04 08:53:05 +02001243 OPT_INTEGER('E', "entries", &print_entries,
1244 "display this many functions"),
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -02001245 OPT_BOOLEAN('U', "hide_user_symbols", &hide_user_symbols,
1246 "hide user symbols"),
Ingo Molnar3da297a2009-06-07 17:39:02 +02001247 OPT_BOOLEAN('v', "verbose", &verbose,
1248 "be more verbose (show counter open errors, etc)"),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001249 OPT_END()
1250};
1251
Ingo Molnarf37a2912009-07-01 12:37:06 +02001252int cmd_top(int argc, const char **argv, const char *prefix __used)
Ingo Molnarb456bae2009-05-26 09:17:18 +02001253{
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02001254 int counter;
Ingo Molnarb456bae2009-05-26 09:17:18 +02001255
1256 page_size = sysconf(_SC_PAGE_SIZE);
1257
Ingo Molnarb456bae2009-05-26 09:17:18 +02001258 argc = parse_options(argc, argv, options, top_usage, 0);
1259 if (argc)
1260 usage_with_options(top_usage, options);
1261
Ingo Molnarb456bae2009-05-26 09:17:18 +02001262 /* CPU and PID are mutually exclusive */
1263 if (target_pid != -1 && profile_cpu != -1) {
1264 printf("WARNING: PID switch overriding CPU\n");
1265 sleep(1);
1266 profile_cpu = -1;
1267 }
1268
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001269 if (!nr_counters)
Ingo Molnarb456bae2009-05-26 09:17:18 +02001270 nr_counters = 1;
Ingo Molnarb456bae2009-05-26 09:17:18 +02001271
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02001272 symbol_conf.priv_size = (sizeof(struct sym_entry) +
1273 (nr_counters + 1) * sizeof(unsigned long));
1274 if (symbol_conf.vmlinux_name == NULL)
1275 symbol_conf.try_vmlinux_path = true;
1276 if (symbol__init(&symbol_conf) < 0)
1277 return -1;
Arnaldo Carvalho de Melo5a8e5a32009-11-17 18:38:01 -02001278
Frederic Weisbecker2f335a02009-06-05 19:31:01 +02001279 if (delay_secs < 1)
1280 delay_secs = 1;
1281
Mike Galbraith923c42c2009-07-22 20:36:03 +02001282 parse_source(sym_filter_entry);
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001283
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +02001284 /*
1285 * User specified count overrides default frequency.
1286 */
1287 if (default_interval)
1288 freq = 0;
1289 else if (freq) {
1290 default_interval = freq;
1291 } else {
1292 fprintf(stderr, "frequency and count are zero, aborting\n");
1293 exit(EXIT_FAILURE);
1294 }
1295
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001296 /*
1297 * Fill in the ones not specifically initialized via -c:
1298 */
Ingo Molnarb456bae2009-05-26 09:17:18 +02001299 for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001300 if (attrs[counter].sample_period)
Ingo Molnarb456bae2009-05-26 09:17:18 +02001301 continue;
1302
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001303 attrs[counter].sample_period = default_interval;
Ingo Molnarb456bae2009-05-26 09:17:18 +02001304 }
1305
1306 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
1307 assert(nr_cpus <= MAX_NR_CPUS);
1308 assert(nr_cpus >= 0);
1309
1310 if (target_pid != -1 || profile_cpu != -1)
1311 nr_cpus = 1;
1312
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -02001313 get_term_dimensions(&winsize);
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -02001314 if (print_entries == 0) {
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -02001315 update_print_entries(&winsize);
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -02001316 signal(SIGWINCH, sig_winch_handler);
1317 }
1318
Ingo Molnarb456bae2009-05-26 09:17:18 +02001319 return __cmd_top();
1320}