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"
63 #include <sys/prctl.h>
66 #define DEFAULT_SEPARATOR " "
67 #define CNTR_NOT_SUPPORTED "<not supported>"
68 #define CNTR_NOT_COUNTED "<not counted>"
70 static void print_stat(int argc
, const char **argv
);
71 static void print_counter_aggr(struct perf_evsel
*counter
, char *prefix
);
72 static void print_counter(struct perf_evsel
*counter
, char *prefix
);
73 static void print_aggr(char *prefix
);
75 /* Default events used for perf stat -T */
76 static const char *transaction_attrs
= {
88 /* More limited version when the CPU does not have all events. */
89 static const char * transaction_limited_attrs
= {
99 static struct perf_evlist
*evsel_list
;
101 static struct target target
= {
105 static int run_count
= 1;
106 static bool no_inherit
= false;
107 static bool scale
= true;
108 static enum aggr_mode aggr_mode
= AGGR_GLOBAL
;
109 static volatile pid_t child_pid
= -1;
110 static bool null_run
= false;
111 static int detailed_run
= 0;
112 static bool transaction_run
;
113 static bool big_num
= true;
114 static int big_num_opt
= -1;
115 static const char *csv_sep
= NULL
;
116 static bool csv_output
= false;
117 static bool group
= false;
118 static FILE *output
= NULL
;
119 static const char *pre_cmd
= NULL
;
120 static const char *post_cmd
= NULL
;
121 static bool sync_run
= false;
122 static unsigned int interval
= 0;
123 static unsigned int initial_delay
= 0;
124 static unsigned int unit_width
= 4; /* strlen("unit") */
125 static bool forever
= false;
126 static struct timespec ref_time
;
127 static struct cpu_map
*aggr_map
;
128 static int (*aggr_get_id
)(struct cpu_map
*m
, int cpu
);
130 static volatile int done
= 0;
132 static inline void diff_timespec(struct timespec
*r
, struct timespec
*a
,
135 r
->tv_sec
= a
->tv_sec
- b
->tv_sec
;
136 if (a
->tv_nsec
< b
->tv_nsec
) {
137 r
->tv_nsec
= a
->tv_nsec
+ 1000000000L - b
->tv_nsec
;
140 r
->tv_nsec
= a
->tv_nsec
- b
->tv_nsec
;
144 static void perf_evsel__reset_stat_priv(struct perf_evsel
*evsel
)
147 struct perf_stat
*ps
= evsel
->priv
;
149 for (i
= 0; i
< 3; i
++)
150 init_stats(&ps
->res_stats
[i
]);
152 perf_stat_evsel_id_init(evsel
);
155 static int perf_evsel__alloc_stat_priv(struct perf_evsel
*evsel
)
157 evsel
->priv
= zalloc(sizeof(struct perf_stat
));
158 if (evsel
->priv
== NULL
)
160 perf_evsel__reset_stat_priv(evsel
);
164 static void perf_evsel__free_stat_priv(struct perf_evsel
*evsel
)
169 static int perf_evsel__alloc_prev_raw_counts(struct perf_evsel
*evsel
)
171 struct perf_counts
*counts
;
173 counts
= perf_counts__new(perf_evsel__nr_cpus(evsel
));
175 evsel
->prev_raw_counts
= counts
;
177 return counts
? 0 : -ENOMEM
;
180 static void perf_evsel__free_prev_raw_counts(struct perf_evsel
*evsel
)
182 perf_counts__delete(evsel
->prev_raw_counts
);
183 evsel
->prev_raw_counts
= NULL
;
186 static void perf_evlist__free_stats(struct perf_evlist
*evlist
)
188 struct perf_evsel
*evsel
;
190 evlist__for_each(evlist
, evsel
) {
191 perf_evsel__free_stat_priv(evsel
);
192 perf_evsel__free_counts(evsel
);
193 perf_evsel__free_prev_raw_counts(evsel
);
197 static int perf_evlist__alloc_stats(struct perf_evlist
*evlist
, bool alloc_raw
)
199 struct perf_evsel
*evsel
;
201 evlist__for_each(evlist
, evsel
) {
202 if (perf_evsel__alloc_stat_priv(evsel
) < 0 ||
203 perf_evsel__alloc_counts(evsel
, perf_evsel__nr_cpus(evsel
)) < 0 ||
204 (alloc_raw
&& perf_evsel__alloc_prev_raw_counts(evsel
) < 0))
211 perf_evlist__free_stats(evlist
);
215 static void perf_stat__reset_stats(struct perf_evlist
*evlist
)
217 struct perf_evsel
*evsel
;
219 evlist__for_each(evlist
, evsel
) {
220 perf_evsel__reset_stat_priv(evsel
);
221 perf_evsel__reset_counts(evsel
, perf_evsel__nr_cpus(evsel
));
224 perf_stat__reset_shadow_stats();
227 static int create_perf_stat_counter(struct perf_evsel
*evsel
)
229 struct perf_event_attr
*attr
= &evsel
->attr
;
232 attr
->read_format
= PERF_FORMAT_TOTAL_TIME_ENABLED
|
233 PERF_FORMAT_TOTAL_TIME_RUNNING
;
235 attr
->inherit
= !no_inherit
;
237 if (target__has_cpu(&target
))
238 return perf_evsel__open_per_cpu(evsel
, perf_evsel__cpus(evsel
));
240 if (!target__has_task(&target
) && perf_evsel__is_group_leader(evsel
)) {
243 attr
->enable_on_exec
= 1;
246 return perf_evsel__open_per_thread(evsel
, evsel_list
->threads
);
250 * Does the counter have nsecs as a unit?
252 static inline int nsec_counter(struct perf_evsel
*evsel
)
254 if (perf_evsel__match(evsel
, SOFTWARE
, SW_CPU_CLOCK
) ||
255 perf_evsel__match(evsel
, SOFTWARE
, SW_TASK_CLOCK
))
261 static void zero_per_pkg(struct perf_evsel
*counter
)
263 if (counter
->per_pkg_mask
)
264 memset(counter
->per_pkg_mask
, 0, MAX_NR_CPUS
);
267 static int check_per_pkg(struct perf_evsel
*counter
, int cpu
, bool *skip
)
269 unsigned long *mask
= counter
->per_pkg_mask
;
270 struct cpu_map
*cpus
= perf_evsel__cpus(counter
);
275 if (!counter
->per_pkg
)
278 if (cpu_map__empty(cpus
))
282 mask
= zalloc(MAX_NR_CPUS
);
286 counter
->per_pkg_mask
= mask
;
289 s
= cpu_map__get_socket(cpus
, cpu
);
293 *skip
= test_and_set_bit(s
, mask
) == 1;
297 static int read_cb(struct perf_evsel
*evsel
, int cpu
, int thread __maybe_unused
,
298 struct perf_counts_values
*count
)
300 struct perf_counts_values
*aggr
= &evsel
->counts
->aggr
;
301 static struct perf_counts_values zero
;
304 if (check_per_pkg(evsel
, cpu
, &skip
)) {
305 pr_err("failed to read per-pkg counter\n");
316 if (!evsel
->snapshot
)
317 perf_evsel__compute_deltas(evsel
, cpu
, count
);
318 perf_counts_values__scale(count
, scale
, NULL
);
319 evsel
->counts
->cpu
[cpu
] = *count
;
320 if (aggr_mode
== AGGR_NONE
)
321 perf_stat__update_shadow_stats(evsel
, count
->values
, cpu
);
324 aggr
->val
+= count
->val
;
326 aggr
->ena
+= count
->ena
;
327 aggr
->run
+= count
->run
;
336 static int read_counter(struct perf_evsel
*counter
);
339 * Read out the results of a single counter:
340 * aggregate counts across CPUs in system-wide mode
342 static int read_counter_aggr(struct perf_evsel
*counter
)
344 struct perf_counts_values
*aggr
= &counter
->counts
->aggr
;
345 struct perf_stat
*ps
= counter
->priv
;
346 u64
*count
= counter
->counts
->aggr
.values
;
349 aggr
->val
= aggr
->ena
= aggr
->run
= 0;
351 if (read_counter(counter
))
354 if (!counter
->snapshot
)
355 perf_evsel__compute_deltas(counter
, -1, aggr
);
356 perf_counts_values__scale(aggr
, scale
, &counter
->counts
->scaled
);
358 for (i
= 0; i
< 3; i
++)
359 update_stats(&ps
->res_stats
[i
], count
[i
]);
362 fprintf(output
, "%s: %" PRIu64
" %" PRIu64
" %" PRIu64
"\n",
363 perf_evsel__name(counter
), count
[0], count
[1], count
[2]);
367 * Save the full runtime - to allow normalization during printout:
369 perf_stat__update_shadow_stats(counter
, count
, 0);
375 * Read out the results of a single counter:
376 * do not aggregate counts across CPUs in system-wide mode
378 static int read_counter(struct perf_evsel
*counter
)
380 int nthreads
= thread_map__nr(evsel_list
->threads
);
381 int ncpus
= perf_evsel__nr_cpus(counter
);
384 if (!counter
->supported
)
387 if (counter
->system_wide
)
390 if (counter
->per_pkg
)
391 zero_per_pkg(counter
);
393 for (thread
= 0; thread
< nthreads
; thread
++) {
394 for (cpu
= 0; cpu
< ncpus
; cpu
++) {
395 if (perf_evsel__read_cb(counter
, cpu
, thread
, read_cb
))
403 static void print_interval(void)
405 static int num_print_interval
;
406 struct perf_evsel
*counter
;
407 struct perf_stat
*ps
;
408 struct timespec ts
, rs
;
411 if (aggr_mode
== AGGR_GLOBAL
) {
412 evlist__for_each(evsel_list
, counter
) {
414 memset(ps
->res_stats
, 0, sizeof(ps
->res_stats
));
415 read_counter_aggr(counter
);
418 evlist__for_each(evsel_list
, counter
) {
420 memset(ps
->res_stats
, 0, sizeof(ps
->res_stats
));
421 read_counter(counter
);
425 clock_gettime(CLOCK_MONOTONIC
, &ts
);
426 diff_timespec(&rs
, &ts
, &ref_time
);
427 sprintf(prefix
, "%6lu.%09lu%s", rs
.tv_sec
, rs
.tv_nsec
, csv_sep
);
429 if (num_print_interval
== 0 && !csv_output
) {
432 fprintf(output
, "# time socket cpus counts %*s events\n", unit_width
, "unit");
435 fprintf(output
, "# time core cpus counts %*s events\n", unit_width
, "unit");
438 fprintf(output
, "# time CPU counts %*s events\n", unit_width
, "unit");
442 fprintf(output
, "# time counts %*s events\n", unit_width
, "unit");
446 if (++num_print_interval
== 25)
447 num_print_interval
= 0;
455 evlist__for_each(evsel_list
, counter
)
456 print_counter(counter
, prefix
);
460 evlist__for_each(evsel_list
, counter
)
461 print_counter_aggr(counter
, prefix
);
467 static void handle_initial_delay(void)
469 struct perf_evsel
*counter
;
472 const int ncpus
= cpu_map__nr(evsel_list
->cpus
),
473 nthreads
= thread_map__nr(evsel_list
->threads
);
475 usleep(initial_delay
* 1000);
476 evlist__for_each(evsel_list
, counter
)
477 perf_evsel__enable(counter
, ncpus
, nthreads
);
481 static volatile int workload_exec_errno
;
484 * perf_evlist__prepare_workload will send a SIGUSR1
485 * if the fork fails, since we asked by setting its
486 * want_signal to true.
488 static void workload_exec_failed_signal(int signo __maybe_unused
, siginfo_t
*info
,
489 void *ucontext __maybe_unused
)
491 workload_exec_errno
= info
->si_value
.sival_int
;
494 static int __run_perf_stat(int argc
, const char **argv
)
497 unsigned long long t0
, t1
;
498 struct perf_evsel
*counter
;
502 const bool forks
= (argc
> 0);
505 ts
.tv_sec
= interval
/ 1000;
506 ts
.tv_nsec
= (interval
% 1000) * 1000000;
513 if (perf_evlist__prepare_workload(evsel_list
, &target
, argv
, false,
514 workload_exec_failed_signal
) < 0) {
515 perror("failed to prepare workload");
518 child_pid
= evsel_list
->workload
.pid
;
522 perf_evlist__set_leader(evsel_list
);
524 evlist__for_each(evsel_list
, counter
) {
525 if (create_perf_stat_counter(counter
) < 0) {
527 * PPC returns ENXIO for HW counters until 2.6.37
528 * (behavior changed with commit b0a873e).
530 if (errno
== EINVAL
|| errno
== ENOSYS
||
531 errno
== ENOENT
|| errno
== EOPNOTSUPP
||
534 ui__warning("%s event is not supported by the kernel.\n",
535 perf_evsel__name(counter
));
536 counter
->supported
= false;
538 if ((counter
->leader
!= counter
) ||
539 !(counter
->leader
->nr_members
> 1))
543 perf_evsel__open_strerror(counter
, &target
,
544 errno
, msg
, sizeof(msg
));
545 ui__error("%s\n", msg
);
548 kill(child_pid
, SIGTERM
);
552 counter
->supported
= true;
554 l
= strlen(counter
->unit
);
559 if (perf_evlist__apply_filters(evsel_list
, &counter
)) {
560 error("failed to set filter \"%s\" on event %s with %d (%s)\n",
561 counter
->filter
, perf_evsel__name(counter
), errno
,
562 strerror_r(errno
, msg
, sizeof(msg
)));
567 * Enable counters and exec the command:
570 clock_gettime(CLOCK_MONOTONIC
, &ref_time
);
573 perf_evlist__start_workload(evsel_list
);
574 handle_initial_delay();
577 while (!waitpid(child_pid
, &status
, WNOHANG
)) {
578 nanosleep(&ts
, NULL
);
584 if (workload_exec_errno
) {
585 const char *emsg
= strerror_r(workload_exec_errno
, msg
, sizeof(msg
));
586 pr_err("Workload failed: %s\n", emsg
);
590 if (WIFSIGNALED(status
))
591 psignal(WTERMSIG(status
), argv
[0]);
593 handle_initial_delay();
595 nanosleep(&ts
, NULL
);
603 update_stats(&walltime_nsecs_stats
, t1
- t0
);
605 if (aggr_mode
== AGGR_GLOBAL
) {
606 evlist__for_each(evsel_list
, counter
) {
607 read_counter_aggr(counter
);
608 perf_evsel__close_fd(counter
, perf_evsel__nr_cpus(counter
),
609 thread_map__nr(evsel_list
->threads
));
612 evlist__for_each(evsel_list
, counter
) {
613 read_counter(counter
);
614 perf_evsel__close_fd(counter
, perf_evsel__nr_cpus(counter
), 1);
618 return WEXITSTATUS(status
);
621 static int run_perf_stat(int argc
, const char **argv
)
626 ret
= system(pre_cmd
);
634 ret
= __run_perf_stat(argc
, argv
);
639 ret
= system(post_cmd
);
647 static void print_running(u64 run
, u64 ena
)
650 fprintf(output
, "%s%" PRIu64
"%s%.2f",
654 ena
? 100.0 * run
/ ena
: 100.0);
655 } else if (run
!= ena
) {
656 fprintf(output
, " (%.2f%%)", 100.0 * run
/ ena
);
660 static void print_noise_pct(double total
, double avg
)
662 double pct
= rel_stddev_stats(total
, avg
);
665 fprintf(output
, "%s%.2f%%", csv_sep
, pct
);
667 fprintf(output
, " ( +-%6.2f%% )", pct
);
670 static void print_noise(struct perf_evsel
*evsel
, double avg
)
672 struct perf_stat
*ps
;
678 print_noise_pct(stddev_stats(&ps
->res_stats
[0]), avg
);
681 static void aggr_printout(struct perf_evsel
*evsel
, int id
, int nr
)
685 fprintf(output
, "S%d-C%*d%s%*d%s",
686 cpu_map__id_to_socket(id
),
688 cpu_map__id_to_cpu(id
),
695 fprintf(output
, "S%*d%s%*d%s",
704 fprintf(output
, "CPU%*d%s",
706 perf_evsel__cpus(evsel
)->map
[id
], csv_sep
);
714 static void nsec_printout(int id
, int nr
, struct perf_evsel
*evsel
, double avg
)
716 double msecs
= avg
/ 1e6
;
717 const char *fmt_v
, *fmt_n
;
720 fmt_v
= csv_output
? "%.6f%s" : "%18.6f%s";
721 fmt_n
= csv_output
? "%s" : "%-25s";
723 aggr_printout(evsel
, id
, nr
);
725 scnprintf(name
, sizeof(name
), "%s%s",
726 perf_evsel__name(evsel
), csv_output
? "" : " (msec)");
728 fprintf(output
, fmt_v
, msecs
, csv_sep
);
731 fprintf(output
, "%s%s", evsel
->unit
, csv_sep
);
733 fprintf(output
, "%-*s%s", unit_width
, evsel
->unit
, csv_sep
);
735 fprintf(output
, fmt_n
, name
);
738 fprintf(output
, "%s%s", csv_sep
, evsel
->cgrp
->name
);
740 if (csv_output
|| interval
)
743 if (perf_evsel__match(evsel
, SOFTWARE
, SW_TASK_CLOCK
))
744 fprintf(output
, " # %8.3f CPUs utilized ",
745 avg
/ avg_stats(&walltime_nsecs_stats
));
747 fprintf(output
, " ");
750 static void abs_printout(int id
, int nr
, struct perf_evsel
*evsel
, double avg
)
752 double sc
= evsel
->scale
;
754 int cpu
= cpu_map__id_to_cpu(id
);
757 fmt
= sc
!= 1.0 ? "%.2f%s" : "%.0f%s";
760 fmt
= sc
!= 1.0 ? "%'18.2f%s" : "%'18.0f%s";
762 fmt
= sc
!= 1.0 ? "%18.2f%s" : "%18.0f%s";
765 aggr_printout(evsel
, id
, nr
);
767 if (aggr_mode
== AGGR_GLOBAL
)
770 fprintf(output
, fmt
, avg
, csv_sep
);
773 fprintf(output
, "%-*s%s",
774 csv_output
? 0 : unit_width
,
775 evsel
->unit
, csv_sep
);
777 fprintf(output
, "%-*s", csv_output
? 0 : 25, perf_evsel__name(evsel
));
780 fprintf(output
, "%s%s", csv_sep
, evsel
->cgrp
->name
);
782 if (csv_output
|| interval
)
785 perf_stat__print_shadow_stats(output
, evsel
, avg
, cpu
, aggr_mode
);
788 static void print_aggr(char *prefix
)
790 struct perf_evsel
*counter
;
791 int cpu
, cpu2
, s
, s2
, id
, nr
;
795 if (!(aggr_map
|| aggr_get_id
))
798 for (s
= 0; s
< aggr_map
->nr
; s
++) {
799 id
= aggr_map
->map
[s
];
800 evlist__for_each(evsel_list
, counter
) {
803 for (cpu
= 0; cpu
< perf_evsel__nr_cpus(counter
); cpu
++) {
804 cpu2
= perf_evsel__cpus(counter
)->map
[cpu
];
805 s2
= aggr_get_id(evsel_list
->cpus
, cpu2
);
808 val
+= counter
->counts
->cpu
[cpu
].val
;
809 ena
+= counter
->counts
->cpu
[cpu
].ena
;
810 run
+= counter
->counts
->cpu
[cpu
].run
;
814 fprintf(output
, "%s", prefix
);
816 if (run
== 0 || ena
== 0) {
817 aggr_printout(counter
, id
, nr
);
819 fprintf(output
, "%*s%s",
821 counter
->supported
? CNTR_NOT_COUNTED
: CNTR_NOT_SUPPORTED
,
824 fprintf(output
, "%-*s%s",
825 csv_output
? 0 : unit_width
,
826 counter
->unit
, csv_sep
);
828 fprintf(output
, "%*s",
829 csv_output
? 0 : -25,
830 perf_evsel__name(counter
));
833 fprintf(output
, "%s%s",
834 csv_sep
, counter
->cgrp
->name
);
836 print_running(run
, ena
);
840 uval
= val
* counter
->scale
;
842 if (nsec_counter(counter
))
843 nsec_printout(id
, nr
, counter
, uval
);
845 abs_printout(id
, nr
, counter
, uval
);
848 print_noise(counter
, 1.0);
850 print_running(run
, ena
);
857 * Print out the results of a single counter:
858 * aggregated counts in system-wide mode
860 static void print_counter_aggr(struct perf_evsel
*counter
, char *prefix
)
862 struct perf_stat
*ps
= counter
->priv
;
863 double avg
= avg_stats(&ps
->res_stats
[0]);
864 int scaled
= counter
->counts
->scaled
;
866 double avg_enabled
, avg_running
;
868 avg_enabled
= avg_stats(&ps
->res_stats
[1]);
869 avg_running
= avg_stats(&ps
->res_stats
[2]);
872 fprintf(output
, "%s", prefix
);
874 if (scaled
== -1 || !counter
->supported
) {
875 fprintf(output
, "%*s%s",
877 counter
->supported
? CNTR_NOT_COUNTED
: CNTR_NOT_SUPPORTED
,
879 fprintf(output
, "%-*s%s",
880 csv_output
? 0 : unit_width
,
881 counter
->unit
, csv_sep
);
882 fprintf(output
, "%*s",
883 csv_output
? 0 : -25,
884 perf_evsel__name(counter
));
887 fprintf(output
, "%s%s", csv_sep
, counter
->cgrp
->name
);
889 print_running(avg_running
, avg_enabled
);
894 uval
= avg
* counter
->scale
;
896 if (nsec_counter(counter
))
897 nsec_printout(-1, 0, counter
, uval
);
899 abs_printout(-1, 0, counter
, uval
);
901 print_noise(counter
, avg
);
903 print_running(avg_running
, avg_enabled
);
904 fprintf(output
, "\n");
908 * Print out the results of a single counter:
909 * does not use aggregated count in system-wide
911 static void print_counter(struct perf_evsel
*counter
, char *prefix
)
917 for (cpu
= 0; cpu
< perf_evsel__nr_cpus(counter
); cpu
++) {
918 val
= counter
->counts
->cpu
[cpu
].val
;
919 ena
= counter
->counts
->cpu
[cpu
].ena
;
920 run
= counter
->counts
->cpu
[cpu
].run
;
923 fprintf(output
, "%s", prefix
);
925 if (run
== 0 || ena
== 0) {
926 fprintf(output
, "CPU%*d%s%*s%s",
928 perf_evsel__cpus(counter
)->map
[cpu
], csv_sep
,
930 counter
->supported
? CNTR_NOT_COUNTED
: CNTR_NOT_SUPPORTED
,
933 fprintf(output
, "%-*s%s",
934 csv_output
? 0 : unit_width
,
935 counter
->unit
, csv_sep
);
937 fprintf(output
, "%*s",
938 csv_output
? 0 : -25,
939 perf_evsel__name(counter
));
942 fprintf(output
, "%s%s",
943 csv_sep
, counter
->cgrp
->name
);
945 print_running(run
, ena
);
950 uval
= val
* counter
->scale
;
952 if (nsec_counter(counter
))
953 nsec_printout(cpu
, 0, counter
, uval
);
955 abs_printout(cpu
, 0, counter
, uval
);
958 print_noise(counter
, 1.0);
959 print_running(run
, ena
);
965 static void print_stat(int argc
, const char **argv
)
967 struct perf_evsel
*counter
;
973 fprintf(output
, "\n");
974 fprintf(output
, " Performance counter stats for ");
975 if (target
.system_wide
)
976 fprintf(output
, "\'system wide");
977 else if (target
.cpu_list
)
978 fprintf(output
, "\'CPU(s) %s", target
.cpu_list
);
979 else if (!target__has_task(&target
)) {
980 fprintf(output
, "\'%s", argv
[0]);
981 for (i
= 1; i
< argc
; i
++)
982 fprintf(output
, " %s", argv
[i
]);
983 } else if (target
.pid
)
984 fprintf(output
, "process id \'%s", target
.pid
);
986 fprintf(output
, "thread id \'%s", target
.tid
);
988 fprintf(output
, "\'");
990 fprintf(output
, " (%d runs)", run_count
);
991 fprintf(output
, ":\n\n");
1000 evlist__for_each(evsel_list
, counter
)
1001 print_counter_aggr(counter
, NULL
);
1004 evlist__for_each(evsel_list
, counter
)
1005 print_counter(counter
, NULL
);
1013 fprintf(output
, "\n");
1014 fprintf(output
, " %17.9f seconds time elapsed",
1015 avg_stats(&walltime_nsecs_stats
)/1e9
);
1016 if (run_count
> 1) {
1017 fprintf(output
, " ");
1018 print_noise_pct(stddev_stats(&walltime_nsecs_stats
),
1019 avg_stats(&walltime_nsecs_stats
));
1021 fprintf(output
, "\n\n");
1025 static volatile int signr
= -1;
1027 static void skip_signal(int signo
)
1029 if ((child_pid
== -1) || interval
)
1034 * render child_pid harmless
1035 * won't send SIGTERM to a random
1036 * process in case of race condition
1037 * and fast PID recycling
1042 static void sig_atexit(void)
1047 * avoid race condition with SIGCHLD handler
1048 * in skip_signal() which is modifying child_pid
1049 * goal is to avoid send SIGTERM to a random
1053 sigaddset(&set
, SIGCHLD
);
1054 sigprocmask(SIG_BLOCK
, &set
, &oset
);
1056 if (child_pid
!= -1)
1057 kill(child_pid
, SIGTERM
);
1059 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
1064 signal(signr
, SIG_DFL
);
1065 kill(getpid(), signr
);
1068 static int stat__set_big_num(const struct option
*opt __maybe_unused
,
1069 const char *s __maybe_unused
, int unset
)
1071 big_num_opt
= unset
? 0 : 1;
1075 static int perf_stat_init_aggr_mode(void)
1077 switch (aggr_mode
) {
1079 if (cpu_map__build_socket_map(evsel_list
->cpus
, &aggr_map
)) {
1080 perror("cannot build socket map");
1083 aggr_get_id
= cpu_map__get_socket
;
1086 if (cpu_map__build_core_map(evsel_list
->cpus
, &aggr_map
)) {
1087 perror("cannot build core map");
1090 aggr_get_id
= cpu_map__get_core
;
1101 * Add default attributes, if there were no attributes specified or
1102 * if -d/--detailed, -d -d or -d -d -d is used:
1104 static int add_default_attributes(void)
1106 struct perf_event_attr default_attrs
[] = {
1108 { .type
= PERF_TYPE_SOFTWARE
, .config
= PERF_COUNT_SW_TASK_CLOCK
},
1109 { .type
= PERF_TYPE_SOFTWARE
, .config
= PERF_COUNT_SW_CONTEXT_SWITCHES
},
1110 { .type
= PERF_TYPE_SOFTWARE
, .config
= PERF_COUNT_SW_CPU_MIGRATIONS
},
1111 { .type
= PERF_TYPE_SOFTWARE
, .config
= PERF_COUNT_SW_PAGE_FAULTS
},
1113 { .type
= PERF_TYPE_HARDWARE
, .config
= PERF_COUNT_HW_CPU_CYCLES
},
1114 { .type
= PERF_TYPE_HARDWARE
, .config
= PERF_COUNT_HW_STALLED_CYCLES_FRONTEND
},
1115 { .type
= PERF_TYPE_HARDWARE
, .config
= PERF_COUNT_HW_STALLED_CYCLES_BACKEND
},
1116 { .type
= PERF_TYPE_HARDWARE
, .config
= PERF_COUNT_HW_INSTRUCTIONS
},
1117 { .type
= PERF_TYPE_HARDWARE
, .config
= PERF_COUNT_HW_BRANCH_INSTRUCTIONS
},
1118 { .type
= PERF_TYPE_HARDWARE
, .config
= PERF_COUNT_HW_BRANCH_MISSES
},
1123 * Detailed stats (-d), covering the L1 and last level data caches:
1125 struct perf_event_attr detailed_attrs
[] = {
1127 { .type
= PERF_TYPE_HW_CACHE
,
1129 PERF_COUNT_HW_CACHE_L1D
<< 0 |
1130 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
1131 (PERF_COUNT_HW_CACHE_RESULT_ACCESS
<< 16) },
1133 { .type
= PERF_TYPE_HW_CACHE
,
1135 PERF_COUNT_HW_CACHE_L1D
<< 0 |
1136 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
1137 (PERF_COUNT_HW_CACHE_RESULT_MISS
<< 16) },
1139 { .type
= PERF_TYPE_HW_CACHE
,
1141 PERF_COUNT_HW_CACHE_LL
<< 0 |
1142 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
1143 (PERF_COUNT_HW_CACHE_RESULT_ACCESS
<< 16) },
1145 { .type
= PERF_TYPE_HW_CACHE
,
1147 PERF_COUNT_HW_CACHE_LL
<< 0 |
1148 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
1149 (PERF_COUNT_HW_CACHE_RESULT_MISS
<< 16) },
1153 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
1155 struct perf_event_attr very_detailed_attrs
[] = {
1157 { .type
= PERF_TYPE_HW_CACHE
,
1159 PERF_COUNT_HW_CACHE_L1I
<< 0 |
1160 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
1161 (PERF_COUNT_HW_CACHE_RESULT_ACCESS
<< 16) },
1163 { .type
= PERF_TYPE_HW_CACHE
,
1165 PERF_COUNT_HW_CACHE_L1I
<< 0 |
1166 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
1167 (PERF_COUNT_HW_CACHE_RESULT_MISS
<< 16) },
1169 { .type
= PERF_TYPE_HW_CACHE
,
1171 PERF_COUNT_HW_CACHE_DTLB
<< 0 |
1172 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
1173 (PERF_COUNT_HW_CACHE_RESULT_ACCESS
<< 16) },
1175 { .type
= PERF_TYPE_HW_CACHE
,
1177 PERF_COUNT_HW_CACHE_DTLB
<< 0 |
1178 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
1179 (PERF_COUNT_HW_CACHE_RESULT_MISS
<< 16) },
1181 { .type
= PERF_TYPE_HW_CACHE
,
1183 PERF_COUNT_HW_CACHE_ITLB
<< 0 |
1184 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
1185 (PERF_COUNT_HW_CACHE_RESULT_ACCESS
<< 16) },
1187 { .type
= PERF_TYPE_HW_CACHE
,
1189 PERF_COUNT_HW_CACHE_ITLB
<< 0 |
1190 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
1191 (PERF_COUNT_HW_CACHE_RESULT_MISS
<< 16) },
1196 * Very, very detailed stats (-d -d -d), adding prefetch events:
1198 struct perf_event_attr very_very_detailed_attrs
[] = {
1200 { .type
= PERF_TYPE_HW_CACHE
,
1202 PERF_COUNT_HW_CACHE_L1D
<< 0 |
1203 (PERF_COUNT_HW_CACHE_OP_PREFETCH
<< 8) |
1204 (PERF_COUNT_HW_CACHE_RESULT_ACCESS
<< 16) },
1206 { .type
= PERF_TYPE_HW_CACHE
,
1208 PERF_COUNT_HW_CACHE_L1D
<< 0 |
1209 (PERF_COUNT_HW_CACHE_OP_PREFETCH
<< 8) |
1210 (PERF_COUNT_HW_CACHE_RESULT_MISS
<< 16) },
1213 /* Set attrs if no event is selected and !null_run: */
1217 if (transaction_run
) {
1219 if (pmu_have_event("cpu", "cycles-ct") &&
1220 pmu_have_event("cpu", "el-start"))
1221 err
= parse_events(evsel_list
, transaction_attrs
, NULL
);
1223 err
= parse_events(evsel_list
, transaction_limited_attrs
, NULL
);
1225 fprintf(stderr
, "Cannot set up transaction events\n");
1231 if (!evsel_list
->nr_entries
) {
1232 if (perf_evlist__add_default_attrs(evsel_list
, default_attrs
) < 0)
1236 /* Detailed events get appended to the event list: */
1238 if (detailed_run
< 1)
1241 /* Append detailed run extra attributes: */
1242 if (perf_evlist__add_default_attrs(evsel_list
, detailed_attrs
) < 0)
1245 if (detailed_run
< 2)
1248 /* Append very detailed run extra attributes: */
1249 if (perf_evlist__add_default_attrs(evsel_list
, very_detailed_attrs
) < 0)
1252 if (detailed_run
< 3)
1255 /* Append very, very detailed run extra attributes: */
1256 return perf_evlist__add_default_attrs(evsel_list
, very_very_detailed_attrs
);
1259 int cmd_stat(int argc
, const char **argv
, const char *prefix __maybe_unused
)
1261 bool append_file
= false;
1263 const char *output_name
= NULL
;
1264 const struct option options
[] = {
1265 OPT_BOOLEAN('T', "transaction", &transaction_run
,
1266 "hardware transaction statistics"),
1267 OPT_CALLBACK('e', "event", &evsel_list
, "event",
1268 "event selector. use 'perf list' to list available events",
1269 parse_events_option
),
1270 OPT_CALLBACK(0, "filter", &evsel_list
, "filter",
1271 "event filter", parse_filter
),
1272 OPT_BOOLEAN('i', "no-inherit", &no_inherit
,
1273 "child tasks do not inherit counters"),
1274 OPT_STRING('p', "pid", &target
.pid
, "pid",
1275 "stat events on existing process id"),
1276 OPT_STRING('t', "tid", &target
.tid
, "tid",
1277 "stat events on existing thread id"),
1278 OPT_BOOLEAN('a', "all-cpus", &target
.system_wide
,
1279 "system-wide collection from all CPUs"),
1280 OPT_BOOLEAN('g', "group", &group
,
1281 "put the counters into a counter group"),
1282 OPT_BOOLEAN('c', "scale", &scale
, "scale/normalize counters"),
1283 OPT_INCR('v', "verbose", &verbose
,
1284 "be more verbose (show counter open errors, etc)"),
1285 OPT_INTEGER('r', "repeat", &run_count
,
1286 "repeat command and print average + stddev (max: 100, forever: 0)"),
1287 OPT_BOOLEAN('n', "null", &null_run
,
1288 "null run - dont start any counters"),
1289 OPT_INCR('d', "detailed", &detailed_run
,
1290 "detailed run - start a lot of events"),
1291 OPT_BOOLEAN('S', "sync", &sync_run
,
1292 "call sync() before starting a run"),
1293 OPT_CALLBACK_NOOPT('B', "big-num", NULL
, NULL
,
1294 "print large numbers with thousands\' separators",
1296 OPT_STRING('C', "cpu", &target
.cpu_list
, "cpu",
1297 "list of cpus to monitor in system-wide"),
1298 OPT_SET_UINT('A', "no-aggr", &aggr_mode
,
1299 "disable CPU count aggregation", AGGR_NONE
),
1300 OPT_STRING('x', "field-separator", &csv_sep
, "separator",
1301 "print counts with custom separator"),
1302 OPT_CALLBACK('G', "cgroup", &evsel_list
, "name",
1303 "monitor event in cgroup name only", parse_cgroups
),
1304 OPT_STRING('o', "output", &output_name
, "file", "output file name"),
1305 OPT_BOOLEAN(0, "append", &append_file
, "append to the output file"),
1306 OPT_INTEGER(0, "log-fd", &output_fd
,
1307 "log output to fd, instead of stderr"),
1308 OPT_STRING(0, "pre", &pre_cmd
, "command",
1309 "command to run prior to the measured command"),
1310 OPT_STRING(0, "post", &post_cmd
, "command",
1311 "command to run after to the measured command"),
1312 OPT_UINTEGER('I', "interval-print", &interval
,
1313 "print counts at regular interval in ms (>= 100)"),
1314 OPT_SET_UINT(0, "per-socket", &aggr_mode
,
1315 "aggregate counts per processor socket", AGGR_SOCKET
),
1316 OPT_SET_UINT(0, "per-core", &aggr_mode
,
1317 "aggregate counts per physical processor core", AGGR_CORE
),
1318 OPT_UINTEGER('D', "delay", &initial_delay
,
1319 "ms to wait before starting measurement after program start"),
1322 const char * const stat_usage
[] = {
1323 "perf stat [<options>] [<command>]",
1326 int status
= -EINVAL
, run_idx
;
1329 setlocale(LC_ALL
, "");
1331 evsel_list
= perf_evlist__new();
1332 if (evsel_list
== NULL
)
1335 argc
= parse_options(argc
, argv
, options
, stat_usage
,
1336 PARSE_OPT_STOP_AT_NON_OPTION
);
1339 if (output_name
&& strcmp(output_name
, "-"))
1342 if (output_name
&& output_fd
) {
1343 fprintf(stderr
, "cannot use both --output and --log-fd\n");
1344 parse_options_usage(stat_usage
, options
, "o", 1);
1345 parse_options_usage(NULL
, options
, "log-fd", 0);
1349 if (output_fd
< 0) {
1350 fprintf(stderr
, "argument to --log-fd must be a > 0\n");
1351 parse_options_usage(stat_usage
, options
, "log-fd", 0);
1357 mode
= append_file
? "a" : "w";
1359 output
= fopen(output_name
, mode
);
1361 perror("failed to create output file");
1364 clock_gettime(CLOCK_REALTIME
, &tm
);
1365 fprintf(output
, "# started on %s\n", ctime(&tm
.tv_sec
));
1366 } else if (output_fd
> 0) {
1367 mode
= append_file
? "a" : "w";
1368 output
= fdopen(output_fd
, mode
);
1370 perror("Failed opening logfd");
1377 if (!strcmp(csv_sep
, "\\t"))
1380 csv_sep
= DEFAULT_SEPARATOR
;
1383 * let the spreadsheet do the pretty-printing
1386 /* User explicitly passed -B? */
1387 if (big_num_opt
== 1) {
1388 fprintf(stderr
, "-B option not supported with -x\n");
1389 parse_options_usage(stat_usage
, options
, "B", 1);
1390 parse_options_usage(NULL
, options
, "x", 1);
1392 } else /* Nope, so disable big number formatting */
1394 } else if (big_num_opt
== 0) /* User passed --no-big-num */
1397 if (!argc
&& target__none(&target
))
1398 usage_with_options(stat_usage
, options
);
1400 if (run_count
< 0) {
1401 pr_err("Run count must be a positive number\n");
1402 parse_options_usage(stat_usage
, options
, "r", 1);
1404 } else if (run_count
== 0) {
1409 /* no_aggr, cgroup are for system-wide only */
1410 if ((aggr_mode
!= AGGR_GLOBAL
|| nr_cgroups
) &&
1411 !target__has_cpu(&target
)) {
1412 fprintf(stderr
, "both cgroup and no-aggregation "
1413 "modes only available in system-wide mode\n");
1415 parse_options_usage(stat_usage
, options
, "G", 1);
1416 parse_options_usage(NULL
, options
, "A", 1);
1417 parse_options_usage(NULL
, options
, "a", 1);
1421 if (add_default_attributes())
1424 target__validate(&target
);
1426 if (perf_evlist__create_maps(evsel_list
, &target
) < 0) {
1427 if (target__has_task(&target
)) {
1428 pr_err("Problems finding threads of monitor\n");
1429 parse_options_usage(stat_usage
, options
, "p", 1);
1430 parse_options_usage(NULL
, options
, "t", 1);
1431 } else if (target__has_cpu(&target
)) {
1432 perror("failed to parse CPUs map");
1433 parse_options_usage(stat_usage
, options
, "C", 1);
1434 parse_options_usage(NULL
, options
, "a", 1);
1438 if (interval
&& interval
< 100) {
1439 pr_err("print interval must be >= 100ms\n");
1440 parse_options_usage(stat_usage
, options
, "I", 1);
1444 if (perf_evlist__alloc_stats(evsel_list
, interval
))
1447 if (perf_stat_init_aggr_mode())
1451 * We dont want to block the signals - that would cause
1452 * child tasks to inherit that and Ctrl-C would not work.
1453 * What we want is for Ctrl-C to work in the exec()-ed
1454 * task, but being ignored by perf stat itself:
1458 signal(SIGINT
, skip_signal
);
1459 signal(SIGCHLD
, skip_signal
);
1460 signal(SIGALRM
, skip_signal
);
1461 signal(SIGABRT
, skip_signal
);
1464 for (run_idx
= 0; forever
|| run_idx
< run_count
; run_idx
++) {
1465 if (run_count
!= 1 && verbose
)
1466 fprintf(output
, "[ perf stat: executing run #%d ... ]\n",
1469 status
= run_perf_stat(argc
, argv
);
1470 if (forever
&& status
!= -1) {
1471 print_stat(argc
, argv
);
1472 perf_stat__reset_stats(evsel_list
);
1476 if (!forever
&& status
!= -1 && !interval
)
1477 print_stat(argc
, argv
);
1479 perf_evlist__free_stats(evsel_list
);
1481 perf_evlist__delete(evsel_list
);