blob: 95db31cff6fdbb63e714dc0725998d7ef76103ea [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;
162 attr->disabled = 1;
163 attr->enable_on_exec = 1;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200164
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200165 fd[0][counter] = sys_perf_event_open(attr, pid, -1, -1, 0);
Jaswinder Singh Rajputcca03c0a2009-06-23 17:12:49 +0530166 if (fd[0][counter] < 0 && verbose)
167 fprintf(stderr, ERR_PERF_OPEN, counter,
168 fd[0][counter], strerror(errno));
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200169 }
170}
171
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200172/*
173 * Does the counter have nsecs as a unit?
174 */
175static inline int nsec_counter(int counter)
176{
Jaswinder Singh Rajputb9ebdcc2009-07-01 15:05:09 +0530177 if (MATCH_EVENT(SOFTWARE, SW_CPU_CLOCK, counter) ||
178 MATCH_EVENT(SOFTWARE, SW_TASK_CLOCK, counter))
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200179 return 1;
180
181 return 0;
182}
183
184/*
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200185 * Read out the results of a single counter:
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200186 */
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200187static void read_counter(int counter)
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200188{
Peter Zijlstra849abde2009-09-04 18:23:38 +0200189 u64 count[3], single_count[3];
Ingo Molnarf37a2912009-07-01 12:37:06 +0200190 unsigned int cpu;
191 size_t res, nv;
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200192 int scaled;
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200193 int i;
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200194
195 count[0] = count[1] = count[2] = 0;
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200196
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200197 nv = scale ? 3 : 1;
Jaswinder Singh Rajputcca03c0a2009-06-23 17:12:49 +0530198 for (cpu = 0; cpu < nr_cpus; cpu++) {
Ingo Molnar743ee1f2009-06-07 17:06:46 +0200199 if (fd[cpu][counter] < 0)
200 continue;
201
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000202 res = read(fd[cpu][counter], single_count, nv * sizeof(u64));
203 assert(res == nv * sizeof(u64));
Ingo Molnarf37a2912009-07-01 12:37:06 +0200204
Ingo Molnar42202dd2009-06-13 14:57:28 +0200205 close(fd[cpu][counter]);
206 fd[cpu][counter] = -1;
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200207
208 count[0] += single_count[0];
209 if (scale) {
210 count[1] += single_count[1];
211 count[2] += single_count[2];
212 }
213 }
214
215 scaled = 0;
216 if (scale) {
217 if (count[2] == 0) {
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200218 event_scaled[counter] = -1;
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200219 count[0] = 0;
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200220 return;
221 }
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200222
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200223 if (count[2] < count[1]) {
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200224 event_scaled[counter] = 1;
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200225 count[0] = (unsigned long long)
226 ((double)count[0] * count[1] / count[2] + 0.5);
227 }
228 }
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200229
230 for (i = 0; i < 3; i++)
231 update_stats(&event_res_stats[counter][i], count[i]);
232
233 if (verbose) {
234 fprintf(stderr, "%s: %Ld %Ld %Ld\n", event_name(counter),
235 count[0], count[1], count[2]);
236 }
237
Ingo Molnarbe1ac0d2009-05-29 09:10:54 +0200238 /*
239 * Save the full runtime - to allow normalization during printout:
240 */
Jaswinder Singh Rajputb9ebdcc2009-07-01 15:05:09 +0530241 if (MATCH_EVENT(SOFTWARE, SW_TASK_CLOCK, counter))
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200242 update_stats(&runtime_nsecs_stats, count[0]);
Jaswinder Singh Rajputb9ebdcc2009-07-01 15:05:09 +0530243 if (MATCH_EVENT(HARDWARE, HW_CPU_CYCLES, counter))
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200244 update_stats(&runtime_cycles_stats, count[0]);
Anton Blanchard11018202009-10-18 22:29:23 +1100245 if (MATCH_EVENT(HARDWARE, HW_BRANCH_INSTRUCTIONS, counter))
246 update_stats(&runtime_branches_stats, count[0]);
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200247}
248
Ingo Molnarf37a2912009-07-01 12:37:06 +0200249static int run_perf_stat(int argc __used, const char **argv)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200250{
251 unsigned long long t0, t1;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200252 int status = 0;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200253 int counter;
Liming Wang60666c62009-12-31 16:05:50 +0800254 int pid = target_pid;
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000255 int child_ready_pipe[2], go_pipe[2];
Liming Wang60666c62009-12-31 16:05:50 +0800256 const bool forks = (target_pid == -1 && argc > 0);
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000257 char buf;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200258
259 if (!system_wide)
260 nr_cpus = 1;
261
Liming Wang60666c62009-12-31 16:05:50 +0800262 if (forks && (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0)) {
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000263 perror("failed to create pipes");
264 exit(1);
265 }
266
Liming Wang60666c62009-12-31 16:05:50 +0800267 if (forks) {
268 if ((pid = fork()) < 0)
269 perror("failed to fork");
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000270
Liming Wang60666c62009-12-31 16:05:50 +0800271 if (!pid) {
272 close(child_ready_pipe[0]);
273 close(go_pipe[1]);
274 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
275
276 /*
277 * Do a dummy execvp to get the PLT entry resolved,
278 * so we avoid the resolver overhead on the real
279 * execvp call.
280 */
281 execvp("", (char **)argv);
282
283 /*
284 * Tell the parent we're ready to go
285 */
286 close(child_ready_pipe[1]);
287
288 /*
289 * Wait until the parent tells us to go.
290 */
291 if (read(go_pipe[0], &buf, 1) == -1)
292 perror("unable to read pipe");
293
294 execvp(argv[0], (char **)argv);
295
296 perror(argv[0]);
297 exit(-1);
298 }
299
300 child_pid = pid;
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000301
302 /*
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
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200312 for (counter = 0; counter < nr_counters; counter++)
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000313 create_perf_stat_counter(counter, pid);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200314
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200315 /*
316 * Enable counters and exec the command:
317 */
318 t0 = rdclock();
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200319
Liming Wang60666c62009-12-31 16:05:50 +0800320 if (forks) {
321 close(go_pipe[1]);
322 wait(&status);
323 } else {
324 while(!done);
325 }
Ingo Molnar44db76c2009-06-03 19:36:07 +0200326
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200327 t1 = rdclock();
328
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200329 update_stats(&walltime_nsecs_stats, t1 - t0);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200330
331 for (counter = 0; counter < nr_counters; counter++)
332 read_counter(counter);
333
334 return WEXITSTATUS(status);
335}
336
Peter Zijlstra849abde2009-09-04 18:23:38 +0200337static void print_noise(int counter, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200338{
Peter Zijlstra849abde2009-09-04 18:23:38 +0200339 if (run_count == 1)
340 return;
341
342 fprintf(stderr, " ( +- %7.3f%% )",
343 100 * stddev_stats(&event_res_stats[counter][0]) / avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200344}
345
Peter Zijlstra849abde2009-09-04 18:23:38 +0200346static void nsec_printout(int counter, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200347{
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200348 double msecs = avg / 1e6;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200349
Jaswinder Singh Rajput6e750a8f2009-06-27 03:02:07 +0530350 fprintf(stderr, " %14.6f %-24s", msecs, event_name(counter));
Ingo Molnar42202dd2009-06-13 14:57:28 +0200351
Jaswinder Singh Rajputb9ebdcc2009-07-01 15:05:09 +0530352 if (MATCH_EVENT(SOFTWARE, SW_TASK_CLOCK, counter)) {
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200353 fprintf(stderr, " # %10.3f CPUs ",
354 avg / avg_stats(&walltime_nsecs_stats));
Ingo Molnar42202dd2009-06-13 14:57:28 +0200355 }
Ingo Molnar42202dd2009-06-13 14:57:28 +0200356}
357
Peter Zijlstra849abde2009-09-04 18:23:38 +0200358static void abs_printout(int counter, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200359{
Ingo Molnarc7f7fea2009-09-22 14:53:51 +0200360 double total, ratio = 0.0;
361
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200362 fprintf(stderr, " %14.0f %-24s", avg, event_name(counter));
Ingo Molnar42202dd2009-06-13 14:57:28 +0200363
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200364 if (MATCH_EVENT(HARDWARE, HW_INSTRUCTIONS, counter)) {
Ingo Molnarc7f7fea2009-09-22 14:53:51 +0200365 total = avg_stats(&runtime_cycles_stats);
366
367 if (total)
368 ratio = avg / total;
369
370 fprintf(stderr, " # %10.3f IPC ", ratio);
Lucas De Marchi7255fe22009-11-15 12:05:08 -0200371 } else if (MATCH_EVENT(HARDWARE, HW_BRANCH_MISSES, counter) &&
372 runtime_branches_stats.n != 0) {
Anton Blanchard11018202009-10-18 22:29:23 +1100373 total = avg_stats(&runtime_branches_stats);
374
375 if (total)
376 ratio = avg * 100 / total;
377
Ingo Molnardd86e722009-10-19 13:33:03 +0200378 fprintf(stderr, " # %10.3f %% ", ratio);
Anton Blanchard11018202009-10-18 22:29:23 +1100379
Lucas De Marchi7255fe22009-11-15 12:05:08 -0200380 } else if (runtime_nsecs_stats.n != 0) {
Ingo Molnarc7f7fea2009-09-22 14:53:51 +0200381 total = avg_stats(&runtime_nsecs_stats);
382
383 if (total)
384 ratio = 1000.0 * avg / total;
385
386 fprintf(stderr, " # %10.3f M/sec", ratio);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200387 }
Ingo Molnar42202dd2009-06-13 14:57:28 +0200388}
389
390/*
391 * Print out the results of a single counter:
392 */
393static void print_counter(int counter)
394{
Peter Zijlstra849abde2009-09-04 18:23:38 +0200395 double avg = avg_stats(&event_res_stats[counter][0]);
Peter Zijlstra63d40de2009-09-04 17:03:13 +0200396 int scaled = event_scaled[counter];
Ingo Molnar42202dd2009-06-13 14:57:28 +0200397
Ingo Molnar42202dd2009-06-13 14:57:28 +0200398 if (scaled == -1) {
Jaswinder Singh Rajput6e750a8f2009-06-27 03:02:07 +0530399 fprintf(stderr, " %14s %-24s\n",
Ingo Molnar42202dd2009-06-13 14:57:28 +0200400 "<not counted>", event_name(counter));
401 return;
402 }
403
404 if (nsec_counter(counter))
Peter Zijlstra849abde2009-09-04 18:23:38 +0200405 nsec_printout(counter, avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200406 else
Peter Zijlstra849abde2009-09-04 18:23:38 +0200407 abs_printout(counter, avg);
408
409 print_noise(counter, avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200410
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200411 if (scaled) {
412 double avg_enabled, avg_running;
413
414 avg_enabled = avg_stats(&event_res_stats[counter][1]);
415 avg_running = avg_stats(&event_res_stats[counter][2]);
416
Ingo Molnar210ad392009-06-29 21:50:54 +0200417 fprintf(stderr, " (scaled from %.2f%%)",
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200418 100 * avg_running / avg_enabled);
419 }
Ingo Molnar42202dd2009-06-13 14:57:28 +0200420
421 fprintf(stderr, "\n");
422}
423
Ingo Molnar42202dd2009-06-13 14:57:28 +0200424static void print_stat(int argc, const char **argv)
425{
426 int i, counter;
427
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200428 fflush(stdout);
429
430 fprintf(stderr, "\n");
Liming Wang60666c62009-12-31 16:05:50 +0800431 fprintf(stderr, " Performance counter stats for ");
432 if(target_pid == -1) {
433 fprintf(stderr, "\'%s", argv[0]);
434 for (i = 1; i < argc; i++)
435 fprintf(stderr, " %s", argv[i]);
436 }else
437 fprintf(stderr, "task pid \'%d", target_pid);
Ingo Molnar44db76c2009-06-03 19:36:07 +0200438
Ingo Molnar42202dd2009-06-13 14:57:28 +0200439 fprintf(stderr, "\'");
440 if (run_count > 1)
441 fprintf(stderr, " (%d runs)", run_count);
442 fprintf(stderr, ":\n\n");
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200443
444 for (counter = 0; counter < nr_counters; counter++)
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200445 print_counter(counter);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200446
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200447 fprintf(stderr, "\n");
Ingo Molnar566747e2009-06-27 06:24:32 +0200448 fprintf(stderr, " %14.9f seconds time elapsed",
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200449 avg_stats(&walltime_nsecs_stats)/1e9);
Ingo Molnar566747e2009-06-27 06:24:32 +0200450 if (run_count > 1) {
451 fprintf(stderr, " ( +- %7.3f%% )",
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200452 100*stddev_stats(&walltime_nsecs_stats) /
453 avg_stats(&walltime_nsecs_stats));
Ingo Molnar566747e2009-06-27 06:24:32 +0200454 }
455 fprintf(stderr, "\n\n");
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200456}
457
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200458static volatile int signr = -1;
459
Ingo Molnar52425192009-05-26 09:17:18 +0200460static void skip_signal(int signo)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200461{
Liming Wang60666c62009-12-31 16:05:50 +0800462 if(target_pid != -1)
463 done = 1;
464
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200465 signr = signo;
466}
467
468static void sig_atexit(void)
469{
Chris Wilson933da832009-10-04 01:35:01 +0100470 if (child_pid != -1)
471 kill(child_pid, SIGTERM);
472
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200473 if (signr == -1)
474 return;
475
476 signal(signr, SIG_DFL);
477 kill(getpid(), signr);
Ingo Molnar52425192009-05-26 09:17:18 +0200478}
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200479
Ingo Molnar52425192009-05-26 09:17:18 +0200480static const char * const stat_usage[] = {
Liming Wang60666c62009-12-31 16:05:50 +0800481 "perf stat [<options>] [<command>]",
Ingo Molnar52425192009-05-26 09:17:18 +0200482 NULL
483};
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200484
Ingo Molnar52425192009-05-26 09:17:18 +0200485static const struct option options[] = {
486 OPT_CALLBACK('e', "event", NULL, "event",
Thomas Gleixner86847b62009-06-06 12:24:17 +0200487 "event selector. use 'perf list' to list available events",
488 parse_events),
Ingo Molnar52425192009-05-26 09:17:18 +0200489 OPT_BOOLEAN('i', "inherit", &inherit,
490 "child tasks inherit counters"),
491 OPT_INTEGER('p', "pid", &target_pid,
492 "stat events on existing pid"),
493 OPT_BOOLEAN('a', "all-cpus", &system_wide,
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530494 "system-wide collection from all CPUs"),
Brice Goglinb26bc5a2009-08-07 10:18:39 +0200495 OPT_BOOLEAN('c', "scale", &scale,
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530496 "scale/normalize counters"),
Ingo Molnar743ee1f2009-06-07 17:06:46 +0200497 OPT_BOOLEAN('v', "verbose", &verbose,
498 "be more verbose (show counter open errors, etc)"),
Ingo Molnar42202dd2009-06-13 14:57:28 +0200499 OPT_INTEGER('r', "repeat", &run_count,
500 "repeat command and print average + stddev (max: 100)"),
Ingo Molnar0cfb7a12009-06-27 06:10:30 +0200501 OPT_BOOLEAN('n', "null", &null_run,
502 "null run - dont start any counters"),
Ingo Molnar52425192009-05-26 09:17:18 +0200503 OPT_END()
504};
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200505
Ingo Molnarf37a2912009-07-01 12:37:06 +0200506int cmd_stat(int argc, const char **argv, const char *prefix __used)
Ingo Molnar52425192009-05-26 09:17:18 +0200507{
Ingo Molnar42202dd2009-06-13 14:57:28 +0200508 int status;
509
Anton Blancharda0541232009-07-22 23:04:12 +1000510 argc = parse_options(argc, argv, options, stat_usage,
511 PARSE_OPT_STOP_AT_NON_OPTION);
Liming Wang60666c62009-12-31 16:05:50 +0800512 if (!argc && target_pid == -1)
Ingo Molnar52425192009-05-26 09:17:18 +0200513 usage_with_options(stat_usage, options);
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200514 if (run_count <= 0)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200515 usage_with_options(stat_usage, options);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200516
Jaswinder Singh Rajputc3043562009-06-27 23:49:09 +0530517 /* Set attrs and nr_counters if no event is selected and !null_run */
518 if (!null_run && !nr_counters) {
519 memcpy(attrs, default_attrs, sizeof(default_attrs));
520 nr_counters = ARRAY_SIZE(default_attrs);
521 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200522
Paul Mackerrasa12b51c2010-03-10 20:36:09 +1100523 if (system_wide)
524 nr_cpus = read_cpu_map();
525 else
526 nr_cpus = 1;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200527
Ingo Molnar58d7e992009-05-15 11:03:23 +0200528 /*
529 * We dont want to block the signals - that would cause
530 * child tasks to inherit that and Ctrl-C would not work.
531 * What we want is for Ctrl-C to work in the exec()-ed
532 * task, but being ignored by perf stat itself:
533 */
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200534 atexit(sig_atexit);
Ingo Molnar58d7e992009-05-15 11:03:23 +0200535 signal(SIGINT, skip_signal);
536 signal(SIGALRM, skip_signal);
537 signal(SIGABRT, skip_signal);
538
Ingo Molnar42202dd2009-06-13 14:57:28 +0200539 status = 0;
540 for (run_idx = 0; run_idx < run_count; run_idx++) {
541 if (run_count != 1 && verbose)
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530542 fprintf(stderr, "[ perf stat: executing run #%d ... ]\n", run_idx + 1);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200543 status = run_perf_stat(argc, argv);
544 }
545
546 print_stat(argc, argv);
547
548 return status;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200549}