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 "util/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/color.h"
56 #include "util/stat.h"
57 #include "util/header.h"
58 #include "util/cpumap.h"
59 #include "util/thread.h"
60 #include "util/thread_map.h"
61 #include "util/counts.h"
64 #include <sys/prctl.h>
67 #define DEFAULT_SEPARATOR " "
68 #define CNTR_NOT_SUPPORTED "<not supported>"
69 #define CNTR_NOT_COUNTED "<not counted>"
71 static void print_counters(struct timespec
*ts
, int argc
, const char **argv
);
73 /* Default events used for perf stat -T */
74 static const char *transaction_attrs
= {
86 /* More limited version when the CPU does not have all events. */
87 static const char * transaction_limited_attrs
= {
97 static struct perf_evlist
*evsel_list
;
99 static struct target target
= {
103 static int run_count
= 1;
104 static bool no_inherit
= false;
105 static volatile pid_t child_pid
= -1;
106 static bool null_run
= false;
107 static int detailed_run
= 0;
108 static bool transaction_run
;
109 static bool big_num
= true;
110 static int big_num_opt
= -1;
111 static const char *csv_sep
= NULL
;
112 static bool csv_output
= false;
113 static bool group
= false;
114 static const char *pre_cmd
= NULL
;
115 static const char *post_cmd
= NULL
;
116 static bool sync_run
= false;
117 static unsigned int initial_delay
= 0;
118 static unsigned int unit_width
= 4; /* strlen("unit") */
119 static bool forever
= false;
120 static struct timespec ref_time
;
121 static struct cpu_map
*aggr_map
;
122 static int (*aggr_get_id
)(struct cpu_map
*m
, int cpu
);
124 static volatile int done
= 0;
126 static struct perf_stat_config stat_config
= {
127 .aggr_mode
= AGGR_GLOBAL
,
131 static inline void diff_timespec(struct timespec
*r
, struct timespec
*a
,
134 r
->tv_sec
= a
->tv_sec
- b
->tv_sec
;
135 if (a
->tv_nsec
< b
->tv_nsec
) {
136 r
->tv_nsec
= a
->tv_nsec
+ 1000000000L - b
->tv_nsec
;
139 r
->tv_nsec
= a
->tv_nsec
- b
->tv_nsec
;
143 static void perf_stat__reset_stats(void)
145 perf_evlist__reset_stats(evsel_list
);
146 perf_stat__reset_shadow_stats();
149 static int create_perf_stat_counter(struct perf_evsel
*evsel
)
151 struct perf_event_attr
*attr
= &evsel
->attr
;
153 if (stat_config
.scale
)
154 attr
->read_format
= PERF_FORMAT_TOTAL_TIME_ENABLED
|
155 PERF_FORMAT_TOTAL_TIME_RUNNING
;
157 attr
->inherit
= !no_inherit
;
159 if (target__has_cpu(&target
))
160 return perf_evsel__open_per_cpu(evsel
, perf_evsel__cpus(evsel
));
162 if (!target__has_task(&target
) && perf_evsel__is_group_leader(evsel
)) {
165 attr
->enable_on_exec
= 1;
168 return perf_evsel__open_per_thread(evsel
, evsel_list
->threads
);
172 * Does the counter have nsecs as a unit?
174 static inline int nsec_counter(struct perf_evsel
*evsel
)
176 if (perf_evsel__match(evsel
, SOFTWARE
, SW_CPU_CLOCK
) ||
177 perf_evsel__match(evsel
, SOFTWARE
, SW_TASK_CLOCK
))
184 * Read out the results of a single counter:
185 * do not aggregate counts across CPUs in system-wide mode
187 static int read_counter(struct perf_evsel
*counter
)
189 int nthreads
= thread_map__nr(evsel_list
->threads
);
190 int ncpus
= perf_evsel__nr_cpus(counter
);
193 if (!counter
->supported
)
196 if (counter
->system_wide
)
199 for (thread
= 0; thread
< nthreads
; thread
++) {
200 for (cpu
= 0; cpu
< ncpus
; cpu
++) {
201 struct perf_counts_values
*count
;
203 count
= perf_counts(counter
->counts
, cpu
, thread
);
204 if (perf_evsel__read(counter
, cpu
, thread
, count
))
212 static void read_counters(bool close_counters
)
214 struct perf_evsel
*counter
;
216 evlist__for_each(evsel_list
, counter
) {
217 if (read_counter(counter
))
218 pr_warning("failed to read counter %s\n", counter
->name
);
220 if (perf_stat_process_counter(&stat_config
, counter
))
221 pr_warning("failed to process counter %s\n", counter
->name
);
223 if (close_counters
) {
224 perf_evsel__close_fd(counter
, perf_evsel__nr_cpus(counter
),
225 thread_map__nr(evsel_list
->threads
));
230 static void process_interval(void)
232 struct timespec ts
, rs
;
234 read_counters(false);
236 clock_gettime(CLOCK_MONOTONIC
, &ts
);
237 diff_timespec(&rs
, &ts
, &ref_time
);
239 print_counters(&rs
, 0, NULL
);
242 static void handle_initial_delay(void)
244 struct perf_evsel
*counter
;
247 const int ncpus
= cpu_map__nr(evsel_list
->cpus
),
248 nthreads
= thread_map__nr(evsel_list
->threads
);
250 usleep(initial_delay
* 1000);
251 evlist__for_each(evsel_list
, counter
)
252 perf_evsel__enable(counter
, ncpus
, nthreads
);
256 static volatile int workload_exec_errno
;
259 * perf_evlist__prepare_workload will send a SIGUSR1
260 * if the fork fails, since we asked by setting its
261 * want_signal to true.
263 static void workload_exec_failed_signal(int signo __maybe_unused
, siginfo_t
*info
,
264 void *ucontext __maybe_unused
)
266 workload_exec_errno
= info
->si_value
.sival_int
;
269 static int __run_perf_stat(int argc
, const char **argv
)
271 int interval
= stat_config
.interval
;
273 unsigned long long t0
, t1
;
274 struct perf_evsel
*counter
;
278 const bool forks
= (argc
> 0);
281 ts
.tv_sec
= interval
/ 1000;
282 ts
.tv_nsec
= (interval
% 1000) * 1000000;
289 if (perf_evlist__prepare_workload(evsel_list
, &target
, argv
, false,
290 workload_exec_failed_signal
) < 0) {
291 perror("failed to prepare workload");
294 child_pid
= evsel_list
->workload
.pid
;
298 perf_evlist__set_leader(evsel_list
);
300 evlist__for_each(evsel_list
, counter
) {
301 if (create_perf_stat_counter(counter
) < 0) {
303 * PPC returns ENXIO for HW counters until 2.6.37
304 * (behavior changed with commit b0a873e).
306 if (errno
== EINVAL
|| errno
== ENOSYS
||
307 errno
== ENOENT
|| errno
== EOPNOTSUPP
||
310 ui__warning("%s event is not supported by the kernel.\n",
311 perf_evsel__name(counter
));
312 counter
->supported
= false;
314 if ((counter
->leader
!= counter
) ||
315 !(counter
->leader
->nr_members
> 1))
319 perf_evsel__open_strerror(counter
, &target
,
320 errno
, msg
, sizeof(msg
));
321 ui__error("%s\n", msg
);
324 kill(child_pid
, SIGTERM
);
328 counter
->supported
= true;
330 l
= strlen(counter
->unit
);
335 if (perf_evlist__apply_filters(evsel_list
, &counter
)) {
336 error("failed to set filter \"%s\" on event %s with %d (%s)\n",
337 counter
->filter
, perf_evsel__name(counter
), errno
,
338 strerror_r(errno
, msg
, sizeof(msg
)));
343 * Enable counters and exec the command:
346 clock_gettime(CLOCK_MONOTONIC
, &ref_time
);
349 perf_evlist__start_workload(evsel_list
);
350 handle_initial_delay();
353 while (!waitpid(child_pid
, &status
, WNOHANG
)) {
354 nanosleep(&ts
, NULL
);
360 if (workload_exec_errno
) {
361 const char *emsg
= strerror_r(workload_exec_errno
, msg
, sizeof(msg
));
362 pr_err("Workload failed: %s\n", emsg
);
366 if (WIFSIGNALED(status
))
367 psignal(WTERMSIG(status
), argv
[0]);
369 handle_initial_delay();
371 nanosleep(&ts
, NULL
);
379 update_stats(&walltime_nsecs_stats
, t1
- t0
);
383 return WEXITSTATUS(status
);
386 static int run_perf_stat(int argc
, const char **argv
)
391 ret
= system(pre_cmd
);
399 ret
= __run_perf_stat(argc
, argv
);
404 ret
= system(post_cmd
);
412 static void print_running(u64 run
, u64 ena
)
415 fprintf(stat_config
.output
, "%s%" PRIu64
"%s%.2f",
419 ena
? 100.0 * run
/ ena
: 100.0);
420 } else if (run
!= ena
) {
421 fprintf(stat_config
.output
, " (%.2f%%)", 100.0 * run
/ ena
);
425 static void print_noise_pct(double total
, double avg
)
427 double pct
= rel_stddev_stats(total
, avg
);
430 fprintf(stat_config
.output
, "%s%.2f%%", csv_sep
, pct
);
432 fprintf(stat_config
.output
, " ( +-%6.2f%% )", pct
);
435 static void print_noise(struct perf_evsel
*evsel
, double avg
)
437 struct perf_stat
*ps
;
443 print_noise_pct(stddev_stats(&ps
->res_stats
[0]), avg
);
446 static void aggr_printout(struct perf_evsel
*evsel
, int id
, int nr
)
448 switch (stat_config
.aggr_mode
) {
450 fprintf(stat_config
.output
, "S%d-C%*d%s%*d%s",
451 cpu_map__id_to_socket(id
),
453 cpu_map__id_to_cpu(id
),
460 fprintf(stat_config
.output
, "S%*d%s%*d%s",
469 fprintf(stat_config
.output
, "CPU%*d%s",
471 perf_evsel__cpus(evsel
)->map
[id
], csv_sep
);
474 fprintf(stat_config
.output
, "%*s-%*d%s",
476 thread_map__comm(evsel
->threads
, id
),
478 thread_map__pid(evsel
->threads
, id
),
487 static void nsec_printout(int id
, int nr
, struct perf_evsel
*evsel
, double avg
)
489 FILE *output
= stat_config
.output
;
490 double msecs
= avg
/ 1e6
;
491 const char *fmt_v
, *fmt_n
;
494 fmt_v
= csv_output
? "%.6f%s" : "%18.6f%s";
495 fmt_n
= csv_output
? "%s" : "%-25s";
497 aggr_printout(evsel
, id
, nr
);
499 scnprintf(name
, sizeof(name
), "%s%s",
500 perf_evsel__name(evsel
), csv_output
? "" : " (msec)");
502 fprintf(output
, fmt_v
, msecs
, csv_sep
);
505 fprintf(output
, "%s%s", evsel
->unit
, csv_sep
);
507 fprintf(output
, "%-*s%s", unit_width
, evsel
->unit
, csv_sep
);
509 fprintf(output
, fmt_n
, name
);
512 fprintf(output
, "%s%s", csv_sep
, evsel
->cgrp
->name
);
514 if (csv_output
|| stat_config
.interval
)
517 if (perf_evsel__match(evsel
, SOFTWARE
, SW_TASK_CLOCK
))
518 fprintf(output
, " # %8.3f CPUs utilized ",
519 avg
/ avg_stats(&walltime_nsecs_stats
));
521 fprintf(output
, " ");
524 static void abs_printout(int id
, int nr
, struct perf_evsel
*evsel
, double avg
)
526 FILE *output
= stat_config
.output
;
527 double sc
= evsel
->scale
;
529 int cpu
= cpu_map__id_to_cpu(id
);
532 fmt
= sc
!= 1.0 ? "%.2f%s" : "%.0f%s";
535 fmt
= sc
!= 1.0 ? "%'18.2f%s" : "%'18.0f%s";
537 fmt
= sc
!= 1.0 ? "%18.2f%s" : "%18.0f%s";
540 aggr_printout(evsel
, id
, nr
);
542 if (stat_config
.aggr_mode
== AGGR_GLOBAL
)
545 fprintf(output
, fmt
, avg
, csv_sep
);
548 fprintf(output
, "%-*s%s",
549 csv_output
? 0 : unit_width
,
550 evsel
->unit
, csv_sep
);
552 fprintf(output
, "%-*s", csv_output
? 0 : 25, perf_evsel__name(evsel
));
555 fprintf(output
, "%s%s", csv_sep
, evsel
->cgrp
->name
);
557 if (csv_output
|| stat_config
.interval
)
560 perf_stat__print_shadow_stats(output
, evsel
, avg
, cpu
,
561 stat_config
.aggr_mode
);
564 static void print_aggr(char *prefix
)
566 FILE *output
= stat_config
.output
;
567 struct perf_evsel
*counter
;
568 int cpu
, s
, s2
, id
, nr
;
572 if (!(aggr_map
|| aggr_get_id
))
575 for (s
= 0; s
< aggr_map
->nr
; s
++) {
576 id
= aggr_map
->map
[s
];
577 evlist__for_each(evsel_list
, counter
) {
580 for (cpu
= 0; cpu
< perf_evsel__nr_cpus(counter
); cpu
++) {
581 s2
= aggr_get_id(perf_evsel__cpus(counter
), cpu
);
584 val
+= perf_counts(counter
->counts
, cpu
, 0)->val
;
585 ena
+= perf_counts(counter
->counts
, cpu
, 0)->ena
;
586 run
+= perf_counts(counter
->counts
, cpu
, 0)->run
;
590 fprintf(output
, "%s", prefix
);
592 if (run
== 0 || ena
== 0) {
593 aggr_printout(counter
, id
, nr
);
595 fprintf(output
, "%*s%s",
597 counter
->supported
? CNTR_NOT_COUNTED
: CNTR_NOT_SUPPORTED
,
600 fprintf(output
, "%-*s%s",
601 csv_output
? 0 : unit_width
,
602 counter
->unit
, csv_sep
);
604 fprintf(output
, "%*s",
605 csv_output
? 0 : -25,
606 perf_evsel__name(counter
));
609 fprintf(output
, "%s%s",
610 csv_sep
, counter
->cgrp
->name
);
612 print_running(run
, ena
);
616 uval
= val
* counter
->scale
;
618 if (nsec_counter(counter
))
619 nsec_printout(id
, nr
, counter
, uval
);
621 abs_printout(id
, nr
, counter
, uval
);
624 print_noise(counter
, 1.0);
626 print_running(run
, ena
);
632 static void print_aggr_thread(struct perf_evsel
*counter
, char *prefix
)
634 FILE *output
= stat_config
.output
;
635 int nthreads
= thread_map__nr(counter
->threads
);
636 int ncpus
= cpu_map__nr(counter
->cpus
);
640 for (thread
= 0; thread
< nthreads
; thread
++) {
641 u64 ena
= 0, run
= 0, val
= 0;
643 for (cpu
= 0; cpu
< ncpus
; cpu
++) {
644 val
+= perf_counts(counter
->counts
, cpu
, thread
)->val
;
645 ena
+= perf_counts(counter
->counts
, cpu
, thread
)->ena
;
646 run
+= perf_counts(counter
->counts
, cpu
, thread
)->run
;
650 fprintf(output
, "%s", prefix
);
652 uval
= val
* counter
->scale
;
654 if (nsec_counter(counter
))
655 nsec_printout(thread
, 0, counter
, uval
);
657 abs_printout(thread
, 0, counter
, uval
);
660 print_noise(counter
, 1.0);
662 print_running(run
, ena
);
668 * Print out the results of a single counter:
669 * aggregated counts in system-wide mode
671 static void print_counter_aggr(struct perf_evsel
*counter
, char *prefix
)
673 FILE *output
= stat_config
.output
;
674 struct perf_stat
*ps
= counter
->priv
;
675 double avg
= avg_stats(&ps
->res_stats
[0]);
676 int scaled
= counter
->counts
->scaled
;
678 double avg_enabled
, avg_running
;
680 avg_enabled
= avg_stats(&ps
->res_stats
[1]);
681 avg_running
= avg_stats(&ps
->res_stats
[2]);
684 fprintf(output
, "%s", prefix
);
686 if (scaled
== -1 || !counter
->supported
) {
687 fprintf(output
, "%*s%s",
689 counter
->supported
? CNTR_NOT_COUNTED
: CNTR_NOT_SUPPORTED
,
691 fprintf(output
, "%-*s%s",
692 csv_output
? 0 : unit_width
,
693 counter
->unit
, csv_sep
);
694 fprintf(output
, "%*s",
695 csv_output
? 0 : -25,
696 perf_evsel__name(counter
));
699 fprintf(output
, "%s%s", csv_sep
, counter
->cgrp
->name
);
701 print_running(avg_running
, avg_enabled
);
706 uval
= avg
* counter
->scale
;
708 if (nsec_counter(counter
))
709 nsec_printout(-1, 0, counter
, uval
);
711 abs_printout(-1, 0, counter
, uval
);
713 print_noise(counter
, avg
);
715 print_running(avg_running
, avg_enabled
);
716 fprintf(output
, "\n");
720 * Print out the results of a single counter:
721 * does not use aggregated count in system-wide
723 static void print_counter(struct perf_evsel
*counter
, char *prefix
)
725 FILE *output
= stat_config
.output
;
730 for (cpu
= 0; cpu
< perf_evsel__nr_cpus(counter
); cpu
++) {
731 val
= perf_counts(counter
->counts
, cpu
, 0)->val
;
732 ena
= perf_counts(counter
->counts
, cpu
, 0)->ena
;
733 run
= perf_counts(counter
->counts
, cpu
, 0)->run
;
736 fprintf(output
, "%s", prefix
);
738 if (run
== 0 || ena
== 0) {
739 fprintf(output
, "CPU%*d%s%*s%s",
741 perf_evsel__cpus(counter
)->map
[cpu
], csv_sep
,
743 counter
->supported
? CNTR_NOT_COUNTED
: CNTR_NOT_SUPPORTED
,
746 fprintf(output
, "%-*s%s",
747 csv_output
? 0 : unit_width
,
748 counter
->unit
, csv_sep
);
750 fprintf(output
, "%*s",
751 csv_output
? 0 : -25,
752 perf_evsel__name(counter
));
755 fprintf(output
, "%s%s",
756 csv_sep
, counter
->cgrp
->name
);
758 print_running(run
, ena
);
763 uval
= val
* counter
->scale
;
765 if (nsec_counter(counter
))
766 nsec_printout(cpu
, 0, counter
, uval
);
768 abs_printout(cpu
, 0, counter
, uval
);
771 print_noise(counter
, 1.0);
772 print_running(run
, ena
);
778 static void print_interval(char *prefix
, struct timespec
*ts
)
780 FILE *output
= stat_config
.output
;
781 static int num_print_interval
;
783 sprintf(prefix
, "%6lu.%09lu%s", ts
->tv_sec
, ts
->tv_nsec
, csv_sep
);
785 if (num_print_interval
== 0 && !csv_output
) {
786 switch (stat_config
.aggr_mode
) {
788 fprintf(output
, "# time socket cpus counts %*s events\n", unit_width
, "unit");
791 fprintf(output
, "# time core cpus counts %*s events\n", unit_width
, "unit");
794 fprintf(output
, "# time CPU counts %*s events\n", unit_width
, "unit");
797 fprintf(output
, "# time comm-pid counts %*s events\n", unit_width
, "unit");
801 fprintf(output
, "# time counts %*s events\n", unit_width
, "unit");
805 if (++num_print_interval
== 25)
806 num_print_interval
= 0;
809 static void print_header(int argc
, const char **argv
)
811 FILE *output
= stat_config
.output
;
817 fprintf(output
, "\n");
818 fprintf(output
, " Performance counter stats for ");
819 if (target
.system_wide
)
820 fprintf(output
, "\'system wide");
821 else if (target
.cpu_list
)
822 fprintf(output
, "\'CPU(s) %s", target
.cpu_list
);
823 else if (!target__has_task(&target
)) {
824 fprintf(output
, "\'%s", argv
[0]);
825 for (i
= 1; i
< argc
; i
++)
826 fprintf(output
, " %s", argv
[i
]);
827 } else if (target
.pid
)
828 fprintf(output
, "process id \'%s", target
.pid
);
830 fprintf(output
, "thread id \'%s", target
.tid
);
832 fprintf(output
, "\'");
834 fprintf(output
, " (%d runs)", run_count
);
835 fprintf(output
, ":\n\n");
839 static void print_footer(void)
841 FILE *output
= stat_config
.output
;
844 fprintf(output
, "\n");
845 fprintf(output
, " %17.9f seconds time elapsed",
846 avg_stats(&walltime_nsecs_stats
)/1e9
);
848 fprintf(output
, " ");
849 print_noise_pct(stddev_stats(&walltime_nsecs_stats
),
850 avg_stats(&walltime_nsecs_stats
));
852 fprintf(output
, "\n\n");
855 static void print_counters(struct timespec
*ts
, int argc
, const char **argv
)
857 int interval
= stat_config
.interval
;
858 struct perf_evsel
*counter
;
859 char buf
[64], *prefix
= NULL
;
862 print_interval(prefix
= buf
, ts
);
864 print_header(argc
, argv
);
866 switch (stat_config
.aggr_mode
) {
872 evlist__for_each(evsel_list
, counter
)
873 print_aggr_thread(counter
, prefix
);
876 evlist__for_each(evsel_list
, counter
)
877 print_counter_aggr(counter
, prefix
);
880 evlist__for_each(evsel_list
, counter
)
881 print_counter(counter
, prefix
);
887 if (!interval
&& !csv_output
)
890 fflush(stat_config
.output
);
893 static volatile int signr
= -1;
895 static void skip_signal(int signo
)
897 if ((child_pid
== -1) || stat_config
.interval
)
902 * render child_pid harmless
903 * won't send SIGTERM to a random
904 * process in case of race condition
905 * and fast PID recycling
910 static void sig_atexit(void)
915 * avoid race condition with SIGCHLD handler
916 * in skip_signal() which is modifying child_pid
917 * goal is to avoid send SIGTERM to a random
921 sigaddset(&set
, SIGCHLD
);
922 sigprocmask(SIG_BLOCK
, &set
, &oset
);
925 kill(child_pid
, SIGTERM
);
927 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
932 signal(signr
, SIG_DFL
);
933 kill(getpid(), signr
);
936 static int stat__set_big_num(const struct option
*opt __maybe_unused
,
937 const char *s __maybe_unused
, int unset
)
939 big_num_opt
= unset
? 0 : 1;
943 static int perf_stat_init_aggr_mode(void)
945 switch (stat_config
.aggr_mode
) {
947 if (cpu_map__build_socket_map(evsel_list
->cpus
, &aggr_map
)) {
948 perror("cannot build socket map");
951 aggr_get_id
= cpu_map__get_socket
;
954 if (cpu_map__build_core_map(evsel_list
->cpus
, &aggr_map
)) {
955 perror("cannot build core map");
958 aggr_get_id
= cpu_map__get_core
;
970 * Add default attributes, if there were no attributes specified or
971 * if -d/--detailed, -d -d or -d -d -d is used:
973 static int add_default_attributes(void)
975 struct perf_event_attr default_attrs
[] = {
977 { .type
= PERF_TYPE_SOFTWARE
, .config
= PERF_COUNT_SW_TASK_CLOCK
},
978 { .type
= PERF_TYPE_SOFTWARE
, .config
= PERF_COUNT_SW_CONTEXT_SWITCHES
},
979 { .type
= PERF_TYPE_SOFTWARE
, .config
= PERF_COUNT_SW_CPU_MIGRATIONS
},
980 { .type
= PERF_TYPE_SOFTWARE
, .config
= PERF_COUNT_SW_PAGE_FAULTS
},
982 { .type
= PERF_TYPE_HARDWARE
, .config
= PERF_COUNT_HW_CPU_CYCLES
},
983 { .type
= PERF_TYPE_HARDWARE
, .config
= PERF_COUNT_HW_STALLED_CYCLES_FRONTEND
},
984 { .type
= PERF_TYPE_HARDWARE
, .config
= PERF_COUNT_HW_STALLED_CYCLES_BACKEND
},
985 { .type
= PERF_TYPE_HARDWARE
, .config
= PERF_COUNT_HW_INSTRUCTIONS
},
986 { .type
= PERF_TYPE_HARDWARE
, .config
= PERF_COUNT_HW_BRANCH_INSTRUCTIONS
},
987 { .type
= PERF_TYPE_HARDWARE
, .config
= PERF_COUNT_HW_BRANCH_MISSES
},
992 * Detailed stats (-d), covering the L1 and last level data caches:
994 struct perf_event_attr detailed_attrs
[] = {
996 { .type
= PERF_TYPE_HW_CACHE
,
998 PERF_COUNT_HW_CACHE_L1D
<< 0 |
999 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
1000 (PERF_COUNT_HW_CACHE_RESULT_ACCESS
<< 16) },
1002 { .type
= PERF_TYPE_HW_CACHE
,
1004 PERF_COUNT_HW_CACHE_L1D
<< 0 |
1005 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
1006 (PERF_COUNT_HW_CACHE_RESULT_MISS
<< 16) },
1008 { .type
= PERF_TYPE_HW_CACHE
,
1010 PERF_COUNT_HW_CACHE_LL
<< 0 |
1011 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
1012 (PERF_COUNT_HW_CACHE_RESULT_ACCESS
<< 16) },
1014 { .type
= PERF_TYPE_HW_CACHE
,
1016 PERF_COUNT_HW_CACHE_LL
<< 0 |
1017 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
1018 (PERF_COUNT_HW_CACHE_RESULT_MISS
<< 16) },
1022 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
1024 struct perf_event_attr very_detailed_attrs
[] = {
1026 { .type
= PERF_TYPE_HW_CACHE
,
1028 PERF_COUNT_HW_CACHE_L1I
<< 0 |
1029 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
1030 (PERF_COUNT_HW_CACHE_RESULT_ACCESS
<< 16) },
1032 { .type
= PERF_TYPE_HW_CACHE
,
1034 PERF_COUNT_HW_CACHE_L1I
<< 0 |
1035 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
1036 (PERF_COUNT_HW_CACHE_RESULT_MISS
<< 16) },
1038 { .type
= PERF_TYPE_HW_CACHE
,
1040 PERF_COUNT_HW_CACHE_DTLB
<< 0 |
1041 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
1042 (PERF_COUNT_HW_CACHE_RESULT_ACCESS
<< 16) },
1044 { .type
= PERF_TYPE_HW_CACHE
,
1046 PERF_COUNT_HW_CACHE_DTLB
<< 0 |
1047 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
1048 (PERF_COUNT_HW_CACHE_RESULT_MISS
<< 16) },
1050 { .type
= PERF_TYPE_HW_CACHE
,
1052 PERF_COUNT_HW_CACHE_ITLB
<< 0 |
1053 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
1054 (PERF_COUNT_HW_CACHE_RESULT_ACCESS
<< 16) },
1056 { .type
= PERF_TYPE_HW_CACHE
,
1058 PERF_COUNT_HW_CACHE_ITLB
<< 0 |
1059 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
1060 (PERF_COUNT_HW_CACHE_RESULT_MISS
<< 16) },
1065 * Very, very detailed stats (-d -d -d), adding prefetch events:
1067 struct perf_event_attr very_very_detailed_attrs
[] = {
1069 { .type
= PERF_TYPE_HW_CACHE
,
1071 PERF_COUNT_HW_CACHE_L1D
<< 0 |
1072 (PERF_COUNT_HW_CACHE_OP_PREFETCH
<< 8) |
1073 (PERF_COUNT_HW_CACHE_RESULT_ACCESS
<< 16) },
1075 { .type
= PERF_TYPE_HW_CACHE
,
1077 PERF_COUNT_HW_CACHE_L1D
<< 0 |
1078 (PERF_COUNT_HW_CACHE_OP_PREFETCH
<< 8) |
1079 (PERF_COUNT_HW_CACHE_RESULT_MISS
<< 16) },
1082 /* Set attrs if no event is selected and !null_run: */
1086 if (transaction_run
) {
1088 if (pmu_have_event("cpu", "cycles-ct") &&
1089 pmu_have_event("cpu", "el-start"))
1090 err
= parse_events(evsel_list
, transaction_attrs
, NULL
);
1092 err
= parse_events(evsel_list
, transaction_limited_attrs
, NULL
);
1094 fprintf(stderr
, "Cannot set up transaction events\n");
1100 if (!evsel_list
->nr_entries
) {
1101 if (perf_evlist__add_default_attrs(evsel_list
, default_attrs
) < 0)
1105 /* Detailed events get appended to the event list: */
1107 if (detailed_run
< 1)
1110 /* Append detailed run extra attributes: */
1111 if (perf_evlist__add_default_attrs(evsel_list
, detailed_attrs
) < 0)
1114 if (detailed_run
< 2)
1117 /* Append very detailed run extra attributes: */
1118 if (perf_evlist__add_default_attrs(evsel_list
, very_detailed_attrs
) < 0)
1121 if (detailed_run
< 3)
1124 /* Append very, very detailed run extra attributes: */
1125 return perf_evlist__add_default_attrs(evsel_list
, very_very_detailed_attrs
);
1128 int cmd_stat(int argc
, const char **argv
, const char *prefix __maybe_unused
)
1130 bool append_file
= false;
1132 const char *output_name
= NULL
;
1133 const struct option options
[] = {
1134 OPT_BOOLEAN('T', "transaction", &transaction_run
,
1135 "hardware transaction statistics"),
1136 OPT_CALLBACK('e', "event", &evsel_list
, "event",
1137 "event selector. use 'perf list' to list available events",
1138 parse_events_option
),
1139 OPT_CALLBACK(0, "filter", &evsel_list
, "filter",
1140 "event filter", parse_filter
),
1141 OPT_BOOLEAN('i', "no-inherit", &no_inherit
,
1142 "child tasks do not inherit counters"),
1143 OPT_STRING('p', "pid", &target
.pid
, "pid",
1144 "stat events on existing process id"),
1145 OPT_STRING('t', "tid", &target
.tid
, "tid",
1146 "stat events on existing thread id"),
1147 OPT_BOOLEAN('a', "all-cpus", &target
.system_wide
,
1148 "system-wide collection from all CPUs"),
1149 OPT_BOOLEAN('g', "group", &group
,
1150 "put the counters into a counter group"),
1151 OPT_BOOLEAN('c', "scale", &stat_config
.scale
, "scale/normalize counters"),
1152 OPT_INCR('v', "verbose", &verbose
,
1153 "be more verbose (show counter open errors, etc)"),
1154 OPT_INTEGER('r', "repeat", &run_count
,
1155 "repeat command and print average + stddev (max: 100, forever: 0)"),
1156 OPT_BOOLEAN('n', "null", &null_run
,
1157 "null run - dont start any counters"),
1158 OPT_INCR('d', "detailed", &detailed_run
,
1159 "detailed run - start a lot of events"),
1160 OPT_BOOLEAN('S', "sync", &sync_run
,
1161 "call sync() before starting a run"),
1162 OPT_CALLBACK_NOOPT('B', "big-num", NULL
, NULL
,
1163 "print large numbers with thousands\' separators",
1165 OPT_STRING('C', "cpu", &target
.cpu_list
, "cpu",
1166 "list of cpus to monitor in system-wide"),
1167 OPT_SET_UINT('A', "no-aggr", &stat_config
.aggr_mode
,
1168 "disable CPU count aggregation", AGGR_NONE
),
1169 OPT_STRING('x', "field-separator", &csv_sep
, "separator",
1170 "print counts with custom separator"),
1171 OPT_CALLBACK('G', "cgroup", &evsel_list
, "name",
1172 "monitor event in cgroup name only", parse_cgroups
),
1173 OPT_STRING('o', "output", &output_name
, "file", "output file name"),
1174 OPT_BOOLEAN(0, "append", &append_file
, "append to the output file"),
1175 OPT_INTEGER(0, "log-fd", &output_fd
,
1176 "log output to fd, instead of stderr"),
1177 OPT_STRING(0, "pre", &pre_cmd
, "command",
1178 "command to run prior to the measured command"),
1179 OPT_STRING(0, "post", &post_cmd
, "command",
1180 "command to run after to the measured command"),
1181 OPT_UINTEGER('I', "interval-print", &stat_config
.interval
,
1182 "print counts at regular interval in ms (>= 100)"),
1183 OPT_SET_UINT(0, "per-socket", &stat_config
.aggr_mode
,
1184 "aggregate counts per processor socket", AGGR_SOCKET
),
1185 OPT_SET_UINT(0, "per-core", &stat_config
.aggr_mode
,
1186 "aggregate counts per physical processor core", AGGR_CORE
),
1187 OPT_SET_UINT(0, "per-thread", &stat_config
.aggr_mode
,
1188 "aggregate counts per thread", AGGR_THREAD
),
1189 OPT_UINTEGER('D', "delay", &initial_delay
,
1190 "ms to wait before starting measurement after program start"),
1193 const char * const stat_usage
[] = {
1194 "perf stat [<options>] [<command>]",
1197 int status
= -EINVAL
, run_idx
;
1199 FILE *output
= stderr
;
1200 unsigned int interval
;
1202 setlocale(LC_ALL
, "");
1204 evsel_list
= perf_evlist__new();
1205 if (evsel_list
== NULL
)
1208 argc
= parse_options(argc
, argv
, options
, stat_usage
,
1209 PARSE_OPT_STOP_AT_NON_OPTION
);
1211 interval
= stat_config
.interval
;
1213 if (output_name
&& strcmp(output_name
, "-"))
1216 if (output_name
&& output_fd
) {
1217 fprintf(stderr
, "cannot use both --output and --log-fd\n");
1218 parse_options_usage(stat_usage
, options
, "o", 1);
1219 parse_options_usage(NULL
, options
, "log-fd", 0);
1223 if (output_fd
< 0) {
1224 fprintf(stderr
, "argument to --log-fd must be a > 0\n");
1225 parse_options_usage(stat_usage
, options
, "log-fd", 0);
1231 mode
= append_file
? "a" : "w";
1233 output
= fopen(output_name
, mode
);
1235 perror("failed to create output file");
1238 clock_gettime(CLOCK_REALTIME
, &tm
);
1239 fprintf(output
, "# started on %s\n", ctime(&tm
.tv_sec
));
1240 } else if (output_fd
> 0) {
1241 mode
= append_file
? "a" : "w";
1242 output
= fdopen(output_fd
, mode
);
1244 perror("Failed opening logfd");
1249 stat_config
.output
= output
;
1253 if (!strcmp(csv_sep
, "\\t"))
1256 csv_sep
= DEFAULT_SEPARATOR
;
1259 * let the spreadsheet do the pretty-printing
1262 /* User explicitly passed -B? */
1263 if (big_num_opt
== 1) {
1264 fprintf(stderr
, "-B option not supported with -x\n");
1265 parse_options_usage(stat_usage
, options
, "B", 1);
1266 parse_options_usage(NULL
, options
, "x", 1);
1268 } else /* Nope, so disable big number formatting */
1270 } else if (big_num_opt
== 0) /* User passed --no-big-num */
1273 if (!argc
&& target__none(&target
))
1274 usage_with_options(stat_usage
, options
);
1276 if (run_count
< 0) {
1277 pr_err("Run count must be a positive number\n");
1278 parse_options_usage(stat_usage
, options
, "r", 1);
1280 } else if (run_count
== 0) {
1285 if ((stat_config
.aggr_mode
== AGGR_THREAD
) && !target__has_task(&target
)) {
1286 fprintf(stderr
, "The --per-thread option is only available "
1287 "when monitoring via -p -t options.\n");
1288 parse_options_usage(NULL
, options
, "p", 1);
1289 parse_options_usage(NULL
, options
, "t", 1);
1294 * no_aggr, cgroup are for system-wide only
1295 * --per-thread is aggregated per thread, we dont mix it with cpu mode
1297 if (((stat_config
.aggr_mode
!= AGGR_GLOBAL
&&
1298 stat_config
.aggr_mode
!= AGGR_THREAD
) || nr_cgroups
) &&
1299 !target__has_cpu(&target
)) {
1300 fprintf(stderr
, "both cgroup and no-aggregation "
1301 "modes only available in system-wide mode\n");
1303 parse_options_usage(stat_usage
, options
, "G", 1);
1304 parse_options_usage(NULL
, options
, "A", 1);
1305 parse_options_usage(NULL
, options
, "a", 1);
1309 if (add_default_attributes())
1312 target__validate(&target
);
1314 if (perf_evlist__create_maps(evsel_list
, &target
) < 0) {
1315 if (target__has_task(&target
)) {
1316 pr_err("Problems finding threads of monitor\n");
1317 parse_options_usage(stat_usage
, options
, "p", 1);
1318 parse_options_usage(NULL
, options
, "t", 1);
1319 } else if (target__has_cpu(&target
)) {
1320 perror("failed to parse CPUs map");
1321 parse_options_usage(stat_usage
, options
, "C", 1);
1322 parse_options_usage(NULL
, options
, "a", 1);
1328 * Initialize thread_map with comm names,
1329 * so we could print it out on output.
1331 if (stat_config
.aggr_mode
== AGGR_THREAD
)
1332 thread_map__read_comms(evsel_list
->threads
);
1334 if (interval
&& interval
< 100) {
1335 pr_err("print interval must be >= 100ms\n");
1336 parse_options_usage(stat_usage
, options
, "I", 1);
1340 if (perf_evlist__alloc_stats(evsel_list
, interval
))
1343 if (perf_stat_init_aggr_mode())
1347 * We dont want to block the signals - that would cause
1348 * child tasks to inherit that and Ctrl-C would not work.
1349 * What we want is for Ctrl-C to work in the exec()-ed
1350 * task, but being ignored by perf stat itself:
1354 signal(SIGINT
, skip_signal
);
1355 signal(SIGCHLD
, skip_signal
);
1356 signal(SIGALRM
, skip_signal
);
1357 signal(SIGABRT
, skip_signal
);
1360 for (run_idx
= 0; forever
|| run_idx
< run_count
; run_idx
++) {
1361 if (run_count
!= 1 && verbose
)
1362 fprintf(output
, "[ perf stat: executing run #%d ... ]\n",
1365 status
= run_perf_stat(argc
, argv
);
1366 if (forever
&& status
!= -1) {
1367 print_counters(NULL
, argc
, argv
);
1368 perf_stat__reset_stats();
1372 if (!forever
&& status
!= -1 && !interval
)
1373 print_counters(NULL
, argc
, argv
);
1375 perf_evlist__free_stats(evsel_list
);
1377 perf_evlist__delete(evsel_list
);