]>
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> | |
ab81f3fd | 8 | * 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com> |
bf9e1876 IM |
9 | * |
10 | * Improvements and fixes by: | |
11 | * | |
12 | * Arjan van de Ven <arjan@linux.intel.com> | |
13 | * Yanmin Zhang <yanmin.zhang@intel.com> | |
14 | * Wu Fengguang <fengguang.wu@intel.com> | |
15 | * Mike Galbraith <efault@gmx.de> | |
16 | * Paul Mackerras <paulus@samba.org> | |
17 | * | |
18 | * Released under the GPL v2. (and only v2, not any later version) | |
07800601 | 19 | */ |
bf9e1876 | 20 | #include "builtin.h" |
07800601 | 21 | |
1a482f38 | 22 | #include "perf.h" |
bf9e1876 | 23 | |
36532461 | 24 | #include "util/annotate.h" |
c0443df1 | 25 | #include "util/cache.h" |
8fc0321f | 26 | #include "util/color.h" |
361c99a6 | 27 | #include "util/evlist.h" |
69aad6f1 | 28 | #include "util/evsel.h" |
b0a7d1a0 | 29 | #include "util/machine.h" |
b3165f41 ACM |
30 | #include "util/session.h" |
31 | #include "util/symbol.h" | |
439d473b | 32 | #include "util/thread.h" |
fd78260b | 33 | #include "util/thread_map.h" |
8c3e10eb | 34 | #include "util/top.h" |
148be2c1 | 35 | #include "util/util.h" |
43cbcd8a | 36 | #include <linux/rbtree.h> |
b456bae0 IM |
37 | #include "util/parse-options.h" |
38 | #include "util/parse-events.h" | |
a12b51c4 | 39 | #include "util/cpumap.h" |
69aad6f1 | 40 | #include "util/xyarray.h" |
ab81f3fd | 41 | #include "util/sort.h" |
6b118e92 | 42 | #include "util/intlist.h" |
07800601 | 43 | |
8f28827a FW |
44 | #include "util/debug.h" |
45 | ||
07800601 | 46 | #include <assert.h> |
31d68e7b | 47 | #include <elf.h> |
07800601 | 48 | #include <fcntl.h> |
0e9b20b8 | 49 | |
07800601 | 50 | #include <stdio.h> |
923c42c1 MG |
51 | #include <termios.h> |
52 | #include <unistd.h> | |
9486aa38 | 53 | #include <inttypes.h> |
0e9b20b8 | 54 | |
07800601 | 55 | #include <errno.h> |
07800601 IM |
56 | #include <time.h> |
57 | #include <sched.h> | |
07800601 IM |
58 | |
59 | #include <sys/syscall.h> | |
60 | #include <sys/ioctl.h> | |
61 | #include <sys/poll.h> | |
62 | #include <sys/prctl.h> | |
63 | #include <sys/wait.h> | |
64 | #include <sys/uio.h> | |
31d68e7b | 65 | #include <sys/utsname.h> |
07800601 IM |
66 | #include <sys/mman.h> |
67 | ||
68 | #include <linux/unistd.h> | |
69 | #include <linux/types.h> | |
70 | ||
11859e82 ACM |
71 | static volatile int done; |
72 | ||
933cbb1c NK |
73 | #define HEADER_LINE_NR 5 |
74 | ||
1758af10 | 75 | static void perf_top__update_print_entries(struct perf_top *top) |
3b6ed988 | 76 | { |
933cbb1c | 77 | top->print_entries = top->winsize.ws_row - HEADER_LINE_NR; |
3b6ed988 ACM |
78 | } |
79 | ||
1d037ca1 IT |
80 | static void perf_top__sig_winch(int sig __maybe_unused, |
81 | siginfo_t *info __maybe_unused, void *arg) | |
3b6ed988 | 82 | { |
1758af10 ACM |
83 | struct perf_top *top = arg; |
84 | ||
85 | get_term_dimensions(&top->winsize); | |
86 | perf_top__update_print_entries(top); | |
3b6ed988 ACM |
87 | } |
88 | ||
1758af10 | 89 | static int perf_top__parse_source(struct perf_top *top, struct hist_entry *he) |
923c42c1 MG |
90 | { |
91 | struct symbol *sym; | |
ce6f4fab | 92 | struct annotation *notes; |
439d473b | 93 | struct map *map; |
36532461 | 94 | int err = -1; |
923c42c1 | 95 | |
ab81f3fd | 96 | if (!he || !he->ms.sym) |
b0a9ab62 ACM |
97 | return -1; |
98 | ||
ab81f3fd ACM |
99 | sym = he->ms.sym; |
100 | map = he->ms.map; | |
b0a9ab62 ACM |
101 | |
102 | /* | |
103 | * We can't annotate with just /proc/kallsyms | |
104 | */ | |
44f24cb3 | 105 | if (map->dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS) { |
ce6f4fab ACM |
106 | pr_err("Can't annotate %s: No vmlinux file was found in the " |
107 | "path\n", sym->name); | |
108 | sleep(1); | |
b0a9ab62 | 109 | return -1; |
b269876c ACM |
110 | } |
111 | ||
ce6f4fab ACM |
112 | notes = symbol__annotation(sym); |
113 | if (notes->src != NULL) { | |
114 | pthread_mutex_lock(¬es->lock); | |
923c42c1 MG |
115 | goto out_assign; |
116 | } | |
923c42c1 | 117 | |
ce6f4fab | 118 | pthread_mutex_lock(¬es->lock); |
923c42c1 | 119 | |
d04b35f8 | 120 | if (symbol__alloc_hist(sym) < 0) { |
c97cf422 | 121 | pthread_mutex_unlock(¬es->lock); |
36532461 ACM |
122 | pr_err("Not enough memory for annotating '%s' symbol!\n", |
123 | sym->name); | |
ce6f4fab | 124 | sleep(1); |
c97cf422 | 125 | return err; |
923c42c1 | 126 | } |
36532461 | 127 | |
ab81f3fd | 128 | err = symbol__annotate(sym, map, 0); |
36532461 | 129 | if (err == 0) { |
923c42c1 | 130 | out_assign: |
1758af10 | 131 | top->sym_filter_entry = he; |
36532461 | 132 | } |
c97cf422 | 133 | |
ce6f4fab | 134 | pthread_mutex_unlock(¬es->lock); |
36532461 | 135 | return err; |
923c42c1 MG |
136 | } |
137 | ||
ab81f3fd | 138 | static void __zero_source_counters(struct hist_entry *he) |
923c42c1 | 139 | { |
ab81f3fd | 140 | struct symbol *sym = he->ms.sym; |
36532461 | 141 | symbol__annotate_zero_histograms(sym); |
923c42c1 MG |
142 | } |
143 | ||
31d68e7b ACM |
144 | static void ui__warn_map_erange(struct map *map, struct symbol *sym, u64 ip) |
145 | { | |
146 | struct utsname uts; | |
147 | int err = uname(&uts); | |
148 | ||
149 | ui__warning("Out of bounds address found:\n\n" | |
150 | "Addr: %" PRIx64 "\n" | |
151 | "DSO: %s %c\n" | |
152 | "Map: %" PRIx64 "-%" PRIx64 "\n" | |
153 | "Symbol: %" PRIx64 "-%" PRIx64 " %c %s\n" | |
154 | "Arch: %s\n" | |
155 | "Kernel: %s\n" | |
156 | "Tools: %s\n\n" | |
157 | "Not all samples will be on the annotation output.\n\n" | |
158 | "Please report to linux-kernel@vger.kernel.org\n", | |
159 | ip, map->dso->long_name, dso__symtab_origin(map->dso), | |
160 | map->start, map->end, sym->start, sym->end, | |
161 | sym->binding == STB_GLOBAL ? 'g' : | |
162 | sym->binding == STB_LOCAL ? 'l' : 'w', sym->name, | |
163 | err ? "[unknown]" : uts.machine, | |
164 | err ? "[unknown]" : uts.release, perf_version_string); | |
165 | if (use_browser <= 0) | |
166 | sleep(5); | |
167 | ||
168 | map->erange_warned = true; | |
169 | } | |
170 | ||
1758af10 ACM |
171 | static void perf_top__record_precise_ip(struct perf_top *top, |
172 | struct hist_entry *he, | |
173 | int counter, u64 ip) | |
923c42c1 | 174 | { |
ce6f4fab ACM |
175 | struct annotation *notes; |
176 | struct symbol *sym; | |
31d68e7b | 177 | int err; |
ce6f4fab | 178 | |
ab81f3fd | 179 | if (he == NULL || he->ms.sym == NULL || |
1758af10 ACM |
180 | ((top->sym_filter_entry == NULL || |
181 | top->sym_filter_entry->ms.sym != he->ms.sym) && use_browser != 1)) | |
923c42c1 MG |
182 | return; |
183 | ||
ab81f3fd | 184 | sym = he->ms.sym; |
ce6f4fab ACM |
185 | notes = symbol__annotation(sym); |
186 | ||
187 | if (pthread_mutex_trylock(¬es->lock)) | |
923c42c1 MG |
188 | return; |
189 | ||
d04b35f8 | 190 | if (notes->src == NULL && symbol__alloc_hist(sym) < 0) { |
ab81f3fd ACM |
191 | pthread_mutex_unlock(¬es->lock); |
192 | pr_err("Not enough memory for annotating '%s' symbol!\n", | |
193 | sym->name); | |
194 | sleep(1); | |
195 | return; | |
196 | } | |
197 | ||
198 | ip = he->ms.map->map_ip(he->ms.map, ip); | |
31d68e7b | 199 | err = symbol__inc_addr_samples(sym, he->ms.map, counter, ip); |
c7ad21af | 200 | |
ce6f4fab | 201 | pthread_mutex_unlock(¬es->lock); |
31d68e7b ACM |
202 | |
203 | if (err == -ERANGE && !he->ms.map->erange_warned) | |
204 | ui__warn_map_erange(he->ms.map, sym, ip); | |
923c42c1 MG |
205 | } |
206 | ||
1758af10 | 207 | static void perf_top__show_details(struct perf_top *top) |
923c42c1 | 208 | { |
1758af10 | 209 | struct hist_entry *he = top->sym_filter_entry; |
ce6f4fab | 210 | struct annotation *notes; |
923c42c1 | 211 | struct symbol *symbol; |
36532461 | 212 | int more; |
923c42c1 | 213 | |
ab81f3fd | 214 | if (!he) |
923c42c1 MG |
215 | return; |
216 | ||
ab81f3fd | 217 | symbol = he->ms.sym; |
ce6f4fab ACM |
218 | notes = symbol__annotation(symbol); |
219 | ||
220 | pthread_mutex_lock(¬es->lock); | |
221 | ||
222 | if (notes->src == NULL) | |
223 | goto out_unlock; | |
923c42c1 | 224 | |
7289f83c | 225 | printf("Showing %s for %s\n", perf_evsel__name(top->sym_evsel), symbol->name); |
1758af10 | 226 | printf(" Events Pcnt (>=%d%%)\n", top->sym_pcnt_filter); |
923c42c1 | 227 | |
db8fd07a | 228 | more = symbol__annotate_printf(symbol, he->ms.map, top->sym_evsel, |
1758af10 ACM |
229 | 0, top->sym_pcnt_filter, top->print_entries, 4); |
230 | if (top->zero) | |
231 | symbol__annotate_zero_histogram(symbol, top->sym_evsel->idx); | |
36532461 | 232 | else |
1758af10 | 233 | symbol__annotate_decay_histogram(symbol, top->sym_evsel->idx); |
36532461 | 234 | if (more != 0) |
923c42c1 | 235 | printf("%d lines not displayed, maybe increase display entries [e]\n", more); |
ce6f4fab ACM |
236 | out_unlock: |
237 | pthread_mutex_unlock(¬es->lock); | |
923c42c1 | 238 | } |
07800601 | 239 | |
07800601 IM |
240 | static const char CONSOLE_CLEAR[] = "\e[H\e[2J"; |
241 | ||
743eb868 ACM |
242 | static struct hist_entry *perf_evsel__add_hist_entry(struct perf_evsel *evsel, |
243 | struct addr_location *al, | |
244 | struct perf_sample *sample) | |
de04687f | 245 | { |
ab81f3fd ACM |
246 | struct hist_entry *he; |
247 | ||
05484298 AK |
248 | he = __hists__add_entry(&evsel->hists, al, NULL, sample->period, |
249 | sample->weight); | |
ab81f3fd ACM |
250 | if (he == NULL) |
251 | return NULL; | |
252 | ||
ab81f3fd ACM |
253 | hists__inc_nr_events(&evsel->hists, PERF_RECORD_SAMPLE); |
254 | return he; | |
de04687f | 255 | } |
07800601 | 256 | |
1758af10 | 257 | static void perf_top__print_sym_table(struct perf_top *top) |
07800601 | 258 | { |
8c3e10eb ACM |
259 | char bf[160]; |
260 | int printed = 0; | |
1758af10 | 261 | const int win_width = top->winsize.ws_col - 1; |
d94b9430 | 262 | |
0f5486b5 | 263 | puts(CONSOLE_CLEAR); |
07800601 | 264 | |
1758af10 | 265 | perf_top__header_snprintf(top, bf, sizeof(bf)); |
8c3e10eb | 266 | printf("%s\n", bf); |
07800601 | 267 | |
1758af10 | 268 | perf_top__reset_sample_counters(top); |
07800601 | 269 | |
1a105f74 | 270 | printf("%-*.*s\n", win_width, win_width, graph_dotted_line); |
07800601 | 271 | |
1758af10 ACM |
272 | if (top->sym_evsel->hists.stats.nr_lost_warned != |
273 | top->sym_evsel->hists.stats.nr_events[PERF_RECORD_LOST]) { | |
274 | top->sym_evsel->hists.stats.nr_lost_warned = | |
275 | top->sym_evsel->hists.stats.nr_events[PERF_RECORD_LOST]; | |
7b27509f ACM |
276 | color_fprintf(stdout, PERF_COLOR_RED, |
277 | "WARNING: LOST %d chunks, Check IO/CPU overload", | |
1758af10 | 278 | top->sym_evsel->hists.stats.nr_lost_warned); |
ab81f3fd | 279 | ++printed; |
93fc64f1 ACM |
280 | } |
281 | ||
1758af10 ACM |
282 | if (top->sym_filter_entry) { |
283 | perf_top__show_details(top); | |
923c42c1 MG |
284 | return; |
285 | } | |
286 | ||
3a5714f8 NK |
287 | hists__collapse_resort(&top->sym_evsel->hists); |
288 | hists__output_resort(&top->sym_evsel->hists); | |
289 | hists__decay_entries(&top->sym_evsel->hists, | |
290 | top->hide_user_symbols, | |
291 | top->hide_kernel_symbols); | |
1758af10 | 292 | hists__output_recalc_col_len(&top->sym_evsel->hists, |
933cbb1c | 293 | top->print_entries - printed); |
7cc017ed | 294 | putchar('\n'); |
41724e4c | 295 | hists__fprintf(&top->sym_evsel->hists, false, |
933cbb1c | 296 | top->print_entries - printed, win_width, stdout); |
07800601 IM |
297 | } |
298 | ||
923c42c1 MG |
299 | static void prompt_integer(int *target, const char *msg) |
300 | { | |
301 | char *buf = malloc(0), *p; | |
302 | size_t dummy = 0; | |
303 | int tmp; | |
304 | ||
305 | fprintf(stdout, "\n%s: ", msg); | |
306 | if (getline(&buf, &dummy, stdin) < 0) | |
307 | return; | |
308 | ||
309 | p = strchr(buf, '\n'); | |
310 | if (p) | |
311 | *p = 0; | |
312 | ||
313 | p = buf; | |
314 | while(*p) { | |
315 | if (!isdigit(*p)) | |
316 | goto out_free; | |
317 | p++; | |
318 | } | |
319 | tmp = strtoul(buf, NULL, 10); | |
320 | *target = tmp; | |
321 | out_free: | |
322 | free(buf); | |
323 | } | |
324 | ||
325 | static void prompt_percent(int *target, const char *msg) | |
326 | { | |
327 | int tmp = 0; | |
328 | ||
329 | prompt_integer(&tmp, msg); | |
330 | if (tmp >= 0 && tmp <= 100) | |
331 | *target = tmp; | |
332 | } | |
333 | ||
1758af10 | 334 | static void perf_top__prompt_symbol(struct perf_top *top, const char *msg) |
923c42c1 MG |
335 | { |
336 | char *buf = malloc(0), *p; | |
1758af10 | 337 | struct hist_entry *syme = top->sym_filter_entry, *n, *found = NULL; |
ab81f3fd | 338 | struct rb_node *next; |
923c42c1 MG |
339 | size_t dummy = 0; |
340 | ||
341 | /* zero counters of active symbol */ | |
342 | if (syme) { | |
923c42c1 | 343 | __zero_source_counters(syme); |
1758af10 | 344 | top->sym_filter_entry = NULL; |
923c42c1 MG |
345 | } |
346 | ||
347 | fprintf(stdout, "\n%s: ", msg); | |
348 | if (getline(&buf, &dummy, stdin) < 0) | |
349 | goto out_free; | |
350 | ||
351 | p = strchr(buf, '\n'); | |
352 | if (p) | |
353 | *p = 0; | |
354 | ||
1758af10 | 355 | next = rb_first(&top->sym_evsel->hists.entries); |
ab81f3fd ACM |
356 | while (next) { |
357 | n = rb_entry(next, struct hist_entry, rb_node); | |
358 | if (n->ms.sym && !strcmp(buf, n->ms.sym->name)) { | |
359 | found = n; | |
923c42c1 MG |
360 | break; |
361 | } | |
ab81f3fd | 362 | next = rb_next(&n->rb_node); |
923c42c1 MG |
363 | } |
364 | ||
365 | if (!found) { | |
66aeb6d5 | 366 | fprintf(stderr, "Sorry, %s is not active.\n", buf); |
923c42c1 | 367 | sleep(1); |
923c42c1 | 368 | } else |
1758af10 | 369 | perf_top__parse_source(top, found); |
923c42c1 MG |
370 | |
371 | out_free: | |
372 | free(buf); | |
373 | } | |
374 | ||
1758af10 | 375 | static void perf_top__print_mapped_keys(struct perf_top *top) |
923c42c1 | 376 | { |
091bd2e9 MG |
377 | char *name = NULL; |
378 | ||
1758af10 ACM |
379 | if (top->sym_filter_entry) { |
380 | struct symbol *sym = top->sym_filter_entry->ms.sym; | |
091bd2e9 MG |
381 | name = sym->name; |
382 | } | |
383 | ||
384 | fprintf(stdout, "\nMapped keys:\n"); | |
1758af10 ACM |
385 | fprintf(stdout, "\t[d] display refresh delay. \t(%d)\n", top->delay_secs); |
386 | fprintf(stdout, "\t[e] display entries (lines). \t(%d)\n", top->print_entries); | |
091bd2e9 | 387 | |
1758af10 | 388 | if (top->evlist->nr_entries > 1) |
7289f83c | 389 | fprintf(stdout, "\t[E] active event counter. \t(%s)\n", perf_evsel__name(top->sym_evsel)); |
091bd2e9 | 390 | |
1758af10 | 391 | fprintf(stdout, "\t[f] profile display filter (count). \t(%d)\n", top->count_filter); |
091bd2e9 | 392 | |
1758af10 | 393 | fprintf(stdout, "\t[F] annotate display filter (percent). \t(%d%%)\n", top->sym_pcnt_filter); |
6cff0e8d KS |
394 | fprintf(stdout, "\t[s] annotate symbol. \t(%s)\n", name?: "NULL"); |
395 | fprintf(stdout, "\t[S] stop annotation.\n"); | |
091bd2e9 | 396 | |
8ffcda17 | 397 | fprintf(stdout, |
1a72cfa6 | 398 | "\t[K] hide kernel_symbols symbols. \t(%s)\n", |
1758af10 | 399 | top->hide_kernel_symbols ? "yes" : "no"); |
8ffcda17 ACM |
400 | fprintf(stdout, |
401 | "\t[U] hide user symbols. \t(%s)\n", | |
1758af10 ACM |
402 | top->hide_user_symbols ? "yes" : "no"); |
403 | fprintf(stdout, "\t[z] toggle sample zeroing. \t(%d)\n", top->zero ? 1 : 0); | |
091bd2e9 MG |
404 | fprintf(stdout, "\t[qQ] quit.\n"); |
405 | } | |
406 | ||
1758af10 | 407 | static int perf_top__key_mapped(struct perf_top *top, int c) |
091bd2e9 MG |
408 | { |
409 | switch (c) { | |
410 | case 'd': | |
411 | case 'e': | |
412 | case 'f': | |
413 | case 'z': | |
414 | case 'q': | |
415 | case 'Q': | |
8ffcda17 ACM |
416 | case 'K': |
417 | case 'U': | |
6cff0e8d KS |
418 | case 'F': |
419 | case 's': | |
420 | case 'S': | |
091bd2e9 MG |
421 | return 1; |
422 | case 'E': | |
1758af10 | 423 | return top->evlist->nr_entries > 1 ? 1 : 0; |
83a0944f IM |
424 | default: |
425 | break; | |
091bd2e9 MG |
426 | } |
427 | ||
428 | return 0; | |
923c42c1 MG |
429 | } |
430 | ||
11859e82 | 431 | static bool perf_top__handle_keypress(struct perf_top *top, int c) |
923c42c1 | 432 | { |
11859e82 ACM |
433 | bool ret = true; |
434 | ||
1758af10 | 435 | if (!perf_top__key_mapped(top, c)) { |
091bd2e9 MG |
436 | struct pollfd stdin_poll = { .fd = 0, .events = POLLIN }; |
437 | struct termios tc, save; | |
438 | ||
1758af10 | 439 | perf_top__print_mapped_keys(top); |
091bd2e9 MG |
440 | fprintf(stdout, "\nEnter selection, or unmapped key to continue: "); |
441 | fflush(stdout); | |
442 | ||
443 | tcgetattr(0, &save); | |
444 | tc = save; | |
445 | tc.c_lflag &= ~(ICANON | ECHO); | |
446 | tc.c_cc[VMIN] = 0; | |
447 | tc.c_cc[VTIME] = 0; | |
448 | tcsetattr(0, TCSANOW, &tc); | |
449 | ||
450 | poll(&stdin_poll, 1, -1); | |
451 | c = getc(stdin); | |
452 | ||
453 | tcsetattr(0, TCSAFLUSH, &save); | |
1758af10 | 454 | if (!perf_top__key_mapped(top, c)) |
11859e82 | 455 | return ret; |
091bd2e9 MG |
456 | } |
457 | ||
923c42c1 MG |
458 | switch (c) { |
459 | case 'd': | |
1758af10 ACM |
460 | prompt_integer(&top->delay_secs, "Enter display delay"); |
461 | if (top->delay_secs < 1) | |
462 | top->delay_secs = 1; | |
923c42c1 MG |
463 | break; |
464 | case 'e': | |
1758af10 ACM |
465 | prompt_integer(&top->print_entries, "Enter display entries (lines)"); |
466 | if (top->print_entries == 0) { | |
467 | struct sigaction act = { | |
468 | .sa_sigaction = perf_top__sig_winch, | |
469 | .sa_flags = SA_SIGINFO, | |
470 | }; | |
471 | perf_top__sig_winch(SIGWINCH, NULL, top); | |
472 | sigaction(SIGWINCH, &act, NULL); | |
509605db | 473 | } else { |
3b6ed988 | 474 | signal(SIGWINCH, SIG_DFL); |
509605db | 475 | } |
923c42c1 MG |
476 | break; |
477 | case 'E': | |
1758af10 | 478 | if (top->evlist->nr_entries > 1) { |
ce2d17ca AN |
479 | /* Select 0 as the default event: */ |
480 | int counter = 0; | |
481 | ||
923c42c1 | 482 | fprintf(stderr, "\nAvailable events:"); |
69aad6f1 | 483 | |
1758af10 | 484 | list_for_each_entry(top->sym_evsel, &top->evlist->entries, node) |
7289f83c | 485 | fprintf(stderr, "\n\t%d %s", top->sym_evsel->idx, perf_evsel__name(top->sym_evsel)); |
923c42c1 | 486 | |
ec52d976 | 487 | prompt_integer(&counter, "Enter details event counter"); |
923c42c1 | 488 | |
1758af10 | 489 | if (counter >= top->evlist->nr_entries) { |
0c21f736 | 490 | top->sym_evsel = perf_evlist__first(top->evlist); |
7289f83c | 491 | fprintf(stderr, "Sorry, no such event, using %s.\n", perf_evsel__name(top->sym_evsel)); |
923c42c1 | 492 | sleep(1); |
69aad6f1 | 493 | break; |
923c42c1 | 494 | } |
1758af10 ACM |
495 | list_for_each_entry(top->sym_evsel, &top->evlist->entries, node) |
496 | if (top->sym_evsel->idx == counter) | |
69aad6f1 | 497 | break; |
ec52d976 | 498 | } else |
0c21f736 | 499 | top->sym_evsel = perf_evlist__first(top->evlist); |
923c42c1 MG |
500 | break; |
501 | case 'f': | |
1758af10 | 502 | prompt_integer(&top->count_filter, "Enter display event count filter"); |
923c42c1 MG |
503 | break; |
504 | case 'F': | |
1758af10 ACM |
505 | prompt_percent(&top->sym_pcnt_filter, |
506 | "Enter details display event filter (percent)"); | |
923c42c1 | 507 | break; |
8ffcda17 | 508 | case 'K': |
1758af10 | 509 | top->hide_kernel_symbols = !top->hide_kernel_symbols; |
8ffcda17 | 510 | break; |
923c42c1 MG |
511 | case 'q': |
512 | case 'Q': | |
513 | printf("exiting.\n"); | |
1758af10 ACM |
514 | if (top->dump_symtab) |
515 | perf_session__fprintf_dsos(top->session, stderr); | |
11859e82 ACM |
516 | ret = false; |
517 | break; | |
923c42c1 | 518 | case 's': |
1758af10 | 519 | perf_top__prompt_symbol(top, "Enter details symbol"); |
923c42c1 MG |
520 | break; |
521 | case 'S': | |
1758af10 | 522 | if (!top->sym_filter_entry) |
923c42c1 MG |
523 | break; |
524 | else { | |
1758af10 | 525 | struct hist_entry *syme = top->sym_filter_entry; |
923c42c1 | 526 | |
1758af10 | 527 | top->sym_filter_entry = NULL; |
923c42c1 | 528 | __zero_source_counters(syme); |
923c42c1 MG |
529 | } |
530 | break; | |
8ffcda17 | 531 | case 'U': |
1758af10 | 532 | top->hide_user_symbols = !top->hide_user_symbols; |
8ffcda17 | 533 | break; |
923c42c1 | 534 | case 'z': |
1758af10 | 535 | top->zero = !top->zero; |
923c42c1 | 536 | break; |
83a0944f IM |
537 | default: |
538 | break; | |
923c42c1 | 539 | } |
11859e82 ACM |
540 | |
541 | return ret; | |
923c42c1 MG |
542 | } |
543 | ||
ab81f3fd ACM |
544 | static void perf_top__sort_new_samples(void *arg) |
545 | { | |
546 | struct perf_top *t = arg; | |
547 | perf_top__reset_sample_counters(t); | |
548 | ||
549 | if (t->evlist->selected != NULL) | |
550 | t->sym_evsel = t->evlist->selected; | |
551 | ||
3a5714f8 NK |
552 | hists__collapse_resort(&t->sym_evsel->hists); |
553 | hists__output_resort(&t->sym_evsel->hists); | |
554 | hists__decay_entries(&t->sym_evsel->hists, | |
555 | t->hide_user_symbols, | |
556 | t->hide_kernel_symbols); | |
ab81f3fd ACM |
557 | } |
558 | ||
1758af10 | 559 | static void *display_thread_tui(void *arg) |
c0443df1 | 560 | { |
0d37aa34 | 561 | struct perf_evsel *pos; |
1758af10 | 562 | struct perf_top *top = arg; |
ab81f3fd | 563 | const char *help = "For a higher level overview, try: perf top --sort comm,dso"; |
9783adf7 NK |
564 | struct hist_browser_timer hbt = { |
565 | .timer = perf_top__sort_new_samples, | |
566 | .arg = top, | |
567 | .refresh = top->delay_secs, | |
568 | }; | |
ab81f3fd | 569 | |
1758af10 | 570 | perf_top__sort_new_samples(top); |
0d37aa34 ACM |
571 | |
572 | /* | |
573 | * Initialize the uid_filter_str, in the future the TUI will allow | |
574 | * Zooming in/out UIDs. For now juse use whatever the user passed | |
575 | * via --uid. | |
576 | */ | |
577 | list_for_each_entry(pos, &top->evlist->entries, node) | |
2376c67a | 578 | pos->hists.uid_filter_str = top->record_opts.target.uid_str; |
0d37aa34 | 579 | |
68d80758 NK |
580 | perf_evlist__tui_browse_hists(top->evlist, help, &hbt, |
581 | &top->session->header.env); | |
ab81f3fd | 582 | |
11859e82 | 583 | done = 1; |
c0443df1 ACM |
584 | return NULL; |
585 | } | |
586 | ||
1758af10 | 587 | static void *display_thread(void *arg) |
07800601 | 588 | { |
0f5486b5 | 589 | struct pollfd stdin_poll = { .fd = 0, .events = POLLIN }; |
923c42c1 | 590 | struct termios tc, save; |
1758af10 | 591 | struct perf_top *top = arg; |
923c42c1 MG |
592 | int delay_msecs, c; |
593 | ||
594 | tcgetattr(0, &save); | |
595 | tc = save; | |
596 | tc.c_lflag &= ~(ICANON | ECHO); | |
597 | tc.c_cc[VMIN] = 0; | |
598 | tc.c_cc[VTIME] = 0; | |
091bd2e9 | 599 | |
3af6e338 | 600 | pthread__unblock_sigwinch(); |
923c42c1 | 601 | repeat: |
1758af10 | 602 | delay_msecs = top->delay_secs * 1000; |
923c42c1 MG |
603 | tcsetattr(0, TCSANOW, &tc); |
604 | /* trash return*/ | |
605 | getc(stdin); | |
07800601 | 606 | |
11859e82 | 607 | while (!done) { |
1758af10 | 608 | perf_top__print_sym_table(top); |
3af6e338 ACM |
609 | /* |
610 | * Either timeout expired or we got an EINTR due to SIGWINCH, | |
611 | * refresh screen in both cases. | |
612 | */ | |
613 | switch (poll(&stdin_poll, 1, delay_msecs)) { | |
614 | case 0: | |
615 | continue; | |
616 | case -1: | |
617 | if (errno == EINTR) | |
618 | continue; | |
619 | /* Fall trhu */ | |
620 | default: | |
11859e82 ACM |
621 | c = getc(stdin); |
622 | tcsetattr(0, TCSAFLUSH, &save); | |
623 | ||
624 | if (perf_top__handle_keypress(top, c)) | |
625 | goto repeat; | |
626 | done = 1; | |
3af6e338 ACM |
627 | } |
628 | } | |
07800601 IM |
629 | |
630 | return NULL; | |
631 | } | |
632 | ||
2ab52083 | 633 | /* Tag samples to be skipped. */ |
f37a291c | 634 | static const char *skip_symbols[] = { |
4bea8b5c | 635 | "intel_idle", |
2ab52083 | 636 | "default_idle", |
b0e8572f | 637 | "native_safe_halt", |
2ab52083 AB |
638 | "cpu_idle", |
639 | "enter_idle", | |
640 | "exit_idle", | |
641 | "mwait_idle", | |
59b90056 | 642 | "mwait_idle_with_hints", |
8357275b | 643 | "poll_idle", |
3a3393ef AB |
644 | "ppc64_runlatch_off", |
645 | "pseries_dedicated_idle_sleep", | |
2ab52083 AB |
646 | NULL |
647 | }; | |
648 | ||
1d037ca1 | 649 | static int symbol_filter(struct map *map __maybe_unused, struct symbol *sym) |
07800601 | 650 | { |
de04687f | 651 | const char *name = sym->name; |
2ab52083 | 652 | int i; |
de04687f | 653 | |
3a3393ef AB |
654 | /* |
655 | * ppc64 uses function descriptors and appends a '.' to the | |
656 | * start of every instruction address. Remove it. | |
657 | */ | |
658 | if (name[0] == '.') | |
659 | name++; | |
660 | ||
de04687f ACM |
661 | if (!strcmp(name, "_text") || |
662 | !strcmp(name, "_etext") || | |
663 | !strcmp(name, "_sinittext") || | |
664 | !strncmp("init_module", name, 11) || | |
665 | !strncmp("cleanup_module", name, 14) || | |
666 | strstr(name, "_text_start") || | |
667 | strstr(name, "_text_end")) | |
07800601 | 668 | return 1; |
07800601 | 669 | |
2ab52083 AB |
670 | for (i = 0; skip_symbols[i]; i++) { |
671 | if (!strcmp(skip_symbols[i], name)) { | |
171b3be9 | 672 | sym->ignore = true; |
2ab52083 AB |
673 | break; |
674 | } | |
675 | } | |
07800601 | 676 | |
07800601 IM |
677 | return 0; |
678 | } | |
679 | ||
1758af10 ACM |
680 | static void perf_event__process_sample(struct perf_tool *tool, |
681 | const union perf_event *event, | |
7b27509f | 682 | struct perf_evsel *evsel, |
8115d60c | 683 | struct perf_sample *sample, |
743eb868 | 684 | struct machine *machine) |
07800601 | 685 | { |
1758af10 | 686 | struct perf_top *top = container_of(tool, struct perf_top, tool); |
19d4ac3c | 687 | struct symbol *parent = NULL; |
8115d60c | 688 | u64 ip = event->ip.ip; |
1ed091c4 | 689 | struct addr_location al; |
19d4ac3c | 690 | int err; |
5b2bb75a | 691 | |
23346f21 | 692 | if (!machine && perf_guest) { |
6b118e92 DA |
693 | static struct intlist *seen; |
694 | ||
695 | if (!seen) | |
ffe0fb76 | 696 | seen = intlist__new(NULL); |
6b118e92 DA |
697 | |
698 | if (!intlist__has_entry(seen, event->ip.pid)) { | |
699 | pr_err("Can't find guest [%d]'s kernel information\n", | |
700 | event->ip.pid); | |
701 | intlist__add(seen, event->ip.pid); | |
702 | } | |
a1645ce1 ZY |
703 | return; |
704 | } | |
705 | ||
0c095715 | 706 | if (!machine) { |
11859e82 | 707 | pr_err("%u unprocessable samples recorded.\r", |
28a6b6aa | 708 | top->session->stats.nr_unprocessable_samples++); |
0c095715 JR |
709 | return; |
710 | } | |
711 | ||
8115d60c | 712 | if (event->header.misc & PERF_RECORD_MISC_EXACT_IP) |
1758af10 | 713 | top->exact_samples++; |
1676b8a0 | 714 | |
743eb868 | 715 | if (perf_event__preprocess_sample(event, machine, &al, sample, |
8115d60c | 716 | symbol_filter) < 0 || |
72b8fa17 | 717 | al.filtered) |
1ed091c4 | 718 | return; |
07800601 | 719 | |
1758af10 | 720 | if (!top->kptr_restrict_warned && |
5f6f5580 ACM |
721 | symbol_conf.kptr_restrict && |
722 | al.cpumode == PERF_RECORD_MISC_KERNEL) { | |
723 | ui__warning( | |
724 | "Kernel address maps (/proc/{kallsyms,modules}) are restricted.\n\n" | |
725 | "Check /proc/sys/kernel/kptr_restrict.\n\n" | |
726 | "Kernel%s samples will not be resolved.\n", | |
727 | !RB_EMPTY_ROOT(&al.map->dso->symbols[MAP__FUNCTION]) ? | |
728 | " modules" : ""); | |
729 | if (use_browser <= 0) | |
730 | sleep(5); | |
1758af10 | 731 | top->kptr_restrict_warned = true; |
5f6f5580 ACM |
732 | } |
733 | ||
72b8fa17 | 734 | if (al.sym == NULL) { |
e4a338d0 | 735 | const char *msg = "Kernel samples will not be resolved.\n"; |
72b8fa17 ACM |
736 | /* |
737 | * As we do lazy loading of symtabs we only will know if the | |
738 | * specified vmlinux file is invalid when we actually have a | |
739 | * hit in kernel space and then try to load it. So if we get | |
740 | * here and there are _no_ symbols in the DSO backing the | |
741 | * kernel map, bail out. | |
742 | * | |
743 | * We may never get here, for instance, if we use -K/ | |
744 | * --hide-kernel-symbols, even if the user specifies an | |
745 | * invalid --vmlinux ;-) | |
746 | */ | |
1758af10 | 747 | if (!top->kptr_restrict_warned && !top->vmlinux_warned && |
e4a338d0 | 748 | al.map == machine->vmlinux_maps[MAP__FUNCTION] && |
72b8fa17 | 749 | RB_EMPTY_ROOT(&al.map->dso->symbols[MAP__FUNCTION])) { |
e4a338d0 ACM |
750 | if (symbol_conf.vmlinux_name) { |
751 | ui__warning("The %s file can't be used.\n%s", | |
752 | symbol_conf.vmlinux_name, msg); | |
753 | } else { | |
754 | ui__warning("A vmlinux file was not found.\n%s", | |
755 | msg); | |
756 | } | |
757 | ||
758 | if (use_browser <= 0) | |
759 | sleep(5); | |
1758af10 | 760 | top->vmlinux_warned = true; |
72b8fa17 | 761 | } |
6cff0e8d KS |
762 | } |
763 | ||
ab81f3fd | 764 | if (al.sym == NULL || !al.sym->ignore) { |
ab81f3fd | 765 | struct hist_entry *he; |
70db7533 | 766 | |
19d4ac3c ACM |
767 | if ((sort__has_parent || symbol_conf.use_callchain) && |
768 | sample->callchain) { | |
71ad0f5e JO |
769 | err = machine__resolve_callchain(machine, evsel, |
770 | al.thread, sample, | |
771 | &parent); | |
772 | ||
19d4ac3c ACM |
773 | if (err) |
774 | return; | |
775 | } | |
776 | ||
743eb868 | 777 | he = perf_evsel__add_hist_entry(evsel, &al, sample); |
ab81f3fd ACM |
778 | if (he == NULL) { |
779 | pr_err("Problem incrementing symbol period, skipping event\n"); | |
780 | return; | |
5807806a | 781 | } |
ab81f3fd | 782 | |
19d4ac3c | 783 | if (symbol_conf.use_callchain) { |
47260645 | 784 | err = callchain_append(he->callchain, &callchain_cursor, |
19d4ac3c ACM |
785 | sample->period); |
786 | if (err) | |
787 | return; | |
788 | } | |
789 | ||
8f0f684b | 790 | if (sort__has_sym) |
1758af10 | 791 | perf_top__record_precise_ip(top, he, evsel->idx, ip); |
5b2bb75a | 792 | } |
ab81f3fd ACM |
793 | |
794 | return; | |
07800601 IM |
795 | } |
796 | ||
1758af10 | 797 | static void perf_top__mmap_read_idx(struct perf_top *top, int idx) |
07800601 | 798 | { |
8d50e5b4 | 799 | struct perf_sample sample; |
7b27509f | 800 | struct perf_evsel *evsel; |
1758af10 | 801 | struct perf_session *session = top->session; |
8115d60c | 802 | union perf_event *event; |
743eb868 ACM |
803 | struct machine *machine; |
804 | u8 origin; | |
5538beca | 805 | int ret; |
07800601 | 806 | |
1758af10 | 807 | while ((event = perf_evlist__mmap_read(top->evlist, idx)) != NULL) { |
0807d2d8 | 808 | ret = perf_evlist__parse_sample(top->evlist, event, &sample); |
5538beca FW |
809 | if (ret) { |
810 | pr_err("Can't parse sample, err = %d\n", ret); | |
811 | continue; | |
812 | } | |
04391deb | 813 | |
1758af10 | 814 | evsel = perf_evlist__id2evsel(session->evlist, sample.id); |
7b27509f ACM |
815 | assert(evsel != NULL); |
816 | ||
743eb868 ACM |
817 | origin = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; |
818 | ||
5b2bb75a | 819 | if (event->header.type == PERF_RECORD_SAMPLE) |
1758af10 | 820 | ++top->samples; |
743eb868 ACM |
821 | |
822 | switch (origin) { | |
823 | case PERF_RECORD_MISC_USER: | |
1758af10 ACM |
824 | ++top->us_samples; |
825 | if (top->hide_user_symbols) | |
743eb868 | 826 | continue; |
34ba5122 | 827 | machine = &session->machines.host; |
743eb868 ACM |
828 | break; |
829 | case PERF_RECORD_MISC_KERNEL: | |
1758af10 ACM |
830 | ++top->kernel_samples; |
831 | if (top->hide_kernel_symbols) | |
743eb868 | 832 | continue; |
34ba5122 | 833 | machine = &session->machines.host; |
743eb868 ACM |
834 | break; |
835 | case PERF_RECORD_MISC_GUEST_KERNEL: | |
1758af10 ACM |
836 | ++top->guest_kernel_samples; |
837 | machine = perf_session__find_machine(session, event->ip.pid); | |
743eb868 ACM |
838 | break; |
839 | case PERF_RECORD_MISC_GUEST_USER: | |
1758af10 | 840 | ++top->guest_us_samples; |
743eb868 ACM |
841 | /* |
842 | * TODO: we don't process guest user from host side | |
843 | * except simple counting. | |
844 | */ | |
845 | /* Fall thru */ | |
846 | default: | |
847 | continue; | |
848 | } | |
849 | ||
850 | ||
1758af10 ACM |
851 | if (event->header.type == PERF_RECORD_SAMPLE) { |
852 | perf_event__process_sample(&top->tool, event, evsel, | |
853 | &sample, machine); | |
854 | } else if (event->header.type < PERF_RECORD_MAX) { | |
7b27509f | 855 | hists__inc_nr_events(&evsel->hists, event->header.type); |
b0a7d1a0 | 856 | machine__process_event(machine, event); |
7b27509f | 857 | } else |
28a6b6aa | 858 | ++session->stats.nr_unknown_events; |
07800601 | 859 | } |
07800601 IM |
860 | } |
861 | ||
1758af10 | 862 | static void perf_top__mmap_read(struct perf_top *top) |
2f01190a | 863 | { |
70db7533 ACM |
864 | int i; |
865 | ||
1758af10 ACM |
866 | for (i = 0; i < top->evlist->nr_mmaps; i++) |
867 | perf_top__mmap_read_idx(top, i); | |
2f01190a FW |
868 | } |
869 | ||
11859e82 | 870 | static int perf_top__start_counters(struct perf_top *top) |
72cb7013 | 871 | { |
56e52e85 | 872 | char msg[512]; |
6a4bb04c | 873 | struct perf_evsel *counter; |
1758af10 | 874 | struct perf_evlist *evlist = top->evlist; |
2376c67a | 875 | struct perf_record_opts *opts = &top->record_opts; |
727ab04e | 876 | |
2376c67a | 877 | perf_evlist__config(evlist, opts); |
7e4ff9e3 | 878 | |
72cb7013 | 879 | list_for_each_entry(counter, &evlist->entries, node) { |
72cb7013 | 880 | try_again: |
1758af10 | 881 | if (perf_evsel__open(counter, top->evlist->cpus, |
6a4bb04c | 882 | top->evlist->threads) < 0) { |
56e52e85 | 883 | if (perf_evsel__fallback(counter, errno, msg, sizeof(msg))) { |
d6d901c2 | 884 | if (verbose) |
c0a54341 | 885 | ui__warning("%s\n", msg); |
d6d901c2 ZY |
886 | goto try_again; |
887 | } | |
c286c419 | 888 | |
56e52e85 ACM |
889 | perf_evsel__open_strerror(counter, &opts->target, |
890 | errno, msg, sizeof(msg)); | |
891 | ui__error("%s\n", msg); | |
c286c419 | 892 | goto out_err; |
d6d901c2 | 893 | } |
716c69fe | 894 | } |
70db7533 | 895 | |
2376c67a | 896 | if (perf_evlist__mmap(evlist, opts->mmap_pages, false) < 0) { |
3780f488 | 897 | ui__error("Failed to mmap with %d (%s)\n", |
c286c419 ACM |
898 | errno, strerror(errno)); |
899 | goto out_err; | |
900 | } | |
901 | ||
11859e82 | 902 | return 0; |
c286c419 ACM |
903 | |
904 | out_err: | |
11859e82 | 905 | return -1; |
716c69fe IM |
906 | } |
907 | ||
8f0f684b | 908 | static int perf_top__setup_sample_type(struct perf_top *top __maybe_unused) |
19d4ac3c | 909 | { |
8f0f684b | 910 | if (!sort__has_sym) { |
19d4ac3c | 911 | if (symbol_conf.use_callchain) { |
3780f488 | 912 | ui__error("Selected -g but \"sym\" not present in --sort/-s."); |
19d4ac3c ACM |
913 | return -EINVAL; |
914 | } | |
2376c67a | 915 | } else if (callchain_param.mode != CHAIN_NONE) { |
19d4ac3c | 916 | if (callchain_register_param(&callchain_param) < 0) { |
3780f488 | 917 | ui__error("Can't register callchain params.\n"); |
19d4ac3c ACM |
918 | return -EINVAL; |
919 | } | |
920 | } | |
921 | ||
922 | return 0; | |
923 | } | |
924 | ||
1758af10 | 925 | static int __cmd_top(struct perf_top *top) |
716c69fe | 926 | { |
2376c67a | 927 | struct perf_record_opts *opts = &top->record_opts; |
716c69fe | 928 | pthread_t thread; |
19d4ac3c | 929 | int ret; |
d8f66248 | 930 | /* |
b3165f41 ACM |
931 | * FIXME: perf_session__new should allow passing a O_MMAP, so that all this |
932 | * mmap reading, etc is encapsulated in it. Use O_WRONLY for now. | |
d8f66248 | 933 | */ |
1758af10 ACM |
934 | top->session = perf_session__new(NULL, O_WRONLY, false, false, NULL); |
935 | if (top->session == NULL) | |
b3165f41 | 936 | return -ENOMEM; |
07800601 | 937 | |
1758af10 | 938 | ret = perf_top__setup_sample_type(top); |
19d4ac3c ACM |
939 | if (ret) |
940 | goto out_delete; | |
941 | ||
2376c67a | 942 | if (perf_target__has_task(&opts->target)) |
1758af10 | 943 | perf_event__synthesize_thread_map(&top->tool, top->evlist->threads, |
743eb868 | 944 | perf_event__process, |
876650e6 | 945 | &top->session->machines.host); |
5b2bb75a | 946 | else |
1758af10 | 947 | perf_event__synthesize_threads(&top->tool, perf_event__process, |
876650e6 | 948 | &top->session->machines.host); |
11859e82 ACM |
949 | |
950 | ret = perf_top__start_counters(top); | |
951 | if (ret) | |
952 | goto out_delete; | |
953 | ||
1758af10 | 954 | top->session->evlist = top->evlist; |
7b56cce2 | 955 | perf_session__set_id_hdr_size(top->session); |
07800601 | 956 | |
2376c67a ACM |
957 | /* |
958 | * When perf is starting the traced process, all the events (apart from | |
959 | * group members) have enable_on_exec=1 set, so don't spoil it by | |
960 | * prematurely enabling them. | |
961 | * | |
962 | * XXX 'top' still doesn't start workloads like record, trace, but should, | |
963 | * so leave the check here. | |
964 | */ | |
965 | if (!perf_target__none(&opts->target)) | |
966 | perf_evlist__enable(top->evlist); | |
967 | ||
2f01190a | 968 | /* Wait for a minimal set of events before starting the snapshot */ |
1758af10 | 969 | poll(top->evlist->pollfd, top->evlist->nr_fds, 100); |
2f01190a | 970 | |
1758af10 | 971 | perf_top__mmap_read(top); |
2f01190a | 972 | |
11859e82 | 973 | ret = -1; |
c0443df1 | 974 | if (pthread_create(&thread, NULL, (use_browser > 0 ? display_thread_tui : |
1758af10 | 975 | display_thread), top)) { |
3780f488 | 976 | ui__error("Could not create display thread.\n"); |
11859e82 | 977 | goto out_delete; |
07800601 IM |
978 | } |
979 | ||
1758af10 | 980 | if (top->realtime_prio) { |
07800601 IM |
981 | struct sched_param param; |
982 | ||
1758af10 | 983 | param.sched_priority = top->realtime_prio; |
07800601 | 984 | if (sched_setscheduler(0, SCHED_FIFO, ¶m)) { |
3780f488 | 985 | ui__error("Could not set realtime priority.\n"); |
11859e82 | 986 | goto out_delete; |
07800601 IM |
987 | } |
988 | } | |
989 | ||
11859e82 | 990 | while (!done) { |
1758af10 | 991 | u64 hits = top->samples; |
07800601 | 992 | |
1758af10 | 993 | perf_top__mmap_read(top); |
07800601 | 994 | |
1758af10 ACM |
995 | if (hits == top->samples) |
996 | ret = poll(top->evlist->pollfd, top->evlist->nr_fds, 100); | |
07800601 IM |
997 | } |
998 | ||
11859e82 | 999 | ret = 0; |
19d4ac3c | 1000 | out_delete: |
1758af10 ACM |
1001 | perf_session__delete(top->session); |
1002 | top->session = NULL; | |
19d4ac3c | 1003 | |
11859e82 | 1004 | return ret; |
19d4ac3c ACM |
1005 | } |
1006 | ||
1007 | static int | |
1758af10 | 1008 | parse_callchain_opt(const struct option *opt, const char *arg, int unset) |
19d4ac3c | 1009 | { |
19d4ac3c ACM |
1010 | /* |
1011 | * --no-call-graph | |
1012 | */ | |
2376c67a | 1013 | if (unset) |
19d4ac3c | 1014 | return 0; |
19d4ac3c ACM |
1015 | |
1016 | symbol_conf.use_callchain = true; | |
1017 | ||
2376c67a | 1018 | return record_parse_callchain_opt(opt, arg, unset); |
07800601 | 1019 | } |
b456bae0 | 1020 | |
1d037ca1 | 1021 | int cmd_top(int argc, const char **argv, const char *prefix __maybe_unused) |
1758af10 | 1022 | { |
16ad2ffb NK |
1023 | int status; |
1024 | char errbuf[BUFSIZ]; | |
1758af10 ACM |
1025 | struct perf_top top = { |
1026 | .count_filter = 5, | |
1027 | .delay_secs = 2, | |
2376c67a ACM |
1028 | .record_opts = { |
1029 | .mmap_pages = UINT_MAX, | |
1030 | .user_freq = UINT_MAX, | |
1031 | .user_interval = ULLONG_MAX, | |
1032 | .freq = 4000, /* 4 KHz */ | |
1033 | .target = { | |
1034 | .uses_mmap = true, | |
1035 | }, | |
d1cb9fce | 1036 | }, |
2376c67a | 1037 | .sym_pcnt_filter = 5, |
1758af10 | 1038 | }; |
2376c67a ACM |
1039 | struct perf_record_opts *opts = &top.record_opts; |
1040 | struct perf_target *target = &opts->target; | |
1758af10 | 1041 | const struct option options[] = { |
8c3e10eb | 1042 | OPT_CALLBACK('e', "event", &top.evlist, "event", |
86847b62 | 1043 | "event selector. use 'perf list' to list available events", |
f120f9d5 | 1044 | parse_events_option), |
2376c67a ACM |
1045 | OPT_U64('c', "count", &opts->user_interval, "event period to sample"), |
1046 | OPT_STRING('p', "pid", &target->pid, "pid", | |
d6d901c2 | 1047 | "profile events on existing process id"), |
2376c67a | 1048 | OPT_STRING('t', "tid", &target->tid, "tid", |
d6d901c2 | 1049 | "profile events on existing thread id"), |
2376c67a | 1050 | OPT_BOOLEAN('a', "all-cpus", &target->system_wide, |
b456bae0 | 1051 | "system-wide collection from all CPUs"), |
2376c67a | 1052 | OPT_STRING('C', "cpu", &target->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"), |
2376c67a ACM |
1058 | OPT_UINTEGER('m', "mmap-pages", &opts->mmap_pages, |
1059 | "number of mmap data pages"), | |
1758af10 | 1060 | OPT_INTEGER('r', "realtime", &top.realtime_prio, |
b456bae0 | 1061 | "collect data with this RT SCHED_FIFO priority"), |
8c3e10eb | 1062 | OPT_INTEGER('d', "delay", &top.delay_secs, |
b456bae0 | 1063 | "number of seconds to delay between refreshes"), |
1758af10 | 1064 | OPT_BOOLEAN('D', "dump-symtab", &top.dump_symtab, |
b456bae0 | 1065 | "dump the symbol table used for profiling"), |
8c3e10eb | 1066 | OPT_INTEGER('f', "count-filter", &top.count_filter, |
b456bae0 | 1067 | "only display functions with more events than this"), |
2376c67a | 1068 | OPT_BOOLEAN('g', "group", &opts->group, |
b456bae0 | 1069 | "put the counters into a counter group"), |
2376c67a ACM |
1070 | OPT_BOOLEAN('i', "no-inherit", &opts->no_inherit, |
1071 | "child tasks do not inherit counters"), | |
1758af10 | 1072 | OPT_STRING(0, "sym-annotate", &top.sym_filter, "symbol name", |
6cff0e8d | 1073 | "symbol to annotate"), |
2376c67a ACM |
1074 | OPT_BOOLEAN('z', "zero", &top.zero, "zero history across updates"), |
1075 | OPT_UINTEGER('F', "freq", &opts->user_freq, "profile at this frequency"), | |
8c3e10eb | 1076 | OPT_INTEGER('E', "entries", &top.print_entries, |
6e53cdf1 | 1077 | "display this many functions"), |
8c3e10eb | 1078 | OPT_BOOLEAN('U', "hide_user_symbols", &top.hide_user_symbols, |
8ffcda17 | 1079 | "hide user symbols"), |
1758af10 ACM |
1080 | OPT_BOOLEAN(0, "tui", &top.use_tui, "Use the TUI interface"), |
1081 | OPT_BOOLEAN(0, "stdio", &top.use_stdio, "Use the stdio interface"), | |
c0555642 | 1082 | OPT_INCR('v', "verbose", &verbose, |
3da297a6 | 1083 | "be more verbose (show counter open errors, etc)"), |
ab81f3fd | 1084 | OPT_STRING('s', "sort", &sort_order, "key[,key2...]", |
05484298 | 1085 | "sort by key(s): pid, comm, dso, symbol, parent, weight, local_weight"), |
ab81f3fd ACM |
1086 | OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples, |
1087 | "Show a column with the number of samples"), | |
2376c67a ACM |
1088 | OPT_CALLBACK_DEFAULT('G', "call-graph", &top.record_opts, |
1089 | "mode[,dump_size]", record_callchain_help, | |
1090 | &parse_callchain_opt, "fp"), | |
ab81f3fd ACM |
1091 | OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period, |
1092 | "Show a column with the sum of periods"), | |
1093 | OPT_STRING(0, "dsos", &symbol_conf.dso_list_str, "dso[,dso...]", | |
1094 | "only consider symbols in these dsos"), | |
1095 | OPT_STRING(0, "comms", &symbol_conf.comm_list_str, "comm[,comm...]", | |
1096 | "only consider symbols in these comms"), | |
1097 | OPT_STRING(0, "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]", | |
1098 | "only consider these symbols"), | |
64c6f0c7 ACM |
1099 | OPT_BOOLEAN(0, "source", &symbol_conf.annotate_src, |
1100 | "Interleave source code with assembly code (default)"), | |
1101 | OPT_BOOLEAN(0, "asm-raw", &symbol_conf.annotate_asm_raw, | |
1102 | "Display raw encoding of assembly instructions (default)"), | |
1103 | OPT_STRING('M', "disassembler-style", &disassembler_style, "disassembler style", | |
1104 | "Specify disassembler style (e.g. -M intel for intel syntax)"), | |
2376c67a | 1105 | OPT_STRING('u', "uid", &target->uid_str, "user", "user to profile"), |
b456bae0 | 1106 | OPT_END() |
1758af10 | 1107 | }; |
be772842 ACM |
1108 | const char * const top_usage[] = { |
1109 | "perf top [<options>]", | |
1110 | NULL | |
1111 | }; | |
b456bae0 | 1112 | |
334fe7a3 | 1113 | top.evlist = perf_evlist__new(); |
8c3e10eb | 1114 | if (top.evlist == NULL) |
361c99a6 ACM |
1115 | return -ENOMEM; |
1116 | ||
ab81f3fd | 1117 | symbol_conf.exclude_other = false; |
b456bae0 | 1118 | |
b456bae0 IM |
1119 | argc = parse_options(argc, argv, options, top_usage, 0); |
1120 | if (argc) | |
1121 | usage_with_options(top_usage, options); | |
1122 | ||
ab81f3fd ACM |
1123 | if (sort_order == default_sort_order) |
1124 | sort_order = "dso,symbol"; | |
1125 | ||
55309985 NK |
1126 | if (setup_sorting() < 0) |
1127 | usage_with_options(top_usage, options); | |
ab81f3fd | 1128 | |
3a5714f8 NK |
1129 | /* display thread wants entries to be collapsed in a different tree */ |
1130 | sort__need_collapse = 1; | |
1131 | ||
1758af10 | 1132 | if (top.use_stdio) |
c0443df1 | 1133 | use_browser = 0; |
1758af10 | 1134 | else if (top.use_tui) |
c0443df1 ACM |
1135 | use_browser = 1; |
1136 | ||
1137 | setup_browser(false); | |
1138 | ||
2376c67a | 1139 | status = perf_target__validate(target); |
16ad2ffb | 1140 | if (status) { |
2376c67a | 1141 | perf_target__strerror(target, status, errbuf, BUFSIZ); |
16ad2ffb NK |
1142 | ui__warning("%s", errbuf); |
1143 | } | |
1144 | ||
2376c67a | 1145 | status = perf_target__parse_uid(target); |
16ad2ffb NK |
1146 | if (status) { |
1147 | int saved_errno = errno; | |
4bd0f2d2 | 1148 | |
2376c67a | 1149 | perf_target__strerror(target, status, errbuf, BUFSIZ); |
3780f488 | 1150 | ui__error("%s", errbuf); |
16ad2ffb NK |
1151 | |
1152 | status = -saved_errno; | |
0d37aa34 | 1153 | goto out_delete_evlist; |
16ad2ffb | 1154 | } |
0d37aa34 | 1155 | |
2376c67a ACM |
1156 | if (perf_target__none(target)) |
1157 | target->system_wide = true; | |
10b47d54 | 1158 | |
2376c67a | 1159 | if (perf_evlist__create_maps(top.evlist, target) < 0) |
7e2ed097 ACM |
1160 | usage_with_options(top_usage, options); |
1161 | ||
8c3e10eb ACM |
1162 | if (!top.evlist->nr_entries && |
1163 | perf_evlist__add_default(top.evlist) < 0) { | |
3780f488 | 1164 | ui__error("Not enough memory for event selector list\n"); |
0de233b9 | 1165 | goto out_delete_maps; |
69aad6f1 | 1166 | } |
5a8e5a30 | 1167 | |
d04b35f8 ACM |
1168 | symbol_conf.nr_events = top.evlist->nr_entries; |
1169 | ||
8c3e10eb ACM |
1170 | if (top.delay_secs < 1) |
1171 | top.delay_secs = 1; | |
2f335a02 | 1172 | |
2376c67a ACM |
1173 | if (opts->user_interval != ULLONG_MAX) |
1174 | opts->default_interval = opts->user_interval; | |
1175 | if (opts->user_freq != UINT_MAX) | |
1176 | opts->freq = opts->user_freq; | |
1177 | ||
7e4ff9e3 MG |
1178 | /* |
1179 | * User specified count overrides default frequency. | |
1180 | */ | |
2376c67a ACM |
1181 | if (opts->default_interval) |
1182 | opts->freq = 0; | |
1183 | else if (opts->freq) { | |
1184 | opts->default_interval = opts->freq; | |
7e4ff9e3 | 1185 | } else { |
3780f488 | 1186 | ui__error("frequency and count are zero, aborting\n"); |
2376c67a | 1187 | status = -EINVAL; |
0de233b9 | 1188 | goto out_delete_maps; |
69aad6f1 ACM |
1189 | } |
1190 | ||
0c21f736 | 1191 | top.sym_evsel = perf_evlist__first(top.evlist); |
cc841580 | 1192 | |
ab81f3fd | 1193 | symbol_conf.priv_size = sizeof(struct annotation); |
69aad6f1 ACM |
1194 | |
1195 | symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL); | |
1196 | if (symbol__init() < 0) | |
1197 | return -1; | |
1198 | ||
08e71542 | 1199 | sort__setup_elide(stdout); |
ab81f3fd | 1200 | |
1758af10 | 1201 | get_term_dimensions(&top.winsize); |
8c3e10eb | 1202 | if (top.print_entries == 0) { |
1758af10 ACM |
1203 | struct sigaction act = { |
1204 | .sa_sigaction = perf_top__sig_winch, | |
1205 | .sa_flags = SA_SIGINFO, | |
1206 | }; | |
1207 | perf_top__update_print_entries(&top); | |
1208 | sigaction(SIGWINCH, &act, NULL); | |
3b6ed988 ACM |
1209 | } |
1210 | ||
1758af10 | 1211 | status = __cmd_top(&top); |
806fb630 | 1212 | |
0de233b9 ACM |
1213 | out_delete_maps: |
1214 | perf_evlist__delete_maps(top.evlist); | |
0d37aa34 | 1215 | out_delete_evlist: |
8c3e10eb | 1216 | perf_evlist__delete(top.evlist); |
69aad6f1 ACM |
1217 | |
1218 | return status; | |
b456bae0 | 1219 | } |