blob: 1fc390f136dd90b1d845359fb16529c3f47d6a4f [file] [log] [blame]
Thomas Gleixner91007042019-05-29 07:12:25 -07001// SPDX-License-Identifier: GPL-2.0-only
Ingo Molnar07800602009-04-20 15:00:56 +02002/*
Ingo Molnarbf9e1872009-06-02 23:37:05 +02003 * builtin-top.c
4 *
5 * Builtin top command: Display a continuously updated profile of
6 * any workload, CPU or specific PID.
7 *
8 * Copyright (C) 2008, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -03009 * 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
Ingo Molnarbf9e1872009-06-02 23:37:05 +020010 *
11 * Improvements and fixes by:
12 *
13 * Arjan van de Ven <arjan@linux.intel.com>
14 * Yanmin Zhang <yanmin.zhang@intel.com>
15 * Wu Fengguang <fengguang.wu@intel.com>
16 * Mike Galbraith <efault@gmx.de>
17 * Paul Mackerras <paulus@samba.org>
Ingo Molnar07800602009-04-20 15:00:56 +020018 */
Ingo Molnarbf9e1872009-06-02 23:37:05 +020019#include "builtin.h"
Ingo Molnar07800602009-04-20 15:00:56 +020020
Peter Zijlstra1a482f32009-05-23 18:28:58 +020021#include "perf.h"
Ingo Molnarbf9e1872009-06-02 23:37:05 +020022
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -020023#include "util/annotate.h"
Arnaldo Carvalho de Meloa40b95b2019-01-17 08:15:20 -080024#include "util/bpf-event.h"
Joshua Martinez51f38242021-06-16 23:18:29 +000025#include "util/cgroup.h"
Taeung Song41840d22016-06-23 17:55:17 +090026#include "util/config.h"
Ingo Molnar8fc03212009-06-04 15:19:47 +020027#include "util/color.h"
Arnaldo Carvalho de Melo4a3cec82019-08-30 11:11:01 -030028#include "util/dso.h"
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020029#include "util/evlist.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020030#include "util/evsel.h"
Arnaldo Carvalho de Melo95be9d12019-09-24 15:56:14 -030031#include "util/evsel_config.h"
Arnaldo Carvalho de Melo5ab8c682017-04-25 15:30:47 -030032#include "util/event.h"
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -030033#include "util/machine.h"
Arnaldo Carvalho de Melo1101f692019-01-27 13:42:37 +010034#include "util/map.h"
Arnaldo Carvalho de Meloe0fcfb02019-09-23 12:20:38 -030035#include "util/mmap.h"
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -020036#include "util/session.h"
Kan Liang13e0c842020-03-19 13:25:15 -070037#include "util/thread.h"
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -020038#include "util/symbol.h"
Arnaldo Carvalho de Meloea49e012019-09-18 11:36:13 -030039#include "util/synthetic-events.h"
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -020040#include "util/top.h"
Arnaldo Carvalho de Melo2da39f12019-08-27 11:51:18 -030041#include "util/util.h"
Arnaldo Carvalho de Melo43cbcd82009-07-01 12:28:37 -030042#include <linux/rbtree.h>
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060043#include <subcmd/parse-options.h>
Ingo Molnarb456bae2009-05-26 09:17:18 +020044#include "util/parse-events.h"
Arnaldo Carvalho de Melo5c9dbe62019-08-30 11:54:00 -030045#include "util/callchain.h"
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110046#include "util/cpumap.h"
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -030047#include "util/sort.h"
Arnaldo Carvalho de Melo6a9fa4e2019-06-25 17:31:26 -030048#include "util/string2.h"
Arnaldo Carvalho de Melob0742e92017-04-18 11:08:10 -030049#include "util/term.h"
David Ahern6b118e92012-07-30 22:31:35 -060050#include "util/intlist.h"
Andi Kleena18b027e2015-07-18 08:24:52 -070051#include "util/parse-branch-options.h"
Sukadev Bhattiprolu0d3942d2013-05-14 22:56:51 -070052#include "arch/common.h"
Arnaldo Carvalho de Melo8520a982019-08-29 16:18:59 -030053#include "ui/ui.h"
Ingo Molnar07800602009-04-20 15:00:56 +020054
Frederic Weisbecker8f288272009-08-16 22:05:48 +020055#include "util/debug.h"
Jiri Olsa16c66bc2018-11-05 13:24:55 +010056#include "util/ordered-events.h"
Stephane Eranian70943492020-05-05 11:29:43 -070057#include "util/pfm.h"
Frederic Weisbecker8f288272009-08-16 22:05:48 +020058
Ingo Molnar07800602009-04-20 15:00:56 +020059#include <assert.h>
Arnaldo Carvalho de Melo31d68e72012-03-27 12:55:57 -030060#include <elf.h>
Ingo Molnar07800602009-04-20 15:00:56 +020061#include <fcntl.h>
Ingo Molnar0e9b20b2009-05-26 09:17:18 +020062
Ingo Molnar07800602009-04-20 15:00:56 +020063#include <stdio.h>
Mike Galbraith923c42c2009-07-22 20:36:03 +020064#include <termios.h>
65#include <unistd.h>
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -020066#include <inttypes.h>
Ingo Molnar0e9b20b2009-05-26 09:17:18 +020067
Ingo Molnar07800602009-04-20 15:00:56 +020068#include <errno.h>
Ingo Molnar07800602009-04-20 15:00:56 +020069#include <time.h>
70#include <sched.h>
Arnaldo Carvalho de Melo9607ad32017-04-19 15:49:18 -030071#include <signal.h>
Ingo Molnar07800602009-04-20 15:00:56 +020072
73#include <sys/syscall.h>
74#include <sys/ioctl.h>
Arnaldo Carvalho de Meloa8fa4962014-09-15 15:54:34 -030075#include <poll.h>
Ingo Molnar07800602009-04-20 15:00:56 +020076#include <sys/prctl.h>
77#include <sys/wait.h>
78#include <sys/uio.h>
Arnaldo Carvalho de Melo31d68e72012-03-27 12:55:57 -030079#include <sys/utsname.h>
Ingo Molnar07800602009-04-20 15:00:56 +020080#include <sys/mman.h>
81
Arnaldo Carvalho de Melo531d2412016-03-23 15:16:55 -030082#include <linux/stringify.h>
Arnaldo Carvalho de Melob9c4b0f2016-08-08 15:37:58 -030083#include <linux/time64.h>
Ingo Molnar07800602009-04-20 15:00:56 +020084#include <linux/types.h>
Mamatha Inamdar6ef81c52019-08-22 12:50:49 +053085#include <linux/err.h>
Ingo Molnar07800602009-04-20 15:00:56 +020086
Arnaldo Carvalho de Melo3052ba52019-06-25 17:27:31 -030087#include <linux/ctype.h>
Jiri Olsa7728fa02019-10-07 14:53:17 +020088#include <perf/mmap.h>
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030089
Arnaldo Carvalho de Melo11859e82013-01-30 13:25:53 -030090static volatile int done;
Jiri Olsab135e5e2017-11-14 10:23:39 +010091static volatile int resize;
Arnaldo Carvalho de Melo11859e82013-01-30 13:25:53 -030092
Namhyung Kim933cbb12013-05-14 11:08:59 +090093#define HEADER_LINE_NR 5
94
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -020095static void perf_top__update_print_entries(struct perf_top *top)
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -020096{
Namhyung Kim933cbb12013-05-14 11:08:59 +090097 top->print_entries = top->winsize.ws_row - HEADER_LINE_NR;
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -020098}
99
Jiri Olsa244a1082017-11-15 14:30:57 +0100100static void winch_sig(int sig __maybe_unused)
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200101{
Jiri Olsab135e5e2017-11-14 10:23:39 +0100102 resize = 1;
103}
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200104
Jiri Olsab135e5e2017-11-14 10:23:39 +0100105static void perf_top__resize(struct perf_top *top)
106{
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200107 get_term_dimensions(&top->winsize);
108 perf_top__update_print_entries(top);
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200109}
110
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200111static int perf_top__parse_source(struct perf_top *top, struct hist_entry *he)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200112{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200113 struct evsel *evsel;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200114 struct symbol *sym;
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200115 struct annotation *notes;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300116 struct map *map;
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200117 int err = -1;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200118
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300119 if (!he || !he->ms.sym)
Arnaldo Carvalho de Melob0a9ab62010-03-15 11:46:58 -0300120 return -1;
121
Leo Yan111442c2019-07-02 18:34:12 +0800122 evsel = hists_to_evsel(he->hists);
123
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300124 sym = he->ms.sym;
125 map = he->ms.map;
Arnaldo Carvalho de Melob0a9ab62010-03-15 11:46:58 -0300126
127 /*
128 * We can't annotate with just /proc/kallsyms
129 */
Adrian Hunterbbb7f842013-08-07 14:38:54 +0300130 if (map->dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS &&
131 !dso__is_kcore(map->dso)) {
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200132 pr_err("Can't annotate %s: No vmlinux file was found in the "
133 "path\n", sym->name);
134 sleep(1);
Arnaldo Carvalho de Melob0a9ab62010-03-15 11:46:58 -0300135 return -1;
Arnaldo Carvalho de Melob2698762009-11-17 18:38:02 -0200136 }
137
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200138 notes = symbol__annotation(sym);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200139 pthread_mutex_lock(&notes->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200140
Jiri Olsa6484d2f2019-07-21 13:24:28 +0200141 if (!symbol__hists(sym, top->evlist->core.nr_entries)) {
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300142 pthread_mutex_unlock(&notes->lock);
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200143 pr_err("Not enough memory for annotating '%s' symbol!\n",
144 sym->name);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200145 sleep(1);
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300146 return err;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200147 }
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200148
Ravi Bangoriae0ad4d62020-02-04 10:22:28 +0530149 err = symbol__annotate(&he->ms, evsel, &top->annotation_opts, NULL);
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200150 if (err == 0) {
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200151 top->sym_filter_entry = he;
Arnaldo Carvalho de Meloee51d852016-07-29 16:27:18 -0300152 } else {
153 char msg[BUFSIZ];
Arnaldo Carvalho de Melo29754892019-11-04 11:10:00 -0300154 symbol__strerror_disassemble(&he->ms, err, msg, sizeof(msg));
Arnaldo Carvalho de Meloee51d852016-07-29 16:27:18 -0300155 pr_err("Couldn't annotate %s: %s\n", sym->name, msg);
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200156 }
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300157
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200158 pthread_mutex_unlock(&notes->lock);
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200159 return err;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200160}
161
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300162static void __zero_source_counters(struct hist_entry *he)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200163{
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300164 struct symbol *sym = he->ms.sym;
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200165 symbol__annotate_zero_histograms(sym);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200166}
167
Arnaldo Carvalho de Melo31d68e72012-03-27 12:55:57 -0300168static void ui__warn_map_erange(struct map *map, struct symbol *sym, u64 ip)
169{
170 struct utsname uts;
171 int err = uname(&uts);
172
173 ui__warning("Out of bounds address found:\n\n"
174 "Addr: %" PRIx64 "\n"
175 "DSO: %s %c\n"
176 "Map: %" PRIx64 "-%" PRIx64 "\n"
177 "Symbol: %" PRIx64 "-%" PRIx64 " %c %s\n"
178 "Arch: %s\n"
179 "Kernel: %s\n"
180 "Tools: %s\n\n"
181 "Not all samples will be on the annotation output.\n\n"
182 "Please report to linux-kernel@vger.kernel.org\n",
183 ip, map->dso->long_name, dso__symtab_origin(map->dso),
184 map->start, map->end, sym->start, sym->end,
185 sym->binding == STB_GLOBAL ? 'g' :
186 sym->binding == STB_LOCAL ? 'l' : 'w', sym->name,
187 err ? "[unknown]" : uts.machine,
188 err ? "[unknown]" : uts.release, perf_version_string);
189 if (use_browser <= 0)
190 sleep(5);
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -0300191
Arnaldo Carvalho de Melo31d68e72012-03-27 12:55:57 -0300192 map->erange_warned = true;
193}
194
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200195static void perf_top__record_precise_ip(struct perf_top *top,
196 struct hist_entry *he,
Taeung Songbab89f62017-07-20 16:28:53 -0300197 struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200198 struct evsel *evsel, u64 ip)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200199{
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200200 struct annotation *notes;
Namhyung Kimbeefb8d2015-12-11 11:56:56 +0900201 struct symbol *sym = he->ms.sym;
Namhyung Kim48c65bd2014-02-20 10:32:53 +0900202 int err = 0;
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200203
Namhyung Kimbeefb8d2015-12-11 11:56:56 +0900204 if (sym == NULL || (use_browser == 0 &&
205 (top->sym_filter_entry == NULL ||
206 top->sym_filter_entry->ms.sym != sym)))
Mike Galbraith923c42c2009-07-22 20:36:03 +0200207 return;
208
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200209 notes = symbol__annotation(sym);
210
211 if (pthread_mutex_trylock(&notes->lock))
Mike Galbraith923c42c2009-07-22 20:36:03 +0200212 return;
213
Arnaldo Carvalho de Meloe345f3b2018-05-24 12:05:39 -0300214 err = hist_entry__inc_addr_samples(he, sample, evsel, ip);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200215
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200216 pthread_mutex_unlock(&notes->lock);
Arnaldo Carvalho de Melo31d68e72012-03-27 12:55:57 -0300217
Namhyung Kim151ee832015-12-11 11:56:54 +0900218 if (unlikely(err)) {
219 /*
220 * This function is now called with he->hists->lock held.
221 * Release it before going to sleep.
222 */
223 pthread_mutex_unlock(&he->hists->lock);
Namhyung Kim7c503912014-01-07 17:41:03 +0900224
Namhyung Kim151ee832015-12-11 11:56:54 +0900225 if (err == -ERANGE && !he->ms.map->erange_warned)
226 ui__warn_map_erange(he->ms.map, sym, ip);
227 else if (err == -ENOMEM) {
228 pr_err("Not enough memory for annotating '%s' symbol!\n",
229 sym->name);
230 sleep(1);
231 }
232
233 pthread_mutex_lock(&he->hists->lock);
Arnaldo Carvalho de Melob66d8c02013-12-18 15:37:41 -0300234 }
Mike Galbraith923c42c2009-07-22 20:36:03 +0200235}
236
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200237static void perf_top__show_details(struct perf_top *top)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200238{
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200239 struct hist_entry *he = top->sym_filter_entry;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200240 struct evsel *evsel;
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200241 struct annotation *notes;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200242 struct symbol *symbol;
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200243 int more;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200244
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300245 if (!he)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200246 return;
247
Leo Yan111442c2019-07-02 18:34:12 +0800248 evsel = hists_to_evsel(he->hists);
249
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300250 symbol = he->ms.sym;
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200251 notes = symbol__annotation(symbol);
252
253 pthread_mutex_lock(&notes->lock);
254
Jiri Olsaf681d592017-10-11 17:01:42 +0200255 symbol__calc_percent(symbol, evsel);
256
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200257 if (notes->src == NULL)
258 goto out_unlock;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200259
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300260 printf("Showing %s for %s\n", evsel__name(top->sym_evsel), symbol->name);
Arnaldo Carvalho de Melo982d4102018-05-25 17:28:37 -0300261 printf(" Events Pcnt (>=%d%%)\n", top->annotation_opts.min_pcnt);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200262
Arnaldo Carvalho de Melo29754892019-11-04 11:10:00 -0300263 more = symbol__annotate_printf(&he->ms, top->sym_evsel, &top->annotation_opts);
Arnaldo Carvalho de Melo5d484f92015-06-17 16:50:52 -0300264
265 if (top->evlist->enabled) {
266 if (top->zero)
Jiri Olsa38fe0e02021-07-06 17:16:59 +0200267 symbol__annotate_zero_histogram(symbol, top->sym_evsel->core.idx);
Arnaldo Carvalho de Melo5d484f92015-06-17 16:50:52 -0300268 else
Jiri Olsa38fe0e02021-07-06 17:16:59 +0200269 symbol__annotate_decay_histogram(symbol, top->sym_evsel->core.idx);
Arnaldo Carvalho de Melo5d484f92015-06-17 16:50:52 -0300270 }
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200271 if (more != 0)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200272 printf("%d lines not displayed, maybe increase display entries [e]\n", more);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200273out_unlock:
274 pthread_mutex_unlock(&notes->lock);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200275}
Ingo Molnar07800602009-04-20 15:00:56 +0200276
Namhyung Kimea4385f2019-08-28 08:15:54 +0900277static void perf_top__resort_hists(struct perf_top *t)
Arnaldo Carvalho de Melo40d81772019-08-09 16:44:34 -0300278{
Namhyung Kimea4385f2019-08-28 08:15:54 +0900279 struct evlist *evlist = t->evlist;
Arnaldo Carvalho de Melo40d81772019-08-09 16:44:34 -0300280 struct evsel *pos;
281
282 evlist__for_each_entry(evlist, pos) {
283 struct hists *hists = evsel__hists(pos);
284
Namhyung Kimbe5863b2019-08-28 08:15:55 +0900285 /*
286 * unlink existing entries so that they can be linked
287 * in a correct order in hists__match() below.
288 */
289 hists__unlink(hists);
290
Namhyung Kimea4385f2019-08-28 08:15:54 +0900291 if (evlist->enabled) {
292 if (t->zero) {
293 hists__delete_entries(hists);
294 } else {
295 hists__decay_entries(hists, t->hide_user_symbols,
296 t->hide_kernel_symbols);
297 }
298 }
299
Arnaldo Carvalho de Melo40d81772019-08-09 16:44:34 -0300300 hists__collapse_resort(hists, NULL);
301
302 /* Non-group events are considered as leader */
Arnaldo Carvalho de Meloc754c382020-04-30 10:51:16 -0300303 if (symbol_conf.event_group && !evsel__is_group_leader(pos)) {
Jiri Olsafba7c862021-07-06 17:17:00 +0200304 struct hists *leader_hists = evsel__hists(evsel__leader(pos));
Arnaldo Carvalho de Melo40d81772019-08-09 16:44:34 -0300305
306 hists__match(leader_hists, hists);
307 hists__link(leader_hists, hists);
308 }
309 }
310
311 evlist__for_each_entry(evlist, pos) {
Arnaldo Carvalho de Melo10c513f2020-05-06 12:58:55 -0300312 evsel__output_resort(pos, NULL);
Arnaldo Carvalho de Melo40d81772019-08-09 16:44:34 -0300313 }
314}
315
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200316static void perf_top__print_sym_table(struct perf_top *top)
Ingo Molnar07800602009-04-20 15:00:56 +0200317{
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200318 char bf[160];
319 int printed = 0;
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200320 const int win_width = top->winsize.ws_col - 1;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200321 struct evsel *evsel = top->sym_evsel;
Jiri Olsa452ce032016-01-18 10:24:00 +0100322 struct hists *hists = evsel__hists(evsel);
Mike Galbraithd94b9432009-05-25 09:57:56 +0200323
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200324 puts(CONSOLE_CLEAR);
Ingo Molnar07800602009-04-20 15:00:56 +0200325
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200326 perf_top__header_snprintf(top, bf, sizeof(bf));
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -0200327 printf("%s\n", bf);
Ingo Molnar07800602009-04-20 15:00:56 +0200328
Arnaldo Carvalho de Melo1a105f72009-11-17 15:40:55 -0200329 printf("%-*.*s\n", win_width, win_width, graph_dotted_line);
Ingo Molnar07800602009-04-20 15:00:56 +0200330
Kan Lianga1ff5b02018-01-18 13:26:30 -0800331 if (!top->record_opts.overwrite &&
Namhyung Kimbf8f8582021-04-26 18:37:12 -0700332 (top->evlist->stats.nr_lost_warned !=
333 top->evlist->stats.nr_events[PERF_RECORD_LOST])) {
334 top->evlist->stats.nr_lost_warned =
335 top->evlist->stats.nr_events[PERF_RECORD_LOST];
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200336 color_fprintf(stdout, PERF_COLOR_RED,
337 "WARNING: LOST %d chunks, Check IO/CPU overload",
Namhyung Kimbf8f8582021-04-26 18:37:12 -0700338 top->evlist->stats.nr_lost_warned);
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300339 ++printed;
Arnaldo Carvalho de Melo93fc64f2011-01-29 12:08:00 -0200340 }
341
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200342 if (top->sym_filter_entry) {
343 perf_top__show_details(top);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200344 return;
345 }
346
Namhyung Kimea4385f2019-08-28 08:15:54 +0900347 perf_top__resort_hists(top);
Namhyung Kim701937b2014-08-12 17:16:05 +0900348
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300349 hists__output_recalc_col_len(hists, top->print_entries - printed);
Arnaldo Carvalho de Melo7cc017e2009-11-24 12:05:14 -0200350 putchar('\n');
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300351 hists__fprintf(hists, false, top->print_entries - printed, win_width,
Arnaldo Carvalho de Meloe9de7e22018-06-20 15:58:20 -0300352 top->min_percent, stdout, !symbol_conf.use_callchain);
Ingo Molnar07800602009-04-20 15:00:56 +0200353}
354
Mike Galbraith923c42c2009-07-22 20:36:03 +0200355static void prompt_integer(int *target, const char *msg)
356{
357 char *buf = malloc(0), *p;
358 size_t dummy = 0;
359 int tmp;
360
361 fprintf(stdout, "\n%s: ", msg);
362 if (getline(&buf, &dummy, stdin) < 0)
363 return;
364
365 p = strchr(buf, '\n');
366 if (p)
367 *p = 0;
368
369 p = buf;
370 while(*p) {
371 if (!isdigit(*p))
372 goto out_free;
373 p++;
374 }
375 tmp = strtoul(buf, NULL, 10);
376 *target = tmp;
377out_free:
378 free(buf);
379}
380
381static void prompt_percent(int *target, const char *msg)
382{
383 int tmp = 0;
384
385 prompt_integer(&tmp, msg);
386 if (tmp >= 0 && tmp <= 100)
387 *target = tmp;
388}
389
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200390static void perf_top__prompt_symbol(struct perf_top *top, const char *msg)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200391{
392 char *buf = malloc(0), *p;
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200393 struct hist_entry *syme = top->sym_filter_entry, *n, *found = NULL;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300394 struct hists *hists = evsel__hists(top->sym_evsel);
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300395 struct rb_node *next;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200396 size_t dummy = 0;
397
398 /* zero counters of active symbol */
399 if (syme) {
Mike Galbraith923c42c2009-07-22 20:36:03 +0200400 __zero_source_counters(syme);
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200401 top->sym_filter_entry = NULL;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200402 }
403
404 fprintf(stdout, "\n%s: ", msg);
405 if (getline(&buf, &dummy, stdin) < 0)
406 goto out_free;
407
408 p = strchr(buf, '\n');
409 if (p)
410 *p = 0;
411
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800412 next = rb_first_cached(&hists->entries);
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300413 while (next) {
414 n = rb_entry(next, struct hist_entry, rb_node);
415 if (n->ms.sym && !strcmp(buf, n->ms.sym->name)) {
416 found = n;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200417 break;
418 }
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300419 next = rb_next(&n->rb_node);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200420 }
421
422 if (!found) {
Kirill Smelkov66aeb6d2010-01-13 13:22:19 -0200423 fprintf(stderr, "Sorry, %s is not active.\n", buf);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200424 sleep(1);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200425 } else
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200426 perf_top__parse_source(top, found);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200427
428out_free:
429 free(buf);
430}
431
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200432static void perf_top__print_mapped_keys(struct perf_top *top)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200433{
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200434 char *name = NULL;
435
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200436 if (top->sym_filter_entry) {
437 struct symbol *sym = top->sym_filter_entry->ms.sym;
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200438 name = sym->name;
439 }
440
441 fprintf(stdout, "\nMapped keys:\n");
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200442 fprintf(stdout, "\t[d] display refresh delay. \t(%d)\n", top->delay_secs);
443 fprintf(stdout, "\t[e] display entries (lines). \t(%d)\n", top->print_entries);
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200444
Jiri Olsa6484d2f2019-07-21 13:24:28 +0200445 if (top->evlist->core.nr_entries > 1)
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300446 fprintf(stdout, "\t[E] active event counter. \t(%s)\n", evsel__name(top->sym_evsel));
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200447
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200448 fprintf(stdout, "\t[f] profile display filter (count). \t(%d)\n", top->count_filter);
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200449
Arnaldo Carvalho de Melo982d4102018-05-25 17:28:37 -0300450 fprintf(stdout, "\t[F] annotate display filter (percent). \t(%d%%)\n", top->annotation_opts.min_pcnt);
Kirill Smelkov6cff0e82010-02-03 16:52:08 -0200451 fprintf(stdout, "\t[s] annotate symbol. \t(%s)\n", name?: "NULL");
452 fprintf(stdout, "\t[S] stop annotation.\n");
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200453
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200454 fprintf(stdout,
Sihyeon Jang8fce3742017-11-12 10:10:47 +0900455 "\t[K] hide kernel symbols. \t(%s)\n",
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200456 top->hide_kernel_symbols ? "yes" : "no");
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200457 fprintf(stdout,
458 "\t[U] hide user symbols. \t(%s)\n",
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200459 top->hide_user_symbols ? "yes" : "no");
460 fprintf(stdout, "\t[z] toggle sample zeroing. \t(%d)\n", top->zero ? 1 : 0);
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200461 fprintf(stdout, "\t[qQ] quit.\n");
462}
463
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200464static int perf_top__key_mapped(struct perf_top *top, int c)
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200465{
466 switch (c) {
467 case 'd':
468 case 'e':
469 case 'f':
470 case 'z':
471 case 'q':
472 case 'Q':
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200473 case 'K':
474 case 'U':
Kirill Smelkov6cff0e82010-02-03 16:52:08 -0200475 case 'F':
476 case 's':
477 case 'S':
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200478 return 1;
479 case 'E':
Jiri Olsa6484d2f2019-07-21 13:24:28 +0200480 return top->evlist->core.nr_entries > 1 ? 1 : 0;
Ingo Molnar83a09442009-08-15 12:26:57 +0200481 default:
482 break;
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200483 }
484
485 return 0;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200486}
487
Arnaldo Carvalho de Melo11859e82013-01-30 13:25:53 -0300488static bool perf_top__handle_keypress(struct perf_top *top, int c)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200489{
Arnaldo Carvalho de Melo11859e82013-01-30 13:25:53 -0300490 bool ret = true;
491
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200492 if (!perf_top__key_mapped(top, c)) {
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200493 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
Jiri Olsa3969cc092014-08-15 16:24:45 -0300494 struct termios save;
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200495
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200496 perf_top__print_mapped_keys(top);
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200497 fprintf(stdout, "\nEnter selection, or unmapped key to continue: ");
498 fflush(stdout);
499
Jiri Olsa3969cc092014-08-15 16:24:45 -0300500 set_term_quiet_input(&save);
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200501
502 poll(&stdin_poll, 1, -1);
503 c = getc(stdin);
504
505 tcsetattr(0, TCSAFLUSH, &save);
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200506 if (!perf_top__key_mapped(top, c))
Arnaldo Carvalho de Melo11859e82013-01-30 13:25:53 -0300507 return ret;
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200508 }
509
Mike Galbraith923c42c2009-07-22 20:36:03 +0200510 switch (c) {
511 case 'd':
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200512 prompt_integer(&top->delay_secs, "Enter display delay");
513 if (top->delay_secs < 1)
514 top->delay_secs = 1;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200515 break;
516 case 'e':
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200517 prompt_integer(&top->print_entries, "Enter display entries (lines)");
518 if (top->print_entries == 0) {
Jiri Olsab135e5e2017-11-14 10:23:39 +0100519 perf_top__resize(top);
Jiri Olsa244a1082017-11-15 14:30:57 +0100520 signal(SIGWINCH, winch_sig);
Stephane Eranian509605d2012-01-30 11:50:37 +0100521 } else {
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -0200522 signal(SIGWINCH, SIG_DFL);
Stephane Eranian509605d2012-01-30 11:50:37 +0100523 }
Mike Galbraith923c42c2009-07-22 20:36:03 +0200524 break;
525 case 'E':
Jiri Olsa6484d2f2019-07-21 13:24:28 +0200526 if (top->evlist->core.nr_entries > 1) {
Akihiro Nagaice2d17c2011-03-23 16:29:39 +0900527 /* Select 0 as the default event: */
528 int counter = 0;
529
Mike Galbraith923c42c2009-07-22 20:36:03 +0200530 fprintf(stderr, "\nAvailable events:");
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200531
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300532 evlist__for_each_entry(top->evlist, top->sym_evsel)
Jiri Olsa38fe0e02021-07-06 17:16:59 +0200533 fprintf(stderr, "\n\t%d %s", top->sym_evsel->core.idx, evsel__name(top->sym_evsel));
Mike Galbraith923c42c2009-07-22 20:36:03 +0200534
Arnaldo Carvalho de Meloec52d972011-03-11 10:11:48 -0300535 prompt_integer(&counter, "Enter details event counter");
Mike Galbraith923c42c2009-07-22 20:36:03 +0200536
Jiri Olsa6484d2f2019-07-21 13:24:28 +0200537 if (counter >= top->evlist->core.nr_entries) {
Jiri Olsa515dbe42019-09-03 10:39:52 +0200538 top->sym_evsel = evlist__first(top->evlist);
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300539 fprintf(stderr, "Sorry, no such event, using %s.\n", evsel__name(top->sym_evsel));
Mike Galbraith923c42c2009-07-22 20:36:03 +0200540 sleep(1);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200541 break;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200542 }
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300543 evlist__for_each_entry(top->evlist, top->sym_evsel)
Jiri Olsa38fe0e02021-07-06 17:16:59 +0200544 if (top->sym_evsel->core.idx == counter)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200545 break;
Arnaldo Carvalho de Meloec52d972011-03-11 10:11:48 -0300546 } else
Jiri Olsa515dbe42019-09-03 10:39:52 +0200547 top->sym_evsel = evlist__first(top->evlist);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200548 break;
549 case 'f':
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200550 prompt_integer(&top->count_filter, "Enter display event count filter");
Mike Galbraith923c42c2009-07-22 20:36:03 +0200551 break;
552 case 'F':
Arnaldo Carvalho de Melo982d4102018-05-25 17:28:37 -0300553 prompt_percent(&top->annotation_opts.min_pcnt,
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200554 "Enter details display event filter (percent)");
Mike Galbraith923c42c2009-07-22 20:36:03 +0200555 break;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200556 case 'K':
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200557 top->hide_kernel_symbols = !top->hide_kernel_symbols;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200558 break;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200559 case 'q':
560 case 'Q':
561 printf("exiting.\n");
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200562 if (top->dump_symtab)
563 perf_session__fprintf_dsos(top->session, stderr);
Arnaldo Carvalho de Melo11859e82013-01-30 13:25:53 -0300564 ret = false;
565 break;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200566 case 's':
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200567 perf_top__prompt_symbol(top, "Enter details symbol");
Mike Galbraith923c42c2009-07-22 20:36:03 +0200568 break;
569 case 'S':
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200570 if (!top->sym_filter_entry)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200571 break;
572 else {
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200573 struct hist_entry *syme = top->sym_filter_entry;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200574
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200575 top->sym_filter_entry = NULL;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200576 __zero_source_counters(syme);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200577 }
578 break;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200579 case 'U':
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200580 top->hide_user_symbols = !top->hide_user_symbols;
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -0200581 break;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200582 case 'z':
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200583 top->zero = !top->zero;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200584 break;
Ingo Molnar83a09442009-08-15 12:26:57 +0200585 default:
586 break;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200587 }
Arnaldo Carvalho de Melo11859e82013-01-30 13:25:53 -0300588
589 return ret;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200590}
591
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300592static void perf_top__sort_new_samples(void *arg)
593{
594 struct perf_top *t = arg;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300595
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300596 if (t->evlist->selected != NULL)
597 t->sym_evsel = t->evlist->selected;
598
Namhyung Kimea4385f2019-08-28 08:15:54 +0900599 perf_top__resort_hists(t);
Jiri Olsa254de742018-11-05 21:34:47 +0100600
Jiri Olsad8590432018-11-19 11:12:01 +0100601 if (t->lost || t->drop)
Jiri Olsa254de742018-11-05 21:34:47 +0100602 pr_warning("Too slow to read ring buffer (change period (-c/-F) or limit CPUs (-C)\n");
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300603}
604
Jiri Olsac94cef42018-11-07 20:11:19 +0100605static void stop_top(void)
606{
607 session_done = 1;
608 done = 1;
609}
610
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200611static void *display_thread_tui(void *arg)
Arnaldo Carvalho de Meloc0443df2011-01-31 18:19:33 -0200612{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200613 struct evsel *pos;
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200614 struct perf_top *top = arg;
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300615 const char *help = "For a higher level overview, try: perf top --sort comm,dso";
Namhyung Kim9783adf2012-11-02 14:50:05 +0900616 struct hist_browser_timer hbt = {
617 .timer = perf_top__sort_new_samples,
618 .arg = top,
619 .refresh = top->delay_secs,
620 };
Jin Yao2605af02020-03-25 06:07:11 +0800621 int ret;
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300622
Krister Johansen868a8322017-07-05 18:48:12 -0700623 /* In order to read symbols from other namespaces perf to needs to call
624 * setns(2). This isn't permitted if the struct_fs has multiple users.
625 * unshare(2) the fs so that we may continue to setns into namespaces
626 * that we're observing.
627 */
628 unshare(CLONE_FS);
629
Arnaldo Carvalho de Melo1205a272019-08-06 11:20:42 -0300630 prctl(PR_SET_NAME, "perf-top-UI", 0, 0, 0);
631
Jin Yao2605af02020-03-25 06:07:11 +0800632repeat:
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200633 perf_top__sort_new_samples(top);
Arnaldo Carvalho de Melo0d37aa32012-01-19 14:08:15 -0200634
635 /*
636 * Initialize the uid_filter_str, in the future the TUI will allow
Ingo Molnaradba1632018-12-03 11:22:00 +0100637 * Zooming in/out UIDs. For now just use whatever the user passed
Arnaldo Carvalho de Melo0d37aa32012-01-19 14:08:15 -0200638 * via --uid.
639 */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300640 evlist__for_each_entry(top->evlist, pos) {
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300641 struct hists *hists = evsel__hists(pos);
642 hists->uid_filter_str = top->record_opts.target.uid_str;
643 }
Arnaldo Carvalho de Melo0d37aa32012-01-19 14:08:15 -0200644
Arnaldo Carvalho de Melof4bd0b42020-11-30 14:23:35 -0300645 ret = evlist__tui_browse_hists(top->evlist, help, &hbt, top->min_percent,
646 &top->session->header.env, !top->record_opts.overwrite,
647 &top->annotation_opts);
Jin Yao2605af02020-03-25 06:07:11 +0800648 if (ret == K_RELOAD) {
649 top->zero = true;
650 goto repeat;
651 } else
652 stop_top();
653
Arnaldo Carvalho de Meloc0443df2011-01-31 18:19:33 -0200654 return NULL;
655}
656
Jiri Olsa4a1a9972014-08-11 10:50:07 +0200657static void display_sig(int sig __maybe_unused)
658{
Jiri Olsac94cef42018-11-07 20:11:19 +0100659 stop_top();
Jiri Olsa4a1a9972014-08-11 10:50:07 +0200660}
661
662static void display_setup_sig(void)
663{
Arnaldo Carvalho de Melo09f4d782015-08-19 15:16:08 -0300664 signal(SIGSEGV, sighandler_dump_stack);
665 signal(SIGFPE, sighandler_dump_stack);
Jiri Olsa4a1a9972014-08-11 10:50:07 +0200666 signal(SIGINT, display_sig);
667 signal(SIGQUIT, display_sig);
668 signal(SIGTERM, display_sig);
669}
670
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200671static void *display_thread(void *arg)
Ingo Molnar07800602009-04-20 15:00:56 +0200672{
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200673 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
Jiri Olsa9398c482014-08-11 10:50:02 +0200674 struct termios save;
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200675 struct perf_top *top = arg;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200676 int delay_msecs, c;
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200677
Krister Johansen868a8322017-07-05 18:48:12 -0700678 /* In order to read symbols from other namespaces perf to needs to call
679 * setns(2). This isn't permitted if the struct_fs has multiple users.
680 * unshare(2) the fs so that we may continue to setns into namespaces
681 * that we're observing.
682 */
683 unshare(CLONE_FS);
684
Arnaldo Carvalho de Melo1205a272019-08-06 11:20:42 -0300685 prctl(PR_SET_NAME, "perf-top-UI", 0, 0, 0);
686
Jiri Olsa4a1a9972014-08-11 10:50:07 +0200687 display_setup_sig();
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300688 pthread__unblock_sigwinch();
Mike Galbraith923c42c2009-07-22 20:36:03 +0200689repeat:
Arnaldo Carvalho de Melob9c4b0f2016-08-08 15:37:58 -0300690 delay_msecs = top->delay_secs * MSEC_PER_SEC;
Jiri Olsa9398c482014-08-11 10:50:02 +0200691 set_term_quiet_input(&save);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200692 /* trash return*/
Tommi Rantala29b4f5f2020-03-05 10:37:12 +0200693 clearerr(stdin);
694 if (poll(&stdin_poll, 1, 0) > 0)
695 getc(stdin);
Ingo Molnar07800602009-04-20 15:00:56 +0200696
Arnaldo Carvalho de Melo11859e82013-01-30 13:25:53 -0300697 while (!done) {
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200698 perf_top__print_sym_table(top);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300699 /*
700 * Either timeout expired or we got an EINTR due to SIGWINCH,
701 * refresh screen in both cases.
702 */
703 switch (poll(&stdin_poll, 1, delay_msecs)) {
704 case 0:
705 continue;
706 case -1:
707 if (errno == EINTR)
708 continue;
Arnaldo Carvalho de Melo7b0214b2017-02-08 17:01:46 -0300709 __fallthrough;
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300710 default:
Arnaldo Carvalho de Melo11859e82013-01-30 13:25:53 -0300711 c = getc(stdin);
712 tcsetattr(0, TCSAFLUSH, &save);
713
714 if (perf_top__handle_keypress(top, c))
715 goto repeat;
Jiri Olsac94cef42018-11-07 20:11:19 +0100716 stop_top();
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300717 }
718 }
Ingo Molnar07800602009-04-20 15:00:56 +0200719
Jiri Olsa4a1a9972014-08-11 10:50:07 +0200720 tcsetattr(0, TCSAFLUSH, &save);
Ingo Molnar07800602009-04-20 15:00:56 +0200721 return NULL;
722}
723
Namhyung Kim7c503912014-01-07 17:41:03 +0900724static int hist_iter__top_callback(struct hist_entry_iter *iter,
725 struct addr_location *al, bool single,
726 void *arg)
727{
728 struct perf_top *top = arg;
729 struct hist_entry *he = iter->he;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200730 struct evsel *evsel = iter->evsel;
Namhyung Kim7c503912014-01-07 17:41:03 +0900731
Jiri Olsa2e0453a2016-05-03 13:54:44 +0200732 if (perf_hpp_list.sym && single)
Arnaldo Carvalho de Meloe345f3b2018-05-24 12:05:39 -0300733 perf_top__record_precise_ip(top, he, iter->sample, evsel, al->addr);
Namhyung Kim7c503912014-01-07 17:41:03 +0900734
Andi Kleena18b027e2015-07-18 08:24:52 -0700735 hist__account_cycles(iter->sample->branch_stack, al, iter->sample,
Jin Yao7841f402019-11-07 15:47:15 +0800736 !(top->record_opts.branch_stack & PERF_SAMPLE_BRANCH_ANY),
737 NULL);
Namhyung Kim7c503912014-01-07 17:41:03 +0900738 return 0;
739}
740
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200741static void perf_event__process_sample(struct perf_tool *tool,
742 const union perf_event *event,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200743 struct evsel *evsel,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200744 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200745 struct machine *machine)
Ingo Molnar07800602009-04-20 15:00:56 +0200746{
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200747 struct perf_top *top = container_of(tool, struct perf_top, tool);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200748 struct addr_location al;
Arnaldo Carvalho de Melo19d4ac32011-10-05 19:30:22 -0300749 int err;
Ingo Molnar07800602009-04-20 15:00:56 +0200750
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300751 if (!machine && perf_guest) {
David Ahern6b118e92012-07-30 22:31:35 -0600752 static struct intlist *seen;
753
754 if (!seen)
Arnaldo Carvalho de Meloffe0fb72013-01-24 16:17:27 -0300755 seen = intlist__new(NULL);
David Ahern6b118e92012-07-30 22:31:35 -0600756
Adrian Hunteref893252013-08-27 11:23:06 +0300757 if (!intlist__has_entry(seen, sample->pid)) {
David Ahern6b118e92012-07-30 22:31:35 -0600758 pr_err("Can't find guest [%d]'s kernel information\n",
Adrian Hunteref893252013-08-27 11:23:06 +0300759 sample->pid);
760 intlist__add(seen, sample->pid);
David Ahern6b118e92012-07-30 22:31:35 -0600761 }
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800762 return;
763 }
764
Joerg Roedel0c095712012-02-10 18:05:04 +0100765 if (!machine) {
Arnaldo Carvalho de Melo11859e82013-01-30 13:25:53 -0300766 pr_err("%u unprocessable samples recorded.\r",
Arnaldo Carvalho de Melo75be9892015-02-14 14:50:11 -0300767 top->session->evlist->stats.nr_unprocessable_samples++);
Joerg Roedel0c095712012-02-10 18:05:04 +0100768 return;
769 }
770
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200771 if (event->header.misc & PERF_RECORD_MISC_EXACT_IP)
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200772 top->exact_samples++;
Peter Zijlstra1676b8a2010-03-04 14:19:36 +0100773
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -0300774 if (machine__resolve(machine, &al, sample) < 0)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200775 return;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200776
Kan Liang13e0c842020-03-19 13:25:15 -0700777 if (top->stitch_lbr)
778 al.thread->lbr_stitch_enable = true;
779
Arnaldo Carvalho de Meloe77a0742016-05-17 11:58:52 -0300780 if (!machine->kptr_restrict_warned &&
Arnaldo Carvalho de Melo5f6f5582011-05-27 11:53:28 -0300781 symbol_conf.kptr_restrict &&
782 al.cpumode == PERF_RECORD_MISC_KERNEL) {
Arnaldo Carvalho de Melo78e1bc22020-11-30 15:07:49 -0300783 if (!evlist__exclude_kernel(top->session->evlist)) {
Arnaldo Carvalho de Melob89a5122017-11-14 13:30:19 -0300784 ui__warning(
Arnaldo Carvalho de Melo5f6f5582011-05-27 11:53:28 -0300785"Kernel address maps (/proc/{kallsyms,modules}) are restricted.\n\n"
Igor Lubashevd06e5fa2019-08-26 21:39:16 -0400786"Check /proc/sys/kernel/kptr_restrict and /proc/sys/kernel/perf_event_paranoid.\n\n"
Arnaldo Carvalho de Melo5f6f5582011-05-27 11:53:28 -0300787"Kernel%s samples will not be resolved.\n",
Arnaldo Carvalho de Meloe94b8612018-04-23 17:13:49 -0300788 al.map && map__has_symbols(al.map) ?
Arnaldo Carvalho de Melo5f6f5582011-05-27 11:53:28 -0300789 " modules" : "");
Arnaldo Carvalho de Melob89a5122017-11-14 13:30:19 -0300790 if (use_browser <= 0)
791 sleep(5);
792 }
Arnaldo Carvalho de Meloe77a0742016-05-17 11:58:52 -0300793 machine->kptr_restrict_warned = true;
Arnaldo Carvalho de Melo5f6f5582011-05-27 11:53:28 -0300794 }
795
Arnaldo Carvalho de Melo68766bf2018-04-23 16:40:02 -0300796 if (al.sym == NULL && al.map != NULL) {
Arnaldo Carvalho de Meloe4a338d2011-05-27 13:42:16 -0300797 const char *msg = "Kernel samples will not be resolved.\n";
Arnaldo Carvalho de Melo72b8fa12010-01-27 21:05:49 -0200798 /*
799 * As we do lazy loading of symtabs we only will know if the
800 * specified vmlinux file is invalid when we actually have a
801 * hit in kernel space and then try to load it. So if we get
802 * here and there are _no_ symbols in the DSO backing the
803 * kernel map, bail out.
804 *
805 * We may never get here, for instance, if we use -K/
806 * --hide-kernel-symbols, even if the user specifies an
807 * invalid --vmlinux ;-)
808 */
Arnaldo Carvalho de Meloe77a0742016-05-17 11:58:52 -0300809 if (!machine->kptr_restrict_warned && !top->vmlinux_warned &&
Arnaldo Carvalho de Meloe94b8612018-04-23 17:13:49 -0300810 __map__is_kernel(al.map) && map__has_symbols(al.map)) {
Arnaldo Carvalho de Meloe4a338d2011-05-27 13:42:16 -0300811 if (symbol_conf.vmlinux_name) {
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300812 char serr[256];
813 dso__strerror_load(al.map->dso, serr, sizeof(serr));
814 ui__warning("The %s file can't be used: %s\n%s",
815 symbol_conf.vmlinux_name, serr, msg);
Arnaldo Carvalho de Meloe4a338d2011-05-27 13:42:16 -0300816 } else {
817 ui__warning("A vmlinux file was not found.\n%s",
818 msg);
819 }
820
821 if (use_browser <= 0)
822 sleep(5);
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200823 top->vmlinux_warned = true;
Arnaldo Carvalho de Melo72b8fa12010-01-27 21:05:49 -0200824 }
Arnaldo Carvalho de Melo72b8fa12010-01-27 21:05:49 -0200825 }
826
Arnaldo Carvalho de Melob55cc4e2016-08-30 11:15:59 -0300827 if (al.sym == NULL || !al.sym->idle) {
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300828 struct hists *hists = evsel__hists(evsel);
Namhyung Kim7c503912014-01-07 17:41:03 +0900829 struct hist_entry_iter iter = {
Namhyung Kim063bd932015-05-19 17:04:10 +0900830 .evsel = evsel,
831 .sample = sample,
832 .add_entry_cb = hist_iter__top_callback,
Namhyung Kim7c503912014-01-07 17:41:03 +0900833 };
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200834
Namhyung Kim7c503912014-01-07 17:41:03 +0900835 if (symbol_conf.cumulate_callchain)
836 iter.ops = &hist_iter_cumulative;
837 else
838 iter.ops = &hist_iter_normal;
Arnaldo Carvalho de Melo19d4ac32011-10-05 19:30:22 -0300839
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300840 pthread_mutex_lock(&hists->lock);
Namhyung Kim7c503912014-01-07 17:41:03 +0900841
Namhyung Kim063bd932015-05-19 17:04:10 +0900842 err = hist_entry_iter__add(&iter, &al, top->max_stack, top);
Namhyung Kim7c503912014-01-07 17:41:03 +0900843 if (err < 0)
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300844 pr_err("Problem incrementing symbol period, skipping event\n");
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300845
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300846 pthread_mutex_unlock(&hists->lock);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200847 }
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300848
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300849 addr_location__put(&al);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200850}
851
Jiri Olsad24e3c92018-11-06 15:45:14 +0100852static void
853perf_top__process_lost(struct perf_top *top, union perf_event *event,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200854 struct evsel *evsel)
Jiri Olsad24e3c92018-11-06 15:45:14 +0100855{
Jiri Olsad24e3c92018-11-06 15:45:14 +0100856 top->lost += event->lost.lost;
857 top->lost_total += event->lost.lost;
Namhyung Kimbf8f8582021-04-26 18:37:12 -0700858 evsel->evlist->stats.total_lost += event->lost.lost;
Jiri Olsad24e3c92018-11-06 15:45:14 +0100859}
860
861static void
862perf_top__process_lost_samples(struct perf_top *top,
863 union perf_event *event,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200864 struct evsel *evsel)
Jiri Olsad24e3c92018-11-06 15:45:14 +0100865{
Jiri Olsad24e3c92018-11-06 15:45:14 +0100866 top->lost += event->lost_samples.lost;
867 top->lost_total += event->lost_samples.lost;
Namhyung Kimbf8f8582021-04-26 18:37:12 -0700868 evsel->evlist->stats.total_lost_samples += event->lost_samples.lost;
Jiri Olsad24e3c92018-11-06 15:45:14 +0100869}
870
Jiri Olsad63b9f62018-11-11 19:52:06 +0100871static u64 last_timestamp;
872
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200873static void perf_top__mmap_read_idx(struct perf_top *top, int idx)
Ingo Molnar07800602009-04-20 15:00:56 +0200874{
Kan Liangebebbf02018-01-18 13:26:31 -0800875 struct record_opts *opts = &top->record_opts;
Jiri Olsa63503db2019-07-21 13:23:52 +0200876 struct evlist *evlist = top->evlist;
Jiri Olsaa5830532019-07-27 20:30:53 +0200877 struct mmap *md;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200878 union perf_event *event;
Ingo Molnar07800602009-04-20 15:00:56 +0200879
Kan Liangebebbf02018-01-18 13:26:31 -0800880 md = opts->overwrite ? &evlist->overwrite_mmap[idx] : &evlist->mmap[idx];
Jiri Olsa7c4d4182019-10-07 14:53:18 +0200881 if (perf_mmap__read_init(&md->core) < 0)
Kan Liangebebbf02018-01-18 13:26:31 -0800882 return;
883
Jiri Olsa151ed5d2019-10-07 14:53:20 +0200884 while ((event = perf_mmap__read_event(&md->core)) != NULL) {
Jiri Olsa16c66bc2018-11-05 13:24:55 +0100885 int ret;
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200886
Arnaldo Carvalho de Melo2a6599c2020-11-30 09:43:07 -0300887 ret = evlist__parse_sample_timestamp(evlist, event, &last_timestamp);
Jiri Olsa16c66bc2018-11-05 13:24:55 +0100888 if (ret && ret != -1)
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200889 break;
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200890
Jiri Olsad63b9f62018-11-11 19:52:06 +0100891 ret = ordered_events__queue(top->qe.in, event, last_timestamp, 0);
Jiri Olsa16c66bc2018-11-05 13:24:55 +0100892 if (ret)
893 break;
Jiri Olsa94ad6e72018-11-05 21:23:40 +0100894
Jiri Olsa7728fa02019-10-07 14:53:17 +0200895 perf_mmap__consume(&md->core);
Jiri Olsa94ad6e72018-11-05 21:23:40 +0100896
897 if (top->qe.rotate) {
898 pthread_mutex_lock(&top->qe.mutex);
899 top->qe.rotate = false;
900 pthread_cond_signal(&top->qe.cond);
901 pthread_mutex_unlock(&top->qe.mutex);
902 }
Ingo Molnar07800602009-04-20 15:00:56 +0200903 }
Kan Liangebebbf02018-01-18 13:26:31 -0800904
Jiri Olsa32fdc2c2019-10-07 14:53:19 +0200905 perf_mmap__read_done(&md->core);
Ingo Molnar07800602009-04-20 15:00:56 +0200906}
907
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200908static void perf_top__mmap_read(struct perf_top *top)
Frederic Weisbecker2f011902009-06-06 23:10:43 +0200909{
Kan Liangebebbf02018-01-18 13:26:31 -0800910 bool overwrite = top->record_opts.overwrite;
Jiri Olsa63503db2019-07-21 13:23:52 +0200911 struct evlist *evlist = top->evlist;
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200912 int i;
Frederic Weisbecker2f011902009-06-06 23:10:43 +0200913
Kan Liangebebbf02018-01-18 13:26:31 -0800914 if (overwrite)
Arnaldo Carvalho de Meloade9d202020-11-30 09:33:55 -0300915 evlist__toggle_bkw_mmap(evlist, BKW_MMAP_DATA_PENDING);
Kan Liangebebbf02018-01-18 13:26:31 -0800916
Jiri Olsac976ee12019-07-30 13:04:59 +0200917 for (i = 0; i < top->evlist->core.nr_mmaps; i++)
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -0200918 perf_top__mmap_read_idx(top, i);
Kan Liangebebbf02018-01-18 13:26:31 -0800919
920 if (overwrite) {
Arnaldo Carvalho de Meloade9d202020-11-30 09:33:55 -0300921 evlist__toggle_bkw_mmap(evlist, BKW_MMAP_EMPTY);
922 evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING);
Kan Liangebebbf02018-01-18 13:26:31 -0800923 }
Frederic Weisbecker2f011902009-06-06 23:10:43 +0200924}
925
Kan Liang63878a52018-01-18 13:26:26 -0800926/*
927 * Check per-event overwrite term.
928 * perf top should support consistent term for all events.
929 * - All events don't have per-event term
930 * E.g. "cpu/cpu-cycles/,cpu/instructions/"
931 * Nothing change, return 0.
932 * - All events have same per-event term
933 * E.g. "cpu/cpu-cycles,no-overwrite/,cpu/instructions,no-overwrite/
934 * Using the per-event setting to replace the opts->overwrite if
935 * they are different, then return 0.
936 * - Events have different per-event term
937 * E.g. "cpu/cpu-cycles,overwrite/,cpu/instructions,no-overwrite/"
938 * Return -1
939 * - Some of the event set per-event term, but some not.
940 * E.g. "cpu/cpu-cycles/,cpu/instructions,no-overwrite/"
941 * Return -1
942 */
943static int perf_top__overwrite_check(struct perf_top *top)
944{
945 struct record_opts *opts = &top->record_opts;
Jiri Olsa63503db2019-07-21 13:23:52 +0200946 struct evlist *evlist = top->evlist;
Arnaldo Carvalho de Melo35ac0ca2020-05-06 13:05:08 -0300947 struct evsel_config_term *term;
Kan Liang63878a52018-01-18 13:26:26 -0800948 struct list_head *config_terms;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200949 struct evsel *evsel;
Kan Liang63878a52018-01-18 13:26:26 -0800950 int set, overwrite = -1;
951
952 evlist__for_each_entry(evlist, evsel) {
953 set = -1;
954 config_terms = &evsel->config_terms;
955 list_for_each_entry(term, config_terms, list) {
Arnaldo Carvalho de Melo35ac0ca2020-05-06 13:05:08 -0300956 if (term->type == EVSEL__CONFIG_TERM_OVERWRITE)
Kan Liang63878a52018-01-18 13:26:26 -0800957 set = term->val.overwrite ? 1 : 0;
958 }
959
960 /* no term for current and previous event (likely) */
961 if ((overwrite < 0) && (set < 0))
962 continue;
963
964 /* has term for both current and previous event, compare */
965 if ((overwrite >= 0) && (set >= 0) && (overwrite != set))
966 return -1;
967
968 /* no term for current event but has term for previous one */
969 if ((overwrite >= 0) && (set < 0))
970 return -1;
971
972 /* has term for current event */
973 if ((overwrite < 0) && (set >= 0)) {
974 /* if it's first event, set overwrite */
Jiri Olsa515dbe42019-09-03 10:39:52 +0200975 if (evsel == evlist__first(evlist))
Kan Liang63878a52018-01-18 13:26:26 -0800976 overwrite = set;
977 else
978 return -1;
979 }
980 }
981
982 if ((overwrite >= 0) && (opts->overwrite != overwrite))
983 opts->overwrite = overwrite;
984
985 return 0;
986}
987
Kan Liang204721d2018-01-18 13:26:28 -0800988static int perf_top_overwrite_fallback(struct perf_top *top,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200989 struct evsel *evsel)
Kan Liang204721d2018-01-18 13:26:28 -0800990{
991 struct record_opts *opts = &top->record_opts;
Jiri Olsa63503db2019-07-21 13:23:52 +0200992 struct evlist *evlist = top->evlist;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200993 struct evsel *counter;
Kan Liang204721d2018-01-18 13:26:28 -0800994
995 if (!opts->overwrite)
996 return 0;
997
998 /* only fall back when first event fails */
Jiri Olsa515dbe42019-09-03 10:39:52 +0200999 if (evsel != evlist__first(evlist))
Kan Liang204721d2018-01-18 13:26:28 -08001000 return 0;
1001
1002 evlist__for_each_entry(evlist, counter)
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001003 counter->core.attr.write_backward = false;
Kan Liang204721d2018-01-18 13:26:28 -08001004 opts->overwrite = false;
Kan Liang853745f2018-02-26 10:17:10 -08001005 pr_debug2("fall back to non-overwrite mode\n");
Kan Liang204721d2018-01-18 13:26:28 -08001006 return 1;
1007}
1008
Arnaldo Carvalho de Melo11859e82013-01-30 13:25:53 -03001009static int perf_top__start_counters(struct perf_top *top)
Arnaldo Carvalho de Melo72cb7012011-01-12 10:52:47 -02001010{
Arnaldo Carvalho de Melod6195a62017-02-13 16:45:24 -03001011 char msg[BUFSIZ];
Jiri Olsa32dcd022019-07-21 13:23:51 +02001012 struct evsel *counter;
Jiri Olsa63503db2019-07-21 13:23:52 +02001013 struct evlist *evlist = top->evlist;
Arnaldo Carvalho de Melob4006792013-12-19 14:43:45 -03001014 struct record_opts *opts = &top->record_opts;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001015
Kan Liang63878a52018-01-18 13:26:26 -08001016 if (perf_top__overwrite_check(top)) {
1017 ui__error("perf top only support consistent per-event "
1018 "overwrite setting for all events\n");
1019 goto out_err;
1020 }
1021
Arnaldo Carvalho de Melo78e1bc22020-11-30 15:07:49 -03001022 evlist__config(evlist, opts, &callchain_param);
Arnaldo Carvalho de Melo72cb7012011-01-12 10:52:47 -02001023
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001024 evlist__for_each_entry(evlist, counter) {
Arnaldo Carvalho de Melo72cb7012011-01-12 10:52:47 -02001025try_again:
Jiri Olsaf72f9012019-07-21 13:24:41 +02001026 if (evsel__open(counter, top->evlist->core.cpus,
Jiri Olsa03617c22019-07-21 13:24:42 +02001027 top->evlist->core.threads) < 0) {
Kan Liang204721d2018-01-18 13:26:28 -08001028
1029 /*
1030 * Specially handle overwrite fall back.
1031 * Because perf top is the only tool which has
1032 * overwrite mode by default, support
1033 * both overwrite and non-overwrite mode, and
1034 * require consistent mode for all events.
1035 *
1036 * May move it to generic code with more tools
1037 * have similar attribute.
1038 */
1039 if (perf_missing_features.write_backward &&
1040 perf_top_overwrite_fallback(top, counter))
1041 goto try_again;
1042
Arnaldo Carvalho de Meloae430892020-04-30 11:46:15 -03001043 if (evsel__fallback(counter, errno, msg, sizeof(msg))) {
Namhyung Kimbb963e12017-02-17 17:17:38 +09001044 if (verbose > 0)
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03001045 ui__warning("%s\n", msg);
Arnaldo Carvalho de Melo72cb7012011-01-12 10:52:47 -02001046 goto try_again;
1047 }
Arnaldo Carvalho de Meloc286c412011-03-28 09:50:11 -03001048
Arnaldo Carvalho de Melo2bb72db2020-05-04 13:43:03 -03001049 evsel__open_strerror(counter, &opts->target, errno, msg, sizeof(msg));
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03001050 ui__error("%s\n", msg);
Arnaldo Carvalho de Meloc286c412011-03-28 09:50:11 -03001051 goto out_err;
Arnaldo Carvalho de Melo72cb7012011-01-12 10:52:47 -02001052 }
Arnaldo Carvalho de Melo72cb7012011-01-12 10:52:47 -02001053 }
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -02001054
Jiri Olsa9521b5f2019-07-28 12:45:35 +02001055 if (evlist__mmap(evlist, opts->mmap_pages) < 0) {
Namhyung Kim3780f482012-05-29 13:22:57 +09001056 ui__error("Failed to mmap with %d (%s)\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03001057 errno, str_error_r(errno, msg, sizeof(msg)));
Arnaldo Carvalho de Meloc286c412011-03-28 09:50:11 -03001058 goto out_err;
1059 }
1060
Arnaldo Carvalho de Melo11859e82013-01-30 13:25:53 -03001061 return 0;
Arnaldo Carvalho de Meloc286c412011-03-28 09:50:11 -03001062
1063out_err:
Arnaldo Carvalho de Melo11859e82013-01-30 13:25:53 -03001064 return -1;
Arnaldo Carvalho de Melo72cb7012011-01-12 10:52:47 -02001065}
1066
Arnaldo Carvalho de Meloe3815262016-04-18 12:30:16 -03001067static int callchain_param__setup_sample_type(struct callchain_param *callchain)
Arnaldo Carvalho de Melo19d4ac32011-10-05 19:30:22 -03001068{
Arnaldo Carvalho de Melof2e14cd2019-01-08 10:56:59 -03001069 if (callchain->mode != CHAIN_NONE) {
Arnaldo Carvalho de Meloe3815262016-04-18 12:30:16 -03001070 if (callchain_register_param(callchain) < 0) {
Namhyung Kim3780f482012-05-29 13:22:57 +09001071 ui__error("Can't register callchain params.\n");
Arnaldo Carvalho de Melo19d4ac32011-10-05 19:30:22 -03001072 return -EINVAL;
1073 }
1074 }
1075
1076 return 0;
1077}
1078
Jiri Olsa16c66bc2018-11-05 13:24:55 +01001079static struct ordered_events *rotate_queues(struct perf_top *top)
1080{
1081 struct ordered_events *in = top->qe.in;
1082
1083 if (top->qe.in == &top->qe.data[1])
1084 top->qe.in = &top->qe.data[0];
1085 else
1086 top->qe.in = &top->qe.data[1];
1087
1088 return in;
1089}
1090
1091static void *process_thread(void *arg)
1092{
1093 struct perf_top *top = arg;
1094
1095 while (!done) {
1096 struct ordered_events *out, *in = top->qe.in;
1097
1098 if (!in->nr_events) {
1099 usleep(100);
1100 continue;
1101 }
1102
Jiri Olsa16c66bc2018-11-05 13:24:55 +01001103 out = rotate_queues(top);
Jiri Olsa94ad6e72018-11-05 21:23:40 +01001104
1105 pthread_mutex_lock(&top->qe.mutex);
1106 top->qe.rotate = true;
1107 pthread_cond_wait(&top->qe.cond, &top->qe.mutex);
1108 pthread_mutex_unlock(&top->qe.mutex);
Jiri Olsa16c66bc2018-11-05 13:24:55 +01001109
1110 if (ordered_events__flush(out, OE_FLUSH__TOP))
1111 pr_err("failed to process events\n");
1112 }
1113
1114 return NULL;
1115}
1116
Jiri Olsad63b9f62018-11-11 19:52:06 +01001117/*
1118 * Allow only 'top->delay_secs' seconds behind samples.
1119 */
1120static int should_drop(struct ordered_event *qevent, struct perf_top *top)
1121{
1122 union perf_event *event = qevent->event;
1123 u64 delay_timestamp;
1124
1125 if (event->header.type != PERF_RECORD_SAMPLE)
1126 return false;
1127
1128 delay_timestamp = qevent->timestamp + top->delay_secs * NSEC_PER_SEC;
1129 return delay_timestamp < last_timestamp;
1130}
1131
Jiri Olsa16c66bc2018-11-05 13:24:55 +01001132static int deliver_event(struct ordered_events *qe,
1133 struct ordered_event *qevent)
1134{
1135 struct perf_top *top = qe->data;
Jiri Olsa63503db2019-07-21 13:23:52 +02001136 struct evlist *evlist = top->evlist;
Jiri Olsa16c66bc2018-11-05 13:24:55 +01001137 struct perf_session *session = top->session;
1138 union perf_event *event = qevent->event;
1139 struct perf_sample sample;
Jiri Olsa32dcd022019-07-21 13:23:51 +02001140 struct evsel *evsel;
Jiri Olsa16c66bc2018-11-05 13:24:55 +01001141 struct machine *machine;
1142 int ret = -1;
1143
Jiri Olsa97f7e0b2018-11-11 20:02:46 +01001144 if (should_drop(qevent, top)) {
1145 top->drop++;
1146 top->drop_total++;
Jiri Olsad63b9f62018-11-11 19:52:06 +01001147 return 0;
Jiri Olsa97f7e0b2018-11-11 20:02:46 +01001148 }
Jiri Olsad63b9f62018-11-11 19:52:06 +01001149
Arnaldo Carvalho de Melo2a6599c2020-11-30 09:43:07 -03001150 ret = evlist__parse_sample(evlist, event, &sample);
Jiri Olsa16c66bc2018-11-05 13:24:55 +01001151 if (ret) {
1152 pr_err("Can't parse sample, err = %d\n", ret);
1153 goto next_event;
1154 }
1155
Arnaldo Carvalho de Melo3ccf8a72020-11-30 14:17:57 -03001156 evsel = evlist__id2evsel(session->evlist, sample.id);
Jiri Olsa16c66bc2018-11-05 13:24:55 +01001157 assert(evsel != NULL);
1158
Arnaldo Carvalho de Melo2f53ae32019-08-15 16:03:26 -03001159 if (event->header.type == PERF_RECORD_SAMPLE) {
1160 if (evswitch__discard(&top->evswitch, evsel))
1161 return 0;
Jiri Olsa16c66bc2018-11-05 13:24:55 +01001162 ++top->samples;
Arnaldo Carvalho de Melo2f53ae32019-08-15 16:03:26 -03001163 }
Jiri Olsa16c66bc2018-11-05 13:24:55 +01001164
1165 switch (sample.cpumode) {
1166 case PERF_RECORD_MISC_USER:
1167 ++top->us_samples;
1168 if (top->hide_user_symbols)
1169 goto next_event;
1170 machine = &session->machines.host;
1171 break;
1172 case PERF_RECORD_MISC_KERNEL:
1173 ++top->kernel_samples;
1174 if (top->hide_kernel_symbols)
1175 goto next_event;
1176 machine = &session->machines.host;
1177 break;
1178 case PERF_RECORD_MISC_GUEST_KERNEL:
1179 ++top->guest_kernel_samples;
1180 machine = perf_session__find_machine(session,
1181 sample.pid);
1182 break;
1183 case PERF_RECORD_MISC_GUEST_USER:
1184 ++top->guest_us_samples;
1185 /*
1186 * TODO: we don't process guest user from host side
1187 * except simple counting.
1188 */
1189 goto next_event;
1190 default:
1191 if (event->header.type == PERF_RECORD_SAMPLE)
1192 goto next_event;
1193 machine = &session->machines.host;
1194 break;
1195 }
1196
1197 if (event->header.type == PERF_RECORD_SAMPLE) {
1198 perf_event__process_sample(&top->tool, event, evsel,
1199 &sample, machine);
1200 } else if (event->header.type == PERF_RECORD_LOST) {
1201 perf_top__process_lost(top, event, evsel);
1202 } else if (event->header.type == PERF_RECORD_LOST_SAMPLES) {
1203 perf_top__process_lost_samples(top, event, evsel);
1204 } else if (event->header.type < PERF_RECORD_MAX) {
Namhyung Kimbf8f8582021-04-26 18:37:12 -07001205 events_stats__inc(&session->evlist->stats, event->header.type);
Jiri Olsa16c66bc2018-11-05 13:24:55 +01001206 machine__process_event(machine, event, &sample);
1207 } else
1208 ++session->evlist->stats.nr_unknown_events;
1209
1210 ret = 0;
1211next_event:
1212 return ret;
1213}
1214
1215static void init_process_thread(struct perf_top *top)
1216{
1217 ordered_events__init(&top->qe.data[0], deliver_event, top);
1218 ordered_events__init(&top->qe.data[1], deliver_event, top);
1219 ordered_events__set_copy_on_queue(&top->qe.data[0], true);
1220 ordered_events__set_copy_on_queue(&top->qe.data[1], true);
1221 top->qe.in = &top->qe.data[0];
Jiri Olsa94ad6e72018-11-05 21:23:40 +01001222 pthread_mutex_init(&top->qe.mutex, NULL);
1223 pthread_cond_init(&top->qe.cond, NULL);
Jiri Olsa16c66bc2018-11-05 13:24:55 +01001224}
1225
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -02001226static int __cmd_top(struct perf_top *top)
Ingo Molnar716c69f2009-06-07 17:31:52 +02001227{
Arnaldo Carvalho de Melob4006792013-12-19 14:43:45 -03001228 struct record_opts *opts = &top->record_opts;
Jiri Olsa16c66bc2018-11-05 13:24:55 +01001229 pthread_t thread, thread_process;
Arnaldo Carvalho de Melo19d4ac32011-10-05 19:30:22 -03001230 int ret;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001231
Arnaldo Carvalho de Melof178fd22018-05-28 14:24:45 -03001232 if (!top->annotation_opts.objdump_path) {
1233 ret = perf_env__lookup_objdump(&top->session->header.env,
1234 &top->annotation_opts.objdump_path);
Sukadev Bhattiprolu0d3942d2013-05-14 22:56:51 -07001235 if (ret)
Changbin Du0dba9e42019-03-16 16:05:47 +08001236 return ret;
Sukadev Bhattiprolu0d3942d2013-05-14 22:56:51 -07001237 }
1238
Arnaldo Carvalho de Meloe3815262016-04-18 12:30:16 -03001239 ret = callchain_param__setup_sample_type(&callchain_param);
Arnaldo Carvalho de Melo19d4ac32011-10-05 19:30:22 -03001240 if (ret)
Changbin Du0dba9e42019-03-16 16:05:47 +08001241 return ret;
Arnaldo Carvalho de Melo19d4ac32011-10-05 19:30:22 -03001242
Masami Hiramatsu9d8b1722015-12-09 11:11:23 +09001243 if (perf_session__register_idle_thread(top->session) < 0)
Changbin Du0dba9e42019-03-16 16:05:47 +08001244 return ret;
Namhyung Kimc53d1382015-09-30 10:45:26 +09001245
Kan Liang0c6b4992017-09-29 07:47:55 -07001246 if (top->nr_threads_synthesize > 1)
1247 perf_set_multithreaded();
Kan Liang340b47f2017-09-29 07:47:54 -07001248
Jiri Olsa16c66bc2018-11-05 13:24:55 +01001249 init_process_thread(top);
1250
Namhyung Kima0c0a4a2019-05-22 14:32:50 +09001251 if (opts->record_namespaces)
1252 top->tool.namespace_events = true;
Namhyung Kimf3828422020-03-25 21:45:35 +09001253 if (opts->record_cgroup) {
1254#ifdef HAVE_FILE_HANDLE
1255 top->tool.cgroup_events = true;
1256#else
1257 pr_err("cgroup tracking is not supported.\n");
1258 return -1;
1259#endif
1260 }
Namhyung Kima0c0a4a2019-05-22 14:32:50 +09001261
Song Liue5416952019-03-11 22:30:41 -07001262 ret = perf_event__synthesize_bpf_events(top->session, perf_event__process,
Arnaldo Carvalho de Meloa40b95b2019-01-17 08:15:20 -08001263 &top->session->machines.host,
1264 &top->record_opts);
1265 if (ret < 0)
Arnaldo Carvalho de Melo2d45ef72019-05-20 11:04:08 -03001266 pr_debug("Couldn't synthesize BPF events: Pre-existing BPF programs won't have symbols resolved.\n");
Arnaldo Carvalho de Meloa40b95b2019-01-17 08:15:20 -08001267
Namhyung Kimf3828422020-03-25 21:45:35 +09001268 ret = perf_event__synthesize_cgroups(&top->tool, perf_event__process,
1269 &top->session->machines.host);
1270 if (ret < 0)
1271 pr_debug("Couldn't synthesize cgroup events.\n");
1272
Arnaldo Carvalho de Meloa33fbd52013-11-11 11:36:12 -03001273 machine__synthesize_threads(&top->session->machines.host, &opts->target,
Namhyung Kim84111b92021-08-10 21:46:57 -07001274 top->evlist->core.threads, true, false,
Kan Liang0c6b4992017-09-29 07:47:55 -07001275 top->nr_threads_synthesize);
Kan Liang340b47f2017-09-29 07:47:54 -07001276
Kan Liang0c6b4992017-09-29 07:47:55 -07001277 if (top->nr_threads_synthesize > 1)
1278 perf_set_singlethreaded();
Kan Liang2e7ea3a2015-09-04 10:45:43 -04001279
Jiri Olsa35a634f2016-05-03 13:54:46 +02001280 if (perf_hpp_list.socket) {
Kan Liang2e7ea3a2015-09-04 10:45:43 -04001281 ret = perf_env__read_cpu_topology_map(&perf_env);
Changbin Du0dba9e42019-03-16 16:05:47 +08001282 if (ret < 0) {
1283 char errbuf[BUFSIZ];
1284 const char *err = str_error_r(-ret, errbuf, sizeof(errbuf));
1285
1286 ui__error("Could not read the CPU topology map: %s\n", err);
1287 return ret;
1288 }
Kan Liang2e7ea3a2015-09-04 10:45:43 -04001289 }
1290
Arnaldo Carvalho de Melo11859e82013-01-30 13:25:53 -03001291 ret = perf_top__start_counters(top);
1292 if (ret)
Changbin Du0dba9e42019-03-16 16:05:47 +08001293 return ret;
Arnaldo Carvalho de Melo11859e82013-01-30 13:25:53 -03001294
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -02001295 top->session->evlist = top->evlist;
Arnaldo Carvalho de Melo7b56cce2012-08-01 19:31:00 -03001296 perf_session__set_id_hdr_size(top->session);
Ingo Molnar07800602009-04-20 15:00:56 +02001297
Arnaldo Carvalho de Melo2376c672012-12-11 16:48:41 -03001298 /*
1299 * When perf is starting the traced process, all the events (apart from
1300 * group members) have enable_on_exec=1 set, so don't spoil it by
1301 * prematurely enabling them.
1302 *
1303 * XXX 'top' still doesn't start workloads like record, trace, but should,
1304 * so leave the check here.
1305 */
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001306 if (!target__none(&opts->target))
Jiri Olsa1c87f162019-07-21 13:24:08 +02001307 evlist__enable(top->evlist);
Arnaldo Carvalho de Melo2376c672012-12-11 16:48:41 -03001308
Arnaldo Carvalho de Melo11859e82013-01-30 13:25:53 -03001309 ret = -1;
Jiri Olsa16c66bc2018-11-05 13:24:55 +01001310 if (pthread_create(&thread_process, NULL, process_thread, top)) {
1311 ui__error("Could not create process thread.\n");
Changbin Du0dba9e42019-03-16 16:05:47 +08001312 return ret;
Jiri Olsa16c66bc2018-11-05 13:24:55 +01001313 }
1314
Arnaldo Carvalho de Meloc0443df2011-01-31 18:19:33 -02001315 if (pthread_create(&thread, NULL, (use_browser > 0 ? display_thread_tui :
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -02001316 display_thread), top)) {
Namhyung Kim3780f482012-05-29 13:22:57 +09001317 ui__error("Could not create display thread.\n");
Jiri Olsa16c66bc2018-11-05 13:24:55 +01001318 goto out_join_thread;
Ingo Molnar07800602009-04-20 15:00:56 +02001319 }
1320
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -02001321 if (top->realtime_prio) {
Ingo Molnar07800602009-04-20 15:00:56 +02001322 struct sched_param param;
1323
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -02001324 param.sched_priority = top->realtime_prio;
Ingo Molnar07800602009-04-20 15:00:56 +02001325 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
Namhyung Kim3780f482012-05-29 13:22:57 +09001326 ui__error("Could not set realtime priority.\n");
Jiri Olsaae256fa2014-08-11 10:50:05 +02001327 goto out_join;
Ingo Molnar07800602009-04-20 15:00:56 +02001328 }
1329 }
1330
David Millerff27a062018-10-30 22:30:03 -07001331 /* Wait for a minimal set of events before starting the snapshot */
Jiri Olsa80ab2982019-08-31 22:48:33 +02001332 evlist__poll(top->evlist, 100);
David Millerff27a062018-10-30 22:30:03 -07001333
1334 perf_top__mmap_read(top);
1335
Arnaldo Carvalho de Melo11859e82013-01-30 13:25:53 -03001336 while (!done) {
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -02001337 u64 hits = top->samples;
Ingo Molnar07800602009-04-20 15:00:56 +02001338
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -02001339 perf_top__mmap_read(top);
Ingo Molnar07800602009-04-20 15:00:56 +02001340
Kan Liangebebbf02018-01-18 13:26:31 -08001341 if (opts->overwrite || (hits == top->samples))
Jiri Olsa80ab2982019-08-31 22:48:33 +02001342 ret = evlist__poll(top->evlist, 100);
Jiri Olsab135e5e2017-11-14 10:23:39 +01001343
1344 if (resize) {
1345 perf_top__resize(top);
1346 resize = 0;
1347 }
Ingo Molnar07800602009-04-20 15:00:56 +02001348 }
1349
Arnaldo Carvalho de Melo11859e82013-01-30 13:25:53 -03001350 ret = 0;
Jiri Olsaae256fa2014-08-11 10:50:05 +02001351out_join:
1352 pthread_join(thread, NULL);
Jiri Olsa16c66bc2018-11-05 13:24:55 +01001353out_join_thread:
Jiri Olsa94ad6e72018-11-05 21:23:40 +01001354 pthread_cond_signal(&top->qe.cond);
Jiri Olsa16c66bc2018-11-05 13:24:55 +01001355 pthread_join(thread_process, NULL);
Arnaldo Carvalho de Melo11859e82013-01-30 13:25:53 -03001356 return ret;
Arnaldo Carvalho de Melo19d4ac32011-10-05 19:30:22 -03001357}
1358
1359static int
Jiri Olsaae779a62013-10-26 16:25:34 +02001360callchain_opt(const struct option *opt, const char *arg, int unset)
1361{
1362 symbol_conf.use_callchain = true;
1363 return record_callchain_opt(opt, arg, unset);
1364}
1365
1366static int
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -02001367parse_callchain_opt(const struct option *opt, const char *arg, int unset)
Arnaldo Carvalho de Melo19d4ac32011-10-05 19:30:22 -03001368{
Arnaldo Carvalho de Melo2ddd5c02016-04-18 12:09:08 -03001369 struct callchain_param *callchain = opt->value;
Namhyung Kima2c10d32015-10-22 15:28:49 +09001370
Arnaldo Carvalho de Melo2ddd5c02016-04-18 12:09:08 -03001371 callchain->enabled = !unset;
1372 callchain->record_mode = CALLCHAIN_FP;
Namhyung Kima2c10d32015-10-22 15:28:49 +09001373
1374 /*
1375 * --no-call-graph
1376 */
1377 if (unset) {
1378 symbol_conf.use_callchain = false;
Arnaldo Carvalho de Melo2ddd5c02016-04-18 12:09:08 -03001379 callchain->record_mode = CALLCHAIN_NONE;
Namhyung Kima2c10d32015-10-22 15:28:49 +09001380 return 0;
1381 }
1382
1383 return parse_callchain_top_opt(arg);
Ingo Molnar07800602009-04-20 15:00:56 +02001384}
Ingo Molnarb456bae2009-05-26 09:17:18 +02001385
Wang Nanb8cbb342016-02-26 09:31:51 +00001386static int perf_top_config(const char *var, const char *value, void *cb __maybe_unused)
Jiri Olsaeb853e82014-02-03 12:44:42 +01001387{
Yisheng Xiea3a4a3b2018-03-12 19:25:56 +08001388 if (!strcmp(var, "top.call-graph")) {
1389 var = "call-graph.record-mode";
1390 return perf_default_config(var, value, cb);
1391 }
Namhyung Kim104ac992013-01-22 18:09:46 +09001392 if (!strcmp(var, "top.children")) {
1393 symbol_conf.cumulate_callchain = perf_config_bool(var, value);
1394 return 0;
1395 }
Jiri Olsaeb853e82014-02-03 12:44:42 +01001396
Wang Nanb8cbb342016-02-26 09:31:51 +00001397 return 0;
Jiri Olsaeb853e82014-02-03 12:44:42 +01001398}
1399
Namhyung Kimfa5df942013-05-14 11:09:05 +09001400static int
1401parse_percent_limit(const struct option *opt, const char *arg,
1402 int unset __maybe_unused)
1403{
1404 struct perf_top *top = opt->value;
1405
1406 top->min_percent = strtof(arg, NULL);
1407 return 0;
1408}
1409
Namhyung Kim76a26542015-10-22 23:28:32 +09001410const char top_callchain_help[] = CALLCHAIN_RECORD_HELP CALLCHAIN_REPORT_HELP
1411 "\n\t\t\t\tDefault: fp,graph,0.5,caller,function";
Namhyung Kima2c10d32015-10-22 15:28:49 +09001412
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001413int cmd_top(int argc, const char **argv)
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -02001414{
Namhyung Kim16ad2ff2012-05-07 14:09:02 +09001415 char errbuf[BUFSIZ];
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -02001416 struct perf_top top = {
1417 .count_filter = 5,
1418 .delay_secs = 2,
Arnaldo Carvalho de Melo2376c672012-12-11 16:48:41 -03001419 .record_opts = {
1420 .mmap_pages = UINT_MAX,
1421 .user_freq = UINT_MAX,
1422 .user_interval = ULLONG_MAX,
1423 .freq = 4000, /* 4 KHz */
Waiman Long5dbb6e82013-10-18 10:38:49 -04001424 .target = {
Arnaldo Carvalho de Melo2376c672012-12-11 16:48:41 -03001425 .uses_mmap = true,
1426 },
Arnaldo Carvalho de Melo218d6112018-10-29 09:47:00 -03001427 /*
1428 * FIXME: This will lose PERF_RECORD_MMAP and other metadata
1429 * when we pause, fix that and reenable. Probably using a
1430 * separate evlist with a dummy event, i.e. a non-overwrite
1431 * ring buffer just for metadata events, while PERF_RECORD_SAMPLE
1432 * stays in overwrite mode. -acme
1433 * */
1434 .overwrite = 0,
Jiri Olsa16c66bc2018-11-05 13:24:55 +01001435 .sample_time = true,
Jiri Olsa1e6db2e2019-04-15 14:53:33 +02001436 .sample_time_set = true,
Namhyung Kimd1cb9fc2012-05-16 18:45:49 +09001437 },
Arnaldo Carvalho de Melo029c75e2018-05-17 16:31:32 -03001438 .max_stack = sysctl__max_stack(),
Arnaldo Carvalho de Melo982d4102018-05-25 17:28:37 -03001439 .annotation_opts = annotation__default_options,
Kan Liang0c6b4992017-09-29 07:47:55 -07001440 .nr_threads_synthesize = UINT_MAX,
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -02001441 };
Arnaldo Carvalho de Melob4006792013-12-19 14:43:45 -03001442 struct record_opts *opts = &top.record_opts;
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001443 struct target *target = &opts->target;
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -02001444 const struct option options[] = {
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001445 OPT_CALLBACK('e', "event", &top.evlist, "event",
Thomas Gleixner86847b62009-06-06 12:24:17 +02001446 "event selector. use 'perf list' to list available events",
Jiri Olsaf120f9d2011-07-14 11:25:32 +02001447 parse_events_option),
Arnaldo Carvalho de Melo2376c672012-12-11 16:48:41 -03001448 OPT_U64('c', "count", &opts->user_interval, "event period to sample"),
1449 OPT_STRING('p', "pid", &target->pid, "pid",
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001450 "profile events on existing process id"),
Arnaldo Carvalho de Melo2376c672012-12-11 16:48:41 -03001451 OPT_STRING('t', "tid", &target->tid, "tid",
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001452 "profile events on existing thread id"),
Arnaldo Carvalho de Melo2376c672012-12-11 16:48:41 -03001453 OPT_BOOLEAN('a', "all-cpus", &target->system_wide,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001454 "system-wide collection from all CPUs"),
Arnaldo Carvalho de Melo2376c672012-12-11 16:48:41 -03001455 OPT_STRING('C', "cpu", &target->cpu_list, "cpu",
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02001456 "list of cpus to monitor"),
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02001457 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1458 "file", "vmlinux pathname"),
Willy Tarreaufc2be692013-09-14 10:32:59 +02001459 OPT_BOOLEAN(0, "ignore-vmlinux", &symbol_conf.ignore_vmlinux,
1460 "don't load vmlinux even if found"),
Arnaldo Carvalho de Melo1b3aae92018-11-27 10:31:03 -03001461 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
1462 "file", "kallsyms pathname"),
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001463 OPT_BOOLEAN('K', "hide_kernel_symbols", &top.hide_kernel_symbols,
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -02001464 "hide kernel symbols"),
Jiri Olsa994a1f72013-09-01 12:36:12 +02001465 OPT_CALLBACK('m', "mmap-pages", &opts->mmap_pages, "pages",
Arnaldo Carvalho de Melo25f847022020-11-30 15:09:45 -03001466 "number of mmap data pages", evlist__parse_mmap_pages),
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -02001467 OPT_INTEGER('r', "realtime", &top.realtime_prio,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001468 "collect data with this RT SCHED_FIFO priority"),
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001469 OPT_INTEGER('d', "delay", &top.delay_secs,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001470 "number of seconds to delay between refreshes"),
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -02001471 OPT_BOOLEAN('D', "dump-symtab", &top.dump_symtab,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001472 "dump the symbol table used for profiling"),
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001473 OPT_INTEGER('f', "count-filter", &top.count_filter,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001474 "only display functions with more events than this"),
David Ahernbf806692013-11-14 20:51:30 -07001475 OPT_BOOLEAN(0, "group", &opts->group,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001476 "put the counters into a counter group"),
Arnaldo Carvalho de Melo2376c672012-12-11 16:48:41 -03001477 OPT_BOOLEAN('i', "no-inherit", &opts->no_inherit,
1478 "child tasks do not inherit counters"),
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -02001479 OPT_STRING(0, "sym-annotate", &top.sym_filter, "symbol name",
Kirill Smelkov6cff0e82010-02-03 16:52:08 -02001480 "symbol to annotate"),
Arnaldo Carvalho de Melo2376c672012-12-11 16:48:41 -03001481 OPT_BOOLEAN('z', "zero", &top.zero, "zero history across updates"),
Arnaldo Carvalho de Melo7831bf22018-03-01 14:25:56 -03001482 OPT_CALLBACK('F', "freq", &top.record_opts, "freq or 'max'",
1483 "profile at this frequency",
1484 record__parse_freq),
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001485 OPT_INTEGER('E', "entries", &top.print_entries,
Ingo Molnar6e53cdf2009-06-04 08:53:05 +02001486 "display this many functions"),
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001487 OPT_BOOLEAN('U', "hide_user_symbols", &top.hide_user_symbols,
Arnaldo Carvalho de Melo8ffcda12009-11-16 21:45:24 -02001488 "hide user symbols"),
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -02001489 OPT_BOOLEAN(0, "tui", &top.use_tui, "Use the TUI interface"),
1490 OPT_BOOLEAN(0, "stdio", &top.use_stdio, "Use the stdio interface"),
Ian Munsiec0555642010-04-13 18:37:33 +10001491 OPT_INCR('v', "verbose", &verbose,
Ingo Molnar3da297a2009-06-07 17:39:02 +02001492 "be more verbose (show counter open errors, etc)"),
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -03001493 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
Namhyung Kima2ce0672014-03-04 09:06:42 +09001494 "sort by key(s): pid, comm, dso, symbol, parent, cpu, srcline, ..."
1495 " Please refer the man page for the complete list."),
Namhyung Kim6fe8c262014-03-04 11:01:41 +09001496 OPT_STRING(0, "fields", &field_order, "key[,keys...]",
1497 "output field(s): overhead, period, sample plus all of sort keys"),
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -03001498 OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
1499 "Show a column with the number of samples"),
Arnaldo Carvalho de Melo2ddd5c02016-04-18 12:09:08 -03001500 OPT_CALLBACK_NOOPT('g', NULL, &callchain_param,
Namhyung Kima2c10d32015-10-22 15:28:49 +09001501 NULL, "enables call-graph recording and display",
Jiri Olsaae779a62013-10-26 16:25:34 +02001502 &callchain_opt),
Arnaldo Carvalho de Melo2ddd5c02016-04-18 12:09:08 -03001503 OPT_CALLBACK(0, "call-graph", &callchain_param,
Namhyung Kim76a26542015-10-22 23:28:32 +09001504 "record_mode[,record_size],print_type,threshold[,print_limit],order,sort_key[,branch]",
Namhyung Kima2c10d32015-10-22 15:28:49 +09001505 top_callchain_help, &parse_callchain_opt),
Namhyung Kim1432ec32013-10-30 17:05:55 +09001506 OPT_BOOLEAN(0, "children", &symbol_conf.cumulate_callchain,
1507 "Accumulate callchains of children and show total overhead as well"),
Waiman Long5dbb6e82013-10-18 10:38:49 -04001508 OPT_INTEGER(0, "max-stack", &top.max_stack,
1509 "Set the maximum stack depth when parsing the callchain. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03001510 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Greg Priceb21484f2012-12-06 21:48:05 -08001511 OPT_CALLBACK(0, "ignore-callees", NULL, "regex",
1512 "ignore callees of these functions in call graphs",
1513 report_parse_ignore_callees_opt),
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -03001514 OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period,
1515 "Show a column with the sum of periods"),
1516 OPT_STRING(0, "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
1517 "only consider symbols in these dsos"),
1518 OPT_STRING(0, "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
1519 "only consider symbols in these comms"),
1520 OPT_STRING(0, "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
1521 "only consider these symbols"),
Arnaldo Carvalho de Melo1eddd9e2018-05-28 11:42:59 -03001522 OPT_BOOLEAN(0, "source", &top.annotation_opts.annotate_src,
Arnaldo Carvalho de Melo64c6f0c2011-10-06 12:48:31 -03001523 "Interleave source code with assembly code (default)"),
Arnaldo Carvalho de Melo1eddd9e2018-05-28 11:42:59 -03001524 OPT_BOOLEAN(0, "asm-raw", &top.annotation_opts.show_asm_raw,
Arnaldo Carvalho de Melo64c6f0c2011-10-06 12:48:31 -03001525 "Display raw encoding of assembly instructions (default)"),
Avi Kivity763122a2014-09-13 07:15:05 +03001526 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
1527 "Enable kernel symbol demangling"),
Song Liuee7a1122019-03-11 22:30:46 -07001528 OPT_BOOLEAN(0, "no-bpf-event", &top.record_opts.no_bpf_event, "do not record bpf events"),
Arnaldo Carvalho de Melof178fd22018-05-28 14:24:45 -03001529 OPT_STRING(0, "objdump", &top.annotation_opts.objdump_path, "path",
Sukadev Bhattiprolu0d3942d2013-05-14 22:56:51 -07001530 "objdump binary to use for disassembly and annotations"),
Arnaldo Carvalho de Meloa47e8432018-05-28 11:50:21 -03001531 OPT_STRING('M', "disassembler-style", &top.annotation_opts.disassembler_style, "disassembler style",
Arnaldo Carvalho de Melo64c6f0c2011-10-06 12:48:31 -03001532 "Specify disassembler style (e.g. -M intel for intel syntax)"),
Andi Kleen3b0b16bf2020-01-07 13:04:44 -08001533 OPT_STRING(0, "prefix", &top.annotation_opts.prefix, "prefix",
1534 "Add prefix to source file path names in programs (with --prefix-strip)"),
1535 OPT_STRING(0, "prefix-strip", &top.annotation_opts.prefix_strip, "N",
1536 "Strip first N entries of source file path name in programs (with --prefix)"),
Arnaldo Carvalho de Melo2376c672012-12-11 16:48:41 -03001537 OPT_STRING('u', "uid", &target->uid_str, "user", "user to profile"),
Namhyung Kimfa5df942013-05-14 11:09:05 +09001538 OPT_CALLBACK(0, "percent-limit", &top, "percent",
1539 "Don't show entries under that percent", parse_percent_limit),
Namhyung Kim33db4562014-02-07 12:06:07 +09001540 OPT_CALLBACK(0, "percentage", NULL, "relative|absolute",
1541 "How to display percentage of filtered entries", parse_filter_percentage),
Namhyung Kimcf590022014-07-31 14:47:39 +09001542 OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str,
1543 "width[,width...]",
1544 "don't try to adjust column width, use these fixed values"),
Mark Drayton3fcb10e2018-12-04 12:34:20 -08001545 OPT_UINTEGER(0, "proc-map-timeout", &proc_map_timeout,
Kan Liang9d9cad72015-06-17 09:51:11 -04001546 "per thread proc mmap processing timeout in ms"),
Andi Kleena18b027e2015-07-18 08:24:52 -07001547 OPT_CALLBACK_NOOPT('b', "branch-any", &opts->branch_stack,
1548 "branch any", "sample any taken branches",
1549 parse_branch_stack),
1550 OPT_CALLBACK('j', "branch-filter", &opts->branch_stack,
1551 "branch filter mask", "branch stack filter modes",
1552 parse_branch_stack),
Namhyung Kim053a3982015-12-23 02:07:05 +09001553 OPT_BOOLEAN(0, "raw-trace", &symbol_conf.raw_trace,
1554 "Show raw trace event output (do not use print fmt or plugins)"),
Namhyung Kimc92fcfd2016-02-25 00:13:50 +09001555 OPT_BOOLEAN(0, "hierarchy", &symbol_conf.report_hierarchy,
1556 "Show entries in a hierarchy"),
Arnaldo Carvalho de Melo4e303fb2018-10-26 15:55:23 -03001557 OPT_BOOLEAN(0, "overwrite", &top.record_opts.overwrite,
Arnaldo Carvalho de Melo218d6112018-10-29 09:47:00 -03001558 "Use a backward ring buffer, default: no"),
Krister Johansen868a8322017-07-05 18:48:12 -07001559 OPT_BOOLEAN(0, "force", &symbol_conf.force, "don't complain, do it"),
Kan Liang0c6b4992017-09-29 07:47:55 -07001560 OPT_UINTEGER(0, "num-thread-synthesize", &top.nr_threads_synthesize,
1561 "number of thread to run event synthesize"),
Joshua Martinez51f38242021-06-16 23:18:29 +00001562 OPT_CALLBACK('G', "cgroup", &top.evlist, "name",
1563 "monitor event in cgroup name only", parse_cgroups),
Namhyung Kima0c0a4a2019-05-22 14:32:50 +09001564 OPT_BOOLEAN(0, "namespaces", &opts->record_namespaces,
1565 "Record namespaces events"),
Namhyung Kimf3828422020-03-25 21:45:35 +09001566 OPT_BOOLEAN(0, "all-cgroups", &opts->record_cgroup,
1567 "Record cgroup events"),
Jin Yaodf7deb22020-03-25 06:07:10 +08001568 OPT_INTEGER(0, "group-sort-idx", &symbol_conf.group_sort_idx,
1569 "Sort the output by the event at the index n in group. "
1570 "If n is invalid, sort by the first event. "
1571 "WARNING: should be used on grouped events."),
Kan Liang13e0c842020-03-19 13:25:15 -07001572 OPT_BOOLEAN(0, "stitch-lbr", &top.stitch_lbr,
1573 "Enable LBR callgraph stitching approach"),
Stephane Eranian70943492020-05-05 11:29:43 -07001574#ifdef HAVE_LIBPFM
1575 OPT_CALLBACK(0, "pfm-events", &top.evlist, "event",
1576 "libpfm4 event selector. use 'perf list' to list available events",
1577 parse_libpfm_events_option),
1578#endif
Arnaldo Carvalho de Melo2f53ae32019-08-15 16:03:26 -03001579 OPTS_EVSWITCH(&top.evswitch),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001580 OPT_END()
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -02001581 };
Arnaldo Carvalho de Melobe772842012-10-01 15:20:58 -03001582 const char * const top_usage[] = {
1583 "perf top [<options>]",
1584 NULL
1585 };
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03001586 int status = hists__init();
1587
1588 if (status < 0)
1589 return status;
Ingo Molnarb456bae2009-05-26 09:17:18 +02001590
Arnaldo Carvalho de Melo982d4102018-05-25 17:28:37 -03001591 top.annotation_opts.min_pcnt = 5;
1592 top.annotation_opts.context = 4;
1593
Jiri Olsa0f98b112019-07-21 13:23:55 +02001594 top.evlist = evlist__new();
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001595 if (top.evlist == NULL)
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001596 return -ENOMEM;
1597
Arnaldo Carvalho de Meloecc4c562017-01-24 13:44:10 -03001598 status = perf_config(perf_top_config, &top);
1599 if (status)
1600 return status;
Arnaldo Carvalho de Melo608127f2019-09-30 11:53:00 -03001601 /*
1602 * Since the per arch annotation init routine may need the cpuid, read
1603 * it here, since we are not getting this from the perf.data header.
1604 */
1605 status = perf_env__read_cpuid(&perf_env);
1606 if (status) {
Arnaldo Carvalho de Melo61208e62019-12-11 10:21:59 -03001607 /*
1608 * Some arches do not provide a get_cpuid(), so just use pr_debug, otherwise
Ingo Molnar4d39c892021-03-23 17:09:15 +01001609 * warn the user explicitly.
Arnaldo Carvalho de Melo61208e62019-12-11 10:21:59 -03001610 */
1611 eprintf(status == ENOSYS ? 1 : 0, verbose,
1612 "Couldn't read the cpuid for this machine: %s\n",
1613 str_error_r(errno, errbuf, sizeof(errbuf)));
Arnaldo Carvalho de Melo608127f2019-09-30 11:53:00 -03001614 }
1615 top.evlist->env = &perf_env;
Jiri Olsaeb853e82014-02-03 12:44:42 +01001616
Ingo Molnarb456bae2009-05-26 09:17:18 +02001617 argc = parse_options(argc, argv, options, top_usage, 0);
1618 if (argc)
1619 usage_with_options(top_usage, options);
1620
James Clark7cc72552021-10-18 14:48:42 +01001621 status = symbol__validate_sym_arguments();
1622 if (status)
1623 goto out_delete_evlist;
1624
Andi Kleen3b0b16bf2020-01-07 13:04:44 -08001625 if (annotate_check_args(&top.annotation_opts) < 0)
1626 goto out_delete_evlist;
1627
Jiri Olsa6484d2f2019-07-21 13:24:28 +02001628 if (!top.evlist->core.nr_entries &&
Arnaldo Carvalho de Meloe251abe2020-06-17 09:16:20 -03001629 evlist__add_default(top.evlist) < 0) {
Namhyung Kim54f8f402015-12-23 02:07:01 +09001630 pr_err("Not enough memory for event selector list\n");
1631 goto out_delete_evlist;
1632 }
1633
Arnaldo Carvalho de Melo2f53ae32019-08-15 16:03:26 -03001634 status = evswitch__init(&top.evswitch, top.evlist, stderr);
1635 if (status)
1636 goto out_delete_evlist;
1637
Namhyung Kimc92fcfd2016-02-25 00:13:50 +09001638 if (symbol_conf.report_hierarchy) {
1639 /* disable incompatible options */
1640 symbol_conf.event_group = false;
1641 symbol_conf.cumulate_callchain = false;
1642
1643 if (field_order) {
1644 pr_err("Error: --hierarchy and --fields options cannot be used together\n");
1645 parse_options_usage(top_usage, options, "fields", 0);
1646 parse_options_usage(NULL, options, "hierarchy", 0);
1647 goto out_delete_evlist;
1648 }
1649 }
1650
Kan Liang13e0c842020-03-19 13:25:15 -07001651 if (top.stitch_lbr && !(callchain_param.record_mode == CALLCHAIN_LBR)) {
1652 pr_err("Error: --stitch-lbr must be used with --call-graph lbr\n");
1653 goto out_delete_evlist;
1654 }
1655
Joshua Martinez51f38242021-06-16 23:18:29 +00001656 if (nr_cgroups > 0 && opts->record_cgroup) {
1657 pr_err("--cgroup and --all-cgroups cannot be used together\n");
1658 goto out_delete_evlist;
1659 }
1660
Jin Yao590ac602018-10-31 19:06:35 +08001661 if (opts->branch_stack && callchain_param.enabled)
1662 symbol_conf.show_branchflag_count = true;
1663
Namhyung Kim512ae1b2014-03-18 11:31:39 +09001664 sort__mode = SORT_MODE__TOP;
Namhyung Kim3a5714f2013-05-14 11:09:01 +09001665 /* display thread wants entries to be collapsed in a different tree */
Jiri Olsa52225032016-05-03 13:54:42 +02001666 perf_hpp_list.need_collapse = 1;
Namhyung Kim3a5714f2013-05-14 11:09:01 +09001667
Arnaldo Carvalho de Melo3ee60c32016-01-18 10:24:06 +01001668 if (top.use_stdio)
1669 use_browser = 0;
1670 else if (top.use_tui)
1671 use_browser = 1;
1672
1673 setup_browser(false);
1674
Namhyung Kim40184c42015-12-23 02:07:01 +09001675 if (setup_sorting(top.evlist) < 0) {
Namhyung Kim6fe8c262014-03-04 11:01:41 +09001676 if (sort_order)
1677 parse_options_usage(top_usage, options, "s", 1);
1678 if (field_order)
1679 parse_options_usage(sort_order ? NULL : top_usage,
1680 options, "fields", 0);
1681 goto out_delete_evlist;
1682 }
Namhyung Kim22af9692014-04-16 11:04:51 +09001683
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001684 status = target__validate(target);
Namhyung Kim16ad2ff2012-05-07 14:09:02 +09001685 if (status) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001686 target__strerror(target, status, errbuf, BUFSIZ);
Ingo Molnarea432a8b2013-11-13 00:26:09 +01001687 ui__warning("%s\n", errbuf);
Namhyung Kim16ad2ff2012-05-07 14:09:02 +09001688 }
Namhyung Kim4bd0f2d2012-04-26 14:15:18 +09001689
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001690 status = target__parse_uid(target);
Namhyung Kim16ad2ff2012-05-07 14:09:02 +09001691 if (status) {
1692 int saved_errno = errno;
1693
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001694 target__strerror(target, status, errbuf, BUFSIZ);
Ingo Molnarea432a8b2013-11-13 00:26:09 +01001695 ui__error("%s\n", errbuf);
Namhyung Kim16ad2ff2012-05-07 14:09:02 +09001696
1697 status = -saved_errno;
Arnaldo Carvalho de Melo0d37aa32012-01-19 14:08:15 -02001698 goto out_delete_evlist;
Namhyung Kim16ad2ff2012-05-07 14:09:02 +09001699 }
Arnaldo Carvalho de Melo0d37aa32012-01-19 14:08:15 -02001700
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001701 if (target__none(target))
Arnaldo Carvalho de Melo2376c672012-12-11 16:48:41 -03001702 target->system_wide = true;
Arnaldo Carvalho de Melo10b47d52012-05-07 16:33:56 -03001703
Arnaldo Carvalho de Melo7748bb72020-11-30 14:56:52 -03001704 if (evlist__create_maps(top.evlist, target) < 0) {
Arnaldo Carvalho de Melof8a5c0b2015-12-10 14:48:45 -03001705 ui__error("Couldn't create thread/CPU maps: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03001706 errno == ENOENT ? "No such process" : str_error_r(errno, errbuf, sizeof(errbuf)));
Arnaldo Carvalho de Melof8a5c0b2015-12-10 14:48:45 -03001707 goto out_delete_evlist;
1708 }
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -02001709
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001710 if (top.delay_secs < 1)
1711 top.delay_secs = 1;
Frederic Weisbecker2f335a02009-06-05 19:31:01 +02001712
Arnaldo Carvalho de Melob4006792013-12-19 14:43:45 -03001713 if (record_opts__config(opts)) {
Arnaldo Carvalho de Melo2376c672012-12-11 16:48:41 -03001714 status = -EINVAL;
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03001715 goto out_delete_evlist;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001716 }
1717
Jiri Olsa515dbe42019-09-03 10:39:52 +02001718 top.sym_evsel = evlist__first(top.evlist);
Arnaldo Carvalho de Melocc841582011-01-11 15:16:52 -02001719
Arnaldo Carvalho de Meloe3815262016-04-18 12:30:16 -03001720 if (!callchain_param.enabled) {
Namhyung Kim1432ec32013-10-30 17:05:55 +09001721 symbol_conf.cumulate_callchain = false;
1722 perf_hpp__cancel_cumulate();
1723 }
1724
Namhyung Kim792aeaf2015-10-22 16:45:46 +09001725 if (symbol_conf.cumulate_callchain && !callchain_param.order_set)
1726 callchain_param.order = ORDER_CALLER;
1727
Arnaldo Carvalho de Melob01141f2016-08-25 16:09:21 -03001728 status = symbol__annotation_init();
1729 if (status < 0)
1730 goto out_delete_evlist;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001731
Ravi Bangoria73840832020-02-13 12:13:03 +05301732 annotation_config__init(&top.annotation_opts);
Arnaldo Carvalho de Melo7f0b6fd2018-03-16 14:33:38 -03001733
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001734 symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL);
Changbin Du70c819e2019-03-16 16:05:48 +08001735 status = symbol__init(NULL);
1736 if (status < 0)
1737 goto out_delete_evlist;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001738
Namhyung Kim08e71542013-04-03 21:26:19 +09001739 sort__setup_elide(stdout);
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -03001740
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -02001741 get_term_dimensions(&top.winsize);
Arnaldo Carvalho de Melo8c3e10e2011-01-31 14:50:39 -02001742 if (top.print_entries == 0) {
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -02001743 perf_top__update_print_entries(&top);
Jiri Olsa244a1082017-11-15 14:30:57 +01001744 signal(SIGWINCH, winch_sig);
Arnaldo Carvalho de Melo3b6ed982009-11-16 19:30:27 -02001745 }
1746
Namhyung Kim2681bd82021-07-19 15:31:49 -07001747 top.session = perf_session__new(NULL, NULL);
Mamatha Inamdar6ef81c52019-08-22 12:50:49 +05301748 if (IS_ERR(top.session)) {
1749 status = PTR_ERR(top.session);
Changbin Du0dba9e42019-03-16 16:05:47 +08001750 goto out_delete_evlist;
1751 }
1752
Tiezhu Yang0c5f1ac2020-08-18 20:30:37 +08001753#ifdef HAVE_LIBBPF_SUPPORT
Arnaldo Carvalho de Melob38d85e2020-04-24 12:24:51 -03001754 if (!top.record_opts.no_bpf_event) {
1755 top.sb_evlist = evlist__new();
1756
1757 if (top.sb_evlist == NULL) {
1758 pr_err("Couldn't create side band evlist.\n.");
1759 goto out_delete_evlist;
1760 }
1761
1762 if (evlist__add_bpf_sb_event(top.sb_evlist, &perf_env)) {
1763 pr_err("Couldn't ask for PERF_RECORD_BPF_EVENT side band events.\n.");
1764 goto out_delete_evlist;
1765 }
1766 }
Tiezhu Yang0c5f1ac2020-08-18 20:30:37 +08001767#endif
Song Liud56354d2019-03-11 22:30:51 -07001768
Arnaldo Carvalho de Melo08c83992020-11-30 09:40:10 -03001769 if (evlist__start_sb_thread(top.sb_evlist, target)) {
Song Liu657ee552019-03-11 22:30:50 -07001770 pr_debug("Couldn't start the BPF side band thread:\nBPF programs starting from now on won't be annotatable\n");
1771 opts->no_bpf_event = true;
1772 }
1773
Arnaldo Carvalho de Melo1758af12011-11-28 09:37:05 -02001774 status = __cmd_top(&top);
Arnaldo Carvalho de Melo806fb632011-11-29 08:05:52 -02001775
Song Liu657ee552019-03-11 22:30:50 -07001776 if (!opts->no_bpf_event)
Arnaldo Carvalho de Melo08c83992020-11-30 09:40:10 -03001777 evlist__stop_sb_thread(top.sb_evlist);
Song Liu657ee552019-03-11 22:30:50 -07001778
Arnaldo Carvalho de Melo0d37aa32012-01-19 14:08:15 -02001779out_delete_evlist:
Jiri Olsac12995a2019-07-21 13:23:56 +02001780 evlist__delete(top.evlist);
Changbin Du0dba9e42019-03-16 16:05:47 +08001781 perf_session__delete(top.session);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001782
1783 return status;
Ingo Molnarb456bae2009-05-26 09:17:18 +02001784}