blob: 4eef3465e837bd95fa5fc8cd30b2ede3acc37de5 [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 Melo43cbcd82009-07-01 12:28:37 -030026#include <linux/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>
Mike Galbraith923c42c2009-07-22 20:36:03 +020034#include <termios.h>
35#include <unistd.h>
Ingo Molnar0e9b20b2009-05-26 09:17:18 +020036
Ingo Molnar07800602009-04-20 15:00:56 +020037#include <errno.h>
Ingo Molnar07800602009-04-20 15:00:56 +020038#include <time.h>
39#include <sched.h>
40#include <pthread.h>
41
42#include <sys/syscall.h>
43#include <sys/ioctl.h>
44#include <sys/poll.h>
45#include <sys/prctl.h>
46#include <sys/wait.h>
47#include <sys/uio.h>
48#include <sys/mman.h>
49
50#include <linux/unistd.h>
51#include <linux/types.h>
52
Ingo Molnara21ca2c2009-06-06 09:58:57 +020053static int fd[MAX_NR_CPUS][MAX_COUNTERS];
54
Ingo Molnar07800602009-04-20 15:00:56 +020055static int system_wide = 0;
56
Ingo Molnara21ca2c2009-06-06 09:58:57 +020057static int default_interval = 100000;
Ingo Molnar07800602009-04-20 15:00:56 +020058
Mike Galbraith923c42c2009-07-22 20:36:03 +020059static int count_filter = 5;
Ingo Molnar6e53cdf2009-06-04 08:53:05 +020060static int print_entries = 15;
Ingo Molnar07800602009-04-20 15:00:56 +020061
Ingo Molnar6e53cdf2009-06-04 08:53:05 +020062static int target_pid = -1;
Mike Galbraith0fdc7e62009-07-21 10:30:36 +020063static int inherit = 0;
Ingo Molnar07800602009-04-20 15:00:56 +020064static int profile_cpu = -1;
65static int nr_cpus = 0;
Ingo Molnar07800602009-04-20 15:00:56 +020066static unsigned int realtime_prio = 0;
67static int group = 0;
68static unsigned int page_size;
Ingo Molnarcf1f4572009-06-05 13:27:02 +020069static unsigned int mmap_pages = 16;
70static int freq = 0;
Ingo Molnar3da297a2009-06-07 17:39:02 +020071static int verbose = 0;
Mike Galbraith42976482009-07-02 08:09:46 +020072static char *vmlinux = NULL;
Ingo Molnar07800602009-04-20 15:00:56 +020073
Ingo Molnar07800602009-04-20 15:00:56 +020074static int delay_secs = 2;
75static int zero;
76static int dump_symtab;
77
Ingo Molnar07800602009-04-20 15:00:56 +020078/*
Mike Galbraith923c42c2009-07-22 20:36:03 +020079 * Source
80 */
81
82struct source_line {
83 u64 eip;
84 unsigned long count[MAX_COUNTERS];
85 char *line;
86 struct source_line *next;
87};
88
89static char *sym_filter = NULL;
90struct sym_entry *sym_filter_entry = NULL;
91static int sym_pcnt_filter = 5;
92static int sym_counter = 0;
Mike Galbraith46ab9762009-07-24 10:09:50 +020093static int display_weighted = -1;
Mike Galbraith923c42c2009-07-22 20:36:03 +020094
95/*
Ingo Molnar07800602009-04-20 15:00:56 +020096 * Symbols
97 */
98
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100099static u64 min_ip;
100static u64 max_ip = -1ll;
Ingo Molnar07800602009-04-20 15:00:56 +0200101
102struct sym_entry {
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300103 struct rb_node rb_node;
104 struct list_head node;
Ingo Molnar07800602009-04-20 15:00:56 +0200105 unsigned long count[MAX_COUNTERS];
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300106 unsigned long snap_count;
107 double weight;
Ingo Molnar07800602009-04-20 15:00:56 +0200108 int skip;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200109 struct source_line *source;
110 struct source_line *lines;
111 struct source_line **lines_tail;
112 pthread_mutex_t source_lock;
Ingo Molnar07800602009-04-20 15:00:56 +0200113};
114
Mike Galbraith923c42c2009-07-22 20:36:03 +0200115/*
116 * Source functions
117 */
118
119static void parse_source(struct sym_entry *syme)
120{
121 struct symbol *sym;
122 struct module *module;
123 struct section *section = NULL;
124 FILE *file;
125 char command[PATH_MAX*2], *path = vmlinux;
126 u64 start, end, len;
127
128 if (!syme)
129 return;
130
131 if (syme->lines) {
132 pthread_mutex_lock(&syme->source_lock);
133 goto out_assign;
134 }
135
136 sym = (struct symbol *)(syme + 1);
137 module = sym->module;
138
139 if (module)
140 path = module->path;
141 if (!path)
142 return;
143
144 start = sym->obj_start;
145 if (!start)
146 start = sym->start;
147
148 if (module) {
149 section = module->sections->find_section(module->sections, ".text");
150 if (section)
151 start -= section->vma;
152 }
153
154 end = start + sym->end - sym->start + 1;
155 len = sym->end - sym->start;
156
157 sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s", start, end, path);
158
159 file = popen(command, "r");
160 if (!file)
161 return;
162
163 pthread_mutex_lock(&syme->source_lock);
164 syme->lines_tail = &syme->lines;
165 while (!feof(file)) {
166 struct source_line *src;
167 size_t dummy = 0;
168 char *c;
169
170 src = malloc(sizeof(struct source_line));
171 assert(src != NULL);
172 memset(src, 0, sizeof(struct source_line));
173
174 if (getline(&src->line, &dummy, file) < 0)
175 break;
176 if (!src->line)
177 break;
178
179 c = strchr(src->line, '\n');
180 if (c)
181 *c = 0;
182
183 src->next = NULL;
184 *syme->lines_tail = src;
185 syme->lines_tail = &src->next;
186
187 if (strlen(src->line)>8 && src->line[8] == ':') {
188 src->eip = strtoull(src->line, NULL, 16);
189 if (section)
190 src->eip += section->vma;
191 }
192 if (strlen(src->line)>8 && src->line[16] == ':') {
193 src->eip = strtoull(src->line, NULL, 16);
194 if (section)
195 src->eip += section->vma;
196 }
197 }
198 pclose(file);
199out_assign:
200 sym_filter_entry = syme;
201 pthread_mutex_unlock(&syme->source_lock);
202}
203
204static void __zero_source_counters(struct sym_entry *syme)
205{
206 int i;
207 struct source_line *line;
208
209 line = syme->lines;
210 while (line) {
211 for (i = 0; i < nr_counters; i++)
212 line->count[i] = 0;
213 line = line->next;
214 }
215}
216
217static void record_precise_ip(struct sym_entry *syme, int counter, u64 ip)
218{
219 struct source_line *line;
220
221 if (syme != sym_filter_entry)
222 return;
223
224 if (pthread_mutex_trylock(&syme->source_lock))
225 return;
226
227 if (!syme->source)
228 goto out_unlock;
229
230 for (line = syme->lines; line; line = line->next) {
231 if (line->eip == ip) {
232 line->count[counter]++;
233 break;
234 }
235 if (line->eip > ip)
236 break;
237 }
238out_unlock:
239 pthread_mutex_unlock(&syme->source_lock);
240}
241
242static void lookup_sym_source(struct sym_entry *syme)
243{
244 struct symbol *symbol = (struct symbol *)(syme + 1);
245 struct source_line *line;
246 char pattern[PATH_MAX];
247 char *idx;
248
249 sprintf(pattern, "<%s>:", symbol->name);
250
251 if (symbol->module) {
252 idx = strstr(pattern, "\t");
253 if (idx)
254 *idx = 0;
255 }
256
257 pthread_mutex_lock(&syme->source_lock);
258 for (line = syme->lines; line; line = line->next) {
259 if (strstr(line->line, pattern)) {
260 syme->source = line;
261 break;
262 }
263 }
264 pthread_mutex_unlock(&syme->source_lock);
265}
266
267static void show_lines(struct source_line *queue, int count, int total)
268{
269 int i;
270 struct source_line *line;
271
272 line = queue;
273 for (i = 0; i < count; i++) {
274 float pcnt = 100.0*(float)line->count[sym_counter]/(float)total;
275
276 printf("%8li %4.1f%%\t%s\n", line->count[sym_counter], pcnt, line->line);
277 line = line->next;
278 }
279}
280
281#define TRACE_COUNT 3
282
283static void show_details(struct sym_entry *syme)
284{
285 struct symbol *symbol;
286 struct source_line *line;
287 struct source_line *line_queue = NULL;
288 int displayed = 0;
289 int line_queue_count = 0, total = 0, more = 0;
290
291 if (!syme)
292 return;
293
294 if (!syme->source)
295 lookup_sym_source(syme);
296
297 if (!syme->source)
298 return;
299
300 symbol = (struct symbol *)(syme + 1);
301 printf("Showing %s for %s\n", event_name(sym_counter), symbol->name);
302 printf(" Events Pcnt (>=%d%%)\n", sym_pcnt_filter);
303
304 pthread_mutex_lock(&syme->source_lock);
305 line = syme->source;
306 while (line) {
307 total += line->count[sym_counter];
308 line = line->next;
309 }
310
311 line = syme->source;
312 while (line) {
313 float pcnt = 0.0;
314
315 if (!line_queue_count)
316 line_queue = line;
317 line_queue_count++;
318
319 if (line->count[sym_counter])
320 pcnt = 100.0 * line->count[sym_counter] / (float)total;
321 if (pcnt >= (float)sym_pcnt_filter) {
322 if (displayed <= print_entries)
323 show_lines(line_queue, line_queue_count, total);
324 else more++;
325 displayed += line_queue_count;
326 line_queue_count = 0;
327 line_queue = NULL;
328 } else if (line_queue_count > TRACE_COUNT) {
329 line_queue = line_queue->next;
330 line_queue_count--;
331 }
332
333 line->count[sym_counter] = zero ? 0 : line->count[sym_counter] * 7 / 8;
334 line = line->next;
335 }
336 pthread_mutex_unlock(&syme->source_lock);
337 if (more)
338 printf("%d lines not displayed, maybe increase display entries [e]\n", more);
339}
Ingo Molnar07800602009-04-20 15:00:56 +0200340
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200341struct dso *kernel_dso;
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300342
343/*
344 * Symbols will be added here in record_ip and will get out
345 * after decayed.
346 */
347static LIST_HEAD(active_symbols);
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300348static pthread_mutex_t active_symbols_lock = PTHREAD_MUTEX_INITIALIZER;
Ingo Molnar07800602009-04-20 15:00:56 +0200349
Ingo Molnar07800602009-04-20 15:00:56 +0200350/*
351 * Ordering weight: count-1 * count-2 * ... / count-n
352 */
353static double sym_weight(const struct sym_entry *sym)
354{
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300355 double weight = sym->snap_count;
Ingo Molnar07800602009-04-20 15:00:56 +0200356 int counter;
357
Mike Galbraith46ab9762009-07-24 10:09:50 +0200358 if (!display_weighted)
359 return weight;
360
Ingo Molnar07800602009-04-20 15:00:56 +0200361 for (counter = 1; counter < nr_counters-1; counter++)
362 weight *= sym->count[counter];
363
364 weight /= (sym->count[counter] + 1);
365
366 return weight;
367}
368
Ingo Molnar2debbc82009-06-05 14:29:10 +0200369static long samples;
370static long userspace_samples;
Ingo Molnar07800602009-04-20 15:00:56 +0200371static const char CONSOLE_CLEAR[] = "";
372
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300373static void __list_insert_active_sym(struct sym_entry *syme)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300374{
375 list_add(&syme->node, &active_symbols);
376}
377
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300378static void list_remove_active_sym(struct sym_entry *syme)
379{
380 pthread_mutex_lock(&active_symbols_lock);
381 list_del_init(&syme->node);
382 pthread_mutex_unlock(&active_symbols_lock);
383}
384
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300385static void rb_insert_active_sym(struct rb_root *tree, struct sym_entry *se)
386{
387 struct rb_node **p = &tree->rb_node;
388 struct rb_node *parent = NULL;
389 struct sym_entry *iter;
390
391 while (*p != NULL) {
392 parent = *p;
393 iter = rb_entry(parent, struct sym_entry, rb_node);
394
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300395 if (se->weight > iter->weight)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300396 p = &(*p)->rb_left;
397 else
398 p = &(*p)->rb_right;
399 }
400
401 rb_link_node(&se->rb_node, parent, p);
402 rb_insert_color(&se->rb_node, tree);
403}
Ingo Molnar07800602009-04-20 15:00:56 +0200404
405static void print_sym_table(void)
406{
Ingo Molnar233f0b92009-06-03 21:48:40 +0200407 int printed = 0, j;
Mike Galbraith46ab9762009-07-24 10:09:50 +0200408 int counter, snap = !display_weighted ? sym_counter : 0;
Ingo Molnar2debbc82009-06-05 14:29:10 +0200409 float samples_per_sec = samples/delay_secs;
410 float ksamples_per_sec = (samples-userspace_samples)/delay_secs;
411 float sum_ksamples = 0.0;
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300412 struct sym_entry *syme, *n;
413 struct rb_root tmp = RB_ROOT;
414 struct rb_node *nd;
Ingo Molnar07800602009-04-20 15:00:56 +0200415
Ingo Molnar2debbc82009-06-05 14:29:10 +0200416 samples = userspace_samples = 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200417
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300418 /* Sort the active symbols */
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300419 pthread_mutex_lock(&active_symbols_lock);
420 syme = list_entry(active_symbols.next, struct sym_entry, node);
421 pthread_mutex_unlock(&active_symbols_lock);
422
423 list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
Mike Galbraith46ab9762009-07-24 10:09:50 +0200424 syme->snap_count = syme->count[snap];
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300425 if (syme->snap_count != 0) {
426 syme->weight = sym_weight(syme);
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300427 rb_insert_active_sym(&tmp, syme);
Ingo Molnar2debbc82009-06-05 14:29:10 +0200428 sum_ksamples += syme->snap_count;
Mike Galbraithd94b9432009-05-25 09:57:56 +0200429
430 for (j = 0; j < nr_counters; j++)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300431 syme->count[j] = zero ? 0 : syme->count[j] * 7 / 8;
432 } else
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300433 list_remove_active_sym(syme);
Mike Galbraithd94b9432009-05-25 09:57:56 +0200434 }
435
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200436 puts(CONSOLE_CLEAR);
Ingo Molnar07800602009-04-20 15:00:56 +0200437
438 printf(
439"------------------------------------------------------------------------------\n");
Ingo Molnarf2521b62009-06-03 19:17:25 +0200440 printf( " PerfTop:%8.0f irqs/sec kernel:%4.1f%% [",
Ingo Molnar2debbc82009-06-05 14:29:10 +0200441 samples_per_sec,
442 100.0 - (100.0*((samples_per_sec-ksamples_per_sec)/samples_per_sec)));
Ingo Molnar07800602009-04-20 15:00:56 +0200443
Mike Galbraith46ab9762009-07-24 10:09:50 +0200444 if (nr_counters == 1 || !display_weighted) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000445 printf("%Ld", (u64)attrs[0].sample_period);
Ingo Molnarcf1f4572009-06-05 13:27:02 +0200446 if (freq)
447 printf("Hz ");
448 else
449 printf(" ");
450 }
Ingo Molnar07800602009-04-20 15:00:56 +0200451
Mike Galbraith46ab9762009-07-24 10:09:50 +0200452 if (!display_weighted)
453 printf("%s", event_name(sym_counter));
454 else for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnar07800602009-04-20 15:00:56 +0200455 if (counter)
456 printf("/");
457
458 printf("%s", event_name(counter));
459 }
460
461 printf( "], ");
462
Ingo Molnarb456bae2009-05-26 09:17:18 +0200463 if (target_pid != -1)
464 printf(" (target_pid: %d", target_pid);
Ingo Molnar07800602009-04-20 15:00:56 +0200465 else
466 printf(" (all");
467
468 if (profile_cpu != -1)
469 printf(", cpu: %d)\n", profile_cpu);
470 else {
Ingo Molnarb456bae2009-05-26 09:17:18 +0200471 if (target_pid != -1)
Ingo Molnar07800602009-04-20 15:00:56 +0200472 printf(")\n");
473 else
474 printf(", %d CPUs)\n", nr_cpus);
475 }
476
477 printf("------------------------------------------------------------------------------\n\n");
478
Mike Galbraith923c42c2009-07-22 20:36:03 +0200479 if (sym_filter_entry) {
480 show_details(sym_filter_entry);
481 return;
482 }
483
Ingo Molnar07800602009-04-20 15:00:56 +0200484 if (nr_counters == 1)
Ingo Molnar2debbc82009-06-05 14:29:10 +0200485 printf(" samples pcnt");
Ingo Molnar07800602009-04-20 15:00:56 +0200486 else
Ingo Molnar2debbc82009-06-05 14:29:10 +0200487 printf(" weight samples pcnt");
Ingo Molnar07800602009-04-20 15:00:56 +0200488
489 printf(" RIP kernel function\n"
Ingo Molnar2debbc82009-06-05 14:29:10 +0200490 " ______ _______ _____ ________________ _______________\n\n"
Ingo Molnar07800602009-04-20 15:00:56 +0200491 );
492
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300493 for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
494 struct sym_entry *syme = rb_entry(nd, struct sym_entry, rb_node);
495 struct symbol *sym = (struct symbol *)(syme + 1);
Ingo Molnar8fc03212009-06-04 15:19:47 +0200496 double pcnt;
Ingo Molnar07800602009-04-20 15:00:56 +0200497
Mike Galbraith923c42c2009-07-22 20:36:03 +0200498 if (++printed > print_entries || (int)syme->snap_count < count_filter)
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300499 continue;
Ingo Molnar07800602009-04-20 15:00:56 +0200500
Ingo Molnar2debbc82009-06-05 14:29:10 +0200501 pcnt = 100.0 - (100.0 * ((sum_ksamples - syme->snap_count) /
502 sum_ksamples));
Mike Galbraithd94b9432009-05-25 09:57:56 +0200503
Mike Galbraith46ab9762009-07-24 10:09:50 +0200504 if (nr_counters == 1 || !display_weighted)
Ingo Molnar2debbc82009-06-05 14:29:10 +0200505 printf("%20.2f - ", syme->weight);
Mike Galbraithd94b9432009-05-25 09:57:56 +0200506 else
Ingo Molnar2debbc82009-06-05 14:29:10 +0200507 printf("%9.1f %10ld - ", syme->weight, syme->snap_count);
Ingo Molnar8fc03212009-06-04 15:19:47 +0200508
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200509 percent_color_fprintf(stdout, "%4.1f%%", pcnt);
Mike Galbraith42976482009-07-02 08:09:46 +0200510 printf(" - %016llx : %s", sym->start, sym->name);
511 if (sym->module)
512 printf("\t[%s]", sym->module->name);
513 printf("\n");
Ingo Molnar07800602009-04-20 15:00:56 +0200514 }
Ingo Molnar07800602009-04-20 15:00:56 +0200515}
516
Mike Galbraith923c42c2009-07-22 20:36:03 +0200517static void prompt_integer(int *target, const char *msg)
518{
519 char *buf = malloc(0), *p;
520 size_t dummy = 0;
521 int tmp;
522
523 fprintf(stdout, "\n%s: ", msg);
524 if (getline(&buf, &dummy, stdin) < 0)
525 return;
526
527 p = strchr(buf, '\n');
528 if (p)
529 *p = 0;
530
531 p = buf;
532 while(*p) {
533 if (!isdigit(*p))
534 goto out_free;
535 p++;
536 }
537 tmp = strtoul(buf, NULL, 10);
538 *target = tmp;
539out_free:
540 free(buf);
541}
542
543static void prompt_percent(int *target, const char *msg)
544{
545 int tmp = 0;
546
547 prompt_integer(&tmp, msg);
548 if (tmp >= 0 && tmp <= 100)
549 *target = tmp;
550}
551
552static void prompt_symbol(struct sym_entry **target, const char *msg)
553{
554 char *buf = malloc(0), *p;
555 struct sym_entry *syme = *target, *n, *found = NULL;
556 size_t dummy = 0;
557
558 /* zero counters of active symbol */
559 if (syme) {
560 pthread_mutex_lock(&syme->source_lock);
561 __zero_source_counters(syme);
562 *target = NULL;
563 pthread_mutex_unlock(&syme->source_lock);
564 }
565
566 fprintf(stdout, "\n%s: ", msg);
567 if (getline(&buf, &dummy, stdin) < 0)
568 goto out_free;
569
570 p = strchr(buf, '\n');
571 if (p)
572 *p = 0;
573
574 pthread_mutex_lock(&active_symbols_lock);
575 syme = list_entry(active_symbols.next, struct sym_entry, node);
576 pthread_mutex_unlock(&active_symbols_lock);
577
578 list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
579 struct symbol *sym = (struct symbol *)(syme + 1);
580
581 if (!strcmp(buf, sym->name)) {
582 found = syme;
583 break;
584 }
585 }
586
587 if (!found) {
588 fprintf(stderr, "Sorry, %s is not active.\n", sym_filter);
589 sleep(1);
590 return;
591 } else
592 parse_source(found);
593
594out_free:
595 free(buf);
596}
597
598static void print_known_keys(void)
599{
600 fprintf(stdout, "\nknown keys:\n");
601 fprintf(stdout, "\t[d] select display delay.\n");
602 fprintf(stdout, "\t[e] select display entries (lines).\n");
Mike Galbraith46ab9762009-07-24 10:09:50 +0200603 fprintf(stdout, "\t[E] active event counter. \t(%s)\n", event_name(sym_counter));
Mike Galbraith923c42c2009-07-22 20:36:03 +0200604 fprintf(stdout, "\t[f] select normal display count filter.\n");
605 fprintf(stdout, "\t[F] select annotation display count filter (percentage).\n");
606 fprintf(stdout, "\t[qQ] quit.\n");
607 fprintf(stdout, "\t[s] select annotation symbol and start annotation.\n");
608 fprintf(stdout, "\t[S] stop annotation, revert to normal display.\n");
Mike Galbraith46ab9762009-07-24 10:09:50 +0200609 fprintf(stdout, "\t[w] toggle display weighted/count[E]r. \t(%d)\n", display_weighted ? 1 : 0);
610 fprintf(stdout, "\t[z] toggle sample zeroing. \t(%d)\n", zero ? 1 : 0);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200611}
612
613static void handle_keypress(int c)
614{
615 int once = 0;
616repeat:
617 switch (c) {
618 case 'd':
619 prompt_integer(&delay_secs, "Enter display delay");
620 break;
621 case 'e':
622 prompt_integer(&print_entries, "Enter display entries (lines)");
623 break;
624 case 'E':
625 if (nr_counters > 1) {
626 int i;
627
628 fprintf(stderr, "\nAvailable events:");
629 for (i = 0; i < nr_counters; i++)
630 fprintf(stderr, "\n\t%d %s", i, event_name(i));
631
632 prompt_integer(&sym_counter, "Enter details event counter");
633
634 if (sym_counter >= nr_counters) {
635 fprintf(stderr, "Sorry, no such event, using %s.\n", event_name(0));
636 sym_counter = 0;
637 sleep(1);
638 }
639 } else sym_counter = 0;
640 break;
641 case 'f':
642 prompt_integer(&count_filter, "Enter display event count filter");
643 break;
644 case 'F':
645 prompt_percent(&sym_pcnt_filter, "Enter details display event filter (percent)");
646 break;
647 case 'q':
648 case 'Q':
649 printf("exiting.\n");
650 exit(0);
651 case 's':
652 prompt_symbol(&sym_filter_entry, "Enter details symbol");
653 break;
654 case 'S':
655 if (!sym_filter_entry)
656 break;
657 else {
658 struct sym_entry *syme = sym_filter_entry;
659
660 pthread_mutex_lock(&syme->source_lock);
661 sym_filter_entry = NULL;
662 __zero_source_counters(syme);
663 pthread_mutex_unlock(&syme->source_lock);
664 }
665 break;
Mike Galbraith46ab9762009-07-24 10:09:50 +0200666 case 'w':
667 display_weighted = ~display_weighted;
668 break;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200669 case 'z':
670 zero = ~zero;
671 break;
672 default: {
673 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
674 struct termios tc, save;
675
676 if (!once) {
677 print_known_keys();
678 once++;
679 }
680
681 tcgetattr(0, &save);
682 tc = save;
683 tc.c_lflag &= ~(ICANON | ECHO);
684 tc.c_cc[VMIN] = 0;
685 tc.c_cc[VTIME] = 0;
686 tcsetattr(0, TCSANOW, &tc);
687
688 poll(&stdin_poll, 1, -1);
689 c = getc(stdin);
690
691 tcsetattr(0, TCSAFLUSH, &save);
692 goto repeat;
693 }
694 }
695}
696
Ingo Molnarf37a2912009-07-01 12:37:06 +0200697static void *display_thread(void *arg __used)
Ingo Molnar07800602009-04-20 15:00:56 +0200698{
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200699 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
Mike Galbraith923c42c2009-07-22 20:36:03 +0200700 struct termios tc, save;
701 int delay_msecs, c;
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200702
Mike Galbraith923c42c2009-07-22 20:36:03 +0200703 tcgetattr(0, &save);
704 tc = save;
705 tc.c_lflag &= ~(ICANON | ECHO);
706 tc.c_cc[VMIN] = 0;
707 tc.c_cc[VTIME] = 0;
708repeat:
709 delay_msecs = delay_secs * 1000;
710 tcsetattr(0, TCSANOW, &tc);
711 /* trash return*/
712 getc(stdin);
Ingo Molnar07800602009-04-20 15:00:56 +0200713
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200714 do {
Ingo Molnar07800602009-04-20 15:00:56 +0200715 print_sym_table();
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200716 } while (!poll(&stdin_poll, 1, delay_msecs) == 1);
717
Mike Galbraith923c42c2009-07-22 20:36:03 +0200718 c = getc(stdin);
719 tcsetattr(0, TCSAFLUSH, &save);
720
721 handle_keypress(c);
722 goto repeat;
Ingo Molnar07800602009-04-20 15:00:56 +0200723
724 return NULL;
725}
726
Anton Blanchard2ab52082009-07-01 09:00:46 +1000727/* Tag samples to be skipped. */
Ingo Molnarf37a2912009-07-01 12:37:06 +0200728static const char *skip_symbols[] = {
Anton Blanchard2ab52082009-07-01 09:00:46 +1000729 "default_idle",
730 "cpu_idle",
731 "enter_idle",
732 "exit_idle",
733 "mwait_idle",
Arnaldo Carvalho de Melo59b90052009-07-26 19:06:19 -0300734 "mwait_idle_with_hints",
Anton Blanchard3a3393e2009-07-01 09:00:47 +1000735 "ppc64_runlatch_off",
736 "pseries_dedicated_idle_sleep",
Anton Blanchard2ab52082009-07-01 09:00:46 +1000737 NULL
738};
739
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300740static int symbol_filter(struct dso *self, struct symbol *sym)
Ingo Molnar07800602009-04-20 15:00:56 +0200741{
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300742 struct sym_entry *syme;
743 const char *name = sym->name;
Anton Blanchard2ab52082009-07-01 09:00:46 +1000744 int i;
Ingo Molnar07800602009-04-20 15:00:56 +0200745
Anton Blanchard3a3393e2009-07-01 09:00:47 +1000746 /*
747 * ppc64 uses function descriptors and appends a '.' to the
748 * start of every instruction address. Remove it.
749 */
750 if (name[0] == '.')
751 name++;
752
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300753 if (!strcmp(name, "_text") ||
754 !strcmp(name, "_etext") ||
755 !strcmp(name, "_sinittext") ||
756 !strncmp("init_module", name, 11) ||
757 !strncmp("cleanup_module", name, 14) ||
758 strstr(name, "_text_start") ||
759 strstr(name, "_text_end"))
Ingo Molnar07800602009-04-20 15:00:56 +0200760 return 1;
761
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300762 syme = dso__sym_priv(self, sym);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200763 pthread_mutex_init(&syme->source_lock, NULL);
764 if (!sym_filter_entry && sym_filter && !strcmp(name, sym_filter))
765 sym_filter_entry = syme;
766
Anton Blanchard2ab52082009-07-01 09:00:46 +1000767 for (i = 0; skip_symbols[i]; i++) {
768 if (!strcmp(skip_symbols[i], name)) {
769 syme->skip = 1;
770 break;
771 }
772 }
Ingo Molnar07800602009-04-20 15:00:56 +0200773
Ingo Molnar07800602009-04-20 15:00:56 +0200774 return 0;
775}
776
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300777static int parse_symbols(void)
Ingo Molnar07800602009-04-20 15:00:56 +0200778{
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300779 struct rb_node *node;
780 struct symbol *sym;
Mike Galbraith42976482009-07-02 08:09:46 +0200781 int modules = vmlinux ? 1 : 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200782
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300783 kernel_dso = dso__new("[kernel]", sizeof(struct sym_entry));
784 if (kernel_dso == NULL)
785 return -1;
Ingo Molnar07800602009-04-20 15:00:56 +0200786
Mike Galbraith42976482009-07-02 08:09:46 +0200787 if (dso__load_kernel(kernel_dso, vmlinux, symbol_filter, verbose, modules) <= 0)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300788 goto out_delete_dso;
Ingo Molnar07800602009-04-20 15:00:56 +0200789
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300790 node = rb_first(&kernel_dso->syms);
791 sym = rb_entry(node, struct symbol, rb_node);
792 min_ip = sym->start;
Ingo Molnar07800602009-04-20 15:00:56 +0200793
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300794 node = rb_last(&kernel_dso->syms);
795 sym = rb_entry(node, struct symbol, rb_node);
Mike Galbraithda417a72009-05-29 08:23:16 +0200796 max_ip = sym->end;
Ingo Molnar07800602009-04-20 15:00:56 +0200797
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300798 if (dump_symtab)
Mike Galbraitha3ec8d72009-05-29 06:46:46 +0200799 dso__fprintf(kernel_dso, stderr);
Ingo Molnar07800602009-04-20 15:00:56 +0200800
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300801 return 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200802
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300803out_delete_dso:
804 dso__delete(kernel_dso);
805 kernel_dso = NULL;
806 return -1;
Ingo Molnar07800602009-04-20 15:00:56 +0200807}
808
Ingo Molnar07800602009-04-20 15:00:56 +0200809/*
810 * Binary search in the histogram table and record the hit:
811 */
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000812static void record_ip(u64 ip, int counter)
Ingo Molnar07800602009-04-20 15:00:56 +0200813{
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300814 struct symbol *sym = dso__find_symbol(kernel_dso, ip);
Ingo Molnar07800602009-04-20 15:00:56 +0200815
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300816 if (sym != NULL) {
817 struct sym_entry *syme = dso__sym_priv(kernel_dso, sym);
Ingo Molnar07800602009-04-20 15:00:56 +0200818
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300819 if (!syme->skip) {
820 syme->count[counter]++;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200821 record_precise_ip(syme, counter, ip);
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300822 pthread_mutex_lock(&active_symbols_lock);
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300823 if (list_empty(&syme->node) || !syme->node.next)
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300824 __list_insert_active_sym(syme);
825 pthread_mutex_unlock(&active_symbols_lock);
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300826 return;
Ingo Molnar07800602009-04-20 15:00:56 +0200827 }
Ingo Molnar07800602009-04-20 15:00:56 +0200828 }
829
Ingo Molnar2debbc82009-06-05 14:29:10 +0200830 samples--;
Ingo Molnar07800602009-04-20 15:00:56 +0200831}
832
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +0200833static void process_event(u64 ip, int counter, int user)
Ingo Molnar07800602009-04-20 15:00:56 +0200834{
Ingo Molnar2debbc82009-06-05 14:29:10 +0200835 samples++;
Ingo Molnar07800602009-04-20 15:00:56 +0200836
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +0200837 if (user) {
Ingo Molnar2debbc82009-06-05 14:29:10 +0200838 userspace_samples++;
Ingo Molnar07800602009-04-20 15:00:56 +0200839 return;
840 }
841
842 record_ip(ip, counter);
843}
844
Ingo Molnar07800602009-04-20 15:00:56 +0200845struct mmap_data {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200846 int counter;
847 void *base;
Ingo Molnarf37a2912009-07-01 12:37:06 +0200848 int mask;
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200849 unsigned int prev;
Ingo Molnar07800602009-04-20 15:00:56 +0200850};
851
852static unsigned int mmap_read_head(struct mmap_data *md)
853{
854 struct perf_counter_mmap_page *pc = md->base;
855 int head;
856
857 head = pc->data_head;
858 rmb();
859
860 return head;
861}
862
863struct timeval last_read, this_read;
864
Frederic Weisbecker2f011902009-06-06 23:10:43 +0200865static void mmap_read_counter(struct mmap_data *md)
Ingo Molnar07800602009-04-20 15:00:56 +0200866{
867 unsigned int head = mmap_read_head(md);
868 unsigned int old = md->prev;
869 unsigned char *data = md->base + page_size;
870 int diff;
871
872 gettimeofday(&this_read, NULL);
873
874 /*
875 * If we're further behind than half the buffer, there's a chance
Ingo Molnar2debbc82009-06-05 14:29:10 +0200876 * the writer will bite our tail and mess up the samples under us.
Ingo Molnar07800602009-04-20 15:00:56 +0200877 *
878 * If we somehow ended up ahead of the head, we got messed up.
879 *
880 * In either case, truncate and restart at head.
881 */
882 diff = head - old;
883 if (diff > md->mask / 2 || diff < 0) {
884 struct timeval iv;
885 unsigned long msecs;
886
887 timersub(&this_read, &last_read, &iv);
888 msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
889
890 fprintf(stderr, "WARNING: failed to keep up with mmap data."
891 " Last read %lu msecs ago.\n", msecs);
892
893 /*
894 * head points to a known good entry, start there.
895 */
896 old = head;
897 }
898
899 last_read = this_read;
900
901 for (; old != head;) {
902 struct ip_event {
903 struct perf_event_header header;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000904 u64 ip;
905 u32 pid, target_pid;
Ingo Molnar07800602009-04-20 15:00:56 +0200906 };
907 struct mmap_event {
908 struct perf_event_header header;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000909 u32 pid, target_pid;
910 u64 start;
911 u64 len;
912 u64 pgoff;
Ingo Molnar07800602009-04-20 15:00:56 +0200913 char filename[PATH_MAX];
914 };
915
916 typedef union event_union {
917 struct perf_event_header header;
918 struct ip_event ip;
919 struct mmap_event mmap;
920 } event_t;
921
922 event_t *event = (event_t *)&data[old & md->mask];
923
924 event_t event_copy;
925
Ingo Molnar6f06ccb2009-04-20 15:22:22 +0200926 size_t size = event->header.size;
Ingo Molnar07800602009-04-20 15:00:56 +0200927
928 /*
929 * Event straddles the mmap boundary -- header should always
930 * be inside due to u64 alignment of output.
931 */
932 if ((old & md->mask) + size != ((old + size) & md->mask)) {
933 unsigned int offset = old;
934 unsigned int len = min(sizeof(*event), size), cpy;
935 void *dst = &event_copy;
936
937 do {
938 cpy = min(md->mask + 1 - (offset & md->mask), len);
939 memcpy(dst, &data[offset & md->mask], cpy);
940 offset += cpy;
941 dst += cpy;
942 len -= cpy;
943 } while (len);
944
945 event = &event_copy;
946 }
947
948 old += size;
949
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +0200950 if (event->header.type == PERF_EVENT_SAMPLE) {
951 int user =
952 (event->header.misc & PERF_EVENT_MISC_CPUMODE_MASK) == PERF_EVENT_MISC_USER;
953 process_event(event->ip.ip, md->counter, user);
Ingo Molnar07800602009-04-20 15:00:56 +0200954 }
955 }
956
957 md->prev = old;
958}
959
Mike Galbraithc2990a22009-05-24 08:35:49 +0200960static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
961static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
962
Frederic Weisbecker2f011902009-06-06 23:10:43 +0200963static void mmap_read(void)
964{
965 int i, counter;
966
967 for (i = 0; i < nr_cpus; i++) {
968 for (counter = 0; counter < nr_counters; counter++)
969 mmap_read_counter(&mmap_array[i][counter]);
970 }
971}
972
Ingo Molnar716c69f2009-06-07 17:31:52 +0200973int nr_poll;
974int group_fd;
975
976static void start_counter(int i, int counter)
Ingo Molnar07800602009-04-20 15:00:56 +0200977{
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200978 struct perf_counter_attr *attr;
Mike Galbraith0fdc7e62009-07-21 10:30:36 +0200979 int cpu;
Ingo Molnar716c69f2009-06-07 17:31:52 +0200980
981 cpu = profile_cpu;
982 if (target_pid == -1 && profile_cpu == -1)
983 cpu = i;
984
985 attr = attrs + counter;
986
987 attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
988 attr->freq = freq;
Mike Galbraith0fdc7e62009-07-21 10:30:36 +0200989 attr->inherit = (cpu < 0) && inherit;
Ingo Molnar716c69f2009-06-07 17:31:52 +0200990
991try_again:
992 fd[i][counter] = sys_perf_counter_open(attr, target_pid, cpu, group_fd, 0);
993
994 if (fd[i][counter] < 0) {
995 int err = errno;
996
Ingo Molnar716c69f2009-06-07 17:31:52 +0200997 if (err == EPERM)
Ingo Molnar3da297a2009-06-07 17:39:02 +0200998 die("No permission - are you root?\n");
Ingo Molnar716c69f2009-06-07 17:31:52 +0200999 /*
1000 * If it's cycles then fall back to hrtimer
1001 * based cpu-clock-tick sw counter, which
1002 * is always available even if no PMU support:
1003 */
1004 if (attr->type == PERF_TYPE_HARDWARE
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02001005 && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
Ingo Molnar716c69f2009-06-07 17:31:52 +02001006
Ingo Molnar3da297a2009-06-07 17:39:02 +02001007 if (verbose)
1008 warning(" ... trying to fall back to cpu-clock-ticks\n");
1009
Ingo Molnar716c69f2009-06-07 17:31:52 +02001010 attr->type = PERF_TYPE_SOFTWARE;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02001011 attr->config = PERF_COUNT_SW_CPU_CLOCK;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001012 goto try_again;
1013 }
Ingo Molnar30c806a2009-06-07 17:46:24 +02001014 printf("\n");
1015 error("perfcounter syscall returned with %d (%s)\n",
1016 fd[i][counter], strerror(err));
1017 die("No CONFIG_PERF_COUNTERS=y kernel support configured?\n");
Ingo Molnar716c69f2009-06-07 17:31:52 +02001018 exit(-1);
1019 }
1020 assert(fd[i][counter] >= 0);
1021 fcntl(fd[i][counter], F_SETFL, O_NONBLOCK);
1022
1023 /*
1024 * First counter acts as the group leader:
1025 */
1026 if (group && group_fd == -1)
1027 group_fd = fd[i][counter];
1028
1029 event_array[nr_poll].fd = fd[i][counter];
1030 event_array[nr_poll].events = POLLIN;
1031 nr_poll++;
1032
1033 mmap_array[i][counter].counter = counter;
1034 mmap_array[i][counter].prev = 0;
1035 mmap_array[i][counter].mask = mmap_pages*page_size - 1;
1036 mmap_array[i][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
1037 PROT_READ, MAP_SHARED, fd[i][counter], 0);
1038 if (mmap_array[i][counter].base == MAP_FAILED)
1039 die("failed to mmap with %d (%s)\n", errno, strerror(errno));
1040}
1041
1042static int __cmd_top(void)
1043{
1044 pthread_t thread;
1045 int i, counter;
Ingo Molnar07800602009-04-20 15:00:56 +02001046 int ret;
1047
Ingo Molnar07800602009-04-20 15:00:56 +02001048 for (i = 0; i < nr_cpus; i++) {
1049 group_fd = -1;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001050 for (counter = 0; counter < nr_counters; counter++)
1051 start_counter(i, counter);
Ingo Molnar07800602009-04-20 15:00:56 +02001052 }
1053
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001054 /* Wait for a minimal set of events before starting the snapshot */
1055 poll(event_array, nr_poll, 100);
1056
1057 mmap_read();
1058
Ingo Molnar07800602009-04-20 15:00:56 +02001059 if (pthread_create(&thread, NULL, display_thread, NULL)) {
1060 printf("Could not create display thread.\n");
1061 exit(-1);
1062 }
1063
1064 if (realtime_prio) {
1065 struct sched_param param;
1066
1067 param.sched_priority = realtime_prio;
1068 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
1069 printf("Could not set realtime priority.\n");
1070 exit(-1);
1071 }
1072 }
1073
1074 while (1) {
Ingo Molnar2debbc82009-06-05 14:29:10 +02001075 int hits = samples;
Ingo Molnar07800602009-04-20 15:00:56 +02001076
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001077 mmap_read();
Ingo Molnar07800602009-04-20 15:00:56 +02001078
Ingo Molnar2debbc82009-06-05 14:29:10 +02001079 if (hits == samples)
Ingo Molnar07800602009-04-20 15:00:56 +02001080 ret = poll(event_array, nr_poll, 100);
1081 }
1082
1083 return 0;
1084}
Ingo Molnarb456bae2009-05-26 09:17:18 +02001085
1086static const char * const top_usage[] = {
1087 "perf top [<options>]",
1088 NULL
1089};
1090
Ingo Molnarb456bae2009-05-26 09:17:18 +02001091static const struct option options[] = {
1092 OPT_CALLBACK('e', "event", NULL, "event",
Thomas Gleixner86847b62009-06-06 12:24:17 +02001093 "event selector. use 'perf list' to list available events",
1094 parse_events),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001095 OPT_INTEGER('c', "count", &default_interval,
1096 "event period to sample"),
1097 OPT_INTEGER('p', "pid", &target_pid,
1098 "profile events on existing pid"),
1099 OPT_BOOLEAN('a', "all-cpus", &system_wide,
1100 "system-wide collection from all CPUs"),
1101 OPT_INTEGER('C', "CPU", &profile_cpu,
1102 "CPU to profile on"),
Mike Galbraith42976482009-07-02 08:09:46 +02001103 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001104 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
1105 "number of mmap data pages"),
1106 OPT_INTEGER('r', "realtime", &realtime_prio,
1107 "collect data with this RT SCHED_FIFO priority"),
Mike Galbraithdb20c002009-05-26 15:25:34 +02001108 OPT_INTEGER('d', "delay", &delay_secs,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001109 "number of seconds to delay between refreshes"),
1110 OPT_BOOLEAN('D', "dump-symtab", &dump_symtab,
1111 "dump the symbol table used for profiling"),
Ingo Molnar6e53cdf2009-06-04 08:53:05 +02001112 OPT_INTEGER('f', "count-filter", &count_filter,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001113 "only display functions with more events than this"),
1114 OPT_BOOLEAN('g', "group", &group,
1115 "put the counters into a counter group"),
Mike Galbraith0fdc7e62009-07-21 10:30:36 +02001116 OPT_BOOLEAN('i', "inherit", &inherit,
1117 "child tasks inherit counters"),
Mike Galbraith923c42c2009-07-22 20:36:03 +02001118 OPT_STRING('s', "sym-annotate", &sym_filter, "symbol name",
1119 "symbol to annotate - requires -k option"),
Anton Blanchard1f208ea2009-07-01 09:00:44 +10001120 OPT_BOOLEAN('z', "zero", &zero,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001121 "zero history across updates"),
Ingo Molnar6e53cdf2009-06-04 08:53:05 +02001122 OPT_INTEGER('F', "freq", &freq,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001123 "profile at this frequency"),
Ingo Molnar6e53cdf2009-06-04 08:53:05 +02001124 OPT_INTEGER('E', "entries", &print_entries,
1125 "display this many functions"),
Ingo Molnar3da297a2009-06-07 17:39:02 +02001126 OPT_BOOLEAN('v', "verbose", &verbose,
1127 "be more verbose (show counter open errors, etc)"),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001128 OPT_END()
1129};
1130
Ingo Molnarf37a2912009-07-01 12:37:06 +02001131int cmd_top(int argc, const char **argv, const char *prefix __used)
Ingo Molnarb456bae2009-05-26 09:17:18 +02001132{
1133 int counter;
1134
Mike Galbraith42976482009-07-02 08:09:46 +02001135 symbol__init();
1136
Ingo Molnarb456bae2009-05-26 09:17:18 +02001137 page_size = sysconf(_SC_PAGE_SIZE);
1138
Ingo Molnarb456bae2009-05-26 09:17:18 +02001139 argc = parse_options(argc, argv, options, top_usage, 0);
1140 if (argc)
1141 usage_with_options(top_usage, options);
1142
1143 if (freq) {
1144 default_interval = freq;
1145 freq = 1;
1146 }
1147
1148 /* CPU and PID are mutually exclusive */
1149 if (target_pid != -1 && profile_cpu != -1) {
1150 printf("WARNING: PID switch overriding CPU\n");
1151 sleep(1);
1152 profile_cpu = -1;
1153 }
1154
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001155 if (!nr_counters)
Ingo Molnarb456bae2009-05-26 09:17:18 +02001156 nr_counters = 1;
Ingo Molnarb456bae2009-05-26 09:17:18 +02001157
Frederic Weisbecker2f335a02009-06-05 19:31:01 +02001158 if (delay_secs < 1)
1159 delay_secs = 1;
1160
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001161 parse_symbols();
Mike Galbraith923c42c2009-07-22 20:36:03 +02001162 parse_source(sym_filter_entry);
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001163
1164 /*
1165 * Fill in the ones not specifically initialized via -c:
1166 */
Ingo Molnarb456bae2009-05-26 09:17:18 +02001167 for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001168 if (attrs[counter].sample_period)
Ingo Molnarb456bae2009-05-26 09:17:18 +02001169 continue;
1170
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001171 attrs[counter].sample_period = default_interval;
Ingo Molnarb456bae2009-05-26 09:17:18 +02001172 }
1173
1174 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
1175 assert(nr_cpus <= MAX_NR_CPUS);
1176 assert(nr_cpus >= 0);
1177
1178 if (target_pid != -1 || profile_cpu != -1)
1179 nr_cpus = 1;
1180
Ingo Molnarb456bae2009-05-26 09:17:18 +02001181 return __cmd_top();
1182}