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/color.h"
24 #include "util/session.h"
25 #include "util/symbol.h"
26 #include "util/thread.h"
27 #include "util/util.h"
28 #include <linux/rbtree.h>
29 #include "util/parse-options.h"
30 #include "util/parse-events.h"
31 #include "util/cpumap.h"
33 #include "util/debug.h"
47 #include <sys/syscall.h>
48 #include <sys/ioctl.h>
50 #include <sys/prctl.h>
55 #include <linux/unistd.h>
56 #include <linux/types.h>
58 static int fd[MAX_NR_CPUS][MAX_COUNTERS];
60 static int system_wide = 0;
62 static int default_interval = 0;
64 static int count_filter = 5;
65 static int print_entries;
67 static int target_pid = -1;
68 static int inherit = 0;
69 static int profile_cpu = -1;
70 static int nr_cpus = 0;
71 static unsigned int realtime_prio = 0;
73 static unsigned int page_size;
74 static unsigned int mmap_pages = 16;
75 static int freq = 1000; /* 1 KHz */
77 static int delay_secs = 2;
79 static int dump_symtab = 0;
81 static bool hide_kernel_symbols = false;
82 static bool hide_user_symbols = false;
83 static struct winsize winsize;
91 unsigned long count[MAX_COUNTERS];
93 struct source_line *next;
96 static char *sym_filter = NULL;
97 struct sym_entry *sym_filter_entry = NULL;
98 struct sym_entry *sym_filter_entry_sched = NULL;
99 static int sym_pcnt_filter = 5;
100 static int sym_counter = 0;
101 static int display_weighted = -1;
107 struct sym_entry_source {
108 struct source_line *source;
109 struct source_line *lines;
110 struct source_line **lines_tail;
111 pthread_mutex_t lock;
115 struct rb_node rb_node;
116 struct list_head node;
117 unsigned long snap_count;
123 struct sym_entry_source *src;
124 unsigned long count[0];
131 static inline struct symbol *sym_entry__symbol(struct sym_entry *self)
133 return ((void *)self) + symbol_conf.priv_size;
136 static void get_term_dimensions(struct winsize *ws)
138 char *s = getenv("LINES");
141 ws->ws_row = atoi(s);
142 s = getenv("COLUMNS");
144 ws->ws_col = atoi(s);
145 if (ws->ws_row && ws->ws_col)
150 if (ioctl(1, TIOCGWINSZ, ws) == 0 &&
151 ws->ws_row && ws->ws_col)
158 static void update_print_entries(struct winsize *ws)
160 print_entries = ws->ws_row;
162 if (print_entries > 9)
166 static void sig_winch_handler(int sig __used)
168 get_term_dimensions(&winsize);
169 update_print_entries(&winsize);
172 static void parse_source(struct sym_entry *syme)
175 struct sym_entry_source *source;
178 char command[PATH_MAX*2];
185 if (syme->src == NULL) {
186 syme->src = zalloc(sizeof(*source));
187 if (syme->src == NULL)
189 pthread_mutex_init(&syme->src->lock, NULL);
195 pthread_mutex_lock(&source->lock);
199 sym = sym_entry__symbol(syme);
201 path = map->dso->long_name;
203 len = sym->end - sym->start;
206 "objdump --start-address=%#0*Lx --stop-address=%#0*Lx -dS %s",
207 BITS_PER_LONG / 4, map__rip_2objdump(map, sym->start),
208 BITS_PER_LONG / 4, map__rip_2objdump(map, sym->end), path);
210 file = popen(command, "r");
214 pthread_mutex_lock(&source->lock);
215 source->lines_tail = &source->lines;
216 while (!feof(file)) {
217 struct source_line *src;
221 src = malloc(sizeof(struct source_line));
223 memset(src, 0, sizeof(struct source_line));
225 if (getline(&src->line, &dummy, file) < 0)
230 c = strchr(src->line, '\n');
235 *source->lines_tail = src;
236 source->lines_tail = &src->next;
238 src->eip = strtoull(src->line, &sep, 16);
240 src->eip = map__objdump_2ip(map, src->eip);
241 else /* this line has no ip info (e.g. source line) */
246 sym_filter_entry = syme;
247 pthread_mutex_unlock(&source->lock);
250 static void __zero_source_counters(struct sym_entry *syme)
253 struct source_line *line;
255 line = syme->src->lines;
257 for (i = 0; i < nr_counters; i++)
263 static void record_precise_ip(struct sym_entry *syme, int counter, u64 ip)
265 struct source_line *line;
267 if (syme != sym_filter_entry)
270 if (pthread_mutex_trylock(&syme->src->lock))
273 if (syme->src == NULL || syme->src->source == NULL)
276 for (line = syme->src->lines; line; line = line->next) {
277 /* skip lines without IP info */
280 if (line->eip == ip) {
281 line->count[counter]++;
288 pthread_mutex_unlock(&syme->src->lock);
291 #define PATTERN_LEN (BITS_PER_LONG / 4 + 2)
293 static void lookup_sym_source(struct sym_entry *syme)
295 struct symbol *symbol = sym_entry__symbol(syme);
296 struct source_line *line;
297 char pattern[PATTERN_LEN + 1];
299 sprintf(pattern, "%0*Lx <", BITS_PER_LONG / 4,
300 map__rip_2objdump(syme->map, symbol->start));
302 pthread_mutex_lock(&syme->src->lock);
303 for (line = syme->src->lines; line; line = line->next) {
304 if (memcmp(line->line, pattern, PATTERN_LEN) == 0) {
305 syme->src->source = line;
309 pthread_mutex_unlock(&syme->src->lock);
312 static void show_lines(struct source_line *queue, int count, int total)
315 struct source_line *line;
318 for (i = 0; i < count; i++) {
319 float pcnt = 100.0*(float)line->count[sym_counter]/(float)total;
321 printf("%8li %4.1f%%\t%s\n", line->count[sym_counter], pcnt, line->line);
326 #define TRACE_COUNT 3
328 static void show_details(struct sym_entry *syme)
330 struct symbol *symbol;
331 struct source_line *line;
332 struct source_line *line_queue = NULL;
334 int line_queue_count = 0, total = 0, more = 0;
339 if (!syme->src->source)
340 lookup_sym_source(syme);
342 if (!syme->src->source)
345 symbol = sym_entry__symbol(syme);
346 printf("Showing %s for %s\n", event_name(sym_counter), symbol->name);
347 printf(" Events Pcnt (>=%d%%)\n", sym_pcnt_filter);
349 pthread_mutex_lock(&syme->src->lock);
350 line = syme->src->source;
352 total += line->count[sym_counter];
356 line = syme->src->source;
360 if (!line_queue_count)
364 if (line->count[sym_counter])
365 pcnt = 100.0 * line->count[sym_counter] / (float)total;
366 if (pcnt >= (float)sym_pcnt_filter) {
367 if (displayed <= print_entries)
368 show_lines(line_queue, line_queue_count, total);
370 displayed += line_queue_count;
371 line_queue_count = 0;
373 } else if (line_queue_count > TRACE_COUNT) {
374 line_queue = line_queue->next;
378 line->count[sym_counter] = zero ? 0 : line->count[sym_counter] * 7 / 8;
381 pthread_mutex_unlock(&syme->src->lock);
383 printf("%d lines not displayed, maybe increase display entries [e]\n", more);
387 * Symbols will be added here in event__process_sample and will get out
390 static LIST_HEAD(active_symbols);
391 static pthread_mutex_t active_symbols_lock = PTHREAD_MUTEX_INITIALIZER;
394 * Ordering weight: count-1 * count-2 * ... / count-n
396 static double sym_weight(const struct sym_entry *sym)
398 double weight = sym->snap_count;
401 if (!display_weighted)
404 for (counter = 1; counter < nr_counters-1; counter++)
405 weight *= sym->count[counter];
407 weight /= (sym->count[counter] + 1);
413 static long userspace_samples;
414 static const char CONSOLE_CLEAR[] = "
\e[H
\e[2J";
416 static void __list_insert_active_sym(struct sym_entry *syme)
418 list_add(&syme->node, &active_symbols);
421 static void list_remove_active_sym(struct sym_entry *syme)
423 pthread_mutex_lock(&active_symbols_lock);
424 list_del_init(&syme->node);
425 pthread_mutex_unlock(&active_symbols_lock);
428 static void rb_insert_active_sym(struct rb_root *tree, struct sym_entry *se)
430 struct rb_node **p = &tree->rb_node;
431 struct rb_node *parent = NULL;
432 struct sym_entry *iter;
436 iter = rb_entry(parent, struct sym_entry, rb_node);
438 if (se->weight > iter->weight)
444 rb_link_node(&se->rb_node, parent, p);
445 rb_insert_color(&se->rb_node, tree);
448 static void print_sym_table(void)
451 int counter, snap = !display_weighted ? sym_counter : 0;
452 float samples_per_sec = samples/delay_secs;
453 float ksamples_per_sec = (samples-userspace_samples)/delay_secs;
454 float sum_ksamples = 0.0;
455 struct sym_entry *syme, *n;
456 struct rb_root tmp = RB_ROOT;
458 int sym_width = 0, dso_width = 0, dso_short_width = 0;
459 const int win_width = winsize.ws_col - 1;
461 samples = userspace_samples = 0;
463 /* Sort the active symbols */
464 pthread_mutex_lock(&active_symbols_lock);
465 syme = list_entry(active_symbols.next, struct sym_entry, node);
466 pthread_mutex_unlock(&active_symbols_lock);
468 list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
469 syme->snap_count = syme->count[snap];
470 if (syme->snap_count != 0) {
472 if ((hide_user_symbols &&
473 syme->origin == PERF_RECORD_MISC_USER) ||
474 (hide_kernel_symbols &&
475 syme->origin == PERF_RECORD_MISC_KERNEL)) {
476 list_remove_active_sym(syme);
479 syme->weight = sym_weight(syme);
480 rb_insert_active_sym(&tmp, syme);
481 sum_ksamples += syme->snap_count;
483 for (j = 0; j < nr_counters; j++)
484 syme->count[j] = zero ? 0 : syme->count[j] * 7 / 8;
486 list_remove_active_sym(syme);
491 printf("%-*.*s\n", win_width, win_width, graph_dotted_line);
492 printf( " PerfTop:%8.0f irqs/sec kernel:%4.1f%% [",
494 100.0 - (100.0*((samples_per_sec-ksamples_per_sec)/samples_per_sec)));
496 if (nr_counters == 1 || !display_weighted) {
497 printf("%Ld", (u64)attrs[0].sample_period);
504 if (!display_weighted)
505 printf("%s", event_name(sym_counter));
506 else for (counter = 0; counter < nr_counters; counter++) {
510 printf("%s", event_name(counter));
515 if (target_pid != -1)
516 printf(" (target_pid: %d", target_pid);
520 if (profile_cpu != -1)
521 printf(", cpu: %d)\n", profile_cpu);
523 if (target_pid != -1)
526 printf(", %d CPUs)\n", nr_cpus);
529 printf("%-*.*s\n", win_width, win_width, graph_dotted_line);
531 if (sym_filter_entry) {
532 show_details(sym_filter_entry);
537 * Find the longest symbol name that will be displayed
539 for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
540 syme = rb_entry(nd, struct sym_entry, rb_node);
541 if (++printed > print_entries ||
542 (int)syme->snap_count < count_filter)
545 if (syme->map->dso->long_name_len > dso_width)
546 dso_width = syme->map->dso->long_name_len;
548 if (syme->map->dso->short_name_len > dso_short_width)
549 dso_short_width = syme->map->dso->short_name_len;
551 if (syme->name_len > sym_width)
552 sym_width = syme->name_len;
557 if (sym_width + dso_width > winsize.ws_col - 29) {
558 dso_width = dso_short_width;
559 if (sym_width + dso_width > winsize.ws_col - 29)
560 sym_width = winsize.ws_col - dso_width - 29;
563 if (nr_counters == 1)
564 printf(" samples pcnt");
566 printf(" weight samples pcnt");
570 printf(" %-*.*s DSO\n", sym_width, sym_width, "function");
571 printf(" %s _______ _____",
572 nr_counters == 1 ? " " : "______");
574 printf(" ________________");
575 printf(" %-*.*s", sym_width, sym_width, graph_line);
576 printf(" %-*.*s", dso_width, dso_width, graph_line);
579 for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
583 syme = rb_entry(nd, struct sym_entry, rb_node);
584 sym = sym_entry__symbol(syme);
586 if (++printed > print_entries || (int)syme->snap_count < count_filter)
589 pcnt = 100.0 - (100.0 * ((sum_ksamples - syme->snap_count) /
592 if (nr_counters == 1 || !display_weighted)
593 printf("%20.2f ", syme->weight);
595 printf("%9.1f %10ld ", syme->weight, syme->snap_count);
597 percent_color_fprintf(stdout, "%4.1f%%", pcnt);
599 printf(" %016llx", sym->start);
600 printf(" %-*.*s", sym_width, sym_width, sym->name);
601 printf(" %-*.*s\n", dso_width, dso_width,
602 dso_width >= syme->map->dso->long_name_len ?
603 syme->map->dso->long_name :
604 syme->map->dso->short_name);
608 static void prompt_integer(int *target, const char *msg)
610 char *buf = malloc(0), *p;
614 fprintf(stdout, "\n%s: ", msg);
615 if (getline(&buf, &dummy, stdin) < 0)
618 p = strchr(buf, '\n');
628 tmp = strtoul(buf, NULL, 10);
634 static void prompt_percent(int *target, const char *msg)
638 prompt_integer(&tmp, msg);
639 if (tmp >= 0 && tmp <= 100)
643 static void prompt_symbol(struct sym_entry **target, const char *msg)
645 char *buf = malloc(0), *p;
646 struct sym_entry *syme = *target, *n, *found = NULL;
649 /* zero counters of active symbol */
651 pthread_mutex_lock(&syme->src->lock);
652 __zero_source_counters(syme);
654 pthread_mutex_unlock(&syme->src->lock);
657 fprintf(stdout, "\n%s: ", msg);
658 if (getline(&buf, &dummy, stdin) < 0)
661 p = strchr(buf, '\n');
665 pthread_mutex_lock(&active_symbols_lock);
666 syme = list_entry(active_symbols.next, struct sym_entry, node);
667 pthread_mutex_unlock(&active_symbols_lock);
669 list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
670 struct symbol *sym = sym_entry__symbol(syme);
672 if (!strcmp(buf, sym->name)) {
679 fprintf(stderr, "Sorry, %s is not active.\n", buf);
689 static void print_mapped_keys(void)
693 if (sym_filter_entry) {
694 struct symbol *sym = sym_entry__symbol(sym_filter_entry);
698 fprintf(stdout, "\nMapped keys:\n");
699 fprintf(stdout, "\t[d] display refresh delay. \t(%d)\n", delay_secs);
700 fprintf(stdout, "\t[e] display entries (lines). \t(%d)\n", print_entries);
703 fprintf(stdout, "\t[E] active event counter. \t(%s)\n", event_name(sym_counter));
705 fprintf(stdout, "\t[f] profile display filter (count). \t(%d)\n", count_filter);
707 fprintf(stdout, "\t[F] annotate display filter (percent). \t(%d%%)\n", sym_pcnt_filter);
708 fprintf(stdout, "\t[s] annotate symbol. \t(%s)\n", name?: "NULL");
709 fprintf(stdout, "\t[S] stop annotation.\n");
712 fprintf(stdout, "\t[w] toggle display weighted/count[E]r. \t(%d)\n", display_weighted ? 1 : 0);
715 "\t[K] hide kernel_symbols symbols. \t(%s)\n",
716 hide_kernel_symbols ? "yes" : "no");
718 "\t[U] hide user symbols. \t(%s)\n",
719 hide_user_symbols ? "yes" : "no");
720 fprintf(stdout, "\t[z] toggle sample zeroing. \t(%d)\n", zero ? 1 : 0);
721 fprintf(stdout, "\t[qQ] quit.\n");
724 static int key_mapped(int c)
741 return nr_counters > 1 ? 1 : 0;
749 static void handle_keypress(int c)
751 if (!key_mapped(c)) {
752 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
753 struct termios tc, save;
756 fprintf(stdout, "\nEnter selection, or unmapped key to continue: ");
761 tc.c_lflag &= ~(ICANON | ECHO);
764 tcsetattr(0, TCSANOW, &tc);
766 poll(&stdin_poll, 1, -1);
769 tcsetattr(0, TCSAFLUSH, &save);
776 prompt_integer(&delay_secs, "Enter display delay");
781 prompt_integer(&print_entries, "Enter display entries (lines)");
782 if (print_entries == 0) {
783 sig_winch_handler(SIGWINCH);
784 signal(SIGWINCH, sig_winch_handler);
786 signal(SIGWINCH, SIG_DFL);
789 if (nr_counters > 1) {
792 fprintf(stderr, "\nAvailable events:");
793 for (i = 0; i < nr_counters; i++)
794 fprintf(stderr, "\n\t%d %s", i, event_name(i));
796 prompt_integer(&sym_counter, "Enter details event counter");
798 if (sym_counter >= nr_counters) {
799 fprintf(stderr, "Sorry, no such event, using %s.\n", event_name(0));
803 } else sym_counter = 0;
806 prompt_integer(&count_filter, "Enter display event count filter");
809 prompt_percent(&sym_pcnt_filter, "Enter details display event filter (percent)");
812 hide_kernel_symbols = !hide_kernel_symbols;
816 printf("exiting.\n");
818 dsos__fprintf(stderr);
821 prompt_symbol(&sym_filter_entry, "Enter details symbol");
824 if (!sym_filter_entry)
827 struct sym_entry *syme = sym_filter_entry;
829 pthread_mutex_lock(&syme->src->lock);
830 sym_filter_entry = NULL;
831 __zero_source_counters(syme);
832 pthread_mutex_unlock(&syme->src->lock);
836 hide_user_symbols = !hide_user_symbols;
839 display_weighted = ~display_weighted;
849 static void *display_thread(void *arg __used)
851 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
852 struct termios tc, save;
857 tc.c_lflag &= ~(ICANON | ECHO);
862 delay_msecs = delay_secs * 1000;
863 tcsetattr(0, TCSANOW, &tc);
869 } while (!poll(&stdin_poll, 1, delay_msecs) == 1);
872 tcsetattr(0, TCSAFLUSH, &save);
880 /* Tag samples to be skipped. */
881 static const char *skip_symbols[] = {
887 "mwait_idle_with_hints",
889 "ppc64_runlatch_off",
890 "pseries_dedicated_idle_sleep",
894 static int symbol_filter(struct map *map, struct symbol *sym)
896 struct sym_entry *syme;
897 const char *name = sym->name;
901 * ppc64 uses function descriptors and appends a '.' to the
902 * start of every instruction address. Remove it.
907 if (!strcmp(name, "_text") ||
908 !strcmp(name, "_etext") ||
909 !strcmp(name, "_sinittext") ||
910 !strncmp("init_module", name, 11) ||
911 !strncmp("cleanup_module", name, 14) ||
912 strstr(name, "_text_start") ||
913 strstr(name, "_text_end"))
916 syme = symbol__priv(sym);
920 if (!sym_filter_entry && sym_filter && !strcmp(name, sym_filter)) {
921 /* schedule initial sym_filter_entry setup */
922 sym_filter_entry_sched = syme;
926 for (i = 0; skip_symbols[i]; i++) {
927 if (!strcmp(skip_symbols[i], name)) {
934 syme->name_len = strlen(sym->name);
939 static void event__process_sample(const event_t *self,
940 struct perf_session *session, int counter)
942 u64 ip = self->ip.ip;
943 struct sym_entry *syme;
944 struct addr_location al;
945 u8 origin = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
950 case PERF_RECORD_MISC_USER:
952 if (hide_user_symbols)
955 case PERF_RECORD_MISC_KERNEL:
956 if (hide_kernel_symbols)
963 if (event__preprocess_sample(self, session, &al, symbol_filter) < 0 ||
967 if (al.sym == NULL) {
969 * As we do lazy loading of symtabs we only will know if the
970 * specified vmlinux file is invalid when we actually have a
971 * hit in kernel space and then try to load it. So if we get
972 * here and there are _no_ symbols in the DSO backing the
973 * kernel map, bail out.
975 * We may never get here, for instance, if we use -K/
976 * --hide-kernel-symbols, even if the user specifies an
977 * invalid --vmlinux ;-)
979 if (al.map == session->vmlinux_maps[MAP__FUNCTION] &&
980 RB_EMPTY_ROOT(&al.map->dso->symbols[MAP__FUNCTION])) {
981 pr_err("The %s file can't be used\n",
982 symbol_conf.vmlinux_name);
989 /* let's see, whether we need to install initial sym_filter_entry */
990 if (sym_filter_entry_sched) {
991 sym_filter_entry = sym_filter_entry_sched;
992 sym_filter_entry_sched = NULL;
993 parse_source(sym_filter_entry);
996 syme = symbol__priv(al.sym);
998 syme->count[counter]++;
999 syme->origin = origin;
1000 record_precise_ip(syme, counter, ip);
1001 pthread_mutex_lock(&active_symbols_lock);
1002 if (list_empty(&syme->node) || !syme->node.next)
1003 __list_insert_active_sym(syme);
1004 pthread_mutex_unlock(&active_symbols_lock);
1008 static int event__process(event_t *event, struct perf_session *session)
1010 switch (event->header.type) {
1011 case PERF_RECORD_COMM:
1012 event__process_comm(event, session);
1014 case PERF_RECORD_MMAP:
1015 event__process_mmap(event, session);
1017 case PERF_RECORD_FORK:
1018 case PERF_RECORD_EXIT:
1019 event__process_task(event, session);
1035 static unsigned int mmap_read_head(struct mmap_data *md)
1037 struct perf_event_mmap_page *pc = md->base;
1040 head = pc->data_head;
1046 static void perf_session__mmap_read_counter(struct perf_session *self,
1047 struct mmap_data *md)
1049 unsigned int head = mmap_read_head(md);
1050 unsigned int old = md->prev;
1051 unsigned char *data = md->base + page_size;
1055 * If we're further behind than half the buffer, there's a chance
1056 * the writer will bite our tail and mess up the samples under us.
1058 * If we somehow ended up ahead of the head, we got messed up.
1060 * In either case, truncate and restart at head.
1063 if (diff > md->mask / 2 || diff < 0) {
1064 fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
1067 * head points to a known good entry, start there.
1072 for (; old != head;) {
1073 event_t *event = (event_t *)&data[old & md->mask];
1077 size_t size = event->header.size;
1080 * Event straddles the mmap boundary -- header should always
1081 * be inside due to u64 alignment of output.
1083 if ((old & md->mask) + size != ((old + size) & md->mask)) {
1084 unsigned int offset = old;
1085 unsigned int len = min(sizeof(*event), size), cpy;
1086 void *dst = &event_copy;
1089 cpy = min(md->mask + 1 - (offset & md->mask), len);
1090 memcpy(dst, &data[offset & md->mask], cpy);
1096 event = &event_copy;
1099 if (event->header.type == PERF_RECORD_SAMPLE)
1100 event__process_sample(event, self, md->counter);
1102 event__process(event, self);
1109 static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
1110 static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
1112 static void perf_session__mmap_read(struct perf_session *self)
1116 for (i = 0; i < nr_cpus; i++) {
1117 for (counter = 0; counter < nr_counters; counter++)
1118 perf_session__mmap_read_counter(self, &mmap_array[i][counter]);
1125 static void start_counter(int i, int counter)
1127 struct perf_event_attr *attr;
1131 if (target_pid == -1 && profile_cpu == -1)
1134 attr = attrs + counter;
1136 attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
1139 attr->sample_type |= PERF_SAMPLE_PERIOD;
1141 attr->sample_freq = freq;
1144 attr->inherit = (cpu < 0) && inherit;
1148 fd[i][counter] = sys_perf_event_open(attr, target_pid, cpu, group_fd, 0);
1150 if (fd[i][counter] < 0) {
1153 if (err == EPERM || err == EACCES)
1154 die("No permission - are you root?\n");
1156 * If it's cycles then fall back to hrtimer
1157 * based cpu-clock-tick sw counter, which
1158 * is always available even if no PMU support:
1160 if (attr->type == PERF_TYPE_HARDWARE
1161 && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
1164 warning(" ... trying to fall back to cpu-clock-ticks\n");
1166 attr->type = PERF_TYPE_SOFTWARE;
1167 attr->config = PERF_COUNT_SW_CPU_CLOCK;
1171 error("perfcounter syscall returned with %d (%s)\n",
1172 fd[i][counter], strerror(err));
1173 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
1176 assert(fd[i][counter] >= 0);
1177 fcntl(fd[i][counter], F_SETFL, O_NONBLOCK);
1180 * First counter acts as the group leader:
1182 if (group && group_fd == -1)
1183 group_fd = fd[i][counter];
1185 event_array[nr_poll].fd = fd[i][counter];
1186 event_array[nr_poll].events = POLLIN;
1189 mmap_array[i][counter].counter = counter;
1190 mmap_array[i][counter].prev = 0;
1191 mmap_array[i][counter].mask = mmap_pages*page_size - 1;
1192 mmap_array[i][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
1193 PROT_READ, MAP_SHARED, fd[i][counter], 0);
1194 if (mmap_array[i][counter].base == MAP_FAILED)
1195 die("failed to mmap with %d (%s)\n", errno, strerror(errno));
1198 static int __cmd_top(void)
1204 * FIXME: perf_session__new should allow passing a O_MMAP, so that all this
1205 * mmap reading, etc is encapsulated in it. Use O_WRONLY for now.
1207 struct perf_session *session = perf_session__new(NULL, O_WRONLY, false);
1208 if (session == NULL)
1211 if (target_pid != -1)
1212 event__synthesize_thread(target_pid, event__process, session);
1214 event__synthesize_threads(event__process, session);
1216 for (i = 0; i < nr_cpus; i++) {
1218 for (counter = 0; counter < nr_counters; counter++)
1219 start_counter(i, counter);
1222 /* Wait for a minimal set of events before starting the snapshot */
1223 poll(event_array, nr_poll, 100);
1225 perf_session__mmap_read(session);
1227 if (pthread_create(&thread, NULL, display_thread, NULL)) {
1228 printf("Could not create display thread.\n");
1232 if (realtime_prio) {
1233 struct sched_param param;
1235 param.sched_priority = realtime_prio;
1236 if (sched_setscheduler(0, SCHED_FIFO, ¶m)) {
1237 printf("Could not set realtime priority.\n");
1245 perf_session__mmap_read(session);
1247 if (hits == samples)
1248 ret = poll(event_array, nr_poll, 100);
1254 static const char * const top_usage[] = {
1255 "perf top [<options>]",
1259 static const struct option options[] = {
1260 OPT_CALLBACK('e', "event", NULL, "event",
1261 "event selector. use 'perf list' to list available events",
1263 OPT_INTEGER('c', "count", &default_interval,
1264 "event period to sample"),
1265 OPT_INTEGER('p', "pid", &target_pid,
1266 "profile events on existing pid"),
1267 OPT_BOOLEAN('a', "all-cpus", &system_wide,
1268 "system-wide collection from all CPUs"),
1269 OPT_INTEGER('C', "CPU", &profile_cpu,
1270 "CPU to profile on"),
1271 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1272 "file", "vmlinux pathname"),
1273 OPT_BOOLEAN('K', "hide_kernel_symbols", &hide_kernel_symbols,
1274 "hide kernel symbols"),
1275 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
1276 "number of mmap data pages"),
1277 OPT_INTEGER('r', "realtime", &realtime_prio,
1278 "collect data with this RT SCHED_FIFO priority"),
1279 OPT_INTEGER('d', "delay", &delay_secs,
1280 "number of seconds to delay between refreshes"),
1281 OPT_BOOLEAN('D', "dump-symtab", &dump_symtab,
1282 "dump the symbol table used for profiling"),
1283 OPT_INTEGER('f', "count-filter", &count_filter,
1284 "only display functions with more events than this"),
1285 OPT_BOOLEAN('g', "group", &group,
1286 "put the counters into a counter group"),
1287 OPT_BOOLEAN('i', "inherit", &inherit,
1288 "child tasks inherit counters"),
1289 OPT_STRING('s', "sym-annotate", &sym_filter, "symbol name",
1290 "symbol to annotate"),
1291 OPT_BOOLEAN('z', "zero", &zero,
1292 "zero history across updates"),
1293 OPT_INTEGER('F', "freq", &freq,
1294 "profile at this frequency"),
1295 OPT_INTEGER('E', "entries", &print_entries,
1296 "display this many functions"),
1297 OPT_BOOLEAN('U', "hide_user_symbols", &hide_user_symbols,
1298 "hide user symbols"),
1299 OPT_BOOLEAN('v', "verbose", &verbose,
1300 "be more verbose (show counter open errors, etc)"),
1304 int cmd_top(int argc, const char **argv, const char *prefix __used)
1308 page_size = sysconf(_SC_PAGE_SIZE);
1310 argc = parse_options(argc, argv, options, top_usage, 0);
1312 usage_with_options(top_usage, options);
1314 /* CPU and PID are mutually exclusive */
1315 if (target_pid != -1 && profile_cpu != -1) {
1316 printf("WARNING: PID switch overriding CPU\n");
1324 symbol_conf.priv_size = (sizeof(struct sym_entry) +
1325 (nr_counters + 1) * sizeof(unsigned long));
1327 symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL);
1328 if (symbol__init() < 0)
1335 * User specified count overrides default frequency.
1337 if (default_interval)
1340 default_interval = freq;
1342 fprintf(stderr, "frequency and count are zero, aborting\n");
1347 * Fill in the ones not specifically initialized via -c:
1349 for (counter = 0; counter < nr_counters; counter++) {
1350 if (attrs[counter].sample_period)
1353 attrs[counter].sample_period = default_interval;
1356 if (target_pid != -1 || profile_cpu != -1)
1359 nr_cpus = read_cpu_map();
1361 get_term_dimensions(&winsize);
1362 if (print_entries == 0) {
1363 update_print_entries(&winsize);
1364 signal(SIGWINCH, sig_winch_handler);