perf stat: Reorganize output
[deliverable/linux.git] / tools / perf / builtin-stat.c
CommitLineData
ddcacfa0 1/*
bf9e1876
IM
2 * builtin-stat.c
3 *
4 * Builtin stat command: Give a precise performance counters summary
5 * overview about any workload, CPU or specific PID.
6 *
7 * Sample output:
ddcacfa0 8
bf9e1876
IM
9 $ perf stat ~/hackbench 10
10 Time: 0.104
ddcacfa0 11
bf9e1876 12 Performance counter stats for '/home/mingo/hackbench':
ddcacfa0 13
bf9e1876
IM
14 1255.538611 task clock ticks # 10.143 CPU utilization factor
15 54011 context switches # 0.043 M/sec
16 385 CPU migrations # 0.000 M/sec
17 17755 pagefaults # 0.014 M/sec
18 3808323185 CPU cycles # 3033.219 M/sec
19 1575111190 instructions # 1254.530 M/sec
20 17367895 cache references # 13.833 M/sec
21 7674421 cache misses # 6.112 M/sec
ddcacfa0 22
bf9e1876 23 Wall-clock time elapsed: 123.786620 msecs
ddcacfa0 24
5242519b
IM
25 *
26 * Copyright (C) 2008, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
27 *
28 * Improvements and fixes by:
29 *
30 * Arjan van de Ven <arjan@linux.intel.com>
31 * Yanmin Zhang <yanmin.zhang@intel.com>
32 * Wu Fengguang <fengguang.wu@intel.com>
33 * Mike Galbraith <efault@gmx.de>
34 * Paul Mackerras <paulus@samba.org>
35 *
36 * Released under the GPL v2. (and only v2, not any later version)
ddcacfa0
IM
37 */
38
1a482f38 39#include "perf.h"
16f762a2 40#include "builtin.h"
148be2c1 41#include "util/util.h"
5242519b
IM
42#include "util/parse-options.h"
43#include "util/parse-events.h"
ddcacfa0 44
ddcacfa0 45#include <sys/prctl.h>
16c8a109 46
a21ca2ca 47static struct perf_counter_attr default_attrs[MAX_COUNTERS] = {
ddcacfa0 48
f4dbfa8f
PZ
49 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
50 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES},
51 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
52 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
53
54 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
55 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
56 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CACHE_REFERENCES},
57 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CACHE_MISSES },
58
ddcacfa0 59};
5242519b 60
a21ca2ca
IM
61static int system_wide = 0;
62static int inherit = 1;
743ee1f8 63static int verbose = 0;
a21ca2ca 64
ddcacfa0
IM
65static int fd[MAX_NR_CPUS][MAX_COUNTERS];
66
5242519b 67static int target_pid = -1;
ddcacfa0 68static int nr_cpus = 0;
ddcacfa0
IM
69static unsigned int page_size;
70
66cf7829 71static int scale = 1;
ddcacfa0
IM
72
73static const unsigned int default_count[] = {
74 1000000,
75 1000000,
76 10000,
77 10000,
78 1000000,
79 10000,
80};
81
2996f5dd
IM
82static __u64 event_res[MAX_COUNTERS][3];
83static __u64 event_scaled[MAX_COUNTERS];
84
be1ac0d8 85static __u64 runtime_nsecs;
d7c29318 86static __u64 walltime_nsecs;
e779898a 87static __u64 runtime_cycles;
be1ac0d8 88
743ee1f8 89static void create_perf_stat_counter(int counter)
ddcacfa0 90{
a21ca2ca 91 struct perf_counter_attr *attr = attrs + counter;
16c8a109 92
ddcacfa0 93 if (scale)
a21ca2ca
IM
94 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
95 PERF_FORMAT_TOTAL_TIME_RUNNING;
ddcacfa0
IM
96
97 if (system_wide) {
98 int cpu;
99 for (cpu = 0; cpu < nr_cpus; cpu ++) {
a21ca2ca 100 fd[cpu][counter] = sys_perf_counter_open(attr, -1, cpu, -1, 0);
743ee1f8
IM
101 if (fd[cpu][counter] < 0 && verbose) {
102 printf("Error: counter %d, sys_perf_counter_open() syscall returned with %d (%s)\n", counter, fd[cpu][counter], strerror(errno));
ddcacfa0
IM
103 }
104 }
105 } else {
a21ca2ca
IM
106 attr->inherit = inherit;
107 attr->disabled = 1;
ddcacfa0 108
a21ca2ca 109 fd[0][counter] = sys_perf_counter_open(attr, 0, -1, -1, 0);
743ee1f8
IM
110 if (fd[0][counter] < 0 && verbose) {
111 printf("Error: counter %d, sys_perf_counter_open() syscall returned with %d (%s)\n", counter, fd[0][counter], strerror(errno));
ddcacfa0
IM
112 }
113 }
114}
115
c04f5e5d
IM
116/*
117 * Does the counter have nsecs as a unit?
118 */
119static inline int nsec_counter(int counter)
120{
a21ca2ca
IM
121 if (attrs[counter].type != PERF_TYPE_SOFTWARE)
122 return 0;
123
f4dbfa8f 124 if (attrs[counter].config == PERF_COUNT_SW_CPU_CLOCK)
c04f5e5d 125 return 1;
a21ca2ca 126
f4dbfa8f 127 if (attrs[counter].config == PERF_COUNT_SW_TASK_CLOCK)
c04f5e5d
IM
128 return 1;
129
130 return 0;
131}
132
133/*
2996f5dd 134 * Read out the results of a single counter:
c04f5e5d 135 */
2996f5dd 136static void read_counter(int counter)
c04f5e5d 137{
2996f5dd 138 __u64 *count, single_count[3];
c04f5e5d
IM
139 ssize_t res;
140 int cpu, nv;
141 int scaled;
142
2996f5dd
IM
143 count = event_res[counter];
144
c04f5e5d 145 count[0] = count[1] = count[2] = 0;
2996f5dd 146
c04f5e5d
IM
147 nv = scale ? 3 : 1;
148 for (cpu = 0; cpu < nr_cpus; cpu ++) {
743ee1f8
IM
149 if (fd[cpu][counter] < 0)
150 continue;
151
c04f5e5d
IM
152 res = read(fd[cpu][counter], single_count, nv * sizeof(__u64));
153 assert(res == nv * sizeof(__u64));
154
155 count[0] += single_count[0];
156 if (scale) {
157 count[1] += single_count[1];
158 count[2] += single_count[2];
159 }
160 }
161
162 scaled = 0;
163 if (scale) {
164 if (count[2] == 0) {
2996f5dd
IM
165 event_scaled[counter] = -1;
166 count[0] = 0;
c04f5e5d
IM
167 return;
168 }
2996f5dd 169
c04f5e5d 170 if (count[2] < count[1]) {
2996f5dd 171 event_scaled[counter] = 1;
c04f5e5d
IM
172 count[0] = (unsigned long long)
173 ((double)count[0] * count[1] / count[2] + 0.5);
174 }
175 }
be1ac0d8
IM
176 /*
177 * Save the full runtime - to allow normalization during printout:
178 */
a21ca2ca 179 if (attrs[counter].type == PERF_TYPE_SOFTWARE &&
f4dbfa8f 180 attrs[counter].config == PERF_COUNT_SW_TASK_CLOCK)
be1ac0d8 181 runtime_nsecs = count[0];
e779898a 182 if (attrs[counter].type == PERF_TYPE_HARDWARE &&
f4dbfa8f 183 attrs[counter].config == PERF_COUNT_HW_CPU_CYCLES)
e779898a 184 runtime_cycles = count[0];
2996f5dd
IM
185}
186
44175b6f
IM
187static void nsec_printout(int counter, __u64 *count)
188{
189 double msecs = (double)count[0] / 1000000;
190
191 fprintf(stderr, " %14.6f %-20s", msecs, event_name(counter));
192
193 if (attrs[counter].type == PERF_TYPE_SOFTWARE &&
194 attrs[counter].config == PERF_COUNT_SW_TASK_CLOCK) {
195
196 if (walltime_nsecs)
197 fprintf(stderr, " # %10.3f CPUs",
198 (double)count[0] / (double)walltime_nsecs);
199 }
200}
201
202static void abs_printout(int counter, __u64 *count)
203{
204 fprintf(stderr, " %14Ld %-20s", count[0], event_name(counter));
205
206 if (runtime_cycles &&
207 attrs[counter].type == PERF_TYPE_HARDWARE &&
208 attrs[counter].config == PERF_COUNT_HW_INSTRUCTIONS) {
209
210 fprintf(stderr, " # %10.3f IPC",
211 (double)count[0] / (double)runtime_cycles);
212
213 return;
214 }
215
216 if (runtime_nsecs)
217 fprintf(stderr, " # %10.3f M/sec",
218 (double)count[0]/runtime_nsecs*1000.0);
219}
220
2996f5dd
IM
221/*
222 * Print out the results of a single counter:
223 */
224static void print_counter(int counter)
225{
226 __u64 *count;
227 int scaled;
228
229 count = event_res[counter];
230 scaled = event_scaled[counter];
231
232 if (scaled == -1) {
233 fprintf(stderr, " %14s %-20s\n",
234 "<not counted>", event_name(counter));
235 return;
236 }
c04f5e5d 237
44175b6f
IM
238 if (nsec_counter(counter))
239 nsec_printout(counter, count);
240 else
241 abs_printout(counter, count);
d7c29318 242
c04f5e5d
IM
243 if (scaled)
244 fprintf(stderr, " (scaled from %.2f%%)",
245 (double) count[2] / count[1] * 100);
44175b6f 246
c04f5e5d
IM
247 fprintf(stderr, "\n");
248}
249
743ee1f8 250static int do_perf_stat(int argc, const char **argv)
ddcacfa0
IM
251{
252 unsigned long long t0, t1;
253 int counter;
ddcacfa0
IM
254 int status;
255 int pid;
44db76c8 256 int i;
ddcacfa0
IM
257
258 if (!system_wide)
259 nr_cpus = 1;
260
261 for (counter = 0; counter < nr_counters; counter++)
743ee1f8 262 create_perf_stat_counter(counter);
ddcacfa0 263
ddcacfa0
IM
264 /*
265 * Enable counters and exec the command:
266 */
267 t0 = rdclock();
268 prctl(PR_TASK_PERF_COUNTERS_ENABLE);
269
270 if ((pid = fork()) < 0)
271 perror("failed to fork");
44db76c8 272
ddcacfa0 273 if (!pid) {
5242519b 274 if (execvp(argv[0], (char **)argv)) {
ddcacfa0
IM
275 perror(argv[0]);
276 exit(-1);
277 }
278 }
44db76c8 279
ddcacfa0
IM
280 while (wait(&status) >= 0)
281 ;
44db76c8 282
ddcacfa0
IM
283 prctl(PR_TASK_PERF_COUNTERS_DISABLE);
284 t1 = rdclock();
285
d7c29318
IM
286 walltime_nsecs = t1 - t0;
287
ddcacfa0
IM
288 fflush(stdout);
289
290 fprintf(stderr, "\n");
44db76c8
IM
291 fprintf(stderr, " Performance counter stats for \'%s", argv[0]);
292
293 for (i = 1; i < argc; i++)
294 fprintf(stderr, " %s", argv[i]);
295
296 fprintf(stderr, "\':\n");
ddcacfa0
IM
297 fprintf(stderr, "\n");
298
2996f5dd
IM
299 for (counter = 0; counter < nr_counters; counter++)
300 read_counter(counter);
301
c04f5e5d
IM
302 for (counter = 0; counter < nr_counters; counter++)
303 print_counter(counter);
ddcacfa0 304
ddcacfa0 305
ddcacfa0 306 fprintf(stderr, "\n");
44175b6f 307 fprintf(stderr, " %14.9f seconds time elapsed.\n", (double)(t1-t0)/1e9);
ddcacfa0
IM
308 fprintf(stderr, "\n");
309
310 return 0;
311}
312
f7b7c26e
PZ
313static volatile int signr = -1;
314
5242519b 315static void skip_signal(int signo)
ddcacfa0 316{
f7b7c26e
PZ
317 signr = signo;
318}
319
320static void sig_atexit(void)
321{
322 if (signr == -1)
323 return;
324
325 signal(signr, SIG_DFL);
326 kill(getpid(), signr);
5242519b
IM
327}
328
329static const char * const stat_usage[] = {
330 "perf stat [<options>] <command>",
331 NULL
332};
333
5242519b
IM
334static const struct option options[] = {
335 OPT_CALLBACK('e', "event", NULL, "event",
86847b62
TG
336 "event selector. use 'perf list' to list available events",
337 parse_events),
5242519b
IM
338 OPT_BOOLEAN('i', "inherit", &inherit,
339 "child tasks inherit counters"),
340 OPT_INTEGER('p', "pid", &target_pid,
341 "stat events on existing pid"),
342 OPT_BOOLEAN('a', "all-cpus", &system_wide,
343 "system-wide collection from all CPUs"),
86847b62 344 OPT_BOOLEAN('S', "scale", &scale,
5242519b 345 "scale/normalize counters"),
743ee1f8
IM
346 OPT_BOOLEAN('v', "verbose", &verbose,
347 "be more verbose (show counter open errors, etc)"),
5242519b
IM
348 OPT_END()
349};
350
351int cmd_stat(int argc, const char **argv, const char *prefix)
352{
5242519b
IM
353 page_size = sysconf(_SC_PAGE_SIZE);
354
a21ca2ca 355 memcpy(attrs, default_attrs, sizeof(attrs));
5242519b
IM
356
357 argc = parse_options(argc, argv, options, stat_usage, 0);
358 if (!argc)
359 usage_with_options(stat_usage, options);
ddcacfa0 360
a21ca2ca 361 if (!nr_counters)
ddcacfa0 362 nr_counters = 8;
ddcacfa0 363
ddcacfa0
IM
364 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
365 assert(nr_cpus <= MAX_NR_CPUS);
366 assert(nr_cpus >= 0);
367
58d7e993
IM
368 /*
369 * We dont want to block the signals - that would cause
370 * child tasks to inherit that and Ctrl-C would not work.
371 * What we want is for Ctrl-C to work in the exec()-ed
372 * task, but being ignored by perf stat itself:
373 */
f7b7c26e 374 atexit(sig_atexit);
58d7e993
IM
375 signal(SIGINT, skip_signal);
376 signal(SIGALRM, skip_signal);
377 signal(SIGABRT, skip_signal);
378
743ee1f8 379 return do_perf_stat(argc, argv);
ddcacfa0 380}
This page took 0.054763 seconds and 5 git commands to generate.