blob: e5f6ece65a1386f51b21f8b537a43fc9833275ea [file] [log] [blame]
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001/*
Ingo Molnarbf9e1872009-06-02 23:37:05 +02002 * 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:
Ingo Molnarddcacfa2009-04-20 15:37:32 +02008
Ingo Molnarbf9e1872009-06-02 23:37:05 +02009 $ perf stat ~/hackbench 10
10 Time: 0.104
Ingo Molnarddcacfa2009-04-20 15:37:32 +020011
Ingo Molnarbf9e1872009-06-02 23:37:05 +020012 Performance counter stats for '/home/mingo/hackbench':
Ingo Molnarddcacfa2009-04-20 15:37:32 +020013
Ingo Molnarbf9e1872009-06-02 23:37:05 +020014 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
Ingo Molnarddcacfa2009-04-20 15:37:32 +020022
Ingo Molnarbf9e1872009-06-02 23:37:05 +020023 Wall-clock time elapsed: 123.786620 msecs
Ingo Molnarddcacfa2009-04-20 15:37:32 +020024
Ingo Molnar52425192009-05-26 09:17:18 +020025 *
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>
Jaswinder Singh Rajput6e750a8f2009-06-27 03:02:07 +053035 * Jaswinder Singh Rajput <jaswinder@kernel.org>
Ingo Molnar52425192009-05-26 09:17:18 +020036 *
37 * Released under the GPL v2. (and only v2, not any later version)
Ingo Molnarddcacfa2009-04-20 15:37:32 +020038 */
39
Peter Zijlstra1a482f32009-05-23 18:28:58 +020040#include "perf.h"
Ingo Molnar16f762a2009-05-27 09:10:38 +020041#include "builtin.h"
Ingo Molnar148be2c2009-04-27 08:02:14 +020042#include "util/util.h"
Ingo Molnar52425192009-05-26 09:17:18 +020043#include "util/parse-options.h"
44#include "util/parse-events.h"
Frederic Weisbecker8f28827a2009-08-16 22:05:48 +020045#include "util/event.h"
46#include "util/debug.h"
Ingo Molnarddcacfa2009-04-20 15:37:32 +020047
Ingo Molnarddcacfa2009-04-20 15:37:32 +020048#include <sys/prctl.h>
Ingo Molnar42202dd2009-06-13 14:57:28 +020049#include <math.h>
Peter Zijlstra16c8a102009-05-05 17:50:27 +020050
Ingo Molnarcdd6c482009-09-21 12:02:48 +020051static struct perf_event_attr default_attrs[] = {
Ingo Molnara21ca2c2009-06-06 09:58:57 +020052
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +020053 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
54 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES},
55 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
56 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
Ingo Molnara21ca2c2009-06-06 09:58:57 +020057
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +020058 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
59 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
60 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CACHE_REFERENCES},
61 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CACHE_MISSES },
62
Ingo Molnara21ca2c2009-06-06 09:58:57 +020063};
64
Ingo Molnarddcacfa2009-04-20 15:37:32 +020065static int system_wide = 0;
Ingo Molnarf37a2912009-07-01 12:37:06 +020066static unsigned int nr_cpus = 0;
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +053067static int run_idx = 0;
68
69static int run_count = 1;
70static int inherit = 1;
71static int scale = 1;
72static int target_pid = -1;
Ingo Molnar0cfb7a12009-06-27 06:10:30 +020073static int null_run = 0;
Ingo Molnarddcacfa2009-04-20 15:37:32 +020074
Ingo Molnarddcacfa2009-04-20 15:37:32 +020075static int fd[MAX_NR_CPUS][MAX_COUNTERS];
76
Peter Zijlstra63d40de2009-09-04 17:03:13 +020077static int event_scaled[MAX_COUNTERS];
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +053078
Peter Zijlstra506d4bc2009-09-04 15:36:12 +020079struct stats
80{
Peter Zijlstra8a026312009-09-04 17:26:26 +020081 double n, mean, M2;
Peter Zijlstra506d4bc2009-09-04 15:36:12 +020082};
Ingo Molnar42202dd2009-06-13 14:57:28 +020083
Peter Zijlstra9e9772c2009-09-04 15:36:08 +020084static void update_stats(struct stats *stats, u64 val)
85{
Peter Zijlstra8a026312009-09-04 17:26:26 +020086 double delta;
Peter Zijlstra9e9772c2009-09-04 15:36:08 +020087
Peter Zijlstra8a026312009-09-04 17:26:26 +020088 stats->n++;
89 delta = val - stats->mean;
90 stats->mean += delta / stats->n;
91 stats->M2 += delta*(val - stats->mean);
Peter Zijlstra9e9772c2009-09-04 15:36:08 +020092}
93
Peter Zijlstra506d4bc2009-09-04 15:36:12 +020094static double avg_stats(struct stats *stats)
95{
Peter Zijlstra8a026312009-09-04 17:26:26 +020096 return stats->mean;
Peter Zijlstra506d4bc2009-09-04 15:36:12 +020097}
Ingo Molnar42202dd2009-06-13 14:57:28 +020098
Peter Zijlstra506d4bc2009-09-04 15:36:12 +020099/*
Peter Zijlstra63d40de2009-09-04 17:03:13 +0200100 * http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
101 *
Peter Zijlstra8a026312009-09-04 17:26:26 +0200102 * (\Sum n_i^2) - ((\Sum n_i)^2)/n
103 * s^2 = -------------------------------
104 * n - 1
Peter Zijlstra63d40de2009-09-04 17:03:13 +0200105 *
106 * http://en.wikipedia.org/wiki/Stddev
107 *
108 * The std dev of the mean is related to the std dev by:
109 *
110 * s
111 * s_mean = -------
112 * sqrt(n)
113 *
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200114 */
115static double stddev_stats(struct stats *stats)
116{
Peter Zijlstra8a026312009-09-04 17:26:26 +0200117 double variance = stats->M2 / (stats->n - 1);
118 double variance_mean = variance / stats->n;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200119
Peter Zijlstra63d40de2009-09-04 17:03:13 +0200120 return sqrt(variance_mean);
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200121}
Ingo Molnar42202dd2009-06-13 14:57:28 +0200122
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200123struct stats event_res_stats[MAX_COUNTERS][3];
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200124struct stats runtime_nsecs_stats;
125struct stats walltime_nsecs_stats;
126struct stats runtime_cycles_stats;
Ingo Molnarbe1ac0d2009-05-29 09:10:54 +0200127
Jaswinder Singh Rajputb9ebdcc2009-07-01 15:05:09 +0530128#define MATCH_EVENT(t, c, counter) \
129 (attrs[counter].type == PERF_TYPE_##t && \
130 attrs[counter].config == PERF_COUNT_##c)
131
Jaswinder Singh Rajputcca03c02009-06-23 17:12:49 +0530132#define ERR_PERF_OPEN \
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200133"Error: counter %d, sys_perf_event_open() syscall returned with %d (%s)\n"
Jaswinder Singh Rajputcca03c02009-06-23 17:12:49 +0530134
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000135static void create_perf_stat_counter(int counter, int pid)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200136{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200137 struct perf_event_attr *attr = attrs + counter;
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200138
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200139 if (scale)
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200140 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
141 PERF_FORMAT_TOTAL_TIME_RUNNING;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200142
143 if (system_wide) {
Ingo Molnarf37a2912009-07-01 12:37:06 +0200144 unsigned int cpu;
145
Jaswinder Singh Rajputcca03c02009-06-23 17:12:49 +0530146 for (cpu = 0; cpu < nr_cpus; cpu++) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200147 fd[cpu][counter] = sys_perf_event_open(attr, -1, cpu, -1, 0);
Jaswinder Singh Rajputcca03c02009-06-23 17:12:49 +0530148 if (fd[cpu][counter] < 0 && verbose)
149 fprintf(stderr, ERR_PERF_OPEN, counter,
150 fd[cpu][counter], strerror(errno));
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200151 }
152 } else {
Paul Mackerras57e79862009-06-30 16:07:19 +1000153 attr->inherit = inherit;
154 attr->disabled = 1;
155 attr->enable_on_exec = 1;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200156
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200157 fd[0][counter] = sys_perf_event_open(attr, pid, -1, -1, 0);
Jaswinder Singh Rajputcca03c02009-06-23 17:12:49 +0530158 if (fd[0][counter] < 0 && verbose)
159 fprintf(stderr, ERR_PERF_OPEN, counter,
160 fd[0][counter], strerror(errno));
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200161 }
162}
163
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200164/*
165 * Does the counter have nsecs as a unit?
166 */
167static inline int nsec_counter(int counter)
168{
Jaswinder Singh Rajputb9ebdcc2009-07-01 15:05:09 +0530169 if (MATCH_EVENT(SOFTWARE, SW_CPU_CLOCK, counter) ||
170 MATCH_EVENT(SOFTWARE, SW_TASK_CLOCK, counter))
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200171 return 1;
172
173 return 0;
174}
175
176/*
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200177 * Read out the results of a single counter:
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200178 */
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200179static void read_counter(int counter)
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200180{
Peter Zijlstra849abde2009-09-04 18:23:38 +0200181 u64 count[3], single_count[3];
Ingo Molnarf37a2912009-07-01 12:37:06 +0200182 unsigned int cpu;
183 size_t res, nv;
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200184 int scaled;
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200185 int i;
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200186
187 count[0] = count[1] = count[2] = 0;
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200188
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200189 nv = scale ? 3 : 1;
Jaswinder Singh Rajputcca03c02009-06-23 17:12:49 +0530190 for (cpu = 0; cpu < nr_cpus; cpu++) {
Ingo Molnar743ee1f2009-06-07 17:06:46 +0200191 if (fd[cpu][counter] < 0)
192 continue;
193
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000194 res = read(fd[cpu][counter], single_count, nv * sizeof(u64));
195 assert(res == nv * sizeof(u64));
Ingo Molnarf37a2912009-07-01 12:37:06 +0200196
Ingo Molnar42202dd2009-06-13 14:57:28 +0200197 close(fd[cpu][counter]);
198 fd[cpu][counter] = -1;
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200199
200 count[0] += single_count[0];
201 if (scale) {
202 count[1] += single_count[1];
203 count[2] += single_count[2];
204 }
205 }
206
207 scaled = 0;
208 if (scale) {
209 if (count[2] == 0) {
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200210 event_scaled[counter] = -1;
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200211 count[0] = 0;
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200212 return;
213 }
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200214
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200215 if (count[2] < count[1]) {
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200216 event_scaled[counter] = 1;
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200217 count[0] = (unsigned long long)
218 ((double)count[0] * count[1] / count[2] + 0.5);
219 }
220 }
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200221
222 for (i = 0; i < 3; i++)
223 update_stats(&event_res_stats[counter][i], count[i]);
224
225 if (verbose) {
226 fprintf(stderr, "%s: %Ld %Ld %Ld\n", event_name(counter),
227 count[0], count[1], count[2]);
228 }
229
Ingo Molnarbe1ac0d2009-05-29 09:10:54 +0200230 /*
231 * Save the full runtime - to allow normalization during printout:
232 */
Jaswinder Singh Rajputb9ebdcc2009-07-01 15:05:09 +0530233 if (MATCH_EVENT(SOFTWARE, SW_TASK_CLOCK, counter))
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200234 update_stats(&runtime_nsecs_stats, count[0]);
Jaswinder Singh Rajputb9ebdcc2009-07-01 15:05:09 +0530235 if (MATCH_EVENT(HARDWARE, HW_CPU_CYCLES, counter))
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200236 update_stats(&runtime_cycles_stats, count[0]);
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200237}
238
Ingo Molnarf37a2912009-07-01 12:37:06 +0200239static int run_perf_stat(int argc __used, const char **argv)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200240{
241 unsigned long long t0, t1;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200242 int status = 0;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200243 int counter;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200244 int pid;
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000245 int child_ready_pipe[2], go_pipe[2];
246 char buf;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200247
248 if (!system_wide)
249 nr_cpus = 1;
250
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000251 if (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0) {
252 perror("failed to create pipes");
253 exit(1);
254 }
255
256 if ((pid = fork()) < 0)
257 perror("failed to fork");
258
259 if (!pid) {
260 close(child_ready_pipe[0]);
261 close(go_pipe[1]);
262 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
263
264 /*
265 * Do a dummy execvp to get the PLT entry resolved,
266 * so we avoid the resolver overhead on the real
267 * execvp call.
268 */
269 execvp("", (char **)argv);
270
271 /*
272 * Tell the parent we're ready to go
273 */
274 close(child_ready_pipe[1]);
275
276 /*
277 * Wait until the parent tells us to go.
278 */
Frederic Weisbeckera92bef02009-07-01 21:02:10 +0200279 if (read(go_pipe[0], &buf, 1) == -1)
280 perror("unable to read pipe");
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000281
282 execvp(argv[0], (char **)argv);
283
284 perror(argv[0]);
285 exit(-1);
286 }
287
288 /*
289 * Wait for the child to be ready to exec.
290 */
291 close(child_ready_pipe[1]);
292 close(go_pipe[0]);
Frederic Weisbeckera92bef02009-07-01 21:02:10 +0200293 if (read(child_ready_pipe[0], &buf, 1) == -1)
294 perror("unable to read pipe");
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000295 close(child_ready_pipe[0]);
296
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200297 for (counter = 0; counter < nr_counters; counter++)
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000298 create_perf_stat_counter(counter, pid);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200299
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200300 /*
301 * Enable counters and exec the command:
302 */
303 t0 = rdclock();
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200304
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000305 close(go_pipe[1]);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200306 wait(&status);
Ingo Molnar44db76c2009-06-03 19:36:07 +0200307
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200308 t1 = rdclock();
309
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200310 update_stats(&walltime_nsecs_stats, t1 - t0);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200311
312 for (counter = 0; counter < nr_counters; counter++)
313 read_counter(counter);
314
315 return WEXITSTATUS(status);
316}
317
Peter Zijlstra849abde2009-09-04 18:23:38 +0200318static void print_noise(int counter, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200319{
Peter Zijlstra849abde2009-09-04 18:23:38 +0200320 if (run_count == 1)
321 return;
322
323 fprintf(stderr, " ( +- %7.3f%% )",
324 100 * stddev_stats(&event_res_stats[counter][0]) / avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200325}
326
Peter Zijlstra849abde2009-09-04 18:23:38 +0200327static void nsec_printout(int counter, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200328{
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200329 double msecs = avg / 1e6;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200330
Jaswinder Singh Rajput6e750a8f2009-06-27 03:02:07 +0530331 fprintf(stderr, " %14.6f %-24s", msecs, event_name(counter));
Ingo Molnar42202dd2009-06-13 14:57:28 +0200332
Jaswinder Singh Rajputb9ebdcc2009-07-01 15:05:09 +0530333 if (MATCH_EVENT(SOFTWARE, SW_TASK_CLOCK, counter)) {
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200334 fprintf(stderr, " # %10.3f CPUs ",
335 avg / avg_stats(&walltime_nsecs_stats));
Ingo Molnar42202dd2009-06-13 14:57:28 +0200336 }
Ingo Molnar42202dd2009-06-13 14:57:28 +0200337}
338
Peter Zijlstra849abde2009-09-04 18:23:38 +0200339static void abs_printout(int counter, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200340{
Ingo Molnarc7f7fea2009-09-22 14:53:51 +0200341 double total, ratio = 0.0;
342
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200343 fprintf(stderr, " %14.0f %-24s", avg, event_name(counter));
Ingo Molnar42202dd2009-06-13 14:57:28 +0200344
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200345 if (MATCH_EVENT(HARDWARE, HW_INSTRUCTIONS, counter)) {
Ingo Molnarc7f7fea2009-09-22 14:53:51 +0200346 total = avg_stats(&runtime_cycles_stats);
347
348 if (total)
349 ratio = avg / total;
350
351 fprintf(stderr, " # %10.3f IPC ", ratio);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200352 } else {
Ingo Molnarc7f7fea2009-09-22 14:53:51 +0200353 total = avg_stats(&runtime_nsecs_stats);
354
355 if (total)
356 ratio = 1000.0 * avg / total;
357
358 fprintf(stderr, " # %10.3f M/sec", ratio);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200359 }
Ingo Molnar42202dd2009-06-13 14:57:28 +0200360}
361
362/*
363 * Print out the results of a single counter:
364 */
365static void print_counter(int counter)
366{
Peter Zijlstra849abde2009-09-04 18:23:38 +0200367 double avg = avg_stats(&event_res_stats[counter][0]);
Peter Zijlstra63d40de2009-09-04 17:03:13 +0200368 int scaled = event_scaled[counter];
Ingo Molnar42202dd2009-06-13 14:57:28 +0200369
Ingo Molnar42202dd2009-06-13 14:57:28 +0200370 if (scaled == -1) {
Jaswinder Singh Rajput6e750a8f2009-06-27 03:02:07 +0530371 fprintf(stderr, " %14s %-24s\n",
Ingo Molnar42202dd2009-06-13 14:57:28 +0200372 "<not counted>", event_name(counter));
373 return;
374 }
375
376 if (nsec_counter(counter))
Peter Zijlstra849abde2009-09-04 18:23:38 +0200377 nsec_printout(counter, avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200378 else
Peter Zijlstra849abde2009-09-04 18:23:38 +0200379 abs_printout(counter, avg);
380
381 print_noise(counter, avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200382
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200383 if (scaled) {
384 double avg_enabled, avg_running;
385
386 avg_enabled = avg_stats(&event_res_stats[counter][1]);
387 avg_running = avg_stats(&event_res_stats[counter][2]);
388
Ingo Molnar210ad392009-06-29 21:50:54 +0200389 fprintf(stderr, " (scaled from %.2f%%)",
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200390 100 * avg_running / avg_enabled);
391 }
Ingo Molnar42202dd2009-06-13 14:57:28 +0200392
393 fprintf(stderr, "\n");
394}
395
Ingo Molnar42202dd2009-06-13 14:57:28 +0200396static void print_stat(int argc, const char **argv)
397{
398 int i, counter;
399
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200400 fflush(stdout);
401
402 fprintf(stderr, "\n");
Ingo Molnar44db76c2009-06-03 19:36:07 +0200403 fprintf(stderr, " Performance counter stats for \'%s", argv[0]);
404
405 for (i = 1; i < argc; i++)
406 fprintf(stderr, " %s", argv[i]);
407
Ingo Molnar42202dd2009-06-13 14:57:28 +0200408 fprintf(stderr, "\'");
409 if (run_count > 1)
410 fprintf(stderr, " (%d runs)", run_count);
411 fprintf(stderr, ":\n\n");
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200412
413 for (counter = 0; counter < nr_counters; counter++)
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200414 print_counter(counter);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200415
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200416 fprintf(stderr, "\n");
Ingo Molnar566747e2009-06-27 06:24:32 +0200417 fprintf(stderr, " %14.9f seconds time elapsed",
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200418 avg_stats(&walltime_nsecs_stats)/1e9);
Ingo Molnar566747e2009-06-27 06:24:32 +0200419 if (run_count > 1) {
420 fprintf(stderr, " ( +- %7.3f%% )",
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200421 100*stddev_stats(&walltime_nsecs_stats) /
422 avg_stats(&walltime_nsecs_stats));
Ingo Molnar566747e2009-06-27 06:24:32 +0200423 }
424 fprintf(stderr, "\n\n");
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200425}
426
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200427static volatile int signr = -1;
428
Ingo Molnar52425192009-05-26 09:17:18 +0200429static void skip_signal(int signo)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200430{
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200431 signr = signo;
432}
433
434static void sig_atexit(void)
435{
436 if (signr == -1)
437 return;
438
439 signal(signr, SIG_DFL);
440 kill(getpid(), signr);
Ingo Molnar52425192009-05-26 09:17:18 +0200441}
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200442
Ingo Molnar52425192009-05-26 09:17:18 +0200443static const char * const stat_usage[] = {
444 "perf stat [<options>] <command>",
445 NULL
446};
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200447
Ingo Molnar52425192009-05-26 09:17:18 +0200448static const struct option options[] = {
449 OPT_CALLBACK('e', "event", NULL, "event",
Thomas Gleixner86847b62009-06-06 12:24:17 +0200450 "event selector. use 'perf list' to list available events",
451 parse_events),
Ingo Molnar52425192009-05-26 09:17:18 +0200452 OPT_BOOLEAN('i', "inherit", &inherit,
453 "child tasks inherit counters"),
454 OPT_INTEGER('p', "pid", &target_pid,
455 "stat events on existing pid"),
456 OPT_BOOLEAN('a', "all-cpus", &system_wide,
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530457 "system-wide collection from all CPUs"),
Brice Goglinb26bc5a2009-08-07 10:18:39 +0200458 OPT_BOOLEAN('c', "scale", &scale,
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530459 "scale/normalize counters"),
Ingo Molnar743ee1f2009-06-07 17:06:46 +0200460 OPT_BOOLEAN('v', "verbose", &verbose,
461 "be more verbose (show counter open errors, etc)"),
Ingo Molnar42202dd2009-06-13 14:57:28 +0200462 OPT_INTEGER('r', "repeat", &run_count,
463 "repeat command and print average + stddev (max: 100)"),
Ingo Molnar0cfb7a12009-06-27 06:10:30 +0200464 OPT_BOOLEAN('n', "null", &null_run,
465 "null run - dont start any counters"),
Ingo Molnar52425192009-05-26 09:17:18 +0200466 OPT_END()
467};
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200468
Ingo Molnarf37a2912009-07-01 12:37:06 +0200469int cmd_stat(int argc, const char **argv, const char *prefix __used)
Ingo Molnar52425192009-05-26 09:17:18 +0200470{
Ingo Molnar42202dd2009-06-13 14:57:28 +0200471 int status;
472
Anton Blancharda0541232009-07-22 23:04:12 +1000473 argc = parse_options(argc, argv, options, stat_usage,
474 PARSE_OPT_STOP_AT_NON_OPTION);
Ingo Molnar52425192009-05-26 09:17:18 +0200475 if (!argc)
476 usage_with_options(stat_usage, options);
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200477 if (run_count <= 0)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200478 usage_with_options(stat_usage, options);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200479
Jaswinder Singh Rajputc3043562009-06-27 23:49:09 +0530480 /* Set attrs and nr_counters if no event is selected and !null_run */
481 if (!null_run && !nr_counters) {
482 memcpy(attrs, default_attrs, sizeof(default_attrs));
483 nr_counters = ARRAY_SIZE(default_attrs);
484 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200485
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200486 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
487 assert(nr_cpus <= MAX_NR_CPUS);
Ingo Molnarf37a2912009-07-01 12:37:06 +0200488 assert((int)nr_cpus >= 0);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200489
Ingo Molnar58d7e992009-05-15 11:03:23 +0200490 /*
491 * We dont want to block the signals - that would cause
492 * child tasks to inherit that and Ctrl-C would not work.
493 * What we want is for Ctrl-C to work in the exec()-ed
494 * task, but being ignored by perf stat itself:
495 */
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200496 atexit(sig_atexit);
Ingo Molnar58d7e992009-05-15 11:03:23 +0200497 signal(SIGINT, skip_signal);
498 signal(SIGALRM, skip_signal);
499 signal(SIGABRT, skip_signal);
500
Ingo Molnar42202dd2009-06-13 14:57:28 +0200501 status = 0;
502 for (run_idx = 0; run_idx < run_count; run_idx++) {
503 if (run_count != 1 && verbose)
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530504 fprintf(stderr, "[ perf stat: executing run #%d ... ]\n", run_idx + 1);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200505 status = run_perf_stat(argc, argv);
506 }
507
508 print_stat(argc, argv);
509
510 return status;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200511}