4 * Builtin stat command: Give a precise performance counters summary
5 * overview about any workload, CPU or specific PID.
9 $ perf stat ./hackbench 10
13 Performance counter stats for './hackbench 10':
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
27 0.154822978 seconds time elapsed
30 * Copyright (C) 2008-2011, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
32 * Improvements and fixes by:
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>
39 * Jaswinder Singh Rajput <jaswinder@kernel.org>
41 * Released under the GPL v2. (and only v2, not any later version)
46 #include "util/cgroup.h"
47 #include "util/util.h"
48 #include <subcmd/parse-options.h>
49 #include "util/parse-events.h"
51 #include "util/event.h"
52 #include "util/evlist.h"
53 #include "util/evsel.h"
54 #include "util/debug.h"
55 #include "util/drv_configs.h"
56 #include "util/color.h"
57 #include "util/stat.h"
58 #include "util/header.h"
59 #include "util/cpumap.h"
60 #include "util/thread.h"
61 #include "util/thread_map.h"
62 #include "util/counts.h"
63 #include "util/group.h"
64 #include "util/session.h"
65 #include "util/tool.h"
66 #include "util/group.h"
69 #include <linux/time64.h>
70 #include <api/fs/fs.h>
72 #include <sys/prctl.h>
76 #define DEFAULT_SEPARATOR " "
77 #define CNTR_NOT_SUPPORTED "<not supported>"
78 #define CNTR_NOT_COUNTED "<not counted>"
80 static void print_counters(struct timespec
*ts
, int argc
, const char **argv
);
82 /* Default events used for perf stat -T */
83 static const char *transaction_attrs
= {
95 /* More limited version when the CPU does not have all events. */
96 static const char * transaction_limited_attrs
= {
106 static const char * topdown_attrs
[] = {
107 "topdown-total-slots",
108 "topdown-slots-retired",
109 "topdown-recovery-bubbles",
110 "topdown-fetch-bubbles",
111 "topdown-slots-issued",
115 static struct perf_evlist
*evsel_list
;
117 static struct target target
= {
121 typedef int (*aggr_get_id_t
)(struct cpu_map
*m
, int cpu
);
123 static int run_count
= 1;
124 static bool no_inherit
= false;
125 static volatile pid_t child_pid
= -1;
126 static bool null_run
= false;
127 static int detailed_run
= 0;
128 static bool transaction_run
;
129 static bool topdown_run
= false;
130 static bool big_num
= true;
131 static int big_num_opt
= -1;
132 static const char *csv_sep
= NULL
;
133 static bool csv_output
= false;
134 static bool group
= false;
135 static const char *pre_cmd
= NULL
;
136 static const char *post_cmd
= NULL
;
137 static bool sync_run
= false;
138 static unsigned int initial_delay
= 0;
139 static unsigned int unit_width
= 4; /* strlen("unit") */
140 static bool forever
= false;
141 static bool metric_only
= false;
142 static bool force_metric_only
= false;
143 static struct timespec ref_time
;
144 static struct cpu_map
*aggr_map
;
145 static aggr_get_id_t aggr_get_id
;
146 static bool append_file
;
147 static const char *output_name
;
148 static int output_fd
;
149 static int print_free_counters_hint
;
153 struct perf_data_file file
;
154 struct perf_session
*session
;
156 struct perf_tool tool
;
158 struct cpu_map
*cpus
;
159 struct thread_map
*threads
;
160 enum aggr_mode aggr_mode
;
163 static struct perf_stat perf_stat
;
164 #define STAT_RECORD perf_stat.record
166 static volatile int done
= 0;
168 static struct perf_stat_config stat_config
= {
169 .aggr_mode
= AGGR_GLOBAL
,
173 static inline void diff_timespec(struct timespec
*r
, struct timespec
*a
,
176 r
->tv_sec
= a
->tv_sec
- b
->tv_sec
;
177 if (a
->tv_nsec
< b
->tv_nsec
) {
178 r
->tv_nsec
= a
->tv_nsec
+ NSEC_PER_SEC
- b
->tv_nsec
;
181 r
->tv_nsec
= a
->tv_nsec
- b
->tv_nsec
;
185 static void perf_stat__reset_stats(void)
187 perf_evlist__reset_stats(evsel_list
);
188 perf_stat__reset_shadow_stats();
191 static int create_perf_stat_counter(struct perf_evsel
*evsel
)
193 struct perf_event_attr
*attr
= &evsel
->attr
;
195 if (stat_config
.scale
)
196 attr
->read_format
= PERF_FORMAT_TOTAL_TIME_ENABLED
|
197 PERF_FORMAT_TOTAL_TIME_RUNNING
;
199 attr
->inherit
= !no_inherit
;
202 * Some events get initialized with sample_(period/type) set,
203 * like tracepoints. Clear it up for counting.
205 attr
->sample_period
= 0;
208 * But set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
209 * while avoiding that older tools show confusing messages.
211 * However for pipe sessions we need to keep it zero,
212 * because script's perf_evsel__check_attr is triggered
213 * by attr->sample_type != 0, and we can't run it on
216 if (!(STAT_RECORD
&& perf_stat
.file
.is_pipe
))
217 attr
->sample_type
= PERF_SAMPLE_IDENTIFIER
;
220 * Disabling all counters initially, they will be enabled
221 * either manually by us or by kernel via enable_on_exec
224 if (perf_evsel__is_group_leader(evsel
)) {
228 * In case of initial_delay we enable tracee
231 if (target__none(&target
) && !initial_delay
)
232 attr
->enable_on_exec
= 1;
235 if (target__has_cpu(&target
))
236 return perf_evsel__open_per_cpu(evsel
, perf_evsel__cpus(evsel
));
238 return perf_evsel__open_per_thread(evsel
, evsel_list
->threads
);
242 * Does the counter have nsecs as a unit?
244 static inline int nsec_counter(struct perf_evsel
*evsel
)
246 if (perf_evsel__match(evsel
, SOFTWARE
, SW_CPU_CLOCK
) ||
247 perf_evsel__match(evsel
, SOFTWARE
, SW_TASK_CLOCK
))
253 static int process_synthesized_event(struct perf_tool
*tool __maybe_unused
,
254 union perf_event
*event
,
255 struct perf_sample
*sample __maybe_unused
,
256 struct machine
*machine __maybe_unused
)
258 if (perf_data_file__write(&perf_stat
.file
, event
, event
->header
.size
) < 0) {
259 pr_err("failed to write perf data, error: %m\n");
263 perf_stat
.bytes_written
+= event
->header
.size
;
267 static int write_stat_round_event(u64 tm
, u64 type
)
269 return perf_event__synthesize_stat_round(NULL
, tm
, type
,
270 process_synthesized_event
,
274 #define WRITE_STAT_ROUND_EVENT(time, interval) \
275 write_stat_round_event(time, PERF_STAT_ROUND_TYPE__ ## interval)
277 #define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
280 perf_evsel__write_stat_event(struct perf_evsel
*counter
, u32 cpu
, u32 thread
,
281 struct perf_counts_values
*count
)
283 struct perf_sample_id
*sid
= SID(counter
, cpu
, thread
);
285 return perf_event__synthesize_stat(NULL
, cpu
, thread
, sid
->id
, count
,
286 process_synthesized_event
, NULL
);
290 * Read out the results of a single counter:
291 * do not aggregate counts across CPUs in system-wide mode
293 static int read_counter(struct perf_evsel
*counter
)
295 int nthreads
= thread_map__nr(evsel_list
->threads
);
296 int ncpus
, cpu
, thread
;
298 if (target__has_cpu(&target
))
299 ncpus
= perf_evsel__nr_cpus(counter
);
303 if (!counter
->supported
)
306 if (counter
->system_wide
)
309 for (thread
= 0; thread
< nthreads
; thread
++) {
310 for (cpu
= 0; cpu
< ncpus
; cpu
++) {
311 struct perf_counts_values
*count
;
313 count
= perf_counts(counter
->counts
, cpu
, thread
);
314 if (perf_evsel__read(counter
, cpu
, thread
, count
)) {
315 counter
->counts
->scaled
= -1;
316 perf_counts(counter
->counts
, cpu
, thread
)->ena
= 0;
317 perf_counts(counter
->counts
, cpu
, thread
)->run
= 0;
322 if (perf_evsel__write_stat_event(counter
, cpu
, thread
, count
)) {
323 pr_err("failed to write stat event\n");
329 fprintf(stat_config
.output
,
330 "%s: %d: %" PRIu64
" %" PRIu64
" %" PRIu64
"\n",
331 perf_evsel__name(counter
),
333 count
->val
, count
->ena
, count
->run
);
341 static void read_counters(void)
343 struct perf_evsel
*counter
;
346 evlist__for_each_entry(evsel_list
, counter
) {
347 ret
= read_counter(counter
);
349 pr_debug("failed to read counter %s\n", counter
->name
);
351 if (ret
== 0 && perf_stat_process_counter(&stat_config
, counter
))
352 pr_warning("failed to process counter %s\n", counter
->name
);
356 static void process_interval(void)
358 struct timespec ts
, rs
;
362 clock_gettime(CLOCK_MONOTONIC
, &ts
);
363 diff_timespec(&rs
, &ts
, &ref_time
);
366 if (WRITE_STAT_ROUND_EVENT(rs
.tv_sec
* NSEC_PER_SEC
+ rs
.tv_nsec
, INTERVAL
))
367 pr_err("failed to write stat round event\n");
370 print_counters(&rs
, 0, NULL
);
373 static void enable_counters(void)
376 usleep(initial_delay
* USEC_PER_MSEC
);
379 * We need to enable counters only if:
380 * - we don't have tracee (attaching to task or cpu)
381 * - we have initial delay configured
383 if (!target__none(&target
) || initial_delay
)
384 perf_evlist__enable(evsel_list
);
387 static void disable_counters(void)
390 * If we don't have tracee (attaching to task or cpu), counters may
391 * still be running. To get accurate group ratios, we must stop groups
392 * from counting before reading their constituent counters.
394 if (!target__none(&target
))
395 perf_evlist__disable(evsel_list
);
398 static volatile int workload_exec_errno
;
401 * perf_evlist__prepare_workload will send a SIGUSR1
402 * if the fork fails, since we asked by setting its
403 * want_signal to true.
405 static void workload_exec_failed_signal(int signo __maybe_unused
, siginfo_t
*info
,
406 void *ucontext __maybe_unused
)
408 workload_exec_errno
= info
->si_value
.sival_int
;
411 static bool has_unit(struct perf_evsel
*counter
)
413 return counter
->unit
&& *counter
->unit
;
416 static bool has_scale(struct perf_evsel
*counter
)
418 return counter
->scale
!= 1;
421 static int perf_stat_synthesize_config(bool is_pipe
)
423 struct perf_evsel
*counter
;
427 err
= perf_event__synthesize_attrs(NULL
, perf_stat
.session
,
428 process_synthesized_event
);
430 pr_err("Couldn't synthesize attrs.\n");
436 * Synthesize other events stuff not carried within
437 * attr event - unit, scale, name
439 evlist__for_each_entry(evsel_list
, counter
) {
440 if (!counter
->supported
)
444 * Synthesize unit and scale only if it's defined.
446 if (has_unit(counter
)) {
447 err
= perf_event__synthesize_event_update_unit(NULL
, counter
, process_synthesized_event
);
449 pr_err("Couldn't synthesize evsel unit.\n");
454 if (has_scale(counter
)) {
455 err
= perf_event__synthesize_event_update_scale(NULL
, counter
, process_synthesized_event
);
457 pr_err("Couldn't synthesize evsel scale.\n");
462 if (counter
->own_cpus
) {
463 err
= perf_event__synthesize_event_update_cpus(NULL
, counter
, process_synthesized_event
);
465 pr_err("Couldn't synthesize evsel scale.\n");
471 * Name is needed only for pipe output,
472 * perf.data carries event names.
475 err
= perf_event__synthesize_event_update_name(NULL
, counter
, process_synthesized_event
);
477 pr_err("Couldn't synthesize evsel name.\n");
483 err
= perf_event__synthesize_thread_map2(NULL
, evsel_list
->threads
,
484 process_synthesized_event
,
487 pr_err("Couldn't synthesize thread map.\n");
491 err
= perf_event__synthesize_cpu_map(NULL
, evsel_list
->cpus
,
492 process_synthesized_event
, NULL
);
494 pr_err("Couldn't synthesize thread map.\n");
498 err
= perf_event__synthesize_stat_config(NULL
, &stat_config
,
499 process_synthesized_event
, NULL
);
501 pr_err("Couldn't synthesize config.\n");
508 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
510 static int __store_counter_ids(struct perf_evsel
*counter
,
511 struct cpu_map
*cpus
,
512 struct thread_map
*threads
)
516 for (cpu
= 0; cpu
< cpus
->nr
; cpu
++) {
517 for (thread
= 0; thread
< threads
->nr
; thread
++) {
518 int fd
= FD(counter
, cpu
, thread
);
520 if (perf_evlist__id_add_fd(evsel_list
, counter
,
521 cpu
, thread
, fd
) < 0)
529 static int store_counter_ids(struct perf_evsel
*counter
)
531 struct cpu_map
*cpus
= counter
->cpus
;
532 struct thread_map
*threads
= counter
->threads
;
534 if (perf_evsel__alloc_id(counter
, cpus
->nr
, threads
->nr
))
537 return __store_counter_ids(counter
, cpus
, threads
);
540 static int __run_perf_stat(int argc
, const char **argv
)
542 int interval
= stat_config
.interval
;
544 unsigned long long t0
, t1
;
545 struct perf_evsel
*counter
;
549 const bool forks
= (argc
> 0);
550 bool is_pipe
= STAT_RECORD
? perf_stat
.file
.is_pipe
: false;
551 struct perf_evsel_config_term
*err_term
;
554 ts
.tv_sec
= interval
/ USEC_PER_MSEC
;
555 ts
.tv_nsec
= (interval
% USEC_PER_MSEC
) * NSEC_PER_MSEC
;
562 if (perf_evlist__prepare_workload(evsel_list
, &target
, argv
, is_pipe
,
563 workload_exec_failed_signal
) < 0) {
564 perror("failed to prepare workload");
567 child_pid
= evsel_list
->workload
.pid
;
571 perf_evlist__set_leader(evsel_list
);
573 evlist__for_each_entry(evsel_list
, counter
) {
575 if (create_perf_stat_counter(counter
) < 0) {
577 * PPC returns ENXIO for HW counters until 2.6.37
578 * (behavior changed with commit b0a873e).
580 if (errno
== EINVAL
|| errno
== ENOSYS
||
581 errno
== ENOENT
|| errno
== EOPNOTSUPP
||
584 ui__warning("%s event is not supported by the kernel.\n",
585 perf_evsel__name(counter
));
586 counter
->supported
= false;
588 if ((counter
->leader
!= counter
) ||
589 !(counter
->leader
->nr_members
> 1))
591 } else if (perf_evsel__fallback(counter
, errno
, msg
, sizeof(msg
))) {
593 ui__warning("%s\n", msg
);
597 perf_evsel__open_strerror(counter
, &target
,
598 errno
, msg
, sizeof(msg
));
599 ui__error("%s\n", msg
);
602 kill(child_pid
, SIGTERM
);
606 counter
->supported
= true;
608 l
= strlen(counter
->unit
);
612 if (STAT_RECORD
&& store_counter_ids(counter
))
616 if (perf_evlist__apply_filters(evsel_list
, &counter
)) {
617 error("failed to set filter \"%s\" on event %s with %d (%s)\n",
618 counter
->filter
, perf_evsel__name(counter
), errno
,
619 str_error_r(errno
, msg
, sizeof(msg
)));
623 if (perf_evlist__apply_drv_configs(evsel_list
, &counter
, &err_term
)) {
624 error("failed to set config \"%s\" on event %s with %d (%s)\n",
625 err_term
->val
.drv_cfg
, perf_evsel__name(counter
), errno
,
626 str_error_r(errno
, msg
, sizeof(msg
)));
631 int err
, fd
= perf_data_file__fd(&perf_stat
.file
);
634 err
= perf_header__write_pipe(perf_data_file__fd(&perf_stat
.file
));
636 err
= perf_session__write_header(perf_stat
.session
, evsel_list
,
643 err
= perf_stat_synthesize_config(is_pipe
);
649 * Enable counters and exec the command:
652 clock_gettime(CLOCK_MONOTONIC
, &ref_time
);
655 perf_evlist__start_workload(evsel_list
);
659 while (!waitpid(child_pid
, &status
, WNOHANG
)) {
660 nanosleep(&ts
, NULL
);
666 if (workload_exec_errno
) {
667 const char *emsg
= str_error_r(workload_exec_errno
, msg
, sizeof(msg
));
668 pr_err("Workload failed: %s\n", emsg
);
672 if (WIFSIGNALED(status
))
673 psignal(WTERMSIG(status
), argv
[0]);
677 nanosleep(&ts
, NULL
);
687 update_stats(&walltime_nsecs_stats
, t1
- t0
);
690 * Closing a group leader splits the group, and as we only disable
691 * group leaders, results in remaining events becoming enabled. To
692 * avoid arbitrary skew, we must read all counters before closing any
696 perf_evlist__close(evsel_list
);
698 return WEXITSTATUS(status
);
701 static int run_perf_stat(int argc
, const char **argv
)
706 ret
= system(pre_cmd
);
714 ret
= __run_perf_stat(argc
, argv
);
719 ret
= system(post_cmd
);
727 static void print_running(u64 run
, u64 ena
)
730 fprintf(stat_config
.output
, "%s%" PRIu64
"%s%.2f",
734 ena
? 100.0 * run
/ ena
: 100.0);
735 } else if (run
!= ena
) {
736 fprintf(stat_config
.output
, " (%.2f%%)", 100.0 * run
/ ena
);
740 static void print_noise_pct(double total
, double avg
)
742 double pct
= rel_stddev_stats(total
, avg
);
745 fprintf(stat_config
.output
, "%s%.2f%%", csv_sep
, pct
);
747 fprintf(stat_config
.output
, " ( +-%6.2f%% )", pct
);
750 static void print_noise(struct perf_evsel
*evsel
, double avg
)
752 struct perf_stat_evsel
*ps
;
758 print_noise_pct(stddev_stats(&ps
->res_stats
[0]), avg
);
761 static void aggr_printout(struct perf_evsel
*evsel
, int id
, int nr
)
763 switch (stat_config
.aggr_mode
) {
765 fprintf(stat_config
.output
, "S%d-C%*d%s%*d%s",
766 cpu_map__id_to_socket(id
),
768 cpu_map__id_to_cpu(id
),
775 fprintf(stat_config
.output
, "S%*d%s%*d%s",
784 fprintf(stat_config
.output
, "CPU%*d%s",
786 perf_evsel__cpus(evsel
)->map
[id
], csv_sep
);
789 fprintf(stat_config
.output
, "%*s-%*d%s",
791 thread_map__comm(evsel
->threads
, id
),
793 thread_map__pid(evsel
->threads
, id
),
809 struct perf_evsel
*evsel
;
812 #define METRIC_LEN 35
814 static void new_line_std(void *ctx
)
816 struct outstate
*os
= ctx
;
821 static void do_new_line_std(struct outstate
*os
)
824 fputs(os
->prefix
, os
->fh
);
825 aggr_printout(os
->evsel
, os
->id
, os
->nr
);
826 if (stat_config
.aggr_mode
== AGGR_NONE
)
827 fprintf(os
->fh
, " ");
828 fprintf(os
->fh
, " ");
831 static void print_metric_std(void *ctx
, const char *color
, const char *fmt
,
832 const char *unit
, double val
)
834 struct outstate
*os
= ctx
;
837 bool newline
= os
->newline
;
841 if (unit
== NULL
|| fmt
== NULL
) {
842 fprintf(out
, "%-*s", METRIC_LEN
, "");
849 n
= fprintf(out
, " # ");
851 n
+= color_fprintf(out
, color
, fmt
, val
);
853 n
+= fprintf(out
, fmt
, val
);
854 fprintf(out
, " %-*s", METRIC_LEN
- n
- 1, unit
);
857 static void new_line_csv(void *ctx
)
859 struct outstate
*os
= ctx
;
864 fprintf(os
->fh
, "%s%s", os
->prefix
, csv_sep
);
865 aggr_printout(os
->evsel
, os
->id
, os
->nr
);
866 for (i
= 0; i
< os
->nfields
; i
++)
867 fputs(csv_sep
, os
->fh
);
870 static void print_metric_csv(void *ctx
,
871 const char *color __maybe_unused
,
872 const char *fmt
, const char *unit
, double val
)
874 struct outstate
*os
= ctx
;
876 char buf
[64], *vals
, *ends
;
878 if (unit
== NULL
|| fmt
== NULL
) {
879 fprintf(out
, "%s%s", csv_sep
, csv_sep
);
882 snprintf(buf
, sizeof(buf
), fmt
, val
);
884 while (isspace(*vals
))
887 while (isdigit(*ends
) || *ends
== '.')
890 while (isspace(*unit
))
892 fprintf(out
, "%s%s%s%s", csv_sep
, vals
, csv_sep
, unit
);
895 #define METRIC_ONLY_LEN 20
897 /* Filter out some columns that don't work well in metrics only mode */
899 static bool valid_only_metric(const char *unit
)
903 if (strstr(unit
, "/sec") ||
904 strstr(unit
, "hz") ||
905 strstr(unit
, "Hz") ||
906 strstr(unit
, "CPUs utilized"))
911 static const char *fixunit(char *buf
, struct perf_evsel
*evsel
,
914 if (!strncmp(unit
, "of all", 6)) {
915 snprintf(buf
, 1024, "%s %s", perf_evsel__name(evsel
),
922 static void print_metric_only(void *ctx
, const char *color
, const char *fmt
,
923 const char *unit
, double val
)
925 struct outstate
*os
= ctx
;
929 unsigned mlen
= METRIC_ONLY_LEN
;
931 if (!valid_only_metric(unit
))
933 unit
= fixunit(buf
, os
->evsel
, unit
);
935 n
= color_fprintf(out
, color
, fmt
, val
);
937 n
= fprintf(out
, fmt
, val
);
938 if (n
> METRIC_ONLY_LEN
)
940 if (mlen
< strlen(unit
))
941 mlen
= strlen(unit
) + 1;
942 fprintf(out
, "%*s", mlen
- n
, "");
945 static void print_metric_only_csv(void *ctx
, const char *color __maybe_unused
,
947 const char *unit
, double val
)
949 struct outstate
*os
= ctx
;
951 char buf
[64], *vals
, *ends
;
954 if (!valid_only_metric(unit
))
956 unit
= fixunit(tbuf
, os
->evsel
, unit
);
957 snprintf(buf
, sizeof buf
, fmt
, val
);
959 while (isspace(*vals
))
962 while (isdigit(*ends
) || *ends
== '.')
965 fprintf(out
, "%s%s", vals
, csv_sep
);
968 static void new_line_metric(void *ctx __maybe_unused
)
972 static void print_metric_header(void *ctx
, const char *color __maybe_unused
,
973 const char *fmt __maybe_unused
,
974 const char *unit
, double val __maybe_unused
)
976 struct outstate
*os
= ctx
;
979 if (!valid_only_metric(unit
))
981 unit
= fixunit(tbuf
, os
->evsel
, unit
);
983 fprintf(os
->fh
, "%s%s", unit
, csv_sep
);
985 fprintf(os
->fh
, "%-*s ", METRIC_ONLY_LEN
, unit
);
988 static void nsec_printout(int id
, int nr
, struct perf_evsel
*evsel
, double avg
)
990 FILE *output
= stat_config
.output
;
991 double msecs
= avg
/ NSEC_PER_MSEC
;
992 const char *fmt_v
, *fmt_n
;
995 fmt_v
= csv_output
? "%.6f%s" : "%18.6f%s";
996 fmt_n
= csv_output
? "%s" : "%-25s";
998 aggr_printout(evsel
, id
, nr
);
1000 scnprintf(name
, sizeof(name
), "%s%s",
1001 perf_evsel__name(evsel
), csv_output
? "" : " (msec)");
1003 fprintf(output
, fmt_v
, msecs
, csv_sep
);
1006 fprintf(output
, "%s%s", evsel
->unit
, csv_sep
);
1008 fprintf(output
, "%-*s%s", unit_width
, evsel
->unit
, csv_sep
);
1010 fprintf(output
, fmt_n
, name
);
1013 fprintf(output
, "%s%s", csv_sep
, evsel
->cgrp
->name
);
1016 static int first_shadow_cpu(struct perf_evsel
*evsel
, int id
)
1023 if (stat_config
.aggr_mode
== AGGR_NONE
)
1026 if (stat_config
.aggr_mode
== AGGR_GLOBAL
)
1029 for (i
= 0; i
< perf_evsel__nr_cpus(evsel
); i
++) {
1030 int cpu2
= perf_evsel__cpus(evsel
)->map
[i
];
1032 if (aggr_get_id(evsel_list
->cpus
, cpu2
) == id
)
1038 static void abs_printout(int id
, int nr
, struct perf_evsel
*evsel
, double avg
)
1040 FILE *output
= stat_config
.output
;
1041 double sc
= evsel
->scale
;
1045 fmt
= floor(sc
) != sc
? "%.2f%s" : "%.0f%s";
1048 fmt
= floor(sc
) != sc
? "%'18.2f%s" : "%'18.0f%s";
1050 fmt
= floor(sc
) != sc
? "%18.2f%s" : "%18.0f%s";
1053 aggr_printout(evsel
, id
, nr
);
1055 fprintf(output
, fmt
, avg
, csv_sep
);
1058 fprintf(output
, "%-*s%s",
1059 csv_output
? 0 : unit_width
,
1060 evsel
->unit
, csv_sep
);
1062 fprintf(output
, "%-*s", csv_output
? 0 : 25, perf_evsel__name(evsel
));
1065 fprintf(output
, "%s%s", csv_sep
, evsel
->cgrp
->name
);
1068 static void printout(int id
, int nr
, struct perf_evsel
*counter
, double uval
,
1069 char *prefix
, u64 run
, u64 ena
, double noise
)
1071 struct perf_stat_output_ctx out
;
1072 struct outstate os
= {
1073 .fh
= stat_config
.output
,
1074 .prefix
= prefix
? prefix
: "",
1079 print_metric_t pm
= print_metric_std
;
1083 nl
= new_line_metric
;
1085 pm
= print_metric_only_csv
;
1087 pm
= print_metric_only
;
1091 if (csv_output
&& !metric_only
) {
1092 static int aggr_fields
[] = {
1100 pm
= print_metric_csv
;
1103 os
.nfields
+= aggr_fields
[stat_config
.aggr_mode
];
1107 if (run
== 0 || ena
== 0 || counter
->counts
->scaled
== -1) {
1109 pm(&os
, NULL
, "", "", 0);
1112 aggr_printout(counter
, id
, nr
);
1114 fprintf(stat_config
.output
, "%*s%s",
1115 csv_output
? 0 : 18,
1116 counter
->supported
? CNTR_NOT_COUNTED
: CNTR_NOT_SUPPORTED
,
1119 if (counter
->supported
)
1120 print_free_counters_hint
= 1;
1122 fprintf(stat_config
.output
, "%-*s%s",
1123 csv_output
? 0 : unit_width
,
1124 counter
->unit
, csv_sep
);
1126 fprintf(stat_config
.output
, "%*s",
1127 csv_output
? 0 : -25,
1128 perf_evsel__name(counter
));
1131 fprintf(stat_config
.output
, "%s%s",
1132 csv_sep
, counter
->cgrp
->name
);
1135 pm(&os
, NULL
, NULL
, "", 0);
1136 print_noise(counter
, noise
);
1137 print_running(run
, ena
);
1139 pm(&os
, NULL
, NULL
, "", 0);
1145 else if (nsec_counter(counter
))
1146 nsec_printout(id
, nr
, counter
, uval
);
1148 abs_printout(id
, nr
, counter
, uval
);
1150 out
.print_metric
= pm
;
1154 if (csv_output
&& !metric_only
) {
1155 print_noise(counter
, noise
);
1156 print_running(run
, ena
);
1159 perf_stat__print_shadow_stats(counter
, uval
,
1160 first_shadow_cpu(counter
, id
),
1162 if (!csv_output
&& !metric_only
) {
1163 print_noise(counter
, noise
);
1164 print_running(run
, ena
);
1168 static void aggr_update_shadow(void)
1172 struct perf_evsel
*counter
;
1174 for (s
= 0; s
< aggr_map
->nr
; s
++) {
1175 id
= aggr_map
->map
[s
];
1176 evlist__for_each_entry(evsel_list
, counter
) {
1178 for (cpu
= 0; cpu
< perf_evsel__nr_cpus(counter
); cpu
++) {
1179 s2
= aggr_get_id(evsel_list
->cpus
, cpu
);
1182 val
+= perf_counts(counter
->counts
, cpu
, 0)->val
;
1184 val
= val
* counter
->scale
;
1185 perf_stat__update_shadow_stats(counter
, &val
,
1186 first_shadow_cpu(counter
, id
));
1191 static void print_aggr(char *prefix
)
1193 FILE *output
= stat_config
.output
;
1194 struct perf_evsel
*counter
;
1195 int cpu
, s
, s2
, id
, nr
;
1200 if (!(aggr_map
|| aggr_get_id
))
1203 aggr_update_shadow();
1206 * With metric_only everything is on a single line.
1207 * Without each counter has its own line.
1209 for (s
= 0; s
< aggr_map
->nr
; s
++) {
1210 if (prefix
&& metric_only
)
1211 fprintf(output
, "%s", prefix
);
1213 id
= aggr_map
->map
[s
];
1215 evlist__for_each_entry(evsel_list
, counter
) {
1216 val
= ena
= run
= 0;
1218 for (cpu
= 0; cpu
< perf_evsel__nr_cpus(counter
); cpu
++) {
1219 s2
= aggr_get_id(perf_evsel__cpus(counter
), cpu
);
1222 val
+= perf_counts(counter
->counts
, cpu
, 0)->val
;
1223 ena
+= perf_counts(counter
->counts
, cpu
, 0)->ena
;
1224 run
+= perf_counts(counter
->counts
, cpu
, 0)->run
;
1227 if (first
&& metric_only
) {
1229 aggr_printout(counter
, id
, nr
);
1231 if (prefix
&& !metric_only
)
1232 fprintf(output
, "%s", prefix
);
1234 uval
= val
* counter
->scale
;
1235 printout(id
, nr
, counter
, uval
, prefix
, run
, ena
, 1.0);
1237 fputc('\n', output
);
1240 fputc('\n', output
);
1244 static void print_aggr_thread(struct perf_evsel
*counter
, char *prefix
)
1246 FILE *output
= stat_config
.output
;
1247 int nthreads
= thread_map__nr(counter
->threads
);
1248 int ncpus
= cpu_map__nr(counter
->cpus
);
1252 for (thread
= 0; thread
< nthreads
; thread
++) {
1253 u64 ena
= 0, run
= 0, val
= 0;
1255 for (cpu
= 0; cpu
< ncpus
; cpu
++) {
1256 val
+= perf_counts(counter
->counts
, cpu
, thread
)->val
;
1257 ena
+= perf_counts(counter
->counts
, cpu
, thread
)->ena
;
1258 run
+= perf_counts(counter
->counts
, cpu
, thread
)->run
;
1262 fprintf(output
, "%s", prefix
);
1264 uval
= val
* counter
->scale
;
1265 printout(thread
, 0, counter
, uval
, prefix
, run
, ena
, 1.0);
1266 fputc('\n', output
);
1271 * Print out the results of a single counter:
1272 * aggregated counts in system-wide mode
1274 static void print_counter_aggr(struct perf_evsel
*counter
, char *prefix
)
1276 FILE *output
= stat_config
.output
;
1277 struct perf_stat_evsel
*ps
= counter
->priv
;
1278 double avg
= avg_stats(&ps
->res_stats
[0]);
1280 double avg_enabled
, avg_running
;
1282 avg_enabled
= avg_stats(&ps
->res_stats
[1]);
1283 avg_running
= avg_stats(&ps
->res_stats
[2]);
1285 if (prefix
&& !metric_only
)
1286 fprintf(output
, "%s", prefix
);
1288 uval
= avg
* counter
->scale
;
1289 printout(-1, 0, counter
, uval
, prefix
, avg_running
, avg_enabled
, avg
);
1291 fprintf(output
, "\n");
1295 * Print out the results of a single counter:
1296 * does not use aggregated count in system-wide
1298 static void print_counter(struct perf_evsel
*counter
, char *prefix
)
1300 FILE *output
= stat_config
.output
;
1305 for (cpu
= 0; cpu
< perf_evsel__nr_cpus(counter
); cpu
++) {
1306 val
= perf_counts(counter
->counts
, cpu
, 0)->val
;
1307 ena
= perf_counts(counter
->counts
, cpu
, 0)->ena
;
1308 run
= perf_counts(counter
->counts
, cpu
, 0)->run
;
1311 fprintf(output
, "%s", prefix
);
1313 uval
= val
* counter
->scale
;
1314 printout(cpu
, 0, counter
, uval
, prefix
, run
, ena
, 1.0);
1316 fputc('\n', output
);
1320 static void print_no_aggr_metric(char *prefix
)
1324 struct perf_evsel
*counter
;
1328 nrcpus
= evsel_list
->cpus
->nr
;
1329 for (cpu
= 0; cpu
< nrcpus
; cpu
++) {
1333 fputs(prefix
, stat_config
.output
);
1334 evlist__for_each_entry(evsel_list
, counter
) {
1336 aggr_printout(counter
, cpu
, 0);
1339 val
= perf_counts(counter
->counts
, cpu
, 0)->val
;
1340 ena
= perf_counts(counter
->counts
, cpu
, 0)->ena
;
1341 run
= perf_counts(counter
->counts
, cpu
, 0)->run
;
1343 uval
= val
* counter
->scale
;
1344 printout(cpu
, 0, counter
, uval
, prefix
, run
, ena
, 1.0);
1346 fputc('\n', stat_config
.output
);
1350 static int aggr_header_lens
[] = {
1358 static const char *aggr_header_csv
[] = {
1359 [AGGR_CORE
] = "core,cpus,",
1360 [AGGR_SOCKET
] = "socket,cpus",
1361 [AGGR_NONE
] = "cpu,",
1362 [AGGR_THREAD
] = "comm-pid,",
1366 static void print_metric_headers(const char *prefix
, bool no_indent
)
1368 struct perf_stat_output_ctx out
;
1369 struct perf_evsel
*counter
;
1370 struct outstate os
= {
1371 .fh
= stat_config
.output
1375 fprintf(stat_config
.output
, "%s", prefix
);
1377 if (!csv_output
&& !no_indent
)
1378 fprintf(stat_config
.output
, "%*s",
1379 aggr_header_lens
[stat_config
.aggr_mode
], "");
1381 if (stat_config
.interval
)
1382 fputs("time,", stat_config
.output
);
1383 fputs(aggr_header_csv
[stat_config
.aggr_mode
],
1384 stat_config
.output
);
1387 /* Print metrics headers only */
1388 evlist__for_each_entry(evsel_list
, counter
) {
1391 out
.print_metric
= print_metric_header
;
1392 out
.new_line
= new_line_metric
;
1394 perf_stat__print_shadow_stats(counter
, 0,
1398 fputc('\n', stat_config
.output
);
1401 static void print_interval(char *prefix
, struct timespec
*ts
)
1403 FILE *output
= stat_config
.output
;
1404 static int num_print_interval
;
1406 sprintf(prefix
, "%6lu.%09lu%s", ts
->tv_sec
, ts
->tv_nsec
, csv_sep
);
1408 if (num_print_interval
== 0 && !csv_output
) {
1409 switch (stat_config
.aggr_mode
) {
1411 fprintf(output
, "# time socket cpus");
1413 fprintf(output
, " counts %*s events\n", unit_width
, "unit");
1416 fprintf(output
, "# time core cpus");
1418 fprintf(output
, " counts %*s events\n", unit_width
, "unit");
1421 fprintf(output
, "# time CPU");
1423 fprintf(output
, " counts %*s events\n", unit_width
, "unit");
1426 fprintf(output
, "# time comm-pid");
1428 fprintf(output
, " counts %*s events\n", unit_width
, "unit");
1432 fprintf(output
, "# time");
1434 fprintf(output
, " counts %*s events\n", unit_width
, "unit");
1440 if (num_print_interval
== 0 && metric_only
)
1441 print_metric_headers(" ", true);
1442 if (++num_print_interval
== 25)
1443 num_print_interval
= 0;
1446 static void print_header(int argc
, const char **argv
)
1448 FILE *output
= stat_config
.output
;
1454 fprintf(output
, "\n");
1455 fprintf(output
, " Performance counter stats for ");
1456 if (target
.system_wide
)
1457 fprintf(output
, "\'system wide");
1458 else if (target
.cpu_list
)
1459 fprintf(output
, "\'CPU(s) %s", target
.cpu_list
);
1460 else if (!target__has_task(&target
)) {
1461 fprintf(output
, "\'%s", argv
? argv
[0] : "pipe");
1462 for (i
= 1; argv
&& (i
< argc
); i
++)
1463 fprintf(output
, " %s", argv
[i
]);
1464 } else if (target
.pid
)
1465 fprintf(output
, "process id \'%s", target
.pid
);
1467 fprintf(output
, "thread id \'%s", target
.tid
);
1469 fprintf(output
, "\'");
1471 fprintf(output
, " (%d runs)", run_count
);
1472 fprintf(output
, ":\n\n");
1476 static void print_footer(void)
1478 FILE *output
= stat_config
.output
;
1481 fprintf(output
, "\n");
1482 fprintf(output
, " %17.9f seconds time elapsed",
1483 avg_stats(&walltime_nsecs_stats
) / NSEC_PER_SEC
);
1484 if (run_count
> 1) {
1485 fprintf(output
, " ");
1486 print_noise_pct(stddev_stats(&walltime_nsecs_stats
),
1487 avg_stats(&walltime_nsecs_stats
));
1489 fprintf(output
, "\n\n");
1491 if (print_free_counters_hint
)
1493 "Some events weren't counted. Try disabling the NMI watchdog:\n"
1494 " echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1496 " echo 1 > /proc/sys/kernel/nmi_watchdog\n");
1499 static void print_counters(struct timespec
*ts
, int argc
, const char **argv
)
1501 int interval
= stat_config
.interval
;
1502 struct perf_evsel
*counter
;
1503 char buf
[64], *prefix
= NULL
;
1505 /* Do not print anything if we record to the pipe. */
1506 if (STAT_RECORD
&& perf_stat
.file
.is_pipe
)
1510 print_interval(prefix
= buf
, ts
);
1512 print_header(argc
, argv
);
1515 static int num_print_iv
;
1517 if (num_print_iv
== 0 && !interval
)
1518 print_metric_headers(prefix
, false);
1519 if (num_print_iv
++ == 25)
1521 if (stat_config
.aggr_mode
== AGGR_GLOBAL
&& prefix
)
1522 fprintf(stat_config
.output
, "%s", prefix
);
1525 switch (stat_config
.aggr_mode
) {
1531 evlist__for_each_entry(evsel_list
, counter
)
1532 print_aggr_thread(counter
, prefix
);
1535 evlist__for_each_entry(evsel_list
, counter
)
1536 print_counter_aggr(counter
, prefix
);
1538 fputc('\n', stat_config
.output
);
1542 print_no_aggr_metric(prefix
);
1544 evlist__for_each_entry(evsel_list
, counter
)
1545 print_counter(counter
, prefix
);
1553 if (!interval
&& !csv_output
)
1556 fflush(stat_config
.output
);
1559 static volatile int signr
= -1;
1561 static void skip_signal(int signo
)
1563 if ((child_pid
== -1) || stat_config
.interval
)
1568 * render child_pid harmless
1569 * won't send SIGTERM to a random
1570 * process in case of race condition
1571 * and fast PID recycling
1576 static void sig_atexit(void)
1581 * avoid race condition with SIGCHLD handler
1582 * in skip_signal() which is modifying child_pid
1583 * goal is to avoid send SIGTERM to a random
1587 sigaddset(&set
, SIGCHLD
);
1588 sigprocmask(SIG_BLOCK
, &set
, &oset
);
1590 if (child_pid
!= -1)
1591 kill(child_pid
, SIGTERM
);
1593 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
1598 signal(signr
, SIG_DFL
);
1599 kill(getpid(), signr
);
1602 static int stat__set_big_num(const struct option
*opt __maybe_unused
,
1603 const char *s __maybe_unused
, int unset
)
1605 big_num_opt
= unset
? 0 : 1;
1609 static int enable_metric_only(const struct option
*opt __maybe_unused
,
1610 const char *s __maybe_unused
, int unset
)
1612 force_metric_only
= true;
1613 metric_only
= !unset
;
1617 static const struct option stat_options
[] = {
1618 OPT_BOOLEAN('T', "transaction", &transaction_run
,
1619 "hardware transaction statistics"),
1620 OPT_CALLBACK('e', "event", &evsel_list
, "event",
1621 "event selector. use 'perf list' to list available events",
1622 parse_events_option
),
1623 OPT_CALLBACK(0, "filter", &evsel_list
, "filter",
1624 "event filter", parse_filter
),
1625 OPT_BOOLEAN('i', "no-inherit", &no_inherit
,
1626 "child tasks do not inherit counters"),
1627 OPT_STRING('p', "pid", &target
.pid
, "pid",
1628 "stat events on existing process id"),
1629 OPT_STRING('t', "tid", &target
.tid
, "tid",
1630 "stat events on existing thread id"),
1631 OPT_BOOLEAN('a', "all-cpus", &target
.system_wide
,
1632 "system-wide collection from all CPUs"),
1633 OPT_BOOLEAN('g', "group", &group
,
1634 "put the counters into a counter group"),
1635 OPT_BOOLEAN('c', "scale", &stat_config
.scale
, "scale/normalize counters"),
1636 OPT_INCR('v', "verbose", &verbose
,
1637 "be more verbose (show counter open errors, etc)"),
1638 OPT_INTEGER('r', "repeat", &run_count
,
1639 "repeat command and print average + stddev (max: 100, forever: 0)"),
1640 OPT_BOOLEAN('n', "null", &null_run
,
1641 "null run - dont start any counters"),
1642 OPT_INCR('d', "detailed", &detailed_run
,
1643 "detailed run - start a lot of events"),
1644 OPT_BOOLEAN('S', "sync", &sync_run
,
1645 "call sync() before starting a run"),
1646 OPT_CALLBACK_NOOPT('B', "big-num", NULL
, NULL
,
1647 "print large numbers with thousands\' separators",
1649 OPT_STRING('C', "cpu", &target
.cpu_list
, "cpu",
1650 "list of cpus to monitor in system-wide"),
1651 OPT_SET_UINT('A', "no-aggr", &stat_config
.aggr_mode
,
1652 "disable CPU count aggregation", AGGR_NONE
),
1653 OPT_STRING('x', "field-separator", &csv_sep
, "separator",
1654 "print counts with custom separator"),
1655 OPT_CALLBACK('G', "cgroup", &evsel_list
, "name",
1656 "monitor event in cgroup name only", parse_cgroups
),
1657 OPT_STRING('o', "output", &output_name
, "file", "output file name"),
1658 OPT_BOOLEAN(0, "append", &append_file
, "append to the output file"),
1659 OPT_INTEGER(0, "log-fd", &output_fd
,
1660 "log output to fd, instead of stderr"),
1661 OPT_STRING(0, "pre", &pre_cmd
, "command",
1662 "command to run prior to the measured command"),
1663 OPT_STRING(0, "post", &post_cmd
, "command",
1664 "command to run after to the measured command"),
1665 OPT_UINTEGER('I', "interval-print", &stat_config
.interval
,
1666 "print counts at regular interval in ms (>= 10)"),
1667 OPT_SET_UINT(0, "per-socket", &stat_config
.aggr_mode
,
1668 "aggregate counts per processor socket", AGGR_SOCKET
),
1669 OPT_SET_UINT(0, "per-core", &stat_config
.aggr_mode
,
1670 "aggregate counts per physical processor core", AGGR_CORE
),
1671 OPT_SET_UINT(0, "per-thread", &stat_config
.aggr_mode
,
1672 "aggregate counts per thread", AGGR_THREAD
),
1673 OPT_UINTEGER('D', "delay", &initial_delay
,
1674 "ms to wait before starting measurement after program start"),
1675 OPT_CALLBACK_NOOPT(0, "metric-only", &metric_only
, NULL
,
1676 "Only print computed metrics. No raw values", enable_metric_only
),
1677 OPT_BOOLEAN(0, "topdown", &topdown_run
,
1678 "measure topdown level 1 statistics"),
1682 static int perf_stat__get_socket(struct cpu_map
*map
, int cpu
)
1684 return cpu_map__get_socket(map
, cpu
, NULL
);
1687 static int perf_stat__get_core(struct cpu_map
*map
, int cpu
)
1689 return cpu_map__get_core(map
, cpu
, NULL
);
1692 static int cpu_map__get_max(struct cpu_map
*map
)
1696 for (i
= 0; i
< map
->nr
; i
++) {
1697 if (map
->map
[i
] > max
)
1704 static struct cpu_map
*cpus_aggr_map
;
1706 static int perf_stat__get_aggr(aggr_get_id_t get_id
, struct cpu_map
*map
, int idx
)
1713 cpu
= map
->map
[idx
];
1715 if (cpus_aggr_map
->map
[cpu
] == -1)
1716 cpus_aggr_map
->map
[cpu
] = get_id(map
, idx
);
1718 return cpus_aggr_map
->map
[cpu
];
1721 static int perf_stat__get_socket_cached(struct cpu_map
*map
, int idx
)
1723 return perf_stat__get_aggr(perf_stat__get_socket
, map
, idx
);
1726 static int perf_stat__get_core_cached(struct cpu_map
*map
, int idx
)
1728 return perf_stat__get_aggr(perf_stat__get_core
, map
, idx
);
1731 static int perf_stat_init_aggr_mode(void)
1735 switch (stat_config
.aggr_mode
) {
1737 if (cpu_map__build_socket_map(evsel_list
->cpus
, &aggr_map
)) {
1738 perror("cannot build socket map");
1741 aggr_get_id
= perf_stat__get_socket_cached
;
1744 if (cpu_map__build_core_map(evsel_list
->cpus
, &aggr_map
)) {
1745 perror("cannot build core map");
1748 aggr_get_id
= perf_stat__get_core_cached
;
1759 * The evsel_list->cpus is the base we operate on,
1760 * taking the highest cpu number to be the size of
1761 * the aggregation translate cpumap.
1763 nr
= cpu_map__get_max(evsel_list
->cpus
);
1764 cpus_aggr_map
= cpu_map__empty_new(nr
+ 1);
1765 return cpus_aggr_map
? 0 : -ENOMEM
;
1768 static void perf_stat__exit_aggr_mode(void)
1770 cpu_map__put(aggr_map
);
1771 cpu_map__put(cpus_aggr_map
);
1773 cpus_aggr_map
= NULL
;
1776 static inline int perf_env__get_cpu(struct perf_env
*env
, struct cpu_map
*map
, int idx
)
1783 cpu
= map
->map
[idx
];
1785 if (cpu
>= env
->nr_cpus_online
)
1791 static int perf_env__get_socket(struct cpu_map
*map
, int idx
, void *data
)
1793 struct perf_env
*env
= data
;
1794 int cpu
= perf_env__get_cpu(env
, map
, idx
);
1796 return cpu
== -1 ? -1 : env
->cpu
[cpu
].socket_id
;
1799 static int perf_env__get_core(struct cpu_map
*map
, int idx
, void *data
)
1801 struct perf_env
*env
= data
;
1802 int core
= -1, cpu
= perf_env__get_cpu(env
, map
, idx
);
1805 int socket_id
= env
->cpu
[cpu
].socket_id
;
1808 * Encode socket in upper 16 bits
1809 * core_id is relative to socket, and
1810 * we need a global id. So we combine
1813 core
= (socket_id
<< 16) | (env
->cpu
[cpu
].core_id
& 0xffff);
1819 static int perf_env__build_socket_map(struct perf_env
*env
, struct cpu_map
*cpus
,
1820 struct cpu_map
**sockp
)
1822 return cpu_map__build_map(cpus
, sockp
, perf_env__get_socket
, env
);
1825 static int perf_env__build_core_map(struct perf_env
*env
, struct cpu_map
*cpus
,
1826 struct cpu_map
**corep
)
1828 return cpu_map__build_map(cpus
, corep
, perf_env__get_core
, env
);
1831 static int perf_stat__get_socket_file(struct cpu_map
*map
, int idx
)
1833 return perf_env__get_socket(map
, idx
, &perf_stat
.session
->header
.env
);
1836 static int perf_stat__get_core_file(struct cpu_map
*map
, int idx
)
1838 return perf_env__get_core(map
, idx
, &perf_stat
.session
->header
.env
);
1841 static int perf_stat_init_aggr_mode_file(struct perf_stat
*st
)
1843 struct perf_env
*env
= &st
->session
->header
.env
;
1845 switch (stat_config
.aggr_mode
) {
1847 if (perf_env__build_socket_map(env
, evsel_list
->cpus
, &aggr_map
)) {
1848 perror("cannot build socket map");
1851 aggr_get_id
= perf_stat__get_socket_file
;
1854 if (perf_env__build_core_map(env
, evsel_list
->cpus
, &aggr_map
)) {
1855 perror("cannot build core map");
1858 aggr_get_id
= perf_stat__get_core_file
;
1871 static int topdown_filter_events(const char **attr
, char **str
, bool use_group
)
1878 for (i
= 0; attr
[i
]; i
++) {
1879 if (pmu_have_event("cpu", attr
[i
])) {
1880 len
+= strlen(attr
[i
]) + 1;
1881 attr
[i
- off
] = attr
[i
];
1885 attr
[i
- off
] = NULL
;
1887 *str
= malloc(len
+ 1 + 2);
1897 for (i
= 0; attr
[i
]; i
++) {
1910 __weak
bool arch_topdown_check_group(bool *warn
)
1916 __weak
void arch_topdown_group_warn(void)
1921 * Add default attributes, if there were no attributes specified or
1922 * if -d/--detailed, -d -d or -d -d -d is used:
1924 static int add_default_attributes(void)
1927 struct perf_event_attr default_attrs0
[] = {
1929 { .type
= PERF_TYPE_SOFTWARE
, .config
= PERF_COUNT_SW_TASK_CLOCK
},
1930 { .type
= PERF_TYPE_SOFTWARE
, .config
= PERF_COUNT_SW_CONTEXT_SWITCHES
},
1931 { .type
= PERF_TYPE_SOFTWARE
, .config
= PERF_COUNT_SW_CPU_MIGRATIONS
},
1932 { .type
= PERF_TYPE_SOFTWARE
, .config
= PERF_COUNT_SW_PAGE_FAULTS
},
1934 { .type
= PERF_TYPE_HARDWARE
, .config
= PERF_COUNT_HW_CPU_CYCLES
},
1936 struct perf_event_attr frontend_attrs
[] = {
1937 { .type
= PERF_TYPE_HARDWARE
, .config
= PERF_COUNT_HW_STALLED_CYCLES_FRONTEND
},
1939 struct perf_event_attr backend_attrs
[] = {
1940 { .type
= PERF_TYPE_HARDWARE
, .config
= PERF_COUNT_HW_STALLED_CYCLES_BACKEND
},
1942 struct perf_event_attr default_attrs1
[] = {
1943 { .type
= PERF_TYPE_HARDWARE
, .config
= PERF_COUNT_HW_INSTRUCTIONS
},
1944 { .type
= PERF_TYPE_HARDWARE
, .config
= PERF_COUNT_HW_BRANCH_INSTRUCTIONS
},
1945 { .type
= PERF_TYPE_HARDWARE
, .config
= PERF_COUNT_HW_BRANCH_MISSES
},
1950 * Detailed stats (-d), covering the L1 and last level data caches:
1952 struct perf_event_attr detailed_attrs
[] = {
1954 { .type
= PERF_TYPE_HW_CACHE
,
1956 PERF_COUNT_HW_CACHE_L1D
<< 0 |
1957 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
1958 (PERF_COUNT_HW_CACHE_RESULT_ACCESS
<< 16) },
1960 { .type
= PERF_TYPE_HW_CACHE
,
1962 PERF_COUNT_HW_CACHE_L1D
<< 0 |
1963 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
1964 (PERF_COUNT_HW_CACHE_RESULT_MISS
<< 16) },
1966 { .type
= PERF_TYPE_HW_CACHE
,
1968 PERF_COUNT_HW_CACHE_LL
<< 0 |
1969 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
1970 (PERF_COUNT_HW_CACHE_RESULT_ACCESS
<< 16) },
1972 { .type
= PERF_TYPE_HW_CACHE
,
1974 PERF_COUNT_HW_CACHE_LL
<< 0 |
1975 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
1976 (PERF_COUNT_HW_CACHE_RESULT_MISS
<< 16) },
1980 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
1982 struct perf_event_attr very_detailed_attrs
[] = {
1984 { .type
= PERF_TYPE_HW_CACHE
,
1986 PERF_COUNT_HW_CACHE_L1I
<< 0 |
1987 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
1988 (PERF_COUNT_HW_CACHE_RESULT_ACCESS
<< 16) },
1990 { .type
= PERF_TYPE_HW_CACHE
,
1992 PERF_COUNT_HW_CACHE_L1I
<< 0 |
1993 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
1994 (PERF_COUNT_HW_CACHE_RESULT_MISS
<< 16) },
1996 { .type
= PERF_TYPE_HW_CACHE
,
1998 PERF_COUNT_HW_CACHE_DTLB
<< 0 |
1999 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
2000 (PERF_COUNT_HW_CACHE_RESULT_ACCESS
<< 16) },
2002 { .type
= PERF_TYPE_HW_CACHE
,
2004 PERF_COUNT_HW_CACHE_DTLB
<< 0 |
2005 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
2006 (PERF_COUNT_HW_CACHE_RESULT_MISS
<< 16) },
2008 { .type
= PERF_TYPE_HW_CACHE
,
2010 PERF_COUNT_HW_CACHE_ITLB
<< 0 |
2011 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
2012 (PERF_COUNT_HW_CACHE_RESULT_ACCESS
<< 16) },
2014 { .type
= PERF_TYPE_HW_CACHE
,
2016 PERF_COUNT_HW_CACHE_ITLB
<< 0 |
2017 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
2018 (PERF_COUNT_HW_CACHE_RESULT_MISS
<< 16) },
2023 * Very, very detailed stats (-d -d -d), adding prefetch events:
2025 struct perf_event_attr very_very_detailed_attrs
[] = {
2027 { .type
= PERF_TYPE_HW_CACHE
,
2029 PERF_COUNT_HW_CACHE_L1D
<< 0 |
2030 (PERF_COUNT_HW_CACHE_OP_PREFETCH
<< 8) |
2031 (PERF_COUNT_HW_CACHE_RESULT_ACCESS
<< 16) },
2033 { .type
= PERF_TYPE_HW_CACHE
,
2035 PERF_COUNT_HW_CACHE_L1D
<< 0 |
2036 (PERF_COUNT_HW_CACHE_OP_PREFETCH
<< 8) |
2037 (PERF_COUNT_HW_CACHE_RESULT_MISS
<< 16) },
2040 /* Set attrs if no event is selected and !null_run: */
2044 if (transaction_run
) {
2045 struct parse_events_error errinfo
;
2047 if (pmu_have_event("cpu", "cycles-ct") &&
2048 pmu_have_event("cpu", "el-start"))
2049 err
= parse_events(evsel_list
, transaction_attrs
,
2052 err
= parse_events(evsel_list
,
2053 transaction_limited_attrs
,
2056 fprintf(stderr
, "Cannot set up transaction events\n");
2066 if (stat_config
.aggr_mode
!= AGGR_GLOBAL
&&
2067 stat_config
.aggr_mode
!= AGGR_CORE
) {
2068 pr_err("top down event configuration requires --per-core mode\n");
2071 stat_config
.aggr_mode
= AGGR_CORE
;
2072 if (nr_cgroups
|| !target__has_cpu(&target
)) {
2073 pr_err("top down event configuration requires system-wide mode (-a)\n");
2077 if (!force_metric_only
)
2079 if (topdown_filter_events(topdown_attrs
, &str
,
2080 arch_topdown_check_group(&warn
)) < 0) {
2081 pr_err("Out of memory\n");
2084 if (topdown_attrs
[0] && str
) {
2086 arch_topdown_group_warn();
2087 err
= parse_events(evsel_list
, str
, NULL
);
2090 "Cannot set up top down events %s: %d\n",
2096 fprintf(stderr
, "System does not support topdown\n");
2102 if (!evsel_list
->nr_entries
) {
2103 if (target__has_cpu(&target
))
2104 default_attrs0
[0].config
= PERF_COUNT_SW_CPU_CLOCK
;
2106 if (perf_evlist__add_default_attrs(evsel_list
, default_attrs0
) < 0)
2108 if (pmu_have_event("cpu", "stalled-cycles-frontend")) {
2109 if (perf_evlist__add_default_attrs(evsel_list
,
2110 frontend_attrs
) < 0)
2113 if (pmu_have_event("cpu", "stalled-cycles-backend")) {
2114 if (perf_evlist__add_default_attrs(evsel_list
,
2118 if (perf_evlist__add_default_attrs(evsel_list
, default_attrs1
) < 0)
2122 /* Detailed events get appended to the event list: */
2124 if (detailed_run
< 1)
2127 /* Append detailed run extra attributes: */
2128 if (perf_evlist__add_default_attrs(evsel_list
, detailed_attrs
) < 0)
2131 if (detailed_run
< 2)
2134 /* Append very detailed run extra attributes: */
2135 if (perf_evlist__add_default_attrs(evsel_list
, very_detailed_attrs
) < 0)
2138 if (detailed_run
< 3)
2141 /* Append very, very detailed run extra attributes: */
2142 return perf_evlist__add_default_attrs(evsel_list
, very_very_detailed_attrs
);
2145 static const char * const stat_record_usage
[] = {
2146 "perf stat record [<options>]",
2150 static void init_features(struct perf_session
*session
)
2154 for (feat
= HEADER_FIRST_FEATURE
; feat
< HEADER_LAST_FEATURE
; feat
++)
2155 perf_header__set_feat(&session
->header
, feat
);
2157 perf_header__clear_feat(&session
->header
, HEADER_BUILD_ID
);
2158 perf_header__clear_feat(&session
->header
, HEADER_TRACING_DATA
);
2159 perf_header__clear_feat(&session
->header
, HEADER_BRANCH_STACK
);
2160 perf_header__clear_feat(&session
->header
, HEADER_AUXTRACE
);
2163 static int __cmd_record(int argc
, const char **argv
)
2165 struct perf_session
*session
;
2166 struct perf_data_file
*file
= &perf_stat
.file
;
2168 argc
= parse_options(argc
, argv
, stat_options
, stat_record_usage
,
2169 PARSE_OPT_STOP_AT_NON_OPTION
);
2172 file
->path
= output_name
;
2174 if (run_count
!= 1 || forever
) {
2175 pr_err("Cannot use -r option with perf stat record.\n");
2179 session
= perf_session__new(file
, false, NULL
);
2180 if (session
== NULL
) {
2181 pr_err("Perf session creation failed.\n");
2185 init_features(session
);
2187 session
->evlist
= evsel_list
;
2188 perf_stat
.session
= session
;
2189 perf_stat
.record
= true;
2193 static int process_stat_round_event(struct perf_tool
*tool __maybe_unused
,
2194 union perf_event
*event
,
2195 struct perf_session
*session
)
2197 struct stat_round_event
*stat_round
= &event
->stat_round
;
2198 struct perf_evsel
*counter
;
2199 struct timespec tsh
, *ts
= NULL
;
2200 const char **argv
= session
->header
.env
.cmdline_argv
;
2201 int argc
= session
->header
.env
.nr_cmdline
;
2203 evlist__for_each_entry(evsel_list
, counter
)
2204 perf_stat_process_counter(&stat_config
, counter
);
2206 if (stat_round
->type
== PERF_STAT_ROUND_TYPE__FINAL
)
2207 update_stats(&walltime_nsecs_stats
, stat_round
->time
);
2209 if (stat_config
.interval
&& stat_round
->time
) {
2210 tsh
.tv_sec
= stat_round
->time
/ NSEC_PER_SEC
;
2211 tsh
.tv_nsec
= stat_round
->time
% NSEC_PER_SEC
;
2215 print_counters(ts
, argc
, argv
);
2220 int process_stat_config_event(struct perf_tool
*tool __maybe_unused
,
2221 union perf_event
*event
,
2222 struct perf_session
*session __maybe_unused
)
2224 struct perf_stat
*st
= container_of(tool
, struct perf_stat
, tool
);
2226 perf_event__read_stat_config(&stat_config
, &event
->stat_config
);
2228 if (cpu_map__empty(st
->cpus
)) {
2229 if (st
->aggr_mode
!= AGGR_UNSET
)
2230 pr_warning("warning: processing task data, aggregation mode not set\n");
2234 if (st
->aggr_mode
!= AGGR_UNSET
)
2235 stat_config
.aggr_mode
= st
->aggr_mode
;
2237 if (perf_stat
.file
.is_pipe
)
2238 perf_stat_init_aggr_mode();
2240 perf_stat_init_aggr_mode_file(st
);
2245 static int set_maps(struct perf_stat
*st
)
2247 if (!st
->cpus
|| !st
->threads
)
2250 if (WARN_ONCE(st
->maps_allocated
, "stats double allocation\n"))
2253 perf_evlist__set_maps(evsel_list
, st
->cpus
, st
->threads
);
2255 if (perf_evlist__alloc_stats(evsel_list
, true))
2258 st
->maps_allocated
= true;
2263 int process_thread_map_event(struct perf_tool
*tool __maybe_unused
,
2264 union perf_event
*event
,
2265 struct perf_session
*session __maybe_unused
)
2267 struct perf_stat
*st
= container_of(tool
, struct perf_stat
, tool
);
2270 pr_warning("Extra thread map event, ignoring.\n");
2274 st
->threads
= thread_map__new_event(&event
->thread_map
);
2278 return set_maps(st
);
2282 int process_cpu_map_event(struct perf_tool
*tool __maybe_unused
,
2283 union perf_event
*event
,
2284 struct perf_session
*session __maybe_unused
)
2286 struct perf_stat
*st
= container_of(tool
, struct perf_stat
, tool
);
2287 struct cpu_map
*cpus
;
2290 pr_warning("Extra cpu map event, ignoring.\n");
2294 cpus
= cpu_map__new_data(&event
->cpu_map
.data
);
2299 return set_maps(st
);
2302 static const char * const stat_report_usage
[] = {
2303 "perf stat report [<options>]",
2307 static struct perf_stat perf_stat
= {
2309 .attr
= perf_event__process_attr
,
2310 .event_update
= perf_event__process_event_update
,
2311 .thread_map
= process_thread_map_event
,
2312 .cpu_map
= process_cpu_map_event
,
2313 .stat_config
= process_stat_config_event
,
2314 .stat
= perf_event__process_stat_event
,
2315 .stat_round
= process_stat_round_event
,
2317 .aggr_mode
= AGGR_UNSET
,
2320 static int __cmd_report(int argc
, const char **argv
)
2322 struct perf_session
*session
;
2323 const struct option options
[] = {
2324 OPT_STRING('i', "input", &input_name
, "file", "input file name"),
2325 OPT_SET_UINT(0, "per-socket", &perf_stat
.aggr_mode
,
2326 "aggregate counts per processor socket", AGGR_SOCKET
),
2327 OPT_SET_UINT(0, "per-core", &perf_stat
.aggr_mode
,
2328 "aggregate counts per physical processor core", AGGR_CORE
),
2329 OPT_SET_UINT('A', "no-aggr", &perf_stat
.aggr_mode
,
2330 "disable CPU count aggregation", AGGR_NONE
),
2336 argc
= parse_options(argc
, argv
, options
, stat_report_usage
, 0);
2338 if (!input_name
|| !strlen(input_name
)) {
2339 if (!fstat(STDIN_FILENO
, &st
) && S_ISFIFO(st
.st_mode
))
2342 input_name
= "perf.data";
2345 perf_stat
.file
.path
= input_name
;
2346 perf_stat
.file
.mode
= PERF_DATA_MODE_READ
;
2348 session
= perf_session__new(&perf_stat
.file
, false, &perf_stat
.tool
);
2349 if (session
== NULL
)
2352 perf_stat
.session
= session
;
2353 stat_config
.output
= stderr
;
2354 evsel_list
= session
->evlist
;
2356 ret
= perf_session__process_events(session
);
2360 perf_session__delete(session
);
2364 int cmd_stat(int argc
, const char **argv
, const char *prefix __maybe_unused
)
2366 const char * const stat_usage
[] = {
2367 "perf stat [<options>] [<command>]",
2370 int status
= -EINVAL
, run_idx
;
2372 FILE *output
= stderr
;
2373 unsigned int interval
;
2374 const char * const stat_subcommands
[] = { "record", "report" };
2376 setlocale(LC_ALL
, "");
2378 evsel_list
= perf_evlist__new();
2379 if (evsel_list
== NULL
)
2382 parse_events__shrink_config_terms();
2383 argc
= parse_options_subcommand(argc
, argv
, stat_options
, stat_subcommands
,
2384 (const char **) stat_usage
,
2385 PARSE_OPT_STOP_AT_NON_OPTION
);
2386 perf_stat__init_shadow_stats();
2390 if (!strcmp(csv_sep
, "\\t"))
2393 csv_sep
= DEFAULT_SEPARATOR
;
2395 if (argc
&& !strncmp(argv
[0], "rec", 3)) {
2396 argc
= __cmd_record(argc
, argv
);
2399 } else if (argc
&& !strncmp(argv
[0], "rep", 3))
2400 return __cmd_report(argc
, argv
);
2402 interval
= stat_config
.interval
;
2405 * For record command the -o is already taken care of.
2407 if (!STAT_RECORD
&& output_name
&& strcmp(output_name
, "-"))
2410 if (output_name
&& output_fd
) {
2411 fprintf(stderr
, "cannot use both --output and --log-fd\n");
2412 parse_options_usage(stat_usage
, stat_options
, "o", 1);
2413 parse_options_usage(NULL
, stat_options
, "log-fd", 0);
2417 if (metric_only
&& stat_config
.aggr_mode
== AGGR_THREAD
) {
2418 fprintf(stderr
, "--metric-only is not supported with --per-thread\n");
2422 if (metric_only
&& run_count
> 1) {
2423 fprintf(stderr
, "--metric-only is not supported with -r\n");
2427 if (output_fd
< 0) {
2428 fprintf(stderr
, "argument to --log-fd must be a > 0\n");
2429 parse_options_usage(stat_usage
, stat_options
, "log-fd", 0);
2435 mode
= append_file
? "a" : "w";
2437 output
= fopen(output_name
, mode
);
2439 perror("failed to create output file");
2442 clock_gettime(CLOCK_REALTIME
, &tm
);
2443 fprintf(output
, "# started on %s\n", ctime(&tm
.tv_sec
));
2444 } else if (output_fd
> 0) {
2445 mode
= append_file
? "a" : "w";
2446 output
= fdopen(output_fd
, mode
);
2448 perror("Failed opening logfd");
2453 stat_config
.output
= output
;
2456 * let the spreadsheet do the pretty-printing
2459 /* User explicitly passed -B? */
2460 if (big_num_opt
== 1) {
2461 fprintf(stderr
, "-B option not supported with -x\n");
2462 parse_options_usage(stat_usage
, stat_options
, "B", 1);
2463 parse_options_usage(NULL
, stat_options
, "x", 1);
2465 } else /* Nope, so disable big number formatting */
2467 } else if (big_num_opt
== 0) /* User passed --no-big-num */
2470 if (!argc
&& target__none(&target
))
2471 usage_with_options(stat_usage
, stat_options
);
2473 if (run_count
< 0) {
2474 pr_err("Run count must be a positive number\n");
2475 parse_options_usage(stat_usage
, stat_options
, "r", 1);
2477 } else if (run_count
== 0) {
2482 if ((stat_config
.aggr_mode
== AGGR_THREAD
) && !target__has_task(&target
)) {
2483 fprintf(stderr
, "The --per-thread option is only available "
2484 "when monitoring via -p -t options.\n");
2485 parse_options_usage(NULL
, stat_options
, "p", 1);
2486 parse_options_usage(NULL
, stat_options
, "t", 1);
2491 * no_aggr, cgroup are for system-wide only
2492 * --per-thread is aggregated per thread, we dont mix it with cpu mode
2494 if (((stat_config
.aggr_mode
!= AGGR_GLOBAL
&&
2495 stat_config
.aggr_mode
!= AGGR_THREAD
) || nr_cgroups
) &&
2496 !target__has_cpu(&target
)) {
2497 fprintf(stderr
, "both cgroup and no-aggregation "
2498 "modes only available in system-wide mode\n");
2500 parse_options_usage(stat_usage
, stat_options
, "G", 1);
2501 parse_options_usage(NULL
, stat_options
, "A", 1);
2502 parse_options_usage(NULL
, stat_options
, "a", 1);
2506 if (add_default_attributes())
2509 target__validate(&target
);
2511 if (perf_evlist__create_maps(evsel_list
, &target
) < 0) {
2512 if (target__has_task(&target
)) {
2513 pr_err("Problems finding threads of monitor\n");
2514 parse_options_usage(stat_usage
, stat_options
, "p", 1);
2515 parse_options_usage(NULL
, stat_options
, "t", 1);
2516 } else if (target__has_cpu(&target
)) {
2517 perror("failed to parse CPUs map");
2518 parse_options_usage(stat_usage
, stat_options
, "C", 1);
2519 parse_options_usage(NULL
, stat_options
, "a", 1);
2525 * Initialize thread_map with comm names,
2526 * so we could print it out on output.
2528 if (stat_config
.aggr_mode
== AGGR_THREAD
)
2529 thread_map__read_comms(evsel_list
->threads
);
2531 if (interval
&& interval
< 100) {
2532 if (interval
< 10) {
2533 pr_err("print interval must be >= 10ms\n");
2534 parse_options_usage(stat_usage
, stat_options
, "I", 1);
2537 pr_warning("print interval < 100ms. "
2538 "The overhead percentage could be high in some cases. "
2539 "Please proceed with caution.\n");
2542 if (perf_evlist__alloc_stats(evsel_list
, interval
))
2545 if (perf_stat_init_aggr_mode())
2549 * We dont want to block the signals - that would cause
2550 * child tasks to inherit that and Ctrl-C would not work.
2551 * What we want is for Ctrl-C to work in the exec()-ed
2552 * task, but being ignored by perf stat itself:
2556 signal(SIGINT
, skip_signal
);
2557 signal(SIGCHLD
, skip_signal
);
2558 signal(SIGALRM
, skip_signal
);
2559 signal(SIGABRT
, skip_signal
);
2562 for (run_idx
= 0; forever
|| run_idx
< run_count
; run_idx
++) {
2563 if (run_count
!= 1 && verbose
)
2564 fprintf(output
, "[ perf stat: executing run #%d ... ]\n",
2568 perf_evlist__reset_prev_raw_counts(evsel_list
);
2570 status
= run_perf_stat(argc
, argv
);
2571 if (forever
&& status
!= -1 && !interval
) {
2572 print_counters(NULL
, argc
, argv
);
2573 perf_stat__reset_stats();
2577 if (!forever
&& status
!= -1 && !interval
)
2578 print_counters(NULL
, argc
, argv
);
2582 * We synthesize the kernel mmap record just so that older tools
2583 * don't emit warnings about not being able to resolve symbols
2584 * due to /proc/sys/kernel/kptr_restrict settings and instear provide
2585 * a saner message about no samples being in the perf.data file.
2587 * This also serves to suppress a warning about f_header.data.size == 0
2588 * in header.c at the moment 'perf stat record' gets introduced, which
2589 * is not really needed once we start adding the stat specific PERF_RECORD_
2590 * records, but the need to suppress the kptr_restrict messages in older
2591 * tools remain -acme
2593 int fd
= perf_data_file__fd(&perf_stat
.file
);
2594 int err
= perf_event__synthesize_kernel_mmap((void *)&perf_stat
,
2595 process_synthesized_event
,
2596 &perf_stat
.session
->machines
.host
);
2598 pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
2599 "older tools may produce warnings about this file\n.");
2603 if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats
.max
, FINAL
))
2604 pr_err("failed to write stat round event\n");
2607 if (!perf_stat
.file
.is_pipe
) {
2608 perf_stat
.session
->header
.data_size
+= perf_stat
.bytes_written
;
2609 perf_session__write_header(perf_stat
.session
, evsel_list
, fd
, true);
2612 perf_session__delete(perf_stat
.session
);
2615 perf_stat__exit_aggr_mode();
2616 perf_evlist__free_stats(evsel_list
);
2618 perf_evlist__delete(evsel_list
);