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;
79 static bool hide_kernel_symbols = false;
80 static bool hide_user_symbols = false;
88 unsigned long count[MAX_COUNTERS];
90 struct source_line *next;
93 static char *sym_filter = NULL;
94 struct sym_entry *sym_filter_entry = NULL;
95 static int sym_pcnt_filter = 5;
96 static int sym_counter = 0;
97 static int display_weighted = -1;
104 struct rb_node rb_node;
105 struct list_head node;
106 unsigned long count[MAX_COUNTERS];
107 unsigned long snap_count;
112 struct source_line *source;
113 struct source_line *lines;
114 struct source_line **lines_tail;
115 pthread_mutex_t source_lock;
122 /* most GUI terminals set LINES (although some don't export it) */
123 static int term_rows(void)
125 char *lines_string = getenv("LINES");
128 if (lines_string && (n_lines = atoi(lines_string)) > 0)
133 if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_row)
140 static void update_print_entries(void)
142 print_entries = term_rows();
143 if (print_entries > 9)
147 static void sig_winch_handler(int sig __used)
149 update_print_entries();
152 static void parse_source(struct sym_entry *syme)
157 char command[PATH_MAX*2];
165 pthread_mutex_lock(&syme->source_lock);
169 sym = (struct symbol *)(syme + 1);
171 path = map->dso->long_name;
173 len = sym->end - sym->start;
176 "objdump --start-address=0x%016Lx "
177 "--stop-address=0x%016Lx -dS %s",
178 map->unmap_ip(map, sym->start),
179 map->unmap_ip(map, sym->end), path);
181 file = popen(command, "r");
185 pthread_mutex_lock(&syme->source_lock);
186 syme->lines_tail = &syme->lines;
187 while (!feof(file)) {
188 struct source_line *src;
192 src = malloc(sizeof(struct source_line));
194 memset(src, 0, sizeof(struct source_line));
196 if (getline(&src->line, &dummy, file) < 0)
201 c = strchr(src->line, '\n');
206 *syme->lines_tail = src;
207 syme->lines_tail = &src->next;
209 if (strlen(src->line)>8 && src->line[8] == ':') {
210 src->eip = strtoull(src->line, NULL, 16);
211 src->eip = map->unmap_ip(map, src->eip);
213 if (strlen(src->line)>8 && src->line[16] == ':') {
214 src->eip = strtoull(src->line, NULL, 16);
215 src->eip = map->unmap_ip(map, src->eip);
220 sym_filter_entry = syme;
221 pthread_mutex_unlock(&syme->source_lock);
224 static void __zero_source_counters(struct sym_entry *syme)
227 struct source_line *line;
231 for (i = 0; i < nr_counters; i++)
237 static void record_precise_ip(struct sym_entry *syme, int counter, u64 ip)
239 struct source_line *line;
241 if (syme != sym_filter_entry)
244 if (pthread_mutex_trylock(&syme->source_lock))
250 for (line = syme->lines; line; line = line->next) {
251 if (line->eip == ip) {
252 line->count[counter]++;
259 pthread_mutex_unlock(&syme->source_lock);
262 static void lookup_sym_source(struct sym_entry *syme)
264 struct symbol *symbol = (struct symbol *)(syme + 1);
265 struct source_line *line;
266 char pattern[PATH_MAX];
268 sprintf(pattern, "<%s>:", symbol->name);
270 pthread_mutex_lock(&syme->source_lock);
271 for (line = syme->lines; line; line = line->next) {
272 if (strstr(line->line, pattern)) {
277 pthread_mutex_unlock(&syme->source_lock);
280 static void show_lines(struct source_line *queue, int count, int total)
283 struct source_line *line;
286 for (i = 0; i < count; i++) {
287 float pcnt = 100.0*(float)line->count[sym_counter]/(float)total;
289 printf("%8li %4.1f%%\t%s\n", line->count[sym_counter], pcnt, line->line);
294 #define TRACE_COUNT 3
296 static void show_details(struct sym_entry *syme)
298 struct symbol *symbol;
299 struct source_line *line;
300 struct source_line *line_queue = NULL;
302 int line_queue_count = 0, total = 0, more = 0;
308 lookup_sym_source(syme);
313 symbol = (struct symbol *)(syme + 1);
314 printf("Showing %s for %s\n", event_name(sym_counter), symbol->name);
315 printf(" Events Pcnt (>=%d%%)\n", sym_pcnt_filter);
317 pthread_mutex_lock(&syme->source_lock);
320 total += line->count[sym_counter];
328 if (!line_queue_count)
332 if (line->count[sym_counter])
333 pcnt = 100.0 * line->count[sym_counter] / (float)total;
334 if (pcnt >= (float)sym_pcnt_filter) {
335 if (displayed <= print_entries)
336 show_lines(line_queue, line_queue_count, total);
338 displayed += line_queue_count;
339 line_queue_count = 0;
341 } else if (line_queue_count > TRACE_COUNT) {
342 line_queue = line_queue->next;
346 line->count[sym_counter] = zero ? 0 : line->count[sym_counter] * 7 / 8;
349 pthread_mutex_unlock(&syme->source_lock);
351 printf("%d lines not displayed, maybe increase display entries [e]\n", more);
355 * Symbols will be added here in event__process_sample and will get out
358 static LIST_HEAD(active_symbols);
359 static pthread_mutex_t active_symbols_lock = PTHREAD_MUTEX_INITIALIZER;
362 * Ordering weight: count-1 * count-2 * ... / count-n
364 static double sym_weight(const struct sym_entry *sym)
366 double weight = sym->snap_count;
369 if (!display_weighted)
372 for (counter = 1; counter < nr_counters-1; counter++)
373 weight *= sym->count[counter];
375 weight /= (sym->count[counter] + 1);
381 static long userspace_samples;
382 static const char CONSOLE_CLEAR[] = "
\e[H
\e[2J";
384 static void __list_insert_active_sym(struct sym_entry *syme)
386 list_add(&syme->node, &active_symbols);
389 static void list_remove_active_sym(struct sym_entry *syme)
391 pthread_mutex_lock(&active_symbols_lock);
392 list_del_init(&syme->node);
393 pthread_mutex_unlock(&active_symbols_lock);
396 static void rb_insert_active_sym(struct rb_root *tree, struct sym_entry *se)
398 struct rb_node **p = &tree->rb_node;
399 struct rb_node *parent = NULL;
400 struct sym_entry *iter;
404 iter = rb_entry(parent, struct sym_entry, rb_node);
406 if (se->weight > iter->weight)
412 rb_link_node(&se->rb_node, parent, p);
413 rb_insert_color(&se->rb_node, tree);
416 static void print_sym_table(void)
419 int counter, snap = !display_weighted ? sym_counter : 0;
420 float samples_per_sec = samples/delay_secs;
421 float ksamples_per_sec = (samples-userspace_samples)/delay_secs;
422 float sum_ksamples = 0.0;
423 struct sym_entry *syme, *n;
424 struct rb_root tmp = RB_ROOT;
427 samples = userspace_samples = 0;
429 /* Sort the active symbols */
430 pthread_mutex_lock(&active_symbols_lock);
431 syme = list_entry(active_symbols.next, struct sym_entry, node);
432 pthread_mutex_unlock(&active_symbols_lock);
434 list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
435 syme->snap_count = syme->count[snap];
436 if (syme->snap_count != 0) {
437 if ((hide_user_symbols &&
438 syme->origin == PERF_RECORD_MISC_USER) ||
439 (hide_kernel_symbols &&
440 syme->origin == PERF_RECORD_MISC_KERNEL)) {
441 list_remove_active_sym(syme);
444 syme->weight = sym_weight(syme);
445 rb_insert_active_sym(&tmp, syme);
446 sum_ksamples += syme->snap_count;
448 for (j = 0; j < nr_counters; j++)
449 syme->count[j] = zero ? 0 : syme->count[j] * 7 / 8;
451 list_remove_active_sym(syme);
457 "------------------------------------------------------------------------------\n");
458 printf( " PerfTop:%8.0f irqs/sec kernel:%4.1f%% [",
460 100.0 - (100.0*((samples_per_sec-ksamples_per_sec)/samples_per_sec)));
462 if (nr_counters == 1 || !display_weighted) {
463 printf("%Ld", (u64)attrs[0].sample_period);
470 if (!display_weighted)
471 printf("%s", event_name(sym_counter));
472 else for (counter = 0; counter < nr_counters; counter++) {
476 printf("%s", event_name(counter));
481 if (target_pid != -1)
482 printf(" (target_pid: %d", target_pid);
486 if (profile_cpu != -1)
487 printf(", cpu: %d)\n", profile_cpu);
489 if (target_pid != -1)
492 printf(", %d CPUs)\n", nr_cpus);
495 printf("------------------------------------------------------------------------------\n\n");
497 if (sym_filter_entry) {
498 show_details(sym_filter_entry);
502 if (nr_counters == 1)
503 printf(" samples pcnt");
505 printf(" weight samples pcnt");
509 printf(" function DSO\n");
510 printf(" %s _______ _____",
511 nr_counters == 1 ? " " : "______");
513 printf(" ________________");
514 printf(" ________________________________ ________________\n\n");
516 for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
520 syme = rb_entry(nd, struct sym_entry, rb_node);
521 sym = (struct symbol *)(syme + 1);
523 if (++printed > print_entries || (int)syme->snap_count < count_filter)
526 pcnt = 100.0 - (100.0 * ((sum_ksamples - syme->snap_count) /
529 if (nr_counters == 1 || !display_weighted)
530 printf("%20.2f ", syme->weight);
532 printf("%9.1f %10ld ", syme->weight, syme->snap_count);
534 percent_color_fprintf(stdout, "%4.1f%%", pcnt);
536 printf(" %016llx", sym->start);
537 printf(" %-32s", sym->name);
538 printf(" %s", syme->map->dso->short_name);
543 static void prompt_integer(int *target, const char *msg)
545 char *buf = malloc(0), *p;
549 fprintf(stdout, "\n%s: ", msg);
550 if (getline(&buf, &dummy, stdin) < 0)
553 p = strchr(buf, '\n');
563 tmp = strtoul(buf, NULL, 10);
569 static void prompt_percent(int *target, const char *msg)
573 prompt_integer(&tmp, msg);
574 if (tmp >= 0 && tmp <= 100)
578 static void prompt_symbol(struct sym_entry **target, const char *msg)
580 char *buf = malloc(0), *p;
581 struct sym_entry *syme = *target, *n, *found = NULL;
584 /* zero counters of active symbol */
586 pthread_mutex_lock(&syme->source_lock);
587 __zero_source_counters(syme);
589 pthread_mutex_unlock(&syme->source_lock);
592 fprintf(stdout, "\n%s: ", msg);
593 if (getline(&buf, &dummy, stdin) < 0)
596 p = strchr(buf, '\n');
600 pthread_mutex_lock(&active_symbols_lock);
601 syme = list_entry(active_symbols.next, struct sym_entry, node);
602 pthread_mutex_unlock(&active_symbols_lock);
604 list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
605 struct symbol *sym = (struct symbol *)(syme + 1);
607 if (!strcmp(buf, sym->name)) {
614 fprintf(stderr, "Sorry, %s is not active.\n", sym_filter);
624 static void print_mapped_keys(void)
628 if (sym_filter_entry) {
629 struct symbol *sym = (struct symbol *)(sym_filter_entry+1);
633 fprintf(stdout, "\nMapped keys:\n");
634 fprintf(stdout, "\t[d] display refresh delay. \t(%d)\n", delay_secs);
635 fprintf(stdout, "\t[e] display entries (lines). \t(%d)\n", print_entries);
638 fprintf(stdout, "\t[E] active event counter. \t(%s)\n", event_name(sym_counter));
640 fprintf(stdout, "\t[f] profile display filter (count). \t(%d)\n", count_filter);
643 fprintf(stdout, "\t[F] annotate display filter (percent). \t(%d%%)\n", sym_pcnt_filter);
644 fprintf(stdout, "\t[s] annotate symbol. \t(%s)\n", name?: "NULL");
645 fprintf(stdout, "\t[S] stop annotation.\n");
649 fprintf(stdout, "\t[w] toggle display weighted/count[E]r. \t(%d)\n", display_weighted ? 1 : 0);
652 "\t[K] hide kernel_symbols symbols. \t(%s)\n",
653 hide_kernel_symbols ? "yes" : "no");
655 "\t[U] hide user symbols. \t(%s)\n",
656 hide_user_symbols ? "yes" : "no");
657 fprintf(stdout, "\t[z] toggle sample zeroing. \t(%d)\n", zero ? 1 : 0);
658 fprintf(stdout, "\t[qQ] quit.\n");
661 static int key_mapped(int c)
675 return nr_counters > 1 ? 1 : 0;
679 return vmlinux_name ? 1 : 0;
687 static void handle_keypress(int c)
689 if (!key_mapped(c)) {
690 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
691 struct termios tc, save;
694 fprintf(stdout, "\nEnter selection, or unmapped key to continue: ");
699 tc.c_lflag &= ~(ICANON | ECHO);
702 tcsetattr(0, TCSANOW, &tc);
704 poll(&stdin_poll, 1, -1);
707 tcsetattr(0, TCSAFLUSH, &save);
714 prompt_integer(&delay_secs, "Enter display delay");
719 prompt_integer(&print_entries, "Enter display entries (lines)");
720 if (print_entries == 0) {
721 update_print_entries();
722 signal(SIGWINCH, sig_winch_handler);
724 signal(SIGWINCH, SIG_DFL);
727 if (nr_counters > 1) {
730 fprintf(stderr, "\nAvailable events:");
731 for (i = 0; i < nr_counters; i++)
732 fprintf(stderr, "\n\t%d %s", i, event_name(i));
734 prompt_integer(&sym_counter, "Enter details event counter");
736 if (sym_counter >= nr_counters) {
737 fprintf(stderr, "Sorry, no such event, using %s.\n", event_name(0));
741 } else sym_counter = 0;
744 prompt_integer(&count_filter, "Enter display event count filter");
747 prompt_percent(&sym_pcnt_filter, "Enter details display event filter (percent)");
750 hide_kernel_symbols = !hide_kernel_symbols;
754 printf("exiting.\n");
757 prompt_symbol(&sym_filter_entry, "Enter details symbol");
760 if (!sym_filter_entry)
763 struct sym_entry *syme = sym_filter_entry;
765 pthread_mutex_lock(&syme->source_lock);
766 sym_filter_entry = NULL;
767 __zero_source_counters(syme);
768 pthread_mutex_unlock(&syme->source_lock);
772 hide_user_symbols = !hide_user_symbols;
775 display_weighted = ~display_weighted;
785 static void *display_thread(void *arg __used)
787 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
788 struct termios tc, save;
793 tc.c_lflag &= ~(ICANON | ECHO);
798 delay_msecs = delay_secs * 1000;
799 tcsetattr(0, TCSANOW, &tc);
805 } while (!poll(&stdin_poll, 1, delay_msecs) == 1);
808 tcsetattr(0, TCSAFLUSH, &save);
816 /* Tag samples to be skipped. */
817 static const char *skip_symbols[] = {
823 "mwait_idle_with_hints",
825 "ppc64_runlatch_off",
826 "pseries_dedicated_idle_sleep",
830 static int symbol_filter(struct map *map, struct symbol *sym)
832 struct sym_entry *syme;
833 const char *name = sym->name;
837 * ppc64 uses function descriptors and appends a '.' to the
838 * start of every instruction address. Remove it.
843 if (!strcmp(name, "_text") ||
844 !strcmp(name, "_etext") ||
845 !strcmp(name, "_sinittext") ||
846 !strncmp("init_module", name, 11) ||
847 !strncmp("cleanup_module", name, 14) ||
848 strstr(name, "_text_start") ||
849 strstr(name, "_text_end"))
852 syme = symbol__priv(sym);
854 pthread_mutex_init(&syme->source_lock, NULL);
855 if (!sym_filter_entry && sym_filter && !strcmp(name, sym_filter))
856 sym_filter_entry = syme;
858 for (i = 0; skip_symbols[i]; i++) {
859 if (!strcmp(skip_symbols[i], name)) {
868 static int parse_symbols(void)
870 if (dsos__load_kernel(vmlinux_name, symbol_filter, 1) <= 0)
874 dsos__fprintf(stderr);
879 static void event__process_sample(const event_t *self, int counter)
881 u64 ip = self->ip.ip;
883 struct sym_entry *syme;
885 u8 origin = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
888 case PERF_RECORD_MISC_USER: {
889 struct thread *thread;
891 if (hide_user_symbols)
894 thread = threads__findnew(self->ip.pid);
898 map = thread__find_map(thread, ip);
900 ip = map->map_ip(map, ip);
901 sym = map__find_symbol(map, ip, symbol_filter);
909 * If this is outside of all known maps,
910 * and is a negative address, try to look it
911 * up in the kernel dso, as it might be a
912 * vsyscall or vdso (which executes in user-mode).
914 if ((long long)ip >= 0)
917 case PERF_RECORD_MISC_KERNEL:
918 if (hide_kernel_symbols)
921 sym = kernel_maps__find_symbol(ip, &map);
929 syme = symbol__priv(sym);
932 syme->count[counter]++;
933 syme->origin = origin;
934 record_precise_ip(syme, counter, ip);
935 pthread_mutex_lock(&active_symbols_lock);
936 if (list_empty(&syme->node) || !syme->node.next)
937 __list_insert_active_sym(syme);
938 pthread_mutex_unlock(&active_symbols_lock);
944 static void event__process_mmap(event_t *self)
946 struct thread *thread = threads__findnew(self->mmap.pid);
948 if (thread != NULL) {
949 struct map *map = map__new(&self->mmap, NULL, 0);
951 thread__insert_map(thread, map);
955 static void event__process_comm(event_t *self)
957 struct thread *thread = threads__findnew(self->comm.pid);
960 thread__set_comm(thread, self->comm.comm);
963 static int event__process(event_t *event)
965 switch (event->header.type) {
966 case PERF_RECORD_COMM:
967 event__process_comm(event);
969 case PERF_RECORD_MMAP:
970 event__process_mmap(event);
986 static unsigned int mmap_read_head(struct mmap_data *md)
988 struct perf_event_mmap_page *pc = md->base;
991 head = pc->data_head;
997 static void mmap_read_counter(struct mmap_data *md)
999 unsigned int head = mmap_read_head(md);
1000 unsigned int old = md->prev;
1001 unsigned char *data = md->base + page_size;
1005 * If we're further behind than half the buffer, there's a chance
1006 * the writer will bite our tail and mess up the samples under us.
1008 * If we somehow ended up ahead of the head, we got messed up.
1010 * In either case, truncate and restart at head.
1013 if (diff > md->mask / 2 || diff < 0) {
1014 fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
1017 * head points to a known good entry, start there.
1022 for (; old != head;) {
1023 event_t *event = (event_t *)&data[old & md->mask];
1027 size_t size = event->header.size;
1030 * Event straddles the mmap boundary -- header should always
1031 * be inside due to u64 alignment of output.
1033 if ((old & md->mask) + size != ((old + size) & md->mask)) {
1034 unsigned int offset = old;
1035 unsigned int len = min(sizeof(*event), size), cpy;
1036 void *dst = &event_copy;
1039 cpy = min(md->mask + 1 - (offset & md->mask), len);
1040 memcpy(dst, &data[offset & md->mask], cpy);
1046 event = &event_copy;
1049 if (event->header.type == PERF_RECORD_SAMPLE)
1050 event__process_sample(event, md->counter);
1052 event__process(event);
1059 static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
1060 static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
1062 static void mmap_read(void)
1066 for (i = 0; i < nr_cpus; i++) {
1067 for (counter = 0; counter < nr_counters; counter++)
1068 mmap_read_counter(&mmap_array[i][counter]);
1075 static void start_counter(int i, int counter)
1077 struct perf_event_attr *attr;
1081 if (target_pid == -1 && profile_cpu == -1)
1084 attr = attrs + counter;
1086 attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
1089 attr->sample_type |= PERF_SAMPLE_PERIOD;
1091 attr->sample_freq = freq;
1094 attr->inherit = (cpu < 0) && inherit;
1098 fd[i][counter] = sys_perf_event_open(attr, target_pid, cpu, group_fd, 0);
1100 if (fd[i][counter] < 0) {
1103 if (err == EPERM || err == EACCES)
1104 die("No permission - are you root?\n");
1106 * If it's cycles then fall back to hrtimer
1107 * based cpu-clock-tick sw counter, which
1108 * is always available even if no PMU support:
1110 if (attr->type == PERF_TYPE_HARDWARE
1111 && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
1114 warning(" ... trying to fall back to cpu-clock-ticks\n");
1116 attr->type = PERF_TYPE_SOFTWARE;
1117 attr->config = PERF_COUNT_SW_CPU_CLOCK;
1121 error("perfcounter syscall returned with %d (%s)\n",
1122 fd[i][counter], strerror(err));
1123 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
1126 assert(fd[i][counter] >= 0);
1127 fcntl(fd[i][counter], F_SETFL, O_NONBLOCK);
1130 * First counter acts as the group leader:
1132 if (group && group_fd == -1)
1133 group_fd = fd[i][counter];
1135 event_array[nr_poll].fd = fd[i][counter];
1136 event_array[nr_poll].events = POLLIN;
1139 mmap_array[i][counter].counter = counter;
1140 mmap_array[i][counter].prev = 0;
1141 mmap_array[i][counter].mask = mmap_pages*page_size - 1;
1142 mmap_array[i][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
1143 PROT_READ, MAP_SHARED, fd[i][counter], 0);
1144 if (mmap_array[i][counter].base == MAP_FAILED)
1145 die("failed to mmap with %d (%s)\n", errno, strerror(errno));
1148 static int __cmd_top(void)
1154 if (target_pid != -1)
1155 event__synthesize_thread(target_pid, event__process);
1157 event__synthesize_threads(event__process);
1159 for (i = 0; i < nr_cpus; i++) {
1161 for (counter = 0; counter < nr_counters; counter++)
1162 start_counter(i, counter);
1165 /* Wait for a minimal set of events before starting the snapshot */
1166 poll(event_array, nr_poll, 100);
1170 if (pthread_create(&thread, NULL, display_thread, NULL)) {
1171 printf("Could not create display thread.\n");
1175 if (realtime_prio) {
1176 struct sched_param param;
1178 param.sched_priority = realtime_prio;
1179 if (sched_setscheduler(0, SCHED_FIFO, ¶m)) {
1180 printf("Could not set realtime priority.\n");
1190 if (hits == samples)
1191 ret = poll(event_array, nr_poll, 100);
1197 static const char * const top_usage[] = {
1198 "perf top [<options>]",
1202 static const struct option options[] = {
1203 OPT_CALLBACK('e', "event", NULL, "event",
1204 "event selector. use 'perf list' to list available events",
1206 OPT_INTEGER('c', "count", &default_interval,
1207 "event period to sample"),
1208 OPT_INTEGER('p', "pid", &target_pid,
1209 "profile events on existing pid"),
1210 OPT_BOOLEAN('a', "all-cpus", &system_wide,
1211 "system-wide collection from all CPUs"),
1212 OPT_INTEGER('C', "CPU", &profile_cpu,
1213 "CPU to profile on"),
1214 OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
1215 OPT_BOOLEAN('K', "hide_kernel_symbols", &hide_kernel_symbols,
1216 "hide kernel symbols"),
1217 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
1218 "number of mmap data pages"),
1219 OPT_INTEGER('r', "realtime", &realtime_prio,
1220 "collect data with this RT SCHED_FIFO priority"),
1221 OPT_INTEGER('d', "delay", &delay_secs,
1222 "number of seconds to delay between refreshes"),
1223 OPT_BOOLEAN('D', "dump-symtab", &dump_symtab,
1224 "dump the symbol table used for profiling"),
1225 OPT_INTEGER('f', "count-filter", &count_filter,
1226 "only display functions with more events than this"),
1227 OPT_BOOLEAN('g', "group", &group,
1228 "put the counters into a counter group"),
1229 OPT_BOOLEAN('i', "inherit", &inherit,
1230 "child tasks inherit counters"),
1231 OPT_STRING('s', "sym-annotate", &sym_filter, "symbol name",
1232 "symbol to annotate - requires -k option"),
1233 OPT_BOOLEAN('z', "zero", &zero,
1234 "zero history across updates"),
1235 OPT_INTEGER('F', "freq", &freq,
1236 "profile at this frequency"),
1237 OPT_INTEGER('E', "entries", &print_entries,
1238 "display this many functions"),
1239 OPT_BOOLEAN('U', "hide_user_symbols", &hide_user_symbols,
1240 "hide user symbols"),
1241 OPT_BOOLEAN('v', "verbose", &verbose,
1242 "be more verbose (show counter open errors, etc)"),
1246 int cmd_top(int argc, const char **argv, const char *prefix __used)
1250 symbol__init(sizeof(struct sym_entry));
1252 page_size = sysconf(_SC_PAGE_SIZE);
1254 argc = parse_options(argc, argv, options, top_usage, 0);
1256 usage_with_options(top_usage, options);
1258 /* CPU and PID are mutually exclusive */
1259 if (target_pid != -1 && profile_cpu != -1) {
1260 printf("WARNING: PID switch overriding CPU\n");
1272 parse_source(sym_filter_entry);
1276 * User specified count overrides default frequency.
1278 if (default_interval)
1281 default_interval = freq;
1283 fprintf(stderr, "frequency and count are zero, aborting\n");
1288 * Fill in the ones not specifically initialized via -c:
1290 for (counter = 0; counter < nr_counters; counter++) {
1291 if (attrs[counter].sample_period)
1294 attrs[counter].sample_period = default_interval;
1297 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
1298 assert(nr_cpus <= MAX_NR_CPUS);
1299 assert(nr_cpus >= 0);
1301 if (target_pid != -1 || profile_cpu != -1)
1304 if (print_entries == 0) {
1305 update_print_entries();
1306 signal(SIGWINCH, sig_winch_handler);