blob: d58701346b1ed516a92cf8403f14cc5ea605096c [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;
93
94/*
Ingo Molnar07800602009-04-20 15:00:56 +020095 * Symbols
96 */
97
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100098static u64 min_ip;
99static u64 max_ip = -1ll;
Ingo Molnar07800602009-04-20 15:00:56 +0200100
101struct sym_entry {
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300102 struct rb_node rb_node;
103 struct list_head node;
Ingo Molnar07800602009-04-20 15:00:56 +0200104 unsigned long count[MAX_COUNTERS];
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300105 unsigned long snap_count;
106 double weight;
Ingo Molnar07800602009-04-20 15:00:56 +0200107 int skip;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200108 struct source_line *source;
109 struct source_line *lines;
110 struct source_line **lines_tail;
111 pthread_mutex_t source_lock;
Ingo Molnar07800602009-04-20 15:00:56 +0200112};
113
Mike Galbraith923c42c2009-07-22 20:36:03 +0200114/*
115 * Source functions
116 */
117
118static void parse_source(struct sym_entry *syme)
119{
120 struct symbol *sym;
121 struct module *module;
122 struct section *section = NULL;
123 FILE *file;
124 char command[PATH_MAX*2], *path = vmlinux;
125 u64 start, end, len;
126
127 if (!syme)
128 return;
129
130 if (syme->lines) {
131 pthread_mutex_lock(&syme->source_lock);
132 goto out_assign;
133 }
134
135 sym = (struct symbol *)(syme + 1);
136 module = sym->module;
137
138 if (module)
139 path = module->path;
140 if (!path)
141 return;
142
143 start = sym->obj_start;
144 if (!start)
145 start = sym->start;
146
147 if (module) {
148 section = module->sections->find_section(module->sections, ".text");
149 if (section)
150 start -= section->vma;
151 }
152
153 end = start + sym->end - sym->start + 1;
154 len = sym->end - sym->start;
155
156 sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s", start, end, path);
157
158 file = popen(command, "r");
159 if (!file)
160 return;
161
162 pthread_mutex_lock(&syme->source_lock);
163 syme->lines_tail = &syme->lines;
164 while (!feof(file)) {
165 struct source_line *src;
166 size_t dummy = 0;
167 char *c;
168
169 src = malloc(sizeof(struct source_line));
170 assert(src != NULL);
171 memset(src, 0, sizeof(struct source_line));
172
173 if (getline(&src->line, &dummy, file) < 0)
174 break;
175 if (!src->line)
176 break;
177
178 c = strchr(src->line, '\n');
179 if (c)
180 *c = 0;
181
182 src->next = NULL;
183 *syme->lines_tail = src;
184 syme->lines_tail = &src->next;
185
186 if (strlen(src->line)>8 && src->line[8] == ':') {
187 src->eip = strtoull(src->line, NULL, 16);
188 if (section)
189 src->eip += section->vma;
190 }
191 if (strlen(src->line)>8 && src->line[16] == ':') {
192 src->eip = strtoull(src->line, NULL, 16);
193 if (section)
194 src->eip += section->vma;
195 }
196 }
197 pclose(file);
198out_assign:
199 sym_filter_entry = syme;
200 pthread_mutex_unlock(&syme->source_lock);
201}
202
203static void __zero_source_counters(struct sym_entry *syme)
204{
205 int i;
206 struct source_line *line;
207
208 line = syme->lines;
209 while (line) {
210 for (i = 0; i < nr_counters; i++)
211 line->count[i] = 0;
212 line = line->next;
213 }
214}
215
216static void record_precise_ip(struct sym_entry *syme, int counter, u64 ip)
217{
218 struct source_line *line;
219
220 if (syme != sym_filter_entry)
221 return;
222
223 if (pthread_mutex_trylock(&syme->source_lock))
224 return;
225
226 if (!syme->source)
227 goto out_unlock;
228
229 for (line = syme->lines; line; line = line->next) {
230 if (line->eip == ip) {
231 line->count[counter]++;
232 break;
233 }
234 if (line->eip > ip)
235 break;
236 }
237out_unlock:
238 pthread_mutex_unlock(&syme->source_lock);
239}
240
241static void lookup_sym_source(struct sym_entry *syme)
242{
243 struct symbol *symbol = (struct symbol *)(syme + 1);
244 struct source_line *line;
245 char pattern[PATH_MAX];
246 char *idx;
247
248 sprintf(pattern, "<%s>:", symbol->name);
249
250 if (symbol->module) {
251 idx = strstr(pattern, "\t");
252 if (idx)
253 *idx = 0;
254 }
255
256 pthread_mutex_lock(&syme->source_lock);
257 for (line = syme->lines; line; line = line->next) {
258 if (strstr(line->line, pattern)) {
259 syme->source = line;
260 break;
261 }
262 }
263 pthread_mutex_unlock(&syme->source_lock);
264}
265
266static void show_lines(struct source_line *queue, int count, int total)
267{
268 int i;
269 struct source_line *line;
270
271 line = queue;
272 for (i = 0; i < count; i++) {
273 float pcnt = 100.0*(float)line->count[sym_counter]/(float)total;
274
275 printf("%8li %4.1f%%\t%s\n", line->count[sym_counter], pcnt, line->line);
276 line = line->next;
277 }
278}
279
280#define TRACE_COUNT 3
281
282static void show_details(struct sym_entry *syme)
283{
284 struct symbol *symbol;
285 struct source_line *line;
286 struct source_line *line_queue = NULL;
287 int displayed = 0;
288 int line_queue_count = 0, total = 0, more = 0;
289
290 if (!syme)
291 return;
292
293 if (!syme->source)
294 lookup_sym_source(syme);
295
296 if (!syme->source)
297 return;
298
299 symbol = (struct symbol *)(syme + 1);
300 printf("Showing %s for %s\n", event_name(sym_counter), symbol->name);
301 printf(" Events Pcnt (>=%d%%)\n", sym_pcnt_filter);
302
303 pthread_mutex_lock(&syme->source_lock);
304 line = syme->source;
305 while (line) {
306 total += line->count[sym_counter];
307 line = line->next;
308 }
309
310 line = syme->source;
311 while (line) {
312 float pcnt = 0.0;
313
314 if (!line_queue_count)
315 line_queue = line;
316 line_queue_count++;
317
318 if (line->count[sym_counter])
319 pcnt = 100.0 * line->count[sym_counter] / (float)total;
320 if (pcnt >= (float)sym_pcnt_filter) {
321 if (displayed <= print_entries)
322 show_lines(line_queue, line_queue_count, total);
323 else more++;
324 displayed += line_queue_count;
325 line_queue_count = 0;
326 line_queue = NULL;
327 } else if (line_queue_count > TRACE_COUNT) {
328 line_queue = line_queue->next;
329 line_queue_count--;
330 }
331
332 line->count[sym_counter] = zero ? 0 : line->count[sym_counter] * 7 / 8;
333 line = line->next;
334 }
335 pthread_mutex_unlock(&syme->source_lock);
336 if (more)
337 printf("%d lines not displayed, maybe increase display entries [e]\n", more);
338}
Ingo Molnar07800602009-04-20 15:00:56 +0200339
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200340struct dso *kernel_dso;
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300341
342/*
343 * Symbols will be added here in record_ip and will get out
344 * after decayed.
345 */
346static LIST_HEAD(active_symbols);
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300347static pthread_mutex_t active_symbols_lock = PTHREAD_MUTEX_INITIALIZER;
Ingo Molnar07800602009-04-20 15:00:56 +0200348
Ingo Molnar07800602009-04-20 15:00:56 +0200349/*
350 * Ordering weight: count-1 * count-2 * ... / count-n
351 */
352static double sym_weight(const struct sym_entry *sym)
353{
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300354 double weight = sym->snap_count;
Ingo Molnar07800602009-04-20 15:00:56 +0200355 int counter;
356
Ingo Molnar07800602009-04-20 15:00:56 +0200357 for (counter = 1; counter < nr_counters-1; counter++)
358 weight *= sym->count[counter];
359
360 weight /= (sym->count[counter] + 1);
361
362 return weight;
363}
364
Ingo Molnar2debbc82009-06-05 14:29:10 +0200365static long samples;
366static long userspace_samples;
Ingo Molnar07800602009-04-20 15:00:56 +0200367static const char CONSOLE_CLEAR[] = "";
368
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300369static void __list_insert_active_sym(struct sym_entry *syme)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300370{
371 list_add(&syme->node, &active_symbols);
372}
373
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300374static void list_remove_active_sym(struct sym_entry *syme)
375{
376 pthread_mutex_lock(&active_symbols_lock);
377 list_del_init(&syme->node);
378 pthread_mutex_unlock(&active_symbols_lock);
379}
380
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300381static void rb_insert_active_sym(struct rb_root *tree, struct sym_entry *se)
382{
383 struct rb_node **p = &tree->rb_node;
384 struct rb_node *parent = NULL;
385 struct sym_entry *iter;
386
387 while (*p != NULL) {
388 parent = *p;
389 iter = rb_entry(parent, struct sym_entry, rb_node);
390
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300391 if (se->weight > iter->weight)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300392 p = &(*p)->rb_left;
393 else
394 p = &(*p)->rb_right;
395 }
396
397 rb_link_node(&se->rb_node, parent, p);
398 rb_insert_color(&se->rb_node, tree);
399}
Ingo Molnar07800602009-04-20 15:00:56 +0200400
401static void print_sym_table(void)
402{
Ingo Molnar233f0b92009-06-03 21:48:40 +0200403 int printed = 0, j;
Ingo Molnar07800602009-04-20 15:00:56 +0200404 int counter;
Ingo Molnar2debbc82009-06-05 14:29:10 +0200405 float samples_per_sec = samples/delay_secs;
406 float ksamples_per_sec = (samples-userspace_samples)/delay_secs;
407 float sum_ksamples = 0.0;
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300408 struct sym_entry *syme, *n;
409 struct rb_root tmp = RB_ROOT;
410 struct rb_node *nd;
Ingo Molnar07800602009-04-20 15:00:56 +0200411
Ingo Molnar2debbc82009-06-05 14:29:10 +0200412 samples = userspace_samples = 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200413
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300414 /* Sort the active symbols */
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300415 pthread_mutex_lock(&active_symbols_lock);
416 syme = list_entry(active_symbols.next, struct sym_entry, node);
417 pthread_mutex_unlock(&active_symbols_lock);
418
419 list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
420 syme->snap_count = syme->count[0];
421 if (syme->snap_count != 0) {
422 syme->weight = sym_weight(syme);
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300423 rb_insert_active_sym(&tmp, syme);
Ingo Molnar2debbc82009-06-05 14:29:10 +0200424 sum_ksamples += syme->snap_count;
Mike Galbraithd94b9432009-05-25 09:57:56 +0200425
426 for (j = 0; j < nr_counters; j++)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300427 syme->count[j] = zero ? 0 : syme->count[j] * 7 / 8;
428 } else
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300429 list_remove_active_sym(syme);
Mike Galbraithd94b9432009-05-25 09:57:56 +0200430 }
431
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200432 puts(CONSOLE_CLEAR);
Ingo Molnar07800602009-04-20 15:00:56 +0200433
434 printf(
435"------------------------------------------------------------------------------\n");
Ingo Molnarf2521b62009-06-03 19:17:25 +0200436 printf( " PerfTop:%8.0f irqs/sec kernel:%4.1f%% [",
Ingo Molnar2debbc82009-06-05 14:29:10 +0200437 samples_per_sec,
438 100.0 - (100.0*((samples_per_sec-ksamples_per_sec)/samples_per_sec)));
Ingo Molnar07800602009-04-20 15:00:56 +0200439
Ingo Molnarcf1f4572009-06-05 13:27:02 +0200440 if (nr_counters == 1) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000441 printf("%Ld", (u64)attrs[0].sample_period);
Ingo Molnarcf1f4572009-06-05 13:27:02 +0200442 if (freq)
443 printf("Hz ");
444 else
445 printf(" ");
446 }
Ingo Molnar07800602009-04-20 15:00:56 +0200447
448 for (counter = 0; counter < nr_counters; counter++) {
449 if (counter)
450 printf("/");
451
452 printf("%s", event_name(counter));
453 }
454
455 printf( "], ");
456
Ingo Molnarb456bae2009-05-26 09:17:18 +0200457 if (target_pid != -1)
458 printf(" (target_pid: %d", target_pid);
Ingo Molnar07800602009-04-20 15:00:56 +0200459 else
460 printf(" (all");
461
462 if (profile_cpu != -1)
463 printf(", cpu: %d)\n", profile_cpu);
464 else {
Ingo Molnarb456bae2009-05-26 09:17:18 +0200465 if (target_pid != -1)
Ingo Molnar07800602009-04-20 15:00:56 +0200466 printf(")\n");
467 else
468 printf(", %d CPUs)\n", nr_cpus);
469 }
470
471 printf("------------------------------------------------------------------------------\n\n");
472
Mike Galbraith923c42c2009-07-22 20:36:03 +0200473 if (sym_filter_entry) {
474 show_details(sym_filter_entry);
475 return;
476 }
477
Ingo Molnar07800602009-04-20 15:00:56 +0200478 if (nr_counters == 1)
Ingo Molnar2debbc82009-06-05 14:29:10 +0200479 printf(" samples pcnt");
Ingo Molnar07800602009-04-20 15:00:56 +0200480 else
Ingo Molnar2debbc82009-06-05 14:29:10 +0200481 printf(" weight samples pcnt");
Ingo Molnar07800602009-04-20 15:00:56 +0200482
483 printf(" RIP kernel function\n"
Ingo Molnar2debbc82009-06-05 14:29:10 +0200484 " ______ _______ _____ ________________ _______________\n\n"
Ingo Molnar07800602009-04-20 15:00:56 +0200485 );
486
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300487 for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
488 struct sym_entry *syme = rb_entry(nd, struct sym_entry, rb_node);
489 struct symbol *sym = (struct symbol *)(syme + 1);
Ingo Molnar8fc03212009-06-04 15:19:47 +0200490 double pcnt;
Ingo Molnar07800602009-04-20 15:00:56 +0200491
Mike Galbraith923c42c2009-07-22 20:36:03 +0200492 if (++printed > print_entries || (int)syme->snap_count < count_filter)
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300493 continue;
Ingo Molnar07800602009-04-20 15:00:56 +0200494
Ingo Molnar2debbc82009-06-05 14:29:10 +0200495 pcnt = 100.0 - (100.0 * ((sum_ksamples - syme->snap_count) /
496 sum_ksamples));
Mike Galbraithd94b9432009-05-25 09:57:56 +0200497
498 if (nr_counters == 1)
Ingo Molnar2debbc82009-06-05 14:29:10 +0200499 printf("%20.2f - ", syme->weight);
Mike Galbraithd94b9432009-05-25 09:57:56 +0200500 else
Ingo Molnar2debbc82009-06-05 14:29:10 +0200501 printf("%9.1f %10ld - ", syme->weight, syme->snap_count);
Ingo Molnar8fc03212009-06-04 15:19:47 +0200502
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200503 percent_color_fprintf(stdout, "%4.1f%%", pcnt);
Mike Galbraith42976482009-07-02 08:09:46 +0200504 printf(" - %016llx : %s", sym->start, sym->name);
505 if (sym->module)
506 printf("\t[%s]", sym->module->name);
507 printf("\n");
Ingo Molnar07800602009-04-20 15:00:56 +0200508 }
Ingo Molnar07800602009-04-20 15:00:56 +0200509}
510
Mike Galbraith923c42c2009-07-22 20:36:03 +0200511static void prompt_integer(int *target, const char *msg)
512{
513 char *buf = malloc(0), *p;
514 size_t dummy = 0;
515 int tmp;
516
517 fprintf(stdout, "\n%s: ", msg);
518 if (getline(&buf, &dummy, stdin) < 0)
519 return;
520
521 p = strchr(buf, '\n');
522 if (p)
523 *p = 0;
524
525 p = buf;
526 while(*p) {
527 if (!isdigit(*p))
528 goto out_free;
529 p++;
530 }
531 tmp = strtoul(buf, NULL, 10);
532 *target = tmp;
533out_free:
534 free(buf);
535}
536
537static void prompt_percent(int *target, const char *msg)
538{
539 int tmp = 0;
540
541 prompt_integer(&tmp, msg);
542 if (tmp >= 0 && tmp <= 100)
543 *target = tmp;
544}
545
546static void prompt_symbol(struct sym_entry **target, const char *msg)
547{
548 char *buf = malloc(0), *p;
549 struct sym_entry *syme = *target, *n, *found = NULL;
550 size_t dummy = 0;
551
552 /* zero counters of active symbol */
553 if (syme) {
554 pthread_mutex_lock(&syme->source_lock);
555 __zero_source_counters(syme);
556 *target = NULL;
557 pthread_mutex_unlock(&syme->source_lock);
558 }
559
560 fprintf(stdout, "\n%s: ", msg);
561 if (getline(&buf, &dummy, stdin) < 0)
562 goto out_free;
563
564 p = strchr(buf, '\n');
565 if (p)
566 *p = 0;
567
568 pthread_mutex_lock(&active_symbols_lock);
569 syme = list_entry(active_symbols.next, struct sym_entry, node);
570 pthread_mutex_unlock(&active_symbols_lock);
571
572 list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
573 struct symbol *sym = (struct symbol *)(syme + 1);
574
575 if (!strcmp(buf, sym->name)) {
576 found = syme;
577 break;
578 }
579 }
580
581 if (!found) {
582 fprintf(stderr, "Sorry, %s is not active.\n", sym_filter);
583 sleep(1);
584 return;
585 } else
586 parse_source(found);
587
588out_free:
589 free(buf);
590}
591
592static void print_known_keys(void)
593{
594 fprintf(stdout, "\nknown keys:\n");
595 fprintf(stdout, "\t[d] select display delay.\n");
596 fprintf(stdout, "\t[e] select display entries (lines).\n");
597 fprintf(stdout, "\t[E] select annotation event counter.\n");
598 fprintf(stdout, "\t[f] select normal display count filter.\n");
599 fprintf(stdout, "\t[F] select annotation display count filter (percentage).\n");
600 fprintf(stdout, "\t[qQ] quit.\n");
601 fprintf(stdout, "\t[s] select annotation symbol and start annotation.\n");
602 fprintf(stdout, "\t[S] stop annotation, revert to normal display.\n");
603 fprintf(stdout, "\t[z] toggle event count zeroing.\n");
604}
605
606static void handle_keypress(int c)
607{
608 int once = 0;
609repeat:
610 switch (c) {
611 case 'd':
612 prompt_integer(&delay_secs, "Enter display delay");
613 break;
614 case 'e':
615 prompt_integer(&print_entries, "Enter display entries (lines)");
616 break;
617 case 'E':
618 if (nr_counters > 1) {
619 int i;
620
621 fprintf(stderr, "\nAvailable events:");
622 for (i = 0; i < nr_counters; i++)
623 fprintf(stderr, "\n\t%d %s", i, event_name(i));
624
625 prompt_integer(&sym_counter, "Enter details event counter");
626
627 if (sym_counter >= nr_counters) {
628 fprintf(stderr, "Sorry, no such event, using %s.\n", event_name(0));
629 sym_counter = 0;
630 sleep(1);
631 }
632 } else sym_counter = 0;
633 break;
634 case 'f':
635 prompt_integer(&count_filter, "Enter display event count filter");
636 break;
637 case 'F':
638 prompt_percent(&sym_pcnt_filter, "Enter details display event filter (percent)");
639 break;
640 case 'q':
641 case 'Q':
642 printf("exiting.\n");
643 exit(0);
644 case 's':
645 prompt_symbol(&sym_filter_entry, "Enter details symbol");
646 break;
647 case 'S':
648 if (!sym_filter_entry)
649 break;
650 else {
651 struct sym_entry *syme = sym_filter_entry;
652
653 pthread_mutex_lock(&syme->source_lock);
654 sym_filter_entry = NULL;
655 __zero_source_counters(syme);
656 pthread_mutex_unlock(&syme->source_lock);
657 }
658 break;
659 case 'z':
660 zero = ~zero;
661 break;
662 default: {
663 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
664 struct termios tc, save;
665
666 if (!once) {
667 print_known_keys();
668 once++;
669 }
670
671 tcgetattr(0, &save);
672 tc = save;
673 tc.c_lflag &= ~(ICANON | ECHO);
674 tc.c_cc[VMIN] = 0;
675 tc.c_cc[VTIME] = 0;
676 tcsetattr(0, TCSANOW, &tc);
677
678 poll(&stdin_poll, 1, -1);
679 c = getc(stdin);
680
681 tcsetattr(0, TCSAFLUSH, &save);
682 goto repeat;
683 }
684 }
685}
686
Ingo Molnarf37a2912009-07-01 12:37:06 +0200687static void *display_thread(void *arg __used)
Ingo Molnar07800602009-04-20 15:00:56 +0200688{
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200689 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
Mike Galbraith923c42c2009-07-22 20:36:03 +0200690 struct termios tc, save;
691 int delay_msecs, c;
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200692
Mike Galbraith923c42c2009-07-22 20:36:03 +0200693 tcgetattr(0, &save);
694 tc = save;
695 tc.c_lflag &= ~(ICANON | ECHO);
696 tc.c_cc[VMIN] = 0;
697 tc.c_cc[VTIME] = 0;
698repeat:
699 delay_msecs = delay_secs * 1000;
700 tcsetattr(0, TCSANOW, &tc);
701 /* trash return*/
702 getc(stdin);
Ingo Molnar07800602009-04-20 15:00:56 +0200703
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200704 do {
Ingo Molnar07800602009-04-20 15:00:56 +0200705 print_sym_table();
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200706 } while (!poll(&stdin_poll, 1, delay_msecs) == 1);
707
Mike Galbraith923c42c2009-07-22 20:36:03 +0200708 c = getc(stdin);
709 tcsetattr(0, TCSAFLUSH, &save);
710
711 handle_keypress(c);
712 goto repeat;
Ingo Molnar07800602009-04-20 15:00:56 +0200713
714 return NULL;
715}
716
Anton Blanchard2ab52082009-07-01 09:00:46 +1000717/* Tag samples to be skipped. */
Ingo Molnarf37a2912009-07-01 12:37:06 +0200718static const char *skip_symbols[] = {
Anton Blanchard2ab52082009-07-01 09:00:46 +1000719 "default_idle",
720 "cpu_idle",
721 "enter_idle",
722 "exit_idle",
723 "mwait_idle",
Arnaldo Carvalho de Melo59b90052009-07-26 19:06:19 -0300724 "mwait_idle_with_hints",
Anton Blanchard3a3393e2009-07-01 09:00:47 +1000725 "ppc64_runlatch_off",
726 "pseries_dedicated_idle_sleep",
Anton Blanchard2ab52082009-07-01 09:00:46 +1000727 NULL
728};
729
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300730static int symbol_filter(struct dso *self, struct symbol *sym)
Ingo Molnar07800602009-04-20 15:00:56 +0200731{
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300732 struct sym_entry *syme;
733 const char *name = sym->name;
Anton Blanchard2ab52082009-07-01 09:00:46 +1000734 int i;
Ingo Molnar07800602009-04-20 15:00:56 +0200735
Anton Blanchard3a3393e2009-07-01 09:00:47 +1000736 /*
737 * ppc64 uses function descriptors and appends a '.' to the
738 * start of every instruction address. Remove it.
739 */
740 if (name[0] == '.')
741 name++;
742
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300743 if (!strcmp(name, "_text") ||
744 !strcmp(name, "_etext") ||
745 !strcmp(name, "_sinittext") ||
746 !strncmp("init_module", name, 11) ||
747 !strncmp("cleanup_module", name, 14) ||
748 strstr(name, "_text_start") ||
749 strstr(name, "_text_end"))
Ingo Molnar07800602009-04-20 15:00:56 +0200750 return 1;
751
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300752 syme = dso__sym_priv(self, sym);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200753 pthread_mutex_init(&syme->source_lock, NULL);
754 if (!sym_filter_entry && sym_filter && !strcmp(name, sym_filter))
755 sym_filter_entry = syme;
756
Anton Blanchard2ab52082009-07-01 09:00:46 +1000757 for (i = 0; skip_symbols[i]; i++) {
758 if (!strcmp(skip_symbols[i], name)) {
759 syme->skip = 1;
760 break;
761 }
762 }
Ingo Molnar07800602009-04-20 15:00:56 +0200763
Ingo Molnar07800602009-04-20 15:00:56 +0200764 return 0;
765}
766
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300767static int parse_symbols(void)
Ingo Molnar07800602009-04-20 15:00:56 +0200768{
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300769 struct rb_node *node;
770 struct symbol *sym;
Mike Galbraith42976482009-07-02 08:09:46 +0200771 int modules = vmlinux ? 1 : 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200772
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300773 kernel_dso = dso__new("[kernel]", sizeof(struct sym_entry));
774 if (kernel_dso == NULL)
775 return -1;
Ingo Molnar07800602009-04-20 15:00:56 +0200776
Mike Galbraith42976482009-07-02 08:09:46 +0200777 if (dso__load_kernel(kernel_dso, vmlinux, symbol_filter, verbose, modules) <= 0)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300778 goto out_delete_dso;
Ingo Molnar07800602009-04-20 15:00:56 +0200779
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300780 node = rb_first(&kernel_dso->syms);
781 sym = rb_entry(node, struct symbol, rb_node);
782 min_ip = sym->start;
Ingo Molnar07800602009-04-20 15:00:56 +0200783
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300784 node = rb_last(&kernel_dso->syms);
785 sym = rb_entry(node, struct symbol, rb_node);
Mike Galbraithda417a72009-05-29 08:23:16 +0200786 max_ip = sym->end;
Ingo Molnar07800602009-04-20 15:00:56 +0200787
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300788 if (dump_symtab)
Mike Galbraitha3ec8d72009-05-29 06:46:46 +0200789 dso__fprintf(kernel_dso, stderr);
Ingo Molnar07800602009-04-20 15:00:56 +0200790
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300791 return 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200792
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300793out_delete_dso:
794 dso__delete(kernel_dso);
795 kernel_dso = NULL;
796 return -1;
Ingo Molnar07800602009-04-20 15:00:56 +0200797}
798
Ingo Molnar07800602009-04-20 15:00:56 +0200799/*
800 * Binary search in the histogram table and record the hit:
801 */
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000802static void record_ip(u64 ip, int counter)
Ingo Molnar07800602009-04-20 15:00:56 +0200803{
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300804 struct symbol *sym = dso__find_symbol(kernel_dso, ip);
Ingo Molnar07800602009-04-20 15:00:56 +0200805
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300806 if (sym != NULL) {
807 struct sym_entry *syme = dso__sym_priv(kernel_dso, sym);
Ingo Molnar07800602009-04-20 15:00:56 +0200808
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300809 if (!syme->skip) {
810 syme->count[counter]++;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200811 record_precise_ip(syme, counter, ip);
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300812 pthread_mutex_lock(&active_symbols_lock);
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300813 if (list_empty(&syme->node) || !syme->node.next)
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300814 __list_insert_active_sym(syme);
815 pthread_mutex_unlock(&active_symbols_lock);
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300816 return;
Ingo Molnar07800602009-04-20 15:00:56 +0200817 }
Ingo Molnar07800602009-04-20 15:00:56 +0200818 }
819
Ingo Molnar2debbc82009-06-05 14:29:10 +0200820 samples--;
Ingo Molnar07800602009-04-20 15:00:56 +0200821}
822
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +0200823static void process_event(u64 ip, int counter, int user)
Ingo Molnar07800602009-04-20 15:00:56 +0200824{
Ingo Molnar2debbc82009-06-05 14:29:10 +0200825 samples++;
Ingo Molnar07800602009-04-20 15:00:56 +0200826
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +0200827 if (user) {
Ingo Molnar2debbc82009-06-05 14:29:10 +0200828 userspace_samples++;
Ingo Molnar07800602009-04-20 15:00:56 +0200829 return;
830 }
831
832 record_ip(ip, counter);
833}
834
Ingo Molnar07800602009-04-20 15:00:56 +0200835struct mmap_data {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200836 int counter;
837 void *base;
Ingo Molnarf37a2912009-07-01 12:37:06 +0200838 int mask;
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200839 unsigned int prev;
Ingo Molnar07800602009-04-20 15:00:56 +0200840};
841
842static unsigned int mmap_read_head(struct mmap_data *md)
843{
844 struct perf_counter_mmap_page *pc = md->base;
845 int head;
846
847 head = pc->data_head;
848 rmb();
849
850 return head;
851}
852
853struct timeval last_read, this_read;
854
Frederic Weisbecker2f011902009-06-06 23:10:43 +0200855static void mmap_read_counter(struct mmap_data *md)
Ingo Molnar07800602009-04-20 15:00:56 +0200856{
857 unsigned int head = mmap_read_head(md);
858 unsigned int old = md->prev;
859 unsigned char *data = md->base + page_size;
860 int diff;
861
862 gettimeofday(&this_read, NULL);
863
864 /*
865 * If we're further behind than half the buffer, there's a chance
Ingo Molnar2debbc82009-06-05 14:29:10 +0200866 * the writer will bite our tail and mess up the samples under us.
Ingo Molnar07800602009-04-20 15:00:56 +0200867 *
868 * If we somehow ended up ahead of the head, we got messed up.
869 *
870 * In either case, truncate and restart at head.
871 */
872 diff = head - old;
873 if (diff > md->mask / 2 || diff < 0) {
874 struct timeval iv;
875 unsigned long msecs;
876
877 timersub(&this_read, &last_read, &iv);
878 msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
879
880 fprintf(stderr, "WARNING: failed to keep up with mmap data."
881 " Last read %lu msecs ago.\n", msecs);
882
883 /*
884 * head points to a known good entry, start there.
885 */
886 old = head;
887 }
888
889 last_read = this_read;
890
891 for (; old != head;) {
892 struct ip_event {
893 struct perf_event_header header;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000894 u64 ip;
895 u32 pid, target_pid;
Ingo Molnar07800602009-04-20 15:00:56 +0200896 };
897 struct mmap_event {
898 struct perf_event_header header;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000899 u32 pid, target_pid;
900 u64 start;
901 u64 len;
902 u64 pgoff;
Ingo Molnar07800602009-04-20 15:00:56 +0200903 char filename[PATH_MAX];
904 };
905
906 typedef union event_union {
907 struct perf_event_header header;
908 struct ip_event ip;
909 struct mmap_event mmap;
910 } event_t;
911
912 event_t *event = (event_t *)&data[old & md->mask];
913
914 event_t event_copy;
915
Ingo Molnar6f06ccb2009-04-20 15:22:22 +0200916 size_t size = event->header.size;
Ingo Molnar07800602009-04-20 15:00:56 +0200917
918 /*
919 * Event straddles the mmap boundary -- header should always
920 * be inside due to u64 alignment of output.
921 */
922 if ((old & md->mask) + size != ((old + size) & md->mask)) {
923 unsigned int offset = old;
924 unsigned int len = min(sizeof(*event), size), cpy;
925 void *dst = &event_copy;
926
927 do {
928 cpy = min(md->mask + 1 - (offset & md->mask), len);
929 memcpy(dst, &data[offset & md->mask], cpy);
930 offset += cpy;
931 dst += cpy;
932 len -= cpy;
933 } while (len);
934
935 event = &event_copy;
936 }
937
938 old += size;
939
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +0200940 if (event->header.type == PERF_EVENT_SAMPLE) {
941 int user =
942 (event->header.misc & PERF_EVENT_MISC_CPUMODE_MASK) == PERF_EVENT_MISC_USER;
943 process_event(event->ip.ip, md->counter, user);
Ingo Molnar07800602009-04-20 15:00:56 +0200944 }
945 }
946
947 md->prev = old;
948}
949
Mike Galbraithc2990a22009-05-24 08:35:49 +0200950static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
951static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
952
Frederic Weisbecker2f011902009-06-06 23:10:43 +0200953static void mmap_read(void)
954{
955 int i, counter;
956
957 for (i = 0; i < nr_cpus; i++) {
958 for (counter = 0; counter < nr_counters; counter++)
959 mmap_read_counter(&mmap_array[i][counter]);
960 }
961}
962
Ingo Molnar716c69f2009-06-07 17:31:52 +0200963int nr_poll;
964int group_fd;
965
966static void start_counter(int i, int counter)
Ingo Molnar07800602009-04-20 15:00:56 +0200967{
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200968 struct perf_counter_attr *attr;
Mike Galbraith0fdc7e62009-07-21 10:30:36 +0200969 int cpu;
Ingo Molnar716c69f2009-06-07 17:31:52 +0200970
971 cpu = profile_cpu;
972 if (target_pid == -1 && profile_cpu == -1)
973 cpu = i;
974
975 attr = attrs + counter;
976
977 attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
978 attr->freq = freq;
Mike Galbraith0fdc7e62009-07-21 10:30:36 +0200979 attr->inherit = (cpu < 0) && inherit;
Ingo Molnar716c69f2009-06-07 17:31:52 +0200980
981try_again:
982 fd[i][counter] = sys_perf_counter_open(attr, target_pid, cpu, group_fd, 0);
983
984 if (fd[i][counter] < 0) {
985 int err = errno;
986
Ingo Molnar716c69f2009-06-07 17:31:52 +0200987 if (err == EPERM)
Ingo Molnar3da297a2009-06-07 17:39:02 +0200988 die("No permission - are you root?\n");
Ingo Molnar716c69f2009-06-07 17:31:52 +0200989 /*
990 * If it's cycles then fall back to hrtimer
991 * based cpu-clock-tick sw counter, which
992 * is always available even if no PMU support:
993 */
994 if (attr->type == PERF_TYPE_HARDWARE
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200995 && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
Ingo Molnar716c69f2009-06-07 17:31:52 +0200996
Ingo Molnar3da297a2009-06-07 17:39:02 +0200997 if (verbose)
998 warning(" ... trying to fall back to cpu-clock-ticks\n");
999
Ingo Molnar716c69f2009-06-07 17:31:52 +02001000 attr->type = PERF_TYPE_SOFTWARE;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02001001 attr->config = PERF_COUNT_SW_CPU_CLOCK;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001002 goto try_again;
1003 }
Ingo Molnar30c806a2009-06-07 17:46:24 +02001004 printf("\n");
1005 error("perfcounter syscall returned with %d (%s)\n",
1006 fd[i][counter], strerror(err));
1007 die("No CONFIG_PERF_COUNTERS=y kernel support configured?\n");
Ingo Molnar716c69f2009-06-07 17:31:52 +02001008 exit(-1);
1009 }
1010 assert(fd[i][counter] >= 0);
1011 fcntl(fd[i][counter], F_SETFL, O_NONBLOCK);
1012
1013 /*
1014 * First counter acts as the group leader:
1015 */
1016 if (group && group_fd == -1)
1017 group_fd = fd[i][counter];
1018
1019 event_array[nr_poll].fd = fd[i][counter];
1020 event_array[nr_poll].events = POLLIN;
1021 nr_poll++;
1022
1023 mmap_array[i][counter].counter = counter;
1024 mmap_array[i][counter].prev = 0;
1025 mmap_array[i][counter].mask = mmap_pages*page_size - 1;
1026 mmap_array[i][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
1027 PROT_READ, MAP_SHARED, fd[i][counter], 0);
1028 if (mmap_array[i][counter].base == MAP_FAILED)
1029 die("failed to mmap with %d (%s)\n", errno, strerror(errno));
1030}
1031
1032static int __cmd_top(void)
1033{
1034 pthread_t thread;
1035 int i, counter;
Ingo Molnar07800602009-04-20 15:00:56 +02001036 int ret;
1037
Ingo Molnar07800602009-04-20 15:00:56 +02001038 for (i = 0; i < nr_cpus; i++) {
1039 group_fd = -1;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001040 for (counter = 0; counter < nr_counters; counter++)
1041 start_counter(i, counter);
Ingo Molnar07800602009-04-20 15:00:56 +02001042 }
1043
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001044 /* Wait for a minimal set of events before starting the snapshot */
1045 poll(event_array, nr_poll, 100);
1046
1047 mmap_read();
1048
Ingo Molnar07800602009-04-20 15:00:56 +02001049 if (pthread_create(&thread, NULL, display_thread, NULL)) {
1050 printf("Could not create display thread.\n");
1051 exit(-1);
1052 }
1053
1054 if (realtime_prio) {
1055 struct sched_param param;
1056
1057 param.sched_priority = realtime_prio;
1058 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
1059 printf("Could not set realtime priority.\n");
1060 exit(-1);
1061 }
1062 }
1063
1064 while (1) {
Ingo Molnar2debbc82009-06-05 14:29:10 +02001065 int hits = samples;
Ingo Molnar07800602009-04-20 15:00:56 +02001066
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001067 mmap_read();
Ingo Molnar07800602009-04-20 15:00:56 +02001068
Ingo Molnar2debbc82009-06-05 14:29:10 +02001069 if (hits == samples)
Ingo Molnar07800602009-04-20 15:00:56 +02001070 ret = poll(event_array, nr_poll, 100);
1071 }
1072
1073 return 0;
1074}
Ingo Molnarb456bae2009-05-26 09:17:18 +02001075
1076static const char * const top_usage[] = {
1077 "perf top [<options>]",
1078 NULL
1079};
1080
Ingo Molnarb456bae2009-05-26 09:17:18 +02001081static const struct option options[] = {
1082 OPT_CALLBACK('e', "event", NULL, "event",
Thomas Gleixner86847b62009-06-06 12:24:17 +02001083 "event selector. use 'perf list' to list available events",
1084 parse_events),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001085 OPT_INTEGER('c', "count", &default_interval,
1086 "event period to sample"),
1087 OPT_INTEGER('p', "pid", &target_pid,
1088 "profile events on existing pid"),
1089 OPT_BOOLEAN('a', "all-cpus", &system_wide,
1090 "system-wide collection from all CPUs"),
1091 OPT_INTEGER('C', "CPU", &profile_cpu,
1092 "CPU to profile on"),
Mike Galbraith42976482009-07-02 08:09:46 +02001093 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001094 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
1095 "number of mmap data pages"),
1096 OPT_INTEGER('r', "realtime", &realtime_prio,
1097 "collect data with this RT SCHED_FIFO priority"),
Mike Galbraithdb20c002009-05-26 15:25:34 +02001098 OPT_INTEGER('d', "delay", &delay_secs,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001099 "number of seconds to delay between refreshes"),
1100 OPT_BOOLEAN('D', "dump-symtab", &dump_symtab,
1101 "dump the symbol table used for profiling"),
Ingo Molnar6e53cdf2009-06-04 08:53:05 +02001102 OPT_INTEGER('f', "count-filter", &count_filter,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001103 "only display functions with more events than this"),
1104 OPT_BOOLEAN('g', "group", &group,
1105 "put the counters into a counter group"),
Mike Galbraith0fdc7e62009-07-21 10:30:36 +02001106 OPT_BOOLEAN('i', "inherit", &inherit,
1107 "child tasks inherit counters"),
Mike Galbraith923c42c2009-07-22 20:36:03 +02001108 OPT_STRING('s', "sym-annotate", &sym_filter, "symbol name",
1109 "symbol to annotate - requires -k option"),
Anton Blanchard1f208ea2009-07-01 09:00:44 +10001110 OPT_BOOLEAN('z', "zero", &zero,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001111 "zero history across updates"),
Ingo Molnar6e53cdf2009-06-04 08:53:05 +02001112 OPT_INTEGER('F', "freq", &freq,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001113 "profile at this frequency"),
Ingo Molnar6e53cdf2009-06-04 08:53:05 +02001114 OPT_INTEGER('E', "entries", &print_entries,
1115 "display this many functions"),
Ingo Molnar3da297a2009-06-07 17:39:02 +02001116 OPT_BOOLEAN('v', "verbose", &verbose,
1117 "be more verbose (show counter open errors, etc)"),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001118 OPT_END()
1119};
1120
Ingo Molnarf37a2912009-07-01 12:37:06 +02001121int cmd_top(int argc, const char **argv, const char *prefix __used)
Ingo Molnarb456bae2009-05-26 09:17:18 +02001122{
1123 int counter;
1124
Mike Galbraith42976482009-07-02 08:09:46 +02001125 symbol__init();
1126
Ingo Molnarb456bae2009-05-26 09:17:18 +02001127 page_size = sysconf(_SC_PAGE_SIZE);
1128
Ingo Molnarb456bae2009-05-26 09:17:18 +02001129 argc = parse_options(argc, argv, options, top_usage, 0);
1130 if (argc)
1131 usage_with_options(top_usage, options);
1132
1133 if (freq) {
1134 default_interval = freq;
1135 freq = 1;
1136 }
1137
1138 /* CPU and PID are mutually exclusive */
1139 if (target_pid != -1 && profile_cpu != -1) {
1140 printf("WARNING: PID switch overriding CPU\n");
1141 sleep(1);
1142 profile_cpu = -1;
1143 }
1144
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001145 if (!nr_counters)
Ingo Molnarb456bae2009-05-26 09:17:18 +02001146 nr_counters = 1;
Ingo Molnarb456bae2009-05-26 09:17:18 +02001147
Frederic Weisbecker2f335a02009-06-05 19:31:01 +02001148 if (delay_secs < 1)
1149 delay_secs = 1;
1150
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001151 parse_symbols();
Mike Galbraith923c42c2009-07-22 20:36:03 +02001152 parse_source(sym_filter_entry);
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001153
1154 /*
1155 * Fill in the ones not specifically initialized via -c:
1156 */
Ingo Molnarb456bae2009-05-26 09:17:18 +02001157 for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001158 if (attrs[counter].sample_period)
Ingo Molnarb456bae2009-05-26 09:17:18 +02001159 continue;
1160
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001161 attrs[counter].sample_period = default_interval;
Ingo Molnarb456bae2009-05-26 09:17:18 +02001162 }
1163
1164 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
1165 assert(nr_cpus <= MAX_NR_CPUS);
1166 assert(nr_cpus >= 0);
1167
1168 if (target_pid != -1 || profile_cpu != -1)
1169 nr_cpus = 1;
1170
Ingo Molnarb456bae2009-05-26 09:17:18 +02001171 return __cmd_top();
1172}