blob: 0d60c517c0ba0375749e312fced57e1d3e37a4c6 [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 Weisbecker8f288272009-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 Melo51a472d2009-11-17 18:38:00 -0200131static inline struct symbol *sym_entry__symbol(struct sym_entry *self)
132{
133 return (struct symbol *)(self + 1);
134}
135
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200136static void get_term_dimensions(struct winsize *ws)
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200137{
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200138 char *s = getenv("LINES");
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200139
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200140 if (s != NULL) {
141 ws->ws_row = atoi(s);
142 s = getenv("COLUMNS");
143 if (s != NULL) {
144 ws->ws_col = atoi(s);
145 if (ws->ws_row && ws->ws_col)
146 return;
147 }
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200148 }
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200149#ifdef TIOCGWINSZ
150 if (ioctl(1, TIOCGWINSZ, ws) == 0 &&
151 ws->ws_row && ws->ws_col)
152 return;
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200153#endif
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200154 ws->ws_row = 25;
155 ws->ws_col = 80;
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200156}
157
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200158static void update_print_entries(struct winsize *ws)
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200159{
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200160 print_entries = ws->ws_row;
161
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200162 if (print_entries > 9)
163 print_entries -= 9;
164}
165
166static void sig_winch_handler(int sig __used)
167{
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200168 get_term_dimensions(&winsize);
169 update_print_entries(&winsize);
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200170}
171
Mike Galbraith923c42c2009-07-22 20:36:03 +0200172static void parse_source(struct sym_entry *syme)
173{
174 struct symbol *sym;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300175 struct map *map;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200176 FILE *file;
Ingo Molnar83a09442009-08-15 12:26:57 +0200177 char command[PATH_MAX*2];
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300178 const char *path;
179 u64 len;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200180
181 if (!syme)
182 return;
183
184 if (syme->lines) {
185 pthread_mutex_lock(&syme->source_lock);
186 goto out_assign;
187 }
188
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200189 sym = sym_entry__symbol(syme);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300190 map = syme->map;
191 path = map->dso->long_name;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200192
Mike Galbraith923c42c2009-07-22 20:36:03 +0200193 len = sym->end - sym->start;
194
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300195 sprintf(command,
196 "objdump --start-address=0x%016Lx "
197 "--stop-address=0x%016Lx -dS %s",
Arnaldo Carvalho de Meloc88e4bf2009-10-20 15:54:55 -0200198 map->unmap_ip(map, sym->start),
199 map->unmap_ip(map, sym->end), path);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200200
201 file = popen(command, "r");
202 if (!file)
203 return;
204
205 pthread_mutex_lock(&syme->source_lock);
206 syme->lines_tail = &syme->lines;
207 while (!feof(file)) {
208 struct source_line *src;
209 size_t dummy = 0;
210 char *c;
211
212 src = malloc(sizeof(struct source_line));
213 assert(src != NULL);
214 memset(src, 0, sizeof(struct source_line));
215
216 if (getline(&src->line, &dummy, file) < 0)
217 break;
218 if (!src->line)
219 break;
220
221 c = strchr(src->line, '\n');
222 if (c)
223 *c = 0;
224
225 src->next = NULL;
226 *syme->lines_tail = src;
227 syme->lines_tail = &src->next;
228
229 if (strlen(src->line)>8 && src->line[8] == ':') {
230 src->eip = strtoull(src->line, NULL, 16);
Arnaldo Carvalho de Meloc88e4bf2009-10-20 15:54:55 -0200231 src->eip = map->unmap_ip(map, src->eip);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200232 }
233 if (strlen(src->line)>8 && src->line[16] == ':') {
234 src->eip = strtoull(src->line, NULL, 16);
Arnaldo Carvalho de Meloc88e4bf2009-10-20 15:54:55 -0200235 src->eip = map->unmap_ip(map, src->eip);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200236 }
237 }
238 pclose(file);
239out_assign:
240 sym_filter_entry = syme;
241 pthread_mutex_unlock(&syme->source_lock);
242}
243
244static void __zero_source_counters(struct sym_entry *syme)
245{
246 int i;
247 struct source_line *line;
248
249 line = syme->lines;
250 while (line) {
251 for (i = 0; i < nr_counters; i++)
252 line->count[i] = 0;
253 line = line->next;
254 }
255}
256
257static void record_precise_ip(struct sym_entry *syme, int counter, u64 ip)
258{
259 struct source_line *line;
260
261 if (syme != sym_filter_entry)
262 return;
263
264 if (pthread_mutex_trylock(&syme->source_lock))
265 return;
266
267 if (!syme->source)
268 goto out_unlock;
269
270 for (line = syme->lines; line; line = line->next) {
271 if (line->eip == ip) {
272 line->count[counter]++;
273 break;
274 }
275 if (line->eip > ip)
276 break;
277 }
278out_unlock:
279 pthread_mutex_unlock(&syme->source_lock);
280}
281
282static void lookup_sym_source(struct sym_entry *syme)
283{
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200284 struct symbol *symbol = sym_entry__symbol(syme);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200285 struct source_line *line;
286 char pattern[PATH_MAX];
Mike Galbraith923c42c2009-07-22 20:36:03 +0200287
288 sprintf(pattern, "<%s>:", symbol->name);
289
Mike Galbraith923c42c2009-07-22 20:36:03 +0200290 pthread_mutex_lock(&syme->source_lock);
291 for (line = syme->lines; line; line = line->next) {
292 if (strstr(line->line, pattern)) {
293 syme->source = line;
294 break;
295 }
296 }
297 pthread_mutex_unlock(&syme->source_lock);
298}
299
300static void show_lines(struct source_line *queue, int count, int total)
301{
302 int i;
303 struct source_line *line;
304
305 line = queue;
306 for (i = 0; i < count; i++) {
307 float pcnt = 100.0*(float)line->count[sym_counter]/(float)total;
308
309 printf("%8li %4.1f%%\t%s\n", line->count[sym_counter], pcnt, line->line);
310 line = line->next;
311 }
312}
313
314#define TRACE_COUNT 3
315
316static void show_details(struct sym_entry *syme)
317{
318 struct symbol *symbol;
319 struct source_line *line;
320 struct source_line *line_queue = NULL;
321 int displayed = 0;
322 int line_queue_count = 0, total = 0, more = 0;
323
324 if (!syme)
325 return;
326
327 if (!syme->source)
328 lookup_sym_source(syme);
329
330 if (!syme->source)
331 return;
332
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200333 symbol = sym_entry__symbol(syme);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200334 printf("Showing %s for %s\n", event_name(sym_counter), symbol->name);
335 printf(" Events Pcnt (>=%d%%)\n", sym_pcnt_filter);
336
337 pthread_mutex_lock(&syme->source_lock);
338 line = syme->source;
339 while (line) {
340 total += line->count[sym_counter];
341 line = line->next;
342 }
343
344 line = syme->source;
345 while (line) {
346 float pcnt = 0.0;
347
348 if (!line_queue_count)
349 line_queue = line;
350 line_queue_count++;
351
352 if (line->count[sym_counter])
353 pcnt = 100.0 * line->count[sym_counter] / (float)total;
354 if (pcnt >= (float)sym_pcnt_filter) {
355 if (displayed <= print_entries)
356 show_lines(line_queue, line_queue_count, total);
357 else more++;
358 displayed += line_queue_count;
359 line_queue_count = 0;
360 line_queue = NULL;
361 } else if (line_queue_count > TRACE_COUNT) {
362 line_queue = line_queue->next;
363 line_queue_count--;
364 }
365
366 line->count[sym_counter] = zero ? 0 : line->count[sym_counter] * 7 / 8;
367 line = line->next;
368 }
369 pthread_mutex_unlock(&syme->source_lock);
370 if (more)
371 printf("%d lines not displayed, maybe increase display entries [e]\n", more);
372}
Ingo Molnar07800602009-04-20 15:00:56 +0200373
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300374/*
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200375 * Symbols will be added here in event__process_sample and will get out
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300376 * after decayed.
377 */
378static LIST_HEAD(active_symbols);
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300379static pthread_mutex_t active_symbols_lock = PTHREAD_MUTEX_INITIALIZER;
Ingo Molnar07800602009-04-20 15:00:56 +0200380
Ingo Molnar07800602009-04-20 15:00:56 +0200381/*
382 * Ordering weight: count-1 * count-2 * ... / count-n
383 */
384static double sym_weight(const struct sym_entry *sym)
385{
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300386 double weight = sym->snap_count;
Ingo Molnar07800602009-04-20 15:00:56 +0200387 int counter;
388
Mike Galbraith46ab9762009-07-24 10:09:50 +0200389 if (!display_weighted)
390 return weight;
391
Ingo Molnar07800602009-04-20 15:00:56 +0200392 for (counter = 1; counter < nr_counters-1; counter++)
393 weight *= sym->count[counter];
394
395 weight /= (sym->count[counter] + 1);
396
397 return weight;
398}
399
Ingo Molnar2debbc82009-06-05 14:29:10 +0200400static long samples;
401static long userspace_samples;
Ingo Molnar07800602009-04-20 15:00:56 +0200402static const char CONSOLE_CLEAR[] = "";
403
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300404static void __list_insert_active_sym(struct sym_entry *syme)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300405{
406 list_add(&syme->node, &active_symbols);
407}
408
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300409static void list_remove_active_sym(struct sym_entry *syme)
410{
411 pthread_mutex_lock(&active_symbols_lock);
412 list_del_init(&syme->node);
413 pthread_mutex_unlock(&active_symbols_lock);
414}
415
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300416static void rb_insert_active_sym(struct rb_root *tree, struct sym_entry *se)
417{
418 struct rb_node **p = &tree->rb_node;
419 struct rb_node *parent = NULL;
420 struct sym_entry *iter;
421
422 while (*p != NULL) {
423 parent = *p;
424 iter = rb_entry(parent, struct sym_entry, rb_node);
425
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300426 if (se->weight > iter->weight)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300427 p = &(*p)->rb_left;
428 else
429 p = &(*p)->rb_right;
430 }
431
432 rb_link_node(&se->rb_node, parent, p);
433 rb_insert_color(&se->rb_node, tree);
434}
Ingo Molnar07800602009-04-20 15:00:56 +0200435
436static void print_sym_table(void)
437{
Ingo Molnar233f0b92009-06-03 21:48:40 +0200438 int printed = 0, j;
Mike Galbraith46ab9762009-07-24 10:09:50 +0200439 int counter, snap = !display_weighted ? sym_counter : 0;
Ingo Molnar2debbc82009-06-05 14:29:10 +0200440 float samples_per_sec = samples/delay_secs;
441 float ksamples_per_sec = (samples-userspace_samples)/delay_secs;
442 float sum_ksamples = 0.0;
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300443 struct sym_entry *syme, *n;
444 struct rb_root tmp = RB_ROOT;
445 struct rb_node *nd;
Arnaldo Carvalho de Melo1a105f72009-11-17 15:40:55 -0200446 int sym_width = 0, dso_width = 0;
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200447 const int win_width = winsize.ws_col - 1;
Arnaldo Carvalho de Melo1a105f72009-11-17 15:40:55 -0200448 struct dso *unique_dso = NULL, *first_dso = NULL;
Ingo Molnar07800602009-04-20 15:00:56 +0200449
Ingo Molnar2debbc82009-06-05 14:29:10 +0200450 samples = userspace_samples = 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200451
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300452 /* Sort the active symbols */
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300453 pthread_mutex_lock(&active_symbols_lock);
454 syme = list_entry(active_symbols.next, struct sym_entry, node);
455 pthread_mutex_unlock(&active_symbols_lock);
456
457 list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
Mike Galbraith46ab9762009-07-24 10:09:50 +0200458 syme->snap_count = syme->count[snap];
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300459 if (syme->snap_count != 0) {
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200460
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200461 if ((hide_user_symbols &&
462 syme->origin == PERF_RECORD_MISC_USER) ||
463 (hide_kernel_symbols &&
464 syme->origin == PERF_RECORD_MISC_KERNEL)) {
465 list_remove_active_sym(syme);
466 continue;
467 }
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300468 syme->weight = sym_weight(syme);
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300469 rb_insert_active_sym(&tmp, syme);
Ingo Molnar2debbc82009-06-05 14:29:10 +0200470 sum_ksamples += syme->snap_count;
Mike Galbraithd94b9432009-05-25 09:57:56 +0200471
472 for (j = 0; j < nr_counters; j++)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300473 syme->count[j] = zero ? 0 : syme->count[j] * 7 / 8;
474 } else
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300475 list_remove_active_sym(syme);
Mike Galbraithd94b9432009-05-25 09:57:56 +0200476 }
477
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200478 puts(CONSOLE_CLEAR);
Ingo Molnar07800602009-04-20 15:00:56 +0200479
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200480 printf("%-*.*s\n", win_width, win_width, graph_dotted_line);
Ingo Molnarf2521b62009-06-03 19:17:25 +0200481 printf( " PerfTop:%8.0f irqs/sec kernel:%4.1f%% [",
Ingo Molnar2debbc82009-06-05 14:29:10 +0200482 samples_per_sec,
483 100.0 - (100.0*((samples_per_sec-ksamples_per_sec)/samples_per_sec)));
Ingo Molnar07800602009-04-20 15:00:56 +0200484
Mike Galbraith46ab9762009-07-24 10:09:50 +0200485 if (nr_counters == 1 || !display_weighted) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000486 printf("%Ld", (u64)attrs[0].sample_period);
Ingo Molnarcf1f4572009-06-05 13:27:02 +0200487 if (freq)
488 printf("Hz ");
489 else
490 printf(" ");
491 }
Ingo Molnar07800602009-04-20 15:00:56 +0200492
Mike Galbraith46ab9762009-07-24 10:09:50 +0200493 if (!display_weighted)
494 printf("%s", event_name(sym_counter));
495 else for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnar07800602009-04-20 15:00:56 +0200496 if (counter)
497 printf("/");
498
499 printf("%s", event_name(counter));
500 }
501
502 printf( "], ");
503
Ingo Molnarb456bae2009-05-26 09:17:18 +0200504 if (target_pid != -1)
505 printf(" (target_pid: %d", target_pid);
Ingo Molnar07800602009-04-20 15:00:56 +0200506 else
507 printf(" (all");
508
509 if (profile_cpu != -1)
510 printf(", cpu: %d)\n", profile_cpu);
511 else {
Ingo Molnarb456bae2009-05-26 09:17:18 +0200512 if (target_pid != -1)
Ingo Molnar07800602009-04-20 15:00:56 +0200513 printf(")\n");
514 else
515 printf(", %d CPUs)\n", nr_cpus);
516 }
517
Arnaldo Carvalho de Melo1a105f72009-11-17 15:40:55 -0200518 printf("%-*.*s\n", win_width, win_width, graph_dotted_line);
Ingo Molnar07800602009-04-20 15:00:56 +0200519
Mike Galbraith923c42c2009-07-22 20:36:03 +0200520 if (sym_filter_entry) {
521 show_details(sym_filter_entry);
522 return;
523 }
524
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200525 /*
526 * Find the longest symbol name that will be displayed
527 */
528 for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
529 syme = rb_entry(nd, struct sym_entry, rb_node);
530 if (++printed > print_entries ||
531 (int)syme->snap_count < count_filter)
532 continue;
533
Arnaldo Carvalho de Melo1a105f72009-11-17 15:40:55 -0200534 if (first_dso == NULL)
535 unique_dso = first_dso = syme->map->dso;
536 else if (syme->map->dso != first_dso)
537 unique_dso = NULL;
538
539 if (syme->map->dso->long_name_len > dso_width)
540 dso_width = syme->map->dso->long_name_len;
541
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200542 if (syme->name_len > sym_width)
543 sym_width = syme->name_len;
544 }
545
546 printed = 0;
547
Arnaldo Carvalho de Melo1a105f72009-11-17 15:40:55 -0200548 if (unique_dso)
549 printf("DSO: %s\n", unique_dso->long_name);
550 else {
551 int max_dso_width = winsize.ws_col - sym_width - 29;
552 if (dso_width > max_dso_width)
553 dso_width = max_dso_width;
554 putchar('\n');
555 }
Ingo Molnar07800602009-04-20 15:00:56 +0200556 if (nr_counters == 1)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200557 printf(" samples pcnt");
Ingo Molnar07800602009-04-20 15:00:56 +0200558 else
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200559 printf(" weight samples pcnt");
Ingo Molnar07800602009-04-20 15:00:56 +0200560
Arnaldo Carvalho de Melo7ced1562009-08-26 11:51:26 -0300561 if (verbose)
562 printf(" RIP ");
Arnaldo Carvalho de Melo1a105f72009-11-17 15:40:55 -0200563 printf(" %-*.*s", sym_width, sym_width, "function");
564 if (!unique_dso)
565 printf(" DSO");
566 putchar('\n');
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200567 printf(" %s _______ _____",
Arnaldo Carvalho de Melo7ced1562009-08-26 11:51:26 -0300568 nr_counters == 1 ? " " : "______");
569 if (verbose)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200570 printf(" ________________");
Arnaldo Carvalho de Melo1a105f72009-11-17 15:40:55 -0200571 printf(" %-*.*s", sym_width, sym_width, graph_line);
572 if (!unique_dso)
573 printf(" %-*.*s", dso_width, dso_width, graph_line);
574 puts("\n");
Ingo Molnar07800602009-04-20 15:00:56 +0200575
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300576 for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
Ingo Molnar83a09442009-08-15 12:26:57 +0200577 struct symbol *sym;
Ingo Molnar8fc03212009-06-04 15:19:47 +0200578 double pcnt;
Ingo Molnar07800602009-04-20 15:00:56 +0200579
Ingo Molnar83a09442009-08-15 12:26:57 +0200580 syme = rb_entry(nd, struct sym_entry, rb_node);
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200581 sym = sym_entry__symbol(syme);
Ingo Molnar83a09442009-08-15 12:26:57 +0200582
Mike Galbraith923c42c2009-07-22 20:36:03 +0200583 if (++printed > print_entries || (int)syme->snap_count < count_filter)
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300584 continue;
Ingo Molnar07800602009-04-20 15:00:56 +0200585
Ingo Molnar2debbc82009-06-05 14:29:10 +0200586 pcnt = 100.0 - (100.0 * ((sum_ksamples - syme->snap_count) /
587 sum_ksamples));
Mike Galbraithd94b9432009-05-25 09:57:56 +0200588
Mike Galbraith46ab9762009-07-24 10:09:50 +0200589 if (nr_counters == 1 || !display_weighted)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200590 printf("%20.2f ", syme->weight);
Mike Galbraithd94b9432009-05-25 09:57:56 +0200591 else
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200592 printf("%9.1f %10ld ", syme->weight, syme->snap_count);
Ingo Molnar8fc03212009-06-04 15:19:47 +0200593
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200594 percent_color_fprintf(stdout, "%4.1f%%", pcnt);
Arnaldo Carvalho de Melo7ced1562009-08-26 11:51:26 -0300595 if (verbose)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200596 printf(" %016llx", sym->start);
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200597 printf(" %-*.*s", sym_width, sym_width, sym->name);
Arnaldo Carvalho de Melo1a105f72009-11-17 15:40:55 -0200598 if (!unique_dso)
599 printf(" %-*.*s", dso_width, dso_width,
600 dso_width >= syme->map->dso->long_name_len ?
601 syme->map->dso->long_name :
602 syme->map->dso->short_name);
Mike Galbraith42976482009-07-02 08:09:46 +0200603 printf("\n");
Ingo Molnar07800602009-04-20 15:00:56 +0200604 }
Ingo Molnar07800602009-04-20 15:00:56 +0200605}
606
Mike Galbraith923c42c2009-07-22 20:36:03 +0200607static void prompt_integer(int *target, const char *msg)
608{
609 char *buf = malloc(0), *p;
610 size_t dummy = 0;
611 int tmp;
612
613 fprintf(stdout, "\n%s: ", msg);
614 if (getline(&buf, &dummy, stdin) < 0)
615 return;
616
617 p = strchr(buf, '\n');
618 if (p)
619 *p = 0;
620
621 p = buf;
622 while(*p) {
623 if (!isdigit(*p))
624 goto out_free;
625 p++;
626 }
627 tmp = strtoul(buf, NULL, 10);
628 *target = tmp;
629out_free:
630 free(buf);
631}
632
633static void prompt_percent(int *target, const char *msg)
634{
635 int tmp = 0;
636
637 prompt_integer(&tmp, msg);
638 if (tmp >= 0 && tmp <= 100)
639 *target = tmp;
640}
641
642static void prompt_symbol(struct sym_entry **target, const char *msg)
643{
644 char *buf = malloc(0), *p;
645 struct sym_entry *syme = *target, *n, *found = NULL;
646 size_t dummy = 0;
647
648 /* zero counters of active symbol */
649 if (syme) {
650 pthread_mutex_lock(&syme->source_lock);
651 __zero_source_counters(syme);
652 *target = NULL;
653 pthread_mutex_unlock(&syme->source_lock);
654 }
655
656 fprintf(stdout, "\n%s: ", msg);
657 if (getline(&buf, &dummy, stdin) < 0)
658 goto out_free;
659
660 p = strchr(buf, '\n');
661 if (p)
662 *p = 0;
663
664 pthread_mutex_lock(&active_symbols_lock);
665 syme = list_entry(active_symbols.next, struct sym_entry, node);
666 pthread_mutex_unlock(&active_symbols_lock);
667
668 list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200669 struct symbol *sym = sym_entry__symbol(syme);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200670
671 if (!strcmp(buf, sym->name)) {
672 found = syme;
673 break;
674 }
675 }
676
677 if (!found) {
678 fprintf(stderr, "Sorry, %s is not active.\n", sym_filter);
679 sleep(1);
680 return;
681 } else
682 parse_source(found);
683
684out_free:
685 free(buf);
686}
687
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200688static void print_mapped_keys(void)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200689{
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200690 char *name = NULL;
691
692 if (sym_filter_entry) {
Arnaldo Carvalho de Melo51a472d2009-11-17 18:38:00 -0200693 struct symbol *sym = sym_entry__symbol(sym_filter_entry);
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200694 name = sym->name;
695 }
696
697 fprintf(stdout, "\nMapped keys:\n");
698 fprintf(stdout, "\t[d] display refresh delay. \t(%d)\n", delay_secs);
699 fprintf(stdout, "\t[e] display entries (lines). \t(%d)\n", print_entries);
700
701 if (nr_counters > 1)
702 fprintf(stdout, "\t[E] active event counter. \t(%s)\n", event_name(sym_counter));
703
704 fprintf(stdout, "\t[f] profile display filter (count). \t(%d)\n", count_filter);
705
Ingo Molnar83a09442009-08-15 12:26:57 +0200706 if (vmlinux_name) {
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200707 fprintf(stdout, "\t[F] annotate display filter (percent). \t(%d%%)\n", sym_pcnt_filter);
708 fprintf(stdout, "\t[s] annotate symbol. \t(%s)\n", name?: "NULL");
709 fprintf(stdout, "\t[S] stop annotation.\n");
710 }
711
712 if (nr_counters > 1)
713 fprintf(stdout, "\t[w] toggle display weighted/count[E]r. \t(%d)\n", display_weighted ? 1 : 0);
714
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200715 fprintf(stdout,
716 "\t[K] hide kernel_symbols symbols. \t(%s)\n",
717 hide_kernel_symbols ? "yes" : "no");
718 fprintf(stdout,
719 "\t[U] hide user symbols. \t(%s)\n",
720 hide_user_symbols ? "yes" : "no");
Mike Galbraith46ab9762009-07-24 10:09:50 +0200721 fprintf(stdout, "\t[z] toggle sample zeroing. \t(%d)\n", zero ? 1 : 0);
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200722 fprintf(stdout, "\t[qQ] quit.\n");
723}
724
725static int key_mapped(int c)
726{
727 switch (c) {
728 case 'd':
729 case 'e':
730 case 'f':
731 case 'z':
732 case 'q':
733 case 'Q':
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200734 case 'K':
735 case 'U':
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200736 return 1;
737 case 'E':
738 case 'w':
739 return nr_counters > 1 ? 1 : 0;
740 case 'F':
741 case 's':
742 case 'S':
Ingo Molnar83a09442009-08-15 12:26:57 +0200743 return vmlinux_name ? 1 : 0;
744 default:
745 break;
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200746 }
747
748 return 0;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200749}
750
751static void handle_keypress(int c)
752{
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200753 if (!key_mapped(c)) {
754 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
755 struct termios tc, save;
756
757 print_mapped_keys();
758 fprintf(stdout, "\nEnter selection, or unmapped key to continue: ");
759 fflush(stdout);
760
761 tcgetattr(0, &save);
762 tc = save;
763 tc.c_lflag &= ~(ICANON | ECHO);
764 tc.c_cc[VMIN] = 0;
765 tc.c_cc[VTIME] = 0;
766 tcsetattr(0, TCSANOW, &tc);
767
768 poll(&stdin_poll, 1, -1);
769 c = getc(stdin);
770
771 tcsetattr(0, TCSAFLUSH, &save);
772 if (!key_mapped(c))
773 return;
774 }
775
Mike Galbraith923c42c2009-07-22 20:36:03 +0200776 switch (c) {
777 case 'd':
778 prompt_integer(&delay_secs, "Enter display delay");
Tim Blechmanndc799592009-10-17 18:08:29 +0200779 if (delay_secs < 1)
780 delay_secs = 1;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200781 break;
782 case 'e':
783 prompt_integer(&print_entries, "Enter display entries (lines)");
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200784 if (print_entries == 0) {
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200785 sig_winch_handler(SIGWINCH);
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200786 signal(SIGWINCH, sig_winch_handler);
787 } else
788 signal(SIGWINCH, SIG_DFL);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200789 break;
790 case 'E':
791 if (nr_counters > 1) {
792 int i;
793
794 fprintf(stderr, "\nAvailable events:");
795 for (i = 0; i < nr_counters; i++)
796 fprintf(stderr, "\n\t%d %s", i, event_name(i));
797
798 prompt_integer(&sym_counter, "Enter details event counter");
799
800 if (sym_counter >= nr_counters) {
801 fprintf(stderr, "Sorry, no such event, using %s.\n", event_name(0));
802 sym_counter = 0;
803 sleep(1);
804 }
805 } else sym_counter = 0;
806 break;
807 case 'f':
808 prompt_integer(&count_filter, "Enter display event count filter");
809 break;
810 case 'F':
811 prompt_percent(&sym_pcnt_filter, "Enter details display event filter (percent)");
812 break;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200813 case 'K':
814 hide_kernel_symbols = !hide_kernel_symbols;
815 break;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200816 case 'q':
817 case 'Q':
818 printf("exiting.\n");
819 exit(0);
820 case 's':
821 prompt_symbol(&sym_filter_entry, "Enter details symbol");
822 break;
823 case 'S':
824 if (!sym_filter_entry)
825 break;
826 else {
827 struct sym_entry *syme = sym_filter_entry;
828
829 pthread_mutex_lock(&syme->source_lock);
830 sym_filter_entry = NULL;
831 __zero_source_counters(syme);
832 pthread_mutex_unlock(&syme->source_lock);
833 }
834 break;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200835 case 'U':
836 hide_user_symbols = !hide_user_symbols;
837 break;
Mike Galbraith46ab9762009-07-24 10:09:50 +0200838 case 'w':
839 display_weighted = ~display_weighted;
840 break;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200841 case 'z':
842 zero = ~zero;
843 break;
Ingo Molnar83a09442009-08-15 12:26:57 +0200844 default:
845 break;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200846 }
847}
848
Ingo Molnarf37a2912009-07-01 12:37:06 +0200849static void *display_thread(void *arg __used)
Ingo Molnar07800602009-04-20 15:00:56 +0200850{
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200851 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
Mike Galbraith923c42c2009-07-22 20:36:03 +0200852 struct termios tc, save;
853 int delay_msecs, c;
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200854
Mike Galbraith923c42c2009-07-22 20:36:03 +0200855 tcgetattr(0, &save);
856 tc = save;
857 tc.c_lflag &= ~(ICANON | ECHO);
858 tc.c_cc[VMIN] = 0;
859 tc.c_cc[VTIME] = 0;
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200860
Mike Galbraith923c42c2009-07-22 20:36:03 +0200861repeat:
862 delay_msecs = delay_secs * 1000;
863 tcsetattr(0, TCSANOW, &tc);
864 /* trash return*/
865 getc(stdin);
Ingo Molnar07800602009-04-20 15:00:56 +0200866
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200867 do {
Ingo Molnar07800602009-04-20 15:00:56 +0200868 print_sym_table();
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200869 } while (!poll(&stdin_poll, 1, delay_msecs) == 1);
870
Mike Galbraith923c42c2009-07-22 20:36:03 +0200871 c = getc(stdin);
872 tcsetattr(0, TCSAFLUSH, &save);
873
874 handle_keypress(c);
875 goto repeat;
Ingo Molnar07800602009-04-20 15:00:56 +0200876
877 return NULL;
878}
879
Anton Blanchard2ab52082009-07-01 09:00:46 +1000880/* Tag samples to be skipped. */
Ingo Molnarf37a2912009-07-01 12:37:06 +0200881static const char *skip_symbols[] = {
Anton Blanchard2ab52082009-07-01 09:00:46 +1000882 "default_idle",
883 "cpu_idle",
884 "enter_idle",
885 "exit_idle",
886 "mwait_idle",
Arnaldo Carvalho de Melo59b90052009-07-26 19:06:19 -0300887 "mwait_idle_with_hints",
Arnaldo Carvalho de Melo83572752009-09-25 15:02:39 -0700888 "poll_idle",
Anton Blanchard3a3393e2009-07-01 09:00:47 +1000889 "ppc64_runlatch_off",
890 "pseries_dedicated_idle_sleep",
Anton Blanchard2ab52082009-07-01 09:00:46 +1000891 NULL
892};
893
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300894static int symbol_filter(struct map *map, struct symbol *sym)
Ingo Molnar07800602009-04-20 15:00:56 +0200895{
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300896 struct sym_entry *syme;
897 const char *name = sym->name;
Anton Blanchard2ab52082009-07-01 09:00:46 +1000898 int i;
Ingo Molnar07800602009-04-20 15:00:56 +0200899
Anton Blanchard3a3393e2009-07-01 09:00:47 +1000900 /*
901 * ppc64 uses function descriptors and appends a '.' to the
902 * start of every instruction address. Remove it.
903 */
904 if (name[0] == '.')
905 name++;
906
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300907 if (!strcmp(name, "_text") ||
908 !strcmp(name, "_etext") ||
909 !strcmp(name, "_sinittext") ||
910 !strncmp("init_module", name, 11) ||
911 !strncmp("cleanup_module", name, 14) ||
912 strstr(name, "_text_start") ||
913 strstr(name, "_text_end"))
Ingo Molnar07800602009-04-20 15:00:56 +0200914 return 1;
915
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200916 syme = symbol__priv(sym);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300917 syme->map = map;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200918 pthread_mutex_init(&syme->source_lock, NULL);
919 if (!sym_filter_entry && sym_filter && !strcmp(name, sym_filter))
920 sym_filter_entry = syme;
921
Anton Blanchard2ab52082009-07-01 09:00:46 +1000922 for (i = 0; skip_symbols[i]; i++) {
923 if (!strcmp(skip_symbols[i], name)) {
924 syme->skip = 1;
925 break;
926 }
927 }
Ingo Molnar07800602009-04-20 15:00:56 +0200928
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -0200929 if (!syme->skip)
930 syme->name_len = strlen(sym->name);
931
Ingo Molnar07800602009-04-20 15:00:56 +0200932 return 0;
933}
934
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300935static int parse_symbols(void)
Ingo Molnar07800602009-04-20 15:00:56 +0200936{
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200937 if (dsos__load_kernel(vmlinux_name, symbol_filter, 1) <= 0)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300938 return -1;
Ingo Molnar07800602009-04-20 15:00:56 +0200939
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300940 if (dump_symtab)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300941 dsos__fprintf(stderr);
Ingo Molnar07800602009-04-20 15:00:56 +0200942
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300943 return 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200944}
945
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200946static void event__process_sample(const event_t *self, int counter)
Ingo Molnar07800602009-04-20 15:00:56 +0200947{
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200948 u64 ip = self->ip.ip;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300949 struct map *map;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200950 struct sym_entry *syme;
951 struct symbol *sym;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200952 u8 origin = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Ingo Molnar07800602009-04-20 15:00:56 +0200953
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200954 switch (origin) {
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200955 case PERF_RECORD_MISC_USER: {
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200956 struct thread *thread;
Ingo Molnar07800602009-04-20 15:00:56 +0200957
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200958 if (hide_user_symbols)
959 return;
960
961 thread = threads__findnew(self->ip.pid);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200962 if (thread == NULL)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300963 return;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200964
965 map = thread__find_map(thread, ip);
966 if (map != NULL) {
967 ip = map->map_ip(map, ip);
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200968 sym = map__find_symbol(map, ip, symbol_filter);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200969 if (sym == NULL)
970 return;
971 userspace_samples++;
972 break;
Ingo Molnar07800602009-04-20 15:00:56 +0200973 }
Ingo Molnar07800602009-04-20 15:00:56 +0200974 }
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200975 /*
976 * If this is outside of all known maps,
977 * and is a negative address, try to look it
978 * up in the kernel dso, as it might be a
979 * vsyscall or vdso (which executes in user-mode).
980 */
981 if ((long long)ip >= 0)
982 return;
983 /* Fall thru */
984 case PERF_RECORD_MISC_KERNEL:
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200985 if (hide_kernel_symbols)
986 return;
987
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200988 sym = kernel_maps__find_symbol(ip, &map);
989 if (sym == NULL)
990 return;
991 break;
992 default:
Ingo Molnar07800602009-04-20 15:00:56 +0200993 return;
994 }
995
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200996 syme = symbol__priv(sym);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200997
998 if (!syme->skip) {
999 syme->count[counter]++;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -02001000 syme->origin = origin;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001001 record_precise_ip(syme, counter, ip);
1002 pthread_mutex_lock(&active_symbols_lock);
1003 if (list_empty(&syme->node) || !syme->node.next)
1004 __list_insert_active_sym(syme);
1005 pthread_mutex_unlock(&active_symbols_lock);
1006 ++samples;
1007 return;
1008 }
1009}
1010
1011static void event__process_mmap(event_t *self)
1012{
1013 struct thread *thread = threads__findnew(self->mmap.pid);
1014
1015 if (thread != NULL) {
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001016 struct map *map = map__new(&self->mmap, NULL, 0);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001017 if (map != NULL)
1018 thread__insert_map(thread, map);
1019 }
1020}
1021
1022static void event__process_comm(event_t *self)
1023{
1024 struct thread *thread = threads__findnew(self->comm.pid);
1025
1026 if (thread != NULL)
1027 thread__set_comm(thread, self->comm.comm);
1028}
1029
1030static int event__process(event_t *event)
1031{
1032 switch (event->header.type) {
1033 case PERF_RECORD_COMM:
1034 event__process_comm(event);
1035 break;
1036 case PERF_RECORD_MMAP:
1037 event__process_mmap(event);
1038 break;
1039 default:
1040 break;
1041 }
1042
1043 return 0;
Ingo Molnar07800602009-04-20 15:00:56 +02001044}
1045
Ingo Molnar07800602009-04-20 15:00:56 +02001046struct mmap_data {
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001047 int counter;
1048 void *base;
Ingo Molnarf37a2912009-07-01 12:37:06 +02001049 int mask;
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001050 unsigned int prev;
Ingo Molnar07800602009-04-20 15:00:56 +02001051};
1052
1053static unsigned int mmap_read_head(struct mmap_data *md)
1054{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001055 struct perf_event_mmap_page *pc = md->base;
Ingo Molnar07800602009-04-20 15:00:56 +02001056 int head;
1057
1058 head = pc->data_head;
1059 rmb();
1060
1061 return head;
1062}
1063
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001064static void mmap_read_counter(struct mmap_data *md)
Ingo Molnar07800602009-04-20 15:00:56 +02001065{
1066 unsigned int head = mmap_read_head(md);
1067 unsigned int old = md->prev;
1068 unsigned char *data = md->base + page_size;
1069 int diff;
1070
Ingo Molnar07800602009-04-20 15:00:56 +02001071 /*
1072 * If we're further behind than half the buffer, there's a chance
Ingo Molnar2debbc82009-06-05 14:29:10 +02001073 * the writer will bite our tail and mess up the samples under us.
Ingo Molnar07800602009-04-20 15:00:56 +02001074 *
1075 * If we somehow ended up ahead of the head, we got messed up.
1076 *
1077 * In either case, truncate and restart at head.
1078 */
1079 diff = head - old;
1080 if (diff > md->mask / 2 || diff < 0) {
Mike Galbraithf4f0b412009-10-13 14:57:20 +02001081 fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
Ingo Molnar07800602009-04-20 15:00:56 +02001082
1083 /*
1084 * head points to a known good entry, start there.
1085 */
1086 old = head;
1087 }
1088
Ingo Molnar07800602009-04-20 15:00:56 +02001089 for (; old != head;) {
Ingo Molnar07800602009-04-20 15:00:56 +02001090 event_t *event = (event_t *)&data[old & md->mask];
1091
1092 event_t event_copy;
1093
Ingo Molnar6f06ccb2009-04-20 15:22:22 +02001094 size_t size = event->header.size;
Ingo Molnar07800602009-04-20 15:00:56 +02001095
1096 /*
1097 * Event straddles the mmap boundary -- header should always
1098 * be inside due to u64 alignment of output.
1099 */
1100 if ((old & md->mask) + size != ((old + size) & md->mask)) {
1101 unsigned int offset = old;
1102 unsigned int len = min(sizeof(*event), size), cpy;
1103 void *dst = &event_copy;
1104
1105 do {
1106 cpy = min(md->mask + 1 - (offset & md->mask), len);
1107 memcpy(dst, &data[offset & md->mask], cpy);
1108 offset += cpy;
1109 dst += cpy;
1110 len -= cpy;
1111 } while (len);
1112
1113 event = &event_copy;
1114 }
1115
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001116 if (event->header.type == PERF_RECORD_SAMPLE)
1117 event__process_sample(event, md->counter);
1118 else
1119 event__process(event);
Ingo Molnar07800602009-04-20 15:00:56 +02001120 old += size;
Ingo Molnar07800602009-04-20 15:00:56 +02001121 }
1122
1123 md->prev = old;
1124}
1125
Mike Galbraithc2990a22009-05-24 08:35:49 +02001126static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
1127static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
1128
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001129static void mmap_read(void)
1130{
1131 int i, counter;
1132
1133 for (i = 0; i < nr_cpus; i++) {
1134 for (counter = 0; counter < nr_counters; counter++)
1135 mmap_read_counter(&mmap_array[i][counter]);
1136 }
1137}
1138
Ingo Molnar716c69f2009-06-07 17:31:52 +02001139int nr_poll;
1140int group_fd;
1141
1142static void start_counter(int i, int counter)
Ingo Molnar07800602009-04-20 15:00:56 +02001143{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001144 struct perf_event_attr *attr;
Mike Galbraith0fdc7e62009-07-21 10:30:36 +02001145 int cpu;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001146
1147 cpu = profile_cpu;
1148 if (target_pid == -1 && profile_cpu == -1)
1149 cpu = i;
1150
1151 attr = attrs + counter;
1152
1153 attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +02001154
1155 if (freq) {
1156 attr->sample_type |= PERF_SAMPLE_PERIOD;
1157 attr->freq = 1;
1158 attr->sample_freq = freq;
1159 }
1160
Mike Galbraith0fdc7e62009-07-21 10:30:36 +02001161 attr->inherit = (cpu < 0) && inherit;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001162 attr->mmap = 1;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001163
1164try_again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001165 fd[i][counter] = sys_perf_event_open(attr, target_pid, cpu, group_fd, 0);
Ingo Molnar716c69f2009-06-07 17:31:52 +02001166
1167 if (fd[i][counter] < 0) {
1168 int err = errno;
1169
Pekka Enbergc10edee2009-11-08 18:01:06 +02001170 if (err == EPERM || err == EACCES)
Ingo Molnar3da297a2009-06-07 17:39:02 +02001171 die("No permission - are you root?\n");
Ingo Molnar716c69f2009-06-07 17:31:52 +02001172 /*
1173 * If it's cycles then fall back to hrtimer
1174 * based cpu-clock-tick sw counter, which
1175 * is always available even if no PMU support:
1176 */
1177 if (attr->type == PERF_TYPE_HARDWARE
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02001178 && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
Ingo Molnar716c69f2009-06-07 17:31:52 +02001179
Ingo Molnar3da297a2009-06-07 17:39:02 +02001180 if (verbose)
1181 warning(" ... trying to fall back to cpu-clock-ticks\n");
1182
Ingo Molnar716c69f2009-06-07 17:31:52 +02001183 attr->type = PERF_TYPE_SOFTWARE;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02001184 attr->config = PERF_COUNT_SW_CPU_CLOCK;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001185 goto try_again;
1186 }
Ingo Molnar30c806a2009-06-07 17:46:24 +02001187 printf("\n");
1188 error("perfcounter syscall returned with %d (%s)\n",
1189 fd[i][counter], strerror(err));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001190 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
Ingo Molnar716c69f2009-06-07 17:31:52 +02001191 exit(-1);
1192 }
1193 assert(fd[i][counter] >= 0);
1194 fcntl(fd[i][counter], F_SETFL, O_NONBLOCK);
1195
1196 /*
1197 * First counter acts as the group leader:
1198 */
1199 if (group && group_fd == -1)
1200 group_fd = fd[i][counter];
1201
1202 event_array[nr_poll].fd = fd[i][counter];
1203 event_array[nr_poll].events = POLLIN;
1204 nr_poll++;
1205
1206 mmap_array[i][counter].counter = counter;
1207 mmap_array[i][counter].prev = 0;
1208 mmap_array[i][counter].mask = mmap_pages*page_size - 1;
1209 mmap_array[i][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
1210 PROT_READ, MAP_SHARED, fd[i][counter], 0);
1211 if (mmap_array[i][counter].base == MAP_FAILED)
1212 die("failed to mmap with %d (%s)\n", errno, strerror(errno));
1213}
1214
1215static int __cmd_top(void)
1216{
1217 pthread_t thread;
1218 int i, counter;
Ingo Molnar07800602009-04-20 15:00:56 +02001219 int ret;
1220
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001221 if (target_pid != -1)
1222 event__synthesize_thread(target_pid, event__process);
1223 else
1224 event__synthesize_threads(event__process);
1225
Ingo Molnar07800602009-04-20 15:00:56 +02001226 for (i = 0; i < nr_cpus; i++) {
1227 group_fd = -1;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001228 for (counter = 0; counter < nr_counters; counter++)
1229 start_counter(i, counter);
Ingo Molnar07800602009-04-20 15:00:56 +02001230 }
1231
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001232 /* Wait for a minimal set of events before starting the snapshot */
1233 poll(event_array, nr_poll, 100);
1234
1235 mmap_read();
1236
Ingo Molnar07800602009-04-20 15:00:56 +02001237 if (pthread_create(&thread, NULL, display_thread, NULL)) {
1238 printf("Could not create display thread.\n");
1239 exit(-1);
1240 }
1241
1242 if (realtime_prio) {
1243 struct sched_param param;
1244
1245 param.sched_priority = realtime_prio;
1246 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
1247 printf("Could not set realtime priority.\n");
1248 exit(-1);
1249 }
1250 }
1251
1252 while (1) {
Ingo Molnar2debbc82009-06-05 14:29:10 +02001253 int hits = samples;
Ingo Molnar07800602009-04-20 15:00:56 +02001254
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001255 mmap_read();
Ingo Molnar07800602009-04-20 15:00:56 +02001256
Ingo Molnar2debbc82009-06-05 14:29:10 +02001257 if (hits == samples)
Ingo Molnar07800602009-04-20 15:00:56 +02001258 ret = poll(event_array, nr_poll, 100);
1259 }
1260
1261 return 0;
1262}
Ingo Molnarb456bae2009-05-26 09:17:18 +02001263
1264static const char * const top_usage[] = {
1265 "perf top [<options>]",
1266 NULL
1267};
1268
Ingo Molnarb456bae2009-05-26 09:17:18 +02001269static const struct option options[] = {
1270 OPT_CALLBACK('e', "event", NULL, "event",
Thomas Gleixner86847b62009-06-06 12:24:17 +02001271 "event selector. use 'perf list' to list available events",
1272 parse_events),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001273 OPT_INTEGER('c', "count", &default_interval,
1274 "event period to sample"),
1275 OPT_INTEGER('p', "pid", &target_pid,
1276 "profile events on existing pid"),
1277 OPT_BOOLEAN('a', "all-cpus", &system_wide,
1278 "system-wide collection from all CPUs"),
1279 OPT_INTEGER('C', "CPU", &profile_cpu,
1280 "CPU to profile on"),
Ingo Molnar83a09442009-08-15 12:26:57 +02001281 OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -02001282 OPT_BOOLEAN('K', "hide_kernel_symbols", &hide_kernel_symbols,
1283 "hide kernel symbols"),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001284 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
1285 "number of mmap data pages"),
1286 OPT_INTEGER('r', "realtime", &realtime_prio,
1287 "collect data with this RT SCHED_FIFO priority"),
Mike Galbraithdb20c002009-05-26 15:25:34 +02001288 OPT_INTEGER('d', "delay", &delay_secs,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001289 "number of seconds to delay between refreshes"),
1290 OPT_BOOLEAN('D', "dump-symtab", &dump_symtab,
1291 "dump the symbol table used for profiling"),
Ingo Molnar6e53cdf2009-06-04 08:53:05 +02001292 OPT_INTEGER('f', "count-filter", &count_filter,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001293 "only display functions with more events than this"),
1294 OPT_BOOLEAN('g', "group", &group,
1295 "put the counters into a counter group"),
Mike Galbraith0fdc7e62009-07-21 10:30:36 +02001296 OPT_BOOLEAN('i', "inherit", &inherit,
1297 "child tasks inherit counters"),
Mike Galbraith923c42c2009-07-22 20:36:03 +02001298 OPT_STRING('s', "sym-annotate", &sym_filter, "symbol name",
1299 "symbol to annotate - requires -k option"),
Anton Blanchard1f208ea2009-07-01 09:00:44 +10001300 OPT_BOOLEAN('z', "zero", &zero,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001301 "zero history across updates"),
Ingo Molnar6e53cdf2009-06-04 08:53:05 +02001302 OPT_INTEGER('F', "freq", &freq,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001303 "profile at this frequency"),
Ingo Molnar6e53cdf2009-06-04 08:53:05 +02001304 OPT_INTEGER('E', "entries", &print_entries,
1305 "display this many functions"),
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -02001306 OPT_BOOLEAN('U', "hide_user_symbols", &hide_user_symbols,
1307 "hide user symbols"),
Ingo Molnar3da297a2009-06-07 17:39:02 +02001308 OPT_BOOLEAN('v', "verbose", &verbose,
1309 "be more verbose (show counter open errors, etc)"),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001310 OPT_END()
1311};
1312
Ingo Molnarf37a2912009-07-01 12:37:06 +02001313int cmd_top(int argc, const char **argv, const char *prefix __used)
Ingo Molnarb456bae2009-05-26 09:17:18 +02001314{
1315 int counter;
1316
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001317 symbol__init(sizeof(struct sym_entry));
Mike Galbraith42976482009-07-02 08:09:46 +02001318
Ingo Molnarb456bae2009-05-26 09:17:18 +02001319 page_size = sysconf(_SC_PAGE_SIZE);
1320
Ingo Molnarb456bae2009-05-26 09:17:18 +02001321 argc = parse_options(argc, argv, options, top_usage, 0);
1322 if (argc)
1323 usage_with_options(top_usage, options);
1324
Ingo Molnarb456bae2009-05-26 09:17:18 +02001325 /* CPU and PID are mutually exclusive */
1326 if (target_pid != -1 && profile_cpu != -1) {
1327 printf("WARNING: PID switch overriding CPU\n");
1328 sleep(1);
1329 profile_cpu = -1;
1330 }
1331
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001332 if (!nr_counters)
Ingo Molnarb456bae2009-05-26 09:17:18 +02001333 nr_counters = 1;
Ingo Molnarb456bae2009-05-26 09:17:18 +02001334
Frederic Weisbecker2f335a02009-06-05 19:31:01 +02001335 if (delay_secs < 1)
1336 delay_secs = 1;
1337
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001338 parse_symbols();
Mike Galbraith923c42c2009-07-22 20:36:03 +02001339 parse_source(sym_filter_entry);
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001340
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +02001341
1342 /*
1343 * User specified count overrides default frequency.
1344 */
1345 if (default_interval)
1346 freq = 0;
1347 else if (freq) {
1348 default_interval = freq;
1349 } else {
1350 fprintf(stderr, "frequency and count are zero, aborting\n");
1351 exit(EXIT_FAILURE);
1352 }
1353
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001354 /*
1355 * Fill in the ones not specifically initialized via -c:
1356 */
Ingo Molnarb456bae2009-05-26 09:17:18 +02001357 for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001358 if (attrs[counter].sample_period)
Ingo Molnarb456bae2009-05-26 09:17:18 +02001359 continue;
1360
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001361 attrs[counter].sample_period = default_interval;
Ingo Molnarb456bae2009-05-26 09:17:18 +02001362 }
1363
1364 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
1365 assert(nr_cpus <= MAX_NR_CPUS);
1366 assert(nr_cpus >= 0);
1367
1368 if (target_pid != -1 || profile_cpu != -1)
1369 nr_cpus = 1;
1370
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -02001371 get_term_dimensions(&winsize);
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -02001372 if (print_entries == 0) {
Arnaldo Carvalho de Melo13cc5072009-11-17 15:40:54 -02001373 update_print_entries(&winsize);
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -02001374 signal(SIGWINCH, sig_winch_handler);
1375 }
1376
Ingo Molnarb456bae2009-05-26 09:17:18 +02001377 return __cmd_top();
1378}