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