4 * Builtin stat command: Give a precise performance counters summary
5 * overview about any workload, CPU or specific PID.
9 $ perf stat ./hackbench 10
13 Performance counter stats for './hackbench 10':
15 1708.761321 task-clock # 11.037 CPUs utilized
16 41,190 context-switches # 0.024 M/sec
17 6,735 CPU-migrations # 0.004 M/sec
18 17,318 page-faults # 0.010 M/sec
19 5,205,202,243 cycles # 3.046 GHz
20 3,856,436,920 stalled-cycles-frontend # 74.09% frontend cycles idle
21 1,600,790,871 stalled-cycles-backend # 30.75% backend cycles idle
22 2,603,501,247 instructions # 0.50 insns per cycle
23 # 1.48 stalled cycles per insn
24 484,357,498 branches # 283.455 M/sec
25 6,388,934 branch-misses # 1.32% of all branches
27 0.154822978 seconds time elapsed
30 * Copyright (C) 2008-2011, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
32 * Improvements and fixes by:
34 * Arjan van de Ven <arjan@linux.intel.com>
35 * Yanmin Zhang <yanmin.zhang@intel.com>
36 * Wu Fengguang <fengguang.wu@intel.com>
37 * Mike Galbraith <efault@gmx.de>
38 * Paul Mackerras <paulus@samba.org>
39 * Jaswinder Singh Rajput <jaswinder@kernel.org>
41 * Released under the GPL v2. (and only v2, not any later version)
46 #include "util/cgroup.h"
47 #include "util/util.h"
48 #include <subcmd/parse-options.h>
49 #include "util/parse-events.h"
51 #include "util/event.h"
52 #include "util/evlist.h"
53 #include "util/evsel.h"
54 #include "util/debug.h"
55 #include "util/drv_configs.h"
56 #include "util/color.h"
57 #include "util/stat.h"
58 #include "util/header.h"
59 #include "util/cpumap.h"
60 #include "util/thread.h"
61 #include "util/thread_map.h"
62 #include "util/counts.h"
63 #include "util/group.h"
64 #include "util/session.h"
65 #include "util/tool.h"
66 #include "util/group.h"
67 #include "util/string2.h"
70 #include <linux/time64.h>
71 #include <api/fs/fs.h>
75 #include <sys/prctl.h>
79 #include <sys/types.h>
84 #include "sane_ctype.h"
86 #define DEFAULT_SEPARATOR " "
87 #define CNTR_NOT_SUPPORTED "<not supported>"
88 #define CNTR_NOT_COUNTED "<not counted>"
89 #define FREEZE_ON_SMI_PATH "devices/cpu/freeze_on_smi"
91 static void print_counters(struct timespec
*ts
, int argc
, const char **argv
);
93 /* Default events used for perf stat -T */
94 static const char *transaction_attrs
= {
106 /* More limited version when the CPU does not have all events. */
107 static const char * transaction_limited_attrs
= {
117 static const char * topdown_attrs
[] = {
118 "topdown-total-slots",
119 "topdown-slots-retired",
120 "topdown-recovery-bubbles",
121 "topdown-fetch-bubbles",
122 "topdown-slots-issued",
126 static const char *smi_cost_attrs
= {
134 static struct perf_evlist
*evsel_list
;
136 static struct target target
= {
140 typedef int (*aggr_get_id_t
)(struct cpu_map
*m
, int cpu
);
142 static int run_count
= 1;
143 static bool no_inherit
= false;
144 static volatile pid_t child_pid
= -1;
145 static bool null_run
= false;
146 static int detailed_run
= 0;
147 static bool transaction_run
;
148 static bool topdown_run
= false;
149 static bool smi_cost
= false;
150 static bool smi_reset
= false;
151 static bool big_num
= true;
152 static int big_num_opt
= -1;
153 static const char *csv_sep
= NULL
;
154 static bool csv_output
= false;
155 static bool group
= false;
156 static const char *pre_cmd
= NULL
;
157 static const char *post_cmd
= NULL
;
158 static bool sync_run
= false;
159 static unsigned int initial_delay
= 0;
160 static unsigned int unit_width
= 4; /* strlen("unit") */
161 static bool forever
= false;
162 static bool metric_only
= false;
163 static bool force_metric_only
= false;
164 static bool no_merge
= false;
165 static struct timespec ref_time
;
166 static struct cpu_map
*aggr_map
;
167 static aggr_get_id_t aggr_get_id
;
168 static bool append_file
;
169 static const char *output_name
;
170 static int output_fd
;
171 static int print_free_counters_hint
;
175 struct perf_data_file file
;
176 struct perf_session
*session
;
178 struct perf_tool tool
;
180 struct cpu_map
*cpus
;
181 struct thread_map
*threads
;
182 enum aggr_mode aggr_mode
;
185 static struct perf_stat perf_stat
;
186 #define STAT_RECORD perf_stat.record
188 static volatile int done
= 0;
190 static struct perf_stat_config stat_config
= {
191 .aggr_mode
= AGGR_GLOBAL
,
195 static inline void diff_timespec(struct timespec
*r
, struct timespec
*a
,
198 r
->tv_sec
= a
->tv_sec
- b
->tv_sec
;
199 if (a
->tv_nsec
< b
->tv_nsec
) {
200 r
->tv_nsec
= a
->tv_nsec
+ NSEC_PER_SEC
- b
->tv_nsec
;
203 r
->tv_nsec
= a
->tv_nsec
- b
->tv_nsec
;
207 static void perf_stat__reset_stats(void)
209 perf_evlist__reset_stats(evsel_list
);
210 perf_stat__reset_shadow_stats();
213 static int create_perf_stat_counter(struct perf_evsel
*evsel
)
215 struct perf_event_attr
*attr
= &evsel
->attr
;
216 struct perf_evsel
*leader
= evsel
->leader
;
218 if (stat_config
.scale
) {
219 attr
->read_format
= PERF_FORMAT_TOTAL_TIME_ENABLED
|
220 PERF_FORMAT_TOTAL_TIME_RUNNING
;
224 * The event is part of non trivial group, let's enable
225 * the group read (for leader) and ID retrieval for all
228 if (leader
->nr_members
> 1)
229 attr
->read_format
|= PERF_FORMAT_ID
|PERF_FORMAT_GROUP
;
231 attr
->inherit
= !no_inherit
;
234 * Some events get initialized with sample_(period/type) set,
235 * like tracepoints. Clear it up for counting.
237 attr
->sample_period
= 0;
240 * But set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
241 * while avoiding that older tools show confusing messages.
243 * However for pipe sessions we need to keep it zero,
244 * because script's perf_evsel__check_attr is triggered
245 * by attr->sample_type != 0, and we can't run it on
248 if (!(STAT_RECORD
&& perf_stat
.file
.is_pipe
))
249 attr
->sample_type
= PERF_SAMPLE_IDENTIFIER
;
252 * Disabling all counters initially, they will be enabled
253 * either manually by us or by kernel via enable_on_exec
256 if (perf_evsel__is_group_leader(evsel
)) {
260 * In case of initial_delay we enable tracee
263 if (target__none(&target
) && !initial_delay
)
264 attr
->enable_on_exec
= 1;
267 if (target__has_cpu(&target
))
268 return perf_evsel__open_per_cpu(evsel
, perf_evsel__cpus(evsel
));
270 return perf_evsel__open_per_thread(evsel
, evsel_list
->threads
);
274 * Does the counter have nsecs as a unit?
276 static inline int nsec_counter(struct perf_evsel
*evsel
)
278 if (perf_evsel__match(evsel
, SOFTWARE
, SW_CPU_CLOCK
) ||
279 perf_evsel__match(evsel
, SOFTWARE
, SW_TASK_CLOCK
))
285 static int process_synthesized_event(struct perf_tool
*tool __maybe_unused
,
286 union perf_event
*event
,
287 struct perf_sample
*sample __maybe_unused
,
288 struct machine
*machine __maybe_unused
)
290 if (perf_data_file__write(&perf_stat
.file
, event
, event
->header
.size
) < 0) {
291 pr_err("failed to write perf data, error: %m\n");
295 perf_stat
.bytes_written
+= event
->header
.size
;
299 static int write_stat_round_event(u64 tm
, u64 type
)
301 return perf_event__synthesize_stat_round(NULL
, tm
, type
,
302 process_synthesized_event
,
306 #define WRITE_STAT_ROUND_EVENT(time, interval) \
307 write_stat_round_event(time, PERF_STAT_ROUND_TYPE__ ## interval)
309 #define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
312 perf_evsel__write_stat_event(struct perf_evsel
*counter
, u32 cpu
, u32 thread
,
313 struct perf_counts_values
*count
)
315 struct perf_sample_id
*sid
= SID(counter
, cpu
, thread
);
317 return perf_event__synthesize_stat(NULL
, cpu
, thread
, sid
->id
, count
,
318 process_synthesized_event
, NULL
);
322 * Read out the results of a single counter:
323 * do not aggregate counts across CPUs in system-wide mode
325 static int read_counter(struct perf_evsel
*counter
)
327 int nthreads
= thread_map__nr(evsel_list
->threads
);
328 int ncpus
, cpu
, thread
;
330 if (target__has_cpu(&target
))
331 ncpus
= perf_evsel__nr_cpus(counter
);
335 if (!counter
->supported
)
338 if (counter
->system_wide
)
341 for (thread
= 0; thread
< nthreads
; thread
++) {
342 for (cpu
= 0; cpu
< ncpus
; cpu
++) {
343 struct perf_counts_values
*count
;
345 count
= perf_counts(counter
->counts
, cpu
, thread
);
348 * The leader's group read loads data into its group members
349 * (via perf_evsel__read_counter) and sets threir count->loaded.
351 if (!count
->loaded
&&
352 perf_evsel__read_counter(counter
, cpu
, thread
)) {
353 counter
->counts
->scaled
= -1;
354 perf_counts(counter
->counts
, cpu
, thread
)->ena
= 0;
355 perf_counts(counter
->counts
, cpu
, thread
)->run
= 0;
359 count
->loaded
= false;
362 if (perf_evsel__write_stat_event(counter
, cpu
, thread
, count
)) {
363 pr_err("failed to write stat event\n");
369 fprintf(stat_config
.output
,
370 "%s: %d: %" PRIu64
" %" PRIu64
" %" PRIu64
"\n",
371 perf_evsel__name(counter
),
373 count
->val
, count
->ena
, count
->run
);
381 static void read_counters(void)
383 struct perf_evsel
*counter
;
386 evlist__for_each_entry(evsel_list
, counter
) {
387 ret
= read_counter(counter
);
389 pr_debug("failed to read counter %s\n", counter
->name
);
391 if (ret
== 0 && perf_stat_process_counter(&stat_config
, counter
))
392 pr_warning("failed to process counter %s\n", counter
->name
);
396 static void process_interval(void)
398 struct timespec ts
, rs
;
402 clock_gettime(CLOCK_MONOTONIC
, &ts
);
403 diff_timespec(&rs
, &ts
, &ref_time
);
406 if (WRITE_STAT_ROUND_EVENT(rs
.tv_sec
* NSEC_PER_SEC
+ rs
.tv_nsec
, INTERVAL
))
407 pr_err("failed to write stat round event\n");
410 print_counters(&rs
, 0, NULL
);
413 static void enable_counters(void)
416 usleep(initial_delay
* USEC_PER_MSEC
);
419 * We need to enable counters only if:
420 * - we don't have tracee (attaching to task or cpu)
421 * - we have initial delay configured
423 if (!target__none(&target
) || initial_delay
)
424 perf_evlist__enable(evsel_list
);
427 static void disable_counters(void)
430 * If we don't have tracee (attaching to task or cpu), counters may
431 * still be running. To get accurate group ratios, we must stop groups
432 * from counting before reading their constituent counters.
434 if (!target__none(&target
))
435 perf_evlist__disable(evsel_list
);
438 static volatile int workload_exec_errno
;
441 * perf_evlist__prepare_workload will send a SIGUSR1
442 * if the fork fails, since we asked by setting its
443 * want_signal to true.
445 static void workload_exec_failed_signal(int signo __maybe_unused
, siginfo_t
*info
,
446 void *ucontext __maybe_unused
)
448 workload_exec_errno
= info
->si_value
.sival_int
;
451 static bool has_unit(struct perf_evsel
*counter
)
453 return counter
->unit
&& *counter
->unit
;
456 static bool has_scale(struct perf_evsel
*counter
)
458 return counter
->scale
!= 1;
461 static int perf_stat_synthesize_config(bool is_pipe
)
463 struct perf_evsel
*counter
;
467 err
= perf_event__synthesize_attrs(NULL
, perf_stat
.session
,
468 process_synthesized_event
);
470 pr_err("Couldn't synthesize attrs.\n");
476 * Synthesize other events stuff not carried within
477 * attr event - unit, scale, name
479 evlist__for_each_entry(evsel_list
, counter
) {
480 if (!counter
->supported
)
484 * Synthesize unit and scale only if it's defined.
486 if (has_unit(counter
)) {
487 err
= perf_event__synthesize_event_update_unit(NULL
, counter
, process_synthesized_event
);
489 pr_err("Couldn't synthesize evsel unit.\n");
494 if (has_scale(counter
)) {
495 err
= perf_event__synthesize_event_update_scale(NULL
, counter
, process_synthesized_event
);
497 pr_err("Couldn't synthesize evsel scale.\n");
502 if (counter
->own_cpus
) {
503 err
= perf_event__synthesize_event_update_cpus(NULL
, counter
, process_synthesized_event
);
505 pr_err("Couldn't synthesize evsel scale.\n");
511 * Name is needed only for pipe output,
512 * perf.data carries event names.
515 err
= perf_event__synthesize_event_update_name(NULL
, counter
, process_synthesized_event
);
517 pr_err("Couldn't synthesize evsel name.\n");
523 err
= perf_event__synthesize_thread_map2(NULL
, evsel_list
->threads
,
524 process_synthesized_event
,
527 pr_err("Couldn't synthesize thread map.\n");
531 err
= perf_event__synthesize_cpu_map(NULL
, evsel_list
->cpus
,
532 process_synthesized_event
, NULL
);
534 pr_err("Couldn't synthesize thread map.\n");
538 err
= perf_event__synthesize_stat_config(NULL
, &stat_config
,
539 process_synthesized_event
, NULL
);
541 pr_err("Couldn't synthesize config.\n");
548 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
550 static int __store_counter_ids(struct perf_evsel
*counter
,
551 struct cpu_map
*cpus
,
552 struct thread_map
*threads
)
556 for (cpu
= 0; cpu
< cpus
->nr
; cpu
++) {
557 for (thread
= 0; thread
< threads
->nr
; thread
++) {
558 int fd
= FD(counter
, cpu
, thread
);
560 if (perf_evlist__id_add_fd(evsel_list
, counter
,
561 cpu
, thread
, fd
) < 0)
569 static int store_counter_ids(struct perf_evsel
*counter
)
571 struct cpu_map
*cpus
= counter
->cpus
;
572 struct thread_map
*threads
= counter
->threads
;
574 if (perf_evsel__alloc_id(counter
, cpus
->nr
, threads
->nr
))
577 return __store_counter_ids(counter
, cpus
, threads
);
580 static bool perf_evsel__should_store_id(struct perf_evsel
*counter
)
582 return STAT_RECORD
|| counter
->attr
.read_format
& PERF_FORMAT_ID
;
585 static int __run_perf_stat(int argc
, const char **argv
)
587 int interval
= stat_config
.interval
;
589 unsigned long long t0
, t1
;
590 struct perf_evsel
*counter
;
594 const bool forks
= (argc
> 0);
595 bool is_pipe
= STAT_RECORD
? perf_stat
.file
.is_pipe
: false;
596 struct perf_evsel_config_term
*err_term
;
599 ts
.tv_sec
= interval
/ USEC_PER_MSEC
;
600 ts
.tv_nsec
= (interval
% USEC_PER_MSEC
) * NSEC_PER_MSEC
;
607 if (perf_evlist__prepare_workload(evsel_list
, &target
, argv
, is_pipe
,
608 workload_exec_failed_signal
) < 0) {
609 perror("failed to prepare workload");
612 child_pid
= evsel_list
->workload
.pid
;
616 perf_evlist__set_leader(evsel_list
);
618 evlist__for_each_entry(evsel_list
, counter
) {
620 if (create_perf_stat_counter(counter
) < 0) {
622 * PPC returns ENXIO for HW counters until 2.6.37
623 * (behavior changed with commit b0a873e).
625 if (errno
== EINVAL
|| errno
== ENOSYS
||
626 errno
== ENOENT
|| errno
== EOPNOTSUPP
||
629 ui__warning("%s event is not supported by the kernel.\n",
630 perf_evsel__name(counter
));
631 counter
->supported
= false;
633 if ((counter
->leader
!= counter
) ||
634 !(counter
->leader
->nr_members
> 1))
636 } else if (perf_evsel__fallback(counter
, errno
, msg
, sizeof(msg
))) {
638 ui__warning("%s\n", msg
);
642 perf_evsel__open_strerror(counter
, &target
,
643 errno
, msg
, sizeof(msg
));
644 ui__error("%s\n", msg
);
647 kill(child_pid
, SIGTERM
);
651 counter
->supported
= true;
653 l
= strlen(counter
->unit
);
657 if (perf_evsel__should_store_id(counter
) &&
658 store_counter_ids(counter
))
662 if (perf_evlist__apply_filters(evsel_list
, &counter
)) {
663 pr_err("failed to set filter \"%s\" on event %s with %d (%s)\n",
664 counter
->filter
, perf_evsel__name(counter
), errno
,
665 str_error_r(errno
, msg
, sizeof(msg
)));
669 if (perf_evlist__apply_drv_configs(evsel_list
, &counter
, &err_term
)) {
670 pr_err("failed to set config \"%s\" on event %s with %d (%s)\n",
671 err_term
->val
.drv_cfg
, perf_evsel__name(counter
), errno
,
672 str_error_r(errno
, msg
, sizeof(msg
)));
677 int err
, fd
= perf_data_file__fd(&perf_stat
.file
);
680 err
= perf_header__write_pipe(perf_data_file__fd(&perf_stat
.file
));
682 err
= perf_session__write_header(perf_stat
.session
, evsel_list
,
689 err
= perf_stat_synthesize_config(is_pipe
);
695 * Enable counters and exec the command:
698 clock_gettime(CLOCK_MONOTONIC
, &ref_time
);
701 perf_evlist__start_workload(evsel_list
);
705 while (!waitpid(child_pid
, &status
, WNOHANG
)) {
706 nanosleep(&ts
, NULL
);
712 if (workload_exec_errno
) {
713 const char *emsg
= str_error_r(workload_exec_errno
, msg
, sizeof(msg
));
714 pr_err("Workload failed: %s\n", emsg
);
718 if (WIFSIGNALED(status
))
719 psignal(WTERMSIG(status
), argv
[0]);
723 nanosleep(&ts
, NULL
);
733 update_stats(&walltime_nsecs_stats
, t1
- t0
);
736 * Closing a group leader splits the group, and as we only disable
737 * group leaders, results in remaining events becoming enabled. To
738 * avoid arbitrary skew, we must read all counters before closing any
742 perf_evlist__close(evsel_list
);
744 return WEXITSTATUS(status
);
747 static int run_perf_stat(int argc
, const char **argv
)
752 ret
= system(pre_cmd
);
760 ret
= __run_perf_stat(argc
, argv
);
765 ret
= system(post_cmd
);
773 static void print_running(u64 run
, u64 ena
)
776 fprintf(stat_config
.output
, "%s%" PRIu64
"%s%.2f",
780 ena
? 100.0 * run
/ ena
: 100.0);
781 } else if (run
!= ena
) {
782 fprintf(stat_config
.output
, " (%.2f%%)", 100.0 * run
/ ena
);
786 static void print_noise_pct(double total
, double avg
)
788 double pct
= rel_stddev_stats(total
, avg
);
791 fprintf(stat_config
.output
, "%s%.2f%%", csv_sep
, pct
);
793 fprintf(stat_config
.output
, " ( +-%6.2f%% )", pct
);
796 static void print_noise(struct perf_evsel
*evsel
, double avg
)
798 struct perf_stat_evsel
*ps
;
804 print_noise_pct(stddev_stats(&ps
->res_stats
[0]), avg
);
807 static void aggr_printout(struct perf_evsel
*evsel
, int id
, int nr
)
809 switch (stat_config
.aggr_mode
) {
811 fprintf(stat_config
.output
, "S%d-C%*d%s%*d%s",
812 cpu_map__id_to_socket(id
),
814 cpu_map__id_to_cpu(id
),
821 fprintf(stat_config
.output
, "S%*d%s%*d%s",
830 fprintf(stat_config
.output
, "CPU%*d%s",
832 perf_evsel__cpus(evsel
)->map
[id
], csv_sep
);
835 fprintf(stat_config
.output
, "%*s-%*d%s",
837 thread_map__comm(evsel
->threads
, id
),
839 thread_map__pid(evsel
->threads
, id
),
855 struct perf_evsel
*evsel
;
858 #define METRIC_LEN 35
860 static void new_line_std(void *ctx
)
862 struct outstate
*os
= ctx
;
867 static void do_new_line_std(struct outstate
*os
)
870 fputs(os
->prefix
, os
->fh
);
871 aggr_printout(os
->evsel
, os
->id
, os
->nr
);
872 if (stat_config
.aggr_mode
== AGGR_NONE
)
873 fprintf(os
->fh
, " ");
874 fprintf(os
->fh
, " ");
877 static void print_metric_std(void *ctx
, const char *color
, const char *fmt
,
878 const char *unit
, double val
)
880 struct outstate
*os
= ctx
;
883 bool newline
= os
->newline
;
887 if (unit
== NULL
|| fmt
== NULL
) {
888 fprintf(out
, "%-*s", METRIC_LEN
, "");
895 n
= fprintf(out
, " # ");
897 n
+= color_fprintf(out
, color
, fmt
, val
);
899 n
+= fprintf(out
, fmt
, val
);
900 fprintf(out
, " %-*s", METRIC_LEN
- n
- 1, unit
);
903 static void new_line_csv(void *ctx
)
905 struct outstate
*os
= ctx
;
910 fprintf(os
->fh
, "%s%s", os
->prefix
, csv_sep
);
911 aggr_printout(os
->evsel
, os
->id
, os
->nr
);
912 for (i
= 0; i
< os
->nfields
; i
++)
913 fputs(csv_sep
, os
->fh
);
916 static void print_metric_csv(void *ctx
,
917 const char *color __maybe_unused
,
918 const char *fmt
, const char *unit
, double val
)
920 struct outstate
*os
= ctx
;
922 char buf
[64], *vals
, *ends
;
924 if (unit
== NULL
|| fmt
== NULL
) {
925 fprintf(out
, "%s%s%s%s", csv_sep
, csv_sep
, csv_sep
, csv_sep
);
928 snprintf(buf
, sizeof(buf
), fmt
, val
);
929 ends
= vals
= ltrim(buf
);
930 while (isdigit(*ends
) || *ends
== '.')
933 while (isspace(*unit
))
935 fprintf(out
, "%s%s%s%s", csv_sep
, vals
, csv_sep
, unit
);
938 #define METRIC_ONLY_LEN 20
940 /* Filter out some columns that don't work well in metrics only mode */
942 static bool valid_only_metric(const char *unit
)
946 if (strstr(unit
, "/sec") ||
947 strstr(unit
, "hz") ||
948 strstr(unit
, "Hz") ||
949 strstr(unit
, "CPUs utilized"))
954 static const char *fixunit(char *buf
, struct perf_evsel
*evsel
,
957 if (!strncmp(unit
, "of all", 6)) {
958 snprintf(buf
, 1024, "%s %s", perf_evsel__name(evsel
),
965 static void print_metric_only(void *ctx
, const char *color
, const char *fmt
,
966 const char *unit
, double val
)
968 struct outstate
*os
= ctx
;
972 unsigned mlen
= METRIC_ONLY_LEN
;
974 if (!valid_only_metric(unit
))
976 unit
= fixunit(buf
, os
->evsel
, unit
);
978 n
= color_fprintf(out
, color
, fmt
, val
);
980 n
= fprintf(out
, fmt
, val
);
981 if (n
> METRIC_ONLY_LEN
)
983 if (mlen
< strlen(unit
))
984 mlen
= strlen(unit
) + 1;
985 fprintf(out
, "%*s", mlen
- n
, "");
988 static void print_metric_only_csv(void *ctx
, const char *color __maybe_unused
,
990 const char *unit
, double val
)
992 struct outstate
*os
= ctx
;
994 char buf
[64], *vals
, *ends
;
997 if (!valid_only_metric(unit
))
999 unit
= fixunit(tbuf
, os
->evsel
, unit
);
1000 snprintf(buf
, sizeof buf
, fmt
, val
);
1001 ends
= vals
= ltrim(buf
);
1002 while (isdigit(*ends
) || *ends
== '.')
1005 fprintf(out
, "%s%s", vals
, csv_sep
);
1008 static void new_line_metric(void *ctx __maybe_unused
)
1012 static void print_metric_header(void *ctx
, const char *color __maybe_unused
,
1013 const char *fmt __maybe_unused
,
1014 const char *unit
, double val __maybe_unused
)
1016 struct outstate
*os
= ctx
;
1019 if (!valid_only_metric(unit
))
1021 unit
= fixunit(tbuf
, os
->evsel
, unit
);
1023 fprintf(os
->fh
, "%s%s", unit
, csv_sep
);
1025 fprintf(os
->fh
, "%-*s ", METRIC_ONLY_LEN
, unit
);
1028 static void nsec_printout(int id
, int nr
, struct perf_evsel
*evsel
, double avg
)
1030 FILE *output
= stat_config
.output
;
1031 double msecs
= avg
/ NSEC_PER_MSEC
;
1032 const char *fmt_v
, *fmt_n
;
1035 fmt_v
= csv_output
? "%.6f%s" : "%18.6f%s";
1036 fmt_n
= csv_output
? "%s" : "%-25s";
1038 aggr_printout(evsel
, id
, nr
);
1040 scnprintf(name
, sizeof(name
), "%s%s",
1041 perf_evsel__name(evsel
), csv_output
? "" : " (msec)");
1043 fprintf(output
, fmt_v
, msecs
, csv_sep
);
1046 fprintf(output
, "%s%s", evsel
->unit
, csv_sep
);
1048 fprintf(output
, "%-*s%s", unit_width
, evsel
->unit
, csv_sep
);
1050 fprintf(output
, fmt_n
, name
);
1053 fprintf(output
, "%s%s", csv_sep
, evsel
->cgrp
->name
);
1056 static int first_shadow_cpu(struct perf_evsel
*evsel
, int id
)
1063 if (stat_config
.aggr_mode
== AGGR_NONE
)
1066 if (stat_config
.aggr_mode
== AGGR_GLOBAL
)
1069 for (i
= 0; i
< perf_evsel__nr_cpus(evsel
); i
++) {
1070 int cpu2
= perf_evsel__cpus(evsel
)->map
[i
];
1072 if (aggr_get_id(evsel_list
->cpus
, cpu2
) == id
)
1078 static void abs_printout(int id
, int nr
, struct perf_evsel
*evsel
, double avg
)
1080 FILE *output
= stat_config
.output
;
1081 double sc
= evsel
->scale
;
1085 fmt
= floor(sc
) != sc
? "%.2f%s" : "%.0f%s";
1088 fmt
= floor(sc
) != sc
? "%'18.2f%s" : "%'18.0f%s";
1090 fmt
= floor(sc
) != sc
? "%18.2f%s" : "%18.0f%s";
1093 aggr_printout(evsel
, id
, nr
);
1095 fprintf(output
, fmt
, avg
, csv_sep
);
1098 fprintf(output
, "%-*s%s",
1099 csv_output
? 0 : unit_width
,
1100 evsel
->unit
, csv_sep
);
1102 fprintf(output
, "%-*s", csv_output
? 0 : 25, perf_evsel__name(evsel
));
1105 fprintf(output
, "%s%s", csv_sep
, evsel
->cgrp
->name
);
1108 static void printout(int id
, int nr
, struct perf_evsel
*counter
, double uval
,
1109 char *prefix
, u64 run
, u64 ena
, double noise
)
1111 struct perf_stat_output_ctx out
;
1112 struct outstate os
= {
1113 .fh
= stat_config
.output
,
1114 .prefix
= prefix
? prefix
: "",
1119 print_metric_t pm
= print_metric_std
;
1123 nl
= new_line_metric
;
1125 pm
= print_metric_only_csv
;
1127 pm
= print_metric_only
;
1131 if (csv_output
&& !metric_only
) {
1132 static int aggr_fields
[] = {
1140 pm
= print_metric_csv
;
1143 os
.nfields
+= aggr_fields
[stat_config
.aggr_mode
];
1147 if (run
== 0 || ena
== 0 || counter
->counts
->scaled
== -1) {
1149 pm(&os
, NULL
, "", "", 0);
1152 aggr_printout(counter
, id
, nr
);
1154 fprintf(stat_config
.output
, "%*s%s",
1155 csv_output
? 0 : 18,
1156 counter
->supported
? CNTR_NOT_COUNTED
: CNTR_NOT_SUPPORTED
,
1159 if (counter
->supported
)
1160 print_free_counters_hint
= 1;
1162 fprintf(stat_config
.output
, "%-*s%s",
1163 csv_output
? 0 : unit_width
,
1164 counter
->unit
, csv_sep
);
1166 fprintf(stat_config
.output
, "%*s",
1167 csv_output
? 0 : -25,
1168 perf_evsel__name(counter
));
1171 fprintf(stat_config
.output
, "%s%s",
1172 csv_sep
, counter
->cgrp
->name
);
1175 pm(&os
, NULL
, NULL
, "", 0);
1176 print_noise(counter
, noise
);
1177 print_running(run
, ena
);
1179 pm(&os
, NULL
, NULL
, "", 0);
1185 else if (nsec_counter(counter
))
1186 nsec_printout(id
, nr
, counter
, uval
);
1188 abs_printout(id
, nr
, counter
, uval
);
1190 out
.print_metric
= pm
;
1193 out
.force_header
= false;
1195 if (csv_output
&& !metric_only
) {
1196 print_noise(counter
, noise
);
1197 print_running(run
, ena
);
1200 perf_stat__print_shadow_stats(counter
, uval
,
1201 first_shadow_cpu(counter
, id
),
1203 if (!csv_output
&& !metric_only
) {
1204 print_noise(counter
, noise
);
1205 print_running(run
, ena
);
1209 static void aggr_update_shadow(void)
1213 struct perf_evsel
*counter
;
1215 for (s
= 0; s
< aggr_map
->nr
; s
++) {
1216 id
= aggr_map
->map
[s
];
1217 evlist__for_each_entry(evsel_list
, counter
) {
1219 for (cpu
= 0; cpu
< perf_evsel__nr_cpus(counter
); cpu
++) {
1220 s2
= aggr_get_id(evsel_list
->cpus
, cpu
);
1223 val
+= perf_counts(counter
->counts
, cpu
, 0)->val
;
1225 val
= val
* counter
->scale
;
1226 perf_stat__update_shadow_stats(counter
, &val
,
1227 first_shadow_cpu(counter
, id
));
1232 static void collect_all_aliases(struct perf_evsel
*counter
,
1233 void (*cb
)(struct perf_evsel
*counter
, void *data
,
1237 struct perf_evsel
*alias
;
1239 alias
= list_prepare_entry(counter
, &(evsel_list
->entries
), node
);
1240 list_for_each_entry_continue (alias
, &evsel_list
->entries
, node
) {
1241 if (strcmp(perf_evsel__name(alias
), perf_evsel__name(counter
)) ||
1242 alias
->scale
!= counter
->scale
||
1243 alias
->cgrp
!= counter
->cgrp
||
1244 strcmp(alias
->unit
, counter
->unit
) ||
1245 nsec_counter(alias
) != nsec_counter(counter
))
1247 alias
->merged_stat
= true;
1248 cb(alias
, data
, false);
1252 static bool collect_data(struct perf_evsel
*counter
,
1253 void (*cb
)(struct perf_evsel
*counter
, void *data
,
1257 if (counter
->merged_stat
)
1259 cb(counter
, data
, true);
1261 collect_all_aliases(counter
, cb
, data
);
1272 static void aggr_cb(struct perf_evsel
*counter
, void *data
, bool first
)
1274 struct aggr_data
*ad
= data
;
1277 for (cpu
= 0; cpu
< perf_evsel__nr_cpus(counter
); cpu
++) {
1278 struct perf_counts_values
*counts
;
1280 s2
= aggr_get_id(perf_evsel__cpus(counter
), cpu
);
1285 counts
= perf_counts(counter
->counts
, cpu
, 0);
1287 * When any result is bad, make them all to give
1288 * consistent output in interval mode.
1290 if (counts
->ena
== 0 || counts
->run
== 0 ||
1291 counter
->counts
->scaled
== -1) {
1296 ad
->val
+= counts
->val
;
1297 ad
->ena
+= counts
->ena
;
1298 ad
->run
+= counts
->run
;
1302 static void print_aggr(char *prefix
)
1304 FILE *output
= stat_config
.output
;
1305 struct perf_evsel
*counter
;
1311 if (!(aggr_map
|| aggr_get_id
))
1314 aggr_update_shadow();
1317 * With metric_only everything is on a single line.
1318 * Without each counter has its own line.
1320 for (s
= 0; s
< aggr_map
->nr
; s
++) {
1321 struct aggr_data ad
;
1322 if (prefix
&& metric_only
)
1323 fprintf(output
, "%s", prefix
);
1325 ad
.id
= id
= aggr_map
->map
[s
];
1327 evlist__for_each_entry(evsel_list
, counter
) {
1328 ad
.val
= ad
.ena
= ad
.run
= 0;
1330 if (!collect_data(counter
, aggr_cb
, &ad
))
1336 if (first
&& metric_only
) {
1338 aggr_printout(counter
, id
, nr
);
1340 if (prefix
&& !metric_only
)
1341 fprintf(output
, "%s", prefix
);
1343 uval
= val
* counter
->scale
;
1344 printout(id
, nr
, counter
, uval
, prefix
, run
, ena
, 1.0);
1346 fputc('\n', output
);
1349 fputc('\n', output
);
1353 static void print_aggr_thread(struct perf_evsel
*counter
, char *prefix
)
1355 FILE *output
= stat_config
.output
;
1356 int nthreads
= thread_map__nr(counter
->threads
);
1357 int ncpus
= cpu_map__nr(counter
->cpus
);
1361 for (thread
= 0; thread
< nthreads
; thread
++) {
1362 u64 ena
= 0, run
= 0, val
= 0;
1364 for (cpu
= 0; cpu
< ncpus
; cpu
++) {
1365 val
+= perf_counts(counter
->counts
, cpu
, thread
)->val
;
1366 ena
+= perf_counts(counter
->counts
, cpu
, thread
)->ena
;
1367 run
+= perf_counts(counter
->counts
, cpu
, thread
)->run
;
1371 fprintf(output
, "%s", prefix
);
1373 uval
= val
* counter
->scale
;
1374 printout(thread
, 0, counter
, uval
, prefix
, run
, ena
, 1.0);
1375 fputc('\n', output
);
1380 double avg
, avg_enabled
, avg_running
;
1383 static void counter_aggr_cb(struct perf_evsel
*counter
, void *data
,
1384 bool first __maybe_unused
)
1386 struct caggr_data
*cd
= data
;
1387 struct perf_stat_evsel
*ps
= counter
->priv
;
1389 cd
->avg
+= avg_stats(&ps
->res_stats
[0]);
1390 cd
->avg_enabled
+= avg_stats(&ps
->res_stats
[1]);
1391 cd
->avg_running
+= avg_stats(&ps
->res_stats
[2]);
1395 * Print out the results of a single counter:
1396 * aggregated counts in system-wide mode
1398 static void print_counter_aggr(struct perf_evsel
*counter
, char *prefix
)
1400 FILE *output
= stat_config
.output
;
1402 struct caggr_data cd
= { .avg
= 0.0 };
1404 if (!collect_data(counter
, counter_aggr_cb
, &cd
))
1407 if (prefix
&& !metric_only
)
1408 fprintf(output
, "%s", prefix
);
1410 uval
= cd
.avg
* counter
->scale
;
1411 printout(-1, 0, counter
, uval
, prefix
, cd
.avg_running
, cd
.avg_enabled
, cd
.avg
);
1413 fprintf(output
, "\n");
1416 static void counter_cb(struct perf_evsel
*counter
, void *data
,
1417 bool first __maybe_unused
)
1419 struct aggr_data
*ad
= data
;
1421 ad
->val
+= perf_counts(counter
->counts
, ad
->cpu
, 0)->val
;
1422 ad
->ena
+= perf_counts(counter
->counts
, ad
->cpu
, 0)->ena
;
1423 ad
->run
+= perf_counts(counter
->counts
, ad
->cpu
, 0)->run
;
1427 * Print out the results of a single counter:
1428 * does not use aggregated count in system-wide
1430 static void print_counter(struct perf_evsel
*counter
, char *prefix
)
1432 FILE *output
= stat_config
.output
;
1437 for (cpu
= 0; cpu
< perf_evsel__nr_cpus(counter
); cpu
++) {
1438 struct aggr_data ad
= { .cpu
= cpu
};
1440 if (!collect_data(counter
, counter_cb
, &ad
))
1447 fprintf(output
, "%s", prefix
);
1449 uval
= val
* counter
->scale
;
1450 printout(cpu
, 0, counter
, uval
, prefix
, run
, ena
, 1.0);
1452 fputc('\n', output
);
1456 static void print_no_aggr_metric(char *prefix
)
1460 struct perf_evsel
*counter
;
1464 nrcpus
= evsel_list
->cpus
->nr
;
1465 for (cpu
= 0; cpu
< nrcpus
; cpu
++) {
1469 fputs(prefix
, stat_config
.output
);
1470 evlist__for_each_entry(evsel_list
, counter
) {
1472 aggr_printout(counter
, cpu
, 0);
1475 val
= perf_counts(counter
->counts
, cpu
, 0)->val
;
1476 ena
= perf_counts(counter
->counts
, cpu
, 0)->ena
;
1477 run
= perf_counts(counter
->counts
, cpu
, 0)->run
;
1479 uval
= val
* counter
->scale
;
1480 printout(cpu
, 0, counter
, uval
, prefix
, run
, ena
, 1.0);
1482 fputc('\n', stat_config
.output
);
1486 static int aggr_header_lens
[] = {
1494 static const char *aggr_header_csv
[] = {
1495 [AGGR_CORE
] = "core,cpus,",
1496 [AGGR_SOCKET
] = "socket,cpus",
1497 [AGGR_NONE
] = "cpu,",
1498 [AGGR_THREAD
] = "comm-pid,",
1502 static void print_metric_headers(const char *prefix
, bool no_indent
)
1504 struct perf_stat_output_ctx out
;
1505 struct perf_evsel
*counter
;
1506 struct outstate os
= {
1507 .fh
= stat_config
.output
1511 fprintf(stat_config
.output
, "%s", prefix
);
1513 if (!csv_output
&& !no_indent
)
1514 fprintf(stat_config
.output
, "%*s",
1515 aggr_header_lens
[stat_config
.aggr_mode
], "");
1517 if (stat_config
.interval
)
1518 fputs("time,", stat_config
.output
);
1519 fputs(aggr_header_csv
[stat_config
.aggr_mode
],
1520 stat_config
.output
);
1523 /* Print metrics headers only */
1524 evlist__for_each_entry(evsel_list
, counter
) {
1527 out
.print_metric
= print_metric_header
;
1528 out
.new_line
= new_line_metric
;
1529 out
.force_header
= true;
1531 perf_stat__print_shadow_stats(counter
, 0,
1535 fputc('\n', stat_config
.output
);
1538 static void print_interval(char *prefix
, struct timespec
*ts
)
1540 FILE *output
= stat_config
.output
;
1541 static int num_print_interval
;
1543 sprintf(prefix
, "%6lu.%09lu%s", ts
->tv_sec
, ts
->tv_nsec
, csv_sep
);
1545 if (num_print_interval
== 0 && !csv_output
) {
1546 switch (stat_config
.aggr_mode
) {
1548 fprintf(output
, "# time socket cpus");
1550 fprintf(output
, " counts %*s events\n", unit_width
, "unit");
1553 fprintf(output
, "# time core cpus");
1555 fprintf(output
, " counts %*s events\n", unit_width
, "unit");
1558 fprintf(output
, "# time CPU");
1560 fprintf(output
, " counts %*s events\n", unit_width
, "unit");
1563 fprintf(output
, "# time comm-pid");
1565 fprintf(output
, " counts %*s events\n", unit_width
, "unit");
1569 fprintf(output
, "# time");
1571 fprintf(output
, " counts %*s events\n", unit_width
, "unit");
1577 if (num_print_interval
== 0 && metric_only
)
1578 print_metric_headers(" ", true);
1579 if (++num_print_interval
== 25)
1580 num_print_interval
= 0;
1583 static void print_header(int argc
, const char **argv
)
1585 FILE *output
= stat_config
.output
;
1591 fprintf(output
, "\n");
1592 fprintf(output
, " Performance counter stats for ");
1593 if (target
.system_wide
)
1594 fprintf(output
, "\'system wide");
1595 else if (target
.cpu_list
)
1596 fprintf(output
, "\'CPU(s) %s", target
.cpu_list
);
1597 else if (!target__has_task(&target
)) {
1598 fprintf(output
, "\'%s", argv
? argv
[0] : "pipe");
1599 for (i
= 1; argv
&& (i
< argc
); i
++)
1600 fprintf(output
, " %s", argv
[i
]);
1601 } else if (target
.pid
)
1602 fprintf(output
, "process id \'%s", target
.pid
);
1604 fprintf(output
, "thread id \'%s", target
.tid
);
1606 fprintf(output
, "\'");
1608 fprintf(output
, " (%d runs)", run_count
);
1609 fprintf(output
, ":\n\n");
1613 static void print_footer(void)
1615 FILE *output
= stat_config
.output
;
1619 fprintf(output
, "\n");
1620 fprintf(output
, " %17.9f seconds time elapsed",
1621 avg_stats(&walltime_nsecs_stats
) / NSEC_PER_SEC
);
1622 if (run_count
> 1) {
1623 fprintf(output
, " ");
1624 print_noise_pct(stddev_stats(&walltime_nsecs_stats
),
1625 avg_stats(&walltime_nsecs_stats
));
1627 fprintf(output
, "\n\n");
1629 if (print_free_counters_hint
&&
1630 sysctl__read_int("kernel/nmi_watchdog", &n
) >= 0 &&
1633 "Some events weren't counted. Try disabling the NMI watchdog:\n"
1634 " echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1636 " echo 1 > /proc/sys/kernel/nmi_watchdog\n");
1639 static void print_counters(struct timespec
*ts
, int argc
, const char **argv
)
1641 int interval
= stat_config
.interval
;
1642 struct perf_evsel
*counter
;
1643 char buf
[64], *prefix
= NULL
;
1645 /* Do not print anything if we record to the pipe. */
1646 if (STAT_RECORD
&& perf_stat
.file
.is_pipe
)
1650 print_interval(prefix
= buf
, ts
);
1652 print_header(argc
, argv
);
1655 static int num_print_iv
;
1657 if (num_print_iv
== 0 && !interval
)
1658 print_metric_headers(prefix
, false);
1659 if (num_print_iv
++ == 25)
1661 if (stat_config
.aggr_mode
== AGGR_GLOBAL
&& prefix
)
1662 fprintf(stat_config
.output
, "%s", prefix
);
1665 switch (stat_config
.aggr_mode
) {
1671 evlist__for_each_entry(evsel_list
, counter
)
1672 print_aggr_thread(counter
, prefix
);
1675 evlist__for_each_entry(evsel_list
, counter
)
1676 print_counter_aggr(counter
, prefix
);
1678 fputc('\n', stat_config
.output
);
1682 print_no_aggr_metric(prefix
);
1684 evlist__for_each_entry(evsel_list
, counter
)
1685 print_counter(counter
, prefix
);
1693 if (!interval
&& !csv_output
)
1696 fflush(stat_config
.output
);
1699 static volatile int signr
= -1;
1701 static void skip_signal(int signo
)
1703 if ((child_pid
== -1) || stat_config
.interval
)
1708 * render child_pid harmless
1709 * won't send SIGTERM to a random
1710 * process in case of race condition
1711 * and fast PID recycling
1716 static void sig_atexit(void)
1721 * avoid race condition with SIGCHLD handler
1722 * in skip_signal() which is modifying child_pid
1723 * goal is to avoid send SIGTERM to a random
1727 sigaddset(&set
, SIGCHLD
);
1728 sigprocmask(SIG_BLOCK
, &set
, &oset
);
1730 if (child_pid
!= -1)
1731 kill(child_pid
, SIGTERM
);
1733 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
1738 signal(signr
, SIG_DFL
);
1739 kill(getpid(), signr
);
1742 static int stat__set_big_num(const struct option
*opt __maybe_unused
,
1743 const char *s __maybe_unused
, int unset
)
1745 big_num_opt
= unset
? 0 : 1;
1749 static int enable_metric_only(const struct option
*opt __maybe_unused
,
1750 const char *s __maybe_unused
, int unset
)
1752 force_metric_only
= true;
1753 metric_only
= !unset
;
1757 static const struct option stat_options
[] = {
1758 OPT_BOOLEAN('T', "transaction", &transaction_run
,
1759 "hardware transaction statistics"),
1760 OPT_CALLBACK('e', "event", &evsel_list
, "event",
1761 "event selector. use 'perf list' to list available events",
1762 parse_events_option
),
1763 OPT_CALLBACK(0, "filter", &evsel_list
, "filter",
1764 "event filter", parse_filter
),
1765 OPT_BOOLEAN('i', "no-inherit", &no_inherit
,
1766 "child tasks do not inherit counters"),
1767 OPT_STRING('p', "pid", &target
.pid
, "pid",
1768 "stat events on existing process id"),
1769 OPT_STRING('t', "tid", &target
.tid
, "tid",
1770 "stat events on existing thread id"),
1771 OPT_BOOLEAN('a', "all-cpus", &target
.system_wide
,
1772 "system-wide collection from all CPUs"),
1773 OPT_BOOLEAN('g', "group", &group
,
1774 "put the counters into a counter group"),
1775 OPT_BOOLEAN('c', "scale", &stat_config
.scale
, "scale/normalize counters"),
1776 OPT_INCR('v', "verbose", &verbose
,
1777 "be more verbose (show counter open errors, etc)"),
1778 OPT_INTEGER('r', "repeat", &run_count
,
1779 "repeat command and print average + stddev (max: 100, forever: 0)"),
1780 OPT_BOOLEAN('n', "null", &null_run
,
1781 "null run - dont start any counters"),
1782 OPT_INCR('d', "detailed", &detailed_run
,
1783 "detailed run - start a lot of events"),
1784 OPT_BOOLEAN('S', "sync", &sync_run
,
1785 "call sync() before starting a run"),
1786 OPT_CALLBACK_NOOPT('B', "big-num", NULL
, NULL
,
1787 "print large numbers with thousands\' separators",
1789 OPT_STRING('C', "cpu", &target
.cpu_list
, "cpu",
1790 "list of cpus to monitor in system-wide"),
1791 OPT_SET_UINT('A', "no-aggr", &stat_config
.aggr_mode
,
1792 "disable CPU count aggregation", AGGR_NONE
),
1793 OPT_BOOLEAN(0, "no-merge", &no_merge
, "Do not merge identical named events"),
1794 OPT_STRING('x', "field-separator", &csv_sep
, "separator",
1795 "print counts with custom separator"),
1796 OPT_CALLBACK('G', "cgroup", &evsel_list
, "name",
1797 "monitor event in cgroup name only", parse_cgroups
),
1798 OPT_STRING('o', "output", &output_name
, "file", "output file name"),
1799 OPT_BOOLEAN(0, "append", &append_file
, "append to the output file"),
1800 OPT_INTEGER(0, "log-fd", &output_fd
,
1801 "log output to fd, instead of stderr"),
1802 OPT_STRING(0, "pre", &pre_cmd
, "command",
1803 "command to run prior to the measured command"),
1804 OPT_STRING(0, "post", &post_cmd
, "command",
1805 "command to run after to the measured command"),
1806 OPT_UINTEGER('I', "interval-print", &stat_config
.interval
,
1807 "print counts at regular interval in ms (>= 10)"),
1808 OPT_SET_UINT(0, "per-socket", &stat_config
.aggr_mode
,
1809 "aggregate counts per processor socket", AGGR_SOCKET
),
1810 OPT_SET_UINT(0, "per-core", &stat_config
.aggr_mode
,
1811 "aggregate counts per physical processor core", AGGR_CORE
),
1812 OPT_SET_UINT(0, "per-thread", &stat_config
.aggr_mode
,
1813 "aggregate counts per thread", AGGR_THREAD
),
1814 OPT_UINTEGER('D', "delay", &initial_delay
,
1815 "ms to wait before starting measurement after program start"),
1816 OPT_CALLBACK_NOOPT(0, "metric-only", &metric_only
, NULL
,
1817 "Only print computed metrics. No raw values", enable_metric_only
),
1818 OPT_BOOLEAN(0, "topdown", &topdown_run
,
1819 "measure topdown level 1 statistics"),
1820 OPT_BOOLEAN(0, "smi-cost", &smi_cost
,
1821 "measure SMI cost"),
1825 static int perf_stat__get_socket(struct cpu_map
*map
, int cpu
)
1827 return cpu_map__get_socket(map
, cpu
, NULL
);
1830 static int perf_stat__get_core(struct cpu_map
*map
, int cpu
)
1832 return cpu_map__get_core(map
, cpu
, NULL
);
1835 static int cpu_map__get_max(struct cpu_map
*map
)
1839 for (i
= 0; i
< map
->nr
; i
++) {
1840 if (map
->map
[i
] > max
)
1847 static struct cpu_map
*cpus_aggr_map
;
1849 static int perf_stat__get_aggr(aggr_get_id_t get_id
, struct cpu_map
*map
, int idx
)
1856 cpu
= map
->map
[idx
];
1858 if (cpus_aggr_map
->map
[cpu
] == -1)
1859 cpus_aggr_map
->map
[cpu
] = get_id(map
, idx
);
1861 return cpus_aggr_map
->map
[cpu
];
1864 static int perf_stat__get_socket_cached(struct cpu_map
*map
, int idx
)
1866 return perf_stat__get_aggr(perf_stat__get_socket
, map
, idx
);
1869 static int perf_stat__get_core_cached(struct cpu_map
*map
, int idx
)
1871 return perf_stat__get_aggr(perf_stat__get_core
, map
, idx
);
1874 static int perf_stat_init_aggr_mode(void)
1878 switch (stat_config
.aggr_mode
) {
1880 if (cpu_map__build_socket_map(evsel_list
->cpus
, &aggr_map
)) {
1881 perror("cannot build socket map");
1884 aggr_get_id
= perf_stat__get_socket_cached
;
1887 if (cpu_map__build_core_map(evsel_list
->cpus
, &aggr_map
)) {
1888 perror("cannot build core map");
1891 aggr_get_id
= perf_stat__get_core_cached
;
1902 * The evsel_list->cpus is the base we operate on,
1903 * taking the highest cpu number to be the size of
1904 * the aggregation translate cpumap.
1906 nr
= cpu_map__get_max(evsel_list
->cpus
);
1907 cpus_aggr_map
= cpu_map__empty_new(nr
+ 1);
1908 return cpus_aggr_map
? 0 : -ENOMEM
;
1911 static void perf_stat__exit_aggr_mode(void)
1913 cpu_map__put(aggr_map
);
1914 cpu_map__put(cpus_aggr_map
);
1916 cpus_aggr_map
= NULL
;
1919 static inline int perf_env__get_cpu(struct perf_env
*env
, struct cpu_map
*map
, int idx
)
1926 cpu
= map
->map
[idx
];
1928 if (cpu
>= env
->nr_cpus_avail
)
1934 static int perf_env__get_socket(struct cpu_map
*map
, int idx
, void *data
)
1936 struct perf_env
*env
= data
;
1937 int cpu
= perf_env__get_cpu(env
, map
, idx
);
1939 return cpu
== -1 ? -1 : env
->cpu
[cpu
].socket_id
;
1942 static int perf_env__get_core(struct cpu_map
*map
, int idx
, void *data
)
1944 struct perf_env
*env
= data
;
1945 int core
= -1, cpu
= perf_env__get_cpu(env
, map
, idx
);
1948 int socket_id
= env
->cpu
[cpu
].socket_id
;
1951 * Encode socket in upper 16 bits
1952 * core_id is relative to socket, and
1953 * we need a global id. So we combine
1956 core
= (socket_id
<< 16) | (env
->cpu
[cpu
].core_id
& 0xffff);
1962 static int perf_env__build_socket_map(struct perf_env
*env
, struct cpu_map
*cpus
,
1963 struct cpu_map
**sockp
)
1965 return cpu_map__build_map(cpus
, sockp
, perf_env__get_socket
, env
);
1968 static int perf_env__build_core_map(struct perf_env
*env
, struct cpu_map
*cpus
,
1969 struct cpu_map
**corep
)
1971 return cpu_map__build_map(cpus
, corep
, perf_env__get_core
, env
);
1974 static int perf_stat__get_socket_file(struct cpu_map
*map
, int idx
)
1976 return perf_env__get_socket(map
, idx
, &perf_stat
.session
->header
.env
);
1979 static int perf_stat__get_core_file(struct cpu_map
*map
, int idx
)
1981 return perf_env__get_core(map
, idx
, &perf_stat
.session
->header
.env
);
1984 static int perf_stat_init_aggr_mode_file(struct perf_stat
*st
)
1986 struct perf_env
*env
= &st
->session
->header
.env
;
1988 switch (stat_config
.aggr_mode
) {
1990 if (perf_env__build_socket_map(env
, evsel_list
->cpus
, &aggr_map
)) {
1991 perror("cannot build socket map");
1994 aggr_get_id
= perf_stat__get_socket_file
;
1997 if (perf_env__build_core_map(env
, evsel_list
->cpus
, &aggr_map
)) {
1998 perror("cannot build core map");
2001 aggr_get_id
= perf_stat__get_core_file
;
2014 static int topdown_filter_events(const char **attr
, char **str
, bool use_group
)
2021 for (i
= 0; attr
[i
]; i
++) {
2022 if (pmu_have_event("cpu", attr
[i
])) {
2023 len
+= strlen(attr
[i
]) + 1;
2024 attr
[i
- off
] = attr
[i
];
2028 attr
[i
- off
] = NULL
;
2030 *str
= malloc(len
+ 1 + 2);
2040 for (i
= 0; attr
[i
]; i
++) {
2053 __weak
bool arch_topdown_check_group(bool *warn
)
2059 __weak
void arch_topdown_group_warn(void)
2064 * Add default attributes, if there were no attributes specified or
2065 * if -d/--detailed, -d -d or -d -d -d is used:
2067 static int add_default_attributes(void)
2070 struct perf_event_attr default_attrs0
[] = {
2072 { .type
= PERF_TYPE_SOFTWARE
, .config
= PERF_COUNT_SW_TASK_CLOCK
},
2073 { .type
= PERF_TYPE_SOFTWARE
, .config
= PERF_COUNT_SW_CONTEXT_SWITCHES
},
2074 { .type
= PERF_TYPE_SOFTWARE
, .config
= PERF_COUNT_SW_CPU_MIGRATIONS
},
2075 { .type
= PERF_TYPE_SOFTWARE
, .config
= PERF_COUNT_SW_PAGE_FAULTS
},
2077 { .type
= PERF_TYPE_HARDWARE
, .config
= PERF_COUNT_HW_CPU_CYCLES
},
2079 struct perf_event_attr frontend_attrs
[] = {
2080 { .type
= PERF_TYPE_HARDWARE
, .config
= PERF_COUNT_HW_STALLED_CYCLES_FRONTEND
},
2082 struct perf_event_attr backend_attrs
[] = {
2083 { .type
= PERF_TYPE_HARDWARE
, .config
= PERF_COUNT_HW_STALLED_CYCLES_BACKEND
},
2085 struct perf_event_attr default_attrs1
[] = {
2086 { .type
= PERF_TYPE_HARDWARE
, .config
= PERF_COUNT_HW_INSTRUCTIONS
},
2087 { .type
= PERF_TYPE_HARDWARE
, .config
= PERF_COUNT_HW_BRANCH_INSTRUCTIONS
},
2088 { .type
= PERF_TYPE_HARDWARE
, .config
= PERF_COUNT_HW_BRANCH_MISSES
},
2093 * Detailed stats (-d), covering the L1 and last level data caches:
2095 struct perf_event_attr detailed_attrs
[] = {
2097 { .type
= PERF_TYPE_HW_CACHE
,
2099 PERF_COUNT_HW_CACHE_L1D
<< 0 |
2100 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
2101 (PERF_COUNT_HW_CACHE_RESULT_ACCESS
<< 16) },
2103 { .type
= PERF_TYPE_HW_CACHE
,
2105 PERF_COUNT_HW_CACHE_L1D
<< 0 |
2106 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
2107 (PERF_COUNT_HW_CACHE_RESULT_MISS
<< 16) },
2109 { .type
= PERF_TYPE_HW_CACHE
,
2111 PERF_COUNT_HW_CACHE_LL
<< 0 |
2112 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
2113 (PERF_COUNT_HW_CACHE_RESULT_ACCESS
<< 16) },
2115 { .type
= PERF_TYPE_HW_CACHE
,
2117 PERF_COUNT_HW_CACHE_LL
<< 0 |
2118 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
2119 (PERF_COUNT_HW_CACHE_RESULT_MISS
<< 16) },
2123 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
2125 struct perf_event_attr very_detailed_attrs
[] = {
2127 { .type
= PERF_TYPE_HW_CACHE
,
2129 PERF_COUNT_HW_CACHE_L1I
<< 0 |
2130 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
2131 (PERF_COUNT_HW_CACHE_RESULT_ACCESS
<< 16) },
2133 { .type
= PERF_TYPE_HW_CACHE
,
2135 PERF_COUNT_HW_CACHE_L1I
<< 0 |
2136 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
2137 (PERF_COUNT_HW_CACHE_RESULT_MISS
<< 16) },
2139 { .type
= PERF_TYPE_HW_CACHE
,
2141 PERF_COUNT_HW_CACHE_DTLB
<< 0 |
2142 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
2143 (PERF_COUNT_HW_CACHE_RESULT_ACCESS
<< 16) },
2145 { .type
= PERF_TYPE_HW_CACHE
,
2147 PERF_COUNT_HW_CACHE_DTLB
<< 0 |
2148 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
2149 (PERF_COUNT_HW_CACHE_RESULT_MISS
<< 16) },
2151 { .type
= PERF_TYPE_HW_CACHE
,
2153 PERF_COUNT_HW_CACHE_ITLB
<< 0 |
2154 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
2155 (PERF_COUNT_HW_CACHE_RESULT_ACCESS
<< 16) },
2157 { .type
= PERF_TYPE_HW_CACHE
,
2159 PERF_COUNT_HW_CACHE_ITLB
<< 0 |
2160 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
2161 (PERF_COUNT_HW_CACHE_RESULT_MISS
<< 16) },
2166 * Very, very detailed stats (-d -d -d), adding prefetch events:
2168 struct perf_event_attr very_very_detailed_attrs
[] = {
2170 { .type
= PERF_TYPE_HW_CACHE
,
2172 PERF_COUNT_HW_CACHE_L1D
<< 0 |
2173 (PERF_COUNT_HW_CACHE_OP_PREFETCH
<< 8) |
2174 (PERF_COUNT_HW_CACHE_RESULT_ACCESS
<< 16) },
2176 { .type
= PERF_TYPE_HW_CACHE
,
2178 PERF_COUNT_HW_CACHE_L1D
<< 0 |
2179 (PERF_COUNT_HW_CACHE_OP_PREFETCH
<< 8) |
2180 (PERF_COUNT_HW_CACHE_RESULT_MISS
<< 16) },
2183 /* Set attrs if no event is selected and !null_run: */
2187 if (transaction_run
) {
2188 if (pmu_have_event("cpu", "cycles-ct") &&
2189 pmu_have_event("cpu", "el-start"))
2190 err
= parse_events(evsel_list
, transaction_attrs
, NULL
);
2192 err
= parse_events(evsel_list
, transaction_limited_attrs
, NULL
);
2194 fprintf(stderr
, "Cannot set up transaction events\n");
2203 if (sysfs__read_int(FREEZE_ON_SMI_PATH
, &smi
) < 0) {
2204 fprintf(stderr
, "freeze_on_smi is not supported.\n");
2209 if (sysfs__write_int(FREEZE_ON_SMI_PATH
, 1) < 0) {
2210 fprintf(stderr
, "Failed to set freeze_on_smi.\n");
2216 if (pmu_have_event("msr", "aperf") &&
2217 pmu_have_event("msr", "smi")) {
2218 if (!force_metric_only
)
2220 err
= parse_events(evsel_list
, smi_cost_attrs
, NULL
);
2222 fprintf(stderr
, "To measure SMI cost, it needs "
2223 "msr/aperf/, msr/smi/ and cpu/cycles/ support\n");
2227 fprintf(stderr
, "Cannot set up SMI cost events\n");
2237 if (stat_config
.aggr_mode
!= AGGR_GLOBAL
&&
2238 stat_config
.aggr_mode
!= AGGR_CORE
) {
2239 pr_err("top down event configuration requires --per-core mode\n");
2242 stat_config
.aggr_mode
= AGGR_CORE
;
2243 if (nr_cgroups
|| !target__has_cpu(&target
)) {
2244 pr_err("top down event configuration requires system-wide mode (-a)\n");
2248 if (!force_metric_only
)
2250 if (topdown_filter_events(topdown_attrs
, &str
,
2251 arch_topdown_check_group(&warn
)) < 0) {
2252 pr_err("Out of memory\n");
2255 if (topdown_attrs
[0] && str
) {
2257 arch_topdown_group_warn();
2258 err
= parse_events(evsel_list
, str
, NULL
);
2261 "Cannot set up top down events %s: %d\n",
2267 fprintf(stderr
, "System does not support topdown\n");
2273 if (!evsel_list
->nr_entries
) {
2274 if (target__has_cpu(&target
))
2275 default_attrs0
[0].config
= PERF_COUNT_SW_CPU_CLOCK
;
2277 if (perf_evlist__add_default_attrs(evsel_list
, default_attrs0
) < 0)
2279 if (pmu_have_event("cpu", "stalled-cycles-frontend")) {
2280 if (perf_evlist__add_default_attrs(evsel_list
,
2281 frontend_attrs
) < 0)
2284 if (pmu_have_event("cpu", "stalled-cycles-backend")) {
2285 if (perf_evlist__add_default_attrs(evsel_list
,
2289 if (perf_evlist__add_default_attrs(evsel_list
, default_attrs1
) < 0)
2293 /* Detailed events get appended to the event list: */
2295 if (detailed_run
< 1)
2298 /* Append detailed run extra attributes: */
2299 if (perf_evlist__add_default_attrs(evsel_list
, detailed_attrs
) < 0)
2302 if (detailed_run
< 2)
2305 /* Append very detailed run extra attributes: */
2306 if (perf_evlist__add_default_attrs(evsel_list
, very_detailed_attrs
) < 0)
2309 if (detailed_run
< 3)
2312 /* Append very, very detailed run extra attributes: */
2313 return perf_evlist__add_default_attrs(evsel_list
, very_very_detailed_attrs
);
2316 static const char * const stat_record_usage
[] = {
2317 "perf stat record [<options>]",
2321 static void init_features(struct perf_session
*session
)
2325 for (feat
= HEADER_FIRST_FEATURE
; feat
< HEADER_LAST_FEATURE
; feat
++)
2326 perf_header__set_feat(&session
->header
, feat
);
2328 perf_header__clear_feat(&session
->header
, HEADER_BUILD_ID
);
2329 perf_header__clear_feat(&session
->header
, HEADER_TRACING_DATA
);
2330 perf_header__clear_feat(&session
->header
, HEADER_BRANCH_STACK
);
2331 perf_header__clear_feat(&session
->header
, HEADER_AUXTRACE
);
2334 static int __cmd_record(int argc
, const char **argv
)
2336 struct perf_session
*session
;
2337 struct perf_data_file
*file
= &perf_stat
.file
;
2339 argc
= parse_options(argc
, argv
, stat_options
, stat_record_usage
,
2340 PARSE_OPT_STOP_AT_NON_OPTION
);
2343 file
->path
= output_name
;
2345 if (run_count
!= 1 || forever
) {
2346 pr_err("Cannot use -r option with perf stat record.\n");
2350 session
= perf_session__new(file
, false, NULL
);
2351 if (session
== NULL
) {
2352 pr_err("Perf session creation failed.\n");
2356 init_features(session
);
2358 session
->evlist
= evsel_list
;
2359 perf_stat
.session
= session
;
2360 perf_stat
.record
= true;
2364 static int process_stat_round_event(struct perf_tool
*tool __maybe_unused
,
2365 union perf_event
*event
,
2366 struct perf_session
*session
)
2368 struct stat_round_event
*stat_round
= &event
->stat_round
;
2369 struct perf_evsel
*counter
;
2370 struct timespec tsh
, *ts
= NULL
;
2371 const char **argv
= session
->header
.env
.cmdline_argv
;
2372 int argc
= session
->header
.env
.nr_cmdline
;
2374 evlist__for_each_entry(evsel_list
, counter
)
2375 perf_stat_process_counter(&stat_config
, counter
);
2377 if (stat_round
->type
== PERF_STAT_ROUND_TYPE__FINAL
)
2378 update_stats(&walltime_nsecs_stats
, stat_round
->time
);
2380 if (stat_config
.interval
&& stat_round
->time
) {
2381 tsh
.tv_sec
= stat_round
->time
/ NSEC_PER_SEC
;
2382 tsh
.tv_nsec
= stat_round
->time
% NSEC_PER_SEC
;
2386 print_counters(ts
, argc
, argv
);
2391 int process_stat_config_event(struct perf_tool
*tool
,
2392 union perf_event
*event
,
2393 struct perf_session
*session __maybe_unused
)
2395 struct perf_stat
*st
= container_of(tool
, struct perf_stat
, tool
);
2397 perf_event__read_stat_config(&stat_config
, &event
->stat_config
);
2399 if (cpu_map__empty(st
->cpus
)) {
2400 if (st
->aggr_mode
!= AGGR_UNSET
)
2401 pr_warning("warning: processing task data, aggregation mode not set\n");
2405 if (st
->aggr_mode
!= AGGR_UNSET
)
2406 stat_config
.aggr_mode
= st
->aggr_mode
;
2408 if (perf_stat
.file
.is_pipe
)
2409 perf_stat_init_aggr_mode();
2411 perf_stat_init_aggr_mode_file(st
);
2416 static int set_maps(struct perf_stat
*st
)
2418 if (!st
->cpus
|| !st
->threads
)
2421 if (WARN_ONCE(st
->maps_allocated
, "stats double allocation\n"))
2424 perf_evlist__set_maps(evsel_list
, st
->cpus
, st
->threads
);
2426 if (perf_evlist__alloc_stats(evsel_list
, true))
2429 st
->maps_allocated
= true;
2434 int process_thread_map_event(struct perf_tool
*tool
,
2435 union perf_event
*event
,
2436 struct perf_session
*session __maybe_unused
)
2438 struct perf_stat
*st
= container_of(tool
, struct perf_stat
, tool
);
2441 pr_warning("Extra thread map event, ignoring.\n");
2445 st
->threads
= thread_map__new_event(&event
->thread_map
);
2449 return set_maps(st
);
2453 int process_cpu_map_event(struct perf_tool
*tool
,
2454 union perf_event
*event
,
2455 struct perf_session
*session __maybe_unused
)
2457 struct perf_stat
*st
= container_of(tool
, struct perf_stat
, tool
);
2458 struct cpu_map
*cpus
;
2461 pr_warning("Extra cpu map event, ignoring.\n");
2465 cpus
= cpu_map__new_data(&event
->cpu_map
.data
);
2470 return set_maps(st
);
2473 static const char * const stat_report_usage
[] = {
2474 "perf stat report [<options>]",
2478 static struct perf_stat perf_stat
= {
2480 .attr
= perf_event__process_attr
,
2481 .event_update
= perf_event__process_event_update
,
2482 .thread_map
= process_thread_map_event
,
2483 .cpu_map
= process_cpu_map_event
,
2484 .stat_config
= process_stat_config_event
,
2485 .stat
= perf_event__process_stat_event
,
2486 .stat_round
= process_stat_round_event
,
2488 .aggr_mode
= AGGR_UNSET
,
2491 static int __cmd_report(int argc
, const char **argv
)
2493 struct perf_session
*session
;
2494 const struct option options
[] = {
2495 OPT_STRING('i', "input", &input_name
, "file", "input file name"),
2496 OPT_SET_UINT(0, "per-socket", &perf_stat
.aggr_mode
,
2497 "aggregate counts per processor socket", AGGR_SOCKET
),
2498 OPT_SET_UINT(0, "per-core", &perf_stat
.aggr_mode
,
2499 "aggregate counts per physical processor core", AGGR_CORE
),
2500 OPT_SET_UINT('A', "no-aggr", &perf_stat
.aggr_mode
,
2501 "disable CPU count aggregation", AGGR_NONE
),
2507 argc
= parse_options(argc
, argv
, options
, stat_report_usage
, 0);
2509 if (!input_name
|| !strlen(input_name
)) {
2510 if (!fstat(STDIN_FILENO
, &st
) && S_ISFIFO(st
.st_mode
))
2513 input_name
= "perf.data";
2516 perf_stat
.file
.path
= input_name
;
2517 perf_stat
.file
.mode
= PERF_DATA_MODE_READ
;
2519 session
= perf_session__new(&perf_stat
.file
, false, &perf_stat
.tool
);
2520 if (session
== NULL
)
2523 perf_stat
.session
= session
;
2524 stat_config
.output
= stderr
;
2525 evsel_list
= session
->evlist
;
2527 ret
= perf_session__process_events(session
);
2531 perf_session__delete(session
);
2535 static void setup_system_wide(int forks
)
2538 * Make system wide (-a) the default target if
2539 * no target was specified and one of following
2540 * conditions is met:
2542 * - there's no workload specified
2543 * - there is workload specified but all requested
2544 * events are system wide events
2546 if (!target__none(&target
))
2550 target
.system_wide
= true;
2552 struct perf_evsel
*counter
;
2554 evlist__for_each_entry(evsel_list
, counter
) {
2555 if (!counter
->system_wide
)
2559 if (evsel_list
->nr_entries
)
2560 target
.system_wide
= true;
2564 int cmd_stat(int argc
, const char **argv
)
2566 const char * const stat_usage
[] = {
2567 "perf stat [<options>] [<command>]",
2570 int status
= -EINVAL
, run_idx
;
2572 FILE *output
= stderr
;
2573 unsigned int interval
;
2574 const char * const stat_subcommands
[] = { "record", "report" };
2576 setlocale(LC_ALL
, "");
2578 evsel_list
= perf_evlist__new();
2579 if (evsel_list
== NULL
)
2582 parse_events__shrink_config_terms();
2583 argc
= parse_options_subcommand(argc
, argv
, stat_options
, stat_subcommands
,
2584 (const char **) stat_usage
,
2585 PARSE_OPT_STOP_AT_NON_OPTION
);
2586 perf_stat__collect_metric_expr(evsel_list
);
2587 perf_stat__init_shadow_stats();
2591 if (!strcmp(csv_sep
, "\\t"))
2594 csv_sep
= DEFAULT_SEPARATOR
;
2596 if (argc
&& !strncmp(argv
[0], "rec", 3)) {
2597 argc
= __cmd_record(argc
, argv
);
2600 } else if (argc
&& !strncmp(argv
[0], "rep", 3))
2601 return __cmd_report(argc
, argv
);
2603 interval
= stat_config
.interval
;
2606 * For record command the -o is already taken care of.
2608 if (!STAT_RECORD
&& output_name
&& strcmp(output_name
, "-"))
2611 if (output_name
&& output_fd
) {
2612 fprintf(stderr
, "cannot use both --output and --log-fd\n");
2613 parse_options_usage(stat_usage
, stat_options
, "o", 1);
2614 parse_options_usage(NULL
, stat_options
, "log-fd", 0);
2618 if (metric_only
&& stat_config
.aggr_mode
== AGGR_THREAD
) {
2619 fprintf(stderr
, "--metric-only is not supported with --per-thread\n");
2623 if (metric_only
&& run_count
> 1) {
2624 fprintf(stderr
, "--metric-only is not supported with -r\n");
2628 if (output_fd
< 0) {
2629 fprintf(stderr
, "argument to --log-fd must be a > 0\n");
2630 parse_options_usage(stat_usage
, stat_options
, "log-fd", 0);
2636 mode
= append_file
? "a" : "w";
2638 output
= fopen(output_name
, mode
);
2640 perror("failed to create output file");
2643 clock_gettime(CLOCK_REALTIME
, &tm
);
2644 fprintf(output
, "# started on %s\n", ctime(&tm
.tv_sec
));
2645 } else if (output_fd
> 0) {
2646 mode
= append_file
? "a" : "w";
2647 output
= fdopen(output_fd
, mode
);
2649 perror("Failed opening logfd");
2654 stat_config
.output
= output
;
2657 * let the spreadsheet do the pretty-printing
2660 /* User explicitly passed -B? */
2661 if (big_num_opt
== 1) {
2662 fprintf(stderr
, "-B option not supported with -x\n");
2663 parse_options_usage(stat_usage
, stat_options
, "B", 1);
2664 parse_options_usage(NULL
, stat_options
, "x", 1);
2666 } else /* Nope, so disable big number formatting */
2668 } else if (big_num_opt
== 0) /* User passed --no-big-num */
2671 setup_system_wide(argc
);
2673 if (run_count
< 0) {
2674 pr_err("Run count must be a positive number\n");
2675 parse_options_usage(stat_usage
, stat_options
, "r", 1);
2677 } else if (run_count
== 0) {
2682 if ((stat_config
.aggr_mode
== AGGR_THREAD
) && !target__has_task(&target
)) {
2683 fprintf(stderr
, "The --per-thread option is only available "
2684 "when monitoring via -p -t options.\n");
2685 parse_options_usage(NULL
, stat_options
, "p", 1);
2686 parse_options_usage(NULL
, stat_options
, "t", 1);
2691 * no_aggr, cgroup are for system-wide only
2692 * --per-thread is aggregated per thread, we dont mix it with cpu mode
2694 if (((stat_config
.aggr_mode
!= AGGR_GLOBAL
&&
2695 stat_config
.aggr_mode
!= AGGR_THREAD
) || nr_cgroups
) &&
2696 !target__has_cpu(&target
)) {
2697 fprintf(stderr
, "both cgroup and no-aggregation "
2698 "modes only available in system-wide mode\n");
2700 parse_options_usage(stat_usage
, stat_options
, "G", 1);
2701 parse_options_usage(NULL
, stat_options
, "A", 1);
2702 parse_options_usage(NULL
, stat_options
, "a", 1);
2706 if (add_default_attributes())
2709 target__validate(&target
);
2711 if (perf_evlist__create_maps(evsel_list
, &target
) < 0) {
2712 if (target__has_task(&target
)) {
2713 pr_err("Problems finding threads of monitor\n");
2714 parse_options_usage(stat_usage
, stat_options
, "p", 1);
2715 parse_options_usage(NULL
, stat_options
, "t", 1);
2716 } else if (target__has_cpu(&target
)) {
2717 perror("failed to parse CPUs map");
2718 parse_options_usage(stat_usage
, stat_options
, "C", 1);
2719 parse_options_usage(NULL
, stat_options
, "a", 1);
2725 * Initialize thread_map with comm names,
2726 * so we could print it out on output.
2728 if (stat_config
.aggr_mode
== AGGR_THREAD
)
2729 thread_map__read_comms(evsel_list
->threads
);
2731 if (interval
&& interval
< 100) {
2732 if (interval
< 10) {
2733 pr_err("print interval must be >= 10ms\n");
2734 parse_options_usage(stat_usage
, stat_options
, "I", 1);
2737 pr_warning("print interval < 100ms. "
2738 "The overhead percentage could be high in some cases. "
2739 "Please proceed with caution.\n");
2742 if (perf_evlist__alloc_stats(evsel_list
, interval
))
2745 if (perf_stat_init_aggr_mode())
2749 * We dont want to block the signals - that would cause
2750 * child tasks to inherit that and Ctrl-C would not work.
2751 * What we want is for Ctrl-C to work in the exec()-ed
2752 * task, but being ignored by perf stat itself:
2756 signal(SIGINT
, skip_signal
);
2757 signal(SIGCHLD
, skip_signal
);
2758 signal(SIGALRM
, skip_signal
);
2759 signal(SIGABRT
, skip_signal
);
2762 for (run_idx
= 0; forever
|| run_idx
< run_count
; run_idx
++) {
2763 if (run_count
!= 1 && verbose
> 0)
2764 fprintf(output
, "[ perf stat: executing run #%d ... ]\n",
2767 status
= run_perf_stat(argc
, argv
);
2768 if (forever
&& status
!= -1) {
2769 print_counters(NULL
, argc
, argv
);
2770 perf_stat__reset_stats();
2774 if (!forever
&& status
!= -1 && !interval
)
2775 print_counters(NULL
, argc
, argv
);
2779 * We synthesize the kernel mmap record just so that older tools
2780 * don't emit warnings about not being able to resolve symbols
2781 * due to /proc/sys/kernel/kptr_restrict settings and instear provide
2782 * a saner message about no samples being in the perf.data file.
2784 * This also serves to suppress a warning about f_header.data.size == 0
2785 * in header.c at the moment 'perf stat record' gets introduced, which
2786 * is not really needed once we start adding the stat specific PERF_RECORD_
2787 * records, but the need to suppress the kptr_restrict messages in older
2788 * tools remain -acme
2790 int fd
= perf_data_file__fd(&perf_stat
.file
);
2791 int err
= perf_event__synthesize_kernel_mmap((void *)&perf_stat
,
2792 process_synthesized_event
,
2793 &perf_stat
.session
->machines
.host
);
2795 pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
2796 "older tools may produce warnings about this file\n.");
2800 if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats
.max
, FINAL
))
2801 pr_err("failed to write stat round event\n");
2804 if (!perf_stat
.file
.is_pipe
) {
2805 perf_stat
.session
->header
.data_size
+= perf_stat
.bytes_written
;
2806 perf_session__write_header(perf_stat
.session
, evsel_list
, fd
, true);
2809 perf_session__delete(perf_stat
.session
);
2812 perf_stat__exit_aggr_mode();
2813 perf_evlist__free_stats(evsel_list
);
2815 if (smi_cost
&& smi_reset
)
2816 sysfs__write_int(FREEZE_ON_SMI_PATH
, 0);
2818 perf_evlist__delete(evsel_list
);