perf annotate: Fix it for non-prelinked *.so
[linux-2.6-block.git] / tools / perf / builtin-top.c
CommitLineData
07800601 1/*
bf9e1876
IM
2 * 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)
07800601 18 */
bf9e1876 19#include "builtin.h"
07800601 20
1a482f38 21#include "perf.h"
bf9e1876 22
8fc0321f 23#include "util/color.h"
b3165f41
ACM
24#include "util/session.h"
25#include "util/symbol.h"
439d473b 26#include "util/thread.h"
148be2c1 27#include "util/util.h"
43cbcd8a 28#include <linux/rbtree.h>
b456bae0
IM
29#include "util/parse-options.h"
30#include "util/parse-events.h"
07800601 31
8f28827a
FW
32#include "util/debug.h"
33
07800601
IM
34#include <assert.h>
35#include <fcntl.h>
0e9b20b8 36
07800601 37#include <stdio.h>
923c42c1
MG
38#include <termios.h>
39#include <unistd.h>
0e9b20b8 40
07800601 41#include <errno.h>
07800601
IM
42#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
a21ca2ca 57static int fd[MAX_NR_CPUS][MAX_COUNTERS];
07800601 58
42e59d7d 59static int system_wide = 0;
07800601 60
7e4ff9e3 61static int default_interval = 0;
07800601 62
42e59d7d 63static int count_filter = 5;
3b6ed988 64static int print_entries;
07800601 65
42e59d7d
IM
66static 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;
07800601 72static unsigned int page_size;
42e59d7d
IM
73static unsigned int mmap_pages = 16;
74static int freq = 1000; /* 1 KHz */
07800601 75
42e59d7d
IM
76static int delay_secs = 2;
77static int zero = 0;
78static int dump_symtab = 0;
07800601 79
8ffcda17
ACM
80static bool hide_kernel_symbols = false;
81static bool hide_user_symbols = false;
13cc5079 82static struct winsize winsize;
8ffcda17 83
923c42c1
MG
84/*
85 * 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
42e59d7d
IM
95static 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;
923c42c1 100
07800601
IM
101/*
102 * Symbols
103 */
104
b269876c
ACM
105struct 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
07800601 112struct sym_entry {
de04687f
ACM
113 struct rb_node rb_node;
114 struct list_head node;
c44613a4
ACM
115 unsigned long snap_count;
116 double weight;
07800601 117 int skip;
13cc5079 118 u16 name_len;
8ffcda17 119 u8 origin;
439d473b 120 struct map *map;
b269876c 121 struct sym_entry_source *src;
5a8e5a30 122 unsigned long count[0];
07800601
IM
123};
124
923c42c1
MG
125/*
126 * Source functions
127 */
128
51a472de
ACM
129static inline struct symbol *sym_entry__symbol(struct sym_entry *self)
130{
b32d133a 131 return ((void *)self) + symbol_conf.priv_size;
51a472de
ACM
132}
133
13cc5079 134static void get_term_dimensions(struct winsize *ws)
3b6ed988 135{
13cc5079
ACM
136 char *s = getenv("LINES");
137
138 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 }
3b6ed988 146 }
13cc5079
ACM
147#ifdef TIOCGWINSZ
148 if (ioctl(1, TIOCGWINSZ, ws) == 0 &&
149 ws->ws_row && ws->ws_col)
150 return;
3b6ed988 151#endif
13cc5079
ACM
152 ws->ws_row = 25;
153 ws->ws_col = 80;
3b6ed988
ACM
154}
155
13cc5079 156static void update_print_entries(struct winsize *ws)
3b6ed988 157{
13cc5079
ACM
158 print_entries = ws->ws_row;
159
3b6ed988
ACM
160 if (print_entries > 9)
161 print_entries -= 9;
162}
163
164static void sig_winch_handler(int sig __used)
165{
13cc5079
ACM
166 get_term_dimensions(&winsize);
167 update_print_entries(&winsize);
3b6ed988
ACM
168}
169
923c42c1
MG
170static void parse_source(struct sym_entry *syme)
171{
172 struct symbol *sym;
b269876c 173 struct sym_entry_source *source;
439d473b 174 struct map *map;
923c42c1 175 FILE *file;
83a0944f 176 char command[PATH_MAX*2];
439d473b
ACM
177 const char *path;
178 u64 len;
923c42c1
MG
179
180 if (!syme)
181 return;
182
b269876c 183 if (syme->src == NULL) {
36479484 184 syme->src = zalloc(sizeof(*source));
b269876c
ACM
185 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);
923c42c1
MG
194 goto out_assign;
195 }
196
51a472de 197 sym = sym_entry__symbol(syme);
439d473b
ACM
198 map = syme->map;
199 path = map->dso->long_name;
923c42c1 200
923c42c1
MG
201 len = sym->end - sym->start;
202
439d473b
ACM
203 sprintf(command,
204 "objdump --start-address=0x%016Lx "
205 "--stop-address=0x%016Lx -dS %s",
c88e4bf6
ACM
206 map->unmap_ip(map, sym->start),
207 map->unmap_ip(map, sym->end), path);
923c42c1
MG
208
209 file = popen(command, "r");
210 if (!file)
211 return;
212
b269876c
ACM
213 pthread_mutex_lock(&source->lock);
214 source->lines_tail = &source->lines;
923c42c1
MG
215 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;
b269876c
ACM
234 *source->lines_tail = src;
235 source->lines_tail = &src->next;
923c42c1
MG
236
237 if (strlen(src->line)>8 && src->line[8] == ':') {
238 src->eip = strtoull(src->line, NULL, 16);
c88e4bf6 239 src->eip = map->unmap_ip(map, src->eip);
923c42c1
MG
240 }
241 if (strlen(src->line)>8 && src->line[16] == ':') {
242 src->eip = strtoull(src->line, NULL, 16);
c88e4bf6 243 src->eip = map->unmap_ip(map, src->eip);
923c42c1
MG
244 }
245 }
246 pclose(file);
247out_assign:
248 sym_filter_entry = syme;
b269876c 249 pthread_mutex_unlock(&source->lock);
923c42c1
MG
250}
251
252static void __zero_source_counters(struct sym_entry *syme)
253{
254 int i;
255 struct source_line *line;
256
b269876c 257 line = syme->src->lines;
923c42c1
MG
258 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
b269876c 272 if (pthread_mutex_trylock(&syme->src->lock))
923c42c1
MG
273 return;
274
b269876c 275 if (syme->src == NULL || syme->src->source == NULL)
923c42c1
MG
276 goto out_unlock;
277
b269876c 278 for (line = syme->src->lines; line; line = line->next) {
923c42c1
MG
279 if (line->eip == ip) {
280 line->count[counter]++;
281 break;
282 }
283 if (line->eip > ip)
284 break;
285 }
286out_unlock:
b269876c 287 pthread_mutex_unlock(&syme->src->lock);
923c42c1
MG
288}
289
290static void lookup_sym_source(struct sym_entry *syme)
291{
51a472de 292 struct symbol *symbol = sym_entry__symbol(syme);
923c42c1
MG
293 struct source_line *line;
294 char pattern[PATH_MAX];
923c42c1
MG
295
296 sprintf(pattern, "<%s>:", symbol->name);
297
b269876c
ACM
298 pthread_mutex_lock(&syme->src->lock);
299 for (line = syme->src->lines; line; line = line->next) {
923c42c1 300 if (strstr(line->line, pattern)) {
b269876c 301 syme->src->source = line;
923c42c1
MG
302 break;
303 }
304 }
b269876c 305 pthread_mutex_unlock(&syme->src->lock);
923c42c1
MG
306}
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
b269876c 335 if (!syme->src->source)
923c42c1
MG
336 lookup_sym_source(syme);
337
b269876c 338 if (!syme->src->source)
923c42c1
MG
339 return;
340
51a472de 341 symbol = sym_entry__symbol(syme);
923c42c1
MG
342 printf("Showing %s for %s\n", event_name(sym_counter), symbol->name);
343 printf(" Events Pcnt (>=%d%%)\n", sym_pcnt_filter);
344
b269876c
ACM
345 pthread_mutex_lock(&syme->src->lock);
346 line = syme->src->source;
923c42c1
MG
347 while (line) {
348 total += line->count[sym_counter];
349 line = line->next;
350 }
351
b269876c 352 line = syme->src->source;
923c42c1
MG
353 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 }
b269876c 377 pthread_mutex_unlock(&syme->src->lock);
923c42c1
MG
378 if (more)
379 printf("%d lines not displayed, maybe increase display entries [e]\n", more);
380}
07800601 381
de04687f 382/*
5b2bb75a 383 * Symbols will be added here in event__process_sample and will get out
de04687f
ACM
384 * after decayed.
385 */
386static LIST_HEAD(active_symbols);
c44613a4 387static pthread_mutex_t active_symbols_lock = PTHREAD_MUTEX_INITIALIZER;
07800601 388
07800601
IM
389/*
390 * Ordering weight: count-1 * count-2 * ... / count-n
391 */
392static double sym_weight(const struct sym_entry *sym)
393{
c44613a4 394 double weight = sym->snap_count;
07800601
IM
395 int counter;
396
46ab9764
MG
397 if (!display_weighted)
398 return weight;
399
07800601
IM
400 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
2debbc83
IM
408static long samples;
409static long userspace_samples;
07800601
IM
410static const char CONSOLE_CLEAR[] = "\e[H\e[2J";
411
c44613a4 412static void __list_insert_active_sym(struct sym_entry *syme)
de04687f
ACM
413{
414 list_add(&syme->node, &active_symbols);
415}
416
c44613a4
ACM
417static 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
de04687f
ACM
424static 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
c44613a4 434 if (se->weight > iter->weight)
de04687f
ACM
435 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}
07800601
IM
443
444static void print_sym_table(void)
445{
233f0b95 446 int printed = 0, j;
46ab9764 447 int counter, snap = !display_weighted ? sym_counter : 0;
2debbc83
IM
448 float samples_per_sec = samples/delay_secs;
449 float ksamples_per_sec = (samples-userspace_samples)/delay_secs;
450 float sum_ksamples = 0.0;
de04687f
ACM
451 struct sym_entry *syme, *n;
452 struct rb_root tmp = RB_ROOT;
453 struct rb_node *nd;
7cc017ed 454 int sym_width = 0, dso_width = 0, max_dso_width;
13cc5079 455 const int win_width = winsize.ws_col - 1;
07800601 456
2debbc83 457 samples = userspace_samples = 0;
07800601 458
de04687f 459 /* Sort the active symbols */
c44613a4
ACM
460 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) {
46ab9764 465 syme->snap_count = syme->count[snap];
c44613a4 466 if (syme->snap_count != 0) {
13cc5079 467
8ffcda17
ACM
468 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 }
c44613a4 475 syme->weight = sym_weight(syme);
de04687f 476 rb_insert_active_sym(&tmp, syme);
2debbc83 477 sum_ksamples += syme->snap_count;
d94b9430
MG
478
479 for (j = 0; j < nr_counters; j++)
de04687f
ACM
480 syme->count[j] = zero ? 0 : syme->count[j] * 7 / 8;
481 } else
c44613a4 482 list_remove_active_sym(syme);
d94b9430
MG
483 }
484
0f5486b5 485 puts(CONSOLE_CLEAR);
07800601 486
13cc5079 487 printf("%-*.*s\n", win_width, win_width, graph_dotted_line);
f2521b6e 488 printf( " PerfTop:%8.0f irqs/sec kernel:%4.1f%% [",
2debbc83
IM
489 samples_per_sec,
490 100.0 - (100.0*((samples_per_sec-ksamples_per_sec)/samples_per_sec)));
07800601 491
46ab9764 492 if (nr_counters == 1 || !display_weighted) {
9cffa8d5 493 printf("%Ld", (u64)attrs[0].sample_period);
cf1f4574
IM
494 if (freq)
495 printf("Hz ");
496 else
497 printf(" ");
498 }
07800601 499
46ab9764
MG
500 if (!display_weighted)
501 printf("%s", event_name(sym_counter));
502 else for (counter = 0; counter < nr_counters; counter++) {
07800601
IM
503 if (counter)
504 printf("/");
505
506 printf("%s", event_name(counter));
507 }
508
509 printf( "], ");
510
b456bae0
IM
511 if (target_pid != -1)
512 printf(" (target_pid: %d", target_pid);
07800601
IM
513 else
514 printf(" (all");
515
516 if (profile_cpu != -1)
517 printf(", cpu: %d)\n", profile_cpu);
518 else {
b456bae0 519 if (target_pid != -1)
07800601
IM
520 printf(")\n");
521 else
522 printf(", %d CPUs)\n", nr_cpus);
523 }
524
1a105f74 525 printf("%-*.*s\n", win_width, win_width, graph_dotted_line);
07800601 526
923c42c1
MG
527 if (sym_filter_entry) {
528 show_details(sym_filter_entry);
529 return;
530 }
531
13cc5079
ACM
532 /*
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
1a105f74
ACM
541 if (syme->map->dso->long_name_len > dso_width)
542 dso_width = syme->map->dso->long_name_len;
543
13cc5079
ACM
544 if (syme->name_len > sym_width)
545 sym_width = syme->name_len;
546 }
547
548 printed = 0;
549
7cc017ed
ACM
550 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');
07800601 554 if (nr_counters == 1)
5b2bb75a 555 printf(" samples pcnt");
07800601 556 else
5b2bb75a 557 printf(" weight samples pcnt");
07800601 558
7ced156b
ACM
559 if (verbose)
560 printf(" RIP ");
7cc017ed 561 printf(" %-*.*s DSO\n", sym_width, sym_width, "function");
5b2bb75a 562 printf(" %s _______ _____",
7ced156b
ACM
563 nr_counters == 1 ? " " : "______");
564 if (verbose)
5b2bb75a 565 printf(" ________________");
1a105f74 566 printf(" %-*.*s", sym_width, sym_width, graph_line);
7cc017ed 567 printf(" %-*.*s", dso_width, dso_width, graph_line);
1a105f74 568 puts("\n");
07800601 569
de04687f 570 for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
83a0944f 571 struct symbol *sym;
8fc0321f 572 double pcnt;
d94b9430 573
83a0944f 574 syme = rb_entry(nd, struct sym_entry, rb_node);
51a472de 575 sym = sym_entry__symbol(syme);
83a0944f 576
923c42c1 577 if (++printed > print_entries || (int)syme->snap_count < count_filter)
c44613a4 578 continue;
d94b9430 579
2debbc83
IM
580 pcnt = 100.0 - (100.0 * ((sum_ksamples - syme->snap_count) /
581 sum_ksamples));
d94b9430 582
46ab9764 583 if (nr_counters == 1 || !display_weighted)
5b2bb75a 584 printf("%20.2f ", syme->weight);
d94b9430 585 else
5b2bb75a 586 printf("%9.1f %10ld ", syme->weight, syme->snap_count);
8fc0321f 587
1e11fd82 588 percent_color_fprintf(stdout, "%4.1f%%", pcnt);
7ced156b 589 if (verbose)
5b2bb75a 590 printf(" %016llx", sym->start);
13cc5079 591 printf(" %-*.*s", sym_width, sym_width, sym->name);
7cc017ed
ACM
592 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);
07800601 596 }
07800601
IM
597}
598
923c42c1
MG
599static 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) {
b269876c 642 pthread_mutex_lock(&syme->src->lock);
923c42c1
MG
643 __zero_source_counters(syme);
644 *target = NULL;
b269876c 645 pthread_mutex_unlock(&syme->src->lock);
923c42c1
MG
646 }
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) {
51a472de 661 struct symbol *sym = sym_entry__symbol(syme);
923c42c1
MG
662
663 if (!strcmp(buf, sym->name)) {
664 found = syme;
665 break;
666 }
667 }
668
669 if (!found) {
66aeb6d5 670 fprintf(stderr, "Sorry, %s is not active.\n", buf);
923c42c1
MG
671 sleep(1);
672 return;
673 } else
674 parse_source(found);
675
676out_free:
677 free(buf);
678}
679
091bd2e9 680static void print_mapped_keys(void)
923c42c1 681{
091bd2e9
MG
682 char *name = NULL;
683
684 if (sym_filter_entry) {
51a472de 685 struct symbol *sym = sym_entry__symbol(sym_filter_entry);
091bd2e9
MG
686 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
b32d133a 698 if (symbol_conf.vmlinux_name) {
091bd2e9
MG
699 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
8ffcda17
ACM
707 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");
46ab9764 713 fprintf(stdout, "\t[z] toggle sample zeroing. \t(%d)\n", zero ? 1 : 0);
091bd2e9
MG
714 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':
8ffcda17
ACM
726 case 'K':
727 case 'U':
091bd2e9
MG
728 return 1;
729 case 'E':
730 case 'w':
731 return nr_counters > 1 ? 1 : 0;
732 case 'F':
733 case 's':
734 case 'S':
b32d133a 735 return symbol_conf.vmlinux_name ? 1 : 0;
83a0944f
IM
736 default:
737 break;
091bd2e9
MG
738 }
739
740 return 0;
923c42c1
MG
741}
742
743static void handle_keypress(int c)
744{
091bd2e9
MG
745 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
923c42c1
MG
768 switch (c) {
769 case 'd':
770 prompt_integer(&delay_secs, "Enter display delay");
dc79959a
TB
771 if (delay_secs < 1)
772 delay_secs = 1;
923c42c1
MG
773 break;
774 case 'e':
775 prompt_integer(&print_entries, "Enter display entries (lines)");
3b6ed988 776 if (print_entries == 0) {
13cc5079 777 sig_winch_handler(SIGWINCH);
3b6ed988
ACM
778 signal(SIGWINCH, sig_winch_handler);
779 } else
780 signal(SIGWINCH, SIG_DFL);
923c42c1
MG
781 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;
8ffcda17
ACM
805 case 'K':
806 hide_kernel_symbols = !hide_kernel_symbols;
807 break;
923c42c1
MG
808 case 'q':
809 case 'Q':
810 printf("exiting.\n");
c338aee8
ACM
811 if (dump_symtab)
812 dsos__fprintf(stderr);
923c42c1
MG
813 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
b269876c 823 pthread_mutex_lock(&syme->src->lock);
923c42c1
MG
824 sym_filter_entry = NULL;
825 __zero_source_counters(syme);
b269876c 826 pthread_mutex_unlock(&syme->src->lock);
923c42c1
MG
827 }
828 break;
8ffcda17
ACM
829 case 'U':
830 hide_user_symbols = !hide_user_symbols;
831 break;
46ab9764
MG
832 case 'w':
833 display_weighted = ~display_weighted;
834 break;
923c42c1
MG
835 case 'z':
836 zero = ~zero;
837 break;
83a0944f
IM
838 default:
839 break;
923c42c1
MG
840 }
841}
842
f37a291c 843static void *display_thread(void *arg __used)
07800601 844{
0f5486b5 845 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
923c42c1
MG
846 struct termios tc, save;
847 int delay_msecs, c;
848
849 tcgetattr(0, &save);
850 tc = save;
851 tc.c_lflag &= ~(ICANON | ECHO);
852 tc.c_cc[VMIN] = 0;
853 tc.c_cc[VTIME] = 0;
091bd2e9 854
923c42c1
MG
855repeat:
856 delay_msecs = delay_secs * 1000;
857 tcsetattr(0, TCSANOW, &tc);
858 /* trash return*/
859 getc(stdin);
07800601 860
0f5486b5 861 do {
07800601 862 print_sym_table();
0f5486b5
FW
863 } while (!poll(&stdin_poll, 1, delay_msecs) == 1);
864
923c42c1
MG
865 c = getc(stdin);
866 tcsetattr(0, TCSAFLUSH, &save);
867
868 handle_keypress(c);
869 goto repeat;
07800601
IM
870
871 return NULL;
872}
873
2ab52083 874/* Tag samples to be skipped. */
f37a291c 875static const char *skip_symbols[] = {
2ab52083
AB
876 "default_idle",
877 "cpu_idle",
878 "enter_idle",
879 "exit_idle",
880 "mwait_idle",
59b90056 881 "mwait_idle_with_hints",
8357275b 882 "poll_idle",
3a3393ef
AB
883 "ppc64_runlatch_off",
884 "pseries_dedicated_idle_sleep",
2ab52083
AB
885 NULL
886};
887
439d473b 888static int symbol_filter(struct map *map, struct symbol *sym)
07800601 889{
de04687f
ACM
890 struct sym_entry *syme;
891 const char *name = sym->name;
2ab52083 892 int i;
de04687f 893
3a3393ef
AB
894 /*
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
de04687f
ACM
901 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"))
07800601 908 return 1;
07800601 909
00a192b3 910 syme = symbol__priv(sym);
439d473b 911 syme->map = map;
b269876c 912 syme->src = NULL;
923c42c1
MG
913 if (!sym_filter_entry && sym_filter && !strcmp(name, sym_filter))
914 sym_filter_entry = syme;
915
2ab52083
AB
916 for (i = 0; skip_symbols[i]; i++) {
917 if (!strcmp(skip_symbols[i], name)) {
918 syme->skip = 1;
919 break;
920 }
921 }
07800601 922
13cc5079
ACM
923 if (!syme->skip)
924 syme->name_len = strlen(sym->name);
925
07800601
IM
926 return 0;
927}
928
b3165f41
ACM
929static void event__process_sample(const event_t *self,
930 struct perf_session *session, int counter)
07800601 931{
5b2bb75a 932 u64 ip = self->ip.ip;
5b2bb75a 933 struct sym_entry *syme;
1ed091c4 934 struct addr_location al;
8ffcda17 935 u8 origin = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
5b2bb75a 936
24bfef0f
ACM
937 ++samples;
938
8ffcda17 939 switch (origin) {
1ed091c4 940 case PERF_RECORD_MISC_USER:
24bfef0f 941 ++userspace_samples;
8ffcda17
ACM
942 if (hide_user_symbols)
943 return;
1ed091c4 944 break;
5b2bb75a 945 case PERF_RECORD_MISC_KERNEL:
8ffcda17
ACM
946 if (hide_kernel_symbols)
947 return;
5b2bb75a
ACM
948 break;
949 default:
950 return;
951 }
952
b3165f41 953 if (event__preprocess_sample(self, session, &al, symbol_filter) < 0 ||
72b8fa17 954 al.filtered)
1ed091c4 955 return;
07800601 956
72b8fa17
ACM
957 if (al.sym == NULL) {
958 /*
959 * As we do lazy loading of symtabs we only will know if the
960 * specified vmlinux file is invalid when we actually have a
961 * hit in kernel space and then try to load it. So if we get
962 * here and there are _no_ symbols in the DSO backing the
963 * kernel map, bail out.
964 *
965 * We may never get here, for instance, if we use -K/
966 * --hide-kernel-symbols, even if the user specifies an
967 * invalid --vmlinux ;-)
968 */
969 if (al.map == session->vmlinux_maps[MAP__FUNCTION] &&
970 RB_EMPTY_ROOT(&al.map->dso->symbols[MAP__FUNCTION])) {
971 pr_err("The %s file can't be used\n",
972 symbol_conf.vmlinux_name);
973 exit(1);
974 }
975
976 return;
977 }
978
1ed091c4 979 syme = symbol__priv(al.sym);
5b2bb75a
ACM
980 if (!syme->skip) {
981 syme->count[counter]++;
8ffcda17 982 syme->origin = origin;
5b2bb75a
ACM
983 record_precise_ip(syme, counter, ip);
984 pthread_mutex_lock(&active_symbols_lock);
985 if (list_empty(&syme->node) || !syme->node.next)
986 __list_insert_active_sym(syme);
987 pthread_mutex_unlock(&active_symbols_lock);
5b2bb75a 988 }
07800601
IM
989}
990
d8f66248 991static int event__process(event_t *event, struct perf_session *session)
5b2bb75a
ACM
992{
993 switch (event->header.type) {
994 case PERF_RECORD_COMM:
d8f66248 995 event__process_comm(event, session);
5b2bb75a
ACM
996 break;
997 case PERF_RECORD_MMAP:
d8f66248 998 event__process_mmap(event, session);
5b2bb75a 999 break;
0f35cd4c
ACM
1000 case PERF_RECORD_FORK:
1001 case PERF_RECORD_EXIT:
1002 event__process_task(event, session);
1003 break;
5b2bb75a
ACM
1004 default:
1005 break;
07800601
IM
1006 }
1007
5b2bb75a 1008 return 0;
07800601
IM
1009}
1010
07800601 1011struct mmap_data {
a21ca2ca
IM
1012 int counter;
1013 void *base;
f37a291c 1014 int mask;
a21ca2ca 1015 unsigned int prev;
07800601
IM
1016};
1017
1018static unsigned int mmap_read_head(struct mmap_data *md)
1019{
cdd6c482 1020 struct perf_event_mmap_page *pc = md->base;
07800601
IM
1021 int head;
1022
1023 head = pc->data_head;
1024 rmb();
1025
1026 return head;
1027}
1028
d8f66248
ACM
1029static void perf_session__mmap_read_counter(struct perf_session *self,
1030 struct mmap_data *md)
07800601
IM
1031{
1032 unsigned int head = mmap_read_head(md);
1033 unsigned int old = md->prev;
1034 unsigned char *data = md->base + page_size;
1035 int diff;
1036
07800601
IM
1037 /*
1038 * If we're further behind than half the buffer, there's a chance
2debbc83 1039 * the writer will bite our tail and mess up the samples under us.
07800601
IM
1040 *
1041 * If we somehow ended up ahead of the head, we got messed up.
1042 *
1043 * In either case, truncate and restart at head.
1044 */
1045 diff = head - old;
1046 if (diff > md->mask / 2 || diff < 0) {
f4f0b418 1047 fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
07800601
IM
1048
1049 /*
1050 * head points to a known good entry, start there.
1051 */
1052 old = head;
1053 }
1054
07800601 1055 for (; old != head;) {
07800601
IM
1056 event_t *event = (event_t *)&data[old & md->mask];
1057
1058 event_t event_copy;
1059
6f06ccbc 1060 size_t size = event->header.size;
07800601
IM
1061
1062 /*
1063 * Event straddles the mmap boundary -- header should always
1064 * be inside due to u64 alignment of output.
1065 */
1066 if ((old & md->mask) + size != ((old + size) & md->mask)) {
1067 unsigned int offset = old;
1068 unsigned int len = min(sizeof(*event), size), cpy;
1069 void *dst = &event_copy;
1070
1071 do {
1072 cpy = min(md->mask + 1 - (offset & md->mask), len);
1073 memcpy(dst, &data[offset & md->mask], cpy);
1074 offset += cpy;
1075 dst += cpy;
1076 len -= cpy;
1077 } while (len);
1078
1079 event = &event_copy;
1080 }
1081
5b2bb75a 1082 if (event->header.type == PERF_RECORD_SAMPLE)
b3165f41 1083 event__process_sample(event, self, md->counter);
5b2bb75a 1084 else
d8f66248 1085 event__process(event, self);
07800601 1086 old += size;
07800601
IM
1087 }
1088
1089 md->prev = old;
1090}
1091
c2990a2a
MG
1092static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
1093static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
1094
d8f66248 1095static void perf_session__mmap_read(struct perf_session *self)
2f01190a
FW
1096{
1097 int i, counter;
1098
1099 for (i = 0; i < nr_cpus; i++) {
1100 for (counter = 0; counter < nr_counters; counter++)
d8f66248 1101 perf_session__mmap_read_counter(self, &mmap_array[i][counter]);
2f01190a
FW
1102 }
1103}
1104
716c69fe
IM
1105int nr_poll;
1106int group_fd;
1107
1108static void start_counter(int i, int counter)
07800601 1109{
cdd6c482 1110 struct perf_event_attr *attr;
0fdc7e67 1111 int cpu;
716c69fe
IM
1112
1113 cpu = profile_cpu;
1114 if (target_pid == -1 && profile_cpu == -1)
1115 cpu = i;
1116
1117 attr = attrs + counter;
1118
1119 attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
7e4ff9e3
MG
1120
1121 if (freq) {
1122 attr->sample_type |= PERF_SAMPLE_PERIOD;
1123 attr->freq = 1;
1124 attr->sample_freq = freq;
1125 }
1126
0fdc7e67 1127 attr->inherit = (cpu < 0) && inherit;
5b2bb75a 1128 attr->mmap = 1;
716c69fe
IM
1129
1130try_again:
cdd6c482 1131 fd[i][counter] = sys_perf_event_open(attr, target_pid, cpu, group_fd, 0);
716c69fe
IM
1132
1133 if (fd[i][counter] < 0) {
1134 int err = errno;
1135
c10edee2 1136 if (err == EPERM || err == EACCES)
3da297a6 1137 die("No permission - are you root?\n");
716c69fe
IM
1138 /*
1139 * If it's cycles then fall back to hrtimer
1140 * based cpu-clock-tick sw counter, which
1141 * is always available even if no PMU support:
1142 */
1143 if (attr->type == PERF_TYPE_HARDWARE
f4dbfa8f 1144 && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
716c69fe 1145
3da297a6
IM
1146 if (verbose)
1147 warning(" ... trying to fall back to cpu-clock-ticks\n");
1148
716c69fe 1149 attr->type = PERF_TYPE_SOFTWARE;
f4dbfa8f 1150 attr->config = PERF_COUNT_SW_CPU_CLOCK;
716c69fe
IM
1151 goto try_again;
1152 }
30c806a0
IM
1153 printf("\n");
1154 error("perfcounter syscall returned with %d (%s)\n",
1155 fd[i][counter], strerror(err));
cdd6c482 1156 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
716c69fe
IM
1157 exit(-1);
1158 }
1159 assert(fd[i][counter] >= 0);
1160 fcntl(fd[i][counter], F_SETFL, O_NONBLOCK);
1161
1162 /*
1163 * First counter acts as the group leader:
1164 */
1165 if (group && group_fd == -1)
1166 group_fd = fd[i][counter];
1167
1168 event_array[nr_poll].fd = fd[i][counter];
1169 event_array[nr_poll].events = POLLIN;
1170 nr_poll++;
1171
1172 mmap_array[i][counter].counter = counter;
1173 mmap_array[i][counter].prev = 0;
1174 mmap_array[i][counter].mask = mmap_pages*page_size - 1;
1175 mmap_array[i][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
1176 PROT_READ, MAP_SHARED, fd[i][counter], 0);
1177 if (mmap_array[i][counter].base == MAP_FAILED)
1178 die("failed to mmap with %d (%s)\n", errno, strerror(errno));
1179}
1180
1181static int __cmd_top(void)
1182{
1183 pthread_t thread;
1184 int i, counter;
07800601 1185 int ret;
d8f66248 1186 /*
b3165f41
ACM
1187 * FIXME: perf_session__new should allow passing a O_MMAP, so that all this
1188 * mmap reading, etc is encapsulated in it. Use O_WRONLY for now.
d8f66248 1189 */
75be6cf4 1190 struct perf_session *session = perf_session__new(NULL, O_WRONLY, false);
b3165f41
ACM
1191 if (session == NULL)
1192 return -ENOMEM;
07800601 1193
5b2bb75a 1194 if (target_pid != -1)
d8f66248 1195 event__synthesize_thread(target_pid, event__process, session);
5b2bb75a 1196 else
d8f66248 1197 event__synthesize_threads(event__process, session);
5b2bb75a 1198
07800601
IM
1199 for (i = 0; i < nr_cpus; i++) {
1200 group_fd = -1;
716c69fe
IM
1201 for (counter = 0; counter < nr_counters; counter++)
1202 start_counter(i, counter);
07800601
IM
1203 }
1204
2f01190a
FW
1205 /* Wait for a minimal set of events before starting the snapshot */
1206 poll(event_array, nr_poll, 100);
1207
d8f66248 1208 perf_session__mmap_read(session);
2f01190a 1209
07800601
IM
1210 if (pthread_create(&thread, NULL, display_thread, NULL)) {
1211 printf("Could not create display thread.\n");
1212 exit(-1);
1213 }
1214
1215 if (realtime_prio) {
1216 struct sched_param param;
1217
1218 param.sched_priority = realtime_prio;
1219 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
1220 printf("Could not set realtime priority.\n");
1221 exit(-1);
1222 }
1223 }
1224
1225 while (1) {
2debbc83 1226 int hits = samples;
07800601 1227
d8f66248 1228 perf_session__mmap_read(session);
07800601 1229
2debbc83 1230 if (hits == samples)
07800601
IM
1231 ret = poll(event_array, nr_poll, 100);
1232 }
1233
1234 return 0;
1235}
b456bae0
IM
1236
1237static const char * const top_usage[] = {
1238 "perf top [<options>]",
1239 NULL
1240};
1241
b456bae0
IM
1242static const struct option options[] = {
1243 OPT_CALLBACK('e', "event", NULL, "event",
86847b62
TG
1244 "event selector. use 'perf list' to list available events",
1245 parse_events),
b456bae0
IM
1246 OPT_INTEGER('c', "count", &default_interval,
1247 "event period to sample"),
1248 OPT_INTEGER('p', "pid", &target_pid,
1249 "profile events on existing pid"),
1250 OPT_BOOLEAN('a', "all-cpus", &system_wide,
1251 "system-wide collection from all CPUs"),
1252 OPT_INTEGER('C', "CPU", &profile_cpu,
1253 "CPU to profile on"),
b32d133a
ACM
1254 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1255 "file", "vmlinux pathname"),
8ffcda17
ACM
1256 OPT_BOOLEAN('K', "hide_kernel_symbols", &hide_kernel_symbols,
1257 "hide kernel symbols"),
b456bae0
IM
1258 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
1259 "number of mmap data pages"),
1260 OPT_INTEGER('r', "realtime", &realtime_prio,
1261 "collect data with this RT SCHED_FIFO priority"),
db20c003 1262 OPT_INTEGER('d', "delay", &delay_secs,
b456bae0
IM
1263 "number of seconds to delay between refreshes"),
1264 OPT_BOOLEAN('D', "dump-symtab", &dump_symtab,
1265 "dump the symbol table used for profiling"),
6e53cdf1 1266 OPT_INTEGER('f', "count-filter", &count_filter,
b456bae0
IM
1267 "only display functions with more events than this"),
1268 OPT_BOOLEAN('g', "group", &group,
1269 "put the counters into a counter group"),
0fdc7e67
MG
1270 OPT_BOOLEAN('i', "inherit", &inherit,
1271 "child tasks inherit counters"),
923c42c1
MG
1272 OPT_STRING('s', "sym-annotate", &sym_filter, "symbol name",
1273 "symbol to annotate - requires -k option"),
1f208ea6 1274 OPT_BOOLEAN('z', "zero", &zero,
b456bae0 1275 "zero history across updates"),
6e53cdf1 1276 OPT_INTEGER('F', "freq", &freq,
b456bae0 1277 "profile at this frequency"),
6e53cdf1
IM
1278 OPT_INTEGER('E', "entries", &print_entries,
1279 "display this many functions"),
8ffcda17
ACM
1280 OPT_BOOLEAN('U', "hide_user_symbols", &hide_user_symbols,
1281 "hide user symbols"),
3da297a6
IM
1282 OPT_BOOLEAN('v', "verbose", &verbose,
1283 "be more verbose (show counter open errors, etc)"),
b456bae0
IM
1284 OPT_END()
1285};
1286
f37a291c 1287int cmd_top(int argc, const char **argv, const char *prefix __used)
b456bae0 1288{
b32d133a 1289 int counter;
b456bae0
IM
1290
1291 page_size = sysconf(_SC_PAGE_SIZE);
1292
b456bae0
IM
1293 argc = parse_options(argc, argv, options, top_usage, 0);
1294 if (argc)
1295 usage_with_options(top_usage, options);
1296
b456bae0
IM
1297 /* CPU and PID are mutually exclusive */
1298 if (target_pid != -1 && profile_cpu != -1) {
1299 printf("WARNING: PID switch overriding CPU\n");
1300 sleep(1);
1301 profile_cpu = -1;
1302 }
1303
a21ca2ca 1304 if (!nr_counters)
b456bae0 1305 nr_counters = 1;
b456bae0 1306
b32d133a
ACM
1307 symbol_conf.priv_size = (sizeof(struct sym_entry) +
1308 (nr_counters + 1) * sizeof(unsigned long));
1309 if (symbol_conf.vmlinux_name == NULL)
1310 symbol_conf.try_vmlinux_path = true;
75be6cf4 1311 if (symbol__init() < 0)
b32d133a 1312 return -1;
5a8e5a30 1313
2f335a02
FW
1314 if (delay_secs < 1)
1315 delay_secs = 1;
1316
923c42c1 1317 parse_source(sym_filter_entry);
a21ca2ca 1318
7e4ff9e3
MG
1319 /*
1320 * User specified count overrides default frequency.
1321 */
1322 if (default_interval)
1323 freq = 0;
1324 else if (freq) {
1325 default_interval = freq;
1326 } else {
1327 fprintf(stderr, "frequency and count are zero, aborting\n");
1328 exit(EXIT_FAILURE);
1329 }
1330
a21ca2ca
IM
1331 /*
1332 * Fill in the ones not specifically initialized via -c:
1333 */
b456bae0 1334 for (counter = 0; counter < nr_counters; counter++) {
a21ca2ca 1335 if (attrs[counter].sample_period)
b456bae0
IM
1336 continue;
1337
a21ca2ca 1338 attrs[counter].sample_period = default_interval;
b456bae0
IM
1339 }
1340
1341 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
1342 assert(nr_cpus <= MAX_NR_CPUS);
1343 assert(nr_cpus >= 0);
1344
1345 if (target_pid != -1 || profile_cpu != -1)
1346 nr_cpus = 1;
1347
13cc5079 1348 get_term_dimensions(&winsize);
3b6ed988 1349 if (print_entries == 0) {
13cc5079 1350 update_print_entries(&winsize);
3b6ed988
ACM
1351 signal(SIGWINCH, sig_winch_handler);
1352 }
1353
b456bae0
IM
1354 return __cmd_top();
1355}