2 * kerneltop.c: show top kernel functions - performance counters showcase
6 make -C Documentation/perf_counter/
10 ------------------------------------------------------------------------------
11 KernelTop: 2669 irqs/sec [cache-misses/cache-refs], (all, cpu: 2)
12 ------------------------------------------------------------------------------
14 weight RIP kernel function
15 ______ ________________ _______________
17 35.20 - ffffffff804ce74b : skb_copy_and_csum_dev
18 33.00 - ffffffff804cb740 : sock_alloc_send_skb
19 31.26 - ffffffff804ce808 : skb_push
20 22.43 - ffffffff80510004 : tcp_established_options
21 19.00 - ffffffff8027d250 : find_get_page
22 15.76 - ffffffff804e4fc9 : eth_type_trans
23 15.20 - ffffffff804d8baa : dst_release
24 14.86 - ffffffff804cf5d8 : skb_release_head_state
25 14.00 - ffffffff802217d5 : read_hpet
26 12.00 - ffffffff804ffb7f : __ip_local_out
27 11.97 - ffffffff804fc0c8 : ip_local_deliver_finish
28 8.54 - ffffffff805001a3 : ip_queue_xmit
32 * Copyright (C) 2008, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
34 * Improvements and fixes by:
36 * Arjan van de Ven <arjan@linux.intel.com>
37 * Yanmin Zhang <yanmin.zhang@intel.com>
38 * Wu Fengguang <fengguang.wu@intel.com>
39 * Mike Galbraith <efault@gmx.de>
40 * Paul Mackerras <paulus@samba.org>
42 * Released under the GPL v2. (and only v2, not any later version)
47 #include "util/util.h"
48 #include "util/util.h"
49 #include "util/parse-options.h"
50 #include "util/parse-events.h"
62 #include <sys/syscall.h>
63 #include <sys/ioctl.h>
65 #include <sys/prctl.h>
70 #include <linux/unistd.h>
71 #include <linux/types.h>
73 static int system_wide = 0;
75 static __u64 default_event_id[MAX_COUNTERS] = {
76 EID(PERF_TYPE_SOFTWARE, PERF_COUNT_TASK_CLOCK),
77 EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CONTEXT_SWITCHES),
78 EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CPU_MIGRATIONS),
79 EID(PERF_TYPE_SOFTWARE, PERF_COUNT_PAGE_FAULTS),
81 EID(PERF_TYPE_HARDWARE, PERF_COUNT_CPU_CYCLES),
82 EID(PERF_TYPE_HARDWARE, PERF_COUNT_INSTRUCTIONS),
83 EID(PERF_TYPE_HARDWARE, PERF_COUNT_CACHE_REFERENCES),
84 EID(PERF_TYPE_HARDWARE, PERF_COUNT_CACHE_MISSES),
86 static int default_interval = 100000;
87 static int event_count[MAX_COUNTERS];
88 static int fd[MAX_NR_CPUS][MAX_COUNTERS];
90 static __u64 count_filter = 100;
92 static int target_pid = -1;
93 static int profile_cpu = -1;
94 static int nr_cpus = 0;
95 static unsigned int realtime_prio = 0;
97 static unsigned int page_size;
98 static unsigned int mmap_pages = 16;
99 static int use_mmap = 0;
100 static int use_munmap = 0;
103 static char *sym_filter;
104 static unsigned long filter_start;
105 static unsigned long filter_end;
107 static int delay_secs = 2;
109 static int dump_symtab;
111 static const unsigned int default_count[] = {
124 static uint64_t min_ip;
125 static uint64_t max_ip = -1ll;
128 unsigned long long addr;
130 unsigned long count[MAX_COUNTERS];
134 #define MAX_SYMS 100000
136 static int sym_table_count;
138 struct sym_entry *sym_filter_entry;
140 static struct sym_entry sym_table[MAX_SYMS];
143 * Ordering weight: count-1 * count-2 * ... / count-n
145 static double sym_weight(const struct sym_entry *sym)
150 weight = sym->count[0];
152 for (counter = 1; counter < nr_counters-1; counter++)
153 weight *= sym->count[counter];
155 weight /= (sym->count[counter] + 1);
160 static int compare(const void *__sym1, const void *__sym2)
162 const struct sym_entry *sym1 = __sym1, *sym2 = __sym2;
164 return sym_weight(sym1) < sym_weight(sym2);
168 static long userspace_events;
169 static const char CONSOLE_CLEAR[] = "
\e[H
\e[2J";
171 static struct sym_entry tmp[MAX_SYMS];
173 static void print_sym_table(void)
175 int i, j, active_count, printed;
177 float events_per_sec = events/delay_secs;
178 float kevents_per_sec = (events-userspace_events)/delay_secs;
179 float sum_kevents = 0.0;
181 events = userspace_events = 0;
183 /* Iterate over symbol table and copy/tally/decay active symbols. */
184 for (i = 0, active_count = 0; i < sym_table_count; i++) {
185 if (sym_table[i].count[0]) {
186 tmp[active_count++] = sym_table[i];
187 sum_kevents += sym_table[i].count[0];
189 for (j = 0; j < nr_counters; j++)
190 sym_table[i].count[j] = zero ? 0 : sym_table[i].count[j] * 7 / 8;
194 qsort(tmp, active_count + 1, sizeof(tmp[0]), compare);
196 write(1, CONSOLE_CLEAR, strlen(CONSOLE_CLEAR));
199 "------------------------------------------------------------------------------\n");
200 printf( " KernelTop:%8.0f irqs/sec kernel:%4.1f%% [",
202 100.0 - (100.0*((events_per_sec-kevents_per_sec)/events_per_sec)));
204 if (nr_counters == 1)
205 printf("%d ", event_count[0]);
207 for (counter = 0; counter < nr_counters; counter++) {
211 printf("%s", event_name(counter));
216 if (target_pid != -1)
217 printf(" (target_pid: %d", target_pid);
221 if (profile_cpu != -1)
222 printf(", cpu: %d)\n", profile_cpu);
224 if (target_pid != -1)
227 printf(", %d CPUs)\n", nr_cpus);
230 printf("------------------------------------------------------------------------------\n\n");
232 if (nr_counters == 1)
233 printf(" events pcnt");
235 printf(" weight events pcnt");
237 printf(" RIP kernel function\n"
238 " ______ ______ _____ ________________ _______________\n\n"
241 for (i = 0, printed = 0; i < active_count; i++) {
244 if (++printed > 18 || tmp[i].count[0] < count_filter)
247 pcnt = 100.0 - (100.0*((sum_kevents-tmp[i].count[0])/sum_kevents));
249 if (nr_counters == 1)
250 printf("%19.2f - %4.1f%% - %016llx : %s\n",
252 pcnt, tmp[i].addr, tmp[i].sym);
254 printf("%8.1f %10ld - %4.1f%% - %016llx : %s\n",
257 pcnt, tmp[i].addr, tmp[i].sym);
261 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
263 if (poll(&stdin_poll, 1, 0) == 1) {
264 printf("key pressed - exiting.\n");
270 static void *display_thread(void *arg)
272 printf("KernelTop refresh period: %d seconds\n", delay_secs);
274 while (!sleep(delay_secs))
280 static int read_symbol(FILE *in, struct sym_entry *s)
282 static int filter_match = 0;
287 rc = fscanf(in, "%llx %c %499s", &s->addr, &stype, str);
293 /* skip until end of line: */
297 if (rc == '\n' || rc == EOF || pos >= 499)
306 /* Filter out known duplicates and non-text symbols. */
307 if (!strcmp(sym, "_text"))
309 if (!min_ip && !strcmp(sym, "_stext"))
311 if (!strcmp(sym, "_etext") || !strcmp(sym, "_sinittext"))
313 if (stype != 'T' && stype != 't')
315 if (!strncmp("init_module", sym, 11) || !strncmp("cleanup_module", sym, 14))
317 if (strstr(sym, "_text_start") || strstr(sym, "_text_end"))
320 s->sym = malloc(strlen(str)+1);
323 strcpy((char *)s->sym, str);
326 /* Tag events to be skipped. */
327 if (!strcmp("default_idle", s->sym) || !strcmp("cpu_idle", s->sym))
329 else if (!strcmp("enter_idle", s->sym) || !strcmp("exit_idle", s->sym))
331 else if (!strcmp("mwait_idle", s->sym))
334 if (filter_match == 1) {
335 filter_end = s->addr;
337 if (filter_end - filter_start > 10000) {
338 printf("hm, too large filter symbol <%s> - skipping.\n",
340 printf("symbol filter start: %016lx\n", filter_start);
341 printf(" end: %016lx\n", filter_end);
342 filter_end = filter_start = 0;
347 if (filter_match == 0 && sym_filter && !strcmp(s->sym, sym_filter)) {
349 filter_start = s->addr;
355 static int compare_addr(const void *__sym1, const void *__sym2)
357 const struct sym_entry *sym1 = __sym1, *sym2 = __sym2;
359 return sym1->addr > sym2->addr;
362 static void sort_symbol_table(void)
367 qsort(sym_table, sym_table_count, sizeof(sym_table[0]), compare_addr);
368 for (i = 0, dups = 0; i < sym_table_count; i++) {
369 if (sym_table[i].addr == sym_table[i+1].addr) {
370 sym_table[i+1].addr = -1ll;
374 sym_table_count -= dups;
378 static void parse_symbols(void)
380 struct sym_entry *last;
382 FILE *kallsyms = fopen("/proc/kallsyms", "r");
385 printf("Could not open /proc/kallsyms - no CONFIG_KALLSYMS_ALL=y?\n");
389 while (!feof(kallsyms)) {
390 if (read_symbol(kallsyms, &sym_table[sym_table_count]) == 0) {
392 assert(sym_table_count <= MAX_SYMS);
397 min_ip = sym_table[0].addr;
398 max_ip = sym_table[sym_table_count-1].addr;
399 last = sym_table + sym_table_count++;
406 for (count=0; count < sym_table_count; count ++) {
407 if (!strcmp(sym_table[count].sym, sym_filter)) {
408 sym_filter_entry = &sym_table[count];
416 for (i = 0; i < sym_table_count; i++)
417 fprintf(stderr, "%llx %s\n",
418 sym_table[i].addr, sym_table[i].sym);
422 #define TRACE_COUNT 3
425 * Binary search in the histogram table and record the hit:
427 static void record_ip(uint64_t ip, int counter)
429 int left_idx, middle_idx, right_idx, idx;
430 unsigned long left, middle, right;
433 right_idx = sym_table_count-1;
434 assert(ip <= max_ip && ip >= min_ip);
436 while (left_idx + 1 < right_idx) {
437 middle_idx = (left_idx + right_idx) / 2;
439 left = sym_table[ left_idx].addr;
440 middle = sym_table[middle_idx].addr;
441 right = sym_table[ right_idx].addr;
443 if (!(left <= middle && middle <= right)) {
444 printf("%016lx...\n%016lx...\n%016lx\n", left, middle, right);
445 printf("%d %d %d\n", left_idx, middle_idx, right_idx);
447 assert(left <= middle && middle <= right);
448 if (!(left <= ip && ip <= right)) {
449 printf(" left: %016lx\n", left);
450 printf(" ip: %016lx\n", (unsigned long)ip);
451 printf("right: %016lx\n", right);
453 assert(left <= ip && ip <= right);
455 * [ left .... target .... middle .... right ]
459 right_idx = middle_idx;
463 * [ left .... middle ... target ... right ]
466 left_idx = middle_idx;
471 if (!sym_table[idx].skip)
472 sym_table[idx].count[counter]++;
476 static void process_event(uint64_t ip, int counter)
480 if (ip < min_ip || ip > max_ip) {
485 record_ip(ip, counter);
495 static unsigned int mmap_read_head(struct mmap_data *md)
497 struct perf_counter_mmap_page *pc = md->base;
500 head = pc->data_head;
506 struct timeval last_read, this_read;
508 static void mmap_read(struct mmap_data *md)
510 unsigned int head = mmap_read_head(md);
511 unsigned int old = md->prev;
512 unsigned char *data = md->base + page_size;
515 gettimeofday(&this_read, NULL);
518 * If we're further behind than half the buffer, there's a chance
519 * the writer will bite our tail and screw up the events under us.
521 * If we somehow ended up ahead of the head, we got messed up.
523 * In either case, truncate and restart at head.
526 if (diff > md->mask / 2 || diff < 0) {
530 timersub(&this_read, &last_read, &iv);
531 msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
533 fprintf(stderr, "WARNING: failed to keep up with mmap data."
534 " Last read %lu msecs ago.\n", msecs);
537 * head points to a known good entry, start there.
542 last_read = this_read;
544 for (; old != head;) {
546 struct perf_event_header header;
548 __u32 pid, target_pid;
551 struct perf_event_header header;
552 __u32 pid, target_pid;
556 char filename[PATH_MAX];
559 typedef union event_union {
560 struct perf_event_header header;
562 struct mmap_event mmap;
565 event_t *event = (event_t *)&data[old & md->mask];
569 size_t size = event->header.size;
572 * Event straddles the mmap boundary -- header should always
573 * be inside due to u64 alignment of output.
575 if ((old & md->mask) + size != ((old + size) & md->mask)) {
576 unsigned int offset = old;
577 unsigned int len = min(sizeof(*event), size), cpy;
578 void *dst = &event_copy;
581 cpy = min(md->mask + 1 - (offset & md->mask), len);
582 memcpy(dst, &data[offset & md->mask], cpy);
593 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) {
594 if (event->header.type & PERF_RECORD_IP)
595 process_event(event->ip.ip, md->counter);
597 switch (event->header.type) {
598 case PERF_EVENT_MMAP:
599 case PERF_EVENT_MUNMAP:
600 printf("%s: %Lu %Lu %Lu %s\n",
601 event->header.type == PERF_EVENT_MMAP
606 event->mmap.filename);
615 static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
616 static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
618 static int __cmd_top(void)
620 struct perf_counter_hw_event hw_event;
622 int i, counter, group_fd, nr_poll = 0;
626 for (i = 0; i < nr_cpus; i++) {
628 for (counter = 0; counter < nr_counters; counter++) {
631 if (target_pid == -1 && profile_cpu == -1)
634 memset(&hw_event, 0, sizeof(hw_event));
635 hw_event.config = event_id[counter];
636 hw_event.irq_period = event_count[counter];
637 hw_event.record_type = PERF_RECORD_IP | PERF_RECORD_TID;
639 hw_event.mmap = use_mmap;
640 hw_event.munmap = use_munmap;
641 hw_event.freq = freq;
643 fd[i][counter] = sys_perf_counter_open(&hw_event, target_pid, cpu, group_fd, 0);
644 if (fd[i][counter] < 0) {
646 printf("kerneltop error: syscall returned with %d (%s)\n",
647 fd[i][counter], strerror(err));
649 printf("Are you root?\n");
652 assert(fd[i][counter] >= 0);
653 fcntl(fd[i][counter], F_SETFL, O_NONBLOCK);
656 * First counter acts as the group leader:
658 if (group && group_fd == -1)
659 group_fd = fd[i][counter];
661 event_array[nr_poll].fd = fd[i][counter];
662 event_array[nr_poll].events = POLLIN;
665 mmap_array[i][counter].counter = counter;
666 mmap_array[i][counter].prev = 0;
667 mmap_array[i][counter].mask = mmap_pages*page_size - 1;
668 mmap_array[i][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
669 PROT_READ, MAP_SHARED, fd[i][counter], 0);
670 if (mmap_array[i][counter].base == MAP_FAILED) {
671 printf("kerneltop error: failed to mmap with %d (%s)\n",
672 errno, strerror(errno));
678 if (pthread_create(&thread, NULL, display_thread, NULL)) {
679 printf("Could not create display thread.\n");
684 struct sched_param param;
686 param.sched_priority = realtime_prio;
687 if (sched_setscheduler(0, SCHED_FIFO, ¶m)) {
688 printf("Could not set realtime priority.\n");
696 for (i = 0; i < nr_cpus; i++) {
697 for (counter = 0; counter < nr_counters; counter++)
698 mmap_read(&mmap_array[i][counter]);
702 ret = poll(event_array, nr_poll, 100);
708 static const char * const top_usage[] = {
709 "perf top [<options>]",
713 static char events_help_msg[EVENTS_HELP_MAX];
715 static const struct option options[] = {
716 OPT_CALLBACK('e', "event", NULL, "event",
717 events_help_msg, parse_events),
718 OPT_INTEGER('c', "count", &default_interval,
719 "event period to sample"),
720 OPT_INTEGER('p', "pid", &target_pid,
721 "profile events on existing pid"),
722 OPT_BOOLEAN('a', "all-cpus", &system_wide,
723 "system-wide collection from all CPUs"),
724 OPT_INTEGER('C', "CPU", &profile_cpu,
725 "CPU to profile on"),
726 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
727 "number of mmap data pages"),
728 OPT_INTEGER('r', "realtime", &realtime_prio,
729 "collect data with this RT SCHED_FIFO priority"),
730 OPT_INTEGER('d', "delay", &realtime_prio,
731 "number of seconds to delay between refreshes"),
732 OPT_BOOLEAN('D', "dump-symtab", &dump_symtab,
733 "dump the symbol table used for profiling"),
734 OPT_INTEGER('f', "--count-filter", &count_filter,
735 "only display functions with more events than this"),
736 OPT_BOOLEAN('g', "group", &group,
737 "put the counters into a counter group"),
738 OPT_STRING('s', "sym-filter", &sym_filter, "pattern",
739 "only display symbols matchig this pattern"),
740 OPT_BOOLEAN('z', "zero", &group,
741 "zero history across updates"),
742 OPT_BOOLEAN('M', "use-mmap", &use_mmap,
743 "track mmap events"),
744 OPT_BOOLEAN('U', "use-munmap", &use_munmap,
745 "track munmap events"),
746 OPT_INTEGER('F', "--freq", &freq,
747 "profile at this frequency"),
751 int cmd_top(int argc, const char **argv, const char *prefix)
755 page_size = sysconf(_SC_PAGE_SIZE);
757 create_events_help(events_help_msg);
758 memcpy(event_id, default_event_id, sizeof(default_event_id));
760 argc = parse_options(argc, argv, options, top_usage, 0);
762 usage_with_options(top_usage, options);
765 default_interval = freq;
769 /* CPU and PID are mutually exclusive */
770 if (target_pid != -1 && profile_cpu != -1) {
771 printf("WARNING: PID switch overriding CPU\n");
781 for (counter = 0; counter < nr_counters; counter++) {
782 if (event_count[counter])
785 event_count[counter] = default_interval;
788 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
789 assert(nr_cpus <= MAX_NR_CPUS);
790 assert(nr_cpus >= 0);
792 if (target_pid != -1 || profile_cpu != -1)