blob: 20e5b12009594b811d7f2c7ad171cbdc3344c326 [file] [log] [blame]
Ingo Molnar07800602009-04-20 15:00:56 +02001/*
Ingo Molnarbf9e1872009-06-02 23:37:05 +02002 * builtin-top.c
3 *
4 * Builtin top command: Display a continuously updated profile of
5 * any workload, CPU or specific PID.
6 *
7 * Copyright (C) 2008, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
8 *
9 * Improvements and fixes by:
10 *
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>
16 *
17 * Released under the GPL v2. (and only v2, not any later version)
Ingo Molnar07800602009-04-20 15:00:56 +020018 */
Ingo Molnarbf9e1872009-06-02 23:37:05 +020019#include "builtin.h"
Ingo Molnar07800602009-04-20 15:00:56 +020020
Peter Zijlstra1a482f32009-05-23 18:28:58 +020021#include "perf.h"
Ingo Molnarbf9e1872009-06-02 23:37:05 +020022
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -030023#include "util/symbol.h"
Ingo Molnar8fc03212009-06-04 15:19:47 +020024#include "util/color.h"
Ingo Molnar148be2c2009-04-27 08:02:14 +020025#include "util/util.h"
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -030026#include "util/rbtree.h"
Ingo Molnarb456bae2009-05-26 09:17:18 +020027#include "util/parse-options.h"
28#include "util/parse-events.h"
Ingo Molnar07800602009-04-20 15:00:56 +020029
Ingo Molnar07800602009-04-20 15:00:56 +020030#include <assert.h>
31#include <fcntl.h>
Ingo Molnar0e9b20b2009-05-26 09:17:18 +020032
Ingo Molnar07800602009-04-20 15:00:56 +020033#include <stdio.h>
Ingo Molnar0e9b20b2009-05-26 09:17:18 +020034
Ingo Molnar07800602009-04-20 15:00:56 +020035#include <errno.h>
Ingo Molnar07800602009-04-20 15:00:56 +020036#include <time.h>
37#include <sched.h>
38#include <pthread.h>
39
40#include <sys/syscall.h>
41#include <sys/ioctl.h>
42#include <sys/poll.h>
43#include <sys/prctl.h>
44#include <sys/wait.h>
45#include <sys/uio.h>
46#include <sys/mman.h>
47
48#include <linux/unistd.h>
49#include <linux/types.h>
50
Ingo Molnar07800602009-04-20 15:00:56 +020051static int system_wide = 0;
52
Ingo Molnarb456bae2009-05-26 09:17:18 +020053static __u64 default_event_id[MAX_COUNTERS] = {
Ingo Molnar07800602009-04-20 15:00:56 +020054 EID(PERF_TYPE_SOFTWARE, PERF_COUNT_TASK_CLOCK),
55 EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CONTEXT_SWITCHES),
56 EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CPU_MIGRATIONS),
57 EID(PERF_TYPE_SOFTWARE, PERF_COUNT_PAGE_FAULTS),
58
59 EID(PERF_TYPE_HARDWARE, PERF_COUNT_CPU_CYCLES),
60 EID(PERF_TYPE_HARDWARE, PERF_COUNT_INSTRUCTIONS),
61 EID(PERF_TYPE_HARDWARE, PERF_COUNT_CACHE_REFERENCES),
62 EID(PERF_TYPE_HARDWARE, PERF_COUNT_CACHE_MISSES),
63};
64static int default_interval = 100000;
65static int event_count[MAX_COUNTERS];
66static int fd[MAX_NR_CPUS][MAX_COUNTERS];
67
Ingo Molnar6e53cdf2009-06-04 08:53:05 +020068static __u64 count_filter = 5;
69static int print_entries = 15;
Ingo Molnar07800602009-04-20 15:00:56 +020070
Ingo Molnar6e53cdf2009-06-04 08:53:05 +020071static int target_pid = -1;
Ingo Molnar07800602009-04-20 15:00:56 +020072static int profile_cpu = -1;
73static int nr_cpus = 0;
Ingo Molnar07800602009-04-20 15:00:56 +020074static unsigned int realtime_prio = 0;
75static int group = 0;
76static unsigned int page_size;
77static unsigned int mmap_pages = 16;
78static int use_mmap = 0;
79static int use_munmap = 0;
Peter Zijlstraf5456a62009-05-15 15:19:29 +020080static int freq = 0;
Ingo Molnar07800602009-04-20 15:00:56 +020081
Ingo Molnar07800602009-04-20 15:00:56 +020082static char *sym_filter;
83static unsigned long filter_start;
84static unsigned long filter_end;
85
86static int delay_secs = 2;
87static int zero;
88static int dump_symtab;
89
Ingo Molnarddcacfa2009-04-20 15:37:32 +020090static const unsigned int default_count[] = {
Ingo Molnar07800602009-04-20 15:00:56 +020091 1000000,
92 1000000,
93 10000,
94 10000,
95 1000000,
96 10000,
97};
98
Ingo Molnar07800602009-04-20 15:00:56 +020099/*
100 * Symbols
101 */
102
103static uint64_t min_ip;
104static uint64_t max_ip = -1ll;
105
106struct sym_entry {
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300107 struct rb_node rb_node;
108 struct list_head node;
Ingo Molnar07800602009-04-20 15:00:56 +0200109 unsigned long count[MAX_COUNTERS];
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300110 unsigned long snap_count;
111 double weight;
Ingo Molnar07800602009-04-20 15:00:56 +0200112 int skip;
Ingo Molnar07800602009-04-20 15:00:56 +0200113};
114
Ingo Molnar07800602009-04-20 15:00:56 +0200115struct sym_entry *sym_filter_entry;
116
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300117struct dso *kernel_dso;
118
119/*
120 * Symbols will be added here in record_ip and will get out
121 * after decayed.
122 */
123static LIST_HEAD(active_symbols);
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300124static pthread_mutex_t active_symbols_lock = PTHREAD_MUTEX_INITIALIZER;
Ingo Molnar07800602009-04-20 15:00:56 +0200125
Ingo Molnar07800602009-04-20 15:00:56 +0200126/*
127 * Ordering weight: count-1 * count-2 * ... / count-n
128 */
129static double sym_weight(const struct sym_entry *sym)
130{
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300131 double weight = sym->snap_count;
Ingo Molnar07800602009-04-20 15:00:56 +0200132 int counter;
133
Ingo Molnar07800602009-04-20 15:00:56 +0200134 for (counter = 1; counter < nr_counters-1; counter++)
135 weight *= sym->count[counter];
136
137 weight /= (sym->count[counter] + 1);
138
139 return weight;
140}
141
Ingo Molnar07800602009-04-20 15:00:56 +0200142static long events;
143static long userspace_events;
144static const char CONSOLE_CLEAR[] = "";
145
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300146static void __list_insert_active_sym(struct sym_entry *syme)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300147{
148 list_add(&syme->node, &active_symbols);
149}
150
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300151static void list_remove_active_sym(struct sym_entry *syme)
152{
153 pthread_mutex_lock(&active_symbols_lock);
154 list_del_init(&syme->node);
155 pthread_mutex_unlock(&active_symbols_lock);
156}
157
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300158static void rb_insert_active_sym(struct rb_root *tree, struct sym_entry *se)
159{
160 struct rb_node **p = &tree->rb_node;
161 struct rb_node *parent = NULL;
162 struct sym_entry *iter;
163
164 while (*p != NULL) {
165 parent = *p;
166 iter = rb_entry(parent, struct sym_entry, rb_node);
167
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300168 if (se->weight > iter->weight)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300169 p = &(*p)->rb_left;
170 else
171 p = &(*p)->rb_right;
172 }
173
174 rb_link_node(&se->rb_node, parent, p);
175 rb_insert_color(&se->rb_node, tree);
176}
Ingo Molnar07800602009-04-20 15:00:56 +0200177
178static void print_sym_table(void)
179{
Ingo Molnar233f0b92009-06-03 21:48:40 +0200180 int printed = 0, j;
Ingo Molnar07800602009-04-20 15:00:56 +0200181 int counter;
182 float events_per_sec = events/delay_secs;
183 float kevents_per_sec = (events-userspace_events)/delay_secs;
184 float sum_kevents = 0.0;
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300185 struct sym_entry *syme, *n;
186 struct rb_root tmp = RB_ROOT;
187 struct rb_node *nd;
Ingo Molnar07800602009-04-20 15:00:56 +0200188
189 events = userspace_events = 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200190
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300191 /* Sort the active symbols */
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300192 pthread_mutex_lock(&active_symbols_lock);
193 syme = list_entry(active_symbols.next, struct sym_entry, node);
194 pthread_mutex_unlock(&active_symbols_lock);
195
196 list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
197 syme->snap_count = syme->count[0];
198 if (syme->snap_count != 0) {
199 syme->weight = sym_weight(syme);
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300200 rb_insert_active_sym(&tmp, syme);
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300201 sum_kevents += syme->snap_count;
Mike Galbraithd94b9432009-05-25 09:57:56 +0200202
203 for (j = 0; j < nr_counters; j++)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300204 syme->count[j] = zero ? 0 : syme->count[j] * 7 / 8;
205 } else
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300206 list_remove_active_sym(syme);
Mike Galbraithd94b9432009-05-25 09:57:56 +0200207 }
208
Ingo Molnar07800602009-04-20 15:00:56 +0200209 write(1, CONSOLE_CLEAR, strlen(CONSOLE_CLEAR));
210
211 printf(
212"------------------------------------------------------------------------------\n");
Ingo Molnarf2521b62009-06-03 19:17:25 +0200213 printf( " PerfTop:%8.0f irqs/sec kernel:%4.1f%% [",
Ingo Molnar07800602009-04-20 15:00:56 +0200214 events_per_sec,
Mike Galbraithf91183f2009-05-26 15:25:34 +0200215 100.0 - (100.0*((events_per_sec-kevents_per_sec)/events_per_sec)));
Ingo Molnar07800602009-04-20 15:00:56 +0200216
217 if (nr_counters == 1)
218 printf("%d ", event_count[0]);
219
220 for (counter = 0; counter < nr_counters; counter++) {
221 if (counter)
222 printf("/");
223
224 printf("%s", event_name(counter));
225 }
226
227 printf( "], ");
228
Ingo Molnarb456bae2009-05-26 09:17:18 +0200229 if (target_pid != -1)
230 printf(" (target_pid: %d", target_pid);
Ingo Molnar07800602009-04-20 15:00:56 +0200231 else
232 printf(" (all");
233
234 if (profile_cpu != -1)
235 printf(", cpu: %d)\n", profile_cpu);
236 else {
Ingo Molnarb456bae2009-05-26 09:17:18 +0200237 if (target_pid != -1)
Ingo Molnar07800602009-04-20 15:00:56 +0200238 printf(")\n");
239 else
240 printf(", %d CPUs)\n", nr_cpus);
241 }
242
243 printf("------------------------------------------------------------------------------\n\n");
244
245 if (nr_counters == 1)
246 printf(" events pcnt");
247 else
248 printf(" weight events pcnt");
249
250 printf(" RIP kernel function\n"
251 " ______ ______ _____ ________________ _______________\n\n"
252 );
253
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300254 for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
255 struct sym_entry *syme = rb_entry(nd, struct sym_entry, rb_node);
256 struct symbol *sym = (struct symbol *)(syme + 1);
Ingo Molnar8fc03212009-06-04 15:19:47 +0200257 char *color = PERF_COLOR_NORMAL;
258 double pcnt;
Ingo Molnar07800602009-04-20 15:00:56 +0200259
Ingo Molnar6e53cdf2009-06-04 08:53:05 +0200260 if (++printed > print_entries || syme->snap_count < count_filter)
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300261 continue;
Ingo Molnar07800602009-04-20 15:00:56 +0200262
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300263 pcnt = 100.0 - (100.0 * ((sum_kevents - syme->snap_count) /
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300264 sum_kevents));
Mike Galbraithd94b9432009-05-25 09:57:56 +0200265
Ingo Molnar8fc03212009-06-04 15:19:47 +0200266 /*
267 * We color high-overhead entries in red, low-overhead
268 * entries in green - and keep the middle ground normal:
269 */
270 if (pcnt >= 5.0)
271 color = PERF_COLOR_RED;
272 if (pcnt < 0.5)
273 color = PERF_COLOR_GREEN;
274
Mike Galbraithd94b9432009-05-25 09:57:56 +0200275 if (nr_counters == 1)
Ingo Molnar8fc03212009-06-04 15:19:47 +0200276 printf("%19.2f - ", syme->weight);
Mike Galbraithd94b9432009-05-25 09:57:56 +0200277 else
Ingo Molnar8fc03212009-06-04 15:19:47 +0200278 printf("%8.1f %10ld - ", syme->weight, syme->snap_count);
279
280 color_fprintf(stdout, color, "%4.1f%%", pcnt);
281 printf(" - %016llx : %s\n", sym->start, sym->name);
Ingo Molnar07800602009-04-20 15:00:56 +0200282 }
283
Ingo Molnar07800602009-04-20 15:00:56 +0200284 {
285 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
286
287 if (poll(&stdin_poll, 1, 0) == 1) {
288 printf("key pressed - exiting.\n");
289 exit(0);
290 }
291 }
292}
293
294static void *display_thread(void *arg)
295{
Ingo Molnarf2521b62009-06-03 19:17:25 +0200296 printf("PerfTop refresh period: %d seconds\n", delay_secs);
Ingo Molnar07800602009-04-20 15:00:56 +0200297
298 while (!sleep(delay_secs))
299 print_sym_table();
300
301 return NULL;
302}
303
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300304static int symbol_filter(struct dso *self, struct symbol *sym)
Ingo Molnar07800602009-04-20 15:00:56 +0200305{
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300306 static int filter_match;
307 struct sym_entry *syme;
308 const char *name = sym->name;
Ingo Molnar07800602009-04-20 15:00:56 +0200309
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300310 if (!strcmp(name, "_text") ||
311 !strcmp(name, "_etext") ||
312 !strcmp(name, "_sinittext") ||
313 !strncmp("init_module", name, 11) ||
314 !strncmp("cleanup_module", name, 14) ||
315 strstr(name, "_text_start") ||
316 strstr(name, "_text_end"))
Ingo Molnar07800602009-04-20 15:00:56 +0200317 return 1;
318
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300319 syme = dso__sym_priv(self, sym);
Ingo Molnar07800602009-04-20 15:00:56 +0200320 /* Tag events to be skipped. */
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300321 if (!strcmp("default_idle", name) ||
322 !strcmp("cpu_idle", name) ||
323 !strcmp("enter_idle", name) ||
324 !strcmp("exit_idle", name) ||
325 !strcmp("mwait_idle", name))
326 syme->skip = 1;
Ingo Molnar07800602009-04-20 15:00:56 +0200327
328 if (filter_match == 1) {
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300329 filter_end = sym->start;
Ingo Molnar07800602009-04-20 15:00:56 +0200330 filter_match = -1;
331 if (filter_end - filter_start > 10000) {
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300332 fprintf(stderr,
333 "hm, too large filter symbol <%s> - skipping.\n",
Ingo Molnar07800602009-04-20 15:00:56 +0200334 sym_filter);
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300335 fprintf(stderr, "symbol filter start: %016lx\n",
336 filter_start);
337 fprintf(stderr, " end: %016lx\n",
338 filter_end);
Ingo Molnar07800602009-04-20 15:00:56 +0200339 filter_end = filter_start = 0;
340 sym_filter = NULL;
341 sleep(1);
342 }
343 }
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300344
345 if (filter_match == 0 && sym_filter && !strcmp(name, sym_filter)) {
Ingo Molnar07800602009-04-20 15:00:56 +0200346 filter_match = 1;
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300347 filter_start = sym->start;
Ingo Molnar07800602009-04-20 15:00:56 +0200348 }
349
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300350
Ingo Molnar07800602009-04-20 15:00:56 +0200351 return 0;
352}
353
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300354static int parse_symbols(void)
Ingo Molnar07800602009-04-20 15:00:56 +0200355{
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300356 struct rb_node *node;
357 struct symbol *sym;
Ingo Molnar07800602009-04-20 15:00:56 +0200358
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300359 kernel_dso = dso__new("[kernel]", sizeof(struct sym_entry));
360 if (kernel_dso == NULL)
361 return -1;
Ingo Molnar07800602009-04-20 15:00:56 +0200362
Ingo Molnarbd741372009-06-04 14:13:04 +0200363 if (dso__load_kernel(kernel_dso, NULL, symbol_filter, 1) != 0)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300364 goto out_delete_dso;
Ingo Molnar07800602009-04-20 15:00:56 +0200365
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300366 node = rb_first(&kernel_dso->syms);
367 sym = rb_entry(node, struct symbol, rb_node);
368 min_ip = sym->start;
Ingo Molnar07800602009-04-20 15:00:56 +0200369
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300370 node = rb_last(&kernel_dso->syms);
371 sym = rb_entry(node, struct symbol, rb_node);
Mike Galbraithda417a72009-05-29 08:23:16 +0200372 max_ip = sym->end;
Ingo Molnar07800602009-04-20 15:00:56 +0200373
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300374 if (dump_symtab)
Mike Galbraitha3ec8d72009-05-29 06:46:46 +0200375 dso__fprintf(kernel_dso, stderr);
Ingo Molnar07800602009-04-20 15:00:56 +0200376
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300377 return 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200378
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300379out_delete_dso:
380 dso__delete(kernel_dso);
381 kernel_dso = NULL;
382 return -1;
Ingo Molnar07800602009-04-20 15:00:56 +0200383}
384
Ingo Molnar07800602009-04-20 15:00:56 +0200385#define TRACE_COUNT 3
386
Ingo Molnar07800602009-04-20 15:00:56 +0200387/*
388 * Binary search in the histogram table and record the hit:
389 */
390static void record_ip(uint64_t ip, int counter)
391{
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300392 struct symbol *sym = dso__find_symbol(kernel_dso, ip);
Ingo Molnar07800602009-04-20 15:00:56 +0200393
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300394 if (sym != NULL) {
395 struct sym_entry *syme = dso__sym_priv(kernel_dso, sym);
Ingo Molnar07800602009-04-20 15:00:56 +0200396
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300397 if (!syme->skip) {
398 syme->count[counter]++;
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300399 pthread_mutex_lock(&active_symbols_lock);
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300400 if (list_empty(&syme->node) || !syme->node.next)
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300401 __list_insert_active_sym(syme);
402 pthread_mutex_unlock(&active_symbols_lock);
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300403 return;
Ingo Molnar07800602009-04-20 15:00:56 +0200404 }
Ingo Molnar07800602009-04-20 15:00:56 +0200405 }
406
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300407 events--;
Ingo Molnar07800602009-04-20 15:00:56 +0200408}
409
410static void process_event(uint64_t ip, int counter)
411{
412 events++;
413
414 if (ip < min_ip || ip > max_ip) {
415 userspace_events++;
416 return;
417 }
418
419 record_ip(ip, counter);
420}
421
Ingo Molnar07800602009-04-20 15:00:56 +0200422struct mmap_data {
423 int counter;
424 void *base;
425 unsigned int mask;
426 unsigned int prev;
427};
428
429static unsigned int mmap_read_head(struct mmap_data *md)
430{
431 struct perf_counter_mmap_page *pc = md->base;
432 int head;
433
434 head = pc->data_head;
435 rmb();
436
437 return head;
438}
439
440struct timeval last_read, this_read;
441
442static void mmap_read(struct mmap_data *md)
443{
444 unsigned int head = mmap_read_head(md);
445 unsigned int old = md->prev;
446 unsigned char *data = md->base + page_size;
447 int diff;
448
449 gettimeofday(&this_read, NULL);
450
451 /*
452 * If we're further behind than half the buffer, there's a chance
453 * the writer will bite our tail and screw up the events under us.
454 *
455 * If we somehow ended up ahead of the head, we got messed up.
456 *
457 * In either case, truncate and restart at head.
458 */
459 diff = head - old;
460 if (diff > md->mask / 2 || diff < 0) {
461 struct timeval iv;
462 unsigned long msecs;
463
464 timersub(&this_read, &last_read, &iv);
465 msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
466
467 fprintf(stderr, "WARNING: failed to keep up with mmap data."
468 " Last read %lu msecs ago.\n", msecs);
469
470 /*
471 * head points to a known good entry, start there.
472 */
473 old = head;
474 }
475
476 last_read = this_read;
477
478 for (; old != head;) {
479 struct ip_event {
480 struct perf_event_header header;
481 __u64 ip;
Ingo Molnarb456bae2009-05-26 09:17:18 +0200482 __u32 pid, target_pid;
Ingo Molnar07800602009-04-20 15:00:56 +0200483 };
484 struct mmap_event {
485 struct perf_event_header header;
Ingo Molnarb456bae2009-05-26 09:17:18 +0200486 __u32 pid, target_pid;
Ingo Molnar07800602009-04-20 15:00:56 +0200487 __u64 start;
488 __u64 len;
489 __u64 pgoff;
490 char filename[PATH_MAX];
491 };
492
493 typedef union event_union {
494 struct perf_event_header header;
495 struct ip_event ip;
496 struct mmap_event mmap;
497 } event_t;
498
499 event_t *event = (event_t *)&data[old & md->mask];
500
501 event_t event_copy;
502
Ingo Molnar6f06ccb2009-04-20 15:22:22 +0200503 size_t size = event->header.size;
Ingo Molnar07800602009-04-20 15:00:56 +0200504
505 /*
506 * Event straddles the mmap boundary -- header should always
507 * be inside due to u64 alignment of output.
508 */
509 if ((old & md->mask) + size != ((old + size) & md->mask)) {
510 unsigned int offset = old;
511 unsigned int len = min(sizeof(*event), size), cpy;
512 void *dst = &event_copy;
513
514 do {
515 cpy = min(md->mask + 1 - (offset & md->mask), len);
516 memcpy(dst, &data[offset & md->mask], cpy);
517 offset += cpy;
518 dst += cpy;
519 len -= cpy;
520 } while (len);
521
522 event = &event_copy;
523 }
524
525 old += size;
526
527 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) {
Peter Zijlstrac70975b2009-06-02 17:38:21 +0200528 if (event->header.type & PERF_SAMPLE_IP)
Ingo Molnar07800602009-04-20 15:00:56 +0200529 process_event(event->ip.ip, md->counter);
530 } else {
531 switch (event->header.type) {
532 case PERF_EVENT_MMAP:
533 case PERF_EVENT_MUNMAP:
534 printf("%s: %Lu %Lu %Lu %s\n",
535 event->header.type == PERF_EVENT_MMAP
536 ? "mmap" : "munmap",
537 event->mmap.start,
538 event->mmap.len,
539 event->mmap.pgoff,
540 event->mmap.filename);
541 break;
542 }
543 }
544 }
545
546 md->prev = old;
547}
548
Mike Galbraithc2990a22009-05-24 08:35:49 +0200549static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
550static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
551
Ingo Molnarb456bae2009-05-26 09:17:18 +0200552static int __cmd_top(void)
Ingo Molnar07800602009-04-20 15:00:56 +0200553{
Peter Zijlstrac70975b2009-06-02 17:38:21 +0200554 struct perf_counter_attr attr;
Ingo Molnar07800602009-04-20 15:00:56 +0200555 pthread_t thread;
556 int i, counter, group_fd, nr_poll = 0;
557 unsigned int cpu;
558 int ret;
559
Ingo Molnar07800602009-04-20 15:00:56 +0200560 for (i = 0; i < nr_cpus; i++) {
561 group_fd = -1;
562 for (counter = 0; counter < nr_counters; counter++) {
563
564 cpu = profile_cpu;
Ingo Molnarb456bae2009-05-26 09:17:18 +0200565 if (target_pid == -1 && profile_cpu == -1)
Ingo Molnar07800602009-04-20 15:00:56 +0200566 cpu = i;
567
Peter Zijlstrac70975b2009-06-02 17:38:21 +0200568 memset(&attr, 0, sizeof(attr));
569 attr.config = event_id[counter];
570 attr.sample_period = event_count[counter];
571 attr.sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
572 attr.mmap = use_mmap;
573 attr.munmap = use_munmap;
574 attr.freq = freq;
Ingo Molnar07800602009-04-20 15:00:56 +0200575
Peter Zijlstrac70975b2009-06-02 17:38:21 +0200576 fd[i][counter] = sys_perf_counter_open(&attr, target_pid, cpu, group_fd, 0);
Ingo Molnar07800602009-04-20 15:00:56 +0200577 if (fd[i][counter] < 0) {
578 int err = errno;
Ingo Molnarf2521b62009-06-03 19:17:25 +0200579
580 error("syscall returned with %d (%s)\n",
Ingo Molnar07800602009-04-20 15:00:56 +0200581 fd[i][counter], strerror(err));
582 if (err == EPERM)
583 printf("Are you root?\n");
584 exit(-1);
585 }
586 assert(fd[i][counter] >= 0);
587 fcntl(fd[i][counter], F_SETFL, O_NONBLOCK);
588
589 /*
590 * First counter acts as the group leader:
591 */
592 if (group && group_fd == -1)
593 group_fd = fd[i][counter];
594
595 event_array[nr_poll].fd = fd[i][counter];
596 event_array[nr_poll].events = POLLIN;
597 nr_poll++;
598
599 mmap_array[i][counter].counter = counter;
600 mmap_array[i][counter].prev = 0;
601 mmap_array[i][counter].mask = mmap_pages*page_size - 1;
602 mmap_array[i][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
603 PROT_READ, MAP_SHARED, fd[i][counter], 0);
Ingo Molnarf2521b62009-06-03 19:17:25 +0200604 if (mmap_array[i][counter].base == MAP_FAILED)
605 die("failed to mmap with %d (%s)\n", errno, strerror(errno));
Ingo Molnar07800602009-04-20 15:00:56 +0200606 }
607 }
608
609 if (pthread_create(&thread, NULL, display_thread, NULL)) {
610 printf("Could not create display thread.\n");
611 exit(-1);
612 }
613
614 if (realtime_prio) {
615 struct sched_param param;
616
617 param.sched_priority = realtime_prio;
618 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
619 printf("Could not set realtime priority.\n");
620 exit(-1);
621 }
622 }
623
624 while (1) {
625 int hits = events;
626
627 for (i = 0; i < nr_cpus; i++) {
628 for (counter = 0; counter < nr_counters; counter++)
629 mmap_read(&mmap_array[i][counter]);
630 }
631
632 if (hits == events)
633 ret = poll(event_array, nr_poll, 100);
634 }
635
636 return 0;
637}
Ingo Molnarb456bae2009-05-26 09:17:18 +0200638
639static const char * const top_usage[] = {
640 "perf top [<options>]",
641 NULL
642};
643
644static char events_help_msg[EVENTS_HELP_MAX];
645
646static const struct option options[] = {
647 OPT_CALLBACK('e', "event", NULL, "event",
648 events_help_msg, parse_events),
649 OPT_INTEGER('c', "count", &default_interval,
650 "event period to sample"),
651 OPT_INTEGER('p', "pid", &target_pid,
652 "profile events on existing pid"),
653 OPT_BOOLEAN('a', "all-cpus", &system_wide,
654 "system-wide collection from all CPUs"),
655 OPT_INTEGER('C', "CPU", &profile_cpu,
656 "CPU to profile on"),
657 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
658 "number of mmap data pages"),
659 OPT_INTEGER('r', "realtime", &realtime_prio,
660 "collect data with this RT SCHED_FIFO priority"),
Mike Galbraithdb20c002009-05-26 15:25:34 +0200661 OPT_INTEGER('d', "delay", &delay_secs,
Ingo Molnarb456bae2009-05-26 09:17:18 +0200662 "number of seconds to delay between refreshes"),
663 OPT_BOOLEAN('D', "dump-symtab", &dump_symtab,
664 "dump the symbol table used for profiling"),
Ingo Molnar6e53cdf2009-06-04 08:53:05 +0200665 OPT_INTEGER('f', "count-filter", &count_filter,
Ingo Molnarb456bae2009-05-26 09:17:18 +0200666 "only display functions with more events than this"),
667 OPT_BOOLEAN('g', "group", &group,
668 "put the counters into a counter group"),
669 OPT_STRING('s', "sym-filter", &sym_filter, "pattern",
670 "only display symbols matchig this pattern"),
671 OPT_BOOLEAN('z', "zero", &group,
672 "zero history across updates"),
673 OPT_BOOLEAN('M', "use-mmap", &use_mmap,
674 "track mmap events"),
675 OPT_BOOLEAN('U', "use-munmap", &use_munmap,
676 "track munmap events"),
Ingo Molnar6e53cdf2009-06-04 08:53:05 +0200677 OPT_INTEGER('F', "freq", &freq,
Ingo Molnarb456bae2009-05-26 09:17:18 +0200678 "profile at this frequency"),
Ingo Molnar6e53cdf2009-06-04 08:53:05 +0200679 OPT_INTEGER('E', "entries", &print_entries,
680 "display this many functions"),
Ingo Molnarb456bae2009-05-26 09:17:18 +0200681 OPT_END()
682};
683
684int cmd_top(int argc, const char **argv, const char *prefix)
685{
686 int counter;
687
688 page_size = sysconf(_SC_PAGE_SIZE);
689
690 create_events_help(events_help_msg);
691 memcpy(event_id, default_event_id, sizeof(default_event_id));
692
693 argc = parse_options(argc, argv, options, top_usage, 0);
694 if (argc)
695 usage_with_options(top_usage, options);
696
697 if (freq) {
698 default_interval = freq;
699 freq = 1;
700 }
701
702 /* CPU and PID are mutually exclusive */
703 if (target_pid != -1 && profile_cpu != -1) {
704 printf("WARNING: PID switch overriding CPU\n");
705 sleep(1);
706 profile_cpu = -1;
707 }
708
709 if (!nr_counters) {
710 nr_counters = 1;
711 event_id[0] = 0;
712 }
713
714 for (counter = 0; counter < nr_counters; counter++) {
715 if (event_count[counter])
716 continue;
717
718 event_count[counter] = default_interval;
719 }
720
721 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
722 assert(nr_cpus <= MAX_NR_CPUS);
723 assert(nr_cpus >= 0);
724
725 if (target_pid != -1 || profile_cpu != -1)
726 nr_cpus = 1;
727
728 parse_symbols();
729
730 return __cmd_top();
731}