Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 1 | /* |
| 2 | * kerneltop.c: show top kernel functions - performance counters showcase |
| 3 | |
| 4 | Build with: |
| 5 | |
Robert Richter | 38105f0 | 2009-04-29 12:47:26 +0200 | [diff] [blame] | 6 | make -C Documentation/perf_counter/ |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 7 | |
| 8 | Sample output: |
| 9 | |
| 10 | ------------------------------------------------------------------------------ |
Mike Galbraith | f91183f | 2009-05-26 15:25:34 +0200 | [diff] [blame] | 11 | KernelTop: 2669 irqs/sec [cache-misses/cache-refs], (all, cpu: 2) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 12 | ------------------------------------------------------------------------------ |
| 13 | |
| 14 | weight RIP kernel function |
| 15 | ______ ________________ _______________ |
| 16 | |
| 17 | 35.20 - ffffffff804ce74b : skb_copy_and_csum_dev |
| 18 | 33.00 - ffffffff804cb740 : sock_alloc_send_skb |
| 19 | 31.26 - ffffffff804ce808 : skb_push |
| 20 | 22.43 - ffffffff80510004 : tcp_established_options |
| 21 | 19.00 - ffffffff8027d250 : find_get_page |
| 22 | 15.76 - ffffffff804e4fc9 : eth_type_trans |
| 23 | 15.20 - ffffffff804d8baa : dst_release |
| 24 | 14.86 - ffffffff804cf5d8 : skb_release_head_state |
| 25 | 14.00 - ffffffff802217d5 : read_hpet |
| 26 | 12.00 - ffffffff804ffb7f : __ip_local_out |
| 27 | 11.97 - ffffffff804fc0c8 : ip_local_deliver_finish |
| 28 | 8.54 - ffffffff805001a3 : ip_queue_xmit |
| 29 | */ |
| 30 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 31 | /* |
| 32 | * Copyright (C) 2008, Red Hat Inc, Ingo Molnar <mingo@redhat.com> |
| 33 | * |
| 34 | * Improvements and fixes by: |
| 35 | * |
| 36 | * Arjan van de Ven <arjan@linux.intel.com> |
| 37 | * Yanmin Zhang <yanmin.zhang@intel.com> |
| 38 | * Wu Fengguang <fengguang.wu@intel.com> |
| 39 | * Mike Galbraith <efault@gmx.de> |
| 40 | * Paul Mackerras <paulus@samba.org> |
| 41 | * |
| 42 | * Released under the GPL v2. (and only v2, not any later version) |
| 43 | */ |
| 44 | |
Peter Zijlstra | 1a482f3 | 2009-05-23 18:28:58 +0200 | [diff] [blame] | 45 | #include "perf.h" |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 46 | #include "builtin.h" |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 47 | #include "util/symbol.h" |
Ingo Molnar | 148be2c | 2009-04-27 08:02:14 +0200 | [diff] [blame] | 48 | #include "util/util.h" |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 49 | #include "util/rbtree.h" |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 50 | #include "util/parse-options.h" |
| 51 | #include "util/parse-events.h" |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 52 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 53 | #include <assert.h> |
| 54 | #include <fcntl.h> |
Ingo Molnar | 0e9b20b | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 55 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 56 | #include <stdio.h> |
Ingo Molnar | 0e9b20b | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 57 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 58 | #include <errno.h> |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 59 | #include <time.h> |
| 60 | #include <sched.h> |
| 61 | #include <pthread.h> |
| 62 | |
| 63 | #include <sys/syscall.h> |
| 64 | #include <sys/ioctl.h> |
| 65 | #include <sys/poll.h> |
| 66 | #include <sys/prctl.h> |
| 67 | #include <sys/wait.h> |
| 68 | #include <sys/uio.h> |
| 69 | #include <sys/mman.h> |
| 70 | |
| 71 | #include <linux/unistd.h> |
| 72 | #include <linux/types.h> |
| 73 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 74 | static int system_wide = 0; |
| 75 | |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 76 | static __u64 default_event_id[MAX_COUNTERS] = { |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 77 | EID(PERF_TYPE_SOFTWARE, PERF_COUNT_TASK_CLOCK), |
| 78 | EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CONTEXT_SWITCHES), |
| 79 | EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CPU_MIGRATIONS), |
| 80 | EID(PERF_TYPE_SOFTWARE, PERF_COUNT_PAGE_FAULTS), |
| 81 | |
| 82 | EID(PERF_TYPE_HARDWARE, PERF_COUNT_CPU_CYCLES), |
| 83 | EID(PERF_TYPE_HARDWARE, PERF_COUNT_INSTRUCTIONS), |
| 84 | EID(PERF_TYPE_HARDWARE, PERF_COUNT_CACHE_REFERENCES), |
| 85 | EID(PERF_TYPE_HARDWARE, PERF_COUNT_CACHE_MISSES), |
| 86 | }; |
| 87 | static int default_interval = 100000; |
| 88 | static int event_count[MAX_COUNTERS]; |
| 89 | static int fd[MAX_NR_CPUS][MAX_COUNTERS]; |
| 90 | |
| 91 | static __u64 count_filter = 100; |
| 92 | |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 93 | static int target_pid = -1; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 94 | static int profile_cpu = -1; |
| 95 | static int nr_cpus = 0; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 96 | static unsigned int realtime_prio = 0; |
| 97 | static int group = 0; |
| 98 | static unsigned int page_size; |
| 99 | static unsigned int mmap_pages = 16; |
| 100 | static int use_mmap = 0; |
| 101 | static int use_munmap = 0; |
Peter Zijlstra | f5456a6 | 2009-05-15 15:19:29 +0200 | [diff] [blame] | 102 | static int freq = 0; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 103 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 104 | static char *sym_filter; |
| 105 | static unsigned long filter_start; |
| 106 | static unsigned long filter_end; |
| 107 | |
| 108 | static int delay_secs = 2; |
| 109 | static int zero; |
| 110 | static int dump_symtab; |
| 111 | |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 112 | static const unsigned int default_count[] = { |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 113 | 1000000, |
| 114 | 1000000, |
| 115 | 10000, |
| 116 | 10000, |
| 117 | 1000000, |
| 118 | 10000, |
| 119 | }; |
| 120 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 121 | /* |
| 122 | * Symbols |
| 123 | */ |
| 124 | |
| 125 | static uint64_t min_ip; |
| 126 | static uint64_t max_ip = -1ll; |
| 127 | |
| 128 | struct sym_entry { |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 129 | struct rb_node rb_node; |
| 130 | struct list_head node; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 131 | unsigned long count[MAX_COUNTERS]; |
Arnaldo Carvalho de Melo | c44613a | 2009-05-29 17:03:07 -0300 | [diff] [blame] | 132 | unsigned long snap_count; |
| 133 | double weight; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 134 | int skip; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 135 | }; |
| 136 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 137 | struct sym_entry *sym_filter_entry; |
| 138 | |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 139 | struct dso *kernel_dso; |
| 140 | |
| 141 | /* |
| 142 | * Symbols will be added here in record_ip and will get out |
| 143 | * after decayed. |
| 144 | */ |
| 145 | static LIST_HEAD(active_symbols); |
Arnaldo Carvalho de Melo | c44613a | 2009-05-29 17:03:07 -0300 | [diff] [blame] | 146 | static pthread_mutex_t active_symbols_lock = PTHREAD_MUTEX_INITIALIZER; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 147 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 148 | /* |
| 149 | * Ordering weight: count-1 * count-2 * ... / count-n |
| 150 | */ |
| 151 | static double sym_weight(const struct sym_entry *sym) |
| 152 | { |
Arnaldo Carvalho de Melo | c44613a | 2009-05-29 17:03:07 -0300 | [diff] [blame] | 153 | double weight = sym->snap_count; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 154 | int counter; |
| 155 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 156 | for (counter = 1; counter < nr_counters-1; counter++) |
| 157 | weight *= sym->count[counter]; |
| 158 | |
| 159 | weight /= (sym->count[counter] + 1); |
| 160 | |
| 161 | return weight; |
| 162 | } |
| 163 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 164 | static long events; |
| 165 | static long userspace_events; |
| 166 | static const char CONSOLE_CLEAR[] = "[H[2J"; |
| 167 | |
Arnaldo Carvalho de Melo | c44613a | 2009-05-29 17:03:07 -0300 | [diff] [blame] | 168 | static void __list_insert_active_sym(struct sym_entry *syme) |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 169 | { |
| 170 | list_add(&syme->node, &active_symbols); |
| 171 | } |
| 172 | |
Arnaldo Carvalho de Melo | c44613a | 2009-05-29 17:03:07 -0300 | [diff] [blame] | 173 | static void list_remove_active_sym(struct sym_entry *syme) |
| 174 | { |
| 175 | pthread_mutex_lock(&active_symbols_lock); |
| 176 | list_del_init(&syme->node); |
| 177 | pthread_mutex_unlock(&active_symbols_lock); |
| 178 | } |
| 179 | |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 180 | static void rb_insert_active_sym(struct rb_root *tree, struct sym_entry *se) |
| 181 | { |
| 182 | struct rb_node **p = &tree->rb_node; |
| 183 | struct rb_node *parent = NULL; |
| 184 | struct sym_entry *iter; |
| 185 | |
| 186 | while (*p != NULL) { |
| 187 | parent = *p; |
| 188 | iter = rb_entry(parent, struct sym_entry, rb_node); |
| 189 | |
Arnaldo Carvalho de Melo | c44613a | 2009-05-29 17:03:07 -0300 | [diff] [blame] | 190 | if (se->weight > iter->weight) |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 191 | p = &(*p)->rb_left; |
| 192 | else |
| 193 | p = &(*p)->rb_right; |
| 194 | } |
| 195 | |
| 196 | rb_link_node(&se->rb_node, parent, p); |
| 197 | rb_insert_color(&se->rb_node, tree); |
| 198 | } |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 199 | |
| 200 | static void print_sym_table(void) |
| 201 | { |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 202 | int printed, j; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 203 | int counter; |
| 204 | float events_per_sec = events/delay_secs; |
| 205 | float kevents_per_sec = (events-userspace_events)/delay_secs; |
| 206 | float sum_kevents = 0.0; |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 207 | struct sym_entry *syme, *n; |
| 208 | struct rb_root tmp = RB_ROOT; |
| 209 | struct rb_node *nd; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 210 | |
| 211 | events = userspace_events = 0; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 212 | |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 213 | /* Sort the active symbols */ |
Arnaldo Carvalho de Melo | c44613a | 2009-05-29 17:03:07 -0300 | [diff] [blame] | 214 | pthread_mutex_lock(&active_symbols_lock); |
| 215 | syme = list_entry(active_symbols.next, struct sym_entry, node); |
| 216 | pthread_mutex_unlock(&active_symbols_lock); |
| 217 | |
| 218 | list_for_each_entry_safe_from(syme, n, &active_symbols, node) { |
| 219 | syme->snap_count = syme->count[0]; |
| 220 | if (syme->snap_count != 0) { |
| 221 | syme->weight = sym_weight(syme); |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 222 | rb_insert_active_sym(&tmp, syme); |
Arnaldo Carvalho de Melo | c44613a | 2009-05-29 17:03:07 -0300 | [diff] [blame] | 223 | sum_kevents += syme->snap_count; |
Mike Galbraith | d94b943 | 2009-05-25 09:57:56 +0200 | [diff] [blame] | 224 | |
| 225 | for (j = 0; j < nr_counters; j++) |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 226 | syme->count[j] = zero ? 0 : syme->count[j] * 7 / 8; |
| 227 | } else |
Arnaldo Carvalho de Melo | c44613a | 2009-05-29 17:03:07 -0300 | [diff] [blame] | 228 | list_remove_active_sym(syme); |
Mike Galbraith | d94b943 | 2009-05-25 09:57:56 +0200 | [diff] [blame] | 229 | } |
| 230 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 231 | write(1, CONSOLE_CLEAR, strlen(CONSOLE_CLEAR)); |
| 232 | |
| 233 | printf( |
| 234 | "------------------------------------------------------------------------------\n"); |
Mike Galbraith | f91183f | 2009-05-26 15:25:34 +0200 | [diff] [blame] | 235 | printf( " KernelTop:%8.0f irqs/sec kernel:%4.1f%% [", |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 236 | events_per_sec, |
Mike Galbraith | f91183f | 2009-05-26 15:25:34 +0200 | [diff] [blame] | 237 | 100.0 - (100.0*((events_per_sec-kevents_per_sec)/events_per_sec))); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 238 | |
| 239 | if (nr_counters == 1) |
| 240 | printf("%d ", event_count[0]); |
| 241 | |
| 242 | for (counter = 0; counter < nr_counters; counter++) { |
| 243 | if (counter) |
| 244 | printf("/"); |
| 245 | |
| 246 | printf("%s", event_name(counter)); |
| 247 | } |
| 248 | |
| 249 | printf( "], "); |
| 250 | |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 251 | if (target_pid != -1) |
| 252 | printf(" (target_pid: %d", target_pid); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 253 | else |
| 254 | printf(" (all"); |
| 255 | |
| 256 | if (profile_cpu != -1) |
| 257 | printf(", cpu: %d)\n", profile_cpu); |
| 258 | else { |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 259 | if (target_pid != -1) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 260 | printf(")\n"); |
| 261 | else |
| 262 | printf(", %d CPUs)\n", nr_cpus); |
| 263 | } |
| 264 | |
| 265 | printf("------------------------------------------------------------------------------\n\n"); |
| 266 | |
| 267 | if (nr_counters == 1) |
| 268 | printf(" events pcnt"); |
| 269 | else |
| 270 | printf(" weight events pcnt"); |
| 271 | |
| 272 | printf(" RIP kernel function\n" |
| 273 | " ______ ______ _____ ________________ _______________\n\n" |
| 274 | ); |
| 275 | |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 276 | for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) { |
| 277 | struct sym_entry *syme = rb_entry(nd, struct sym_entry, rb_node); |
| 278 | struct symbol *sym = (struct symbol *)(syme + 1); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 279 | float pcnt; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 280 | |
Arnaldo Carvalho de Melo | c44613a | 2009-05-29 17:03:07 -0300 | [diff] [blame] | 281 | if (++printed > 18 || syme->snap_count < count_filter) |
| 282 | continue; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 283 | |
Arnaldo Carvalho de Melo | c44613a | 2009-05-29 17:03:07 -0300 | [diff] [blame] | 284 | pcnt = 100.0 - (100.0 * ((sum_kevents - syme->snap_count) / |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 285 | sum_kevents)); |
Mike Galbraith | d94b943 | 2009-05-25 09:57:56 +0200 | [diff] [blame] | 286 | |
| 287 | if (nr_counters == 1) |
| 288 | printf("%19.2f - %4.1f%% - %016llx : %s\n", |
Arnaldo Carvalho de Melo | c44613a | 2009-05-29 17:03:07 -0300 | [diff] [blame] | 289 | syme->weight, pcnt, sym->start, sym->name); |
Mike Galbraith | d94b943 | 2009-05-25 09:57:56 +0200 | [diff] [blame] | 290 | else |
| 291 | printf("%8.1f %10ld - %4.1f%% - %016llx : %s\n", |
Arnaldo Carvalho de Melo | c44613a | 2009-05-29 17:03:07 -0300 | [diff] [blame] | 292 | syme->weight, syme->snap_count, |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 293 | pcnt, sym->start, sym->name); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 294 | } |
| 295 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 296 | { |
| 297 | struct pollfd stdin_poll = { .fd = 0, .events = POLLIN }; |
| 298 | |
| 299 | if (poll(&stdin_poll, 1, 0) == 1) { |
| 300 | printf("key pressed - exiting.\n"); |
| 301 | exit(0); |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | static void *display_thread(void *arg) |
| 307 | { |
| 308 | printf("KernelTop refresh period: %d seconds\n", delay_secs); |
| 309 | |
| 310 | while (!sleep(delay_secs)) |
| 311 | print_sym_table(); |
| 312 | |
| 313 | return NULL; |
| 314 | } |
| 315 | |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 316 | static int symbol_filter(struct dso *self, struct symbol *sym) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 317 | { |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 318 | static int filter_match; |
| 319 | struct sym_entry *syme; |
| 320 | const char *name = sym->name; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 321 | |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 322 | if (!strcmp(name, "_text") || |
| 323 | !strcmp(name, "_etext") || |
| 324 | !strcmp(name, "_sinittext") || |
| 325 | !strncmp("init_module", name, 11) || |
| 326 | !strncmp("cleanup_module", name, 14) || |
| 327 | strstr(name, "_text_start") || |
| 328 | strstr(name, "_text_end")) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 329 | return 1; |
| 330 | |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 331 | syme = dso__sym_priv(self, sym); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 332 | /* Tag events to be skipped. */ |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 333 | if (!strcmp("default_idle", name) || |
| 334 | !strcmp("cpu_idle", name) || |
| 335 | !strcmp("enter_idle", name) || |
| 336 | !strcmp("exit_idle", name) || |
| 337 | !strcmp("mwait_idle", name)) |
| 338 | syme->skip = 1; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 339 | |
| 340 | if (filter_match == 1) { |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 341 | filter_end = sym->start; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 342 | filter_match = -1; |
| 343 | if (filter_end - filter_start > 10000) { |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 344 | fprintf(stderr, |
| 345 | "hm, too large filter symbol <%s> - skipping.\n", |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 346 | sym_filter); |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 347 | fprintf(stderr, "symbol filter start: %016lx\n", |
| 348 | filter_start); |
| 349 | fprintf(stderr, " end: %016lx\n", |
| 350 | filter_end); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 351 | filter_end = filter_start = 0; |
| 352 | sym_filter = NULL; |
| 353 | sleep(1); |
| 354 | } |
| 355 | } |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 356 | |
| 357 | if (filter_match == 0 && sym_filter && !strcmp(name, sym_filter)) { |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 358 | filter_match = 1; |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 359 | filter_start = sym->start; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 360 | } |
| 361 | |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 362 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 363 | return 0; |
| 364 | } |
| 365 | |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 366 | static int parse_symbols(void) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 367 | { |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 368 | struct rb_node *node; |
| 369 | struct symbol *sym; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 370 | |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 371 | kernel_dso = dso__new("[kernel]", sizeof(struct sym_entry)); |
| 372 | if (kernel_dso == NULL) |
| 373 | return -1; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 374 | |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 375 | if (dso__load_kernel(kernel_dso, NULL, symbol_filter) != 0) |
| 376 | goto out_delete_dso; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 377 | |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 378 | node = rb_first(&kernel_dso->syms); |
| 379 | sym = rb_entry(node, struct symbol, rb_node); |
| 380 | min_ip = sym->start; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 381 | |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 382 | node = rb_last(&kernel_dso->syms); |
| 383 | sym = rb_entry(node, struct symbol, rb_node); |
Mike Galbraith | da417a7 | 2009-05-29 08:23:16 +0200 | [diff] [blame] | 384 | max_ip = sym->end; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 385 | |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 386 | if (dump_symtab) |
Mike Galbraith | a3ec8d7 | 2009-05-29 06:46:46 +0200 | [diff] [blame] | 387 | dso__fprintf(kernel_dso, stderr); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 388 | |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 389 | return 0; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 390 | |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 391 | out_delete_dso: |
| 392 | dso__delete(kernel_dso); |
| 393 | kernel_dso = NULL; |
| 394 | return -1; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 395 | } |
| 396 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 397 | #define TRACE_COUNT 3 |
| 398 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 399 | /* |
| 400 | * Binary search in the histogram table and record the hit: |
| 401 | */ |
| 402 | static void record_ip(uint64_t ip, int counter) |
| 403 | { |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 404 | struct symbol *sym = dso__find_symbol(kernel_dso, ip); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 405 | |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 406 | if (sym != NULL) { |
| 407 | struct sym_entry *syme = dso__sym_priv(kernel_dso, sym); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 408 | |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 409 | if (!syme->skip) { |
| 410 | syme->count[counter]++; |
Arnaldo Carvalho de Melo | c44613a | 2009-05-29 17:03:07 -0300 | [diff] [blame] | 411 | pthread_mutex_lock(&active_symbols_lock); |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 412 | if (list_empty(&syme->node) || !syme->node.next) |
Arnaldo Carvalho de Melo | c44613a | 2009-05-29 17:03:07 -0300 | [diff] [blame] | 413 | __list_insert_active_sym(syme); |
| 414 | pthread_mutex_unlock(&active_symbols_lock); |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 415 | return; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 416 | } |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 417 | } |
| 418 | |
Arnaldo Carvalho de Melo | de04687 | 2009-05-28 14:55:41 -0300 | [diff] [blame] | 419 | events--; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | static void process_event(uint64_t ip, int counter) |
| 423 | { |
| 424 | events++; |
| 425 | |
| 426 | if (ip < min_ip || ip > max_ip) { |
| 427 | userspace_events++; |
| 428 | return; |
| 429 | } |
| 430 | |
| 431 | record_ip(ip, counter); |
| 432 | } |
| 433 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 434 | struct mmap_data { |
| 435 | int counter; |
| 436 | void *base; |
| 437 | unsigned int mask; |
| 438 | unsigned int prev; |
| 439 | }; |
| 440 | |
| 441 | static unsigned int mmap_read_head(struct mmap_data *md) |
| 442 | { |
| 443 | struct perf_counter_mmap_page *pc = md->base; |
| 444 | int head; |
| 445 | |
| 446 | head = pc->data_head; |
| 447 | rmb(); |
| 448 | |
| 449 | return head; |
| 450 | } |
| 451 | |
| 452 | struct timeval last_read, this_read; |
| 453 | |
| 454 | static void mmap_read(struct mmap_data *md) |
| 455 | { |
| 456 | unsigned int head = mmap_read_head(md); |
| 457 | unsigned int old = md->prev; |
| 458 | unsigned char *data = md->base + page_size; |
| 459 | int diff; |
| 460 | |
| 461 | gettimeofday(&this_read, NULL); |
| 462 | |
| 463 | /* |
| 464 | * If we're further behind than half the buffer, there's a chance |
| 465 | * the writer will bite our tail and screw up the events under us. |
| 466 | * |
| 467 | * If we somehow ended up ahead of the head, we got messed up. |
| 468 | * |
| 469 | * In either case, truncate and restart at head. |
| 470 | */ |
| 471 | diff = head - old; |
| 472 | if (diff > md->mask / 2 || diff < 0) { |
| 473 | struct timeval iv; |
| 474 | unsigned long msecs; |
| 475 | |
| 476 | timersub(&this_read, &last_read, &iv); |
| 477 | msecs = iv.tv_sec*1000 + iv.tv_usec/1000; |
| 478 | |
| 479 | fprintf(stderr, "WARNING: failed to keep up with mmap data." |
| 480 | " Last read %lu msecs ago.\n", msecs); |
| 481 | |
| 482 | /* |
| 483 | * head points to a known good entry, start there. |
| 484 | */ |
| 485 | old = head; |
| 486 | } |
| 487 | |
| 488 | last_read = this_read; |
| 489 | |
| 490 | for (; old != head;) { |
| 491 | struct ip_event { |
| 492 | struct perf_event_header header; |
| 493 | __u64 ip; |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 494 | __u32 pid, target_pid; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 495 | }; |
| 496 | struct mmap_event { |
| 497 | struct perf_event_header header; |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 498 | __u32 pid, target_pid; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 499 | __u64 start; |
| 500 | __u64 len; |
| 501 | __u64 pgoff; |
| 502 | char filename[PATH_MAX]; |
| 503 | }; |
| 504 | |
| 505 | typedef union event_union { |
| 506 | struct perf_event_header header; |
| 507 | struct ip_event ip; |
| 508 | struct mmap_event mmap; |
| 509 | } event_t; |
| 510 | |
| 511 | event_t *event = (event_t *)&data[old & md->mask]; |
| 512 | |
| 513 | event_t event_copy; |
| 514 | |
Ingo Molnar | 6f06ccb | 2009-04-20 15:22:22 +0200 | [diff] [blame] | 515 | size_t size = event->header.size; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 516 | |
| 517 | /* |
| 518 | * Event straddles the mmap boundary -- header should always |
| 519 | * be inside due to u64 alignment of output. |
| 520 | */ |
| 521 | if ((old & md->mask) + size != ((old + size) & md->mask)) { |
| 522 | unsigned int offset = old; |
| 523 | unsigned int len = min(sizeof(*event), size), cpy; |
| 524 | void *dst = &event_copy; |
| 525 | |
| 526 | do { |
| 527 | cpy = min(md->mask + 1 - (offset & md->mask), len); |
| 528 | memcpy(dst, &data[offset & md->mask], cpy); |
| 529 | offset += cpy; |
| 530 | dst += cpy; |
| 531 | len -= cpy; |
| 532 | } while (len); |
| 533 | |
| 534 | event = &event_copy; |
| 535 | } |
| 536 | |
| 537 | old += size; |
| 538 | |
| 539 | if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) { |
Peter Zijlstra | c70975b | 2009-06-02 17:38:21 +0200 | [diff] [blame^] | 540 | if (event->header.type & PERF_SAMPLE_IP) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 541 | process_event(event->ip.ip, md->counter); |
| 542 | } else { |
| 543 | switch (event->header.type) { |
| 544 | case PERF_EVENT_MMAP: |
| 545 | case PERF_EVENT_MUNMAP: |
| 546 | printf("%s: %Lu %Lu %Lu %s\n", |
| 547 | event->header.type == PERF_EVENT_MMAP |
| 548 | ? "mmap" : "munmap", |
| 549 | event->mmap.start, |
| 550 | event->mmap.len, |
| 551 | event->mmap.pgoff, |
| 552 | event->mmap.filename); |
| 553 | break; |
| 554 | } |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | md->prev = old; |
| 559 | } |
| 560 | |
Mike Galbraith | c2990a2 | 2009-05-24 08:35:49 +0200 | [diff] [blame] | 561 | static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS]; |
| 562 | static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS]; |
| 563 | |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 564 | static int __cmd_top(void) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 565 | { |
Peter Zijlstra | c70975b | 2009-06-02 17:38:21 +0200 | [diff] [blame^] | 566 | struct perf_counter_attr attr; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 567 | pthread_t thread; |
| 568 | int i, counter, group_fd, nr_poll = 0; |
| 569 | unsigned int cpu; |
| 570 | int ret; |
| 571 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 572 | for (i = 0; i < nr_cpus; i++) { |
| 573 | group_fd = -1; |
| 574 | for (counter = 0; counter < nr_counters; counter++) { |
| 575 | |
| 576 | cpu = profile_cpu; |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 577 | if (target_pid == -1 && profile_cpu == -1) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 578 | cpu = i; |
| 579 | |
Peter Zijlstra | c70975b | 2009-06-02 17:38:21 +0200 | [diff] [blame^] | 580 | memset(&attr, 0, sizeof(attr)); |
| 581 | attr.config = event_id[counter]; |
| 582 | attr.sample_period = event_count[counter]; |
| 583 | attr.sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID; |
| 584 | attr.mmap = use_mmap; |
| 585 | attr.munmap = use_munmap; |
| 586 | attr.freq = freq; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 587 | |
Peter Zijlstra | c70975b | 2009-06-02 17:38:21 +0200 | [diff] [blame^] | 588 | fd[i][counter] = sys_perf_counter_open(&attr, target_pid, cpu, group_fd, 0); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 589 | if (fd[i][counter] < 0) { |
| 590 | int err = errno; |
| 591 | printf("kerneltop error: syscall returned with %d (%s)\n", |
| 592 | fd[i][counter], strerror(err)); |
| 593 | if (err == EPERM) |
| 594 | printf("Are you root?\n"); |
| 595 | exit(-1); |
| 596 | } |
| 597 | assert(fd[i][counter] >= 0); |
| 598 | fcntl(fd[i][counter], F_SETFL, O_NONBLOCK); |
| 599 | |
| 600 | /* |
| 601 | * First counter acts as the group leader: |
| 602 | */ |
| 603 | if (group && group_fd == -1) |
| 604 | group_fd = fd[i][counter]; |
| 605 | |
| 606 | event_array[nr_poll].fd = fd[i][counter]; |
| 607 | event_array[nr_poll].events = POLLIN; |
| 608 | nr_poll++; |
| 609 | |
| 610 | mmap_array[i][counter].counter = counter; |
| 611 | mmap_array[i][counter].prev = 0; |
| 612 | mmap_array[i][counter].mask = mmap_pages*page_size - 1; |
| 613 | mmap_array[i][counter].base = mmap(NULL, (mmap_pages+1)*page_size, |
| 614 | PROT_READ, MAP_SHARED, fd[i][counter], 0); |
| 615 | if (mmap_array[i][counter].base == MAP_FAILED) { |
| 616 | printf("kerneltop error: failed to mmap with %d (%s)\n", |
| 617 | errno, strerror(errno)); |
| 618 | exit(-1); |
| 619 | } |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | if (pthread_create(&thread, NULL, display_thread, NULL)) { |
| 624 | printf("Could not create display thread.\n"); |
| 625 | exit(-1); |
| 626 | } |
| 627 | |
| 628 | if (realtime_prio) { |
| 629 | struct sched_param param; |
| 630 | |
| 631 | param.sched_priority = realtime_prio; |
| 632 | if (sched_setscheduler(0, SCHED_FIFO, ¶m)) { |
| 633 | printf("Could not set realtime priority.\n"); |
| 634 | exit(-1); |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | while (1) { |
| 639 | int hits = events; |
| 640 | |
| 641 | for (i = 0; i < nr_cpus; i++) { |
| 642 | for (counter = 0; counter < nr_counters; counter++) |
| 643 | mmap_read(&mmap_array[i][counter]); |
| 644 | } |
| 645 | |
| 646 | if (hits == events) |
| 647 | ret = poll(event_array, nr_poll, 100); |
| 648 | } |
| 649 | |
| 650 | return 0; |
| 651 | } |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 652 | |
| 653 | static const char * const top_usage[] = { |
| 654 | "perf top [<options>]", |
| 655 | NULL |
| 656 | }; |
| 657 | |
| 658 | static char events_help_msg[EVENTS_HELP_MAX]; |
| 659 | |
| 660 | static const struct option options[] = { |
| 661 | OPT_CALLBACK('e', "event", NULL, "event", |
| 662 | events_help_msg, parse_events), |
| 663 | OPT_INTEGER('c', "count", &default_interval, |
| 664 | "event period to sample"), |
| 665 | OPT_INTEGER('p', "pid", &target_pid, |
| 666 | "profile events on existing pid"), |
| 667 | OPT_BOOLEAN('a', "all-cpus", &system_wide, |
| 668 | "system-wide collection from all CPUs"), |
| 669 | OPT_INTEGER('C', "CPU", &profile_cpu, |
| 670 | "CPU to profile on"), |
| 671 | OPT_INTEGER('m', "mmap-pages", &mmap_pages, |
| 672 | "number of mmap data pages"), |
| 673 | OPT_INTEGER('r', "realtime", &realtime_prio, |
| 674 | "collect data with this RT SCHED_FIFO priority"), |
Mike Galbraith | db20c00 | 2009-05-26 15:25:34 +0200 | [diff] [blame] | 675 | OPT_INTEGER('d', "delay", &delay_secs, |
Ingo Molnar | b456bae | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 676 | "number of seconds to delay between refreshes"), |
| 677 | OPT_BOOLEAN('D', "dump-symtab", &dump_symtab, |
| 678 | "dump the symbol table used for profiling"), |
| 679 | OPT_INTEGER('f', "--count-filter", &count_filter, |
| 680 | "only display functions with more events than this"), |
| 681 | OPT_BOOLEAN('g', "group", &group, |
| 682 | "put the counters into a counter group"), |
| 683 | OPT_STRING('s', "sym-filter", &sym_filter, "pattern", |
| 684 | "only display symbols matchig this pattern"), |
| 685 | OPT_BOOLEAN('z', "zero", &group, |
| 686 | "zero history across updates"), |
| 687 | OPT_BOOLEAN('M', "use-mmap", &use_mmap, |
| 688 | "track mmap events"), |
| 689 | OPT_BOOLEAN('U', "use-munmap", &use_munmap, |
| 690 | "track munmap events"), |
| 691 | OPT_INTEGER('F', "--freq", &freq, |
| 692 | "profile at this frequency"), |
| 693 | OPT_END() |
| 694 | }; |
| 695 | |
| 696 | int cmd_top(int argc, const char **argv, const char *prefix) |
| 697 | { |
| 698 | int counter; |
| 699 | |
| 700 | page_size = sysconf(_SC_PAGE_SIZE); |
| 701 | |
| 702 | create_events_help(events_help_msg); |
| 703 | memcpy(event_id, default_event_id, sizeof(default_event_id)); |
| 704 | |
| 705 | argc = parse_options(argc, argv, options, top_usage, 0); |
| 706 | if (argc) |
| 707 | usage_with_options(top_usage, options); |
| 708 | |
| 709 | if (freq) { |
| 710 | default_interval = freq; |
| 711 | freq = 1; |
| 712 | } |
| 713 | |
| 714 | /* CPU and PID are mutually exclusive */ |
| 715 | if (target_pid != -1 && profile_cpu != -1) { |
| 716 | printf("WARNING: PID switch overriding CPU\n"); |
| 717 | sleep(1); |
| 718 | profile_cpu = -1; |
| 719 | } |
| 720 | |
| 721 | if (!nr_counters) { |
| 722 | nr_counters = 1; |
| 723 | event_id[0] = 0; |
| 724 | } |
| 725 | |
| 726 | for (counter = 0; counter < nr_counters; counter++) { |
| 727 | if (event_count[counter]) |
| 728 | continue; |
| 729 | |
| 730 | event_count[counter] = default_interval; |
| 731 | } |
| 732 | |
| 733 | nr_cpus = sysconf(_SC_NPROCESSORS_ONLN); |
| 734 | assert(nr_cpus <= MAX_NR_CPUS); |
| 735 | assert(nr_cpus >= 0); |
| 736 | |
| 737 | if (target_pid != -1 || profile_cpu != -1) |
| 738 | nr_cpus = 1; |
| 739 | |
| 740 | parse_symbols(); |
| 741 | |
| 742 | return __cmd_top(); |
| 743 | } |