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