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