blob: 9a6dbbff9a9f0b80c28b737036bcb2078b481572 [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 Molnar07800602009-04-20 15:00:56 +020071
Ingo Molnar07800602009-04-20 15:00:56 +020072static int delay_secs = 2;
73static int zero;
74static int dump_symtab;
75
Ingo Molnar07800602009-04-20 15:00:56 +020076/*
Mike Galbraith923c42c2009-07-22 20:36:03 +020077 * Source
78 */
79
80struct source_line {
81 u64 eip;
82 unsigned long count[MAX_COUNTERS];
83 char *line;
84 struct source_line *next;
85};
86
87static char *sym_filter = NULL;
88struct sym_entry *sym_filter_entry = NULL;
89static int sym_pcnt_filter = 5;
90static int sym_counter = 0;
Mike Galbraith46ab9762009-07-24 10:09:50 +020091static int display_weighted = -1;
Mike Galbraith923c42c2009-07-22 20:36:03 +020092
93/*
Ingo Molnar07800602009-04-20 15:00:56 +020094 * Symbols
95 */
96
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100097static u64 min_ip;
98static u64 max_ip = -1ll;
Ingo Molnar07800602009-04-20 15:00:56 +020099
100struct sym_entry {
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300101 struct rb_node rb_node;
102 struct list_head node;
Ingo Molnar07800602009-04-20 15:00:56 +0200103 unsigned long count[MAX_COUNTERS];
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300104 unsigned long snap_count;
105 double weight;
Ingo Molnar07800602009-04-20 15:00:56 +0200106 int skip;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200107 struct source_line *source;
108 struct source_line *lines;
109 struct source_line **lines_tail;
110 pthread_mutex_t source_lock;
Ingo Molnar07800602009-04-20 15:00:56 +0200111};
112
Mike Galbraith923c42c2009-07-22 20:36:03 +0200113/*
114 * Source functions
115 */
116
117static void parse_source(struct sym_entry *syme)
118{
119 struct symbol *sym;
120 struct module *module;
121 struct section *section = NULL;
122 FILE *file;
123 char command[PATH_MAX*2], *path = vmlinux;
124 u64 start, end, len;
125
126 if (!syme)
127 return;
128
129 if (syme->lines) {
130 pthread_mutex_lock(&syme->source_lock);
131 goto out_assign;
132 }
133
134 sym = (struct symbol *)(syme + 1);
135 module = sym->module;
136
137 if (module)
138 path = module->path;
139 if (!path)
140 return;
141
142 start = sym->obj_start;
143 if (!start)
144 start = sym->start;
145
146 if (module) {
147 section = module->sections->find_section(module->sections, ".text");
148 if (section)
149 start -= section->vma;
150 }
151
152 end = start + sym->end - sym->start + 1;
153 len = sym->end - sym->start;
154
155 sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s", start, end, path);
156
157 file = popen(command, "r");
158 if (!file)
159 return;
160
161 pthread_mutex_lock(&syme->source_lock);
162 syme->lines_tail = &syme->lines;
163 while (!feof(file)) {
164 struct source_line *src;
165 size_t dummy = 0;
166 char *c;
167
168 src = malloc(sizeof(struct source_line));
169 assert(src != NULL);
170 memset(src, 0, sizeof(struct source_line));
171
172 if (getline(&src->line, &dummy, file) < 0)
173 break;
174 if (!src->line)
175 break;
176
177 c = strchr(src->line, '\n');
178 if (c)
179 *c = 0;
180
181 src->next = NULL;
182 *syme->lines_tail = src;
183 syme->lines_tail = &src->next;
184
185 if (strlen(src->line)>8 && src->line[8] == ':') {
186 src->eip = strtoull(src->line, NULL, 16);
187 if (section)
188 src->eip += section->vma;
189 }
190 if (strlen(src->line)>8 && src->line[16] == ':') {
191 src->eip = strtoull(src->line, NULL, 16);
192 if (section)
193 src->eip += section->vma;
194 }
195 }
196 pclose(file);
197out_assign:
198 sym_filter_entry = syme;
199 pthread_mutex_unlock(&syme->source_lock);
200}
201
202static void __zero_source_counters(struct sym_entry *syme)
203{
204 int i;
205 struct source_line *line;
206
207 line = syme->lines;
208 while (line) {
209 for (i = 0; i < nr_counters; i++)
210 line->count[i] = 0;
211 line = line->next;
212 }
213}
214
215static void record_precise_ip(struct sym_entry *syme, int counter, u64 ip)
216{
217 struct source_line *line;
218
219 if (syme != sym_filter_entry)
220 return;
221
222 if (pthread_mutex_trylock(&syme->source_lock))
223 return;
224
225 if (!syme->source)
226 goto out_unlock;
227
228 for (line = syme->lines; line; line = line->next) {
229 if (line->eip == ip) {
230 line->count[counter]++;
231 break;
232 }
233 if (line->eip > ip)
234 break;
235 }
236out_unlock:
237 pthread_mutex_unlock(&syme->source_lock);
238}
239
240static void lookup_sym_source(struct sym_entry *syme)
241{
242 struct symbol *symbol = (struct symbol *)(syme + 1);
243 struct source_line *line;
244 char pattern[PATH_MAX];
245 char *idx;
246
247 sprintf(pattern, "<%s>:", symbol->name);
248
249 if (symbol->module) {
250 idx = strstr(pattern, "\t");
251 if (idx)
252 *idx = 0;
253 }
254
255 pthread_mutex_lock(&syme->source_lock);
256 for (line = syme->lines; line; line = line->next) {
257 if (strstr(line->line, pattern)) {
258 syme->source = line;
259 break;
260 }
261 }
262 pthread_mutex_unlock(&syme->source_lock);
263}
264
265static void show_lines(struct source_line *queue, int count, int total)
266{
267 int i;
268 struct source_line *line;
269
270 line = queue;
271 for (i = 0; i < count; i++) {
272 float pcnt = 100.0*(float)line->count[sym_counter]/(float)total;
273
274 printf("%8li %4.1f%%\t%s\n", line->count[sym_counter], pcnt, line->line);
275 line = line->next;
276 }
277}
278
279#define TRACE_COUNT 3
280
281static void show_details(struct sym_entry *syme)
282{
283 struct symbol *symbol;
284 struct source_line *line;
285 struct source_line *line_queue = NULL;
286 int displayed = 0;
287 int line_queue_count = 0, total = 0, more = 0;
288
289 if (!syme)
290 return;
291
292 if (!syme->source)
293 lookup_sym_source(syme);
294
295 if (!syme->source)
296 return;
297
298 symbol = (struct symbol *)(syme + 1);
299 printf("Showing %s for %s\n", event_name(sym_counter), symbol->name);
300 printf(" Events Pcnt (>=%d%%)\n", sym_pcnt_filter);
301
302 pthread_mutex_lock(&syme->source_lock);
303 line = syme->source;
304 while (line) {
305 total += line->count[sym_counter];
306 line = line->next;
307 }
308
309 line = syme->source;
310 while (line) {
311 float pcnt = 0.0;
312
313 if (!line_queue_count)
314 line_queue = line;
315 line_queue_count++;
316
317 if (line->count[sym_counter])
318 pcnt = 100.0 * line->count[sym_counter] / (float)total;
319 if (pcnt >= (float)sym_pcnt_filter) {
320 if (displayed <= print_entries)
321 show_lines(line_queue, line_queue_count, total);
322 else more++;
323 displayed += line_queue_count;
324 line_queue_count = 0;
325 line_queue = NULL;
326 } else if (line_queue_count > TRACE_COUNT) {
327 line_queue = line_queue->next;
328 line_queue_count--;
329 }
330
331 line->count[sym_counter] = zero ? 0 : line->count[sym_counter] * 7 / 8;
332 line = line->next;
333 }
334 pthread_mutex_unlock(&syme->source_lock);
335 if (more)
336 printf("%d lines not displayed, maybe increase display entries [e]\n", more);
337}
Ingo Molnar07800602009-04-20 15:00:56 +0200338
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300339/*
340 * Symbols will be added here in record_ip and will get out
341 * after decayed.
342 */
343static LIST_HEAD(active_symbols);
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300344static pthread_mutex_t active_symbols_lock = PTHREAD_MUTEX_INITIALIZER;
Ingo Molnar07800602009-04-20 15:00:56 +0200345
Ingo Molnar07800602009-04-20 15:00:56 +0200346/*
347 * Ordering weight: count-1 * count-2 * ... / count-n
348 */
349static double sym_weight(const struct sym_entry *sym)
350{
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300351 double weight = sym->snap_count;
Ingo Molnar07800602009-04-20 15:00:56 +0200352 int counter;
353
Mike Galbraith46ab9762009-07-24 10:09:50 +0200354 if (!display_weighted)
355 return weight;
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;
Mike Galbraith46ab9762009-07-24 10:09:50 +0200404 int counter, snap = !display_weighted ? sym_counter : 0;
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) {
Mike Galbraith46ab9762009-07-24 10:09:50 +0200420 syme->snap_count = syme->count[snap];
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300421 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
Mike Galbraith46ab9762009-07-24 10:09:50 +0200440 if (nr_counters == 1 || !display_weighted) {
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
Mike Galbraith46ab9762009-07-24 10:09:50 +0200448 if (!display_weighted)
449 printf("%s", event_name(sym_counter));
450 else for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnar07800602009-04-20 15:00:56 +0200451 if (counter)
452 printf("/");
453
454 printf("%s", event_name(counter));
455 }
456
457 printf( "], ");
458
Ingo Molnarb456bae2009-05-26 09:17:18 +0200459 if (target_pid != -1)
460 printf(" (target_pid: %d", target_pid);
Ingo Molnar07800602009-04-20 15:00:56 +0200461 else
462 printf(" (all");
463
464 if (profile_cpu != -1)
465 printf(", cpu: %d)\n", profile_cpu);
466 else {
Ingo Molnarb456bae2009-05-26 09:17:18 +0200467 if (target_pid != -1)
Ingo Molnar07800602009-04-20 15:00:56 +0200468 printf(")\n");
469 else
470 printf(", %d CPUs)\n", nr_cpus);
471 }
472
473 printf("------------------------------------------------------------------------------\n\n");
474
Mike Galbraith923c42c2009-07-22 20:36:03 +0200475 if (sym_filter_entry) {
476 show_details(sym_filter_entry);
477 return;
478 }
479
Ingo Molnar07800602009-04-20 15:00:56 +0200480 if (nr_counters == 1)
Ingo Molnar2debbc82009-06-05 14:29:10 +0200481 printf(" samples pcnt");
Ingo Molnar07800602009-04-20 15:00:56 +0200482 else
Ingo Molnar2debbc82009-06-05 14:29:10 +0200483 printf(" weight samples pcnt");
Ingo Molnar07800602009-04-20 15:00:56 +0200484
485 printf(" RIP kernel function\n"
Ingo Molnar2debbc82009-06-05 14:29:10 +0200486 " ______ _______ _____ ________________ _______________\n\n"
Ingo Molnar07800602009-04-20 15:00:56 +0200487 );
488
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300489 for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
490 struct sym_entry *syme = rb_entry(nd, struct sym_entry, rb_node);
491 struct symbol *sym = (struct symbol *)(syme + 1);
Ingo Molnar8fc03212009-06-04 15:19:47 +0200492 double pcnt;
Ingo Molnar07800602009-04-20 15:00:56 +0200493
Mike Galbraith923c42c2009-07-22 20:36:03 +0200494 if (++printed > print_entries || (int)syme->snap_count < count_filter)
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300495 continue;
Ingo Molnar07800602009-04-20 15:00:56 +0200496
Ingo Molnar2debbc82009-06-05 14:29:10 +0200497 pcnt = 100.0 - (100.0 * ((sum_ksamples - syme->snap_count) /
498 sum_ksamples));
Mike Galbraithd94b9432009-05-25 09:57:56 +0200499
Mike Galbraith46ab9762009-07-24 10:09:50 +0200500 if (nr_counters == 1 || !display_weighted)
Ingo Molnar2debbc82009-06-05 14:29:10 +0200501 printf("%20.2f - ", syme->weight);
Mike Galbraithd94b9432009-05-25 09:57:56 +0200502 else
Ingo Molnar2debbc82009-06-05 14:29:10 +0200503 printf("%9.1f %10ld - ", syme->weight, syme->snap_count);
Ingo Molnar8fc03212009-06-04 15:19:47 +0200504
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200505 percent_color_fprintf(stdout, "%4.1f%%", pcnt);
Mike Galbraith42976482009-07-02 08:09:46 +0200506 printf(" - %016llx : %s", sym->start, sym->name);
507 if (sym->module)
508 printf("\t[%s]", sym->module->name);
509 printf("\n");
Ingo Molnar07800602009-04-20 15:00:56 +0200510 }
Ingo Molnar07800602009-04-20 15:00:56 +0200511}
512
Mike Galbraith923c42c2009-07-22 20:36:03 +0200513static void prompt_integer(int *target, const char *msg)
514{
515 char *buf = malloc(0), *p;
516 size_t dummy = 0;
517 int tmp;
518
519 fprintf(stdout, "\n%s: ", msg);
520 if (getline(&buf, &dummy, stdin) < 0)
521 return;
522
523 p = strchr(buf, '\n');
524 if (p)
525 *p = 0;
526
527 p = buf;
528 while(*p) {
529 if (!isdigit(*p))
530 goto out_free;
531 p++;
532 }
533 tmp = strtoul(buf, NULL, 10);
534 *target = tmp;
535out_free:
536 free(buf);
537}
538
539static void prompt_percent(int *target, const char *msg)
540{
541 int tmp = 0;
542
543 prompt_integer(&tmp, msg);
544 if (tmp >= 0 && tmp <= 100)
545 *target = tmp;
546}
547
548static void prompt_symbol(struct sym_entry **target, const char *msg)
549{
550 char *buf = malloc(0), *p;
551 struct sym_entry *syme = *target, *n, *found = NULL;
552 size_t dummy = 0;
553
554 /* zero counters of active symbol */
555 if (syme) {
556 pthread_mutex_lock(&syme->source_lock);
557 __zero_source_counters(syme);
558 *target = NULL;
559 pthread_mutex_unlock(&syme->source_lock);
560 }
561
562 fprintf(stdout, "\n%s: ", msg);
563 if (getline(&buf, &dummy, stdin) < 0)
564 goto out_free;
565
566 p = strchr(buf, '\n');
567 if (p)
568 *p = 0;
569
570 pthread_mutex_lock(&active_symbols_lock);
571 syme = list_entry(active_symbols.next, struct sym_entry, node);
572 pthread_mutex_unlock(&active_symbols_lock);
573
574 list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
575 struct symbol *sym = (struct symbol *)(syme + 1);
576
577 if (!strcmp(buf, sym->name)) {
578 found = syme;
579 break;
580 }
581 }
582
583 if (!found) {
584 fprintf(stderr, "Sorry, %s is not active.\n", sym_filter);
585 sleep(1);
586 return;
587 } else
588 parse_source(found);
589
590out_free:
591 free(buf);
592}
593
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200594static void print_mapped_keys(void)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200595{
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200596 char *name = NULL;
597
598 if (sym_filter_entry) {
599 struct symbol *sym = (struct symbol *)(sym_filter_entry+1);
600 name = sym->name;
601 }
602
603 fprintf(stdout, "\nMapped keys:\n");
604 fprintf(stdout, "\t[d] display refresh delay. \t(%d)\n", delay_secs);
605 fprintf(stdout, "\t[e] display entries (lines). \t(%d)\n", print_entries);
606
607 if (nr_counters > 1)
608 fprintf(stdout, "\t[E] active event counter. \t(%s)\n", event_name(sym_counter));
609
610 fprintf(stdout, "\t[f] profile display filter (count). \t(%d)\n", count_filter);
611
612 if (vmlinux) {
613 fprintf(stdout, "\t[F] annotate display filter (percent). \t(%d%%)\n", sym_pcnt_filter);
614 fprintf(stdout, "\t[s] annotate symbol. \t(%s)\n", name?: "NULL");
615 fprintf(stdout, "\t[S] stop annotation.\n");
616 }
617
618 if (nr_counters > 1)
619 fprintf(stdout, "\t[w] toggle display weighted/count[E]r. \t(%d)\n", display_weighted ? 1 : 0);
620
Mike Galbraith46ab9762009-07-24 10:09:50 +0200621 fprintf(stdout, "\t[z] toggle sample zeroing. \t(%d)\n", zero ? 1 : 0);
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200622 fprintf(stdout, "\t[qQ] quit.\n");
623}
624
625static int key_mapped(int c)
626{
627 switch (c) {
628 case 'd':
629 case 'e':
630 case 'f':
631 case 'z':
632 case 'q':
633 case 'Q':
634 return 1;
635 case 'E':
636 case 'w':
637 return nr_counters > 1 ? 1 : 0;
638 case 'F':
639 case 's':
640 case 'S':
641 return vmlinux ? 1 : 0;
642 }
643
644 return 0;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200645}
646
647static void handle_keypress(int c)
648{
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200649 if (!key_mapped(c)) {
650 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
651 struct termios tc, save;
652
653 print_mapped_keys();
654 fprintf(stdout, "\nEnter selection, or unmapped key to continue: ");
655 fflush(stdout);
656
657 tcgetattr(0, &save);
658 tc = save;
659 tc.c_lflag &= ~(ICANON | ECHO);
660 tc.c_cc[VMIN] = 0;
661 tc.c_cc[VTIME] = 0;
662 tcsetattr(0, TCSANOW, &tc);
663
664 poll(&stdin_poll, 1, -1);
665 c = getc(stdin);
666
667 tcsetattr(0, TCSAFLUSH, &save);
668 if (!key_mapped(c))
669 return;
670 }
671
Mike Galbraith923c42c2009-07-22 20:36:03 +0200672 switch (c) {
673 case 'd':
674 prompt_integer(&delay_secs, "Enter display delay");
675 break;
676 case 'e':
677 prompt_integer(&print_entries, "Enter display entries (lines)");
678 break;
679 case 'E':
680 if (nr_counters > 1) {
681 int i;
682
683 fprintf(stderr, "\nAvailable events:");
684 for (i = 0; i < nr_counters; i++)
685 fprintf(stderr, "\n\t%d %s", i, event_name(i));
686
687 prompt_integer(&sym_counter, "Enter details event counter");
688
689 if (sym_counter >= nr_counters) {
690 fprintf(stderr, "Sorry, no such event, using %s.\n", event_name(0));
691 sym_counter = 0;
692 sleep(1);
693 }
694 } else sym_counter = 0;
695 break;
696 case 'f':
697 prompt_integer(&count_filter, "Enter display event count filter");
698 break;
699 case 'F':
700 prompt_percent(&sym_pcnt_filter, "Enter details display event filter (percent)");
701 break;
702 case 'q':
703 case 'Q':
704 printf("exiting.\n");
705 exit(0);
706 case 's':
707 prompt_symbol(&sym_filter_entry, "Enter details symbol");
708 break;
709 case 'S':
710 if (!sym_filter_entry)
711 break;
712 else {
713 struct sym_entry *syme = sym_filter_entry;
714
715 pthread_mutex_lock(&syme->source_lock);
716 sym_filter_entry = NULL;
717 __zero_source_counters(syme);
718 pthread_mutex_unlock(&syme->source_lock);
719 }
720 break;
Mike Galbraith46ab9762009-07-24 10:09:50 +0200721 case 'w':
722 display_weighted = ~display_weighted;
723 break;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200724 case 'z':
725 zero = ~zero;
726 break;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200727 }
728}
729
Ingo Molnarf37a2912009-07-01 12:37:06 +0200730static void *display_thread(void *arg __used)
Ingo Molnar07800602009-04-20 15:00:56 +0200731{
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200732 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
Mike Galbraith923c42c2009-07-22 20:36:03 +0200733 struct termios tc, save;
734 int delay_msecs, c;
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200735
Mike Galbraith923c42c2009-07-22 20:36:03 +0200736 tcgetattr(0, &save);
737 tc = save;
738 tc.c_lflag &= ~(ICANON | ECHO);
739 tc.c_cc[VMIN] = 0;
740 tc.c_cc[VTIME] = 0;
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200741
Mike Galbraith923c42c2009-07-22 20:36:03 +0200742repeat:
743 delay_msecs = delay_secs * 1000;
744 tcsetattr(0, TCSANOW, &tc);
745 /* trash return*/
746 getc(stdin);
Ingo Molnar07800602009-04-20 15:00:56 +0200747
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200748 do {
Ingo Molnar07800602009-04-20 15:00:56 +0200749 print_sym_table();
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200750 } while (!poll(&stdin_poll, 1, delay_msecs) == 1);
751
Mike Galbraith923c42c2009-07-22 20:36:03 +0200752 c = getc(stdin);
753 tcsetattr(0, TCSAFLUSH, &save);
754
755 handle_keypress(c);
756 goto repeat;
Ingo Molnar07800602009-04-20 15:00:56 +0200757
758 return NULL;
759}
760
Anton Blanchard2ab52082009-07-01 09:00:46 +1000761/* Tag samples to be skipped. */
Ingo Molnarf37a2912009-07-01 12:37:06 +0200762static const char *skip_symbols[] = {
Anton Blanchard2ab52082009-07-01 09:00:46 +1000763 "default_idle",
764 "cpu_idle",
765 "enter_idle",
766 "exit_idle",
767 "mwait_idle",
Arnaldo Carvalho de Melo59b90052009-07-26 19:06:19 -0300768 "mwait_idle_with_hints",
Anton Blanchard3a3393e2009-07-01 09:00:47 +1000769 "ppc64_runlatch_off",
770 "pseries_dedicated_idle_sleep",
Anton Blanchard2ab52082009-07-01 09:00:46 +1000771 NULL
772};
773
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300774static int symbol_filter(struct dso *self, struct symbol *sym)
Ingo Molnar07800602009-04-20 15:00:56 +0200775{
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300776 struct sym_entry *syme;
777 const char *name = sym->name;
Anton Blanchard2ab52082009-07-01 09:00:46 +1000778 int i;
Ingo Molnar07800602009-04-20 15:00:56 +0200779
Anton Blanchard3a3393e2009-07-01 09:00:47 +1000780 /*
781 * ppc64 uses function descriptors and appends a '.' to the
782 * start of every instruction address. Remove it.
783 */
784 if (name[0] == '.')
785 name++;
786
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300787 if (!strcmp(name, "_text") ||
788 !strcmp(name, "_etext") ||
789 !strcmp(name, "_sinittext") ||
790 !strncmp("init_module", name, 11) ||
791 !strncmp("cleanup_module", name, 14) ||
792 strstr(name, "_text_start") ||
793 strstr(name, "_text_end"))
Ingo Molnar07800602009-04-20 15:00:56 +0200794 return 1;
795
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300796 syme = dso__sym_priv(self, sym);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200797 pthread_mutex_init(&syme->source_lock, NULL);
798 if (!sym_filter_entry && sym_filter && !strcmp(name, sym_filter))
799 sym_filter_entry = syme;
800
Anton Blanchard2ab52082009-07-01 09:00:46 +1000801 for (i = 0; skip_symbols[i]; i++) {
802 if (!strcmp(skip_symbols[i], name)) {
803 syme->skip = 1;
804 break;
805 }
806 }
Ingo Molnar07800602009-04-20 15:00:56 +0200807
Ingo Molnar07800602009-04-20 15:00:56 +0200808 return 0;
809}
810
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300811static int parse_symbols(void)
Ingo Molnar07800602009-04-20 15:00:56 +0200812{
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300813 struct rb_node *node;
814 struct symbol *sym;
Mike Galbraith42976482009-07-02 08:09:46 +0200815 int modules = vmlinux ? 1 : 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200816
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300817 kernel_dso = dso__new("[kernel]", sizeof(struct sym_entry));
818 if (kernel_dso == NULL)
819 return -1;
Ingo Molnar07800602009-04-20 15:00:56 +0200820
Mike Galbraith42976482009-07-02 08:09:46 +0200821 if (dso__load_kernel(kernel_dso, vmlinux, symbol_filter, verbose, modules) <= 0)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300822 goto out_delete_dso;
Ingo Molnar07800602009-04-20 15:00:56 +0200823
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300824 node = rb_first(&kernel_dso->syms);
825 sym = rb_entry(node, struct symbol, rb_node);
826 min_ip = sym->start;
Ingo Molnar07800602009-04-20 15:00:56 +0200827
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300828 node = rb_last(&kernel_dso->syms);
829 sym = rb_entry(node, struct symbol, rb_node);
Mike Galbraithda417a72009-05-29 08:23:16 +0200830 max_ip = sym->end;
Ingo Molnar07800602009-04-20 15:00:56 +0200831
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300832 if (dump_symtab)
Mike Galbraitha3ec8d72009-05-29 06:46:46 +0200833 dso__fprintf(kernel_dso, stderr);
Ingo Molnar07800602009-04-20 15:00:56 +0200834
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300835 return 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200836
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300837out_delete_dso:
838 dso__delete(kernel_dso);
839 kernel_dso = NULL;
840 return -1;
Ingo Molnar07800602009-04-20 15:00:56 +0200841}
842
Ingo Molnar07800602009-04-20 15:00:56 +0200843/*
844 * Binary search in the histogram table and record the hit:
845 */
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000846static void record_ip(u64 ip, int counter)
Ingo Molnar07800602009-04-20 15:00:56 +0200847{
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300848 struct symbol *sym = dso__find_symbol(kernel_dso, ip);
Ingo Molnar07800602009-04-20 15:00:56 +0200849
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300850 if (sym != NULL) {
851 struct sym_entry *syme = dso__sym_priv(kernel_dso, sym);
Ingo Molnar07800602009-04-20 15:00:56 +0200852
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300853 if (!syme->skip) {
854 syme->count[counter]++;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200855 record_precise_ip(syme, counter, ip);
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300856 pthread_mutex_lock(&active_symbols_lock);
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300857 if (list_empty(&syme->node) || !syme->node.next)
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300858 __list_insert_active_sym(syme);
859 pthread_mutex_unlock(&active_symbols_lock);
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300860 return;
Ingo Molnar07800602009-04-20 15:00:56 +0200861 }
Ingo Molnar07800602009-04-20 15:00:56 +0200862 }
863
Ingo Molnar2debbc82009-06-05 14:29:10 +0200864 samples--;
Ingo Molnar07800602009-04-20 15:00:56 +0200865}
866
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +0200867static void process_event(u64 ip, int counter, int user)
Ingo Molnar07800602009-04-20 15:00:56 +0200868{
Ingo Molnar2debbc82009-06-05 14:29:10 +0200869 samples++;
Ingo Molnar07800602009-04-20 15:00:56 +0200870
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +0200871 if (user) {
Ingo Molnar2debbc82009-06-05 14:29:10 +0200872 userspace_samples++;
Ingo Molnar07800602009-04-20 15:00:56 +0200873 return;
874 }
875
876 record_ip(ip, counter);
877}
878
Ingo Molnar07800602009-04-20 15:00:56 +0200879struct mmap_data {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200880 int counter;
881 void *base;
Ingo Molnarf37a2912009-07-01 12:37:06 +0200882 int mask;
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200883 unsigned int prev;
Ingo Molnar07800602009-04-20 15:00:56 +0200884};
885
886static unsigned int mmap_read_head(struct mmap_data *md)
887{
888 struct perf_counter_mmap_page *pc = md->base;
889 int head;
890
891 head = pc->data_head;
892 rmb();
893
894 return head;
895}
896
897struct timeval last_read, this_read;
898
Frederic Weisbecker2f011902009-06-06 23:10:43 +0200899static void mmap_read_counter(struct mmap_data *md)
Ingo Molnar07800602009-04-20 15:00:56 +0200900{
901 unsigned int head = mmap_read_head(md);
902 unsigned int old = md->prev;
903 unsigned char *data = md->base + page_size;
904 int diff;
905
906 gettimeofday(&this_read, NULL);
907
908 /*
909 * If we're further behind than half the buffer, there's a chance
Ingo Molnar2debbc82009-06-05 14:29:10 +0200910 * the writer will bite our tail and mess up the samples under us.
Ingo Molnar07800602009-04-20 15:00:56 +0200911 *
912 * If we somehow ended up ahead of the head, we got messed up.
913 *
914 * In either case, truncate and restart at head.
915 */
916 diff = head - old;
917 if (diff > md->mask / 2 || diff < 0) {
918 struct timeval iv;
919 unsigned long msecs;
920
921 timersub(&this_read, &last_read, &iv);
922 msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
923
924 fprintf(stderr, "WARNING: failed to keep up with mmap data."
925 " Last read %lu msecs ago.\n", msecs);
926
927 /*
928 * head points to a known good entry, start there.
929 */
930 old = head;
931 }
932
933 last_read = this_read;
934
935 for (; old != head;) {
Ingo Molnar07800602009-04-20 15:00:56 +0200936 event_t *event = (event_t *)&data[old & md->mask];
937
938 event_t event_copy;
939
Ingo Molnar6f06ccb2009-04-20 15:22:22 +0200940 size_t size = event->header.size;
Ingo Molnar07800602009-04-20 15:00:56 +0200941
942 /*
943 * Event straddles the mmap boundary -- header should always
944 * be inside due to u64 alignment of output.
945 */
946 if ((old & md->mask) + size != ((old + size) & md->mask)) {
947 unsigned int offset = old;
948 unsigned int len = min(sizeof(*event), size), cpy;
949 void *dst = &event_copy;
950
951 do {
952 cpy = min(md->mask + 1 - (offset & md->mask), len);
953 memcpy(dst, &data[offset & md->mask], cpy);
954 offset += cpy;
955 dst += cpy;
956 len -= cpy;
957 } while (len);
958
959 event = &event_copy;
960 }
961
962 old += size;
963
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +0200964 if (event->header.type == PERF_EVENT_SAMPLE) {
965 int user =
966 (event->header.misc & PERF_EVENT_MISC_CPUMODE_MASK) == PERF_EVENT_MISC_USER;
967 process_event(event->ip.ip, md->counter, user);
Ingo Molnar07800602009-04-20 15:00:56 +0200968 }
969 }
970
971 md->prev = old;
972}
973
Mike Galbraithc2990a22009-05-24 08:35:49 +0200974static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
975static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
976
Frederic Weisbecker2f011902009-06-06 23:10:43 +0200977static void mmap_read(void)
978{
979 int i, counter;
980
981 for (i = 0; i < nr_cpus; i++) {
982 for (counter = 0; counter < nr_counters; counter++)
983 mmap_read_counter(&mmap_array[i][counter]);
984 }
985}
986
Ingo Molnar716c69f2009-06-07 17:31:52 +0200987int nr_poll;
988int group_fd;
989
990static void start_counter(int i, int counter)
Ingo Molnar07800602009-04-20 15:00:56 +0200991{
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200992 struct perf_counter_attr *attr;
Mike Galbraith0fdc7e62009-07-21 10:30:36 +0200993 int cpu;
Ingo Molnar716c69f2009-06-07 17:31:52 +0200994
995 cpu = profile_cpu;
996 if (target_pid == -1 && profile_cpu == -1)
997 cpu = i;
998
999 attr = attrs + counter;
1000
1001 attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
1002 attr->freq = freq;
Mike Galbraith0fdc7e62009-07-21 10:30:36 +02001003 attr->inherit = (cpu < 0) && inherit;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001004
1005try_again:
1006 fd[i][counter] = sys_perf_counter_open(attr, target_pid, cpu, group_fd, 0);
1007
1008 if (fd[i][counter] < 0) {
1009 int err = errno;
1010
Ingo Molnar716c69f2009-06-07 17:31:52 +02001011 if (err == EPERM)
Ingo Molnar3da297a2009-06-07 17:39:02 +02001012 die("No permission - are you root?\n");
Ingo Molnar716c69f2009-06-07 17:31:52 +02001013 /*
1014 * If it's cycles then fall back to hrtimer
1015 * based cpu-clock-tick sw counter, which
1016 * is always available even if no PMU support:
1017 */
1018 if (attr->type == PERF_TYPE_HARDWARE
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02001019 && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
Ingo Molnar716c69f2009-06-07 17:31:52 +02001020
Ingo Molnar3da297a2009-06-07 17:39:02 +02001021 if (verbose)
1022 warning(" ... trying to fall back to cpu-clock-ticks\n");
1023
Ingo Molnar716c69f2009-06-07 17:31:52 +02001024 attr->type = PERF_TYPE_SOFTWARE;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02001025 attr->config = PERF_COUNT_SW_CPU_CLOCK;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001026 goto try_again;
1027 }
Ingo Molnar30c806a2009-06-07 17:46:24 +02001028 printf("\n");
1029 error("perfcounter syscall returned with %d (%s)\n",
1030 fd[i][counter], strerror(err));
1031 die("No CONFIG_PERF_COUNTERS=y kernel support configured?\n");
Ingo Molnar716c69f2009-06-07 17:31:52 +02001032 exit(-1);
1033 }
1034 assert(fd[i][counter] >= 0);
1035 fcntl(fd[i][counter], F_SETFL, O_NONBLOCK);
1036
1037 /*
1038 * First counter acts as the group leader:
1039 */
1040 if (group && group_fd == -1)
1041 group_fd = fd[i][counter];
1042
1043 event_array[nr_poll].fd = fd[i][counter];
1044 event_array[nr_poll].events = POLLIN;
1045 nr_poll++;
1046
1047 mmap_array[i][counter].counter = counter;
1048 mmap_array[i][counter].prev = 0;
1049 mmap_array[i][counter].mask = mmap_pages*page_size - 1;
1050 mmap_array[i][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
1051 PROT_READ, MAP_SHARED, fd[i][counter], 0);
1052 if (mmap_array[i][counter].base == MAP_FAILED)
1053 die("failed to mmap with %d (%s)\n", errno, strerror(errno));
1054}
1055
1056static int __cmd_top(void)
1057{
1058 pthread_t thread;
1059 int i, counter;
Ingo Molnar07800602009-04-20 15:00:56 +02001060 int ret;
1061
Ingo Molnar07800602009-04-20 15:00:56 +02001062 for (i = 0; i < nr_cpus; i++) {
1063 group_fd = -1;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001064 for (counter = 0; counter < nr_counters; counter++)
1065 start_counter(i, counter);
Ingo Molnar07800602009-04-20 15:00:56 +02001066 }
1067
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001068 /* Wait for a minimal set of events before starting the snapshot */
1069 poll(event_array, nr_poll, 100);
1070
1071 mmap_read();
1072
Ingo Molnar07800602009-04-20 15:00:56 +02001073 if (pthread_create(&thread, NULL, display_thread, NULL)) {
1074 printf("Could not create display thread.\n");
1075 exit(-1);
1076 }
1077
1078 if (realtime_prio) {
1079 struct sched_param param;
1080
1081 param.sched_priority = realtime_prio;
1082 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
1083 printf("Could not set realtime priority.\n");
1084 exit(-1);
1085 }
1086 }
1087
1088 while (1) {
Ingo Molnar2debbc82009-06-05 14:29:10 +02001089 int hits = samples;
Ingo Molnar07800602009-04-20 15:00:56 +02001090
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001091 mmap_read();
Ingo Molnar07800602009-04-20 15:00:56 +02001092
Ingo Molnar2debbc82009-06-05 14:29:10 +02001093 if (hits == samples)
Ingo Molnar07800602009-04-20 15:00:56 +02001094 ret = poll(event_array, nr_poll, 100);
1095 }
1096
1097 return 0;
1098}
Ingo Molnarb456bae2009-05-26 09:17:18 +02001099
1100static const char * const top_usage[] = {
1101 "perf top [<options>]",
1102 NULL
1103};
1104
Ingo Molnarb456bae2009-05-26 09:17:18 +02001105static const struct option options[] = {
1106 OPT_CALLBACK('e', "event", NULL, "event",
Thomas Gleixner86847b62009-06-06 12:24:17 +02001107 "event selector. use 'perf list' to list available events",
1108 parse_events),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001109 OPT_INTEGER('c', "count", &default_interval,
1110 "event period to sample"),
1111 OPT_INTEGER('p', "pid", &target_pid,
1112 "profile events on existing pid"),
1113 OPT_BOOLEAN('a', "all-cpus", &system_wide,
1114 "system-wide collection from all CPUs"),
1115 OPT_INTEGER('C', "CPU", &profile_cpu,
1116 "CPU to profile on"),
Mike Galbraith42976482009-07-02 08:09:46 +02001117 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001118 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
1119 "number of mmap data pages"),
1120 OPT_INTEGER('r', "realtime", &realtime_prio,
1121 "collect data with this RT SCHED_FIFO priority"),
Mike Galbraithdb20c002009-05-26 15:25:34 +02001122 OPT_INTEGER('d', "delay", &delay_secs,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001123 "number of seconds to delay between refreshes"),
1124 OPT_BOOLEAN('D', "dump-symtab", &dump_symtab,
1125 "dump the symbol table used for profiling"),
Ingo Molnar6e53cdf2009-06-04 08:53:05 +02001126 OPT_INTEGER('f', "count-filter", &count_filter,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001127 "only display functions with more events than this"),
1128 OPT_BOOLEAN('g', "group", &group,
1129 "put the counters into a counter group"),
Mike Galbraith0fdc7e62009-07-21 10:30:36 +02001130 OPT_BOOLEAN('i', "inherit", &inherit,
1131 "child tasks inherit counters"),
Mike Galbraith923c42c2009-07-22 20:36:03 +02001132 OPT_STRING('s', "sym-annotate", &sym_filter, "symbol name",
1133 "symbol to annotate - requires -k option"),
Anton Blanchard1f208ea2009-07-01 09:00:44 +10001134 OPT_BOOLEAN('z', "zero", &zero,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001135 "zero history across updates"),
Ingo Molnar6e53cdf2009-06-04 08:53:05 +02001136 OPT_INTEGER('F', "freq", &freq,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001137 "profile at this frequency"),
Ingo Molnar6e53cdf2009-06-04 08:53:05 +02001138 OPT_INTEGER('E', "entries", &print_entries,
1139 "display this many functions"),
Ingo Molnar3da297a2009-06-07 17:39:02 +02001140 OPT_BOOLEAN('v', "verbose", &verbose,
1141 "be more verbose (show counter open errors, etc)"),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001142 OPT_END()
1143};
1144
Ingo Molnarf37a2912009-07-01 12:37:06 +02001145int cmd_top(int argc, const char **argv, const char *prefix __used)
Ingo Molnarb456bae2009-05-26 09:17:18 +02001146{
1147 int counter;
1148
Mike Galbraith42976482009-07-02 08:09:46 +02001149 symbol__init();
1150
Ingo Molnarb456bae2009-05-26 09:17:18 +02001151 page_size = sysconf(_SC_PAGE_SIZE);
1152
Ingo Molnarb456bae2009-05-26 09:17:18 +02001153 argc = parse_options(argc, argv, options, top_usage, 0);
1154 if (argc)
1155 usage_with_options(top_usage, options);
1156
1157 if (freq) {
1158 default_interval = freq;
1159 freq = 1;
1160 }
1161
1162 /* CPU and PID are mutually exclusive */
1163 if (target_pid != -1 && profile_cpu != -1) {
1164 printf("WARNING: PID switch overriding CPU\n");
1165 sleep(1);
1166 profile_cpu = -1;
1167 }
1168
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001169 if (!nr_counters)
Ingo Molnarb456bae2009-05-26 09:17:18 +02001170 nr_counters = 1;
Ingo Molnarb456bae2009-05-26 09:17:18 +02001171
Frederic Weisbecker2f335a02009-06-05 19:31:01 +02001172 if (delay_secs < 1)
1173 delay_secs = 1;
1174
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001175 parse_symbols();
Mike Galbraith923c42c2009-07-22 20:36:03 +02001176 parse_source(sym_filter_entry);
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001177
1178 /*
1179 * Fill in the ones not specifically initialized via -c:
1180 */
Ingo Molnarb456bae2009-05-26 09:17:18 +02001181 for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001182 if (attrs[counter].sample_period)
Ingo Molnarb456bae2009-05-26 09:17:18 +02001183 continue;
1184
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001185 attrs[counter].sample_period = default_interval;
Ingo Molnarb456bae2009-05-26 09:17:18 +02001186 }
1187
1188 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
1189 assert(nr_cpus <= MAX_NR_CPUS);
1190 assert(nr_cpus >= 0);
1191
1192 if (target_pid != -1 || profile_cpu != -1)
1193 nr_cpus = 1;
1194
Ingo Molnarb456bae2009-05-26 09:17:18 +02001195 return __cmd_top();
1196}