Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 1 | /* |
Ingo Molnar | bf9e187 | 2009-06-02 23:37:05 +0200 | [diff] [blame] | 2 | * builtin-top.c |
| 3 | * |
| 4 | * Builtin top command: Display a continuously updated profile of |
| 5 | * any workload, CPU or specific PID. |
| 6 | * |
| 7 | * Copyright (C) 2008, Red Hat Inc, Ingo Molnar <mingo@redhat.com> |
| 8 | * |
| 9 | * Improvements and fixes by: |
| 10 | * |
| 11 | * Arjan van de Ven <arjan@linux.intel.com> |
| 12 | * Yanmin Zhang <yanmin.zhang@intel.com> |
| 13 | * Wu Fengguang <fengguang.wu@intel.com> |
| 14 | * Mike Galbraith <efault@gmx.de> |
| 15 | * Paul Mackerras <paulus@samba.org> |
| 16 | * |
| 17 | * Released under the GPL v2. (and only v2, not any later version) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 18 | */ |
Ingo Molnar | bf9e187 | 2009-06-02 23:37:05 +0200 | [diff] [blame] | 19 | #include "builtin.h" |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 20 | |
Peter Zijlstra | 1a482f3 | 2009-05-23 18:28:58 +0200 | [diff] [blame] | 21 | #include "perf.h" |
Ingo Molnar | bf9e187 | 2009-06-02 23:37:05 +0200 | [diff] [blame] | 22 | |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 23 | #include "util/symbol.h" |
Ingo Molnar | 8fc0321 | 2009-06-04 15:19:47 +0200 | [diff] [blame] | 24 | #include "util/color.h" |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 25 | #include "util/thread.h" |
Ingo Molnar | 148be2c | 2009-04-27 08:02:14 +0200 | [diff] [blame] | 26 | #include "util/util.h" |
Arnaldo Carvalho de Melo | 43cbcd8 | 2009-07-01 12:28:37 -0300 | [diff] [blame] | 27 | #include <linux/rbtree.h> |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 28 | #include "util/parse-options.h" |
| 29 | #include "util/parse-events.h" |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 30 | |
Frederic Weisbecker | 8f28827a | 2009-08-16 22:05:48 +0200 | [diff] [blame] | 31 | #include "util/debug.h" |
| 32 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 33 | #include <assert.h> |
| 34 | #include <fcntl.h> |
Ingo Molnar | 0e9b20b | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 35 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 36 | #include <stdio.h> |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 37 | #include <termios.h> |
| 38 | #include <unistd.h> |
Ingo Molnar | 0e9b20b | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 39 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 40 | #include <errno.h> |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 41 | #include <time.h> |
| 42 | #include <sched.h> |
| 43 | #include <pthread.h> |
| 44 | |
| 45 | #include <sys/syscall.h> |
| 46 | #include <sys/ioctl.h> |
| 47 | #include <sys/poll.h> |
| 48 | #include <sys/prctl.h> |
| 49 | #include <sys/wait.h> |
| 50 | #include <sys/uio.h> |
| 51 | #include <sys/mman.h> |
| 52 | |
| 53 | #include <linux/unistd.h> |
| 54 | #include <linux/types.h> |
| 55 | |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 56 | static int fd[MAX_NR_CPUS][MAX_COUNTERS]; |
| 57 | |
Ingo Molnar | 42e59d7 | 2009-10-06 15:14:21 +0200 | [diff] [blame] | 58 | static int system_wide = 0; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 59 | |
Mike Galbraith | 7e4ff9e | 2009-10-12 07:56:03 +0200 | [diff] [blame] | 60 | static int default_interval = 0; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 61 | |
Ingo Molnar | 42e59d7 | 2009-10-06 15:14:21 +0200 | [diff] [blame] | 62 | static int count_filter = 5; |
Arnaldo Carvalho de Melo | 3b6ed98 | 2009-11-16 19:30:27 -0200 | [diff] [blame] | 63 | static int print_entries; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 64 | |
Ingo Molnar | 42e59d7 | 2009-10-06 15:14:21 +0200 | [diff] [blame] | 65 | static int target_pid = -1; |
| 66 | static int inherit = 0; |
| 67 | static int profile_cpu = -1; |
| 68 | static int nr_cpus = 0; |
| 69 | static unsigned int realtime_prio = 0; |
| 70 | static int group = 0; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 71 | static unsigned int page_size; |
Ingo Molnar | 42e59d7 | 2009-10-06 15:14:21 +0200 | [diff] [blame] | 72 | static unsigned int mmap_pages = 16; |
| 73 | static int freq = 1000; /* 1 KHz */ |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 74 | |
Ingo Molnar | 42e59d7 | 2009-10-06 15:14:21 +0200 | [diff] [blame] | 75 | static int delay_secs = 2; |
| 76 | static int zero = 0; |
| 77 | static int dump_symtab = 0; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 78 | |
Arnaldo Carvalho de Melo | 8ffcda1 | 2009-11-16 21:45:24 -0200 | [diff] [blame] | 79 | static bool hide_kernel_symbols = false; |
| 80 | static bool hide_user_symbols = false; |
Arnaldo Carvalho de Melo | 13cc507 | 2009-11-17 15:40:54 -0200 | [diff] [blame] | 81 | static struct winsize winsize; |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame^] | 82 | const char *vmlinux_name; |
Arnaldo Carvalho de Melo | 13cc507 | 2009-11-17 15:40:54 -0200 | [diff] [blame] | 83 | static const char *graph_line = |
| 84 | "_____________________________________________________________________" |
| 85 | "_____________________________________________________________________"; |
| 86 | static const char *graph_dotted_line = |
| 87 | "---------------------------------------------------------------------" |
| 88 | "---------------------------------------------------------------------" |
| 89 | "---------------------------------------------------------------------"; |
Arnaldo Carvalho de Melo | 8ffcda1 | 2009-11-16 21:45:24 -0200 | [diff] [blame] | 90 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 91 | /* |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 92 | * Source |
| 93 | */ |
| 94 | |
| 95 | struct source_line { |
| 96 | u64 eip; |
| 97 | unsigned long count[MAX_COUNTERS]; |
| 98 | char *line; |
| 99 | struct source_line *next; |
| 100 | }; |
| 101 | |
Ingo Molnar | 42e59d7 | 2009-10-06 15:14:21 +0200 | [diff] [blame] | 102 | static char *sym_filter = NULL; |
| 103 | struct sym_entry *sym_filter_entry = NULL; |
| 104 | static int sym_pcnt_filter = 5; |
| 105 | static int sym_counter = 0; |
| 106 | static int display_weighted = -1; |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 107 | |
| 108 | /* |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 109 | * Symbols |
| 110 | */ |
| 111 | |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 112 | struct sym_entry_source { |
| 113 | struct source_line *source; |
| 114 | struct source_line *lines; |
| 115 | struct source_line **lines_tail; |
| 116 | pthread_mutex_t lock; |
| 117 | }; |
| 118 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 119 | struct sym_entry { |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 120 | struct rb_node rb_node; |
| 121 | struct list_head node; |
Arnaldo Carvalho de Melo | c44613a | 2009-05-29 17:03:07 -0300 | [diff] [blame] | 122 | unsigned long snap_count; |
| 123 | double weight; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 124 | int skip; |
Arnaldo Carvalho de Melo | 13cc507 | 2009-11-17 15:40:54 -0200 | [diff] [blame] | 125 | u16 name_len; |
Arnaldo Carvalho de Melo | 8ffcda1 | 2009-11-16 21:45:24 -0200 | [diff] [blame] | 126 | u8 origin; |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 127 | struct map *map; |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 128 | struct sym_entry_source *src; |
Arnaldo Carvalho de Melo | 5a8e5a3 | 2009-11-17 18:38:01 -0200 | [diff] [blame] | 129 | unsigned long count[0]; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 130 | }; |
| 131 | |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 132 | /* |
| 133 | * Source functions |
| 134 | */ |
| 135 | |
Arnaldo Carvalho de Melo | 51a472d | 2009-11-17 18:38:00 -0200 | [diff] [blame] | 136 | static inline struct symbol *sym_entry__symbol(struct sym_entry *self) |
| 137 | { |
Arnaldo Carvalho de Melo | 5a8e5a3 | 2009-11-17 18:38:01 -0200 | [diff] [blame] | 138 | return ((void *)self) + symbol__priv_size; |
Arnaldo Carvalho de Melo | 51a472d | 2009-11-17 18:38:00 -0200 | [diff] [blame] | 139 | } |
| 140 | |
Arnaldo Carvalho de Melo | 13cc507 | 2009-11-17 15:40:54 -0200 | [diff] [blame] | 141 | static void get_term_dimensions(struct winsize *ws) |
Arnaldo Carvalho de Melo | 3b6ed98 | 2009-11-16 19:30:27 -0200 | [diff] [blame] | 142 | { |
Arnaldo Carvalho de Melo | 13cc507 | 2009-11-17 15:40:54 -0200 | [diff] [blame] | 143 | char *s = getenv("LINES"); |
Arnaldo Carvalho de Melo | 3b6ed98 | 2009-11-16 19:30:27 -0200 | [diff] [blame] | 144 | |
Arnaldo Carvalho de Melo | 13cc507 | 2009-11-17 15:40:54 -0200 | [diff] [blame] | 145 | if (s != NULL) { |
| 146 | ws->ws_row = atoi(s); |
| 147 | s = getenv("COLUMNS"); |
| 148 | if (s != NULL) { |
| 149 | ws->ws_col = atoi(s); |
| 150 | if (ws->ws_row && ws->ws_col) |
| 151 | return; |
| 152 | } |
Arnaldo Carvalho de Melo | 3b6ed98 | 2009-11-16 19:30:27 -0200 | [diff] [blame] | 153 | } |
Arnaldo Carvalho de Melo | 13cc507 | 2009-11-17 15:40:54 -0200 | [diff] [blame] | 154 | #ifdef TIOCGWINSZ |
| 155 | if (ioctl(1, TIOCGWINSZ, ws) == 0 && |
| 156 | ws->ws_row && ws->ws_col) |
| 157 | return; |
Arnaldo Carvalho de Melo | 3b6ed98 | 2009-11-16 19:30:27 -0200 | [diff] [blame] | 158 | #endif |
Arnaldo Carvalho de Melo | 13cc507 | 2009-11-17 15:40:54 -0200 | [diff] [blame] | 159 | ws->ws_row = 25; |
| 160 | ws->ws_col = 80; |
Arnaldo Carvalho de Melo | 3b6ed98 | 2009-11-16 19:30:27 -0200 | [diff] [blame] | 161 | } |
| 162 | |
Arnaldo Carvalho de Melo | 13cc507 | 2009-11-17 15:40:54 -0200 | [diff] [blame] | 163 | static void update_print_entries(struct winsize *ws) |
Arnaldo Carvalho de Melo | 3b6ed98 | 2009-11-16 19:30:27 -0200 | [diff] [blame] | 164 | { |
Arnaldo Carvalho de Melo | 13cc507 | 2009-11-17 15:40:54 -0200 | [diff] [blame] | 165 | print_entries = ws->ws_row; |
| 166 | |
Arnaldo Carvalho de Melo | 3b6ed98 | 2009-11-16 19:30:27 -0200 | [diff] [blame] | 167 | if (print_entries > 9) |
| 168 | print_entries -= 9; |
| 169 | } |
| 170 | |
| 171 | static void sig_winch_handler(int sig __used) |
| 172 | { |
Arnaldo Carvalho de Melo | 13cc507 | 2009-11-17 15:40:54 -0200 | [diff] [blame] | 173 | get_term_dimensions(&winsize); |
| 174 | update_print_entries(&winsize); |
Arnaldo Carvalho de Melo | 3b6ed98 | 2009-11-16 19:30:27 -0200 | [diff] [blame] | 175 | } |
| 176 | |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 177 | static void parse_source(struct sym_entry *syme) |
| 178 | { |
| 179 | struct symbol *sym; |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 180 | struct sym_entry_source *source; |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 181 | struct map *map; |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 182 | FILE *file; |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 183 | char command[PATH_MAX*2]; |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 184 | const char *path; |
| 185 | u64 len; |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 186 | |
| 187 | if (!syme) |
| 188 | return; |
| 189 | |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 190 | if (syme->src == NULL) { |
| 191 | syme->src = calloc(1, sizeof(*source)); |
| 192 | if (syme->src == NULL) |
| 193 | return; |
| 194 | pthread_mutex_init(&syme->src->lock, NULL); |
| 195 | } |
| 196 | |
| 197 | source = syme->src; |
| 198 | |
| 199 | if (source->lines) { |
| 200 | pthread_mutex_lock(&source->lock); |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 201 | goto out_assign; |
| 202 | } |
| 203 | |
Arnaldo Carvalho de Melo | 51a472d | 2009-11-17 18:38:00 -0200 | [diff] [blame] | 204 | sym = sym_entry__symbol(syme); |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 205 | map = syme->map; |
| 206 | path = map->dso->long_name; |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 207 | |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 208 | len = sym->end - sym->start; |
| 209 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 210 | sprintf(command, |
| 211 | "objdump --start-address=0x%016Lx " |
| 212 | "--stop-address=0x%016Lx -dS %s", |
Arnaldo Carvalho de Melo | c88e4bf | 2009-10-20 15:54:55 -0200 | [diff] [blame] | 213 | map->unmap_ip(map, sym->start), |
| 214 | map->unmap_ip(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) |
| 218 | return; |
| 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; |
| 225 | char *c; |
| 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 | |
| 244 | if (strlen(src->line)>8 && src->line[8] == ':') { |
| 245 | src->eip = strtoull(src->line, NULL, 16); |
Arnaldo Carvalho de Melo | c88e4bf | 2009-10-20 15:54:55 -0200 | [diff] [blame] | 246 | src->eip = map->unmap_ip(map, src->eip); |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 247 | } |
| 248 | if (strlen(src->line)>8 && src->line[16] == ':') { |
| 249 | src->eip = strtoull(src->line, NULL, 16); |
Arnaldo Carvalho de Melo | c88e4bf | 2009-10-20 15:54:55 -0200 | [diff] [blame] | 250 | src->eip = map->unmap_ip(map, src->eip); |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | pclose(file); |
| 254 | out_assign: |
| 255 | sym_filter_entry = syme; |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 256 | pthread_mutex_unlock(&source->lock); |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | static void __zero_source_counters(struct sym_entry *syme) |
| 260 | { |
| 261 | int i; |
| 262 | struct source_line *line; |
| 263 | |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 264 | line = syme->src->lines; |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 265 | while (line) { |
| 266 | for (i = 0; i < nr_counters; i++) |
| 267 | line->count[i] = 0; |
| 268 | line = line->next; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | static void record_precise_ip(struct sym_entry *syme, int counter, u64 ip) |
| 273 | { |
| 274 | struct source_line *line; |
| 275 | |
| 276 | if (syme != sym_filter_entry) |
| 277 | return; |
| 278 | |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 279 | if (pthread_mutex_trylock(&syme->src->lock)) |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 280 | return; |
| 281 | |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 282 | if (syme->src == NULL || syme->src->source == NULL) |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 283 | goto out_unlock; |
| 284 | |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 285 | for (line = syme->src->lines; line; line = line->next) { |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 286 | if (line->eip == ip) { |
| 287 | line->count[counter]++; |
| 288 | break; |
| 289 | } |
| 290 | if (line->eip > ip) |
| 291 | break; |
| 292 | } |
| 293 | out_unlock: |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 294 | pthread_mutex_unlock(&syme->src->lock); |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | static void lookup_sym_source(struct sym_entry *syme) |
| 298 | { |
Arnaldo Carvalho de Melo | 51a472d | 2009-11-17 18:38:00 -0200 | [diff] [blame] | 299 | struct symbol *symbol = sym_entry__symbol(syme); |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 300 | struct source_line *line; |
| 301 | char pattern[PATH_MAX]; |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 302 | |
| 303 | sprintf(pattern, "<%s>:", symbol->name); |
| 304 | |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 305 | pthread_mutex_lock(&syme->src->lock); |
| 306 | for (line = syme->src->lines; line; line = line->next) { |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 307 | if (strstr(line->line, pattern)) { |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 308 | syme->src->source = line; |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 309 | break; |
| 310 | } |
| 311 | } |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 312 | pthread_mutex_unlock(&syme->src->lock); |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | static void show_lines(struct source_line *queue, int count, int total) |
| 316 | { |
| 317 | int i; |
| 318 | struct source_line *line; |
| 319 | |
| 320 | line = queue; |
| 321 | for (i = 0; i < count; i++) { |
| 322 | float pcnt = 100.0*(float)line->count[sym_counter]/(float)total; |
| 323 | |
| 324 | printf("%8li %4.1f%%\t%s\n", line->count[sym_counter], pcnt, line->line); |
| 325 | line = line->next; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | #define TRACE_COUNT 3 |
| 330 | |
| 331 | static void show_details(struct sym_entry *syme) |
| 332 | { |
| 333 | struct symbol *symbol; |
| 334 | struct source_line *line; |
| 335 | struct source_line *line_queue = NULL; |
| 336 | int displayed = 0; |
| 337 | int line_queue_count = 0, total = 0, more = 0; |
| 338 | |
| 339 | if (!syme) |
| 340 | return; |
| 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 | lookup_sym_source(syme); |
| 344 | |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 345 | if (!syme->src->source) |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 346 | return; |
| 347 | |
Arnaldo Carvalho de Melo | 51a472d | 2009-11-17 18:38:00 -0200 | [diff] [blame] | 348 | symbol = sym_entry__symbol(syme); |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 349 | printf("Showing %s for %s\n", event_name(sym_counter), symbol->name); |
| 350 | printf(" Events Pcnt (>=%d%%)\n", sym_pcnt_filter); |
| 351 | |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 352 | pthread_mutex_lock(&syme->src->lock); |
| 353 | line = syme->src->source; |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 354 | while (line) { |
| 355 | total += line->count[sym_counter]; |
| 356 | line = line->next; |
| 357 | } |
| 358 | |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 359 | line = syme->src->source; |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 360 | while (line) { |
| 361 | float pcnt = 0.0; |
| 362 | |
| 363 | if (!line_queue_count) |
| 364 | line_queue = line; |
| 365 | line_queue_count++; |
| 366 | |
| 367 | if (line->count[sym_counter]) |
| 368 | pcnt = 100.0 * line->count[sym_counter] / (float)total; |
| 369 | if (pcnt >= (float)sym_pcnt_filter) { |
| 370 | if (displayed <= print_entries) |
| 371 | show_lines(line_queue, line_queue_count, total); |
| 372 | else more++; |
| 373 | displayed += line_queue_count; |
| 374 | line_queue_count = 0; |
| 375 | line_queue = NULL; |
| 376 | } else if (line_queue_count > TRACE_COUNT) { |
| 377 | line_queue = line_queue->next; |
| 378 | line_queue_count--; |
| 379 | } |
| 380 | |
| 381 | line->count[sym_counter] = zero ? 0 : line->count[sym_counter] * 7 / 8; |
| 382 | line = line->next; |
| 383 | } |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 384 | pthread_mutex_unlock(&syme->src->lock); |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 385 | if (more) |
| 386 | printf("%d lines not displayed, maybe increase display entries [e]\n", more); |
| 387 | } |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 388 | |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 389 | /* |
Arnaldo Carvalho de Melo | 5b2bb75 | 2009-10-26 19:23:19 -0200 | [diff] [blame] | 390 | * 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] | 391 | * after decayed. |
| 392 | */ |
| 393 | static LIST_HEAD(active_symbols); |
Arnaldo Carvalho de Melo | c44613a | 2009-05-29 17:03:07 -0300 | [diff] [blame] | 394 | static pthread_mutex_t active_symbols_lock = PTHREAD_MUTEX_INITIALIZER; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 395 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 396 | /* |
| 397 | * Ordering weight: count-1 * count-2 * ... / count-n |
| 398 | */ |
| 399 | static double sym_weight(const struct sym_entry *sym) |
| 400 | { |
Arnaldo Carvalho de Melo | c44613a | 2009-05-29 17:03:07 -0300 | [diff] [blame] | 401 | double weight = sym->snap_count; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 402 | int counter; |
| 403 | |
Mike Galbraith | 46ab976 | 2009-07-24 10:09:50 +0200 | [diff] [blame] | 404 | if (!display_weighted) |
| 405 | return weight; |
| 406 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 407 | for (counter = 1; counter < nr_counters-1; counter++) |
| 408 | weight *= sym->count[counter]; |
| 409 | |
| 410 | weight /= (sym->count[counter] + 1); |
| 411 | |
| 412 | return weight; |
| 413 | } |
| 414 | |
Ingo Molnar | 2debbc8 | 2009-06-05 14:29:10 +0200 | [diff] [blame] | 415 | static long samples; |
| 416 | static long userspace_samples; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 417 | static const char CONSOLE_CLEAR[] = "[H[2J"; |
| 418 | |
Arnaldo Carvalho de Melo | c44613a | 2009-05-29 17:03:07 -0300 | [diff] [blame] | 419 | static void __list_insert_active_sym(struct sym_entry *syme) |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 420 | { |
| 421 | list_add(&syme->node, &active_symbols); |
| 422 | } |
| 423 | |
Arnaldo Carvalho de Melo | c44613a | 2009-05-29 17:03:07 -0300 | [diff] [blame] | 424 | static void list_remove_active_sym(struct sym_entry *syme) |
| 425 | { |
| 426 | pthread_mutex_lock(&active_symbols_lock); |
| 427 | list_del_init(&syme->node); |
| 428 | pthread_mutex_unlock(&active_symbols_lock); |
| 429 | } |
| 430 | |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 431 | static void rb_insert_active_sym(struct rb_root *tree, struct sym_entry *se) |
| 432 | { |
| 433 | struct rb_node **p = &tree->rb_node; |
| 434 | struct rb_node *parent = NULL; |
| 435 | struct sym_entry *iter; |
| 436 | |
| 437 | while (*p != NULL) { |
| 438 | parent = *p; |
| 439 | iter = rb_entry(parent, struct sym_entry, rb_node); |
| 440 | |
Arnaldo Carvalho de Melo | c44613a | 2009-05-29 17:03:07 -0300 | [diff] [blame] | 441 | if (se->weight > iter->weight) |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 442 | p = &(*p)->rb_left; |
| 443 | else |
| 444 | p = &(*p)->rb_right; |
| 445 | } |
| 446 | |
| 447 | rb_link_node(&se->rb_node, parent, p); |
| 448 | rb_insert_color(&se->rb_node, tree); |
| 449 | } |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 450 | |
| 451 | static void print_sym_table(void) |
| 452 | { |
Ingo Molnar | 233f0b9 | 2009-06-03 21:48:40 +0200 | [diff] [blame] | 453 | int printed = 0, j; |
Mike Galbraith | 46ab976 | 2009-07-24 10:09:50 +0200 | [diff] [blame] | 454 | int counter, snap = !display_weighted ? sym_counter : 0; |
Ingo Molnar | 2debbc8 | 2009-06-05 14:29:10 +0200 | [diff] [blame] | 455 | float samples_per_sec = samples/delay_secs; |
| 456 | float ksamples_per_sec = (samples-userspace_samples)/delay_secs; |
| 457 | float sum_ksamples = 0.0; |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 458 | struct sym_entry *syme, *n; |
| 459 | struct rb_root tmp = RB_ROOT; |
| 460 | struct rb_node *nd; |
Arnaldo Carvalho de Melo | 1a105f7 | 2009-11-17 15:40:55 -0200 | [diff] [blame] | 461 | int sym_width = 0, dso_width = 0; |
Arnaldo Carvalho de Melo | 13cc507 | 2009-11-17 15:40:54 -0200 | [diff] [blame] | 462 | const int win_width = winsize.ws_col - 1; |
Arnaldo Carvalho de Melo | 1a105f7 | 2009-11-17 15:40:55 -0200 | [diff] [blame] | 463 | struct dso *unique_dso = NULL, *first_dso = NULL; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 464 | |
Ingo Molnar | 2debbc8 | 2009-06-05 14:29:10 +0200 | [diff] [blame] | 465 | samples = userspace_samples = 0; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 466 | |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 467 | /* Sort the active symbols */ |
Arnaldo Carvalho de Melo | c44613a | 2009-05-29 17:03:07 -0300 | [diff] [blame] | 468 | pthread_mutex_lock(&active_symbols_lock); |
| 469 | syme = list_entry(active_symbols.next, struct sym_entry, node); |
| 470 | pthread_mutex_unlock(&active_symbols_lock); |
| 471 | |
| 472 | list_for_each_entry_safe_from(syme, n, &active_symbols, node) { |
Mike Galbraith | 46ab976 | 2009-07-24 10:09:50 +0200 | [diff] [blame] | 473 | syme->snap_count = syme->count[snap]; |
Arnaldo Carvalho de Melo | c44613a | 2009-05-29 17:03:07 -0300 | [diff] [blame] | 474 | if (syme->snap_count != 0) { |
Arnaldo Carvalho de Melo | 13cc507 | 2009-11-17 15:40:54 -0200 | [diff] [blame] | 475 | |
Arnaldo Carvalho de Melo | 8ffcda1 | 2009-11-16 21:45:24 -0200 | [diff] [blame] | 476 | if ((hide_user_symbols && |
| 477 | syme->origin == PERF_RECORD_MISC_USER) || |
| 478 | (hide_kernel_symbols && |
| 479 | syme->origin == PERF_RECORD_MISC_KERNEL)) { |
| 480 | list_remove_active_sym(syme); |
| 481 | continue; |
| 482 | } |
Arnaldo Carvalho de Melo | c44613a | 2009-05-29 17:03:07 -0300 | [diff] [blame] | 483 | syme->weight = sym_weight(syme); |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 484 | rb_insert_active_sym(&tmp, syme); |
Ingo Molnar | 2debbc8 | 2009-06-05 14:29:10 +0200 | [diff] [blame] | 485 | sum_ksamples += syme->snap_count; |
Mike Galbraith | d94b943 | 2009-05-25 09:57:56 +0200 | [diff] [blame] | 486 | |
| 487 | for (j = 0; j < nr_counters; j++) |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 488 | syme->count[j] = zero ? 0 : syme->count[j] * 7 / 8; |
| 489 | } else |
Arnaldo Carvalho de Melo | c44613a | 2009-05-29 17:03:07 -0300 | [diff] [blame] | 490 | list_remove_active_sym(syme); |
Mike Galbraith | d94b943 | 2009-05-25 09:57:56 +0200 | [diff] [blame] | 491 | } |
| 492 | |
Frederic Weisbecker | 0f5486b | 2009-06-04 20:48:04 +0200 | [diff] [blame] | 493 | puts(CONSOLE_CLEAR); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 494 | |
Arnaldo Carvalho de Melo | 13cc507 | 2009-11-17 15:40:54 -0200 | [diff] [blame] | 495 | printf("%-*.*s\n", win_width, win_width, graph_dotted_line); |
Ingo Molnar | f2521b6 | 2009-06-03 19:17:25 +0200 | [diff] [blame] | 496 | printf( " PerfTop:%8.0f irqs/sec kernel:%4.1f%% [", |
Ingo Molnar | 2debbc8 | 2009-06-05 14:29:10 +0200 | [diff] [blame] | 497 | samples_per_sec, |
| 498 | 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] | 499 | |
Mike Galbraith | 46ab976 | 2009-07-24 10:09:50 +0200 | [diff] [blame] | 500 | if (nr_counters == 1 || !display_weighted) { |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 501 | printf("%Ld", (u64)attrs[0].sample_period); |
Ingo Molnar | cf1f457 | 2009-06-05 13:27:02 +0200 | [diff] [blame] | 502 | if (freq) |
| 503 | printf("Hz "); |
| 504 | else |
| 505 | printf(" "); |
| 506 | } |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 507 | |
Mike Galbraith | 46ab976 | 2009-07-24 10:09:50 +0200 | [diff] [blame] | 508 | if (!display_weighted) |
| 509 | printf("%s", event_name(sym_counter)); |
| 510 | else for (counter = 0; counter < nr_counters; counter++) { |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 511 | if (counter) |
| 512 | printf("/"); |
| 513 | |
| 514 | printf("%s", event_name(counter)); |
| 515 | } |
| 516 | |
| 517 | printf( "], "); |
| 518 | |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 519 | if (target_pid != -1) |
| 520 | printf(" (target_pid: %d", target_pid); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 521 | else |
| 522 | printf(" (all"); |
| 523 | |
| 524 | if (profile_cpu != -1) |
| 525 | printf(", cpu: %d)\n", profile_cpu); |
| 526 | else { |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 527 | if (target_pid != -1) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 528 | printf(")\n"); |
| 529 | else |
| 530 | printf(", %d CPUs)\n", nr_cpus); |
| 531 | } |
| 532 | |
Arnaldo Carvalho de Melo | 1a105f7 | 2009-11-17 15:40:55 -0200 | [diff] [blame] | 533 | printf("%-*.*s\n", win_width, win_width, graph_dotted_line); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 534 | |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 535 | if (sym_filter_entry) { |
| 536 | show_details(sym_filter_entry); |
| 537 | return; |
| 538 | } |
| 539 | |
Arnaldo Carvalho de Melo | 13cc507 | 2009-11-17 15:40:54 -0200 | [diff] [blame] | 540 | /* |
| 541 | * Find the longest symbol name that will be displayed |
| 542 | */ |
| 543 | for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) { |
| 544 | syme = rb_entry(nd, struct sym_entry, rb_node); |
| 545 | if (++printed > print_entries || |
| 546 | (int)syme->snap_count < count_filter) |
| 547 | continue; |
| 548 | |
Arnaldo Carvalho de Melo | 1a105f7 | 2009-11-17 15:40:55 -0200 | [diff] [blame] | 549 | if (first_dso == NULL) |
| 550 | unique_dso = first_dso = syme->map->dso; |
| 551 | else if (syme->map->dso != first_dso) |
| 552 | unique_dso = NULL; |
| 553 | |
| 554 | if (syme->map->dso->long_name_len > dso_width) |
| 555 | dso_width = syme->map->dso->long_name_len; |
| 556 | |
Arnaldo Carvalho de Melo | 13cc507 | 2009-11-17 15:40:54 -0200 | [diff] [blame] | 557 | if (syme->name_len > sym_width) |
| 558 | sym_width = syme->name_len; |
| 559 | } |
| 560 | |
| 561 | printed = 0; |
| 562 | |
Arnaldo Carvalho de Melo | 1a105f7 | 2009-11-17 15:40:55 -0200 | [diff] [blame] | 563 | if (unique_dso) |
| 564 | printf("DSO: %s\n", unique_dso->long_name); |
| 565 | else { |
| 566 | int max_dso_width = winsize.ws_col - sym_width - 29; |
| 567 | if (dso_width > max_dso_width) |
| 568 | dso_width = max_dso_width; |
| 569 | putchar('\n'); |
| 570 | } |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 571 | if (nr_counters == 1) |
Arnaldo Carvalho de Melo | 5b2bb75 | 2009-10-26 19:23:19 -0200 | [diff] [blame] | 572 | printf(" samples pcnt"); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 573 | else |
Arnaldo Carvalho de Melo | 5b2bb75 | 2009-10-26 19:23:19 -0200 | [diff] [blame] | 574 | printf(" weight samples pcnt"); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 575 | |
Arnaldo Carvalho de Melo | 7ced156 | 2009-08-26 11:51:26 -0300 | [diff] [blame] | 576 | if (verbose) |
| 577 | printf(" RIP "); |
Arnaldo Carvalho de Melo | 1a105f7 | 2009-11-17 15:40:55 -0200 | [diff] [blame] | 578 | printf(" %-*.*s", sym_width, sym_width, "function"); |
| 579 | if (!unique_dso) |
| 580 | printf(" DSO"); |
| 581 | putchar('\n'); |
Arnaldo Carvalho de Melo | 5b2bb75 | 2009-10-26 19:23:19 -0200 | [diff] [blame] | 582 | printf(" %s _______ _____", |
Arnaldo Carvalho de Melo | 7ced156 | 2009-08-26 11:51:26 -0300 | [diff] [blame] | 583 | nr_counters == 1 ? " " : "______"); |
| 584 | if (verbose) |
Arnaldo Carvalho de Melo | 5b2bb75 | 2009-10-26 19:23:19 -0200 | [diff] [blame] | 585 | printf(" ________________"); |
Arnaldo Carvalho de Melo | 1a105f7 | 2009-11-17 15:40:55 -0200 | [diff] [blame] | 586 | printf(" %-*.*s", sym_width, sym_width, graph_line); |
| 587 | if (!unique_dso) |
| 588 | printf(" %-*.*s", dso_width, dso_width, graph_line); |
| 589 | puts("\n"); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 590 | |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 591 | for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) { |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 592 | struct symbol *sym; |
Ingo Molnar | 8fc0321 | 2009-06-04 15:19:47 +0200 | [diff] [blame] | 593 | double pcnt; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 594 | |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 595 | syme = rb_entry(nd, struct sym_entry, rb_node); |
Arnaldo Carvalho de Melo | 51a472d | 2009-11-17 18:38:00 -0200 | [diff] [blame] | 596 | sym = sym_entry__symbol(syme); |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 597 | |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 598 | if (++printed > print_entries || (int)syme->snap_count < count_filter) |
Arnaldo Carvalho de Melo | c44613a | 2009-05-29 17:03:07 -0300 | [diff] [blame] | 599 | continue; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 600 | |
Ingo Molnar | 2debbc8 | 2009-06-05 14:29:10 +0200 | [diff] [blame] | 601 | pcnt = 100.0 - (100.0 * ((sum_ksamples - syme->snap_count) / |
| 602 | sum_ksamples)); |
Mike Galbraith | d94b943 | 2009-05-25 09:57:56 +0200 | [diff] [blame] | 603 | |
Mike Galbraith | 46ab976 | 2009-07-24 10:09:50 +0200 | [diff] [blame] | 604 | if (nr_counters == 1 || !display_weighted) |
Arnaldo Carvalho de Melo | 5b2bb75 | 2009-10-26 19:23:19 -0200 | [diff] [blame] | 605 | printf("%20.2f ", syme->weight); |
Mike Galbraith | d94b943 | 2009-05-25 09:57:56 +0200 | [diff] [blame] | 606 | else |
Arnaldo Carvalho de Melo | 5b2bb75 | 2009-10-26 19:23:19 -0200 | [diff] [blame] | 607 | printf("%9.1f %10ld ", syme->weight, syme->snap_count); |
Ingo Molnar | 8fc0321 | 2009-06-04 15:19:47 +0200 | [diff] [blame] | 608 | |
Frederic Weisbecker | 1e11fd8 | 2009-07-02 20:14:34 +0200 | [diff] [blame] | 609 | percent_color_fprintf(stdout, "%4.1f%%", pcnt); |
Arnaldo Carvalho de Melo | 7ced156 | 2009-08-26 11:51:26 -0300 | [diff] [blame] | 610 | if (verbose) |
Arnaldo Carvalho de Melo | 5b2bb75 | 2009-10-26 19:23:19 -0200 | [diff] [blame] | 611 | printf(" %016llx", sym->start); |
Arnaldo Carvalho de Melo | 13cc507 | 2009-11-17 15:40:54 -0200 | [diff] [blame] | 612 | printf(" %-*.*s", sym_width, sym_width, sym->name); |
Arnaldo Carvalho de Melo | 1a105f7 | 2009-11-17 15:40:55 -0200 | [diff] [blame] | 613 | if (!unique_dso) |
| 614 | printf(" %-*.*s", dso_width, dso_width, |
| 615 | dso_width >= syme->map->dso->long_name_len ? |
| 616 | syme->map->dso->long_name : |
| 617 | syme->map->dso->short_name); |
Mike Galbraith | 4297648 | 2009-07-02 08:09:46 +0200 | [diff] [blame] | 618 | printf("\n"); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 619 | } |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 620 | } |
| 621 | |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 622 | static void prompt_integer(int *target, const char *msg) |
| 623 | { |
| 624 | char *buf = malloc(0), *p; |
| 625 | size_t dummy = 0; |
| 626 | int tmp; |
| 627 | |
| 628 | fprintf(stdout, "\n%s: ", msg); |
| 629 | if (getline(&buf, &dummy, stdin) < 0) |
| 630 | return; |
| 631 | |
| 632 | p = strchr(buf, '\n'); |
| 633 | if (p) |
| 634 | *p = 0; |
| 635 | |
| 636 | p = buf; |
| 637 | while(*p) { |
| 638 | if (!isdigit(*p)) |
| 639 | goto out_free; |
| 640 | p++; |
| 641 | } |
| 642 | tmp = strtoul(buf, NULL, 10); |
| 643 | *target = tmp; |
| 644 | out_free: |
| 645 | free(buf); |
| 646 | } |
| 647 | |
| 648 | static void prompt_percent(int *target, const char *msg) |
| 649 | { |
| 650 | int tmp = 0; |
| 651 | |
| 652 | prompt_integer(&tmp, msg); |
| 653 | if (tmp >= 0 && tmp <= 100) |
| 654 | *target = tmp; |
| 655 | } |
| 656 | |
| 657 | static void prompt_symbol(struct sym_entry **target, const char *msg) |
| 658 | { |
| 659 | char *buf = malloc(0), *p; |
| 660 | struct sym_entry *syme = *target, *n, *found = NULL; |
| 661 | size_t dummy = 0; |
| 662 | |
| 663 | /* zero counters of active symbol */ |
| 664 | if (syme) { |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 665 | pthread_mutex_lock(&syme->src->lock); |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 666 | __zero_source_counters(syme); |
| 667 | *target = NULL; |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 668 | pthread_mutex_unlock(&syme->src->lock); |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | fprintf(stdout, "\n%s: ", msg); |
| 672 | if (getline(&buf, &dummy, stdin) < 0) |
| 673 | goto out_free; |
| 674 | |
| 675 | p = strchr(buf, '\n'); |
| 676 | if (p) |
| 677 | *p = 0; |
| 678 | |
| 679 | pthread_mutex_lock(&active_symbols_lock); |
| 680 | syme = list_entry(active_symbols.next, struct sym_entry, node); |
| 681 | pthread_mutex_unlock(&active_symbols_lock); |
| 682 | |
| 683 | 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] | 684 | struct symbol *sym = sym_entry__symbol(syme); |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 685 | |
| 686 | if (!strcmp(buf, sym->name)) { |
| 687 | found = syme; |
| 688 | break; |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | if (!found) { |
| 693 | fprintf(stderr, "Sorry, %s is not active.\n", sym_filter); |
| 694 | sleep(1); |
| 695 | return; |
| 696 | } else |
| 697 | parse_source(found); |
| 698 | |
| 699 | out_free: |
| 700 | free(buf); |
| 701 | } |
| 702 | |
Mike Galbraith | 091bd2e | 2009-08-04 10:21:23 +0200 | [diff] [blame] | 703 | static void print_mapped_keys(void) |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 704 | { |
Mike Galbraith | 091bd2e | 2009-08-04 10:21:23 +0200 | [diff] [blame] | 705 | char *name = NULL; |
| 706 | |
| 707 | if (sym_filter_entry) { |
Arnaldo Carvalho de Melo | 51a472d | 2009-11-17 18:38:00 -0200 | [diff] [blame] | 708 | struct symbol *sym = sym_entry__symbol(sym_filter_entry); |
Mike Galbraith | 091bd2e | 2009-08-04 10:21:23 +0200 | [diff] [blame] | 709 | name = sym->name; |
| 710 | } |
| 711 | |
| 712 | fprintf(stdout, "\nMapped keys:\n"); |
| 713 | fprintf(stdout, "\t[d] display refresh delay. \t(%d)\n", delay_secs); |
| 714 | fprintf(stdout, "\t[e] display entries (lines). \t(%d)\n", print_entries); |
| 715 | |
| 716 | if (nr_counters > 1) |
| 717 | fprintf(stdout, "\t[E] active event counter. \t(%s)\n", event_name(sym_counter)); |
| 718 | |
| 719 | fprintf(stdout, "\t[f] profile display filter (count). \t(%d)\n", count_filter); |
| 720 | |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 721 | if (vmlinux_name) { |
Mike Galbraith | 091bd2e | 2009-08-04 10:21:23 +0200 | [diff] [blame] | 722 | fprintf(stdout, "\t[F] annotate display filter (percent). \t(%d%%)\n", sym_pcnt_filter); |
| 723 | fprintf(stdout, "\t[s] annotate symbol. \t(%s)\n", name?: "NULL"); |
| 724 | fprintf(stdout, "\t[S] stop annotation.\n"); |
| 725 | } |
| 726 | |
| 727 | if (nr_counters > 1) |
| 728 | fprintf(stdout, "\t[w] toggle display weighted/count[E]r. \t(%d)\n", display_weighted ? 1 : 0); |
| 729 | |
Arnaldo Carvalho de Melo | 8ffcda1 | 2009-11-16 21:45:24 -0200 | [diff] [blame] | 730 | fprintf(stdout, |
| 731 | "\t[K] hide kernel_symbols symbols. \t(%s)\n", |
| 732 | hide_kernel_symbols ? "yes" : "no"); |
| 733 | fprintf(stdout, |
| 734 | "\t[U] hide user symbols. \t(%s)\n", |
| 735 | hide_user_symbols ? "yes" : "no"); |
Mike Galbraith | 46ab976 | 2009-07-24 10:09:50 +0200 | [diff] [blame] | 736 | 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] | 737 | fprintf(stdout, "\t[qQ] quit.\n"); |
| 738 | } |
| 739 | |
| 740 | static int key_mapped(int c) |
| 741 | { |
| 742 | switch (c) { |
| 743 | case 'd': |
| 744 | case 'e': |
| 745 | case 'f': |
| 746 | case 'z': |
| 747 | case 'q': |
| 748 | case 'Q': |
Arnaldo Carvalho de Melo | 8ffcda1 | 2009-11-16 21:45:24 -0200 | [diff] [blame] | 749 | case 'K': |
| 750 | case 'U': |
Mike Galbraith | 091bd2e | 2009-08-04 10:21:23 +0200 | [diff] [blame] | 751 | return 1; |
| 752 | case 'E': |
| 753 | case 'w': |
| 754 | return nr_counters > 1 ? 1 : 0; |
| 755 | case 'F': |
| 756 | case 's': |
| 757 | case 'S': |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 758 | return vmlinux_name ? 1 : 0; |
| 759 | default: |
| 760 | break; |
Mike Galbraith | 091bd2e | 2009-08-04 10:21:23 +0200 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | return 0; |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | static void handle_keypress(int c) |
| 767 | { |
Mike Galbraith | 091bd2e | 2009-08-04 10:21:23 +0200 | [diff] [blame] | 768 | if (!key_mapped(c)) { |
| 769 | struct pollfd stdin_poll = { .fd = 0, .events = POLLIN }; |
| 770 | struct termios tc, save; |
| 771 | |
| 772 | print_mapped_keys(); |
| 773 | fprintf(stdout, "\nEnter selection, or unmapped key to continue: "); |
| 774 | fflush(stdout); |
| 775 | |
| 776 | tcgetattr(0, &save); |
| 777 | tc = save; |
| 778 | tc.c_lflag &= ~(ICANON | ECHO); |
| 779 | tc.c_cc[VMIN] = 0; |
| 780 | tc.c_cc[VTIME] = 0; |
| 781 | tcsetattr(0, TCSANOW, &tc); |
| 782 | |
| 783 | poll(&stdin_poll, 1, -1); |
| 784 | c = getc(stdin); |
| 785 | |
| 786 | tcsetattr(0, TCSAFLUSH, &save); |
| 787 | if (!key_mapped(c)) |
| 788 | return; |
| 789 | } |
| 790 | |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 791 | switch (c) { |
| 792 | case 'd': |
| 793 | prompt_integer(&delay_secs, "Enter display delay"); |
Tim Blechmann | dc79959 | 2009-10-17 18:08:29 +0200 | [diff] [blame] | 794 | if (delay_secs < 1) |
| 795 | delay_secs = 1; |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 796 | break; |
| 797 | case 'e': |
| 798 | prompt_integer(&print_entries, "Enter display entries (lines)"); |
Arnaldo Carvalho de Melo | 3b6ed98 | 2009-11-16 19:30:27 -0200 | [diff] [blame] | 799 | if (print_entries == 0) { |
Arnaldo Carvalho de Melo | 13cc507 | 2009-11-17 15:40:54 -0200 | [diff] [blame] | 800 | sig_winch_handler(SIGWINCH); |
Arnaldo Carvalho de Melo | 3b6ed98 | 2009-11-16 19:30:27 -0200 | [diff] [blame] | 801 | signal(SIGWINCH, sig_winch_handler); |
| 802 | } else |
| 803 | signal(SIGWINCH, SIG_DFL); |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 804 | break; |
| 805 | case 'E': |
| 806 | if (nr_counters > 1) { |
| 807 | int i; |
| 808 | |
| 809 | fprintf(stderr, "\nAvailable events:"); |
| 810 | for (i = 0; i < nr_counters; i++) |
| 811 | fprintf(stderr, "\n\t%d %s", i, event_name(i)); |
| 812 | |
| 813 | prompt_integer(&sym_counter, "Enter details event counter"); |
| 814 | |
| 815 | if (sym_counter >= nr_counters) { |
| 816 | fprintf(stderr, "Sorry, no such event, using %s.\n", event_name(0)); |
| 817 | sym_counter = 0; |
| 818 | sleep(1); |
| 819 | } |
| 820 | } else sym_counter = 0; |
| 821 | break; |
| 822 | case 'f': |
| 823 | prompt_integer(&count_filter, "Enter display event count filter"); |
| 824 | break; |
| 825 | case 'F': |
| 826 | prompt_percent(&sym_pcnt_filter, "Enter details display event filter (percent)"); |
| 827 | break; |
Arnaldo Carvalho de Melo | 8ffcda1 | 2009-11-16 21:45:24 -0200 | [diff] [blame] | 828 | case 'K': |
| 829 | hide_kernel_symbols = !hide_kernel_symbols; |
| 830 | break; |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 831 | case 'q': |
| 832 | case 'Q': |
| 833 | printf("exiting.\n"); |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 834 | if (dump_symtab) |
| 835 | dsos__fprintf(stderr); |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 836 | exit(0); |
| 837 | case 's': |
| 838 | prompt_symbol(&sym_filter_entry, "Enter details symbol"); |
| 839 | break; |
| 840 | case 'S': |
| 841 | if (!sym_filter_entry) |
| 842 | break; |
| 843 | else { |
| 844 | struct sym_entry *syme = sym_filter_entry; |
| 845 | |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 846 | pthread_mutex_lock(&syme->src->lock); |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 847 | sym_filter_entry = NULL; |
| 848 | __zero_source_counters(syme); |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 849 | pthread_mutex_unlock(&syme->src->lock); |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 850 | } |
| 851 | break; |
Arnaldo Carvalho de Melo | 8ffcda1 | 2009-11-16 21:45:24 -0200 | [diff] [blame] | 852 | case 'U': |
| 853 | hide_user_symbols = !hide_user_symbols; |
| 854 | break; |
Mike Galbraith | 46ab976 | 2009-07-24 10:09:50 +0200 | [diff] [blame] | 855 | case 'w': |
| 856 | display_weighted = ~display_weighted; |
| 857 | break; |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 858 | case 'z': |
| 859 | zero = ~zero; |
| 860 | break; |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 861 | default: |
| 862 | break; |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 863 | } |
| 864 | } |
| 865 | |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 866 | static void *display_thread(void *arg __used) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 867 | { |
Frederic Weisbecker | 0f5486b | 2009-06-04 20:48:04 +0200 | [diff] [blame] | 868 | struct pollfd stdin_poll = { .fd = 0, .events = POLLIN }; |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 869 | struct termios tc, save; |
| 870 | int delay_msecs, c; |
Frederic Weisbecker | 0f5486b | 2009-06-04 20:48:04 +0200 | [diff] [blame] | 871 | |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 872 | tcgetattr(0, &save); |
| 873 | tc = save; |
| 874 | tc.c_lflag &= ~(ICANON | ECHO); |
| 875 | tc.c_cc[VMIN] = 0; |
| 876 | tc.c_cc[VTIME] = 0; |
Mike Galbraith | 091bd2e | 2009-08-04 10:21:23 +0200 | [diff] [blame] | 877 | |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 878 | repeat: |
| 879 | delay_msecs = delay_secs * 1000; |
| 880 | tcsetattr(0, TCSANOW, &tc); |
| 881 | /* trash return*/ |
| 882 | getc(stdin); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 883 | |
Frederic Weisbecker | 0f5486b | 2009-06-04 20:48:04 +0200 | [diff] [blame] | 884 | do { |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 885 | print_sym_table(); |
Frederic Weisbecker | 0f5486b | 2009-06-04 20:48:04 +0200 | [diff] [blame] | 886 | } while (!poll(&stdin_poll, 1, delay_msecs) == 1); |
| 887 | |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 888 | c = getc(stdin); |
| 889 | tcsetattr(0, TCSAFLUSH, &save); |
| 890 | |
| 891 | handle_keypress(c); |
| 892 | goto repeat; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 893 | |
| 894 | return NULL; |
| 895 | } |
| 896 | |
Anton Blanchard | 2ab5208 | 2009-07-01 09:00:46 +1000 | [diff] [blame] | 897 | /* Tag samples to be skipped. */ |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 898 | static const char *skip_symbols[] = { |
Anton Blanchard | 2ab5208 | 2009-07-01 09:00:46 +1000 | [diff] [blame] | 899 | "default_idle", |
| 900 | "cpu_idle", |
| 901 | "enter_idle", |
| 902 | "exit_idle", |
| 903 | "mwait_idle", |
Arnaldo Carvalho de Melo | 59b9005 | 2009-07-26 19:06:19 -0300 | [diff] [blame] | 904 | "mwait_idle_with_hints", |
Arnaldo Carvalho de Melo | 8357275 | 2009-09-25 15:02:39 -0700 | [diff] [blame] | 905 | "poll_idle", |
Anton Blanchard | 3a3393e | 2009-07-01 09:00:47 +1000 | [diff] [blame] | 906 | "ppc64_runlatch_off", |
| 907 | "pseries_dedicated_idle_sleep", |
Anton Blanchard | 2ab5208 | 2009-07-01 09:00:46 +1000 | [diff] [blame] | 908 | NULL |
| 909 | }; |
| 910 | |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 911 | static int symbol_filter(struct map *map, struct symbol *sym) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 912 | { |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 913 | struct sym_entry *syme; |
| 914 | const char *name = sym->name; |
Anton Blanchard | 2ab5208 | 2009-07-01 09:00:46 +1000 | [diff] [blame] | 915 | int i; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 916 | |
Anton Blanchard | 3a3393e | 2009-07-01 09:00:47 +1000 | [diff] [blame] | 917 | /* |
| 918 | * ppc64 uses function descriptors and appends a '.' to the |
| 919 | * start of every instruction address. Remove it. |
| 920 | */ |
| 921 | if (name[0] == '.') |
| 922 | name++; |
| 923 | |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 924 | if (!strcmp(name, "_text") || |
| 925 | !strcmp(name, "_etext") || |
| 926 | !strcmp(name, "_sinittext") || |
| 927 | !strncmp("init_module", name, 11) || |
| 928 | !strncmp("cleanup_module", name, 14) || |
| 929 | strstr(name, "_text_start") || |
| 930 | strstr(name, "_text_end")) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 931 | return 1; |
| 932 | |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 933 | syme = symbol__priv(sym); |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 934 | syme->map = map; |
Arnaldo Carvalho de Melo | b269876 | 2009-11-17 18:38:02 -0200 | [diff] [blame] | 935 | syme->src = NULL; |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 936 | if (!sym_filter_entry && sym_filter && !strcmp(name, sym_filter)) |
| 937 | sym_filter_entry = syme; |
| 938 | |
Anton Blanchard | 2ab5208 | 2009-07-01 09:00:46 +1000 | [diff] [blame] | 939 | for (i = 0; skip_symbols[i]; i++) { |
| 940 | if (!strcmp(skip_symbols[i], name)) { |
| 941 | syme->skip = 1; |
| 942 | break; |
| 943 | } |
| 944 | } |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 945 | |
Arnaldo Carvalho de Melo | 13cc507 | 2009-11-17 15:40:54 -0200 | [diff] [blame] | 946 | if (!syme->skip) |
| 947 | syme->name_len = strlen(sym->name); |
| 948 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 949 | return 0; |
| 950 | } |
| 951 | |
Arnaldo Carvalho de Melo | 5b2bb75 | 2009-10-26 19:23:19 -0200 | [diff] [blame] | 952 | static void event__process_sample(const event_t *self, int counter) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 953 | { |
Arnaldo Carvalho de Melo | 5b2bb75 | 2009-10-26 19:23:19 -0200 | [diff] [blame] | 954 | u64 ip = self->ip.ip; |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 955 | struct map *map; |
Arnaldo Carvalho de Melo | 5b2bb75 | 2009-10-26 19:23:19 -0200 | [diff] [blame] | 956 | struct sym_entry *syme; |
| 957 | struct symbol *sym; |
Arnaldo Carvalho de Melo | 8ffcda1 | 2009-11-16 21:45:24 -0200 | [diff] [blame] | 958 | u8 origin = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 959 | |
Arnaldo Carvalho de Melo | 8ffcda1 | 2009-11-16 21:45:24 -0200 | [diff] [blame] | 960 | switch (origin) { |
Arnaldo Carvalho de Melo | 5b2bb75 | 2009-10-26 19:23:19 -0200 | [diff] [blame] | 961 | case PERF_RECORD_MISC_USER: { |
Arnaldo Carvalho de Melo | 8ffcda1 | 2009-11-16 21:45:24 -0200 | [diff] [blame] | 962 | struct thread *thread; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 963 | |
Arnaldo Carvalho de Melo | 8ffcda1 | 2009-11-16 21:45:24 -0200 | [diff] [blame] | 964 | if (hide_user_symbols) |
| 965 | return; |
| 966 | |
| 967 | thread = threads__findnew(self->ip.pid); |
Arnaldo Carvalho de Melo | 5b2bb75 | 2009-10-26 19:23:19 -0200 | [diff] [blame] | 968 | if (thread == NULL) |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 969 | return; |
Arnaldo Carvalho de Melo | 5b2bb75 | 2009-10-26 19:23:19 -0200 | [diff] [blame] | 970 | |
| 971 | map = thread__find_map(thread, ip); |
| 972 | if (map != NULL) { |
| 973 | ip = map->map_ip(map, ip); |
Arnaldo Carvalho de Melo | 66bd842 | 2009-10-28 21:51:21 -0200 | [diff] [blame] | 974 | sym = map__find_symbol(map, ip, symbol_filter); |
Arnaldo Carvalho de Melo | 5b2bb75 | 2009-10-26 19:23:19 -0200 | [diff] [blame] | 975 | if (sym == NULL) |
| 976 | return; |
| 977 | userspace_samples++; |
| 978 | break; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 979 | } |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 980 | } |
Arnaldo Carvalho de Melo | 5b2bb75 | 2009-10-26 19:23:19 -0200 | [diff] [blame] | 981 | /* |
| 982 | * If this is outside of all known maps, |
| 983 | * and is a negative address, try to look it |
| 984 | * up in the kernel dso, as it might be a |
| 985 | * vsyscall or vdso (which executes in user-mode). |
| 986 | */ |
| 987 | if ((long long)ip >= 0) |
| 988 | return; |
| 989 | /* Fall thru */ |
| 990 | case PERF_RECORD_MISC_KERNEL: |
Arnaldo Carvalho de Melo | 8ffcda1 | 2009-11-16 21:45:24 -0200 | [diff] [blame] | 991 | if (hide_kernel_symbols) |
| 992 | return; |
| 993 | |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 994 | sym = kernel_maps__find_symbol(ip, &map, symbol_filter); |
Arnaldo Carvalho de Melo | 5b2bb75 | 2009-10-26 19:23:19 -0200 | [diff] [blame] | 995 | if (sym == NULL) |
| 996 | return; |
| 997 | break; |
| 998 | default: |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 999 | return; |
| 1000 | } |
| 1001 | |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 1002 | syme = symbol__priv(sym); |
Arnaldo Carvalho de Melo | 5b2bb75 | 2009-10-26 19:23:19 -0200 | [diff] [blame] | 1003 | |
| 1004 | if (!syme->skip) { |
| 1005 | syme->count[counter]++; |
Arnaldo Carvalho de Melo | 8ffcda1 | 2009-11-16 21:45:24 -0200 | [diff] [blame] | 1006 | syme->origin = origin; |
Arnaldo Carvalho de Melo | 5b2bb75 | 2009-10-26 19:23:19 -0200 | [diff] [blame] | 1007 | record_precise_ip(syme, counter, ip); |
| 1008 | pthread_mutex_lock(&active_symbols_lock); |
| 1009 | if (list_empty(&syme->node) || !syme->node.next) |
| 1010 | __list_insert_active_sym(syme); |
| 1011 | pthread_mutex_unlock(&active_symbols_lock); |
| 1012 | ++samples; |
| 1013 | return; |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | static void event__process_mmap(event_t *self) |
| 1018 | { |
| 1019 | struct thread *thread = threads__findnew(self->mmap.pid); |
| 1020 | |
| 1021 | if (thread != NULL) { |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 1022 | struct map *map = map__new(&self->mmap, NULL, 0); |
Arnaldo Carvalho de Melo | 5b2bb75 | 2009-10-26 19:23:19 -0200 | [diff] [blame] | 1023 | if (map != NULL) |
| 1024 | thread__insert_map(thread, map); |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | static void event__process_comm(event_t *self) |
| 1029 | { |
| 1030 | struct thread *thread = threads__findnew(self->comm.pid); |
| 1031 | |
| 1032 | if (thread != NULL) |
| 1033 | thread__set_comm(thread, self->comm.comm); |
| 1034 | } |
| 1035 | |
| 1036 | static int event__process(event_t *event) |
| 1037 | { |
| 1038 | switch (event->header.type) { |
| 1039 | case PERF_RECORD_COMM: |
| 1040 | event__process_comm(event); |
| 1041 | break; |
| 1042 | case PERF_RECORD_MMAP: |
| 1043 | event__process_mmap(event); |
| 1044 | break; |
| 1045 | default: |
| 1046 | break; |
| 1047 | } |
| 1048 | |
| 1049 | return 0; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 1050 | } |
| 1051 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 1052 | struct mmap_data { |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 1053 | int counter; |
| 1054 | void *base; |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 1055 | int mask; |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 1056 | unsigned int prev; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 1057 | }; |
| 1058 | |
| 1059 | static unsigned int mmap_read_head(struct mmap_data *md) |
| 1060 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 1061 | struct perf_event_mmap_page *pc = md->base; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 1062 | int head; |
| 1063 | |
| 1064 | head = pc->data_head; |
| 1065 | rmb(); |
| 1066 | |
| 1067 | return head; |
| 1068 | } |
| 1069 | |
Frederic Weisbecker | 2f01190 | 2009-06-06 23:10:43 +0200 | [diff] [blame] | 1070 | static void mmap_read_counter(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) |
| 1123 | event__process_sample(event, md->counter); |
| 1124 | else |
| 1125 | event__process(event); |
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 | |
Frederic Weisbecker | 2f01190 | 2009-06-06 23:10:43 +0200 | [diff] [blame] | 1135 | static void mmap_read(void) |
| 1136 | { |
| 1137 | int i, counter; |
| 1138 | |
| 1139 | for (i = 0; i < nr_cpus; i++) { |
| 1140 | for (counter = 0; counter < nr_counters; counter++) |
| 1141 | mmap_read_counter(&mmap_array[i][counter]); |
| 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) |
| 1155 | cpu = i; |
| 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; |
| 1226 | |
Arnaldo Carvalho de Melo | 5b2bb75 | 2009-10-26 19:23:19 -0200 | [diff] [blame] | 1227 | if (target_pid != -1) |
| 1228 | event__synthesize_thread(target_pid, event__process); |
| 1229 | else |
| 1230 | event__synthesize_threads(event__process); |
| 1231 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 1232 | for (i = 0; i < nr_cpus; i++) { |
| 1233 | group_fd = -1; |
Ingo Molnar | 716c69f | 2009-06-07 17:31:52 +0200 | [diff] [blame] | 1234 | for (counter = 0; counter < nr_counters; counter++) |
| 1235 | start_counter(i, counter); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 1236 | } |
| 1237 | |
Frederic Weisbecker | 2f01190 | 2009-06-06 23:10:43 +0200 | [diff] [blame] | 1238 | /* Wait for a minimal set of events before starting the snapshot */ |
| 1239 | poll(event_array, nr_poll, 100); |
| 1240 | |
| 1241 | mmap_read(); |
| 1242 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 1243 | if (pthread_create(&thread, NULL, display_thread, NULL)) { |
| 1244 | printf("Could not create display thread.\n"); |
| 1245 | exit(-1); |
| 1246 | } |
| 1247 | |
| 1248 | if (realtime_prio) { |
| 1249 | struct sched_param param; |
| 1250 | |
| 1251 | param.sched_priority = realtime_prio; |
| 1252 | if (sched_setscheduler(0, SCHED_FIFO, ¶m)) { |
| 1253 | printf("Could not set realtime priority.\n"); |
| 1254 | exit(-1); |
| 1255 | } |
| 1256 | } |
| 1257 | |
| 1258 | while (1) { |
Ingo Molnar | 2debbc8 | 2009-06-05 14:29:10 +0200 | [diff] [blame] | 1259 | int hits = samples; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 1260 | |
Frederic Weisbecker | 2f01190 | 2009-06-06 23:10:43 +0200 | [diff] [blame] | 1261 | mmap_read(); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 1262 | |
Ingo Molnar | 2debbc8 | 2009-06-05 14:29:10 +0200 | [diff] [blame] | 1263 | if (hits == samples) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 1264 | ret = poll(event_array, nr_poll, 100); |
| 1265 | } |
| 1266 | |
| 1267 | return 0; |
| 1268 | } |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1269 | |
| 1270 | static const char * const top_usage[] = { |
| 1271 | "perf top [<options>]", |
| 1272 | NULL |
| 1273 | }; |
| 1274 | |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1275 | static const struct option options[] = { |
| 1276 | OPT_CALLBACK('e', "event", NULL, "event", |
Thomas Gleixner | 86847b6 | 2009-06-06 12:24:17 +0200 | [diff] [blame] | 1277 | "event selector. use 'perf list' to list available events", |
| 1278 | parse_events), |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1279 | OPT_INTEGER('c', "count", &default_interval, |
| 1280 | "event period to sample"), |
| 1281 | OPT_INTEGER('p', "pid", &target_pid, |
| 1282 | "profile events on existing pid"), |
| 1283 | OPT_BOOLEAN('a', "all-cpus", &system_wide, |
| 1284 | "system-wide collection from all CPUs"), |
| 1285 | OPT_INTEGER('C', "CPU", &profile_cpu, |
| 1286 | "CPU to profile on"), |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 1287 | OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"), |
Arnaldo Carvalho de Melo | 8ffcda1 | 2009-11-16 21:45:24 -0200 | [diff] [blame] | 1288 | OPT_BOOLEAN('K', "hide_kernel_symbols", &hide_kernel_symbols, |
| 1289 | "hide kernel symbols"), |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1290 | OPT_INTEGER('m', "mmap-pages", &mmap_pages, |
| 1291 | "number of mmap data pages"), |
| 1292 | OPT_INTEGER('r', "realtime", &realtime_prio, |
| 1293 | "collect data with this RT SCHED_FIFO priority"), |
Mike Galbraith | db20c00 | 2009-05-26 15:25:34 +0200 | [diff] [blame] | 1294 | OPT_INTEGER('d', "delay", &delay_secs, |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1295 | "number of seconds to delay between refreshes"), |
| 1296 | OPT_BOOLEAN('D', "dump-symtab", &dump_symtab, |
| 1297 | "dump the symbol table used for profiling"), |
Ingo Molnar | 6e53cdf | 2009-06-04 08:53:05 +0200 | [diff] [blame] | 1298 | OPT_INTEGER('f', "count-filter", &count_filter, |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1299 | "only display functions with more events than this"), |
| 1300 | OPT_BOOLEAN('g', "group", &group, |
| 1301 | "put the counters into a counter group"), |
Mike Galbraith | 0fdc7e6 | 2009-07-21 10:30:36 +0200 | [diff] [blame] | 1302 | OPT_BOOLEAN('i', "inherit", &inherit, |
| 1303 | "child tasks inherit counters"), |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 1304 | OPT_STRING('s', "sym-annotate", &sym_filter, "symbol name", |
| 1305 | "symbol to annotate - requires -k option"), |
Anton Blanchard | 1f208ea | 2009-07-01 09:00:44 +1000 | [diff] [blame] | 1306 | OPT_BOOLEAN('z', "zero", &zero, |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1307 | "zero history across updates"), |
Ingo Molnar | 6e53cdf | 2009-06-04 08:53:05 +0200 | [diff] [blame] | 1308 | OPT_INTEGER('F', "freq", &freq, |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1309 | "profile at this frequency"), |
Ingo Molnar | 6e53cdf | 2009-06-04 08:53:05 +0200 | [diff] [blame] | 1310 | OPT_INTEGER('E', "entries", &print_entries, |
| 1311 | "display this many functions"), |
Arnaldo Carvalho de Melo | 8ffcda1 | 2009-11-16 21:45:24 -0200 | [diff] [blame] | 1312 | OPT_BOOLEAN('U', "hide_user_symbols", &hide_user_symbols, |
| 1313 | "hide user symbols"), |
Ingo Molnar | 3da297a | 2009-06-07 17:39:02 +0200 | [diff] [blame] | 1314 | OPT_BOOLEAN('v', "verbose", &verbose, |
| 1315 | "be more verbose (show counter open errors, etc)"), |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1316 | OPT_END() |
| 1317 | }; |
| 1318 | |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 1319 | int cmd_top(int argc, const char **argv, const char *prefix __used) |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1320 | { |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 1321 | int counter, err; |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1322 | |
| 1323 | page_size = sysconf(_SC_PAGE_SIZE); |
| 1324 | |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1325 | argc = parse_options(argc, argv, options, top_usage, 0); |
| 1326 | if (argc) |
| 1327 | usage_with_options(top_usage, options); |
| 1328 | |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1329 | /* CPU and PID are mutually exclusive */ |
| 1330 | if (target_pid != -1 && profile_cpu != -1) { |
| 1331 | printf("WARNING: PID switch overriding CPU\n"); |
| 1332 | sleep(1); |
| 1333 | profile_cpu = -1; |
| 1334 | } |
| 1335 | |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 1336 | if (!nr_counters) |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1337 | nr_counters = 1; |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1338 | |
Arnaldo Carvalho de Melo | 5a8e5a3 | 2009-11-17 18:38:01 -0200 | [diff] [blame] | 1339 | symbol__init(sizeof(struct sym_entry) + |
| 1340 | (nr_counters + 1) * sizeof(unsigned long)); |
| 1341 | |
Frederic Weisbecker | 2f335a0 | 2009-06-05 19:31:01 +0200 | [diff] [blame] | 1342 | if (delay_secs < 1) |
| 1343 | delay_secs = 1; |
| 1344 | |
Arnaldo Carvalho de Melo | cc612d8 | 2009-11-23 16:39:10 -0200 | [diff] [blame^] | 1345 | err = kernel_maps__init(vmlinux_name, !vmlinux_name, true); |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 1346 | if (err < 0) |
| 1347 | return err; |
Mike Galbraith | 923c42c | 2009-07-22 20:36:03 +0200 | [diff] [blame] | 1348 | parse_source(sym_filter_entry); |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 1349 | |
Mike Galbraith | 7e4ff9e | 2009-10-12 07:56:03 +0200 | [diff] [blame] | 1350 | /* |
| 1351 | * User specified count overrides default frequency. |
| 1352 | */ |
| 1353 | if (default_interval) |
| 1354 | freq = 0; |
| 1355 | else if (freq) { |
| 1356 | default_interval = freq; |
| 1357 | } else { |
| 1358 | fprintf(stderr, "frequency and count are zero, aborting\n"); |
| 1359 | exit(EXIT_FAILURE); |
| 1360 | } |
| 1361 | |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 1362 | /* |
| 1363 | * Fill in the ones not specifically initialized via -c: |
| 1364 | */ |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1365 | for (counter = 0; counter < nr_counters; counter++) { |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 1366 | if (attrs[counter].sample_period) |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1367 | continue; |
| 1368 | |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 1369 | attrs[counter].sample_period = default_interval; |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1370 | } |
| 1371 | |
| 1372 | nr_cpus = sysconf(_SC_NPROCESSORS_ONLN); |
| 1373 | assert(nr_cpus <= MAX_NR_CPUS); |
| 1374 | assert(nr_cpus >= 0); |
| 1375 | |
| 1376 | if (target_pid != -1 || profile_cpu != -1) |
| 1377 | nr_cpus = 1; |
| 1378 | |
Arnaldo Carvalho de Melo | 13cc507 | 2009-11-17 15:40:54 -0200 | [diff] [blame] | 1379 | get_term_dimensions(&winsize); |
Arnaldo Carvalho de Melo | 3b6ed98 | 2009-11-16 19:30:27 -0200 | [diff] [blame] | 1380 | if (print_entries == 0) { |
Arnaldo Carvalho de Melo | 13cc507 | 2009-11-17 15:40:54 -0200 | [diff] [blame] | 1381 | update_print_entries(&winsize); |
Arnaldo Carvalho de Melo | 3b6ed98 | 2009-11-16 19:30:27 -0200 | [diff] [blame] | 1382 | signal(SIGWINCH, sig_winch_handler); |
| 1383 | } |
| 1384 | |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1385 | return __cmd_top(); |
| 1386 | } |