2 * kerneltop.c: show top kernel functions - performance counters showcase
6 make -C Documentation/perf_counter/
10 ------------------------------------------------------------------------------
11 KernelTop: 2669 irqs/sec [NMI, 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)
45 #include "util/util.h"
56 #include <sys/syscall.h>
57 #include <sys/ioctl.h>
59 #include <sys/prctl.h>
64 #include <linux/unistd.h>
65 #include <linux/types.h>
67 #include "../../include/linux/perf_counter.h"
71 static int system_wide
= 0;
73 static int nr_counters
= 0;
74 static __u64 event_id
[MAX_COUNTERS
] = {
75 EID(PERF_TYPE_SOFTWARE
, PERF_COUNT_TASK_CLOCK
),
76 EID(PERF_TYPE_SOFTWARE
, PERF_COUNT_CONTEXT_SWITCHES
),
77 EID(PERF_TYPE_SOFTWARE
, PERF_COUNT_CPU_MIGRATIONS
),
78 EID(PERF_TYPE_SOFTWARE
, PERF_COUNT_PAGE_FAULTS
),
80 EID(PERF_TYPE_HARDWARE
, PERF_COUNT_CPU_CYCLES
),
81 EID(PERF_TYPE_HARDWARE
, PERF_COUNT_INSTRUCTIONS
),
82 EID(PERF_TYPE_HARDWARE
, PERF_COUNT_CACHE_REFERENCES
),
83 EID(PERF_TYPE_HARDWARE
, PERF_COUNT_CACHE_MISSES
),
85 static int default_interval
= 100000;
86 static int event_count
[MAX_COUNTERS
];
87 static int fd
[MAX_NR_CPUS
][MAX_COUNTERS
];
89 static __u64 count_filter
= 100;
92 static int profile_cpu
= -1;
93 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;
102 static char *vmlinux
;
104 static char *sym_filter
;
105 static unsigned long filter_start
;
106 static unsigned long filter_end
;
108 static int delay_secs
= 2;
110 static int dump_symtab
;
118 struct source_line
*next
;
121 static struct source_line
*lines
;
122 static struct source_line
**lines_tail
;
124 static const unsigned int default_count
[] = {
133 static char *hw_event_names
[] = {
143 static char *sw_event_names
[] = {
153 struct event_symbol
{
158 static struct event_symbol event_symbols
[] = {
159 {EID(PERF_TYPE_HARDWARE
, PERF_COUNT_CPU_CYCLES
), "cpu-cycles", },
160 {EID(PERF_TYPE_HARDWARE
, PERF_COUNT_CPU_CYCLES
), "cycles", },
161 {EID(PERF_TYPE_HARDWARE
, PERF_COUNT_INSTRUCTIONS
), "instructions", },
162 {EID(PERF_TYPE_HARDWARE
, PERF_COUNT_CACHE_REFERENCES
), "cache-references", },
163 {EID(PERF_TYPE_HARDWARE
, PERF_COUNT_CACHE_MISSES
), "cache-misses", },
164 {EID(PERF_TYPE_HARDWARE
, PERF_COUNT_BRANCH_INSTRUCTIONS
), "branch-instructions", },
165 {EID(PERF_TYPE_HARDWARE
, PERF_COUNT_BRANCH_INSTRUCTIONS
), "branches", },
166 {EID(PERF_TYPE_HARDWARE
, PERF_COUNT_BRANCH_MISSES
), "branch-misses", },
167 {EID(PERF_TYPE_HARDWARE
, PERF_COUNT_BUS_CYCLES
), "bus-cycles", },
169 {EID(PERF_TYPE_SOFTWARE
, PERF_COUNT_CPU_CLOCK
), "cpu-clock", },
170 {EID(PERF_TYPE_SOFTWARE
, PERF_COUNT_TASK_CLOCK
), "task-clock", },
171 {EID(PERF_TYPE_SOFTWARE
, PERF_COUNT_PAGE_FAULTS
), "page-faults", },
172 {EID(PERF_TYPE_SOFTWARE
, PERF_COUNT_PAGE_FAULTS
), "faults", },
173 {EID(PERF_TYPE_SOFTWARE
, PERF_COUNT_PAGE_FAULTS_MIN
), "minor-faults", },
174 {EID(PERF_TYPE_SOFTWARE
, PERF_COUNT_PAGE_FAULTS_MAJ
), "major-faults", },
175 {EID(PERF_TYPE_SOFTWARE
, PERF_COUNT_CONTEXT_SWITCHES
), "context-switches", },
176 {EID(PERF_TYPE_SOFTWARE
, PERF_COUNT_CONTEXT_SWITCHES
), "cs", },
177 {EID(PERF_TYPE_SOFTWARE
, PERF_COUNT_CPU_MIGRATIONS
), "cpu-migrations", },
178 {EID(PERF_TYPE_SOFTWARE
, PERF_COUNT_CPU_MIGRATIONS
), "migrations", },
181 #define __PERF_COUNTER_FIELD(config, name) \
182 ((config & PERF_COUNTER_##name##_MASK) >> PERF_COUNTER_##name##_SHIFT)
184 #define PERF_COUNTER_RAW(config) __PERF_COUNTER_FIELD(config, RAW)
185 #define PERF_COUNTER_CONFIG(config) __PERF_COUNTER_FIELD(config, CONFIG)
186 #define PERF_COUNTER_TYPE(config) __PERF_COUNTER_FIELD(config, TYPE)
187 #define PERF_COUNTER_ID(config) __PERF_COUNTER_FIELD(config, EVENT)
189 static void display_events_help(void)
195 " -e EVENT --event=EVENT # symbolic-name abbreviations");
197 for (i
= 0; i
< ARRAY_SIZE(event_symbols
); i
++) {
200 e
= event_symbols
[i
].event
;
201 type
= PERF_COUNTER_TYPE(e
);
202 id
= PERF_COUNTER_ID(e
);
204 printf("\n %d:%d: %-20s",
205 type
, id
, event_symbols
[i
].symbol
);
209 " rNNN: raw PMU events (eventsel+umask)\n\n");
212 static void display_help(void)
215 "Usage: kerneltop [<options>]\n"
216 " Or: kerneltop -S [<options>] COMMAND [ARGS]\n\n"
217 "KernelTop Options (up to %d event types can be specified at once):\n\n",
220 display_events_help();
223 " -c CNT --count=CNT # event period to sample\n\n"
224 " -C CPU --cpu=CPU # CPU (-1 for all) [default: -1]\n"
225 " -p PID --pid=PID # PID of sampled task (-1 for all) [default: -1]\n\n"
226 " -l # show scale factor for RR events\n"
227 " -d delay --delay=<seconds> # sampling/display delay [default: 2]\n"
228 " -f CNT --filter=CNT # min-event-count filter [default: 100]\n\n"
229 " -r prio --realtime=<prio> # event acquisition runs with SCHED_FIFO policy\n"
230 " -s symbol --symbol=<symbol> # function to be showed annotated one-shot\n"
231 " -x path --vmlinux=<path> # the vmlinux binary, required for -s use\n"
232 " -z --zero # zero counts after display\n"
233 " -D --dump_symtab # dump symbol table to stderr on startup\n"
234 " -m pages --mmap_pages=<pages> # number of mmap data pages\n"
235 " -M --mmap_info # print mmap info stream\n"
236 " -U --munmap_info # print munmap info stream\n"
242 static char *event_name(int ctr
)
244 __u64 config
= event_id
[ctr
];
245 int type
= PERF_COUNTER_TYPE(config
);
246 int id
= PERF_COUNTER_ID(config
);
249 if (PERF_COUNTER_RAW(config
)) {
250 sprintf(buf
, "raw 0x%llx", PERF_COUNTER_CONFIG(config
));
255 case PERF_TYPE_HARDWARE
:
256 if (id
< PERF_HW_EVENTS_MAX
)
257 return hw_event_names
[id
];
258 return "unknown-hardware";
260 case PERF_TYPE_SOFTWARE
:
261 if (id
< PERF_SW_EVENTS_MAX
)
262 return sw_event_names
[id
];
263 return "unknown-software";
273 * Each event can have multiple symbolic names.
274 * Symbolic names are (almost) exactly matched.
276 static __u64
match_event_symbols(char *str
)
282 if (sscanf(str
, "r%llx", &config
) == 1)
283 return config
| PERF_COUNTER_RAW_MASK
;
285 if (sscanf(str
, "%d:%llu", &type
, &id
) == 2)
286 return EID(type
, id
);
288 for (i
= 0; i
< ARRAY_SIZE(event_symbols
); i
++) {
289 if (!strncmp(str
, event_symbols
[i
].symbol
,
290 strlen(event_symbols
[i
].symbol
)))
291 return event_symbols
[i
].event
;
297 static int parse_events(char *str
)
302 if (nr_counters
== MAX_COUNTERS
)
305 config
= match_event_symbols(str
);
309 event_id
[nr_counters
] = config
;
312 str
= strstr(str
, ",");
325 static uint64_t min_ip
;
326 static uint64_t max_ip
= -1ll;
329 unsigned long long addr
;
331 unsigned long count
[MAX_COUNTERS
];
333 struct source_line
*source
;
336 #define MAX_SYMS 100000
338 static int sym_table_count
;
340 struct sym_entry
*sym_filter_entry
;
342 static struct sym_entry sym_table
[MAX_SYMS
];
344 static void show_details(struct sym_entry
*sym
);
347 * Ordering weight: count-1 * count-2 * ... / count-n
349 static double sym_weight(const struct sym_entry
*sym
)
354 weight
= sym
->count
[0];
356 for (counter
= 1; counter
< nr_counters
-1; counter
++)
357 weight
*= sym
->count
[counter
];
359 weight
/= (sym
->count
[counter
] + 1);
364 static int compare(const void *__sym1
, const void *__sym2
)
366 const struct sym_entry
*sym1
= __sym1
, *sym2
= __sym2
;
368 return sym_weight(sym1
) < sym_weight(sym2
);
372 static long userspace_events
;
373 static const char CONSOLE_CLEAR
[] = "\e[H\e[2J";
375 static struct sym_entry tmp
[MAX_SYMS
];
377 static void print_sym_table(void)
381 float events_per_sec
= events
/delay_secs
;
382 float kevents_per_sec
= (events
-userspace_events
)/delay_secs
;
383 float sum_kevents
= 0.0;
385 events
= userspace_events
= 0;
386 memcpy(tmp
, sym_table
, sizeof(sym_table
[0])*sym_table_count
);
387 qsort(tmp
, sym_table_count
, sizeof(tmp
[0]), compare
);
389 for (i
= 0; i
< sym_table_count
&& tmp
[i
].count
[0]; i
++)
390 sum_kevents
+= tmp
[i
].count
[0];
392 write(1, CONSOLE_CLEAR
, strlen(CONSOLE_CLEAR
));
395 "------------------------------------------------------------------------------\n");
396 printf( " KernelTop:%8.0f irqs/sec kernel:%4.1f%% [%s, ",
398 100.0 - (100.0*((events_per_sec
-kevents_per_sec
)/events_per_sec
)),
399 nmi
? "NMI" : "IRQ");
401 if (nr_counters
== 1)
402 printf("%d ", event_count
[0]);
404 for (counter
= 0; counter
< nr_counters
; counter
++) {
408 printf("%s", event_name(counter
));
414 printf(" (tid: %d", tid
);
418 if (profile_cpu
!= -1)
419 printf(", cpu: %d)\n", profile_cpu
);
424 printf(", %d CPUs)\n", nr_cpus
);
427 printf("------------------------------------------------------------------------------\n\n");
429 if (nr_counters
== 1)
430 printf(" events pcnt");
432 printf(" weight events pcnt");
434 printf(" RIP kernel function\n"
435 " ______ ______ _____ ________________ _______________\n\n"
438 for (i
= 0, printed
= 0; i
< sym_table_count
; i
++) {
442 if (printed
<= 18 && tmp
[i
].count
[0] >= count_filter
) {
443 pcnt
= 100.0 - (100.0*((sum_kevents
-tmp
[i
].count
[0])/sum_kevents
));
445 if (nr_counters
== 1)
446 printf("%19.2f - %4.1f%% - %016llx : %s\n",
448 pcnt
, tmp
[i
].addr
, tmp
[i
].sym
);
450 printf("%8.1f %10ld - %4.1f%% - %016llx : %s\n",
453 pcnt
, tmp
[i
].addr
, tmp
[i
].sym
);
457 * Add decay to the counts:
459 for (count
= 0; count
< nr_counters
; count
++)
460 sym_table
[i
].count
[count
] = zero
? 0 : sym_table
[i
].count
[count
] * 7 / 8;
463 if (sym_filter_entry
)
464 show_details(sym_filter_entry
);
467 struct pollfd stdin_poll
= { .fd
= 0, .events
= POLLIN
};
469 if (poll(&stdin_poll
, 1, 0) == 1) {
470 printf("key pressed - exiting.\n");
476 static void *display_thread(void *arg
)
478 printf("KernelTop refresh period: %d seconds\n", delay_secs
);
480 while (!sleep(delay_secs
))
486 static int read_symbol(FILE *in
, struct sym_entry
*s
)
488 static int filter_match
= 0;
493 rc
= fscanf(in
, "%llx %c %499s", &s
->addr
, &stype
, str
);
499 /* skip until end of line: */
503 if (rc
== '\n' || rc
== EOF
|| pos
>= 499)
512 /* Filter out known duplicates and non-text symbols. */
513 if (!strcmp(sym
, "_text"))
515 if (!min_ip
&& !strcmp(sym
, "_stext"))
517 if (!strcmp(sym
, "_etext") || !strcmp(sym
, "_sinittext"))
519 if (stype
!= 'T' && stype
!= 't')
521 if (!strncmp("init_module", sym
, 11) || !strncmp("cleanup_module", sym
, 14))
523 if (strstr(sym
, "_text_start") || strstr(sym
, "_text_end"))
526 s
->sym
= malloc(strlen(str
));
529 strcpy((char *)s
->sym
, str
);
532 /* Tag events to be skipped. */
533 if (!strcmp("default_idle", s
->sym
) || !strcmp("cpu_idle", s
->sym
))
535 else if (!strcmp("enter_idle", s
->sym
) || !strcmp("exit_idle", s
->sym
))
537 else if (!strcmp("mwait_idle", s
->sym
))
540 if (filter_match
== 1) {
541 filter_end
= s
->addr
;
543 if (filter_end
- filter_start
> 10000) {
544 printf("hm, too large filter symbol <%s> - skipping.\n",
546 printf("symbol filter start: %016lx\n", filter_start
);
547 printf(" end: %016lx\n", filter_end
);
548 filter_end
= filter_start
= 0;
553 if (filter_match
== 0 && sym_filter
&& !strcmp(s
->sym
, sym_filter
)) {
555 filter_start
= s
->addr
;
561 static int compare_addr(const void *__sym1
, const void *__sym2
)
563 const struct sym_entry
*sym1
= __sym1
, *sym2
= __sym2
;
565 return sym1
->addr
> sym2
->addr
;
568 static void sort_symbol_table(void)
573 qsort(sym_table
, sym_table_count
, sizeof(sym_table
[0]), compare_addr
);
574 for (i
= 0, dups
= 0; i
< sym_table_count
; i
++) {
575 if (sym_table
[i
].addr
== sym_table
[i
+1].addr
) {
576 sym_table
[i
+1].addr
= -1ll;
580 sym_table_count
-= dups
;
584 static void parse_symbols(void)
586 struct sym_entry
*last
;
588 FILE *kallsyms
= fopen("/proc/kallsyms", "r");
591 printf("Could not open /proc/kallsyms - no CONFIG_KALLSYMS_ALL=y?\n");
595 while (!feof(kallsyms
)) {
596 if (read_symbol(kallsyms
, &sym_table
[sym_table_count
]) == 0) {
598 assert(sym_table_count
<= MAX_SYMS
);
603 min_ip
= sym_table
[0].addr
;
604 max_ip
= sym_table
[sym_table_count
-1].addr
;
605 last
= sym_table
+ sym_table_count
++;
612 for (count
=0; count
< sym_table_count
; count
++) {
613 if (!strcmp(sym_table
[count
].sym
, sym_filter
)) {
614 sym_filter_entry
= &sym_table
[count
];
622 for (i
= 0; i
< sym_table_count
; i
++)
623 fprintf(stderr
, "%llx %s\n",
624 sym_table
[i
].addr
, sym_table
[i
].sym
);
632 static void parse_vmlinux(char *filename
)
635 char command
[PATH_MAX
*2];
639 sprintf(command
, "objdump --start-address=0x%016lx --stop-address=0x%016lx -dS %s", filter_start
, filter_end
, filename
);
641 file
= popen(command
, "r");
646 while (!feof(file
)) {
647 struct source_line
*src
;
651 src
= malloc(sizeof(struct source_line
));
653 memset(src
, 0, sizeof(struct source_line
));
655 if (getline(&src
->line
, &dummy
, file
) < 0)
660 c
= strchr(src
->line
, '\n');
666 lines_tail
= &src
->next
;
668 if (strlen(src
->line
)>8 && src
->line
[8] == ':')
669 src
->EIP
= strtoull(src
->line
, NULL
, 16);
670 if (strlen(src
->line
)>8 && src
->line
[16] == ':')
671 src
->EIP
= strtoull(src
->line
, NULL
, 16);
676 static void record_precise_ip(uint64_t ip
)
678 struct source_line
*line
;
680 for (line
= lines
; line
; line
= line
->next
) {
688 static void lookup_sym_in_vmlinux(struct sym_entry
*sym
)
690 struct source_line
*line
;
691 char pattern
[PATH_MAX
];
692 sprintf(pattern
, "<%s>:", sym
->sym
);
694 for (line
= lines
; line
; line
= line
->next
) {
695 if (strstr(line
->line
, pattern
)) {
702 static void show_lines(struct source_line
*line_queue
, int line_queue_count
)
705 struct source_line
*line
;
708 for (i
= 0; i
< line_queue_count
; i
++) {
709 printf("%8li\t%s\n", line
->count
, line
->line
);
714 #define TRACE_COUNT 3
716 static void show_details(struct sym_entry
*sym
)
718 struct source_line
*line
;
719 struct source_line
*line_queue
= NULL
;
721 int line_queue_count
= 0;
724 lookup_sym_in_vmlinux(sym
);
728 printf("Showing details for %s\n", sym
->sym
);
732 if (displayed
&& strstr(line
->line
, ">:"))
735 if (!line_queue_count
)
739 if (line
->count
>= count_filter
) {
740 show_lines(line_queue
, line_queue_count
);
741 line_queue_count
= 0;
743 } else if (line_queue_count
> TRACE_COUNT
) {
744 line_queue
= line_queue
->next
;
757 * Binary search in the histogram table and record the hit:
759 static void record_ip(uint64_t ip
, int counter
)
761 int left_idx
, middle_idx
, right_idx
, idx
;
762 unsigned long left
, middle
, right
;
764 record_precise_ip(ip
);
767 right_idx
= sym_table_count
-1;
768 assert(ip
<= max_ip
&& ip
>= min_ip
);
770 while (left_idx
+ 1 < right_idx
) {
771 middle_idx
= (left_idx
+ right_idx
) / 2;
773 left
= sym_table
[ left_idx
].addr
;
774 middle
= sym_table
[middle_idx
].addr
;
775 right
= sym_table
[ right_idx
].addr
;
777 if (!(left
<= middle
&& middle
<= right
)) {
778 printf("%016lx...\n%016lx...\n%016lx\n", left
, middle
, right
);
779 printf("%d %d %d\n", left_idx
, middle_idx
, right_idx
);
781 assert(left
<= middle
&& middle
<= right
);
782 if (!(left
<= ip
&& ip
<= right
)) {
783 printf(" left: %016lx\n", left
);
784 printf(" ip: %016lx\n", (unsigned long)ip
);
785 printf("right: %016lx\n", right
);
787 assert(left
<= ip
&& ip
<= right
);
789 * [ left .... target .... middle .... right ]
793 right_idx
= middle_idx
;
797 * [ left .... middle ... target ... right ]
800 left_idx
= middle_idx
;
805 if (!sym_table
[idx
].skip
)
806 sym_table
[idx
].count
[counter
]++;
810 static void process_event(uint64_t ip
, int counter
)
814 if (ip
< min_ip
|| ip
> max_ip
) {
819 record_ip(ip
, counter
);
822 static void process_options(int argc
, char **argv
)
824 int error
= 0, counter
;
827 int option_index
= 0;
828 /** Options for getopt */
829 static struct option long_options
[] = {
830 {"count", required_argument
, NULL
, 'c'},
831 {"cpu", required_argument
, NULL
, 'C'},
832 {"delay", required_argument
, NULL
, 'd'},
833 {"dump_symtab", no_argument
, NULL
, 'D'},
834 {"event", required_argument
, NULL
, 'e'},
835 {"filter", required_argument
, NULL
, 'f'},
836 {"group", required_argument
, NULL
, 'g'},
837 {"help", no_argument
, NULL
, 'h'},
838 {"nmi", required_argument
, NULL
, 'n'},
839 {"mmap_info", no_argument
, NULL
, 'M'},
840 {"mmap_pages", required_argument
, NULL
, 'm'},
841 {"munmap_info", no_argument
, NULL
, 'U'},
842 {"pid", required_argument
, NULL
, 'p'},
843 {"realtime", required_argument
, NULL
, 'r'},
844 {"scale", no_argument
, NULL
, 'l'},
845 {"symbol", required_argument
, NULL
, 's'},
846 {"stat", no_argument
, NULL
, 'S'},
847 {"vmlinux", required_argument
, NULL
, 'x'},
848 {"zero", no_argument
, NULL
, 'z'},
851 int c
= getopt_long(argc
, argv
, "+:ac:C:d:De:f:g:hln:m:p:r:s:Sx:zMU",
852 long_options
, &option_index
);
857 case 'a': system_wide
= 1; break;
858 case 'c': default_interval
= atoi(optarg
); break;
860 /* CPU and PID are mutually exclusive */
862 printf("WARNING: CPU switch overriding PID\n");
866 profile_cpu
= atoi(optarg
); break;
867 case 'd': delay_secs
= atoi(optarg
); break;
868 case 'D': dump_symtab
= 1; break;
870 case 'e': error
= parse_events(optarg
); break;
872 case 'f': count_filter
= atoi(optarg
); break;
873 case 'g': group
= atoi(optarg
); break;
874 case 'h': display_help(); break;
875 case 'l': scale
= 1; break;
876 case 'n': nmi
= atoi(optarg
); break;
878 /* CPU and PID are mutually exclusive */
879 if (profile_cpu
!= -1) {
880 printf("WARNING: PID switch overriding CPU\n");
884 tid
= atoi(optarg
); break;
885 case 'r': realtime_prio
= atoi(optarg
); break;
886 case 's': sym_filter
= strdup(optarg
); break;
887 case 'x': vmlinux
= strdup(optarg
); break;
888 case 'z': zero
= 1; break;
889 case 'm': mmap_pages
= atoi(optarg
); break;
890 case 'M': use_mmap
= 1; break;
891 case 'U': use_munmap
= 1; break;
892 default: error
= 1; break;
903 for (counter
= 0; counter
< nr_counters
; counter
++) {
904 if (event_count
[counter
])
907 event_count
[counter
] = default_interval
;
918 static unsigned int mmap_read_head(struct mmap_data
*md
)
920 struct perf_counter_mmap_page
*pc
= md
->base
;
923 head
= pc
->data_head
;
929 struct timeval last_read
, this_read
;
931 static void mmap_read(struct mmap_data
*md
)
933 unsigned int head
= mmap_read_head(md
);
934 unsigned int old
= md
->prev
;
935 unsigned char *data
= md
->base
+ page_size
;
938 gettimeofday(&this_read
, NULL
);
941 * If we're further behind than half the buffer, there's a chance
942 * the writer will bite our tail and screw up the events under us.
944 * If we somehow ended up ahead of the head, we got messed up.
946 * In either case, truncate and restart at head.
949 if (diff
> md
->mask
/ 2 || diff
< 0) {
953 timersub(&this_read
, &last_read
, &iv
);
954 msecs
= iv
.tv_sec
*1000 + iv
.tv_usec
/1000;
956 fprintf(stderr
, "WARNING: failed to keep up with mmap data."
957 " Last read %lu msecs ago.\n", msecs
);
960 * head points to a known good entry, start there.
965 last_read
= this_read
;
967 for (; old
!= head
;) {
969 struct perf_event_header header
;
974 struct perf_event_header header
;
979 char filename
[PATH_MAX
];
982 typedef union event_union
{
983 struct perf_event_header header
;
985 struct mmap_event mmap
;
988 event_t
*event
= (event_t
*)&data
[old
& md
->mask
];
992 size_t size
= event
->header
.size
;
995 * Event straddles the mmap boundary -- header should always
996 * be inside due to u64 alignment of output.
998 if ((old
& md
->mask
) + size
!= ((old
+ size
) & md
->mask
)) {
999 unsigned int offset
= old
;
1000 unsigned int len
= min(sizeof(*event
), size
), cpy
;
1001 void *dst
= &event_copy
;
1004 cpy
= min(md
->mask
+ 1 - (offset
& md
->mask
), len
);
1005 memcpy(dst
, &data
[offset
& md
->mask
], cpy
);
1011 event
= &event_copy
;
1016 if (event
->header
.misc
& PERF_EVENT_MISC_OVERFLOW
) {
1017 if (event
->header
.type
& PERF_RECORD_IP
)
1018 process_event(event
->ip
.ip
, md
->counter
);
1020 switch (event
->header
.type
) {
1021 case PERF_EVENT_MMAP
:
1022 case PERF_EVENT_MUNMAP
:
1023 printf("%s: %Lu %Lu %Lu %s\n",
1024 event
->header
.type
== PERF_EVENT_MMAP
1025 ? "mmap" : "munmap",
1029 event
->mmap
.filename
);
1038 int cmd_top(int argc
, char **argv
, const char *prefix
)
1040 struct pollfd event_array
[MAX_NR_CPUS
* MAX_COUNTERS
];
1041 struct mmap_data mmap_array
[MAX_NR_CPUS
][MAX_COUNTERS
];
1042 struct perf_counter_hw_event hw_event
;
1044 int i
, counter
, group_fd
, nr_poll
= 0;
1048 page_size
= sysconf(_SC_PAGE_SIZE
);
1050 process_options(argc
, argv
);
1052 nr_cpus
= sysconf(_SC_NPROCESSORS_ONLN
);
1053 assert(nr_cpus
<= MAX_NR_CPUS
);
1054 assert(nr_cpus
>= 0);
1056 if (tid
!= -1 || profile_cpu
!= -1)
1060 if (vmlinux
&& sym_filter_entry
)
1061 parse_vmlinux(vmlinux
);
1063 for (i
= 0; i
< nr_cpus
; i
++) {
1065 for (counter
= 0; counter
< nr_counters
; counter
++) {
1068 if (tid
== -1 && profile_cpu
== -1)
1071 memset(&hw_event
, 0, sizeof(hw_event
));
1072 hw_event
.config
= event_id
[counter
];
1073 hw_event
.irq_period
= event_count
[counter
];
1074 hw_event
.record_type
= PERF_RECORD_IP
| PERF_RECORD_TID
;
1076 hw_event
.mmap
= use_mmap
;
1077 hw_event
.munmap
= use_munmap
;
1079 fd
[i
][counter
] = sys_perf_counter_open(&hw_event
, tid
, cpu
, group_fd
, 0);
1080 if (fd
[i
][counter
] < 0) {
1082 printf("kerneltop error: syscall returned with %d (%s)\n",
1083 fd
[i
][counter
], strerror(err
));
1085 printf("Are you root?\n");
1088 assert(fd
[i
][counter
] >= 0);
1089 fcntl(fd
[i
][counter
], F_SETFL
, O_NONBLOCK
);
1092 * First counter acts as the group leader:
1094 if (group
&& group_fd
== -1)
1095 group_fd
= fd
[i
][counter
];
1097 event_array
[nr_poll
].fd
= fd
[i
][counter
];
1098 event_array
[nr_poll
].events
= POLLIN
;
1101 mmap_array
[i
][counter
].counter
= counter
;
1102 mmap_array
[i
][counter
].prev
= 0;
1103 mmap_array
[i
][counter
].mask
= mmap_pages
*page_size
- 1;
1104 mmap_array
[i
][counter
].base
= mmap(NULL
, (mmap_pages
+1)*page_size
,
1105 PROT_READ
, MAP_SHARED
, fd
[i
][counter
], 0);
1106 if (mmap_array
[i
][counter
].base
== MAP_FAILED
) {
1107 printf("kerneltop error: failed to mmap with %d (%s)\n",
1108 errno
, strerror(errno
));
1114 if (pthread_create(&thread
, NULL
, display_thread
, NULL
)) {
1115 printf("Could not create display thread.\n");
1119 if (realtime_prio
) {
1120 struct sched_param param
;
1122 param
.sched_priority
= realtime_prio
;
1123 if (sched_setscheduler(0, SCHED_FIFO
, ¶m
)) {
1124 printf("Could not set realtime priority.\n");
1132 for (i
= 0; i
< nr_cpus
; i
++) {
1133 for (counter
= 0; counter
< nr_counters
; counter
++)
1134 mmap_read(&mmap_array
[i
][counter
]);
1138 ret
= poll(event_array
, nr_poll
, 100);