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