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