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 = 0;
62 static int count_filter = 5;
63 static int print_entries;
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 /* most GUI terminals set LINES (although some don't export it) */
119 static int term_rows(void)
121 char *lines_string = getenv("LINES");
124 if (lines_string && (n_lines = atoi(lines_string)) > 0)
129 if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_row)
136 static void update_print_entries(void)
138 print_entries = term_rows();
139 if (print_entries > 9)
143 static void sig_winch_handler(int sig __used)
145 update_print_entries();
148 static void parse_source(struct sym_entry *syme)
153 char command[PATH_MAX*2];
161 pthread_mutex_lock(&syme->source_lock);
165 sym = (struct symbol *)(syme + 1);
167 path = map->dso->long_name;
169 len = sym->end - sym->start;
172 "objdump --start-address=0x%016Lx "
173 "--stop-address=0x%016Lx -dS %s",
174 map->unmap_ip(map, sym->start),
175 map->unmap_ip(map, sym->end), path);
177 file = popen(command, "r");
181 pthread_mutex_lock(&syme->source_lock);
182 syme->lines_tail = &syme->lines;
183 while (!feof(file)) {
184 struct source_line *src;
188 src = malloc(sizeof(struct source_line));
190 memset(src, 0, sizeof(struct source_line));
192 if (getline(&src->line, &dummy, file) < 0)
197 c = strchr(src->line, '\n');
202 *syme->lines_tail = src;
203 syme->lines_tail = &src->next;
205 if (strlen(src->line)>8 && src->line[8] == ':') {
206 src->eip = strtoull(src->line, NULL, 16);
207 src->eip = map->unmap_ip(map, src->eip);
209 if (strlen(src->line)>8 && src->line[16] == ':') {
210 src->eip = strtoull(src->line, NULL, 16);
211 src->eip = map->unmap_ip(map, src->eip);
216 sym_filter_entry = syme;
217 pthread_mutex_unlock(&syme->source_lock);
220 static void __zero_source_counters(struct sym_entry *syme)
223 struct source_line *line;
227 for (i = 0; i < nr_counters; i++)
233 static void record_precise_ip(struct sym_entry *syme, int counter, u64 ip)
235 struct source_line *line;
237 if (syme != sym_filter_entry)
240 if (pthread_mutex_trylock(&syme->source_lock))
246 for (line = syme->lines; line; line = line->next) {
247 if (line->eip == ip) {
248 line->count[counter]++;
255 pthread_mutex_unlock(&syme->source_lock);
258 static void lookup_sym_source(struct sym_entry *syme)
260 struct symbol *symbol = (struct symbol *)(syme + 1);
261 struct source_line *line;
262 char pattern[PATH_MAX];
264 sprintf(pattern, "<%s>:", symbol->name);
266 pthread_mutex_lock(&syme->source_lock);
267 for (line = syme->lines; line; line = line->next) {
268 if (strstr(line->line, pattern)) {
273 pthread_mutex_unlock(&syme->source_lock);
276 static void show_lines(struct source_line *queue, int count, int total)
279 struct source_line *line;
282 for (i = 0; i < count; i++) {
283 float pcnt = 100.0*(float)line->count[sym_counter]/(float)total;
285 printf("%8li %4.1f%%\t%s\n", line->count[sym_counter], pcnt, line->line);
290 #define TRACE_COUNT 3
292 static void show_details(struct sym_entry *syme)
294 struct symbol *symbol;
295 struct source_line *line;
296 struct source_line *line_queue = NULL;
298 int line_queue_count = 0, total = 0, more = 0;
304 lookup_sym_source(syme);
309 symbol = (struct symbol *)(syme + 1);
310 printf("Showing %s for %s\n", event_name(sym_counter), symbol->name);
311 printf(" Events Pcnt (>=%d%%)\n", sym_pcnt_filter);
313 pthread_mutex_lock(&syme->source_lock);
316 total += line->count[sym_counter];
324 if (!line_queue_count)
328 if (line->count[sym_counter])
329 pcnt = 100.0 * line->count[sym_counter] / (float)total;
330 if (pcnt >= (float)sym_pcnt_filter) {
331 if (displayed <= print_entries)
332 show_lines(line_queue, line_queue_count, total);
334 displayed += line_queue_count;
335 line_queue_count = 0;
337 } else if (line_queue_count > TRACE_COUNT) {
338 line_queue = line_queue->next;
342 line->count[sym_counter] = zero ? 0 : line->count[sym_counter] * 7 / 8;
345 pthread_mutex_unlock(&syme->source_lock);
347 printf("%d lines not displayed, maybe increase display entries [e]\n", more);
351 * Symbols will be added here in event__process_sample and will get out
354 static LIST_HEAD(active_symbols);
355 static pthread_mutex_t active_symbols_lock = PTHREAD_MUTEX_INITIALIZER;
358 * Ordering weight: count-1 * count-2 * ... / count-n
360 static double sym_weight(const struct sym_entry *sym)
362 double weight = sym->snap_count;
365 if (!display_weighted)
368 for (counter = 1; counter < nr_counters-1; counter++)
369 weight *= sym->count[counter];
371 weight /= (sym->count[counter] + 1);
377 static long userspace_samples;
378 static const char CONSOLE_CLEAR[] = "
\e[H
\e[2J";
380 static void __list_insert_active_sym(struct sym_entry *syme)
382 list_add(&syme->node, &active_symbols);
385 static void list_remove_active_sym(struct sym_entry *syme)
387 pthread_mutex_lock(&active_symbols_lock);
388 list_del_init(&syme->node);
389 pthread_mutex_unlock(&active_symbols_lock);
392 static void rb_insert_active_sym(struct rb_root *tree, struct sym_entry *se)
394 struct rb_node **p = &tree->rb_node;
395 struct rb_node *parent = NULL;
396 struct sym_entry *iter;
400 iter = rb_entry(parent, struct sym_entry, rb_node);
402 if (se->weight > iter->weight)
408 rb_link_node(&se->rb_node, parent, p);
409 rb_insert_color(&se->rb_node, tree);
412 static void print_sym_table(void)
415 int counter, snap = !display_weighted ? sym_counter : 0;
416 float samples_per_sec = samples/delay_secs;
417 float ksamples_per_sec = (samples-userspace_samples)/delay_secs;
418 float sum_ksamples = 0.0;
419 struct sym_entry *syme, *n;
420 struct rb_root tmp = RB_ROOT;
423 samples = userspace_samples = 0;
425 /* Sort the active symbols */
426 pthread_mutex_lock(&active_symbols_lock);
427 syme = list_entry(active_symbols.next, struct sym_entry, node);
428 pthread_mutex_unlock(&active_symbols_lock);
430 list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
431 syme->snap_count = syme->count[snap];
432 if (syme->snap_count != 0) {
433 syme->weight = sym_weight(syme);
434 rb_insert_active_sym(&tmp, syme);
435 sum_ksamples += syme->snap_count;
437 for (j = 0; j < nr_counters; j++)
438 syme->count[j] = zero ? 0 : syme->count[j] * 7 / 8;
440 list_remove_active_sym(syme);
446 "------------------------------------------------------------------------------\n");
447 printf( " PerfTop:%8.0f irqs/sec kernel:%4.1f%% [",
449 100.0 - (100.0*((samples_per_sec-ksamples_per_sec)/samples_per_sec)));
451 if (nr_counters == 1 || !display_weighted) {
452 printf("%Ld", (u64)attrs[0].sample_period);
459 if (!display_weighted)
460 printf("%s", event_name(sym_counter));
461 else for (counter = 0; counter < nr_counters; counter++) {
465 printf("%s", event_name(counter));
470 if (target_pid != -1)
471 printf(" (target_pid: %d", target_pid);
475 if (profile_cpu != -1)
476 printf(", cpu: %d)\n", profile_cpu);
478 if (target_pid != -1)
481 printf(", %d CPUs)\n", nr_cpus);
484 printf("------------------------------------------------------------------------------\n\n");
486 if (sym_filter_entry) {
487 show_details(sym_filter_entry);
491 if (nr_counters == 1)
492 printf(" samples pcnt");
494 printf(" weight samples pcnt");
498 printf(" function DSO\n");
499 printf(" %s _______ _____",
500 nr_counters == 1 ? " " : "______");
502 printf(" ________________");
503 printf(" ________________________________ ________________\n\n");
505 for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
509 syme = rb_entry(nd, struct sym_entry, rb_node);
510 sym = (struct symbol *)(syme + 1);
512 if (++printed > print_entries || (int)syme->snap_count < count_filter)
515 pcnt = 100.0 - (100.0 * ((sum_ksamples - syme->snap_count) /
518 if (nr_counters == 1 || !display_weighted)
519 printf("%20.2f ", syme->weight);
521 printf("%9.1f %10ld ", syme->weight, syme->snap_count);
523 percent_color_fprintf(stdout, "%4.1f%%", pcnt);
525 printf(" %016llx", sym->start);
526 printf(" %-32s", sym->name);
527 printf(" %s", syme->map->dso->short_name);
532 static void prompt_integer(int *target, const char *msg)
534 char *buf = malloc(0), *p;
538 fprintf(stdout, "\n%s: ", msg);
539 if (getline(&buf, &dummy, stdin) < 0)
542 p = strchr(buf, '\n');
552 tmp = strtoul(buf, NULL, 10);
558 static void prompt_percent(int *target, const char *msg)
562 prompt_integer(&tmp, msg);
563 if (tmp >= 0 && tmp <= 100)
567 static void prompt_symbol(struct sym_entry **target, const char *msg)
569 char *buf = malloc(0), *p;
570 struct sym_entry *syme = *target, *n, *found = NULL;
573 /* zero counters of active symbol */
575 pthread_mutex_lock(&syme->source_lock);
576 __zero_source_counters(syme);
578 pthread_mutex_unlock(&syme->source_lock);
581 fprintf(stdout, "\n%s: ", msg);
582 if (getline(&buf, &dummy, stdin) < 0)
585 p = strchr(buf, '\n');
589 pthread_mutex_lock(&active_symbols_lock);
590 syme = list_entry(active_symbols.next, struct sym_entry, node);
591 pthread_mutex_unlock(&active_symbols_lock);
593 list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
594 struct symbol *sym = (struct symbol *)(syme + 1);
596 if (!strcmp(buf, sym->name)) {
603 fprintf(stderr, "Sorry, %s is not active.\n", sym_filter);
613 static void print_mapped_keys(void)
617 if (sym_filter_entry) {
618 struct symbol *sym = (struct symbol *)(sym_filter_entry+1);
622 fprintf(stdout, "\nMapped keys:\n");
623 fprintf(stdout, "\t[d] display refresh delay. \t(%d)\n", delay_secs);
624 fprintf(stdout, "\t[e] display entries (lines). \t(%d)\n", print_entries);
627 fprintf(stdout, "\t[E] active event counter. \t(%s)\n", event_name(sym_counter));
629 fprintf(stdout, "\t[f] profile display filter (count). \t(%d)\n", count_filter);
632 fprintf(stdout, "\t[F] annotate display filter (percent). \t(%d%%)\n", sym_pcnt_filter);
633 fprintf(stdout, "\t[s] annotate symbol. \t(%s)\n", name?: "NULL");
634 fprintf(stdout, "\t[S] stop annotation.\n");
638 fprintf(stdout, "\t[w] toggle display weighted/count[E]r. \t(%d)\n", display_weighted ? 1 : 0);
640 fprintf(stdout, "\t[z] toggle sample zeroing. \t(%d)\n", zero ? 1 : 0);
641 fprintf(stdout, "\t[qQ] quit.\n");
644 static int key_mapped(int c)
656 return nr_counters > 1 ? 1 : 0;
660 return vmlinux_name ? 1 : 0;
668 static void handle_keypress(int c)
670 if (!key_mapped(c)) {
671 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
672 struct termios tc, save;
675 fprintf(stdout, "\nEnter selection, or unmapped key to continue: ");
680 tc.c_lflag &= ~(ICANON | ECHO);
683 tcsetattr(0, TCSANOW, &tc);
685 poll(&stdin_poll, 1, -1);
688 tcsetattr(0, TCSAFLUSH, &save);
695 prompt_integer(&delay_secs, "Enter display delay");
700 prompt_integer(&print_entries, "Enter display entries (lines)");
701 if (print_entries == 0) {
702 update_print_entries();
703 signal(SIGWINCH, sig_winch_handler);
705 signal(SIGWINCH, SIG_DFL);
708 if (nr_counters > 1) {
711 fprintf(stderr, "\nAvailable events:");
712 for (i = 0; i < nr_counters; i++)
713 fprintf(stderr, "\n\t%d %s", i, event_name(i));
715 prompt_integer(&sym_counter, "Enter details event counter");
717 if (sym_counter >= nr_counters) {
718 fprintf(stderr, "Sorry, no such event, using %s.\n", event_name(0));
722 } else sym_counter = 0;
725 prompt_integer(&count_filter, "Enter display event count filter");
728 prompt_percent(&sym_pcnt_filter, "Enter details display event filter (percent)");
732 printf("exiting.\n");
735 prompt_symbol(&sym_filter_entry, "Enter details symbol");
738 if (!sym_filter_entry)
741 struct sym_entry *syme = sym_filter_entry;
743 pthread_mutex_lock(&syme->source_lock);
744 sym_filter_entry = NULL;
745 __zero_source_counters(syme);
746 pthread_mutex_unlock(&syme->source_lock);
750 display_weighted = ~display_weighted;
760 static void *display_thread(void *arg __used)
762 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
763 struct termios tc, save;
768 tc.c_lflag &= ~(ICANON | ECHO);
773 delay_msecs = delay_secs * 1000;
774 tcsetattr(0, TCSANOW, &tc);
780 } while (!poll(&stdin_poll, 1, delay_msecs) == 1);
783 tcsetattr(0, TCSAFLUSH, &save);
791 /* Tag samples to be skipped. */
792 static const char *skip_symbols[] = {
798 "mwait_idle_with_hints",
800 "ppc64_runlatch_off",
801 "pseries_dedicated_idle_sleep",
805 static int symbol_filter(struct map *map, struct symbol *sym)
807 struct sym_entry *syme;
808 const char *name = sym->name;
812 * ppc64 uses function descriptors and appends a '.' to the
813 * start of every instruction address. Remove it.
818 if (!strcmp(name, "_text") ||
819 !strcmp(name, "_etext") ||
820 !strcmp(name, "_sinittext") ||
821 !strncmp("init_module", name, 11) ||
822 !strncmp("cleanup_module", name, 14) ||
823 strstr(name, "_text_start") ||
824 strstr(name, "_text_end"))
827 syme = symbol__priv(sym);
829 pthread_mutex_init(&syme->source_lock, NULL);
830 if (!sym_filter_entry && sym_filter && !strcmp(name, sym_filter))
831 sym_filter_entry = syme;
833 for (i = 0; skip_symbols[i]; i++) {
834 if (!strcmp(skip_symbols[i], name)) {
843 static int parse_symbols(void)
845 if (dsos__load_kernel(vmlinux_name, symbol_filter, 1) <= 0)
849 dsos__fprintf(stderr);
854 static void event__process_sample(const event_t *self, int counter)
856 u64 ip = self->ip.ip;
858 struct sym_entry *syme;
861 switch (self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK) {
862 case PERF_RECORD_MISC_USER: {
863 struct thread *thread = threads__findnew(self->ip.pid);
868 map = thread__find_map(thread, ip);
870 ip = map->map_ip(map, ip);
871 sym = map__find_symbol(map, ip, symbol_filter);
879 * If this is outside of all known maps,
880 * and is a negative address, try to look it
881 * up in the kernel dso, as it might be a
882 * vsyscall or vdso (which executes in user-mode).
884 if ((long long)ip >= 0)
887 case PERF_RECORD_MISC_KERNEL:
888 sym = kernel_maps__find_symbol(ip, &map);
896 syme = symbol__priv(sym);
899 syme->count[counter]++;
900 record_precise_ip(syme, counter, ip);
901 pthread_mutex_lock(&active_symbols_lock);
902 if (list_empty(&syme->node) || !syme->node.next)
903 __list_insert_active_sym(syme);
904 pthread_mutex_unlock(&active_symbols_lock);
910 static void event__process_mmap(event_t *self)
912 struct thread *thread = threads__findnew(self->mmap.pid);
914 if (thread != NULL) {
915 struct map *map = map__new(&self->mmap, NULL, 0);
917 thread__insert_map(thread, map);
921 static void event__process_comm(event_t *self)
923 struct thread *thread = threads__findnew(self->comm.pid);
926 thread__set_comm(thread, self->comm.comm);
929 static int event__process(event_t *event)
931 switch (event->header.type) {
932 case PERF_RECORD_COMM:
933 event__process_comm(event);
935 case PERF_RECORD_MMAP:
936 event__process_mmap(event);
952 static unsigned int mmap_read_head(struct mmap_data *md)
954 struct perf_event_mmap_page *pc = md->base;
957 head = pc->data_head;
963 static void mmap_read_counter(struct mmap_data *md)
965 unsigned int head = mmap_read_head(md);
966 unsigned int old = md->prev;
967 unsigned char *data = md->base + page_size;
971 * If we're further behind than half the buffer, there's a chance
972 * the writer will bite our tail and mess up the samples under us.
974 * If we somehow ended up ahead of the head, we got messed up.
976 * In either case, truncate and restart at head.
979 if (diff > md->mask / 2 || diff < 0) {
980 fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
983 * head points to a known good entry, start there.
988 for (; old != head;) {
989 event_t *event = (event_t *)&data[old & md->mask];
993 size_t size = event->header.size;
996 * Event straddles the mmap boundary -- header should always
997 * be inside due to u64 alignment of output.
999 if ((old & md->mask) + size != ((old + size) & md->mask)) {
1000 unsigned int offset = old;
1001 unsigned int len = min(sizeof(*event), size), cpy;
1002 void *dst = &event_copy;
1005 cpy = min(md->mask + 1 - (offset & md->mask), len);
1006 memcpy(dst, &data[offset & md->mask], cpy);
1012 event = &event_copy;
1015 if (event->header.type == PERF_RECORD_SAMPLE)
1016 event__process_sample(event, md->counter);
1018 event__process(event);
1025 static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
1026 static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
1028 static void mmap_read(void)
1032 for (i = 0; i < nr_cpus; i++) {
1033 for (counter = 0; counter < nr_counters; counter++)
1034 mmap_read_counter(&mmap_array[i][counter]);
1041 static void start_counter(int i, int counter)
1043 struct perf_event_attr *attr;
1047 if (target_pid == -1 && profile_cpu == -1)
1050 attr = attrs + counter;
1052 attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
1055 attr->sample_type |= PERF_SAMPLE_PERIOD;
1057 attr->sample_freq = freq;
1060 attr->inherit = (cpu < 0) && inherit;
1064 fd[i][counter] = sys_perf_event_open(attr, target_pid, cpu, group_fd, 0);
1066 if (fd[i][counter] < 0) {
1069 if (err == EPERM || err == EACCES)
1070 die("No permission - are you root?\n");
1072 * If it's cycles then fall back to hrtimer
1073 * based cpu-clock-tick sw counter, which
1074 * is always available even if no PMU support:
1076 if (attr->type == PERF_TYPE_HARDWARE
1077 && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
1080 warning(" ... trying to fall back to cpu-clock-ticks\n");
1082 attr->type = PERF_TYPE_SOFTWARE;
1083 attr->config = PERF_COUNT_SW_CPU_CLOCK;
1087 error("perfcounter syscall returned with %d (%s)\n",
1088 fd[i][counter], strerror(err));
1089 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
1092 assert(fd[i][counter] >= 0);
1093 fcntl(fd[i][counter], F_SETFL, O_NONBLOCK);
1096 * First counter acts as the group leader:
1098 if (group && group_fd == -1)
1099 group_fd = fd[i][counter];
1101 event_array[nr_poll].fd = fd[i][counter];
1102 event_array[nr_poll].events = POLLIN;
1105 mmap_array[i][counter].counter = counter;
1106 mmap_array[i][counter].prev = 0;
1107 mmap_array[i][counter].mask = mmap_pages*page_size - 1;
1108 mmap_array[i][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
1109 PROT_READ, MAP_SHARED, fd[i][counter], 0);
1110 if (mmap_array[i][counter].base == MAP_FAILED)
1111 die("failed to mmap with %d (%s)\n", errno, strerror(errno));
1114 static int __cmd_top(void)
1120 if (target_pid != -1)
1121 event__synthesize_thread(target_pid, event__process);
1123 event__synthesize_threads(event__process);
1125 for (i = 0; i < nr_cpus; i++) {
1127 for (counter = 0; counter < nr_counters; counter++)
1128 start_counter(i, counter);
1131 /* Wait for a minimal set of events before starting the snapshot */
1132 poll(event_array, nr_poll, 100);
1136 if (pthread_create(&thread, NULL, display_thread, NULL)) {
1137 printf("Could not create display thread.\n");
1141 if (realtime_prio) {
1142 struct sched_param param;
1144 param.sched_priority = realtime_prio;
1145 if (sched_setscheduler(0, SCHED_FIFO, ¶m)) {
1146 printf("Could not set realtime priority.\n");
1156 if (hits == samples)
1157 ret = poll(event_array, nr_poll, 100);
1163 static const char * const top_usage[] = {
1164 "perf top [<options>]",
1168 static const struct option options[] = {
1169 OPT_CALLBACK('e', "event", NULL, "event",
1170 "event selector. use 'perf list' to list available events",
1172 OPT_INTEGER('c', "count", &default_interval,
1173 "event period to sample"),
1174 OPT_INTEGER('p', "pid", &target_pid,
1175 "profile events on existing pid"),
1176 OPT_BOOLEAN('a', "all-cpus", &system_wide,
1177 "system-wide collection from all CPUs"),
1178 OPT_INTEGER('C', "CPU", &profile_cpu,
1179 "CPU to profile on"),
1180 OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
1181 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
1182 "number of mmap data pages"),
1183 OPT_INTEGER('r', "realtime", &realtime_prio,
1184 "collect data with this RT SCHED_FIFO priority"),
1185 OPT_INTEGER('d', "delay", &delay_secs,
1186 "number of seconds to delay between refreshes"),
1187 OPT_BOOLEAN('D', "dump-symtab", &dump_symtab,
1188 "dump the symbol table used for profiling"),
1189 OPT_INTEGER('f', "count-filter", &count_filter,
1190 "only display functions with more events than this"),
1191 OPT_BOOLEAN('g', "group", &group,
1192 "put the counters into a counter group"),
1193 OPT_BOOLEAN('i', "inherit", &inherit,
1194 "child tasks inherit counters"),
1195 OPT_STRING('s', "sym-annotate", &sym_filter, "symbol name",
1196 "symbol to annotate - requires -k option"),
1197 OPT_BOOLEAN('z', "zero", &zero,
1198 "zero history across updates"),
1199 OPT_INTEGER('F', "freq", &freq,
1200 "profile at this frequency"),
1201 OPT_INTEGER('E', "entries", &print_entries,
1202 "display this many functions"),
1203 OPT_BOOLEAN('v', "verbose", &verbose,
1204 "be more verbose (show counter open errors, etc)"),
1208 int cmd_top(int argc, const char **argv, const char *prefix __used)
1212 symbol__init(sizeof(struct sym_entry));
1214 page_size = sysconf(_SC_PAGE_SIZE);
1216 argc = parse_options(argc, argv, options, top_usage, 0);
1218 usage_with_options(top_usage, options);
1220 /* CPU and PID are mutually exclusive */
1221 if (target_pid != -1 && profile_cpu != -1) {
1222 printf("WARNING: PID switch overriding CPU\n");
1234 parse_source(sym_filter_entry);
1238 * User specified count overrides default frequency.
1240 if (default_interval)
1243 default_interval = freq;
1245 fprintf(stderr, "frequency and count are zero, aborting\n");
1250 * Fill in the ones not specifically initialized via -c:
1252 for (counter = 0; counter < nr_counters; counter++) {
1253 if (attrs[counter].sample_period)
1256 attrs[counter].sample_period = default_interval;
1259 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
1260 assert(nr_cpus <= MAX_NR_CPUS);
1261 assert(nr_cpus >= 0);
1263 if (target_pid != -1 || profile_cpu != -1)
1266 if (print_entries == 0) {
1267 update_print_entries();
1268 signal(SIGWINCH, sig_winch_handler);