blob: a368978d517703f4a2f5af79260336d9076bed1b [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
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -020079static bool hide_kernel_symbols = false;
80static bool hide_user_symbols = false;
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -020081static struct winsize winsize;
82static const char *graph_line =
83 "_____________________________________________________________________"
84 "_____________________________________________________________________";
85static const char *graph_dotted_line =
86 "---------------------------------------------------------------------"
87 "---------------------------------------------------------------------"
88 "---------------------------------------------------------------------";
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -020089
Ingo Molnar07800602009-04-20 15:00:56 +020090/*
Mike Galbraith923c42c2009-07-22 20:36:03 +020091 * Source
92 */
93
94struct source_line {
95 u64 eip;
96 unsigned long count[MAX_COUNTERS];
97 char *line;
98 struct source_line *next;
99};
100
Ingo Molnar42e59d72009-10-06 15:14:21 +0200101static char *sym_filter = NULL;
102struct sym_entry *sym_filter_entry = NULL;
103static int sym_pcnt_filter = 5;
104static int sym_counter = 0;
105static int display_weighted = -1;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200106
107/*
Ingo Molnar07800602009-04-20 15:00:56 +0200108 * Symbols
109 */
110
Ingo Molnar07800602009-04-20 15:00:56 +0200111struct sym_entry {
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300112 struct rb_node rb_node;
113 struct list_head node;
Ingo Molnar07800602009-04-20 15:00:56 +0200114 unsigned long count[MAX_COUNTERS];
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300115 unsigned long snap_count;
116 double weight;
Ingo Molnar07800602009-04-20 15:00:56 +0200117 int skip;
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200118 u16 name_len;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200119 u8 origin;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300120 struct map *map;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200121 struct source_line *source;
122 struct source_line *lines;
123 struct source_line **lines_tail;
124 pthread_mutex_t source_lock;
Ingo Molnar07800602009-04-20 15:00:56 +0200125};
126
Mike Galbraith923c42c2009-07-22 20:36:03 +0200127/*
128 * Source functions
129 */
130
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200131static void get_term_dimensions(struct winsize *ws)
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200132{
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200133 char *s = getenv("LINES");
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200134
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200135 if (s != NULL) {
136 ws->ws_row = atoi(s);
137 s = getenv("COLUMNS");
138 if (s != NULL) {
139 ws->ws_col = atoi(s);
140 if (ws->ws_row && ws->ws_col)
141 return;
142 }
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200143 }
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200144#ifdef TIOCGWINSZ
145 if (ioctl(1, TIOCGWINSZ, ws) == 0 &&
146 ws->ws_row && ws->ws_col)
147 return;
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200148#endif
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200149 ws->ws_row = 25;
150 ws->ws_col = 80;
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200151}
152
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200153static void update_print_entries(struct winsize *ws)
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200154{
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200155 print_entries = ws->ws_row;
156
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200157 if (print_entries > 9)
158 print_entries -= 9;
159}
160
161static void sig_winch_handler(int sig __used)
162{
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200163 get_term_dimensions(&winsize);
164 update_print_entries(&winsize);
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200165}
166
Mike Galbraith923c42c2009-07-22 20:36:03 +0200167static void parse_source(struct sym_entry *syme)
168{
169 struct symbol *sym;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300170 struct map *map;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200171 FILE *file;
Ingo Molnar83a09442009-08-15 12:26:57 +0200172 char command[PATH_MAX*2];
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300173 const char *path;
174 u64 len;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200175
176 if (!syme)
177 return;
178
179 if (syme->lines) {
180 pthread_mutex_lock(&syme->source_lock);
181 goto out_assign;
182 }
183
184 sym = (struct symbol *)(syme + 1);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300185 map = syme->map;
186 path = map->dso->long_name;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200187
Mike Galbraith923c42c2009-07-22 20:36:03 +0200188 len = sym->end - sym->start;
189
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300190 sprintf(command,
191 "objdump --start-address=0x%016Lx "
192 "--stop-address=0x%016Lx -dS %s",
Arnaldo Carvalho de Meloc88e4bf2009-10-20 15:54:55 -0200193 map->unmap_ip(map, sym->start),
194 map->unmap_ip(map, sym->end), path);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200195
196 file = popen(command, "r");
197 if (!file)
198 return;
199
200 pthread_mutex_lock(&syme->source_lock);
201 syme->lines_tail = &syme->lines;
202 while (!feof(file)) {
203 struct source_line *src;
204 size_t dummy = 0;
205 char *c;
206
207 src = malloc(sizeof(struct source_line));
208 assert(src != NULL);
209 memset(src, 0, sizeof(struct source_line));
210
211 if (getline(&src->line, &dummy, file) < 0)
212 break;
213 if (!src->line)
214 break;
215
216 c = strchr(src->line, '\n');
217 if (c)
218 *c = 0;
219
220 src->next = NULL;
221 *syme->lines_tail = src;
222 syme->lines_tail = &src->next;
223
224 if (strlen(src->line)>8 && src->line[8] == ':') {
225 src->eip = strtoull(src->line, NULL, 16);
Arnaldo Carvalho de Meloc88e4bf2009-10-20 15:54:55 -0200226 src->eip = map->unmap_ip(map, src->eip);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200227 }
228 if (strlen(src->line)>8 && src->line[16] == ':') {
229 src->eip = strtoull(src->line, NULL, 16);
Arnaldo Carvalho de Meloc88e4bf2009-10-20 15:54:55 -0200230 src->eip = map->unmap_ip(map, src->eip);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200231 }
232 }
233 pclose(file);
234out_assign:
235 sym_filter_entry = syme;
236 pthread_mutex_unlock(&syme->source_lock);
237}
238
239static void __zero_source_counters(struct sym_entry *syme)
240{
241 int i;
242 struct source_line *line;
243
244 line = syme->lines;
245 while (line) {
246 for (i = 0; i < nr_counters; i++)
247 line->count[i] = 0;
248 line = line->next;
249 }
250}
251
252static void record_precise_ip(struct sym_entry *syme, int counter, u64 ip)
253{
254 struct source_line *line;
255
256 if (syme != sym_filter_entry)
257 return;
258
259 if (pthread_mutex_trylock(&syme->source_lock))
260 return;
261
262 if (!syme->source)
263 goto out_unlock;
264
265 for (line = syme->lines; line; line = line->next) {
266 if (line->eip == ip) {
267 line->count[counter]++;
268 break;
269 }
270 if (line->eip > ip)
271 break;
272 }
273out_unlock:
274 pthread_mutex_unlock(&syme->source_lock);
275}
276
277static void lookup_sym_source(struct sym_entry *syme)
278{
279 struct symbol *symbol = (struct symbol *)(syme + 1);
280 struct source_line *line;
281 char pattern[PATH_MAX];
Mike Galbraith923c42c2009-07-22 20:36:03 +0200282
283 sprintf(pattern, "<%s>:", symbol->name);
284
Mike Galbraith923c42c2009-07-22 20:36:03 +0200285 pthread_mutex_lock(&syme->source_lock);
286 for (line = syme->lines; line; line = line->next) {
287 if (strstr(line->line, pattern)) {
288 syme->source = line;
289 break;
290 }
291 }
292 pthread_mutex_unlock(&syme->source_lock);
293}
294
295static void show_lines(struct source_line *queue, int count, int total)
296{
297 int i;
298 struct source_line *line;
299
300 line = queue;
301 for (i = 0; i < count; i++) {
302 float pcnt = 100.0*(float)line->count[sym_counter]/(float)total;
303
304 printf("%8li %4.1f%%\t%s\n", line->count[sym_counter], pcnt, line->line);
305 line = line->next;
306 }
307}
308
309#define TRACE_COUNT 3
310
311static void show_details(struct sym_entry *syme)
312{
313 struct symbol *symbol;
314 struct source_line *line;
315 struct source_line *line_queue = NULL;
316 int displayed = 0;
317 int line_queue_count = 0, total = 0, more = 0;
318
319 if (!syme)
320 return;
321
322 if (!syme->source)
323 lookup_sym_source(syme);
324
325 if (!syme->source)
326 return;
327
328 symbol = (struct symbol *)(syme + 1);
329 printf("Showing %s for %s\n", event_name(sym_counter), symbol->name);
330 printf(" Events Pcnt (>=%d%%)\n", sym_pcnt_filter);
331
332 pthread_mutex_lock(&syme->source_lock);
333 line = syme->source;
334 while (line) {
335 total += line->count[sym_counter];
336 line = line->next;
337 }
338
339 line = syme->source;
340 while (line) {
341 float pcnt = 0.0;
342
343 if (!line_queue_count)
344 line_queue = line;
345 line_queue_count++;
346
347 if (line->count[sym_counter])
348 pcnt = 100.0 * line->count[sym_counter] / (float)total;
349 if (pcnt >= (float)sym_pcnt_filter) {
350 if (displayed <= print_entries)
351 show_lines(line_queue, line_queue_count, total);
352 else more++;
353 displayed += line_queue_count;
354 line_queue_count = 0;
355 line_queue = NULL;
356 } else if (line_queue_count > TRACE_COUNT) {
357 line_queue = line_queue->next;
358 line_queue_count--;
359 }
360
361 line->count[sym_counter] = zero ? 0 : line->count[sym_counter] * 7 / 8;
362 line = line->next;
363 }
364 pthread_mutex_unlock(&syme->source_lock);
365 if (more)
366 printf("%d lines not displayed, maybe increase display entries [e]\n", more);
367}
Ingo Molnar07800602009-04-20 15:00:56 +0200368
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300369/*
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200370 * Symbols will be added here in event__process_sample and will get out
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300371 * after decayed.
372 */
373static LIST_HEAD(active_symbols);
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300374static pthread_mutex_t active_symbols_lock = PTHREAD_MUTEX_INITIALIZER;
Ingo Molnar07800602009-04-20 15:00:56 +0200375
Ingo Molnar07800602009-04-20 15:00:56 +0200376/*
377 * Ordering weight: count-1 * count-2 * ... / count-n
378 */
379static double sym_weight(const struct sym_entry *sym)
380{
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300381 double weight = sym->snap_count;
Ingo Molnar07800602009-04-20 15:00:56 +0200382 int counter;
383
Mike Galbraith46ab9762009-07-24 10:09:50 +0200384 if (!display_weighted)
385 return weight;
386
Ingo Molnar07800602009-04-20 15:00:56 +0200387 for (counter = 1; counter < nr_counters-1; counter++)
388 weight *= sym->count[counter];
389
390 weight /= (sym->count[counter] + 1);
391
392 return weight;
393}
394
Ingo Molnar2debbc82009-06-05 14:29:10 +0200395static long samples;
396static long userspace_samples;
Ingo Molnar07800602009-04-20 15:00:56 +0200397static const char CONSOLE_CLEAR[] = "";
398
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300399static void __list_insert_active_sym(struct sym_entry *syme)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300400{
401 list_add(&syme->node, &active_symbols);
402}
403
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300404static void list_remove_active_sym(struct sym_entry *syme)
405{
406 pthread_mutex_lock(&active_symbols_lock);
407 list_del_init(&syme->node);
408 pthread_mutex_unlock(&active_symbols_lock);
409}
410
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300411static void rb_insert_active_sym(struct rb_root *tree, struct sym_entry *se)
412{
413 struct rb_node **p = &tree->rb_node;
414 struct rb_node *parent = NULL;
415 struct sym_entry *iter;
416
417 while (*p != NULL) {
418 parent = *p;
419 iter = rb_entry(parent, struct sym_entry, rb_node);
420
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300421 if (se->weight > iter->weight)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300422 p = &(*p)->rb_left;
423 else
424 p = &(*p)->rb_right;
425 }
426
427 rb_link_node(&se->rb_node, parent, p);
428 rb_insert_color(&se->rb_node, tree);
429}
Ingo Molnar07800602009-04-20 15:00:56 +0200430
431static void print_sym_table(void)
432{
Ingo Molnar233f0b92009-06-03 21:48:40 +0200433 int printed = 0, j;
Mike Galbraith46ab9762009-07-24 10:09:50 +0200434 int counter, snap = !display_weighted ? sym_counter : 0;
Ingo Molnar2debbc82009-06-05 14:29:10 +0200435 float samples_per_sec = samples/delay_secs;
436 float ksamples_per_sec = (samples-userspace_samples)/delay_secs;
437 float sum_ksamples = 0.0;
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300438 struct sym_entry *syme, *n;
439 struct rb_root tmp = RB_ROOT;
440 struct rb_node *nd;
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200441 int sym_width = 0, dso_width;
442 const int win_width = winsize.ws_col - 1;
Ingo Molnar07800602009-04-20 15:00:56 +0200443
Ingo Molnar2debbc82009-06-05 14:29:10 +0200444 samples = userspace_samples = 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200445
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300446 /* Sort the active symbols */
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300447 pthread_mutex_lock(&active_symbols_lock);
448 syme = list_entry(active_symbols.next, struct sym_entry, node);
449 pthread_mutex_unlock(&active_symbols_lock);
450
451 list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
Mike Galbraith46ab9762009-07-24 10:09:50 +0200452 syme->snap_count = syme->count[snap];
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300453 if (syme->snap_count != 0) {
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200454
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200455 if ((hide_user_symbols &&
456 syme->origin == PERF_RECORD_MISC_USER) ||
457 (hide_kernel_symbols &&
458 syme->origin == PERF_RECORD_MISC_KERNEL)) {
459 list_remove_active_sym(syme);
460 continue;
461 }
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300462 syme->weight = sym_weight(syme);
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300463 rb_insert_active_sym(&tmp, syme);
Ingo Molnar2debbc82009-06-05 14:29:10 +0200464 sum_ksamples += syme->snap_count;
Mike Galbraithd94b9432009-05-25 09:57:56 +0200465
466 for (j = 0; j < nr_counters; j++)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300467 syme->count[j] = zero ? 0 : syme->count[j] * 7 / 8;
468 } else
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300469 list_remove_active_sym(syme);
Mike Galbraithd94b9432009-05-25 09:57:56 +0200470 }
471
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200472 puts(CONSOLE_CLEAR);
Ingo Molnar07800602009-04-20 15:00:56 +0200473
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200474 printf("%-*.*s\n", win_width, win_width, graph_dotted_line);
Ingo Molnarf2521b62009-06-03 19:17:25 +0200475 printf( " PerfTop:%8.0f irqs/sec kernel:%4.1f%% [",
Ingo Molnar2debbc82009-06-05 14:29:10 +0200476 samples_per_sec,
477 100.0 - (100.0*((samples_per_sec-ksamples_per_sec)/samples_per_sec)));
Ingo Molnar07800602009-04-20 15:00:56 +0200478
Mike Galbraith46ab9762009-07-24 10:09:50 +0200479 if (nr_counters == 1 || !display_weighted) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000480 printf("%Ld", (u64)attrs[0].sample_period);
Ingo Molnarcf1f4572009-06-05 13:27:02 +0200481 if (freq)
482 printf("Hz ");
483 else
484 printf(" ");
485 }
Ingo Molnar07800602009-04-20 15:00:56 +0200486
Mike Galbraith46ab9762009-07-24 10:09:50 +0200487 if (!display_weighted)
488 printf("%s", event_name(sym_counter));
489 else for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnar07800602009-04-20 15:00:56 +0200490 if (counter)
491 printf("/");
492
493 printf("%s", event_name(counter));
494 }
495
496 printf( "], ");
497
Ingo Molnarb456bae2009-05-26 09:17:18 +0200498 if (target_pid != -1)
499 printf(" (target_pid: %d", target_pid);
Ingo Molnar07800602009-04-20 15:00:56 +0200500 else
501 printf(" (all");
502
503 if (profile_cpu != -1)
504 printf(", cpu: %d)\n", profile_cpu);
505 else {
Ingo Molnarb456bae2009-05-26 09:17:18 +0200506 if (target_pid != -1)
Ingo Molnar07800602009-04-20 15:00:56 +0200507 printf(")\n");
508 else
509 printf(", %d CPUs)\n", nr_cpus);
510 }
511
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200512 printf("%-*.*s\n\n", win_width, win_width, graph_dotted_line);
Ingo Molnar07800602009-04-20 15:00:56 +0200513
Mike Galbraith923c42c2009-07-22 20:36:03 +0200514 if (sym_filter_entry) {
515 show_details(sym_filter_entry);
516 return;
517 }
518
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200519 /*
520 * Find the longest symbol name that will be displayed
521 */
522 for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
523 syme = rb_entry(nd, struct sym_entry, rb_node);
524 if (++printed > print_entries ||
525 (int)syme->snap_count < count_filter)
526 continue;
527
528 if (syme->name_len > sym_width)
529 sym_width = syme->name_len;
530 }
531
532 printed = 0;
533
Ingo Molnar07800602009-04-20 15:00:56 +0200534 if (nr_counters == 1)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200535 printf(" samples pcnt");
Ingo Molnar07800602009-04-20 15:00:56 +0200536 else
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200537 printf(" weight samples pcnt");
Ingo Molnar07800602009-04-20 15:00:56 +0200538
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200539 dso_width = winsize.ws_col - sym_width - 29;
540
Arnaldo Carvalho de Melo7ced1562009-08-26 11:51:26 -0300541 if (verbose)
542 printf(" RIP ");
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200543 printf(" %-*.*s DSO\n", sym_width, sym_width, "function");
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200544 printf(" %s _______ _____",
Arnaldo Carvalho de Melo7ced1562009-08-26 11:51:26 -0300545 nr_counters == 1 ? " " : "______");
546 if (verbose)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200547 printf(" ________________");
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200548 printf(" %-*.*s %-*.*s\n\n", sym_width, sym_width, graph_line,
549 dso_width, dso_width, graph_line);
Ingo Molnar07800602009-04-20 15:00:56 +0200550
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300551 for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
Ingo Molnar83a09442009-08-15 12:26:57 +0200552 struct symbol *sym;
Ingo Molnar8fc03212009-06-04 15:19:47 +0200553 double pcnt;
Ingo Molnar07800602009-04-20 15:00:56 +0200554
Ingo Molnar83a09442009-08-15 12:26:57 +0200555 syme = rb_entry(nd, struct sym_entry, rb_node);
556 sym = (struct symbol *)(syme + 1);
557
Mike Galbraith923c42c2009-07-22 20:36:03 +0200558 if (++printed > print_entries || (int)syme->snap_count < count_filter)
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300559 continue;
Ingo Molnar07800602009-04-20 15:00:56 +0200560
Ingo Molnar2debbc82009-06-05 14:29:10 +0200561 pcnt = 100.0 - (100.0 * ((sum_ksamples - syme->snap_count) /
562 sum_ksamples));
Mike Galbraithd94b9432009-05-25 09:57:56 +0200563
Mike Galbraith46ab9762009-07-24 10:09:50 +0200564 if (nr_counters == 1 || !display_weighted)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200565 printf("%20.2f ", syme->weight);
Mike Galbraithd94b9432009-05-25 09:57:56 +0200566 else
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200567 printf("%9.1f %10ld ", syme->weight, syme->snap_count);
Ingo Molnar8fc03212009-06-04 15:19:47 +0200568
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200569 percent_color_fprintf(stdout, "%4.1f%%", pcnt);
Arnaldo Carvalho de Melo7ced1562009-08-26 11:51:26 -0300570 if (verbose)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200571 printf(" %016llx", sym->start);
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200572 printf(" %-*.*s", sym_width, sym_width, sym->name);
573 printf(" %-*.*s", dso_width, dso_width,
574 dso_width >= syme->map->dso->long_name_len ?
575 syme->map->dso->long_name :
576 syme->map->dso->short_name);
Mike Galbraith42976482009-07-02 08:09:46 +0200577 printf("\n");
Ingo Molnar07800602009-04-20 15:00:56 +0200578 }
Ingo Molnar07800602009-04-20 15:00:56 +0200579}
580
Mike Galbraith923c42c2009-07-22 20:36:03 +0200581static void prompt_integer(int *target, const char *msg)
582{
583 char *buf = malloc(0), *p;
584 size_t dummy = 0;
585 int tmp;
586
587 fprintf(stdout, "\n%s: ", msg);
588 if (getline(&buf, &dummy, stdin) < 0)
589 return;
590
591 p = strchr(buf, '\n');
592 if (p)
593 *p = 0;
594
595 p = buf;
596 while(*p) {
597 if (!isdigit(*p))
598 goto out_free;
599 p++;
600 }
601 tmp = strtoul(buf, NULL, 10);
602 *target = tmp;
603out_free:
604 free(buf);
605}
606
607static void prompt_percent(int *target, const char *msg)
608{
609 int tmp = 0;
610
611 prompt_integer(&tmp, msg);
612 if (tmp >= 0 && tmp <= 100)
613 *target = tmp;
614}
615
616static void prompt_symbol(struct sym_entry **target, const char *msg)
617{
618 char *buf = malloc(0), *p;
619 struct sym_entry *syme = *target, *n, *found = NULL;
620 size_t dummy = 0;
621
622 /* zero counters of active symbol */
623 if (syme) {
624 pthread_mutex_lock(&syme->source_lock);
625 __zero_source_counters(syme);
626 *target = NULL;
627 pthread_mutex_unlock(&syme->source_lock);
628 }
629
630 fprintf(stdout, "\n%s: ", msg);
631 if (getline(&buf, &dummy, stdin) < 0)
632 goto out_free;
633
634 p = strchr(buf, '\n');
635 if (p)
636 *p = 0;
637
638 pthread_mutex_lock(&active_symbols_lock);
639 syme = list_entry(active_symbols.next, struct sym_entry, node);
640 pthread_mutex_unlock(&active_symbols_lock);
641
642 list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
643 struct symbol *sym = (struct symbol *)(syme + 1);
644
645 if (!strcmp(buf, sym->name)) {
646 found = syme;
647 break;
648 }
649 }
650
651 if (!found) {
652 fprintf(stderr, "Sorry, %s is not active.\n", sym_filter);
653 sleep(1);
654 return;
655 } else
656 parse_source(found);
657
658out_free:
659 free(buf);
660}
661
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200662static void print_mapped_keys(void)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200663{
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200664 char *name = NULL;
665
666 if (sym_filter_entry) {
667 struct symbol *sym = (struct symbol *)(sym_filter_entry+1);
668 name = sym->name;
669 }
670
671 fprintf(stdout, "\nMapped keys:\n");
672 fprintf(stdout, "\t[d] display refresh delay. \t(%d)\n", delay_secs);
673 fprintf(stdout, "\t[e] display entries (lines). \t(%d)\n", print_entries);
674
675 if (nr_counters > 1)
676 fprintf(stdout, "\t[E] active event counter. \t(%s)\n", event_name(sym_counter));
677
678 fprintf(stdout, "\t[f] profile display filter (count). \t(%d)\n", count_filter);
679
Ingo Molnar83a09442009-08-15 12:26:57 +0200680 if (vmlinux_name) {
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200681 fprintf(stdout, "\t[F] annotate display filter (percent). \t(%d%%)\n", sym_pcnt_filter);
682 fprintf(stdout, "\t[s] annotate symbol. \t(%s)\n", name?: "NULL");
683 fprintf(stdout, "\t[S] stop annotation.\n");
684 }
685
686 if (nr_counters > 1)
687 fprintf(stdout, "\t[w] toggle display weighted/count[E]r. \t(%d)\n", display_weighted ? 1 : 0);
688
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200689 fprintf(stdout,
690 "\t[K] hide kernel_symbols symbols. \t(%s)\n",
691 hide_kernel_symbols ? "yes" : "no");
692 fprintf(stdout,
693 "\t[U] hide user symbols. \t(%s)\n",
694 hide_user_symbols ? "yes" : "no");
Mike Galbraith46ab9762009-07-24 10:09:50 +0200695 fprintf(stdout, "\t[z] toggle sample zeroing. \t(%d)\n", zero ? 1 : 0);
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200696 fprintf(stdout, "\t[qQ] quit.\n");
697}
698
699static int key_mapped(int c)
700{
701 switch (c) {
702 case 'd':
703 case 'e':
704 case 'f':
705 case 'z':
706 case 'q':
707 case 'Q':
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200708 case 'K':
709 case 'U':
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200710 return 1;
711 case 'E':
712 case 'w':
713 return nr_counters > 1 ? 1 : 0;
714 case 'F':
715 case 's':
716 case 'S':
Ingo Molnar83a09442009-08-15 12:26:57 +0200717 return vmlinux_name ? 1 : 0;
718 default:
719 break;
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200720 }
721
722 return 0;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200723}
724
725static void handle_keypress(int c)
726{
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200727 if (!key_mapped(c)) {
728 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
729 struct termios tc, save;
730
731 print_mapped_keys();
732 fprintf(stdout, "\nEnter selection, or unmapped key to continue: ");
733 fflush(stdout);
734
735 tcgetattr(0, &save);
736 tc = save;
737 tc.c_lflag &= ~(ICANON | ECHO);
738 tc.c_cc[VMIN] = 0;
739 tc.c_cc[VTIME] = 0;
740 tcsetattr(0, TCSANOW, &tc);
741
742 poll(&stdin_poll, 1, -1);
743 c = getc(stdin);
744
745 tcsetattr(0, TCSAFLUSH, &save);
746 if (!key_mapped(c))
747 return;
748 }
749
Mike Galbraith923c42c2009-07-22 20:36:03 +0200750 switch (c) {
751 case 'd':
752 prompt_integer(&delay_secs, "Enter display delay");
Tim Blechmanndc799592009-10-17 18:08:29 +0200753 if (delay_secs < 1)
754 delay_secs = 1;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200755 break;
756 case 'e':
757 prompt_integer(&print_entries, "Enter display entries (lines)");
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200758 if (print_entries == 0) {
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200759 sig_winch_handler(SIGWINCH);
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200760 signal(SIGWINCH, sig_winch_handler);
761 } else
762 signal(SIGWINCH, SIG_DFL);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200763 break;
764 case 'E':
765 if (nr_counters > 1) {
766 int i;
767
768 fprintf(stderr, "\nAvailable events:");
769 for (i = 0; i < nr_counters; i++)
770 fprintf(stderr, "\n\t%d %s", i, event_name(i));
771
772 prompt_integer(&sym_counter, "Enter details event counter");
773
774 if (sym_counter >= nr_counters) {
775 fprintf(stderr, "Sorry, no such event, using %s.\n", event_name(0));
776 sym_counter = 0;
777 sleep(1);
778 }
779 } else sym_counter = 0;
780 break;
781 case 'f':
782 prompt_integer(&count_filter, "Enter display event count filter");
783 break;
784 case 'F':
785 prompt_percent(&sym_pcnt_filter, "Enter details display event filter (percent)");
786 break;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200787 case 'K':
788 hide_kernel_symbols = !hide_kernel_symbols;
789 break;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200790 case 'q':
791 case 'Q':
792 printf("exiting.\n");
793 exit(0);
794 case 's':
795 prompt_symbol(&sym_filter_entry, "Enter details symbol");
796 break;
797 case 'S':
798 if (!sym_filter_entry)
799 break;
800 else {
801 struct sym_entry *syme = sym_filter_entry;
802
803 pthread_mutex_lock(&syme->source_lock);
804 sym_filter_entry = NULL;
805 __zero_source_counters(syme);
806 pthread_mutex_unlock(&syme->source_lock);
807 }
808 break;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200809 case 'U':
810 hide_user_symbols = !hide_user_symbols;
811 break;
Mike Galbraith46ab9762009-07-24 10:09:50 +0200812 case 'w':
813 display_weighted = ~display_weighted;
814 break;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200815 case 'z':
816 zero = ~zero;
817 break;
Ingo Molnar83a09442009-08-15 12:26:57 +0200818 default:
819 break;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200820 }
821}
822
Ingo Molnarf37a2912009-07-01 12:37:06 +0200823static void *display_thread(void *arg __used)
Ingo Molnar07800602009-04-20 15:00:56 +0200824{
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200825 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
Mike Galbraith923c42c2009-07-22 20:36:03 +0200826 struct termios tc, save;
827 int delay_msecs, c;
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200828
Mike Galbraith923c42c2009-07-22 20:36:03 +0200829 tcgetattr(0, &save);
830 tc = save;
831 tc.c_lflag &= ~(ICANON | ECHO);
832 tc.c_cc[VMIN] = 0;
833 tc.c_cc[VTIME] = 0;
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200834
Mike Galbraith923c42c2009-07-22 20:36:03 +0200835repeat:
836 delay_msecs = delay_secs * 1000;
837 tcsetattr(0, TCSANOW, &tc);
838 /* trash return*/
839 getc(stdin);
Ingo Molnar07800602009-04-20 15:00:56 +0200840
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200841 do {
Ingo Molnar07800602009-04-20 15:00:56 +0200842 print_sym_table();
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200843 } while (!poll(&stdin_poll, 1, delay_msecs) == 1);
844
Mike Galbraith923c42c2009-07-22 20:36:03 +0200845 c = getc(stdin);
846 tcsetattr(0, TCSAFLUSH, &save);
847
848 handle_keypress(c);
849 goto repeat;
Ingo Molnar07800602009-04-20 15:00:56 +0200850
851 return NULL;
852}
853
Anton Blanchard2ab52082009-07-01 09:00:46 +1000854/* Tag samples to be skipped. */
Ingo Molnarf37a2912009-07-01 12:37:06 +0200855static const char *skip_symbols[] = {
Anton Blanchard2ab52082009-07-01 09:00:46 +1000856 "default_idle",
857 "cpu_idle",
858 "enter_idle",
859 "exit_idle",
860 "mwait_idle",
Arnaldo Carvalho de Melo59b90052009-07-26 19:06:19 -0300861 "mwait_idle_with_hints",
Arnaldo Carvalho de Melo83572752009-09-25 15:02:39 -0700862 "poll_idle",
Anton Blanchard3a3393e2009-07-01 09:00:47 +1000863 "ppc64_runlatch_off",
864 "pseries_dedicated_idle_sleep",
Anton Blanchard2ab52082009-07-01 09:00:46 +1000865 NULL
866};
867
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300868static int symbol_filter(struct map *map, struct symbol *sym)
Ingo Molnar07800602009-04-20 15:00:56 +0200869{
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300870 struct sym_entry *syme;
871 const char *name = sym->name;
Anton Blanchard2ab52082009-07-01 09:00:46 +1000872 int i;
Ingo Molnar07800602009-04-20 15:00:56 +0200873
Anton Blanchard3a3393e2009-07-01 09:00:47 +1000874 /*
875 * ppc64 uses function descriptors and appends a '.' to the
876 * start of every instruction address. Remove it.
877 */
878 if (name[0] == '.')
879 name++;
880
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300881 if (!strcmp(name, "_text") ||
882 !strcmp(name, "_etext") ||
883 !strcmp(name, "_sinittext") ||
884 !strncmp("init_module", name, 11) ||
885 !strncmp("cleanup_module", name, 14) ||
886 strstr(name, "_text_start") ||
887 strstr(name, "_text_end"))
Ingo Molnar07800602009-04-20 15:00:56 +0200888 return 1;
889
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200890 syme = symbol__priv(sym);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300891 syme->map = map;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200892 pthread_mutex_init(&syme->source_lock, NULL);
893 if (!sym_filter_entry && sym_filter && !strcmp(name, sym_filter))
894 sym_filter_entry = syme;
895
Anton Blanchard2ab52082009-07-01 09:00:46 +1000896 for (i = 0; skip_symbols[i]; i++) {
897 if (!strcmp(skip_symbols[i], name)) {
898 syme->skip = 1;
899 break;
900 }
901 }
Ingo Molnar07800602009-04-20 15:00:56 +0200902
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200903 if (!syme->skip)
904 syme->name_len = strlen(sym->name);
905
Ingo Molnar07800602009-04-20 15:00:56 +0200906 return 0;
907}
908
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300909static int parse_symbols(void)
Ingo Molnar07800602009-04-20 15:00:56 +0200910{
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200911 if (dsos__load_kernel(vmlinux_name, symbol_filter, 1) <= 0)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300912 return -1;
Ingo Molnar07800602009-04-20 15:00:56 +0200913
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300914 if (dump_symtab)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300915 dsos__fprintf(stderr);
Ingo Molnar07800602009-04-20 15:00:56 +0200916
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300917 return 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200918}
919
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200920static void event__process_sample(const event_t *self, int counter)
Ingo Molnar07800602009-04-20 15:00:56 +0200921{
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200922 u64 ip = self->ip.ip;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300923 struct map *map;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200924 struct sym_entry *syme;
925 struct symbol *sym;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200926 u8 origin = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Ingo Molnar07800602009-04-20 15:00:56 +0200927
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200928 switch (origin) {
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200929 case PERF_RECORD_MISC_USER: {
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200930 struct thread *thread;
Ingo Molnar07800602009-04-20 15:00:56 +0200931
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200932 if (hide_user_symbols)
933 return;
934
935 thread = threads__findnew(self->ip.pid);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200936 if (thread == NULL)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300937 return;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200938
939 map = thread__find_map(thread, ip);
940 if (map != NULL) {
941 ip = map->map_ip(map, ip);
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200942 sym = map__find_symbol(map, ip, symbol_filter);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200943 if (sym == NULL)
944 return;
945 userspace_samples++;
946 break;
Ingo Molnar07800602009-04-20 15:00:56 +0200947 }
Ingo Molnar07800602009-04-20 15:00:56 +0200948 }
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200949 /*
950 * If this is outside of all known maps,
951 * and is a negative address, try to look it
952 * up in the kernel dso, as it might be a
953 * vsyscall or vdso (which executes in user-mode).
954 */
955 if ((long long)ip >= 0)
956 return;
957 /* Fall thru */
958 case PERF_RECORD_MISC_KERNEL:
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200959 if (hide_kernel_symbols)
960 return;
961
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200962 sym = kernel_maps__find_symbol(ip, &map);
963 if (sym == NULL)
964 return;
965 break;
966 default:
Ingo Molnar07800602009-04-20 15:00:56 +0200967 return;
968 }
969
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200970 syme = symbol__priv(sym);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200971
972 if (!syme->skip) {
973 syme->count[counter]++;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200974 syme->origin = origin;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200975 record_precise_ip(syme, counter, ip);
976 pthread_mutex_lock(&active_symbols_lock);
977 if (list_empty(&syme->node) || !syme->node.next)
978 __list_insert_active_sym(syme);
979 pthread_mutex_unlock(&active_symbols_lock);
980 ++samples;
981 return;
982 }
983}
984
985static void event__process_mmap(event_t *self)
986{
987 struct thread *thread = threads__findnew(self->mmap.pid);
988
989 if (thread != NULL) {
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200990 struct map *map = map__new(&self->mmap, NULL, 0);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200991 if (map != NULL)
992 thread__insert_map(thread, map);
993 }
994}
995
996static void event__process_comm(event_t *self)
997{
998 struct thread *thread = threads__findnew(self->comm.pid);
999
1000 if (thread != NULL)
1001 thread__set_comm(thread, self->comm.comm);
1002}
1003
1004static int event__process(event_t *event)
1005{
1006 switch (event->header.type) {
1007 case PERF_RECORD_COMM:
1008 event__process_comm(event);
1009 break;
1010 case PERF_RECORD_MMAP:
1011 event__process_mmap(event);
1012 break;
1013 default:
1014 break;
1015 }
1016
1017 return 0;
Ingo Molnar07800602009-04-20 15:00:56 +02001018}
1019
Ingo Molnar07800602009-04-20 15:00:56 +02001020struct mmap_data {
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001021 int counter;
1022 void *base;
Ingo Molnarf37a2912009-07-01 12:37:06 +02001023 int mask;
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001024 unsigned int prev;
Ingo Molnar07800602009-04-20 15:00:56 +02001025};
1026
1027static unsigned int mmap_read_head(struct mmap_data *md)
1028{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001029 struct perf_event_mmap_page *pc = md->base;
Ingo Molnar07800602009-04-20 15:00:56 +02001030 int head;
1031
1032 head = pc->data_head;
1033 rmb();
1034
1035 return head;
1036}
1037
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001038static void mmap_read_counter(struct mmap_data *md)
Ingo Molnar07800602009-04-20 15:00:56 +02001039{
1040 unsigned int head = mmap_read_head(md);
1041 unsigned int old = md->prev;
1042 unsigned char *data = md->base + page_size;
1043 int diff;
1044
Ingo Molnar07800602009-04-20 15:00:56 +02001045 /*
1046 * If we're further behind than half the buffer, there's a chance
Ingo Molnar2debbc82009-06-05 14:29:10 +02001047 * the writer will bite our tail and mess up the samples under us.
Ingo Molnar07800602009-04-20 15:00:56 +02001048 *
1049 * If we somehow ended up ahead of the head, we got messed up.
1050 *
1051 * In either case, truncate and restart at head.
1052 */
1053 diff = head - old;
1054 if (diff > md->mask / 2 || diff < 0) {
Mike Galbraithf4f0b412009-10-13 14:57:20 +02001055 fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
Ingo Molnar07800602009-04-20 15:00:56 +02001056
1057 /*
1058 * head points to a known good entry, start there.
1059 */
1060 old = head;
1061 }
1062
Ingo Molnar07800602009-04-20 15:00:56 +02001063 for (; old != head;) {
Ingo Molnar07800602009-04-20 15:00:56 +02001064 event_t *event = (event_t *)&data[old & md->mask];
1065
1066 event_t event_copy;
1067
Ingo Molnar6f06ccb2009-04-20 15:22:22 +02001068 size_t size = event->header.size;
Ingo Molnar07800602009-04-20 15:00:56 +02001069
1070 /*
1071 * Event straddles the mmap boundary -- header should always
1072 * be inside due to u64 alignment of output.
1073 */
1074 if ((old & md->mask) + size != ((old + size) & md->mask)) {
1075 unsigned int offset = old;
1076 unsigned int len = min(sizeof(*event), size), cpy;
1077 void *dst = &event_copy;
1078
1079 do {
1080 cpy = min(md->mask + 1 - (offset & md->mask), len);
1081 memcpy(dst, &data[offset & md->mask], cpy);
1082 offset += cpy;
1083 dst += cpy;
1084 len -= cpy;
1085 } while (len);
1086
1087 event = &event_copy;
1088 }
1089
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001090 if (event->header.type == PERF_RECORD_SAMPLE)
1091 event__process_sample(event, md->counter);
1092 else
1093 event__process(event);
Ingo Molnar07800602009-04-20 15:00:56 +02001094 old += size;
Ingo Molnar07800602009-04-20 15:00:56 +02001095 }
1096
1097 md->prev = old;
1098}
1099
Mike Galbraithc2990a22009-05-24 08:35:49 +02001100static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
1101static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
1102
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001103static void mmap_read(void)
1104{
1105 int i, counter;
1106
1107 for (i = 0; i < nr_cpus; i++) {
1108 for (counter = 0; counter < nr_counters; counter++)
1109 mmap_read_counter(&mmap_array[i][counter]);
1110 }
1111}
1112
Ingo Molnar716c69f2009-06-07 17:31:52 +02001113int nr_poll;
1114int group_fd;
1115
1116static void start_counter(int i, int counter)
Ingo Molnar07800602009-04-20 15:00:56 +02001117{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001118 struct perf_event_attr *attr;
Mike Galbraith0fdc7e62009-07-21 10:30:36 +02001119 int cpu;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001120
1121 cpu = profile_cpu;
1122 if (target_pid == -1 && profile_cpu == -1)
1123 cpu = i;
1124
1125 attr = attrs + counter;
1126
1127 attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +02001128
1129 if (freq) {
1130 attr->sample_type |= PERF_SAMPLE_PERIOD;
1131 attr->freq = 1;
1132 attr->sample_freq = freq;
1133 }
1134
Mike Galbraith0fdc7e62009-07-21 10:30:36 +02001135 attr->inherit = (cpu < 0) && inherit;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001136 attr->mmap = 1;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001137
1138try_again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001139 fd[i][counter] = sys_perf_event_open(attr, target_pid, cpu, group_fd, 0);
Ingo Molnar716c69f2009-06-07 17:31:52 +02001140
1141 if (fd[i][counter] < 0) {
1142 int err = errno;
1143
Pekka Enbergc10edee2009-11-08 18:01:06 +02001144 if (err == EPERM || err == EACCES)
Ingo Molnar3da297a2009-06-07 17:39:02 +02001145 die("No permission - are you root?\n");
Ingo Molnar716c69f2009-06-07 17:31:52 +02001146 /*
1147 * If it's cycles then fall back to hrtimer
1148 * based cpu-clock-tick sw counter, which
1149 * is always available even if no PMU support:
1150 */
1151 if (attr->type == PERF_TYPE_HARDWARE
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02001152 && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
Ingo Molnar716c69f2009-06-07 17:31:52 +02001153
Ingo Molnar3da297a2009-06-07 17:39:02 +02001154 if (verbose)
1155 warning(" ... trying to fall back to cpu-clock-ticks\n");
1156
Ingo Molnar716c69f2009-06-07 17:31:52 +02001157 attr->type = PERF_TYPE_SOFTWARE;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02001158 attr->config = PERF_COUNT_SW_CPU_CLOCK;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001159 goto try_again;
1160 }
Ingo Molnar30c806a2009-06-07 17:46:24 +02001161 printf("\n");
1162 error("perfcounter syscall returned with %d (%s)\n",
1163 fd[i][counter], strerror(err));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001164 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
Ingo Molnar716c69f2009-06-07 17:31:52 +02001165 exit(-1);
1166 }
1167 assert(fd[i][counter] >= 0);
1168 fcntl(fd[i][counter], F_SETFL, O_NONBLOCK);
1169
1170 /*
1171 * First counter acts as the group leader:
1172 */
1173 if (group && group_fd == -1)
1174 group_fd = fd[i][counter];
1175
1176 event_array[nr_poll].fd = fd[i][counter];
1177 event_array[nr_poll].events = POLLIN;
1178 nr_poll++;
1179
1180 mmap_array[i][counter].counter = counter;
1181 mmap_array[i][counter].prev = 0;
1182 mmap_array[i][counter].mask = mmap_pages*page_size - 1;
1183 mmap_array[i][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
1184 PROT_READ, MAP_SHARED, fd[i][counter], 0);
1185 if (mmap_array[i][counter].base == MAP_FAILED)
1186 die("failed to mmap with %d (%s)\n", errno, strerror(errno));
1187}
1188
1189static int __cmd_top(void)
1190{
1191 pthread_t thread;
1192 int i, counter;
Ingo Molnar07800602009-04-20 15:00:56 +02001193 int ret;
1194
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001195 if (target_pid != -1)
1196 event__synthesize_thread(target_pid, event__process);
1197 else
1198 event__synthesize_threads(event__process);
1199
Ingo Molnar07800602009-04-20 15:00:56 +02001200 for (i = 0; i < nr_cpus; i++) {
1201 group_fd = -1;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001202 for (counter = 0; counter < nr_counters; counter++)
1203 start_counter(i, counter);
Ingo Molnar07800602009-04-20 15:00:56 +02001204 }
1205
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001206 /* Wait for a minimal set of events before starting the snapshot */
1207 poll(event_array, nr_poll, 100);
1208
1209 mmap_read();
1210
Ingo Molnar07800602009-04-20 15:00:56 +02001211 if (pthread_create(&thread, NULL, display_thread, NULL)) {
1212 printf("Could not create display thread.\n");
1213 exit(-1);
1214 }
1215
1216 if (realtime_prio) {
1217 struct sched_param param;
1218
1219 param.sched_priority = realtime_prio;
1220 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
1221 printf("Could not set realtime priority.\n");
1222 exit(-1);
1223 }
1224 }
1225
1226 while (1) {
Ingo Molnar2debbc82009-06-05 14:29:10 +02001227 int hits = samples;
Ingo Molnar07800602009-04-20 15:00:56 +02001228
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001229 mmap_read();
Ingo Molnar07800602009-04-20 15:00:56 +02001230
Ingo Molnar2debbc82009-06-05 14:29:10 +02001231 if (hits == samples)
Ingo Molnar07800602009-04-20 15:00:56 +02001232 ret = poll(event_array, nr_poll, 100);
1233 }
1234
1235 return 0;
1236}
Ingo Molnarb456bae2009-05-26 09:17:18 +02001237
1238static const char * const top_usage[] = {
1239 "perf top [<options>]",
1240 NULL
1241};
1242
Ingo Molnarb456bae2009-05-26 09:17:18 +02001243static const struct option options[] = {
1244 OPT_CALLBACK('e', "event", NULL, "event",
Thomas Gleixner86847b62009-06-06 12:24:17 +02001245 "event selector. use 'perf list' to list available events",
1246 parse_events),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001247 OPT_INTEGER('c', "count", &default_interval,
1248 "event period to sample"),
1249 OPT_INTEGER('p', "pid", &target_pid,
1250 "profile events on existing pid"),
1251 OPT_BOOLEAN('a', "all-cpus", &system_wide,
1252 "system-wide collection from all CPUs"),
1253 OPT_INTEGER('C', "CPU", &profile_cpu,
1254 "CPU to profile on"),
Ingo Molnar83a09442009-08-15 12:26:57 +02001255 OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -02001256 OPT_BOOLEAN('K', "hide_kernel_symbols", &hide_kernel_symbols,
1257 "hide kernel symbols"),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001258 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
1259 "number of mmap data pages"),
1260 OPT_INTEGER('r', "realtime", &realtime_prio,
1261 "collect data with this RT SCHED_FIFO priority"),
Mike Galbraithdb20c002009-05-26 15:25:34 +02001262 OPT_INTEGER('d', "delay", &delay_secs,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001263 "number of seconds to delay between refreshes"),
1264 OPT_BOOLEAN('D', "dump-symtab", &dump_symtab,
1265 "dump the symbol table used for profiling"),
Ingo Molnar6e53cdf2009-06-04 08:53:05 +02001266 OPT_INTEGER('f', "count-filter", &count_filter,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001267 "only display functions with more events than this"),
1268 OPT_BOOLEAN('g', "group", &group,
1269 "put the counters into a counter group"),
Mike Galbraith0fdc7e62009-07-21 10:30:36 +02001270 OPT_BOOLEAN('i', "inherit", &inherit,
1271 "child tasks inherit counters"),
Mike Galbraith923c42c2009-07-22 20:36:03 +02001272 OPT_STRING('s', "sym-annotate", &sym_filter, "symbol name",
1273 "symbol to annotate - requires -k option"),
Anton Blanchard1f208ea2009-07-01 09:00:44 +10001274 OPT_BOOLEAN('z', "zero", &zero,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001275 "zero history across updates"),
Ingo Molnar6e53cdf2009-06-04 08:53:05 +02001276 OPT_INTEGER('F', "freq", &freq,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001277 "profile at this frequency"),
Ingo Molnar6e53cdf2009-06-04 08:53:05 +02001278 OPT_INTEGER('E', "entries", &print_entries,
1279 "display this many functions"),
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -02001280 OPT_BOOLEAN('U', "hide_user_symbols", &hide_user_symbols,
1281 "hide user symbols"),
Ingo Molnar3da297a2009-06-07 17:39:02 +02001282 OPT_BOOLEAN('v', "verbose", &verbose,
1283 "be more verbose (show counter open errors, etc)"),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001284 OPT_END()
1285};
1286
Ingo Molnarf37a2912009-07-01 12:37:06 +02001287int cmd_top(int argc, const char **argv, const char *prefix __used)
Ingo Molnarb456bae2009-05-26 09:17:18 +02001288{
1289 int counter;
1290
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001291 symbol__init(sizeof(struct sym_entry));
Mike Galbraith42976482009-07-02 08:09:46 +02001292
Ingo Molnarb456bae2009-05-26 09:17:18 +02001293 page_size = sysconf(_SC_PAGE_SIZE);
1294
Ingo Molnarb456bae2009-05-26 09:17:18 +02001295 argc = parse_options(argc, argv, options, top_usage, 0);
1296 if (argc)
1297 usage_with_options(top_usage, options);
1298
Ingo Molnarb456bae2009-05-26 09:17:18 +02001299 /* CPU and PID are mutually exclusive */
1300 if (target_pid != -1 && profile_cpu != -1) {
1301 printf("WARNING: PID switch overriding CPU\n");
1302 sleep(1);
1303 profile_cpu = -1;
1304 }
1305
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001306 if (!nr_counters)
Ingo Molnarb456bae2009-05-26 09:17:18 +02001307 nr_counters = 1;
Ingo Molnarb456bae2009-05-26 09:17:18 +02001308
Frederic Weisbecker2f335a02009-06-05 19:31:01 +02001309 if (delay_secs < 1)
1310 delay_secs = 1;
1311
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001312 parse_symbols();
Mike Galbraith923c42c2009-07-22 20:36:03 +02001313 parse_source(sym_filter_entry);
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001314
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +02001315
1316 /*
1317 * User specified count overrides default frequency.
1318 */
1319 if (default_interval)
1320 freq = 0;
1321 else if (freq) {
1322 default_interval = freq;
1323 } else {
1324 fprintf(stderr, "frequency and count are zero, aborting\n");
1325 exit(EXIT_FAILURE);
1326 }
1327
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001328 /*
1329 * Fill in the ones not specifically initialized via -c:
1330 */
Ingo Molnarb456bae2009-05-26 09:17:18 +02001331 for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001332 if (attrs[counter].sample_period)
Ingo Molnarb456bae2009-05-26 09:17:18 +02001333 continue;
1334
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001335 attrs[counter].sample_period = default_interval;
Ingo Molnarb456bae2009-05-26 09:17:18 +02001336 }
1337
1338 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
1339 assert(nr_cpus <= MAX_NR_CPUS);
1340 assert(nr_cpus >= 0);
1341
1342 if (target_pid != -1 || profile_cpu != -1)
1343 nr_cpus = 1;
1344
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -02001345 get_term_dimensions(&winsize);
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -02001346 if (print_entries == 0) {
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -02001347 update_print_entries(&winsize);
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -02001348 signal(SIGWINCH, sig_winch_handler);
1349 }
1350
Ingo Molnarb456bae2009-05-26 09:17:18 +02001351 return __cmd_top();
1352}