blob: 3af95203208a1d71746896a2fadbdcb44e8f7bb2 [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"
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030025#include "util/thread.h"
Ingo Molnar148be2c2009-04-27 08:02:14 +020026#include "util/util.h"
Arnaldo Carvalho de Melo43cbcd82009-07-01 12:28:37 -030027#include <linux/rbtree.h>
Ingo Molnarb456bae2009-05-26 09:17:18 +020028#include "util/parse-options.h"
29#include "util/parse-events.h"
Ingo Molnar07800602009-04-20 15:00:56 +020030
Frederic Weisbecker8f28827a2009-08-16 22:05:48 +020031#include "util/debug.h"
32
Ingo Molnar07800602009-04-20 15:00:56 +020033#include <assert.h>
34#include <fcntl.h>
Ingo Molnar0e9b20b2009-05-26 09:17:18 +020035
Ingo Molnar07800602009-04-20 15:00:56 +020036#include <stdio.h>
Mike Galbraith923c42c2009-07-22 20:36:03 +020037#include <termios.h>
38#include <unistd.h>
Ingo Molnar0e9b20b2009-05-26 09:17:18 +020039
Ingo Molnar07800602009-04-20 15:00:56 +020040#include <errno.h>
Ingo Molnar07800602009-04-20 15:00:56 +020041#include <time.h>
42#include <sched.h>
43#include <pthread.h>
44
45#include <sys/syscall.h>
46#include <sys/ioctl.h>
47#include <sys/poll.h>
48#include <sys/prctl.h>
49#include <sys/wait.h>
50#include <sys/uio.h>
51#include <sys/mman.h>
52
53#include <linux/unistd.h>
54#include <linux/types.h>
55
Ingo Molnara21ca2c2009-06-06 09:58:57 +020056static int fd[MAX_NR_CPUS][MAX_COUNTERS];
57
Ingo Molnar42e59d72009-10-06 15:14:21 +020058static int system_wide = 0;
Ingo Molnar07800602009-04-20 15:00:56 +020059
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +020060static int default_interval = 0;
Ingo Molnar07800602009-04-20 15:00:56 +020061
Ingo Molnar42e59d72009-10-06 15:14:21 +020062static int count_filter = 5;
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -020063static int print_entries;
Ingo Molnar07800602009-04-20 15:00:56 +020064
Ingo Molnar42e59d72009-10-06 15:14:21 +020065static int target_pid = -1;
66static int inherit = 0;
67static int profile_cpu = -1;
68static int nr_cpus = 0;
69static unsigned int realtime_prio = 0;
70static int group = 0;
Ingo Molnar07800602009-04-20 15:00:56 +020071static unsigned int page_size;
Ingo Molnar42e59d72009-10-06 15:14:21 +020072static unsigned int mmap_pages = 16;
73static int freq = 1000; /* 1 KHz */
Ingo Molnar07800602009-04-20 15:00:56 +020074
Ingo Molnar42e59d72009-10-06 15:14:21 +020075static int delay_secs = 2;
76static int zero = 0;
77static int dump_symtab = 0;
Ingo Molnar07800602009-04-20 15:00:56 +020078
Ingo Molnar07800602009-04-20 15:00:56 +020079/*
Mike Galbraith923c42c2009-07-22 20:36:03 +020080 * Source
81 */
82
83struct source_line {
84 u64 eip;
85 unsigned long count[MAX_COUNTERS];
86 char *line;
87 struct source_line *next;
88};
89
Ingo Molnar42e59d72009-10-06 15:14:21 +020090static char *sym_filter = NULL;
91struct sym_entry *sym_filter_entry = NULL;
92static int sym_pcnt_filter = 5;
93static int sym_counter = 0;
94static int display_weighted = -1;
Mike Galbraith923c42c2009-07-22 20:36:03 +020095
96/*
Ingo Molnar07800602009-04-20 15:00:56 +020097 * Symbols
98 */
99
Ingo Molnar07800602009-04-20 15:00:56 +0200100struct 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;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300107 struct map *map;
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
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200118/* most GUI terminals set LINES (although some don't export it) */
119static int term_rows(void)
120{
121 char *lines_string = getenv("LINES");
122 int n_lines;
123
124 if (lines_string && (n_lines = atoi(lines_string)) > 0)
125 return n_lines;
126#ifdef TIOCGWINSZ
127 else {
128 struct winsize ws;
129 if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_row)
130 return ws.ws_row;
131 }
132#endif
133 return 25;
134}
135
136static void update_print_entries(void)
137{
138 print_entries = term_rows();
139 if (print_entries > 9)
140 print_entries -= 9;
141}
142
143static void sig_winch_handler(int sig __used)
144{
145 update_print_entries();
146}
147
Mike Galbraith923c42c2009-07-22 20:36:03 +0200148static void parse_source(struct sym_entry *syme)
149{
150 struct symbol *sym;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300151 struct map *map;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200152 FILE *file;
Ingo Molnar83a09442009-08-15 12:26:57 +0200153 char command[PATH_MAX*2];
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300154 const char *path;
155 u64 len;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200156
157 if (!syme)
158 return;
159
160 if (syme->lines) {
161 pthread_mutex_lock(&syme->source_lock);
162 goto out_assign;
163 }
164
165 sym = (struct symbol *)(syme + 1);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300166 map = syme->map;
167 path = map->dso->long_name;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200168
Mike Galbraith923c42c2009-07-22 20:36:03 +0200169 len = sym->end - sym->start;
170
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300171 sprintf(command,
172 "objdump --start-address=0x%016Lx "
173 "--stop-address=0x%016Lx -dS %s",
Arnaldo Carvalho de Meloc88e4bf2009-10-20 15:54:55 -0200174 map->unmap_ip(map, sym->start),
175 map->unmap_ip(map, sym->end), path);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200176
177 file = popen(command, "r");
178 if (!file)
179 return;
180
181 pthread_mutex_lock(&syme->source_lock);
182 syme->lines_tail = &syme->lines;
183 while (!feof(file)) {
184 struct source_line *src;
185 size_t dummy = 0;
186 char *c;
187
188 src = malloc(sizeof(struct source_line));
189 assert(src != NULL);
190 memset(src, 0, sizeof(struct source_line));
191
192 if (getline(&src->line, &dummy, file) < 0)
193 break;
194 if (!src->line)
195 break;
196
197 c = strchr(src->line, '\n');
198 if (c)
199 *c = 0;
200
201 src->next = NULL;
202 *syme->lines_tail = src;
203 syme->lines_tail = &src->next;
204
205 if (strlen(src->line)>8 && src->line[8] == ':') {
206 src->eip = strtoull(src->line, NULL, 16);
Arnaldo Carvalho de Meloc88e4bf2009-10-20 15:54:55 -0200207 src->eip = map->unmap_ip(map, src->eip);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200208 }
209 if (strlen(src->line)>8 && src->line[16] == ':') {
210 src->eip = strtoull(src->line, NULL, 16);
Arnaldo Carvalho de Meloc88e4bf2009-10-20 15:54:55 -0200211 src->eip = map->unmap_ip(map, src->eip);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200212 }
213 }
214 pclose(file);
215out_assign:
216 sym_filter_entry = syme;
217 pthread_mutex_unlock(&syme->source_lock);
218}
219
220static void __zero_source_counters(struct sym_entry *syme)
221{
222 int i;
223 struct source_line *line;
224
225 line = syme->lines;
226 while (line) {
227 for (i = 0; i < nr_counters; i++)
228 line->count[i] = 0;
229 line = line->next;
230 }
231}
232
233static void record_precise_ip(struct sym_entry *syme, int counter, u64 ip)
234{
235 struct source_line *line;
236
237 if (syme != sym_filter_entry)
238 return;
239
240 if (pthread_mutex_trylock(&syme->source_lock))
241 return;
242
243 if (!syme->source)
244 goto out_unlock;
245
246 for (line = syme->lines; line; line = line->next) {
247 if (line->eip == ip) {
248 line->count[counter]++;
249 break;
250 }
251 if (line->eip > ip)
252 break;
253 }
254out_unlock:
255 pthread_mutex_unlock(&syme->source_lock);
256}
257
258static void lookup_sym_source(struct sym_entry *syme)
259{
260 struct symbol *symbol = (struct symbol *)(syme + 1);
261 struct source_line *line;
262 char pattern[PATH_MAX];
Mike Galbraith923c42c2009-07-22 20:36:03 +0200263
264 sprintf(pattern, "<%s>:", symbol->name);
265
Mike Galbraith923c42c2009-07-22 20:36:03 +0200266 pthread_mutex_lock(&syme->source_lock);
267 for (line = syme->lines; line; line = line->next) {
268 if (strstr(line->line, pattern)) {
269 syme->source = line;
270 break;
271 }
272 }
273 pthread_mutex_unlock(&syme->source_lock);
274}
275
276static void show_lines(struct source_line *queue, int count, int total)
277{
278 int i;
279 struct source_line *line;
280
281 line = queue;
282 for (i = 0; i < count; i++) {
283 float pcnt = 100.0*(float)line->count[sym_counter]/(float)total;
284
285 printf("%8li %4.1f%%\t%s\n", line->count[sym_counter], pcnt, line->line);
286 line = line->next;
287 }
288}
289
290#define TRACE_COUNT 3
291
292static void show_details(struct sym_entry *syme)
293{
294 struct symbol *symbol;
295 struct source_line *line;
296 struct source_line *line_queue = NULL;
297 int displayed = 0;
298 int line_queue_count = 0, total = 0, more = 0;
299
300 if (!syme)
301 return;
302
303 if (!syme->source)
304 lookup_sym_source(syme);
305
306 if (!syme->source)
307 return;
308
309 symbol = (struct symbol *)(syme + 1);
310 printf("Showing %s for %s\n", event_name(sym_counter), symbol->name);
311 printf(" Events Pcnt (>=%d%%)\n", sym_pcnt_filter);
312
313 pthread_mutex_lock(&syme->source_lock);
314 line = syme->source;
315 while (line) {
316 total += line->count[sym_counter];
317 line = line->next;
318 }
319
320 line = syme->source;
321 while (line) {
322 float pcnt = 0.0;
323
324 if (!line_queue_count)
325 line_queue = line;
326 line_queue_count++;
327
328 if (line->count[sym_counter])
329 pcnt = 100.0 * line->count[sym_counter] / (float)total;
330 if (pcnt >= (float)sym_pcnt_filter) {
331 if (displayed <= print_entries)
332 show_lines(line_queue, line_queue_count, total);
333 else more++;
334 displayed += line_queue_count;
335 line_queue_count = 0;
336 line_queue = NULL;
337 } else if (line_queue_count > TRACE_COUNT) {
338 line_queue = line_queue->next;
339 line_queue_count--;
340 }
341
342 line->count[sym_counter] = zero ? 0 : line->count[sym_counter] * 7 / 8;
343 line = line->next;
344 }
345 pthread_mutex_unlock(&syme->source_lock);
346 if (more)
347 printf("%d lines not displayed, maybe increase display entries [e]\n", more);
348}
Ingo Molnar07800602009-04-20 15:00:56 +0200349
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300350/*
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200351 * Symbols will be added here in event__process_sample and will get out
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300352 * after decayed.
353 */
354static LIST_HEAD(active_symbols);
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300355static pthread_mutex_t active_symbols_lock = PTHREAD_MUTEX_INITIALIZER;
Ingo Molnar07800602009-04-20 15:00:56 +0200356
Ingo Molnar07800602009-04-20 15:00:56 +0200357/*
358 * Ordering weight: count-1 * count-2 * ... / count-n
359 */
360static double sym_weight(const struct sym_entry *sym)
361{
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300362 double weight = sym->snap_count;
Ingo Molnar07800602009-04-20 15:00:56 +0200363 int counter;
364
Mike Galbraith46ab9762009-07-24 10:09:50 +0200365 if (!display_weighted)
366 return weight;
367
Ingo Molnar07800602009-04-20 15:00:56 +0200368 for (counter = 1; counter < nr_counters-1; counter++)
369 weight *= sym->count[counter];
370
371 weight /= (sym->count[counter] + 1);
372
373 return weight;
374}
375
Ingo Molnar2debbc82009-06-05 14:29:10 +0200376static long samples;
377static long userspace_samples;
Ingo Molnar07800602009-04-20 15:00:56 +0200378static const char CONSOLE_CLEAR[] = "";
379
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300380static void __list_insert_active_sym(struct sym_entry *syme)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300381{
382 list_add(&syme->node, &active_symbols);
383}
384
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300385static void list_remove_active_sym(struct sym_entry *syme)
386{
387 pthread_mutex_lock(&active_symbols_lock);
388 list_del_init(&syme->node);
389 pthread_mutex_unlock(&active_symbols_lock);
390}
391
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300392static void rb_insert_active_sym(struct rb_root *tree, struct sym_entry *se)
393{
394 struct rb_node **p = &tree->rb_node;
395 struct rb_node *parent = NULL;
396 struct sym_entry *iter;
397
398 while (*p != NULL) {
399 parent = *p;
400 iter = rb_entry(parent, struct sym_entry, rb_node);
401
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300402 if (se->weight > iter->weight)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300403 p = &(*p)->rb_left;
404 else
405 p = &(*p)->rb_right;
406 }
407
408 rb_link_node(&se->rb_node, parent, p);
409 rb_insert_color(&se->rb_node, tree);
410}
Ingo Molnar07800602009-04-20 15:00:56 +0200411
412static void print_sym_table(void)
413{
Ingo Molnar233f0b92009-06-03 21:48:40 +0200414 int printed = 0, j;
Mike Galbraith46ab9762009-07-24 10:09:50 +0200415 int counter, snap = !display_weighted ? sym_counter : 0;
Ingo Molnar2debbc82009-06-05 14:29:10 +0200416 float samples_per_sec = samples/delay_secs;
417 float ksamples_per_sec = (samples-userspace_samples)/delay_secs;
418 float sum_ksamples = 0.0;
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300419 struct sym_entry *syme, *n;
420 struct rb_root tmp = RB_ROOT;
421 struct rb_node *nd;
Ingo Molnar07800602009-04-20 15:00:56 +0200422
Ingo Molnar2debbc82009-06-05 14:29:10 +0200423 samples = userspace_samples = 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200424
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300425 /* Sort the active symbols */
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300426 pthread_mutex_lock(&active_symbols_lock);
427 syme = list_entry(active_symbols.next, struct sym_entry, node);
428 pthread_mutex_unlock(&active_symbols_lock);
429
430 list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
Mike Galbraith46ab9762009-07-24 10:09:50 +0200431 syme->snap_count = syme->count[snap];
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300432 if (syme->snap_count != 0) {
433 syme->weight = sym_weight(syme);
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300434 rb_insert_active_sym(&tmp, syme);
Ingo Molnar2debbc82009-06-05 14:29:10 +0200435 sum_ksamples += syme->snap_count;
Mike Galbraithd94b9432009-05-25 09:57:56 +0200436
437 for (j = 0; j < nr_counters; j++)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300438 syme->count[j] = zero ? 0 : syme->count[j] * 7 / 8;
439 } else
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300440 list_remove_active_sym(syme);
Mike Galbraithd94b9432009-05-25 09:57:56 +0200441 }
442
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200443 puts(CONSOLE_CLEAR);
Ingo Molnar07800602009-04-20 15:00:56 +0200444
445 printf(
446"------------------------------------------------------------------------------\n");
Ingo Molnarf2521b62009-06-03 19:17:25 +0200447 printf( " PerfTop:%8.0f irqs/sec kernel:%4.1f%% [",
Ingo Molnar2debbc82009-06-05 14:29:10 +0200448 samples_per_sec,
449 100.0 - (100.0*((samples_per_sec-ksamples_per_sec)/samples_per_sec)));
Ingo Molnar07800602009-04-20 15:00:56 +0200450
Mike Galbraith46ab9762009-07-24 10:09:50 +0200451 if (nr_counters == 1 || !display_weighted) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000452 printf("%Ld", (u64)attrs[0].sample_period);
Ingo Molnarcf1f4572009-06-05 13:27:02 +0200453 if (freq)
454 printf("Hz ");
455 else
456 printf(" ");
457 }
Ingo Molnar07800602009-04-20 15:00:56 +0200458
Mike Galbraith46ab9762009-07-24 10:09:50 +0200459 if (!display_weighted)
460 printf("%s", event_name(sym_counter));
461 else for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnar07800602009-04-20 15:00:56 +0200462 if (counter)
463 printf("/");
464
465 printf("%s", event_name(counter));
466 }
467
468 printf( "], ");
469
Ingo Molnarb456bae2009-05-26 09:17:18 +0200470 if (target_pid != -1)
471 printf(" (target_pid: %d", target_pid);
Ingo Molnar07800602009-04-20 15:00:56 +0200472 else
473 printf(" (all");
474
475 if (profile_cpu != -1)
476 printf(", cpu: %d)\n", profile_cpu);
477 else {
Ingo Molnarb456bae2009-05-26 09:17:18 +0200478 if (target_pid != -1)
Ingo Molnar07800602009-04-20 15:00:56 +0200479 printf(")\n");
480 else
481 printf(", %d CPUs)\n", nr_cpus);
482 }
483
484 printf("------------------------------------------------------------------------------\n\n");
485
Mike Galbraith923c42c2009-07-22 20:36:03 +0200486 if (sym_filter_entry) {
487 show_details(sym_filter_entry);
488 return;
489 }
490
Ingo Molnar07800602009-04-20 15:00:56 +0200491 if (nr_counters == 1)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200492 printf(" samples pcnt");
Ingo Molnar07800602009-04-20 15:00:56 +0200493 else
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200494 printf(" weight samples pcnt");
Ingo Molnar07800602009-04-20 15:00:56 +0200495
Arnaldo Carvalho de Melo7ced1562009-08-26 11:51:26 -0300496 if (verbose)
497 printf(" RIP ");
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200498 printf(" function DSO\n");
499 printf(" %s _______ _____",
Arnaldo Carvalho de Melo7ced1562009-08-26 11:51:26 -0300500 nr_counters == 1 ? " " : "______");
501 if (verbose)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200502 printf(" ________________");
503 printf(" ________________________________ ________________\n\n");
Ingo Molnar07800602009-04-20 15:00:56 +0200504
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300505 for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
Ingo Molnar83a09442009-08-15 12:26:57 +0200506 struct symbol *sym;
Ingo Molnar8fc03212009-06-04 15:19:47 +0200507 double pcnt;
Ingo Molnar07800602009-04-20 15:00:56 +0200508
Ingo Molnar83a09442009-08-15 12:26:57 +0200509 syme = rb_entry(nd, struct sym_entry, rb_node);
510 sym = (struct symbol *)(syme + 1);
511
Mike Galbraith923c42c2009-07-22 20:36:03 +0200512 if (++printed > print_entries || (int)syme->snap_count < count_filter)
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300513 continue;
Ingo Molnar07800602009-04-20 15:00:56 +0200514
Ingo Molnar2debbc82009-06-05 14:29:10 +0200515 pcnt = 100.0 - (100.0 * ((sum_ksamples - syme->snap_count) /
516 sum_ksamples));
Mike Galbraithd94b9432009-05-25 09:57:56 +0200517
Mike Galbraith46ab9762009-07-24 10:09:50 +0200518 if (nr_counters == 1 || !display_weighted)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200519 printf("%20.2f ", syme->weight);
Mike Galbraithd94b9432009-05-25 09:57:56 +0200520 else
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200521 printf("%9.1f %10ld ", syme->weight, syme->snap_count);
Ingo Molnar8fc03212009-06-04 15:19:47 +0200522
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200523 percent_color_fprintf(stdout, "%4.1f%%", pcnt);
Arnaldo Carvalho de Melo7ced1562009-08-26 11:51:26 -0300524 if (verbose)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200525 printf(" %016llx", sym->start);
526 printf(" %-32s", sym->name);
527 printf(" %s", syme->map->dso->short_name);
Mike Galbraith42976482009-07-02 08:09:46 +0200528 printf("\n");
Ingo Molnar07800602009-04-20 15:00:56 +0200529 }
Ingo Molnar07800602009-04-20 15:00:56 +0200530}
531
Mike Galbraith923c42c2009-07-22 20:36:03 +0200532static void prompt_integer(int *target, const char *msg)
533{
534 char *buf = malloc(0), *p;
535 size_t dummy = 0;
536 int tmp;
537
538 fprintf(stdout, "\n%s: ", msg);
539 if (getline(&buf, &dummy, stdin) < 0)
540 return;
541
542 p = strchr(buf, '\n');
543 if (p)
544 *p = 0;
545
546 p = buf;
547 while(*p) {
548 if (!isdigit(*p))
549 goto out_free;
550 p++;
551 }
552 tmp = strtoul(buf, NULL, 10);
553 *target = tmp;
554out_free:
555 free(buf);
556}
557
558static void prompt_percent(int *target, const char *msg)
559{
560 int tmp = 0;
561
562 prompt_integer(&tmp, msg);
563 if (tmp >= 0 && tmp <= 100)
564 *target = tmp;
565}
566
567static void prompt_symbol(struct sym_entry **target, const char *msg)
568{
569 char *buf = malloc(0), *p;
570 struct sym_entry *syme = *target, *n, *found = NULL;
571 size_t dummy = 0;
572
573 /* zero counters of active symbol */
574 if (syme) {
575 pthread_mutex_lock(&syme->source_lock);
576 __zero_source_counters(syme);
577 *target = NULL;
578 pthread_mutex_unlock(&syme->source_lock);
579 }
580
581 fprintf(stdout, "\n%s: ", msg);
582 if (getline(&buf, &dummy, stdin) < 0)
583 goto out_free;
584
585 p = strchr(buf, '\n');
586 if (p)
587 *p = 0;
588
589 pthread_mutex_lock(&active_symbols_lock);
590 syme = list_entry(active_symbols.next, struct sym_entry, node);
591 pthread_mutex_unlock(&active_symbols_lock);
592
593 list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
594 struct symbol *sym = (struct symbol *)(syme + 1);
595
596 if (!strcmp(buf, sym->name)) {
597 found = syme;
598 break;
599 }
600 }
601
602 if (!found) {
603 fprintf(stderr, "Sorry, %s is not active.\n", sym_filter);
604 sleep(1);
605 return;
606 } else
607 parse_source(found);
608
609out_free:
610 free(buf);
611}
612
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200613static void print_mapped_keys(void)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200614{
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200615 char *name = NULL;
616
617 if (sym_filter_entry) {
618 struct symbol *sym = (struct symbol *)(sym_filter_entry+1);
619 name = sym->name;
620 }
621
622 fprintf(stdout, "\nMapped keys:\n");
623 fprintf(stdout, "\t[d] display refresh delay. \t(%d)\n", delay_secs);
624 fprintf(stdout, "\t[e] display entries (lines). \t(%d)\n", print_entries);
625
626 if (nr_counters > 1)
627 fprintf(stdout, "\t[E] active event counter. \t(%s)\n", event_name(sym_counter));
628
629 fprintf(stdout, "\t[f] profile display filter (count). \t(%d)\n", count_filter);
630
Ingo Molnar83a09442009-08-15 12:26:57 +0200631 if (vmlinux_name) {
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200632 fprintf(stdout, "\t[F] annotate display filter (percent). \t(%d%%)\n", sym_pcnt_filter);
633 fprintf(stdout, "\t[s] annotate symbol. \t(%s)\n", name?: "NULL");
634 fprintf(stdout, "\t[S] stop annotation.\n");
635 }
636
637 if (nr_counters > 1)
638 fprintf(stdout, "\t[w] toggle display weighted/count[E]r. \t(%d)\n", display_weighted ? 1 : 0);
639
Mike Galbraith46ab9762009-07-24 10:09:50 +0200640 fprintf(stdout, "\t[z] toggle sample zeroing. \t(%d)\n", zero ? 1 : 0);
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200641 fprintf(stdout, "\t[qQ] quit.\n");
642}
643
644static int key_mapped(int c)
645{
646 switch (c) {
647 case 'd':
648 case 'e':
649 case 'f':
650 case 'z':
651 case 'q':
652 case 'Q':
653 return 1;
654 case 'E':
655 case 'w':
656 return nr_counters > 1 ? 1 : 0;
657 case 'F':
658 case 's':
659 case 'S':
Ingo Molnar83a09442009-08-15 12:26:57 +0200660 return vmlinux_name ? 1 : 0;
661 default:
662 break;
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200663 }
664
665 return 0;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200666}
667
668static void handle_keypress(int c)
669{
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200670 if (!key_mapped(c)) {
671 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
672 struct termios tc, save;
673
674 print_mapped_keys();
675 fprintf(stdout, "\nEnter selection, or unmapped key to continue: ");
676 fflush(stdout);
677
678 tcgetattr(0, &save);
679 tc = save;
680 tc.c_lflag &= ~(ICANON | ECHO);
681 tc.c_cc[VMIN] = 0;
682 tc.c_cc[VTIME] = 0;
683 tcsetattr(0, TCSANOW, &tc);
684
685 poll(&stdin_poll, 1, -1);
686 c = getc(stdin);
687
688 tcsetattr(0, TCSAFLUSH, &save);
689 if (!key_mapped(c))
690 return;
691 }
692
Mike Galbraith923c42c2009-07-22 20:36:03 +0200693 switch (c) {
694 case 'd':
695 prompt_integer(&delay_secs, "Enter display delay");
Tim Blechmanndc799592009-10-17 18:08:29 +0200696 if (delay_secs < 1)
697 delay_secs = 1;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200698 break;
699 case 'e':
700 prompt_integer(&print_entries, "Enter display entries (lines)");
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200701 if (print_entries == 0) {
702 update_print_entries();
703 signal(SIGWINCH, sig_winch_handler);
704 } else
705 signal(SIGWINCH, SIG_DFL);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200706 break;
707 case 'E':
708 if (nr_counters > 1) {
709 int i;
710
711 fprintf(stderr, "\nAvailable events:");
712 for (i = 0; i < nr_counters; i++)
713 fprintf(stderr, "\n\t%d %s", i, event_name(i));
714
715 prompt_integer(&sym_counter, "Enter details event counter");
716
717 if (sym_counter >= nr_counters) {
718 fprintf(stderr, "Sorry, no such event, using %s.\n", event_name(0));
719 sym_counter = 0;
720 sleep(1);
721 }
722 } else sym_counter = 0;
723 break;
724 case 'f':
725 prompt_integer(&count_filter, "Enter display event count filter");
726 break;
727 case 'F':
728 prompt_percent(&sym_pcnt_filter, "Enter details display event filter (percent)");
729 break;
730 case 'q':
731 case 'Q':
732 printf("exiting.\n");
733 exit(0);
734 case 's':
735 prompt_symbol(&sym_filter_entry, "Enter details symbol");
736 break;
737 case 'S':
738 if (!sym_filter_entry)
739 break;
740 else {
741 struct sym_entry *syme = sym_filter_entry;
742
743 pthread_mutex_lock(&syme->source_lock);
744 sym_filter_entry = NULL;
745 __zero_source_counters(syme);
746 pthread_mutex_unlock(&syme->source_lock);
747 }
748 break;
Mike Galbraith46ab9762009-07-24 10:09:50 +0200749 case 'w':
750 display_weighted = ~display_weighted;
751 break;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200752 case 'z':
753 zero = ~zero;
754 break;
Ingo Molnar83a09442009-08-15 12:26:57 +0200755 default:
756 break;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200757 }
758}
759
Ingo Molnarf37a2912009-07-01 12:37:06 +0200760static void *display_thread(void *arg __used)
Ingo Molnar07800602009-04-20 15:00:56 +0200761{
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200762 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
Mike Galbraith923c42c2009-07-22 20:36:03 +0200763 struct termios tc, save;
764 int delay_msecs, c;
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200765
Mike Galbraith923c42c2009-07-22 20:36:03 +0200766 tcgetattr(0, &save);
767 tc = save;
768 tc.c_lflag &= ~(ICANON | ECHO);
769 tc.c_cc[VMIN] = 0;
770 tc.c_cc[VTIME] = 0;
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200771
Mike Galbraith923c42c2009-07-22 20:36:03 +0200772repeat:
773 delay_msecs = delay_secs * 1000;
774 tcsetattr(0, TCSANOW, &tc);
775 /* trash return*/
776 getc(stdin);
Ingo Molnar07800602009-04-20 15:00:56 +0200777
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200778 do {
Ingo Molnar07800602009-04-20 15:00:56 +0200779 print_sym_table();
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200780 } while (!poll(&stdin_poll, 1, delay_msecs) == 1);
781
Mike Galbraith923c42c2009-07-22 20:36:03 +0200782 c = getc(stdin);
783 tcsetattr(0, TCSAFLUSH, &save);
784
785 handle_keypress(c);
786 goto repeat;
Ingo Molnar07800602009-04-20 15:00:56 +0200787
788 return NULL;
789}
790
Anton Blanchard2ab52082009-07-01 09:00:46 +1000791/* Tag samples to be skipped. */
Ingo Molnarf37a2912009-07-01 12:37:06 +0200792static const char *skip_symbols[] = {
Anton Blanchard2ab52082009-07-01 09:00:46 +1000793 "default_idle",
794 "cpu_idle",
795 "enter_idle",
796 "exit_idle",
797 "mwait_idle",
Arnaldo Carvalho de Melo59b90052009-07-26 19:06:19 -0300798 "mwait_idle_with_hints",
Arnaldo Carvalho de Melo83572752009-09-25 15:02:39 -0700799 "poll_idle",
Anton Blanchard3a3393e2009-07-01 09:00:47 +1000800 "ppc64_runlatch_off",
801 "pseries_dedicated_idle_sleep",
Anton Blanchard2ab52082009-07-01 09:00:46 +1000802 NULL
803};
804
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300805static int symbol_filter(struct map *map, struct symbol *sym)
Ingo Molnar07800602009-04-20 15:00:56 +0200806{
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300807 struct sym_entry *syme;
808 const char *name = sym->name;
Anton Blanchard2ab52082009-07-01 09:00:46 +1000809 int i;
Ingo Molnar07800602009-04-20 15:00:56 +0200810
Anton Blanchard3a3393e2009-07-01 09:00:47 +1000811 /*
812 * ppc64 uses function descriptors and appends a '.' to the
813 * start of every instruction address. Remove it.
814 */
815 if (name[0] == '.')
816 name++;
817
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300818 if (!strcmp(name, "_text") ||
819 !strcmp(name, "_etext") ||
820 !strcmp(name, "_sinittext") ||
821 !strncmp("init_module", name, 11) ||
822 !strncmp("cleanup_module", name, 14) ||
823 strstr(name, "_text_start") ||
824 strstr(name, "_text_end"))
Ingo Molnar07800602009-04-20 15:00:56 +0200825 return 1;
826
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200827 syme = symbol__priv(sym);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300828 syme->map = map;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200829 pthread_mutex_init(&syme->source_lock, NULL);
830 if (!sym_filter_entry && sym_filter && !strcmp(name, sym_filter))
831 sym_filter_entry = syme;
832
Anton Blanchard2ab52082009-07-01 09:00:46 +1000833 for (i = 0; skip_symbols[i]; i++) {
834 if (!strcmp(skip_symbols[i], name)) {
835 syme->skip = 1;
836 break;
837 }
838 }
Ingo Molnar07800602009-04-20 15:00:56 +0200839
Ingo Molnar07800602009-04-20 15:00:56 +0200840 return 0;
841}
842
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300843static int parse_symbols(void)
Ingo Molnar07800602009-04-20 15:00:56 +0200844{
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200845 if (dsos__load_kernel(vmlinux_name, symbol_filter, 1) <= 0)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300846 return -1;
Ingo Molnar07800602009-04-20 15:00:56 +0200847
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300848 if (dump_symtab)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300849 dsos__fprintf(stderr);
Ingo Molnar07800602009-04-20 15:00:56 +0200850
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300851 return 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200852}
853
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200854static void event__process_sample(const event_t *self, int counter)
Ingo Molnar07800602009-04-20 15:00:56 +0200855{
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200856 u64 ip = self->ip.ip;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300857 struct map *map;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200858 struct sym_entry *syme;
859 struct symbol *sym;
Ingo Molnar07800602009-04-20 15:00:56 +0200860
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200861 switch (self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK) {
862 case PERF_RECORD_MISC_USER: {
863 struct thread *thread = threads__findnew(self->ip.pid);
Ingo Molnar07800602009-04-20 15:00:56 +0200864
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200865 if (thread == NULL)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300866 return;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200867
868 map = thread__find_map(thread, ip);
869 if (map != NULL) {
870 ip = map->map_ip(map, ip);
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200871 sym = map__find_symbol(map, ip, symbol_filter);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200872 if (sym == NULL)
873 return;
874 userspace_samples++;
875 break;
Ingo Molnar07800602009-04-20 15:00:56 +0200876 }
Ingo Molnar07800602009-04-20 15:00:56 +0200877 }
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200878 /*
879 * If this is outside of all known maps,
880 * and is a negative address, try to look it
881 * up in the kernel dso, as it might be a
882 * vsyscall or vdso (which executes in user-mode).
883 */
884 if ((long long)ip >= 0)
885 return;
886 /* Fall thru */
887 case PERF_RECORD_MISC_KERNEL:
888 sym = kernel_maps__find_symbol(ip, &map);
889 if (sym == NULL)
890 return;
891 break;
892 default:
Ingo Molnar07800602009-04-20 15:00:56 +0200893 return;
894 }
895
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200896 syme = symbol__priv(sym);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200897
898 if (!syme->skip) {
899 syme->count[counter]++;
900 record_precise_ip(syme, counter, ip);
901 pthread_mutex_lock(&active_symbols_lock);
902 if (list_empty(&syme->node) || !syme->node.next)
903 __list_insert_active_sym(syme);
904 pthread_mutex_unlock(&active_symbols_lock);
905 ++samples;
906 return;
907 }
908}
909
910static void event__process_mmap(event_t *self)
911{
912 struct thread *thread = threads__findnew(self->mmap.pid);
913
914 if (thread != NULL) {
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200915 struct map *map = map__new(&self->mmap, NULL, 0);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200916 if (map != NULL)
917 thread__insert_map(thread, map);
918 }
919}
920
921static void event__process_comm(event_t *self)
922{
923 struct thread *thread = threads__findnew(self->comm.pid);
924
925 if (thread != NULL)
926 thread__set_comm(thread, self->comm.comm);
927}
928
929static int event__process(event_t *event)
930{
931 switch (event->header.type) {
932 case PERF_RECORD_COMM:
933 event__process_comm(event);
934 break;
935 case PERF_RECORD_MMAP:
936 event__process_mmap(event);
937 break;
938 default:
939 break;
940 }
941
942 return 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200943}
944
Ingo Molnar07800602009-04-20 15:00:56 +0200945struct mmap_data {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200946 int counter;
947 void *base;
Ingo Molnarf37a2912009-07-01 12:37:06 +0200948 int mask;
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200949 unsigned int prev;
Ingo Molnar07800602009-04-20 15:00:56 +0200950};
951
952static unsigned int mmap_read_head(struct mmap_data *md)
953{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200954 struct perf_event_mmap_page *pc = md->base;
Ingo Molnar07800602009-04-20 15:00:56 +0200955 int head;
956
957 head = pc->data_head;
958 rmb();
959
960 return head;
961}
962
Frederic Weisbecker2f011902009-06-06 23:10:43 +0200963static void mmap_read_counter(struct mmap_data *md)
Ingo Molnar07800602009-04-20 15:00:56 +0200964{
965 unsigned int head = mmap_read_head(md);
966 unsigned int old = md->prev;
967 unsigned char *data = md->base + page_size;
968 int diff;
969
Ingo Molnar07800602009-04-20 15:00:56 +0200970 /*
971 * If we're further behind than half the buffer, there's a chance
Ingo Molnar2debbc82009-06-05 14:29:10 +0200972 * the writer will bite our tail and mess up the samples under us.
Ingo Molnar07800602009-04-20 15:00:56 +0200973 *
974 * If we somehow ended up ahead of the head, we got messed up.
975 *
976 * In either case, truncate and restart at head.
977 */
978 diff = head - old;
979 if (diff > md->mask / 2 || diff < 0) {
Mike Galbraithf4f0b412009-10-13 14:57:20 +0200980 fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
Ingo Molnar07800602009-04-20 15:00:56 +0200981
982 /*
983 * head points to a known good entry, start there.
984 */
985 old = head;
986 }
987
Ingo Molnar07800602009-04-20 15:00:56 +0200988 for (; old != head;) {
Ingo Molnar07800602009-04-20 15:00:56 +0200989 event_t *event = (event_t *)&data[old & md->mask];
990
991 event_t event_copy;
992
Ingo Molnar6f06ccb2009-04-20 15:22:22 +0200993 size_t size = event->header.size;
Ingo Molnar07800602009-04-20 15:00:56 +0200994
995 /*
996 * Event straddles the mmap boundary -- header should always
997 * be inside due to u64 alignment of output.
998 */
999 if ((old & md->mask) + size != ((old + size) & md->mask)) {
1000 unsigned int offset = old;
1001 unsigned int len = min(sizeof(*event), size), cpy;
1002 void *dst = &event_copy;
1003
1004 do {
1005 cpy = min(md->mask + 1 - (offset & md->mask), len);
1006 memcpy(dst, &data[offset & md->mask], cpy);
1007 offset += cpy;
1008 dst += cpy;
1009 len -= cpy;
1010 } while (len);
1011
1012 event = &event_copy;
1013 }
1014
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001015 if (event->header.type == PERF_RECORD_SAMPLE)
1016 event__process_sample(event, md->counter);
1017 else
1018 event__process(event);
Ingo Molnar07800602009-04-20 15:00:56 +02001019 old += size;
Ingo Molnar07800602009-04-20 15:00:56 +02001020 }
1021
1022 md->prev = old;
1023}
1024
Mike Galbraithc2990a22009-05-24 08:35:49 +02001025static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
1026static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
1027
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001028static void mmap_read(void)
1029{
1030 int i, counter;
1031
1032 for (i = 0; i < nr_cpus; i++) {
1033 for (counter = 0; counter < nr_counters; counter++)
1034 mmap_read_counter(&mmap_array[i][counter]);
1035 }
1036}
1037
Ingo Molnar716c69f2009-06-07 17:31:52 +02001038int nr_poll;
1039int group_fd;
1040
1041static void start_counter(int i, int counter)
Ingo Molnar07800602009-04-20 15:00:56 +02001042{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001043 struct perf_event_attr *attr;
Mike Galbraith0fdc7e62009-07-21 10:30:36 +02001044 int cpu;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001045
1046 cpu = profile_cpu;
1047 if (target_pid == -1 && profile_cpu == -1)
1048 cpu = i;
1049
1050 attr = attrs + counter;
1051
1052 attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +02001053
1054 if (freq) {
1055 attr->sample_type |= PERF_SAMPLE_PERIOD;
1056 attr->freq = 1;
1057 attr->sample_freq = freq;
1058 }
1059
Mike Galbraith0fdc7e62009-07-21 10:30:36 +02001060 attr->inherit = (cpu < 0) && inherit;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001061 attr->mmap = 1;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001062
1063try_again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001064 fd[i][counter] = sys_perf_event_open(attr, target_pid, cpu, group_fd, 0);
Ingo Molnar716c69f2009-06-07 17:31:52 +02001065
1066 if (fd[i][counter] < 0) {
1067 int err = errno;
1068
Pekka Enbergc10edee2009-11-08 18:01:06 +02001069 if (err == EPERM || err == EACCES)
Ingo Molnar3da297a2009-06-07 17:39:02 +02001070 die("No permission - are you root?\n");
Ingo Molnar716c69f2009-06-07 17:31:52 +02001071 /*
1072 * If it's cycles then fall back to hrtimer
1073 * based cpu-clock-tick sw counter, which
1074 * is always available even if no PMU support:
1075 */
1076 if (attr->type == PERF_TYPE_HARDWARE
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02001077 && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
Ingo Molnar716c69f2009-06-07 17:31:52 +02001078
Ingo Molnar3da297a2009-06-07 17:39:02 +02001079 if (verbose)
1080 warning(" ... trying to fall back to cpu-clock-ticks\n");
1081
Ingo Molnar716c69f2009-06-07 17:31:52 +02001082 attr->type = PERF_TYPE_SOFTWARE;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02001083 attr->config = PERF_COUNT_SW_CPU_CLOCK;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001084 goto try_again;
1085 }
Ingo Molnar30c806a2009-06-07 17:46:24 +02001086 printf("\n");
1087 error("perfcounter syscall returned with %d (%s)\n",
1088 fd[i][counter], strerror(err));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001089 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
Ingo Molnar716c69f2009-06-07 17:31:52 +02001090 exit(-1);
1091 }
1092 assert(fd[i][counter] >= 0);
1093 fcntl(fd[i][counter], F_SETFL, O_NONBLOCK);
1094
1095 /*
1096 * First counter acts as the group leader:
1097 */
1098 if (group && group_fd == -1)
1099 group_fd = fd[i][counter];
1100
1101 event_array[nr_poll].fd = fd[i][counter];
1102 event_array[nr_poll].events = POLLIN;
1103 nr_poll++;
1104
1105 mmap_array[i][counter].counter = counter;
1106 mmap_array[i][counter].prev = 0;
1107 mmap_array[i][counter].mask = mmap_pages*page_size - 1;
1108 mmap_array[i][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
1109 PROT_READ, MAP_SHARED, fd[i][counter], 0);
1110 if (mmap_array[i][counter].base == MAP_FAILED)
1111 die("failed to mmap with %d (%s)\n", errno, strerror(errno));
1112}
1113
1114static int __cmd_top(void)
1115{
1116 pthread_t thread;
1117 int i, counter;
Ingo Molnar07800602009-04-20 15:00:56 +02001118 int ret;
1119
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001120 if (target_pid != -1)
1121 event__synthesize_thread(target_pid, event__process);
1122 else
1123 event__synthesize_threads(event__process);
1124
Ingo Molnar07800602009-04-20 15:00:56 +02001125 for (i = 0; i < nr_cpus; i++) {
1126 group_fd = -1;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001127 for (counter = 0; counter < nr_counters; counter++)
1128 start_counter(i, counter);
Ingo Molnar07800602009-04-20 15:00:56 +02001129 }
1130
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001131 /* Wait for a minimal set of events before starting the snapshot */
1132 poll(event_array, nr_poll, 100);
1133
1134 mmap_read();
1135
Ingo Molnar07800602009-04-20 15:00:56 +02001136 if (pthread_create(&thread, NULL, display_thread, NULL)) {
1137 printf("Could not create display thread.\n");
1138 exit(-1);
1139 }
1140
1141 if (realtime_prio) {
1142 struct sched_param param;
1143
1144 param.sched_priority = realtime_prio;
1145 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
1146 printf("Could not set realtime priority.\n");
1147 exit(-1);
1148 }
1149 }
1150
1151 while (1) {
Ingo Molnar2debbc82009-06-05 14:29:10 +02001152 int hits = samples;
Ingo Molnar07800602009-04-20 15:00:56 +02001153
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001154 mmap_read();
Ingo Molnar07800602009-04-20 15:00:56 +02001155
Ingo Molnar2debbc82009-06-05 14:29:10 +02001156 if (hits == samples)
Ingo Molnar07800602009-04-20 15:00:56 +02001157 ret = poll(event_array, nr_poll, 100);
1158 }
1159
1160 return 0;
1161}
Ingo Molnarb456bae2009-05-26 09:17:18 +02001162
1163static const char * const top_usage[] = {
1164 "perf top [<options>]",
1165 NULL
1166};
1167
Ingo Molnarb456bae2009-05-26 09:17:18 +02001168static const struct option options[] = {
1169 OPT_CALLBACK('e', "event", NULL, "event",
Thomas Gleixner86847b62009-06-06 12:24:17 +02001170 "event selector. use 'perf list' to list available events",
1171 parse_events),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001172 OPT_INTEGER('c', "count", &default_interval,
1173 "event period to sample"),
1174 OPT_INTEGER('p', "pid", &target_pid,
1175 "profile events on existing pid"),
1176 OPT_BOOLEAN('a', "all-cpus", &system_wide,
1177 "system-wide collection from all CPUs"),
1178 OPT_INTEGER('C', "CPU", &profile_cpu,
1179 "CPU to profile on"),
Ingo Molnar83a09442009-08-15 12:26:57 +02001180 OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001181 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
1182 "number of mmap data pages"),
1183 OPT_INTEGER('r', "realtime", &realtime_prio,
1184 "collect data with this RT SCHED_FIFO priority"),
Mike Galbraithdb20c002009-05-26 15:25:34 +02001185 OPT_INTEGER('d', "delay", &delay_secs,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001186 "number of seconds to delay between refreshes"),
1187 OPT_BOOLEAN('D', "dump-symtab", &dump_symtab,
1188 "dump the symbol table used for profiling"),
Ingo Molnar6e53cdf2009-06-04 08:53:05 +02001189 OPT_INTEGER('f', "count-filter", &count_filter,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001190 "only display functions with more events than this"),
1191 OPT_BOOLEAN('g', "group", &group,
1192 "put the counters into a counter group"),
Mike Galbraith0fdc7e62009-07-21 10:30:36 +02001193 OPT_BOOLEAN('i', "inherit", &inherit,
1194 "child tasks inherit counters"),
Mike Galbraith923c42c2009-07-22 20:36:03 +02001195 OPT_STRING('s', "sym-annotate", &sym_filter, "symbol name",
1196 "symbol to annotate - requires -k option"),
Anton Blanchard1f208ea2009-07-01 09:00:44 +10001197 OPT_BOOLEAN('z', "zero", &zero,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001198 "zero history across updates"),
Ingo Molnar6e53cdf2009-06-04 08:53:05 +02001199 OPT_INTEGER('F', "freq", &freq,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001200 "profile at this frequency"),
Ingo Molnar6e53cdf2009-06-04 08:53:05 +02001201 OPT_INTEGER('E', "entries", &print_entries,
1202 "display this many functions"),
Ingo Molnar3da297a2009-06-07 17:39:02 +02001203 OPT_BOOLEAN('v', "verbose", &verbose,
1204 "be more verbose (show counter open errors, etc)"),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001205 OPT_END()
1206};
1207
Ingo Molnarf37a2912009-07-01 12:37:06 +02001208int cmd_top(int argc, const char **argv, const char *prefix __used)
Ingo Molnarb456bae2009-05-26 09:17:18 +02001209{
1210 int counter;
1211
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001212 symbol__init(sizeof(struct sym_entry));
Mike Galbraith42976482009-07-02 08:09:46 +02001213
Ingo Molnarb456bae2009-05-26 09:17:18 +02001214 page_size = sysconf(_SC_PAGE_SIZE);
1215
Ingo Molnarb456bae2009-05-26 09:17:18 +02001216 argc = parse_options(argc, argv, options, top_usage, 0);
1217 if (argc)
1218 usage_with_options(top_usage, options);
1219
Ingo Molnarb456bae2009-05-26 09:17:18 +02001220 /* CPU and PID are mutually exclusive */
1221 if (target_pid != -1 && profile_cpu != -1) {
1222 printf("WARNING: PID switch overriding CPU\n");
1223 sleep(1);
1224 profile_cpu = -1;
1225 }
1226
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001227 if (!nr_counters)
Ingo Molnarb456bae2009-05-26 09:17:18 +02001228 nr_counters = 1;
Ingo Molnarb456bae2009-05-26 09:17:18 +02001229
Frederic Weisbecker2f335a02009-06-05 19:31:01 +02001230 if (delay_secs < 1)
1231 delay_secs = 1;
1232
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001233 parse_symbols();
Mike Galbraith923c42c2009-07-22 20:36:03 +02001234 parse_source(sym_filter_entry);
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001235
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +02001236
1237 /*
1238 * User specified count overrides default frequency.
1239 */
1240 if (default_interval)
1241 freq = 0;
1242 else if (freq) {
1243 default_interval = freq;
1244 } else {
1245 fprintf(stderr, "frequency and count are zero, aborting\n");
1246 exit(EXIT_FAILURE);
1247 }
1248
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001249 /*
1250 * Fill in the ones not specifically initialized via -c:
1251 */
Ingo Molnarb456bae2009-05-26 09:17:18 +02001252 for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001253 if (attrs[counter].sample_period)
Ingo Molnarb456bae2009-05-26 09:17:18 +02001254 continue;
1255
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001256 attrs[counter].sample_period = default_interval;
Ingo Molnarb456bae2009-05-26 09:17:18 +02001257 }
1258
1259 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
1260 assert(nr_cpus <= MAX_NR_CPUS);
1261 assert(nr_cpus >= 0);
1262
1263 if (target_pid != -1 || profile_cpu != -1)
1264 nr_cpus = 1;
1265
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -02001266 if (print_entries == 0) {
1267 update_print_entries();
1268 signal(SIGWINCH, sig_winch_handler);
1269 }
1270
Ingo Molnarb456bae2009-05-26 09:17:18 +02001271 return __cmd_top();
1272}