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