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