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 bool system_wide
= false;
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 bool inherit
= false;
69 static int profile_cpu
= -1;
70 static int nr_cpus
= 0;
71 static unsigned int realtime_prio
= 0;
72 static bool group
= false;
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;
78 static bool zero
= false;
79 static bool dump_symtab
= false;
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 int parse_source(struct sym_entry
*syme
)
175 struct sym_entry_source
*source
;
178 char command
[PATH_MAX
*2];
185 sym
= sym_entry__symbol(syme
);
189 * We can't annotate with just /proc/kallsyms
191 if (map
->dso
->origin
== DSO__ORIG_KERNEL
)
194 if (syme
->src
== NULL
) {
195 syme
->src
= zalloc(sizeof(*source
));
196 if (syme
->src
== NULL
)
198 pthread_mutex_init(&syme
->src
->lock
, NULL
);
204 pthread_mutex_lock(&source
->lock
);
207 path
= map
->dso
->long_name
;
209 len
= sym
->end
- sym
->start
;
212 "objdump --start-address=%#0*Lx --stop-address=%#0*Lx -dS %s",
213 BITS_PER_LONG
/ 4, map__rip_2objdump(map
, sym
->start
),
214 BITS_PER_LONG
/ 4, map__rip_2objdump(map
, sym
->end
), path
);
216 file
= popen(command
, "r");
220 pthread_mutex_lock(&source
->lock
);
221 source
->lines_tail
= &source
->lines
;
222 while (!feof(file
)) {
223 struct source_line
*src
;
227 src
= malloc(sizeof(struct source_line
));
229 memset(src
, 0, sizeof(struct source_line
));
231 if (getline(&src
->line
, &dummy
, file
) < 0)
236 c
= strchr(src
->line
, '\n');
241 *source
->lines_tail
= src
;
242 source
->lines_tail
= &src
->next
;
244 src
->eip
= strtoull(src
->line
, &sep
, 16);
246 src
->eip
= map__objdump_2ip(map
, src
->eip
);
247 else /* this line has no ip info (e.g. source line) */
252 sym_filter_entry
= syme
;
253 pthread_mutex_unlock(&source
->lock
);
257 static void __zero_source_counters(struct sym_entry
*syme
)
260 struct source_line
*line
;
262 line
= syme
->src
->lines
;
264 for (i
= 0; i
< nr_counters
; i
++)
270 static void record_precise_ip(struct sym_entry
*syme
, int counter
, u64 ip
)
272 struct source_line
*line
;
274 if (syme
!= sym_filter_entry
)
277 if (pthread_mutex_trylock(&syme
->src
->lock
))
280 if (syme
->src
== NULL
|| syme
->src
->source
== NULL
)
283 for (line
= syme
->src
->lines
; line
; line
= line
->next
) {
284 /* skip lines without IP info */
287 if (line
->eip
== ip
) {
288 line
->count
[counter
]++;
295 pthread_mutex_unlock(&syme
->src
->lock
);
298 #define PATTERN_LEN (BITS_PER_LONG / 4 + 2)
300 static void lookup_sym_source(struct sym_entry
*syme
)
302 struct symbol
*symbol
= sym_entry__symbol(syme
);
303 struct source_line
*line
;
304 char pattern
[PATTERN_LEN
+ 1];
306 sprintf(pattern
, "%0*Lx <", BITS_PER_LONG
/ 4,
307 map__rip_2objdump(syme
->map
, symbol
->start
));
309 pthread_mutex_lock(&syme
->src
->lock
);
310 for (line
= syme
->src
->lines
; line
; line
= line
->next
) {
311 if (memcmp(line
->line
, pattern
, PATTERN_LEN
) == 0) {
312 syme
->src
->source
= line
;
316 pthread_mutex_unlock(&syme
->src
->lock
);
319 static void show_lines(struct source_line
*queue
, int count
, int total
)
322 struct source_line
*line
;
325 for (i
= 0; i
< count
; i
++) {
326 float pcnt
= 100.0*(float)line
->count
[sym_counter
]/(float)total
;
328 printf("%8li %4.1f%%\t%s\n", line
->count
[sym_counter
], pcnt
, line
->line
);
333 #define TRACE_COUNT 3
335 static void show_details(struct sym_entry
*syme
)
337 struct symbol
*symbol
;
338 struct source_line
*line
;
339 struct source_line
*line_queue
= NULL
;
341 int line_queue_count
= 0, total
= 0, more
= 0;
346 if (!syme
->src
->source
)
347 lookup_sym_source(syme
);
349 if (!syme
->src
->source
)
352 symbol
= sym_entry__symbol(syme
);
353 printf("Showing %s for %s\n", event_name(sym_counter
), symbol
->name
);
354 printf(" Events Pcnt (>=%d%%)\n", sym_pcnt_filter
);
356 pthread_mutex_lock(&syme
->src
->lock
);
357 line
= syme
->src
->source
;
359 total
+= line
->count
[sym_counter
];
363 line
= syme
->src
->source
;
367 if (!line_queue_count
)
371 if (line
->count
[sym_counter
])
372 pcnt
= 100.0 * line
->count
[sym_counter
] / (float)total
;
373 if (pcnt
>= (float)sym_pcnt_filter
) {
374 if (displayed
<= print_entries
)
375 show_lines(line_queue
, line_queue_count
, total
);
377 displayed
+= line_queue_count
;
378 line_queue_count
= 0;
380 } else if (line_queue_count
> TRACE_COUNT
) {
381 line_queue
= line_queue
->next
;
385 line
->count
[sym_counter
] = zero
? 0 : line
->count
[sym_counter
] * 7 / 8;
388 pthread_mutex_unlock(&syme
->src
->lock
);
390 printf("%d lines not displayed, maybe increase display entries [e]\n", more
);
394 * Symbols will be added here in event__process_sample and will get out
397 static LIST_HEAD(active_symbols
);
398 static pthread_mutex_t active_symbols_lock
= PTHREAD_MUTEX_INITIALIZER
;
401 * Ordering weight: count-1 * count-2 * ... / count-n
403 static double sym_weight(const struct sym_entry
*sym
)
405 double weight
= sym
->snap_count
;
408 if (!display_weighted
)
411 for (counter
= 1; counter
< nr_counters
-1; counter
++)
412 weight
*= sym
->count
[counter
];
414 weight
/= (sym
->count
[counter
] + 1);
420 static long userspace_samples
;
421 static const char CONSOLE_CLEAR
[] = "\e[H\e[2J";
423 static void __list_insert_active_sym(struct sym_entry
*syme
)
425 list_add(&syme
->node
, &active_symbols
);
428 static void list_remove_active_sym(struct sym_entry
*syme
)
430 pthread_mutex_lock(&active_symbols_lock
);
431 list_del_init(&syme
->node
);
432 pthread_mutex_unlock(&active_symbols_lock
);
435 static void rb_insert_active_sym(struct rb_root
*tree
, struct sym_entry
*se
)
437 struct rb_node
**p
= &tree
->rb_node
;
438 struct rb_node
*parent
= NULL
;
439 struct sym_entry
*iter
;
443 iter
= rb_entry(parent
, struct sym_entry
, rb_node
);
445 if (se
->weight
> iter
->weight
)
451 rb_link_node(&se
->rb_node
, parent
, p
);
452 rb_insert_color(&se
->rb_node
, tree
);
455 static void print_sym_table(void)
458 int counter
, snap
= !display_weighted
? sym_counter
: 0;
459 float samples_per_sec
= samples
/delay_secs
;
460 float ksamples_per_sec
= (samples
-userspace_samples
)/delay_secs
;
461 float sum_ksamples
= 0.0;
462 struct sym_entry
*syme
, *n
;
463 struct rb_root tmp
= RB_ROOT
;
465 int sym_width
= 0, dso_width
= 0, dso_short_width
= 0;
466 const int win_width
= winsize
.ws_col
- 1;
468 samples
= userspace_samples
= 0;
470 /* Sort the active symbols */
471 pthread_mutex_lock(&active_symbols_lock
);
472 syme
= list_entry(active_symbols
.next
, struct sym_entry
, node
);
473 pthread_mutex_unlock(&active_symbols_lock
);
475 list_for_each_entry_safe_from(syme
, n
, &active_symbols
, node
) {
476 syme
->snap_count
= syme
->count
[snap
];
477 if (syme
->snap_count
!= 0) {
479 if ((hide_user_symbols
&&
480 syme
->origin
== PERF_RECORD_MISC_USER
) ||
481 (hide_kernel_symbols
&&
482 syme
->origin
== PERF_RECORD_MISC_KERNEL
)) {
483 list_remove_active_sym(syme
);
486 syme
->weight
= sym_weight(syme
);
487 rb_insert_active_sym(&tmp
, syme
);
488 sum_ksamples
+= syme
->snap_count
;
490 for (j
= 0; j
< nr_counters
; j
++)
491 syme
->count
[j
] = zero
? 0 : syme
->count
[j
] * 7 / 8;
493 list_remove_active_sym(syme
);
498 printf("%-*.*s\n", win_width
, win_width
, graph_dotted_line
);
499 printf( " PerfTop:%8.0f irqs/sec kernel:%4.1f%% [",
501 100.0 - (100.0*((samples_per_sec
-ksamples_per_sec
)/samples_per_sec
)));
503 if (nr_counters
== 1 || !display_weighted
) {
504 printf("%Ld", (u64
)attrs
[0].sample_period
);
511 if (!display_weighted
)
512 printf("%s", event_name(sym_counter
));
513 else for (counter
= 0; counter
< nr_counters
; counter
++) {
517 printf("%s", event_name(counter
));
522 if (target_pid
!= -1)
523 printf(" (target_pid: %d", target_pid
);
527 if (profile_cpu
!= -1)
528 printf(", cpu: %d)\n", profile_cpu
);
530 if (target_pid
!= -1)
533 printf(", %d CPUs)\n", nr_cpus
);
536 printf("%-*.*s\n", win_width
, win_width
, graph_dotted_line
);
538 if (sym_filter_entry
) {
539 show_details(sym_filter_entry
);
544 * Find the longest symbol name that will be displayed
546 for (nd
= rb_first(&tmp
); nd
; nd
= rb_next(nd
)) {
547 syme
= rb_entry(nd
, struct sym_entry
, rb_node
);
548 if (++printed
> print_entries
||
549 (int)syme
->snap_count
< count_filter
)
552 if (syme
->map
->dso
->long_name_len
> dso_width
)
553 dso_width
= syme
->map
->dso
->long_name_len
;
555 if (syme
->map
->dso
->short_name_len
> dso_short_width
)
556 dso_short_width
= syme
->map
->dso
->short_name_len
;
558 if (syme
->name_len
> sym_width
)
559 sym_width
= syme
->name_len
;
564 if (sym_width
+ dso_width
> winsize
.ws_col
- 29) {
565 dso_width
= dso_short_width
;
566 if (sym_width
+ dso_width
> winsize
.ws_col
- 29)
567 sym_width
= winsize
.ws_col
- dso_width
- 29;
570 if (nr_counters
== 1)
571 printf(" samples pcnt");
573 printf(" weight samples pcnt");
577 printf(" %-*.*s DSO\n", sym_width
, sym_width
, "function");
578 printf(" %s _______ _____",
579 nr_counters
== 1 ? " " : "______");
581 printf(" ________________");
582 printf(" %-*.*s", sym_width
, sym_width
, graph_line
);
583 printf(" %-*.*s", dso_width
, dso_width
, graph_line
);
586 for (nd
= rb_first(&tmp
); nd
; nd
= rb_next(nd
)) {
590 syme
= rb_entry(nd
, struct sym_entry
, rb_node
);
591 sym
= sym_entry__symbol(syme
);
593 if (++printed
> print_entries
|| (int)syme
->snap_count
< count_filter
)
596 pcnt
= 100.0 - (100.0 * ((sum_ksamples
- syme
->snap_count
) /
599 if (nr_counters
== 1 || !display_weighted
)
600 printf("%20.2f ", syme
->weight
);
602 printf("%9.1f %10ld ", syme
->weight
, syme
->snap_count
);
604 percent_color_fprintf(stdout
, "%4.1f%%", pcnt
);
606 printf(" %016llx", sym
->start
);
607 printf(" %-*.*s", sym_width
, sym_width
, sym
->name
);
608 printf(" %-*.*s\n", dso_width
, dso_width
,
609 dso_width
>= syme
->map
->dso
->long_name_len
?
610 syme
->map
->dso
->long_name
:
611 syme
->map
->dso
->short_name
);
615 static void prompt_integer(int *target
, const char *msg
)
617 char *buf
= malloc(0), *p
;
621 fprintf(stdout
, "\n%s: ", msg
);
622 if (getline(&buf
, &dummy
, stdin
) < 0)
625 p
= strchr(buf
, '\n');
635 tmp
= strtoul(buf
, NULL
, 10);
641 static void prompt_percent(int *target
, const char *msg
)
645 prompt_integer(&tmp
, msg
);
646 if (tmp
>= 0 && tmp
<= 100)
650 static void prompt_symbol(struct sym_entry
**target
, const char *msg
)
652 char *buf
= malloc(0), *p
;
653 struct sym_entry
*syme
= *target
, *n
, *found
= NULL
;
656 /* zero counters of active symbol */
658 pthread_mutex_lock(&syme
->src
->lock
);
659 __zero_source_counters(syme
);
661 pthread_mutex_unlock(&syme
->src
->lock
);
664 fprintf(stdout
, "\n%s: ", msg
);
665 if (getline(&buf
, &dummy
, stdin
) < 0)
668 p
= strchr(buf
, '\n');
672 pthread_mutex_lock(&active_symbols_lock
);
673 syme
= list_entry(active_symbols
.next
, struct sym_entry
, node
);
674 pthread_mutex_unlock(&active_symbols_lock
);
676 list_for_each_entry_safe_from(syme
, n
, &active_symbols
, node
) {
677 struct symbol
*sym
= sym_entry__symbol(syme
);
679 if (!strcmp(buf
, sym
->name
)) {
686 fprintf(stderr
, "Sorry, %s is not active.\n", buf
);
696 static void print_mapped_keys(void)
700 if (sym_filter_entry
) {
701 struct symbol
*sym
= sym_entry__symbol(sym_filter_entry
);
705 fprintf(stdout
, "\nMapped keys:\n");
706 fprintf(stdout
, "\t[d] display refresh delay. \t(%d)\n", delay_secs
);
707 fprintf(stdout
, "\t[e] display entries (lines). \t(%d)\n", print_entries
);
710 fprintf(stdout
, "\t[E] active event counter. \t(%s)\n", event_name(sym_counter
));
712 fprintf(stdout
, "\t[f] profile display filter (count). \t(%d)\n", count_filter
);
714 fprintf(stdout
, "\t[F] annotate display filter (percent). \t(%d%%)\n", sym_pcnt_filter
);
715 fprintf(stdout
, "\t[s] annotate symbol. \t(%s)\n", name
?: "NULL");
716 fprintf(stdout
, "\t[S] stop annotation.\n");
719 fprintf(stdout
, "\t[w] toggle display weighted/count[E]r. \t(%d)\n", display_weighted
? 1 : 0);
722 "\t[K] hide kernel_symbols symbols. \t(%s)\n",
723 hide_kernel_symbols
? "yes" : "no");
725 "\t[U] hide user symbols. \t(%s)\n",
726 hide_user_symbols
? "yes" : "no");
727 fprintf(stdout
, "\t[z] toggle sample zeroing. \t(%d)\n", zero
? 1 : 0);
728 fprintf(stdout
, "\t[qQ] quit.\n");
731 static int key_mapped(int c
)
748 return nr_counters
> 1 ? 1 : 0;
756 static void handle_keypress(int c
)
758 if (!key_mapped(c
)) {
759 struct pollfd stdin_poll
= { .fd
= 0, .events
= POLLIN
};
760 struct termios tc
, save
;
763 fprintf(stdout
, "\nEnter selection, or unmapped key to continue: ");
768 tc
.c_lflag
&= ~(ICANON
| ECHO
);
771 tcsetattr(0, TCSANOW
, &tc
);
773 poll(&stdin_poll
, 1, -1);
776 tcsetattr(0, TCSAFLUSH
, &save
);
783 prompt_integer(&delay_secs
, "Enter display delay");
788 prompt_integer(&print_entries
, "Enter display entries (lines)");
789 if (print_entries
== 0) {
790 sig_winch_handler(SIGWINCH
);
791 signal(SIGWINCH
, sig_winch_handler
);
793 signal(SIGWINCH
, SIG_DFL
);
796 if (nr_counters
> 1) {
799 fprintf(stderr
, "\nAvailable events:");
800 for (i
= 0; i
< nr_counters
; i
++)
801 fprintf(stderr
, "\n\t%d %s", i
, event_name(i
));
803 prompt_integer(&sym_counter
, "Enter details event counter");
805 if (sym_counter
>= nr_counters
) {
806 fprintf(stderr
, "Sorry, no such event, using %s.\n", event_name(0));
810 } else sym_counter
= 0;
813 prompt_integer(&count_filter
, "Enter display event count filter");
816 prompt_percent(&sym_pcnt_filter
, "Enter details display event filter (percent)");
819 hide_kernel_symbols
= !hide_kernel_symbols
;
823 printf("exiting.\n");
825 dsos__fprintf(stderr
);
828 prompt_symbol(&sym_filter_entry
, "Enter details symbol");
831 if (!sym_filter_entry
)
834 struct sym_entry
*syme
= sym_filter_entry
;
836 pthread_mutex_lock(&syme
->src
->lock
);
837 sym_filter_entry
= NULL
;
838 __zero_source_counters(syme
);
839 pthread_mutex_unlock(&syme
->src
->lock
);
843 hide_user_symbols
= !hide_user_symbols
;
846 display_weighted
= ~display_weighted
;
856 static void *display_thread(void *arg __used
)
858 struct pollfd stdin_poll
= { .fd
= 0, .events
= POLLIN
};
859 struct termios tc
, save
;
864 tc
.c_lflag
&= ~(ICANON
| ECHO
);
869 delay_msecs
= delay_secs
* 1000;
870 tcsetattr(0, TCSANOW
, &tc
);
876 } while (!poll(&stdin_poll
, 1, delay_msecs
) == 1);
879 tcsetattr(0, TCSAFLUSH
, &save
);
887 /* Tag samples to be skipped. */
888 static const char *skip_symbols
[] = {
894 "mwait_idle_with_hints",
896 "ppc64_runlatch_off",
897 "pseries_dedicated_idle_sleep",
901 static int symbol_filter(struct map
*map
, struct symbol
*sym
)
903 struct sym_entry
*syme
;
904 const char *name
= sym
->name
;
908 * ppc64 uses function descriptors and appends a '.' to the
909 * start of every instruction address. Remove it.
914 if (!strcmp(name
, "_text") ||
915 !strcmp(name
, "_etext") ||
916 !strcmp(name
, "_sinittext") ||
917 !strncmp("init_module", name
, 11) ||
918 !strncmp("cleanup_module", name
, 14) ||
919 strstr(name
, "_text_start") ||
920 strstr(name
, "_text_end"))
923 syme
= symbol__priv(sym
);
927 if (!sym_filter_entry
&& sym_filter
&& !strcmp(name
, sym_filter
)) {
928 /* schedule initial sym_filter_entry setup */
929 sym_filter_entry_sched
= syme
;
933 for (i
= 0; skip_symbols
[i
]; i
++) {
934 if (!strcmp(skip_symbols
[i
], name
)) {
941 syme
->name_len
= strlen(sym
->name
);
946 static void event__process_sample(const event_t
*self
,
947 struct perf_session
*session
, int counter
)
949 u64 ip
= self
->ip
.ip
;
950 struct sym_entry
*syme
;
951 struct addr_location al
;
952 u8 origin
= self
->header
.misc
& PERF_RECORD_MISC_CPUMODE_MASK
;
957 case PERF_RECORD_MISC_USER
:
959 if (hide_user_symbols
)
962 case PERF_RECORD_MISC_KERNEL
:
963 if (hide_kernel_symbols
)
970 if (event__preprocess_sample(self
, session
, &al
, symbol_filter
) < 0 ||
974 if (al
.sym
== NULL
) {
976 * As we do lazy loading of symtabs we only will know if the
977 * specified vmlinux file is invalid when we actually have a
978 * hit in kernel space and then try to load it. So if we get
979 * here and there are _no_ symbols in the DSO backing the
980 * kernel map, bail out.
982 * We may never get here, for instance, if we use -K/
983 * --hide-kernel-symbols, even if the user specifies an
984 * invalid --vmlinux ;-)
986 if (al
.map
== session
->vmlinux_maps
[MAP__FUNCTION
] &&
987 RB_EMPTY_ROOT(&al
.map
->dso
->symbols
[MAP__FUNCTION
])) {
988 pr_err("The %s file can't be used\n",
989 symbol_conf
.vmlinux_name
);
996 /* let's see, whether we need to install initial sym_filter_entry */
997 if (sym_filter_entry_sched
) {
998 sym_filter_entry
= sym_filter_entry_sched
;
999 sym_filter_entry_sched
= NULL
;
1000 if (parse_source(sym_filter_entry
) < 0) {
1001 struct symbol
*sym
= sym_entry__symbol(sym_filter_entry
);
1003 pr_err("Can't annotate %s", sym
->name
);
1004 if (sym_filter_entry
->map
->dso
->origin
== DSO__ORIG_KERNEL
) {
1005 pr_err(": No vmlinux file was found in the path:\n");
1006 vmlinux_path__fprintf(stderr
);
1013 syme
= symbol__priv(al
.sym
);
1015 syme
->count
[counter
]++;
1016 syme
->origin
= origin
;
1017 record_precise_ip(syme
, counter
, ip
);
1018 pthread_mutex_lock(&active_symbols_lock
);
1019 if (list_empty(&syme
->node
) || !syme
->node
.next
)
1020 __list_insert_active_sym(syme
);
1021 pthread_mutex_unlock(&active_symbols_lock
);
1025 static int event__process(event_t
*event
, struct perf_session
*session
)
1027 switch (event
->header
.type
) {
1028 case PERF_RECORD_COMM
:
1029 event__process_comm(event
, session
);
1031 case PERF_RECORD_MMAP
:
1032 event__process_mmap(event
, session
);
1034 case PERF_RECORD_FORK
:
1035 case PERF_RECORD_EXIT
:
1036 event__process_task(event
, session
);
1052 static unsigned int mmap_read_head(struct mmap_data
*md
)
1054 struct perf_event_mmap_page
*pc
= md
->base
;
1057 head
= pc
->data_head
;
1063 static void perf_session__mmap_read_counter(struct perf_session
*self
,
1064 struct mmap_data
*md
)
1066 unsigned int head
= mmap_read_head(md
);
1067 unsigned int old
= md
->prev
;
1068 unsigned char *data
= md
->base
+ page_size
;
1072 * If we're further behind than half the buffer, there's a chance
1073 * the writer will bite our tail and mess up the samples under us.
1075 * If we somehow ended up ahead of the head, we got messed up.
1077 * In either case, truncate and restart at head.
1080 if (diff
> md
->mask
/ 2 || diff
< 0) {
1081 fprintf(stderr
, "WARNING: failed to keep up with mmap data.\n");
1084 * head points to a known good entry, start there.
1089 for (; old
!= head
;) {
1090 event_t
*event
= (event_t
*)&data
[old
& md
->mask
];
1094 size_t size
= event
->header
.size
;
1097 * Event straddles the mmap boundary -- header should always
1098 * be inside due to u64 alignment of output.
1100 if ((old
& md
->mask
) + size
!= ((old
+ size
) & md
->mask
)) {
1101 unsigned int offset
= old
;
1102 unsigned int len
= min(sizeof(*event
), size
), cpy
;
1103 void *dst
= &event_copy
;
1106 cpy
= min(md
->mask
+ 1 - (offset
& md
->mask
), len
);
1107 memcpy(dst
, &data
[offset
& md
->mask
], cpy
);
1113 event
= &event_copy
;
1116 if (event
->header
.type
== PERF_RECORD_SAMPLE
)
1117 event__process_sample(event
, self
, md
->counter
);
1119 event__process(event
, self
);
1126 static struct pollfd event_array
[MAX_NR_CPUS
* MAX_COUNTERS
];
1127 static struct mmap_data mmap_array
[MAX_NR_CPUS
][MAX_COUNTERS
];
1129 static void perf_session__mmap_read(struct perf_session
*self
)
1133 for (i
= 0; i
< nr_cpus
; i
++) {
1134 for (counter
= 0; counter
< nr_counters
; counter
++)
1135 perf_session__mmap_read_counter(self
, &mmap_array
[i
][counter
]);
1142 static void start_counter(int i
, int counter
)
1144 struct perf_event_attr
*attr
;
1148 if (target_pid
== -1 && profile_cpu
== -1)
1151 attr
= attrs
+ counter
;
1153 attr
->sample_type
= PERF_SAMPLE_IP
| PERF_SAMPLE_TID
;
1156 attr
->sample_type
|= PERF_SAMPLE_PERIOD
;
1158 attr
->sample_freq
= freq
;
1161 attr
->inherit
= (cpu
< 0) && inherit
;
1165 fd
[i
][counter
] = sys_perf_event_open(attr
, target_pid
, cpu
, group_fd
, 0);
1167 if (fd
[i
][counter
] < 0) {
1170 if (err
== EPERM
|| err
== EACCES
)
1171 die("No permission - are you root?\n");
1173 * If it's cycles then fall back to hrtimer
1174 * based cpu-clock-tick sw counter, which
1175 * is always available even if no PMU support:
1177 if (attr
->type
== PERF_TYPE_HARDWARE
1178 && attr
->config
== PERF_COUNT_HW_CPU_CYCLES
) {
1181 warning(" ... trying to fall back to cpu-clock-ticks\n");
1183 attr
->type
= PERF_TYPE_SOFTWARE
;
1184 attr
->config
= PERF_COUNT_SW_CPU_CLOCK
;
1188 error("perfcounter syscall returned with %d (%s)\n",
1189 fd
[i
][counter
], strerror(err
));
1190 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
1193 assert(fd
[i
][counter
] >= 0);
1194 fcntl(fd
[i
][counter
], F_SETFL
, O_NONBLOCK
);
1197 * First counter acts as the group leader:
1199 if (group
&& group_fd
== -1)
1200 group_fd
= fd
[i
][counter
];
1202 event_array
[nr_poll
].fd
= fd
[i
][counter
];
1203 event_array
[nr_poll
].events
= POLLIN
;
1206 mmap_array
[i
][counter
].counter
= counter
;
1207 mmap_array
[i
][counter
].prev
= 0;
1208 mmap_array
[i
][counter
].mask
= mmap_pages
*page_size
- 1;
1209 mmap_array
[i
][counter
].base
= mmap(NULL
, (mmap_pages
+1)*page_size
,
1210 PROT_READ
, MAP_SHARED
, fd
[i
][counter
], 0);
1211 if (mmap_array
[i
][counter
].base
== MAP_FAILED
)
1212 die("failed to mmap with %d (%s)\n", errno
, strerror(errno
));
1215 static int __cmd_top(void)
1221 * FIXME: perf_session__new should allow passing a O_MMAP, so that all this
1222 * mmap reading, etc is encapsulated in it. Use O_WRONLY for now.
1224 struct perf_session
*session
= perf_session__new(NULL
, O_WRONLY
, false);
1225 if (session
== NULL
)
1228 if (target_pid
!= -1)
1229 event__synthesize_thread(target_pid
, event__process
, session
);
1231 event__synthesize_threads(event__process
, session
);
1233 for (i
= 0; i
< nr_cpus
; i
++) {
1235 for (counter
= 0; counter
< nr_counters
; counter
++)
1236 start_counter(i
, counter
);
1239 /* Wait for a minimal set of events before starting the snapshot */
1240 poll(event_array
, nr_poll
, 100);
1242 perf_session__mmap_read(session
);
1244 if (pthread_create(&thread
, NULL
, display_thread
, NULL
)) {
1245 printf("Could not create display thread.\n");
1249 if (realtime_prio
) {
1250 struct sched_param param
;
1252 param
.sched_priority
= realtime_prio
;
1253 if (sched_setscheduler(0, SCHED_FIFO
, ¶m
)) {
1254 printf("Could not set realtime priority.\n");
1262 perf_session__mmap_read(session
);
1264 if (hits
== samples
)
1265 ret
= poll(event_array
, nr_poll
, 100);
1271 static const char * const top_usage
[] = {
1272 "perf top [<options>]",
1276 static const struct option options
[] = {
1277 OPT_CALLBACK('e', "event", NULL
, "event",
1278 "event selector. use 'perf list' to list available events",
1280 OPT_INTEGER('c', "count", &default_interval
,
1281 "event period to sample"),
1282 OPT_INTEGER('p', "pid", &target_pid
,
1283 "profile events on existing pid"),
1284 OPT_BOOLEAN('a', "all-cpus", &system_wide
,
1285 "system-wide collection from all CPUs"),
1286 OPT_INTEGER('C', "CPU", &profile_cpu
,
1287 "CPU to profile on"),
1288 OPT_STRING('k', "vmlinux", &symbol_conf
.vmlinux_name
,
1289 "file", "vmlinux pathname"),
1290 OPT_BOOLEAN('K', "hide_kernel_symbols", &hide_kernel_symbols
,
1291 "hide kernel symbols"),
1292 OPT_INTEGER('m', "mmap-pages", &mmap_pages
,
1293 "number of mmap data pages"),
1294 OPT_INTEGER('r', "realtime", &realtime_prio
,
1295 "collect data with this RT SCHED_FIFO priority"),
1296 OPT_INTEGER('d', "delay", &delay_secs
,
1297 "number of seconds to delay between refreshes"),
1298 OPT_BOOLEAN('D', "dump-symtab", &dump_symtab
,
1299 "dump the symbol table used for profiling"),
1300 OPT_INTEGER('f', "count-filter", &count_filter
,
1301 "only display functions with more events than this"),
1302 OPT_BOOLEAN('g', "group", &group
,
1303 "put the counters into a counter group"),
1304 OPT_BOOLEAN('i', "inherit", &inherit
,
1305 "child tasks inherit counters"),
1306 OPT_STRING('s', "sym-annotate", &sym_filter
, "symbol name",
1307 "symbol to annotate"),
1308 OPT_BOOLEAN('z', "zero", &zero
,
1309 "zero history across updates"),
1310 OPT_INTEGER('F', "freq", &freq
,
1311 "profile at this frequency"),
1312 OPT_INTEGER('E', "entries", &print_entries
,
1313 "display this many functions"),
1314 OPT_BOOLEAN('U', "hide_user_symbols", &hide_user_symbols
,
1315 "hide user symbols"),
1316 OPT_INCR('v', "verbose", &verbose
,
1317 "be more verbose (show counter open errors, etc)"),
1321 int cmd_top(int argc
, const char **argv
, const char *prefix __used
)
1325 page_size
= sysconf(_SC_PAGE_SIZE
);
1327 argc
= parse_options(argc
, argv
, options
, top_usage
, 0);
1329 usage_with_options(top_usage
, options
);
1331 /* CPU and PID are mutually exclusive */
1332 if (target_pid
!= -1 && profile_cpu
!= -1) {
1333 printf("WARNING: PID switch overriding CPU\n");
1341 symbol_conf
.priv_size
= (sizeof(struct sym_entry
) +
1342 (nr_counters
+ 1) * sizeof(unsigned long));
1344 symbol_conf
.try_vmlinux_path
= (symbol_conf
.vmlinux_name
== NULL
);
1345 if (symbol__init() < 0)
1352 * User specified count overrides default frequency.
1354 if (default_interval
)
1357 default_interval
= freq
;
1359 fprintf(stderr
, "frequency and count are zero, aborting\n");
1364 * Fill in the ones not specifically initialized via -c:
1366 for (counter
= 0; counter
< nr_counters
; counter
++) {
1367 if (attrs
[counter
].sample_period
)
1370 attrs
[counter
].sample_period
= default_interval
;
1373 if (target_pid
!= -1 || profile_cpu
!= -1)
1376 nr_cpus
= read_cpu_map();
1378 get_term_dimensions(&winsize
);
1379 if (print_entries
== 0) {
1380 update_print_entries(&winsize
);
1381 signal(SIGWINCH
, sig_winch_handler
);