]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - tools/perf/builtin-stat.c
KVM: SVM: Move spec control call after restore of GS
[mirror_ubuntu-artful-kernel.git] / tools / perf / builtin-stat.c
CommitLineData
ddcacfa0 1/*
bf9e1876
IM
2 * builtin-stat.c
3 *
4 * Builtin stat command: Give a precise performance counters summary
5 * overview about any workload, CPU or specific PID.
6 *
7 * Sample output:
ddcacfa0 8
2cba3ffb 9 $ perf stat ./hackbench 10
ddcacfa0 10
2cba3ffb 11 Time: 0.118
ddcacfa0 12
2cba3ffb 13 Performance counter stats for './hackbench 10':
ddcacfa0 14
2cba3ffb
IM
15 1708.761321 task-clock # 11.037 CPUs utilized
16 41,190 context-switches # 0.024 M/sec
17 6,735 CPU-migrations # 0.004 M/sec
18 17,318 page-faults # 0.010 M/sec
19 5,205,202,243 cycles # 3.046 GHz
20 3,856,436,920 stalled-cycles-frontend # 74.09% frontend cycles idle
21 1,600,790,871 stalled-cycles-backend # 30.75% backend cycles idle
22 2,603,501,247 instructions # 0.50 insns per cycle
23 # 1.48 stalled cycles per insn
24 484,357,498 branches # 283.455 M/sec
25 6,388,934 branch-misses # 1.32% of all branches
26
27 0.154822978 seconds time elapsed
ddcacfa0 28
5242519b 29 *
2cba3ffb 30 * Copyright (C) 2008-2011, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
5242519b
IM
31 *
32 * Improvements and fixes by:
33 *
34 * Arjan van de Ven <arjan@linux.intel.com>
35 * Yanmin Zhang <yanmin.zhang@intel.com>
36 * Wu Fengguang <fengguang.wu@intel.com>
37 * Mike Galbraith <efault@gmx.de>
38 * Paul Mackerras <paulus@samba.org>
6e750a8f 39 * Jaswinder Singh Rajput <jaswinder@kernel.org>
5242519b
IM
40 *
41 * Released under the GPL v2. (and only v2, not any later version)
ddcacfa0
IM
42 */
43
1a482f38 44#include "perf.h"
16f762a2 45#include "builtin.h"
f14d5707 46#include "util/cgroup.h"
148be2c1 47#include "util/util.h"
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"
5d8bb1ec 55#include "util/drv_configs.h"
a5d243d0 56#include "util/color.h"
0007ecea 57#include "util/stat.h"
60666c63 58#include "util/header.h"
a12b51c4 59#include "util/cpumap.h"
d6d901c2 60#include "util/thread.h"
fd78260b 61#include "util/thread_map.h"
d809560b 62#include "util/counts.h"
44b1e60a 63#include "util/group.h"
4979d0c7 64#include "util/session.h"
ba6039b6 65#include "util/tool.h"
44b1e60a 66#include "util/group.h"
a067558e 67#include "util/string2.h"
ba6039b6 68#include "asm/bug.h"
ddcacfa0 69
bd48c63e 70#include <linux/time64.h>
44b1e60a 71#include <api/fs/fs.h>
a43783ae 72#include <errno.h>
9607ad3a 73#include <signal.h>
1f16c575 74#include <stdlib.h>
ddcacfa0 75#include <sys/prctl.h>
fd20e811 76#include <inttypes.h>
5af52b51 77#include <locale.h>
e3b03b6c 78#include <math.h>
7a8ef4c4
ACM
79#include <sys/types.h>
80#include <sys/stat.h>
4208735d 81#include <sys/wait.h>
7a8ef4c4 82#include <unistd.h>
16c8a109 83
3d689ed6
ACM
84#include "sane_ctype.h"
85
d7470b6a 86#define DEFAULT_SEPARATOR " "
2cee77c4
DA
87#define CNTR_NOT_SUPPORTED "<not supported>"
88#define CNTR_NOT_COUNTED "<not counted>"
daefd0bc 89#define FREEZE_ON_SMI_PATH "devices/cpu/freeze_on_smi"
d7470b6a 90
d4f63a47 91static void print_counters(struct timespec *ts, int argc, const char **argv);
13370a9b 92
4cabc3d1 93/* Default events used for perf stat -T */
a454742c
JO
94static const char *transaction_attrs = {
95 "task-clock,"
4cabc3d1
AK
96 "{"
97 "instructions,"
98 "cycles,"
99 "cpu/cycles-t/,"
100 "cpu/tx-start/,"
101 "cpu/el-start/,"
102 "cpu/cycles-ct/"
103 "}"
104};
105
106/* More limited version when the CPU does not have all events. */
a454742c
JO
107static const char * transaction_limited_attrs = {
108 "task-clock,"
4cabc3d1
AK
109 "{"
110 "instructions,"
111 "cycles,"
112 "cpu/cycles-t/,"
113 "cpu/tx-start/"
114 "}"
115};
116
44b1e60a
AK
117static const char * topdown_attrs[] = {
118 "topdown-total-slots",
119 "topdown-slots-retired",
120 "topdown-recovery-bubbles",
121 "topdown-fetch-bubbles",
122 "topdown-slots-issued",
123 NULL,
124};
125
daefd0bc
KL
126static const char *smi_cost_attrs = {
127 "{"
128 "msr/aperf/,"
129 "msr/smi/,"
130 "cycles"
131 "}"
132};
133
666e6d48 134static struct perf_evlist *evsel_list;
361c99a6 135
602ad878 136static struct target target = {
77a6f014
NK
137 .uid = UINT_MAX,
138};
ddcacfa0 139
1e5a2931
JO
140typedef int (*aggr_get_id_t)(struct cpu_map *m, int cpu);
141
3d632595 142static int run_count = 1;
2e6cdf99 143static bool no_inherit = false;
d07f0b12 144static volatile pid_t child_pid = -1;
c0555642 145static bool null_run = false;
2cba3ffb 146static int detailed_run = 0;
4cabc3d1 147static bool transaction_run;
44b1e60a 148static bool topdown_run = false;
daefd0bc
KL
149static bool smi_cost = false;
150static bool smi_reset = false;
201e0b06 151static bool big_num = true;
d7470b6a 152static int big_num_opt = -1;
d7470b6a
SE
153static const char *csv_sep = NULL;
154static bool csv_output = false;
43bece79 155static bool group = false;
1f16c575
PZ
156static const char *pre_cmd = NULL;
157static const char *post_cmd = NULL;
158static bool sync_run = false;
41191688 159static unsigned int initial_delay = 0;
410136f5 160static unsigned int unit_width = 4; /* strlen("unit") */
a7e191c3 161static bool forever = false;
54b50916 162static bool metric_only = false;
44b1e60a 163static bool force_metric_only = false;
430daf2d 164static bool no_merge = false;
13370a9b 165static struct timespec ref_time;
86ee6e18 166static struct cpu_map *aggr_map;
1e5a2931 167static aggr_get_id_t aggr_get_id;
e0547311
JO
168static bool append_file;
169static const char *output_name;
170static int output_fd;
02d492e5 171static int print_free_counters_hint;
5af52b51 172
4979d0c7
JO
173struct perf_stat {
174 bool record;
175 struct perf_data_file file;
176 struct perf_session *session;
177 u64 bytes_written;
ba6039b6 178 struct perf_tool tool;
1975d36e
JO
179 bool maps_allocated;
180 struct cpu_map *cpus;
181 struct thread_map *threads;
89af4e05 182 enum aggr_mode aggr_mode;
4979d0c7
JO
183};
184
185static struct perf_stat perf_stat;
186#define STAT_RECORD perf_stat.record
187
60666c63
LW
188static volatile int done = 0;
189
421a50f3
JO
190static struct perf_stat_config stat_config = {
191 .aggr_mode = AGGR_GLOBAL,
711a572e 192 .scale = true,
421a50f3
JO
193};
194
13370a9b
SE
195static inline void diff_timespec(struct timespec *r, struct timespec *a,
196 struct timespec *b)
197{
198 r->tv_sec = a->tv_sec - b->tv_sec;
199 if (a->tv_nsec < b->tv_nsec) {
310ebb93 200 r->tv_nsec = a->tv_nsec + NSEC_PER_SEC - b->tv_nsec;
13370a9b
SE
201 r->tv_sec--;
202 } else {
203 r->tv_nsec = a->tv_nsec - b->tv_nsec ;
204 }
205}
206
254ecbc7
JO
207static void perf_stat__reset_stats(void)
208{
209 perf_evlist__reset_stats(evsel_list);
f87027b9 210 perf_stat__reset_shadow_stats();
1eda3b21
JO
211}
212
cac21425 213static int create_perf_stat_counter(struct perf_evsel *evsel)
ddcacfa0 214{
69aad6f1 215 struct perf_event_attr *attr = &evsel->attr;
727ab04e 216
711a572e 217 if (stat_config.scale)
a21ca2ca
IM
218 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
219 PERF_FORMAT_TOTAL_TIME_RUNNING;
ddcacfa0 220
5d2cd909
ACM
221 attr->inherit = !no_inherit;
222
6acd8e92
JO
223 /*
224 * Some events get initialized with sample_(period/type) set,
225 * like tracepoints. Clear it up for counting.
226 */
227 attr->sample_period = 0;
6db1a5c1 228
4979d0c7
JO
229 /*
230 * But set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
231 * while avoiding that older tools show confusing messages.
6db1a5c1
JO
232 *
233 * However for pipe sessions we need to keep it zero,
234 * because script's perf_evsel__check_attr is triggered
235 * by attr->sample_type != 0, and we can't run it on
236 * stat sessions.
4979d0c7 237 */
6db1a5c1
JO
238 if (!(STAT_RECORD && perf_stat.file.is_pipe))
239 attr->sample_type = PERF_SAMPLE_IDENTIFIER;
6acd8e92 240
67ccdecd
JO
241 /*
242 * Disabling all counters initially, they will be enabled
243 * either manually by us or by kernel via enable_on_exec
244 * set later.
245 */
c8280cec 246 if (perf_evsel__is_group_leader(evsel)) {
67ccdecd
JO
247 attr->disabled = 1;
248
c8280cec
JO
249 /*
250 * In case of initial_delay we enable tracee
251 * events manually.
252 */
253 if (target__none(&target) && !initial_delay)
41191688 254 attr->enable_on_exec = 1;
ddcacfa0 255 }
084ab9f8 256
c8280cec
JO
257 if (target__has_cpu(&target))
258 return perf_evsel__open_per_cpu(evsel, perf_evsel__cpus(evsel));
259
594ac61a 260 return perf_evsel__open_per_thread(evsel, evsel_list->threads);
ddcacfa0
IM
261}
262
c04f5e5d
IM
263/*
264 * Does the counter have nsecs as a unit?
265 */
daec78a0 266static inline int nsec_counter(struct perf_evsel *evsel)
c04f5e5d 267{
daec78a0
ACM
268 if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) ||
269 perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
c04f5e5d
IM
270 return 1;
271
272 return 0;
273}
274
8b99b1a4
JO
275static int process_synthesized_event(struct perf_tool *tool __maybe_unused,
276 union perf_event *event,
277 struct perf_sample *sample __maybe_unused,
278 struct machine *machine __maybe_unused)
4979d0c7 279{
8b99b1a4 280 if (perf_data_file__write(&perf_stat.file, event, event->header.size) < 0) {
4979d0c7
JO
281 pr_err("failed to write perf data, error: %m\n");
282 return -1;
283 }
284
8b99b1a4 285 perf_stat.bytes_written += event->header.size;
4979d0c7
JO
286 return 0;
287}
288
1975d36e 289static int write_stat_round_event(u64 tm, u64 type)
7aad0c32 290{
1975d36e 291 return perf_event__synthesize_stat_round(NULL, tm, type,
7aad0c32
JO
292 process_synthesized_event,
293 NULL);
294}
295
296#define WRITE_STAT_ROUND_EVENT(time, interval) \
297 write_stat_round_event(time, PERF_STAT_ROUND_TYPE__ ## interval)
298
5a6ea81b
JO
299#define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
300
301static int
302perf_evsel__write_stat_event(struct perf_evsel *counter, u32 cpu, u32 thread,
303 struct perf_counts_values *count)
304{
305 struct perf_sample_id *sid = SID(counter, cpu, thread);
306
307 return perf_event__synthesize_stat(NULL, cpu, thread, sid->id, count,
308 process_synthesized_event, NULL);
309}
310
f5b4a9c3
SE
311/*
312 * Read out the results of a single counter:
313 * do not aggregate counts across CPUs in system-wide mode
314 */
c52b12ed 315static int read_counter(struct perf_evsel *counter)
f5b4a9c3 316{
9bf1a529 317 int nthreads = thread_map__nr(evsel_list->threads);
00e727bb
MR
318 int ncpus, cpu, thread;
319
320 if (target__has_cpu(&target))
321 ncpus = perf_evsel__nr_cpus(counter);
322 else
323 ncpus = 1;
f5b4a9c3 324
3b4331d9
SP
325 if (!counter->supported)
326 return -ENOENT;
327
9bf1a529
JO
328 if (counter->system_wide)
329 nthreads = 1;
330
331 for (thread = 0; thread < nthreads; thread++) {
332 for (cpu = 0; cpu < ncpus; cpu++) {
3b3eb044
JO
333 struct perf_counts_values *count;
334
335 count = perf_counts(counter->counts, cpu, thread);
db49a717
SE
336 if (perf_evsel__read(counter, cpu, thread, count)) {
337 counter->counts->scaled = -1;
338 perf_counts(counter->counts, cpu, thread)->ena = 0;
339 perf_counts(counter->counts, cpu, thread)->run = 0;
9bf1a529 340 return -1;
db49a717 341 }
5a6ea81b
JO
342
343 if (STAT_RECORD) {
344 if (perf_evsel__write_stat_event(counter, cpu, thread, count)) {
345 pr_err("failed to write stat event\n");
346 return -1;
347 }
348 }
0b1abbf4
AK
349
350 if (verbose > 1) {
351 fprintf(stat_config.output,
352 "%s: %d: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
353 perf_evsel__name(counter),
354 cpu,
355 count->val, count->ena, count->run);
356 }
9bf1a529 357 }
f5b4a9c3 358 }
c52b12ed
ACM
359
360 return 0;
2996f5dd
IM
361}
362
3df33eff 363static void read_counters(void)
13370a9b 364{
13370a9b 365 struct perf_evsel *counter;
db49a717 366 int ret;
13370a9b 367
e5cadb93 368 evlist__for_each_entry(evsel_list, counter) {
db49a717
SE
369 ret = read_counter(counter);
370 if (ret)
245bad8e 371 pr_debug("failed to read counter %s\n", counter->name);
3b3eb044 372
db49a717 373 if (ret == 0 && perf_stat_process_counter(&stat_config, counter))
3b3eb044 374 pr_warning("failed to process counter %s\n", counter->name);
13370a9b 375 }
106a94a0
JO
376}
377
ba411a95 378static void process_interval(void)
106a94a0 379{
106a94a0 380 struct timespec ts, rs;
106a94a0 381
3df33eff 382 read_counters();
86ee6e18 383
13370a9b
SE
384 clock_gettime(CLOCK_MONOTONIC, &ts);
385 diff_timespec(&rs, &ts, &ref_time);
13370a9b 386
7aad0c32 387 if (STAT_RECORD) {
bd48c63e 388 if (WRITE_STAT_ROUND_EVENT(rs.tv_sec * NSEC_PER_SEC + rs.tv_nsec, INTERVAL))
7aad0c32
JO
389 pr_err("failed to write stat round event\n");
390 }
391
d4f63a47 392 print_counters(&rs, 0, NULL);
13370a9b
SE
393}
394
67ccdecd 395static void enable_counters(void)
41191688 396{
67ccdecd 397 if (initial_delay)
310ebb93 398 usleep(initial_delay * USEC_PER_MSEC);
67ccdecd
JO
399
400 /*
401 * We need to enable counters only if:
402 * - we don't have tracee (attaching to task or cpu)
403 * - we have initial delay configured
404 */
405 if (!target__none(&target) || initial_delay)
ab46db0a 406 perf_evlist__enable(evsel_list);
41191688
AK
407}
408
3df33eff
MR
409static void disable_counters(void)
410{
411 /*
412 * If we don't have tracee (attaching to task or cpu), counters may
413 * still be running. To get accurate group ratios, we must stop groups
414 * from counting before reading their constituent counters.
415 */
416 if (!target__none(&target))
417 perf_evlist__disable(evsel_list);
418}
419
f33cbe72 420static volatile int workload_exec_errno;
6af206fd
ACM
421
422/*
423 * perf_evlist__prepare_workload will send a SIGUSR1
424 * if the fork fails, since we asked by setting its
425 * want_signal to true.
426 */
f33cbe72
ACM
427static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info,
428 void *ucontext __maybe_unused)
6af206fd 429{
f33cbe72 430 workload_exec_errno = info->si_value.sival_int;
6af206fd
ACM
431}
432
7b60a7e3
JO
433static bool has_unit(struct perf_evsel *counter)
434{
435 return counter->unit && *counter->unit;
436}
437
438static bool has_scale(struct perf_evsel *counter)
439{
440 return counter->scale != 1;
441}
442
664c98d4 443static int perf_stat_synthesize_config(bool is_pipe)
8b99b1a4 444{
7b60a7e3 445 struct perf_evsel *counter;
8b99b1a4
JO
446 int err;
447
664c98d4
JO
448 if (is_pipe) {
449 err = perf_event__synthesize_attrs(NULL, perf_stat.session,
450 process_synthesized_event);
451 if (err < 0) {
452 pr_err("Couldn't synthesize attrs.\n");
453 return err;
454 }
455 }
456
7b60a7e3
JO
457 /*
458 * Synthesize other events stuff not carried within
459 * attr event - unit, scale, name
460 */
e5cadb93 461 evlist__for_each_entry(evsel_list, counter) {
7b60a7e3
JO
462 if (!counter->supported)
463 continue;
464
465 /*
466 * Synthesize unit and scale only if it's defined.
467 */
468 if (has_unit(counter)) {
469 err = perf_event__synthesize_event_update_unit(NULL, counter, process_synthesized_event);
470 if (err < 0) {
471 pr_err("Couldn't synthesize evsel unit.\n");
472 return err;
473 }
474 }
475
476 if (has_scale(counter)) {
477 err = perf_event__synthesize_event_update_scale(NULL, counter, process_synthesized_event);
478 if (err < 0) {
479 pr_err("Couldn't synthesize evsel scale.\n");
480 return err;
481 }
482 }
483
484 if (counter->own_cpus) {
485 err = perf_event__synthesize_event_update_cpus(NULL, counter, process_synthesized_event);
486 if (err < 0) {
487 pr_err("Couldn't synthesize evsel scale.\n");
488 return err;
489 }
490 }
491
492 /*
493 * Name is needed only for pipe output,
494 * perf.data carries event names.
495 */
496 if (is_pipe) {
497 err = perf_event__synthesize_event_update_name(NULL, counter, process_synthesized_event);
498 if (err < 0) {
499 pr_err("Couldn't synthesize evsel name.\n");
500 return err;
501 }
502 }
503 }
504
8b99b1a4
JO
505 err = perf_event__synthesize_thread_map2(NULL, evsel_list->threads,
506 process_synthesized_event,
507 NULL);
508 if (err < 0) {
509 pr_err("Couldn't synthesize thread map.\n");
510 return err;
511 }
512
513 err = perf_event__synthesize_cpu_map(NULL, evsel_list->cpus,
514 process_synthesized_event, NULL);
515 if (err < 0) {
516 pr_err("Couldn't synthesize thread map.\n");
517 return err;
518 }
519
520 err = perf_event__synthesize_stat_config(NULL, &stat_config,
521 process_synthesized_event, NULL);
522 if (err < 0) {
523 pr_err("Couldn't synthesize config.\n");
524 return err;
525 }
526
527 return 0;
528}
529
2af4646d
JO
530#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
531
532static int __store_counter_ids(struct perf_evsel *counter,
533 struct cpu_map *cpus,
534 struct thread_map *threads)
535{
536 int cpu, thread;
537
538 for (cpu = 0; cpu < cpus->nr; cpu++) {
539 for (thread = 0; thread < threads->nr; thread++) {
540 int fd = FD(counter, cpu, thread);
541
542 if (perf_evlist__id_add_fd(evsel_list, counter,
543 cpu, thread, fd) < 0)
544 return -1;
545 }
546 }
547
548 return 0;
549}
550
551static int store_counter_ids(struct perf_evsel *counter)
552{
553 struct cpu_map *cpus = counter->cpus;
554 struct thread_map *threads = counter->threads;
555
556 if (perf_evsel__alloc_id(counter, cpus->nr, threads->nr))
557 return -ENOMEM;
558
559 return __store_counter_ids(counter, cpus, threads);
560}
561
acf28922 562static int __run_perf_stat(int argc, const char **argv)
42202dd5 563{
ec0d3d1f 564 int interval = stat_config.interval;
d6195a6a 565 char msg[BUFSIZ];
42202dd5 566 unsigned long long t0, t1;
cac21425 567 struct perf_evsel *counter;
13370a9b 568 struct timespec ts;
410136f5 569 size_t l;
42202dd5 570 int status = 0;
6be2850e 571 const bool forks = (argc > 0);
664c98d4 572 bool is_pipe = STAT_RECORD ? perf_stat.file.is_pipe : false;
5d8bb1ec 573 struct perf_evsel_config_term *err_term;
42202dd5 574
13370a9b 575 if (interval) {
310ebb93
ACM
576 ts.tv_sec = interval / USEC_PER_MSEC;
577 ts.tv_nsec = (interval % USEC_PER_MSEC) * NSEC_PER_MSEC;
13370a9b
SE
578 } else {
579 ts.tv_sec = 1;
580 ts.tv_nsec = 0;
581 }
582
60666c63 583 if (forks) {
664c98d4 584 if (perf_evlist__prepare_workload(evsel_list, &target, argv, is_pipe,
735f7e0b 585 workload_exec_failed_signal) < 0) {
acf28922
NK
586 perror("failed to prepare workload");
587 return -1;
60666c63 588 }
d20a47e7 589 child_pid = evsel_list->workload.pid;
051ae7f7
PM
590 }
591
6a4bb04c 592 if (group)
63dab225 593 perf_evlist__set_leader(evsel_list);
6a4bb04c 594
e5cadb93 595 evlist__for_each_entry(evsel_list, counter) {
42ef8a78 596try_again:
cac21425 597 if (create_perf_stat_counter(counter) < 0) {
979987a5
DA
598 /*
599 * PPC returns ENXIO for HW counters until 2.6.37
600 * (behavior changed with commit b0a873e).
601 */
38f6ae1e 602 if (errno == EINVAL || errno == ENOSYS ||
979987a5
DA
603 errno == ENOENT || errno == EOPNOTSUPP ||
604 errno == ENXIO) {
bb963e16 605 if (verbose > 0)
c63ca0c0 606 ui__warning("%s event is not supported by the kernel.\n",
7289f83c 607 perf_evsel__name(counter));
2cee77c4 608 counter->supported = false;
cb5ef600
KL
609
610 if ((counter->leader != counter) ||
611 !(counter->leader->nr_members > 1))
612 continue;
42ef8a78 613 } else if (perf_evsel__fallback(counter, errno, msg, sizeof(msg))) {
bb963e16 614 if (verbose > 0)
42ef8a78
ACM
615 ui__warning("%s\n", msg);
616 goto try_again;
617 }
ede70290 618
56e52e85
ACM
619 perf_evsel__open_strerror(counter, &target,
620 errno, msg, sizeof(msg));
621 ui__error("%s\n", msg);
622
48290609
ACM
623 if (child_pid != -1)
624 kill(child_pid, SIGTERM);
fceda7fe 625
48290609
ACM
626 return -1;
627 }
2cee77c4 628 counter->supported = true;
410136f5
SE
629
630 l = strlen(counter->unit);
631 if (l > unit_width)
632 unit_width = l;
2af4646d
JO
633
634 if (STAT_RECORD && store_counter_ids(counter))
635 return -1;
084ab9f8 636 }
42202dd5 637
23d4aad4 638 if (perf_evlist__apply_filters(evsel_list, &counter)) {
62d94b00 639 pr_err("failed to set filter \"%s\" on event %s with %d (%s)\n",
23d4aad4 640 counter->filter, perf_evsel__name(counter), errno,
c8b5f2c9 641 str_error_r(errno, msg, sizeof(msg)));
cfd748ae
FW
642 return -1;
643 }
644
5d8bb1ec 645 if (perf_evlist__apply_drv_configs(evsel_list, &counter, &err_term)) {
62d94b00 646 pr_err("failed to set config \"%s\" on event %s with %d (%s)\n",
5d8bb1ec
MP
647 err_term->val.drv_cfg, perf_evsel__name(counter), errno,
648 str_error_r(errno, msg, sizeof(msg)));
649 return -1;
650 }
651
4979d0c7
JO
652 if (STAT_RECORD) {
653 int err, fd = perf_data_file__fd(&perf_stat.file);
654
664c98d4
JO
655 if (is_pipe) {
656 err = perf_header__write_pipe(perf_data_file__fd(&perf_stat.file));
657 } else {
658 err = perf_session__write_header(perf_stat.session, evsel_list,
659 fd, false);
660 }
661
4979d0c7
JO
662 if (err < 0)
663 return err;
8b99b1a4 664
664c98d4 665 err = perf_stat_synthesize_config(is_pipe);
8b99b1a4
JO
666 if (err < 0)
667 return err;
4979d0c7
JO
668 }
669
42202dd5
IM
670 /*
671 * Enable counters and exec the command:
672 */
673 t0 = rdclock();
13370a9b 674 clock_gettime(CLOCK_MONOTONIC, &ref_time);
42202dd5 675
60666c63 676 if (forks) {
acf28922 677 perf_evlist__start_workload(evsel_list);
67ccdecd 678 enable_counters();
acf28922 679
13370a9b
SE
680 if (interval) {
681 while (!waitpid(child_pid, &status, WNOHANG)) {
682 nanosleep(&ts, NULL);
ba411a95 683 process_interval();
13370a9b
SE
684 }
685 }
60666c63 686 wait(&status);
6af206fd 687
f33cbe72 688 if (workload_exec_errno) {
c8b5f2c9 689 const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
f33cbe72 690 pr_err("Workload failed: %s\n", emsg);
6af206fd 691 return -1;
f33cbe72 692 }
6af206fd 693
33e49ea7
AK
694 if (WIFSIGNALED(status))
695 psignal(WTERMSIG(status), argv[0]);
60666c63 696 } else {
67ccdecd 697 enable_counters();
13370a9b
SE
698 while (!done) {
699 nanosleep(&ts, NULL);
700 if (interval)
ba411a95 701 process_interval();
13370a9b 702 }
60666c63 703 }
42202dd5 704
3df33eff
MR
705 disable_counters();
706
42202dd5
IM
707 t1 = rdclock();
708
9e9772c4 709 update_stats(&walltime_nsecs_stats, t1 - t0);
42202dd5 710
3df33eff
MR
711 /*
712 * Closing a group leader splits the group, and as we only disable
713 * group leaders, results in remaining events becoming enabled. To
714 * avoid arbitrary skew, we must read all counters before closing any
715 * group leaders.
716 */
717 read_counters();
718 perf_evlist__close(evsel_list);
c52b12ed 719
42202dd5
IM
720 return WEXITSTATUS(status);
721}
722
41cde476 723static int run_perf_stat(int argc, const char **argv)
1f16c575
PZ
724{
725 int ret;
726
727 if (pre_cmd) {
728 ret = system(pre_cmd);
729 if (ret)
730 return ret;
731 }
732
733 if (sync_run)
734 sync();
735
736 ret = __run_perf_stat(argc, argv);
737 if (ret)
738 return ret;
739
740 if (post_cmd) {
741 ret = system(post_cmd);
742 if (ret)
743 return ret;
744 }
745
746 return ret;
747}
748
d73515c0
AK
749static void print_running(u64 run, u64 ena)
750{
751 if (csv_output) {
5821522e 752 fprintf(stat_config.output, "%s%" PRIu64 "%s%.2f",
d73515c0
AK
753 csv_sep,
754 run,
755 csv_sep,
756 ena ? 100.0 * run / ena : 100.0);
757 } else if (run != ena) {
5821522e 758 fprintf(stat_config.output, " (%.2f%%)", 100.0 * run / ena);
d73515c0
AK
759 }
760}
761
f99844cb
IM
762static void print_noise_pct(double total, double avg)
763{
0007ecea 764 double pct = rel_stddev_stats(total, avg);
f99844cb 765
3ae9a34d 766 if (csv_output)
5821522e 767 fprintf(stat_config.output, "%s%.2f%%", csv_sep, pct);
a1bca6cc 768 else if (pct)
5821522e 769 fprintf(stat_config.output, " ( +-%6.2f%% )", pct);
f99844cb
IM
770}
771
69aad6f1 772static void print_noise(struct perf_evsel *evsel, double avg)
42202dd5 773{
581cc8a2 774 struct perf_stat_evsel *ps;
69aad6f1 775
849abde9
PZ
776 if (run_count == 1)
777 return;
778
69aad6f1 779 ps = evsel->priv;
f99844cb 780 print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
42202dd5
IM
781}
782
12c08a9f 783static void aggr_printout(struct perf_evsel *evsel, int id, int nr)
44175b6f 784{
421a50f3 785 switch (stat_config.aggr_mode) {
12c08a9f 786 case AGGR_CORE:
5821522e 787 fprintf(stat_config.output, "S%d-C%*d%s%*d%s",
12c08a9f
SE
788 cpu_map__id_to_socket(id),
789 csv_output ? 0 : -8,
790 cpu_map__id_to_cpu(id),
791 csv_sep,
792 csv_output ? 0 : 4,
793 nr,
794 csv_sep);
795 break;
86ee6e18 796 case AGGR_SOCKET:
5821522e 797 fprintf(stat_config.output, "S%*d%s%*d%s",
d7e7a451 798 csv_output ? 0 : -5,
12c08a9f 799 id,
d7e7a451
SE
800 csv_sep,
801 csv_output ? 0 : 4,
802 nr,
803 csv_sep);
86ee6e18
SE
804 break;
805 case AGGR_NONE:
5821522e 806 fprintf(stat_config.output, "CPU%*d%s",
d7470b6a 807 csv_output ? 0 : -4,
12c08a9f 808 perf_evsel__cpus(evsel)->map[id], csv_sep);
86ee6e18 809 break;
32b8af82 810 case AGGR_THREAD:
5821522e 811 fprintf(stat_config.output, "%*s-%*d%s",
32b8af82
JO
812 csv_output ? 0 : 16,
813 thread_map__comm(evsel->threads, id),
814 csv_output ? 0 : -8,
815 thread_map__pid(evsel->threads, id),
816 csv_sep);
817 break;
86ee6e18 818 case AGGR_GLOBAL:
208df99e 819 case AGGR_UNSET:
86ee6e18
SE
820 default:
821 break;
822 }
823}
824
140aeadc
AK
825struct outstate {
826 FILE *fh;
827 bool newline;
f9483392 828 const char *prefix;
92a61f64 829 int nfields;
44d49a60
AK
830 int id, nr;
831 struct perf_evsel *evsel;
140aeadc
AK
832};
833
834#define METRIC_LEN 35
835
836static void new_line_std(void *ctx)
837{
838 struct outstate *os = ctx;
839
840 os->newline = true;
841}
842
843static void do_new_line_std(struct outstate *os)
844{
845 fputc('\n', os->fh);
f9483392 846 fputs(os->prefix, os->fh);
44d49a60 847 aggr_printout(os->evsel, os->id, os->nr);
140aeadc
AK
848 if (stat_config.aggr_mode == AGGR_NONE)
849 fprintf(os->fh, " ");
140aeadc
AK
850 fprintf(os->fh, " ");
851}
852
853static void print_metric_std(void *ctx, const char *color, const char *fmt,
854 const char *unit, double val)
855{
856 struct outstate *os = ctx;
857 FILE *out = os->fh;
858 int n;
859 bool newline = os->newline;
860
861 os->newline = false;
862
863 if (unit == NULL || fmt == NULL) {
864 fprintf(out, "%-*s", METRIC_LEN, "");
865 return;
866 }
867
868 if (newline)
869 do_new_line_std(os);
870
871 n = fprintf(out, " # ");
872 if (color)
873 n += color_fprintf(out, color, fmt, val);
874 else
875 n += fprintf(out, fmt, val);
876 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
877}
878
92a61f64
AK
879static void new_line_csv(void *ctx)
880{
881 struct outstate *os = ctx;
882 int i;
883
884 fputc('\n', os->fh);
885 if (os->prefix)
886 fprintf(os->fh, "%s%s", os->prefix, csv_sep);
44d49a60 887 aggr_printout(os->evsel, os->id, os->nr);
92a61f64
AK
888 for (i = 0; i < os->nfields; i++)
889 fputs(csv_sep, os->fh);
890}
891
892static void print_metric_csv(void *ctx,
893 const char *color __maybe_unused,
894 const char *fmt, const char *unit, double val)
895{
896 struct outstate *os = ctx;
897 FILE *out = os->fh;
898 char buf[64], *vals, *ends;
899
900 if (unit == NULL || fmt == NULL) {
901 fprintf(out, "%s%s%s%s", csv_sep, csv_sep, csv_sep, csv_sep);
902 return;
903 }
904 snprintf(buf, sizeof(buf), fmt, val);
b07c40df 905 ends = vals = ltrim(buf);
92a61f64
AK
906 while (isdigit(*ends) || *ends == '.')
907 ends++;
908 *ends = 0;
909 while (isspace(*unit))
910 unit++;
911 fprintf(out, "%s%s%s%s", csv_sep, vals, csv_sep, unit);
912}
913
54b50916
AK
914#define METRIC_ONLY_LEN 20
915
916/* Filter out some columns that don't work well in metrics only mode */
917
918static bool valid_only_metric(const char *unit)
919{
920 if (!unit)
921 return false;
922 if (strstr(unit, "/sec") ||
923 strstr(unit, "hz") ||
924 strstr(unit, "Hz") ||
925 strstr(unit, "CPUs utilized"))
926 return false;
927 return true;
928}
929
930static const char *fixunit(char *buf, struct perf_evsel *evsel,
931 const char *unit)
932{
933 if (!strncmp(unit, "of all", 6)) {
934 snprintf(buf, 1024, "%s %s", perf_evsel__name(evsel),
935 unit);
936 return buf;
937 }
938 return unit;
939}
940
941static void print_metric_only(void *ctx, const char *color, const char *fmt,
942 const char *unit, double val)
943{
944 struct outstate *os = ctx;
945 FILE *out = os->fh;
946 int n;
947 char buf[1024];
948 unsigned mlen = METRIC_ONLY_LEN;
949
950 if (!valid_only_metric(unit))
951 return;
952 unit = fixunit(buf, os->evsel, unit);
953 if (color)
954 n = color_fprintf(out, color, fmt, val);
955 else
956 n = fprintf(out, fmt, val);
957 if (n > METRIC_ONLY_LEN)
958 n = METRIC_ONLY_LEN;
959 if (mlen < strlen(unit))
960 mlen = strlen(unit) + 1;
961 fprintf(out, "%*s", mlen - n, "");
962}
963
964static void print_metric_only_csv(void *ctx, const char *color __maybe_unused,
965 const char *fmt,
966 const char *unit, double val)
967{
968 struct outstate *os = ctx;
969 FILE *out = os->fh;
970 char buf[64], *vals, *ends;
971 char tbuf[1024];
972
973 if (!valid_only_metric(unit))
974 return;
975 unit = fixunit(tbuf, os->evsel, unit);
976 snprintf(buf, sizeof buf, fmt, val);
b07c40df 977 ends = vals = ltrim(buf);
54b50916
AK
978 while (isdigit(*ends) || *ends == '.')
979 ends++;
980 *ends = 0;
981 fprintf(out, "%s%s", vals, csv_sep);
982}
983
984static void new_line_metric(void *ctx __maybe_unused)
985{
986}
987
988static void print_metric_header(void *ctx, const char *color __maybe_unused,
989 const char *fmt __maybe_unused,
990 const char *unit, double val __maybe_unused)
991{
992 struct outstate *os = ctx;
993 char tbuf[1024];
994
995 if (!valid_only_metric(unit))
996 return;
997 unit = fixunit(tbuf, os->evsel, unit);
998 if (csv_output)
999 fprintf(os->fh, "%s%s", unit, csv_sep);
1000 else
1001 fprintf(os->fh, "%-*s ", METRIC_ONLY_LEN, unit);
1002}
1003
da88c7f7 1004static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg)
86ee6e18 1005{
5821522e 1006 FILE *output = stat_config.output;
310ebb93 1007 double msecs = avg / NSEC_PER_MSEC;
410136f5 1008 const char *fmt_v, *fmt_n;
4bbe5a61 1009 char name[25];
86ee6e18 1010
410136f5
SE
1011 fmt_v = csv_output ? "%.6f%s" : "%18.6f%s";
1012 fmt_n = csv_output ? "%s" : "%-25s";
1013
da88c7f7 1014 aggr_printout(evsel, id, nr);
d7470b6a 1015
4bbe5a61
DA
1016 scnprintf(name, sizeof(name), "%s%s",
1017 perf_evsel__name(evsel), csv_output ? "" : " (msec)");
410136f5
SE
1018
1019 fprintf(output, fmt_v, msecs, csv_sep);
1020
1021 if (csv_output)
1022 fprintf(output, "%s%s", evsel->unit, csv_sep);
1023 else
1024 fprintf(output, "%-*s%s", unit_width, evsel->unit, csv_sep);
1025
1026 fprintf(output, fmt_n, name);
d7470b6a 1027
023695d9 1028 if (evsel->cgrp)
4aa9015f 1029 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
44175b6f
IM
1030}
1031
44d49a60
AK
1032static int first_shadow_cpu(struct perf_evsel *evsel, int id)
1033{
1034 int i;
1035
1036 if (!aggr_get_id)
1037 return 0;
1038
1039 if (stat_config.aggr_mode == AGGR_NONE)
1040 return id;
1041
1042 if (stat_config.aggr_mode == AGGR_GLOBAL)
1043 return 0;
1044
1045 for (i = 0; i < perf_evsel__nr_cpus(evsel); i++) {
1046 int cpu2 = perf_evsel__cpus(evsel)->map[i];
1047
1048 if (aggr_get_id(evsel_list->cpus, cpu2) == id)
1049 return cpu2;
1050 }
1051 return 0;
1052}
1053
556b1fb7
JO
1054static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg)
1055{
5821522e 1056 FILE *output = stat_config.output;
556b1fb7
JO
1057 double sc = evsel->scale;
1058 const char *fmt;
556b1fb7
JO
1059
1060 if (csv_output) {
e3b03b6c 1061 fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
556b1fb7
JO
1062 } else {
1063 if (big_num)
e3b03b6c 1064 fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
556b1fb7 1065 else
e3b03b6c 1066 fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
556b1fb7
JO
1067 }
1068
1069 aggr_printout(evsel, id, nr);
1070
556b1fb7
JO
1071 fprintf(output, fmt, avg, csv_sep);
1072
1073 if (evsel->unit)
1074 fprintf(output, "%-*s%s",
1075 csv_output ? 0 : unit_width,
1076 evsel->unit, csv_sep);
1077
1078 fprintf(output, "%-*s", csv_output ? 0 : 25, perf_evsel__name(evsel));
1079
1080 if (evsel->cgrp)
1081 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
eedfcb4b 1082}
556b1fb7 1083
f9483392 1084static void printout(int id, int nr, struct perf_evsel *counter, double uval,
cb110f47 1085 char *prefix, u64 run, u64 ena, double noise)
eedfcb4b 1086{
140aeadc 1087 struct perf_stat_output_ctx out;
f9483392
AK
1088 struct outstate os = {
1089 .fh = stat_config.output,
44d49a60
AK
1090 .prefix = prefix ? prefix : "",
1091 .id = id,
1092 .nr = nr,
1093 .evsel = counter,
f9483392 1094 };
140aeadc
AK
1095 print_metric_t pm = print_metric_std;
1096 void (*nl)(void *);
eedfcb4b 1097
54b50916
AK
1098 if (metric_only) {
1099 nl = new_line_metric;
1100 if (csv_output)
1101 pm = print_metric_only_csv;
1102 else
1103 pm = print_metric_only;
1104 } else
1105 nl = new_line_std;
eedfcb4b 1106
54b50916 1107 if (csv_output && !metric_only) {
92a61f64
AK
1108 static int aggr_fields[] = {
1109 [AGGR_GLOBAL] = 0,
1110 [AGGR_THREAD] = 1,
1111 [AGGR_NONE] = 1,
1112 [AGGR_SOCKET] = 2,
1113 [AGGR_CORE] = 2,
1114 };
1115
1116 pm = print_metric_csv;
1117 nl = new_line_csv;
1118 os.nfields = 3;
1119 os.nfields += aggr_fields[stat_config.aggr_mode];
1120 if (counter->cgrp)
1121 os.nfields++;
1122 }
b002f3bb 1123 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
54b50916
AK
1124 if (metric_only) {
1125 pm(&os, NULL, "", "", 0);
1126 return;
1127 }
cb110f47
AK
1128 aggr_printout(counter, id, nr);
1129
1130 fprintf(stat_config.output, "%*s%s",
1131 csv_output ? 0 : 18,
1132 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
1133 csv_sep);
1134
02d492e5
BP
1135 if (counter->supported)
1136 print_free_counters_hint = 1;
1137
cb110f47
AK
1138 fprintf(stat_config.output, "%-*s%s",
1139 csv_output ? 0 : unit_width,
1140 counter->unit, csv_sep);
1141
1142 fprintf(stat_config.output, "%*s",
1143 csv_output ? 0 : -25,
1144 perf_evsel__name(counter));
1145
1146 if (counter->cgrp)
1147 fprintf(stat_config.output, "%s%s",
1148 csv_sep, counter->cgrp->name);
1149
92a61f64
AK
1150 if (!csv_output)
1151 pm(&os, NULL, NULL, "", 0);
1152 print_noise(counter, noise);
cb110f47 1153 print_running(run, ena);
92a61f64
AK
1154 if (csv_output)
1155 pm(&os, NULL, NULL, "", 0);
cb110f47
AK
1156 return;
1157 }
1158
54b50916
AK
1159 if (metric_only)
1160 /* nothing */;
1161 else if (nsec_counter(counter))
eedfcb4b
AK
1162 nsec_printout(id, nr, counter, uval);
1163 else
1164 abs_printout(id, nr, counter, uval);
556b1fb7 1165
140aeadc
AK
1166 out.print_metric = pm;
1167 out.new_line = nl;
1168 out.ctx = &os;
37932c18 1169 out.force_header = false;
140aeadc 1170
54b50916 1171 if (csv_output && !metric_only) {
92a61f64
AK
1172 print_noise(counter, noise);
1173 print_running(run, ena);
1174 }
1175
1176 perf_stat__print_shadow_stats(counter, uval,
44d49a60 1177 first_shadow_cpu(counter, id),
140aeadc 1178 &out);
54b50916 1179 if (!csv_output && !metric_only) {
92a61f64
AK
1180 print_noise(counter, noise);
1181 print_running(run, ena);
1182 }
556b1fb7
JO
1183}
1184
44d49a60
AK
1185static void aggr_update_shadow(void)
1186{
1187 int cpu, s2, id, s;
1188 u64 val;
1189 struct perf_evsel *counter;
1190
1191 for (s = 0; s < aggr_map->nr; s++) {
1192 id = aggr_map->map[s];
e5cadb93 1193 evlist__for_each_entry(evsel_list, counter) {
44d49a60
AK
1194 val = 0;
1195 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1196 s2 = aggr_get_id(evsel_list->cpus, cpu);
1197 if (s2 != id)
1198 continue;
1199 val += perf_counts(counter->counts, cpu, 0)->val;
1200 }
1201 val = val * counter->scale;
1202 perf_stat__update_shadow_stats(counter, &val,
1203 first_shadow_cpu(counter, id));
1204 }
1205 }
1206}
1207
430daf2d 1208static void collect_all_aliases(struct perf_evsel *counter,
fbe51fba
AK
1209 void (*cb)(struct perf_evsel *counter, void *data,
1210 bool first),
1211 void *data)
1212{
430daf2d
AK
1213 struct perf_evsel *alias;
1214
1215 alias = list_prepare_entry(counter, &(evsel_list->entries), node);
1216 list_for_each_entry_continue (alias, &evsel_list->entries, node) {
1217 if (strcmp(perf_evsel__name(alias), perf_evsel__name(counter)) ||
1218 alias->scale != counter->scale ||
1219 alias->cgrp != counter->cgrp ||
1220 strcmp(alias->unit, counter->unit) ||
1221 nsec_counter(alias) != nsec_counter(counter))
1222 break;
1223 alias->merged_stat = true;
1224 cb(alias, data, false);
1225 }
1226}
1227
1228static bool collect_data(struct perf_evsel *counter,
1229 void (*cb)(struct perf_evsel *counter, void *data,
1230 bool first),
1231 void *data)
1232{
1233 if (counter->merged_stat)
1234 return false;
fbe51fba 1235 cb(counter, data, true);
430daf2d
AK
1236 if (!no_merge)
1237 collect_all_aliases(counter, cb, data);
1238 return true;
fbe51fba
AK
1239}
1240
1241struct aggr_data {
1242 u64 ena, run, val;
1243 int id;
1244 int nr;
1245 int cpu;
1246};
1247
1248static void aggr_cb(struct perf_evsel *counter, void *data, bool first)
1249{
1250 struct aggr_data *ad = data;
1251 int cpu, s2;
1252
1253 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1254 struct perf_counts_values *counts;
1255
1256 s2 = aggr_get_id(perf_evsel__cpus(counter), cpu);
1257 if (s2 != ad->id)
1258 continue;
1259 if (first)
1260 ad->nr++;
1261 counts = perf_counts(counter->counts, cpu, 0);
b4229e9d
AK
1262 /*
1263 * When any result is bad, make them all to give
1264 * consistent output in interval mode.
1265 */
1266 if (counts->ena == 0 || counts->run == 0 ||
1267 counter->counts->scaled == -1) {
1268 ad->ena = 0;
1269 ad->run = 0;
1270 break;
1271 }
fbe51fba
AK
1272 ad->val += counts->val;
1273 ad->ena += counts->ena;
1274 ad->run += counts->run;
1275 }
1276}
1277
86ee6e18 1278static void print_aggr(char *prefix)
d7e7a451 1279{
5821522e 1280 FILE *output = stat_config.output;
d7e7a451 1281 struct perf_evsel *counter;
fbe51fba 1282 int s, id, nr;
410136f5 1283 double uval;
d7e7a451 1284 u64 ena, run, val;
54b50916 1285 bool first;
d7e7a451 1286
86ee6e18 1287 if (!(aggr_map || aggr_get_id))
d7e7a451
SE
1288 return;
1289
44d49a60
AK
1290 aggr_update_shadow();
1291
54b50916
AK
1292 /*
1293 * With metric_only everything is on a single line.
1294 * Without each counter has its own line.
1295 */
86ee6e18 1296 for (s = 0; s < aggr_map->nr; s++) {
fbe51fba 1297 struct aggr_data ad;
54b50916
AK
1298 if (prefix && metric_only)
1299 fprintf(output, "%s", prefix);
1300
fbe51fba 1301 ad.id = id = aggr_map->map[s];
54b50916 1302 first = true;
e5cadb93 1303 evlist__for_each_entry(evsel_list, counter) {
fbe51fba
AK
1304 ad.val = ad.ena = ad.run = 0;
1305 ad.nr = 0;
430daf2d
AK
1306 if (!collect_data(counter, aggr_cb, &ad))
1307 continue;
fbe51fba
AK
1308 nr = ad.nr;
1309 ena = ad.ena;
1310 run = ad.run;
1311 val = ad.val;
54b50916
AK
1312 if (first && metric_only) {
1313 first = false;
1314 aggr_printout(counter, id, nr);
1315 }
1316 if (prefix && !metric_only)
d7e7a451
SE
1317 fprintf(output, "%s", prefix);
1318
410136f5 1319 uval = val * counter->scale;
cb110f47 1320 printout(id, nr, counter, uval, prefix, run, ena, 1.0);
54b50916
AK
1321 if (!metric_only)
1322 fputc('\n', output);
d7e7a451 1323 }
54b50916
AK
1324 if (metric_only)
1325 fputc('\n', output);
d7e7a451
SE
1326 }
1327}
1328
32b8af82
JO
1329static void print_aggr_thread(struct perf_evsel *counter, char *prefix)
1330{
5821522e 1331 FILE *output = stat_config.output;
32b8af82
JO
1332 int nthreads = thread_map__nr(counter->threads);
1333 int ncpus = cpu_map__nr(counter->cpus);
1334 int cpu, thread;
1335 double uval;
1336
1337 for (thread = 0; thread < nthreads; thread++) {
1338 u64 ena = 0, run = 0, val = 0;
1339
1340 for (cpu = 0; cpu < ncpus; cpu++) {
1341 val += perf_counts(counter->counts, cpu, thread)->val;
1342 ena += perf_counts(counter->counts, cpu, thread)->ena;
1343 run += perf_counts(counter->counts, cpu, thread)->run;
1344 }
1345
1346 if (prefix)
1347 fprintf(output, "%s", prefix);
1348
1349 uval = val * counter->scale;
cb110f47 1350 printout(thread, 0, counter, uval, prefix, run, ena, 1.0);
32b8af82
JO
1351 fputc('\n', output);
1352 }
1353}
1354
fbe51fba
AK
1355struct caggr_data {
1356 double avg, avg_enabled, avg_running;
1357};
1358
1359static void counter_aggr_cb(struct perf_evsel *counter, void *data,
1360 bool first __maybe_unused)
1361{
1362 struct caggr_data *cd = data;
1363 struct perf_stat_evsel *ps = counter->priv;
1364
1365 cd->avg += avg_stats(&ps->res_stats[0]);
1366 cd->avg_enabled += avg_stats(&ps->res_stats[1]);
1367 cd->avg_running += avg_stats(&ps->res_stats[2]);
1368}
1369
2996f5dd
IM
1370/*
1371 * Print out the results of a single counter:
f5b4a9c3 1372 * aggregated counts in system-wide mode
2996f5dd 1373 */
13370a9b 1374static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
2996f5dd 1375{
5821522e 1376 FILE *output = stat_config.output;
410136f5 1377 double uval;
fbe51fba 1378 struct caggr_data cd = { .avg = 0.0 };
d73515c0 1379
430daf2d
AK
1380 if (!collect_data(counter, counter_aggr_cb, &cd))
1381 return;
2996f5dd 1382
54b50916 1383 if (prefix && !metric_only)
13370a9b
SE
1384 fprintf(output, "%s", prefix);
1385
fbe51fba
AK
1386 uval = cd.avg * counter->scale;
1387 printout(-1, 0, counter, uval, prefix, cd.avg_running, cd.avg_enabled, cd.avg);
54b50916
AK
1388 if (!metric_only)
1389 fprintf(output, "\n");
c04f5e5d
IM
1390}
1391
fbe51fba
AK
1392static void counter_cb(struct perf_evsel *counter, void *data,
1393 bool first __maybe_unused)
1394{
1395 struct aggr_data *ad = data;
1396
1397 ad->val += perf_counts(counter->counts, ad->cpu, 0)->val;
1398 ad->ena += perf_counts(counter->counts, ad->cpu, 0)->ena;
1399 ad->run += perf_counts(counter->counts, ad->cpu, 0)->run;
1400}
1401
f5b4a9c3
SE
1402/*
1403 * Print out the results of a single counter:
1404 * does not use aggregated count in system-wide
1405 */
13370a9b 1406static void print_counter(struct perf_evsel *counter, char *prefix)
f5b4a9c3 1407{
5821522e 1408 FILE *output = stat_config.output;
f5b4a9c3 1409 u64 ena, run, val;
410136f5 1410 double uval;
f5b4a9c3
SE
1411 int cpu;
1412
7ae92e74 1413 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
fbe51fba
AK
1414 struct aggr_data ad = { .cpu = cpu };
1415
430daf2d
AK
1416 if (!collect_data(counter, counter_cb, &ad))
1417 return;
fbe51fba
AK
1418 val = ad.val;
1419 ena = ad.ena;
1420 run = ad.run;
13370a9b
SE
1421
1422 if (prefix)
1423 fprintf(output, "%s", prefix);
1424
410136f5 1425 uval = val * counter->scale;
cb110f47 1426 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0);
f5b4a9c3 1427
4aa9015f 1428 fputc('\n', output);
f5b4a9c3
SE
1429 }
1430}
1431
206cab65
AK
1432static void print_no_aggr_metric(char *prefix)
1433{
1434 int cpu;
1435 int nrcpus = 0;
1436 struct perf_evsel *counter;
1437 u64 ena, run, val;
1438 double uval;
1439
1440 nrcpus = evsel_list->cpus->nr;
1441 for (cpu = 0; cpu < nrcpus; cpu++) {
1442 bool first = true;
1443
1444 if (prefix)
1445 fputs(prefix, stat_config.output);
e5cadb93 1446 evlist__for_each_entry(evsel_list, counter) {
206cab65
AK
1447 if (first) {
1448 aggr_printout(counter, cpu, 0);
1449 first = false;
1450 }
1451 val = perf_counts(counter->counts, cpu, 0)->val;
1452 ena = perf_counts(counter->counts, cpu, 0)->ena;
1453 run = perf_counts(counter->counts, cpu, 0)->run;
1454
1455 uval = val * counter->scale;
1456 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0);
1457 }
1458 fputc('\n', stat_config.output);
1459 }
1460}
1461
54b50916
AK
1462static int aggr_header_lens[] = {
1463 [AGGR_CORE] = 18,
1464 [AGGR_SOCKET] = 12,
206cab65 1465 [AGGR_NONE] = 6,
54b50916
AK
1466 [AGGR_THREAD] = 24,
1467 [AGGR_GLOBAL] = 0,
1468};
1469
c51fd639
AK
1470static const char *aggr_header_csv[] = {
1471 [AGGR_CORE] = "core,cpus,",
1472 [AGGR_SOCKET] = "socket,cpus",
1473 [AGGR_NONE] = "cpu,",
1474 [AGGR_THREAD] = "comm-pid,",
1475 [AGGR_GLOBAL] = ""
1476};
1477
41c8ca2a 1478static void print_metric_headers(const char *prefix, bool no_indent)
54b50916
AK
1479{
1480 struct perf_stat_output_ctx out;
1481 struct perf_evsel *counter;
1482 struct outstate os = {
1483 .fh = stat_config.output
1484 };
1485
1486 if (prefix)
1487 fprintf(stat_config.output, "%s", prefix);
1488
41c8ca2a 1489 if (!csv_output && !no_indent)
54b50916
AK
1490 fprintf(stat_config.output, "%*s",
1491 aggr_header_lens[stat_config.aggr_mode], "");
c51fd639
AK
1492 if (csv_output) {
1493 if (stat_config.interval)
1494 fputs("time,", stat_config.output);
1495 fputs(aggr_header_csv[stat_config.aggr_mode],
1496 stat_config.output);
1497 }
54b50916
AK
1498
1499 /* Print metrics headers only */
e5cadb93 1500 evlist__for_each_entry(evsel_list, counter) {
54b50916
AK
1501 os.evsel = counter;
1502 out.ctx = &os;
1503 out.print_metric = print_metric_header;
1504 out.new_line = new_line_metric;
37932c18 1505 out.force_header = true;
54b50916
AK
1506 os.evsel = counter;
1507 perf_stat__print_shadow_stats(counter, 0,
1508 0,
1509 &out);
1510 }
1511 fputc('\n', stat_config.output);
1512}
1513
d4f63a47
JO
1514static void print_interval(char *prefix, struct timespec *ts)
1515{
5821522e 1516 FILE *output = stat_config.output;
d4f63a47
JO
1517 static int num_print_interval;
1518
1519 sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, csv_sep);
1520
41c8ca2a 1521 if (num_print_interval == 0 && !csv_output) {
421a50f3 1522 switch (stat_config.aggr_mode) {
d4f63a47 1523 case AGGR_SOCKET:
41c8ca2a
AK
1524 fprintf(output, "# time socket cpus");
1525 if (!metric_only)
1526 fprintf(output, " counts %*s events\n", unit_width, "unit");
d4f63a47
JO
1527 break;
1528 case AGGR_CORE:
41c8ca2a
AK
1529 fprintf(output, "# time core cpus");
1530 if (!metric_only)
1531 fprintf(output, " counts %*s events\n", unit_width, "unit");
d4f63a47
JO
1532 break;
1533 case AGGR_NONE:
41c8ca2a
AK
1534 fprintf(output, "# time CPU");
1535 if (!metric_only)
1536 fprintf(output, " counts %*s events\n", unit_width, "unit");
d4f63a47 1537 break;
32b8af82 1538 case AGGR_THREAD:
41c8ca2a
AK
1539 fprintf(output, "# time comm-pid");
1540 if (!metric_only)
1541 fprintf(output, " counts %*s events\n", unit_width, "unit");
32b8af82 1542 break;
d4f63a47
JO
1543 case AGGR_GLOBAL:
1544 default:
41c8ca2a
AK
1545 fprintf(output, "# time");
1546 if (!metric_only)
1547 fprintf(output, " counts %*s events\n", unit_width, "unit");
208df99e
JO
1548 case AGGR_UNSET:
1549 break;
d4f63a47
JO
1550 }
1551 }
1552
41c8ca2a
AK
1553 if (num_print_interval == 0 && metric_only)
1554 print_metric_headers(" ", true);
d4f63a47
JO
1555 if (++num_print_interval == 25)
1556 num_print_interval = 0;
1557}
1558
1559static void print_header(int argc, const char **argv)
42202dd5 1560{
5821522e 1561 FILE *output = stat_config.output;
69aad6f1 1562 int i;
42202dd5 1563
ddcacfa0
IM
1564 fflush(stdout);
1565
d7470b6a 1566 if (!csv_output) {
4aa9015f
SE
1567 fprintf(output, "\n");
1568 fprintf(output, " Performance counter stats for ");
62d3b617
DA
1569 if (target.system_wide)
1570 fprintf(output, "\'system wide");
1571 else if (target.cpu_list)
1572 fprintf(output, "\'CPU(s) %s", target.cpu_list);
602ad878 1573 else if (!target__has_task(&target)) {
ba6039b6
JO
1574 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1575 for (i = 1; argv && (i < argc); i++)
4aa9015f 1576 fprintf(output, " %s", argv[i]);
20f946b4
NK
1577 } else if (target.pid)
1578 fprintf(output, "process id \'%s", target.pid);
d7470b6a 1579 else
20f946b4 1580 fprintf(output, "thread id \'%s", target.tid);
44db76c8 1581
4aa9015f 1582 fprintf(output, "\'");
d7470b6a 1583 if (run_count > 1)
4aa9015f
SE
1584 fprintf(output, " (%d runs)", run_count);
1585 fprintf(output, ":\n\n");
d7470b6a 1586 }
d4f63a47
JO
1587}
1588
1589static void print_footer(void)
1590{
5821522e 1591 FILE *output = stat_config.output;
918c7b06 1592 int n;
5821522e 1593
d4f63a47
JO
1594 if (!null_run)
1595 fprintf(output, "\n");
1596 fprintf(output, " %17.9f seconds time elapsed",
310ebb93 1597 avg_stats(&walltime_nsecs_stats) / NSEC_PER_SEC);
d4f63a47
JO
1598 if (run_count > 1) {
1599 fprintf(output, " ");
1600 print_noise_pct(stddev_stats(&walltime_nsecs_stats),
1601 avg_stats(&walltime_nsecs_stats));
1602 }
1603 fprintf(output, "\n\n");
02d492e5 1604
918c7b06
AK
1605 if (print_free_counters_hint &&
1606 sysctl__read_int("kernel/nmi_watchdog", &n) >= 0 &&
1607 n > 0)
02d492e5
BP
1608 fprintf(output,
1609"Some events weren't counted. Try disabling the NMI watchdog:\n"
1610" echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1611" perf stat ...\n"
1612" echo 1 > /proc/sys/kernel/nmi_watchdog\n");
d4f63a47
JO
1613}
1614
1615static void print_counters(struct timespec *ts, int argc, const char **argv)
1616{
ec0d3d1f 1617 int interval = stat_config.interval;
d4f63a47
JO
1618 struct perf_evsel *counter;
1619 char buf[64], *prefix = NULL;
1620
664c98d4
JO
1621 /* Do not print anything if we record to the pipe. */
1622 if (STAT_RECORD && perf_stat.file.is_pipe)
1623 return;
1624
d4f63a47
JO
1625 if (interval)
1626 print_interval(prefix = buf, ts);
1627 else
1628 print_header(argc, argv);
2996f5dd 1629
54b50916
AK
1630 if (metric_only) {
1631 static int num_print_iv;
1632
41c8ca2a
AK
1633 if (num_print_iv == 0 && !interval)
1634 print_metric_headers(prefix, false);
54b50916
AK
1635 if (num_print_iv++ == 25)
1636 num_print_iv = 0;
1637 if (stat_config.aggr_mode == AGGR_GLOBAL && prefix)
1638 fprintf(stat_config.output, "%s", prefix);
1639 }
1640
421a50f3 1641 switch (stat_config.aggr_mode) {
12c08a9f 1642 case AGGR_CORE:
86ee6e18 1643 case AGGR_SOCKET:
d4f63a47 1644 print_aggr(prefix);
86ee6e18 1645 break;
32b8af82 1646 case AGGR_THREAD:
e5cadb93 1647 evlist__for_each_entry(evsel_list, counter)
32b8af82
JO
1648 print_aggr_thread(counter, prefix);
1649 break;
86ee6e18 1650 case AGGR_GLOBAL:
e5cadb93 1651 evlist__for_each_entry(evsel_list, counter)
d4f63a47 1652 print_counter_aggr(counter, prefix);
54b50916
AK
1653 if (metric_only)
1654 fputc('\n', stat_config.output);
86ee6e18
SE
1655 break;
1656 case AGGR_NONE:
206cab65
AK
1657 if (metric_only)
1658 print_no_aggr_metric(prefix);
1659 else {
e5cadb93 1660 evlist__for_each_entry(evsel_list, counter)
206cab65
AK
1661 print_counter(counter, prefix);
1662 }
86ee6e18 1663 break;
208df99e 1664 case AGGR_UNSET:
86ee6e18
SE
1665 default:
1666 break;
f5b4a9c3 1667 }
ddcacfa0 1668
d4f63a47
JO
1669 if (!interval && !csv_output)
1670 print_footer();
1671
5821522e 1672 fflush(stat_config.output);
ddcacfa0
IM
1673}
1674
f7b7c26e
PZ
1675static volatile int signr = -1;
1676
5242519b 1677static void skip_signal(int signo)
ddcacfa0 1678{
ec0d3d1f 1679 if ((child_pid == -1) || stat_config.interval)
60666c63
LW
1680 done = 1;
1681
f7b7c26e 1682 signr = signo;
d07f0b12
SE
1683 /*
1684 * render child_pid harmless
1685 * won't send SIGTERM to a random
1686 * process in case of race condition
1687 * and fast PID recycling
1688 */
1689 child_pid = -1;
f7b7c26e
PZ
1690}
1691
1692static void sig_atexit(void)
1693{
d07f0b12
SE
1694 sigset_t set, oset;
1695
1696 /*
1697 * avoid race condition with SIGCHLD handler
1698 * in skip_signal() which is modifying child_pid
1699 * goal is to avoid send SIGTERM to a random
1700 * process
1701 */
1702 sigemptyset(&set);
1703 sigaddset(&set, SIGCHLD);
1704 sigprocmask(SIG_BLOCK, &set, &oset);
1705
933da83a
CW
1706 if (child_pid != -1)
1707 kill(child_pid, SIGTERM);
1708
d07f0b12
SE
1709 sigprocmask(SIG_SETMASK, &oset, NULL);
1710
f7b7c26e
PZ
1711 if (signr == -1)
1712 return;
1713
1714 signal(signr, SIG_DFL);
1715 kill(getpid(), signr);
5242519b
IM
1716}
1717
1d037ca1
IT
1718static int stat__set_big_num(const struct option *opt __maybe_unused,
1719 const char *s __maybe_unused, int unset)
d7470b6a
SE
1720{
1721 big_num_opt = unset ? 0 : 1;
1722 return 0;
1723}
1724
44b1e60a
AK
1725static int enable_metric_only(const struct option *opt __maybe_unused,
1726 const char *s __maybe_unused, int unset)
1727{
1728 force_metric_only = true;
1729 metric_only = !unset;
1730 return 0;
1731}
1732
e0547311
JO
1733static const struct option stat_options[] = {
1734 OPT_BOOLEAN('T', "transaction", &transaction_run,
1735 "hardware transaction statistics"),
1736 OPT_CALLBACK('e', "event", &evsel_list, "event",
1737 "event selector. use 'perf list' to list available events",
1738 parse_events_option),
1739 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
1740 "event filter", parse_filter),
1741 OPT_BOOLEAN('i', "no-inherit", &no_inherit,
1742 "child tasks do not inherit counters"),
1743 OPT_STRING('p', "pid", &target.pid, "pid",
1744 "stat events on existing process id"),
1745 OPT_STRING('t', "tid", &target.tid, "tid",
1746 "stat events on existing thread id"),
1747 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
1748 "system-wide collection from all CPUs"),
1749 OPT_BOOLEAN('g', "group", &group,
1750 "put the counters into a counter group"),
1751 OPT_BOOLEAN('c', "scale", &stat_config.scale, "scale/normalize counters"),
1752 OPT_INCR('v', "verbose", &verbose,
1753 "be more verbose (show counter open errors, etc)"),
1754 OPT_INTEGER('r', "repeat", &run_count,
1755 "repeat command and print average + stddev (max: 100, forever: 0)"),
1756 OPT_BOOLEAN('n', "null", &null_run,
1757 "null run - dont start any counters"),
1758 OPT_INCR('d', "detailed", &detailed_run,
1759 "detailed run - start a lot of events"),
1760 OPT_BOOLEAN('S', "sync", &sync_run,
1761 "call sync() before starting a run"),
1762 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
1763 "print large numbers with thousands\' separators",
1764 stat__set_big_num),
1765 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
1766 "list of cpus to monitor in system-wide"),
1767 OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode,
1768 "disable CPU count aggregation", AGGR_NONE),
430daf2d 1769 OPT_BOOLEAN(0, "no-merge", &no_merge, "Do not merge identical named events"),
e0547311
JO
1770 OPT_STRING('x', "field-separator", &csv_sep, "separator",
1771 "print counts with custom separator"),
1772 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
1773 "monitor event in cgroup name only", parse_cgroups),
1774 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1775 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
1776 OPT_INTEGER(0, "log-fd", &output_fd,
1777 "log output to fd, instead of stderr"),
1778 OPT_STRING(0, "pre", &pre_cmd, "command",
1779 "command to run prior to the measured command"),
1780 OPT_STRING(0, "post", &post_cmd, "command",
1781 "command to run after to the measured command"),
1782 OPT_UINTEGER('I', "interval-print", &stat_config.interval,
1783 "print counts at regular interval in ms (>= 10)"),
1784 OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode,
1785 "aggregate counts per processor socket", AGGR_SOCKET),
1786 OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode,
1787 "aggregate counts per physical processor core", AGGR_CORE),
1788 OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode,
1789 "aggregate counts per thread", AGGR_THREAD),
1790 OPT_UINTEGER('D', "delay", &initial_delay,
1791 "ms to wait before starting measurement after program start"),
44b1e60a
AK
1792 OPT_CALLBACK_NOOPT(0, "metric-only", &metric_only, NULL,
1793 "Only print computed metrics. No raw values", enable_metric_only),
1794 OPT_BOOLEAN(0, "topdown", &topdown_run,
1795 "measure topdown level 1 statistics"),
daefd0bc
KL
1796 OPT_BOOLEAN(0, "smi-cost", &smi_cost,
1797 "measure SMI cost"),
e0547311
JO
1798 OPT_END()
1799};
1800
1fe7a300
JO
1801static int perf_stat__get_socket(struct cpu_map *map, int cpu)
1802{
1803 return cpu_map__get_socket(map, cpu, NULL);
1804}
1805
1806static int perf_stat__get_core(struct cpu_map *map, int cpu)
1807{
1808 return cpu_map__get_core(map, cpu, NULL);
1809}
1810
1e5a2931
JO
1811static int cpu_map__get_max(struct cpu_map *map)
1812{
1813 int i, max = -1;
1814
1815 for (i = 0; i < map->nr; i++) {
1816 if (map->map[i] > max)
1817 max = map->map[i];
1818 }
1819
1820 return max;
1821}
1822
1823static struct cpu_map *cpus_aggr_map;
1824
1825static int perf_stat__get_aggr(aggr_get_id_t get_id, struct cpu_map *map, int idx)
1826{
1827 int cpu;
1828
1829 if (idx >= map->nr)
1830 return -1;
1831
1832 cpu = map->map[idx];
1833
1834 if (cpus_aggr_map->map[cpu] == -1)
1835 cpus_aggr_map->map[cpu] = get_id(map, idx);
1836
1837 return cpus_aggr_map->map[cpu];
1838}
1839
1840static int perf_stat__get_socket_cached(struct cpu_map *map, int idx)
1841{
1842 return perf_stat__get_aggr(perf_stat__get_socket, map, idx);
1843}
1844
1845static int perf_stat__get_core_cached(struct cpu_map *map, int idx)
1846{
1847 return perf_stat__get_aggr(perf_stat__get_core, map, idx);
1848}
1849
86ee6e18
SE
1850static int perf_stat_init_aggr_mode(void)
1851{
1e5a2931
JO
1852 int nr;
1853
421a50f3 1854 switch (stat_config.aggr_mode) {
86ee6e18
SE
1855 case AGGR_SOCKET:
1856 if (cpu_map__build_socket_map(evsel_list->cpus, &aggr_map)) {
1857 perror("cannot build socket map");
1858 return -1;
1859 }
1e5a2931 1860 aggr_get_id = perf_stat__get_socket_cached;
86ee6e18 1861 break;
12c08a9f
SE
1862 case AGGR_CORE:
1863 if (cpu_map__build_core_map(evsel_list->cpus, &aggr_map)) {
1864 perror("cannot build core map");
1865 return -1;
1866 }
1e5a2931 1867 aggr_get_id = perf_stat__get_core_cached;
12c08a9f 1868 break;
86ee6e18
SE
1869 case AGGR_NONE:
1870 case AGGR_GLOBAL:
32b8af82 1871 case AGGR_THREAD:
208df99e 1872 case AGGR_UNSET:
86ee6e18
SE
1873 default:
1874 break;
1875 }
1e5a2931
JO
1876
1877 /*
1878 * The evsel_list->cpus is the base we operate on,
1879 * taking the highest cpu number to be the size of
1880 * the aggregation translate cpumap.
1881 */
1882 nr = cpu_map__get_max(evsel_list->cpus);
1883 cpus_aggr_map = cpu_map__empty_new(nr + 1);
1884 return cpus_aggr_map ? 0 : -ENOMEM;
86ee6e18
SE
1885}
1886
544c2ae7
MH
1887static void perf_stat__exit_aggr_mode(void)
1888{
1889 cpu_map__put(aggr_map);
1890 cpu_map__put(cpus_aggr_map);
1891 aggr_map = NULL;
1892 cpus_aggr_map = NULL;
1893}
1894
68d702f7
JO
1895static inline int perf_env__get_cpu(struct perf_env *env, struct cpu_map *map, int idx)
1896{
1897 int cpu;
1898
1899 if (idx > map->nr)
1900 return -1;
1901
1902 cpu = map->map[idx];
1903
da8a58b5 1904 if (cpu >= env->nr_cpus_avail)
68d702f7
JO
1905 return -1;
1906
1907 return cpu;
1908}
1909
1910static int perf_env__get_socket(struct cpu_map *map, int idx, void *data)
1911{
1912 struct perf_env *env = data;
1913 int cpu = perf_env__get_cpu(env, map, idx);
1914
1915 return cpu == -1 ? -1 : env->cpu[cpu].socket_id;
1916}
1917
1918static int perf_env__get_core(struct cpu_map *map, int idx, void *data)
1919{
1920 struct perf_env *env = data;
1921 int core = -1, cpu = perf_env__get_cpu(env, map, idx);
1922
1923 if (cpu != -1) {
1924 int socket_id = env->cpu[cpu].socket_id;
1925
1926 /*
1927 * Encode socket in upper 16 bits
1928 * core_id is relative to socket, and
1929 * we need a global id. So we combine
1930 * socket + core id.
1931 */
1932 core = (socket_id << 16) | (env->cpu[cpu].core_id & 0xffff);
1933 }
1934
1935 return core;
1936}
1937
1938static int perf_env__build_socket_map(struct perf_env *env, struct cpu_map *cpus,
1939 struct cpu_map **sockp)
1940{
1941 return cpu_map__build_map(cpus, sockp, perf_env__get_socket, env);
1942}
1943
1944static int perf_env__build_core_map(struct perf_env *env, struct cpu_map *cpus,
1945 struct cpu_map **corep)
1946{
1947 return cpu_map__build_map(cpus, corep, perf_env__get_core, env);
1948}
1949
1950static int perf_stat__get_socket_file(struct cpu_map *map, int idx)
1951{
1952 return perf_env__get_socket(map, idx, &perf_stat.session->header.env);
1953}
1954
1955static int perf_stat__get_core_file(struct cpu_map *map, int idx)
1956{
1957 return perf_env__get_core(map, idx, &perf_stat.session->header.env);
1958}
1959
1960static int perf_stat_init_aggr_mode_file(struct perf_stat *st)
1961{
1962 struct perf_env *env = &st->session->header.env;
1963
1964 switch (stat_config.aggr_mode) {
1965 case AGGR_SOCKET:
1966 if (perf_env__build_socket_map(env, evsel_list->cpus, &aggr_map)) {
1967 perror("cannot build socket map");
1968 return -1;
1969 }
1970 aggr_get_id = perf_stat__get_socket_file;
1971 break;
1972 case AGGR_CORE:
1973 if (perf_env__build_core_map(env, evsel_list->cpus, &aggr_map)) {
1974 perror("cannot build core map");
1975 return -1;
1976 }
1977 aggr_get_id = perf_stat__get_core_file;
1978 break;
1979 case AGGR_NONE:
1980 case AGGR_GLOBAL:
1981 case AGGR_THREAD:
1982 case AGGR_UNSET:
1983 default:
1984 break;
1985 }
1986
1987 return 0;
1988}
1989
44b1e60a
AK
1990static int topdown_filter_events(const char **attr, char **str, bool use_group)
1991{
1992 int off = 0;
1993 int i;
1994 int len = 0;
1995 char *s;
1996
1997 for (i = 0; attr[i]; i++) {
1998 if (pmu_have_event("cpu", attr[i])) {
1999 len += strlen(attr[i]) + 1;
2000 attr[i - off] = attr[i];
2001 } else
2002 off++;
2003 }
2004 attr[i - off] = NULL;
2005
2006 *str = malloc(len + 1 + 2);
2007 if (!*str)
2008 return -1;
2009 s = *str;
2010 if (i - off == 0) {
2011 *s = 0;
2012 return 0;
2013 }
2014 if (use_group)
2015 *s++ = '{';
2016 for (i = 0; attr[i]; i++) {
2017 strcpy(s, attr[i]);
2018 s += strlen(s);
2019 *s++ = ',';
2020 }
2021 if (use_group) {
2022 s[-1] = '}';
2023 *s = 0;
2024 } else
2025 s[-1] = 0;
2026 return 0;
2027}
2028
2029__weak bool arch_topdown_check_group(bool *warn)
2030{
2031 *warn = false;
2032 return false;
2033}
2034
2035__weak void arch_topdown_group_warn(void)
2036{
2037}
2038
2cba3ffb
IM
2039/*
2040 * Add default attributes, if there were no attributes specified or
2041 * if -d/--detailed, -d -d or -d -d -d is used:
2042 */
2043static int add_default_attributes(void)
2044{
44b1e60a 2045 int err;
9dec4473 2046 struct perf_event_attr default_attrs0[] = {
b070a547
ACM
2047
2048 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
2049 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
2050 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
2051 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
2052
2053 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
9dec4473
AK
2054};
2055 struct perf_event_attr frontend_attrs[] = {
b070a547 2056 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
9dec4473
AK
2057};
2058 struct perf_event_attr backend_attrs[] = {
b070a547 2059 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
9dec4473
AK
2060};
2061 struct perf_event_attr default_attrs1[] = {
b070a547
ACM
2062 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
2063 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
2064 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
2065
2066};
2067
2068/*
2069 * Detailed stats (-d), covering the L1 and last level data caches:
2070 */
2071 struct perf_event_attr detailed_attrs[] = {
2072
2073 { .type = PERF_TYPE_HW_CACHE,
2074 .config =
2075 PERF_COUNT_HW_CACHE_L1D << 0 |
2076 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2077 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2078
2079 { .type = PERF_TYPE_HW_CACHE,
2080 .config =
2081 PERF_COUNT_HW_CACHE_L1D << 0 |
2082 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2083 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2084
2085 { .type = PERF_TYPE_HW_CACHE,
2086 .config =
2087 PERF_COUNT_HW_CACHE_LL << 0 |
2088 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2089 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2090
2091 { .type = PERF_TYPE_HW_CACHE,
2092 .config =
2093 PERF_COUNT_HW_CACHE_LL << 0 |
2094 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2095 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2096};
2097
2098/*
2099 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
2100 */
2101 struct perf_event_attr very_detailed_attrs[] = {
2102
2103 { .type = PERF_TYPE_HW_CACHE,
2104 .config =
2105 PERF_COUNT_HW_CACHE_L1I << 0 |
2106 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2107 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2108
2109 { .type = PERF_TYPE_HW_CACHE,
2110 .config =
2111 PERF_COUNT_HW_CACHE_L1I << 0 |
2112 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2113 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2114
2115 { .type = PERF_TYPE_HW_CACHE,
2116 .config =
2117 PERF_COUNT_HW_CACHE_DTLB << 0 |
2118 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2119 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2120
2121 { .type = PERF_TYPE_HW_CACHE,
2122 .config =
2123 PERF_COUNT_HW_CACHE_DTLB << 0 |
2124 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2125 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2126
2127 { .type = PERF_TYPE_HW_CACHE,
2128 .config =
2129 PERF_COUNT_HW_CACHE_ITLB << 0 |
2130 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2131 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2132
2133 { .type = PERF_TYPE_HW_CACHE,
2134 .config =
2135 PERF_COUNT_HW_CACHE_ITLB << 0 |
2136 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2137 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2138
2139};
2140
2141/*
2142 * Very, very detailed stats (-d -d -d), adding prefetch events:
2143 */
2144 struct perf_event_attr very_very_detailed_attrs[] = {
2145
2146 { .type = PERF_TYPE_HW_CACHE,
2147 .config =
2148 PERF_COUNT_HW_CACHE_L1D << 0 |
2149 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2150 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2151
2152 { .type = PERF_TYPE_HW_CACHE,
2153 .config =
2154 PERF_COUNT_HW_CACHE_L1D << 0 |
2155 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2156 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2157};
2158
2cba3ffb
IM
2159 /* Set attrs if no event is selected and !null_run: */
2160 if (null_run)
2161 return 0;
2162
4cabc3d1 2163 if (transaction_run) {
4cabc3d1
AK
2164 if (pmu_have_event("cpu", "cycles-ct") &&
2165 pmu_have_event("cpu", "el-start"))
a454742c 2166 err = parse_events(evsel_list, transaction_attrs, NULL);
4cabc3d1 2167 else
a454742c
JO
2168 err = parse_events(evsel_list, transaction_limited_attrs, NULL);
2169 if (err) {
4cabc3d1
AK
2170 fprintf(stderr, "Cannot set up transaction events\n");
2171 return -1;
2172 }
2173 return 0;
2174 }
2175
daefd0bc
KL
2176 if (smi_cost) {
2177 int smi;
2178
2179 if (sysfs__read_int(FREEZE_ON_SMI_PATH, &smi) < 0) {
2180 fprintf(stderr, "freeze_on_smi is not supported.\n");
2181 return -1;
2182 }
2183
2184 if (!smi) {
2185 if (sysfs__write_int(FREEZE_ON_SMI_PATH, 1) < 0) {
2186 fprintf(stderr, "Failed to set freeze_on_smi.\n");
2187 return -1;
2188 }
2189 smi_reset = true;
2190 }
2191
2192 if (pmu_have_event("msr", "aperf") &&
2193 pmu_have_event("msr", "smi")) {
2194 if (!force_metric_only)
2195 metric_only = true;
2196 err = parse_events(evsel_list, smi_cost_attrs, NULL);
2197 } else {
2198 fprintf(stderr, "To measure SMI cost, it needs "
2199 "msr/aperf/, msr/smi/ and cpu/cycles/ support\n");
2200 return -1;
2201 }
2202 if (err) {
2203 fprintf(stderr, "Cannot set up SMI cost events\n");
2204 return -1;
2205 }
2206 return 0;
2207 }
2208
44b1e60a
AK
2209 if (topdown_run) {
2210 char *str = NULL;
2211 bool warn = false;
2212
2213 if (stat_config.aggr_mode != AGGR_GLOBAL &&
2214 stat_config.aggr_mode != AGGR_CORE) {
2215 pr_err("top down event configuration requires --per-core mode\n");
2216 return -1;
2217 }
2218 stat_config.aggr_mode = AGGR_CORE;
2219 if (nr_cgroups || !target__has_cpu(&target)) {
2220 pr_err("top down event configuration requires system-wide mode (-a)\n");
2221 return -1;
2222 }
2223
2224 if (!force_metric_only)
2225 metric_only = true;
2226 if (topdown_filter_events(topdown_attrs, &str,
2227 arch_topdown_check_group(&warn)) < 0) {
2228 pr_err("Out of memory\n");
2229 return -1;
2230 }
2231 if (topdown_attrs[0] && str) {
2232 if (warn)
2233 arch_topdown_group_warn();
2234 err = parse_events(evsel_list, str, NULL);
2235 if (err) {
2236 fprintf(stderr,
2237 "Cannot set up top down events %s: %d\n",
2238 str, err);
2239 free(str);
2240 return -1;
2241 }
2242 } else {
2243 fprintf(stderr, "System does not support topdown\n");
2244 return -1;
2245 }
2246 free(str);
2247 }
2248
2cba3ffb 2249 if (!evsel_list->nr_entries) {
a1f3d567
NK
2250 if (target__has_cpu(&target))
2251 default_attrs0[0].config = PERF_COUNT_SW_CPU_CLOCK;
2252
9dec4473
AK
2253 if (perf_evlist__add_default_attrs(evsel_list, default_attrs0) < 0)
2254 return -1;
2255 if (pmu_have_event("cpu", "stalled-cycles-frontend")) {
2256 if (perf_evlist__add_default_attrs(evsel_list,
2257 frontend_attrs) < 0)
2258 return -1;
2259 }
2260 if (pmu_have_event("cpu", "stalled-cycles-backend")) {
2261 if (perf_evlist__add_default_attrs(evsel_list,
2262 backend_attrs) < 0)
2263 return -1;
2264 }
2265 if (perf_evlist__add_default_attrs(evsel_list, default_attrs1) < 0)
50d08e47 2266 return -1;
2cba3ffb
IM
2267 }
2268
2269 /* Detailed events get appended to the event list: */
2270
2271 if (detailed_run < 1)
2272 return 0;
2273
2274 /* Append detailed run extra attributes: */
79695e1b 2275 if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
50d08e47 2276 return -1;
2cba3ffb
IM
2277
2278 if (detailed_run < 2)
2279 return 0;
2280
2281 /* Append very detailed run extra attributes: */
79695e1b 2282 if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
50d08e47 2283 return -1;
2cba3ffb
IM
2284
2285 if (detailed_run < 3)
2286 return 0;
2287
2288 /* Append very, very detailed run extra attributes: */
79695e1b 2289 return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
2cba3ffb
IM
2290}
2291
8a59f3cc 2292static const char * const stat_record_usage[] = {
4979d0c7
JO
2293 "perf stat record [<options>]",
2294 NULL,
2295};
2296
3ba78bd0
JO
2297static void init_features(struct perf_session *session)
2298{
2299 int feat;
2300
2301 for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
2302 perf_header__set_feat(&session->header, feat);
2303
2304 perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
2305 perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
2306 perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
2307 perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
2308}
2309
4979d0c7
JO
2310static int __cmd_record(int argc, const char **argv)
2311{
2312 struct perf_session *session;
2313 struct perf_data_file *file = &perf_stat.file;
2314
8a59f3cc 2315 argc = parse_options(argc, argv, stat_options, stat_record_usage,
4979d0c7
JO
2316 PARSE_OPT_STOP_AT_NON_OPTION);
2317
2318 if (output_name)
2319 file->path = output_name;
2320
e9d6db8e
JO
2321 if (run_count != 1 || forever) {
2322 pr_err("Cannot use -r option with perf stat record.\n");
2323 return -1;
2324 }
2325
4979d0c7
JO
2326 session = perf_session__new(file, false, NULL);
2327 if (session == NULL) {
2328 pr_err("Perf session creation failed.\n");
2329 return -1;
2330 }
2331
3ba78bd0
JO
2332 init_features(session);
2333
4979d0c7
JO
2334 session->evlist = evsel_list;
2335 perf_stat.session = session;
2336 perf_stat.record = true;
2337 return argc;
2338}
2339
a56f9390
JO
2340static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2341 union perf_event *event,
2342 struct perf_session *session)
2343{
e3b03b6c 2344 struct stat_round_event *stat_round = &event->stat_round;
a56f9390
JO
2345 struct perf_evsel *counter;
2346 struct timespec tsh, *ts = NULL;
2347 const char **argv = session->header.env.cmdline_argv;
2348 int argc = session->header.env.nr_cmdline;
2349
e5cadb93 2350 evlist__for_each_entry(evsel_list, counter)
a56f9390
JO
2351 perf_stat_process_counter(&stat_config, counter);
2352
e3b03b6c
AK
2353 if (stat_round->type == PERF_STAT_ROUND_TYPE__FINAL)
2354 update_stats(&walltime_nsecs_stats, stat_round->time);
a56f9390 2355
e3b03b6c 2356 if (stat_config.interval && stat_round->time) {
bd48c63e
ACM
2357 tsh.tv_sec = stat_round->time / NSEC_PER_SEC;
2358 tsh.tv_nsec = stat_round->time % NSEC_PER_SEC;
a56f9390
JO
2359 ts = &tsh;
2360 }
2361
2362 print_counters(ts, argc, argv);
2363 return 0;
2364}
2365
62ba18ba 2366static
7e6a7998 2367int process_stat_config_event(struct perf_tool *tool,
62ba18ba
JO
2368 union perf_event *event,
2369 struct perf_session *session __maybe_unused)
2370{
68d702f7
JO
2371 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2372
62ba18ba 2373 perf_event__read_stat_config(&stat_config, &event->stat_config);
68d702f7 2374
89af4e05
JO
2375 if (cpu_map__empty(st->cpus)) {
2376 if (st->aggr_mode != AGGR_UNSET)
2377 pr_warning("warning: processing task data, aggregation mode not set\n");
2378 return 0;
2379 }
2380
2381 if (st->aggr_mode != AGGR_UNSET)
2382 stat_config.aggr_mode = st->aggr_mode;
2383
68d702f7
JO
2384 if (perf_stat.file.is_pipe)
2385 perf_stat_init_aggr_mode();
2386 else
2387 perf_stat_init_aggr_mode_file(st);
2388
62ba18ba
JO
2389 return 0;
2390}
2391
1975d36e
JO
2392static int set_maps(struct perf_stat *st)
2393{
2394 if (!st->cpus || !st->threads)
2395 return 0;
2396
2397 if (WARN_ONCE(st->maps_allocated, "stats double allocation\n"))
2398 return -EINVAL;
2399
2400 perf_evlist__set_maps(evsel_list, st->cpus, st->threads);
2401
2402 if (perf_evlist__alloc_stats(evsel_list, true))
2403 return -ENOMEM;
2404
2405 st->maps_allocated = true;
2406 return 0;
2407}
2408
2409static
7e6a7998 2410int process_thread_map_event(struct perf_tool *tool,
1975d36e
JO
2411 union perf_event *event,
2412 struct perf_session *session __maybe_unused)
2413{
2414 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2415
2416 if (st->threads) {
2417 pr_warning("Extra thread map event, ignoring.\n");
2418 return 0;
2419 }
2420
2421 st->threads = thread_map__new_event(&event->thread_map);
2422 if (!st->threads)
2423 return -ENOMEM;
2424
2425 return set_maps(st);
2426}
2427
2428static
7e6a7998 2429int process_cpu_map_event(struct perf_tool *tool,
1975d36e
JO
2430 union perf_event *event,
2431 struct perf_session *session __maybe_unused)
2432{
2433 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2434 struct cpu_map *cpus;
2435
2436 if (st->cpus) {
2437 pr_warning("Extra cpu map event, ignoring.\n");
2438 return 0;
2439 }
2440
2441 cpus = cpu_map__new_data(&event->cpu_map.data);
2442 if (!cpus)
2443 return -ENOMEM;
2444
2445 st->cpus = cpus;
2446 return set_maps(st);
2447}
2448
8a59f3cc 2449static const char * const stat_report_usage[] = {
ba6039b6
JO
2450 "perf stat report [<options>]",
2451 NULL,
2452};
2453
2454static struct perf_stat perf_stat = {
2455 .tool = {
2456 .attr = perf_event__process_attr,
fa6ea781 2457 .event_update = perf_event__process_event_update,
1975d36e
JO
2458 .thread_map = process_thread_map_event,
2459 .cpu_map = process_cpu_map_event,
62ba18ba 2460 .stat_config = process_stat_config_event,
a56f9390
JO
2461 .stat = perf_event__process_stat_event,
2462 .stat_round = process_stat_round_event,
ba6039b6 2463 },
89af4e05 2464 .aggr_mode = AGGR_UNSET,
ba6039b6
JO
2465};
2466
2467static int __cmd_report(int argc, const char **argv)
2468{
2469 struct perf_session *session;
2470 const struct option options[] = {
2471 OPT_STRING('i', "input", &input_name, "file", "input file name"),
89af4e05
JO
2472 OPT_SET_UINT(0, "per-socket", &perf_stat.aggr_mode,
2473 "aggregate counts per processor socket", AGGR_SOCKET),
2474 OPT_SET_UINT(0, "per-core", &perf_stat.aggr_mode,
2475 "aggregate counts per physical processor core", AGGR_CORE),
2476 OPT_SET_UINT('A', "no-aggr", &perf_stat.aggr_mode,
2477 "disable CPU count aggregation", AGGR_NONE),
ba6039b6
JO
2478 OPT_END()
2479 };
2480 struct stat st;
2481 int ret;
2482
8a59f3cc 2483 argc = parse_options(argc, argv, options, stat_report_usage, 0);
ba6039b6
JO
2484
2485 if (!input_name || !strlen(input_name)) {
2486 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
2487 input_name = "-";
2488 else
2489 input_name = "perf.data";
2490 }
2491
2492 perf_stat.file.path = input_name;
2493 perf_stat.file.mode = PERF_DATA_MODE_READ;
2494
2495 session = perf_session__new(&perf_stat.file, false, &perf_stat.tool);
2496 if (session == NULL)
2497 return -1;
2498
2499 perf_stat.session = session;
2500 stat_config.output = stderr;
2501 evsel_list = session->evlist;
2502
2503 ret = perf_session__process_events(session);
2504 if (ret)
2505 return ret;
2506
2507 perf_session__delete(session);
2508 return 0;
2509}
2510
e3ba76de
JO
2511static void setup_system_wide(int forks)
2512{
2513 /*
2514 * Make system wide (-a) the default target if
2515 * no target was specified and one of following
2516 * conditions is met:
2517 *
2518 * - there's no workload specified
2519 * - there is workload specified but all requested
2520 * events are system wide events
2521 */
2522 if (!target__none(&target))
2523 return;
2524
2525 if (!forks)
2526 target.system_wide = true;
2527 else {
2528 struct perf_evsel *counter;
2529
2530 evlist__for_each_entry(evsel_list, counter) {
2531 if (!counter->system_wide)
2532 return;
2533 }
2534
2535 if (evsel_list->nr_entries)
2536 target.system_wide = true;
2537 }
2538}
2539
b0ad8ea6 2540int cmd_stat(int argc, const char **argv)
5242519b 2541{
b070a547
ACM
2542 const char * const stat_usage[] = {
2543 "perf stat [<options>] [<command>]",
2544 NULL
2545 };
cc03c542 2546 int status = -EINVAL, run_idx;
4aa9015f 2547 const char *mode;
5821522e 2548 FILE *output = stderr;
ec0d3d1f 2549 unsigned int interval;
ba6039b6 2550 const char * const stat_subcommands[] = { "record", "report" };
42202dd5 2551
5af52b51
SE
2552 setlocale(LC_ALL, "");
2553
334fe7a3 2554 evsel_list = perf_evlist__new();
361c99a6
ACM
2555 if (evsel_list == NULL)
2556 return -ENOMEM;
2557
1669e509 2558 parse_events__shrink_config_terms();
4979d0c7
JO
2559 argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
2560 (const char **) stat_usage,
2561 PARSE_OPT_STOP_AT_NON_OPTION);
37932c18 2562 perf_stat__collect_metric_expr(evsel_list);
fb4605ba 2563 perf_stat__init_shadow_stats();
4979d0c7 2564
6edb78a2
JO
2565 if (csv_sep) {
2566 csv_output = true;
2567 if (!strcmp(csv_sep, "\\t"))
2568 csv_sep = "\t";
2569 } else
2570 csv_sep = DEFAULT_SEPARATOR;
2571
4979d0c7
JO
2572 if (argc && !strncmp(argv[0], "rec", 3)) {
2573 argc = __cmd_record(argc, argv);
2574 if (argc < 0)
2575 return -1;
ba6039b6
JO
2576 } else if (argc && !strncmp(argv[0], "rep", 3))
2577 return __cmd_report(argc, argv);
d7470b6a 2578
ec0d3d1f
JO
2579 interval = stat_config.interval;
2580
4979d0c7
JO
2581 /*
2582 * For record command the -o is already taken care of.
2583 */
2584 if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
4aa9015f
SE
2585 output = NULL;
2586
56f3bae7
JC
2587 if (output_name && output_fd) {
2588 fprintf(stderr, "cannot use both --output and --log-fd\n");
e0547311
JO
2589 parse_options_usage(stat_usage, stat_options, "o", 1);
2590 parse_options_usage(NULL, stat_options, "log-fd", 0);
cc03c542 2591 goto out;
56f3bae7 2592 }
fc3e4d07 2593
54b50916
AK
2594 if (metric_only && stat_config.aggr_mode == AGGR_THREAD) {
2595 fprintf(stderr, "--metric-only is not supported with --per-thread\n");
2596 goto out;
2597 }
2598
54b50916
AK
2599 if (metric_only && run_count > 1) {
2600 fprintf(stderr, "--metric-only is not supported with -r\n");
2601 goto out;
2602 }
2603
fc3e4d07
SE
2604 if (output_fd < 0) {
2605 fprintf(stderr, "argument to --log-fd must be a > 0\n");
e0547311 2606 parse_options_usage(stat_usage, stat_options, "log-fd", 0);
cc03c542 2607 goto out;
fc3e4d07
SE
2608 }
2609
4aa9015f
SE
2610 if (!output) {
2611 struct timespec tm;
2612 mode = append_file ? "a" : "w";
2613
2614 output = fopen(output_name, mode);
2615 if (!output) {
2616 perror("failed to create output file");
fceda7fe 2617 return -1;
4aa9015f
SE
2618 }
2619 clock_gettime(CLOCK_REALTIME, &tm);
2620 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
fc3e4d07 2621 } else if (output_fd > 0) {
56f3bae7
JC
2622 mode = append_file ? "a" : "w";
2623 output = fdopen(output_fd, mode);
2624 if (!output) {
2625 perror("Failed opening logfd");
2626 return -errno;
2627 }
4aa9015f
SE
2628 }
2629
5821522e
JO
2630 stat_config.output = output;
2631
d7470b6a
SE
2632 /*
2633 * let the spreadsheet do the pretty-printing
2634 */
2635 if (csv_output) {
61a9f324 2636 /* User explicitly passed -B? */
d7470b6a
SE
2637 if (big_num_opt == 1) {
2638 fprintf(stderr, "-B option not supported with -x\n");
e0547311
JO
2639 parse_options_usage(stat_usage, stat_options, "B", 1);
2640 parse_options_usage(NULL, stat_options, "x", 1);
cc03c542 2641 goto out;
d7470b6a
SE
2642 } else /* Nope, so disable big number formatting */
2643 big_num = false;
2644 } else if (big_num_opt == 0) /* User passed --no-big-num */
2645 big_num = false;
2646
e3ba76de 2647 setup_system_wide(argc);
ac3063bd 2648
a7e191c3 2649 if (run_count < 0) {
cc03c542 2650 pr_err("Run count must be a positive number\n");
e0547311 2651 parse_options_usage(stat_usage, stat_options, "r", 1);
cc03c542 2652 goto out;
a7e191c3
FD
2653 } else if (run_count == 0) {
2654 forever = true;
2655 run_count = 1;
2656 }
ddcacfa0 2657
421a50f3 2658 if ((stat_config.aggr_mode == AGGR_THREAD) && !target__has_task(&target)) {
32b8af82
JO
2659 fprintf(stderr, "The --per-thread option is only available "
2660 "when monitoring via -p -t options.\n");
e0547311
JO
2661 parse_options_usage(NULL, stat_options, "p", 1);
2662 parse_options_usage(NULL, stat_options, "t", 1);
32b8af82
JO
2663 goto out;
2664 }
2665
2666 /*
2667 * no_aggr, cgroup are for system-wide only
2668 * --per-thread is aggregated per thread, we dont mix it with cpu mode
2669 */
421a50f3
JO
2670 if (((stat_config.aggr_mode != AGGR_GLOBAL &&
2671 stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) &&
602ad878 2672 !target__has_cpu(&target)) {
023695d9
SE
2673 fprintf(stderr, "both cgroup and no-aggregation "
2674 "modes only available in system-wide mode\n");
2675
e0547311
JO
2676 parse_options_usage(stat_usage, stat_options, "G", 1);
2677 parse_options_usage(NULL, stat_options, "A", 1);
2678 parse_options_usage(NULL, stat_options, "a", 1);
cc03c542 2679 goto out;
d7e7a451
SE
2680 }
2681
2cba3ffb
IM
2682 if (add_default_attributes())
2683 goto out;
ddcacfa0 2684
602ad878 2685 target__validate(&target);
5c98d466 2686
77a6f014 2687 if (perf_evlist__create_maps(evsel_list, &target) < 0) {
602ad878 2688 if (target__has_task(&target)) {
77a6f014 2689 pr_err("Problems finding threads of monitor\n");
e0547311
JO
2690 parse_options_usage(stat_usage, stat_options, "p", 1);
2691 parse_options_usage(NULL, stat_options, "t", 1);
602ad878 2692 } else if (target__has_cpu(&target)) {
77a6f014 2693 perror("failed to parse CPUs map");
e0547311
JO
2694 parse_options_usage(stat_usage, stat_options, "C", 1);
2695 parse_options_usage(NULL, stat_options, "a", 1);
cc03c542
NK
2696 }
2697 goto out;
60d567e2 2698 }
32b8af82
JO
2699
2700 /*
2701 * Initialize thread_map with comm names,
2702 * so we could print it out on output.
2703 */
421a50f3 2704 if (stat_config.aggr_mode == AGGR_THREAD)
32b8af82
JO
2705 thread_map__read_comms(evsel_list->threads);
2706
13370a9b 2707 if (interval && interval < 100) {
19afd104
KL
2708 if (interval < 10) {
2709 pr_err("print interval must be >= 10ms\n");
e0547311 2710 parse_options_usage(stat_usage, stat_options, "I", 1);
19afd104
KL
2711 goto out;
2712 } else
2713 pr_warning("print interval < 100ms. "
2714 "The overhead percentage could be high in some cases. "
2715 "Please proceed with caution.\n");
13370a9b 2716 }
c45c6ea2 2717
d134ffb9 2718 if (perf_evlist__alloc_stats(evsel_list, interval))
03ad9747 2719 goto out;
d6d901c2 2720
86ee6e18 2721 if (perf_stat_init_aggr_mode())
03ad9747 2722 goto out;
86ee6e18 2723
58d7e993
IM
2724 /*
2725 * We dont want to block the signals - that would cause
2726 * child tasks to inherit that and Ctrl-C would not work.
2727 * What we want is for Ctrl-C to work in the exec()-ed
2728 * task, but being ignored by perf stat itself:
2729 */
f7b7c26e 2730 atexit(sig_atexit);
a7e191c3
FD
2731 if (!forever)
2732 signal(SIGINT, skip_signal);
13370a9b 2733 signal(SIGCHLD, skip_signal);
58d7e993
IM
2734 signal(SIGALRM, skip_signal);
2735 signal(SIGABRT, skip_signal);
2736
42202dd5 2737 status = 0;
a7e191c3 2738 for (run_idx = 0; forever || run_idx < run_count; run_idx++) {
bb963e16 2739 if (run_count != 1 && verbose > 0)
4aa9015f
SE
2740 fprintf(output, "[ perf stat: executing run #%d ... ]\n",
2741 run_idx + 1);
f9cef0a9 2742
42202dd5 2743 status = run_perf_stat(argc, argv);
a7e191c3 2744 if (forever && status != -1) {
d4f63a47 2745 print_counters(NULL, argc, argv);
254ecbc7 2746 perf_stat__reset_stats();
a7e191c3 2747 }
42202dd5
IM
2748 }
2749
a7e191c3 2750 if (!forever && status != -1 && !interval)
d4f63a47 2751 print_counters(NULL, argc, argv);
d134ffb9 2752
4979d0c7
JO
2753 if (STAT_RECORD) {
2754 /*
2755 * We synthesize the kernel mmap record just so that older tools
2756 * don't emit warnings about not being able to resolve symbols
2757 * due to /proc/sys/kernel/kptr_restrict settings and instear provide
2758 * a saner message about no samples being in the perf.data file.
2759 *
2760 * This also serves to suppress a warning about f_header.data.size == 0
8b99b1a4
JO
2761 * in header.c at the moment 'perf stat record' gets introduced, which
2762 * is not really needed once we start adding the stat specific PERF_RECORD_
2763 * records, but the need to suppress the kptr_restrict messages in older
2764 * tools remain -acme
4979d0c7
JO
2765 */
2766 int fd = perf_data_file__fd(&perf_stat.file);
2767 int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
2768 process_synthesized_event,
2769 &perf_stat.session->machines.host);
2770 if (err) {
2771 pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
2772 "older tools may produce warnings about this file\n.");
2773 }
2774
7aad0c32
JO
2775 if (!interval) {
2776 if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats.max, FINAL))
2777 pr_err("failed to write stat round event\n");
2778 }
2779
664c98d4
JO
2780 if (!perf_stat.file.is_pipe) {
2781 perf_stat.session->header.data_size += perf_stat.bytes_written;
2782 perf_session__write_header(perf_stat.session, evsel_list, fd, true);
2783 }
4979d0c7
JO
2784
2785 perf_session__delete(perf_stat.session);
2786 }
2787
544c2ae7 2788 perf_stat__exit_aggr_mode();
d134ffb9 2789 perf_evlist__free_stats(evsel_list);
0015e2e1 2790out:
daefd0bc
KL
2791 if (smi_cost && smi_reset)
2792 sysfs__write_int(FREEZE_ON_SMI_PATH, 0);
2793
0015e2e1 2794 perf_evlist__delete(evsel_list);
42202dd5 2795 return status;
ddcacfa0 2796}