blob: 52ba9f4216cf57b21fbf907a4f38a408b8d268b9 [file] [log] [blame]
Ingo Molnar07800602009-04-20 15:00:56 +02001/*
2 * kerneltop.c: show top kernel functions - performance counters showcase
3
4 Build with:
5
Robert Richter38105f02009-04-29 12:47:26 +02006 make -C Documentation/perf_counter/
Ingo Molnar07800602009-04-20 15:00:56 +02007
8 Sample output:
9
10------------------------------------------------------------------------------
Mike Galbraithf91183f2009-05-26 15:25:34 +020011 KernelTop: 2669 irqs/sec [cache-misses/cache-refs], (all, cpu: 2)
Ingo Molnar07800602009-04-20 15:00:56 +020012------------------------------------------------------------------------------
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 Molnar07800602009-04-20 15:00:56 +020031 /*
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 Zijlstra1a482f32009-05-23 18:28:58 +020045#include "perf.h"
Ingo Molnar16f762a2009-05-27 09:10:38 +020046#include "builtin.h"
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -030047#include "util/symbol.h"
Ingo Molnar148be2c2009-04-27 08:02:14 +020048#include "util/util.h"
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -030049#include "util/rbtree.h"
Ingo Molnarb456bae2009-05-26 09:17:18 +020050#include "util/parse-options.h"
51#include "util/parse-events.h"
Ingo Molnar07800602009-04-20 15:00:56 +020052
Ingo Molnar07800602009-04-20 15:00:56 +020053#include <assert.h>
54#include <fcntl.h>
Ingo Molnar0e9b20b2009-05-26 09:17:18 +020055
Ingo Molnar07800602009-04-20 15:00:56 +020056#include <stdio.h>
Ingo Molnar0e9b20b2009-05-26 09:17:18 +020057
Ingo Molnar07800602009-04-20 15:00:56 +020058#include <errno.h>
Ingo Molnar07800602009-04-20 15:00:56 +020059#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 Molnar07800602009-04-20 15:00:56 +020074static int system_wide = 0;
75
Ingo Molnarb456bae2009-05-26 09:17:18 +020076static __u64 default_event_id[MAX_COUNTERS] = {
Ingo Molnar07800602009-04-20 15:00:56 +020077 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};
87static int default_interval = 100000;
88static int event_count[MAX_COUNTERS];
89static int fd[MAX_NR_CPUS][MAX_COUNTERS];
90
91static __u64 count_filter = 100;
92
Ingo Molnarb456bae2009-05-26 09:17:18 +020093static int target_pid = -1;
Ingo Molnar07800602009-04-20 15:00:56 +020094static int profile_cpu = -1;
95static int nr_cpus = 0;
Ingo Molnar07800602009-04-20 15:00:56 +020096static unsigned int realtime_prio = 0;
97static int group = 0;
98static unsigned int page_size;
99static unsigned int mmap_pages = 16;
100static int use_mmap = 0;
101static int use_munmap = 0;
Peter Zijlstraf5456a62009-05-15 15:19:29 +0200102static int freq = 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200103
Ingo Molnar07800602009-04-20 15:00:56 +0200104static char *sym_filter;
105static unsigned long filter_start;
106static unsigned long filter_end;
107
108static int delay_secs = 2;
109static int zero;
110static int dump_symtab;
111
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200112static const unsigned int default_count[] = {
Ingo Molnar07800602009-04-20 15:00:56 +0200113 1000000,
114 1000000,
115 10000,
116 10000,
117 1000000,
118 10000,
119};
120
Ingo Molnar07800602009-04-20 15:00:56 +0200121/*
122 * Symbols
123 */
124
125static uint64_t min_ip;
126static uint64_t max_ip = -1ll;
127
128struct sym_entry {
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300129 struct rb_node rb_node;
130 struct list_head node;
Ingo Molnar07800602009-04-20 15:00:56 +0200131 unsigned long count[MAX_COUNTERS];
132 int skip;
Ingo Molnar07800602009-04-20 15:00:56 +0200133};
134
Ingo Molnar07800602009-04-20 15:00:56 +0200135struct sym_entry *sym_filter_entry;
136
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300137struct dso *kernel_dso;
138
139/*
140 * Symbols will be added here in record_ip and will get out
141 * after decayed.
142 */
143static LIST_HEAD(active_symbols);
Ingo Molnar07800602009-04-20 15:00:56 +0200144
Ingo Molnar07800602009-04-20 15:00:56 +0200145/*
146 * Ordering weight: count-1 * count-2 * ... / count-n
147 */
148static double sym_weight(const struct sym_entry *sym)
149{
150 double weight;
151 int counter;
152
153 weight = sym->count[0];
154
155 for (counter = 1; counter < nr_counters-1; counter++)
156 weight *= sym->count[counter];
157
158 weight /= (sym->count[counter] + 1);
159
160 return weight;
161}
162
Ingo Molnar07800602009-04-20 15:00:56 +0200163static long events;
164static long userspace_events;
165static const char CONSOLE_CLEAR[] = "";
166
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300167static void list_insert_active_sym(struct sym_entry *syme)
168{
169 list_add(&syme->node, &active_symbols);
170}
171
172static void rb_insert_active_sym(struct rb_root *tree, struct sym_entry *se)
173{
174 struct rb_node **p = &tree->rb_node;
175 struct rb_node *parent = NULL;
176 struct sym_entry *iter;
177
178 while (*p != NULL) {
179 parent = *p;
180 iter = rb_entry(parent, struct sym_entry, rb_node);
181
182 if (sym_weight(se) > sym_weight(iter))
183 p = &(*p)->rb_left;
184 else
185 p = &(*p)->rb_right;
186 }
187
188 rb_link_node(&se->rb_node, parent, p);
189 rb_insert_color(&se->rb_node, tree);
190}
Ingo Molnar07800602009-04-20 15:00:56 +0200191
192static void print_sym_table(void)
193{
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300194 int printed, j;
Ingo Molnar07800602009-04-20 15:00:56 +0200195 int counter;
196 float events_per_sec = events/delay_secs;
197 float kevents_per_sec = (events-userspace_events)/delay_secs;
198 float sum_kevents = 0.0;
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300199 struct sym_entry *syme, *n;
200 struct rb_root tmp = RB_ROOT;
201 struct rb_node *nd;
Ingo Molnar07800602009-04-20 15:00:56 +0200202
203 events = userspace_events = 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200204
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300205 /* Sort the active symbols */
206 list_for_each_entry_safe(syme, n, &active_symbols, node) {
207 if (syme->count[0] != 0) {
208 rb_insert_active_sym(&tmp, syme);
209 sum_kevents += syme->count[0];
Mike Galbraithd94b9432009-05-25 09:57:56 +0200210
211 for (j = 0; j < nr_counters; j++)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300212 syme->count[j] = zero ? 0 : syme->count[j] * 7 / 8;
213 } else
214 list_del_init(&syme->node);
Mike Galbraithd94b9432009-05-25 09:57:56 +0200215 }
216
Ingo Molnar07800602009-04-20 15:00:56 +0200217 write(1, CONSOLE_CLEAR, strlen(CONSOLE_CLEAR));
218
219 printf(
220"------------------------------------------------------------------------------\n");
Mike Galbraithf91183f2009-05-26 15:25:34 +0200221 printf( " KernelTop:%8.0f irqs/sec kernel:%4.1f%% [",
Ingo Molnar07800602009-04-20 15:00:56 +0200222 events_per_sec,
Mike Galbraithf91183f2009-05-26 15:25:34 +0200223 100.0 - (100.0*((events_per_sec-kevents_per_sec)/events_per_sec)));
Ingo Molnar07800602009-04-20 15:00:56 +0200224
225 if (nr_counters == 1)
226 printf("%d ", event_count[0]);
227
228 for (counter = 0; counter < nr_counters; counter++) {
229 if (counter)
230 printf("/");
231
232 printf("%s", event_name(counter));
233 }
234
235 printf( "], ");
236
Ingo Molnarb456bae2009-05-26 09:17:18 +0200237 if (target_pid != -1)
238 printf(" (target_pid: %d", target_pid);
Ingo Molnar07800602009-04-20 15:00:56 +0200239 else
240 printf(" (all");
241
242 if (profile_cpu != -1)
243 printf(", cpu: %d)\n", profile_cpu);
244 else {
Ingo Molnarb456bae2009-05-26 09:17:18 +0200245 if (target_pid != -1)
Ingo Molnar07800602009-04-20 15:00:56 +0200246 printf(")\n");
247 else
248 printf(", %d CPUs)\n", nr_cpus);
249 }
250
251 printf("------------------------------------------------------------------------------\n\n");
252
253 if (nr_counters == 1)
254 printf(" events pcnt");
255 else
256 printf(" weight events pcnt");
257
258 printf(" RIP kernel function\n"
259 " ______ ______ _____ ________________ _______________\n\n"
260 );
261
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300262 for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
263 struct sym_entry *syme = rb_entry(nd, struct sym_entry, rb_node);
264 struct symbol *sym = (struct symbol *)(syme + 1);
Ingo Molnar07800602009-04-20 15:00:56 +0200265 float pcnt;
Ingo Molnar07800602009-04-20 15:00:56 +0200266
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300267 if (++printed > 18 || syme->count[0] < count_filter)
Mike Galbraithd94b9432009-05-25 09:57:56 +0200268 break;
Ingo Molnar07800602009-04-20 15:00:56 +0200269
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300270 pcnt = 100.0 - (100.0 * ((sum_kevents - syme->count[0]) /
271 sum_kevents));
Mike Galbraithd94b9432009-05-25 09:57:56 +0200272
273 if (nr_counters == 1)
274 printf("%19.2f - %4.1f%% - %016llx : %s\n",
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300275 sym_weight(syme),
276 pcnt, sym->start, sym->name);
Mike Galbraithd94b9432009-05-25 09:57:56 +0200277 else
278 printf("%8.1f %10ld - %4.1f%% - %016llx : %s\n",
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300279 sym_weight(syme), syme->count[0],
280 pcnt, sym->start, sym->name);
Ingo Molnar07800602009-04-20 15:00:56 +0200281 }
282
Ingo Molnar07800602009-04-20 15:00:56 +0200283 {
284 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
285
286 if (poll(&stdin_poll, 1, 0) == 1) {
287 printf("key pressed - exiting.\n");
288 exit(0);
289 }
290 }
291}
292
293static void *display_thread(void *arg)
294{
295 printf("KernelTop refresh period: %d seconds\n", delay_secs);
296
297 while (!sleep(delay_secs))
298 print_sym_table();
299
300 return NULL;
301}
302
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300303static int symbol_filter(struct dso *self, struct symbol *sym)
Ingo Molnar07800602009-04-20 15:00:56 +0200304{
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300305 static int filter_match;
306 struct sym_entry *syme;
307 const char *name = sym->name;
Ingo Molnar07800602009-04-20 15:00:56 +0200308
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300309 if (!strcmp(name, "_text") ||
310 !strcmp(name, "_etext") ||
311 !strcmp(name, "_sinittext") ||
312 !strncmp("init_module", name, 11) ||
313 !strncmp("cleanup_module", name, 14) ||
314 strstr(name, "_text_start") ||
315 strstr(name, "_text_end"))
Ingo Molnar07800602009-04-20 15:00:56 +0200316 return 1;
317
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300318 syme = dso__sym_priv(self, sym);
Ingo Molnar07800602009-04-20 15:00:56 +0200319 /* Tag events to be skipped. */
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300320 if (!strcmp("default_idle", name) ||
321 !strcmp("cpu_idle", name) ||
322 !strcmp("enter_idle", name) ||
323 !strcmp("exit_idle", name) ||
324 !strcmp("mwait_idle", name))
325 syme->skip = 1;
Ingo Molnar07800602009-04-20 15:00:56 +0200326
327 if (filter_match == 1) {
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300328 filter_end = sym->start;
Ingo Molnar07800602009-04-20 15:00:56 +0200329 filter_match = -1;
330 if (filter_end - filter_start > 10000) {
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300331 fprintf(stderr,
332 "hm, too large filter symbol <%s> - skipping.\n",
Ingo Molnar07800602009-04-20 15:00:56 +0200333 sym_filter);
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300334 fprintf(stderr, "symbol filter start: %016lx\n",
335 filter_start);
336 fprintf(stderr, " end: %016lx\n",
337 filter_end);
Ingo Molnar07800602009-04-20 15:00:56 +0200338 filter_end = filter_start = 0;
339 sym_filter = NULL;
340 sleep(1);
341 }
342 }
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300343
344 if (filter_match == 0 && sym_filter && !strcmp(name, sym_filter)) {
Ingo Molnar07800602009-04-20 15:00:56 +0200345 filter_match = 1;
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300346 filter_start = sym->start;
Ingo Molnar07800602009-04-20 15:00:56 +0200347 }
348
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300349
Ingo Molnar07800602009-04-20 15:00:56 +0200350 return 0;
351}
352
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300353static int parse_symbols(void)
Ingo Molnar07800602009-04-20 15:00:56 +0200354{
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300355 struct rb_node *node;
356 struct symbol *sym;
Ingo Molnar07800602009-04-20 15:00:56 +0200357
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300358 kernel_dso = dso__new("[kernel]", sizeof(struct sym_entry));
359 if (kernel_dso == NULL)
360 return -1;
Ingo Molnar07800602009-04-20 15:00:56 +0200361
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300362 if (dso__load_kernel(kernel_dso, NULL, symbol_filter) != 0)
363 goto out_delete_dso;
Ingo Molnar07800602009-04-20 15:00:56 +0200364
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300365 node = rb_first(&kernel_dso->syms);
366 sym = rb_entry(node, struct symbol, rb_node);
367 min_ip = sym->start;
Ingo Molnar07800602009-04-20 15:00:56 +0200368
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300369 node = rb_last(&kernel_dso->syms);
370 sym = rb_entry(node, struct symbol, rb_node);
371 max_ip = sym->start;
Ingo Molnar07800602009-04-20 15:00:56 +0200372
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300373 if (dump_symtab)
374 dso__fprintf(kernel_dso, stdout);
Ingo Molnar07800602009-04-20 15:00:56 +0200375
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300376 return 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200377
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300378out_delete_dso:
379 dso__delete(kernel_dso);
380 kernel_dso = NULL;
381 return -1;
Ingo Molnar07800602009-04-20 15:00:56 +0200382}
383
Ingo Molnar07800602009-04-20 15:00:56 +0200384#define TRACE_COUNT 3
385
Ingo Molnar07800602009-04-20 15:00:56 +0200386/*
387 * Binary search in the histogram table and record the hit:
388 */
389static void record_ip(uint64_t ip, int counter)
390{
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300391 struct symbol *sym = dso__find_symbol(kernel_dso, ip);
Ingo Molnar07800602009-04-20 15:00:56 +0200392
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300393 if (sym != NULL) {
394 struct sym_entry *syme = dso__sym_priv(kernel_dso, sym);
Ingo Molnar07800602009-04-20 15:00:56 +0200395
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300396 if (!syme->skip) {
397 syme->count[counter]++;
398 if (list_empty(&syme->node) || !syme->node.next)
399 list_insert_active_sym(syme);
400 return;
Ingo Molnar07800602009-04-20 15:00:56 +0200401 }
Ingo Molnar07800602009-04-20 15:00:56 +0200402 }
403
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300404 events--;
Ingo Molnar07800602009-04-20 15:00:56 +0200405}
406
407static void process_event(uint64_t ip, int counter)
408{
409 events++;
410
411 if (ip < min_ip || ip > max_ip) {
412 userspace_events++;
413 return;
414 }
415
416 record_ip(ip, counter);
417}
418
Ingo Molnar07800602009-04-20 15:00:56 +0200419struct mmap_data {
420 int counter;
421 void *base;
422 unsigned int mask;
423 unsigned int prev;
424};
425
426static unsigned int mmap_read_head(struct mmap_data *md)
427{
428 struct perf_counter_mmap_page *pc = md->base;
429 int head;
430
431 head = pc->data_head;
432 rmb();
433
434 return head;
435}
436
437struct timeval last_read, this_read;
438
439static void mmap_read(struct mmap_data *md)
440{
441 unsigned int head = mmap_read_head(md);
442 unsigned int old = md->prev;
443 unsigned char *data = md->base + page_size;
444 int diff;
445
446 gettimeofday(&this_read, NULL);
447
448 /*
449 * If we're further behind than half the buffer, there's a chance
450 * the writer will bite our tail and screw up the events under us.
451 *
452 * If we somehow ended up ahead of the head, we got messed up.
453 *
454 * In either case, truncate and restart at head.
455 */
456 diff = head - old;
457 if (diff > md->mask / 2 || diff < 0) {
458 struct timeval iv;
459 unsigned long msecs;
460
461 timersub(&this_read, &last_read, &iv);
462 msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
463
464 fprintf(stderr, "WARNING: failed to keep up with mmap data."
465 " Last read %lu msecs ago.\n", msecs);
466
467 /*
468 * head points to a known good entry, start there.
469 */
470 old = head;
471 }
472
473 last_read = this_read;
474
475 for (; old != head;) {
476 struct ip_event {
477 struct perf_event_header header;
478 __u64 ip;
Ingo Molnarb456bae2009-05-26 09:17:18 +0200479 __u32 pid, target_pid;
Ingo Molnar07800602009-04-20 15:00:56 +0200480 };
481 struct mmap_event {
482 struct perf_event_header header;
Ingo Molnarb456bae2009-05-26 09:17:18 +0200483 __u32 pid, target_pid;
Ingo Molnar07800602009-04-20 15:00:56 +0200484 __u64 start;
485 __u64 len;
486 __u64 pgoff;
487 char filename[PATH_MAX];
488 };
489
490 typedef union event_union {
491 struct perf_event_header header;
492 struct ip_event ip;
493 struct mmap_event mmap;
494 } event_t;
495
496 event_t *event = (event_t *)&data[old & md->mask];
497
498 event_t event_copy;
499
Ingo Molnar6f06ccb2009-04-20 15:22:22 +0200500 size_t size = event->header.size;
Ingo Molnar07800602009-04-20 15:00:56 +0200501
502 /*
503 * Event straddles the mmap boundary -- header should always
504 * be inside due to u64 alignment of output.
505 */
506 if ((old & md->mask) + size != ((old + size) & md->mask)) {
507 unsigned int offset = old;
508 unsigned int len = min(sizeof(*event), size), cpy;
509 void *dst = &event_copy;
510
511 do {
512 cpy = min(md->mask + 1 - (offset & md->mask), len);
513 memcpy(dst, &data[offset & md->mask], cpy);
514 offset += cpy;
515 dst += cpy;
516 len -= cpy;
517 } while (len);
518
519 event = &event_copy;
520 }
521
522 old += size;
523
524 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) {
525 if (event->header.type & PERF_RECORD_IP)
526 process_event(event->ip.ip, md->counter);
527 } else {
528 switch (event->header.type) {
529 case PERF_EVENT_MMAP:
530 case PERF_EVENT_MUNMAP:
531 printf("%s: %Lu %Lu %Lu %s\n",
532 event->header.type == PERF_EVENT_MMAP
533 ? "mmap" : "munmap",
534 event->mmap.start,
535 event->mmap.len,
536 event->mmap.pgoff,
537 event->mmap.filename);
538 break;
539 }
540 }
541 }
542
543 md->prev = old;
544}
545
Mike Galbraithc2990a22009-05-24 08:35:49 +0200546static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
547static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
548
Ingo Molnarb456bae2009-05-26 09:17:18 +0200549static int __cmd_top(void)
Ingo Molnar07800602009-04-20 15:00:56 +0200550{
Ingo Molnar07800602009-04-20 15:00:56 +0200551 struct perf_counter_hw_event hw_event;
552 pthread_t thread;
553 int i, counter, group_fd, nr_poll = 0;
554 unsigned int cpu;
555 int ret;
556
Ingo Molnar07800602009-04-20 15:00:56 +0200557 for (i = 0; i < nr_cpus; i++) {
558 group_fd = -1;
559 for (counter = 0; counter < nr_counters; counter++) {
560
561 cpu = profile_cpu;
Ingo Molnarb456bae2009-05-26 09:17:18 +0200562 if (target_pid == -1 && profile_cpu == -1)
Ingo Molnar07800602009-04-20 15:00:56 +0200563 cpu = i;
564
565 memset(&hw_event, 0, sizeof(hw_event));
566 hw_event.config = event_id[counter];
567 hw_event.irq_period = event_count[counter];
568 hw_event.record_type = PERF_RECORD_IP | PERF_RECORD_TID;
Mike Galbraithf91183f2009-05-26 15:25:34 +0200569 hw_event.nmi = 1;
Ingo Molnar07800602009-04-20 15:00:56 +0200570 hw_event.mmap = use_mmap;
571 hw_event.munmap = use_munmap;
Peter Zijlstraf5456a62009-05-15 15:19:29 +0200572 hw_event.freq = freq;
Ingo Molnar07800602009-04-20 15:00:56 +0200573
Ingo Molnarb456bae2009-05-26 09:17:18 +0200574 fd[i][counter] = sys_perf_counter_open(&hw_event, target_pid, cpu, group_fd, 0);
Ingo Molnar07800602009-04-20 15:00:56 +0200575 if (fd[i][counter] < 0) {
576 int err = errno;
577 printf("kerneltop error: syscall returned with %d (%s)\n",
578 fd[i][counter], strerror(err));
579 if (err == EPERM)
580 printf("Are you root?\n");
581 exit(-1);
582 }
583 assert(fd[i][counter] >= 0);
584 fcntl(fd[i][counter], F_SETFL, O_NONBLOCK);
585
586 /*
587 * First counter acts as the group leader:
588 */
589 if (group && group_fd == -1)
590 group_fd = fd[i][counter];
591
592 event_array[nr_poll].fd = fd[i][counter];
593 event_array[nr_poll].events = POLLIN;
594 nr_poll++;
595
596 mmap_array[i][counter].counter = counter;
597 mmap_array[i][counter].prev = 0;
598 mmap_array[i][counter].mask = mmap_pages*page_size - 1;
599 mmap_array[i][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
600 PROT_READ, MAP_SHARED, fd[i][counter], 0);
601 if (mmap_array[i][counter].base == MAP_FAILED) {
602 printf("kerneltop error: failed to mmap with %d (%s)\n",
603 errno, strerror(errno));
604 exit(-1);
605 }
606 }
607 }
608
609 if (pthread_create(&thread, NULL, display_thread, NULL)) {
610 printf("Could not create display thread.\n");
611 exit(-1);
612 }
613
614 if (realtime_prio) {
615 struct sched_param param;
616
617 param.sched_priority = realtime_prio;
618 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
619 printf("Could not set realtime priority.\n");
620 exit(-1);
621 }
622 }
623
624 while (1) {
625 int hits = events;
626
627 for (i = 0; i < nr_cpus; i++) {
628 for (counter = 0; counter < nr_counters; counter++)
629 mmap_read(&mmap_array[i][counter]);
630 }
631
632 if (hits == events)
633 ret = poll(event_array, nr_poll, 100);
634 }
635
636 return 0;
637}
Ingo Molnarb456bae2009-05-26 09:17:18 +0200638
639static const char * const top_usage[] = {
640 "perf top [<options>]",
641 NULL
642};
643
644static char events_help_msg[EVENTS_HELP_MAX];
645
646static const struct option options[] = {
647 OPT_CALLBACK('e', "event", NULL, "event",
648 events_help_msg, parse_events),
649 OPT_INTEGER('c', "count", &default_interval,
650 "event period to sample"),
651 OPT_INTEGER('p', "pid", &target_pid,
652 "profile events on existing pid"),
653 OPT_BOOLEAN('a', "all-cpus", &system_wide,
654 "system-wide collection from all CPUs"),
655 OPT_INTEGER('C', "CPU", &profile_cpu,
656 "CPU to profile on"),
657 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
658 "number of mmap data pages"),
659 OPT_INTEGER('r', "realtime", &realtime_prio,
660 "collect data with this RT SCHED_FIFO priority"),
Mike Galbraithdb20c002009-05-26 15:25:34 +0200661 OPT_INTEGER('d', "delay", &delay_secs,
Ingo Molnarb456bae2009-05-26 09:17:18 +0200662 "number of seconds to delay between refreshes"),
663 OPT_BOOLEAN('D', "dump-symtab", &dump_symtab,
664 "dump the symbol table used for profiling"),
665 OPT_INTEGER('f', "--count-filter", &count_filter,
666 "only display functions with more events than this"),
667 OPT_BOOLEAN('g', "group", &group,
668 "put the counters into a counter group"),
669 OPT_STRING('s', "sym-filter", &sym_filter, "pattern",
670 "only display symbols matchig this pattern"),
671 OPT_BOOLEAN('z', "zero", &group,
672 "zero history across updates"),
673 OPT_BOOLEAN('M', "use-mmap", &use_mmap,
674 "track mmap events"),
675 OPT_BOOLEAN('U', "use-munmap", &use_munmap,
676 "track munmap events"),
677 OPT_INTEGER('F', "--freq", &freq,
678 "profile at this frequency"),
679 OPT_END()
680};
681
682int cmd_top(int argc, const char **argv, const char *prefix)
683{
684 int counter;
685
686 page_size = sysconf(_SC_PAGE_SIZE);
687
688 create_events_help(events_help_msg);
689 memcpy(event_id, default_event_id, sizeof(default_event_id));
690
691 argc = parse_options(argc, argv, options, top_usage, 0);
692 if (argc)
693 usage_with_options(top_usage, options);
694
695 if (freq) {
696 default_interval = freq;
697 freq = 1;
698 }
699
700 /* CPU and PID are mutually exclusive */
701 if (target_pid != -1 && profile_cpu != -1) {
702 printf("WARNING: PID switch overriding CPU\n");
703 sleep(1);
704 profile_cpu = -1;
705 }
706
707 if (!nr_counters) {
708 nr_counters = 1;
709 event_id[0] = 0;
710 }
711
712 for (counter = 0; counter < nr_counters; counter++) {
713 if (event_count[counter])
714 continue;
715
716 event_count[counter] = default_interval;
717 }
718
719 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
720 assert(nr_cpus <= MAX_NR_CPUS);
721 assert(nr_cpus >= 0);
722
723 if (target_pid != -1 || profile_cpu != -1)
724 nr_cpus = 1;
725
726 parse_symbols();
727
728 return __cmd_top();
729}