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