xref: /OK3568_Linux_fs/kernel/tools/perf/builtin-top.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * builtin-top.c
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Builtin top command: Display a continuously updated profile of
6*4882a593Smuzhiyun  * any workload, CPU or specific PID.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * Copyright (C) 2008, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
9*4882a593Smuzhiyun  *		 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * Improvements and fixes by:
12*4882a593Smuzhiyun  *
13*4882a593Smuzhiyun  *   Arjan van de Ven <arjan@linux.intel.com>
14*4882a593Smuzhiyun  *   Yanmin Zhang <yanmin.zhang@intel.com>
15*4882a593Smuzhiyun  *   Wu Fengguang <fengguang.wu@intel.com>
16*4882a593Smuzhiyun  *   Mike Galbraith <efault@gmx.de>
17*4882a593Smuzhiyun  *   Paul Mackerras <paulus@samba.org>
18*4882a593Smuzhiyun  */
19*4882a593Smuzhiyun #include "builtin.h"
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun #include "perf.h"
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun #include "util/annotate.h"
24*4882a593Smuzhiyun #include "util/bpf-event.h"
25*4882a593Smuzhiyun #include "util/config.h"
26*4882a593Smuzhiyun #include "util/color.h"
27*4882a593Smuzhiyun #include "util/dso.h"
28*4882a593Smuzhiyun #include "util/evlist.h"
29*4882a593Smuzhiyun #include "util/evsel.h"
30*4882a593Smuzhiyun #include "util/evsel_config.h"
31*4882a593Smuzhiyun #include "util/event.h"
32*4882a593Smuzhiyun #include "util/machine.h"
33*4882a593Smuzhiyun #include "util/map.h"
34*4882a593Smuzhiyun #include "util/mmap.h"
35*4882a593Smuzhiyun #include "util/session.h"
36*4882a593Smuzhiyun #include "util/thread.h"
37*4882a593Smuzhiyun #include "util/symbol.h"
38*4882a593Smuzhiyun #include "util/synthetic-events.h"
39*4882a593Smuzhiyun #include "util/top.h"
40*4882a593Smuzhiyun #include "util/util.h"
41*4882a593Smuzhiyun #include <linux/rbtree.h>
42*4882a593Smuzhiyun #include <subcmd/parse-options.h>
43*4882a593Smuzhiyun #include "util/parse-events.h"
44*4882a593Smuzhiyun #include "util/callchain.h"
45*4882a593Smuzhiyun #include "util/cpumap.h"
46*4882a593Smuzhiyun #include "util/sort.h"
47*4882a593Smuzhiyun #include "util/string2.h"
48*4882a593Smuzhiyun #include "util/term.h"
49*4882a593Smuzhiyun #include "util/intlist.h"
50*4882a593Smuzhiyun #include "util/parse-branch-options.h"
51*4882a593Smuzhiyun #include "arch/common.h"
52*4882a593Smuzhiyun #include "ui/ui.h"
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun #include "util/debug.h"
55*4882a593Smuzhiyun #include "util/ordered-events.h"
56*4882a593Smuzhiyun #include "util/pfm.h"
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun #include <assert.h>
59*4882a593Smuzhiyun #include <elf.h>
60*4882a593Smuzhiyun #include <fcntl.h>
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun #include <stdio.h>
63*4882a593Smuzhiyun #include <termios.h>
64*4882a593Smuzhiyun #include <unistd.h>
65*4882a593Smuzhiyun #include <inttypes.h>
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun #include <errno.h>
68*4882a593Smuzhiyun #include <time.h>
69*4882a593Smuzhiyun #include <sched.h>
70*4882a593Smuzhiyun #include <signal.h>
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun #include <sys/syscall.h>
73*4882a593Smuzhiyun #include <sys/ioctl.h>
74*4882a593Smuzhiyun #include <poll.h>
75*4882a593Smuzhiyun #include <sys/prctl.h>
76*4882a593Smuzhiyun #include <sys/wait.h>
77*4882a593Smuzhiyun #include <sys/uio.h>
78*4882a593Smuzhiyun #include <sys/utsname.h>
79*4882a593Smuzhiyun #include <sys/mman.h>
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun #include <linux/stringify.h>
82*4882a593Smuzhiyun #include <linux/time64.h>
83*4882a593Smuzhiyun #include <linux/types.h>
84*4882a593Smuzhiyun #include <linux/err.h>
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun #include <linux/ctype.h>
87*4882a593Smuzhiyun #include <perf/mmap.h>
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun static volatile int done;
90*4882a593Smuzhiyun static volatile int resize;
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun #define HEADER_LINE_NR  5
93*4882a593Smuzhiyun 
perf_top__update_print_entries(struct perf_top * top)94*4882a593Smuzhiyun static void perf_top__update_print_entries(struct perf_top *top)
95*4882a593Smuzhiyun {
96*4882a593Smuzhiyun 	top->print_entries = top->winsize.ws_row - HEADER_LINE_NR;
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun 
winch_sig(int sig __maybe_unused)99*4882a593Smuzhiyun static void winch_sig(int sig __maybe_unused)
100*4882a593Smuzhiyun {
101*4882a593Smuzhiyun 	resize = 1;
102*4882a593Smuzhiyun }
103*4882a593Smuzhiyun 
perf_top__resize(struct perf_top * top)104*4882a593Smuzhiyun static void perf_top__resize(struct perf_top *top)
105*4882a593Smuzhiyun {
106*4882a593Smuzhiyun 	get_term_dimensions(&top->winsize);
107*4882a593Smuzhiyun 	perf_top__update_print_entries(top);
108*4882a593Smuzhiyun }
109*4882a593Smuzhiyun 
perf_top__parse_source(struct perf_top * top,struct hist_entry * he)110*4882a593Smuzhiyun static int perf_top__parse_source(struct perf_top *top, struct hist_entry *he)
111*4882a593Smuzhiyun {
112*4882a593Smuzhiyun 	struct evsel *evsel;
113*4882a593Smuzhiyun 	struct symbol *sym;
114*4882a593Smuzhiyun 	struct annotation *notes;
115*4882a593Smuzhiyun 	struct map *map;
116*4882a593Smuzhiyun 	int err = -1;
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun 	if (!he || !he->ms.sym)
119*4882a593Smuzhiyun 		return -1;
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	evsel = hists_to_evsel(he->hists);
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun 	sym = he->ms.sym;
124*4882a593Smuzhiyun 	map = he->ms.map;
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun 	/*
127*4882a593Smuzhiyun 	 * We can't annotate with just /proc/kallsyms
128*4882a593Smuzhiyun 	 */
129*4882a593Smuzhiyun 	if (map->dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS &&
130*4882a593Smuzhiyun 	    !dso__is_kcore(map->dso)) {
131*4882a593Smuzhiyun 		pr_err("Can't annotate %s: No vmlinux file was found in the "
132*4882a593Smuzhiyun 		       "path\n", sym->name);
133*4882a593Smuzhiyun 		sleep(1);
134*4882a593Smuzhiyun 		return -1;
135*4882a593Smuzhiyun 	}
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun 	notes = symbol__annotation(sym);
138*4882a593Smuzhiyun 	pthread_mutex_lock(&notes->lock);
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun 	if (!symbol__hists(sym, top->evlist->core.nr_entries)) {
141*4882a593Smuzhiyun 		pthread_mutex_unlock(&notes->lock);
142*4882a593Smuzhiyun 		pr_err("Not enough memory for annotating '%s' symbol!\n",
143*4882a593Smuzhiyun 		       sym->name);
144*4882a593Smuzhiyun 		sleep(1);
145*4882a593Smuzhiyun 		return err;
146*4882a593Smuzhiyun 	}
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 	err = symbol__annotate(&he->ms, evsel, &top->annotation_opts, NULL);
149*4882a593Smuzhiyun 	if (err == 0) {
150*4882a593Smuzhiyun 		top->sym_filter_entry = he;
151*4882a593Smuzhiyun 	} else {
152*4882a593Smuzhiyun 		char msg[BUFSIZ];
153*4882a593Smuzhiyun 		symbol__strerror_disassemble(&he->ms, err, msg, sizeof(msg));
154*4882a593Smuzhiyun 		pr_err("Couldn't annotate %s: %s\n", sym->name, msg);
155*4882a593Smuzhiyun 	}
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 	pthread_mutex_unlock(&notes->lock);
158*4882a593Smuzhiyun 	return err;
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun 
__zero_source_counters(struct hist_entry * he)161*4882a593Smuzhiyun static void __zero_source_counters(struct hist_entry *he)
162*4882a593Smuzhiyun {
163*4882a593Smuzhiyun 	struct symbol *sym = he->ms.sym;
164*4882a593Smuzhiyun 	symbol__annotate_zero_histograms(sym);
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun 
ui__warn_map_erange(struct map * map,struct symbol * sym,u64 ip)167*4882a593Smuzhiyun static void ui__warn_map_erange(struct map *map, struct symbol *sym, u64 ip)
168*4882a593Smuzhiyun {
169*4882a593Smuzhiyun 	struct utsname uts;
170*4882a593Smuzhiyun 	int err = uname(&uts);
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 	ui__warning("Out of bounds address found:\n\n"
173*4882a593Smuzhiyun 		    "Addr:   %" PRIx64 "\n"
174*4882a593Smuzhiyun 		    "DSO:    %s %c\n"
175*4882a593Smuzhiyun 		    "Map:    %" PRIx64 "-%" PRIx64 "\n"
176*4882a593Smuzhiyun 		    "Symbol: %" PRIx64 "-%" PRIx64 " %c %s\n"
177*4882a593Smuzhiyun 		    "Arch:   %s\n"
178*4882a593Smuzhiyun 		    "Kernel: %s\n"
179*4882a593Smuzhiyun 		    "Tools:  %s\n\n"
180*4882a593Smuzhiyun 		    "Not all samples will be on the annotation output.\n\n"
181*4882a593Smuzhiyun 		    "Please report to linux-kernel@vger.kernel.org\n",
182*4882a593Smuzhiyun 		    ip, map->dso->long_name, dso__symtab_origin(map->dso),
183*4882a593Smuzhiyun 		    map->start, map->end, sym->start, sym->end,
184*4882a593Smuzhiyun 		    sym->binding == STB_GLOBAL ? 'g' :
185*4882a593Smuzhiyun 		    sym->binding == STB_LOCAL  ? 'l' : 'w', sym->name,
186*4882a593Smuzhiyun 		    err ? "[unknown]" : uts.machine,
187*4882a593Smuzhiyun 		    err ? "[unknown]" : uts.release, perf_version_string);
188*4882a593Smuzhiyun 	if (use_browser <= 0)
189*4882a593Smuzhiyun 		sleep(5);
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun 	map->erange_warned = true;
192*4882a593Smuzhiyun }
193*4882a593Smuzhiyun 
perf_top__record_precise_ip(struct perf_top * top,struct hist_entry * he,struct perf_sample * sample,struct evsel * evsel,u64 ip)194*4882a593Smuzhiyun static void perf_top__record_precise_ip(struct perf_top *top,
195*4882a593Smuzhiyun 					struct hist_entry *he,
196*4882a593Smuzhiyun 					struct perf_sample *sample,
197*4882a593Smuzhiyun 					struct evsel *evsel, u64 ip)
198*4882a593Smuzhiyun {
199*4882a593Smuzhiyun 	struct annotation *notes;
200*4882a593Smuzhiyun 	struct symbol *sym = he->ms.sym;
201*4882a593Smuzhiyun 	int err = 0;
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun 	if (sym == NULL || (use_browser == 0 &&
204*4882a593Smuzhiyun 			    (top->sym_filter_entry == NULL ||
205*4882a593Smuzhiyun 			     top->sym_filter_entry->ms.sym != sym)))
206*4882a593Smuzhiyun 		return;
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun 	notes = symbol__annotation(sym);
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	if (pthread_mutex_trylock(&notes->lock))
211*4882a593Smuzhiyun 		return;
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 	err = hist_entry__inc_addr_samples(he, sample, evsel, ip);
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun 	pthread_mutex_unlock(&notes->lock);
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun 	if (unlikely(err)) {
218*4882a593Smuzhiyun 		/*
219*4882a593Smuzhiyun 		 * This function is now called with he->hists->lock held.
220*4882a593Smuzhiyun 		 * Release it before going to sleep.
221*4882a593Smuzhiyun 		 */
222*4882a593Smuzhiyun 		pthread_mutex_unlock(&he->hists->lock);
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 		if (err == -ERANGE && !he->ms.map->erange_warned)
225*4882a593Smuzhiyun 			ui__warn_map_erange(he->ms.map, sym, ip);
226*4882a593Smuzhiyun 		else if (err == -ENOMEM) {
227*4882a593Smuzhiyun 			pr_err("Not enough memory for annotating '%s' symbol!\n",
228*4882a593Smuzhiyun 			       sym->name);
229*4882a593Smuzhiyun 			sleep(1);
230*4882a593Smuzhiyun 		}
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun 		pthread_mutex_lock(&he->hists->lock);
233*4882a593Smuzhiyun 	}
234*4882a593Smuzhiyun }
235*4882a593Smuzhiyun 
perf_top__show_details(struct perf_top * top)236*4882a593Smuzhiyun static void perf_top__show_details(struct perf_top *top)
237*4882a593Smuzhiyun {
238*4882a593Smuzhiyun 	struct hist_entry *he = top->sym_filter_entry;
239*4882a593Smuzhiyun 	struct evsel *evsel;
240*4882a593Smuzhiyun 	struct annotation *notes;
241*4882a593Smuzhiyun 	struct symbol *symbol;
242*4882a593Smuzhiyun 	int more;
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 	if (!he)
245*4882a593Smuzhiyun 		return;
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 	evsel = hists_to_evsel(he->hists);
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun 	symbol = he->ms.sym;
250*4882a593Smuzhiyun 	notes = symbol__annotation(symbol);
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun 	pthread_mutex_lock(&notes->lock);
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun 	symbol__calc_percent(symbol, evsel);
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun 	if (notes->src == NULL)
257*4882a593Smuzhiyun 		goto out_unlock;
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun 	printf("Showing %s for %s\n", evsel__name(top->sym_evsel), symbol->name);
260*4882a593Smuzhiyun 	printf("  Events  Pcnt (>=%d%%)\n", top->annotation_opts.min_pcnt);
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun 	more = symbol__annotate_printf(&he->ms, top->sym_evsel, &top->annotation_opts);
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun 	if (top->evlist->enabled) {
265*4882a593Smuzhiyun 		if (top->zero)
266*4882a593Smuzhiyun 			symbol__annotate_zero_histogram(symbol, top->sym_evsel->idx);
267*4882a593Smuzhiyun 		else
268*4882a593Smuzhiyun 			symbol__annotate_decay_histogram(symbol, top->sym_evsel->idx);
269*4882a593Smuzhiyun 	}
270*4882a593Smuzhiyun 	if (more != 0)
271*4882a593Smuzhiyun 		printf("%d lines not displayed, maybe increase display entries [e]\n", more);
272*4882a593Smuzhiyun out_unlock:
273*4882a593Smuzhiyun 	pthread_mutex_unlock(&notes->lock);
274*4882a593Smuzhiyun }
275*4882a593Smuzhiyun 
perf_top__resort_hists(struct perf_top * t)276*4882a593Smuzhiyun static void perf_top__resort_hists(struct perf_top *t)
277*4882a593Smuzhiyun {
278*4882a593Smuzhiyun 	struct evlist *evlist = t->evlist;
279*4882a593Smuzhiyun 	struct evsel *pos;
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun 	evlist__for_each_entry(evlist, pos) {
282*4882a593Smuzhiyun 		struct hists *hists = evsel__hists(pos);
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun 		/*
285*4882a593Smuzhiyun 		 * unlink existing entries so that they can be linked
286*4882a593Smuzhiyun 		 * in a correct order in hists__match() below.
287*4882a593Smuzhiyun 		 */
288*4882a593Smuzhiyun 		hists__unlink(hists);
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun 		if (evlist->enabled) {
291*4882a593Smuzhiyun 			if (t->zero) {
292*4882a593Smuzhiyun 				hists__delete_entries(hists);
293*4882a593Smuzhiyun 			} else {
294*4882a593Smuzhiyun 				hists__decay_entries(hists, t->hide_user_symbols,
295*4882a593Smuzhiyun 						     t->hide_kernel_symbols);
296*4882a593Smuzhiyun 			}
297*4882a593Smuzhiyun 		}
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun 		hists__collapse_resort(hists, NULL);
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun 		/* Non-group events are considered as leader */
302*4882a593Smuzhiyun 		if (symbol_conf.event_group && !evsel__is_group_leader(pos)) {
303*4882a593Smuzhiyun 			struct hists *leader_hists = evsel__hists(pos->leader);
304*4882a593Smuzhiyun 
305*4882a593Smuzhiyun 			hists__match(leader_hists, hists);
306*4882a593Smuzhiyun 			hists__link(leader_hists, hists);
307*4882a593Smuzhiyun 		}
308*4882a593Smuzhiyun 	}
309*4882a593Smuzhiyun 
310*4882a593Smuzhiyun 	evlist__for_each_entry(evlist, pos) {
311*4882a593Smuzhiyun 		evsel__output_resort(pos, NULL);
312*4882a593Smuzhiyun 	}
313*4882a593Smuzhiyun }
314*4882a593Smuzhiyun 
perf_top__print_sym_table(struct perf_top * top)315*4882a593Smuzhiyun static void perf_top__print_sym_table(struct perf_top *top)
316*4882a593Smuzhiyun {
317*4882a593Smuzhiyun 	char bf[160];
318*4882a593Smuzhiyun 	int printed = 0;
319*4882a593Smuzhiyun 	const int win_width = top->winsize.ws_col - 1;
320*4882a593Smuzhiyun 	struct evsel *evsel = top->sym_evsel;
321*4882a593Smuzhiyun 	struct hists *hists = evsel__hists(evsel);
322*4882a593Smuzhiyun 
323*4882a593Smuzhiyun 	puts(CONSOLE_CLEAR);
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 	perf_top__header_snprintf(top, bf, sizeof(bf));
326*4882a593Smuzhiyun 	printf("%s\n", bf);
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun 	printf("%-*.*s\n", win_width, win_width, graph_dotted_line);
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun 	if (!top->record_opts.overwrite &&
331*4882a593Smuzhiyun 	    (hists->stats.nr_lost_warned !=
332*4882a593Smuzhiyun 	    hists->stats.nr_events[PERF_RECORD_LOST])) {
333*4882a593Smuzhiyun 		hists->stats.nr_lost_warned =
334*4882a593Smuzhiyun 			      hists->stats.nr_events[PERF_RECORD_LOST];
335*4882a593Smuzhiyun 		color_fprintf(stdout, PERF_COLOR_RED,
336*4882a593Smuzhiyun 			      "WARNING: LOST %d chunks, Check IO/CPU overload",
337*4882a593Smuzhiyun 			      hists->stats.nr_lost_warned);
338*4882a593Smuzhiyun 		++printed;
339*4882a593Smuzhiyun 	}
340*4882a593Smuzhiyun 
341*4882a593Smuzhiyun 	if (top->sym_filter_entry) {
342*4882a593Smuzhiyun 		perf_top__show_details(top);
343*4882a593Smuzhiyun 		return;
344*4882a593Smuzhiyun 	}
345*4882a593Smuzhiyun 
346*4882a593Smuzhiyun 	perf_top__resort_hists(top);
347*4882a593Smuzhiyun 
348*4882a593Smuzhiyun 	hists__output_recalc_col_len(hists, top->print_entries - printed);
349*4882a593Smuzhiyun 	putchar('\n');
350*4882a593Smuzhiyun 	hists__fprintf(hists, false, top->print_entries - printed, win_width,
351*4882a593Smuzhiyun 		       top->min_percent, stdout, !symbol_conf.use_callchain);
352*4882a593Smuzhiyun }
353*4882a593Smuzhiyun 
prompt_integer(int * target,const char * msg)354*4882a593Smuzhiyun static void prompt_integer(int *target, const char *msg)
355*4882a593Smuzhiyun {
356*4882a593Smuzhiyun 	char *buf = malloc(0), *p;
357*4882a593Smuzhiyun 	size_t dummy = 0;
358*4882a593Smuzhiyun 	int tmp;
359*4882a593Smuzhiyun 
360*4882a593Smuzhiyun 	fprintf(stdout, "\n%s: ", msg);
361*4882a593Smuzhiyun 	if (getline(&buf, &dummy, stdin) < 0)
362*4882a593Smuzhiyun 		return;
363*4882a593Smuzhiyun 
364*4882a593Smuzhiyun 	p = strchr(buf, '\n');
365*4882a593Smuzhiyun 	if (p)
366*4882a593Smuzhiyun 		*p = 0;
367*4882a593Smuzhiyun 
368*4882a593Smuzhiyun 	p = buf;
369*4882a593Smuzhiyun 	while(*p) {
370*4882a593Smuzhiyun 		if (!isdigit(*p))
371*4882a593Smuzhiyun 			goto out_free;
372*4882a593Smuzhiyun 		p++;
373*4882a593Smuzhiyun 	}
374*4882a593Smuzhiyun 	tmp = strtoul(buf, NULL, 10);
375*4882a593Smuzhiyun 	*target = tmp;
376*4882a593Smuzhiyun out_free:
377*4882a593Smuzhiyun 	free(buf);
378*4882a593Smuzhiyun }
379*4882a593Smuzhiyun 
prompt_percent(int * target,const char * msg)380*4882a593Smuzhiyun static void prompt_percent(int *target, const char *msg)
381*4882a593Smuzhiyun {
382*4882a593Smuzhiyun 	int tmp = 0;
383*4882a593Smuzhiyun 
384*4882a593Smuzhiyun 	prompt_integer(&tmp, msg);
385*4882a593Smuzhiyun 	if (tmp >= 0 && tmp <= 100)
386*4882a593Smuzhiyun 		*target = tmp;
387*4882a593Smuzhiyun }
388*4882a593Smuzhiyun 
perf_top__prompt_symbol(struct perf_top * top,const char * msg)389*4882a593Smuzhiyun static void perf_top__prompt_symbol(struct perf_top *top, const char *msg)
390*4882a593Smuzhiyun {
391*4882a593Smuzhiyun 	char *buf = malloc(0), *p;
392*4882a593Smuzhiyun 	struct hist_entry *syme = top->sym_filter_entry, *n, *found = NULL;
393*4882a593Smuzhiyun 	struct hists *hists = evsel__hists(top->sym_evsel);
394*4882a593Smuzhiyun 	struct rb_node *next;
395*4882a593Smuzhiyun 	size_t dummy = 0;
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun 	/* zero counters of active symbol */
398*4882a593Smuzhiyun 	if (syme) {
399*4882a593Smuzhiyun 		__zero_source_counters(syme);
400*4882a593Smuzhiyun 		top->sym_filter_entry = NULL;
401*4882a593Smuzhiyun 	}
402*4882a593Smuzhiyun 
403*4882a593Smuzhiyun 	fprintf(stdout, "\n%s: ", msg);
404*4882a593Smuzhiyun 	if (getline(&buf, &dummy, stdin) < 0)
405*4882a593Smuzhiyun 		goto out_free;
406*4882a593Smuzhiyun 
407*4882a593Smuzhiyun 	p = strchr(buf, '\n');
408*4882a593Smuzhiyun 	if (p)
409*4882a593Smuzhiyun 		*p = 0;
410*4882a593Smuzhiyun 
411*4882a593Smuzhiyun 	next = rb_first_cached(&hists->entries);
412*4882a593Smuzhiyun 	while (next) {
413*4882a593Smuzhiyun 		n = rb_entry(next, struct hist_entry, rb_node);
414*4882a593Smuzhiyun 		if (n->ms.sym && !strcmp(buf, n->ms.sym->name)) {
415*4882a593Smuzhiyun 			found = n;
416*4882a593Smuzhiyun 			break;
417*4882a593Smuzhiyun 		}
418*4882a593Smuzhiyun 		next = rb_next(&n->rb_node);
419*4882a593Smuzhiyun 	}
420*4882a593Smuzhiyun 
421*4882a593Smuzhiyun 	if (!found) {
422*4882a593Smuzhiyun 		fprintf(stderr, "Sorry, %s is not active.\n", buf);
423*4882a593Smuzhiyun 		sleep(1);
424*4882a593Smuzhiyun 	} else
425*4882a593Smuzhiyun 		perf_top__parse_source(top, found);
426*4882a593Smuzhiyun 
427*4882a593Smuzhiyun out_free:
428*4882a593Smuzhiyun 	free(buf);
429*4882a593Smuzhiyun }
430*4882a593Smuzhiyun 
perf_top__print_mapped_keys(struct perf_top * top)431*4882a593Smuzhiyun static void perf_top__print_mapped_keys(struct perf_top *top)
432*4882a593Smuzhiyun {
433*4882a593Smuzhiyun 	char *name = NULL;
434*4882a593Smuzhiyun 
435*4882a593Smuzhiyun 	if (top->sym_filter_entry) {
436*4882a593Smuzhiyun 		struct symbol *sym = top->sym_filter_entry->ms.sym;
437*4882a593Smuzhiyun 		name = sym->name;
438*4882a593Smuzhiyun 	}
439*4882a593Smuzhiyun 
440*4882a593Smuzhiyun 	fprintf(stdout, "\nMapped keys:\n");
441*4882a593Smuzhiyun 	fprintf(stdout, "\t[d]     display refresh delay.             \t(%d)\n", top->delay_secs);
442*4882a593Smuzhiyun 	fprintf(stdout, "\t[e]     display entries (lines).           \t(%d)\n", top->print_entries);
443*4882a593Smuzhiyun 
444*4882a593Smuzhiyun 	if (top->evlist->core.nr_entries > 1)
445*4882a593Smuzhiyun 		fprintf(stdout, "\t[E]     active event counter.              \t(%s)\n", evsel__name(top->sym_evsel));
446*4882a593Smuzhiyun 
447*4882a593Smuzhiyun 	fprintf(stdout, "\t[f]     profile display filter (count).    \t(%d)\n", top->count_filter);
448*4882a593Smuzhiyun 
449*4882a593Smuzhiyun 	fprintf(stdout, "\t[F]     annotate display filter (percent). \t(%d%%)\n", top->annotation_opts.min_pcnt);
450*4882a593Smuzhiyun 	fprintf(stdout, "\t[s]     annotate symbol.                   \t(%s)\n", name?: "NULL");
451*4882a593Smuzhiyun 	fprintf(stdout, "\t[S]     stop annotation.\n");
452*4882a593Smuzhiyun 
453*4882a593Smuzhiyun 	fprintf(stdout,
454*4882a593Smuzhiyun 		"\t[K]     hide kernel symbols.             \t(%s)\n",
455*4882a593Smuzhiyun 		top->hide_kernel_symbols ? "yes" : "no");
456*4882a593Smuzhiyun 	fprintf(stdout,
457*4882a593Smuzhiyun 		"\t[U]     hide user symbols.               \t(%s)\n",
458*4882a593Smuzhiyun 		top->hide_user_symbols ? "yes" : "no");
459*4882a593Smuzhiyun 	fprintf(stdout, "\t[z]     toggle sample zeroing.             \t(%d)\n", top->zero ? 1 : 0);
460*4882a593Smuzhiyun 	fprintf(stdout, "\t[qQ]    quit.\n");
461*4882a593Smuzhiyun }
462*4882a593Smuzhiyun 
perf_top__key_mapped(struct perf_top * top,int c)463*4882a593Smuzhiyun static int perf_top__key_mapped(struct perf_top *top, int c)
464*4882a593Smuzhiyun {
465*4882a593Smuzhiyun 	switch (c) {
466*4882a593Smuzhiyun 		case 'd':
467*4882a593Smuzhiyun 		case 'e':
468*4882a593Smuzhiyun 		case 'f':
469*4882a593Smuzhiyun 		case 'z':
470*4882a593Smuzhiyun 		case 'q':
471*4882a593Smuzhiyun 		case 'Q':
472*4882a593Smuzhiyun 		case 'K':
473*4882a593Smuzhiyun 		case 'U':
474*4882a593Smuzhiyun 		case 'F':
475*4882a593Smuzhiyun 		case 's':
476*4882a593Smuzhiyun 		case 'S':
477*4882a593Smuzhiyun 			return 1;
478*4882a593Smuzhiyun 		case 'E':
479*4882a593Smuzhiyun 			return top->evlist->core.nr_entries > 1 ? 1 : 0;
480*4882a593Smuzhiyun 		default:
481*4882a593Smuzhiyun 			break;
482*4882a593Smuzhiyun 	}
483*4882a593Smuzhiyun 
484*4882a593Smuzhiyun 	return 0;
485*4882a593Smuzhiyun }
486*4882a593Smuzhiyun 
perf_top__handle_keypress(struct perf_top * top,int c)487*4882a593Smuzhiyun static bool perf_top__handle_keypress(struct perf_top *top, int c)
488*4882a593Smuzhiyun {
489*4882a593Smuzhiyun 	bool ret = true;
490*4882a593Smuzhiyun 
491*4882a593Smuzhiyun 	if (!perf_top__key_mapped(top, c)) {
492*4882a593Smuzhiyun 		struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
493*4882a593Smuzhiyun 		struct termios save;
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun 		perf_top__print_mapped_keys(top);
496*4882a593Smuzhiyun 		fprintf(stdout, "\nEnter selection, or unmapped key to continue: ");
497*4882a593Smuzhiyun 		fflush(stdout);
498*4882a593Smuzhiyun 
499*4882a593Smuzhiyun 		set_term_quiet_input(&save);
500*4882a593Smuzhiyun 
501*4882a593Smuzhiyun 		poll(&stdin_poll, 1, -1);
502*4882a593Smuzhiyun 		c = getc(stdin);
503*4882a593Smuzhiyun 
504*4882a593Smuzhiyun 		tcsetattr(0, TCSAFLUSH, &save);
505*4882a593Smuzhiyun 		if (!perf_top__key_mapped(top, c))
506*4882a593Smuzhiyun 			return ret;
507*4882a593Smuzhiyun 	}
508*4882a593Smuzhiyun 
509*4882a593Smuzhiyun 	switch (c) {
510*4882a593Smuzhiyun 		case 'd':
511*4882a593Smuzhiyun 			prompt_integer(&top->delay_secs, "Enter display delay");
512*4882a593Smuzhiyun 			if (top->delay_secs < 1)
513*4882a593Smuzhiyun 				top->delay_secs = 1;
514*4882a593Smuzhiyun 			break;
515*4882a593Smuzhiyun 		case 'e':
516*4882a593Smuzhiyun 			prompt_integer(&top->print_entries, "Enter display entries (lines)");
517*4882a593Smuzhiyun 			if (top->print_entries == 0) {
518*4882a593Smuzhiyun 				perf_top__resize(top);
519*4882a593Smuzhiyun 				signal(SIGWINCH, winch_sig);
520*4882a593Smuzhiyun 			} else {
521*4882a593Smuzhiyun 				signal(SIGWINCH, SIG_DFL);
522*4882a593Smuzhiyun 			}
523*4882a593Smuzhiyun 			break;
524*4882a593Smuzhiyun 		case 'E':
525*4882a593Smuzhiyun 			if (top->evlist->core.nr_entries > 1) {
526*4882a593Smuzhiyun 				/* Select 0 as the default event: */
527*4882a593Smuzhiyun 				int counter = 0;
528*4882a593Smuzhiyun 
529*4882a593Smuzhiyun 				fprintf(stderr, "\nAvailable events:");
530*4882a593Smuzhiyun 
531*4882a593Smuzhiyun 				evlist__for_each_entry(top->evlist, top->sym_evsel)
532*4882a593Smuzhiyun 					fprintf(stderr, "\n\t%d %s", top->sym_evsel->idx, evsel__name(top->sym_evsel));
533*4882a593Smuzhiyun 
534*4882a593Smuzhiyun 				prompt_integer(&counter, "Enter details event counter");
535*4882a593Smuzhiyun 
536*4882a593Smuzhiyun 				if (counter >= top->evlist->core.nr_entries) {
537*4882a593Smuzhiyun 					top->sym_evsel = evlist__first(top->evlist);
538*4882a593Smuzhiyun 					fprintf(stderr, "Sorry, no such event, using %s.\n", evsel__name(top->sym_evsel));
539*4882a593Smuzhiyun 					sleep(1);
540*4882a593Smuzhiyun 					break;
541*4882a593Smuzhiyun 				}
542*4882a593Smuzhiyun 				evlist__for_each_entry(top->evlist, top->sym_evsel)
543*4882a593Smuzhiyun 					if (top->sym_evsel->idx == counter)
544*4882a593Smuzhiyun 						break;
545*4882a593Smuzhiyun 			} else
546*4882a593Smuzhiyun 				top->sym_evsel = evlist__first(top->evlist);
547*4882a593Smuzhiyun 			break;
548*4882a593Smuzhiyun 		case 'f':
549*4882a593Smuzhiyun 			prompt_integer(&top->count_filter, "Enter display event count filter");
550*4882a593Smuzhiyun 			break;
551*4882a593Smuzhiyun 		case 'F':
552*4882a593Smuzhiyun 			prompt_percent(&top->annotation_opts.min_pcnt,
553*4882a593Smuzhiyun 				       "Enter details display event filter (percent)");
554*4882a593Smuzhiyun 			break;
555*4882a593Smuzhiyun 		case 'K':
556*4882a593Smuzhiyun 			top->hide_kernel_symbols = !top->hide_kernel_symbols;
557*4882a593Smuzhiyun 			break;
558*4882a593Smuzhiyun 		case 'q':
559*4882a593Smuzhiyun 		case 'Q':
560*4882a593Smuzhiyun 			printf("exiting.\n");
561*4882a593Smuzhiyun 			if (top->dump_symtab)
562*4882a593Smuzhiyun 				perf_session__fprintf_dsos(top->session, stderr);
563*4882a593Smuzhiyun 			ret = false;
564*4882a593Smuzhiyun 			break;
565*4882a593Smuzhiyun 		case 's':
566*4882a593Smuzhiyun 			perf_top__prompt_symbol(top, "Enter details symbol");
567*4882a593Smuzhiyun 			break;
568*4882a593Smuzhiyun 		case 'S':
569*4882a593Smuzhiyun 			if (!top->sym_filter_entry)
570*4882a593Smuzhiyun 				break;
571*4882a593Smuzhiyun 			else {
572*4882a593Smuzhiyun 				struct hist_entry *syme = top->sym_filter_entry;
573*4882a593Smuzhiyun 
574*4882a593Smuzhiyun 				top->sym_filter_entry = NULL;
575*4882a593Smuzhiyun 				__zero_source_counters(syme);
576*4882a593Smuzhiyun 			}
577*4882a593Smuzhiyun 			break;
578*4882a593Smuzhiyun 		case 'U':
579*4882a593Smuzhiyun 			top->hide_user_symbols = !top->hide_user_symbols;
580*4882a593Smuzhiyun 			break;
581*4882a593Smuzhiyun 		case 'z':
582*4882a593Smuzhiyun 			top->zero = !top->zero;
583*4882a593Smuzhiyun 			break;
584*4882a593Smuzhiyun 		default:
585*4882a593Smuzhiyun 			break;
586*4882a593Smuzhiyun 	}
587*4882a593Smuzhiyun 
588*4882a593Smuzhiyun 	return ret;
589*4882a593Smuzhiyun }
590*4882a593Smuzhiyun 
perf_top__sort_new_samples(void * arg)591*4882a593Smuzhiyun static void perf_top__sort_new_samples(void *arg)
592*4882a593Smuzhiyun {
593*4882a593Smuzhiyun 	struct perf_top *t = arg;
594*4882a593Smuzhiyun 
595*4882a593Smuzhiyun 	if (t->evlist->selected != NULL)
596*4882a593Smuzhiyun 		t->sym_evsel = t->evlist->selected;
597*4882a593Smuzhiyun 
598*4882a593Smuzhiyun 	perf_top__resort_hists(t);
599*4882a593Smuzhiyun 
600*4882a593Smuzhiyun 	if (t->lost || t->drop)
601*4882a593Smuzhiyun 		pr_warning("Too slow to read ring buffer (change period (-c/-F) or limit CPUs (-C)\n");
602*4882a593Smuzhiyun }
603*4882a593Smuzhiyun 
stop_top(void)604*4882a593Smuzhiyun static void stop_top(void)
605*4882a593Smuzhiyun {
606*4882a593Smuzhiyun 	session_done = 1;
607*4882a593Smuzhiyun 	done = 1;
608*4882a593Smuzhiyun }
609*4882a593Smuzhiyun 
display_thread_tui(void * arg)610*4882a593Smuzhiyun static void *display_thread_tui(void *arg)
611*4882a593Smuzhiyun {
612*4882a593Smuzhiyun 	struct evsel *pos;
613*4882a593Smuzhiyun 	struct perf_top *top = arg;
614*4882a593Smuzhiyun 	const char *help = "For a higher level overview, try: perf top --sort comm,dso";
615*4882a593Smuzhiyun 	struct hist_browser_timer hbt = {
616*4882a593Smuzhiyun 		.timer		= perf_top__sort_new_samples,
617*4882a593Smuzhiyun 		.arg		= top,
618*4882a593Smuzhiyun 		.refresh	= top->delay_secs,
619*4882a593Smuzhiyun 	};
620*4882a593Smuzhiyun 	int ret;
621*4882a593Smuzhiyun 
622*4882a593Smuzhiyun 	/* In order to read symbols from other namespaces perf to  needs to call
623*4882a593Smuzhiyun 	 * setns(2).  This isn't permitted if the struct_fs has multiple users.
624*4882a593Smuzhiyun 	 * unshare(2) the fs so that we may continue to setns into namespaces
625*4882a593Smuzhiyun 	 * that we're observing.
626*4882a593Smuzhiyun 	 */
627*4882a593Smuzhiyun 	unshare(CLONE_FS);
628*4882a593Smuzhiyun 
629*4882a593Smuzhiyun 	prctl(PR_SET_NAME, "perf-top-UI", 0, 0, 0);
630*4882a593Smuzhiyun 
631*4882a593Smuzhiyun repeat:
632*4882a593Smuzhiyun 	perf_top__sort_new_samples(top);
633*4882a593Smuzhiyun 
634*4882a593Smuzhiyun 	/*
635*4882a593Smuzhiyun 	 * Initialize the uid_filter_str, in the future the TUI will allow
636*4882a593Smuzhiyun 	 * Zooming in/out UIDs. For now just use whatever the user passed
637*4882a593Smuzhiyun 	 * via --uid.
638*4882a593Smuzhiyun 	 */
639*4882a593Smuzhiyun 	evlist__for_each_entry(top->evlist, pos) {
640*4882a593Smuzhiyun 		struct hists *hists = evsel__hists(pos);
641*4882a593Smuzhiyun 		hists->uid_filter_str = top->record_opts.target.uid_str;
642*4882a593Smuzhiyun 	}
643*4882a593Smuzhiyun 
644*4882a593Smuzhiyun 	ret = perf_evlist__tui_browse_hists(top->evlist, help, &hbt,
645*4882a593Smuzhiyun 				      top->min_percent,
646*4882a593Smuzhiyun 				      &top->session->header.env,
647*4882a593Smuzhiyun 				      !top->record_opts.overwrite,
648*4882a593Smuzhiyun 				      &top->annotation_opts);
649*4882a593Smuzhiyun 
650*4882a593Smuzhiyun 	if (ret == K_RELOAD) {
651*4882a593Smuzhiyun 		top->zero = true;
652*4882a593Smuzhiyun 		goto repeat;
653*4882a593Smuzhiyun 	} else
654*4882a593Smuzhiyun 		stop_top();
655*4882a593Smuzhiyun 
656*4882a593Smuzhiyun 	return NULL;
657*4882a593Smuzhiyun }
658*4882a593Smuzhiyun 
display_sig(int sig __maybe_unused)659*4882a593Smuzhiyun static void display_sig(int sig __maybe_unused)
660*4882a593Smuzhiyun {
661*4882a593Smuzhiyun 	stop_top();
662*4882a593Smuzhiyun }
663*4882a593Smuzhiyun 
display_setup_sig(void)664*4882a593Smuzhiyun static void display_setup_sig(void)
665*4882a593Smuzhiyun {
666*4882a593Smuzhiyun 	signal(SIGSEGV, sighandler_dump_stack);
667*4882a593Smuzhiyun 	signal(SIGFPE, sighandler_dump_stack);
668*4882a593Smuzhiyun 	signal(SIGINT,  display_sig);
669*4882a593Smuzhiyun 	signal(SIGQUIT, display_sig);
670*4882a593Smuzhiyun 	signal(SIGTERM, display_sig);
671*4882a593Smuzhiyun }
672*4882a593Smuzhiyun 
display_thread(void * arg)673*4882a593Smuzhiyun static void *display_thread(void *arg)
674*4882a593Smuzhiyun {
675*4882a593Smuzhiyun 	struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
676*4882a593Smuzhiyun 	struct termios save;
677*4882a593Smuzhiyun 	struct perf_top *top = arg;
678*4882a593Smuzhiyun 	int delay_msecs, c;
679*4882a593Smuzhiyun 
680*4882a593Smuzhiyun 	/* In order to read symbols from other namespaces perf to  needs to call
681*4882a593Smuzhiyun 	 * setns(2).  This isn't permitted if the struct_fs has multiple users.
682*4882a593Smuzhiyun 	 * unshare(2) the fs so that we may continue to setns into namespaces
683*4882a593Smuzhiyun 	 * that we're observing.
684*4882a593Smuzhiyun 	 */
685*4882a593Smuzhiyun 	unshare(CLONE_FS);
686*4882a593Smuzhiyun 
687*4882a593Smuzhiyun 	prctl(PR_SET_NAME, "perf-top-UI", 0, 0, 0);
688*4882a593Smuzhiyun 
689*4882a593Smuzhiyun 	display_setup_sig();
690*4882a593Smuzhiyun 	pthread__unblock_sigwinch();
691*4882a593Smuzhiyun repeat:
692*4882a593Smuzhiyun 	delay_msecs = top->delay_secs * MSEC_PER_SEC;
693*4882a593Smuzhiyun 	set_term_quiet_input(&save);
694*4882a593Smuzhiyun 	/* trash return*/
695*4882a593Smuzhiyun 	clearerr(stdin);
696*4882a593Smuzhiyun 	if (poll(&stdin_poll, 1, 0) > 0)
697*4882a593Smuzhiyun 		getc(stdin);
698*4882a593Smuzhiyun 
699*4882a593Smuzhiyun 	while (!done) {
700*4882a593Smuzhiyun 		perf_top__print_sym_table(top);
701*4882a593Smuzhiyun 		/*
702*4882a593Smuzhiyun 		 * Either timeout expired or we got an EINTR due to SIGWINCH,
703*4882a593Smuzhiyun 		 * refresh screen in both cases.
704*4882a593Smuzhiyun 		 */
705*4882a593Smuzhiyun 		switch (poll(&stdin_poll, 1, delay_msecs)) {
706*4882a593Smuzhiyun 		case 0:
707*4882a593Smuzhiyun 			continue;
708*4882a593Smuzhiyun 		case -1:
709*4882a593Smuzhiyun 			if (errno == EINTR)
710*4882a593Smuzhiyun 				continue;
711*4882a593Smuzhiyun 			__fallthrough;
712*4882a593Smuzhiyun 		default:
713*4882a593Smuzhiyun 			c = getc(stdin);
714*4882a593Smuzhiyun 			tcsetattr(0, TCSAFLUSH, &save);
715*4882a593Smuzhiyun 
716*4882a593Smuzhiyun 			if (perf_top__handle_keypress(top, c))
717*4882a593Smuzhiyun 				goto repeat;
718*4882a593Smuzhiyun 			stop_top();
719*4882a593Smuzhiyun 		}
720*4882a593Smuzhiyun 	}
721*4882a593Smuzhiyun 
722*4882a593Smuzhiyun 	tcsetattr(0, TCSAFLUSH, &save);
723*4882a593Smuzhiyun 	return NULL;
724*4882a593Smuzhiyun }
725*4882a593Smuzhiyun 
hist_iter__top_callback(struct hist_entry_iter * iter,struct addr_location * al,bool single,void * arg)726*4882a593Smuzhiyun static int hist_iter__top_callback(struct hist_entry_iter *iter,
727*4882a593Smuzhiyun 				   struct addr_location *al, bool single,
728*4882a593Smuzhiyun 				   void *arg)
729*4882a593Smuzhiyun {
730*4882a593Smuzhiyun 	struct perf_top *top = arg;
731*4882a593Smuzhiyun 	struct hist_entry *he = iter->he;
732*4882a593Smuzhiyun 	struct evsel *evsel = iter->evsel;
733*4882a593Smuzhiyun 
734*4882a593Smuzhiyun 	if (perf_hpp_list.sym && single)
735*4882a593Smuzhiyun 		perf_top__record_precise_ip(top, he, iter->sample, evsel, al->addr);
736*4882a593Smuzhiyun 
737*4882a593Smuzhiyun 	hist__account_cycles(iter->sample->branch_stack, al, iter->sample,
738*4882a593Smuzhiyun 		     !(top->record_opts.branch_stack & PERF_SAMPLE_BRANCH_ANY),
739*4882a593Smuzhiyun 		     NULL);
740*4882a593Smuzhiyun 	return 0;
741*4882a593Smuzhiyun }
742*4882a593Smuzhiyun 
perf_event__process_sample(struct perf_tool * tool,const union perf_event * event,struct evsel * evsel,struct perf_sample * sample,struct machine * machine)743*4882a593Smuzhiyun static void perf_event__process_sample(struct perf_tool *tool,
744*4882a593Smuzhiyun 				       const union perf_event *event,
745*4882a593Smuzhiyun 				       struct evsel *evsel,
746*4882a593Smuzhiyun 				       struct perf_sample *sample,
747*4882a593Smuzhiyun 				       struct machine *machine)
748*4882a593Smuzhiyun {
749*4882a593Smuzhiyun 	struct perf_top *top = container_of(tool, struct perf_top, tool);
750*4882a593Smuzhiyun 	struct addr_location al;
751*4882a593Smuzhiyun 	int err;
752*4882a593Smuzhiyun 
753*4882a593Smuzhiyun 	if (!machine && perf_guest) {
754*4882a593Smuzhiyun 		static struct intlist *seen;
755*4882a593Smuzhiyun 
756*4882a593Smuzhiyun 		if (!seen)
757*4882a593Smuzhiyun 			seen = intlist__new(NULL);
758*4882a593Smuzhiyun 
759*4882a593Smuzhiyun 		if (!intlist__has_entry(seen, sample->pid)) {
760*4882a593Smuzhiyun 			pr_err("Can't find guest [%d]'s kernel information\n",
761*4882a593Smuzhiyun 				sample->pid);
762*4882a593Smuzhiyun 			intlist__add(seen, sample->pid);
763*4882a593Smuzhiyun 		}
764*4882a593Smuzhiyun 		return;
765*4882a593Smuzhiyun 	}
766*4882a593Smuzhiyun 
767*4882a593Smuzhiyun 	if (!machine) {
768*4882a593Smuzhiyun 		pr_err("%u unprocessable samples recorded.\r",
769*4882a593Smuzhiyun 		       top->session->evlist->stats.nr_unprocessable_samples++);
770*4882a593Smuzhiyun 		return;
771*4882a593Smuzhiyun 	}
772*4882a593Smuzhiyun 
773*4882a593Smuzhiyun 	if (event->header.misc & PERF_RECORD_MISC_EXACT_IP)
774*4882a593Smuzhiyun 		top->exact_samples++;
775*4882a593Smuzhiyun 
776*4882a593Smuzhiyun 	if (machine__resolve(machine, &al, sample) < 0)
777*4882a593Smuzhiyun 		return;
778*4882a593Smuzhiyun 
779*4882a593Smuzhiyun 	if (top->stitch_lbr)
780*4882a593Smuzhiyun 		al.thread->lbr_stitch_enable = true;
781*4882a593Smuzhiyun 
782*4882a593Smuzhiyun 	if (!machine->kptr_restrict_warned &&
783*4882a593Smuzhiyun 	    symbol_conf.kptr_restrict &&
784*4882a593Smuzhiyun 	    al.cpumode == PERF_RECORD_MISC_KERNEL) {
785*4882a593Smuzhiyun 		if (!perf_evlist__exclude_kernel(top->session->evlist)) {
786*4882a593Smuzhiyun 			ui__warning(
787*4882a593Smuzhiyun "Kernel address maps (/proc/{kallsyms,modules}) are restricted.\n\n"
788*4882a593Smuzhiyun "Check /proc/sys/kernel/kptr_restrict and /proc/sys/kernel/perf_event_paranoid.\n\n"
789*4882a593Smuzhiyun "Kernel%s samples will not be resolved.\n",
790*4882a593Smuzhiyun 			  al.map && map__has_symbols(al.map) ?
791*4882a593Smuzhiyun 			  " modules" : "");
792*4882a593Smuzhiyun 			if (use_browser <= 0)
793*4882a593Smuzhiyun 				sleep(5);
794*4882a593Smuzhiyun 		}
795*4882a593Smuzhiyun 		machine->kptr_restrict_warned = true;
796*4882a593Smuzhiyun 	}
797*4882a593Smuzhiyun 
798*4882a593Smuzhiyun 	if (al.sym == NULL && al.map != NULL) {
799*4882a593Smuzhiyun 		const char *msg = "Kernel samples will not be resolved.\n";
800*4882a593Smuzhiyun 		/*
801*4882a593Smuzhiyun 		 * As we do lazy loading of symtabs we only will know if the
802*4882a593Smuzhiyun 		 * specified vmlinux file is invalid when we actually have a
803*4882a593Smuzhiyun 		 * hit in kernel space and then try to load it. So if we get
804*4882a593Smuzhiyun 		 * here and there are _no_ symbols in the DSO backing the
805*4882a593Smuzhiyun 		 * kernel map, bail out.
806*4882a593Smuzhiyun 		 *
807*4882a593Smuzhiyun 		 * We may never get here, for instance, if we use -K/
808*4882a593Smuzhiyun 		 * --hide-kernel-symbols, even if the user specifies an
809*4882a593Smuzhiyun 		 * invalid --vmlinux ;-)
810*4882a593Smuzhiyun 		 */
811*4882a593Smuzhiyun 		if (!machine->kptr_restrict_warned && !top->vmlinux_warned &&
812*4882a593Smuzhiyun 		    __map__is_kernel(al.map) && map__has_symbols(al.map)) {
813*4882a593Smuzhiyun 			if (symbol_conf.vmlinux_name) {
814*4882a593Smuzhiyun 				char serr[256];
815*4882a593Smuzhiyun 				dso__strerror_load(al.map->dso, serr, sizeof(serr));
816*4882a593Smuzhiyun 				ui__warning("The %s file can't be used: %s\n%s",
817*4882a593Smuzhiyun 					    symbol_conf.vmlinux_name, serr, msg);
818*4882a593Smuzhiyun 			} else {
819*4882a593Smuzhiyun 				ui__warning("A vmlinux file was not found.\n%s",
820*4882a593Smuzhiyun 					    msg);
821*4882a593Smuzhiyun 			}
822*4882a593Smuzhiyun 
823*4882a593Smuzhiyun 			if (use_browser <= 0)
824*4882a593Smuzhiyun 				sleep(5);
825*4882a593Smuzhiyun 			top->vmlinux_warned = true;
826*4882a593Smuzhiyun 		}
827*4882a593Smuzhiyun 	}
828*4882a593Smuzhiyun 
829*4882a593Smuzhiyun 	if (al.sym == NULL || !al.sym->idle) {
830*4882a593Smuzhiyun 		struct hists *hists = evsel__hists(evsel);
831*4882a593Smuzhiyun 		struct hist_entry_iter iter = {
832*4882a593Smuzhiyun 			.evsel		= evsel,
833*4882a593Smuzhiyun 			.sample 	= sample,
834*4882a593Smuzhiyun 			.add_entry_cb 	= hist_iter__top_callback,
835*4882a593Smuzhiyun 		};
836*4882a593Smuzhiyun 
837*4882a593Smuzhiyun 		if (symbol_conf.cumulate_callchain)
838*4882a593Smuzhiyun 			iter.ops = &hist_iter_cumulative;
839*4882a593Smuzhiyun 		else
840*4882a593Smuzhiyun 			iter.ops = &hist_iter_normal;
841*4882a593Smuzhiyun 
842*4882a593Smuzhiyun 		pthread_mutex_lock(&hists->lock);
843*4882a593Smuzhiyun 
844*4882a593Smuzhiyun 		err = hist_entry_iter__add(&iter, &al, top->max_stack, top);
845*4882a593Smuzhiyun 		if (err < 0)
846*4882a593Smuzhiyun 			pr_err("Problem incrementing symbol period, skipping event\n");
847*4882a593Smuzhiyun 
848*4882a593Smuzhiyun 		pthread_mutex_unlock(&hists->lock);
849*4882a593Smuzhiyun 	}
850*4882a593Smuzhiyun 
851*4882a593Smuzhiyun 	addr_location__put(&al);
852*4882a593Smuzhiyun }
853*4882a593Smuzhiyun 
854*4882a593Smuzhiyun static void
perf_top__process_lost(struct perf_top * top,union perf_event * event,struct evsel * evsel)855*4882a593Smuzhiyun perf_top__process_lost(struct perf_top *top, union perf_event *event,
856*4882a593Smuzhiyun 		       struct evsel *evsel)
857*4882a593Smuzhiyun {
858*4882a593Smuzhiyun 	struct hists *hists = evsel__hists(evsel);
859*4882a593Smuzhiyun 
860*4882a593Smuzhiyun 	top->lost += event->lost.lost;
861*4882a593Smuzhiyun 	top->lost_total += event->lost.lost;
862*4882a593Smuzhiyun 	hists->stats.total_lost += event->lost.lost;
863*4882a593Smuzhiyun }
864*4882a593Smuzhiyun 
865*4882a593Smuzhiyun static void
perf_top__process_lost_samples(struct perf_top * top,union perf_event * event,struct evsel * evsel)866*4882a593Smuzhiyun perf_top__process_lost_samples(struct perf_top *top,
867*4882a593Smuzhiyun 			       union perf_event *event,
868*4882a593Smuzhiyun 			       struct evsel *evsel)
869*4882a593Smuzhiyun {
870*4882a593Smuzhiyun 	struct hists *hists = evsel__hists(evsel);
871*4882a593Smuzhiyun 
872*4882a593Smuzhiyun 	top->lost += event->lost_samples.lost;
873*4882a593Smuzhiyun 	top->lost_total += event->lost_samples.lost;
874*4882a593Smuzhiyun 	hists->stats.total_lost_samples += event->lost_samples.lost;
875*4882a593Smuzhiyun }
876*4882a593Smuzhiyun 
877*4882a593Smuzhiyun static u64 last_timestamp;
878*4882a593Smuzhiyun 
perf_top__mmap_read_idx(struct perf_top * top,int idx)879*4882a593Smuzhiyun static void perf_top__mmap_read_idx(struct perf_top *top, int idx)
880*4882a593Smuzhiyun {
881*4882a593Smuzhiyun 	struct record_opts *opts = &top->record_opts;
882*4882a593Smuzhiyun 	struct evlist *evlist = top->evlist;
883*4882a593Smuzhiyun 	struct mmap *md;
884*4882a593Smuzhiyun 	union perf_event *event;
885*4882a593Smuzhiyun 
886*4882a593Smuzhiyun 	md = opts->overwrite ? &evlist->overwrite_mmap[idx] : &evlist->mmap[idx];
887*4882a593Smuzhiyun 	if (perf_mmap__read_init(&md->core) < 0)
888*4882a593Smuzhiyun 		return;
889*4882a593Smuzhiyun 
890*4882a593Smuzhiyun 	while ((event = perf_mmap__read_event(&md->core)) != NULL) {
891*4882a593Smuzhiyun 		int ret;
892*4882a593Smuzhiyun 
893*4882a593Smuzhiyun 		ret = perf_evlist__parse_sample_timestamp(evlist, event, &last_timestamp);
894*4882a593Smuzhiyun 		if (ret && ret != -1)
895*4882a593Smuzhiyun 			break;
896*4882a593Smuzhiyun 
897*4882a593Smuzhiyun 		ret = ordered_events__queue(top->qe.in, event, last_timestamp, 0);
898*4882a593Smuzhiyun 		if (ret)
899*4882a593Smuzhiyun 			break;
900*4882a593Smuzhiyun 
901*4882a593Smuzhiyun 		perf_mmap__consume(&md->core);
902*4882a593Smuzhiyun 
903*4882a593Smuzhiyun 		if (top->qe.rotate) {
904*4882a593Smuzhiyun 			pthread_mutex_lock(&top->qe.mutex);
905*4882a593Smuzhiyun 			top->qe.rotate = false;
906*4882a593Smuzhiyun 			pthread_cond_signal(&top->qe.cond);
907*4882a593Smuzhiyun 			pthread_mutex_unlock(&top->qe.mutex);
908*4882a593Smuzhiyun 		}
909*4882a593Smuzhiyun 	}
910*4882a593Smuzhiyun 
911*4882a593Smuzhiyun 	perf_mmap__read_done(&md->core);
912*4882a593Smuzhiyun }
913*4882a593Smuzhiyun 
perf_top__mmap_read(struct perf_top * top)914*4882a593Smuzhiyun static void perf_top__mmap_read(struct perf_top *top)
915*4882a593Smuzhiyun {
916*4882a593Smuzhiyun 	bool overwrite = top->record_opts.overwrite;
917*4882a593Smuzhiyun 	struct evlist *evlist = top->evlist;
918*4882a593Smuzhiyun 	int i;
919*4882a593Smuzhiyun 
920*4882a593Smuzhiyun 	if (overwrite)
921*4882a593Smuzhiyun 		perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_DATA_PENDING);
922*4882a593Smuzhiyun 
923*4882a593Smuzhiyun 	for (i = 0; i < top->evlist->core.nr_mmaps; i++)
924*4882a593Smuzhiyun 		perf_top__mmap_read_idx(top, i);
925*4882a593Smuzhiyun 
926*4882a593Smuzhiyun 	if (overwrite) {
927*4882a593Smuzhiyun 		perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_EMPTY);
928*4882a593Smuzhiyun 		perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING);
929*4882a593Smuzhiyun 	}
930*4882a593Smuzhiyun }
931*4882a593Smuzhiyun 
932*4882a593Smuzhiyun /*
933*4882a593Smuzhiyun  * Check per-event overwrite term.
934*4882a593Smuzhiyun  * perf top should support consistent term for all events.
935*4882a593Smuzhiyun  * - All events don't have per-event term
936*4882a593Smuzhiyun  *   E.g. "cpu/cpu-cycles/,cpu/instructions/"
937*4882a593Smuzhiyun  *   Nothing change, return 0.
938*4882a593Smuzhiyun  * - All events have same per-event term
939*4882a593Smuzhiyun  *   E.g. "cpu/cpu-cycles,no-overwrite/,cpu/instructions,no-overwrite/
940*4882a593Smuzhiyun  *   Using the per-event setting to replace the opts->overwrite if
941*4882a593Smuzhiyun  *   they are different, then return 0.
942*4882a593Smuzhiyun  * - Events have different per-event term
943*4882a593Smuzhiyun  *   E.g. "cpu/cpu-cycles,overwrite/,cpu/instructions,no-overwrite/"
944*4882a593Smuzhiyun  *   Return -1
945*4882a593Smuzhiyun  * - Some of the event set per-event term, but some not.
946*4882a593Smuzhiyun  *   E.g. "cpu/cpu-cycles/,cpu/instructions,no-overwrite/"
947*4882a593Smuzhiyun  *   Return -1
948*4882a593Smuzhiyun  */
perf_top__overwrite_check(struct perf_top * top)949*4882a593Smuzhiyun static int perf_top__overwrite_check(struct perf_top *top)
950*4882a593Smuzhiyun {
951*4882a593Smuzhiyun 	struct record_opts *opts = &top->record_opts;
952*4882a593Smuzhiyun 	struct evlist *evlist = top->evlist;
953*4882a593Smuzhiyun 	struct evsel_config_term *term;
954*4882a593Smuzhiyun 	struct list_head *config_terms;
955*4882a593Smuzhiyun 	struct evsel *evsel;
956*4882a593Smuzhiyun 	int set, overwrite = -1;
957*4882a593Smuzhiyun 
958*4882a593Smuzhiyun 	evlist__for_each_entry(evlist, evsel) {
959*4882a593Smuzhiyun 		set = -1;
960*4882a593Smuzhiyun 		config_terms = &evsel->config_terms;
961*4882a593Smuzhiyun 		list_for_each_entry(term, config_terms, list) {
962*4882a593Smuzhiyun 			if (term->type == EVSEL__CONFIG_TERM_OVERWRITE)
963*4882a593Smuzhiyun 				set = term->val.overwrite ? 1 : 0;
964*4882a593Smuzhiyun 		}
965*4882a593Smuzhiyun 
966*4882a593Smuzhiyun 		/* no term for current and previous event (likely) */
967*4882a593Smuzhiyun 		if ((overwrite < 0) && (set < 0))
968*4882a593Smuzhiyun 			continue;
969*4882a593Smuzhiyun 
970*4882a593Smuzhiyun 		/* has term for both current and previous event, compare */
971*4882a593Smuzhiyun 		if ((overwrite >= 0) && (set >= 0) && (overwrite != set))
972*4882a593Smuzhiyun 			return -1;
973*4882a593Smuzhiyun 
974*4882a593Smuzhiyun 		/* no term for current event but has term for previous one */
975*4882a593Smuzhiyun 		if ((overwrite >= 0) && (set < 0))
976*4882a593Smuzhiyun 			return -1;
977*4882a593Smuzhiyun 
978*4882a593Smuzhiyun 		/* has term for current event */
979*4882a593Smuzhiyun 		if ((overwrite < 0) && (set >= 0)) {
980*4882a593Smuzhiyun 			/* if it's first event, set overwrite */
981*4882a593Smuzhiyun 			if (evsel == evlist__first(evlist))
982*4882a593Smuzhiyun 				overwrite = set;
983*4882a593Smuzhiyun 			else
984*4882a593Smuzhiyun 				return -1;
985*4882a593Smuzhiyun 		}
986*4882a593Smuzhiyun 	}
987*4882a593Smuzhiyun 
988*4882a593Smuzhiyun 	if ((overwrite >= 0) && (opts->overwrite != overwrite))
989*4882a593Smuzhiyun 		opts->overwrite = overwrite;
990*4882a593Smuzhiyun 
991*4882a593Smuzhiyun 	return 0;
992*4882a593Smuzhiyun }
993*4882a593Smuzhiyun 
perf_top_overwrite_fallback(struct perf_top * top,struct evsel * evsel)994*4882a593Smuzhiyun static int perf_top_overwrite_fallback(struct perf_top *top,
995*4882a593Smuzhiyun 				       struct evsel *evsel)
996*4882a593Smuzhiyun {
997*4882a593Smuzhiyun 	struct record_opts *opts = &top->record_opts;
998*4882a593Smuzhiyun 	struct evlist *evlist = top->evlist;
999*4882a593Smuzhiyun 	struct evsel *counter;
1000*4882a593Smuzhiyun 
1001*4882a593Smuzhiyun 	if (!opts->overwrite)
1002*4882a593Smuzhiyun 		return 0;
1003*4882a593Smuzhiyun 
1004*4882a593Smuzhiyun 	/* only fall back when first event fails */
1005*4882a593Smuzhiyun 	if (evsel != evlist__first(evlist))
1006*4882a593Smuzhiyun 		return 0;
1007*4882a593Smuzhiyun 
1008*4882a593Smuzhiyun 	evlist__for_each_entry(evlist, counter)
1009*4882a593Smuzhiyun 		counter->core.attr.write_backward = false;
1010*4882a593Smuzhiyun 	opts->overwrite = false;
1011*4882a593Smuzhiyun 	pr_debug2("fall back to non-overwrite mode\n");
1012*4882a593Smuzhiyun 	return 1;
1013*4882a593Smuzhiyun }
1014*4882a593Smuzhiyun 
perf_top__start_counters(struct perf_top * top)1015*4882a593Smuzhiyun static int perf_top__start_counters(struct perf_top *top)
1016*4882a593Smuzhiyun {
1017*4882a593Smuzhiyun 	char msg[BUFSIZ];
1018*4882a593Smuzhiyun 	struct evsel *counter;
1019*4882a593Smuzhiyun 	struct evlist *evlist = top->evlist;
1020*4882a593Smuzhiyun 	struct record_opts *opts = &top->record_opts;
1021*4882a593Smuzhiyun 
1022*4882a593Smuzhiyun 	if (perf_top__overwrite_check(top)) {
1023*4882a593Smuzhiyun 		ui__error("perf top only support consistent per-event "
1024*4882a593Smuzhiyun 			  "overwrite setting for all events\n");
1025*4882a593Smuzhiyun 		goto out_err;
1026*4882a593Smuzhiyun 	}
1027*4882a593Smuzhiyun 
1028*4882a593Smuzhiyun 	perf_evlist__config(evlist, opts, &callchain_param);
1029*4882a593Smuzhiyun 
1030*4882a593Smuzhiyun 	evlist__for_each_entry(evlist, counter) {
1031*4882a593Smuzhiyun try_again:
1032*4882a593Smuzhiyun 		if (evsel__open(counter, top->evlist->core.cpus,
1033*4882a593Smuzhiyun 				     top->evlist->core.threads) < 0) {
1034*4882a593Smuzhiyun 
1035*4882a593Smuzhiyun 			/*
1036*4882a593Smuzhiyun 			 * Specially handle overwrite fall back.
1037*4882a593Smuzhiyun 			 * Because perf top is the only tool which has
1038*4882a593Smuzhiyun 			 * overwrite mode by default, support
1039*4882a593Smuzhiyun 			 * both overwrite and non-overwrite mode, and
1040*4882a593Smuzhiyun 			 * require consistent mode for all events.
1041*4882a593Smuzhiyun 			 *
1042*4882a593Smuzhiyun 			 * May move it to generic code with more tools
1043*4882a593Smuzhiyun 			 * have similar attribute.
1044*4882a593Smuzhiyun 			 */
1045*4882a593Smuzhiyun 			if (perf_missing_features.write_backward &&
1046*4882a593Smuzhiyun 			    perf_top_overwrite_fallback(top, counter))
1047*4882a593Smuzhiyun 				goto try_again;
1048*4882a593Smuzhiyun 
1049*4882a593Smuzhiyun 			if (evsel__fallback(counter, errno, msg, sizeof(msg))) {
1050*4882a593Smuzhiyun 				if (verbose > 0)
1051*4882a593Smuzhiyun 					ui__warning("%s\n", msg);
1052*4882a593Smuzhiyun 				goto try_again;
1053*4882a593Smuzhiyun 			}
1054*4882a593Smuzhiyun 
1055*4882a593Smuzhiyun 			evsel__open_strerror(counter, &opts->target, errno, msg, sizeof(msg));
1056*4882a593Smuzhiyun 			ui__error("%s\n", msg);
1057*4882a593Smuzhiyun 			goto out_err;
1058*4882a593Smuzhiyun 		}
1059*4882a593Smuzhiyun 	}
1060*4882a593Smuzhiyun 
1061*4882a593Smuzhiyun 	if (evlist__mmap(evlist, opts->mmap_pages) < 0) {
1062*4882a593Smuzhiyun 		ui__error("Failed to mmap with %d (%s)\n",
1063*4882a593Smuzhiyun 			    errno, str_error_r(errno, msg, sizeof(msg)));
1064*4882a593Smuzhiyun 		goto out_err;
1065*4882a593Smuzhiyun 	}
1066*4882a593Smuzhiyun 
1067*4882a593Smuzhiyun 	return 0;
1068*4882a593Smuzhiyun 
1069*4882a593Smuzhiyun out_err:
1070*4882a593Smuzhiyun 	return -1;
1071*4882a593Smuzhiyun }
1072*4882a593Smuzhiyun 
callchain_param__setup_sample_type(struct callchain_param * callchain)1073*4882a593Smuzhiyun static int callchain_param__setup_sample_type(struct callchain_param *callchain)
1074*4882a593Smuzhiyun {
1075*4882a593Smuzhiyun 	if (callchain->mode != CHAIN_NONE) {
1076*4882a593Smuzhiyun 		if (callchain_register_param(callchain) < 0) {
1077*4882a593Smuzhiyun 			ui__error("Can't register callchain params.\n");
1078*4882a593Smuzhiyun 			return -EINVAL;
1079*4882a593Smuzhiyun 		}
1080*4882a593Smuzhiyun 	}
1081*4882a593Smuzhiyun 
1082*4882a593Smuzhiyun 	return 0;
1083*4882a593Smuzhiyun }
1084*4882a593Smuzhiyun 
rotate_queues(struct perf_top * top)1085*4882a593Smuzhiyun static struct ordered_events *rotate_queues(struct perf_top *top)
1086*4882a593Smuzhiyun {
1087*4882a593Smuzhiyun 	struct ordered_events *in = top->qe.in;
1088*4882a593Smuzhiyun 
1089*4882a593Smuzhiyun 	if (top->qe.in == &top->qe.data[1])
1090*4882a593Smuzhiyun 		top->qe.in = &top->qe.data[0];
1091*4882a593Smuzhiyun 	else
1092*4882a593Smuzhiyun 		top->qe.in = &top->qe.data[1];
1093*4882a593Smuzhiyun 
1094*4882a593Smuzhiyun 	return in;
1095*4882a593Smuzhiyun }
1096*4882a593Smuzhiyun 
process_thread(void * arg)1097*4882a593Smuzhiyun static void *process_thread(void *arg)
1098*4882a593Smuzhiyun {
1099*4882a593Smuzhiyun 	struct perf_top *top = arg;
1100*4882a593Smuzhiyun 
1101*4882a593Smuzhiyun 	while (!done) {
1102*4882a593Smuzhiyun 		struct ordered_events *out, *in = top->qe.in;
1103*4882a593Smuzhiyun 
1104*4882a593Smuzhiyun 		if (!in->nr_events) {
1105*4882a593Smuzhiyun 			usleep(100);
1106*4882a593Smuzhiyun 			continue;
1107*4882a593Smuzhiyun 		}
1108*4882a593Smuzhiyun 
1109*4882a593Smuzhiyun 		out = rotate_queues(top);
1110*4882a593Smuzhiyun 
1111*4882a593Smuzhiyun 		pthread_mutex_lock(&top->qe.mutex);
1112*4882a593Smuzhiyun 		top->qe.rotate = true;
1113*4882a593Smuzhiyun 		pthread_cond_wait(&top->qe.cond, &top->qe.mutex);
1114*4882a593Smuzhiyun 		pthread_mutex_unlock(&top->qe.mutex);
1115*4882a593Smuzhiyun 
1116*4882a593Smuzhiyun 		if (ordered_events__flush(out, OE_FLUSH__TOP))
1117*4882a593Smuzhiyun 			pr_err("failed to process events\n");
1118*4882a593Smuzhiyun 	}
1119*4882a593Smuzhiyun 
1120*4882a593Smuzhiyun 	return NULL;
1121*4882a593Smuzhiyun }
1122*4882a593Smuzhiyun 
1123*4882a593Smuzhiyun /*
1124*4882a593Smuzhiyun  * Allow only 'top->delay_secs' seconds behind samples.
1125*4882a593Smuzhiyun  */
should_drop(struct ordered_event * qevent,struct perf_top * top)1126*4882a593Smuzhiyun static int should_drop(struct ordered_event *qevent, struct perf_top *top)
1127*4882a593Smuzhiyun {
1128*4882a593Smuzhiyun 	union perf_event *event = qevent->event;
1129*4882a593Smuzhiyun 	u64 delay_timestamp;
1130*4882a593Smuzhiyun 
1131*4882a593Smuzhiyun 	if (event->header.type != PERF_RECORD_SAMPLE)
1132*4882a593Smuzhiyun 		return false;
1133*4882a593Smuzhiyun 
1134*4882a593Smuzhiyun 	delay_timestamp = qevent->timestamp + top->delay_secs * NSEC_PER_SEC;
1135*4882a593Smuzhiyun 	return delay_timestamp < last_timestamp;
1136*4882a593Smuzhiyun }
1137*4882a593Smuzhiyun 
deliver_event(struct ordered_events * qe,struct ordered_event * qevent)1138*4882a593Smuzhiyun static int deliver_event(struct ordered_events *qe,
1139*4882a593Smuzhiyun 			 struct ordered_event *qevent)
1140*4882a593Smuzhiyun {
1141*4882a593Smuzhiyun 	struct perf_top *top = qe->data;
1142*4882a593Smuzhiyun 	struct evlist *evlist = top->evlist;
1143*4882a593Smuzhiyun 	struct perf_session *session = top->session;
1144*4882a593Smuzhiyun 	union perf_event *event = qevent->event;
1145*4882a593Smuzhiyun 	struct perf_sample sample;
1146*4882a593Smuzhiyun 	struct evsel *evsel;
1147*4882a593Smuzhiyun 	struct machine *machine;
1148*4882a593Smuzhiyun 	int ret = -1;
1149*4882a593Smuzhiyun 
1150*4882a593Smuzhiyun 	if (should_drop(qevent, top)) {
1151*4882a593Smuzhiyun 		top->drop++;
1152*4882a593Smuzhiyun 		top->drop_total++;
1153*4882a593Smuzhiyun 		return 0;
1154*4882a593Smuzhiyun 	}
1155*4882a593Smuzhiyun 
1156*4882a593Smuzhiyun 	ret = perf_evlist__parse_sample(evlist, event, &sample);
1157*4882a593Smuzhiyun 	if (ret) {
1158*4882a593Smuzhiyun 		pr_err("Can't parse sample, err = %d\n", ret);
1159*4882a593Smuzhiyun 		goto next_event;
1160*4882a593Smuzhiyun 	}
1161*4882a593Smuzhiyun 
1162*4882a593Smuzhiyun 	evsel = perf_evlist__id2evsel(session->evlist, sample.id);
1163*4882a593Smuzhiyun 	assert(evsel != NULL);
1164*4882a593Smuzhiyun 
1165*4882a593Smuzhiyun 	if (event->header.type == PERF_RECORD_SAMPLE) {
1166*4882a593Smuzhiyun 		if (evswitch__discard(&top->evswitch, evsel))
1167*4882a593Smuzhiyun 			return 0;
1168*4882a593Smuzhiyun 		++top->samples;
1169*4882a593Smuzhiyun 	}
1170*4882a593Smuzhiyun 
1171*4882a593Smuzhiyun 	switch (sample.cpumode) {
1172*4882a593Smuzhiyun 	case PERF_RECORD_MISC_USER:
1173*4882a593Smuzhiyun 		++top->us_samples;
1174*4882a593Smuzhiyun 		if (top->hide_user_symbols)
1175*4882a593Smuzhiyun 			goto next_event;
1176*4882a593Smuzhiyun 		machine = &session->machines.host;
1177*4882a593Smuzhiyun 		break;
1178*4882a593Smuzhiyun 	case PERF_RECORD_MISC_KERNEL:
1179*4882a593Smuzhiyun 		++top->kernel_samples;
1180*4882a593Smuzhiyun 		if (top->hide_kernel_symbols)
1181*4882a593Smuzhiyun 			goto next_event;
1182*4882a593Smuzhiyun 		machine = &session->machines.host;
1183*4882a593Smuzhiyun 		break;
1184*4882a593Smuzhiyun 	case PERF_RECORD_MISC_GUEST_KERNEL:
1185*4882a593Smuzhiyun 		++top->guest_kernel_samples;
1186*4882a593Smuzhiyun 		machine = perf_session__find_machine(session,
1187*4882a593Smuzhiyun 						     sample.pid);
1188*4882a593Smuzhiyun 		break;
1189*4882a593Smuzhiyun 	case PERF_RECORD_MISC_GUEST_USER:
1190*4882a593Smuzhiyun 		++top->guest_us_samples;
1191*4882a593Smuzhiyun 		/*
1192*4882a593Smuzhiyun 		 * TODO: we don't process guest user from host side
1193*4882a593Smuzhiyun 		 * except simple counting.
1194*4882a593Smuzhiyun 		 */
1195*4882a593Smuzhiyun 		goto next_event;
1196*4882a593Smuzhiyun 	default:
1197*4882a593Smuzhiyun 		if (event->header.type == PERF_RECORD_SAMPLE)
1198*4882a593Smuzhiyun 			goto next_event;
1199*4882a593Smuzhiyun 		machine = &session->machines.host;
1200*4882a593Smuzhiyun 		break;
1201*4882a593Smuzhiyun 	}
1202*4882a593Smuzhiyun 
1203*4882a593Smuzhiyun 	if (event->header.type == PERF_RECORD_SAMPLE) {
1204*4882a593Smuzhiyun 		perf_event__process_sample(&top->tool, event, evsel,
1205*4882a593Smuzhiyun 					   &sample, machine);
1206*4882a593Smuzhiyun 	} else if (event->header.type == PERF_RECORD_LOST) {
1207*4882a593Smuzhiyun 		perf_top__process_lost(top, event, evsel);
1208*4882a593Smuzhiyun 	} else if (event->header.type == PERF_RECORD_LOST_SAMPLES) {
1209*4882a593Smuzhiyun 		perf_top__process_lost_samples(top, event, evsel);
1210*4882a593Smuzhiyun 	} else if (event->header.type < PERF_RECORD_MAX) {
1211*4882a593Smuzhiyun 		hists__inc_nr_events(evsel__hists(evsel), event->header.type);
1212*4882a593Smuzhiyun 		machine__process_event(machine, event, &sample);
1213*4882a593Smuzhiyun 	} else
1214*4882a593Smuzhiyun 		++session->evlist->stats.nr_unknown_events;
1215*4882a593Smuzhiyun 
1216*4882a593Smuzhiyun 	ret = 0;
1217*4882a593Smuzhiyun next_event:
1218*4882a593Smuzhiyun 	return ret;
1219*4882a593Smuzhiyun }
1220*4882a593Smuzhiyun 
init_process_thread(struct perf_top * top)1221*4882a593Smuzhiyun static void init_process_thread(struct perf_top *top)
1222*4882a593Smuzhiyun {
1223*4882a593Smuzhiyun 	ordered_events__init(&top->qe.data[0], deliver_event, top);
1224*4882a593Smuzhiyun 	ordered_events__init(&top->qe.data[1], deliver_event, top);
1225*4882a593Smuzhiyun 	ordered_events__set_copy_on_queue(&top->qe.data[0], true);
1226*4882a593Smuzhiyun 	ordered_events__set_copy_on_queue(&top->qe.data[1], true);
1227*4882a593Smuzhiyun 	top->qe.in = &top->qe.data[0];
1228*4882a593Smuzhiyun 	pthread_mutex_init(&top->qe.mutex, NULL);
1229*4882a593Smuzhiyun 	pthread_cond_init(&top->qe.cond, NULL);
1230*4882a593Smuzhiyun }
1231*4882a593Smuzhiyun 
__cmd_top(struct perf_top * top)1232*4882a593Smuzhiyun static int __cmd_top(struct perf_top *top)
1233*4882a593Smuzhiyun {
1234*4882a593Smuzhiyun 	struct record_opts *opts = &top->record_opts;
1235*4882a593Smuzhiyun 	pthread_t thread, thread_process;
1236*4882a593Smuzhiyun 	int ret;
1237*4882a593Smuzhiyun 
1238*4882a593Smuzhiyun 	if (!top->annotation_opts.objdump_path) {
1239*4882a593Smuzhiyun 		ret = perf_env__lookup_objdump(&top->session->header.env,
1240*4882a593Smuzhiyun 					       &top->annotation_opts.objdump_path);
1241*4882a593Smuzhiyun 		if (ret)
1242*4882a593Smuzhiyun 			return ret;
1243*4882a593Smuzhiyun 	}
1244*4882a593Smuzhiyun 
1245*4882a593Smuzhiyun 	ret = callchain_param__setup_sample_type(&callchain_param);
1246*4882a593Smuzhiyun 	if (ret)
1247*4882a593Smuzhiyun 		return ret;
1248*4882a593Smuzhiyun 
1249*4882a593Smuzhiyun 	if (perf_session__register_idle_thread(top->session) < 0)
1250*4882a593Smuzhiyun 		return ret;
1251*4882a593Smuzhiyun 
1252*4882a593Smuzhiyun 	if (top->nr_threads_synthesize > 1)
1253*4882a593Smuzhiyun 		perf_set_multithreaded();
1254*4882a593Smuzhiyun 
1255*4882a593Smuzhiyun 	init_process_thread(top);
1256*4882a593Smuzhiyun 
1257*4882a593Smuzhiyun 	if (opts->record_namespaces)
1258*4882a593Smuzhiyun 		top->tool.namespace_events = true;
1259*4882a593Smuzhiyun 	if (opts->record_cgroup) {
1260*4882a593Smuzhiyun #ifdef HAVE_FILE_HANDLE
1261*4882a593Smuzhiyun 		top->tool.cgroup_events = true;
1262*4882a593Smuzhiyun #else
1263*4882a593Smuzhiyun 		pr_err("cgroup tracking is not supported.\n");
1264*4882a593Smuzhiyun 		return -1;
1265*4882a593Smuzhiyun #endif
1266*4882a593Smuzhiyun 	}
1267*4882a593Smuzhiyun 
1268*4882a593Smuzhiyun 	ret = perf_event__synthesize_bpf_events(top->session, perf_event__process,
1269*4882a593Smuzhiyun 						&top->session->machines.host,
1270*4882a593Smuzhiyun 						&top->record_opts);
1271*4882a593Smuzhiyun 	if (ret < 0)
1272*4882a593Smuzhiyun 		pr_debug("Couldn't synthesize BPF events: Pre-existing BPF programs won't have symbols resolved.\n");
1273*4882a593Smuzhiyun 
1274*4882a593Smuzhiyun 	ret = perf_event__synthesize_cgroups(&top->tool, perf_event__process,
1275*4882a593Smuzhiyun 					     &top->session->machines.host);
1276*4882a593Smuzhiyun 	if (ret < 0)
1277*4882a593Smuzhiyun 		pr_debug("Couldn't synthesize cgroup events.\n");
1278*4882a593Smuzhiyun 
1279*4882a593Smuzhiyun 	machine__synthesize_threads(&top->session->machines.host, &opts->target,
1280*4882a593Smuzhiyun 				    top->evlist->core.threads, false,
1281*4882a593Smuzhiyun 				    top->nr_threads_synthesize);
1282*4882a593Smuzhiyun 
1283*4882a593Smuzhiyun 	if (top->nr_threads_synthesize > 1)
1284*4882a593Smuzhiyun 		perf_set_singlethreaded();
1285*4882a593Smuzhiyun 
1286*4882a593Smuzhiyun 	if (perf_hpp_list.socket) {
1287*4882a593Smuzhiyun 		ret = perf_env__read_cpu_topology_map(&perf_env);
1288*4882a593Smuzhiyun 		if (ret < 0) {
1289*4882a593Smuzhiyun 			char errbuf[BUFSIZ];
1290*4882a593Smuzhiyun 			const char *err = str_error_r(-ret, errbuf, sizeof(errbuf));
1291*4882a593Smuzhiyun 
1292*4882a593Smuzhiyun 			ui__error("Could not read the CPU topology map: %s\n", err);
1293*4882a593Smuzhiyun 			return ret;
1294*4882a593Smuzhiyun 		}
1295*4882a593Smuzhiyun 	}
1296*4882a593Smuzhiyun 
1297*4882a593Smuzhiyun 	ret = perf_top__start_counters(top);
1298*4882a593Smuzhiyun 	if (ret)
1299*4882a593Smuzhiyun 		return ret;
1300*4882a593Smuzhiyun 
1301*4882a593Smuzhiyun 	top->session->evlist = top->evlist;
1302*4882a593Smuzhiyun 	perf_session__set_id_hdr_size(top->session);
1303*4882a593Smuzhiyun 
1304*4882a593Smuzhiyun 	/*
1305*4882a593Smuzhiyun 	 * When perf is starting the traced process, all the events (apart from
1306*4882a593Smuzhiyun 	 * group members) have enable_on_exec=1 set, so don't spoil it by
1307*4882a593Smuzhiyun 	 * prematurely enabling them.
1308*4882a593Smuzhiyun 	 *
1309*4882a593Smuzhiyun 	 * XXX 'top' still doesn't start workloads like record, trace, but should,
1310*4882a593Smuzhiyun 	 * so leave the check here.
1311*4882a593Smuzhiyun 	 */
1312*4882a593Smuzhiyun         if (!target__none(&opts->target))
1313*4882a593Smuzhiyun 		evlist__enable(top->evlist);
1314*4882a593Smuzhiyun 
1315*4882a593Smuzhiyun 	ret = -1;
1316*4882a593Smuzhiyun 	if (pthread_create(&thread_process, NULL, process_thread, top)) {
1317*4882a593Smuzhiyun 		ui__error("Could not create process thread.\n");
1318*4882a593Smuzhiyun 		return ret;
1319*4882a593Smuzhiyun 	}
1320*4882a593Smuzhiyun 
1321*4882a593Smuzhiyun 	if (pthread_create(&thread, NULL, (use_browser > 0 ? display_thread_tui :
1322*4882a593Smuzhiyun 							    display_thread), top)) {
1323*4882a593Smuzhiyun 		ui__error("Could not create display thread.\n");
1324*4882a593Smuzhiyun 		goto out_join_thread;
1325*4882a593Smuzhiyun 	}
1326*4882a593Smuzhiyun 
1327*4882a593Smuzhiyun 	if (top->realtime_prio) {
1328*4882a593Smuzhiyun 		struct sched_param param;
1329*4882a593Smuzhiyun 
1330*4882a593Smuzhiyun 		param.sched_priority = top->realtime_prio;
1331*4882a593Smuzhiyun 		if (sched_setscheduler(0, SCHED_FIFO, &param)) {
1332*4882a593Smuzhiyun 			ui__error("Could not set realtime priority.\n");
1333*4882a593Smuzhiyun 			goto out_join;
1334*4882a593Smuzhiyun 		}
1335*4882a593Smuzhiyun 	}
1336*4882a593Smuzhiyun 
1337*4882a593Smuzhiyun 	/* Wait for a minimal set of events before starting the snapshot */
1338*4882a593Smuzhiyun 	evlist__poll(top->evlist, 100);
1339*4882a593Smuzhiyun 
1340*4882a593Smuzhiyun 	perf_top__mmap_read(top);
1341*4882a593Smuzhiyun 
1342*4882a593Smuzhiyun 	while (!done) {
1343*4882a593Smuzhiyun 		u64 hits = top->samples;
1344*4882a593Smuzhiyun 
1345*4882a593Smuzhiyun 		perf_top__mmap_read(top);
1346*4882a593Smuzhiyun 
1347*4882a593Smuzhiyun 		if (opts->overwrite || (hits == top->samples))
1348*4882a593Smuzhiyun 			ret = evlist__poll(top->evlist, 100);
1349*4882a593Smuzhiyun 
1350*4882a593Smuzhiyun 		if (resize) {
1351*4882a593Smuzhiyun 			perf_top__resize(top);
1352*4882a593Smuzhiyun 			resize = 0;
1353*4882a593Smuzhiyun 		}
1354*4882a593Smuzhiyun 	}
1355*4882a593Smuzhiyun 
1356*4882a593Smuzhiyun 	ret = 0;
1357*4882a593Smuzhiyun out_join:
1358*4882a593Smuzhiyun 	pthread_join(thread, NULL);
1359*4882a593Smuzhiyun out_join_thread:
1360*4882a593Smuzhiyun 	pthread_cond_signal(&top->qe.cond);
1361*4882a593Smuzhiyun 	pthread_join(thread_process, NULL);
1362*4882a593Smuzhiyun 	return ret;
1363*4882a593Smuzhiyun }
1364*4882a593Smuzhiyun 
1365*4882a593Smuzhiyun static int
callchain_opt(const struct option * opt,const char * arg,int unset)1366*4882a593Smuzhiyun callchain_opt(const struct option *opt, const char *arg, int unset)
1367*4882a593Smuzhiyun {
1368*4882a593Smuzhiyun 	symbol_conf.use_callchain = true;
1369*4882a593Smuzhiyun 	return record_callchain_opt(opt, arg, unset);
1370*4882a593Smuzhiyun }
1371*4882a593Smuzhiyun 
1372*4882a593Smuzhiyun static int
parse_callchain_opt(const struct option * opt,const char * arg,int unset)1373*4882a593Smuzhiyun parse_callchain_opt(const struct option *opt, const char *arg, int unset)
1374*4882a593Smuzhiyun {
1375*4882a593Smuzhiyun 	struct callchain_param *callchain = opt->value;
1376*4882a593Smuzhiyun 
1377*4882a593Smuzhiyun 	callchain->enabled = !unset;
1378*4882a593Smuzhiyun 	callchain->record_mode = CALLCHAIN_FP;
1379*4882a593Smuzhiyun 
1380*4882a593Smuzhiyun 	/*
1381*4882a593Smuzhiyun 	 * --no-call-graph
1382*4882a593Smuzhiyun 	 */
1383*4882a593Smuzhiyun 	if (unset) {
1384*4882a593Smuzhiyun 		symbol_conf.use_callchain = false;
1385*4882a593Smuzhiyun 		callchain->record_mode = CALLCHAIN_NONE;
1386*4882a593Smuzhiyun 		return 0;
1387*4882a593Smuzhiyun 	}
1388*4882a593Smuzhiyun 
1389*4882a593Smuzhiyun 	return parse_callchain_top_opt(arg);
1390*4882a593Smuzhiyun }
1391*4882a593Smuzhiyun 
perf_top_config(const char * var,const char * value,void * cb __maybe_unused)1392*4882a593Smuzhiyun static int perf_top_config(const char *var, const char *value, void *cb __maybe_unused)
1393*4882a593Smuzhiyun {
1394*4882a593Smuzhiyun 	if (!strcmp(var, "top.call-graph")) {
1395*4882a593Smuzhiyun 		var = "call-graph.record-mode";
1396*4882a593Smuzhiyun 		return perf_default_config(var, value, cb);
1397*4882a593Smuzhiyun 	}
1398*4882a593Smuzhiyun 	if (!strcmp(var, "top.children")) {
1399*4882a593Smuzhiyun 		symbol_conf.cumulate_callchain = perf_config_bool(var, value);
1400*4882a593Smuzhiyun 		return 0;
1401*4882a593Smuzhiyun 	}
1402*4882a593Smuzhiyun 
1403*4882a593Smuzhiyun 	return 0;
1404*4882a593Smuzhiyun }
1405*4882a593Smuzhiyun 
1406*4882a593Smuzhiyun static int
parse_percent_limit(const struct option * opt,const char * arg,int unset __maybe_unused)1407*4882a593Smuzhiyun parse_percent_limit(const struct option *opt, const char *arg,
1408*4882a593Smuzhiyun 		    int unset __maybe_unused)
1409*4882a593Smuzhiyun {
1410*4882a593Smuzhiyun 	struct perf_top *top = opt->value;
1411*4882a593Smuzhiyun 
1412*4882a593Smuzhiyun 	top->min_percent = strtof(arg, NULL);
1413*4882a593Smuzhiyun 	return 0;
1414*4882a593Smuzhiyun }
1415*4882a593Smuzhiyun 
1416*4882a593Smuzhiyun const char top_callchain_help[] = CALLCHAIN_RECORD_HELP CALLCHAIN_REPORT_HELP
1417*4882a593Smuzhiyun 	"\n\t\t\t\tDefault: fp,graph,0.5,caller,function";
1418*4882a593Smuzhiyun 
cmd_top(int argc,const char ** argv)1419*4882a593Smuzhiyun int cmd_top(int argc, const char **argv)
1420*4882a593Smuzhiyun {
1421*4882a593Smuzhiyun 	char errbuf[BUFSIZ];
1422*4882a593Smuzhiyun 	struct perf_top top = {
1423*4882a593Smuzhiyun 		.count_filter	     = 5,
1424*4882a593Smuzhiyun 		.delay_secs	     = 2,
1425*4882a593Smuzhiyun 		.record_opts = {
1426*4882a593Smuzhiyun 			.mmap_pages	= UINT_MAX,
1427*4882a593Smuzhiyun 			.user_freq	= UINT_MAX,
1428*4882a593Smuzhiyun 			.user_interval	= ULLONG_MAX,
1429*4882a593Smuzhiyun 			.freq		= 4000, /* 4 KHz */
1430*4882a593Smuzhiyun 			.target		= {
1431*4882a593Smuzhiyun 				.uses_mmap   = true,
1432*4882a593Smuzhiyun 			},
1433*4882a593Smuzhiyun 			/*
1434*4882a593Smuzhiyun 			 * FIXME: This will lose PERF_RECORD_MMAP and other metadata
1435*4882a593Smuzhiyun 			 * when we pause, fix that and reenable. Probably using a
1436*4882a593Smuzhiyun 			 * separate evlist with a dummy event, i.e. a non-overwrite
1437*4882a593Smuzhiyun 			 * ring buffer just for metadata events, while PERF_RECORD_SAMPLE
1438*4882a593Smuzhiyun 			 * stays in overwrite mode. -acme
1439*4882a593Smuzhiyun 			 * */
1440*4882a593Smuzhiyun 			.overwrite	= 0,
1441*4882a593Smuzhiyun 			.sample_time	= true,
1442*4882a593Smuzhiyun 			.sample_time_set = true,
1443*4882a593Smuzhiyun 		},
1444*4882a593Smuzhiyun 		.max_stack	     = sysctl__max_stack(),
1445*4882a593Smuzhiyun 		.annotation_opts     = annotation__default_options,
1446*4882a593Smuzhiyun 		.nr_threads_synthesize = UINT_MAX,
1447*4882a593Smuzhiyun 	};
1448*4882a593Smuzhiyun 	struct record_opts *opts = &top.record_opts;
1449*4882a593Smuzhiyun 	struct target *target = &opts->target;
1450*4882a593Smuzhiyun 	const struct option options[] = {
1451*4882a593Smuzhiyun 	OPT_CALLBACK('e', "event", &top.evlist, "event",
1452*4882a593Smuzhiyun 		     "event selector. use 'perf list' to list available events",
1453*4882a593Smuzhiyun 		     parse_events_option),
1454*4882a593Smuzhiyun 	OPT_U64('c', "count", &opts->user_interval, "event period to sample"),
1455*4882a593Smuzhiyun 	OPT_STRING('p', "pid", &target->pid, "pid",
1456*4882a593Smuzhiyun 		    "profile events on existing process id"),
1457*4882a593Smuzhiyun 	OPT_STRING('t', "tid", &target->tid, "tid",
1458*4882a593Smuzhiyun 		    "profile events on existing thread id"),
1459*4882a593Smuzhiyun 	OPT_BOOLEAN('a', "all-cpus", &target->system_wide,
1460*4882a593Smuzhiyun 			    "system-wide collection from all CPUs"),
1461*4882a593Smuzhiyun 	OPT_STRING('C', "cpu", &target->cpu_list, "cpu",
1462*4882a593Smuzhiyun 		    "list of cpus to monitor"),
1463*4882a593Smuzhiyun 	OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1464*4882a593Smuzhiyun 		   "file", "vmlinux pathname"),
1465*4882a593Smuzhiyun 	OPT_BOOLEAN(0, "ignore-vmlinux", &symbol_conf.ignore_vmlinux,
1466*4882a593Smuzhiyun 		    "don't load vmlinux even if found"),
1467*4882a593Smuzhiyun 	OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
1468*4882a593Smuzhiyun 		   "file", "kallsyms pathname"),
1469*4882a593Smuzhiyun 	OPT_BOOLEAN('K', "hide_kernel_symbols", &top.hide_kernel_symbols,
1470*4882a593Smuzhiyun 		    "hide kernel symbols"),
1471*4882a593Smuzhiyun 	OPT_CALLBACK('m', "mmap-pages", &opts->mmap_pages, "pages",
1472*4882a593Smuzhiyun 		     "number of mmap data pages",
1473*4882a593Smuzhiyun 		     perf_evlist__parse_mmap_pages),
1474*4882a593Smuzhiyun 	OPT_INTEGER('r', "realtime", &top.realtime_prio,
1475*4882a593Smuzhiyun 		    "collect data with this RT SCHED_FIFO priority"),
1476*4882a593Smuzhiyun 	OPT_INTEGER('d', "delay", &top.delay_secs,
1477*4882a593Smuzhiyun 		    "number of seconds to delay between refreshes"),
1478*4882a593Smuzhiyun 	OPT_BOOLEAN('D', "dump-symtab", &top.dump_symtab,
1479*4882a593Smuzhiyun 			    "dump the symbol table used for profiling"),
1480*4882a593Smuzhiyun 	OPT_INTEGER('f', "count-filter", &top.count_filter,
1481*4882a593Smuzhiyun 		    "only display functions with more events than this"),
1482*4882a593Smuzhiyun 	OPT_BOOLEAN(0, "group", &opts->group,
1483*4882a593Smuzhiyun 			    "put the counters into a counter group"),
1484*4882a593Smuzhiyun 	OPT_BOOLEAN('i', "no-inherit", &opts->no_inherit,
1485*4882a593Smuzhiyun 		    "child tasks do not inherit counters"),
1486*4882a593Smuzhiyun 	OPT_STRING(0, "sym-annotate", &top.sym_filter, "symbol name",
1487*4882a593Smuzhiyun 		    "symbol to annotate"),
1488*4882a593Smuzhiyun 	OPT_BOOLEAN('z', "zero", &top.zero, "zero history across updates"),
1489*4882a593Smuzhiyun 	OPT_CALLBACK('F', "freq", &top.record_opts, "freq or 'max'",
1490*4882a593Smuzhiyun 		     "profile at this frequency",
1491*4882a593Smuzhiyun 		      record__parse_freq),
1492*4882a593Smuzhiyun 	OPT_INTEGER('E', "entries", &top.print_entries,
1493*4882a593Smuzhiyun 		    "display this many functions"),
1494*4882a593Smuzhiyun 	OPT_BOOLEAN('U', "hide_user_symbols", &top.hide_user_symbols,
1495*4882a593Smuzhiyun 		    "hide user symbols"),
1496*4882a593Smuzhiyun 	OPT_BOOLEAN(0, "tui", &top.use_tui, "Use the TUI interface"),
1497*4882a593Smuzhiyun 	OPT_BOOLEAN(0, "stdio", &top.use_stdio, "Use the stdio interface"),
1498*4882a593Smuzhiyun 	OPT_INCR('v', "verbose", &verbose,
1499*4882a593Smuzhiyun 		    "be more verbose (show counter open errors, etc)"),
1500*4882a593Smuzhiyun 	OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1501*4882a593Smuzhiyun 		   "sort by key(s): pid, comm, dso, symbol, parent, cpu, srcline, ..."
1502*4882a593Smuzhiyun 		   " Please refer the man page for the complete list."),
1503*4882a593Smuzhiyun 	OPT_STRING(0, "fields", &field_order, "key[,keys...]",
1504*4882a593Smuzhiyun 		   "output field(s): overhead, period, sample plus all of sort keys"),
1505*4882a593Smuzhiyun 	OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
1506*4882a593Smuzhiyun 		    "Show a column with the number of samples"),
1507*4882a593Smuzhiyun 	OPT_CALLBACK_NOOPT('g', NULL, &callchain_param,
1508*4882a593Smuzhiyun 			   NULL, "enables call-graph recording and display",
1509*4882a593Smuzhiyun 			   &callchain_opt),
1510*4882a593Smuzhiyun 	OPT_CALLBACK(0, "call-graph", &callchain_param,
1511*4882a593Smuzhiyun 		     "record_mode[,record_size],print_type,threshold[,print_limit],order,sort_key[,branch]",
1512*4882a593Smuzhiyun 		     top_callchain_help, &parse_callchain_opt),
1513*4882a593Smuzhiyun 	OPT_BOOLEAN(0, "children", &symbol_conf.cumulate_callchain,
1514*4882a593Smuzhiyun 		    "Accumulate callchains of children and show total overhead as well"),
1515*4882a593Smuzhiyun 	OPT_INTEGER(0, "max-stack", &top.max_stack,
1516*4882a593Smuzhiyun 		    "Set the maximum stack depth when parsing the callchain. "
1517*4882a593Smuzhiyun 		    "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
1518*4882a593Smuzhiyun 	OPT_CALLBACK(0, "ignore-callees", NULL, "regex",
1519*4882a593Smuzhiyun 		   "ignore callees of these functions in call graphs",
1520*4882a593Smuzhiyun 		   report_parse_ignore_callees_opt),
1521*4882a593Smuzhiyun 	OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period,
1522*4882a593Smuzhiyun 		    "Show a column with the sum of periods"),
1523*4882a593Smuzhiyun 	OPT_STRING(0, "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
1524*4882a593Smuzhiyun 		   "only consider symbols in these dsos"),
1525*4882a593Smuzhiyun 	OPT_STRING(0, "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
1526*4882a593Smuzhiyun 		   "only consider symbols in these comms"),
1527*4882a593Smuzhiyun 	OPT_STRING(0, "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
1528*4882a593Smuzhiyun 		   "only consider these symbols"),
1529*4882a593Smuzhiyun 	OPT_BOOLEAN(0, "source", &top.annotation_opts.annotate_src,
1530*4882a593Smuzhiyun 		    "Interleave source code with assembly code (default)"),
1531*4882a593Smuzhiyun 	OPT_BOOLEAN(0, "asm-raw", &top.annotation_opts.show_asm_raw,
1532*4882a593Smuzhiyun 		    "Display raw encoding of assembly instructions (default)"),
1533*4882a593Smuzhiyun 	OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
1534*4882a593Smuzhiyun 		    "Enable kernel symbol demangling"),
1535*4882a593Smuzhiyun 	OPT_BOOLEAN(0, "no-bpf-event", &top.record_opts.no_bpf_event, "do not record bpf events"),
1536*4882a593Smuzhiyun 	OPT_STRING(0, "objdump", &top.annotation_opts.objdump_path, "path",
1537*4882a593Smuzhiyun 		    "objdump binary to use for disassembly and annotations"),
1538*4882a593Smuzhiyun 	OPT_STRING('M', "disassembler-style", &top.annotation_opts.disassembler_style, "disassembler style",
1539*4882a593Smuzhiyun 		   "Specify disassembler style (e.g. -M intel for intel syntax)"),
1540*4882a593Smuzhiyun 	OPT_STRING(0, "prefix", &top.annotation_opts.prefix, "prefix",
1541*4882a593Smuzhiyun 		    "Add prefix to source file path names in programs (with --prefix-strip)"),
1542*4882a593Smuzhiyun 	OPT_STRING(0, "prefix-strip", &top.annotation_opts.prefix_strip, "N",
1543*4882a593Smuzhiyun 		    "Strip first N entries of source file path name in programs (with --prefix)"),
1544*4882a593Smuzhiyun 	OPT_STRING('u', "uid", &target->uid_str, "user", "user to profile"),
1545*4882a593Smuzhiyun 	OPT_CALLBACK(0, "percent-limit", &top, "percent",
1546*4882a593Smuzhiyun 		     "Don't show entries under that percent", parse_percent_limit),
1547*4882a593Smuzhiyun 	OPT_CALLBACK(0, "percentage", NULL, "relative|absolute",
1548*4882a593Smuzhiyun 		     "How to display percentage of filtered entries", parse_filter_percentage),
1549*4882a593Smuzhiyun 	OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str,
1550*4882a593Smuzhiyun 		   "width[,width...]",
1551*4882a593Smuzhiyun 		   "don't try to adjust column width, use these fixed values"),
1552*4882a593Smuzhiyun 	OPT_UINTEGER(0, "proc-map-timeout", &proc_map_timeout,
1553*4882a593Smuzhiyun 			"per thread proc mmap processing timeout in ms"),
1554*4882a593Smuzhiyun 	OPT_CALLBACK_NOOPT('b', "branch-any", &opts->branch_stack,
1555*4882a593Smuzhiyun 		     "branch any", "sample any taken branches",
1556*4882a593Smuzhiyun 		     parse_branch_stack),
1557*4882a593Smuzhiyun 	OPT_CALLBACK('j', "branch-filter", &opts->branch_stack,
1558*4882a593Smuzhiyun 		     "branch filter mask", "branch stack filter modes",
1559*4882a593Smuzhiyun 		     parse_branch_stack),
1560*4882a593Smuzhiyun 	OPT_BOOLEAN(0, "raw-trace", &symbol_conf.raw_trace,
1561*4882a593Smuzhiyun 		    "Show raw trace event output (do not use print fmt or plugins)"),
1562*4882a593Smuzhiyun 	OPT_BOOLEAN(0, "hierarchy", &symbol_conf.report_hierarchy,
1563*4882a593Smuzhiyun 		    "Show entries in a hierarchy"),
1564*4882a593Smuzhiyun 	OPT_BOOLEAN(0, "overwrite", &top.record_opts.overwrite,
1565*4882a593Smuzhiyun 		    "Use a backward ring buffer, default: no"),
1566*4882a593Smuzhiyun 	OPT_BOOLEAN(0, "force", &symbol_conf.force, "don't complain, do it"),
1567*4882a593Smuzhiyun 	OPT_UINTEGER(0, "num-thread-synthesize", &top.nr_threads_synthesize,
1568*4882a593Smuzhiyun 			"number of thread to run event synthesize"),
1569*4882a593Smuzhiyun 	OPT_BOOLEAN(0, "namespaces", &opts->record_namespaces,
1570*4882a593Smuzhiyun 		    "Record namespaces events"),
1571*4882a593Smuzhiyun 	OPT_BOOLEAN(0, "all-cgroups", &opts->record_cgroup,
1572*4882a593Smuzhiyun 		    "Record cgroup events"),
1573*4882a593Smuzhiyun 	OPT_INTEGER(0, "group-sort-idx", &symbol_conf.group_sort_idx,
1574*4882a593Smuzhiyun 		    "Sort the output by the event at the index n in group. "
1575*4882a593Smuzhiyun 		    "If n is invalid, sort by the first event. "
1576*4882a593Smuzhiyun 		    "WARNING: should be used on grouped events."),
1577*4882a593Smuzhiyun 	OPT_BOOLEAN(0, "stitch-lbr", &top.stitch_lbr,
1578*4882a593Smuzhiyun 		    "Enable LBR callgraph stitching approach"),
1579*4882a593Smuzhiyun #ifdef HAVE_LIBPFM
1580*4882a593Smuzhiyun 	OPT_CALLBACK(0, "pfm-events", &top.evlist, "event",
1581*4882a593Smuzhiyun 		"libpfm4 event selector. use 'perf list' to list available events",
1582*4882a593Smuzhiyun 		parse_libpfm_events_option),
1583*4882a593Smuzhiyun #endif
1584*4882a593Smuzhiyun 	OPTS_EVSWITCH(&top.evswitch),
1585*4882a593Smuzhiyun 	OPT_END()
1586*4882a593Smuzhiyun 	};
1587*4882a593Smuzhiyun 	const char * const top_usage[] = {
1588*4882a593Smuzhiyun 		"perf top [<options>]",
1589*4882a593Smuzhiyun 		NULL
1590*4882a593Smuzhiyun 	};
1591*4882a593Smuzhiyun 	int status = hists__init();
1592*4882a593Smuzhiyun 
1593*4882a593Smuzhiyun 	if (status < 0)
1594*4882a593Smuzhiyun 		return status;
1595*4882a593Smuzhiyun 
1596*4882a593Smuzhiyun 	top.annotation_opts.min_pcnt = 5;
1597*4882a593Smuzhiyun 	top.annotation_opts.context  = 4;
1598*4882a593Smuzhiyun 
1599*4882a593Smuzhiyun 	top.evlist = evlist__new();
1600*4882a593Smuzhiyun 	if (top.evlist == NULL)
1601*4882a593Smuzhiyun 		return -ENOMEM;
1602*4882a593Smuzhiyun 
1603*4882a593Smuzhiyun 	status = perf_config(perf_top_config, &top);
1604*4882a593Smuzhiyun 	if (status)
1605*4882a593Smuzhiyun 		return status;
1606*4882a593Smuzhiyun 	/*
1607*4882a593Smuzhiyun 	 * Since the per arch annotation init routine may need the cpuid, read
1608*4882a593Smuzhiyun 	 * it here, since we are not getting this from the perf.data header.
1609*4882a593Smuzhiyun 	 */
1610*4882a593Smuzhiyun 	status = perf_env__read_cpuid(&perf_env);
1611*4882a593Smuzhiyun 	if (status) {
1612*4882a593Smuzhiyun 		/*
1613*4882a593Smuzhiyun 		 * Some arches do not provide a get_cpuid(), so just use pr_debug, otherwise
1614*4882a593Smuzhiyun 		 * warn the user explicitely.
1615*4882a593Smuzhiyun 		 */
1616*4882a593Smuzhiyun 		eprintf(status == ENOSYS ? 1 : 0, verbose,
1617*4882a593Smuzhiyun 			"Couldn't read the cpuid for this machine: %s\n",
1618*4882a593Smuzhiyun 			str_error_r(errno, errbuf, sizeof(errbuf)));
1619*4882a593Smuzhiyun 	}
1620*4882a593Smuzhiyun 	top.evlist->env = &perf_env;
1621*4882a593Smuzhiyun 
1622*4882a593Smuzhiyun 	argc = parse_options(argc, argv, options, top_usage, 0);
1623*4882a593Smuzhiyun 	if (argc)
1624*4882a593Smuzhiyun 		usage_with_options(top_usage, options);
1625*4882a593Smuzhiyun 
1626*4882a593Smuzhiyun 	if (annotate_check_args(&top.annotation_opts) < 0)
1627*4882a593Smuzhiyun 		goto out_delete_evlist;
1628*4882a593Smuzhiyun 
1629*4882a593Smuzhiyun 	if (!top.evlist->core.nr_entries &&
1630*4882a593Smuzhiyun 	    evlist__add_default(top.evlist) < 0) {
1631*4882a593Smuzhiyun 		pr_err("Not enough memory for event selector list\n");
1632*4882a593Smuzhiyun 		goto out_delete_evlist;
1633*4882a593Smuzhiyun 	}
1634*4882a593Smuzhiyun 
1635*4882a593Smuzhiyun 	status = evswitch__init(&top.evswitch, top.evlist, stderr);
1636*4882a593Smuzhiyun 	if (status)
1637*4882a593Smuzhiyun 		goto out_delete_evlist;
1638*4882a593Smuzhiyun 
1639*4882a593Smuzhiyun 	if (symbol_conf.report_hierarchy) {
1640*4882a593Smuzhiyun 		/* disable incompatible options */
1641*4882a593Smuzhiyun 		symbol_conf.event_group = false;
1642*4882a593Smuzhiyun 		symbol_conf.cumulate_callchain = false;
1643*4882a593Smuzhiyun 
1644*4882a593Smuzhiyun 		if (field_order) {
1645*4882a593Smuzhiyun 			pr_err("Error: --hierarchy and --fields options cannot be used together\n");
1646*4882a593Smuzhiyun 			parse_options_usage(top_usage, options, "fields", 0);
1647*4882a593Smuzhiyun 			parse_options_usage(NULL, options, "hierarchy", 0);
1648*4882a593Smuzhiyun 			goto out_delete_evlist;
1649*4882a593Smuzhiyun 		}
1650*4882a593Smuzhiyun 	}
1651*4882a593Smuzhiyun 
1652*4882a593Smuzhiyun 	if (top.stitch_lbr && !(callchain_param.record_mode == CALLCHAIN_LBR)) {
1653*4882a593Smuzhiyun 		pr_err("Error: --stitch-lbr must be used with --call-graph lbr\n");
1654*4882a593Smuzhiyun 		goto out_delete_evlist;
1655*4882a593Smuzhiyun 	}
1656*4882a593Smuzhiyun 
1657*4882a593Smuzhiyun 	if (opts->branch_stack && callchain_param.enabled)
1658*4882a593Smuzhiyun 		symbol_conf.show_branchflag_count = true;
1659*4882a593Smuzhiyun 
1660*4882a593Smuzhiyun 	sort__mode = SORT_MODE__TOP;
1661*4882a593Smuzhiyun 	/* display thread wants entries to be collapsed in a different tree */
1662*4882a593Smuzhiyun 	perf_hpp_list.need_collapse = 1;
1663*4882a593Smuzhiyun 
1664*4882a593Smuzhiyun 	if (top.use_stdio)
1665*4882a593Smuzhiyun 		use_browser = 0;
1666*4882a593Smuzhiyun 	else if (top.use_tui)
1667*4882a593Smuzhiyun 		use_browser = 1;
1668*4882a593Smuzhiyun 
1669*4882a593Smuzhiyun 	setup_browser(false);
1670*4882a593Smuzhiyun 
1671*4882a593Smuzhiyun 	if (setup_sorting(top.evlist) < 0) {
1672*4882a593Smuzhiyun 		if (sort_order)
1673*4882a593Smuzhiyun 			parse_options_usage(top_usage, options, "s", 1);
1674*4882a593Smuzhiyun 		if (field_order)
1675*4882a593Smuzhiyun 			parse_options_usage(sort_order ? NULL : top_usage,
1676*4882a593Smuzhiyun 					    options, "fields", 0);
1677*4882a593Smuzhiyun 		goto out_delete_evlist;
1678*4882a593Smuzhiyun 	}
1679*4882a593Smuzhiyun 
1680*4882a593Smuzhiyun 	status = target__validate(target);
1681*4882a593Smuzhiyun 	if (status) {
1682*4882a593Smuzhiyun 		target__strerror(target, status, errbuf, BUFSIZ);
1683*4882a593Smuzhiyun 		ui__warning("%s\n", errbuf);
1684*4882a593Smuzhiyun 	}
1685*4882a593Smuzhiyun 
1686*4882a593Smuzhiyun 	status = target__parse_uid(target);
1687*4882a593Smuzhiyun 	if (status) {
1688*4882a593Smuzhiyun 		int saved_errno = errno;
1689*4882a593Smuzhiyun 
1690*4882a593Smuzhiyun 		target__strerror(target, status, errbuf, BUFSIZ);
1691*4882a593Smuzhiyun 		ui__error("%s\n", errbuf);
1692*4882a593Smuzhiyun 
1693*4882a593Smuzhiyun 		status = -saved_errno;
1694*4882a593Smuzhiyun 		goto out_delete_evlist;
1695*4882a593Smuzhiyun 	}
1696*4882a593Smuzhiyun 
1697*4882a593Smuzhiyun 	if (target__none(target))
1698*4882a593Smuzhiyun 		target->system_wide = true;
1699*4882a593Smuzhiyun 
1700*4882a593Smuzhiyun 	if (perf_evlist__create_maps(top.evlist, target) < 0) {
1701*4882a593Smuzhiyun 		ui__error("Couldn't create thread/CPU maps: %s\n",
1702*4882a593Smuzhiyun 			  errno == ENOENT ? "No such process" : str_error_r(errno, errbuf, sizeof(errbuf)));
1703*4882a593Smuzhiyun 		goto out_delete_evlist;
1704*4882a593Smuzhiyun 	}
1705*4882a593Smuzhiyun 
1706*4882a593Smuzhiyun 	if (top.delay_secs < 1)
1707*4882a593Smuzhiyun 		top.delay_secs = 1;
1708*4882a593Smuzhiyun 
1709*4882a593Smuzhiyun 	if (record_opts__config(opts)) {
1710*4882a593Smuzhiyun 		status = -EINVAL;
1711*4882a593Smuzhiyun 		goto out_delete_evlist;
1712*4882a593Smuzhiyun 	}
1713*4882a593Smuzhiyun 
1714*4882a593Smuzhiyun 	top.sym_evsel = evlist__first(top.evlist);
1715*4882a593Smuzhiyun 
1716*4882a593Smuzhiyun 	if (!callchain_param.enabled) {
1717*4882a593Smuzhiyun 		symbol_conf.cumulate_callchain = false;
1718*4882a593Smuzhiyun 		perf_hpp__cancel_cumulate();
1719*4882a593Smuzhiyun 	}
1720*4882a593Smuzhiyun 
1721*4882a593Smuzhiyun 	if (symbol_conf.cumulate_callchain && !callchain_param.order_set)
1722*4882a593Smuzhiyun 		callchain_param.order = ORDER_CALLER;
1723*4882a593Smuzhiyun 
1724*4882a593Smuzhiyun 	status = symbol__annotation_init();
1725*4882a593Smuzhiyun 	if (status < 0)
1726*4882a593Smuzhiyun 		goto out_delete_evlist;
1727*4882a593Smuzhiyun 
1728*4882a593Smuzhiyun 	annotation_config__init(&top.annotation_opts);
1729*4882a593Smuzhiyun 
1730*4882a593Smuzhiyun 	symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL);
1731*4882a593Smuzhiyun 	status = symbol__init(NULL);
1732*4882a593Smuzhiyun 	if (status < 0)
1733*4882a593Smuzhiyun 		goto out_delete_evlist;
1734*4882a593Smuzhiyun 
1735*4882a593Smuzhiyun 	sort__setup_elide(stdout);
1736*4882a593Smuzhiyun 
1737*4882a593Smuzhiyun 	get_term_dimensions(&top.winsize);
1738*4882a593Smuzhiyun 	if (top.print_entries == 0) {
1739*4882a593Smuzhiyun 		perf_top__update_print_entries(&top);
1740*4882a593Smuzhiyun 		signal(SIGWINCH, winch_sig);
1741*4882a593Smuzhiyun 	}
1742*4882a593Smuzhiyun 
1743*4882a593Smuzhiyun 	top.session = perf_session__new(NULL, false, NULL);
1744*4882a593Smuzhiyun 	if (IS_ERR(top.session)) {
1745*4882a593Smuzhiyun 		status = PTR_ERR(top.session);
1746*4882a593Smuzhiyun 		goto out_delete_evlist;
1747*4882a593Smuzhiyun 	}
1748*4882a593Smuzhiyun 
1749*4882a593Smuzhiyun #ifdef HAVE_LIBBPF_SUPPORT
1750*4882a593Smuzhiyun 	if (!top.record_opts.no_bpf_event) {
1751*4882a593Smuzhiyun 		top.sb_evlist = evlist__new();
1752*4882a593Smuzhiyun 
1753*4882a593Smuzhiyun 		if (top.sb_evlist == NULL) {
1754*4882a593Smuzhiyun 			pr_err("Couldn't create side band evlist.\n.");
1755*4882a593Smuzhiyun 			goto out_delete_evlist;
1756*4882a593Smuzhiyun 		}
1757*4882a593Smuzhiyun 
1758*4882a593Smuzhiyun 		if (evlist__add_bpf_sb_event(top.sb_evlist, &perf_env)) {
1759*4882a593Smuzhiyun 			pr_err("Couldn't ask for PERF_RECORD_BPF_EVENT side band events.\n.");
1760*4882a593Smuzhiyun 			goto out_delete_evlist;
1761*4882a593Smuzhiyun 		}
1762*4882a593Smuzhiyun 	}
1763*4882a593Smuzhiyun #endif
1764*4882a593Smuzhiyun 
1765*4882a593Smuzhiyun 	if (perf_evlist__start_sb_thread(top.sb_evlist, target)) {
1766*4882a593Smuzhiyun 		pr_debug("Couldn't start the BPF side band thread:\nBPF programs starting from now on won't be annotatable\n");
1767*4882a593Smuzhiyun 		opts->no_bpf_event = true;
1768*4882a593Smuzhiyun 	}
1769*4882a593Smuzhiyun 
1770*4882a593Smuzhiyun 	status = __cmd_top(&top);
1771*4882a593Smuzhiyun 
1772*4882a593Smuzhiyun 	if (!opts->no_bpf_event)
1773*4882a593Smuzhiyun 		perf_evlist__stop_sb_thread(top.sb_evlist);
1774*4882a593Smuzhiyun 
1775*4882a593Smuzhiyun out_delete_evlist:
1776*4882a593Smuzhiyun 	evlist__delete(top.evlist);
1777*4882a593Smuzhiyun 	perf_session__delete(top.session);
1778*4882a593Smuzhiyun 
1779*4882a593Smuzhiyun 	return status;
1780*4882a593Smuzhiyun }
1781