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