cacaa3c2518d083efe40eccecfec5fc918e6c8e6
[pandora-kernel.git] / Documentation / perf_counter / builtin-top.c
1 /*
2  * kerneltop.c: show top kernel functions - performance counters showcase
3
4    Build with:
5
6      make -C Documentation/perf_counter/
7
8    Sample output:
9
10 ------------------------------------------------------------------------------
11  KernelTop:    2669 irqs/sec  [cache-misses/cache-refs],  (all, cpu: 2)
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
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
45
46 #include "perf.h"
47 #include "util/util.h"
48 #include "util/util.h"
49 #include "util/parse-options.h"
50 #include "util/parse-events.h"
51
52 #include <assert.h>
53 #include <fcntl.h>
54
55 #include <stdio.h>
56
57 #include <errno.h>
58 #include <time.h>
59 #include <sched.h>
60 #include <pthread.h>
61
62 #include <sys/syscall.h>
63 #include <sys/ioctl.h>
64 #include <sys/poll.h>
65 #include <sys/prctl.h>
66 #include <sys/wait.h>
67 #include <sys/uio.h>
68 #include <sys/mman.h>
69
70 #include <linux/unistd.h>
71 #include <linux/types.h>
72
73 static int                      system_wide                     =  0;
74
75 static __u64                    default_event_id[MAX_COUNTERS]          = {
76         EID(PERF_TYPE_SOFTWARE, PERF_COUNT_TASK_CLOCK),
77         EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CONTEXT_SWITCHES),
78         EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CPU_MIGRATIONS),
79         EID(PERF_TYPE_SOFTWARE, PERF_COUNT_PAGE_FAULTS),
80
81         EID(PERF_TYPE_HARDWARE, PERF_COUNT_CPU_CYCLES),
82         EID(PERF_TYPE_HARDWARE, PERF_COUNT_INSTRUCTIONS),
83         EID(PERF_TYPE_HARDWARE, PERF_COUNT_CACHE_REFERENCES),
84         EID(PERF_TYPE_HARDWARE, PERF_COUNT_CACHE_MISSES),
85 };
86 static int                      default_interval = 100000;
87 static int                      event_count[MAX_COUNTERS];
88 static int                      fd[MAX_NR_CPUS][MAX_COUNTERS];
89
90 static __u64                    count_filter                   = 100;
91
92 static int                      target_pid                              = -1;
93 static int                      profile_cpu                     = -1;
94 static int                      nr_cpus                         =  0;
95 static unsigned int             realtime_prio                   =  0;
96 static int                      group                           =  0;
97 static unsigned int             page_size;
98 static unsigned int             mmap_pages                      =  16;
99 static int                      use_mmap                        = 0;
100 static int                      use_munmap                      = 0;
101 static int                      freq                            = 0;
102
103 static char                     *sym_filter;
104 static unsigned long            filter_start;
105 static unsigned long            filter_end;
106
107 static int                      delay_secs                      =  2;
108 static int                      zero;
109 static int                      dump_symtab;
110
111 static const unsigned int default_count[] = {
112         1000000,
113         1000000,
114           10000,
115           10000,
116         1000000,
117           10000,
118 };
119
120 /*
121  * Symbols
122  */
123
124 static uint64_t                 min_ip;
125 static uint64_t                 max_ip = -1ll;
126
127 struct sym_entry {
128         unsigned long long      addr;
129         char                    *sym;
130         unsigned long           count[MAX_COUNTERS];
131         int                     skip;
132 };
133
134 #define MAX_SYMS                100000
135
136 static int sym_table_count;
137
138 struct sym_entry                *sym_filter_entry;
139
140 static struct sym_entry         sym_table[MAX_SYMS];
141
142 /*
143  * Ordering weight: count-1 * count-2 * ... / count-n
144  */
145 static double sym_weight(const struct sym_entry *sym)
146 {
147         double weight;
148         int counter;
149
150         weight = sym->count[0];
151
152         for (counter = 1; counter < nr_counters-1; counter++)
153                 weight *= sym->count[counter];
154
155         weight /= (sym->count[counter] + 1);
156
157         return weight;
158 }
159
160 static int compare(const void *__sym1, const void *__sym2)
161 {
162         const struct sym_entry *sym1 = __sym1, *sym2 = __sym2;
163
164         return sym_weight(sym1) < sym_weight(sym2);
165 }
166
167 static long                     events;
168 static long                     userspace_events;
169 static const char               CONSOLE_CLEAR[] = "\e[H\e[2J";
170
171 static struct sym_entry         tmp[MAX_SYMS];
172
173 static void print_sym_table(void)
174 {
175         int i, j, active_count, printed;
176         int counter;
177         float events_per_sec = events/delay_secs;
178         float kevents_per_sec = (events-userspace_events)/delay_secs;
179         float sum_kevents = 0.0;
180
181         events = userspace_events = 0;
182
183         /* Iterate over symbol table and copy/tally/decay active symbols. */
184         for (i = 0, active_count = 0; i < sym_table_count; i++) {
185                 if (sym_table[i].count[0]) {
186                         tmp[active_count++] = sym_table[i];
187                         sum_kevents += sym_table[i].count[0];
188
189                         for (j = 0; j < nr_counters; j++)
190                                 sym_table[i].count[j] = zero ? 0 : sym_table[i].count[j] * 7 / 8;
191                 }
192         }
193
194         qsort(tmp, active_count + 1, sizeof(tmp[0]), compare);
195
196         write(1, CONSOLE_CLEAR, strlen(CONSOLE_CLEAR));
197
198         printf(
199 "------------------------------------------------------------------------------\n");
200         printf( " KernelTop:%8.0f irqs/sec  kernel:%4.1f%% [",
201                 events_per_sec,
202                 100.0 - (100.0*((events_per_sec-kevents_per_sec)/events_per_sec)));
203
204         if (nr_counters == 1)
205                 printf("%d ", event_count[0]);
206
207         for (counter = 0; counter < nr_counters; counter++) {
208                 if (counter)
209                         printf("/");
210
211                 printf("%s", event_name(counter));
212         }
213
214         printf( "], ");
215
216         if (target_pid != -1)
217                 printf(" (target_pid: %d", target_pid);
218         else
219                 printf(" (all");
220
221         if (profile_cpu != -1)
222                 printf(", cpu: %d)\n", profile_cpu);
223         else {
224                 if (target_pid != -1)
225                         printf(")\n");
226                 else
227                         printf(", %d CPUs)\n", nr_cpus);
228         }
229
230         printf("------------------------------------------------------------------------------\n\n");
231
232         if (nr_counters == 1)
233                 printf("             events    pcnt");
234         else
235                 printf("  weight     events    pcnt");
236
237         printf("         RIP          kernel function\n"
238                        "  ______     ______   _____   ________________   _______________\n\n"
239         );
240
241         for (i = 0, printed = 0; i < active_count; i++) {
242                 float pcnt;
243
244                 if (++printed > 18 || tmp[i].count[0] < count_filter)
245                         break;
246
247                 pcnt = 100.0 - (100.0*((sum_kevents-tmp[i].count[0])/sum_kevents));
248
249                 if (nr_counters == 1)
250                         printf("%19.2f - %4.1f%% - %016llx : %s\n",
251                                 sym_weight(tmp + i),
252                                 pcnt, tmp[i].addr, tmp[i].sym);
253                 else
254                         printf("%8.1f %10ld - %4.1f%% - %016llx : %s\n",
255                                 sym_weight(tmp + i),
256                                 tmp[i].count[0],
257                                 pcnt, tmp[i].addr, tmp[i].sym);
258         }
259
260         {
261                 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
262
263                 if (poll(&stdin_poll, 1, 0) == 1) {
264                         printf("key pressed - exiting.\n");
265                         exit(0);
266                 }
267         }
268 }
269
270 static void *display_thread(void *arg)
271 {
272         printf("KernelTop refresh period: %d seconds\n", delay_secs);
273
274         while (!sleep(delay_secs))
275                 print_sym_table();
276
277         return NULL;
278 }
279
280 static int read_symbol(FILE *in, struct sym_entry *s)
281 {
282         static int filter_match = 0;
283         char *sym, stype;
284         char str[500];
285         int rc, pos;
286
287         rc = fscanf(in, "%llx %c %499s", &s->addr, &stype, str);
288         if (rc == EOF)
289                 return -1;
290
291         assert(rc == 3);
292
293         /* skip until end of line: */
294         pos = strlen(str);
295         do {
296                 rc = fgetc(in);
297                 if (rc == '\n' || rc == EOF || pos >= 499)
298                         break;
299                 str[pos] = rc;
300                 pos++;
301         } while (1);
302         str[pos] = 0;
303
304         sym = str;
305
306         /* Filter out known duplicates and non-text symbols. */
307         if (!strcmp(sym, "_text"))
308                 return 1;
309         if (!min_ip && !strcmp(sym, "_stext"))
310                 return 1;
311         if (!strcmp(sym, "_etext") || !strcmp(sym, "_sinittext"))
312                 return 1;
313         if (stype != 'T' && stype != 't')
314                 return 1;
315         if (!strncmp("init_module", sym, 11) || !strncmp("cleanup_module", sym, 14))
316                 return 1;
317         if (strstr(sym, "_text_start") || strstr(sym, "_text_end"))
318                 return 1;
319
320         s->sym = malloc(strlen(str)+1);
321         assert(s->sym);
322
323         strcpy((char *)s->sym, str);
324         s->skip = 0;
325
326         /* Tag events to be skipped. */
327         if (!strcmp("default_idle", s->sym) || !strcmp("cpu_idle", s->sym))
328                 s->skip = 1;
329         else if (!strcmp("enter_idle", s->sym) || !strcmp("exit_idle", s->sym))
330                 s->skip = 1;
331         else if (!strcmp("mwait_idle", s->sym))
332                 s->skip = 1;
333
334         if (filter_match == 1) {
335                 filter_end = s->addr;
336                 filter_match = -1;
337                 if (filter_end - filter_start > 10000) {
338                         printf("hm, too large filter symbol <%s> - skipping.\n",
339                                 sym_filter);
340                         printf("symbol filter start: %016lx\n", filter_start);
341                         printf("                end: %016lx\n", filter_end);
342                         filter_end = filter_start = 0;
343                         sym_filter = NULL;
344                         sleep(1);
345                 }
346         }
347         if (filter_match == 0 && sym_filter && !strcmp(s->sym, sym_filter)) {
348                 filter_match = 1;
349                 filter_start = s->addr;
350         }
351
352         return 0;
353 }
354
355 static int compare_addr(const void *__sym1, const void *__sym2)
356 {
357         const struct sym_entry *sym1 = __sym1, *sym2 = __sym2;
358
359         return sym1->addr > sym2->addr;
360 }
361
362 static void sort_symbol_table(void)
363 {
364         int i, dups;
365
366         do {
367                 qsort(sym_table, sym_table_count, sizeof(sym_table[0]), compare_addr);
368                 for (i = 0, dups = 0; i < sym_table_count; i++) {
369                         if (sym_table[i].addr == sym_table[i+1].addr) {
370                                 sym_table[i+1].addr = -1ll;
371                                 dups++;
372                         }
373                 }
374                 sym_table_count -= dups;
375         } while(dups);
376 }
377
378 static void parse_symbols(void)
379 {
380         struct sym_entry *last;
381
382         FILE *kallsyms = fopen("/proc/kallsyms", "r");
383
384         if (!kallsyms) {
385                 printf("Could not open /proc/kallsyms - no CONFIG_KALLSYMS_ALL=y?\n");
386                 exit(-1);
387         }
388
389         while (!feof(kallsyms)) {
390                 if (read_symbol(kallsyms, &sym_table[sym_table_count]) == 0) {
391                         sym_table_count++;
392                         assert(sym_table_count <= MAX_SYMS);
393                 }
394         }
395
396         sort_symbol_table();
397         min_ip = sym_table[0].addr;
398         max_ip = sym_table[sym_table_count-1].addr;
399         last = sym_table + sym_table_count++;
400
401         last->addr = -1ll;
402         last->sym = "<end>";
403
404         if (filter_end) {
405                 int count;
406                 for (count=0; count < sym_table_count; count ++) {
407                         if (!strcmp(sym_table[count].sym, sym_filter)) {
408                                 sym_filter_entry = &sym_table[count];
409                                 break;
410                         }
411                 }
412         }
413         if (dump_symtab) {
414                 int i;
415
416                 for (i = 0; i < sym_table_count; i++)
417                         fprintf(stderr, "%llx %s\n",
418                                 sym_table[i].addr, sym_table[i].sym);
419         }
420 }
421
422 #define TRACE_COUNT     3
423
424 /*
425  * Binary search in the histogram table and record the hit:
426  */
427 static void record_ip(uint64_t ip, int counter)
428 {
429         int left_idx, middle_idx, right_idx, idx;
430         unsigned long left, middle, right;
431
432         left_idx = 0;
433         right_idx = sym_table_count-1;
434         assert(ip <= max_ip && ip >= min_ip);
435
436         while (left_idx + 1 < right_idx) {
437                 middle_idx = (left_idx + right_idx) / 2;
438
439                 left   = sym_table[  left_idx].addr;
440                 middle = sym_table[middle_idx].addr;
441                 right  = sym_table[ right_idx].addr;
442
443                 if (!(left <= middle && middle <= right)) {
444                         printf("%016lx...\n%016lx...\n%016lx\n", left, middle, right);
445                         printf("%d %d %d\n", left_idx, middle_idx, right_idx);
446                 }
447                 assert(left <= middle && middle <= right);
448                 if (!(left <= ip && ip <= right)) {
449                         printf(" left: %016lx\n", left);
450                         printf("   ip: %016lx\n", (unsigned long)ip);
451                         printf("right: %016lx\n", right);
452                 }
453                 assert(left <= ip && ip <= right);
454                 /*
455                  * [ left .... target .... middle .... right ]
456                  *   => right := middle
457                  */
458                 if (ip < middle) {
459                         right_idx = middle_idx;
460                         continue;
461                 }
462                 /*
463                  * [ left .... middle ... target ... right ]
464                  *   => left := middle
465                  */
466                 left_idx = middle_idx;
467         }
468
469         idx = left_idx;
470
471         if (!sym_table[idx].skip)
472                 sym_table[idx].count[counter]++;
473         else events--;
474 }
475
476 static void process_event(uint64_t ip, int counter)
477 {
478         events++;
479
480         if (ip < min_ip || ip > max_ip) {
481                 userspace_events++;
482                 return;
483         }
484
485         record_ip(ip, counter);
486 }
487
488 struct mmap_data {
489         int counter;
490         void *base;
491         unsigned int mask;
492         unsigned int prev;
493 };
494
495 static unsigned int mmap_read_head(struct mmap_data *md)
496 {
497         struct perf_counter_mmap_page *pc = md->base;
498         int head;
499
500         head = pc->data_head;
501         rmb();
502
503         return head;
504 }
505
506 struct timeval last_read, this_read;
507
508 static void mmap_read(struct mmap_data *md)
509 {
510         unsigned int head = mmap_read_head(md);
511         unsigned int old = md->prev;
512         unsigned char *data = md->base + page_size;
513         int diff;
514
515         gettimeofday(&this_read, NULL);
516
517         /*
518          * If we're further behind than half the buffer, there's a chance
519          * the writer will bite our tail and screw up the events under us.
520          *
521          * If we somehow ended up ahead of the head, we got messed up.
522          *
523          * In either case, truncate and restart at head.
524          */
525         diff = head - old;
526         if (diff > md->mask / 2 || diff < 0) {
527                 struct timeval iv;
528                 unsigned long msecs;
529
530                 timersub(&this_read, &last_read, &iv);
531                 msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
532
533                 fprintf(stderr, "WARNING: failed to keep up with mmap data."
534                                 "  Last read %lu msecs ago.\n", msecs);
535
536                 /*
537                  * head points to a known good entry, start there.
538                  */
539                 old = head;
540         }
541
542         last_read = this_read;
543
544         for (; old != head;) {
545                 struct ip_event {
546                         struct perf_event_header header;
547                         __u64 ip;
548                         __u32 pid, target_pid;
549                 };
550                 struct mmap_event {
551                         struct perf_event_header header;
552                         __u32 pid, target_pid;
553                         __u64 start;
554                         __u64 len;
555                         __u64 pgoff;
556                         char filename[PATH_MAX];
557                 };
558
559                 typedef union event_union {
560                         struct perf_event_header header;
561                         struct ip_event ip;
562                         struct mmap_event mmap;
563                 } event_t;
564
565                 event_t *event = (event_t *)&data[old & md->mask];
566
567                 event_t event_copy;
568
569                 size_t size = event->header.size;
570
571                 /*
572                  * Event straddles the mmap boundary -- header should always
573                  * be inside due to u64 alignment of output.
574                  */
575                 if ((old & md->mask) + size != ((old + size) & md->mask)) {
576                         unsigned int offset = old;
577                         unsigned int len = min(sizeof(*event), size), cpy;
578                         void *dst = &event_copy;
579
580                         do {
581                                 cpy = min(md->mask + 1 - (offset & md->mask), len);
582                                 memcpy(dst, &data[offset & md->mask], cpy);
583                                 offset += cpy;
584                                 dst += cpy;
585                                 len -= cpy;
586                         } while (len);
587
588                         event = &event_copy;
589                 }
590
591                 old += size;
592
593                 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) {
594                         if (event->header.type & PERF_RECORD_IP)
595                                 process_event(event->ip.ip, md->counter);
596                 } else {
597                         switch (event->header.type) {
598                                 case PERF_EVENT_MMAP:
599                                 case PERF_EVENT_MUNMAP:
600                                         printf("%s: %Lu %Lu %Lu %s\n",
601                                                         event->header.type == PERF_EVENT_MMAP
602                                                         ? "mmap" : "munmap",
603                                                         event->mmap.start,
604                                                         event->mmap.len,
605                                                         event->mmap.pgoff,
606                                                         event->mmap.filename);
607                                         break;
608                         }
609                 }
610         }
611
612         md->prev = old;
613 }
614
615 static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
616 static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
617
618 static int __cmd_top(void)
619 {
620         struct perf_counter_hw_event hw_event;
621         pthread_t thread;
622         int i, counter, group_fd, nr_poll = 0;
623         unsigned int cpu;
624         int ret;
625
626         for (i = 0; i < nr_cpus; i++) {
627                 group_fd = -1;
628                 for (counter = 0; counter < nr_counters; counter++) {
629
630                         cpu     = profile_cpu;
631                         if (target_pid == -1 && profile_cpu == -1)
632                                 cpu = i;
633
634                         memset(&hw_event, 0, sizeof(hw_event));
635                         hw_event.config         = event_id[counter];
636                         hw_event.irq_period     = event_count[counter];
637                         hw_event.record_type    = PERF_RECORD_IP | PERF_RECORD_TID;
638                         hw_event.nmi            = 1;
639                         hw_event.mmap           = use_mmap;
640                         hw_event.munmap         = use_munmap;
641                         hw_event.freq           = freq;
642
643                         fd[i][counter] = sys_perf_counter_open(&hw_event, target_pid, cpu, group_fd, 0);
644                         if (fd[i][counter] < 0) {
645                                 int err = errno;
646                                 printf("kerneltop error: syscall returned with %d (%s)\n",
647                                         fd[i][counter], strerror(err));
648                                 if (err == EPERM)
649                                         printf("Are you root?\n");
650                                 exit(-1);
651                         }
652                         assert(fd[i][counter] >= 0);
653                         fcntl(fd[i][counter], F_SETFL, O_NONBLOCK);
654
655                         /*
656                          * First counter acts as the group leader:
657                          */
658                         if (group && group_fd == -1)
659                                 group_fd = fd[i][counter];
660
661                         event_array[nr_poll].fd = fd[i][counter];
662                         event_array[nr_poll].events = POLLIN;
663                         nr_poll++;
664
665                         mmap_array[i][counter].counter = counter;
666                         mmap_array[i][counter].prev = 0;
667                         mmap_array[i][counter].mask = mmap_pages*page_size - 1;
668                         mmap_array[i][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
669                                         PROT_READ, MAP_SHARED, fd[i][counter], 0);
670                         if (mmap_array[i][counter].base == MAP_FAILED) {
671                                 printf("kerneltop error: failed to mmap with %d (%s)\n",
672                                                 errno, strerror(errno));
673                                 exit(-1);
674                         }
675                 }
676         }
677
678         if (pthread_create(&thread, NULL, display_thread, NULL)) {
679                 printf("Could not create display thread.\n");
680                 exit(-1);
681         }
682
683         if (realtime_prio) {
684                 struct sched_param param;
685
686                 param.sched_priority = realtime_prio;
687                 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
688                         printf("Could not set realtime priority.\n");
689                         exit(-1);
690                 }
691         }
692
693         while (1) {
694                 int hits = events;
695
696                 for (i = 0; i < nr_cpus; i++) {
697                         for (counter = 0; counter < nr_counters; counter++)
698                                 mmap_read(&mmap_array[i][counter]);
699                 }
700
701                 if (hits == events)
702                         ret = poll(event_array, nr_poll, 100);
703         }
704
705         return 0;
706 }
707
708 static const char * const top_usage[] = {
709         "perf top [<options>]",
710         NULL
711 };
712
713 static char events_help_msg[EVENTS_HELP_MAX];
714
715 static const struct option options[] = {
716         OPT_CALLBACK('e', "event", NULL, "event",
717                      events_help_msg, parse_events),
718         OPT_INTEGER('c', "count", &default_interval,
719                     "event period to sample"),
720         OPT_INTEGER('p', "pid", &target_pid,
721                     "profile events on existing pid"),
722         OPT_BOOLEAN('a', "all-cpus", &system_wide,
723                             "system-wide collection from all CPUs"),
724         OPT_INTEGER('C', "CPU", &profile_cpu,
725                     "CPU to profile on"),
726         OPT_INTEGER('m', "mmap-pages", &mmap_pages,
727                     "number of mmap data pages"),
728         OPT_INTEGER('r', "realtime", &realtime_prio,
729                     "collect data with this RT SCHED_FIFO priority"),
730         OPT_INTEGER('d', "delay", &realtime_prio,
731                     "number of seconds to delay between refreshes"),
732         OPT_BOOLEAN('D', "dump-symtab", &dump_symtab,
733                             "dump the symbol table used for profiling"),
734         OPT_INTEGER('f', "--count-filter", &count_filter,
735                     "only display functions with more events than this"),
736         OPT_BOOLEAN('g', "group", &group,
737                             "put the counters into a counter group"),
738         OPT_STRING('s', "sym-filter", &sym_filter, "pattern",
739                     "only display symbols matchig this pattern"),
740         OPT_BOOLEAN('z', "zero", &group,
741                     "zero history across updates"),
742         OPT_BOOLEAN('M', "use-mmap", &use_mmap,
743                     "track mmap events"),
744         OPT_BOOLEAN('U', "use-munmap", &use_munmap,
745                     "track munmap events"),
746         OPT_INTEGER('F', "--freq", &freq,
747                     "profile at this frequency"),
748         OPT_END()
749 };
750
751 int cmd_top(int argc, const char **argv, const char *prefix)
752 {
753         int counter;
754
755         page_size = sysconf(_SC_PAGE_SIZE);
756
757         create_events_help(events_help_msg);
758         memcpy(event_id, default_event_id, sizeof(default_event_id));
759
760         argc = parse_options(argc, argv, options, top_usage, 0);
761         if (argc)
762                 usage_with_options(top_usage, options);
763
764         if (freq) {
765                 default_interval = freq;
766                 freq = 1;
767         }
768
769         /* CPU and PID are mutually exclusive */
770         if (target_pid != -1 && profile_cpu != -1) {
771                 printf("WARNING: PID switch overriding CPU\n");
772                 sleep(1);
773                 profile_cpu = -1;
774         }
775
776         if (!nr_counters) {
777                 nr_counters = 1;
778                 event_id[0] = 0;
779         }
780
781         for (counter = 0; counter < nr_counters; counter++) {
782                 if (event_count[counter])
783                         continue;
784
785                 event_count[counter] = default_interval;
786         }
787
788         nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
789         assert(nr_cpus <= MAX_NR_CPUS);
790         assert(nr_cpus >= 0);
791
792         if (target_pid != -1 || profile_cpu != -1)
793                 nr_cpus = 1;
794
795         parse_symbols();
796
797         return __cmd_top();
798 }