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