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/evlist.h"
25 #include "util/evsel.h"
26 #include "util/session.h"
27 #include "util/symbol.h"
28 #include "util/thread.h"
29 #include "util/util.h"
30 #include <linux/rbtree.h>
31 #include "util/parse-options.h"
32 #include "util/parse-events.h"
33 #include "util/cpumap.h"
34 #include "util/xyarray.h"
36 #include "util/debug.h"
51 #include <sys/syscall.h>
52 #include <sys/ioctl.h>
54 #include <sys/prctl.h>
59 #include <linux/unistd.h>
60 #include <linux/types.h>
62 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
64 struct perf_evlist *evsel_list;
66 static bool system_wide = false;
68 static int default_interval = 0;
70 static int count_filter = 5;
71 static int print_entries;
73 static int target_pid = -1;
74 static int target_tid = -1;
75 static struct thread_map *threads;
76 static bool inherit = false;
77 static struct cpu_map *cpus;
78 static int realtime_prio = 0;
79 static bool group = false;
80 static unsigned int page_size;
81 static unsigned int mmap_pages = 128;
82 static int freq = 1000; /* 1 KHz */
84 static int delay_secs = 2;
85 static bool zero = false;
86 static bool dump_symtab = false;
88 static bool hide_kernel_symbols = false;
89 static bool hide_user_symbols = false;
90 static struct winsize winsize;
98 unsigned long count[MAX_COUNTERS];
100 struct source_line *next;
103 static const char *sym_filter = NULL;
104 struct sym_entry *sym_filter_entry = NULL;
105 struct sym_entry *sym_filter_entry_sched = NULL;
106 static int sym_pcnt_filter = 5;
107 static int sym_counter = 0;
108 static struct perf_evsel *sym_evsel = NULL;
109 static int display_weighted = -1;
110 static const char *cpu_list;
116 struct sym_entry_source {
117 struct source_line *source;
118 struct source_line *lines;
119 struct source_line **lines_tail;
120 pthread_mutex_t lock;
124 struct rb_node rb_node;
125 struct list_head node;
126 unsigned long snap_count;
132 struct sym_entry_source *src;
133 unsigned long count[0];
140 static inline struct symbol *sym_entry__symbol(struct sym_entry *self)
142 return ((void *)self) + symbol_conf.priv_size;
145 void get_term_dimensions(struct winsize *ws)
147 char *s = getenv("LINES");
150 ws->ws_row = atoi(s);
151 s = getenv("COLUMNS");
153 ws->ws_col = atoi(s);
154 if (ws->ws_row && ws->ws_col)
159 if (ioctl(1, TIOCGWINSZ, ws) == 0 &&
160 ws->ws_row && ws->ws_col)
167 static void update_print_entries(struct winsize *ws)
169 print_entries = ws->ws_row;
171 if (print_entries > 9)
175 static void sig_winch_handler(int sig __used)
177 get_term_dimensions(&winsize);
178 update_print_entries(&winsize);
181 static int parse_source(struct sym_entry *syme)
184 struct sym_entry_source *source;
187 char command[PATH_MAX*2];
194 sym = sym_entry__symbol(syme);
198 * We can't annotate with just /proc/kallsyms
200 if (map->dso->origin == DSO__ORIG_KERNEL)
203 if (syme->src == NULL) {
204 syme->src = zalloc(sizeof(*source));
205 if (syme->src == NULL)
207 pthread_mutex_init(&syme->src->lock, NULL);
213 pthread_mutex_lock(&source->lock);
216 path = map->dso->long_name;
218 len = sym->end - sym->start;
221 "objdump --start-address=%#0*" PRIx64 " --stop-address=%#0*" PRIx64 " -dS %s",
222 BITS_PER_LONG / 4, map__rip_2objdump(map, sym->start),
223 BITS_PER_LONG / 4, map__rip_2objdump(map, sym->end), path);
225 file = popen(command, "r");
229 pthread_mutex_lock(&source->lock);
230 source->lines_tail = &source->lines;
231 while (!feof(file)) {
232 struct source_line *src;
236 src = malloc(sizeof(struct source_line));
238 memset(src, 0, sizeof(struct source_line));
240 if (getline(&src->line, &dummy, file) < 0)
245 c = strchr(src->line, '\n');
250 *source->lines_tail = src;
251 source->lines_tail = &src->next;
253 src->eip = strtoull(src->line, &sep, 16);
255 src->eip = map__objdump_2ip(map, src->eip);
256 else /* this line has no ip info (e.g. source line) */
261 sym_filter_entry = syme;
262 pthread_mutex_unlock(&source->lock);
266 static void __zero_source_counters(struct sym_entry *syme)
269 struct source_line *line;
271 line = syme->src->lines;
273 for (i = 0; i < evsel_list->nr_entries; i++)
279 static void record_precise_ip(struct sym_entry *syme, int counter, u64 ip)
281 struct source_line *line;
283 if (syme != sym_filter_entry)
286 if (pthread_mutex_trylock(&syme->src->lock))
289 if (syme->src == NULL || syme->src->source == NULL)
292 for (line = syme->src->lines; line; line = line->next) {
293 /* skip lines without IP info */
296 if (line->eip == ip) {
297 line->count[counter]++;
304 pthread_mutex_unlock(&syme->src->lock);
307 #define PATTERN_LEN (BITS_PER_LONG / 4 + 2)
309 static void lookup_sym_source(struct sym_entry *syme)
311 struct symbol *symbol = sym_entry__symbol(syme);
312 struct source_line *line;
313 char pattern[PATTERN_LEN + 1];
315 sprintf(pattern, "%0*" PRIx64 " <", BITS_PER_LONG / 4,
316 map__rip_2objdump(syme->map, symbol->start));
318 pthread_mutex_lock(&syme->src->lock);
319 for (line = syme->src->lines; line; line = line->next) {
320 if (memcmp(line->line, pattern, PATTERN_LEN) == 0) {
321 syme->src->source = line;
325 pthread_mutex_unlock(&syme->src->lock);
328 static void show_lines(struct source_line *queue, int count, int total)
331 struct source_line *line;
334 for (i = 0; i < count; i++) {
335 float pcnt = 100.0*(float)line->count[sym_counter]/(float)total;
337 printf("%8li %4.1f%%\t%s\n", line->count[sym_counter], pcnt, line->line);
342 #define TRACE_COUNT 3
344 static void show_details(struct sym_entry *syme)
346 struct symbol *symbol;
347 struct source_line *line;
348 struct source_line *line_queue = NULL;
350 int line_queue_count = 0, total = 0, more = 0;
355 if (!syme->src->source)
356 lookup_sym_source(syme);
358 if (!syme->src->source)
361 symbol = sym_entry__symbol(syme);
362 printf("Showing %s for %s\n", event_name(sym_evsel), symbol->name);
363 printf(" Events Pcnt (>=%d%%)\n", sym_pcnt_filter);
365 pthread_mutex_lock(&syme->src->lock);
366 line = syme->src->source;
368 total += line->count[sym_counter];
372 line = syme->src->source;
376 if (!line_queue_count)
380 if (line->count[sym_counter])
381 pcnt = 100.0 * line->count[sym_counter] / (float)total;
382 if (pcnt >= (float)sym_pcnt_filter) {
383 if (displayed <= print_entries)
384 show_lines(line_queue, line_queue_count, total);
386 displayed += line_queue_count;
387 line_queue_count = 0;
389 } else if (line_queue_count > TRACE_COUNT) {
390 line_queue = line_queue->next;
394 line->count[sym_counter] = zero ? 0 : line->count[sym_counter] * 7 / 8;
397 pthread_mutex_unlock(&syme->src->lock);
399 printf("%d lines not displayed, maybe increase display entries [e]\n", more);
403 * Symbols will be added here in event__process_sample and will get out
406 static LIST_HEAD(active_symbols);
407 static pthread_mutex_t active_symbols_lock = PTHREAD_MUTEX_INITIALIZER;
410 * Ordering weight: count-1 * count-2 * ... / count-n
412 static double sym_weight(const struct sym_entry *sym)
414 double weight = sym->snap_count;
417 if (!display_weighted)
420 for (counter = 1; counter < evsel_list->nr_entries - 1; counter++)
421 weight *= sym->count[counter];
423 weight /= (sym->count[counter] + 1);
429 static long kernel_samples, us_samples;
430 static long exact_samples;
431 static long guest_us_samples, guest_kernel_samples;
432 static const char CONSOLE_CLEAR[] = "
\e[H
\e[2J";
434 static void __list_insert_active_sym(struct sym_entry *syme)
436 list_add(&syme->node, &active_symbols);
439 static void list_remove_active_sym(struct sym_entry *syme)
441 pthread_mutex_lock(&active_symbols_lock);
442 list_del_init(&syme->node);
443 pthread_mutex_unlock(&active_symbols_lock);
446 static void rb_insert_active_sym(struct rb_root *tree, struct sym_entry *se)
448 struct rb_node **p = &tree->rb_node;
449 struct rb_node *parent = NULL;
450 struct sym_entry *iter;
454 iter = rb_entry(parent, struct sym_entry, rb_node);
456 if (se->weight > iter->weight)
462 rb_link_node(&se->rb_node, parent, p);
463 rb_insert_color(&se->rb_node, tree);
466 static void print_sym_table(void)
469 struct perf_evsel *counter;
470 int snap = !display_weighted ? sym_counter : 0;
471 float samples_per_sec = samples/delay_secs;
472 float ksamples_per_sec = kernel_samples/delay_secs;
473 float us_samples_per_sec = (us_samples)/delay_secs;
474 float guest_kernel_samples_per_sec = (guest_kernel_samples)/delay_secs;
475 float guest_us_samples_per_sec = (guest_us_samples)/delay_secs;
476 float esamples_percent = (100.0*exact_samples)/samples;
477 float sum_ksamples = 0.0;
478 struct sym_entry *syme, *n;
479 struct rb_root tmp = RB_ROOT;
481 int sym_width = 0, dso_width = 0, dso_short_width = 0;
482 const int win_width = winsize.ws_col - 1;
484 samples = us_samples = kernel_samples = exact_samples = 0;
485 guest_kernel_samples = guest_us_samples = 0;
487 /* Sort the active symbols */
488 pthread_mutex_lock(&active_symbols_lock);
489 syme = list_entry(active_symbols.next, struct sym_entry, node);
490 pthread_mutex_unlock(&active_symbols_lock);
492 list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
493 syme->snap_count = syme->count[snap];
494 if (syme->snap_count != 0) {
496 if ((hide_user_symbols &&
497 syme->origin == PERF_RECORD_MISC_USER) ||
498 (hide_kernel_symbols &&
499 syme->origin == PERF_RECORD_MISC_KERNEL)) {
500 list_remove_active_sym(syme);
503 syme->weight = sym_weight(syme);
504 rb_insert_active_sym(&tmp, syme);
505 sum_ksamples += syme->snap_count;
507 for (j = 0; j < evsel_list->nr_entries; j++)
508 syme->count[j] = zero ? 0 : syme->count[j] * 7 / 8;
510 list_remove_active_sym(syme);
515 printf("%-*.*s\n", win_width, win_width, graph_dotted_line);
517 printf(" PerfTop:%8.0f irqs/sec kernel:%4.1f%%"
520 100.0 - (100.0 * ((samples_per_sec - ksamples_per_sec) /
524 printf(" PerfTop:%8.0f irqs/sec kernel:%4.1f%% us:%4.1f%%"
525 " guest kernel:%4.1f%% guest us:%4.1f%%"
528 100.0 - (100.0 * ((samples_per_sec-ksamples_per_sec) /
530 100.0 - (100.0 * ((samples_per_sec-us_samples_per_sec) /
532 100.0 - (100.0 * ((samples_per_sec -
533 guest_kernel_samples_per_sec) /
535 100.0 - (100.0 * ((samples_per_sec -
536 guest_us_samples_per_sec) /
541 if (evsel_list->nr_entries == 1 || !display_weighted) {
542 struct perf_evsel *first;
543 first = list_entry(evsel_list->entries.next, struct perf_evsel, node);
544 printf("%" PRIu64, (uint64_t)first->attr.sample_period);
551 if (!display_weighted)
552 printf("%s", event_name(sym_evsel));
553 else list_for_each_entry(counter, &evsel_list->entries, node) {
557 printf("%s", event_name(counter));
562 if (target_pid != -1)
563 printf(" (target_pid: %d", target_pid);
564 else if (target_tid != -1)
565 printf(" (target_tid: %d", target_tid);
570 printf(", CPU%s: %s)\n", cpus->nr > 1 ? "s" : "", cpu_list);
572 if (target_tid != -1)
575 printf(", %d CPU%s)\n", cpus->nr, cpus->nr > 1 ? "s" : "");
578 printf("%-*.*s\n", win_width, win_width, graph_dotted_line);
580 if (sym_filter_entry) {
581 show_details(sym_filter_entry);
586 * Find the longest symbol name that will be displayed
588 for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
589 syme = rb_entry(nd, struct sym_entry, rb_node);
590 if (++printed > print_entries ||
591 (int)syme->snap_count < count_filter)
594 if (syme->map->dso->long_name_len > dso_width)
595 dso_width = syme->map->dso->long_name_len;
597 if (syme->map->dso->short_name_len > dso_short_width)
598 dso_short_width = syme->map->dso->short_name_len;
600 if (syme->name_len > sym_width)
601 sym_width = syme->name_len;
606 if (sym_width + dso_width > winsize.ws_col - 29) {
607 dso_width = dso_short_width;
608 if (sym_width + dso_width > winsize.ws_col - 29)
609 sym_width = winsize.ws_col - dso_width - 29;
612 if (evsel_list->nr_entries == 1)
613 printf(" samples pcnt");
615 printf(" weight samples pcnt");
619 printf(" %-*.*s DSO\n", sym_width, sym_width, "function");
620 printf(" %s _______ _____",
621 evsel_list->nr_entries == 1 ? " " : "______");
623 printf(" ________________");
624 printf(" %-*.*s", sym_width, sym_width, graph_line);
625 printf(" %-*.*s", dso_width, dso_width, graph_line);
628 for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
632 syme = rb_entry(nd, struct sym_entry, rb_node);
633 sym = sym_entry__symbol(syme);
634 if (++printed > print_entries || (int)syme->snap_count < count_filter)
637 pcnt = 100.0 - (100.0 * ((sum_ksamples - syme->snap_count) /
640 if (evsel_list->nr_entries == 1 || !display_weighted)
641 printf("%20.2f ", syme->weight);
643 printf("%9.1f %10ld ", syme->weight, syme->snap_count);
645 percent_color_fprintf(stdout, "%4.1f%%", pcnt);
647 printf(" %016" PRIx64, sym->start);
648 printf(" %-*.*s", sym_width, sym_width, sym->name);
649 printf(" %-*.*s\n", dso_width, dso_width,
650 dso_width >= syme->map->dso->long_name_len ?
651 syme->map->dso->long_name :
652 syme->map->dso->short_name);
656 static void prompt_integer(int *target, const char *msg)
658 char *buf = malloc(0), *p;
662 fprintf(stdout, "\n%s: ", msg);
663 if (getline(&buf, &dummy, stdin) < 0)
666 p = strchr(buf, '\n');
676 tmp = strtoul(buf, NULL, 10);
682 static void prompt_percent(int *target, const char *msg)
686 prompt_integer(&tmp, msg);
687 if (tmp >= 0 && tmp <= 100)
691 static void prompt_symbol(struct sym_entry **target, const char *msg)
693 char *buf = malloc(0), *p;
694 struct sym_entry *syme = *target, *n, *found = NULL;
697 /* zero counters of active symbol */
699 pthread_mutex_lock(&syme->src->lock);
700 __zero_source_counters(syme);
702 pthread_mutex_unlock(&syme->src->lock);
705 fprintf(stdout, "\n%s: ", msg);
706 if (getline(&buf, &dummy, stdin) < 0)
709 p = strchr(buf, '\n');
713 pthread_mutex_lock(&active_symbols_lock);
714 syme = list_entry(active_symbols.next, struct sym_entry, node);
715 pthread_mutex_unlock(&active_symbols_lock);
717 list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
718 struct symbol *sym = sym_entry__symbol(syme);
720 if (!strcmp(buf, sym->name)) {
727 fprintf(stderr, "Sorry, %s is not active.\n", buf);
737 static void print_mapped_keys(void)
741 if (sym_filter_entry) {
742 struct symbol *sym = sym_entry__symbol(sym_filter_entry);
746 fprintf(stdout, "\nMapped keys:\n");
747 fprintf(stdout, "\t[d] display refresh delay. \t(%d)\n", delay_secs);
748 fprintf(stdout, "\t[e] display entries (lines). \t(%d)\n", print_entries);
750 if (evsel_list->nr_entries > 1)
751 fprintf(stdout, "\t[E] active event counter. \t(%s)\n", event_name(sym_evsel));
753 fprintf(stdout, "\t[f] profile display filter (count). \t(%d)\n", count_filter);
755 fprintf(stdout, "\t[F] annotate display filter (percent). \t(%d%%)\n", sym_pcnt_filter);
756 fprintf(stdout, "\t[s] annotate symbol. \t(%s)\n", name?: "NULL");
757 fprintf(stdout, "\t[S] stop annotation.\n");
759 if (evsel_list->nr_entries > 1)
760 fprintf(stdout, "\t[w] toggle display weighted/count[E]r. \t(%d)\n", display_weighted ? 1 : 0);
763 "\t[K] hide kernel_symbols symbols. \t(%s)\n",
764 hide_kernel_symbols ? "yes" : "no");
766 "\t[U] hide user symbols. \t(%s)\n",
767 hide_user_symbols ? "yes" : "no");
768 fprintf(stdout, "\t[z] toggle sample zeroing. \t(%d)\n", zero ? 1 : 0);
769 fprintf(stdout, "\t[qQ] quit.\n");
772 static int key_mapped(int c)
789 return evsel_list->nr_entries > 1 ? 1 : 0;
797 static void handle_keypress(struct perf_session *session, int c)
799 if (!key_mapped(c)) {
800 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
801 struct termios tc, save;
804 fprintf(stdout, "\nEnter selection, or unmapped key to continue: ");
809 tc.c_lflag &= ~(ICANON | ECHO);
812 tcsetattr(0, TCSANOW, &tc);
814 poll(&stdin_poll, 1, -1);
817 tcsetattr(0, TCSAFLUSH, &save);
824 prompt_integer(&delay_secs, "Enter display delay");
829 prompt_integer(&print_entries, "Enter display entries (lines)");
830 if (print_entries == 0) {
831 sig_winch_handler(SIGWINCH);
832 signal(SIGWINCH, sig_winch_handler);
834 signal(SIGWINCH, SIG_DFL);
837 if (evsel_list->nr_entries > 1) {
838 fprintf(stderr, "\nAvailable events:");
840 list_for_each_entry(sym_evsel, &evsel_list->entries, node)
841 fprintf(stderr, "\n\t%d %s", sym_evsel->idx, event_name(sym_evsel));
843 prompt_integer(&sym_counter, "Enter details event counter");
845 if (sym_counter >= evsel_list->nr_entries) {
846 sym_evsel = list_entry(evsel_list->entries.next, struct perf_evsel, node);
848 fprintf(stderr, "Sorry, no such event, using %s.\n", event_name(sym_evsel));
852 list_for_each_entry(sym_evsel, &evsel_list->entries, node)
853 if (sym_evsel->idx == sym_counter)
855 } else sym_counter = 0;
858 prompt_integer(&count_filter, "Enter display event count filter");
861 prompt_percent(&sym_pcnt_filter, "Enter details display event filter (percent)");
864 hide_kernel_symbols = !hide_kernel_symbols;
868 printf("exiting.\n");
870 perf_session__fprintf_dsos(session, stderr);
873 prompt_symbol(&sym_filter_entry, "Enter details symbol");
876 if (!sym_filter_entry)
879 struct sym_entry *syme = sym_filter_entry;
881 pthread_mutex_lock(&syme->src->lock);
882 sym_filter_entry = NULL;
883 __zero_source_counters(syme);
884 pthread_mutex_unlock(&syme->src->lock);
888 hide_user_symbols = !hide_user_symbols;
891 display_weighted = ~display_weighted;
901 static void *display_thread(void *arg __used)
903 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
904 struct termios tc, save;
906 struct perf_session *session = (struct perf_session *) arg;
910 tc.c_lflag &= ~(ICANON | ECHO);
915 delay_msecs = delay_secs * 1000;
916 tcsetattr(0, TCSANOW, &tc);
922 } while (!poll(&stdin_poll, 1, delay_msecs) == 1);
925 tcsetattr(0, TCSAFLUSH, &save);
927 handle_keypress(session, c);
933 /* Tag samples to be skipped. */
934 static const char *skip_symbols[] = {
940 "mwait_idle_with_hints",
942 "ppc64_runlatch_off",
943 "pseries_dedicated_idle_sleep",
947 static int symbol_filter(struct map *map, struct symbol *sym)
949 struct sym_entry *syme;
950 const char *name = sym->name;
954 * ppc64 uses function descriptors and appends a '.' to the
955 * start of every instruction address. Remove it.
960 if (!strcmp(name, "_text") ||
961 !strcmp(name, "_etext") ||
962 !strcmp(name, "_sinittext") ||
963 !strncmp("init_module", name, 11) ||
964 !strncmp("cleanup_module", name, 14) ||
965 strstr(name, "_text_start") ||
966 strstr(name, "_text_end"))
969 syme = symbol__priv(sym);
973 if (!sym_filter_entry && sym_filter && !strcmp(name, sym_filter)) {
974 /* schedule initial sym_filter_entry setup */
975 sym_filter_entry_sched = syme;
979 for (i = 0; skip_symbols[i]; i++) {
980 if (!strcmp(skip_symbols[i], name)) {
987 syme->name_len = strlen(sym->name);
992 static void event__process_sample(const event_t *self,
993 struct sample_data *sample,
994 struct perf_session *session)
996 u64 ip = self->ip.ip;
997 struct sym_entry *syme;
998 struct addr_location al;
999 struct machine *machine;
1000 u8 origin = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1005 case PERF_RECORD_MISC_USER:
1007 if (hide_user_symbols)
1009 machine = perf_session__find_host_machine(session);
1011 case PERF_RECORD_MISC_KERNEL:
1013 if (hide_kernel_symbols)
1015 machine = perf_session__find_host_machine(session);
1017 case PERF_RECORD_MISC_GUEST_KERNEL:
1018 ++guest_kernel_samples;
1019 machine = perf_session__find_machine(session, self->ip.pid);
1021 case PERF_RECORD_MISC_GUEST_USER:
1024 * TODO: we don't process guest user from host side
1025 * except simple counting.
1032 if (!machine && perf_guest) {
1033 pr_err("Can't find guest [%d]'s kernel information\n",
1038 if (self->header.misc & PERF_RECORD_MISC_EXACT_IP)
1041 if (event__preprocess_sample(self, session, &al, sample,
1042 symbol_filter) < 0 ||
1046 if (al.sym == NULL) {
1048 * As we do lazy loading of symtabs we only will know if the
1049 * specified vmlinux file is invalid when we actually have a
1050 * hit in kernel space and then try to load it. So if we get
1051 * here and there are _no_ symbols in the DSO backing the
1052 * kernel map, bail out.
1054 * We may never get here, for instance, if we use -K/
1055 * --hide-kernel-symbols, even if the user specifies an
1056 * invalid --vmlinux ;-)
1058 if (al.map == machine->vmlinux_maps[MAP__FUNCTION] &&
1059 RB_EMPTY_ROOT(&al.map->dso->symbols[MAP__FUNCTION])) {
1060 pr_err("The %s file can't be used\n",
1061 symbol_conf.vmlinux_name);
1068 /* let's see, whether we need to install initial sym_filter_entry */
1069 if (sym_filter_entry_sched) {
1070 sym_filter_entry = sym_filter_entry_sched;
1071 sym_filter_entry_sched = NULL;
1072 if (parse_source(sym_filter_entry) < 0) {
1073 struct symbol *sym = sym_entry__symbol(sym_filter_entry);
1075 pr_err("Can't annotate %s", sym->name);
1076 if (sym_filter_entry->map->dso->origin == DSO__ORIG_KERNEL) {
1077 pr_err(": No vmlinux file was found in the path:\n");
1078 machine__fprintf_vmlinux_path(machine, stderr);
1085 syme = symbol__priv(al.sym);
1087 struct perf_evsel *evsel;
1089 syme->origin = origin;
1090 evsel = perf_evlist__id2evsel(evsel_list, sample->id);
1091 assert(evsel != NULL);
1092 syme->count[evsel->idx]++;
1093 record_precise_ip(syme, evsel->idx, ip);
1094 pthread_mutex_lock(&active_symbols_lock);
1095 if (list_empty(&syme->node) || !syme->node.next)
1096 __list_insert_active_sym(syme);
1097 pthread_mutex_unlock(&active_symbols_lock);
1101 static void perf_session__mmap_read_cpu(struct perf_session *self, int cpu)
1103 struct perf_mmap *md = &evsel_list->mmap[cpu];
1104 unsigned int head = perf_mmap__read_head(md);
1105 unsigned int old = md->prev;
1106 unsigned char *data = md->base + page_size;
1107 struct sample_data sample;
1111 * If we're further behind than half the buffer, there's a chance
1112 * the writer will bite our tail and mess up the samples under us.
1114 * If we somehow ended up ahead of the head, we got messed up.
1116 * In either case, truncate and restart at head.
1119 if (diff > md->mask / 2 || diff < 0) {
1120 fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
1123 * head points to a known good entry, start there.
1128 for (; old != head;) {
1129 event_t *event = (event_t *)&data[old & md->mask];
1133 size_t size = event->header.size;
1136 * Event straddles the mmap boundary -- header should always
1137 * be inside due to u64 alignment of output.
1139 if ((old & md->mask) + size != ((old + size) & md->mask)) {
1140 unsigned int offset = old;
1141 unsigned int len = min(sizeof(*event), size), cpy;
1142 void *dst = &event_copy;
1145 cpy = min(md->mask + 1 - (offset & md->mask), len);
1146 memcpy(dst, &data[offset & md->mask], cpy);
1152 event = &event_copy;
1155 event__parse_sample(event, self, &sample);
1156 if (event->header.type == PERF_RECORD_SAMPLE)
1157 event__process_sample(event, &sample, self);
1159 event__process(event, &sample, self);
1166 static void perf_session__mmap_read(struct perf_session *self)
1170 for (i = 0; i < cpus->nr; i++)
1171 perf_session__mmap_read_cpu(self, i);
1174 static void start_counters(struct perf_evlist *evlist)
1176 struct perf_evsel *counter;
1178 list_for_each_entry(counter, &evlist->entries, node) {
1179 struct perf_event_attr *attr = &counter->attr;
1181 attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
1184 attr->sample_type |= PERF_SAMPLE_PERIOD;
1186 attr->sample_freq = freq;
1189 if (evlist->nr_entries > 1) {
1190 attr->sample_type |= PERF_SAMPLE_ID;
1191 attr->read_format |= PERF_FORMAT_ID;
1196 if (perf_evsel__open(counter, cpus, threads, group, inherit) < 0) {
1199 if (err == EPERM || err == EACCES)
1200 die("Permission error - are you root?\n"
1201 "\t Consider tweaking"
1202 " /proc/sys/kernel/perf_event_paranoid.\n");
1204 * If it's cycles then fall back to hrtimer
1205 * based cpu-clock-tick sw counter, which
1206 * is always available even if no PMU support:
1208 if (attr->type == PERF_TYPE_HARDWARE &&
1209 attr->config == PERF_COUNT_HW_CPU_CYCLES) {
1212 warning(" ... trying to fall back to cpu-clock-ticks\n");
1214 attr->type = PERF_TYPE_SOFTWARE;
1215 attr->config = PERF_COUNT_SW_CPU_CLOCK;
1219 error("sys_perf_event_open() syscall returned with %d "
1220 "(%s). /bin/dmesg may provide additional information.\n",
1221 err, strerror(err));
1222 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
1227 if (perf_evlist__mmap(evlist, cpus, threads, mmap_pages, true) < 0)
1228 die("failed to mmap with %d (%s)\n", errno, strerror(errno));
1231 static int __cmd_top(void)
1234 struct perf_evsel *first;
1237 * FIXME: perf_session__new should allow passing a O_MMAP, so that all this
1238 * mmap reading, etc is encapsulated in it. Use O_WRONLY for now.
1240 struct perf_session *session = perf_session__new(NULL, O_WRONLY, false, false, NULL);
1241 if (session == NULL)
1244 if (target_tid != -1)
1245 event__synthesize_thread(target_tid, event__process, session);
1247 event__synthesize_threads(event__process, session);
1249 start_counters(evsel_list);
1250 first = list_entry(evsel_list->entries.next, struct perf_evsel, node);
1251 perf_session__set_sample_type(session, first->attr.sample_type);
1253 /* Wait for a minimal set of events before starting the snapshot */
1254 poll(evsel_list->pollfd, evsel_list->nr_fds, 100);
1256 perf_session__mmap_read(session);
1258 if (pthread_create(&thread, NULL, display_thread, session)) {
1259 printf("Could not create display thread.\n");
1263 if (realtime_prio) {
1264 struct sched_param param;
1266 param.sched_priority = realtime_prio;
1267 if (sched_setscheduler(0, SCHED_FIFO, ¶m)) {
1268 printf("Could not set realtime priority.\n");
1276 perf_session__mmap_read(session);
1278 if (hits == samples)
1279 ret = poll(evsel_list->pollfd, evsel_list->nr_fds, 100);
1285 static const char * const top_usage[] = {
1286 "perf top [<options>]",
1290 static const struct option options[] = {
1291 OPT_CALLBACK('e', "event", &evsel_list, "event",
1292 "event selector. use 'perf list' to list available events",
1294 OPT_INTEGER('c', "count", &default_interval,
1295 "event period to sample"),
1296 OPT_INTEGER('p', "pid", &target_pid,
1297 "profile events on existing process id"),
1298 OPT_INTEGER('t', "tid", &target_tid,
1299 "profile events on existing thread id"),
1300 OPT_BOOLEAN('a', "all-cpus", &system_wide,
1301 "system-wide collection from all CPUs"),
1302 OPT_STRING('C', "cpu", &cpu_list, "cpu",
1303 "list of cpus to monitor"),
1304 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1305 "file", "vmlinux pathname"),
1306 OPT_BOOLEAN('K', "hide_kernel_symbols", &hide_kernel_symbols,
1307 "hide kernel symbols"),
1308 OPT_UINTEGER('m', "mmap-pages", &mmap_pages, "number of mmap data pages"),
1309 OPT_INTEGER('r', "realtime", &realtime_prio,
1310 "collect data with this RT SCHED_FIFO priority"),
1311 OPT_INTEGER('d', "delay", &delay_secs,
1312 "number of seconds to delay between refreshes"),
1313 OPT_BOOLEAN('D', "dump-symtab", &dump_symtab,
1314 "dump the symbol table used for profiling"),
1315 OPT_INTEGER('f', "count-filter", &count_filter,
1316 "only display functions with more events than this"),
1317 OPT_BOOLEAN('g', "group", &group,
1318 "put the counters into a counter group"),
1319 OPT_BOOLEAN('i', "inherit", &inherit,
1320 "child tasks inherit counters"),
1321 OPT_STRING('s', "sym-annotate", &sym_filter, "symbol name",
1322 "symbol to annotate"),
1323 OPT_BOOLEAN('z', "zero", &zero,
1324 "zero history across updates"),
1325 OPT_INTEGER('F', "freq", &freq,
1326 "profile at this frequency"),
1327 OPT_INTEGER('E', "entries", &print_entries,
1328 "display this many functions"),
1329 OPT_BOOLEAN('U', "hide_user_symbols", &hide_user_symbols,
1330 "hide user symbols"),
1331 OPT_INCR('v', "verbose", &verbose,
1332 "be more verbose (show counter open errors, etc)"),
1336 int cmd_top(int argc, const char **argv, const char *prefix __used)
1338 struct perf_evsel *pos;
1339 int status = -ENOMEM;
1341 evsel_list = perf_evlist__new();
1342 if (evsel_list == NULL)
1345 page_size = sysconf(_SC_PAGE_SIZE);
1347 argc = parse_options(argc, argv, options, top_usage, 0);
1349 usage_with_options(top_usage, options);
1351 if (target_pid != -1)
1352 target_tid = target_pid;
1354 threads = thread_map__new(target_pid, target_tid);
1355 if (threads == NULL) {
1356 pr_err("Problems finding threads of monitor\n");
1357 usage_with_options(top_usage, options);
1360 /* CPU and PID are mutually exclusive */
1361 if (target_tid > 0 && cpu_list) {
1362 printf("WARNING: PID switch overriding CPU\n");
1367 if (!evsel_list->nr_entries &&
1368 perf_evlist__add_default(evsel_list) < 0) {
1369 pr_err("Not enough memory for event selector list\n");
1377 * User specified count overrides default frequency.
1379 if (default_interval)
1382 default_interval = freq;
1384 fprintf(stderr, "frequency and count are zero, aborting\n");
1388 if (target_tid != -1)
1389 cpus = cpu_map__dummy_new();
1391 cpus = cpu_map__new(cpu_list);
1394 usage_with_options(top_usage, options);
1396 list_for_each_entry(pos, &evsel_list->entries, node) {
1397 if (perf_evsel__alloc_fd(pos, cpus->nr, threads->nr) < 0)
1400 * Fill in the ones not specifically initialized via -c:
1402 if (pos->attr.sample_period)
1405 pos->attr.sample_period = default_interval;
1408 if (perf_evlist__alloc_pollfd(evsel_list, cpus->nr, threads->nr) < 0 ||
1409 perf_evlist__alloc_mmap(evsel_list, cpus->nr) < 0)
1412 sym_evsel = list_entry(evsel_list->entries.next, struct perf_evsel, node);
1414 symbol_conf.priv_size = (sizeof(struct sym_entry) +
1415 (evsel_list->nr_entries + 1) * sizeof(unsigned long));
1417 symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL);
1418 if (symbol__init() < 0)
1421 get_term_dimensions(&winsize);
1422 if (print_entries == 0) {
1423 update_print_entries(&winsize);
1424 signal(SIGWINCH, sig_winch_handler);
1427 status = __cmd_top();
1429 perf_evlist__delete(evsel_list);