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