perf stat: Add stalled cycles accounting, prettify the resulting output
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / tools / perf / builtin-stat.c
CommitLineData
ddcacfa0 1/*
bf9e1876
IM
2 * builtin-stat.c
3 *
4 * Builtin stat command: Give a precise performance counters summary
5 * overview about any workload, CPU or specific PID.
6 *
7 * Sample output:
ddcacfa0 8
bf9e1876
IM
9 $ perf stat ~/hackbench 10
10 Time: 0.104
ddcacfa0 11
bf9e1876 12 Performance counter stats for '/home/mingo/hackbench':
ddcacfa0 13
bf9e1876
IM
14 1255.538611 task clock ticks # 10.143 CPU utilization factor
15 54011 context switches # 0.043 M/sec
16 385 CPU migrations # 0.000 M/sec
17 17755 pagefaults # 0.014 M/sec
18 3808323185 CPU cycles # 3033.219 M/sec
19 1575111190 instructions # 1254.530 M/sec
20 17367895 cache references # 13.833 M/sec
21 7674421 cache misses # 6.112 M/sec
ddcacfa0 22
bf9e1876 23 Wall-clock time elapsed: 123.786620 msecs
ddcacfa0 24
5242519b
IM
25 *
26 * Copyright (C) 2008, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
27 *
28 * Improvements and fixes by:
29 *
30 * Arjan van de Ven <arjan@linux.intel.com>
31 * Yanmin Zhang <yanmin.zhang@intel.com>
32 * Wu Fengguang <fengguang.wu@intel.com>
33 * Mike Galbraith <efault@gmx.de>
34 * Paul Mackerras <paulus@samba.org>
6e750a8f 35 * Jaswinder Singh Rajput <jaswinder@kernel.org>
5242519b
IM
36 *
37 * Released under the GPL v2. (and only v2, not any later version)
ddcacfa0
IM
38 */
39
1a482f38 40#include "perf.h"
16f762a2 41#include "builtin.h"
148be2c1 42#include "util/util.h"
5242519b
IM
43#include "util/parse-options.h"
44#include "util/parse-events.h"
8f28827a 45#include "util/event.h"
361c99a6 46#include "util/evlist.h"
69aad6f1 47#include "util/evsel.h"
8f28827a 48#include "util/debug.h"
60666c63 49#include "util/header.h"
a12b51c4 50#include "util/cpumap.h"
d6d901c2 51#include "util/thread.h"
fd78260b 52#include "util/thread_map.h"
ddcacfa0 53
ddcacfa0 54#include <sys/prctl.h>
42202dd5 55#include <math.h>
5af52b51 56#include <locale.h>
16c8a109 57
d7470b6a
SE
58#define DEFAULT_SEPARATOR " "
59
cdd6c482 60static struct perf_event_attr default_attrs[] = {
ddcacfa0 61
56aab464
IM
62 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
63 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
64 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
65 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
f4dbfa8f 66
56aab464
IM
67 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
68 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
56aab464
IM
69 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
70 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
dd86e72a
IM
71 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CACHE_REFERENCES },
72 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CACHE_MISSES },
f4dbfa8f 73
ddcacfa0 74};
5242519b 75
361c99a6
ACM
76struct perf_evlist *evsel_list;
77
c0555642 78static bool system_wide = false;
3d632595 79static int run_idx = 0;
ddcacfa0 80
3d632595 81static int run_count = 1;
2e6cdf99 82static bool no_inherit = false;
c0555642 83static bool scale = true;
f5b4a9c3 84static bool no_aggr = false;
933da83a 85static pid_t target_pid = -1;
d6d901c2 86static pid_t target_tid = -1;
933da83a 87static pid_t child_pid = -1;
c0555642 88static bool null_run = false;
201e0b06 89static bool big_num = true;
d7470b6a 90static int big_num_opt = -1;
c45c6ea2 91static const char *cpu_list;
d7470b6a
SE
92static const char *csv_sep = NULL;
93static bool csv_output = false;
5af52b51 94
60666c63
LW
95static volatile int done = 0;
96
506d4bc8
PZ
97struct stats
98{
8a02631a 99 double n, mean, M2;
506d4bc8 100};
42202dd5 101
69aad6f1
ACM
102struct perf_stat {
103 struct stats res_stats[3];
69aad6f1
ACM
104};
105
c52b12ed 106static int perf_evsel__alloc_stat_priv(struct perf_evsel *evsel)
69aad6f1 107{
c52b12ed 108 evsel->priv = zalloc(sizeof(struct perf_stat));
69aad6f1
ACM
109 return evsel->priv == NULL ? -ENOMEM : 0;
110}
111
112static void perf_evsel__free_stat_priv(struct perf_evsel *evsel)
113{
114 free(evsel->priv);
115 evsel->priv = NULL;
116}
117
9e9772c4
PZ
118static void update_stats(struct stats *stats, u64 val)
119{
8a02631a 120 double delta;
9e9772c4 121
8a02631a
PZ
122 stats->n++;
123 delta = val - stats->mean;
124 stats->mean += delta / stats->n;
125 stats->M2 += delta*(val - stats->mean);
9e9772c4
PZ
126}
127
506d4bc8
PZ
128static double avg_stats(struct stats *stats)
129{
8a02631a 130 return stats->mean;
506d4bc8 131}
42202dd5 132
506d4bc8 133/*
63d40deb
PZ
134 * http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
135 *
8a02631a
PZ
136 * (\Sum n_i^2) - ((\Sum n_i)^2)/n
137 * s^2 = -------------------------------
138 * n - 1
63d40deb
PZ
139 *
140 * http://en.wikipedia.org/wiki/Stddev
141 *
142 * The std dev of the mean is related to the std dev by:
143 *
144 * s
145 * s_mean = -------
146 * sqrt(n)
147 *
506d4bc8
PZ
148 */
149static double stddev_stats(struct stats *stats)
150{
8a02631a
PZ
151 double variance = stats->M2 / (stats->n - 1);
152 double variance_mean = variance / stats->n;
42202dd5 153
63d40deb 154 return sqrt(variance_mean);
506d4bc8 155}
42202dd5 156
f5b4a9c3
SE
157struct stats runtime_nsecs_stats[MAX_NR_CPUS];
158struct stats runtime_cycles_stats[MAX_NR_CPUS];
481f988a 159struct stats runtime_stalled_cycles_stats[MAX_NR_CPUS];
f5b4a9c3 160struct stats runtime_branches_stats[MAX_NR_CPUS];
d58f4c82 161struct stats runtime_cacherefs_stats[MAX_NR_CPUS];
506d4bc8 162struct stats walltime_nsecs_stats;
be1ac0d8 163
48290609 164static int create_perf_stat_counter(struct perf_evsel *evsel)
ddcacfa0 165{
69aad6f1 166 struct perf_event_attr *attr = &evsel->attr;
16c8a109 167
ddcacfa0 168 if (scale)
a21ca2ca
IM
169 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
170 PERF_FORMAT_TOTAL_TIME_RUNNING;
ddcacfa0 171
5d2cd909
ACM
172 attr->inherit = !no_inherit;
173
48290609 174 if (system_wide)
5d2cd909 175 return perf_evsel__open_per_cpu(evsel, evsel_list->cpus, false);
48290609 176
48290609
ACM
177 if (target_pid == -1 && target_tid == -1) {
178 attr->disabled = 1;
179 attr->enable_on_exec = 1;
ddcacfa0 180 }
084ab9f8 181
5d2cd909 182 return perf_evsel__open_per_thread(evsel, evsel_list->threads, false);
ddcacfa0
IM
183}
184
c04f5e5d
IM
185/*
186 * Does the counter have nsecs as a unit?
187 */
daec78a0 188static inline int nsec_counter(struct perf_evsel *evsel)
c04f5e5d 189{
daec78a0
ACM
190 if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) ||
191 perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
c04f5e5d
IM
192 return 1;
193
194 return 0;
195}
196
dcd9936a
IM
197/*
198 * Update various tracking values we maintain to print
199 * more semantic information such as miss/hit ratios,
200 * instruction rates, etc:
201 */
202static void update_shadow_stats(struct perf_evsel *counter, u64 *count)
203{
204 if (perf_evsel__match(counter, SOFTWARE, SW_TASK_CLOCK))
205 update_stats(&runtime_nsecs_stats[0], count[0]);
206 else if (perf_evsel__match(counter, HARDWARE, HW_CPU_CYCLES))
207 update_stats(&runtime_cycles_stats[0], count[0]);
481f988a
IM
208 else if (perf_evsel__match(counter, HARDWARE, HW_STALLED_CYCLES))
209 update_stats(&runtime_stalled_cycles_stats[0], count[0]);
dcd9936a
IM
210 else if (perf_evsel__match(counter, HARDWARE, HW_BRANCH_INSTRUCTIONS))
211 update_stats(&runtime_branches_stats[0], count[0]);
212 else if (perf_evsel__match(counter, HARDWARE, HW_CACHE_REFERENCES))
213 update_stats(&runtime_cacherefs_stats[0], count[0]);
214}
215
c04f5e5d 216/*
2996f5dd 217 * Read out the results of a single counter:
f5b4a9c3 218 * aggregate counts across CPUs in system-wide mode
c04f5e5d 219 */
c52b12ed 220static int read_counter_aggr(struct perf_evsel *counter)
c04f5e5d 221{
69aad6f1 222 struct perf_stat *ps = counter->priv;
c52b12ed
ACM
223 u64 *count = counter->counts->aggr.values;
224 int i;
2996f5dd 225
7e2ed097
ACM
226 if (__perf_evsel__read(counter, evsel_list->cpus->nr,
227 evsel_list->threads->nr, scale) < 0)
c52b12ed 228 return -1;
9e9772c4
PZ
229
230 for (i = 0; i < 3; i++)
69aad6f1 231 update_stats(&ps->res_stats[i], count[i]);
9e9772c4
PZ
232
233 if (verbose) {
9486aa38
ACM
234 fprintf(stderr, "%s: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
235 event_name(counter), count[0], count[1], count[2]);
9e9772c4
PZ
236 }
237
be1ac0d8
IM
238 /*
239 * Save the full runtime - to allow normalization during printout:
240 */
dcd9936a 241 update_shadow_stats(counter, count);
c52b12ed
ACM
242
243 return 0;
f5b4a9c3
SE
244}
245
246/*
247 * Read out the results of a single counter:
248 * do not aggregate counts across CPUs in system-wide mode
249 */
c52b12ed 250static int read_counter(struct perf_evsel *counter)
f5b4a9c3 251{
c52b12ed 252 u64 *count;
f5b4a9c3 253 int cpu;
f5b4a9c3 254
7e2ed097 255 for (cpu = 0; cpu < evsel_list->cpus->nr; cpu++) {
c52b12ed
ACM
256 if (__perf_evsel__read_on_cpu(counter, cpu, 0, scale) < 0)
257 return -1;
f5b4a9c3 258
c52b12ed 259 count = counter->counts->cpu[cpu].values;
f5b4a9c3 260
dcd9936a 261 update_shadow_stats(counter, count);
f5b4a9c3 262 }
c52b12ed
ACM
263
264 return 0;
2996f5dd
IM
265}
266
f37a291c 267static int run_perf_stat(int argc __used, const char **argv)
42202dd5
IM
268{
269 unsigned long long t0, t1;
69aad6f1 270 struct perf_evsel *counter;
42202dd5 271 int status = 0;
051ae7f7 272 int child_ready_pipe[2], go_pipe[2];
6be2850e 273 const bool forks = (argc > 0);
051ae7f7 274 char buf;
42202dd5 275
60666c63 276 if (forks && (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0)) {
051ae7f7
PM
277 perror("failed to create pipes");
278 exit(1);
279 }
280
60666c63 281 if (forks) {
6be2850e 282 if ((child_pid = fork()) < 0)
60666c63
LW
283 perror("failed to fork");
284
6be2850e 285 if (!child_pid) {
60666c63
LW
286 close(child_ready_pipe[0]);
287 close(go_pipe[1]);
288 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
289
290 /*
291 * Do a dummy execvp to get the PLT entry resolved,
292 * so we avoid the resolver overhead on the real
293 * execvp call.
294 */
295 execvp("", (char **)argv);
296
297 /*
298 * Tell the parent we're ready to go
299 */
300 close(child_ready_pipe[1]);
301
302 /*
303 * Wait until the parent tells us to go.
304 */
305 if (read(go_pipe[0], &buf, 1) == -1)
306 perror("unable to read pipe");
307
308 execvp(argv[0], (char **)argv);
309
310 perror(argv[0]);
311 exit(-1);
312 }
051ae7f7 313
d6d901c2 314 if (target_tid == -1 && target_pid == -1 && !system_wide)
7e2ed097 315 evsel_list->threads->map[0] = child_pid;
d6d901c2 316
051ae7f7 317 /*
60666c63 318 * Wait for the child to be ready to exec.
051ae7f7
PM
319 */
320 close(child_ready_pipe[1]);
60666c63
LW
321 close(go_pipe[0]);
322 if (read(child_ready_pipe[0], &buf, 1) == -1)
a92bef0f 323 perror("unable to read pipe");
60666c63 324 close(child_ready_pipe[0]);
051ae7f7
PM
325 }
326
361c99a6 327 list_for_each_entry(counter, &evsel_list->entries, node) {
48290609
ACM
328 if (create_perf_stat_counter(counter) < 0) {
329 if (errno == -EPERM || errno == -EACCES) {
330 error("You may not have permission to collect %sstats.\n"
331 "\t Consider tweaking"
332 " /proc/sys/kernel/perf_event_paranoid or running as root.",
333 system_wide ? "system-wide " : "");
5a3446bc
DA
334 } else if (errno == ENOENT) {
335 error("%s event is not supported. ", event_name(counter));
48290609
ACM
336 } else {
337 error("open_counter returned with %d (%s). "
338 "/bin/dmesg may provide additional information.\n",
339 errno, strerror(errno));
340 }
341 if (child_pid != -1)
342 kill(child_pid, SIGTERM);
343 die("Not all events could be opened.\n");
344 return -1;
345 }
084ab9f8 346 }
42202dd5 347
cfd748ae
FW
348 if (perf_evlist__set_filters(evsel_list)) {
349 error("failed to set filter with %d (%s)\n", errno,
350 strerror(errno));
351 return -1;
352 }
353
42202dd5
IM
354 /*
355 * Enable counters and exec the command:
356 */
357 t0 = rdclock();
42202dd5 358
60666c63
LW
359 if (forks) {
360 close(go_pipe[1]);
361 wait(&status);
362 } else {
6be2850e 363 while(!done) sleep(1);
60666c63 364 }
42202dd5 365
42202dd5
IM
366 t1 = rdclock();
367
9e9772c4 368 update_stats(&walltime_nsecs_stats, t1 - t0);
42202dd5 369
f5b4a9c3 370 if (no_aggr) {
361c99a6 371 list_for_each_entry(counter, &evsel_list->entries, node) {
f5b4a9c3 372 read_counter(counter);
7e2ed097 373 perf_evsel__close_fd(counter, evsel_list->cpus->nr, 1);
c52b12ed 374 }
f5b4a9c3 375 } else {
361c99a6 376 list_for_each_entry(counter, &evsel_list->entries, node) {
f5b4a9c3 377 read_counter_aggr(counter);
7e2ed097
ACM
378 perf_evsel__close_fd(counter, evsel_list->cpus->nr,
379 evsel_list->threads->nr);
c52b12ed 380 }
f5b4a9c3 381 }
c52b12ed 382
42202dd5
IM
383 return WEXITSTATUS(status);
384}
385
69aad6f1 386static void print_noise(struct perf_evsel *evsel, double avg)
42202dd5 387{
69aad6f1
ACM
388 struct perf_stat *ps;
389
849abde9
PZ
390 if (run_count == 1)
391 return;
392
69aad6f1 393 ps = evsel->priv;
849abde9 394 fprintf(stderr, " ( +- %7.3f%% )",
69aad6f1 395 100 * stddev_stats(&ps->res_stats[0]) / avg);
42202dd5
IM
396}
397
daec78a0 398static void nsec_printout(int cpu, struct perf_evsel *evsel, double avg)
44175b6f 399{
506d4bc8 400 double msecs = avg / 1e6;
d7470b6a
SE
401 char cpustr[16] = { '\0', };
402 const char *fmt = csv_output ? "%s%.6f%s%s" : "%s%18.6f%s%-24s";
44175b6f 403
f5b4a9c3 404 if (no_aggr)
d7470b6a
SE
405 sprintf(cpustr, "CPU%*d%s",
406 csv_output ? 0 : -4,
7e2ed097 407 evsel_list->cpus->map[cpu], csv_sep);
d7470b6a 408
daec78a0 409 fprintf(stderr, fmt, cpustr, msecs, csv_sep, event_name(evsel));
d7470b6a 410
023695d9
SE
411 if (evsel->cgrp)
412 fprintf(stderr, "%s%s", csv_sep, evsel->cgrp->name);
413
d7470b6a
SE
414 if (csv_output)
415 return;
44175b6f 416
daec78a0 417 if (perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
481f988a 418 fprintf(stderr, " # %8.3f CPUs utilized ", avg / avg_stats(&walltime_nsecs_stats));
44175b6f
IM
419}
420
daec78a0 421static void abs_printout(int cpu, struct perf_evsel *evsel, double avg)
44175b6f 422{
c7f7fea3 423 double total, ratio = 0.0;
f5b4a9c3 424 char cpustr[16] = { '\0', };
d7470b6a
SE
425 const char *fmt;
426
427 if (csv_output)
428 fmt = "%s%.0f%s%s";
429 else if (big_num)
430 fmt = "%s%'18.0f%s%-24s";
431 else
432 fmt = "%s%18.0f%s%-24s";
f5b4a9c3
SE
433
434 if (no_aggr)
d7470b6a
SE
435 sprintf(cpustr, "CPU%*d%s",
436 csv_output ? 0 : -4,
7e2ed097 437 evsel_list->cpus->map[cpu], csv_sep);
f5b4a9c3
SE
438 else
439 cpu = 0;
c7f7fea3 440
daec78a0 441 fprintf(stderr, fmt, cpustr, avg, csv_sep, event_name(evsel));
d7470b6a 442
023695d9
SE
443 if (evsel->cgrp)
444 fprintf(stderr, "%s%s", csv_sep, evsel->cgrp->name);
445
d7470b6a
SE
446 if (csv_output)
447 return;
44175b6f 448
daec78a0 449 if (perf_evsel__match(evsel, HARDWARE, HW_INSTRUCTIONS)) {
f5b4a9c3 450 total = avg_stats(&runtime_cycles_stats[cpu]);
c7f7fea3
IM
451
452 if (total)
453 ratio = avg / total;
454
481f988a
IM
455 fprintf(stderr, " # %4.2f insns per cycle", ratio);
456
457 total = avg_stats(&runtime_stalled_cycles_stats[cpu]);
458
459 if (total && avg) {
460 ratio = total / avg;
461 fprintf(stderr, "\n # %4.2f stalled cycles per insn", ratio);
462 }
463
daec78a0 464 } else if (perf_evsel__match(evsel, HARDWARE, HW_BRANCH_MISSES) &&
f5b4a9c3
SE
465 runtime_branches_stats[cpu].n != 0) {
466 total = avg_stats(&runtime_branches_stats[cpu]);
11018201
AB
467
468 if (total)
469 ratio = avg * 100 / total;
470
481f988a 471 fprintf(stderr, " # %8.3f %% of all branches", ratio);
11018201 472
d58f4c82
IM
473 } else if (perf_evsel__match(evsel, HARDWARE, HW_CACHE_MISSES) &&
474 runtime_cacherefs_stats[cpu].n != 0) {
475 total = avg_stats(&runtime_cacherefs_stats[cpu]);
476
477 if (total)
478 ratio = avg * 100 / total;
479
481f988a 480 fprintf(stderr, " # %8.3f %% of all cache refs ", ratio);
d58f4c82 481
481f988a
IM
482 } else if (perf_evsel__match(evsel, HARDWARE, HW_STALLED_CYCLES)) {
483 total = avg_stats(&runtime_cycles_stats[cpu]);
484
485 if (total)
486 ratio = avg / total * 100.0;
487
488 fprintf(stderr, " # %5.2f%% of all cycles are idle ", ratio);
489 } else if (perf_evsel__match(evsel, HARDWARE, HW_CPU_CYCLES)) {
f5b4a9c3 490 total = avg_stats(&runtime_nsecs_stats[cpu]);
c7f7fea3
IM
491
492 if (total)
481f988a 493 ratio = 1.0 * avg / total;
c7f7fea3 494
481f988a
IM
495 fprintf(stderr, " # %8.3f GHz ", ratio);
496 } else if (runtime_nsecs_stats[cpu].n != 0) {
497 total = avg_stats(&runtime_nsecs_stats[cpu]);
11ba2b85
IM
498
499 if (total)
481f988a 500 ratio = 1000.0 * avg / total;
11ba2b85 501
481f988a 502 fprintf(stderr, " # %8.3f M/sec ", ratio);
44175b6f 503 }
44175b6f
IM
504}
505
2996f5dd
IM
506/*
507 * Print out the results of a single counter:
f5b4a9c3 508 * aggregated counts in system-wide mode
2996f5dd 509 */
69aad6f1 510static void print_counter_aggr(struct perf_evsel *counter)
2996f5dd 511{
69aad6f1
ACM
512 struct perf_stat *ps = counter->priv;
513 double avg = avg_stats(&ps->res_stats[0]);
c52b12ed 514 int scaled = counter->counts->scaled;
2996f5dd 515
2996f5dd 516 if (scaled == -1) {
023695d9 517 fprintf(stderr, "%*s%s%*s",
d7470b6a 518 csv_output ? 0 : 18,
023695d9
SE
519 "<not counted>",
520 csv_sep,
521 csv_output ? 0 : -24,
522 event_name(counter));
523
524 if (counter->cgrp)
525 fprintf(stderr, "%s%s", csv_sep, counter->cgrp->name);
526
527 fputc('\n', stderr);
2996f5dd
IM
528 return;
529 }
c04f5e5d 530
44175b6f 531 if (nsec_counter(counter))
f5b4a9c3 532 nsec_printout(-1, counter, avg);
44175b6f 533 else
f5b4a9c3 534 abs_printout(-1, counter, avg);
849abde9 535
d7470b6a
SE
536 if (csv_output) {
537 fputc('\n', stderr);
538 return;
539 }
540
849abde9 541 print_noise(counter, avg);
506d4bc8
PZ
542
543 if (scaled) {
544 double avg_enabled, avg_running;
545
69aad6f1
ACM
546 avg_enabled = avg_stats(&ps->res_stats[1]);
547 avg_running = avg_stats(&ps->res_stats[2]);
d7c29318 548
210ad39f 549 fprintf(stderr, " (scaled from %.2f%%)",
506d4bc8
PZ
550 100 * avg_running / avg_enabled);
551 }
c04f5e5d
IM
552 fprintf(stderr, "\n");
553}
554
f5b4a9c3
SE
555/*
556 * Print out the results of a single counter:
557 * does not use aggregated count in system-wide
558 */
69aad6f1 559static void print_counter(struct perf_evsel *counter)
f5b4a9c3
SE
560{
561 u64 ena, run, val;
562 int cpu;
563
7e2ed097 564 for (cpu = 0; cpu < evsel_list->cpus->nr; cpu++) {
c52b12ed
ACM
565 val = counter->counts->cpu[cpu].val;
566 ena = counter->counts->cpu[cpu].ena;
567 run = counter->counts->cpu[cpu].run;
f5b4a9c3 568 if (run == 0 || ena == 0) {
023695d9 569 fprintf(stderr, "CPU%*d%s%*s%s%*s",
d7470b6a 570 csv_output ? 0 : -4,
7e2ed097 571 evsel_list->cpus->map[cpu], csv_sep,
d7470b6a
SE
572 csv_output ? 0 : 18,
573 "<not counted>", csv_sep,
023695d9 574 csv_output ? 0 : -24,
d7470b6a 575 event_name(counter));
f5b4a9c3 576
023695d9
SE
577 if (counter->cgrp)
578 fprintf(stderr, "%s%s", csv_sep, counter->cgrp->name);
579
580 fputc('\n', stderr);
f5b4a9c3
SE
581 continue;
582 }
583
584 if (nsec_counter(counter))
585 nsec_printout(cpu, counter, val);
586 else
587 abs_printout(cpu, counter, val);
588
d7470b6a
SE
589 if (!csv_output) {
590 print_noise(counter, 1.0);
f5b4a9c3 591
d7470b6a
SE
592 if (run != ena) {
593 fprintf(stderr, " (scaled from %.2f%%)",
f5b4a9c3 594 100.0 * run / ena);
d7470b6a 595 }
f5b4a9c3 596 }
023695d9 597 fputc('\n', stderr);
f5b4a9c3
SE
598 }
599}
600
42202dd5
IM
601static void print_stat(int argc, const char **argv)
602{
69aad6f1
ACM
603 struct perf_evsel *counter;
604 int i;
42202dd5 605
ddcacfa0
IM
606 fflush(stdout);
607
d7470b6a
SE
608 if (!csv_output) {
609 fprintf(stderr, "\n");
610 fprintf(stderr, " Performance counter stats for ");
611 if(target_pid == -1 && target_tid == -1) {
612 fprintf(stderr, "\'%s", argv[0]);
613 for (i = 1; i < argc; i++)
614 fprintf(stderr, " %s", argv[i]);
615 } else if (target_pid != -1)
616 fprintf(stderr, "process id \'%d", target_pid);
617 else
618 fprintf(stderr, "thread id \'%d", target_tid);
44db76c8 619
d7470b6a
SE
620 fprintf(stderr, "\'");
621 if (run_count > 1)
622 fprintf(stderr, " (%d runs)", run_count);
623 fprintf(stderr, ":\n\n");
624 }
2996f5dd 625
f5b4a9c3 626 if (no_aggr) {
361c99a6 627 list_for_each_entry(counter, &evsel_list->entries, node)
f5b4a9c3
SE
628 print_counter(counter);
629 } else {
361c99a6 630 list_for_each_entry(counter, &evsel_list->entries, node)
f5b4a9c3
SE
631 print_counter_aggr(counter);
632 }
ddcacfa0 633
d7470b6a
SE
634 if (!csv_output) {
635 fprintf(stderr, "\n");
636 fprintf(stderr, " %18.9f seconds time elapsed",
637 avg_stats(&walltime_nsecs_stats)/1e9);
638 if (run_count > 1) {
481f988a 639 fprintf(stderr, " ( +-%5.2f%% )",
506d4bc8
PZ
640 100*stddev_stats(&walltime_nsecs_stats) /
641 avg_stats(&walltime_nsecs_stats));
d7470b6a
SE
642 }
643 fprintf(stderr, "\n\n");
566747e6 644 }
ddcacfa0
IM
645}
646
f7b7c26e
PZ
647static volatile int signr = -1;
648
5242519b 649static void skip_signal(int signo)
ddcacfa0 650{
6be2850e 651 if(child_pid == -1)
60666c63
LW
652 done = 1;
653
f7b7c26e
PZ
654 signr = signo;
655}
656
657static void sig_atexit(void)
658{
933da83a
CW
659 if (child_pid != -1)
660 kill(child_pid, SIGTERM);
661
f7b7c26e
PZ
662 if (signr == -1)
663 return;
664
665 signal(signr, SIG_DFL);
666 kill(getpid(), signr);
5242519b
IM
667}
668
669static const char * const stat_usage[] = {
60666c63 670 "perf stat [<options>] [<command>]",
5242519b
IM
671 NULL
672};
673
d7470b6a
SE
674static int stat__set_big_num(const struct option *opt __used,
675 const char *s __used, int unset)
676{
677 big_num_opt = unset ? 0 : 1;
678 return 0;
679}
680
5242519b 681static const struct option options[] = {
361c99a6 682 OPT_CALLBACK('e', "event", &evsel_list, "event",
86847b62
TG
683 "event selector. use 'perf list' to list available events",
684 parse_events),
cfd748ae
FW
685 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
686 "event filter", parse_filter),
2e6cdf99
SE
687 OPT_BOOLEAN('i', "no-inherit", &no_inherit,
688 "child tasks do not inherit counters"),
5242519b 689 OPT_INTEGER('p', "pid", &target_pid,
d6d901c2
ZY
690 "stat events on existing process id"),
691 OPT_INTEGER('t', "tid", &target_tid,
692 "stat events on existing thread id"),
5242519b 693 OPT_BOOLEAN('a', "all-cpus", &system_wide,
3d632595 694 "system-wide collection from all CPUs"),
b26bc5a7 695 OPT_BOOLEAN('c', "scale", &scale,
3d632595 696 "scale/normalize counters"),
c0555642 697 OPT_INCR('v', "verbose", &verbose,
743ee1f8 698 "be more verbose (show counter open errors, etc)"),
42202dd5
IM
699 OPT_INTEGER('r', "repeat", &run_count,
700 "repeat command and print average + stddev (max: 100)"),
0cfb7a13
IM
701 OPT_BOOLEAN('n', "null", &null_run,
702 "null run - dont start any counters"),
d7470b6a
SE
703 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
704 "print large numbers with thousands\' separators",
705 stat__set_big_num),
c45c6ea2
SE
706 OPT_STRING('C', "cpu", &cpu_list, "cpu",
707 "list of cpus to monitor in system-wide"),
f5b4a9c3
SE
708 OPT_BOOLEAN('A', "no-aggr", &no_aggr,
709 "disable CPU count aggregation"),
d7470b6a
SE
710 OPT_STRING('x', "field-separator", &csv_sep, "separator",
711 "print counts with custom separator"),
023695d9
SE
712 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
713 "monitor event in cgroup name only",
714 parse_cgroups),
5242519b
IM
715 OPT_END()
716};
717
f37a291c 718int cmd_stat(int argc, const char **argv, const char *prefix __used)
5242519b 719{
69aad6f1
ACM
720 struct perf_evsel *pos;
721 int status = -ENOMEM;
42202dd5 722
5af52b51
SE
723 setlocale(LC_ALL, "");
724
7e2ed097 725 evsel_list = perf_evlist__new(NULL, NULL);
361c99a6
ACM
726 if (evsel_list == NULL)
727 return -ENOMEM;
728
a0541234
AB
729 argc = parse_options(argc, argv, options, stat_usage,
730 PARSE_OPT_STOP_AT_NON_OPTION);
d7470b6a
SE
731
732 if (csv_sep)
733 csv_output = true;
734 else
735 csv_sep = DEFAULT_SEPARATOR;
736
737 /*
738 * let the spreadsheet do the pretty-printing
739 */
740 if (csv_output) {
741 /* User explicitely passed -B? */
742 if (big_num_opt == 1) {
743 fprintf(stderr, "-B option not supported with -x\n");
744 usage_with_options(stat_usage, options);
745 } else /* Nope, so disable big number formatting */
746 big_num = false;
747 } else if (big_num_opt == 0) /* User passed --no-big-num */
748 big_num = false;
749
d6d901c2 750 if (!argc && target_pid == -1 && target_tid == -1)
5242519b 751 usage_with_options(stat_usage, options);
9e9772c4 752 if (run_count <= 0)
42202dd5 753 usage_with_options(stat_usage, options);
ddcacfa0 754
023695d9
SE
755 /* no_aggr, cgroup are for system-wide only */
756 if ((no_aggr || nr_cgroups) && !system_wide) {
757 fprintf(stderr, "both cgroup and no-aggregation "
758 "modes only available in system-wide mode\n");
759
f5b4a9c3 760 usage_with_options(stat_usage, options);
023695d9 761 }
f5b4a9c3 762
c3043569 763 /* Set attrs and nr_counters if no event is selected and !null_run */
361c99a6 764 if (!null_run && !evsel_list->nr_entries) {
69aad6f1
ACM
765 size_t c;
766
69aad6f1 767 for (c = 0; c < ARRAY_SIZE(default_attrs); ++c) {
361c99a6 768 pos = perf_evsel__new(&default_attrs[c], c);
69aad6f1
ACM
769 if (pos == NULL)
770 goto out;
361c99a6 771 perf_evlist__add(evsel_list, pos);
69aad6f1 772 }
c3043569 773 }
ddcacfa0 774
5c98d466
ACM
775 if (target_pid != -1)
776 target_tid = target_pid;
777
7e2ed097
ACM
778 evsel_list->threads = thread_map__new(target_pid, target_tid);
779 if (evsel_list->threads == NULL) {
5c98d466
ACM
780 pr_err("Problems finding threads of monitor\n");
781 usage_with_options(stat_usage, options);
782 }
783
a12b51c4 784 if (system_wide)
7e2ed097 785 evsel_list->cpus = cpu_map__new(cpu_list);
a12b51c4 786 else
7e2ed097 787 evsel_list->cpus = cpu_map__dummy_new();
ddcacfa0 788
7e2ed097 789 if (evsel_list->cpus == NULL) {
60d567e2 790 perror("failed to parse CPUs map");
c45c6ea2 791 usage_with_options(stat_usage, options);
60d567e2
ACM
792 return -1;
793 }
c45c6ea2 794
361c99a6 795 list_for_each_entry(pos, &evsel_list->entries, node) {
c52b12ed 796 if (perf_evsel__alloc_stat_priv(pos) < 0 ||
7e2ed097
ACM
797 perf_evsel__alloc_counts(pos, evsel_list->cpus->nr) < 0 ||
798 perf_evsel__alloc_fd(pos, evsel_list->cpus->nr, evsel_list->threads->nr) < 0)
69aad6f1 799 goto out_free_fd;
d6d901c2
ZY
800 }
801
58d7e993
IM
802 /*
803 * We dont want to block the signals - that would cause
804 * child tasks to inherit that and Ctrl-C would not work.
805 * What we want is for Ctrl-C to work in the exec()-ed
806 * task, but being ignored by perf stat itself:
807 */
f7b7c26e 808 atexit(sig_atexit);
58d7e993
IM
809 signal(SIGINT, skip_signal);
810 signal(SIGALRM, skip_signal);
811 signal(SIGABRT, skip_signal);
812
42202dd5
IM
813 status = 0;
814 for (run_idx = 0; run_idx < run_count; run_idx++) {
815 if (run_count != 1 && verbose)
3d632595 816 fprintf(stderr, "[ perf stat: executing run #%d ... ]\n", run_idx + 1);
42202dd5
IM
817 status = run_perf_stat(argc, argv);
818 }
819
084ab9f8
ACM
820 if (status != -1)
821 print_stat(argc, argv);
69aad6f1 822out_free_fd:
361c99a6 823 list_for_each_entry(pos, &evsel_list->entries, node)
69aad6f1 824 perf_evsel__free_stat_priv(pos);
7e2ed097 825 perf_evlist__delete_maps(evsel_list);
0015e2e1
ACM
826out:
827 perf_evlist__delete(evsel_list);
42202dd5 828 return status;
ddcacfa0 829}