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