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 = 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 map->unmap_ip(map, sym->start),
145 map->unmap_ip(map, sym->end), path);
147 file = popen(command, "r");
151 pthread_mutex_lock(&syme->source_lock);
152 syme->lines_tail = &syme->lines;
153 while (!feof(file)) {
154 struct source_line *src;
158 src = malloc(sizeof(struct source_line));
160 memset(src, 0, sizeof(struct source_line));
162 if (getline(&src->line, &dummy, file) < 0)
167 c = strchr(src->line, '\n');
172 *syme->lines_tail = src;
173 syme->lines_tail = &src->next;
175 if (strlen(src->line)>8 && src->line[8] == ':') {
176 src->eip = strtoull(src->line, NULL, 16);
177 src->eip = map->unmap_ip(map, src->eip);
179 if (strlen(src->line)>8 && src->line[16] == ':') {
180 src->eip = strtoull(src->line, NULL, 16);
181 src->eip = map->unmap_ip(map, src->eip);
186 sym_filter_entry = syme;
187 pthread_mutex_unlock(&syme->source_lock);
190 static void __zero_source_counters(struct sym_entry *syme)
193 struct source_line *line;
197 for (i = 0; i < nr_counters; i++)
203 static void record_precise_ip(struct sym_entry *syme, int counter, u64 ip)
205 struct source_line *line;
207 if (syme != sym_filter_entry)
210 if (pthread_mutex_trylock(&syme->source_lock))
216 for (line = syme->lines; line; line = line->next) {
217 if (line->eip == ip) {
218 line->count[counter]++;
225 pthread_mutex_unlock(&syme->source_lock);
228 static void lookup_sym_source(struct sym_entry *syme)
230 struct symbol *symbol = (struct symbol *)(syme + 1);
231 struct source_line *line;
232 char pattern[PATH_MAX];
234 sprintf(pattern, "<%s>:", symbol->name);
236 pthread_mutex_lock(&syme->source_lock);
237 for (line = syme->lines; line; line = line->next) {
238 if (strstr(line->line, pattern)) {
243 pthread_mutex_unlock(&syme->source_lock);
246 static void show_lines(struct source_line *queue, int count, int total)
249 struct source_line *line;
252 for (i = 0; i < count; i++) {
253 float pcnt = 100.0*(float)line->count[sym_counter]/(float)total;
255 printf("%8li %4.1f%%\t%s\n", line->count[sym_counter], pcnt, line->line);
260 #define TRACE_COUNT 3
262 static void show_details(struct sym_entry *syme)
264 struct symbol *symbol;
265 struct source_line *line;
266 struct source_line *line_queue = NULL;
268 int line_queue_count = 0, total = 0, more = 0;
274 lookup_sym_source(syme);
279 symbol = (struct symbol *)(syme + 1);
280 printf("Showing %s for %s\n", event_name(sym_counter), symbol->name);
281 printf(" Events Pcnt (>=%d%%)\n", sym_pcnt_filter);
283 pthread_mutex_lock(&syme->source_lock);
286 total += line->count[sym_counter];
294 if (!line_queue_count)
298 if (line->count[sym_counter])
299 pcnt = 100.0 * line->count[sym_counter] / (float)total;
300 if (pcnt >= (float)sym_pcnt_filter) {
301 if (displayed <= print_entries)
302 show_lines(line_queue, line_queue_count, total);
304 displayed += line_queue_count;
305 line_queue_count = 0;
307 } else if (line_queue_count > TRACE_COUNT) {
308 line_queue = line_queue->next;
312 line->count[sym_counter] = zero ? 0 : line->count[sym_counter] * 7 / 8;
315 pthread_mutex_unlock(&syme->source_lock);
317 printf("%d lines not displayed, maybe increase display entries [e]\n", more);
321 * Symbols will be added here in event__process_sample and will get out
324 static LIST_HEAD(active_symbols);
325 static pthread_mutex_t active_symbols_lock = PTHREAD_MUTEX_INITIALIZER;
328 * Ordering weight: count-1 * count-2 * ... / count-n
330 static double sym_weight(const struct sym_entry *sym)
332 double weight = sym->snap_count;
335 if (!display_weighted)
338 for (counter = 1; counter < nr_counters-1; counter++)
339 weight *= sym->count[counter];
341 weight /= (sym->count[counter] + 1);
347 static long userspace_samples;
348 static const char CONSOLE_CLEAR[] = "
\e[H
\e[2J";
350 static void __list_insert_active_sym(struct sym_entry *syme)
352 list_add(&syme->node, &active_symbols);
355 static void list_remove_active_sym(struct sym_entry *syme)
357 pthread_mutex_lock(&active_symbols_lock);
358 list_del_init(&syme->node);
359 pthread_mutex_unlock(&active_symbols_lock);
362 static void rb_insert_active_sym(struct rb_root *tree, struct sym_entry *se)
364 struct rb_node **p = &tree->rb_node;
365 struct rb_node *parent = NULL;
366 struct sym_entry *iter;
370 iter = rb_entry(parent, struct sym_entry, rb_node);
372 if (se->weight > iter->weight)
378 rb_link_node(&se->rb_node, parent, p);
379 rb_insert_color(&se->rb_node, tree);
382 static void print_sym_table(void)
385 int counter, snap = !display_weighted ? sym_counter : 0;
386 float samples_per_sec = samples/delay_secs;
387 float ksamples_per_sec = (samples-userspace_samples)/delay_secs;
388 float sum_ksamples = 0.0;
389 struct sym_entry *syme, *n;
390 struct rb_root tmp = RB_ROOT;
393 samples = userspace_samples = 0;
395 /* Sort the active symbols */
396 pthread_mutex_lock(&active_symbols_lock);
397 syme = list_entry(active_symbols.next, struct sym_entry, node);
398 pthread_mutex_unlock(&active_symbols_lock);
400 list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
401 syme->snap_count = syme->count[snap];
402 if (syme->snap_count != 0) {
403 syme->weight = sym_weight(syme);
404 rb_insert_active_sym(&tmp, syme);
405 sum_ksamples += syme->snap_count;
407 for (j = 0; j < nr_counters; j++)
408 syme->count[j] = zero ? 0 : syme->count[j] * 7 / 8;
410 list_remove_active_sym(syme);
416 "------------------------------------------------------------------------------\n");
417 printf( " PerfTop:%8.0f irqs/sec kernel:%4.1f%% [",
419 100.0 - (100.0*((samples_per_sec-ksamples_per_sec)/samples_per_sec)));
421 if (nr_counters == 1 || !display_weighted) {
422 printf("%Ld", (u64)attrs[0].sample_period);
429 if (!display_weighted)
430 printf("%s", event_name(sym_counter));
431 else for (counter = 0; counter < nr_counters; counter++) {
435 printf("%s", event_name(counter));
440 if (target_pid != -1)
441 printf(" (target_pid: %d", target_pid);
445 if (profile_cpu != -1)
446 printf(", cpu: %d)\n", profile_cpu);
448 if (target_pid != -1)
451 printf(", %d CPUs)\n", nr_cpus);
454 printf("------------------------------------------------------------------------------\n\n");
456 if (sym_filter_entry) {
457 show_details(sym_filter_entry);
461 if (nr_counters == 1)
462 printf(" samples pcnt");
464 printf(" weight samples pcnt");
468 printf(" function DSO\n");
469 printf(" %s _______ _____",
470 nr_counters == 1 ? " " : "______");
472 printf(" ________________");
473 printf(" ________________________________ ________________\n\n");
475 for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
479 syme = rb_entry(nd, struct sym_entry, rb_node);
480 sym = (struct symbol *)(syme + 1);
482 if (++printed > print_entries || (int)syme->snap_count < count_filter)
485 pcnt = 100.0 - (100.0 * ((sum_ksamples - syme->snap_count) /
488 if (nr_counters == 1 || !display_weighted)
489 printf("%20.2f ", syme->weight);
491 printf("%9.1f %10ld ", syme->weight, syme->snap_count);
493 percent_color_fprintf(stdout, "%4.1f%%", pcnt);
495 printf(" %016llx", sym->start);
496 printf(" %-32s", sym->name);
497 printf(" %s", syme->map->dso->short_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");
670 prompt_integer(&print_entries, "Enter display entries (lines)");
673 if (nr_counters > 1) {
676 fprintf(stderr, "\nAvailable events:");
677 for (i = 0; i < nr_counters; i++)
678 fprintf(stderr, "\n\t%d %s", i, event_name(i));
680 prompt_integer(&sym_counter, "Enter details event counter");
682 if (sym_counter >= nr_counters) {
683 fprintf(stderr, "Sorry, no such event, using %s.\n", event_name(0));
687 } else sym_counter = 0;
690 prompt_integer(&count_filter, "Enter display event count filter");
693 prompt_percent(&sym_pcnt_filter, "Enter details display event filter (percent)");
697 printf("exiting.\n");
700 prompt_symbol(&sym_filter_entry, "Enter details symbol");
703 if (!sym_filter_entry)
706 struct sym_entry *syme = sym_filter_entry;
708 pthread_mutex_lock(&syme->source_lock);
709 sym_filter_entry = NULL;
710 __zero_source_counters(syme);
711 pthread_mutex_unlock(&syme->source_lock);
715 display_weighted = ~display_weighted;
725 static void *display_thread(void *arg __used)
727 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
728 struct termios tc, save;
733 tc.c_lflag &= ~(ICANON | ECHO);
738 delay_msecs = delay_secs * 1000;
739 tcsetattr(0, TCSANOW, &tc);
745 } while (!poll(&stdin_poll, 1, delay_msecs) == 1);
748 tcsetattr(0, TCSAFLUSH, &save);
756 /* Tag samples to be skipped. */
757 static const char *skip_symbols[] = {
763 "mwait_idle_with_hints",
765 "ppc64_runlatch_off",
766 "pseries_dedicated_idle_sleep",
770 static int symbol_filter(struct map *map, struct symbol *sym)
772 struct sym_entry *syme;
773 const char *name = sym->name;
777 * ppc64 uses function descriptors and appends a '.' to the
778 * start of every instruction address. Remove it.
783 if (!strcmp(name, "_text") ||
784 !strcmp(name, "_etext") ||
785 !strcmp(name, "_sinittext") ||
786 !strncmp("init_module", name, 11) ||
787 !strncmp("cleanup_module", name, 14) ||
788 strstr(name, "_text_start") ||
789 strstr(name, "_text_end"))
792 syme = symbol__priv(sym);
794 pthread_mutex_init(&syme->source_lock, NULL);
795 if (!sym_filter_entry && sym_filter && !strcmp(name, sym_filter))
796 sym_filter_entry = syme;
798 for (i = 0; skip_symbols[i]; i++) {
799 if (!strcmp(skip_symbols[i], name)) {
808 static int parse_symbols(void)
810 if (dsos__load_kernel(vmlinux_name, symbol_filter, 1) <= 0)
814 dsos__fprintf(stderr);
819 static void event__process_sample(const event_t *self, int counter)
821 u64 ip = self->ip.ip;
823 struct sym_entry *syme;
826 switch (self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK) {
827 case PERF_RECORD_MISC_USER: {
828 struct thread *thread = threads__findnew(self->ip.pid);
833 map = thread__find_map(thread, ip);
835 ip = map->map_ip(map, ip);
836 sym = map__find_symbol(map, ip, symbol_filter);
844 * If this is outside of all known maps,
845 * and is a negative address, try to look it
846 * up in the kernel dso, as it might be a
847 * vsyscall or vdso (which executes in user-mode).
849 if ((long long)ip >= 0)
852 case PERF_RECORD_MISC_KERNEL:
853 sym = kernel_maps__find_symbol(ip, &map);
861 syme = symbol__priv(sym);
864 syme->count[counter]++;
865 record_precise_ip(syme, counter, ip);
866 pthread_mutex_lock(&active_symbols_lock);
867 if (list_empty(&syme->node) || !syme->node.next)
868 __list_insert_active_sym(syme);
869 pthread_mutex_unlock(&active_symbols_lock);
875 static void event__process_mmap(event_t *self)
877 struct thread *thread = threads__findnew(self->mmap.pid);
879 if (thread != NULL) {
880 struct map *map = map__new(&self->mmap, NULL, 0);
882 thread__insert_map(thread, map);
886 static void event__process_comm(event_t *self)
888 struct thread *thread = threads__findnew(self->comm.pid);
891 thread__set_comm(thread, self->comm.comm);
894 static int event__process(event_t *event)
896 switch (event->header.type) {
897 case PERF_RECORD_COMM:
898 event__process_comm(event);
900 case PERF_RECORD_MMAP:
901 event__process_mmap(event);
917 static unsigned int mmap_read_head(struct mmap_data *md)
919 struct perf_event_mmap_page *pc = md->base;
922 head = pc->data_head;
928 static void mmap_read_counter(struct mmap_data *md)
930 unsigned int head = mmap_read_head(md);
931 unsigned int old = md->prev;
932 unsigned char *data = md->base + page_size;
936 * If we're further behind than half the buffer, there's a chance
937 * the writer will bite our tail and mess up the samples under us.
939 * If we somehow ended up ahead of the head, we got messed up.
941 * In either case, truncate and restart at head.
944 if (diff > md->mask / 2 || diff < 0) {
945 fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
948 * head points to a known good entry, start there.
953 for (; old != head;) {
954 event_t *event = (event_t *)&data[old & md->mask];
958 size_t size = event->header.size;
961 * Event straddles the mmap boundary -- header should always
962 * be inside due to u64 alignment of output.
964 if ((old & md->mask) + size != ((old + size) & md->mask)) {
965 unsigned int offset = old;
966 unsigned int len = min(sizeof(*event), size), cpy;
967 void *dst = &event_copy;
970 cpy = min(md->mask + 1 - (offset & md->mask), len);
971 memcpy(dst, &data[offset & md->mask], cpy);
980 if (event->header.type == PERF_RECORD_SAMPLE)
981 event__process_sample(event, md->counter);
983 event__process(event);
990 static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
991 static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
993 static void mmap_read(void)
997 for (i = 0; i < nr_cpus; i++) {
998 for (counter = 0; counter < nr_counters; counter++)
999 mmap_read_counter(&mmap_array[i][counter]);
1006 static void start_counter(int i, int counter)
1008 struct perf_event_attr *attr;
1012 if (target_pid == -1 && profile_cpu == -1)
1015 attr = attrs + counter;
1017 attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
1020 attr->sample_type |= PERF_SAMPLE_PERIOD;
1022 attr->sample_freq = freq;
1025 attr->inherit = (cpu < 0) && inherit;
1029 fd[i][counter] = sys_perf_event_open(attr, target_pid, cpu, group_fd, 0);
1031 if (fd[i][counter] < 0) {
1034 if (err == EPERM || err == EACCES)
1035 die("No permission - are you root?\n");
1037 * If it's cycles then fall back to hrtimer
1038 * based cpu-clock-tick sw counter, which
1039 * is always available even if no PMU support:
1041 if (attr->type == PERF_TYPE_HARDWARE
1042 && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
1045 warning(" ... trying to fall back to cpu-clock-ticks\n");
1047 attr->type = PERF_TYPE_SOFTWARE;
1048 attr->config = PERF_COUNT_SW_CPU_CLOCK;
1052 error("perfcounter syscall returned with %d (%s)\n",
1053 fd[i][counter], strerror(err));
1054 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
1057 assert(fd[i][counter] >= 0);
1058 fcntl(fd[i][counter], F_SETFL, O_NONBLOCK);
1061 * First counter acts as the group leader:
1063 if (group && group_fd == -1)
1064 group_fd = fd[i][counter];
1066 event_array[nr_poll].fd = fd[i][counter];
1067 event_array[nr_poll].events = POLLIN;
1070 mmap_array[i][counter].counter = counter;
1071 mmap_array[i][counter].prev = 0;
1072 mmap_array[i][counter].mask = mmap_pages*page_size - 1;
1073 mmap_array[i][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
1074 PROT_READ, MAP_SHARED, fd[i][counter], 0);
1075 if (mmap_array[i][counter].base == MAP_FAILED)
1076 die("failed to mmap with %d (%s)\n", errno, strerror(errno));
1079 static int __cmd_top(void)
1085 if (target_pid != -1)
1086 event__synthesize_thread(target_pid, event__process);
1088 event__synthesize_threads(event__process);
1090 for (i = 0; i < nr_cpus; i++) {
1092 for (counter = 0; counter < nr_counters; counter++)
1093 start_counter(i, counter);
1096 /* Wait for a minimal set of events before starting the snapshot */
1097 poll(event_array, nr_poll, 100);
1101 if (pthread_create(&thread, NULL, display_thread, NULL)) {
1102 printf("Could not create display thread.\n");
1106 if (realtime_prio) {
1107 struct sched_param param;
1109 param.sched_priority = realtime_prio;
1110 if (sched_setscheduler(0, SCHED_FIFO, ¶m)) {
1111 printf("Could not set realtime priority.\n");
1121 if (hits == samples)
1122 ret = poll(event_array, nr_poll, 100);
1128 static const char * const top_usage[] = {
1129 "perf top [<options>]",
1133 static const struct option options[] = {
1134 OPT_CALLBACK('e', "event", NULL, "event",
1135 "event selector. use 'perf list' to list available events",
1137 OPT_INTEGER('c', "count", &default_interval,
1138 "event period to sample"),
1139 OPT_INTEGER('p', "pid", &target_pid,
1140 "profile events on existing pid"),
1141 OPT_BOOLEAN('a', "all-cpus", &system_wide,
1142 "system-wide collection from all CPUs"),
1143 OPT_INTEGER('C', "CPU", &profile_cpu,
1144 "CPU to profile on"),
1145 OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
1146 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
1147 "number of mmap data pages"),
1148 OPT_INTEGER('r', "realtime", &realtime_prio,
1149 "collect data with this RT SCHED_FIFO priority"),
1150 OPT_INTEGER('d', "delay", &delay_secs,
1151 "number of seconds to delay between refreshes"),
1152 OPT_BOOLEAN('D', "dump-symtab", &dump_symtab,
1153 "dump the symbol table used for profiling"),
1154 OPT_INTEGER('f', "count-filter", &count_filter,
1155 "only display functions with more events than this"),
1156 OPT_BOOLEAN('g', "group", &group,
1157 "put the counters into a counter group"),
1158 OPT_BOOLEAN('i', "inherit", &inherit,
1159 "child tasks inherit counters"),
1160 OPT_STRING('s', "sym-annotate", &sym_filter, "symbol name",
1161 "symbol to annotate - requires -k option"),
1162 OPT_BOOLEAN('z', "zero", &zero,
1163 "zero history across updates"),
1164 OPT_INTEGER('F', "freq", &freq,
1165 "profile at this frequency"),
1166 OPT_INTEGER('E', "entries", &print_entries,
1167 "display this many functions"),
1168 OPT_BOOLEAN('v', "verbose", &verbose,
1169 "be more verbose (show counter open errors, etc)"),
1173 int cmd_top(int argc, const char **argv, const char *prefix __used)
1177 symbol__init(sizeof(struct sym_entry));
1179 page_size = sysconf(_SC_PAGE_SIZE);
1181 argc = parse_options(argc, argv, options, top_usage, 0);
1183 usage_with_options(top_usage, options);
1185 /* CPU and PID are mutually exclusive */
1186 if (target_pid != -1 && profile_cpu != -1) {
1187 printf("WARNING: PID switch overriding CPU\n");
1199 parse_source(sym_filter_entry);
1203 * User specified count overrides default frequency.
1205 if (default_interval)
1208 default_interval = freq;
1210 fprintf(stderr, "frequency and count are zero, aborting\n");
1215 * Fill in the ones not specifically initialized via -c:
1217 for (counter = 0; counter < nr_counters; counter++) {
1218 if (attrs[counter].sample_period)
1221 attrs[counter].sample_period = default_interval;
1224 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
1225 assert(nr_cpus <= MAX_NR_CPUS);
1226 assert(nr_cpus >= 0);
1228 if (target_pid != -1 || profile_cpu != -1)