perf annotate: Move locking to struct annotation
[deliverable/linux.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
36532461 23#include "util/annotate.h"
c0443df1 24#include "util/cache.h"
8fc0321f 25#include "util/color.h"
361c99a6 26#include "util/evlist.h"
69aad6f1 27#include "util/evsel.h"
b3165f41
ACM
28#include "util/session.h"
29#include "util/symbol.h"
439d473b 30#include "util/thread.h"
fd78260b 31#include "util/thread_map.h"
8c3e10eb 32#include "util/top.h"
148be2c1 33#include "util/util.h"
43cbcd8a 34#include <linux/rbtree.h>
b456bae0
IM
35#include "util/parse-options.h"
36#include "util/parse-events.h"
a12b51c4 37#include "util/cpumap.h"
69aad6f1 38#include "util/xyarray.h"
07800601 39
8f28827a
FW
40#include "util/debug.h"
41
07800601
IM
42#include <assert.h>
43#include <fcntl.h>
0e9b20b8 44
07800601 45#include <stdio.h>
923c42c1
MG
46#include <termios.h>
47#include <unistd.h>
9486aa38 48#include <inttypes.h>
0e9b20b8 49
07800601 50#include <errno.h>
07800601
IM
51#include <time.h>
52#include <sched.h>
07800601
IM
53
54#include <sys/syscall.h>
55#include <sys/ioctl.h>
56#include <sys/poll.h>
57#include <sys/prctl.h>
58#include <sys/wait.h>
59#include <sys/uio.h>
60#include <sys/mman.h>
61
62#include <linux/unistd.h>
63#include <linux/types.h>
64
69aad6f1 65#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
07800601 66
8c3e10eb
ACM
67static struct perf_top top = {
68 .count_filter = 5,
69 .delay_secs = 2,
70 .display_weighted = -1,
71 .target_pid = -1,
72 .target_tid = -1,
73 .active_symbols = LIST_HEAD_INIT(top.active_symbols),
74 .active_symbols_lock = PTHREAD_MUTEX_INITIALIZER,
75 .freq = 1000, /* 1 KHz */
76};
361c99a6 77
c0555642 78static bool system_wide = false;
07800601 79
c0443df1
ACM
80static bool use_tui, use_stdio;
81
7e4ff9e3 82static int default_interval = 0;
07800601 83
c0555642 84static bool inherit = false;
1967936d 85static int realtime_prio = 0;
c0555642 86static bool group = false;
07800601 87static unsigned int page_size;
70db7533 88static unsigned int mmap_pages = 128;
07800601 89
c0555642 90static bool dump_symtab = false;
07800601 91
13cc5079 92static struct winsize winsize;
8ffcda17 93
edb7c60e 94static const char *sym_filter = NULL;
42e59d7d 95struct sym_entry *sym_filter_entry = NULL;
6cff0e8d 96struct sym_entry *sym_filter_entry_sched = NULL;
42e59d7d 97static int sym_pcnt_filter = 5;
07800601 98
923c42c1
MG
99/*
100 * Source functions
101 */
102
895f0edc 103void get_term_dimensions(struct winsize *ws)
3b6ed988 104{
13cc5079
ACM
105 char *s = getenv("LINES");
106
107 if (s != NULL) {
108 ws->ws_row = atoi(s);
109 s = getenv("COLUMNS");
110 if (s != NULL) {
111 ws->ws_col = atoi(s);
112 if (ws->ws_row && ws->ws_col)
113 return;
114 }
3b6ed988 115 }
13cc5079
ACM
116#ifdef TIOCGWINSZ
117 if (ioctl(1, TIOCGWINSZ, ws) == 0 &&
118 ws->ws_row && ws->ws_col)
119 return;
3b6ed988 120#endif
13cc5079
ACM
121 ws->ws_row = 25;
122 ws->ws_col = 80;
3b6ed988
ACM
123}
124
13cc5079 125static void update_print_entries(struct winsize *ws)
3b6ed988 126{
8c3e10eb 127 top.print_entries = ws->ws_row;
13cc5079 128
8c3e10eb
ACM
129 if (top.print_entries > 9)
130 top.print_entries -= 9;
3b6ed988
ACM
131}
132
133static void sig_winch_handler(int sig __used)
134{
13cc5079
ACM
135 get_term_dimensions(&winsize);
136 update_print_entries(&winsize);
3b6ed988
ACM
137}
138
b0a9ab62 139static int parse_source(struct sym_entry *syme)
923c42c1
MG
140{
141 struct symbol *sym;
ce6f4fab 142 struct annotation *notes;
439d473b 143 struct map *map;
36532461 144 int err = -1;
923c42c1
MG
145
146 if (!syme)
b0a9ab62
ACM
147 return -1;
148
149 sym = sym_entry__symbol(syme);
150 map = syme->map;
151
152 /*
153 * We can't annotate with just /proc/kallsyms
154 */
ce6f4fab
ACM
155 if (map->dso->origin == DSO__ORIG_KERNEL) {
156 pr_err("Can't annotate %s: No vmlinux file was found in the "
157 "path\n", sym->name);
158 sleep(1);
b0a9ab62 159 return -1;
b269876c
ACM
160 }
161
ce6f4fab
ACM
162 notes = symbol__annotation(sym);
163 if (notes->src != NULL) {
164 pthread_mutex_lock(&notes->lock);
923c42c1
MG
165 goto out_assign;
166 }
923c42c1 167
ce6f4fab 168 pthread_mutex_lock(&notes->lock);
923c42c1 169
36532461
ACM
170 if (symbol__alloc_hist(sym, top.evlist->nr_entries) < 0) {
171 pr_err("Not enough memory for annotating '%s' symbol!\n",
172 sym->name);
ce6f4fab 173 sleep(1);
36532461 174 goto out_unlock;
923c42c1 175 }
36532461 176
ce6f4fab 177 err = symbol__annotate(sym, syme->map, 0);
36532461 178 if (err == 0) {
923c42c1
MG
179out_assign:
180 sym_filter_entry = syme;
36532461
ACM
181 }
182out_unlock:
ce6f4fab 183 pthread_mutex_unlock(&notes->lock);
36532461 184 return err;
923c42c1
MG
185}
186
187static void __zero_source_counters(struct sym_entry *syme)
188{
36532461
ACM
189 struct symbol *sym = sym_entry__symbol(syme);
190 symbol__annotate_zero_histograms(sym);
923c42c1
MG
191}
192
193static void record_precise_ip(struct sym_entry *syme, int counter, u64 ip)
194{
ce6f4fab
ACM
195 struct annotation *notes;
196 struct symbol *sym;
197
923c42c1
MG
198 if (syme != sym_filter_entry)
199 return;
200
ce6f4fab
ACM
201 sym = sym_entry__symbol(syme);
202 notes = symbol__annotation(sym);
203
204 if (pthread_mutex_trylock(&notes->lock))
923c42c1
MG
205 return;
206
36532461 207 ip = syme->map->map_ip(syme->map, ip);
ce6f4fab 208 symbol__inc_addr_samples(sym, syme->map, counter, ip);
c7ad21af 209
ce6f4fab 210 pthread_mutex_unlock(&notes->lock);
923c42c1
MG
211}
212
923c42c1
MG
213static void show_details(struct sym_entry *syme)
214{
ce6f4fab 215 struct annotation *notes;
923c42c1 216 struct symbol *symbol;
36532461 217 int more;
923c42c1
MG
218
219 if (!syme)
220 return;
221
36532461 222 symbol = sym_entry__symbol(syme);
ce6f4fab
ACM
223 notes = symbol__annotation(symbol);
224
225 pthread_mutex_lock(&notes->lock);
226
227 if (notes->src == NULL)
228 goto out_unlock;
923c42c1 229
8c3e10eb 230 printf("Showing %s for %s\n", event_name(top.sym_evsel), symbol->name);
923c42c1
MG
231 printf(" Events Pcnt (>=%d%%)\n", sym_pcnt_filter);
232
ce6f4fab
ACM
233 more = symbol__annotate_printf(symbol, syme->map, top.sym_evsel->idx,
234 0, sym_pcnt_filter, top.print_entries);
36532461
ACM
235 if (top.zero)
236 symbol__annotate_zero_histogram(symbol, top.sym_evsel->idx);
237 else
ce6f4fab 238 symbol__annotate_decay_histogram(symbol, top.sym_evsel->idx);
36532461 239 if (more != 0)
923c42c1 240 printf("%d lines not displayed, maybe increase display entries [e]\n", more);
ce6f4fab
ACM
241out_unlock:
242 pthread_mutex_unlock(&notes->lock);
923c42c1 243}
07800601 244
07800601
IM
245static const char CONSOLE_CLEAR[] = "\e[H\e[2J";
246
c44613a4 247static void __list_insert_active_sym(struct sym_entry *syme)
de04687f 248{
8c3e10eb 249 list_add(&syme->node, &top.active_symbols);
de04687f 250}
07800601 251
93fc64f1 252static void print_sym_table(struct perf_session *session)
07800601 253{
8c3e10eb
ACM
254 char bf[160];
255 int printed = 0;
de04687f 256 struct rb_node *nd;
8c3e10eb
ACM
257 struct sym_entry *syme;
258 struct rb_root tmp = RB_ROOT;
13cc5079 259 const int win_width = winsize.ws_col - 1;
8c3e10eb
ACM
260 int sym_width, dso_width, dso_short_width;
261 float sum_ksamples = perf_top__decay_samples(&top, &tmp);
d94b9430 262
0f5486b5 263 puts(CONSOLE_CLEAR);
07800601 264
8c3e10eb
ACM
265 perf_top__header_snprintf(&top, bf, sizeof(bf));
266 printf("%s\n", bf);
07800601 267
8c3e10eb 268 perf_top__reset_sample_counters(&top);
07800601 269
1a105f74 270 printf("%-*.*s\n", win_width, win_width, graph_dotted_line);
07800601 271
93fc64f1
ACM
272 if (session->hists.stats.total_lost != 0) {
273 color_fprintf(stdout, PERF_COLOR_RED, "WARNING:");
274 printf(" LOST %" PRIu64 " events, Check IO/CPU overload\n",
275 session->hists.stats.total_lost);
276 }
277
923c42c1
MG
278 if (sym_filter_entry) {
279 show_details(sym_filter_entry);
280 return;
281 }
282
8c3e10eb
ACM
283 perf_top__find_widths(&top, &tmp, &dso_width, &dso_short_width,
284 &sym_width);
13cc5079 285
b63be8d7
ACM
286 if (sym_width + dso_width > winsize.ws_col - 29) {
287 dso_width = dso_short_width;
288 if (sym_width + dso_width > winsize.ws_col - 29)
289 sym_width = winsize.ws_col - dso_width - 29;
290 }
7cc017ed 291 putchar('\n');
8c3e10eb 292 if (top.evlist->nr_entries == 1)
5b2bb75a 293 printf(" samples pcnt");
07800601 294 else
5b2bb75a 295 printf(" weight samples pcnt");
07800601 296
7ced156b
ACM
297 if (verbose)
298 printf(" RIP ");
7cc017ed 299 printf(" %-*.*s DSO\n", sym_width, sym_width, "function");
5b2bb75a 300 printf(" %s _______ _____",
8c3e10eb 301 top.evlist->nr_entries == 1 ? " " : "______");
7ced156b 302 if (verbose)
5b2bb75a 303 printf(" ________________");
1a105f74 304 printf(" %-*.*s", sym_width, sym_width, graph_line);
7cc017ed 305 printf(" %-*.*s", dso_width, dso_width, graph_line);
1a105f74 306 puts("\n");
07800601 307
de04687f 308 for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
83a0944f 309 struct symbol *sym;
8fc0321f 310 double pcnt;
d94b9430 311
83a0944f 312 syme = rb_entry(nd, struct sym_entry, rb_node);
51a472de 313 sym = sym_entry__symbol(syme);
8c3e10eb
ACM
314 if (++printed > top.print_entries ||
315 (int)syme->snap_count < top.count_filter)
c44613a4 316 continue;
d94b9430 317
2debbc83
IM
318 pcnt = 100.0 - (100.0 * ((sum_ksamples - syme->snap_count) /
319 sum_ksamples));
d94b9430 320
8c3e10eb 321 if (top.evlist->nr_entries == 1 || !top.display_weighted)
5b2bb75a 322 printf("%20.2f ", syme->weight);
d94b9430 323 else
5b2bb75a 324 printf("%9.1f %10ld ", syme->weight, syme->snap_count);
8fc0321f 325
1e11fd82 326 percent_color_fprintf(stdout, "%4.1f%%", pcnt);
7ced156b 327 if (verbose)
9486aa38 328 printf(" %016" PRIx64, sym->start);
13cc5079 329 printf(" %-*.*s", sym_width, sym_width, sym->name);
7cc017ed
ACM
330 printf(" %-*.*s\n", dso_width, dso_width,
331 dso_width >= syme->map->dso->long_name_len ?
332 syme->map->dso->long_name :
333 syme->map->dso->short_name);
07800601 334 }
07800601
IM
335}
336
923c42c1
MG
337static void prompt_integer(int *target, const char *msg)
338{
339 char *buf = malloc(0), *p;
340 size_t dummy = 0;
341 int tmp;
342
343 fprintf(stdout, "\n%s: ", msg);
344 if (getline(&buf, &dummy, stdin) < 0)
345 return;
346
347 p = strchr(buf, '\n');
348 if (p)
349 *p = 0;
350
351 p = buf;
352 while(*p) {
353 if (!isdigit(*p))
354 goto out_free;
355 p++;
356 }
357 tmp = strtoul(buf, NULL, 10);
358 *target = tmp;
359out_free:
360 free(buf);
361}
362
363static void prompt_percent(int *target, const char *msg)
364{
365 int tmp = 0;
366
367 prompt_integer(&tmp, msg);
368 if (tmp >= 0 && tmp <= 100)
369 *target = tmp;
370}
371
372static void prompt_symbol(struct sym_entry **target, const char *msg)
373{
374 char *buf = malloc(0), *p;
375 struct sym_entry *syme = *target, *n, *found = NULL;
376 size_t dummy = 0;
377
378 /* zero counters of active symbol */
379 if (syme) {
923c42c1
MG
380 __zero_source_counters(syme);
381 *target = NULL;
923c42c1
MG
382 }
383
384 fprintf(stdout, "\n%s: ", msg);
385 if (getline(&buf, &dummy, stdin) < 0)
386 goto out_free;
387
388 p = strchr(buf, '\n');
389 if (p)
390 *p = 0;
391
8c3e10eb
ACM
392 pthread_mutex_lock(&top.active_symbols_lock);
393 syme = list_entry(top.active_symbols.next, struct sym_entry, node);
394 pthread_mutex_unlock(&top.active_symbols_lock);
923c42c1 395
8c3e10eb 396 list_for_each_entry_safe_from(syme, n, &top.active_symbols, node) {
51a472de 397 struct symbol *sym = sym_entry__symbol(syme);
923c42c1
MG
398
399 if (!strcmp(buf, sym->name)) {
400 found = syme;
401 break;
402 }
403 }
404
405 if (!found) {
66aeb6d5 406 fprintf(stderr, "Sorry, %s is not active.\n", buf);
923c42c1
MG
407 sleep(1);
408 return;
409 } else
410 parse_source(found);
411
412out_free:
413 free(buf);
414}
415
091bd2e9 416static void print_mapped_keys(void)
923c42c1 417{
091bd2e9
MG
418 char *name = NULL;
419
420 if (sym_filter_entry) {
51a472de 421 struct symbol *sym = sym_entry__symbol(sym_filter_entry);
091bd2e9
MG
422 name = sym->name;
423 }
424
425 fprintf(stdout, "\nMapped keys:\n");
8c3e10eb
ACM
426 fprintf(stdout, "\t[d] display refresh delay. \t(%d)\n", top.delay_secs);
427 fprintf(stdout, "\t[e] display entries (lines). \t(%d)\n", top.print_entries);
091bd2e9 428
8c3e10eb
ACM
429 if (top.evlist->nr_entries > 1)
430 fprintf(stdout, "\t[E] active event counter. \t(%s)\n", event_name(top.sym_evsel));
091bd2e9 431
8c3e10eb 432 fprintf(stdout, "\t[f] profile display filter (count). \t(%d)\n", top.count_filter);
091bd2e9 433
6cff0e8d
KS
434 fprintf(stdout, "\t[F] annotate display filter (percent). \t(%d%%)\n", sym_pcnt_filter);
435 fprintf(stdout, "\t[s] annotate symbol. \t(%s)\n", name?: "NULL");
436 fprintf(stdout, "\t[S] stop annotation.\n");
091bd2e9 437
8c3e10eb
ACM
438 if (top.evlist->nr_entries > 1)
439 fprintf(stdout, "\t[w] toggle display weighted/count[E]r. \t(%d)\n", top.display_weighted ? 1 : 0);
091bd2e9 440
8ffcda17 441 fprintf(stdout,
1a72cfa6 442 "\t[K] hide kernel_symbols symbols. \t(%s)\n",
8c3e10eb 443 top.hide_kernel_symbols ? "yes" : "no");
8ffcda17
ACM
444 fprintf(stdout,
445 "\t[U] hide user symbols. \t(%s)\n",
8c3e10eb
ACM
446 top.hide_user_symbols ? "yes" : "no");
447 fprintf(stdout, "\t[z] toggle sample zeroing. \t(%d)\n", top.zero ? 1 : 0);
091bd2e9
MG
448 fprintf(stdout, "\t[qQ] quit.\n");
449}
450
451static int key_mapped(int c)
452{
453 switch (c) {
454 case 'd':
455 case 'e':
456 case 'f':
457 case 'z':
458 case 'q':
459 case 'Q':
8ffcda17
ACM
460 case 'K':
461 case 'U':
6cff0e8d
KS
462 case 'F':
463 case 's':
464 case 'S':
091bd2e9
MG
465 return 1;
466 case 'E':
467 case 'w':
8c3e10eb 468 return top.evlist->nr_entries > 1 ? 1 : 0;
83a0944f
IM
469 default:
470 break;
091bd2e9
MG
471 }
472
473 return 0;
923c42c1
MG
474}
475
a1645ce1 476static void handle_keypress(struct perf_session *session, int c)
923c42c1 477{
091bd2e9
MG
478 if (!key_mapped(c)) {
479 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
480 struct termios tc, save;
481
482 print_mapped_keys();
483 fprintf(stdout, "\nEnter selection, or unmapped key to continue: ");
484 fflush(stdout);
485
486 tcgetattr(0, &save);
487 tc = save;
488 tc.c_lflag &= ~(ICANON | ECHO);
489 tc.c_cc[VMIN] = 0;
490 tc.c_cc[VTIME] = 0;
491 tcsetattr(0, TCSANOW, &tc);
492
493 poll(&stdin_poll, 1, -1);
494 c = getc(stdin);
495
496 tcsetattr(0, TCSAFLUSH, &save);
497 if (!key_mapped(c))
498 return;
499 }
500
923c42c1
MG
501 switch (c) {
502 case 'd':
8c3e10eb
ACM
503 prompt_integer(&top.delay_secs, "Enter display delay");
504 if (top.delay_secs < 1)
505 top.delay_secs = 1;
923c42c1
MG
506 break;
507 case 'e':
8c3e10eb
ACM
508 prompt_integer(&top.print_entries, "Enter display entries (lines)");
509 if (top.print_entries == 0) {
13cc5079 510 sig_winch_handler(SIGWINCH);
3b6ed988
ACM
511 signal(SIGWINCH, sig_winch_handler);
512 } else
513 signal(SIGWINCH, SIG_DFL);
923c42c1
MG
514 break;
515 case 'E':
8c3e10eb 516 if (top.evlist->nr_entries > 1) {
923c42c1 517 fprintf(stderr, "\nAvailable events:");
69aad6f1 518
8c3e10eb
ACM
519 list_for_each_entry(top.sym_evsel, &top.evlist->entries, node)
520 fprintf(stderr, "\n\t%d %s", top.sym_evsel->idx, event_name(top.sym_evsel));
923c42c1 521
8c3e10eb 522 prompt_integer(&top.sym_counter, "Enter details event counter");
923c42c1 523
8c3e10eb
ACM
524 if (top.sym_counter >= top.evlist->nr_entries) {
525 top.sym_evsel = list_entry(top.evlist->entries.next, struct perf_evsel, node);
526 top.sym_counter = 0;
527 fprintf(stderr, "Sorry, no such event, using %s.\n", event_name(top.sym_evsel));
923c42c1 528 sleep(1);
69aad6f1 529 break;
923c42c1 530 }
8c3e10eb
ACM
531 list_for_each_entry(top.sym_evsel, &top.evlist->entries, node)
532 if (top.sym_evsel->idx == top.sym_counter)
69aad6f1 533 break;
8c3e10eb 534 } else top.sym_counter = 0;
923c42c1
MG
535 break;
536 case 'f':
8c3e10eb 537 prompt_integer(&top.count_filter, "Enter display event count filter");
923c42c1
MG
538 break;
539 case 'F':
540 prompt_percent(&sym_pcnt_filter, "Enter details display event filter (percent)");
541 break;
8ffcda17 542 case 'K':
8c3e10eb 543 top.hide_kernel_symbols = !top.hide_kernel_symbols;
8ffcda17 544 break;
923c42c1
MG
545 case 'q':
546 case 'Q':
547 printf("exiting.\n");
c338aee8 548 if (dump_symtab)
cbf69680 549 perf_session__fprintf_dsos(session, stderr);
923c42c1
MG
550 exit(0);
551 case 's':
552 prompt_symbol(&sym_filter_entry, "Enter details symbol");
553 break;
554 case 'S':
555 if (!sym_filter_entry)
556 break;
557 else {
558 struct sym_entry *syme = sym_filter_entry;
559
923c42c1
MG
560 sym_filter_entry = NULL;
561 __zero_source_counters(syme);
923c42c1
MG
562 }
563 break;
8ffcda17 564 case 'U':
8c3e10eb 565 top.hide_user_symbols = !top.hide_user_symbols;
8ffcda17 566 break;
46ab9764 567 case 'w':
8c3e10eb 568 top.display_weighted = ~top.display_weighted;
46ab9764 569 break;
923c42c1 570 case 'z':
8c3e10eb 571 top.zero = !top.zero;
923c42c1 572 break;
83a0944f
IM
573 default:
574 break;
923c42c1
MG
575 }
576}
577
c0443df1
ACM
578static void *display_thread_tui(void *arg __used)
579{
580 perf_top__tui_browser(&top);
581 exit_browser(0);
582 exit(0);
583 return NULL;
584}
585
f37a291c 586static void *display_thread(void *arg __used)
07800601 587{
0f5486b5 588 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
923c42c1
MG
589 struct termios tc, save;
590 int delay_msecs, c;
a1645ce1 591 struct perf_session *session = (struct perf_session *) arg;
923c42c1
MG
592
593 tcgetattr(0, &save);
594 tc = save;
595 tc.c_lflag &= ~(ICANON | ECHO);
596 tc.c_cc[VMIN] = 0;
597 tc.c_cc[VTIME] = 0;
091bd2e9 598
923c42c1 599repeat:
8c3e10eb 600 delay_msecs = top.delay_secs * 1000;
923c42c1
MG
601 tcsetattr(0, TCSANOW, &tc);
602 /* trash return*/
603 getc(stdin);
07800601 604
0f5486b5 605 do {
93fc64f1 606 print_sym_table(session);
0f5486b5
FW
607 } while (!poll(&stdin_poll, 1, delay_msecs) == 1);
608
923c42c1
MG
609 c = getc(stdin);
610 tcsetattr(0, TCSAFLUSH, &save);
611
a1645ce1 612 handle_keypress(session, c);
923c42c1 613 goto repeat;
07800601
IM
614
615 return NULL;
616}
617
2ab52083 618/* Tag samples to be skipped. */
f37a291c 619static const char *skip_symbols[] = {
2ab52083 620 "default_idle",
b0e8572f 621 "native_safe_halt",
2ab52083
AB
622 "cpu_idle",
623 "enter_idle",
624 "exit_idle",
625 "mwait_idle",
59b90056 626 "mwait_idle_with_hints",
8357275b 627 "poll_idle",
3a3393ef
AB
628 "ppc64_runlatch_off",
629 "pseries_dedicated_idle_sleep",
2ab52083
AB
630 NULL
631};
632
439d473b 633static int symbol_filter(struct map *map, struct symbol *sym)
07800601 634{
de04687f
ACM
635 struct sym_entry *syme;
636 const char *name = sym->name;
2ab52083 637 int i;
de04687f 638
3a3393ef
AB
639 /*
640 * ppc64 uses function descriptors and appends a '.' to the
641 * start of every instruction address. Remove it.
642 */
643 if (name[0] == '.')
644 name++;
645
de04687f
ACM
646 if (!strcmp(name, "_text") ||
647 !strcmp(name, "_etext") ||
648 !strcmp(name, "_sinittext") ||
649 !strncmp("init_module", name, 11) ||
650 !strncmp("cleanup_module", name, 14) ||
651 strstr(name, "_text_start") ||
652 strstr(name, "_text_end"))
07800601 653 return 1;
07800601 654
00a192b3 655 syme = symbol__priv(sym);
439d473b 656 syme->map = map;
ce6f4fab 657 symbol__annotate_init(map, sym);
6cff0e8d
KS
658
659 if (!sym_filter_entry && sym_filter && !strcmp(name, sym_filter)) {
660 /* schedule initial sym_filter_entry setup */
661 sym_filter_entry_sched = syme;
662 sym_filter = NULL;
663 }
923c42c1 664
2ab52083
AB
665 for (i = 0; skip_symbols[i]; i++) {
666 if (!strcmp(skip_symbols[i], name)) {
667 syme->skip = 1;
668 break;
669 }
670 }
07800601 671
07800601
IM
672 return 0;
673}
674
8115d60c
ACM
675static void perf_event__process_sample(const union perf_event *event,
676 struct perf_sample *sample,
677 struct perf_session *session)
07800601 678{
8115d60c 679 u64 ip = event->ip.ip;
5b2bb75a 680 struct sym_entry *syme;
1ed091c4 681 struct addr_location al;
23346f21 682 struct machine *machine;
8115d60c 683 u8 origin = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
5b2bb75a 684
8c3e10eb 685 ++top.samples;
24bfef0f 686
8ffcda17 687 switch (origin) {
1ed091c4 688 case PERF_RECORD_MISC_USER:
8c3e10eb
ACM
689 ++top.us_samples;
690 if (top.hide_user_symbols)
8ffcda17 691 return;
23346f21 692 machine = perf_session__find_host_machine(session);
1ed091c4 693 break;
5b2bb75a 694 case PERF_RECORD_MISC_KERNEL:
8c3e10eb
ACM
695 ++top.kernel_samples;
696 if (top.hide_kernel_symbols)
8ffcda17 697 return;
23346f21 698 machine = perf_session__find_host_machine(session);
a1645ce1
ZY
699 break;
700 case PERF_RECORD_MISC_GUEST_KERNEL:
8c3e10eb 701 ++top.guest_kernel_samples;
8115d60c 702 machine = perf_session__find_machine(session, event->ip.pid);
5b2bb75a 703 break;
a1645ce1 704 case PERF_RECORD_MISC_GUEST_USER:
8c3e10eb 705 ++top.guest_us_samples;
a1645ce1
ZY
706 /*
707 * TODO: we don't process guest user from host side
708 * except simple counting.
709 */
710 return;
5b2bb75a
ACM
711 default:
712 return;
713 }
714
23346f21 715 if (!machine && perf_guest) {
a1645ce1 716 pr_err("Can't find guest [%d]'s kernel information\n",
8115d60c 717 event->ip.pid);
a1645ce1
ZY
718 return;
719 }
720
8115d60c 721 if (event->header.misc & PERF_RECORD_MISC_EXACT_IP)
8c3e10eb 722 top.exact_samples++;
1676b8a0 723
8115d60c
ACM
724 if (perf_event__preprocess_sample(event, session, &al, sample,
725 symbol_filter) < 0 ||
72b8fa17 726 al.filtered)
1ed091c4 727 return;
07800601 728
72b8fa17
ACM
729 if (al.sym == NULL) {
730 /*
731 * As we do lazy loading of symtabs we only will know if the
732 * specified vmlinux file is invalid when we actually have a
733 * hit in kernel space and then try to load it. So if we get
734 * here and there are _no_ symbols in the DSO backing the
735 * kernel map, bail out.
736 *
737 * We may never get here, for instance, if we use -K/
738 * --hide-kernel-symbols, even if the user specifies an
739 * invalid --vmlinux ;-)
740 */
23346f21 741 if (al.map == machine->vmlinux_maps[MAP__FUNCTION] &&
72b8fa17
ACM
742 RB_EMPTY_ROOT(&al.map->dso->symbols[MAP__FUNCTION])) {
743 pr_err("The %s file can't be used\n",
744 symbol_conf.vmlinux_name);
745 exit(1);
746 }
747
748 return;
749 }
750
6cff0e8d
KS
751 /* let's see, whether we need to install initial sym_filter_entry */
752 if (sym_filter_entry_sched) {
753 sym_filter_entry = sym_filter_entry_sched;
754 sym_filter_entry_sched = NULL;
b0a9ab62
ACM
755 if (parse_source(sym_filter_entry) < 0) {
756 struct symbol *sym = sym_entry__symbol(sym_filter_entry);
757
758 pr_err("Can't annotate %s", sym->name);
759 if (sym_filter_entry->map->dso->origin == DSO__ORIG_KERNEL) {
760 pr_err(": No vmlinux file was found in the path:\n");
5ad90e4e 761 machine__fprintf_vmlinux_path(machine, stderr);
b0a9ab62
ACM
762 } else
763 pr_err(".\n");
764 exit(1);
765 }
6cff0e8d
KS
766 }
767
1ed091c4 768 syme = symbol__priv(al.sym);
5b2bb75a 769 if (!syme->skip) {
70db7533
ACM
770 struct perf_evsel *evsel;
771
8ffcda17 772 syme->origin = origin;
8c3e10eb 773 evsel = perf_evlist__id2evsel(top.evlist, sample->id);
70db7533
ACM
774 assert(evsel != NULL);
775 syme->count[evsel->idx]++;
69aad6f1 776 record_precise_ip(syme, evsel->idx, ip);
8c3e10eb 777 pthread_mutex_lock(&top.active_symbols_lock);
5b2bb75a
ACM
778 if (list_empty(&syme->node) || !syme->node.next)
779 __list_insert_active_sym(syme);
8c3e10eb 780 pthread_mutex_unlock(&top.active_symbols_lock);
5b2bb75a 781 }
07800601
IM
782}
783
70db7533 784static void perf_session__mmap_read_cpu(struct perf_session *self, int cpu)
07800601 785{
8d50e5b4 786 struct perf_sample sample;
8115d60c 787 union perf_event *event;
07800601 788
8c3e10eb 789 while ((event = perf_evlist__read_on_cpu(top.evlist, cpu)) != NULL) {
d0dd74e8 790 perf_session__parse_sample(self, event, &sample);
04391deb 791
5b2bb75a 792 if (event->header.type == PERF_RECORD_SAMPLE)
8115d60c 793 perf_event__process_sample(event, &sample, self);
5b2bb75a 794 else
8115d60c 795 perf_event__process(event, &sample, self);
07800601 796 }
07800601
IM
797}
798
d8f66248 799static void perf_session__mmap_read(struct perf_session *self)
2f01190a 800{
70db7533
ACM
801 int i;
802
8c3e10eb 803 for (i = 0; i < top.evlist->cpus->nr; i++)
70db7533 804 perf_session__mmap_read_cpu(self, i);
2f01190a
FW
805}
806
72cb7013
ACM
807static void start_counters(struct perf_evlist *evlist)
808{
809 struct perf_evsel *counter;
7e4ff9e3 810
72cb7013
ACM
811 list_for_each_entry(counter, &evlist->entries, node) {
812 struct perf_event_attr *attr = &counter->attr;
716c69fe 813
72cb7013
ACM
814 attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
815
8c3e10eb 816 if (top.freq) {
72cb7013
ACM
817 attr->sample_type |= PERF_SAMPLE_PERIOD;
818 attr->freq = 1;
8c3e10eb 819 attr->sample_freq = top.freq;
72cb7013 820 }
d6d901c2 821
70db7533
ACM
822 if (evlist->nr_entries > 1) {
823 attr->sample_type |= PERF_SAMPLE_ID;
824 attr->read_format |= PERF_FORMAT_ID;
825 }
826
72cb7013
ACM
827 attr->mmap = 1;
828try_again:
8c3e10eb
ACM
829 if (perf_evsel__open(counter, top.evlist->cpus,
830 top.evlist->threads, group, inherit) < 0) {
d6d901c2
ZY
831 int err = errno;
832
833 if (err == EPERM || err == EACCES)
d9cf837e
CA
834 die("Permission error - are you root?\n"
835 "\t Consider tweaking"
836 " /proc/sys/kernel/perf_event_paranoid.\n");
d6d901c2
ZY
837 /*
838 * If it's cycles then fall back to hrtimer
839 * based cpu-clock-tick sw counter, which
840 * is always available even if no PMU support:
841 */
72cb7013
ACM
842 if (attr->type == PERF_TYPE_HARDWARE &&
843 attr->config == PERF_COUNT_HW_CPU_CYCLES) {
d6d901c2
ZY
844
845 if (verbose)
846 warning(" ... trying to fall back to cpu-clock-ticks\n");
847
848 attr->type = PERF_TYPE_SOFTWARE;
849 attr->config = PERF_COUNT_SW_CPU_CLOCK;
850 goto try_again;
851 }
852 printf("\n");
72cb7013
ACM
853 error("sys_perf_event_open() syscall returned with %d "
854 "(%s). /bin/dmesg may provide additional information.\n",
855 err, strerror(err));
d6d901c2
ZY
856 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
857 exit(-1);
858 }
716c69fe 859 }
70db7533 860
7e2ed097 861 if (perf_evlist__mmap(evlist, mmap_pages, false) < 0)
70db7533 862 die("failed to mmap with %d (%s)\n", errno, strerror(errno));
716c69fe
IM
863}
864
865static int __cmd_top(void)
866{
867 pthread_t thread;
70db7533 868 struct perf_evsel *first;
fb7d0b3c 869 int ret __used;
d8f66248 870 /*
b3165f41
ACM
871 * FIXME: perf_session__new should allow passing a O_MMAP, so that all this
872 * mmap reading, etc is encapsulated in it. Use O_WRONLY for now.
d8f66248 873 */
21ef97f0 874 struct perf_session *session = perf_session__new(NULL, O_WRONLY, false, false, NULL);
b3165f41
ACM
875 if (session == NULL)
876 return -ENOMEM;
07800601 877
8c3e10eb
ACM
878 if (top.target_tid != -1)
879 perf_event__synthesize_thread(top.target_tid, perf_event__process,
8115d60c 880 session);
5b2bb75a 881 else
8115d60c 882 perf_event__synthesize_threads(perf_event__process, session);
5b2bb75a 883
8c3e10eb
ACM
884 start_counters(top.evlist);
885 first = list_entry(top.evlist->entries.next, struct perf_evsel, node);
70db7533 886 perf_session__set_sample_type(session, first->attr.sample_type);
07800601 887
2f01190a 888 /* Wait for a minimal set of events before starting the snapshot */
8c3e10eb 889 poll(top.evlist->pollfd, top.evlist->nr_fds, 100);
2f01190a 890
d8f66248 891 perf_session__mmap_read(session);
2f01190a 892
c0443df1
ACM
893 if (pthread_create(&thread, NULL, (use_browser > 0 ? display_thread_tui :
894 display_thread), session)) {
07800601
IM
895 printf("Could not create display thread.\n");
896 exit(-1);
897 }
898
899 if (realtime_prio) {
900 struct sched_param param;
901
902 param.sched_priority = realtime_prio;
903 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
904 printf("Could not set realtime priority.\n");
905 exit(-1);
906 }
907 }
908
909 while (1) {
8c3e10eb 910 u64 hits = top.samples;
07800601 911
d8f66248 912 perf_session__mmap_read(session);
07800601 913
8c3e10eb
ACM
914 if (hits == top.samples)
915 ret = poll(top.evlist->pollfd, top.evlist->nr_fds, 100);
07800601
IM
916 }
917
918 return 0;
919}
b456bae0
IM
920
921static const char * const top_usage[] = {
922 "perf top [<options>]",
923 NULL
924};
925
b456bae0 926static const struct option options[] = {
8c3e10eb 927 OPT_CALLBACK('e', "event", &top.evlist, "event",
86847b62
TG
928 "event selector. use 'perf list' to list available events",
929 parse_events),
b456bae0
IM
930 OPT_INTEGER('c', "count", &default_interval,
931 "event period to sample"),
8c3e10eb 932 OPT_INTEGER('p', "pid", &top.target_pid,
d6d901c2 933 "profile events on existing process id"),
8c3e10eb 934 OPT_INTEGER('t', "tid", &top.target_tid,
d6d901c2 935 "profile events on existing thread id"),
b456bae0
IM
936 OPT_BOOLEAN('a', "all-cpus", &system_wide,
937 "system-wide collection from all CPUs"),
8c3e10eb 938 OPT_STRING('C', "cpu", &top.cpu_list, "cpu",
c45c6ea2 939 "list of cpus to monitor"),
b32d133a
ACM
940 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
941 "file", "vmlinux pathname"),
8c3e10eb 942 OPT_BOOLEAN('K', "hide_kernel_symbols", &top.hide_kernel_symbols,
8ffcda17 943 "hide kernel symbols"),
1967936d 944 OPT_UINTEGER('m', "mmap-pages", &mmap_pages, "number of mmap data pages"),
b456bae0
IM
945 OPT_INTEGER('r', "realtime", &realtime_prio,
946 "collect data with this RT SCHED_FIFO priority"),
8c3e10eb 947 OPT_INTEGER('d', "delay", &top.delay_secs,
b456bae0
IM
948 "number of seconds to delay between refreshes"),
949 OPT_BOOLEAN('D', "dump-symtab", &dump_symtab,
950 "dump the symbol table used for profiling"),
8c3e10eb 951 OPT_INTEGER('f', "count-filter", &top.count_filter,
b456bae0
IM
952 "only display functions with more events than this"),
953 OPT_BOOLEAN('g', "group", &group,
954 "put the counters into a counter group"),
0fdc7e67
MG
955 OPT_BOOLEAN('i', "inherit", &inherit,
956 "child tasks inherit counters"),
923c42c1 957 OPT_STRING('s', "sym-annotate", &sym_filter, "symbol name",
6cff0e8d 958 "symbol to annotate"),
8c3e10eb 959 OPT_BOOLEAN('z', "zero", &top.zero,
b456bae0 960 "zero history across updates"),
8c3e10eb 961 OPT_INTEGER('F', "freq", &top.freq,
b456bae0 962 "profile at this frequency"),
8c3e10eb 963 OPT_INTEGER('E', "entries", &top.print_entries,
6e53cdf1 964 "display this many functions"),
8c3e10eb 965 OPT_BOOLEAN('U', "hide_user_symbols", &top.hide_user_symbols,
8ffcda17 966 "hide user symbols"),
c0443df1
ACM
967 OPT_BOOLEAN(0, "tui", &use_tui, "Use the TUI interface"),
968 OPT_BOOLEAN(0, "stdio", &use_stdio, "Use the stdio interface"),
c0555642 969 OPT_INCR('v', "verbose", &verbose,
3da297a6 970 "be more verbose (show counter open errors, etc)"),
b456bae0
IM
971 OPT_END()
972};
973
f37a291c 974int cmd_top(int argc, const char **argv, const char *prefix __used)
b456bae0 975{
69aad6f1
ACM
976 struct perf_evsel *pos;
977 int status = -ENOMEM;
b456bae0 978
8c3e10eb
ACM
979 top.evlist = perf_evlist__new(NULL, NULL);
980 if (top.evlist == NULL)
361c99a6
ACM
981 return -ENOMEM;
982
b456bae0
IM
983 page_size = sysconf(_SC_PAGE_SIZE);
984
b456bae0
IM
985 argc = parse_options(argc, argv, options, top_usage, 0);
986 if (argc)
987 usage_with_options(top_usage, options);
988
c0443df1
ACM
989 /*
990 * XXX For now start disabled, only using TUI if explicitely asked for.
991 * Change that when handle_keys equivalent gets written, live annotation
992 * done, etc.
993 */
994 use_browser = 0;
995
996 if (use_stdio)
997 use_browser = 0;
998 else if (use_tui)
999 use_browser = 1;
1000
1001 setup_browser(false);
1002
b456bae0 1003 /* CPU and PID are mutually exclusive */
8c3e10eb 1004 if (top.target_tid > 0 && top.cpu_list) {
b456bae0
IM
1005 printf("WARNING: PID switch overriding CPU\n");
1006 sleep(1);
8c3e10eb 1007 top.cpu_list = NULL;
b456bae0
IM
1008 }
1009
8c3e10eb
ACM
1010 if (top.target_pid != -1)
1011 top.target_tid = top.target_pid;
7e2ed097 1012
8c3e10eb
ACM
1013 if (perf_evlist__create_maps(top.evlist, top.target_pid,
1014 top.target_tid, top.cpu_list) < 0)
7e2ed097
ACM
1015 usage_with_options(top_usage, options);
1016
8c3e10eb
ACM
1017 if (!top.evlist->nr_entries &&
1018 perf_evlist__add_default(top.evlist) < 0) {
69aad6f1
ACM
1019 pr_err("Not enough memory for event selector list\n");
1020 return -ENOMEM;
1021 }
5a8e5a30 1022
8c3e10eb
ACM
1023 if (top.delay_secs < 1)
1024 top.delay_secs = 1;
2f335a02 1025
7e4ff9e3
MG
1026 /*
1027 * User specified count overrides default frequency.
1028 */
1029 if (default_interval)
8c3e10eb
ACM
1030 top.freq = 0;
1031 else if (top.freq) {
1032 default_interval = top.freq;
7e4ff9e3
MG
1033 } else {
1034 fprintf(stderr, "frequency and count are zero, aborting\n");
1035 exit(EXIT_FAILURE);
1036 }
1037
8c3e10eb
ACM
1038 list_for_each_entry(pos, &top.evlist->entries, node) {
1039 if (perf_evsel__alloc_fd(pos, top.evlist->cpus->nr,
1040 top.evlist->threads->nr) < 0)
69aad6f1
ACM
1041 goto out_free_fd;
1042 /*
1043 * Fill in the ones not specifically initialized via -c:
1044 */
1045 if (pos->attr.sample_period)
1046 continue;
1047
1048 pos->attr.sample_period = default_interval;
1049 }
1050
8c3e10eb
ACM
1051 if (perf_evlist__alloc_pollfd(top.evlist) < 0 ||
1052 perf_evlist__alloc_mmap(top.evlist) < 0)
5c581041
ACM
1053 goto out_free_fd;
1054
8c3e10eb 1055 top.sym_evsel = list_entry(top.evlist->entries.next, struct perf_evsel, node);
cc841580 1056
36532461 1057 symbol_conf.priv_size = (sizeof(struct sym_entry) + sizeof(struct annotation) +
8c3e10eb 1058 (top.evlist->nr_entries + 1) * sizeof(unsigned long));
69aad6f1
ACM
1059
1060 symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL);
1061 if (symbol__init() < 0)
1062 return -1;
1063
13cc5079 1064 get_term_dimensions(&winsize);
8c3e10eb 1065 if (top.print_entries == 0) {
13cc5079 1066 update_print_entries(&winsize);
3b6ed988
ACM
1067 signal(SIGWINCH, sig_winch_handler);
1068 }
1069
69aad6f1
ACM
1070 status = __cmd_top();
1071out_free_fd:
8c3e10eb 1072 perf_evlist__delete(top.evlist);
69aad6f1
ACM
1073
1074 return status;
b456bae0 1075}
This page took 0.16097 seconds and 5 git commands to generate.