]>
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 | |
c0443df1 | 23 | #include "util/cache.h" |
8fc0321f | 24 | #include "util/color.h" |
361c99a6 | 25 | #include "util/evlist.h" |
69aad6f1 | 26 | #include "util/evsel.h" |
b3165f41 ACM |
27 | #include "util/session.h" |
28 | #include "util/symbol.h" | |
439d473b | 29 | #include "util/thread.h" |
fd78260b | 30 | #include "util/thread_map.h" |
8c3e10eb | 31 | #include "util/top.h" |
148be2c1 | 32 | #include "util/util.h" |
43cbcd8a | 33 | #include <linux/rbtree.h> |
b456bae0 IM |
34 | #include "util/parse-options.h" |
35 | #include "util/parse-events.h" | |
a12b51c4 | 36 | #include "util/cpumap.h" |
69aad6f1 | 37 | #include "util/xyarray.h" |
07800601 | 38 | |
8f28827a FW |
39 | #include "util/debug.h" |
40 | ||
07800601 IM |
41 | #include <assert.h> |
42 | #include <fcntl.h> | |
0e9b20b8 | 43 | |
07800601 | 44 | #include <stdio.h> |
923c42c1 MG |
45 | #include <termios.h> |
46 | #include <unistd.h> | |
9486aa38 | 47 | #include <inttypes.h> |
0e9b20b8 | 48 | |
07800601 | 49 | #include <errno.h> |
07800601 IM |
50 | #include <time.h> |
51 | #include <sched.h> | |
07800601 IM |
52 | |
53 | #include <sys/syscall.h> | |
54 | #include <sys/ioctl.h> | |
55 | #include <sys/poll.h> | |
56 | #include <sys/prctl.h> | |
57 | #include <sys/wait.h> | |
58 | #include <sys/uio.h> | |
59 | #include <sys/mman.h> | |
60 | ||
61 | #include <linux/unistd.h> | |
62 | #include <linux/types.h> | |
63 | ||
69aad6f1 | 64 | #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y)) |
07800601 | 65 | |
8c3e10eb ACM |
66 | static struct perf_top top = { |
67 | .count_filter = 5, | |
68 | .delay_secs = 2, | |
69 | .display_weighted = -1, | |
70 | .target_pid = -1, | |
71 | .target_tid = -1, | |
72 | .active_symbols = LIST_HEAD_INIT(top.active_symbols), | |
73 | .active_symbols_lock = PTHREAD_MUTEX_INITIALIZER, | |
74 | .freq = 1000, /* 1 KHz */ | |
75 | }; | |
361c99a6 | 76 | |
c0555642 | 77 | static bool system_wide = false; |
07800601 | 78 | |
c0443df1 ACM |
79 | static bool use_tui, use_stdio; |
80 | ||
7e4ff9e3 | 81 | static int default_interval = 0; |
07800601 | 82 | |
c0555642 | 83 | static bool inherit = false; |
1967936d | 84 | static int realtime_prio = 0; |
c0555642 | 85 | static bool group = false; |
07800601 | 86 | static unsigned int page_size; |
70db7533 | 87 | static unsigned int mmap_pages = 128; |
07800601 | 88 | |
c0555642 | 89 | static bool dump_symtab = false; |
07800601 | 90 | |
13cc5079 | 91 | static struct winsize winsize; |
8ffcda17 | 92 | |
edb7c60e | 93 | static const char *sym_filter = NULL; |
42e59d7d | 94 | struct sym_entry *sym_filter_entry = NULL; |
6cff0e8d | 95 | struct sym_entry *sym_filter_entry_sched = NULL; |
42e59d7d | 96 | static int sym_pcnt_filter = 5; |
07800601 | 97 | |
923c42c1 MG |
98 | /* |
99 | * Source functions | |
100 | */ | |
101 | ||
895f0edc | 102 | void get_term_dimensions(struct winsize *ws) |
3b6ed988 | 103 | { |
13cc5079 ACM |
104 | char *s = getenv("LINES"); |
105 | ||
106 | if (s != NULL) { | |
107 | ws->ws_row = atoi(s); | |
108 | s = getenv("COLUMNS"); | |
109 | if (s != NULL) { | |
110 | ws->ws_col = atoi(s); | |
111 | if (ws->ws_row && ws->ws_col) | |
112 | return; | |
113 | } | |
3b6ed988 | 114 | } |
13cc5079 ACM |
115 | #ifdef TIOCGWINSZ |
116 | if (ioctl(1, TIOCGWINSZ, ws) == 0 && | |
117 | ws->ws_row && ws->ws_col) | |
118 | return; | |
3b6ed988 | 119 | #endif |
13cc5079 ACM |
120 | ws->ws_row = 25; |
121 | ws->ws_col = 80; | |
3b6ed988 ACM |
122 | } |
123 | ||
13cc5079 | 124 | static void update_print_entries(struct winsize *ws) |
3b6ed988 | 125 | { |
8c3e10eb | 126 | top.print_entries = ws->ws_row; |
13cc5079 | 127 | |
8c3e10eb ACM |
128 | if (top.print_entries > 9) |
129 | top.print_entries -= 9; | |
3b6ed988 ACM |
130 | } |
131 | ||
132 | static void sig_winch_handler(int sig __used) | |
133 | { | |
13cc5079 ACM |
134 | get_term_dimensions(&winsize); |
135 | update_print_entries(&winsize); | |
3b6ed988 ACM |
136 | } |
137 | ||
b0a9ab62 | 138 | static int parse_source(struct sym_entry *syme) |
923c42c1 MG |
139 | { |
140 | struct symbol *sym; | |
b269876c | 141 | struct sym_entry_source *source; |
439d473b | 142 | struct map *map; |
923c42c1 | 143 | FILE *file; |
83a0944f | 144 | char command[PATH_MAX*2]; |
439d473b ACM |
145 | const char *path; |
146 | u64 len; | |
923c42c1 MG |
147 | |
148 | if (!syme) | |
b0a9ab62 ACM |
149 | return -1; |
150 | ||
151 | sym = sym_entry__symbol(syme); | |
152 | map = syme->map; | |
153 | ||
154 | /* | |
155 | * We can't annotate with just /proc/kallsyms | |
156 | */ | |
157 | if (map->dso->origin == DSO__ORIG_KERNEL) | |
158 | return -1; | |
923c42c1 | 159 | |
b269876c | 160 | if (syme->src == NULL) { |
36479484 | 161 | syme->src = zalloc(sizeof(*source)); |
b269876c | 162 | if (syme->src == NULL) |
b0a9ab62 | 163 | return -1; |
b269876c ACM |
164 | pthread_mutex_init(&syme->src->lock, NULL); |
165 | } | |
166 | ||
167 | source = syme->src; | |
168 | ||
169 | if (source->lines) { | |
170 | pthread_mutex_lock(&source->lock); | |
923c42c1 MG |
171 | goto out_assign; |
172 | } | |
439d473b | 173 | path = map->dso->long_name; |
923c42c1 | 174 | |
923c42c1 MG |
175 | len = sym->end - sym->start; |
176 | ||
439d473b | 177 | sprintf(command, |
9486aa38 | 178 | "objdump --start-address=%#0*" PRIx64 " --stop-address=%#0*" PRIx64 " -dS %s", |
5f485364 ACM |
179 | BITS_PER_LONG / 4, map__rip_2objdump(map, sym->start), |
180 | BITS_PER_LONG / 4, map__rip_2objdump(map, sym->end), path); | |
923c42c1 MG |
181 | |
182 | file = popen(command, "r"); | |
183 | if (!file) | |
b0a9ab62 | 184 | return -1; |
923c42c1 | 185 | |
b269876c ACM |
186 | pthread_mutex_lock(&source->lock); |
187 | source->lines_tail = &source->lines; | |
923c42c1 MG |
188 | while (!feof(file)) { |
189 | struct source_line *src; | |
190 | size_t dummy = 0; | |
ee11b90b | 191 | char *c, *sep; |
923c42c1 MG |
192 | |
193 | src = malloc(sizeof(struct source_line)); | |
194 | assert(src != NULL); | |
195 | memset(src, 0, sizeof(struct source_line)); | |
196 | ||
197 | if (getline(&src->line, &dummy, file) < 0) | |
198 | break; | |
199 | if (!src->line) | |
200 | break; | |
201 | ||
202 | c = strchr(src->line, '\n'); | |
203 | if (c) | |
204 | *c = 0; | |
205 | ||
206 | src->next = NULL; | |
b269876c ACM |
207 | *source->lines_tail = src; |
208 | source->lines_tail = &src->next; | |
923c42c1 | 209 | |
ee11b90b KS |
210 | src->eip = strtoull(src->line, &sep, 16); |
211 | if (*sep == ':') | |
212 | src->eip = map__objdump_2ip(map, src->eip); | |
213 | else /* this line has no ip info (e.g. source line) */ | |
214 | src->eip = 0; | |
923c42c1 MG |
215 | } |
216 | pclose(file); | |
217 | out_assign: | |
218 | sym_filter_entry = syme; | |
b269876c | 219 | pthread_mutex_unlock(&source->lock); |
b0a9ab62 | 220 | return 0; |
923c42c1 MG |
221 | } |
222 | ||
223 | static void __zero_source_counters(struct sym_entry *syme) | |
224 | { | |
225 | int i; | |
226 | struct source_line *line; | |
227 | ||
b269876c | 228 | line = syme->src->lines; |
923c42c1 | 229 | while (line) { |
8c3e10eb | 230 | for (i = 0; i < top.evlist->nr_entries; i++) |
923c42c1 MG |
231 | line->count[i] = 0; |
232 | line = line->next; | |
233 | } | |
234 | } | |
235 | ||
236 | static void record_precise_ip(struct sym_entry *syme, int counter, u64 ip) | |
237 | { | |
238 | struct source_line *line; | |
239 | ||
240 | if (syme != sym_filter_entry) | |
241 | return; | |
242 | ||
b269876c | 243 | if (pthread_mutex_trylock(&syme->src->lock)) |
923c42c1 MG |
244 | return; |
245 | ||
b269876c | 246 | if (syme->src == NULL || syme->src->source == NULL) |
923c42c1 MG |
247 | goto out_unlock; |
248 | ||
b269876c | 249 | for (line = syme->src->lines; line; line = line->next) { |
ee11b90b KS |
250 | /* skip lines without IP info */ |
251 | if (line->eip == 0) | |
252 | continue; | |
923c42c1 MG |
253 | if (line->eip == ip) { |
254 | line->count[counter]++; | |
255 | break; | |
256 | } | |
257 | if (line->eip > ip) | |
258 | break; | |
259 | } | |
260 | out_unlock: | |
b269876c | 261 | pthread_mutex_unlock(&syme->src->lock); |
923c42c1 MG |
262 | } |
263 | ||
c7ad21af ACM |
264 | #define PATTERN_LEN (BITS_PER_LONG / 4 + 2) |
265 | ||
923c42c1 MG |
266 | static void lookup_sym_source(struct sym_entry *syme) |
267 | { | |
51a472de | 268 | struct symbol *symbol = sym_entry__symbol(syme); |
923c42c1 | 269 | struct source_line *line; |
c7ad21af | 270 | char pattern[PATTERN_LEN + 1]; |
923c42c1 | 271 | |
9486aa38 | 272 | sprintf(pattern, "%0*" PRIx64 " <", BITS_PER_LONG / 4, |
5f485364 | 273 | map__rip_2objdump(syme->map, symbol->start)); |
923c42c1 | 274 | |
b269876c ACM |
275 | pthread_mutex_lock(&syme->src->lock); |
276 | for (line = syme->src->lines; line; line = line->next) { | |
c7ad21af | 277 | if (memcmp(line->line, pattern, PATTERN_LEN) == 0) { |
b269876c | 278 | syme->src->source = line; |
923c42c1 MG |
279 | break; |
280 | } | |
281 | } | |
b269876c | 282 | pthread_mutex_unlock(&syme->src->lock); |
923c42c1 MG |
283 | } |
284 | ||
285 | static void show_lines(struct source_line *queue, int count, int total) | |
286 | { | |
287 | int i; | |
288 | struct source_line *line; | |
289 | ||
290 | line = queue; | |
291 | for (i = 0; i < count; i++) { | |
8c3e10eb | 292 | float pcnt = 100.0*(float)line->count[top.sym_counter]/(float)total; |
923c42c1 | 293 | |
8c3e10eb | 294 | printf("%8li %4.1f%%\t%s\n", line->count[top.sym_counter], pcnt, line->line); |
923c42c1 MG |
295 | line = line->next; |
296 | } | |
297 | } | |
298 | ||
299 | #define TRACE_COUNT 3 | |
300 | ||
301 | static void show_details(struct sym_entry *syme) | |
302 | { | |
303 | struct symbol *symbol; | |
304 | struct source_line *line; | |
305 | struct source_line *line_queue = NULL; | |
306 | int displayed = 0; | |
307 | int line_queue_count = 0, total = 0, more = 0; | |
308 | ||
309 | if (!syme) | |
310 | return; | |
311 | ||
b269876c | 312 | if (!syme->src->source) |
923c42c1 MG |
313 | lookup_sym_source(syme); |
314 | ||
b269876c | 315 | if (!syme->src->source) |
923c42c1 MG |
316 | return; |
317 | ||
51a472de | 318 | symbol = sym_entry__symbol(syme); |
8c3e10eb | 319 | printf("Showing %s for %s\n", event_name(top.sym_evsel), symbol->name); |
923c42c1 MG |
320 | printf(" Events Pcnt (>=%d%%)\n", sym_pcnt_filter); |
321 | ||
b269876c ACM |
322 | pthread_mutex_lock(&syme->src->lock); |
323 | line = syme->src->source; | |
923c42c1 | 324 | while (line) { |
8c3e10eb | 325 | total += line->count[top.sym_counter]; |
923c42c1 MG |
326 | line = line->next; |
327 | } | |
328 | ||
b269876c | 329 | line = syme->src->source; |
923c42c1 MG |
330 | while (line) { |
331 | float pcnt = 0.0; | |
332 | ||
333 | if (!line_queue_count) | |
334 | line_queue = line; | |
335 | line_queue_count++; | |
336 | ||
8c3e10eb ACM |
337 | if (line->count[top.sym_counter]) |
338 | pcnt = 100.0 * line->count[top.sym_counter] / (float)total; | |
923c42c1 | 339 | if (pcnt >= (float)sym_pcnt_filter) { |
8c3e10eb | 340 | if (displayed <= top.print_entries) |
923c42c1 MG |
341 | show_lines(line_queue, line_queue_count, total); |
342 | else more++; | |
343 | displayed += line_queue_count; | |
344 | line_queue_count = 0; | |
345 | line_queue = NULL; | |
346 | } else if (line_queue_count > TRACE_COUNT) { | |
347 | line_queue = line_queue->next; | |
348 | line_queue_count--; | |
349 | } | |
350 | ||
8c3e10eb | 351 | line->count[top.sym_counter] = top.zero ? 0 : line->count[top.sym_counter] * 7 / 8; |
923c42c1 MG |
352 | line = line->next; |
353 | } | |
b269876c | 354 | pthread_mutex_unlock(&syme->src->lock); |
923c42c1 MG |
355 | if (more) |
356 | printf("%d lines not displayed, maybe increase display entries [e]\n", more); | |
357 | } | |
07800601 | 358 | |
07800601 IM |
359 | static const char CONSOLE_CLEAR[] = "\e[H\e[2J"; |
360 | ||
c44613a4 | 361 | static void __list_insert_active_sym(struct sym_entry *syme) |
de04687f | 362 | { |
8c3e10eb | 363 | list_add(&syme->node, &top.active_symbols); |
de04687f | 364 | } |
07800601 | 365 | |
93fc64f1 | 366 | static void print_sym_table(struct perf_session *session) |
07800601 | 367 | { |
8c3e10eb ACM |
368 | char bf[160]; |
369 | int printed = 0; | |
de04687f | 370 | struct rb_node *nd; |
8c3e10eb ACM |
371 | struct sym_entry *syme; |
372 | struct rb_root tmp = RB_ROOT; | |
13cc5079 | 373 | const int win_width = winsize.ws_col - 1; |
8c3e10eb ACM |
374 | int sym_width, dso_width, dso_short_width; |
375 | float sum_ksamples = perf_top__decay_samples(&top, &tmp); | |
d94b9430 | 376 | |
0f5486b5 | 377 | puts(CONSOLE_CLEAR); |
07800601 | 378 | |
8c3e10eb ACM |
379 | perf_top__header_snprintf(&top, bf, sizeof(bf)); |
380 | printf("%s\n", bf); | |
07800601 | 381 | |
8c3e10eb | 382 | perf_top__reset_sample_counters(&top); |
07800601 | 383 | |
1a105f74 | 384 | printf("%-*.*s\n", win_width, win_width, graph_dotted_line); |
07800601 | 385 | |
93fc64f1 ACM |
386 | if (session->hists.stats.total_lost != 0) { |
387 | color_fprintf(stdout, PERF_COLOR_RED, "WARNING:"); | |
388 | printf(" LOST %" PRIu64 " events, Check IO/CPU overload\n", | |
389 | session->hists.stats.total_lost); | |
390 | } | |
391 | ||
923c42c1 MG |
392 | if (sym_filter_entry) { |
393 | show_details(sym_filter_entry); | |
394 | return; | |
395 | } | |
396 | ||
8c3e10eb ACM |
397 | perf_top__find_widths(&top, &tmp, &dso_width, &dso_short_width, |
398 | &sym_width); | |
13cc5079 | 399 | |
b63be8d7 ACM |
400 | if (sym_width + dso_width > winsize.ws_col - 29) { |
401 | dso_width = dso_short_width; | |
402 | if (sym_width + dso_width > winsize.ws_col - 29) | |
403 | sym_width = winsize.ws_col - dso_width - 29; | |
404 | } | |
7cc017ed | 405 | putchar('\n'); |
8c3e10eb | 406 | if (top.evlist->nr_entries == 1) |
5b2bb75a | 407 | printf(" samples pcnt"); |
07800601 | 408 | else |
5b2bb75a | 409 | printf(" weight samples pcnt"); |
07800601 | 410 | |
7ced156b ACM |
411 | if (verbose) |
412 | printf(" RIP "); | |
7cc017ed | 413 | printf(" %-*.*s DSO\n", sym_width, sym_width, "function"); |
5b2bb75a | 414 | printf(" %s _______ _____", |
8c3e10eb | 415 | top.evlist->nr_entries == 1 ? " " : "______"); |
7ced156b | 416 | if (verbose) |
5b2bb75a | 417 | printf(" ________________"); |
1a105f74 | 418 | printf(" %-*.*s", sym_width, sym_width, graph_line); |
7cc017ed | 419 | printf(" %-*.*s", dso_width, dso_width, graph_line); |
1a105f74 | 420 | puts("\n"); |
07800601 | 421 | |
de04687f | 422 | for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) { |
83a0944f | 423 | struct symbol *sym; |
8fc0321f | 424 | double pcnt; |
d94b9430 | 425 | |
83a0944f | 426 | syme = rb_entry(nd, struct sym_entry, rb_node); |
51a472de | 427 | sym = sym_entry__symbol(syme); |
8c3e10eb ACM |
428 | if (++printed > top.print_entries || |
429 | (int)syme->snap_count < top.count_filter) | |
c44613a4 | 430 | continue; |
d94b9430 | 431 | |
2debbc83 IM |
432 | pcnt = 100.0 - (100.0 * ((sum_ksamples - syme->snap_count) / |
433 | sum_ksamples)); | |
d94b9430 | 434 | |
8c3e10eb | 435 | if (top.evlist->nr_entries == 1 || !top.display_weighted) |
5b2bb75a | 436 | printf("%20.2f ", syme->weight); |
d94b9430 | 437 | else |
5b2bb75a | 438 | printf("%9.1f %10ld ", syme->weight, syme->snap_count); |
8fc0321f | 439 | |
1e11fd82 | 440 | percent_color_fprintf(stdout, "%4.1f%%", pcnt); |
7ced156b | 441 | if (verbose) |
9486aa38 | 442 | printf(" %016" PRIx64, sym->start); |
13cc5079 | 443 | printf(" %-*.*s", sym_width, sym_width, sym->name); |
7cc017ed ACM |
444 | printf(" %-*.*s\n", dso_width, dso_width, |
445 | dso_width >= syme->map->dso->long_name_len ? | |
446 | syme->map->dso->long_name : | |
447 | syme->map->dso->short_name); | |
07800601 | 448 | } |
07800601 IM |
449 | } |
450 | ||
923c42c1 MG |
451 | static void prompt_integer(int *target, const char *msg) |
452 | { | |
453 | char *buf = malloc(0), *p; | |
454 | size_t dummy = 0; | |
455 | int tmp; | |
456 | ||
457 | fprintf(stdout, "\n%s: ", msg); | |
458 | if (getline(&buf, &dummy, stdin) < 0) | |
459 | return; | |
460 | ||
461 | p = strchr(buf, '\n'); | |
462 | if (p) | |
463 | *p = 0; | |
464 | ||
465 | p = buf; | |
466 | while(*p) { | |
467 | if (!isdigit(*p)) | |
468 | goto out_free; | |
469 | p++; | |
470 | } | |
471 | tmp = strtoul(buf, NULL, 10); | |
472 | *target = tmp; | |
473 | out_free: | |
474 | free(buf); | |
475 | } | |
476 | ||
477 | static void prompt_percent(int *target, const char *msg) | |
478 | { | |
479 | int tmp = 0; | |
480 | ||
481 | prompt_integer(&tmp, msg); | |
482 | if (tmp >= 0 && tmp <= 100) | |
483 | *target = tmp; | |
484 | } | |
485 | ||
486 | static void prompt_symbol(struct sym_entry **target, const char *msg) | |
487 | { | |
488 | char *buf = malloc(0), *p; | |
489 | struct sym_entry *syme = *target, *n, *found = NULL; | |
490 | size_t dummy = 0; | |
491 | ||
492 | /* zero counters of active symbol */ | |
493 | if (syme) { | |
b269876c | 494 | pthread_mutex_lock(&syme->src->lock); |
923c42c1 MG |
495 | __zero_source_counters(syme); |
496 | *target = NULL; | |
b269876c | 497 | pthread_mutex_unlock(&syme->src->lock); |
923c42c1 MG |
498 | } |
499 | ||
500 | fprintf(stdout, "\n%s: ", msg); | |
501 | if (getline(&buf, &dummy, stdin) < 0) | |
502 | goto out_free; | |
503 | ||
504 | p = strchr(buf, '\n'); | |
505 | if (p) | |
506 | *p = 0; | |
507 | ||
8c3e10eb ACM |
508 | pthread_mutex_lock(&top.active_symbols_lock); |
509 | syme = list_entry(top.active_symbols.next, struct sym_entry, node); | |
510 | pthread_mutex_unlock(&top.active_symbols_lock); | |
923c42c1 | 511 | |
8c3e10eb | 512 | list_for_each_entry_safe_from(syme, n, &top.active_symbols, node) { |
51a472de | 513 | struct symbol *sym = sym_entry__symbol(syme); |
923c42c1 MG |
514 | |
515 | if (!strcmp(buf, sym->name)) { | |
516 | found = syme; | |
517 | break; | |
518 | } | |
519 | } | |
520 | ||
521 | if (!found) { | |
66aeb6d5 | 522 | fprintf(stderr, "Sorry, %s is not active.\n", buf); |
923c42c1 MG |
523 | sleep(1); |
524 | return; | |
525 | } else | |
526 | parse_source(found); | |
527 | ||
528 | out_free: | |
529 | free(buf); | |
530 | } | |
531 | ||
091bd2e9 | 532 | static void print_mapped_keys(void) |
923c42c1 | 533 | { |
091bd2e9 MG |
534 | char *name = NULL; |
535 | ||
536 | if (sym_filter_entry) { | |
51a472de | 537 | struct symbol *sym = sym_entry__symbol(sym_filter_entry); |
091bd2e9 MG |
538 | name = sym->name; |
539 | } | |
540 | ||
541 | fprintf(stdout, "\nMapped keys:\n"); | |
8c3e10eb ACM |
542 | fprintf(stdout, "\t[d] display refresh delay. \t(%d)\n", top.delay_secs); |
543 | fprintf(stdout, "\t[e] display entries (lines). \t(%d)\n", top.print_entries); | |
091bd2e9 | 544 | |
8c3e10eb ACM |
545 | if (top.evlist->nr_entries > 1) |
546 | fprintf(stdout, "\t[E] active event counter. \t(%s)\n", event_name(top.sym_evsel)); | |
091bd2e9 | 547 | |
8c3e10eb | 548 | fprintf(stdout, "\t[f] profile display filter (count). \t(%d)\n", top.count_filter); |
091bd2e9 | 549 | |
6cff0e8d KS |
550 | fprintf(stdout, "\t[F] annotate display filter (percent). \t(%d%%)\n", sym_pcnt_filter); |
551 | fprintf(stdout, "\t[s] annotate symbol. \t(%s)\n", name?: "NULL"); | |
552 | fprintf(stdout, "\t[S] stop annotation.\n"); | |
091bd2e9 | 553 | |
8c3e10eb ACM |
554 | if (top.evlist->nr_entries > 1) |
555 | fprintf(stdout, "\t[w] toggle display weighted/count[E]r. \t(%d)\n", top.display_weighted ? 1 : 0); | |
091bd2e9 | 556 | |
8ffcda17 | 557 | fprintf(stdout, |
1a72cfa6 | 558 | "\t[K] hide kernel_symbols symbols. \t(%s)\n", |
8c3e10eb | 559 | top.hide_kernel_symbols ? "yes" : "no"); |
8ffcda17 ACM |
560 | fprintf(stdout, |
561 | "\t[U] hide user symbols. \t(%s)\n", | |
8c3e10eb ACM |
562 | top.hide_user_symbols ? "yes" : "no"); |
563 | fprintf(stdout, "\t[z] toggle sample zeroing. \t(%d)\n", top.zero ? 1 : 0); | |
091bd2e9 MG |
564 | fprintf(stdout, "\t[qQ] quit.\n"); |
565 | } | |
566 | ||
567 | static int key_mapped(int c) | |
568 | { | |
569 | switch (c) { | |
570 | case 'd': | |
571 | case 'e': | |
572 | case 'f': | |
573 | case 'z': | |
574 | case 'q': | |
575 | case 'Q': | |
8ffcda17 ACM |
576 | case 'K': |
577 | case 'U': | |
6cff0e8d KS |
578 | case 'F': |
579 | case 's': | |
580 | case 'S': | |
091bd2e9 MG |
581 | return 1; |
582 | case 'E': | |
583 | case 'w': | |
8c3e10eb | 584 | return top.evlist->nr_entries > 1 ? 1 : 0; |
83a0944f IM |
585 | default: |
586 | break; | |
091bd2e9 MG |
587 | } |
588 | ||
589 | return 0; | |
923c42c1 MG |
590 | } |
591 | ||
a1645ce1 | 592 | static void handle_keypress(struct perf_session *session, int c) |
923c42c1 | 593 | { |
091bd2e9 MG |
594 | if (!key_mapped(c)) { |
595 | struct pollfd stdin_poll = { .fd = 0, .events = POLLIN }; | |
596 | struct termios tc, save; | |
597 | ||
598 | print_mapped_keys(); | |
599 | fprintf(stdout, "\nEnter selection, or unmapped key to continue: "); | |
600 | fflush(stdout); | |
601 | ||
602 | tcgetattr(0, &save); | |
603 | tc = save; | |
604 | tc.c_lflag &= ~(ICANON | ECHO); | |
605 | tc.c_cc[VMIN] = 0; | |
606 | tc.c_cc[VTIME] = 0; | |
607 | tcsetattr(0, TCSANOW, &tc); | |
608 | ||
609 | poll(&stdin_poll, 1, -1); | |
610 | c = getc(stdin); | |
611 | ||
612 | tcsetattr(0, TCSAFLUSH, &save); | |
613 | if (!key_mapped(c)) | |
614 | return; | |
615 | } | |
616 | ||
923c42c1 MG |
617 | switch (c) { |
618 | case 'd': | |
8c3e10eb ACM |
619 | prompt_integer(&top.delay_secs, "Enter display delay"); |
620 | if (top.delay_secs < 1) | |
621 | top.delay_secs = 1; | |
923c42c1 MG |
622 | break; |
623 | case 'e': | |
8c3e10eb ACM |
624 | prompt_integer(&top.print_entries, "Enter display entries (lines)"); |
625 | if (top.print_entries == 0) { | |
13cc5079 | 626 | sig_winch_handler(SIGWINCH); |
3b6ed988 ACM |
627 | signal(SIGWINCH, sig_winch_handler); |
628 | } else | |
629 | signal(SIGWINCH, SIG_DFL); | |
923c42c1 MG |
630 | break; |
631 | case 'E': | |
8c3e10eb | 632 | if (top.evlist->nr_entries > 1) { |
923c42c1 | 633 | fprintf(stderr, "\nAvailable events:"); |
69aad6f1 | 634 | |
8c3e10eb ACM |
635 | list_for_each_entry(top.sym_evsel, &top.evlist->entries, node) |
636 | fprintf(stderr, "\n\t%d %s", top.sym_evsel->idx, event_name(top.sym_evsel)); | |
923c42c1 | 637 | |
8c3e10eb | 638 | prompt_integer(&top.sym_counter, "Enter details event counter"); |
923c42c1 | 639 | |
8c3e10eb ACM |
640 | if (top.sym_counter >= top.evlist->nr_entries) { |
641 | top.sym_evsel = list_entry(top.evlist->entries.next, struct perf_evsel, node); | |
642 | top.sym_counter = 0; | |
643 | fprintf(stderr, "Sorry, no such event, using %s.\n", event_name(top.sym_evsel)); | |
923c42c1 | 644 | sleep(1); |
69aad6f1 | 645 | break; |
923c42c1 | 646 | } |
8c3e10eb ACM |
647 | list_for_each_entry(top.sym_evsel, &top.evlist->entries, node) |
648 | if (top.sym_evsel->idx == top.sym_counter) | |
69aad6f1 | 649 | break; |
8c3e10eb | 650 | } else top.sym_counter = 0; |
923c42c1 MG |
651 | break; |
652 | case 'f': | |
8c3e10eb | 653 | prompt_integer(&top.count_filter, "Enter display event count filter"); |
923c42c1 MG |
654 | break; |
655 | case 'F': | |
656 | prompt_percent(&sym_pcnt_filter, "Enter details display event filter (percent)"); | |
657 | break; | |
8ffcda17 | 658 | case 'K': |
8c3e10eb | 659 | top.hide_kernel_symbols = !top.hide_kernel_symbols; |
8ffcda17 | 660 | break; |
923c42c1 MG |
661 | case 'q': |
662 | case 'Q': | |
663 | printf("exiting.\n"); | |
c338aee8 | 664 | if (dump_symtab) |
cbf69680 | 665 | perf_session__fprintf_dsos(session, stderr); |
923c42c1 MG |
666 | exit(0); |
667 | case 's': | |
668 | prompt_symbol(&sym_filter_entry, "Enter details symbol"); | |
669 | break; | |
670 | case 'S': | |
671 | if (!sym_filter_entry) | |
672 | break; | |
673 | else { | |
674 | struct sym_entry *syme = sym_filter_entry; | |
675 | ||
b269876c | 676 | pthread_mutex_lock(&syme->src->lock); |
923c42c1 MG |
677 | sym_filter_entry = NULL; |
678 | __zero_source_counters(syme); | |
b269876c | 679 | pthread_mutex_unlock(&syme->src->lock); |
923c42c1 MG |
680 | } |
681 | break; | |
8ffcda17 | 682 | case 'U': |
8c3e10eb | 683 | top.hide_user_symbols = !top.hide_user_symbols; |
8ffcda17 | 684 | break; |
46ab9764 | 685 | case 'w': |
8c3e10eb | 686 | top.display_weighted = ~top.display_weighted; |
46ab9764 | 687 | break; |
923c42c1 | 688 | case 'z': |
8c3e10eb | 689 | top.zero = !top.zero; |
923c42c1 | 690 | break; |
83a0944f IM |
691 | default: |
692 | break; | |
923c42c1 MG |
693 | } |
694 | } | |
695 | ||
c0443df1 ACM |
696 | static void *display_thread_tui(void *arg __used) |
697 | { | |
698 | perf_top__tui_browser(&top); | |
699 | exit_browser(0); | |
700 | exit(0); | |
701 | return NULL; | |
702 | } | |
703 | ||
f37a291c | 704 | static void *display_thread(void *arg __used) |
07800601 | 705 | { |
0f5486b5 | 706 | struct pollfd stdin_poll = { .fd = 0, .events = POLLIN }; |
923c42c1 MG |
707 | struct termios tc, save; |
708 | int delay_msecs, c; | |
a1645ce1 | 709 | struct perf_session *session = (struct perf_session *) arg; |
923c42c1 MG |
710 | |
711 | tcgetattr(0, &save); | |
712 | tc = save; | |
713 | tc.c_lflag &= ~(ICANON | ECHO); | |
714 | tc.c_cc[VMIN] = 0; | |
715 | tc.c_cc[VTIME] = 0; | |
091bd2e9 | 716 | |
923c42c1 | 717 | repeat: |
8c3e10eb | 718 | delay_msecs = top.delay_secs * 1000; |
923c42c1 MG |
719 | tcsetattr(0, TCSANOW, &tc); |
720 | /* trash return*/ | |
721 | getc(stdin); | |
07800601 | 722 | |
0f5486b5 | 723 | do { |
93fc64f1 | 724 | print_sym_table(session); |
0f5486b5 FW |
725 | } while (!poll(&stdin_poll, 1, delay_msecs) == 1); |
726 | ||
923c42c1 MG |
727 | c = getc(stdin); |
728 | tcsetattr(0, TCSAFLUSH, &save); | |
729 | ||
a1645ce1 | 730 | handle_keypress(session, c); |
923c42c1 | 731 | goto repeat; |
07800601 IM |
732 | |
733 | return NULL; | |
734 | } | |
735 | ||
2ab52083 | 736 | /* Tag samples to be skipped. */ |
f37a291c | 737 | static const char *skip_symbols[] = { |
2ab52083 | 738 | "default_idle", |
b0e8572f | 739 | "native_safe_halt", |
2ab52083 AB |
740 | "cpu_idle", |
741 | "enter_idle", | |
742 | "exit_idle", | |
743 | "mwait_idle", | |
59b90056 | 744 | "mwait_idle_with_hints", |
8357275b | 745 | "poll_idle", |
3a3393ef AB |
746 | "ppc64_runlatch_off", |
747 | "pseries_dedicated_idle_sleep", | |
2ab52083 AB |
748 | NULL |
749 | }; | |
750 | ||
439d473b | 751 | static int symbol_filter(struct map *map, struct symbol *sym) |
07800601 | 752 | { |
de04687f ACM |
753 | struct sym_entry *syme; |
754 | const char *name = sym->name; | |
2ab52083 | 755 | int i; |
de04687f | 756 | |
3a3393ef AB |
757 | /* |
758 | * ppc64 uses function descriptors and appends a '.' to the | |
759 | * start of every instruction address. Remove it. | |
760 | */ | |
761 | if (name[0] == '.') | |
762 | name++; | |
763 | ||
de04687f ACM |
764 | if (!strcmp(name, "_text") || |
765 | !strcmp(name, "_etext") || | |
766 | !strcmp(name, "_sinittext") || | |
767 | !strncmp("init_module", name, 11) || | |
768 | !strncmp("cleanup_module", name, 14) || | |
769 | strstr(name, "_text_start") || | |
770 | strstr(name, "_text_end")) | |
07800601 | 771 | return 1; |
07800601 | 772 | |
00a192b3 | 773 | syme = symbol__priv(sym); |
439d473b | 774 | syme->map = map; |
b269876c | 775 | syme->src = NULL; |
6cff0e8d KS |
776 | |
777 | if (!sym_filter_entry && sym_filter && !strcmp(name, sym_filter)) { | |
778 | /* schedule initial sym_filter_entry setup */ | |
779 | sym_filter_entry_sched = syme; | |
780 | sym_filter = NULL; | |
781 | } | |
923c42c1 | 782 | |
2ab52083 AB |
783 | for (i = 0; skip_symbols[i]; i++) { |
784 | if (!strcmp(skip_symbols[i], name)) { | |
785 | syme->skip = 1; | |
786 | break; | |
787 | } | |
788 | } | |
07800601 | 789 | |
07800601 IM |
790 | return 0; |
791 | } | |
792 | ||
8115d60c ACM |
793 | static void perf_event__process_sample(const union perf_event *event, |
794 | struct perf_sample *sample, | |
795 | struct perf_session *session) | |
07800601 | 796 | { |
8115d60c | 797 | u64 ip = event->ip.ip; |
5b2bb75a | 798 | struct sym_entry *syme; |
1ed091c4 | 799 | struct addr_location al; |
23346f21 | 800 | struct machine *machine; |
8115d60c | 801 | u8 origin = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; |
5b2bb75a | 802 | |
8c3e10eb | 803 | ++top.samples; |
24bfef0f | 804 | |
8ffcda17 | 805 | switch (origin) { |
1ed091c4 | 806 | case PERF_RECORD_MISC_USER: |
8c3e10eb ACM |
807 | ++top.us_samples; |
808 | if (top.hide_user_symbols) | |
8ffcda17 | 809 | return; |
23346f21 | 810 | machine = perf_session__find_host_machine(session); |
1ed091c4 | 811 | break; |
5b2bb75a | 812 | case PERF_RECORD_MISC_KERNEL: |
8c3e10eb ACM |
813 | ++top.kernel_samples; |
814 | if (top.hide_kernel_symbols) | |
8ffcda17 | 815 | return; |
23346f21 | 816 | machine = perf_session__find_host_machine(session); |
a1645ce1 ZY |
817 | break; |
818 | case PERF_RECORD_MISC_GUEST_KERNEL: | |
8c3e10eb | 819 | ++top.guest_kernel_samples; |
8115d60c | 820 | machine = perf_session__find_machine(session, event->ip.pid); |
5b2bb75a | 821 | break; |
a1645ce1 | 822 | case PERF_RECORD_MISC_GUEST_USER: |
8c3e10eb | 823 | ++top.guest_us_samples; |
a1645ce1 ZY |
824 | /* |
825 | * TODO: we don't process guest user from host side | |
826 | * except simple counting. | |
827 | */ | |
828 | return; | |
5b2bb75a ACM |
829 | default: |
830 | return; | |
831 | } | |
832 | ||
23346f21 | 833 | if (!machine && perf_guest) { |
a1645ce1 | 834 | pr_err("Can't find guest [%d]'s kernel information\n", |
8115d60c | 835 | event->ip.pid); |
a1645ce1 ZY |
836 | return; |
837 | } | |
838 | ||
8115d60c | 839 | if (event->header.misc & PERF_RECORD_MISC_EXACT_IP) |
8c3e10eb | 840 | top.exact_samples++; |
1676b8a0 | 841 | |
8115d60c ACM |
842 | if (perf_event__preprocess_sample(event, session, &al, sample, |
843 | symbol_filter) < 0 || | |
72b8fa17 | 844 | al.filtered) |
1ed091c4 | 845 | return; |
07800601 | 846 | |
72b8fa17 ACM |
847 | if (al.sym == NULL) { |
848 | /* | |
849 | * As we do lazy loading of symtabs we only will know if the | |
850 | * specified vmlinux file is invalid when we actually have a | |
851 | * hit in kernel space and then try to load it. So if we get | |
852 | * here and there are _no_ symbols in the DSO backing the | |
853 | * kernel map, bail out. | |
854 | * | |
855 | * We may never get here, for instance, if we use -K/ | |
856 | * --hide-kernel-symbols, even if the user specifies an | |
857 | * invalid --vmlinux ;-) | |
858 | */ | |
23346f21 | 859 | if (al.map == machine->vmlinux_maps[MAP__FUNCTION] && |
72b8fa17 ACM |
860 | RB_EMPTY_ROOT(&al.map->dso->symbols[MAP__FUNCTION])) { |
861 | pr_err("The %s file can't be used\n", | |
862 | symbol_conf.vmlinux_name); | |
863 | exit(1); | |
864 | } | |
865 | ||
866 | return; | |
867 | } | |
868 | ||
6cff0e8d KS |
869 | /* let's see, whether we need to install initial sym_filter_entry */ |
870 | if (sym_filter_entry_sched) { | |
871 | sym_filter_entry = sym_filter_entry_sched; | |
872 | sym_filter_entry_sched = NULL; | |
b0a9ab62 ACM |
873 | if (parse_source(sym_filter_entry) < 0) { |
874 | struct symbol *sym = sym_entry__symbol(sym_filter_entry); | |
875 | ||
876 | pr_err("Can't annotate %s", sym->name); | |
877 | if (sym_filter_entry->map->dso->origin == DSO__ORIG_KERNEL) { | |
878 | pr_err(": No vmlinux file was found in the path:\n"); | |
5ad90e4e | 879 | machine__fprintf_vmlinux_path(machine, stderr); |
b0a9ab62 ACM |
880 | } else |
881 | pr_err(".\n"); | |
882 | exit(1); | |
883 | } | |
6cff0e8d KS |
884 | } |
885 | ||
1ed091c4 | 886 | syme = symbol__priv(al.sym); |
5b2bb75a | 887 | if (!syme->skip) { |
70db7533 ACM |
888 | struct perf_evsel *evsel; |
889 | ||
8ffcda17 | 890 | syme->origin = origin; |
8c3e10eb | 891 | evsel = perf_evlist__id2evsel(top.evlist, sample->id); |
70db7533 ACM |
892 | assert(evsel != NULL); |
893 | syme->count[evsel->idx]++; | |
69aad6f1 | 894 | record_precise_ip(syme, evsel->idx, ip); |
8c3e10eb | 895 | pthread_mutex_lock(&top.active_symbols_lock); |
5b2bb75a ACM |
896 | if (list_empty(&syme->node) || !syme->node.next) |
897 | __list_insert_active_sym(syme); | |
8c3e10eb | 898 | pthread_mutex_unlock(&top.active_symbols_lock); |
5b2bb75a | 899 | } |
07800601 IM |
900 | } |
901 | ||
70db7533 | 902 | static void perf_session__mmap_read_cpu(struct perf_session *self, int cpu) |
07800601 | 903 | { |
8d50e5b4 | 904 | struct perf_sample sample; |
8115d60c | 905 | union perf_event *event; |
07800601 | 906 | |
8c3e10eb | 907 | while ((event = perf_evlist__read_on_cpu(top.evlist, cpu)) != NULL) { |
d0dd74e8 | 908 | perf_session__parse_sample(self, event, &sample); |
04391deb | 909 | |
5b2bb75a | 910 | if (event->header.type == PERF_RECORD_SAMPLE) |
8115d60c | 911 | perf_event__process_sample(event, &sample, self); |
5b2bb75a | 912 | else |
8115d60c | 913 | perf_event__process(event, &sample, self); |
07800601 | 914 | } |
07800601 IM |
915 | } |
916 | ||
d8f66248 | 917 | static void perf_session__mmap_read(struct perf_session *self) |
2f01190a | 918 | { |
70db7533 ACM |
919 | int i; |
920 | ||
8c3e10eb | 921 | for (i = 0; i < top.evlist->cpus->nr; i++) |
70db7533 | 922 | perf_session__mmap_read_cpu(self, i); |
2f01190a FW |
923 | } |
924 | ||
72cb7013 ACM |
925 | static void start_counters(struct perf_evlist *evlist) |
926 | { | |
927 | struct perf_evsel *counter; | |
7e4ff9e3 | 928 | |
72cb7013 ACM |
929 | list_for_each_entry(counter, &evlist->entries, node) { |
930 | struct perf_event_attr *attr = &counter->attr; | |
716c69fe | 931 | |
72cb7013 ACM |
932 | attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID; |
933 | ||
8c3e10eb | 934 | if (top.freq) { |
72cb7013 ACM |
935 | attr->sample_type |= PERF_SAMPLE_PERIOD; |
936 | attr->freq = 1; | |
8c3e10eb | 937 | attr->sample_freq = top.freq; |
72cb7013 | 938 | } |
d6d901c2 | 939 | |
70db7533 ACM |
940 | if (evlist->nr_entries > 1) { |
941 | attr->sample_type |= PERF_SAMPLE_ID; | |
942 | attr->read_format |= PERF_FORMAT_ID; | |
943 | } | |
944 | ||
72cb7013 ACM |
945 | attr->mmap = 1; |
946 | try_again: | |
8c3e10eb ACM |
947 | if (perf_evsel__open(counter, top.evlist->cpus, |
948 | top.evlist->threads, group, inherit) < 0) { | |
d6d901c2 ZY |
949 | int err = errno; |
950 | ||
951 | if (err == EPERM || err == EACCES) | |
d9cf837e CA |
952 | die("Permission error - are you root?\n" |
953 | "\t Consider tweaking" | |
954 | " /proc/sys/kernel/perf_event_paranoid.\n"); | |
d6d901c2 ZY |
955 | /* |
956 | * If it's cycles then fall back to hrtimer | |
957 | * based cpu-clock-tick sw counter, which | |
958 | * is always available even if no PMU support: | |
959 | */ | |
72cb7013 ACM |
960 | if (attr->type == PERF_TYPE_HARDWARE && |
961 | attr->config == PERF_COUNT_HW_CPU_CYCLES) { | |
d6d901c2 ZY |
962 | |
963 | if (verbose) | |
964 | warning(" ... trying to fall back to cpu-clock-ticks\n"); | |
965 | ||
966 | attr->type = PERF_TYPE_SOFTWARE; | |
967 | attr->config = PERF_COUNT_SW_CPU_CLOCK; | |
968 | goto try_again; | |
969 | } | |
970 | printf("\n"); | |
72cb7013 ACM |
971 | error("sys_perf_event_open() syscall returned with %d " |
972 | "(%s). /bin/dmesg may provide additional information.\n", | |
973 | err, strerror(err)); | |
d6d901c2 ZY |
974 | die("No CONFIG_PERF_EVENTS=y kernel support configured?\n"); |
975 | exit(-1); | |
976 | } | |
716c69fe | 977 | } |
70db7533 | 978 | |
7e2ed097 | 979 | if (perf_evlist__mmap(evlist, mmap_pages, false) < 0) |
70db7533 | 980 | die("failed to mmap with %d (%s)\n", errno, strerror(errno)); |
716c69fe IM |
981 | } |
982 | ||
983 | static int __cmd_top(void) | |
984 | { | |
985 | pthread_t thread; | |
70db7533 | 986 | struct perf_evsel *first; |
72cb7013 | 987 | int ret; |
d8f66248 | 988 | /* |
b3165f41 ACM |
989 | * FIXME: perf_session__new should allow passing a O_MMAP, so that all this |
990 | * mmap reading, etc is encapsulated in it. Use O_WRONLY for now. | |
d8f66248 | 991 | */ |
21ef97f0 | 992 | struct perf_session *session = perf_session__new(NULL, O_WRONLY, false, false, NULL); |
b3165f41 ACM |
993 | if (session == NULL) |
994 | return -ENOMEM; | |
07800601 | 995 | |
8c3e10eb ACM |
996 | if (top.target_tid != -1) |
997 | perf_event__synthesize_thread(top.target_tid, perf_event__process, | |
8115d60c | 998 | session); |
5b2bb75a | 999 | else |
8115d60c | 1000 | perf_event__synthesize_threads(perf_event__process, session); |
5b2bb75a | 1001 | |
8c3e10eb ACM |
1002 | start_counters(top.evlist); |
1003 | first = list_entry(top.evlist->entries.next, struct perf_evsel, node); | |
70db7533 | 1004 | perf_session__set_sample_type(session, first->attr.sample_type); |
07800601 | 1005 | |
2f01190a | 1006 | /* Wait for a minimal set of events before starting the snapshot */ |
8c3e10eb | 1007 | poll(top.evlist->pollfd, top.evlist->nr_fds, 100); |
2f01190a | 1008 | |
d8f66248 | 1009 | perf_session__mmap_read(session); |
2f01190a | 1010 | |
c0443df1 ACM |
1011 | if (pthread_create(&thread, NULL, (use_browser > 0 ? display_thread_tui : |
1012 | display_thread), session)) { | |
07800601 IM |
1013 | printf("Could not create display thread.\n"); |
1014 | exit(-1); | |
1015 | } | |
1016 | ||
1017 | if (realtime_prio) { | |
1018 | struct sched_param param; | |
1019 | ||
1020 | param.sched_priority = realtime_prio; | |
1021 | if (sched_setscheduler(0, SCHED_FIFO, ¶m)) { | |
1022 | printf("Could not set realtime priority.\n"); | |
1023 | exit(-1); | |
1024 | } | |
1025 | } | |
1026 | ||
1027 | while (1) { | |
8c3e10eb | 1028 | u64 hits = top.samples; |
07800601 | 1029 | |
d8f66248 | 1030 | perf_session__mmap_read(session); |
07800601 | 1031 | |
8c3e10eb ACM |
1032 | if (hits == top.samples) |
1033 | ret = poll(top.evlist->pollfd, top.evlist->nr_fds, 100); | |
07800601 IM |
1034 | } |
1035 | ||
1036 | return 0; | |
1037 | } | |
b456bae0 IM |
1038 | |
1039 | static const char * const top_usage[] = { | |
1040 | "perf top [<options>]", | |
1041 | NULL | |
1042 | }; | |
1043 | ||
b456bae0 | 1044 | static const struct option options[] = { |
8c3e10eb | 1045 | OPT_CALLBACK('e', "event", &top.evlist, "event", |
86847b62 TG |
1046 | "event selector. use 'perf list' to list available events", |
1047 | parse_events), | |
b456bae0 IM |
1048 | OPT_INTEGER('c', "count", &default_interval, |
1049 | "event period to sample"), | |
8c3e10eb | 1050 | OPT_INTEGER('p', "pid", &top.target_pid, |
d6d901c2 | 1051 | "profile events on existing process id"), |
8c3e10eb | 1052 | OPT_INTEGER('t', "tid", &top.target_tid, |
d6d901c2 | 1053 | "profile events on existing thread id"), |
b456bae0 IM |
1054 | OPT_BOOLEAN('a', "all-cpus", &system_wide, |
1055 | "system-wide collection from all CPUs"), | |
8c3e10eb | 1056 | OPT_STRING('C', "cpu", &top.cpu_list, "cpu", |
c45c6ea2 | 1057 | "list of cpus to monitor"), |
b32d133a ACM |
1058 | OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name, |
1059 | "file", "vmlinux pathname"), | |
8c3e10eb | 1060 | OPT_BOOLEAN('K', "hide_kernel_symbols", &top.hide_kernel_symbols, |
8ffcda17 | 1061 | "hide kernel symbols"), |
1967936d | 1062 | OPT_UINTEGER('m', "mmap-pages", &mmap_pages, "number of mmap data pages"), |
b456bae0 IM |
1063 | OPT_INTEGER('r', "realtime", &realtime_prio, |
1064 | "collect data with this RT SCHED_FIFO priority"), | |
8c3e10eb | 1065 | OPT_INTEGER('d', "delay", &top.delay_secs, |
b456bae0 IM |
1066 | "number of seconds to delay between refreshes"), |
1067 | OPT_BOOLEAN('D', "dump-symtab", &dump_symtab, | |
1068 | "dump the symbol table used for profiling"), | |
8c3e10eb | 1069 | OPT_INTEGER('f', "count-filter", &top.count_filter, |
b456bae0 IM |
1070 | "only display functions with more events than this"), |
1071 | OPT_BOOLEAN('g', "group", &group, | |
1072 | "put the counters into a counter group"), | |
0fdc7e67 MG |
1073 | OPT_BOOLEAN('i', "inherit", &inherit, |
1074 | "child tasks inherit counters"), | |
923c42c1 | 1075 | OPT_STRING('s', "sym-annotate", &sym_filter, "symbol name", |
6cff0e8d | 1076 | "symbol to annotate"), |
8c3e10eb | 1077 | OPT_BOOLEAN('z', "zero", &top.zero, |
b456bae0 | 1078 | "zero history across updates"), |
8c3e10eb | 1079 | OPT_INTEGER('F', "freq", &top.freq, |
b456bae0 | 1080 | "profile at this frequency"), |
8c3e10eb | 1081 | OPT_INTEGER('E', "entries", &top.print_entries, |
6e53cdf1 | 1082 | "display this many functions"), |
8c3e10eb | 1083 | OPT_BOOLEAN('U', "hide_user_symbols", &top.hide_user_symbols, |
8ffcda17 | 1084 | "hide user symbols"), |
c0443df1 ACM |
1085 | OPT_BOOLEAN(0, "tui", &use_tui, "Use the TUI interface"), |
1086 | OPT_BOOLEAN(0, "stdio", &use_stdio, "Use the stdio interface"), | |
c0555642 | 1087 | OPT_INCR('v', "verbose", &verbose, |
3da297a6 | 1088 | "be more verbose (show counter open errors, etc)"), |
b456bae0 IM |
1089 | OPT_END() |
1090 | }; | |
1091 | ||
f37a291c | 1092 | int cmd_top(int argc, const char **argv, const char *prefix __used) |
b456bae0 | 1093 | { |
69aad6f1 ACM |
1094 | struct perf_evsel *pos; |
1095 | int status = -ENOMEM; | |
b456bae0 | 1096 | |
8c3e10eb ACM |
1097 | top.evlist = perf_evlist__new(NULL, NULL); |
1098 | if (top.evlist == NULL) | |
361c99a6 ACM |
1099 | return -ENOMEM; |
1100 | ||
b456bae0 IM |
1101 | page_size = sysconf(_SC_PAGE_SIZE); |
1102 | ||
b456bae0 IM |
1103 | argc = parse_options(argc, argv, options, top_usage, 0); |
1104 | if (argc) | |
1105 | usage_with_options(top_usage, options); | |
1106 | ||
c0443df1 ACM |
1107 | /* |
1108 | * XXX For now start disabled, only using TUI if explicitely asked for. | |
1109 | * Change that when handle_keys equivalent gets written, live annotation | |
1110 | * done, etc. | |
1111 | */ | |
1112 | use_browser = 0; | |
1113 | ||
1114 | if (use_stdio) | |
1115 | use_browser = 0; | |
1116 | else if (use_tui) | |
1117 | use_browser = 1; | |
1118 | ||
1119 | setup_browser(false); | |
1120 | ||
b456bae0 | 1121 | /* CPU and PID are mutually exclusive */ |
8c3e10eb | 1122 | if (top.target_tid > 0 && top.cpu_list) { |
b456bae0 IM |
1123 | printf("WARNING: PID switch overriding CPU\n"); |
1124 | sleep(1); | |
8c3e10eb | 1125 | top.cpu_list = NULL; |
b456bae0 IM |
1126 | } |
1127 | ||
8c3e10eb ACM |
1128 | if (top.target_pid != -1) |
1129 | top.target_tid = top.target_pid; | |
7e2ed097 | 1130 | |
8c3e10eb ACM |
1131 | if (perf_evlist__create_maps(top.evlist, top.target_pid, |
1132 | top.target_tid, top.cpu_list) < 0) | |
7e2ed097 ACM |
1133 | usage_with_options(top_usage, options); |
1134 | ||
8c3e10eb ACM |
1135 | if (!top.evlist->nr_entries && |
1136 | perf_evlist__add_default(top.evlist) < 0) { | |
69aad6f1 ACM |
1137 | pr_err("Not enough memory for event selector list\n"); |
1138 | return -ENOMEM; | |
1139 | } | |
5a8e5a30 | 1140 | |
8c3e10eb ACM |
1141 | if (top.delay_secs < 1) |
1142 | top.delay_secs = 1; | |
2f335a02 | 1143 | |
7e4ff9e3 MG |
1144 | /* |
1145 | * User specified count overrides default frequency. | |
1146 | */ | |
1147 | if (default_interval) | |
8c3e10eb ACM |
1148 | top.freq = 0; |
1149 | else if (top.freq) { | |
1150 | default_interval = top.freq; | |
7e4ff9e3 MG |
1151 | } else { |
1152 | fprintf(stderr, "frequency and count are zero, aborting\n"); | |
1153 | exit(EXIT_FAILURE); | |
1154 | } | |
1155 | ||
8c3e10eb ACM |
1156 | list_for_each_entry(pos, &top.evlist->entries, node) { |
1157 | if (perf_evsel__alloc_fd(pos, top.evlist->cpus->nr, | |
1158 | top.evlist->threads->nr) < 0) | |
69aad6f1 ACM |
1159 | goto out_free_fd; |
1160 | /* | |
1161 | * Fill in the ones not specifically initialized via -c: | |
1162 | */ | |
1163 | if (pos->attr.sample_period) | |
1164 | continue; | |
1165 | ||
1166 | pos->attr.sample_period = default_interval; | |
1167 | } | |
1168 | ||
8c3e10eb ACM |
1169 | if (perf_evlist__alloc_pollfd(top.evlist) < 0 || |
1170 | perf_evlist__alloc_mmap(top.evlist) < 0) | |
5c581041 ACM |
1171 | goto out_free_fd; |
1172 | ||
8c3e10eb | 1173 | top.sym_evsel = list_entry(top.evlist->entries.next, struct perf_evsel, node); |
cc841580 | 1174 | |
69aad6f1 | 1175 | symbol_conf.priv_size = (sizeof(struct sym_entry) + |
8c3e10eb | 1176 | (top.evlist->nr_entries + 1) * sizeof(unsigned long)); |
69aad6f1 ACM |
1177 | |
1178 | symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL); | |
1179 | if (symbol__init() < 0) | |
1180 | return -1; | |
1181 | ||
13cc5079 | 1182 | get_term_dimensions(&winsize); |
8c3e10eb | 1183 | if (top.print_entries == 0) { |
13cc5079 | 1184 | update_print_entries(&winsize); |
3b6ed988 ACM |
1185 | signal(SIGWINCH, sig_winch_handler); |
1186 | } | |
1187 | ||
69aad6f1 ACM |
1188 | status = __cmd_top(); |
1189 | out_free_fd: | |
8c3e10eb | 1190 | perf_evlist__delete(top.evlist); |
69aad6f1 ACM |
1191 | |
1192 | return status; | |
b456bae0 | 1193 | } |