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