blob: 0f7a4da2924c7e0edeb89b1b67dd1909a56e3f37 [file] [log] [blame]
Ingo Molnar07800602009-04-20 15:00:56 +02001/*
Ingo Molnarbf9e1872009-06-02 23:37:05 +02002 * builtin-top.c
3 *
4 * Builtin top command: Display a continuously updated profile of
5 * any workload, CPU or specific PID.
6 *
7 * Copyright (C) 2008, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
8 *
9 * Improvements and fixes by:
10 *
11 * Arjan van de Ven <arjan@linux.intel.com>
12 * Yanmin Zhang <yanmin.zhang@intel.com>
13 * Wu Fengguang <fengguang.wu@intel.com>
14 * Mike Galbraith <efault@gmx.de>
15 * Paul Mackerras <paulus@samba.org>
16 *
17 * Released under the GPL v2. (and only v2, not any later version)
Ingo Molnar07800602009-04-20 15:00:56 +020018 */
Ingo Molnarbf9e1872009-06-02 23:37:05 +020019#include "builtin.h"
Ingo Molnar07800602009-04-20 15:00:56 +020020
Peter Zijlstra1a482f32009-05-23 18:28:58 +020021#include "perf.h"
Ingo Molnarbf9e1872009-06-02 23:37:05 +020022
Ingo Molnar8fc03212009-06-04 15:19:47 +020023#include "util/color.h"
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -020024#include "util/session.h"
25#include "util/symbol.h"
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030026#include "util/thread.h"
Ingo Molnar148be2c2009-04-27 08:02:14 +020027#include "util/util.h"
Arnaldo Carvalho de Melo43cbcd82009-07-01 12:28:37 -030028#include <linux/rbtree.h>
Ingo Molnarb456bae2009-05-26 09:17:18 +020029#include "util/parse-options.h"
30#include "util/parse-events.h"
Ingo Molnar07800602009-04-20 15:00:56 +020031
Frederic Weisbecker8f288272009-08-16 22:05:48 +020032#include "util/debug.h"
33
Ingo Molnar07800602009-04-20 15:00:56 +020034#include <assert.h>
35#include <fcntl.h>
Ingo Molnar0e9b20b2009-05-26 09:17:18 +020036
Ingo Molnar07800602009-04-20 15:00:56 +020037#include <stdio.h>
Mike Galbraith923c42c2009-07-22 20:36:03 +020038#include <termios.h>
39#include <unistd.h>
Ingo Molnar0e9b20b2009-05-26 09:17:18 +020040
Ingo Molnar07800602009-04-20 15:00:56 +020041#include <errno.h>
Ingo Molnar07800602009-04-20 15:00:56 +020042#include <time.h>
43#include <sched.h>
44#include <pthread.h>
45
46#include <sys/syscall.h>
47#include <sys/ioctl.h>
48#include <sys/poll.h>
49#include <sys/prctl.h>
50#include <sys/wait.h>
51#include <sys/uio.h>
52#include <sys/mman.h>
53
54#include <linux/unistd.h>
55#include <linux/types.h>
56
Ingo Molnara21ca2c2009-06-06 09:58:57 +020057static int fd[MAX_NR_CPUS][MAX_COUNTERS];
58
Ingo Molnar42e59d72009-10-06 15:14:21 +020059static int system_wide = 0;
Ingo Molnar07800602009-04-20 15:00:56 +020060
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +020061static int default_interval = 0;
Ingo Molnar07800602009-04-20 15:00:56 +020062
Ingo Molnar42e59d72009-10-06 15:14:21 +020063static int count_filter = 5;
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -020064static int print_entries;
Ingo Molnar07800602009-04-20 15:00:56 +020065
Ingo Molnar42e59d72009-10-06 15:14:21 +020066static int target_pid = -1;
67static int inherit = 0;
68static int profile_cpu = -1;
69static int nr_cpus = 0;
70static unsigned int realtime_prio = 0;
71static int group = 0;
Ingo Molnar07800602009-04-20 15:00:56 +020072static unsigned int page_size;
Ingo Molnar42e59d72009-10-06 15:14:21 +020073static unsigned int mmap_pages = 16;
74static int freq = 1000; /* 1 KHz */
Ingo Molnar07800602009-04-20 15:00:56 +020075
Ingo Molnar42e59d72009-10-06 15:14:21 +020076static int delay_secs = 2;
77static int zero = 0;
78static int dump_symtab = 0;
Ingo Molnar07800602009-04-20 15:00:56 +020079
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -020080static bool hide_kernel_symbols = false;
81static bool hide_user_symbols = false;
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -020082static struct winsize winsize;
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -020083struct symbol_conf symbol_conf;
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;
98static int sym_pcnt_filter = 5;
99static int sym_counter = 0;
100static int display_weighted = -1;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200101
102/*
Ingo Molnar07800602009-04-20 15:00:56 +0200103 * Symbols
104 */
105
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200106struct sym_entry_source {
107 struct source_line *source;
108 struct source_line *lines;
109 struct source_line **lines_tail;
110 pthread_mutex_t lock;
111};
112
Ingo Molnar07800602009-04-20 15:00:56 +0200113struct sym_entry {
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300114 struct rb_node rb_node;
115 struct list_head node;
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300116 unsigned long snap_count;
117 double weight;
Ingo Molnar07800602009-04-20 15:00:56 +0200118 int skip;
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200119 u16 name_len;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200120 u8 origin;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300121 struct map *map;
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200122 struct sym_entry_source *src;
Arnaldo Carvalho de Melo5a8e5a32009-11-17 18:38:01 -0200123 unsigned long count[0];
Ingo Molnar07800602009-04-20 15:00:56 +0200124};
125
Mike Galbraith923c42c2009-07-22 20:36:03 +0200126/*
127 * Source functions
128 */
129
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200130static inline struct symbol *sym_entry__symbol(struct sym_entry *self)
131{
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -0200132 return ((void *)self) + symbol_conf.priv_size;
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200133}
134
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200135static void get_term_dimensions(struct winsize *ws)
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200136{
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200137 char *s = getenv("LINES");
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200138
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200139 if (s != NULL) {
140 ws->ws_row = atoi(s);
141 s = getenv("COLUMNS");
142 if (s != NULL) {
143 ws->ws_col = atoi(s);
144 if (ws->ws_row && ws->ws_col)
145 return;
146 }
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200147 }
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200148#ifdef TIOCGWINSZ
149 if (ioctl(1, TIOCGWINSZ, ws) == 0 &&
150 ws->ws_row && ws->ws_col)
151 return;
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200152#endif
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200153 ws->ws_row = 25;
154 ws->ws_col = 80;
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200155}
156
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200157static void update_print_entries(struct winsize *ws)
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200158{
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200159 print_entries = ws->ws_row;
160
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200161 if (print_entries > 9)
162 print_entries -= 9;
163}
164
165static void sig_winch_handler(int sig __used)
166{
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200167 get_term_dimensions(&winsize);
168 update_print_entries(&winsize);
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200169}
170
Mike Galbraith923c42c2009-07-22 20:36:03 +0200171static void parse_source(struct sym_entry *syme)
172{
173 struct symbol *sym;
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200174 struct sym_entry_source *source;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300175 struct map *map;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200176 FILE *file;
Ingo Molnar83a09442009-08-15 12:26:57 +0200177 char command[PATH_MAX*2];
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300178 const char *path;
179 u64 len;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200180
181 if (!syme)
182 return;
183
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200184 if (syme->src == NULL) {
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200185 syme->src = zalloc(sizeof(*source));
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200186 if (syme->src == NULL)
187 return;
188 pthread_mutex_init(&syme->src->lock, NULL);
189 }
190
191 source = syme->src;
192
193 if (source->lines) {
194 pthread_mutex_lock(&source->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200195 goto out_assign;
196 }
197
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200198 sym = sym_entry__symbol(syme);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300199 map = syme->map;
200 path = map->dso->long_name;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200201
Mike Galbraith923c42c2009-07-22 20:36:03 +0200202 len = sym->end - sym->start;
203
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300204 sprintf(command,
205 "objdump --start-address=0x%016Lx "
206 "--stop-address=0x%016Lx -dS %s",
Arnaldo Carvalho de Meloc88e4bf2009-10-20 15:54:55 -0200207 map->unmap_ip(map, sym->start),
208 map->unmap_ip(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;
219 char *c;
220
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
238 if (strlen(src->line)>8 && src->line[8] == ':') {
239 src->eip = strtoull(src->line, NULL, 16);
Arnaldo Carvalho de Meloc88e4bf2009-10-20 15:54:55 -0200240 src->eip = map->unmap_ip(map, src->eip);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200241 }
242 if (strlen(src->line)>8 && src->line[16] == ':') {
243 src->eip = strtoull(src->line, NULL, 16);
Arnaldo Carvalho de Meloc88e4bf2009-10-20 15:54:55 -0200244 src->eip = map->unmap_ip(map, src->eip);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200245 }
246 }
247 pclose(file);
248out_assign:
249 sym_filter_entry = syme;
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200250 pthread_mutex_unlock(&source->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200251}
252
253static void __zero_source_counters(struct sym_entry *syme)
254{
255 int i;
256 struct source_line *line;
257
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200258 line = syme->src->lines;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200259 while (line) {
260 for (i = 0; i < nr_counters; i++)
261 line->count[i] = 0;
262 line = line->next;
263 }
264}
265
266static void record_precise_ip(struct sym_entry *syme, int counter, u64 ip)
267{
268 struct source_line *line;
269
270 if (syme != sym_filter_entry)
271 return;
272
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200273 if (pthread_mutex_trylock(&syme->src->lock))
Mike Galbraith923c42c2009-07-22 20:36:03 +0200274 return;
275
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200276 if (syme->src == NULL || syme->src->source == NULL)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200277 goto out_unlock;
278
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200279 for (line = syme->src->lines; line; line = line->next) {
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
291static void lookup_sym_source(struct sym_entry *syme)
292{
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200293 struct symbol *symbol = sym_entry__symbol(syme);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200294 struct source_line *line;
295 char pattern[PATH_MAX];
Mike Galbraith923c42c2009-07-22 20:36:03 +0200296
297 sprintf(pattern, "<%s>:", symbol->name);
298
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200299 pthread_mutex_lock(&syme->src->lock);
300 for (line = syme->src->lines; line; line = line->next) {
Mike Galbraith923c42c2009-07-22 20:36:03 +0200301 if (strstr(line->line, pattern)) {
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200302 syme->src->source = line;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200303 break;
304 }
305 }
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200306 pthread_mutex_unlock(&syme->src->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200307}
308
309static void show_lines(struct source_line *queue, int count, int total)
310{
311 int i;
312 struct source_line *line;
313
314 line = queue;
315 for (i = 0; i < count; i++) {
316 float pcnt = 100.0*(float)line->count[sym_counter]/(float)total;
317
318 printf("%8li %4.1f%%\t%s\n", line->count[sym_counter], pcnt, line->line);
319 line = line->next;
320 }
321}
322
323#define TRACE_COUNT 3
324
325static void show_details(struct sym_entry *syme)
326{
327 struct symbol *symbol;
328 struct source_line *line;
329 struct source_line *line_queue = NULL;
330 int displayed = 0;
331 int line_queue_count = 0, total = 0, more = 0;
332
333 if (!syme)
334 return;
335
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200336 if (!syme->src->source)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200337 lookup_sym_source(syme);
338
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200339 if (!syme->src->source)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200340 return;
341
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200342 symbol = sym_entry__symbol(syme);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200343 printf("Showing %s for %s\n", event_name(sym_counter), symbol->name);
344 printf(" Events Pcnt (>=%d%%)\n", sym_pcnt_filter);
345
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200346 pthread_mutex_lock(&syme->src->lock);
347 line = syme->src->source;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200348 while (line) {
349 total += line->count[sym_counter];
350 line = line->next;
351 }
352
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200353 line = syme->src->source;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200354 while (line) {
355 float pcnt = 0.0;
356
357 if (!line_queue_count)
358 line_queue = line;
359 line_queue_count++;
360
361 if (line->count[sym_counter])
362 pcnt = 100.0 * line->count[sym_counter] / (float)total;
363 if (pcnt >= (float)sym_pcnt_filter) {
364 if (displayed <= print_entries)
365 show_lines(line_queue, line_queue_count, total);
366 else more++;
367 displayed += line_queue_count;
368 line_queue_count = 0;
369 line_queue = NULL;
370 } else if (line_queue_count > TRACE_COUNT) {
371 line_queue = line_queue->next;
372 line_queue_count--;
373 }
374
375 line->count[sym_counter] = zero ? 0 : line->count[sym_counter] * 7 / 8;
376 line = line->next;
377 }
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200378 pthread_mutex_unlock(&syme->src->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200379 if (more)
380 printf("%d lines not displayed, maybe increase display entries [e]\n", more);
381}
Ingo Molnar07800602009-04-20 15:00:56 +0200382
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300383/*
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200384 * Symbols will be added here in event__process_sample and will get out
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300385 * after decayed.
386 */
387static LIST_HEAD(active_symbols);
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300388static pthread_mutex_t active_symbols_lock = PTHREAD_MUTEX_INITIALIZER;
Ingo Molnar07800602009-04-20 15:00:56 +0200389
Ingo Molnar07800602009-04-20 15:00:56 +0200390/*
391 * Ordering weight: count-1 * count-2 * ... / count-n
392 */
393static double sym_weight(const struct sym_entry *sym)
394{
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300395 double weight = sym->snap_count;
Ingo Molnar07800602009-04-20 15:00:56 +0200396 int counter;
397
Mike Galbraith46ab9762009-07-24 10:09:50 +0200398 if (!display_weighted)
399 return weight;
400
Ingo Molnar07800602009-04-20 15:00:56 +0200401 for (counter = 1; counter < nr_counters-1; counter++)
402 weight *= sym->count[counter];
403
404 weight /= (sym->count[counter] + 1);
405
406 return weight;
407}
408
Ingo Molnar2debbc82009-06-05 14:29:10 +0200409static long samples;
410static long userspace_samples;
Ingo Molnar07800602009-04-20 15:00:56 +0200411static const char CONSOLE_CLEAR[] = "";
412
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300413static void __list_insert_active_sym(struct sym_entry *syme)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300414{
415 list_add(&syme->node, &active_symbols);
416}
417
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300418static void list_remove_active_sym(struct sym_entry *syme)
419{
420 pthread_mutex_lock(&active_symbols_lock);
421 list_del_init(&syme->node);
422 pthread_mutex_unlock(&active_symbols_lock);
423}
424
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300425static void rb_insert_active_sym(struct rb_root *tree, struct sym_entry *se)
426{
427 struct rb_node **p = &tree->rb_node;
428 struct rb_node *parent = NULL;
429 struct sym_entry *iter;
430
431 while (*p != NULL) {
432 parent = *p;
433 iter = rb_entry(parent, struct sym_entry, rb_node);
434
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300435 if (se->weight > iter->weight)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300436 p = &(*p)->rb_left;
437 else
438 p = &(*p)->rb_right;
439 }
440
441 rb_link_node(&se->rb_node, parent, p);
442 rb_insert_color(&se->rb_node, tree);
443}
Ingo Molnar07800602009-04-20 15:00:56 +0200444
445static void print_sym_table(void)
446{
Ingo Molnar233f0b92009-06-03 21:48:40 +0200447 int printed = 0, j;
Mike Galbraith46ab9762009-07-24 10:09:50 +0200448 int counter, snap = !display_weighted ? sym_counter : 0;
Ingo Molnar2debbc82009-06-05 14:29:10 +0200449 float samples_per_sec = samples/delay_secs;
450 float ksamples_per_sec = (samples-userspace_samples)/delay_secs;
451 float sum_ksamples = 0.0;
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300452 struct sym_entry *syme, *n;
453 struct rb_root tmp = RB_ROOT;
454 struct rb_node *nd;
Arnaldo Carvalho de Melo7cc017e2009-11-24 12:05:14 -0200455 int sym_width = 0, dso_width = 0, max_dso_width;
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200456 const int win_width = winsize.ws_col - 1;
Ingo Molnar07800602009-04-20 15:00:56 +0200457
Ingo Molnar2debbc82009-06-05 14:29:10 +0200458 samples = userspace_samples = 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200459
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300460 /* Sort the active symbols */
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300461 pthread_mutex_lock(&active_symbols_lock);
462 syme = list_entry(active_symbols.next, struct sym_entry, node);
463 pthread_mutex_unlock(&active_symbols_lock);
464
465 list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
Mike Galbraith46ab9762009-07-24 10:09:50 +0200466 syme->snap_count = syme->count[snap];
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300467 if (syme->snap_count != 0) {
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200468
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200469 if ((hide_user_symbols &&
470 syme->origin == PERF_RECORD_MISC_USER) ||
471 (hide_kernel_symbols &&
472 syme->origin == PERF_RECORD_MISC_KERNEL)) {
473 list_remove_active_sym(syme);
474 continue;
475 }
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300476 syme->weight = sym_weight(syme);
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300477 rb_insert_active_sym(&tmp, syme);
Ingo Molnar2debbc82009-06-05 14:29:10 +0200478 sum_ksamples += syme->snap_count;
Mike Galbraithd94b9432009-05-25 09:57:56 +0200479
480 for (j = 0; j < nr_counters; j++)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300481 syme->count[j] = zero ? 0 : syme->count[j] * 7 / 8;
482 } else
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300483 list_remove_active_sym(syme);
Mike Galbraithd94b9432009-05-25 09:57:56 +0200484 }
485
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200486 puts(CONSOLE_CLEAR);
Ingo Molnar07800602009-04-20 15:00:56 +0200487
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200488 printf("%-*.*s\n", win_width, win_width, graph_dotted_line);
Ingo Molnarf2521b62009-06-03 19:17:25 +0200489 printf( " PerfTop:%8.0f irqs/sec kernel:%4.1f%% [",
Ingo Molnar2debbc82009-06-05 14:29:10 +0200490 samples_per_sec,
491 100.0 - (100.0*((samples_per_sec-ksamples_per_sec)/samples_per_sec)));
Ingo Molnar07800602009-04-20 15:00:56 +0200492
Mike Galbraith46ab9762009-07-24 10:09:50 +0200493 if (nr_counters == 1 || !display_weighted) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000494 printf("%Ld", (u64)attrs[0].sample_period);
Ingo Molnarcf1f4572009-06-05 13:27:02 +0200495 if (freq)
496 printf("Hz ");
497 else
498 printf(" ");
499 }
Ingo Molnar07800602009-04-20 15:00:56 +0200500
Mike Galbraith46ab9762009-07-24 10:09:50 +0200501 if (!display_weighted)
502 printf("%s", event_name(sym_counter));
503 else for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnar07800602009-04-20 15:00:56 +0200504 if (counter)
505 printf("/");
506
507 printf("%s", event_name(counter));
508 }
509
510 printf( "], ");
511
Ingo Molnarb456bae2009-05-26 09:17:18 +0200512 if (target_pid != -1)
513 printf(" (target_pid: %d", target_pid);
Ingo Molnar07800602009-04-20 15:00:56 +0200514 else
515 printf(" (all");
516
517 if (profile_cpu != -1)
518 printf(", cpu: %d)\n", profile_cpu);
519 else {
Ingo Molnarb456bae2009-05-26 09:17:18 +0200520 if (target_pid != -1)
Ingo Molnar07800602009-04-20 15:00:56 +0200521 printf(")\n");
522 else
523 printf(", %d CPUs)\n", nr_cpus);
524 }
525
Arnaldo Carvalho de Melo1a105f72009-11-17 15:40:55 -0200526 printf("%-*.*s\n", win_width, win_width, graph_dotted_line);
Ingo Molnar07800602009-04-20 15:00:56 +0200527
Mike Galbraith923c42c2009-07-22 20:36:03 +0200528 if (sym_filter_entry) {
529 show_details(sym_filter_entry);
530 return;
531 }
532
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200533 /*
534 * Find the longest symbol name that will be displayed
535 */
536 for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
537 syme = rb_entry(nd, struct sym_entry, rb_node);
538 if (++printed > print_entries ||
539 (int)syme->snap_count < count_filter)
540 continue;
541
Arnaldo Carvalho de Melo1a105f72009-11-17 15:40:55 -0200542 if (syme->map->dso->long_name_len > dso_width)
543 dso_width = syme->map->dso->long_name_len;
544
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200545 if (syme->name_len > sym_width)
546 sym_width = syme->name_len;
547 }
548
549 printed = 0;
550
Arnaldo Carvalho de Melo7cc017e2009-11-24 12:05:14 -0200551 max_dso_width = winsize.ws_col - sym_width - 29;
552 if (dso_width > max_dso_width)
553 dso_width = max_dso_width;
554 putchar('\n');
Ingo Molnar07800602009-04-20 15:00:56 +0200555 if (nr_counters == 1)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200556 printf(" samples pcnt");
Ingo Molnar07800602009-04-20 15:00:56 +0200557 else
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200558 printf(" weight samples pcnt");
Ingo Molnar07800602009-04-20 15:00:56 +0200559
Arnaldo Carvalho de Melo7ced1562009-08-26 11:51:26 -0300560 if (verbose)
561 printf(" RIP ");
Arnaldo Carvalho de Melo7cc017e2009-11-24 12:05:14 -0200562 printf(" %-*.*s DSO\n", sym_width, sym_width, "function");
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200563 printf(" %s _______ _____",
Arnaldo Carvalho de Melo7ced1562009-08-26 11:51:26 -0300564 nr_counters == 1 ? " " : "______");
565 if (verbose)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200566 printf(" ________________");
Arnaldo Carvalho de Melo1a105f72009-11-17 15:40:55 -0200567 printf(" %-*.*s", sym_width, sym_width, graph_line);
Arnaldo Carvalho de Melo7cc017e2009-11-24 12:05:14 -0200568 printf(" %-*.*s", dso_width, dso_width, graph_line);
Arnaldo Carvalho de Melo1a105f72009-11-17 15:40:55 -0200569 puts("\n");
Ingo Molnar07800602009-04-20 15:00:56 +0200570
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300571 for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
Ingo Molnar83a09442009-08-15 12:26:57 +0200572 struct symbol *sym;
Ingo Molnar8fc03212009-06-04 15:19:47 +0200573 double pcnt;
Ingo Molnar07800602009-04-20 15:00:56 +0200574
Ingo Molnar83a09442009-08-15 12:26:57 +0200575 syme = rb_entry(nd, struct sym_entry, rb_node);
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200576 sym = sym_entry__symbol(syme);
Ingo Molnar83a09442009-08-15 12:26:57 +0200577
Mike Galbraith923c42c2009-07-22 20:36:03 +0200578 if (++printed > print_entries || (int)syme->snap_count < count_filter)
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300579 continue;
Ingo Molnar07800602009-04-20 15:00:56 +0200580
Ingo Molnar2debbc82009-06-05 14:29:10 +0200581 pcnt = 100.0 - (100.0 * ((sum_ksamples - syme->snap_count) /
582 sum_ksamples));
Mike Galbraithd94b9432009-05-25 09:57:56 +0200583
Mike Galbraith46ab9762009-07-24 10:09:50 +0200584 if (nr_counters == 1 || !display_weighted)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200585 printf("%20.2f ", syme->weight);
Mike Galbraithd94b9432009-05-25 09:57:56 +0200586 else
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200587 printf("%9.1f %10ld ", syme->weight, syme->snap_count);
Ingo Molnar8fc03212009-06-04 15:19:47 +0200588
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200589 percent_color_fprintf(stdout, "%4.1f%%", pcnt);
Arnaldo Carvalho de Melo7ced1562009-08-26 11:51:26 -0300590 if (verbose)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200591 printf(" %016llx", sym->start);
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200592 printf(" %-*.*s", sym_width, sym_width, sym->name);
Arnaldo Carvalho de Melo7cc017e2009-11-24 12:05:14 -0200593 printf(" %-*.*s\n", dso_width, dso_width,
594 dso_width >= syme->map->dso->long_name_len ?
595 syme->map->dso->long_name :
596 syme->map->dso->short_name);
Ingo Molnar07800602009-04-20 15:00:56 +0200597 }
Ingo Molnar07800602009-04-20 15:00:56 +0200598}
599
Mike Galbraith923c42c2009-07-22 20:36:03 +0200600static void prompt_integer(int *target, const char *msg)
601{
602 char *buf = malloc(0), *p;
603 size_t dummy = 0;
604 int tmp;
605
606 fprintf(stdout, "\n%s: ", msg);
607 if (getline(&buf, &dummy, stdin) < 0)
608 return;
609
610 p = strchr(buf, '\n');
611 if (p)
612 *p = 0;
613
614 p = buf;
615 while(*p) {
616 if (!isdigit(*p))
617 goto out_free;
618 p++;
619 }
620 tmp = strtoul(buf, NULL, 10);
621 *target = tmp;
622out_free:
623 free(buf);
624}
625
626static void prompt_percent(int *target, const char *msg)
627{
628 int tmp = 0;
629
630 prompt_integer(&tmp, msg);
631 if (tmp >= 0 && tmp <= 100)
632 *target = tmp;
633}
634
635static void prompt_symbol(struct sym_entry **target, const char *msg)
636{
637 char *buf = malloc(0), *p;
638 struct sym_entry *syme = *target, *n, *found = NULL;
639 size_t dummy = 0;
640
641 /* zero counters of active symbol */
642 if (syme) {
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200643 pthread_mutex_lock(&syme->src->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200644 __zero_source_counters(syme);
645 *target = NULL;
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200646 pthread_mutex_unlock(&syme->src->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200647 }
648
649 fprintf(stdout, "\n%s: ", msg);
650 if (getline(&buf, &dummy, stdin) < 0)
651 goto out_free;
652
653 p = strchr(buf, '\n');
654 if (p)
655 *p = 0;
656
657 pthread_mutex_lock(&active_symbols_lock);
658 syme = list_entry(active_symbols.next, struct sym_entry, node);
659 pthread_mutex_unlock(&active_symbols_lock);
660
661 list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200662 struct symbol *sym = sym_entry__symbol(syme);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200663
664 if (!strcmp(buf, sym->name)) {
665 found = syme;
666 break;
667 }
668 }
669
670 if (!found) {
671 fprintf(stderr, "Sorry, %s is not active.\n", sym_filter);
672 sleep(1);
673 return;
674 } else
675 parse_source(found);
676
677out_free:
678 free(buf);
679}
680
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200681static void print_mapped_keys(void)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200682{
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200683 char *name = NULL;
684
685 if (sym_filter_entry) {
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200686 struct symbol *sym = sym_entry__symbol(sym_filter_entry);
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200687 name = sym->name;
688 }
689
690 fprintf(stdout, "\nMapped keys:\n");
691 fprintf(stdout, "\t[d] display refresh delay. \t(%d)\n", delay_secs);
692 fprintf(stdout, "\t[e] display entries (lines). \t(%d)\n", print_entries);
693
694 if (nr_counters > 1)
695 fprintf(stdout, "\t[E] active event counter. \t(%s)\n", event_name(sym_counter));
696
697 fprintf(stdout, "\t[f] profile display filter (count). \t(%d)\n", count_filter);
698
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -0200699 if (symbol_conf.vmlinux_name) {
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200700 fprintf(stdout, "\t[F] annotate display filter (percent). \t(%d%%)\n", sym_pcnt_filter);
701 fprintf(stdout, "\t[s] annotate symbol. \t(%s)\n", name?: "NULL");
702 fprintf(stdout, "\t[S] stop annotation.\n");
703 }
704
705 if (nr_counters > 1)
706 fprintf(stdout, "\t[w] toggle display weighted/count[E]r. \t(%d)\n", display_weighted ? 1 : 0);
707
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200708 fprintf(stdout,
709 "\t[K] hide kernel_symbols symbols. \t(%s)\n",
710 hide_kernel_symbols ? "yes" : "no");
711 fprintf(stdout,
712 "\t[U] hide user symbols. \t(%s)\n",
713 hide_user_symbols ? "yes" : "no");
Mike Galbraith46ab9762009-07-24 10:09:50 +0200714 fprintf(stdout, "\t[z] toggle sample zeroing. \t(%d)\n", zero ? 1 : 0);
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200715 fprintf(stdout, "\t[qQ] quit.\n");
716}
717
718static int key_mapped(int c)
719{
720 switch (c) {
721 case 'd':
722 case 'e':
723 case 'f':
724 case 'z':
725 case 'q':
726 case 'Q':
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200727 case 'K':
728 case 'U':
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200729 return 1;
730 case 'E':
731 case 'w':
732 return nr_counters > 1 ? 1 : 0;
733 case 'F':
734 case 's':
735 case 'S':
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -0200736 return symbol_conf.vmlinux_name ? 1 : 0;
Ingo Molnar83a09442009-08-15 12:26:57 +0200737 default:
738 break;
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200739 }
740
741 return 0;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200742}
743
744static void handle_keypress(int c)
745{
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200746 if (!key_mapped(c)) {
747 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
748 struct termios tc, save;
749
750 print_mapped_keys();
751 fprintf(stdout, "\nEnter selection, or unmapped key to continue: ");
752 fflush(stdout);
753
754 tcgetattr(0, &save);
755 tc = save;
756 tc.c_lflag &= ~(ICANON | ECHO);
757 tc.c_cc[VMIN] = 0;
758 tc.c_cc[VTIME] = 0;
759 tcsetattr(0, TCSANOW, &tc);
760
761 poll(&stdin_poll, 1, -1);
762 c = getc(stdin);
763
764 tcsetattr(0, TCSAFLUSH, &save);
765 if (!key_mapped(c))
766 return;
767 }
768
Mike Galbraith923c42c2009-07-22 20:36:03 +0200769 switch (c) {
770 case 'd':
771 prompt_integer(&delay_secs, "Enter display delay");
Tim Blechmanndc799592009-10-17 18:08:29 +0200772 if (delay_secs < 1)
773 delay_secs = 1;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200774 break;
775 case 'e':
776 prompt_integer(&print_entries, "Enter display entries (lines)");
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200777 if (print_entries == 0) {
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200778 sig_winch_handler(SIGWINCH);
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200779 signal(SIGWINCH, sig_winch_handler);
780 } else
781 signal(SIGWINCH, SIG_DFL);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200782 break;
783 case 'E':
784 if (nr_counters > 1) {
785 int i;
786
787 fprintf(stderr, "\nAvailable events:");
788 for (i = 0; i < nr_counters; i++)
789 fprintf(stderr, "\n\t%d %s", i, event_name(i));
790
791 prompt_integer(&sym_counter, "Enter details event counter");
792
793 if (sym_counter >= nr_counters) {
794 fprintf(stderr, "Sorry, no such event, using %s.\n", event_name(0));
795 sym_counter = 0;
796 sleep(1);
797 }
798 } else sym_counter = 0;
799 break;
800 case 'f':
801 prompt_integer(&count_filter, "Enter display event count filter");
802 break;
803 case 'F':
804 prompt_percent(&sym_pcnt_filter, "Enter details display event filter (percent)");
805 break;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200806 case 'K':
807 hide_kernel_symbols = !hide_kernel_symbols;
808 break;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200809 case 'q':
810 case 'Q':
811 printf("exiting.\n");
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200812 if (dump_symtab)
813 dsos__fprintf(stderr);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200814 exit(0);
815 case 's':
816 prompt_symbol(&sym_filter_entry, "Enter details symbol");
817 break;
818 case 'S':
819 if (!sym_filter_entry)
820 break;
821 else {
822 struct sym_entry *syme = sym_filter_entry;
823
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200824 pthread_mutex_lock(&syme->src->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200825 sym_filter_entry = NULL;
826 __zero_source_counters(syme);
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200827 pthread_mutex_unlock(&syme->src->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200828 }
829 break;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200830 case 'U':
831 hide_user_symbols = !hide_user_symbols;
832 break;
Mike Galbraith46ab9762009-07-24 10:09:50 +0200833 case 'w':
834 display_weighted = ~display_weighted;
835 break;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200836 case 'z':
837 zero = ~zero;
838 break;
Ingo Molnar83a09442009-08-15 12:26:57 +0200839 default:
840 break;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200841 }
842}
843
Ingo Molnarf37a2912009-07-01 12:37:06 +0200844static void *display_thread(void *arg __used)
Ingo Molnar07800602009-04-20 15:00:56 +0200845{
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200846 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
Mike Galbraith923c42c2009-07-22 20:36:03 +0200847 struct termios tc, save;
848 int delay_msecs, c;
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200849
Mike Galbraith923c42c2009-07-22 20:36:03 +0200850 tcgetattr(0, &save);
851 tc = save;
852 tc.c_lflag &= ~(ICANON | ECHO);
853 tc.c_cc[VMIN] = 0;
854 tc.c_cc[VTIME] = 0;
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200855
Mike Galbraith923c42c2009-07-22 20:36:03 +0200856repeat:
857 delay_msecs = delay_secs * 1000;
858 tcsetattr(0, TCSANOW, &tc);
859 /* trash return*/
860 getc(stdin);
Ingo Molnar07800602009-04-20 15:00:56 +0200861
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200862 do {
Ingo Molnar07800602009-04-20 15:00:56 +0200863 print_sym_table();
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200864 } while (!poll(&stdin_poll, 1, delay_msecs) == 1);
865
Mike Galbraith923c42c2009-07-22 20:36:03 +0200866 c = getc(stdin);
867 tcsetattr(0, TCSAFLUSH, &save);
868
869 handle_keypress(c);
870 goto repeat;
Ingo Molnar07800602009-04-20 15:00:56 +0200871
872 return NULL;
873}
874
Anton Blanchard2ab52082009-07-01 09:00:46 +1000875/* Tag samples to be skipped. */
Ingo Molnarf37a2912009-07-01 12:37:06 +0200876static const char *skip_symbols[] = {
Anton Blanchard2ab52082009-07-01 09:00:46 +1000877 "default_idle",
878 "cpu_idle",
879 "enter_idle",
880 "exit_idle",
881 "mwait_idle",
Arnaldo Carvalho de Melo59b90052009-07-26 19:06:19 -0300882 "mwait_idle_with_hints",
Arnaldo Carvalho de Melo83572752009-09-25 15:02:39 -0700883 "poll_idle",
Anton Blanchard3a3393e2009-07-01 09:00:47 +1000884 "ppc64_runlatch_off",
885 "pseries_dedicated_idle_sleep",
Anton Blanchard2ab52082009-07-01 09:00:46 +1000886 NULL
887};
888
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300889static int symbol_filter(struct map *map, struct symbol *sym)
Ingo Molnar07800602009-04-20 15:00:56 +0200890{
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300891 struct sym_entry *syme;
892 const char *name = sym->name;
Anton Blanchard2ab52082009-07-01 09:00:46 +1000893 int i;
Ingo Molnar07800602009-04-20 15:00:56 +0200894
Anton Blanchard3a3393e2009-07-01 09:00:47 +1000895 /*
896 * ppc64 uses function descriptors and appends a '.' to the
897 * start of every instruction address. Remove it.
898 */
899 if (name[0] == '.')
900 name++;
901
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300902 if (!strcmp(name, "_text") ||
903 !strcmp(name, "_etext") ||
904 !strcmp(name, "_sinittext") ||
905 !strncmp("init_module", name, 11) ||
906 !strncmp("cleanup_module", name, 14) ||
907 strstr(name, "_text_start") ||
908 strstr(name, "_text_end"))
Ingo Molnar07800602009-04-20 15:00:56 +0200909 return 1;
910
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200911 syme = symbol__priv(sym);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300912 syme->map = map;
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200913 syme->src = NULL;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200914 if (!sym_filter_entry && sym_filter && !strcmp(name, sym_filter))
915 sym_filter_entry = syme;
916
Anton Blanchard2ab52082009-07-01 09:00:46 +1000917 for (i = 0; skip_symbols[i]; i++) {
918 if (!strcmp(skip_symbols[i], name)) {
919 syme->skip = 1;
920 break;
921 }
922 }
Ingo Molnar07800602009-04-20 15:00:56 +0200923
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200924 if (!syme->skip)
925 syme->name_len = strlen(sym->name);
926
Ingo Molnar07800602009-04-20 15:00:56 +0200927 return 0;
928}
929
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200930static void event__process_sample(const event_t *self,
931 struct perf_session *session, int counter)
Ingo Molnar07800602009-04-20 15:00:56 +0200932{
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200933 u64 ip = self->ip.ip;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200934 struct sym_entry *syme;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200935 struct addr_location al;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200936 u8 origin = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Ingo Molnar07800602009-04-20 15:00:56 +0200937
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200938 switch (origin) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200939 case PERF_RECORD_MISC_USER:
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200940 if (hide_user_symbols)
941 return;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200942 break;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200943 case PERF_RECORD_MISC_KERNEL:
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200944 if (hide_kernel_symbols)
945 return;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200946 break;
947 default:
Ingo Molnar07800602009-04-20 15:00:56 +0200948 return;
949 }
950
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200951 if (event__preprocess_sample(self, session, &al, symbol_filter) < 0 ||
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200952 al.sym == NULL)
953 return;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200954
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200955 syme = symbol__priv(al.sym);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200956 if (!syme->skip) {
957 syme->count[counter]++;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200958 syme->origin = origin;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200959 record_precise_ip(syme, counter, ip);
960 pthread_mutex_lock(&active_symbols_lock);
961 if (list_empty(&syme->node) || !syme->node.next)
962 __list_insert_active_sym(syme);
963 pthread_mutex_unlock(&active_symbols_lock);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200964 if (origin == PERF_RECORD_MISC_USER)
965 ++userspace_samples;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200966 ++samples;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200967 }
968}
969
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200970static int event__process(event_t *event, struct perf_session *session)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200971{
972 switch (event->header.type) {
973 case PERF_RECORD_COMM:
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200974 event__process_comm(event, session);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200975 break;
976 case PERF_RECORD_MMAP:
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200977 event__process_mmap(event, session);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200978 break;
979 default:
980 break;
981 }
982
983 return 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200984}
985
Ingo Molnar07800602009-04-20 15:00:56 +0200986struct mmap_data {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200987 int counter;
988 void *base;
Ingo Molnarf37a2912009-07-01 12:37:06 +0200989 int mask;
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200990 unsigned int prev;
Ingo Molnar07800602009-04-20 15:00:56 +0200991};
992
993static unsigned int mmap_read_head(struct mmap_data *md)
994{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200995 struct perf_event_mmap_page *pc = md->base;
Ingo Molnar07800602009-04-20 15:00:56 +0200996 int head;
997
998 head = pc->data_head;
999 rmb();
1000
1001 return head;
1002}
1003
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001004static void perf_session__mmap_read_counter(struct perf_session *self,
1005 struct mmap_data *md)
Ingo Molnar07800602009-04-20 15:00:56 +02001006{
1007 unsigned int head = mmap_read_head(md);
1008 unsigned int old = md->prev;
1009 unsigned char *data = md->base + page_size;
1010 int diff;
1011
Ingo Molnar07800602009-04-20 15:00:56 +02001012 /*
1013 * If we're further behind than half the buffer, there's a chance
Ingo Molnar2debbc82009-06-05 14:29:10 +02001014 * the writer will bite our tail and mess up the samples under us.
Ingo Molnar07800602009-04-20 15:00:56 +02001015 *
1016 * If we somehow ended up ahead of the head, we got messed up.
1017 *
1018 * In either case, truncate and restart at head.
1019 */
1020 diff = head - old;
1021 if (diff > md->mask / 2 || diff < 0) {
Mike Galbraithf4f0b412009-10-13 14:57:20 +02001022 fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
Ingo Molnar07800602009-04-20 15:00:56 +02001023
1024 /*
1025 * head points to a known good entry, start there.
1026 */
1027 old = head;
1028 }
1029
Ingo Molnar07800602009-04-20 15:00:56 +02001030 for (; old != head;) {
Ingo Molnar07800602009-04-20 15:00:56 +02001031 event_t *event = (event_t *)&data[old & md->mask];
1032
1033 event_t event_copy;
1034
Ingo Molnar6f06ccb2009-04-20 15:22:22 +02001035 size_t size = event->header.size;
Ingo Molnar07800602009-04-20 15:00:56 +02001036
1037 /*
1038 * Event straddles the mmap boundary -- header should always
1039 * be inside due to u64 alignment of output.
1040 */
1041 if ((old & md->mask) + size != ((old + size) & md->mask)) {
1042 unsigned int offset = old;
1043 unsigned int len = min(sizeof(*event), size), cpy;
1044 void *dst = &event_copy;
1045
1046 do {
1047 cpy = min(md->mask + 1 - (offset & md->mask), len);
1048 memcpy(dst, &data[offset & md->mask], cpy);
1049 offset += cpy;
1050 dst += cpy;
1051 len -= cpy;
1052 } while (len);
1053
1054 event = &event_copy;
1055 }
1056
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001057 if (event->header.type == PERF_RECORD_SAMPLE)
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001058 event__process_sample(event, self, md->counter);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001059 else
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001060 event__process(event, self);
Ingo Molnar07800602009-04-20 15:00:56 +02001061 old += size;
Ingo Molnar07800602009-04-20 15:00:56 +02001062 }
1063
1064 md->prev = old;
1065}
1066
Mike Galbraithc2990a22009-05-24 08:35:49 +02001067static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
1068static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
1069
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001070static void perf_session__mmap_read(struct perf_session *self)
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001071{
1072 int i, counter;
1073
1074 for (i = 0; i < nr_cpus; i++) {
1075 for (counter = 0; counter < nr_counters; counter++)
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001076 perf_session__mmap_read_counter(self, &mmap_array[i][counter]);
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001077 }
1078}
1079
Ingo Molnar716c69f2009-06-07 17:31:52 +02001080int nr_poll;
1081int group_fd;
1082
1083static void start_counter(int i, int counter)
Ingo Molnar07800602009-04-20 15:00:56 +02001084{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001085 struct perf_event_attr *attr;
Mike Galbraith0fdc7e62009-07-21 10:30:36 +02001086 int cpu;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001087
1088 cpu = profile_cpu;
1089 if (target_pid == -1 && profile_cpu == -1)
1090 cpu = i;
1091
1092 attr = attrs + counter;
1093
1094 attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +02001095
1096 if (freq) {
1097 attr->sample_type |= PERF_SAMPLE_PERIOD;
1098 attr->freq = 1;
1099 attr->sample_freq = freq;
1100 }
1101
Mike Galbraith0fdc7e62009-07-21 10:30:36 +02001102 attr->inherit = (cpu < 0) && inherit;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001103 attr->mmap = 1;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001104
1105try_again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001106 fd[i][counter] = sys_perf_event_open(attr, target_pid, cpu, group_fd, 0);
Ingo Molnar716c69f2009-06-07 17:31:52 +02001107
1108 if (fd[i][counter] < 0) {
1109 int err = errno;
1110
Pekka Enbergc10edee2009-11-08 18:01:06 +02001111 if (err == EPERM || err == EACCES)
Ingo Molnar3da297a2009-06-07 17:39:02 +02001112 die("No permission - are you root?\n");
Ingo Molnar716c69f2009-06-07 17:31:52 +02001113 /*
1114 * If it's cycles then fall back to hrtimer
1115 * based cpu-clock-tick sw counter, which
1116 * is always available even if no PMU support:
1117 */
1118 if (attr->type == PERF_TYPE_HARDWARE
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02001119 && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
Ingo Molnar716c69f2009-06-07 17:31:52 +02001120
Ingo Molnar3da297a2009-06-07 17:39:02 +02001121 if (verbose)
1122 warning(" ... trying to fall back to cpu-clock-ticks\n");
1123
Ingo Molnar716c69f2009-06-07 17:31:52 +02001124 attr->type = PERF_TYPE_SOFTWARE;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02001125 attr->config = PERF_COUNT_SW_CPU_CLOCK;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001126 goto try_again;
1127 }
Ingo Molnar30c806a2009-06-07 17:46:24 +02001128 printf("\n");
1129 error("perfcounter syscall returned with %d (%s)\n",
1130 fd[i][counter], strerror(err));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001131 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
Ingo Molnar716c69f2009-06-07 17:31:52 +02001132 exit(-1);
1133 }
1134 assert(fd[i][counter] >= 0);
1135 fcntl(fd[i][counter], F_SETFL, O_NONBLOCK);
1136
1137 /*
1138 * First counter acts as the group leader:
1139 */
1140 if (group && group_fd == -1)
1141 group_fd = fd[i][counter];
1142
1143 event_array[nr_poll].fd = fd[i][counter];
1144 event_array[nr_poll].events = POLLIN;
1145 nr_poll++;
1146
1147 mmap_array[i][counter].counter = counter;
1148 mmap_array[i][counter].prev = 0;
1149 mmap_array[i][counter].mask = mmap_pages*page_size - 1;
1150 mmap_array[i][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
1151 PROT_READ, MAP_SHARED, fd[i][counter], 0);
1152 if (mmap_array[i][counter].base == MAP_FAILED)
1153 die("failed to mmap with %d (%s)\n", errno, strerror(errno));
1154}
1155
1156static int __cmd_top(void)
1157{
1158 pthread_t thread;
1159 int i, counter;
Ingo Molnar07800602009-04-20 15:00:56 +02001160 int ret;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001161 /*
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001162 * FIXME: perf_session__new should allow passing a O_MMAP, so that all this
1163 * mmap reading, etc is encapsulated in it. Use O_WRONLY for now.
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001164 */
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001165 struct perf_session *session = perf_session__new(NULL, O_WRONLY, false);
1166
1167 if (session == NULL)
1168 return -ENOMEM;
Ingo Molnar07800602009-04-20 15:00:56 +02001169
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001170 if (target_pid != -1)
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001171 event__synthesize_thread(target_pid, event__process, session);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001172 else
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001173 event__synthesize_threads(event__process, session);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001174
Ingo Molnar07800602009-04-20 15:00:56 +02001175 for (i = 0; i < nr_cpus; i++) {
1176 group_fd = -1;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001177 for (counter = 0; counter < nr_counters; counter++)
1178 start_counter(i, counter);
Ingo Molnar07800602009-04-20 15:00:56 +02001179 }
1180
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001181 /* Wait for a minimal set of events before starting the snapshot */
1182 poll(event_array, nr_poll, 100);
1183
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001184 perf_session__mmap_read(session);
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001185
Ingo Molnar07800602009-04-20 15:00:56 +02001186 if (pthread_create(&thread, NULL, display_thread, NULL)) {
1187 printf("Could not create display thread.\n");
1188 exit(-1);
1189 }
1190
1191 if (realtime_prio) {
1192 struct sched_param param;
1193
1194 param.sched_priority = realtime_prio;
1195 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
1196 printf("Could not set realtime priority.\n");
1197 exit(-1);
1198 }
1199 }
1200
1201 while (1) {
Ingo Molnar2debbc82009-06-05 14:29:10 +02001202 int hits = samples;
Ingo Molnar07800602009-04-20 15:00:56 +02001203
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001204 perf_session__mmap_read(session);
Ingo Molnar07800602009-04-20 15:00:56 +02001205
Ingo Molnar2debbc82009-06-05 14:29:10 +02001206 if (hits == samples)
Ingo Molnar07800602009-04-20 15:00:56 +02001207 ret = poll(event_array, nr_poll, 100);
1208 }
1209
1210 return 0;
1211}
Ingo Molnarb456bae2009-05-26 09:17:18 +02001212
1213static const char * const top_usage[] = {
1214 "perf top [<options>]",
1215 NULL
1216};
1217
Ingo Molnarb456bae2009-05-26 09:17:18 +02001218static const struct option options[] = {
1219 OPT_CALLBACK('e', "event", NULL, "event",
Thomas Gleixner86847b62009-06-06 12:24:17 +02001220 "event selector. use 'perf list' to list available events",
1221 parse_events),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001222 OPT_INTEGER('c', "count", &default_interval,
1223 "event period to sample"),
1224 OPT_INTEGER('p', "pid", &target_pid,
1225 "profile events on existing pid"),
1226 OPT_BOOLEAN('a', "all-cpus", &system_wide,
1227 "system-wide collection from all CPUs"),
1228 OPT_INTEGER('C', "CPU", &profile_cpu,
1229 "CPU to profile on"),
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02001230 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1231 "file", "vmlinux pathname"),
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -02001232 OPT_BOOLEAN('K', "hide_kernel_symbols", &hide_kernel_symbols,
1233 "hide kernel symbols"),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001234 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
1235 "number of mmap data pages"),
1236 OPT_INTEGER('r', "realtime", &realtime_prio,
1237 "collect data with this RT SCHED_FIFO priority"),
Mike Galbraithdb20c002009-05-26 15:25:34 +02001238 OPT_INTEGER('d', "delay", &delay_secs,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001239 "number of seconds to delay between refreshes"),
1240 OPT_BOOLEAN('D', "dump-symtab", &dump_symtab,
1241 "dump the symbol table used for profiling"),
Ingo Molnar6e53cdf2009-06-04 08:53:05 +02001242 OPT_INTEGER('f', "count-filter", &count_filter,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001243 "only display functions with more events than this"),
1244 OPT_BOOLEAN('g', "group", &group,
1245 "put the counters into a counter group"),
Mike Galbraith0fdc7e62009-07-21 10:30:36 +02001246 OPT_BOOLEAN('i', "inherit", &inherit,
1247 "child tasks inherit counters"),
Mike Galbraith923c42c2009-07-22 20:36:03 +02001248 OPT_STRING('s', "sym-annotate", &sym_filter, "symbol name",
1249 "symbol to annotate - requires -k option"),
Anton Blanchard1f208ea2009-07-01 09:00:44 +10001250 OPT_BOOLEAN('z', "zero", &zero,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001251 "zero history across updates"),
Ingo Molnar6e53cdf2009-06-04 08:53:05 +02001252 OPT_INTEGER('F', "freq", &freq,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001253 "profile at this frequency"),
Ingo Molnar6e53cdf2009-06-04 08:53:05 +02001254 OPT_INTEGER('E', "entries", &print_entries,
1255 "display this many functions"),
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -02001256 OPT_BOOLEAN('U', "hide_user_symbols", &hide_user_symbols,
1257 "hide user symbols"),
Ingo Molnar3da297a2009-06-07 17:39:02 +02001258 OPT_BOOLEAN('v', "verbose", &verbose,
1259 "be more verbose (show counter open errors, etc)"),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001260 OPT_END()
1261};
1262
Ingo Molnarf37a2912009-07-01 12:37:06 +02001263int cmd_top(int argc, const char **argv, const char *prefix __used)
Ingo Molnarb456bae2009-05-26 09:17:18 +02001264{
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02001265 int counter;
Ingo Molnarb456bae2009-05-26 09:17:18 +02001266
1267 page_size = sysconf(_SC_PAGE_SIZE);
1268
Ingo Molnarb456bae2009-05-26 09:17:18 +02001269 argc = parse_options(argc, argv, options, top_usage, 0);
1270 if (argc)
1271 usage_with_options(top_usage, options);
1272
Ingo Molnarb456bae2009-05-26 09:17:18 +02001273 /* CPU and PID are mutually exclusive */
1274 if (target_pid != -1 && profile_cpu != -1) {
1275 printf("WARNING: PID switch overriding CPU\n");
1276 sleep(1);
1277 profile_cpu = -1;
1278 }
1279
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001280 if (!nr_counters)
Ingo Molnarb456bae2009-05-26 09:17:18 +02001281 nr_counters = 1;
Ingo Molnarb456bae2009-05-26 09:17:18 +02001282
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02001283 symbol_conf.priv_size = (sizeof(struct sym_entry) +
1284 (nr_counters + 1) * sizeof(unsigned long));
1285 if (symbol_conf.vmlinux_name == NULL)
1286 symbol_conf.try_vmlinux_path = true;
1287 if (symbol__init(&symbol_conf) < 0)
1288 return -1;
Arnaldo Carvalho de Melo5a8e5a32009-11-17 18:38:01 -02001289
Frederic Weisbecker2f335a02009-06-05 19:31:01 +02001290 if (delay_secs < 1)
1291 delay_secs = 1;
1292
Mike Galbraith923c42c2009-07-22 20:36:03 +02001293 parse_source(sym_filter_entry);
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001294
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +02001295 /*
1296 * User specified count overrides default frequency.
1297 */
1298 if (default_interval)
1299 freq = 0;
1300 else if (freq) {
1301 default_interval = freq;
1302 } else {
1303 fprintf(stderr, "frequency and count are zero, aborting\n");
1304 exit(EXIT_FAILURE);
1305 }
1306
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001307 /*
1308 * Fill in the ones not specifically initialized via -c:
1309 */
Ingo Molnarb456bae2009-05-26 09:17:18 +02001310 for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001311 if (attrs[counter].sample_period)
Ingo Molnarb456bae2009-05-26 09:17:18 +02001312 continue;
1313
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001314 attrs[counter].sample_period = default_interval;
Ingo Molnarb456bae2009-05-26 09:17:18 +02001315 }
1316
1317 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
1318 assert(nr_cpus <= MAX_NR_CPUS);
1319 assert(nr_cpus >= 0);
1320
1321 if (target_pid != -1 || profile_cpu != -1)
1322 nr_cpus = 1;
1323
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -02001324 get_term_dimensions(&winsize);
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -02001325 if (print_entries == 0) {
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -02001326 update_print_entries(&winsize);
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -02001327 signal(SIGWINCH, sig_winch_handler);
1328 }
1329
Ingo Molnarb456bae2009-05-26 09:17:18 +02001330 return __cmd_top();
1331}