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 | |
Ingo Molnar | 8fc0321 | 2009-06-04 15:19:47 +0200 | [diff] [blame] | 23 | #include "util/color.h" |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 24 | #include "util/session.h" |
| 25 | #include "util/symbol.h" |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 26 | #include "util/thread.h" |
Ingo Molnar | 148be2c | 2009-04-27 08:02:14 +0200 | [diff] [blame] | 27 | #include "util/util.h" |
Arnaldo Carvalho de Melo | 43cbcd8 | 2009-07-01 12:28:37 -0300 | [diff] [blame] | 28 | #include <linux/rbtree.h> |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 29 | #include "util/parse-options.h" |
| 30 | #include "util/parse-events.h" |
Paul Mackerras | a12b51c | 2010-03-10 20:36:09 +1100 | [diff] [blame] | 31 | #include "util/cpumap.h" |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 32 | |
Frederic Weisbecker | 8f28827a | 2009-08-16 22:05:48 +0200 | [diff] [blame] | 33 | #include "util/debug.h" |
| 34 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 35 | #include <assert.h> |
| 36 | #include <fcntl.h> |
Ingo Molnar | 0e9b20b | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 37 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 38 | #include <stdio.h> |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 39 | #include <termios.h> |
| 40 | #include <unistd.h> |
Ingo Molnar | 0e9b20b | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 41 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 42 | #include <errno.h> |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 43 | #include <time.h> |
| 44 | #include <sched.h> |
| 45 | #include <pthread.h> |
| 46 | |
| 47 | #include <sys/syscall.h> |
| 48 | #include <sys/ioctl.h> |
| 49 | #include <sys/poll.h> |
| 50 | #include <sys/prctl.h> |
| 51 | #include <sys/wait.h> |
| 52 | #include <sys/uio.h> |
| 53 | #include <sys/mman.h> |
| 54 | |
| 55 | #include <linux/unistd.h> |
| 56 | #include <linux/types.h> |
| 57 | |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 58 | static int fd[MAX_NR_CPUS][MAX_COUNTERS]; |
| 59 | |
Ingo Molnar | 42e59d7 | 2009-10-06 15:14:21 +0200 | [diff] [blame] | 60 | static int system_wide = 0; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 61 | |
Mike Galbraith | 7e4ff9e | 2009-10-12 07:56:03 +0200 | [diff] [blame] | 62 | static int default_interval = 0; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 63 | |
Ingo Molnar | 42e59d7 | 2009-10-06 15:14:21 +0200 | [diff] [blame] | 64 | static int count_filter = 5; |
Arnaldo Carvalho de Melo | 3b6ed98 | 2009-11-16 19:30:27 -0200 | [diff] [blame] | 65 | static int print_entries; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 66 | |
Ingo Molnar | 42e59d7 | 2009-10-06 15:14:21 +0200 | [diff] [blame] | 67 | static int target_pid = -1; |
| 68 | static int inherit = 0; |
| 69 | static int profile_cpu = -1; |
| 70 | static int nr_cpus = 0; |
| 71 | static unsigned int realtime_prio = 0; |
| 72 | static int group = 0; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 73 | static unsigned int page_size; |
Ingo Molnar | 42e59d7 | 2009-10-06 15:14:21 +0200 | [diff] [blame] | 74 | static unsigned int mmap_pages = 16; |
| 75 | static int freq = 1000; /* 1 KHz */ |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 76 | |
Ingo Molnar | 42e59d7 | 2009-10-06 15:14:21 +0200 | [diff] [blame] | 77 | static int delay_secs = 2; |
| 78 | static int zero = 0; |
| 79 | static int dump_symtab = 0; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 80 | |
Arnaldo Carvalho de Melo | 8ffcda1 | 2009-11-16 21:45:24 -0200 | [diff] [blame] | 81 | static bool hide_kernel_symbols = false; |
| 82 | static bool hide_user_symbols = false; |
Arnaldo Carvalho de Melo | 13cc507 | 2009-11-17 15:40:54 -0200 | [diff] [blame] | 83 | static struct winsize winsize; |
Arnaldo Carvalho de Melo | 8ffcda1 | 2009-11-16 21:45:24 -0200 | [diff] [blame] | 84 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 85 | /* |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 86 | * Source |
| 87 | */ |
| 88 | |
| 89 | struct source_line { |
| 90 | u64 eip; |
| 91 | unsigned long count[MAX_COUNTERS]; |
| 92 | char *line; |
| 93 | struct source_line *next; |
| 94 | }; |
| 95 | |
Ingo Molnar | 42e59d7 | 2009-10-06 15:14:21 +0200 | [diff] [blame] | 96 | static char *sym_filter = NULL; |
| 97 | struct sym_entry *sym_filter_entry = NULL; |
Kirill Smelkov | 6cff0e8 | 2010-02-03 16:52:08 -0200 | [diff] [blame] | 98 | struct sym_entry *sym_filter_entry_sched = NULL; |
Ingo Molnar | 42e59d7 | 2009-10-06 15:14:21 +0200 | [diff] [blame] | 99 | static int sym_pcnt_filter = 5; |
| 100 | static int sym_counter = 0; |
| 101 | static int display_weighted = -1; |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 102 | |
| 103 | /* |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 104 | * Symbols |
| 105 | */ |
| 106 | |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 107 | struct sym_entry_source { |
| 108 | struct source_line *source; |
| 109 | struct source_line *lines; |
| 110 | struct source_line **lines_tail; |
| 111 | pthread_mutex_t lock; |
| 112 | }; |
| 113 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 114 | struct sym_entry { |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 115 | struct rb_node rb_node; |
| 116 | struct list_head node; |
Arnaldo Carvalho de Melo | c44613a | 2009-05-29 17:03:07 -0300 | [diff] [blame] | 117 | unsigned long snap_count; |
| 118 | double weight; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 119 | int skip; |
Arnaldo Carvalho de Melo | 13cc507 | 2009-11-17 15:40:54 -0200 | [diff] [blame] | 120 | u16 name_len; |
Arnaldo Carvalho de Melo | 8ffcda1 | 2009-11-16 21:45:24 -0200 | [diff] [blame] | 121 | u8 origin; |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 122 | struct map *map; |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 123 | struct sym_entry_source *src; |
Arnaldo Carvalho de Melo | 5a8e5a3 | 2009-11-17 18:38:01 -0200 | [diff] [blame] | 124 | unsigned long count[0]; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 125 | }; |
| 126 | |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 127 | /* |
| 128 | * Source functions |
| 129 | */ |
| 130 | |
Arnaldo Carvalho de Melo | 51a472d | 2009-11-17 18:38:00 -0200 | [diff] [blame] | 131 | static inline struct symbol *sym_entry__symbol(struct sym_entry *self) |
| 132 | { |
Arnaldo Carvalho de Melo | b32d133 | 2009-11-24 12:05:15 -0200 | [diff] [blame] | 133 | return ((void *)self) + symbol_conf.priv_size; |
Arnaldo Carvalho de Melo | 51a472d | 2009-11-17 18:38:00 -0200 | [diff] [blame] | 134 | } |
| 135 | |
Arnaldo Carvalho de Melo | 895f0ed | 2010-03-11 20:12:41 -0300 | [diff] [blame] | 136 | void get_term_dimensions(struct winsize *ws) |
Arnaldo Carvalho de Melo | 3b6ed98 | 2009-11-16 19:30:27 -0200 | [diff] [blame] | 137 | { |
Arnaldo Carvalho de Melo | 13cc507 | 2009-11-17 15:40:54 -0200 | [diff] [blame] | 138 | char *s = getenv("LINES"); |
Arnaldo Carvalho de Melo | 3b6ed98 | 2009-11-16 19:30:27 -0200 | [diff] [blame] | 139 | |
Arnaldo Carvalho de Melo | 13cc507 | 2009-11-17 15:40:54 -0200 | [diff] [blame] | 140 | if (s != NULL) { |
| 141 | ws->ws_row = atoi(s); |
| 142 | s = getenv("COLUMNS"); |
| 143 | if (s != NULL) { |
| 144 | ws->ws_col = atoi(s); |
| 145 | if (ws->ws_row && ws->ws_col) |
| 146 | return; |
| 147 | } |
Arnaldo Carvalho de Melo | 3b6ed98 | 2009-11-16 19:30:27 -0200 | [diff] [blame] | 148 | } |
Arnaldo Carvalho de Melo | 13cc507 | 2009-11-17 15:40:54 -0200 | [diff] [blame] | 149 | #ifdef TIOCGWINSZ |
| 150 | if (ioctl(1, TIOCGWINSZ, ws) == 0 && |
| 151 | ws->ws_row && ws->ws_col) |
| 152 | return; |
Arnaldo Carvalho de Melo | 3b6ed98 | 2009-11-16 19:30:27 -0200 | [diff] [blame] | 153 | #endif |
Arnaldo Carvalho de Melo | 13cc507 | 2009-11-17 15:40:54 -0200 | [diff] [blame] | 154 | ws->ws_row = 25; |
| 155 | ws->ws_col = 80; |
Arnaldo Carvalho de Melo | 3b6ed98 | 2009-11-16 19:30:27 -0200 | [diff] [blame] | 156 | } |
| 157 | |
Arnaldo Carvalho de Melo | 13cc507 | 2009-11-17 15:40:54 -0200 | [diff] [blame] | 158 | static void update_print_entries(struct winsize *ws) |
Arnaldo Carvalho de Melo | 3b6ed98 | 2009-11-16 19:30:27 -0200 | [diff] [blame] | 159 | { |
Arnaldo Carvalho de Melo | 13cc507 | 2009-11-17 15:40:54 -0200 | [diff] [blame] | 160 | print_entries = ws->ws_row; |
| 161 | |
Arnaldo Carvalho de Melo | 3b6ed98 | 2009-11-16 19:30:27 -0200 | [diff] [blame] | 162 | if (print_entries > 9) |
| 163 | print_entries -= 9; |
| 164 | } |
| 165 | |
| 166 | static void sig_winch_handler(int sig __used) |
| 167 | { |
Arnaldo Carvalho de Melo | 13cc507 | 2009-11-17 15:40:54 -0200 | [diff] [blame] | 168 | get_term_dimensions(&winsize); |
| 169 | update_print_entries(&winsize); |
Arnaldo Carvalho de Melo | 3b6ed98 | 2009-11-16 19:30:27 -0200 | [diff] [blame] | 170 | } |
| 171 | |
Arnaldo Carvalho de Melo | b0a9ab6 | 2010-03-15 11:46:58 -0300 | [diff] [blame] | 172 | static int parse_source(struct sym_entry *syme) |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 173 | { |
| 174 | struct symbol *sym; |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 175 | struct sym_entry_source *source; |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 176 | struct map *map; |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 177 | FILE *file; |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 178 | char command[PATH_MAX*2]; |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 179 | const char *path; |
| 180 | u64 len; |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 181 | |
| 182 | if (!syme) |
Arnaldo Carvalho de Melo | b0a9ab6 | 2010-03-15 11:46:58 -0300 | [diff] [blame] | 183 | return -1; |
| 184 | |
| 185 | sym = sym_entry__symbol(syme); |
| 186 | map = syme->map; |
| 187 | |
| 188 | /* |
| 189 | * We can't annotate with just /proc/kallsyms |
| 190 | */ |
| 191 | if (map->dso->origin == DSO__ORIG_KERNEL) |
| 192 | return -1; |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 193 | |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 194 | if (syme->src == NULL) { |
Arnaldo Carvalho de Melo | 3647948 | 2009-11-24 12:05:16 -0200 | [diff] [blame] | 195 | syme->src = zalloc(sizeof(*source)); |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 196 | if (syme->src == NULL) |
Arnaldo Carvalho de Melo | b0a9ab6 | 2010-03-15 11:46:58 -0300 | [diff] [blame] | 197 | return -1; |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 198 | pthread_mutex_init(&syme->src->lock, NULL); |
| 199 | } |
| 200 | |
| 201 | source = syme->src; |
| 202 | |
| 203 | if (source->lines) { |
| 204 | pthread_mutex_lock(&source->lock); |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 205 | goto out_assign; |
| 206 | } |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 207 | path = map->dso->long_name; |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 208 | |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 209 | len = sym->end - sym->start; |
| 210 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 211 | sprintf(command, |
Arnaldo Carvalho de Melo | 5f48536 | 2010-02-07 11:46:16 -0200 | [diff] [blame] | 212 | "objdump --start-address=%#0*Lx --stop-address=%#0*Lx -dS %s", |
| 213 | BITS_PER_LONG / 4, map__rip_2objdump(map, sym->start), |
| 214 | BITS_PER_LONG / 4, map__rip_2objdump(map, sym->end), path); |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 215 | |
| 216 | file = popen(command, "r"); |
| 217 | if (!file) |
Arnaldo Carvalho de Melo | b0a9ab6 | 2010-03-15 11:46:58 -0300 | [diff] [blame] | 218 | return -1; |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 219 | |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 220 | pthread_mutex_lock(&source->lock); |
| 221 | source->lines_tail = &source->lines; |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 222 | while (!feof(file)) { |
| 223 | struct source_line *src; |
| 224 | size_t dummy = 0; |
Kirill Smelkov | ee11b90 | 2010-02-07 11:46:15 -0200 | [diff] [blame] | 225 | char *c, *sep; |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 226 | |
| 227 | src = malloc(sizeof(struct source_line)); |
| 228 | assert(src != NULL); |
| 229 | memset(src, 0, sizeof(struct source_line)); |
| 230 | |
| 231 | if (getline(&src->line, &dummy, file) < 0) |
| 232 | break; |
| 233 | if (!src->line) |
| 234 | break; |
| 235 | |
| 236 | c = strchr(src->line, '\n'); |
| 237 | if (c) |
| 238 | *c = 0; |
| 239 | |
| 240 | src->next = NULL; |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 241 | *source->lines_tail = src; |
| 242 | source->lines_tail = &src->next; |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 243 | |
Kirill Smelkov | ee11b90 | 2010-02-07 11:46:15 -0200 | [diff] [blame] | 244 | src->eip = strtoull(src->line, &sep, 16); |
| 245 | if (*sep == ':') |
| 246 | src->eip = map__objdump_2ip(map, src->eip); |
| 247 | else /* this line has no ip info (e.g. source line) */ |
| 248 | src->eip = 0; |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 249 | } |
| 250 | pclose(file); |
| 251 | out_assign: |
| 252 | sym_filter_entry = syme; |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 253 | pthread_mutex_unlock(&source->lock); |
Arnaldo Carvalho de Melo | b0a9ab6 | 2010-03-15 11:46:58 -0300 | [diff] [blame] | 254 | return 0; |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | static void __zero_source_counters(struct sym_entry *syme) |
| 258 | { |
| 259 | int i; |
| 260 | struct source_line *line; |
| 261 | |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 262 | line = syme->src->lines; |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 263 | while (line) { |
| 264 | for (i = 0; i < nr_counters; i++) |
| 265 | line->count[i] = 0; |
| 266 | line = line->next; |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | static void record_precise_ip(struct sym_entry *syme, int counter, u64 ip) |
| 271 | { |
| 272 | struct source_line *line; |
| 273 | |
| 274 | if (syme != sym_filter_entry) |
| 275 | return; |
| 276 | |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 277 | if (pthread_mutex_trylock(&syme->src->lock)) |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 278 | return; |
| 279 | |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 280 | if (syme->src == NULL || syme->src->source == NULL) |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 281 | goto out_unlock; |
| 282 | |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 283 | for (line = syme->src->lines; line; line = line->next) { |
Kirill Smelkov | ee11b90 | 2010-02-07 11:46:15 -0200 | [diff] [blame] | 284 | /* skip lines without IP info */ |
| 285 | if (line->eip == 0) |
| 286 | continue; |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 287 | if (line->eip == ip) { |
| 288 | line->count[counter]++; |
| 289 | break; |
| 290 | } |
| 291 | if (line->eip > ip) |
| 292 | break; |
| 293 | } |
| 294 | out_unlock: |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 295 | pthread_mutex_unlock(&syme->src->lock); |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 296 | } |
| 297 | |
Arnaldo Carvalho de Melo | c7ad21a | 2010-02-22 16:14:22 -0300 | [diff] [blame] | 298 | #define PATTERN_LEN (BITS_PER_LONG / 4 + 2) |
| 299 | |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 300 | static void lookup_sym_source(struct sym_entry *syme) |
| 301 | { |
Arnaldo Carvalho de Melo | 51a472d | 2009-11-17 18:38:00 -0200 | [diff] [blame] | 302 | struct symbol *symbol = sym_entry__symbol(syme); |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 303 | struct source_line *line; |
Arnaldo Carvalho de Melo | c7ad21a | 2010-02-22 16:14:22 -0300 | [diff] [blame] | 304 | char pattern[PATTERN_LEN + 1]; |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 305 | |
Arnaldo Carvalho de Melo | 5f48536 | 2010-02-07 11:46:16 -0200 | [diff] [blame] | 306 | sprintf(pattern, "%0*Lx <", BITS_PER_LONG / 4, |
| 307 | map__rip_2objdump(syme->map, symbol->start)); |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 308 | |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 309 | pthread_mutex_lock(&syme->src->lock); |
| 310 | for (line = syme->src->lines; line; line = line->next) { |
Arnaldo Carvalho de Melo | c7ad21a | 2010-02-22 16:14:22 -0300 | [diff] [blame] | 311 | if (memcmp(line->line, pattern, PATTERN_LEN) == 0) { |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 312 | syme->src->source = line; |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 313 | break; |
| 314 | } |
| 315 | } |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 316 | pthread_mutex_unlock(&syme->src->lock); |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | static void show_lines(struct source_line *queue, int count, int total) |
| 320 | { |
| 321 | int i; |
| 322 | struct source_line *line; |
| 323 | |
| 324 | line = queue; |
| 325 | for (i = 0; i < count; i++) { |
| 326 | float pcnt = 100.0*(float)line->count[sym_counter]/(float)total; |
| 327 | |
| 328 | printf("%8li %4.1f%%\t%s\n", line->count[sym_counter], pcnt, line->line); |
| 329 | line = line->next; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | #define TRACE_COUNT 3 |
| 334 | |
| 335 | static void show_details(struct sym_entry *syme) |
| 336 | { |
| 337 | struct symbol *symbol; |
| 338 | struct source_line *line; |
| 339 | struct source_line *line_queue = NULL; |
| 340 | int displayed = 0; |
| 341 | int line_queue_count = 0, total = 0, more = 0; |
| 342 | |
| 343 | if (!syme) |
| 344 | return; |
| 345 | |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 346 | if (!syme->src->source) |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 347 | lookup_sym_source(syme); |
| 348 | |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 349 | if (!syme->src->source) |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 350 | return; |
| 351 | |
Arnaldo Carvalho de Melo | 51a472d | 2009-11-17 18:38:00 -0200 | [diff] [blame] | 352 | symbol = sym_entry__symbol(syme); |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 353 | printf("Showing %s for %s\n", event_name(sym_counter), symbol->name); |
| 354 | printf(" Events Pcnt (>=%d%%)\n", sym_pcnt_filter); |
| 355 | |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 356 | pthread_mutex_lock(&syme->src->lock); |
| 357 | line = syme->src->source; |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 358 | while (line) { |
| 359 | total += line->count[sym_counter]; |
| 360 | line = line->next; |
| 361 | } |
| 362 | |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 363 | line = syme->src->source; |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 364 | while (line) { |
| 365 | float pcnt = 0.0; |
| 366 | |
| 367 | if (!line_queue_count) |
| 368 | line_queue = line; |
| 369 | line_queue_count++; |
| 370 | |
| 371 | if (line->count[sym_counter]) |
| 372 | pcnt = 100.0 * line->count[sym_counter] / (float)total; |
| 373 | if (pcnt >= (float)sym_pcnt_filter) { |
| 374 | if (displayed <= print_entries) |
| 375 | show_lines(line_queue, line_queue_count, total); |
| 376 | else more++; |
| 377 | displayed += line_queue_count; |
| 378 | line_queue_count = 0; |
| 379 | line_queue = NULL; |
| 380 | } else if (line_queue_count > TRACE_COUNT) { |
| 381 | line_queue = line_queue->next; |
| 382 | line_queue_count--; |
| 383 | } |
| 384 | |
| 385 | line->count[sym_counter] = zero ? 0 : line->count[sym_counter] * 7 / 8; |
| 386 | line = line->next; |
| 387 | } |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 388 | pthread_mutex_unlock(&syme->src->lock); |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 389 | if (more) |
| 390 | printf("%d lines not displayed, maybe increase display entries [e]\n", more); |
| 391 | } |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 392 | |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 393 | /* |
Arnaldo Carvalho de Melo | 5b2bb75 | 2009-10-26 19:23:19 -0200 | [diff] [blame] | 394 | * Symbols will be added here in event__process_sample and will get out |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 395 | * after decayed. |
| 396 | */ |
| 397 | static LIST_HEAD(active_symbols); |
Arnaldo Carvalho de Melo | c44613a | 2009-05-29 17:03:07 -0300 | [diff] [blame] | 398 | static pthread_mutex_t active_symbols_lock = PTHREAD_MUTEX_INITIALIZER; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 399 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 400 | /* |
| 401 | * Ordering weight: count-1 * count-2 * ... / count-n |
| 402 | */ |
| 403 | static double sym_weight(const struct sym_entry *sym) |
| 404 | { |
Arnaldo Carvalho de Melo | c44613a | 2009-05-29 17:03:07 -0300 | [diff] [blame] | 405 | double weight = sym->snap_count; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 406 | int counter; |
| 407 | |
Mike Galbraith | 46ab976 | 2009-07-24 10:09:50 +0200 | [diff] [blame] | 408 | if (!display_weighted) |
| 409 | return weight; |
| 410 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 411 | for (counter = 1; counter < nr_counters-1; counter++) |
| 412 | weight *= sym->count[counter]; |
| 413 | |
| 414 | weight /= (sym->count[counter] + 1); |
| 415 | |
| 416 | return weight; |
| 417 | } |
| 418 | |
Ingo Molnar | 2debbc8 | 2009-06-05 14:29:10 +0200 | [diff] [blame] | 419 | static long samples; |
| 420 | static long userspace_samples; |
Peter Zijlstra | 1676b8a | 2010-03-04 14:19:36 +0100 | [diff] [blame] | 421 | static long exact_samples; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 422 | static const char CONSOLE_CLEAR[] = "[H[2J"; |
| 423 | |
Arnaldo Carvalho de Melo | c44613a | 2009-05-29 17:03:07 -0300 | [diff] [blame] | 424 | static void __list_insert_active_sym(struct sym_entry *syme) |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 425 | { |
| 426 | list_add(&syme->node, &active_symbols); |
| 427 | } |
| 428 | |
Arnaldo Carvalho de Melo | c44613a | 2009-05-29 17:03:07 -0300 | [diff] [blame] | 429 | static void list_remove_active_sym(struct sym_entry *syme) |
| 430 | { |
| 431 | pthread_mutex_lock(&active_symbols_lock); |
| 432 | list_del_init(&syme->node); |
| 433 | pthread_mutex_unlock(&active_symbols_lock); |
| 434 | } |
| 435 | |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 436 | static void rb_insert_active_sym(struct rb_root *tree, struct sym_entry *se) |
| 437 | { |
| 438 | struct rb_node **p = &tree->rb_node; |
| 439 | struct rb_node *parent = NULL; |
| 440 | struct sym_entry *iter; |
| 441 | |
| 442 | while (*p != NULL) { |
| 443 | parent = *p; |
| 444 | iter = rb_entry(parent, struct sym_entry, rb_node); |
| 445 | |
Arnaldo Carvalho de Melo | c44613a | 2009-05-29 17:03:07 -0300 | [diff] [blame] | 446 | if (se->weight > iter->weight) |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 447 | p = &(*p)->rb_left; |
| 448 | else |
| 449 | p = &(*p)->rb_right; |
| 450 | } |
| 451 | |
| 452 | rb_link_node(&se->rb_node, parent, p); |
| 453 | rb_insert_color(&se->rb_node, tree); |
| 454 | } |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 455 | |
| 456 | static void print_sym_table(void) |
| 457 | { |
Ingo Molnar | 233f0b9 | 2009-06-03 21:48:40 +0200 | [diff] [blame] | 458 | int printed = 0, j; |
Mike Galbraith | 46ab976 | 2009-07-24 10:09:50 +0200 | [diff] [blame] | 459 | int counter, snap = !display_weighted ? sym_counter : 0; |
Ingo Molnar | 2debbc8 | 2009-06-05 14:29:10 +0200 | [diff] [blame] | 460 | float samples_per_sec = samples/delay_secs; |
| 461 | float ksamples_per_sec = (samples-userspace_samples)/delay_secs; |
Peter Zijlstra | 1676b8a | 2010-03-04 14:19:36 +0100 | [diff] [blame] | 462 | float esamples_percent = (100.0*exact_samples)/samples; |
Ingo Molnar | 2debbc8 | 2009-06-05 14:29:10 +0200 | [diff] [blame] | 463 | float sum_ksamples = 0.0; |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 464 | struct sym_entry *syme, *n; |
| 465 | struct rb_root tmp = RB_ROOT; |
| 466 | struct rb_node *nd; |
Arnaldo Carvalho de Melo | 00909e9 | 2010-03-16 18:28:46 -0300 | [diff] [blame] | 467 | int sym_width = 0, dso_width = 0, dso_short_width = 0; |
Arnaldo Carvalho de Melo | 13cc507 | 2009-11-17 15:40:54 -0200 | [diff] [blame] | 468 | const int win_width = winsize.ws_col - 1; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 469 | |
Peter Zijlstra | 1676b8a | 2010-03-04 14:19:36 +0100 | [diff] [blame] | 470 | samples = userspace_samples = exact_samples = 0; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 471 | |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 472 | /* Sort the active symbols */ |
Arnaldo Carvalho de Melo | c44613a | 2009-05-29 17:03:07 -0300 | [diff] [blame] | 473 | pthread_mutex_lock(&active_symbols_lock); |
| 474 | syme = list_entry(active_symbols.next, struct sym_entry, node); |
| 475 | pthread_mutex_unlock(&active_symbols_lock); |
| 476 | |
| 477 | list_for_each_entry_safe_from(syme, n, &active_symbols, node) { |
Mike Galbraith | 46ab976 | 2009-07-24 10:09:50 +0200 | [diff] [blame] | 478 | syme->snap_count = syme->count[snap]; |
Arnaldo Carvalho de Melo | c44613a | 2009-05-29 17:03:07 -0300 | [diff] [blame] | 479 | if (syme->snap_count != 0) { |
Arnaldo Carvalho de Melo | 13cc507 | 2009-11-17 15:40:54 -0200 | [diff] [blame] | 480 | |
Arnaldo Carvalho de Melo | 8ffcda1 | 2009-11-16 21:45:24 -0200 | [diff] [blame] | 481 | if ((hide_user_symbols && |
| 482 | syme->origin == PERF_RECORD_MISC_USER) || |
| 483 | (hide_kernel_symbols && |
| 484 | syme->origin == PERF_RECORD_MISC_KERNEL)) { |
| 485 | list_remove_active_sym(syme); |
| 486 | continue; |
| 487 | } |
Arnaldo Carvalho de Melo | c44613a | 2009-05-29 17:03:07 -0300 | [diff] [blame] | 488 | syme->weight = sym_weight(syme); |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 489 | rb_insert_active_sym(&tmp, syme); |
Ingo Molnar | 2debbc8 | 2009-06-05 14:29:10 +0200 | [diff] [blame] | 490 | sum_ksamples += syme->snap_count; |
Mike Galbraith | d94b943 | 2009-05-25 09:57:56 +0200 | [diff] [blame] | 491 | |
| 492 | for (j = 0; j < nr_counters; j++) |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 493 | syme->count[j] = zero ? 0 : syme->count[j] * 7 / 8; |
| 494 | } else |
Arnaldo Carvalho de Melo | c44613a | 2009-05-29 17:03:07 -0300 | [diff] [blame] | 495 | list_remove_active_sym(syme); |
Mike Galbraith | d94b943 | 2009-05-25 09:57:56 +0200 | [diff] [blame] | 496 | } |
| 497 | |
Frederic Weisbecker | 0f5486b | 2009-06-04 20:48:04 +0200 | [diff] [blame] | 498 | puts(CONSOLE_CLEAR); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 499 | |
Arnaldo Carvalho de Melo | 13cc507 | 2009-11-17 15:40:54 -0200 | [diff] [blame] | 500 | printf("%-*.*s\n", win_width, win_width, graph_dotted_line); |
Peter Zijlstra | 1676b8a | 2010-03-04 14:19:36 +0100 | [diff] [blame] | 501 | printf( " PerfTop:%8.0f irqs/sec kernel:%4.1f%% exact: %4.1f%% [", |
Ingo Molnar | 2debbc8 | 2009-06-05 14:29:10 +0200 | [diff] [blame] | 502 | samples_per_sec, |
Peter Zijlstra | 1676b8a | 2010-03-04 14:19:36 +0100 | [diff] [blame] | 503 | 100.0 - (100.0*((samples_per_sec-ksamples_per_sec)/samples_per_sec)), |
| 504 | esamples_percent); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 505 | |
Mike Galbraith | 46ab976 | 2009-07-24 10:09:50 +0200 | [diff] [blame] | 506 | if (nr_counters == 1 || !display_weighted) { |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 507 | printf("%Ld", (u64)attrs[0].sample_period); |
Ingo Molnar | cf1f457 | 2009-06-05 13:27:02 +0200 | [diff] [blame] | 508 | if (freq) |
| 509 | printf("Hz "); |
| 510 | else |
| 511 | printf(" "); |
| 512 | } |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 513 | |
Mike Galbraith | 46ab976 | 2009-07-24 10:09:50 +0200 | [diff] [blame] | 514 | if (!display_weighted) |
| 515 | printf("%s", event_name(sym_counter)); |
| 516 | else for (counter = 0; counter < nr_counters; counter++) { |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 517 | if (counter) |
| 518 | printf("/"); |
| 519 | |
| 520 | printf("%s", event_name(counter)); |
| 521 | } |
| 522 | |
| 523 | printf( "], "); |
| 524 | |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 525 | if (target_pid != -1) |
| 526 | printf(" (target_pid: %d", target_pid); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 527 | else |
| 528 | printf(" (all"); |
| 529 | |
| 530 | if (profile_cpu != -1) |
| 531 | printf(", cpu: %d)\n", profile_cpu); |
| 532 | else { |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 533 | if (target_pid != -1) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 534 | printf(")\n"); |
| 535 | else |
| 536 | printf(", %d CPUs)\n", nr_cpus); |
| 537 | } |
| 538 | |
Arnaldo Carvalho de Melo | 1a105f7 | 2009-11-17 15:40:55 -0200 | [diff] [blame] | 539 | printf("%-*.*s\n", win_width, win_width, graph_dotted_line); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 540 | |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 541 | if (sym_filter_entry) { |
| 542 | show_details(sym_filter_entry); |
| 543 | return; |
| 544 | } |
| 545 | |
Arnaldo Carvalho de Melo | 13cc507 | 2009-11-17 15:40:54 -0200 | [diff] [blame] | 546 | /* |
| 547 | * Find the longest symbol name that will be displayed |
| 548 | */ |
| 549 | for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) { |
| 550 | syme = rb_entry(nd, struct sym_entry, rb_node); |
| 551 | if (++printed > print_entries || |
| 552 | (int)syme->snap_count < count_filter) |
| 553 | continue; |
| 554 | |
Arnaldo Carvalho de Melo | 1a105f7 | 2009-11-17 15:40:55 -0200 | [diff] [blame] | 555 | if (syme->map->dso->long_name_len > dso_width) |
| 556 | dso_width = syme->map->dso->long_name_len; |
| 557 | |
Arnaldo Carvalho de Melo | b63be8d | 2010-03-15 15:03:50 -0300 | [diff] [blame] | 558 | if (syme->map->dso->short_name_len > dso_short_width) |
| 559 | dso_short_width = syme->map->dso->short_name_len; |
| 560 | |
Arnaldo Carvalho de Melo | 13cc507 | 2009-11-17 15:40:54 -0200 | [diff] [blame] | 561 | if (syme->name_len > sym_width) |
| 562 | sym_width = syme->name_len; |
| 563 | } |
| 564 | |
| 565 | printed = 0; |
| 566 | |
Arnaldo Carvalho de Melo | b63be8d | 2010-03-15 15:03:50 -0300 | [diff] [blame] | 567 | if (sym_width + dso_width > winsize.ws_col - 29) { |
| 568 | dso_width = dso_short_width; |
| 569 | if (sym_width + dso_width > winsize.ws_col - 29) |
| 570 | sym_width = winsize.ws_col - dso_width - 29; |
| 571 | } |
Arnaldo Carvalho de Melo | 7cc017e | 2009-11-24 12:05:14 -0200 | [diff] [blame] | 572 | putchar('\n'); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 573 | if (nr_counters == 1) |
Arnaldo Carvalho de Melo | 5b2bb75 | 2009-10-26 19:23:19 -0200 | [diff] [blame] | 574 | printf(" samples pcnt"); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 575 | else |
Arnaldo Carvalho de Melo | 5b2bb75 | 2009-10-26 19:23:19 -0200 | [diff] [blame] | 576 | printf(" weight samples pcnt"); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 577 | |
Arnaldo Carvalho de Melo | 7ced156 | 2009-08-26 11:51:26 -0300 | [diff] [blame] | 578 | if (verbose) |
| 579 | printf(" RIP "); |
Arnaldo Carvalho de Melo | 7cc017e | 2009-11-24 12:05:14 -0200 | [diff] [blame] | 580 | printf(" %-*.*s DSO\n", sym_width, sym_width, "function"); |
Arnaldo Carvalho de Melo | 5b2bb75 | 2009-10-26 19:23:19 -0200 | [diff] [blame] | 581 | printf(" %s _______ _____", |
Arnaldo Carvalho de Melo | 7ced156 | 2009-08-26 11:51:26 -0300 | [diff] [blame] | 582 | nr_counters == 1 ? " " : "______"); |
| 583 | if (verbose) |
Arnaldo Carvalho de Melo | 5b2bb75 | 2009-10-26 19:23:19 -0200 | [diff] [blame] | 584 | printf(" ________________"); |
Arnaldo Carvalho de Melo | 1a105f7 | 2009-11-17 15:40:55 -0200 | [diff] [blame] | 585 | printf(" %-*.*s", sym_width, sym_width, graph_line); |
Arnaldo Carvalho de Melo | 7cc017e | 2009-11-24 12:05:14 -0200 | [diff] [blame] | 586 | printf(" %-*.*s", dso_width, dso_width, graph_line); |
Arnaldo Carvalho de Melo | 1a105f7 | 2009-11-17 15:40:55 -0200 | [diff] [blame] | 587 | puts("\n"); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 588 | |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 589 | for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) { |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 590 | struct symbol *sym; |
Ingo Molnar | 8fc0321 | 2009-06-04 15:19:47 +0200 | [diff] [blame] | 591 | double pcnt; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 592 | |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 593 | syme = rb_entry(nd, struct sym_entry, rb_node); |
Arnaldo Carvalho de Melo | 51a472d | 2009-11-17 18:38:00 -0200 | [diff] [blame] | 594 | sym = sym_entry__symbol(syme); |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 595 | |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 596 | if (++printed > print_entries || (int)syme->snap_count < count_filter) |
Arnaldo Carvalho de Melo | c44613a | 2009-05-29 17:03:07 -0300 | [diff] [blame] | 597 | continue; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 598 | |
Ingo Molnar | 2debbc8 | 2009-06-05 14:29:10 +0200 | [diff] [blame] | 599 | pcnt = 100.0 - (100.0 * ((sum_ksamples - syme->snap_count) / |
| 600 | sum_ksamples)); |
Mike Galbraith | d94b943 | 2009-05-25 09:57:56 +0200 | [diff] [blame] | 601 | |
Mike Galbraith | 46ab976 | 2009-07-24 10:09:50 +0200 | [diff] [blame] | 602 | if (nr_counters == 1 || !display_weighted) |
Arnaldo Carvalho de Melo | 5b2bb75 | 2009-10-26 19:23:19 -0200 | [diff] [blame] | 603 | printf("%20.2f ", syme->weight); |
Mike Galbraith | d94b943 | 2009-05-25 09:57:56 +0200 | [diff] [blame] | 604 | else |
Arnaldo Carvalho de Melo | 5b2bb75 | 2009-10-26 19:23:19 -0200 | [diff] [blame] | 605 | printf("%9.1f %10ld ", syme->weight, syme->snap_count); |
Ingo Molnar | 8fc0321 | 2009-06-04 15:19:47 +0200 | [diff] [blame] | 606 | |
Frederic Weisbecker | 1e11fd8 | 2009-07-02 20:14:34 +0200 | [diff] [blame] | 607 | percent_color_fprintf(stdout, "%4.1f%%", pcnt); |
Arnaldo Carvalho de Melo | 7ced156 | 2009-08-26 11:51:26 -0300 | [diff] [blame] | 608 | if (verbose) |
Arnaldo Carvalho de Melo | 5b2bb75 | 2009-10-26 19:23:19 -0200 | [diff] [blame] | 609 | printf(" %016llx", sym->start); |
Arnaldo Carvalho de Melo | 13cc507 | 2009-11-17 15:40:54 -0200 | [diff] [blame] | 610 | printf(" %-*.*s", sym_width, sym_width, sym->name); |
Arnaldo Carvalho de Melo | 7cc017e | 2009-11-24 12:05:14 -0200 | [diff] [blame] | 611 | printf(" %-*.*s\n", dso_width, dso_width, |
| 612 | dso_width >= syme->map->dso->long_name_len ? |
| 613 | syme->map->dso->long_name : |
| 614 | syme->map->dso->short_name); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 615 | } |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 616 | } |
| 617 | |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 618 | static void prompt_integer(int *target, const char *msg) |
| 619 | { |
| 620 | char *buf = malloc(0), *p; |
| 621 | size_t dummy = 0; |
| 622 | int tmp; |
| 623 | |
| 624 | fprintf(stdout, "\n%s: ", msg); |
| 625 | if (getline(&buf, &dummy, stdin) < 0) |
| 626 | return; |
| 627 | |
| 628 | p = strchr(buf, '\n'); |
| 629 | if (p) |
| 630 | *p = 0; |
| 631 | |
| 632 | p = buf; |
| 633 | while(*p) { |
| 634 | if (!isdigit(*p)) |
| 635 | goto out_free; |
| 636 | p++; |
| 637 | } |
| 638 | tmp = strtoul(buf, NULL, 10); |
| 639 | *target = tmp; |
| 640 | out_free: |
| 641 | free(buf); |
| 642 | } |
| 643 | |
| 644 | static void prompt_percent(int *target, const char *msg) |
| 645 | { |
| 646 | int tmp = 0; |
| 647 | |
| 648 | prompt_integer(&tmp, msg); |
| 649 | if (tmp >= 0 && tmp <= 100) |
| 650 | *target = tmp; |
| 651 | } |
| 652 | |
| 653 | static void prompt_symbol(struct sym_entry **target, const char *msg) |
| 654 | { |
| 655 | char *buf = malloc(0), *p; |
| 656 | struct sym_entry *syme = *target, *n, *found = NULL; |
| 657 | size_t dummy = 0; |
| 658 | |
| 659 | /* zero counters of active symbol */ |
| 660 | if (syme) { |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 661 | pthread_mutex_lock(&syme->src->lock); |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 662 | __zero_source_counters(syme); |
| 663 | *target = NULL; |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 664 | pthread_mutex_unlock(&syme->src->lock); |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | fprintf(stdout, "\n%s: ", msg); |
| 668 | if (getline(&buf, &dummy, stdin) < 0) |
| 669 | goto out_free; |
| 670 | |
| 671 | p = strchr(buf, '\n'); |
| 672 | if (p) |
| 673 | *p = 0; |
| 674 | |
| 675 | pthread_mutex_lock(&active_symbols_lock); |
| 676 | syme = list_entry(active_symbols.next, struct sym_entry, node); |
| 677 | pthread_mutex_unlock(&active_symbols_lock); |
| 678 | |
| 679 | list_for_each_entry_safe_from(syme, n, &active_symbols, node) { |
Arnaldo Carvalho de Melo | 51a472d | 2009-11-17 18:38:00 -0200 | [diff] [blame] | 680 | struct symbol *sym = sym_entry__symbol(syme); |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 681 | |
| 682 | if (!strcmp(buf, sym->name)) { |
| 683 | found = syme; |
| 684 | break; |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | if (!found) { |
Kirill Smelkov | 66aeb6d | 2010-01-13 13:22:19 -0200 | [diff] [blame] | 689 | fprintf(stderr, "Sorry, %s is not active.\n", buf); |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 690 | sleep(1); |
| 691 | return; |
| 692 | } else |
| 693 | parse_source(found); |
| 694 | |
| 695 | out_free: |
| 696 | free(buf); |
| 697 | } |
| 698 | |
Mike Galbraith | 091bd2e | 2009-08-04 10:21:23 +0200 | [diff] [blame] | 699 | static void print_mapped_keys(void) |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 700 | { |
Mike Galbraith | 091bd2e | 2009-08-04 10:21:23 +0200 | [diff] [blame] | 701 | char *name = NULL; |
| 702 | |
| 703 | if (sym_filter_entry) { |
Arnaldo Carvalho de Melo | 51a472d | 2009-11-17 18:38:00 -0200 | [diff] [blame] | 704 | struct symbol *sym = sym_entry__symbol(sym_filter_entry); |
Mike Galbraith | 091bd2e | 2009-08-04 10:21:23 +0200 | [diff] [blame] | 705 | name = sym->name; |
| 706 | } |
| 707 | |
| 708 | fprintf(stdout, "\nMapped keys:\n"); |
| 709 | fprintf(stdout, "\t[d] display refresh delay. \t(%d)\n", delay_secs); |
| 710 | fprintf(stdout, "\t[e] display entries (lines). \t(%d)\n", print_entries); |
| 711 | |
| 712 | if (nr_counters > 1) |
| 713 | fprintf(stdout, "\t[E] active event counter. \t(%s)\n", event_name(sym_counter)); |
| 714 | |
| 715 | fprintf(stdout, "\t[f] profile display filter (count). \t(%d)\n", count_filter); |
| 716 | |
Kirill Smelkov | 6cff0e8 | 2010-02-03 16:52:08 -0200 | [diff] [blame] | 717 | fprintf(stdout, "\t[F] annotate display filter (percent). \t(%d%%)\n", sym_pcnt_filter); |
| 718 | fprintf(stdout, "\t[s] annotate symbol. \t(%s)\n", name?: "NULL"); |
| 719 | fprintf(stdout, "\t[S] stop annotation.\n"); |
Mike Galbraith | 091bd2e | 2009-08-04 10:21:23 +0200 | [diff] [blame] | 720 | |
| 721 | if (nr_counters > 1) |
| 722 | fprintf(stdout, "\t[w] toggle display weighted/count[E]r. \t(%d)\n", display_weighted ? 1 : 0); |
| 723 | |
Arnaldo Carvalho de Melo | 8ffcda1 | 2009-11-16 21:45:24 -0200 | [diff] [blame] | 724 | fprintf(stdout, |
Kirill Smelkov | 1a72cfa | 2010-02-12 19:20:59 +0300 | [diff] [blame] | 725 | "\t[K] hide kernel_symbols symbols. \t(%s)\n", |
Arnaldo Carvalho de Melo | 8ffcda1 | 2009-11-16 21:45:24 -0200 | [diff] [blame] | 726 | hide_kernel_symbols ? "yes" : "no"); |
| 727 | fprintf(stdout, |
| 728 | "\t[U] hide user symbols. \t(%s)\n", |
| 729 | hide_user_symbols ? "yes" : "no"); |
Mike Galbraith | 46ab976 | 2009-07-24 10:09:50 +0200 | [diff] [blame] | 730 | fprintf(stdout, "\t[z] toggle sample zeroing. \t(%d)\n", zero ? 1 : 0); |
Mike Galbraith | 091bd2e | 2009-08-04 10:21:23 +0200 | [diff] [blame] | 731 | fprintf(stdout, "\t[qQ] quit.\n"); |
| 732 | } |
| 733 | |
| 734 | static int key_mapped(int c) |
| 735 | { |
| 736 | switch (c) { |
| 737 | case 'd': |
| 738 | case 'e': |
| 739 | case 'f': |
| 740 | case 'z': |
| 741 | case 'q': |
| 742 | case 'Q': |
Arnaldo Carvalho de Melo | 8ffcda1 | 2009-11-16 21:45:24 -0200 | [diff] [blame] | 743 | case 'K': |
| 744 | case 'U': |
Kirill Smelkov | 6cff0e8 | 2010-02-03 16:52:08 -0200 | [diff] [blame] | 745 | case 'F': |
| 746 | case 's': |
| 747 | case 'S': |
Mike Galbraith | 091bd2e | 2009-08-04 10:21:23 +0200 | [diff] [blame] | 748 | return 1; |
| 749 | case 'E': |
| 750 | case 'w': |
| 751 | return nr_counters > 1 ? 1 : 0; |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 752 | default: |
| 753 | break; |
Mike Galbraith | 091bd2e | 2009-08-04 10:21:23 +0200 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | return 0; |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | static void handle_keypress(int c) |
| 760 | { |
Mike Galbraith | 091bd2e | 2009-08-04 10:21:23 +0200 | [diff] [blame] | 761 | if (!key_mapped(c)) { |
| 762 | struct pollfd stdin_poll = { .fd = 0, .events = POLLIN }; |
| 763 | struct termios tc, save; |
| 764 | |
| 765 | print_mapped_keys(); |
| 766 | fprintf(stdout, "\nEnter selection, or unmapped key to continue: "); |
| 767 | fflush(stdout); |
| 768 | |
| 769 | tcgetattr(0, &save); |
| 770 | tc = save; |
| 771 | tc.c_lflag &= ~(ICANON | ECHO); |
| 772 | tc.c_cc[VMIN] = 0; |
| 773 | tc.c_cc[VTIME] = 0; |
| 774 | tcsetattr(0, TCSANOW, &tc); |
| 775 | |
| 776 | poll(&stdin_poll, 1, -1); |
| 777 | c = getc(stdin); |
| 778 | |
| 779 | tcsetattr(0, TCSAFLUSH, &save); |
| 780 | if (!key_mapped(c)) |
| 781 | return; |
| 782 | } |
| 783 | |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 784 | switch (c) { |
| 785 | case 'd': |
| 786 | prompt_integer(&delay_secs, "Enter display delay"); |
Tim Blechmann | dc79959 | 2009-10-17 18:08:29 +0200 | [diff] [blame] | 787 | if (delay_secs < 1) |
| 788 | delay_secs = 1; |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 789 | break; |
| 790 | case 'e': |
| 791 | prompt_integer(&print_entries, "Enter display entries (lines)"); |
Arnaldo Carvalho de Melo | 3b6ed98 | 2009-11-16 19:30:27 -0200 | [diff] [blame] | 792 | if (print_entries == 0) { |
Arnaldo Carvalho de Melo | 13cc507 | 2009-11-17 15:40:54 -0200 | [diff] [blame] | 793 | sig_winch_handler(SIGWINCH); |
Arnaldo Carvalho de Melo | 3b6ed98 | 2009-11-16 19:30:27 -0200 | [diff] [blame] | 794 | signal(SIGWINCH, sig_winch_handler); |
| 795 | } else |
| 796 | signal(SIGWINCH, SIG_DFL); |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 797 | break; |
| 798 | case 'E': |
| 799 | if (nr_counters > 1) { |
| 800 | int i; |
| 801 | |
| 802 | fprintf(stderr, "\nAvailable events:"); |
| 803 | for (i = 0; i < nr_counters; i++) |
| 804 | fprintf(stderr, "\n\t%d %s", i, event_name(i)); |
| 805 | |
| 806 | prompt_integer(&sym_counter, "Enter details event counter"); |
| 807 | |
| 808 | if (sym_counter >= nr_counters) { |
| 809 | fprintf(stderr, "Sorry, no such event, using %s.\n", event_name(0)); |
| 810 | sym_counter = 0; |
| 811 | sleep(1); |
| 812 | } |
| 813 | } else sym_counter = 0; |
| 814 | break; |
| 815 | case 'f': |
| 816 | prompt_integer(&count_filter, "Enter display event count filter"); |
| 817 | break; |
| 818 | case 'F': |
| 819 | prompt_percent(&sym_pcnt_filter, "Enter details display event filter (percent)"); |
| 820 | break; |
Arnaldo Carvalho de Melo | 8ffcda1 | 2009-11-16 21:45:24 -0200 | [diff] [blame] | 821 | case 'K': |
| 822 | hide_kernel_symbols = !hide_kernel_symbols; |
| 823 | break; |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 824 | case 'q': |
| 825 | case 'Q': |
| 826 | printf("exiting.\n"); |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 827 | if (dump_symtab) |
| 828 | dsos__fprintf(stderr); |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 829 | exit(0); |
| 830 | case 's': |
| 831 | prompt_symbol(&sym_filter_entry, "Enter details symbol"); |
| 832 | break; |
| 833 | case 'S': |
| 834 | if (!sym_filter_entry) |
| 835 | break; |
| 836 | else { |
| 837 | struct sym_entry *syme = sym_filter_entry; |
| 838 | |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 839 | pthread_mutex_lock(&syme->src->lock); |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 840 | sym_filter_entry = NULL; |
| 841 | __zero_source_counters(syme); |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 842 | pthread_mutex_unlock(&syme->src->lock); |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 843 | } |
| 844 | break; |
Arnaldo Carvalho de Melo | 8ffcda1 | 2009-11-16 21:45:24 -0200 | [diff] [blame] | 845 | case 'U': |
| 846 | hide_user_symbols = !hide_user_symbols; |
| 847 | break; |
Mike Galbraith | 46ab976 | 2009-07-24 10:09:50 +0200 | [diff] [blame] | 848 | case 'w': |
| 849 | display_weighted = ~display_weighted; |
| 850 | break; |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 851 | case 'z': |
| 852 | zero = ~zero; |
| 853 | break; |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 854 | default: |
| 855 | break; |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 856 | } |
| 857 | } |
| 858 | |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 859 | static void *display_thread(void *arg __used) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 860 | { |
Frederic Weisbecker | 0f5486b | 2009-06-04 20:48:04 +0200 | [diff] [blame] | 861 | struct pollfd stdin_poll = { .fd = 0, .events = POLLIN }; |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 862 | struct termios tc, save; |
| 863 | int delay_msecs, c; |
Frederic Weisbecker | 0f5486b | 2009-06-04 20:48:04 +0200 | [diff] [blame] | 864 | |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 865 | tcgetattr(0, &save); |
| 866 | tc = save; |
| 867 | tc.c_lflag &= ~(ICANON | ECHO); |
| 868 | tc.c_cc[VMIN] = 0; |
| 869 | tc.c_cc[VTIME] = 0; |
Mike Galbraith | 091bd2e | 2009-08-04 10:21:23 +0200 | [diff] [blame] | 870 | |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 871 | repeat: |
| 872 | delay_msecs = delay_secs * 1000; |
| 873 | tcsetattr(0, TCSANOW, &tc); |
| 874 | /* trash return*/ |
| 875 | getc(stdin); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 876 | |
Frederic Weisbecker | 0f5486b | 2009-06-04 20:48:04 +0200 | [diff] [blame] | 877 | do { |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 878 | print_sym_table(); |
Frederic Weisbecker | 0f5486b | 2009-06-04 20:48:04 +0200 | [diff] [blame] | 879 | } while (!poll(&stdin_poll, 1, delay_msecs) == 1); |
| 880 | |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 881 | c = getc(stdin); |
| 882 | tcsetattr(0, TCSAFLUSH, &save); |
| 883 | |
| 884 | handle_keypress(c); |
| 885 | goto repeat; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 886 | |
| 887 | return NULL; |
| 888 | } |
| 889 | |
Anton Blanchard | 2ab5208 | 2009-07-01 09:00:46 +1000 | [diff] [blame] | 890 | /* Tag samples to be skipped. */ |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 891 | static const char *skip_symbols[] = { |
Anton Blanchard | 2ab5208 | 2009-07-01 09:00:46 +1000 | [diff] [blame] | 892 | "default_idle", |
| 893 | "cpu_idle", |
| 894 | "enter_idle", |
| 895 | "exit_idle", |
| 896 | "mwait_idle", |
Arnaldo Carvalho de Melo | 59b9005 | 2009-07-26 19:06:19 -0300 | [diff] [blame] | 897 | "mwait_idle_with_hints", |
Arnaldo Carvalho de Melo | 8357275 | 2009-09-25 15:02:39 -0700 | [diff] [blame] | 898 | "poll_idle", |
Anton Blanchard | 3a3393e | 2009-07-01 09:00:47 +1000 | [diff] [blame] | 899 | "ppc64_runlatch_off", |
| 900 | "pseries_dedicated_idle_sleep", |
Anton Blanchard | 2ab5208 | 2009-07-01 09:00:46 +1000 | [diff] [blame] | 901 | NULL |
| 902 | }; |
| 903 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 904 | static int symbol_filter(struct map *map, struct symbol *sym) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 905 | { |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 906 | struct sym_entry *syme; |
| 907 | const char *name = sym->name; |
Anton Blanchard | 2ab5208 | 2009-07-01 09:00:46 +1000 | [diff] [blame] | 908 | int i; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 909 | |
Anton Blanchard | 3a3393e | 2009-07-01 09:00:47 +1000 | [diff] [blame] | 910 | /* |
| 911 | * ppc64 uses function descriptors and appends a '.' to the |
| 912 | * start of every instruction address. Remove it. |
| 913 | */ |
| 914 | if (name[0] == '.') |
| 915 | name++; |
| 916 | |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 917 | if (!strcmp(name, "_text") || |
| 918 | !strcmp(name, "_etext") || |
| 919 | !strcmp(name, "_sinittext") || |
| 920 | !strncmp("init_module", name, 11) || |
| 921 | !strncmp("cleanup_module", name, 14) || |
| 922 | strstr(name, "_text_start") || |
| 923 | strstr(name, "_text_end")) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 924 | return 1; |
| 925 | |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 926 | syme = symbol__priv(sym); |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 927 | syme->map = map; |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 928 | syme->src = NULL; |
Kirill Smelkov | 6cff0e8 | 2010-02-03 16:52:08 -0200 | [diff] [blame] | 929 | |
| 930 | if (!sym_filter_entry && sym_filter && !strcmp(name, sym_filter)) { |
| 931 | /* schedule initial sym_filter_entry setup */ |
| 932 | sym_filter_entry_sched = syme; |
| 933 | sym_filter = NULL; |
| 934 | } |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 935 | |
Anton Blanchard | 2ab5208 | 2009-07-01 09:00:46 +1000 | [diff] [blame] | 936 | for (i = 0; skip_symbols[i]; i++) { |
| 937 | if (!strcmp(skip_symbols[i], name)) { |
| 938 | syme->skip = 1; |
| 939 | break; |
| 940 | } |
| 941 | } |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 942 | |
Arnaldo Carvalho de Melo | 13cc507 | 2009-11-17 15:40:54 -0200 | [diff] [blame] | 943 | if (!syme->skip) |
| 944 | syme->name_len = strlen(sym->name); |
| 945 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 946 | return 0; |
| 947 | } |
| 948 | |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 949 | static void event__process_sample(const event_t *self, |
| 950 | struct perf_session *session, int counter) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 951 | { |
Arnaldo Carvalho de Melo | 5b2bb75 | 2009-10-26 19:23:19 -0200 | [diff] [blame] | 952 | u64 ip = self->ip.ip; |
Arnaldo Carvalho de Melo | 5b2bb75 | 2009-10-26 19:23:19 -0200 | [diff] [blame] | 953 | struct sym_entry *syme; |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 954 | struct addr_location al; |
Arnaldo Carvalho de Melo | 8ffcda1 | 2009-11-16 21:45:24 -0200 | [diff] [blame] | 955 | u8 origin = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 956 | |
Arnaldo Carvalho de Melo | 24bfef0 | 2010-01-21 13:04:43 -0200 | [diff] [blame] | 957 | ++samples; |
| 958 | |
Arnaldo Carvalho de Melo | 8ffcda1 | 2009-11-16 21:45:24 -0200 | [diff] [blame] | 959 | switch (origin) { |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 960 | case PERF_RECORD_MISC_USER: |
Arnaldo Carvalho de Melo | 24bfef0 | 2010-01-21 13:04:43 -0200 | [diff] [blame] | 961 | ++userspace_samples; |
Arnaldo Carvalho de Melo | 8ffcda1 | 2009-11-16 21:45:24 -0200 | [diff] [blame] | 962 | if (hide_user_symbols) |
| 963 | return; |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 964 | break; |
Arnaldo Carvalho de Melo | 5b2bb75 | 2009-10-26 19:23:19 -0200 | [diff] [blame] | 965 | case PERF_RECORD_MISC_KERNEL: |
Arnaldo Carvalho de Melo | 8ffcda1 | 2009-11-16 21:45:24 -0200 | [diff] [blame] | 966 | if (hide_kernel_symbols) |
| 967 | return; |
Arnaldo Carvalho de Melo | 5b2bb75 | 2009-10-26 19:23:19 -0200 | [diff] [blame] | 968 | break; |
| 969 | default: |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 970 | return; |
| 971 | } |
| 972 | |
Peter Zijlstra | 1676b8a | 2010-03-04 14:19:36 +0100 | [diff] [blame] | 973 | if (self->header.misc & PERF_RECORD_MISC_EXACT) |
| 974 | exact_samples++; |
| 975 | |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 976 | if (event__preprocess_sample(self, session, &al, symbol_filter) < 0 || |
Arnaldo Carvalho de Melo | 72b8fa1 | 2010-01-27 21:05:49 -0200 | [diff] [blame] | 977 | al.filtered) |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 978 | return; |
Arnaldo Carvalho de Melo | 5b2bb75 | 2009-10-26 19:23:19 -0200 | [diff] [blame] | 979 | |
Arnaldo Carvalho de Melo | 72b8fa1 | 2010-01-27 21:05:49 -0200 | [diff] [blame] | 980 | if (al.sym == NULL) { |
| 981 | /* |
| 982 | * As we do lazy loading of symtabs we only will know if the |
| 983 | * specified vmlinux file is invalid when we actually have a |
| 984 | * hit in kernel space and then try to load it. So if we get |
| 985 | * here and there are _no_ symbols in the DSO backing the |
| 986 | * kernel map, bail out. |
| 987 | * |
| 988 | * We may never get here, for instance, if we use -K/ |
| 989 | * --hide-kernel-symbols, even if the user specifies an |
| 990 | * invalid --vmlinux ;-) |
| 991 | */ |
| 992 | if (al.map == session->vmlinux_maps[MAP__FUNCTION] && |
| 993 | RB_EMPTY_ROOT(&al.map->dso->symbols[MAP__FUNCTION])) { |
| 994 | pr_err("The %s file can't be used\n", |
| 995 | symbol_conf.vmlinux_name); |
| 996 | exit(1); |
| 997 | } |
| 998 | |
| 999 | return; |
| 1000 | } |
| 1001 | |
Kirill Smelkov | 6cff0e8 | 2010-02-03 16:52:08 -0200 | [diff] [blame] | 1002 | /* let's see, whether we need to install initial sym_filter_entry */ |
| 1003 | if (sym_filter_entry_sched) { |
| 1004 | sym_filter_entry = sym_filter_entry_sched; |
| 1005 | sym_filter_entry_sched = NULL; |
Arnaldo Carvalho de Melo | b0a9ab6 | 2010-03-15 11:46:58 -0300 | [diff] [blame] | 1006 | if (parse_source(sym_filter_entry) < 0) { |
| 1007 | struct symbol *sym = sym_entry__symbol(sym_filter_entry); |
| 1008 | |
| 1009 | pr_err("Can't annotate %s", sym->name); |
| 1010 | if (sym_filter_entry->map->dso->origin == DSO__ORIG_KERNEL) { |
| 1011 | pr_err(": No vmlinux file was found in the path:\n"); |
| 1012 | vmlinux_path__fprintf(stderr); |
| 1013 | } else |
| 1014 | pr_err(".\n"); |
| 1015 | exit(1); |
| 1016 | } |
Kirill Smelkov | 6cff0e8 | 2010-02-03 16:52:08 -0200 | [diff] [blame] | 1017 | } |
| 1018 | |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1019 | syme = symbol__priv(al.sym); |
Arnaldo Carvalho de Melo | 5b2bb75 | 2009-10-26 19:23:19 -0200 | [diff] [blame] | 1020 | if (!syme->skip) { |
| 1021 | syme->count[counter]++; |
Arnaldo Carvalho de Melo | 8ffcda1 | 2009-11-16 21:45:24 -0200 | [diff] [blame] | 1022 | syme->origin = origin; |
Arnaldo Carvalho de Melo | 5b2bb75 | 2009-10-26 19:23:19 -0200 | [diff] [blame] | 1023 | record_precise_ip(syme, counter, ip); |
| 1024 | pthread_mutex_lock(&active_symbols_lock); |
| 1025 | if (list_empty(&syme->node) || !syme->node.next) |
| 1026 | __list_insert_active_sym(syme); |
| 1027 | pthread_mutex_unlock(&active_symbols_lock); |
Arnaldo Carvalho de Melo | 5b2bb75 | 2009-10-26 19:23:19 -0200 | [diff] [blame] | 1028 | } |
| 1029 | } |
| 1030 | |
Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 1031 | static int event__process(event_t *event, struct perf_session *session) |
Arnaldo Carvalho de Melo | 5b2bb75 | 2009-10-26 19:23:19 -0200 | [diff] [blame] | 1032 | { |
| 1033 | switch (event->header.type) { |
| 1034 | case PERF_RECORD_COMM: |
Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 1035 | event__process_comm(event, session); |
Arnaldo Carvalho de Melo | 5b2bb75 | 2009-10-26 19:23:19 -0200 | [diff] [blame] | 1036 | break; |
| 1037 | case PERF_RECORD_MMAP: |
Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 1038 | event__process_mmap(event, session); |
Arnaldo Carvalho de Melo | 5b2bb75 | 2009-10-26 19:23:19 -0200 | [diff] [blame] | 1039 | break; |
Arnaldo Carvalho de Melo | 0f35cd4 | 2010-01-21 13:04:44 -0200 | [diff] [blame] | 1040 | case PERF_RECORD_FORK: |
| 1041 | case PERF_RECORD_EXIT: |
| 1042 | event__process_task(event, session); |
| 1043 | break; |
Arnaldo Carvalho de Melo | 5b2bb75 | 2009-10-26 19:23:19 -0200 | [diff] [blame] | 1044 | default: |
| 1045 | break; |
| 1046 | } |
| 1047 | |
| 1048 | return 0; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 1049 | } |
| 1050 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 1051 | struct mmap_data { |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 1052 | int counter; |
| 1053 | void *base; |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 1054 | int mask; |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 1055 | unsigned int prev; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 1056 | }; |
| 1057 | |
| 1058 | static unsigned int mmap_read_head(struct mmap_data *md) |
| 1059 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1060 | struct perf_event_mmap_page *pc = md->base; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 1061 | int head; |
| 1062 | |
| 1063 | head = pc->data_head; |
| 1064 | rmb(); |
| 1065 | |
| 1066 | return head; |
| 1067 | } |
| 1068 | |
Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 1069 | static void perf_session__mmap_read_counter(struct perf_session *self, |
| 1070 | struct mmap_data *md) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 1071 | { |
| 1072 | unsigned int head = mmap_read_head(md); |
| 1073 | unsigned int old = md->prev; |
| 1074 | unsigned char *data = md->base + page_size; |
| 1075 | int diff; |
| 1076 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 1077 | /* |
| 1078 | * If we're further behind than half the buffer, there's a chance |
Ingo Molnar | 2debbc8 | 2009-06-05 14:29:10 +0200 | [diff] [blame] | 1079 | * the writer will bite our tail and mess up the samples under us. |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 1080 | * |
| 1081 | * If we somehow ended up ahead of the head, we got messed up. |
| 1082 | * |
| 1083 | * In either case, truncate and restart at head. |
| 1084 | */ |
| 1085 | diff = head - old; |
| 1086 | if (diff > md->mask / 2 || diff < 0) { |
Mike Galbraith | f4f0b41 | 2009-10-13 14:57:20 +0200 | [diff] [blame] | 1087 | fprintf(stderr, "WARNING: failed to keep up with mmap data.\n"); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 1088 | |
| 1089 | /* |
| 1090 | * head points to a known good entry, start there. |
| 1091 | */ |
| 1092 | old = head; |
| 1093 | } |
| 1094 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 1095 | for (; old != head;) { |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 1096 | event_t *event = (event_t *)&data[old & md->mask]; |
| 1097 | |
| 1098 | event_t event_copy; |
| 1099 | |
Ingo Molnar | 6f06ccb | 2009-04-20 15:22:22 +0200 | [diff] [blame] | 1100 | size_t size = event->header.size; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 1101 | |
| 1102 | /* |
| 1103 | * Event straddles the mmap boundary -- header should always |
| 1104 | * be inside due to u64 alignment of output. |
| 1105 | */ |
| 1106 | if ((old & md->mask) + size != ((old + size) & md->mask)) { |
| 1107 | unsigned int offset = old; |
| 1108 | unsigned int len = min(sizeof(*event), size), cpy; |
| 1109 | void *dst = &event_copy; |
| 1110 | |
| 1111 | do { |
| 1112 | cpy = min(md->mask + 1 - (offset & md->mask), len); |
| 1113 | memcpy(dst, &data[offset & md->mask], cpy); |
| 1114 | offset += cpy; |
| 1115 | dst += cpy; |
| 1116 | len -= cpy; |
| 1117 | } while (len); |
| 1118 | |
| 1119 | event = &event_copy; |
| 1120 | } |
| 1121 | |
Arnaldo Carvalho de Melo | 5b2bb75 | 2009-10-26 19:23:19 -0200 | [diff] [blame] | 1122 | if (event->header.type == PERF_RECORD_SAMPLE) |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 1123 | event__process_sample(event, self, md->counter); |
Arnaldo Carvalho de Melo | 5b2bb75 | 2009-10-26 19:23:19 -0200 | [diff] [blame] | 1124 | else |
Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 1125 | event__process(event, self); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 1126 | old += size; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 1127 | } |
| 1128 | |
| 1129 | md->prev = old; |
| 1130 | } |
| 1131 | |
Mike Galbraith | c2990a2 | 2009-05-24 08:35:49 +0200 | [diff] [blame] | 1132 | static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS]; |
| 1133 | static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS]; |
| 1134 | |
Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 1135 | static void perf_session__mmap_read(struct perf_session *self) |
Frederic Weisbecker | 2f01190 | 2009-06-06 23:10:43 +0200 | [diff] [blame] | 1136 | { |
| 1137 | int i, counter; |
| 1138 | |
| 1139 | for (i = 0; i < nr_cpus; i++) { |
| 1140 | for (counter = 0; counter < nr_counters; counter++) |
Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 1141 | perf_session__mmap_read_counter(self, &mmap_array[i][counter]); |
Frederic Weisbecker | 2f01190 | 2009-06-06 23:10:43 +0200 | [diff] [blame] | 1142 | } |
| 1143 | } |
| 1144 | |
Ingo Molnar | 716c69f | 2009-06-07 17:31:52 +0200 | [diff] [blame] | 1145 | int nr_poll; |
| 1146 | int group_fd; |
| 1147 | |
| 1148 | static void start_counter(int i, int counter) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 1149 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1150 | struct perf_event_attr *attr; |
Mike Galbraith | 0fdc7e6 | 2009-07-21 10:30:36 +0200 | [diff] [blame] | 1151 | int cpu; |
Ingo Molnar | 716c69f | 2009-06-07 17:31:52 +0200 | [diff] [blame] | 1152 | |
| 1153 | cpu = profile_cpu; |
| 1154 | if (target_pid == -1 && profile_cpu == -1) |
Paul Mackerras | a12b51c | 2010-03-10 20:36:09 +1100 | [diff] [blame] | 1155 | cpu = cpumap[i]; |
Ingo Molnar | 716c69f | 2009-06-07 17:31:52 +0200 | [diff] [blame] | 1156 | |
| 1157 | attr = attrs + counter; |
| 1158 | |
| 1159 | attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID; |
Mike Galbraith | 7e4ff9e | 2009-10-12 07:56:03 +0200 | [diff] [blame] | 1160 | |
| 1161 | if (freq) { |
| 1162 | attr->sample_type |= PERF_SAMPLE_PERIOD; |
| 1163 | attr->freq = 1; |
| 1164 | attr->sample_freq = freq; |
| 1165 | } |
| 1166 | |
Mike Galbraith | 0fdc7e6 | 2009-07-21 10:30:36 +0200 | [diff] [blame] | 1167 | attr->inherit = (cpu < 0) && inherit; |
Arnaldo Carvalho de Melo | 5b2bb75 | 2009-10-26 19:23:19 -0200 | [diff] [blame] | 1168 | attr->mmap = 1; |
Ingo Molnar | 716c69f | 2009-06-07 17:31:52 +0200 | [diff] [blame] | 1169 | |
| 1170 | try_again: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1171 | fd[i][counter] = sys_perf_event_open(attr, target_pid, cpu, group_fd, 0); |
Ingo Molnar | 716c69f | 2009-06-07 17:31:52 +0200 | [diff] [blame] | 1172 | |
| 1173 | if (fd[i][counter] < 0) { |
| 1174 | int err = errno; |
| 1175 | |
Pekka Enberg | c10edee | 2009-11-08 18:01:06 +0200 | [diff] [blame] | 1176 | if (err == EPERM || err == EACCES) |
Ingo Molnar | 3da297a | 2009-06-07 17:39:02 +0200 | [diff] [blame] | 1177 | die("No permission - are you root?\n"); |
Ingo Molnar | 716c69f | 2009-06-07 17:31:52 +0200 | [diff] [blame] | 1178 | /* |
| 1179 | * If it's cycles then fall back to hrtimer |
| 1180 | * based cpu-clock-tick sw counter, which |
| 1181 | * is always available even if no PMU support: |
| 1182 | */ |
| 1183 | if (attr->type == PERF_TYPE_HARDWARE |
Peter Zijlstra | f4dbfa8 | 2009-06-11 14:06:28 +0200 | [diff] [blame] | 1184 | && attr->config == PERF_COUNT_HW_CPU_CYCLES) { |
Ingo Molnar | 716c69f | 2009-06-07 17:31:52 +0200 | [diff] [blame] | 1185 | |
Ingo Molnar | 3da297a | 2009-06-07 17:39:02 +0200 | [diff] [blame] | 1186 | if (verbose) |
| 1187 | warning(" ... trying to fall back to cpu-clock-ticks\n"); |
| 1188 | |
Ingo Molnar | 716c69f | 2009-06-07 17:31:52 +0200 | [diff] [blame] | 1189 | attr->type = PERF_TYPE_SOFTWARE; |
Peter Zijlstra | f4dbfa8 | 2009-06-11 14:06:28 +0200 | [diff] [blame] | 1190 | attr->config = PERF_COUNT_SW_CPU_CLOCK; |
Ingo Molnar | 716c69f | 2009-06-07 17:31:52 +0200 | [diff] [blame] | 1191 | goto try_again; |
| 1192 | } |
Ingo Molnar | 30c806a | 2009-06-07 17:46:24 +0200 | [diff] [blame] | 1193 | printf("\n"); |
| 1194 | error("perfcounter syscall returned with %d (%s)\n", |
| 1195 | fd[i][counter], strerror(err)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1196 | die("No CONFIG_PERF_EVENTS=y kernel support configured?\n"); |
Ingo Molnar | 716c69f | 2009-06-07 17:31:52 +0200 | [diff] [blame] | 1197 | exit(-1); |
| 1198 | } |
| 1199 | assert(fd[i][counter] >= 0); |
| 1200 | fcntl(fd[i][counter], F_SETFL, O_NONBLOCK); |
| 1201 | |
| 1202 | /* |
| 1203 | * First counter acts as the group leader: |
| 1204 | */ |
| 1205 | if (group && group_fd == -1) |
| 1206 | group_fd = fd[i][counter]; |
| 1207 | |
| 1208 | event_array[nr_poll].fd = fd[i][counter]; |
| 1209 | event_array[nr_poll].events = POLLIN; |
| 1210 | nr_poll++; |
| 1211 | |
| 1212 | mmap_array[i][counter].counter = counter; |
| 1213 | mmap_array[i][counter].prev = 0; |
| 1214 | mmap_array[i][counter].mask = mmap_pages*page_size - 1; |
| 1215 | mmap_array[i][counter].base = mmap(NULL, (mmap_pages+1)*page_size, |
| 1216 | PROT_READ, MAP_SHARED, fd[i][counter], 0); |
| 1217 | if (mmap_array[i][counter].base == MAP_FAILED) |
| 1218 | die("failed to mmap with %d (%s)\n", errno, strerror(errno)); |
| 1219 | } |
| 1220 | |
| 1221 | static int __cmd_top(void) |
| 1222 | { |
| 1223 | pthread_t thread; |
| 1224 | int i, counter; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 1225 | int ret; |
Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 1226 | /* |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 1227 | * FIXME: perf_session__new should allow passing a O_MMAP, so that all this |
| 1228 | * 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] | 1229 | */ |
Arnaldo Carvalho de Melo | 75be6cf | 2009-12-15 20:04:39 -0200 | [diff] [blame] | 1230 | struct perf_session *session = perf_session__new(NULL, O_WRONLY, false); |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 1231 | if (session == NULL) |
| 1232 | return -ENOMEM; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 1233 | |
Arnaldo Carvalho de Melo | 5b2bb75 | 2009-10-26 19:23:19 -0200 | [diff] [blame] | 1234 | if (target_pid != -1) |
Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 1235 | event__synthesize_thread(target_pid, event__process, session); |
Arnaldo Carvalho de Melo | 5b2bb75 | 2009-10-26 19:23:19 -0200 | [diff] [blame] | 1236 | else |
Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 1237 | event__synthesize_threads(event__process, session); |
Arnaldo Carvalho de Melo | 5b2bb75 | 2009-10-26 19:23:19 -0200 | [diff] [blame] | 1238 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 1239 | for (i = 0; i < nr_cpus; i++) { |
| 1240 | group_fd = -1; |
Ingo Molnar | 716c69f | 2009-06-07 17:31:52 +0200 | [diff] [blame] | 1241 | for (counter = 0; counter < nr_counters; counter++) |
| 1242 | start_counter(i, counter); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 1243 | } |
| 1244 | |
Frederic Weisbecker | 2f01190 | 2009-06-06 23:10:43 +0200 | [diff] [blame] | 1245 | /* Wait for a minimal set of events before starting the snapshot */ |
| 1246 | poll(event_array, nr_poll, 100); |
| 1247 | |
Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 1248 | perf_session__mmap_read(session); |
Frederic Weisbecker | 2f01190 | 2009-06-06 23:10:43 +0200 | [diff] [blame] | 1249 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 1250 | if (pthread_create(&thread, NULL, display_thread, NULL)) { |
| 1251 | printf("Could not create display thread.\n"); |
| 1252 | exit(-1); |
| 1253 | } |
| 1254 | |
| 1255 | if (realtime_prio) { |
| 1256 | struct sched_param param; |
| 1257 | |
| 1258 | param.sched_priority = realtime_prio; |
| 1259 | if (sched_setscheduler(0, SCHED_FIFO, ¶m)) { |
| 1260 | printf("Could not set realtime priority.\n"); |
| 1261 | exit(-1); |
| 1262 | } |
| 1263 | } |
| 1264 | |
| 1265 | while (1) { |
Ingo Molnar | 2debbc8 | 2009-06-05 14:29:10 +0200 | [diff] [blame] | 1266 | int hits = samples; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 1267 | |
Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 1268 | perf_session__mmap_read(session); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 1269 | |
Ingo Molnar | 2debbc8 | 2009-06-05 14:29:10 +0200 | [diff] [blame] | 1270 | if (hits == samples) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 1271 | ret = poll(event_array, nr_poll, 100); |
| 1272 | } |
| 1273 | |
| 1274 | return 0; |
| 1275 | } |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1276 | |
| 1277 | static const char * const top_usage[] = { |
| 1278 | "perf top [<options>]", |
| 1279 | NULL |
| 1280 | }; |
| 1281 | |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1282 | static const struct option options[] = { |
| 1283 | OPT_CALLBACK('e', "event", NULL, "event", |
Thomas Gleixner | 86847b6 | 2009-06-06 12:24:17 +0200 | [diff] [blame] | 1284 | "event selector. use 'perf list' to list available events", |
| 1285 | parse_events), |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1286 | OPT_INTEGER('c', "count", &default_interval, |
| 1287 | "event period to sample"), |
| 1288 | OPT_INTEGER('p', "pid", &target_pid, |
| 1289 | "profile events on existing pid"), |
| 1290 | OPT_BOOLEAN('a', "all-cpus", &system_wide, |
| 1291 | "system-wide collection from all CPUs"), |
| 1292 | OPT_INTEGER('C', "CPU", &profile_cpu, |
| 1293 | "CPU to profile on"), |
Arnaldo Carvalho de Melo | b32d133 | 2009-11-24 12:05:15 -0200 | [diff] [blame] | 1294 | OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name, |
| 1295 | "file", "vmlinux pathname"), |
Arnaldo Carvalho de Melo | 8ffcda1 | 2009-11-16 21:45:24 -0200 | [diff] [blame] | 1296 | OPT_BOOLEAN('K', "hide_kernel_symbols", &hide_kernel_symbols, |
| 1297 | "hide kernel symbols"), |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1298 | OPT_INTEGER('m', "mmap-pages", &mmap_pages, |
| 1299 | "number of mmap data pages"), |
| 1300 | OPT_INTEGER('r', "realtime", &realtime_prio, |
| 1301 | "collect data with this RT SCHED_FIFO priority"), |
Mike Galbraith | db20c00 | 2009-05-26 15:25:34 +0200 | [diff] [blame] | 1302 | OPT_INTEGER('d', "delay", &delay_secs, |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1303 | "number of seconds to delay between refreshes"), |
| 1304 | OPT_BOOLEAN('D', "dump-symtab", &dump_symtab, |
| 1305 | "dump the symbol table used for profiling"), |
Ingo Molnar | 6e53cdf | 2009-06-04 08:53:05 +0200 | [diff] [blame] | 1306 | OPT_INTEGER('f', "count-filter", &count_filter, |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1307 | "only display functions with more events than this"), |
| 1308 | OPT_BOOLEAN('g', "group", &group, |
| 1309 | "put the counters into a counter group"), |
Mike Galbraith | 0fdc7e6 | 2009-07-21 10:30:36 +0200 | [diff] [blame] | 1310 | OPT_BOOLEAN('i', "inherit", &inherit, |
| 1311 | "child tasks inherit counters"), |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 1312 | OPT_STRING('s', "sym-annotate", &sym_filter, "symbol name", |
Kirill Smelkov | 6cff0e8 | 2010-02-03 16:52:08 -0200 | [diff] [blame] | 1313 | "symbol to annotate"), |
Anton Blanchard | 1f208ea | 2009-07-01 09:00:44 +1000 | [diff] [blame] | 1314 | OPT_BOOLEAN('z', "zero", &zero, |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1315 | "zero history across updates"), |
Ingo Molnar | 6e53cdf | 2009-06-04 08:53:05 +0200 | [diff] [blame] | 1316 | OPT_INTEGER('F', "freq", &freq, |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1317 | "profile at this frequency"), |
Ingo Molnar | 6e53cdf | 2009-06-04 08:53:05 +0200 | [diff] [blame] | 1318 | OPT_INTEGER('E', "entries", &print_entries, |
| 1319 | "display this many functions"), |
Arnaldo Carvalho de Melo | 8ffcda1 | 2009-11-16 21:45:24 -0200 | [diff] [blame] | 1320 | OPT_BOOLEAN('U', "hide_user_symbols", &hide_user_symbols, |
| 1321 | "hide user symbols"), |
Ingo Molnar | 3da297a | 2009-06-07 17:39:02 +0200 | [diff] [blame] | 1322 | OPT_BOOLEAN('v', "verbose", &verbose, |
| 1323 | "be more verbose (show counter open errors, etc)"), |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1324 | OPT_END() |
| 1325 | }; |
| 1326 | |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 1327 | int cmd_top(int argc, const char **argv, const char *prefix __used) |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1328 | { |
Arnaldo Carvalho de Melo | b32d133 | 2009-11-24 12:05:15 -0200 | [diff] [blame] | 1329 | int counter; |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1330 | |
| 1331 | page_size = sysconf(_SC_PAGE_SIZE); |
| 1332 | |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1333 | argc = parse_options(argc, argv, options, top_usage, 0); |
| 1334 | if (argc) |
| 1335 | usage_with_options(top_usage, options); |
| 1336 | |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1337 | /* CPU and PID are mutually exclusive */ |
| 1338 | if (target_pid != -1 && profile_cpu != -1) { |
| 1339 | printf("WARNING: PID switch overriding CPU\n"); |
| 1340 | sleep(1); |
| 1341 | profile_cpu = -1; |
| 1342 | } |
| 1343 | |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 1344 | if (!nr_counters) |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1345 | nr_counters = 1; |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1346 | |
Arnaldo Carvalho de Melo | b32d133 | 2009-11-24 12:05:15 -0200 | [diff] [blame] | 1347 | symbol_conf.priv_size = (sizeof(struct sym_entry) + |
| 1348 | (nr_counters + 1) * sizeof(unsigned long)); |
Kirill Smelkov | 6cff0e8 | 2010-02-03 16:52:08 -0200 | [diff] [blame] | 1349 | |
| 1350 | symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL); |
Arnaldo Carvalho de Melo | 75be6cf | 2009-12-15 20:04:39 -0200 | [diff] [blame] | 1351 | if (symbol__init() < 0) |
Arnaldo Carvalho de Melo | b32d133 | 2009-11-24 12:05:15 -0200 | [diff] [blame] | 1352 | return -1; |
Arnaldo Carvalho de Melo | 5a8e5a3 | 2009-11-17 18:38:01 -0200 | [diff] [blame] | 1353 | |
Frederic Weisbecker | 2f335a0 | 2009-06-05 19:31:01 +0200 | [diff] [blame] | 1354 | if (delay_secs < 1) |
| 1355 | delay_secs = 1; |
| 1356 | |
Mike Galbraith | 7e4ff9e | 2009-10-12 07:56:03 +0200 | [diff] [blame] | 1357 | /* |
| 1358 | * User specified count overrides default frequency. |
| 1359 | */ |
| 1360 | if (default_interval) |
| 1361 | freq = 0; |
| 1362 | else if (freq) { |
| 1363 | default_interval = freq; |
| 1364 | } else { |
| 1365 | fprintf(stderr, "frequency and count are zero, aborting\n"); |
| 1366 | exit(EXIT_FAILURE); |
| 1367 | } |
| 1368 | |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 1369 | /* |
| 1370 | * Fill in the ones not specifically initialized via -c: |
| 1371 | */ |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1372 | for (counter = 0; counter < nr_counters; counter++) { |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 1373 | if (attrs[counter].sample_period) |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1374 | continue; |
| 1375 | |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 1376 | attrs[counter].sample_period = default_interval; |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1377 | } |
| 1378 | |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1379 | if (target_pid != -1 || profile_cpu != -1) |
| 1380 | nr_cpus = 1; |
Paul Mackerras | a12b51c | 2010-03-10 20:36:09 +1100 | [diff] [blame] | 1381 | else |
| 1382 | nr_cpus = read_cpu_map(); |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1383 | |
Arnaldo Carvalho de Melo | 13cc507 | 2009-11-17 15:40:54 -0200 | [diff] [blame] | 1384 | get_term_dimensions(&winsize); |
Arnaldo Carvalho de Melo | 3b6ed98 | 2009-11-16 19:30:27 -0200 | [diff] [blame] | 1385 | if (print_entries == 0) { |
Arnaldo Carvalho de Melo | 13cc507 | 2009-11-17 15:40:54 -0200 | [diff] [blame] | 1386 | update_print_entries(&winsize); |
Arnaldo Carvalho de Melo | 3b6ed98 | 2009-11-16 19:30:27 -0200 | [diff] [blame] | 1387 | signal(SIGWINCH, sig_winch_handler); |
| 1388 | } |
| 1389 | |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1390 | return __cmd_top(); |
| 1391 | } |