blob: 0b07cc30b669036c8ed7b9ae443d6985749e189d [file] [log] [blame]
Ingo Molnar07800602009-04-20 15:00:56 +02001/*
Ingo Molnarbf9e1872009-06-02 23:37:05 +02002 * builtin-top.c
3 *
4 * Builtin top command: Display a continuously updated profile of
5 * any workload, CPU or specific PID.
6 *
7 * Copyright (C) 2008, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
8 *
9 * Improvements and fixes by:
10 *
11 * Arjan van de Ven <arjan@linux.intel.com>
12 * Yanmin Zhang <yanmin.zhang@intel.com>
13 * Wu Fengguang <fengguang.wu@intel.com>
14 * Mike Galbraith <efault@gmx.de>
15 * Paul Mackerras <paulus@samba.org>
16 *
17 * Released under the GPL v2. (and only v2, not any later version)
Ingo Molnar07800602009-04-20 15:00:56 +020018 */
Ingo Molnarbf9e1872009-06-02 23:37:05 +020019#include "builtin.h"
Ingo Molnar07800602009-04-20 15:00:56 +020020
Peter Zijlstra1a482f32009-05-23 18:28:58 +020021#include "perf.h"
Ingo Molnarbf9e1872009-06-02 23:37:05 +020022
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -020023#include "util/annotate.h"
Arnaldo Carvalho de Meloc0443df2011-01-31 18:19:33 -020024#include "util/cache.h"
Ingo Molnar8fc03212009-06-04 15:19:47 +020025#include "util/color.h"
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020026#include "util/evlist.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020027#include "util/evsel.h"
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -020028#include "util/session.h"
29#include "util/symbol.h"
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030030#include "util/thread.h"
Arnaldo Carvalho de Melofd782602011-01-18 15:15:24 -020031#include "util/thread_map.h"
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -020032#include "util/top.h"
Ingo Molnar148be2c2009-04-27 08:02:14 +020033#include "util/util.h"
Arnaldo Carvalho de Melo43cbcd82009-07-01 12:28:37 -030034#include <linux/rbtree.h>
Ingo Molnarb456bae2009-05-26 09:17:18 +020035#include "util/parse-options.h"
36#include "util/parse-events.h"
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110037#include "util/cpumap.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020038#include "util/xyarray.h"
Ingo Molnar07800602009-04-20 15:00:56 +020039
Frederic Weisbecker8f288272009-08-16 22:05:48 +020040#include "util/debug.h"
41
Ingo Molnar07800602009-04-20 15:00:56 +020042#include <assert.h>
43#include <fcntl.h>
Ingo Molnar0e9b20b2009-05-26 09:17:18 +020044
Ingo Molnar07800602009-04-20 15:00:56 +020045#include <stdio.h>
Mike Galbraith923c42c2009-07-22 20:36:03 +020046#include <termios.h>
47#include <unistd.h>
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -020048#include <inttypes.h>
Ingo Molnar0e9b20b2009-05-26 09:17:18 +020049
Ingo Molnar07800602009-04-20 15:00:56 +020050#include <errno.h>
Ingo Molnar07800602009-04-20 15:00:56 +020051#include <time.h>
52#include <sched.h>
Ingo Molnar07800602009-04-20 15:00:56 +020053
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
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020065#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
Ingo Molnara21ca2c2009-06-06 09:58:57 +020066
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -020067static 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};
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020077
Ian Munsiec0555642010-04-13 18:37:33 +100078static bool system_wide = false;
Ingo Molnar07800602009-04-20 15:00:56 +020079
Arnaldo Carvalho de Meloc0443df2011-01-31 18:19:33 -020080static bool use_tui, use_stdio;
81
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +020082static int default_interval = 0;
Ingo Molnar07800602009-04-20 15:00:56 +020083
Ian Munsiec0555642010-04-13 18:37:33 +100084static bool inherit = false;
Arnaldo Carvalho de Melo19679362010-05-17 15:39:16 -030085static int realtime_prio = 0;
Ian Munsiec0555642010-04-13 18:37:33 +100086static bool group = false;
Ingo Molnar07800602009-04-20 15:00:56 +020087static unsigned int page_size;
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -020088static unsigned int mmap_pages = 128;
Ingo Molnar07800602009-04-20 15:00:56 +020089
Ian Munsiec0555642010-04-13 18:37:33 +100090static bool dump_symtab = false;
Ingo Molnar07800602009-04-20 15:00:56 +020091
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -020092static struct winsize winsize;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -020093
Arnaldo Carvalho de Meloedb7c602010-05-17 16:22:41 -030094static const char *sym_filter = NULL;
Kirill Smelkov6cff0e82010-02-03 16:52:08 -020095struct sym_entry *sym_filter_entry_sched = NULL;
Ingo Molnar42e59d72009-10-06 15:14:21 +020096static int sym_pcnt_filter = 5;
Ingo Molnar07800602009-04-20 15:00:56 +020097
Mike Galbraith923c42c2009-07-22 20:36:03 +020098/*
99 * Source functions
100 */
101
Arnaldo Carvalho de Melo895f0ed2010-03-11 20:12:41 -0300102void get_term_dimensions(struct winsize *ws)
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200103{
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200104 char *s = getenv("LINES");
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200105
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200106 if (s != NULL) {
107 ws->ws_row = atoi(s);
108 s = getenv("COLUMNS");
109 if (s != NULL) {
110 ws->ws_col = atoi(s);
111 if (ws->ws_row && ws->ws_col)
112 return;
113 }
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200114 }
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200115#ifdef TIOCGWINSZ
116 if (ioctl(1, TIOCGWINSZ, ws) == 0 &&
117 ws->ws_row && ws->ws_col)
118 return;
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200119#endif
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200120 ws->ws_row = 25;
121 ws->ws_col = 80;
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200122}
123
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200124static void update_print_entries(struct winsize *ws)
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200125{
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200126 top.print_entries = ws->ws_row;
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200127
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200128 if (top.print_entries > 9)
129 top.print_entries -= 9;
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200130}
131
132static void sig_winch_handler(int sig __used)
133{
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200134 get_term_dimensions(&winsize);
135 update_print_entries(&winsize);
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200136}
137
Arnaldo Carvalho de Melob0a9ab62010-03-15 11:46:58 -0300138static int parse_source(struct sym_entry *syme)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200139{
140 struct symbol *sym;
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200141 struct annotation *notes;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300142 struct map *map;
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200143 int err = -1;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200144
145 if (!syme)
Arnaldo Carvalho de Melob0a9ab62010-03-15 11:46:58 -0300146 return -1;
147
148 sym = sym_entry__symbol(syme);
149 map = syme->map;
150
151 /*
152 * We can't annotate with just /proc/kallsyms
153 */
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200154 if (map->dso->origin == DSO__ORIG_KERNEL) {
155 pr_err("Can't annotate %s: No vmlinux file was found in the "
156 "path\n", sym->name);
157 sleep(1);
Arnaldo Carvalho de Melob0a9ab62010-03-15 11:46:58 -0300158 return -1;
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200159 }
160
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200161 notes = symbol__annotation(sym);
162 if (notes->src != NULL) {
163 pthread_mutex_lock(&notes->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200164 goto out_assign;
165 }
Mike Galbraith923c42c2009-07-22 20:36:03 +0200166
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200167 pthread_mutex_lock(&notes->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200168
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200169 if (symbol__alloc_hist(sym, top.evlist->nr_entries) < 0) {
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300170 pthread_mutex_unlock(&notes->lock);
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200171 pr_err("Not enough memory for annotating '%s' symbol!\n",
172 sym->name);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200173 sleep(1);
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300174 return err;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200175 }
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200176
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200177 err = symbol__annotate(sym, syme->map, 0);
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200178 if (err == 0) {
Mike Galbraith923c42c2009-07-22 20:36:03 +0200179out_assign:
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300180 top.sym_filter_entry = syme;
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200181 }
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300182
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200183 pthread_mutex_unlock(&notes->lock);
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200184 return err;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200185}
186
187static void __zero_source_counters(struct sym_entry *syme)
188{
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200189 struct symbol *sym = sym_entry__symbol(syme);
190 symbol__annotate_zero_histograms(sym);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200191}
192
193static void record_precise_ip(struct sym_entry *syme, int counter, u64 ip)
194{
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200195 struct annotation *notes;
196 struct symbol *sym;
197
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300198 if (syme != top.sym_filter_entry)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200199 return;
200
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200201 sym = sym_entry__symbol(syme);
202 notes = symbol__annotation(sym);
203
204 if (pthread_mutex_trylock(&notes->lock))
Mike Galbraith923c42c2009-07-22 20:36:03 +0200205 return;
206
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200207 ip = syme->map->map_ip(syme->map, ip);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200208 symbol__inc_addr_samples(sym, syme->map, counter, ip);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200209
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200210 pthread_mutex_unlock(&notes->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200211}
212
Mike Galbraith923c42c2009-07-22 20:36:03 +0200213static void show_details(struct sym_entry *syme)
214{
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200215 struct annotation *notes;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200216 struct symbol *symbol;
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200217 int more;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200218
219 if (!syme)
220 return;
221
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200222 symbol = sym_entry__symbol(syme);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200223 notes = symbol__annotation(symbol);
224
225 pthread_mutex_lock(&notes->lock);
226
227 if (notes->src == NULL)
228 goto out_unlock;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200229
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200230 printf("Showing %s for %s\n", event_name(top.sym_evsel), symbol->name);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200231 printf(" Events Pcnt (>=%d%%)\n", sym_pcnt_filter);
232
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200233 more = symbol__annotate_printf(symbol, syme->map, top.sym_evsel->idx,
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -0200234 0, sym_pcnt_filter, top.print_entries, 4);
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200235 if (top.zero)
236 symbol__annotate_zero_histogram(symbol, top.sym_evsel->idx);
237 else
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200238 symbol__annotate_decay_histogram(symbol, top.sym_evsel->idx);
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200239 if (more != 0)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200240 printf("%d lines not displayed, maybe increase display entries [e]\n", more);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200241out_unlock:
242 pthread_mutex_unlock(&notes->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200243}
Ingo Molnar07800602009-04-20 15:00:56 +0200244
Ingo Molnar07800602009-04-20 15:00:56 +0200245static const char CONSOLE_CLEAR[] = "";
246
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300247static void __list_insert_active_sym(struct sym_entry *syme)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300248{
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200249 list_add(&syme->node, &top.active_symbols);
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300250}
Ingo Molnar07800602009-04-20 15:00:56 +0200251
Arnaldo Carvalho de Melo93fc64f2011-01-29 12:08:00 -0200252static void print_sym_table(struct perf_session *session)
Ingo Molnar07800602009-04-20 15:00:56 +0200253{
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200254 char bf[160];
255 int printed = 0;
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300256 struct rb_node *nd;
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200257 struct sym_entry *syme;
258 struct rb_root tmp = RB_ROOT;
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200259 const int win_width = winsize.ws_col - 1;
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200260 int sym_width, dso_width, dso_short_width;
261 float sum_ksamples = perf_top__decay_samples(&top, &tmp);
Mike Galbraithd94b9432009-05-25 09:57:56 +0200262
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200263 puts(CONSOLE_CLEAR);
Ingo Molnar07800602009-04-20 15:00:56 +0200264
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200265 perf_top__header_snprintf(&top, bf, sizeof(bf));
266 printf("%s\n", bf);
Ingo Molnar07800602009-04-20 15:00:56 +0200267
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200268 perf_top__reset_sample_counters(&top);
Ingo Molnar07800602009-04-20 15:00:56 +0200269
Arnaldo Carvalho de Melo1a105f72009-11-17 15:40:55 -0200270 printf("%-*.*s\n", win_width, win_width, graph_dotted_line);
Ingo Molnar07800602009-04-20 15:00:56 +0200271
Arnaldo Carvalho de Melo93fc64f2011-01-29 12:08:00 -0200272 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
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300278 if (top.sym_filter_entry) {
279 show_details(top.sym_filter_entry);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200280 return;
281 }
282
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200283 perf_top__find_widths(&top, &tmp, &dso_width, &dso_short_width,
284 &sym_width);
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200285
Arnaldo Carvalho de Melob63be8d2010-03-15 15:03:50 -0300286 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 }
Arnaldo Carvalho de Melo7cc017e2009-11-24 12:05:14 -0200291 putchar('\n');
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200292 if (top.evlist->nr_entries == 1)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200293 printf(" samples pcnt");
Ingo Molnar07800602009-04-20 15:00:56 +0200294 else
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200295 printf(" weight samples pcnt");
Ingo Molnar07800602009-04-20 15:00:56 +0200296
Arnaldo Carvalho de Melo7ced1562009-08-26 11:51:26 -0300297 if (verbose)
298 printf(" RIP ");
Arnaldo Carvalho de Melo7cc017e2009-11-24 12:05:14 -0200299 printf(" %-*.*s DSO\n", sym_width, sym_width, "function");
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200300 printf(" %s _______ _____",
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200301 top.evlist->nr_entries == 1 ? " " : "______");
Arnaldo Carvalho de Melo7ced1562009-08-26 11:51:26 -0300302 if (verbose)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200303 printf(" ________________");
Arnaldo Carvalho de Melo1a105f72009-11-17 15:40:55 -0200304 printf(" %-*.*s", sym_width, sym_width, graph_line);
Arnaldo Carvalho de Melo7cc017e2009-11-24 12:05:14 -0200305 printf(" %-*.*s", dso_width, dso_width, graph_line);
Arnaldo Carvalho de Melo1a105f72009-11-17 15:40:55 -0200306 puts("\n");
Ingo Molnar07800602009-04-20 15:00:56 +0200307
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300308 for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
Ingo Molnar83a09442009-08-15 12:26:57 +0200309 struct symbol *sym;
Ingo Molnar8fc03212009-06-04 15:19:47 +0200310 double pcnt;
Ingo Molnar07800602009-04-20 15:00:56 +0200311
Ingo Molnar83a09442009-08-15 12:26:57 +0200312 syme = rb_entry(nd, struct sym_entry, rb_node);
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200313 sym = sym_entry__symbol(syme);
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200314 if (++printed > top.print_entries ||
315 (int)syme->snap_count < top.count_filter)
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300316 continue;
Ingo Molnar07800602009-04-20 15:00:56 +0200317
Ingo Molnar2debbc82009-06-05 14:29:10 +0200318 pcnt = 100.0 - (100.0 * ((sum_ksamples - syme->snap_count) /
319 sum_ksamples));
Mike Galbraithd94b9432009-05-25 09:57:56 +0200320
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200321 if (top.evlist->nr_entries == 1 || !top.display_weighted)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200322 printf("%20.2f ", syme->weight);
Mike Galbraithd94b9432009-05-25 09:57:56 +0200323 else
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200324 printf("%9.1f %10ld ", syme->weight, syme->snap_count);
Ingo Molnar8fc03212009-06-04 15:19:47 +0200325
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200326 percent_color_fprintf(stdout, "%4.1f%%", pcnt);
Arnaldo Carvalho de Melo7ced1562009-08-26 11:51:26 -0300327 if (verbose)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200328 printf(" %016" PRIx64, sym->start);
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200329 printf(" %-*.*s", sym_width, sym_width, sym->name);
Arnaldo Carvalho de Melo7cc017e2009-11-24 12:05:14 -0200330 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);
Ingo Molnar07800602009-04-20 15:00:56 +0200334 }
Ingo Molnar07800602009-04-20 15:00:56 +0200335}
336
Mike Galbraith923c42c2009-07-22 20:36:03 +0200337static 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) {
Mike Galbraith923c42c2009-07-22 20:36:03 +0200380 __zero_source_counters(syme);
381 *target = NULL;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200382 }
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
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200392 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);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200395
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200396 list_for_each_entry_safe_from(syme, n, &top.active_symbols, node) {
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200397 struct symbol *sym = sym_entry__symbol(syme);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200398
399 if (!strcmp(buf, sym->name)) {
400 found = syme;
401 break;
402 }
403 }
404
405 if (!found) {
Kirill Smelkov66aeb6d2010-01-13 13:22:19 -0200406 fprintf(stderr, "Sorry, %s is not active.\n", buf);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200407 sleep(1);
408 return;
409 } else
410 parse_source(found);
411
412out_free:
413 free(buf);
414}
415
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200416static void print_mapped_keys(void)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200417{
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200418 char *name = NULL;
419
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300420 if (top.sym_filter_entry) {
421 struct symbol *sym = sym_entry__symbol(top.sym_filter_entry);
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200422 name = sym->name;
423 }
424
425 fprintf(stdout, "\nMapped keys:\n");
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200426 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);
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200428
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200429 if (top.evlist->nr_entries > 1)
430 fprintf(stdout, "\t[E] active event counter. \t(%s)\n", event_name(top.sym_evsel));
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200431
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200432 fprintf(stdout, "\t[f] profile display filter (count). \t(%d)\n", top.count_filter);
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200433
Kirill Smelkov6cff0e82010-02-03 16:52:08 -0200434 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");
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200437
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200438 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);
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200440
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200441 fprintf(stdout,
Kirill Smelkov1a72cfa2010-02-12 19:20:59 +0300442 "\t[K] hide kernel_symbols symbols. \t(%s)\n",
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200443 top.hide_kernel_symbols ? "yes" : "no");
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200444 fprintf(stdout,
445 "\t[U] hide user symbols. \t(%s)\n",
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200446 top.hide_user_symbols ? "yes" : "no");
447 fprintf(stdout, "\t[z] toggle sample zeroing. \t(%d)\n", top.zero ? 1 : 0);
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200448 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':
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200460 case 'K':
461 case 'U':
Kirill Smelkov6cff0e82010-02-03 16:52:08 -0200462 case 'F':
463 case 's':
464 case 'S':
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200465 return 1;
466 case 'E':
467 case 'w':
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200468 return top.evlist->nr_entries > 1 ? 1 : 0;
Ingo Molnar83a09442009-08-15 12:26:57 +0200469 default:
470 break;
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200471 }
472
473 return 0;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200474}
475
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800476static void handle_keypress(struct perf_session *session, int c)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200477{
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200478 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
Mike Galbraith923c42c2009-07-22 20:36:03 +0200501 switch (c) {
502 case 'd':
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200503 prompt_integer(&top.delay_secs, "Enter display delay");
504 if (top.delay_secs < 1)
505 top.delay_secs = 1;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200506 break;
507 case 'e':
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200508 prompt_integer(&top.print_entries, "Enter display entries (lines)");
509 if (top.print_entries == 0) {
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200510 sig_winch_handler(SIGWINCH);
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200511 signal(SIGWINCH, sig_winch_handler);
512 } else
513 signal(SIGWINCH, SIG_DFL);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200514 break;
515 case 'E':
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200516 if (top.evlist->nr_entries > 1) {
Mike Galbraith923c42c2009-07-22 20:36:03 +0200517 fprintf(stderr, "\nAvailable events:");
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200518
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200519 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));
Mike Galbraith923c42c2009-07-22 20:36:03 +0200521
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200522 prompt_integer(&top.sym_counter, "Enter details event counter");
Mike Galbraith923c42c2009-07-22 20:36:03 +0200523
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200524 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));
Mike Galbraith923c42c2009-07-22 20:36:03 +0200528 sleep(1);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200529 break;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200530 }
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200531 list_for_each_entry(top.sym_evsel, &top.evlist->entries, node)
532 if (top.sym_evsel->idx == top.sym_counter)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200533 break;
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200534 } else top.sym_counter = 0;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200535 break;
536 case 'f':
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200537 prompt_integer(&top.count_filter, "Enter display event count filter");
Mike Galbraith923c42c2009-07-22 20:36:03 +0200538 break;
539 case 'F':
540 prompt_percent(&sym_pcnt_filter, "Enter details display event filter (percent)");
541 break;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200542 case 'K':
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200543 top.hide_kernel_symbols = !top.hide_kernel_symbols;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200544 break;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200545 case 'q':
546 case 'Q':
547 printf("exiting.\n");
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200548 if (dump_symtab)
Arnaldo Carvalho de Melocbf69682010-04-27 21:22:44 -0300549 perf_session__fprintf_dsos(session, stderr);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200550 exit(0);
551 case 's':
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300552 prompt_symbol(&top.sym_filter_entry, "Enter details symbol");
Mike Galbraith923c42c2009-07-22 20:36:03 +0200553 break;
554 case 'S':
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300555 if (!top.sym_filter_entry)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200556 break;
557 else {
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300558 struct sym_entry *syme = top.sym_filter_entry;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200559
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300560 top.sym_filter_entry = NULL;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200561 __zero_source_counters(syme);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200562 }
563 break;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200564 case 'U':
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200565 top.hide_user_symbols = !top.hide_user_symbols;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200566 break;
Mike Galbraith46ab9762009-07-24 10:09:50 +0200567 case 'w':
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200568 top.display_weighted = ~top.display_weighted;
Mike Galbraith46ab9762009-07-24 10:09:50 +0200569 break;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200570 case 'z':
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200571 top.zero = !top.zero;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200572 break;
Ingo Molnar83a09442009-08-15 12:26:57 +0200573 default:
574 break;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200575 }
576}
577
Arnaldo Carvalho de Meloc0443df2011-01-31 18:19:33 -0200578static 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
Ingo Molnarf37a2912009-07-01 12:37:06 +0200586static void *display_thread(void *arg __used)
Ingo Molnar07800602009-04-20 15:00:56 +0200587{
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200588 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
Mike Galbraith923c42c2009-07-22 20:36:03 +0200589 struct termios tc, save;
590 int delay_msecs, c;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800591 struct perf_session *session = (struct perf_session *) arg;
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200592
Mike Galbraith923c42c2009-07-22 20:36:03 +0200593 tcgetattr(0, &save);
594 tc = save;
595 tc.c_lflag &= ~(ICANON | ECHO);
596 tc.c_cc[VMIN] = 0;
597 tc.c_cc[VTIME] = 0;
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200598
Mike Galbraith923c42c2009-07-22 20:36:03 +0200599repeat:
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200600 delay_msecs = top.delay_secs * 1000;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200601 tcsetattr(0, TCSANOW, &tc);
602 /* trash return*/
603 getc(stdin);
Ingo Molnar07800602009-04-20 15:00:56 +0200604
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200605 do {
Arnaldo Carvalho de Melo93fc64f2011-01-29 12:08:00 -0200606 print_sym_table(session);
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200607 } while (!poll(&stdin_poll, 1, delay_msecs) == 1);
608
Mike Galbraith923c42c2009-07-22 20:36:03 +0200609 c = getc(stdin);
610 tcsetattr(0, TCSAFLUSH, &save);
611
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800612 handle_keypress(session, c);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200613 goto repeat;
Ingo Molnar07800602009-04-20 15:00:56 +0200614
615 return NULL;
616}
617
Anton Blanchard2ab52082009-07-01 09:00:46 +1000618/* Tag samples to be skipped. */
Ingo Molnarf37a2912009-07-01 12:37:06 +0200619static const char *skip_symbols[] = {
Anton Blanchard2ab52082009-07-01 09:00:46 +1000620 "default_idle",
Arnaldo Carvalho de Melob0e85722011-01-16 17:39:15 -0200621 "native_safe_halt",
Anton Blanchard2ab52082009-07-01 09:00:46 +1000622 "cpu_idle",
623 "enter_idle",
624 "exit_idle",
625 "mwait_idle",
Arnaldo Carvalho de Melo59b90052009-07-26 19:06:19 -0300626 "mwait_idle_with_hints",
Arnaldo Carvalho de Melo83572752009-09-25 15:02:39 -0700627 "poll_idle",
Anton Blanchard3a3393e2009-07-01 09:00:47 +1000628 "ppc64_runlatch_off",
629 "pseries_dedicated_idle_sleep",
Anton Blanchard2ab52082009-07-01 09:00:46 +1000630 NULL
631};
632
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300633static int symbol_filter(struct map *map, struct symbol *sym)
Ingo Molnar07800602009-04-20 15:00:56 +0200634{
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300635 struct sym_entry *syme;
636 const char *name = sym->name;
Anton Blanchard2ab52082009-07-01 09:00:46 +1000637 int i;
Ingo Molnar07800602009-04-20 15:00:56 +0200638
Anton Blanchard3a3393e2009-07-01 09:00:47 +1000639 /*
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
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300646 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"))
Ingo Molnar07800602009-04-20 15:00:56 +0200653 return 1;
654
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200655 syme = symbol__priv(sym);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300656 syme->map = map;
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200657 symbol__annotate_init(map, sym);
Kirill Smelkov6cff0e82010-02-03 16:52:08 -0200658
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300659 if (!top.sym_filter_entry && sym_filter && !strcmp(name, sym_filter)) {
Kirill Smelkov6cff0e82010-02-03 16:52:08 -0200660 /* schedule initial sym_filter_entry setup */
661 sym_filter_entry_sched = syme;
662 sym_filter = NULL;
663 }
Mike Galbraith923c42c2009-07-22 20:36:03 +0200664
Anton Blanchard2ab52082009-07-01 09:00:46 +1000665 for (i = 0; skip_symbols[i]; i++) {
666 if (!strcmp(skip_symbols[i], name)) {
667 syme->skip = 1;
668 break;
669 }
670 }
Ingo Molnar07800602009-04-20 15:00:56 +0200671
Ingo Molnar07800602009-04-20 15:00:56 +0200672 return 0;
673}
674
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200675static void perf_event__process_sample(const union perf_event *event,
676 struct perf_sample *sample,
677 struct perf_session *session)
Ingo Molnar07800602009-04-20 15:00:56 +0200678{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200679 u64 ip = event->ip.ip;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200680 struct sym_entry *syme;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200681 struct addr_location al;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300682 struct machine *machine;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200683 u8 origin = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Ingo Molnar07800602009-04-20 15:00:56 +0200684
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200685 ++top.samples;
Arnaldo Carvalho de Melo24bfef02010-01-21 13:04:43 -0200686
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200687 switch (origin) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200688 case PERF_RECORD_MISC_USER:
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200689 ++top.us_samples;
690 if (top.hide_user_symbols)
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200691 return;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300692 machine = perf_session__find_host_machine(session);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200693 break;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200694 case PERF_RECORD_MISC_KERNEL:
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200695 ++top.kernel_samples;
696 if (top.hide_kernel_symbols)
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200697 return;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300698 machine = perf_session__find_host_machine(session);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200699 break;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800700 case PERF_RECORD_MISC_GUEST_KERNEL:
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200701 ++top.guest_kernel_samples;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200702 machine = perf_session__find_machine(session, event->ip.pid);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800703 break;
704 case PERF_RECORD_MISC_GUEST_USER:
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200705 ++top.guest_us_samples;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800706 /*
707 * TODO: we don't process guest user from host side
708 * except simple counting.
709 */
710 return;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200711 default:
Ingo Molnar07800602009-04-20 15:00:56 +0200712 return;
713 }
714
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300715 if (!machine && perf_guest) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800716 pr_err("Can't find guest [%d]'s kernel information\n",
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200717 event->ip.pid);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800718 return;
719 }
720
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200721 if (event->header.misc & PERF_RECORD_MISC_EXACT_IP)
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200722 top.exact_samples++;
Peter Zijlstra1676b8a2010-03-04 14:19:36 +0100723
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200724 if (perf_event__preprocess_sample(event, session, &al, sample,
725 symbol_filter) < 0 ||
Arnaldo Carvalho de Melo72b8fa12010-01-27 21:05:49 -0200726 al.filtered)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200727 return;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200728
Arnaldo Carvalho de Melo72b8fa12010-01-27 21:05:49 -0200729 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 */
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300741 if (al.map == machine->vmlinux_maps[MAP__FUNCTION] &&
Arnaldo Carvalho de Melo72b8fa12010-01-27 21:05:49 -0200742 RB_EMPTY_ROOT(&al.map->dso->symbols[MAP__FUNCTION])) {
Arnaldo Carvalho de Melo374cfe52011-03-01 10:27:27 -0300743 ui__warning("The %s file can't be used\n",
744 symbol_conf.vmlinux_name);
745 exit_browser(0);
Arnaldo Carvalho de Melo72b8fa12010-01-27 21:05:49 -0200746 exit(1);
747 }
748
749 return;
750 }
751
Kirill Smelkov6cff0e82010-02-03 16:52:08 -0200752 /* let's see, whether we need to install initial sym_filter_entry */
753 if (sym_filter_entry_sched) {
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300754 top.sym_filter_entry = sym_filter_entry_sched;
Kirill Smelkov6cff0e82010-02-03 16:52:08 -0200755 sym_filter_entry_sched = NULL;
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300756 if (parse_source(top.sym_filter_entry) < 0) {
757 struct symbol *sym = sym_entry__symbol(top.sym_filter_entry);
Arnaldo Carvalho de Melob0a9ab62010-03-15 11:46:58 -0300758
759 pr_err("Can't annotate %s", sym->name);
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300760 if (top.sym_filter_entry->map->dso->origin == DSO__ORIG_KERNEL) {
Arnaldo Carvalho de Melob0a9ab62010-03-15 11:46:58 -0300761 pr_err(": No vmlinux file was found in the path:\n");
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -0300762 machine__fprintf_vmlinux_path(machine, stderr);
Arnaldo Carvalho de Melob0a9ab62010-03-15 11:46:58 -0300763 } else
764 pr_err(".\n");
765 exit(1);
766 }
Kirill Smelkov6cff0e82010-02-03 16:52:08 -0200767 }
768
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200769 syme = symbol__priv(al.sym);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200770 if (!syme->skip) {
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200771 struct perf_evsel *evsel;
772
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200773 syme->origin = origin;
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200774 evsel = perf_evlist__id2evsel(top.evlist, sample->id);
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200775 assert(evsel != NULL);
776 syme->count[evsel->idx]++;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200777 record_precise_ip(syme, evsel->idx, ip);
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200778 pthread_mutex_lock(&top.active_symbols_lock);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200779 if (list_empty(&syme->node) || !syme->node.next)
780 __list_insert_active_sym(syme);
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200781 pthread_mutex_unlock(&top.active_symbols_lock);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200782 }
783}
784
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200785static void perf_session__mmap_read_cpu(struct perf_session *self, int cpu)
Ingo Molnar07800602009-04-20 15:00:56 +0200786{
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200787 struct perf_sample sample;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200788 union perf_event *event;
Ingo Molnar07800602009-04-20 15:00:56 +0200789
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200790 while ((event = perf_evlist__read_on_cpu(top.evlist, cpu)) != NULL) {
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200791 perf_session__parse_sample(self, event, &sample);
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200792
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200793 if (event->header.type == PERF_RECORD_SAMPLE)
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200794 perf_event__process_sample(event, &sample, self);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200795 else
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200796 perf_event__process(event, &sample, self);
Ingo Molnar07800602009-04-20 15:00:56 +0200797 }
Ingo Molnar07800602009-04-20 15:00:56 +0200798}
799
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200800static void perf_session__mmap_read(struct perf_session *self)
Frederic Weisbecker2f011902009-06-06 23:10:43 +0200801{
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200802 int i;
Frederic Weisbecker2f011902009-06-06 23:10:43 +0200803
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200804 for (i = 0; i < top.evlist->cpus->nr; i++)
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200805 perf_session__mmap_read_cpu(self, i);
Frederic Weisbecker2f011902009-06-06 23:10:43 +0200806}
807
Arnaldo Carvalho de Melo72cb7012011-01-12 10:52:47 -0200808static void start_counters(struct perf_evlist *evlist)
809{
810 struct perf_evsel *counter;
Arnaldo Carvalho de Melo72cb7012011-01-12 10:52:47 -0200811
812 list_for_each_entry(counter, &evlist->entries, node) {
813 struct perf_event_attr *attr = &counter->attr;
814
815 attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
816
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200817 if (top.freq) {
Arnaldo Carvalho de Melo72cb7012011-01-12 10:52:47 -0200818 attr->sample_type |= PERF_SAMPLE_PERIOD;
819 attr->freq = 1;
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200820 attr->sample_freq = top.freq;
Arnaldo Carvalho de Melo72cb7012011-01-12 10:52:47 -0200821 }
822
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200823 if (evlist->nr_entries > 1) {
824 attr->sample_type |= PERF_SAMPLE_ID;
825 attr->read_format |= PERF_FORMAT_ID;
826 }
827
Arnaldo Carvalho de Melo72cb7012011-01-12 10:52:47 -0200828 attr->mmap = 1;
829try_again:
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200830 if (perf_evsel__open(counter, top.evlist->cpus,
831 top.evlist->threads, group, inherit) < 0) {
Arnaldo Carvalho de Melo72cb7012011-01-12 10:52:47 -0200832 int err = errno;
833
834 if (err == EPERM || err == EACCES)
835 die("Permission error - are you root?\n"
836 "\t Consider tweaking"
837 " /proc/sys/kernel/perf_event_paranoid.\n");
838 /*
839 * If it's cycles then fall back to hrtimer
840 * based cpu-clock-tick sw counter, which
841 * is always available even if no PMU support:
842 */
843 if (attr->type == PERF_TYPE_HARDWARE &&
844 attr->config == PERF_COUNT_HW_CPU_CYCLES) {
845
846 if (verbose)
847 warning(" ... trying to fall back to cpu-clock-ticks\n");
848
849 attr->type = PERF_TYPE_SOFTWARE;
850 attr->config = PERF_COUNT_SW_CPU_CLOCK;
851 goto try_again;
852 }
853 printf("\n");
854 error("sys_perf_event_open() syscall returned with %d "
855 "(%s). /bin/dmesg may provide additional information.\n",
856 err, strerror(err));
857 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
858 exit(-1);
859 }
Arnaldo Carvalho de Melo72cb7012011-01-12 10:52:47 -0200860 }
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200861
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200862 if (perf_evlist__mmap(evlist, mmap_pages, false) < 0)
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200863 die("failed to mmap with %d (%s)\n", errno, strerror(errno));
Arnaldo Carvalho de Melo72cb7012011-01-12 10:52:47 -0200864}
865
Ingo Molnar716c69f2009-06-07 17:31:52 +0200866static int __cmd_top(void)
867{
868 pthread_t thread;
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200869 struct perf_evsel *first;
Kyle McMartinfb7d0b32011-01-24 11:13:04 -0500870 int ret __used;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200871 /*
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200872 * FIXME: perf_session__new should allow passing a O_MMAP, so that all this
873 * mmap reading, etc is encapsulated in it. Use O_WRONLY for now.
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200874 */
Ian Munsie21ef97f2010-12-10 14:09:16 +1100875 struct perf_session *session = perf_session__new(NULL, O_WRONLY, false, false, NULL);
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200876 if (session == NULL)
877 return -ENOMEM;
Ingo Molnar07800602009-04-20 15:00:56 +0200878
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200879 if (top.target_tid != -1)
Arnaldo Carvalho de Melo7c940c12011-02-11 11:45:54 -0200880 perf_event__synthesize_thread_map(top.evlist->threads,
881 perf_event__process, session);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200882 else
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200883 perf_event__synthesize_threads(perf_event__process, session);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200884
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200885 start_counters(top.evlist);
886 first = list_entry(top.evlist->entries.next, struct perf_evsel, node);
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200887 perf_session__set_sample_type(session, first->attr.sample_type);
Ingo Molnar07800602009-04-20 15:00:56 +0200888
Frederic Weisbecker2f011902009-06-06 23:10:43 +0200889 /* Wait for a minimal set of events before starting the snapshot */
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200890 poll(top.evlist->pollfd, top.evlist->nr_fds, 100);
Frederic Weisbecker2f011902009-06-06 23:10:43 +0200891
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200892 perf_session__mmap_read(session);
Frederic Weisbecker2f011902009-06-06 23:10:43 +0200893
Arnaldo Carvalho de Meloc0443df2011-01-31 18:19:33 -0200894 if (pthread_create(&thread, NULL, (use_browser > 0 ? display_thread_tui :
895 display_thread), session)) {
Ingo Molnar07800602009-04-20 15:00:56 +0200896 printf("Could not create display thread.\n");
897 exit(-1);
898 }
899
900 if (realtime_prio) {
901 struct sched_param param;
902
903 param.sched_priority = realtime_prio;
904 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
905 printf("Could not set realtime priority.\n");
906 exit(-1);
907 }
908 }
909
910 while (1) {
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200911 u64 hits = top.samples;
Ingo Molnar07800602009-04-20 15:00:56 +0200912
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200913 perf_session__mmap_read(session);
Ingo Molnar07800602009-04-20 15:00:56 +0200914
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200915 if (hits == top.samples)
916 ret = poll(top.evlist->pollfd, top.evlist->nr_fds, 100);
Ingo Molnar07800602009-04-20 15:00:56 +0200917 }
918
919 return 0;
920}
Ingo Molnarb456bae2009-05-26 09:17:18 +0200921
922static const char * const top_usage[] = {
923 "perf top [<options>]",
924 NULL
925};
926
Ingo Molnarb456bae2009-05-26 09:17:18 +0200927static const struct option options[] = {
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200928 OPT_CALLBACK('e', "event", &top.evlist, "event",
Thomas Gleixner86847b62009-06-06 12:24:17 +0200929 "event selector. use 'perf list' to list available events",
930 parse_events),
Ingo Molnarb456bae2009-05-26 09:17:18 +0200931 OPT_INTEGER('c', "count", &default_interval,
932 "event period to sample"),
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200933 OPT_INTEGER('p', "pid", &top.target_pid,
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300934 "profile events on existing process id"),
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200935 OPT_INTEGER('t', "tid", &top.target_tid,
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300936 "profile events on existing thread id"),
Ingo Molnarb456bae2009-05-26 09:17:18 +0200937 OPT_BOOLEAN('a', "all-cpus", &system_wide,
938 "system-wide collection from all CPUs"),
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200939 OPT_STRING('C', "cpu", &top.cpu_list, "cpu",
Stephane Eranianc45c6ea2010-05-28 12:00:01 +0200940 "list of cpus to monitor"),
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -0200941 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
942 "file", "vmlinux pathname"),
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200943 OPT_BOOLEAN('K', "hide_kernel_symbols", &top.hide_kernel_symbols,
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200944 "hide kernel symbols"),
Arnaldo Carvalho de Melo19679362010-05-17 15:39:16 -0300945 OPT_UINTEGER('m', "mmap-pages", &mmap_pages, "number of mmap data pages"),
Ingo Molnarb456bae2009-05-26 09:17:18 +0200946 OPT_INTEGER('r', "realtime", &realtime_prio,
947 "collect data with this RT SCHED_FIFO priority"),
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200948 OPT_INTEGER('d', "delay", &top.delay_secs,
Ingo Molnarb456bae2009-05-26 09:17:18 +0200949 "number of seconds to delay between refreshes"),
950 OPT_BOOLEAN('D', "dump-symtab", &dump_symtab,
951 "dump the symbol table used for profiling"),
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200952 OPT_INTEGER('f', "count-filter", &top.count_filter,
Ingo Molnarb456bae2009-05-26 09:17:18 +0200953 "only display functions with more events than this"),
954 OPT_BOOLEAN('g', "group", &group,
955 "put the counters into a counter group"),
Mike Galbraith0fdc7e62009-07-21 10:30:36 +0200956 OPT_BOOLEAN('i', "inherit", &inherit,
957 "child tasks inherit counters"),
Mike Galbraith923c42c2009-07-22 20:36:03 +0200958 OPT_STRING('s', "sym-annotate", &sym_filter, "symbol name",
Kirill Smelkov6cff0e82010-02-03 16:52:08 -0200959 "symbol to annotate"),
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200960 OPT_BOOLEAN('z', "zero", &top.zero,
Ingo Molnarb456bae2009-05-26 09:17:18 +0200961 "zero history across updates"),
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200962 OPT_INTEGER('F', "freq", &top.freq,
Ingo Molnarb456bae2009-05-26 09:17:18 +0200963 "profile at this frequency"),
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200964 OPT_INTEGER('E', "entries", &top.print_entries,
Ingo Molnar6e53cdf2009-06-04 08:53:05 +0200965 "display this many functions"),
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200966 OPT_BOOLEAN('U', "hide_user_symbols", &top.hide_user_symbols,
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200967 "hide user symbols"),
Arnaldo Carvalho de Meloc0443df2011-01-31 18:19:33 -0200968 OPT_BOOLEAN(0, "tui", &use_tui, "Use the TUI interface"),
969 OPT_BOOLEAN(0, "stdio", &use_stdio, "Use the stdio interface"),
Ian Munsiec0555642010-04-13 18:37:33 +1000970 OPT_INCR('v', "verbose", &verbose,
Ingo Molnar3da297a2009-06-07 17:39:02 +0200971 "be more verbose (show counter open errors, etc)"),
Ingo Molnarb456bae2009-05-26 09:17:18 +0200972 OPT_END()
973};
974
Ingo Molnarf37a2912009-07-01 12:37:06 +0200975int cmd_top(int argc, const char **argv, const char *prefix __used)
Ingo Molnarb456bae2009-05-26 09:17:18 +0200976{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200977 struct perf_evsel *pos;
978 int status = -ENOMEM;
Ingo Molnarb456bae2009-05-26 09:17:18 +0200979
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200980 top.evlist = perf_evlist__new(NULL, NULL);
981 if (top.evlist == NULL)
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200982 return -ENOMEM;
983
Ingo Molnarb456bae2009-05-26 09:17:18 +0200984 page_size = sysconf(_SC_PAGE_SIZE);
985
Ingo Molnarb456bae2009-05-26 09:17:18 +0200986 argc = parse_options(argc, argv, options, top_usage, 0);
987 if (argc)
988 usage_with_options(top_usage, options);
989
Arnaldo Carvalho de Meloc0443df2011-01-31 18:19:33 -0200990 /*
991 * XXX For now start disabled, only using TUI if explicitely asked for.
992 * Change that when handle_keys equivalent gets written, live annotation
993 * done, etc.
994 */
995 use_browser = 0;
996
997 if (use_stdio)
998 use_browser = 0;
999 else if (use_tui)
1000 use_browser = 1;
1001
1002 setup_browser(false);
1003
Ingo Molnarb456bae2009-05-26 09:17:18 +02001004 /* CPU and PID are mutually exclusive */
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001005 if (top.target_tid > 0 && top.cpu_list) {
Ingo Molnarb456bae2009-05-26 09:17:18 +02001006 printf("WARNING: PID switch overriding CPU\n");
1007 sleep(1);
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001008 top.cpu_list = NULL;
Ingo Molnarb456bae2009-05-26 09:17:18 +02001009 }
1010
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001011 if (top.target_pid != -1)
1012 top.target_tid = top.target_pid;
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -02001013
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001014 if (perf_evlist__create_maps(top.evlist, top.target_pid,
1015 top.target_tid, top.cpu_list) < 0)
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -02001016 usage_with_options(top_usage, options);
1017
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001018 if (!top.evlist->nr_entries &&
1019 perf_evlist__add_default(top.evlist) < 0) {
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001020 pr_err("Not enough memory for event selector list\n");
1021 return -ENOMEM;
1022 }
Arnaldo Carvalho de Melo5a8e5a32009-11-17 18:38:01 -02001023
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001024 if (top.delay_secs < 1)
1025 top.delay_secs = 1;
Frederic Weisbecker2f335a02009-06-05 19:31:01 +02001026
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +02001027 /*
1028 * User specified count overrides default frequency.
1029 */
1030 if (default_interval)
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001031 top.freq = 0;
1032 else if (top.freq) {
1033 default_interval = top.freq;
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +02001034 } else {
1035 fprintf(stderr, "frequency and count are zero, aborting\n");
1036 exit(EXIT_FAILURE);
1037 }
1038
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001039 list_for_each_entry(pos, &top.evlist->entries, node) {
1040 if (perf_evsel__alloc_fd(pos, top.evlist->cpus->nr,
1041 top.evlist->threads->nr) < 0)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001042 goto out_free_fd;
1043 /*
1044 * Fill in the ones not specifically initialized via -c:
1045 */
1046 if (pos->attr.sample_period)
1047 continue;
1048
1049 pos->attr.sample_period = default_interval;
1050 }
1051
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001052 if (perf_evlist__alloc_pollfd(top.evlist) < 0 ||
1053 perf_evlist__alloc_mmap(top.evlist) < 0)
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -02001054 goto out_free_fd;
1055
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001056 top.sym_evsel = list_entry(top.evlist->entries.next, struct perf_evsel, node);
Arnaldo Carvalho de Melocc841582011-01-11 15:16:52 -02001057
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -02001058 symbol_conf.priv_size = (sizeof(struct sym_entry) + sizeof(struct annotation) +
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001059 (top.evlist->nr_entries + 1) * sizeof(unsigned long));
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001060
1061 symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL);
1062 if (symbol__init() < 0)
1063 return -1;
1064
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -02001065 get_term_dimensions(&winsize);
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001066 if (top.print_entries == 0) {
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -02001067 update_print_entries(&winsize);
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -02001068 signal(SIGWINCH, sig_winch_handler);
1069 }
1070
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001071 status = __cmd_top();
1072out_free_fd:
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001073 perf_evlist__delete(top.evlist);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001074
1075 return status;
Ingo Molnarb456bae2009-05-26 09:17:18 +02001076}