.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* |
---|
2 | 3 | * builtin-top.c |
---|
3 | 4 | * |
---|
.. | .. |
---|
14 | 15 | * Wu Fengguang <fengguang.wu@intel.com> |
---|
15 | 16 | * Mike Galbraith <efault@gmx.de> |
---|
16 | 17 | * Paul Mackerras <paulus@samba.org> |
---|
17 | | - * |
---|
18 | | - * Released under the GPL v2. (and only v2, not any later version) |
---|
19 | 18 | */ |
---|
20 | 19 | #include "builtin.h" |
---|
21 | 20 | |
---|
22 | 21 | #include "perf.h" |
---|
23 | 22 | |
---|
24 | 23 | #include "util/annotate.h" |
---|
| 24 | +#include "util/bpf-event.h" |
---|
25 | 25 | #include "util/config.h" |
---|
26 | 26 | #include "util/color.h" |
---|
27 | | -#include "util/drv_configs.h" |
---|
| 27 | +#include "util/dso.h" |
---|
28 | 28 | #include "util/evlist.h" |
---|
29 | 29 | #include "util/evsel.h" |
---|
| 30 | +#include "util/evsel_config.h" |
---|
30 | 31 | #include "util/event.h" |
---|
31 | 32 | #include "util/machine.h" |
---|
| 33 | +#include "util/map.h" |
---|
| 34 | +#include "util/mmap.h" |
---|
32 | 35 | #include "util/session.h" |
---|
33 | | -#include "util/symbol.h" |
---|
34 | 36 | #include "util/thread.h" |
---|
35 | | -#include "util/thread_map.h" |
---|
| 37 | +#include "util/symbol.h" |
---|
| 38 | +#include "util/synthetic-events.h" |
---|
36 | 39 | #include "util/top.h" |
---|
| 40 | +#include "util/util.h" |
---|
37 | 41 | #include <linux/rbtree.h> |
---|
38 | 42 | #include <subcmd/parse-options.h> |
---|
39 | 43 | #include "util/parse-events.h" |
---|
| 44 | +#include "util/callchain.h" |
---|
40 | 45 | #include "util/cpumap.h" |
---|
41 | | -#include "util/xyarray.h" |
---|
42 | 46 | #include "util/sort.h" |
---|
| 47 | +#include "util/string2.h" |
---|
43 | 48 | #include "util/term.h" |
---|
44 | 49 | #include "util/intlist.h" |
---|
45 | 50 | #include "util/parse-branch-options.h" |
---|
46 | 51 | #include "arch/common.h" |
---|
| 52 | +#include "ui/ui.h" |
---|
47 | 53 | |
---|
48 | 54 | #include "util/debug.h" |
---|
| 55 | +#include "util/ordered-events.h" |
---|
| 56 | +#include "util/pfm.h" |
---|
49 | 57 | |
---|
50 | 58 | #include <assert.h> |
---|
51 | 59 | #include <elf.h> |
---|
.. | .. |
---|
73 | 81 | #include <linux/stringify.h> |
---|
74 | 82 | #include <linux/time64.h> |
---|
75 | 83 | #include <linux/types.h> |
---|
| 84 | +#include <linux/err.h> |
---|
76 | 85 | |
---|
77 | | -#include "sane_ctype.h" |
---|
| 86 | +#include <linux/ctype.h> |
---|
| 87 | +#include <perf/mmap.h> |
---|
78 | 88 | |
---|
79 | 89 | static volatile int done; |
---|
80 | 90 | static volatile int resize; |
---|
.. | .. |
---|
99 | 109 | |
---|
100 | 110 | static int perf_top__parse_source(struct perf_top *top, struct hist_entry *he) |
---|
101 | 111 | { |
---|
102 | | - struct perf_evsel *evsel; |
---|
| 112 | + struct evsel *evsel; |
---|
103 | 113 | struct symbol *sym; |
---|
104 | 114 | struct annotation *notes; |
---|
105 | 115 | struct map *map; |
---|
.. | .. |
---|
127 | 137 | notes = symbol__annotation(sym); |
---|
128 | 138 | pthread_mutex_lock(¬es->lock); |
---|
129 | 139 | |
---|
130 | | - if (!symbol__hists(sym, top->evlist->nr_entries)) { |
---|
| 140 | + if (!symbol__hists(sym, top->evlist->core.nr_entries)) { |
---|
131 | 141 | pthread_mutex_unlock(¬es->lock); |
---|
132 | 142 | pr_err("Not enough memory for annotating '%s' symbol!\n", |
---|
133 | 143 | sym->name); |
---|
.. | .. |
---|
135 | 145 | return err; |
---|
136 | 146 | } |
---|
137 | 147 | |
---|
138 | | - err = symbol__annotate(sym, map, evsel, 0, &top->annotation_opts, NULL); |
---|
| 148 | + err = symbol__annotate(&he->ms, evsel, &top->annotation_opts, NULL); |
---|
139 | 149 | if (err == 0) { |
---|
140 | 150 | top->sym_filter_entry = he; |
---|
141 | 151 | } else { |
---|
142 | 152 | char msg[BUFSIZ]; |
---|
143 | | - symbol__strerror_disassemble(sym, map, err, msg, sizeof(msg)); |
---|
| 153 | + symbol__strerror_disassemble(&he->ms, err, msg, sizeof(msg)); |
---|
144 | 154 | pr_err("Couldn't annotate %s: %s\n", sym->name, msg); |
---|
145 | 155 | } |
---|
146 | 156 | |
---|
.. | .. |
---|
184 | 194 | static void perf_top__record_precise_ip(struct perf_top *top, |
---|
185 | 195 | struct hist_entry *he, |
---|
186 | 196 | struct perf_sample *sample, |
---|
187 | | - struct perf_evsel *evsel, u64 ip) |
---|
| 197 | + struct evsel *evsel, u64 ip) |
---|
188 | 198 | { |
---|
189 | 199 | struct annotation *notes; |
---|
190 | 200 | struct symbol *sym = he->ms.sym; |
---|
.. | .. |
---|
226 | 236 | static void perf_top__show_details(struct perf_top *top) |
---|
227 | 237 | { |
---|
228 | 238 | struct hist_entry *he = top->sym_filter_entry; |
---|
229 | | - struct perf_evsel *evsel; |
---|
| 239 | + struct evsel *evsel; |
---|
230 | 240 | struct annotation *notes; |
---|
231 | 241 | struct symbol *symbol; |
---|
232 | 242 | int more; |
---|
.. | .. |
---|
246 | 256 | if (notes->src == NULL) |
---|
247 | 257 | goto out_unlock; |
---|
248 | 258 | |
---|
249 | | - printf("Showing %s for %s\n", perf_evsel__name(top->sym_evsel), symbol->name); |
---|
| 259 | + printf("Showing %s for %s\n", evsel__name(top->sym_evsel), symbol->name); |
---|
250 | 260 | printf(" Events Pcnt (>=%d%%)\n", top->annotation_opts.min_pcnt); |
---|
251 | 261 | |
---|
252 | | - more = symbol__annotate_printf(symbol, he->ms.map, top->sym_evsel, &top->annotation_opts); |
---|
| 262 | + more = symbol__annotate_printf(&he->ms, top->sym_evsel, &top->annotation_opts); |
---|
253 | 263 | |
---|
254 | 264 | if (top->evlist->enabled) { |
---|
255 | 265 | if (top->zero) |
---|
.. | .. |
---|
263 | 273 | pthread_mutex_unlock(¬es->lock); |
---|
264 | 274 | } |
---|
265 | 275 | |
---|
| 276 | +static void perf_top__resort_hists(struct perf_top *t) |
---|
| 277 | +{ |
---|
| 278 | + struct evlist *evlist = t->evlist; |
---|
| 279 | + struct evsel *pos; |
---|
| 280 | + |
---|
| 281 | + evlist__for_each_entry(evlist, pos) { |
---|
| 282 | + struct hists *hists = evsel__hists(pos); |
---|
| 283 | + |
---|
| 284 | + /* |
---|
| 285 | + * unlink existing entries so that they can be linked |
---|
| 286 | + * in a correct order in hists__match() below. |
---|
| 287 | + */ |
---|
| 288 | + hists__unlink(hists); |
---|
| 289 | + |
---|
| 290 | + if (evlist->enabled) { |
---|
| 291 | + if (t->zero) { |
---|
| 292 | + hists__delete_entries(hists); |
---|
| 293 | + } else { |
---|
| 294 | + hists__decay_entries(hists, t->hide_user_symbols, |
---|
| 295 | + t->hide_kernel_symbols); |
---|
| 296 | + } |
---|
| 297 | + } |
---|
| 298 | + |
---|
| 299 | + hists__collapse_resort(hists, NULL); |
---|
| 300 | + |
---|
| 301 | + /* Non-group events are considered as leader */ |
---|
| 302 | + if (symbol_conf.event_group && !evsel__is_group_leader(pos)) { |
---|
| 303 | + struct hists *leader_hists = evsel__hists(pos->leader); |
---|
| 304 | + |
---|
| 305 | + hists__match(leader_hists, hists); |
---|
| 306 | + hists__link(leader_hists, hists); |
---|
| 307 | + } |
---|
| 308 | + } |
---|
| 309 | + |
---|
| 310 | + evlist__for_each_entry(evlist, pos) { |
---|
| 311 | + evsel__output_resort(pos, NULL); |
---|
| 312 | + } |
---|
| 313 | +} |
---|
| 314 | + |
---|
266 | 315 | static void perf_top__print_sym_table(struct perf_top *top) |
---|
267 | 316 | { |
---|
268 | 317 | char bf[160]; |
---|
269 | 318 | int printed = 0; |
---|
270 | 319 | const int win_width = top->winsize.ws_col - 1; |
---|
271 | | - struct perf_evsel *evsel = top->sym_evsel; |
---|
| 320 | + struct evsel *evsel = top->sym_evsel; |
---|
272 | 321 | struct hists *hists = evsel__hists(evsel); |
---|
273 | 322 | |
---|
274 | 323 | puts(CONSOLE_CLEAR); |
---|
275 | 324 | |
---|
276 | 325 | perf_top__header_snprintf(top, bf, sizeof(bf)); |
---|
277 | 326 | printf("%s\n", bf); |
---|
278 | | - |
---|
279 | | - perf_top__reset_sample_counters(top); |
---|
280 | 327 | |
---|
281 | 328 | printf("%-*.*s\n", win_width, win_width, graph_dotted_line); |
---|
282 | 329 | |
---|
.. | .. |
---|
296 | 343 | return; |
---|
297 | 344 | } |
---|
298 | 345 | |
---|
299 | | - if (top->evlist->enabled) { |
---|
300 | | - if (top->zero) { |
---|
301 | | - hists__delete_entries(hists); |
---|
302 | | - } else { |
---|
303 | | - hists__decay_entries(hists, top->hide_user_symbols, |
---|
304 | | - top->hide_kernel_symbols); |
---|
305 | | - } |
---|
306 | | - } |
---|
307 | | - |
---|
308 | | - hists__collapse_resort(hists, NULL); |
---|
309 | | - perf_evsel__output_resort(evsel, NULL); |
---|
| 346 | + perf_top__resort_hists(top); |
---|
310 | 347 | |
---|
311 | 348 | hists__output_recalc_col_len(hists, top->print_entries - printed); |
---|
312 | 349 | putchar('\n'); |
---|
.. | .. |
---|
371 | 408 | if (p) |
---|
372 | 409 | *p = 0; |
---|
373 | 410 | |
---|
374 | | - next = rb_first(&hists->entries); |
---|
| 411 | + next = rb_first_cached(&hists->entries); |
---|
375 | 412 | while (next) { |
---|
376 | 413 | n = rb_entry(next, struct hist_entry, rb_node); |
---|
377 | 414 | if (n->ms.sym && !strcmp(buf, n->ms.sym->name)) { |
---|
.. | .. |
---|
404 | 441 | fprintf(stdout, "\t[d] display refresh delay. \t(%d)\n", top->delay_secs); |
---|
405 | 442 | fprintf(stdout, "\t[e] display entries (lines). \t(%d)\n", top->print_entries); |
---|
406 | 443 | |
---|
407 | | - if (top->evlist->nr_entries > 1) |
---|
408 | | - fprintf(stdout, "\t[E] active event counter. \t(%s)\n", perf_evsel__name(top->sym_evsel)); |
---|
| 444 | + if (top->evlist->core.nr_entries > 1) |
---|
| 445 | + fprintf(stdout, "\t[E] active event counter. \t(%s)\n", evsel__name(top->sym_evsel)); |
---|
409 | 446 | |
---|
410 | 447 | fprintf(stdout, "\t[f] profile display filter (count). \t(%d)\n", top->count_filter); |
---|
411 | 448 | |
---|
.. | .. |
---|
439 | 476 | case 'S': |
---|
440 | 477 | return 1; |
---|
441 | 478 | case 'E': |
---|
442 | | - return top->evlist->nr_entries > 1 ? 1 : 0; |
---|
| 479 | + return top->evlist->core.nr_entries > 1 ? 1 : 0; |
---|
443 | 480 | default: |
---|
444 | 481 | break; |
---|
445 | 482 | } |
---|
.. | .. |
---|
485 | 522 | } |
---|
486 | 523 | break; |
---|
487 | 524 | case 'E': |
---|
488 | | - if (top->evlist->nr_entries > 1) { |
---|
| 525 | + if (top->evlist->core.nr_entries > 1) { |
---|
489 | 526 | /* Select 0 as the default event: */ |
---|
490 | 527 | int counter = 0; |
---|
491 | 528 | |
---|
492 | 529 | fprintf(stderr, "\nAvailable events:"); |
---|
493 | 530 | |
---|
494 | 531 | evlist__for_each_entry(top->evlist, top->sym_evsel) |
---|
495 | | - fprintf(stderr, "\n\t%d %s", top->sym_evsel->idx, perf_evsel__name(top->sym_evsel)); |
---|
| 532 | + fprintf(stderr, "\n\t%d %s", top->sym_evsel->idx, evsel__name(top->sym_evsel)); |
---|
496 | 533 | |
---|
497 | 534 | prompt_integer(&counter, "Enter details event counter"); |
---|
498 | 535 | |
---|
499 | | - if (counter >= top->evlist->nr_entries) { |
---|
500 | | - top->sym_evsel = perf_evlist__first(top->evlist); |
---|
501 | | - fprintf(stderr, "Sorry, no such event, using %s.\n", perf_evsel__name(top->sym_evsel)); |
---|
| 536 | + if (counter >= top->evlist->core.nr_entries) { |
---|
| 537 | + top->sym_evsel = evlist__first(top->evlist); |
---|
| 538 | + fprintf(stderr, "Sorry, no such event, using %s.\n", evsel__name(top->sym_evsel)); |
---|
502 | 539 | sleep(1); |
---|
503 | 540 | break; |
---|
504 | 541 | } |
---|
.. | .. |
---|
506 | 543 | if (top->sym_evsel->idx == counter) |
---|
507 | 544 | break; |
---|
508 | 545 | } else |
---|
509 | | - top->sym_evsel = perf_evlist__first(top->evlist); |
---|
| 546 | + top->sym_evsel = evlist__first(top->evlist); |
---|
510 | 547 | break; |
---|
511 | 548 | case 'f': |
---|
512 | 549 | prompt_integer(&top->count_filter, "Enter display event count filter"); |
---|
.. | .. |
---|
554 | 591 | static void perf_top__sort_new_samples(void *arg) |
---|
555 | 592 | { |
---|
556 | 593 | struct perf_top *t = arg; |
---|
557 | | - struct perf_evsel *evsel = t->sym_evsel; |
---|
558 | | - struct hists *hists; |
---|
559 | | - |
---|
560 | | - perf_top__reset_sample_counters(t); |
---|
561 | 594 | |
---|
562 | 595 | if (t->evlist->selected != NULL) |
---|
563 | 596 | t->sym_evsel = t->evlist->selected; |
---|
564 | 597 | |
---|
565 | | - hists = evsel__hists(evsel); |
---|
| 598 | + perf_top__resort_hists(t); |
---|
566 | 599 | |
---|
567 | | - if (t->evlist->enabled) { |
---|
568 | | - if (t->zero) { |
---|
569 | | - hists__delete_entries(hists); |
---|
570 | | - } else { |
---|
571 | | - hists__decay_entries(hists, t->hide_user_symbols, |
---|
572 | | - t->hide_kernel_symbols); |
---|
573 | | - } |
---|
574 | | - } |
---|
| 600 | + if (t->lost || t->drop) |
---|
| 601 | + pr_warning("Too slow to read ring buffer (change period (-c/-F) or limit CPUs (-C)\n"); |
---|
| 602 | +} |
---|
575 | 603 | |
---|
576 | | - hists__collapse_resort(hists, NULL); |
---|
577 | | - perf_evsel__output_resort(evsel, NULL); |
---|
| 604 | +static void stop_top(void) |
---|
| 605 | +{ |
---|
| 606 | + session_done = 1; |
---|
| 607 | + done = 1; |
---|
578 | 608 | } |
---|
579 | 609 | |
---|
580 | 610 | static void *display_thread_tui(void *arg) |
---|
581 | 611 | { |
---|
582 | | - struct perf_evsel *pos; |
---|
| 612 | + struct evsel *pos; |
---|
583 | 613 | struct perf_top *top = arg; |
---|
584 | 614 | const char *help = "For a higher level overview, try: perf top --sort comm,dso"; |
---|
585 | 615 | struct hist_browser_timer hbt = { |
---|
.. | .. |
---|
587 | 617 | .arg = top, |
---|
588 | 618 | .refresh = top->delay_secs, |
---|
589 | 619 | }; |
---|
| 620 | + int ret; |
---|
590 | 621 | |
---|
591 | 622 | /* In order to read symbols from other namespaces perf to needs to call |
---|
592 | 623 | * setns(2). This isn't permitted if the struct_fs has multiple users. |
---|
.. | .. |
---|
595 | 626 | */ |
---|
596 | 627 | unshare(CLONE_FS); |
---|
597 | 628 | |
---|
| 629 | + prctl(PR_SET_NAME, "perf-top-UI", 0, 0, 0); |
---|
| 630 | + |
---|
| 631 | +repeat: |
---|
598 | 632 | perf_top__sort_new_samples(top); |
---|
599 | 633 | |
---|
600 | 634 | /* |
---|
601 | 635 | * Initialize the uid_filter_str, in the future the TUI will allow |
---|
602 | | - * Zooming in/out UIDs. For now juse use whatever the user passed |
---|
| 636 | + * Zooming in/out UIDs. For now just use whatever the user passed |
---|
603 | 637 | * via --uid. |
---|
604 | 638 | */ |
---|
605 | 639 | evlist__for_each_entry(top->evlist, pos) { |
---|
.. | .. |
---|
607 | 641 | hists->uid_filter_str = top->record_opts.target.uid_str; |
---|
608 | 642 | } |
---|
609 | 643 | |
---|
610 | | - perf_evlist__tui_browse_hists(top->evlist, help, &hbt, |
---|
| 644 | + ret = perf_evlist__tui_browse_hists(top->evlist, help, &hbt, |
---|
611 | 645 | top->min_percent, |
---|
612 | 646 | &top->session->header.env, |
---|
613 | 647 | !top->record_opts.overwrite, |
---|
614 | 648 | &top->annotation_opts); |
---|
615 | 649 | |
---|
616 | | - done = 1; |
---|
| 650 | + if (ret == K_RELOAD) { |
---|
| 651 | + top->zero = true; |
---|
| 652 | + goto repeat; |
---|
| 653 | + } else |
---|
| 654 | + stop_top(); |
---|
| 655 | + |
---|
617 | 656 | return NULL; |
---|
618 | 657 | } |
---|
619 | 658 | |
---|
620 | 659 | static void display_sig(int sig __maybe_unused) |
---|
621 | 660 | { |
---|
622 | | - done = 1; |
---|
| 661 | + stop_top(); |
---|
623 | 662 | } |
---|
624 | 663 | |
---|
625 | 664 | static void display_setup_sig(void) |
---|
.. | .. |
---|
644 | 683 | * that we're observing. |
---|
645 | 684 | */ |
---|
646 | 685 | unshare(CLONE_FS); |
---|
| 686 | + |
---|
| 687 | + prctl(PR_SET_NAME, "perf-top-UI", 0, 0, 0); |
---|
647 | 688 | |
---|
648 | 689 | display_setup_sig(); |
---|
649 | 690 | pthread__unblock_sigwinch(); |
---|
.. | .. |
---|
674 | 715 | |
---|
675 | 716 | if (perf_top__handle_keypress(top, c)) |
---|
676 | 717 | goto repeat; |
---|
677 | | - done = 1; |
---|
| 718 | + stop_top(); |
---|
678 | 719 | } |
---|
679 | 720 | } |
---|
680 | 721 | |
---|
.. | .. |
---|
688 | 729 | { |
---|
689 | 730 | struct perf_top *top = arg; |
---|
690 | 731 | struct hist_entry *he = iter->he; |
---|
691 | | - struct perf_evsel *evsel = iter->evsel; |
---|
| 732 | + struct evsel *evsel = iter->evsel; |
---|
692 | 733 | |
---|
693 | 734 | if (perf_hpp_list.sym && single) |
---|
694 | 735 | perf_top__record_precise_ip(top, he, iter->sample, evsel, al->addr); |
---|
695 | 736 | |
---|
696 | 737 | hist__account_cycles(iter->sample->branch_stack, al, iter->sample, |
---|
697 | | - !(top->record_opts.branch_stack & PERF_SAMPLE_BRANCH_ANY)); |
---|
| 738 | + !(top->record_opts.branch_stack & PERF_SAMPLE_BRANCH_ANY), |
---|
| 739 | + NULL); |
---|
698 | 740 | return 0; |
---|
699 | 741 | } |
---|
700 | 742 | |
---|
701 | 743 | static void perf_event__process_sample(struct perf_tool *tool, |
---|
702 | 744 | const union perf_event *event, |
---|
703 | | - struct perf_evsel *evsel, |
---|
| 745 | + struct evsel *evsel, |
---|
704 | 746 | struct perf_sample *sample, |
---|
705 | 747 | struct machine *machine) |
---|
706 | 748 | { |
---|
.. | .. |
---|
734 | 776 | if (machine__resolve(machine, &al, sample) < 0) |
---|
735 | 777 | return; |
---|
736 | 778 | |
---|
| 779 | + if (top->stitch_lbr) |
---|
| 780 | + al.thread->lbr_stitch_enable = true; |
---|
| 781 | + |
---|
737 | 782 | if (!machine->kptr_restrict_warned && |
---|
738 | 783 | symbol_conf.kptr_restrict && |
---|
739 | 784 | al.cpumode == PERF_RECORD_MISC_KERNEL) { |
---|
740 | 785 | if (!perf_evlist__exclude_kernel(top->session->evlist)) { |
---|
741 | 786 | ui__warning( |
---|
742 | 787 | "Kernel address maps (/proc/{kallsyms,modules}) are restricted.\n\n" |
---|
743 | | -"Check /proc/sys/kernel/kptr_restrict.\n\n" |
---|
| 788 | +"Check /proc/sys/kernel/kptr_restrict and /proc/sys/kernel/perf_event_paranoid.\n\n" |
---|
744 | 789 | "Kernel%s samples will not be resolved.\n", |
---|
745 | 790 | al.map && map__has_symbols(al.map) ? |
---|
746 | 791 | " modules" : ""); |
---|
.. | .. |
---|
806 | 851 | addr_location__put(&al); |
---|
807 | 852 | } |
---|
808 | 853 | |
---|
| 854 | +static void |
---|
| 855 | +perf_top__process_lost(struct perf_top *top, union perf_event *event, |
---|
| 856 | + struct evsel *evsel) |
---|
| 857 | +{ |
---|
| 858 | + struct hists *hists = evsel__hists(evsel); |
---|
| 859 | + |
---|
| 860 | + top->lost += event->lost.lost; |
---|
| 861 | + top->lost_total += event->lost.lost; |
---|
| 862 | + hists->stats.total_lost += event->lost.lost; |
---|
| 863 | +} |
---|
| 864 | + |
---|
| 865 | +static void |
---|
| 866 | +perf_top__process_lost_samples(struct perf_top *top, |
---|
| 867 | + union perf_event *event, |
---|
| 868 | + struct evsel *evsel) |
---|
| 869 | +{ |
---|
| 870 | + struct hists *hists = evsel__hists(evsel); |
---|
| 871 | + |
---|
| 872 | + top->lost += event->lost_samples.lost; |
---|
| 873 | + top->lost_total += event->lost_samples.lost; |
---|
| 874 | + hists->stats.total_lost_samples += event->lost_samples.lost; |
---|
| 875 | +} |
---|
| 876 | + |
---|
| 877 | +static u64 last_timestamp; |
---|
| 878 | + |
---|
809 | 879 | static void perf_top__mmap_read_idx(struct perf_top *top, int idx) |
---|
810 | 880 | { |
---|
811 | 881 | struct record_opts *opts = &top->record_opts; |
---|
812 | | - struct perf_evlist *evlist = top->evlist; |
---|
813 | | - struct perf_sample sample; |
---|
814 | | - struct perf_evsel *evsel; |
---|
815 | | - struct perf_mmap *md; |
---|
816 | | - struct perf_session *session = top->session; |
---|
| 882 | + struct evlist *evlist = top->evlist; |
---|
| 883 | + struct mmap *md; |
---|
817 | 884 | union perf_event *event; |
---|
818 | | - struct machine *machine; |
---|
819 | | - int ret; |
---|
820 | 885 | |
---|
821 | 886 | md = opts->overwrite ? &evlist->overwrite_mmap[idx] : &evlist->mmap[idx]; |
---|
822 | | - if (perf_mmap__read_init(md) < 0) |
---|
| 887 | + if (perf_mmap__read_init(&md->core) < 0) |
---|
823 | 888 | return; |
---|
824 | 889 | |
---|
825 | | - while ((event = perf_mmap__read_event(md)) != NULL) { |
---|
826 | | - ret = perf_evlist__parse_sample(evlist, event, &sample); |
---|
827 | | - if (ret) { |
---|
828 | | - pr_err("Can't parse sample, err = %d\n", ret); |
---|
829 | | - goto next_event; |
---|
| 890 | + while ((event = perf_mmap__read_event(&md->core)) != NULL) { |
---|
| 891 | + int ret; |
---|
| 892 | + |
---|
| 893 | + ret = perf_evlist__parse_sample_timestamp(evlist, event, &last_timestamp); |
---|
| 894 | + if (ret && ret != -1) |
---|
| 895 | + break; |
---|
| 896 | + |
---|
| 897 | + ret = ordered_events__queue(top->qe.in, event, last_timestamp, 0); |
---|
| 898 | + if (ret) |
---|
| 899 | + break; |
---|
| 900 | + |
---|
| 901 | + perf_mmap__consume(&md->core); |
---|
| 902 | + |
---|
| 903 | + if (top->qe.rotate) { |
---|
| 904 | + pthread_mutex_lock(&top->qe.mutex); |
---|
| 905 | + top->qe.rotate = false; |
---|
| 906 | + pthread_cond_signal(&top->qe.cond); |
---|
| 907 | + pthread_mutex_unlock(&top->qe.mutex); |
---|
830 | 908 | } |
---|
831 | | - |
---|
832 | | - evsel = perf_evlist__id2evsel(session->evlist, sample.id); |
---|
833 | | - assert(evsel != NULL); |
---|
834 | | - |
---|
835 | | - if (event->header.type == PERF_RECORD_SAMPLE) |
---|
836 | | - ++top->samples; |
---|
837 | | - |
---|
838 | | - switch (sample.cpumode) { |
---|
839 | | - case PERF_RECORD_MISC_USER: |
---|
840 | | - ++top->us_samples; |
---|
841 | | - if (top->hide_user_symbols) |
---|
842 | | - goto next_event; |
---|
843 | | - machine = &session->machines.host; |
---|
844 | | - break; |
---|
845 | | - case PERF_RECORD_MISC_KERNEL: |
---|
846 | | - ++top->kernel_samples; |
---|
847 | | - if (top->hide_kernel_symbols) |
---|
848 | | - goto next_event; |
---|
849 | | - machine = &session->machines.host; |
---|
850 | | - break; |
---|
851 | | - case PERF_RECORD_MISC_GUEST_KERNEL: |
---|
852 | | - ++top->guest_kernel_samples; |
---|
853 | | - machine = perf_session__find_machine(session, |
---|
854 | | - sample.pid); |
---|
855 | | - break; |
---|
856 | | - case PERF_RECORD_MISC_GUEST_USER: |
---|
857 | | - ++top->guest_us_samples; |
---|
858 | | - /* |
---|
859 | | - * TODO: we don't process guest user from host side |
---|
860 | | - * except simple counting. |
---|
861 | | - */ |
---|
862 | | - goto next_event; |
---|
863 | | - default: |
---|
864 | | - if (event->header.type == PERF_RECORD_SAMPLE) |
---|
865 | | - goto next_event; |
---|
866 | | - machine = &session->machines.host; |
---|
867 | | - break; |
---|
868 | | - } |
---|
869 | | - |
---|
870 | | - |
---|
871 | | - if (event->header.type == PERF_RECORD_SAMPLE) { |
---|
872 | | - perf_event__process_sample(&top->tool, event, evsel, |
---|
873 | | - &sample, machine); |
---|
874 | | - } else if (event->header.type < PERF_RECORD_MAX) { |
---|
875 | | - hists__inc_nr_events(evsel__hists(evsel), event->header.type); |
---|
876 | | - machine__process_event(machine, event, &sample); |
---|
877 | | - } else |
---|
878 | | - ++session->evlist->stats.nr_unknown_events; |
---|
879 | | -next_event: |
---|
880 | | - perf_mmap__consume(md); |
---|
881 | 909 | } |
---|
882 | 910 | |
---|
883 | | - perf_mmap__read_done(md); |
---|
| 911 | + perf_mmap__read_done(&md->core); |
---|
884 | 912 | } |
---|
885 | 913 | |
---|
886 | 914 | static void perf_top__mmap_read(struct perf_top *top) |
---|
887 | 915 | { |
---|
888 | 916 | bool overwrite = top->record_opts.overwrite; |
---|
889 | | - struct perf_evlist *evlist = top->evlist; |
---|
890 | | - unsigned long long start, end; |
---|
| 917 | + struct evlist *evlist = top->evlist; |
---|
891 | 918 | int i; |
---|
892 | 919 | |
---|
893 | | - start = rdclock(); |
---|
894 | 920 | if (overwrite) |
---|
895 | 921 | perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_DATA_PENDING); |
---|
896 | 922 | |
---|
897 | | - for (i = 0; i < top->evlist->nr_mmaps; i++) |
---|
| 923 | + for (i = 0; i < top->evlist->core.nr_mmaps; i++) |
---|
898 | 924 | perf_top__mmap_read_idx(top, i); |
---|
899 | 925 | |
---|
900 | 926 | if (overwrite) { |
---|
901 | 927 | perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_EMPTY); |
---|
902 | 928 | perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING); |
---|
903 | 929 | } |
---|
904 | | - end = rdclock(); |
---|
905 | | - |
---|
906 | | - if ((end - start) > (unsigned long long)top->delay_secs * NSEC_PER_SEC) |
---|
907 | | - ui__warning("Too slow to read ring buffer.\n" |
---|
908 | | - "Please try increasing the period (-c) or\n" |
---|
909 | | - "decreasing the freq (-F) or\n" |
---|
910 | | - "limiting the number of CPUs (-C)\n"); |
---|
911 | 930 | } |
---|
912 | 931 | |
---|
913 | 932 | /* |
---|
.. | .. |
---|
930 | 949 | static int perf_top__overwrite_check(struct perf_top *top) |
---|
931 | 950 | { |
---|
932 | 951 | struct record_opts *opts = &top->record_opts; |
---|
933 | | - struct perf_evlist *evlist = top->evlist; |
---|
934 | | - struct perf_evsel_config_term *term; |
---|
| 952 | + struct evlist *evlist = top->evlist; |
---|
| 953 | + struct evsel_config_term *term; |
---|
935 | 954 | struct list_head *config_terms; |
---|
936 | | - struct perf_evsel *evsel; |
---|
| 955 | + struct evsel *evsel; |
---|
937 | 956 | int set, overwrite = -1; |
---|
938 | 957 | |
---|
939 | 958 | evlist__for_each_entry(evlist, evsel) { |
---|
940 | 959 | set = -1; |
---|
941 | 960 | config_terms = &evsel->config_terms; |
---|
942 | 961 | list_for_each_entry(term, config_terms, list) { |
---|
943 | | - if (term->type == PERF_EVSEL__CONFIG_TERM_OVERWRITE) |
---|
| 962 | + if (term->type == EVSEL__CONFIG_TERM_OVERWRITE) |
---|
944 | 963 | set = term->val.overwrite ? 1 : 0; |
---|
945 | 964 | } |
---|
946 | 965 | |
---|
.. | .. |
---|
959 | 978 | /* has term for current event */ |
---|
960 | 979 | if ((overwrite < 0) && (set >= 0)) { |
---|
961 | 980 | /* if it's first event, set overwrite */ |
---|
962 | | - if (evsel == perf_evlist__first(evlist)) |
---|
| 981 | + if (evsel == evlist__first(evlist)) |
---|
963 | 982 | overwrite = set; |
---|
964 | 983 | else |
---|
965 | 984 | return -1; |
---|
.. | .. |
---|
973 | 992 | } |
---|
974 | 993 | |
---|
975 | 994 | static int perf_top_overwrite_fallback(struct perf_top *top, |
---|
976 | | - struct perf_evsel *evsel) |
---|
| 995 | + struct evsel *evsel) |
---|
977 | 996 | { |
---|
978 | 997 | struct record_opts *opts = &top->record_opts; |
---|
979 | | - struct perf_evlist *evlist = top->evlist; |
---|
980 | | - struct perf_evsel *counter; |
---|
| 998 | + struct evlist *evlist = top->evlist; |
---|
| 999 | + struct evsel *counter; |
---|
981 | 1000 | |
---|
982 | 1001 | if (!opts->overwrite) |
---|
983 | 1002 | return 0; |
---|
984 | 1003 | |
---|
985 | 1004 | /* only fall back when first event fails */ |
---|
986 | | - if (evsel != perf_evlist__first(evlist)) |
---|
| 1005 | + if (evsel != evlist__first(evlist)) |
---|
987 | 1006 | return 0; |
---|
988 | 1007 | |
---|
989 | 1008 | evlist__for_each_entry(evlist, counter) |
---|
990 | | - counter->attr.write_backward = false; |
---|
| 1009 | + counter->core.attr.write_backward = false; |
---|
991 | 1010 | opts->overwrite = false; |
---|
992 | 1011 | pr_debug2("fall back to non-overwrite mode\n"); |
---|
993 | 1012 | return 1; |
---|
.. | .. |
---|
996 | 1015 | static int perf_top__start_counters(struct perf_top *top) |
---|
997 | 1016 | { |
---|
998 | 1017 | char msg[BUFSIZ]; |
---|
999 | | - struct perf_evsel *counter; |
---|
1000 | | - struct perf_evlist *evlist = top->evlist; |
---|
| 1018 | + struct evsel *counter; |
---|
| 1019 | + struct evlist *evlist = top->evlist; |
---|
1001 | 1020 | struct record_opts *opts = &top->record_opts; |
---|
1002 | 1021 | |
---|
1003 | 1022 | if (perf_top__overwrite_check(top)) { |
---|
.. | .. |
---|
1010 | 1029 | |
---|
1011 | 1030 | evlist__for_each_entry(evlist, counter) { |
---|
1012 | 1031 | try_again: |
---|
1013 | | - if (perf_evsel__open(counter, top->evlist->cpus, |
---|
1014 | | - top->evlist->threads) < 0) { |
---|
| 1032 | + if (evsel__open(counter, top->evlist->core.cpus, |
---|
| 1033 | + top->evlist->core.threads) < 0) { |
---|
1015 | 1034 | |
---|
1016 | 1035 | /* |
---|
1017 | 1036 | * Specially handle overwrite fall back. |
---|
.. | .. |
---|
1027 | 1046 | perf_top_overwrite_fallback(top, counter)) |
---|
1028 | 1047 | goto try_again; |
---|
1029 | 1048 | |
---|
1030 | | - if (perf_evsel__fallback(counter, errno, msg, sizeof(msg))) { |
---|
| 1049 | + if (evsel__fallback(counter, errno, msg, sizeof(msg))) { |
---|
1031 | 1050 | if (verbose > 0) |
---|
1032 | 1051 | ui__warning("%s\n", msg); |
---|
1033 | 1052 | goto try_again; |
---|
1034 | 1053 | } |
---|
1035 | 1054 | |
---|
1036 | | - perf_evsel__open_strerror(counter, &opts->target, |
---|
1037 | | - errno, msg, sizeof(msg)); |
---|
| 1055 | + evsel__open_strerror(counter, &opts->target, errno, msg, sizeof(msg)); |
---|
1038 | 1056 | ui__error("%s\n", msg); |
---|
1039 | 1057 | goto out_err; |
---|
1040 | 1058 | } |
---|
1041 | 1059 | } |
---|
1042 | 1060 | |
---|
1043 | | - if (perf_evlist__mmap(evlist, opts->mmap_pages) < 0) { |
---|
| 1061 | + if (evlist__mmap(evlist, opts->mmap_pages) < 0) { |
---|
1044 | 1062 | ui__error("Failed to mmap with %d (%s)\n", |
---|
1045 | 1063 | errno, str_error_r(errno, msg, sizeof(msg))); |
---|
1046 | 1064 | goto out_err; |
---|
.. | .. |
---|
1054 | 1072 | |
---|
1055 | 1073 | static int callchain_param__setup_sample_type(struct callchain_param *callchain) |
---|
1056 | 1074 | { |
---|
1057 | | - if (!perf_hpp_list.sym) { |
---|
1058 | | - if (callchain->enabled) { |
---|
1059 | | - ui__error("Selected -g but \"sym\" not present in --sort/-s."); |
---|
1060 | | - return -EINVAL; |
---|
1061 | | - } |
---|
1062 | | - } else if (callchain->mode != CHAIN_NONE) { |
---|
| 1075 | + if (callchain->mode != CHAIN_NONE) { |
---|
1063 | 1076 | if (callchain_register_param(callchain) < 0) { |
---|
1064 | 1077 | ui__error("Can't register callchain params.\n"); |
---|
1065 | 1078 | return -EINVAL; |
---|
.. | .. |
---|
1069 | 1082 | return 0; |
---|
1070 | 1083 | } |
---|
1071 | 1084 | |
---|
| 1085 | +static struct ordered_events *rotate_queues(struct perf_top *top) |
---|
| 1086 | +{ |
---|
| 1087 | + struct ordered_events *in = top->qe.in; |
---|
| 1088 | + |
---|
| 1089 | + if (top->qe.in == &top->qe.data[1]) |
---|
| 1090 | + top->qe.in = &top->qe.data[0]; |
---|
| 1091 | + else |
---|
| 1092 | + top->qe.in = &top->qe.data[1]; |
---|
| 1093 | + |
---|
| 1094 | + return in; |
---|
| 1095 | +} |
---|
| 1096 | + |
---|
| 1097 | +static void *process_thread(void *arg) |
---|
| 1098 | +{ |
---|
| 1099 | + struct perf_top *top = arg; |
---|
| 1100 | + |
---|
| 1101 | + while (!done) { |
---|
| 1102 | + struct ordered_events *out, *in = top->qe.in; |
---|
| 1103 | + |
---|
| 1104 | + if (!in->nr_events) { |
---|
| 1105 | + usleep(100); |
---|
| 1106 | + continue; |
---|
| 1107 | + } |
---|
| 1108 | + |
---|
| 1109 | + out = rotate_queues(top); |
---|
| 1110 | + |
---|
| 1111 | + pthread_mutex_lock(&top->qe.mutex); |
---|
| 1112 | + top->qe.rotate = true; |
---|
| 1113 | + pthread_cond_wait(&top->qe.cond, &top->qe.mutex); |
---|
| 1114 | + pthread_mutex_unlock(&top->qe.mutex); |
---|
| 1115 | + |
---|
| 1116 | + if (ordered_events__flush(out, OE_FLUSH__TOP)) |
---|
| 1117 | + pr_err("failed to process events\n"); |
---|
| 1118 | + } |
---|
| 1119 | + |
---|
| 1120 | + return NULL; |
---|
| 1121 | +} |
---|
| 1122 | + |
---|
| 1123 | +/* |
---|
| 1124 | + * Allow only 'top->delay_secs' seconds behind samples. |
---|
| 1125 | + */ |
---|
| 1126 | +static int should_drop(struct ordered_event *qevent, struct perf_top *top) |
---|
| 1127 | +{ |
---|
| 1128 | + union perf_event *event = qevent->event; |
---|
| 1129 | + u64 delay_timestamp; |
---|
| 1130 | + |
---|
| 1131 | + if (event->header.type != PERF_RECORD_SAMPLE) |
---|
| 1132 | + return false; |
---|
| 1133 | + |
---|
| 1134 | + delay_timestamp = qevent->timestamp + top->delay_secs * NSEC_PER_SEC; |
---|
| 1135 | + return delay_timestamp < last_timestamp; |
---|
| 1136 | +} |
---|
| 1137 | + |
---|
| 1138 | +static int deliver_event(struct ordered_events *qe, |
---|
| 1139 | + struct ordered_event *qevent) |
---|
| 1140 | +{ |
---|
| 1141 | + struct perf_top *top = qe->data; |
---|
| 1142 | + struct evlist *evlist = top->evlist; |
---|
| 1143 | + struct perf_session *session = top->session; |
---|
| 1144 | + union perf_event *event = qevent->event; |
---|
| 1145 | + struct perf_sample sample; |
---|
| 1146 | + struct evsel *evsel; |
---|
| 1147 | + struct machine *machine; |
---|
| 1148 | + int ret = -1; |
---|
| 1149 | + |
---|
| 1150 | + if (should_drop(qevent, top)) { |
---|
| 1151 | + top->drop++; |
---|
| 1152 | + top->drop_total++; |
---|
| 1153 | + return 0; |
---|
| 1154 | + } |
---|
| 1155 | + |
---|
| 1156 | + ret = perf_evlist__parse_sample(evlist, event, &sample); |
---|
| 1157 | + if (ret) { |
---|
| 1158 | + pr_err("Can't parse sample, err = %d\n", ret); |
---|
| 1159 | + goto next_event; |
---|
| 1160 | + } |
---|
| 1161 | + |
---|
| 1162 | + evsel = perf_evlist__id2evsel(session->evlist, sample.id); |
---|
| 1163 | + assert(evsel != NULL); |
---|
| 1164 | + |
---|
| 1165 | + if (event->header.type == PERF_RECORD_SAMPLE) { |
---|
| 1166 | + if (evswitch__discard(&top->evswitch, evsel)) |
---|
| 1167 | + return 0; |
---|
| 1168 | + ++top->samples; |
---|
| 1169 | + } |
---|
| 1170 | + |
---|
| 1171 | + switch (sample.cpumode) { |
---|
| 1172 | + case PERF_RECORD_MISC_USER: |
---|
| 1173 | + ++top->us_samples; |
---|
| 1174 | + if (top->hide_user_symbols) |
---|
| 1175 | + goto next_event; |
---|
| 1176 | + machine = &session->machines.host; |
---|
| 1177 | + break; |
---|
| 1178 | + case PERF_RECORD_MISC_KERNEL: |
---|
| 1179 | + ++top->kernel_samples; |
---|
| 1180 | + if (top->hide_kernel_symbols) |
---|
| 1181 | + goto next_event; |
---|
| 1182 | + machine = &session->machines.host; |
---|
| 1183 | + break; |
---|
| 1184 | + case PERF_RECORD_MISC_GUEST_KERNEL: |
---|
| 1185 | + ++top->guest_kernel_samples; |
---|
| 1186 | + machine = perf_session__find_machine(session, |
---|
| 1187 | + sample.pid); |
---|
| 1188 | + break; |
---|
| 1189 | + case PERF_RECORD_MISC_GUEST_USER: |
---|
| 1190 | + ++top->guest_us_samples; |
---|
| 1191 | + /* |
---|
| 1192 | + * TODO: we don't process guest user from host side |
---|
| 1193 | + * except simple counting. |
---|
| 1194 | + */ |
---|
| 1195 | + goto next_event; |
---|
| 1196 | + default: |
---|
| 1197 | + if (event->header.type == PERF_RECORD_SAMPLE) |
---|
| 1198 | + goto next_event; |
---|
| 1199 | + machine = &session->machines.host; |
---|
| 1200 | + break; |
---|
| 1201 | + } |
---|
| 1202 | + |
---|
| 1203 | + if (event->header.type == PERF_RECORD_SAMPLE) { |
---|
| 1204 | + perf_event__process_sample(&top->tool, event, evsel, |
---|
| 1205 | + &sample, machine); |
---|
| 1206 | + } else if (event->header.type == PERF_RECORD_LOST) { |
---|
| 1207 | + perf_top__process_lost(top, event, evsel); |
---|
| 1208 | + } else if (event->header.type == PERF_RECORD_LOST_SAMPLES) { |
---|
| 1209 | + perf_top__process_lost_samples(top, event, evsel); |
---|
| 1210 | + } else if (event->header.type < PERF_RECORD_MAX) { |
---|
| 1211 | + hists__inc_nr_events(evsel__hists(evsel), event->header.type); |
---|
| 1212 | + machine__process_event(machine, event, &sample); |
---|
| 1213 | + } else |
---|
| 1214 | + ++session->evlist->stats.nr_unknown_events; |
---|
| 1215 | + |
---|
| 1216 | + ret = 0; |
---|
| 1217 | +next_event: |
---|
| 1218 | + return ret; |
---|
| 1219 | +} |
---|
| 1220 | + |
---|
| 1221 | +static void init_process_thread(struct perf_top *top) |
---|
| 1222 | +{ |
---|
| 1223 | + ordered_events__init(&top->qe.data[0], deliver_event, top); |
---|
| 1224 | + ordered_events__init(&top->qe.data[1], deliver_event, top); |
---|
| 1225 | + ordered_events__set_copy_on_queue(&top->qe.data[0], true); |
---|
| 1226 | + ordered_events__set_copy_on_queue(&top->qe.data[1], true); |
---|
| 1227 | + top->qe.in = &top->qe.data[0]; |
---|
| 1228 | + pthread_mutex_init(&top->qe.mutex, NULL); |
---|
| 1229 | + pthread_cond_init(&top->qe.cond, NULL); |
---|
| 1230 | +} |
---|
| 1231 | + |
---|
1072 | 1232 | static int __cmd_top(struct perf_top *top) |
---|
1073 | 1233 | { |
---|
1074 | | - char msg[512]; |
---|
1075 | | - struct perf_evsel *pos; |
---|
1076 | | - struct perf_evsel_config_term *err_term; |
---|
1077 | | - struct perf_evlist *evlist = top->evlist; |
---|
1078 | 1234 | struct record_opts *opts = &top->record_opts; |
---|
1079 | | - pthread_t thread; |
---|
| 1235 | + pthread_t thread, thread_process; |
---|
1080 | 1236 | int ret; |
---|
1081 | | - |
---|
1082 | | - top->session = perf_session__new(NULL, false, NULL); |
---|
1083 | | - if (top->session == NULL) |
---|
1084 | | - return -1; |
---|
1085 | 1237 | |
---|
1086 | 1238 | if (!top->annotation_opts.objdump_path) { |
---|
1087 | 1239 | ret = perf_env__lookup_objdump(&top->session->header.env, |
---|
1088 | 1240 | &top->annotation_opts.objdump_path); |
---|
1089 | 1241 | if (ret) |
---|
1090 | | - goto out_delete; |
---|
| 1242 | + return ret; |
---|
1091 | 1243 | } |
---|
1092 | 1244 | |
---|
1093 | 1245 | ret = callchain_param__setup_sample_type(&callchain_param); |
---|
1094 | 1246 | if (ret) |
---|
1095 | | - goto out_delete; |
---|
| 1247 | + return ret; |
---|
1096 | 1248 | |
---|
1097 | 1249 | if (perf_session__register_idle_thread(top->session) < 0) |
---|
1098 | | - goto out_delete; |
---|
| 1250 | + return ret; |
---|
1099 | 1251 | |
---|
1100 | 1252 | if (top->nr_threads_synthesize > 1) |
---|
1101 | 1253 | perf_set_multithreaded(); |
---|
1102 | 1254 | |
---|
| 1255 | + init_process_thread(top); |
---|
| 1256 | + |
---|
| 1257 | + if (opts->record_namespaces) |
---|
| 1258 | + top->tool.namespace_events = true; |
---|
| 1259 | + if (opts->record_cgroup) { |
---|
| 1260 | +#ifdef HAVE_FILE_HANDLE |
---|
| 1261 | + top->tool.cgroup_events = true; |
---|
| 1262 | +#else |
---|
| 1263 | + pr_err("cgroup tracking is not supported.\n"); |
---|
| 1264 | + return -1; |
---|
| 1265 | +#endif |
---|
| 1266 | + } |
---|
| 1267 | + |
---|
| 1268 | + ret = perf_event__synthesize_bpf_events(top->session, perf_event__process, |
---|
| 1269 | + &top->session->machines.host, |
---|
| 1270 | + &top->record_opts); |
---|
| 1271 | + if (ret < 0) |
---|
| 1272 | + pr_debug("Couldn't synthesize BPF events: Pre-existing BPF programs won't have symbols resolved.\n"); |
---|
| 1273 | + |
---|
| 1274 | + ret = perf_event__synthesize_cgroups(&top->tool, perf_event__process, |
---|
| 1275 | + &top->session->machines.host); |
---|
| 1276 | + if (ret < 0) |
---|
| 1277 | + pr_debug("Couldn't synthesize cgroup events.\n"); |
---|
| 1278 | + |
---|
1103 | 1279 | machine__synthesize_threads(&top->session->machines.host, &opts->target, |
---|
1104 | | - top->evlist->threads, false, |
---|
1105 | | - opts->proc_map_timeout, |
---|
| 1280 | + top->evlist->core.threads, false, |
---|
1106 | 1281 | top->nr_threads_synthesize); |
---|
1107 | 1282 | |
---|
1108 | 1283 | if (top->nr_threads_synthesize > 1) |
---|
.. | .. |
---|
1110 | 1285 | |
---|
1111 | 1286 | if (perf_hpp_list.socket) { |
---|
1112 | 1287 | ret = perf_env__read_cpu_topology_map(&perf_env); |
---|
1113 | | - if (ret < 0) |
---|
1114 | | - goto out_err_cpu_topo; |
---|
| 1288 | + if (ret < 0) { |
---|
| 1289 | + char errbuf[BUFSIZ]; |
---|
| 1290 | + const char *err = str_error_r(-ret, errbuf, sizeof(errbuf)); |
---|
| 1291 | + |
---|
| 1292 | + ui__error("Could not read the CPU topology map: %s\n", err); |
---|
| 1293 | + return ret; |
---|
| 1294 | + } |
---|
1115 | 1295 | } |
---|
1116 | 1296 | |
---|
1117 | 1297 | ret = perf_top__start_counters(top); |
---|
1118 | 1298 | if (ret) |
---|
1119 | | - goto out_delete; |
---|
1120 | | - |
---|
1121 | | - ret = perf_evlist__apply_drv_configs(evlist, &pos, &err_term); |
---|
1122 | | - if (ret) { |
---|
1123 | | - pr_err("failed to set config \"%s\" on event %s with %d (%s)\n", |
---|
1124 | | - err_term->val.drv_cfg, perf_evsel__name(pos), errno, |
---|
1125 | | - str_error_r(errno, msg, sizeof(msg))); |
---|
1126 | | - goto out_delete; |
---|
1127 | | - } |
---|
| 1299 | + return ret; |
---|
1128 | 1300 | |
---|
1129 | 1301 | top->session->evlist = top->evlist; |
---|
1130 | 1302 | perf_session__set_id_hdr_size(top->session); |
---|
.. | .. |
---|
1138 | 1310 | * so leave the check here. |
---|
1139 | 1311 | */ |
---|
1140 | 1312 | if (!target__none(&opts->target)) |
---|
1141 | | - perf_evlist__enable(top->evlist); |
---|
1142 | | - |
---|
1143 | | - /* Wait for a minimal set of events before starting the snapshot */ |
---|
1144 | | - perf_evlist__poll(top->evlist, 100); |
---|
1145 | | - |
---|
1146 | | - perf_top__mmap_read(top); |
---|
| 1313 | + evlist__enable(top->evlist); |
---|
1147 | 1314 | |
---|
1148 | 1315 | ret = -1; |
---|
| 1316 | + if (pthread_create(&thread_process, NULL, process_thread, top)) { |
---|
| 1317 | + ui__error("Could not create process thread.\n"); |
---|
| 1318 | + return ret; |
---|
| 1319 | + } |
---|
| 1320 | + |
---|
1149 | 1321 | if (pthread_create(&thread, NULL, (use_browser > 0 ? display_thread_tui : |
---|
1150 | 1322 | display_thread), top)) { |
---|
1151 | 1323 | ui__error("Could not create display thread.\n"); |
---|
1152 | | - goto out_delete; |
---|
| 1324 | + goto out_join_thread; |
---|
1153 | 1325 | } |
---|
1154 | 1326 | |
---|
1155 | 1327 | if (top->realtime_prio) { |
---|
.. | .. |
---|
1162 | 1334 | } |
---|
1163 | 1335 | } |
---|
1164 | 1336 | |
---|
| 1337 | + /* Wait for a minimal set of events before starting the snapshot */ |
---|
| 1338 | + evlist__poll(top->evlist, 100); |
---|
| 1339 | + |
---|
| 1340 | + perf_top__mmap_read(top); |
---|
| 1341 | + |
---|
1165 | 1342 | while (!done) { |
---|
1166 | 1343 | u64 hits = top->samples; |
---|
1167 | 1344 | |
---|
1168 | 1345 | perf_top__mmap_read(top); |
---|
1169 | 1346 | |
---|
1170 | 1347 | if (opts->overwrite || (hits == top->samples)) |
---|
1171 | | - ret = perf_evlist__poll(top->evlist, 100); |
---|
| 1348 | + ret = evlist__poll(top->evlist, 100); |
---|
1172 | 1349 | |
---|
1173 | 1350 | if (resize) { |
---|
1174 | 1351 | perf_top__resize(top); |
---|
.. | .. |
---|
1179 | 1356 | ret = 0; |
---|
1180 | 1357 | out_join: |
---|
1181 | 1358 | pthread_join(thread, NULL); |
---|
1182 | | -out_delete: |
---|
1183 | | - perf_session__delete(top->session); |
---|
1184 | | - top->session = NULL; |
---|
1185 | | - |
---|
| 1359 | +out_join_thread: |
---|
| 1360 | + pthread_cond_signal(&top->qe.cond); |
---|
| 1361 | + pthread_join(thread_process, NULL); |
---|
1186 | 1362 | return ret; |
---|
1187 | | - |
---|
1188 | | -out_err_cpu_topo: { |
---|
1189 | | - char errbuf[BUFSIZ]; |
---|
1190 | | - const char *err = str_error_r(-ret, errbuf, sizeof(errbuf)); |
---|
1191 | | - |
---|
1192 | | - ui__error("Could not read the CPU topology map: %s\n", err); |
---|
1193 | | - goto out_delete; |
---|
1194 | | -} |
---|
1195 | 1363 | } |
---|
1196 | 1364 | |
---|
1197 | 1365 | static int |
---|
.. | .. |
---|
1262 | 1430 | .target = { |
---|
1263 | 1431 | .uses_mmap = true, |
---|
1264 | 1432 | }, |
---|
1265 | | - .proc_map_timeout = 500, |
---|
1266 | | - .overwrite = 1, |
---|
| 1433 | + /* |
---|
| 1434 | + * FIXME: This will lose PERF_RECORD_MMAP and other metadata |
---|
| 1435 | + * when we pause, fix that and reenable. Probably using a |
---|
| 1436 | + * separate evlist with a dummy event, i.e. a non-overwrite |
---|
| 1437 | + * ring buffer just for metadata events, while PERF_RECORD_SAMPLE |
---|
| 1438 | + * stays in overwrite mode. -acme |
---|
| 1439 | + * */ |
---|
| 1440 | + .overwrite = 0, |
---|
| 1441 | + .sample_time = true, |
---|
| 1442 | + .sample_time_set = true, |
---|
1267 | 1443 | }, |
---|
1268 | 1444 | .max_stack = sysctl__max_stack(), |
---|
1269 | 1445 | .annotation_opts = annotation__default_options, |
---|
.. | .. |
---|
1288 | 1464 | "file", "vmlinux pathname"), |
---|
1289 | 1465 | OPT_BOOLEAN(0, "ignore-vmlinux", &symbol_conf.ignore_vmlinux, |
---|
1290 | 1466 | "don't load vmlinux even if found"), |
---|
| 1467 | + OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name, |
---|
| 1468 | + "file", "kallsyms pathname"), |
---|
1291 | 1469 | OPT_BOOLEAN('K', "hide_kernel_symbols", &top.hide_kernel_symbols, |
---|
1292 | 1470 | "hide kernel symbols"), |
---|
1293 | 1471 | OPT_CALLBACK('m', "mmap-pages", &opts->mmap_pages, "pages", |
---|
.. | .. |
---|
1354 | 1532 | "Display raw encoding of assembly instructions (default)"), |
---|
1355 | 1533 | OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel, |
---|
1356 | 1534 | "Enable kernel symbol demangling"), |
---|
| 1535 | + OPT_BOOLEAN(0, "no-bpf-event", &top.record_opts.no_bpf_event, "do not record bpf events"), |
---|
1357 | 1536 | OPT_STRING(0, "objdump", &top.annotation_opts.objdump_path, "path", |
---|
1358 | 1537 | "objdump binary to use for disassembly and annotations"), |
---|
1359 | 1538 | OPT_STRING('M', "disassembler-style", &top.annotation_opts.disassembler_style, "disassembler style", |
---|
1360 | 1539 | "Specify disassembler style (e.g. -M intel for intel syntax)"), |
---|
| 1540 | + OPT_STRING(0, "prefix", &top.annotation_opts.prefix, "prefix", |
---|
| 1541 | + "Add prefix to source file path names in programs (with --prefix-strip)"), |
---|
| 1542 | + OPT_STRING(0, "prefix-strip", &top.annotation_opts.prefix_strip, "N", |
---|
| 1543 | + "Strip first N entries of source file path name in programs (with --prefix)"), |
---|
1361 | 1544 | OPT_STRING('u', "uid", &target->uid_str, "user", "user to profile"), |
---|
1362 | 1545 | OPT_CALLBACK(0, "percent-limit", &top, "percent", |
---|
1363 | 1546 | "Don't show entries under that percent", parse_percent_limit), |
---|
.. | .. |
---|
1366 | 1549 | OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str, |
---|
1367 | 1550 | "width[,width...]", |
---|
1368 | 1551 | "don't try to adjust column width, use these fixed values"), |
---|
1369 | | - OPT_UINTEGER(0, "proc-map-timeout", &opts->proc_map_timeout, |
---|
| 1552 | + OPT_UINTEGER(0, "proc-map-timeout", &proc_map_timeout, |
---|
1370 | 1553 | "per thread proc mmap processing timeout in ms"), |
---|
1371 | 1554 | OPT_CALLBACK_NOOPT('b', "branch-any", &opts->branch_stack, |
---|
1372 | 1555 | "branch any", "sample any taken branches", |
---|
.. | .. |
---|
1378 | 1561 | "Show raw trace event output (do not use print fmt or plugins)"), |
---|
1379 | 1562 | OPT_BOOLEAN(0, "hierarchy", &symbol_conf.report_hierarchy, |
---|
1380 | 1563 | "Show entries in a hierarchy"), |
---|
| 1564 | + OPT_BOOLEAN(0, "overwrite", &top.record_opts.overwrite, |
---|
| 1565 | + "Use a backward ring buffer, default: no"), |
---|
1381 | 1566 | OPT_BOOLEAN(0, "force", &symbol_conf.force, "don't complain, do it"), |
---|
1382 | 1567 | OPT_UINTEGER(0, "num-thread-synthesize", &top.nr_threads_synthesize, |
---|
1383 | 1568 | "number of thread to run event synthesize"), |
---|
| 1569 | + OPT_BOOLEAN(0, "namespaces", &opts->record_namespaces, |
---|
| 1570 | + "Record namespaces events"), |
---|
| 1571 | + OPT_BOOLEAN(0, "all-cgroups", &opts->record_cgroup, |
---|
| 1572 | + "Record cgroup events"), |
---|
| 1573 | + OPT_INTEGER(0, "group-sort-idx", &symbol_conf.group_sort_idx, |
---|
| 1574 | + "Sort the output by the event at the index n in group. " |
---|
| 1575 | + "If n is invalid, sort by the first event. " |
---|
| 1576 | + "WARNING: should be used on grouped events."), |
---|
| 1577 | + OPT_BOOLEAN(0, "stitch-lbr", &top.stitch_lbr, |
---|
| 1578 | + "Enable LBR callgraph stitching approach"), |
---|
| 1579 | +#ifdef HAVE_LIBPFM |
---|
| 1580 | + OPT_CALLBACK(0, "pfm-events", &top.evlist, "event", |
---|
| 1581 | + "libpfm4 event selector. use 'perf list' to list available events", |
---|
| 1582 | + parse_libpfm_events_option), |
---|
| 1583 | +#endif |
---|
| 1584 | + OPTS_EVSWITCH(&top.evswitch), |
---|
1384 | 1585 | OPT_END() |
---|
1385 | 1586 | }; |
---|
1386 | 1587 | const char * const top_usage[] = { |
---|
.. | .. |
---|
1395 | 1596 | top.annotation_opts.min_pcnt = 5; |
---|
1396 | 1597 | top.annotation_opts.context = 4; |
---|
1397 | 1598 | |
---|
1398 | | - top.evlist = perf_evlist__new(); |
---|
| 1599 | + top.evlist = evlist__new(); |
---|
1399 | 1600 | if (top.evlist == NULL) |
---|
1400 | 1601 | return -ENOMEM; |
---|
1401 | 1602 | |
---|
1402 | 1603 | status = perf_config(perf_top_config, &top); |
---|
1403 | 1604 | if (status) |
---|
1404 | 1605 | return status; |
---|
| 1606 | + /* |
---|
| 1607 | + * Since the per arch annotation init routine may need the cpuid, read |
---|
| 1608 | + * it here, since we are not getting this from the perf.data header. |
---|
| 1609 | + */ |
---|
| 1610 | + status = perf_env__read_cpuid(&perf_env); |
---|
| 1611 | + if (status) { |
---|
| 1612 | + /* |
---|
| 1613 | + * Some arches do not provide a get_cpuid(), so just use pr_debug, otherwise |
---|
| 1614 | + * warn the user explicitely. |
---|
| 1615 | + */ |
---|
| 1616 | + eprintf(status == ENOSYS ? 1 : 0, verbose, |
---|
| 1617 | + "Couldn't read the cpuid for this machine: %s\n", |
---|
| 1618 | + str_error_r(errno, errbuf, sizeof(errbuf))); |
---|
| 1619 | + } |
---|
| 1620 | + top.evlist->env = &perf_env; |
---|
1405 | 1621 | |
---|
1406 | 1622 | argc = parse_options(argc, argv, options, top_usage, 0); |
---|
1407 | 1623 | if (argc) |
---|
1408 | 1624 | usage_with_options(top_usage, options); |
---|
1409 | 1625 | |
---|
1410 | | - if (!top.evlist->nr_entries && |
---|
1411 | | - perf_evlist__add_default(top.evlist) < 0) { |
---|
| 1626 | + if (annotate_check_args(&top.annotation_opts) < 0) |
---|
| 1627 | + goto out_delete_evlist; |
---|
| 1628 | + |
---|
| 1629 | + if (!top.evlist->core.nr_entries && |
---|
| 1630 | + evlist__add_default(top.evlist) < 0) { |
---|
1412 | 1631 | pr_err("Not enough memory for event selector list\n"); |
---|
1413 | 1632 | goto out_delete_evlist; |
---|
1414 | 1633 | } |
---|
| 1634 | + |
---|
| 1635 | + status = evswitch__init(&top.evswitch, top.evlist, stderr); |
---|
| 1636 | + if (status) |
---|
| 1637 | + goto out_delete_evlist; |
---|
1415 | 1638 | |
---|
1416 | 1639 | if (symbol_conf.report_hierarchy) { |
---|
1417 | 1640 | /* disable incompatible options */ |
---|
.. | .. |
---|
1425 | 1648 | goto out_delete_evlist; |
---|
1426 | 1649 | } |
---|
1427 | 1650 | } |
---|
| 1651 | + |
---|
| 1652 | + if (top.stitch_lbr && !(callchain_param.record_mode == CALLCHAIN_LBR)) { |
---|
| 1653 | + pr_err("Error: --stitch-lbr must be used with --call-graph lbr\n"); |
---|
| 1654 | + goto out_delete_evlist; |
---|
| 1655 | + } |
---|
| 1656 | + |
---|
| 1657 | + if (opts->branch_stack && callchain_param.enabled) |
---|
| 1658 | + symbol_conf.show_branchflag_count = true; |
---|
1428 | 1659 | |
---|
1429 | 1660 | sort__mode = SORT_MODE__TOP; |
---|
1430 | 1661 | /* display thread wants entries to be collapsed in a different tree */ |
---|
.. | .. |
---|
1480 | 1711 | goto out_delete_evlist; |
---|
1481 | 1712 | } |
---|
1482 | 1713 | |
---|
1483 | | - top.sym_evsel = perf_evlist__first(top.evlist); |
---|
| 1714 | + top.sym_evsel = evlist__first(top.evlist); |
---|
1484 | 1715 | |
---|
1485 | 1716 | if (!callchain_param.enabled) { |
---|
1486 | 1717 | symbol_conf.cumulate_callchain = false; |
---|
.. | .. |
---|
1494 | 1725 | if (status < 0) |
---|
1495 | 1726 | goto out_delete_evlist; |
---|
1496 | 1727 | |
---|
1497 | | - annotation_config__init(); |
---|
| 1728 | + annotation_config__init(&top.annotation_opts); |
---|
1498 | 1729 | |
---|
1499 | 1730 | symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL); |
---|
1500 | 1731 | status = symbol__init(NULL); |
---|
.. | .. |
---|
1509 | 1740 | signal(SIGWINCH, winch_sig); |
---|
1510 | 1741 | } |
---|
1511 | 1742 | |
---|
| 1743 | + top.session = perf_session__new(NULL, false, NULL); |
---|
| 1744 | + if (IS_ERR(top.session)) { |
---|
| 1745 | + status = PTR_ERR(top.session); |
---|
| 1746 | + top.session = NULL; |
---|
| 1747 | + goto out_delete_evlist; |
---|
| 1748 | + } |
---|
| 1749 | + |
---|
| 1750 | +#ifdef HAVE_LIBBPF_SUPPORT |
---|
| 1751 | + if (!top.record_opts.no_bpf_event) { |
---|
| 1752 | + top.sb_evlist = evlist__new(); |
---|
| 1753 | + |
---|
| 1754 | + if (top.sb_evlist == NULL) { |
---|
| 1755 | + pr_err("Couldn't create side band evlist.\n."); |
---|
| 1756 | + goto out_delete_evlist; |
---|
| 1757 | + } |
---|
| 1758 | + |
---|
| 1759 | + if (evlist__add_bpf_sb_event(top.sb_evlist, &perf_env)) { |
---|
| 1760 | + pr_err("Couldn't ask for PERF_RECORD_BPF_EVENT side band events.\n."); |
---|
| 1761 | + goto out_delete_evlist; |
---|
| 1762 | + } |
---|
| 1763 | + } |
---|
| 1764 | +#endif |
---|
| 1765 | + |
---|
| 1766 | + if (perf_evlist__start_sb_thread(top.sb_evlist, target)) { |
---|
| 1767 | + pr_debug("Couldn't start the BPF side band thread:\nBPF programs starting from now on won't be annotatable\n"); |
---|
| 1768 | + opts->no_bpf_event = true; |
---|
| 1769 | + } |
---|
| 1770 | + |
---|
1512 | 1771 | status = __cmd_top(&top); |
---|
1513 | 1772 | |
---|
| 1773 | + if (!opts->no_bpf_event) |
---|
| 1774 | + perf_evlist__stop_sb_thread(top.sb_evlist); |
---|
| 1775 | + |
---|
1514 | 1776 | out_delete_evlist: |
---|
1515 | | - perf_evlist__delete(top.evlist); |
---|
| 1777 | + evlist__delete(top.evlist); |
---|
| 1778 | + perf_session__delete(top.session); |
---|
1516 | 1779 | |
---|
1517 | 1780 | return status; |
---|
1518 | 1781 | } |
---|