perf evsel: Introduce perf_evlist
[deliverable/linux.git] / tools / perf / builtin-top.c
CommitLineData
07800601 1/*
bf9e1876
IM
2 * 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)
07800601 18 */
bf9e1876 19#include "builtin.h"
07800601 20
1a482f38 21#include "perf.h"
bf9e1876 22
8fc0321f 23#include "util/color.h"
361c99a6 24#include "util/evlist.h"
69aad6f1 25#include "util/evsel.h"
b3165f41
ACM
26#include "util/session.h"
27#include "util/symbol.h"
439d473b 28#include "util/thread.h"
148be2c1 29#include "util/util.h"
43cbcd8a 30#include <linux/rbtree.h>
b456bae0
IM
31#include "util/parse-options.h"
32#include "util/parse-events.h"
a12b51c4 33#include "util/cpumap.h"
69aad6f1 34#include "util/xyarray.h"
07800601 35
8f28827a
FW
36#include "util/debug.h"
37
07800601
IM
38#include <assert.h>
39#include <fcntl.h>
0e9b20b8 40
07800601 41#include <stdio.h>
923c42c1
MG
42#include <termios.h>
43#include <unistd.h>
9486aa38 44#include <inttypes.h>
0e9b20b8 45
07800601 46#include <errno.h>
07800601
IM
47#include <time.h>
48#include <sched.h>
49#include <pthread.h>
50
51#include <sys/syscall.h>
52#include <sys/ioctl.h>
53#include <sys/poll.h>
54#include <sys/prctl.h>
55#include <sys/wait.h>
56#include <sys/uio.h>
57#include <sys/mman.h>
58
59#include <linux/unistd.h>
60#include <linux/types.h>
61
69aad6f1 62#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
07800601 63
361c99a6
ACM
64struct perf_evlist *evsel_list;
65
c0555642 66static bool system_wide = false;
07800601 67
7e4ff9e3 68static int default_interval = 0;
07800601 69
42e59d7d 70static int count_filter = 5;
3b6ed988 71static int print_entries;
07800601 72
42e59d7d 73static int target_pid = -1;
d6d901c2 74static int target_tid = -1;
5c98d466 75static struct thread_map *threads;
c0555642 76static bool inherit = false;
60d567e2 77static struct cpu_map *cpus;
1967936d 78static int realtime_prio = 0;
c0555642 79static bool group = false;
07800601 80static unsigned int page_size;
42e59d7d
IM
81static unsigned int mmap_pages = 16;
82static int freq = 1000; /* 1 KHz */
07800601 83
42e59d7d 84static int delay_secs = 2;
c0555642
IM
85static bool zero = false;
86static bool dump_symtab = false;
07800601 87
8ffcda17
ACM
88static bool hide_kernel_symbols = false;
89static bool hide_user_symbols = false;
13cc5079 90static struct winsize winsize;
8ffcda17 91
923c42c1
MG
92/*
93 * Source
94 */
95
96struct source_line {
97 u64 eip;
98 unsigned long count[MAX_COUNTERS];
99 char *line;
100 struct source_line *next;
101};
102
edb7c60e 103static const char *sym_filter = NULL;
42e59d7d 104struct sym_entry *sym_filter_entry = NULL;
6cff0e8d 105struct sym_entry *sym_filter_entry_sched = NULL;
42e59d7d
IM
106static int sym_pcnt_filter = 5;
107static int sym_counter = 0;
69aad6f1 108static struct perf_evsel *sym_evsel = NULL;
42e59d7d 109static int display_weighted = -1;
c45c6ea2 110static const char *cpu_list;
923c42c1 111
07800601
IM
112/*
113 * Symbols
114 */
115
b269876c
ACM
116struct sym_entry_source {
117 struct source_line *source;
118 struct source_line *lines;
119 struct source_line **lines_tail;
120 pthread_mutex_t lock;
121};
122
07800601 123struct sym_entry {
de04687f
ACM
124 struct rb_node rb_node;
125 struct list_head node;
c44613a4
ACM
126 unsigned long snap_count;
127 double weight;
07800601 128 int skip;
13cc5079 129 u16 name_len;
8ffcda17 130 u8 origin;
439d473b 131 struct map *map;
b269876c 132 struct sym_entry_source *src;
5a8e5a30 133 unsigned long count[0];
07800601
IM
134};
135
923c42c1
MG
136/*
137 * Source functions
138 */
139
51a472de
ACM
140static inline struct symbol *sym_entry__symbol(struct sym_entry *self)
141{
b32d133a 142 return ((void *)self) + symbol_conf.priv_size;
51a472de
ACM
143}
144
895f0edc 145void get_term_dimensions(struct winsize *ws)
3b6ed988 146{
13cc5079
ACM
147 char *s = getenv("LINES");
148
149 if (s != NULL) {
150 ws->ws_row = atoi(s);
151 s = getenv("COLUMNS");
152 if (s != NULL) {
153 ws->ws_col = atoi(s);
154 if (ws->ws_row && ws->ws_col)
155 return;
156 }
3b6ed988 157 }
13cc5079
ACM
158#ifdef TIOCGWINSZ
159 if (ioctl(1, TIOCGWINSZ, ws) == 0 &&
160 ws->ws_row && ws->ws_col)
161 return;
3b6ed988 162#endif
13cc5079
ACM
163 ws->ws_row = 25;
164 ws->ws_col = 80;
3b6ed988
ACM
165}
166
13cc5079 167static void update_print_entries(struct winsize *ws)
3b6ed988 168{
13cc5079
ACM
169 print_entries = ws->ws_row;
170
3b6ed988
ACM
171 if (print_entries > 9)
172 print_entries -= 9;
173}
174
175static void sig_winch_handler(int sig __used)
176{
13cc5079
ACM
177 get_term_dimensions(&winsize);
178 update_print_entries(&winsize);
3b6ed988
ACM
179}
180
b0a9ab62 181static int parse_source(struct sym_entry *syme)
923c42c1
MG
182{
183 struct symbol *sym;
b269876c 184 struct sym_entry_source *source;
439d473b 185 struct map *map;
923c42c1 186 FILE *file;
83a0944f 187 char command[PATH_MAX*2];
439d473b
ACM
188 const char *path;
189 u64 len;
923c42c1
MG
190
191 if (!syme)
b0a9ab62
ACM
192 return -1;
193
194 sym = sym_entry__symbol(syme);
195 map = syme->map;
196
197 /*
198 * We can't annotate with just /proc/kallsyms
199 */
200 if (map->dso->origin == DSO__ORIG_KERNEL)
201 return -1;
923c42c1 202
b269876c 203 if (syme->src == NULL) {
36479484 204 syme->src = zalloc(sizeof(*source));
b269876c 205 if (syme->src == NULL)
b0a9ab62 206 return -1;
b269876c
ACM
207 pthread_mutex_init(&syme->src->lock, NULL);
208 }
209
210 source = syme->src;
211
212 if (source->lines) {
213 pthread_mutex_lock(&source->lock);
923c42c1
MG
214 goto out_assign;
215 }
439d473b 216 path = map->dso->long_name;
923c42c1 217
923c42c1
MG
218 len = sym->end - sym->start;
219
439d473b 220 sprintf(command,
9486aa38 221 "objdump --start-address=%#0*" PRIx64 " --stop-address=%#0*" PRIx64 " -dS %s",
5f485364
ACM
222 BITS_PER_LONG / 4, map__rip_2objdump(map, sym->start),
223 BITS_PER_LONG / 4, map__rip_2objdump(map, sym->end), path);
923c42c1
MG
224
225 file = popen(command, "r");
226 if (!file)
b0a9ab62 227 return -1;
923c42c1 228
b269876c
ACM
229 pthread_mutex_lock(&source->lock);
230 source->lines_tail = &source->lines;
923c42c1
MG
231 while (!feof(file)) {
232 struct source_line *src;
233 size_t dummy = 0;
ee11b90b 234 char *c, *sep;
923c42c1
MG
235
236 src = malloc(sizeof(struct source_line));
237 assert(src != NULL);
238 memset(src, 0, sizeof(struct source_line));
239
240 if (getline(&src->line, &dummy, file) < 0)
241 break;
242 if (!src->line)
243 break;
244
245 c = strchr(src->line, '\n');
246 if (c)
247 *c = 0;
248
249 src->next = NULL;
b269876c
ACM
250 *source->lines_tail = src;
251 source->lines_tail = &src->next;
923c42c1 252
ee11b90b
KS
253 src->eip = strtoull(src->line, &sep, 16);
254 if (*sep == ':')
255 src->eip = map__objdump_2ip(map, src->eip);
256 else /* this line has no ip info (e.g. source line) */
257 src->eip = 0;
923c42c1
MG
258 }
259 pclose(file);
260out_assign:
261 sym_filter_entry = syme;
b269876c 262 pthread_mutex_unlock(&source->lock);
b0a9ab62 263 return 0;
923c42c1
MG
264}
265
266static void __zero_source_counters(struct sym_entry *syme)
267{
268 int i;
269 struct source_line *line;
270
b269876c 271 line = syme->src->lines;
923c42c1 272 while (line) {
361c99a6 273 for (i = 0; i < evsel_list->nr_entries; i++)
923c42c1
MG
274 line->count[i] = 0;
275 line = line->next;
276 }
277}
278
279static void record_precise_ip(struct sym_entry *syme, int counter, u64 ip)
280{
281 struct source_line *line;
282
283 if (syme != sym_filter_entry)
284 return;
285
b269876c 286 if (pthread_mutex_trylock(&syme->src->lock))
923c42c1
MG
287 return;
288
b269876c 289 if (syme->src == NULL || syme->src->source == NULL)
923c42c1
MG
290 goto out_unlock;
291
b269876c 292 for (line = syme->src->lines; line; line = line->next) {
ee11b90b
KS
293 /* skip lines without IP info */
294 if (line->eip == 0)
295 continue;
923c42c1
MG
296 if (line->eip == ip) {
297 line->count[counter]++;
298 break;
299 }
300 if (line->eip > ip)
301 break;
302 }
303out_unlock:
b269876c 304 pthread_mutex_unlock(&syme->src->lock);
923c42c1
MG
305}
306
c7ad21af
ACM
307#define PATTERN_LEN (BITS_PER_LONG / 4 + 2)
308
923c42c1
MG
309static void lookup_sym_source(struct sym_entry *syme)
310{
51a472de 311 struct symbol *symbol = sym_entry__symbol(syme);
923c42c1 312 struct source_line *line;
c7ad21af 313 char pattern[PATTERN_LEN + 1];
923c42c1 314
9486aa38 315 sprintf(pattern, "%0*" PRIx64 " <", BITS_PER_LONG / 4,
5f485364 316 map__rip_2objdump(syme->map, symbol->start));
923c42c1 317
b269876c
ACM
318 pthread_mutex_lock(&syme->src->lock);
319 for (line = syme->src->lines; line; line = line->next) {
c7ad21af 320 if (memcmp(line->line, pattern, PATTERN_LEN) == 0) {
b269876c 321 syme->src->source = line;
923c42c1
MG
322 break;
323 }
324 }
b269876c 325 pthread_mutex_unlock(&syme->src->lock);
923c42c1
MG
326}
327
328static void show_lines(struct source_line *queue, int count, int total)
329{
330 int i;
331 struct source_line *line;
332
333 line = queue;
334 for (i = 0; i < count; i++) {
335 float pcnt = 100.0*(float)line->count[sym_counter]/(float)total;
336
337 printf("%8li %4.1f%%\t%s\n", line->count[sym_counter], pcnt, line->line);
338 line = line->next;
339 }
340}
341
342#define TRACE_COUNT 3
343
344static void show_details(struct sym_entry *syme)
345{
346 struct symbol *symbol;
347 struct source_line *line;
348 struct source_line *line_queue = NULL;
349 int displayed = 0;
350 int line_queue_count = 0, total = 0, more = 0;
351
352 if (!syme)
353 return;
354
b269876c 355 if (!syme->src->source)
923c42c1
MG
356 lookup_sym_source(syme);
357
b269876c 358 if (!syme->src->source)
923c42c1
MG
359 return;
360
51a472de 361 symbol = sym_entry__symbol(syme);
69aad6f1 362 printf("Showing %s for %s\n", event_name(sym_evsel), symbol->name);
923c42c1
MG
363 printf(" Events Pcnt (>=%d%%)\n", sym_pcnt_filter);
364
b269876c
ACM
365 pthread_mutex_lock(&syme->src->lock);
366 line = syme->src->source;
923c42c1
MG
367 while (line) {
368 total += line->count[sym_counter];
369 line = line->next;
370 }
371
b269876c 372 line = syme->src->source;
923c42c1
MG
373 while (line) {
374 float pcnt = 0.0;
375
376 if (!line_queue_count)
377 line_queue = line;
378 line_queue_count++;
379
380 if (line->count[sym_counter])
381 pcnt = 100.0 * line->count[sym_counter] / (float)total;
382 if (pcnt >= (float)sym_pcnt_filter) {
383 if (displayed <= print_entries)
384 show_lines(line_queue, line_queue_count, total);
385 else more++;
386 displayed += line_queue_count;
387 line_queue_count = 0;
388 line_queue = NULL;
389 } else if (line_queue_count > TRACE_COUNT) {
390 line_queue = line_queue->next;
391 line_queue_count--;
392 }
393
394 line->count[sym_counter] = zero ? 0 : line->count[sym_counter] * 7 / 8;
395 line = line->next;
396 }
b269876c 397 pthread_mutex_unlock(&syme->src->lock);
923c42c1
MG
398 if (more)
399 printf("%d lines not displayed, maybe increase display entries [e]\n", more);
400}
07800601 401
de04687f 402/*
5b2bb75a 403 * Symbols will be added here in event__process_sample and will get out
de04687f
ACM
404 * after decayed.
405 */
406static LIST_HEAD(active_symbols);
c44613a4 407static pthread_mutex_t active_symbols_lock = PTHREAD_MUTEX_INITIALIZER;
07800601 408
07800601
IM
409/*
410 * Ordering weight: count-1 * count-2 * ... / count-n
411 */
412static double sym_weight(const struct sym_entry *sym)
413{
c44613a4 414 double weight = sym->snap_count;
07800601
IM
415 int counter;
416
46ab9764
MG
417 if (!display_weighted)
418 return weight;
419
361c99a6 420 for (counter = 1; counter < evsel_list->nr_entries - 1; counter++)
07800601
IM
421 weight *= sym->count[counter];
422
423 weight /= (sym->count[counter] + 1);
424
425 return weight;
426}
427
2debbc83 428static long samples;
a1645ce1 429static long kernel_samples, us_samples;
1676b8a0 430static long exact_samples;
a1645ce1 431static long guest_us_samples, guest_kernel_samples;
07800601
IM
432static const char CONSOLE_CLEAR[] = "\e[H\e[2J";
433
c44613a4 434static void __list_insert_active_sym(struct sym_entry *syme)
de04687f
ACM
435{
436 list_add(&syme->node, &active_symbols);
437}
438
c44613a4
ACM
439static void list_remove_active_sym(struct sym_entry *syme)
440{
441 pthread_mutex_lock(&active_symbols_lock);
442 list_del_init(&syme->node);
443 pthread_mutex_unlock(&active_symbols_lock);
444}
445
de04687f
ACM
446static void rb_insert_active_sym(struct rb_root *tree, struct sym_entry *se)
447{
448 struct rb_node **p = &tree->rb_node;
449 struct rb_node *parent = NULL;
450 struct sym_entry *iter;
451
452 while (*p != NULL) {
453 parent = *p;
454 iter = rb_entry(parent, struct sym_entry, rb_node);
455
c44613a4 456 if (se->weight > iter->weight)
de04687f
ACM
457 p = &(*p)->rb_left;
458 else
459 p = &(*p)->rb_right;
460 }
461
462 rb_link_node(&se->rb_node, parent, p);
463 rb_insert_color(&se->rb_node, tree);
464}
07800601
IM
465
466static void print_sym_table(void)
467{
233f0b95 468 int printed = 0, j;
69aad6f1
ACM
469 struct perf_evsel *counter;
470 int snap = !display_weighted ? sym_counter : 0;
2debbc83 471 float samples_per_sec = samples/delay_secs;
a1645ce1
ZY
472 float ksamples_per_sec = kernel_samples/delay_secs;
473 float us_samples_per_sec = (us_samples)/delay_secs;
474 float guest_kernel_samples_per_sec = (guest_kernel_samples)/delay_secs;
475 float guest_us_samples_per_sec = (guest_us_samples)/delay_secs;
1676b8a0 476 float esamples_percent = (100.0*exact_samples)/samples;
2debbc83 477 float sum_ksamples = 0.0;
de04687f
ACM
478 struct sym_entry *syme, *n;
479 struct rb_root tmp = RB_ROOT;
480 struct rb_node *nd;
00909e95 481 int sym_width = 0, dso_width = 0, dso_short_width = 0;
13cc5079 482 const int win_width = winsize.ws_col - 1;
07800601 483
a1645ce1
ZY
484 samples = us_samples = kernel_samples = exact_samples = 0;
485 guest_kernel_samples = guest_us_samples = 0;
07800601 486
de04687f 487 /* Sort the active symbols */
c44613a4
ACM
488 pthread_mutex_lock(&active_symbols_lock);
489 syme = list_entry(active_symbols.next, struct sym_entry, node);
490 pthread_mutex_unlock(&active_symbols_lock);
491
492 list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
46ab9764 493 syme->snap_count = syme->count[snap];
c44613a4 494 if (syme->snap_count != 0) {
13cc5079 495
8ffcda17
ACM
496 if ((hide_user_symbols &&
497 syme->origin == PERF_RECORD_MISC_USER) ||
498 (hide_kernel_symbols &&
499 syme->origin == PERF_RECORD_MISC_KERNEL)) {
500 list_remove_active_sym(syme);
501 continue;
502 }
c44613a4 503 syme->weight = sym_weight(syme);
de04687f 504 rb_insert_active_sym(&tmp, syme);
2debbc83 505 sum_ksamples += syme->snap_count;
d94b9430 506
361c99a6 507 for (j = 0; j < evsel_list->nr_entries; j++)
de04687f
ACM
508 syme->count[j] = zero ? 0 : syme->count[j] * 7 / 8;
509 } else
c44613a4 510 list_remove_active_sym(syme);
d94b9430
MG
511 }
512
0f5486b5 513 puts(CONSOLE_CLEAR);
07800601 514
13cc5079 515 printf("%-*.*s\n", win_width, win_width, graph_dotted_line);
a1645ce1
ZY
516 if (!perf_guest) {
517 printf(" PerfTop:%8.0f irqs/sec kernel:%4.1f%%"
518 " exact: %4.1f%% [",
519 samples_per_sec,
520 100.0 - (100.0 * ((samples_per_sec - ksamples_per_sec) /
521 samples_per_sec)),
522 esamples_percent);
523 } else {
524 printf(" PerfTop:%8.0f irqs/sec kernel:%4.1f%% us:%4.1f%%"
525 " guest kernel:%4.1f%% guest us:%4.1f%%"
526 " exact: %4.1f%% [",
527 samples_per_sec,
528 100.0 - (100.0 * ((samples_per_sec-ksamples_per_sec) /
529 samples_per_sec)),
530 100.0 - (100.0 * ((samples_per_sec-us_samples_per_sec) /
531 samples_per_sec)),
532 100.0 - (100.0 * ((samples_per_sec -
533 guest_kernel_samples_per_sec) /
534 samples_per_sec)),
535 100.0 - (100.0 * ((samples_per_sec -
536 guest_us_samples_per_sec) /
537 samples_per_sec)),
538 esamples_percent);
539 }
07800601 540
361c99a6 541 if (evsel_list->nr_entries == 1 || !display_weighted) {
69aad6f1 542 struct perf_evsel *first;
361c99a6 543 first = list_entry(evsel_list->entries.next, struct perf_evsel, node);
9486aa38 544 printf("%" PRIu64, (uint64_t)first->attr.sample_period);
cf1f4574
IM
545 if (freq)
546 printf("Hz ");
547 else
548 printf(" ");
549 }
07800601 550
46ab9764 551 if (!display_weighted)
69aad6f1 552 printf("%s", event_name(sym_evsel));
361c99a6 553 else list_for_each_entry(counter, &evsel_list->entries, node) {
69aad6f1 554 if (counter->idx)
07800601
IM
555 printf("/");
556
557 printf("%s", event_name(counter));
558 }
559
560 printf( "], ");
561
b456bae0
IM
562 if (target_pid != -1)
563 printf(" (target_pid: %d", target_pid);
d6d901c2
ZY
564 else if (target_tid != -1)
565 printf(" (target_tid: %d", target_tid);
07800601
IM
566 else
567 printf(" (all");
568
01797c59 569 if (cpu_list)
60d567e2 570 printf(", CPU%s: %s)\n", cpus->nr > 1 ? "s" : "", cpu_list);
07800601 571 else {
d6d901c2 572 if (target_tid != -1)
07800601
IM
573 printf(")\n");
574 else
60d567e2 575 printf(", %d CPU%s)\n", cpus->nr, cpus->nr > 1 ? "s" : "");
07800601
IM
576 }
577
1a105f74 578 printf("%-*.*s\n", win_width, win_width, graph_dotted_line);
07800601 579
923c42c1
MG
580 if (sym_filter_entry) {
581 show_details(sym_filter_entry);
582 return;
583 }
584
13cc5079
ACM
585 /*
586 * Find the longest symbol name that will be displayed
587 */
588 for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
589 syme = rb_entry(nd, struct sym_entry, rb_node);
590 if (++printed > print_entries ||
591 (int)syme->snap_count < count_filter)
592 continue;
593
1a105f74
ACM
594 if (syme->map->dso->long_name_len > dso_width)
595 dso_width = syme->map->dso->long_name_len;
596
b63be8d7
ACM
597 if (syme->map->dso->short_name_len > dso_short_width)
598 dso_short_width = syme->map->dso->short_name_len;
599
13cc5079
ACM
600 if (syme->name_len > sym_width)
601 sym_width = syme->name_len;
602 }
603
604 printed = 0;
605
b63be8d7
ACM
606 if (sym_width + dso_width > winsize.ws_col - 29) {
607 dso_width = dso_short_width;
608 if (sym_width + dso_width > winsize.ws_col - 29)
609 sym_width = winsize.ws_col - dso_width - 29;
610 }
7cc017ed 611 putchar('\n');
361c99a6 612 if (evsel_list->nr_entries == 1)
5b2bb75a 613 printf(" samples pcnt");
07800601 614 else
5b2bb75a 615 printf(" weight samples pcnt");
07800601 616
7ced156b
ACM
617 if (verbose)
618 printf(" RIP ");
7cc017ed 619 printf(" %-*.*s DSO\n", sym_width, sym_width, "function");
5b2bb75a 620 printf(" %s _______ _____",
361c99a6 621 evsel_list->nr_entries == 1 ? " " : "______");
7ced156b 622 if (verbose)
5b2bb75a 623 printf(" ________________");
1a105f74 624 printf(" %-*.*s", sym_width, sym_width, graph_line);
7cc017ed 625 printf(" %-*.*s", dso_width, dso_width, graph_line);
1a105f74 626 puts("\n");
07800601 627
de04687f 628 for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
83a0944f 629 struct symbol *sym;
8fc0321f 630 double pcnt;
d94b9430 631
83a0944f 632 syme = rb_entry(nd, struct sym_entry, rb_node);
51a472de 633 sym = sym_entry__symbol(syme);
923c42c1 634 if (++printed > print_entries || (int)syme->snap_count < count_filter)
c44613a4 635 continue;
d94b9430 636
2debbc83
IM
637 pcnt = 100.0 - (100.0 * ((sum_ksamples - syme->snap_count) /
638 sum_ksamples));
d94b9430 639
361c99a6 640 if (evsel_list->nr_entries == 1 || !display_weighted)
5b2bb75a 641 printf("%20.2f ", syme->weight);
d94b9430 642 else
5b2bb75a 643 printf("%9.1f %10ld ", syme->weight, syme->snap_count);
8fc0321f 644
1e11fd82 645 percent_color_fprintf(stdout, "%4.1f%%", pcnt);
7ced156b 646 if (verbose)
9486aa38 647 printf(" %016" PRIx64, sym->start);
13cc5079 648 printf(" %-*.*s", sym_width, sym_width, sym->name);
7cc017ed
ACM
649 printf(" %-*.*s\n", dso_width, dso_width,
650 dso_width >= syme->map->dso->long_name_len ?
651 syme->map->dso->long_name :
652 syme->map->dso->short_name);
07800601 653 }
07800601
IM
654}
655
923c42c1
MG
656static void prompt_integer(int *target, const char *msg)
657{
658 char *buf = malloc(0), *p;
659 size_t dummy = 0;
660 int tmp;
661
662 fprintf(stdout, "\n%s: ", msg);
663 if (getline(&buf, &dummy, stdin) < 0)
664 return;
665
666 p = strchr(buf, '\n');
667 if (p)
668 *p = 0;
669
670 p = buf;
671 while(*p) {
672 if (!isdigit(*p))
673 goto out_free;
674 p++;
675 }
676 tmp = strtoul(buf, NULL, 10);
677 *target = tmp;
678out_free:
679 free(buf);
680}
681
682static void prompt_percent(int *target, const char *msg)
683{
684 int tmp = 0;
685
686 prompt_integer(&tmp, msg);
687 if (tmp >= 0 && tmp <= 100)
688 *target = tmp;
689}
690
691static void prompt_symbol(struct sym_entry **target, const char *msg)
692{
693 char *buf = malloc(0), *p;
694 struct sym_entry *syme = *target, *n, *found = NULL;
695 size_t dummy = 0;
696
697 /* zero counters of active symbol */
698 if (syme) {
b269876c 699 pthread_mutex_lock(&syme->src->lock);
923c42c1
MG
700 __zero_source_counters(syme);
701 *target = NULL;
b269876c 702 pthread_mutex_unlock(&syme->src->lock);
923c42c1
MG
703 }
704
705 fprintf(stdout, "\n%s: ", msg);
706 if (getline(&buf, &dummy, stdin) < 0)
707 goto out_free;
708
709 p = strchr(buf, '\n');
710 if (p)
711 *p = 0;
712
713 pthread_mutex_lock(&active_symbols_lock);
714 syme = list_entry(active_symbols.next, struct sym_entry, node);
715 pthread_mutex_unlock(&active_symbols_lock);
716
717 list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
51a472de 718 struct symbol *sym = sym_entry__symbol(syme);
923c42c1
MG
719
720 if (!strcmp(buf, sym->name)) {
721 found = syme;
722 break;
723 }
724 }
725
726 if (!found) {
66aeb6d5 727 fprintf(stderr, "Sorry, %s is not active.\n", buf);
923c42c1
MG
728 sleep(1);
729 return;
730 } else
731 parse_source(found);
732
733out_free:
734 free(buf);
735}
736
091bd2e9 737static void print_mapped_keys(void)
923c42c1 738{
091bd2e9
MG
739 char *name = NULL;
740
741 if (sym_filter_entry) {
51a472de 742 struct symbol *sym = sym_entry__symbol(sym_filter_entry);
091bd2e9
MG
743 name = sym->name;
744 }
745
746 fprintf(stdout, "\nMapped keys:\n");
747 fprintf(stdout, "\t[d] display refresh delay. \t(%d)\n", delay_secs);
748 fprintf(stdout, "\t[e] display entries (lines). \t(%d)\n", print_entries);
749
361c99a6 750 if (evsel_list->nr_entries > 1)
69aad6f1 751 fprintf(stdout, "\t[E] active event counter. \t(%s)\n", event_name(sym_evsel));
091bd2e9
MG
752
753 fprintf(stdout, "\t[f] profile display filter (count). \t(%d)\n", count_filter);
754
6cff0e8d
KS
755 fprintf(stdout, "\t[F] annotate display filter (percent). \t(%d%%)\n", sym_pcnt_filter);
756 fprintf(stdout, "\t[s] annotate symbol. \t(%s)\n", name?: "NULL");
757 fprintf(stdout, "\t[S] stop annotation.\n");
091bd2e9 758
361c99a6 759 if (evsel_list->nr_entries > 1)
091bd2e9
MG
760 fprintf(stdout, "\t[w] toggle display weighted/count[E]r. \t(%d)\n", display_weighted ? 1 : 0);
761
8ffcda17 762 fprintf(stdout,
1a72cfa6 763 "\t[K] hide kernel_symbols symbols. \t(%s)\n",
8ffcda17
ACM
764 hide_kernel_symbols ? "yes" : "no");
765 fprintf(stdout,
766 "\t[U] hide user symbols. \t(%s)\n",
767 hide_user_symbols ? "yes" : "no");
46ab9764 768 fprintf(stdout, "\t[z] toggle sample zeroing. \t(%d)\n", zero ? 1 : 0);
091bd2e9
MG
769 fprintf(stdout, "\t[qQ] quit.\n");
770}
771
772static int key_mapped(int c)
773{
774 switch (c) {
775 case 'd':
776 case 'e':
777 case 'f':
778 case 'z':
779 case 'q':
780 case 'Q':
8ffcda17
ACM
781 case 'K':
782 case 'U':
6cff0e8d
KS
783 case 'F':
784 case 's':
785 case 'S':
091bd2e9
MG
786 return 1;
787 case 'E':
788 case 'w':
361c99a6 789 return evsel_list->nr_entries > 1 ? 1 : 0;
83a0944f
IM
790 default:
791 break;
091bd2e9
MG
792 }
793
794 return 0;
923c42c1
MG
795}
796
a1645ce1 797static void handle_keypress(struct perf_session *session, int c)
923c42c1 798{
091bd2e9
MG
799 if (!key_mapped(c)) {
800 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
801 struct termios tc, save;
802
803 print_mapped_keys();
804 fprintf(stdout, "\nEnter selection, or unmapped key to continue: ");
805 fflush(stdout);
806
807 tcgetattr(0, &save);
808 tc = save;
809 tc.c_lflag &= ~(ICANON | ECHO);
810 tc.c_cc[VMIN] = 0;
811 tc.c_cc[VTIME] = 0;
812 tcsetattr(0, TCSANOW, &tc);
813
814 poll(&stdin_poll, 1, -1);
815 c = getc(stdin);
816
817 tcsetattr(0, TCSAFLUSH, &save);
818 if (!key_mapped(c))
819 return;
820 }
821
923c42c1
MG
822 switch (c) {
823 case 'd':
824 prompt_integer(&delay_secs, "Enter display delay");
dc79959a
TB
825 if (delay_secs < 1)
826 delay_secs = 1;
923c42c1
MG
827 break;
828 case 'e':
829 prompt_integer(&print_entries, "Enter display entries (lines)");
3b6ed988 830 if (print_entries == 0) {
13cc5079 831 sig_winch_handler(SIGWINCH);
3b6ed988
ACM
832 signal(SIGWINCH, sig_winch_handler);
833 } else
834 signal(SIGWINCH, SIG_DFL);
923c42c1
MG
835 break;
836 case 'E':
361c99a6 837 if (evsel_list->nr_entries > 1) {
923c42c1 838 fprintf(stderr, "\nAvailable events:");
69aad6f1 839
361c99a6 840 list_for_each_entry(sym_evsel, &evsel_list->entries, node)
69aad6f1 841 fprintf(stderr, "\n\t%d %s", sym_evsel->idx, event_name(sym_evsel));
923c42c1
MG
842
843 prompt_integer(&sym_counter, "Enter details event counter");
844
361c99a6
ACM
845 if (sym_counter >= evsel_list->nr_entries) {
846 sym_evsel = list_entry(evsel_list->entries.next, struct perf_evsel, node);
923c42c1 847 sym_counter = 0;
69aad6f1 848 fprintf(stderr, "Sorry, no such event, using %s.\n", event_name(sym_evsel));
923c42c1 849 sleep(1);
69aad6f1 850 break;
923c42c1 851 }
361c99a6 852 list_for_each_entry(sym_evsel, &evsel_list->entries, node)
69aad6f1
ACM
853 if (sym_evsel->idx == sym_counter)
854 break;
923c42c1
MG
855 } else sym_counter = 0;
856 break;
857 case 'f':
858 prompt_integer(&count_filter, "Enter display event count filter");
859 break;
860 case 'F':
861 prompt_percent(&sym_pcnt_filter, "Enter details display event filter (percent)");
862 break;
8ffcda17
ACM
863 case 'K':
864 hide_kernel_symbols = !hide_kernel_symbols;
865 break;
923c42c1
MG
866 case 'q':
867 case 'Q':
868 printf("exiting.\n");
c338aee8 869 if (dump_symtab)
cbf69680 870 perf_session__fprintf_dsos(session, stderr);
923c42c1
MG
871 exit(0);
872 case 's':
873 prompt_symbol(&sym_filter_entry, "Enter details symbol");
874 break;
875 case 'S':
876 if (!sym_filter_entry)
877 break;
878 else {
879 struct sym_entry *syme = sym_filter_entry;
880
b269876c 881 pthread_mutex_lock(&syme->src->lock);
923c42c1
MG
882 sym_filter_entry = NULL;
883 __zero_source_counters(syme);
b269876c 884 pthread_mutex_unlock(&syme->src->lock);
923c42c1
MG
885 }
886 break;
8ffcda17
ACM
887 case 'U':
888 hide_user_symbols = !hide_user_symbols;
889 break;
46ab9764
MG
890 case 'w':
891 display_weighted = ~display_weighted;
892 break;
923c42c1 893 case 'z':
c0555642 894 zero = !zero;
923c42c1 895 break;
83a0944f
IM
896 default:
897 break;
923c42c1
MG
898 }
899}
900
f37a291c 901static void *display_thread(void *arg __used)
07800601 902{
0f5486b5 903 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
923c42c1
MG
904 struct termios tc, save;
905 int delay_msecs, c;
a1645ce1 906 struct perf_session *session = (struct perf_session *) arg;
923c42c1
MG
907
908 tcgetattr(0, &save);
909 tc = save;
910 tc.c_lflag &= ~(ICANON | ECHO);
911 tc.c_cc[VMIN] = 0;
912 tc.c_cc[VTIME] = 0;
091bd2e9 913
923c42c1
MG
914repeat:
915 delay_msecs = delay_secs * 1000;
916 tcsetattr(0, TCSANOW, &tc);
917 /* trash return*/
918 getc(stdin);
07800601 919
0f5486b5 920 do {
07800601 921 print_sym_table();
0f5486b5
FW
922 } while (!poll(&stdin_poll, 1, delay_msecs) == 1);
923
923c42c1
MG
924 c = getc(stdin);
925 tcsetattr(0, TCSAFLUSH, &save);
926
a1645ce1 927 handle_keypress(session, c);
923c42c1 928 goto repeat;
07800601
IM
929
930 return NULL;
931}
932
2ab52083 933/* Tag samples to be skipped. */
f37a291c 934static const char *skip_symbols[] = {
2ab52083
AB
935 "default_idle",
936 "cpu_idle",
937 "enter_idle",
938 "exit_idle",
939 "mwait_idle",
59b90056 940 "mwait_idle_with_hints",
8357275b 941 "poll_idle",
3a3393ef
AB
942 "ppc64_runlatch_off",
943 "pseries_dedicated_idle_sleep",
2ab52083
AB
944 NULL
945};
946
439d473b 947static int symbol_filter(struct map *map, struct symbol *sym)
07800601 948{
de04687f
ACM
949 struct sym_entry *syme;
950 const char *name = sym->name;
2ab52083 951 int i;
de04687f 952
3a3393ef
AB
953 /*
954 * ppc64 uses function descriptors and appends a '.' to the
955 * start of every instruction address. Remove it.
956 */
957 if (name[0] == '.')
958 name++;
959
de04687f
ACM
960 if (!strcmp(name, "_text") ||
961 !strcmp(name, "_etext") ||
962 !strcmp(name, "_sinittext") ||
963 !strncmp("init_module", name, 11) ||
964 !strncmp("cleanup_module", name, 14) ||
965 strstr(name, "_text_start") ||
966 strstr(name, "_text_end"))
07800601 967 return 1;
07800601 968
00a192b3 969 syme = symbol__priv(sym);
439d473b 970 syme->map = map;
b269876c 971 syme->src = NULL;
6cff0e8d
KS
972
973 if (!sym_filter_entry && sym_filter && !strcmp(name, sym_filter)) {
974 /* schedule initial sym_filter_entry setup */
975 sym_filter_entry_sched = syme;
976 sym_filter = NULL;
977 }
923c42c1 978
2ab52083
AB
979 for (i = 0; skip_symbols[i]; i++) {
980 if (!strcmp(skip_symbols[i], name)) {
981 syme->skip = 1;
982 break;
983 }
984 }
07800601 985
13cc5079
ACM
986 if (!syme->skip)
987 syme->name_len = strlen(sym->name);
988
07800601
IM
989 return 0;
990}
991
b3165f41 992static void event__process_sample(const event_t *self,
9c90a61c 993 struct sample_data *sample,
69aad6f1
ACM
994 struct perf_session *session,
995 struct perf_evsel *evsel)
07800601 996{
5b2bb75a 997 u64 ip = self->ip.ip;
5b2bb75a 998 struct sym_entry *syme;
1ed091c4 999 struct addr_location al;
23346f21 1000 struct machine *machine;
8ffcda17 1001 u8 origin = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
5b2bb75a 1002
24bfef0f
ACM
1003 ++samples;
1004
8ffcda17 1005 switch (origin) {
1ed091c4 1006 case PERF_RECORD_MISC_USER:
a1645ce1 1007 ++us_samples;
8ffcda17
ACM
1008 if (hide_user_symbols)
1009 return;
23346f21 1010 machine = perf_session__find_host_machine(session);
1ed091c4 1011 break;
5b2bb75a 1012 case PERF_RECORD_MISC_KERNEL:
a1645ce1 1013 ++kernel_samples;
8ffcda17
ACM
1014 if (hide_kernel_symbols)
1015 return;
23346f21 1016 machine = perf_session__find_host_machine(session);
a1645ce1
ZY
1017 break;
1018 case PERF_RECORD_MISC_GUEST_KERNEL:
1019 ++guest_kernel_samples;
23346f21 1020 machine = perf_session__find_machine(session, self->ip.pid);
5b2bb75a 1021 break;
a1645ce1
ZY
1022 case PERF_RECORD_MISC_GUEST_USER:
1023 ++guest_us_samples;
1024 /*
1025 * TODO: we don't process guest user from host side
1026 * except simple counting.
1027 */
1028 return;
5b2bb75a
ACM
1029 default:
1030 return;
1031 }
1032
23346f21 1033 if (!machine && perf_guest) {
a1645ce1
ZY
1034 pr_err("Can't find guest [%d]'s kernel information\n",
1035 self->ip.pid);
1036 return;
1037 }
1038
ab608344 1039 if (self->header.misc & PERF_RECORD_MISC_EXACT_IP)
1676b8a0
PZ
1040 exact_samples++;
1041
9c90a61c 1042 if (event__preprocess_sample(self, session, &al, sample,
41a37e20 1043 symbol_filter) < 0 ||
72b8fa17 1044 al.filtered)
1ed091c4 1045 return;
07800601 1046
72b8fa17
ACM
1047 if (al.sym == NULL) {
1048 /*
1049 * As we do lazy loading of symtabs we only will know if the
1050 * specified vmlinux file is invalid when we actually have a
1051 * hit in kernel space and then try to load it. So if we get
1052 * here and there are _no_ symbols in the DSO backing the
1053 * kernel map, bail out.
1054 *
1055 * We may never get here, for instance, if we use -K/
1056 * --hide-kernel-symbols, even if the user specifies an
1057 * invalid --vmlinux ;-)
1058 */
23346f21 1059 if (al.map == machine->vmlinux_maps[MAP__FUNCTION] &&
72b8fa17
ACM
1060 RB_EMPTY_ROOT(&al.map->dso->symbols[MAP__FUNCTION])) {
1061 pr_err("The %s file can't be used\n",
1062 symbol_conf.vmlinux_name);
1063 exit(1);
1064 }
1065
1066 return;
1067 }
1068
6cff0e8d
KS
1069 /* let's see, whether we need to install initial sym_filter_entry */
1070 if (sym_filter_entry_sched) {
1071 sym_filter_entry = sym_filter_entry_sched;
1072 sym_filter_entry_sched = NULL;
b0a9ab62
ACM
1073 if (parse_source(sym_filter_entry) < 0) {
1074 struct symbol *sym = sym_entry__symbol(sym_filter_entry);
1075
1076 pr_err("Can't annotate %s", sym->name);
1077 if (sym_filter_entry->map->dso->origin == DSO__ORIG_KERNEL) {
1078 pr_err(": No vmlinux file was found in the path:\n");
5ad90e4e 1079 machine__fprintf_vmlinux_path(machine, stderr);
b0a9ab62
ACM
1080 } else
1081 pr_err(".\n");
1082 exit(1);
1083 }
6cff0e8d
KS
1084 }
1085
1ed091c4 1086 syme = symbol__priv(al.sym);
5b2bb75a 1087 if (!syme->skip) {
69aad6f1 1088 syme->count[evsel->idx]++;
8ffcda17 1089 syme->origin = origin;
69aad6f1 1090 record_precise_ip(syme, evsel->idx, ip);
5b2bb75a
ACM
1091 pthread_mutex_lock(&active_symbols_lock);
1092 if (list_empty(&syme->node) || !syme->node.next)
1093 __list_insert_active_sym(syme);
1094 pthread_mutex_unlock(&active_symbols_lock);
5b2bb75a 1095 }
07800601
IM
1096}
1097
07800601 1098struct mmap_data {
a21ca2ca 1099 void *base;
f37a291c 1100 int mask;
a21ca2ca 1101 unsigned int prev;
07800601
IM
1102};
1103
69aad6f1
ACM
1104static int perf_evsel__alloc_mmap_per_thread(struct perf_evsel *evsel,
1105 int ncpus, int nthreads)
1106{
1107 evsel->priv = xyarray__new(ncpus, nthreads, sizeof(struct mmap_data));
1108 return evsel->priv != NULL ? 0 : -ENOMEM;
1109}
1110
1111static void perf_evsel__free_mmap(struct perf_evsel *evsel)
1112{
1113 xyarray__delete(evsel->priv);
1114 evsel->priv = NULL;
1115}
1116
07800601
IM
1117static unsigned int mmap_read_head(struct mmap_data *md)
1118{
cdd6c482 1119 struct perf_event_mmap_page *pc = md->base;
07800601
IM
1120 int head;
1121
1122 head = pc->data_head;
1123 rmb();
1124
1125 return head;
1126}
1127
d8f66248 1128static void perf_session__mmap_read_counter(struct perf_session *self,
69aad6f1
ACM
1129 struct perf_evsel *evsel,
1130 int cpu, int thread_idx)
07800601 1131{
69aad6f1
ACM
1132 struct xyarray *mmap_array = evsel->priv;
1133 struct mmap_data *md = xyarray__entry(mmap_array, cpu, thread_idx);
07800601
IM
1134 unsigned int head = mmap_read_head(md);
1135 unsigned int old = md->prev;
1136 unsigned char *data = md->base + page_size;
9c90a61c 1137 struct sample_data sample;
07800601
IM
1138 int diff;
1139
07800601
IM
1140 /*
1141 * If we're further behind than half the buffer, there's a chance
2debbc83 1142 * the writer will bite our tail and mess up the samples under us.
07800601
IM
1143 *
1144 * If we somehow ended up ahead of the head, we got messed up.
1145 *
1146 * In either case, truncate and restart at head.
1147 */
1148 diff = head - old;
1149 if (diff > md->mask / 2 || diff < 0) {
f4f0b418 1150 fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
07800601
IM
1151
1152 /*
1153 * head points to a known good entry, start there.
1154 */
1155 old = head;
1156 }
1157
07800601 1158 for (; old != head;) {
07800601
IM
1159 event_t *event = (event_t *)&data[old & md->mask];
1160
1161 event_t event_copy;
1162
6f06ccbc 1163 size_t size = event->header.size;
07800601
IM
1164
1165 /*
1166 * Event straddles the mmap boundary -- header should always
1167 * be inside due to u64 alignment of output.
1168 */
1169 if ((old & md->mask) + size != ((old + size) & md->mask)) {
1170 unsigned int offset = old;
1171 unsigned int len = min(sizeof(*event), size), cpy;
1172 void *dst = &event_copy;
1173
1174 do {
1175 cpy = min(md->mask + 1 - (offset & md->mask), len);
1176 memcpy(dst, &data[offset & md->mask], cpy);
1177 offset += cpy;
1178 dst += cpy;
1179 len -= cpy;
1180 } while (len);
1181
1182 event = &event_copy;
1183 }
1184
9c90a61c 1185 event__parse_sample(event, self, &sample);
5b2bb75a 1186 if (event->header.type == PERF_RECORD_SAMPLE)
69aad6f1 1187 event__process_sample(event, &sample, self, evsel);
5b2bb75a 1188 else
9c90a61c 1189 event__process(event, &sample, self);
07800601 1190 old += size;
07800601
IM
1191 }
1192
1193 md->prev = old;
1194}
1195
d6d901c2 1196static struct pollfd *event_array;
c2990a2a 1197
d8f66248 1198static void perf_session__mmap_read(struct perf_session *self)
2f01190a 1199{
69aad6f1
ACM
1200 struct perf_evsel *counter;
1201 int i, thread_index;
2f01190a 1202
60d567e2 1203 for (i = 0; i < cpus->nr; i++) {
361c99a6 1204 list_for_each_entry(counter, &evsel_list->entries, node) {
d6d901c2 1205 for (thread_index = 0;
5c98d466 1206 thread_index < threads->nr;
d6d901c2
ZY
1207 thread_index++) {
1208 perf_session__mmap_read_counter(self,
69aad6f1 1209 counter, i, thread_index);
d6d901c2 1210 }
69aad6f1 1211 }
2f01190a
FW
1212 }
1213}
1214
716c69fe
IM
1215int nr_poll;
1216int group_fd;
1217
69aad6f1 1218static void start_counter(int i, struct perf_evsel *evsel)
07800601 1219{
69aad6f1
ACM
1220 struct xyarray *mmap_array = evsel->priv;
1221 struct mmap_data *mm;
cdd6c482 1222 struct perf_event_attr *attr;
01797c59 1223 int cpu = -1;
d6d901c2 1224 int thread_index;
716c69fe 1225
01797c59 1226 if (target_tid == -1)
60d567e2 1227 cpu = cpus->map[i];
716c69fe 1228
69aad6f1 1229 attr = &evsel->attr;
716c69fe
IM
1230
1231 attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
7e4ff9e3
MG
1232
1233 if (freq) {
1234 attr->sample_type |= PERF_SAMPLE_PERIOD;
1235 attr->freq = 1;
1236 attr->sample_freq = freq;
1237 }
1238
0fdc7e67 1239 attr->inherit = (cpu < 0) && inherit;
5b2bb75a 1240 attr->mmap = 1;
716c69fe 1241
5c98d466 1242 for (thread_index = 0; thread_index < threads->nr; thread_index++) {
716c69fe 1243try_again:
69aad6f1 1244 FD(evsel, i, thread_index) = sys_perf_event_open(attr,
5c98d466 1245 threads->map[thread_index], cpu, group_fd, 0);
d6d901c2 1246
69aad6f1 1247 if (FD(evsel, i, thread_index) < 0) {
d6d901c2
ZY
1248 int err = errno;
1249
1250 if (err == EPERM || err == EACCES)
d9cf837e
CA
1251 die("Permission error - are you root?\n"
1252 "\t Consider tweaking"
1253 " /proc/sys/kernel/perf_event_paranoid.\n");
d6d901c2
ZY
1254 /*
1255 * If it's cycles then fall back to hrtimer
1256 * based cpu-clock-tick sw counter, which
1257 * is always available even if no PMU support:
1258 */
1259 if (attr->type == PERF_TYPE_HARDWARE
1260 && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
1261
1262 if (verbose)
1263 warning(" ... trying to fall back to cpu-clock-ticks\n");
1264
1265 attr->type = PERF_TYPE_SOFTWARE;
1266 attr->config = PERF_COUNT_SW_CPU_CLOCK;
1267 goto try_again;
1268 }
1269 printf("\n");
d9cf837e 1270 error("sys_perf_event_open() syscall returned with %d (%s). /bin/dmesg may provide additional information.\n",
69aad6f1 1271 FD(evsel, i, thread_index), strerror(err));
d6d901c2
ZY
1272 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
1273 exit(-1);
1274 }
69aad6f1
ACM
1275 assert(FD(evsel, i, thread_index) >= 0);
1276 fcntl(FD(evsel, i, thread_index), F_SETFL, O_NONBLOCK);
716c69fe 1277
716c69fe 1278 /*
d6d901c2 1279 * First counter acts as the group leader:
716c69fe 1280 */
d6d901c2 1281 if (group && group_fd == -1)
69aad6f1 1282 group_fd = FD(evsel, i, thread_index);
d6d901c2 1283
69aad6f1 1284 event_array[nr_poll].fd = FD(evsel, i, thread_index);
d6d901c2
ZY
1285 event_array[nr_poll].events = POLLIN;
1286 nr_poll++;
1287
69aad6f1
ACM
1288 mm = xyarray__entry(mmap_array, i, thread_index);
1289 mm->prev = 0;
1290 mm->mask = mmap_pages*page_size - 1;
1291 mm->base = mmap(NULL, (mmap_pages+1)*page_size,
1292 PROT_READ, MAP_SHARED, FD(evsel, i, thread_index), 0);
1293 if (mm->base == MAP_FAILED)
d6d901c2 1294 die("failed to mmap with %d (%s)\n", errno, strerror(errno));
716c69fe 1295 }
716c69fe
IM
1296}
1297
1298static int __cmd_top(void)
1299{
1300 pthread_t thread;
69aad6f1
ACM
1301 struct perf_evsel *counter;
1302 int i, ret;
d8f66248 1303 /*
b3165f41
ACM
1304 * FIXME: perf_session__new should allow passing a O_MMAP, so that all this
1305 * mmap reading, etc is encapsulated in it. Use O_WRONLY for now.
d8f66248 1306 */
21ef97f0 1307 struct perf_session *session = perf_session__new(NULL, O_WRONLY, false, false, NULL);
b3165f41
ACM
1308 if (session == NULL)
1309 return -ENOMEM;
07800601 1310
d6d901c2
ZY
1311 if (target_tid != -1)
1312 event__synthesize_thread(target_tid, event__process, session);
5b2bb75a 1313 else
d8f66248 1314 event__synthesize_threads(event__process, session);
5b2bb75a 1315
60d567e2 1316 for (i = 0; i < cpus->nr; i++) {
07800601 1317 group_fd = -1;
361c99a6 1318 list_for_each_entry(counter, &evsel_list->entries, node)
716c69fe 1319 start_counter(i, counter);
07800601
IM
1320 }
1321
2f01190a 1322 /* Wait for a minimal set of events before starting the snapshot */
d6d901c2 1323 poll(&event_array[0], nr_poll, 100);
2f01190a 1324
d8f66248 1325 perf_session__mmap_read(session);
2f01190a 1326
a1645ce1 1327 if (pthread_create(&thread, NULL, display_thread, session)) {
07800601
IM
1328 printf("Could not create display thread.\n");
1329 exit(-1);
1330 }
1331
1332 if (realtime_prio) {
1333 struct sched_param param;
1334
1335 param.sched_priority = realtime_prio;
1336 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
1337 printf("Could not set realtime priority.\n");
1338 exit(-1);
1339 }
1340 }
1341
1342 while (1) {
2debbc83 1343 int hits = samples;
07800601 1344
d8f66248 1345 perf_session__mmap_read(session);
07800601 1346
2debbc83 1347 if (hits == samples)
07800601
IM
1348 ret = poll(event_array, nr_poll, 100);
1349 }
1350
1351 return 0;
1352}
b456bae0
IM
1353
1354static const char * const top_usage[] = {
1355 "perf top [<options>]",
1356 NULL
1357};
1358
b456bae0 1359static const struct option options[] = {
361c99a6 1360 OPT_CALLBACK('e', "event", &evsel_list, "event",
86847b62
TG
1361 "event selector. use 'perf list' to list available events",
1362 parse_events),
b456bae0
IM
1363 OPT_INTEGER('c', "count", &default_interval,
1364 "event period to sample"),
1365 OPT_INTEGER('p', "pid", &target_pid,
d6d901c2
ZY
1366 "profile events on existing process id"),
1367 OPT_INTEGER('t', "tid", &target_tid,
1368 "profile events on existing thread id"),
b456bae0
IM
1369 OPT_BOOLEAN('a', "all-cpus", &system_wide,
1370 "system-wide collection from all CPUs"),
c45c6ea2
SE
1371 OPT_STRING('C', "cpu", &cpu_list, "cpu",
1372 "list of cpus to monitor"),
b32d133a
ACM
1373 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1374 "file", "vmlinux pathname"),
8ffcda17
ACM
1375 OPT_BOOLEAN('K', "hide_kernel_symbols", &hide_kernel_symbols,
1376 "hide kernel symbols"),
1967936d 1377 OPT_UINTEGER('m', "mmap-pages", &mmap_pages, "number of mmap data pages"),
b456bae0
IM
1378 OPT_INTEGER('r', "realtime", &realtime_prio,
1379 "collect data with this RT SCHED_FIFO priority"),
db20c003 1380 OPT_INTEGER('d', "delay", &delay_secs,
b456bae0
IM
1381 "number of seconds to delay between refreshes"),
1382 OPT_BOOLEAN('D', "dump-symtab", &dump_symtab,
1383 "dump the symbol table used for profiling"),
6e53cdf1 1384 OPT_INTEGER('f', "count-filter", &count_filter,
b456bae0
IM
1385 "only display functions with more events than this"),
1386 OPT_BOOLEAN('g', "group", &group,
1387 "put the counters into a counter group"),
0fdc7e67
MG
1388 OPT_BOOLEAN('i', "inherit", &inherit,
1389 "child tasks inherit counters"),
923c42c1 1390 OPT_STRING('s', "sym-annotate", &sym_filter, "symbol name",
6cff0e8d 1391 "symbol to annotate"),
1f208ea6 1392 OPT_BOOLEAN('z', "zero", &zero,
b456bae0 1393 "zero history across updates"),
6e53cdf1 1394 OPT_INTEGER('F', "freq", &freq,
b456bae0 1395 "profile at this frequency"),
6e53cdf1
IM
1396 OPT_INTEGER('E', "entries", &print_entries,
1397 "display this many functions"),
8ffcda17
ACM
1398 OPT_BOOLEAN('U', "hide_user_symbols", &hide_user_symbols,
1399 "hide user symbols"),
c0555642 1400 OPT_INCR('v', "verbose", &verbose,
3da297a6 1401 "be more verbose (show counter open errors, etc)"),
b456bae0
IM
1402 OPT_END()
1403};
1404
f37a291c 1405int cmd_top(int argc, const char **argv, const char *prefix __used)
b456bae0 1406{
69aad6f1
ACM
1407 struct perf_evsel *pos;
1408 int status = -ENOMEM;
b456bae0 1409
361c99a6
ACM
1410 evsel_list = perf_evlist__new();
1411 if (evsel_list == NULL)
1412 return -ENOMEM;
1413
b456bae0
IM
1414 page_size = sysconf(_SC_PAGE_SIZE);
1415
b456bae0
IM
1416 argc = parse_options(argc, argv, options, top_usage, 0);
1417 if (argc)
1418 usage_with_options(top_usage, options);
1419
5c98d466 1420 if (target_pid != -1)
d6d901c2 1421 target_tid = target_pid;
d6d901c2 1422
5c98d466
ACM
1423 threads = thread_map__new(target_pid, target_tid);
1424 if (threads == NULL) {
1425 pr_err("Problems finding threads of monitor\n");
1426 usage_with_options(top_usage, options);
d6d901c2
ZY
1427 }
1428
5c98d466
ACM
1429 event_array = malloc((sizeof(struct pollfd) *
1430 MAX_NR_CPUS * MAX_COUNTERS * threads->nr));
d6d901c2
ZY
1431 if (!event_array)
1432 return -ENOMEM;
1433
b456bae0 1434 /* CPU and PID are mutually exclusive */
c45c6ea2 1435 if (target_tid > 0 && cpu_list) {
b456bae0
IM
1436 printf("WARNING: PID switch overriding CPU\n");
1437 sleep(1);
c45c6ea2 1438 cpu_list = NULL;
b456bae0
IM
1439 }
1440
361c99a6
ACM
1441 if (!evsel_list->nr_entries &&
1442 perf_evlist__add_default(evsel_list) < 0) {
69aad6f1
ACM
1443 pr_err("Not enough memory for event selector list\n");
1444 return -ENOMEM;
1445 }
5a8e5a30 1446
2f335a02
FW
1447 if (delay_secs < 1)
1448 delay_secs = 1;
1449
7e4ff9e3
MG
1450 /*
1451 * User specified count overrides default frequency.
1452 */
1453 if (default_interval)
1454 freq = 0;
1455 else if (freq) {
1456 default_interval = freq;
1457 } else {
1458 fprintf(stderr, "frequency and count are zero, aborting\n");
1459 exit(EXIT_FAILURE);
1460 }
1461
c45c6ea2 1462 if (target_tid != -1)
60d567e2 1463 cpus = cpu_map__dummy_new();
a12b51c4 1464 else
60d567e2 1465 cpus = cpu_map__new(cpu_list);
c45c6ea2 1466
60d567e2 1467 if (cpus == NULL)
c45c6ea2 1468 usage_with_options(top_usage, options);
b456bae0 1469
361c99a6 1470 list_for_each_entry(pos, &evsel_list->entries, node) {
5c98d466
ACM
1471 if (perf_evsel__alloc_mmap_per_thread(pos, cpus->nr, threads->nr) < 0 ||
1472 perf_evsel__alloc_fd(pos, cpus->nr, threads->nr) < 0)
69aad6f1
ACM
1473 goto out_free_fd;
1474 /*
1475 * Fill in the ones not specifically initialized via -c:
1476 */
1477 if (pos->attr.sample_period)
1478 continue;
1479
1480 pos->attr.sample_period = default_interval;
1481 }
1482
361c99a6 1483 sym_evsel = list_entry(evsel_list->entries.next, struct perf_evsel, node);
cc841580 1484
69aad6f1 1485 symbol_conf.priv_size = (sizeof(struct sym_entry) +
361c99a6 1486 (evsel_list->nr_entries + 1) * sizeof(unsigned long));
69aad6f1
ACM
1487
1488 symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL);
1489 if (symbol__init() < 0)
1490 return -1;
1491
13cc5079 1492 get_term_dimensions(&winsize);
3b6ed988 1493 if (print_entries == 0) {
13cc5079 1494 update_print_entries(&winsize);
3b6ed988
ACM
1495 signal(SIGWINCH, sig_winch_handler);
1496 }
1497
69aad6f1
ACM
1498 status = __cmd_top();
1499out_free_fd:
361c99a6 1500 list_for_each_entry(pos, &evsel_list->entries, node)
69aad6f1 1501 perf_evsel__free_mmap(pos);
361c99a6 1502 perf_evlist__delete(evsel_list);
69aad6f1
ACM
1503
1504 return status;
b456bae0 1505}
This page took 0.183869 seconds and 5 git commands to generate.