blob: e8c85d5aec4151fc965f2b16063c60da456d6e67 [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"
Ingo Molnarddcacfa2009-04-20 15:37:32 +020048
Ingo Molnarddcacfa2009-04-20 15:37:32 +020049#include <sys/prctl.h>
Ingo Molnar42202dd2009-06-13 14:57:28 +020050#include <math.h>
Peter Zijlstra16c8a102009-05-05 17:50:27 +020051
Ingo Molnarcdd6c482009-09-21 12:02:48 +020052static struct perf_event_attr default_attrs[] = {
Ingo Molnara21ca2c2009-06-06 09:58:57 +020053
Ingo Molnar56aab462009-10-19 13:27:08 +020054 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
55 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
56 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
57 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
Ingo Molnara21ca2c2009-06-06 09:58:57 +020058
Ingo Molnar56aab462009-10-19 13:27:08 +020059 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
60 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
Ingo Molnar56aab462009-10-19 13:27:08 +020061 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
62 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
Ingo Molnardd86e722009-10-19 13:33:03 +020063 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CACHE_REFERENCES },
64 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CACHE_MISSES },
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +020065
Ingo Molnara21ca2c2009-06-06 09:58:57 +020066};
67
Ingo Molnarddcacfa2009-04-20 15:37:32 +020068static int system_wide = 0;
Ingo Molnarf37a2912009-07-01 12:37:06 +020069static unsigned int nr_cpus = 0;
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +053070static int run_idx = 0;
71
72static int run_count = 1;
73static int inherit = 1;
74static int scale = 1;
Chris Wilson933da832009-10-04 01:35:01 +010075static pid_t target_pid = -1;
76static pid_t child_pid = -1;
Ingo Molnar0cfb7a12009-06-27 06:10:30 +020077static int null_run = 0;
Ingo Molnarddcacfa2009-04-20 15:37:32 +020078
Ingo Molnarddcacfa2009-04-20 15:37:32 +020079static int fd[MAX_NR_CPUS][MAX_COUNTERS];
80
Peter Zijlstra63d40de2009-09-04 17:03:13 +020081static int event_scaled[MAX_COUNTERS];
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +053082
Liming Wang60666c62009-12-31 16:05:50 +080083static volatile int done = 0;
84
Peter Zijlstra506d4bc2009-09-04 15:36:12 +020085struct stats
86{
Peter Zijlstra8a026312009-09-04 17:26:26 +020087 double n, mean, M2;
Peter Zijlstra506d4bc2009-09-04 15:36:12 +020088};
Ingo Molnar42202dd2009-06-13 14:57:28 +020089
Peter Zijlstra9e9772c2009-09-04 15:36:08 +020090static void update_stats(struct stats *stats, u64 val)
91{
Peter Zijlstra8a026312009-09-04 17:26:26 +020092 double delta;
Peter Zijlstra9e9772c2009-09-04 15:36:08 +020093
Peter Zijlstra8a026312009-09-04 17:26:26 +020094 stats->n++;
95 delta = val - stats->mean;
96 stats->mean += delta / stats->n;
97 stats->M2 += delta*(val - stats->mean);
Peter Zijlstra9e9772c2009-09-04 15:36:08 +020098}
99
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200100static double avg_stats(struct stats *stats)
101{
Peter Zijlstra8a026312009-09-04 17:26:26 +0200102 return stats->mean;
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200103}
Ingo Molnar42202dd2009-06-13 14:57:28 +0200104
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200105/*
Peter Zijlstra63d40de2009-09-04 17:03:13 +0200106 * http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
107 *
Peter Zijlstra8a026312009-09-04 17:26:26 +0200108 * (\Sum n_i^2) - ((\Sum n_i)^2)/n
109 * s^2 = -------------------------------
110 * n - 1
Peter Zijlstra63d40de2009-09-04 17:03:13 +0200111 *
112 * http://en.wikipedia.org/wiki/Stddev
113 *
114 * The std dev of the mean is related to the std dev by:
115 *
116 * s
117 * s_mean = -------
118 * sqrt(n)
119 *
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200120 */
121static double stddev_stats(struct stats *stats)
122{
Peter Zijlstra8a026312009-09-04 17:26:26 +0200123 double variance = stats->M2 / (stats->n - 1);
124 double variance_mean = variance / stats->n;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200125
Peter Zijlstra63d40de2009-09-04 17:03:13 +0200126 return sqrt(variance_mean);
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200127}
Ingo Molnar42202dd2009-06-13 14:57:28 +0200128
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200129struct stats event_res_stats[MAX_COUNTERS][3];
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200130struct stats runtime_nsecs_stats;
131struct stats walltime_nsecs_stats;
132struct stats runtime_cycles_stats;
Anton Blanchard11018202009-10-18 22:29:23 +1100133struct stats runtime_branches_stats;
Ingo Molnarbe1ac0d2009-05-29 09:10:54 +0200134
Jaswinder Singh Rajputb9ebdcc2009-07-01 15:05:09 +0530135#define MATCH_EVENT(t, c, counter) \
136 (attrs[counter].type == PERF_TYPE_##t && \
137 attrs[counter].config == PERF_COUNT_##c)
138
Jaswinder Singh Rajputcca03c0a2009-06-23 17:12:49 +0530139#define ERR_PERF_OPEN \
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200140"Error: counter %d, sys_perf_event_open() syscall returned with %d (%s)\n"
Jaswinder Singh Rajputcca03c0a2009-06-23 17:12:49 +0530141
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000142static void create_perf_stat_counter(int counter, int pid)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200143{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200144 struct perf_event_attr *attr = attrs + counter;
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200145
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200146 if (scale)
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200147 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
148 PERF_FORMAT_TOTAL_TIME_RUNNING;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200149
150 if (system_wide) {
Ingo Molnarf37a2912009-07-01 12:37:06 +0200151 unsigned int cpu;
152
Jaswinder Singh Rajputcca03c0a2009-06-23 17:12:49 +0530153 for (cpu = 0; cpu < nr_cpus; cpu++) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200154 fd[cpu][counter] = sys_perf_event_open(attr, -1, cpu, -1, 0);
Jaswinder Singh Rajputcca03c0a2009-06-23 17:12:49 +0530155 if (fd[cpu][counter] < 0 && verbose)
156 fprintf(stderr, ERR_PERF_OPEN, counter,
157 fd[cpu][counter], strerror(errno));
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200158 }
159 } else {
Paul Mackerras57e79862009-06-30 16:07:19 +1000160 attr->inherit = inherit;
161 attr->disabled = 1;
162 attr->enable_on_exec = 1;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200163
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200164 fd[0][counter] = sys_perf_event_open(attr, pid, -1, -1, 0);
Jaswinder Singh Rajputcca03c0a2009-06-23 17:12:49 +0530165 if (fd[0][counter] < 0 && verbose)
166 fprintf(stderr, ERR_PERF_OPEN, counter,
167 fd[0][counter], strerror(errno));
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200168 }
169}
170
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200171/*
172 * Does the counter have nsecs as a unit?
173 */
174static inline int nsec_counter(int counter)
175{
Jaswinder Singh Rajputb9ebdcc2009-07-01 15:05:09 +0530176 if (MATCH_EVENT(SOFTWARE, SW_CPU_CLOCK, counter) ||
177 MATCH_EVENT(SOFTWARE, SW_TASK_CLOCK, counter))
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200178 return 1;
179
180 return 0;
181}
182
183/*
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200184 * Read out the results of a single counter:
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200185 */
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200186static void read_counter(int counter)
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200187{
Peter Zijlstra849abde2009-09-04 18:23:38 +0200188 u64 count[3], single_count[3];
Ingo Molnarf37a2912009-07-01 12:37:06 +0200189 unsigned int cpu;
190 size_t res, nv;
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200191 int scaled;
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200192 int i;
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200193
194 count[0] = count[1] = count[2] = 0;
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200195
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200196 nv = scale ? 3 : 1;
Jaswinder Singh Rajputcca03c0a2009-06-23 17:12:49 +0530197 for (cpu = 0; cpu < nr_cpus; cpu++) {
Ingo Molnar743ee1f2009-06-07 17:06:46 +0200198 if (fd[cpu][counter] < 0)
199 continue;
200
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000201 res = read(fd[cpu][counter], single_count, nv * sizeof(u64));
202 assert(res == nv * sizeof(u64));
Ingo Molnarf37a2912009-07-01 12:37:06 +0200203
Ingo Molnar42202dd2009-06-13 14:57:28 +0200204 close(fd[cpu][counter]);
205 fd[cpu][counter] = -1;
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200206
207 count[0] += single_count[0];
208 if (scale) {
209 count[1] += single_count[1];
210 count[2] += single_count[2];
211 }
212 }
213
214 scaled = 0;
215 if (scale) {
216 if (count[2] == 0) {
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200217 event_scaled[counter] = -1;
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200218 count[0] = 0;
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200219 return;
220 }
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200221
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200222 if (count[2] < count[1]) {
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200223 event_scaled[counter] = 1;
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200224 count[0] = (unsigned long long)
225 ((double)count[0] * count[1] / count[2] + 0.5);
226 }
227 }
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200228
229 for (i = 0; i < 3; i++)
230 update_stats(&event_res_stats[counter][i], count[i]);
231
232 if (verbose) {
233 fprintf(stderr, "%s: %Ld %Ld %Ld\n", event_name(counter),
234 count[0], count[1], count[2]);
235 }
236
Ingo Molnarbe1ac0d2009-05-29 09:10:54 +0200237 /*
238 * Save the full runtime - to allow normalization during printout:
239 */
Jaswinder Singh Rajputb9ebdcc2009-07-01 15:05:09 +0530240 if (MATCH_EVENT(SOFTWARE, SW_TASK_CLOCK, counter))
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200241 update_stats(&runtime_nsecs_stats, count[0]);
Jaswinder Singh Rajputb9ebdcc2009-07-01 15:05:09 +0530242 if (MATCH_EVENT(HARDWARE, HW_CPU_CYCLES, counter))
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200243 update_stats(&runtime_cycles_stats, count[0]);
Anton Blanchard11018202009-10-18 22:29:23 +1100244 if (MATCH_EVENT(HARDWARE, HW_BRANCH_INSTRUCTIONS, counter))
245 update_stats(&runtime_branches_stats, count[0]);
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200246}
247
Ingo Molnarf37a2912009-07-01 12:37:06 +0200248static int run_perf_stat(int argc __used, const char **argv)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200249{
250 unsigned long long t0, t1;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200251 int status = 0;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200252 int counter;
Liming Wang60666c62009-12-31 16:05:50 +0800253 int pid = target_pid;
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000254 int child_ready_pipe[2], go_pipe[2];
Liming Wang60666c62009-12-31 16:05:50 +0800255 const bool forks = (target_pid == -1 && argc > 0);
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000256 char buf;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200257
258 if (!system_wide)
259 nr_cpus = 1;
260
Liming Wang60666c62009-12-31 16:05:50 +0800261 if (forks && (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0)) {
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000262 perror("failed to create pipes");
263 exit(1);
264 }
265
Liming Wang60666c62009-12-31 16:05:50 +0800266 if (forks) {
267 if ((pid = fork()) < 0)
268 perror("failed to fork");
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000269
Liming Wang60666c62009-12-31 16:05:50 +0800270 if (!pid) {
271 close(child_ready_pipe[0]);
272 close(go_pipe[1]);
273 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
274
275 /*
276 * Do a dummy execvp to get the PLT entry resolved,
277 * so we avoid the resolver overhead on the real
278 * execvp call.
279 */
280 execvp("", (char **)argv);
281
282 /*
283 * Tell the parent we're ready to go
284 */
285 close(child_ready_pipe[1]);
286
287 /*
288 * Wait until the parent tells us to go.
289 */
290 if (read(go_pipe[0], &buf, 1) == -1)
291 perror("unable to read pipe");
292
293 execvp(argv[0], (char **)argv);
294
295 perror(argv[0]);
296 exit(-1);
297 }
298
299 child_pid = pid;
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000300
301 /*
Liming Wang60666c62009-12-31 16:05:50 +0800302 * Wait for the child to be ready to exec.
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000303 */
304 close(child_ready_pipe[1]);
Liming Wang60666c62009-12-31 16:05:50 +0800305 close(go_pipe[0]);
306 if (read(child_ready_pipe[0], &buf, 1) == -1)
Frederic Weisbeckera92bef02009-07-01 21:02:10 +0200307 perror("unable to read pipe");
Liming Wang60666c62009-12-31 16:05:50 +0800308 close(child_ready_pipe[0]);
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000309 }
310
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200311 for (counter = 0; counter < nr_counters; counter++)
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000312 create_perf_stat_counter(counter, pid);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200313
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200314 /*
315 * Enable counters and exec the command:
316 */
317 t0 = rdclock();
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200318
Liming Wang60666c62009-12-31 16:05:50 +0800319 if (forks) {
320 close(go_pipe[1]);
321 wait(&status);
322 } else {
323 while(!done);
324 }
Ingo Molnar44db76c2009-06-03 19:36:07 +0200325
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200326 t1 = rdclock();
327
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200328 update_stats(&walltime_nsecs_stats, t1 - t0);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200329
330 for (counter = 0; counter < nr_counters; counter++)
331 read_counter(counter);
332
333 return WEXITSTATUS(status);
334}
335
Peter Zijlstra849abde2009-09-04 18:23:38 +0200336static void print_noise(int counter, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200337{
Peter Zijlstra849abde2009-09-04 18:23:38 +0200338 if (run_count == 1)
339 return;
340
341 fprintf(stderr, " ( +- %7.3f%% )",
342 100 * stddev_stats(&event_res_stats[counter][0]) / avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200343}
344
Peter Zijlstra849abde2009-09-04 18:23:38 +0200345static void nsec_printout(int counter, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200346{
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200347 double msecs = avg / 1e6;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200348
Jaswinder Singh Rajput6e750a8f2009-06-27 03:02:07 +0530349 fprintf(stderr, " %14.6f %-24s", msecs, event_name(counter));
Ingo Molnar42202dd2009-06-13 14:57:28 +0200350
Jaswinder Singh Rajputb9ebdcc2009-07-01 15:05:09 +0530351 if (MATCH_EVENT(SOFTWARE, SW_TASK_CLOCK, counter)) {
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200352 fprintf(stderr, " # %10.3f CPUs ",
353 avg / avg_stats(&walltime_nsecs_stats));
Ingo Molnar42202dd2009-06-13 14:57:28 +0200354 }
Ingo Molnar42202dd2009-06-13 14:57:28 +0200355}
356
Peter Zijlstra849abde2009-09-04 18:23:38 +0200357static void abs_printout(int counter, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200358{
Ingo Molnarc7f7fea2009-09-22 14:53:51 +0200359 double total, ratio = 0.0;
360
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200361 fprintf(stderr, " %14.0f %-24s", avg, event_name(counter));
Ingo Molnar42202dd2009-06-13 14:57:28 +0200362
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200363 if (MATCH_EVENT(HARDWARE, HW_INSTRUCTIONS, counter)) {
Ingo Molnarc7f7fea2009-09-22 14:53:51 +0200364 total = avg_stats(&runtime_cycles_stats);
365
366 if (total)
367 ratio = avg / total;
368
369 fprintf(stderr, " # %10.3f IPC ", ratio);
Lucas De Marchi7255fe22009-11-15 12:05:08 -0200370 } else if (MATCH_EVENT(HARDWARE, HW_BRANCH_MISSES, counter) &&
371 runtime_branches_stats.n != 0) {
Anton Blanchard11018202009-10-18 22:29:23 +1100372 total = avg_stats(&runtime_branches_stats);
373
374 if (total)
375 ratio = avg * 100 / total;
376
Ingo Molnardd86e722009-10-19 13:33:03 +0200377 fprintf(stderr, " # %10.3f %% ", ratio);
Anton Blanchard11018202009-10-18 22:29:23 +1100378
Lucas De Marchi7255fe22009-11-15 12:05:08 -0200379 } else if (runtime_nsecs_stats.n != 0) {
Ingo Molnarc7f7fea2009-09-22 14:53:51 +0200380 total = avg_stats(&runtime_nsecs_stats);
381
382 if (total)
383 ratio = 1000.0 * avg / total;
384
385 fprintf(stderr, " # %10.3f M/sec", ratio);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200386 }
Ingo Molnar42202dd2009-06-13 14:57:28 +0200387}
388
389/*
390 * Print out the results of a single counter:
391 */
392static void print_counter(int counter)
393{
Peter Zijlstra849abde2009-09-04 18:23:38 +0200394 double avg = avg_stats(&event_res_stats[counter][0]);
Peter Zijlstra63d40de2009-09-04 17:03:13 +0200395 int scaled = event_scaled[counter];
Ingo Molnar42202dd2009-06-13 14:57:28 +0200396
Ingo Molnar42202dd2009-06-13 14:57:28 +0200397 if (scaled == -1) {
Jaswinder Singh Rajput6e750a8f2009-06-27 03:02:07 +0530398 fprintf(stderr, " %14s %-24s\n",
Ingo Molnar42202dd2009-06-13 14:57:28 +0200399 "<not counted>", event_name(counter));
400 return;
401 }
402
403 if (nsec_counter(counter))
Peter Zijlstra849abde2009-09-04 18:23:38 +0200404 nsec_printout(counter, avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200405 else
Peter Zijlstra849abde2009-09-04 18:23:38 +0200406 abs_printout(counter, avg);
407
408 print_noise(counter, avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200409
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200410 if (scaled) {
411 double avg_enabled, avg_running;
412
413 avg_enabled = avg_stats(&event_res_stats[counter][1]);
414 avg_running = avg_stats(&event_res_stats[counter][2]);
415
Ingo Molnar210ad392009-06-29 21:50:54 +0200416 fprintf(stderr, " (scaled from %.2f%%)",
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200417 100 * avg_running / avg_enabled);
418 }
Ingo Molnar42202dd2009-06-13 14:57:28 +0200419
420 fprintf(stderr, "\n");
421}
422
Ingo Molnar42202dd2009-06-13 14:57:28 +0200423static void print_stat(int argc, const char **argv)
424{
425 int i, counter;
426
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200427 fflush(stdout);
428
429 fprintf(stderr, "\n");
Liming Wang60666c62009-12-31 16:05:50 +0800430 fprintf(stderr, " Performance counter stats for ");
431 if(target_pid == -1) {
432 fprintf(stderr, "\'%s", argv[0]);
433 for (i = 1; i < argc; i++)
434 fprintf(stderr, " %s", argv[i]);
435 }else
436 fprintf(stderr, "task pid \'%d", target_pid);
Ingo Molnar44db76c2009-06-03 19:36:07 +0200437
Ingo Molnar42202dd2009-06-13 14:57:28 +0200438 fprintf(stderr, "\'");
439 if (run_count > 1)
440 fprintf(stderr, " (%d runs)", run_count);
441 fprintf(stderr, ":\n\n");
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200442
443 for (counter = 0; counter < nr_counters; counter++)
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200444 print_counter(counter);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200445
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200446 fprintf(stderr, "\n");
Ingo Molnar566747e2009-06-27 06:24:32 +0200447 fprintf(stderr, " %14.9f seconds time elapsed",
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200448 avg_stats(&walltime_nsecs_stats)/1e9);
Ingo Molnar566747e2009-06-27 06:24:32 +0200449 if (run_count > 1) {
450 fprintf(stderr, " ( +- %7.3f%% )",
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200451 100*stddev_stats(&walltime_nsecs_stats) /
452 avg_stats(&walltime_nsecs_stats));
Ingo Molnar566747e2009-06-27 06:24:32 +0200453 }
454 fprintf(stderr, "\n\n");
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200455}
456
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200457static volatile int signr = -1;
458
Ingo Molnar52425192009-05-26 09:17:18 +0200459static void skip_signal(int signo)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200460{
Liming Wang60666c62009-12-31 16:05:50 +0800461 if(target_pid != -1)
462 done = 1;
463
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200464 signr = signo;
465}
466
467static void sig_atexit(void)
468{
Chris Wilson933da832009-10-04 01:35:01 +0100469 if (child_pid != -1)
470 kill(child_pid, SIGTERM);
471
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200472 if (signr == -1)
473 return;
474
475 signal(signr, SIG_DFL);
476 kill(getpid(), signr);
Ingo Molnar52425192009-05-26 09:17:18 +0200477}
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200478
Ingo Molnar52425192009-05-26 09:17:18 +0200479static const char * const stat_usage[] = {
Liming Wang60666c62009-12-31 16:05:50 +0800480 "perf stat [<options>] [<command>]",
Ingo Molnar52425192009-05-26 09:17:18 +0200481 NULL
482};
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200483
Ingo Molnar52425192009-05-26 09:17:18 +0200484static const struct option options[] = {
485 OPT_CALLBACK('e', "event", NULL, "event",
Thomas Gleixner86847b62009-06-06 12:24:17 +0200486 "event selector. use 'perf list' to list available events",
487 parse_events),
Ingo Molnar52425192009-05-26 09:17:18 +0200488 OPT_BOOLEAN('i', "inherit", &inherit,
489 "child tasks inherit counters"),
490 OPT_INTEGER('p', "pid", &target_pid,
491 "stat events on existing pid"),
492 OPT_BOOLEAN('a', "all-cpus", &system_wide,
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530493 "system-wide collection from all CPUs"),
Brice Goglinb26bc5a2009-08-07 10:18:39 +0200494 OPT_BOOLEAN('c', "scale", &scale,
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530495 "scale/normalize counters"),
Ingo Molnar743ee1f2009-06-07 17:06:46 +0200496 OPT_BOOLEAN('v', "verbose", &verbose,
497 "be more verbose (show counter open errors, etc)"),
Ingo Molnar42202dd2009-06-13 14:57:28 +0200498 OPT_INTEGER('r', "repeat", &run_count,
499 "repeat command and print average + stddev (max: 100)"),
Ingo Molnar0cfb7a12009-06-27 06:10:30 +0200500 OPT_BOOLEAN('n', "null", &null_run,
501 "null run - dont start any counters"),
Ingo Molnar52425192009-05-26 09:17:18 +0200502 OPT_END()
503};
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200504
Ingo Molnarf37a2912009-07-01 12:37:06 +0200505int cmd_stat(int argc, const char **argv, const char *prefix __used)
Ingo Molnar52425192009-05-26 09:17:18 +0200506{
Ingo Molnar42202dd2009-06-13 14:57:28 +0200507 int status;
508
Anton Blancharda0541232009-07-22 23:04:12 +1000509 argc = parse_options(argc, argv, options, stat_usage,
510 PARSE_OPT_STOP_AT_NON_OPTION);
Liming Wang60666c62009-12-31 16:05:50 +0800511 if (!argc && target_pid == -1)
Ingo Molnar52425192009-05-26 09:17:18 +0200512 usage_with_options(stat_usage, options);
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200513 if (run_count <= 0)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200514 usage_with_options(stat_usage, options);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200515
Jaswinder Singh Rajputc3043562009-06-27 23:49:09 +0530516 /* Set attrs and nr_counters if no event is selected and !null_run */
517 if (!null_run && !nr_counters) {
518 memcpy(attrs, default_attrs, sizeof(default_attrs));
519 nr_counters = ARRAY_SIZE(default_attrs);
520 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200521
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200522 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
523 assert(nr_cpus <= MAX_NR_CPUS);
Ingo Molnarf37a2912009-07-01 12:37:06 +0200524 assert((int)nr_cpus >= 0);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200525
Ingo Molnar58d7e992009-05-15 11:03:23 +0200526 /*
527 * We dont want to block the signals - that would cause
528 * child tasks to inherit that and Ctrl-C would not work.
529 * What we want is for Ctrl-C to work in the exec()-ed
530 * task, but being ignored by perf stat itself:
531 */
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200532 atexit(sig_atexit);
Ingo Molnar58d7e992009-05-15 11:03:23 +0200533 signal(SIGINT, skip_signal);
534 signal(SIGALRM, skip_signal);
535 signal(SIGABRT, skip_signal);
536
Ingo Molnar42202dd2009-06-13 14:57:28 +0200537 status = 0;
538 for (run_idx = 0; run_idx < run_count; run_idx++) {
539 if (run_count != 1 && verbose)
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530540 fprintf(stderr, "[ perf stat: executing run #%d ... ]\n", run_idx + 1);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200541 status = run_perf_stat(argc, argv);
542 }
543
544 print_stat(argc, argv);
545
546 return status;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200547}