blob: 37512e936235359aa4ebbf93d21831cce56750d7 [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
Frederic Weisbecker8f288272009-08-16 22:05:48 +020030#include "util/debug.h"
31
Ingo Molnar07800602009-04-20 15:00:56 +020032#include <assert.h>
33#include <fcntl.h>
Ingo Molnar0e9b20b2009-05-26 09:17:18 +020034
Ingo Molnar07800602009-04-20 15:00:56 +020035#include <stdio.h>
Mike Galbraith923c42c2009-07-22 20:36:03 +020036#include <termios.h>
37#include <unistd.h>
Ingo Molnar0e9b20b2009-05-26 09:17:18 +020038
Ingo Molnar07800602009-04-20 15:00:56 +020039#include <errno.h>
Ingo Molnar07800602009-04-20 15:00:56 +020040#include <time.h>
41#include <sched.h>
42#include <pthread.h>
43
44#include <sys/syscall.h>
45#include <sys/ioctl.h>
46#include <sys/poll.h>
47#include <sys/prctl.h>
48#include <sys/wait.h>
49#include <sys/uio.h>
50#include <sys/mman.h>
51
52#include <linux/unistd.h>
53#include <linux/types.h>
54
Ingo Molnara21ca2c2009-06-06 09:58:57 +020055static int fd[MAX_NR_CPUS][MAX_COUNTERS];
56
Ingo Molnar07800602009-04-20 15:00:56 +020057static int system_wide = 0;
58
Ingo Molnara21ca2c2009-06-06 09:58:57 +020059static int default_interval = 100000;
Ingo Molnar07800602009-04-20 15:00:56 +020060
Mike Galbraith923c42c2009-07-22 20:36:03 +020061static int count_filter = 5;
Ingo Molnar6e53cdf2009-06-04 08:53:05 +020062static int print_entries = 15;
Ingo Molnar07800602009-04-20 15:00:56 +020063
Ingo Molnar6e53cdf2009-06-04 08:53:05 +020064static int target_pid = -1;
Mike Galbraith0fdc7e62009-07-21 10:30:36 +020065static int inherit = 0;
Ingo Molnar07800602009-04-20 15:00:56 +020066static int profile_cpu = -1;
67static int nr_cpus = 0;
Ingo Molnar07800602009-04-20 15:00:56 +020068static unsigned int realtime_prio = 0;
69static int group = 0;
70static unsigned int page_size;
Ingo Molnarcf1f4572009-06-05 13:27:02 +020071static unsigned int mmap_pages = 16;
72static int freq = 0;
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;
Ingo Molnar83a09442009-08-15 12:26:57 +0200125 char command[PATH_MAX*2];
126 const char *path = vmlinux_name;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200127 u64 start, end, len;
128
129 if (!syme)
130 return;
131
132 if (syme->lines) {
133 pthread_mutex_lock(&syme->source_lock);
134 goto out_assign;
135 }
136
137 sym = (struct symbol *)(syme + 1);
138 module = sym->module;
139
140 if (module)
141 path = module->path;
142 if (!path)
143 return;
144
145 start = sym->obj_start;
146 if (!start)
147 start = sym->start;
148
149 if (module) {
150 section = module->sections->find_section(module->sections, ".text");
151 if (section)
152 start -= section->vma;
153 }
154
155 end = start + sym->end - sym->start + 1;
156 len = sym->end - sym->start;
157
158 sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s", start, end, path);
159
160 file = popen(command, "r");
161 if (!file)
162 return;
163
164 pthread_mutex_lock(&syme->source_lock);
165 syme->lines_tail = &syme->lines;
166 while (!feof(file)) {
167 struct source_line *src;
168 size_t dummy = 0;
169 char *c;
170
171 src = malloc(sizeof(struct source_line));
172 assert(src != NULL);
173 memset(src, 0, sizeof(struct source_line));
174
175 if (getline(&src->line, &dummy, file) < 0)
176 break;
177 if (!src->line)
178 break;
179
180 c = strchr(src->line, '\n');
181 if (c)
182 *c = 0;
183
184 src->next = NULL;
185 *syme->lines_tail = src;
186 syme->lines_tail = &src->next;
187
188 if (strlen(src->line)>8 && src->line[8] == ':') {
189 src->eip = strtoull(src->line, NULL, 16);
190 if (section)
191 src->eip += section->vma;
192 }
193 if (strlen(src->line)>8 && src->line[16] == ':') {
194 src->eip = strtoull(src->line, NULL, 16);
195 if (section)
196 src->eip += section->vma;
197 }
198 }
199 pclose(file);
200out_assign:
201 sym_filter_entry = syme;
202 pthread_mutex_unlock(&syme->source_lock);
203}
204
205static void __zero_source_counters(struct sym_entry *syme)
206{
207 int i;
208 struct source_line *line;
209
210 line = syme->lines;
211 while (line) {
212 for (i = 0; i < nr_counters; i++)
213 line->count[i] = 0;
214 line = line->next;
215 }
216}
217
218static void record_precise_ip(struct sym_entry *syme, int counter, u64 ip)
219{
220 struct source_line *line;
221
222 if (syme != sym_filter_entry)
223 return;
224
225 if (pthread_mutex_trylock(&syme->source_lock))
226 return;
227
228 if (!syme->source)
229 goto out_unlock;
230
231 for (line = syme->lines; line; line = line->next) {
232 if (line->eip == ip) {
233 line->count[counter]++;
234 break;
235 }
236 if (line->eip > ip)
237 break;
238 }
239out_unlock:
240 pthread_mutex_unlock(&syme->source_lock);
241}
242
243static void lookup_sym_source(struct sym_entry *syme)
244{
245 struct symbol *symbol = (struct symbol *)(syme + 1);
246 struct source_line *line;
247 char pattern[PATH_MAX];
248 char *idx;
249
250 sprintf(pattern, "<%s>:", symbol->name);
251
252 if (symbol->module) {
253 idx = strstr(pattern, "\t");
254 if (idx)
255 *idx = 0;
256 }
257
258 pthread_mutex_lock(&syme->source_lock);
259 for (line = syme->lines; line; line = line->next) {
260 if (strstr(line->line, pattern)) {
261 syme->source = line;
262 break;
263 }
264 }
265 pthread_mutex_unlock(&syme->source_lock);
266}
267
268static void show_lines(struct source_line *queue, int count, int total)
269{
270 int i;
271 struct source_line *line;
272
273 line = queue;
274 for (i = 0; i < count; i++) {
275 float pcnt = 100.0*(float)line->count[sym_counter]/(float)total;
276
277 printf("%8li %4.1f%%\t%s\n", line->count[sym_counter], pcnt, line->line);
278 line = line->next;
279 }
280}
281
282#define TRACE_COUNT 3
283
284static void show_details(struct sym_entry *syme)
285{
286 struct symbol *symbol;
287 struct source_line *line;
288 struct source_line *line_queue = NULL;
289 int displayed = 0;
290 int line_queue_count = 0, total = 0, more = 0;
291
292 if (!syme)
293 return;
294
295 if (!syme->source)
296 lookup_sym_source(syme);
297
298 if (!syme->source)
299 return;
300
301 symbol = (struct symbol *)(syme + 1);
302 printf("Showing %s for %s\n", event_name(sym_counter), symbol->name);
303 printf(" Events Pcnt (>=%d%%)\n", sym_pcnt_filter);
304
305 pthread_mutex_lock(&syme->source_lock);
306 line = syme->source;
307 while (line) {
308 total += line->count[sym_counter];
309 line = line->next;
310 }
311
312 line = syme->source;
313 while (line) {
314 float pcnt = 0.0;
315
316 if (!line_queue_count)
317 line_queue = line;
318 line_queue_count++;
319
320 if (line->count[sym_counter])
321 pcnt = 100.0 * line->count[sym_counter] / (float)total;
322 if (pcnt >= (float)sym_pcnt_filter) {
323 if (displayed <= print_entries)
324 show_lines(line_queue, line_queue_count, total);
325 else more++;
326 displayed += line_queue_count;
327 line_queue_count = 0;
328 line_queue = NULL;
329 } else if (line_queue_count > TRACE_COUNT) {
330 line_queue = line_queue->next;
331 line_queue_count--;
332 }
333
334 line->count[sym_counter] = zero ? 0 : line->count[sym_counter] * 7 / 8;
335 line = line->next;
336 }
337 pthread_mutex_unlock(&syme->source_lock);
338 if (more)
339 printf("%d lines not displayed, maybe increase display entries [e]\n", more);
340}
Ingo Molnar07800602009-04-20 15:00:56 +0200341
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300342/*
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
Mike Galbraith46ab9762009-07-24 10:09:50 +0200357 if (!display_weighted)
358 return weight;
359
Ingo Molnar07800602009-04-20 15:00:56 +0200360 for (counter = 1; counter < nr_counters-1; counter++)
361 weight *= sym->count[counter];
362
363 weight /= (sym->count[counter] + 1);
364
365 return weight;
366}
367
Ingo Molnar2debbc82009-06-05 14:29:10 +0200368static long samples;
369static long userspace_samples;
Ingo Molnar07800602009-04-20 15:00:56 +0200370static const char CONSOLE_CLEAR[] = "";
371
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300372static void __list_insert_active_sym(struct sym_entry *syme)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300373{
374 list_add(&syme->node, &active_symbols);
375}
376
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300377static void list_remove_active_sym(struct sym_entry *syme)
378{
379 pthread_mutex_lock(&active_symbols_lock);
380 list_del_init(&syme->node);
381 pthread_mutex_unlock(&active_symbols_lock);
382}
383
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300384static void rb_insert_active_sym(struct rb_root *tree, struct sym_entry *se)
385{
386 struct rb_node **p = &tree->rb_node;
387 struct rb_node *parent = NULL;
388 struct sym_entry *iter;
389
390 while (*p != NULL) {
391 parent = *p;
392 iter = rb_entry(parent, struct sym_entry, rb_node);
393
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300394 if (se->weight > iter->weight)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300395 p = &(*p)->rb_left;
396 else
397 p = &(*p)->rb_right;
398 }
399
400 rb_link_node(&se->rb_node, parent, p);
401 rb_insert_color(&se->rb_node, tree);
402}
Ingo Molnar07800602009-04-20 15:00:56 +0200403
404static void print_sym_table(void)
405{
Ingo Molnar233f0b92009-06-03 21:48:40 +0200406 int printed = 0, j;
Mike Galbraith46ab9762009-07-24 10:09:50 +0200407 int counter, snap = !display_weighted ? sym_counter : 0;
Ingo Molnar2debbc82009-06-05 14:29:10 +0200408 float samples_per_sec = samples/delay_secs;
409 float ksamples_per_sec = (samples-userspace_samples)/delay_secs;
410 float sum_ksamples = 0.0;
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300411 struct sym_entry *syme, *n;
412 struct rb_root tmp = RB_ROOT;
413 struct rb_node *nd;
Ingo Molnar07800602009-04-20 15:00:56 +0200414
Ingo Molnar2debbc82009-06-05 14:29:10 +0200415 samples = userspace_samples = 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200416
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300417 /* Sort the active symbols */
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300418 pthread_mutex_lock(&active_symbols_lock);
419 syme = list_entry(active_symbols.next, struct sym_entry, node);
420 pthread_mutex_unlock(&active_symbols_lock);
421
422 list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
Mike Galbraith46ab9762009-07-24 10:09:50 +0200423 syme->snap_count = syme->count[snap];
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300424 if (syme->snap_count != 0) {
425 syme->weight = sym_weight(syme);
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300426 rb_insert_active_sym(&tmp, syme);
Ingo Molnar2debbc82009-06-05 14:29:10 +0200427 sum_ksamples += syme->snap_count;
Mike Galbraithd94b9432009-05-25 09:57:56 +0200428
429 for (j = 0; j < nr_counters; j++)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300430 syme->count[j] = zero ? 0 : syme->count[j] * 7 / 8;
431 } else
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300432 list_remove_active_sym(syme);
Mike Galbraithd94b9432009-05-25 09:57:56 +0200433 }
434
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200435 puts(CONSOLE_CLEAR);
Ingo Molnar07800602009-04-20 15:00:56 +0200436
437 printf(
438"------------------------------------------------------------------------------\n");
Ingo Molnarf2521b62009-06-03 19:17:25 +0200439 printf( " PerfTop:%8.0f irqs/sec kernel:%4.1f%% [",
Ingo Molnar2debbc82009-06-05 14:29:10 +0200440 samples_per_sec,
441 100.0 - (100.0*((samples_per_sec-ksamples_per_sec)/samples_per_sec)));
Ingo Molnar07800602009-04-20 15:00:56 +0200442
Mike Galbraith46ab9762009-07-24 10:09:50 +0200443 if (nr_counters == 1 || !display_weighted) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000444 printf("%Ld", (u64)attrs[0].sample_period);
Ingo Molnarcf1f4572009-06-05 13:27:02 +0200445 if (freq)
446 printf("Hz ");
447 else
448 printf(" ");
449 }
Ingo Molnar07800602009-04-20 15:00:56 +0200450
Mike Galbraith46ab9762009-07-24 10:09:50 +0200451 if (!display_weighted)
452 printf("%s", event_name(sym_counter));
453 else for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnar07800602009-04-20 15:00:56 +0200454 if (counter)
455 printf("/");
456
457 printf("%s", event_name(counter));
458 }
459
460 printf( "], ");
461
Ingo Molnarb456bae2009-05-26 09:17:18 +0200462 if (target_pid != -1)
463 printf(" (target_pid: %d", target_pid);
Ingo Molnar07800602009-04-20 15:00:56 +0200464 else
465 printf(" (all");
466
467 if (profile_cpu != -1)
468 printf(", cpu: %d)\n", profile_cpu);
469 else {
Ingo Molnarb456bae2009-05-26 09:17:18 +0200470 if (target_pid != -1)
Ingo Molnar07800602009-04-20 15:00:56 +0200471 printf(")\n");
472 else
473 printf(", %d CPUs)\n", nr_cpus);
474 }
475
476 printf("------------------------------------------------------------------------------\n\n");
477
Mike Galbraith923c42c2009-07-22 20:36:03 +0200478 if (sym_filter_entry) {
479 show_details(sym_filter_entry);
480 return;
481 }
482
Ingo Molnar07800602009-04-20 15:00:56 +0200483 if (nr_counters == 1)
Ingo Molnar2debbc82009-06-05 14:29:10 +0200484 printf(" samples pcnt");
Ingo Molnar07800602009-04-20 15:00:56 +0200485 else
Arnaldo Carvalho de Melo7ced1562009-08-26 11:51:26 -0300486 printf(" weight samples pcnt");
Ingo Molnar07800602009-04-20 15:00:56 +0200487
Arnaldo Carvalho de Melo7ced1562009-08-26 11:51:26 -0300488 if (verbose)
489 printf(" RIP ");
490 printf(" kernel function\n");
491 printf(" %s _______ _____",
492 nr_counters == 1 ? " " : "______");
493 if (verbose)
494 printf(" ________________");
495 printf(" _______________\n\n");
Ingo Molnar07800602009-04-20 15:00:56 +0200496
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300497 for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
Ingo Molnar83a09442009-08-15 12:26:57 +0200498 struct symbol *sym;
Ingo Molnar8fc03212009-06-04 15:19:47 +0200499 double pcnt;
Ingo Molnar07800602009-04-20 15:00:56 +0200500
Ingo Molnar83a09442009-08-15 12:26:57 +0200501 syme = rb_entry(nd, struct sym_entry, rb_node);
502 sym = (struct symbol *)(syme + 1);
503
Mike Galbraith923c42c2009-07-22 20:36:03 +0200504 if (++printed > print_entries || (int)syme->snap_count < count_filter)
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300505 continue;
Ingo Molnar07800602009-04-20 15:00:56 +0200506
Ingo Molnar2debbc82009-06-05 14:29:10 +0200507 pcnt = 100.0 - (100.0 * ((sum_ksamples - syme->snap_count) /
508 sum_ksamples));
Mike Galbraithd94b9432009-05-25 09:57:56 +0200509
Mike Galbraith46ab9762009-07-24 10:09:50 +0200510 if (nr_counters == 1 || !display_weighted)
Ingo Molnar2debbc82009-06-05 14:29:10 +0200511 printf("%20.2f - ", syme->weight);
Mike Galbraithd94b9432009-05-25 09:57:56 +0200512 else
Ingo Molnar2debbc82009-06-05 14:29:10 +0200513 printf("%9.1f %10ld - ", syme->weight, syme->snap_count);
Ingo Molnar8fc03212009-06-04 15:19:47 +0200514
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200515 percent_color_fprintf(stdout, "%4.1f%%", pcnt);
Arnaldo Carvalho de Melo7ced1562009-08-26 11:51:26 -0300516 if (verbose)
517 printf(" - %016llx", sym->start);
518 printf(" : %s", sym->name);
Mike Galbraith42976482009-07-02 08:09:46 +0200519 if (sym->module)
520 printf("\t[%s]", sym->module->name);
521 printf("\n");
Ingo Molnar07800602009-04-20 15:00:56 +0200522 }
Ingo Molnar07800602009-04-20 15:00:56 +0200523}
524
Mike Galbraith923c42c2009-07-22 20:36:03 +0200525static void prompt_integer(int *target, const char *msg)
526{
527 char *buf = malloc(0), *p;
528 size_t dummy = 0;
529 int tmp;
530
531 fprintf(stdout, "\n%s: ", msg);
532 if (getline(&buf, &dummy, stdin) < 0)
533 return;
534
535 p = strchr(buf, '\n');
536 if (p)
537 *p = 0;
538
539 p = buf;
540 while(*p) {
541 if (!isdigit(*p))
542 goto out_free;
543 p++;
544 }
545 tmp = strtoul(buf, NULL, 10);
546 *target = tmp;
547out_free:
548 free(buf);
549}
550
551static void prompt_percent(int *target, const char *msg)
552{
553 int tmp = 0;
554
555 prompt_integer(&tmp, msg);
556 if (tmp >= 0 && tmp <= 100)
557 *target = tmp;
558}
559
560static void prompt_symbol(struct sym_entry **target, const char *msg)
561{
562 char *buf = malloc(0), *p;
563 struct sym_entry *syme = *target, *n, *found = NULL;
564 size_t dummy = 0;
565
566 /* zero counters of active symbol */
567 if (syme) {
568 pthread_mutex_lock(&syme->source_lock);
569 __zero_source_counters(syme);
570 *target = NULL;
571 pthread_mutex_unlock(&syme->source_lock);
572 }
573
574 fprintf(stdout, "\n%s: ", msg);
575 if (getline(&buf, &dummy, stdin) < 0)
576 goto out_free;
577
578 p = strchr(buf, '\n');
579 if (p)
580 *p = 0;
581
582 pthread_mutex_lock(&active_symbols_lock);
583 syme = list_entry(active_symbols.next, struct sym_entry, node);
584 pthread_mutex_unlock(&active_symbols_lock);
585
586 list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
587 struct symbol *sym = (struct symbol *)(syme + 1);
588
589 if (!strcmp(buf, sym->name)) {
590 found = syme;
591 break;
592 }
593 }
594
595 if (!found) {
596 fprintf(stderr, "Sorry, %s is not active.\n", sym_filter);
597 sleep(1);
598 return;
599 } else
600 parse_source(found);
601
602out_free:
603 free(buf);
604}
605
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200606static void print_mapped_keys(void)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200607{
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200608 char *name = NULL;
609
610 if (sym_filter_entry) {
611 struct symbol *sym = (struct symbol *)(sym_filter_entry+1);
612 name = sym->name;
613 }
614
615 fprintf(stdout, "\nMapped keys:\n");
616 fprintf(stdout, "\t[d] display refresh delay. \t(%d)\n", delay_secs);
617 fprintf(stdout, "\t[e] display entries (lines). \t(%d)\n", print_entries);
618
619 if (nr_counters > 1)
620 fprintf(stdout, "\t[E] active event counter. \t(%s)\n", event_name(sym_counter));
621
622 fprintf(stdout, "\t[f] profile display filter (count). \t(%d)\n", count_filter);
623
Ingo Molnar83a09442009-08-15 12:26:57 +0200624 if (vmlinux_name) {
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200625 fprintf(stdout, "\t[F] annotate display filter (percent). \t(%d%%)\n", sym_pcnt_filter);
626 fprintf(stdout, "\t[s] annotate symbol. \t(%s)\n", name?: "NULL");
627 fprintf(stdout, "\t[S] stop annotation.\n");
628 }
629
630 if (nr_counters > 1)
631 fprintf(stdout, "\t[w] toggle display weighted/count[E]r. \t(%d)\n", display_weighted ? 1 : 0);
632
Mike Galbraith46ab9762009-07-24 10:09:50 +0200633 fprintf(stdout, "\t[z] toggle sample zeroing. \t(%d)\n", zero ? 1 : 0);
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200634 fprintf(stdout, "\t[qQ] quit.\n");
635}
636
637static int key_mapped(int c)
638{
639 switch (c) {
640 case 'd':
641 case 'e':
642 case 'f':
643 case 'z':
644 case 'q':
645 case 'Q':
646 return 1;
647 case 'E':
648 case 'w':
649 return nr_counters > 1 ? 1 : 0;
650 case 'F':
651 case 's':
652 case 'S':
Ingo Molnar83a09442009-08-15 12:26:57 +0200653 return vmlinux_name ? 1 : 0;
654 default:
655 break;
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200656 }
657
658 return 0;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200659}
660
661static void handle_keypress(int c)
662{
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200663 if (!key_mapped(c)) {
664 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
665 struct termios tc, save;
666
667 print_mapped_keys();
668 fprintf(stdout, "\nEnter selection, or unmapped key to continue: ");
669 fflush(stdout);
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 if (!key_mapped(c))
683 return;
684 }
685
Mike Galbraith923c42c2009-07-22 20:36:03 +0200686 switch (c) {
687 case 'd':
688 prompt_integer(&delay_secs, "Enter display delay");
689 break;
690 case 'e':
691 prompt_integer(&print_entries, "Enter display entries (lines)");
692 break;
693 case 'E':
694 if (nr_counters > 1) {
695 int i;
696
697 fprintf(stderr, "\nAvailable events:");
698 for (i = 0; i < nr_counters; i++)
699 fprintf(stderr, "\n\t%d %s", i, event_name(i));
700
701 prompt_integer(&sym_counter, "Enter details event counter");
702
703 if (sym_counter >= nr_counters) {
704 fprintf(stderr, "Sorry, no such event, using %s.\n", event_name(0));
705 sym_counter = 0;
706 sleep(1);
707 }
708 } else sym_counter = 0;
709 break;
710 case 'f':
711 prompt_integer(&count_filter, "Enter display event count filter");
712 break;
713 case 'F':
714 prompt_percent(&sym_pcnt_filter, "Enter details display event filter (percent)");
715 break;
716 case 'q':
717 case 'Q':
718 printf("exiting.\n");
719 exit(0);
720 case 's':
721 prompt_symbol(&sym_filter_entry, "Enter details symbol");
722 break;
723 case 'S':
724 if (!sym_filter_entry)
725 break;
726 else {
727 struct sym_entry *syme = sym_filter_entry;
728
729 pthread_mutex_lock(&syme->source_lock);
730 sym_filter_entry = NULL;
731 __zero_source_counters(syme);
732 pthread_mutex_unlock(&syme->source_lock);
733 }
734 break;
Mike Galbraith46ab9762009-07-24 10:09:50 +0200735 case 'w':
736 display_weighted = ~display_weighted;
737 break;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200738 case 'z':
739 zero = ~zero;
740 break;
Ingo Molnar83a09442009-08-15 12:26:57 +0200741 default:
742 break;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200743 }
744}
745
Ingo Molnarf37a2912009-07-01 12:37:06 +0200746static void *display_thread(void *arg __used)
Ingo Molnar07800602009-04-20 15:00:56 +0200747{
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200748 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
Mike Galbraith923c42c2009-07-22 20:36:03 +0200749 struct termios tc, save;
750 int delay_msecs, c;
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200751
Mike Galbraith923c42c2009-07-22 20:36:03 +0200752 tcgetattr(0, &save);
753 tc = save;
754 tc.c_lflag &= ~(ICANON | ECHO);
755 tc.c_cc[VMIN] = 0;
756 tc.c_cc[VTIME] = 0;
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200757
Mike Galbraith923c42c2009-07-22 20:36:03 +0200758repeat:
759 delay_msecs = delay_secs * 1000;
760 tcsetattr(0, TCSANOW, &tc);
761 /* trash return*/
762 getc(stdin);
Ingo Molnar07800602009-04-20 15:00:56 +0200763
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200764 do {
Ingo Molnar07800602009-04-20 15:00:56 +0200765 print_sym_table();
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200766 } while (!poll(&stdin_poll, 1, delay_msecs) == 1);
767
Mike Galbraith923c42c2009-07-22 20:36:03 +0200768 c = getc(stdin);
769 tcsetattr(0, TCSAFLUSH, &save);
770
771 handle_keypress(c);
772 goto repeat;
Ingo Molnar07800602009-04-20 15:00:56 +0200773
774 return NULL;
775}
776
Anton Blanchard2ab52082009-07-01 09:00:46 +1000777/* Tag samples to be skipped. */
Ingo Molnarf37a2912009-07-01 12:37:06 +0200778static const char *skip_symbols[] = {
Anton Blanchard2ab52082009-07-01 09:00:46 +1000779 "default_idle",
780 "cpu_idle",
781 "enter_idle",
782 "exit_idle",
783 "mwait_idle",
Arnaldo Carvalho de Melo59b90052009-07-26 19:06:19 -0300784 "mwait_idle_with_hints",
Arnaldo Carvalho de Melo83572752009-09-25 15:02:39 -0700785 "poll_idle",
Anton Blanchard3a3393e2009-07-01 09:00:47 +1000786 "ppc64_runlatch_off",
787 "pseries_dedicated_idle_sleep",
Anton Blanchard2ab52082009-07-01 09:00:46 +1000788 NULL
789};
790
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300791static int symbol_filter(struct dso *self, struct symbol *sym)
Ingo Molnar07800602009-04-20 15:00:56 +0200792{
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300793 struct sym_entry *syme;
794 const char *name = sym->name;
Anton Blanchard2ab52082009-07-01 09:00:46 +1000795 int i;
Ingo Molnar07800602009-04-20 15:00:56 +0200796
Anton Blanchard3a3393e2009-07-01 09:00:47 +1000797 /*
798 * ppc64 uses function descriptors and appends a '.' to the
799 * start of every instruction address. Remove it.
800 */
801 if (name[0] == '.')
802 name++;
803
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300804 if (!strcmp(name, "_text") ||
805 !strcmp(name, "_etext") ||
806 !strcmp(name, "_sinittext") ||
807 !strncmp("init_module", name, 11) ||
808 !strncmp("cleanup_module", name, 14) ||
809 strstr(name, "_text_start") ||
810 strstr(name, "_text_end"))
Ingo Molnar07800602009-04-20 15:00:56 +0200811 return 1;
812
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300813 syme = dso__sym_priv(self, sym);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200814 pthread_mutex_init(&syme->source_lock, NULL);
815 if (!sym_filter_entry && sym_filter && !strcmp(name, sym_filter))
816 sym_filter_entry = syme;
817
Anton Blanchard2ab52082009-07-01 09:00:46 +1000818 for (i = 0; skip_symbols[i]; i++) {
819 if (!strcmp(skip_symbols[i], name)) {
820 syme->skip = 1;
821 break;
822 }
823 }
Ingo Molnar07800602009-04-20 15:00:56 +0200824
Ingo Molnar07800602009-04-20 15:00:56 +0200825 return 0;
826}
827
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300828static int parse_symbols(void)
Ingo Molnar07800602009-04-20 15:00:56 +0200829{
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300830 struct rb_node *node;
831 struct symbol *sym;
Ingo Molnar83a09442009-08-15 12:26:57 +0200832 int use_modules = vmlinux_name ? 1 : 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200833
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300834 kernel_dso = dso__new("[kernel]", sizeof(struct sym_entry));
835 if (kernel_dso == NULL)
836 return -1;
Ingo Molnar07800602009-04-20 15:00:56 +0200837
Ingo Molnar83a09442009-08-15 12:26:57 +0200838 if (dso__load_kernel(kernel_dso, vmlinux_name, symbol_filter, verbose, use_modules) <= 0)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300839 goto out_delete_dso;
Ingo Molnar07800602009-04-20 15:00:56 +0200840
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300841 node = rb_first(&kernel_dso->syms);
842 sym = rb_entry(node, struct symbol, rb_node);
843 min_ip = sym->start;
Ingo Molnar07800602009-04-20 15:00:56 +0200844
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300845 node = rb_last(&kernel_dso->syms);
846 sym = rb_entry(node, struct symbol, rb_node);
Mike Galbraithda417a72009-05-29 08:23:16 +0200847 max_ip = sym->end;
Ingo Molnar07800602009-04-20 15:00:56 +0200848
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300849 if (dump_symtab)
Mike Galbraitha3ec8d72009-05-29 06:46:46 +0200850 dso__fprintf(kernel_dso, stderr);
Ingo Molnar07800602009-04-20 15:00:56 +0200851
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300852 return 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200853
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300854out_delete_dso:
855 dso__delete(kernel_dso);
856 kernel_dso = NULL;
857 return -1;
Ingo Molnar07800602009-04-20 15:00:56 +0200858}
859
Ingo Molnar07800602009-04-20 15:00:56 +0200860/*
861 * Binary search in the histogram table and record the hit:
862 */
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000863static void record_ip(u64 ip, int counter)
Ingo Molnar07800602009-04-20 15:00:56 +0200864{
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300865 struct symbol *sym = dso__find_symbol(kernel_dso, ip);
Ingo Molnar07800602009-04-20 15:00:56 +0200866
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300867 if (sym != NULL) {
868 struct sym_entry *syme = dso__sym_priv(kernel_dso, sym);
Ingo Molnar07800602009-04-20 15:00:56 +0200869
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300870 if (!syme->skip) {
871 syme->count[counter]++;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200872 record_precise_ip(syme, counter, ip);
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300873 pthread_mutex_lock(&active_symbols_lock);
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300874 if (list_empty(&syme->node) || !syme->node.next)
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300875 __list_insert_active_sym(syme);
876 pthread_mutex_unlock(&active_symbols_lock);
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300877 return;
Ingo Molnar07800602009-04-20 15:00:56 +0200878 }
Ingo Molnar07800602009-04-20 15:00:56 +0200879 }
880
Ingo Molnar2debbc82009-06-05 14:29:10 +0200881 samples--;
Ingo Molnar07800602009-04-20 15:00:56 +0200882}
883
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +0200884static void process_event(u64 ip, int counter, int user)
Ingo Molnar07800602009-04-20 15:00:56 +0200885{
Ingo Molnar2debbc82009-06-05 14:29:10 +0200886 samples++;
Ingo Molnar07800602009-04-20 15:00:56 +0200887
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +0200888 if (user) {
Ingo Molnar2debbc82009-06-05 14:29:10 +0200889 userspace_samples++;
Ingo Molnar07800602009-04-20 15:00:56 +0200890 return;
891 }
892
893 record_ip(ip, counter);
894}
895
Ingo Molnar07800602009-04-20 15:00:56 +0200896struct mmap_data {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200897 int counter;
898 void *base;
Ingo Molnarf37a2912009-07-01 12:37:06 +0200899 int mask;
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200900 unsigned int prev;
Ingo Molnar07800602009-04-20 15:00:56 +0200901};
902
903static unsigned int mmap_read_head(struct mmap_data *md)
904{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200905 struct perf_event_mmap_page *pc = md->base;
Ingo Molnar07800602009-04-20 15:00:56 +0200906 int head;
907
908 head = pc->data_head;
909 rmb();
910
911 return head;
912}
913
914struct timeval last_read, this_read;
915
Frederic Weisbecker2f011902009-06-06 23:10:43 +0200916static void mmap_read_counter(struct mmap_data *md)
Ingo Molnar07800602009-04-20 15:00:56 +0200917{
918 unsigned int head = mmap_read_head(md);
919 unsigned int old = md->prev;
920 unsigned char *data = md->base + page_size;
921 int diff;
922
923 gettimeofday(&this_read, NULL);
924
925 /*
926 * If we're further behind than half the buffer, there's a chance
Ingo Molnar2debbc82009-06-05 14:29:10 +0200927 * the writer will bite our tail and mess up the samples under us.
Ingo Molnar07800602009-04-20 15:00:56 +0200928 *
929 * If we somehow ended up ahead of the head, we got messed up.
930 *
931 * In either case, truncate and restart at head.
932 */
933 diff = head - old;
934 if (diff > md->mask / 2 || diff < 0) {
935 struct timeval iv;
936 unsigned long msecs;
937
938 timersub(&this_read, &last_read, &iv);
939 msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
940
941 fprintf(stderr, "WARNING: failed to keep up with mmap data."
942 " Last read %lu msecs ago.\n", msecs);
943
944 /*
945 * head points to a known good entry, start there.
946 */
947 old = head;
948 }
949
950 last_read = this_read;
951
952 for (; old != head;) {
Ingo Molnar07800602009-04-20 15:00:56 +0200953 event_t *event = (event_t *)&data[old & md->mask];
954
955 event_t event_copy;
956
Ingo Molnar6f06ccb2009-04-20 15:22:22 +0200957 size_t size = event->header.size;
Ingo Molnar07800602009-04-20 15:00:56 +0200958
959 /*
960 * Event straddles the mmap boundary -- header should always
961 * be inside due to u64 alignment of output.
962 */
963 if ((old & md->mask) + size != ((old + size) & md->mask)) {
964 unsigned int offset = old;
965 unsigned int len = min(sizeof(*event), size), cpy;
966 void *dst = &event_copy;
967
968 do {
969 cpy = min(md->mask + 1 - (offset & md->mask), len);
970 memcpy(dst, &data[offset & md->mask], cpy);
971 offset += cpy;
972 dst += cpy;
973 len -= cpy;
974 } while (len);
975
976 event = &event_copy;
977 }
978
979 old += size;
980
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200981 if (event->header.type == PERF_RECORD_SAMPLE) {
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +0200982 int user =
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200983 (event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK) == PERF_RECORD_MISC_USER;
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +0200984 process_event(event->ip.ip, md->counter, user);
Ingo Molnar07800602009-04-20 15:00:56 +0200985 }
986 }
987
988 md->prev = old;
989}
990
Mike Galbraithc2990a22009-05-24 08:35:49 +0200991static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
992static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
993
Frederic Weisbecker2f011902009-06-06 23:10:43 +0200994static void mmap_read(void)
995{
996 int i, counter;
997
998 for (i = 0; i < nr_cpus; i++) {
999 for (counter = 0; counter < nr_counters; counter++)
1000 mmap_read_counter(&mmap_array[i][counter]);
1001 }
1002}
1003
Ingo Molnar716c69f2009-06-07 17:31:52 +02001004int nr_poll;
1005int group_fd;
1006
1007static void start_counter(int i, int counter)
Ingo Molnar07800602009-04-20 15:00:56 +02001008{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001009 struct perf_event_attr *attr;
Mike Galbraith0fdc7e62009-07-21 10:30:36 +02001010 int cpu;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001011
1012 cpu = profile_cpu;
1013 if (target_pid == -1 && profile_cpu == -1)
1014 cpu = i;
1015
1016 attr = attrs + counter;
1017
1018 attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
1019 attr->freq = freq;
Mike Galbraith0fdc7e62009-07-21 10:30:36 +02001020 attr->inherit = (cpu < 0) && inherit;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001021
1022try_again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001023 fd[i][counter] = sys_perf_event_open(attr, target_pid, cpu, group_fd, 0);
Ingo Molnar716c69f2009-06-07 17:31:52 +02001024
1025 if (fd[i][counter] < 0) {
1026 int err = errno;
1027
Ingo Molnar716c69f2009-06-07 17:31:52 +02001028 if (err == EPERM)
Ingo Molnar3da297a2009-06-07 17:39:02 +02001029 die("No permission - are you root?\n");
Ingo Molnar716c69f2009-06-07 17:31:52 +02001030 /*
1031 * If it's cycles then fall back to hrtimer
1032 * based cpu-clock-tick sw counter, which
1033 * is always available even if no PMU support:
1034 */
1035 if (attr->type == PERF_TYPE_HARDWARE
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02001036 && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
Ingo Molnar716c69f2009-06-07 17:31:52 +02001037
Ingo Molnar3da297a2009-06-07 17:39:02 +02001038 if (verbose)
1039 warning(" ... trying to fall back to cpu-clock-ticks\n");
1040
Ingo Molnar716c69f2009-06-07 17:31:52 +02001041 attr->type = PERF_TYPE_SOFTWARE;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02001042 attr->config = PERF_COUNT_SW_CPU_CLOCK;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001043 goto try_again;
1044 }
Ingo Molnar30c806a2009-06-07 17:46:24 +02001045 printf("\n");
1046 error("perfcounter syscall returned with %d (%s)\n",
1047 fd[i][counter], strerror(err));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001048 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
Ingo Molnar716c69f2009-06-07 17:31:52 +02001049 exit(-1);
1050 }
1051 assert(fd[i][counter] >= 0);
1052 fcntl(fd[i][counter], F_SETFL, O_NONBLOCK);
1053
1054 /*
1055 * First counter acts as the group leader:
1056 */
1057 if (group && group_fd == -1)
1058 group_fd = fd[i][counter];
1059
1060 event_array[nr_poll].fd = fd[i][counter];
1061 event_array[nr_poll].events = POLLIN;
1062 nr_poll++;
1063
1064 mmap_array[i][counter].counter = counter;
1065 mmap_array[i][counter].prev = 0;
1066 mmap_array[i][counter].mask = mmap_pages*page_size - 1;
1067 mmap_array[i][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
1068 PROT_READ, MAP_SHARED, fd[i][counter], 0);
1069 if (mmap_array[i][counter].base == MAP_FAILED)
1070 die("failed to mmap with %d (%s)\n", errno, strerror(errno));
1071}
1072
1073static int __cmd_top(void)
1074{
1075 pthread_t thread;
1076 int i, counter;
Ingo Molnar07800602009-04-20 15:00:56 +02001077 int ret;
1078
Ingo Molnar07800602009-04-20 15:00:56 +02001079 for (i = 0; i < nr_cpus; i++) {
1080 group_fd = -1;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001081 for (counter = 0; counter < nr_counters; counter++)
1082 start_counter(i, counter);
Ingo Molnar07800602009-04-20 15:00:56 +02001083 }
1084
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001085 /* Wait for a minimal set of events before starting the snapshot */
1086 poll(event_array, nr_poll, 100);
1087
1088 mmap_read();
1089
Ingo Molnar07800602009-04-20 15:00:56 +02001090 if (pthread_create(&thread, NULL, display_thread, NULL)) {
1091 printf("Could not create display thread.\n");
1092 exit(-1);
1093 }
1094
1095 if (realtime_prio) {
1096 struct sched_param param;
1097
1098 param.sched_priority = realtime_prio;
1099 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
1100 printf("Could not set realtime priority.\n");
1101 exit(-1);
1102 }
1103 }
1104
1105 while (1) {
Ingo Molnar2debbc82009-06-05 14:29:10 +02001106 int hits = samples;
Ingo Molnar07800602009-04-20 15:00:56 +02001107
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001108 mmap_read();
Ingo Molnar07800602009-04-20 15:00:56 +02001109
Ingo Molnar2debbc82009-06-05 14:29:10 +02001110 if (hits == samples)
Ingo Molnar07800602009-04-20 15:00:56 +02001111 ret = poll(event_array, nr_poll, 100);
1112 }
1113
1114 return 0;
1115}
Ingo Molnarb456bae2009-05-26 09:17:18 +02001116
1117static const char * const top_usage[] = {
1118 "perf top [<options>]",
1119 NULL
1120};
1121
Ingo Molnarb456bae2009-05-26 09:17:18 +02001122static const struct option options[] = {
1123 OPT_CALLBACK('e', "event", NULL, "event",
Thomas Gleixner86847b62009-06-06 12:24:17 +02001124 "event selector. use 'perf list' to list available events",
1125 parse_events),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001126 OPT_INTEGER('c', "count", &default_interval,
1127 "event period to sample"),
1128 OPT_INTEGER('p', "pid", &target_pid,
1129 "profile events on existing pid"),
1130 OPT_BOOLEAN('a', "all-cpus", &system_wide,
1131 "system-wide collection from all CPUs"),
1132 OPT_INTEGER('C', "CPU", &profile_cpu,
1133 "CPU to profile on"),
Ingo Molnar83a09442009-08-15 12:26:57 +02001134 OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001135 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
1136 "number of mmap data pages"),
1137 OPT_INTEGER('r', "realtime", &realtime_prio,
1138 "collect data with this RT SCHED_FIFO priority"),
Mike Galbraithdb20c002009-05-26 15:25:34 +02001139 OPT_INTEGER('d', "delay", &delay_secs,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001140 "number of seconds to delay between refreshes"),
1141 OPT_BOOLEAN('D', "dump-symtab", &dump_symtab,
1142 "dump the symbol table used for profiling"),
Ingo Molnar6e53cdf2009-06-04 08:53:05 +02001143 OPT_INTEGER('f', "count-filter", &count_filter,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001144 "only display functions with more events than this"),
1145 OPT_BOOLEAN('g', "group", &group,
1146 "put the counters into a counter group"),
Mike Galbraith0fdc7e62009-07-21 10:30:36 +02001147 OPT_BOOLEAN('i', "inherit", &inherit,
1148 "child tasks inherit counters"),
Mike Galbraith923c42c2009-07-22 20:36:03 +02001149 OPT_STRING('s', "sym-annotate", &sym_filter, "symbol name",
1150 "symbol to annotate - requires -k option"),
Anton Blanchard1f208ea2009-07-01 09:00:44 +10001151 OPT_BOOLEAN('z', "zero", &zero,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001152 "zero history across updates"),
Ingo Molnar6e53cdf2009-06-04 08:53:05 +02001153 OPT_INTEGER('F', "freq", &freq,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001154 "profile at this frequency"),
Ingo Molnar6e53cdf2009-06-04 08:53:05 +02001155 OPT_INTEGER('E', "entries", &print_entries,
1156 "display this many functions"),
Ingo Molnar3da297a2009-06-07 17:39:02 +02001157 OPT_BOOLEAN('v', "verbose", &verbose,
1158 "be more verbose (show counter open errors, etc)"),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001159 OPT_END()
1160};
1161
Ingo Molnarf37a2912009-07-01 12:37:06 +02001162int cmd_top(int argc, const char **argv, const char *prefix __used)
Ingo Molnarb456bae2009-05-26 09:17:18 +02001163{
1164 int counter;
1165
Mike Galbraith42976482009-07-02 08:09:46 +02001166 symbol__init();
1167
Ingo Molnarb456bae2009-05-26 09:17:18 +02001168 page_size = sysconf(_SC_PAGE_SIZE);
1169
Ingo Molnarb456bae2009-05-26 09:17:18 +02001170 argc = parse_options(argc, argv, options, top_usage, 0);
1171 if (argc)
1172 usage_with_options(top_usage, options);
1173
1174 if (freq) {
1175 default_interval = freq;
1176 freq = 1;
1177 }
1178
1179 /* CPU and PID are mutually exclusive */
1180 if (target_pid != -1 && profile_cpu != -1) {
1181 printf("WARNING: PID switch overriding CPU\n");
1182 sleep(1);
1183 profile_cpu = -1;
1184 }
1185
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001186 if (!nr_counters)
Ingo Molnarb456bae2009-05-26 09:17:18 +02001187 nr_counters = 1;
Ingo Molnarb456bae2009-05-26 09:17:18 +02001188
Frederic Weisbecker2f335a02009-06-05 19:31:01 +02001189 if (delay_secs < 1)
1190 delay_secs = 1;
1191
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001192 parse_symbols();
Mike Galbraith923c42c2009-07-22 20:36:03 +02001193 parse_source(sym_filter_entry);
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001194
1195 /*
1196 * Fill in the ones not specifically initialized via -c:
1197 */
Ingo Molnarb456bae2009-05-26 09:17:18 +02001198 for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001199 if (attrs[counter].sample_period)
Ingo Molnarb456bae2009-05-26 09:17:18 +02001200 continue;
1201
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001202 attrs[counter].sample_period = default_interval;
Ingo Molnarb456bae2009-05-26 09:17:18 +02001203 }
1204
1205 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
1206 assert(nr_cpus <= MAX_NR_CPUS);
1207 assert(nr_cpus >= 0);
1208
1209 if (target_pid != -1 || profile_cpu != -1)
1210 nr_cpus = 1;
1211
Ingo Molnarb456bae2009-05-26 09:17:18 +02001212 return __cmd_top();
1213}