blob: 5f41244cbbf2d5ce0f506f7b474a98c6db666628 [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 Weisbecker8f288272009-08-16 22:05:48 +020045#include "util/event.h"
46#include "util/debug.h"
Liming Wang60666c62009-12-31 16:05:50 +080047#include "util/header.h"
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110048#include "util/cpumap.h"
Ingo Molnarddcacfa2009-04-20 15:37:32 +020049
Ingo Molnarddcacfa2009-04-20 15:37:32 +020050#include <sys/prctl.h>
Ingo Molnar42202dd2009-06-13 14:57:28 +020051#include <math.h>
Peter Zijlstra16c8a102009-05-05 17:50:27 +020052
Ingo Molnarcdd6c482009-09-21 12:02:48 +020053static struct perf_event_attr default_attrs[] = {
Ingo Molnara21ca2c2009-06-06 09:58:57 +020054
Ingo Molnar56aab462009-10-19 13:27:08 +020055 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
56 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
57 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
58 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
Ingo Molnara21ca2c2009-06-06 09:58:57 +020059
Ingo Molnar56aab462009-10-19 13:27:08 +020060 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
61 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
Ingo Molnar56aab462009-10-19 13:27:08 +020062 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
63 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
Ingo Molnardd86e722009-10-19 13:33:03 +020064 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CACHE_REFERENCES },
65 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CACHE_MISSES },
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +020066
Ingo Molnara21ca2c2009-06-06 09:58:57 +020067};
68
Ingo Molnarddcacfa2009-04-20 15:37:32 +020069static int system_wide = 0;
Ingo Molnarf37a2912009-07-01 12:37:06 +020070static unsigned int nr_cpus = 0;
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +053071static int run_idx = 0;
72
73static int run_count = 1;
74static int inherit = 1;
75static int scale = 1;
Chris Wilson933da832009-10-04 01:35:01 +010076static pid_t target_pid = -1;
77static pid_t child_pid = -1;
Ingo Molnar0cfb7a12009-06-27 06:10:30 +020078static int null_run = 0;
Ingo Molnarddcacfa2009-04-20 15:37:32 +020079
Ingo Molnarddcacfa2009-04-20 15:37:32 +020080static int fd[MAX_NR_CPUS][MAX_COUNTERS];
81
Peter Zijlstra63d40de2009-09-04 17:03:13 +020082static int event_scaled[MAX_COUNTERS];
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +053083
Liming Wang60666c62009-12-31 16:05:50 +080084static volatile int done = 0;
85
Peter Zijlstra506d4bc2009-09-04 15:36:12 +020086struct stats
87{
Peter Zijlstra8a026312009-09-04 17:26:26 +020088 double n, mean, M2;
Peter Zijlstra506d4bc2009-09-04 15:36:12 +020089};
Ingo Molnar42202dd2009-06-13 14:57:28 +020090
Peter Zijlstra9e9772c2009-09-04 15:36:08 +020091static void update_stats(struct stats *stats, u64 val)
92{
Peter Zijlstra8a026312009-09-04 17:26:26 +020093 double delta;
Peter Zijlstra9e9772c2009-09-04 15:36:08 +020094
Peter Zijlstra8a026312009-09-04 17:26:26 +020095 stats->n++;
96 delta = val - stats->mean;
97 stats->mean += delta / stats->n;
98 stats->M2 += delta*(val - stats->mean);
Peter Zijlstra9e9772c2009-09-04 15:36:08 +020099}
100
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200101static double avg_stats(struct stats *stats)
102{
Peter Zijlstra8a026312009-09-04 17:26:26 +0200103 return stats->mean;
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200104}
Ingo Molnar42202dd2009-06-13 14:57:28 +0200105
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200106/*
Peter Zijlstra63d40de2009-09-04 17:03:13 +0200107 * http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
108 *
Peter Zijlstra8a026312009-09-04 17:26:26 +0200109 * (\Sum n_i^2) - ((\Sum n_i)^2)/n
110 * s^2 = -------------------------------
111 * n - 1
Peter Zijlstra63d40de2009-09-04 17:03:13 +0200112 *
113 * http://en.wikipedia.org/wiki/Stddev
114 *
115 * The std dev of the mean is related to the std dev by:
116 *
117 * s
118 * s_mean = -------
119 * sqrt(n)
120 *
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200121 */
122static double stddev_stats(struct stats *stats)
123{
Peter Zijlstra8a026312009-09-04 17:26:26 +0200124 double variance = stats->M2 / (stats->n - 1);
125 double variance_mean = variance / stats->n;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200126
Peter Zijlstra63d40de2009-09-04 17:03:13 +0200127 return sqrt(variance_mean);
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200128}
Ingo Molnar42202dd2009-06-13 14:57:28 +0200129
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200130struct stats event_res_stats[MAX_COUNTERS][3];
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200131struct stats runtime_nsecs_stats;
132struct stats walltime_nsecs_stats;
133struct stats runtime_cycles_stats;
Anton Blanchard11018202009-10-18 22:29:23 +1100134struct stats runtime_branches_stats;
Ingo Molnarbe1ac0d2009-05-29 09:10:54 +0200135
Jaswinder Singh Rajputb9ebdcc2009-07-01 15:05:09 +0530136#define MATCH_EVENT(t, c, counter) \
137 (attrs[counter].type == PERF_TYPE_##t && \
138 attrs[counter].config == PERF_COUNT_##c)
139
Jaswinder Singh Rajputcca03c0a2009-06-23 17:12:49 +0530140#define ERR_PERF_OPEN \
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200141"Error: counter %d, sys_perf_event_open() syscall returned with %d (%s)\n"
Jaswinder Singh Rajputcca03c0a2009-06-23 17:12:49 +0530142
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000143static void create_perf_stat_counter(int counter, int pid)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200144{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200145 struct perf_event_attr *attr = attrs + counter;
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200146
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200147 if (scale)
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200148 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
149 PERF_FORMAT_TOTAL_TIME_RUNNING;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200150
151 if (system_wide) {
Ingo Molnarf37a2912009-07-01 12:37:06 +0200152 unsigned int cpu;
153
Jaswinder Singh Rajputcca03c0a2009-06-23 17:12:49 +0530154 for (cpu = 0; cpu < nr_cpus; cpu++) {
Paul Mackerrasa12b51c2010-03-10 20:36:09 +1100155 fd[cpu][counter] = sys_perf_event_open(attr, -1, cpumap[cpu], -1, 0);
Jaswinder Singh Rajputcca03c0a2009-06-23 17:12:49 +0530156 if (fd[cpu][counter] < 0 && verbose)
157 fprintf(stderr, ERR_PERF_OPEN, counter,
158 fd[cpu][counter], strerror(errno));
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200159 }
160 } else {
Paul Mackerras57e79862009-06-30 16:07:19 +1000161 attr->inherit = inherit;
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300162 if (target_pid == -1) {
163 attr->disabled = 1;
164 attr->enable_on_exec = 1;
165 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200166
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200167 fd[0][counter] = sys_perf_event_open(attr, pid, -1, -1, 0);
Jaswinder Singh Rajputcca03c0a2009-06-23 17:12:49 +0530168 if (fd[0][counter] < 0 && verbose)
169 fprintf(stderr, ERR_PERF_OPEN, counter,
170 fd[0][counter], strerror(errno));
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200171 }
172}
173
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200174/*
175 * Does the counter have nsecs as a unit?
176 */
177static inline int nsec_counter(int counter)
178{
Jaswinder Singh Rajputb9ebdcc2009-07-01 15:05:09 +0530179 if (MATCH_EVENT(SOFTWARE, SW_CPU_CLOCK, counter) ||
180 MATCH_EVENT(SOFTWARE, SW_TASK_CLOCK, counter))
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200181 return 1;
182
183 return 0;
184}
185
186/*
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200187 * Read out the results of a single counter:
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200188 */
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200189static void read_counter(int counter)
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200190{
Peter Zijlstra849abde2009-09-04 18:23:38 +0200191 u64 count[3], single_count[3];
Ingo Molnarf37a2912009-07-01 12:37:06 +0200192 unsigned int cpu;
193 size_t res, nv;
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200194 int scaled;
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200195 int i;
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200196
197 count[0] = count[1] = count[2] = 0;
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200198
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200199 nv = scale ? 3 : 1;
Jaswinder Singh Rajputcca03c0a2009-06-23 17:12:49 +0530200 for (cpu = 0; cpu < nr_cpus; cpu++) {
Ingo Molnar743ee1f2009-06-07 17:06:46 +0200201 if (fd[cpu][counter] < 0)
202 continue;
203
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000204 res = read(fd[cpu][counter], single_count, nv * sizeof(u64));
205 assert(res == nv * sizeof(u64));
Ingo Molnarf37a2912009-07-01 12:37:06 +0200206
Ingo Molnar42202dd2009-06-13 14:57:28 +0200207 close(fd[cpu][counter]);
208 fd[cpu][counter] = -1;
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200209
210 count[0] += single_count[0];
211 if (scale) {
212 count[1] += single_count[1];
213 count[2] += single_count[2];
214 }
215 }
216
217 scaled = 0;
218 if (scale) {
219 if (count[2] == 0) {
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200220 event_scaled[counter] = -1;
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200221 count[0] = 0;
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200222 return;
223 }
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200224
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200225 if (count[2] < count[1]) {
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200226 event_scaled[counter] = 1;
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200227 count[0] = (unsigned long long)
228 ((double)count[0] * count[1] / count[2] + 0.5);
229 }
230 }
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200231
232 for (i = 0; i < 3; i++)
233 update_stats(&event_res_stats[counter][i], count[i]);
234
235 if (verbose) {
236 fprintf(stderr, "%s: %Ld %Ld %Ld\n", event_name(counter),
237 count[0], count[1], count[2]);
238 }
239
Ingo Molnarbe1ac0d2009-05-29 09:10:54 +0200240 /*
241 * Save the full runtime - to allow normalization during printout:
242 */
Jaswinder Singh Rajputb9ebdcc2009-07-01 15:05:09 +0530243 if (MATCH_EVENT(SOFTWARE, SW_TASK_CLOCK, counter))
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200244 update_stats(&runtime_nsecs_stats, count[0]);
Jaswinder Singh Rajputb9ebdcc2009-07-01 15:05:09 +0530245 if (MATCH_EVENT(HARDWARE, HW_CPU_CYCLES, counter))
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200246 update_stats(&runtime_cycles_stats, count[0]);
Anton Blanchard11018202009-10-18 22:29:23 +1100247 if (MATCH_EVENT(HARDWARE, HW_BRANCH_INSTRUCTIONS, counter))
248 update_stats(&runtime_branches_stats, count[0]);
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200249}
250
Ingo Molnarf37a2912009-07-01 12:37:06 +0200251static int run_perf_stat(int argc __used, const char **argv)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200252{
253 unsigned long long t0, t1;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200254 int status = 0;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200255 int counter;
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300256 int pid;
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000257 int child_ready_pipe[2], go_pipe[2];
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300258 const bool forks = (argc > 0);
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000259 char buf;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200260
261 if (!system_wide)
262 nr_cpus = 1;
263
Liming Wang60666c62009-12-31 16:05:50 +0800264 if (forks && (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0)) {
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000265 perror("failed to create pipes");
266 exit(1);
267 }
268
Liming Wang60666c62009-12-31 16:05:50 +0800269 if (forks) {
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300270 if ((child_pid = fork()) < 0)
Liming Wang60666c62009-12-31 16:05:50 +0800271 perror("failed to fork");
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000272
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300273 if (!child_pid) {
Liming Wang60666c62009-12-31 16:05:50 +0800274 close(child_ready_pipe[0]);
275 close(go_pipe[1]);
276 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
277
278 /*
279 * Do a dummy execvp to get the PLT entry resolved,
280 * so we avoid the resolver overhead on the real
281 * execvp call.
282 */
283 execvp("", (char **)argv);
284
285 /*
286 * Tell the parent we're ready to go
287 */
288 close(child_ready_pipe[1]);
289
290 /*
291 * Wait until the parent tells us to go.
292 */
293 if (read(go_pipe[0], &buf, 1) == -1)
294 perror("unable to read pipe");
295
296 execvp(argv[0], (char **)argv);
297
298 perror(argv[0]);
299 exit(-1);
300 }
301
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000302 /*
Liming Wang60666c62009-12-31 16:05:50 +0800303 * Wait for the child to be ready to exec.
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000304 */
305 close(child_ready_pipe[1]);
Liming Wang60666c62009-12-31 16:05:50 +0800306 close(go_pipe[0]);
307 if (read(child_ready_pipe[0], &buf, 1) == -1)
Frederic Weisbeckera92bef02009-07-01 21:02:10 +0200308 perror("unable to read pipe");
Liming Wang60666c62009-12-31 16:05:50 +0800309 close(child_ready_pipe[0]);
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000310 }
311
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300312 if (target_pid == -1)
313 pid = child_pid;
314 else
315 pid = target_pid;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200316 for (counter = 0; counter < nr_counters; counter++)
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000317 create_perf_stat_counter(counter, pid);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200318
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200319 /*
320 * Enable counters and exec the command:
321 */
322 t0 = rdclock();
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200323
Liming Wang60666c62009-12-31 16:05:50 +0800324 if (forks) {
325 close(go_pipe[1]);
326 wait(&status);
327 } else {
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300328 while(!done) sleep(1);
Liming Wang60666c62009-12-31 16:05:50 +0800329 }
Ingo Molnar44db76c2009-06-03 19:36:07 +0200330
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200331 t1 = rdclock();
332
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200333 update_stats(&walltime_nsecs_stats, t1 - t0);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200334
335 for (counter = 0; counter < nr_counters; counter++)
336 read_counter(counter);
337
338 return WEXITSTATUS(status);
339}
340
Peter Zijlstra849abde2009-09-04 18:23:38 +0200341static void print_noise(int counter, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200342{
Peter Zijlstra849abde2009-09-04 18:23:38 +0200343 if (run_count == 1)
344 return;
345
346 fprintf(stderr, " ( +- %7.3f%% )",
347 100 * stddev_stats(&event_res_stats[counter][0]) / avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200348}
349
Peter Zijlstra849abde2009-09-04 18:23:38 +0200350static void nsec_printout(int counter, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200351{
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200352 double msecs = avg / 1e6;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200353
Jaswinder Singh Rajput6e750a8f2009-06-27 03:02:07 +0530354 fprintf(stderr, " %14.6f %-24s", msecs, event_name(counter));
Ingo Molnar42202dd2009-06-13 14:57:28 +0200355
Jaswinder Singh Rajputb9ebdcc2009-07-01 15:05:09 +0530356 if (MATCH_EVENT(SOFTWARE, SW_TASK_CLOCK, counter)) {
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200357 fprintf(stderr, " # %10.3f CPUs ",
358 avg / avg_stats(&walltime_nsecs_stats));
Ingo Molnar42202dd2009-06-13 14:57:28 +0200359 }
Ingo Molnar42202dd2009-06-13 14:57:28 +0200360}
361
Peter Zijlstra849abde2009-09-04 18:23:38 +0200362static void abs_printout(int counter, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200363{
Ingo Molnarc7f7fea2009-09-22 14:53:51 +0200364 double total, ratio = 0.0;
365
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200366 fprintf(stderr, " %14.0f %-24s", avg, event_name(counter));
Ingo Molnar42202dd2009-06-13 14:57:28 +0200367
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200368 if (MATCH_EVENT(HARDWARE, HW_INSTRUCTIONS, counter)) {
Ingo Molnarc7f7fea2009-09-22 14:53:51 +0200369 total = avg_stats(&runtime_cycles_stats);
370
371 if (total)
372 ratio = avg / total;
373
374 fprintf(stderr, " # %10.3f IPC ", ratio);
Lucas De Marchi7255fe22009-11-15 12:05:08 -0200375 } else if (MATCH_EVENT(HARDWARE, HW_BRANCH_MISSES, counter) &&
376 runtime_branches_stats.n != 0) {
Anton Blanchard11018202009-10-18 22:29:23 +1100377 total = avg_stats(&runtime_branches_stats);
378
379 if (total)
380 ratio = avg * 100 / total;
381
Ingo Molnardd86e722009-10-19 13:33:03 +0200382 fprintf(stderr, " # %10.3f %% ", ratio);
Anton Blanchard11018202009-10-18 22:29:23 +1100383
Lucas De Marchi7255fe22009-11-15 12:05:08 -0200384 } else if (runtime_nsecs_stats.n != 0) {
Ingo Molnarc7f7fea2009-09-22 14:53:51 +0200385 total = avg_stats(&runtime_nsecs_stats);
386
387 if (total)
388 ratio = 1000.0 * avg / total;
389
390 fprintf(stderr, " # %10.3f M/sec", ratio);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200391 }
Ingo Molnar42202dd2009-06-13 14:57:28 +0200392}
393
394/*
395 * Print out the results of a single counter:
396 */
397static void print_counter(int counter)
398{
Peter Zijlstra849abde2009-09-04 18:23:38 +0200399 double avg = avg_stats(&event_res_stats[counter][0]);
Peter Zijlstra63d40de2009-09-04 17:03:13 +0200400 int scaled = event_scaled[counter];
Ingo Molnar42202dd2009-06-13 14:57:28 +0200401
Ingo Molnar42202dd2009-06-13 14:57:28 +0200402 if (scaled == -1) {
Jaswinder Singh Rajput6e750a8f2009-06-27 03:02:07 +0530403 fprintf(stderr, " %14s %-24s\n",
Ingo Molnar42202dd2009-06-13 14:57:28 +0200404 "<not counted>", event_name(counter));
405 return;
406 }
407
408 if (nsec_counter(counter))
Peter Zijlstra849abde2009-09-04 18:23:38 +0200409 nsec_printout(counter, avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200410 else
Peter Zijlstra849abde2009-09-04 18:23:38 +0200411 abs_printout(counter, avg);
412
413 print_noise(counter, avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200414
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200415 if (scaled) {
416 double avg_enabled, avg_running;
417
418 avg_enabled = avg_stats(&event_res_stats[counter][1]);
419 avg_running = avg_stats(&event_res_stats[counter][2]);
420
Ingo Molnar210ad392009-06-29 21:50:54 +0200421 fprintf(stderr, " (scaled from %.2f%%)",
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200422 100 * avg_running / avg_enabled);
423 }
Ingo Molnar42202dd2009-06-13 14:57:28 +0200424
425 fprintf(stderr, "\n");
426}
427
Ingo Molnar42202dd2009-06-13 14:57:28 +0200428static void print_stat(int argc, const char **argv)
429{
430 int i, counter;
431
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200432 fflush(stdout);
433
434 fprintf(stderr, "\n");
Liming Wang60666c62009-12-31 16:05:50 +0800435 fprintf(stderr, " Performance counter stats for ");
436 if(target_pid == -1) {
437 fprintf(stderr, "\'%s", argv[0]);
438 for (i = 1; i < argc; i++)
439 fprintf(stderr, " %s", argv[i]);
440 }else
441 fprintf(stderr, "task pid \'%d", target_pid);
Ingo Molnar44db76c2009-06-03 19:36:07 +0200442
Ingo Molnar42202dd2009-06-13 14:57:28 +0200443 fprintf(stderr, "\'");
444 if (run_count > 1)
445 fprintf(stderr, " (%d runs)", run_count);
446 fprintf(stderr, ":\n\n");
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200447
448 for (counter = 0; counter < nr_counters; counter++)
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200449 print_counter(counter);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200450
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200451 fprintf(stderr, "\n");
Ingo Molnar566747e2009-06-27 06:24:32 +0200452 fprintf(stderr, " %14.9f seconds time elapsed",
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200453 avg_stats(&walltime_nsecs_stats)/1e9);
Ingo Molnar566747e2009-06-27 06:24:32 +0200454 if (run_count > 1) {
455 fprintf(stderr, " ( +- %7.3f%% )",
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200456 100*stddev_stats(&walltime_nsecs_stats) /
457 avg_stats(&walltime_nsecs_stats));
Ingo Molnar566747e2009-06-27 06:24:32 +0200458 }
459 fprintf(stderr, "\n\n");
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200460}
461
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200462static volatile int signr = -1;
463
Ingo Molnar52425192009-05-26 09:17:18 +0200464static void skip_signal(int signo)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200465{
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300466 if(child_pid == -1)
Liming Wang60666c62009-12-31 16:05:50 +0800467 done = 1;
468
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200469 signr = signo;
470}
471
472static void sig_atexit(void)
473{
Chris Wilson933da832009-10-04 01:35:01 +0100474 if (child_pid != -1)
475 kill(child_pid, SIGTERM);
476
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200477 if (signr == -1)
478 return;
479
480 signal(signr, SIG_DFL);
481 kill(getpid(), signr);
Ingo Molnar52425192009-05-26 09:17:18 +0200482}
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200483
Ingo Molnar52425192009-05-26 09:17:18 +0200484static const char * const stat_usage[] = {
Liming Wang60666c62009-12-31 16:05:50 +0800485 "perf stat [<options>] [<command>]",
Ingo Molnar52425192009-05-26 09:17:18 +0200486 NULL
487};
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200488
Ingo Molnar52425192009-05-26 09:17:18 +0200489static const struct option options[] = {
490 OPT_CALLBACK('e', "event", NULL, "event",
Thomas Gleixner86847b62009-06-06 12:24:17 +0200491 "event selector. use 'perf list' to list available events",
492 parse_events),
Ingo Molnar52425192009-05-26 09:17:18 +0200493 OPT_BOOLEAN('i', "inherit", &inherit,
494 "child tasks inherit counters"),
495 OPT_INTEGER('p', "pid", &target_pid,
496 "stat events on existing pid"),
497 OPT_BOOLEAN('a', "all-cpus", &system_wide,
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530498 "system-wide collection from all CPUs"),
Brice Goglinb26bc5a2009-08-07 10:18:39 +0200499 OPT_BOOLEAN('c', "scale", &scale,
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530500 "scale/normalize counters"),
Ingo Molnar743ee1f2009-06-07 17:06:46 +0200501 OPT_BOOLEAN('v', "verbose", &verbose,
502 "be more verbose (show counter open errors, etc)"),
Ingo Molnar42202dd2009-06-13 14:57:28 +0200503 OPT_INTEGER('r', "repeat", &run_count,
504 "repeat command and print average + stddev (max: 100)"),
Ingo Molnar0cfb7a12009-06-27 06:10:30 +0200505 OPT_BOOLEAN('n', "null", &null_run,
506 "null run - dont start any counters"),
Ingo Molnar52425192009-05-26 09:17:18 +0200507 OPT_END()
508};
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200509
Ingo Molnarf37a2912009-07-01 12:37:06 +0200510int cmd_stat(int argc, const char **argv, const char *prefix __used)
Ingo Molnar52425192009-05-26 09:17:18 +0200511{
Ingo Molnar42202dd2009-06-13 14:57:28 +0200512 int status;
513
Anton Blancharda0541232009-07-22 23:04:12 +1000514 argc = parse_options(argc, argv, options, stat_usage,
515 PARSE_OPT_STOP_AT_NON_OPTION);
Liming Wang60666c62009-12-31 16:05:50 +0800516 if (!argc && target_pid == -1)
Ingo Molnar52425192009-05-26 09:17:18 +0200517 usage_with_options(stat_usage, options);
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200518 if (run_count <= 0)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200519 usage_with_options(stat_usage, options);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200520
Jaswinder Singh Rajputc3043562009-06-27 23:49:09 +0530521 /* Set attrs and nr_counters if no event is selected and !null_run */
522 if (!null_run && !nr_counters) {
523 memcpy(attrs, default_attrs, sizeof(default_attrs));
524 nr_counters = ARRAY_SIZE(default_attrs);
525 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200526
Paul Mackerrasa12b51c2010-03-10 20:36:09 +1100527 if (system_wide)
528 nr_cpus = read_cpu_map();
529 else
530 nr_cpus = 1;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200531
Ingo Molnar58d7e992009-05-15 11:03:23 +0200532 /*
533 * We dont want to block the signals - that would cause
534 * child tasks to inherit that and Ctrl-C would not work.
535 * What we want is for Ctrl-C to work in the exec()-ed
536 * task, but being ignored by perf stat itself:
537 */
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200538 atexit(sig_atexit);
Ingo Molnar58d7e992009-05-15 11:03:23 +0200539 signal(SIGINT, skip_signal);
540 signal(SIGALRM, skip_signal);
541 signal(SIGABRT, skip_signal);
542
Ingo Molnar42202dd2009-06-13 14:57:28 +0200543 status = 0;
544 for (run_idx = 0; run_idx < run_count; run_idx++) {
545 if (run_count != 1 && verbose)
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530546 fprintf(stderr, "[ perf stat: executing run #%d ... ]\n", run_idx + 1);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200547 status = run_perf_stat(argc, argv);
548 }
549
550 print_stat(argc, argv);
551
552 return status;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200553}