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