perf stat: Use xyarray for cpu evsel counts
[linux-2.6-block.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
2cba3ffb 9 $ perf stat ./hackbench 10
ddcacfa0 10
2cba3ffb 11 Time: 0.118
ddcacfa0 12
2cba3ffb 13 Performance counter stats for './hackbench 10':
ddcacfa0 14
2cba3ffb
IM
15 1708.761321 task-clock # 11.037 CPUs utilized
16 41,190 context-switches # 0.024 M/sec
17 6,735 CPU-migrations # 0.004 M/sec
18 17,318 page-faults # 0.010 M/sec
19 5,205,202,243 cycles # 3.046 GHz
20 3,856,436,920 stalled-cycles-frontend # 74.09% frontend cycles idle
21 1,600,790,871 stalled-cycles-backend # 30.75% backend cycles idle
22 2,603,501,247 instructions # 0.50 insns per cycle
23 # 1.48 stalled cycles per insn
24 484,357,498 branches # 283.455 M/sec
25 6,388,934 branch-misses # 1.32% of all branches
26
27 0.154822978 seconds time elapsed
ddcacfa0 28
5242519b 29 *
2cba3ffb 30 * Copyright (C) 2008-2011, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
5242519b
IM
31 *
32 * Improvements and fixes by:
33 *
34 * Arjan van de Ven <arjan@linux.intel.com>
35 * Yanmin Zhang <yanmin.zhang@intel.com>
36 * Wu Fengguang <fengguang.wu@intel.com>
37 * Mike Galbraith <efault@gmx.de>
38 * Paul Mackerras <paulus@samba.org>
6e750a8f 39 * Jaswinder Singh Rajput <jaswinder@kernel.org>
5242519b
IM
40 *
41 * Released under the GPL v2. (and only v2, not any later version)
ddcacfa0
IM
42 */
43
1a482f38 44#include "perf.h"
16f762a2 45#include "builtin.h"
f14d5707 46#include "util/cgroup.h"
148be2c1 47#include "util/util.h"
5242519b
IM
48#include "util/parse-options.h"
49#include "util/parse-events.h"
4cabc3d1 50#include "util/pmu.h"
8f28827a 51#include "util/event.h"
361c99a6 52#include "util/evlist.h"
69aad6f1 53#include "util/evsel.h"
8f28827a 54#include "util/debug.h"
a5d243d0 55#include "util/color.h"
0007ecea 56#include "util/stat.h"
60666c63 57#include "util/header.h"
a12b51c4 58#include "util/cpumap.h"
d6d901c2 59#include "util/thread.h"
fd78260b 60#include "util/thread_map.h"
ddcacfa0 61
1f16c575 62#include <stdlib.h>
ddcacfa0 63#include <sys/prctl.h>
5af52b51 64#include <locale.h>
16c8a109 65
d7470b6a 66#define DEFAULT_SEPARATOR " "
2cee77c4
DA
67#define CNTR_NOT_SUPPORTED "<not supported>"
68#define CNTR_NOT_COUNTED "<not counted>"
d7470b6a 69
13370a9b
SE
70static void print_stat(int argc, const char **argv);
71static void print_counter_aggr(struct perf_evsel *counter, char *prefix);
72static void print_counter(struct perf_evsel *counter, char *prefix);
86ee6e18 73static void print_aggr(char *prefix);
13370a9b 74
4cabc3d1 75/* Default events used for perf stat -T */
a454742c
JO
76static const char *transaction_attrs = {
77 "task-clock,"
4cabc3d1
AK
78 "{"
79 "instructions,"
80 "cycles,"
81 "cpu/cycles-t/,"
82 "cpu/tx-start/,"
83 "cpu/el-start/,"
84 "cpu/cycles-ct/"
85 "}"
86};
87
88/* More limited version when the CPU does not have all events. */
a454742c
JO
89static const char * transaction_limited_attrs = {
90 "task-clock,"
4cabc3d1
AK
91 "{"
92 "instructions,"
93 "cycles,"
94 "cpu/cycles-t/,"
95 "cpu/tx-start/"
96 "}"
97};
98
666e6d48 99static struct perf_evlist *evsel_list;
361c99a6 100
602ad878 101static struct target target = {
77a6f014
NK
102 .uid = UINT_MAX,
103};
ddcacfa0 104
3d632595 105static int run_count = 1;
2e6cdf99 106static bool no_inherit = false;
c0555642 107static bool scale = true;
86ee6e18 108static enum aggr_mode aggr_mode = AGGR_GLOBAL;
d07f0b12 109static volatile pid_t child_pid = -1;
c0555642 110static bool null_run = false;
2cba3ffb 111static int detailed_run = 0;
4cabc3d1 112static bool transaction_run;
201e0b06 113static bool big_num = true;
d7470b6a 114static int big_num_opt = -1;
d7470b6a
SE
115static const char *csv_sep = NULL;
116static bool csv_output = false;
43bece79 117static bool group = false;
4aa9015f 118static FILE *output = NULL;
1f16c575
PZ
119static const char *pre_cmd = NULL;
120static const char *post_cmd = NULL;
121static bool sync_run = false;
13370a9b 122static unsigned int interval = 0;
41191688 123static unsigned int initial_delay = 0;
410136f5 124static unsigned int unit_width = 4; /* strlen("unit") */
a7e191c3 125static bool forever = false;
13370a9b 126static struct timespec ref_time;
86ee6e18
SE
127static struct cpu_map *aggr_map;
128static int (*aggr_get_id)(struct cpu_map *m, int cpu);
5af52b51 129
60666c63
LW
130static volatile int done = 0;
131
13370a9b
SE
132static inline void diff_timespec(struct timespec *r, struct timespec *a,
133 struct timespec *b)
134{
135 r->tv_sec = a->tv_sec - b->tv_sec;
136 if (a->tv_nsec < b->tv_nsec) {
137 r->tv_nsec = a->tv_nsec + 1000000000L - b->tv_nsec;
138 r->tv_sec--;
139 } else {
140 r->tv_nsec = a->tv_nsec - b->tv_nsec ;
141 }
142}
143
a7e191c3
FD
144static void perf_evsel__reset_stat_priv(struct perf_evsel *evsel)
145{
90f6bb6c
AK
146 int i;
147 struct perf_stat *ps = evsel->priv;
148
149 for (i = 0; i < 3; i++)
150 init_stats(&ps->res_stats[i]);
e2f56da1
JO
151
152 perf_stat_evsel_id_init(evsel);
a7e191c3
FD
153}
154
c52b12ed 155static int perf_evsel__alloc_stat_priv(struct perf_evsel *evsel)
69aad6f1 156{
c52b12ed 157 evsel->priv = zalloc(sizeof(struct perf_stat));
d180ac14 158 if (evsel->priv == NULL)
90f6bb6c
AK
159 return -ENOMEM;
160 perf_evsel__reset_stat_priv(evsel);
161 return 0;
69aad6f1
ACM
162}
163
164static void perf_evsel__free_stat_priv(struct perf_evsel *evsel)
165{
04662523 166 zfree(&evsel->priv);
69aad6f1
ACM
167}
168
13370a9b 169static int perf_evsel__alloc_prev_raw_counts(struct perf_evsel *evsel)
7ae92e74 170{
9df38e82 171 struct perf_counts *counts;
13370a9b 172
9df38e82
JO
173 counts = perf_counts__new(perf_evsel__nr_cpus(evsel));
174 if (counts)
175 evsel->prev_raw_counts = counts;
13370a9b 176
9df38e82 177 return counts ? 0 : -ENOMEM;
7ae92e74
YZ
178}
179
13370a9b 180static void perf_evsel__free_prev_raw_counts(struct perf_evsel *evsel)
7ae92e74 181{
9df38e82
JO
182 perf_counts__delete(evsel->prev_raw_counts);
183 evsel->prev_raw_counts = NULL;
7ae92e74
YZ
184}
185
d134ffb9
ACM
186static void perf_evlist__free_stats(struct perf_evlist *evlist)
187{
188 struct perf_evsel *evsel;
189
0050f7aa 190 evlist__for_each(evlist, evsel) {
d134ffb9
ACM
191 perf_evsel__free_stat_priv(evsel);
192 perf_evsel__free_counts(evsel);
193 perf_evsel__free_prev_raw_counts(evsel);
194 }
195}
196
197static int perf_evlist__alloc_stats(struct perf_evlist *evlist, bool alloc_raw)
198{
199 struct perf_evsel *evsel;
200
0050f7aa 201 evlist__for_each(evlist, evsel) {
d134ffb9
ACM
202 if (perf_evsel__alloc_stat_priv(evsel) < 0 ||
203 perf_evsel__alloc_counts(evsel, perf_evsel__nr_cpus(evsel)) < 0 ||
204 (alloc_raw && perf_evsel__alloc_prev_raw_counts(evsel) < 0))
205 goto out_free;
206 }
207
208 return 0;
209
210out_free:
211 perf_evlist__free_stats(evlist);
212 return -1;
213}
214
1eda3b21
JO
215static void perf_stat__reset_stats(struct perf_evlist *evlist)
216{
217 struct perf_evsel *evsel;
218
219 evlist__for_each(evlist, evsel) {
220 perf_evsel__reset_stat_priv(evsel);
a8e02324 221 perf_evsel__reset_counts(evsel);
1eda3b21
JO
222 }
223
f87027b9 224 perf_stat__reset_shadow_stats();
1eda3b21
JO
225}
226
cac21425 227static int create_perf_stat_counter(struct perf_evsel *evsel)
ddcacfa0 228{
69aad6f1 229 struct perf_event_attr *attr = &evsel->attr;
727ab04e 230
ddcacfa0 231 if (scale)
a21ca2ca
IM
232 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
233 PERF_FORMAT_TOTAL_TIME_RUNNING;
ddcacfa0 234
5d2cd909
ACM
235 attr->inherit = !no_inherit;
236
602ad878 237 if (target__has_cpu(&target))
594ac61a 238 return perf_evsel__open_per_cpu(evsel, perf_evsel__cpus(evsel));
5622c07b 239
602ad878 240 if (!target__has_task(&target) && perf_evsel__is_group_leader(evsel)) {
48290609 241 attr->disabled = 1;
41191688
AK
242 if (!initial_delay)
243 attr->enable_on_exec = 1;
ddcacfa0 244 }
084ab9f8 245
594ac61a 246 return perf_evsel__open_per_thread(evsel, evsel_list->threads);
ddcacfa0
IM
247}
248
c04f5e5d
IM
249/*
250 * Does the counter have nsecs as a unit?
251 */
daec78a0 252static inline int nsec_counter(struct perf_evsel *evsel)
c04f5e5d 253{
daec78a0
ACM
254 if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) ||
255 perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
c04f5e5d
IM
256 return 1;
257
258 return 0;
259}
260
779d0b99
JO
261static void zero_per_pkg(struct perf_evsel *counter)
262{
263 if (counter->per_pkg_mask)
264 memset(counter->per_pkg_mask, 0, MAX_NR_CPUS);
265}
266
267static int check_per_pkg(struct perf_evsel *counter, int cpu, bool *skip)
268{
269 unsigned long *mask = counter->per_pkg_mask;
270 struct cpu_map *cpus = perf_evsel__cpus(counter);
271 int s;
272
273 *skip = false;
274
275 if (!counter->per_pkg)
276 return 0;
277
278 if (cpu_map__empty(cpus))
279 return 0;
280
281 if (!mask) {
282 mask = zalloc(MAX_NR_CPUS);
283 if (!mask)
284 return -ENOMEM;
285
286 counter->per_pkg_mask = mask;
287 }
288
289 s = cpu_map__get_socket(cpus, cpu);
290 if (s < 0)
291 return -1;
292
293 *skip = test_and_set_bit(s, mask) == 1;
294 return 0;
295}
296
060c4f9c
JO
297static int read_cb(struct perf_evsel *evsel, int cpu, int thread __maybe_unused,
298 struct perf_counts_values *count)
299{
1971f59f 300 struct perf_counts_values *aggr = &evsel->counts->aggr;
779d0b99
JO
301 static struct perf_counts_values zero;
302 bool skip = false;
303
304 if (check_per_pkg(evsel, cpu, &skip)) {
305 pr_err("failed to read per-pkg counter\n");
306 return -1;
307 }
308
309 if (skip)
310 count = &zero;
1971f59f 311
060c4f9c
JO
312 switch (aggr_mode) {
313 case AGGR_CORE:
314 case AGGR_SOCKET:
315 case AGGR_NONE:
6c0345b7
JO
316 if (!evsel->snapshot)
317 perf_evsel__compute_deltas(evsel, cpu, count);
060c4f9c 318 perf_counts_values__scale(count, scale, NULL);
1ac77e1c 319 *perf_counts(evsel->counts, cpu) = *count;
56f0fd45 320 if (aggr_mode == AGGR_NONE)
f87027b9 321 perf_stat__update_shadow_stats(evsel, count->values, cpu);
060c4f9c
JO
322 break;
323 case AGGR_GLOBAL:
1971f59f
JO
324 aggr->val += count->val;
325 if (scale) {
326 aggr->ena += count->ena;
327 aggr->run += count->run;
328 }
060c4f9c
JO
329 default:
330 break;
331 }
332
333 return 0;
334}
335
1971f59f
JO
336static int read_counter(struct perf_evsel *counter);
337
c04f5e5d 338/*
2996f5dd 339 * Read out the results of a single counter:
f5b4a9c3 340 * aggregate counts across CPUs in system-wide mode
c04f5e5d 341 */
c52b12ed 342static int read_counter_aggr(struct perf_evsel *counter)
c04f5e5d 343{
1971f59f 344 struct perf_counts_values *aggr = &counter->counts->aggr;
69aad6f1 345 struct perf_stat *ps = counter->priv;
c52b12ed
ACM
346 u64 *count = counter->counts->aggr.values;
347 int i;
2996f5dd 348
1971f59f
JO
349 aggr->val = aggr->ena = aggr->run = 0;
350
351 if (read_counter(counter))
c52b12ed 352 return -1;
9e9772c4 353
6c0345b7
JO
354 if (!counter->snapshot)
355 perf_evsel__compute_deltas(counter, -1, aggr);
1971f59f
JO
356 perf_counts_values__scale(aggr, scale, &counter->counts->scaled);
357
9e9772c4 358 for (i = 0; i < 3; i++)
69aad6f1 359 update_stats(&ps->res_stats[i], count[i]);
9e9772c4
PZ
360
361 if (verbose) {
4aa9015f 362 fprintf(output, "%s: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
7289f83c 363 perf_evsel__name(counter), count[0], count[1], count[2]);
9e9772c4
PZ
364 }
365
be1ac0d8
IM
366 /*
367 * Save the full runtime - to allow normalization during printout:
368 */
f87027b9 369 perf_stat__update_shadow_stats(counter, count, 0);
c52b12ed
ACM
370
371 return 0;
f5b4a9c3
SE
372}
373
374/*
375 * Read out the results of a single counter:
376 * do not aggregate counts across CPUs in system-wide mode
377 */
c52b12ed 378static int read_counter(struct perf_evsel *counter)
f5b4a9c3 379{
9bf1a529
JO
380 int nthreads = thread_map__nr(evsel_list->threads);
381 int ncpus = perf_evsel__nr_cpus(counter);
382 int cpu, thread;
f5b4a9c3 383
3b4331d9
SP
384 if (!counter->supported)
385 return -ENOENT;
386
9bf1a529
JO
387 if (counter->system_wide)
388 nthreads = 1;
389
779d0b99
JO
390 if (counter->per_pkg)
391 zero_per_pkg(counter);
392
9bf1a529
JO
393 for (thread = 0; thread < nthreads; thread++) {
394 for (cpu = 0; cpu < ncpus; cpu++) {
395 if (perf_evsel__read_cb(counter, cpu, thread, read_cb))
396 return -1;
397 }
f5b4a9c3 398 }
c52b12ed
ACM
399
400 return 0;
2996f5dd
IM
401}
402
13370a9b
SE
403static void print_interval(void)
404{
405 static int num_print_interval;
406 struct perf_evsel *counter;
407 struct perf_stat *ps;
408 struct timespec ts, rs;
409 char prefix[64];
410
86ee6e18 411 if (aggr_mode == AGGR_GLOBAL) {
0050f7aa 412 evlist__for_each(evsel_list, counter) {
13370a9b
SE
413 ps = counter->priv;
414 memset(ps->res_stats, 0, sizeof(ps->res_stats));
86ee6e18 415 read_counter_aggr(counter);
13370a9b 416 }
86ee6e18 417 } else {
0050f7aa 418 evlist__for_each(evsel_list, counter) {
13370a9b
SE
419 ps = counter->priv;
420 memset(ps->res_stats, 0, sizeof(ps->res_stats));
86ee6e18 421 read_counter(counter);
13370a9b
SE
422 }
423 }
86ee6e18 424
13370a9b
SE
425 clock_gettime(CLOCK_MONOTONIC, &ts);
426 diff_timespec(&rs, &ts, &ref_time);
427 sprintf(prefix, "%6lu.%09lu%s", rs.tv_sec, rs.tv_nsec, csv_sep);
428
429 if (num_print_interval == 0 && !csv_output) {
86ee6e18
SE
430 switch (aggr_mode) {
431 case AGGR_SOCKET:
410136f5 432 fprintf(output, "# time socket cpus counts %*s events\n", unit_width, "unit");
86ee6e18 433 break;
12c08a9f 434 case AGGR_CORE:
410136f5 435 fprintf(output, "# time core cpus counts %*s events\n", unit_width, "unit");
12c08a9f 436 break;
86ee6e18 437 case AGGR_NONE:
410136f5 438 fprintf(output, "# time CPU counts %*s events\n", unit_width, "unit");
86ee6e18
SE
439 break;
440 case AGGR_GLOBAL:
441 default:
410136f5 442 fprintf(output, "# time counts %*s events\n", unit_width, "unit");
86ee6e18 443 }
13370a9b
SE
444 }
445
446 if (++num_print_interval == 25)
447 num_print_interval = 0;
448
86ee6e18 449 switch (aggr_mode) {
12c08a9f 450 case AGGR_CORE:
86ee6e18
SE
451 case AGGR_SOCKET:
452 print_aggr(prefix);
453 break;
454 case AGGR_NONE:
0050f7aa 455 evlist__for_each(evsel_list, counter)
13370a9b 456 print_counter(counter, prefix);
86ee6e18
SE
457 break;
458 case AGGR_GLOBAL:
459 default:
0050f7aa 460 evlist__for_each(evsel_list, counter)
13370a9b
SE
461 print_counter_aggr(counter, prefix);
462 }
2bbf03f1
AK
463
464 fflush(output);
13370a9b
SE
465}
466
41191688
AK
467static void handle_initial_delay(void)
468{
469 struct perf_evsel *counter;
470
471 if (initial_delay) {
472 const int ncpus = cpu_map__nr(evsel_list->cpus),
473 nthreads = thread_map__nr(evsel_list->threads);
474
475 usleep(initial_delay * 1000);
0050f7aa 476 evlist__for_each(evsel_list, counter)
41191688
AK
477 perf_evsel__enable(counter, ncpus, nthreads);
478 }
479}
480
f33cbe72 481static volatile int workload_exec_errno;
6af206fd
ACM
482
483/*
484 * perf_evlist__prepare_workload will send a SIGUSR1
485 * if the fork fails, since we asked by setting its
486 * want_signal to true.
487 */
f33cbe72
ACM
488static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info,
489 void *ucontext __maybe_unused)
6af206fd 490{
f33cbe72 491 workload_exec_errno = info->si_value.sival_int;
6af206fd
ACM
492}
493
acf28922 494static int __run_perf_stat(int argc, const char **argv)
42202dd5 495{
56e52e85 496 char msg[512];
42202dd5 497 unsigned long long t0, t1;
cac21425 498 struct perf_evsel *counter;
13370a9b 499 struct timespec ts;
410136f5 500 size_t l;
42202dd5 501 int status = 0;
6be2850e 502 const bool forks = (argc > 0);
42202dd5 503
13370a9b
SE
504 if (interval) {
505 ts.tv_sec = interval / 1000;
506 ts.tv_nsec = (interval % 1000) * 1000000;
507 } else {
508 ts.tv_sec = 1;
509 ts.tv_nsec = 0;
510 }
511
60666c63 512 if (forks) {
735f7e0b
ACM
513 if (perf_evlist__prepare_workload(evsel_list, &target, argv, false,
514 workload_exec_failed_signal) < 0) {
acf28922
NK
515 perror("failed to prepare workload");
516 return -1;
60666c63 517 }
d20a47e7 518 child_pid = evsel_list->workload.pid;
051ae7f7
PM
519 }
520
6a4bb04c 521 if (group)
63dab225 522 perf_evlist__set_leader(evsel_list);
6a4bb04c 523
0050f7aa 524 evlist__for_each(evsel_list, counter) {
cac21425 525 if (create_perf_stat_counter(counter) < 0) {
979987a5
DA
526 /*
527 * PPC returns ENXIO for HW counters until 2.6.37
528 * (behavior changed with commit b0a873e).
529 */
38f6ae1e 530 if (errno == EINVAL || errno == ENOSYS ||
979987a5
DA
531 errno == ENOENT || errno == EOPNOTSUPP ||
532 errno == ENXIO) {
c63ca0c0
DA
533 if (verbose)
534 ui__warning("%s event is not supported by the kernel.\n",
7289f83c 535 perf_evsel__name(counter));
2cee77c4 536 counter->supported = false;
cb5ef600
KL
537
538 if ((counter->leader != counter) ||
539 !(counter->leader->nr_members > 1))
540 continue;
c63ca0c0 541 }
ede70290 542
56e52e85
ACM
543 perf_evsel__open_strerror(counter, &target,
544 errno, msg, sizeof(msg));
545 ui__error("%s\n", msg);
546
48290609
ACM
547 if (child_pid != -1)
548 kill(child_pid, SIGTERM);
fceda7fe 549
48290609
ACM
550 return -1;
551 }
2cee77c4 552 counter->supported = true;
410136f5
SE
553
554 l = strlen(counter->unit);
555 if (l > unit_width)
556 unit_width = l;
084ab9f8 557 }
42202dd5 558
23d4aad4
ACM
559 if (perf_evlist__apply_filters(evsel_list, &counter)) {
560 error("failed to set filter \"%s\" on event %s with %d (%s)\n",
561 counter->filter, perf_evsel__name(counter), errno,
759e612b 562 strerror_r(errno, msg, sizeof(msg)));
cfd748ae
FW
563 return -1;
564 }
565
42202dd5
IM
566 /*
567 * Enable counters and exec the command:
568 */
569 t0 = rdclock();
13370a9b 570 clock_gettime(CLOCK_MONOTONIC, &ref_time);
42202dd5 571
60666c63 572 if (forks) {
acf28922 573 perf_evlist__start_workload(evsel_list);
41191688 574 handle_initial_delay();
acf28922 575
13370a9b
SE
576 if (interval) {
577 while (!waitpid(child_pid, &status, WNOHANG)) {
578 nanosleep(&ts, NULL);
579 print_interval();
580 }
581 }
60666c63 582 wait(&status);
6af206fd 583
f33cbe72
ACM
584 if (workload_exec_errno) {
585 const char *emsg = strerror_r(workload_exec_errno, msg, sizeof(msg));
586 pr_err("Workload failed: %s\n", emsg);
6af206fd 587 return -1;
f33cbe72 588 }
6af206fd 589
33e49ea7
AK
590 if (WIFSIGNALED(status))
591 psignal(WTERMSIG(status), argv[0]);
60666c63 592 } else {
41191688 593 handle_initial_delay();
13370a9b
SE
594 while (!done) {
595 nanosleep(&ts, NULL);
596 if (interval)
597 print_interval();
598 }
60666c63 599 }
42202dd5 600
42202dd5
IM
601 t1 = rdclock();
602
9e9772c4 603 update_stats(&walltime_nsecs_stats, t1 - t0);
42202dd5 604
86ee6e18 605 if (aggr_mode == AGGR_GLOBAL) {
0050f7aa 606 evlist__for_each(evsel_list, counter) {
f5b4a9c3 607 read_counter_aggr(counter);
7ae92e74 608 perf_evsel__close_fd(counter, perf_evsel__nr_cpus(counter),
b3a319d5 609 thread_map__nr(evsel_list->threads));
c52b12ed 610 }
86ee6e18 611 } else {
0050f7aa 612 evlist__for_each(evsel_list, counter) {
86ee6e18
SE
613 read_counter(counter);
614 perf_evsel__close_fd(counter, perf_evsel__nr_cpus(counter), 1);
615 }
f5b4a9c3 616 }
c52b12ed 617
42202dd5
IM
618 return WEXITSTATUS(status);
619}
620
41cde476 621static int run_perf_stat(int argc, const char **argv)
1f16c575
PZ
622{
623 int ret;
624
625 if (pre_cmd) {
626 ret = system(pre_cmd);
627 if (ret)
628 return ret;
629 }
630
631 if (sync_run)
632 sync();
633
634 ret = __run_perf_stat(argc, argv);
635 if (ret)
636 return ret;
637
638 if (post_cmd) {
639 ret = system(post_cmd);
640 if (ret)
641 return ret;
642 }
643
644 return ret;
645}
646
d73515c0
AK
647static void print_running(u64 run, u64 ena)
648{
649 if (csv_output) {
650 fprintf(output, "%s%" PRIu64 "%s%.2f",
651 csv_sep,
652 run,
653 csv_sep,
654 ena ? 100.0 * run / ena : 100.0);
655 } else if (run != ena) {
656 fprintf(output, " (%.2f%%)", 100.0 * run / ena);
657 }
658}
659
f99844cb
IM
660static void print_noise_pct(double total, double avg)
661{
0007ecea 662 double pct = rel_stddev_stats(total, avg);
f99844cb 663
3ae9a34d 664 if (csv_output)
4aa9015f 665 fprintf(output, "%s%.2f%%", csv_sep, pct);
a1bca6cc 666 else if (pct)
4aa9015f 667 fprintf(output, " ( +-%6.2f%% )", pct);
f99844cb
IM
668}
669
69aad6f1 670static void print_noise(struct perf_evsel *evsel, double avg)
42202dd5 671{
69aad6f1
ACM
672 struct perf_stat *ps;
673
849abde9
PZ
674 if (run_count == 1)
675 return;
676
69aad6f1 677 ps = evsel->priv;
f99844cb 678 print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
42202dd5
IM
679}
680
12c08a9f 681static void aggr_printout(struct perf_evsel *evsel, int id, int nr)
44175b6f 682{
86ee6e18 683 switch (aggr_mode) {
12c08a9f
SE
684 case AGGR_CORE:
685 fprintf(output, "S%d-C%*d%s%*d%s",
686 cpu_map__id_to_socket(id),
687 csv_output ? 0 : -8,
688 cpu_map__id_to_cpu(id),
689 csv_sep,
690 csv_output ? 0 : 4,
691 nr,
692 csv_sep);
693 break;
86ee6e18
SE
694 case AGGR_SOCKET:
695 fprintf(output, "S%*d%s%*d%s",
d7e7a451 696 csv_output ? 0 : -5,
12c08a9f 697 id,
d7e7a451
SE
698 csv_sep,
699 csv_output ? 0 : 4,
700 nr,
701 csv_sep);
86ee6e18
SE
702 break;
703 case AGGR_NONE:
704 fprintf(output, "CPU%*d%s",
d7470b6a 705 csv_output ? 0 : -4,
12c08a9f 706 perf_evsel__cpus(evsel)->map[id], csv_sep);
86ee6e18
SE
707 break;
708 case AGGR_GLOBAL:
709 default:
710 break;
711 }
712}
713
da88c7f7 714static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg)
86ee6e18
SE
715{
716 double msecs = avg / 1e6;
410136f5 717 const char *fmt_v, *fmt_n;
4bbe5a61 718 char name[25];
86ee6e18 719
410136f5
SE
720 fmt_v = csv_output ? "%.6f%s" : "%18.6f%s";
721 fmt_n = csv_output ? "%s" : "%-25s";
722
da88c7f7 723 aggr_printout(evsel, id, nr);
d7470b6a 724
4bbe5a61
DA
725 scnprintf(name, sizeof(name), "%s%s",
726 perf_evsel__name(evsel), csv_output ? "" : " (msec)");
410136f5
SE
727
728 fprintf(output, fmt_v, msecs, csv_sep);
729
730 if (csv_output)
731 fprintf(output, "%s%s", evsel->unit, csv_sep);
732 else
733 fprintf(output, "%-*s%s", unit_width, evsel->unit, csv_sep);
734
735 fprintf(output, fmt_n, name);
d7470b6a 736
023695d9 737 if (evsel->cgrp)
4aa9015f 738 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
023695d9 739
13370a9b 740 if (csv_output || interval)
d7470b6a 741 return;
44175b6f 742
daec78a0 743 if (perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
4aa9015f
SE
744 fprintf(output, " # %8.3f CPUs utilized ",
745 avg / avg_stats(&walltime_nsecs_stats));
9dac6a29
NK
746 else
747 fprintf(output, " ");
44175b6f
IM
748}
749
556b1fb7
JO
750static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg)
751{
752 double sc = evsel->scale;
753 const char *fmt;
754 int cpu = cpu_map__id_to_cpu(id);
755
756 if (csv_output) {
757 fmt = sc != 1.0 ? "%.2f%s" : "%.0f%s";
758 } else {
759 if (big_num)
760 fmt = sc != 1.0 ? "%'18.2f%s" : "%'18.0f%s";
761 else
762 fmt = sc != 1.0 ? "%18.2f%s" : "%18.0f%s";
763 }
764
765 aggr_printout(evsel, id, nr);
766
767 if (aggr_mode == AGGR_GLOBAL)
768 cpu = 0;
769
770 fprintf(output, fmt, avg, csv_sep);
771
772 if (evsel->unit)
773 fprintf(output, "%-*s%s",
774 csv_output ? 0 : unit_width,
775 evsel->unit, csv_sep);
776
777 fprintf(output, "%-*s", csv_output ? 0 : 25, perf_evsel__name(evsel));
778
779 if (evsel->cgrp)
780 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
781
782 if (csv_output || interval)
783 return;
784
f87027b9 785 perf_stat__print_shadow_stats(output, evsel, avg, cpu, aggr_mode);
556b1fb7
JO
786}
787
86ee6e18 788static void print_aggr(char *prefix)
d7e7a451
SE
789{
790 struct perf_evsel *counter;
582ec082 791 int cpu, cpu2, s, s2, id, nr;
410136f5 792 double uval;
d7e7a451 793 u64 ena, run, val;
d7e7a451 794
86ee6e18 795 if (!(aggr_map || aggr_get_id))
d7e7a451
SE
796 return;
797
86ee6e18
SE
798 for (s = 0; s < aggr_map->nr; s++) {
799 id = aggr_map->map[s];
0050f7aa 800 evlist__for_each(evsel_list, counter) {
d7e7a451
SE
801 val = ena = run = 0;
802 nr = 0;
803 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
582ec082
SE
804 cpu2 = perf_evsel__cpus(counter)->map[cpu];
805 s2 = aggr_get_id(evsel_list->cpus, cpu2);
86ee6e18 806 if (s2 != id)
d7e7a451 807 continue;
1ac77e1c
JO
808 val += perf_counts(counter->counts, cpu)->val;
809 ena += perf_counts(counter->counts, cpu)->ena;
810 run += perf_counts(counter->counts, cpu)->run;
d7e7a451
SE
811 nr++;
812 }
813 if (prefix)
814 fprintf(output, "%s", prefix);
815
816 if (run == 0 || ena == 0) {
582ec082 817 aggr_printout(counter, id, nr);
86ee6e18 818
410136f5 819 fprintf(output, "%*s%s",
d7e7a451
SE
820 csv_output ? 0 : 18,
821 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
410136f5
SE
822 csv_sep);
823
824 fprintf(output, "%-*s%s",
825 csv_output ? 0 : unit_width,
826 counter->unit, csv_sep);
827
828 fprintf(output, "%*s",
829 csv_output ? 0 : -25,
d7e7a451 830 perf_evsel__name(counter));
86ee6e18 831
d7e7a451
SE
832 if (counter->cgrp)
833 fprintf(output, "%s%s",
834 csv_sep, counter->cgrp->name);
835
d73515c0 836 print_running(run, ena);
d7e7a451
SE
837 fputc('\n', output);
838 continue;
839 }
410136f5 840 uval = val * counter->scale;
d7e7a451
SE
841
842 if (nsec_counter(counter))
410136f5 843 nsec_printout(id, nr, counter, uval);
d7e7a451 844 else
410136f5 845 abs_printout(id, nr, counter, uval);
d7e7a451 846
d73515c0 847 if (!csv_output)
d7e7a451
SE
848 print_noise(counter, 1.0);
849
d73515c0 850 print_running(run, ena);
d7e7a451
SE
851 fputc('\n', output);
852 }
853 }
854}
855
2996f5dd
IM
856/*
857 * Print out the results of a single counter:
f5b4a9c3 858 * aggregated counts in system-wide mode
2996f5dd 859 */
13370a9b 860static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
2996f5dd 861{
69aad6f1
ACM
862 struct perf_stat *ps = counter->priv;
863 double avg = avg_stats(&ps->res_stats[0]);
c52b12ed 864 int scaled = counter->counts->scaled;
410136f5 865 double uval;
d73515c0
AK
866 double avg_enabled, avg_running;
867
868 avg_enabled = avg_stats(&ps->res_stats[1]);
869 avg_running = avg_stats(&ps->res_stats[2]);
2996f5dd 870
13370a9b
SE
871 if (prefix)
872 fprintf(output, "%s", prefix);
873
3b4331d9 874 if (scaled == -1 || !counter->supported) {
410136f5 875 fprintf(output, "%*s%s",
d7470b6a 876 csv_output ? 0 : 18,
2cee77c4 877 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
410136f5
SE
878 csv_sep);
879 fprintf(output, "%-*s%s",
880 csv_output ? 0 : unit_width,
881 counter->unit, csv_sep);
882 fprintf(output, "%*s",
883 csv_output ? 0 : -25,
7289f83c 884 perf_evsel__name(counter));
023695d9
SE
885
886 if (counter->cgrp)
4aa9015f 887 fprintf(output, "%s%s", csv_sep, counter->cgrp->name);
023695d9 888
d73515c0 889 print_running(avg_running, avg_enabled);
4aa9015f 890 fputc('\n', output);
2996f5dd
IM
891 return;
892 }
c04f5e5d 893
410136f5
SE
894 uval = avg * counter->scale;
895
44175b6f 896 if (nsec_counter(counter))
410136f5 897 nsec_printout(-1, 0, counter, uval);
44175b6f 898 else
410136f5 899 abs_printout(-1, 0, counter, uval);
849abde9 900
3ae9a34d
ZH
901 print_noise(counter, avg);
902
d73515c0 903 print_running(avg_running, avg_enabled);
4aa9015f 904 fprintf(output, "\n");
c04f5e5d
IM
905}
906
f5b4a9c3
SE
907/*
908 * Print out the results of a single counter:
909 * does not use aggregated count in system-wide
910 */
13370a9b 911static void print_counter(struct perf_evsel *counter, char *prefix)
f5b4a9c3
SE
912{
913 u64 ena, run, val;
410136f5 914 double uval;
f5b4a9c3
SE
915 int cpu;
916
7ae92e74 917 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1ac77e1c
JO
918 val = perf_counts(counter->counts, cpu)->val;
919 ena = perf_counts(counter->counts, cpu)->ena;
920 run = perf_counts(counter->counts, cpu)->run;
13370a9b
SE
921
922 if (prefix)
923 fprintf(output, "%s", prefix);
924
f5b4a9c3 925 if (run == 0 || ena == 0) {
410136f5 926 fprintf(output, "CPU%*d%s%*s%s",
d7470b6a 927 csv_output ? 0 : -4,
7ae92e74 928 perf_evsel__cpus(counter)->map[cpu], csv_sep,
d7470b6a 929 csv_output ? 0 : 18,
2cee77c4 930 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
410136f5
SE
931 csv_sep);
932
933 fprintf(output, "%-*s%s",
934 csv_output ? 0 : unit_width,
935 counter->unit, csv_sep);
936
937 fprintf(output, "%*s",
938 csv_output ? 0 : -25,
939 perf_evsel__name(counter));
f5b4a9c3 940
023695d9 941 if (counter->cgrp)
4aa9015f
SE
942 fprintf(output, "%s%s",
943 csv_sep, counter->cgrp->name);
023695d9 944
d73515c0 945 print_running(run, ena);
4aa9015f 946 fputc('\n', output);
f5b4a9c3
SE
947 continue;
948 }
949
410136f5
SE
950 uval = val * counter->scale;
951
f5b4a9c3 952 if (nsec_counter(counter))
410136f5 953 nsec_printout(cpu, 0, counter, uval);
f5b4a9c3 954 else
410136f5 955 abs_printout(cpu, 0, counter, uval);
f5b4a9c3 956
d73515c0 957 if (!csv_output)
d7470b6a 958 print_noise(counter, 1.0);
d73515c0 959 print_running(run, ena);
f5b4a9c3 960
4aa9015f 961 fputc('\n', output);
f5b4a9c3
SE
962 }
963}
964
42202dd5
IM
965static void print_stat(int argc, const char **argv)
966{
69aad6f1
ACM
967 struct perf_evsel *counter;
968 int i;
42202dd5 969
ddcacfa0
IM
970 fflush(stdout);
971
d7470b6a 972 if (!csv_output) {
4aa9015f
SE
973 fprintf(output, "\n");
974 fprintf(output, " Performance counter stats for ");
62d3b617
DA
975 if (target.system_wide)
976 fprintf(output, "\'system wide");
977 else if (target.cpu_list)
978 fprintf(output, "\'CPU(s) %s", target.cpu_list);
602ad878 979 else if (!target__has_task(&target)) {
4aa9015f 980 fprintf(output, "\'%s", argv[0]);
d7470b6a 981 for (i = 1; i < argc; i++)
4aa9015f 982 fprintf(output, " %s", argv[i]);
20f946b4
NK
983 } else if (target.pid)
984 fprintf(output, "process id \'%s", target.pid);
d7470b6a 985 else
20f946b4 986 fprintf(output, "thread id \'%s", target.tid);
44db76c8 987
4aa9015f 988 fprintf(output, "\'");
d7470b6a 989 if (run_count > 1)
4aa9015f
SE
990 fprintf(output, " (%d runs)", run_count);
991 fprintf(output, ":\n\n");
d7470b6a 992 }
2996f5dd 993
86ee6e18 994 switch (aggr_mode) {
12c08a9f 995 case AGGR_CORE:
86ee6e18
SE
996 case AGGR_SOCKET:
997 print_aggr(NULL);
998 break;
999 case AGGR_GLOBAL:
0050f7aa 1000 evlist__for_each(evsel_list, counter)
13370a9b 1001 print_counter_aggr(counter, NULL);
86ee6e18
SE
1002 break;
1003 case AGGR_NONE:
0050f7aa 1004 evlist__for_each(evsel_list, counter)
86ee6e18
SE
1005 print_counter(counter, NULL);
1006 break;
1007 default:
1008 break;
f5b4a9c3 1009 }
ddcacfa0 1010
d7470b6a 1011 if (!csv_output) {
c3305257 1012 if (!null_run)
4aa9015f
SE
1013 fprintf(output, "\n");
1014 fprintf(output, " %17.9f seconds time elapsed",
d7470b6a
SE
1015 avg_stats(&walltime_nsecs_stats)/1e9);
1016 if (run_count > 1) {
4aa9015f 1017 fprintf(output, " ");
f99844cb
IM
1018 print_noise_pct(stddev_stats(&walltime_nsecs_stats),
1019 avg_stats(&walltime_nsecs_stats));
d7470b6a 1020 }
4aa9015f 1021 fprintf(output, "\n\n");
566747e6 1022 }
ddcacfa0
IM
1023}
1024
f7b7c26e
PZ
1025static volatile int signr = -1;
1026
5242519b 1027static void skip_signal(int signo)
ddcacfa0 1028{
13370a9b 1029 if ((child_pid == -1) || interval)
60666c63
LW
1030 done = 1;
1031
f7b7c26e 1032 signr = signo;
d07f0b12
SE
1033 /*
1034 * render child_pid harmless
1035 * won't send SIGTERM to a random
1036 * process in case of race condition
1037 * and fast PID recycling
1038 */
1039 child_pid = -1;
f7b7c26e
PZ
1040}
1041
1042static void sig_atexit(void)
1043{
d07f0b12
SE
1044 sigset_t set, oset;
1045
1046 /*
1047 * avoid race condition with SIGCHLD handler
1048 * in skip_signal() which is modifying child_pid
1049 * goal is to avoid send SIGTERM to a random
1050 * process
1051 */
1052 sigemptyset(&set);
1053 sigaddset(&set, SIGCHLD);
1054 sigprocmask(SIG_BLOCK, &set, &oset);
1055
933da83a
CW
1056 if (child_pid != -1)
1057 kill(child_pid, SIGTERM);
1058
d07f0b12
SE
1059 sigprocmask(SIG_SETMASK, &oset, NULL);
1060
f7b7c26e
PZ
1061 if (signr == -1)
1062 return;
1063
1064 signal(signr, SIG_DFL);
1065 kill(getpid(), signr);
5242519b
IM
1066}
1067
1d037ca1
IT
1068static int stat__set_big_num(const struct option *opt __maybe_unused,
1069 const char *s __maybe_unused, int unset)
d7470b6a
SE
1070{
1071 big_num_opt = unset ? 0 : 1;
1072 return 0;
1073}
1074
86ee6e18
SE
1075static int perf_stat_init_aggr_mode(void)
1076{
1077 switch (aggr_mode) {
1078 case AGGR_SOCKET:
1079 if (cpu_map__build_socket_map(evsel_list->cpus, &aggr_map)) {
1080 perror("cannot build socket map");
1081 return -1;
1082 }
1083 aggr_get_id = cpu_map__get_socket;
1084 break;
12c08a9f
SE
1085 case AGGR_CORE:
1086 if (cpu_map__build_core_map(evsel_list->cpus, &aggr_map)) {
1087 perror("cannot build core map");
1088 return -1;
1089 }
1090 aggr_get_id = cpu_map__get_core;
1091 break;
86ee6e18
SE
1092 case AGGR_NONE:
1093 case AGGR_GLOBAL:
1094 default:
1095 break;
1096 }
1097 return 0;
1098}
1099
2cba3ffb
IM
1100/*
1101 * Add default attributes, if there were no attributes specified or
1102 * if -d/--detailed, -d -d or -d -d -d is used:
1103 */
1104static int add_default_attributes(void)
1105{
b070a547
ACM
1106 struct perf_event_attr default_attrs[] = {
1107
1108 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
1109 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
1110 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
1111 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
1112
1113 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
1114 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
1115 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
1116 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
1117 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
1118 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
1119
1120};
1121
1122/*
1123 * Detailed stats (-d), covering the L1 and last level data caches:
1124 */
1125 struct perf_event_attr detailed_attrs[] = {
1126
1127 { .type = PERF_TYPE_HW_CACHE,
1128 .config =
1129 PERF_COUNT_HW_CACHE_L1D << 0 |
1130 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1131 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1132
1133 { .type = PERF_TYPE_HW_CACHE,
1134 .config =
1135 PERF_COUNT_HW_CACHE_L1D << 0 |
1136 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1137 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1138
1139 { .type = PERF_TYPE_HW_CACHE,
1140 .config =
1141 PERF_COUNT_HW_CACHE_LL << 0 |
1142 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1143 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1144
1145 { .type = PERF_TYPE_HW_CACHE,
1146 .config =
1147 PERF_COUNT_HW_CACHE_LL << 0 |
1148 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1149 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1150};
1151
1152/*
1153 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
1154 */
1155 struct perf_event_attr very_detailed_attrs[] = {
1156
1157 { .type = PERF_TYPE_HW_CACHE,
1158 .config =
1159 PERF_COUNT_HW_CACHE_L1I << 0 |
1160 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1161 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1162
1163 { .type = PERF_TYPE_HW_CACHE,
1164 .config =
1165 PERF_COUNT_HW_CACHE_L1I << 0 |
1166 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1167 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1168
1169 { .type = PERF_TYPE_HW_CACHE,
1170 .config =
1171 PERF_COUNT_HW_CACHE_DTLB << 0 |
1172 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1173 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1174
1175 { .type = PERF_TYPE_HW_CACHE,
1176 .config =
1177 PERF_COUNT_HW_CACHE_DTLB << 0 |
1178 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1179 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1180
1181 { .type = PERF_TYPE_HW_CACHE,
1182 .config =
1183 PERF_COUNT_HW_CACHE_ITLB << 0 |
1184 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1185 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1186
1187 { .type = PERF_TYPE_HW_CACHE,
1188 .config =
1189 PERF_COUNT_HW_CACHE_ITLB << 0 |
1190 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1191 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1192
1193};
1194
1195/*
1196 * Very, very detailed stats (-d -d -d), adding prefetch events:
1197 */
1198 struct perf_event_attr very_very_detailed_attrs[] = {
1199
1200 { .type = PERF_TYPE_HW_CACHE,
1201 .config =
1202 PERF_COUNT_HW_CACHE_L1D << 0 |
1203 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
1204 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1205
1206 { .type = PERF_TYPE_HW_CACHE,
1207 .config =
1208 PERF_COUNT_HW_CACHE_L1D << 0 |
1209 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
1210 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1211};
1212
2cba3ffb
IM
1213 /* Set attrs if no event is selected and !null_run: */
1214 if (null_run)
1215 return 0;
1216
4cabc3d1
AK
1217 if (transaction_run) {
1218 int err;
1219 if (pmu_have_event("cpu", "cycles-ct") &&
1220 pmu_have_event("cpu", "el-start"))
a454742c 1221 err = parse_events(evsel_list, transaction_attrs, NULL);
4cabc3d1 1222 else
a454742c
JO
1223 err = parse_events(evsel_list, transaction_limited_attrs, NULL);
1224 if (err) {
4cabc3d1
AK
1225 fprintf(stderr, "Cannot set up transaction events\n");
1226 return -1;
1227 }
1228 return 0;
1229 }
1230
2cba3ffb 1231 if (!evsel_list->nr_entries) {
79695e1b 1232 if (perf_evlist__add_default_attrs(evsel_list, default_attrs) < 0)
50d08e47 1233 return -1;
2cba3ffb
IM
1234 }
1235
1236 /* Detailed events get appended to the event list: */
1237
1238 if (detailed_run < 1)
1239 return 0;
1240
1241 /* Append detailed run extra attributes: */
79695e1b 1242 if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
50d08e47 1243 return -1;
2cba3ffb
IM
1244
1245 if (detailed_run < 2)
1246 return 0;
1247
1248 /* Append very detailed run extra attributes: */
79695e1b 1249 if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
50d08e47 1250 return -1;
2cba3ffb
IM
1251
1252 if (detailed_run < 3)
1253 return 0;
1254
1255 /* Append very, very detailed run extra attributes: */
79695e1b 1256 return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
2cba3ffb
IM
1257}
1258
1d037ca1 1259int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
5242519b 1260{
1f16c575 1261 bool append_file = false;
b070a547
ACM
1262 int output_fd = 0;
1263 const char *output_name = NULL;
1264 const struct option options[] = {
4cabc3d1
AK
1265 OPT_BOOLEAN('T', "transaction", &transaction_run,
1266 "hardware transaction statistics"),
b070a547
ACM
1267 OPT_CALLBACK('e', "event", &evsel_list, "event",
1268 "event selector. use 'perf list' to list available events",
1269 parse_events_option),
1270 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
1271 "event filter", parse_filter),
1272 OPT_BOOLEAN('i', "no-inherit", &no_inherit,
1273 "child tasks do not inherit counters"),
1274 OPT_STRING('p', "pid", &target.pid, "pid",
1275 "stat events on existing process id"),
1276 OPT_STRING('t', "tid", &target.tid, "tid",
1277 "stat events on existing thread id"),
1278 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
1279 "system-wide collection from all CPUs"),
1280 OPT_BOOLEAN('g', "group", &group,
1281 "put the counters into a counter group"),
1282 OPT_BOOLEAN('c', "scale", &scale, "scale/normalize counters"),
1283 OPT_INCR('v', "verbose", &verbose,
1284 "be more verbose (show counter open errors, etc)"),
1285 OPT_INTEGER('r', "repeat", &run_count,
a7e191c3 1286 "repeat command and print average + stddev (max: 100, forever: 0)"),
b070a547
ACM
1287 OPT_BOOLEAN('n', "null", &null_run,
1288 "null run - dont start any counters"),
1289 OPT_INCR('d', "detailed", &detailed_run,
1290 "detailed run - start a lot of events"),
1291 OPT_BOOLEAN('S', "sync", &sync_run,
1292 "call sync() before starting a run"),
48000a1a 1293 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
b070a547
ACM
1294 "print large numbers with thousands\' separators",
1295 stat__set_big_num),
1296 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
1297 "list of cpus to monitor in system-wide"),
86ee6e18
SE
1298 OPT_SET_UINT('A', "no-aggr", &aggr_mode,
1299 "disable CPU count aggregation", AGGR_NONE),
b070a547
ACM
1300 OPT_STRING('x', "field-separator", &csv_sep, "separator",
1301 "print counts with custom separator"),
1302 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
1303 "monitor event in cgroup name only", parse_cgroups),
1304 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1305 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
1306 OPT_INTEGER(0, "log-fd", &output_fd,
1307 "log output to fd, instead of stderr"),
1f16c575
PZ
1308 OPT_STRING(0, "pre", &pre_cmd, "command",
1309 "command to run prior to the measured command"),
1310 OPT_STRING(0, "post", &post_cmd, "command",
1311 "command to run after to the measured command"),
13370a9b
SE
1312 OPT_UINTEGER('I', "interval-print", &interval,
1313 "print counts at regular interval in ms (>= 100)"),
d4304958 1314 OPT_SET_UINT(0, "per-socket", &aggr_mode,
86ee6e18 1315 "aggregate counts per processor socket", AGGR_SOCKET),
12c08a9f
SE
1316 OPT_SET_UINT(0, "per-core", &aggr_mode,
1317 "aggregate counts per physical processor core", AGGR_CORE),
41191688
AK
1318 OPT_UINTEGER('D', "delay", &initial_delay,
1319 "ms to wait before starting measurement after program start"),
b070a547
ACM
1320 OPT_END()
1321 };
1322 const char * const stat_usage[] = {
1323 "perf stat [<options>] [<command>]",
1324 NULL
1325 };
cc03c542 1326 int status = -EINVAL, run_idx;
4aa9015f 1327 const char *mode;
42202dd5 1328
5af52b51
SE
1329 setlocale(LC_ALL, "");
1330
334fe7a3 1331 evsel_list = perf_evlist__new();
361c99a6
ACM
1332 if (evsel_list == NULL)
1333 return -ENOMEM;
1334
a0541234
AB
1335 argc = parse_options(argc, argv, options, stat_usage,
1336 PARSE_OPT_STOP_AT_NON_OPTION);
d7470b6a 1337
4aa9015f
SE
1338 output = stderr;
1339 if (output_name && strcmp(output_name, "-"))
1340 output = NULL;
1341
56f3bae7
JC
1342 if (output_name && output_fd) {
1343 fprintf(stderr, "cannot use both --output and --log-fd\n");
cc03c542
NK
1344 parse_options_usage(stat_usage, options, "o", 1);
1345 parse_options_usage(NULL, options, "log-fd", 0);
1346 goto out;
56f3bae7 1347 }
fc3e4d07
SE
1348
1349 if (output_fd < 0) {
1350 fprintf(stderr, "argument to --log-fd must be a > 0\n");
cc03c542
NK
1351 parse_options_usage(stat_usage, options, "log-fd", 0);
1352 goto out;
fc3e4d07
SE
1353 }
1354
4aa9015f
SE
1355 if (!output) {
1356 struct timespec tm;
1357 mode = append_file ? "a" : "w";
1358
1359 output = fopen(output_name, mode);
1360 if (!output) {
1361 perror("failed to create output file");
fceda7fe 1362 return -1;
4aa9015f
SE
1363 }
1364 clock_gettime(CLOCK_REALTIME, &tm);
1365 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
fc3e4d07 1366 } else if (output_fd > 0) {
56f3bae7
JC
1367 mode = append_file ? "a" : "w";
1368 output = fdopen(output_fd, mode);
1369 if (!output) {
1370 perror("Failed opening logfd");
1371 return -errno;
1372 }
4aa9015f
SE
1373 }
1374
d4ffd04d 1375 if (csv_sep) {
d7470b6a 1376 csv_output = true;
d4ffd04d
JC
1377 if (!strcmp(csv_sep, "\\t"))
1378 csv_sep = "\t";
1379 } else
d7470b6a
SE
1380 csv_sep = DEFAULT_SEPARATOR;
1381
1382 /*
1383 * let the spreadsheet do the pretty-printing
1384 */
1385 if (csv_output) {
61a9f324 1386 /* User explicitly passed -B? */
d7470b6a
SE
1387 if (big_num_opt == 1) {
1388 fprintf(stderr, "-B option not supported with -x\n");
cc03c542
NK
1389 parse_options_usage(stat_usage, options, "B", 1);
1390 parse_options_usage(NULL, options, "x", 1);
1391 goto out;
d7470b6a
SE
1392 } else /* Nope, so disable big number formatting */
1393 big_num = false;
1394 } else if (big_num_opt == 0) /* User passed --no-big-num */
1395 big_num = false;
1396
602ad878 1397 if (!argc && target__none(&target))
5242519b 1398 usage_with_options(stat_usage, options);
ac3063bd 1399
a7e191c3 1400 if (run_count < 0) {
cc03c542
NK
1401 pr_err("Run count must be a positive number\n");
1402 parse_options_usage(stat_usage, options, "r", 1);
1403 goto out;
a7e191c3
FD
1404 } else if (run_count == 0) {
1405 forever = true;
1406 run_count = 1;
1407 }
ddcacfa0 1408
023695d9 1409 /* no_aggr, cgroup are for system-wide only */
602ad878
ACM
1410 if ((aggr_mode != AGGR_GLOBAL || nr_cgroups) &&
1411 !target__has_cpu(&target)) {
023695d9
SE
1412 fprintf(stderr, "both cgroup and no-aggregation "
1413 "modes only available in system-wide mode\n");
1414
cc03c542
NK
1415 parse_options_usage(stat_usage, options, "G", 1);
1416 parse_options_usage(NULL, options, "A", 1);
1417 parse_options_usage(NULL, options, "a", 1);
1418 goto out;
d7e7a451
SE
1419 }
1420
2cba3ffb
IM
1421 if (add_default_attributes())
1422 goto out;
ddcacfa0 1423
602ad878 1424 target__validate(&target);
5c98d466 1425
77a6f014 1426 if (perf_evlist__create_maps(evsel_list, &target) < 0) {
602ad878 1427 if (target__has_task(&target)) {
77a6f014 1428 pr_err("Problems finding threads of monitor\n");
cc03c542
NK
1429 parse_options_usage(stat_usage, options, "p", 1);
1430 parse_options_usage(NULL, options, "t", 1);
602ad878 1431 } else if (target__has_cpu(&target)) {
77a6f014 1432 perror("failed to parse CPUs map");
cc03c542
NK
1433 parse_options_usage(stat_usage, options, "C", 1);
1434 parse_options_usage(NULL, options, "a", 1);
1435 }
1436 goto out;
60d567e2 1437 }
13370a9b
SE
1438 if (interval && interval < 100) {
1439 pr_err("print interval must be >= 100ms\n");
cc03c542 1440 parse_options_usage(stat_usage, options, "I", 1);
03ad9747 1441 goto out;
13370a9b 1442 }
c45c6ea2 1443
d134ffb9 1444 if (perf_evlist__alloc_stats(evsel_list, interval))
03ad9747 1445 goto out;
d6d901c2 1446
86ee6e18 1447 if (perf_stat_init_aggr_mode())
03ad9747 1448 goto out;
86ee6e18 1449
58d7e993
IM
1450 /*
1451 * We dont want to block the signals - that would cause
1452 * child tasks to inherit that and Ctrl-C would not work.
1453 * What we want is for Ctrl-C to work in the exec()-ed
1454 * task, but being ignored by perf stat itself:
1455 */
f7b7c26e 1456 atexit(sig_atexit);
a7e191c3
FD
1457 if (!forever)
1458 signal(SIGINT, skip_signal);
13370a9b 1459 signal(SIGCHLD, skip_signal);
58d7e993
IM
1460 signal(SIGALRM, skip_signal);
1461 signal(SIGABRT, skip_signal);
1462
42202dd5 1463 status = 0;
a7e191c3 1464 for (run_idx = 0; forever || run_idx < run_count; run_idx++) {
42202dd5 1465 if (run_count != 1 && verbose)
4aa9015f
SE
1466 fprintf(output, "[ perf stat: executing run #%d ... ]\n",
1467 run_idx + 1);
f9cef0a9 1468
42202dd5 1469 status = run_perf_stat(argc, argv);
a7e191c3
FD
1470 if (forever && status != -1) {
1471 print_stat(argc, argv);
d134ffb9 1472 perf_stat__reset_stats(evsel_list);
a7e191c3 1473 }
42202dd5
IM
1474 }
1475
a7e191c3 1476 if (!forever && status != -1 && !interval)
084ab9f8 1477 print_stat(argc, argv);
d134ffb9
ACM
1478
1479 perf_evlist__free_stats(evsel_list);
0015e2e1
ACM
1480out:
1481 perf_evlist__delete(evsel_list);
42202dd5 1482 return status;
ddcacfa0 1483}