4 * Builtin top command: Display a continuously updated profile of
5 * any workload, CPU or specific PID.
7 * Copyright (C) 2008, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
9 * Improvements and fixes by:
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>
17 * Released under the GPL v2. (and only v2, not any later version)
23 #include "util/symbol.h"
24 #include "util/color.h"
25 #include "util/thread.h"
26 #include "util/util.h"
27 #include <linux/rbtree.h>
28 #include "util/parse-options.h"
29 #include "util/parse-events.h"
31 #include "util/debug.h"
45 #include <sys/syscall.h>
46 #include <sys/ioctl.h>
48 #include <sys/prctl.h>
53 #include <linux/unistd.h>
54 #include <linux/types.h>
56 static int fd
[MAX_NR_CPUS
][MAX_COUNTERS
];
58 static int system_wide
= 0;
60 static int default_interval
= 100000;
62 static int count_filter
= 5;
63 static int print_entries
= 15;
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;
71 static unsigned int page_size
;
72 static unsigned int mmap_pages
= 16;
73 static int freq
= 1000; /* 1 KHz */
75 static int delay_secs
= 2;
77 static int dump_symtab
= 0;
85 unsigned long count
[MAX_COUNTERS
];
87 struct source_line
*next
;
90 static char *sym_filter
= NULL
;
91 struct sym_entry
*sym_filter_entry
= NULL
;
92 static int sym_pcnt_filter
= 5;
93 static int sym_counter
= 0;
94 static int display_weighted
= -1;
101 struct rb_node rb_node
;
102 struct list_head node
;
103 unsigned long count
[MAX_COUNTERS
];
104 unsigned long snap_count
;
108 struct source_line
*source
;
109 struct source_line
*lines
;
110 struct source_line
**lines_tail
;
111 pthread_mutex_t source_lock
;
118 static void parse_source(struct sym_entry
*syme
)
123 char command
[PATH_MAX
*2];
131 pthread_mutex_lock(&syme
->source_lock
);
135 sym
= (struct symbol
*)(syme
+ 1);
137 path
= map
->dso
->long_name
;
139 len
= sym
->end
- sym
->start
;
142 "objdump --start-address=0x%016Lx "
143 "--stop-address=0x%016Lx -dS %s",
144 sym
->start
, sym
->end
, path
);
146 file
= popen(command
, "r");
150 pthread_mutex_lock(&syme
->source_lock
);
151 syme
->lines_tail
= &syme
->lines
;
152 while (!feof(file
)) {
153 struct source_line
*src
;
157 src
= malloc(sizeof(struct source_line
));
159 memset(src
, 0, sizeof(struct source_line
));
161 if (getline(&src
->line
, &dummy
, file
) < 0)
166 c
= strchr(src
->line
, '\n');
171 *syme
->lines_tail
= src
;
172 syme
->lines_tail
= &src
->next
;
174 if (strlen(src
->line
)>8 && src
->line
[8] == ':') {
175 src
->eip
= strtoull(src
->line
, NULL
, 16);
176 src
->eip
+= map
->start
;
178 if (strlen(src
->line
)>8 && src
->line
[16] == ':') {
179 src
->eip
= strtoull(src
->line
, NULL
, 16);
180 src
->eip
+= map
->start
;
185 sym_filter_entry
= syme
;
186 pthread_mutex_unlock(&syme
->source_lock
);
189 static void __zero_source_counters(struct sym_entry
*syme
)
192 struct source_line
*line
;
196 for (i
= 0; i
< nr_counters
; i
++)
202 static void record_precise_ip(struct sym_entry
*syme
, int counter
, u64 ip
)
204 struct source_line
*line
;
206 if (syme
!= sym_filter_entry
)
209 if (pthread_mutex_trylock(&syme
->source_lock
))
215 for (line
= syme
->lines
; line
; line
= line
->next
) {
216 if (line
->eip
== ip
) {
217 line
->count
[counter
]++;
224 pthread_mutex_unlock(&syme
->source_lock
);
227 static void lookup_sym_source(struct sym_entry
*syme
)
229 struct symbol
*symbol
= (struct symbol
*)(syme
+ 1);
230 struct source_line
*line
;
231 char pattern
[PATH_MAX
];
233 sprintf(pattern
, "<%s>:", symbol
->name
);
235 pthread_mutex_lock(&syme
->source_lock
);
236 for (line
= syme
->lines
; line
; line
= line
->next
) {
237 if (strstr(line
->line
, pattern
)) {
242 pthread_mutex_unlock(&syme
->source_lock
);
245 static void show_lines(struct source_line
*queue
, int count
, int total
)
248 struct source_line
*line
;
251 for (i
= 0; i
< count
; i
++) {
252 float pcnt
= 100.0*(float)line
->count
[sym_counter
]/(float)total
;
254 printf("%8li %4.1f%%\t%s\n", line
->count
[sym_counter
], pcnt
, line
->line
);
259 #define TRACE_COUNT 3
261 static void show_details(struct sym_entry
*syme
)
263 struct symbol
*symbol
;
264 struct source_line
*line
;
265 struct source_line
*line_queue
= NULL
;
267 int line_queue_count
= 0, total
= 0, more
= 0;
273 lookup_sym_source(syme
);
278 symbol
= (struct symbol
*)(syme
+ 1);
279 printf("Showing %s for %s\n", event_name(sym_counter
), symbol
->name
);
280 printf(" Events Pcnt (>=%d%%)\n", sym_pcnt_filter
);
282 pthread_mutex_lock(&syme
->source_lock
);
285 total
+= line
->count
[sym_counter
];
293 if (!line_queue_count
)
297 if (line
->count
[sym_counter
])
298 pcnt
= 100.0 * line
->count
[sym_counter
] / (float)total
;
299 if (pcnt
>= (float)sym_pcnt_filter
) {
300 if (displayed
<= print_entries
)
301 show_lines(line_queue
, line_queue_count
, total
);
303 displayed
+= line_queue_count
;
304 line_queue_count
= 0;
306 } else if (line_queue_count
> TRACE_COUNT
) {
307 line_queue
= line_queue
->next
;
311 line
->count
[sym_counter
] = zero
? 0 : line
->count
[sym_counter
] * 7 / 8;
314 pthread_mutex_unlock(&syme
->source_lock
);
316 printf("%d lines not displayed, maybe increase display entries [e]\n", more
);
320 * Symbols will be added here in record_ip and will get out
323 static LIST_HEAD(active_symbols
);
324 static pthread_mutex_t active_symbols_lock
= PTHREAD_MUTEX_INITIALIZER
;
327 * Ordering weight: count-1 * count-2 * ... / count-n
329 static double sym_weight(const struct sym_entry
*sym
)
331 double weight
= sym
->snap_count
;
334 if (!display_weighted
)
337 for (counter
= 1; counter
< nr_counters
-1; counter
++)
338 weight
*= sym
->count
[counter
];
340 weight
/= (sym
->count
[counter
] + 1);
346 static long userspace_samples
;
347 static const char CONSOLE_CLEAR
[] = "\e[H\e[2J";
349 static void __list_insert_active_sym(struct sym_entry
*syme
)
351 list_add(&syme
->node
, &active_symbols
);
354 static void list_remove_active_sym(struct sym_entry
*syme
)
356 pthread_mutex_lock(&active_symbols_lock
);
357 list_del_init(&syme
->node
);
358 pthread_mutex_unlock(&active_symbols_lock
);
361 static void rb_insert_active_sym(struct rb_root
*tree
, struct sym_entry
*se
)
363 struct rb_node
**p
= &tree
->rb_node
;
364 struct rb_node
*parent
= NULL
;
365 struct sym_entry
*iter
;
369 iter
= rb_entry(parent
, struct sym_entry
, rb_node
);
371 if (se
->weight
> iter
->weight
)
377 rb_link_node(&se
->rb_node
, parent
, p
);
378 rb_insert_color(&se
->rb_node
, tree
);
381 static void print_sym_table(void)
384 int counter
, snap
= !display_weighted
? sym_counter
: 0;
385 float samples_per_sec
= samples
/delay_secs
;
386 float ksamples_per_sec
= (samples
-userspace_samples
)/delay_secs
;
387 float sum_ksamples
= 0.0;
388 struct sym_entry
*syme
, *n
;
389 struct rb_root tmp
= RB_ROOT
;
392 samples
= userspace_samples
= 0;
394 /* Sort the active symbols */
395 pthread_mutex_lock(&active_symbols_lock
);
396 syme
= list_entry(active_symbols
.next
, struct sym_entry
, node
);
397 pthread_mutex_unlock(&active_symbols_lock
);
399 list_for_each_entry_safe_from(syme
, n
, &active_symbols
, node
) {
400 syme
->snap_count
= syme
->count
[snap
];
401 if (syme
->snap_count
!= 0) {
402 syme
->weight
= sym_weight(syme
);
403 rb_insert_active_sym(&tmp
, syme
);
404 sum_ksamples
+= syme
->snap_count
;
406 for (j
= 0; j
< nr_counters
; j
++)
407 syme
->count
[j
] = zero
? 0 : syme
->count
[j
] * 7 / 8;
409 list_remove_active_sym(syme
);
415 "------------------------------------------------------------------------------\n");
416 printf( " PerfTop:%8.0f irqs/sec kernel:%4.1f%% [",
418 100.0 - (100.0*((samples_per_sec
-ksamples_per_sec
)/samples_per_sec
)));
420 if (nr_counters
== 1 || !display_weighted
) {
421 printf("%Ld", (u64
)attrs
[0].sample_period
);
428 if (!display_weighted
)
429 printf("%s", event_name(sym_counter
));
430 else for (counter
= 0; counter
< nr_counters
; counter
++) {
434 printf("%s", event_name(counter
));
439 if (target_pid
!= -1)
440 printf(" (target_pid: %d", target_pid
);
444 if (profile_cpu
!= -1)
445 printf(", cpu: %d)\n", profile_cpu
);
447 if (target_pid
!= -1)
450 printf(", %d CPUs)\n", nr_cpus
);
453 printf("------------------------------------------------------------------------------\n\n");
455 if (sym_filter_entry
) {
456 show_details(sym_filter_entry
);
460 if (nr_counters
== 1)
461 printf(" samples pcnt");
463 printf(" weight samples pcnt");
467 printf(" kernel function\n");
468 printf(" %s _______ _____",
469 nr_counters
== 1 ? " " : "______");
471 printf(" ________________");
472 printf(" _______________\n\n");
474 for (nd
= rb_first(&tmp
); nd
; nd
= rb_next(nd
)) {
478 syme
= rb_entry(nd
, struct sym_entry
, rb_node
);
479 sym
= (struct symbol
*)(syme
+ 1);
481 if (++printed
> print_entries
|| (int)syme
->snap_count
< count_filter
)
484 pcnt
= 100.0 - (100.0 * ((sum_ksamples
- syme
->snap_count
) /
487 if (nr_counters
== 1 || !display_weighted
)
488 printf("%20.2f - ", syme
->weight
);
490 printf("%9.1f %10ld - ", syme
->weight
, syme
->snap_count
);
492 percent_color_fprintf(stdout
, "%4.1f%%", pcnt
);
494 printf(" - %016llx", sym
->start
);
495 printf(" : %s", sym
->name
);
496 if (syme
->map
->dso
->name
[0] == '[')
497 printf(" \t%s", syme
->map
->dso
->name
);
502 static void prompt_integer(int *target
, const char *msg
)
504 char *buf
= malloc(0), *p
;
508 fprintf(stdout
, "\n%s: ", msg
);
509 if (getline(&buf
, &dummy
, stdin
) < 0)
512 p
= strchr(buf
, '\n');
522 tmp
= strtoul(buf
, NULL
, 10);
528 static void prompt_percent(int *target
, const char *msg
)
532 prompt_integer(&tmp
, msg
);
533 if (tmp
>= 0 && tmp
<= 100)
537 static void prompt_symbol(struct sym_entry
**target
, const char *msg
)
539 char *buf
= malloc(0), *p
;
540 struct sym_entry
*syme
= *target
, *n
, *found
= NULL
;
543 /* zero counters of active symbol */
545 pthread_mutex_lock(&syme
->source_lock
);
546 __zero_source_counters(syme
);
548 pthread_mutex_unlock(&syme
->source_lock
);
551 fprintf(stdout
, "\n%s: ", msg
);
552 if (getline(&buf
, &dummy
, stdin
) < 0)
555 p
= strchr(buf
, '\n');
559 pthread_mutex_lock(&active_symbols_lock
);
560 syme
= list_entry(active_symbols
.next
, struct sym_entry
, node
);
561 pthread_mutex_unlock(&active_symbols_lock
);
563 list_for_each_entry_safe_from(syme
, n
, &active_symbols
, node
) {
564 struct symbol
*sym
= (struct symbol
*)(syme
+ 1);
566 if (!strcmp(buf
, sym
->name
)) {
573 fprintf(stderr
, "Sorry, %s is not active.\n", sym_filter
);
583 static void print_mapped_keys(void)
587 if (sym_filter_entry
) {
588 struct symbol
*sym
= (struct symbol
*)(sym_filter_entry
+1);
592 fprintf(stdout
, "\nMapped keys:\n");
593 fprintf(stdout
, "\t[d] display refresh delay. \t(%d)\n", delay_secs
);
594 fprintf(stdout
, "\t[e] display entries (lines). \t(%d)\n", print_entries
);
597 fprintf(stdout
, "\t[E] active event counter. \t(%s)\n", event_name(sym_counter
));
599 fprintf(stdout
, "\t[f] profile display filter (count). \t(%d)\n", count_filter
);
602 fprintf(stdout
, "\t[F] annotate display filter (percent). \t(%d%%)\n", sym_pcnt_filter
);
603 fprintf(stdout
, "\t[s] annotate symbol. \t(%s)\n", name
?: "NULL");
604 fprintf(stdout
, "\t[S] stop annotation.\n");
608 fprintf(stdout
, "\t[w] toggle display weighted/count[E]r. \t(%d)\n", display_weighted
? 1 : 0);
610 fprintf(stdout
, "\t[z] toggle sample zeroing. \t(%d)\n", zero
? 1 : 0);
611 fprintf(stdout
, "\t[qQ] quit.\n");
614 static int key_mapped(int c
)
626 return nr_counters
> 1 ? 1 : 0;
630 return vmlinux_name
? 1 : 0;
638 static void handle_keypress(int c
)
640 if (!key_mapped(c
)) {
641 struct pollfd stdin_poll
= { .fd
= 0, .events
= POLLIN
};
642 struct termios tc
, save
;
645 fprintf(stdout
, "\nEnter selection, or unmapped key to continue: ");
650 tc
.c_lflag
&= ~(ICANON
| ECHO
);
653 tcsetattr(0, TCSANOW
, &tc
);
655 poll(&stdin_poll
, 1, -1);
658 tcsetattr(0, TCSAFLUSH
, &save
);
665 prompt_integer(&delay_secs
, "Enter display delay");
668 prompt_integer(&print_entries
, "Enter display entries (lines)");
671 if (nr_counters
> 1) {
674 fprintf(stderr
, "\nAvailable events:");
675 for (i
= 0; i
< nr_counters
; i
++)
676 fprintf(stderr
, "\n\t%d %s", i
, event_name(i
));
678 prompt_integer(&sym_counter
, "Enter details event counter");
680 if (sym_counter
>= nr_counters
) {
681 fprintf(stderr
, "Sorry, no such event, using %s.\n", event_name(0));
685 } else sym_counter
= 0;
688 prompt_integer(&count_filter
, "Enter display event count filter");
691 prompt_percent(&sym_pcnt_filter
, "Enter details display event filter (percent)");
695 printf("exiting.\n");
698 prompt_symbol(&sym_filter_entry
, "Enter details symbol");
701 if (!sym_filter_entry
)
704 struct sym_entry
*syme
= sym_filter_entry
;
706 pthread_mutex_lock(&syme
->source_lock
);
707 sym_filter_entry
= NULL
;
708 __zero_source_counters(syme
);
709 pthread_mutex_unlock(&syme
->source_lock
);
713 display_weighted
= ~display_weighted
;
723 static void *display_thread(void *arg __used
)
725 struct pollfd stdin_poll
= { .fd
= 0, .events
= POLLIN
};
726 struct termios tc
, save
;
731 tc
.c_lflag
&= ~(ICANON
| ECHO
);
736 delay_msecs
= delay_secs
* 1000;
737 tcsetattr(0, TCSANOW
, &tc
);
743 } while (!poll(&stdin_poll
, 1, delay_msecs
) == 1);
746 tcsetattr(0, TCSAFLUSH
, &save
);
754 /* Tag samples to be skipped. */
755 static const char *skip_symbols
[] = {
761 "mwait_idle_with_hints",
763 "ppc64_runlatch_off",
764 "pseries_dedicated_idle_sleep",
768 static int symbol_filter(struct map
*map
, struct symbol
*sym
)
770 struct sym_entry
*syme
;
771 const char *name
= sym
->name
;
775 * ppc64 uses function descriptors and appends a '.' to the
776 * start of every instruction address. Remove it.
781 if (!strcmp(name
, "_text") ||
782 !strcmp(name
, "_etext") ||
783 !strcmp(name
, "_sinittext") ||
784 !strncmp("init_module", name
, 11) ||
785 !strncmp("cleanup_module", name
, 14) ||
786 strstr(name
, "_text_start") ||
787 strstr(name
, "_text_end"))
790 syme
= dso__sym_priv(map
->dso
, sym
);
792 pthread_mutex_init(&syme
->source_lock
, NULL
);
793 if (!sym_filter_entry
&& sym_filter
&& !strcmp(name
, sym_filter
))
794 sym_filter_entry
= syme
;
796 for (i
= 0; skip_symbols
[i
]; i
++) {
797 if (!strcmp(skip_symbols
[i
], name
)) {
806 static int parse_symbols(void)
808 if (dsos__load_kernel(vmlinux_name
, sizeof(struct sym_entry
),
809 symbol_filter
, verbose
, 1) <= 0)
813 dsos__fprintf(stderr
);
819 * Binary search in the histogram table and record the hit:
821 static void record_ip(u64 ip
, int counter
)
824 struct symbol
*sym
= kernel_maps__find_symbol(ip
, &map
);
827 struct sym_entry
*syme
= dso__sym_priv(map
->dso
, sym
);
830 syme
->count
[counter
]++;
831 record_precise_ip(syme
, counter
, ip
);
832 pthread_mutex_lock(&active_symbols_lock
);
833 if (list_empty(&syme
->node
) || !syme
->node
.next
)
834 __list_insert_active_sym(syme
);
835 pthread_mutex_unlock(&active_symbols_lock
);
843 static void process_event(u64 ip
, int counter
, int user
)
852 record_ip(ip
, counter
);
862 static unsigned int mmap_read_head(struct mmap_data
*md
)
864 struct perf_event_mmap_page
*pc
= md
->base
;
867 head
= pc
->data_head
;
873 struct timeval last_read
, this_read
;
875 static void mmap_read_counter(struct mmap_data
*md
)
877 unsigned int head
= mmap_read_head(md
);
878 unsigned int old
= md
->prev
;
879 unsigned char *data
= md
->base
+ page_size
;
882 gettimeofday(&this_read
, NULL
);
885 * If we're further behind than half the buffer, there's a chance
886 * the writer will bite our tail and mess up the samples under us.
888 * If we somehow ended up ahead of the head, we got messed up.
890 * In either case, truncate and restart at head.
893 if (diff
> md
->mask
/ 2 || diff
< 0) {
897 timersub(&this_read
, &last_read
, &iv
);
898 msecs
= iv
.tv_sec
*1000 + iv
.tv_usec
/1000;
900 fprintf(stderr
, "WARNING: failed to keep up with mmap data."
901 " Last read %lu msecs ago.\n", msecs
);
904 * head points to a known good entry, start there.
909 last_read
= this_read
;
911 for (; old
!= head
;) {
912 event_t
*event
= (event_t
*)&data
[old
& md
->mask
];
916 size_t size
= event
->header
.size
;
919 * Event straddles the mmap boundary -- header should always
920 * be inside due to u64 alignment of output.
922 if ((old
& md
->mask
) + size
!= ((old
+ size
) & md
->mask
)) {
923 unsigned int offset
= old
;
924 unsigned int len
= min(sizeof(*event
), size
), cpy
;
925 void *dst
= &event_copy
;
928 cpy
= min(md
->mask
+ 1 - (offset
& md
->mask
), len
);
929 memcpy(dst
, &data
[offset
& md
->mask
], cpy
);
940 if (event
->header
.type
== PERF_RECORD_SAMPLE
) {
942 (event
->header
.misc
& PERF_RECORD_MISC_CPUMODE_MASK
) == PERF_RECORD_MISC_USER
;
943 process_event(event
->ip
.ip
, md
->counter
, user
);
950 static struct pollfd event_array
[MAX_NR_CPUS
* MAX_COUNTERS
];
951 static struct mmap_data mmap_array
[MAX_NR_CPUS
][MAX_COUNTERS
];
953 static void mmap_read(void)
957 for (i
= 0; i
< nr_cpus
; i
++) {
958 for (counter
= 0; counter
< nr_counters
; counter
++)
959 mmap_read_counter(&mmap_array
[i
][counter
]);
966 static void start_counter(int i
, int counter
)
968 struct perf_event_attr
*attr
;
972 if (target_pid
== -1 && profile_cpu
== -1)
975 attr
= attrs
+ counter
;
977 attr
->sample_type
= PERF_SAMPLE_IP
| PERF_SAMPLE_TID
;
979 attr
->inherit
= (cpu
< 0) && inherit
;
982 fd
[i
][counter
] = sys_perf_event_open(attr
, target_pid
, cpu
, group_fd
, 0);
984 if (fd
[i
][counter
] < 0) {
988 die("No permission - are you root?\n");
990 * If it's cycles then fall back to hrtimer
991 * based cpu-clock-tick sw counter, which
992 * is always available even if no PMU support:
994 if (attr
->type
== PERF_TYPE_HARDWARE
995 && attr
->config
== PERF_COUNT_HW_CPU_CYCLES
) {
998 warning(" ... trying to fall back to cpu-clock-ticks\n");
1000 attr
->type
= PERF_TYPE_SOFTWARE
;
1001 attr
->config
= PERF_COUNT_SW_CPU_CLOCK
;
1005 error("perfcounter syscall returned with %d (%s)\n",
1006 fd
[i
][counter
], strerror(err
));
1007 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
1010 assert(fd
[i
][counter
] >= 0);
1011 fcntl(fd
[i
][counter
], F_SETFL
, O_NONBLOCK
);
1014 * First counter acts as the group leader:
1016 if (group
&& group_fd
== -1)
1017 group_fd
= fd
[i
][counter
];
1019 event_array
[nr_poll
].fd
= fd
[i
][counter
];
1020 event_array
[nr_poll
].events
= POLLIN
;
1023 mmap_array
[i
][counter
].counter
= counter
;
1024 mmap_array
[i
][counter
].prev
= 0;
1025 mmap_array
[i
][counter
].mask
= mmap_pages
*page_size
- 1;
1026 mmap_array
[i
][counter
].base
= mmap(NULL
, (mmap_pages
+1)*page_size
,
1027 PROT_READ
, MAP_SHARED
, fd
[i
][counter
], 0);
1028 if (mmap_array
[i
][counter
].base
== MAP_FAILED
)
1029 die("failed to mmap with %d (%s)\n", errno
, strerror(errno
));
1032 static int __cmd_top(void)
1038 for (i
= 0; i
< nr_cpus
; i
++) {
1040 for (counter
= 0; counter
< nr_counters
; counter
++)
1041 start_counter(i
, counter
);
1044 /* Wait for a minimal set of events before starting the snapshot */
1045 poll(event_array
, nr_poll
, 100);
1049 if (pthread_create(&thread
, NULL
, display_thread
, NULL
)) {
1050 printf("Could not create display thread.\n");
1054 if (realtime_prio
) {
1055 struct sched_param param
;
1057 param
.sched_priority
= realtime_prio
;
1058 if (sched_setscheduler(0, SCHED_FIFO
, ¶m
)) {
1059 printf("Could not set realtime priority.\n");
1069 if (hits
== samples
)
1070 ret
= poll(event_array
, nr_poll
, 100);
1076 static const char * const top_usage
[] = {
1077 "perf top [<options>]",
1081 static const struct option options
[] = {
1082 OPT_CALLBACK('e', "event", NULL
, "event",
1083 "event selector. use 'perf list' to list available events",
1085 OPT_INTEGER('c', "count", &default_interval
,
1086 "event period to sample"),
1087 OPT_INTEGER('p', "pid", &target_pid
,
1088 "profile events on existing pid"),
1089 OPT_BOOLEAN('a', "all-cpus", &system_wide
,
1090 "system-wide collection from all CPUs"),
1091 OPT_INTEGER('C', "CPU", &profile_cpu
,
1092 "CPU to profile on"),
1093 OPT_STRING('k', "vmlinux", &vmlinux_name
, "file", "vmlinux pathname"),
1094 OPT_INTEGER('m', "mmap-pages", &mmap_pages
,
1095 "number of mmap data pages"),
1096 OPT_INTEGER('r', "realtime", &realtime_prio
,
1097 "collect data with this RT SCHED_FIFO priority"),
1098 OPT_INTEGER('d', "delay", &delay_secs
,
1099 "number of seconds to delay between refreshes"),
1100 OPT_BOOLEAN('D', "dump-symtab", &dump_symtab
,
1101 "dump the symbol table used for profiling"),
1102 OPT_INTEGER('f', "count-filter", &count_filter
,
1103 "only display functions with more events than this"),
1104 OPT_BOOLEAN('g', "group", &group
,
1105 "put the counters into a counter group"),
1106 OPT_BOOLEAN('i', "inherit", &inherit
,
1107 "child tasks inherit counters"),
1108 OPT_STRING('s', "sym-annotate", &sym_filter
, "symbol name",
1109 "symbol to annotate - requires -k option"),
1110 OPT_BOOLEAN('z', "zero", &zero
,
1111 "zero history across updates"),
1112 OPT_INTEGER('F', "freq", &freq
,
1113 "profile at this frequency"),
1114 OPT_INTEGER('E', "entries", &print_entries
,
1115 "display this many functions"),
1116 OPT_BOOLEAN('v', "verbose", &verbose
,
1117 "be more verbose (show counter open errors, etc)"),
1121 int cmd_top(int argc
, const char **argv
, const char *prefix __used
)
1127 page_size
= sysconf(_SC_PAGE_SIZE
);
1129 argc
= parse_options(argc
, argv
, options
, top_usage
, 0);
1131 usage_with_options(top_usage
, options
);
1134 default_interval
= freq
;
1138 /* CPU and PID are mutually exclusive */
1139 if (target_pid
!= -1 && profile_cpu
!= -1) {
1140 printf("WARNING: PID switch overriding CPU\n");
1152 parse_source(sym_filter_entry
);
1155 * Fill in the ones not specifically initialized via -c:
1157 for (counter
= 0; counter
< nr_counters
; counter
++) {
1158 if (attrs
[counter
].sample_period
)
1161 attrs
[counter
].sample_period
= default_interval
;
1164 nr_cpus
= sysconf(_SC_NPROCESSORS_ONLN
);
1165 assert(nr_cpus
<= MAX_NR_CPUS
);
1166 assert(nr_cpus
>= 0);
1168 if (target_pid
!= -1 || profile_cpu
!= -1)