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/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"
62 #include "util/group.h"
63 #include "util/session.h"
64 #include "util/tool.h"
65 #include "util/group.h"
68 #include <api/fs/fs.h>
70 #include <sys/prctl.h>
74 #define DEFAULT_SEPARATOR " "
75 #define CNTR_NOT_SUPPORTED "<not supported>"
76 #define CNTR_NOT_COUNTED "<not counted>"
78 static void print_counters(struct timespec
*ts
, int argc
, const char **argv
);
80 /* Default events used for perf stat -T */
81 static const char *transaction_attrs
= {
93 /* More limited version when the CPU does not have all events. */
94 static const char * transaction_limited_attrs
= {
104 static const char * topdown_attrs
[] = {
105 "topdown-total-slots",
106 "topdown-slots-retired",
107 "topdown-recovery-bubbles",
108 "topdown-fetch-bubbles",
109 "topdown-slots-issued",
113 static struct perf_evlist
*evsel_list
;
115 static struct target target
= {
119 typedef int (*aggr_get_id_t
)(struct cpu_map
*m
, int cpu
);
121 static int run_count
= 1;
122 static bool no_inherit
= false;
123 static volatile pid_t child_pid
= -1;
124 static bool null_run
= false;
125 static int detailed_run
= 0;
126 static bool transaction_run
;
127 static bool topdown_run
= false;
128 static bool big_num
= true;
129 static int big_num_opt
= -1;
130 static const char *csv_sep
= NULL
;
131 static bool csv_output
= false;
132 static bool group
= false;
133 static const char *pre_cmd
= NULL
;
134 static const char *post_cmd
= NULL
;
135 static bool sync_run
= false;
136 static unsigned int initial_delay
= 0;
137 static unsigned int unit_width
= 4; /* strlen("unit") */
138 static bool forever
= false;
139 static bool metric_only
= false;
140 static bool force_metric_only
= false;
141 static struct timespec ref_time
;
142 static struct cpu_map
*aggr_map
;
143 static aggr_get_id_t aggr_get_id
;
144 static bool append_file
;
145 static const char *output_name
;
146 static int output_fd
;
150 struct perf_data_file file
;
151 struct perf_session
*session
;
153 struct perf_tool tool
;
155 struct cpu_map
*cpus
;
156 struct thread_map
*threads
;
157 enum aggr_mode aggr_mode
;
160 static struct perf_stat perf_stat
;
161 #define STAT_RECORD perf_stat.record
163 static volatile int done
= 0;
165 static struct perf_stat_config stat_config
= {
166 .aggr_mode
= AGGR_GLOBAL
,
170 static inline void diff_timespec(struct timespec
*r
, struct timespec
*a
,
173 r
->tv_sec
= a
->tv_sec
- b
->tv_sec
;
174 if (a
->tv_nsec
< b
->tv_nsec
) {
175 r
->tv_nsec
= a
->tv_nsec
+ 1000000000L - b
->tv_nsec
;
178 r
->tv_nsec
= a
->tv_nsec
- b
->tv_nsec
;
182 static void perf_stat__reset_stats(void)
184 perf_evlist__reset_stats(evsel_list
);
185 perf_stat__reset_shadow_stats();
188 static int create_perf_stat_counter(struct perf_evsel
*evsel
)
190 struct perf_event_attr
*attr
= &evsel
->attr
;
192 if (stat_config
.scale
)
193 attr
->read_format
= PERF_FORMAT_TOTAL_TIME_ENABLED
|
194 PERF_FORMAT_TOTAL_TIME_RUNNING
;
196 attr
->inherit
= !no_inherit
;
199 * Some events get initialized with sample_(period/type) set,
200 * like tracepoints. Clear it up for counting.
202 attr
->sample_period
= 0;
205 * But set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
206 * while avoiding that older tools show confusing messages.
208 * However for pipe sessions we need to keep it zero,
209 * because script's perf_evsel__check_attr is triggered
210 * by attr->sample_type != 0, and we can't run it on
213 if (!(STAT_RECORD
&& perf_stat
.file
.is_pipe
))
214 attr
->sample_type
= PERF_SAMPLE_IDENTIFIER
;
217 * Disabling all counters initially, they will be enabled
218 * either manually by us or by kernel via enable_on_exec
221 if (perf_evsel__is_group_leader(evsel
)) {
225 * In case of initial_delay we enable tracee
228 if (target__none(&target
) && !initial_delay
)
229 attr
->enable_on_exec
= 1;
232 if (target__has_cpu(&target
))
233 return perf_evsel__open_per_cpu(evsel
, perf_evsel__cpus(evsel
));
235 return perf_evsel__open_per_thread(evsel
, evsel_list
->threads
);
239 * Does the counter have nsecs as a unit?
241 static inline int nsec_counter(struct perf_evsel
*evsel
)
243 if (perf_evsel__match(evsel
, SOFTWARE
, SW_CPU_CLOCK
) ||
244 perf_evsel__match(evsel
, SOFTWARE
, SW_TASK_CLOCK
))
250 static int process_synthesized_event(struct perf_tool
*tool __maybe_unused
,
251 union perf_event
*event
,
252 struct perf_sample
*sample __maybe_unused
,
253 struct machine
*machine __maybe_unused
)
255 if (perf_data_file__write(&perf_stat
.file
, event
, event
->header
.size
) < 0) {
256 pr_err("failed to write perf data, error: %m\n");
260 perf_stat
.bytes_written
+= event
->header
.size
;
264 static int write_stat_round_event(u64 tm
, u64 type
)
266 return perf_event__synthesize_stat_round(NULL
, tm
, type
,
267 process_synthesized_event
,
271 #define WRITE_STAT_ROUND_EVENT(time, interval) \
272 write_stat_round_event(time, PERF_STAT_ROUND_TYPE__ ## interval)
274 #define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
277 perf_evsel__write_stat_event(struct perf_evsel
*counter
, u32 cpu
, u32 thread
,
278 struct perf_counts_values
*count
)
280 struct perf_sample_id
*sid
= SID(counter
, cpu
, thread
);
282 return perf_event__synthesize_stat(NULL
, cpu
, thread
, sid
->id
, count
,
283 process_synthesized_event
, NULL
);
287 * Read out the results of a single counter:
288 * do not aggregate counts across CPUs in system-wide mode
290 static int read_counter(struct perf_evsel
*counter
)
292 int nthreads
= thread_map__nr(evsel_list
->threads
);
293 int ncpus
, cpu
, thread
;
295 if (target__has_cpu(&target
))
296 ncpus
= perf_evsel__nr_cpus(counter
);
300 if (!counter
->supported
)
303 if (counter
->system_wide
)
306 for (thread
= 0; thread
< nthreads
; thread
++) {
307 for (cpu
= 0; cpu
< ncpus
; cpu
++) {
308 struct perf_counts_values
*count
;
310 count
= perf_counts(counter
->counts
, cpu
, thread
);
311 if (perf_evsel__read(counter
, cpu
, thread
, count
))
315 if (perf_evsel__write_stat_event(counter
, cpu
, thread
, count
)) {
316 pr_err("failed to write stat event\n");
322 fprintf(stat_config
.output
,
323 "%s: %d: %" PRIu64
" %" PRIu64
" %" PRIu64
"\n",
324 perf_evsel__name(counter
),
326 count
->val
, count
->ena
, count
->run
);
334 static void read_counters(void)
336 struct perf_evsel
*counter
;
338 evlist__for_each_entry(evsel_list
, counter
) {
339 if (read_counter(counter
))
340 pr_debug("failed to read counter %s\n", counter
->name
);
342 if (perf_stat_process_counter(&stat_config
, counter
))
343 pr_warning("failed to process counter %s\n", counter
->name
);
347 static void process_interval(void)
349 struct timespec ts
, rs
;
353 clock_gettime(CLOCK_MONOTONIC
, &ts
);
354 diff_timespec(&rs
, &ts
, &ref_time
);
357 if (WRITE_STAT_ROUND_EVENT(rs
.tv_sec
* NSECS_PER_SEC
+ rs
.tv_nsec
, INTERVAL
))
358 pr_err("failed to write stat round event\n");
361 print_counters(&rs
, 0, NULL
);
364 static void enable_counters(void)
367 usleep(initial_delay
* 1000);
370 * We need to enable counters only if:
371 * - we don't have tracee (attaching to task or cpu)
372 * - we have initial delay configured
374 if (!target__none(&target
) || initial_delay
)
375 perf_evlist__enable(evsel_list
);
378 static void disable_counters(void)
381 * If we don't have tracee (attaching to task or cpu), counters may
382 * still be running. To get accurate group ratios, we must stop groups
383 * from counting before reading their constituent counters.
385 if (!target__none(&target
))
386 perf_evlist__disable(evsel_list
);
389 static volatile int workload_exec_errno
;
392 * perf_evlist__prepare_workload will send a SIGUSR1
393 * if the fork fails, since we asked by setting its
394 * want_signal to true.
396 static void workload_exec_failed_signal(int signo __maybe_unused
, siginfo_t
*info
,
397 void *ucontext __maybe_unused
)
399 workload_exec_errno
= info
->si_value
.sival_int
;
402 static bool has_unit(struct perf_evsel
*counter
)
404 return counter
->unit
&& *counter
->unit
;
407 static bool has_scale(struct perf_evsel
*counter
)
409 return counter
->scale
!= 1;
412 static int perf_stat_synthesize_config(bool is_pipe
)
414 struct perf_evsel
*counter
;
418 err
= perf_event__synthesize_attrs(NULL
, perf_stat
.session
,
419 process_synthesized_event
);
421 pr_err("Couldn't synthesize attrs.\n");
427 * Synthesize other events stuff not carried within
428 * attr event - unit, scale, name
430 evlist__for_each_entry(evsel_list
, counter
) {
431 if (!counter
->supported
)
435 * Synthesize unit and scale only if it's defined.
437 if (has_unit(counter
)) {
438 err
= perf_event__synthesize_event_update_unit(NULL
, counter
, process_synthesized_event
);
440 pr_err("Couldn't synthesize evsel unit.\n");
445 if (has_scale(counter
)) {
446 err
= perf_event__synthesize_event_update_scale(NULL
, counter
, process_synthesized_event
);
448 pr_err("Couldn't synthesize evsel scale.\n");
453 if (counter
->own_cpus
) {
454 err
= perf_event__synthesize_event_update_cpus(NULL
, counter
, process_synthesized_event
);
456 pr_err("Couldn't synthesize evsel scale.\n");
462 * Name is needed only for pipe output,
463 * perf.data carries event names.
466 err
= perf_event__synthesize_event_update_name(NULL
, counter
, process_synthesized_event
);
468 pr_err("Couldn't synthesize evsel name.\n");
474 err
= perf_event__synthesize_thread_map2(NULL
, evsel_list
->threads
,
475 process_synthesized_event
,
478 pr_err("Couldn't synthesize thread map.\n");
482 err
= perf_event__synthesize_cpu_map(NULL
, evsel_list
->cpus
,
483 process_synthesized_event
, NULL
);
485 pr_err("Couldn't synthesize thread map.\n");
489 err
= perf_event__synthesize_stat_config(NULL
, &stat_config
,
490 process_synthesized_event
, NULL
);
492 pr_err("Couldn't synthesize config.\n");
499 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
501 static int __store_counter_ids(struct perf_evsel
*counter
,
502 struct cpu_map
*cpus
,
503 struct thread_map
*threads
)
507 for (cpu
= 0; cpu
< cpus
->nr
; cpu
++) {
508 for (thread
= 0; thread
< threads
->nr
; thread
++) {
509 int fd
= FD(counter
, cpu
, thread
);
511 if (perf_evlist__id_add_fd(evsel_list
, counter
,
512 cpu
, thread
, fd
) < 0)
520 static int store_counter_ids(struct perf_evsel
*counter
)
522 struct cpu_map
*cpus
= counter
->cpus
;
523 struct thread_map
*threads
= counter
->threads
;
525 if (perf_evsel__alloc_id(counter
, cpus
->nr
, threads
->nr
))
528 return __store_counter_ids(counter
, cpus
, threads
);
531 static int __run_perf_stat(int argc
, const char **argv
)
533 int interval
= stat_config
.interval
;
535 unsigned long long t0
, t1
;
536 struct perf_evsel
*counter
;
540 const bool forks
= (argc
> 0);
541 bool is_pipe
= STAT_RECORD
? perf_stat
.file
.is_pipe
: false;
544 ts
.tv_sec
= interval
/ 1000;
545 ts
.tv_nsec
= (interval
% 1000) * 1000000;
552 if (perf_evlist__prepare_workload(evsel_list
, &target
, argv
, is_pipe
,
553 workload_exec_failed_signal
) < 0) {
554 perror("failed to prepare workload");
557 child_pid
= evsel_list
->workload
.pid
;
561 perf_evlist__set_leader(evsel_list
);
563 evlist__for_each_entry(evsel_list
, counter
) {
565 if (create_perf_stat_counter(counter
) < 0) {
567 * PPC returns ENXIO for HW counters until 2.6.37
568 * (behavior changed with commit b0a873e).
570 if (errno
== EINVAL
|| errno
== ENOSYS
||
571 errno
== ENOENT
|| errno
== EOPNOTSUPP
||
574 ui__warning("%s event is not supported by the kernel.\n",
575 perf_evsel__name(counter
));
576 counter
->supported
= false;
578 if ((counter
->leader
!= counter
) ||
579 !(counter
->leader
->nr_members
> 1))
581 } else if (perf_evsel__fallback(counter
, errno
, msg
, sizeof(msg
))) {
583 ui__warning("%s\n", msg
);
587 perf_evsel__open_strerror(counter
, &target
,
588 errno
, msg
, sizeof(msg
));
589 ui__error("%s\n", msg
);
592 kill(child_pid
, SIGTERM
);
596 counter
->supported
= true;
598 l
= strlen(counter
->unit
);
602 if (STAT_RECORD
&& store_counter_ids(counter
))
606 if (perf_evlist__apply_filters(evsel_list
, &counter
)) {
607 error("failed to set filter \"%s\" on event %s with %d (%s)\n",
608 counter
->filter
, perf_evsel__name(counter
), errno
,
609 str_error_r(errno
, msg
, sizeof(msg
)));
614 int err
, fd
= perf_data_file__fd(&perf_stat
.file
);
617 err
= perf_header__write_pipe(perf_data_file__fd(&perf_stat
.file
));
619 err
= perf_session__write_header(perf_stat
.session
, evsel_list
,
626 err
= perf_stat_synthesize_config(is_pipe
);
632 * Enable counters and exec the command:
635 clock_gettime(CLOCK_MONOTONIC
, &ref_time
);
638 perf_evlist__start_workload(evsel_list
);
642 while (!waitpid(child_pid
, &status
, WNOHANG
)) {
643 nanosleep(&ts
, NULL
);
649 if (workload_exec_errno
) {
650 const char *emsg
= str_error_r(workload_exec_errno
, msg
, sizeof(msg
));
651 pr_err("Workload failed: %s\n", emsg
);
655 if (WIFSIGNALED(status
))
656 psignal(WTERMSIG(status
), argv
[0]);
660 nanosleep(&ts
, NULL
);
670 update_stats(&walltime_nsecs_stats
, t1
- t0
);
673 * Closing a group leader splits the group, and as we only disable
674 * group leaders, results in remaining events becoming enabled. To
675 * avoid arbitrary skew, we must read all counters before closing any
679 perf_evlist__close(evsel_list
);
681 return WEXITSTATUS(status
);
684 static int run_perf_stat(int argc
, const char **argv
)
689 ret
= system(pre_cmd
);
697 ret
= __run_perf_stat(argc
, argv
);
702 ret
= system(post_cmd
);
710 static void print_running(u64 run
, u64 ena
)
713 fprintf(stat_config
.output
, "%s%" PRIu64
"%s%.2f",
717 ena
? 100.0 * run
/ ena
: 100.0);
718 } else if (run
!= ena
) {
719 fprintf(stat_config
.output
, " (%.2f%%)", 100.0 * run
/ ena
);
723 static void print_noise_pct(double total
, double avg
)
725 double pct
= rel_stddev_stats(total
, avg
);
728 fprintf(stat_config
.output
, "%s%.2f%%", csv_sep
, pct
);
730 fprintf(stat_config
.output
, " ( +-%6.2f%% )", pct
);
733 static void print_noise(struct perf_evsel
*evsel
, double avg
)
735 struct perf_stat_evsel
*ps
;
741 print_noise_pct(stddev_stats(&ps
->res_stats
[0]), avg
);
744 static void aggr_printout(struct perf_evsel
*evsel
, int id
, int nr
)
746 switch (stat_config
.aggr_mode
) {
748 fprintf(stat_config
.output
, "S%d-C%*d%s%*d%s",
749 cpu_map__id_to_socket(id
),
751 cpu_map__id_to_cpu(id
),
758 fprintf(stat_config
.output
, "S%*d%s%*d%s",
767 fprintf(stat_config
.output
, "CPU%*d%s",
769 perf_evsel__cpus(evsel
)->map
[id
], csv_sep
);
772 fprintf(stat_config
.output
, "%*s-%*d%s",
774 thread_map__comm(evsel
->threads
, id
),
776 thread_map__pid(evsel
->threads
, id
),
792 struct perf_evsel
*evsel
;
795 #define METRIC_LEN 35
797 static void new_line_std(void *ctx
)
799 struct outstate
*os
= ctx
;
804 static void do_new_line_std(struct outstate
*os
)
807 fputs(os
->prefix
, os
->fh
);
808 aggr_printout(os
->evsel
, os
->id
, os
->nr
);
809 if (stat_config
.aggr_mode
== AGGR_NONE
)
810 fprintf(os
->fh
, " ");
811 fprintf(os
->fh
, " ");
814 static void print_metric_std(void *ctx
, const char *color
, const char *fmt
,
815 const char *unit
, double val
)
817 struct outstate
*os
= ctx
;
820 bool newline
= os
->newline
;
824 if (unit
== NULL
|| fmt
== NULL
) {
825 fprintf(out
, "%-*s", METRIC_LEN
, "");
832 n
= fprintf(out
, " # ");
834 n
+= color_fprintf(out
, color
, fmt
, val
);
836 n
+= fprintf(out
, fmt
, val
);
837 fprintf(out
, " %-*s", METRIC_LEN
- n
- 1, unit
);
840 static void new_line_csv(void *ctx
)
842 struct outstate
*os
= ctx
;
847 fprintf(os
->fh
, "%s%s", os
->prefix
, csv_sep
);
848 aggr_printout(os
->evsel
, os
->id
, os
->nr
);
849 for (i
= 0; i
< os
->nfields
; i
++)
850 fputs(csv_sep
, os
->fh
);
853 static void print_metric_csv(void *ctx
,
854 const char *color __maybe_unused
,
855 const char *fmt
, const char *unit
, double val
)
857 struct outstate
*os
= ctx
;
859 char buf
[64], *vals
, *ends
;
861 if (unit
== NULL
|| fmt
== NULL
) {
862 fprintf(out
, "%s%s%s%s", csv_sep
, csv_sep
, csv_sep
, csv_sep
);
865 snprintf(buf
, sizeof(buf
), fmt
, val
);
867 while (isspace(*vals
))
870 while (isdigit(*ends
) || *ends
== '.')
873 while (isspace(*unit
))
875 fprintf(out
, "%s%s%s%s", csv_sep
, vals
, csv_sep
, unit
);
878 #define METRIC_ONLY_LEN 20
880 /* Filter out some columns that don't work well in metrics only mode */
882 static bool valid_only_metric(const char *unit
)
886 if (strstr(unit
, "/sec") ||
887 strstr(unit
, "hz") ||
888 strstr(unit
, "Hz") ||
889 strstr(unit
, "CPUs utilized"))
894 static const char *fixunit(char *buf
, struct perf_evsel
*evsel
,
897 if (!strncmp(unit
, "of all", 6)) {
898 snprintf(buf
, 1024, "%s %s", perf_evsel__name(evsel
),
905 static void print_metric_only(void *ctx
, const char *color
, const char *fmt
,
906 const char *unit
, double val
)
908 struct outstate
*os
= ctx
;
912 unsigned mlen
= METRIC_ONLY_LEN
;
914 if (!valid_only_metric(unit
))
916 unit
= fixunit(buf
, os
->evsel
, unit
);
918 n
= color_fprintf(out
, color
, fmt
, val
);
920 n
= fprintf(out
, fmt
, val
);
921 if (n
> METRIC_ONLY_LEN
)
923 if (mlen
< strlen(unit
))
924 mlen
= strlen(unit
) + 1;
925 fprintf(out
, "%*s", mlen
- n
, "");
928 static void print_metric_only_csv(void *ctx
, const char *color __maybe_unused
,
930 const char *unit
, double val
)
932 struct outstate
*os
= ctx
;
934 char buf
[64], *vals
, *ends
;
937 if (!valid_only_metric(unit
))
939 unit
= fixunit(tbuf
, os
->evsel
, unit
);
940 snprintf(buf
, sizeof buf
, fmt
, val
);
942 while (isspace(*vals
))
945 while (isdigit(*ends
) || *ends
== '.')
948 fprintf(out
, "%s%s", vals
, csv_sep
);
951 static void new_line_metric(void *ctx __maybe_unused
)
955 static void print_metric_header(void *ctx
, const char *color __maybe_unused
,
956 const char *fmt __maybe_unused
,
957 const char *unit
, double val __maybe_unused
)
959 struct outstate
*os
= ctx
;
962 if (!valid_only_metric(unit
))
964 unit
= fixunit(tbuf
, os
->evsel
, unit
);
966 fprintf(os
->fh
, "%s%s", unit
, csv_sep
);
968 fprintf(os
->fh
, "%-*s ", METRIC_ONLY_LEN
, unit
);
971 static void nsec_printout(int id
, int nr
, struct perf_evsel
*evsel
, double avg
)
973 FILE *output
= stat_config
.output
;
974 double msecs
= avg
/ 1e6
;
975 const char *fmt_v
, *fmt_n
;
978 fmt_v
= csv_output
? "%.6f%s" : "%18.6f%s";
979 fmt_n
= csv_output
? "%s" : "%-25s";
981 aggr_printout(evsel
, id
, nr
);
983 scnprintf(name
, sizeof(name
), "%s%s",
984 perf_evsel__name(evsel
), csv_output
? "" : " (msec)");
986 fprintf(output
, fmt_v
, msecs
, csv_sep
);
989 fprintf(output
, "%s%s", evsel
->unit
, csv_sep
);
991 fprintf(output
, "%-*s%s", unit_width
, evsel
->unit
, csv_sep
);
993 fprintf(output
, fmt_n
, name
);
996 fprintf(output
, "%s%s", csv_sep
, evsel
->cgrp
->name
);
999 static int first_shadow_cpu(struct perf_evsel
*evsel
, int id
)
1006 if (stat_config
.aggr_mode
== AGGR_NONE
)
1009 if (stat_config
.aggr_mode
== AGGR_GLOBAL
)
1012 for (i
= 0; i
< perf_evsel__nr_cpus(evsel
); i
++) {
1013 int cpu2
= perf_evsel__cpus(evsel
)->map
[i
];
1015 if (aggr_get_id(evsel_list
->cpus
, cpu2
) == id
)
1021 static void abs_printout(int id
, int nr
, struct perf_evsel
*evsel
, double avg
)
1023 FILE *output
= stat_config
.output
;
1024 double sc
= evsel
->scale
;
1028 fmt
= floor(sc
) != sc
? "%.2f%s" : "%.0f%s";
1031 fmt
= floor(sc
) != sc
? "%'18.2f%s" : "%'18.0f%s";
1033 fmt
= floor(sc
) != sc
? "%18.2f%s" : "%18.0f%s";
1036 aggr_printout(evsel
, id
, nr
);
1038 fprintf(output
, fmt
, avg
, csv_sep
);
1041 fprintf(output
, "%-*s%s",
1042 csv_output
? 0 : unit_width
,
1043 evsel
->unit
, csv_sep
);
1045 fprintf(output
, "%-*s", csv_output
? 0 : 25, perf_evsel__name(evsel
));
1048 fprintf(output
, "%s%s", csv_sep
, evsel
->cgrp
->name
);
1051 static void printout(int id
, int nr
, struct perf_evsel
*counter
, double uval
,
1052 char *prefix
, u64 run
, u64 ena
, double noise
)
1054 struct perf_stat_output_ctx out
;
1055 struct outstate os
= {
1056 .fh
= stat_config
.output
,
1057 .prefix
= prefix
? prefix
: "",
1062 print_metric_t pm
= print_metric_std
;
1066 nl
= new_line_metric
;
1068 pm
= print_metric_only_csv
;
1070 pm
= print_metric_only
;
1074 if (csv_output
&& !metric_only
) {
1075 static int aggr_fields
[] = {
1083 pm
= print_metric_csv
;
1086 os
.nfields
+= aggr_fields
[stat_config
.aggr_mode
];
1090 if (run
== 0 || ena
== 0 || counter
->counts
->scaled
== -1) {
1092 pm(&os
, NULL
, "", "", 0);
1095 aggr_printout(counter
, id
, nr
);
1097 fprintf(stat_config
.output
, "%*s%s",
1098 csv_output
? 0 : 18,
1099 counter
->supported
? CNTR_NOT_COUNTED
: CNTR_NOT_SUPPORTED
,
1102 fprintf(stat_config
.output
, "%-*s%s",
1103 csv_output
? 0 : unit_width
,
1104 counter
->unit
, csv_sep
);
1106 fprintf(stat_config
.output
, "%*s",
1107 csv_output
? 0 : -25,
1108 perf_evsel__name(counter
));
1111 fprintf(stat_config
.output
, "%s%s",
1112 csv_sep
, counter
->cgrp
->name
);
1115 pm(&os
, NULL
, NULL
, "", 0);
1116 print_noise(counter
, noise
);
1117 print_running(run
, ena
);
1119 pm(&os
, NULL
, NULL
, "", 0);
1125 else if (nsec_counter(counter
))
1126 nsec_printout(id
, nr
, counter
, uval
);
1128 abs_printout(id
, nr
, counter
, uval
);
1130 out
.print_metric
= pm
;
1134 if (csv_output
&& !metric_only
) {
1135 print_noise(counter
, noise
);
1136 print_running(run
, ena
);
1139 perf_stat__print_shadow_stats(counter
, uval
,
1140 first_shadow_cpu(counter
, id
),
1142 if (!csv_output
&& !metric_only
) {
1143 print_noise(counter
, noise
);
1144 print_running(run
, ena
);
1148 static void aggr_update_shadow(void)
1152 struct perf_evsel
*counter
;
1154 for (s
= 0; s
< aggr_map
->nr
; s
++) {
1155 id
= aggr_map
->map
[s
];
1156 evlist__for_each_entry(evsel_list
, counter
) {
1158 for (cpu
= 0; cpu
< perf_evsel__nr_cpus(counter
); cpu
++) {
1159 s2
= aggr_get_id(evsel_list
->cpus
, cpu
);
1162 val
+= perf_counts(counter
->counts
, cpu
, 0)->val
;
1164 val
= val
* counter
->scale
;
1165 perf_stat__update_shadow_stats(counter
, &val
,
1166 first_shadow_cpu(counter
, id
));
1171 static void print_aggr(char *prefix
)
1173 FILE *output
= stat_config
.output
;
1174 struct perf_evsel
*counter
;
1175 int cpu
, s
, s2
, id
, nr
;
1180 if (!(aggr_map
|| aggr_get_id
))
1183 aggr_update_shadow();
1186 * With metric_only everything is on a single line.
1187 * Without each counter has its own line.
1189 for (s
= 0; s
< aggr_map
->nr
; s
++) {
1190 if (prefix
&& metric_only
)
1191 fprintf(output
, "%s", prefix
);
1193 id
= aggr_map
->map
[s
];
1195 evlist__for_each_entry(evsel_list
, counter
) {
1196 val
= ena
= run
= 0;
1198 for (cpu
= 0; cpu
< perf_evsel__nr_cpus(counter
); cpu
++) {
1199 s2
= aggr_get_id(perf_evsel__cpus(counter
), cpu
);
1202 val
+= perf_counts(counter
->counts
, cpu
, 0)->val
;
1203 ena
+= perf_counts(counter
->counts
, cpu
, 0)->ena
;
1204 run
+= perf_counts(counter
->counts
, cpu
, 0)->run
;
1207 if (first
&& metric_only
) {
1209 aggr_printout(counter
, id
, nr
);
1211 if (prefix
&& !metric_only
)
1212 fprintf(output
, "%s", prefix
);
1214 uval
= val
* counter
->scale
;
1215 printout(id
, nr
, counter
, uval
, prefix
, run
, ena
, 1.0);
1217 fputc('\n', output
);
1220 fputc('\n', output
);
1224 static void print_aggr_thread(struct perf_evsel
*counter
, char *prefix
)
1226 FILE *output
= stat_config
.output
;
1227 int nthreads
= thread_map__nr(counter
->threads
);
1228 int ncpus
= cpu_map__nr(counter
->cpus
);
1232 for (thread
= 0; thread
< nthreads
; thread
++) {
1233 u64 ena
= 0, run
= 0, val
= 0;
1235 for (cpu
= 0; cpu
< ncpus
; cpu
++) {
1236 val
+= perf_counts(counter
->counts
, cpu
, thread
)->val
;
1237 ena
+= perf_counts(counter
->counts
, cpu
, thread
)->ena
;
1238 run
+= perf_counts(counter
->counts
, cpu
, thread
)->run
;
1242 fprintf(output
, "%s", prefix
);
1244 uval
= val
* counter
->scale
;
1245 printout(thread
, 0, counter
, uval
, prefix
, run
, ena
, 1.0);
1246 fputc('\n', output
);
1251 * Print out the results of a single counter:
1252 * aggregated counts in system-wide mode
1254 static void print_counter_aggr(struct perf_evsel
*counter
, char *prefix
)
1256 FILE *output
= stat_config
.output
;
1257 struct perf_stat_evsel
*ps
= counter
->priv
;
1258 double avg
= avg_stats(&ps
->res_stats
[0]);
1260 double avg_enabled
, avg_running
;
1262 avg_enabled
= avg_stats(&ps
->res_stats
[1]);
1263 avg_running
= avg_stats(&ps
->res_stats
[2]);
1265 if (prefix
&& !metric_only
)
1266 fprintf(output
, "%s", prefix
);
1268 uval
= avg
* counter
->scale
;
1269 printout(-1, 0, counter
, uval
, prefix
, avg_running
, avg_enabled
, avg
);
1271 fprintf(output
, "\n");
1275 * Print out the results of a single counter:
1276 * does not use aggregated count in system-wide
1278 static void print_counter(struct perf_evsel
*counter
, char *prefix
)
1280 FILE *output
= stat_config
.output
;
1285 for (cpu
= 0; cpu
< perf_evsel__nr_cpus(counter
); cpu
++) {
1286 val
= perf_counts(counter
->counts
, cpu
, 0)->val
;
1287 ena
= perf_counts(counter
->counts
, cpu
, 0)->ena
;
1288 run
= perf_counts(counter
->counts
, cpu
, 0)->run
;
1291 fprintf(output
, "%s", prefix
);
1293 uval
= val
* counter
->scale
;
1294 printout(cpu
, 0, counter
, uval
, prefix
, run
, ena
, 1.0);
1296 fputc('\n', output
);
1300 static void print_no_aggr_metric(char *prefix
)
1304 struct perf_evsel
*counter
;
1308 nrcpus
= evsel_list
->cpus
->nr
;
1309 for (cpu
= 0; cpu
< nrcpus
; cpu
++) {
1313 fputs(prefix
, stat_config
.output
);
1314 evlist__for_each_entry(evsel_list
, counter
) {
1316 aggr_printout(counter
, cpu
, 0);
1319 val
= perf_counts(counter
->counts
, cpu
, 0)->val
;
1320 ena
= perf_counts(counter
->counts
, cpu
, 0)->ena
;
1321 run
= perf_counts(counter
->counts
, cpu
, 0)->run
;
1323 uval
= val
* counter
->scale
;
1324 printout(cpu
, 0, counter
, uval
, prefix
, run
, ena
, 1.0);
1326 fputc('\n', stat_config
.output
);
1330 static int aggr_header_lens
[] = {
1338 static const char *aggr_header_csv
[] = {
1339 [AGGR_CORE
] = "core,cpus,",
1340 [AGGR_SOCKET
] = "socket,cpus",
1341 [AGGR_NONE
] = "cpu,",
1342 [AGGR_THREAD
] = "comm-pid,",
1346 static void print_metric_headers(const char *prefix
, bool no_indent
)
1348 struct perf_stat_output_ctx out
;
1349 struct perf_evsel
*counter
;
1350 struct outstate os
= {
1351 .fh
= stat_config
.output
1355 fprintf(stat_config
.output
, "%s", prefix
);
1357 if (!csv_output
&& !no_indent
)
1358 fprintf(stat_config
.output
, "%*s",
1359 aggr_header_lens
[stat_config
.aggr_mode
], "");
1361 if (stat_config
.interval
)
1362 fputs("time,", stat_config
.output
);
1363 fputs(aggr_header_csv
[stat_config
.aggr_mode
],
1364 stat_config
.output
);
1367 /* Print metrics headers only */
1368 evlist__for_each_entry(evsel_list
, counter
) {
1371 out
.print_metric
= print_metric_header
;
1372 out
.new_line
= new_line_metric
;
1374 perf_stat__print_shadow_stats(counter
, 0,
1378 fputc('\n', stat_config
.output
);
1381 static void print_interval(char *prefix
, struct timespec
*ts
)
1383 FILE *output
= stat_config
.output
;
1384 static int num_print_interval
;
1386 sprintf(prefix
, "%6lu.%09lu%s", ts
->tv_sec
, ts
->tv_nsec
, csv_sep
);
1388 if (num_print_interval
== 0 && !csv_output
) {
1389 switch (stat_config
.aggr_mode
) {
1391 fprintf(output
, "# time socket cpus");
1393 fprintf(output
, " counts %*s events\n", unit_width
, "unit");
1396 fprintf(output
, "# time core cpus");
1398 fprintf(output
, " counts %*s events\n", unit_width
, "unit");
1401 fprintf(output
, "# time CPU");
1403 fprintf(output
, " counts %*s events\n", unit_width
, "unit");
1406 fprintf(output
, "# time comm-pid");
1408 fprintf(output
, " counts %*s events\n", unit_width
, "unit");
1412 fprintf(output
, "# time");
1414 fprintf(output
, " counts %*s events\n", unit_width
, "unit");
1420 if (num_print_interval
== 0 && metric_only
)
1421 print_metric_headers(" ", true);
1422 if (++num_print_interval
== 25)
1423 num_print_interval
= 0;
1426 static void print_header(int argc
, const char **argv
)
1428 FILE *output
= stat_config
.output
;
1434 fprintf(output
, "\n");
1435 fprintf(output
, " Performance counter stats for ");
1436 if (target
.system_wide
)
1437 fprintf(output
, "\'system wide");
1438 else if (target
.cpu_list
)
1439 fprintf(output
, "\'CPU(s) %s", target
.cpu_list
);
1440 else if (!target__has_task(&target
)) {
1441 fprintf(output
, "\'%s", argv
? argv
[0] : "pipe");
1442 for (i
= 1; argv
&& (i
< argc
); i
++)
1443 fprintf(output
, " %s", argv
[i
]);
1444 } else if (target
.pid
)
1445 fprintf(output
, "process id \'%s", target
.pid
);
1447 fprintf(output
, "thread id \'%s", target
.tid
);
1449 fprintf(output
, "\'");
1451 fprintf(output
, " (%d runs)", run_count
);
1452 fprintf(output
, ":\n\n");
1456 static void print_footer(void)
1458 FILE *output
= stat_config
.output
;
1461 fprintf(output
, "\n");
1462 fprintf(output
, " %17.9f seconds time elapsed",
1463 avg_stats(&walltime_nsecs_stats
)/1e9
);
1464 if (run_count
> 1) {
1465 fprintf(output
, " ");
1466 print_noise_pct(stddev_stats(&walltime_nsecs_stats
),
1467 avg_stats(&walltime_nsecs_stats
));
1469 fprintf(output
, "\n\n");
1472 static void print_counters(struct timespec
*ts
, int argc
, const char **argv
)
1474 int interval
= stat_config
.interval
;
1475 struct perf_evsel
*counter
;
1476 char buf
[64], *prefix
= NULL
;
1478 /* Do not print anything if we record to the pipe. */
1479 if (STAT_RECORD
&& perf_stat
.file
.is_pipe
)
1483 print_interval(prefix
= buf
, ts
);
1485 print_header(argc
, argv
);
1488 static int num_print_iv
;
1490 if (num_print_iv
== 0 && !interval
)
1491 print_metric_headers(prefix
, false);
1492 if (num_print_iv
++ == 25)
1494 if (stat_config
.aggr_mode
== AGGR_GLOBAL
&& prefix
)
1495 fprintf(stat_config
.output
, "%s", prefix
);
1498 switch (stat_config
.aggr_mode
) {
1504 evlist__for_each_entry(evsel_list
, counter
)
1505 print_aggr_thread(counter
, prefix
);
1508 evlist__for_each_entry(evsel_list
, counter
)
1509 print_counter_aggr(counter
, prefix
);
1511 fputc('\n', stat_config
.output
);
1515 print_no_aggr_metric(prefix
);
1517 evlist__for_each_entry(evsel_list
, counter
)
1518 print_counter(counter
, prefix
);
1526 if (!interval
&& !csv_output
)
1529 fflush(stat_config
.output
);
1532 static volatile int signr
= -1;
1534 static void skip_signal(int signo
)
1536 if ((child_pid
== -1) || stat_config
.interval
)
1541 * render child_pid harmless
1542 * won't send SIGTERM to a random
1543 * process in case of race condition
1544 * and fast PID recycling
1549 static void sig_atexit(void)
1554 * avoid race condition with SIGCHLD handler
1555 * in skip_signal() which is modifying child_pid
1556 * goal is to avoid send SIGTERM to a random
1560 sigaddset(&set
, SIGCHLD
);
1561 sigprocmask(SIG_BLOCK
, &set
, &oset
);
1563 if (child_pid
!= -1)
1564 kill(child_pid
, SIGTERM
);
1566 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
1571 signal(signr
, SIG_DFL
);
1572 kill(getpid(), signr
);
1575 static int stat__set_big_num(const struct option
*opt __maybe_unused
,
1576 const char *s __maybe_unused
, int unset
)
1578 big_num_opt
= unset
? 0 : 1;
1582 static int enable_metric_only(const struct option
*opt __maybe_unused
,
1583 const char *s __maybe_unused
, int unset
)
1585 force_metric_only
= true;
1586 metric_only
= !unset
;
1590 static const struct option stat_options
[] = {
1591 OPT_BOOLEAN('T', "transaction", &transaction_run
,
1592 "hardware transaction statistics"),
1593 OPT_CALLBACK('e', "event", &evsel_list
, "event",
1594 "event selector. use 'perf list' to list available events",
1595 parse_events_option
),
1596 OPT_CALLBACK(0, "filter", &evsel_list
, "filter",
1597 "event filter", parse_filter
),
1598 OPT_BOOLEAN('i', "no-inherit", &no_inherit
,
1599 "child tasks do not inherit counters"),
1600 OPT_STRING('p', "pid", &target
.pid
, "pid",
1601 "stat events on existing process id"),
1602 OPT_STRING('t', "tid", &target
.tid
, "tid",
1603 "stat events on existing thread id"),
1604 OPT_BOOLEAN('a', "all-cpus", &target
.system_wide
,
1605 "system-wide collection from all CPUs"),
1606 OPT_BOOLEAN('g', "group", &group
,
1607 "put the counters into a counter group"),
1608 OPT_BOOLEAN('c', "scale", &stat_config
.scale
, "scale/normalize counters"),
1609 OPT_INCR('v', "verbose", &verbose
,
1610 "be more verbose (show counter open errors, etc)"),
1611 OPT_INTEGER('r', "repeat", &run_count
,
1612 "repeat command and print average + stddev (max: 100, forever: 0)"),
1613 OPT_BOOLEAN('n', "null", &null_run
,
1614 "null run - dont start any counters"),
1615 OPT_INCR('d', "detailed", &detailed_run
,
1616 "detailed run - start a lot of events"),
1617 OPT_BOOLEAN('S', "sync", &sync_run
,
1618 "call sync() before starting a run"),
1619 OPT_CALLBACK_NOOPT('B', "big-num", NULL
, NULL
,
1620 "print large numbers with thousands\' separators",
1622 OPT_STRING('C', "cpu", &target
.cpu_list
, "cpu",
1623 "list of cpus to monitor in system-wide"),
1624 OPT_SET_UINT('A', "no-aggr", &stat_config
.aggr_mode
,
1625 "disable CPU count aggregation", AGGR_NONE
),
1626 OPT_STRING('x', "field-separator", &csv_sep
, "separator",
1627 "print counts with custom separator"),
1628 OPT_CALLBACK('G', "cgroup", &evsel_list
, "name",
1629 "monitor event in cgroup name only", parse_cgroups
),
1630 OPT_STRING('o', "output", &output_name
, "file", "output file name"),
1631 OPT_BOOLEAN(0, "append", &append_file
, "append to the output file"),
1632 OPT_INTEGER(0, "log-fd", &output_fd
,
1633 "log output to fd, instead of stderr"),
1634 OPT_STRING(0, "pre", &pre_cmd
, "command",
1635 "command to run prior to the measured command"),
1636 OPT_STRING(0, "post", &post_cmd
, "command",
1637 "command to run after to the measured command"),
1638 OPT_UINTEGER('I', "interval-print", &stat_config
.interval
,
1639 "print counts at regular interval in ms (>= 10)"),
1640 OPT_SET_UINT(0, "per-socket", &stat_config
.aggr_mode
,
1641 "aggregate counts per processor socket", AGGR_SOCKET
),
1642 OPT_SET_UINT(0, "per-core", &stat_config
.aggr_mode
,
1643 "aggregate counts per physical processor core", AGGR_CORE
),
1644 OPT_SET_UINT(0, "per-thread", &stat_config
.aggr_mode
,
1645 "aggregate counts per thread", AGGR_THREAD
),
1646 OPT_UINTEGER('D', "delay", &initial_delay
,
1647 "ms to wait before starting measurement after program start"),
1648 OPT_CALLBACK_NOOPT(0, "metric-only", &metric_only
, NULL
,
1649 "Only print computed metrics. No raw values", enable_metric_only
),
1650 OPT_BOOLEAN(0, "topdown", &topdown_run
,
1651 "measure topdown level 1 statistics"),
1655 static int perf_stat__get_socket(struct cpu_map
*map
, int cpu
)
1657 return cpu_map__get_socket(map
, cpu
, NULL
);
1660 static int perf_stat__get_core(struct cpu_map
*map
, int cpu
)
1662 return cpu_map__get_core(map
, cpu
, NULL
);
1665 static int cpu_map__get_max(struct cpu_map
*map
)
1669 for (i
= 0; i
< map
->nr
; i
++) {
1670 if (map
->map
[i
] > max
)
1677 static struct cpu_map
*cpus_aggr_map
;
1679 static int perf_stat__get_aggr(aggr_get_id_t get_id
, struct cpu_map
*map
, int idx
)
1686 cpu
= map
->map
[idx
];
1688 if (cpus_aggr_map
->map
[cpu
] == -1)
1689 cpus_aggr_map
->map
[cpu
] = get_id(map
, idx
);
1691 return cpus_aggr_map
->map
[cpu
];
1694 static int perf_stat__get_socket_cached(struct cpu_map
*map
, int idx
)
1696 return perf_stat__get_aggr(perf_stat__get_socket
, map
, idx
);
1699 static int perf_stat__get_core_cached(struct cpu_map
*map
, int idx
)
1701 return perf_stat__get_aggr(perf_stat__get_core
, map
, idx
);
1704 static int perf_stat_init_aggr_mode(void)
1708 switch (stat_config
.aggr_mode
) {
1710 if (cpu_map__build_socket_map(evsel_list
->cpus
, &aggr_map
)) {
1711 perror("cannot build socket map");
1714 aggr_get_id
= perf_stat__get_socket_cached
;
1717 if (cpu_map__build_core_map(evsel_list
->cpus
, &aggr_map
)) {
1718 perror("cannot build core map");
1721 aggr_get_id
= perf_stat__get_core_cached
;
1732 * The evsel_list->cpus is the base we operate on,
1733 * taking the highest cpu number to be the size of
1734 * the aggregation translate cpumap.
1736 nr
= cpu_map__get_max(evsel_list
->cpus
);
1737 cpus_aggr_map
= cpu_map__empty_new(nr
+ 1);
1738 return cpus_aggr_map
? 0 : -ENOMEM
;
1741 static void perf_stat__exit_aggr_mode(void)
1743 cpu_map__put(aggr_map
);
1744 cpu_map__put(cpus_aggr_map
);
1746 cpus_aggr_map
= NULL
;
1749 static inline int perf_env__get_cpu(struct perf_env
*env
, struct cpu_map
*map
, int idx
)
1756 cpu
= map
->map
[idx
];
1758 if (cpu
>= env
->nr_cpus_online
)
1764 static int perf_env__get_socket(struct cpu_map
*map
, int idx
, void *data
)
1766 struct perf_env
*env
= data
;
1767 int cpu
= perf_env__get_cpu(env
, map
, idx
);
1769 return cpu
== -1 ? -1 : env
->cpu
[cpu
].socket_id
;
1772 static int perf_env__get_core(struct cpu_map
*map
, int idx
, void *data
)
1774 struct perf_env
*env
= data
;
1775 int core
= -1, cpu
= perf_env__get_cpu(env
, map
, idx
);
1778 int socket_id
= env
->cpu
[cpu
].socket_id
;
1781 * Encode socket in upper 16 bits
1782 * core_id is relative to socket, and
1783 * we need a global id. So we combine
1786 core
= (socket_id
<< 16) | (env
->cpu
[cpu
].core_id
& 0xffff);
1792 static int perf_env__build_socket_map(struct perf_env
*env
, struct cpu_map
*cpus
,
1793 struct cpu_map
**sockp
)
1795 return cpu_map__build_map(cpus
, sockp
, perf_env__get_socket
, env
);
1798 static int perf_env__build_core_map(struct perf_env
*env
, struct cpu_map
*cpus
,
1799 struct cpu_map
**corep
)
1801 return cpu_map__build_map(cpus
, corep
, perf_env__get_core
, env
);
1804 static int perf_stat__get_socket_file(struct cpu_map
*map
, int idx
)
1806 return perf_env__get_socket(map
, idx
, &perf_stat
.session
->header
.env
);
1809 static int perf_stat__get_core_file(struct cpu_map
*map
, int idx
)
1811 return perf_env__get_core(map
, idx
, &perf_stat
.session
->header
.env
);
1814 static int perf_stat_init_aggr_mode_file(struct perf_stat
*st
)
1816 struct perf_env
*env
= &st
->session
->header
.env
;
1818 switch (stat_config
.aggr_mode
) {
1820 if (perf_env__build_socket_map(env
, evsel_list
->cpus
, &aggr_map
)) {
1821 perror("cannot build socket map");
1824 aggr_get_id
= perf_stat__get_socket_file
;
1827 if (perf_env__build_core_map(env
, evsel_list
->cpus
, &aggr_map
)) {
1828 perror("cannot build core map");
1831 aggr_get_id
= perf_stat__get_core_file
;
1844 static int topdown_filter_events(const char **attr
, char **str
, bool use_group
)
1851 for (i
= 0; attr
[i
]; i
++) {
1852 if (pmu_have_event("cpu", attr
[i
])) {
1853 len
+= strlen(attr
[i
]) + 1;
1854 attr
[i
- off
] = attr
[i
];
1858 attr
[i
- off
] = NULL
;
1860 *str
= malloc(len
+ 1 + 2);
1870 for (i
= 0; attr
[i
]; i
++) {
1883 __weak
bool arch_topdown_check_group(bool *warn
)
1889 __weak
void arch_topdown_group_warn(void)
1894 * Add default attributes, if there were no attributes specified or
1895 * if -d/--detailed, -d -d or -d -d -d is used:
1897 static int add_default_attributes(void)
1900 struct perf_event_attr default_attrs0
[] = {
1902 { .type
= PERF_TYPE_SOFTWARE
, .config
= PERF_COUNT_SW_TASK_CLOCK
},
1903 { .type
= PERF_TYPE_SOFTWARE
, .config
= PERF_COUNT_SW_CONTEXT_SWITCHES
},
1904 { .type
= PERF_TYPE_SOFTWARE
, .config
= PERF_COUNT_SW_CPU_MIGRATIONS
},
1905 { .type
= PERF_TYPE_SOFTWARE
, .config
= PERF_COUNT_SW_PAGE_FAULTS
},
1907 { .type
= PERF_TYPE_HARDWARE
, .config
= PERF_COUNT_HW_CPU_CYCLES
},
1909 struct perf_event_attr frontend_attrs
[] = {
1910 { .type
= PERF_TYPE_HARDWARE
, .config
= PERF_COUNT_HW_STALLED_CYCLES_FRONTEND
},
1912 struct perf_event_attr backend_attrs
[] = {
1913 { .type
= PERF_TYPE_HARDWARE
, .config
= PERF_COUNT_HW_STALLED_CYCLES_BACKEND
},
1915 struct perf_event_attr default_attrs1
[] = {
1916 { .type
= PERF_TYPE_HARDWARE
, .config
= PERF_COUNT_HW_INSTRUCTIONS
},
1917 { .type
= PERF_TYPE_HARDWARE
, .config
= PERF_COUNT_HW_BRANCH_INSTRUCTIONS
},
1918 { .type
= PERF_TYPE_HARDWARE
, .config
= PERF_COUNT_HW_BRANCH_MISSES
},
1923 * Detailed stats (-d), covering the L1 and last level data caches:
1925 struct perf_event_attr detailed_attrs
[] = {
1927 { .type
= PERF_TYPE_HW_CACHE
,
1929 PERF_COUNT_HW_CACHE_L1D
<< 0 |
1930 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
1931 (PERF_COUNT_HW_CACHE_RESULT_ACCESS
<< 16) },
1933 { .type
= PERF_TYPE_HW_CACHE
,
1935 PERF_COUNT_HW_CACHE_L1D
<< 0 |
1936 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
1937 (PERF_COUNT_HW_CACHE_RESULT_MISS
<< 16) },
1939 { .type
= PERF_TYPE_HW_CACHE
,
1941 PERF_COUNT_HW_CACHE_LL
<< 0 |
1942 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
1943 (PERF_COUNT_HW_CACHE_RESULT_ACCESS
<< 16) },
1945 { .type
= PERF_TYPE_HW_CACHE
,
1947 PERF_COUNT_HW_CACHE_LL
<< 0 |
1948 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
1949 (PERF_COUNT_HW_CACHE_RESULT_MISS
<< 16) },
1953 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
1955 struct perf_event_attr very_detailed_attrs
[] = {
1957 { .type
= PERF_TYPE_HW_CACHE
,
1959 PERF_COUNT_HW_CACHE_L1I
<< 0 |
1960 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
1961 (PERF_COUNT_HW_CACHE_RESULT_ACCESS
<< 16) },
1963 { .type
= PERF_TYPE_HW_CACHE
,
1965 PERF_COUNT_HW_CACHE_L1I
<< 0 |
1966 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
1967 (PERF_COUNT_HW_CACHE_RESULT_MISS
<< 16) },
1969 { .type
= PERF_TYPE_HW_CACHE
,
1971 PERF_COUNT_HW_CACHE_DTLB
<< 0 |
1972 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
1973 (PERF_COUNT_HW_CACHE_RESULT_ACCESS
<< 16) },
1975 { .type
= PERF_TYPE_HW_CACHE
,
1977 PERF_COUNT_HW_CACHE_DTLB
<< 0 |
1978 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
1979 (PERF_COUNT_HW_CACHE_RESULT_MISS
<< 16) },
1981 { .type
= PERF_TYPE_HW_CACHE
,
1983 PERF_COUNT_HW_CACHE_ITLB
<< 0 |
1984 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
1985 (PERF_COUNT_HW_CACHE_RESULT_ACCESS
<< 16) },
1987 { .type
= PERF_TYPE_HW_CACHE
,
1989 PERF_COUNT_HW_CACHE_ITLB
<< 0 |
1990 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
1991 (PERF_COUNT_HW_CACHE_RESULT_MISS
<< 16) },
1996 * Very, very detailed stats (-d -d -d), adding prefetch events:
1998 struct perf_event_attr very_very_detailed_attrs
[] = {
2000 { .type
= PERF_TYPE_HW_CACHE
,
2002 PERF_COUNT_HW_CACHE_L1D
<< 0 |
2003 (PERF_COUNT_HW_CACHE_OP_PREFETCH
<< 8) |
2004 (PERF_COUNT_HW_CACHE_RESULT_ACCESS
<< 16) },
2006 { .type
= PERF_TYPE_HW_CACHE
,
2008 PERF_COUNT_HW_CACHE_L1D
<< 0 |
2009 (PERF_COUNT_HW_CACHE_OP_PREFETCH
<< 8) |
2010 (PERF_COUNT_HW_CACHE_RESULT_MISS
<< 16) },
2013 /* Set attrs if no event is selected and !null_run: */
2017 if (transaction_run
) {
2018 if (pmu_have_event("cpu", "cycles-ct") &&
2019 pmu_have_event("cpu", "el-start"))
2020 err
= parse_events(evsel_list
, transaction_attrs
, NULL
);
2022 err
= parse_events(evsel_list
, transaction_limited_attrs
, NULL
);
2024 fprintf(stderr
, "Cannot set up transaction events\n");
2034 if (stat_config
.aggr_mode
!= AGGR_GLOBAL
&&
2035 stat_config
.aggr_mode
!= AGGR_CORE
) {
2036 pr_err("top down event configuration requires --per-core mode\n");
2039 stat_config
.aggr_mode
= AGGR_CORE
;
2040 if (nr_cgroups
|| !target__has_cpu(&target
)) {
2041 pr_err("top down event configuration requires system-wide mode (-a)\n");
2045 if (!force_metric_only
)
2047 if (topdown_filter_events(topdown_attrs
, &str
,
2048 arch_topdown_check_group(&warn
)) < 0) {
2049 pr_err("Out of memory\n");
2052 if (topdown_attrs
[0] && str
) {
2054 arch_topdown_group_warn();
2055 err
= parse_events(evsel_list
, str
, NULL
);
2058 "Cannot set up top down events %s: %d\n",
2064 fprintf(stderr
, "System does not support topdown\n");
2070 if (!evsel_list
->nr_entries
) {
2071 if (target__has_cpu(&target
))
2072 default_attrs0
[0].config
= PERF_COUNT_SW_CPU_CLOCK
;
2074 if (perf_evlist__add_default_attrs(evsel_list
, default_attrs0
) < 0)
2076 if (pmu_have_event("cpu", "stalled-cycles-frontend")) {
2077 if (perf_evlist__add_default_attrs(evsel_list
,
2078 frontend_attrs
) < 0)
2081 if (pmu_have_event("cpu", "stalled-cycles-backend")) {
2082 if (perf_evlist__add_default_attrs(evsel_list
,
2086 if (perf_evlist__add_default_attrs(evsel_list
, default_attrs1
) < 0)
2090 /* Detailed events get appended to the event list: */
2092 if (detailed_run
< 1)
2095 /* Append detailed run extra attributes: */
2096 if (perf_evlist__add_default_attrs(evsel_list
, detailed_attrs
) < 0)
2099 if (detailed_run
< 2)
2102 /* Append very detailed run extra attributes: */
2103 if (perf_evlist__add_default_attrs(evsel_list
, very_detailed_attrs
) < 0)
2106 if (detailed_run
< 3)
2109 /* Append very, very detailed run extra attributes: */
2110 return perf_evlist__add_default_attrs(evsel_list
, very_very_detailed_attrs
);
2113 static const char * const stat_record_usage
[] = {
2114 "perf stat record [<options>]",
2118 static void init_features(struct perf_session
*session
)
2122 for (feat
= HEADER_FIRST_FEATURE
; feat
< HEADER_LAST_FEATURE
; feat
++)
2123 perf_header__set_feat(&session
->header
, feat
);
2125 perf_header__clear_feat(&session
->header
, HEADER_BUILD_ID
);
2126 perf_header__clear_feat(&session
->header
, HEADER_TRACING_DATA
);
2127 perf_header__clear_feat(&session
->header
, HEADER_BRANCH_STACK
);
2128 perf_header__clear_feat(&session
->header
, HEADER_AUXTRACE
);
2131 static int __cmd_record(int argc
, const char **argv
)
2133 struct perf_session
*session
;
2134 struct perf_data_file
*file
= &perf_stat
.file
;
2136 argc
= parse_options(argc
, argv
, stat_options
, stat_record_usage
,
2137 PARSE_OPT_STOP_AT_NON_OPTION
);
2140 file
->path
= output_name
;
2142 if (run_count
!= 1 || forever
) {
2143 pr_err("Cannot use -r option with perf stat record.\n");
2147 session
= perf_session__new(file
, false, NULL
);
2148 if (session
== NULL
) {
2149 pr_err("Perf session creation failed.\n");
2153 init_features(session
);
2155 session
->evlist
= evsel_list
;
2156 perf_stat
.session
= session
;
2157 perf_stat
.record
= true;
2161 static int process_stat_round_event(struct perf_tool
*tool __maybe_unused
,
2162 union perf_event
*event
,
2163 struct perf_session
*session
)
2165 struct stat_round_event
*stat_round
= &event
->stat_round
;
2166 struct perf_evsel
*counter
;
2167 struct timespec tsh
, *ts
= NULL
;
2168 const char **argv
= session
->header
.env
.cmdline_argv
;
2169 int argc
= session
->header
.env
.nr_cmdline
;
2171 evlist__for_each_entry(evsel_list
, counter
)
2172 perf_stat_process_counter(&stat_config
, counter
);
2174 if (stat_round
->type
== PERF_STAT_ROUND_TYPE__FINAL
)
2175 update_stats(&walltime_nsecs_stats
, stat_round
->time
);
2177 if (stat_config
.interval
&& stat_round
->time
) {
2178 tsh
.tv_sec
= stat_round
->time
/ NSECS_PER_SEC
;
2179 tsh
.tv_nsec
= stat_round
->time
% NSECS_PER_SEC
;
2183 print_counters(ts
, argc
, argv
);
2188 int process_stat_config_event(struct perf_tool
*tool __maybe_unused
,
2189 union perf_event
*event
,
2190 struct perf_session
*session __maybe_unused
)
2192 struct perf_stat
*st
= container_of(tool
, struct perf_stat
, tool
);
2194 perf_event__read_stat_config(&stat_config
, &event
->stat_config
);
2196 if (cpu_map__empty(st
->cpus
)) {
2197 if (st
->aggr_mode
!= AGGR_UNSET
)
2198 pr_warning("warning: processing task data, aggregation mode not set\n");
2202 if (st
->aggr_mode
!= AGGR_UNSET
)
2203 stat_config
.aggr_mode
= st
->aggr_mode
;
2205 if (perf_stat
.file
.is_pipe
)
2206 perf_stat_init_aggr_mode();
2208 perf_stat_init_aggr_mode_file(st
);
2213 static int set_maps(struct perf_stat
*st
)
2215 if (!st
->cpus
|| !st
->threads
)
2218 if (WARN_ONCE(st
->maps_allocated
, "stats double allocation\n"))
2221 perf_evlist__set_maps(evsel_list
, st
->cpus
, st
->threads
);
2223 if (perf_evlist__alloc_stats(evsel_list
, true))
2226 st
->maps_allocated
= true;
2231 int process_thread_map_event(struct perf_tool
*tool __maybe_unused
,
2232 union perf_event
*event
,
2233 struct perf_session
*session __maybe_unused
)
2235 struct perf_stat
*st
= container_of(tool
, struct perf_stat
, tool
);
2238 pr_warning("Extra thread map event, ignoring.\n");
2242 st
->threads
= thread_map__new_event(&event
->thread_map
);
2246 return set_maps(st
);
2250 int process_cpu_map_event(struct perf_tool
*tool __maybe_unused
,
2251 union perf_event
*event
,
2252 struct perf_session
*session __maybe_unused
)
2254 struct perf_stat
*st
= container_of(tool
, struct perf_stat
, tool
);
2255 struct cpu_map
*cpus
;
2258 pr_warning("Extra cpu map event, ignoring.\n");
2262 cpus
= cpu_map__new_data(&event
->cpu_map
.data
);
2267 return set_maps(st
);
2270 static const char * const stat_report_usage
[] = {
2271 "perf stat report [<options>]",
2275 static struct perf_stat perf_stat
= {
2277 .attr
= perf_event__process_attr
,
2278 .event_update
= perf_event__process_event_update
,
2279 .thread_map
= process_thread_map_event
,
2280 .cpu_map
= process_cpu_map_event
,
2281 .stat_config
= process_stat_config_event
,
2282 .stat
= perf_event__process_stat_event
,
2283 .stat_round
= process_stat_round_event
,
2285 .aggr_mode
= AGGR_UNSET
,
2288 static int __cmd_report(int argc
, const char **argv
)
2290 struct perf_session
*session
;
2291 const struct option options
[] = {
2292 OPT_STRING('i', "input", &input_name
, "file", "input file name"),
2293 OPT_SET_UINT(0, "per-socket", &perf_stat
.aggr_mode
,
2294 "aggregate counts per processor socket", AGGR_SOCKET
),
2295 OPT_SET_UINT(0, "per-core", &perf_stat
.aggr_mode
,
2296 "aggregate counts per physical processor core", AGGR_CORE
),
2297 OPT_SET_UINT('A', "no-aggr", &perf_stat
.aggr_mode
,
2298 "disable CPU count aggregation", AGGR_NONE
),
2304 argc
= parse_options(argc
, argv
, options
, stat_report_usage
, 0);
2306 if (!input_name
|| !strlen(input_name
)) {
2307 if (!fstat(STDIN_FILENO
, &st
) && S_ISFIFO(st
.st_mode
))
2310 input_name
= "perf.data";
2313 perf_stat
.file
.path
= input_name
;
2314 perf_stat
.file
.mode
= PERF_DATA_MODE_READ
;
2316 session
= perf_session__new(&perf_stat
.file
, false, &perf_stat
.tool
);
2317 if (session
== NULL
)
2320 perf_stat
.session
= session
;
2321 stat_config
.output
= stderr
;
2322 evsel_list
= session
->evlist
;
2324 ret
= perf_session__process_events(session
);
2328 perf_session__delete(session
);
2332 int cmd_stat(int argc
, const char **argv
, const char *prefix __maybe_unused
)
2334 const char * const stat_usage
[] = {
2335 "perf stat [<options>] [<command>]",
2338 int status
= -EINVAL
, run_idx
;
2340 FILE *output
= stderr
;
2341 unsigned int interval
;
2342 const char * const stat_subcommands
[] = { "record", "report" };
2344 setlocale(LC_ALL
, "");
2346 evsel_list
= perf_evlist__new();
2347 if (evsel_list
== NULL
)
2350 parse_events__shrink_config_terms();
2351 argc
= parse_options_subcommand(argc
, argv
, stat_options
, stat_subcommands
,
2352 (const char **) stat_usage
,
2353 PARSE_OPT_STOP_AT_NON_OPTION
);
2354 perf_stat__init_shadow_stats();
2358 if (!strcmp(csv_sep
, "\\t"))
2361 csv_sep
= DEFAULT_SEPARATOR
;
2363 if (argc
&& !strncmp(argv
[0], "rec", 3)) {
2364 argc
= __cmd_record(argc
, argv
);
2367 } else if (argc
&& !strncmp(argv
[0], "rep", 3))
2368 return __cmd_report(argc
, argv
);
2370 interval
= stat_config
.interval
;
2373 * For record command the -o is already taken care of.
2375 if (!STAT_RECORD
&& output_name
&& strcmp(output_name
, "-"))
2378 if (output_name
&& output_fd
) {
2379 fprintf(stderr
, "cannot use both --output and --log-fd\n");
2380 parse_options_usage(stat_usage
, stat_options
, "o", 1);
2381 parse_options_usage(NULL
, stat_options
, "log-fd", 0);
2385 if (metric_only
&& stat_config
.aggr_mode
== AGGR_THREAD
) {
2386 fprintf(stderr
, "--metric-only is not supported with --per-thread\n");
2390 if (metric_only
&& run_count
> 1) {
2391 fprintf(stderr
, "--metric-only is not supported with -r\n");
2395 if (output_fd
< 0) {
2396 fprintf(stderr
, "argument to --log-fd must be a > 0\n");
2397 parse_options_usage(stat_usage
, stat_options
, "log-fd", 0);
2403 mode
= append_file
? "a" : "w";
2405 output
= fopen(output_name
, mode
);
2407 perror("failed to create output file");
2410 clock_gettime(CLOCK_REALTIME
, &tm
);
2411 fprintf(output
, "# started on %s\n", ctime(&tm
.tv_sec
));
2412 } else if (output_fd
> 0) {
2413 mode
= append_file
? "a" : "w";
2414 output
= fdopen(output_fd
, mode
);
2416 perror("Failed opening logfd");
2421 stat_config
.output
= output
;
2424 * let the spreadsheet do the pretty-printing
2427 /* User explicitly passed -B? */
2428 if (big_num_opt
== 1) {
2429 fprintf(stderr
, "-B option not supported with -x\n");
2430 parse_options_usage(stat_usage
, stat_options
, "B", 1);
2431 parse_options_usage(NULL
, stat_options
, "x", 1);
2433 } else /* Nope, so disable big number formatting */
2435 } else if (big_num_opt
== 0) /* User passed --no-big-num */
2438 if (!argc
&& target__none(&target
))
2439 usage_with_options(stat_usage
, stat_options
);
2441 if (run_count
< 0) {
2442 pr_err("Run count must be a positive number\n");
2443 parse_options_usage(stat_usage
, stat_options
, "r", 1);
2445 } else if (run_count
== 0) {
2450 if ((stat_config
.aggr_mode
== AGGR_THREAD
) && !target__has_task(&target
)) {
2451 fprintf(stderr
, "The --per-thread option is only available "
2452 "when monitoring via -p -t options.\n");
2453 parse_options_usage(NULL
, stat_options
, "p", 1);
2454 parse_options_usage(NULL
, stat_options
, "t", 1);
2459 * no_aggr, cgroup are for system-wide only
2460 * --per-thread is aggregated per thread, we dont mix it with cpu mode
2462 if (((stat_config
.aggr_mode
!= AGGR_GLOBAL
&&
2463 stat_config
.aggr_mode
!= AGGR_THREAD
) || nr_cgroups
) &&
2464 !target__has_cpu(&target
)) {
2465 fprintf(stderr
, "both cgroup and no-aggregation "
2466 "modes only available in system-wide mode\n");
2468 parse_options_usage(stat_usage
, stat_options
, "G", 1);
2469 parse_options_usage(NULL
, stat_options
, "A", 1);
2470 parse_options_usage(NULL
, stat_options
, "a", 1);
2474 if (add_default_attributes())
2477 target__validate(&target
);
2479 if (perf_evlist__create_maps(evsel_list
, &target
) < 0) {
2480 if (target__has_task(&target
)) {
2481 pr_err("Problems finding threads of monitor\n");
2482 parse_options_usage(stat_usage
, stat_options
, "p", 1);
2483 parse_options_usage(NULL
, stat_options
, "t", 1);
2484 } else if (target__has_cpu(&target
)) {
2485 perror("failed to parse CPUs map");
2486 parse_options_usage(stat_usage
, stat_options
, "C", 1);
2487 parse_options_usage(NULL
, stat_options
, "a", 1);
2493 * Initialize thread_map with comm names,
2494 * so we could print it out on output.
2496 if (stat_config
.aggr_mode
== AGGR_THREAD
)
2497 thread_map__read_comms(evsel_list
->threads
);
2499 if (interval
&& interval
< 100) {
2500 if (interval
< 10) {
2501 pr_err("print interval must be >= 10ms\n");
2502 parse_options_usage(stat_usage
, stat_options
, "I", 1);
2505 pr_warning("print interval < 100ms. "
2506 "The overhead percentage could be high in some cases. "
2507 "Please proceed with caution.\n");
2510 if (perf_evlist__alloc_stats(evsel_list
, interval
))
2513 if (perf_stat_init_aggr_mode())
2517 * We dont want to block the signals - that would cause
2518 * child tasks to inherit that and Ctrl-C would not work.
2519 * What we want is for Ctrl-C to work in the exec()-ed
2520 * task, but being ignored by perf stat itself:
2524 signal(SIGINT
, skip_signal
);
2525 signal(SIGCHLD
, skip_signal
);
2526 signal(SIGALRM
, skip_signal
);
2527 signal(SIGABRT
, skip_signal
);
2530 for (run_idx
= 0; forever
|| run_idx
< run_count
; run_idx
++) {
2531 if (run_count
!= 1 && verbose
)
2532 fprintf(output
, "[ perf stat: executing run #%d ... ]\n",
2535 status
= run_perf_stat(argc
, argv
);
2536 if (forever
&& status
!= -1) {
2537 print_counters(NULL
, argc
, argv
);
2538 perf_stat__reset_stats();
2542 if (!forever
&& status
!= -1 && !interval
)
2543 print_counters(NULL
, argc
, argv
);
2547 * We synthesize the kernel mmap record just so that older tools
2548 * don't emit warnings about not being able to resolve symbols
2549 * due to /proc/sys/kernel/kptr_restrict settings and instear provide
2550 * a saner message about no samples being in the perf.data file.
2552 * This also serves to suppress a warning about f_header.data.size == 0
2553 * in header.c at the moment 'perf stat record' gets introduced, which
2554 * is not really needed once we start adding the stat specific PERF_RECORD_
2555 * records, but the need to suppress the kptr_restrict messages in older
2556 * tools remain -acme
2558 int fd
= perf_data_file__fd(&perf_stat
.file
);
2559 int err
= perf_event__synthesize_kernel_mmap((void *)&perf_stat
,
2560 process_synthesized_event
,
2561 &perf_stat
.session
->machines
.host
);
2563 pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
2564 "older tools may produce warnings about this file\n.");
2568 if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats
.max
, FINAL
))
2569 pr_err("failed to write stat round event\n");
2572 if (!perf_stat
.file
.is_pipe
) {
2573 perf_stat
.session
->header
.data_size
+= perf_stat
.bytes_written
;
2574 perf_session__write_header(perf_stat
.session
, evsel_list
, fd
, true);
2577 perf_session__delete(perf_stat
.session
);
2580 perf_stat__exit_aggr_mode();
2581 perf_evlist__free_stats(evsel_list
);
2583 perf_evlist__delete(evsel_list
);