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