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