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