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"
67 #include "util/string2.h"
70 #include <linux/time64.h>
71 #include <api/fs/fs.h>
75 #include <sys/prctl.h>
79 #include <sys/types.h>
84 #include "sane_ctype.h"
86 #define DEFAULT_SEPARATOR " "
87 #define CNTR_NOT_SUPPORTED "<not supported>"
88 #define CNTR_NOT_COUNTED "<not counted>"
90 static void print_counters(struct timespec
*ts
, int argc
, const char **argv
);
92 /* Default events used for perf stat -T */
93 static const char *transaction_attrs
= {
105 /* More limited version when the CPU does not have all events. */
106 static const char * transaction_limited_attrs
= {
116 static const char * topdown_attrs
[] = {
117 "topdown-total-slots",
118 "topdown-slots-retired",
119 "topdown-recovery-bubbles",
120 "topdown-fetch-bubbles",
121 "topdown-slots-issued",
125 static struct perf_evlist
*evsel_list
;
127 static struct target target
= {
131 typedef int (*aggr_get_id_t
)(struct cpu_map
*m
, int cpu
);
133 static int run_count
= 1;
134 static bool no_inherit
= false;
135 static volatile pid_t child_pid
= -1;
136 static bool null_run
= false;
137 static int detailed_run
= 0;
138 static bool transaction_run
;
139 static bool topdown_run
= false;
140 static bool big_num
= true;
141 static int big_num_opt
= -1;
142 static const char *csv_sep
= NULL
;
143 static bool csv_output
= false;
144 static bool group
= false;
145 static const char *pre_cmd
= NULL
;
146 static const char *post_cmd
= NULL
;
147 static bool sync_run
= false;
148 static unsigned int initial_delay
= 0;
149 static unsigned int unit_width
= 4; /* strlen("unit") */
150 static bool forever
= false;
151 static bool metric_only
= false;
152 static bool force_metric_only
= false;
153 static bool no_merge
= false;
154 static struct timespec ref_time
;
155 static struct cpu_map
*aggr_map
;
156 static aggr_get_id_t aggr_get_id
;
157 static bool append_file
;
158 static const char *output_name
;
159 static int output_fd
;
160 static int print_free_counters_hint
;
164 struct perf_data_file file
;
165 struct perf_session
*session
;
167 struct perf_tool tool
;
169 struct cpu_map
*cpus
;
170 struct thread_map
*threads
;
171 enum aggr_mode aggr_mode
;
174 static struct perf_stat perf_stat
;
175 #define STAT_RECORD perf_stat.record
177 static volatile int done
= 0;
179 static struct perf_stat_config stat_config
= {
180 .aggr_mode
= AGGR_GLOBAL
,
184 static inline void diff_timespec(struct timespec
*r
, struct timespec
*a
,
187 r
->tv_sec
= a
->tv_sec
- b
->tv_sec
;
188 if (a
->tv_nsec
< b
->tv_nsec
) {
189 r
->tv_nsec
= a
->tv_nsec
+ NSEC_PER_SEC
- b
->tv_nsec
;
192 r
->tv_nsec
= a
->tv_nsec
- b
->tv_nsec
;
196 static void perf_stat__reset_stats(void)
198 perf_evlist__reset_stats(evsel_list
);
199 perf_stat__reset_shadow_stats();
202 static int create_perf_stat_counter(struct perf_evsel
*evsel
)
204 struct perf_event_attr
*attr
= &evsel
->attr
;
206 if (stat_config
.scale
)
207 attr
->read_format
= PERF_FORMAT_TOTAL_TIME_ENABLED
|
208 PERF_FORMAT_TOTAL_TIME_RUNNING
;
210 attr
->inherit
= !no_inherit
;
213 * Some events get initialized with sample_(period/type) set,
214 * like tracepoints. Clear it up for counting.
216 attr
->sample_period
= 0;
219 * But set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
220 * while avoiding that older tools show confusing messages.
222 * However for pipe sessions we need to keep it zero,
223 * because script's perf_evsel__check_attr is triggered
224 * by attr->sample_type != 0, and we can't run it on
227 if (!(STAT_RECORD
&& perf_stat
.file
.is_pipe
))
228 attr
->sample_type
= PERF_SAMPLE_IDENTIFIER
;
231 * Disabling all counters initially, they will be enabled
232 * either manually by us or by kernel via enable_on_exec
235 if (perf_evsel__is_group_leader(evsel
)) {
239 * In case of initial_delay we enable tracee
242 if (target__none(&target
) && !initial_delay
)
243 attr
->enable_on_exec
= 1;
246 if (target__has_cpu(&target
))
247 return perf_evsel__open_per_cpu(evsel
, perf_evsel__cpus(evsel
));
249 return perf_evsel__open_per_thread(evsel
, evsel_list
->threads
);
253 * Does the counter have nsecs as a unit?
255 static inline int nsec_counter(struct perf_evsel
*evsel
)
257 if (perf_evsel__match(evsel
, SOFTWARE
, SW_CPU_CLOCK
) ||
258 perf_evsel__match(evsel
, SOFTWARE
, SW_TASK_CLOCK
))
264 static int process_synthesized_event(struct perf_tool
*tool __maybe_unused
,
265 union perf_event
*event
,
266 struct perf_sample
*sample __maybe_unused
,
267 struct machine
*machine __maybe_unused
)
269 if (perf_data_file__write(&perf_stat
.file
, event
, event
->header
.size
) < 0) {
270 pr_err("failed to write perf data, error: %m\n");
274 perf_stat
.bytes_written
+= event
->header
.size
;
278 static int write_stat_round_event(u64 tm
, u64 type
)
280 return perf_event__synthesize_stat_round(NULL
, tm
, type
,
281 process_synthesized_event
,
285 #define WRITE_STAT_ROUND_EVENT(time, interval) \
286 write_stat_round_event(time, PERF_STAT_ROUND_TYPE__ ## interval)
288 #define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
291 perf_evsel__write_stat_event(struct perf_evsel
*counter
, u32 cpu
, u32 thread
,
292 struct perf_counts_values
*count
)
294 struct perf_sample_id
*sid
= SID(counter
, cpu
, thread
);
296 return perf_event__synthesize_stat(NULL
, cpu
, thread
, sid
->id
, count
,
297 process_synthesized_event
, NULL
);
301 * Read out the results of a single counter:
302 * do not aggregate counts across CPUs in system-wide mode
304 static int read_counter(struct perf_evsel
*counter
)
306 int nthreads
= thread_map__nr(evsel_list
->threads
);
307 int ncpus
, cpu
, thread
;
309 if (target__has_cpu(&target
))
310 ncpus
= perf_evsel__nr_cpus(counter
);
314 if (!counter
->supported
)
317 if (counter
->system_wide
)
320 for (thread
= 0; thread
< nthreads
; thread
++) {
321 for (cpu
= 0; cpu
< ncpus
; cpu
++) {
322 struct perf_counts_values
*count
;
324 count
= perf_counts(counter
->counts
, cpu
, thread
);
325 if (perf_evsel__read(counter
, cpu
, thread
, count
)) {
326 counter
->counts
->scaled
= -1;
327 perf_counts(counter
->counts
, cpu
, thread
)->ena
= 0;
328 perf_counts(counter
->counts
, cpu
, thread
)->run
= 0;
333 if (perf_evsel__write_stat_event(counter
, cpu
, thread
, count
)) {
334 pr_err("failed to write stat event\n");
340 fprintf(stat_config
.output
,
341 "%s: %d: %" PRIu64
" %" PRIu64
" %" PRIu64
"\n",
342 perf_evsel__name(counter
),
344 count
->val
, count
->ena
, count
->run
);
352 static void read_counters(void)
354 struct perf_evsel
*counter
;
357 evlist__for_each_entry(evsel_list
, counter
) {
358 ret
= read_counter(counter
);
360 pr_debug("failed to read counter %s\n", counter
->name
);
362 if (ret
== 0 && perf_stat_process_counter(&stat_config
, counter
))
363 pr_warning("failed to process counter %s\n", counter
->name
);
367 static void process_interval(void)
369 struct timespec ts
, rs
;
373 clock_gettime(CLOCK_MONOTONIC
, &ts
);
374 diff_timespec(&rs
, &ts
, &ref_time
);
377 if (WRITE_STAT_ROUND_EVENT(rs
.tv_sec
* NSEC_PER_SEC
+ rs
.tv_nsec
, INTERVAL
))
378 pr_err("failed to write stat round event\n");
381 print_counters(&rs
, 0, NULL
);
384 static void enable_counters(void)
387 usleep(initial_delay
* USEC_PER_MSEC
);
390 * We need to enable counters only if:
391 * - we don't have tracee (attaching to task or cpu)
392 * - we have initial delay configured
394 if (!target__none(&target
) || initial_delay
)
395 perf_evlist__enable(evsel_list
);
398 static void disable_counters(void)
401 * If we don't have tracee (attaching to task or cpu), counters may
402 * still be running. To get accurate group ratios, we must stop groups
403 * from counting before reading their constituent counters.
405 if (!target__none(&target
))
406 perf_evlist__disable(evsel_list
);
409 static volatile int workload_exec_errno
;
412 * perf_evlist__prepare_workload will send a SIGUSR1
413 * if the fork fails, since we asked by setting its
414 * want_signal to true.
416 static void workload_exec_failed_signal(int signo __maybe_unused
, siginfo_t
*info
,
417 void *ucontext __maybe_unused
)
419 workload_exec_errno
= info
->si_value
.sival_int
;
422 static bool has_unit(struct perf_evsel
*counter
)
424 return counter
->unit
&& *counter
->unit
;
427 static bool has_scale(struct perf_evsel
*counter
)
429 return counter
->scale
!= 1;
432 static int perf_stat_synthesize_config(bool is_pipe
)
434 struct perf_evsel
*counter
;
438 err
= perf_event__synthesize_attrs(NULL
, perf_stat
.session
,
439 process_synthesized_event
);
441 pr_err("Couldn't synthesize attrs.\n");
447 * Synthesize other events stuff not carried within
448 * attr event - unit, scale, name
450 evlist__for_each_entry(evsel_list
, counter
) {
451 if (!counter
->supported
)
455 * Synthesize unit and scale only if it's defined.
457 if (has_unit(counter
)) {
458 err
= perf_event__synthesize_event_update_unit(NULL
, counter
, process_synthesized_event
);
460 pr_err("Couldn't synthesize evsel unit.\n");
465 if (has_scale(counter
)) {
466 err
= perf_event__synthesize_event_update_scale(NULL
, counter
, process_synthesized_event
);
468 pr_err("Couldn't synthesize evsel scale.\n");
473 if (counter
->own_cpus
) {
474 err
= perf_event__synthesize_event_update_cpus(NULL
, counter
, process_synthesized_event
);
476 pr_err("Couldn't synthesize evsel scale.\n");
482 * Name is needed only for pipe output,
483 * perf.data carries event names.
486 err
= perf_event__synthesize_event_update_name(NULL
, counter
, process_synthesized_event
);
488 pr_err("Couldn't synthesize evsel name.\n");
494 err
= perf_event__synthesize_thread_map2(NULL
, evsel_list
->threads
,
495 process_synthesized_event
,
498 pr_err("Couldn't synthesize thread map.\n");
502 err
= perf_event__synthesize_cpu_map(NULL
, evsel_list
->cpus
,
503 process_synthesized_event
, NULL
);
505 pr_err("Couldn't synthesize thread map.\n");
509 err
= perf_event__synthesize_stat_config(NULL
, &stat_config
,
510 process_synthesized_event
, NULL
);
512 pr_err("Couldn't synthesize config.\n");
519 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
521 static int __store_counter_ids(struct perf_evsel
*counter
,
522 struct cpu_map
*cpus
,
523 struct thread_map
*threads
)
527 for (cpu
= 0; cpu
< cpus
->nr
; cpu
++) {
528 for (thread
= 0; thread
< threads
->nr
; thread
++) {
529 int fd
= FD(counter
, cpu
, thread
);
531 if (perf_evlist__id_add_fd(evsel_list
, counter
,
532 cpu
, thread
, fd
) < 0)
540 static int store_counter_ids(struct perf_evsel
*counter
)
542 struct cpu_map
*cpus
= counter
->cpus
;
543 struct thread_map
*threads
= counter
->threads
;
545 if (perf_evsel__alloc_id(counter
, cpus
->nr
, threads
->nr
))
548 return __store_counter_ids(counter
, cpus
, threads
);
551 static int __run_perf_stat(int argc
, const char **argv
)
553 int interval
= stat_config
.interval
;
555 unsigned long long t0
, t1
;
556 struct perf_evsel
*counter
;
560 const bool forks
= (argc
> 0);
561 bool is_pipe
= STAT_RECORD
? perf_stat
.file
.is_pipe
: false;
562 struct perf_evsel_config_term
*err_term
;
565 ts
.tv_sec
= interval
/ USEC_PER_MSEC
;
566 ts
.tv_nsec
= (interval
% USEC_PER_MSEC
) * NSEC_PER_MSEC
;
573 if (perf_evlist__prepare_workload(evsel_list
, &target
, argv
, is_pipe
,
574 workload_exec_failed_signal
) < 0) {
575 perror("failed to prepare workload");
578 child_pid
= evsel_list
->workload
.pid
;
582 perf_evlist__set_leader(evsel_list
);
584 evlist__for_each_entry(evsel_list
, counter
) {
586 if (create_perf_stat_counter(counter
) < 0) {
588 * PPC returns ENXIO for HW counters until 2.6.37
589 * (behavior changed with commit b0a873e).
591 if (errno
== EINVAL
|| errno
== ENOSYS
||
592 errno
== ENOENT
|| errno
== EOPNOTSUPP
||
595 ui__warning("%s event is not supported by the kernel.\n",
596 perf_evsel__name(counter
));
597 counter
->supported
= false;
599 if ((counter
->leader
!= counter
) ||
600 !(counter
->leader
->nr_members
> 1))
602 } else if (perf_evsel__fallback(counter
, errno
, msg
, sizeof(msg
))) {
604 ui__warning("%s\n", msg
);
608 perf_evsel__open_strerror(counter
, &target
,
609 errno
, msg
, sizeof(msg
));
610 ui__error("%s\n", msg
);
613 kill(child_pid
, SIGTERM
);
617 counter
->supported
= true;
619 l
= strlen(counter
->unit
);
623 if (STAT_RECORD
&& store_counter_ids(counter
))
627 if (perf_evlist__apply_filters(evsel_list
, &counter
)) {
628 error("failed to set filter \"%s\" on event %s with %d (%s)\n",
629 counter
->filter
, perf_evsel__name(counter
), errno
,
630 str_error_r(errno
, msg
, sizeof(msg
)));
634 if (perf_evlist__apply_drv_configs(evsel_list
, &counter
, &err_term
)) {
635 error("failed to set config \"%s\" on event %s with %d (%s)\n",
636 err_term
->val
.drv_cfg
, perf_evsel__name(counter
), errno
,
637 str_error_r(errno
, msg
, sizeof(msg
)));
642 int err
, fd
= perf_data_file__fd(&perf_stat
.file
);
645 err
= perf_header__write_pipe(perf_data_file__fd(&perf_stat
.file
));
647 err
= perf_session__write_header(perf_stat
.session
, evsel_list
,
654 err
= perf_stat_synthesize_config(is_pipe
);
660 * Enable counters and exec the command:
663 clock_gettime(CLOCK_MONOTONIC
, &ref_time
);
666 perf_evlist__start_workload(evsel_list
);
670 while (!waitpid(child_pid
, &status
, WNOHANG
)) {
671 nanosleep(&ts
, NULL
);
677 if (workload_exec_errno
) {
678 const char *emsg
= str_error_r(workload_exec_errno
, msg
, sizeof(msg
));
679 pr_err("Workload failed: %s\n", emsg
);
683 if (WIFSIGNALED(status
))
684 psignal(WTERMSIG(status
), argv
[0]);
688 nanosleep(&ts
, NULL
);
698 update_stats(&walltime_nsecs_stats
, t1
- t0
);
701 * Closing a group leader splits the group, and as we only disable
702 * group leaders, results in remaining events becoming enabled. To
703 * avoid arbitrary skew, we must read all counters before closing any
707 perf_evlist__close(evsel_list
);
709 return WEXITSTATUS(status
);
712 static int run_perf_stat(int argc
, const char **argv
)
717 ret
= system(pre_cmd
);
725 ret
= __run_perf_stat(argc
, argv
);
730 ret
= system(post_cmd
);
738 static void print_running(u64 run
, u64 ena
)
741 fprintf(stat_config
.output
, "%s%" PRIu64
"%s%.2f",
745 ena
? 100.0 * run
/ ena
: 100.0);
746 } else if (run
!= ena
) {
747 fprintf(stat_config
.output
, " (%.2f%%)", 100.0 * run
/ ena
);
751 static void print_noise_pct(double total
, double avg
)
753 double pct
= rel_stddev_stats(total
, avg
);
756 fprintf(stat_config
.output
, "%s%.2f%%", csv_sep
, pct
);
758 fprintf(stat_config
.output
, " ( +-%6.2f%% )", pct
);
761 static void print_noise(struct perf_evsel
*evsel
, double avg
)
763 struct perf_stat_evsel
*ps
;
769 print_noise_pct(stddev_stats(&ps
->res_stats
[0]), avg
);
772 static void aggr_printout(struct perf_evsel
*evsel
, int id
, int nr
)
774 switch (stat_config
.aggr_mode
) {
776 fprintf(stat_config
.output
, "S%d-C%*d%s%*d%s",
777 cpu_map__id_to_socket(id
),
779 cpu_map__id_to_cpu(id
),
786 fprintf(stat_config
.output
, "S%*d%s%*d%s",
795 fprintf(stat_config
.output
, "CPU%*d%s",
797 perf_evsel__cpus(evsel
)->map
[id
], csv_sep
);
800 fprintf(stat_config
.output
, "%*s-%*d%s",
802 thread_map__comm(evsel
->threads
, id
),
804 thread_map__pid(evsel
->threads
, id
),
820 struct perf_evsel
*evsel
;
823 #define METRIC_LEN 35
825 static void new_line_std(void *ctx
)
827 struct outstate
*os
= ctx
;
832 static void do_new_line_std(struct outstate
*os
)
835 fputs(os
->prefix
, os
->fh
);
836 aggr_printout(os
->evsel
, os
->id
, os
->nr
);
837 if (stat_config
.aggr_mode
== AGGR_NONE
)
838 fprintf(os
->fh
, " ");
839 fprintf(os
->fh
, " ");
842 static void print_metric_std(void *ctx
, const char *color
, const char *fmt
,
843 const char *unit
, double val
)
845 struct outstate
*os
= ctx
;
848 bool newline
= os
->newline
;
852 if (unit
== NULL
|| fmt
== NULL
) {
853 fprintf(out
, "%-*s", METRIC_LEN
, "");
860 n
= fprintf(out
, " # ");
862 n
+= color_fprintf(out
, color
, fmt
, val
);
864 n
+= fprintf(out
, fmt
, val
);
865 fprintf(out
, " %-*s", METRIC_LEN
- n
- 1, unit
);
868 static void new_line_csv(void *ctx
)
870 struct outstate
*os
= ctx
;
875 fprintf(os
->fh
, "%s%s", os
->prefix
, csv_sep
);
876 aggr_printout(os
->evsel
, os
->id
, os
->nr
);
877 for (i
= 0; i
< os
->nfields
; i
++)
878 fputs(csv_sep
, os
->fh
);
881 static void print_metric_csv(void *ctx
,
882 const char *color __maybe_unused
,
883 const char *fmt
, const char *unit
, double val
)
885 struct outstate
*os
= ctx
;
887 char buf
[64], *vals
, *ends
;
889 if (unit
== NULL
|| fmt
== NULL
) {
890 fprintf(out
, "%s%s%s%s", csv_sep
, csv_sep
, csv_sep
, csv_sep
);
893 snprintf(buf
, sizeof(buf
), fmt
, val
);
894 ends
= vals
= ltrim(buf
);
895 while (isdigit(*ends
) || *ends
== '.')
898 while (isspace(*unit
))
900 fprintf(out
, "%s%s%s%s", csv_sep
, vals
, csv_sep
, unit
);
903 #define METRIC_ONLY_LEN 20
905 /* Filter out some columns that don't work well in metrics only mode */
907 static bool valid_only_metric(const char *unit
)
911 if (strstr(unit
, "/sec") ||
912 strstr(unit
, "hz") ||
913 strstr(unit
, "Hz") ||
914 strstr(unit
, "CPUs utilized"))
919 static const char *fixunit(char *buf
, struct perf_evsel
*evsel
,
922 if (!strncmp(unit
, "of all", 6)) {
923 snprintf(buf
, 1024, "%s %s", perf_evsel__name(evsel
),
930 static void print_metric_only(void *ctx
, const char *color
, const char *fmt
,
931 const char *unit
, double val
)
933 struct outstate
*os
= ctx
;
937 unsigned mlen
= METRIC_ONLY_LEN
;
939 if (!valid_only_metric(unit
))
941 unit
= fixunit(buf
, os
->evsel
, unit
);
943 n
= color_fprintf(out
, color
, fmt
, val
);
945 n
= fprintf(out
, fmt
, val
);
946 if (n
> METRIC_ONLY_LEN
)
948 if (mlen
< strlen(unit
))
949 mlen
= strlen(unit
) + 1;
950 fprintf(out
, "%*s", mlen
- n
, "");
953 static void print_metric_only_csv(void *ctx
, const char *color __maybe_unused
,
955 const char *unit
, double val
)
957 struct outstate
*os
= ctx
;
959 char buf
[64], *vals
, *ends
;
962 if (!valid_only_metric(unit
))
964 unit
= fixunit(tbuf
, os
->evsel
, unit
);
965 snprintf(buf
, sizeof buf
, fmt
, val
);
966 ends
= vals
= ltrim(buf
);
967 while (isdigit(*ends
) || *ends
== '.')
970 fprintf(out
, "%s%s", vals
, csv_sep
);
973 static void new_line_metric(void *ctx __maybe_unused
)
977 static void print_metric_header(void *ctx
, const char *color __maybe_unused
,
978 const char *fmt __maybe_unused
,
979 const char *unit
, double val __maybe_unused
)
981 struct outstate
*os
= ctx
;
984 if (!valid_only_metric(unit
))
986 unit
= fixunit(tbuf
, os
->evsel
, unit
);
988 fprintf(os
->fh
, "%s%s", unit
, csv_sep
);
990 fprintf(os
->fh
, "%-*s ", METRIC_ONLY_LEN
, unit
);
993 static void nsec_printout(int id
, int nr
, struct perf_evsel
*evsel
, double avg
)
995 FILE *output
= stat_config
.output
;
996 double msecs
= avg
/ NSEC_PER_MSEC
;
997 const char *fmt_v
, *fmt_n
;
1000 fmt_v
= csv_output
? "%.6f%s" : "%18.6f%s";
1001 fmt_n
= csv_output
? "%s" : "%-25s";
1003 aggr_printout(evsel
, id
, nr
);
1005 scnprintf(name
, sizeof(name
), "%s%s",
1006 perf_evsel__name(evsel
), csv_output
? "" : " (msec)");
1008 fprintf(output
, fmt_v
, msecs
, csv_sep
);
1011 fprintf(output
, "%s%s", evsel
->unit
, csv_sep
);
1013 fprintf(output
, "%-*s%s", unit_width
, evsel
->unit
, csv_sep
);
1015 fprintf(output
, fmt_n
, name
);
1018 fprintf(output
, "%s%s", csv_sep
, evsel
->cgrp
->name
);
1021 static int first_shadow_cpu(struct perf_evsel
*evsel
, int id
)
1028 if (stat_config
.aggr_mode
== AGGR_NONE
)
1031 if (stat_config
.aggr_mode
== AGGR_GLOBAL
)
1034 for (i
= 0; i
< perf_evsel__nr_cpus(evsel
); i
++) {
1035 int cpu2
= perf_evsel__cpus(evsel
)->map
[i
];
1037 if (aggr_get_id(evsel_list
->cpus
, cpu2
) == id
)
1043 static void abs_printout(int id
, int nr
, struct perf_evsel
*evsel
, double avg
)
1045 FILE *output
= stat_config
.output
;
1046 double sc
= evsel
->scale
;
1050 fmt
= floor(sc
) != sc
? "%.2f%s" : "%.0f%s";
1053 fmt
= floor(sc
) != sc
? "%'18.2f%s" : "%'18.0f%s";
1055 fmt
= floor(sc
) != sc
? "%18.2f%s" : "%18.0f%s";
1058 aggr_printout(evsel
, id
, nr
);
1060 fprintf(output
, fmt
, avg
, csv_sep
);
1063 fprintf(output
, "%-*s%s",
1064 csv_output
? 0 : unit_width
,
1065 evsel
->unit
, csv_sep
);
1067 fprintf(output
, "%-*s", csv_output
? 0 : 25, perf_evsel__name(evsel
));
1070 fprintf(output
, "%s%s", csv_sep
, evsel
->cgrp
->name
);
1073 static void printout(int id
, int nr
, struct perf_evsel
*counter
, double uval
,
1074 char *prefix
, u64 run
, u64 ena
, double noise
)
1076 struct perf_stat_output_ctx out
;
1077 struct outstate os
= {
1078 .fh
= stat_config
.output
,
1079 .prefix
= prefix
? prefix
: "",
1084 print_metric_t pm
= print_metric_std
;
1088 nl
= new_line_metric
;
1090 pm
= print_metric_only_csv
;
1092 pm
= print_metric_only
;
1096 if (csv_output
&& !metric_only
) {
1097 static int aggr_fields
[] = {
1105 pm
= print_metric_csv
;
1108 os
.nfields
+= aggr_fields
[stat_config
.aggr_mode
];
1112 if (run
== 0 || ena
== 0 || counter
->counts
->scaled
== -1) {
1114 pm(&os
, NULL
, "", "", 0);
1117 aggr_printout(counter
, id
, nr
);
1119 fprintf(stat_config
.output
, "%*s%s",
1120 csv_output
? 0 : 18,
1121 counter
->supported
? CNTR_NOT_COUNTED
: CNTR_NOT_SUPPORTED
,
1124 if (counter
->supported
)
1125 print_free_counters_hint
= 1;
1127 fprintf(stat_config
.output
, "%-*s%s",
1128 csv_output
? 0 : unit_width
,
1129 counter
->unit
, csv_sep
);
1131 fprintf(stat_config
.output
, "%*s",
1132 csv_output
? 0 : -25,
1133 perf_evsel__name(counter
));
1136 fprintf(stat_config
.output
, "%s%s",
1137 csv_sep
, counter
->cgrp
->name
);
1140 pm(&os
, NULL
, NULL
, "", 0);
1141 print_noise(counter
, noise
);
1142 print_running(run
, ena
);
1144 pm(&os
, NULL
, NULL
, "", 0);
1150 else if (nsec_counter(counter
))
1151 nsec_printout(id
, nr
, counter
, uval
);
1153 abs_printout(id
, nr
, counter
, uval
);
1155 out
.print_metric
= pm
;
1158 out
.force_header
= false;
1160 if (csv_output
&& !metric_only
) {
1161 print_noise(counter
, noise
);
1162 print_running(run
, ena
);
1165 perf_stat__print_shadow_stats(counter
, uval
,
1166 first_shadow_cpu(counter
, id
),
1168 if (!csv_output
&& !metric_only
) {
1169 print_noise(counter
, noise
);
1170 print_running(run
, ena
);
1174 static void aggr_update_shadow(void)
1178 struct perf_evsel
*counter
;
1180 for (s
= 0; s
< aggr_map
->nr
; s
++) {
1181 id
= aggr_map
->map
[s
];
1182 evlist__for_each_entry(evsel_list
, counter
) {
1184 for (cpu
= 0; cpu
< perf_evsel__nr_cpus(counter
); cpu
++) {
1185 s2
= aggr_get_id(evsel_list
->cpus
, cpu
);
1188 val
+= perf_counts(counter
->counts
, cpu
, 0)->val
;
1190 val
= val
* counter
->scale
;
1191 perf_stat__update_shadow_stats(counter
, &val
,
1192 first_shadow_cpu(counter
, id
));
1197 static void collect_all_aliases(struct perf_evsel
*counter
,
1198 void (*cb
)(struct perf_evsel
*counter
, void *data
,
1202 struct perf_evsel
*alias
;
1204 alias
= list_prepare_entry(counter
, &(evsel_list
->entries
), node
);
1205 list_for_each_entry_continue (alias
, &evsel_list
->entries
, node
) {
1206 if (strcmp(perf_evsel__name(alias
), perf_evsel__name(counter
)) ||
1207 alias
->scale
!= counter
->scale
||
1208 alias
->cgrp
!= counter
->cgrp
||
1209 strcmp(alias
->unit
, counter
->unit
) ||
1210 nsec_counter(alias
) != nsec_counter(counter
))
1212 alias
->merged_stat
= true;
1213 cb(alias
, data
, false);
1217 static bool collect_data(struct perf_evsel
*counter
,
1218 void (*cb
)(struct perf_evsel
*counter
, void *data
,
1222 if (counter
->merged_stat
)
1224 cb(counter
, data
, true);
1226 collect_all_aliases(counter
, cb
, data
);
1237 static void aggr_cb(struct perf_evsel
*counter
, void *data
, bool first
)
1239 struct aggr_data
*ad
= data
;
1242 for (cpu
= 0; cpu
< perf_evsel__nr_cpus(counter
); cpu
++) {
1243 struct perf_counts_values
*counts
;
1245 s2
= aggr_get_id(perf_evsel__cpus(counter
), cpu
);
1250 counts
= perf_counts(counter
->counts
, cpu
, 0);
1252 * When any result is bad, make them all to give
1253 * consistent output in interval mode.
1255 if (counts
->ena
== 0 || counts
->run
== 0 ||
1256 counter
->counts
->scaled
== -1) {
1261 ad
->val
+= counts
->val
;
1262 ad
->ena
+= counts
->ena
;
1263 ad
->run
+= counts
->run
;
1267 static void print_aggr(char *prefix
)
1269 FILE *output
= stat_config
.output
;
1270 struct perf_evsel
*counter
;
1276 if (!(aggr_map
|| aggr_get_id
))
1279 aggr_update_shadow();
1282 * With metric_only everything is on a single line.
1283 * Without each counter has its own line.
1285 for (s
= 0; s
< aggr_map
->nr
; s
++) {
1286 struct aggr_data ad
;
1287 if (prefix
&& metric_only
)
1288 fprintf(output
, "%s", prefix
);
1290 ad
.id
= id
= aggr_map
->map
[s
];
1292 evlist__for_each_entry(evsel_list
, counter
) {
1293 ad
.val
= ad
.ena
= ad
.run
= 0;
1295 if (!collect_data(counter
, aggr_cb
, &ad
))
1301 if (first
&& metric_only
) {
1303 aggr_printout(counter
, id
, nr
);
1305 if (prefix
&& !metric_only
)
1306 fprintf(output
, "%s", prefix
);
1308 uval
= val
* counter
->scale
;
1309 printout(id
, nr
, counter
, uval
, prefix
, run
, ena
, 1.0);
1311 fputc('\n', output
);
1314 fputc('\n', output
);
1318 static void print_aggr_thread(struct perf_evsel
*counter
, char *prefix
)
1320 FILE *output
= stat_config
.output
;
1321 int nthreads
= thread_map__nr(counter
->threads
);
1322 int ncpus
= cpu_map__nr(counter
->cpus
);
1326 for (thread
= 0; thread
< nthreads
; thread
++) {
1327 u64 ena
= 0, run
= 0, val
= 0;
1329 for (cpu
= 0; cpu
< ncpus
; cpu
++) {
1330 val
+= perf_counts(counter
->counts
, cpu
, thread
)->val
;
1331 ena
+= perf_counts(counter
->counts
, cpu
, thread
)->ena
;
1332 run
+= perf_counts(counter
->counts
, cpu
, thread
)->run
;
1336 fprintf(output
, "%s", prefix
);
1338 uval
= val
* counter
->scale
;
1339 printout(thread
, 0, counter
, uval
, prefix
, run
, ena
, 1.0);
1340 fputc('\n', output
);
1345 double avg
, avg_enabled
, avg_running
;
1348 static void counter_aggr_cb(struct perf_evsel
*counter
, void *data
,
1349 bool first __maybe_unused
)
1351 struct caggr_data
*cd
= data
;
1352 struct perf_stat_evsel
*ps
= counter
->priv
;
1354 cd
->avg
+= avg_stats(&ps
->res_stats
[0]);
1355 cd
->avg_enabled
+= avg_stats(&ps
->res_stats
[1]);
1356 cd
->avg_running
+= avg_stats(&ps
->res_stats
[2]);
1360 * Print out the results of a single counter:
1361 * aggregated counts in system-wide mode
1363 static void print_counter_aggr(struct perf_evsel
*counter
, char *prefix
)
1365 FILE *output
= stat_config
.output
;
1367 struct caggr_data cd
= { .avg
= 0.0 };
1369 if (!collect_data(counter
, counter_aggr_cb
, &cd
))
1372 if (prefix
&& !metric_only
)
1373 fprintf(output
, "%s", prefix
);
1375 uval
= cd
.avg
* counter
->scale
;
1376 printout(-1, 0, counter
, uval
, prefix
, cd
.avg_running
, cd
.avg_enabled
, cd
.avg
);
1378 fprintf(output
, "\n");
1381 static void counter_cb(struct perf_evsel
*counter
, void *data
,
1382 bool first __maybe_unused
)
1384 struct aggr_data
*ad
= data
;
1386 ad
->val
+= perf_counts(counter
->counts
, ad
->cpu
, 0)->val
;
1387 ad
->ena
+= perf_counts(counter
->counts
, ad
->cpu
, 0)->ena
;
1388 ad
->run
+= perf_counts(counter
->counts
, ad
->cpu
, 0)->run
;
1392 * Print out the results of a single counter:
1393 * does not use aggregated count in system-wide
1395 static void print_counter(struct perf_evsel
*counter
, char *prefix
)
1397 FILE *output
= stat_config
.output
;
1402 for (cpu
= 0; cpu
< perf_evsel__nr_cpus(counter
); cpu
++) {
1403 struct aggr_data ad
= { .cpu
= cpu
};
1405 if (!collect_data(counter
, counter_cb
, &ad
))
1412 fprintf(output
, "%s", prefix
);
1414 uval
= val
* counter
->scale
;
1415 printout(cpu
, 0, counter
, uval
, prefix
, run
, ena
, 1.0);
1417 fputc('\n', output
);
1421 static void print_no_aggr_metric(char *prefix
)
1425 struct perf_evsel
*counter
;
1429 nrcpus
= evsel_list
->cpus
->nr
;
1430 for (cpu
= 0; cpu
< nrcpus
; cpu
++) {
1434 fputs(prefix
, stat_config
.output
);
1435 evlist__for_each_entry(evsel_list
, counter
) {
1437 aggr_printout(counter
, cpu
, 0);
1440 val
= perf_counts(counter
->counts
, cpu
, 0)->val
;
1441 ena
= perf_counts(counter
->counts
, cpu
, 0)->ena
;
1442 run
= perf_counts(counter
->counts
, cpu
, 0)->run
;
1444 uval
= val
* counter
->scale
;
1445 printout(cpu
, 0, counter
, uval
, prefix
, run
, ena
, 1.0);
1447 fputc('\n', stat_config
.output
);
1451 static int aggr_header_lens
[] = {
1459 static const char *aggr_header_csv
[] = {
1460 [AGGR_CORE
] = "core,cpus,",
1461 [AGGR_SOCKET
] = "socket,cpus",
1462 [AGGR_NONE
] = "cpu,",
1463 [AGGR_THREAD
] = "comm-pid,",
1467 static void print_metric_headers(const char *prefix
, bool no_indent
)
1469 struct perf_stat_output_ctx out
;
1470 struct perf_evsel
*counter
;
1471 struct outstate os
= {
1472 .fh
= stat_config
.output
1476 fprintf(stat_config
.output
, "%s", prefix
);
1478 if (!csv_output
&& !no_indent
)
1479 fprintf(stat_config
.output
, "%*s",
1480 aggr_header_lens
[stat_config
.aggr_mode
], "");
1482 if (stat_config
.interval
)
1483 fputs("time,", stat_config
.output
);
1484 fputs(aggr_header_csv
[stat_config
.aggr_mode
],
1485 stat_config
.output
);
1488 /* Print metrics headers only */
1489 evlist__for_each_entry(evsel_list
, counter
) {
1492 out
.print_metric
= print_metric_header
;
1493 out
.new_line
= new_line_metric
;
1494 out
.force_header
= true;
1496 perf_stat__print_shadow_stats(counter
, 0,
1500 fputc('\n', stat_config
.output
);
1503 static void print_interval(char *prefix
, struct timespec
*ts
)
1505 FILE *output
= stat_config
.output
;
1506 static int num_print_interval
;
1508 sprintf(prefix
, "%6lu.%09lu%s", ts
->tv_sec
, ts
->tv_nsec
, csv_sep
);
1510 if (num_print_interval
== 0 && !csv_output
) {
1511 switch (stat_config
.aggr_mode
) {
1513 fprintf(output
, "# time socket cpus");
1515 fprintf(output
, " counts %*s events\n", unit_width
, "unit");
1518 fprintf(output
, "# time core cpus");
1520 fprintf(output
, " counts %*s events\n", unit_width
, "unit");
1523 fprintf(output
, "# time CPU");
1525 fprintf(output
, " counts %*s events\n", unit_width
, "unit");
1528 fprintf(output
, "# time comm-pid");
1530 fprintf(output
, " counts %*s events\n", unit_width
, "unit");
1534 fprintf(output
, "# time");
1536 fprintf(output
, " counts %*s events\n", unit_width
, "unit");
1542 if (num_print_interval
== 0 && metric_only
)
1543 print_metric_headers(" ", true);
1544 if (++num_print_interval
== 25)
1545 num_print_interval
= 0;
1548 static void print_header(int argc
, const char **argv
)
1550 FILE *output
= stat_config
.output
;
1556 fprintf(output
, "\n");
1557 fprintf(output
, " Performance counter stats for ");
1558 if (target
.system_wide
)
1559 fprintf(output
, "\'system wide");
1560 else if (target
.cpu_list
)
1561 fprintf(output
, "\'CPU(s) %s", target
.cpu_list
);
1562 else if (!target__has_task(&target
)) {
1563 fprintf(output
, "\'%s", argv
? argv
[0] : "pipe");
1564 for (i
= 1; argv
&& (i
< argc
); i
++)
1565 fprintf(output
, " %s", argv
[i
]);
1566 } else if (target
.pid
)
1567 fprintf(output
, "process id \'%s", target
.pid
);
1569 fprintf(output
, "thread id \'%s", target
.tid
);
1571 fprintf(output
, "\'");
1573 fprintf(output
, " (%d runs)", run_count
);
1574 fprintf(output
, ":\n\n");
1578 static void print_footer(void)
1580 FILE *output
= stat_config
.output
;
1583 fprintf(output
, "\n");
1584 fprintf(output
, " %17.9f seconds time elapsed",
1585 avg_stats(&walltime_nsecs_stats
) / NSEC_PER_SEC
);
1586 if (run_count
> 1) {
1587 fprintf(output
, " ");
1588 print_noise_pct(stddev_stats(&walltime_nsecs_stats
),
1589 avg_stats(&walltime_nsecs_stats
));
1591 fprintf(output
, "\n\n");
1593 if (print_free_counters_hint
)
1595 "Some events weren't counted. Try disabling the NMI watchdog:\n"
1596 " echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1598 " echo 1 > /proc/sys/kernel/nmi_watchdog\n");
1601 static void print_counters(struct timespec
*ts
, int argc
, const char **argv
)
1603 int interval
= stat_config
.interval
;
1604 struct perf_evsel
*counter
;
1605 char buf
[64], *prefix
= NULL
;
1607 /* Do not print anything if we record to the pipe. */
1608 if (STAT_RECORD
&& perf_stat
.file
.is_pipe
)
1612 print_interval(prefix
= buf
, ts
);
1614 print_header(argc
, argv
);
1617 static int num_print_iv
;
1619 if (num_print_iv
== 0 && !interval
)
1620 print_metric_headers(prefix
, false);
1621 if (num_print_iv
++ == 25)
1623 if (stat_config
.aggr_mode
== AGGR_GLOBAL
&& prefix
)
1624 fprintf(stat_config
.output
, "%s", prefix
);
1627 switch (stat_config
.aggr_mode
) {
1633 evlist__for_each_entry(evsel_list
, counter
)
1634 print_aggr_thread(counter
, prefix
);
1637 evlist__for_each_entry(evsel_list
, counter
)
1638 print_counter_aggr(counter
, prefix
);
1640 fputc('\n', stat_config
.output
);
1644 print_no_aggr_metric(prefix
);
1646 evlist__for_each_entry(evsel_list
, counter
)
1647 print_counter(counter
, prefix
);
1655 if (!interval
&& !csv_output
)
1658 fflush(stat_config
.output
);
1661 static volatile int signr
= -1;
1663 static void skip_signal(int signo
)
1665 if ((child_pid
== -1) || stat_config
.interval
)
1670 * render child_pid harmless
1671 * won't send SIGTERM to a random
1672 * process in case of race condition
1673 * and fast PID recycling
1678 static void sig_atexit(void)
1683 * avoid race condition with SIGCHLD handler
1684 * in skip_signal() which is modifying child_pid
1685 * goal is to avoid send SIGTERM to a random
1689 sigaddset(&set
, SIGCHLD
);
1690 sigprocmask(SIG_BLOCK
, &set
, &oset
);
1692 if (child_pid
!= -1)
1693 kill(child_pid
, SIGTERM
);
1695 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
1700 signal(signr
, SIG_DFL
);
1701 kill(getpid(), signr
);
1704 static int stat__set_big_num(const struct option
*opt __maybe_unused
,
1705 const char *s __maybe_unused
, int unset
)
1707 big_num_opt
= unset
? 0 : 1;
1711 static int enable_metric_only(const struct option
*opt __maybe_unused
,
1712 const char *s __maybe_unused
, int unset
)
1714 force_metric_only
= true;
1715 metric_only
= !unset
;
1719 static const struct option stat_options
[] = {
1720 OPT_BOOLEAN('T', "transaction", &transaction_run
,
1721 "hardware transaction statistics"),
1722 OPT_CALLBACK('e', "event", &evsel_list
, "event",
1723 "event selector. use 'perf list' to list available events",
1724 parse_events_option
),
1725 OPT_CALLBACK(0, "filter", &evsel_list
, "filter",
1726 "event filter", parse_filter
),
1727 OPT_BOOLEAN('i', "no-inherit", &no_inherit
,
1728 "child tasks do not inherit counters"),
1729 OPT_STRING('p', "pid", &target
.pid
, "pid",
1730 "stat events on existing process id"),
1731 OPT_STRING('t', "tid", &target
.tid
, "tid",
1732 "stat events on existing thread id"),
1733 OPT_BOOLEAN('a', "all-cpus", &target
.system_wide
,
1734 "system-wide collection from all CPUs"),
1735 OPT_BOOLEAN('g', "group", &group
,
1736 "put the counters into a counter group"),
1737 OPT_BOOLEAN('c', "scale", &stat_config
.scale
, "scale/normalize counters"),
1738 OPT_INCR('v', "verbose", &verbose
,
1739 "be more verbose (show counter open errors, etc)"),
1740 OPT_INTEGER('r', "repeat", &run_count
,
1741 "repeat command and print average + stddev (max: 100, forever: 0)"),
1742 OPT_BOOLEAN('n', "null", &null_run
,
1743 "null run - dont start any counters"),
1744 OPT_INCR('d', "detailed", &detailed_run
,
1745 "detailed run - start a lot of events"),
1746 OPT_BOOLEAN('S', "sync", &sync_run
,
1747 "call sync() before starting a run"),
1748 OPT_CALLBACK_NOOPT('B', "big-num", NULL
, NULL
,
1749 "print large numbers with thousands\' separators",
1751 OPT_STRING('C', "cpu", &target
.cpu_list
, "cpu",
1752 "list of cpus to monitor in system-wide"),
1753 OPT_SET_UINT('A', "no-aggr", &stat_config
.aggr_mode
,
1754 "disable CPU count aggregation", AGGR_NONE
),
1755 OPT_BOOLEAN(0, "no-merge", &no_merge
, "Do not merge identical named events"),
1756 OPT_STRING('x', "field-separator", &csv_sep
, "separator",
1757 "print counts with custom separator"),
1758 OPT_CALLBACK('G', "cgroup", &evsel_list
, "name",
1759 "monitor event in cgroup name only", parse_cgroups
),
1760 OPT_STRING('o', "output", &output_name
, "file", "output file name"),
1761 OPT_BOOLEAN(0, "append", &append_file
, "append to the output file"),
1762 OPT_INTEGER(0, "log-fd", &output_fd
,
1763 "log output to fd, instead of stderr"),
1764 OPT_STRING(0, "pre", &pre_cmd
, "command",
1765 "command to run prior to the measured command"),
1766 OPT_STRING(0, "post", &post_cmd
, "command",
1767 "command to run after to the measured command"),
1768 OPT_UINTEGER('I', "interval-print", &stat_config
.interval
,
1769 "print counts at regular interval in ms (>= 10)"),
1770 OPT_SET_UINT(0, "per-socket", &stat_config
.aggr_mode
,
1771 "aggregate counts per processor socket", AGGR_SOCKET
),
1772 OPT_SET_UINT(0, "per-core", &stat_config
.aggr_mode
,
1773 "aggregate counts per physical processor core", AGGR_CORE
),
1774 OPT_SET_UINT(0, "per-thread", &stat_config
.aggr_mode
,
1775 "aggregate counts per thread", AGGR_THREAD
),
1776 OPT_UINTEGER('D', "delay", &initial_delay
,
1777 "ms to wait before starting measurement after program start"),
1778 OPT_CALLBACK_NOOPT(0, "metric-only", &metric_only
, NULL
,
1779 "Only print computed metrics. No raw values", enable_metric_only
),
1780 OPT_BOOLEAN(0, "topdown", &topdown_run
,
1781 "measure topdown level 1 statistics"),
1785 static int perf_stat__get_socket(struct cpu_map
*map
, int cpu
)
1787 return cpu_map__get_socket(map
, cpu
, NULL
);
1790 static int perf_stat__get_core(struct cpu_map
*map
, int cpu
)
1792 return cpu_map__get_core(map
, cpu
, NULL
);
1795 static int cpu_map__get_max(struct cpu_map
*map
)
1799 for (i
= 0; i
< map
->nr
; i
++) {
1800 if (map
->map
[i
] > max
)
1807 static struct cpu_map
*cpus_aggr_map
;
1809 static int perf_stat__get_aggr(aggr_get_id_t get_id
, struct cpu_map
*map
, int idx
)
1816 cpu
= map
->map
[idx
];
1818 if (cpus_aggr_map
->map
[cpu
] == -1)
1819 cpus_aggr_map
->map
[cpu
] = get_id(map
, idx
);
1821 return cpus_aggr_map
->map
[cpu
];
1824 static int perf_stat__get_socket_cached(struct cpu_map
*map
, int idx
)
1826 return perf_stat__get_aggr(perf_stat__get_socket
, map
, idx
);
1829 static int perf_stat__get_core_cached(struct cpu_map
*map
, int idx
)
1831 return perf_stat__get_aggr(perf_stat__get_core
, map
, idx
);
1834 static int perf_stat_init_aggr_mode(void)
1838 switch (stat_config
.aggr_mode
) {
1840 if (cpu_map__build_socket_map(evsel_list
->cpus
, &aggr_map
)) {
1841 perror("cannot build socket map");
1844 aggr_get_id
= perf_stat__get_socket_cached
;
1847 if (cpu_map__build_core_map(evsel_list
->cpus
, &aggr_map
)) {
1848 perror("cannot build core map");
1851 aggr_get_id
= perf_stat__get_core_cached
;
1862 * The evsel_list->cpus is the base we operate on,
1863 * taking the highest cpu number to be the size of
1864 * the aggregation translate cpumap.
1866 nr
= cpu_map__get_max(evsel_list
->cpus
);
1867 cpus_aggr_map
= cpu_map__empty_new(nr
+ 1);
1868 return cpus_aggr_map
? 0 : -ENOMEM
;
1871 static void perf_stat__exit_aggr_mode(void)
1873 cpu_map__put(aggr_map
);
1874 cpu_map__put(cpus_aggr_map
);
1876 cpus_aggr_map
= NULL
;
1879 static inline int perf_env__get_cpu(struct perf_env
*env
, struct cpu_map
*map
, int idx
)
1886 cpu
= map
->map
[idx
];
1888 if (cpu
>= env
->nr_cpus_avail
)
1894 static int perf_env__get_socket(struct cpu_map
*map
, int idx
, void *data
)
1896 struct perf_env
*env
= data
;
1897 int cpu
= perf_env__get_cpu(env
, map
, idx
);
1899 return cpu
== -1 ? -1 : env
->cpu
[cpu
].socket_id
;
1902 static int perf_env__get_core(struct cpu_map
*map
, int idx
, void *data
)
1904 struct perf_env
*env
= data
;
1905 int core
= -1, cpu
= perf_env__get_cpu(env
, map
, idx
);
1908 int socket_id
= env
->cpu
[cpu
].socket_id
;
1911 * Encode socket in upper 16 bits
1912 * core_id is relative to socket, and
1913 * we need a global id. So we combine
1916 core
= (socket_id
<< 16) | (env
->cpu
[cpu
].core_id
& 0xffff);
1922 static int perf_env__build_socket_map(struct perf_env
*env
, struct cpu_map
*cpus
,
1923 struct cpu_map
**sockp
)
1925 return cpu_map__build_map(cpus
, sockp
, perf_env__get_socket
, env
);
1928 static int perf_env__build_core_map(struct perf_env
*env
, struct cpu_map
*cpus
,
1929 struct cpu_map
**corep
)
1931 return cpu_map__build_map(cpus
, corep
, perf_env__get_core
, env
);
1934 static int perf_stat__get_socket_file(struct cpu_map
*map
, int idx
)
1936 return perf_env__get_socket(map
, idx
, &perf_stat
.session
->header
.env
);
1939 static int perf_stat__get_core_file(struct cpu_map
*map
, int idx
)
1941 return perf_env__get_core(map
, idx
, &perf_stat
.session
->header
.env
);
1944 static int perf_stat_init_aggr_mode_file(struct perf_stat
*st
)
1946 struct perf_env
*env
= &st
->session
->header
.env
;
1948 switch (stat_config
.aggr_mode
) {
1950 if (perf_env__build_socket_map(env
, evsel_list
->cpus
, &aggr_map
)) {
1951 perror("cannot build socket map");
1954 aggr_get_id
= perf_stat__get_socket_file
;
1957 if (perf_env__build_core_map(env
, evsel_list
->cpus
, &aggr_map
)) {
1958 perror("cannot build core map");
1961 aggr_get_id
= perf_stat__get_core_file
;
1974 static int topdown_filter_events(const char **attr
, char **str
, bool use_group
)
1981 for (i
= 0; attr
[i
]; i
++) {
1982 if (pmu_have_event("cpu", attr
[i
])) {
1983 len
+= strlen(attr
[i
]) + 1;
1984 attr
[i
- off
] = attr
[i
];
1988 attr
[i
- off
] = NULL
;
1990 *str
= malloc(len
+ 1 + 2);
2000 for (i
= 0; attr
[i
]; i
++) {
2013 __weak
bool arch_topdown_check_group(bool *warn
)
2019 __weak
void arch_topdown_group_warn(void)
2024 * Add default attributes, if there were no attributes specified or
2025 * if -d/--detailed, -d -d or -d -d -d is used:
2027 static int add_default_attributes(void)
2030 struct perf_event_attr default_attrs0
[] = {
2032 { .type
= PERF_TYPE_SOFTWARE
, .config
= PERF_COUNT_SW_TASK_CLOCK
},
2033 { .type
= PERF_TYPE_SOFTWARE
, .config
= PERF_COUNT_SW_CONTEXT_SWITCHES
},
2034 { .type
= PERF_TYPE_SOFTWARE
, .config
= PERF_COUNT_SW_CPU_MIGRATIONS
},
2035 { .type
= PERF_TYPE_SOFTWARE
, .config
= PERF_COUNT_SW_PAGE_FAULTS
},
2037 { .type
= PERF_TYPE_HARDWARE
, .config
= PERF_COUNT_HW_CPU_CYCLES
},
2039 struct perf_event_attr frontend_attrs
[] = {
2040 { .type
= PERF_TYPE_HARDWARE
, .config
= PERF_COUNT_HW_STALLED_CYCLES_FRONTEND
},
2042 struct perf_event_attr backend_attrs
[] = {
2043 { .type
= PERF_TYPE_HARDWARE
, .config
= PERF_COUNT_HW_STALLED_CYCLES_BACKEND
},
2045 struct perf_event_attr default_attrs1
[] = {
2046 { .type
= PERF_TYPE_HARDWARE
, .config
= PERF_COUNT_HW_INSTRUCTIONS
},
2047 { .type
= PERF_TYPE_HARDWARE
, .config
= PERF_COUNT_HW_BRANCH_INSTRUCTIONS
},
2048 { .type
= PERF_TYPE_HARDWARE
, .config
= PERF_COUNT_HW_BRANCH_MISSES
},
2053 * Detailed stats (-d), covering the L1 and last level data caches:
2055 struct perf_event_attr detailed_attrs
[] = {
2057 { .type
= PERF_TYPE_HW_CACHE
,
2059 PERF_COUNT_HW_CACHE_L1D
<< 0 |
2060 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
2061 (PERF_COUNT_HW_CACHE_RESULT_ACCESS
<< 16) },
2063 { .type
= PERF_TYPE_HW_CACHE
,
2065 PERF_COUNT_HW_CACHE_L1D
<< 0 |
2066 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
2067 (PERF_COUNT_HW_CACHE_RESULT_MISS
<< 16) },
2069 { .type
= PERF_TYPE_HW_CACHE
,
2071 PERF_COUNT_HW_CACHE_LL
<< 0 |
2072 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
2073 (PERF_COUNT_HW_CACHE_RESULT_ACCESS
<< 16) },
2075 { .type
= PERF_TYPE_HW_CACHE
,
2077 PERF_COUNT_HW_CACHE_LL
<< 0 |
2078 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
2079 (PERF_COUNT_HW_CACHE_RESULT_MISS
<< 16) },
2083 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
2085 struct perf_event_attr very_detailed_attrs
[] = {
2087 { .type
= PERF_TYPE_HW_CACHE
,
2089 PERF_COUNT_HW_CACHE_L1I
<< 0 |
2090 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
2091 (PERF_COUNT_HW_CACHE_RESULT_ACCESS
<< 16) },
2093 { .type
= PERF_TYPE_HW_CACHE
,
2095 PERF_COUNT_HW_CACHE_L1I
<< 0 |
2096 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
2097 (PERF_COUNT_HW_CACHE_RESULT_MISS
<< 16) },
2099 { .type
= PERF_TYPE_HW_CACHE
,
2101 PERF_COUNT_HW_CACHE_DTLB
<< 0 |
2102 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
2103 (PERF_COUNT_HW_CACHE_RESULT_ACCESS
<< 16) },
2105 { .type
= PERF_TYPE_HW_CACHE
,
2107 PERF_COUNT_HW_CACHE_DTLB
<< 0 |
2108 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
2109 (PERF_COUNT_HW_CACHE_RESULT_MISS
<< 16) },
2111 { .type
= PERF_TYPE_HW_CACHE
,
2113 PERF_COUNT_HW_CACHE_ITLB
<< 0 |
2114 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
2115 (PERF_COUNT_HW_CACHE_RESULT_ACCESS
<< 16) },
2117 { .type
= PERF_TYPE_HW_CACHE
,
2119 PERF_COUNT_HW_CACHE_ITLB
<< 0 |
2120 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
2121 (PERF_COUNT_HW_CACHE_RESULT_MISS
<< 16) },
2126 * Very, very detailed stats (-d -d -d), adding prefetch events:
2128 struct perf_event_attr very_very_detailed_attrs
[] = {
2130 { .type
= PERF_TYPE_HW_CACHE
,
2132 PERF_COUNT_HW_CACHE_L1D
<< 0 |
2133 (PERF_COUNT_HW_CACHE_OP_PREFETCH
<< 8) |
2134 (PERF_COUNT_HW_CACHE_RESULT_ACCESS
<< 16) },
2136 { .type
= PERF_TYPE_HW_CACHE
,
2138 PERF_COUNT_HW_CACHE_L1D
<< 0 |
2139 (PERF_COUNT_HW_CACHE_OP_PREFETCH
<< 8) |
2140 (PERF_COUNT_HW_CACHE_RESULT_MISS
<< 16) },
2143 /* Set attrs if no event is selected and !null_run: */
2147 if (transaction_run
) {
2148 if (pmu_have_event("cpu", "cycles-ct") &&
2149 pmu_have_event("cpu", "el-start"))
2150 err
= parse_events(evsel_list
, transaction_attrs
, NULL
);
2152 err
= parse_events(evsel_list
, transaction_limited_attrs
, NULL
);
2154 fprintf(stderr
, "Cannot set up transaction events\n");
2164 if (stat_config
.aggr_mode
!= AGGR_GLOBAL
&&
2165 stat_config
.aggr_mode
!= AGGR_CORE
) {
2166 pr_err("top down event configuration requires --per-core mode\n");
2169 stat_config
.aggr_mode
= AGGR_CORE
;
2170 if (nr_cgroups
|| !target__has_cpu(&target
)) {
2171 pr_err("top down event configuration requires system-wide mode (-a)\n");
2175 if (!force_metric_only
)
2177 if (topdown_filter_events(topdown_attrs
, &str
,
2178 arch_topdown_check_group(&warn
)) < 0) {
2179 pr_err("Out of memory\n");
2182 if (topdown_attrs
[0] && str
) {
2184 arch_topdown_group_warn();
2185 err
= parse_events(evsel_list
, str
, NULL
);
2188 "Cannot set up top down events %s: %d\n",
2194 fprintf(stderr
, "System does not support topdown\n");
2200 if (!evsel_list
->nr_entries
) {
2201 if (target__has_cpu(&target
))
2202 default_attrs0
[0].config
= PERF_COUNT_SW_CPU_CLOCK
;
2204 if (perf_evlist__add_default_attrs(evsel_list
, default_attrs0
) < 0)
2206 if (pmu_have_event("cpu", "stalled-cycles-frontend")) {
2207 if (perf_evlist__add_default_attrs(evsel_list
,
2208 frontend_attrs
) < 0)
2211 if (pmu_have_event("cpu", "stalled-cycles-backend")) {
2212 if (perf_evlist__add_default_attrs(evsel_list
,
2216 if (perf_evlist__add_default_attrs(evsel_list
, default_attrs1
) < 0)
2220 /* Detailed events get appended to the event list: */
2222 if (detailed_run
< 1)
2225 /* Append detailed run extra attributes: */
2226 if (perf_evlist__add_default_attrs(evsel_list
, detailed_attrs
) < 0)
2229 if (detailed_run
< 2)
2232 /* Append very detailed run extra attributes: */
2233 if (perf_evlist__add_default_attrs(evsel_list
, very_detailed_attrs
) < 0)
2236 if (detailed_run
< 3)
2239 /* Append very, very detailed run extra attributes: */
2240 return perf_evlist__add_default_attrs(evsel_list
, very_very_detailed_attrs
);
2243 static const char * const stat_record_usage
[] = {
2244 "perf stat record [<options>]",
2248 static void init_features(struct perf_session
*session
)
2252 for (feat
= HEADER_FIRST_FEATURE
; feat
< HEADER_LAST_FEATURE
; feat
++)
2253 perf_header__set_feat(&session
->header
, feat
);
2255 perf_header__clear_feat(&session
->header
, HEADER_BUILD_ID
);
2256 perf_header__clear_feat(&session
->header
, HEADER_TRACING_DATA
);
2257 perf_header__clear_feat(&session
->header
, HEADER_BRANCH_STACK
);
2258 perf_header__clear_feat(&session
->header
, HEADER_AUXTRACE
);
2261 static int __cmd_record(int argc
, const char **argv
)
2263 struct perf_session
*session
;
2264 struct perf_data_file
*file
= &perf_stat
.file
;
2266 argc
= parse_options(argc
, argv
, stat_options
, stat_record_usage
,
2267 PARSE_OPT_STOP_AT_NON_OPTION
);
2270 file
->path
= output_name
;
2272 if (run_count
!= 1 || forever
) {
2273 pr_err("Cannot use -r option with perf stat record.\n");
2277 session
= perf_session__new(file
, false, NULL
);
2278 if (session
== NULL
) {
2279 pr_err("Perf session creation failed.\n");
2283 init_features(session
);
2285 session
->evlist
= evsel_list
;
2286 perf_stat
.session
= session
;
2287 perf_stat
.record
= true;
2291 static int process_stat_round_event(struct perf_tool
*tool __maybe_unused
,
2292 union perf_event
*event
,
2293 struct perf_session
*session
)
2295 struct stat_round_event
*stat_round
= &event
->stat_round
;
2296 struct perf_evsel
*counter
;
2297 struct timespec tsh
, *ts
= NULL
;
2298 const char **argv
= session
->header
.env
.cmdline_argv
;
2299 int argc
= session
->header
.env
.nr_cmdline
;
2301 evlist__for_each_entry(evsel_list
, counter
)
2302 perf_stat_process_counter(&stat_config
, counter
);
2304 if (stat_round
->type
== PERF_STAT_ROUND_TYPE__FINAL
)
2305 update_stats(&walltime_nsecs_stats
, stat_round
->time
);
2307 if (stat_config
.interval
&& stat_round
->time
) {
2308 tsh
.tv_sec
= stat_round
->time
/ NSEC_PER_SEC
;
2309 tsh
.tv_nsec
= stat_round
->time
% NSEC_PER_SEC
;
2313 print_counters(ts
, argc
, argv
);
2318 int process_stat_config_event(struct perf_tool
*tool
,
2319 union perf_event
*event
,
2320 struct perf_session
*session __maybe_unused
)
2322 struct perf_stat
*st
= container_of(tool
, struct perf_stat
, tool
);
2324 perf_event__read_stat_config(&stat_config
, &event
->stat_config
);
2326 if (cpu_map__empty(st
->cpus
)) {
2327 if (st
->aggr_mode
!= AGGR_UNSET
)
2328 pr_warning("warning: processing task data, aggregation mode not set\n");
2332 if (st
->aggr_mode
!= AGGR_UNSET
)
2333 stat_config
.aggr_mode
= st
->aggr_mode
;
2335 if (perf_stat
.file
.is_pipe
)
2336 perf_stat_init_aggr_mode();
2338 perf_stat_init_aggr_mode_file(st
);
2343 static int set_maps(struct perf_stat
*st
)
2345 if (!st
->cpus
|| !st
->threads
)
2348 if (WARN_ONCE(st
->maps_allocated
, "stats double allocation\n"))
2351 perf_evlist__set_maps(evsel_list
, st
->cpus
, st
->threads
);
2353 if (perf_evlist__alloc_stats(evsel_list
, true))
2356 st
->maps_allocated
= true;
2361 int process_thread_map_event(struct perf_tool
*tool
,
2362 union perf_event
*event
,
2363 struct perf_session
*session __maybe_unused
)
2365 struct perf_stat
*st
= container_of(tool
, struct perf_stat
, tool
);
2368 pr_warning("Extra thread map event, ignoring.\n");
2372 st
->threads
= thread_map__new_event(&event
->thread_map
);
2376 return set_maps(st
);
2380 int process_cpu_map_event(struct perf_tool
*tool
,
2381 union perf_event
*event
,
2382 struct perf_session
*session __maybe_unused
)
2384 struct perf_stat
*st
= container_of(tool
, struct perf_stat
, tool
);
2385 struct cpu_map
*cpus
;
2388 pr_warning("Extra cpu map event, ignoring.\n");
2392 cpus
= cpu_map__new_data(&event
->cpu_map
.data
);
2397 return set_maps(st
);
2400 static const char * const stat_report_usage
[] = {
2401 "perf stat report [<options>]",
2405 static struct perf_stat perf_stat
= {
2407 .attr
= perf_event__process_attr
,
2408 .event_update
= perf_event__process_event_update
,
2409 .thread_map
= process_thread_map_event
,
2410 .cpu_map
= process_cpu_map_event
,
2411 .stat_config
= process_stat_config_event
,
2412 .stat
= perf_event__process_stat_event
,
2413 .stat_round
= process_stat_round_event
,
2415 .aggr_mode
= AGGR_UNSET
,
2418 static int __cmd_report(int argc
, const char **argv
)
2420 struct perf_session
*session
;
2421 const struct option options
[] = {
2422 OPT_STRING('i', "input", &input_name
, "file", "input file name"),
2423 OPT_SET_UINT(0, "per-socket", &perf_stat
.aggr_mode
,
2424 "aggregate counts per processor socket", AGGR_SOCKET
),
2425 OPT_SET_UINT(0, "per-core", &perf_stat
.aggr_mode
,
2426 "aggregate counts per physical processor core", AGGR_CORE
),
2427 OPT_SET_UINT('A', "no-aggr", &perf_stat
.aggr_mode
,
2428 "disable CPU count aggregation", AGGR_NONE
),
2434 argc
= parse_options(argc
, argv
, options
, stat_report_usage
, 0);
2436 if (!input_name
|| !strlen(input_name
)) {
2437 if (!fstat(STDIN_FILENO
, &st
) && S_ISFIFO(st
.st_mode
))
2440 input_name
= "perf.data";
2443 perf_stat
.file
.path
= input_name
;
2444 perf_stat
.file
.mode
= PERF_DATA_MODE_READ
;
2446 session
= perf_session__new(&perf_stat
.file
, false, &perf_stat
.tool
);
2447 if (session
== NULL
)
2450 perf_stat
.session
= session
;
2451 stat_config
.output
= stderr
;
2452 evsel_list
= session
->evlist
;
2454 ret
= perf_session__process_events(session
);
2458 perf_session__delete(session
);
2462 static void setup_system_wide(int forks
)
2465 * Make system wide (-a) the default target if
2466 * no target was specified and one of following
2467 * conditions is met:
2469 * - there's no workload specified
2470 * - there is workload specified but all requested
2471 * events are system wide events
2473 if (!target__none(&target
))
2477 target
.system_wide
= true;
2479 struct perf_evsel
*counter
;
2481 evlist__for_each_entry(evsel_list
, counter
) {
2482 if (!counter
->system_wide
)
2486 if (evsel_list
->nr_entries
)
2487 target
.system_wide
= true;
2491 int cmd_stat(int argc
, const char **argv
)
2493 const char * const stat_usage
[] = {
2494 "perf stat [<options>] [<command>]",
2497 int status
= -EINVAL
, run_idx
;
2499 FILE *output
= stderr
;
2500 unsigned int interval
;
2501 const char * const stat_subcommands
[] = { "record", "report" };
2503 setlocale(LC_ALL
, "");
2505 evsel_list
= perf_evlist__new();
2506 if (evsel_list
== NULL
)
2509 parse_events__shrink_config_terms();
2510 argc
= parse_options_subcommand(argc
, argv
, stat_options
, stat_subcommands
,
2511 (const char **) stat_usage
,
2512 PARSE_OPT_STOP_AT_NON_OPTION
);
2513 perf_stat__collect_metric_expr(evsel_list
);
2514 perf_stat__init_shadow_stats();
2518 if (!strcmp(csv_sep
, "\\t"))
2521 csv_sep
= DEFAULT_SEPARATOR
;
2523 if (argc
&& !strncmp(argv
[0], "rec", 3)) {
2524 argc
= __cmd_record(argc
, argv
);
2527 } else if (argc
&& !strncmp(argv
[0], "rep", 3))
2528 return __cmd_report(argc
, argv
);
2530 interval
= stat_config
.interval
;
2533 * For record command the -o is already taken care of.
2535 if (!STAT_RECORD
&& output_name
&& strcmp(output_name
, "-"))
2538 if (output_name
&& output_fd
) {
2539 fprintf(stderr
, "cannot use both --output and --log-fd\n");
2540 parse_options_usage(stat_usage
, stat_options
, "o", 1);
2541 parse_options_usage(NULL
, stat_options
, "log-fd", 0);
2545 if (metric_only
&& stat_config
.aggr_mode
== AGGR_THREAD
) {
2546 fprintf(stderr
, "--metric-only is not supported with --per-thread\n");
2550 if (metric_only
&& run_count
> 1) {
2551 fprintf(stderr
, "--metric-only is not supported with -r\n");
2555 if (output_fd
< 0) {
2556 fprintf(stderr
, "argument to --log-fd must be a > 0\n");
2557 parse_options_usage(stat_usage
, stat_options
, "log-fd", 0);
2563 mode
= append_file
? "a" : "w";
2565 output
= fopen(output_name
, mode
);
2567 perror("failed to create output file");
2570 clock_gettime(CLOCK_REALTIME
, &tm
);
2571 fprintf(output
, "# started on %s\n", ctime(&tm
.tv_sec
));
2572 } else if (output_fd
> 0) {
2573 mode
= append_file
? "a" : "w";
2574 output
= fdopen(output_fd
, mode
);
2576 perror("Failed opening logfd");
2581 stat_config
.output
= output
;
2584 * let the spreadsheet do the pretty-printing
2587 /* User explicitly passed -B? */
2588 if (big_num_opt
== 1) {
2589 fprintf(stderr
, "-B option not supported with -x\n");
2590 parse_options_usage(stat_usage
, stat_options
, "B", 1);
2591 parse_options_usage(NULL
, stat_options
, "x", 1);
2593 } else /* Nope, so disable big number formatting */
2595 } else if (big_num_opt
== 0) /* User passed --no-big-num */
2598 setup_system_wide(argc
);
2600 if (run_count
< 0) {
2601 pr_err("Run count must be a positive number\n");
2602 parse_options_usage(stat_usage
, stat_options
, "r", 1);
2604 } else if (run_count
== 0) {
2609 if ((stat_config
.aggr_mode
== AGGR_THREAD
) && !target__has_task(&target
)) {
2610 fprintf(stderr
, "The --per-thread option is only available "
2611 "when monitoring via -p -t options.\n");
2612 parse_options_usage(NULL
, stat_options
, "p", 1);
2613 parse_options_usage(NULL
, stat_options
, "t", 1);
2618 * no_aggr, cgroup are for system-wide only
2619 * --per-thread is aggregated per thread, we dont mix it with cpu mode
2621 if (((stat_config
.aggr_mode
!= AGGR_GLOBAL
&&
2622 stat_config
.aggr_mode
!= AGGR_THREAD
) || nr_cgroups
) &&
2623 !target__has_cpu(&target
)) {
2624 fprintf(stderr
, "both cgroup and no-aggregation "
2625 "modes only available in system-wide mode\n");
2627 parse_options_usage(stat_usage
, stat_options
, "G", 1);
2628 parse_options_usage(NULL
, stat_options
, "A", 1);
2629 parse_options_usage(NULL
, stat_options
, "a", 1);
2633 if (add_default_attributes())
2636 target__validate(&target
);
2638 if (perf_evlist__create_maps(evsel_list
, &target
) < 0) {
2639 if (target__has_task(&target
)) {
2640 pr_err("Problems finding threads of monitor\n");
2641 parse_options_usage(stat_usage
, stat_options
, "p", 1);
2642 parse_options_usage(NULL
, stat_options
, "t", 1);
2643 } else if (target__has_cpu(&target
)) {
2644 perror("failed to parse CPUs map");
2645 parse_options_usage(stat_usage
, stat_options
, "C", 1);
2646 parse_options_usage(NULL
, stat_options
, "a", 1);
2652 * Initialize thread_map with comm names,
2653 * so we could print it out on output.
2655 if (stat_config
.aggr_mode
== AGGR_THREAD
)
2656 thread_map__read_comms(evsel_list
->threads
);
2658 if (interval
&& interval
< 100) {
2659 if (interval
< 10) {
2660 pr_err("print interval must be >= 10ms\n");
2661 parse_options_usage(stat_usage
, stat_options
, "I", 1);
2664 pr_warning("print interval < 100ms. "
2665 "The overhead percentage could be high in some cases. "
2666 "Please proceed with caution.\n");
2669 if (perf_evlist__alloc_stats(evsel_list
, interval
))
2672 if (perf_stat_init_aggr_mode())
2676 * We dont want to block the signals - that would cause
2677 * child tasks to inherit that and Ctrl-C would not work.
2678 * What we want is for Ctrl-C to work in the exec()-ed
2679 * task, but being ignored by perf stat itself:
2683 signal(SIGINT
, skip_signal
);
2684 signal(SIGCHLD
, skip_signal
);
2685 signal(SIGALRM
, skip_signal
);
2686 signal(SIGABRT
, skip_signal
);
2689 for (run_idx
= 0; forever
|| run_idx
< run_count
; run_idx
++) {
2690 if (run_count
!= 1 && verbose
> 0)
2691 fprintf(output
, "[ perf stat: executing run #%d ... ]\n",
2694 status
= run_perf_stat(argc
, argv
);
2695 if (forever
&& status
!= -1) {
2696 print_counters(NULL
, argc
, argv
);
2697 perf_stat__reset_stats();
2701 if (!forever
&& status
!= -1 && !interval
)
2702 print_counters(NULL
, argc
, argv
);
2706 * We synthesize the kernel mmap record just so that older tools
2707 * don't emit warnings about not being able to resolve symbols
2708 * due to /proc/sys/kernel/kptr_restrict settings and instear provide
2709 * a saner message about no samples being in the perf.data file.
2711 * This also serves to suppress a warning about f_header.data.size == 0
2712 * in header.c at the moment 'perf stat record' gets introduced, which
2713 * is not really needed once we start adding the stat specific PERF_RECORD_
2714 * records, but the need to suppress the kptr_restrict messages in older
2715 * tools remain -acme
2717 int fd
= perf_data_file__fd(&perf_stat
.file
);
2718 int err
= perf_event__synthesize_kernel_mmap((void *)&perf_stat
,
2719 process_synthesized_event
,
2720 &perf_stat
.session
->machines
.host
);
2722 pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
2723 "older tools may produce warnings about this file\n.");
2727 if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats
.max
, FINAL
))
2728 pr_err("failed to write stat round event\n");
2731 if (!perf_stat
.file
.is_pipe
) {
2732 perf_stat
.session
->header
.data_size
+= perf_stat
.bytes_written
;
2733 perf_session__write_header(perf_stat
.session
, evsel_list
, fd
, true);
2736 perf_session__delete(perf_stat
.session
);
2739 perf_stat__exit_aggr_mode();
2740 perf_evlist__free_stats(evsel_list
);
2742 perf_evlist__delete(evsel_list
);