Commit ea1fe3a88763d4dfef7e2529ba606f96e8e6b271

Authored by Vineet Gupta
Committed by Arnaldo Carvalho de Melo
1 parent a83d869f30

perf tools: Avoid build splat for syscall numbers with uclibc

This is due to duplicated unistd inclusion (via uClibc headers + kernel headers)
Also seen on ARM uClibc based tools

   ------- ARC build ---------->8-------------

  CC       util/evlist.o
In file included from
~/arc/k.org/arch/arc/include/uapi/asm/unistd.h:25:0,
                 from util/../perf-sys.h:10,
                 from util/../perf.h:15,
                 from util/event.h:7,
                 from util/event.c:3:
~/arc/k.org/include/uapi/asm-generic/unistd.h:906:0:
warning: "__NR_fcntl64" redefined [enabled by default]
 #define __NR_fcntl64 __NR3264_fcntl
 ^
In file included from
~/arc/gnu/INSTALL_1412-arc-2014.12-rc1/arc-snps-linux-uclibc/sysroot/usr/include/sys/syscall.h:24:0,
                 from util/../perf-sys.h:6,
   ----------------->8-------------------

   ------- ARM build ---------->8-------------

  CC FPIC  plugin_scsi.o
In file included from util/../perf-sys.h:9:0,
                 from util/../perf.h:15,
                 from util/cache.h:7,
                 from perf.c:12:
~/arc/k.org/arch/arm/include/uapi/asm/unistd.h:28:0:
warning: "__NR_restart_syscall" redefined [enabled by default]
In file included from
~/buildroot/host/usr/arm-buildroot-linux-uclibcgnueabi/sysroot/usr/include/sys/syscall.h:25:0,
                 from util/../perf-sys.h:6,
                 from util/../perf.h:15,
                 from util/cache.h:7,
                 from perf.c:12:
~/buildroot/host/usr/arm-buildroot-linux-uclibcgnueabi/sysroot/usr/include/bits/sysnum.h:17:0:
note: this is the location of the previous definition
   ----------------->8-------------------

Signed-off-by: Vineet Gupta <vgupta@synopsys.com>
Cc: Alexey Brodkin <Alexey.Brodkin@synopsys.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: http://lkml.kernel.org/r/1421156604-30603-4-git-send-email-vgupta@synopsys.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

Showing 3 changed files with 1 additions and 3 deletions Inline Diff

