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