blob: 89b7f68a1799ee8f45c5e601d9ae8fd1f33e6763 [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;
81
Ingo Molnar07800602009-04-20 15:00:56 +020082/*
Mike Galbraith923c42c2009-07-22 20:36:03 +020083 * Source
84 */
85
86struct source_line {
87 u64 eip;
88 unsigned long count[MAX_COUNTERS];
89 char *line;
90 struct source_line *next;
91};
92
Ingo Molnar42e59d72009-10-06 15:14:21 +020093static char *sym_filter = NULL;
94struct sym_entry *sym_filter_entry = NULL;
95static int sym_pcnt_filter = 5;
96static int sym_counter = 0;
97static int display_weighted = -1;
Mike Galbraith923c42c2009-07-22 20:36:03 +020098
99/*
Ingo Molnar07800602009-04-20 15:00:56 +0200100 * Symbols
101 */
102
Ingo Molnar07800602009-04-20 15:00:56 +0200103struct sym_entry {
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300104 struct rb_node rb_node;
105 struct list_head node;
Ingo Molnar07800602009-04-20 15:00:56 +0200106 unsigned long count[MAX_COUNTERS];
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300107 unsigned long snap_count;
108 double weight;
Ingo Molnar07800602009-04-20 15:00:56 +0200109 int skip;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200110 u8 origin;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300111 struct map *map;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200112 struct source_line *source;
113 struct source_line *lines;
114 struct source_line **lines_tail;
115 pthread_mutex_t source_lock;
Ingo Molnar07800602009-04-20 15:00:56 +0200116};
117
Mike Galbraith923c42c2009-07-22 20:36:03 +0200118/*
119 * Source functions
120 */
121
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200122/* most GUI terminals set LINES (although some don't export it) */
123static int term_rows(void)
124{
125 char *lines_string = getenv("LINES");
126 int n_lines;
127
128 if (lines_string && (n_lines = atoi(lines_string)) > 0)
129 return n_lines;
130#ifdef TIOCGWINSZ
131 else {
132 struct winsize ws;
133 if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_row)
134 return ws.ws_row;
135 }
136#endif
137 return 25;
138}
139
140static void update_print_entries(void)
141{
142 print_entries = term_rows();
143 if (print_entries > 9)
144 print_entries -= 9;
145}
146
147static void sig_winch_handler(int sig __used)
148{
149 update_print_entries();
150}
151
Mike Galbraith923c42c2009-07-22 20:36:03 +0200152static void parse_source(struct sym_entry *syme)
153{
154 struct symbol *sym;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300155 struct map *map;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200156 FILE *file;
Ingo Molnar83a09442009-08-15 12:26:57 +0200157 char command[PATH_MAX*2];
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300158 const char *path;
159 u64 len;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200160
161 if (!syme)
162 return;
163
164 if (syme->lines) {
165 pthread_mutex_lock(&syme->source_lock);
166 goto out_assign;
167 }
168
169 sym = (struct symbol *)(syme + 1);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300170 map = syme->map;
171 path = map->dso->long_name;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200172
Mike Galbraith923c42c2009-07-22 20:36:03 +0200173 len = sym->end - sym->start;
174
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300175 sprintf(command,
176 "objdump --start-address=0x%016Lx "
177 "--stop-address=0x%016Lx -dS %s",
Arnaldo Carvalho de Meloc88e4bf2009-10-20 15:54:55 -0200178 map->unmap_ip(map, sym->start),
179 map->unmap_ip(map, sym->end), path);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200180
181 file = popen(command, "r");
182 if (!file)
183 return;
184
185 pthread_mutex_lock(&syme->source_lock);
186 syme->lines_tail = &syme->lines;
187 while (!feof(file)) {
188 struct source_line *src;
189 size_t dummy = 0;
190 char *c;
191
192 src = malloc(sizeof(struct source_line));
193 assert(src != NULL);
194 memset(src, 0, sizeof(struct source_line));
195
196 if (getline(&src->line, &dummy, file) < 0)
197 break;
198 if (!src->line)
199 break;
200
201 c = strchr(src->line, '\n');
202 if (c)
203 *c = 0;
204
205 src->next = NULL;
206 *syme->lines_tail = src;
207 syme->lines_tail = &src->next;
208
209 if (strlen(src->line)>8 && src->line[8] == ':') {
210 src->eip = strtoull(src->line, NULL, 16);
Arnaldo Carvalho de Meloc88e4bf2009-10-20 15:54:55 -0200211 src->eip = map->unmap_ip(map, src->eip);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200212 }
213 if (strlen(src->line)>8 && src->line[16] == ':') {
214 src->eip = strtoull(src->line, NULL, 16);
Arnaldo Carvalho de Meloc88e4bf2009-10-20 15:54:55 -0200215 src->eip = map->unmap_ip(map, src->eip);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200216 }
217 }
218 pclose(file);
219out_assign:
220 sym_filter_entry = syme;
221 pthread_mutex_unlock(&syme->source_lock);
222}
223
224static void __zero_source_counters(struct sym_entry *syme)
225{
226 int i;
227 struct source_line *line;
228
229 line = syme->lines;
230 while (line) {
231 for (i = 0; i < nr_counters; i++)
232 line->count[i] = 0;
233 line = line->next;
234 }
235}
236
237static void record_precise_ip(struct sym_entry *syme, int counter, u64 ip)
238{
239 struct source_line *line;
240
241 if (syme != sym_filter_entry)
242 return;
243
244 if (pthread_mutex_trylock(&syme->source_lock))
245 return;
246
247 if (!syme->source)
248 goto out_unlock;
249
250 for (line = syme->lines; line; line = line->next) {
251 if (line->eip == ip) {
252 line->count[counter]++;
253 break;
254 }
255 if (line->eip > ip)
256 break;
257 }
258out_unlock:
259 pthread_mutex_unlock(&syme->source_lock);
260}
261
262static void lookup_sym_source(struct sym_entry *syme)
263{
264 struct symbol *symbol = (struct symbol *)(syme + 1);
265 struct source_line *line;
266 char pattern[PATH_MAX];
Mike Galbraith923c42c2009-07-22 20:36:03 +0200267
268 sprintf(pattern, "<%s>:", symbol->name);
269
Mike Galbraith923c42c2009-07-22 20:36:03 +0200270 pthread_mutex_lock(&syme->source_lock);
271 for (line = syme->lines; line; line = line->next) {
272 if (strstr(line->line, pattern)) {
273 syme->source = line;
274 break;
275 }
276 }
277 pthread_mutex_unlock(&syme->source_lock);
278}
279
280static void show_lines(struct source_line *queue, int count, int total)
281{
282 int i;
283 struct source_line *line;
284
285 line = queue;
286 for (i = 0; i < count; i++) {
287 float pcnt = 100.0*(float)line->count[sym_counter]/(float)total;
288
289 printf("%8li %4.1f%%\t%s\n", line->count[sym_counter], pcnt, line->line);
290 line = line->next;
291 }
292}
293
294#define TRACE_COUNT 3
295
296static void show_details(struct sym_entry *syme)
297{
298 struct symbol *symbol;
299 struct source_line *line;
300 struct source_line *line_queue = NULL;
301 int displayed = 0;
302 int line_queue_count = 0, total = 0, more = 0;
303
304 if (!syme)
305 return;
306
307 if (!syme->source)
308 lookup_sym_source(syme);
309
310 if (!syme->source)
311 return;
312
313 symbol = (struct symbol *)(syme + 1);
314 printf("Showing %s for %s\n", event_name(sym_counter), symbol->name);
315 printf(" Events Pcnt (>=%d%%)\n", sym_pcnt_filter);
316
317 pthread_mutex_lock(&syme->source_lock);
318 line = syme->source;
319 while (line) {
320 total += line->count[sym_counter];
321 line = line->next;
322 }
323
324 line = syme->source;
325 while (line) {
326 float pcnt = 0.0;
327
328 if (!line_queue_count)
329 line_queue = line;
330 line_queue_count++;
331
332 if (line->count[sym_counter])
333 pcnt = 100.0 * line->count[sym_counter] / (float)total;
334 if (pcnt >= (float)sym_pcnt_filter) {
335 if (displayed <= print_entries)
336 show_lines(line_queue, line_queue_count, total);
337 else more++;
338 displayed += line_queue_count;
339 line_queue_count = 0;
340 line_queue = NULL;
341 } else if (line_queue_count > TRACE_COUNT) {
342 line_queue = line_queue->next;
343 line_queue_count--;
344 }
345
346 line->count[sym_counter] = zero ? 0 : line->count[sym_counter] * 7 / 8;
347 line = line->next;
348 }
349 pthread_mutex_unlock(&syme->source_lock);
350 if (more)
351 printf("%d lines not displayed, maybe increase display entries [e]\n", more);
352}
Ingo Molnar07800602009-04-20 15:00:56 +0200353
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300354/*
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200355 * Symbols will be added here in event__process_sample and will get out
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300356 * after decayed.
357 */
358static LIST_HEAD(active_symbols);
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300359static pthread_mutex_t active_symbols_lock = PTHREAD_MUTEX_INITIALIZER;
Ingo Molnar07800602009-04-20 15:00:56 +0200360
Ingo Molnar07800602009-04-20 15:00:56 +0200361/*
362 * Ordering weight: count-1 * count-2 * ... / count-n
363 */
364static double sym_weight(const struct sym_entry *sym)
365{
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300366 double weight = sym->snap_count;
Ingo Molnar07800602009-04-20 15:00:56 +0200367 int counter;
368
Mike Galbraith46ab9762009-07-24 10:09:50 +0200369 if (!display_weighted)
370 return weight;
371
Ingo Molnar07800602009-04-20 15:00:56 +0200372 for (counter = 1; counter < nr_counters-1; counter++)
373 weight *= sym->count[counter];
374
375 weight /= (sym->count[counter] + 1);
376
377 return weight;
378}
379
Ingo Molnar2debbc82009-06-05 14:29:10 +0200380static long samples;
381static long userspace_samples;
Ingo Molnar07800602009-04-20 15:00:56 +0200382static const char CONSOLE_CLEAR[] = "";
383
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300384static void __list_insert_active_sym(struct sym_entry *syme)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300385{
386 list_add(&syme->node, &active_symbols);
387}
388
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300389static void list_remove_active_sym(struct sym_entry *syme)
390{
391 pthread_mutex_lock(&active_symbols_lock);
392 list_del_init(&syme->node);
393 pthread_mutex_unlock(&active_symbols_lock);
394}
395
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300396static void rb_insert_active_sym(struct rb_root *tree, struct sym_entry *se)
397{
398 struct rb_node **p = &tree->rb_node;
399 struct rb_node *parent = NULL;
400 struct sym_entry *iter;
401
402 while (*p != NULL) {
403 parent = *p;
404 iter = rb_entry(parent, struct sym_entry, rb_node);
405
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300406 if (se->weight > iter->weight)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300407 p = &(*p)->rb_left;
408 else
409 p = &(*p)->rb_right;
410 }
411
412 rb_link_node(&se->rb_node, parent, p);
413 rb_insert_color(&se->rb_node, tree);
414}
Ingo Molnar07800602009-04-20 15:00:56 +0200415
416static void print_sym_table(void)
417{
Ingo Molnar233f0b92009-06-03 21:48:40 +0200418 int printed = 0, j;
Mike Galbraith46ab9762009-07-24 10:09:50 +0200419 int counter, snap = !display_weighted ? sym_counter : 0;
Ingo Molnar2debbc82009-06-05 14:29:10 +0200420 float samples_per_sec = samples/delay_secs;
421 float ksamples_per_sec = (samples-userspace_samples)/delay_secs;
422 float sum_ksamples = 0.0;
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300423 struct sym_entry *syme, *n;
424 struct rb_root tmp = RB_ROOT;
425 struct rb_node *nd;
Ingo Molnar07800602009-04-20 15:00:56 +0200426
Ingo Molnar2debbc82009-06-05 14:29:10 +0200427 samples = userspace_samples = 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200428
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300429 /* Sort the active symbols */
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300430 pthread_mutex_lock(&active_symbols_lock);
431 syme = list_entry(active_symbols.next, struct sym_entry, node);
432 pthread_mutex_unlock(&active_symbols_lock);
433
434 list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
Mike Galbraith46ab9762009-07-24 10:09:50 +0200435 syme->snap_count = syme->count[snap];
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300436 if (syme->snap_count != 0) {
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200437 if ((hide_user_symbols &&
438 syme->origin == PERF_RECORD_MISC_USER) ||
439 (hide_kernel_symbols &&
440 syme->origin == PERF_RECORD_MISC_KERNEL)) {
441 list_remove_active_sym(syme);
442 continue;
443 }
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300444 syme->weight = sym_weight(syme);
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300445 rb_insert_active_sym(&tmp, syme);
Ingo Molnar2debbc82009-06-05 14:29:10 +0200446 sum_ksamples += syme->snap_count;
Mike Galbraithd94b9432009-05-25 09:57:56 +0200447
448 for (j = 0; j < nr_counters; j++)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300449 syme->count[j] = zero ? 0 : syme->count[j] * 7 / 8;
450 } else
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300451 list_remove_active_sym(syme);
Mike Galbraithd94b9432009-05-25 09:57:56 +0200452 }
453
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200454 puts(CONSOLE_CLEAR);
Ingo Molnar07800602009-04-20 15:00:56 +0200455
456 printf(
457"------------------------------------------------------------------------------\n");
Ingo Molnarf2521b62009-06-03 19:17:25 +0200458 printf( " PerfTop:%8.0f irqs/sec kernel:%4.1f%% [",
Ingo Molnar2debbc82009-06-05 14:29:10 +0200459 samples_per_sec,
460 100.0 - (100.0*((samples_per_sec-ksamples_per_sec)/samples_per_sec)));
Ingo Molnar07800602009-04-20 15:00:56 +0200461
Mike Galbraith46ab9762009-07-24 10:09:50 +0200462 if (nr_counters == 1 || !display_weighted) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000463 printf("%Ld", (u64)attrs[0].sample_period);
Ingo Molnarcf1f4572009-06-05 13:27:02 +0200464 if (freq)
465 printf("Hz ");
466 else
467 printf(" ");
468 }
Ingo Molnar07800602009-04-20 15:00:56 +0200469
Mike Galbraith46ab9762009-07-24 10:09:50 +0200470 if (!display_weighted)
471 printf("%s", event_name(sym_counter));
472 else for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnar07800602009-04-20 15:00:56 +0200473 if (counter)
474 printf("/");
475
476 printf("%s", event_name(counter));
477 }
478
479 printf( "], ");
480
Ingo Molnarb456bae2009-05-26 09:17:18 +0200481 if (target_pid != -1)
482 printf(" (target_pid: %d", target_pid);
Ingo Molnar07800602009-04-20 15:00:56 +0200483 else
484 printf(" (all");
485
486 if (profile_cpu != -1)
487 printf(", cpu: %d)\n", profile_cpu);
488 else {
Ingo Molnarb456bae2009-05-26 09:17:18 +0200489 if (target_pid != -1)
Ingo Molnar07800602009-04-20 15:00:56 +0200490 printf(")\n");
491 else
492 printf(", %d CPUs)\n", nr_cpus);
493 }
494
495 printf("------------------------------------------------------------------------------\n\n");
496
Mike Galbraith923c42c2009-07-22 20:36:03 +0200497 if (sym_filter_entry) {
498 show_details(sym_filter_entry);
499 return;
500 }
501
Ingo Molnar07800602009-04-20 15:00:56 +0200502 if (nr_counters == 1)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200503 printf(" samples pcnt");
Ingo Molnar07800602009-04-20 15:00:56 +0200504 else
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200505 printf(" weight samples pcnt");
Ingo Molnar07800602009-04-20 15:00:56 +0200506
Arnaldo Carvalho de Melo7ced1562009-08-26 11:51:26 -0300507 if (verbose)
508 printf(" RIP ");
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200509 printf(" function DSO\n");
510 printf(" %s _______ _____",
Arnaldo Carvalho de Melo7ced1562009-08-26 11:51:26 -0300511 nr_counters == 1 ? " " : "______");
512 if (verbose)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200513 printf(" ________________");
514 printf(" ________________________________ ________________\n\n");
Ingo Molnar07800602009-04-20 15:00:56 +0200515
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300516 for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
Ingo Molnar83a09442009-08-15 12:26:57 +0200517 struct symbol *sym;
Ingo Molnar8fc03212009-06-04 15:19:47 +0200518 double pcnt;
Ingo Molnar07800602009-04-20 15:00:56 +0200519
Ingo Molnar83a09442009-08-15 12:26:57 +0200520 syme = rb_entry(nd, struct sym_entry, rb_node);
521 sym = (struct symbol *)(syme + 1);
522
Mike Galbraith923c42c2009-07-22 20:36:03 +0200523 if (++printed > print_entries || (int)syme->snap_count < count_filter)
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300524 continue;
Ingo Molnar07800602009-04-20 15:00:56 +0200525
Ingo Molnar2debbc82009-06-05 14:29:10 +0200526 pcnt = 100.0 - (100.0 * ((sum_ksamples - syme->snap_count) /
527 sum_ksamples));
Mike Galbraithd94b9432009-05-25 09:57:56 +0200528
Mike Galbraith46ab9762009-07-24 10:09:50 +0200529 if (nr_counters == 1 || !display_weighted)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200530 printf("%20.2f ", syme->weight);
Mike Galbraithd94b9432009-05-25 09:57:56 +0200531 else
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200532 printf("%9.1f %10ld ", syme->weight, syme->snap_count);
Ingo Molnar8fc03212009-06-04 15:19:47 +0200533
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200534 percent_color_fprintf(stdout, "%4.1f%%", pcnt);
Arnaldo Carvalho de Melo7ced1562009-08-26 11:51:26 -0300535 if (verbose)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200536 printf(" %016llx", sym->start);
537 printf(" %-32s", sym->name);
538 printf(" %s", syme->map->dso->short_name);
Mike Galbraith42976482009-07-02 08:09:46 +0200539 printf("\n");
Ingo Molnar07800602009-04-20 15:00:56 +0200540 }
Ingo Molnar07800602009-04-20 15:00:56 +0200541}
542
Mike Galbraith923c42c2009-07-22 20:36:03 +0200543static void prompt_integer(int *target, const char *msg)
544{
545 char *buf = malloc(0), *p;
546 size_t dummy = 0;
547 int tmp;
548
549 fprintf(stdout, "\n%s: ", msg);
550 if (getline(&buf, &dummy, stdin) < 0)
551 return;
552
553 p = strchr(buf, '\n');
554 if (p)
555 *p = 0;
556
557 p = buf;
558 while(*p) {
559 if (!isdigit(*p))
560 goto out_free;
561 p++;
562 }
563 tmp = strtoul(buf, NULL, 10);
564 *target = tmp;
565out_free:
566 free(buf);
567}
568
569static void prompt_percent(int *target, const char *msg)
570{
571 int tmp = 0;
572
573 prompt_integer(&tmp, msg);
574 if (tmp >= 0 && tmp <= 100)
575 *target = tmp;
576}
577
578static void prompt_symbol(struct sym_entry **target, const char *msg)
579{
580 char *buf = malloc(0), *p;
581 struct sym_entry *syme = *target, *n, *found = NULL;
582 size_t dummy = 0;
583
584 /* zero counters of active symbol */
585 if (syme) {
586 pthread_mutex_lock(&syme->source_lock);
587 __zero_source_counters(syme);
588 *target = NULL;
589 pthread_mutex_unlock(&syme->source_lock);
590 }
591
592 fprintf(stdout, "\n%s: ", msg);
593 if (getline(&buf, &dummy, stdin) < 0)
594 goto out_free;
595
596 p = strchr(buf, '\n');
597 if (p)
598 *p = 0;
599
600 pthread_mutex_lock(&active_symbols_lock);
601 syme = list_entry(active_symbols.next, struct sym_entry, node);
602 pthread_mutex_unlock(&active_symbols_lock);
603
604 list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
605 struct symbol *sym = (struct symbol *)(syme + 1);
606
607 if (!strcmp(buf, sym->name)) {
608 found = syme;
609 break;
610 }
611 }
612
613 if (!found) {
614 fprintf(stderr, "Sorry, %s is not active.\n", sym_filter);
615 sleep(1);
616 return;
617 } else
618 parse_source(found);
619
620out_free:
621 free(buf);
622}
623
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200624static void print_mapped_keys(void)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200625{
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200626 char *name = NULL;
627
628 if (sym_filter_entry) {
629 struct symbol *sym = (struct symbol *)(sym_filter_entry+1);
630 name = sym->name;
631 }
632
633 fprintf(stdout, "\nMapped keys:\n");
634 fprintf(stdout, "\t[d] display refresh delay. \t(%d)\n", delay_secs);
635 fprintf(stdout, "\t[e] display entries (lines). \t(%d)\n", print_entries);
636
637 if (nr_counters > 1)
638 fprintf(stdout, "\t[E] active event counter. \t(%s)\n", event_name(sym_counter));
639
640 fprintf(stdout, "\t[f] profile display filter (count). \t(%d)\n", count_filter);
641
Ingo Molnar83a09442009-08-15 12:26:57 +0200642 if (vmlinux_name) {
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200643 fprintf(stdout, "\t[F] annotate display filter (percent). \t(%d%%)\n", sym_pcnt_filter);
644 fprintf(stdout, "\t[s] annotate symbol. \t(%s)\n", name?: "NULL");
645 fprintf(stdout, "\t[S] stop annotation.\n");
646 }
647
648 if (nr_counters > 1)
649 fprintf(stdout, "\t[w] toggle display weighted/count[E]r. \t(%d)\n", display_weighted ? 1 : 0);
650
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200651 fprintf(stdout,
652 "\t[K] hide kernel_symbols symbols. \t(%s)\n",
653 hide_kernel_symbols ? "yes" : "no");
654 fprintf(stdout,
655 "\t[U] hide user symbols. \t(%s)\n",
656 hide_user_symbols ? "yes" : "no");
Mike Galbraith46ab9762009-07-24 10:09:50 +0200657 fprintf(stdout, "\t[z] toggle sample zeroing. \t(%d)\n", zero ? 1 : 0);
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200658 fprintf(stdout, "\t[qQ] quit.\n");
659}
660
661static int key_mapped(int c)
662{
663 switch (c) {
664 case 'd':
665 case 'e':
666 case 'f':
667 case 'z':
668 case 'q':
669 case 'Q':
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200670 case 'K':
671 case 'U':
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200672 return 1;
673 case 'E':
674 case 'w':
675 return nr_counters > 1 ? 1 : 0;
676 case 'F':
677 case 's':
678 case 'S':
Ingo Molnar83a09442009-08-15 12:26:57 +0200679 return vmlinux_name ? 1 : 0;
680 default:
681 break;
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200682 }
683
684 return 0;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200685}
686
687static void handle_keypress(int c)
688{
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200689 if (!key_mapped(c)) {
690 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
691 struct termios tc, save;
692
693 print_mapped_keys();
694 fprintf(stdout, "\nEnter selection, or unmapped key to continue: ");
695 fflush(stdout);
696
697 tcgetattr(0, &save);
698 tc = save;
699 tc.c_lflag &= ~(ICANON | ECHO);
700 tc.c_cc[VMIN] = 0;
701 tc.c_cc[VTIME] = 0;
702 tcsetattr(0, TCSANOW, &tc);
703
704 poll(&stdin_poll, 1, -1);
705 c = getc(stdin);
706
707 tcsetattr(0, TCSAFLUSH, &save);
708 if (!key_mapped(c))
709 return;
710 }
711
Mike Galbraith923c42c2009-07-22 20:36:03 +0200712 switch (c) {
713 case 'd':
714 prompt_integer(&delay_secs, "Enter display delay");
Tim Blechmanndc799592009-10-17 18:08:29 +0200715 if (delay_secs < 1)
716 delay_secs = 1;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200717 break;
718 case 'e':
719 prompt_integer(&print_entries, "Enter display entries (lines)");
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200720 if (print_entries == 0) {
721 update_print_entries();
722 signal(SIGWINCH, sig_winch_handler);
723 } else
724 signal(SIGWINCH, SIG_DFL);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200725 break;
726 case 'E':
727 if (nr_counters > 1) {
728 int i;
729
730 fprintf(stderr, "\nAvailable events:");
731 for (i = 0; i < nr_counters; i++)
732 fprintf(stderr, "\n\t%d %s", i, event_name(i));
733
734 prompt_integer(&sym_counter, "Enter details event counter");
735
736 if (sym_counter >= nr_counters) {
737 fprintf(stderr, "Sorry, no such event, using %s.\n", event_name(0));
738 sym_counter = 0;
739 sleep(1);
740 }
741 } else sym_counter = 0;
742 break;
743 case 'f':
744 prompt_integer(&count_filter, "Enter display event count filter");
745 break;
746 case 'F':
747 prompt_percent(&sym_pcnt_filter, "Enter details display event filter (percent)");
748 break;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200749 case 'K':
750 hide_kernel_symbols = !hide_kernel_symbols;
751 break;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200752 case 'q':
753 case 'Q':
754 printf("exiting.\n");
755 exit(0);
756 case 's':
757 prompt_symbol(&sym_filter_entry, "Enter details symbol");
758 break;
759 case 'S':
760 if (!sym_filter_entry)
761 break;
762 else {
763 struct sym_entry *syme = sym_filter_entry;
764
765 pthread_mutex_lock(&syme->source_lock);
766 sym_filter_entry = NULL;
767 __zero_source_counters(syme);
768 pthread_mutex_unlock(&syme->source_lock);
769 }
770 break;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200771 case 'U':
772 hide_user_symbols = !hide_user_symbols;
773 break;
Mike Galbraith46ab9762009-07-24 10:09:50 +0200774 case 'w':
775 display_weighted = ~display_weighted;
776 break;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200777 case 'z':
778 zero = ~zero;
779 break;
Ingo Molnar83a09442009-08-15 12:26:57 +0200780 default:
781 break;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200782 }
783}
784
Ingo Molnarf37a2912009-07-01 12:37:06 +0200785static void *display_thread(void *arg __used)
Ingo Molnar07800602009-04-20 15:00:56 +0200786{
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200787 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
Mike Galbraith923c42c2009-07-22 20:36:03 +0200788 struct termios tc, save;
789 int delay_msecs, c;
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200790
Mike Galbraith923c42c2009-07-22 20:36:03 +0200791 tcgetattr(0, &save);
792 tc = save;
793 tc.c_lflag &= ~(ICANON | ECHO);
794 tc.c_cc[VMIN] = 0;
795 tc.c_cc[VTIME] = 0;
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200796
Mike Galbraith923c42c2009-07-22 20:36:03 +0200797repeat:
798 delay_msecs = delay_secs * 1000;
799 tcsetattr(0, TCSANOW, &tc);
800 /* trash return*/
801 getc(stdin);
Ingo Molnar07800602009-04-20 15:00:56 +0200802
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200803 do {
Ingo Molnar07800602009-04-20 15:00:56 +0200804 print_sym_table();
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200805 } while (!poll(&stdin_poll, 1, delay_msecs) == 1);
806
Mike Galbraith923c42c2009-07-22 20:36:03 +0200807 c = getc(stdin);
808 tcsetattr(0, TCSAFLUSH, &save);
809
810 handle_keypress(c);
811 goto repeat;
Ingo Molnar07800602009-04-20 15:00:56 +0200812
813 return NULL;
814}
815
Anton Blanchard2ab52082009-07-01 09:00:46 +1000816/* Tag samples to be skipped. */
Ingo Molnarf37a2912009-07-01 12:37:06 +0200817static const char *skip_symbols[] = {
Anton Blanchard2ab52082009-07-01 09:00:46 +1000818 "default_idle",
819 "cpu_idle",
820 "enter_idle",
821 "exit_idle",
822 "mwait_idle",
Arnaldo Carvalho de Melo59b90052009-07-26 19:06:19 -0300823 "mwait_idle_with_hints",
Arnaldo Carvalho de Melo83572752009-09-25 15:02:39 -0700824 "poll_idle",
Anton Blanchard3a3393e2009-07-01 09:00:47 +1000825 "ppc64_runlatch_off",
826 "pseries_dedicated_idle_sleep",
Anton Blanchard2ab52082009-07-01 09:00:46 +1000827 NULL
828};
829
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300830static int symbol_filter(struct map *map, struct symbol *sym)
Ingo Molnar07800602009-04-20 15:00:56 +0200831{
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300832 struct sym_entry *syme;
833 const char *name = sym->name;
Anton Blanchard2ab52082009-07-01 09:00:46 +1000834 int i;
Ingo Molnar07800602009-04-20 15:00:56 +0200835
Anton Blanchard3a3393e2009-07-01 09:00:47 +1000836 /*
837 * ppc64 uses function descriptors and appends a '.' to the
838 * start of every instruction address. Remove it.
839 */
840 if (name[0] == '.')
841 name++;
842
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300843 if (!strcmp(name, "_text") ||
844 !strcmp(name, "_etext") ||
845 !strcmp(name, "_sinittext") ||
846 !strncmp("init_module", name, 11) ||
847 !strncmp("cleanup_module", name, 14) ||
848 strstr(name, "_text_start") ||
849 strstr(name, "_text_end"))
Ingo Molnar07800602009-04-20 15:00:56 +0200850 return 1;
851
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200852 syme = symbol__priv(sym);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300853 syme->map = map;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200854 pthread_mutex_init(&syme->source_lock, NULL);
855 if (!sym_filter_entry && sym_filter && !strcmp(name, sym_filter))
856 sym_filter_entry = syme;
857
Anton Blanchard2ab52082009-07-01 09:00:46 +1000858 for (i = 0; skip_symbols[i]; i++) {
859 if (!strcmp(skip_symbols[i], name)) {
860 syme->skip = 1;
861 break;
862 }
863 }
Ingo Molnar07800602009-04-20 15:00:56 +0200864
Ingo Molnar07800602009-04-20 15:00:56 +0200865 return 0;
866}
867
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300868static int parse_symbols(void)
Ingo Molnar07800602009-04-20 15:00:56 +0200869{
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200870 if (dsos__load_kernel(vmlinux_name, symbol_filter, 1) <= 0)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300871 return -1;
Ingo Molnar07800602009-04-20 15:00:56 +0200872
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300873 if (dump_symtab)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300874 dsos__fprintf(stderr);
Ingo Molnar07800602009-04-20 15:00:56 +0200875
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300876 return 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200877}
878
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200879static void event__process_sample(const event_t *self, int counter)
Ingo Molnar07800602009-04-20 15:00:56 +0200880{
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200881 u64 ip = self->ip.ip;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300882 struct map *map;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200883 struct sym_entry *syme;
884 struct symbol *sym;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200885 u8 origin = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Ingo Molnar07800602009-04-20 15:00:56 +0200886
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200887 switch (origin) {
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200888 case PERF_RECORD_MISC_USER: {
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200889 struct thread *thread;
Ingo Molnar07800602009-04-20 15:00:56 +0200890
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200891 if (hide_user_symbols)
892 return;
893
894 thread = threads__findnew(self->ip.pid);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200895 if (thread == NULL)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300896 return;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200897
898 map = thread__find_map(thread, ip);
899 if (map != NULL) {
900 ip = map->map_ip(map, ip);
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200901 sym = map__find_symbol(map, ip, symbol_filter);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200902 if (sym == NULL)
903 return;
904 userspace_samples++;
905 break;
Ingo Molnar07800602009-04-20 15:00:56 +0200906 }
Ingo Molnar07800602009-04-20 15:00:56 +0200907 }
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200908 /*
909 * If this is outside of all known maps,
910 * and is a negative address, try to look it
911 * up in the kernel dso, as it might be a
912 * vsyscall or vdso (which executes in user-mode).
913 */
914 if ((long long)ip >= 0)
915 return;
916 /* Fall thru */
917 case PERF_RECORD_MISC_KERNEL:
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200918 if (hide_kernel_symbols)
919 return;
920
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200921 sym = kernel_maps__find_symbol(ip, &map);
922 if (sym == NULL)
923 return;
924 break;
925 default:
Ingo Molnar07800602009-04-20 15:00:56 +0200926 return;
927 }
928
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200929 syme = symbol__priv(sym);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200930
931 if (!syme->skip) {
932 syme->count[counter]++;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200933 syme->origin = origin;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200934 record_precise_ip(syme, counter, ip);
935 pthread_mutex_lock(&active_symbols_lock);
936 if (list_empty(&syme->node) || !syme->node.next)
937 __list_insert_active_sym(syme);
938 pthread_mutex_unlock(&active_symbols_lock);
939 ++samples;
940 return;
941 }
942}
943
944static void event__process_mmap(event_t *self)
945{
946 struct thread *thread = threads__findnew(self->mmap.pid);
947
948 if (thread != NULL) {
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200949 struct map *map = map__new(&self->mmap, NULL, 0);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200950 if (map != NULL)
951 thread__insert_map(thread, map);
952 }
953}
954
955static void event__process_comm(event_t *self)
956{
957 struct thread *thread = threads__findnew(self->comm.pid);
958
959 if (thread != NULL)
960 thread__set_comm(thread, self->comm.comm);
961}
962
963static int event__process(event_t *event)
964{
965 switch (event->header.type) {
966 case PERF_RECORD_COMM:
967 event__process_comm(event);
968 break;
969 case PERF_RECORD_MMAP:
970 event__process_mmap(event);
971 break;
972 default:
973 break;
974 }
975
976 return 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200977}
978
Ingo Molnar07800602009-04-20 15:00:56 +0200979struct mmap_data {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200980 int counter;
981 void *base;
Ingo Molnarf37a2912009-07-01 12:37:06 +0200982 int mask;
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200983 unsigned int prev;
Ingo Molnar07800602009-04-20 15:00:56 +0200984};
985
986static unsigned int mmap_read_head(struct mmap_data *md)
987{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200988 struct perf_event_mmap_page *pc = md->base;
Ingo Molnar07800602009-04-20 15:00:56 +0200989 int head;
990
991 head = pc->data_head;
992 rmb();
993
994 return head;
995}
996
Frederic Weisbecker2f011902009-06-06 23:10:43 +0200997static void mmap_read_counter(struct mmap_data *md)
Ingo Molnar07800602009-04-20 15:00:56 +0200998{
999 unsigned int head = mmap_read_head(md);
1000 unsigned int old = md->prev;
1001 unsigned char *data = md->base + page_size;
1002 int diff;
1003
Ingo Molnar07800602009-04-20 15:00:56 +02001004 /*
1005 * If we're further behind than half the buffer, there's a chance
Ingo Molnar2debbc82009-06-05 14:29:10 +02001006 * the writer will bite our tail and mess up the samples under us.
Ingo Molnar07800602009-04-20 15:00:56 +02001007 *
1008 * If we somehow ended up ahead of the head, we got messed up.
1009 *
1010 * In either case, truncate and restart at head.
1011 */
1012 diff = head - old;
1013 if (diff > md->mask / 2 || diff < 0) {
Mike Galbraithf4f0b412009-10-13 14:57:20 +02001014 fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
Ingo Molnar07800602009-04-20 15:00:56 +02001015
1016 /*
1017 * head points to a known good entry, start there.
1018 */
1019 old = head;
1020 }
1021
Ingo Molnar07800602009-04-20 15:00:56 +02001022 for (; old != head;) {
Ingo Molnar07800602009-04-20 15:00:56 +02001023 event_t *event = (event_t *)&data[old & md->mask];
1024
1025 event_t event_copy;
1026
Ingo Molnar6f06ccb2009-04-20 15:22:22 +02001027 size_t size = event->header.size;
Ingo Molnar07800602009-04-20 15:00:56 +02001028
1029 /*
1030 * Event straddles the mmap boundary -- header should always
1031 * be inside due to u64 alignment of output.
1032 */
1033 if ((old & md->mask) + size != ((old + size) & md->mask)) {
1034 unsigned int offset = old;
1035 unsigned int len = min(sizeof(*event), size), cpy;
1036 void *dst = &event_copy;
1037
1038 do {
1039 cpy = min(md->mask + 1 - (offset & md->mask), len);
1040 memcpy(dst, &data[offset & md->mask], cpy);
1041 offset += cpy;
1042 dst += cpy;
1043 len -= cpy;
1044 } while (len);
1045
1046 event = &event_copy;
1047 }
1048
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001049 if (event->header.type == PERF_RECORD_SAMPLE)
1050 event__process_sample(event, md->counter);
1051 else
1052 event__process(event);
Ingo Molnar07800602009-04-20 15:00:56 +02001053 old += size;
Ingo Molnar07800602009-04-20 15:00:56 +02001054 }
1055
1056 md->prev = old;
1057}
1058
Mike Galbraithc2990a22009-05-24 08:35:49 +02001059static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
1060static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
1061
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001062static void mmap_read(void)
1063{
1064 int i, counter;
1065
1066 for (i = 0; i < nr_cpus; i++) {
1067 for (counter = 0; counter < nr_counters; counter++)
1068 mmap_read_counter(&mmap_array[i][counter]);
1069 }
1070}
1071
Ingo Molnar716c69f2009-06-07 17:31:52 +02001072int nr_poll;
1073int group_fd;
1074
1075static void start_counter(int i, int counter)
Ingo Molnar07800602009-04-20 15:00:56 +02001076{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001077 struct perf_event_attr *attr;
Mike Galbraith0fdc7e62009-07-21 10:30:36 +02001078 int cpu;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001079
1080 cpu = profile_cpu;
1081 if (target_pid == -1 && profile_cpu == -1)
1082 cpu = i;
1083
1084 attr = attrs + counter;
1085
1086 attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +02001087
1088 if (freq) {
1089 attr->sample_type |= PERF_SAMPLE_PERIOD;
1090 attr->freq = 1;
1091 attr->sample_freq = freq;
1092 }
1093
Mike Galbraith0fdc7e62009-07-21 10:30:36 +02001094 attr->inherit = (cpu < 0) && inherit;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001095 attr->mmap = 1;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001096
1097try_again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001098 fd[i][counter] = sys_perf_event_open(attr, target_pid, cpu, group_fd, 0);
Ingo Molnar716c69f2009-06-07 17:31:52 +02001099
1100 if (fd[i][counter] < 0) {
1101 int err = errno;
1102
Pekka Enbergc10edee2009-11-08 18:01:06 +02001103 if (err == EPERM || err == EACCES)
Ingo Molnar3da297a2009-06-07 17:39:02 +02001104 die("No permission - are you root?\n");
Ingo Molnar716c69f2009-06-07 17:31:52 +02001105 /*
1106 * If it's cycles then fall back to hrtimer
1107 * based cpu-clock-tick sw counter, which
1108 * is always available even if no PMU support:
1109 */
1110 if (attr->type == PERF_TYPE_HARDWARE
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02001111 && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
Ingo Molnar716c69f2009-06-07 17:31:52 +02001112
Ingo Molnar3da297a2009-06-07 17:39:02 +02001113 if (verbose)
1114 warning(" ... trying to fall back to cpu-clock-ticks\n");
1115
Ingo Molnar716c69f2009-06-07 17:31:52 +02001116 attr->type = PERF_TYPE_SOFTWARE;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02001117 attr->config = PERF_COUNT_SW_CPU_CLOCK;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001118 goto try_again;
1119 }
Ingo Molnar30c806a2009-06-07 17:46:24 +02001120 printf("\n");
1121 error("perfcounter syscall returned with %d (%s)\n",
1122 fd[i][counter], strerror(err));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001123 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
Ingo Molnar716c69f2009-06-07 17:31:52 +02001124 exit(-1);
1125 }
1126 assert(fd[i][counter] >= 0);
1127 fcntl(fd[i][counter], F_SETFL, O_NONBLOCK);
1128
1129 /*
1130 * First counter acts as the group leader:
1131 */
1132 if (group && group_fd == -1)
1133 group_fd = fd[i][counter];
1134
1135 event_array[nr_poll].fd = fd[i][counter];
1136 event_array[nr_poll].events = POLLIN;
1137 nr_poll++;
1138
1139 mmap_array[i][counter].counter = counter;
1140 mmap_array[i][counter].prev = 0;
1141 mmap_array[i][counter].mask = mmap_pages*page_size - 1;
1142 mmap_array[i][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
1143 PROT_READ, MAP_SHARED, fd[i][counter], 0);
1144 if (mmap_array[i][counter].base == MAP_FAILED)
1145 die("failed to mmap with %d (%s)\n", errno, strerror(errno));
1146}
1147
1148static int __cmd_top(void)
1149{
1150 pthread_t thread;
1151 int i, counter;
Ingo Molnar07800602009-04-20 15:00:56 +02001152 int ret;
1153
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001154 if (target_pid != -1)
1155 event__synthesize_thread(target_pid, event__process);
1156 else
1157 event__synthesize_threads(event__process);
1158
Ingo Molnar07800602009-04-20 15:00:56 +02001159 for (i = 0; i < nr_cpus; i++) {
1160 group_fd = -1;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001161 for (counter = 0; counter < nr_counters; counter++)
1162 start_counter(i, counter);
Ingo Molnar07800602009-04-20 15:00:56 +02001163 }
1164
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001165 /* Wait for a minimal set of events before starting the snapshot */
1166 poll(event_array, nr_poll, 100);
1167
1168 mmap_read();
1169
Ingo Molnar07800602009-04-20 15:00:56 +02001170 if (pthread_create(&thread, NULL, display_thread, NULL)) {
1171 printf("Could not create display thread.\n");
1172 exit(-1);
1173 }
1174
1175 if (realtime_prio) {
1176 struct sched_param param;
1177
1178 param.sched_priority = realtime_prio;
1179 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
1180 printf("Could not set realtime priority.\n");
1181 exit(-1);
1182 }
1183 }
1184
1185 while (1) {
Ingo Molnar2debbc82009-06-05 14:29:10 +02001186 int hits = samples;
Ingo Molnar07800602009-04-20 15:00:56 +02001187
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001188 mmap_read();
Ingo Molnar07800602009-04-20 15:00:56 +02001189
Ingo Molnar2debbc82009-06-05 14:29:10 +02001190 if (hits == samples)
Ingo Molnar07800602009-04-20 15:00:56 +02001191 ret = poll(event_array, nr_poll, 100);
1192 }
1193
1194 return 0;
1195}
Ingo Molnarb456bae2009-05-26 09:17:18 +02001196
1197static const char * const top_usage[] = {
1198 "perf top [<options>]",
1199 NULL
1200};
1201
Ingo Molnarb456bae2009-05-26 09:17:18 +02001202static const struct option options[] = {
1203 OPT_CALLBACK('e', "event", NULL, "event",
Thomas Gleixner86847b62009-06-06 12:24:17 +02001204 "event selector. use 'perf list' to list available events",
1205 parse_events),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001206 OPT_INTEGER('c', "count", &default_interval,
1207 "event period to sample"),
1208 OPT_INTEGER('p', "pid", &target_pid,
1209 "profile events on existing pid"),
1210 OPT_BOOLEAN('a', "all-cpus", &system_wide,
1211 "system-wide collection from all CPUs"),
1212 OPT_INTEGER('C', "CPU", &profile_cpu,
1213 "CPU to profile on"),
Ingo Molnar83a09442009-08-15 12:26:57 +02001214 OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -02001215 OPT_BOOLEAN('K', "hide_kernel_symbols", &hide_kernel_symbols,
1216 "hide kernel symbols"),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001217 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
1218 "number of mmap data pages"),
1219 OPT_INTEGER('r', "realtime", &realtime_prio,
1220 "collect data with this RT SCHED_FIFO priority"),
Mike Galbraithdb20c002009-05-26 15:25:34 +02001221 OPT_INTEGER('d', "delay", &delay_secs,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001222 "number of seconds to delay between refreshes"),
1223 OPT_BOOLEAN('D', "dump-symtab", &dump_symtab,
1224 "dump the symbol table used for profiling"),
Ingo Molnar6e53cdf2009-06-04 08:53:05 +02001225 OPT_INTEGER('f', "count-filter", &count_filter,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001226 "only display functions with more events than this"),
1227 OPT_BOOLEAN('g', "group", &group,
1228 "put the counters into a counter group"),
Mike Galbraith0fdc7e62009-07-21 10:30:36 +02001229 OPT_BOOLEAN('i', "inherit", &inherit,
1230 "child tasks inherit counters"),
Mike Galbraith923c42c2009-07-22 20:36:03 +02001231 OPT_STRING('s', "sym-annotate", &sym_filter, "symbol name",
1232 "symbol to annotate - requires -k option"),
Anton Blanchard1f208ea2009-07-01 09:00:44 +10001233 OPT_BOOLEAN('z', "zero", &zero,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001234 "zero history across updates"),
Ingo Molnar6e53cdf2009-06-04 08:53:05 +02001235 OPT_INTEGER('F', "freq", &freq,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001236 "profile at this frequency"),
Ingo Molnar6e53cdf2009-06-04 08:53:05 +02001237 OPT_INTEGER('E', "entries", &print_entries,
1238 "display this many functions"),
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -02001239 OPT_BOOLEAN('U', "hide_user_symbols", &hide_user_symbols,
1240 "hide user symbols"),
Ingo Molnar3da297a2009-06-07 17:39:02 +02001241 OPT_BOOLEAN('v', "verbose", &verbose,
1242 "be more verbose (show counter open errors, etc)"),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001243 OPT_END()
1244};
1245
Ingo Molnarf37a2912009-07-01 12:37:06 +02001246int cmd_top(int argc, const char **argv, const char *prefix __used)
Ingo Molnarb456bae2009-05-26 09:17:18 +02001247{
1248 int counter;
1249
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001250 symbol__init(sizeof(struct sym_entry));
Mike Galbraith42976482009-07-02 08:09:46 +02001251
Ingo Molnarb456bae2009-05-26 09:17:18 +02001252 page_size = sysconf(_SC_PAGE_SIZE);
1253
Ingo Molnarb456bae2009-05-26 09:17:18 +02001254 argc = parse_options(argc, argv, options, top_usage, 0);
1255 if (argc)
1256 usage_with_options(top_usage, options);
1257
Ingo Molnarb456bae2009-05-26 09:17:18 +02001258 /* CPU and PID are mutually exclusive */
1259 if (target_pid != -1 && profile_cpu != -1) {
1260 printf("WARNING: PID switch overriding CPU\n");
1261 sleep(1);
1262 profile_cpu = -1;
1263 }
1264
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001265 if (!nr_counters)
Ingo Molnarb456bae2009-05-26 09:17:18 +02001266 nr_counters = 1;
Ingo Molnarb456bae2009-05-26 09:17:18 +02001267
Frederic Weisbecker2f335a02009-06-05 19:31:01 +02001268 if (delay_secs < 1)
1269 delay_secs = 1;
1270
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001271 parse_symbols();
Mike Galbraith923c42c2009-07-22 20:36:03 +02001272 parse_source(sym_filter_entry);
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001273
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +02001274
1275 /*
1276 * User specified count overrides default frequency.
1277 */
1278 if (default_interval)
1279 freq = 0;
1280 else if (freq) {
1281 default_interval = freq;
1282 } else {
1283 fprintf(stderr, "frequency and count are zero, aborting\n");
1284 exit(EXIT_FAILURE);
1285 }
1286
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001287 /*
1288 * Fill in the ones not specifically initialized via -c:
1289 */
Ingo Molnarb456bae2009-05-26 09:17:18 +02001290 for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001291 if (attrs[counter].sample_period)
Ingo Molnarb456bae2009-05-26 09:17:18 +02001292 continue;
1293
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001294 attrs[counter].sample_period = default_interval;
Ingo Molnarb456bae2009-05-26 09:17:18 +02001295 }
1296
1297 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
1298 assert(nr_cpus <= MAX_NR_CPUS);
1299 assert(nr_cpus >= 0);
1300
1301 if (target_pid != -1 || profile_cpu != -1)
1302 nr_cpus = 1;
1303
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -02001304 if (print_entries == 0) {
1305 update_print_entries();
1306 signal(SIGWINCH, sig_winch_handler);
1307 }
1308
Ingo Molnarb456bae2009-05-26 09:17:18 +02001309 return __cmd_top();
1310}