]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - Documentation/perf_counter/builtin-stat.c
perf_counter tools: Clean up builtin-stat.c's do_perfstat()
[mirror_ubuntu-focal-kernel.git] / Documentation / perf_counter / builtin-stat.c
CommitLineData
ddcacfa0 1/*
5242519b 2 * perf stat: /usr/bin/time -alike performance counter statistics utility
ddcacfa0
IM
3
4 It summarizes the counter events of all tasks (and child tasks),
5 covering all CPUs that the command (or workload) executes on.
6 It only counts the per-task events of the workload started,
7 independent of how many other tasks run on those CPUs.
8
9 Sample output:
10
5242519b 11 $ perf stat -e 1 -e 3 -e 5 ls -lR /usr/include/ >/dev/null
ddcacfa0
IM
12
13 Performance counter stats for 'ls':
14
15 163516953 instructions
16 2295 cache-misses
17 2855182 branch-misses
5242519b
IM
18 *
19 * Copyright (C) 2008, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
20 *
21 * Improvements and fixes by:
22 *
23 * Arjan van de Ven <arjan@linux.intel.com>
24 * Yanmin Zhang <yanmin.zhang@intel.com>
25 * Wu Fengguang <fengguang.wu@intel.com>
26 * Mike Galbraith <efault@gmx.de>
27 * Paul Mackerras <paulus@samba.org>
28 *
29 * Released under the GPL v2. (and only v2, not any later version)
ddcacfa0
IM
30 */
31
1a482f38 32#include "perf.h"
16f762a2 33#include "builtin.h"
148be2c1 34#include "util/util.h"
5242519b
IM
35#include "util/parse-options.h"
36#include "util/parse-events.h"
ddcacfa0 37
ddcacfa0 38#include <sys/prctl.h>
16c8a109 39
ddcacfa0 40static int system_wide = 0;
5242519b 41static int inherit = 1;
ddcacfa0 42
5242519b 43static __u64 default_event_id[MAX_COUNTERS] = {
ddcacfa0
IM
44 EID(PERF_TYPE_SOFTWARE, PERF_COUNT_TASK_CLOCK),
45 EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CONTEXT_SWITCHES),
46 EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CPU_MIGRATIONS),
47 EID(PERF_TYPE_SOFTWARE, PERF_COUNT_PAGE_FAULTS),
48
49 EID(PERF_TYPE_HARDWARE, PERF_COUNT_CPU_CYCLES),
50 EID(PERF_TYPE_HARDWARE, PERF_COUNT_INSTRUCTIONS),
51 EID(PERF_TYPE_HARDWARE, PERF_COUNT_CACHE_REFERENCES),
52 EID(PERF_TYPE_HARDWARE, PERF_COUNT_CACHE_MISSES),
53};
5242519b 54
ddcacfa0
IM
55static int default_interval = 100000;
56static int event_count[MAX_COUNTERS];
57static int fd[MAX_NR_CPUS][MAX_COUNTERS];
58
5242519b 59static int target_pid = -1;
ddcacfa0 60static int nr_cpus = 0;
ddcacfa0
IM
61static unsigned int page_size;
62
66cf7829 63static int scale = 1;
ddcacfa0
IM
64
65static const unsigned int default_count[] = {
66 1000000,
67 1000000,
68 10000,
69 10000,
70 1000000,
71 10000,
72};
73
ddcacfa0
IM
74static void create_perfstat_counter(int counter)
75{
76 struct perf_counter_hw_event hw_event;
77
78 memset(&hw_event, 0, sizeof(hw_event));
79 hw_event.config = event_id[counter];
80 hw_event.record_type = 0;
5242519b 81 hw_event.nmi = 1;
16c8a109
PZ
82 hw_event.exclude_kernel = event_mask[counter] & EVENT_MASK_KERNEL;
83 hw_event.exclude_user = event_mask[counter] & EVENT_MASK_USER;
84
ddcacfa0
IM
85 if (scale)
86 hw_event.read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
87 PERF_FORMAT_TOTAL_TIME_RUNNING;
88
89 if (system_wide) {
90 int cpu;
91 for (cpu = 0; cpu < nr_cpus; cpu ++) {
92 fd[cpu][counter] = sys_perf_counter_open(&hw_event, -1, cpu, -1, 0);
93 if (fd[cpu][counter] < 0) {
94 printf("perfstat error: syscall returned with %d (%s)\n",
95 fd[cpu][counter], strerror(errno));
96 exit(-1);
97 }
98 }
99 } else {
5242519b 100 hw_event.inherit = inherit;
ddcacfa0
IM
101 hw_event.disabled = 1;
102
103 fd[0][counter] = sys_perf_counter_open(&hw_event, 0, -1, -1, 0);
104 if (fd[0][counter] < 0) {
105 printf("perfstat error: syscall returned with %d (%s)\n",
106 fd[0][counter], strerror(errno));
107 exit(-1);
108 }
109 }
110}
111
c04f5e5d
IM
112/*
113 * Does the counter have nsecs as a unit?
114 */
115static inline int nsec_counter(int counter)
116{
117 if (event_id[counter] == EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CPU_CLOCK))
118 return 1;
119 if (event_id[counter] == EID(PERF_TYPE_SOFTWARE, PERF_COUNT_TASK_CLOCK))
120 return 1;
121
122 return 0;
123}
124
125/*
126 * Print out the results of a single counter:
127 */
128static void print_counter(int counter)
129{
130 __u64 count[3], single_count[3];
131 ssize_t res;
132 int cpu, nv;
133 int scaled;
134
135 count[0] = count[1] = count[2] = 0;
136 nv = scale ? 3 : 1;
137 for (cpu = 0; cpu < nr_cpus; cpu ++) {
138 res = read(fd[cpu][counter], single_count, nv * sizeof(__u64));
139 assert(res == nv * sizeof(__u64));
140
141 count[0] += single_count[0];
142 if (scale) {
143 count[1] += single_count[1];
144 count[2] += single_count[2];
145 }
146 }
147
148 scaled = 0;
149 if (scale) {
150 if (count[2] == 0) {
151 fprintf(stderr, " %14s %-20s\n",
152 "<not counted>", event_name(counter));
153 return;
154 }
155 if (count[2] < count[1]) {
156 scaled = 1;
157 count[0] = (unsigned long long)
158 ((double)count[0] * count[1] / count[2] + 0.5);
159 }
160 }
161
162 if (nsec_counter(counter)) {
163 double msecs = (double)count[0] / 1000000;
164
165 fprintf(stderr, " %14.6f %-20s (msecs)",
166 msecs, event_name(counter));
167 } else {
168 fprintf(stderr, " %14Ld %-20s (events)",
169 count[0], event_name(counter));
170 }
171 if (scaled)
172 fprintf(stderr, " (scaled from %.2f%%)",
173 (double) count[2] / count[1] * 100);
174 fprintf(stderr, "\n");
175}
176
16f762a2 177static int do_perfstat(int argc, const char **argv)
ddcacfa0
IM
178{
179 unsigned long long t0, t1;
180 int counter;
ddcacfa0
IM
181 int status;
182 int pid;
183
184 if (!system_wide)
185 nr_cpus = 1;
186
187 for (counter = 0; counter < nr_counters; counter++)
188 create_perfstat_counter(counter);
189
ddcacfa0
IM
190 /*
191 * Enable counters and exec the command:
192 */
193 t0 = rdclock();
194 prctl(PR_TASK_PERF_COUNTERS_ENABLE);
195
196 if ((pid = fork()) < 0)
197 perror("failed to fork");
198 if (!pid) {
5242519b 199 if (execvp(argv[0], (char **)argv)) {
ddcacfa0
IM
200 perror(argv[0]);
201 exit(-1);
202 }
203 }
204 while (wait(&status) >= 0)
205 ;
206 prctl(PR_TASK_PERF_COUNTERS_DISABLE);
207 t1 = rdclock();
208
209 fflush(stdout);
210
211 fprintf(stderr, "\n");
212 fprintf(stderr, " Performance counter stats for \'%s\':\n",
213 argv[0]);
214 fprintf(stderr, "\n");
215
c04f5e5d
IM
216 for (counter = 0; counter < nr_counters; counter++)
217 print_counter(counter);
ddcacfa0 218
ddcacfa0 219
ddcacfa0
IM
220 fprintf(stderr, "\n");
221 fprintf(stderr, " Wall-clock time elapsed: %12.6f msecs\n",
222 (double)(t1-t0)/1e6);
223 fprintf(stderr, "\n");
224
225 return 0;
226}
227
5242519b 228static void skip_signal(int signo)
ddcacfa0 229{
5242519b
IM
230}
231
232static const char * const stat_usage[] = {
233 "perf stat [<options>] <command>",
234 NULL
235};
236
237static char events_help_msg[EVENTS_HELP_MAX];
238
239static const struct option options[] = {
240 OPT_CALLBACK('e', "event", NULL, "event",
241 events_help_msg, parse_events),
242 OPT_INTEGER('c', "count", &default_interval,
243 "event period to sample"),
244 OPT_BOOLEAN('i', "inherit", &inherit,
245 "child tasks inherit counters"),
246 OPT_INTEGER('p', "pid", &target_pid,
247 "stat events on existing pid"),
248 OPT_BOOLEAN('a', "all-cpus", &system_wide,
249 "system-wide collection from all CPUs"),
250 OPT_BOOLEAN('l', "scale", &scale,
251 "scale/normalize counters"),
252 OPT_END()
253};
254
255int cmd_stat(int argc, const char **argv, const char *prefix)
256{
257 int counter;
258
259 page_size = sysconf(_SC_PAGE_SIZE);
260
261 create_events_help(events_help_msg);
262 memcpy(event_id, default_event_id, sizeof(default_event_id));
263
264 argc = parse_options(argc, argv, options, stat_usage, 0);
265 if (!argc)
266 usage_with_options(stat_usage, options);
ddcacfa0
IM
267
268 if (!nr_counters) {
269 nr_counters = 8;
270 }
271
272 for (counter = 0; counter < nr_counters; counter++) {
273 if (event_count[counter])
274 continue;
275
276 event_count[counter] = default_interval;
277 }
ddcacfa0
IM
278 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
279 assert(nr_cpus <= MAX_NR_CPUS);
280 assert(nr_cpus >= 0);
281
58d7e993
IM
282 /*
283 * We dont want to block the signals - that would cause
284 * child tasks to inherit that and Ctrl-C would not work.
285 * What we want is for Ctrl-C to work in the exec()-ed
286 * task, but being ignored by perf stat itself:
287 */
288 signal(SIGINT, skip_signal);
289 signal(SIGALRM, skip_signal);
290 signal(SIGABRT, skip_signal);
291
ddcacfa0
IM
292 return do_perfstat(argc, argv);
293}