tools/perf/bench/sched-pipe.c
1 /* 1 /*
2 * 2 *
3 * sched-pipe.c 3 * sched-pipe.c
4 * 4 *
5 * pipe: Benchmark for pipe() 5 * pipe: Benchmark for pipe()
6 * 6 *
7 * Based on pipe-test-1m.c by Ingo Molnar <mingo@redhat.com> 7 * Based on pipe-test-1m.c by Ingo Molnar <mingo@redhat.com>
8 * http://people.redhat.com/mingo/cfs-scheduler/tools/pipe-test-1m.c 8 * http://people.redhat.com/mingo/cfs-scheduler/tools/pipe-test-1m.c
9 * Ported to perf by Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp> 9 * Ported to perf by Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp>
10 */ 10 */
11 #include "../perf.h" 11 #include "../perf.h"
12 #include "../util/util.h" 12 #include "../util/util.h"
13 #include "../util/parse-options.h" 13 #include "../util/parse-options.h"
14 #include "../builtin.h" 14 #include "../builtin.h"
15 #include "bench.h" 15 #include "bench.h"
16 16
17 #include <unistd.h> 17 #include <unistd.h>
18 #include <stdio.h> 18 #include <stdio.h>
19 #include <stdlib.h> 19 #include <stdlib.h>
20 #include <signal.h> 20 #include <signal.h>
21 #include <sys/wait.h> 21 #include <sys/wait.h>
22 #include <linux/unistd.h>
23 #include <string.h> 22 #include <string.h>
24 #include <errno.h> 23 #include <errno.h>
25 #include <assert.h> 24 #include <assert.h>
26 #include <sys/time.h> 25 #include <sys/time.h>
27 #include <sys/types.h> 26 #include <sys/types.h>
27 #include <sys/syscall.h>
28 28
29 #include <pthread.h> 29 #include <pthread.h>
30 30
31 struct thread_data { 31 struct thread_data {
32 int nr; 32 int nr;
33 int pipe_read; 33 int pipe_read;
34 int pipe_write; 34 int pipe_write;
35 pthread_t pthread; 35 pthread_t pthread;
36 }; 36 };
37 37
38 #define LOOPS_DEFAULT 1000000 38 #define LOOPS_DEFAULT 1000000
39 static int loops = LOOPS_DEFAULT; 39 static int loops = LOOPS_DEFAULT;
40 40
41 /* Use processes by default: */ 41 /* Use processes by default: */
42 static bool threaded; 42 static bool threaded;
43 43
44 static const struct option options[] = { 44 static const struct option options[] = {
45 OPT_INTEGER('l', "loop", &loops, "Specify number of loops"), 45 OPT_INTEGER('l', "loop", &loops, "Specify number of loops"),
46 OPT_BOOLEAN('T', "threaded", &threaded, "Specify threads/process based task setup"), 46 OPT_BOOLEAN('T', "threaded", &threaded, "Specify threads/process based task setup"),
47 OPT_END() 47 OPT_END()
48 }; 48 };
49 49
50 static const char * const bench_sched_pipe_usage[] = { 50 static const char * const bench_sched_pipe_usage[] = {
51 "perf bench sched pipe <options>", 51 "perf bench sched pipe <options>",
52 NULL 52 NULL
53 }; 53 };
54 54
55 static void *worker_thread(void *__tdata) 55 static void *worker_thread(void *__tdata)
56 { 56 {
57 struct thread_data *td = __tdata; 57 struct thread_data *td = __tdata;
58 int m = 0, i; 58 int m = 0, i;
59 int ret; 59 int ret;
60 60
61 for (i = 0; i < loops; i++) { 61 for (i = 0; i < loops; i++) {
62 if (!td->nr) { 62 if (!td->nr) {
63 ret = read(td->pipe_read, &m, sizeof(int)); 63 ret = read(td->pipe_read, &m, sizeof(int));
64 BUG_ON(ret != sizeof(int)); 64 BUG_ON(ret != sizeof(int));
65 ret = write(td->pipe_write, &m, sizeof(int)); 65 ret = write(td->pipe_write, &m, sizeof(int));
66 BUG_ON(ret != sizeof(int)); 66 BUG_ON(ret != sizeof(int));
67 } else { 67 } else {
68 ret = write(td->pipe_write, &m, sizeof(int)); 68 ret = write(td->pipe_write, &m, sizeof(int));
69 BUG_ON(ret != sizeof(int)); 69 BUG_ON(ret != sizeof(int));
70 ret = read(td->pipe_read, &m, sizeof(int)); 70 ret = read(td->pipe_read, &m, sizeof(int));
71 BUG_ON(ret != sizeof(int)); 71 BUG_ON(ret != sizeof(int));
72 } 72 }
73 } 73 }
74 74
75 return NULL; 75 return NULL;
76 } 76 }
77 77
78 int bench_sched_pipe(int argc, const char **argv, const char *prefix __maybe_unused) 78 int bench_sched_pipe(int argc, const char **argv, const char *prefix __maybe_unused)
79 { 79 {
80 struct thread_data threads[2], *td; 80 struct thread_data threads[2], *td;
81 int pipe_1[2], pipe_2[2]; 81 int pipe_1[2], pipe_2[2];
82 struct timeval start, stop, diff; 82 struct timeval start, stop, diff;
83 unsigned long long result_usec = 0; 83 unsigned long long result_usec = 0;
84 int nr_threads = 2; 84 int nr_threads = 2;
85 int t; 85 int t;
86 86
87 /* 87 /*
88 * why does "ret" exist? 88 * why does "ret" exist?
89 * discarding returned value of read(), write() 89 * discarding returned value of read(), write()
90 * causes error in building environment for perf 90 * causes error in building environment for perf
91 */ 91 */
92 int __maybe_unused ret, wait_stat; 92 int __maybe_unused ret, wait_stat;
93 pid_t pid, retpid __maybe_unused; 93 pid_t pid, retpid __maybe_unused;
94 94
95 argc = parse_options(argc, argv, options, bench_sched_pipe_usage, 0); 95 argc = parse_options(argc, argv, options, bench_sched_pipe_usage, 0);
96 96
97 BUG_ON(pipe(pipe_1)); 97 BUG_ON(pipe(pipe_1));
98 BUG_ON(pipe(pipe_2)); 98 BUG_ON(pipe(pipe_2));
99 99
100 gettimeofday(&start, NULL); 100 gettimeofday(&start, NULL);
101 101
102 for (t = 0; t < nr_threads; t++) { 102 for (t = 0; t < nr_threads; t++) {
103 td = threads + t; 103 td = threads + t;
104 104
105 td->nr = t; 105 td->nr = t;
106 106
107 if (t == 0) { 107 if (t == 0) {
108 td->pipe_read = pipe_1[0]; 108 td->pipe_read = pipe_1[0];
109 td->pipe_write = pipe_2[1]; 109 td->pipe_write = pipe_2[1];
110 } else { 110 } else {
111 td->pipe_write = pipe_1[1]; 111 td->pipe_write = pipe_1[1];
112 td->pipe_read = pipe_2[0]; 112 td->pipe_read = pipe_2[0];
113 } 113 }
114 } 114 }
115 115
116 116
117 if (threaded) { 117 if (threaded) {
118 118
119 for (t = 0; t < nr_threads; t++) { 119 for (t = 0; t < nr_threads; t++) {
120 td = threads + t; 120 td = threads + t;
121 121
122 ret = pthread_create(&td->pthread, NULL, worker_thread, td); 122 ret = pthread_create(&td->pthread, NULL, worker_thread, td);
123 BUG_ON(ret); 123 BUG_ON(ret);
124 } 124 }
125 125
126 for (t = 0; t < nr_threads; t++) { 126 for (t = 0; t < nr_threads; t++) {
127 td = threads + t; 127 td = threads + t;
128 128
129 ret = pthread_join(td->pthread, NULL); 129 ret = pthread_join(td->pthread, NULL);
130 BUG_ON(ret); 130 BUG_ON(ret);
131 } 131 }
132 132
133 } else { 133 } else {
134 pid = fork(); 134 pid = fork();
135 assert(pid >= 0); 135 assert(pid >= 0);
136 136
137 if (!pid) { 137 if (!pid) {
138 worker_thread(threads + 0); 138 worker_thread(threads + 0);
139 exit(0); 139 exit(0);
140 } else { 140 } else {
141 worker_thread(threads + 1); 141 worker_thread(threads + 1);
142 } 142 }
143 143
144 retpid = waitpid(pid, &wait_stat, 0); 144 retpid = waitpid(pid, &wait_stat, 0);
145 assert((retpid == pid) && WIFEXITED(wait_stat)); 145 assert((retpid == pid) && WIFEXITED(wait_stat));
146 } 146 }
147 147
148 gettimeofday(&stop, NULL); 148 gettimeofday(&stop, NULL);
149 timersub(&stop, &start, &diff); 149 timersub(&stop, &start, &diff);
150 150
151 switch (bench_format) { 151 switch (bench_format) {
152 case BENCH_FORMAT_DEFAULT: 152 case BENCH_FORMAT_DEFAULT:
153 printf("# Executed %d pipe operations between two %s\n\n", 153 printf("# Executed %d pipe operations between two %s\n\n",
154 loops, threaded ? "threads" : "processes"); 154 loops, threaded ? "threads" : "processes");
155 155
156 result_usec = diff.tv_sec * 1000000; 156 result_usec = diff.tv_sec * 1000000;
157 result_usec += diff.tv_usec; 157 result_usec += diff.tv_usec;
158 158
159 printf(" %14s: %lu.%03lu [sec]\n\n", "Total time", 159 printf(" %14s: %lu.%03lu [sec]\n\n", "Total time",
160 diff.tv_sec, 160 diff.tv_sec,
161 (unsigned long) (diff.tv_usec/1000)); 161 (unsigned long) (diff.tv_usec/1000));
162 162
163 printf(" %14lf usecs/op\n", 163 printf(" %14lf usecs/op\n",
164 (double)result_usec / (double)loops); 164 (double)result_usec / (double)loops);
165 printf(" %14d ops/sec\n", 165 printf(" %14d ops/sec\n",
166 (int)((double)loops / 166 (int)((double)loops /
167 ((double)result_usec / (double)1000000))); 167 ((double)result_usec / (double)1000000)));
168 break; 168 break;
169 169
170 case BENCH_FORMAT_SIMPLE: 170 case BENCH_FORMAT_SIMPLE:
171 printf("%lu.%03lu\n", 171 printf("%lu.%03lu\n",
172 diff.tv_sec, 172 diff.tv_sec,
173 (unsigned long) (diff.tv_usec / 1000)); 173 (unsigned long) (diff.tv_usec / 1000));
174 break; 174 break;
175 175
176 default: 176 default:
177 /* reaching here is something disaster */ 177 /* reaching here is something disaster */
178 fprintf(stderr, "Unknown format:%d\n", bench_format); 178 fprintf(stderr, "Unknown format:%d\n", bench_format);
179 exit(1); 179 exit(1);
180 break; 180 break;
181 } 181 }
182 182
183 return 0; 183 return 0;
184 } 184 }
tools/perf/builtin-top.c
1 /* 1 /*
2 * builtin-top.c 2 * builtin-top.c
3 * 3 *
4 * Builtin top command: Display a continuously updated profile of 4 * Builtin top command: Display a continuously updated profile of
5 * any workload, CPU or specific PID. 5 * any workload, CPU or specific PID.
6 * 6 *
7 * Copyright (C) 2008, Red Hat Inc, Ingo Molnar <mingo@redhat.com> 7 * Copyright (C) 2008, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
8 * 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com> 8 * 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
9 * 9 *
10 * Improvements and fixes by: 10 * Improvements and fixes by:
11 * 11 *
12 * Arjan van de Ven <arjan@linux.intel.com> 12 * Arjan van de Ven <arjan@linux.intel.com>
13 * Yanmin Zhang <yanmin.zhang@intel.com> 13 * Yanmin Zhang <yanmin.zhang@intel.com>
14 * Wu Fengguang <fengguang.wu@intel.com> 14 * Wu Fengguang <fengguang.wu@intel.com>
15 * Mike Galbraith <efault@gmx.de> 15 * Mike Galbraith <efault@gmx.de>
16 * Paul Mackerras <paulus@samba.org> 16 * Paul Mackerras <paulus@samba.org>
17 * 17 *
18 * Released under the GPL v2. (and only v2, not any later version) 18 * Released under the GPL v2. (and only v2, not any later version)
19 */ 19 */
20 #include "builtin.h" 20 #include "builtin.h"
21 21
22 #include "perf.h" 22 #include "perf.h"
23 23
24 #include "util/annotate.h" 24 #include "util/annotate.h"
25 #include "util/cache.h" 25 #include "util/cache.h"
26 #include "util/color.h" 26 #include "util/color.h"
27 #include "util/evlist.h" 27 #include "util/evlist.h"
28 #include "util/evsel.h" 28 #include "util/evsel.h"
29 #include "util/machine.h" 29 #include "util/machine.h"
30 #include "util/session.h" 30 #include "util/session.h"
31 #include "util/symbol.h" 31 #include "util/symbol.h"
32 #include "util/thread.h" 32 #include "util/thread.h"
33 #include "util/thread_map.h" 33 #include "util/thread_map.h"
34 #include "util/top.h" 34 #include "util/top.h"
35 #include "util/util.h" 35 #include "util/util.h"
36 #include <linux/rbtree.h> 36 #include <linux/rbtree.h>
37 #include "util/parse-options.h" 37 #include "util/parse-options.h"
38 #include "util/parse-events.h" 38 #include "util/parse-events.h"
39 #include "util/cpumap.h" 39 #include "util/cpumap.h"
40 #include "util/xyarray.h" 40 #include "util/xyarray.h"
41 #include "util/sort.h" 41 #include "util/sort.h"
42 #include "util/intlist.h" 42 #include "util/intlist.h"
43 #include "arch/common.h" 43 #include "arch/common.h"
44 44
45 #include "util/debug.h" 45 #include "util/debug.h"
46 46
47 #include <assert.h> 47 #include <assert.h>
48 #include <elf.h> 48 #include <elf.h>
49 #include <fcntl.h> 49 #include <fcntl.h>
50 50
51 #include <stdio.h> 51 #include <stdio.h>
52 #include <termios.h> 52 #include <termios.h>
53 #include <unistd.h> 53 #include <unistd.h>
54 #include <inttypes.h> 54 #include <inttypes.h>
55 55
56 #include <errno.h> 56 #include <errno.h>
57 #include <time.h> 57 #include <time.h>
58 #include <sched.h> 58 #include <sched.h>
59 59
60 #include <sys/syscall.h> 60 #include <sys/syscall.h>
61 #include <sys/ioctl.h> 61 #include <sys/ioctl.h>
62 #include <poll.h> 62 #include <poll.h>
63 #include <sys/prctl.h> 63 #include <sys/prctl.h>
64 #include <sys/wait.h> 64 #include <sys/wait.h>
65 #include <sys/uio.h> 65 #include <sys/uio.h>
66 #include <sys/utsname.h> 66 #include <sys/utsname.h>
67 #include <sys/mman.h> 67 #include <sys/mman.h>
68 68
69 #include <linux/unistd.h>
70 #include <linux/types.h> 69 #include <linux/types.h>
71 70
72 static volatile int done; 71 static volatile int done;
73 72
74 #define HEADER_LINE_NR 5 73 #define HEADER_LINE_NR 5
75 74
76 static void perf_top__update_print_entries(struct perf_top *top) 75 static void perf_top__update_print_entries(struct perf_top *top)
77 { 76 {
78 top->print_entries = top->winsize.ws_row - HEADER_LINE_NR; 77 top->print_entries = top->winsize.ws_row - HEADER_LINE_NR;
79 } 78 }
80 79
81 static void perf_top__sig_winch(int sig __maybe_unused, 80 static void perf_top__sig_winch(int sig __maybe_unused,
82 siginfo_t *info __maybe_unused, void *arg) 81 siginfo_t *info __maybe_unused, void *arg)
83 { 82 {
84 struct perf_top *top = arg; 83 struct perf_top *top = arg;
85 84
86 get_term_dimensions(&top->winsize); 85 get_term_dimensions(&top->winsize);
87 perf_top__update_print_entries(top); 86 perf_top__update_print_entries(top);
88 } 87 }
89 88
90 static int perf_top__parse_source(struct perf_top *top, struct hist_entry *he) 89 static int perf_top__parse_source(struct perf_top *top, struct hist_entry *he)
91 { 90 {
92 struct symbol *sym; 91 struct symbol *sym;
93 struct annotation *notes; 92 struct annotation *notes;
94 struct map *map; 93 struct map *map;
95 int err = -1; 94 int err = -1;
96 95
97 if (!he || !he->ms.sym) 96 if (!he || !he->ms.sym)
98 return -1; 97 return -1;
99 98
100 sym = he->ms.sym; 99 sym = he->ms.sym;
101 map = he->ms.map; 100 map = he->ms.map;
102 101
103 /* 102 /*
104 * We can't annotate with just /proc/kallsyms 103 * We can't annotate with just /proc/kallsyms
105 */ 104 */
106 if (map->dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS && 105 if (map->dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS &&
107 !dso__is_kcore(map->dso)) { 106 !dso__is_kcore(map->dso)) {
108 pr_err("Can't annotate %s: No vmlinux file was found in the " 107 pr_err("Can't annotate %s: No vmlinux file was found in the "
109 "path\n", sym->name); 108 "path\n", sym->name);
110 sleep(1); 109 sleep(1);
111 return -1; 110 return -1;
112 } 111 }
113 112
114 notes = symbol__annotation(sym); 113 notes = symbol__annotation(sym);
115 if (notes->src != NULL) { 114 if (notes->src != NULL) {
116 pthread_mutex_lock(&notes->lock); 115 pthread_mutex_lock(&notes->lock);
117 goto out_assign; 116 goto out_assign;
118 } 117 }
119 118
120 pthread_mutex_lock(&notes->lock); 119 pthread_mutex_lock(&notes->lock);
121 120
122 if (symbol__alloc_hist(sym) < 0) { 121 if (symbol__alloc_hist(sym) < 0) {
123 pthread_mutex_unlock(&notes->lock); 122 pthread_mutex_unlock(&notes->lock);
124 pr_err("Not enough memory for annotating '%s' symbol!\n", 123 pr_err("Not enough memory for annotating '%s' symbol!\n",
125 sym->name); 124 sym->name);
126 sleep(1); 125 sleep(1);
127 return err; 126 return err;
128 } 127 }
129 128
130 err = symbol__annotate(sym, map, 0); 129 err = symbol__annotate(sym, map, 0);
131 if (err == 0) { 130 if (err == 0) {
132 out_assign: 131 out_assign:
133 top->sym_filter_entry = he; 132 top->sym_filter_entry = he;
134 } 133 }
135 134
136 pthread_mutex_unlock(&notes->lock); 135 pthread_mutex_unlock(&notes->lock);
137 return err; 136 return err;
138 } 137 }
139 138
140 static void __zero_source_counters(struct hist_entry *he) 139 static void __zero_source_counters(struct hist_entry *he)
141 { 140 {
142 struct symbol *sym = he->ms.sym; 141 struct symbol *sym = he->ms.sym;
143 symbol__annotate_zero_histograms(sym); 142 symbol__annotate_zero_histograms(sym);
144 } 143 }
145 144
146 static void ui__warn_map_erange(struct map *map, struct symbol *sym, u64 ip) 145 static void ui__warn_map_erange(struct map *map, struct symbol *sym, u64 ip)
147 { 146 {
148 struct utsname uts; 147 struct utsname uts;
149 int err = uname(&uts); 148 int err = uname(&uts);
150 149
151 ui__warning("Out of bounds address found:\n\n" 150 ui__warning("Out of bounds address found:\n\n"
152 "Addr: %" PRIx64 "\n" 151 "Addr: %" PRIx64 "\n"
153 "DSO: %s %c\n" 152 "DSO: %s %c\n"
154 "Map: %" PRIx64 "-%" PRIx64 "\n" 153 "Map: %" PRIx64 "-%" PRIx64 "\n"
155 "Symbol: %" PRIx64 "-%" PRIx64 " %c %s\n" 154 "Symbol: %" PRIx64 "-%" PRIx64 " %c %s\n"
156 "Arch: %s\n" 155 "Arch: %s\n"
157 "Kernel: %s\n" 156 "Kernel: %s\n"
158 "Tools: %s\n\n" 157 "Tools: %s\n\n"
159 "Not all samples will be on the annotation output.\n\n" 158 "Not all samples will be on the annotation output.\n\n"
160 "Please report to linux-kernel@vger.kernel.org\n", 159 "Please report to linux-kernel@vger.kernel.org\n",
161 ip, map->dso->long_name, dso__symtab_origin(map->dso), 160 ip, map->dso->long_name, dso__symtab_origin(map->dso),
162 map->start, map->end, sym->start, sym->end, 161 map->start, map->end, sym->start, sym->end,
163 sym->binding == STB_GLOBAL ? 'g' : 162 sym->binding == STB_GLOBAL ? 'g' :
164 sym->binding == STB_LOCAL ? 'l' : 'w', sym->name, 163 sym->binding == STB_LOCAL ? 'l' : 'w', sym->name,
165 err ? "[unknown]" : uts.machine, 164 err ? "[unknown]" : uts.machine,
166 err ? "[unknown]" : uts.release, perf_version_string); 165 err ? "[unknown]" : uts.release, perf_version_string);
167 if (use_browser <= 0) 166 if (use_browser <= 0)
168 sleep(5); 167 sleep(5);
169 168
170 map->erange_warned = true; 169 map->erange_warned = true;
171 } 170 }
172 171
173 static void perf_top__record_precise_ip(struct perf_top *top, 172 static void perf_top__record_precise_ip(struct perf_top *top,
174 struct hist_entry *he, 173 struct hist_entry *he,
175 int counter, u64 ip) 174 int counter, u64 ip)
176 { 175 {
177 struct annotation *notes; 176 struct annotation *notes;
178 struct symbol *sym; 177 struct symbol *sym;
179 int err = 0; 178 int err = 0;
180 179
181 if (he == NULL || he->ms.sym == NULL || 180 if (he == NULL || he->ms.sym == NULL ||
182 ((top->sym_filter_entry == NULL || 181 ((top->sym_filter_entry == NULL ||
183 top->sym_filter_entry->ms.sym != he->ms.sym) && use_browser != 1)) 182 top->sym_filter_entry->ms.sym != he->ms.sym) && use_browser != 1))
184 return; 183 return;
185 184
186 sym = he->ms.sym; 185 sym = he->ms.sym;
187 notes = symbol__annotation(sym); 186 notes = symbol__annotation(sym);
188 187
189 if (pthread_mutex_trylock(&notes->lock)) 188 if (pthread_mutex_trylock(&notes->lock))
190 return; 189 return;
191 190
192 ip = he->ms.map->map_ip(he->ms.map, ip); 191 ip = he->ms.map->map_ip(he->ms.map, ip);
193 192
194 if (ui__has_annotation()) 193 if (ui__has_annotation())
195 err = hist_entry__inc_addr_samples(he, counter, ip); 194 err = hist_entry__inc_addr_samples(he, counter, ip);
196 195
197 pthread_mutex_unlock(&notes->lock); 196 pthread_mutex_unlock(&notes->lock);
198 197
199 /* 198 /*
200 * This function is now called with he->hists->lock held. 199 * This function is now called with he->hists->lock held.
201 * Release it before going to sleep. 200 * Release it before going to sleep.
202 */ 201 */
203 pthread_mutex_unlock(&he->hists->lock); 202 pthread_mutex_unlock(&he->hists->lock);
204 203
205 if (err == -ERANGE && !he->ms.map->erange_warned) 204 if (err == -ERANGE && !he->ms.map->erange_warned)
206 ui__warn_map_erange(he->ms.map, sym, ip); 205 ui__warn_map_erange(he->ms.map, sym, ip);
207 else if (err == -ENOMEM) { 206 else if (err == -ENOMEM) {
208 pr_err("Not enough memory for annotating '%s' symbol!\n", 207 pr_err("Not enough memory for annotating '%s' symbol!\n",
209 sym->name); 208 sym->name);
210 sleep(1); 209 sleep(1);
211 } 210 }
212 211
213 pthread_mutex_lock(&he->hists->lock); 212 pthread_mutex_lock(&he->hists->lock);
214 } 213 }
215 214
216 static void perf_top__show_details(struct perf_top *top) 215 static void perf_top__show_details(struct perf_top *top)
217 { 216 {
218 struct hist_entry *he = top->sym_filter_entry; 217 struct hist_entry *he = top->sym_filter_entry;
219 struct annotation *notes; 218 struct annotation *notes;
220 struct symbol *symbol; 219 struct symbol *symbol;
221 int more; 220 int more;
222 221
223 if (!he) 222 if (!he)
224 return; 223 return;
225 224
226 symbol = he->ms.sym; 225 symbol = he->ms.sym;
227 notes = symbol__annotation(symbol); 226 notes = symbol__annotation(symbol);
228 227
229 pthread_mutex_lock(&notes->lock); 228 pthread_mutex_lock(&notes->lock);
230 229
231 if (notes->src == NULL) 230 if (notes->src == NULL)
232 goto out_unlock; 231 goto out_unlock;
233 232
234 printf("Showing %s for %s\n", perf_evsel__name(top->sym_evsel), symbol->name); 233 printf("Showing %s for %s\n", perf_evsel__name(top->sym_evsel), symbol->name);
235 printf(" Events Pcnt (>=%d%%)\n", top->sym_pcnt_filter); 234 printf(" Events Pcnt (>=%d%%)\n", top->sym_pcnt_filter);
236 235
237 more = symbol__annotate_printf(symbol, he->ms.map, top->sym_evsel, 236 more = symbol__annotate_printf(symbol, he->ms.map, top->sym_evsel,
238 0, top->sym_pcnt_filter, top->print_entries, 4); 237 0, top->sym_pcnt_filter, top->print_entries, 4);
239 if (top->zero) 238 if (top->zero)
240 symbol__annotate_zero_histogram(symbol, top->sym_evsel->idx); 239 symbol__annotate_zero_histogram(symbol, top->sym_evsel->idx);
241 else 240 else
242 symbol__annotate_decay_histogram(symbol, top->sym_evsel->idx); 241 symbol__annotate_decay_histogram(symbol, top->sym_evsel->idx);
243 if (more != 0) 242 if (more != 0)
244 printf("%d lines not displayed, maybe increase display entries [e]\n", more); 243 printf("%d lines not displayed, maybe increase display entries [e]\n", more);
245 out_unlock: 244 out_unlock:
246 pthread_mutex_unlock(&notes->lock); 245 pthread_mutex_unlock(&notes->lock);
247 } 246 }
248 247
249 static void perf_top__print_sym_table(struct perf_top *top) 248 static void perf_top__print_sym_table(struct perf_top *top)
250 { 249 {
251 char bf[160]; 250 char bf[160];
252 int printed = 0; 251 int printed = 0;
253 const int win_width = top->winsize.ws_col - 1; 252 const int win_width = top->winsize.ws_col - 1;
254 struct hists *hists = evsel__hists(top->sym_evsel); 253 struct hists *hists = evsel__hists(top->sym_evsel);
255 254
256 puts(CONSOLE_CLEAR); 255 puts(CONSOLE_CLEAR);
257 256
258 perf_top__header_snprintf(top, bf, sizeof(bf)); 257 perf_top__header_snprintf(top, bf, sizeof(bf));
259 printf("%s\n", bf); 258 printf("%s\n", bf);
260 259
261 perf_top__reset_sample_counters(top); 260 perf_top__reset_sample_counters(top);
262 261
263 printf("%-*.*s\n", win_width, win_width, graph_dotted_line); 262 printf("%-*.*s\n", win_width, win_width, graph_dotted_line);
264 263
265 if (hists->stats.nr_lost_warned != 264 if (hists->stats.nr_lost_warned !=
266 hists->stats.nr_events[PERF_RECORD_LOST]) { 265 hists->stats.nr_events[PERF_RECORD_LOST]) {
267 hists->stats.nr_lost_warned = 266 hists->stats.nr_lost_warned =
268 hists->stats.nr_events[PERF_RECORD_LOST]; 267 hists->stats.nr_events[PERF_RECORD_LOST];
269 color_fprintf(stdout, PERF_COLOR_RED, 268 color_fprintf(stdout, PERF_COLOR_RED,
270 "WARNING: LOST %d chunks, Check IO/CPU overload", 269 "WARNING: LOST %d chunks, Check IO/CPU overload",
271 hists->stats.nr_lost_warned); 270 hists->stats.nr_lost_warned);
272 ++printed; 271 ++printed;
273 } 272 }
274 273
275 if (top->sym_filter_entry) { 274 if (top->sym_filter_entry) {
276 perf_top__show_details(top); 275 perf_top__show_details(top);
277 return; 276 return;
278 } 277 }
279 278
280 if (top->zero) { 279 if (top->zero) {
281 hists__delete_entries(hists); 280 hists__delete_entries(hists);
282 } else { 281 } else {
283 hists__decay_entries(hists, top->hide_user_symbols, 282 hists__decay_entries(hists, top->hide_user_symbols,
284 top->hide_kernel_symbols); 283 top->hide_kernel_symbols);
285 } 284 }
286 285
287 hists__collapse_resort(hists, NULL); 286 hists__collapse_resort(hists, NULL);
288 hists__output_resort(hists, NULL); 287 hists__output_resort(hists, NULL);
289 288
290 hists__output_recalc_col_len(hists, top->print_entries - printed); 289 hists__output_recalc_col_len(hists, top->print_entries - printed);
291 putchar('\n'); 290 putchar('\n');
292 hists__fprintf(hists, false, top->print_entries - printed, win_width, 291 hists__fprintf(hists, false, top->print_entries - printed, win_width,
293 top->min_percent, stdout); 292 top->min_percent, stdout);
294 } 293 }
295 294
296 static void prompt_integer(int *target, const char *msg) 295 static void prompt_integer(int *target, const char *msg)
297 { 296 {
298 char *buf = malloc(0), *p; 297 char *buf = malloc(0), *p;
299 size_t dummy = 0; 298 size_t dummy = 0;
300 int tmp; 299 int tmp;
301 300
302 fprintf(stdout, "\n%s: ", msg); 301 fprintf(stdout, "\n%s: ", msg);
303 if (getline(&buf, &dummy, stdin) < 0) 302 if (getline(&buf, &dummy, stdin) < 0)
304 return; 303 return;
305 304
306 p = strchr(buf, '\n'); 305 p = strchr(buf, '\n');
307 if (p) 306 if (p)
308 *p = 0; 307 *p = 0;
309 308
310 p = buf; 309 p = buf;
311 while(*p) { 310 while(*p) {
312 if (!isdigit(*p)) 311 if (!isdigit(*p))
313 goto out_free; 312 goto out_free;
314 p++; 313 p++;
315 } 314 }
316 tmp = strtoul(buf, NULL, 10); 315 tmp = strtoul(buf, NULL, 10);
317 *target = tmp; 316 *target = tmp;
318 out_free: 317 out_free:
319 free(buf); 318 free(buf);
320 } 319 }
321 320
322 static void prompt_percent(int *target, const char *msg) 321 static void prompt_percent(int *target, const char *msg)
323 { 322 {
324 int tmp = 0; 323 int tmp = 0;
325 324
326 prompt_integer(&tmp, msg); 325 prompt_integer(&tmp, msg);
327 if (tmp >= 0 && tmp <= 100) 326 if (tmp >= 0 && tmp <= 100)
328 *target = tmp; 327 *target = tmp;
329 } 328 }
330 329
331 static void perf_top__prompt_symbol(struct perf_top *top, const char *msg) 330 static void perf_top__prompt_symbol(struct perf_top *top, const char *msg)
332 { 331 {
333 char *buf = malloc(0), *p; 332 char *buf = malloc(0), *p;
334 struct hist_entry *syme = top->sym_filter_entry, *n, *found = NULL; 333 struct hist_entry *syme = top->sym_filter_entry, *n, *found = NULL;
335 struct hists *hists = evsel__hists(top->sym_evsel); 334 struct hists *hists = evsel__hists(top->sym_evsel);
336 struct rb_node *next; 335 struct rb_node *next;
337 size_t dummy = 0; 336 size_t dummy = 0;
338 337
339 /* zero counters of active symbol */ 338 /* zero counters of active symbol */
340 if (syme) { 339 if (syme) {
341 __zero_source_counters(syme); 340 __zero_source_counters(syme);
342 top->sym_filter_entry = NULL; 341 top->sym_filter_entry = NULL;
343 } 342 }
344 343
345 fprintf(stdout, "\n%s: ", msg); 344 fprintf(stdout, "\n%s: ", msg);
346 if (getline(&buf, &dummy, stdin) < 0) 345 if (getline(&buf, &dummy, stdin) < 0)
347 goto out_free; 346 goto out_free;
348 347
349 p = strchr(buf, '\n'); 348 p = strchr(buf, '\n');
350 if (p) 349 if (p)
351 *p = 0; 350 *p = 0;
352 351
353 next = rb_first(&hists->entries); 352 next = rb_first(&hists->entries);
354 while (next) { 353 while (next) {
355 n = rb_entry(next, struct hist_entry, rb_node); 354 n = rb_entry(next, struct hist_entry, rb_node);
356 if (n->ms.sym && !strcmp(buf, n->ms.sym->name)) { 355 if (n->ms.sym && !strcmp(buf, n->ms.sym->name)) {
357 found = n; 356 found = n;
358 break; 357 break;
359 } 358 }
360 next = rb_next(&n->rb_node); 359 next = rb_next(&n->rb_node);
361 } 360 }
362 361
363 if (!found) { 362 if (!found) {
364 fprintf(stderr, "Sorry, %s is not active.\n", buf); 363 fprintf(stderr, "Sorry, %s is not active.\n", buf);
365 sleep(1); 364 sleep(1);
366 } else 365 } else
367 perf_top__parse_source(top, found); 366 perf_top__parse_source(top, found);
368 367
369 out_free: 368 out_free:
370 free(buf); 369 free(buf);
371 } 370 }
372 371
373 static void perf_top__print_mapped_keys(struct perf_top *top) 372 static void perf_top__print_mapped_keys(struct perf_top *top)
374 { 373 {
375 char *name = NULL; 374 char *name = NULL;
376 375
377 if (top->sym_filter_entry) { 376 if (top->sym_filter_entry) {
378 struct symbol *sym = top->sym_filter_entry->ms.sym; 377 struct symbol *sym = top->sym_filter_entry->ms.sym;
379 name = sym->name; 378 name = sym->name;
380 } 379 }
381 380
382 fprintf(stdout, "\nMapped keys:\n"); 381 fprintf(stdout, "\nMapped keys:\n");
383 fprintf(stdout, "\t[d] display refresh delay. \t(%d)\n", top->delay_secs); 382 fprintf(stdout, "\t[d] display refresh delay. \t(%d)\n", top->delay_secs);
384 fprintf(stdout, "\t[e] display entries (lines). \t(%d)\n", top->print_entries); 383 fprintf(stdout, "\t[e] display entries (lines). \t(%d)\n", top->print_entries);
385 384
386 if (top->evlist->nr_entries > 1) 385 if (top->evlist->nr_entries > 1)
387 fprintf(stdout, "\t[E] active event counter. \t(%s)\n", perf_evsel__name(top->sym_evsel)); 386 fprintf(stdout, "\t[E] active event counter. \t(%s)\n", perf_evsel__name(top->sym_evsel));
388 387
389 fprintf(stdout, "\t[f] profile display filter (count). \t(%d)\n", top->count_filter); 388 fprintf(stdout, "\t[f] profile display filter (count). \t(%d)\n", top->count_filter);
390 389
391 fprintf(stdout, "\t[F] annotate display filter (percent). \t(%d%%)\n", top->sym_pcnt_filter); 390 fprintf(stdout, "\t[F] annotate display filter (percent). \t(%d%%)\n", top->sym_pcnt_filter);
392 fprintf(stdout, "\t[s] annotate symbol. \t(%s)\n", name?: "NULL"); 391 fprintf(stdout, "\t[s] annotate symbol. \t(%s)\n", name?: "NULL");
393 fprintf(stdout, "\t[S] stop annotation.\n"); 392 fprintf(stdout, "\t[S] stop annotation.\n");
394 393
395 fprintf(stdout, 394 fprintf(stdout,
396 "\t[K] hide kernel_symbols symbols. \t(%s)\n", 395 "\t[K] hide kernel_symbols symbols. \t(%s)\n",
397 top->hide_kernel_symbols ? "yes" : "no"); 396 top->hide_kernel_symbols ? "yes" : "no");
398 fprintf(stdout, 397 fprintf(stdout,
399 "\t[U] hide user symbols. \t(%s)\n", 398 "\t[U] hide user symbols. \t(%s)\n",
400 top->hide_user_symbols ? "yes" : "no"); 399 top->hide_user_symbols ? "yes" : "no");
401 fprintf(stdout, "\t[z] toggle sample zeroing. \t(%d)\n", top->zero ? 1 : 0); 400 fprintf(stdout, "\t[z] toggle sample zeroing. \t(%d)\n", top->zero ? 1 : 0);
402 fprintf(stdout, "\t[qQ] quit.\n"); 401 fprintf(stdout, "\t[qQ] quit.\n");
403 } 402 }
404 403
405 static int perf_top__key_mapped(struct perf_top *top, int c) 404 static int perf_top__key_mapped(struct perf_top *top, int c)
406 { 405 {
407 switch (c) { 406 switch (c) {
408 case 'd': 407 case 'd':
409 case 'e': 408 case 'e':
410 case 'f': 409 case 'f':
411 case 'z': 410 case 'z':
412 case 'q': 411 case 'q':
413 case 'Q': 412 case 'Q':
414 case 'K': 413 case 'K':
415 case 'U': 414 case 'U':
416 case 'F': 415 case 'F':
417 case 's': 416 case 's':
418 case 'S': 417 case 'S':
419 return 1; 418 return 1;
420 case 'E': 419 case 'E':
421 return top->evlist->nr_entries > 1 ? 1 : 0; 420 return top->evlist->nr_entries > 1 ? 1 : 0;
422 default: 421 default:
423 break; 422 break;
424 } 423 }
425 424
426 return 0; 425 return 0;
427 } 426 }
428 427
429 static bool perf_top__handle_keypress(struct perf_top *top, int c) 428 static bool perf_top__handle_keypress(struct perf_top *top, int c)
430 { 429 {
431 bool ret = true; 430 bool ret = true;
432 431
433 if (!perf_top__key_mapped(top, c)) { 432 if (!perf_top__key_mapped(top, c)) {
434 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN }; 433 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
435 struct termios save; 434 struct termios save;
436 435
437 perf_top__print_mapped_keys(top); 436 perf_top__print_mapped_keys(top);
438 fprintf(stdout, "\nEnter selection, or unmapped key to continue: "); 437 fprintf(stdout, "\nEnter selection, or unmapped key to continue: ");
439 fflush(stdout); 438 fflush(stdout);
440 439
441 set_term_quiet_input(&save); 440 set_term_quiet_input(&save);
442 441
443 poll(&stdin_poll, 1, -1); 442 poll(&stdin_poll, 1, -1);
444 c = getc(stdin); 443 c = getc(stdin);
445 444
446 tcsetattr(0, TCSAFLUSH, &save); 445 tcsetattr(0, TCSAFLUSH, &save);
447 if (!perf_top__key_mapped(top, c)) 446 if (!perf_top__key_mapped(top, c))
448 return ret; 447 return ret;
449 } 448 }
450 449
451 switch (c) { 450 switch (c) {
452 case 'd': 451 case 'd':
453 prompt_integer(&top->delay_secs, "Enter display delay"); 452 prompt_integer(&top->delay_secs, "Enter display delay");
454 if (top->delay_secs < 1) 453 if (top->delay_secs < 1)
455 top->delay_secs = 1; 454 top->delay_secs = 1;
456 break; 455 break;
457 case 'e': 456 case 'e':
458 prompt_integer(&top->print_entries, "Enter display entries (lines)"); 457 prompt_integer(&top->print_entries, "Enter display entries (lines)");
459 if (top->print_entries == 0) { 458 if (top->print_entries == 0) {
460 struct sigaction act = { 459 struct sigaction act = {
461 .sa_sigaction = perf_top__sig_winch, 460 .sa_sigaction = perf_top__sig_winch,
462 .sa_flags = SA_SIGINFO, 461 .sa_flags = SA_SIGINFO,
463 }; 462 };
464 perf_top__sig_winch(SIGWINCH, NULL, top); 463 perf_top__sig_winch(SIGWINCH, NULL, top);
465 sigaction(SIGWINCH, &act, NULL); 464 sigaction(SIGWINCH, &act, NULL);
466 } else { 465 } else {
467 signal(SIGWINCH, SIG_DFL); 466 signal(SIGWINCH, SIG_DFL);
468 } 467 }
469 break; 468 break;
470 case 'E': 469 case 'E':
471 if (top->evlist->nr_entries > 1) { 470 if (top->evlist->nr_entries > 1) {
472 /* Select 0 as the default event: */ 471 /* Select 0 as the default event: */
473 int counter = 0; 472 int counter = 0;
474 473
475 fprintf(stderr, "\nAvailable events:"); 474 fprintf(stderr, "\nAvailable events:");
476 475
477 evlist__for_each(top->evlist, top->sym_evsel) 476 evlist__for_each(top->evlist, top->sym_evsel)
478 fprintf(stderr, "\n\t%d %s", top->sym_evsel->idx, perf_evsel__name(top->sym_evsel)); 477 fprintf(stderr, "\n\t%d %s", top->sym_evsel->idx, perf_evsel__name(top->sym_evsel));
479 478
480 prompt_integer(&counter, "Enter details event counter"); 479 prompt_integer(&counter, "Enter details event counter");
481 480
482 if (counter >= top->evlist->nr_entries) { 481 if (counter >= top->evlist->nr_entries) {
483 top->sym_evsel = perf_evlist__first(top->evlist); 482 top->sym_evsel = perf_evlist__first(top->evlist);
484 fprintf(stderr, "Sorry, no such event, using %s.\n", perf_evsel__name(top->sym_evsel)); 483 fprintf(stderr, "Sorry, no such event, using %s.\n", perf_evsel__name(top->sym_evsel));
485 sleep(1); 484 sleep(1);
486 break; 485 break;
487 } 486 }
488 evlist__for_each(top->evlist, top->sym_evsel) 487 evlist__for_each(top->evlist, top->sym_evsel)
489 if (top->sym_evsel->idx == counter) 488 if (top->sym_evsel->idx == counter)
490 break; 489 break;
491 } else 490 } else
492 top->sym_evsel = perf_evlist__first(top->evlist); 491 top->sym_evsel = perf_evlist__first(top->evlist);
493 break; 492 break;
494 case 'f': 493 case 'f':
495 prompt_integer(&top->count_filter, "Enter display event count filter"); 494 prompt_integer(&top->count_filter, "Enter display event count filter");
496 break; 495 break;
497 case 'F': 496 case 'F':
498 prompt_percent(&top->sym_pcnt_filter, 497 prompt_percent(&top->sym_pcnt_filter,
499 "Enter details display event filter (percent)"); 498 "Enter details display event filter (percent)");
500 break; 499 break;
501 case 'K': 500 case 'K':
502 top->hide_kernel_symbols = !top->hide_kernel_symbols; 501 top->hide_kernel_symbols = !top->hide_kernel_symbols;
503 break; 502 break;
504 case 'q': 503 case 'q':
505 case 'Q': 504 case 'Q':
506 printf("exiting.\n"); 505 printf("exiting.\n");
507 if (top->dump_symtab) 506 if (top->dump_symtab)
508 perf_session__fprintf_dsos(top->session, stderr); 507 perf_session__fprintf_dsos(top->session, stderr);
509 ret = false; 508 ret = false;
510 break; 509 break;
511 case 's': 510 case 's':
512 perf_top__prompt_symbol(top, "Enter details symbol"); 511 perf_top__prompt_symbol(top, "Enter details symbol");
513 break; 512 break;
514 case 'S': 513 case 'S':
515 if (!top->sym_filter_entry) 514 if (!top->sym_filter_entry)
516 break; 515 break;
517 else { 516 else {
518 struct hist_entry *syme = top->sym_filter_entry; 517 struct hist_entry *syme = top->sym_filter_entry;
519 518
520 top->sym_filter_entry = NULL; 519 top->sym_filter_entry = NULL;
521 __zero_source_counters(syme); 520 __zero_source_counters(syme);
522 } 521 }
523 break; 522 break;
524 case 'U': 523 case 'U':
525 top->hide_user_symbols = !top->hide_user_symbols; 524 top->hide_user_symbols = !top->hide_user_symbols;
526 break; 525 break;
527 case 'z': 526 case 'z':
528 top->zero = !top->zero; 527 top->zero = !top->zero;
529 break; 528 break;
530 default: 529 default:
531 break; 530 break;
532 } 531 }
533 532
534 return ret; 533 return ret;
535 } 534 }
536 535
537 static void perf_top__sort_new_samples(void *arg) 536 static void perf_top__sort_new_samples(void *arg)
538 { 537 {
539 struct perf_top *t = arg; 538 struct perf_top *t = arg;
540 struct hists *hists; 539 struct hists *hists;
541 540
542 perf_top__reset_sample_counters(t); 541 perf_top__reset_sample_counters(t);
543 542
544 if (t->evlist->selected != NULL) 543 if (t->evlist->selected != NULL)
545 t->sym_evsel = t->evlist->selected; 544 t->sym_evsel = t->evlist->selected;
546 545
547 hists = evsel__hists(t->sym_evsel); 546 hists = evsel__hists(t->sym_evsel);
548 547
549 if (t->zero) { 548 if (t->zero) {
550 hists__delete_entries(hists); 549 hists__delete_entries(hists);
551 } else { 550 } else {
552 hists__decay_entries(hists, t->hide_user_symbols, 551 hists__decay_entries(hists, t->hide_user_symbols,
553 t->hide_kernel_symbols); 552 t->hide_kernel_symbols);
554 } 553 }
555 554
556 hists__collapse_resort(hists, NULL); 555 hists__collapse_resort(hists, NULL);
557 hists__output_resort(hists, NULL); 556 hists__output_resort(hists, NULL);
558 } 557 }
559 558
560 static void *display_thread_tui(void *arg) 559 static void *display_thread_tui(void *arg)
561 { 560 {
562 struct perf_evsel *pos; 561 struct perf_evsel *pos;
563 struct perf_top *top = arg; 562 struct perf_top *top = arg;
564 const char *help = "For a higher level overview, try: perf top --sort comm,dso"; 563 const char *help = "For a higher level overview, try: perf top --sort comm,dso";
565 struct hist_browser_timer hbt = { 564 struct hist_browser_timer hbt = {
566 .timer = perf_top__sort_new_samples, 565 .timer = perf_top__sort_new_samples,
567 .arg = top, 566 .arg = top,
568 .refresh = top->delay_secs, 567 .refresh = top->delay_secs,
569 }; 568 };
570 569
571 perf_top__sort_new_samples(top); 570 perf_top__sort_new_samples(top);
572 571
573 /* 572 /*
574 * Initialize the uid_filter_str, in the future the TUI will allow 573 * Initialize the uid_filter_str, in the future the TUI will allow
575 * Zooming in/out UIDs. For now juse use whatever the user passed 574 * Zooming in/out UIDs. For now juse use whatever the user passed
576 * via --uid. 575 * via --uid.
577 */ 576 */
578 evlist__for_each(top->evlist, pos) { 577 evlist__for_each(top->evlist, pos) {
579 struct hists *hists = evsel__hists(pos); 578 struct hists *hists = evsel__hists(pos);
580 hists->uid_filter_str = top->record_opts.target.uid_str; 579 hists->uid_filter_str = top->record_opts.target.uid_str;
581 } 580 }
582 581
583 perf_evlist__tui_browse_hists(top->evlist, help, &hbt, top->min_percent, 582 perf_evlist__tui_browse_hists(top->evlist, help, &hbt, top->min_percent,
584 &top->session->header.env); 583 &top->session->header.env);
585 584
586 done = 1; 585 done = 1;
587 return NULL; 586 return NULL;
588 } 587 }
589 588
590 static void display_sig(int sig __maybe_unused) 589 static void display_sig(int sig __maybe_unused)
591 { 590 {
592 done = 1; 591 done = 1;
593 } 592 }
594 593
595 static void display_setup_sig(void) 594 static void display_setup_sig(void)
596 { 595 {
597 signal(SIGSEGV, display_sig); 596 signal(SIGSEGV, display_sig);
598 signal(SIGFPE, display_sig); 597 signal(SIGFPE, display_sig);
599 signal(SIGINT, display_sig); 598 signal(SIGINT, display_sig);
600 signal(SIGQUIT, display_sig); 599 signal(SIGQUIT, display_sig);
601 signal(SIGTERM, display_sig); 600 signal(SIGTERM, display_sig);
602 } 601 }
603 602
604 static void *display_thread(void *arg) 603 static void *display_thread(void *arg)
605 { 604 {
606 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN }; 605 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
607 struct termios save; 606 struct termios save;
608 struct perf_top *top = arg; 607 struct perf_top *top = arg;
609 int delay_msecs, c; 608 int delay_msecs, c;
610 609
611 display_setup_sig(); 610 display_setup_sig();
612 pthread__unblock_sigwinch(); 611 pthread__unblock_sigwinch();
613 repeat: 612 repeat:
614 delay_msecs = top->delay_secs * 1000; 613 delay_msecs = top->delay_secs * 1000;
615 set_term_quiet_input(&save); 614 set_term_quiet_input(&save);
616 /* trash return*/ 615 /* trash return*/
617 getc(stdin); 616 getc(stdin);
618 617
619 while (!done) { 618 while (!done) {
620 perf_top__print_sym_table(top); 619 perf_top__print_sym_table(top);
621 /* 620 /*
622 * Either timeout expired or we got an EINTR due to SIGWINCH, 621 * Either timeout expired or we got an EINTR due to SIGWINCH,
623 * refresh screen in both cases. 622 * refresh screen in both cases.
624 */ 623 */
625 switch (poll(&stdin_poll, 1, delay_msecs)) { 624 switch (poll(&stdin_poll, 1, delay_msecs)) {
626 case 0: 625 case 0:
627 continue; 626 continue;
628 case -1: 627 case -1:
629 if (errno == EINTR) 628 if (errno == EINTR)
630 continue; 629 continue;
631 /* Fall trhu */ 630 /* Fall trhu */
632 default: 631 default:
633 c = getc(stdin); 632 c = getc(stdin);
634 tcsetattr(0, TCSAFLUSH, &save); 633 tcsetattr(0, TCSAFLUSH, &save);
635 634
636 if (perf_top__handle_keypress(top, c)) 635 if (perf_top__handle_keypress(top, c))
637 goto repeat; 636 goto repeat;
638 done = 1; 637 done = 1;
639 } 638 }
640 } 639 }
641 640
642 tcsetattr(0, TCSAFLUSH, &save); 641 tcsetattr(0, TCSAFLUSH, &save);
643 return NULL; 642 return NULL;
644 } 643 }
645 644
646 static int symbol_filter(struct map *map, struct symbol *sym) 645 static int symbol_filter(struct map *map, struct symbol *sym)
647 { 646 {
648 const char *name = sym->name; 647 const char *name = sym->name;
649 648
650 if (!map->dso->kernel) 649 if (!map->dso->kernel)
651 return 0; 650 return 0;
652 /* 651 /*
653 * ppc64 uses function descriptors and appends a '.' to the 652 * ppc64 uses function descriptors and appends a '.' to the
654 * start of every instruction address. Remove it. 653 * start of every instruction address. Remove it.
655 */ 654 */
656 if (name[0] == '.') 655 if (name[0] == '.')
657 name++; 656 name++;
658 657
659 if (!strcmp(name, "_text") || 658 if (!strcmp(name, "_text") ||
660 !strcmp(name, "_etext") || 659 !strcmp(name, "_etext") ||
661 !strcmp(name, "_sinittext") || 660 !strcmp(name, "_sinittext") ||
662 !strncmp("init_module", name, 11) || 661 !strncmp("init_module", name, 11) ||
663 !strncmp("cleanup_module", name, 14) || 662 !strncmp("cleanup_module", name, 14) ||
664 strstr(name, "_text_start") || 663 strstr(name, "_text_start") ||
665 strstr(name, "_text_end")) 664 strstr(name, "_text_end"))
666 return 1; 665 return 1;
667 666
668 if (symbol__is_idle(sym)) 667 if (symbol__is_idle(sym))
669 sym->ignore = true; 668 sym->ignore = true;
670 669
671 return 0; 670 return 0;
672 } 671 }
673 672
674 static int hist_iter__top_callback(struct hist_entry_iter *iter, 673 static int hist_iter__top_callback(struct hist_entry_iter *iter,
675 struct addr_location *al, bool single, 674 struct addr_location *al, bool single,
676 void *arg) 675 void *arg)
677 { 676 {
678 struct perf_top *top = arg; 677 struct perf_top *top = arg;
679 struct hist_entry *he = iter->he; 678 struct hist_entry *he = iter->he;
680 struct perf_evsel *evsel = iter->evsel; 679 struct perf_evsel *evsel = iter->evsel;
681 680
682 if (sort__has_sym && single) { 681 if (sort__has_sym && single) {
683 u64 ip = al->addr; 682 u64 ip = al->addr;
684 683
685 if (al->map) 684 if (al->map)
686 ip = al->map->unmap_ip(al->map, ip); 685 ip = al->map->unmap_ip(al->map, ip);
687 686
688 perf_top__record_precise_ip(top, he, evsel->idx, ip); 687 perf_top__record_precise_ip(top, he, evsel->idx, ip);
689 } 688 }
690 689
691 return 0; 690 return 0;
692 } 691 }
693 692
694 static void perf_event__process_sample(struct perf_tool *tool, 693 static void perf_event__process_sample(struct perf_tool *tool,
695 const union perf_event *event, 694 const union perf_event *event,
696 struct perf_evsel *evsel, 695 struct perf_evsel *evsel,
697 struct perf_sample *sample, 696 struct perf_sample *sample,
698 struct machine *machine) 697 struct machine *machine)
699 { 698 {
700 struct perf_top *top = container_of(tool, struct perf_top, tool); 699 struct perf_top *top = container_of(tool, struct perf_top, tool);
701 struct addr_location al; 700 struct addr_location al;
702 int err; 701 int err;
703 702
704 if (!machine && perf_guest) { 703 if (!machine && perf_guest) {
705 static struct intlist *seen; 704 static struct intlist *seen;
706 705
707 if (!seen) 706 if (!seen)
708 seen = intlist__new(NULL); 707 seen = intlist__new(NULL);
709 708
710 if (!intlist__has_entry(seen, sample->pid)) { 709 if (!intlist__has_entry(seen, sample->pid)) {
711 pr_err("Can't find guest [%d]'s kernel information\n", 710 pr_err("Can't find guest [%d]'s kernel information\n",
712 sample->pid); 711 sample->pid);
713 intlist__add(seen, sample->pid); 712 intlist__add(seen, sample->pid);
714 } 713 }
715 return; 714 return;
716 } 715 }
717 716
718 if (!machine) { 717 if (!machine) {
719 pr_err("%u unprocessable samples recorded.\r", 718 pr_err("%u unprocessable samples recorded.\r",
720 top->session->stats.nr_unprocessable_samples++); 719 top->session->stats.nr_unprocessable_samples++);
721 return; 720 return;
722 } 721 }
723 722
724 if (event->header.misc & PERF_RECORD_MISC_EXACT_IP) 723 if (event->header.misc & PERF_RECORD_MISC_EXACT_IP)
725 top->exact_samples++; 724 top->exact_samples++;
726 725
727 if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) 726 if (perf_event__preprocess_sample(event, machine, &al, sample) < 0)
728 return; 727 return;
729 728
730 if (!top->kptr_restrict_warned && 729 if (!top->kptr_restrict_warned &&
731 symbol_conf.kptr_restrict && 730 symbol_conf.kptr_restrict &&
732 al.cpumode == PERF_RECORD_MISC_KERNEL) { 731 al.cpumode == PERF_RECORD_MISC_KERNEL) {
733 ui__warning( 732 ui__warning(
734 "Kernel address maps (/proc/{kallsyms,modules}) are restricted.\n\n" 733 "Kernel address maps (/proc/{kallsyms,modules}) are restricted.\n\n"
735 "Check /proc/sys/kernel/kptr_restrict.\n\n" 734 "Check /proc/sys/kernel/kptr_restrict.\n\n"
736 "Kernel%s samples will not be resolved.\n", 735 "Kernel%s samples will not be resolved.\n",
737 !RB_EMPTY_ROOT(&al.map->dso->symbols[MAP__FUNCTION]) ? 736 !RB_EMPTY_ROOT(&al.map->dso->symbols[MAP__FUNCTION]) ?
738 " modules" : ""); 737 " modules" : "");
739 if (use_browser <= 0) 738 if (use_browser <= 0)
740 sleep(5); 739 sleep(5);
741 top->kptr_restrict_warned = true; 740 top->kptr_restrict_warned = true;
742 } 741 }
743 742
744 if (al.sym == NULL) { 743 if (al.sym == NULL) {
745 const char *msg = "Kernel samples will not be resolved.\n"; 744 const char *msg = "Kernel samples will not be resolved.\n";
746 /* 745 /*
747 * As we do lazy loading of symtabs we only will know if the 746 * As we do lazy loading of symtabs we only will know if the
748 * specified vmlinux file is invalid when we actually have a 747 * specified vmlinux file is invalid when we actually have a
749 * hit in kernel space and then try to load it. So if we get 748 * hit in kernel space and then try to load it. So if we get
750 * here and there are _no_ symbols in the DSO backing the 749 * here and there are _no_ symbols in the DSO backing the
751 * kernel map, bail out. 750 * kernel map, bail out.
752 * 751 *
753 * We may never get here, for instance, if we use -K/ 752 * We may never get here, for instance, if we use -K/
754 * --hide-kernel-symbols, even if the user specifies an 753 * --hide-kernel-symbols, even if the user specifies an
755 * invalid --vmlinux ;-) 754 * invalid --vmlinux ;-)
756 */ 755 */
757 if (!top->kptr_restrict_warned && !top->vmlinux_warned && 756 if (!top->kptr_restrict_warned && !top->vmlinux_warned &&
758 al.map == machine->vmlinux_maps[MAP__FUNCTION] && 757 al.map == machine->vmlinux_maps[MAP__FUNCTION] &&
759 RB_EMPTY_ROOT(&al.map->dso->symbols[MAP__FUNCTION])) { 758 RB_EMPTY_ROOT(&al.map->dso->symbols[MAP__FUNCTION])) {
760 if (symbol_conf.vmlinux_name) { 759 if (symbol_conf.vmlinux_name) {
761 ui__warning("The %s file can't be used.\n%s", 760 ui__warning("The %s file can't be used.\n%s",
762 symbol_conf.vmlinux_name, msg); 761 symbol_conf.vmlinux_name, msg);
763 } else { 762 } else {
764 ui__warning("A vmlinux file was not found.\n%s", 763 ui__warning("A vmlinux file was not found.\n%s",
765 msg); 764 msg);
766 } 765 }
767 766
768 if (use_browser <= 0) 767 if (use_browser <= 0)
769 sleep(5); 768 sleep(5);
770 top->vmlinux_warned = true; 769 top->vmlinux_warned = true;
771 } 770 }
772 } 771 }
773 772
774 if (al.sym == NULL || !al.sym->ignore) { 773 if (al.sym == NULL || !al.sym->ignore) {
775 struct hists *hists = evsel__hists(evsel); 774 struct hists *hists = evsel__hists(evsel);
776 struct hist_entry_iter iter = { 775 struct hist_entry_iter iter = {
777 .add_entry_cb = hist_iter__top_callback, 776 .add_entry_cb = hist_iter__top_callback,
778 }; 777 };
779 778
780 if (symbol_conf.cumulate_callchain) 779 if (symbol_conf.cumulate_callchain)
781 iter.ops = &hist_iter_cumulative; 780 iter.ops = &hist_iter_cumulative;
782 else 781 else
783 iter.ops = &hist_iter_normal; 782 iter.ops = &hist_iter_normal;
784 783
785 pthread_mutex_lock(&hists->lock); 784 pthread_mutex_lock(&hists->lock);
786 785
787 err = hist_entry_iter__add(&iter, &al, evsel, sample, 786 err = hist_entry_iter__add(&iter, &al, evsel, sample,
788 top->max_stack, top); 787 top->max_stack, top);
789 if (err < 0) 788 if (err < 0)
790 pr_err("Problem incrementing symbol period, skipping event\n"); 789 pr_err("Problem incrementing symbol period, skipping event\n");
791 790
792 pthread_mutex_unlock(&hists->lock); 791 pthread_mutex_unlock(&hists->lock);
793 } 792 }
794 793
795 return; 794 return;
796 } 795 }
797 796
798 static void perf_top__mmap_read_idx(struct perf_top *top, int idx) 797 static void perf_top__mmap_read_idx(struct perf_top *top, int idx)
799 { 798 {
800 struct perf_sample sample; 799 struct perf_sample sample;
801 struct perf_evsel *evsel; 800 struct perf_evsel *evsel;
802 struct perf_session *session = top->session; 801 struct perf_session *session = top->session;
803 union perf_event *event; 802 union perf_event *event;
804 struct machine *machine; 803 struct machine *machine;
805 u8 origin; 804 u8 origin;
806 int ret; 805 int ret;
807 806
808 while ((event = perf_evlist__mmap_read(top->evlist, idx)) != NULL) { 807 while ((event = perf_evlist__mmap_read(top->evlist, idx)) != NULL) {
809 ret = perf_evlist__parse_sample(top->evlist, event, &sample); 808 ret = perf_evlist__parse_sample(top->evlist, event, &sample);
810 if (ret) { 809 if (ret) {
811 pr_err("Can't parse sample, err = %d\n", ret); 810 pr_err("Can't parse sample, err = %d\n", ret);
812 goto next_event; 811 goto next_event;
813 } 812 }
814 813
815 evsel = perf_evlist__id2evsel(session->evlist, sample.id); 814 evsel = perf_evlist__id2evsel(session->evlist, sample.id);
816 assert(evsel != NULL); 815 assert(evsel != NULL);
817 816
818 origin = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; 817 origin = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
819 818
820 if (event->header.type == PERF_RECORD_SAMPLE) 819 if (event->header.type == PERF_RECORD_SAMPLE)
821 ++top->samples; 820 ++top->samples;
822 821
823 switch (origin) { 822 switch (origin) {
824 case PERF_RECORD_MISC_USER: 823 case PERF_RECORD_MISC_USER:
825 ++top->us_samples; 824 ++top->us_samples;
826 if (top->hide_user_symbols) 825 if (top->hide_user_symbols)
827 goto next_event; 826 goto next_event;
828 machine = &session->machines.host; 827 machine = &session->machines.host;
829 break; 828 break;
830 case PERF_RECORD_MISC_KERNEL: 829 case PERF_RECORD_MISC_KERNEL:
831 ++top->kernel_samples; 830 ++top->kernel_samples;
832 if (top->hide_kernel_symbols) 831 if (top->hide_kernel_symbols)
833 goto next_event; 832 goto next_event;
834 machine = &session->machines.host; 833 machine = &session->machines.host;
835 break; 834 break;
836 case PERF_RECORD_MISC_GUEST_KERNEL: 835 case PERF_RECORD_MISC_GUEST_KERNEL:
837 ++top->guest_kernel_samples; 836 ++top->guest_kernel_samples;
838 machine = perf_session__find_machine(session, 837 machine = perf_session__find_machine(session,
839 sample.pid); 838 sample.pid);
840 break; 839 break;
841 case PERF_RECORD_MISC_GUEST_USER: 840 case PERF_RECORD_MISC_GUEST_USER:
842 ++top->guest_us_samples; 841 ++top->guest_us_samples;
843 /* 842 /*
844 * TODO: we don't process guest user from host side 843 * TODO: we don't process guest user from host side
845 * except simple counting. 844 * except simple counting.
846 */ 845 */
847 /* Fall thru */ 846 /* Fall thru */
848 default: 847 default:
849 goto next_event; 848 goto next_event;
850 } 849 }
851 850
852 851
853 if (event->header.type == PERF_RECORD_SAMPLE) { 852 if (event->header.type == PERF_RECORD_SAMPLE) {
854 perf_event__process_sample(&top->tool, event, evsel, 853 perf_event__process_sample(&top->tool, event, evsel,
855 &sample, machine); 854 &sample, machine);
856 } else if (event->header.type < PERF_RECORD_MAX) { 855 } else if (event->header.type < PERF_RECORD_MAX) {
857 hists__inc_nr_events(evsel__hists(evsel), event->header.type); 856 hists__inc_nr_events(evsel__hists(evsel), event->header.type);
858 machine__process_event(machine, event, &sample); 857 machine__process_event(machine, event, &sample);
859 } else 858 } else
860 ++session->stats.nr_unknown_events; 859 ++session->stats.nr_unknown_events;
861 next_event: 860 next_event:
862 perf_evlist__mmap_consume(top->evlist, idx); 861 perf_evlist__mmap_consume(top->evlist, idx);
863 } 862 }
864 } 863 }
865 864
866 static void perf_top__mmap_read(struct perf_top *top) 865 static void perf_top__mmap_read(struct perf_top *top)
867 { 866 {
868 int i; 867 int i;
869 868
870 for (i = 0; i < top->evlist->nr_mmaps; i++) 869 for (i = 0; i < top->evlist->nr_mmaps; i++)
871 perf_top__mmap_read_idx(top, i); 870 perf_top__mmap_read_idx(top, i);
872 } 871 }
873 872
874 static int perf_top__start_counters(struct perf_top *top) 873 static int perf_top__start_counters(struct perf_top *top)
875 { 874 {
876 char msg[512]; 875 char msg[512];
877 struct perf_evsel *counter; 876 struct perf_evsel *counter;
878 struct perf_evlist *evlist = top->evlist; 877 struct perf_evlist *evlist = top->evlist;
879 struct record_opts *opts = &top->record_opts; 878 struct record_opts *opts = &top->record_opts;
880 879
881 perf_evlist__config(evlist, opts); 880 perf_evlist__config(evlist, opts);
882 881
883 evlist__for_each(evlist, counter) { 882 evlist__for_each(evlist, counter) {
884 try_again: 883 try_again:
885 if (perf_evsel__open(counter, top->evlist->cpus, 884 if (perf_evsel__open(counter, top->evlist->cpus,
886 top->evlist->threads) < 0) { 885 top->evlist->threads) < 0) {
887 if (perf_evsel__fallback(counter, errno, msg, sizeof(msg))) { 886 if (perf_evsel__fallback(counter, errno, msg, sizeof(msg))) {
888 if (verbose) 887 if (verbose)
889 ui__warning("%s\n", msg); 888 ui__warning("%s\n", msg);
890 goto try_again; 889 goto try_again;
891 } 890 }
892 891
893 perf_evsel__open_strerror(counter, &opts->target, 892 perf_evsel__open_strerror(counter, &opts->target,
894 errno, msg, sizeof(msg)); 893 errno, msg, sizeof(msg));
895 ui__error("%s\n", msg); 894 ui__error("%s\n", msg);
896 goto out_err; 895 goto out_err;
897 } 896 }
898 } 897 }
899 898
900 if (perf_evlist__mmap(evlist, opts->mmap_pages, false) < 0) { 899 if (perf_evlist__mmap(evlist, opts->mmap_pages, false) < 0) {
901 ui__error("Failed to mmap with %d (%s)\n", 900 ui__error("Failed to mmap with %d (%s)\n",
902 errno, strerror_r(errno, msg, sizeof(msg))); 901 errno, strerror_r(errno, msg, sizeof(msg)));
903 goto out_err; 902 goto out_err;
904 } 903 }
905 904
906 return 0; 905 return 0;
907 906
908 out_err: 907 out_err:
909 return -1; 908 return -1;
910 } 909 }
911 910
912 static int perf_top__setup_sample_type(struct perf_top *top __maybe_unused) 911 static int perf_top__setup_sample_type(struct perf_top *top __maybe_unused)
913 { 912 {
914 if (!sort__has_sym) { 913 if (!sort__has_sym) {
915 if (symbol_conf.use_callchain) { 914 if (symbol_conf.use_callchain) {
916 ui__error("Selected -g but \"sym\" not present in --sort/-s."); 915 ui__error("Selected -g but \"sym\" not present in --sort/-s.");
917 return -EINVAL; 916 return -EINVAL;
918 } 917 }
919 } else if (callchain_param.mode != CHAIN_NONE) { 918 } else if (callchain_param.mode != CHAIN_NONE) {
920 if (callchain_register_param(&callchain_param) < 0) { 919 if (callchain_register_param(&callchain_param) < 0) {
921 ui__error("Can't register callchain params.\n"); 920 ui__error("Can't register callchain params.\n");
922 return -EINVAL; 921 return -EINVAL;
923 } 922 }
924 } 923 }
925 924
926 return 0; 925 return 0;
927 } 926 }
928 927
929 static int __cmd_top(struct perf_top *top) 928 static int __cmd_top(struct perf_top *top)
930 { 929 {
931 struct record_opts *opts = &top->record_opts; 930 struct record_opts *opts = &top->record_opts;
932 pthread_t thread; 931 pthread_t thread;
933 int ret; 932 int ret;
934 933
935 top->session = perf_session__new(NULL, false, NULL); 934 top->session = perf_session__new(NULL, false, NULL);
936 if (top->session == NULL) 935 if (top->session == NULL)
937 return -1; 936 return -1;
938 937
939 machines__set_symbol_filter(&top->session->machines, symbol_filter); 938 machines__set_symbol_filter(&top->session->machines, symbol_filter);
940 939
941 if (!objdump_path) { 940 if (!objdump_path) {
942 ret = perf_session_env__lookup_objdump(&top->session->header.env); 941 ret = perf_session_env__lookup_objdump(&top->session->header.env);
943 if (ret) 942 if (ret)
944 goto out_delete; 943 goto out_delete;
945 } 944 }
946 945
947 ret = perf_top__setup_sample_type(top); 946 ret = perf_top__setup_sample_type(top);
948 if (ret) 947 if (ret)
949 goto out_delete; 948 goto out_delete;
950 949
951 machine__synthesize_threads(&top->session->machines.host, &opts->target, 950 machine__synthesize_threads(&top->session->machines.host, &opts->target,
952 top->evlist->threads, false); 951 top->evlist->threads, false);
953 ret = perf_top__start_counters(top); 952 ret = perf_top__start_counters(top);
954 if (ret) 953 if (ret)
955 goto out_delete; 954 goto out_delete;
956 955
957 top->session->evlist = top->evlist; 956 top->session->evlist = top->evlist;
958 perf_session__set_id_hdr_size(top->session); 957 perf_session__set_id_hdr_size(top->session);
959 958
960 /* 959 /*
961 * When perf is starting the traced process, all the events (apart from 960 * When perf is starting the traced process, all the events (apart from
962 * group members) have enable_on_exec=1 set, so don't spoil it by 961 * group members) have enable_on_exec=1 set, so don't spoil it by
963 * prematurely enabling them. 962 * prematurely enabling them.
964 * 963 *
965 * XXX 'top' still doesn't start workloads like record, trace, but should, 964 * XXX 'top' still doesn't start workloads like record, trace, but should,
966 * so leave the check here. 965 * so leave the check here.
967 */ 966 */
968 if (!target__none(&opts->target)) 967 if (!target__none(&opts->target))
969 perf_evlist__enable(top->evlist); 968 perf_evlist__enable(top->evlist);
970 969
971 /* Wait for a minimal set of events before starting the snapshot */ 970 /* Wait for a minimal set of events before starting the snapshot */
972 perf_evlist__poll(top->evlist, 100); 971 perf_evlist__poll(top->evlist, 100);
973 972
974 perf_top__mmap_read(top); 973 perf_top__mmap_read(top);
975 974
976 ret = -1; 975 ret = -1;
977 if (pthread_create(&thread, NULL, (use_browser > 0 ? display_thread_tui : 976 if (pthread_create(&thread, NULL, (use_browser > 0 ? display_thread_tui :
978 display_thread), top)) { 977 display_thread), top)) {
979 ui__error("Could not create display thread.\n"); 978 ui__error("Could not create display thread.\n");
980 goto out_delete; 979 goto out_delete;
981 } 980 }
982 981
983 if (top->realtime_prio) { 982 if (top->realtime_prio) {
984 struct sched_param param; 983 struct sched_param param;
985 984
986 param.sched_priority = top->realtime_prio; 985 param.sched_priority = top->realtime_prio;
987 if (sched_setscheduler(0, SCHED_FIFO, &param)) { 986 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
988 ui__error("Could not set realtime priority.\n"); 987 ui__error("Could not set realtime priority.\n");
989 goto out_join; 988 goto out_join;
990 } 989 }
991 } 990 }
992 991
993 while (!done) { 992 while (!done) {
994 u64 hits = top->samples; 993 u64 hits = top->samples;
995 994
996 perf_top__mmap_read(top); 995 perf_top__mmap_read(top);
997 996
998 if (hits == top->samples) 997 if (hits == top->samples)
999 ret = perf_evlist__poll(top->evlist, 100); 998 ret = perf_evlist__poll(top->evlist, 100);
1000 } 999 }
1001 1000
1002 ret = 0; 1001 ret = 0;
1003 out_join: 1002 out_join:
1004 pthread_join(thread, NULL); 1003 pthread_join(thread, NULL);
1005 out_delete: 1004 out_delete:
1006 perf_session__delete(top->session); 1005 perf_session__delete(top->session);
1007 top->session = NULL; 1006 top->session = NULL;
1008 1007
1009 return ret; 1008 return ret;
1010 } 1009 }
1011 1010
1012 static int 1011 static int
1013 callchain_opt(const struct option *opt, const char *arg, int unset) 1012 callchain_opt(const struct option *opt, const char *arg, int unset)
1014 { 1013 {
1015 symbol_conf.use_callchain = true; 1014 symbol_conf.use_callchain = true;
1016 return record_callchain_opt(opt, arg, unset); 1015 return record_callchain_opt(opt, arg, unset);
1017 } 1016 }
1018 1017
1019 static int 1018 static int
1020 parse_callchain_opt(const struct option *opt, const char *arg, int unset) 1019 parse_callchain_opt(const struct option *opt, const char *arg, int unset)
1021 { 1020 {
1022 symbol_conf.use_callchain = true; 1021 symbol_conf.use_callchain = true;
1023 return record_parse_callchain_opt(opt, arg, unset); 1022 return record_parse_callchain_opt(opt, arg, unset);
1024 } 1023 }
1025 1024
1026 static int perf_top_config(const char *var, const char *value, void *cb) 1025 static int perf_top_config(const char *var, const char *value, void *cb)
1027 { 1026 {
1028 if (!strcmp(var, "top.call-graph")) 1027 if (!strcmp(var, "top.call-graph"))
1029 var = "call-graph.record-mode"; /* fall-through */ 1028 var = "call-graph.record-mode"; /* fall-through */
1030 if (!strcmp(var, "top.children")) { 1029 if (!strcmp(var, "top.children")) {
1031 symbol_conf.cumulate_callchain = perf_config_bool(var, value); 1030 symbol_conf.cumulate_callchain = perf_config_bool(var, value);
1032 return 0; 1031 return 0;
1033 } 1032 }
1034 1033
1035 return perf_default_config(var, value, cb); 1034 return perf_default_config(var, value, cb);
1036 } 1035 }
1037 1036
1038 static int 1037 static int
1039 parse_percent_limit(const struct option *opt, const char *arg, 1038 parse_percent_limit(const struct option *opt, const char *arg,
1040 int unset __maybe_unused) 1039 int unset __maybe_unused)
1041 { 1040 {
1042 struct perf_top *top = opt->value; 1041 struct perf_top *top = opt->value;
1043 1042
1044 top->min_percent = strtof(arg, NULL); 1043 top->min_percent = strtof(arg, NULL);
1045 return 0; 1044 return 0;
1046 } 1045 }
1047 1046
1048 int cmd_top(int argc, const char **argv, const char *prefix __maybe_unused) 1047 int cmd_top(int argc, const char **argv, const char *prefix __maybe_unused)
1049 { 1048 {
1050 char errbuf[BUFSIZ]; 1049 char errbuf[BUFSIZ];
1051 struct perf_top top = { 1050 struct perf_top top = {
1052 .count_filter = 5, 1051 .count_filter = 5,
1053 .delay_secs = 2, 1052 .delay_secs = 2,
1054 .record_opts = { 1053 .record_opts = {
1055 .mmap_pages = UINT_MAX, 1054 .mmap_pages = UINT_MAX,
1056 .user_freq = UINT_MAX, 1055 .user_freq = UINT_MAX,
1057 .user_interval = ULLONG_MAX, 1056 .user_interval = ULLONG_MAX,
1058 .freq = 4000, /* 4 KHz */ 1057 .freq = 4000, /* 4 KHz */
1059 .target = { 1058 .target = {
1060 .uses_mmap = true, 1059 .uses_mmap = true,
1061 }, 1060 },
1062 }, 1061 },
1063 .max_stack = PERF_MAX_STACK_DEPTH, 1062 .max_stack = PERF_MAX_STACK_DEPTH,
1064 .sym_pcnt_filter = 5, 1063 .sym_pcnt_filter = 5,
1065 }; 1064 };
1066 struct record_opts *opts = &top.record_opts; 1065 struct record_opts *opts = &top.record_opts;
1067 struct target *target = &opts->target; 1066 struct target *target = &opts->target;
1068 const struct option options[] = { 1067 const struct option options[] = {
1069 OPT_CALLBACK('e', "event", &top.evlist, "event", 1068 OPT_CALLBACK('e', "event", &top.evlist, "event",
1070 "event selector. use 'perf list' to list available events", 1069 "event selector. use 'perf list' to list available events",
1071 parse_events_option), 1070 parse_events_option),
1072 OPT_U64('c', "count", &opts->user_interval, "event period to sample"), 1071 OPT_U64('c', "count", &opts->user_interval, "event period to sample"),
1073 OPT_STRING('p', "pid", &target->pid, "pid", 1072 OPT_STRING('p', "pid", &target->pid, "pid",
1074 "profile events on existing process id"), 1073 "profile events on existing process id"),
1075 OPT_STRING('t', "tid", &target->tid, "tid", 1074 OPT_STRING('t', "tid", &target->tid, "tid",
1076 "profile events on existing thread id"), 1075 "profile events on existing thread id"),
1077 OPT_BOOLEAN('a', "all-cpus", &target->system_wide, 1076 OPT_BOOLEAN('a', "all-cpus", &target->system_wide,
1078 "system-wide collection from all CPUs"), 1077 "system-wide collection from all CPUs"),
1079 OPT_STRING('C', "cpu", &target->cpu_list, "cpu", 1078 OPT_STRING('C', "cpu", &target->cpu_list, "cpu",
1080 "list of cpus to monitor"), 1079 "list of cpus to monitor"),
1081 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name, 1080 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1082 "file", "vmlinux pathname"), 1081 "file", "vmlinux pathname"),
1083 OPT_BOOLEAN(0, "ignore-vmlinux", &symbol_conf.ignore_vmlinux, 1082 OPT_BOOLEAN(0, "ignore-vmlinux", &symbol_conf.ignore_vmlinux,
1084 "don't load vmlinux even if found"), 1083 "don't load vmlinux even if found"),
1085 OPT_BOOLEAN('K', "hide_kernel_symbols", &top.hide_kernel_symbols, 1084 OPT_BOOLEAN('K', "hide_kernel_symbols", &top.hide_kernel_symbols,
1086 "hide kernel symbols"), 1085 "hide kernel symbols"),
1087 OPT_CALLBACK('m', "mmap-pages", &opts->mmap_pages, "pages", 1086 OPT_CALLBACK('m', "mmap-pages", &opts->mmap_pages, "pages",
1088 "number of mmap data pages", 1087 "number of mmap data pages",
1089 perf_evlist__parse_mmap_pages), 1088 perf_evlist__parse_mmap_pages),
1090 OPT_INTEGER('r', "realtime", &top.realtime_prio, 1089 OPT_INTEGER('r', "realtime", &top.realtime_prio,
1091 "collect data with this RT SCHED_FIFO priority"), 1090 "collect data with this RT SCHED_FIFO priority"),
1092 OPT_INTEGER('d', "delay", &top.delay_secs, 1091 OPT_INTEGER('d', "delay", &top.delay_secs,
1093 "number of seconds to delay between refreshes"), 1092 "number of seconds to delay between refreshes"),
1094 OPT_BOOLEAN('D', "dump-symtab", &top.dump_symtab, 1093 OPT_BOOLEAN('D', "dump-symtab", &top.dump_symtab,
1095 "dump the symbol table used for profiling"), 1094 "dump the symbol table used for profiling"),
1096 OPT_INTEGER('f', "count-filter", &top.count_filter, 1095 OPT_INTEGER('f', "count-filter", &top.count_filter,
1097 "only display functions with more events than this"), 1096 "only display functions with more events than this"),
1098 OPT_BOOLEAN(0, "group", &opts->group, 1097 OPT_BOOLEAN(0, "group", &opts->group,
1099 "put the counters into a counter group"), 1098 "put the counters into a counter group"),
1100 OPT_BOOLEAN('i', "no-inherit", &opts->no_inherit, 1099 OPT_BOOLEAN('i', "no-inherit", &opts->no_inherit,
1101 "child tasks do not inherit counters"), 1100 "child tasks do not inherit counters"),
1102 OPT_STRING(0, "sym-annotate", &top.sym_filter, "symbol name", 1101 OPT_STRING(0, "sym-annotate", &top.sym_filter, "symbol name",
1103 "symbol to annotate"), 1102 "symbol to annotate"),
1104 OPT_BOOLEAN('z', "zero", &top.zero, "zero history across updates"), 1103 OPT_BOOLEAN('z', "zero", &top.zero, "zero history across updates"),
1105 OPT_UINTEGER('F', "freq", &opts->user_freq, "profile at this frequency"), 1104 OPT_UINTEGER('F', "freq", &opts->user_freq, "profile at this frequency"),
1106 OPT_INTEGER('E', "entries", &top.print_entries, 1105 OPT_INTEGER('E', "entries", &top.print_entries,
1107 "display this many functions"), 1106 "display this many functions"),
1108 OPT_BOOLEAN('U', "hide_user_symbols", &top.hide_user_symbols, 1107 OPT_BOOLEAN('U', "hide_user_symbols", &top.hide_user_symbols,
1109 "hide user symbols"), 1108 "hide user symbols"),
1110 OPT_BOOLEAN(0, "tui", &top.use_tui, "Use the TUI interface"), 1109 OPT_BOOLEAN(0, "tui", &top.use_tui, "Use the TUI interface"),
1111 OPT_BOOLEAN(0, "stdio", &top.use_stdio, "Use the stdio interface"), 1110 OPT_BOOLEAN(0, "stdio", &top.use_stdio, "Use the stdio interface"),
1112 OPT_INCR('v', "verbose", &verbose, 1111 OPT_INCR('v', "verbose", &verbose,
1113 "be more verbose (show counter open errors, etc)"), 1112 "be more verbose (show counter open errors, etc)"),
1114 OPT_STRING('s', "sort", &sort_order, "key[,key2...]", 1113 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1115 "sort by key(s): pid, comm, dso, symbol, parent, cpu, srcline, ..." 1114 "sort by key(s): pid, comm, dso, symbol, parent, cpu, srcline, ..."
1116 " Please refer the man page for the complete list."), 1115 " Please refer the man page for the complete list."),
1117 OPT_STRING(0, "fields", &field_order, "key[,keys...]", 1116 OPT_STRING(0, "fields", &field_order, "key[,keys...]",
1118 "output field(s): overhead, period, sample plus all of sort keys"), 1117 "output field(s): overhead, period, sample plus all of sort keys"),
1119 OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples, 1118 OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
1120 "Show a column with the number of samples"), 1119 "Show a column with the number of samples"),
1121 OPT_CALLBACK_NOOPT('g', NULL, &top.record_opts, 1120 OPT_CALLBACK_NOOPT('g', NULL, &top.record_opts,
1122 NULL, "enables call-graph recording", 1121 NULL, "enables call-graph recording",
1123 &callchain_opt), 1122 &callchain_opt),
1124 OPT_CALLBACK(0, "call-graph", &top.record_opts, 1123 OPT_CALLBACK(0, "call-graph", &top.record_opts,
1125 "mode[,dump_size]", record_callchain_help, 1124 "mode[,dump_size]", record_callchain_help,
1126 &parse_callchain_opt), 1125 &parse_callchain_opt),
1127 OPT_BOOLEAN(0, "children", &symbol_conf.cumulate_callchain, 1126 OPT_BOOLEAN(0, "children", &symbol_conf.cumulate_callchain,
1128 "Accumulate callchains of children and show total overhead as well"), 1127 "Accumulate callchains of children and show total overhead as well"),
1129 OPT_INTEGER(0, "max-stack", &top.max_stack, 1128 OPT_INTEGER(0, "max-stack", &top.max_stack,
1130 "Set the maximum stack depth when parsing the callchain. " 1129 "Set the maximum stack depth when parsing the callchain. "
1131 "Default: " __stringify(PERF_MAX_STACK_DEPTH)), 1130 "Default: " __stringify(PERF_MAX_STACK_DEPTH)),
1132 OPT_CALLBACK(0, "ignore-callees", NULL, "regex", 1131 OPT_CALLBACK(0, "ignore-callees", NULL, "regex",
1133 "ignore callees of these functions in call graphs", 1132 "ignore callees of these functions in call graphs",
1134 report_parse_ignore_callees_opt), 1133 report_parse_ignore_callees_opt),
1135 OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period, 1134 OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period,
1136 "Show a column with the sum of periods"), 1135 "Show a column with the sum of periods"),
1137 OPT_STRING(0, "dsos", &symbol_conf.dso_list_str, "dso[,dso...]", 1136 OPT_STRING(0, "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
1138 "only consider symbols in these dsos"), 1137 "only consider symbols in these dsos"),
1139 OPT_STRING(0, "comms", &symbol_conf.comm_list_str, "comm[,comm...]", 1138 OPT_STRING(0, "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
1140 "only consider symbols in these comms"), 1139 "only consider symbols in these comms"),
1141 OPT_STRING(0, "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]", 1140 OPT_STRING(0, "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
1142 "only consider these symbols"), 1141 "only consider these symbols"),
1143 OPT_BOOLEAN(0, "source", &symbol_conf.annotate_src, 1142 OPT_BOOLEAN(0, "source", &symbol_conf.annotate_src,
1144 "Interleave source code with assembly code (default)"), 1143 "Interleave source code with assembly code (default)"),
1145 OPT_BOOLEAN(0, "asm-raw", &symbol_conf.annotate_asm_raw, 1144 OPT_BOOLEAN(0, "asm-raw", &symbol_conf.annotate_asm_raw,
1146 "Display raw encoding of assembly instructions (default)"), 1145 "Display raw encoding of assembly instructions (default)"),
1147 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel, 1146 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
1148 "Enable kernel symbol demangling"), 1147 "Enable kernel symbol demangling"),
1149 OPT_STRING(0, "objdump", &objdump_path, "path", 1148 OPT_STRING(0, "objdump", &objdump_path, "path",
1150 "objdump binary to use for disassembly and annotations"), 1149 "objdump binary to use for disassembly and annotations"),
1151 OPT_STRING('M', "disassembler-style", &disassembler_style, "disassembler style", 1150 OPT_STRING('M', "disassembler-style", &disassembler_style, "disassembler style",
1152 "Specify disassembler style (e.g. -M intel for intel syntax)"), 1151 "Specify disassembler style (e.g. -M intel for intel syntax)"),
1153 OPT_STRING('u', "uid", &target->uid_str, "user", "user to profile"), 1152 OPT_STRING('u', "uid", &target->uid_str, "user", "user to profile"),
1154 OPT_CALLBACK(0, "percent-limit", &top, "percent", 1153 OPT_CALLBACK(0, "percent-limit", &top, "percent",
1155 "Don't show entries under that percent", parse_percent_limit), 1154 "Don't show entries under that percent", parse_percent_limit),
1156 OPT_CALLBACK(0, "percentage", NULL, "relative|absolute", 1155 OPT_CALLBACK(0, "percentage", NULL, "relative|absolute",
1157 "How to display percentage of filtered entries", parse_filter_percentage), 1156 "How to display percentage of filtered entries", parse_filter_percentage),
1158 OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str, 1157 OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str,
1159 "width[,width...]", 1158 "width[,width...]",
1160 "don't try to adjust column width, use these fixed values"), 1159 "don't try to adjust column width, use these fixed values"),
1161 OPT_END() 1160 OPT_END()
1162 }; 1161 };
1163 const char * const top_usage[] = { 1162 const char * const top_usage[] = {
1164 "perf top [<options>]", 1163 "perf top [<options>]",
1165 NULL 1164 NULL
1166 }; 1165 };
1167 int status = hists__init(); 1166 int status = hists__init();
1168 1167
1169 if (status < 0) 1168 if (status < 0)
1170 return status; 1169 return status;
1171 1170
1172 top.evlist = perf_evlist__new(); 1171 top.evlist = perf_evlist__new();
1173 if (top.evlist == NULL) 1172 if (top.evlist == NULL)
1174 return -ENOMEM; 1173 return -ENOMEM;
1175 1174
1176 perf_config(perf_top_config, &top); 1175 perf_config(perf_top_config, &top);
1177 1176
1178 argc = parse_options(argc, argv, options, top_usage, 0); 1177 argc = parse_options(argc, argv, options, top_usage, 0);
1179 if (argc) 1178 if (argc)
1180 usage_with_options(top_usage, options); 1179 usage_with_options(top_usage, options);
1181 1180
1182 sort__mode = SORT_MODE__TOP; 1181 sort__mode = SORT_MODE__TOP;
1183 /* display thread wants entries to be collapsed in a different tree */ 1182 /* display thread wants entries to be collapsed in a different tree */
1184 sort__need_collapse = 1; 1183 sort__need_collapse = 1;
1185 1184
1186 if (setup_sorting() < 0) { 1185 if (setup_sorting() < 0) {
1187 if (sort_order) 1186 if (sort_order)
1188 parse_options_usage(top_usage, options, "s", 1); 1187 parse_options_usage(top_usage, options, "s", 1);
1189 if (field_order) 1188 if (field_order)
1190 parse_options_usage(sort_order ? NULL : top_usage, 1189 parse_options_usage(sort_order ? NULL : top_usage,
1191 options, "fields", 0); 1190 options, "fields", 0);
1192 goto out_delete_evlist; 1191 goto out_delete_evlist;
1193 } 1192 }
1194 1193
1195 if (top.use_stdio) 1194 if (top.use_stdio)
1196 use_browser = 0; 1195 use_browser = 0;
1197 else if (top.use_tui) 1196 else if (top.use_tui)
1198 use_browser = 1; 1197 use_browser = 1;
1199 1198
1200 setup_browser(false); 1199 setup_browser(false);
1201 1200
1202 status = target__validate(target); 1201 status = target__validate(target);
1203 if (status) { 1202 if (status) {
1204 target__strerror(target, status, errbuf, BUFSIZ); 1203 target__strerror(target, status, errbuf, BUFSIZ);
1205 ui__warning("%s\n", errbuf); 1204 ui__warning("%s\n", errbuf);
1206 } 1205 }
1207 1206
1208 status = target__parse_uid(target); 1207 status = target__parse_uid(target);
1209 if (status) { 1208 if (status) {
1210 int saved_errno = errno; 1209 int saved_errno = errno;
1211 1210
1212 target__strerror(target, status, errbuf, BUFSIZ); 1211 target__strerror(target, status, errbuf, BUFSIZ);
1213 ui__error("%s\n", errbuf); 1212 ui__error("%s\n", errbuf);
1214 1213
1215 status = -saved_errno; 1214 status = -saved_errno;
1216 goto out_delete_evlist; 1215 goto out_delete_evlist;
1217 } 1216 }
1218 1217
1219 if (target__none(target)) 1218 if (target__none(target))
1220 target->system_wide = true; 1219 target->system_wide = true;
1221 1220
1222 if (perf_evlist__create_maps(top.evlist, target) < 0) 1221 if (perf_evlist__create_maps(top.evlist, target) < 0)
1223 usage_with_options(top_usage, options); 1222 usage_with_options(top_usage, options);
1224 1223
1225 if (!top.evlist->nr_entries && 1224 if (!top.evlist->nr_entries &&
1226 perf_evlist__add_default(top.evlist) < 0) { 1225 perf_evlist__add_default(top.evlist) < 0) {
1227 ui__error("Not enough memory for event selector list\n"); 1226 ui__error("Not enough memory for event selector list\n");
1228 goto out_delete_evlist; 1227 goto out_delete_evlist;
1229 } 1228 }
1230 1229
1231 symbol_conf.nr_events = top.evlist->nr_entries; 1230 symbol_conf.nr_events = top.evlist->nr_entries;
1232 1231
1233 if (top.delay_secs < 1) 1232 if (top.delay_secs < 1)
1234 top.delay_secs = 1; 1233 top.delay_secs = 1;
1235 1234
1236 if (record_opts__config(opts)) { 1235 if (record_opts__config(opts)) {
1237 status = -EINVAL; 1236 status = -EINVAL;
1238 goto out_delete_evlist; 1237 goto out_delete_evlist;
1239 } 1238 }
1240 1239
1241 top.sym_evsel = perf_evlist__first(top.evlist); 1240 top.sym_evsel = perf_evlist__first(top.evlist);
1242 1241
1243 if (!symbol_conf.use_callchain) { 1242 if (!symbol_conf.use_callchain) {
1244 symbol_conf.cumulate_callchain = false; 1243 symbol_conf.cumulate_callchain = false;
1245 perf_hpp__cancel_cumulate(); 1244 perf_hpp__cancel_cumulate();
1246 } 1245 }
1247 1246
1248 symbol_conf.priv_size = sizeof(struct annotation); 1247 symbol_conf.priv_size = sizeof(struct annotation);
1249 1248
1250 symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL); 1249 symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL);
1251 if (symbol__init(NULL) < 0) 1250 if (symbol__init(NULL) < 0)
1252 return -1; 1251 return -1;
1253 1252
1254 sort__setup_elide(stdout); 1253 sort__setup_elide(stdout);
1255 1254
1256 get_term_dimensions(&top.winsize); 1255 get_term_dimensions(&top.winsize);
1257 if (top.print_entries == 0) { 1256 if (top.print_entries == 0) {
1258 struct sigaction act = { 1257 struct sigaction act = {
1259 .sa_sigaction = perf_top__sig_winch, 1258 .sa_sigaction = perf_top__sig_winch,
1260 .sa_flags = SA_SIGINFO, 1259 .sa_flags = SA_SIGINFO,
1261 }; 1260 };
1262 perf_top__update_print_entries(&top); 1261 perf_top__update_print_entries(&top);
1263 sigaction(SIGWINCH, &act, NULL); 1262 sigaction(SIGWINCH, &act, NULL);
1264 } 1263 }
1265 1264
1266 status = __cmd_top(&top); 1265 status = __cmd_top(&top);
1267 1266
1268 out_delete_evlist: 1267 out_delete_evlist:
1269 perf_evlist__delete(top.evlist); 1268 perf_evlist__delete(top.evlist);
1270 1269
1271 return status; 1270 return status;
1272 } 1271 }
1273 1272
tools/perf/perf-sys.h
1 #ifndef _PERF_SYS_H 1 #ifndef _PERF_SYS_H
2 #define _PERF_SYS_H 2 #define _PERF_SYS_H
3 3
4 #include <unistd.h> 4 #include <unistd.h>
5 #include <sys/types.h> 5 #include <sys/types.h>
6 #include <sys/syscall.h> 6 #include <sys/syscall.h>
7 #include <linux/types.h> 7 #include <linux/types.h>
8 #include <linux/perf_event.h> 8 #include <linux/perf_event.h>
9 #include <asm/unistd.h>
10 9
11 #if defined(__i386__) 10 #if defined(__i386__)
12 #define mb() asm volatile("lock; addl $0,0(%%esp)" ::: "memory") 11 #define mb() asm volatile("lock; addl $0,0(%%esp)" ::: "memory")
13 #define wmb() asm volatile("lock; addl $0,0(%%esp)" ::: "memory") 12 #define wmb() asm volatile("lock; addl $0,0(%%esp)" ::: "memory")
14 #define rmb() asm volatile("lock; addl $0,0(%%esp)" ::: "memory") 13 #define rmb() asm volatile("lock; addl $0,0(%%esp)" ::: "memory")
15 #define cpu_relax() asm volatile("rep; nop" ::: "memory"); 14 #define cpu_relax() asm volatile("rep; nop" ::: "memory");
16 #define CPUINFO_PROC {"model name"} 15 #define CPUINFO_PROC {"model name"}
17 #ifndef __NR_perf_event_open 16 #ifndef __NR_perf_event_open
18 # define __NR_perf_event_open 336 17 # define __NR_perf_event_open 336
19 #endif 18 #endif
20 #ifndef __NR_futex 19 #ifndef __NR_futex
21 # define __NR_futex 240 20 # define __NR_futex 240
22 #endif 21 #endif
23 #ifndef __NR_gettid 22 #ifndef __NR_gettid
24 # define __NR_gettid 224 23 # define __NR_gettid 224
25 #endif 24 #endif
26 #endif 25 #endif
27 26
28 #if defined(__x86_64__) 27 #if defined(__x86_64__)
29 #define mb() asm volatile("mfence" ::: "memory") 28 #define mb() asm volatile("mfence" ::: "memory")
30 #define wmb() asm volatile("sfence" ::: "memory") 29 #define wmb() asm volatile("sfence" ::: "memory")
31 #define rmb() asm volatile("lfence" ::: "memory") 30 #define rmb() asm volatile("lfence" ::: "memory")
32 #define cpu_relax() asm volatile("rep; nop" ::: "memory"); 31 #define cpu_relax() asm volatile("rep; nop" ::: "memory");
33 #define CPUINFO_PROC {"model name"} 32 #define CPUINFO_PROC {"model name"}
34 #ifndef __NR_perf_event_open 33 #ifndef __NR_perf_event_open
35 # define __NR_perf_event_open 298 34 # define __NR_perf_event_open 298
36 #endif 35 #endif
37 #ifndef __NR_futex 36 #ifndef __NR_futex
38 # define __NR_futex 202 37 # define __NR_futex 202
39 #endif 38 #endif
40 #ifndef __NR_gettid 39 #ifndef __NR_gettid
41 # define __NR_gettid 186 40 # define __NR_gettid 186
42 #endif 41 #endif
43 #endif 42 #endif
44 43
45 #ifdef __powerpc__ 44 #ifdef __powerpc__
46 #include "../../arch/powerpc/include/uapi/asm/unistd.h" 45 #include "../../arch/powerpc/include/uapi/asm/unistd.h"
47 #define mb() asm volatile ("sync" ::: "memory") 46 #define mb() asm volatile ("sync" ::: "memory")
48 #define wmb() asm volatile ("sync" ::: "memory") 47 #define wmb() asm volatile ("sync" ::: "memory")
49 #define rmb() asm volatile ("sync" ::: "memory") 48 #define rmb() asm volatile ("sync" ::: "memory")
50 #define CPUINFO_PROC {"cpu"} 49 #define CPUINFO_PROC {"cpu"}
51 #endif 50 #endif
52 51
53 #ifdef __s390__ 52 #ifdef __s390__
54 #define mb() asm volatile("bcr 15,0" ::: "memory") 53 #define mb() asm volatile("bcr 15,0" ::: "memory")
55 #define wmb() asm volatile("bcr 15,0" ::: "memory") 54 #define wmb() asm volatile("bcr 15,0" ::: "memory")
56 #define rmb() asm volatile("bcr 15,0" ::: "memory") 55 #define rmb() asm volatile("bcr 15,0" ::: "memory")
57 #define CPUINFO_PROC {"vendor_id"} 56 #define CPUINFO_PROC {"vendor_id"}
58 #endif 57 #endif
59 58
60 #ifdef __sh__ 59 #ifdef __sh__
61 #if defined(__SH4A__) || defined(__SH5__) 60 #if defined(__SH4A__) || defined(__SH5__)
62 # define mb() asm volatile("synco" ::: "memory") 61 # define mb() asm volatile("synco" ::: "memory")
63 # define wmb() asm volatile("synco" ::: "memory") 62 # define wmb() asm volatile("synco" ::: "memory")
64 # define rmb() asm volatile("synco" ::: "memory") 63 # define rmb() asm volatile("synco" ::: "memory")
65 #else 64 #else
66 # define mb() asm volatile("" ::: "memory") 65 # define mb() asm volatile("" ::: "memory")
67 # define wmb() asm volatile("" ::: "memory") 66 # define wmb() asm volatile("" ::: "memory")
68 # define rmb() asm volatile("" ::: "memory") 67 # define rmb() asm volatile("" ::: "memory")
69 #endif 68 #endif
70 #define CPUINFO_PROC {"cpu type"} 69 #define CPUINFO_PROC {"cpu type"}
71 #endif 70 #endif
72 71
73 #ifdef __hppa__ 72 #ifdef __hppa__
74 #define mb() asm volatile("" ::: "memory") 73 #define mb() asm volatile("" ::: "memory")
75 #define wmb() asm volatile("" ::: "memory") 74 #define wmb() asm volatile("" ::: "memory")
76 #define rmb() asm volatile("" ::: "memory") 75 #define rmb() asm volatile("" ::: "memory")
77 #define CPUINFO_PROC {"cpu"} 76 #define CPUINFO_PROC {"cpu"}
78 #endif 77 #endif
79 78
80 #ifdef __sparc__ 79 #ifdef __sparc__
81 #ifdef __LP64__ 80 #ifdef __LP64__
82 #define mb() asm volatile("ba,pt %%xcc, 1f\n" \ 81 #define mb() asm volatile("ba,pt %%xcc, 1f\n" \
83 "membar #StoreLoad\n" \ 82 "membar #StoreLoad\n" \
84 "1:\n":::"memory") 83 "1:\n":::"memory")
85 #else 84 #else
86 #define mb() asm volatile("":::"memory") 85 #define mb() asm volatile("":::"memory")
87 #endif 86 #endif
88 #define wmb() asm volatile("":::"memory") 87 #define wmb() asm volatile("":::"memory")
89 #define rmb() asm volatile("":::"memory") 88 #define rmb() asm volatile("":::"memory")
90 #define CPUINFO_PROC {"cpu"} 89 #define CPUINFO_PROC {"cpu"}
91 #endif 90 #endif
92 91
93 #ifdef __alpha__ 92 #ifdef __alpha__
94 #define mb() asm volatile("mb" ::: "memory") 93 #define mb() asm volatile("mb" ::: "memory")
95 #define wmb() asm volatile("wmb" ::: "memory") 94 #define wmb() asm volatile("wmb" ::: "memory")
96 #define rmb() asm volatile("mb" ::: "memory") 95 #define rmb() asm volatile("mb" ::: "memory")
97 #define CPUINFO_PROC {"cpu model"} 96 #define CPUINFO_PROC {"cpu model"}
98 #endif 97 #endif
99 98
100 #ifdef __ia64__ 99 #ifdef __ia64__
101 #define mb() asm volatile ("mf" ::: "memory") 100 #define mb() asm volatile ("mf" ::: "memory")
102 #define wmb() asm volatile ("mf" ::: "memory") 101 #define wmb() asm volatile ("mf" ::: "memory")
103 #define rmb() asm volatile ("mf" ::: "memory") 102 #define rmb() asm volatile ("mf" ::: "memory")
104 #define cpu_relax() asm volatile ("hint @pause" ::: "memory") 103 #define cpu_relax() asm volatile ("hint @pause" ::: "memory")
105 #define CPUINFO_PROC {"model name"} 104 #define CPUINFO_PROC {"model name"}
106 #endif 105 #endif
107 106
108 #ifdef __arm__ 107 #ifdef __arm__
109 /* 108 /*
110 * Use the __kuser_memory_barrier helper in the CPU helper page. See 109 * Use the __kuser_memory_barrier helper in the CPU helper page. See
111 * arch/arm/kernel/entry-armv.S in the kernel source for details. 110 * arch/arm/kernel/entry-armv.S in the kernel source for details.
112 */ 111 */
113 #define mb() ((void(*)(void))0xffff0fa0)() 112 #define mb() ((void(*)(void))0xffff0fa0)()
114 #define wmb() ((void(*)(void))0xffff0fa0)() 113 #define wmb() ((void(*)(void))0xffff0fa0)()
115 #define rmb() ((void(*)(void))0xffff0fa0)() 114 #define rmb() ((void(*)(void))0xffff0fa0)()
116 #define CPUINFO_PROC {"model name", "Processor"} 115 #define CPUINFO_PROC {"model name", "Processor"}
117 #endif 116 #endif
118 117
119 #ifdef __aarch64__ 118 #ifdef __aarch64__
120 #define mb() asm volatile("dmb ish" ::: "memory") 119 #define mb() asm volatile("dmb ish" ::: "memory")
121 #define wmb() asm volatile("dmb ishst" ::: "memory") 120 #define wmb() asm volatile("dmb ishst" ::: "memory")
122 #define rmb() asm volatile("dmb ishld" ::: "memory") 121 #define rmb() asm volatile("dmb ishld" ::: "memory")
123 #define cpu_relax() asm volatile("yield" ::: "memory") 122 #define cpu_relax() asm volatile("yield" ::: "memory")
124 #endif 123 #endif
125 124
126 #ifdef __mips__ 125 #ifdef __mips__
127 #define mb() asm volatile( \ 126 #define mb() asm volatile( \
128 ".set mips2\n\t" \ 127 ".set mips2\n\t" \
129 "sync\n\t" \ 128 "sync\n\t" \
130 ".set mips0" \ 129 ".set mips0" \
131 : /* no output */ \ 130 : /* no output */ \
132 : /* no input */ \ 131 : /* no input */ \
133 : "memory") 132 : "memory")
134 #define wmb() mb() 133 #define wmb() mb()
135 #define rmb() mb() 134 #define rmb() mb()
136 #define CPUINFO_PROC {"cpu model"} 135 #define CPUINFO_PROC {"cpu model"}
137 #endif 136 #endif
138 137
139 #ifdef __arc__ 138 #ifdef __arc__
140 #define mb() asm volatile("" ::: "memory") 139 #define mb() asm volatile("" ::: "memory")
141 #define wmb() asm volatile("" ::: "memory") 140 #define wmb() asm volatile("" ::: "memory")
142 #define rmb() asm volatile("" ::: "memory") 141 #define rmb() asm volatile("" ::: "memory")
143 #define CPUINFO_PROC {"Processor"} 142 #define CPUINFO_PROC {"Processor"}
144 #endif 143 #endif
145 144
146 #ifdef __metag__ 145 #ifdef __metag__
147 #define mb() asm volatile("" ::: "memory") 146 #define mb() asm volatile("" ::: "memory")
148 #define wmb() asm volatile("" ::: "memory") 147 #define wmb() asm volatile("" ::: "memory")
149 #define rmb() asm volatile("" ::: "memory") 148 #define rmb() asm volatile("" ::: "memory")
150 #define CPUINFO_PROC {"CPU"} 149 #define CPUINFO_PROC {"CPU"}
151 #endif 150 #endif
152 151
153 #ifdef __xtensa__ 152 #ifdef __xtensa__
154 #define mb() asm volatile("memw" ::: "memory") 153 #define mb() asm volatile("memw" ::: "memory")
155 #define wmb() asm volatile("memw" ::: "memory") 154 #define wmb() asm volatile("memw" ::: "memory")
156 #define rmb() asm volatile("" ::: "memory") 155 #define rmb() asm volatile("" ::: "memory")
157 #define CPUINFO_PROC {"core ID"} 156 #define CPUINFO_PROC {"core ID"}
158 #endif 157 #endif
159 158
160 #ifdef __tile__ 159 #ifdef __tile__
161 #define mb() asm volatile ("mf" ::: "memory") 160 #define mb() asm volatile ("mf" ::: "memory")
162 #define wmb() asm volatile ("mf" ::: "memory") 161 #define wmb() asm volatile ("mf" ::: "memory")
163 #define rmb() asm volatile ("mf" ::: "memory") 162 #define rmb() asm volatile ("mf" ::: "memory")
164 #define cpu_relax() asm volatile ("mfspr zero, PASS" ::: "memory") 163 #define cpu_relax() asm volatile ("mfspr zero, PASS" ::: "memory")
165 #define CPUINFO_PROC {"model name"} 164 #define CPUINFO_PROC {"model name"}
166 #endif 165 #endif
167 166
168 #define barrier() asm volatile ("" ::: "memory") 167 #define barrier() asm volatile ("" ::: "memory")
169 168
170 #ifndef cpu_relax 169 #ifndef cpu_relax
171 #define cpu_relax() barrier() 170 #define cpu_relax() barrier()
172 #endif 171 #endif
173 172
174 static inline int 173 static inline int
175 sys_perf_event_open(struct perf_event_attr *attr, 174 sys_perf_event_open(struct perf_event_attr *attr,
176 pid_t pid, int cpu, int group_fd, 175 pid_t pid, int cpu, int group_fd,
177 unsigned long flags) 176 unsigned long flags)
178 { 177 {
179 int fd; 178 int fd;
180 179
181 fd = syscall(__NR_perf_event_open, attr, pid, cpu, 180 fd = syscall(__NR_perf_event_open, attr, pid, cpu,
182 group_fd, flags); 181 group_fd, flags);
183 182
184 #ifdef HAVE_ATTR_TEST 183 #ifdef HAVE_ATTR_TEST
185 if (unlikely(test_attr__enabled)) 184 if (unlikely(test_attr__enabled))
186 test_attr__open(attr, pid, cpu, fd, group_fd, flags); 185 test_attr__open(attr, pid, cpu, fd, group_fd, flags);
187 #endif 186 #endif
188 return fd; 187 return fd;
189 } 188 }
190 189
191 #endif /* _PERF_SYS_H */ 190 #endif /* _PERF_SYS_H */
192 191