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